2 * Copyright (C) 1995-2010 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 * Return the value of a floating point Quot.
549 static ir_tarval *do_computed_value_Quot(const ir_node *a, const ir_node *b)
551 ir_tarval *ta = value_of(a);
552 ir_tarval *tb = value_of(b);
554 /* cannot optimize 0 / b = 0 because of NaN */
555 if (ta != tarval_bad && tb != tarval_bad)
556 return tarval_quo(ta, tb);
558 } /* do_computed_value_Quot */
561 * Calculate the value of an integer Div of two nodes.
562 * Special case: 0 / b
564 static ir_tarval *do_computed_value_Div(const ir_node *a, const ir_node *b)
566 ir_tarval *ta = value_of(a);
568 const ir_node *dummy;
570 /* Compute c1 / c2 or 0 / a, a != 0 */
571 if (tarval_is_null(ta) && value_not_zero(b, &dummy))
572 return ta; /* 0 / b == 0 */
574 if (ta != tarval_bad && tb != tarval_bad)
575 return tarval_div(ta, tb);
577 } /* do_computed_value_Div */
580 * Calculate the value of an integer Mod of two nodes.
581 * Special case: a % 1
583 static ir_tarval *do_computed_value_Mod(const ir_node *a, const ir_node *b)
585 ir_tarval *ta = value_of(a);
586 ir_tarval *tb = value_of(b);
588 /* Compute a % 1 or c1 % c2 */
589 if (tarval_is_one(tb))
590 return get_mode_null(get_irn_mode(a));
591 if (ta != tarval_bad && tb != tarval_bad)
592 return tarval_mod(ta, tb);
594 } /* do_computed_value_Mod */
597 * Return the value of a Proj(DivMod).
599 static ir_tarval *computed_value_Proj_DivMod(const ir_node *n)
601 long proj_nr = get_Proj_proj(n);
603 /* compute either the Div or the Mod part */
604 if (proj_nr == pn_DivMod_res_div) {
605 const ir_node *a = get_Proj_pred(n);
606 return do_computed_value_Div(get_DivMod_left(a), get_DivMod_right(a));
607 } else if (proj_nr == pn_DivMod_res_mod) {
608 const ir_node *a = get_Proj_pred(n);
609 return do_computed_value_Mod(get_DivMod_left(a), get_DivMod_right(a));
612 } /* computed_value_Proj_DivMod */
615 * Return the value of a Proj(Div).
617 static ir_tarval *computed_value_Proj_Div(const ir_node *n)
619 long proj_nr = get_Proj_proj(n);
621 if (proj_nr == pn_Div_res) {
622 const ir_node *a = get_Proj_pred(n);
623 return do_computed_value_Div(get_Div_left(a), get_Div_right(a));
626 } /* computed_value_Proj_Div */
629 * Return the value of a Proj(Mod).
631 static ir_tarval *computed_value_Proj_Mod(const ir_node *n)
633 long proj_nr = get_Proj_proj(n);
635 if (proj_nr == pn_Mod_res) {
636 const ir_node *a = get_Proj_pred(n);
637 return do_computed_value_Mod(get_Mod_left(a), get_Mod_right(a));
640 } /* computed_value_Proj_Mod */
643 * Return the value of a Proj(Quot).
645 static ir_tarval *computed_value_Proj_Quot(const ir_node *n)
647 long proj_nr = get_Proj_proj(n);
649 if (proj_nr == pn_Quot_res) {
650 const ir_node *a = get_Proj_pred(n);
651 return do_computed_value_Quot(get_Quot_left(a), get_Quot_right(a));
654 } /* computed_value_Proj_Quot */
657 * Return the value of a Proj.
659 static ir_tarval *computed_value_Proj(const ir_node *proj)
661 ir_node *n = get_Proj_pred(proj);
663 if (n->op->ops.computed_value_Proj != NULL)
664 return n->op->ops.computed_value_Proj(proj);
666 } /* computed_value_Proj */
669 * If the parameter n can be computed, return its value, else tarval_bad.
670 * Performs constant folding.
672 * @param n The node this should be evaluated
674 ir_tarval *computed_value(const ir_node *n)
676 vrp_attr *vrp = vrp_get_info(n);
677 if (vrp && vrp->valid && tarval_cmp(vrp->bits_set, vrp->bits_not_set) == pn_Cmp_Eq) {
678 return vrp->bits_set;
680 if (n->op->ops.computed_value)
681 return n->op->ops.computed_value(n);
683 } /* computed_value */
686 * Set the default computed_value evaluator in an ir_op_ops.
688 * @param code the opcode for the default operation
689 * @param ops the operations initialized
694 static ir_op_ops *firm_set_default_computed_value(ir_opcode code, ir_op_ops *ops)
698 ops->computed_value = computed_value_##a; \
700 #define CASE_PROJ(a) \
702 ops->computed_value_Proj = computed_value_Proj_##a; \
739 } /* firm_set_default_computed_value */
742 * Returns a equivalent block for another block.
743 * If the block has only one predecessor, this is
744 * the equivalent one. If the only predecessor of a block is
745 * the block itself, this is a dead block.
747 * If both predecessors of a block are the branches of a binary
748 * Cond, the equivalent block is Cond's block.
750 * If all predecessors of a block are bad or lies in a dead
751 * block, the current block is dead as well.
753 * Note, that blocks are NEVER turned into Bad's, instead
754 * the dead_block flag is set. So, never test for is_Bad(block),
755 * always use is_dead_Block(block).
757 static ir_node *equivalent_node_Block(ir_node *n)
763 /* don't optimize dead or labeled blocks */
764 if (is_Block_dead(n) || has_Block_entity(n))
767 n_preds = get_Block_n_cfgpreds(n);
769 /* The Block constructor does not call optimize, but mature_immBlock()
770 calls the optimization. */
771 assert(get_Block_matured(n));
773 irg = get_irn_irg(n);
775 /* Straightening: a single entry Block following a single exit Block
776 can be merged, if it is not the Start block. */
777 /* !!! Beware, all Phi-nodes of n must have been optimized away.
778 This should be true, as the block is matured before optimize is called.
779 But what about Phi-cycles with the Phi0/Id that could not be resolved?
780 Remaining Phi nodes are just Ids. */
782 ir_node *pred = skip_Proj(get_Block_cfgpred(n, 0));
785 ir_node *predblock = get_nodes_block(pred);
786 if (predblock == oldn) {
787 /* Jmp jumps into the block it is in -- deal self cycle. */
788 n = set_Block_dead(n);
789 DBG_OPT_DEAD_BLOCK(oldn, n);
792 DBG_OPT_STG(oldn, n);
794 } else if (is_Cond(pred)) {
795 ir_node *predblock = get_nodes_block(pred);
796 if (predblock == oldn) {
797 /* Jmp jumps into the block it is in -- deal self cycle. */
798 n = set_Block_dead(n);
799 DBG_OPT_DEAD_BLOCK(oldn, n);
802 } else if (n_preds == 2) {
803 /* Test whether Cond jumps twice to this block
804 * The more general case which more than 2 predecessors is handles
805 * in optimize_cf(), we handle only this special case for speed here.
807 ir_node *a = get_Block_cfgpred(n, 0);
808 ir_node *b = get_Block_cfgpred(n, 1);
810 if (is_Proj(a) && is_Proj(b)) {
811 ir_node *cond = get_Proj_pred(a);
813 if (cond == get_Proj_pred(b) && is_Cond(cond) &&
814 get_irn_mode(get_Cond_selector(cond)) == mode_b) {
815 /* Also a single entry Block following a single exit Block. Phis have
816 twice the same operand and will be optimized away. */
817 n = get_nodes_block(cond);
818 DBG_OPT_IFSIM1(oldn, a, b, n);
821 } else if (get_opt_unreachable_code() &&
822 (n != get_irg_start_block(irg)) &&
823 (n != get_irg_end_block(irg))) {
826 /* If all inputs are dead, this block is dead too, except if it is
827 the start or end block. This is one step of unreachable code
829 for (i = get_Block_n_cfgpreds(n) - 1; i >= 0; --i) {
830 ir_node *pred = get_Block_cfgpred(n, i);
833 if (is_Bad(pred)) continue;
834 pred_blk = get_nodes_block(skip_Proj(pred));
836 if (is_Block_dead(pred_blk)) continue;
839 /* really found a living input */
844 n = set_Block_dead(n);
845 DBG_OPT_DEAD_BLOCK(oldn, n);
850 } /* equivalent_node_Block */
853 * Returns a equivalent node for a Jmp, a Bad :-)
854 * Of course this only happens if the Block of the Jmp is dead.
856 static ir_node *equivalent_node_Jmp(ir_node *n)
860 /* unreachable code elimination */
861 if (is_Block_dead(get_nodes_block(n))) {
862 ir_graph *irg = get_irn_irg(n);
863 n = get_irg_bad(irg);
864 DBG_OPT_DEAD_BLOCK(oldn, n);
867 } /* equivalent_node_Jmp */
869 /** Raise is handled in the same way as Jmp. */
870 #define equivalent_node_Raise equivalent_node_Jmp
873 /* We do not evaluate Cond here as we replace it by a new node, a Jmp.
874 See transform_node_Proj_Cond(). */
877 * Optimize operations that are commutative and have neutral 0,
878 * so a op 0 = 0 op a = a.
880 static ir_node *equivalent_node_neutral_zero(ir_node *n)
884 ir_node *a = get_binop_left(n);
885 ir_node *b = get_binop_right(n);
890 /* After running compute_node there is only one constant predecessor.
891 Find this predecessors value and remember the other node: */
892 if ((tv = value_of(a)) != tarval_bad) {
894 } else if ((tv = value_of(b)) != tarval_bad) {
899 /* If this predecessors constant value is zero, the operation is
900 * unnecessary. Remove it.
902 * Beware: If n is a Add, the mode of on and n might be different
903 * which happens in this rare construction: NULL + 3.
904 * Then, a Conv would be needed which we cannot include here.
906 if (tarval_is_null(tv) && get_irn_mode(on) == get_irn_mode(n)) {
909 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_0);
913 } /* equivalent_node_neutral_zero */
916 * Eor is commutative and has neutral 0.
918 static ir_node *equivalent_node_Eor(ir_node *n)
924 n = equivalent_node_neutral_zero(n);
925 if (n != oldn) return n;
928 b = get_Eor_right(n);
931 ir_node *aa = get_Eor_left(a);
932 ir_node *ab = get_Eor_right(a);
935 /* (a ^ b) ^ a -> b */
937 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_EOR_A_B_A);
939 } else if (ab == b) {
940 /* (a ^ b) ^ b -> a */
942 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_EOR_A_B_A);
947 ir_node *ba = get_Eor_left(b);
948 ir_node *bb = get_Eor_right(b);
951 /* a ^ (a ^ b) -> b */
953 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_EOR_A_B_A);
955 } else if (bb == a) {
956 /* a ^ (b ^ a) -> b */
958 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_EOR_A_B_A);
966 * Optimize a - 0 and (a - x) + x (for modes with wrap-around).
968 * The second one looks strange, but this construct
969 * is used heavily in the LCC sources :-).
971 * Beware: The Mode of an Add may be different than the mode of its
972 * predecessors, so we could not return a predecessors in all cases.
974 static ir_node *equivalent_node_Add(ir_node *n)
977 ir_node *left, *right;
978 ir_mode *mode = get_irn_mode(n);
980 n = equivalent_node_neutral_zero(n);
984 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
985 if (mode_is_float(mode)) {
986 ir_graph *irg = get_irn_irg(n);
987 if (get_irg_fp_model(irg) & fp_strict_algebraic)
991 left = get_Add_left(n);
992 right = get_Add_right(n);
995 if (get_Sub_right(left) == right) {
998 n = get_Sub_left(left);
999 if (mode == get_irn_mode(n)) {
1000 DBG_OPT_ALGSIM1(oldn, left, right, n, FS_OPT_ADD_SUB);
1005 if (is_Sub(right)) {
1006 if (get_Sub_right(right) == left) {
1009 n = get_Sub_left(right);
1010 if (mode == get_irn_mode(n)) {
1011 DBG_OPT_ALGSIM1(oldn, left, right, n, FS_OPT_ADD_SUB);
1017 } /* equivalent_node_Add */
1020 * optimize operations that are not commutative but have neutral 0 on left,
1023 static ir_node *equivalent_node_left_zero(ir_node *n)
1027 ir_node *a = get_binop_left(n);
1028 ir_node *b = get_binop_right(n);
1029 ir_tarval *tb = value_of(b);
1031 if (tarval_is_null(tb)) {
1034 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_0);
1037 } /* equivalent_node_left_zero */
1039 #define equivalent_node_Shl equivalent_node_left_zero
1040 #define equivalent_node_Shr equivalent_node_left_zero
1041 #define equivalent_node_Shrs equivalent_node_left_zero
1042 #define equivalent_node_Rotl equivalent_node_left_zero
1045 * Optimize a - 0 and (a + x) - x (for modes with wrap-around).
1047 * The second one looks strange, but this construct
1048 * is used heavily in the LCC sources :-).
1050 * Beware: The Mode of a Sub may be different than the mode of its
1051 * predecessors, so we could not return a predecessors in all cases.
1053 static ir_node *equivalent_node_Sub(ir_node *n)
1057 ir_mode *mode = get_irn_mode(n);
1060 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
1061 if (mode_is_float(mode)) {
1062 ir_graph *irg = get_irn_irg(n);
1063 if (get_irg_fp_model(irg) & fp_strict_algebraic)
1067 b = get_Sub_right(n);
1070 /* Beware: modes might be different */
1071 if (tarval_is_null(tb)) {
1072 ir_node *a = get_Sub_left(n);
1073 if (mode == get_irn_mode(a)) {
1076 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_0);
1080 } /* equivalent_node_Sub */
1084 * Optimize an "self-inverse unary op", ie op(op(n)) = n.
1087 * -(-a) == a, but might overflow two times.
1088 * We handle it anyway here but the better way would be a
1089 * flag. This would be needed for Pascal for instance.
1091 static ir_node *equivalent_node_idempotent_unop(ir_node *n)
1094 ir_node *pred = get_unop_op(n);
1096 /* optimize symmetric unop */
1097 if (get_irn_op(pred) == get_irn_op(n)) {
1098 n = get_unop_op(pred);
1099 DBG_OPT_ALGSIM2(oldn, pred, n, FS_OPT_IDEM_UNARY);
1102 } /* equivalent_node_idempotent_unop */
1104 /** Optimize Not(Not(x)) == x. */
1105 #define equivalent_node_Not equivalent_node_idempotent_unop
1107 /** -(-x) == x ??? Is this possible or can --x raise an
1108 out of bounds exception if min =! max? */
1109 #define equivalent_node_Minus equivalent_node_idempotent_unop
1112 * Optimize a * 1 = 1 * a = a.
1114 static ir_node *equivalent_node_Mul(ir_node *n)
1117 ir_node *a = get_Mul_left(n);
1119 /* we can handle here only the n * n = n bit cases */
1120 if (get_irn_mode(n) == get_irn_mode(a)) {
1121 ir_node *b = get_Mul_right(n);
1125 * Mul is commutative and has again an other neutral element.
1126 * Constants are place right, so check this case first.
1129 if (tarval_is_one(tv)) {
1131 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
1134 if (tarval_is_one(tv)) {
1136 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
1141 } /* equivalent_node_Mul */
1144 * Use algebraic simplification a | a = a | 0 = 0 | a = a.
1146 static ir_node *equivalent_node_Or(ir_node *n)
1150 ir_node *a = get_Or_left(n);
1151 ir_node *b = get_Or_right(n);
1155 n = a; /* Or has it's own neutral element */
1156 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_OR);
1159 /* constants are cormalized to right, check this site first */
1161 if (tarval_is_null(tv)) {
1163 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_OR);
1167 if (tarval_is_null(tv)) {
1169 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_OR);
1174 } /* equivalent_node_Or */
1177 * Optimize a & 0b1...1 = 0b1...1 & a = a & a = (a|X) & a = a.
1179 static ir_node *equivalent_node_And(ir_node *n)
1183 ir_node *a = get_And_left(n);
1184 ir_node *b = get_And_right(n);
1188 n = a; /* And has it's own neutral element */
1189 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_AND);
1192 /* constants are normalized to right, check this site first */
1194 if (tarval_is_all_one(tv)) {
1196 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1199 if (tv != get_tarval_bad()) {
1200 ir_mode *mode = get_irn_mode(n);
1201 if (!mode_is_signed(mode) && is_Conv(a)) {
1202 ir_node *convop = get_Conv_op(a);
1203 ir_mode *convopmode = get_irn_mode(convop);
1204 if (!mode_is_signed(convopmode)) {
1205 if (tarval_is_all_one(tarval_convert_to(tv, convopmode))) {
1206 /* Conv(X) & all_one(mode(X)) = Conv(X) */
1208 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1215 if (tarval_is_all_one(tv)) {
1217 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1221 if (b == get_Or_left(a) || b == get_Or_right(a)) {
1224 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1229 if (a == get_Or_left(b) || a == get_Or_right(b)) {
1232 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1237 } /* equivalent_node_And */
1240 * Try to remove useless Conv's:
1242 static ir_node *equivalent_node_Conv(ir_node *n)
1245 ir_node *a = get_Conv_op(n);
1247 ir_mode *n_mode = get_irn_mode(n);
1248 ir_mode *a_mode = get_irn_mode(a);
1251 if (n_mode == a_mode) { /* No Conv necessary */
1252 if (get_Conv_strict(n)) {
1255 /* neither Minus nor Confirm change the precision,
1256 so we can "look-through" */
1259 p = get_Minus_op(p);
1260 } else if (is_Confirm(p)) {
1261 p = get_Confirm_value(p);
1267 if (is_Conv(p) && get_Conv_strict(p)) {
1268 /* we known already, that a_mode == n_mode, and neither
1269 Minus change the mode, so the second Conv
1271 assert(get_irn_mode(p) == n_mode);
1273 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CONV);
1277 ir_node *pred = get_Proj_pred(p);
1278 if (is_Load(pred)) {
1279 /* Loads always return with the exact precision of n_mode */
1280 assert(get_Load_mode(pred) == n_mode);
1282 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CONV);
1285 if (is_Proj(pred) && get_Proj_proj(pred) == pn_Start_T_args) {
1286 pred = get_Proj_pred(pred);
1287 if (is_Start(pred)) {
1288 /* Arguments always return with the exact precision,
1289 as strictConv's are place before Call -- if the
1290 caller was compiled with the same setting.
1291 Otherwise, the semantics is probably still right. */
1292 assert(get_irn_mode(p) == n_mode);
1294 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CONV);
1300 /* special case: the immediate predecessor is also a Conv */
1301 if (! get_Conv_strict(a)) {
1302 /* first one is not strict, kick it */
1304 a_mode = get_irn_mode(a);
1308 /* else both are strict conv, second is superfluous */
1310 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CONV);
1315 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CONV);
1318 } else if (is_Conv(a)) { /* Conv(Conv(b)) */
1319 ir_node *b = get_Conv_op(a);
1320 ir_mode *b_mode = get_irn_mode(b);
1322 if (get_Conv_strict(n) && get_Conv_strict(a)) {
1323 /* both are strict conv */
1324 if (smaller_mode(a_mode, n_mode)) {
1325 /* both are strict, but the first is smaller, so
1326 the second cannot remove more precision, remove the
1328 set_Conv_strict(n, 0);
1331 if (n_mode == b_mode) {
1332 if (! get_Conv_strict(n) && ! get_Conv_strict(a)) {
1333 if (n_mode == mode_b) {
1334 n = b; /* Convb(Conv*(xxxb(...))) == xxxb(...) */
1335 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
1337 } else if (get_mode_arithmetic(n_mode) == get_mode_arithmetic(a_mode)) {
1338 if (values_in_mode(b_mode, a_mode)) {
1339 n = b; /* ConvS(ConvL(xxxS(...))) == xxxS(...) */
1340 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
1345 if (mode_is_int(n_mode) && get_mode_arithmetic(a_mode) == irma_ieee754) {
1346 /* ConvI(ConvF(I)) -> I, iff float mantissa >= int mode */
1347 unsigned int_mantissa = get_mode_size_bits(n_mode) - (mode_is_signed(n_mode) ? 1 : 0);
1348 unsigned float_mantissa = tarval_ieee754_get_mantissa_size(a_mode);
1350 if (float_mantissa >= int_mantissa) {
1352 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
1357 if (smaller_mode(b_mode, a_mode)) {
1358 if (get_Conv_strict(n))
1359 set_Conv_strict(b, 1);
1360 n = b; /* ConvA(ConvB(ConvA(...))) == ConvA(...) */
1361 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
1368 } /* equivalent_node_Conv */
1371 * - fold Phi-nodes, iff they have only one predecessor except
1374 static ir_node *equivalent_node_Phi(ir_node *n)
1380 ir_node *first_val = NULL; /* to shutup gcc */
1382 if (!get_opt_optimize() &&
1383 get_irg_phase_state(get_irn_irg(n)) != phase_building)
1386 n_preds = get_Phi_n_preds(n);
1388 block = get_nodes_block(n);
1390 if (is_Block_dead(block)) {
1391 ir_graph *irg = get_irn_irg(n);
1392 return get_irg_bad(irg);
1395 if (n_preds == 0) return n; /* Phi of dead Region without predecessors. */
1397 /* Find first non-self-referencing input */
1398 for (i = 0; i < n_preds; ++i) {
1399 first_val = get_Phi_pred(n, i);
1400 if ( (first_val != n) /* not self pointer */
1402 /* BEWARE: when the if is changed to 1, Phi's will ignore it's Bad
1403 * predecessors. Then, Phi nodes in dead code might be removed, causing
1404 * nodes pointing to themself (Add's for instance).
1405 * This is really bad and causes endless recursions in several
1406 * code pathes, so we do NOT optimize such a code.
1407 * This is not that bad as it sounds, optimize_cf() removes bad control flow
1408 * (and bad Phi predecessors), so live code is optimized later.
1410 && (! is_Bad(get_Block_cfgpred(block, i)))
1412 ) { /* value not dead */
1413 break; /* then found first value. */
1418 ir_graph *irg = get_irn_irg(n);
1419 /* A totally Bad or self-referencing Phi (we didn't break the above loop) */
1420 return get_irg_bad(irg);
1423 /* search for rest of inputs, determine if any of these
1424 are non-self-referencing */
1425 while (++i < n_preds) {
1426 ir_node *scnd_val = get_Phi_pred(n, i);
1427 if ( (scnd_val != n)
1428 && (scnd_val != first_val)
1431 && (! is_Bad(get_Block_cfgpred(block, i)))
1438 if (i >= n_preds && !is_Dummy(first_val)) {
1439 /* Fold, if no multiple distinct non-self-referencing inputs */
1441 DBG_OPT_PHI(oldn, n);
1444 } /* equivalent_node_Phi */
1447 * Several optimizations:
1448 * - fold Sync-nodes, iff they have only one predecessor except
1451 static ir_node *equivalent_node_Sync(ir_node *n)
1453 int arity = get_Sync_n_preds(n);
1456 for (i = 0; i < arity;) {
1457 ir_node *pred = get_Sync_pred(n, i);
1460 /* Remove Bad predecessors */
1467 /* Remove duplicate predecessors */
1473 if (get_Sync_pred(n, j) == pred) {
1482 ir_graph *irg = get_irn_irg(n);
1483 return get_irg_bad(irg);
1485 if (arity == 1) return get_Sync_pred(n, 0);
1487 } /* equivalent_node_Sync */
1490 * Optimize Proj(Tuple).
1492 static ir_node *equivalent_node_Proj_Tuple(ir_node *proj)
1494 ir_node *oldn = proj;
1495 ir_node *tuple = get_Proj_pred(proj);
1497 /* Remove the Tuple/Proj combination. */
1498 proj = get_Tuple_pred(tuple, get_Proj_proj(proj));
1499 DBG_OPT_TUPLE(oldn, tuple, proj);
1502 } /* equivalent_node_Proj_Tuple */
1505 * Optimize a / 1 = a.
1507 static ir_node *equivalent_node_Proj_Div(ir_node *proj)
1509 ir_node *oldn = proj;
1510 ir_node *div = get_Proj_pred(proj);
1511 ir_node *b = get_Div_right(div);
1512 ir_tarval *tb = value_of(b);
1514 /* Div is not commutative. */
1515 if (tarval_is_one(tb)) { /* div(x, 1) == x */
1516 switch (get_Proj_proj(proj)) {
1518 proj = get_Div_mem(div);
1519 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NEUTRAL_1);
1523 proj = get_Div_left(div);
1524 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NEUTRAL_1);
1528 /* we cannot replace the exception Proj's here, this is done in
1529 transform_node_Proj_Div() */
1534 } /* equivalent_node_Proj_Div */
1537 * Optimize a / 1.0 = a.
1539 static ir_node *equivalent_node_Proj_Quot(ir_node *proj)
1541 ir_node *oldn = proj;
1542 ir_node *quot = get_Proj_pred(proj);
1543 ir_node *b = get_Quot_right(quot);
1544 ir_tarval *tb = value_of(b);
1546 /* Div is not commutative. */
1547 if (tarval_is_one(tb)) { /* Quot(x, 1) == x */
1548 switch (get_Proj_proj(proj)) {
1550 proj = get_Quot_mem(quot);
1551 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NEUTRAL_1);
1555 proj = get_Quot_left(quot);
1556 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NEUTRAL_1);
1560 /* we cannot replace the exception Proj's here, this is done in
1561 transform_node_Proj_Quot() */
1566 } /* equivalent_node_Proj_Quot */
1569 * Optimize a / 1 = a.
1571 static ir_node *equivalent_node_Proj_DivMod(ir_node *proj)
1573 ir_node *oldn = proj;
1574 ir_node *divmod = get_Proj_pred(proj);
1575 ir_node *b = get_DivMod_right(divmod);
1576 ir_tarval *tb = value_of(b);
1578 /* Div is not commutative. */
1579 if (tarval_is_one(tb)) { /* div(x, 1) == x */
1580 switch (get_Proj_proj(proj)) {
1582 proj = get_DivMod_mem(divmod);
1583 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NEUTRAL_1);
1586 case pn_DivMod_res_div:
1587 proj = get_DivMod_left(divmod);
1588 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NEUTRAL_1);
1592 /* we cannot replace the exception Proj's here, this is done in
1593 transform_node_Proj_DivMod().
1594 Note further that the pn_DivMod_res_div case is handled in
1595 computed_value_Proj(). */
1600 } /* equivalent_node_Proj_DivMod */
1603 * Optimize CopyB(mem, x, x) into a Nop.
1605 static ir_node *equivalent_node_Proj_CopyB(ir_node *proj)
1607 ir_node *oldn = proj;
1608 ir_node *copyb = get_Proj_pred(proj);
1609 ir_node *a = get_CopyB_dst(copyb);
1610 ir_node *b = get_CopyB_src(copyb);
1613 /* Turn CopyB into a tuple (mem, jmp, bad, bad) */
1614 switch (get_Proj_proj(proj)) {
1616 proj = get_CopyB_mem(copyb);
1617 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NOP);
1620 case pn_CopyB_X_except: {
1621 ir_graph *irg = get_irn_irg(proj);
1622 DBG_OPT_EXC_REM(proj);
1623 proj = get_irg_bad(irg);
1629 } /* equivalent_node_Proj_CopyB */
1632 * Optimize Bounds(idx, idx, upper) into idx.
1634 static ir_node *equivalent_node_Proj_Bound(ir_node *proj)
1636 ir_node *oldn = proj;
1637 ir_node *bound = get_Proj_pred(proj);
1638 ir_node *idx = get_Bound_index(bound);
1639 ir_node *pred = skip_Proj(idx);
1642 if (idx == get_Bound_lower(bound))
1644 else if (is_Bound(pred)) {
1646 * idx was Bounds checked previously, it is still valid if
1647 * lower <= pred_lower && pred_upper <= upper.
1649 ir_node *lower = get_Bound_lower(bound);
1650 ir_node *upper = get_Bound_upper(bound);
1651 if (get_Bound_lower(pred) == lower &&
1652 get_Bound_upper(pred) == upper) {
1654 * One could expect that we simply return the previous
1655 * Bound here. However, this would be wrong, as we could
1656 * add an exception Proj to a new location then.
1657 * So, we must turn in into a tuple.
1663 /* Turn Bound into a tuple (mem, jmp, bad, idx) */
1664 switch (get_Proj_proj(proj)) {
1666 DBG_OPT_EXC_REM(proj);
1667 proj = get_Bound_mem(bound);
1669 case pn_Bound_X_except: {
1670 ir_graph *irg = get_irn_irg(proj);
1671 DBG_OPT_EXC_REM(proj);
1672 proj = get_irg_bad(irg);
1677 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NOP);
1680 /* cannot optimize pn_Bound_X_regular, handled in transform ... */
1685 } /* equivalent_node_Proj_Bound */
1688 * Optimize an Exception Proj(Load) with a non-null address.
1690 static ir_node *equivalent_node_Proj_Load(ir_node *proj)
1692 if (get_opt_ldst_only_null_ptr_exceptions()) {
1693 if (get_irn_mode(proj) == mode_X) {
1694 ir_node *load = get_Proj_pred(proj);
1696 /* get the Load address */
1697 const ir_node *addr = get_Load_ptr(load);
1698 const ir_node *confirm;
1700 if (value_not_null(addr, &confirm)) {
1701 if (get_Proj_proj(proj) == pn_Load_X_except) {
1702 ir_graph *irg = get_irn_irg(proj);
1703 DBG_OPT_EXC_REM(proj);
1704 return get_irg_bad(irg);
1710 } /* equivalent_node_Proj_Load */
1713 * Optimize an Exception Proj(Store) with a non-null address.
1715 static ir_node *equivalent_node_Proj_Store(ir_node *proj)
1717 if (get_opt_ldst_only_null_ptr_exceptions()) {
1718 if (get_irn_mode(proj) == mode_X) {
1719 ir_node *store = get_Proj_pred(proj);
1721 /* get the load/store address */
1722 const ir_node *addr = get_Store_ptr(store);
1723 const ir_node *confirm;
1725 if (value_not_null(addr, &confirm)) {
1726 if (get_Proj_proj(proj) == pn_Store_X_except) {
1727 ir_graph *irg = get_irn_irg(proj);
1728 DBG_OPT_EXC_REM(proj);
1729 return get_irg_bad(irg);
1735 } /* equivalent_node_Proj_Store */
1738 * Does all optimizations on nodes that must be done on it's Proj's
1739 * because of creating new nodes.
1741 static ir_node *equivalent_node_Proj(ir_node *proj)
1743 ir_node *n = get_Proj_pred(proj);
1745 if (get_irn_mode(proj) == mode_X) {
1746 if (is_Block_dead(get_nodes_block(n))) {
1747 /* Remove dead control flow -- early gigo(). */
1748 ir_graph *irg = get_irn_irg(proj);
1749 return get_irg_bad(irg);
1752 if (n->op->ops.equivalent_node_Proj)
1753 return n->op->ops.equivalent_node_Proj(proj);
1755 } /* equivalent_node_Proj */
1760 static ir_node *equivalent_node_Id(ir_node *n)
1768 DBG_OPT_ID(oldn, n);
1770 } /* equivalent_node_Id */
1775 static ir_node *equivalent_node_Mux(ir_node *n)
1777 ir_node *oldn = n, *sel = get_Mux_sel(n);
1779 ir_tarval *ts = value_of(sel);
1781 /* Mux(true, f, t) == t */
1782 if (ts == tarval_b_true) {
1783 n = get_Mux_true(n);
1784 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_C);
1787 /* Mux(false, f, t) == f */
1788 if (ts == tarval_b_false) {
1789 n = get_Mux_false(n);
1790 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_C);
1793 n_t = get_Mux_true(n);
1794 n_f = get_Mux_false(n);
1796 /* Mux(v, x, T) == x */
1797 if (is_Unknown(n_f)) {
1799 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_EQ);
1802 /* Mux(v, T, x) == x */
1803 if (is_Unknown(n_t)) {
1805 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_EQ);
1809 /* Mux(v, x, x) == x */
1812 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_EQ);
1815 if (is_Proj(sel) && !mode_honor_signed_zeros(get_irn_mode(n))) {
1816 ir_node *cmp = get_Proj_pred(sel);
1817 long proj_nr = get_Proj_proj(sel);
1818 ir_node *f = get_Mux_false(n);
1819 ir_node *t = get_Mux_true(n);
1822 * Note further that these optimization work even for floating point
1823 * with NaN's because -NaN == NaN.
1824 * However, if +0 and -0 is handled differently, we cannot use the first one.
1827 ir_node *const cmp_l = get_Cmp_left(cmp);
1828 ir_node *const cmp_r = get_Cmp_right(cmp);
1832 if ((cmp_l == t && cmp_r == f) || /* Mux(t == f, t, f) -> f */
1833 (cmp_l == f && cmp_r == t)) { /* Mux(f == t, t, f) -> f */
1835 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1842 if ((cmp_l == t && cmp_r == f) || /* Mux(t != f, t, f) -> t */
1843 (cmp_l == f && cmp_r == t)) { /* Mux(f != t, t, f) -> t */
1845 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1852 * Note: normalization puts the constant on the right side,
1853 * so we check only one case.
1855 if (cmp_l == t && tarval_is_null(value_of(cmp_r))) {
1856 /* Mux(t CMP 0, X, t) */
1857 if (is_Minus(f) && get_Minus_op(f) == t) {
1858 /* Mux(t CMP 0, -t, t) */
1859 if (proj_nr == pn_Cmp_Eq) {
1860 /* Mux(t == 0, -t, t) ==> -t */
1862 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1863 } else if (proj_nr == pn_Cmp_Lg || proj_nr == pn_Cmp_Ne) {
1864 /* Mux(t != 0, -t, t) ==> t */
1866 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1873 } /* equivalent_node_Mux */
1876 * Remove Confirm nodes if setting is on.
1877 * Replace Confirms(x, '=', Constlike) by Constlike.
1879 static ir_node *equivalent_node_Confirm(ir_node *n)
1881 ir_node *pred = get_Confirm_value(n);
1882 pn_Cmp pnc = get_Confirm_cmp(n);
1884 while (is_Confirm(pred) && pnc == get_Confirm_cmp(pred)) {
1886 * rare case: two identical Confirms one after another,
1887 * replace the second one with the first.
1890 pred = get_Confirm_value(n);
1891 pnc = get_Confirm_cmp(n);
1897 * equivalent_node() returns a node equivalent to input n. It skips all nodes that
1898 * perform no actual computation, as, e.g., the Id nodes. It does not create
1899 * new nodes. It is therefore safe to free n if the node returned is not n.
1900 * If a node returns a Tuple we can not just skip it. If the size of the
1901 * in array fits, we transform n into a tuple (e.g., Div).
1903 ir_node *equivalent_node(ir_node *n)
1905 if (n->op->ops.equivalent_node)
1906 return n->op->ops.equivalent_node(n);
1908 } /* equivalent_node */
1911 * Sets the default equivalent node operation for an ir_op_ops.
1913 * @param code the opcode for the default operation
1914 * @param ops the operations initialized
1919 static ir_op_ops *firm_set_default_equivalent_node(ir_opcode code, ir_op_ops *ops)
1923 ops->equivalent_node = equivalent_node_##a; \
1925 #define CASE_PROJ(a) \
1927 ops->equivalent_node_Proj = equivalent_node_Proj_##a; \
1969 } /* firm_set_default_equivalent_node */
1972 * Returns non-zero if a node is a Phi node
1973 * with all predecessors constant.
1975 static int is_const_Phi(ir_node *n)
1979 if (! is_Phi(n) || get_irn_arity(n) == 0)
1981 for (i = get_irn_arity(n) - 1; i >= 0; --i) {
1982 if (! is_Const(get_irn_n(n, i)))
1986 } /* is_const_Phi */
1988 typedef ir_tarval *(*tarval_sub_type)(ir_tarval *a, ir_tarval *b, ir_mode *mode);
1989 typedef ir_tarval *(*tarval_binop_type)(ir_tarval *a, ir_tarval *b);
1992 * in reality eval_func should be tarval (*eval_func)() but incomplete
1993 * declarations are bad style and generate noisy warnings
1995 typedef void (*eval_func)(void);
1998 * Wrapper for the tarval binop evaluation, tarval_sub has one more parameter.
2000 static ir_tarval *do_eval(eval_func eval, ir_tarval *a, ir_tarval *b, ir_mode *mode)
2002 if (eval == (eval_func) tarval_sub) {
2003 tarval_sub_type func = (tarval_sub_type)eval;
2005 return func(a, b, mode);
2007 tarval_binop_type func = (tarval_binop_type)eval;
2014 * Apply an evaluator on a binop with a constant operators (and one Phi).
2016 * @param phi the Phi node
2017 * @param other the other operand
2018 * @param eval an evaluator function
2019 * @param mode the mode of the result, may be different from the mode of the Phi!
2020 * @param left if non-zero, other is the left operand, else the right
2022 * @return a new Phi node if the conversion was successful, NULL else
2024 static ir_node *apply_binop_on_phi(ir_node *phi, ir_tarval *other, eval_func eval, ir_mode *mode, int left)
2030 int i, n = get_irn_arity(phi);
2032 NEW_ARR_A(void *, res, n);
2034 for (i = 0; i < n; ++i) {
2035 pred = get_irn_n(phi, i);
2036 tv = get_Const_tarval(pred);
2037 tv = do_eval(eval, other, tv, mode);
2039 if (tv == tarval_bad) {
2040 /* folding failed, bad */
2046 for (i = 0; i < n; ++i) {
2047 pred = get_irn_n(phi, i);
2048 tv = get_Const_tarval(pred);
2049 tv = do_eval(eval, tv, other, mode);
2051 if (tv == tarval_bad) {
2052 /* folding failed, bad */
2058 irg = get_irn_irg(phi);
2059 for (i = 0; i < n; ++i) {
2060 pred = get_irn_n(phi, i);
2061 res[i] = new_r_Const(irg, (ir_tarval*)res[i]);
2063 return new_r_Phi(get_nodes_block(phi), n, (ir_node **)res, mode);
2064 } /* apply_binop_on_phi */
2067 * Apply an evaluator on a binop with two constant Phi.
2069 * @param a the left Phi node
2070 * @param b the right Phi node
2071 * @param eval an evaluator function
2072 * @param mode the mode of the result, may be different from the mode of the Phi!
2074 * @return a new Phi node if the conversion was successful, NULL else
2076 static ir_node *apply_binop_on_2_phis(ir_node *a, ir_node *b, eval_func eval, ir_mode *mode)
2078 ir_tarval *tv_l, *tv_r, *tv;
2084 if (get_nodes_block(a) != get_nodes_block(b))
2087 n = get_irn_arity(a);
2088 NEW_ARR_A(void *, res, n);
2090 for (i = 0; i < n; ++i) {
2091 pred = get_irn_n(a, i);
2092 tv_l = get_Const_tarval(pred);
2093 pred = get_irn_n(b, i);
2094 tv_r = get_Const_tarval(pred);
2095 tv = do_eval(eval, tv_l, tv_r, mode);
2097 if (tv == tarval_bad) {
2098 /* folding failed, bad */
2103 irg = get_irn_irg(a);
2104 for (i = 0; i < n; ++i) {
2105 pred = get_irn_n(a, i);
2106 res[i] = new_r_Const(irg, (ir_tarval*)res[i]);
2108 return new_r_Phi(get_nodes_block(a), n, (ir_node **)res, mode);
2109 } /* apply_binop_on_2_phis */
2112 * Apply an evaluator on a unop with a constant operator (a Phi).
2114 * @param phi the Phi node
2115 * @param eval an evaluator function
2117 * @return a new Phi node if the conversion was successful, NULL else
2119 static ir_node *apply_unop_on_phi(ir_node *phi, ir_tarval *(*eval)(ir_tarval *))
2126 int i, n = get_irn_arity(phi);
2128 NEW_ARR_A(void *, res, n);
2129 for (i = 0; i < n; ++i) {
2130 pred = get_irn_n(phi, i);
2131 tv = get_Const_tarval(pred);
2134 if (tv == tarval_bad) {
2135 /* folding failed, bad */
2140 mode = get_irn_mode(phi);
2141 irg = get_irn_irg(phi);
2142 for (i = 0; i < n; ++i) {
2143 pred = get_irn_n(phi, i);
2144 res[i] = new_r_Const(irg, (ir_tarval*)res[i]);
2146 return new_r_Phi(get_nodes_block(phi), n, (ir_node **)res, mode);
2147 } /* apply_unop_on_phi */
2150 * Apply a conversion on a constant operator (a Phi).
2152 * @param phi the Phi node
2154 * @return a new Phi node if the conversion was successful, NULL else
2156 static ir_node *apply_conv_on_phi(ir_node *phi, ir_mode *mode)
2162 int i, n = get_irn_arity(phi);
2164 NEW_ARR_A(void *, res, n);
2165 for (i = 0; i < n; ++i) {
2166 pred = get_irn_n(phi, i);
2167 tv = get_Const_tarval(pred);
2168 tv = tarval_convert_to(tv, mode);
2170 if (tv == tarval_bad) {
2171 /* folding failed, bad */
2176 irg = get_irn_irg(phi);
2177 for (i = 0; i < n; ++i) {
2178 pred = get_irn_n(phi, i);
2179 res[i] = new_r_Const(irg, (ir_tarval*)res[i]);
2181 return new_r_Phi(get_nodes_block(phi), n, (ir_node **)res, mode);
2182 } /* apply_conv_on_phi */
2185 * Transform AddP(P, ConvIs(Iu)), AddP(P, ConvIu(Is)) and
2186 * SubP(P, ConvIs(Iu)), SubP(P, ConvIu(Is)).
2187 * If possible, remove the Conv's.
2189 static ir_node *transform_node_AddSub(ir_node *n)
2191 ir_mode *mode = get_irn_mode(n);
2193 if (mode_is_reference(mode)) {
2194 ir_node *left = get_binop_left(n);
2195 ir_node *right = get_binop_right(n);
2196 unsigned ref_bits = get_mode_size_bits(mode);
2198 if (is_Conv(left)) {
2199 ir_mode *lmode = get_irn_mode(left);
2200 unsigned bits = get_mode_size_bits(lmode);
2202 if (ref_bits == bits &&
2203 mode_is_int(lmode) &&
2204 get_mode_arithmetic(lmode) == irma_twos_complement) {
2205 ir_node *pre = get_Conv_op(left);
2206 ir_mode *pre_mode = get_irn_mode(pre);
2208 if (mode_is_int(pre_mode) &&
2209 get_mode_size_bits(pre_mode) == bits &&
2210 get_mode_arithmetic(pre_mode) == irma_twos_complement) {
2211 /* ok, this conv just changes to sign, moreover the calculation
2212 * is done with same number of bits as our address mode, so
2213 * we can ignore the conv as address calculation can be viewed
2214 * as either signed or unsigned
2216 set_binop_left(n, pre);
2221 if (is_Conv(right)) {
2222 ir_mode *rmode = get_irn_mode(right);
2223 unsigned bits = get_mode_size_bits(rmode);
2225 if (ref_bits == bits &&
2226 mode_is_int(rmode) &&
2227 get_mode_arithmetic(rmode) == irma_twos_complement) {
2228 ir_node *pre = get_Conv_op(right);
2229 ir_mode *pre_mode = get_irn_mode(pre);
2231 if (mode_is_int(pre_mode) &&
2232 get_mode_size_bits(pre_mode) == bits &&
2233 get_mode_arithmetic(pre_mode) == irma_twos_complement) {
2234 /* ok, this conv just changes to sign, moreover the calculation
2235 * is done with same number of bits as our address mode, so
2236 * we can ignore the conv as address calculation can be viewed
2237 * as either signed or unsigned
2239 set_binop_right(n, pre);
2244 /* let address arithmetic use unsigned modes */
2245 if (is_Const(right)) {
2246 ir_mode *rmode = get_irn_mode(right);
2248 if (mode_is_signed(rmode) && get_mode_arithmetic(rmode) == irma_twos_complement) {
2249 /* convert a AddP(P, *s) into AddP(P, *u) */
2250 ir_mode *nm = get_reference_mode_unsigned_eq(mode);
2252 ir_node *pre = new_r_Conv(get_nodes_block(n), right, nm);
2253 set_binop_right(n, pre);
2259 } /* transform_node_AddSub */
2261 #define HANDLE_BINOP_PHI(eval, a, b, c, mode) \
2264 if (is_Const(b) && is_const_Phi(a)) { \
2265 /* check for Op(Phi, Const) */ \
2266 c = apply_binop_on_phi(a, get_Const_tarval(b), eval, mode, 0);\
2268 else if (is_Const(a) && is_const_Phi(b)) { \
2269 /* check for Op(Const, Phi) */ \
2270 c = apply_binop_on_phi(b, get_Const_tarval(a), eval, mode, 1);\
2272 else if (is_const_Phi(a) && is_const_Phi(b)) { \
2273 /* check for Op(Phi, Phi) */ \
2274 c = apply_binop_on_2_phis(a, b, eval, mode); \
2277 DBG_OPT_ALGSIM0(oldn, c, FS_OPT_CONST_PHI); \
2282 #define HANDLE_UNOP_PHI(eval, a, c) \
2285 if (is_const_Phi(a)) { \
2286 /* check for Op(Phi) */ \
2287 c = apply_unop_on_phi(a, eval); \
2289 DBG_OPT_ALGSIM0(oldn, c, FS_OPT_CONST_PHI); \
2296 * Do the AddSub optimization, then Transform
2297 * Constant folding on Phi
2298 * Add(a,a) -> Mul(a, 2)
2299 * Add(Mul(a, x), a) -> Mul(a, x+1)
2300 * if the mode is integer or float.
2301 * Transform Add(a,-b) into Sub(a,b).
2302 * Reassociation might fold this further.
2304 static ir_node *transform_node_Add(ir_node *n)
2307 ir_node *a, *b, *c, *oldn = n;
2308 vrp_attr *a_vrp, *b_vrp;
2310 n = transform_node_AddSub(n);
2312 a = get_Add_left(n);
2313 b = get_Add_right(n);
2315 mode = get_irn_mode(n);
2317 if (mode_is_reference(mode)) {
2318 ir_mode *lmode = get_irn_mode(a);
2320 if (is_Const(b) && is_Const_null(b) && mode_is_int(lmode)) {
2321 /* an Add(a, NULL) is a hidden Conv */
2322 dbg_info *dbg = get_irn_dbg_info(n);
2323 return new_rd_Conv(dbg, get_nodes_block(n), a, mode);
2327 HANDLE_BINOP_PHI((eval_func) tarval_add, a, b, c, mode);
2329 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
2330 if (mode_is_float(mode)) {
2331 ir_graph *irg = get_irn_irg(n);
2332 if (get_irg_fp_model(irg) & fp_strict_algebraic)
2336 if (mode_is_num(mode)) {
2337 ir_graph *irg = get_irn_irg(n);
2338 /* the following code leads to endless recursion when Mul are replaced by a simple instruction chain */
2339 if (!is_irg_state(irg, IR_GRAPH_STATE_ARCH_DEP)
2340 && a == b && mode_is_int(mode)) {
2341 ir_node *block = get_nodes_block(n);
2344 get_irn_dbg_info(n),
2347 new_r_Const_long(irg, mode, 2),
2349 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_A);
2354 get_irn_dbg_info(n),
2359 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_MINUS_B);
2364 get_irn_dbg_info(n),
2369 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_MINUS_B);
2372 if (get_mode_arithmetic(mode) == irma_twos_complement) {
2373 /* Here we rely on constants be on the RIGHT side */
2375 ir_node *op = get_Not_op(a);
2377 if (is_Const(b) && is_Const_one(b)) {
2379 ir_node *blk = get_nodes_block(n);
2380 n = new_rd_Minus(get_irn_dbg_info(n), blk, op, mode);
2381 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_PLUS_1);
2386 n = new_r_Const(irg, get_mode_minus_one(mode));
2387 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_X_NOT_X);
2392 ir_node *op = get_Not_op(b);
2396 n = new_r_Const(irg, get_mode_minus_one(mode));
2397 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_X_NOT_X);
2404 a_vrp = vrp_get_info(a);
2405 b_vrp = vrp_get_info(b);
2407 if (a_vrp && b_vrp) {
2408 ir_tarval *c = tarval_and(a_vrp->bits_not_set, b_vrp->bits_not_set);
2410 if (tarval_is_null(c)) {
2411 dbg_info *dbgi = get_irn_dbg_info(n);
2412 return new_rd_Or(dbgi, get_nodes_block(n), a, b, mode);
2416 } /* transform_node_Add */
2419 * returns -cnst or NULL if impossible
2421 static ir_node *const_negate(ir_node *cnst)
2423 ir_tarval *tv = tarval_neg(get_Const_tarval(cnst));
2424 dbg_info *dbgi = get_irn_dbg_info(cnst);
2425 ir_graph *irg = get_irn_irg(cnst);
2426 if (tv == tarval_bad) return NULL;
2427 return new_rd_Const(dbgi, irg, tv);
2431 * Do the AddSub optimization, then Transform
2432 * Constant folding on Phi
2433 * Sub(0,a) -> Minus(a)
2434 * Sub(Mul(a, x), a) -> Mul(a, x-1)
2435 * Sub(Sub(x, y), b) -> Sub(x, Add(y,b))
2436 * Sub(Add(a, x), x) -> a
2437 * Sub(x, Add(x, a)) -> -a
2438 * Sub(x, Const) -> Add(x, -Const)
2440 static ir_node *transform_node_Sub(ir_node *n)
2446 n = transform_node_AddSub(n);
2448 a = get_Sub_left(n);
2449 b = get_Sub_right(n);
2451 mode = get_irn_mode(n);
2453 if (mode_is_int(mode)) {
2454 ir_mode *lmode = get_irn_mode(a);
2456 if (is_Const(b) && is_Const_null(b) && mode_is_reference(lmode)) {
2457 /* a Sub(a, NULL) is a hidden Conv */
2458 dbg_info *dbg = get_irn_dbg_info(n);
2459 n = new_rd_Conv(dbg, get_nodes_block(n), a, mode);
2460 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_CONV);
2464 if (mode == lmode &&
2465 get_mode_arithmetic(mode) == irma_twos_complement &&
2467 get_Const_tarval(a) == get_mode_minus_one(mode)) {
2469 dbg_info *dbg = get_irn_dbg_info(n);
2470 n = new_rd_Not(dbg, get_nodes_block(n), b, mode);
2471 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_NOT);
2477 HANDLE_BINOP_PHI((eval_func) tarval_sub, a, b, c, mode);
2479 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
2480 if (mode_is_float(mode)) {
2481 ir_graph *irg = get_irn_irg(n);
2482 if (get_irg_fp_model(irg) & fp_strict_algebraic)
2486 if (is_Const(b) && !mode_is_reference(get_irn_mode(b))) {
2487 /* a - C -> a + (-C) */
2488 ir_node *cnst = const_negate(b);
2490 ir_node *block = get_nodes_block(n);
2491 dbg_info *dbgi = get_irn_dbg_info(n);
2493 n = new_rd_Add(dbgi, block, a, cnst, mode);
2494 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2499 if (is_Minus(a)) { /* (-a) - b -> -(a + b) */
2500 dbg_info *dbg = get_irn_dbg_info(n);
2501 ir_node *block = get_nodes_block(n);
2502 ir_node *left = get_Minus_op(a);
2503 ir_node *add = new_rd_Add(dbg, block, left, b, mode);
2505 n = new_rd_Minus(dbg, block, add, mode);
2506 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2508 } else if (is_Minus(b)) { /* a - (-b) -> a + b */
2509 dbg_info *dbg = get_irn_dbg_info(n);
2510 ir_node *block = get_nodes_block(n);
2511 ir_node *right = get_Minus_op(b);
2513 n = new_rd_Add(dbg, block, a, right, mode);
2514 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MINUS);
2516 } else if (is_Sub(b)) {
2517 /* a - (b - c) -> a + (c - b)
2518 * -> (a - b) + c iff (b - c) is a pointer */
2519 dbg_info *s_dbg = get_irn_dbg_info(b);
2520 ir_node *s_block = get_nodes_block(b);
2521 ir_node *s_left = get_Sub_left(b);
2522 ir_node *s_right = get_Sub_right(b);
2523 ir_mode *s_mode = get_irn_mode(b);
2524 if (mode_is_reference(s_mode)) {
2525 ir_node *sub = new_rd_Sub(s_dbg, s_block, a, s_left, mode);
2526 dbg_info *a_dbg = get_irn_dbg_info(n);
2527 ir_node *a_block = get_nodes_block(n);
2530 s_right = new_r_Conv(a_block, s_right, mode);
2531 n = new_rd_Add(a_dbg, a_block, sub, s_right, mode);
2533 ir_node *sub = new_rd_Sub(s_dbg, s_block, s_right, s_left, s_mode);
2534 dbg_info *a_dbg = get_irn_dbg_info(n);
2535 ir_node *a_block = get_nodes_block(n);
2537 n = new_rd_Add(a_dbg, a_block, a, sub, mode);
2539 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2541 } else if (is_Mul(b)) { /* a - (b * C) -> a + (b * -C) */
2542 ir_node *m_right = get_Mul_right(b);
2543 if (is_Const(m_right)) {
2544 ir_node *cnst2 = const_negate(m_right);
2545 if (cnst2 != NULL) {
2546 dbg_info *m_dbg = get_irn_dbg_info(b);
2547 ir_node *m_block = get_nodes_block(b);
2548 ir_node *m_left = get_Mul_left(b);
2549 ir_mode *m_mode = get_irn_mode(b);
2550 ir_node *mul = new_rd_Mul(m_dbg, m_block, m_left, cnst2, m_mode);
2551 dbg_info *a_dbg = get_irn_dbg_info(n);
2552 ir_node *a_block = get_nodes_block(n);
2554 n = new_rd_Add(a_dbg, a_block, a, mul, mode);
2555 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2561 /* Beware of Sub(P, P) which cannot be optimized into a simple Minus ... */
2562 if (mode_is_num(mode) && mode == get_irn_mode(a) && is_Const(a) && is_Const_null(a)) {
2564 get_irn_dbg_info(n),
2568 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_0_A);
2572 if (mode_wrap_around(mode)) {
2573 ir_node *left = get_Add_left(a);
2574 ir_node *right = get_Add_right(a);
2576 /* FIXME: Does the Conv's work only for two complement or generally? */
2578 if (mode != get_irn_mode(right)) {
2579 /* This Sub is an effective Cast */
2580 right = new_r_Conv(get_nodes_block(n), right, mode);
2583 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2585 } else if (right == b) {
2586 if (mode != get_irn_mode(left)) {
2587 /* This Sub is an effective Cast */
2588 left = new_r_Conv(get_nodes_block(n), left, mode);
2591 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2597 if (mode_wrap_around(mode)) {
2598 ir_node *left = get_Add_left(b);
2599 ir_node *right = get_Add_right(b);
2601 /* FIXME: Does the Conv's work only for two complement or generally? */
2603 ir_mode *r_mode = get_irn_mode(right);
2605 n = new_r_Minus(get_nodes_block(n), right, r_mode);
2606 if (mode != r_mode) {
2607 /* This Sub is an effective Cast */
2608 n = new_r_Conv(get_nodes_block(n), n, mode);
2610 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2612 } else if (right == a) {
2613 ir_mode *l_mode = get_irn_mode(left);
2615 n = new_r_Minus(get_nodes_block(n), left, l_mode);
2616 if (mode != l_mode) {
2617 /* This Sub is an effective Cast */
2618 n = new_r_Conv(get_nodes_block(n), n, mode);
2620 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2625 if (mode_is_int(mode) && is_Conv(a) && is_Conv(b)) {
2626 ir_mode *mode = get_irn_mode(a);
2628 if (mode == get_irn_mode(b)) {
2630 ir_node *op_a = get_Conv_op(a);
2631 ir_node *op_b = get_Conv_op(b);
2633 /* check if it's allowed to skip the conv */
2634 ma = get_irn_mode(op_a);
2635 mb = get_irn_mode(op_b);
2637 if (mode_is_reference(ma) && mode_is_reference(mb)) {
2638 /* SubInt(ConvInt(aP), ConvInt(bP)) -> SubInt(aP,bP) */
2641 set_Sub_right(n, b);
2647 /* do NOT execute this code if reassociation is enabled, it does the inverse! */
2648 if (!is_reassoc_running() && is_Mul(a)) {
2649 ir_node *ma = get_Mul_left(a);
2650 ir_node *mb = get_Mul_right(a);
2653 ir_node *blk = get_nodes_block(n);
2654 ir_graph *irg = get_irn_irg(n);
2656 get_irn_dbg_info(n),
2660 get_irn_dbg_info(n),
2663 new_r_Const(irg, get_mode_one(mode)),
2666 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MUL_A_X_A);
2668 } else if (mb == b) {
2669 ir_node *blk = get_nodes_block(n);
2670 ir_graph *irg = get_irn_irg(n);
2672 get_irn_dbg_info(n),
2676 get_irn_dbg_info(n),
2679 new_r_Const(irg, get_mode_one(mode)),
2682 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MUL_A_X_A);
2686 if (is_Sub(a)) { /* (x - y) - b -> x - (y + b) */
2687 ir_node *x = get_Sub_left(a);
2688 ir_node *y = get_Sub_right(a);
2689 ir_node *blk = get_nodes_block(n);
2690 ir_mode *m_b = get_irn_mode(b);
2691 ir_mode *m_y = get_irn_mode(y);
2695 /* Determine the right mode for the Add. */
2698 else if (mode_is_reference(m_b))
2700 else if (mode_is_reference(m_y))
2704 * Both modes are different but none is reference,
2705 * happens for instance in SubP(SubP(P, Iu), Is).
2706 * We have two possibilities here: Cast or ignore.
2707 * Currently we ignore this case.
2712 add = new_r_Add(blk, y, b, add_mode);
2714 n = new_rd_Sub(get_irn_dbg_info(n), blk, x, add, mode);
2715 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_SUB_X_Y_Z);
2719 if (get_mode_arithmetic(mode) == irma_twos_complement) {
2720 if (is_Const(a) && is_Not(b)) {
2721 /* c - ~X = X + (c+1) */
2722 ir_tarval *tv = get_Const_tarval(a);
2724 tv = tarval_add(tv, get_mode_one(mode));
2725 if (tv != tarval_bad) {
2726 ir_node *blk = get_nodes_block(n);
2727 ir_graph *irg = get_irn_irg(n);
2728 ir_node *c = new_r_Const(irg, tv);
2729 n = new_rd_Add(get_irn_dbg_info(n), blk, get_Not_op(b), c, mode);
2730 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_C_NOT_X);
2736 } /* transform_node_Sub */
2739 * Several transformation done on n*n=2n bits mul.
2740 * These transformations must be done here because new nodes may be produced.
2742 static ir_node *transform_node_Mul2n(ir_node *n, ir_mode *mode)
2745 ir_node *a = get_Mul_left(n);
2746 ir_node *b = get_Mul_right(n);
2747 ir_tarval *ta = value_of(a);
2748 ir_tarval *tb = value_of(b);
2749 ir_mode *smode = get_irn_mode(a);
2751 if (ta == get_mode_one(smode)) {
2752 /* (L)1 * (L)b = (L)b */
2753 ir_node *blk = get_nodes_block(n);
2754 n = new_rd_Conv(get_irn_dbg_info(n), blk, b, mode);
2755 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
2758 else if (ta == get_mode_minus_one(smode)) {
2759 /* (L)-1 * (L)b = (L)b */
2760 ir_node *blk = get_nodes_block(n);
2761 n = new_rd_Minus(get_irn_dbg_info(n), blk, b, smode);
2762 n = new_rd_Conv(get_irn_dbg_info(n), blk, n, mode);
2763 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2766 if (tb == get_mode_one(smode)) {
2767 /* (L)a * (L)1 = (L)a */
2768 ir_node *blk = get_irn_n(a, -1);
2769 n = new_rd_Conv(get_irn_dbg_info(n), blk, a, mode);
2770 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
2773 else if (tb == get_mode_minus_one(smode)) {
2774 /* (L)a * (L)-1 = (L)-a */
2775 ir_node *blk = get_nodes_block(n);
2776 n = new_rd_Minus(get_irn_dbg_info(n), blk, a, smode);
2777 n = new_rd_Conv(get_irn_dbg_info(n), blk, n, mode);
2778 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2785 * Transform Mul(a,-1) into -a.
2786 * Do constant evaluation of Phi nodes.
2787 * Do architecture dependent optimizations on Mul nodes
2789 static ir_node *transform_node_Mul(ir_node *n)
2791 ir_node *c, *oldn = n;
2792 ir_mode *mode = get_irn_mode(n);
2793 ir_node *a = get_Mul_left(n);
2794 ir_node *b = get_Mul_right(n);
2796 if (is_Bad(a) || is_Bad(b))
2799 if (mode != get_irn_mode(a))
2800 return transform_node_Mul2n(n, mode);
2802 HANDLE_BINOP_PHI((eval_func) tarval_mul, a, b, c, mode);
2804 if (mode_is_signed(mode)) {
2807 if (value_of(a) == get_mode_minus_one(mode))
2809 else if (value_of(b) == get_mode_minus_one(mode))
2812 n = new_rd_Minus(get_irn_dbg_info(n), get_nodes_block(n), r, mode);
2813 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2818 if (is_Const(b)) { /* (-a) * const -> a * -const */
2819 ir_node *cnst = const_negate(b);
2821 dbg_info *dbgi = get_irn_dbg_info(n);
2822 ir_node *block = get_nodes_block(n);
2823 n = new_rd_Mul(dbgi, block, get_Minus_op(a), cnst, mode);
2824 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2827 } else if (is_Minus(b)) { /* (-a) * (-b) -> a * b */
2828 dbg_info *dbgi = get_irn_dbg_info(n);
2829 ir_node *block = get_nodes_block(n);
2830 n = new_rd_Mul(dbgi, block, get_Minus_op(a), get_Minus_op(b), mode);
2831 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_MINUS);
2833 } else if (is_Sub(b)) { /* (-a) * (b - c) -> a * (c - b) */
2834 ir_node *sub_l = get_Sub_left(b);
2835 ir_node *sub_r = get_Sub_right(b);
2836 dbg_info *dbgi = get_irn_dbg_info(n);
2837 ir_node *block = get_nodes_block(n);
2838 ir_node *new_b = new_rd_Sub(dbgi, block, sub_r, sub_l, mode);
2839 n = new_rd_Mul(dbgi, block, get_Minus_op(a), new_b, mode);
2840 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS);
2843 } else if (is_Minus(b)) {
2844 if (is_Sub(a)) { /* (a - b) * (-c) -> (b - a) * c */
2845 ir_node *sub_l = get_Sub_left(a);
2846 ir_node *sub_r = get_Sub_right(a);
2847 dbg_info *dbgi = get_irn_dbg_info(n);
2848 ir_node *block = get_nodes_block(n);
2849 ir_node *new_a = new_rd_Sub(dbgi, block, sub_r, sub_l, mode);
2850 n = new_rd_Mul(dbgi, block, new_a, get_Minus_op(b), mode);
2851 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS);
2854 } else if (is_Shl(a)) {
2855 ir_node *const shl_l = get_Shl_left(a);
2856 if (is_Const(shl_l) && is_Const_one(shl_l)) {
2857 /* (1 << x) * b -> b << x */
2858 dbg_info *const dbgi = get_irn_dbg_info(n);
2859 ir_node *const block = get_nodes_block(n);
2860 ir_node *const shl_r = get_Shl_right(a);
2861 n = new_rd_Shl(dbgi, block, b, shl_r, mode);
2862 // TODO add me DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_SHIFT);
2865 } else if (is_Shl(b)) {
2866 ir_node *const shl_l = get_Shl_left(b);
2867 if (is_Const(shl_l) && is_Const_one(shl_l)) {
2868 /* a * (1 << x) -> a << x */
2869 dbg_info *const dbgi = get_irn_dbg_info(n);
2870 ir_node *const block = get_nodes_block(n);
2871 ir_node *const shl_r = get_Shl_right(b);
2872 n = new_rd_Shl(dbgi, block, a, shl_r, mode);
2873 // TODO add me DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_SHIFT);
2877 if (get_mode_arithmetic(mode) == irma_ieee754) {
2879 ir_tarval *tv = get_Const_tarval(a);
2880 if (tarval_ieee754_get_exponent(tv) == 1 && tarval_ieee754_zero_mantissa(tv)
2881 && !tarval_is_negative(tv)) {
2882 /* 2.0 * b = b + b */
2883 n = new_rd_Add(get_irn_dbg_info(n), get_nodes_block(n), b, b, mode);
2884 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_A_A);
2888 else if (is_Const(b)) {
2889 ir_tarval *tv = get_Const_tarval(b);
2890 if (tarval_ieee754_get_exponent(tv) == 1 && tarval_ieee754_zero_mantissa(tv)
2891 && !tarval_is_negative(tv)) {
2892 /* a * 2.0 = a + a */
2893 n = new_rd_Add(get_irn_dbg_info(n), get_nodes_block(n), a, a, mode);
2894 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_A_A);
2899 return arch_dep_replace_mul_with_shifts(n);
2900 } /* transform_node_Mul */
2903 * Transform a Div Node.
2905 static ir_node *transform_node_Div(ir_node *n)
2907 ir_mode *mode = get_Div_resmode(n);
2908 ir_node *a = get_Div_left(n);
2909 ir_node *b = get_Div_right(n);
2911 const ir_node *dummy;
2913 if (is_Const(b) && is_const_Phi(a)) {
2914 /* check for Div(Phi, Const) */
2915 value = apply_binop_on_phi(a, get_Const_tarval(b), (eval_func) tarval_div, mode, 0);
2917 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2921 else if (is_Const(a) && is_const_Phi(b)) {
2922 /* check for Div(Const, Phi) */
2923 value = apply_binop_on_phi(b, get_Const_tarval(a), (eval_func) tarval_div, mode, 1);
2925 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2929 else if (is_const_Phi(a) && is_const_Phi(b)) {
2930 /* check for Div(Phi, Phi) */
2931 value = apply_binop_on_2_phis(a, b, (eval_func) tarval_div, mode);
2933 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2940 if (a == b && value_not_zero(a, &dummy)) {
2941 ir_graph *irg = get_irn_irg(n);
2942 /* BEWARE: we can optimize a/a to 1 only if this cannot cause a exception */
2943 value = new_r_Const(irg, get_mode_one(mode));
2944 DBG_OPT_CSTEVAL(n, value);
2947 if (mode_is_signed(mode) && is_Const(b)) {
2948 ir_tarval *tv = get_Const_tarval(b);
2950 if (tv == get_mode_minus_one(mode)) {
2952 value = new_rd_Minus(get_irn_dbg_info(n), get_nodes_block(n), a, mode);
2953 DBG_OPT_CSTEVAL(n, value);
2957 /* Try architecture dependent optimization */
2958 value = arch_dep_replace_div_by_const(n);
2966 /* Turn Div into a tuple (mem, jmp, bad, value) */
2967 mem = get_Div_mem(n);
2968 blk = get_nodes_block(n);
2969 irg = get_irn_irg(blk);
2971 /* skip a potential Pin */
2972 mem = skip_Pin(mem);
2973 turn_into_tuple(n, pn_Div_max);
2974 set_Tuple_pred(n, pn_Div_M, mem);
2975 set_Tuple_pred(n, pn_Div_X_regular, new_r_Jmp(blk));
2976 set_Tuple_pred(n, pn_Div_X_except, get_irg_bad(irg));
2977 set_Tuple_pred(n, pn_Div_res, value);
2980 } /* transform_node_Div */
2983 * Transform a Mod node.
2985 static ir_node *transform_node_Mod(ir_node *n)
2987 ir_mode *mode = get_Mod_resmode(n);
2988 ir_node *a = get_Mod_left(n);
2989 ir_node *b = get_Mod_right(n);
2994 if (is_Const(b) && is_const_Phi(a)) {
2995 /* check for Div(Phi, Const) */
2996 value = apply_binop_on_phi(a, get_Const_tarval(b), (eval_func) tarval_mod, mode, 0);
2998 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
3002 else if (is_Const(a) && is_const_Phi(b)) {
3003 /* check for Div(Const, Phi) */
3004 value = apply_binop_on_phi(b, get_Const_tarval(a), (eval_func) tarval_mod, mode, 1);
3006 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
3010 else if (is_const_Phi(a) && is_const_Phi(b)) {
3011 /* check for Div(Phi, Phi) */
3012 value = apply_binop_on_2_phis(a, b, (eval_func) tarval_mod, mode);
3014 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
3021 irg = get_irn_irg(n);
3022 if (tv != tarval_bad) {
3023 value = new_r_Const(irg, tv);
3025 DBG_OPT_CSTEVAL(n, value);
3028 ir_node *a = get_Mod_left(n);
3029 ir_node *b = get_Mod_right(n);
3030 const ir_node *dummy;
3032 if (a == b && value_not_zero(a, &dummy)) {
3033 /* BEWARE: we can optimize a%a to 0 only if this cannot cause a exception */
3034 value = new_r_Const(irg, get_mode_null(mode));
3035 DBG_OPT_CSTEVAL(n, value);
3038 if (mode_is_signed(mode) && is_Const(b)) {
3039 ir_tarval *tv = get_Const_tarval(b);
3041 if (tv == get_mode_minus_one(mode)) {
3043 value = new_r_Const(irg, get_mode_null(mode));
3044 DBG_OPT_CSTEVAL(n, value);
3048 /* Try architecture dependent optimization */
3049 value = arch_dep_replace_mod_by_const(n);
3058 /* Turn Mod into a tuple (mem, jmp, bad, value) */
3059 mem = get_Mod_mem(n);
3060 blk = get_nodes_block(n);
3061 irg = get_irn_irg(blk);
3063 /* skip a potential Pin */
3064 mem = skip_Pin(mem);
3065 turn_into_tuple(n, pn_Mod_max);
3066 set_Tuple_pred(n, pn_Mod_M, mem);
3067 set_Tuple_pred(n, pn_Mod_X_regular, new_r_Jmp(blk));
3068 set_Tuple_pred(n, pn_Mod_X_except, get_irg_bad(irg));
3069 set_Tuple_pred(n, pn_Mod_res, value);
3072 } /* transform_node_Mod */
3075 * Transform a DivMod node.
3077 static ir_node *transform_node_DivMod(ir_node *n)
3079 const ir_node *dummy;
3080 ir_node *a = get_DivMod_left(n);
3081 ir_node *b = get_DivMod_right(n);
3082 ir_mode *mode = get_DivMod_resmode(n);
3084 ir_graph *irg = get_irn_irg(n);
3088 if (is_Const(b) && is_const_Phi(a)) {
3089 /* check for Div(Phi, Const) */
3090 va = apply_binop_on_phi(a, get_Const_tarval(b), (eval_func) tarval_div, mode, 0);
3091 vb = apply_binop_on_phi(a, get_Const_tarval(b), (eval_func) tarval_mod, mode, 0);
3093 DBG_OPT_ALGSIM0(n, va, FS_OPT_CONST_PHI);
3094 DBG_OPT_ALGSIM0(n, vb, FS_OPT_CONST_PHI);
3098 else if (is_Const(a) && is_const_Phi(b)) {
3099 /* check for Div(Const, Phi) */
3100 va = apply_binop_on_phi(b, get_Const_tarval(a), (eval_func) tarval_div, mode, 1);
3101 vb = apply_binop_on_phi(b, get_Const_tarval(a), (eval_func) tarval_mod, mode, 1);
3103 DBG_OPT_ALGSIM0(n, va, FS_OPT_CONST_PHI);
3104 DBG_OPT_ALGSIM0(n, vb, FS_OPT_CONST_PHI);
3108 else if (is_const_Phi(a) && is_const_Phi(b)) {
3109 /* check for Div(Phi, Phi) */
3110 va = apply_binop_on_2_phis(a, b, (eval_func) tarval_div, mode);
3111 vb = apply_binop_on_2_phis(a, b, (eval_func) tarval_mod, mode);
3113 DBG_OPT_ALGSIM0(n, va, FS_OPT_CONST_PHI);
3114 DBG_OPT_ALGSIM0(n, vb, FS_OPT_CONST_PHI);
3121 if (tb != tarval_bad) {
3122 if (tb == get_mode_one(get_tarval_mode(tb))) {
3124 vb = new_r_Const(irg, get_mode_null(mode));
3125 DBG_OPT_CSTEVAL(n, vb);
3127 } else if (ta != tarval_bad) {
3128 ir_tarval *resa, *resb;
3129 resa = tarval_div(ta, tb);
3130 if (resa == tarval_bad) return n; /* Causes exception!!! Model by replacing through
3131 Jmp for X result!? */
3132 resb = tarval_mod(ta, tb);
3133 if (resb == tarval_bad) return n; /* Causes exception! */
3134 va = new_r_Const(irg, resa);
3135 vb = new_r_Const(irg, resb);
3136 DBG_OPT_CSTEVAL(n, va);
3137 DBG_OPT_CSTEVAL(n, vb);
3139 } else if (mode_is_signed(mode) && tb == get_mode_minus_one(mode)) {
3140 va = new_rd_Minus(get_irn_dbg_info(n), get_nodes_block(n), a, mode);
3141 vb = new_r_Const(irg, get_mode_null(mode));
3142 DBG_OPT_CSTEVAL(n, va);
3143 DBG_OPT_CSTEVAL(n, vb);
3145 } else { /* Try architecture dependent optimization */
3148 arch_dep_replace_divmod_by_const(&va, &vb, n);
3149 evaluated = va != NULL;
3151 } else if (a == b) {
3152 if (value_not_zero(a, &dummy)) {
3154 va = new_r_Const(irg, get_mode_one(mode));
3155 vb = new_r_Const(irg, get_mode_null(mode));
3156 DBG_OPT_CSTEVAL(n, va);
3157 DBG_OPT_CSTEVAL(n, vb);
3160 /* BEWARE: it is NOT possible to optimize a/a to 1, as this may cause a exception */
3163 } else if (ta == get_mode_null(mode) && value_not_zero(b, &dummy)) {
3164 /* 0 / non-Const = 0 */
3169 if (evaluated) { /* replace by tuple */
3173 mem = get_DivMod_mem(n);
3174 /* skip a potential Pin */
3175 mem = skip_Pin(mem);
3177 blk = get_nodes_block(n);
3178 turn_into_tuple(n, pn_DivMod_max);
3179 set_Tuple_pred(n, pn_DivMod_M, mem);
3180 set_Tuple_pred(n, pn_DivMod_X_regular, new_r_Jmp(blk));
3181 set_Tuple_pred(n, pn_DivMod_X_except, get_irg_bad(irg)); /*no exception*/
3182 set_Tuple_pred(n, pn_DivMod_res_div, va);
3183 set_Tuple_pred(n, pn_DivMod_res_mod, vb);
3187 } /* transform_node_DivMod */
3190 * Optimize x / c to x * (1/c)
3192 static ir_node *transform_node_Quot(ir_node *n)
3194 ir_mode *mode = get_Quot_resmode(n);
3197 if (get_mode_arithmetic(mode) == irma_ieee754) {
3198 ir_node *b = get_Quot_right(n);
3199 ir_tarval *tv = value_of(b);
3201 if (tv != tarval_bad) {
3202 int rem = tarval_fp_ops_enabled();
3205 * Floating point constant folding might be disabled here to
3207 * However, as we check for exact result, doing it is safe.
3210 tarval_enable_fp_ops(1);
3211 tv = tarval_quo(get_mode_one(mode), tv);
3212 tarval_enable_fp_ops(rem);
3214 /* Do the transformation if the result is either exact or we are not
3215 using strict rules. */
3216 if (tv != tarval_bad &&
3217 (tarval_ieee754_get_exact() || (get_irg_fp_model(get_irn_irg(n)) & fp_strict_algebraic) == 0)) {
3218 ir_node *blk = get_nodes_block(n);
3219 ir_graph *irg = get_irn_irg(blk);
3220 ir_node *c = new_r_Const(irg, tv);
3221 ir_node *a = get_Quot_left(n);
3222 ir_node *m = new_rd_Mul(get_irn_dbg_info(n), blk, a, c, mode);
3223 ir_node *mem = get_Quot_mem(n);
3225 /* skip a potential Pin */
3226 mem = skip_Pin(mem);
3227 turn_into_tuple(n, pn_Quot_max);
3228 set_Tuple_pred(n, pn_Quot_M, mem);
3229 set_Tuple_pred(n, pn_Quot_X_regular, new_r_Jmp(blk));
3230 set_Tuple_pred(n, pn_Quot_X_except, get_irg_bad(irg));
3231 set_Tuple_pred(n, pn_Quot_res, m);
3232 DBG_OPT_ALGSIM1(oldn, a, b, m, FS_OPT_FP_INV_MUL);
3237 } /* transform_node_Quot */
3240 * Optimize -a CMP -b into b CMP a.
3241 * This works only for for modes where unary Minus
3243 * Note that two-complement integers can Overflow
3244 * so it will NOT work.
3246 * For == and != can be handled in Proj(Cmp)
3248 static ir_node *transform_node_Cmp(ir_node *n)
3251 ir_node *left = get_Cmp_left(n);
3252 ir_node *right = get_Cmp_right(n);
3254 if (is_Minus(left) && is_Minus(right) &&
3255 !mode_overflow_on_unary_Minus(get_irn_mode(left))) {
3256 ir_node *const new_left = get_Minus_op(right);
3257 ir_node *const new_right = get_Minus_op(left);
3258 n = new_rd_Cmp(get_irn_dbg_info(n), get_nodes_block(n), new_left, new_right);
3259 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CMP_OP_OP);
3262 } /* transform_node_Cmp */
3266 * Transform a Cond node.
3268 * Replace the Cond by a Jmp if it branches on a constant
3271 static ir_node *transform_node_Cond(ir_node *n)
3274 ir_node *a = get_Cond_selector(n);
3275 ir_tarval *ta = value_of(a);
3276 ir_graph *irg = get_irn_irg(n);
3279 /* we need block info which is not available in floating irgs */
3280 if (get_irg_pinned(irg) == op_pin_state_floats)
3283 if ((ta != tarval_bad) &&
3284 (get_irn_mode(a) == mode_b) &&
3285 (get_opt_unreachable_code())) {
3286 /* It's a boolean Cond, branching on a boolean constant.
3287 Replace it by a tuple (Bad, Jmp) or (Jmp, Bad) */
3288 ir_node *blk = get_nodes_block(n);
3289 jmp = new_r_Jmp(blk);
3290 turn_into_tuple(n, pn_Cond_max);
3291 if (ta == tarval_b_true) {
3292 set_Tuple_pred(n, pn_Cond_false, get_irg_bad(irg));
3293 set_Tuple_pred(n, pn_Cond_true, jmp);
3295 set_Tuple_pred(n, pn_Cond_false, jmp);
3296 set_Tuple_pred(n, pn_Cond_true, get_irg_bad(irg));
3298 /* We might generate an endless loop, so keep it alive. */
3299 add_End_keepalive(get_irg_end(irg), blk);
3302 } /* transform_node_Cond */
3305 * Prototype of a recursive transform function
3306 * for bitwise distributive transformations.
3308 typedef ir_node* (*recursive_transform)(ir_node *n);
3311 * makes use of distributive laws for and, or, eor
3312 * and(a OP c, b OP c) -> and(a, b) OP c
3313 * note, might return a different op than n
3315 static ir_node *transform_bitwise_distributive(ir_node *n,
3316 recursive_transform trans_func)
3319 ir_node *a = get_binop_left(n);
3320 ir_node *b = get_binop_right(n);
3321 ir_op *op = get_irn_op(a);
3322 ir_op *op_root = get_irn_op(n);
3324 if (op != get_irn_op(b))
3327 /* and(conv(a), conv(b)) -> conv(and(a,b)) */
3328 if (op == op_Conv) {
3329 ir_node *a_op = get_Conv_op(a);
3330 ir_node *b_op = get_Conv_op(b);
3331 ir_mode *a_mode = get_irn_mode(a_op);
3332 ir_mode *b_mode = get_irn_mode(b_op);
3333 if (a_mode == b_mode && (mode_is_int(a_mode) || a_mode == mode_b)) {
3334 ir_node *blk = get_nodes_block(n);
3337 set_binop_left(n, a_op);
3338 set_binop_right(n, b_op);
3339 set_irn_mode(n, a_mode);
3341 n = new_r_Conv(blk, n, get_irn_mode(oldn));
3343 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
3349 /* nothing to gain here */
3353 if (op == op_Shrs || op == op_Shr || op == op_Shl
3354 || op == op_And || op == op_Or || op == op_Eor) {
3355 ir_node *a_left = get_binop_left(a);
3356 ir_node *a_right = get_binop_right(a);
3357 ir_node *b_left = get_binop_left(b);
3358 ir_node *b_right = get_binop_right(b);
3360 ir_node *op1 = NULL;
3361 ir_node *op2 = NULL;
3363 if (is_op_commutative(op)) {
3364 if (a_left == b_left) {
3368 } else if (a_left == b_right) {
3372 } else if (a_right == b_left) {
3378 if (a_right == b_right) {
3385 /* (a sop c) & (b sop c) => (a & b) sop c */
3386 ir_node *blk = get_nodes_block(n);
3388 ir_node *new_n = exact_copy(n);
3389 set_binop_left(new_n, op1);
3390 set_binop_right(new_n, op2);
3391 new_n = trans_func(new_n);
3393 if (op_root == op_Eor && op == op_Or) {
3394 dbg_info *dbgi = get_irn_dbg_info(n);
3395 ir_mode *mode = get_irn_mode(c);
3397 c = new_rd_Not(dbgi, blk, c, mode);
3398 n = new_rd_And(dbgi, blk, new_n, c, mode);
3401 set_nodes_block(n, blk);
3402 set_binop_left(n, new_n);
3403 set_binop_right(n, c);
3407 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_SHIFT_AND);
3418 static ir_node *transform_node_And(ir_node *n)
3420 ir_node *c, *oldn = n;
3421 ir_node *a = get_And_left(n);
3422 ir_node *b = get_And_right(n);
3424 vrp_attr *a_vrp, *b_vrp;
3426 mode = get_irn_mode(n);
3427 HANDLE_BINOP_PHI((eval_func) tarval_and, a, b, c, mode);
3429 /* we can evaluate 2 Projs of the same Cmp */
3430 if (mode == mode_b && is_Proj(a) && is_Proj(b)) {
3431 ir_node *pred_a = get_Proj_pred(a);
3432 ir_node *pred_b = get_Proj_pred(b);
3433 if (pred_a == pred_b) {
3434 dbg_info *dbgi = get_irn_dbg_info(n);
3435 pn_Cmp pn_a = get_Proj_pn_cmp(a);
3436 pn_Cmp pn_b = get_Proj_pn_cmp(b);
3437 /* yes, we can simply calculate with pncs */
3438 pn_Cmp new_pnc = pn_a & pn_b;
3440 return new_rd_Proj(dbgi, pred_a, mode_b, new_pnc);
3445 ir_node *op = get_Not_op(b);
3447 ir_node *ba = get_And_left(op);
3448 ir_node *bb = get_And_right(op);
3450 /* it's enough to test the following cases due to normalization! */
3451 if (get_Or_left(a) == ba && get_Or_right(a) == bb) {
3452 /* (a|b) & ~(a&b) = a^b */
3453 ir_node *block = get_nodes_block(n);
3455 n = new_rd_Eor(get_irn_dbg_info(n), block, ba, bb, mode);
3456 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_TO_EOR);
3464 ir_node *op = get_Not_op(a);
3466 ir_node *aa = get_And_left(op);
3467 ir_node *ab = get_And_right(op);
3469 /* it's enough to test the following cases due to normalization! */
3470 if (get_Or_left(b) == aa && get_Or_right(b) == ab) {
3471 /* (a|b) & ~(a&b) = a^b */
3472 ir_node *block = get_nodes_block(n);
3474 n = new_rd_Eor(get_irn_dbg_info(n), block, aa, ab, mode);
3475 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_TO_EOR);
3482 ir_node *al = get_Eor_left(a);
3483 ir_node *ar = get_Eor_right(a);
3486 /* (b ^ a) & b -> ~a & b */
3487 dbg_info *dbg = get_irn_dbg_info(n);
3488 ir_node *block = get_nodes_block(n);
3490 ar = new_rd_Not(dbg, block, ar, mode);
3491 n = new_rd_And(dbg, block, ar, b, mode);
3492 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3496 /* (a ^ b) & b -> ~a & b */
3497 dbg_info *dbg = get_irn_dbg_info(n);
3498 ir_node *block = get_nodes_block(n);
3500 al = new_rd_Not(dbg, block, al, mode);
3501 n = new_rd_And(dbg, block, al, b, mode);
3502 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3507 ir_node *bl = get_Eor_left(b);
3508 ir_node *br = get_Eor_right(b);
3511 /* a & (a ^ b) -> a & ~b */
3512 dbg_info *dbg = get_irn_dbg_info(n);
3513 ir_node *block = get_nodes_block(n);
3515 br = new_rd_Not(dbg, block, br, mode);
3516 n = new_rd_And(dbg, block, br, a, mode);
3517 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3521 /* a & (b ^ a) -> a & ~b */
3522 dbg_info *dbg = get_irn_dbg_info(n);
3523 ir_node *block = get_nodes_block(n);
3525 bl = new_rd_Not(dbg, block, bl, mode);
3526 n = new_rd_And(dbg, block, bl, a, mode);
3527 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3531 if (is_Not(a) && is_Not(b)) {
3532 /* ~a & ~b = ~(a|b) */
3533 ir_node *block = get_nodes_block(n);
3534 ir_mode *mode = get_irn_mode(n);
3538 n = new_rd_Or(get_irn_dbg_info(n), block, a, b, mode);
3539 n = new_rd_Not(get_irn_dbg_info(n), block, n, mode);
3540 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_DEMORGAN);
3544 b_vrp = vrp_get_info(b);
3545 if (is_Const(a) && b_vrp && (tarval_cmp(tarval_or(get_Const_tarval(a),
3546 b_vrp->bits_not_set), get_Const_tarval(a)) == pn_Cmp_Eq)) {
3552 a_vrp = vrp_get_info(a);
3553 if (is_Const(b) && a_vrp && (tarval_cmp(tarval_or(get_Const_tarval(b),
3554 a_vrp->bits_not_set), get_Const_tarval(b)) == pn_Cmp_Eq)) {
3558 n = transform_bitwise_distributive(n, transform_node_And);
3561 } /* transform_node_And */
3563 /* the order of the values is important! */
3564 typedef enum const_class {
3570 static const_class classify_const(const ir_node* n)
3572 if (is_Const(n)) return const_const;
3573 if (is_irn_constlike(n)) return const_like;
3578 * Determines whether r is more constlike or has a larger index (in that order)
3581 static bool operands_are_normalized(const ir_node *l, const ir_node *r)
3583 const const_class l_order = classify_const(l);
3584 const const_class r_order = classify_const(r);
3586 l_order > r_order ||
3587 (l_order == r_order && get_irn_idx(l) <= get_irn_idx(r));
3593 static ir_node *transform_node_Eor(ir_node *n)
3595 ir_node *c, *oldn = n;
3596 ir_node *a = get_Eor_left(n);
3597 ir_node *b = get_Eor_right(n);
3598 ir_mode *mode = get_irn_mode(n);
3600 HANDLE_BINOP_PHI((eval_func) tarval_eor, a, b, c, mode);
3602 /* we can evaluate 2 Projs of the same Cmp */
3603 if (mode == mode_b && is_Proj(a) && is_Proj(b)) {
3604 ir_node *pred_a = get_Proj_pred(a);
3605 ir_node *pred_b = get_Proj_pred(b);
3606 if (pred_a == pred_b) {
3607 dbg_info *dbgi = get_irn_dbg_info(n);
3608 pn_Cmp pn_a = get_Proj_pn_cmp(a);
3609 pn_Cmp pn_b = get_Proj_pn_cmp(b);
3610 /* yes, we can simply calculate with pncs */
3611 pn_Cmp new_pnc = pn_a ^ pn_b;
3613 return new_rd_Proj(dbgi, pred_a, mode_b, new_pnc);
3617 /* normalize not nodes... ~a ^ b <=> a ^ ~b */
3618 if (is_Not(a) && operands_are_normalized(get_Not_op(a), b)) {
3619 dbg_info *dbg = get_irn_dbg_info(n);
3620 ir_node *block = get_nodes_block(n);
3621 ir_node *new_not = new_rd_Not(dbg, block, b, mode);
3622 ir_node *new_left = get_Not_op(a);
3623 n = new_rd_Eor(dbg, block, new_left, new_not, mode);
3624 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3626 } else if (is_Not(b) && !operands_are_normalized(a, get_Not_op(b))) {
3627 dbg_info *dbg = get_irn_dbg_info(n);
3628 ir_node *block = get_nodes_block(n);
3629 ir_node *new_not = new_rd_Not(dbg, block, a, mode);
3630 ir_node *new_right = get_Not_op(b);
3631 n = new_rd_Eor(dbg, block, new_not, new_right, mode);
3632 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3636 /* x ^ 1...1 -> ~1 */
3637 if (is_Const(b) && is_Const_all_one(b)) {
3638 n = new_r_Not(get_nodes_block(n), a, mode);
3639 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3643 n = transform_bitwise_distributive(n, transform_node_Eor);
3645 } /* transform_node_Eor */
3650 static ir_node *transform_node_Not(ir_node *n)
3652 ir_node *c, *oldn = n;
3653 ir_node *a = get_Not_op(n);
3654 ir_mode *mode = get_irn_mode(n);
3656 HANDLE_UNOP_PHI(tarval_not,a,c);
3658 /* check for a boolean Not */
3659 if (mode == mode_b && is_Proj(a)) {
3660 ir_node *a_pred = get_Proj_pred(a);
3661 if (is_Cmp(a_pred)) {
3662 /* We negate a Cmp. The Cmp has the negated result anyways! */
3663 n = new_r_Proj(get_Proj_pred(a),
3664 mode_b, get_negated_pnc(get_Proj_proj(a), mode_b));
3665 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_CMP);
3670 /* normalize ~(a ^ b) => a ^ ~b */
3672 dbg_info *dbg = get_irn_dbg_info(n);
3673 ir_node *block = get_nodes_block(n);
3674 ir_node *eor_right = get_Eor_right(a);
3675 ir_node *eor_left = get_Eor_left(a);
3676 eor_right = new_rd_Not(dbg, block, eor_right, mode);
3677 n = new_rd_Eor(dbg, block, eor_left, eor_right, mode);
3681 if (get_mode_arithmetic(mode) == irma_twos_complement) {
3682 if (is_Minus(a)) { /* ~-x -> x + -1 */
3683 dbg_info *dbg = get_irn_dbg_info(n);
3684 ir_graph *irg = get_irn_irg(n);
3685 ir_node *block = get_nodes_block(n);
3686 ir_node *add_l = get_Minus_op(a);
3687 ir_node *add_r = new_rd_Const(dbg, irg, get_mode_minus_one(mode));
3688 n = new_rd_Add(dbg, block, add_l, add_r, mode);
3689 } else if (is_Add(a)) {
3690 ir_node *add_r = get_Add_right(a);
3691 if (is_Const(add_r) && is_Const_all_one(add_r)) {
3692 /* ~(x + -1) = -x */
3693 ir_node *op = get_Add_left(a);
3694 ir_node *blk = get_nodes_block(n);
3695 n = new_rd_Minus(get_irn_dbg_info(n), blk, op, get_irn_mode(n));
3696 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_MINUS_1);
3701 } /* transform_node_Not */
3704 * Transform a Minus.
3708 * -(a >>u (size-1)) = a >>s (size-1)
3709 * -(a >>s (size-1)) = a >>u (size-1)
3710 * -(a * const) -> a * -const
3712 static ir_node *transform_node_Minus(ir_node *n)
3714 ir_node *c, *oldn = n;
3715 ir_node *a = get_Minus_op(n);
3718 HANDLE_UNOP_PHI(tarval_neg,a,c);
3720 mode = get_irn_mode(a);
3721 if (get_mode_arithmetic(mode) == irma_twos_complement) {
3722 /* the following rules are only to twos-complement */
3725 ir_node *op = get_Not_op(a);
3726 ir_tarval *tv = get_mode_one(mode);
3727 ir_node *blk = get_nodes_block(n);
3728 ir_graph *irg = get_irn_irg(blk);
3729 ir_node *c = new_r_Const(irg, tv);
3730 n = new_rd_Add(get_irn_dbg_info(n), blk, op, c, mode);
3731 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_MINUS_NOT);
3735 ir_node *c = get_Shr_right(a);
3738 ir_tarval *tv = get_Const_tarval(c);
3740 if (tarval_is_long(tv) && get_tarval_long(tv) == (int) get_mode_size_bits(mode) - 1) {
3741 /* -(a >>u (size-1)) = a >>s (size-1) */
3742 ir_node *v = get_Shr_left(a);
3744 n = new_rd_Shrs(get_irn_dbg_info(n), get_nodes_block(n), v, c, mode);
3745 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_PREDICATE);
3751 ir_node *c = get_Shrs_right(a);
3754 ir_tarval *tv = get_Const_tarval(c);
3756 if (tarval_is_long(tv) && get_tarval_long(tv) == (int) get_mode_size_bits(mode) - 1) {
3757 /* -(a >>s (size-1)) = a >>u (size-1) */
3758 ir_node *v = get_Shrs_left(a);
3760 n = new_rd_Shr(get_irn_dbg_info(n), get_nodes_block(n), v, c, mode);
3761 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_PREDICATE);
3768 /* - (a-b) = b - a */
3769 ir_node *la = get_Sub_left(a);
3770 ir_node *ra = get_Sub_right(a);
3771 ir_node *blk = get_nodes_block(n);
3773 n = new_rd_Sub(get_irn_dbg_info(n), blk, ra, la, mode);
3774 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_MINUS_SUB);
3778 if (is_Mul(a)) { /* -(a * const) -> a * -const */
3779 ir_node *mul_l = get_Mul_left(a);
3780 ir_node *mul_r = get_Mul_right(a);
3781 ir_tarval *tv = value_of(mul_r);
3782 if (tv != tarval_bad) {
3783 tv = tarval_neg(tv);
3784 if (tv != tarval_bad) {
3785 ir_graph *irg = get_irn_irg(n);
3786 ir_node *cnst = new_r_Const(irg, tv);
3787 dbg_info *dbg = get_irn_dbg_info(a);
3788 ir_node *block = get_nodes_block(a);
3789 n = new_rd_Mul(dbg, block, mul_l, cnst, mode);
3790 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_MINUS_MUL_C);
3797 } /* transform_node_Minus */
3800 * Transform a Proj(Load) with a non-null address.
3802 static ir_node *transform_node_Proj_Load(ir_node *proj)
3804 if (get_opt_ldst_only_null_ptr_exceptions()) {
3805 if (get_irn_mode(proj) == mode_X) {
3806 ir_node *load = get_Proj_pred(proj);
3808 /* get the Load address */
3809 const ir_node *addr = get_Load_ptr(load);
3810 const ir_node *confirm;
3812 if (value_not_null(addr, &confirm)) {
3813 if (confirm == NULL) {
3814 /* this node may float if it did not depend on a Confirm */
3815 set_irn_pinned(load, op_pin_state_floats);
3817 if (get_Proj_proj(proj) == pn_Load_X_except) {
3818 ir_graph *irg = get_irn_irg(proj);
3819 DBG_OPT_EXC_REM(proj);
3820 return get_irg_bad(irg);
3822 ir_node *blk = get_nodes_block(load);
3823 return new_r_Jmp(blk);
3829 } /* transform_node_Proj_Load */
3832 * Transform a Proj(Store) with a non-null address.
3834 static ir_node *transform_node_Proj_Store(ir_node *proj)
3836 if (get_opt_ldst_only_null_ptr_exceptions()) {
3837 if (get_irn_mode(proj) == mode_X) {
3838 ir_node *store = get_Proj_pred(proj);
3840 /* get the load/store address */
3841 const ir_node *addr = get_Store_ptr(store);
3842 const ir_node *confirm;
3844 if (value_not_null(addr, &confirm)) {
3845 if (confirm == NULL) {
3846 /* this node may float if it did not depend on a Confirm */
3847 set_irn_pinned(store, op_pin_state_floats);
3849 if (get_Proj_proj(proj) == pn_Store_X_except) {
3850 ir_graph *irg = get_irn_irg(proj);
3851 DBG_OPT_EXC_REM(proj);
3852 return get_irg_bad(irg);
3854 ir_node *blk = get_nodes_block(store);
3855 return new_r_Jmp(blk);
3861 } /* transform_node_Proj_Store */
3864 * Transform a Proj(Div) with a non-zero value.
3865 * Removes the exceptions and routes the memory to the NoMem node.
3867 static ir_node *transform_node_Proj_Div(ir_node *proj)
3869 ir_node *div = get_Proj_pred(proj);
3870 ir_node *b = get_Div_right(div);
3871 ir_node *res, *new_mem;
3872 const ir_node *confirm;
3875 if (value_not_zero(b, &confirm)) {
3876 /* div(x, y) && y != 0 */
3877 if (confirm == NULL) {
3878 /* we are sure we have a Const != 0 */
3879 new_mem = get_Div_mem(div);
3880 new_mem = skip_Pin(new_mem);
3881 set_Div_mem(div, new_mem);
3882 set_irn_pinned(div, op_pin_state_floats);
3885 proj_nr = get_Proj_proj(proj);
3887 case pn_Div_X_regular:
3888 return new_r_Jmp(get_nodes_block(div));
3890 case pn_Div_X_except: {
3891 ir_graph *irg = get_irn_irg(proj);
3892 /* we found an exception handler, remove it */
3893 DBG_OPT_EXC_REM(proj);
3894 return get_irg_bad(irg);
3898 ir_graph *irg = get_irn_irg(proj);
3899 res = get_Div_mem(div);
3900 new_mem = get_irg_no_mem(irg);
3903 /* This node can only float up to the Confirm block */
3904 new_mem = new_r_Pin(get_nodes_block(confirm), new_mem);
3906 set_irn_pinned(div, op_pin_state_floats);
3907 /* this is a Div without exception, we can remove the memory edge */
3908 set_Div_mem(div, new_mem);
3914 } /* transform_node_Proj_Div */
3917 * Transform a Proj(Mod) with a non-zero value.
3918 * Removes the exceptions and routes the memory to the NoMem node.
3920 static ir_node *transform_node_Proj_Mod(ir_node *proj)
3922 ir_node *mod = get_Proj_pred(proj);
3923 ir_node *b = get_Mod_right(mod);
3924 ir_node *res, *new_mem;
3925 const ir_node *confirm;
3928 if (value_not_zero(b, &confirm)) {
3929 /* mod(x, y) && y != 0 */
3930 proj_nr = get_Proj_proj(proj);
3932 if (confirm == NULL) {
3933 /* we are sure we have a Const != 0 */
3934 new_mem = get_Mod_mem(mod);
3935 new_mem = skip_Pin(new_mem);
3936 set_Mod_mem(mod, new_mem);
3937 set_irn_pinned(mod, op_pin_state_floats);
3942 case pn_Mod_X_regular:
3943 return new_r_Jmp(get_irn_n(mod, -1));
3945 case pn_Mod_X_except: {
3946 ir_graph *irg = get_irn_irg(proj);
3947 /* we found an exception handler, remove it */
3948 DBG_OPT_EXC_REM(proj);
3949 return get_irg_bad(irg);
3953 ir_graph *irg = get_irn_irg(proj);
3954 res = get_Mod_mem(mod);
3955 new_mem = get_irg_no_mem(irg);
3958 /* This node can only float up to the Confirm block */
3959 new_mem = new_r_Pin(get_nodes_block(confirm), new_mem);
3961 /* this is a Mod without exception, we can remove the memory edge */
3962 set_Mod_mem(mod, new_mem);
3966 if (get_Mod_left(mod) == b) {
3967 /* a % a = 0 if a != 0 */
3968 ir_graph *irg = get_irn_irg(proj);
3969 ir_mode *mode = get_irn_mode(proj);
3970 ir_node *res = new_r_Const(irg, get_mode_null(mode));
3972 DBG_OPT_CSTEVAL(mod, res);
3978 } /* transform_node_Proj_Mod */
3981 * Transform a Proj(DivMod) with a non-zero value.
3982 * Removes the exceptions and routes the memory to the NoMem node.
3984 static ir_node *transform_node_Proj_DivMod(ir_node *proj)
3986 ir_node *divmod = get_Proj_pred(proj);
3987 ir_node *b = get_DivMod_right(divmod);
3988 ir_node *res, *new_mem;
3989 const ir_node *confirm;
3992 if (value_not_zero(b, &confirm)) {
3993 /* DivMod(x, y) && y != 0 */
3994 proj_nr = get_Proj_proj(proj);
3996 if (confirm == NULL) {
3997 /* we are sure we have a Const != 0 */
3998 new_mem = get_DivMod_mem(divmod);
3999 new_mem = skip_Pin(new_mem);
4000 set_DivMod_mem(divmod, new_mem);
4001 set_irn_pinned(divmod, op_pin_state_floats);
4006 case pn_DivMod_X_regular:
4007 return new_r_Jmp(get_nodes_block(divmod));
4009 case pn_DivMod_X_except: {
4010 /* we found an exception handler, remove it */
4011 ir_graph *irg = get_irn_irg(proj);
4012 DBG_OPT_EXC_REM(proj);
4013 return get_irg_bad(irg);
4017 ir_graph *irg = get_irn_irg(proj);
4018 res = get_DivMod_mem(divmod);
4019 new_mem = get_irg_no_mem(irg);
4022 /* This node can only float up to the Confirm block */
4023 new_mem = new_r_Pin(get_nodes_block(confirm), new_mem);
4025 /* this is a DivMod without exception, we can remove the memory edge */
4026 set_DivMod_mem(divmod, new_mem);
4030 case pn_DivMod_res_mod:
4031 if (get_DivMod_left(divmod) == b) {
4032 /* a % a = 0 if a != 0 */
4033 ir_graph *irg = get_irn_irg(proj);
4034 ir_mode *mode = get_irn_mode(proj);
4035 ir_node *res = new_r_Const(irg, get_mode_null(mode));
4037 DBG_OPT_CSTEVAL(divmod, res);
4043 } /* transform_node_Proj_DivMod */
4046 * Optimizes jump tables (CondIs or CondIu) by removing all impossible cases.
4048 static ir_node *transform_node_Proj_Cond(ir_node *proj)
4050 if (get_opt_unreachable_code()) {
4051 ir_node *n = get_Proj_pred(proj);
4052 ir_node *b = get_Cond_selector(n);
4054 if (mode_is_int(get_irn_mode(b))) {
4055 ir_tarval *tb = value_of(b);
4057 if (tb != tarval_bad) {
4058 /* we have a constant switch */
4059 long num = get_Proj_proj(proj);
4061 if (num != get_Cond_default_proj(n)) { /* we cannot optimize default Proj's yet */
4062 if (get_tarval_long(tb) == num) {
4063 /* Do NOT create a jump here, or we will have 2 control flow ops
4064 * in a block. This case is optimized away in optimize_cf(). */
4067 ir_graph *irg = get_irn_irg(proj);
4068 /* this case will NEVER be taken, kill it */
4069 return get_irg_bad(irg);
4073 long num = get_Proj_proj(proj);
4074 vrp_attr *b_vrp = vrp_get_info(b);
4075 if (num != get_Cond_default_proj(n) && b_vrp) {
4076 /* Try handling with vrp data. We only remove dead parts. */
4077 ir_tarval *tp = new_tarval_from_long(num, get_irn_mode(b));
4079 if (b_vrp->range_type == VRP_RANGE) {
4080 pn_Cmp cmp_result = tarval_cmp(b_vrp->range_bottom, tp);
4081 pn_Cmp cmp_result2 = tarval_cmp(b_vrp->range_top, tp);
4083 if ((cmp_result & pn_Cmp_Gt) == cmp_result && (cmp_result2
4084 & pn_Cmp_Lt) == cmp_result2) {
4085 ir_graph *irg = get_irn_irg(proj);
4086 return get_irg_bad(irg);
4088 } else if (b_vrp->range_type == VRP_ANTIRANGE) {
4089 pn_Cmp cmp_result = tarval_cmp(b_vrp->range_bottom, tp);
4090 pn_Cmp cmp_result2 = tarval_cmp(b_vrp->range_top, tp);
4092 if ((cmp_result & pn_Cmp_Le) == cmp_result && (cmp_result2
4093 & pn_Cmp_Ge) == cmp_result2) {
4094 ir_graph *irg = get_irn_irg(proj);
4095 return get_irg_bad(irg);
4100 tarval_and( b_vrp->bits_set, tp),
4103 ir_graph *irg = get_irn_irg(proj);
4104 return get_irg_bad(irg);
4110 tarval_not(b_vrp->bits_not_set)),
4111 tarval_not(b_vrp->bits_not_set))
4113 ir_graph *irg = get_irn_irg(proj);
4114 return get_irg_bad(irg);
4123 } /* transform_node_Proj_Cond */
4126 * Create a 0 constant of given mode.
4128 static ir_node *create_zero_const(ir_graph *irg, ir_mode *mode)
4130 ir_tarval *tv = get_mode_null(mode);
4131 ir_node *cnst = new_r_Const(irg, tv);
4137 * Normalizes and optimizes Cmp nodes.
4139 static ir_node *transform_node_Proj_Cmp(ir_node *proj)
4141 ir_node *n = get_Proj_pred(proj);
4142 ir_node *left = get_Cmp_left(n);
4143 ir_node *right = get_Cmp_right(n);
4144 ir_tarval *tv = NULL;
4146 ir_mode *mode = NULL;
4147 long proj_nr = get_Proj_proj(proj);
4149 /* we can evaluate some cases directly */
4151 case pn_Cmp_False: {
4152 ir_graph *irg = get_irn_irg(proj);
4153 return new_r_Const(irg, get_tarval_b_false());
4156 ir_graph *irg = get_irn_irg(proj);
4157 return new_r_Const(irg, get_tarval_b_true());
4160 if (!mode_is_float(get_irn_mode(left))) {
4161 ir_graph *irg = get_irn_irg(proj);
4162 return new_r_Const(irg, get_tarval_b_true());
4169 /* remove Casts of both sides */
4170 left = skip_Cast(left);
4171 right = skip_Cast(right);
4173 /* Remove unnecessary conversions */
4174 /* TODO handle constants */
4175 if (is_Conv(left) && is_Conv(right)) {
4176 ir_mode *mode = get_irn_mode(left);
4177 ir_node *op_left = get_Conv_op(left);
4178 ir_node *op_right = get_Conv_op(right);
4179 ir_mode *mode_left = get_irn_mode(op_left);
4180 ir_mode *mode_right = get_irn_mode(op_right);
4182 if (smaller_mode(mode_left, mode) && smaller_mode(mode_right, mode)
4183 && mode_left != mode_b && mode_right != mode_b) {
4184 ir_node *block = get_nodes_block(n);
4186 if (mode_left == mode_right) {
4190 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV_CONV);
4191 } else if (smaller_mode(mode_left, mode_right)) {
4192 left = new_r_Conv(block, op_left, mode_right);
4195 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV);
4196 } else if (smaller_mode(mode_right, mode_left)) {
4198 right = new_r_Conv(block, op_right, mode_left);
4200 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV);
4205 /* remove operation on both sides if possible */
4206 if (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) {
4208 * The following operations are NOT safe for floating point operations, for instance
4209 * 1.0 + inf == 2.0 + inf, =/=> x == y
4211 if (mode_is_int(get_irn_mode(left))) {
4212 unsigned lop = get_irn_opcode(left);
4214 if (lop == get_irn_opcode(right)) {
4215 ir_node *ll, *lr, *rl, *rr;
4217 /* same operation on both sides, try to remove */
4221 /* ~a CMP ~b => a CMP b, -a CMP -b ==> a CMP b */
4222 left = get_unop_op(left);
4223 right = get_unop_op(right);
4225 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4228 ll = get_Add_left(left);
4229 lr = get_Add_right(left);
4230 rl = get_Add_left(right);
4231 rr = get_Add_right(right);
4234 /* X + a CMP X + b ==> a CMP b */
4238 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4239 } else if (ll == rr) {
4240 /* X + a CMP b + X ==> a CMP b */
4244 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4245 } else if (lr == rl) {
4246 /* a + X CMP X + b ==> a CMP b */
4250 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4251 } else if (lr == rr) {
4252 /* a + X CMP b + X ==> a CMP b */
4256 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4260 ll = get_Sub_left(left);
4261 lr = get_Sub_right(left);
4262 rl = get_Sub_left(right);
4263 rr = get_Sub_right(right);
4266 /* X - a CMP X - b ==> a CMP b */
4270 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4271 } else if (lr == rr) {
4272 /* a - X CMP b - X ==> a CMP b */
4276 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4280 if (get_Rotl_right(left) == get_Rotl_right(right)) {
4281 /* a ROTL X CMP b ROTL X ==> a CMP b */
4282 left = get_Rotl_left(left);
4283 right = get_Rotl_left(right);
4285 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4293 /* X+A == A, A+X == A, A-X == A -> X == 0 */
4294 if (is_Add(left) || is_Sub(left)) {
4295 ir_node *ll = get_binop_left(left);
4296 ir_node *lr = get_binop_right(left);
4298 if (lr == right && is_Add(left)) {
4304 ir_graph *irg = get_irn_irg(n);
4306 right = create_zero_const(irg, get_irn_mode(left));
4308 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4311 if (is_Add(right) || is_Sub(right)) {
4312 ir_node *rl = get_binop_left(right);
4313 ir_node *rr = get_binop_right(right);
4315 if (rr == left && is_Add(right)) {
4321 ir_graph *irg = get_irn_irg(n);
4323 right = create_zero_const(irg, get_irn_mode(left));
4325 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4328 if (is_And(left) && is_Const(right)) {
4329 ir_node *ll = get_binop_left(left);
4330 ir_node *lr = get_binop_right(left);
4331 if (is_Shr(ll) && is_Const(lr)) {
4332 /* Cmp((x >>u c1) & c2, c3) = Cmp(x & (c2 << c1), c3 << c1) */
4333 ir_node *block = get_nodes_block(n);
4334 ir_mode *mode = get_irn_mode(left);
4336 ir_node *llr = get_Shr_right(ll);
4337 if (is_Const(llr)) {
4338 dbg_info *dbg = get_irn_dbg_info(left);
4339 ir_graph *irg = get_irn_irg(left);
4341 ir_tarval *c1 = get_Const_tarval(llr);
4342 ir_tarval *c2 = get_Const_tarval(lr);
4343 ir_tarval *c3 = get_Const_tarval(right);
4344 ir_tarval *mask = tarval_shl(c2, c1);
4345 ir_tarval *value = tarval_shl(c3, c1);
4347 left = new_rd_And(dbg, block, get_Shr_left(ll), new_r_Const(irg, mask), mode);
4348 right = new_r_Const(irg, value);
4353 /* Cmp(Eor(x, y), 0) <=> Cmp(x, y) at least for the ==0,!=0
4355 if (is_Const(right) && is_Const_null(right) && is_Eor(left)) {
4356 right = get_Eor_right(left);
4357 left = get_Eor_left(left);
4360 } /* mode_is_int(...) */
4361 } /* proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg */
4363 /* replace mode_b compares with ands/ors */
4364 if (get_irn_mode(left) == mode_b) {
4365 ir_node *block = get_nodes_block(n);
4369 case pn_Cmp_Le: bres = new_r_Or( block, new_r_Not(block, left, mode_b), right, mode_b); break;
4370 case pn_Cmp_Lt: bres = new_r_And(block, new_r_Not(block, left, mode_b), right, mode_b); break;
4371 case pn_Cmp_Ge: bres = new_r_Or( block, left, new_r_Not(block, right, mode_b), mode_b); break;
4372 case pn_Cmp_Gt: bres = new_r_And(block, left, new_r_Not(block, right, mode_b), mode_b); break;
4373 case pn_Cmp_Lg: bres = new_r_Eor(block, left, right, mode_b); break;
4374 case pn_Cmp_Eq: bres = new_r_Not(block, new_r_Eor(block, left, right, mode_b), mode_b); break;
4375 default: bres = NULL;
4378 DBG_OPT_ALGSIM0(n, bres, FS_OPT_CMP_TO_BOOL);
4384 * First step: normalize the compare op
4385 * by placing the constant on the right side
4386 * or moving the lower address node to the left.
4388 if (!operands_are_normalized(left, right)) {
4394 proj_nr = get_inversed_pnc(proj_nr);
4399 * Second step: Try to reduce the magnitude
4400 * of a constant. This may help to generate better code
4401 * later and may help to normalize more compares.
4402 * Of course this is only possible for integer values.
4404 tv = value_of(right);
4405 if (tv != tarval_bad) {
4406 mode = get_irn_mode(right);
4408 /* TODO extend to arbitrary constants */
4409 if (is_Conv(left) && tarval_is_null(tv)) {
4410 ir_node *op = get_Conv_op(left);
4411 ir_mode *op_mode = get_irn_mode(op);
4414 * UpConv(x) REL 0 ==> x REL 0
4415 * Don't do this for float values as it's unclear whether it is a
4416 * win. (on the other side it makes detection/creation of fabs hard)
4418 if (get_mode_size_bits(mode) > get_mode_size_bits(op_mode) &&
4419 ((proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) ||
4420 mode_is_signed(mode) || !mode_is_signed(op_mode)) &&
4421 !mode_is_float(mode)) {
4422 tv = get_mode_null(op_mode);
4426 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV);
4430 if (tv != tarval_bad) {
4431 /* the following optimization is possible on modes without Overflow
4432 * on Unary Minus or on == and !=:
4433 * -a CMP c ==> a swap(CMP) -c
4435 * Beware: for two-complement Overflow may occur, so only == and != can
4436 * be optimized, see this:
4437 * -MININT < 0 =/=> MININT > 0 !!!
4439 if (is_Minus(left) &&
4440 (!mode_overflow_on_unary_Minus(mode) ||
4441 (mode_is_int(mode) && (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg)))) {
4442 tv = tarval_neg(tv);
4444 if (tv != tarval_bad) {
4445 left = get_Minus_op(left);
4446 proj_nr = get_inversed_pnc(proj_nr);
4448 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4450 } else if (is_Not(left) && (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg)) {
4451 /* Not(a) ==/!= c ==> a ==/!= Not(c) */
4452 tv = tarval_not(tv);
4454 if (tv != tarval_bad) {
4455 left = get_Not_op(left);
4457 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4461 /* for integer modes, we have more */
4462 if (mode_is_int(mode)) {
4463 /* Ne includes Unordered which is not possible on integers.
4464 * However, frontends often use this wrong, so fix it here */
4465 if (proj_nr & pn_Cmp_Uo) {
4466 proj_nr &= ~pn_Cmp_Uo;
4467 set_Proj_proj(proj, proj_nr);
4470 /* c > 0 : a < c ==> a <= (c-1) a >= c ==> a > (c-1) */
4471 if ((proj_nr == pn_Cmp_Lt || proj_nr == pn_Cmp_Ge) &&
4472 tarval_cmp(tv, get_mode_null(mode)) == pn_Cmp_Gt) {
4473 tv = tarval_sub(tv, get_mode_one(mode), NULL);
4475 if (tv != tarval_bad) {
4476 proj_nr ^= pn_Cmp_Eq;
4478 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CNST_MAGN);
4481 /* c < 0 : a > c ==> a >= (c+1) a <= c ==> a < (c+1) */
4482 else if ((proj_nr == pn_Cmp_Gt || proj_nr == pn_Cmp_Le) &&
4483 tarval_cmp(tv, get_mode_null(mode)) == pn_Cmp_Lt) {
4484 tv = tarval_add(tv, get_mode_one(mode));
4486 if (tv != tarval_bad) {
4487 proj_nr ^= pn_Cmp_Eq;
4489 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CNST_MAGN);
4493 /* the following reassociations work only for == and != */
4494 if (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) {
4496 #if 0 /* Might be not that good in general */
4497 /* a-b == 0 ==> a == b, a-b != 0 ==> a != b */
4498 if (tarval_is_null(tv) && is_Sub(left)) {
4499 right = get_Sub_right(left);
4500 left = get_Sub_left(left);
4502 tv = value_of(right);
4504 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4508 if (tv != tarval_bad) {
4509 /* a-c1 == c2 ==> a == c2+c1, a-c1 != c2 ==> a != c2+c1 */
4511 ir_node *c1 = get_Sub_right(left);
4512 ir_tarval *tv2 = value_of(c1);
4514 if (tv2 != tarval_bad) {
4515 tv2 = tarval_add(tv, value_of(c1));
4517 if (tv2 != tarval_bad) {
4518 left = get_Sub_left(left);
4521 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4525 /* a+c1 == c2 ==> a == c2-c1, a+c1 != c2 ==> a != c2-c1 */
4526 else if (is_Add(left)) {
4527 ir_node *a_l = get_Add_left(left);
4528 ir_node *a_r = get_Add_right(left);
4532 if (is_Const(a_l)) {
4534 tv2 = value_of(a_l);
4537 tv2 = value_of(a_r);
4540 if (tv2 != tarval_bad) {
4541 tv2 = tarval_sub(tv, tv2, NULL);
4543 if (tv2 != tarval_bad) {
4547 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4551 /* -a == c ==> a == -c, -a != c ==> a != -c */
4552 else if (is_Minus(left)) {
4553 ir_tarval *tv2 = tarval_sub(get_mode_null(mode), tv, NULL);
4555 if (tv2 != tarval_bad) {
4556 left = get_Minus_op(left);
4559 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4566 if (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) {
4567 switch (get_irn_opcode(left)) {
4571 c1 = get_And_right(left);
4574 * And(x, C1) == C2 ==> FALSE if C2 & C1 != C2
4575 * And(x, C1) != C2 ==> TRUE if C2 & C1 != C2
4577 ir_tarval *mask = tarval_and(get_Const_tarval(c1), tv);
4579 /* TODO: move to constant evaluation */
4580 ir_graph *irg = get_irn_irg(n);
4581 tv = proj_nr == pn_Cmp_Eq ? get_tarval_b_false() : get_tarval_b_true();
4582 c1 = new_r_Const(irg, tv);
4583 DBG_OPT_CSTEVAL(proj, c1);
4587 if (tarval_is_single_bit(tv)) {
4589 * optimization for AND:
4591 * And(x, C) == C ==> And(x, C) != 0
4592 * And(x, C) != C ==> And(X, C) == 0
4594 * if C is a single Bit constant.
4597 /* check for Constant's match. We have check hare the tarvals,
4598 because our const might be changed */
4599 if (get_Const_tarval(c1) == tv) {
4600 /* fine: do the transformation */
4601 tv = get_mode_null(get_tarval_mode(tv));
4602 proj_nr ^= pn_Cmp_Leg;
4604 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CNST_MAGN);
4610 c1 = get_Or_right(left);
4611 if (is_Const(c1) && tarval_is_null(tv)) {
4613 * Or(x, C) == 0 && C != 0 ==> FALSE
4614 * Or(x, C) != 0 && C != 0 ==> TRUE
4616 if (! tarval_is_null(get_Const_tarval(c1))) {
4617 /* TODO: move to constant evaluation */
4618 ir_graph *irg = get_irn_irg(n);
4619 tv = proj_nr == pn_Cmp_Eq ? get_tarval_b_false() : get_tarval_b_true();
4620 c1 = new_r_Const(irg, tv);
4621 DBG_OPT_CSTEVAL(proj, c1);
4628 * optimize x << c1 == c into x & (-1 >>u c1) == c >> c1 if c & (-1 << c1) == c
4630 * optimize x << c1 != c into x & (-1 >>u c1) != c >> c1 if c & (-1 << c1) == c
4633 c1 = get_Shl_right(left);
4635 ir_graph *irg = get_irn_irg(c1);
4636 ir_tarval *tv1 = get_Const_tarval(c1);
4637 ir_mode *mode = get_irn_mode(left);
4638 ir_tarval *minus1 = get_mode_all_one(mode);
4639 ir_tarval *amask = tarval_shr(minus1, tv1);
4640 ir_tarval *cmask = tarval_shl(minus1, tv1);
4643 if (tarval_and(tv, cmask) != tv) {
4644 /* condition not met */
4645 tv = proj_nr == pn_Cmp_Eq ? get_tarval_b_false() : get_tarval_b_true();
4646 c1 = new_r_Const(irg, tv);
4647 DBG_OPT_CSTEVAL(proj, c1);
4650 sl = get_Shl_left(left);
4651 blk = get_nodes_block(n);
4652 left = new_rd_And(get_irn_dbg_info(left), blk, sl, new_r_Const(irg, amask), mode);
4653 tv = tarval_shr(tv, tv1);
4655 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_SHF_TO_AND);
4660 * optimize x >>u c1 == c into x & (-1 << c1) == c << c1 if c & (-1 >>u c1) == c
4662 * optimize x >>u c1 != c into x & (-1 << c1) != c << c1 if c & (-1 >>u c1) == c
4665 c1 = get_Shr_right(left);
4667 ir_graph *irg = get_irn_irg(c1);
4668 ir_tarval *tv1 = get_Const_tarval(c1);
4669 ir_mode *mode = get_irn_mode(left);
4670 ir_tarval *minus1 = get_mode_all_one(mode);
4671 ir_tarval *amask = tarval_shl(minus1, tv1);
4672 ir_tarval *cmask = tarval_shr(minus1, tv1);
4675 if (tarval_and(tv, cmask) != tv) {
4676 /* condition not met */
4677 tv = proj_nr == pn_Cmp_Eq ? get_tarval_b_false() : get_tarval_b_true();
4678 c1 = new_r_Const(irg, tv);
4679 DBG_OPT_CSTEVAL(proj, c1);
4682 sl = get_Shr_left(left);
4683 blk = get_nodes_block(n);
4684 left = new_rd_And(get_irn_dbg_info(left), blk, sl, new_r_Const(irg, amask), mode);
4685 tv = tarval_shl(tv, tv1);
4687 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_SHF_TO_AND);
4692 * optimize x >>s c1 == c into x & (-1 << c1) == c << c1 if (c >>s (BITS - c1)) \in {0,-1}
4694 * optimize x >>s c1 != c into x & (-1 << c1) != c << c1 if (c >>s (BITS - c1)) \in {0,-1}
4697 c1 = get_Shrs_right(left);
4699 ir_graph *irg = get_irn_irg(c1);
4700 ir_tarval *tv1 = get_Const_tarval(c1);
4701 ir_mode *mode = get_irn_mode(left);
4702 ir_tarval *minus1 = get_mode_all_one(mode);
4703 ir_tarval *amask = tarval_shl(minus1, tv1);
4704 ir_tarval *cond = new_tarval_from_long(get_mode_size_bits(mode), get_tarval_mode(tv1));
4707 cond = tarval_sub(cond, tv1, NULL);
4708 cond = tarval_shrs(tv, cond);
4710 if (!tarval_is_all_one(cond) && !tarval_is_null(cond)) {
4711 /* condition not met */
4712 tv = proj_nr == pn_Cmp_Eq ? get_tarval_b_false() : get_tarval_b_true();
4713 c1 = new_r_Const(irg, tv);
4714 DBG_OPT_CSTEVAL(proj, c1);
4717 sl = get_Shrs_left(left);
4718 blk = get_nodes_block(n);
4719 left = new_rd_And(get_irn_dbg_info(left), blk, sl, new_r_Const(irg, amask), mode);
4720 tv = tarval_shl(tv, tv1);
4722 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_SHF_TO_AND);
4727 } /* tarval != bad */
4730 if (changed & 2) { /* need a new Const */
4731 ir_graph *irg = get_irn_irg(n);
4732 right = new_r_Const(irg, tv);
4735 if ((proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) && is_Const(right) && is_Const_null(right) && is_Proj(left)) {
4736 ir_node *op = get_Proj_pred(left);
4738 if ((is_Mod(op) && get_Proj_proj(left) == pn_Mod_res) ||
4739 (is_DivMod(op) && get_Proj_proj(left) == pn_DivMod_res_mod)) {
4740 ir_node *c = get_binop_right(op);
4743 ir_tarval *tv = get_Const_tarval(c);
4745 if (tarval_is_single_bit(tv)) {
4746 /* special case: (x % 2^n) CMP 0 ==> x & (2^n-1) CMP 0 */
4747 ir_node *v = get_binop_left(op);
4748 ir_node *blk = get_irn_n(op, -1);
4749 ir_graph *irg = get_irn_irg(op);
4750 ir_mode *mode = get_irn_mode(v);
4752 tv = tarval_sub(tv, get_mode_one(mode), NULL);
4753 left = new_rd_And(get_irn_dbg_info(op), blk, v, new_r_Const(irg, tv), mode);
4755 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_MOD_TO_AND);
4762 ir_node *block = get_nodes_block(n);
4764 /* create a new compare */
4765 n = new_rd_Cmp(get_irn_dbg_info(n), block, left, right);
4766 proj = new_rd_Proj(get_irn_dbg_info(proj), n, get_irn_mode(proj), proj_nr);
4770 } /* transform_node_Proj_Cmp */
4773 * Optimize CopyB(mem, x, x) into a Nop.
4775 static ir_node *transform_node_Proj_CopyB(ir_node *proj)
4777 ir_node *copyb = get_Proj_pred(proj);
4778 ir_node *a = get_CopyB_dst(copyb);
4779 ir_node *b = get_CopyB_src(copyb);
4782 switch (get_Proj_proj(proj)) {
4783 case pn_CopyB_X_regular:
4784 /* Turn CopyB into a tuple (mem, jmp, bad, bad) */
4785 DBG_OPT_EXC_REM(proj);
4786 proj = new_r_Jmp(get_nodes_block(copyb));
4788 case pn_CopyB_X_except:
4789 DBG_OPT_EXC_REM(proj);
4790 proj = get_irg_bad(get_irn_irg(proj));
4797 } /* transform_node_Proj_CopyB */
4800 * Optimize Bounds(idx, idx, upper) into idx.
4802 static ir_node *transform_node_Proj_Bound(ir_node *proj)
4804 ir_node *oldn = proj;
4805 ir_node *bound = get_Proj_pred(proj);
4806 ir_node *idx = get_Bound_index(bound);
4807 ir_node *pred = skip_Proj(idx);
4810 if (idx == get_Bound_lower(bound))
4812 else if (is_Bound(pred)) {
4814 * idx was Bounds checked previously, it is still valid if
4815 * lower <= pred_lower && pred_upper <= upper.
4817 ir_node *lower = get_Bound_lower(bound);
4818 ir_node *upper = get_Bound_upper(bound);
4819 if (get_Bound_lower(pred) == lower &&
4820 get_Bound_upper(pred) == upper) {
4822 * One could expect that we simply return the previous
4823 * Bound here. However, this would be wrong, as we could
4824 * add an exception Proj to a new location then.
4825 * So, we must turn in into a tuple.
4831 /* Turn Bound into a tuple (mem, jmp, bad, idx) */
4832 switch (get_Proj_proj(proj)) {
4834 DBG_OPT_EXC_REM(proj);
4835 proj = get_Bound_mem(bound);
4837 case pn_Bound_X_except:
4838 DBG_OPT_EXC_REM(proj);
4839 proj = get_irg_bad(get_irn_irg(proj));
4843 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NOP);
4845 case pn_Bound_X_regular:
4846 DBG_OPT_EXC_REM(proj);
4847 proj = new_r_Jmp(get_nodes_block(bound));
4854 } /* transform_node_Proj_Bound */
4857 * Does all optimizations on nodes that must be done on it's Proj's
4858 * because of creating new nodes.
4860 static ir_node *transform_node_Proj(ir_node *proj)
4862 ir_node *n = get_Proj_pred(proj);
4864 if (n->op->ops.transform_node_Proj)
4865 return n->op->ops.transform_node_Proj(proj);
4867 } /* transform_node_Proj */
4870 * Move Confirms down through Phi nodes.
4872 static ir_node *transform_node_Phi(ir_node *phi)
4875 ir_mode *mode = get_irn_mode(phi);
4877 if (mode_is_reference(mode)) {
4878 n = get_irn_arity(phi);
4880 /* Beware of Phi0 */
4882 ir_node *pred = get_irn_n(phi, 0);
4883 ir_node *bound, *new_phi, *block, **in;
4886 if (! is_Confirm(pred))
4889 bound = get_Confirm_bound(pred);
4890 pnc = get_Confirm_cmp(pred);
4892 NEW_ARR_A(ir_node *, in, n);
4893 in[0] = get_Confirm_value(pred);
4895 for (i = 1; i < n; ++i) {
4896 pred = get_irn_n(phi, i);
4898 if (! is_Confirm(pred) ||
4899 get_Confirm_bound(pred) != bound ||
4900 get_Confirm_cmp(pred) != pnc)
4902 in[i] = get_Confirm_value(pred);
4904 /* move the Confirm nodes "behind" the Phi */
4905 block = get_irn_n(phi, -1);
4906 new_phi = new_r_Phi(block, n, in, get_irn_mode(phi));
4907 return new_r_Confirm(block, new_phi, bound, pnc);
4911 } /* transform_node_Phi */
4914 * Returns the operands of a commutative bin-op, if one operand is
4915 * a const, it is returned as the second one.
4917 static void get_comm_Binop_Ops(ir_node *binop, ir_node **a, ir_node **c)
4919 ir_node *op_a = get_binop_left(binop);
4920 ir_node *op_b = get_binop_right(binop);
4922 assert(is_op_commutative(get_irn_op(binop)));
4924 if (is_Const(op_a)) {
4931 } /* get_comm_Binop_Ops */
4934 * Optimize a Or(And(Or(And(v,c4),c3),c2),c1) pattern if possible.
4935 * Such pattern may arise in bitfield stores.
4937 * value c4 value c4 & c2
4938 * AND c3 AND c1 | c3
4945 * AND c1 ===> OR if (c1 | c2) == 0x111..11
4948 static ir_node *transform_node_Or_bf_store(ir_node *irn_or)
4950 ir_node *irn_and, *c1;
4952 ir_node *and_l, *c3;
4953 ir_node *value, *c4;
4954 ir_node *new_and, *new_const, *block;
4955 ir_mode *mode = get_irn_mode(irn_or);
4957 ir_tarval *tv1, *tv2, *tv3, *tv4, *tv;
4961 get_comm_Binop_Ops(irn_or, &irn_and, &c1);
4962 if (!is_Const(c1) || !is_And(irn_and))
4965 get_comm_Binop_Ops(irn_and, &or_l, &c2);
4969 tv1 = get_Const_tarval(c1);
4970 tv2 = get_Const_tarval(c2);
4972 tv = tarval_or(tv1, tv2);
4973 if (tarval_is_all_one(tv)) {
4974 /* the AND does NOT clear a bit with isn't set by the OR */
4975 set_Or_left(irn_or, or_l);
4976 set_Or_right(irn_or, c1);
4978 /* check for more */
4985 get_comm_Binop_Ops(or_l, &and_l, &c3);
4986 if (!is_Const(c3) || !is_And(and_l))
4989 get_comm_Binop_Ops(and_l, &value, &c4);
4993 /* ok, found the pattern, check for conditions */
4994 assert(mode == get_irn_mode(irn_and));
4995 assert(mode == get_irn_mode(or_l));
4996 assert(mode == get_irn_mode(and_l));
4998 tv3 = get_Const_tarval(c3);
4999 tv4 = get_Const_tarval(c4);
5001 tv = tarval_or(tv4, tv2);
5002 if (!tarval_is_all_one(tv)) {
5003 /* have at least one 0 at the same bit position */
5007 if (tv3 != tarval_andnot(tv3, tv4)) {
5008 /* bit in the or_mask is outside the and_mask */
5012 if (tv1 != tarval_andnot(tv1, tv2)) {
5013 /* bit in the or_mask is outside the and_mask */
5017 /* ok, all conditions met */
5018 block = get_irn_n(irn_or, -1);
5019 irg = get_irn_irg(block);
5021 new_and = new_r_And(block, value, new_r_Const(irg, tarval_and(tv4, tv2)), mode);
5023 new_const = new_r_Const(irg, tarval_or(tv3, tv1));
5025 set_Or_left(irn_or, new_and);
5026 set_Or_right(irn_or, new_const);
5028 /* check for more */
5030 } /* transform_node_Or_bf_store */
5033 * Optimize an Or(shl(x, c), shr(x, bits - c)) into a Rotl
5035 static ir_node *transform_node_Or_Rotl(ir_node *irn_or)
5037 ir_mode *mode = get_irn_mode(irn_or);
5038 ir_node *shl, *shr, *block;
5039 ir_node *irn, *x, *c1, *c2, *n;
5040 ir_tarval *tv1, *tv2;
5042 /* some backends can't handle rotl */
5043 if (!be_get_backend_param()->support_rotl)
5046 if (! mode_is_int(mode))
5049 shl = get_binop_left(irn_or);
5050 shr = get_binop_right(irn_or);
5059 } else if (!is_Shl(shl)) {
5061 } else if (!is_Shr(shr)) {
5064 x = get_Shl_left(shl);
5065 if (x != get_Shr_left(shr))
5068 c1 = get_Shl_right(shl);
5069 c2 = get_Shr_right(shr);
5070 if (is_Const(c1) && is_Const(c2)) {
5071 tv1 = get_Const_tarval(c1);
5072 if (! tarval_is_long(tv1))
5075 tv2 = get_Const_tarval(c2);
5076 if (! tarval_is_long(tv2))
5079 if (get_tarval_long(tv1) + get_tarval_long(tv2)
5080 != (int) get_mode_size_bits(mode))
5083 /* yet, condition met */
5084 block = get_nodes_block(irn_or);
5086 n = new_r_Rotl(block, x, c1, mode);
5088 DBG_OPT_ALGSIM1(irn_or, shl, shr, n, FS_OPT_OR_SHFT_TO_ROTL);
5092 /* Note: the obvious rot formulation (a << x) | (a >> (32-x)) gets
5093 * transformed to (a << x) | (a >> -x) by transform_node_shift_modulo() */
5094 if (!is_negated_value(c1, c2)) {
5098 /* yet, condition met */
5099 block = get_nodes_block(irn_or);
5100 n = new_r_Rotl(block, x, c1, mode);
5101 DBG_OPT_ALGSIM0(irn_or, n, FS_OPT_OR_SHFT_TO_ROTL);
5103 } /* transform_node_Or_Rotl */
5108 static ir_node *transform_node_Or(ir_node *n)
5110 ir_node *c, *oldn = n;
5111 ir_node *a = get_Or_left(n);
5112 ir_node *b = get_Or_right(n);
5115 if (is_Not(a) && is_Not(b)) {
5116 /* ~a | ~b = ~(a&b) */
5117 ir_node *block = get_nodes_block(n);
5119 mode = get_irn_mode(n);
5122 n = new_rd_And(get_irn_dbg_info(n), block, a, b, mode);
5123 n = new_rd_Not(get_irn_dbg_info(n), block, n, mode);
5124 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_DEMORGAN);
5128 /* we can evaluate 2 Projs of the same Cmp */
5129 if (get_irn_mode(n) == mode_b && is_Proj(a) && is_Proj(b)) {
5130 ir_node *pred_a = get_Proj_pred(a);
5131 ir_node *pred_b = get_Proj_pred(b);
5132 if (pred_a == pred_b) {
5133 dbg_info *dbgi = get_irn_dbg_info(n);
5134 pn_Cmp pn_a = get_Proj_pn_cmp(a);
5135 pn_Cmp pn_b = get_Proj_pn_cmp(b);
5136 /* yes, we can simply calculate with pncs */
5137 pn_Cmp new_pnc = pn_a | pn_b;
5139 return new_rd_Proj(dbgi, pred_a, mode_b, new_pnc);
5143 mode = get_irn_mode(n);
5144 HANDLE_BINOP_PHI((eval_func) tarval_or, a, b, c, mode);
5146 n = transform_node_Or_bf_store(n);
5147 n = transform_node_Or_Rotl(n);
5151 n = transform_bitwise_distributive(n, transform_node_Or);
5154 } /* transform_node_Or */
5158 static ir_node *transform_node(ir_node *n);
5161 * Optimize (a >> c1) >> c2), works for Shr, Shrs, Shl, Rotl.
5163 * Should be moved to reassociation?
5165 static ir_node *transform_node_shift(ir_node *n)
5167 ir_node *left, *right;
5169 ir_tarval *tv1, *tv2, *res;
5170 ir_node *in[2], *irn, *block;
5173 left = get_binop_left(n);
5175 /* different operations */
5176 if (get_irn_op(left) != get_irn_op(n))
5179 right = get_binop_right(n);
5180 tv1 = value_of(right);
5181 if (tv1 == tarval_bad)
5184 tv2 = value_of(get_binop_right(left));
5185 if (tv2 == tarval_bad)
5188 res = tarval_add(tv1, tv2);
5189 mode = get_irn_mode(n);
5190 irg = get_irn_irg(n);
5192 /* beware: a simple replacement works only, if res < modulo shift */
5194 int modulo_shf = get_mode_modulo_shift(mode);
5195 if (modulo_shf > 0) {
5196 ir_tarval *modulo = new_tarval_from_long(modulo_shf,
5197 get_tarval_mode(res));
5199 assert(modulo_shf >= (int) get_mode_size_bits(mode));
5201 /* shifting too much */
5202 if (!(tarval_cmp(res, modulo) & pn_Cmp_Lt)) {
5204 ir_node *block = get_nodes_block(n);
5205 dbg_info *dbgi = get_irn_dbg_info(n);
5206 ir_mode *smode = get_irn_mode(right);
5207 ir_node *cnst = new_r_Const_long(irg, smode, get_mode_size_bits(mode) - 1);
5208 return new_rd_Shrs(dbgi, block, get_binop_left(left), cnst, mode);
5211 return new_r_Const(irg, get_mode_null(mode));
5215 res = tarval_mod(res, new_tarval_from_long(get_mode_size_bits(mode), get_tarval_mode(res)));
5218 /* ok, we can replace it */
5219 block = get_nodes_block(n);
5221 in[0] = get_binop_left(left);
5222 in[1] = new_r_Const(irg, res);
5224 irn = new_ir_node(NULL, get_Block_irg(block), block, get_irn_op(n), mode, 2, in);
5226 DBG_OPT_ALGSIM0(n, irn, FS_OPT_REASSOC_SHIFT);
5228 return transform_node(irn);
5229 } /* transform_node_shift */
5232 * normalisation: (x & c1) >> c2 to (x >> c2) & (c1 >> c2)
5234 * - and, or, xor instead of &
5235 * - Shl, Shr, Shrs, rotl instead of >>
5236 * (with a special case for Or/Xor + Shrs)
5238 static ir_node *transform_node_bitop_shift(ir_node *n)
5241 ir_node *right = get_binop_right(n);
5242 ir_mode *mode = get_irn_mode(n);
5243 ir_node *bitop_left;
5244 ir_node *bitop_right;
5254 ir_tarval *tv_shift;
5256 assert(is_Shrs(n) || is_Shr(n) || is_Shl(n) || is_Rotl(n));
5258 if (!is_Const(right))
5261 left = get_binop_left(n);
5262 op_left = get_irn_op(left);
5263 if (op_left != op_And && op_left != op_Or && op_left != op_Eor)
5266 /* doing it with Shrs is not legal if the Or/Eor affects the topmost bit */
5267 if (is_Shrs(n) && (op_left == op_Or || op_left == op_Eor)) {
5268 /* TODO: test if sign bit is affectes */
5272 bitop_right = get_binop_right(left);
5273 if (!is_Const(bitop_right))
5276 bitop_left = get_binop_left(left);
5278 block = get_nodes_block(n);
5279 dbgi = get_irn_dbg_info(n);
5280 tv1 = get_Const_tarval(bitop_right);
5281 tv2 = get_Const_tarval(right);
5283 assert(get_tarval_mode(tv1) == mode);
5286 new_shift = new_rd_Shl(dbgi, block, bitop_left, right, mode);
5287 tv_shift = tarval_shl(tv1, tv2);
5288 } else if (is_Shr(n)) {
5289 new_shift = new_rd_Shr(dbgi, block, bitop_left, right, mode);
5290 tv_shift = tarval_shr(tv1, tv2);
5291 } else if (is_Shrs(n)) {
5292 new_shift = new_rd_Shrs(dbgi, block, bitop_left, right, mode);
5293 tv_shift = tarval_shrs(tv1, tv2);
5296 new_shift = new_rd_Rotl(dbgi, block, bitop_left, right, mode);
5297 tv_shift = tarval_rotl(tv1, tv2);
5300 assert(get_tarval_mode(tv_shift) == mode);
5301 irg = get_irn_irg(n);
5302 new_const = new_r_Const(irg, tv_shift);
5304 if (op_left == op_And) {
5305 new_bitop = new_rd_And(dbgi, block, new_shift, new_const, mode);
5306 } else if (op_left == op_Or) {
5307 new_bitop = new_rd_Or(dbgi, block, new_shift, new_const, mode);
5309 assert(op_left == op_Eor);
5310 new_bitop = new_rd_Eor(dbgi, block, new_shift, new_const, mode);
5318 * (x << c1) >> c2 <=> x OP (c2-c1) & ((-1 << c1) >> c2)
5320 * (x >> c1) << c2 <=> x OP (c2-c1) & ((-1 >> c1) << c2)
5321 * (also with x >>s c1 when c1>=c2)
5323 static ir_node *transform_node_shl_shr(ir_node *n)
5326 ir_node *right = get_binop_right(n);
5336 ir_tarval *tv_shift;
5342 assert(is_Shl(n) || is_Shr(n) || is_Shrs(n));
5344 if (!is_Const(right))
5347 left = get_binop_left(n);
5348 mode = get_irn_mode(n);
5349 if (is_Shl(n) && (is_Shr(left) || is_Shrs(left))) {
5350 ir_node *shr_right = get_binop_right(left);
5352 if (!is_Const(shr_right))
5355 x = get_binop_left(left);
5356 tv_shr = get_Const_tarval(shr_right);
5357 tv_shl = get_Const_tarval(right);
5359 if (is_Shrs(left)) {
5360 /* shrs variant only allowed if c1 >= c2 */
5361 if (! (tarval_cmp(tv_shl, tv_shr) & pn_Cmp_Ge))
5364 tv_mask = tarval_shrs(get_mode_all_one(mode), tv_shr);
5367 tv_mask = tarval_shr(get_mode_all_one(mode), tv_shr);
5369 tv_mask = tarval_shl(tv_mask, tv_shl);
5370 } else if (is_Shr(n) && is_Shl(left)) {
5371 ir_node *shl_right = get_Shl_right(left);
5373 if (!is_Const(shl_right))
5376 x = get_Shl_left(left);
5377 tv_shr = get_Const_tarval(right);
5378 tv_shl = get_Const_tarval(shl_right);
5380 tv_mask = tarval_shl(get_mode_all_one(mode), tv_shl);
5381 tv_mask = tarval_shr(tv_mask, tv_shr);
5386 if (get_tarval_mode(tv_shl) != get_tarval_mode(tv_shr)) {
5387 tv_shl = tarval_convert_to(tv_shl, get_tarval_mode(tv_shr));
5390 assert(tv_mask != tarval_bad);
5391 assert(get_tarval_mode(tv_mask) == mode);
5393 block = get_nodes_block(n);
5394 irg = get_irn_irg(block);
5395 dbgi = get_irn_dbg_info(n);
5397 pnc = tarval_cmp(tv_shl, tv_shr);
5398 if (pnc == pn_Cmp_Lt || pnc == pn_Cmp_Eq) {
5399 tv_shift = tarval_sub(tv_shr, tv_shl, NULL);
5400 new_const = new_r_Const(irg, tv_shift);
5402 new_shift = new_rd_Shrs(dbgi, block, x, new_const, mode);
5404 new_shift = new_rd_Shr(dbgi, block, x, new_const, mode);
5407 assert(pnc == pn_Cmp_Gt);
5408 tv_shift = tarval_sub(tv_shl, tv_shr, NULL);
5409 new_const = new_r_Const(irg, tv_shift);
5410 new_shift = new_rd_Shl(dbgi, block, x, new_const, mode);
5413 new_const = new_r_Const(irg, tv_mask);
5414 new_and = new_rd_And(dbgi, block, new_shift, new_const, mode);
5419 static ir_tarval *get_modulo_tv_value(ir_tarval *tv, int modulo_val)
5421 ir_mode *mode = get_tarval_mode(tv);
5422 ir_tarval *modulo_tv = new_tarval_from_long(modulo_val, mode);
5423 return tarval_mod(tv, modulo_tv);
5426 typedef ir_node*(*new_shift_func)(dbg_info *dbgi, ir_node *block,
5427 ir_node *left, ir_node *right, ir_mode *mode);
5430 * Normalisation: if we have a shl/shr with modulo_shift behaviour
5431 * then we can use that to minimize the value of Add(x, const) or
5432 * Sub(Const, x). In particular this often avoids 1 instruction in some
5433 * backends for the Shift(x, Sub(Const, y)) case because it can be replaced
5434 * by Shift(x, Minus(y)) which doesnt't need an explicit Const constructed.
5436 static ir_node *transform_node_shift_modulo(ir_node *n,
5437 new_shift_func new_shift)
5439 ir_mode *mode = get_irn_mode(n);
5440 int modulo = get_mode_modulo_shift(mode);
5441 ir_node *newop = NULL;
5442 ir_mode *mode_right;
5449 if (get_mode_arithmetic(mode) != irma_twos_complement)
5451 if (!is_po2(modulo))
5454 irg = get_irn_irg(n);
5455 block = get_nodes_block(n);
5456 right = get_binop_right(n);
5457 mode_right = get_irn_mode(right);
5458 if (is_Const(right)) {
5459 ir_tarval *tv = get_Const_tarval(right);
5460 ir_tarval *tv_mod = get_modulo_tv_value(tv, modulo);
5465 newop = new_r_Const(irg, tv_mod);
5466 } else if (is_Add(right)) {
5467 ir_node *add_right = get_Add_right(right);
5468 if (is_Const(add_right)) {
5469 ir_tarval *tv = get_Const_tarval(add_right);
5470 ir_tarval *tv_mod = get_modulo_tv_value(tv, modulo);
5475 newconst = new_r_Const(irg, tv_mod);
5476 newop = new_r_Add(block, get_Add_left(right), newconst,
5479 } else if (is_Sub(right)) {
5480 ir_node *sub_left = get_Sub_left(right);
5481 if (is_Const(sub_left)) {
5482 ir_tarval *tv = get_Const_tarval(sub_left);
5483 ir_tarval *tv_mod = get_modulo_tv_value(tv, modulo);
5488 newconst = new_r_Const(irg, tv_mod);
5489 newop = new_r_Sub(block, newconst, get_Sub_right(right),
5496 if (newop != NULL) {
5497 dbg_info *dbgi = get_irn_dbg_info(n);
5498 ir_node *left = get_binop_left(n);
5499 return new_shift(dbgi, block, left, newop, mode);
5507 static ir_node *transform_node_Shr(ir_node *n)
5509 ir_node *c, *oldn = n;
5510 ir_node *left = get_Shr_left(n);
5511 ir_node *right = get_Shr_right(n);
5512 ir_mode *mode = get_irn_mode(n);
5514 HANDLE_BINOP_PHI((eval_func) tarval_shr, left, right, c, mode);
5515 n = transform_node_shift(n);
5518 n = transform_node_shift_modulo(n, new_rd_Shr);
5520 n = transform_node_shl_shr(n);
5522 n = transform_node_bitop_shift(n);
5525 } /* transform_node_Shr */
5530 static ir_node *transform_node_Shrs(ir_node *n)
5532 ir_node *c, *oldn = n;
5533 ir_node *a = get_Shrs_left(n);
5534 ir_node *b = get_Shrs_right(n);
5535 ir_mode *mode = get_irn_mode(n);
5537 HANDLE_BINOP_PHI((eval_func) tarval_shrs, a, b, c, mode);
5538 n = transform_node_shift(n);
5541 n = transform_node_shift_modulo(n, new_rd_Shrs);
5543 n = transform_node_bitop_shift(n);
5546 } /* transform_node_Shrs */
5551 static ir_node *transform_node_Shl(ir_node *n)
5553 ir_node *c, *oldn = n;
5554 ir_node *a = get_Shl_left(n);
5555 ir_node *b = get_Shl_right(n);
5556 ir_mode *mode = get_irn_mode(n);
5558 HANDLE_BINOP_PHI((eval_func) tarval_shl, a, b, c, mode);
5559 n = transform_node_shift(n);
5562 n = transform_node_shift_modulo(n, new_rd_Shl);
5564 n = transform_node_shl_shr(n);
5566 n = transform_node_bitop_shift(n);
5569 } /* transform_node_Shl */
5574 static ir_node *transform_node_Rotl(ir_node *n)
5576 ir_node *c, *oldn = n;
5577 ir_node *a = get_Rotl_left(n);
5578 ir_node *b = get_Rotl_right(n);
5579 ir_mode *mode = get_irn_mode(n);
5581 HANDLE_BINOP_PHI((eval_func) tarval_rotl, a, b, c, mode);
5582 n = transform_node_shift(n);
5585 n = transform_node_bitop_shift(n);
5588 } /* transform_node_Rotl */
5593 static ir_node *transform_node_Conv(ir_node *n)
5595 ir_node *c, *oldn = n;
5596 ir_mode *mode = get_irn_mode(n);
5597 ir_node *a = get_Conv_op(n);
5599 if (mode != mode_b && is_const_Phi(a)) {
5600 /* Do NOT optimize mode_b Conv's, this leads to remaining
5601 * Phib nodes later, because the conv_b_lower operation
5602 * is instantly reverted, when it tries to insert a Convb.
5604 c = apply_conv_on_phi(a, mode);
5606 DBG_OPT_ALGSIM0(oldn, c, FS_OPT_CONST_PHI);
5611 if (is_Unknown(a)) { /* Conv_A(Unknown_B) -> Unknown_A */
5612 ir_graph *irg = get_irn_irg(n);
5613 return new_r_Unknown(irg, mode);
5616 if (mode_is_reference(mode) &&
5617 get_mode_size_bits(mode) == get_mode_size_bits(get_irn_mode(a)) &&
5619 ir_node *l = get_Add_left(a);
5620 ir_node *r = get_Add_right(a);
5621 dbg_info *dbgi = get_irn_dbg_info(a);
5622 ir_node *block = get_nodes_block(n);
5624 ir_node *lop = get_Conv_op(l);
5625 if (get_irn_mode(lop) == mode) {
5626 /* ConvP(AddI(ConvI(P), x)) -> AddP(P, x) */
5627 n = new_rd_Add(dbgi, block, lop, r, mode);
5632 ir_node *rop = get_Conv_op(r);
5633 if (get_irn_mode(rop) == mode) {
5634 /* ConvP(AddI(x, ConvI(P))) -> AddP(x, P) */
5635 n = new_rd_Add(dbgi, block, l, rop, mode);
5642 } /* transform_node_Conv */
5645 * Remove dead blocks and nodes in dead blocks
5646 * in keep alive list. We do not generate a new End node.
5648 static ir_node *transform_node_End(ir_node *n)
5650 int i, j, n_keepalives = get_End_n_keepalives(n);
5653 NEW_ARR_A(ir_node *, in, n_keepalives);
5655 for (i = j = 0; i < n_keepalives; ++i) {
5656 ir_node *ka = get_End_keepalive(n, i);
5658 if (! is_Block_dead(ka)) {
5662 } else if (is_irn_pinned_in_irg(ka) && is_Block_dead(get_nodes_block(ka))) {
5664 } else if (is_Bad(ka)) {
5665 /* no need to keep Bad */
5670 if (j != n_keepalives)
5671 set_End_keepalives(n, j, in);
5673 } /* transform_node_End */
5675 bool is_negated_value(ir_node *a, ir_node *b)
5677 if (is_Minus(a) && get_Minus_op(a) == b)
5679 if (is_Minus(b) && get_Minus_op(b) == a)
5681 if (is_Sub(a) && is_Sub(b)) {
5682 ir_node *a_left = get_Sub_left(a);
5683 ir_node *a_right = get_Sub_right(a);
5684 ir_node *b_left = get_Sub_left(b);
5685 ir_node *b_right = get_Sub_right(b);
5687 if (a_left == b_right && a_right == b_left)
5695 * Optimize a Mux into some simpler cases.
5697 static ir_node *transform_node_Mux(ir_node *n)
5699 ir_node *oldn = n, *sel = get_Mux_sel(n);
5700 ir_mode *mode = get_irn_mode(n);
5701 ir_node *t = get_Mux_true(n);
5702 ir_node *f = get_Mux_false(n);
5703 ir_graph *irg = get_irn_irg(n);
5705 if (is_irg_state(irg, IR_GRAPH_STATE_KEEP_MUX))
5709 ir_node* block = get_nodes_block(n);
5711 ir_node* c1 = get_Mux_sel(t);
5712 ir_node* t1 = get_Mux_true(t);
5713 ir_node* f1 = get_Mux_false(t);
5715 /* Mux(cond0, Mux(cond1, x, y), y) -> typical if (cond0 && cond1) x else y */
5716 ir_node* and_ = new_r_And(block, c0, c1, mode_b);
5717 ir_node* new_mux = new_r_Mux(block, and_, f1, t1, mode);
5722 DBG_OPT_ALGSIM0(oldn, t, FS_OPT_MUX_COMBINE);
5723 } else if (f == t1) {
5724 /* Mux(cond0, Mux(cond1, x, y), x) */
5725 ir_node* not_c1 = new_r_Not(block, c1, mode_b);
5726 ir_node* and_ = new_r_And(block, c0, not_c1, mode_b);
5727 ir_node* new_mux = new_r_Mux(block, and_, t1, f1, mode);
5732 DBG_OPT_ALGSIM0(oldn, t, FS_OPT_MUX_COMBINE);
5734 } else if (is_Mux(f)) {
5735 ir_node* block = get_nodes_block(n);
5737 ir_node* c1 = get_Mux_sel(f);
5738 ir_node* t1 = get_Mux_true(f);
5739 ir_node* f1 = get_Mux_false(f);
5741 /* Mux(cond0, x, Mux(cond1, x, y)) -> typical if (cond0 || cond1) x else y */
5742 ir_node* or_ = new_r_Or(block, c0, c1, mode_b);
5743 ir_node* new_mux = new_r_Mux(block, or_, f1, t1, mode);
5748 DBG_OPT_ALGSIM0(oldn, f, FS_OPT_MUX_COMBINE);
5749 } else if (t == f1) {
5750 /* Mux(cond0, x, Mux(cond1, y, x)) */
5751 ir_node* not_c1 = new_r_Not(block, c1, mode_b);
5752 ir_node* or_ = new_r_Or(block, c0, not_c1, mode_b);
5753 ir_node* new_mux = new_r_Mux(block, or_, t1, f1, mode);
5758 DBG_OPT_ALGSIM0(oldn, f, FS_OPT_MUX_COMBINE);
5762 /* first normalization step: try to move a constant to the false side,
5763 * 0 preferred on false side too */
5765 ir_node *cmp = get_Proj_pred(sel);
5767 if (is_Cmp(cmp) && is_Const(t) &&
5768 (!is_Const(f) || (is_Const_null(t) && !is_Const_null(f)))) {
5769 pn_Cmp pnc = get_Proj_pn_cmp(sel);
5774 /* Mux(x, a, b) => Mux(not(x), b, a) */
5775 sel = new_r_Proj(cmp, mode_b,
5776 get_negated_pnc(pnc, get_irn_mode(get_Cmp_left(cmp))));
5777 n = new_rd_Mux(get_irn_dbg_info(n), get_nodes_block(n), sel, f, t, mode);
5781 /* note: after normalization, false can only happen on default */
5782 if (mode == mode_b) {
5783 dbg_info *dbg = get_irn_dbg_info(n);
5784 ir_node *block = get_nodes_block(n);
5787 ir_tarval *tv_t = get_Const_tarval(t);
5788 if (tv_t == tarval_b_true) {
5790 /* Muxb(sel, true, false) = sel */
5791 assert(get_Const_tarval(f) == tarval_b_false);
5792 DBG_OPT_ALGSIM0(oldn, sel, FS_OPT_MUX_BOOL);
5795 /* Muxb(sel, true, x) = Or(sel, x) */
5796 n = new_rd_Or(dbg, block, sel, f, mode_b);
5797 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_OR_BOOL);
5801 } else if (is_Const(f)) {
5802 ir_tarval *tv_f = get_Const_tarval(f);
5803 if (tv_f == tarval_b_true) {
5804 /* Muxb(sel, x, true) = Or(Not(sel), x) */
5805 ir_node* not_sel = new_rd_Not(dbg, block, sel, mode_b);
5806 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_ORNOT_BOOL);
5807 n = new_rd_Or(dbg, block, not_sel, t, mode_b);
5810 /* Muxb(sel, x, false) = And(sel, x) */
5811 assert(tv_f == tarval_b_false);
5812 n = new_rd_And(dbg, block, sel, t, mode_b);
5813 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_AND_BOOL);
5819 /* more normalization: Mux(sel, 0, 1) is simply a conv from the mode_b
5820 * value to integer. */
5821 if (is_Const(t) && is_Const(f) && mode_is_int(mode)) {
5822 ir_tarval *a = get_Const_tarval(t);
5823 ir_tarval *b = get_Const_tarval(f);
5825 if (tarval_is_one(a) && tarval_is_null(b)) {
5826 ir_node *block = get_nodes_block(n);
5827 ir_node *conv = new_r_Conv(block, sel, mode);
5829 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_CONV);
5831 } else if (tarval_is_null(a) && tarval_is_one(b)) {
5832 ir_node *block = get_nodes_block(n);
5833 ir_node *not_ = new_r_Not(block, sel, mode_b);
5834 ir_node *conv = new_r_Conv(block, not_, mode);
5836 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_CONV);
5842 ir_node *cmp = get_Proj_pred(sel);
5843 long pn = get_Proj_proj(sel);
5846 * Note: normalization puts the constant on the right side,
5847 * so we check only one case.
5850 ir_node *cmp_r = get_Cmp_right(cmp);
5851 if (is_Const(cmp_r) && is_Const_null(cmp_r)) {
5852 ir_node *block = get_nodes_block(n);
5853 ir_node *cmp_l = get_Cmp_left(cmp);
5855 if (mode_is_int(mode)) {
5857 if ((pn == pn_Cmp_Lg || pn == pn_Cmp_Eq) && is_And(cmp_l)) {
5858 /* Mux((a & b) != 0, c, 0) */
5859 ir_node *and_r = get_And_right(cmp_l);
5862 if (and_r == t && f == cmp_r) {
5863 if (is_Const(t) && tarval_is_single_bit(get_Const_tarval(t))) {
5864 if (pn == pn_Cmp_Lg) {
5865 /* Mux((a & 2^C) != 0, 2^C, 0) */
5867 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_BITOP);
5869 /* Mux((a & 2^C) == 0, 2^C, 0) */
5870 n = new_rd_Eor(get_irn_dbg_info(n),
5871 block, cmp_l, t, mode);
5872 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_BITOP);
5877 if (is_Shl(and_r)) {
5878 ir_node *shl_l = get_Shl_left(and_r);
5879 if (is_Const(shl_l) && is_Const_one(shl_l)) {
5880 if (and_r == t && f == cmp_r) {
5881 if (pn == pn_Cmp_Lg) {
5882 /* (a & (1 << n)) != 0, (1 << n), 0) */
5884 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_BITOP);
5886 /* (a & (1 << n)) == 0, (1 << n), 0) */
5887 n = new_rd_Eor(get_irn_dbg_info(n),
5888 block, cmp_l, t, mode);
5889 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_BITOP);
5895 and_l = get_And_left(cmp_l);
5896 if (is_Shl(and_l)) {
5897 ir_node *shl_l = get_Shl_left(and_l);
5898 if (is_Const(shl_l) && is_Const_one(shl_l)) {
5899 if (and_l == t && f == cmp_r) {
5900 if (pn == pn_Cmp_Lg) {
5901 /* ((1 << n) & a) != 0, (1 << n), 0) */
5903 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_BITOP);
5905 /* ((1 << n) & a) == 0, (1 << n), 0) */
5906 n = new_rd_Eor(get_irn_dbg_info(n),
5907 block, cmp_l, t, mode);
5908 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_BITOP);
5921 } /* transform_node_Mux */
5924 * optimize Sync nodes that have other syncs as input we simply add the inputs
5925 * of the other sync to our own inputs
5927 static ir_node *transform_node_Sync(ir_node *n)
5929 int arity = get_Sync_n_preds(n);
5932 for (i = 0; i < arity;) {
5933 ir_node *pred = get_Sync_pred(n, i);
5937 if (!is_Sync(pred)) {
5945 pred_arity = get_Sync_n_preds(pred);
5946 for (j = 0; j < pred_arity; ++j) {
5947 ir_node *pred_pred = get_Sync_pred(pred, j);
5952 add_irn_n(n, pred_pred);
5956 if (get_Sync_pred(n, k) == pred_pred) break;
5961 /* rehash the sync node */
5965 } /* transform_node_Sync */
5968 * optimize a trampoline Call into a direct Call
5970 static ir_node *transform_node_Call(ir_node *call)
5972 ir_node *callee = get_Call_ptr(call);
5973 ir_node *adr, *mem, *res, *bl, **in;
5974 ir_type *ctp, *mtp, *tp;
5978 int i, n_res, n_param;
5981 if (! is_Proj(callee))
5983 callee = get_Proj_pred(callee);
5984 if (! is_Builtin(callee))
5986 if (get_Builtin_kind(callee) != ir_bk_inner_trampoline)
5989 mem = get_Call_mem(call);
5991 if (skip_Proj(mem) == callee) {
5992 /* memory is routed to the trampoline, skip */
5993 mem = get_Builtin_mem(callee);
5996 /* build a new call type */
5997 mtp = get_Call_type(call);
5998 tdb = get_type_dbg_info(mtp);
6000 n_res = get_method_n_ress(mtp);
6001 n_param = get_method_n_params(mtp);
6002 ctp = new_d_type_method(n_param + 1, n_res, tdb);
6004 for (i = 0; i < n_res; ++i)
6005 set_method_res_type(ctp, i, get_method_res_type(mtp, i));
6007 NEW_ARR_A(ir_node *, in, n_param + 1);
6009 /* FIXME: we don't need a new pointer type in every step */
6010 irg = get_irn_irg(call);
6011 tp = get_irg_frame_type(irg);
6012 tp = new_type_pointer(tp);
6013 set_method_param_type(ctp, 0, tp);
6015 in[0] = get_Builtin_param(callee, 2);
6016 for (i = 0; i < n_param; ++i) {
6017 set_method_param_type(ctp, i + 1, get_method_param_type(mtp, i));
6018 in[i + 1] = get_Call_param(call, i);
6020 var = get_method_variadicity(mtp);
6021 set_method_variadicity(ctp, var);
6022 if (var == variadicity_variadic) {
6023 set_method_first_variadic_param_index(ctp, get_method_first_variadic_param_index(mtp) + 1);
6025 /* When we resolve a trampoline, the function must be called by a this-call */
6026 set_method_calling_convention(ctp, get_method_calling_convention(mtp) | cc_this_call);
6027 set_method_additional_properties(ctp, get_method_additional_properties(mtp));
6029 adr = get_Builtin_param(callee, 1);
6031 db = get_irn_dbg_info(call);
6032 bl = get_nodes_block(call);
6034 res = new_rd_Call(db, bl, mem, adr, n_param + 1, in, ctp);
6035 if (get_irn_pinned(call) == op_pin_state_floats)
6036 set_irn_pinned(res, op_pin_state_floats);
6038 } /* transform_node_Call */
6041 * Tries several [inplace] [optimizing] transformations and returns an
6042 * equivalent node. The difference to equivalent_node() is that these
6043 * transformations _do_ generate new nodes, and thus the old node must
6044 * not be freed even if the equivalent node isn't the old one.
6046 static ir_node *transform_node(ir_node *n)
6051 * Transform_node is the only "optimizing transformation" that might
6052 * return a node with a different opcode. We iterate HERE until fixpoint
6053 * to get the final result.
6057 if (n->op->ops.transform_node != NULL)
6058 n = n->op->ops.transform_node(n);
6059 } while (oldn != n);
6062 } /* transform_node */
6065 * Sets the default transform node operation for an ir_op_ops.
6067 * @param code the opcode for the default operation
6068 * @param ops the operations initialized
6073 static ir_op_ops *firm_set_default_transform_node(ir_opcode code, ir_op_ops *ops)
6077 ops->transform_node = transform_node_##a; \
6079 #define CASE_PROJ(a) \
6081 ops->transform_node_Proj = transform_node_Proj_##a; \
6083 #define CASE_PROJ_EX(a) \
6085 ops->transform_node = transform_node_##a; \
6086 ops->transform_node_Proj = transform_node_Proj_##a; \
6095 CASE_PROJ_EX(DivMod);
6128 } /* firm_set_default_transform_node */
6131 /* **************** Common Subexpression Elimination **************** */
6133 /** The size of the hash table used, should estimate the number of nodes
6135 #define N_IR_NODES 512
6137 /** Compares the attributes of two Const nodes. */
6138 static int node_cmp_attr_Const(ir_node *a, ir_node *b)
6140 return get_Const_tarval(a) != get_Const_tarval(b);
6143 /** Compares the attributes of two Proj nodes. */
6144 static int node_cmp_attr_Proj(ir_node *a, ir_node *b)
6146 return a->attr.proj.proj != b->attr.proj.proj;
6147 } /* node_cmp_attr_Proj */
6149 /** Compares the attributes of two Alloc nodes. */
6150 static int node_cmp_attr_Alloc(ir_node *a, ir_node *b)
6152 const alloc_attr *pa = &a->attr.alloc;
6153 const alloc_attr *pb = &b->attr.alloc;
6154 return (pa->where != pb->where) || (pa->type != pb->type);
6155 } /* node_cmp_attr_Alloc */
6157 /** Compares the attributes of two Free nodes. */
6158 static int node_cmp_attr_Free(ir_node *a, ir_node *b)
6160 const free_attr *pa = &a->attr.free;
6161 const free_attr *pb = &b->attr.free;
6162 return (pa->where != pb->where) || (pa->type != pb->type);
6163 } /* node_cmp_attr_Free */
6165 /** Compares the attributes of two SymConst nodes. */
6166 static int node_cmp_attr_SymConst(ir_node *a, ir_node *b)
6168 const symconst_attr *pa = &a->attr.symc;
6169 const symconst_attr *pb = &b->attr.symc;
6170 return (pa->kind != pb->kind)
6171 || (pa->sym.type_p != pb->sym.type_p);
6174 /** Compares the attributes of two Call nodes. */
6175 static int node_cmp_attr_Call(ir_node *a, ir_node *b)
6177 const call_attr *pa = &a->attr.call;
6178 const call_attr *pb = &b->attr.call;
6179 return (pa->type != pb->type)
6180 || (pa->tail_call != pb->tail_call);
6181 } /* node_cmp_attr_Call */
6183 /** Compares the attributes of two Sel nodes. */
6184 static int node_cmp_attr_Sel(ir_node *a, ir_node *b)
6186 const ir_entity *a_ent = get_Sel_entity(a);
6187 const ir_entity *b_ent = get_Sel_entity(b);
6188 return a_ent != b_ent;
6189 } /* node_cmp_attr_Sel */
6191 /** Compares the attributes of two Phi nodes. */
6192 static int node_cmp_attr_Phi(ir_node *a, ir_node *b)
6194 /* we can only enter this function if both nodes have the same number of inputs,
6195 hence it is enough to check if one of them is a Phi0 */
6197 /* check the Phi0 pos attribute */
6198 return a->attr.phi.u.pos != b->attr.phi.u.pos;
6201 } /* node_cmp_attr_Phi */
6203 /** Compares the attributes of two Conv nodes. */
6204 static int node_cmp_attr_Conv(ir_node *a, ir_node *b)
6206 return get_Conv_strict(a) != get_Conv_strict(b);
6207 } /* node_cmp_attr_Conv */
6209 /** Compares the attributes of two Cast nodes. */
6210 static int node_cmp_attr_Cast(ir_node *a, ir_node *b)
6212 return get_Cast_type(a) != get_Cast_type(b);
6213 } /* node_cmp_attr_Cast */
6215 /** Compares the attributes of two Load nodes. */
6216 static int node_cmp_attr_Load(ir_node *a, ir_node *b)
6218 if (get_Load_volatility(a) == volatility_is_volatile ||
6219 get_Load_volatility(b) == volatility_is_volatile)
6220 /* NEVER do CSE on volatile Loads */
6222 /* do not CSE Loads with different alignment. Be conservative. */
6223 if (get_Load_align(a) != get_Load_align(b))
6226 return get_Load_mode(a) != get_Load_mode(b);
6227 } /* node_cmp_attr_Load */
6229 /** Compares the attributes of two Store nodes. */
6230 static int node_cmp_attr_Store(ir_node *a, ir_node *b)
6232 /* do not CSE Stores with different alignment. Be conservative. */
6233 if (get_Store_align(a) != get_Store_align(b))
6236 /* NEVER do CSE on volatile Stores */
6237 return (get_Store_volatility(a) == volatility_is_volatile ||
6238 get_Store_volatility(b) == volatility_is_volatile);
6239 } /* node_cmp_attr_Store */
6241 /** Compares two exception attributes */
6242 static int node_cmp_exception(ir_node *a, ir_node *b)
6244 const except_attr *ea = &a->attr.except;
6245 const except_attr *eb = &b->attr.except;
6247 return ea->pin_state != eb->pin_state;
6250 #define node_cmp_attr_Bound node_cmp_exception
6252 /** Compares the attributes of two Div nodes. */
6253 static int node_cmp_attr_Div(ir_node *a, ir_node *b)
6255 const divmod_attr *ma = &a->attr.divmod;
6256 const divmod_attr *mb = &b->attr.divmod;
6257 return ma->exc.pin_state != mb->exc.pin_state ||
6258 ma->resmode != mb->resmode ||
6259 ma->no_remainder != mb->no_remainder;
6260 } /* node_cmp_attr_Div */
6262 /** Compares the attributes of two DivMod nodes. */
6263 static int node_cmp_attr_DivMod(ir_node *a, ir_node *b)
6265 const divmod_attr *ma = &a->attr.divmod;
6266 const divmod_attr *mb = &b->attr.divmod;
6267 return ma->exc.pin_state != mb->exc.pin_state ||
6268 ma->resmode != mb->resmode;
6269 } /* node_cmp_attr_DivMod */
6271 /** Compares the attributes of two Mod nodes. */
6272 static int node_cmp_attr_Mod(ir_node *a, ir_node *b)
6274 return node_cmp_attr_DivMod(a, b);
6275 } /* node_cmp_attr_Mod */
6277 /** Compares the attributes of two Quot nodes. */
6278 static int node_cmp_attr_Quot(ir_node *a, ir_node *b)
6280 return node_cmp_attr_DivMod(a, b);
6281 } /* node_cmp_attr_Quot */
6283 /** Compares the attributes of two Confirm nodes. */
6284 static int node_cmp_attr_Confirm(ir_node *a, ir_node *b)
6286 /* no need to compare the bound, as this is a input */
6287 return (get_Confirm_cmp(a) != get_Confirm_cmp(b));
6288 } /* node_cmp_attr_Confirm */
6290 /** Compares the attributes of two Builtin nodes. */
6291 static int node_cmp_attr_Builtin(ir_node *a, ir_node *b)
6293 /* no need to compare the type, equal kind means equal type */
6294 return get_Builtin_kind(a) != get_Builtin_kind(b);
6295 } /* node_cmp_attr_Builtin */
6297 /** Compares the attributes of two ASM nodes. */
6298 static int node_cmp_attr_ASM(ir_node *a, ir_node *b)
6301 const ir_asm_constraint *ca;
6302 const ir_asm_constraint *cb;
6305 if (get_ASM_text(a) != get_ASM_text(b))
6308 /* Should we really check the constraints here? Should be better, but is strange. */
6309 n = get_ASM_n_input_constraints(a);
6310 if (n != get_ASM_n_input_constraints(b))
6313 ca = get_ASM_input_constraints(a);
6314 cb = get_ASM_input_constraints(b);
6315 for (i = 0; i < n; ++i) {
6316 if (ca[i].pos != cb[i].pos || ca[i].constraint != cb[i].constraint)
6320 n = get_ASM_n_output_constraints(a);
6321 if (n != get_ASM_n_output_constraints(b))
6324 ca = get_ASM_output_constraints(a);
6325 cb = get_ASM_output_constraints(b);
6326 for (i = 0; i < n; ++i) {
6327 if (ca[i].pos != cb[i].pos || ca[i].constraint != cb[i].constraint)
6331 n = get_ASM_n_clobbers(a);
6332 if (n != get_ASM_n_clobbers(b))
6335 cla = get_ASM_clobbers(a);
6336 clb = get_ASM_clobbers(b);
6337 for (i = 0; i < n; ++i) {
6338 if (cla[i] != clb[i])
6342 } /* node_cmp_attr_ASM */
6344 /** Compares the inexistent attributes of two Dummy nodes. */
6345 static int node_cmp_attr_Dummy(ir_node *a, ir_node *b)
6353 * Set the default node attribute compare operation for an ir_op_ops.
6355 * @param code the opcode for the default operation
6356 * @param ops the operations initialized
6361 static ir_op_ops *firm_set_default_node_cmp_attr(ir_opcode code, ir_op_ops *ops)
6365 ops->node_cmp_attr = node_cmp_attr_##a; \
6398 } /* firm_set_default_node_cmp_attr */
6401 * Compare function for two nodes in the value table. Gets two
6402 * nodes as parameters. Returns 0 if the nodes are a Common Sub Expression.
6404 int identities_cmp(const void *elt, const void *key)
6406 ir_node *a = (ir_node *)elt;
6407 ir_node *b = (ir_node *)key;
6410 if (a == b) return 0;
6412 if ((get_irn_op(a) != get_irn_op(b)) ||
6413 (get_irn_mode(a) != get_irn_mode(b))) return 1;
6415 /* compare if a's in and b's in are of equal length */
6416 irn_arity_a = get_irn_arity(a);
6417 if (irn_arity_a != get_irn_arity(b))
6420 /* blocks are never the same */
6424 if (get_irn_pinned(a) == op_pin_state_pinned) {
6425 /* for pinned nodes, the block inputs must be equal */
6426 if (get_irn_n(a, -1) != get_irn_n(b, -1))
6428 } else if (! get_opt_global_cse()) {
6429 /* for block-local CSE both nodes must be in the same Block */
6430 if (get_nodes_block(a) != get_nodes_block(b))
6434 /* compare a->in[0..ins] with b->in[0..ins] */
6435 for (i = 0; i < irn_arity_a; ++i) {
6436 ir_node *pred_a = get_irn_n(a, i);
6437 ir_node *pred_b = get_irn_n(b, i);
6438 if (pred_a != pred_b) {
6439 /* if both predecessors are CSE neutral they might be different */
6440 if (!is_irn_cse_neutral(pred_a) || !is_irn_cse_neutral(pred_b))
6446 * here, we already now that the nodes are identical except their
6449 if (a->op->ops.node_cmp_attr)
6450 return a->op->ops.node_cmp_attr(a, b);
6453 } /* identities_cmp */
6456 * Calculate a hash value of a node.
6458 * @param node The IR-node
6460 unsigned ir_node_hash(const ir_node *node)
6462 return node->op->ops.hash(node);
6463 } /* ir_node_hash */
6466 void new_identities(ir_graph *irg)
6468 if (irg->value_table != NULL)
6469 del_pset(irg->value_table);
6470 irg->value_table = new_pset(identities_cmp, N_IR_NODES);
6471 } /* new_identities */
6473 void del_identities(ir_graph *irg)
6475 if (irg->value_table != NULL)
6476 del_pset(irg->value_table);
6477 } /* del_identities */
6479 /* Normalize a node by putting constants (and operands with larger
6480 * node index) on the right (operator side). */
6481 void ir_normalize_node(ir_node *n)
6483 if (is_op_commutative(get_irn_op(n))) {
6484 ir_node *l = get_binop_left(n);
6485 ir_node *r = get_binop_right(n);
6487 /* For commutative operators perform a OP b == b OP a but keep
6488 * constants on the RIGHT side. This helps greatly in some
6489 * optimizations. Moreover we use the idx number to make the form
6491 if (!operands_are_normalized(l, r)) {
6492 set_binop_left(n, r);
6493 set_binop_right(n, l);
6497 } /* ir_normalize_node */
6500 * Return the canonical node computing the same value as n.
6501 * Looks up the node in a hash table, enters it in the table
6502 * if it isn't there yet.
6504 * @param n the node to look up
6506 * @return a node that computes the same value as n or n if no such
6507 * node could be found
6509 ir_node *identify_remember(ir_node *n)
6511 ir_graph *irg = get_irn_irg(n);
6512 pset *value_table = irg->value_table;
6515 if (value_table == NULL)
6518 ir_normalize_node(n);
6519 /* lookup or insert in hash table with given hash key. */
6520 nn = (ir_node*)pset_insert(value_table, n, ir_node_hash(n));
6523 /* n is reachable again */
6524 edges_node_revival(nn, get_irn_irg(nn));
6528 } /* identify_remember */
6531 * During construction we set the op_pin_state_pinned flag in the graph right
6532 * when the optimization is performed. The flag turning on procedure global
6533 * cse could be changed between two allocations. This way we are safe.
6535 * @param n The node to lookup
6537 static inline ir_node *identify_cons(ir_node *n)
6541 n = identify_remember(n);
6542 if (n != old && get_nodes_block(old) != get_nodes_block(n)) {
6543 ir_graph *irg = get_irn_irg(n);
6544 set_irg_pinned(irg, op_pin_state_floats);
6547 } /* identify_cons */
6549 /* Add a node to the identities value table. */
6550 void add_identities(ir_node *node)
6557 identify_remember(node);
6560 /* Visit each node in the value table of a graph. */
6561 void visit_all_identities(ir_graph *irg, irg_walk_func visit, void *env)
6564 ir_graph *rem = current_ir_graph;
6566 current_ir_graph = irg;
6567 foreach_pset(irg->value_table, ir_node*, node) {
6570 current_ir_graph = rem;
6571 } /* visit_all_identities */
6574 * Garbage in, garbage out. If a node has a dead input, i.e., the
6575 * Bad node is input to the node, return the Bad node.
6577 static ir_node *gigo(ir_node *node)
6580 ir_op *op = get_irn_op(node);
6582 /* remove garbage blocks by looking at control flow that leaves the block
6583 and replacing the control flow by Bad. */
6584 if (get_irn_mode(node) == mode_X) {
6585 ir_node *block = get_nodes_block(skip_Proj(node));
6586 ir_graph *irg = get_irn_irg(block);
6588 /* Don't optimize nodes in immature blocks. */
6589 if (!get_Block_matured(block))
6591 /* Don't optimize End, may have Bads. */
6592 if (op == op_End) return node;
6594 if (is_Block(block)) {
6595 if (is_Block_dead(block)) {
6596 /* control flow from dead block is dead */
6597 return get_irg_bad(irg);
6600 for (i = get_irn_arity(block) - 1; i >= 0; --i) {
6601 if (!is_Bad(get_irn_n(block, i)))
6605 ir_graph *irg = get_irn_irg(block);
6606 /* the start block is never dead */
6607 if (block != get_irg_start_block(irg)
6608 && block != get_irg_end_block(irg)) {
6610 * Do NOT kill control flow without setting
6611 * the block to dead of bad things can happen:
6612 * We get a Block that is not reachable be irg_block_walk()
6613 * but can be found by irg_walk()!
6615 set_Block_dead(block);
6616 return get_irg_bad(irg);
6622 /* Blocks, Phis and Tuples may have dead inputs, e.g., if one of the
6623 blocks predecessors is dead. */
6624 if (op != op_Block && op != op_Phi && op != op_Tuple && op != op_Anchor) {
6625 ir_graph *irg = get_irn_irg(node);
6626 irn_arity = get_irn_arity(node);
6629 * Beware: we can only read the block of a non-floating node.
6631 if (is_irn_pinned_in_irg(node) &&
6632 is_Block_dead(get_nodes_block(skip_Proj(node))))
6633 return get_irg_bad(irg);
6635 for (i = 0; i < irn_arity; i++) {
6636 ir_node *pred = get_irn_n(node, i);
6639 return get_irg_bad(irg);
6641 /* Propagating Unknowns here seems to be a bad idea, because
6642 sometimes we need a node as a input and did not want that
6644 However, it might be useful to move this into a later phase
6645 (if you think that optimizing such code is useful). */
6646 if (is_Unknown(pred) && mode_is_data(get_irn_mode(node)))
6647 return new_r_Unknown(irg, get_irn_mode(node));
6652 /* With this code we violate the agreement that local_optimize
6653 only leaves Bads in Block, Phi and Tuple nodes. */
6654 /* If Block has only Bads as predecessors it's garbage. */
6655 /* If Phi has only Bads as predecessors it's garbage. */
6656 if ((op == op_Block && get_Block_matured(node)) || op == op_Phi) {
6657 irn_arity = get_irn_arity(node);
6658 for (i = 0; i < irn_arity; i++) {
6659 if (!is_Bad(get_irn_n(node, i))) break;
6661 if (i == irn_arity) node = get_irg_bad(irg);
6668 * These optimizations deallocate nodes from the obstack.
6669 * It can only be called if it is guaranteed that no other nodes
6670 * reference this one, i.e., right after construction of a node.
6672 * @param n The node to optimize
6674 ir_node *optimize_node(ir_node *n)
6677 ir_graph *irg = get_irn_irg(n);
6678 unsigned iro = get_irn_opcode(n);
6681 /* Always optimize Phi nodes: part of the construction. */
6682 if ((!get_opt_optimize()) && (iro != iro_Phi)) return n;
6684 /* constant expression evaluation / constant folding */
6685 if (get_opt_constant_folding()) {
6686 /* neither constants nor Tuple values can be evaluated */
6687 if (iro != iro_Const && (get_irn_mode(n) != mode_T)) {
6688 /* try to evaluate */
6689 tv = computed_value(n);
6690 if (tv != tarval_bad) {
6695 * we MUST copy the node here temporary, because it's still
6696 * needed for DBG_OPT_CSTEVAL
6698 node_size = offsetof(ir_node, attr) + n->op->attr_size;
6699 oldn = (ir_node*)alloca(node_size);
6701 memcpy(oldn, n, node_size);
6702 CLONE_ARR_A(ir_node *, oldn->in, n->in);
6704 /* ARG, copy the in array, we need it for statistics */
6705 memcpy(oldn->in, n->in, ARR_LEN(n->in) * sizeof(n->in[0]));
6707 /* note the inplace edges module */
6708 edges_node_deleted(n, irg);
6710 /* evaluation was successful -- replace the node. */
6711 irg_kill_node(irg, n);
6712 nw = new_r_Const(irg, tv);
6714 DBG_OPT_CSTEVAL(oldn, nw);
6720 /* remove unnecessary nodes */
6721 if (get_opt_algebraic_simplification() ||
6722 (iro == iro_Phi) || /* always optimize these nodes. */
6724 (iro == iro_Proj) ||
6725 (iro == iro_Block) ) /* Flags tested local. */
6726 n = equivalent_node(n);
6728 /* Common Subexpression Elimination.
6730 * Checks whether n is already available.
6731 * The block input is used to distinguish different subexpressions. Right
6732 * now all nodes are op_pin_state_pinned to blocks, i.e., the CSE only finds common
6733 * subexpressions within a block.
6736 n = identify_cons(n);
6739 edges_node_deleted(oldn, irg);
6741 /* We found an existing, better node, so we can deallocate the old node. */
6742 irg_kill_node(irg, oldn);
6746 /* Some more constant expression evaluation that does not allow to
6748 iro = get_irn_opcode(n);
6749 if (get_opt_algebraic_simplification() ||
6750 (iro == iro_Cond) ||
6751 (iro == iro_Proj)) /* Flags tested local. */
6752 n = transform_node(n);
6754 /* Remove nodes with dead (Bad) input.
6755 Run always for transformation induced Bads. */
6758 /* Now we have a legal, useful node. Enter it in hash table for CSE */
6759 if (get_opt_cse() && (get_irn_opcode(n) != iro_Block)) {
6761 n = identify_remember(o);
6767 } /* optimize_node */
6771 * These optimizations never deallocate nodes (in place). This can cause dead
6772 * nodes lying on the obstack. Remove these by a dead node elimination,
6773 * i.e., a copying garbage collection.
6775 ir_node *optimize_in_place_2(ir_node *n)
6779 unsigned iro = get_irn_opcode(n);
6781 if (!get_opt_optimize() && !is_Phi(n)) return n;
6783 if (iro == iro_Deleted)
6786 /* constant expression evaluation / constant folding */
6787 if (get_opt_constant_folding()) {
6788 /* neither constants nor Tuple values can be evaluated */
6789 if (iro != iro_Const && get_irn_mode(n) != mode_T) {
6790 /* try to evaluate */
6791 tv = computed_value(n);
6792 if (tv != tarval_bad) {
6793 /* evaluation was successful -- replace the node. */
6794 ir_graph *irg = get_irn_irg(n);
6796 n = new_r_Const(irg, tv);
6798 DBG_OPT_CSTEVAL(oldn, n);
6804 /* remove unnecessary nodes */
6805 if (get_opt_constant_folding() ||
6806 (iro == iro_Phi) || /* always optimize these nodes. */
6807 (iro == iro_Id) || /* ... */
6808 (iro == iro_Proj) || /* ... */
6809 (iro == iro_Block) ) /* Flags tested local. */
6810 n = equivalent_node(n);
6812 /** common subexpression elimination **/
6813 /* Checks whether n is already available. */
6814 /* The block input is used to distinguish different subexpressions. Right
6815 now all nodes are op_pin_state_pinned to blocks, i.e., the cse only finds common
6816 subexpressions within a block. */
6817 if (get_opt_cse()) {
6819 n = identify_remember(o);
6824 /* Some more constant expression evaluation. */
6825 iro = get_irn_opcode(n);
6826 if (get_opt_constant_folding() ||
6827 (iro == iro_Cond) ||
6828 (iro == iro_Proj)) /* Flags tested local. */
6829 n = transform_node(n);
6831 /* Remove nodes with dead (Bad) input.
6832 Run always for transformation induced Bads. */
6835 /* Now we can verify the node, as it has no dead inputs any more. */
6838 /* Now we have a legal, useful node. Enter it in hash table for cse.
6839 Blocks should be unique anyways. (Except the successor of start:
6840 is cse with the start block!) */
6841 if (get_opt_cse() && (get_irn_opcode(n) != iro_Block)) {
6843 n = identify_remember(o);
6849 } /* optimize_in_place_2 */
6852 * Wrapper for external use, set proper status bits after optimization.
6854 ir_node *optimize_in_place(ir_node *n)
6856 ir_graph *irg = get_irn_irg(n);
6857 /* Handle graph state */
6858 assert(get_irg_phase_state(irg) != phase_building);
6860 if (get_opt_global_cse())
6861 set_irg_pinned(irg, op_pin_state_floats);
6862 if (get_irg_outs_state(irg) == outs_consistent)
6863 set_irg_outs_inconsistent(irg);
6865 /* FIXME: Maybe we could also test whether optimizing the node can
6866 change the control graph. */
6867 set_irg_doms_inconsistent(irg);
6868 return optimize_in_place_2(n);
6869 } /* optimize_in_place */
6872 * Calculate a hash value of a Const node.
6874 static unsigned hash_Const(const ir_node *node)
6878 /* special value for const, as they only differ in their tarval. */
6879 h = HASH_PTR(node->attr.con.tarval);
6885 * Calculate a hash value of a SymConst node.
6887 static unsigned hash_SymConst(const ir_node *node)
6891 /* all others are pointers */
6892 h = HASH_PTR(node->attr.symc.sym.type_p);
6895 } /* hash_SymConst */
6898 * Set the default hash operation in an ir_op_ops.
6900 * @param code the opcode for the default operation
6901 * @param ops the operations initialized
6906 static ir_op_ops *firm_set_default_hash(ir_opcode code, ir_op_ops *ops)
6910 ops->hash = hash_##a; \
6913 /* hash function already set */
6914 if (ops->hash != NULL)
6921 /* use input/mode default hash if no function was given */
6922 ops->hash = firm_default_hash;
6930 * Sets the default operation for an ir_ops.
6932 ir_op_ops *firm_set_default_operations(ir_opcode code, ir_op_ops *ops)
6934 ops = firm_set_default_hash(code, ops);
6935 ops = firm_set_default_computed_value(code, ops);
6936 ops = firm_set_default_equivalent_node(code, ops);
6937 ops = firm_set_default_transform_node(code, ops);
6938 ops = firm_set_default_node_cmp_attr(code, ops);
6939 ops = firm_set_default_get_type_attr(code, ops);
6940 ops = firm_set_default_get_entity_attr(code, ops);
6943 } /* firm_set_default_operations */