2 * Copyright (C) 1995-2011 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"
39 #include "iroptimize.h"
41 #include "dbginfo_t.h"
42 #include "iropt_dbg.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 ir_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 ir_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 ir_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 ir_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 ir_tarval *ta = value_of(a);
129 ir_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 ir_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 ir_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);
174 ir_tarval *ta = value_of(a);
175 ir_tarval *tb = value_of(b);
177 if ((ta != tarval_bad) && (tb != tarval_bad)) {
179 return tarval_carry() ? get_mode_one(m) : get_mode_null(m);
181 if (tarval_is_null(ta) || tarval_is_null(tb))
182 return get_mode_null(m);
185 } /* computed_value_Carry */
188 * Return the value of a Borrow.
191 static ir_tarval *computed_value_Borrow(const ir_node *n)
193 ir_node *a = get_binop_left(n);
194 ir_node *b = get_binop_right(n);
195 ir_mode *m = get_irn_mode(n);
196 ir_tarval *ta = value_of(a);
197 ir_tarval *tb = value_of(b);
199 if ((ta != tarval_bad) && (tb != tarval_bad)) {
200 return tarval_cmp(ta, tb) == pn_Cmp_Lt ? get_mode_one(m) : get_mode_null(m);
201 } else if (tarval_is_null(ta)) {
202 return get_mode_null(m);
205 } /* computed_value_Borrow */
208 * Return the value of an unary Minus.
210 static ir_tarval *computed_value_Minus(const ir_node *n)
212 ir_node *a = get_Minus_op(n);
213 ir_tarval *ta = value_of(a);
215 if (ta != tarval_bad)
216 return tarval_neg(ta);
219 } /* computed_value_Minus */
222 * Return the value of a Mul.
224 static ir_tarval *computed_value_Mul(const ir_node *n)
226 ir_node *a = get_Mul_left(n);
227 ir_node *b = get_Mul_right(n);
228 ir_tarval *ta = value_of(a);
229 ir_tarval *tb = value_of(b);
232 mode = get_irn_mode(n);
233 if (mode != get_irn_mode(a)) {
234 /* n * n = 2n bit multiplication */
235 ta = tarval_convert_to(ta, mode);
236 tb = tarval_convert_to(tb, mode);
239 if (ta != tarval_bad && tb != tarval_bad) {
240 return tarval_mul(ta, tb);
242 /* a * 0 != 0 if a == NaN or a == Inf */
243 if (!mode_is_float(mode)) {
244 /* a*0 = 0 or 0*b = 0 */
245 if (ta == get_mode_null(mode))
247 if (tb == get_mode_null(mode))
252 } /* computed_value_Mul */
255 * Return the value of an And.
256 * Special case: a & 0, 0 & b
258 static ir_tarval *computed_value_And(const ir_node *n)
260 ir_node *a = get_And_left(n);
261 ir_node *b = get_And_right(n);
262 ir_tarval *ta = value_of(a);
263 ir_tarval *tb = value_of(b);
265 if ((ta != tarval_bad) && (tb != tarval_bad)) {
266 return tarval_and (ta, tb);
268 if (tarval_is_null(ta)) return ta;
269 if (tarval_is_null(tb)) return tb;
272 } /* computed_value_And */
275 * Return the value of an Or.
276 * Special case: a | 1...1, 1...1 | b
278 static ir_tarval *computed_value_Or(const ir_node *n)
280 ir_node *a = get_Or_left(n);
281 ir_node *b = get_Or_right(n);
282 ir_tarval *ta = value_of(a);
283 ir_tarval *tb = value_of(b);
285 if ((ta != tarval_bad) && (tb != tarval_bad)) {
286 return tarval_or (ta, tb);
288 if (tarval_is_all_one(ta)) return ta;
289 if (tarval_is_all_one(tb)) return tb;
292 } /* computed_value_Or */
295 * Return the value of an Eor.
297 static ir_tarval *computed_value_Eor(const ir_node *n)
299 ir_node *a = get_Eor_left(n);
300 ir_node *b = get_Eor_right(n);
305 return get_mode_null(get_irn_mode(n));
310 if ((ta != tarval_bad) && (tb != tarval_bad)) {
311 return tarval_eor(ta, tb);
314 } /* computed_value_Eor */
317 * Return the value of a Not.
319 static ir_tarval *computed_value_Not(const ir_node *n)
321 ir_node *a = get_Not_op(n);
322 ir_tarval *ta = value_of(a);
324 if (ta != tarval_bad)
325 return tarval_not(ta);
328 } /* computed_value_Not */
331 * Return the value of a Shl.
333 static ir_tarval *computed_value_Shl(const ir_node *n)
335 ir_node *a = get_Shl_left(n);
336 ir_node *b = get_Shl_right(n);
338 ir_tarval *ta = value_of(a);
339 ir_tarval *tb = value_of(b);
341 if ((ta != tarval_bad) && (tb != tarval_bad)) {
342 return tarval_shl(ta, tb);
345 } /* computed_value_Shl */
348 * Return the value of a Shr.
350 static ir_tarval *computed_value_Shr(const ir_node *n)
352 ir_node *a = get_Shr_left(n);
353 ir_node *b = get_Shr_right(n);
355 ir_tarval *ta = value_of(a);
356 ir_tarval *tb = value_of(b);
358 if ((ta != tarval_bad) && (tb != tarval_bad)) {
359 return tarval_shr(ta, tb);
362 } /* computed_value_Shr */
365 * Return the value of a Shrs.
367 static ir_tarval *computed_value_Shrs(const ir_node *n)
369 ir_node *a = get_Shrs_left(n);
370 ir_node *b = get_Shrs_right(n);
372 ir_tarval *ta = value_of(a);
373 ir_tarval *tb = value_of(b);
375 if ((ta != tarval_bad) && (tb != tarval_bad)) {
376 return tarval_shrs(ta, tb);
379 } /* computed_value_Shrs */
382 * Return the value of a Rotl.
384 static ir_tarval *computed_value_Rotl(const ir_node *n)
386 ir_node *a = get_Rotl_left(n);
387 ir_node *b = get_Rotl_right(n);
389 ir_tarval *ta = value_of(a);
390 ir_tarval *tb = value_of(b);
392 if ((ta != tarval_bad) && (tb != tarval_bad)) {
393 return tarval_rotl(ta, tb);
396 } /* computed_value_Rotl */
399 * Return the value of a Conv.
401 static ir_tarval *computed_value_Conv(const ir_node *n)
403 ir_node *a = get_Conv_op(n);
404 ir_tarval *ta = value_of(a);
406 if (ta != tarval_bad)
407 return tarval_convert_to(ta, get_irn_mode(n));
410 } /* computed_value_Conv */
413 * Calculate the value of a Mux: can be evaluated, if the
414 * sel and the right input are known.
416 static ir_tarval *computed_value_Mux(const ir_node *n)
418 ir_node *sel = get_Mux_sel(n);
419 ir_tarval *ts = value_of(sel);
421 if (ts == get_tarval_b_true()) {
422 ir_node *v = get_Mux_true(n);
425 else if (ts == get_tarval_b_false()) {
426 ir_node *v = get_Mux_false(n);
430 } /* computed_value_Mux */
433 * Calculate the value of a Confirm: can be evaluated,
434 * if it has the form Confirm(x, '=', Const).
436 static ir_tarval *computed_value_Confirm(const ir_node *n)
438 if (get_Confirm_cmp(n) == pn_Cmp_Eq) {
439 ir_tarval *tv = value_of(get_Confirm_bound(n));
440 if (tv != tarval_bad)
443 return value_of(get_Confirm_value(n));
444 } /* computed_value_Confirm */
447 * Return the value of a Proj(Cmp).
449 * This performs a first step of unreachable code elimination.
450 * Proj can not be computed, but folding a Cmp above the Proj here is
451 * not as wasteful as folding a Cmp into a Tuple of 16 Consts of which
453 * There are several case where we can evaluate a Cmp node, see later.
455 static ir_tarval *computed_value_Proj_Cmp(const ir_node *n)
457 ir_node *cmp = get_Proj_pred(n);
458 ir_node *left = get_Cmp_left(cmp);
459 ir_node *right = get_Cmp_right(cmp);
460 pn_Cmp pn_cmp = get_Proj_pn_cmp(n);
461 ir_mode *mode = get_irn_mode(left);
462 ir_tarval *tv_l, *tv_r;
465 * BEWARE: a == a is NOT always True for floating Point values, as
466 * NaN != NaN is defined, so we must check this here.
468 if (left == right && (!mode_is_float(mode) || pn_cmp == pn_Cmp_Lt || pn_cmp == pn_Cmp_Gt)) {
469 /* This is a trick with the bits used for encoding the Cmp
470 Proj numbers, the following statement is not the same:
471 return new_tarval_from_long(pn_cmp == pn_Cmp_Eq, mode_b) */
472 return new_tarval_from_long(pn_cmp & pn_Cmp_Eq, mode_b);
474 tv_l = value_of(left);
475 tv_r = value_of(right);
477 if ((tv_l != tarval_bad) && (tv_r != tarval_bad)) {
479 * The predecessors of Cmp are target values. We can evaluate
482 pn_Cmp flags = tarval_cmp(tv_l, tv_r);
483 if (flags != pn_Cmp_False) {
484 return new_tarval_from_long (pn_cmp & flags, mode_b);
486 } else if (mode_is_int(mode)) {
487 /* for integer values, we can check against MIN/MAX */
490 if (tv_l == get_mode_min(mode)) {
491 /* MIN <=/> x. This results in true/false. */
492 if (pn_cmp == pn_Cmp_Le)
493 return tarval_b_true;
494 else if (pn_cmp == pn_Cmp_Gt)
495 return tarval_b_false;
496 } else if (tv_r == get_mode_min(mode)) {
497 /* x >=/< MIN. This results in true/false. */
498 if (pn_cmp == pn_Cmp_Ge)
499 return tarval_b_true;
500 else if (pn_cmp == pn_Cmp_Lt)
501 return tarval_b_false;
502 } else if (tv_l == get_mode_max(mode)) {
503 /* MAX >=/< x. This results in true/false. */
504 if (pn_cmp == pn_Cmp_Ge)
505 return tarval_b_true;
506 else if (pn_cmp == pn_Cmp_Lt)
507 return tarval_b_false;
508 } else if (tv_r == get_mode_max(mode)) {
509 /* x <=/> MAX. This results in true/false. */
510 if (pn_cmp == pn_Cmp_Le)
511 return tarval_b_true;
512 else if (pn_cmp == pn_Cmp_Gt)
513 return tarval_b_false;
516 cmp_result = vrp_cmp(left, right);
517 if (cmp_result != pn_Cmp_False) {
518 if (cmp_result == pn_Cmp_Lg) {
519 if (pn_cmp == pn_Cmp_Eq) {
520 return tarval_b_false;
521 } else if (pn_cmp == pn_Cmp_Lg) {
522 return tarval_b_true;
525 return new_tarval_from_long(cmp_result & pn_cmp, mode_b);
528 } else if (mode_is_reference(mode)) {
529 /* pointer compare */
530 ir_node *s_l = skip_Proj(left);
531 ir_node *s_r = skip_Proj(right);
533 if ((is_Alloc(s_l) && tarval_is_null(tv_r)) ||
534 (tarval_is_null(tv_l) && is_Alloc(s_r))) {
536 * The predecessors are Allocs and (void*)(0) constants. In Firm Allocs never
537 * return NULL, they raise an exception. Therefore we can predict
540 return new_tarval_from_long(pn_cmp & pn_Cmp_Lg, mode_b);
543 return computed_value_Cmp_Confirm(cmp, left, right, pn_cmp);
544 } /* computed_value_Proj_Cmp */
547 * Calculate the value of an integer Div of two nodes.
548 * Special case: 0 / b
550 static ir_tarval *do_computed_value_Div(const ir_node *a, const ir_node *b)
552 ir_tarval *ta = value_of(a);
554 const ir_node *dummy;
556 /* Compute c1 / c2 or 0 / a, a != 0 */
557 if (tarval_is_null(ta) && value_not_zero(b, &dummy))
558 return ta; /* 0 / b == 0 */
560 if (ta != tarval_bad && tb != tarval_bad)
561 return tarval_div(ta, tb);
563 } /* do_computed_value_Div */
566 * Calculate the value of an integer Mod of two nodes.
567 * Special case: a % 1
569 static ir_tarval *do_computed_value_Mod(const ir_node *a, const ir_node *b)
571 ir_tarval *ta = value_of(a);
572 ir_tarval *tb = value_of(b);
574 /* Compute a % 1 or c1 % c2 */
575 if (tarval_is_one(tb))
576 return get_mode_null(get_irn_mode(a));
577 if (ta != tarval_bad && tb != tarval_bad)
578 return tarval_mod(ta, tb);
580 } /* do_computed_value_Mod */
583 * Return the value of a Proj(Div).
585 static ir_tarval *computed_value_Proj_Div(const ir_node *n)
587 long proj_nr = get_Proj_proj(n);
589 if (proj_nr == pn_Div_res) {
590 const ir_node *a = get_Proj_pred(n);
591 return do_computed_value_Div(get_Div_left(a), get_Div_right(a));
594 } /* computed_value_Proj_Div */
597 * Return the value of a Proj(Mod).
599 static ir_tarval *computed_value_Proj_Mod(const ir_node *n)
601 long proj_nr = get_Proj_proj(n);
603 if (proj_nr == pn_Mod_res) {
604 const ir_node *a = get_Proj_pred(n);
605 return do_computed_value_Mod(get_Mod_left(a), get_Mod_right(a));
608 } /* computed_value_Proj_Mod */
611 * Return the value of a Proj.
613 static ir_tarval *computed_value_Proj(const ir_node *proj)
615 ir_node *n = get_Proj_pred(proj);
617 if (n->op->ops.computed_value_Proj != NULL)
618 return n->op->ops.computed_value_Proj(proj);
620 } /* computed_value_Proj */
623 * If the parameter n can be computed, return its value, else tarval_bad.
624 * Performs constant folding.
626 * @param n The node this should be evaluated
628 ir_tarval *computed_value(const ir_node *n)
630 vrp_attr *vrp = vrp_get_info(n);
631 if (vrp && vrp->valid && tarval_cmp(vrp->bits_set, vrp->bits_not_set) == pn_Cmp_Eq) {
632 return vrp->bits_set;
634 if (n->op->ops.computed_value)
635 return n->op->ops.computed_value(n);
637 } /* computed_value */
640 * Set the default computed_value evaluator in an ir_op_ops.
642 * @param code the opcode for the default operation
643 * @param ops the operations initialized
648 static ir_op_ops *firm_set_default_computed_value(ir_opcode code, ir_op_ops *ops)
652 ops->computed_value = computed_value_##a; \
654 #define CASE_PROJ(a) \
656 ops->computed_value_Proj = computed_value_Proj_##a; \
691 } /* firm_set_default_computed_value */
694 * Returns a equivalent block for another block.
695 * If the block has only one predecessor, this is
696 * the equivalent one. If the only predecessor of a block is
697 * the block itself, this is a dead block.
699 * If both predecessors of a block are the branches of a binary
700 * Cond, the equivalent block is Cond's block.
702 * If all predecessors of a block are bad or lies in a dead
703 * block, the current block is dead as well.
705 * Note, that blocks are NEVER turned into Bad's, instead
706 * the dead_block flag is set. So, never test for is_Bad(block),
707 * always use is_dead_Block(block).
709 static ir_node *equivalent_node_Block(ir_node *n)
715 /* don't optimize dead or labeled blocks */
716 if (is_Block_dead(n) || has_Block_entity(n))
719 n_preds = get_Block_n_cfgpreds(n);
721 /* The Block constructor does not call optimize, but mature_immBlock()
722 calls the optimization. */
723 assert(get_Block_matured(n));
725 irg = get_irn_irg(n);
727 /* Straightening: a single entry Block following a single exit Block
728 can be merged, if it is not the Start block. */
729 /* !!! Beware, all Phi-nodes of n must have been optimized away.
730 This should be true, as the block is matured before optimize is called.
731 But what about Phi-cycles with the Phi0/Id that could not be resolved?
732 Remaining Phi nodes are just Ids. */
734 ir_node *pred = skip_Proj(get_Block_cfgpred(n, 0));
737 ir_node *predblock = get_nodes_block(pred);
738 if (predblock == oldn) {
739 /* Jmp jumps into the block it is in -- deal self cycle. */
740 n = set_Block_dead(n);
741 DBG_OPT_DEAD_BLOCK(oldn, n);
744 DBG_OPT_STG(oldn, n);
746 } else if (is_Cond(pred)) {
747 ir_node *predblock = get_nodes_block(pred);
748 if (predblock == oldn) {
749 /* Jmp jumps into the block it is in -- deal self cycle. */
750 n = set_Block_dead(n);
751 DBG_OPT_DEAD_BLOCK(oldn, n);
754 } else if (n_preds == 2) {
755 /* Test whether Cond jumps twice to this block
756 * The more general case which more than 2 predecessors is handles
757 * in optimize_cf(), we handle only this special case for speed here.
759 ir_node *a = get_Block_cfgpred(n, 0);
760 ir_node *b = get_Block_cfgpred(n, 1);
762 if (is_Proj(a) && is_Proj(b)) {
763 ir_node *cond = get_Proj_pred(a);
765 if (cond == get_Proj_pred(b) && is_Cond(cond) &&
766 get_irn_mode(get_Cond_selector(cond)) == mode_b) {
767 /* Also a single entry Block following a single exit Block. Phis have
768 twice the same operand and will be optimized away. */
769 n = get_nodes_block(cond);
770 DBG_OPT_IFSIM1(oldn, a, b, n);
773 } else if (get_opt_unreachable_code() &&
774 (n != get_irg_start_block(irg)) &&
775 (n != get_irg_end_block(irg))) {
778 /* If all inputs are dead, this block is dead too, except if it is
779 the start or end block. This is one step of unreachable code
781 for (i = get_Block_n_cfgpreds(n) - 1; i >= 0; --i) {
782 ir_node *pred = get_Block_cfgpred(n, i);
785 if (is_Bad(pred)) continue;
786 pred_blk = get_nodes_block(skip_Proj(pred));
788 if (is_Block_dead(pred_blk)) continue;
791 /* really found a living input */
796 n = set_Block_dead(n);
797 DBG_OPT_DEAD_BLOCK(oldn, n);
802 } /* equivalent_node_Block */
805 * Returns a equivalent node for a Jmp, a Bad :-)
806 * Of course this only happens if the Block of the Jmp is dead.
808 static ir_node *equivalent_node_Jmp(ir_node *n)
812 /* unreachable code elimination */
813 if (is_Block_dead(get_nodes_block(n))) {
814 ir_graph *irg = get_irn_irg(n);
815 n = get_irg_bad(irg);
816 DBG_OPT_DEAD_BLOCK(oldn, n);
819 } /* equivalent_node_Jmp */
821 /** Raise is handled in the same way as Jmp. */
822 #define equivalent_node_Raise equivalent_node_Jmp
825 /* We do not evaluate Cond here as we replace it by a new node, a Jmp.
826 See transform_node_Proj_Cond(). */
829 * Optimize operations that are commutative and have neutral 0,
830 * so a op 0 = 0 op a = a.
832 static ir_node *equivalent_node_neutral_zero(ir_node *n)
836 ir_node *a = get_binop_left(n);
837 ir_node *b = get_binop_right(n);
842 /* After running compute_node there is only one constant predecessor.
843 Find this predecessors value and remember the other node: */
844 if ((tv = value_of(a)) != tarval_bad) {
846 } else if ((tv = value_of(b)) != tarval_bad) {
851 /* If this predecessors constant value is zero, the operation is
852 * unnecessary. Remove it.
854 * Beware: If n is a Add, the mode of on and n might be different
855 * which happens in this rare construction: NULL + 3.
856 * Then, a Conv would be needed which we cannot include here.
858 if (tarval_is_null(tv) && get_irn_mode(on) == get_irn_mode(n)) {
861 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_0);
865 } /* equivalent_node_neutral_zero */
868 * Eor is commutative and has neutral 0.
870 static ir_node *equivalent_node_Eor(ir_node *n)
876 n = equivalent_node_neutral_zero(n);
877 if (n != oldn) return n;
880 b = get_Eor_right(n);
883 ir_node *aa = get_Eor_left(a);
884 ir_node *ab = get_Eor_right(a);
887 /* (a ^ b) ^ a -> b */
889 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_EOR_A_B_A);
891 } else if (ab == b) {
892 /* (a ^ b) ^ b -> a */
894 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_EOR_A_B_A);
899 ir_node *ba = get_Eor_left(b);
900 ir_node *bb = get_Eor_right(b);
903 /* a ^ (a ^ b) -> b */
905 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_EOR_A_B_A);
907 } else if (bb == a) {
908 /* a ^ (b ^ a) -> b */
910 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_EOR_A_B_A);
918 * Optimize a - 0 and (a - x) + x (for modes with wrap-around).
920 * The second one looks strange, but this construct
921 * is used heavily in the LCC sources :-).
923 * Beware: The Mode of an Add may be different than the mode of its
924 * predecessors, so we could not return a predecessors in all cases.
926 static ir_node *equivalent_node_Add(ir_node *n)
929 ir_node *left, *right;
930 ir_mode *mode = get_irn_mode(n);
932 n = equivalent_node_neutral_zero(n);
936 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
937 if (mode_is_float(mode)) {
938 ir_graph *irg = get_irn_irg(n);
939 if (get_irg_fp_model(irg) & fp_strict_algebraic)
943 left = get_Add_left(n);
944 right = get_Add_right(n);
947 if (get_Sub_right(left) == right) {
950 n = get_Sub_left(left);
951 if (mode == get_irn_mode(n)) {
952 DBG_OPT_ALGSIM1(oldn, left, right, n, FS_OPT_ADD_SUB);
958 if (get_Sub_right(right) == left) {
961 n = get_Sub_left(right);
962 if (mode == get_irn_mode(n)) {
963 DBG_OPT_ALGSIM1(oldn, left, right, n, FS_OPT_ADD_SUB);
969 } /* equivalent_node_Add */
972 * optimize operations that are not commutative but have neutral 0 on left,
975 static ir_node *equivalent_node_left_zero(ir_node *n)
979 ir_node *a = get_binop_left(n);
980 ir_node *b = get_binop_right(n);
981 ir_tarval *tb = value_of(b);
983 if (tarval_is_null(tb)) {
986 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_0);
989 } /* equivalent_node_left_zero */
991 #define equivalent_node_Shl equivalent_node_left_zero
992 #define equivalent_node_Shr equivalent_node_left_zero
993 #define equivalent_node_Shrs equivalent_node_left_zero
994 #define equivalent_node_Rotl equivalent_node_left_zero
997 * Optimize a - 0 and (a + x) - x (for modes with wrap-around).
999 * The second one looks strange, but this construct
1000 * is used heavily in the LCC sources :-).
1002 * Beware: The Mode of a Sub may be different than the mode of its
1003 * predecessors, so we could not return a predecessors in all cases.
1005 static ir_node *equivalent_node_Sub(ir_node *n)
1009 ir_mode *mode = get_irn_mode(n);
1012 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
1013 if (mode_is_float(mode)) {
1014 ir_graph *irg = get_irn_irg(n);
1015 if (get_irg_fp_model(irg) & fp_strict_algebraic)
1019 b = get_Sub_right(n);
1022 /* Beware: modes might be different */
1023 if (tarval_is_null(tb)) {
1024 ir_node *a = get_Sub_left(n);
1025 if (mode == get_irn_mode(a)) {
1028 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_0);
1032 } /* equivalent_node_Sub */
1036 * Optimize an "self-inverse unary op", ie op(op(n)) = n.
1039 * -(-a) == a, but might overflow two times.
1040 * We handle it anyway here but the better way would be a
1041 * flag. This would be needed for Pascal for instance.
1043 static ir_node *equivalent_node_idempotent_unop(ir_node *n)
1046 ir_node *pred = get_unop_op(n);
1048 /* optimize symmetric unop */
1049 if (get_irn_op(pred) == get_irn_op(n)) {
1050 n = get_unop_op(pred);
1051 DBG_OPT_ALGSIM2(oldn, pred, n, FS_OPT_IDEM_UNARY);
1054 } /* equivalent_node_idempotent_unop */
1056 /** Optimize Not(Not(x)) == x. */
1057 #define equivalent_node_Not equivalent_node_idempotent_unop
1059 /** -(-x) == x ??? Is this possible or can --x raise an
1060 out of bounds exception if min =! max? */
1061 #define equivalent_node_Minus equivalent_node_idempotent_unop
1064 * Optimize a * 1 = 1 * a = a.
1066 static ir_node *equivalent_node_Mul(ir_node *n)
1069 ir_node *a = get_Mul_left(n);
1071 /* we can handle here only the n * n = n bit cases */
1072 if (get_irn_mode(n) == get_irn_mode(a)) {
1073 ir_node *b = get_Mul_right(n);
1077 * Mul is commutative and has again an other neutral element.
1078 * Constants are place right, so check this case first.
1081 if (tarval_is_one(tv)) {
1083 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
1086 if (tarval_is_one(tv)) {
1088 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
1093 } /* equivalent_node_Mul */
1096 * Use algebraic simplification a | a = a | 0 = 0 | a = a.
1098 static ir_node *equivalent_node_Or(ir_node *n)
1102 ir_node *a = get_Or_left(n);
1103 ir_node *b = get_Or_right(n);
1107 n = a; /* Or has it's own neutral element */
1108 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_OR);
1111 /* constants are cormalized to right, check this site first */
1113 if (tarval_is_null(tv)) {
1115 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_OR);
1119 if (tarval_is_null(tv)) {
1121 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_OR);
1126 } /* equivalent_node_Or */
1129 * Optimize a & 0b1...1 = 0b1...1 & a = a & a = (a|X) & a = a.
1131 static ir_node *equivalent_node_And(ir_node *n)
1135 ir_node *a = get_And_left(n);
1136 ir_node *b = get_And_right(n);
1140 n = a; /* And has it's own neutral element */
1141 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_AND);
1144 /* constants are normalized to right, check this site first */
1146 if (tarval_is_all_one(tv)) {
1148 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1151 if (tv != get_tarval_bad()) {
1152 ir_mode *mode = get_irn_mode(n);
1153 if (!mode_is_signed(mode) && is_Conv(a)) {
1154 ir_node *convop = get_Conv_op(a);
1155 ir_mode *convopmode = get_irn_mode(convop);
1156 if (!mode_is_signed(convopmode)) {
1157 if (tarval_is_all_one(tarval_convert_to(tv, convopmode))) {
1158 /* Conv(X) & all_one(mode(X)) = Conv(X) */
1160 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1167 if (tarval_is_all_one(tv)) {
1169 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1173 if (b == get_Or_left(a) || b == get_Or_right(a)) {
1176 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1181 if (a == get_Or_left(b) || a == get_Or_right(b)) {
1184 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1189 } /* equivalent_node_And */
1192 * Try to remove useless Conv's:
1194 static ir_node *equivalent_node_Conv(ir_node *n)
1197 ir_node *a = get_Conv_op(n);
1199 ir_mode *n_mode = get_irn_mode(n);
1200 ir_mode *a_mode = get_irn_mode(a);
1203 if (n_mode == a_mode) { /* No Conv necessary */
1204 if (get_Conv_strict(n)) {
1207 /* neither Minus nor Confirm change the precision,
1208 so we can "look-through" */
1211 p = get_Minus_op(p);
1212 } else if (is_Confirm(p)) {
1213 p = get_Confirm_value(p);
1219 if (is_Conv(p) && get_Conv_strict(p)) {
1220 /* we known already, that a_mode == n_mode, and neither
1221 Minus change the mode, so the second Conv
1223 assert(get_irn_mode(p) == n_mode);
1225 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CONV);
1229 ir_node *pred = get_Proj_pred(p);
1230 if (is_Load(pred)) {
1231 /* Loads always return with the exact precision of n_mode */
1232 assert(get_Load_mode(pred) == n_mode);
1234 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CONV);
1237 if (is_Proj(pred) && get_Proj_proj(pred) == pn_Start_T_args) {
1238 pred = get_Proj_pred(pred);
1239 if (is_Start(pred)) {
1240 /* Arguments always return with the exact precision,
1241 as strictConv's are place before Call -- if the
1242 caller was compiled with the same setting.
1243 Otherwise, the semantics is probably still right. */
1244 assert(get_irn_mode(p) == n_mode);
1246 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CONV);
1252 /* special case: the immediate predecessor is also a Conv */
1253 if (! get_Conv_strict(a)) {
1254 /* first one is not strict, kick it */
1256 a_mode = get_irn_mode(a);
1260 /* else both are strict conv, second is superfluous */
1262 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CONV);
1267 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CONV);
1270 } else if (is_Conv(a)) { /* Conv(Conv(b)) */
1271 ir_node *b = get_Conv_op(a);
1272 ir_mode *b_mode = get_irn_mode(b);
1274 if (get_Conv_strict(n) && get_Conv_strict(a)) {
1275 /* both are strict conv */
1276 if (smaller_mode(a_mode, n_mode)) {
1277 /* both are strict, but the first is smaller, so
1278 the second cannot remove more precision, remove the
1280 set_Conv_strict(n, 0);
1283 if (n_mode == b_mode) {
1284 if (! get_Conv_strict(n) && ! get_Conv_strict(a)) {
1285 if (n_mode == mode_b) {
1286 n = b; /* Convb(Conv*(xxxb(...))) == xxxb(...) */
1287 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
1289 } else if (get_mode_arithmetic(n_mode) == get_mode_arithmetic(a_mode)) {
1290 if (values_in_mode(b_mode, a_mode)) {
1291 n = b; /* ConvS(ConvL(xxxS(...))) == xxxS(...) */
1292 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
1297 if (mode_is_int(n_mode) && get_mode_arithmetic(a_mode) == irma_ieee754) {
1298 /* ConvI(ConvF(I)) -> I, iff float mantissa >= int mode */
1299 unsigned int_mantissa = get_mode_size_bits(n_mode) - (mode_is_signed(n_mode) ? 1 : 0);
1300 unsigned float_mantissa = tarval_ieee754_get_mantissa_size(a_mode);
1302 if (float_mantissa >= int_mantissa) {
1304 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
1309 if (smaller_mode(b_mode, a_mode)) {
1310 if (get_Conv_strict(n))
1311 set_Conv_strict(b, 1);
1312 n = b; /* ConvA(ConvB(ConvA(...))) == ConvA(...) */
1313 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
1320 } /* equivalent_node_Conv */
1323 * - fold Phi-nodes, iff they have only one predecessor except
1326 static ir_node *equivalent_node_Phi(ir_node *n)
1332 ir_node *first_val = NULL; /* to shutup gcc */
1334 if (!get_opt_optimize() &&
1335 get_irg_phase_state(get_irn_irg(n)) != phase_building)
1338 n_preds = get_Phi_n_preds(n);
1340 block = get_nodes_block(n);
1342 if (is_Block_dead(block)) {
1343 ir_graph *irg = get_irn_irg(n);
1344 return get_irg_bad(irg);
1347 if (n_preds == 0) return n; /* Phi of dead Region without predecessors. */
1349 /* Find first non-self-referencing input */
1350 for (i = 0; i < n_preds; ++i) {
1351 first_val = get_Phi_pred(n, i);
1352 if ( (first_val != n) /* not self pointer */
1354 /* BEWARE: when the if is changed to 1, Phi's will ignore it's Bad
1355 * predecessors. Then, Phi nodes in dead code might be removed, causing
1356 * nodes pointing to themself (Add's for instance).
1357 * This is really bad and causes endless recursions in several
1358 * code pathes, so we do NOT optimize such a code.
1359 * This is not that bad as it sounds, optimize_cf() removes bad control flow
1360 * (and bad Phi predecessors), so live code is optimized later.
1362 && (! is_Bad(get_Block_cfgpred(block, i)))
1364 ) { /* value not dead */
1365 break; /* then found first value. */
1370 ir_graph *irg = get_irn_irg(n);
1371 /* A totally Bad or self-referencing Phi (we didn't break the above loop) */
1372 return get_irg_bad(irg);
1375 /* search for rest of inputs, determine if any of these
1376 are non-self-referencing */
1377 while (++i < n_preds) {
1378 ir_node *scnd_val = get_Phi_pred(n, i);
1379 if ( (scnd_val != n)
1380 && (scnd_val != first_val)
1383 && (! is_Bad(get_Block_cfgpred(block, i)))
1390 if (i >= n_preds && !is_Dummy(first_val)) {
1391 /* Fold, if no multiple distinct non-self-referencing inputs */
1393 DBG_OPT_PHI(oldn, n);
1396 } /* equivalent_node_Phi */
1399 * Several optimizations:
1400 * - fold Sync-nodes, iff they have only one predecessor except
1403 static ir_node *equivalent_node_Sync(ir_node *n)
1405 int arity = get_Sync_n_preds(n);
1408 for (i = 0; i < arity;) {
1409 ir_node *pred = get_Sync_pred(n, i);
1412 /* Remove Bad predecessors */
1419 /* Remove duplicate predecessors */
1425 if (get_Sync_pred(n, j) == pred) {
1434 ir_graph *irg = get_irn_irg(n);
1435 return get_irg_bad(irg);
1437 if (arity == 1) return get_Sync_pred(n, 0);
1439 } /* equivalent_node_Sync */
1442 * Optimize Proj(Tuple).
1444 static ir_node *equivalent_node_Proj_Tuple(ir_node *proj)
1446 ir_node *oldn = proj;
1447 ir_node *tuple = get_Proj_pred(proj);
1449 /* Remove the Tuple/Proj combination. */
1450 proj = get_Tuple_pred(tuple, get_Proj_proj(proj));
1451 DBG_OPT_TUPLE(oldn, tuple, proj);
1454 } /* equivalent_node_Proj_Tuple */
1457 * Optimize a / 1 = a.
1459 static ir_node *equivalent_node_Proj_Div(ir_node *proj)
1461 ir_node *oldn = proj;
1462 ir_node *div = get_Proj_pred(proj);
1463 ir_node *b = get_Div_right(div);
1464 ir_tarval *tb = value_of(b);
1466 /* Div is not commutative. */
1467 if (tarval_is_one(tb)) { /* div(x, 1) == x */
1468 switch (get_Proj_proj(proj)) {
1470 proj = get_Div_mem(div);
1471 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NEUTRAL_1);
1475 proj = get_Div_left(div);
1476 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NEUTRAL_1);
1480 /* we cannot replace the exception Proj's here, this is done in
1481 transform_node_Proj_Div() */
1486 } /* equivalent_node_Proj_Div */
1489 * Optimize CopyB(mem, x, x) into a Nop.
1491 static ir_node *equivalent_node_Proj_CopyB(ir_node *proj)
1493 ir_node *oldn = proj;
1494 ir_node *copyb = get_Proj_pred(proj);
1495 ir_node *a = get_CopyB_dst(copyb);
1496 ir_node *b = get_CopyB_src(copyb);
1499 /* Turn CopyB into a tuple (mem, jmp, bad, bad) */
1500 switch (get_Proj_proj(proj)) {
1502 proj = get_CopyB_mem(copyb);
1503 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NOP);
1506 case pn_CopyB_X_except: {
1507 ir_graph *irg = get_irn_irg(proj);
1508 DBG_OPT_EXC_REM(proj);
1509 proj = get_irg_bad(irg);
1515 } /* equivalent_node_Proj_CopyB */
1518 * Optimize Bounds(idx, idx, upper) into idx.
1520 static ir_node *equivalent_node_Proj_Bound(ir_node *proj)
1522 ir_node *oldn = proj;
1523 ir_node *bound = get_Proj_pred(proj);
1524 ir_node *idx = get_Bound_index(bound);
1525 ir_node *pred = skip_Proj(idx);
1528 if (idx == get_Bound_lower(bound))
1530 else if (is_Bound(pred)) {
1532 * idx was Bounds checked previously, it is still valid if
1533 * lower <= pred_lower && pred_upper <= upper.
1535 ir_node *lower = get_Bound_lower(bound);
1536 ir_node *upper = get_Bound_upper(bound);
1537 if (get_Bound_lower(pred) == lower &&
1538 get_Bound_upper(pred) == upper) {
1540 * One could expect that we simply return the previous
1541 * Bound here. However, this would be wrong, as we could
1542 * add an exception Proj to a new location then.
1543 * So, we must turn in into a tuple.
1549 /* Turn Bound into a tuple (mem, jmp, bad, idx) */
1550 switch (get_Proj_proj(proj)) {
1552 DBG_OPT_EXC_REM(proj);
1553 proj = get_Bound_mem(bound);
1555 case pn_Bound_X_except: {
1556 ir_graph *irg = get_irn_irg(proj);
1557 DBG_OPT_EXC_REM(proj);
1558 proj = get_irg_bad(irg);
1563 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NOP);
1566 /* cannot optimize pn_Bound_X_regular, handled in transform ... */
1571 } /* equivalent_node_Proj_Bound */
1574 * Optimize an Exception Proj(Load) with a non-null address.
1576 static ir_node *equivalent_node_Proj_Load(ir_node *proj)
1578 if (get_opt_ldst_only_null_ptr_exceptions()) {
1579 if (get_irn_mode(proj) == mode_X) {
1580 ir_node *load = get_Proj_pred(proj);
1582 /* get the Load address */
1583 const ir_node *addr = get_Load_ptr(load);
1584 const ir_node *confirm;
1586 if (value_not_null(addr, &confirm)) {
1587 if (get_Proj_proj(proj) == pn_Load_X_except) {
1588 ir_graph *irg = get_irn_irg(proj);
1589 DBG_OPT_EXC_REM(proj);
1590 return get_irg_bad(irg);
1596 } /* equivalent_node_Proj_Load */
1599 * Optimize an Exception Proj(Store) with a non-null address.
1601 static ir_node *equivalent_node_Proj_Store(ir_node *proj)
1603 if (get_opt_ldst_only_null_ptr_exceptions()) {
1604 if (get_irn_mode(proj) == mode_X) {
1605 ir_node *store = get_Proj_pred(proj);
1607 /* get the load/store address */
1608 const ir_node *addr = get_Store_ptr(store);
1609 const ir_node *confirm;
1611 if (value_not_null(addr, &confirm)) {
1612 if (get_Proj_proj(proj) == pn_Store_X_except) {
1613 ir_graph *irg = get_irn_irg(proj);
1614 DBG_OPT_EXC_REM(proj);
1615 return get_irg_bad(irg);
1621 } /* equivalent_node_Proj_Store */
1624 * Does all optimizations on nodes that must be done on it's Proj's
1625 * because of creating new nodes.
1627 static ir_node *equivalent_node_Proj(ir_node *proj)
1629 ir_node *n = get_Proj_pred(proj);
1631 if (get_irn_mode(proj) == mode_X) {
1632 if (is_Block_dead(get_nodes_block(n))) {
1633 /* Remove dead control flow -- early gigo(). */
1634 ir_graph *irg = get_irn_irg(proj);
1635 return get_irg_bad(irg);
1638 if (n->op->ops.equivalent_node_Proj)
1639 return n->op->ops.equivalent_node_Proj(proj);
1641 } /* equivalent_node_Proj */
1646 static ir_node *equivalent_node_Id(ir_node *n)
1654 DBG_OPT_ID(oldn, n);
1656 } /* equivalent_node_Id */
1661 static ir_node *equivalent_node_Mux(ir_node *n)
1663 ir_node *oldn = n, *sel = get_Mux_sel(n);
1665 ir_tarval *ts = value_of(sel);
1667 /* Mux(true, f, t) == t */
1668 if (ts == tarval_b_true) {
1669 n = get_Mux_true(n);
1670 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_C);
1673 /* Mux(false, f, t) == f */
1674 if (ts == tarval_b_false) {
1675 n = get_Mux_false(n);
1676 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_C);
1679 n_t = get_Mux_true(n);
1680 n_f = get_Mux_false(n);
1682 /* Mux(v, x, T) == x */
1683 if (is_Unknown(n_f)) {
1685 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_EQ);
1688 /* Mux(v, T, x) == x */
1689 if (is_Unknown(n_t)) {
1691 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_EQ);
1695 /* Mux(v, x, x) == x */
1698 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_EQ);
1701 if (is_Proj(sel) && !mode_honor_signed_zeros(get_irn_mode(n))) {
1702 ir_node *cmp = get_Proj_pred(sel);
1703 long proj_nr = get_Proj_proj(sel);
1704 ir_node *f = get_Mux_false(n);
1705 ir_node *t = get_Mux_true(n);
1708 * Note further that these optimization work even for floating point
1709 * with NaN's because -NaN == NaN.
1710 * However, if +0 and -0 is handled differently, we cannot use the first one.
1713 ir_node *const cmp_l = get_Cmp_left(cmp);
1714 ir_node *const cmp_r = get_Cmp_right(cmp);
1718 if ((cmp_l == t && cmp_r == f) || /* Mux(t == f, t, f) -> f */
1719 (cmp_l == f && cmp_r == t)) { /* Mux(f == t, t, f) -> f */
1721 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1728 if ((cmp_l == t && cmp_r == f) || /* Mux(t != f, t, f) -> t */
1729 (cmp_l == f && cmp_r == t)) { /* Mux(f != t, t, f) -> t */
1731 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1738 * Note: normalization puts the constant on the right side,
1739 * so we check only one case.
1741 if (cmp_l == t && tarval_is_null(value_of(cmp_r))) {
1742 /* Mux(t CMP 0, X, t) */
1743 if (is_Minus(f) && get_Minus_op(f) == t) {
1744 /* Mux(t CMP 0, -t, t) */
1745 if (proj_nr == pn_Cmp_Eq) {
1746 /* Mux(t == 0, -t, t) ==> -t */
1748 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1749 } else if (proj_nr == pn_Cmp_Lg || proj_nr == pn_Cmp_Ne) {
1750 /* Mux(t != 0, -t, t) ==> t */
1752 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1759 } /* equivalent_node_Mux */
1762 * Remove Confirm nodes if setting is on.
1763 * Replace Confirms(x, '=', Constlike) by Constlike.
1765 static ir_node *equivalent_node_Confirm(ir_node *n)
1767 ir_node *pred = get_Confirm_value(n);
1768 pn_Cmp pnc = get_Confirm_cmp(n);
1770 while (is_Confirm(pred) && pnc == get_Confirm_cmp(pred)) {
1772 * rare case: two identical Confirms one after another,
1773 * replace the second one with the first.
1776 pred = get_Confirm_value(n);
1777 pnc = get_Confirm_cmp(n);
1783 * equivalent_node() returns a node equivalent to input n. It skips all nodes that
1784 * perform no actual computation, as, e.g., the Id nodes. It does not create
1785 * new nodes. It is therefore safe to free n if the node returned is not n.
1786 * If a node returns a Tuple we can not just skip it. If the size of the
1787 * in array fits, we transform n into a tuple (e.g., Div).
1789 ir_node *equivalent_node(ir_node *n)
1791 if (n->op->ops.equivalent_node)
1792 return n->op->ops.equivalent_node(n);
1794 } /* equivalent_node */
1797 * Sets the default equivalent node operation for an ir_op_ops.
1799 * @param code the opcode for the default operation
1800 * @param ops the operations initialized
1805 static ir_op_ops *firm_set_default_equivalent_node(ir_opcode code, ir_op_ops *ops)
1809 ops->equivalent_node = equivalent_node_##a; \
1811 #define CASE_PROJ(a) \
1813 ops->equivalent_node_Proj = equivalent_node_Proj_##a; \
1853 } /* firm_set_default_equivalent_node */
1856 * Returns non-zero if a node is a Phi node
1857 * with all predecessors constant.
1859 static int is_const_Phi(ir_node *n)
1863 if (! is_Phi(n) || get_irn_arity(n) == 0)
1865 for (i = get_irn_arity(n) - 1; i >= 0; --i) {
1866 if (! is_Const(get_irn_n(n, i)))
1870 } /* is_const_Phi */
1872 typedef ir_tarval *(*tarval_sub_type)(ir_tarval *a, ir_tarval *b, ir_mode *mode);
1873 typedef ir_tarval *(*tarval_binop_type)(ir_tarval *a, ir_tarval *b);
1876 * in reality eval_func should be tarval (*eval_func)() but incomplete
1877 * declarations are bad style and generate noisy warnings
1879 typedef void (*eval_func)(void);
1882 * Wrapper for the tarval binop evaluation, tarval_sub has one more parameter.
1884 static ir_tarval *do_eval(eval_func eval, ir_tarval *a, ir_tarval *b, ir_mode *mode)
1886 if (eval == (eval_func) tarval_sub) {
1887 tarval_sub_type func = (tarval_sub_type)eval;
1889 return func(a, b, mode);
1891 tarval_binop_type func = (tarval_binop_type)eval;
1898 * Apply an evaluator on a binop with a constant operators (and one Phi).
1900 * @param phi the Phi node
1901 * @param other the other operand
1902 * @param eval an evaluator function
1903 * @param mode the mode of the result, may be different from the mode of the Phi!
1904 * @param left if non-zero, other is the left operand, else the right
1906 * @return a new Phi node if the conversion was successful, NULL else
1908 static ir_node *apply_binop_on_phi(ir_node *phi, ir_tarval *other, eval_func eval, ir_mode *mode, int left)
1914 int i, n = get_irn_arity(phi);
1916 NEW_ARR_A(void *, res, n);
1918 for (i = 0; i < n; ++i) {
1919 pred = get_irn_n(phi, i);
1920 tv = get_Const_tarval(pred);
1921 tv = do_eval(eval, other, tv, mode);
1923 if (tv == tarval_bad) {
1924 /* folding failed, bad */
1930 for (i = 0; i < n; ++i) {
1931 pred = get_irn_n(phi, i);
1932 tv = get_Const_tarval(pred);
1933 tv = do_eval(eval, tv, other, mode);
1935 if (tv == tarval_bad) {
1936 /* folding failed, bad */
1942 irg = get_irn_irg(phi);
1943 for (i = 0; i < n; ++i) {
1944 pred = get_irn_n(phi, i);
1945 res[i] = new_r_Const(irg, (ir_tarval*)res[i]);
1947 return new_r_Phi(get_nodes_block(phi), n, (ir_node **)res, mode);
1948 } /* apply_binop_on_phi */
1951 * Apply an evaluator on a binop with two constant Phi.
1953 * @param a the left Phi node
1954 * @param b the right Phi node
1955 * @param eval an evaluator function
1956 * @param mode the mode of the result, may be different from the mode of the Phi!
1958 * @return a new Phi node if the conversion was successful, NULL else
1960 static ir_node *apply_binop_on_2_phis(ir_node *a, ir_node *b, eval_func eval, ir_mode *mode)
1962 ir_tarval *tv_l, *tv_r, *tv;
1968 if (get_nodes_block(a) != get_nodes_block(b))
1971 n = get_irn_arity(a);
1972 NEW_ARR_A(void *, res, n);
1974 for (i = 0; i < n; ++i) {
1975 pred = get_irn_n(a, i);
1976 tv_l = get_Const_tarval(pred);
1977 pred = get_irn_n(b, i);
1978 tv_r = get_Const_tarval(pred);
1979 tv = do_eval(eval, tv_l, tv_r, mode);
1981 if (tv == tarval_bad) {
1982 /* folding failed, bad */
1987 irg = get_irn_irg(a);
1988 for (i = 0; i < n; ++i) {
1989 pred = get_irn_n(a, i);
1990 res[i] = new_r_Const(irg, (ir_tarval*)res[i]);
1992 return new_r_Phi(get_nodes_block(a), n, (ir_node **)res, mode);
1993 } /* apply_binop_on_2_phis */
1996 * Apply an evaluator on a unop with a constant operator (a Phi).
1998 * @param phi the Phi node
1999 * @param eval an evaluator function
2001 * @return a new Phi node if the conversion was successful, NULL else
2003 static ir_node *apply_unop_on_phi(ir_node *phi, ir_tarval *(*eval)(ir_tarval *))
2010 int i, n = get_irn_arity(phi);
2012 NEW_ARR_A(void *, res, n);
2013 for (i = 0; i < n; ++i) {
2014 pred = get_irn_n(phi, i);
2015 tv = get_Const_tarval(pred);
2018 if (tv == tarval_bad) {
2019 /* folding failed, bad */
2024 mode = get_irn_mode(phi);
2025 irg = get_irn_irg(phi);
2026 for (i = 0; i < n; ++i) {
2027 pred = get_irn_n(phi, i);
2028 res[i] = new_r_Const(irg, (ir_tarval*)res[i]);
2030 return new_r_Phi(get_nodes_block(phi), n, (ir_node **)res, mode);
2031 } /* apply_unop_on_phi */
2034 * Apply a conversion on a constant operator (a Phi).
2036 * @param phi the Phi node
2038 * @return a new Phi node if the conversion was successful, NULL else
2040 static ir_node *apply_conv_on_phi(ir_node *phi, ir_mode *mode)
2046 int i, n = get_irn_arity(phi);
2048 NEW_ARR_A(void *, res, n);
2049 for (i = 0; i < n; ++i) {
2050 pred = get_irn_n(phi, i);
2051 tv = get_Const_tarval(pred);
2052 tv = tarval_convert_to(tv, mode);
2054 if (tv == tarval_bad) {
2055 /* folding failed, bad */
2060 irg = get_irn_irg(phi);
2061 for (i = 0; i < n; ++i) {
2062 pred = get_irn_n(phi, i);
2063 res[i] = new_r_Const(irg, (ir_tarval*)res[i]);
2065 return new_r_Phi(get_nodes_block(phi), n, (ir_node **)res, mode);
2066 } /* apply_conv_on_phi */
2069 * Transform AddP(P, ConvIs(Iu)), AddP(P, ConvIu(Is)) and
2070 * SubP(P, ConvIs(Iu)), SubP(P, ConvIu(Is)).
2071 * If possible, remove the Conv's.
2073 static ir_node *transform_node_AddSub(ir_node *n)
2075 ir_mode *mode = get_irn_mode(n);
2077 if (mode_is_reference(mode)) {
2078 ir_node *left = get_binop_left(n);
2079 ir_node *right = get_binop_right(n);
2080 unsigned ref_bits = get_mode_size_bits(mode);
2082 if (is_Conv(left)) {
2083 ir_mode *lmode = get_irn_mode(left);
2084 unsigned bits = get_mode_size_bits(lmode);
2086 if (ref_bits == bits &&
2087 mode_is_int(lmode) &&
2088 get_mode_arithmetic(lmode) == irma_twos_complement) {
2089 ir_node *pre = get_Conv_op(left);
2090 ir_mode *pre_mode = get_irn_mode(pre);
2092 if (mode_is_int(pre_mode) &&
2093 get_mode_size_bits(pre_mode) == bits &&
2094 get_mode_arithmetic(pre_mode) == irma_twos_complement) {
2095 /* ok, this conv just changes to sign, moreover the calculation
2096 * is done with same number of bits as our address mode, so
2097 * we can ignore the conv as address calculation can be viewed
2098 * as either signed or unsigned
2100 set_binop_left(n, pre);
2105 if (is_Conv(right)) {
2106 ir_mode *rmode = get_irn_mode(right);
2107 unsigned bits = get_mode_size_bits(rmode);
2109 if (ref_bits == bits &&
2110 mode_is_int(rmode) &&
2111 get_mode_arithmetic(rmode) == irma_twos_complement) {
2112 ir_node *pre = get_Conv_op(right);
2113 ir_mode *pre_mode = get_irn_mode(pre);
2115 if (mode_is_int(pre_mode) &&
2116 get_mode_size_bits(pre_mode) == bits &&
2117 get_mode_arithmetic(pre_mode) == irma_twos_complement) {
2118 /* ok, this conv just changes to sign, moreover the calculation
2119 * is done with same number of bits as our address mode, so
2120 * we can ignore the conv as address calculation can be viewed
2121 * as either signed or unsigned
2123 set_binop_right(n, pre);
2128 /* let address arithmetic use unsigned modes */
2129 if (is_Const(right)) {
2130 ir_mode *rmode = get_irn_mode(right);
2132 if (mode_is_signed(rmode) && get_mode_arithmetic(rmode) == irma_twos_complement) {
2133 /* convert a AddP(P, *s) into AddP(P, *u) */
2134 ir_mode *nm = get_reference_mode_unsigned_eq(mode);
2136 ir_node *pre = new_r_Conv(get_nodes_block(n), right, nm);
2137 set_binop_right(n, pre);
2143 } /* transform_node_AddSub */
2145 #define HANDLE_BINOP_PHI(eval, a, b, c, mode) \
2148 if (is_Const(b) && is_const_Phi(a)) { \
2149 /* check for Op(Phi, Const) */ \
2150 c = apply_binop_on_phi(a, get_Const_tarval(b), eval, mode, 0);\
2152 else if (is_Const(a) && is_const_Phi(b)) { \
2153 /* check for Op(Const, Phi) */ \
2154 c = apply_binop_on_phi(b, get_Const_tarval(a), eval, mode, 1);\
2156 else if (is_const_Phi(a) && is_const_Phi(b)) { \
2157 /* check for Op(Phi, Phi) */ \
2158 c = apply_binop_on_2_phis(a, b, eval, mode); \
2161 DBG_OPT_ALGSIM0(oldn, c, FS_OPT_CONST_PHI); \
2166 #define HANDLE_UNOP_PHI(eval, a, c) \
2169 if (is_const_Phi(a)) { \
2170 /* check for Op(Phi) */ \
2171 c = apply_unop_on_phi(a, eval); \
2173 DBG_OPT_ALGSIM0(oldn, c, FS_OPT_CONST_PHI); \
2180 * Do the AddSub optimization, then Transform
2181 * Constant folding on Phi
2182 * Add(a,a) -> Mul(a, 2)
2183 * Add(Mul(a, x), a) -> Mul(a, x+1)
2184 * if the mode is integer or float.
2185 * Transform Add(a,-b) into Sub(a,b).
2186 * Reassociation might fold this further.
2188 static ir_node *transform_node_Add(ir_node *n)
2191 ir_node *a, *b, *c, *oldn = n;
2192 vrp_attr *a_vrp, *b_vrp;
2194 n = transform_node_AddSub(n);
2196 a = get_Add_left(n);
2197 b = get_Add_right(n);
2199 mode = get_irn_mode(n);
2201 if (mode_is_reference(mode)) {
2202 ir_mode *lmode = get_irn_mode(a);
2204 if (is_Const(b) && is_Const_null(b) && mode_is_int(lmode)) {
2205 /* an Add(a, NULL) is a hidden Conv */
2206 dbg_info *dbg = get_irn_dbg_info(n);
2207 return new_rd_Conv(dbg, get_nodes_block(n), a, mode);
2211 HANDLE_BINOP_PHI((eval_func) tarval_add, a, b, c, mode);
2213 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
2214 if (mode_is_float(mode)) {
2215 ir_graph *irg = get_irn_irg(n);
2216 if (get_irg_fp_model(irg) & fp_strict_algebraic)
2220 if (mode_is_num(mode)) {
2221 ir_graph *irg = get_irn_irg(n);
2222 /* the following code leads to endless recursion when Mul are replaced by a simple instruction chain */
2223 if (!is_irg_state(irg, IR_GRAPH_STATE_ARCH_DEP)
2224 && a == b && mode_is_int(mode)) {
2225 ir_node *block = get_nodes_block(n);
2228 get_irn_dbg_info(n),
2231 new_r_Const_long(irg, mode, 2),
2233 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_A);
2238 get_irn_dbg_info(n),
2243 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_MINUS_B);
2248 get_irn_dbg_info(n),
2253 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_MINUS_B);
2256 if (get_mode_arithmetic(mode) == irma_twos_complement) {
2257 /* Here we rely on constants be on the RIGHT side */
2259 ir_node *op = get_Not_op(a);
2261 if (is_Const(b) && is_Const_one(b)) {
2263 ir_node *blk = get_nodes_block(n);
2264 n = new_rd_Minus(get_irn_dbg_info(n), blk, op, mode);
2265 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_PLUS_1);
2270 n = new_r_Const(irg, get_mode_minus_one(mode));
2271 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_X_NOT_X);
2276 ir_node *op = get_Not_op(b);
2280 n = new_r_Const(irg, get_mode_minus_one(mode));
2281 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_X_NOT_X);
2288 a_vrp = vrp_get_info(a);
2289 b_vrp = vrp_get_info(b);
2291 if (a_vrp && b_vrp) {
2292 ir_tarval *c = tarval_and(a_vrp->bits_not_set, b_vrp->bits_not_set);
2294 if (tarval_is_null(c)) {
2295 dbg_info *dbgi = get_irn_dbg_info(n);
2296 return new_rd_Or(dbgi, get_nodes_block(n), a, b, mode);
2300 } /* transform_node_Add */
2303 * returns -cnst or NULL if impossible
2305 static ir_node *const_negate(ir_node *cnst)
2307 ir_tarval *tv = tarval_neg(get_Const_tarval(cnst));
2308 dbg_info *dbgi = get_irn_dbg_info(cnst);
2309 ir_graph *irg = get_irn_irg(cnst);
2310 if (tv == tarval_bad) return NULL;
2311 return new_rd_Const(dbgi, irg, tv);
2315 * Do the AddSub optimization, then Transform
2316 * Constant folding on Phi
2317 * Sub(0,a) -> Minus(a)
2318 * Sub(Mul(a, x), a) -> Mul(a, x-1)
2319 * Sub(Sub(x, y), b) -> Sub(x, Add(y,b))
2320 * Sub(Add(a, x), x) -> a
2321 * Sub(x, Add(x, a)) -> -a
2322 * Sub(x, Const) -> Add(x, -Const)
2324 static ir_node *transform_node_Sub(ir_node *n)
2330 n = transform_node_AddSub(n);
2332 a = get_Sub_left(n);
2333 b = get_Sub_right(n);
2335 mode = get_irn_mode(n);
2337 if (mode_is_int(mode)) {
2338 ir_mode *lmode = get_irn_mode(a);
2340 if (is_Const(b) && is_Const_null(b) && mode_is_reference(lmode)) {
2341 /* a Sub(a, NULL) is a hidden Conv */
2342 dbg_info *dbg = get_irn_dbg_info(n);
2343 n = new_rd_Conv(dbg, get_nodes_block(n), a, mode);
2344 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_CONV);
2348 if (mode == lmode &&
2349 get_mode_arithmetic(mode) == irma_twos_complement &&
2351 get_Const_tarval(a) == get_mode_minus_one(mode)) {
2353 dbg_info *dbg = get_irn_dbg_info(n);
2354 n = new_rd_Not(dbg, get_nodes_block(n), b, mode);
2355 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_NOT);
2361 HANDLE_BINOP_PHI((eval_func) tarval_sub, a, b, c, mode);
2363 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
2364 if (mode_is_float(mode)) {
2365 ir_graph *irg = get_irn_irg(n);
2366 if (get_irg_fp_model(irg) & fp_strict_algebraic)
2370 if (is_Const(b) && !mode_is_reference(get_irn_mode(b))) {
2371 /* a - C -> a + (-C) */
2372 ir_node *cnst = const_negate(b);
2374 ir_node *block = get_nodes_block(n);
2375 dbg_info *dbgi = get_irn_dbg_info(n);
2377 n = new_rd_Add(dbgi, block, a, cnst, mode);
2378 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2383 if (is_Minus(a)) { /* (-a) - b -> -(a + b) */
2384 dbg_info *dbg = get_irn_dbg_info(n);
2385 ir_node *block = get_nodes_block(n);
2386 ir_node *left = get_Minus_op(a);
2387 ir_node *add = new_rd_Add(dbg, block, left, b, mode);
2389 n = new_rd_Minus(dbg, block, add, mode);
2390 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2392 } else if (is_Minus(b)) { /* a - (-b) -> a + b */
2393 dbg_info *dbg = get_irn_dbg_info(n);
2394 ir_node *block = get_nodes_block(n);
2395 ir_node *right = get_Minus_op(b);
2397 n = new_rd_Add(dbg, block, a, right, mode);
2398 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MINUS);
2400 } else if (is_Sub(b)) {
2401 /* a - (b - c) -> a + (c - b)
2402 * -> (a - b) + c iff (b - c) is a pointer */
2403 dbg_info *s_dbg = get_irn_dbg_info(b);
2404 ir_node *s_block = get_nodes_block(b);
2405 ir_node *s_left = get_Sub_left(b);
2406 ir_node *s_right = get_Sub_right(b);
2407 ir_mode *s_mode = get_irn_mode(b);
2408 if (mode_is_reference(s_mode)) {
2409 ir_node *sub = new_rd_Sub(s_dbg, s_block, a, s_left, mode);
2410 dbg_info *a_dbg = get_irn_dbg_info(n);
2411 ir_node *a_block = get_nodes_block(n);
2414 s_right = new_r_Conv(a_block, s_right, mode);
2415 n = new_rd_Add(a_dbg, a_block, sub, s_right, mode);
2417 ir_node *sub = new_rd_Sub(s_dbg, s_block, s_right, s_left, s_mode);
2418 dbg_info *a_dbg = get_irn_dbg_info(n);
2419 ir_node *a_block = get_nodes_block(n);
2421 n = new_rd_Add(a_dbg, a_block, a, sub, mode);
2423 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2425 } else if (is_Mul(b)) { /* a - (b * C) -> a + (b * -C) */
2426 ir_node *m_right = get_Mul_right(b);
2427 if (is_Const(m_right)) {
2428 ir_node *cnst2 = const_negate(m_right);
2429 if (cnst2 != NULL) {
2430 dbg_info *m_dbg = get_irn_dbg_info(b);
2431 ir_node *m_block = get_nodes_block(b);
2432 ir_node *m_left = get_Mul_left(b);
2433 ir_mode *m_mode = get_irn_mode(b);
2434 ir_node *mul = new_rd_Mul(m_dbg, m_block, m_left, cnst2, m_mode);
2435 dbg_info *a_dbg = get_irn_dbg_info(n);
2436 ir_node *a_block = get_nodes_block(n);
2438 n = new_rd_Add(a_dbg, a_block, a, mul, mode);
2439 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2445 /* Beware of Sub(P, P) which cannot be optimized into a simple Minus ... */
2446 if (mode_is_num(mode) && mode == get_irn_mode(a) && is_Const(a) && is_Const_null(a)) {
2448 get_irn_dbg_info(n),
2452 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_0_A);
2456 if (mode_wrap_around(mode)) {
2457 ir_node *left = get_Add_left(a);
2458 ir_node *right = get_Add_right(a);
2460 /* FIXME: Does the Conv's work only for two complement or generally? */
2462 if (mode != get_irn_mode(right)) {
2463 /* This Sub is an effective Cast */
2464 right = new_r_Conv(get_nodes_block(n), right, mode);
2467 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2469 } else if (right == b) {
2470 if (mode != get_irn_mode(left)) {
2471 /* This Sub is an effective Cast */
2472 left = new_r_Conv(get_nodes_block(n), left, mode);
2475 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2481 if (mode_wrap_around(mode)) {
2482 ir_node *left = get_Add_left(b);
2483 ir_node *right = get_Add_right(b);
2485 /* FIXME: Does the Conv's work only for two complement or generally? */
2487 ir_mode *r_mode = get_irn_mode(right);
2489 n = new_r_Minus(get_nodes_block(n), right, r_mode);
2490 if (mode != r_mode) {
2491 /* This Sub is an effective Cast */
2492 n = new_r_Conv(get_nodes_block(n), n, mode);
2494 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2496 } else if (right == a) {
2497 ir_mode *l_mode = get_irn_mode(left);
2499 n = new_r_Minus(get_nodes_block(n), left, l_mode);
2500 if (mode != l_mode) {
2501 /* This Sub is an effective Cast */
2502 n = new_r_Conv(get_nodes_block(n), n, mode);
2504 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2509 if (mode_is_int(mode) && is_Conv(a) && is_Conv(b)) {
2510 ir_mode *mode = get_irn_mode(a);
2512 if (mode == get_irn_mode(b)) {
2514 ir_node *op_a = get_Conv_op(a);
2515 ir_node *op_b = get_Conv_op(b);
2517 /* check if it's allowed to skip the conv */
2518 ma = get_irn_mode(op_a);
2519 mb = get_irn_mode(op_b);
2521 if (mode_is_reference(ma) && mode_is_reference(mb)) {
2522 /* SubInt(ConvInt(aP), ConvInt(bP)) -> SubInt(aP,bP) */
2525 set_Sub_right(n, b);
2531 /* do NOT execute this code if reassociation is enabled, it does the inverse! */
2532 if (!is_reassoc_running() && is_Mul(a)) {
2533 ir_node *ma = get_Mul_left(a);
2534 ir_node *mb = get_Mul_right(a);
2537 ir_node *blk = get_nodes_block(n);
2538 ir_graph *irg = get_irn_irg(n);
2540 get_irn_dbg_info(n),
2544 get_irn_dbg_info(n),
2547 new_r_Const(irg, get_mode_one(mode)),
2550 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MUL_A_X_A);
2552 } else if (mb == b) {
2553 ir_node *blk = get_nodes_block(n);
2554 ir_graph *irg = get_irn_irg(n);
2556 get_irn_dbg_info(n),
2560 get_irn_dbg_info(n),
2563 new_r_Const(irg, get_mode_one(mode)),
2566 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MUL_A_X_A);
2570 if (is_Sub(a)) { /* (x - y) - b -> x - (y + b) */
2571 ir_node *x = get_Sub_left(a);
2572 ir_node *y = get_Sub_right(a);
2573 ir_node *blk = get_nodes_block(n);
2574 ir_mode *m_b = get_irn_mode(b);
2575 ir_mode *m_y = get_irn_mode(y);
2579 /* Determine the right mode for the Add. */
2582 else if (mode_is_reference(m_b))
2584 else if (mode_is_reference(m_y))
2588 * Both modes are different but none is reference,
2589 * happens for instance in SubP(SubP(P, Iu), Is).
2590 * We have two possibilities here: Cast or ignore.
2591 * Currently we ignore this case.
2596 add = new_r_Add(blk, y, b, add_mode);
2598 n = new_rd_Sub(get_irn_dbg_info(n), blk, x, add, mode);
2599 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_SUB_X_Y_Z);
2603 if (get_mode_arithmetic(mode) == irma_twos_complement) {
2604 if (is_Const(a) && is_Not(b)) {
2605 /* c - ~X = X + (c+1) */
2606 ir_tarval *tv = get_Const_tarval(a);
2608 tv = tarval_add(tv, get_mode_one(mode));
2609 if (tv != tarval_bad) {
2610 ir_node *blk = get_nodes_block(n);
2611 ir_graph *irg = get_irn_irg(n);
2612 ir_node *c = new_r_Const(irg, tv);
2613 n = new_rd_Add(get_irn_dbg_info(n), blk, get_Not_op(b), c, mode);
2614 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_C_NOT_X);
2620 } /* transform_node_Sub */
2623 * Several transformation done on n*n=2n bits mul.
2624 * These transformations must be done here because new nodes may be produced.
2626 static ir_node *transform_node_Mul2n(ir_node *n, ir_mode *mode)
2629 ir_node *a = get_Mul_left(n);
2630 ir_node *b = get_Mul_right(n);
2631 ir_tarval *ta = value_of(a);
2632 ir_tarval *tb = value_of(b);
2633 ir_mode *smode = get_irn_mode(a);
2635 if (ta == get_mode_one(smode)) {
2636 /* (L)1 * (L)b = (L)b */
2637 ir_node *blk = get_nodes_block(n);
2638 n = new_rd_Conv(get_irn_dbg_info(n), blk, b, mode);
2639 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
2642 else if (ta == get_mode_minus_one(smode)) {
2643 /* (L)-1 * (L)b = (L)b */
2644 ir_node *blk = get_nodes_block(n);
2645 n = new_rd_Minus(get_irn_dbg_info(n), blk, b, smode);
2646 n = new_rd_Conv(get_irn_dbg_info(n), blk, n, mode);
2647 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2650 if (tb == get_mode_one(smode)) {
2651 /* (L)a * (L)1 = (L)a */
2652 ir_node *blk = get_irn_n(a, -1);
2653 n = new_rd_Conv(get_irn_dbg_info(n), blk, a, mode);
2654 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
2657 else if (tb == get_mode_minus_one(smode)) {
2658 /* (L)a * (L)-1 = (L)-a */
2659 ir_node *blk = get_nodes_block(n);
2660 n = new_rd_Minus(get_irn_dbg_info(n), blk, a, smode);
2661 n = new_rd_Conv(get_irn_dbg_info(n), blk, n, mode);
2662 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2669 * Transform Mul(a,-1) into -a.
2670 * Do constant evaluation of Phi nodes.
2671 * Do architecture dependent optimizations on Mul nodes
2673 static ir_node *transform_node_Mul(ir_node *n)
2675 ir_node *c, *oldn = n;
2676 ir_mode *mode = get_irn_mode(n);
2677 ir_node *a = get_Mul_left(n);
2678 ir_node *b = get_Mul_right(n);
2680 if (is_Bad(a) || is_Bad(b))
2683 if (mode != get_irn_mode(a))
2684 return transform_node_Mul2n(n, mode);
2686 HANDLE_BINOP_PHI((eval_func) tarval_mul, a, b, c, mode);
2688 if (mode_is_signed(mode)) {
2691 if (value_of(a) == get_mode_minus_one(mode))
2693 else if (value_of(b) == get_mode_minus_one(mode))
2696 n = new_rd_Minus(get_irn_dbg_info(n), get_nodes_block(n), r, mode);
2697 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2702 if (is_Const(b)) { /* (-a) * const -> a * -const */
2703 ir_node *cnst = const_negate(b);
2705 dbg_info *dbgi = get_irn_dbg_info(n);
2706 ir_node *block = get_nodes_block(n);
2707 n = new_rd_Mul(dbgi, block, get_Minus_op(a), cnst, mode);
2708 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2711 } else if (is_Minus(b)) { /* (-a) * (-b) -> a * b */
2712 dbg_info *dbgi = get_irn_dbg_info(n);
2713 ir_node *block = get_nodes_block(n);
2714 n = new_rd_Mul(dbgi, block, get_Minus_op(a), get_Minus_op(b), mode);
2715 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_MINUS);
2717 } else if (is_Sub(b)) { /* (-a) * (b - c) -> a * (c - b) */
2718 ir_node *sub_l = get_Sub_left(b);
2719 ir_node *sub_r = get_Sub_right(b);
2720 dbg_info *dbgi = get_irn_dbg_info(n);
2721 ir_node *block = get_nodes_block(n);
2722 ir_node *new_b = new_rd_Sub(dbgi, block, sub_r, sub_l, mode);
2723 n = new_rd_Mul(dbgi, block, get_Minus_op(a), new_b, mode);
2724 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS);
2727 } else if (is_Minus(b)) {
2728 if (is_Sub(a)) { /* (a - b) * (-c) -> (b - a) * c */
2729 ir_node *sub_l = get_Sub_left(a);
2730 ir_node *sub_r = get_Sub_right(a);
2731 dbg_info *dbgi = get_irn_dbg_info(n);
2732 ir_node *block = get_nodes_block(n);
2733 ir_node *new_a = new_rd_Sub(dbgi, block, sub_r, sub_l, mode);
2734 n = new_rd_Mul(dbgi, block, new_a, get_Minus_op(b), mode);
2735 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS);
2738 } else if (is_Shl(a)) {
2739 ir_node *const shl_l = get_Shl_left(a);
2740 if (is_Const(shl_l) && is_Const_one(shl_l)) {
2741 /* (1 << x) * b -> b << x */
2742 dbg_info *const dbgi = get_irn_dbg_info(n);
2743 ir_node *const block = get_nodes_block(n);
2744 ir_node *const shl_r = get_Shl_right(a);
2745 n = new_rd_Shl(dbgi, block, b, shl_r, mode);
2746 // TODO add me DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_SHIFT);
2749 } else if (is_Shl(b)) {
2750 ir_node *const shl_l = get_Shl_left(b);
2751 if (is_Const(shl_l) && is_Const_one(shl_l)) {
2752 /* a * (1 << x) -> a << x */
2753 dbg_info *const dbgi = get_irn_dbg_info(n);
2754 ir_node *const block = get_nodes_block(n);
2755 ir_node *const shl_r = get_Shl_right(b);
2756 n = new_rd_Shl(dbgi, block, a, shl_r, mode);
2757 // TODO add me DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_SHIFT);
2761 if (get_mode_arithmetic(mode) == irma_ieee754) {
2763 ir_tarval *tv = get_Const_tarval(a);
2764 if (tarval_ieee754_get_exponent(tv) == 1 && tarval_ieee754_zero_mantissa(tv)
2765 && !tarval_is_negative(tv)) {
2766 /* 2.0 * b = b + b */
2767 n = new_rd_Add(get_irn_dbg_info(n), get_nodes_block(n), b, b, mode);
2768 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_A_A);
2772 else if (is_Const(b)) {
2773 ir_tarval *tv = get_Const_tarval(b);
2774 if (tarval_ieee754_get_exponent(tv) == 1 && tarval_ieee754_zero_mantissa(tv)
2775 && !tarval_is_negative(tv)) {
2776 /* a * 2.0 = a + a */
2777 n = new_rd_Add(get_irn_dbg_info(n), get_nodes_block(n), a, a, mode);
2778 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_A_A);
2783 return arch_dep_replace_mul_with_shifts(n);
2784 } /* transform_node_Mul */
2787 * Transform a Div Node.
2789 static ir_node *transform_node_Div(ir_node *n)
2791 ir_mode *mode = get_Div_resmode(n);
2792 ir_node *a = get_Div_left(n);
2793 ir_node *b = get_Div_right(n);
2795 const ir_node *dummy;
2797 if (is_Const(b) && is_const_Phi(a)) {
2798 /* check for Div(Phi, Const) */
2799 value = apply_binop_on_phi(a, get_Const_tarval(b), (eval_func) tarval_div, mode, 0);
2801 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2804 } else if (is_Const(a) && is_const_Phi(b)) {
2805 /* check for Div(Const, Phi) */
2806 value = apply_binop_on_phi(b, get_Const_tarval(a), (eval_func) tarval_div, mode, 1);
2808 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2811 } else if (is_const_Phi(a) && is_const_Phi(b)) {
2812 /* check for Div(Phi, Phi) */
2813 value = apply_binop_on_2_phis(a, b, (eval_func) tarval_div, mode);
2815 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2822 if (mode_is_float(mode)) {
2823 /* Optimize x/c to x*(1/c) */
2824 if (get_mode_arithmetic(mode) == irma_ieee754) {
2825 ir_tarval *tv = value_of(b);
2827 if (tv != tarval_bad) {
2828 int rem = tarval_fp_ops_enabled();
2831 * Floating point constant folding might be disabled here to
2833 * However, as we check for exact result, doing it is safe.
2836 tarval_enable_fp_ops(1);
2837 tv = tarval_div(get_mode_one(mode), tv);
2838 tarval_enable_fp_ops(rem);
2840 /* Do the transformation if the result is either exact or we are
2841 not using strict rules. */
2842 if (tv != tarval_bad &&
2843 (tarval_ieee754_get_exact() || (get_irg_fp_model(get_irn_irg(n)) & fp_strict_algebraic) == 0)) {
2844 ir_node *block = get_nodes_block(n);
2845 ir_graph *irg = get_irn_irg(block);
2846 ir_node *c = new_r_Const(irg, tv);
2847 dbg_info *dbgi = get_irn_dbg_info(n);
2848 value = new_rd_Mul(dbgi, block, a, c, mode);
2856 assert(mode_is_int(mode));
2858 if (a == b && value_not_zero(a, &dummy)) {
2859 ir_graph *irg = get_irn_irg(n);
2860 /* BEWARE: we can optimize a/a to 1 only if this cannot cause a exception */
2861 value = new_r_Const(irg, get_mode_one(mode));
2862 DBG_OPT_CSTEVAL(n, value);
2865 if (mode_is_signed(mode) && is_Const(b)) {
2866 ir_tarval *tv = get_Const_tarval(b);
2868 if (tv == get_mode_minus_one(mode)) {
2870 value = new_rd_Minus(get_irn_dbg_info(n), get_nodes_block(n), a, mode);
2871 DBG_OPT_CSTEVAL(n, value);
2875 /* Try architecture dependent optimization */
2876 value = arch_dep_replace_div_by_const(n);
2886 /* Turn Div into a tuple (mem, jmp, bad, value) */
2887 mem = get_Div_mem(n);
2888 blk = get_nodes_block(n);
2889 irg = get_irn_irg(blk);
2891 /* skip a potential Pin */
2892 mem = skip_Pin(mem);
2893 turn_into_tuple(n, pn_Div_max);
2894 set_Tuple_pred(n, pn_Div_M, mem);
2895 set_Tuple_pred(n, pn_Div_X_regular, new_r_Jmp(blk));
2896 set_Tuple_pred(n, pn_Div_X_except, get_irg_bad(irg));
2897 set_Tuple_pred(n, pn_Div_res, value);
2900 } /* transform_node_Div */
2903 * Transform a Mod node.
2905 static ir_node *transform_node_Mod(ir_node *n)
2907 ir_mode *mode = get_Mod_resmode(n);
2908 ir_node *a = get_Mod_left(n);
2909 ir_node *b = get_Mod_right(n);
2914 if (is_Const(b) && is_const_Phi(a)) {
2915 /* check for Div(Phi, Const) */
2916 value = apply_binop_on_phi(a, get_Const_tarval(b), (eval_func) tarval_mod, mode, 0);
2918 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2922 else if (is_Const(a) && is_const_Phi(b)) {
2923 /* check for Div(Const, Phi) */
2924 value = apply_binop_on_phi(b, get_Const_tarval(a), (eval_func) tarval_mod, mode, 1);
2926 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2930 else if (is_const_Phi(a) && is_const_Phi(b)) {
2931 /* check for Div(Phi, Phi) */
2932 value = apply_binop_on_2_phis(a, b, (eval_func) tarval_mod, mode);
2934 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2941 irg = get_irn_irg(n);
2942 if (tv != tarval_bad) {
2943 value = new_r_Const(irg, tv);
2945 DBG_OPT_CSTEVAL(n, value);
2948 ir_node *a = get_Mod_left(n);
2949 ir_node *b = get_Mod_right(n);
2950 const ir_node *dummy;
2952 if (a == b && value_not_zero(a, &dummy)) {
2953 /* BEWARE: we can optimize a%a to 0 only if this cannot cause a exception */
2954 value = new_r_Const(irg, get_mode_null(mode));
2955 DBG_OPT_CSTEVAL(n, value);
2958 if (mode_is_signed(mode) && is_Const(b)) {
2959 ir_tarval *tv = get_Const_tarval(b);
2961 if (tv == get_mode_minus_one(mode)) {
2963 value = new_r_Const(irg, get_mode_null(mode));
2964 DBG_OPT_CSTEVAL(n, value);
2968 /* Try architecture dependent optimization */
2969 value = arch_dep_replace_mod_by_const(n);
2978 /* Turn Mod into a tuple (mem, jmp, bad, value) */
2979 mem = get_Mod_mem(n);
2980 blk = get_nodes_block(n);
2981 irg = get_irn_irg(blk);
2983 /* skip a potential Pin */
2984 mem = skip_Pin(mem);
2985 turn_into_tuple(n, pn_Mod_max);
2986 set_Tuple_pred(n, pn_Mod_M, mem);
2987 set_Tuple_pred(n, pn_Mod_X_regular, new_r_Jmp(blk));
2988 set_Tuple_pred(n, pn_Mod_X_except, get_irg_bad(irg));
2989 set_Tuple_pred(n, pn_Mod_res, value);
2992 } /* transform_node_Mod */
2995 * Optimize -a CMP -b into b CMP a.
2996 * This works only for for modes where unary Minus
2998 * Note that two-complement integers can Overflow
2999 * so it will NOT work.
3001 * For == and != can be handled in Proj(Cmp)
3003 static ir_node *transform_node_Cmp(ir_node *n)
3006 ir_node *left = get_Cmp_left(n);
3007 ir_node *right = get_Cmp_right(n);
3009 if (is_Minus(left) && is_Minus(right) &&
3010 !mode_overflow_on_unary_Minus(get_irn_mode(left))) {
3011 ir_node *const new_left = get_Minus_op(right);
3012 ir_node *const new_right = get_Minus_op(left);
3013 n = new_rd_Cmp(get_irn_dbg_info(n), get_nodes_block(n), new_left, new_right);
3014 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CMP_OP_OP);
3017 } /* transform_node_Cmp */
3021 * Transform a Cond node.
3023 * Replace the Cond by a Jmp if it branches on a constant
3026 static ir_node *transform_node_Cond(ir_node *n)
3029 ir_node *a = get_Cond_selector(n);
3030 ir_tarval *ta = value_of(a);
3031 ir_graph *irg = get_irn_irg(n);
3034 /* we need block info which is not available in floating irgs */
3035 if (get_irg_pinned(irg) == op_pin_state_floats)
3038 if ((ta != tarval_bad) &&
3039 (get_irn_mode(a) == mode_b) &&
3040 (get_opt_unreachable_code())) {
3041 /* It's a boolean Cond, branching on a boolean constant.
3042 Replace it by a tuple (Bad, Jmp) or (Jmp, Bad) */
3043 ir_node *blk = get_nodes_block(n);
3044 jmp = new_r_Jmp(blk);
3045 turn_into_tuple(n, pn_Cond_max);
3046 if (ta == tarval_b_true) {
3047 set_Tuple_pred(n, pn_Cond_false, get_irg_bad(irg));
3048 set_Tuple_pred(n, pn_Cond_true, jmp);
3050 set_Tuple_pred(n, pn_Cond_false, jmp);
3051 set_Tuple_pred(n, pn_Cond_true, get_irg_bad(irg));
3053 /* We might generate an endless loop, so keep it alive. */
3054 add_End_keepalive(get_irg_end(irg), blk);
3057 } /* transform_node_Cond */
3060 * Prototype of a recursive transform function
3061 * for bitwise distributive transformations.
3063 typedef ir_node* (*recursive_transform)(ir_node *n);
3066 * makes use of distributive laws for and, or, eor
3067 * and(a OP c, b OP c) -> and(a, b) OP c
3068 * note, might return a different op than n
3070 static ir_node *transform_bitwise_distributive(ir_node *n,
3071 recursive_transform trans_func)
3074 ir_node *a = get_binop_left(n);
3075 ir_node *b = get_binop_right(n);
3076 ir_op *op = get_irn_op(a);
3077 ir_op *op_root = get_irn_op(n);
3079 if (op != get_irn_op(b))
3082 /* and(conv(a), conv(b)) -> conv(and(a,b)) */
3083 if (op == op_Conv) {
3084 ir_node *a_op = get_Conv_op(a);
3085 ir_node *b_op = get_Conv_op(b);
3086 ir_mode *a_mode = get_irn_mode(a_op);
3087 ir_mode *b_mode = get_irn_mode(b_op);
3088 if (a_mode == b_mode && (mode_is_int(a_mode) || a_mode == mode_b)) {
3089 ir_node *blk = get_nodes_block(n);
3092 set_binop_left(n, a_op);
3093 set_binop_right(n, b_op);
3094 set_irn_mode(n, a_mode);
3096 n = new_r_Conv(blk, n, get_irn_mode(oldn));
3098 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
3104 /* nothing to gain here */
3108 if (op == op_Shrs || op == op_Shr || op == op_Shl
3109 || op == op_And || op == op_Or || op == op_Eor) {
3110 ir_node *a_left = get_binop_left(a);
3111 ir_node *a_right = get_binop_right(a);
3112 ir_node *b_left = get_binop_left(b);
3113 ir_node *b_right = get_binop_right(b);
3115 ir_node *op1 = NULL;
3116 ir_node *op2 = NULL;
3118 if (is_op_commutative(op)) {
3119 if (a_left == b_left) {
3123 } else if (a_left == b_right) {
3127 } else if (a_right == b_left) {
3133 if (a_right == b_right) {
3140 /* (a sop c) & (b sop c) => (a & b) sop c */
3141 ir_node *blk = get_nodes_block(n);
3143 ir_node *new_n = exact_copy(n);
3144 set_binop_left(new_n, op1);
3145 set_binop_right(new_n, op2);
3146 new_n = trans_func(new_n);
3148 if (op_root == op_Eor && op == op_Or) {
3149 dbg_info *dbgi = get_irn_dbg_info(n);
3150 ir_mode *mode = get_irn_mode(c);
3152 c = new_rd_Not(dbgi, blk, c, mode);
3153 n = new_rd_And(dbgi, blk, new_n, c, mode);
3156 set_nodes_block(n, blk);
3157 set_binop_left(n, new_n);
3158 set_binop_right(n, c);
3162 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_SHIFT_AND);
3173 static ir_node *transform_node_And(ir_node *n)
3175 ir_node *c, *oldn = n;
3176 ir_node *a = get_And_left(n);
3177 ir_node *b = get_And_right(n);
3179 vrp_attr *a_vrp, *b_vrp;
3181 mode = get_irn_mode(n);
3182 HANDLE_BINOP_PHI((eval_func) tarval_and, a, b, c, mode);
3184 /* we can evaluate 2 Projs of the same Cmp */
3185 if (mode == mode_b && is_Proj(a) && is_Proj(b)) {
3186 ir_node *pred_a = get_Proj_pred(a);
3187 ir_node *pred_b = get_Proj_pred(b);
3188 if (pred_a == pred_b) {
3189 dbg_info *dbgi = get_irn_dbg_info(n);
3190 pn_Cmp pn_a = get_Proj_pn_cmp(a);
3191 pn_Cmp pn_b = get_Proj_pn_cmp(b);
3192 /* yes, we can simply calculate with pncs */
3193 pn_Cmp new_pnc = pn_a & pn_b;
3195 return new_rd_Proj(dbgi, pred_a, mode_b, new_pnc);
3200 ir_node *op = get_Not_op(b);
3202 ir_node *ba = get_And_left(op);
3203 ir_node *bb = get_And_right(op);
3205 /* it's enough to test the following cases due to normalization! */
3206 if (get_Or_left(a) == ba && get_Or_right(a) == bb) {
3207 /* (a|b) & ~(a&b) = a^b */
3208 ir_node *block = get_nodes_block(n);
3210 n = new_rd_Eor(get_irn_dbg_info(n), block, ba, bb, mode);
3211 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_TO_EOR);
3219 ir_node *op = get_Not_op(a);
3221 ir_node *aa = get_And_left(op);
3222 ir_node *ab = get_And_right(op);
3224 /* it's enough to test the following cases due to normalization! */
3225 if (get_Or_left(b) == aa && get_Or_right(b) == ab) {
3226 /* (a|b) & ~(a&b) = a^b */
3227 ir_node *block = get_nodes_block(n);
3229 n = new_rd_Eor(get_irn_dbg_info(n), block, aa, ab, mode);
3230 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_TO_EOR);
3237 ir_node *al = get_Eor_left(a);
3238 ir_node *ar = get_Eor_right(a);
3241 /* (b ^ a) & b -> ~a & b */
3242 dbg_info *dbg = get_irn_dbg_info(n);
3243 ir_node *block = get_nodes_block(n);
3245 ar = new_rd_Not(dbg, block, ar, mode);
3246 n = new_rd_And(dbg, block, ar, b, mode);
3247 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3251 /* (a ^ b) & b -> ~a & b */
3252 dbg_info *dbg = get_irn_dbg_info(n);
3253 ir_node *block = get_nodes_block(n);
3255 al = new_rd_Not(dbg, block, al, mode);
3256 n = new_rd_And(dbg, block, al, b, mode);
3257 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3262 ir_node *bl = get_Eor_left(b);
3263 ir_node *br = get_Eor_right(b);
3266 /* a & (a ^ b) -> a & ~b */
3267 dbg_info *dbg = get_irn_dbg_info(n);
3268 ir_node *block = get_nodes_block(n);
3270 br = new_rd_Not(dbg, block, br, mode);
3271 n = new_rd_And(dbg, block, br, a, mode);
3272 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3276 /* a & (b ^ a) -> a & ~b */
3277 dbg_info *dbg = get_irn_dbg_info(n);
3278 ir_node *block = get_nodes_block(n);
3280 bl = new_rd_Not(dbg, block, bl, mode);
3281 n = new_rd_And(dbg, block, bl, a, mode);
3282 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3286 if (is_Not(a) && is_Not(b)) {
3287 /* ~a & ~b = ~(a|b) */
3288 ir_node *block = get_nodes_block(n);
3289 ir_mode *mode = get_irn_mode(n);
3293 n = new_rd_Or(get_irn_dbg_info(n), block, a, b, mode);
3294 n = new_rd_Not(get_irn_dbg_info(n), block, n, mode);
3295 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_DEMORGAN);
3299 b_vrp = vrp_get_info(b);
3300 if (is_Const(a) && b_vrp && (tarval_cmp(tarval_or(get_Const_tarval(a),
3301 b_vrp->bits_not_set), get_Const_tarval(a)) == pn_Cmp_Eq)) {
3307 a_vrp = vrp_get_info(a);
3308 if (is_Const(b) && a_vrp && (tarval_cmp(tarval_or(get_Const_tarval(b),
3309 a_vrp->bits_not_set), get_Const_tarval(b)) == pn_Cmp_Eq)) {
3313 n = transform_bitwise_distributive(n, transform_node_And);
3316 } /* transform_node_And */
3318 /* the order of the values is important! */
3319 typedef enum const_class {
3325 static const_class classify_const(const ir_node* n)
3327 if (is_Const(n)) return const_const;
3328 if (is_irn_constlike(n)) return const_like;
3333 * Determines whether r is more constlike or has a larger index (in that order)
3336 static bool operands_are_normalized(const ir_node *l, const ir_node *r)
3338 const const_class l_order = classify_const(l);
3339 const const_class r_order = classify_const(r);
3341 l_order > r_order ||
3342 (l_order == r_order && get_irn_idx(l) <= get_irn_idx(r));
3348 static ir_node *transform_node_Eor(ir_node *n)
3350 ir_node *c, *oldn = n;
3351 ir_node *a = get_Eor_left(n);
3352 ir_node *b = get_Eor_right(n);
3353 ir_mode *mode = get_irn_mode(n);
3355 HANDLE_BINOP_PHI((eval_func) tarval_eor, a, b, c, mode);
3357 /* we can evaluate 2 Projs of the same Cmp */
3358 if (mode == mode_b && is_Proj(a) && is_Proj(b)) {
3359 ir_node *pred_a = get_Proj_pred(a);
3360 ir_node *pred_b = get_Proj_pred(b);
3361 if (pred_a == pred_b) {
3362 dbg_info *dbgi = get_irn_dbg_info(n);
3363 pn_Cmp pn_a = get_Proj_pn_cmp(a);
3364 pn_Cmp pn_b = get_Proj_pn_cmp(b);
3365 /* yes, we can simply calculate with pncs */
3366 pn_Cmp new_pnc = pn_a ^ pn_b;
3368 return new_rd_Proj(dbgi, pred_a, mode_b, new_pnc);
3372 /* normalize not nodes... ~a ^ b <=> a ^ ~b */
3373 if (is_Not(a) && operands_are_normalized(get_Not_op(a), b)) {
3374 dbg_info *dbg = get_irn_dbg_info(n);
3375 ir_node *block = get_nodes_block(n);
3376 ir_node *new_not = new_rd_Not(dbg, block, b, mode);
3377 ir_node *new_left = get_Not_op(a);
3378 n = new_rd_Eor(dbg, block, new_left, new_not, mode);
3379 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3381 } else if (is_Not(b) && !operands_are_normalized(a, get_Not_op(b))) {
3382 dbg_info *dbg = get_irn_dbg_info(n);
3383 ir_node *block = get_nodes_block(n);
3384 ir_node *new_not = new_rd_Not(dbg, block, a, mode);
3385 ir_node *new_right = get_Not_op(b);
3386 n = new_rd_Eor(dbg, block, new_not, new_right, mode);
3387 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3391 /* x ^ 1...1 -> ~1 */
3392 if (is_Const(b) && is_Const_all_one(b)) {
3393 n = new_r_Not(get_nodes_block(n), a, mode);
3394 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3398 n = transform_bitwise_distributive(n, transform_node_Eor);
3400 } /* transform_node_Eor */
3405 static ir_node *transform_node_Not(ir_node *n)
3407 ir_node *c, *oldn = n;
3408 ir_node *a = get_Not_op(n);
3409 ir_mode *mode = get_irn_mode(n);
3411 HANDLE_UNOP_PHI(tarval_not,a,c);
3413 /* check for a boolean Not */
3414 if (mode == mode_b && is_Proj(a)) {
3415 ir_node *a_pred = get_Proj_pred(a);
3416 if (is_Cmp(a_pred)) {
3417 /* We negate a Cmp. The Cmp has the negated result anyways! */
3418 n = new_r_Proj(get_Proj_pred(a),
3419 mode_b, get_negated_pnc(get_Proj_proj(a), mode_b));
3420 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_CMP);
3425 /* normalize ~(a ^ b) => a ^ ~b */
3427 dbg_info *dbg = get_irn_dbg_info(n);
3428 ir_node *block = get_nodes_block(n);
3429 ir_node *eor_right = get_Eor_right(a);
3430 ir_node *eor_left = get_Eor_left(a);
3431 eor_right = new_rd_Not(dbg, block, eor_right, mode);
3432 n = new_rd_Eor(dbg, block, eor_left, eor_right, mode);
3436 if (get_mode_arithmetic(mode) == irma_twos_complement) {
3437 if (is_Minus(a)) { /* ~-x -> x + -1 */
3438 dbg_info *dbg = get_irn_dbg_info(n);
3439 ir_graph *irg = get_irn_irg(n);
3440 ir_node *block = get_nodes_block(n);
3441 ir_node *add_l = get_Minus_op(a);
3442 ir_node *add_r = new_rd_Const(dbg, irg, get_mode_minus_one(mode));
3443 n = new_rd_Add(dbg, block, add_l, add_r, mode);
3444 } else if (is_Add(a)) {
3445 ir_node *add_r = get_Add_right(a);
3446 if (is_Const(add_r) && is_Const_all_one(add_r)) {
3447 /* ~(x + -1) = -x */
3448 ir_node *op = get_Add_left(a);
3449 ir_node *blk = get_nodes_block(n);
3450 n = new_rd_Minus(get_irn_dbg_info(n), blk, op, get_irn_mode(n));
3451 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_MINUS_1);
3456 } /* transform_node_Not */
3459 * Transform a Minus.
3463 * -(a >>u (size-1)) = a >>s (size-1)
3464 * -(a >>s (size-1)) = a >>u (size-1)
3465 * -(a * const) -> a * -const
3467 static ir_node *transform_node_Minus(ir_node *n)
3469 ir_node *c, *oldn = n;
3470 ir_node *a = get_Minus_op(n);
3473 HANDLE_UNOP_PHI(tarval_neg,a,c);
3475 mode = get_irn_mode(a);
3476 if (get_mode_arithmetic(mode) == irma_twos_complement) {
3477 /* the following rules are only to twos-complement */
3480 ir_node *op = get_Not_op(a);
3481 ir_tarval *tv = get_mode_one(mode);
3482 ir_node *blk = get_nodes_block(n);
3483 ir_graph *irg = get_irn_irg(blk);
3484 ir_node *c = new_r_Const(irg, tv);
3485 n = new_rd_Add(get_irn_dbg_info(n), blk, op, c, mode);
3486 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_MINUS_NOT);
3490 ir_node *c = get_Shr_right(a);
3493 ir_tarval *tv = get_Const_tarval(c);
3495 if (tarval_is_long(tv) && get_tarval_long(tv) == (int) get_mode_size_bits(mode) - 1) {
3496 /* -(a >>u (size-1)) = a >>s (size-1) */
3497 ir_node *v = get_Shr_left(a);
3499 n = new_rd_Shrs(get_irn_dbg_info(n), get_nodes_block(n), v, c, mode);
3500 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_PREDICATE);
3506 ir_node *c = get_Shrs_right(a);
3509 ir_tarval *tv = get_Const_tarval(c);
3511 if (tarval_is_long(tv) && get_tarval_long(tv) == (int) get_mode_size_bits(mode) - 1) {
3512 /* -(a >>s (size-1)) = a >>u (size-1) */
3513 ir_node *v = get_Shrs_left(a);
3515 n = new_rd_Shr(get_irn_dbg_info(n), get_nodes_block(n), v, c, mode);
3516 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_PREDICATE);
3523 /* - (a-b) = b - a */
3524 ir_node *la = get_Sub_left(a);
3525 ir_node *ra = get_Sub_right(a);
3526 ir_node *blk = get_nodes_block(n);
3528 n = new_rd_Sub(get_irn_dbg_info(n), blk, ra, la, mode);
3529 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_MINUS_SUB);
3533 if (is_Mul(a)) { /* -(a * const) -> a * -const */
3534 ir_node *mul_l = get_Mul_left(a);
3535 ir_node *mul_r = get_Mul_right(a);
3536 ir_tarval *tv = value_of(mul_r);
3537 if (tv != tarval_bad) {
3538 tv = tarval_neg(tv);
3539 if (tv != tarval_bad) {
3540 ir_graph *irg = get_irn_irg(n);
3541 ir_node *cnst = new_r_Const(irg, tv);
3542 dbg_info *dbg = get_irn_dbg_info(a);
3543 ir_node *block = get_nodes_block(a);
3544 n = new_rd_Mul(dbg, block, mul_l, cnst, mode);
3545 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_MINUS_MUL_C);
3552 } /* transform_node_Minus */
3555 * Transform a Proj(Load) with a non-null address.
3557 static ir_node *transform_node_Proj_Load(ir_node *proj)
3559 if (get_opt_ldst_only_null_ptr_exceptions()) {
3560 if (get_irn_mode(proj) == mode_X) {
3561 ir_node *load = get_Proj_pred(proj);
3563 /* get the Load address */
3564 const ir_node *addr = get_Load_ptr(load);
3565 const ir_node *confirm;
3567 if (value_not_null(addr, &confirm)) {
3568 if (confirm == NULL) {
3569 /* this node may float if it did not depend on a Confirm */
3570 set_irn_pinned(load, op_pin_state_floats);
3572 if (get_Proj_proj(proj) == pn_Load_X_except) {
3573 ir_graph *irg = get_irn_irg(proj);
3574 DBG_OPT_EXC_REM(proj);
3575 return get_irg_bad(irg);
3577 ir_node *blk = get_nodes_block(load);
3578 return new_r_Jmp(blk);
3584 } /* transform_node_Proj_Load */
3587 * Transform a Proj(Store) with a non-null address.
3589 static ir_node *transform_node_Proj_Store(ir_node *proj)
3591 if (get_opt_ldst_only_null_ptr_exceptions()) {
3592 if (get_irn_mode(proj) == mode_X) {
3593 ir_node *store = get_Proj_pred(proj);
3595 /* get the load/store address */
3596 const ir_node *addr = get_Store_ptr(store);
3597 const ir_node *confirm;
3599 if (value_not_null(addr, &confirm)) {
3600 if (confirm == NULL) {
3601 /* this node may float if it did not depend on a Confirm */
3602 set_irn_pinned(store, op_pin_state_floats);
3604 if (get_Proj_proj(proj) == pn_Store_X_except) {
3605 ir_graph *irg = get_irn_irg(proj);
3606 DBG_OPT_EXC_REM(proj);
3607 return get_irg_bad(irg);
3609 ir_node *blk = get_nodes_block(store);
3610 return new_r_Jmp(blk);
3616 } /* transform_node_Proj_Store */
3619 * Transform a Proj(Div) with a non-zero value.
3620 * Removes the exceptions and routes the memory to the NoMem node.
3622 static ir_node *transform_node_Proj_Div(ir_node *proj)
3624 ir_node *div = get_Proj_pred(proj);
3625 ir_node *b = get_Div_right(div);
3626 ir_node *res, *new_mem;
3627 const ir_node *confirm;
3630 if (value_not_zero(b, &confirm)) {
3631 /* div(x, y) && y != 0 */
3632 if (confirm == NULL) {
3633 /* we are sure we have a Const != 0 */
3634 new_mem = get_Div_mem(div);
3635 new_mem = skip_Pin(new_mem);
3636 set_Div_mem(div, new_mem);
3637 set_irn_pinned(div, op_pin_state_floats);
3640 proj_nr = get_Proj_proj(proj);
3642 case pn_Div_X_regular:
3643 return new_r_Jmp(get_nodes_block(div));
3645 case pn_Div_X_except: {
3646 ir_graph *irg = get_irn_irg(proj);
3647 /* we found an exception handler, remove it */
3648 DBG_OPT_EXC_REM(proj);
3649 return get_irg_bad(irg);
3653 ir_graph *irg = get_irn_irg(proj);
3654 res = get_Div_mem(div);
3655 new_mem = get_irg_no_mem(irg);
3658 /* This node can only float up to the Confirm block */
3659 new_mem = new_r_Pin(get_nodes_block(confirm), new_mem);
3661 set_irn_pinned(div, op_pin_state_floats);
3662 /* this is a Div without exception, we can remove the memory edge */
3663 set_Div_mem(div, new_mem);
3669 } /* transform_node_Proj_Div */
3672 * Transform a Proj(Mod) with a non-zero value.
3673 * Removes the exceptions and routes the memory to the NoMem node.
3675 static ir_node *transform_node_Proj_Mod(ir_node *proj)
3677 ir_node *mod = get_Proj_pred(proj);
3678 ir_node *b = get_Mod_right(mod);
3679 ir_node *res, *new_mem;
3680 const ir_node *confirm;
3683 if (value_not_zero(b, &confirm)) {
3684 /* mod(x, y) && y != 0 */
3685 proj_nr = get_Proj_proj(proj);
3687 if (confirm == NULL) {
3688 /* we are sure we have a Const != 0 */
3689 new_mem = get_Mod_mem(mod);
3690 new_mem = skip_Pin(new_mem);
3691 set_Mod_mem(mod, new_mem);
3692 set_irn_pinned(mod, op_pin_state_floats);
3697 case pn_Mod_X_regular:
3698 return new_r_Jmp(get_irn_n(mod, -1));
3700 case pn_Mod_X_except: {
3701 ir_graph *irg = get_irn_irg(proj);
3702 /* we found an exception handler, remove it */
3703 DBG_OPT_EXC_REM(proj);
3704 return get_irg_bad(irg);
3708 ir_graph *irg = get_irn_irg(proj);
3709 res = get_Mod_mem(mod);
3710 new_mem = get_irg_no_mem(irg);
3713 /* This node can only float up to the Confirm block */
3714 new_mem = new_r_Pin(get_nodes_block(confirm), new_mem);
3716 /* this is a Mod without exception, we can remove the memory edge */
3717 set_Mod_mem(mod, new_mem);
3721 if (get_Mod_left(mod) == b) {
3722 /* a % a = 0 if a != 0 */
3723 ir_graph *irg = get_irn_irg(proj);
3724 ir_mode *mode = get_irn_mode(proj);
3725 ir_node *res = new_r_Const(irg, get_mode_null(mode));
3727 DBG_OPT_CSTEVAL(mod, res);
3733 } /* transform_node_Proj_Mod */
3736 * Optimizes jump tables (CondIs or CondIu) by removing all impossible cases.
3738 static ir_node *transform_node_Proj_Cond(ir_node *proj)
3740 if (get_opt_unreachable_code()) {
3741 ir_node *n = get_Proj_pred(proj);
3742 ir_node *b = get_Cond_selector(n);
3744 if (mode_is_int(get_irn_mode(b))) {
3745 ir_tarval *tb = value_of(b);
3747 if (tb != tarval_bad) {
3748 /* we have a constant switch */
3749 long num = get_Proj_proj(proj);
3751 if (num != get_Cond_default_proj(n)) { /* we cannot optimize default Proj's yet */
3752 if (get_tarval_long(tb) == num) {
3753 /* Do NOT create a jump here, or we will have 2 control flow ops
3754 * in a block. This case is optimized away in optimize_cf(). */
3757 ir_graph *irg = get_irn_irg(proj);
3758 /* this case will NEVER be taken, kill it */
3759 return get_irg_bad(irg);
3763 long num = get_Proj_proj(proj);
3764 vrp_attr *b_vrp = vrp_get_info(b);
3765 if (num != get_Cond_default_proj(n) && b_vrp) {
3766 /* Try handling with vrp data. We only remove dead parts. */
3767 ir_tarval *tp = new_tarval_from_long(num, get_irn_mode(b));
3769 if (b_vrp->range_type == VRP_RANGE) {
3770 pn_Cmp cmp_result = tarval_cmp(b_vrp->range_bottom, tp);
3771 pn_Cmp cmp_result2 = tarval_cmp(b_vrp->range_top, tp);
3773 if ((cmp_result & pn_Cmp_Gt) == cmp_result && (cmp_result2
3774 & pn_Cmp_Lt) == cmp_result2) {
3775 ir_graph *irg = get_irn_irg(proj);
3776 return get_irg_bad(irg);
3778 } else if (b_vrp->range_type == VRP_ANTIRANGE) {
3779 pn_Cmp cmp_result = tarval_cmp(b_vrp->range_bottom, tp);
3780 pn_Cmp cmp_result2 = tarval_cmp(b_vrp->range_top, tp);
3782 if ((cmp_result & pn_Cmp_Le) == cmp_result && (cmp_result2
3783 & pn_Cmp_Ge) == cmp_result2) {
3784 ir_graph *irg = get_irn_irg(proj);
3785 return get_irg_bad(irg);
3790 tarval_and( b_vrp->bits_set, tp),
3793 ir_graph *irg = get_irn_irg(proj);
3794 return get_irg_bad(irg);
3800 tarval_not(b_vrp->bits_not_set)),
3801 tarval_not(b_vrp->bits_not_set))
3803 ir_graph *irg = get_irn_irg(proj);
3804 return get_irg_bad(irg);
3813 } /* transform_node_Proj_Cond */
3816 * Create a 0 constant of given mode.
3818 static ir_node *create_zero_const(ir_graph *irg, ir_mode *mode)
3820 ir_tarval *tv = get_mode_null(mode);
3821 ir_node *cnst = new_r_Const(irg, tv);
3827 * Normalizes and optimizes Cmp nodes.
3829 static ir_node *transform_node_Proj_Cmp(ir_node *proj)
3831 ir_node *n = get_Proj_pred(proj);
3832 ir_node *left = get_Cmp_left(n);
3833 ir_node *right = get_Cmp_right(n);
3834 ir_tarval *tv = NULL;
3836 ir_mode *mode = NULL;
3837 long proj_nr = get_Proj_proj(proj);
3839 /* we can evaluate some cases directly */
3841 case pn_Cmp_False: {
3842 ir_graph *irg = get_irn_irg(proj);
3843 return new_r_Const(irg, get_tarval_b_false());
3846 ir_graph *irg = get_irn_irg(proj);
3847 return new_r_Const(irg, get_tarval_b_true());
3850 if (!mode_is_float(get_irn_mode(left))) {
3851 ir_graph *irg = get_irn_irg(proj);
3852 return new_r_Const(irg, get_tarval_b_true());
3859 /* remove Casts of both sides */
3860 left = skip_Cast(left);
3861 right = skip_Cast(right);
3863 /* Remove unnecessary conversions */
3864 /* TODO handle constants */
3865 if (is_Conv(left) && is_Conv(right)) {
3866 ir_mode *mode = get_irn_mode(left);
3867 ir_node *op_left = get_Conv_op(left);
3868 ir_node *op_right = get_Conv_op(right);
3869 ir_mode *mode_left = get_irn_mode(op_left);
3870 ir_mode *mode_right = get_irn_mode(op_right);
3872 if (smaller_mode(mode_left, mode) && smaller_mode(mode_right, mode)
3873 && mode_left != mode_b && mode_right != mode_b) {
3874 ir_node *block = get_nodes_block(n);
3876 if (mode_left == mode_right) {
3880 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV_CONV);
3881 } else if (smaller_mode(mode_left, mode_right)) {
3882 left = new_r_Conv(block, op_left, mode_right);
3885 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV);
3886 } else if (smaller_mode(mode_right, mode_left)) {
3888 right = new_r_Conv(block, op_right, mode_left);
3890 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV);
3895 /* remove operation on both sides if possible */
3896 if (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) {
3898 * The following operations are NOT safe for floating point operations, for instance
3899 * 1.0 + inf == 2.0 + inf, =/=> x == y
3901 if (mode_is_int(get_irn_mode(left))) {
3902 unsigned lop = get_irn_opcode(left);
3904 if (lop == get_irn_opcode(right)) {
3905 ir_node *ll, *lr, *rl, *rr;
3907 /* same operation on both sides, try to remove */
3911 /* ~a CMP ~b => a CMP b, -a CMP -b ==> a CMP b */
3912 left = get_unop_op(left);
3913 right = get_unop_op(right);
3915 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3918 ll = get_Add_left(left);
3919 lr = get_Add_right(left);
3920 rl = get_Add_left(right);
3921 rr = get_Add_right(right);
3924 /* X + a CMP X + b ==> a CMP b */
3928 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3929 } else if (ll == rr) {
3930 /* X + a CMP b + X ==> a CMP b */
3934 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3935 } else if (lr == rl) {
3936 /* a + X CMP X + b ==> a CMP b */
3940 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3941 } else if (lr == rr) {
3942 /* a + X CMP b + X ==> a CMP b */
3946 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3950 ll = get_Sub_left(left);
3951 lr = get_Sub_right(left);
3952 rl = get_Sub_left(right);
3953 rr = get_Sub_right(right);
3956 /* X - a CMP X - b ==> a CMP b */
3960 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3961 } else if (lr == rr) {
3962 /* a - X CMP b - X ==> a CMP b */
3966 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3970 if (get_Rotl_right(left) == get_Rotl_right(right)) {
3971 /* a ROTL X CMP b ROTL X ==> a CMP b */
3972 left = get_Rotl_left(left);
3973 right = get_Rotl_left(right);
3975 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3983 /* X+A == A, A+X == A, A-X == A -> X == 0 */
3984 if (is_Add(left) || is_Sub(left)) {
3985 ir_node *ll = get_binop_left(left);
3986 ir_node *lr = get_binop_right(left);
3988 if (lr == right && is_Add(left)) {
3994 ir_graph *irg = get_irn_irg(n);
3996 right = create_zero_const(irg, get_irn_mode(left));
3998 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4001 if (is_Add(right) || is_Sub(right)) {
4002 ir_node *rl = get_binop_left(right);
4003 ir_node *rr = get_binop_right(right);
4005 if (rr == left && is_Add(right)) {
4011 ir_graph *irg = get_irn_irg(n);
4013 right = create_zero_const(irg, get_irn_mode(left));
4015 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4018 if (is_And(left) && is_Const(right)) {
4019 ir_node *ll = get_binop_left(left);
4020 ir_node *lr = get_binop_right(left);
4021 if (is_Shr(ll) && is_Const(lr)) {
4022 /* Cmp((x >>u c1) & c2, c3) = Cmp(x & (c2 << c1), c3 << c1) */
4023 ir_node *block = get_nodes_block(n);
4024 ir_mode *mode = get_irn_mode(left);
4026 ir_node *llr = get_Shr_right(ll);
4027 if (is_Const(llr)) {
4028 dbg_info *dbg = get_irn_dbg_info(left);
4029 ir_graph *irg = get_irn_irg(left);
4031 ir_tarval *c1 = get_Const_tarval(llr);
4032 ir_tarval *c2 = get_Const_tarval(lr);
4033 ir_tarval *c3 = get_Const_tarval(right);
4034 ir_tarval *mask = tarval_shl(c2, c1);
4035 ir_tarval *value = tarval_shl(c3, c1);
4037 left = new_rd_And(dbg, block, get_Shr_left(ll), new_r_Const(irg, mask), mode);
4038 right = new_r_Const(irg, value);
4043 /* Cmp(Eor(x, y), 0) <=> Cmp(x, y) at least for the ==0,!=0
4045 if (is_Const(right) && is_Const_null(right) && is_Eor(left)) {
4046 right = get_Eor_right(left);
4047 left = get_Eor_left(left);
4050 } /* mode_is_int(...) */
4051 } /* proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg */
4053 /* replace mode_b compares with ands/ors */
4054 if (get_irn_mode(left) == mode_b) {
4055 ir_node *block = get_nodes_block(n);
4059 case pn_Cmp_Le: bres = new_r_Or( block, new_r_Not(block, left, mode_b), right, mode_b); break;
4060 case pn_Cmp_Lt: bres = new_r_And(block, new_r_Not(block, left, mode_b), right, mode_b); break;
4061 case pn_Cmp_Ge: bres = new_r_Or( block, left, new_r_Not(block, right, mode_b), mode_b); break;
4062 case pn_Cmp_Gt: bres = new_r_And(block, left, new_r_Not(block, right, mode_b), mode_b); break;
4063 case pn_Cmp_Lg: bres = new_r_Eor(block, left, right, mode_b); break;
4064 case pn_Cmp_Eq: bres = new_r_Not(block, new_r_Eor(block, left, right, mode_b), mode_b); break;
4065 default: bres = NULL;
4068 DBG_OPT_ALGSIM0(n, bres, FS_OPT_CMP_TO_BOOL);
4074 * First step: normalize the compare op
4075 * by placing the constant on the right side
4076 * or moving the lower address node to the left.
4078 if (!operands_are_normalized(left, right)) {
4084 proj_nr = get_inversed_pnc(proj_nr);
4089 * Second step: Try to reduce the magnitude
4090 * of a constant. This may help to generate better code
4091 * later and may help to normalize more compares.
4092 * Of course this is only possible for integer values.
4094 tv = value_of(right);
4095 if (tv != tarval_bad) {
4096 mode = get_irn_mode(right);
4098 /* TODO extend to arbitrary constants */
4099 if (is_Conv(left) && tarval_is_null(tv)) {
4100 ir_node *op = get_Conv_op(left);
4101 ir_mode *op_mode = get_irn_mode(op);
4104 * UpConv(x) REL 0 ==> x REL 0
4105 * Don't do this for float values as it's unclear whether it is a
4106 * win. (on the other side it makes detection/creation of fabs hard)
4108 if (get_mode_size_bits(mode) > get_mode_size_bits(op_mode) &&
4109 ((proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) ||
4110 mode_is_signed(mode) || !mode_is_signed(op_mode)) &&
4111 !mode_is_float(mode)) {
4112 tv = get_mode_null(op_mode);
4116 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV);
4120 if (tv != tarval_bad) {
4121 /* the following optimization is possible on modes without Overflow
4122 * on Unary Minus or on == and !=:
4123 * -a CMP c ==> a swap(CMP) -c
4125 * Beware: for two-complement Overflow may occur, so only == and != can
4126 * be optimized, see this:
4127 * -MININT < 0 =/=> MININT > 0 !!!
4129 if (is_Minus(left) &&
4130 (!mode_overflow_on_unary_Minus(mode) ||
4131 (mode_is_int(mode) && (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg)))) {
4132 tv = tarval_neg(tv);
4134 if (tv != tarval_bad) {
4135 left = get_Minus_op(left);
4136 proj_nr = get_inversed_pnc(proj_nr);
4138 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4140 } else if (is_Not(left) && (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg)) {
4141 /* Not(a) ==/!= c ==> a ==/!= Not(c) */
4142 tv = tarval_not(tv);
4144 if (tv != tarval_bad) {
4145 left = get_Not_op(left);
4147 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4151 /* for integer modes, we have more */
4152 if (mode_is_int(mode)) {
4153 /* Ne includes Unordered which is not possible on integers.
4154 * However, frontends often use this wrong, so fix it here */
4155 if (proj_nr & pn_Cmp_Uo) {
4156 proj_nr &= ~pn_Cmp_Uo;
4157 set_Proj_proj(proj, proj_nr);
4160 /* c > 0 : a < c ==> a <= (c-1) a >= c ==> a > (c-1) */
4161 if ((proj_nr == pn_Cmp_Lt || proj_nr == pn_Cmp_Ge) &&
4162 tarval_cmp(tv, get_mode_null(mode)) == pn_Cmp_Gt) {
4163 tv = tarval_sub(tv, get_mode_one(mode), NULL);
4165 if (tv != tarval_bad) {
4166 proj_nr ^= pn_Cmp_Eq;
4168 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CNST_MAGN);
4171 /* c < 0 : a > c ==> a >= (c+1) a <= c ==> a < (c+1) */
4172 else if ((proj_nr == pn_Cmp_Gt || proj_nr == pn_Cmp_Le) &&
4173 tarval_cmp(tv, get_mode_null(mode)) == pn_Cmp_Lt) {
4174 tv = tarval_add(tv, get_mode_one(mode));
4176 if (tv != tarval_bad) {
4177 proj_nr ^= pn_Cmp_Eq;
4179 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CNST_MAGN);
4183 /* the following reassociations work only for == and != */
4184 if (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) {
4186 #if 0 /* Might be not that good in general */
4187 /* a-b == 0 ==> a == b, a-b != 0 ==> a != b */
4188 if (tarval_is_null(tv) && is_Sub(left)) {
4189 right = get_Sub_right(left);
4190 left = get_Sub_left(left);
4192 tv = value_of(right);
4194 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4198 if (tv != tarval_bad) {
4199 /* a-c1 == c2 ==> a == c2+c1, a-c1 != c2 ==> a != c2+c1 */
4201 ir_node *c1 = get_Sub_right(left);
4202 ir_tarval *tv2 = value_of(c1);
4204 if (tv2 != tarval_bad) {
4205 tv2 = tarval_add(tv, value_of(c1));
4207 if (tv2 != tarval_bad) {
4208 left = get_Sub_left(left);
4211 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4215 /* a+c1 == c2 ==> a == c2-c1, a+c1 != c2 ==> a != c2-c1 */
4216 else if (is_Add(left)) {
4217 ir_node *a_l = get_Add_left(left);
4218 ir_node *a_r = get_Add_right(left);
4222 if (is_Const(a_l)) {
4224 tv2 = value_of(a_l);
4227 tv2 = value_of(a_r);
4230 if (tv2 != tarval_bad) {
4231 tv2 = tarval_sub(tv, tv2, NULL);
4233 if (tv2 != tarval_bad) {
4237 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4241 /* -a == c ==> a == -c, -a != c ==> a != -c */
4242 else if (is_Minus(left)) {
4243 ir_tarval *tv2 = tarval_sub(get_mode_null(mode), tv, NULL);
4245 if (tv2 != tarval_bad) {
4246 left = get_Minus_op(left);
4249 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4256 if (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) {
4257 switch (get_irn_opcode(left)) {
4261 c1 = get_And_right(left);
4264 * And(x, C1) == C2 ==> FALSE if C2 & C1 != C2
4265 * And(x, C1) != C2 ==> TRUE if C2 & C1 != C2
4267 ir_tarval *mask = tarval_and(get_Const_tarval(c1), tv);
4269 /* TODO: move to constant evaluation */
4270 ir_graph *irg = get_irn_irg(n);
4271 tv = proj_nr == pn_Cmp_Eq ? get_tarval_b_false() : get_tarval_b_true();
4272 c1 = new_r_Const(irg, tv);
4273 DBG_OPT_CSTEVAL(proj, c1);
4277 if (tarval_is_single_bit(tv)) {
4279 * optimization for AND:
4281 * And(x, C) == C ==> And(x, C) != 0
4282 * And(x, C) != C ==> And(X, C) == 0
4284 * if C is a single Bit constant.
4287 /* check for Constant's match. We have check hare the tarvals,
4288 because our const might be changed */
4289 if (get_Const_tarval(c1) == tv) {
4290 /* fine: do the transformation */
4291 tv = get_mode_null(get_tarval_mode(tv));
4292 proj_nr ^= pn_Cmp_Leg;
4294 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CNST_MAGN);
4300 c1 = get_Or_right(left);
4301 if (is_Const(c1) && tarval_is_null(tv)) {
4303 * Or(x, C) == 0 && C != 0 ==> FALSE
4304 * Or(x, C) != 0 && C != 0 ==> TRUE
4306 if (! tarval_is_null(get_Const_tarval(c1))) {
4307 /* TODO: move to constant evaluation */
4308 ir_graph *irg = get_irn_irg(n);
4309 tv = proj_nr == pn_Cmp_Eq ? get_tarval_b_false() : get_tarval_b_true();
4310 c1 = new_r_Const(irg, tv);
4311 DBG_OPT_CSTEVAL(proj, c1);
4318 * optimize x << c1 == c into x & (-1 >>u c1) == c >> c1 if c & (-1 << c1) == c
4320 * optimize x << c1 != c into x & (-1 >>u c1) != c >> c1 if c & (-1 << c1) == c
4323 c1 = get_Shl_right(left);
4325 ir_graph *irg = get_irn_irg(c1);
4326 ir_tarval *tv1 = get_Const_tarval(c1);
4327 ir_mode *mode = get_irn_mode(left);
4328 ir_tarval *minus1 = get_mode_all_one(mode);
4329 ir_tarval *amask = tarval_shr(minus1, tv1);
4330 ir_tarval *cmask = tarval_shl(minus1, tv1);
4333 if (tarval_and(tv, cmask) != tv) {
4334 /* condition not met */
4335 tv = proj_nr == pn_Cmp_Eq ? get_tarval_b_false() : get_tarval_b_true();
4336 c1 = new_r_Const(irg, tv);
4337 DBG_OPT_CSTEVAL(proj, c1);
4340 sl = get_Shl_left(left);
4341 blk = get_nodes_block(n);
4342 left = new_rd_And(get_irn_dbg_info(left), blk, sl, new_r_Const(irg, amask), mode);
4343 tv = tarval_shr(tv, tv1);
4345 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_SHF_TO_AND);
4350 * optimize x >>u c1 == c into x & (-1 << c1) == c << c1 if c & (-1 >>u c1) == c
4352 * optimize x >>u c1 != c into x & (-1 << c1) != c << c1 if c & (-1 >>u c1) == c
4355 c1 = get_Shr_right(left);
4357 ir_graph *irg = get_irn_irg(c1);
4358 ir_tarval *tv1 = get_Const_tarval(c1);
4359 ir_mode *mode = get_irn_mode(left);
4360 ir_tarval *minus1 = get_mode_all_one(mode);
4361 ir_tarval *amask = tarval_shl(minus1, tv1);
4362 ir_tarval *cmask = tarval_shr(minus1, tv1);
4365 if (tarval_and(tv, cmask) != tv) {
4366 /* condition not met */
4367 tv = proj_nr == pn_Cmp_Eq ? get_tarval_b_false() : get_tarval_b_true();
4368 c1 = new_r_Const(irg, tv);
4369 DBG_OPT_CSTEVAL(proj, c1);
4372 sl = get_Shr_left(left);
4373 blk = get_nodes_block(n);
4374 left = new_rd_And(get_irn_dbg_info(left), blk, sl, new_r_Const(irg, amask), mode);
4375 tv = tarval_shl(tv, tv1);
4377 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_SHF_TO_AND);
4382 * optimize x >>s c1 == c into x & (-1 << c1) == c << c1 if (c >>s (BITS - c1)) \in {0,-1}
4384 * optimize x >>s c1 != c into x & (-1 << c1) != c << c1 if (c >>s (BITS - c1)) \in {0,-1}
4387 c1 = get_Shrs_right(left);
4389 ir_graph *irg = get_irn_irg(c1);
4390 ir_tarval *tv1 = get_Const_tarval(c1);
4391 ir_mode *mode = get_irn_mode(left);
4392 ir_tarval *minus1 = get_mode_all_one(mode);
4393 ir_tarval *amask = tarval_shl(minus1, tv1);
4394 ir_tarval *cond = new_tarval_from_long(get_mode_size_bits(mode), get_tarval_mode(tv1));
4397 cond = tarval_sub(cond, tv1, NULL);
4398 cond = tarval_shrs(tv, cond);
4400 if (!tarval_is_all_one(cond) && !tarval_is_null(cond)) {
4401 /* condition not met */
4402 tv = proj_nr == pn_Cmp_Eq ? get_tarval_b_false() : get_tarval_b_true();
4403 c1 = new_r_Const(irg, tv);
4404 DBG_OPT_CSTEVAL(proj, c1);
4407 sl = get_Shrs_left(left);
4408 blk = get_nodes_block(n);
4409 left = new_rd_And(get_irn_dbg_info(left), blk, sl, new_r_Const(irg, amask), mode);
4410 tv = tarval_shl(tv, tv1);
4412 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_SHF_TO_AND);
4417 } /* tarval != bad */
4420 if (changed & 2) { /* need a new Const */
4421 ir_graph *irg = get_irn_irg(n);
4422 right = new_r_Const(irg, tv);
4425 if ((proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) && is_Const(right) && is_Const_null(right) && is_Proj(left)) {
4426 ir_node *op = get_Proj_pred(left);
4428 if (is_Mod(op) && get_Proj_proj(left) == pn_Mod_res) {
4429 ir_node *c = get_binop_right(op);
4432 ir_tarval *tv = get_Const_tarval(c);
4434 if (tarval_is_single_bit(tv)) {
4435 /* special case: (x % 2^n) CMP 0 ==> x & (2^n-1) CMP 0 */
4436 ir_node *v = get_binop_left(op);
4437 ir_node *blk = get_irn_n(op, -1);
4438 ir_graph *irg = get_irn_irg(op);
4439 ir_mode *mode = get_irn_mode(v);
4441 tv = tarval_sub(tv, get_mode_one(mode), NULL);
4442 left = new_rd_And(get_irn_dbg_info(op), blk, v, new_r_Const(irg, tv), mode);
4444 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_MOD_TO_AND);
4451 ir_node *block = get_nodes_block(n);
4453 /* create a new compare */
4454 n = new_rd_Cmp(get_irn_dbg_info(n), block, left, right);
4455 proj = new_rd_Proj(get_irn_dbg_info(proj), n, get_irn_mode(proj), proj_nr);
4459 } /* transform_node_Proj_Cmp */
4462 * Optimize CopyB(mem, x, x) into a Nop.
4464 static ir_node *transform_node_Proj_CopyB(ir_node *proj)
4466 ir_node *copyb = get_Proj_pred(proj);
4467 ir_node *a = get_CopyB_dst(copyb);
4468 ir_node *b = get_CopyB_src(copyb);
4471 switch (get_Proj_proj(proj)) {
4472 case pn_CopyB_X_regular:
4473 /* Turn CopyB into a tuple (mem, jmp, bad, bad) */
4474 DBG_OPT_EXC_REM(proj);
4475 proj = new_r_Jmp(get_nodes_block(copyb));
4477 case pn_CopyB_X_except:
4478 DBG_OPT_EXC_REM(proj);
4479 proj = get_irg_bad(get_irn_irg(proj));
4486 } /* transform_node_Proj_CopyB */
4489 * Optimize Bounds(idx, idx, upper) into idx.
4491 static ir_node *transform_node_Proj_Bound(ir_node *proj)
4493 ir_node *oldn = proj;
4494 ir_node *bound = get_Proj_pred(proj);
4495 ir_node *idx = get_Bound_index(bound);
4496 ir_node *pred = skip_Proj(idx);
4499 if (idx == get_Bound_lower(bound))
4501 else if (is_Bound(pred)) {
4503 * idx was Bounds checked previously, it is still valid if
4504 * lower <= pred_lower && pred_upper <= upper.
4506 ir_node *lower = get_Bound_lower(bound);
4507 ir_node *upper = get_Bound_upper(bound);
4508 if (get_Bound_lower(pred) == lower &&
4509 get_Bound_upper(pred) == upper) {
4511 * One could expect that we simply return the previous
4512 * Bound here. However, this would be wrong, as we could
4513 * add an exception Proj to a new location then.
4514 * So, we must turn in into a tuple.
4520 /* Turn Bound into a tuple (mem, jmp, bad, idx) */
4521 switch (get_Proj_proj(proj)) {
4523 DBG_OPT_EXC_REM(proj);
4524 proj = get_Bound_mem(bound);
4526 case pn_Bound_X_except:
4527 DBG_OPT_EXC_REM(proj);
4528 proj = get_irg_bad(get_irn_irg(proj));
4532 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NOP);
4534 case pn_Bound_X_regular:
4535 DBG_OPT_EXC_REM(proj);
4536 proj = new_r_Jmp(get_nodes_block(bound));
4543 } /* transform_node_Proj_Bound */
4546 * Does all optimizations on nodes that must be done on it's Proj's
4547 * because of creating new nodes.
4549 static ir_node *transform_node_Proj(ir_node *proj)
4551 ir_node *n = get_Proj_pred(proj);
4553 if (n->op->ops.transform_node_Proj)
4554 return n->op->ops.transform_node_Proj(proj);
4556 } /* transform_node_Proj */
4559 * Move Confirms down through Phi nodes.
4561 static ir_node *transform_node_Phi(ir_node *phi)
4564 ir_mode *mode = get_irn_mode(phi);
4566 if (mode_is_reference(mode)) {
4567 n = get_irn_arity(phi);
4569 /* Beware of Phi0 */
4571 ir_node *pred = get_irn_n(phi, 0);
4572 ir_node *bound, *new_phi, *block, **in;
4575 if (! is_Confirm(pred))
4578 bound = get_Confirm_bound(pred);
4579 pnc = get_Confirm_cmp(pred);
4581 NEW_ARR_A(ir_node *, in, n);
4582 in[0] = get_Confirm_value(pred);
4584 for (i = 1; i < n; ++i) {
4585 pred = get_irn_n(phi, i);
4587 if (! is_Confirm(pred) ||
4588 get_Confirm_bound(pred) != bound ||
4589 get_Confirm_cmp(pred) != pnc)
4591 in[i] = get_Confirm_value(pred);
4593 /* move the Confirm nodes "behind" the Phi */
4594 block = get_irn_n(phi, -1);
4595 new_phi = new_r_Phi(block, n, in, get_irn_mode(phi));
4596 return new_r_Confirm(block, new_phi, bound, pnc);
4600 } /* transform_node_Phi */
4603 * Returns the operands of a commutative bin-op, if one operand is
4604 * a const, it is returned as the second one.
4606 static void get_comm_Binop_Ops(ir_node *binop, ir_node **a, ir_node **c)
4608 ir_node *op_a = get_binop_left(binop);
4609 ir_node *op_b = get_binop_right(binop);
4611 assert(is_op_commutative(get_irn_op(binop)));
4613 if (is_Const(op_a)) {
4620 } /* get_comm_Binop_Ops */
4623 * Optimize a Or(And(Or(And(v,c4),c3),c2),c1) pattern if possible.
4624 * Such pattern may arise in bitfield stores.
4626 * value c4 value c4 & c2
4627 * AND c3 AND c1 | c3
4634 * AND c1 ===> OR if (c1 | c2) == 0x111..11
4637 static ir_node *transform_node_Or_bf_store(ir_node *irn_or)
4639 ir_node *irn_and, *c1;
4641 ir_node *and_l, *c3;
4642 ir_node *value, *c4;
4643 ir_node *new_and, *new_const, *block;
4644 ir_mode *mode = get_irn_mode(irn_or);
4646 ir_tarval *tv1, *tv2, *tv3, *tv4, *tv;
4650 get_comm_Binop_Ops(irn_or, &irn_and, &c1);
4651 if (!is_Const(c1) || !is_And(irn_and))
4654 get_comm_Binop_Ops(irn_and, &or_l, &c2);
4658 tv1 = get_Const_tarval(c1);
4659 tv2 = get_Const_tarval(c2);
4661 tv = tarval_or(tv1, tv2);
4662 if (tarval_is_all_one(tv)) {
4663 /* the AND does NOT clear a bit with isn't set by the OR */
4664 set_Or_left(irn_or, or_l);
4665 set_Or_right(irn_or, c1);
4667 /* check for more */
4674 get_comm_Binop_Ops(or_l, &and_l, &c3);
4675 if (!is_Const(c3) || !is_And(and_l))
4678 get_comm_Binop_Ops(and_l, &value, &c4);
4682 /* ok, found the pattern, check for conditions */
4683 assert(mode == get_irn_mode(irn_and));
4684 assert(mode == get_irn_mode(or_l));
4685 assert(mode == get_irn_mode(and_l));
4687 tv3 = get_Const_tarval(c3);
4688 tv4 = get_Const_tarval(c4);
4690 tv = tarval_or(tv4, tv2);
4691 if (!tarval_is_all_one(tv)) {
4692 /* have at least one 0 at the same bit position */
4696 if (tv3 != tarval_andnot(tv3, tv4)) {
4697 /* bit in the or_mask is outside the and_mask */
4701 if (tv1 != tarval_andnot(tv1, tv2)) {
4702 /* bit in the or_mask is outside the and_mask */
4706 /* ok, all conditions met */
4707 block = get_irn_n(irn_or, -1);
4708 irg = get_irn_irg(block);
4710 new_and = new_r_And(block, value, new_r_Const(irg, tarval_and(tv4, tv2)), mode);
4712 new_const = new_r_Const(irg, tarval_or(tv3, tv1));
4714 set_Or_left(irn_or, new_and);
4715 set_Or_right(irn_or, new_const);
4717 /* check for more */
4719 } /* transform_node_Or_bf_store */
4722 * Optimize an Or(shl(x, c), shr(x, bits - c)) into a Rotl
4724 static ir_node *transform_node_Or_Rotl(ir_node *irn_or)
4726 ir_mode *mode = get_irn_mode(irn_or);
4727 ir_node *shl, *shr, *block;
4728 ir_node *irn, *x, *c1, *c2, *n;
4729 ir_tarval *tv1, *tv2;
4731 /* some backends can't handle rotl */
4732 if (!be_get_backend_param()->support_rotl)
4735 if (! mode_is_int(mode))
4738 shl = get_binop_left(irn_or);
4739 shr = get_binop_right(irn_or);
4748 } else if (!is_Shl(shl)) {
4750 } else if (!is_Shr(shr)) {
4753 x = get_Shl_left(shl);
4754 if (x != get_Shr_left(shr))
4757 c1 = get_Shl_right(shl);
4758 c2 = get_Shr_right(shr);
4759 if (is_Const(c1) && is_Const(c2)) {
4760 tv1 = get_Const_tarval(c1);
4761 if (! tarval_is_long(tv1))
4764 tv2 = get_Const_tarval(c2);
4765 if (! tarval_is_long(tv2))
4768 if (get_tarval_long(tv1) + get_tarval_long(tv2)
4769 != (int) get_mode_size_bits(mode))
4772 /* yet, condition met */
4773 block = get_nodes_block(irn_or);
4775 n = new_r_Rotl(block, x, c1, mode);
4777 DBG_OPT_ALGSIM1(irn_or, shl, shr, n, FS_OPT_OR_SHFT_TO_ROTL);
4781 /* Note: the obvious rot formulation (a << x) | (a >> (32-x)) gets
4782 * transformed to (a << x) | (a >> -x) by transform_node_shift_modulo() */
4783 if (!is_negated_value(c1, c2)) {
4787 /* yet, condition met */
4788 block = get_nodes_block(irn_or);
4789 n = new_r_Rotl(block, x, c1, mode);
4790 DBG_OPT_ALGSIM0(irn_or, n, FS_OPT_OR_SHFT_TO_ROTL);
4792 } /* transform_node_Or_Rotl */
4797 static ir_node *transform_node_Or(ir_node *n)
4799 ir_node *c, *oldn = n;
4800 ir_node *a = get_Or_left(n);
4801 ir_node *b = get_Or_right(n);
4804 if (is_Not(a) && is_Not(b)) {
4805 /* ~a | ~b = ~(a&b) */
4806 ir_node *block = get_nodes_block(n);
4808 mode = get_irn_mode(n);
4811 n = new_rd_And(get_irn_dbg_info(n), block, a, b, mode);
4812 n = new_rd_Not(get_irn_dbg_info(n), block, n, mode);
4813 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_DEMORGAN);
4817 /* we can evaluate 2 Projs of the same Cmp */
4818 if (get_irn_mode(n) == mode_b && is_Proj(a) && is_Proj(b)) {
4819 ir_node *pred_a = get_Proj_pred(a);
4820 ir_node *pred_b = get_Proj_pred(b);
4821 if (pred_a == pred_b) {
4822 dbg_info *dbgi = get_irn_dbg_info(n);
4823 pn_Cmp pn_a = get_Proj_pn_cmp(a);
4824 pn_Cmp pn_b = get_Proj_pn_cmp(b);
4825 /* yes, we can simply calculate with pncs */
4826 pn_Cmp new_pnc = pn_a | pn_b;
4828 return new_rd_Proj(dbgi, pred_a, mode_b, new_pnc);
4832 mode = get_irn_mode(n);
4833 HANDLE_BINOP_PHI((eval_func) tarval_or, a, b, c, mode);
4835 n = transform_node_Or_bf_store(n);
4836 n = transform_node_Or_Rotl(n);
4840 n = transform_bitwise_distributive(n, transform_node_Or);
4843 } /* transform_node_Or */
4847 static ir_node *transform_node(ir_node *n);
4850 * Optimize (a >> c1) >> c2), works for Shr, Shrs, Shl, Rotl.
4852 * Should be moved to reassociation?
4854 static ir_node *transform_node_shift(ir_node *n)
4856 ir_node *left, *right;
4858 ir_tarval *tv1, *tv2, *res;
4859 ir_node *in[2], *irn, *block;
4862 left = get_binop_left(n);
4864 /* different operations */
4865 if (get_irn_op(left) != get_irn_op(n))
4868 right = get_binop_right(n);
4869 tv1 = value_of(right);
4870 if (tv1 == tarval_bad)
4873 tv2 = value_of(get_binop_right(left));
4874 if (tv2 == tarval_bad)
4877 res = tarval_add(tv1, tv2);
4878 mode = get_irn_mode(n);
4879 irg = get_irn_irg(n);
4881 /* beware: a simple replacement works only, if res < modulo shift */
4883 int modulo_shf = get_mode_modulo_shift(mode);
4884 if (modulo_shf > 0) {
4885 ir_tarval *modulo = new_tarval_from_long(modulo_shf,
4886 get_tarval_mode(res));
4888 assert(modulo_shf >= (int) get_mode_size_bits(mode));
4890 /* shifting too much */
4891 if (!(tarval_cmp(res, modulo) & pn_Cmp_Lt)) {
4893 ir_node *block = get_nodes_block(n);
4894 dbg_info *dbgi = get_irn_dbg_info(n);
4895 ir_mode *smode = get_irn_mode(right);
4896 ir_node *cnst = new_r_Const_long(irg, smode, get_mode_size_bits(mode) - 1);
4897 return new_rd_Shrs(dbgi, block, get_binop_left(left), cnst, mode);
4900 return new_r_Const(irg, get_mode_null(mode));
4904 res = tarval_mod(res, new_tarval_from_long(get_mode_size_bits(mode), get_tarval_mode(res)));
4907 /* ok, we can replace it */
4908 block = get_nodes_block(n);
4910 in[0] = get_binop_left(left);
4911 in[1] = new_r_Const(irg, res);
4913 irn = new_ir_node(NULL, get_Block_irg(block), block, get_irn_op(n), mode, 2, in);
4915 DBG_OPT_ALGSIM0(n, irn, FS_OPT_REASSOC_SHIFT);
4917 return transform_node(irn);
4918 } /* transform_node_shift */
4921 * normalisation: (x & c1) >> c2 to (x >> c2) & (c1 >> c2)
4923 * - and, or, xor instead of &
4924 * - Shl, Shr, Shrs, rotl instead of >>
4925 * (with a special case for Or/Xor + Shrs)
4927 static ir_node *transform_node_bitop_shift(ir_node *n)
4930 ir_node *right = get_binop_right(n);
4931 ir_mode *mode = get_irn_mode(n);
4932 ir_node *bitop_left;
4933 ir_node *bitop_right;
4943 ir_tarval *tv_shift;
4945 assert(is_Shrs(n) || is_Shr(n) || is_Shl(n) || is_Rotl(n));
4947 if (!is_Const(right))
4950 left = get_binop_left(n);
4951 op_left = get_irn_op(left);
4952 if (op_left != op_And && op_left != op_Or && op_left != op_Eor)
4955 /* doing it with Shrs is not legal if the Or/Eor affects the topmost bit */
4956 if (is_Shrs(n) && (op_left == op_Or || op_left == op_Eor)) {
4957 /* TODO: test if sign bit is affectes */
4961 bitop_right = get_binop_right(left);
4962 if (!is_Const(bitop_right))
4965 bitop_left = get_binop_left(left);
4967 block = get_nodes_block(n);
4968 dbgi = get_irn_dbg_info(n);
4969 tv1 = get_Const_tarval(bitop_right);
4970 tv2 = get_Const_tarval(right);
4972 assert(get_tarval_mode(tv1) == mode);
4975 new_shift = new_rd_Shl(dbgi, block, bitop_left, right, mode);
4976 tv_shift = tarval_shl(tv1, tv2);
4977 } else if (is_Shr(n)) {
4978 new_shift = new_rd_Shr(dbgi, block, bitop_left, right, mode);
4979 tv_shift = tarval_shr(tv1, tv2);
4980 } else if (is_Shrs(n)) {
4981 new_shift = new_rd_Shrs(dbgi, block, bitop_left, right, mode);
4982 tv_shift = tarval_shrs(tv1, tv2);
4985 new_shift = new_rd_Rotl(dbgi, block, bitop_left, right, mode);
4986 tv_shift = tarval_rotl(tv1, tv2);
4989 assert(get_tarval_mode(tv_shift) == mode);
4990 irg = get_irn_irg(n);
4991 new_const = new_r_Const(irg, tv_shift);
4993 if (op_left == op_And) {
4994 new_bitop = new_rd_And(dbgi, block, new_shift, new_const, mode);
4995 } else if (op_left == op_Or) {
4996 new_bitop = new_rd_Or(dbgi, block, new_shift, new_const, mode);
4998 assert(op_left == op_Eor);
4999 new_bitop = new_rd_Eor(dbgi, block, new_shift, new_const, mode);
5007 * (x << c1) >> c2 <=> x OP (c2-c1) & ((-1 << c1) >> c2)
5009 * (x >> c1) << c2 <=> x OP (c2-c1) & ((-1 >> c1) << c2)
5010 * (also with x >>s c1 when c1>=c2)
5012 static ir_node *transform_node_shl_shr(ir_node *n)
5015 ir_node *right = get_binop_right(n);
5025 ir_tarval *tv_shift;
5031 assert(is_Shl(n) || is_Shr(n) || is_Shrs(n));
5033 if (!is_Const(right))
5036 left = get_binop_left(n);
5037 mode = get_irn_mode(n);
5038 if (is_Shl(n) && (is_Shr(left) || is_Shrs(left))) {
5039 ir_node *shr_right = get_binop_right(left);
5041 if (!is_Const(shr_right))
5044 x = get_binop_left(left);
5045 tv_shr = get_Const_tarval(shr_right);
5046 tv_shl = get_Const_tarval(right);
5048 if (is_Shrs(left)) {
5049 /* shrs variant only allowed if c1 >= c2 */
5050 if (! (tarval_cmp(tv_shl, tv_shr) & pn_Cmp_Ge))
5053 tv_mask = tarval_shrs(get_mode_all_one(mode), tv_shr);
5056 tv_mask = tarval_shr(get_mode_all_one(mode), tv_shr);
5058 tv_mask = tarval_shl(tv_mask, tv_shl);
5059 } else if (is_Shr(n) && is_Shl(left)) {
5060 ir_node *shl_right = get_Shl_right(left);
5062 if (!is_Const(shl_right))
5065 x = get_Shl_left(left);
5066 tv_shr = get_Const_tarval(right);
5067 tv_shl = get_Const_tarval(shl_right);
5069 tv_mask = tarval_shl(get_mode_all_one(mode), tv_shl);
5070 tv_mask = tarval_shr(tv_mask, tv_shr);
5075 if (get_tarval_mode(tv_shl) != get_tarval_mode(tv_shr)) {
5076 tv_shl = tarval_convert_to(tv_shl, get_tarval_mode(tv_shr));
5079 assert(tv_mask != tarval_bad);
5080 assert(get_tarval_mode(tv_mask) == mode);
5082 block = get_nodes_block(n);
5083 irg = get_irn_irg(block);
5084 dbgi = get_irn_dbg_info(n);
5086 pnc = tarval_cmp(tv_shl, tv_shr);
5087 if (pnc == pn_Cmp_Lt || pnc == pn_Cmp_Eq) {
5088 tv_shift = tarval_sub(tv_shr, tv_shl, NULL);
5089 new_const = new_r_Const(irg, tv_shift);
5091 new_shift = new_rd_Shrs(dbgi, block, x, new_const, mode);
5093 new_shift = new_rd_Shr(dbgi, block, x, new_const, mode);
5096 assert(pnc == pn_Cmp_Gt);
5097 tv_shift = tarval_sub(tv_shl, tv_shr, NULL);
5098 new_const = new_r_Const(irg, tv_shift);
5099 new_shift = new_rd_Shl(dbgi, block, x, new_const, mode);
5102 new_const = new_r_Const(irg, tv_mask);
5103 new_and = new_rd_And(dbgi, block, new_shift, new_const, mode);
5108 static ir_tarval *get_modulo_tv_value(ir_tarval *tv, int modulo_val)
5110 ir_mode *mode = get_tarval_mode(tv);
5111 ir_tarval *modulo_tv = new_tarval_from_long(modulo_val, mode);
5112 return tarval_mod(tv, modulo_tv);
5115 typedef ir_node*(*new_shift_func)(dbg_info *dbgi, ir_node *block,
5116 ir_node *left, ir_node *right, ir_mode *mode);
5119 * Normalisation: if we have a shl/shr with modulo_shift behaviour
5120 * then we can use that to minimize the value of Add(x, const) or
5121 * Sub(Const, x). In particular this often avoids 1 instruction in some
5122 * backends for the Shift(x, Sub(Const, y)) case because it can be replaced
5123 * by Shift(x, Minus(y)) which doesnt't need an explicit Const constructed.
5125 static ir_node *transform_node_shift_modulo(ir_node *n,
5126 new_shift_func new_shift)
5128 ir_mode *mode = get_irn_mode(n);
5129 int modulo = get_mode_modulo_shift(mode);
5130 ir_node *newop = NULL;
5131 ir_mode *mode_right;
5138 if (get_mode_arithmetic(mode) != irma_twos_complement)
5140 if (!is_po2(modulo))
5143 irg = get_irn_irg(n);
5144 block = get_nodes_block(n);
5145 right = get_binop_right(n);
5146 mode_right = get_irn_mode(right);
5147 if (is_Const(right)) {
5148 ir_tarval *tv = get_Const_tarval(right);
5149 ir_tarval *tv_mod = get_modulo_tv_value(tv, modulo);
5154 newop = new_r_Const(irg, tv_mod);
5155 } else if (is_Add(right)) {
5156 ir_node *add_right = get_Add_right(right);
5157 if (is_Const(add_right)) {
5158 ir_tarval *tv = get_Const_tarval(add_right);
5159 ir_tarval *tv_mod = get_modulo_tv_value(tv, modulo);
5164 newconst = new_r_Const(irg, tv_mod);
5165 newop = new_r_Add(block, get_Add_left(right), newconst,
5168 } else if (is_Sub(right)) {
5169 ir_node *sub_left = get_Sub_left(right);
5170 if (is_Const(sub_left)) {
5171 ir_tarval *tv = get_Const_tarval(sub_left);
5172 ir_tarval *tv_mod = get_modulo_tv_value(tv, modulo);
5177 newconst = new_r_Const(irg, tv_mod);
5178 newop = new_r_Sub(block, newconst, get_Sub_right(right),
5185 if (newop != NULL) {
5186 dbg_info *dbgi = get_irn_dbg_info(n);
5187 ir_node *left = get_binop_left(n);
5188 return new_shift(dbgi, block, left, newop, mode);
5196 static ir_node *transform_node_Shr(ir_node *n)
5198 ir_node *c, *oldn = n;
5199 ir_node *left = get_Shr_left(n);
5200 ir_node *right = get_Shr_right(n);
5201 ir_mode *mode = get_irn_mode(n);
5203 HANDLE_BINOP_PHI((eval_func) tarval_shr, left, right, c, mode);
5204 n = transform_node_shift(n);
5207 n = transform_node_shift_modulo(n, new_rd_Shr);
5209 n = transform_node_shl_shr(n);
5211 n = transform_node_bitop_shift(n);
5214 } /* transform_node_Shr */
5219 static ir_node *transform_node_Shrs(ir_node *n)
5221 ir_node *c, *oldn = n;
5222 ir_node *a = get_Shrs_left(n);
5223 ir_node *b = get_Shrs_right(n);
5224 ir_mode *mode = get_irn_mode(n);
5226 HANDLE_BINOP_PHI((eval_func) tarval_shrs, a, b, c, mode);
5227 n = transform_node_shift(n);
5230 n = transform_node_shift_modulo(n, new_rd_Shrs);
5232 n = transform_node_bitop_shift(n);
5235 } /* transform_node_Shrs */
5240 static ir_node *transform_node_Shl(ir_node *n)
5242 ir_node *c, *oldn = n;
5243 ir_node *a = get_Shl_left(n);
5244 ir_node *b = get_Shl_right(n);
5245 ir_mode *mode = get_irn_mode(n);
5247 HANDLE_BINOP_PHI((eval_func) tarval_shl, a, b, c, mode);
5248 n = transform_node_shift(n);
5251 n = transform_node_shift_modulo(n, new_rd_Shl);
5253 n = transform_node_shl_shr(n);
5255 n = transform_node_bitop_shift(n);
5258 } /* transform_node_Shl */
5263 static ir_node *transform_node_Rotl(ir_node *n)
5265 ir_node *c, *oldn = n;
5266 ir_node *a = get_Rotl_left(n);
5267 ir_node *b = get_Rotl_right(n);
5268 ir_mode *mode = get_irn_mode(n);
5270 HANDLE_BINOP_PHI((eval_func) tarval_rotl, a, b, c, mode);
5271 n = transform_node_shift(n);
5274 n = transform_node_bitop_shift(n);
5277 } /* transform_node_Rotl */
5282 static ir_node *transform_node_Conv(ir_node *n)
5284 ir_node *c, *oldn = n;
5285 ir_mode *mode = get_irn_mode(n);
5286 ir_node *a = get_Conv_op(n);
5288 if (mode != mode_b && is_const_Phi(a)) {
5289 /* Do NOT optimize mode_b Conv's, this leads to remaining
5290 * Phib nodes later, because the conv_b_lower operation
5291 * is instantly reverted, when it tries to insert a Convb.
5293 c = apply_conv_on_phi(a, mode);
5295 DBG_OPT_ALGSIM0(oldn, c, FS_OPT_CONST_PHI);
5300 if (is_Unknown(a)) { /* Conv_A(Unknown_B) -> Unknown_A */
5301 ir_graph *irg = get_irn_irg(n);
5302 return new_r_Unknown(irg, mode);
5305 if (mode_is_reference(mode) &&
5306 get_mode_size_bits(mode) == get_mode_size_bits(get_irn_mode(a)) &&
5308 ir_node *l = get_Add_left(a);
5309 ir_node *r = get_Add_right(a);
5310 dbg_info *dbgi = get_irn_dbg_info(a);
5311 ir_node *block = get_nodes_block(n);
5313 ir_node *lop = get_Conv_op(l);
5314 if (get_irn_mode(lop) == mode) {
5315 /* ConvP(AddI(ConvI(P), x)) -> AddP(P, x) */
5316 n = new_rd_Add(dbgi, block, lop, r, mode);
5321 ir_node *rop = get_Conv_op(r);
5322 if (get_irn_mode(rop) == mode) {
5323 /* ConvP(AddI(x, ConvI(P))) -> AddP(x, P) */
5324 n = new_rd_Add(dbgi, block, l, rop, mode);
5331 } /* transform_node_Conv */
5334 * Remove dead blocks and nodes in dead blocks
5335 * in keep alive list. We do not generate a new End node.
5337 static ir_node *transform_node_End(ir_node *n)
5339 int i, j, n_keepalives = get_End_n_keepalives(n);
5342 NEW_ARR_A(ir_node *, in, n_keepalives);
5344 for (i = j = 0; i < n_keepalives; ++i) {
5345 ir_node *ka = get_End_keepalive(n, i);
5347 if (! is_Block_dead(ka)) {
5351 } else if (is_irn_pinned_in_irg(ka) && is_Block_dead(get_nodes_block(ka))) {
5353 } else if (is_Bad(ka)) {
5354 /* no need to keep Bad */
5359 if (j != n_keepalives)
5360 set_End_keepalives(n, j, in);
5362 } /* transform_node_End */
5364 bool is_negated_value(ir_node *a, ir_node *b)
5366 if (is_Minus(a) && get_Minus_op(a) == b)
5368 if (is_Minus(b) && get_Minus_op(b) == a)
5370 if (is_Sub(a) && is_Sub(b)) {
5371 ir_node *a_left = get_Sub_left(a);
5372 ir_node *a_right = get_Sub_right(a);
5373 ir_node *b_left = get_Sub_left(b);
5374 ir_node *b_right = get_Sub_right(b);
5376 if (a_left == b_right && a_right == b_left)
5384 * Optimize a Mux into some simpler cases.
5386 static ir_node *transform_node_Mux(ir_node *n)
5388 ir_node *oldn = n, *sel = get_Mux_sel(n);
5389 ir_mode *mode = get_irn_mode(n);
5390 ir_node *t = get_Mux_true(n);
5391 ir_node *f = get_Mux_false(n);
5392 ir_graph *irg = get_irn_irg(n);
5394 if (is_irg_state(irg, IR_GRAPH_STATE_KEEP_MUX))
5398 ir_node* block = get_nodes_block(n);
5400 ir_node* c1 = get_Mux_sel(t);
5401 ir_node* t1 = get_Mux_true(t);
5402 ir_node* f1 = get_Mux_false(t);
5404 /* Mux(cond0, Mux(cond1, x, y), y) -> typical if (cond0 && cond1) x else y */
5405 ir_node* and_ = new_r_And(block, c0, c1, mode_b);
5406 ir_node* new_mux = new_r_Mux(block, and_, f1, t1, mode);
5411 DBG_OPT_ALGSIM0(oldn, t, FS_OPT_MUX_COMBINE);
5412 } else if (f == t1) {
5413 /* Mux(cond0, Mux(cond1, x, y), x) */
5414 ir_node* not_c1 = new_r_Not(block, c1, mode_b);
5415 ir_node* and_ = new_r_And(block, c0, not_c1, mode_b);
5416 ir_node* new_mux = new_r_Mux(block, and_, t1, f1, mode);
5421 DBG_OPT_ALGSIM0(oldn, t, FS_OPT_MUX_COMBINE);
5423 } else if (is_Mux(f)) {
5424 ir_node* block = get_nodes_block(n);
5426 ir_node* c1 = get_Mux_sel(f);
5427 ir_node* t1 = get_Mux_true(f);
5428 ir_node* f1 = get_Mux_false(f);
5430 /* Mux(cond0, x, Mux(cond1, x, y)) -> typical if (cond0 || cond1) x else y */
5431 ir_node* or_ = new_r_Or(block, c0, c1, mode_b);
5432 ir_node* new_mux = new_r_Mux(block, or_, f1, t1, mode);
5437 DBG_OPT_ALGSIM0(oldn, f, FS_OPT_MUX_COMBINE);
5438 } else if (t == f1) {
5439 /* Mux(cond0, x, Mux(cond1, y, x)) */
5440 ir_node* not_c1 = new_r_Not(block, c1, mode_b);
5441 ir_node* or_ = new_r_Or(block, c0, not_c1, mode_b);
5442 ir_node* new_mux = new_r_Mux(block, or_, t1, f1, mode);
5447 DBG_OPT_ALGSIM0(oldn, f, FS_OPT_MUX_COMBINE);
5451 /* first normalization step: try to move a constant to the false side,
5452 * 0 preferred on false side too */
5454 ir_node *cmp = get_Proj_pred(sel);
5456 if (is_Cmp(cmp) && is_Const(t) &&
5457 (!is_Const(f) || (is_Const_null(t) && !is_Const_null(f)))) {
5458 pn_Cmp pnc = get_Proj_pn_cmp(sel);
5463 /* Mux(x, a, b) => Mux(not(x), b, a) */
5464 sel = new_r_Proj(cmp, mode_b,
5465 get_negated_pnc(pnc, get_irn_mode(get_Cmp_left(cmp))));
5466 n = new_rd_Mux(get_irn_dbg_info(n), get_nodes_block(n), sel, f, t, mode);
5470 /* note: after normalization, false can only happen on default */
5471 if (mode == mode_b) {
5472 dbg_info *dbg = get_irn_dbg_info(n);
5473 ir_node *block = get_nodes_block(n);
5476 ir_tarval *tv_t = get_Const_tarval(t);
5477 if (tv_t == tarval_b_true) {
5479 /* Muxb(sel, true, false) = sel */
5480 assert(get_Const_tarval(f) == tarval_b_false);
5481 DBG_OPT_ALGSIM0(oldn, sel, FS_OPT_MUX_BOOL);
5484 /* Muxb(sel, true, x) = Or(sel, x) */
5485 n = new_rd_Or(dbg, block, sel, f, mode_b);
5486 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_OR_BOOL);
5490 } else if (is_Const(f)) {
5491 ir_tarval *tv_f = get_Const_tarval(f);
5492 if (tv_f == tarval_b_true) {
5493 /* Muxb(sel, x, true) = Or(Not(sel), x) */
5494 ir_node* not_sel = new_rd_Not(dbg, block, sel, mode_b);
5495 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_ORNOT_BOOL);
5496 n = new_rd_Or(dbg, block, not_sel, t, mode_b);
5499 /* Muxb(sel, x, false) = And(sel, x) */
5500 assert(tv_f == tarval_b_false);
5501 n = new_rd_And(dbg, block, sel, t, mode_b);
5502 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_AND_BOOL);
5508 /* more normalization: Mux(sel, 0, 1) is simply a conv from the mode_b
5509 * value to integer. */
5510 if (is_Const(t) && is_Const(f) && mode_is_int(mode)) {
5511 ir_tarval *a = get_Const_tarval(t);
5512 ir_tarval *b = get_Const_tarval(f);
5514 if (tarval_is_one(a) && tarval_is_null(b)) {
5515 ir_node *block = get_nodes_block(n);
5516 ir_node *conv = new_r_Conv(block, sel, mode);
5518 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_CONV);
5520 } else if (tarval_is_null(a) && tarval_is_one(b)) {
5521 ir_node *block = get_nodes_block(n);
5522 ir_node *not_ = new_r_Not(block, sel, mode_b);
5523 ir_node *conv = new_r_Conv(block, not_, mode);
5525 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_CONV);
5531 ir_node *cmp = get_Proj_pred(sel);
5532 long pn = get_Proj_proj(sel);
5535 * Note: normalization puts the constant on the right side,
5536 * so we check only one case.
5539 ir_node *cmp_r = get_Cmp_right(cmp);
5540 if (is_Const(cmp_r) && is_Const_null(cmp_r)) {
5541 ir_node *block = get_nodes_block(n);
5542 ir_node *cmp_l = get_Cmp_left(cmp);
5544 if (mode_is_int(mode)) {
5546 if ((pn == pn_Cmp_Lg || pn == pn_Cmp_Eq) && is_And(cmp_l)) {
5547 /* Mux((a & b) != 0, c, 0) */
5548 ir_node *and_r = get_And_right(cmp_l);
5551 if (and_r == t && f == cmp_r) {
5552 if (is_Const(t) && tarval_is_single_bit(get_Const_tarval(t))) {
5553 if (pn == pn_Cmp_Lg) {
5554 /* Mux((a & 2^C) != 0, 2^C, 0) */
5556 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_BITOP);
5558 /* Mux((a & 2^C) == 0, 2^C, 0) */
5559 n = new_rd_Eor(get_irn_dbg_info(n),
5560 block, cmp_l, t, mode);
5561 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_BITOP);
5566 if (is_Shl(and_r)) {
5567 ir_node *shl_l = get_Shl_left(and_r);
5568 if (is_Const(shl_l) && is_Const_one(shl_l)) {
5569 if (and_r == t && f == cmp_r) {
5570 if (pn == pn_Cmp_Lg) {
5571 /* (a & (1 << n)) != 0, (1 << n), 0) */
5573 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_BITOP);
5575 /* (a & (1 << n)) == 0, (1 << n), 0) */
5576 n = new_rd_Eor(get_irn_dbg_info(n),
5577 block, cmp_l, t, mode);
5578 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_BITOP);
5584 and_l = get_And_left(cmp_l);
5585 if (is_Shl(and_l)) {
5586 ir_node *shl_l = get_Shl_left(and_l);
5587 if (is_Const(shl_l) && is_Const_one(shl_l)) {
5588 if (and_l == t && f == cmp_r) {
5589 if (pn == pn_Cmp_Lg) {
5590 /* ((1 << n) & a) != 0, (1 << n), 0) */
5592 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_BITOP);
5594 /* ((1 << n) & a) == 0, (1 << n), 0) */
5595 n = new_rd_Eor(get_irn_dbg_info(n),
5596 block, cmp_l, t, mode);
5597 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_BITOP);
5610 } /* transform_node_Mux */
5613 * optimize Sync nodes that have other syncs as input we simply add the inputs
5614 * of the other sync to our own inputs
5616 static ir_node *transform_node_Sync(ir_node *n)
5618 int arity = get_Sync_n_preds(n);
5621 for (i = 0; i < arity;) {
5622 ir_node *pred = get_Sync_pred(n, i);
5626 if (!is_Sync(pred)) {
5634 pred_arity = get_Sync_n_preds(pred);
5635 for (j = 0; j < pred_arity; ++j) {
5636 ir_node *pred_pred = get_Sync_pred(pred, j);
5641 add_irn_n(n, pred_pred);
5645 if (get_Sync_pred(n, k) == pred_pred) break;
5650 /* rehash the sync node */
5654 } /* transform_node_Sync */
5657 * optimize a trampoline Call into a direct Call
5659 static ir_node *transform_node_Call(ir_node *call)
5661 ir_node *callee = get_Call_ptr(call);
5662 ir_node *adr, *mem, *res, *bl, **in;
5663 ir_type *ctp, *mtp, *tp;
5667 int i, n_res, n_param;
5670 if (! is_Proj(callee))
5672 callee = get_Proj_pred(callee);
5673 if (! is_Builtin(callee))
5675 if (get_Builtin_kind(callee) != ir_bk_inner_trampoline)
5678 mem = get_Call_mem(call);
5680 if (skip_Proj(mem) == callee) {
5681 /* memory is routed to the trampoline, skip */
5682 mem = get_Builtin_mem(callee);
5685 /* build a new call type */
5686 mtp = get_Call_type(call);
5687 tdb = get_type_dbg_info(mtp);
5689 n_res = get_method_n_ress(mtp);
5690 n_param = get_method_n_params(mtp);
5691 ctp = new_d_type_method(n_param + 1, n_res, tdb);
5693 for (i = 0; i < n_res; ++i)
5694 set_method_res_type(ctp, i, get_method_res_type(mtp, i));
5696 NEW_ARR_A(ir_node *, in, n_param + 1);
5698 /* FIXME: we don't need a new pointer type in every step */
5699 irg = get_irn_irg(call);
5700 tp = get_irg_frame_type(irg);
5701 tp = new_type_pointer(tp);
5702 set_method_param_type(ctp, 0, tp);
5704 in[0] = get_Builtin_param(callee, 2);
5705 for (i = 0; i < n_param; ++i) {
5706 set_method_param_type(ctp, i + 1, get_method_param_type(mtp, i));
5707 in[i + 1] = get_Call_param(call, i);
5709 var = get_method_variadicity(mtp);
5710 set_method_variadicity(ctp, var);
5711 if (var == variadicity_variadic) {
5712 set_method_first_variadic_param_index(ctp, get_method_first_variadic_param_index(mtp) + 1);
5714 /* When we resolve a trampoline, the function must be called by a this-call */
5715 set_method_calling_convention(ctp, get_method_calling_convention(mtp) | cc_this_call);
5716 set_method_additional_properties(ctp, get_method_additional_properties(mtp));
5718 adr = get_Builtin_param(callee, 1);
5720 db = get_irn_dbg_info(call);
5721 bl = get_nodes_block(call);
5723 res = new_rd_Call(db, bl, mem, adr, n_param + 1, in, ctp);
5724 if (get_irn_pinned(call) == op_pin_state_floats)
5725 set_irn_pinned(res, op_pin_state_floats);
5727 } /* transform_node_Call */
5730 * Tries several [inplace] [optimizing] transformations and returns an
5731 * equivalent node. The difference to equivalent_node() is that these
5732 * transformations _do_ generate new nodes, and thus the old node must
5733 * not be freed even if the equivalent node isn't the old one.
5735 static ir_node *transform_node(ir_node *n)
5740 * Transform_node is the only "optimizing transformation" that might
5741 * return a node with a different opcode. We iterate HERE until fixpoint
5742 * to get the final result.
5746 if (n->op->ops.transform_node != NULL)
5747 n = n->op->ops.transform_node(n);
5748 } while (oldn != n);
5751 } /* transform_node */
5754 * Sets the default transform node operation for an ir_op_ops.
5756 * @param code the opcode for the default operation
5757 * @param ops the operations initialized
5762 static ir_op_ops *firm_set_default_transform_node(ir_opcode code, ir_op_ops *ops)
5766 ops->transform_node = transform_node_##a; \
5768 #define CASE_PROJ(a) \
5770 ops->transform_node_Proj = transform_node_Proj_##a; \
5772 #define CASE_PROJ_EX(a) \
5774 ops->transform_node = transform_node_##a; \
5775 ops->transform_node_Proj = transform_node_Proj_##a; \
5815 } /* firm_set_default_transform_node */
5818 /* **************** Common Subexpression Elimination **************** */
5820 /** The size of the hash table used, should estimate the number of nodes
5822 #define N_IR_NODES 512
5824 /** Compares the attributes of two Const nodes. */
5825 static int node_cmp_attr_Const(ir_node *a, ir_node *b)
5827 return get_Const_tarval(a) != get_Const_tarval(b);
5830 /** Compares the attributes of two Proj nodes. */
5831 static int node_cmp_attr_Proj(ir_node *a, ir_node *b)
5833 return a->attr.proj.proj != b->attr.proj.proj;
5834 } /* node_cmp_attr_Proj */
5836 /** Compares the attributes of two Alloc nodes. */
5837 static int node_cmp_attr_Alloc(ir_node *a, ir_node *b)
5839 const alloc_attr *pa = &a->attr.alloc;
5840 const alloc_attr *pb = &b->attr.alloc;
5841 return (pa->where != pb->where) || (pa->type != pb->type);
5842 } /* node_cmp_attr_Alloc */
5844 /** Compares the attributes of two Free nodes. */
5845 static int node_cmp_attr_Free(ir_node *a, ir_node *b)
5847 const free_attr *pa = &a->attr.free;
5848 const free_attr *pb = &b->attr.free;
5849 return (pa->where != pb->where) || (pa->type != pb->type);
5850 } /* node_cmp_attr_Free */
5852 /** Compares the attributes of two SymConst nodes. */
5853 static int node_cmp_attr_SymConst(ir_node *a, ir_node *b)
5855 const symconst_attr *pa = &a->attr.symc;
5856 const symconst_attr *pb = &b->attr.symc;
5857 return (pa->kind != pb->kind)
5858 || (pa->sym.type_p != pb->sym.type_p);
5861 /** Compares the attributes of two Call nodes. */
5862 static int node_cmp_attr_Call(ir_node *a, ir_node *b)
5864 const call_attr *pa = &a->attr.call;
5865 const call_attr *pb = &b->attr.call;
5866 return (pa->type != pb->type)
5867 || (pa->tail_call != pb->tail_call);
5868 } /* node_cmp_attr_Call */
5870 /** Compares the attributes of two Sel nodes. */
5871 static int node_cmp_attr_Sel(ir_node *a, ir_node *b)
5873 const ir_entity *a_ent = get_Sel_entity(a);
5874 const ir_entity *b_ent = get_Sel_entity(b);
5875 return a_ent != b_ent;
5876 } /* node_cmp_attr_Sel */
5878 /** Compares the attributes of two Phi nodes. */
5879 static int node_cmp_attr_Phi(ir_node *a, ir_node *b)
5881 /* we can only enter this function if both nodes have the same number of inputs,
5882 hence it is enough to check if one of them is a Phi0 */
5884 /* check the Phi0 pos attribute */
5885 return a->attr.phi.u.pos != b->attr.phi.u.pos;
5888 } /* node_cmp_attr_Phi */
5890 /** Compares the attributes of two Conv nodes. */
5891 static int node_cmp_attr_Conv(ir_node *a, ir_node *b)
5893 return get_Conv_strict(a) != get_Conv_strict(b);
5894 } /* node_cmp_attr_Conv */
5896 /** Compares the attributes of two Cast nodes. */
5897 static int node_cmp_attr_Cast(ir_node *a, ir_node *b)
5899 return get_Cast_type(a) != get_Cast_type(b);
5900 } /* node_cmp_attr_Cast */
5902 /** Compares the attributes of two Load nodes. */
5903 static int node_cmp_attr_Load(ir_node *a, ir_node *b)
5905 if (get_Load_volatility(a) == volatility_is_volatile ||
5906 get_Load_volatility(b) == volatility_is_volatile)
5907 /* NEVER do CSE on volatile Loads */
5909 /* do not CSE Loads with different alignment. Be conservative. */
5910 if (get_Load_align(a) != get_Load_align(b))
5913 return get_Load_mode(a) != get_Load_mode(b);
5914 } /* node_cmp_attr_Load */
5916 /** Compares the attributes of two Store nodes. */
5917 static int node_cmp_attr_Store(ir_node *a, ir_node *b)
5919 /* do not CSE Stores with different alignment. Be conservative. */
5920 if (get_Store_align(a) != get_Store_align(b))
5923 /* NEVER do CSE on volatile Stores */
5924 return (get_Store_volatility(a) == volatility_is_volatile ||
5925 get_Store_volatility(b) == volatility_is_volatile);
5926 } /* node_cmp_attr_Store */
5928 /** Compares two exception attributes */
5929 static int node_cmp_exception(ir_node *a, ir_node *b)
5931 const except_attr *ea = &a->attr.except;
5932 const except_attr *eb = &b->attr.except;
5934 return ea->pin_state != eb->pin_state;
5937 #define node_cmp_attr_Bound node_cmp_exception
5939 /** Compares the attributes of two Div nodes. */
5940 static int node_cmp_attr_Div(ir_node *a, ir_node *b)
5942 const divmod_attr *ma = &a->attr.divmod;
5943 const divmod_attr *mb = &b->attr.divmod;
5944 return ma->exc.pin_state != mb->exc.pin_state ||
5945 ma->resmode != mb->resmode ||
5946 ma->no_remainder != mb->no_remainder;
5947 } /* node_cmp_attr_Div */
5949 /** Compares the attributes of two Div or Mod nodes. */
5950 static int node_cmp_attr_Div_Mod(ir_node *a, ir_node *b)
5952 const divmod_attr *ma = &a->attr.divmod;
5953 const divmod_attr *mb = &b->attr.divmod;
5954 return ma->exc.pin_state != mb->exc.pin_state ||
5955 ma->resmode != mb->resmode;
5956 } /* node_cmp_attr_Div_Mod */
5958 /** Compares the attributes of two Mod nodes. */
5959 static int node_cmp_attr_Mod(ir_node *a, ir_node *b)
5961 return node_cmp_attr_Div_Mod(a, b);
5962 } /* node_cmp_attr_Mod */
5964 /** Compares the attributes of two Confirm nodes. */
5965 static int node_cmp_attr_Confirm(ir_node *a, ir_node *b)
5967 /* no need to compare the bound, as this is a input */
5968 return (get_Confirm_cmp(a) != get_Confirm_cmp(b));
5969 } /* node_cmp_attr_Confirm */
5971 /** Compares the attributes of two Builtin nodes. */
5972 static int node_cmp_attr_Builtin(ir_node *a, ir_node *b)
5974 /* no need to compare the type, equal kind means equal type */
5975 return get_Builtin_kind(a) != get_Builtin_kind(b);
5976 } /* node_cmp_attr_Builtin */
5978 /** Compares the attributes of two ASM nodes. */
5979 static int node_cmp_attr_ASM(ir_node *a, ir_node *b)
5982 const ir_asm_constraint *ca;
5983 const ir_asm_constraint *cb;
5986 if (get_ASM_text(a) != get_ASM_text(b))
5989 /* Should we really check the constraints here? Should be better, but is strange. */
5990 n = get_ASM_n_input_constraints(a);
5991 if (n != get_ASM_n_input_constraints(b))
5994 ca = get_ASM_input_constraints(a);
5995 cb = get_ASM_input_constraints(b);
5996 for (i = 0; i < n; ++i) {
5997 if (ca[i].pos != cb[i].pos || ca[i].constraint != cb[i].constraint)
6001 n = get_ASM_n_output_constraints(a);
6002 if (n != get_ASM_n_output_constraints(b))
6005 ca = get_ASM_output_constraints(a);
6006 cb = get_ASM_output_constraints(b);
6007 for (i = 0; i < n; ++i) {
6008 if (ca[i].pos != cb[i].pos || ca[i].constraint != cb[i].constraint)
6012 n = get_ASM_n_clobbers(a);
6013 if (n != get_ASM_n_clobbers(b))
6016 cla = get_ASM_clobbers(a);
6017 clb = get_ASM_clobbers(b);
6018 for (i = 0; i < n; ++i) {
6019 if (cla[i] != clb[i])
6023 } /* node_cmp_attr_ASM */
6025 /** Compares the inexistent attributes of two Dummy nodes. */
6026 static int node_cmp_attr_Dummy(ir_node *a, ir_node *b)
6034 * Set the default node attribute compare operation for an ir_op_ops.
6036 * @param code the opcode for the default operation
6037 * @param ops the operations initialized
6042 static ir_op_ops *firm_set_default_node_cmp_attr(ir_opcode code, ir_op_ops *ops)
6046 ops->node_cmp_attr = node_cmp_attr_##a; \
6077 } /* firm_set_default_node_cmp_attr */
6080 * Compare function for two nodes in the value table. Gets two
6081 * nodes as parameters. Returns 0 if the nodes are a Common Sub Expression.
6083 int identities_cmp(const void *elt, const void *key)
6085 ir_node *a = (ir_node *)elt;
6086 ir_node *b = (ir_node *)key;
6089 if (a == b) return 0;
6091 if ((get_irn_op(a) != get_irn_op(b)) ||
6092 (get_irn_mode(a) != get_irn_mode(b))) return 1;
6094 /* compare if a's in and b's in are of equal length */
6095 irn_arity_a = get_irn_arity(a);
6096 if (irn_arity_a != get_irn_arity(b))
6099 /* blocks are never the same */
6103 if (get_irn_pinned(a) == op_pin_state_pinned) {
6104 /* for pinned nodes, the block inputs must be equal */
6105 if (get_irn_n(a, -1) != get_irn_n(b, -1))
6107 } else if (! get_opt_global_cse()) {
6108 /* for block-local CSE both nodes must be in the same Block */
6109 if (get_nodes_block(a) != get_nodes_block(b))
6113 /* compare a->in[0..ins] with b->in[0..ins] */
6114 for (i = 0; i < irn_arity_a; ++i) {
6115 ir_node *pred_a = get_irn_n(a, i);
6116 ir_node *pred_b = get_irn_n(b, i);
6117 if (pred_a != pred_b) {
6118 /* if both predecessors are CSE neutral they might be different */
6119 if (!is_irn_cse_neutral(pred_a) || !is_irn_cse_neutral(pred_b))
6125 * here, we already now that the nodes are identical except their
6128 if (a->op->ops.node_cmp_attr)
6129 return a->op->ops.node_cmp_attr(a, b);
6132 } /* identities_cmp */
6135 * Calculate a hash value of a node.
6137 * @param node The IR-node
6139 unsigned ir_node_hash(const ir_node *node)
6141 return node->op->ops.hash(node);
6142 } /* ir_node_hash */
6145 void new_identities(ir_graph *irg)
6147 if (irg->value_table != NULL)
6148 del_pset(irg->value_table);
6149 irg->value_table = new_pset(identities_cmp, N_IR_NODES);
6150 } /* new_identities */
6152 void del_identities(ir_graph *irg)
6154 if (irg->value_table != NULL)
6155 del_pset(irg->value_table);
6156 } /* del_identities */
6158 /* Normalize a node by putting constants (and operands with larger
6159 * node index) on the right (operator side). */
6160 void ir_normalize_node(ir_node *n)
6162 if (is_op_commutative(get_irn_op(n))) {
6163 ir_node *l = get_binop_left(n);
6164 ir_node *r = get_binop_right(n);
6166 /* For commutative operators perform a OP b == b OP a but keep
6167 * constants on the RIGHT side. This helps greatly in some
6168 * optimizations. Moreover we use the idx number to make the form
6170 if (!operands_are_normalized(l, r)) {
6171 set_binop_left(n, r);
6172 set_binop_right(n, l);
6176 } /* ir_normalize_node */
6179 * Return the canonical node computing the same value as n.
6180 * Looks up the node in a hash table, enters it in the table
6181 * if it isn't there yet.
6183 * @param n the node to look up
6185 * @return a node that computes the same value as n or n if no such
6186 * node could be found
6188 ir_node *identify_remember(ir_node *n)
6190 ir_graph *irg = get_irn_irg(n);
6191 pset *value_table = irg->value_table;
6194 if (value_table == NULL)
6197 ir_normalize_node(n);
6198 /* lookup or insert in hash table with given hash key. */
6199 nn = (ir_node*)pset_insert(value_table, n, ir_node_hash(n));
6202 /* n is reachable again */
6203 edges_node_revival(nn, get_irn_irg(nn));
6207 } /* identify_remember */
6210 * During construction we set the op_pin_state_pinned flag in the graph right
6211 * when the optimization is performed. The flag turning on procedure global
6212 * cse could be changed between two allocations. This way we are safe.
6214 * @param n The node to lookup
6216 static inline ir_node *identify_cons(ir_node *n)
6220 n = identify_remember(n);
6221 if (n != old && get_nodes_block(old) != get_nodes_block(n)) {
6222 ir_graph *irg = get_irn_irg(n);
6223 set_irg_pinned(irg, op_pin_state_floats);
6226 } /* identify_cons */
6228 /* Add a node to the identities value table. */
6229 void add_identities(ir_node *node)
6236 identify_remember(node);
6239 /* Visit each node in the value table of a graph. */
6240 void visit_all_identities(ir_graph *irg, irg_walk_func visit, void *env)
6243 ir_graph *rem = current_ir_graph;
6245 current_ir_graph = irg;
6246 foreach_pset(irg->value_table, ir_node*, node) {
6249 current_ir_graph = rem;
6250 } /* visit_all_identities */
6253 * Garbage in, garbage out. If a node has a dead input, i.e., the
6254 * Bad node is input to the node, return the Bad node.
6256 static ir_node *gigo(ir_node *node)
6259 ir_op *op = get_irn_op(node);
6261 /* remove garbage blocks by looking at control flow that leaves the block
6262 and replacing the control flow by Bad. */
6263 if (get_irn_mode(node) == mode_X) {
6264 ir_node *block = get_nodes_block(skip_Proj(node));
6265 ir_graph *irg = get_irn_irg(block);
6267 /* Don't optimize nodes in immature blocks. */
6268 if (!get_Block_matured(block))
6270 /* Don't optimize End, may have Bads. */
6271 if (op == op_End) return node;
6273 if (is_Block(block)) {
6274 if (is_Block_dead(block)) {
6275 /* control flow from dead block is dead */
6276 return get_irg_bad(irg);
6279 for (i = get_irn_arity(block) - 1; i >= 0; --i) {
6280 if (!is_Bad(get_irn_n(block, i)))
6284 ir_graph *irg = get_irn_irg(block);
6285 /* the start block is never dead */
6286 if (block != get_irg_start_block(irg)
6287 && block != get_irg_end_block(irg)) {
6289 * Do NOT kill control flow without setting
6290 * the block to dead of bad things can happen:
6291 * We get a Block that is not reachable be irg_block_walk()
6292 * but can be found by irg_walk()!
6294 set_Block_dead(block);
6295 return get_irg_bad(irg);
6301 /* Blocks, Phis and Tuples may have dead inputs, e.g., if one of the
6302 blocks predecessors is dead. */
6303 if (op != op_Block && op != op_Phi && op != op_Tuple && op != op_Anchor) {
6304 ir_graph *irg = get_irn_irg(node);
6305 irn_arity = get_irn_arity(node);
6308 * Beware: we can only read the block of a non-floating node.
6310 if (is_irn_pinned_in_irg(node) &&
6311 is_Block_dead(get_nodes_block(skip_Proj(node))))
6312 return get_irg_bad(irg);
6314 for (i = 0; i < irn_arity; i++) {
6315 ir_node *pred = get_irn_n(node, i);
6318 return get_irg_bad(irg);
6320 /* Propagating Unknowns here seems to be a bad idea, because
6321 sometimes we need a node as a input and did not want that
6323 However, it might be useful to move this into a later phase
6324 (if you think that optimizing such code is useful). */
6325 if (is_Unknown(pred) && mode_is_data(get_irn_mode(node)))
6326 return new_r_Unknown(irg, get_irn_mode(node));
6331 /* With this code we violate the agreement that local_optimize
6332 only leaves Bads in Block, Phi and Tuple nodes. */
6333 /* If Block has only Bads as predecessors it's garbage. */
6334 /* If Phi has only Bads as predecessors it's garbage. */
6335 if ((op == op_Block && get_Block_matured(node)) || op == op_Phi) {
6336 irn_arity = get_irn_arity(node);
6337 for (i = 0; i < irn_arity; i++) {
6338 if (!is_Bad(get_irn_n(node, i))) break;
6340 if (i == irn_arity) node = get_irg_bad(irg);
6347 * These optimizations deallocate nodes from the obstack.
6348 * It can only be called if it is guaranteed that no other nodes
6349 * reference this one, i.e., right after construction of a node.
6351 * @param n The node to optimize
6353 ir_node *optimize_node(ir_node *n)
6356 ir_graph *irg = get_irn_irg(n);
6357 unsigned iro = get_irn_opcode(n);
6360 /* Always optimize Phi nodes: part of the construction. */
6361 if ((!get_opt_optimize()) && (iro != iro_Phi)) return n;
6363 /* constant expression evaluation / constant folding */
6364 if (get_opt_constant_folding()) {
6365 /* neither constants nor Tuple values can be evaluated */
6366 if (iro != iro_Const && (get_irn_mode(n) != mode_T)) {
6367 /* try to evaluate */
6368 tv = computed_value(n);
6369 if (tv != tarval_bad) {
6374 * we MUST copy the node here temporary, because it's still
6375 * needed for DBG_OPT_CSTEVAL
6377 node_size = offsetof(ir_node, attr) + n->op->attr_size;
6378 oldn = (ir_node*)alloca(node_size);
6380 memcpy(oldn, n, node_size);
6381 CLONE_ARR_A(ir_node *, oldn->in, n->in);
6383 /* ARG, copy the in array, we need it for statistics */
6384 memcpy(oldn->in, n->in, ARR_LEN(n->in) * sizeof(n->in[0]));
6386 /* note the inplace edges module */
6387 edges_node_deleted(n, irg);
6389 /* evaluation was successful -- replace the node. */
6390 irg_kill_node(irg, n);
6391 nw = new_r_Const(irg, tv);
6393 DBG_OPT_CSTEVAL(oldn, nw);
6399 /* remove unnecessary nodes */
6400 if (get_opt_algebraic_simplification() ||
6401 (iro == iro_Phi) || /* always optimize these nodes. */
6403 (iro == iro_Proj) ||
6404 (iro == iro_Block) ) /* Flags tested local. */
6405 n = equivalent_node(n);
6407 /* Common Subexpression Elimination.
6409 * Checks whether n is already available.
6410 * The block input is used to distinguish different subexpressions. Right
6411 * now all nodes are op_pin_state_pinned to blocks, i.e., the CSE only finds common
6412 * subexpressions within a block.
6415 n = identify_cons(n);
6418 edges_node_deleted(oldn, irg);
6420 /* We found an existing, better node, so we can deallocate the old node. */
6421 irg_kill_node(irg, oldn);
6425 /* Some more constant expression evaluation that does not allow to
6427 iro = get_irn_opcode(n);
6428 if (get_opt_algebraic_simplification() ||
6429 (iro == iro_Cond) ||
6430 (iro == iro_Proj)) /* Flags tested local. */
6431 n = transform_node(n);
6433 /* Remove nodes with dead (Bad) input.
6434 Run always for transformation induced Bads. */
6437 /* Now we have a legal, useful node. Enter it in hash table for CSE */
6438 if (get_opt_cse() && (get_irn_opcode(n) != iro_Block)) {
6440 n = identify_remember(o);
6446 } /* optimize_node */
6450 * These optimizations never deallocate nodes (in place). This can cause dead
6451 * nodes lying on the obstack. Remove these by a dead node elimination,
6452 * i.e., a copying garbage collection.
6454 ir_node *optimize_in_place_2(ir_node *n)
6458 unsigned iro = get_irn_opcode(n);
6460 if (!get_opt_optimize() && !is_Phi(n)) return n;
6462 if (iro == iro_Deleted)
6465 /* constant expression evaluation / constant folding */
6466 if (get_opt_constant_folding()) {
6467 /* neither constants nor Tuple values can be evaluated */
6468 if (iro != iro_Const && get_irn_mode(n) != mode_T) {
6469 /* try to evaluate */
6470 tv = computed_value(n);
6471 if (tv != tarval_bad) {
6472 /* evaluation was successful -- replace the node. */
6473 ir_graph *irg = get_irn_irg(n);
6475 n = new_r_Const(irg, tv);
6477 DBG_OPT_CSTEVAL(oldn, n);
6483 /* remove unnecessary nodes */
6484 if (get_opt_constant_folding() ||
6485 (iro == iro_Phi) || /* always optimize these nodes. */
6486 (iro == iro_Id) || /* ... */
6487 (iro == iro_Proj) || /* ... */
6488 (iro == iro_Block) ) /* Flags tested local. */
6489 n = equivalent_node(n);
6491 /** common subexpression elimination **/
6492 /* Checks whether n is already available. */
6493 /* The block input is used to distinguish different subexpressions. Right
6494 now all nodes are op_pin_state_pinned to blocks, i.e., the cse only finds common
6495 subexpressions within a block. */
6496 if (get_opt_cse()) {
6498 n = identify_remember(o);
6503 /* Some more constant expression evaluation. */
6504 iro = get_irn_opcode(n);
6505 if (get_opt_constant_folding() ||
6506 (iro == iro_Cond) ||
6507 (iro == iro_Proj)) /* Flags tested local. */
6508 n = transform_node(n);
6510 /* Remove nodes with dead (Bad) input.
6511 Run always for transformation induced Bads. */
6514 /* Now we can verify the node, as it has no dead inputs any more. */
6517 /* Now we have a legal, useful node. Enter it in hash table for cse.
6518 Blocks should be unique anyways. (Except the successor of start:
6519 is cse with the start block!) */
6520 if (get_opt_cse() && (get_irn_opcode(n) != iro_Block)) {
6522 n = identify_remember(o);
6528 } /* optimize_in_place_2 */
6531 * Wrapper for external use, set proper status bits after optimization.
6533 ir_node *optimize_in_place(ir_node *n)
6535 ir_graph *irg = get_irn_irg(n);
6536 /* Handle graph state */
6537 assert(get_irg_phase_state(irg) != phase_building);
6539 if (get_opt_global_cse())
6540 set_irg_pinned(irg, op_pin_state_floats);
6541 if (get_irg_outs_state(irg) == outs_consistent)
6542 set_irg_outs_inconsistent(irg);
6544 /* FIXME: Maybe we could also test whether optimizing the node can
6545 change the control graph. */
6546 set_irg_doms_inconsistent(irg);
6547 return optimize_in_place_2(n);
6548 } /* optimize_in_place */
6551 * Calculate a hash value of a Const node.
6553 static unsigned hash_Const(const ir_node *node)
6557 /* special value for const, as they only differ in their tarval. */
6558 h = HASH_PTR(node->attr.con.tarval);
6564 * Calculate a hash value of a SymConst node.
6566 static unsigned hash_SymConst(const ir_node *node)
6570 /* all others are pointers */
6571 h = HASH_PTR(node->attr.symc.sym.type_p);
6574 } /* hash_SymConst */
6577 * Set the default hash operation in an ir_op_ops.
6579 * @param code the opcode for the default operation
6580 * @param ops the operations initialized
6585 static ir_op_ops *firm_set_default_hash(ir_opcode code, ir_op_ops *ops)
6589 ops->hash = hash_##a; \
6592 /* hash function already set */
6593 if (ops->hash != NULL)
6600 /* use input/mode default hash if no function was given */
6601 ops->hash = firm_default_hash;
6609 * Sets the default operation for an ir_ops.
6611 ir_op_ops *firm_set_default_operations(ir_opcode code, ir_op_ops *ops)
6613 ops = firm_set_default_hash(code, ops);
6614 ops = firm_set_default_computed_value(code, ops);
6615 ops = firm_set_default_equivalent_node(code, ops);
6616 ops = firm_set_default_transform_node(code, ops);
6617 ops = firm_set_default_node_cmp_attr(code, ops);
6618 ops = firm_set_default_get_type_attr(code, ops);
6619 ops = firm_set_default_get_entity_attr(code, ops);
6622 } /* firm_set_default_operations */