2 * Copyright (C) 1995-2008 University of Karlsruhe. All right reserved.
4 * This file is part of libFirm.
6 * This file may be distributed and/or modified under the terms of the
7 * GNU General Public License version 2 as published by the Free Software
8 * Foundation and appearing in the file LICENSE.GPL included in the
9 * packaging of this file.
11 * Licensees holding valid libFirm Professional Edition licenses may use
12 * this file in accordance with the libFirm Commercial License.
13 * Agreement provided with the Software.
15 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * @brief iropt --- optimizations intertwined with IR construction.
23 * @author Christian Schaefer, Goetz Lindenmaier, Michael Beck
32 #include "irgraph_t.h"
33 #include "iredges_t.h"
40 #include "dbginfo_t.h"
41 #include "iropt_dbg.h"
46 #include "opt_confirms.h"
47 #include "opt_polymorphy.h"
52 #include "firm_types.h"
53 #include "bitfiddle.h"
56 /* Make types visible to allow most efficient access */
60 * Returns the tarval of a Const node or tarval_bad for all other nodes.
62 static tarval *default_value_of(const ir_node *n)
65 return get_Const_tarval(n); /* might return tarval_bad */
70 value_of_func value_of_ptr = default_value_of;
72 /* * Set a new value_of function. */
73 void set_value_of_func(value_of_func func)
78 value_of_ptr = default_value_of;
82 * Return the value of a Constant.
84 static tarval *computed_value_Const(const ir_node *n)
86 return get_Const_tarval(n);
87 } /* computed_value_Const */
90 * Return the value of a 'sizeof', 'alignof' or 'offsetof' SymConst.
92 static tarval *computed_value_SymConst(const ir_node *n)
97 switch (get_SymConst_kind(n)) {
98 case symconst_type_size:
99 type = get_SymConst_type(n);
100 if (get_type_state(type) == layout_fixed)
101 return new_tarval_from_long(get_type_size_bytes(type), get_irn_mode(n));
103 case symconst_type_align:
104 type = get_SymConst_type(n);
105 if (get_type_state(type) == layout_fixed)
106 return new_tarval_from_long(get_type_alignment_bytes(type), get_irn_mode(n));
108 case symconst_ofs_ent:
109 ent = get_SymConst_entity(n);
110 type = get_entity_owner(ent);
111 if (get_type_state(type) == layout_fixed)
112 return new_tarval_from_long(get_entity_offset(ent), get_irn_mode(n));
118 } /* computed_value_SymConst */
121 * Return the value of an Add.
123 static tarval *computed_value_Add(const ir_node *n)
125 ir_node *a = get_Add_left(n);
126 ir_node *b = get_Add_right(n);
128 tarval *ta = value_of(a);
129 tarval *tb = value_of(b);
131 if ((ta != tarval_bad) && (tb != tarval_bad))
132 return tarval_add(ta, tb);
135 } /* computed_value_Add */
138 * Return the value of a Sub.
139 * Special case: a - a
141 static tarval *computed_value_Sub(const ir_node *n)
143 ir_mode *mode = get_irn_mode(n);
144 ir_node *a = get_Sub_left(n);
145 ir_node *b = get_Sub_right(n);
150 if (! mode_is_float(mode)) {
153 return get_mode_null(mode);
159 if ((ta != tarval_bad) && (tb != tarval_bad))
160 return tarval_sub(ta, tb, mode);
163 } /* computed_value_Sub */
166 * Return the value of a Carry.
167 * Special : a op 0, 0 op b
169 static tarval *computed_value_Carry(const ir_node *n)
171 ir_node *a = get_binop_left(n);
172 ir_node *b = get_binop_right(n);
173 ir_mode *m = get_irn_mode(n);
175 tarval *ta = value_of(a);
176 tarval *tb = value_of(b);
178 if ((ta != tarval_bad) && (tb != tarval_bad)) {
180 return tarval_carry() ? get_mode_one(m) : get_mode_null(m);
182 if (tarval_is_null(ta) || tarval_is_null(tb))
183 return get_mode_null(m);
186 } /* computed_value_Carry */
189 * Return the value of a Borrow.
192 static tarval *computed_value_Borrow(const ir_node *n)
194 ir_node *a = get_binop_left(n);
195 ir_node *b = get_binop_right(n);
196 ir_mode *m = get_irn_mode(n);
198 tarval *ta = value_of(a);
199 tarval *tb = value_of(b);
201 if ((ta != tarval_bad) && (tb != tarval_bad)) {
202 return tarval_cmp(ta, tb) == pn_Cmp_Lt ? get_mode_one(m) : get_mode_null(m);
203 } else if (tarval_is_null(ta)) {
204 return get_mode_null(m);
207 } /* computed_value_Borrow */
210 * Return the value of an unary Minus.
212 static tarval *computed_value_Minus(const ir_node *n)
214 ir_node *a = get_Minus_op(n);
215 tarval *ta = value_of(a);
217 if (ta != tarval_bad)
218 return tarval_neg(ta);
221 } /* computed_value_Minus */
224 * Return the value of a Mul.
226 static tarval *computed_value_Mul(const ir_node *n)
228 ir_node *a = get_Mul_left(n);
229 ir_node *b = get_Mul_right(n);
232 tarval *ta = value_of(a);
233 tarval *tb = value_of(b);
235 mode = get_irn_mode(n);
236 if (mode != get_irn_mode(a)) {
237 /* n * n = 2n bit multiplication */
238 ta = tarval_convert_to(ta, mode);
239 tb = tarval_convert_to(tb, mode);
242 if (ta != tarval_bad && tb != tarval_bad) {
243 return tarval_mul(ta, tb);
245 /* a * 0 != 0 if a == NaN or a == Inf */
246 if (!mode_is_float(mode)) {
247 /* a*0 = 0 or 0*b = 0 */
248 if (ta == get_mode_null(mode))
250 if (tb == get_mode_null(mode))
255 } /* computed_value_Mul */
258 * Return the value of an And.
259 * Special case: a & 0, 0 & b
261 static tarval *computed_value_And(const ir_node *n)
263 ir_node *a = get_And_left(n);
264 ir_node *b = get_And_right(n);
266 tarval *ta = value_of(a);
267 tarval *tb = value_of(b);
269 if ((ta != tarval_bad) && (tb != tarval_bad)) {
270 return tarval_and (ta, tb);
272 if (tarval_is_null(ta)) return ta;
273 if (tarval_is_null(tb)) return tb;
276 } /* computed_value_And */
279 * Return the value of an Or.
280 * Special case: a | 1...1, 1...1 | b
282 static tarval *computed_value_Or(const ir_node *n)
284 ir_node *a = get_Or_left(n);
285 ir_node *b = get_Or_right(n);
287 tarval *ta = value_of(a);
288 tarval *tb = value_of(b);
290 if ((ta != tarval_bad) && (tb != tarval_bad)) {
291 return tarval_or (ta, tb);
293 if (tarval_is_all_one(ta)) return ta;
294 if (tarval_is_all_one(tb)) return tb;
297 } /* computed_value_Or */
300 * Return the value of an Eor.
302 static tarval *computed_value_Eor(const ir_node *n)
304 ir_node *a = get_Eor_left(n);
305 ir_node *b = get_Eor_right(n);
310 return get_mode_null(get_irn_mode(n));
315 if ((ta != tarval_bad) && (tb != tarval_bad)) {
316 return tarval_eor(ta, tb);
319 } /* computed_value_Eor */
322 * Return the value of a Not.
324 static tarval *computed_value_Not(const ir_node *n)
326 ir_node *a = get_Not_op(n);
327 tarval *ta = value_of(a);
329 if (ta != tarval_bad)
330 return tarval_not(ta);
333 } /* computed_value_Not */
336 * Return the value of a Shl.
338 static tarval *computed_value_Shl(const ir_node *n)
340 ir_node *a = get_Shl_left(n);
341 ir_node *b = get_Shl_right(n);
343 tarval *ta = value_of(a);
344 tarval *tb = value_of(b);
346 if ((ta != tarval_bad) && (tb != tarval_bad)) {
347 return tarval_shl(ta, tb);
350 } /* computed_value_Shl */
353 * Return the value of a Shr.
355 static tarval *computed_value_Shr(const ir_node *n)
357 ir_node *a = get_Shr_left(n);
358 ir_node *b = get_Shr_right(n);
360 tarval *ta = value_of(a);
361 tarval *tb = value_of(b);
363 if ((ta != tarval_bad) && (tb != tarval_bad)) {
364 return tarval_shr(ta, tb);
367 } /* computed_value_Shr */
370 * Return the value of a Shrs.
372 static tarval *computed_value_Shrs(const ir_node *n)
374 ir_node *a = get_Shrs_left(n);
375 ir_node *b = get_Shrs_right(n);
377 tarval *ta = value_of(a);
378 tarval *tb = value_of(b);
380 if ((ta != tarval_bad) && (tb != tarval_bad)) {
381 return tarval_shrs(ta, tb);
384 } /* computed_value_Shrs */
387 * Return the value of a Rotl.
389 static tarval *computed_value_Rotl(const ir_node *n)
391 ir_node *a = get_Rotl_left(n);
392 ir_node *b = get_Rotl_right(n);
394 tarval *ta = value_of(a);
395 tarval *tb = value_of(b);
397 if ((ta != tarval_bad) && (tb != tarval_bad)) {
398 return tarval_rotl(ta, tb);
401 } /* computed_value_Rotl */
404 * Return the value of a Conv.
406 static tarval *computed_value_Conv(const ir_node *n)
408 ir_node *a = get_Conv_op(n);
409 tarval *ta = value_of(a);
411 if (ta != tarval_bad)
412 return tarval_convert_to(ta, get_irn_mode(n));
415 } /* computed_value_Conv */
418 * Calculate the value of a Mux: can be evaluated, if the
419 * sel and the right input are known.
421 static tarval *computed_value_Mux(const ir_node *n)
423 ir_node *sel = get_Mux_sel(n);
424 tarval *ts = value_of(sel);
426 if (ts == get_tarval_b_true()) {
427 ir_node *v = get_Mux_true(n);
430 else if (ts == get_tarval_b_false()) {
431 ir_node *v = get_Mux_false(n);
435 } /* computed_value_Mux */
438 * Calculate the value of a Confirm: can be evaluated,
439 * if it has the form Confirm(x, '=', Const).
441 static tarval *computed_value_Confirm(const ir_node *n)
444 * Beware: we might produce Phi(Confirm(x == true), Confirm(x == false)).
445 * Do NOT optimize them away (jump threading wants them), so wait until
446 * remove_confirm is activated.
448 if (get_opt_remove_confirm()) {
449 if (get_Confirm_cmp(n) == pn_Cmp_Eq) {
450 tarval *tv = value_of(get_Confirm_bound(n));
451 if (tv != tarval_bad)
455 return value_of(get_Confirm_value(n));
456 } /* computed_value_Confirm */
459 * Return the value of a Proj(Cmp).
461 * This performs a first step of unreachable code elimination.
462 * Proj can not be computed, but folding a Cmp above the Proj here is
463 * not as wasteful as folding a Cmp into a Tuple of 16 Consts of which
465 * There are several case where we can evaluate a Cmp node, see later.
467 static tarval *computed_value_Proj_Cmp(const ir_node *n)
469 ir_node *cmp = get_Proj_pred(n);
470 ir_node *left = get_Cmp_left(cmp);
471 ir_node *right = get_Cmp_right(cmp);
472 long pn_cmp = get_Proj_proj(n);
473 ir_mode *mode = get_irn_mode(left);
477 * BEWARE: a == a is NOT always True for floating Point values, as
478 * NaN != NaN is defined, so we must check this here.
480 if (left == right && (!mode_is_float(mode) || pn_cmp == pn_Cmp_Lt || pn_cmp == pn_Cmp_Gt)) {
481 /* This is a trick with the bits used for encoding the Cmp
482 Proj numbers, the following statement is not the same:
483 return new_tarval_from_long(pn_cmp == pn_Cmp_Eq, mode_b) */
484 return new_tarval_from_long(pn_cmp & pn_Cmp_Eq, mode_b);
486 tv_l = value_of(left);
487 tv_r = value_of(right);
489 if ((tv_l != tarval_bad) && (tv_r != tarval_bad)) {
491 * The predecessors of Cmp are target values. We can evaluate
494 pn_Cmp flags = tarval_cmp(tv_l, tv_r);
495 if (flags != pn_Cmp_False) {
496 return new_tarval_from_long (pn_cmp & flags, mode_b);
498 } else if (mode_is_int(mode)) {
499 /* for integer values, we can check against MIN/MAX */
502 if (tv_l == get_mode_min(mode)) {
503 /* MIN <=/> x. This results in true/false. */
504 if (pn_cmp == pn_Cmp_Le)
505 return tarval_b_true;
506 else if (pn_cmp == pn_Cmp_Gt)
507 return tarval_b_false;
508 } else if (tv_r == get_mode_min(mode)) {
509 /* x >=/< MIN. This results in true/false. */
510 if (pn_cmp == pn_Cmp_Ge)
511 return tarval_b_true;
512 else if (pn_cmp == pn_Cmp_Lt)
513 return tarval_b_false;
514 } else if (tv_l == get_mode_max(mode)) {
515 /* MAX >=/< x. This results in true/false. */
516 if (pn_cmp == pn_Cmp_Ge)
517 return tarval_b_true;
518 else if (pn_cmp == pn_Cmp_Lt)
519 return tarval_b_false;
520 } else if (tv_r == get_mode_max(mode)) {
521 /* x <=/> MAX. This results in true/false. */
522 if (pn_cmp == pn_Cmp_Le)
523 return tarval_b_true;
524 else if (pn_cmp == pn_Cmp_Gt)
525 return tarval_b_false;
528 cmp_result = vrp_cmp(left, right);
529 if (cmp_result != pn_Cmp_False) {
530 if (cmp_result == pn_Cmp_Lg) {
531 if (pn_cmp == pn_Cmp_Eq) {
532 return tarval_b_false;
533 } else if (pn_cmp == pn_Cmp_Lg) {
534 return tarval_b_true;
537 return new_tarval_from_long(cmp_result & pn_cmp, mode_b);
540 } else if (mode_is_reference(mode)) {
541 /* pointer compare */
542 ir_node *s_l = skip_Proj(left);
543 ir_node *s_r = skip_Proj(right);
545 if ((is_Alloc(s_l) && tarval_is_null(tv_r)) ||
546 (tarval_is_null(tv_l) && is_Alloc(s_r))) {
548 * The predecessors are Allocs and (void*)(0) constants. In Firm Allocs never
549 * return NULL, they raise an exception. Therefore we can predict
552 return new_tarval_from_long(pn_cmp & pn_Cmp_Lg, mode_b);
555 return computed_value_Cmp_Confirm(cmp, left, right, pn_cmp);
556 } /* computed_value_Proj_Cmp */
559 * Return the value of a floating point Quot.
561 static tarval *do_computed_value_Quot(const ir_node *a, const ir_node *b)
563 tarval *ta = value_of(a);
564 tarval *tb = value_of(b);
566 /* cannot optimize 0 / b = 0 because of NaN */
567 if (ta != tarval_bad && tb != tarval_bad)
568 return tarval_quo(ta, tb);
570 } /* do_computed_value_Quot */
573 * Calculate the value of an integer Div of two nodes.
574 * Special case: 0 / b
576 static tarval *do_computed_value_Div(const ir_node *a, const ir_node *b)
578 tarval *ta = value_of(a);
580 const ir_node *dummy;
582 /* Compute c1 / c2 or 0 / a, a != 0 */
583 if (tarval_is_null(ta) && value_not_zero(b, &dummy))
584 return ta; /* 0 / b == 0 */
586 if (ta != tarval_bad && tb != tarval_bad)
587 return tarval_div(ta, tb);
589 } /* do_computed_value_Div */
592 * Calculate the value of an integer Mod of two nodes.
593 * Special case: a % 1
595 static tarval *do_computed_value_Mod(const ir_node *a, const ir_node *b)
597 tarval *ta = value_of(a);
598 tarval *tb = value_of(b);
600 /* Compute a % 1 or c1 % c2 */
601 if (tarval_is_one(tb))
602 return get_mode_null(get_irn_mode(a));
603 if (ta != tarval_bad && tb != tarval_bad)
604 return tarval_mod(ta, tb);
606 } /* do_computed_value_Mod */
609 * Return the value of a Proj(DivMod).
611 static tarval *computed_value_Proj_DivMod(const ir_node *n)
613 long proj_nr = get_Proj_proj(n);
615 /* compute either the Div or the Mod part */
616 if (proj_nr == pn_DivMod_res_div) {
617 const ir_node *a = get_Proj_pred(n);
618 return do_computed_value_Div(get_DivMod_left(a), get_DivMod_right(a));
619 } else if (proj_nr == pn_DivMod_res_mod) {
620 const ir_node *a = get_Proj_pred(n);
621 return do_computed_value_Mod(get_DivMod_left(a), get_DivMod_right(a));
624 } /* computed_value_Proj_DivMod */
627 * Return the value of a Proj(Div).
629 static tarval *computed_value_Proj_Div(const ir_node *n)
631 long proj_nr = get_Proj_proj(n);
633 if (proj_nr == pn_Div_res) {
634 const ir_node *a = get_Proj_pred(n);
635 return do_computed_value_Div(get_Div_left(a), get_Div_right(a));
638 } /* computed_value_Proj_Div */
641 * Return the value of a Proj(Mod).
643 static tarval *computed_value_Proj_Mod(const ir_node *n)
645 long proj_nr = get_Proj_proj(n);
647 if (proj_nr == pn_Mod_res) {
648 const ir_node *a = get_Proj_pred(n);
649 return do_computed_value_Mod(get_Mod_left(a), get_Mod_right(a));
652 } /* computed_value_Proj_Mod */
655 * Return the value of a Proj(Quot).
657 static tarval *computed_value_Proj_Quot(const ir_node *n)
659 long proj_nr = get_Proj_proj(n);
661 if (proj_nr == pn_Quot_res) {
662 const ir_node *a = get_Proj_pred(n);
663 return do_computed_value_Quot(get_Quot_left(a), get_Quot_right(a));
666 } /* computed_value_Proj_Quot */
669 * Return the value of a Proj.
671 static tarval *computed_value_Proj(const ir_node *proj)
673 ir_node *n = get_Proj_pred(proj);
675 if (n->op->ops.computed_value_Proj != NULL)
676 return n->op->ops.computed_value_Proj(proj);
678 } /* computed_value_Proj */
681 * If the parameter n can be computed, return its value, else tarval_bad.
682 * Performs constant folding.
684 * @param n The node this should be evaluated
686 tarval *computed_value(const ir_node *n)
688 vrp_attr *vrp = vrp_get_info(n);
689 if (vrp && vrp->valid && tarval_cmp(vrp->bits_set, vrp->bits_not_set) == pn_Cmp_Eq) {
690 return vrp->bits_set;
692 if (n->op->ops.computed_value)
693 return n->op->ops.computed_value(n);
695 } /* computed_value */
698 * Set the default computed_value evaluator in an ir_op_ops.
700 * @param code the opcode for the default operation
701 * @param ops the operations initialized
706 static ir_op_ops *firm_set_default_computed_value(ir_opcode code, ir_op_ops *ops)
710 ops->computed_value = computed_value_##a; \
712 #define CASE_PROJ(a) \
714 ops->computed_value_Proj = computed_value_Proj_##a; \
751 } /* firm_set_default_computed_value */
754 * Returns a equivalent block for another block.
755 * If the block has only one predecessor, this is
756 * the equivalent one. If the only predecessor of a block is
757 * the block itself, this is a dead block.
759 * If both predecessors of a block are the branches of a binary
760 * Cond, the equivalent block is Cond's block.
762 * If all predecessors of a block are bad or lies in a dead
763 * block, the current block is dead as well.
765 * Note, that blocks are NEVER turned into Bad's, instead
766 * the dead_block flag is set. So, never test for is_Bad(block),
767 * always use is_dead_Block(block).
769 static ir_node *equivalent_node_Block(ir_node *n)
775 /* don't optimize dead or labeled blocks */
776 if (is_Block_dead(n) || has_Block_entity(n))
779 n_preds = get_Block_n_cfgpreds(n);
781 /* The Block constructor does not call optimize, but mature_immBlock()
782 calls the optimization. */
783 assert(get_Block_matured(n));
785 irg = get_irn_irg(n);
787 /* Straightening: a single entry Block following a single exit Block
788 can be merged, if it is not the Start block. */
789 /* !!! Beware, all Phi-nodes of n must have been optimized away.
790 This should be true, as the block is matured before optimize is called.
791 But what about Phi-cycles with the Phi0/Id that could not be resolved?
792 Remaining Phi nodes are just Ids. */
794 ir_node *pred = skip_Proj(get_Block_cfgpred(n, 0));
797 ir_node *predblock = get_nodes_block(pred);
798 if (predblock == oldn) {
799 /* Jmp jumps into the block it is in -- deal self cycle. */
800 n = set_Block_dead(n);
801 DBG_OPT_DEAD_BLOCK(oldn, n);
802 } else if (get_opt_control_flow_straightening()) {
804 DBG_OPT_STG(oldn, n);
806 } else if (is_Cond(pred)) {
807 ir_node *predblock = get_nodes_block(pred);
808 if (predblock == oldn) {
809 /* Jmp jumps into the block it is in -- deal self cycle. */
810 n = set_Block_dead(n);
811 DBG_OPT_DEAD_BLOCK(oldn, n);
814 } else if ((n_preds == 2) &&
815 (get_opt_control_flow_weak_simplification())) {
816 /* Test whether Cond jumps twice to this block
817 * The more general case which more than 2 predecessors is handles
818 * in optimize_cf(), we handle only this special case for speed here.
820 ir_node *a = get_Block_cfgpred(n, 0);
821 ir_node *b = get_Block_cfgpred(n, 1);
823 if (is_Proj(a) && is_Proj(b)) {
824 ir_node *cond = get_Proj_pred(a);
826 if (cond == get_Proj_pred(b) && is_Cond(cond) &&
827 get_irn_mode(get_Cond_selector(cond)) == mode_b) {
828 /* Also a single entry Block following a single exit Block. Phis have
829 twice the same operand and will be optimized away. */
830 n = get_nodes_block(cond);
831 DBG_OPT_IFSIM1(oldn, a, b, n);
834 } else if (get_opt_unreachable_code() &&
835 (n != get_irg_start_block(irg)) &&
836 (n != get_irg_end_block(irg))) {
839 /* If all inputs are dead, this block is dead too, except if it is
840 the start or end block. This is one step of unreachable code
842 for (i = get_Block_n_cfgpreds(n) - 1; i >= 0; --i) {
843 ir_node *pred = get_Block_cfgpred(n, i);
846 if (is_Bad(pred)) continue;
847 pred_blk = get_nodes_block(skip_Proj(pred));
849 if (is_Block_dead(pred_blk)) continue;
852 /* really found a living input */
857 n = set_Block_dead(n);
858 DBG_OPT_DEAD_BLOCK(oldn, n);
863 } /* equivalent_node_Block */
866 * Returns a equivalent node for a Jmp, a Bad :-)
867 * Of course this only happens if the Block of the Jmp is dead.
869 static ir_node *equivalent_node_Jmp(ir_node *n)
873 /* unreachable code elimination */
874 if (is_Block_dead(get_nodes_block(n))) {
875 ir_graph *irg = get_irn_irg(n);
876 n = get_irg_bad(irg);
877 DBG_OPT_DEAD_BLOCK(oldn, n);
880 } /* equivalent_node_Jmp */
882 /** Raise is handled in the same way as Jmp. */
883 #define equivalent_node_Raise equivalent_node_Jmp
886 /* We do not evaluate Cond here as we replace it by a new node, a Jmp.
887 See transform_node_Proj_Cond(). */
890 * Optimize operations that are commutative and have neutral 0,
891 * so a op 0 = 0 op a = a.
893 static ir_node *equivalent_node_neutral_zero(ir_node *n)
897 ir_node *a = get_binop_left(n);
898 ir_node *b = get_binop_right(n);
903 /* After running compute_node there is only one constant predecessor.
904 Find this predecessors value and remember the other node: */
905 if ((tv = value_of(a)) != tarval_bad) {
907 } else if ((tv = value_of(b)) != tarval_bad) {
912 /* If this predecessors constant value is zero, the operation is
913 * unnecessary. Remove it.
915 * Beware: If n is a Add, the mode of on and n might be different
916 * which happens in this rare construction: NULL + 3.
917 * Then, a Conv would be needed which we cannot include here.
919 if (tarval_is_null(tv) && get_irn_mode(on) == get_irn_mode(n)) {
922 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_0);
926 } /* equivalent_node_neutral_zero */
929 * Eor is commutative and has neutral 0.
931 static ir_node *equivalent_node_Eor(ir_node *n)
937 n = equivalent_node_neutral_zero(n);
938 if (n != oldn) return n;
941 b = get_Eor_right(n);
944 ir_node *aa = get_Eor_left(a);
945 ir_node *ab = get_Eor_right(a);
948 /* (a ^ b) ^ a -> b */
950 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_EOR_A_B_A);
952 } else if (ab == b) {
953 /* (a ^ b) ^ b -> a */
955 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_EOR_A_B_A);
960 ir_node *ba = get_Eor_left(b);
961 ir_node *bb = get_Eor_right(b);
964 /* a ^ (a ^ b) -> b */
966 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_EOR_A_B_A);
968 } else if (bb == a) {
969 /* a ^ (b ^ a) -> b */
971 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_EOR_A_B_A);
979 * Optimize a - 0 and (a - x) + x (for modes with wrap-around).
981 * The second one looks strange, but this construct
982 * is used heavily in the LCC sources :-).
984 * Beware: The Mode of an Add may be different than the mode of its
985 * predecessors, so we could not return a predecessors in all cases.
987 static ir_node *equivalent_node_Add(ir_node *n)
990 ir_node *left, *right;
991 ir_mode *mode = get_irn_mode(n);
993 n = equivalent_node_neutral_zero(n);
997 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
998 if (mode_is_float(mode)) {
999 ir_graph *irg = get_irn_irg(n);
1000 if (get_irg_fp_model(irg) & fp_strict_algebraic)
1004 left = get_Add_left(n);
1005 right = get_Add_right(n);
1008 if (get_Sub_right(left) == right) {
1011 n = get_Sub_left(left);
1012 if (mode == get_irn_mode(n)) {
1013 DBG_OPT_ALGSIM1(oldn, left, right, n, FS_OPT_ADD_SUB);
1018 if (is_Sub(right)) {
1019 if (get_Sub_right(right) == left) {
1022 n = get_Sub_left(right);
1023 if (mode == get_irn_mode(n)) {
1024 DBG_OPT_ALGSIM1(oldn, left, right, n, FS_OPT_ADD_SUB);
1030 } /* equivalent_node_Add */
1033 * optimize operations that are not commutative but have neutral 0 on left,
1036 static ir_node *equivalent_node_left_zero(ir_node *n)
1040 ir_node *a = get_binop_left(n);
1041 ir_node *b = get_binop_right(n);
1042 tarval *tb = value_of(b);
1044 if (tarval_is_null(tb)) {
1047 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_0);
1050 } /* equivalent_node_left_zero */
1052 #define equivalent_node_Shl equivalent_node_left_zero
1053 #define equivalent_node_Shr equivalent_node_left_zero
1054 #define equivalent_node_Shrs equivalent_node_left_zero
1055 #define equivalent_node_Rotl equivalent_node_left_zero
1058 * Optimize a - 0 and (a + x) - x (for modes with wrap-around).
1060 * The second one looks strange, but this construct
1061 * is used heavily in the LCC sources :-).
1063 * Beware: The Mode of a Sub may be different than the mode of its
1064 * predecessors, so we could not return a predecessors in all cases.
1066 static ir_node *equivalent_node_Sub(ir_node *n)
1070 ir_mode *mode = get_irn_mode(n);
1073 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
1074 if (mode_is_float(mode)) {
1075 ir_graph *irg = get_irn_irg(n);
1076 if (get_irg_fp_model(irg) & fp_strict_algebraic)
1080 b = get_Sub_right(n);
1083 /* Beware: modes might be different */
1084 if (tarval_is_null(tb)) {
1085 ir_node *a = get_Sub_left(n);
1086 if (mode == get_irn_mode(a)) {
1089 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_0);
1093 } /* equivalent_node_Sub */
1097 * Optimize an "self-inverse unary op", ie op(op(n)) = n.
1100 * -(-a) == a, but might overflow two times.
1101 * We handle it anyway here but the better way would be a
1102 * flag. This would be needed for Pascal for instance.
1104 static ir_node *equivalent_node_idempotent_unop(ir_node *n)
1107 ir_node *pred = get_unop_op(n);
1109 /* optimize symmetric unop */
1110 if (get_irn_op(pred) == get_irn_op(n)) {
1111 n = get_unop_op(pred);
1112 DBG_OPT_ALGSIM2(oldn, pred, n, FS_OPT_IDEM_UNARY);
1115 } /* equivalent_node_idempotent_unop */
1117 /** Optimize Not(Not(x)) == x. */
1118 #define equivalent_node_Not equivalent_node_idempotent_unop
1120 /** -(-x) == x ??? Is this possible or can --x raise an
1121 out of bounds exception if min =! max? */
1122 #define equivalent_node_Minus equivalent_node_idempotent_unop
1125 * Optimize a * 1 = 1 * a = a.
1127 static ir_node *equivalent_node_Mul(ir_node *n)
1130 ir_node *a = get_Mul_left(n);
1132 /* we can handle here only the n * n = n bit cases */
1133 if (get_irn_mode(n) == get_irn_mode(a)) {
1134 ir_node *b = get_Mul_right(n);
1138 * Mul is commutative and has again an other neutral element.
1139 * Constants are place right, so check this case first.
1142 if (tarval_is_one(tv)) {
1144 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
1147 if (tarval_is_one(tv)) {
1149 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
1154 } /* equivalent_node_Mul */
1157 * Use algebraic simplification a | a = a | 0 = 0 | a = a.
1159 static ir_node *equivalent_node_Or(ir_node *n)
1163 ir_node *a = get_Or_left(n);
1164 ir_node *b = get_Or_right(n);
1168 n = a; /* Or has it's own neutral element */
1169 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_OR);
1172 /* constants are cormalized to right, check this site first */
1174 if (tarval_is_null(tv)) {
1176 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_OR);
1180 if (tarval_is_null(tv)) {
1182 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_OR);
1187 } /* equivalent_node_Or */
1190 * Optimize a & 0b1...1 = 0b1...1 & a = a & a = (a|X) & a = a.
1192 static ir_node *equivalent_node_And(ir_node *n)
1196 ir_node *a = get_And_left(n);
1197 ir_node *b = get_And_right(n);
1201 n = a; /* And has it's own neutral element */
1202 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_AND);
1205 /* constants are normalized to right, check this site first */
1207 if (tarval_is_all_one(tv)) {
1209 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1212 if (tv != get_tarval_bad()) {
1213 ir_mode *mode = get_irn_mode(n);
1214 if (!mode_is_signed(mode) && is_Conv(a)) {
1215 ir_node *convop = get_Conv_op(a);
1216 ir_mode *convopmode = get_irn_mode(convop);
1217 if (!mode_is_signed(convopmode)) {
1218 if (tarval_is_all_one(tarval_convert_to(tv, convopmode))) {
1219 /* Conv(X) & all_one(mode(X)) = Conv(X) */
1221 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1228 if (tarval_is_all_one(tv)) {
1230 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1234 if (b == get_Or_left(a) || b == get_Or_right(a)) {
1237 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1242 if (a == get_Or_left(b) || a == get_Or_right(b)) {
1245 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1250 } /* equivalent_node_And */
1253 * Try to remove useless Conv's:
1255 static ir_node *equivalent_node_Conv(ir_node *n)
1258 ir_node *a = get_Conv_op(n);
1260 ir_mode *n_mode = get_irn_mode(n);
1261 ir_mode *a_mode = get_irn_mode(a);
1264 if (n_mode == a_mode) { /* No Conv necessary */
1265 if (get_Conv_strict(n)) {
1268 /* neither Minus nor Confirm change the precision,
1269 so we can "look-through" */
1272 p = get_Minus_op(p);
1273 } else if (is_Confirm(p)) {
1274 p = get_Confirm_value(p);
1280 if (is_Conv(p) && get_Conv_strict(p)) {
1281 /* we known already, that a_mode == n_mode, and neither
1282 Minus change the mode, so the second Conv
1284 assert(get_irn_mode(p) == n_mode);
1286 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CONV);
1290 ir_node *pred = get_Proj_pred(p);
1291 if (is_Load(pred)) {
1292 /* Loads always return with the exact precision of n_mode */
1293 assert(get_Load_mode(pred) == n_mode);
1295 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CONV);
1298 if (is_Proj(pred) && get_Proj_proj(pred) == pn_Start_T_args) {
1299 pred = get_Proj_pred(pred);
1300 if (is_Start(pred)) {
1301 /* Arguments always return with the exact precision,
1302 as strictConv's are place before Call -- if the
1303 caller was compiled with the same setting.
1304 Otherwise, the semantics is probably still right. */
1305 assert(get_irn_mode(p) == n_mode);
1307 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CONV);
1313 /* special case: the immediate predecessor is also a Conv */
1314 if (! get_Conv_strict(a)) {
1315 /* first one is not strict, kick it */
1317 a_mode = get_irn_mode(a);
1321 /* else both are strict conv, second is superfluous */
1323 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CONV);
1328 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CONV);
1331 } else if (is_Conv(a)) { /* Conv(Conv(b)) */
1332 ir_node *b = get_Conv_op(a);
1333 ir_mode *b_mode = get_irn_mode(b);
1335 if (get_Conv_strict(n) && get_Conv_strict(a)) {
1336 /* both are strict conv */
1337 if (smaller_mode(a_mode, n_mode)) {
1338 /* both are strict, but the first is smaller, so
1339 the second cannot remove more precision, remove the
1341 set_Conv_strict(n, 0);
1344 if (n_mode == b_mode) {
1345 if (! get_Conv_strict(n) && ! get_Conv_strict(a)) {
1346 if (n_mode == mode_b) {
1347 n = b; /* Convb(Conv*(xxxb(...))) == xxxb(...) */
1348 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
1350 } else if (get_mode_arithmetic(n_mode) == get_mode_arithmetic(a_mode)) {
1351 if (values_in_mode(b_mode, a_mode)) {
1352 n = b; /* ConvS(ConvL(xxxS(...))) == xxxS(...) */
1353 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
1358 if (mode_is_int(n_mode) && get_mode_arithmetic(a_mode) == irma_ieee754) {
1359 /* ConvI(ConvF(I)) -> I, iff float mantissa >= int mode */
1360 unsigned int_mantissa = get_mode_size_bits(n_mode) - (mode_is_signed(n_mode) ? 1 : 0);
1361 unsigned float_mantissa = tarval_ieee754_get_mantissa_size(a_mode);
1363 if (float_mantissa >= int_mantissa) {
1365 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
1370 if (smaller_mode(b_mode, a_mode)) {
1371 if (get_Conv_strict(n))
1372 set_Conv_strict(b, 1);
1373 n = b; /* ConvA(ConvB(ConvA(...))) == ConvA(...) */
1374 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
1381 } /* equivalent_node_Conv */
1384 * A Cast may be removed if the type of the previous node
1385 * is already the type of the Cast.
1387 static ir_node *equivalent_node_Cast(ir_node *n)
1390 ir_node *pred = get_Cast_op(n);
1392 if (get_irn_type(pred) == get_Cast_type(n)) {
1394 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CAST);
1397 } /* equivalent_node_Cast */
1400 * - fold Phi-nodes, iff they have only one predecessor except
1403 static ir_node *equivalent_node_Phi(ir_node *n)
1409 ir_node *first_val = NULL; /* to shutup gcc */
1411 if (!get_opt_normalize()) return n;
1413 n_preds = get_Phi_n_preds(n);
1415 block = get_nodes_block(n);
1417 if (is_Block_dead(block)) {
1418 ir_graph *irg = get_irn_irg(n);
1419 return get_irg_bad(irg);
1422 if (n_preds == 0) return n; /* Phi of dead Region without predecessors. */
1424 /* Find first non-self-referencing input */
1425 for (i = 0; i < n_preds; ++i) {
1426 first_val = get_Phi_pred(n, i);
1427 if ( (first_val != n) /* not self pointer */
1429 /* BEWARE: when the if is changed to 1, Phi's will ignore it's Bad
1430 * predecessors. Then, Phi nodes in dead code might be removed, causing
1431 * nodes pointing to themself (Add's for instance).
1432 * This is really bad and causes endless recursions in several
1433 * code pathes, so we do NOT optimize such a code.
1434 * This is not that bad as it sounds, optimize_cf() removes bad control flow
1435 * (and bad Phi predecessors), so live code is optimized later.
1437 && (! is_Bad(get_Block_cfgpred(block, i)))
1439 ) { /* value not dead */
1440 break; /* then found first value. */
1445 ir_graph *irg = get_irn_irg(n);
1446 /* A totally Bad or self-referencing Phi (we didn't break the above loop) */
1447 return get_irg_bad(irg);
1450 /* search for rest of inputs, determine if any of these
1451 are non-self-referencing */
1452 while (++i < n_preds) {
1453 ir_node *scnd_val = get_Phi_pred(n, i);
1454 if ( (scnd_val != n)
1455 && (scnd_val != first_val)
1458 && (! is_Bad(get_Block_cfgpred(block, i)))
1466 /* Fold, if no multiple distinct non-self-referencing inputs */
1468 DBG_OPT_PHI(oldn, n);
1471 } /* equivalent_node_Phi */
1474 * Several optimizations:
1475 * - fold Sync-nodes, iff they have only one predecessor except
1478 static ir_node *equivalent_node_Sync(ir_node *n)
1480 int arity = get_Sync_n_preds(n);
1483 for (i = 0; i < arity;) {
1484 ir_node *pred = get_Sync_pred(n, i);
1487 /* Remove Bad predecessors */
1494 /* Remove duplicate predecessors */
1500 if (get_Sync_pred(n, j) == pred) {
1509 ir_graph *irg = get_irn_irg(n);
1510 return get_irg_bad(irg);
1512 if (arity == 1) return get_Sync_pred(n, 0);
1514 } /* equivalent_node_Sync */
1517 * Optimize Proj(Tuple).
1519 static ir_node *equivalent_node_Proj_Tuple(ir_node *proj)
1521 ir_node *oldn = proj;
1522 ir_node *tuple = get_Proj_pred(proj);
1524 /* Remove the Tuple/Proj combination. */
1525 proj = get_Tuple_pred(tuple, get_Proj_proj(proj));
1526 DBG_OPT_TUPLE(oldn, tuple, proj);
1529 } /* equivalent_node_Proj_Tuple */
1532 * Optimize a / 1 = a.
1534 static ir_node *equivalent_node_Proj_Div(ir_node *proj)
1536 ir_node *oldn = proj;
1537 ir_node *div = get_Proj_pred(proj);
1538 ir_node *b = get_Div_right(div);
1539 tarval *tb = value_of(b);
1541 /* Div is not commutative. */
1542 if (tarval_is_one(tb)) { /* div(x, 1) == x */
1543 switch (get_Proj_proj(proj)) {
1545 proj = get_Div_mem(div);
1546 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NEUTRAL_1);
1550 proj = get_Div_left(div);
1551 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NEUTRAL_1);
1555 /* we cannot replace the exception Proj's here, this is done in
1556 transform_node_Proj_Div() */
1561 } /* equivalent_node_Proj_Div */
1564 * Optimize a / 1.0 = a.
1566 static ir_node *equivalent_node_Proj_Quot(ir_node *proj)
1568 ir_node *oldn = proj;
1569 ir_node *quot = get_Proj_pred(proj);
1570 ir_node *b = get_Quot_right(quot);
1571 tarval *tb = value_of(b);
1573 /* Div is not commutative. */
1574 if (tarval_is_one(tb)) { /* Quot(x, 1) == x */
1575 switch (get_Proj_proj(proj)) {
1577 proj = get_Quot_mem(quot);
1578 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NEUTRAL_1);
1582 proj = get_Quot_left(quot);
1583 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NEUTRAL_1);
1587 /* we cannot replace the exception Proj's here, this is done in
1588 transform_node_Proj_Quot() */
1593 } /* equivalent_node_Proj_Quot */
1596 * Optimize a / 1 = a.
1598 static ir_node *equivalent_node_Proj_DivMod(ir_node *proj)
1600 ir_node *oldn = proj;
1601 ir_node *divmod = get_Proj_pred(proj);
1602 ir_node *b = get_DivMod_right(divmod);
1603 tarval *tb = value_of(b);
1605 /* Div is not commutative. */
1606 if (tarval_is_one(tb)) { /* div(x, 1) == x */
1607 switch (get_Proj_proj(proj)) {
1609 proj = get_DivMod_mem(divmod);
1610 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NEUTRAL_1);
1613 case pn_DivMod_res_div:
1614 proj = get_DivMod_left(divmod);
1615 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NEUTRAL_1);
1619 /* we cannot replace the exception Proj's here, this is done in
1620 transform_node_Proj_DivMod().
1621 Note further that the pn_DivMod_res_div case is handled in
1622 computed_value_Proj(). */
1627 } /* equivalent_node_Proj_DivMod */
1630 * Optimize CopyB(mem, x, x) into a Nop.
1632 static ir_node *equivalent_node_Proj_CopyB(ir_node *proj)
1634 ir_node *oldn = proj;
1635 ir_node *copyb = get_Proj_pred(proj);
1636 ir_node *a = get_CopyB_dst(copyb);
1637 ir_node *b = get_CopyB_src(copyb);
1640 /* Turn CopyB into a tuple (mem, jmp, bad, bad) */
1641 switch (get_Proj_proj(proj)) {
1643 proj = get_CopyB_mem(copyb);
1644 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NOP);
1647 case pn_CopyB_X_except: {
1648 ir_graph *irg = get_irn_irg(proj);
1649 DBG_OPT_EXC_REM(proj);
1650 proj = get_irg_bad(irg);
1656 } /* equivalent_node_Proj_CopyB */
1659 * Optimize Bounds(idx, idx, upper) into idx.
1661 static ir_node *equivalent_node_Proj_Bound(ir_node *proj)
1663 ir_node *oldn = proj;
1664 ir_node *bound = get_Proj_pred(proj);
1665 ir_node *idx = get_Bound_index(bound);
1666 ir_node *pred = skip_Proj(idx);
1669 if (idx == get_Bound_lower(bound))
1671 else if (is_Bound(pred)) {
1673 * idx was Bounds checked in the same MacroBlock previously,
1674 * it is still valid if lower <= pred_lower && pred_upper <= upper.
1676 ir_node *lower = get_Bound_lower(bound);
1677 ir_node *upper = get_Bound_upper(bound);
1678 if (get_Bound_lower(pred) == lower &&
1679 get_Bound_upper(pred) == upper &&
1680 get_irn_MacroBlock(bound) == get_irn_MacroBlock(pred)) {
1682 * One could expect that we simply return the previous
1683 * Bound here. However, this would be wrong, as we could
1684 * add an exception Proj to a new location then.
1685 * So, we must turn in into a tuple.
1691 /* Turn Bound into a tuple (mem, jmp, bad, idx) */
1692 switch (get_Proj_proj(proj)) {
1694 DBG_OPT_EXC_REM(proj);
1695 proj = get_Bound_mem(bound);
1697 case pn_Bound_X_except: {
1698 ir_graph *irg = get_irn_irg(proj);
1699 DBG_OPT_EXC_REM(proj);
1700 proj = get_irg_bad(irg);
1705 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NOP);
1708 /* cannot optimize pn_Bound_X_regular, handled in transform ... */
1713 } /* equivalent_node_Proj_Bound */
1716 * Optimize an Exception Proj(Load) with a non-null address.
1718 static ir_node *equivalent_node_Proj_Load(ir_node *proj)
1720 if (get_opt_ldst_only_null_ptr_exceptions()) {
1721 if (get_irn_mode(proj) == mode_X) {
1722 ir_node *load = get_Proj_pred(proj);
1724 /* get the Load address */
1725 const ir_node *addr = get_Load_ptr(load);
1726 const ir_node *confirm;
1728 if (value_not_null(addr, &confirm)) {
1729 if (get_Proj_proj(proj) == pn_Load_X_except) {
1730 ir_graph *irg = get_irn_irg(proj);
1731 DBG_OPT_EXC_REM(proj);
1732 return get_irg_bad(irg);
1738 } /* equivalent_node_Proj_Load */
1741 * Optimize an Exception Proj(Store) with a non-null address.
1743 static ir_node *equivalent_node_Proj_Store(ir_node *proj)
1745 if (get_opt_ldst_only_null_ptr_exceptions()) {
1746 if (get_irn_mode(proj) == mode_X) {
1747 ir_node *store = get_Proj_pred(proj);
1749 /* get the load/store address */
1750 const ir_node *addr = get_Store_ptr(store);
1751 const ir_node *confirm;
1753 if (value_not_null(addr, &confirm)) {
1754 if (get_Proj_proj(proj) == pn_Store_X_except) {
1755 ir_graph *irg = get_irn_irg(proj);
1756 DBG_OPT_EXC_REM(proj);
1757 return get_irg_bad(irg);
1763 } /* equivalent_node_Proj_Store */
1766 * Does all optimizations on nodes that must be done on it's Proj's
1767 * because of creating new nodes.
1769 static ir_node *equivalent_node_Proj(ir_node *proj)
1771 ir_node *n = get_Proj_pred(proj);
1773 if (get_irn_mode(proj) == mode_X) {
1774 if (is_Block_dead(get_nodes_block(n))) {
1775 /* Remove dead control flow -- early gigo(). */
1776 ir_graph *irg = get_irn_irg(proj);
1777 return get_irg_bad(irg);
1780 if (n->op->ops.equivalent_node_Proj)
1781 return n->op->ops.equivalent_node_Proj(proj);
1783 } /* equivalent_node_Proj */
1788 static ir_node *equivalent_node_Id(ir_node *n)
1796 DBG_OPT_ID(oldn, n);
1798 } /* equivalent_node_Id */
1803 static ir_node *equivalent_node_Mux(ir_node *n)
1805 ir_node *oldn = n, *sel = get_Mux_sel(n);
1807 tarval *ts = value_of(sel);
1809 /* Mux(true, f, t) == t */
1810 if (ts == tarval_b_true) {
1811 n = get_Mux_true(n);
1812 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_C);
1815 /* Mux(false, f, t) == f */
1816 if (ts == tarval_b_false) {
1817 n = get_Mux_false(n);
1818 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_C);
1821 n_t = get_Mux_true(n);
1822 n_f = get_Mux_false(n);
1824 /* Mux(v, x, T) == x */
1825 if (is_Unknown(n_f)) {
1827 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_EQ);
1830 /* Mux(v, T, x) == x */
1831 if (is_Unknown(n_t)) {
1833 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_EQ);
1837 /* Mux(v, x, x) == x */
1840 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_EQ);
1843 if (is_Proj(sel) && !mode_honor_signed_zeros(get_irn_mode(n))) {
1844 ir_node *cmp = get_Proj_pred(sel);
1845 long proj_nr = get_Proj_proj(sel);
1846 ir_node *f = get_Mux_false(n);
1847 ir_node *t = get_Mux_true(n);
1850 * Note further that these optimization work even for floating point
1851 * with NaN's because -NaN == NaN.
1852 * However, if +0 and -0 is handled differently, we cannot use the first one.
1855 ir_node *const cmp_l = get_Cmp_left(cmp);
1856 ir_node *const cmp_r = get_Cmp_right(cmp);
1860 if ((cmp_l == t && cmp_r == f) || /* Mux(t == f, t, f) -> f */
1861 (cmp_l == f && cmp_r == t)) { /* Mux(f == t, t, f) -> f */
1863 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1870 if ((cmp_l == t && cmp_r == f) || /* Mux(t != f, t, f) -> t */
1871 (cmp_l == f && cmp_r == t)) { /* Mux(f != t, t, f) -> t */
1873 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1880 * Note: normalization puts the constant on the right side,
1881 * so we check only one case.
1883 if (cmp_l == t && tarval_is_null(value_of(cmp_r))) {
1884 /* Mux(t CMP 0, X, t) */
1885 if (is_Minus(f) && get_Minus_op(f) == t) {
1886 /* Mux(t CMP 0, -t, t) */
1887 if (proj_nr == pn_Cmp_Eq) {
1888 /* Mux(t == 0, -t, t) ==> -t */
1890 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1891 } else if (proj_nr == pn_Cmp_Lg || proj_nr == pn_Cmp_Ne) {
1892 /* Mux(t != 0, -t, t) ==> t */
1894 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1901 } /* equivalent_node_Mux */
1904 * Remove Confirm nodes if setting is on.
1905 * Replace Confirms(x, '=', Constlike) by Constlike.
1907 static ir_node *equivalent_node_Confirm(ir_node *n)
1909 ir_node *pred = get_Confirm_value(n);
1910 pn_Cmp pnc = get_Confirm_cmp(n);
1912 while (is_Confirm(pred) && pnc == get_Confirm_cmp(pred)) {
1914 * rare case: two identical Confirms one after another,
1915 * replace the second one with the first.
1918 pred = get_Confirm_value(n);
1919 pnc = get_Confirm_cmp(n);
1921 if (get_opt_remove_confirm())
1922 return get_Confirm_value(n);
1927 * equivalent_node() returns a node equivalent to input n. It skips all nodes that
1928 * perform no actual computation, as, e.g., the Id nodes. It does not create
1929 * new nodes. It is therefore safe to free n if the node returned is not n.
1930 * If a node returns a Tuple we can not just skip it. If the size of the
1931 * in array fits, we transform n into a tuple (e.g., Div).
1933 ir_node *equivalent_node(ir_node *n)
1935 if (n->op->ops.equivalent_node)
1936 return n->op->ops.equivalent_node(n);
1938 } /* equivalent_node */
1941 * Sets the default equivalent node operation for an ir_op_ops.
1943 * @param code the opcode for the default operation
1944 * @param ops the operations initialized
1949 static ir_op_ops *firm_set_default_equivalent_node(ir_opcode code, ir_op_ops *ops)
1953 ops->equivalent_node = equivalent_node_##a; \
1955 #define CASE_PROJ(a) \
1957 ops->equivalent_node_Proj = equivalent_node_Proj_##a; \
2000 } /* firm_set_default_equivalent_node */
2003 * Returns non-zero if a node is a Phi node
2004 * with all predecessors constant.
2006 static int is_const_Phi(ir_node *n)
2010 if (! is_Phi(n) || get_irn_arity(n) == 0)
2012 for (i = get_irn_arity(n) - 1; i >= 0; --i) {
2013 if (! is_Const(get_irn_n(n, i)))
2017 } /* is_const_Phi */
2019 typedef tarval *(*tarval_sub_type)(tarval *a, tarval *b, ir_mode *mode);
2020 typedef tarval *(*tarval_binop_type)(tarval *a, tarval *b);
2023 * in reality eval_func should be tarval (*eval_func)() but incomplete
2024 * declarations are bad style and generate noisy warnings
2026 typedef void (*eval_func)(void);
2029 * Wrapper for the tarval binop evaluation, tarval_sub has one more parameter.
2031 static tarval *do_eval(eval_func eval, tarval *a, tarval *b, ir_mode *mode)
2033 if (eval == (eval_func) tarval_sub) {
2034 tarval_sub_type func = (tarval_sub_type)eval;
2036 return func(a, b, mode);
2038 tarval_binop_type func = (tarval_binop_type)eval;
2045 * Apply an evaluator on a binop with a constant operators (and one Phi).
2047 * @param phi the Phi node
2048 * @param other the other operand
2049 * @param eval an evaluator function
2050 * @param mode the mode of the result, may be different from the mode of the Phi!
2051 * @param left if non-zero, other is the left operand, else the right
2053 * @return a new Phi node if the conversion was successful, NULL else
2055 static ir_node *apply_binop_on_phi(ir_node *phi, tarval *other, eval_func eval, ir_mode *mode, int left)
2061 int i, n = get_irn_arity(phi);
2063 NEW_ARR_A(void *, res, n);
2065 for (i = 0; i < n; ++i) {
2066 pred = get_irn_n(phi, i);
2067 tv = get_Const_tarval(pred);
2068 tv = do_eval(eval, other, tv, mode);
2070 if (tv == tarval_bad) {
2071 /* folding failed, bad */
2077 for (i = 0; i < n; ++i) {
2078 pred = get_irn_n(phi, i);
2079 tv = get_Const_tarval(pred);
2080 tv = do_eval(eval, tv, other, mode);
2082 if (tv == tarval_bad) {
2083 /* folding failed, bad */
2089 irg = get_irn_irg(phi);
2090 for (i = 0; i < n; ++i) {
2091 pred = get_irn_n(phi, i);
2092 res[i] = new_r_Const_type(irg, res[i], get_Const_type(pred));
2094 return new_r_Phi(get_nodes_block(phi), n, (ir_node **)res, mode);
2095 } /* apply_binop_on_phi */
2098 * Apply an evaluator on a binop with two constant Phi.
2100 * @param a the left Phi node
2101 * @param b the right Phi node
2102 * @param eval an evaluator function
2103 * @param mode the mode of the result, may be different from the mode of the Phi!
2105 * @return a new Phi node if the conversion was successful, NULL else
2107 static ir_node *apply_binop_on_2_phis(ir_node *a, ir_node *b, eval_func eval, ir_mode *mode)
2109 tarval *tv_l, *tv_r, *tv;
2115 if (get_nodes_block(a) != get_nodes_block(b))
2118 n = get_irn_arity(a);
2119 NEW_ARR_A(void *, res, n);
2121 for (i = 0; i < n; ++i) {
2122 pred = get_irn_n(a, i);
2123 tv_l = get_Const_tarval(pred);
2124 pred = get_irn_n(b, i);
2125 tv_r = get_Const_tarval(pred);
2126 tv = do_eval(eval, tv_l, tv_r, mode);
2128 if (tv == tarval_bad) {
2129 /* folding failed, bad */
2134 irg = get_irn_irg(a);
2135 for (i = 0; i < n; ++i) {
2136 pred = get_irn_n(a, i);
2137 res[i] = new_r_Const_type(irg, res[i], get_Const_type(pred));
2139 return new_r_Phi(get_nodes_block(a), n, (ir_node **)res, mode);
2140 } /* apply_binop_on_2_phis */
2143 * Apply an evaluator on a unop with a constant operator (a Phi).
2145 * @param phi the Phi node
2146 * @param eval an evaluator function
2148 * @return a new Phi node if the conversion was successful, NULL else
2150 static ir_node *apply_unop_on_phi(ir_node *phi, tarval *(*eval)(tarval *))
2157 int i, n = get_irn_arity(phi);
2159 NEW_ARR_A(void *, res, n);
2160 for (i = 0; i < n; ++i) {
2161 pred = get_irn_n(phi, i);
2162 tv = get_Const_tarval(pred);
2165 if (tv == tarval_bad) {
2166 /* folding failed, bad */
2171 mode = get_irn_mode(phi);
2172 irg = get_irn_irg(phi);
2173 for (i = 0; i < n; ++i) {
2174 pred = get_irn_n(phi, i);
2175 res[i] = new_r_Const_type(irg, res[i], get_Const_type(pred));
2177 return new_r_Phi(get_nodes_block(phi), n, (ir_node **)res, mode);
2178 } /* apply_unop_on_phi */
2181 * Apply a conversion on a constant operator (a Phi).
2183 * @param phi the Phi node
2185 * @return a new Phi node if the conversion was successful, NULL else
2187 static ir_node *apply_conv_on_phi(ir_node *phi, ir_mode *mode)
2193 int i, n = get_irn_arity(phi);
2195 NEW_ARR_A(void *, res, n);
2196 for (i = 0; i < n; ++i) {
2197 pred = get_irn_n(phi, i);
2198 tv = get_Const_tarval(pred);
2199 tv = tarval_convert_to(tv, mode);
2201 if (tv == tarval_bad) {
2202 /* folding failed, bad */
2207 irg = get_irn_irg(phi);
2208 for (i = 0; i < n; ++i) {
2209 pred = get_irn_n(phi, i);
2210 res[i] = new_r_Const_type(irg, res[i], get_Const_type(pred));
2212 return new_r_Phi(get_nodes_block(phi), n, (ir_node **)res, mode);
2213 } /* apply_conv_on_phi */
2216 * Transform AddP(P, ConvIs(Iu)), AddP(P, ConvIu(Is)) and
2217 * SubP(P, ConvIs(Iu)), SubP(P, ConvIu(Is)).
2218 * If possible, remove the Conv's.
2220 static ir_node *transform_node_AddSub(ir_node *n)
2222 ir_mode *mode = get_irn_mode(n);
2224 if (mode_is_reference(mode)) {
2225 ir_node *left = get_binop_left(n);
2226 ir_node *right = get_binop_right(n);
2227 unsigned ref_bits = get_mode_size_bits(mode);
2229 if (is_Conv(left)) {
2230 ir_mode *lmode = get_irn_mode(left);
2231 unsigned bits = get_mode_size_bits(lmode);
2233 if (ref_bits == bits &&
2234 mode_is_int(lmode) &&
2235 get_mode_arithmetic(lmode) == irma_twos_complement) {
2236 ir_node *pre = get_Conv_op(left);
2237 ir_mode *pre_mode = get_irn_mode(pre);
2239 if (mode_is_int(pre_mode) &&
2240 get_mode_size_bits(pre_mode) == bits &&
2241 get_mode_arithmetic(pre_mode) == irma_twos_complement) {
2242 /* ok, this conv just changes to sign, moreover the calculation
2243 * is done with same number of bits as our address mode, so
2244 * we can ignore the conv as address calculation can be viewed
2245 * as either signed or unsigned
2247 set_binop_left(n, pre);
2252 if (is_Conv(right)) {
2253 ir_mode *rmode = get_irn_mode(right);
2254 unsigned bits = get_mode_size_bits(rmode);
2256 if (ref_bits == bits &&
2257 mode_is_int(rmode) &&
2258 get_mode_arithmetic(rmode) == irma_twos_complement) {
2259 ir_node *pre = get_Conv_op(right);
2260 ir_mode *pre_mode = get_irn_mode(pre);
2262 if (mode_is_int(pre_mode) &&
2263 get_mode_size_bits(pre_mode) == bits &&
2264 get_mode_arithmetic(pre_mode) == irma_twos_complement) {
2265 /* ok, this conv just changes to sign, moreover the calculation
2266 * is done with same number of bits as our address mode, so
2267 * we can ignore the conv as address calculation can be viewed
2268 * as either signed or unsigned
2270 set_binop_right(n, pre);
2275 /* let address arithmetic use unsigned modes */
2276 if (is_Const(right)) {
2277 ir_mode *rmode = get_irn_mode(right);
2279 if (mode_is_signed(rmode) && get_mode_arithmetic(rmode) == irma_twos_complement) {
2280 /* convert a AddP(P, *s) into AddP(P, *u) */
2281 ir_mode *nm = get_reference_mode_unsigned_eq(mode);
2283 ir_node *pre = new_r_Conv(get_nodes_block(n), right, nm);
2284 set_binop_right(n, pre);
2290 } /* transform_node_AddSub */
2292 #define HANDLE_BINOP_PHI(eval, a, b, c, mode) \
2295 if (is_Const(b) && is_const_Phi(a)) { \
2296 /* check for Op(Phi, Const) */ \
2297 c = apply_binop_on_phi(a, get_Const_tarval(b), eval, mode, 0);\
2299 else if (is_Const(a) && is_const_Phi(b)) { \
2300 /* check for Op(Const, Phi) */ \
2301 c = apply_binop_on_phi(b, get_Const_tarval(a), eval, mode, 1);\
2303 else if (is_const_Phi(a) && is_const_Phi(b)) { \
2304 /* check for Op(Phi, Phi) */ \
2305 c = apply_binop_on_2_phis(a, b, eval, mode); \
2308 DBG_OPT_ALGSIM0(oldn, c, FS_OPT_CONST_PHI); \
2313 #define HANDLE_UNOP_PHI(eval, a, c) \
2316 if (is_const_Phi(a)) { \
2317 /* check for Op(Phi) */ \
2318 c = apply_unop_on_phi(a, eval); \
2320 DBG_OPT_ALGSIM0(oldn, c, FS_OPT_CONST_PHI); \
2327 * Do the AddSub optimization, then Transform
2328 * Constant folding on Phi
2329 * Add(a,a) -> Mul(a, 2)
2330 * Add(Mul(a, x), a) -> Mul(a, x+1)
2331 * if the mode is integer or float.
2332 * Transform Add(a,-b) into Sub(a,b).
2333 * Reassociation might fold this further.
2335 static ir_node *transform_node_Add(ir_node *n)
2338 ir_node *a, *b, *c, *oldn = n;
2339 vrp_attr *a_vrp, *b_vrp;
2341 n = transform_node_AddSub(n);
2343 a = get_Add_left(n);
2344 b = get_Add_right(n);
2346 mode = get_irn_mode(n);
2348 if (mode_is_reference(mode)) {
2349 ir_mode *lmode = get_irn_mode(a);
2351 if (is_Const(b) && is_Const_null(b) && mode_is_int(lmode)) {
2352 /* an Add(a, NULL) is a hidden Conv */
2353 dbg_info *dbg = get_irn_dbg_info(n);
2354 return new_rd_Conv(dbg, get_nodes_block(n), a, mode);
2358 HANDLE_BINOP_PHI((eval_func) tarval_add, a, b, c, mode);
2360 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
2361 if (mode_is_float(mode)) {
2362 ir_graph *irg = get_irn_irg(n);
2363 if (get_irg_fp_model(irg) & fp_strict_algebraic)
2367 if (mode_is_num(mode)) {
2368 ir_graph *irg = get_irn_irg(n);
2369 /* the following code leads to endless recursion when Mul are replaced by a simple instruction chain */
2370 if (!is_irg_state(irg, IR_GRAPH_STATE_ARCH_DEP)
2371 && a == b && mode_is_int(mode)) {
2372 ir_node *block = get_nodes_block(n);
2375 get_irn_dbg_info(n),
2378 new_Const_long(mode, 2),
2380 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_A);
2385 get_irn_dbg_info(n),
2390 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_MINUS_B);
2395 get_irn_dbg_info(n),
2400 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_MINUS_B);
2403 if (get_mode_arithmetic(mode) == irma_twos_complement) {
2404 /* Here we rely on constants be on the RIGHT side */
2406 ir_node *op = get_Not_op(a);
2408 if (is_Const(b) && is_Const_one(b)) {
2410 ir_node *blk = get_nodes_block(n);
2411 n = new_rd_Minus(get_irn_dbg_info(n), blk, op, mode);
2412 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_PLUS_1);
2417 n = new_Const(get_mode_minus_one(mode));
2418 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_X_NOT_X);
2423 ir_node *op = get_Not_op(b);
2427 n = new_Const(get_mode_minus_one(mode));
2428 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_X_NOT_X);
2435 a_vrp = vrp_get_info(a);
2436 b_vrp = vrp_get_info(b);
2438 if (a_vrp && b_vrp) {
2439 tarval *c = tarval_and(
2440 a_vrp->bits_not_set,
2444 if (tarval_is_null(c)) {
2445 dbg_info *dbgi = get_irn_dbg_info(n);
2446 return new_rd_Or(dbgi, get_nodes_block(n),
2451 } /* transform_node_Add */
2454 * returns -cnst or NULL if impossible
2456 static ir_node *const_negate(ir_node *cnst)
2458 tarval *tv = tarval_neg(get_Const_tarval(cnst));
2459 dbg_info *dbgi = get_irn_dbg_info(cnst);
2460 ir_graph *irg = get_irn_irg(cnst);
2461 if (tv == tarval_bad) return NULL;
2462 return new_rd_Const(dbgi, irg, tv);
2466 * Do the AddSub optimization, then Transform
2467 * Constant folding on Phi
2468 * Sub(0,a) -> Minus(a)
2469 * Sub(Mul(a, x), a) -> Mul(a, x-1)
2470 * Sub(Sub(x, y), b) -> Sub(x, Add(y,b))
2471 * Sub(Add(a, x), x) -> a
2472 * Sub(x, Add(x, a)) -> -a
2473 * Sub(x, Const) -> Add(x, -Const)
2475 static ir_node *transform_node_Sub(ir_node *n)
2481 n = transform_node_AddSub(n);
2483 a = get_Sub_left(n);
2484 b = get_Sub_right(n);
2486 mode = get_irn_mode(n);
2488 if (mode_is_int(mode)) {
2489 ir_mode *lmode = get_irn_mode(a);
2491 if (is_Const(b) && is_Const_null(b) && mode_is_reference(lmode)) {
2492 /* a Sub(a, NULL) is a hidden Conv */
2493 dbg_info *dbg = get_irn_dbg_info(n);
2494 n = new_rd_Conv(dbg, get_nodes_block(n), a, mode);
2495 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_CONV);
2499 if (mode == lmode &&
2500 get_mode_arithmetic(mode) == irma_twos_complement &&
2502 get_Const_tarval(a) == get_mode_minus_one(mode)) {
2504 dbg_info *dbg = get_irn_dbg_info(n);
2505 n = new_rd_Not(dbg, get_nodes_block(n), b, mode);
2506 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_NOT);
2512 HANDLE_BINOP_PHI((eval_func) tarval_sub, a, b, c, mode);
2514 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
2515 if (mode_is_float(mode)) {
2516 ir_graph *irg = get_irn_irg(n);
2517 if (get_irg_fp_model(irg) & fp_strict_algebraic)
2521 if (is_Const(b) && !mode_is_reference(get_irn_mode(b))) {
2522 /* a - C -> a + (-C) */
2523 ir_node *cnst = const_negate(b);
2525 ir_node *block = get_nodes_block(n);
2526 dbg_info *dbgi = get_irn_dbg_info(n);
2528 n = new_rd_Add(dbgi, block, a, cnst, mode);
2529 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2534 if (is_Minus(a)) { /* (-a) - b -> -(a + b) */
2535 dbg_info *dbg = get_irn_dbg_info(n);
2536 ir_node *block = get_nodes_block(n);
2537 ir_node *left = get_Minus_op(a);
2538 ir_node *add = new_rd_Add(dbg, block, left, b, mode);
2540 n = new_rd_Minus(dbg, block, add, mode);
2541 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2543 } else if (is_Minus(b)) { /* a - (-b) -> a + b */
2544 dbg_info *dbg = get_irn_dbg_info(n);
2545 ir_node *block = get_nodes_block(n);
2546 ir_node *right = get_Minus_op(b);
2548 n = new_rd_Add(dbg, block, a, right, mode);
2549 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MINUS);
2551 } else if (is_Sub(b)) {
2552 /* a - (b - c) -> a + (c - b)
2553 * -> (a - b) + c iff (b - c) is a pointer */
2554 dbg_info *s_dbg = get_irn_dbg_info(b);
2555 ir_node *s_block = get_nodes_block(b);
2556 ir_node *s_left = get_Sub_left(b);
2557 ir_node *s_right = get_Sub_right(b);
2558 ir_mode *s_mode = get_irn_mode(b);
2559 if (mode_is_reference(s_mode)) {
2560 ir_node *sub = new_rd_Sub(s_dbg, s_block, a, s_left, mode);
2561 dbg_info *a_dbg = get_irn_dbg_info(n);
2562 ir_node *a_block = get_nodes_block(n);
2565 s_right = new_r_Conv(a_block, s_right, mode);
2566 n = new_rd_Add(a_dbg, a_block, sub, s_right, mode);
2568 ir_node *sub = new_rd_Sub(s_dbg, s_block, s_right, s_left, s_mode);
2569 dbg_info *a_dbg = get_irn_dbg_info(n);
2570 ir_node *a_block = get_nodes_block(n);
2572 n = new_rd_Add(a_dbg, a_block, a, sub, mode);
2574 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2576 } else if (is_Mul(b)) { /* a - (b * C) -> a + (b * -C) */
2577 ir_node *m_right = get_Mul_right(b);
2578 if (is_Const(m_right)) {
2579 ir_node *cnst2 = const_negate(m_right);
2580 if (cnst2 != NULL) {
2581 dbg_info *m_dbg = get_irn_dbg_info(b);
2582 ir_node *m_block = get_nodes_block(b);
2583 ir_node *m_left = get_Mul_left(b);
2584 ir_mode *m_mode = get_irn_mode(b);
2585 ir_node *mul = new_rd_Mul(m_dbg, m_block, m_left, cnst2, m_mode);
2586 dbg_info *a_dbg = get_irn_dbg_info(n);
2587 ir_node *a_block = get_nodes_block(n);
2589 n = new_rd_Add(a_dbg, a_block, a, mul, mode);
2590 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2596 /* Beware of Sub(P, P) which cannot be optimized into a simple Minus ... */
2597 if (mode_is_num(mode) && mode == get_irn_mode(a) && is_Const(a) && is_Const_null(a)) {
2599 get_irn_dbg_info(n),
2603 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_0_A);
2607 if (mode_wrap_around(mode)) {
2608 ir_node *left = get_Add_left(a);
2609 ir_node *right = get_Add_right(a);
2611 /* FIXME: Does the Conv's work only for two complement or generally? */
2613 if (mode != get_irn_mode(right)) {
2614 /* This Sub is an effective Cast */
2615 right = new_r_Conv(get_nodes_block(n), right, mode);
2618 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2620 } else if (right == b) {
2621 if (mode != get_irn_mode(left)) {
2622 /* This Sub is an effective Cast */
2623 left = new_r_Conv(get_nodes_block(n), left, mode);
2626 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2632 if (mode_wrap_around(mode)) {
2633 ir_node *left = get_Add_left(b);
2634 ir_node *right = get_Add_right(b);
2636 /* FIXME: Does the Conv's work only for two complement or generally? */
2638 ir_mode *r_mode = get_irn_mode(right);
2640 n = new_r_Minus(get_nodes_block(n), right, r_mode);
2641 if (mode != r_mode) {
2642 /* This Sub is an effective Cast */
2643 n = new_r_Conv(get_nodes_block(n), n, mode);
2645 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2647 } else if (right == a) {
2648 ir_mode *l_mode = get_irn_mode(left);
2650 n = new_r_Minus(get_nodes_block(n), left, l_mode);
2651 if (mode != l_mode) {
2652 /* This Sub is an effective Cast */
2653 n = new_r_Conv(get_nodes_block(n), n, mode);
2655 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2660 if (mode_is_int(mode) && is_Conv(a) && is_Conv(b)) {
2661 ir_mode *mode = get_irn_mode(a);
2663 if (mode == get_irn_mode(b)) {
2665 ir_node *op_a = get_Conv_op(a);
2666 ir_node *op_b = get_Conv_op(b);
2668 /* check if it's allowed to skip the conv */
2669 ma = get_irn_mode(op_a);
2670 mb = get_irn_mode(op_b);
2672 if (mode_is_reference(ma) && mode_is_reference(mb)) {
2673 /* SubInt(ConvInt(aP), ConvInt(bP)) -> SubInt(aP,bP) */
2676 set_Sub_right(n, b);
2682 /* do NOT execute this code if reassociation is enabled, it does the inverse! */
2683 if (!is_reassoc_running() && is_Mul(a)) {
2684 ir_node *ma = get_Mul_left(a);
2685 ir_node *mb = get_Mul_right(a);
2688 ir_node *blk = get_nodes_block(n);
2690 get_irn_dbg_info(n),
2694 get_irn_dbg_info(n),
2697 new_Const(get_mode_one(mode)),
2700 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MUL_A_X_A);
2702 } else if (mb == b) {
2703 ir_node *blk = get_nodes_block(n);
2705 get_irn_dbg_info(n),
2709 get_irn_dbg_info(n),
2712 new_Const(get_mode_one(mode)),
2715 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MUL_A_X_A);
2719 if (is_Sub(a)) { /* (x - y) - b -> x - (y + b) */
2720 ir_node *x = get_Sub_left(a);
2721 ir_node *y = get_Sub_right(a);
2722 ir_node *blk = get_nodes_block(n);
2723 ir_mode *m_b = get_irn_mode(b);
2724 ir_mode *m_y = get_irn_mode(y);
2728 /* Determine the right mode for the Add. */
2731 else if (mode_is_reference(m_b))
2733 else if (mode_is_reference(m_y))
2737 * Both modes are different but none is reference,
2738 * happens for instance in SubP(SubP(P, Iu), Is).
2739 * We have two possibilities here: Cast or ignore.
2740 * Currently we ignore this case.
2745 add = new_r_Add(blk, y, b, add_mode);
2747 n = new_rd_Sub(get_irn_dbg_info(n), blk, x, add, mode);
2748 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_SUB_X_Y_Z);
2752 if (get_mode_arithmetic(mode) == irma_twos_complement) {
2753 if (is_Const(a) && is_Not(b)) {
2754 /* c - ~X = X + (c+1) */
2755 tarval *tv = get_Const_tarval(a);
2757 tv = tarval_add(tv, get_mode_one(mode));
2758 if (tv != tarval_bad) {
2759 ir_node *blk = get_nodes_block(n);
2760 ir_node *c = new_Const(tv);
2761 n = new_rd_Add(get_irn_dbg_info(n), blk, get_Not_op(b), c, mode);
2762 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_C_NOT_X);
2768 } /* transform_node_Sub */
2771 * Several transformation done on n*n=2n bits mul.
2772 * These transformations must be done here because new nodes may be produced.
2774 static ir_node *transform_node_Mul2n(ir_node *n, ir_mode *mode)
2777 ir_node *a = get_Mul_left(n);
2778 ir_node *b = get_Mul_right(n);
2779 tarval *ta = value_of(a);
2780 tarval *tb = value_of(b);
2781 ir_mode *smode = get_irn_mode(a);
2783 if (ta == get_mode_one(smode)) {
2784 /* (L)1 * (L)b = (L)b */
2785 ir_node *blk = get_nodes_block(n);
2786 n = new_rd_Conv(get_irn_dbg_info(n), blk, b, mode);
2787 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
2790 else if (ta == get_mode_minus_one(smode)) {
2791 /* (L)-1 * (L)b = (L)b */
2792 ir_node *blk = get_nodes_block(n);
2793 n = new_rd_Minus(get_irn_dbg_info(n), blk, b, smode);
2794 n = new_rd_Conv(get_irn_dbg_info(n), blk, n, mode);
2795 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2798 if (tb == get_mode_one(smode)) {
2799 /* (L)a * (L)1 = (L)a */
2800 ir_node *blk = get_irn_n(a, -1);
2801 n = new_rd_Conv(get_irn_dbg_info(n), blk, a, mode);
2802 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
2805 else if (tb == get_mode_minus_one(smode)) {
2806 /* (L)a * (L)-1 = (L)-a */
2807 ir_node *blk = get_nodes_block(n);
2808 n = new_rd_Minus(get_irn_dbg_info(n), blk, a, smode);
2809 n = new_rd_Conv(get_irn_dbg_info(n), blk, n, mode);
2810 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2817 * Transform Mul(a,-1) into -a.
2818 * Do constant evaluation of Phi nodes.
2819 * Do architecture dependent optimizations on Mul nodes
2821 static ir_node *transform_node_Mul(ir_node *n)
2823 ir_node *c, *oldn = n;
2824 ir_mode *mode = get_irn_mode(n);
2825 ir_node *a = get_Mul_left(n);
2826 ir_node *b = get_Mul_right(n);
2828 if (is_Bad(a) || is_Bad(b))
2831 if (mode != get_irn_mode(a))
2832 return transform_node_Mul2n(n, mode);
2834 HANDLE_BINOP_PHI((eval_func) tarval_mul, a, b, c, mode);
2836 if (mode_is_signed(mode)) {
2839 if (value_of(a) == get_mode_minus_one(mode))
2841 else if (value_of(b) == get_mode_minus_one(mode))
2844 n = new_rd_Minus(get_irn_dbg_info(n), get_nodes_block(n), r, mode);
2845 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2850 if (is_Const(b)) { /* (-a) * const -> a * -const */
2851 ir_node *cnst = const_negate(b);
2853 dbg_info *dbgi = get_irn_dbg_info(n);
2854 ir_node *block = get_nodes_block(n);
2855 n = new_rd_Mul(dbgi, block, get_Minus_op(a), cnst, mode);
2856 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2859 } else if (is_Minus(b)) { /* (-a) * (-b) -> a * b */
2860 dbg_info *dbgi = get_irn_dbg_info(n);
2861 ir_node *block = get_nodes_block(n);
2862 n = new_rd_Mul(dbgi, block, get_Minus_op(a), get_Minus_op(b), mode);
2863 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_MINUS);
2865 } else if (is_Sub(b)) { /* (-a) * (b - c) -> a * (c - b) */
2866 ir_node *sub_l = get_Sub_left(b);
2867 ir_node *sub_r = get_Sub_right(b);
2868 dbg_info *dbgi = get_irn_dbg_info(n);
2869 ir_node *block = get_nodes_block(n);
2870 ir_node *new_b = new_rd_Sub(dbgi, block, sub_r, sub_l, mode);
2871 n = new_rd_Mul(dbgi, block, get_Minus_op(a), new_b, mode);
2872 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS);
2875 } else if (is_Minus(b)) {
2876 if (is_Sub(a)) { /* (a - b) * (-c) -> (b - a) * c */
2877 ir_node *sub_l = get_Sub_left(a);
2878 ir_node *sub_r = get_Sub_right(a);
2879 dbg_info *dbgi = get_irn_dbg_info(n);
2880 ir_node *block = get_nodes_block(n);
2881 ir_node *new_a = new_rd_Sub(dbgi, block, sub_r, sub_l, mode);
2882 n = new_rd_Mul(dbgi, block, new_a, get_Minus_op(b), mode);
2883 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS);
2886 } else if (is_Shl(a)) {
2887 ir_node *const shl_l = get_Shl_left(a);
2888 if (is_Const(shl_l) && is_Const_one(shl_l)) {
2889 /* (1 << x) * b -> b << x */
2890 dbg_info *const dbgi = get_irn_dbg_info(n);
2891 ir_node *const block = get_nodes_block(n);
2892 ir_node *const shl_r = get_Shl_right(a);
2893 n = new_rd_Shl(dbgi, block, b, shl_r, mode);
2894 // TODO add me DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_SHIFT);
2897 } else if (is_Shl(b)) {
2898 ir_node *const shl_l = get_Shl_left(b);
2899 if (is_Const(shl_l) && is_Const_one(shl_l)) {
2900 /* a * (1 << x) -> a << x */
2901 dbg_info *const dbgi = get_irn_dbg_info(n);
2902 ir_node *const block = get_nodes_block(n);
2903 ir_node *const shl_r = get_Shl_right(b);
2904 n = new_rd_Shl(dbgi, block, a, shl_r, mode);
2905 // TODO add me DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_SHIFT);
2909 if (get_mode_arithmetic(mode) == irma_ieee754) {
2911 tarval *tv = get_Const_tarval(a);
2912 if (tarval_ieee754_get_exponent(tv) == 1 && tarval_ieee754_zero_mantissa(tv)
2913 && !tarval_is_negative(tv)) {
2914 /* 2.0 * b = b + b */
2915 n = new_rd_Add(get_irn_dbg_info(n), get_nodes_block(n), b, b, mode);
2916 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_A_A);
2920 else if (is_Const(b)) {
2921 tarval *tv = get_Const_tarval(b);
2922 if (tarval_ieee754_get_exponent(tv) == 1 && tarval_ieee754_zero_mantissa(tv)
2923 && !tarval_is_negative(tv)) {
2924 /* a * 2.0 = a + a */
2925 n = new_rd_Add(get_irn_dbg_info(n), get_nodes_block(n), a, a, mode);
2926 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_A_A);
2931 return arch_dep_replace_mul_with_shifts(n);
2932 } /* transform_node_Mul */
2935 * Transform a Div Node.
2937 static ir_node *transform_node_Div(ir_node *n)
2939 ir_mode *mode = get_Div_resmode(n);
2940 ir_node *a = get_Div_left(n);
2941 ir_node *b = get_Div_right(n);
2943 const ir_node *dummy;
2945 if (is_Const(b) && is_const_Phi(a)) {
2946 /* check for Div(Phi, Const) */
2947 value = apply_binop_on_phi(a, get_Const_tarval(b), (eval_func) tarval_div, mode, 0);
2949 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2953 else if (is_Const(a) && is_const_Phi(b)) {
2954 /* check for Div(Const, Phi) */
2955 value = apply_binop_on_phi(b, get_Const_tarval(a), (eval_func) tarval_div, mode, 1);
2957 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2961 else if (is_const_Phi(a) && is_const_Phi(b)) {
2962 /* check for Div(Phi, Phi) */
2963 value = apply_binop_on_2_phis(a, b, (eval_func) tarval_div, mode);
2965 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2972 if (a == b && value_not_zero(a, &dummy)) {
2973 /* BEWARE: we can optimize a/a to 1 only if this cannot cause a exception */
2974 value = new_Const(get_mode_one(mode));
2975 DBG_OPT_CSTEVAL(n, value);
2978 if (mode_is_signed(mode) && is_Const(b)) {
2979 tarval *tv = get_Const_tarval(b);
2981 if (tv == get_mode_minus_one(mode)) {
2983 value = new_rd_Minus(get_irn_dbg_info(n), get_nodes_block(n), a, mode);
2984 DBG_OPT_CSTEVAL(n, value);
2988 /* Try architecture dependent optimization */
2989 value = arch_dep_replace_div_by_const(n);
2996 /* Turn Div into a tuple (mem, jmp, bad, value) */
2997 mem = get_Div_mem(n);
2998 blk = get_nodes_block(n);
3000 /* skip a potential Pin */
3001 mem = skip_Pin(mem);
3002 turn_into_tuple(n, pn_Div_max);
3003 set_Tuple_pred(n, pn_Div_M, mem);
3004 set_Tuple_pred(n, pn_Div_X_regular, new_r_Jmp(blk));
3005 set_Tuple_pred(n, pn_Div_X_except, new_Bad());
3006 set_Tuple_pred(n, pn_Div_res, value);
3009 } /* transform_node_Div */
3012 * Transform a Mod node.
3014 static ir_node *transform_node_Mod(ir_node *n)
3016 ir_mode *mode = get_Mod_resmode(n);
3017 ir_node *a = get_Mod_left(n);
3018 ir_node *b = get_Mod_right(n);
3022 if (is_Const(b) && is_const_Phi(a)) {
3023 /* check for Div(Phi, Const) */
3024 value = apply_binop_on_phi(a, get_Const_tarval(b), (eval_func) tarval_mod, mode, 0);
3026 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
3030 else if (is_Const(a) && is_const_Phi(b)) {
3031 /* check for Div(Const, Phi) */
3032 value = apply_binop_on_phi(b, get_Const_tarval(a), (eval_func) tarval_mod, mode, 1);
3034 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
3038 else if (is_const_Phi(a) && is_const_Phi(b)) {
3039 /* check for Div(Phi, Phi) */
3040 value = apply_binop_on_2_phis(a, b, (eval_func) tarval_mod, mode);
3042 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
3049 if (tv != tarval_bad) {
3050 value = new_Const(tv);
3052 DBG_OPT_CSTEVAL(n, value);
3055 ir_node *a = get_Mod_left(n);
3056 ir_node *b = get_Mod_right(n);
3057 const ir_node *dummy;
3059 if (a == b && value_not_zero(a, &dummy)) {
3060 /* BEWARE: we can optimize a%a to 0 only if this cannot cause a exception */
3061 value = new_Const(get_mode_null(mode));
3062 DBG_OPT_CSTEVAL(n, value);
3065 if (mode_is_signed(mode) && is_Const(b)) {
3066 tarval *tv = get_Const_tarval(b);
3068 if (tv == get_mode_minus_one(mode)) {
3070 value = new_Const(get_mode_null(mode));
3071 DBG_OPT_CSTEVAL(n, value);
3075 /* Try architecture dependent optimization */
3076 value = arch_dep_replace_mod_by_const(n);
3084 /* Turn Mod into a tuple (mem, jmp, bad, value) */
3085 mem = get_Mod_mem(n);
3086 blk = get_nodes_block(n);
3088 /* skip a potential Pin */
3089 mem = skip_Pin(mem);
3090 turn_into_tuple(n, pn_Mod_max);
3091 set_Tuple_pred(n, pn_Mod_M, mem);
3092 set_Tuple_pred(n, pn_Mod_X_regular, new_r_Jmp(blk));
3093 set_Tuple_pred(n, pn_Mod_X_except, new_Bad());
3094 set_Tuple_pred(n, pn_Mod_res, value);
3097 } /* transform_node_Mod */
3100 * Transform a DivMod node.
3102 static ir_node *transform_node_DivMod(ir_node *n)
3104 const ir_node *dummy;
3105 ir_node *a = get_DivMod_left(n);
3106 ir_node *b = get_DivMod_right(n);
3107 ir_mode *mode = get_DivMod_resmode(n);
3112 if (is_Const(b) && is_const_Phi(a)) {
3113 /* check for Div(Phi, Const) */
3114 va = apply_binop_on_phi(a, get_Const_tarval(b), (eval_func) tarval_div, mode, 0);
3115 vb = apply_binop_on_phi(a, get_Const_tarval(b), (eval_func) tarval_mod, mode, 0);
3117 DBG_OPT_ALGSIM0(n, va, FS_OPT_CONST_PHI);
3118 DBG_OPT_ALGSIM0(n, vb, FS_OPT_CONST_PHI);
3122 else if (is_Const(a) && is_const_Phi(b)) {
3123 /* check for Div(Const, Phi) */
3124 va = apply_binop_on_phi(b, get_Const_tarval(a), (eval_func) tarval_div, mode, 1);
3125 vb = apply_binop_on_phi(b, get_Const_tarval(a), (eval_func) tarval_mod, mode, 1);
3127 DBG_OPT_ALGSIM0(n, va, FS_OPT_CONST_PHI);
3128 DBG_OPT_ALGSIM0(n, vb, FS_OPT_CONST_PHI);
3132 else if (is_const_Phi(a) && is_const_Phi(b)) {
3133 /* check for Div(Phi, Phi) */
3134 va = apply_binop_on_2_phis(a, b, (eval_func) tarval_div, mode);
3135 vb = apply_binop_on_2_phis(a, b, (eval_func) tarval_mod, mode);
3137 DBG_OPT_ALGSIM0(n, va, FS_OPT_CONST_PHI);
3138 DBG_OPT_ALGSIM0(n, vb, FS_OPT_CONST_PHI);
3145 if (tb != tarval_bad) {
3146 if (tb == get_mode_one(get_tarval_mode(tb))) {
3148 vb = new_Const(get_mode_null(mode));
3149 DBG_OPT_CSTEVAL(n, vb);
3151 } else if (ta != tarval_bad) {
3152 tarval *resa, *resb;
3153 resa = tarval_div(ta, tb);
3154 if (resa == tarval_bad) return n; /* Causes exception!!! Model by replacing through
3155 Jmp for X result!? */
3156 resb = tarval_mod(ta, tb);
3157 if (resb == tarval_bad) return n; /* Causes exception! */
3158 va = new_Const(resa);
3159 vb = new_Const(resb);
3160 DBG_OPT_CSTEVAL(n, va);
3161 DBG_OPT_CSTEVAL(n, vb);
3163 } else if (mode_is_signed(mode) && tb == get_mode_minus_one(mode)) {
3164 va = new_rd_Minus(get_irn_dbg_info(n), get_nodes_block(n), a, mode);
3165 vb = new_Const(get_mode_null(mode));
3166 DBG_OPT_CSTEVAL(n, va);
3167 DBG_OPT_CSTEVAL(n, vb);
3169 } else { /* Try architecture dependent optimization */
3172 arch_dep_replace_divmod_by_const(&va, &vb, n);
3173 evaluated = va != NULL;
3175 } else if (a == b) {
3176 if (value_not_zero(a, &dummy)) {
3178 va = new_Const(get_mode_one(mode));
3179 vb = new_Const(get_mode_null(mode));
3180 DBG_OPT_CSTEVAL(n, va);
3181 DBG_OPT_CSTEVAL(n, vb);
3184 /* BEWARE: it is NOT possible to optimize a/a to 1, as this may cause a exception */
3187 } else if (ta == get_mode_null(mode) && value_not_zero(b, &dummy)) {
3188 /* 0 / non-Const = 0 */
3193 if (evaluated) { /* replace by tuple */
3197 mem = get_DivMod_mem(n);
3198 /* skip a potential Pin */
3199 mem = skip_Pin(mem);
3201 blk = get_nodes_block(n);
3202 turn_into_tuple(n, pn_DivMod_max);
3203 set_Tuple_pred(n, pn_DivMod_M, mem);
3204 set_Tuple_pred(n, pn_DivMod_X_regular, new_r_Jmp(blk));
3205 set_Tuple_pred(n, pn_DivMod_X_except, new_Bad()); /* no exception */
3206 set_Tuple_pred(n, pn_DivMod_res_div, va);
3207 set_Tuple_pred(n, pn_DivMod_res_mod, vb);
3211 } /* transform_node_DivMod */
3214 * Optimize x / c to x * (1/c)
3216 static ir_node *transform_node_Quot(ir_node *n)
3218 ir_mode *mode = get_Quot_resmode(n);
3221 if (get_mode_arithmetic(mode) == irma_ieee754) {
3222 ir_node *b = get_Quot_right(n);
3223 tarval *tv = value_of(b);
3225 if (tv != tarval_bad) {
3226 int rem = tarval_fp_ops_enabled();
3229 * Floating point constant folding might be disabled here to
3231 * However, as we check for exact result, doing it is safe.
3234 tarval_enable_fp_ops(1);
3235 tv = tarval_quo(get_mode_one(mode), tv);
3236 tarval_enable_fp_ops(rem);
3238 /* Do the transformation if the result is either exact or we are not
3239 using strict rules. */
3240 if (tv != tarval_bad &&
3241 (tarval_ieee754_get_exact() || (get_irg_fp_model(get_irn_irg(n)) & fp_strict_algebraic) == 0)) {
3242 ir_node *blk = get_nodes_block(n);
3243 ir_node *c = new_Const(tv);
3244 ir_node *a = get_Quot_left(n);
3245 ir_node *m = new_rd_Mul(get_irn_dbg_info(n), blk, a, c, mode);
3246 ir_node *mem = get_Quot_mem(n);
3248 /* skip a potential Pin */
3249 mem = skip_Pin(mem);
3250 turn_into_tuple(n, pn_Quot_max);
3251 set_Tuple_pred(n, pn_Quot_M, mem);
3252 set_Tuple_pred(n, pn_Quot_X_regular, new_r_Jmp(blk));
3253 set_Tuple_pred(n, pn_Quot_X_except, new_Bad());
3254 set_Tuple_pred(n, pn_Quot_res, m);
3255 DBG_OPT_ALGSIM1(oldn, a, b, m, FS_OPT_FP_INV_MUL);
3260 } /* transform_node_Quot */
3263 * Optimize -a CMP -b into b CMP a.
3264 * This works only for for modes where unary Minus
3266 * Note that two-complement integers can Overflow
3267 * so it will NOT work.
3269 * For == and != can be handled in Proj(Cmp)
3271 static ir_node *transform_node_Cmp(ir_node *n)
3274 ir_node *left = get_Cmp_left(n);
3275 ir_node *right = get_Cmp_right(n);
3277 if (is_Minus(left) && is_Minus(right) &&
3278 !mode_overflow_on_unary_Minus(get_irn_mode(left))) {
3279 ir_node *const new_left = get_Minus_op(right);
3280 ir_node *const new_right = get_Minus_op(left);
3281 n = new_rd_Cmp(get_irn_dbg_info(n), get_nodes_block(n), new_left, new_right);
3282 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CMP_OP_OP);
3285 } /* transform_node_Cmp */
3289 * Transform a Cond node.
3291 * Replace the Cond by a Jmp if it branches on a constant
3294 static ir_node *transform_node_Cond(ir_node *n)
3298 ir_node *a = get_Cond_selector(n);
3299 tarval *ta = value_of(a);
3300 ir_graph *irg = get_irn_irg(n);
3302 /* we need block info which is not available in floating irgs */
3303 if (get_irg_pinned(irg) == op_pin_state_floats)
3306 if ((ta != tarval_bad) &&
3307 (get_irn_mode(a) == mode_b) &&
3308 (get_opt_unreachable_code())) {
3309 /* It's a boolean Cond, branching on a boolean constant.
3310 Replace it by a tuple (Bad, Jmp) or (Jmp, Bad) */
3311 ir_node *blk = get_nodes_block(n);
3312 jmp = new_r_Jmp(blk);
3313 turn_into_tuple(n, pn_Cond_max);
3314 if (ta == tarval_b_true) {
3315 set_Tuple_pred(n, pn_Cond_false, new_Bad());
3316 set_Tuple_pred(n, pn_Cond_true, jmp);
3318 set_Tuple_pred(n, pn_Cond_false, jmp);
3319 set_Tuple_pred(n, pn_Cond_true, new_Bad());
3321 /* We might generate an endless loop, so keep it alive. */
3322 add_End_keepalive(get_irg_end(irg), blk);
3325 } /* transform_node_Cond */
3328 * Prototype of a recursive transform function
3329 * for bitwise distributive transformations.
3331 typedef ir_node* (*recursive_transform)(ir_node *n);
3334 * makes use of distributive laws for and, or, eor
3335 * and(a OP c, b OP c) -> and(a, b) OP c
3336 * note, might return a different op than n
3338 static ir_node *transform_bitwise_distributive(ir_node *n,
3339 recursive_transform trans_func)
3342 ir_node *a = get_binop_left(n);
3343 ir_node *b = get_binop_right(n);
3344 ir_op *op = get_irn_op(a);
3345 ir_op *op_root = get_irn_op(n);
3347 if (op != get_irn_op(b))
3350 /* and(conv(a), conv(b)) -> conv(and(a,b)) */
3351 if (op == op_Conv) {
3352 ir_node *a_op = get_Conv_op(a);
3353 ir_node *b_op = get_Conv_op(b);
3354 ir_mode *a_mode = get_irn_mode(a_op);
3355 ir_mode *b_mode = get_irn_mode(b_op);
3356 if (a_mode == b_mode && (mode_is_int(a_mode) || a_mode == mode_b)) {
3357 ir_node *blk = get_nodes_block(n);
3360 set_binop_left(n, a_op);
3361 set_binop_right(n, b_op);
3362 set_irn_mode(n, a_mode);
3364 n = new_r_Conv(blk, n, get_irn_mode(oldn));
3366 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
3372 /* nothing to gain here */
3376 if (op == op_Shrs || op == op_Shr || op == op_Shl
3377 || op == op_And || op == op_Or || op == op_Eor) {
3378 ir_node *a_left = get_binop_left(a);
3379 ir_node *a_right = get_binop_right(a);
3380 ir_node *b_left = get_binop_left(b);
3381 ir_node *b_right = get_binop_right(b);
3383 ir_node *op1 = NULL;
3384 ir_node *op2 = NULL;
3386 if (is_op_commutative(op)) {
3387 if (a_left == b_left) {
3391 } else if (a_left == b_right) {
3395 } else if (a_right == b_left) {
3401 if (a_right == b_right) {
3408 /* (a sop c) & (b sop c) => (a & b) sop c */
3409 ir_node *blk = get_nodes_block(n);
3411 ir_node *new_n = exact_copy(n);
3412 set_binop_left(new_n, op1);
3413 set_binop_right(new_n, op2);
3414 new_n = trans_func(new_n);
3416 if (op_root == op_Eor && op == op_Or) {
3417 dbg_info *dbgi = get_irn_dbg_info(n);
3418 ir_mode *mode = get_irn_mode(c);
3420 c = new_rd_Not(dbgi, blk, c, mode);
3421 n = new_rd_And(dbgi, blk, new_n, c, mode);
3424 set_nodes_block(n, blk);
3425 set_binop_left(n, new_n);
3426 set_binop_right(n, c);
3430 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_SHIFT_AND);
3441 static ir_node *transform_node_And(ir_node *n)
3443 ir_node *c, *oldn = n;
3444 ir_node *a = get_And_left(n);
3445 ir_node *b = get_And_right(n);
3447 vrp_attr *a_vrp, *b_vrp;
3449 mode = get_irn_mode(n);
3450 HANDLE_BINOP_PHI((eval_func) tarval_and, a, b, c, mode);
3452 /* we can evaluate 2 Projs of the same Cmp */
3453 if (mode == mode_b && is_Proj(a) && is_Proj(b)) {
3454 ir_node *pred_a = get_Proj_pred(a);
3455 ir_node *pred_b = get_Proj_pred(b);
3456 if (pred_a == pred_b) {
3457 dbg_info *dbgi = get_irn_dbg_info(n);
3458 pn_Cmp pn_a = get_Proj_proj(a);
3459 pn_Cmp pn_b = get_Proj_proj(b);
3460 /* yes, we can simply calculate with pncs */
3461 pn_Cmp new_pnc = pn_a & pn_b;
3463 return new_rd_Proj(dbgi, pred_a, mode_b, new_pnc);
3468 ir_node *op = get_Not_op(b);
3470 ir_node *ba = get_And_left(op);
3471 ir_node *bb = get_And_right(op);
3473 /* it's enough to test the following cases due to normalization! */
3474 if (get_Or_left(a) == ba && get_Or_right(a) == bb) {
3475 /* (a|b) & ~(a&b) = a^b */
3476 ir_node *block = get_nodes_block(n);
3478 n = new_rd_Eor(get_irn_dbg_info(n), block, ba, bb, mode);
3479 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_TO_EOR);
3487 ir_node *op = get_Not_op(a);
3489 ir_node *aa = get_And_left(op);
3490 ir_node *ab = get_And_right(op);
3492 /* it's enough to test the following cases due to normalization! */
3493 if (get_Or_left(b) == aa && get_Or_right(b) == ab) {
3494 /* (a|b) & ~(a&b) = a^b */
3495 ir_node *block = get_nodes_block(n);
3497 n = new_rd_Eor(get_irn_dbg_info(n), block, aa, ab, mode);
3498 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_TO_EOR);
3505 ir_node *al = get_Eor_left(a);
3506 ir_node *ar = get_Eor_right(a);
3509 /* (b ^ a) & b -> ~a & b */
3510 dbg_info *dbg = get_irn_dbg_info(n);
3511 ir_node *block = get_nodes_block(n);
3513 ar = new_rd_Not(dbg, block, ar, mode);
3514 n = new_rd_And(dbg, block, ar, b, mode);
3515 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3519 /* (a ^ b) & b -> ~a & b */
3520 dbg_info *dbg = get_irn_dbg_info(n);
3521 ir_node *block = get_nodes_block(n);
3523 al = new_rd_Not(dbg, block, al, mode);
3524 n = new_rd_And(dbg, block, al, b, mode);
3525 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3530 ir_node *bl = get_Eor_left(b);
3531 ir_node *br = get_Eor_right(b);
3534 /* a & (a ^ b) -> a & ~b */
3535 dbg_info *dbg = get_irn_dbg_info(n);
3536 ir_node *block = get_nodes_block(n);
3538 br = new_rd_Not(dbg, block, br, mode);
3539 n = new_rd_And(dbg, block, br, a, mode);
3540 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3544 /* a & (b ^ a) -> a & ~b */
3545 dbg_info *dbg = get_irn_dbg_info(n);
3546 ir_node *block = get_nodes_block(n);
3548 bl = new_rd_Not(dbg, block, bl, mode);
3549 n = new_rd_And(dbg, block, bl, a, mode);
3550 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3554 if (is_Not(a) && is_Not(b)) {
3555 /* ~a & ~b = ~(a|b) */
3556 ir_node *block = get_nodes_block(n);
3557 ir_mode *mode = get_irn_mode(n);
3561 n = new_rd_Or(get_irn_dbg_info(n), block, a, b, mode);
3562 n = new_rd_Not(get_irn_dbg_info(n), block, n, mode);
3563 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_DEMORGAN);
3567 b_vrp = vrp_get_info(b);
3568 if (is_Const(a) && b_vrp && (tarval_cmp(tarval_or(get_Const_tarval(a),
3569 b_vrp->bits_not_set), get_Const_tarval(a)) == pn_Cmp_Eq)) {
3575 a_vrp = vrp_get_info(a);
3576 if (is_Const(b) && a_vrp && (tarval_cmp(tarval_or(get_Const_tarval(b),
3577 a_vrp->bits_not_set), get_Const_tarval(b)) == pn_Cmp_Eq)) {
3581 n = transform_bitwise_distributive(n, transform_node_And);
3584 } /* transform_node_And */
3586 /* the order of the values is important! */
3587 typedef enum const_class {
3593 static const_class classify_const(const ir_node* n)
3595 if (is_Const(n)) return const_const;
3596 if (is_irn_constlike(n)) return const_like;
3601 * Determines whether r is more constlike or has a larger index (in that order)
3604 static bool operands_are_normalized(const ir_node *l, const ir_node *r)
3606 const const_class l_order = classify_const(l);
3607 const const_class r_order = classify_const(r);
3609 l_order > r_order ||
3610 (l_order == r_order && get_irn_idx(l) <= get_irn_idx(r));
3616 static ir_node *transform_node_Eor(ir_node *n)
3618 ir_node *c, *oldn = n;
3619 ir_node *a = get_Eor_left(n);
3620 ir_node *b = get_Eor_right(n);
3621 ir_mode *mode = get_irn_mode(n);
3623 HANDLE_BINOP_PHI((eval_func) tarval_eor, a, b, c, mode);
3625 /* we can evaluate 2 Projs of the same Cmp */
3626 if (mode == mode_b && is_Proj(a) && is_Proj(b)) {
3627 ir_node *pred_a = get_Proj_pred(a);
3628 ir_node *pred_b = get_Proj_pred(b);
3629 if (pred_a == pred_b) {
3630 dbg_info *dbgi = get_irn_dbg_info(n);
3631 pn_Cmp pn_a = get_Proj_proj(a);
3632 pn_Cmp pn_b = get_Proj_proj(b);
3633 /* yes, we can simply calculate with pncs */
3634 pn_Cmp new_pnc = pn_a ^ pn_b;
3636 return new_rd_Proj(dbgi, pred_a, mode_b, new_pnc);
3640 /* normalize not nodes... ~a ^ b <=> a ^ ~b */
3641 if (is_Not(a) && operands_are_normalized(get_Not_op(a), b)) {
3642 dbg_info *dbg = get_irn_dbg_info(n);
3643 ir_node *block = get_nodes_block(n);
3644 ir_node *new_not = new_rd_Not(dbg, block, b, mode);
3645 ir_node *new_left = get_Not_op(a);
3646 n = new_rd_Eor(dbg, block, new_left, new_not, mode);
3647 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3649 } else if (is_Not(b) && !operands_are_normalized(a, get_Not_op(b))) {
3650 dbg_info *dbg = get_irn_dbg_info(n);
3651 ir_node *block = get_nodes_block(n);
3652 ir_node *new_not = new_rd_Not(dbg, block, a, mode);
3653 ir_node *new_right = get_Not_op(b);
3654 n = new_rd_Eor(dbg, block, new_not, new_right, mode);
3655 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3659 /* x ^ 1...1 -> ~1 */
3660 if (is_Const(b) && is_Const_all_one(b)) {
3661 n = new_r_Not(get_nodes_block(n), a, mode);
3662 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3666 n = transform_bitwise_distributive(n, transform_node_Eor);
3668 } /* transform_node_Eor */
3673 static ir_node *transform_node_Not(ir_node *n)
3675 ir_node *c, *oldn = n;
3676 ir_node *a = get_Not_op(n);
3677 ir_mode *mode = get_irn_mode(n);
3679 HANDLE_UNOP_PHI(tarval_not,a,c);
3681 /* check for a boolean Not */
3682 if (mode == mode_b && is_Proj(a)) {
3683 ir_node *a_pred = get_Proj_pred(a);
3684 if (is_Cmp(a_pred)) {
3685 /* We negate a Cmp. The Cmp has the negated result anyways! */
3686 n = new_r_Proj(get_Proj_pred(a),
3687 mode_b, get_negated_pnc(get_Proj_proj(a), mode_b));
3688 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_CMP);
3693 /* normalize ~(a ^ b) => a ^ ~b */
3695 dbg_info *dbg = get_irn_dbg_info(n);
3696 ir_node *block = get_nodes_block(n);
3697 ir_node *eor_right = get_Eor_right(a);
3698 ir_node *eor_left = get_Eor_left(a);
3699 eor_right = new_rd_Not(dbg, block, eor_right, mode);
3700 n = new_rd_Eor(dbg, block, eor_left, eor_right, mode);
3704 if (get_mode_arithmetic(mode) == irma_twos_complement) {
3705 if (is_Minus(a)) { /* ~-x -> x + -1 */
3706 dbg_info *dbg = get_irn_dbg_info(n);
3707 ir_graph *irg = get_irn_irg(n);
3708 ir_node *block = get_nodes_block(n);
3709 ir_node *add_l = get_Minus_op(a);
3710 ir_node *add_r = new_rd_Const(dbg, irg, get_mode_minus_one(mode));
3711 n = new_rd_Add(dbg, block, add_l, add_r, mode);
3712 } else if (is_Add(a)) {
3713 ir_node *add_r = get_Add_right(a);
3714 if (is_Const(add_r) && is_Const_all_one(add_r)) {
3715 /* ~(x + -1) = -x */
3716 ir_node *op = get_Add_left(a);
3717 ir_node *blk = get_nodes_block(n);
3718 n = new_rd_Minus(get_irn_dbg_info(n), blk, op, get_irn_mode(n));
3719 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_MINUS_1);
3724 } /* transform_node_Not */
3727 * Transform a Minus.
3731 * -(a >>u (size-1)) = a >>s (size-1)
3732 * -(a >>s (size-1)) = a >>u (size-1)
3733 * -(a * const) -> a * -const
3735 static ir_node *transform_node_Minus(ir_node *n)
3737 ir_node *c, *oldn = n;
3738 ir_node *a = get_Minus_op(n);
3741 HANDLE_UNOP_PHI(tarval_neg,a,c);
3743 mode = get_irn_mode(a);
3744 if (get_mode_arithmetic(mode) == irma_twos_complement) {
3745 /* the following rules are only to twos-complement */
3748 ir_node *op = get_Not_op(a);
3749 tarval *tv = get_mode_one(mode);
3750 ir_node *blk = get_nodes_block(n);
3751 ir_node *c = new_Const(tv);
3752 n = new_rd_Add(get_irn_dbg_info(n), blk, op, c, mode);
3753 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_MINUS_NOT);
3757 ir_node *c = get_Shr_right(a);
3760 tarval *tv = get_Const_tarval(c);
3762 if (tarval_is_long(tv) && get_tarval_long(tv) == (int) get_mode_size_bits(mode) - 1) {
3763 /* -(a >>u (size-1)) = a >>s (size-1) */
3764 ir_node *v = get_Shr_left(a);
3766 n = new_rd_Shrs(get_irn_dbg_info(n), get_nodes_block(n), v, c, mode);
3767 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_PREDICATE);
3773 ir_node *c = get_Shrs_right(a);
3776 tarval *tv = get_Const_tarval(c);
3778 if (tarval_is_long(tv) && get_tarval_long(tv) == (int) get_mode_size_bits(mode) - 1) {
3779 /* -(a >>s (size-1)) = a >>u (size-1) */
3780 ir_node *v = get_Shrs_left(a);
3782 n = new_rd_Shr(get_irn_dbg_info(n), get_nodes_block(n), v, c, mode);
3783 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_PREDICATE);
3790 /* - (a-b) = b - a */
3791 ir_node *la = get_Sub_left(a);
3792 ir_node *ra = get_Sub_right(a);
3793 ir_node *blk = get_nodes_block(n);
3795 n = new_rd_Sub(get_irn_dbg_info(n), blk, ra, la, mode);
3796 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_MINUS_SUB);
3800 if (is_Mul(a)) { /* -(a * const) -> a * -const */
3801 ir_node *mul_l = get_Mul_left(a);
3802 ir_node *mul_r = get_Mul_right(a);
3803 tarval *tv = value_of(mul_r);
3804 if (tv != tarval_bad) {
3805 tv = tarval_neg(tv);
3806 if (tv != tarval_bad) {
3807 ir_node *cnst = new_Const(tv);
3808 dbg_info *dbg = get_irn_dbg_info(a);
3809 ir_node *block = get_nodes_block(a);
3810 n = new_rd_Mul(dbg, block, mul_l, cnst, mode);
3811 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_MINUS_MUL_C);
3818 } /* transform_node_Minus */
3821 * Transform a Cast_type(Const) into a new Const_type
3823 static ir_node *transform_node_Cast(ir_node *n)
3826 ir_node *pred = get_Cast_op(n);
3827 ir_type *tp = get_irn_type(n);
3829 if (is_Const(pred) && get_Const_type(pred) != tp) {
3830 ir_graph *irg = get_irn_irg(n);
3831 n = new_rd_Const_type(NULL, irg, get_Const_tarval(pred), tp);
3832 DBG_OPT_CSTEVAL(oldn, n);
3833 } else if (is_SymConst(pred) && get_SymConst_value_type(pred) != tp) {
3834 ir_graph *irg = get_irn_irg(n);
3835 n = new_rd_SymConst_type(NULL, irg, get_irn_mode(pred),
3836 get_SymConst_symbol(pred), get_SymConst_kind(pred), tp);
3837 DBG_OPT_CSTEVAL(oldn, n);
3841 } /* transform_node_Cast */
3844 * Transform a Proj(Load) with a non-null address.
3846 static ir_node *transform_node_Proj_Load(ir_node *proj)
3848 if (get_opt_ldst_only_null_ptr_exceptions()) {
3849 if (get_irn_mode(proj) == mode_X) {
3850 ir_node *load = get_Proj_pred(proj);
3852 /* get the Load address */
3853 const ir_node *addr = get_Load_ptr(load);
3854 const ir_node *confirm;
3856 if (value_not_null(addr, &confirm)) {
3857 if (confirm == NULL) {
3858 /* this node may float if it did not depend on a Confirm */
3859 set_irn_pinned(load, op_pin_state_floats);
3861 if (get_Proj_proj(proj) == pn_Load_X_except) {
3862 ir_graph *irg = get_irn_irg(proj);
3863 DBG_OPT_EXC_REM(proj);
3864 return get_irg_bad(irg);
3866 ir_node *blk = get_nodes_block(load);
3867 return new_r_Jmp(blk);
3873 } /* transform_node_Proj_Load */
3876 * Transform a Proj(Store) with a non-null address.
3878 static ir_node *transform_node_Proj_Store(ir_node *proj)
3880 if (get_opt_ldst_only_null_ptr_exceptions()) {
3881 if (get_irn_mode(proj) == mode_X) {
3882 ir_node *store = get_Proj_pred(proj);
3884 /* get the load/store address */
3885 const ir_node *addr = get_Store_ptr(store);
3886 const ir_node *confirm;
3888 if (value_not_null(addr, &confirm)) {
3889 if (confirm == NULL) {
3890 /* this node may float if it did not depend on a Confirm */
3891 set_irn_pinned(store, op_pin_state_floats);
3893 if (get_Proj_proj(proj) == pn_Store_X_except) {
3894 ir_graph *irg = get_irn_irg(proj);
3895 DBG_OPT_EXC_REM(proj);
3896 return get_irg_bad(irg);
3898 ir_node *blk = get_nodes_block(store);
3899 return new_r_Jmp(blk);
3905 } /* transform_node_Proj_Store */
3908 * Transform a Proj(Div) with a non-zero value.
3909 * Removes the exceptions and routes the memory to the NoMem node.
3911 static ir_node *transform_node_Proj_Div(ir_node *proj)
3913 ir_node *div = get_Proj_pred(proj);
3914 ir_node *b = get_Div_right(div);
3915 ir_node *res, *new_mem;
3916 const ir_node *confirm;
3919 if (value_not_zero(b, &confirm)) {
3920 /* div(x, y) && y != 0 */
3921 if (confirm == NULL) {
3922 /* we are sure we have a Const != 0 */
3923 new_mem = get_Div_mem(div);
3924 new_mem = skip_Pin(new_mem);
3925 set_Div_mem(div, new_mem);
3926 set_irn_pinned(div, op_pin_state_floats);
3929 proj_nr = get_Proj_proj(proj);
3931 case pn_Div_X_regular:
3932 return new_r_Jmp(get_nodes_block(div));
3934 case pn_Div_X_except:
3935 /* we found an exception handler, remove it */
3936 DBG_OPT_EXC_REM(proj);
3940 ir_graph *irg = get_irn_irg(proj);
3941 res = get_Div_mem(div);
3942 new_mem = get_irg_no_mem(irg);
3945 /* This node can only float up to the Confirm block */
3946 new_mem = new_r_Pin(get_nodes_block(confirm), new_mem);
3948 set_irn_pinned(div, op_pin_state_floats);
3949 /* this is a Div without exception, we can remove the memory edge */
3950 set_Div_mem(div, new_mem);
3956 } /* transform_node_Proj_Div */
3959 * Transform a Proj(Mod) with a non-zero value.
3960 * Removes the exceptions and routes the memory to the NoMem node.
3962 static ir_node *transform_node_Proj_Mod(ir_node *proj)
3964 ir_node *mod = get_Proj_pred(proj);
3965 ir_node *b = get_Mod_right(mod);
3966 ir_node *res, *new_mem;
3967 const ir_node *confirm;
3970 if (value_not_zero(b, &confirm)) {
3971 /* mod(x, y) && y != 0 */
3972 proj_nr = get_Proj_proj(proj);
3974 if (confirm == NULL) {
3975 /* we are sure we have a Const != 0 */
3976 new_mem = get_Mod_mem(mod);
3977 new_mem = skip_Pin(new_mem);
3978 set_Mod_mem(mod, new_mem);
3979 set_irn_pinned(mod, op_pin_state_floats);
3984 case pn_Mod_X_regular:
3985 return new_r_Jmp(get_irn_n(mod, -1));
3987 case pn_Mod_X_except:
3988 /* we found an exception handler, remove it */
3989 DBG_OPT_EXC_REM(proj);
3993 ir_graph *irg = get_irn_irg(proj);
3994 res = get_Mod_mem(mod);
3995 new_mem = get_irg_no_mem(irg);
3998 /* This node can only float up to the Confirm block */
3999 new_mem = new_r_Pin(get_nodes_block(confirm), new_mem);
4001 /* this is a Mod without exception, we can remove the memory edge */
4002 set_Mod_mem(mod, new_mem);
4006 if (get_Mod_left(mod) == b) {
4007 /* a % a = 0 if a != 0 */
4008 ir_mode *mode = get_irn_mode(proj);
4009 ir_node *res = new_Const(get_mode_null(mode));
4011 DBG_OPT_CSTEVAL(mod, res);
4017 } /* transform_node_Proj_Mod */
4020 * Transform a Proj(DivMod) with a non-zero value.
4021 * Removes the exceptions and routes the memory to the NoMem node.
4023 static ir_node *transform_node_Proj_DivMod(ir_node *proj)
4025 ir_node *divmod = get_Proj_pred(proj);
4026 ir_node *b = get_DivMod_right(divmod);
4027 ir_node *res, *new_mem;
4028 const ir_node *confirm;
4031 if (value_not_zero(b, &confirm)) {
4032 /* DivMod(x, y) && y != 0 */
4033 proj_nr = get_Proj_proj(proj);
4035 if (confirm == NULL) {
4036 /* we are sure we have a Const != 0 */
4037 new_mem = get_DivMod_mem(divmod);
4038 new_mem = skip_Pin(new_mem);
4039 set_DivMod_mem(divmod, new_mem);
4040 set_irn_pinned(divmod, op_pin_state_floats);
4045 case pn_DivMod_X_regular:
4046 return new_r_Jmp(get_nodes_block(divmod));
4048 case pn_DivMod_X_except:
4049 /* we found an exception handler, remove it */
4050 DBG_OPT_EXC_REM(proj);
4054 ir_graph *irg = get_irn_irg(proj);
4055 res = get_DivMod_mem(divmod);
4056 new_mem = get_irg_no_mem(irg);
4059 /* This node can only float up to the Confirm block */
4060 new_mem = new_r_Pin(get_nodes_block(confirm), new_mem);
4062 /* this is a DivMod without exception, we can remove the memory edge */
4063 set_DivMod_mem(divmod, new_mem);
4067 case pn_DivMod_res_mod:
4068 if (get_DivMod_left(divmod) == b) {
4069 /* a % a = 0 if a != 0 */
4070 ir_mode *mode = get_irn_mode(proj);
4071 ir_node *res = new_Const(get_mode_null(mode));
4073 DBG_OPT_CSTEVAL(divmod, res);
4079 } /* transform_node_Proj_DivMod */
4082 * Optimizes jump tables (CondIs or CondIu) by removing all impossible cases.
4084 static ir_node *transform_node_Proj_Cond(ir_node *proj)
4086 if (get_opt_unreachable_code()) {
4087 ir_node *n = get_Proj_pred(proj);
4088 ir_node *b = get_Cond_selector(n);
4090 if (mode_is_int(get_irn_mode(b))) {
4091 tarval *tb = value_of(b);
4093 if (tb != tarval_bad) {
4094 /* we have a constant switch */
4095 long num = get_Proj_proj(proj);
4097 if (num != get_Cond_default_proj(n)) { /* we cannot optimize default Proj's yet */
4098 if (get_tarval_long(tb) == num) {
4099 /* Do NOT create a jump here, or we will have 2 control flow ops
4100 * in a block. This case is optimized away in optimize_cf(). */
4103 ir_graph *irg = get_irn_irg(proj);
4104 /* this case will NEVER be taken, kill it */
4105 return get_irg_bad(irg);
4109 long num = get_Proj_proj(proj);
4110 vrp_attr *b_vrp = vrp_get_info(b);
4111 if (num != get_Cond_default_proj(n) && b_vrp) {
4112 /* Try handling with vrp data. We only remove dead parts. */
4113 tarval *tp = new_tarval_from_long(num, get_irn_mode(b));
4115 if (b_vrp->range_type == VRP_RANGE) {
4116 pn_Cmp cmp_result = tarval_cmp(b_vrp->range_bottom, tp);
4117 pn_Cmp cmp_result2 = tarval_cmp(b_vrp->range_top, tp);
4119 if ((cmp_result & pn_Cmp_Gt) == cmp_result && (cmp_result2
4120 & pn_Cmp_Lt) == cmp_result2) {
4121 ir_graph *irg = get_irn_irg(proj);
4122 return get_irg_bad(irg);
4124 } else if (b_vrp->range_type == VRP_ANTIRANGE) {
4125 pn_Cmp cmp_result = tarval_cmp(b_vrp->range_bottom, tp);
4126 pn_Cmp cmp_result2 = tarval_cmp(b_vrp->range_top, tp);
4128 if ((cmp_result & pn_Cmp_Le) == cmp_result && (cmp_result2
4129 & pn_Cmp_Ge) == cmp_result2) {
4130 ir_graph *irg = get_irn_irg(proj);
4131 return get_irg_bad(irg);
4136 tarval_and( b_vrp->bits_set, tp),
4139 ir_graph *irg = get_irn_irg(proj);
4140 return get_irg_bad(irg);
4146 tarval_not(b_vrp->bits_not_set)),
4147 tarval_not(b_vrp->bits_not_set))
4149 ir_graph *irg = get_irn_irg(proj);
4150 return get_irg_bad(irg);
4159 } /* transform_node_Proj_Cond */
4162 * Create a 0 constant of given mode.
4164 static ir_node *create_zero_const(ir_mode *mode)
4166 tarval *tv = get_mode_null(mode);
4167 ir_node *cnst = new_Const(tv);
4173 * Normalizes and optimizes Cmp nodes.
4175 static ir_node *transform_node_Proj_Cmp(ir_node *proj)
4177 ir_node *n = get_Proj_pred(proj);
4178 ir_node *left = get_Cmp_left(n);
4179 ir_node *right = get_Cmp_right(n);
4182 ir_mode *mode = NULL;
4183 long proj_nr = get_Proj_proj(proj);
4185 /* we can evaluate some cases directly */
4188 return new_Const(get_tarval_b_false());
4190 return new_Const(get_tarval_b_true());
4192 if (!mode_is_float(get_irn_mode(left)))
4193 return new_Const(get_tarval_b_true());
4199 /* remove Casts of both sides */
4200 left = skip_Cast(left);
4201 right = skip_Cast(right);
4203 /* Remove unnecessary conversions */
4204 /* TODO handle constants */
4205 if (is_Conv(left) && is_Conv(right)) {
4206 ir_mode *mode = get_irn_mode(left);
4207 ir_node *op_left = get_Conv_op(left);
4208 ir_node *op_right = get_Conv_op(right);
4209 ir_mode *mode_left = get_irn_mode(op_left);
4210 ir_mode *mode_right = get_irn_mode(op_right);
4212 if (smaller_mode(mode_left, mode) && smaller_mode(mode_right, mode)
4213 && mode_left != mode_b && mode_right != mode_b) {
4214 ir_node *block = get_nodes_block(n);
4216 if (mode_left == mode_right) {
4220 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV_CONV);
4221 } else if (smaller_mode(mode_left, mode_right)) {
4222 left = new_r_Conv(block, op_left, mode_right);
4225 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV);
4226 } else if (smaller_mode(mode_right, mode_left)) {
4228 right = new_r_Conv(block, op_right, mode_left);
4230 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV);
4235 /* remove operation on both sides if possible */
4236 if (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) {
4238 * The following operations are NOT safe for floating point operations, for instance
4239 * 1.0 + inf == 2.0 + inf, =/=> x == y
4241 if (mode_is_int(get_irn_mode(left))) {
4242 unsigned lop = get_irn_opcode(left);
4244 if (lop == get_irn_opcode(right)) {
4245 ir_node *ll, *lr, *rl, *rr;
4247 /* same operation on both sides, try to remove */
4251 /* ~a CMP ~b => a CMP b, -a CMP -b ==> a CMP b */
4252 left = get_unop_op(left);
4253 right = get_unop_op(right);
4255 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4258 ll = get_Add_left(left);
4259 lr = get_Add_right(left);
4260 rl = get_Add_left(right);
4261 rr = get_Add_right(right);
4264 /* X + a CMP X + b ==> a CMP b */
4268 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4269 } else if (ll == rr) {
4270 /* X + a CMP b + X ==> a CMP b */
4274 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4275 } else if (lr == rl) {
4276 /* a + X CMP X + b ==> a CMP b */
4280 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4281 } else if (lr == rr) {
4282 /* a + X CMP b + X ==> a CMP b */
4286 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4290 ll = get_Sub_left(left);
4291 lr = get_Sub_right(left);
4292 rl = get_Sub_left(right);
4293 rr = get_Sub_right(right);
4296 /* X - a CMP X - b ==> a CMP b */
4300 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4301 } else if (lr == rr) {
4302 /* a - X CMP b - X ==> a CMP b */
4306 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4310 if (get_Rotl_right(left) == get_Rotl_right(right)) {
4311 /* a ROTL X CMP b ROTL X ==> a CMP b */
4312 left = get_Rotl_left(left);
4313 right = get_Rotl_left(right);
4315 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4323 /* X+A == A, A+X == A, A-X == A -> X == 0 */
4324 if (is_Add(left) || is_Sub(left)) {
4325 ir_node *ll = get_binop_left(left);
4326 ir_node *lr = get_binop_right(left);
4328 if (lr == right && is_Add(left)) {
4335 right = create_zero_const(get_irn_mode(left));
4337 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4340 if (is_Add(right) || is_Sub(right)) {
4341 ir_node *rl = get_binop_left(right);
4342 ir_node *rr = get_binop_right(right);
4344 if (rr == left && is_Add(right)) {
4351 right = create_zero_const(get_irn_mode(left));
4353 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4356 if (is_And(left) && is_Const(right)) {
4357 ir_node *ll = get_binop_left(left);
4358 ir_node *lr = get_binop_right(left);
4359 if (is_Shr(ll) && is_Const(lr)) {
4360 /* Cmp((x >>u c1) & c2, c3) = Cmp(x & (c2 << c1), c3 << c1) */
4361 ir_node *block = get_nodes_block(n);
4362 ir_mode *mode = get_irn_mode(left);
4364 ir_node *llr = get_Shr_right(ll);
4365 if (is_Const(llr)) {
4366 dbg_info *dbg = get_irn_dbg_info(left);
4368 tarval *c1 = get_Const_tarval(llr);
4369 tarval *c2 = get_Const_tarval(lr);
4370 tarval *c3 = get_Const_tarval(right);
4371 tarval *mask = tarval_shl(c2, c1);
4372 tarval *value = tarval_shl(c3, c1);
4374 left = new_rd_And(dbg, block, get_Shr_left(ll), new_Const(mask), mode);
4375 right = new_Const(value);
4380 /* Cmp(Eor(x, y), 0) <=> Cmp(x, y) at least for the ==0,!=0
4382 if (is_Const(right) && is_Const_null(right) && is_Eor(left)) {
4383 right = get_Eor_right(left);
4384 left = get_Eor_left(left);
4387 } /* mode_is_int(...) */
4388 } /* proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg */
4390 /* replace mode_b compares with ands/ors */
4391 if (get_irn_mode(left) == mode_b) {
4392 ir_node *block = get_nodes_block(n);
4396 case pn_Cmp_Le: bres = new_r_Or( block, new_r_Not(block, left, mode_b), right, mode_b); break;
4397 case pn_Cmp_Lt: bres = new_r_And(block, new_r_Not(block, left, mode_b), right, mode_b); break;
4398 case pn_Cmp_Ge: bres = new_r_Or( block, left, new_r_Not(block, right, mode_b), mode_b); break;
4399 case pn_Cmp_Gt: bres = new_r_And(block, left, new_r_Not(block, right, mode_b), mode_b); break;
4400 case pn_Cmp_Lg: bres = new_r_Eor(block, left, right, mode_b); break;
4401 case pn_Cmp_Eq: bres = new_r_Not(block, new_r_Eor(block, left, right, mode_b), mode_b); break;
4402 default: bres = NULL;
4405 DBG_OPT_ALGSIM0(n, bres, FS_OPT_CMP_TO_BOOL);
4411 * First step: normalize the compare op
4412 * by placing the constant on the right side
4413 * or moving the lower address node to the left.
4415 if (!operands_are_normalized(left, right)) {
4421 proj_nr = get_inversed_pnc(proj_nr);
4426 * Second step: Try to reduce the magnitude
4427 * of a constant. This may help to generate better code
4428 * later and may help to normalize more compares.
4429 * Of course this is only possible for integer values.
4431 tv = value_of(right);
4432 if (tv != tarval_bad) {
4433 mode = get_irn_mode(right);
4435 /* TODO extend to arbitrary constants */
4436 if (is_Conv(left) && tarval_is_null(tv)) {
4437 ir_node *op = get_Conv_op(left);
4438 ir_mode *op_mode = get_irn_mode(op);
4441 * UpConv(x) REL 0 ==> x REL 0
4442 * Don't do this for float values as it's unclear whether it is a
4443 * win. (on the other side it makes detection/creation of fabs hard)
4445 if (get_mode_size_bits(mode) > get_mode_size_bits(op_mode) &&
4446 ((proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) ||
4447 mode_is_signed(mode) || !mode_is_signed(op_mode)) &&
4448 !mode_is_float(mode)) {
4449 tv = get_mode_null(op_mode);
4453 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV);
4457 if (tv != tarval_bad) {
4458 /* the following optimization is possible on modes without Overflow
4459 * on Unary Minus or on == and !=:
4460 * -a CMP c ==> a swap(CMP) -c
4462 * Beware: for two-complement Overflow may occur, so only == and != can
4463 * be optimized, see this:
4464 * -MININT < 0 =/=> MININT > 0 !!!
4466 if (is_Minus(left) &&
4467 (!mode_overflow_on_unary_Minus(mode) ||
4468 (mode_is_int(mode) && (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg)))) {
4469 tv = tarval_neg(tv);
4471 if (tv != tarval_bad) {
4472 left = get_Minus_op(left);
4473 proj_nr = get_inversed_pnc(proj_nr);
4475 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4477 } else if (is_Not(left) && (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg)) {
4478 /* Not(a) ==/!= c ==> a ==/!= Not(c) */
4479 tv = tarval_not(tv);
4481 if (tv != tarval_bad) {
4482 left = get_Not_op(left);
4484 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4488 /* for integer modes, we have more */
4489 if (mode_is_int(mode)) {
4490 /* Ne includes Unordered which is not possible on integers.
4491 * However, frontends often use this wrong, so fix it here */
4492 if (proj_nr & pn_Cmp_Uo) {
4493 proj_nr &= ~pn_Cmp_Uo;
4494 set_Proj_proj(proj, proj_nr);
4497 /* c > 0 : a < c ==> a <= (c-1) a >= c ==> a > (c-1) */
4498 if ((proj_nr == pn_Cmp_Lt || proj_nr == pn_Cmp_Ge) &&
4499 tarval_cmp(tv, get_mode_null(mode)) == pn_Cmp_Gt) {
4500 tv = tarval_sub(tv, get_mode_one(mode), NULL);
4502 if (tv != tarval_bad) {
4503 proj_nr ^= pn_Cmp_Eq;
4505 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CNST_MAGN);
4508 /* c < 0 : a > c ==> a >= (c+1) a <= c ==> a < (c+1) */
4509 else if ((proj_nr == pn_Cmp_Gt || proj_nr == pn_Cmp_Le) &&
4510 tarval_cmp(tv, get_mode_null(mode)) == pn_Cmp_Lt) {
4511 tv = tarval_add(tv, get_mode_one(mode));
4513 if (tv != tarval_bad) {
4514 proj_nr ^= pn_Cmp_Eq;
4516 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CNST_MAGN);
4520 /* the following reassociations work only for == and != */
4521 if (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) {
4523 #if 0 /* Might be not that good in general */
4524 /* a-b == 0 ==> a == b, a-b != 0 ==> a != b */
4525 if (tarval_is_null(tv) && is_Sub(left)) {
4526 right = get_Sub_right(left);
4527 left = get_Sub_left(left);
4529 tv = value_of(right);
4531 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4535 if (tv != tarval_bad) {
4536 /* a-c1 == c2 ==> a == c2+c1, a-c1 != c2 ==> a != c2+c1 */
4538 ir_node *c1 = get_Sub_right(left);
4539 tarval *tv2 = value_of(c1);
4541 if (tv2 != tarval_bad) {
4542 tv2 = tarval_add(tv, value_of(c1));
4544 if (tv2 != tarval_bad) {
4545 left = get_Sub_left(left);
4548 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4552 /* a+c1 == c2 ==> a == c2-c1, a+c1 != c2 ==> a != c2-c1 */
4553 else if (is_Add(left)) {
4554 ir_node *a_l = get_Add_left(left);
4555 ir_node *a_r = get_Add_right(left);
4559 if (is_Const(a_l)) {
4561 tv2 = value_of(a_l);
4564 tv2 = value_of(a_r);
4567 if (tv2 != tarval_bad) {
4568 tv2 = tarval_sub(tv, tv2, NULL);
4570 if (tv2 != tarval_bad) {
4574 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4578 /* -a == c ==> a == -c, -a != c ==> a != -c */
4579 else if (is_Minus(left)) {
4580 tarval *tv2 = tarval_sub(get_mode_null(mode), tv, NULL);
4582 if (tv2 != tarval_bad) {
4583 left = get_Minus_op(left);
4586 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4593 if (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) {
4594 switch (get_irn_opcode(left)) {
4598 c1 = get_And_right(left);
4601 * And(x, C1) == C2 ==> FALSE if C2 & C1 != C2
4602 * And(x, C1) != C2 ==> TRUE if C2 & C1 != C2
4604 tarval *mask = tarval_and(get_Const_tarval(c1), tv);
4606 /* TODO: move to constant evaluation */
4607 tv = proj_nr == pn_Cmp_Eq ? get_tarval_b_false() : get_tarval_b_true();
4609 DBG_OPT_CSTEVAL(proj, c1);
4613 if (tarval_is_single_bit(tv)) {
4615 * optimization for AND:
4617 * And(x, C) == C ==> And(x, C) != 0
4618 * And(x, C) != C ==> And(X, C) == 0
4620 * if C is a single Bit constant.
4623 /* check for Constant's match. We have check hare the tarvals,
4624 because our const might be changed */
4625 if (get_Const_tarval(c1) == tv) {
4626 /* fine: do the transformation */
4627 tv = get_mode_null(get_tarval_mode(tv));
4628 proj_nr ^= pn_Cmp_Leg;
4630 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CNST_MAGN);
4636 c1 = get_Or_right(left);
4637 if (is_Const(c1) && tarval_is_null(tv)) {
4639 * Or(x, C) == 0 && C != 0 ==> FALSE
4640 * Or(x, C) != 0 && C != 0 ==> TRUE
4642 if (! tarval_is_null(get_Const_tarval(c1))) {
4643 /* TODO: move to constant evaluation */
4644 tv = proj_nr == pn_Cmp_Eq ? get_tarval_b_false() : get_tarval_b_true();
4646 DBG_OPT_CSTEVAL(proj, c1);
4653 * optimize x << c1 == c into x & (-1 >>u c1) == c >> c1 if c & (-1 << c1) == c
4655 * optimize x << c1 != c into x & (-1 >>u c1) != c >> c1 if c & (-1 << c1) == c
4658 c1 = get_Shl_right(left);
4660 tarval *tv1 = get_Const_tarval(c1);
4661 ir_mode *mode = get_irn_mode(left);
4662 tarval *minus1 = get_mode_all_one(mode);
4663 tarval *amask = tarval_shr(minus1, tv1);
4664 tarval *cmask = tarval_shl(minus1, tv1);
4667 if (tarval_and(tv, cmask) != tv) {
4668 /* condition not met */
4669 tv = proj_nr == pn_Cmp_Eq ? get_tarval_b_false() : get_tarval_b_true();
4671 DBG_OPT_CSTEVAL(proj, c1);
4674 sl = get_Shl_left(left);
4675 blk = get_nodes_block(n);
4676 left = new_rd_And(get_irn_dbg_info(left), blk, sl, new_Const(amask), mode);
4677 tv = tarval_shr(tv, tv1);
4679 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_SHF_TO_AND);
4684 * optimize x >>u c1 == c into x & (-1 << c1) == c << c1 if c & (-1 >>u c1) == c
4686 * optimize x >>u c1 != c into x & (-1 << c1) != c << c1 if c & (-1 >>u c1) == c
4689 c1 = get_Shr_right(left);
4691 tarval *tv1 = get_Const_tarval(c1);
4692 ir_mode *mode = get_irn_mode(left);
4693 tarval *minus1 = get_mode_all_one(mode);
4694 tarval *amask = tarval_shl(minus1, tv1);
4695 tarval *cmask = tarval_shr(minus1, tv1);
4698 if (tarval_and(tv, cmask) != tv) {
4699 /* condition not met */
4700 tv = proj_nr == pn_Cmp_Eq ? get_tarval_b_false() : get_tarval_b_true();
4702 DBG_OPT_CSTEVAL(proj, c1);
4705 sl = get_Shr_left(left);
4706 blk = get_nodes_block(n);
4707 left = new_rd_And(get_irn_dbg_info(left), blk, sl, new_Const(amask), mode);
4708 tv = tarval_shl(tv, tv1);
4710 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_SHF_TO_AND);
4715 * optimize x >>s c1 == c into x & (-1 << c1) == c << c1 if (c >>s (BITS - c1)) \in {0,-1}
4717 * optimize x >>s c1 != c into x & (-1 << c1) != c << c1 if (c >>s (BITS - c1)) \in {0,-1}
4720 c1 = get_Shrs_right(left);
4722 tarval *tv1 = get_Const_tarval(c1);
4723 ir_mode *mode = get_irn_mode(left);
4724 tarval *minus1 = get_mode_all_one(mode);
4725 tarval *amask = tarval_shl(minus1, tv1);
4726 tarval *cond = new_tarval_from_long(get_mode_size_bits(mode), get_tarval_mode(tv1));
4729 cond = tarval_sub(cond, tv1, NULL);
4730 cond = tarval_shrs(tv, cond);
4732 if (!tarval_is_all_one(cond) && !tarval_is_null(cond)) {
4733 /* condition not met */
4734 tv = proj_nr == pn_Cmp_Eq ? get_tarval_b_false() : get_tarval_b_true();
4736 DBG_OPT_CSTEVAL(proj, c1);
4739 sl = get_Shrs_left(left);
4740 blk = get_nodes_block(n);
4741 left = new_rd_And(get_irn_dbg_info(left), blk, sl, new_Const(amask), mode);
4742 tv = tarval_shl(tv, tv1);
4744 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_SHF_TO_AND);
4749 } /* tarval != bad */
4752 if (changed & 2) /* need a new Const */
4753 right = new_Const(tv);
4755 if ((proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) && is_Const(right) && is_Const_null(right) && is_Proj(left)) {
4756 ir_node *op = get_Proj_pred(left);
4758 if ((is_Mod(op) && get_Proj_proj(left) == pn_Mod_res) ||
4759 (is_DivMod(op) && get_Proj_proj(left) == pn_DivMod_res_mod)) {
4760 ir_node *c = get_binop_right(op);
4763 tarval *tv = get_Const_tarval(c);
4765 if (tarval_is_single_bit(tv)) {
4766 /* special case: (x % 2^n) CMP 0 ==> x & (2^n-1) CMP 0 */
4767 ir_node *v = get_binop_left(op);
4768 ir_node *blk = get_irn_n(op, -1);
4769 ir_mode *mode = get_irn_mode(v);
4771 tv = tarval_sub(tv, get_mode_one(mode), NULL);
4772 left = new_rd_And(get_irn_dbg_info(op), blk, v, new_Const(tv), mode);
4774 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_MOD_TO_AND);
4781 ir_node *block = get_nodes_block(n);
4783 /* create a new compare */
4784 n = new_rd_Cmp(get_irn_dbg_info(n), block, left, right);
4785 proj = new_rd_Proj(get_irn_dbg_info(proj), n, get_irn_mode(proj), proj_nr);
4789 } /* transform_node_Proj_Cmp */
4792 * Optimize CopyB(mem, x, x) into a Nop.
4794 static ir_node *transform_node_Proj_CopyB(ir_node *proj)
4796 ir_node *copyb = get_Proj_pred(proj);
4797 ir_node *a = get_CopyB_dst(copyb);
4798 ir_node *b = get_CopyB_src(copyb);
4801 switch (get_Proj_proj(proj)) {
4802 case pn_CopyB_X_regular:
4803 /* Turn CopyB into a tuple (mem, jmp, bad, bad) */
4804 DBG_OPT_EXC_REM(proj);
4805 proj = new_r_Jmp(get_nodes_block(copyb));
4807 case pn_CopyB_X_except:
4808 DBG_OPT_EXC_REM(proj);
4809 proj = get_irg_bad(get_irn_irg(proj));
4816 } /* transform_node_Proj_CopyB */
4819 * Optimize Bounds(idx, idx, upper) into idx.
4821 static ir_node *transform_node_Proj_Bound(ir_node *proj)
4823 ir_node *oldn = proj;
4824 ir_node *bound = get_Proj_pred(proj);
4825 ir_node *idx = get_Bound_index(bound);
4826 ir_node *pred = skip_Proj(idx);
4829 if (idx == get_Bound_lower(bound))
4831 else if (is_Bound(pred)) {
4833 * idx was Bounds checked in the same MacroBlock previously,
4834 * it is still valid if lower <= pred_lower && pred_upper <= upper.
4836 ir_node *lower = get_Bound_lower(bound);
4837 ir_node *upper = get_Bound_upper(bound);
4838 if (get_Bound_lower(pred) == lower &&
4839 get_Bound_upper(pred) == upper &&
4840 get_irn_MacroBlock(bound) == get_irn_MacroBlock(pred)) {
4842 * One could expect that we simply return the previous
4843 * Bound here. However, this would be wrong, as we could
4844 * add an exception Proj to a new location then.
4845 * So, we must turn in into a tuple.
4851 /* Turn Bound into a tuple (mem, jmp, bad, idx) */
4852 switch (get_Proj_proj(proj)) {
4854 DBG_OPT_EXC_REM(proj);
4855 proj = get_Bound_mem(bound);
4857 case pn_Bound_X_except:
4858 DBG_OPT_EXC_REM(proj);
4859 proj = get_irg_bad(get_irn_irg(proj));
4863 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NOP);
4865 case pn_Bound_X_regular:
4866 DBG_OPT_EXC_REM(proj);
4867 proj = new_r_Jmp(get_nodes_block(bound));
4874 } /* transform_node_Proj_Bound */
4877 * Does all optimizations on nodes that must be done on it's Proj's
4878 * because of creating new nodes.
4880 static ir_node *transform_node_Proj(ir_node *proj)
4882 ir_node *n = get_Proj_pred(proj);
4884 if (n->op->ops.transform_node_Proj)
4885 return n->op->ops.transform_node_Proj(proj);
4887 } /* transform_node_Proj */
4890 * Move Confirms down through Phi nodes.
4892 static ir_node *transform_node_Phi(ir_node *phi)
4895 ir_mode *mode = get_irn_mode(phi);
4897 if (mode_is_reference(mode)) {
4898 n = get_irn_arity(phi);
4900 /* Beware of Phi0 */
4902 ir_node *pred = get_irn_n(phi, 0);
4903 ir_node *bound, *new_Phi, *block, **in;
4906 if (! is_Confirm(pred))
4909 bound = get_Confirm_bound(pred);
4910 pnc = get_Confirm_cmp(pred);
4912 NEW_ARR_A(ir_node *, in, n);
4913 in[0] = get_Confirm_value(pred);
4915 for (i = 1; i < n; ++i) {
4916 pred = get_irn_n(phi, i);
4918 if (! is_Confirm(pred) ||
4919 get_Confirm_bound(pred) != bound ||
4920 get_Confirm_cmp(pred) != pnc)
4922 in[i] = get_Confirm_value(pred);
4924 /* move the Confirm nodes "behind" the Phi */
4925 block = get_irn_n(phi, -1);
4926 new_Phi = new_r_Phi(block, n, in, get_irn_mode(phi));
4927 return new_r_Confirm(block, new_Phi, bound, pnc);
4931 } /* transform_node_Phi */
4934 * Returns the operands of a commutative bin-op, if one operand is
4935 * a const, it is returned as the second one.
4937 static void get_comm_Binop_Ops(ir_node *binop, ir_node **a, ir_node **c)
4939 ir_node *op_a = get_binop_left(binop);
4940 ir_node *op_b = get_binop_right(binop);
4942 assert(is_op_commutative(get_irn_op(binop)));
4944 if (is_Const(op_a)) {
4951 } /* get_comm_Binop_Ops */
4954 * Optimize a Or(And(Or(And(v,c4),c3),c2),c1) pattern if possible.
4955 * Such pattern may arise in bitfield stores.
4957 * value c4 value c4 & c2
4958 * AND c3 AND c1 | c3
4965 * AND c1 ===> OR if (c1 | c2) == 0x111..11
4968 static ir_node *transform_node_Or_bf_store(ir_node *or)
4972 ir_node *and_l, *c3;
4973 ir_node *value, *c4;
4974 ir_node *new_and, *new_const, *block;
4975 ir_mode *mode = get_irn_mode(or);
4977 tarval *tv1, *tv2, *tv3, *tv4, *tv;
4980 get_comm_Binop_Ops(or, &and, &c1);
4981 if (!is_Const(c1) || !is_And(and))
4984 get_comm_Binop_Ops(and, &or_l, &c2);
4988 tv1 = get_Const_tarval(c1);
4989 tv2 = get_Const_tarval(c2);
4991 tv = tarval_or(tv1, tv2);
4992 if (tarval_is_all_one(tv)) {
4993 /* the AND does NOT clear a bit with isn't set by the OR */
4994 set_Or_left(or, or_l);
4995 set_Or_right(or, c1);
4997 /* check for more */
5004 get_comm_Binop_Ops(or_l, &and_l, &c3);
5005 if (!is_Const(c3) || !is_And(and_l))
5008 get_comm_Binop_Ops(and_l, &value, &c4);
5012 /* ok, found the pattern, check for conditions */
5013 assert(mode == get_irn_mode(and));
5014 assert(mode == get_irn_mode(or_l));
5015 assert(mode == get_irn_mode(and_l));
5017 tv3 = get_Const_tarval(c3);
5018 tv4 = get_Const_tarval(c4);
5020 tv = tarval_or(tv4, tv2);
5021 if (!tarval_is_all_one(tv)) {
5022 /* have at least one 0 at the same bit position */
5026 if (tv3 != tarval_andnot(tv3, tv4)) {
5027 /* bit in the or_mask is outside the and_mask */
5031 if (tv1 != tarval_andnot(tv1, tv2)) {
5032 /* bit in the or_mask is outside the and_mask */
5036 /* ok, all conditions met */
5037 block = get_irn_n(or, -1);
5039 new_and = new_r_And(block, value, new_Const(tarval_and(tv4, tv2)), mode);
5041 new_const = new_Const(tarval_or(tv3, tv1));
5043 set_Or_left(or, new_and);
5044 set_Or_right(or, new_const);
5046 /* check for more */
5048 } /* transform_node_Or_bf_store */
5051 * Optimize an Or(shl(x, c), shr(x, bits - c)) into a Rotl
5053 static ir_node *transform_node_Or_Rotl(ir_node *or)
5055 ir_mode *mode = get_irn_mode(or);
5056 ir_node *shl, *shr, *block;
5057 ir_node *irn, *x, *c1, *c2, *n;
5060 /* some backends can't handle rotl */
5061 if (!be_get_backend_param()->support_rotl)
5064 if (! mode_is_int(mode))
5067 shl = get_binop_left(or);
5068 shr = get_binop_right(or);
5077 } else if (!is_Shl(shl)) {
5079 } else if (!is_Shr(shr)) {
5082 x = get_Shl_left(shl);
5083 if (x != get_Shr_left(shr))
5086 c1 = get_Shl_right(shl);
5087 c2 = get_Shr_right(shr);
5088 if (is_Const(c1) && is_Const(c2)) {
5089 tv1 = get_Const_tarval(c1);
5090 if (! tarval_is_long(tv1))
5093 tv2 = get_Const_tarval(c2);
5094 if (! tarval_is_long(tv2))
5097 if (get_tarval_long(tv1) + get_tarval_long(tv2)
5098 != (int) get_mode_size_bits(mode))
5101 /* yet, condition met */
5102 block = get_nodes_block(or);
5104 n = new_r_Rotl(block, x, c1, mode);
5106 DBG_OPT_ALGSIM1(or, shl, shr, n, FS_OPT_OR_SHFT_TO_ROTL);
5110 /* Note: the obvious rot formulation (a << x) | (a >> (32-x)) gets
5111 * transformed to (a << x) | (a >> -x) by transform_node_shift_modulo() */
5112 if (!is_negated_value(c1, c2)) {
5116 /* yet, condition met */
5117 block = get_nodes_block(or);
5118 n = new_r_Rotl(block, x, c1, mode);
5119 DBG_OPT_ALGSIM0(or, n, FS_OPT_OR_SHFT_TO_ROTL);
5121 } /* transform_node_Or_Rotl */
5126 static ir_node *transform_node_Or(ir_node *n)
5128 ir_node *c, *oldn = n;
5129 ir_node *a = get_Or_left(n);
5130 ir_node *b = get_Or_right(n);
5133 if (is_Not(a) && is_Not(b)) {
5134 /* ~a | ~b = ~(a&b) */
5135 ir_node *block = get_nodes_block(n);
5137 mode = get_irn_mode(n);
5140 n = new_rd_And(get_irn_dbg_info(n), block, a, b, mode);
5141 n = new_rd_Not(get_irn_dbg_info(n), block, n, mode);
5142 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_DEMORGAN);
5146 /* we can evaluate 2 Projs of the same Cmp */
5147 if (get_irn_mode(n) == mode_b && is_Proj(a) && is_Proj(b)) {
5148 ir_node *pred_a = get_Proj_pred(a);
5149 ir_node *pred_b = get_Proj_pred(b);
5150 if (pred_a == pred_b) {
5151 dbg_info *dbgi = get_irn_dbg_info(n);
5152 pn_Cmp pn_a = get_Proj_proj(a);
5153 pn_Cmp pn_b = get_Proj_proj(b);
5154 /* yes, we can simply calculate with pncs */
5155 pn_Cmp new_pnc = pn_a | pn_b;
5157 return new_rd_Proj(dbgi, pred_a, mode_b, new_pnc);
5161 mode = get_irn_mode(n);
5162 HANDLE_BINOP_PHI((eval_func) tarval_or, a, b, c, mode);
5164 n = transform_node_Or_bf_store(n);
5165 n = transform_node_Or_Rotl(n);
5169 n = transform_bitwise_distributive(n, transform_node_Or);
5172 } /* transform_node_Or */
5176 static ir_node *transform_node(ir_node *n);
5179 * Optimize (a >> c1) >> c2), works for Shr, Shrs, Shl, Rotl.
5181 * Should be moved to reassociation?
5183 static ir_node *transform_node_shift(ir_node *n)
5185 ir_node *left, *right;
5187 tarval *tv1, *tv2, *res;
5188 ir_node *in[2], *irn, *block;
5190 left = get_binop_left(n);
5192 /* different operations */
5193 if (get_irn_op(left) != get_irn_op(n))
5196 right = get_binop_right(n);
5197 tv1 = value_of(right);
5198 if (tv1 == tarval_bad)
5201 tv2 = value_of(get_binop_right(left));
5202 if (tv2 == tarval_bad)
5205 res = tarval_add(tv1, tv2);
5206 mode = get_irn_mode(n);
5208 /* beware: a simple replacement works only, if res < modulo shift */
5210 int modulo_shf = get_mode_modulo_shift(mode);
5211 if (modulo_shf > 0) {
5212 tarval *modulo = new_tarval_from_long(modulo_shf,
5213 get_tarval_mode(res));
5215 assert(modulo_shf >= (int) get_mode_size_bits(mode));
5217 /* shifting too much */
5218 if (!(tarval_cmp(res, modulo) & pn_Cmp_Lt)) {
5220 ir_node *block = get_nodes_block(n);
5221 dbg_info *dbgi = get_irn_dbg_info(n);
5222 ir_mode *smode = get_irn_mode(right);
5223 ir_node *cnst = new_Const_long(smode, get_mode_size_bits(mode) - 1);
5224 return new_rd_Shrs(dbgi, block, get_binop_left(left), cnst, mode);
5227 return new_Const(get_mode_null(mode));
5231 res = tarval_mod(res, new_tarval_from_long(get_mode_size_bits(mode), get_tarval_mode(res)));
5234 /* ok, we can replace it */
5235 block = get_nodes_block(n);
5237 in[0] = get_binop_left(left);
5238 in[1] = new_Const(res);
5240 irn = new_ir_node(NULL, get_Block_irg(block), block, get_irn_op(n), mode, 2, in);
5242 DBG_OPT_ALGSIM0(n, irn, FS_OPT_REASSOC_SHIFT);
5244 return transform_node(irn);
5245 } /* transform_node_shift */
5248 * normalisation: (x & c1) >> c2 to (x >> c2) & (c1 >> c2)
5250 * - and, or, xor instead of &
5251 * - Shl, Shr, Shrs, rotl instead of >>
5252 * (with a special case for Or/Xor + Shrs)
5254 static ir_node *transform_node_bitop_shift(ir_node *n)
5257 ir_node *right = get_binop_right(n);
5258 ir_mode *mode = get_irn_mode(n);
5259 ir_node *bitop_left;
5260 ir_node *bitop_right;
5271 assert(is_Shrs(n) || is_Shr(n) || is_Shl(n) || is_Rotl(n));
5273 if (!is_Const(right))
5276 left = get_binop_left(n);
5277 op_left = get_irn_op(left);
5278 if (op_left != op_And && op_left != op_Or && op_left != op_Eor)
5281 /* doing it with Shrs is not legal if the Or/Eor affects the topmost bit */
5282 if (is_Shrs(n) && (op_left == op_Or || op_left == op_Eor)) {
5283 /* TODO: test if sign bit is affectes */
5287 bitop_right = get_binop_right(left);
5288 if (!is_Const(bitop_right))
5291 bitop_left = get_binop_left(left);
5293 block = get_nodes_block(n);
5294 dbgi = get_irn_dbg_info(n);
5295 tv1 = get_Const_tarval(bitop_right);
5296 tv2 = get_Const_tarval(right);
5298 assert(get_tarval_mode(tv1) == mode);
5301 new_shift = new_rd_Shl(dbgi, block, bitop_left, right, mode);
5302 tv_shift = tarval_shl(tv1, tv2);
5303 } else if (is_Shr(n)) {
5304 new_shift = new_rd_Shr(dbgi, block, bitop_left, right, mode);
5305 tv_shift = tarval_shr(tv1, tv2);
5306 } else if (is_Shrs(n)) {
5307 new_shift = new_rd_Shrs(dbgi, block, bitop_left, right, mode);
5308 tv_shift = tarval_shrs(tv1, tv2);
5311 new_shift = new_rd_Rotl(dbgi, block, bitop_left, right, mode);
5312 tv_shift = tarval_rotl(tv1, tv2);
5315 assert(get_tarval_mode(tv_shift) == mode);
5316 new_const = new_Const(tv_shift);
5318 if (op_left == op_And) {
5319 new_bitop = new_rd_And(dbgi, block, new_shift, new_const, mode);
5320 } else if (op_left == op_Or) {
5321 new_bitop = new_rd_Or(dbgi, block, new_shift, new_const, mode);
5323 assert(op_left == op_Eor);
5324 new_bitop = new_rd_Eor(dbgi, block, new_shift, new_const, mode);
5332 * (x << c1) >> c2 <=> x OP (c2-c1) & ((-1 << c1) >> c2)
5334 * (x >> c1) << c2 <=> x OP (c2-c1) & ((-1 >> c1) << c2)
5335 * (also with x >>s c1 when c1>=c2)
5337 static ir_node *transform_node_shl_shr(ir_node *n)
5340 ir_node *right = get_binop_right(n);
5355 assert(is_Shl(n) || is_Shr(n) || is_Shrs(n));
5357 if (!is_Const(right))
5360 left = get_binop_left(n);
5361 mode = get_irn_mode(n);
5362 if (is_Shl(n) && (is_Shr(left) || is_Shrs(left))) {
5363 ir_node *shr_right = get_binop_right(left);
5365 if (!is_Const(shr_right))
5368 x = get_binop_left(left);
5369 tv_shr = get_Const_tarval(shr_right);
5370 tv_shl = get_Const_tarval(right);
5372 if (is_Shrs(left)) {
5373 /* shrs variant only allowed if c1 >= c2 */
5374 if (! (tarval_cmp(tv_shl, tv_shr) & pn_Cmp_Ge))
5377 tv_mask = tarval_shrs(get_mode_all_one(mode), tv_shr);
5380 tv_mask = tarval_shr(get_mode_all_one(mode), tv_shr);
5382 tv_mask = tarval_shl(tv_mask, tv_shl);
5383 } else if (is_Shr(n) && is_Shl(left)) {
5384 ir_node *shl_right = get_Shl_right(left);
5386 if (!is_Const(shl_right))
5389 x = get_Shl_left(left);
5390 tv_shr = get_Const_tarval(right);
5391 tv_shl = get_Const_tarval(shl_right);
5393 tv_mask = tarval_shl(get_mode_all_one(mode), tv_shl);
5394 tv_mask = tarval_shr(tv_mask, tv_shr);
5399 if (get_tarval_mode(tv_shl) != get_tarval_mode(tv_shr)) {
5400 tv_shl = tarval_convert_to(tv_shl, get_tarval_mode(tv_shr));
5403 assert(tv_mask != tarval_bad);
5404 assert(get_tarval_mode(tv_mask) == mode);
5406 block = get_nodes_block(n);
5407 dbgi = get_irn_dbg_info(n);
5409 pnc = tarval_cmp(tv_shl, tv_shr);
5410 if (pnc == pn_Cmp_Lt || pnc == pn_Cmp_Eq) {
5411 tv_shift = tarval_sub(tv_shr, tv_shl, NULL);
5412 new_const = new_Const(tv_shift);
5414 new_shift = new_rd_Shrs(dbgi, block, x, new_const, mode);
5416 new_shift = new_rd_Shr(dbgi, block, x, new_const, mode);
5419 assert(pnc == pn_Cmp_Gt);
5420 tv_shift = tarval_sub(tv_shl, tv_shr, NULL);
5421 new_const = new_Const(tv_shift);
5422 new_shift = new_rd_Shl(dbgi, block, x, new_const, mode);
5425 new_const = new_Const(tv_mask);
5426 new_and = new_rd_And(dbgi, block, new_shift, new_const, mode);
5431 static tarval *get_modulo_tv_value(tarval *tv, int modulo_val)
5433 ir_mode *mode = get_tarval_mode(tv);
5434 tarval *modulo_tv = new_tarval_from_long(modulo_val, mode);
5435 return tarval_mod(tv, modulo_tv);
5438 typedef ir_node*(*new_shift_func)(dbg_info *dbgi, ir_node *block,
5439 ir_node *left, ir_node *right, ir_mode *mode);
5442 * Normalisation: if we have a shl/shr with modulo_shift behaviour
5443 * then we can use that to minimize the value of Add(x, const) or
5444 * Sub(Const, x). In particular this often avoids 1 instruction in some
5445 * backends for the Shift(x, Sub(Const, y)) case because it can be replaced
5446 * by Shift(x, Minus(y)) which doesnt't need an explicit Const constructed.
5448 static ir_node *transform_node_shift_modulo(ir_node *n,
5449 new_shift_func new_shift)
5451 ir_mode *mode = get_irn_mode(n);
5452 int modulo = get_mode_modulo_shift(mode);
5453 ir_node *newop = NULL;
5454 ir_mode *mode_right;
5461 if (get_mode_arithmetic(mode) != irma_twos_complement)
5463 if (!is_po2(modulo))
5466 irg = get_irn_irg(n);
5467 block = get_nodes_block(n);
5468 right = get_binop_right(n);
5469 mode_right = get_irn_mode(right);
5470 if (is_Const(right)) {
5471 tarval *tv = get_Const_tarval(right);
5472 tarval *tv_mod = get_modulo_tv_value(tv, modulo);
5477 newop = new_r_Const(irg, tv_mod);
5478 } else if (is_Add(right)) {
5479 ir_node *add_right = get_Add_right(right);
5480 if (is_Const(add_right)) {
5481 tarval *tv = get_Const_tarval(add_right);
5482 tarval *tv_mod = get_modulo_tv_value(tv, modulo);
5487 newconst = new_r_Const(irg, tv_mod);
5488 newop = new_r_Add(block, get_Add_left(right), newconst,
5491 } else if (is_Sub(right)) {
5492 ir_node *sub_left = get_Sub_left(right);
5493 if (is_Const(sub_left)) {
5494 tarval *tv = get_Const_tarval(sub_left);
5495 tarval *tv_mod = get_modulo_tv_value(tv, modulo);
5500 newconst = new_r_Const(irg, tv_mod);
5501 newop = new_r_Sub(block, newconst, get_Sub_right(right),
5508 if (newop != NULL) {
5509 dbg_info *dbgi = get_irn_dbg_info(n);
5510 ir_node *left = get_binop_left(n);
5511 return new_shift(dbgi, block, left, newop, mode);
5519 static ir_node *transform_node_Shr(ir_node *n)
5521 ir_node *c, *oldn = n;
5522 ir_node *left = get_Shr_left(n);
5523 ir_node *right = get_Shr_right(n);
5524 ir_mode *mode = get_irn_mode(n);
5526 HANDLE_BINOP_PHI((eval_func) tarval_shr, left, right, c, mode);
5527 n = transform_node_shift(n);
5530 n = transform_node_shift_modulo(n, new_rd_Shr);
5532 n = transform_node_shl_shr(n);
5534 n = transform_node_bitop_shift(n);
5537 } /* transform_node_Shr */
5542 static ir_node *transform_node_Shrs(ir_node *n)
5544 ir_node *c, *oldn = n;
5545 ir_node *a = get_Shrs_left(n);
5546 ir_node *b = get_Shrs_right(n);
5547 ir_mode *mode = get_irn_mode(n);
5549 HANDLE_BINOP_PHI((eval_func) tarval_shrs, a, b, c, mode);
5550 n = transform_node_shift(n);
5553 n = transform_node_shift_modulo(n, new_rd_Shrs);
5555 n = transform_node_bitop_shift(n);
5558 } /* transform_node_Shrs */
5563 static ir_node *transform_node_Shl(ir_node *n)
5565 ir_node *c, *oldn = n;
5566 ir_node *a = get_Shl_left(n);
5567 ir_node *b = get_Shl_right(n);
5568 ir_mode *mode = get_irn_mode(n);
5570 HANDLE_BINOP_PHI((eval_func) tarval_shl, a, b, c, mode);
5571 n = transform_node_shift(n);
5574 n = transform_node_shift_modulo(n, new_rd_Shl);
5576 n = transform_node_shl_shr(n);
5578 n = transform_node_bitop_shift(n);
5581 } /* transform_node_Shl */
5586 static ir_node *transform_node_Rotl(ir_node *n)
5588 ir_node *c, *oldn = n;
5589 ir_node *a = get_Rotl_left(n);
5590 ir_node *b = get_Rotl_right(n);
5591 ir_mode *mode = get_irn_mode(n);
5593 HANDLE_BINOP_PHI((eval_func) tarval_rotl, a, b, c, mode);
5594 n = transform_node_shift(n);
5597 n = transform_node_bitop_shift(n);
5600 } /* transform_node_Rotl */
5605 static ir_node *transform_node_Conv(ir_node *n)
5607 ir_node *c, *oldn = n;
5608 ir_mode *mode = get_irn_mode(n);
5609 ir_node *a = get_Conv_op(n);
5611 if (mode != mode_b && is_const_Phi(a)) {
5612 /* Do NOT optimize mode_b Conv's, this leads to remaining
5613 * Phib nodes later, because the conv_b_lower operation
5614 * is instantly reverted, when it tries to insert a Convb.
5616 c = apply_conv_on_phi(a, mode);
5618 DBG_OPT_ALGSIM0(oldn, c, FS_OPT_CONST_PHI);
5623 if (is_Unknown(a)) { /* Conv_A(Unknown_B) -> Unknown_A */
5624 ir_graph *irg = get_irn_irg(n);
5625 return new_r_Unknown(irg, mode);
5628 if (mode_is_reference(mode) &&
5629 get_mode_size_bits(mode) == get_mode_size_bits(get_irn_mode(a)) &&
5631 ir_node *l = get_Add_left(a);
5632 ir_node *r = get_Add_right(a);
5633 dbg_info *dbgi = get_irn_dbg_info(a);
5634 ir_node *block = get_nodes_block(n);
5636 ir_node *lop = get_Conv_op(l);
5637 if (get_irn_mode(lop) == mode) {
5638 /* ConvP(AddI(ConvI(P), x)) -> AddP(P, x) */
5639 n = new_rd_Add(dbgi, block, lop, r, mode);
5644 ir_node *rop = get_Conv_op(r);
5645 if (get_irn_mode(rop) == mode) {
5646 /* ConvP(AddI(x, ConvI(P))) -> AddP(x, P) */
5647 n = new_rd_Add(dbgi, block, l, rop, mode);
5654 } /* transform_node_Conv */
5657 * Remove dead blocks and nodes in dead blocks
5658 * in keep alive list. We do not generate a new End node.
5660 static ir_node *transform_node_End(ir_node *n)
5662 int i, j, n_keepalives = get_End_n_keepalives(n);
5665 NEW_ARR_A(ir_node *, in, n_keepalives);
5667 for (i = j = 0; i < n_keepalives; ++i) {
5668 ir_node *ka = get_End_keepalive(n, i);
5670 if (! is_Block_dead(ka)) {
5674 } else if (is_irn_pinned_in_irg(ka) && is_Block_dead(get_nodes_block(ka))) {
5676 } else if (is_Bad(ka)) {
5677 /* no need to keep Bad */
5682 if (j != n_keepalives)
5683 set_End_keepalives(n, j, in);
5685 } /* transform_node_End */
5687 bool is_negated_value(ir_node *a, ir_node *b)
5689 if (is_Minus(a) && get_Minus_op(a) == b)
5691 if (is_Minus(b) && get_Minus_op(b) == a)
5693 if (is_Sub(a) && is_Sub(b)) {
5694 ir_node *a_left = get_Sub_left(a);
5695 ir_node *a_right = get_Sub_right(a);
5696 ir_node *b_left = get_Sub_left(b);
5697 ir_node *b_right = get_Sub_right(b);
5699 if (a_left == b_right && a_right == b_left)
5707 * Optimize a Mux into some simpler cases.
5709 static ir_node *transform_node_Mux(ir_node *n)
5711 ir_node *oldn = n, *sel = get_Mux_sel(n);
5712 ir_mode *mode = get_irn_mode(n);
5713 ir_node *t = get_Mux_true(n);
5714 ir_node *f = get_Mux_false(n);
5715 ir_graph *irg = get_irn_irg(n);
5717 if (is_irg_state(irg, IR_GRAPH_STATE_KEEP_MUX))
5721 ir_node* block = get_nodes_block(n);
5723 ir_node* c1 = get_Mux_sel(t);
5724 ir_node* t1 = get_Mux_true(t);
5725 ir_node* f1 = get_Mux_false(t);
5727 /* Mux(cond0, Mux(cond1, x, y), y) -> typical if (cond0 && cond1) x else y */
5728 ir_node* and_ = new_r_And(block, c0, c1, mode_b);
5729 ir_node* new_mux = new_r_Mux(block, and_, f1, t1, mode);
5734 DBG_OPT_ALGSIM0(oldn, t, FS_OPT_MUX_COMBINE);
5735 } else if (f == t1) {
5736 /* Mux(cond0, Mux(cond1, x, y), x) */
5737 ir_node* not_c1 = new_r_Not(block, c1, mode_b);
5738 ir_node* and_ = new_r_And(block, c0, not_c1, mode_b);
5739 ir_node* new_mux = new_r_Mux(block, and_, t1, f1, mode);
5744 DBG_OPT_ALGSIM0(oldn, t, FS_OPT_MUX_COMBINE);
5746 } else if (is_Mux(f)) {
5747 ir_node* block = get_nodes_block(n);
5749 ir_node* c1 = get_Mux_sel(f);
5750 ir_node* t1 = get_Mux_true(f);
5751 ir_node* f1 = get_Mux_false(f);
5753 /* Mux(cond0, x, Mux(cond1, x, y)) -> typical if (cond0 || cond1) x else y */
5754 ir_node* or_ = new_r_Or(block, c0, c1, mode_b);
5755 ir_node* new_mux = new_r_Mux(block, or_, f1, t1, mode);
5760 DBG_OPT_ALGSIM0(oldn, f, FS_OPT_MUX_COMBINE);
5761 } else if (t == f1) {
5762 /* Mux(cond0, x, Mux(cond1, y, x)) */
5763 ir_node* not_c1 = new_r_Not(block, c1, mode_b);
5764 ir_node* or_ = new_r_Or(block, c0, not_c1, mode_b);
5765 ir_node* new_mux = new_r_Mux(block, or_, t1, f1, mode);
5770 DBG_OPT_ALGSIM0(oldn, f, FS_OPT_MUX_COMBINE);
5774 /* first normalization step: try to move a constant to the false side,
5775 * 0 preferred on false side too */
5777 ir_node *cmp = get_Proj_pred(sel);
5779 if (is_Cmp(cmp) && is_Const(t) &&
5780 (!is_Const(f) || (is_Const_null(t) && !is_Const_null(f)))) {
5781 pn_Cmp pnc = get_Proj_proj(sel);
5786 /* Mux(x, a, b) => Mux(not(x), b, a) */
5787 sel = new_r_Proj(cmp, mode_b,
5788 get_negated_pnc(pnc, get_irn_mode(get_Cmp_left(cmp))));
5789 n = new_rd_Mux(get_irn_dbg_info(n), get_nodes_block(n), sel, f, t, mode);
5793 /* note: after normalization, false can only happen on default */
5794 if (mode == mode_b) {
5795 dbg_info *dbg = get_irn_dbg_info(n);
5796 ir_node *block = get_nodes_block(n);
5799 tarval *tv_t = get_Const_tarval(t);
5800 if (tv_t == tarval_b_true) {
5802 /* Muxb(sel, true, false) = sel */
5803 assert(get_Const_tarval(f) == tarval_b_false);
5804 DBG_OPT_ALGSIM0(oldn, sel, FS_OPT_MUX_BOOL);
5807 /* Muxb(sel, true, x) = Or(sel, x) */
5808 n = new_rd_Or(dbg, block, sel, f, mode_b);
5809 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_OR_BOOL);
5813 } else if (is_Const(f)) {
5814 tarval *tv_f = get_Const_tarval(f);
5815 if (tv_f == tarval_b_true) {
5816 /* Muxb(sel, x, true) = Or(Not(sel), x) */
5817 ir_node* not_sel = new_rd_Not(dbg, block, sel, mode_b);
5818 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_ORNOT_BOOL);
5819 n = new_rd_Or(dbg, block, not_sel, t, mode_b);
5822 /* Muxb(sel, x, false) = And(sel, x) */
5823 assert(tv_f == tarval_b_false);
5824 n = new_rd_And(dbg, block, sel, t, mode_b);
5825 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_AND_BOOL);
5831 /* more normalization: Mux(sel, 0, 1) is simply a conv from the mode_b
5832 * value to integer. */
5833 if (is_Const(t) && is_Const(f) && mode_is_int(mode)) {
5834 tarval *a = get_Const_tarval(t);
5835 tarval *b = get_Const_tarval(f);
5837 if (tarval_is_one(a) && tarval_is_null(b)) {
5838 ir_node *block = get_nodes_block(n);
5839 ir_node *conv = new_r_Conv(block, sel, mode);
5841 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_CONV);
5843 } else if (tarval_is_null(a) && tarval_is_one(b)) {
5844 ir_node *block = get_nodes_block(n);
5845 ir_node *not_ = new_r_Not(block, sel, mode_b);
5846 ir_node *conv = new_r_Conv(block, not_, mode);
5848 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_CONV);
5854 ir_node *cmp = get_Proj_pred(sel);
5855 long pn = get_Proj_proj(sel);
5858 * Note: normalization puts the constant on the right side,
5859 * so we check only one case.
5862 ir_node *cmp_r = get_Cmp_right(cmp);
5863 if (is_Const(cmp_r) && is_Const_null(cmp_r)) {
5864 ir_node *block = get_nodes_block(n);
5865 ir_node *cmp_l = get_Cmp_left(cmp);
5867 if (mode_is_int(mode)) {
5869 if ((pn == pn_Cmp_Lg || pn == pn_Cmp_Eq) && is_And(cmp_l)) {
5870 /* Mux((a & b) != 0, c, 0) */
5871 ir_node *and_r = get_And_right(cmp_l);
5874 if (and_r == t && f == cmp_r) {
5875 if (is_Const(t) && tarval_is_single_bit(get_Const_tarval(t))) {
5876 if (pn == pn_Cmp_Lg) {
5877 /* Mux((a & 2^C) != 0, 2^C, 0) */
5879 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_BITOP);
5881 /* Mux((a & 2^C) == 0, 2^C, 0) */
5882 n = new_rd_Eor(get_irn_dbg_info(n),
5883 block, cmp_l, t, mode);
5884 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_BITOP);
5889 if (is_Shl(and_r)) {
5890 ir_node *shl_l = get_Shl_left(and_r);
5891 if (is_Const(shl_l) && is_Const_one(shl_l)) {
5892 if (and_r == t && f == cmp_r) {
5893 if (pn == pn_Cmp_Lg) {
5894 /* (a & (1 << n)) != 0, (1 << n), 0) */
5896 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_BITOP);
5898 /* (a & (1 << n)) == 0, (1 << n), 0) */
5899 n = new_rd_Eor(get_irn_dbg_info(n),
5900 block, cmp_l, t, mode);
5901 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_BITOP);
5907 and_l = get_And_left(cmp_l);
5908 if (is_Shl(and_l)) {
5909 ir_node *shl_l = get_Shl_left(and_l);
5910 if (is_Const(shl_l) && is_Const_one(shl_l)) {
5911 if (and_l == t && f == cmp_r) {
5912 if (pn == pn_Cmp_Lg) {
5913 /* ((1 << n) & a) != 0, (1 << n), 0) */
5915 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_BITOP);
5917 /* ((1 << n) & a) == 0, (1 << n), 0) */
5918 n = new_rd_Eor(get_irn_dbg_info(n),
5919 block, cmp_l, t, mode);
5920 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_BITOP);
5933 } /* transform_node_Mux */
5936 * optimize Sync nodes that have other syncs as input we simply add the inputs
5937 * of the other sync to our own inputs
5939 static ir_node *transform_node_Sync(ir_node *n)
5941 int arity = get_Sync_n_preds(n);
5944 for (i = 0; i < arity;) {
5945 ir_node *pred = get_Sync_pred(n, i);
5949 if (!is_Sync(pred)) {
5957 pred_arity = get_Sync_n_preds(pred);
5958 for (j = 0; j < pred_arity; ++j) {
5959 ir_node *pred_pred = get_Sync_pred(pred, j);
5964 add_irn_n(n, pred_pred);
5968 if (get_Sync_pred(n, k) == pred_pred) break;
5973 /* rehash the sync node */
5977 } /* transform_node_Sync */
5980 * optimize a trampoline Call into a direct Call
5982 static ir_node *transform_node_Call(ir_node *call)
5984 ir_node *callee = get_Call_ptr(call);
5985 ir_node *adr, *mem, *res, *bl, **in;
5986 ir_type *ctp, *mtp, *tp;
5990 int i, n_res, n_param;
5993 if (! is_Proj(callee))
5995 callee = get_Proj_pred(callee);
5996 if (! is_Builtin(callee))
5998 if (get_Builtin_kind(callee) != ir_bk_inner_trampoline)
6001 mem = get_Call_mem(call);
6003 if (skip_Proj(mem) == callee) {
6004 /* memory is routed to the trampoline, skip */
6005 mem = get_Builtin_mem(callee);
6008 /* build a new call type */
6009 mtp = get_Call_type(call);
6010 tdb = get_type_dbg_info(mtp);
6012 n_res = get_method_n_ress(mtp);
6013 n_param = get_method_n_params(mtp);
6014 ctp = new_d_type_method(n_param + 1, n_res, tdb);
6016 for (i = 0; i < n_res; ++i)
6017 set_method_res_type(ctp, i, get_method_res_type(mtp, i));
6019 NEW_ARR_A(ir_node *, in, n_param + 1);
6021 /* FIXME: we don't need a new pointer type in every step */
6022 irg = get_irn_irg(call);
6023 tp = get_irg_frame_type(irg);
6024 tp = new_type_pointer(tp);
6025 set_method_param_type(ctp, 0, tp);
6027 in[0] = get_Builtin_param(callee, 2);
6028 for (i = 0; i < n_param; ++i) {
6029 set_method_param_type(ctp, i + 1, get_method_param_type(mtp, i));
6030 in[i + 1] = get_Call_param(call, i);
6032 var = get_method_variadicity(mtp);
6033 set_method_variadicity(ctp, var);
6034 if (var == variadicity_variadic) {
6035 set_method_first_variadic_param_index(ctp, get_method_first_variadic_param_index(mtp) + 1);
6037 /* When we resolve a trampoline, the function must be called by a this-call */
6038 set_method_calling_convention(ctp, get_method_calling_convention(mtp) | cc_this_call);
6039 set_method_additional_properties(ctp, get_method_additional_properties(mtp));
6041 adr = get_Builtin_param(callee, 1);
6043 db = get_irn_dbg_info(call);
6044 bl = get_nodes_block(call);
6046 res = new_rd_Call(db, bl, mem, adr, n_param + 1, in, ctp);
6047 if (get_irn_pinned(call) == op_pin_state_floats)
6048 set_irn_pinned(res, op_pin_state_floats);
6050 } /* transform_node_Call */
6053 * Tries several [inplace] [optimizing] transformations and returns an
6054 * equivalent node. The difference to equivalent_node() is that these
6055 * transformations _do_ generate new nodes, and thus the old node must
6056 * not be freed even if the equivalent node isn't the old one.
6058 static ir_node *transform_node(ir_node *n)
6063 * Transform_node is the only "optimizing transformation" that might
6064 * return a node with a different opcode. We iterate HERE until fixpoint
6065 * to get the final result.
6069 if (n->op->ops.transform_node != NULL)
6070 n = n->op->ops.transform_node(n);
6071 } while (oldn != n);
6074 } /* transform_node */
6077 * Sets the default transform node operation for an ir_op_ops.
6079 * @param code the opcode for the default operation
6080 * @param ops the operations initialized
6085 static ir_op_ops *firm_set_default_transform_node(ir_opcode code, ir_op_ops *ops)
6089 ops->transform_node = transform_node_##a; \
6091 #define CASE_PROJ(a) \
6093 ops->transform_node_Proj = transform_node_Proj_##a; \
6095 #define CASE_PROJ_EX(a) \
6097 ops->transform_node = transform_node_##a; \
6098 ops->transform_node_Proj = transform_node_Proj_##a; \
6107 CASE_PROJ_EX(DivMod);
6141 } /* firm_set_default_transform_node */
6144 /* **************** Common Subexpression Elimination **************** */
6146 /** The size of the hash table used, should estimate the number of nodes
6148 #define N_IR_NODES 512
6150 /** Compares the attributes of two Const nodes. */
6151 static int node_cmp_attr_Const(ir_node *a, ir_node *b)
6153 return (get_Const_tarval(a) != get_Const_tarval(b))
6154 || (get_Const_type(a) != get_Const_type(b));
6155 } /* node_cmp_attr_Const */
6157 /** Compares the attributes of two Proj nodes. */
6158 static int node_cmp_attr_Proj(ir_node *a, ir_node *b)
6160 return a->attr.proj != b->attr.proj;
6161 } /* node_cmp_attr_Proj */
6163 /** Compares the attributes of two Alloc nodes. */
6164 static int node_cmp_attr_Alloc(ir_node *a, ir_node *b)
6166 const alloc_attr *pa = &a->attr.alloc;
6167 const alloc_attr *pb = &b->attr.alloc;
6168 return (pa->where != pb->where) || (pa->type != pb->type);
6169 } /* node_cmp_attr_Alloc */
6171 /** Compares the attributes of two Free nodes. */
6172 static int node_cmp_attr_Free(ir_node *a, ir_node *b)
6174 const free_attr *pa = &a->attr.free;
6175 const free_attr *pb = &b->attr.free;
6176 return (pa->where != pb->where) || (pa->type != pb->type);
6177 } /* node_cmp_attr_Free */
6179 /** Compares the attributes of two SymConst nodes. */
6180 static int node_cmp_attr_SymConst(ir_node *a, ir_node *b)
6182 const symconst_attr *pa = &a->attr.symc;
6183 const symconst_attr *pb = &b->attr.symc;
6184 return (pa->kind != pb->kind)
6185 || (pa->sym.type_p != pb->sym.type_p)
6186 || (pa->tp != pb->tp);
6187 } /* node_cmp_attr_SymConst */
6189 /** Compares the attributes of two Call nodes. */
6190 static int node_cmp_attr_Call(ir_node *a, ir_node *b)
6192 const call_attr *pa = &a->attr.call;
6193 const call_attr *pb = &b->attr.call;
6194 return (pa->type != pb->type)
6195 || (pa->tail_call != pb->tail_call);
6196 } /* node_cmp_attr_Call */
6198 /** Compares the attributes of two Sel nodes. */
6199 static int node_cmp_attr_Sel(ir_node *a, ir_node *b)
6201 const ir_entity *a_ent = get_Sel_entity(a);
6202 const ir_entity *b_ent = get_Sel_entity(b);
6203 return a_ent != b_ent;
6204 } /* node_cmp_attr_Sel */
6206 /** Compares the attributes of two Phi nodes. */
6207 static int node_cmp_attr_Phi(ir_node *a, ir_node *b)
6209 /* we can only enter this function if both nodes have the same number of inputs,
6210 hence it is enough to check if one of them is a Phi0 */
6212 /* check the Phi0 pos attribute */
6213 return a->attr.phi.u.pos != b->attr.phi.u.pos;
6216 } /* node_cmp_attr_Phi */
6218 /** Compares the attributes of two Conv nodes. */
6219 static int node_cmp_attr_Conv(ir_node *a, ir_node *b)
6221 return get_Conv_strict(a) != get_Conv_strict(b);
6222 } /* node_cmp_attr_Conv */
6224 /** Compares the attributes of two Cast nodes. */
6225 static int node_cmp_attr_Cast(ir_node *a, ir_node *b)
6227 return get_Cast_type(a) != get_Cast_type(b);
6228 } /* node_cmp_attr_Cast */
6230 /** Compares the attributes of two Load nodes. */
6231 static int node_cmp_attr_Load(ir_node *a, ir_node *b)
6233 if (get_Load_volatility(a) == volatility_is_volatile ||
6234 get_Load_volatility(b) == volatility_is_volatile)
6235 /* NEVER do CSE on volatile Loads */
6237 /* do not CSE Loads with different alignment. Be conservative. */
6238 if (get_Load_align(a) != get_Load_align(b))
6241 return get_Load_mode(a) != get_Load_mode(b);
6242 } /* node_cmp_attr_Load */
6244 /** Compares the attributes of two Store nodes. */
6245 static int node_cmp_attr_Store(ir_node *a, ir_node *b)
6247 /* do not CSE Stores with different alignment. Be conservative. */
6248 if (get_Store_align(a) != get_Store_align(b))
6251 /* NEVER do CSE on volatile Stores */
6252 return (get_Store_volatility(a) == volatility_is_volatile ||
6253 get_Store_volatility(b) == volatility_is_volatile);
6254 } /* node_cmp_attr_Store */
6256 /** Compares two exception attributes */
6257 static int node_cmp_exception(ir_node *a, ir_node *b)
6259 const except_attr *ea = &a->attr.except;
6260 const except_attr *eb = &b->attr.except;
6262 return ea->pin_state != eb->pin_state;
6265 #define node_cmp_attr_Bound node_cmp_exception
6267 /** Compares the attributes of two Div nodes. */
6268 static int node_cmp_attr_Div(ir_node *a, ir_node *b)
6270 const divmod_attr *ma = &a->attr.divmod;
6271 const divmod_attr *mb = &b->attr.divmod;
6272 return ma->exc.pin_state != mb->exc.pin_state ||
6273 ma->resmode != mb->resmode ||
6274 ma->no_remainder != mb->no_remainder;
6275 } /* node_cmp_attr_Div */
6277 /** Compares the attributes of two DivMod nodes. */
6278 static int node_cmp_attr_DivMod(ir_node *a, ir_node *b)
6280 const divmod_attr *ma = &a->attr.divmod;
6281 const divmod_attr *mb = &b->attr.divmod;
6282 return ma->exc.pin_state != mb->exc.pin_state ||
6283 ma->resmode != mb->resmode;
6284 } /* node_cmp_attr_DivMod */
6286 /** Compares the attributes of two Mod nodes. */
6287 static int node_cmp_attr_Mod(ir_node *a, ir_node *b)
6289 return node_cmp_attr_DivMod(a, b);
6290 } /* node_cmp_attr_Mod */
6292 /** Compares the attributes of two Quot nodes. */
6293 static int node_cmp_attr_Quot(ir_node *a, ir_node *b)
6295 return node_cmp_attr_DivMod(a, b);
6296 } /* node_cmp_attr_Quot */
6298 /** Compares the attributes of two Confirm nodes. */
6299 static int node_cmp_attr_Confirm(ir_node *a, ir_node *b)
6301 /* no need to compare the bound, as this is a input */
6302 return (get_Confirm_cmp(a) != get_Confirm_cmp(b));
6303 } /* node_cmp_attr_Confirm */
6305 /** Compares the attributes of two Builtin nodes. */
6306 static int node_cmp_attr_Builtin(ir_node *a, ir_node *b)
6308 /* no need to compare the type, equal kind means equal type */
6309 return get_Builtin_kind(a) != get_Builtin_kind(b);
6310 } /* node_cmp_attr_Builtin */
6312 /** Compares the attributes of two ASM nodes. */
6313 static int node_cmp_attr_ASM(ir_node *a, ir_node *b)
6316 const ir_asm_constraint *ca;
6317 const ir_asm_constraint *cb;
6320 if (get_ASM_text(a) != get_ASM_text(b))
6323 /* Should we really check the constraints here? Should be better, but is strange. */
6324 n = get_ASM_n_input_constraints(a);
6325 if (n != get_ASM_n_input_constraints(b))
6328 ca = get_ASM_input_constraints(a);
6329 cb = get_ASM_input_constraints(b);
6330 for (i = 0; i < n; ++i) {
6331 if (ca[i].pos != cb[i].pos || ca[i].constraint != cb[i].constraint)
6335 n = get_ASM_n_output_constraints(a);
6336 if (n != get_ASM_n_output_constraints(b))
6339 ca = get_ASM_output_constraints(a);
6340 cb = get_ASM_output_constraints(b);
6341 for (i = 0; i < n; ++i) {
6342 if (ca[i].pos != cb[i].pos || ca[i].constraint != cb[i].constraint)
6346 n = get_ASM_n_clobbers(a);
6347 if (n != get_ASM_n_clobbers(b))
6350 cla = get_ASM_clobbers(a);
6351 clb = get_ASM_clobbers(b);
6352 for (i = 0; i < n; ++i) {
6353 if (cla[i] != clb[i])
6357 } /* node_cmp_attr_ASM */
6359 /** Compares the inexistent attributes of two Dummy nodes. */
6360 static int node_cmp_attr_Dummy(ir_node *a, ir_node *b)
6368 * Set the default node attribute compare operation for an ir_op_ops.
6370 * @param code the opcode for the default operation
6371 * @param ops the operations initialized
6376 static ir_op_ops *firm_set_default_node_cmp_attr(ir_opcode code, ir_op_ops *ops)
6380 ops->node_cmp_attr = node_cmp_attr_##a; \
6413 } /* firm_set_default_node_cmp_attr */
6416 * Compare function for two nodes in the value table. Gets two
6417 * nodes as parameters. Returns 0 if the nodes are a Common Sub Expression.
6419 int identities_cmp(const void *elt, const void *key)
6421 ir_node *a = (ir_node *)elt;
6422 ir_node *b = (ir_node *)key;
6425 if (a == b) return 0;
6427 if ((get_irn_op(a) != get_irn_op(b)) ||
6428 (get_irn_mode(a) != get_irn_mode(b))) return 1;
6430 /* compare if a's in and b's in are of equal length */
6431 irn_arity_a = get_irn_arity(a);
6432 if (irn_arity_a != get_irn_arity(b))
6435 if (get_irn_pinned(a) == op_pin_state_pinned) {
6436 /* for pinned nodes, the block inputs must be equal */
6437 if (get_irn_n(a, -1) != get_irn_n(b, -1))
6439 } else if (! get_opt_global_cse()) {
6440 /* for block-local CSE both nodes must be in the same MacroBlock */
6441 if (get_irn_MacroBlock(a) != get_irn_MacroBlock(b))
6445 /* compare a->in[0..ins] with b->in[0..ins] */
6446 for (i = 0; i < irn_arity_a; ++i) {
6447 ir_node *pred_a = get_irn_n(a, i);
6448 ir_node *pred_b = get_irn_n(b, i);
6449 if (pred_a != pred_b) {
6450 /* if both predecessors are CSE neutral they might be different */
6451 if (!is_irn_cse_neutral(pred_a) || !is_irn_cse_neutral(pred_b))
6457 * here, we already now that the nodes are identical except their
6460 if (a->op->ops.node_cmp_attr)
6461 return a->op->ops.node_cmp_attr(a, b);
6464 } /* identities_cmp */
6467 * Calculate a hash value of a node.
6469 * @param node The IR-node
6471 unsigned ir_node_hash(const ir_node *node)
6473 return node->op->ops.hash(node);
6474 } /* ir_node_hash */
6477 void new_identities(ir_graph *irg)
6479 if (irg->value_table != NULL)
6480 del_pset(irg->value_table);
6481 irg->value_table = new_pset(identities_cmp, N_IR_NODES);
6482 } /* new_identities */
6484 void del_identities(ir_graph *irg)
6486 if (irg->value_table != NULL)
6487 del_pset(irg->value_table);
6488 } /* del_identities */
6490 /* Normalize a node by putting constants (and operands with larger
6491 * node index) on the right (operator side). */
6492 void ir_normalize_node(ir_node *n)
6494 if (is_op_commutative(get_irn_op(n))) {
6495 ir_node *l = get_binop_left(n);
6496 ir_node *r = get_binop_right(n);
6498 /* For commutative operators perform a OP b == b OP a but keep
6499 * constants on the RIGHT side. This helps greatly in some
6500 * optimizations. Moreover we use the idx number to make the form
6502 if (!operands_are_normalized(l, r)) {
6503 set_binop_left(n, r);
6504 set_binop_right(n, l);
6508 } /* ir_normalize_node */
6511 * Update the nodes after a match in the value table. If both nodes have
6512 * the same MacroBlock but different Blocks, we must ensure that the node
6513 * with the dominating Block (the node that is near to the MacroBlock header
6514 * is stored in the table.
6515 * Because a MacroBlock has only one "non-exception" flow, we don't need
6516 * dominance info here: We known, that one block must dominate the other and
6517 * following the only block input will allow to find it.
6519 static void update_known_irn(ir_node *known_irn, const ir_node *new_ir_node)
6521 ir_node *known_blk, *new_block, *block, *mbh;
6523 if (get_opt_global_cse()) {
6524 /* Block inputs are meaning less */
6527 known_blk = get_irn_n(known_irn, -1);
6528 new_block = get_irn_n(new_ir_node, -1);
6529 if (known_blk == new_block) {
6530 /* already in the same block */
6534 * We expect the typical case when we built the graph. In that case, the
6535 * known_irn is already the upper one, so checking this should be faster.
6538 mbh = get_Block_MacroBlock(new_block);
6540 if (block == known_blk) {
6541 /* ok, we have found it: known_block dominates new_block as expected */
6546 * We have reached the MacroBlock header NOT founding
6547 * the known_block. new_block must dominate known_block.
6550 set_irn_n(known_irn, -1, new_block);
6553 assert(get_Block_n_cfgpreds(block) == 1);
6554 block = get_Block_cfgpred_block(block, 0);
6556 } /* update_value_table */
6559 * Return the canonical node computing the same value as n.
6560 * Looks up the node in a hash table, enters it in the table
6561 * if it isn't there yet.
6563 * @param n the node to look up
6565 * @return a node that computes the same value as n or n if no such
6566 * node could be found
6568 ir_node *identify_remember(ir_node *n)
6570 ir_graph *irg = get_irn_irg(n);
6571 pset *value_table = irg->value_table;
6574 if (value_table == NULL)
6577 ir_normalize_node(n);
6578 /* lookup or insert in hash table with given hash key. */
6579 nn = pset_insert(value_table, n, ir_node_hash(n));
6582 update_known_irn(nn, n);
6584 /* n is reachable again */
6585 edges_node_revival(nn, get_irn_irg(nn));
6589 } /* identify_remember */
6592 * During construction we set the op_pin_state_pinned flag in the graph right when the
6593 * optimization is performed. The flag turning on procedure global cse could
6594 * be changed between two allocations. This way we are safe.
6596 * @param n The node to lookup
6598 static inline ir_node *identify_cons(ir_node *n)
6602 n = identify_remember(n);
6603 if (n != old && get_irn_MacroBlock(old) != get_irn_MacroBlock(n)) {
6604 ir_graph *irg = get_irn_irg(n);
6605 set_irg_pinned(irg, op_pin_state_floats);
6608 } /* identify_cons */
6610 /* Add a node to the identities value table. */
6611 void add_identities(ir_node *node)
6618 identify_remember(node);
6621 /* Visit each node in the value table of a graph. */
6622 void visit_all_identities(ir_graph *irg, irg_walk_func visit, void *env)
6625 ir_graph *rem = current_ir_graph;
6627 current_ir_graph = irg;
6628 foreach_pset(irg->value_table, node) {
6631 current_ir_graph = rem;
6632 } /* visit_all_identities */
6635 * Garbage in, garbage out. If a node has a dead input, i.e., the
6636 * Bad node is input to the node, return the Bad node.
6638 static ir_node *gigo(ir_node *node)
6641 ir_op *op = get_irn_op(node);
6643 /* remove garbage blocks by looking at control flow that leaves the block
6644 and replacing the control flow by Bad. */
6645 if (get_irn_mode(node) == mode_X) {
6646 ir_node *block = get_nodes_block(skip_Proj(node));
6648 /* Don't optimize nodes in immature blocks. */
6649 if (!get_Block_matured(block))
6651 /* Don't optimize End, may have Bads. */
6652 if (op == op_End) return node;
6654 if (is_Block(block)) {
6655 if (is_Block_dead(block)) {
6656 /* control flow from dead block is dead */
6660 for (i = get_irn_arity(block) - 1; i >= 0; --i) {
6661 if (!is_Bad(get_irn_n(block, i)))
6665 ir_graph *irg = get_irn_irg(block);
6666 /* the start block is never dead */
6667 if (block != get_irg_start_block(irg)
6668 && block != get_irg_end_block(irg)) {
6670 * Do NOT kill control flow without setting
6671 * the block to dead of bad things can happen:
6672 * We get a Block that is not reachable be irg_block_walk()
6673 * but can be found by irg_walk()!
6675 set_Block_dead(block);
6682 /* Blocks, Phis and Tuples may have dead inputs, e.g., if one of the
6683 blocks predecessors is dead. */
6684 if (op != op_Block && op != op_Phi && op != op_Tuple) {
6685 irn_arity = get_irn_arity(node);
6688 * Beware: we can only read the block of a non-floating node.
6690 if (is_irn_pinned_in_irg(node) &&
6691 is_Block_dead(get_nodes_block(skip_Proj(node))))
6694 for (i = 0; i < irn_arity; i++) {
6695 ir_node *pred = get_irn_n(node, i);
6700 /* Propagating Unknowns here seems to be a bad idea, because
6701 sometimes we need a node as a input and did not want that
6703 However, it might be useful to move this into a later phase
6704 (if you think that optimizing such code is useful). */
6705 if (is_Unknown(pred) && mode_is_data(get_irn_mode(node)))
6706 return new_Unknown(get_irn_mode(node));
6711 /* With this code we violate the agreement that local_optimize
6712 only leaves Bads in Block, Phi and Tuple nodes. */
6713 /* If Block has only Bads as predecessors it's garbage. */
6714 /* If Phi has only Bads as predecessors it's garbage. */
6715 if ((op == op_Block && get_Block_matured(node)) || op == op_Phi) {
6716 irn_arity = get_irn_arity(node);
6717 for (i = 0; i < irn_arity; i++) {
6718 if (!is_Bad(get_irn_n(node, i))) break;
6720 if (i == irn_arity) node = new_Bad();
6727 * These optimizations deallocate nodes from the obstack.
6728 * It can only be called if it is guaranteed that no other nodes
6729 * reference this one, i.e., right after construction of a node.
6731 * @param n The node to optimize
6733 ir_node *optimize_node(ir_node *n)
6737 ir_graph *irg = get_irn_irg(n);
6738 ir_opcode iro = get_irn_opcode(n);
6740 /* Always optimize Phi nodes: part of the construction. */
6741 if ((!get_opt_optimize()) && (iro != iro_Phi)) return n;
6743 /* constant expression evaluation / constant folding */
6744 if (get_opt_constant_folding()) {
6745 /* neither constants nor Tuple values can be evaluated */
6746 if (iro != iro_Const && (get_irn_mode(n) != mode_T)) {
6747 unsigned fp_model = get_irg_fp_model(irg);
6748 int old_fp_mode = tarval_fp_ops_enabled();
6750 tarval_enable_fp_ops(! (fp_model & fp_no_float_fold));
6752 /* try to evaluate */
6753 tv = computed_value(n);
6754 if (tv != tarval_bad) {
6756 ir_type *old_tp = get_irn_type(n);
6757 int i, arity = get_irn_arity(n);
6761 * Try to recover the type of the new expression.
6763 for (i = 0; i < arity && !old_tp; ++i)
6764 old_tp = get_irn_type(get_irn_n(n, i));
6767 * we MUST copy the node here temporary, because it's still needed
6768 * for DBG_OPT_CSTEVAL
6770 node_size = offsetof(ir_node, attr) + n->op->attr_size;
6771 oldn = alloca(node_size);
6773 memcpy(oldn, n, node_size);
6774 CLONE_ARR_A(ir_node *, oldn->in, n->in);
6776 /* ARG, copy the in array, we need it for statistics */
6777 memcpy(oldn->in, n->in, ARR_LEN(n->in) * sizeof(n->in[0]));
6779 /* note the inplace edges module */
6780 edges_node_deleted(n, irg);
6782 /* evaluation was successful -- replace the node. */
6783 irg_kill_node(irg, n);
6786 if (old_tp && get_type_mode(old_tp) == get_tarval_mode(tv))
6787 set_Const_type(nw, old_tp);
6788 DBG_OPT_CSTEVAL(oldn, nw);
6789 tarval_enable_fp_ops(old_fp_mode);
6792 tarval_enable_fp_ops(old_fp_mode);
6796 /* remove unnecessary nodes */
6797 if (get_opt_algebraic_simplification() ||
6798 (iro == iro_Phi) || /* always optimize these nodes. */
6800 (iro == iro_Proj) ||
6801 (iro == iro_Block) ) /* Flags tested local. */
6802 n = equivalent_node(n);
6804 /* Common Subexpression Elimination.
6806 * Checks whether n is already available.
6807 * The block input is used to distinguish different subexpressions. Right
6808 * now all nodes are op_pin_state_pinned to blocks, i.e., the CSE only finds common
6809 * subexpressions within a block.
6812 n = identify_cons(n);
6815 edges_node_deleted(oldn, irg);
6817 /* We found an existing, better node, so we can deallocate the old node. */
6818 irg_kill_node(irg, oldn);
6822 /* Some more constant expression evaluation that does not allow to
6824 iro = get_irn_opcode(n);
6825 if (get_opt_algebraic_simplification() ||
6826 (iro == iro_Cond) ||
6827 (iro == iro_Proj)) /* Flags tested local. */
6828 n = transform_node(n);
6830 /* Remove nodes with dead (Bad) input.
6831 Run always for transformation induced Bads. */
6834 /* Now we have a legal, useful node. Enter it in hash table for CSE */
6835 if (get_opt_cse() && (get_irn_opcode(n) != iro_Block)) {
6837 n = identify_remember(o);
6843 } /* optimize_node */
6847 * These optimizations never deallocate nodes (in place). This can cause dead
6848 * nodes lying on the obstack. Remove these by a dead node elimination,
6849 * i.e., a copying garbage collection.
6851 ir_node *optimize_in_place_2(ir_node *n)
6855 ir_opcode iro = get_irn_opcode(n);
6857 if (!get_opt_optimize() && !is_Phi(n)) return n;
6859 /* constant expression evaluation / constant folding */
6860 if (get_opt_constant_folding()) {
6861 /* neither constants nor Tuple values can be evaluated */
6862 if (iro != iro_Const && get_irn_mode(n) != mode_T) {
6863 ir_graph *irg = get_irn_irg(n);
6864 unsigned fp_model = get_irg_fp_model(irg);
6865 int old_fp_mode = tarval_fp_ops_enabled();
6867 tarval_enable_fp_ops((fp_model & fp_strict_algebraic) == 0);
6868 /* try to evaluate */
6869 tv = computed_value(n);
6870 if (tv != tarval_bad) {
6871 /* evaluation was successful -- replace the node. */
6872 ir_type *old_tp = get_irn_type(n);
6873 int i, arity = get_irn_arity(n);
6876 * Try to recover the type of the new expression.
6878 for (i = 0; i < arity && !old_tp; ++i)
6879 old_tp = get_irn_type(get_irn_n(n, i));
6883 if (old_tp && get_type_mode(old_tp) == get_tarval_mode(tv))
6884 set_Const_type(n, old_tp);
6886 DBG_OPT_CSTEVAL(oldn, n);
6887 tarval_enable_fp_ops(old_fp_mode);
6890 tarval_enable_fp_ops(old_fp_mode);
6894 /* remove unnecessary nodes */
6895 if (get_opt_constant_folding() ||
6896 (iro == iro_Phi) || /* always optimize these nodes. */
6897 (iro == iro_Id) || /* ... */
6898 (iro == iro_Proj) || /* ... */
6899 (iro == iro_Block) ) /* Flags tested local. */
6900 n = equivalent_node(n);
6902 /** common subexpression elimination **/
6903 /* Checks whether n is already available. */
6904 /* The block input is used to distinguish different subexpressions. Right
6905 now all nodes are op_pin_state_pinned to blocks, i.e., the cse only finds common
6906 subexpressions within a block. */
6907 if (get_opt_cse()) {
6909 n = identify_remember(o);
6914 /* Some more constant expression evaluation. */
6915 iro = get_irn_opcode(n);
6916 if (get_opt_constant_folding() ||
6917 (iro == iro_Cond) ||
6918 (iro == iro_Proj)) /* Flags tested local. */
6919 n = transform_node(n);
6921 /* Remove nodes with dead (Bad) input.
6922 Run always for transformation induced Bads. */
6925 /* Now we can verify the node, as it has no dead inputs any more. */
6928 /* Now we have a legal, useful node. Enter it in hash table for cse.
6929 Blocks should be unique anyways. (Except the successor of start:
6930 is cse with the start block!) */
6931 if (get_opt_cse() && (get_irn_opcode(n) != iro_Block)) {
6933 n = identify_remember(o);
6939 } /* optimize_in_place_2 */
6942 * Wrapper for external use, set proper status bits after optimization.
6944 ir_node *optimize_in_place(ir_node *n)
6946 ir_graph *irg = get_irn_irg(n);
6947 /* Handle graph state */
6948 assert(get_irg_phase_state(irg) != phase_building);
6950 if (get_opt_global_cse())
6951 set_irg_pinned(irg, op_pin_state_floats);
6952 if (get_irg_outs_state(irg) == outs_consistent)
6953 set_irg_outs_inconsistent(irg);
6955 /* FIXME: Maybe we could also test whether optimizing the node can
6956 change the control graph. */
6957 set_irg_doms_inconsistent(irg);
6958 return optimize_in_place_2(n);
6959 } /* optimize_in_place */
6962 * Calculate a hash value of a Const node.
6964 static unsigned hash_Const(const ir_node *node)
6968 /* special value for const, as they only differ in their tarval. */
6969 h = HASH_PTR(node->attr.con.tarval);
6975 * Calculate a hash value of a SymConst node.
6977 static unsigned hash_SymConst(const ir_node *node)
6981 /* all others are pointers */
6982 h = HASH_PTR(node->attr.symc.sym.type_p);
6985 } /* hash_SymConst */
6988 * Set the default hash operation in an ir_op_ops.
6990 * @param code the opcode for the default operation
6991 * @param ops the operations initialized
6996 static ir_op_ops *firm_set_default_hash(ir_opcode code, ir_op_ops *ops)
7000 ops->hash = hash_##a; \
7003 /* hash function already set */
7004 if (ops->hash != NULL)
7011 /* use input/mode default hash if no function was given */
7012 ops->hash = firm_default_hash;
7020 * Sets the default operation for an ir_ops.
7022 ir_op_ops *firm_set_default_operations(ir_opcode code, ir_op_ops *ops)
7024 ops = firm_set_default_hash(code, ops);
7025 ops = firm_set_default_computed_value(code, ops);
7026 ops = firm_set_default_equivalent_node(code, ops);
7027 ops = firm_set_default_transform_node(code, ops);
7028 ops = firm_set_default_node_cmp_attr(code, ops);
7029 ops = firm_set_default_get_type(code, ops);
7030 ops = firm_set_default_get_type_attr(code, ops);
7031 ops = firm_set_default_get_entity_attr(code, ops);
7034 } /* firm_set_default_operations */