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 previously, it is still valid if
1674 * 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) {
1681 * One could expect that we simply return the previous
1682 * Bound here. However, this would be wrong, as we could
1683 * add an exception Proj to a new location then.
1684 * So, we must turn in into a tuple.
1690 /* Turn Bound into a tuple (mem, jmp, bad, idx) */
1691 switch (get_Proj_proj(proj)) {
1693 DBG_OPT_EXC_REM(proj);
1694 proj = get_Bound_mem(bound);
1696 case pn_Bound_X_except: {
1697 ir_graph *irg = get_irn_irg(proj);
1698 DBG_OPT_EXC_REM(proj);
1699 proj = get_irg_bad(irg);
1704 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NOP);
1707 /* cannot optimize pn_Bound_X_regular, handled in transform ... */
1712 } /* equivalent_node_Proj_Bound */
1715 * Optimize an Exception Proj(Load) with a non-null address.
1717 static ir_node *equivalent_node_Proj_Load(ir_node *proj)
1719 if (get_opt_ldst_only_null_ptr_exceptions()) {
1720 if (get_irn_mode(proj) == mode_X) {
1721 ir_node *load = get_Proj_pred(proj);
1723 /* get the Load address */
1724 const ir_node *addr = get_Load_ptr(load);
1725 const ir_node *confirm;
1727 if (value_not_null(addr, &confirm)) {
1728 if (get_Proj_proj(proj) == pn_Load_X_except) {
1729 ir_graph *irg = get_irn_irg(proj);
1730 DBG_OPT_EXC_REM(proj);
1731 return get_irg_bad(irg);
1737 } /* equivalent_node_Proj_Load */
1740 * Optimize an Exception Proj(Store) with a non-null address.
1742 static ir_node *equivalent_node_Proj_Store(ir_node *proj)
1744 if (get_opt_ldst_only_null_ptr_exceptions()) {
1745 if (get_irn_mode(proj) == mode_X) {
1746 ir_node *store = get_Proj_pred(proj);
1748 /* get the load/store address */
1749 const ir_node *addr = get_Store_ptr(store);
1750 const ir_node *confirm;
1752 if (value_not_null(addr, &confirm)) {
1753 if (get_Proj_proj(proj) == pn_Store_X_except) {
1754 ir_graph *irg = get_irn_irg(proj);
1755 DBG_OPT_EXC_REM(proj);
1756 return get_irg_bad(irg);
1762 } /* equivalent_node_Proj_Store */
1765 * Does all optimizations on nodes that must be done on it's Proj's
1766 * because of creating new nodes.
1768 static ir_node *equivalent_node_Proj(ir_node *proj)
1770 ir_node *n = get_Proj_pred(proj);
1772 if (get_irn_mode(proj) == mode_X) {
1773 if (is_Block_dead(get_nodes_block(n))) {
1774 /* Remove dead control flow -- early gigo(). */
1775 ir_graph *irg = get_irn_irg(proj);
1776 return get_irg_bad(irg);
1779 if (n->op->ops.equivalent_node_Proj)
1780 return n->op->ops.equivalent_node_Proj(proj);
1782 } /* equivalent_node_Proj */
1787 static ir_node *equivalent_node_Id(ir_node *n)
1795 DBG_OPT_ID(oldn, n);
1797 } /* equivalent_node_Id */
1802 static ir_node *equivalent_node_Mux(ir_node *n)
1804 ir_node *oldn = n, *sel = get_Mux_sel(n);
1806 tarval *ts = value_of(sel);
1808 /* Mux(true, f, t) == t */
1809 if (ts == tarval_b_true) {
1810 n = get_Mux_true(n);
1811 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_C);
1814 /* Mux(false, f, t) == f */
1815 if (ts == tarval_b_false) {
1816 n = get_Mux_false(n);
1817 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_C);
1820 n_t = get_Mux_true(n);
1821 n_f = get_Mux_false(n);
1823 /* Mux(v, x, T) == x */
1824 if (is_Unknown(n_f)) {
1826 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_EQ);
1829 /* Mux(v, T, x) == x */
1830 if (is_Unknown(n_t)) {
1832 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_EQ);
1836 /* Mux(v, x, x) == x */
1839 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_EQ);
1842 if (is_Proj(sel) && !mode_honor_signed_zeros(get_irn_mode(n))) {
1843 ir_node *cmp = get_Proj_pred(sel);
1844 long proj_nr = get_Proj_proj(sel);
1845 ir_node *f = get_Mux_false(n);
1846 ir_node *t = get_Mux_true(n);
1849 * Note further that these optimization work even for floating point
1850 * with NaN's because -NaN == NaN.
1851 * However, if +0 and -0 is handled differently, we cannot use the first one.
1854 ir_node *const cmp_l = get_Cmp_left(cmp);
1855 ir_node *const cmp_r = get_Cmp_right(cmp);
1859 if ((cmp_l == t && cmp_r == f) || /* Mux(t == f, t, f) -> f */
1860 (cmp_l == f && cmp_r == t)) { /* Mux(f == t, t, f) -> f */
1862 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1869 if ((cmp_l == t && cmp_r == f) || /* Mux(t != f, t, f) -> t */
1870 (cmp_l == f && cmp_r == t)) { /* Mux(f != t, t, f) -> t */
1872 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1879 * Note: normalization puts the constant on the right side,
1880 * so we check only one case.
1882 if (cmp_l == t && tarval_is_null(value_of(cmp_r))) {
1883 /* Mux(t CMP 0, X, t) */
1884 if (is_Minus(f) && get_Minus_op(f) == t) {
1885 /* Mux(t CMP 0, -t, t) */
1886 if (proj_nr == pn_Cmp_Eq) {
1887 /* Mux(t == 0, -t, t) ==> -t */
1889 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1890 } else if (proj_nr == pn_Cmp_Lg || proj_nr == pn_Cmp_Ne) {
1891 /* Mux(t != 0, -t, t) ==> t */
1893 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1900 } /* equivalent_node_Mux */
1903 * Remove Confirm nodes if setting is on.
1904 * Replace Confirms(x, '=', Constlike) by Constlike.
1906 static ir_node *equivalent_node_Confirm(ir_node *n)
1908 ir_node *pred = get_Confirm_value(n);
1909 pn_Cmp pnc = get_Confirm_cmp(n);
1911 while (is_Confirm(pred) && pnc == get_Confirm_cmp(pred)) {
1913 * rare case: two identical Confirms one after another,
1914 * replace the second one with the first.
1917 pred = get_Confirm_value(n);
1918 pnc = get_Confirm_cmp(n);
1920 if (get_opt_remove_confirm())
1921 return get_Confirm_value(n);
1926 * equivalent_node() returns a node equivalent to input n. It skips all nodes that
1927 * perform no actual computation, as, e.g., the Id nodes. It does not create
1928 * new nodes. It is therefore safe to free n if the node returned is not n.
1929 * If a node returns a Tuple we can not just skip it. If the size of the
1930 * in array fits, we transform n into a tuple (e.g., Div).
1932 ir_node *equivalent_node(ir_node *n)
1934 if (n->op->ops.equivalent_node)
1935 return n->op->ops.equivalent_node(n);
1937 } /* equivalent_node */
1940 * Sets the default equivalent node operation for an ir_op_ops.
1942 * @param code the opcode for the default operation
1943 * @param ops the operations initialized
1948 static ir_op_ops *firm_set_default_equivalent_node(ir_opcode code, ir_op_ops *ops)
1952 ops->equivalent_node = equivalent_node_##a; \
1954 #define CASE_PROJ(a) \
1956 ops->equivalent_node_Proj = equivalent_node_Proj_##a; \
1999 } /* firm_set_default_equivalent_node */
2002 * Returns non-zero if a node is a Phi node
2003 * with all predecessors constant.
2005 static int is_const_Phi(ir_node *n)
2009 if (! is_Phi(n) || get_irn_arity(n) == 0)
2011 for (i = get_irn_arity(n) - 1; i >= 0; --i) {
2012 if (! is_Const(get_irn_n(n, i)))
2016 } /* is_const_Phi */
2018 typedef tarval *(*tarval_sub_type)(tarval *a, tarval *b, ir_mode *mode);
2019 typedef tarval *(*tarval_binop_type)(tarval *a, tarval *b);
2022 * in reality eval_func should be tarval (*eval_func)() but incomplete
2023 * declarations are bad style and generate noisy warnings
2025 typedef void (*eval_func)(void);
2028 * Wrapper for the tarval binop evaluation, tarval_sub has one more parameter.
2030 static tarval *do_eval(eval_func eval, tarval *a, tarval *b, ir_mode *mode)
2032 if (eval == (eval_func) tarval_sub) {
2033 tarval_sub_type func = (tarval_sub_type)eval;
2035 return func(a, b, mode);
2037 tarval_binop_type func = (tarval_binop_type)eval;
2044 * Apply an evaluator on a binop with a constant operators (and one Phi).
2046 * @param phi the Phi node
2047 * @param other the other operand
2048 * @param eval an evaluator function
2049 * @param mode the mode of the result, may be different from the mode of the Phi!
2050 * @param left if non-zero, other is the left operand, else the right
2052 * @return a new Phi node if the conversion was successful, NULL else
2054 static ir_node *apply_binop_on_phi(ir_node *phi, tarval *other, eval_func eval, ir_mode *mode, int left)
2060 int i, n = get_irn_arity(phi);
2062 NEW_ARR_A(void *, res, n);
2064 for (i = 0; i < n; ++i) {
2065 pred = get_irn_n(phi, i);
2066 tv = get_Const_tarval(pred);
2067 tv = do_eval(eval, other, tv, mode);
2069 if (tv == tarval_bad) {
2070 /* folding failed, bad */
2076 for (i = 0; i < n; ++i) {
2077 pred = get_irn_n(phi, i);
2078 tv = get_Const_tarval(pred);
2079 tv = do_eval(eval, tv, other, mode);
2081 if (tv == tarval_bad) {
2082 /* folding failed, bad */
2088 irg = get_irn_irg(phi);
2089 for (i = 0; i < n; ++i) {
2090 pred = get_irn_n(phi, i);
2091 res[i] = new_r_Const_type(irg, res[i], get_Const_type(pred));
2093 return new_r_Phi(get_nodes_block(phi), n, (ir_node **)res, mode);
2094 } /* apply_binop_on_phi */
2097 * Apply an evaluator on a binop with two constant Phi.
2099 * @param a the left Phi node
2100 * @param b the right Phi node
2101 * @param eval an evaluator function
2102 * @param mode the mode of the result, may be different from the mode of the Phi!
2104 * @return a new Phi node if the conversion was successful, NULL else
2106 static ir_node *apply_binop_on_2_phis(ir_node *a, ir_node *b, eval_func eval, ir_mode *mode)
2108 tarval *tv_l, *tv_r, *tv;
2114 if (get_nodes_block(a) != get_nodes_block(b))
2117 n = get_irn_arity(a);
2118 NEW_ARR_A(void *, res, n);
2120 for (i = 0; i < n; ++i) {
2121 pred = get_irn_n(a, i);
2122 tv_l = get_Const_tarval(pred);
2123 pred = get_irn_n(b, i);
2124 tv_r = get_Const_tarval(pred);
2125 tv = do_eval(eval, tv_l, tv_r, mode);
2127 if (tv == tarval_bad) {
2128 /* folding failed, bad */
2133 irg = get_irn_irg(a);
2134 for (i = 0; i < n; ++i) {
2135 pred = get_irn_n(a, i);
2136 res[i] = new_r_Const_type(irg, res[i], get_Const_type(pred));
2138 return new_r_Phi(get_nodes_block(a), n, (ir_node **)res, mode);
2139 } /* apply_binop_on_2_phis */
2142 * Apply an evaluator on a unop with a constant operator (a Phi).
2144 * @param phi the Phi node
2145 * @param eval an evaluator function
2147 * @return a new Phi node if the conversion was successful, NULL else
2149 static ir_node *apply_unop_on_phi(ir_node *phi, tarval *(*eval)(tarval *))
2156 int i, n = get_irn_arity(phi);
2158 NEW_ARR_A(void *, res, n);
2159 for (i = 0; i < n; ++i) {
2160 pred = get_irn_n(phi, i);
2161 tv = get_Const_tarval(pred);
2164 if (tv == tarval_bad) {
2165 /* folding failed, bad */
2170 mode = get_irn_mode(phi);
2171 irg = get_irn_irg(phi);
2172 for (i = 0; i < n; ++i) {
2173 pred = get_irn_n(phi, i);
2174 res[i] = new_r_Const_type(irg, res[i], get_Const_type(pred));
2176 return new_r_Phi(get_nodes_block(phi), n, (ir_node **)res, mode);
2177 } /* apply_unop_on_phi */
2180 * Apply a conversion on a constant operator (a Phi).
2182 * @param phi the Phi node
2184 * @return a new Phi node if the conversion was successful, NULL else
2186 static ir_node *apply_conv_on_phi(ir_node *phi, ir_mode *mode)
2192 int i, n = get_irn_arity(phi);
2194 NEW_ARR_A(void *, res, n);
2195 for (i = 0; i < n; ++i) {
2196 pred = get_irn_n(phi, i);
2197 tv = get_Const_tarval(pred);
2198 tv = tarval_convert_to(tv, mode);
2200 if (tv == tarval_bad) {
2201 /* folding failed, bad */
2206 irg = get_irn_irg(phi);
2207 for (i = 0; i < n; ++i) {
2208 pred = get_irn_n(phi, i);
2209 res[i] = new_r_Const_type(irg, res[i], get_Const_type(pred));
2211 return new_r_Phi(get_nodes_block(phi), n, (ir_node **)res, mode);
2212 } /* apply_conv_on_phi */
2215 * Transform AddP(P, ConvIs(Iu)), AddP(P, ConvIu(Is)) and
2216 * SubP(P, ConvIs(Iu)), SubP(P, ConvIu(Is)).
2217 * If possible, remove the Conv's.
2219 static ir_node *transform_node_AddSub(ir_node *n)
2221 ir_mode *mode = get_irn_mode(n);
2223 if (mode_is_reference(mode)) {
2224 ir_node *left = get_binop_left(n);
2225 ir_node *right = get_binop_right(n);
2226 unsigned ref_bits = get_mode_size_bits(mode);
2228 if (is_Conv(left)) {
2229 ir_mode *lmode = get_irn_mode(left);
2230 unsigned bits = get_mode_size_bits(lmode);
2232 if (ref_bits == bits &&
2233 mode_is_int(lmode) &&
2234 get_mode_arithmetic(lmode) == irma_twos_complement) {
2235 ir_node *pre = get_Conv_op(left);
2236 ir_mode *pre_mode = get_irn_mode(pre);
2238 if (mode_is_int(pre_mode) &&
2239 get_mode_size_bits(pre_mode) == bits &&
2240 get_mode_arithmetic(pre_mode) == irma_twos_complement) {
2241 /* ok, this conv just changes to sign, moreover the calculation
2242 * is done with same number of bits as our address mode, so
2243 * we can ignore the conv as address calculation can be viewed
2244 * as either signed or unsigned
2246 set_binop_left(n, pre);
2251 if (is_Conv(right)) {
2252 ir_mode *rmode = get_irn_mode(right);
2253 unsigned bits = get_mode_size_bits(rmode);
2255 if (ref_bits == bits &&
2256 mode_is_int(rmode) &&
2257 get_mode_arithmetic(rmode) == irma_twos_complement) {
2258 ir_node *pre = get_Conv_op(right);
2259 ir_mode *pre_mode = get_irn_mode(pre);
2261 if (mode_is_int(pre_mode) &&
2262 get_mode_size_bits(pre_mode) == bits &&
2263 get_mode_arithmetic(pre_mode) == irma_twos_complement) {
2264 /* ok, this conv just changes to sign, moreover the calculation
2265 * is done with same number of bits as our address mode, so
2266 * we can ignore the conv as address calculation can be viewed
2267 * as either signed or unsigned
2269 set_binop_right(n, pre);
2274 /* let address arithmetic use unsigned modes */
2275 if (is_Const(right)) {
2276 ir_mode *rmode = get_irn_mode(right);
2278 if (mode_is_signed(rmode) && get_mode_arithmetic(rmode) == irma_twos_complement) {
2279 /* convert a AddP(P, *s) into AddP(P, *u) */
2280 ir_mode *nm = get_reference_mode_unsigned_eq(mode);
2282 ir_node *pre = new_r_Conv(get_nodes_block(n), right, nm);
2283 set_binop_right(n, pre);
2289 } /* transform_node_AddSub */
2291 #define HANDLE_BINOP_PHI(eval, a, b, c, mode) \
2294 if (is_Const(b) && is_const_Phi(a)) { \
2295 /* check for Op(Phi, Const) */ \
2296 c = apply_binop_on_phi(a, get_Const_tarval(b), eval, mode, 0);\
2298 else if (is_Const(a) && is_const_Phi(b)) { \
2299 /* check for Op(Const, Phi) */ \
2300 c = apply_binop_on_phi(b, get_Const_tarval(a), eval, mode, 1);\
2302 else if (is_const_Phi(a) && is_const_Phi(b)) { \
2303 /* check for Op(Phi, Phi) */ \
2304 c = apply_binop_on_2_phis(a, b, eval, mode); \
2307 DBG_OPT_ALGSIM0(oldn, c, FS_OPT_CONST_PHI); \
2312 #define HANDLE_UNOP_PHI(eval, a, c) \
2315 if (is_const_Phi(a)) { \
2316 /* check for Op(Phi) */ \
2317 c = apply_unop_on_phi(a, eval); \
2319 DBG_OPT_ALGSIM0(oldn, c, FS_OPT_CONST_PHI); \
2326 * Do the AddSub optimization, then Transform
2327 * Constant folding on Phi
2328 * Add(a,a) -> Mul(a, 2)
2329 * Add(Mul(a, x), a) -> Mul(a, x+1)
2330 * if the mode is integer or float.
2331 * Transform Add(a,-b) into Sub(a,b).
2332 * Reassociation might fold this further.
2334 static ir_node *transform_node_Add(ir_node *n)
2337 ir_node *a, *b, *c, *oldn = n;
2338 vrp_attr *a_vrp, *b_vrp;
2340 n = transform_node_AddSub(n);
2342 a = get_Add_left(n);
2343 b = get_Add_right(n);
2345 mode = get_irn_mode(n);
2347 if (mode_is_reference(mode)) {
2348 ir_mode *lmode = get_irn_mode(a);
2350 if (is_Const(b) && is_Const_null(b) && mode_is_int(lmode)) {
2351 /* an Add(a, NULL) is a hidden Conv */
2352 dbg_info *dbg = get_irn_dbg_info(n);
2353 return new_rd_Conv(dbg, get_nodes_block(n), a, mode);
2357 HANDLE_BINOP_PHI((eval_func) tarval_add, a, b, c, mode);
2359 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
2360 if (mode_is_float(mode)) {
2361 ir_graph *irg = get_irn_irg(n);
2362 if (get_irg_fp_model(irg) & fp_strict_algebraic)
2366 if (mode_is_num(mode)) {
2367 ir_graph *irg = get_irn_irg(n);
2368 /* the following code leads to endless recursion when Mul are replaced by a simple instruction chain */
2369 if (!is_irg_state(irg, IR_GRAPH_STATE_ARCH_DEP)
2370 && a == b && mode_is_int(mode)) {
2371 ir_node *block = get_nodes_block(n);
2374 get_irn_dbg_info(n),
2377 new_Const_long(mode, 2),
2379 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_A);
2384 get_irn_dbg_info(n),
2389 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_MINUS_B);
2394 get_irn_dbg_info(n),
2399 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_MINUS_B);
2402 if (get_mode_arithmetic(mode) == irma_twos_complement) {
2403 /* Here we rely on constants be on the RIGHT side */
2405 ir_node *op = get_Not_op(a);
2407 if (is_Const(b) && is_Const_one(b)) {
2409 ir_node *blk = get_nodes_block(n);
2410 n = new_rd_Minus(get_irn_dbg_info(n), blk, op, mode);
2411 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_PLUS_1);
2416 n = new_Const(get_mode_minus_one(mode));
2417 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_X_NOT_X);
2422 ir_node *op = get_Not_op(b);
2426 n = new_Const(get_mode_minus_one(mode));
2427 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_X_NOT_X);
2434 a_vrp = vrp_get_info(a);
2435 b_vrp = vrp_get_info(b);
2437 if (a_vrp && b_vrp) {
2438 tarval *c = tarval_and(
2439 a_vrp->bits_not_set,
2443 if (tarval_is_null(c)) {
2444 dbg_info *dbgi = get_irn_dbg_info(n);
2445 return new_rd_Or(dbgi, get_nodes_block(n),
2450 } /* transform_node_Add */
2453 * returns -cnst or NULL if impossible
2455 static ir_node *const_negate(ir_node *cnst)
2457 tarval *tv = tarval_neg(get_Const_tarval(cnst));
2458 dbg_info *dbgi = get_irn_dbg_info(cnst);
2459 ir_graph *irg = get_irn_irg(cnst);
2460 if (tv == tarval_bad) return NULL;
2461 return new_rd_Const(dbgi, irg, tv);
2465 * Do the AddSub optimization, then Transform
2466 * Constant folding on Phi
2467 * Sub(0,a) -> Minus(a)
2468 * Sub(Mul(a, x), a) -> Mul(a, x-1)
2469 * Sub(Sub(x, y), b) -> Sub(x, Add(y,b))
2470 * Sub(Add(a, x), x) -> a
2471 * Sub(x, Add(x, a)) -> -a
2472 * Sub(x, Const) -> Add(x, -Const)
2474 static ir_node *transform_node_Sub(ir_node *n)
2480 n = transform_node_AddSub(n);
2482 a = get_Sub_left(n);
2483 b = get_Sub_right(n);
2485 mode = get_irn_mode(n);
2487 if (mode_is_int(mode)) {
2488 ir_mode *lmode = get_irn_mode(a);
2490 if (is_Const(b) && is_Const_null(b) && mode_is_reference(lmode)) {
2491 /* a Sub(a, NULL) is a hidden Conv */
2492 dbg_info *dbg = get_irn_dbg_info(n);
2493 n = new_rd_Conv(dbg, get_nodes_block(n), a, mode);
2494 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_CONV);
2498 if (mode == lmode &&
2499 get_mode_arithmetic(mode) == irma_twos_complement &&
2501 get_Const_tarval(a) == get_mode_minus_one(mode)) {
2503 dbg_info *dbg = get_irn_dbg_info(n);
2504 n = new_rd_Not(dbg, get_nodes_block(n), b, mode);
2505 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_NOT);
2511 HANDLE_BINOP_PHI((eval_func) tarval_sub, a, b, c, mode);
2513 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
2514 if (mode_is_float(mode)) {
2515 ir_graph *irg = get_irn_irg(n);
2516 if (get_irg_fp_model(irg) & fp_strict_algebraic)
2520 if (is_Const(b) && !mode_is_reference(get_irn_mode(b))) {
2521 /* a - C -> a + (-C) */
2522 ir_node *cnst = const_negate(b);
2524 ir_node *block = get_nodes_block(n);
2525 dbg_info *dbgi = get_irn_dbg_info(n);
2527 n = new_rd_Add(dbgi, block, a, cnst, mode);
2528 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2533 if (is_Minus(a)) { /* (-a) - b -> -(a + b) */
2534 dbg_info *dbg = get_irn_dbg_info(n);
2535 ir_node *block = get_nodes_block(n);
2536 ir_node *left = get_Minus_op(a);
2537 ir_node *add = new_rd_Add(dbg, block, left, b, mode);
2539 n = new_rd_Minus(dbg, block, add, mode);
2540 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2542 } else if (is_Minus(b)) { /* a - (-b) -> a + b */
2543 dbg_info *dbg = get_irn_dbg_info(n);
2544 ir_node *block = get_nodes_block(n);
2545 ir_node *right = get_Minus_op(b);
2547 n = new_rd_Add(dbg, block, a, right, mode);
2548 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MINUS);
2550 } else if (is_Sub(b)) {
2551 /* a - (b - c) -> a + (c - b)
2552 * -> (a - b) + c iff (b - c) is a pointer */
2553 dbg_info *s_dbg = get_irn_dbg_info(b);
2554 ir_node *s_block = get_nodes_block(b);
2555 ir_node *s_left = get_Sub_left(b);
2556 ir_node *s_right = get_Sub_right(b);
2557 ir_mode *s_mode = get_irn_mode(b);
2558 if (mode_is_reference(s_mode)) {
2559 ir_node *sub = new_rd_Sub(s_dbg, s_block, a, s_left, mode);
2560 dbg_info *a_dbg = get_irn_dbg_info(n);
2561 ir_node *a_block = get_nodes_block(n);
2564 s_right = new_r_Conv(a_block, s_right, mode);
2565 n = new_rd_Add(a_dbg, a_block, sub, s_right, mode);
2567 ir_node *sub = new_rd_Sub(s_dbg, s_block, s_right, s_left, s_mode);
2568 dbg_info *a_dbg = get_irn_dbg_info(n);
2569 ir_node *a_block = get_nodes_block(n);
2571 n = new_rd_Add(a_dbg, a_block, a, sub, mode);
2573 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2575 } else if (is_Mul(b)) { /* a - (b * C) -> a + (b * -C) */
2576 ir_node *m_right = get_Mul_right(b);
2577 if (is_Const(m_right)) {
2578 ir_node *cnst2 = const_negate(m_right);
2579 if (cnst2 != NULL) {
2580 dbg_info *m_dbg = get_irn_dbg_info(b);
2581 ir_node *m_block = get_nodes_block(b);
2582 ir_node *m_left = get_Mul_left(b);
2583 ir_mode *m_mode = get_irn_mode(b);
2584 ir_node *mul = new_rd_Mul(m_dbg, m_block, m_left, cnst2, m_mode);
2585 dbg_info *a_dbg = get_irn_dbg_info(n);
2586 ir_node *a_block = get_nodes_block(n);
2588 n = new_rd_Add(a_dbg, a_block, a, mul, mode);
2589 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2595 /* Beware of Sub(P, P) which cannot be optimized into a simple Minus ... */
2596 if (mode_is_num(mode) && mode == get_irn_mode(a) && is_Const(a) && is_Const_null(a)) {
2598 get_irn_dbg_info(n),
2602 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_0_A);
2606 if (mode_wrap_around(mode)) {
2607 ir_node *left = get_Add_left(a);
2608 ir_node *right = get_Add_right(a);
2610 /* FIXME: Does the Conv's work only for two complement or generally? */
2612 if (mode != get_irn_mode(right)) {
2613 /* This Sub is an effective Cast */
2614 right = new_r_Conv(get_nodes_block(n), right, mode);
2617 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2619 } else if (right == b) {
2620 if (mode != get_irn_mode(left)) {
2621 /* This Sub is an effective Cast */
2622 left = new_r_Conv(get_nodes_block(n), left, mode);
2625 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2631 if (mode_wrap_around(mode)) {
2632 ir_node *left = get_Add_left(b);
2633 ir_node *right = get_Add_right(b);
2635 /* FIXME: Does the Conv's work only for two complement or generally? */
2637 ir_mode *r_mode = get_irn_mode(right);
2639 n = new_r_Minus(get_nodes_block(n), right, r_mode);
2640 if (mode != r_mode) {
2641 /* This Sub is an effective Cast */
2642 n = new_r_Conv(get_nodes_block(n), n, mode);
2644 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2646 } else if (right == a) {
2647 ir_mode *l_mode = get_irn_mode(left);
2649 n = new_r_Minus(get_nodes_block(n), left, l_mode);
2650 if (mode != l_mode) {
2651 /* This Sub is an effective Cast */
2652 n = new_r_Conv(get_nodes_block(n), n, mode);
2654 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2659 if (mode_is_int(mode) && is_Conv(a) && is_Conv(b)) {
2660 ir_mode *mode = get_irn_mode(a);
2662 if (mode == get_irn_mode(b)) {
2664 ir_node *op_a = get_Conv_op(a);
2665 ir_node *op_b = get_Conv_op(b);
2667 /* check if it's allowed to skip the conv */
2668 ma = get_irn_mode(op_a);
2669 mb = get_irn_mode(op_b);
2671 if (mode_is_reference(ma) && mode_is_reference(mb)) {
2672 /* SubInt(ConvInt(aP), ConvInt(bP)) -> SubInt(aP,bP) */
2675 set_Sub_right(n, b);
2681 /* do NOT execute this code if reassociation is enabled, it does the inverse! */
2682 if (!is_reassoc_running() && is_Mul(a)) {
2683 ir_node *ma = get_Mul_left(a);
2684 ir_node *mb = get_Mul_right(a);
2687 ir_node *blk = get_nodes_block(n);
2689 get_irn_dbg_info(n),
2693 get_irn_dbg_info(n),
2696 new_Const(get_mode_one(mode)),
2699 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MUL_A_X_A);
2701 } else if (mb == b) {
2702 ir_node *blk = get_nodes_block(n);
2704 get_irn_dbg_info(n),
2708 get_irn_dbg_info(n),
2711 new_Const(get_mode_one(mode)),
2714 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MUL_A_X_A);
2718 if (is_Sub(a)) { /* (x - y) - b -> x - (y + b) */
2719 ir_node *x = get_Sub_left(a);
2720 ir_node *y = get_Sub_right(a);
2721 ir_node *blk = get_nodes_block(n);
2722 ir_mode *m_b = get_irn_mode(b);
2723 ir_mode *m_y = get_irn_mode(y);
2727 /* Determine the right mode for the Add. */
2730 else if (mode_is_reference(m_b))
2732 else if (mode_is_reference(m_y))
2736 * Both modes are different but none is reference,
2737 * happens for instance in SubP(SubP(P, Iu), Is).
2738 * We have two possibilities here: Cast or ignore.
2739 * Currently we ignore this case.
2744 add = new_r_Add(blk, y, b, add_mode);
2746 n = new_rd_Sub(get_irn_dbg_info(n), blk, x, add, mode);
2747 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_SUB_X_Y_Z);
2751 if (get_mode_arithmetic(mode) == irma_twos_complement) {
2752 if (is_Const(a) && is_Not(b)) {
2753 /* c - ~X = X + (c+1) */
2754 tarval *tv = get_Const_tarval(a);
2756 tv = tarval_add(tv, get_mode_one(mode));
2757 if (tv != tarval_bad) {
2758 ir_node *blk = get_nodes_block(n);
2759 ir_node *c = new_Const(tv);
2760 n = new_rd_Add(get_irn_dbg_info(n), blk, get_Not_op(b), c, mode);
2761 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_C_NOT_X);
2767 } /* transform_node_Sub */
2770 * Several transformation done on n*n=2n bits mul.
2771 * These transformations must be done here because new nodes may be produced.
2773 static ir_node *transform_node_Mul2n(ir_node *n, ir_mode *mode)
2776 ir_node *a = get_Mul_left(n);
2777 ir_node *b = get_Mul_right(n);
2778 tarval *ta = value_of(a);
2779 tarval *tb = value_of(b);
2780 ir_mode *smode = get_irn_mode(a);
2782 if (ta == get_mode_one(smode)) {
2783 /* (L)1 * (L)b = (L)b */
2784 ir_node *blk = get_nodes_block(n);
2785 n = new_rd_Conv(get_irn_dbg_info(n), blk, b, mode);
2786 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
2789 else if (ta == get_mode_minus_one(smode)) {
2790 /* (L)-1 * (L)b = (L)b */
2791 ir_node *blk = get_nodes_block(n);
2792 n = new_rd_Minus(get_irn_dbg_info(n), blk, b, smode);
2793 n = new_rd_Conv(get_irn_dbg_info(n), blk, n, mode);
2794 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2797 if (tb == get_mode_one(smode)) {
2798 /* (L)a * (L)1 = (L)a */
2799 ir_node *blk = get_irn_n(a, -1);
2800 n = new_rd_Conv(get_irn_dbg_info(n), blk, a, mode);
2801 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
2804 else if (tb == get_mode_minus_one(smode)) {
2805 /* (L)a * (L)-1 = (L)-a */
2806 ir_node *blk = get_nodes_block(n);
2807 n = new_rd_Minus(get_irn_dbg_info(n), blk, a, smode);
2808 n = new_rd_Conv(get_irn_dbg_info(n), blk, n, mode);
2809 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2816 * Transform Mul(a,-1) into -a.
2817 * Do constant evaluation of Phi nodes.
2818 * Do architecture dependent optimizations on Mul nodes
2820 static ir_node *transform_node_Mul(ir_node *n)
2822 ir_node *c, *oldn = n;
2823 ir_mode *mode = get_irn_mode(n);
2824 ir_node *a = get_Mul_left(n);
2825 ir_node *b = get_Mul_right(n);
2827 if (is_Bad(a) || is_Bad(b))
2830 if (mode != get_irn_mode(a))
2831 return transform_node_Mul2n(n, mode);
2833 HANDLE_BINOP_PHI((eval_func) tarval_mul, a, b, c, mode);
2835 if (mode_is_signed(mode)) {
2838 if (value_of(a) == get_mode_minus_one(mode))
2840 else if (value_of(b) == get_mode_minus_one(mode))
2843 n = new_rd_Minus(get_irn_dbg_info(n), get_nodes_block(n), r, mode);
2844 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2849 if (is_Const(b)) { /* (-a) * const -> a * -const */
2850 ir_node *cnst = const_negate(b);
2852 dbg_info *dbgi = get_irn_dbg_info(n);
2853 ir_node *block = get_nodes_block(n);
2854 n = new_rd_Mul(dbgi, block, get_Minus_op(a), cnst, mode);
2855 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2858 } else if (is_Minus(b)) { /* (-a) * (-b) -> a * b */
2859 dbg_info *dbgi = get_irn_dbg_info(n);
2860 ir_node *block = get_nodes_block(n);
2861 n = new_rd_Mul(dbgi, block, get_Minus_op(a), get_Minus_op(b), mode);
2862 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_MINUS);
2864 } else if (is_Sub(b)) { /* (-a) * (b - c) -> a * (c - b) */
2865 ir_node *sub_l = get_Sub_left(b);
2866 ir_node *sub_r = get_Sub_right(b);
2867 dbg_info *dbgi = get_irn_dbg_info(n);
2868 ir_node *block = get_nodes_block(n);
2869 ir_node *new_b = new_rd_Sub(dbgi, block, sub_r, sub_l, mode);
2870 n = new_rd_Mul(dbgi, block, get_Minus_op(a), new_b, mode);
2871 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS);
2874 } else if (is_Minus(b)) {
2875 if (is_Sub(a)) { /* (a - b) * (-c) -> (b - a) * c */
2876 ir_node *sub_l = get_Sub_left(a);
2877 ir_node *sub_r = get_Sub_right(a);
2878 dbg_info *dbgi = get_irn_dbg_info(n);
2879 ir_node *block = get_nodes_block(n);
2880 ir_node *new_a = new_rd_Sub(dbgi, block, sub_r, sub_l, mode);
2881 n = new_rd_Mul(dbgi, block, new_a, get_Minus_op(b), mode);
2882 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS);
2885 } else if (is_Shl(a)) {
2886 ir_node *const shl_l = get_Shl_left(a);
2887 if (is_Const(shl_l) && is_Const_one(shl_l)) {
2888 /* (1 << x) * b -> b << x */
2889 dbg_info *const dbgi = get_irn_dbg_info(n);
2890 ir_node *const block = get_nodes_block(n);
2891 ir_node *const shl_r = get_Shl_right(a);
2892 n = new_rd_Shl(dbgi, block, b, shl_r, mode);
2893 // TODO add me DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_SHIFT);
2896 } else if (is_Shl(b)) {
2897 ir_node *const shl_l = get_Shl_left(b);
2898 if (is_Const(shl_l) && is_Const_one(shl_l)) {
2899 /* a * (1 << x) -> a << x */
2900 dbg_info *const dbgi = get_irn_dbg_info(n);
2901 ir_node *const block = get_nodes_block(n);
2902 ir_node *const shl_r = get_Shl_right(b);
2903 n = new_rd_Shl(dbgi, block, a, shl_r, mode);
2904 // TODO add me DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_SHIFT);
2908 if (get_mode_arithmetic(mode) == irma_ieee754) {
2910 tarval *tv = get_Const_tarval(a);
2911 if (tarval_ieee754_get_exponent(tv) == 1 && tarval_ieee754_zero_mantissa(tv)
2912 && !tarval_is_negative(tv)) {
2913 /* 2.0 * b = b + b */
2914 n = new_rd_Add(get_irn_dbg_info(n), get_nodes_block(n), b, b, mode);
2915 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_A_A);
2919 else if (is_Const(b)) {
2920 tarval *tv = get_Const_tarval(b);
2921 if (tarval_ieee754_get_exponent(tv) == 1 && tarval_ieee754_zero_mantissa(tv)
2922 && !tarval_is_negative(tv)) {
2923 /* a * 2.0 = a + a */
2924 n = new_rd_Add(get_irn_dbg_info(n), get_nodes_block(n), a, a, mode);
2925 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_A_A);
2930 return arch_dep_replace_mul_with_shifts(n);
2931 } /* transform_node_Mul */
2934 * Transform a Div Node.
2936 static ir_node *transform_node_Div(ir_node *n)
2938 ir_mode *mode = get_Div_resmode(n);
2939 ir_node *a = get_Div_left(n);
2940 ir_node *b = get_Div_right(n);
2942 const ir_node *dummy;
2944 if (is_Const(b) && is_const_Phi(a)) {
2945 /* check for Div(Phi, Const) */
2946 value = apply_binop_on_phi(a, get_Const_tarval(b), (eval_func) tarval_div, mode, 0);
2948 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2952 else if (is_Const(a) && is_const_Phi(b)) {
2953 /* check for Div(Const, Phi) */
2954 value = apply_binop_on_phi(b, get_Const_tarval(a), (eval_func) tarval_div, mode, 1);
2956 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2960 else if (is_const_Phi(a) && is_const_Phi(b)) {
2961 /* check for Div(Phi, Phi) */
2962 value = apply_binop_on_2_phis(a, b, (eval_func) tarval_div, mode);
2964 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2971 if (a == b && value_not_zero(a, &dummy)) {
2972 /* BEWARE: we can optimize a/a to 1 only if this cannot cause a exception */
2973 value = new_Const(get_mode_one(mode));
2974 DBG_OPT_CSTEVAL(n, value);
2977 if (mode_is_signed(mode) && is_Const(b)) {
2978 tarval *tv = get_Const_tarval(b);
2980 if (tv == get_mode_minus_one(mode)) {
2982 value = new_rd_Minus(get_irn_dbg_info(n), get_nodes_block(n), a, mode);
2983 DBG_OPT_CSTEVAL(n, value);
2987 /* Try architecture dependent optimization */
2988 value = arch_dep_replace_div_by_const(n);
2995 /* Turn Div into a tuple (mem, jmp, bad, value) */
2996 mem = get_Div_mem(n);
2997 blk = get_nodes_block(n);
2999 /* skip a potential Pin */
3000 mem = skip_Pin(mem);
3001 turn_into_tuple(n, pn_Div_max);
3002 set_Tuple_pred(n, pn_Div_M, mem);
3003 set_Tuple_pred(n, pn_Div_X_regular, new_r_Jmp(blk));
3004 set_Tuple_pred(n, pn_Div_X_except, new_Bad());
3005 set_Tuple_pred(n, pn_Div_res, value);
3008 } /* transform_node_Div */
3011 * Transform a Mod node.
3013 static ir_node *transform_node_Mod(ir_node *n)
3015 ir_mode *mode = get_Mod_resmode(n);
3016 ir_node *a = get_Mod_left(n);
3017 ir_node *b = get_Mod_right(n);
3021 if (is_Const(b) && is_const_Phi(a)) {
3022 /* check for Div(Phi, Const) */
3023 value = apply_binop_on_phi(a, get_Const_tarval(b), (eval_func) tarval_mod, mode, 0);
3025 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
3029 else if (is_Const(a) && is_const_Phi(b)) {
3030 /* check for Div(Const, Phi) */
3031 value = apply_binop_on_phi(b, get_Const_tarval(a), (eval_func) tarval_mod, mode, 1);
3033 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
3037 else if (is_const_Phi(a) && is_const_Phi(b)) {
3038 /* check for Div(Phi, Phi) */
3039 value = apply_binop_on_2_phis(a, b, (eval_func) tarval_mod, mode);
3041 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
3048 if (tv != tarval_bad) {
3049 value = new_Const(tv);
3051 DBG_OPT_CSTEVAL(n, value);
3054 ir_node *a = get_Mod_left(n);
3055 ir_node *b = get_Mod_right(n);
3056 const ir_node *dummy;
3058 if (a == b && value_not_zero(a, &dummy)) {
3059 /* BEWARE: we can optimize a%a to 0 only if this cannot cause a exception */
3060 value = new_Const(get_mode_null(mode));
3061 DBG_OPT_CSTEVAL(n, value);
3064 if (mode_is_signed(mode) && is_Const(b)) {
3065 tarval *tv = get_Const_tarval(b);
3067 if (tv == get_mode_minus_one(mode)) {
3069 value = new_Const(get_mode_null(mode));
3070 DBG_OPT_CSTEVAL(n, value);
3074 /* Try architecture dependent optimization */
3075 value = arch_dep_replace_mod_by_const(n);
3083 /* Turn Mod into a tuple (mem, jmp, bad, value) */
3084 mem = get_Mod_mem(n);
3085 blk = get_nodes_block(n);
3087 /* skip a potential Pin */
3088 mem = skip_Pin(mem);
3089 turn_into_tuple(n, pn_Mod_max);
3090 set_Tuple_pred(n, pn_Mod_M, mem);
3091 set_Tuple_pred(n, pn_Mod_X_regular, new_r_Jmp(blk));
3092 set_Tuple_pred(n, pn_Mod_X_except, new_Bad());
3093 set_Tuple_pred(n, pn_Mod_res, value);
3096 } /* transform_node_Mod */
3099 * Transform a DivMod node.
3101 static ir_node *transform_node_DivMod(ir_node *n)
3103 const ir_node *dummy;
3104 ir_node *a = get_DivMod_left(n);
3105 ir_node *b = get_DivMod_right(n);
3106 ir_mode *mode = get_DivMod_resmode(n);
3111 if (is_Const(b) && is_const_Phi(a)) {
3112 /* check for Div(Phi, Const) */
3113 va = apply_binop_on_phi(a, get_Const_tarval(b), (eval_func) tarval_div, mode, 0);
3114 vb = apply_binop_on_phi(a, get_Const_tarval(b), (eval_func) tarval_mod, mode, 0);
3116 DBG_OPT_ALGSIM0(n, va, FS_OPT_CONST_PHI);
3117 DBG_OPT_ALGSIM0(n, vb, FS_OPT_CONST_PHI);
3121 else if (is_Const(a) && is_const_Phi(b)) {
3122 /* check for Div(Const, Phi) */
3123 va = apply_binop_on_phi(b, get_Const_tarval(a), (eval_func) tarval_div, mode, 1);
3124 vb = apply_binop_on_phi(b, get_Const_tarval(a), (eval_func) tarval_mod, mode, 1);
3126 DBG_OPT_ALGSIM0(n, va, FS_OPT_CONST_PHI);
3127 DBG_OPT_ALGSIM0(n, vb, FS_OPT_CONST_PHI);
3131 else if (is_const_Phi(a) && is_const_Phi(b)) {
3132 /* check for Div(Phi, Phi) */
3133 va = apply_binop_on_2_phis(a, b, (eval_func) tarval_div, mode);
3134 vb = apply_binop_on_2_phis(a, b, (eval_func) tarval_mod, mode);
3136 DBG_OPT_ALGSIM0(n, va, FS_OPT_CONST_PHI);
3137 DBG_OPT_ALGSIM0(n, vb, FS_OPT_CONST_PHI);
3144 if (tb != tarval_bad) {
3145 if (tb == get_mode_one(get_tarval_mode(tb))) {
3147 vb = new_Const(get_mode_null(mode));
3148 DBG_OPT_CSTEVAL(n, vb);
3150 } else if (ta != tarval_bad) {
3151 tarval *resa, *resb;
3152 resa = tarval_div(ta, tb);
3153 if (resa == tarval_bad) return n; /* Causes exception!!! Model by replacing through
3154 Jmp for X result!? */
3155 resb = tarval_mod(ta, tb);
3156 if (resb == tarval_bad) return n; /* Causes exception! */
3157 va = new_Const(resa);
3158 vb = new_Const(resb);
3159 DBG_OPT_CSTEVAL(n, va);
3160 DBG_OPT_CSTEVAL(n, vb);
3162 } else if (mode_is_signed(mode) && tb == get_mode_minus_one(mode)) {
3163 va = new_rd_Minus(get_irn_dbg_info(n), get_nodes_block(n), a, mode);
3164 vb = new_Const(get_mode_null(mode));
3165 DBG_OPT_CSTEVAL(n, va);
3166 DBG_OPT_CSTEVAL(n, vb);
3168 } else { /* Try architecture dependent optimization */
3171 arch_dep_replace_divmod_by_const(&va, &vb, n);
3172 evaluated = va != NULL;
3174 } else if (a == b) {
3175 if (value_not_zero(a, &dummy)) {
3177 va = new_Const(get_mode_one(mode));
3178 vb = new_Const(get_mode_null(mode));
3179 DBG_OPT_CSTEVAL(n, va);
3180 DBG_OPT_CSTEVAL(n, vb);
3183 /* BEWARE: it is NOT possible to optimize a/a to 1, as this may cause a exception */
3186 } else if (ta == get_mode_null(mode) && value_not_zero(b, &dummy)) {
3187 /* 0 / non-Const = 0 */
3192 if (evaluated) { /* replace by tuple */
3196 mem = get_DivMod_mem(n);
3197 /* skip a potential Pin */
3198 mem = skip_Pin(mem);
3200 blk = get_nodes_block(n);
3201 turn_into_tuple(n, pn_DivMod_max);
3202 set_Tuple_pred(n, pn_DivMod_M, mem);
3203 set_Tuple_pred(n, pn_DivMod_X_regular, new_r_Jmp(blk));
3204 set_Tuple_pred(n, pn_DivMod_X_except, new_Bad()); /* no exception */
3205 set_Tuple_pred(n, pn_DivMod_res_div, va);
3206 set_Tuple_pred(n, pn_DivMod_res_mod, vb);
3210 } /* transform_node_DivMod */
3213 * Optimize x / c to x * (1/c)
3215 static ir_node *transform_node_Quot(ir_node *n)
3217 ir_mode *mode = get_Quot_resmode(n);
3220 if (get_mode_arithmetic(mode) == irma_ieee754) {
3221 ir_node *b = get_Quot_right(n);
3222 tarval *tv = value_of(b);
3224 if (tv != tarval_bad) {
3225 int rem = tarval_fp_ops_enabled();
3228 * Floating point constant folding might be disabled here to
3230 * However, as we check for exact result, doing it is safe.
3233 tarval_enable_fp_ops(1);
3234 tv = tarval_quo(get_mode_one(mode), tv);
3235 tarval_enable_fp_ops(rem);
3237 /* Do the transformation if the result is either exact or we are not
3238 using strict rules. */
3239 if (tv != tarval_bad &&
3240 (tarval_ieee754_get_exact() || (get_irg_fp_model(get_irn_irg(n)) & fp_strict_algebraic) == 0)) {
3241 ir_node *blk = get_nodes_block(n);
3242 ir_node *c = new_Const(tv);
3243 ir_node *a = get_Quot_left(n);
3244 ir_node *m = new_rd_Mul(get_irn_dbg_info(n), blk, a, c, mode);
3245 ir_node *mem = get_Quot_mem(n);
3247 /* skip a potential Pin */
3248 mem = skip_Pin(mem);
3249 turn_into_tuple(n, pn_Quot_max);
3250 set_Tuple_pred(n, pn_Quot_M, mem);
3251 set_Tuple_pred(n, pn_Quot_X_regular, new_r_Jmp(blk));
3252 set_Tuple_pred(n, pn_Quot_X_except, new_Bad());
3253 set_Tuple_pred(n, pn_Quot_res, m);
3254 DBG_OPT_ALGSIM1(oldn, a, b, m, FS_OPT_FP_INV_MUL);
3259 } /* transform_node_Quot */
3262 * Optimize -a CMP -b into b CMP a.
3263 * This works only for for modes where unary Minus
3265 * Note that two-complement integers can Overflow
3266 * so it will NOT work.
3268 * For == and != can be handled in Proj(Cmp)
3270 static ir_node *transform_node_Cmp(ir_node *n)
3273 ir_node *left = get_Cmp_left(n);
3274 ir_node *right = get_Cmp_right(n);
3276 if (is_Minus(left) && is_Minus(right) &&
3277 !mode_overflow_on_unary_Minus(get_irn_mode(left))) {
3278 ir_node *const new_left = get_Minus_op(right);
3279 ir_node *const new_right = get_Minus_op(left);
3280 n = new_rd_Cmp(get_irn_dbg_info(n), get_nodes_block(n), new_left, new_right);
3281 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CMP_OP_OP);
3284 } /* transform_node_Cmp */
3288 * Transform a Cond node.
3290 * Replace the Cond by a Jmp if it branches on a constant
3293 static ir_node *transform_node_Cond(ir_node *n)
3297 ir_node *a = get_Cond_selector(n);
3298 tarval *ta = value_of(a);
3299 ir_graph *irg = get_irn_irg(n);
3301 /* we need block info which is not available in floating irgs */
3302 if (get_irg_pinned(irg) == op_pin_state_floats)
3305 if ((ta != tarval_bad) &&
3306 (get_irn_mode(a) == mode_b) &&
3307 (get_opt_unreachable_code())) {
3308 /* It's a boolean Cond, branching on a boolean constant.
3309 Replace it by a tuple (Bad, Jmp) or (Jmp, Bad) */
3310 ir_node *blk = get_nodes_block(n);
3311 jmp = new_r_Jmp(blk);
3312 turn_into_tuple(n, pn_Cond_max);
3313 if (ta == tarval_b_true) {
3314 set_Tuple_pred(n, pn_Cond_false, new_Bad());
3315 set_Tuple_pred(n, pn_Cond_true, jmp);
3317 set_Tuple_pred(n, pn_Cond_false, jmp);
3318 set_Tuple_pred(n, pn_Cond_true, new_Bad());
3320 /* We might generate an endless loop, so keep it alive. */
3321 add_End_keepalive(get_irg_end(irg), blk);
3324 } /* transform_node_Cond */
3327 * Prototype of a recursive transform function
3328 * for bitwise distributive transformations.
3330 typedef ir_node* (*recursive_transform)(ir_node *n);
3333 * makes use of distributive laws for and, or, eor
3334 * and(a OP c, b OP c) -> and(a, b) OP c
3335 * note, might return a different op than n
3337 static ir_node *transform_bitwise_distributive(ir_node *n,
3338 recursive_transform trans_func)
3341 ir_node *a = get_binop_left(n);
3342 ir_node *b = get_binop_right(n);
3343 ir_op *op = get_irn_op(a);
3344 ir_op *op_root = get_irn_op(n);
3346 if (op != get_irn_op(b))
3349 /* and(conv(a), conv(b)) -> conv(and(a,b)) */
3350 if (op == op_Conv) {
3351 ir_node *a_op = get_Conv_op(a);
3352 ir_node *b_op = get_Conv_op(b);
3353 ir_mode *a_mode = get_irn_mode(a_op);
3354 ir_mode *b_mode = get_irn_mode(b_op);
3355 if (a_mode == b_mode && (mode_is_int(a_mode) || a_mode == mode_b)) {
3356 ir_node *blk = get_nodes_block(n);
3359 set_binop_left(n, a_op);
3360 set_binop_right(n, b_op);
3361 set_irn_mode(n, a_mode);
3363 n = new_r_Conv(blk, n, get_irn_mode(oldn));
3365 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
3371 /* nothing to gain here */
3375 if (op == op_Shrs || op == op_Shr || op == op_Shl
3376 || op == op_And || op == op_Or || op == op_Eor) {
3377 ir_node *a_left = get_binop_left(a);
3378 ir_node *a_right = get_binop_right(a);
3379 ir_node *b_left = get_binop_left(b);
3380 ir_node *b_right = get_binop_right(b);
3382 ir_node *op1 = NULL;
3383 ir_node *op2 = NULL;
3385 if (is_op_commutative(op)) {
3386 if (a_left == b_left) {
3390 } else if (a_left == b_right) {
3394 } else if (a_right == b_left) {
3400 if (a_right == b_right) {
3407 /* (a sop c) & (b sop c) => (a & b) sop c */
3408 ir_node *blk = get_nodes_block(n);
3410 ir_node *new_n = exact_copy(n);
3411 set_binop_left(new_n, op1);
3412 set_binop_right(new_n, op2);
3413 new_n = trans_func(new_n);
3415 if (op_root == op_Eor && op == op_Or) {
3416 dbg_info *dbgi = get_irn_dbg_info(n);
3417 ir_mode *mode = get_irn_mode(c);
3419 c = new_rd_Not(dbgi, blk, c, mode);
3420 n = new_rd_And(dbgi, blk, new_n, c, mode);
3423 set_nodes_block(n, blk);
3424 set_binop_left(n, new_n);
3425 set_binop_right(n, c);
3429 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_SHIFT_AND);
3440 static ir_node *transform_node_And(ir_node *n)
3442 ir_node *c, *oldn = n;
3443 ir_node *a = get_And_left(n);
3444 ir_node *b = get_And_right(n);
3446 vrp_attr *a_vrp, *b_vrp;
3448 mode = get_irn_mode(n);
3449 HANDLE_BINOP_PHI((eval_func) tarval_and, a, b, c, mode);
3451 /* we can evaluate 2 Projs of the same Cmp */
3452 if (mode == mode_b && is_Proj(a) && is_Proj(b)) {
3453 ir_node *pred_a = get_Proj_pred(a);
3454 ir_node *pred_b = get_Proj_pred(b);
3455 if (pred_a == pred_b) {
3456 dbg_info *dbgi = get_irn_dbg_info(n);
3457 pn_Cmp pn_a = get_Proj_proj(a);
3458 pn_Cmp pn_b = get_Proj_proj(b);
3459 /* yes, we can simply calculate with pncs */
3460 pn_Cmp new_pnc = pn_a & pn_b;
3462 return new_rd_Proj(dbgi, pred_a, mode_b, new_pnc);
3467 ir_node *op = get_Not_op(b);
3469 ir_node *ba = get_And_left(op);
3470 ir_node *bb = get_And_right(op);
3472 /* it's enough to test the following cases due to normalization! */
3473 if (get_Or_left(a) == ba && get_Or_right(a) == bb) {
3474 /* (a|b) & ~(a&b) = a^b */
3475 ir_node *block = get_nodes_block(n);
3477 n = new_rd_Eor(get_irn_dbg_info(n), block, ba, bb, mode);
3478 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_TO_EOR);
3486 ir_node *op = get_Not_op(a);
3488 ir_node *aa = get_And_left(op);
3489 ir_node *ab = get_And_right(op);
3491 /* it's enough to test the following cases due to normalization! */
3492 if (get_Or_left(b) == aa && get_Or_right(b) == ab) {
3493 /* (a|b) & ~(a&b) = a^b */
3494 ir_node *block = get_nodes_block(n);
3496 n = new_rd_Eor(get_irn_dbg_info(n), block, aa, ab, mode);
3497 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_TO_EOR);
3504 ir_node *al = get_Eor_left(a);
3505 ir_node *ar = get_Eor_right(a);
3508 /* (b ^ a) & b -> ~a & b */
3509 dbg_info *dbg = get_irn_dbg_info(n);
3510 ir_node *block = get_nodes_block(n);
3512 ar = new_rd_Not(dbg, block, ar, mode);
3513 n = new_rd_And(dbg, block, ar, b, mode);
3514 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3518 /* (a ^ b) & b -> ~a & b */
3519 dbg_info *dbg = get_irn_dbg_info(n);
3520 ir_node *block = get_nodes_block(n);
3522 al = new_rd_Not(dbg, block, al, mode);
3523 n = new_rd_And(dbg, block, al, b, mode);
3524 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3529 ir_node *bl = get_Eor_left(b);
3530 ir_node *br = get_Eor_right(b);
3533 /* a & (a ^ b) -> a & ~b */
3534 dbg_info *dbg = get_irn_dbg_info(n);
3535 ir_node *block = get_nodes_block(n);
3537 br = new_rd_Not(dbg, block, br, mode);
3538 n = new_rd_And(dbg, block, br, a, mode);
3539 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3543 /* a & (b ^ a) -> a & ~b */
3544 dbg_info *dbg = get_irn_dbg_info(n);
3545 ir_node *block = get_nodes_block(n);
3547 bl = new_rd_Not(dbg, block, bl, mode);
3548 n = new_rd_And(dbg, block, bl, a, mode);
3549 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3553 if (is_Not(a) && is_Not(b)) {
3554 /* ~a & ~b = ~(a|b) */
3555 ir_node *block = get_nodes_block(n);
3556 ir_mode *mode = get_irn_mode(n);
3560 n = new_rd_Or(get_irn_dbg_info(n), block, a, b, mode);
3561 n = new_rd_Not(get_irn_dbg_info(n), block, n, mode);
3562 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_DEMORGAN);
3566 b_vrp = vrp_get_info(b);
3567 if (is_Const(a) && b_vrp && (tarval_cmp(tarval_or(get_Const_tarval(a),
3568 b_vrp->bits_not_set), get_Const_tarval(a)) == pn_Cmp_Eq)) {
3574 a_vrp = vrp_get_info(a);
3575 if (is_Const(b) && a_vrp && (tarval_cmp(tarval_or(get_Const_tarval(b),
3576 a_vrp->bits_not_set), get_Const_tarval(b)) == pn_Cmp_Eq)) {
3580 n = transform_bitwise_distributive(n, transform_node_And);
3583 } /* transform_node_And */
3585 /* the order of the values is important! */
3586 typedef enum const_class {
3592 static const_class classify_const(const ir_node* n)
3594 if (is_Const(n)) return const_const;
3595 if (is_irn_constlike(n)) return const_like;
3600 * Determines whether r is more constlike or has a larger index (in that order)
3603 static bool operands_are_normalized(const ir_node *l, const ir_node *r)
3605 const const_class l_order = classify_const(l);
3606 const const_class r_order = classify_const(r);
3608 l_order > r_order ||
3609 (l_order == r_order && get_irn_idx(l) <= get_irn_idx(r));
3615 static ir_node *transform_node_Eor(ir_node *n)
3617 ir_node *c, *oldn = n;
3618 ir_node *a = get_Eor_left(n);
3619 ir_node *b = get_Eor_right(n);
3620 ir_mode *mode = get_irn_mode(n);
3622 HANDLE_BINOP_PHI((eval_func) tarval_eor, a, b, c, mode);
3624 /* we can evaluate 2 Projs of the same Cmp */
3625 if (mode == mode_b && is_Proj(a) && is_Proj(b)) {
3626 ir_node *pred_a = get_Proj_pred(a);
3627 ir_node *pred_b = get_Proj_pred(b);
3628 if (pred_a == pred_b) {
3629 dbg_info *dbgi = get_irn_dbg_info(n);
3630 pn_Cmp pn_a = get_Proj_proj(a);
3631 pn_Cmp pn_b = get_Proj_proj(b);
3632 /* yes, we can simply calculate with pncs */
3633 pn_Cmp new_pnc = pn_a ^ pn_b;
3635 return new_rd_Proj(dbgi, pred_a, mode_b, new_pnc);
3639 /* normalize not nodes... ~a ^ b <=> a ^ ~b */
3640 if (is_Not(a) && operands_are_normalized(get_Not_op(a), b)) {
3641 dbg_info *dbg = get_irn_dbg_info(n);
3642 ir_node *block = get_nodes_block(n);
3643 ir_node *new_not = new_rd_Not(dbg, block, b, mode);
3644 ir_node *new_left = get_Not_op(a);
3645 n = new_rd_Eor(dbg, block, new_left, new_not, mode);
3646 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3648 } else if (is_Not(b) && !operands_are_normalized(a, get_Not_op(b))) {
3649 dbg_info *dbg = get_irn_dbg_info(n);
3650 ir_node *block = get_nodes_block(n);
3651 ir_node *new_not = new_rd_Not(dbg, block, a, mode);
3652 ir_node *new_right = get_Not_op(b);
3653 n = new_rd_Eor(dbg, block, new_not, new_right, mode);
3654 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3658 /* x ^ 1...1 -> ~1 */
3659 if (is_Const(b) && is_Const_all_one(b)) {
3660 n = new_r_Not(get_nodes_block(n), a, mode);
3661 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3665 n = transform_bitwise_distributive(n, transform_node_Eor);
3667 } /* transform_node_Eor */
3672 static ir_node *transform_node_Not(ir_node *n)
3674 ir_node *c, *oldn = n;
3675 ir_node *a = get_Not_op(n);
3676 ir_mode *mode = get_irn_mode(n);
3678 HANDLE_UNOP_PHI(tarval_not,a,c);
3680 /* check for a boolean Not */
3681 if (mode == mode_b && is_Proj(a)) {
3682 ir_node *a_pred = get_Proj_pred(a);
3683 if (is_Cmp(a_pred)) {
3684 /* We negate a Cmp. The Cmp has the negated result anyways! */
3685 n = new_r_Proj(get_Proj_pred(a),
3686 mode_b, get_negated_pnc(get_Proj_proj(a), mode_b));
3687 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_CMP);
3692 /* normalize ~(a ^ b) => a ^ ~b */
3694 dbg_info *dbg = get_irn_dbg_info(n);
3695 ir_node *block = get_nodes_block(n);
3696 ir_node *eor_right = get_Eor_right(a);
3697 ir_node *eor_left = get_Eor_left(a);
3698 eor_right = new_rd_Not(dbg, block, eor_right, mode);
3699 n = new_rd_Eor(dbg, block, eor_left, eor_right, mode);
3703 if (get_mode_arithmetic(mode) == irma_twos_complement) {
3704 if (is_Minus(a)) { /* ~-x -> x + -1 */
3705 dbg_info *dbg = get_irn_dbg_info(n);
3706 ir_graph *irg = get_irn_irg(n);
3707 ir_node *block = get_nodes_block(n);
3708 ir_node *add_l = get_Minus_op(a);
3709 ir_node *add_r = new_rd_Const(dbg, irg, get_mode_minus_one(mode));
3710 n = new_rd_Add(dbg, block, add_l, add_r, mode);
3711 } else if (is_Add(a)) {
3712 ir_node *add_r = get_Add_right(a);
3713 if (is_Const(add_r) && is_Const_all_one(add_r)) {
3714 /* ~(x + -1) = -x */
3715 ir_node *op = get_Add_left(a);
3716 ir_node *blk = get_nodes_block(n);
3717 n = new_rd_Minus(get_irn_dbg_info(n), blk, op, get_irn_mode(n));
3718 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_MINUS_1);
3723 } /* transform_node_Not */
3726 * Transform a Minus.
3730 * -(a >>u (size-1)) = a >>s (size-1)
3731 * -(a >>s (size-1)) = a >>u (size-1)
3732 * -(a * const) -> a * -const
3734 static ir_node *transform_node_Minus(ir_node *n)
3736 ir_node *c, *oldn = n;
3737 ir_node *a = get_Minus_op(n);
3740 HANDLE_UNOP_PHI(tarval_neg,a,c);
3742 mode = get_irn_mode(a);
3743 if (get_mode_arithmetic(mode) == irma_twos_complement) {
3744 /* the following rules are only to twos-complement */
3747 ir_node *op = get_Not_op(a);
3748 tarval *tv = get_mode_one(mode);
3749 ir_node *blk = get_nodes_block(n);
3750 ir_node *c = new_Const(tv);
3751 n = new_rd_Add(get_irn_dbg_info(n), blk, op, c, mode);
3752 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_MINUS_NOT);
3756 ir_node *c = get_Shr_right(a);
3759 tarval *tv = get_Const_tarval(c);
3761 if (tarval_is_long(tv) && get_tarval_long(tv) == (int) get_mode_size_bits(mode) - 1) {
3762 /* -(a >>u (size-1)) = a >>s (size-1) */
3763 ir_node *v = get_Shr_left(a);
3765 n = new_rd_Shrs(get_irn_dbg_info(n), get_nodes_block(n), v, c, mode);
3766 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_PREDICATE);
3772 ir_node *c = get_Shrs_right(a);
3775 tarval *tv = get_Const_tarval(c);
3777 if (tarval_is_long(tv) && get_tarval_long(tv) == (int) get_mode_size_bits(mode) - 1) {
3778 /* -(a >>s (size-1)) = a >>u (size-1) */
3779 ir_node *v = get_Shrs_left(a);
3781 n = new_rd_Shr(get_irn_dbg_info(n), get_nodes_block(n), v, c, mode);
3782 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_PREDICATE);
3789 /* - (a-b) = b - a */
3790 ir_node *la = get_Sub_left(a);
3791 ir_node *ra = get_Sub_right(a);
3792 ir_node *blk = get_nodes_block(n);
3794 n = new_rd_Sub(get_irn_dbg_info(n), blk, ra, la, mode);
3795 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_MINUS_SUB);
3799 if (is_Mul(a)) { /* -(a * const) -> a * -const */
3800 ir_node *mul_l = get_Mul_left(a);
3801 ir_node *mul_r = get_Mul_right(a);
3802 tarval *tv = value_of(mul_r);
3803 if (tv != tarval_bad) {
3804 tv = tarval_neg(tv);
3805 if (tv != tarval_bad) {
3806 ir_node *cnst = new_Const(tv);
3807 dbg_info *dbg = get_irn_dbg_info(a);
3808 ir_node *block = get_nodes_block(a);
3809 n = new_rd_Mul(dbg, block, mul_l, cnst, mode);
3810 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_MINUS_MUL_C);
3817 } /* transform_node_Minus */
3820 * Transform a Cast_type(Const) into a new Const_type
3822 static ir_node *transform_node_Cast(ir_node *n)
3825 ir_node *pred = get_Cast_op(n);
3826 ir_type *tp = get_irn_type(n);
3828 if (is_Const(pred) && get_Const_type(pred) != tp) {
3829 ir_graph *irg = get_irn_irg(n);
3830 n = new_rd_Const_type(NULL, irg, get_Const_tarval(pred), tp);
3831 DBG_OPT_CSTEVAL(oldn, n);
3832 } else if (is_SymConst(pred) && get_SymConst_value_type(pred) != tp) {
3833 ir_graph *irg = get_irn_irg(n);
3834 n = new_rd_SymConst_type(NULL, irg, get_irn_mode(pred),
3835 get_SymConst_symbol(pred), get_SymConst_kind(pred), tp);
3836 DBG_OPT_CSTEVAL(oldn, n);
3840 } /* transform_node_Cast */
3843 * Transform a Proj(Load) with a non-null address.
3845 static ir_node *transform_node_Proj_Load(ir_node *proj)
3847 if (get_opt_ldst_only_null_ptr_exceptions()) {
3848 if (get_irn_mode(proj) == mode_X) {
3849 ir_node *load = get_Proj_pred(proj);
3851 /* get the Load address */
3852 const ir_node *addr = get_Load_ptr(load);
3853 const ir_node *confirm;
3855 if (value_not_null(addr, &confirm)) {
3856 if (confirm == NULL) {
3857 /* this node may float if it did not depend on a Confirm */
3858 set_irn_pinned(load, op_pin_state_floats);
3860 if (get_Proj_proj(proj) == pn_Load_X_except) {
3861 ir_graph *irg = get_irn_irg(proj);
3862 DBG_OPT_EXC_REM(proj);
3863 return get_irg_bad(irg);
3865 ir_node *blk = get_nodes_block(load);
3866 return new_r_Jmp(blk);
3872 } /* transform_node_Proj_Load */
3875 * Transform a Proj(Store) with a non-null address.
3877 static ir_node *transform_node_Proj_Store(ir_node *proj)
3879 if (get_opt_ldst_only_null_ptr_exceptions()) {
3880 if (get_irn_mode(proj) == mode_X) {
3881 ir_node *store = get_Proj_pred(proj);
3883 /* get the load/store address */
3884 const ir_node *addr = get_Store_ptr(store);
3885 const ir_node *confirm;
3887 if (value_not_null(addr, &confirm)) {
3888 if (confirm == NULL) {
3889 /* this node may float if it did not depend on a Confirm */
3890 set_irn_pinned(store, op_pin_state_floats);
3892 if (get_Proj_proj(proj) == pn_Store_X_except) {
3893 ir_graph *irg = get_irn_irg(proj);
3894 DBG_OPT_EXC_REM(proj);
3895 return get_irg_bad(irg);
3897 ir_node *blk = get_nodes_block(store);
3898 return new_r_Jmp(blk);
3904 } /* transform_node_Proj_Store */
3907 * Transform a Proj(Div) with a non-zero value.
3908 * Removes the exceptions and routes the memory to the NoMem node.
3910 static ir_node *transform_node_Proj_Div(ir_node *proj)
3912 ir_node *div = get_Proj_pred(proj);
3913 ir_node *b = get_Div_right(div);
3914 ir_node *res, *new_mem;
3915 const ir_node *confirm;
3918 if (value_not_zero(b, &confirm)) {
3919 /* div(x, y) && y != 0 */
3920 if (confirm == NULL) {
3921 /* we are sure we have a Const != 0 */
3922 new_mem = get_Div_mem(div);
3923 new_mem = skip_Pin(new_mem);
3924 set_Div_mem(div, new_mem);
3925 set_irn_pinned(div, op_pin_state_floats);
3928 proj_nr = get_Proj_proj(proj);
3930 case pn_Div_X_regular:
3931 return new_r_Jmp(get_nodes_block(div));
3933 case pn_Div_X_except:
3934 /* we found an exception handler, remove it */
3935 DBG_OPT_EXC_REM(proj);
3939 ir_graph *irg = get_irn_irg(proj);
3940 res = get_Div_mem(div);
3941 new_mem = get_irg_no_mem(irg);
3944 /* This node can only float up to the Confirm block */
3945 new_mem = new_r_Pin(get_nodes_block(confirm), new_mem);
3947 set_irn_pinned(div, op_pin_state_floats);
3948 /* this is a Div without exception, we can remove the memory edge */
3949 set_Div_mem(div, new_mem);
3955 } /* transform_node_Proj_Div */
3958 * Transform a Proj(Mod) with a non-zero value.
3959 * Removes the exceptions and routes the memory to the NoMem node.
3961 static ir_node *transform_node_Proj_Mod(ir_node *proj)
3963 ir_node *mod = get_Proj_pred(proj);
3964 ir_node *b = get_Mod_right(mod);
3965 ir_node *res, *new_mem;
3966 const ir_node *confirm;
3969 if (value_not_zero(b, &confirm)) {
3970 /* mod(x, y) && y != 0 */
3971 proj_nr = get_Proj_proj(proj);
3973 if (confirm == NULL) {
3974 /* we are sure we have a Const != 0 */
3975 new_mem = get_Mod_mem(mod);
3976 new_mem = skip_Pin(new_mem);
3977 set_Mod_mem(mod, new_mem);
3978 set_irn_pinned(mod, op_pin_state_floats);
3983 case pn_Mod_X_regular:
3984 return new_r_Jmp(get_irn_n(mod, -1));
3986 case pn_Mod_X_except:
3987 /* we found an exception handler, remove it */
3988 DBG_OPT_EXC_REM(proj);
3992 ir_graph *irg = get_irn_irg(proj);
3993 res = get_Mod_mem(mod);
3994 new_mem = get_irg_no_mem(irg);
3997 /* This node can only float up to the Confirm block */
3998 new_mem = new_r_Pin(get_nodes_block(confirm), new_mem);
4000 /* this is a Mod without exception, we can remove the memory edge */
4001 set_Mod_mem(mod, new_mem);
4005 if (get_Mod_left(mod) == b) {
4006 /* a % a = 0 if a != 0 */
4007 ir_mode *mode = get_irn_mode(proj);
4008 ir_node *res = new_Const(get_mode_null(mode));
4010 DBG_OPT_CSTEVAL(mod, res);
4016 } /* transform_node_Proj_Mod */
4019 * Transform a Proj(DivMod) with a non-zero value.
4020 * Removes the exceptions and routes the memory to the NoMem node.
4022 static ir_node *transform_node_Proj_DivMod(ir_node *proj)
4024 ir_node *divmod = get_Proj_pred(proj);
4025 ir_node *b = get_DivMod_right(divmod);
4026 ir_node *res, *new_mem;
4027 const ir_node *confirm;
4030 if (value_not_zero(b, &confirm)) {
4031 /* DivMod(x, y) && y != 0 */
4032 proj_nr = get_Proj_proj(proj);
4034 if (confirm == NULL) {
4035 /* we are sure we have a Const != 0 */
4036 new_mem = get_DivMod_mem(divmod);
4037 new_mem = skip_Pin(new_mem);
4038 set_DivMod_mem(divmod, new_mem);
4039 set_irn_pinned(divmod, op_pin_state_floats);
4044 case pn_DivMod_X_regular:
4045 return new_r_Jmp(get_nodes_block(divmod));
4047 case pn_DivMod_X_except:
4048 /* we found an exception handler, remove it */
4049 DBG_OPT_EXC_REM(proj);
4053 ir_graph *irg = get_irn_irg(proj);
4054 res = get_DivMod_mem(divmod);
4055 new_mem = get_irg_no_mem(irg);
4058 /* This node can only float up to the Confirm block */
4059 new_mem = new_r_Pin(get_nodes_block(confirm), new_mem);
4061 /* this is a DivMod without exception, we can remove the memory edge */
4062 set_DivMod_mem(divmod, new_mem);
4066 case pn_DivMod_res_mod:
4067 if (get_DivMod_left(divmod) == b) {
4068 /* a % a = 0 if a != 0 */
4069 ir_mode *mode = get_irn_mode(proj);
4070 ir_node *res = new_Const(get_mode_null(mode));
4072 DBG_OPT_CSTEVAL(divmod, res);
4078 } /* transform_node_Proj_DivMod */
4081 * Optimizes jump tables (CondIs or CondIu) by removing all impossible cases.
4083 static ir_node *transform_node_Proj_Cond(ir_node *proj)
4085 if (get_opt_unreachable_code()) {
4086 ir_node *n = get_Proj_pred(proj);
4087 ir_node *b = get_Cond_selector(n);
4089 if (mode_is_int(get_irn_mode(b))) {
4090 tarval *tb = value_of(b);
4092 if (tb != tarval_bad) {
4093 /* we have a constant switch */
4094 long num = get_Proj_proj(proj);
4096 if (num != get_Cond_default_proj(n)) { /* we cannot optimize default Proj's yet */
4097 if (get_tarval_long(tb) == num) {
4098 /* Do NOT create a jump here, or we will have 2 control flow ops
4099 * in a block. This case is optimized away in optimize_cf(). */
4102 ir_graph *irg = get_irn_irg(proj);
4103 /* this case will NEVER be taken, kill it */
4104 return get_irg_bad(irg);
4108 long num = get_Proj_proj(proj);
4109 vrp_attr *b_vrp = vrp_get_info(b);
4110 if (num != get_Cond_default_proj(n) && b_vrp) {
4111 /* Try handling with vrp data. We only remove dead parts. */
4112 tarval *tp = new_tarval_from_long(num, get_irn_mode(b));
4114 if (b_vrp->range_type == VRP_RANGE) {
4115 pn_Cmp cmp_result = tarval_cmp(b_vrp->range_bottom, tp);
4116 pn_Cmp cmp_result2 = tarval_cmp(b_vrp->range_top, tp);
4118 if ((cmp_result & pn_Cmp_Gt) == cmp_result && (cmp_result2
4119 & pn_Cmp_Lt) == cmp_result2) {
4120 ir_graph *irg = get_irn_irg(proj);
4121 return get_irg_bad(irg);
4123 } else if (b_vrp->range_type == VRP_ANTIRANGE) {
4124 pn_Cmp cmp_result = tarval_cmp(b_vrp->range_bottom, tp);
4125 pn_Cmp cmp_result2 = tarval_cmp(b_vrp->range_top, tp);
4127 if ((cmp_result & pn_Cmp_Le) == cmp_result && (cmp_result2
4128 & pn_Cmp_Ge) == cmp_result2) {
4129 ir_graph *irg = get_irn_irg(proj);
4130 return get_irg_bad(irg);
4135 tarval_and( b_vrp->bits_set, tp),
4138 ir_graph *irg = get_irn_irg(proj);
4139 return get_irg_bad(irg);
4145 tarval_not(b_vrp->bits_not_set)),
4146 tarval_not(b_vrp->bits_not_set))
4148 ir_graph *irg = get_irn_irg(proj);
4149 return get_irg_bad(irg);
4158 } /* transform_node_Proj_Cond */
4161 * Create a 0 constant of given mode.
4163 static ir_node *create_zero_const(ir_mode *mode)
4165 tarval *tv = get_mode_null(mode);
4166 ir_node *cnst = new_Const(tv);
4172 * Normalizes and optimizes Cmp nodes.
4174 static ir_node *transform_node_Proj_Cmp(ir_node *proj)
4176 ir_node *n = get_Proj_pred(proj);
4177 ir_node *left = get_Cmp_left(n);
4178 ir_node *right = get_Cmp_right(n);
4181 ir_mode *mode = NULL;
4182 long proj_nr = get_Proj_proj(proj);
4184 /* we can evaluate some cases directly */
4187 return new_Const(get_tarval_b_false());
4189 return new_Const(get_tarval_b_true());
4191 if (!mode_is_float(get_irn_mode(left)))
4192 return new_Const(get_tarval_b_true());
4198 /* remove Casts of both sides */
4199 left = skip_Cast(left);
4200 right = skip_Cast(right);
4202 /* Remove unnecessary conversions */
4203 /* TODO handle constants */
4204 if (is_Conv(left) && is_Conv(right)) {
4205 ir_mode *mode = get_irn_mode(left);
4206 ir_node *op_left = get_Conv_op(left);
4207 ir_node *op_right = get_Conv_op(right);
4208 ir_mode *mode_left = get_irn_mode(op_left);
4209 ir_mode *mode_right = get_irn_mode(op_right);
4211 if (smaller_mode(mode_left, mode) && smaller_mode(mode_right, mode)
4212 && mode_left != mode_b && mode_right != mode_b) {
4213 ir_node *block = get_nodes_block(n);
4215 if (mode_left == mode_right) {
4219 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV_CONV);
4220 } else if (smaller_mode(mode_left, mode_right)) {
4221 left = new_r_Conv(block, op_left, mode_right);
4224 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV);
4225 } else if (smaller_mode(mode_right, mode_left)) {
4227 right = new_r_Conv(block, op_right, mode_left);
4229 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV);
4234 /* remove operation on both sides if possible */
4235 if (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) {
4237 * The following operations are NOT safe for floating point operations, for instance
4238 * 1.0 + inf == 2.0 + inf, =/=> x == y
4240 if (mode_is_int(get_irn_mode(left))) {
4241 unsigned lop = get_irn_opcode(left);
4243 if (lop == get_irn_opcode(right)) {
4244 ir_node *ll, *lr, *rl, *rr;
4246 /* same operation on both sides, try to remove */
4250 /* ~a CMP ~b => a CMP b, -a CMP -b ==> a CMP b */
4251 left = get_unop_op(left);
4252 right = get_unop_op(right);
4254 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4257 ll = get_Add_left(left);
4258 lr = get_Add_right(left);
4259 rl = get_Add_left(right);
4260 rr = get_Add_right(right);
4263 /* X + a CMP X + b ==> a CMP b */
4267 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4268 } else if (ll == rr) {
4269 /* X + a CMP b + X ==> a CMP b */
4273 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4274 } else if (lr == rl) {
4275 /* a + X CMP X + b ==> a CMP b */
4279 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4280 } else if (lr == rr) {
4281 /* a + X CMP b + X ==> a CMP b */
4285 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4289 ll = get_Sub_left(left);
4290 lr = get_Sub_right(left);
4291 rl = get_Sub_left(right);
4292 rr = get_Sub_right(right);
4295 /* X - a CMP X - b ==> a CMP b */
4299 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4300 } else if (lr == rr) {
4301 /* a - X CMP b - X ==> a CMP b */
4305 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4309 if (get_Rotl_right(left) == get_Rotl_right(right)) {
4310 /* a ROTL X CMP b ROTL X ==> a CMP b */
4311 left = get_Rotl_left(left);
4312 right = get_Rotl_left(right);
4314 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4322 /* X+A == A, A+X == A, A-X == A -> X == 0 */
4323 if (is_Add(left) || is_Sub(left)) {
4324 ir_node *ll = get_binop_left(left);
4325 ir_node *lr = get_binop_right(left);
4327 if (lr == right && is_Add(left)) {
4334 right = create_zero_const(get_irn_mode(left));
4336 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4339 if (is_Add(right) || is_Sub(right)) {
4340 ir_node *rl = get_binop_left(right);
4341 ir_node *rr = get_binop_right(right);
4343 if (rr == left && is_Add(right)) {
4350 right = create_zero_const(get_irn_mode(left));
4352 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4355 if (is_And(left) && is_Const(right)) {
4356 ir_node *ll = get_binop_left(left);
4357 ir_node *lr = get_binop_right(left);
4358 if (is_Shr(ll) && is_Const(lr)) {
4359 /* Cmp((x >>u c1) & c2, c3) = Cmp(x & (c2 << c1), c3 << c1) */
4360 ir_node *block = get_nodes_block(n);
4361 ir_mode *mode = get_irn_mode(left);
4363 ir_node *llr = get_Shr_right(ll);
4364 if (is_Const(llr)) {
4365 dbg_info *dbg = get_irn_dbg_info(left);
4367 tarval *c1 = get_Const_tarval(llr);
4368 tarval *c2 = get_Const_tarval(lr);
4369 tarval *c3 = get_Const_tarval(right);
4370 tarval *mask = tarval_shl(c2, c1);
4371 tarval *value = tarval_shl(c3, c1);
4373 left = new_rd_And(dbg, block, get_Shr_left(ll), new_Const(mask), mode);
4374 right = new_Const(value);
4379 /* Cmp(Eor(x, y), 0) <=> Cmp(x, y) at least for the ==0,!=0
4381 if (is_Const(right) && is_Const_null(right) && is_Eor(left)) {
4382 right = get_Eor_right(left);
4383 left = get_Eor_left(left);
4386 } /* mode_is_int(...) */
4387 } /* proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg */
4389 /* replace mode_b compares with ands/ors */
4390 if (get_irn_mode(left) == mode_b) {
4391 ir_node *block = get_nodes_block(n);
4395 case pn_Cmp_Le: bres = new_r_Or( block, new_r_Not(block, left, mode_b), right, mode_b); break;
4396 case pn_Cmp_Lt: bres = new_r_And(block, new_r_Not(block, left, mode_b), right, mode_b); break;
4397 case pn_Cmp_Ge: bres = new_r_Or( block, left, new_r_Not(block, right, mode_b), mode_b); break;
4398 case pn_Cmp_Gt: bres = new_r_And(block, left, new_r_Not(block, right, mode_b), mode_b); break;
4399 case pn_Cmp_Lg: bres = new_r_Eor(block, left, right, mode_b); break;
4400 case pn_Cmp_Eq: bres = new_r_Not(block, new_r_Eor(block, left, right, mode_b), mode_b); break;
4401 default: bres = NULL;
4404 DBG_OPT_ALGSIM0(n, bres, FS_OPT_CMP_TO_BOOL);
4410 * First step: normalize the compare op
4411 * by placing the constant on the right side
4412 * or moving the lower address node to the left.
4414 if (!operands_are_normalized(left, right)) {
4420 proj_nr = get_inversed_pnc(proj_nr);
4425 * Second step: Try to reduce the magnitude
4426 * of a constant. This may help to generate better code
4427 * later and may help to normalize more compares.
4428 * Of course this is only possible for integer values.
4430 tv = value_of(right);
4431 if (tv != tarval_bad) {
4432 mode = get_irn_mode(right);
4434 /* TODO extend to arbitrary constants */
4435 if (is_Conv(left) && tarval_is_null(tv)) {
4436 ir_node *op = get_Conv_op(left);
4437 ir_mode *op_mode = get_irn_mode(op);
4440 * UpConv(x) REL 0 ==> x REL 0
4441 * Don't do this for float values as it's unclear whether it is a
4442 * win. (on the other side it makes detection/creation of fabs hard)
4444 if (get_mode_size_bits(mode) > get_mode_size_bits(op_mode) &&
4445 ((proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) ||
4446 mode_is_signed(mode) || !mode_is_signed(op_mode)) &&
4447 !mode_is_float(mode)) {
4448 tv = get_mode_null(op_mode);
4452 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV);
4456 if (tv != tarval_bad) {
4457 /* the following optimization is possible on modes without Overflow
4458 * on Unary Minus or on == and !=:
4459 * -a CMP c ==> a swap(CMP) -c
4461 * Beware: for two-complement Overflow may occur, so only == and != can
4462 * be optimized, see this:
4463 * -MININT < 0 =/=> MININT > 0 !!!
4465 if (is_Minus(left) &&
4466 (!mode_overflow_on_unary_Minus(mode) ||
4467 (mode_is_int(mode) && (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg)))) {
4468 tv = tarval_neg(tv);
4470 if (tv != tarval_bad) {
4471 left = get_Minus_op(left);
4472 proj_nr = get_inversed_pnc(proj_nr);
4474 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4476 } else if (is_Not(left) && (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg)) {
4477 /* Not(a) ==/!= c ==> a ==/!= Not(c) */
4478 tv = tarval_not(tv);
4480 if (tv != tarval_bad) {
4481 left = get_Not_op(left);
4483 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4487 /* for integer modes, we have more */
4488 if (mode_is_int(mode)) {
4489 /* Ne includes Unordered which is not possible on integers.
4490 * However, frontends often use this wrong, so fix it here */
4491 if (proj_nr & pn_Cmp_Uo) {
4492 proj_nr &= ~pn_Cmp_Uo;
4493 set_Proj_proj(proj, proj_nr);
4496 /* c > 0 : a < c ==> a <= (c-1) a >= c ==> a > (c-1) */
4497 if ((proj_nr == pn_Cmp_Lt || proj_nr == pn_Cmp_Ge) &&
4498 tarval_cmp(tv, get_mode_null(mode)) == pn_Cmp_Gt) {
4499 tv = tarval_sub(tv, get_mode_one(mode), NULL);
4501 if (tv != tarval_bad) {
4502 proj_nr ^= pn_Cmp_Eq;
4504 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CNST_MAGN);
4507 /* c < 0 : a > c ==> a >= (c+1) a <= c ==> a < (c+1) */
4508 else if ((proj_nr == pn_Cmp_Gt || proj_nr == pn_Cmp_Le) &&
4509 tarval_cmp(tv, get_mode_null(mode)) == pn_Cmp_Lt) {
4510 tv = tarval_add(tv, get_mode_one(mode));
4512 if (tv != tarval_bad) {
4513 proj_nr ^= pn_Cmp_Eq;
4515 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CNST_MAGN);
4519 /* the following reassociations work only for == and != */
4520 if (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) {
4522 #if 0 /* Might be not that good in general */
4523 /* a-b == 0 ==> a == b, a-b != 0 ==> a != b */
4524 if (tarval_is_null(tv) && is_Sub(left)) {
4525 right = get_Sub_right(left);
4526 left = get_Sub_left(left);
4528 tv = value_of(right);
4530 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4534 if (tv != tarval_bad) {
4535 /* a-c1 == c2 ==> a == c2+c1, a-c1 != c2 ==> a != c2+c1 */
4537 ir_node *c1 = get_Sub_right(left);
4538 tarval *tv2 = value_of(c1);
4540 if (tv2 != tarval_bad) {
4541 tv2 = tarval_add(tv, value_of(c1));
4543 if (tv2 != tarval_bad) {
4544 left = get_Sub_left(left);
4547 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4551 /* a+c1 == c2 ==> a == c2-c1, a+c1 != c2 ==> a != c2-c1 */
4552 else if (is_Add(left)) {
4553 ir_node *a_l = get_Add_left(left);
4554 ir_node *a_r = get_Add_right(left);
4558 if (is_Const(a_l)) {
4560 tv2 = value_of(a_l);
4563 tv2 = value_of(a_r);
4566 if (tv2 != tarval_bad) {
4567 tv2 = tarval_sub(tv, tv2, NULL);
4569 if (tv2 != tarval_bad) {
4573 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4577 /* -a == c ==> a == -c, -a != c ==> a != -c */
4578 else if (is_Minus(left)) {
4579 tarval *tv2 = tarval_sub(get_mode_null(mode), tv, NULL);
4581 if (tv2 != tarval_bad) {
4582 left = get_Minus_op(left);
4585 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4592 if (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) {
4593 switch (get_irn_opcode(left)) {
4597 c1 = get_And_right(left);
4600 * And(x, C1) == C2 ==> FALSE if C2 & C1 != C2
4601 * And(x, C1) != C2 ==> TRUE if C2 & C1 != C2
4603 tarval *mask = tarval_and(get_Const_tarval(c1), tv);
4605 /* TODO: move to constant evaluation */
4606 tv = proj_nr == pn_Cmp_Eq ? get_tarval_b_false() : get_tarval_b_true();
4608 DBG_OPT_CSTEVAL(proj, c1);
4612 if (tarval_is_single_bit(tv)) {
4614 * optimization for AND:
4616 * And(x, C) == C ==> And(x, C) != 0
4617 * And(x, C) != C ==> And(X, C) == 0
4619 * if C is a single Bit constant.
4622 /* check for Constant's match. We have check hare the tarvals,
4623 because our const might be changed */
4624 if (get_Const_tarval(c1) == tv) {
4625 /* fine: do the transformation */
4626 tv = get_mode_null(get_tarval_mode(tv));
4627 proj_nr ^= pn_Cmp_Leg;
4629 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CNST_MAGN);
4635 c1 = get_Or_right(left);
4636 if (is_Const(c1) && tarval_is_null(tv)) {
4638 * Or(x, C) == 0 && C != 0 ==> FALSE
4639 * Or(x, C) != 0 && C != 0 ==> TRUE
4641 if (! tarval_is_null(get_Const_tarval(c1))) {
4642 /* TODO: move to constant evaluation */
4643 tv = proj_nr == pn_Cmp_Eq ? get_tarval_b_false() : get_tarval_b_true();
4645 DBG_OPT_CSTEVAL(proj, c1);
4652 * optimize x << c1 == c into x & (-1 >>u c1) == c >> c1 if c & (-1 << c1) == c
4654 * optimize x << c1 != c into x & (-1 >>u c1) != c >> c1 if c & (-1 << c1) == c
4657 c1 = get_Shl_right(left);
4659 tarval *tv1 = get_Const_tarval(c1);
4660 ir_mode *mode = get_irn_mode(left);
4661 tarval *minus1 = get_mode_all_one(mode);
4662 tarval *amask = tarval_shr(minus1, tv1);
4663 tarval *cmask = tarval_shl(minus1, tv1);
4666 if (tarval_and(tv, cmask) != tv) {
4667 /* condition not met */
4668 tv = proj_nr == pn_Cmp_Eq ? get_tarval_b_false() : get_tarval_b_true();
4670 DBG_OPT_CSTEVAL(proj, c1);
4673 sl = get_Shl_left(left);
4674 blk = get_nodes_block(n);
4675 left = new_rd_And(get_irn_dbg_info(left), blk, sl, new_Const(amask), mode);
4676 tv = tarval_shr(tv, tv1);
4678 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_SHF_TO_AND);
4683 * optimize x >>u c1 == c into x & (-1 << c1) == c << c1 if c & (-1 >>u c1) == c
4685 * optimize x >>u c1 != c into x & (-1 << c1) != c << c1 if c & (-1 >>u c1) == c
4688 c1 = get_Shr_right(left);
4690 tarval *tv1 = get_Const_tarval(c1);
4691 ir_mode *mode = get_irn_mode(left);
4692 tarval *minus1 = get_mode_all_one(mode);
4693 tarval *amask = tarval_shl(minus1, tv1);
4694 tarval *cmask = tarval_shr(minus1, tv1);
4697 if (tarval_and(tv, cmask) != tv) {
4698 /* condition not met */
4699 tv = proj_nr == pn_Cmp_Eq ? get_tarval_b_false() : get_tarval_b_true();
4701 DBG_OPT_CSTEVAL(proj, c1);
4704 sl = get_Shr_left(left);
4705 blk = get_nodes_block(n);
4706 left = new_rd_And(get_irn_dbg_info(left), blk, sl, new_Const(amask), mode);
4707 tv = tarval_shl(tv, tv1);
4709 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_SHF_TO_AND);
4714 * optimize x >>s c1 == c into x & (-1 << c1) == c << c1 if (c >>s (BITS - c1)) \in {0,-1}
4716 * optimize x >>s c1 != c into x & (-1 << c1) != c << c1 if (c >>s (BITS - c1)) \in {0,-1}
4719 c1 = get_Shrs_right(left);
4721 tarval *tv1 = get_Const_tarval(c1);
4722 ir_mode *mode = get_irn_mode(left);
4723 tarval *minus1 = get_mode_all_one(mode);
4724 tarval *amask = tarval_shl(minus1, tv1);
4725 tarval *cond = new_tarval_from_long(get_mode_size_bits(mode), get_tarval_mode(tv1));
4728 cond = tarval_sub(cond, tv1, NULL);
4729 cond = tarval_shrs(tv, cond);
4731 if (!tarval_is_all_one(cond) && !tarval_is_null(cond)) {
4732 /* condition not met */
4733 tv = proj_nr == pn_Cmp_Eq ? get_tarval_b_false() : get_tarval_b_true();
4735 DBG_OPT_CSTEVAL(proj, c1);
4738 sl = get_Shrs_left(left);
4739 blk = get_nodes_block(n);
4740 left = new_rd_And(get_irn_dbg_info(left), blk, sl, new_Const(amask), mode);
4741 tv = tarval_shl(tv, tv1);
4743 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_SHF_TO_AND);
4748 } /* tarval != bad */
4751 if (changed & 2) /* need a new Const */
4752 right = new_Const(tv);
4754 if ((proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) && is_Const(right) && is_Const_null(right) && is_Proj(left)) {
4755 ir_node *op = get_Proj_pred(left);
4757 if ((is_Mod(op) && get_Proj_proj(left) == pn_Mod_res) ||
4758 (is_DivMod(op) && get_Proj_proj(left) == pn_DivMod_res_mod)) {
4759 ir_node *c = get_binop_right(op);
4762 tarval *tv = get_Const_tarval(c);
4764 if (tarval_is_single_bit(tv)) {
4765 /* special case: (x % 2^n) CMP 0 ==> x & (2^n-1) CMP 0 */
4766 ir_node *v = get_binop_left(op);
4767 ir_node *blk = get_irn_n(op, -1);
4768 ir_mode *mode = get_irn_mode(v);
4770 tv = tarval_sub(tv, get_mode_one(mode), NULL);
4771 left = new_rd_And(get_irn_dbg_info(op), blk, v, new_Const(tv), mode);
4773 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_MOD_TO_AND);
4780 ir_node *block = get_nodes_block(n);
4782 /* create a new compare */
4783 n = new_rd_Cmp(get_irn_dbg_info(n), block, left, right);
4784 proj = new_rd_Proj(get_irn_dbg_info(proj), n, get_irn_mode(proj), proj_nr);
4788 } /* transform_node_Proj_Cmp */
4791 * Optimize CopyB(mem, x, x) into a Nop.
4793 static ir_node *transform_node_Proj_CopyB(ir_node *proj)
4795 ir_node *copyb = get_Proj_pred(proj);
4796 ir_node *a = get_CopyB_dst(copyb);
4797 ir_node *b = get_CopyB_src(copyb);
4800 switch (get_Proj_proj(proj)) {
4801 case pn_CopyB_X_regular:
4802 /* Turn CopyB into a tuple (mem, jmp, bad, bad) */
4803 DBG_OPT_EXC_REM(proj);
4804 proj = new_r_Jmp(get_nodes_block(copyb));
4806 case pn_CopyB_X_except:
4807 DBG_OPT_EXC_REM(proj);
4808 proj = get_irg_bad(get_irn_irg(proj));
4815 } /* transform_node_Proj_CopyB */
4818 * Optimize Bounds(idx, idx, upper) into idx.
4820 static ir_node *transform_node_Proj_Bound(ir_node *proj)
4822 ir_node *oldn = proj;
4823 ir_node *bound = get_Proj_pred(proj);
4824 ir_node *idx = get_Bound_index(bound);
4825 ir_node *pred = skip_Proj(idx);
4828 if (idx == get_Bound_lower(bound))
4830 else if (is_Bound(pred)) {
4832 * idx was Bounds checked previously, it is still valid if
4833 * lower <= pred_lower && pred_upper <= upper.
4835 ir_node *lower = get_Bound_lower(bound);
4836 ir_node *upper = get_Bound_upper(bound);
4837 if (get_Bound_lower(pred) == lower &&
4838 get_Bound_upper(pred) == upper) {
4840 * One could expect that we simply return the previous
4841 * Bound here. However, this would be wrong, as we could
4842 * add an exception Proj to a new location then.
4843 * So, we must turn in into a tuple.
4849 /* Turn Bound into a tuple (mem, jmp, bad, idx) */
4850 switch (get_Proj_proj(proj)) {
4852 DBG_OPT_EXC_REM(proj);
4853 proj = get_Bound_mem(bound);
4855 case pn_Bound_X_except:
4856 DBG_OPT_EXC_REM(proj);
4857 proj = get_irg_bad(get_irn_irg(proj));
4861 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NOP);
4863 case pn_Bound_X_regular:
4864 DBG_OPT_EXC_REM(proj);
4865 proj = new_r_Jmp(get_nodes_block(bound));
4872 } /* transform_node_Proj_Bound */
4875 * Does all optimizations on nodes that must be done on it's Proj's
4876 * because of creating new nodes.
4878 static ir_node *transform_node_Proj(ir_node *proj)
4880 ir_node *n = get_Proj_pred(proj);
4882 if (n->op->ops.transform_node_Proj)
4883 return n->op->ops.transform_node_Proj(proj);
4885 } /* transform_node_Proj */
4888 * Move Confirms down through Phi nodes.
4890 static ir_node *transform_node_Phi(ir_node *phi)
4893 ir_mode *mode = get_irn_mode(phi);
4895 if (mode_is_reference(mode)) {
4896 n = get_irn_arity(phi);
4898 /* Beware of Phi0 */
4900 ir_node *pred = get_irn_n(phi, 0);
4901 ir_node *bound, *new_Phi, *block, **in;
4904 if (! is_Confirm(pred))
4907 bound = get_Confirm_bound(pred);
4908 pnc = get_Confirm_cmp(pred);
4910 NEW_ARR_A(ir_node *, in, n);
4911 in[0] = get_Confirm_value(pred);
4913 for (i = 1; i < n; ++i) {
4914 pred = get_irn_n(phi, i);
4916 if (! is_Confirm(pred) ||
4917 get_Confirm_bound(pred) != bound ||
4918 get_Confirm_cmp(pred) != pnc)
4920 in[i] = get_Confirm_value(pred);
4922 /* move the Confirm nodes "behind" the Phi */
4923 block = get_irn_n(phi, -1);
4924 new_Phi = new_r_Phi(block, n, in, get_irn_mode(phi));
4925 return new_r_Confirm(block, new_Phi, bound, pnc);
4929 } /* transform_node_Phi */
4932 * Returns the operands of a commutative bin-op, if one operand is
4933 * a const, it is returned as the second one.
4935 static void get_comm_Binop_Ops(ir_node *binop, ir_node **a, ir_node **c)
4937 ir_node *op_a = get_binop_left(binop);
4938 ir_node *op_b = get_binop_right(binop);
4940 assert(is_op_commutative(get_irn_op(binop)));
4942 if (is_Const(op_a)) {
4949 } /* get_comm_Binop_Ops */
4952 * Optimize a Or(And(Or(And(v,c4),c3),c2),c1) pattern if possible.
4953 * Such pattern may arise in bitfield stores.
4955 * value c4 value c4 & c2
4956 * AND c3 AND c1 | c3
4963 * AND c1 ===> OR if (c1 | c2) == 0x111..11
4966 static ir_node *transform_node_Or_bf_store(ir_node *or)
4970 ir_node *and_l, *c3;
4971 ir_node *value, *c4;
4972 ir_node *new_and, *new_const, *block;
4973 ir_mode *mode = get_irn_mode(or);
4975 tarval *tv1, *tv2, *tv3, *tv4, *tv;
4978 get_comm_Binop_Ops(or, &and, &c1);
4979 if (!is_Const(c1) || !is_And(and))
4982 get_comm_Binop_Ops(and, &or_l, &c2);
4986 tv1 = get_Const_tarval(c1);
4987 tv2 = get_Const_tarval(c2);
4989 tv = tarval_or(tv1, tv2);
4990 if (tarval_is_all_one(tv)) {
4991 /* the AND does NOT clear a bit with isn't set by the OR */
4992 set_Or_left(or, or_l);
4993 set_Or_right(or, c1);
4995 /* check for more */
5002 get_comm_Binop_Ops(or_l, &and_l, &c3);
5003 if (!is_Const(c3) || !is_And(and_l))
5006 get_comm_Binop_Ops(and_l, &value, &c4);
5010 /* ok, found the pattern, check for conditions */
5011 assert(mode == get_irn_mode(and));
5012 assert(mode == get_irn_mode(or_l));
5013 assert(mode == get_irn_mode(and_l));
5015 tv3 = get_Const_tarval(c3);
5016 tv4 = get_Const_tarval(c4);
5018 tv = tarval_or(tv4, tv2);
5019 if (!tarval_is_all_one(tv)) {
5020 /* have at least one 0 at the same bit position */
5024 if (tv3 != tarval_andnot(tv3, tv4)) {
5025 /* bit in the or_mask is outside the and_mask */
5029 if (tv1 != tarval_andnot(tv1, tv2)) {
5030 /* bit in the or_mask is outside the and_mask */
5034 /* ok, all conditions met */
5035 block = get_irn_n(or, -1);
5037 new_and = new_r_And(block, value, new_Const(tarval_and(tv4, tv2)), mode);
5039 new_const = new_Const(tarval_or(tv3, tv1));
5041 set_Or_left(or, new_and);
5042 set_Or_right(or, new_const);
5044 /* check for more */
5046 } /* transform_node_Or_bf_store */
5049 * Optimize an Or(shl(x, c), shr(x, bits - c)) into a Rotl
5051 static ir_node *transform_node_Or_Rotl(ir_node *or)
5053 ir_mode *mode = get_irn_mode(or);
5054 ir_node *shl, *shr, *block;
5055 ir_node *irn, *x, *c1, *c2, *n;
5058 /* some backends can't handle rotl */
5059 if (!be_get_backend_param()->support_rotl)
5062 if (! mode_is_int(mode))
5065 shl = get_binop_left(or);
5066 shr = get_binop_right(or);
5075 } else if (!is_Shl(shl)) {
5077 } else if (!is_Shr(shr)) {
5080 x = get_Shl_left(shl);
5081 if (x != get_Shr_left(shr))
5084 c1 = get_Shl_right(shl);
5085 c2 = get_Shr_right(shr);
5086 if (is_Const(c1) && is_Const(c2)) {
5087 tv1 = get_Const_tarval(c1);
5088 if (! tarval_is_long(tv1))
5091 tv2 = get_Const_tarval(c2);
5092 if (! tarval_is_long(tv2))
5095 if (get_tarval_long(tv1) + get_tarval_long(tv2)
5096 != (int) get_mode_size_bits(mode))
5099 /* yet, condition met */
5100 block = get_nodes_block(or);
5102 n = new_r_Rotl(block, x, c1, mode);
5104 DBG_OPT_ALGSIM1(or, shl, shr, n, FS_OPT_OR_SHFT_TO_ROTL);
5108 /* Note: the obvious rot formulation (a << x) | (a >> (32-x)) gets
5109 * transformed to (a << x) | (a >> -x) by transform_node_shift_modulo() */
5110 if (!is_negated_value(c1, c2)) {
5114 /* yet, condition met */
5115 block = get_nodes_block(or);
5116 n = new_r_Rotl(block, x, c1, mode);
5117 DBG_OPT_ALGSIM0(or, n, FS_OPT_OR_SHFT_TO_ROTL);
5119 } /* transform_node_Or_Rotl */
5124 static ir_node *transform_node_Or(ir_node *n)
5126 ir_node *c, *oldn = n;
5127 ir_node *a = get_Or_left(n);
5128 ir_node *b = get_Or_right(n);
5131 if (is_Not(a) && is_Not(b)) {
5132 /* ~a | ~b = ~(a&b) */
5133 ir_node *block = get_nodes_block(n);
5135 mode = get_irn_mode(n);
5138 n = new_rd_And(get_irn_dbg_info(n), block, a, b, mode);
5139 n = new_rd_Not(get_irn_dbg_info(n), block, n, mode);
5140 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_DEMORGAN);
5144 /* we can evaluate 2 Projs of the same Cmp */
5145 if (get_irn_mode(n) == mode_b && is_Proj(a) && is_Proj(b)) {
5146 ir_node *pred_a = get_Proj_pred(a);
5147 ir_node *pred_b = get_Proj_pred(b);
5148 if (pred_a == pred_b) {
5149 dbg_info *dbgi = get_irn_dbg_info(n);
5150 pn_Cmp pn_a = get_Proj_proj(a);
5151 pn_Cmp pn_b = get_Proj_proj(b);
5152 /* yes, we can simply calculate with pncs */
5153 pn_Cmp new_pnc = pn_a | pn_b;
5155 return new_rd_Proj(dbgi, pred_a, mode_b, new_pnc);
5159 mode = get_irn_mode(n);
5160 HANDLE_BINOP_PHI((eval_func) tarval_or, a, b, c, mode);
5162 n = transform_node_Or_bf_store(n);
5163 n = transform_node_Or_Rotl(n);
5167 n = transform_bitwise_distributive(n, transform_node_Or);
5170 } /* transform_node_Or */
5174 static ir_node *transform_node(ir_node *n);
5177 * Optimize (a >> c1) >> c2), works for Shr, Shrs, Shl, Rotl.
5179 * Should be moved to reassociation?
5181 static ir_node *transform_node_shift(ir_node *n)
5183 ir_node *left, *right;
5185 tarval *tv1, *tv2, *res;
5186 ir_node *in[2], *irn, *block;
5188 left = get_binop_left(n);
5190 /* different operations */
5191 if (get_irn_op(left) != get_irn_op(n))
5194 right = get_binop_right(n);
5195 tv1 = value_of(right);
5196 if (tv1 == tarval_bad)
5199 tv2 = value_of(get_binop_right(left));
5200 if (tv2 == tarval_bad)
5203 res = tarval_add(tv1, tv2);
5204 mode = get_irn_mode(n);
5206 /* beware: a simple replacement works only, if res < modulo shift */
5208 int modulo_shf = get_mode_modulo_shift(mode);
5209 if (modulo_shf > 0) {
5210 tarval *modulo = new_tarval_from_long(modulo_shf,
5211 get_tarval_mode(res));
5213 assert(modulo_shf >= (int) get_mode_size_bits(mode));
5215 /* shifting too much */
5216 if (!(tarval_cmp(res, modulo) & pn_Cmp_Lt)) {
5218 ir_node *block = get_nodes_block(n);
5219 dbg_info *dbgi = get_irn_dbg_info(n);
5220 ir_mode *smode = get_irn_mode(right);
5221 ir_node *cnst = new_Const_long(smode, get_mode_size_bits(mode) - 1);
5222 return new_rd_Shrs(dbgi, block, get_binop_left(left), cnst, mode);
5225 return new_Const(get_mode_null(mode));
5229 res = tarval_mod(res, new_tarval_from_long(get_mode_size_bits(mode), get_tarval_mode(res)));
5232 /* ok, we can replace it */
5233 block = get_nodes_block(n);
5235 in[0] = get_binop_left(left);
5236 in[1] = new_Const(res);
5238 irn = new_ir_node(NULL, get_Block_irg(block), block, get_irn_op(n), mode, 2, in);
5240 DBG_OPT_ALGSIM0(n, irn, FS_OPT_REASSOC_SHIFT);
5242 return transform_node(irn);
5243 } /* transform_node_shift */
5246 * normalisation: (x & c1) >> c2 to (x >> c2) & (c1 >> c2)
5248 * - and, or, xor instead of &
5249 * - Shl, Shr, Shrs, rotl instead of >>
5250 * (with a special case for Or/Xor + Shrs)
5252 static ir_node *transform_node_bitop_shift(ir_node *n)
5255 ir_node *right = get_binop_right(n);
5256 ir_mode *mode = get_irn_mode(n);
5257 ir_node *bitop_left;
5258 ir_node *bitop_right;
5269 assert(is_Shrs(n) || is_Shr(n) || is_Shl(n) || is_Rotl(n));
5271 if (!is_Const(right))
5274 left = get_binop_left(n);
5275 op_left = get_irn_op(left);
5276 if (op_left != op_And && op_left != op_Or && op_left != op_Eor)
5279 /* doing it with Shrs is not legal if the Or/Eor affects the topmost bit */
5280 if (is_Shrs(n) && (op_left == op_Or || op_left == op_Eor)) {
5281 /* TODO: test if sign bit is affectes */
5285 bitop_right = get_binop_right(left);
5286 if (!is_Const(bitop_right))
5289 bitop_left = get_binop_left(left);
5291 block = get_nodes_block(n);
5292 dbgi = get_irn_dbg_info(n);
5293 tv1 = get_Const_tarval(bitop_right);
5294 tv2 = get_Const_tarval(right);
5296 assert(get_tarval_mode(tv1) == mode);
5299 new_shift = new_rd_Shl(dbgi, block, bitop_left, right, mode);
5300 tv_shift = tarval_shl(tv1, tv2);
5301 } else if (is_Shr(n)) {
5302 new_shift = new_rd_Shr(dbgi, block, bitop_left, right, mode);
5303 tv_shift = tarval_shr(tv1, tv2);
5304 } else if (is_Shrs(n)) {
5305 new_shift = new_rd_Shrs(dbgi, block, bitop_left, right, mode);
5306 tv_shift = tarval_shrs(tv1, tv2);
5309 new_shift = new_rd_Rotl(dbgi, block, bitop_left, right, mode);
5310 tv_shift = tarval_rotl(tv1, tv2);
5313 assert(get_tarval_mode(tv_shift) == mode);
5314 new_const = new_Const(tv_shift);
5316 if (op_left == op_And) {
5317 new_bitop = new_rd_And(dbgi, block, new_shift, new_const, mode);
5318 } else if (op_left == op_Or) {
5319 new_bitop = new_rd_Or(dbgi, block, new_shift, new_const, mode);
5321 assert(op_left == op_Eor);
5322 new_bitop = new_rd_Eor(dbgi, block, new_shift, new_const, mode);
5330 * (x << c1) >> c2 <=> x OP (c2-c1) & ((-1 << c1) >> c2)
5332 * (x >> c1) << c2 <=> x OP (c2-c1) & ((-1 >> c1) << c2)
5333 * (also with x >>s c1 when c1>=c2)
5335 static ir_node *transform_node_shl_shr(ir_node *n)
5338 ir_node *right = get_binop_right(n);
5353 assert(is_Shl(n) || is_Shr(n) || is_Shrs(n));
5355 if (!is_Const(right))
5358 left = get_binop_left(n);
5359 mode = get_irn_mode(n);
5360 if (is_Shl(n) && (is_Shr(left) || is_Shrs(left))) {
5361 ir_node *shr_right = get_binop_right(left);
5363 if (!is_Const(shr_right))
5366 x = get_binop_left(left);
5367 tv_shr = get_Const_tarval(shr_right);
5368 tv_shl = get_Const_tarval(right);
5370 if (is_Shrs(left)) {
5371 /* shrs variant only allowed if c1 >= c2 */
5372 if (! (tarval_cmp(tv_shl, tv_shr) & pn_Cmp_Ge))
5375 tv_mask = tarval_shrs(get_mode_all_one(mode), tv_shr);
5378 tv_mask = tarval_shr(get_mode_all_one(mode), tv_shr);
5380 tv_mask = tarval_shl(tv_mask, tv_shl);
5381 } else if (is_Shr(n) && is_Shl(left)) {
5382 ir_node *shl_right = get_Shl_right(left);
5384 if (!is_Const(shl_right))
5387 x = get_Shl_left(left);
5388 tv_shr = get_Const_tarval(right);
5389 tv_shl = get_Const_tarval(shl_right);
5391 tv_mask = tarval_shl(get_mode_all_one(mode), tv_shl);
5392 tv_mask = tarval_shr(tv_mask, tv_shr);
5397 if (get_tarval_mode(tv_shl) != get_tarval_mode(tv_shr)) {
5398 tv_shl = tarval_convert_to(tv_shl, get_tarval_mode(tv_shr));
5401 assert(tv_mask != tarval_bad);
5402 assert(get_tarval_mode(tv_mask) == mode);
5404 block = get_nodes_block(n);
5405 dbgi = get_irn_dbg_info(n);
5407 pnc = tarval_cmp(tv_shl, tv_shr);
5408 if (pnc == pn_Cmp_Lt || pnc == pn_Cmp_Eq) {
5409 tv_shift = tarval_sub(tv_shr, tv_shl, NULL);
5410 new_const = new_Const(tv_shift);
5412 new_shift = new_rd_Shrs(dbgi, block, x, new_const, mode);
5414 new_shift = new_rd_Shr(dbgi, block, x, new_const, mode);
5417 assert(pnc == pn_Cmp_Gt);
5418 tv_shift = tarval_sub(tv_shl, tv_shr, NULL);
5419 new_const = new_Const(tv_shift);
5420 new_shift = new_rd_Shl(dbgi, block, x, new_const, mode);
5423 new_const = new_Const(tv_mask);
5424 new_and = new_rd_And(dbgi, block, new_shift, new_const, mode);
5429 static tarval *get_modulo_tv_value(tarval *tv, int modulo_val)
5431 ir_mode *mode = get_tarval_mode(tv);
5432 tarval *modulo_tv = new_tarval_from_long(modulo_val, mode);
5433 return tarval_mod(tv, modulo_tv);
5436 typedef ir_node*(*new_shift_func)(dbg_info *dbgi, ir_node *block,
5437 ir_node *left, ir_node *right, ir_mode *mode);
5440 * Normalisation: if we have a shl/shr with modulo_shift behaviour
5441 * then we can use that to minimize the value of Add(x, const) or
5442 * Sub(Const, x). In particular this often avoids 1 instruction in some
5443 * backends for the Shift(x, Sub(Const, y)) case because it can be replaced
5444 * by Shift(x, Minus(y)) which doesnt't need an explicit Const constructed.
5446 static ir_node *transform_node_shift_modulo(ir_node *n,
5447 new_shift_func new_shift)
5449 ir_mode *mode = get_irn_mode(n);
5450 int modulo = get_mode_modulo_shift(mode);
5451 ir_node *newop = NULL;
5452 ir_mode *mode_right;
5459 if (get_mode_arithmetic(mode) != irma_twos_complement)
5461 if (!is_po2(modulo))
5464 irg = get_irn_irg(n);
5465 block = get_nodes_block(n);
5466 right = get_binop_right(n);
5467 mode_right = get_irn_mode(right);
5468 if (is_Const(right)) {
5469 tarval *tv = get_Const_tarval(right);
5470 tarval *tv_mod = get_modulo_tv_value(tv, modulo);
5475 newop = new_r_Const(irg, tv_mod);
5476 } else if (is_Add(right)) {
5477 ir_node *add_right = get_Add_right(right);
5478 if (is_Const(add_right)) {
5479 tarval *tv = get_Const_tarval(add_right);
5480 tarval *tv_mod = get_modulo_tv_value(tv, modulo);
5485 newconst = new_r_Const(irg, tv_mod);
5486 newop = new_r_Add(block, get_Add_left(right), newconst,
5489 } else if (is_Sub(right)) {
5490 ir_node *sub_left = get_Sub_left(right);
5491 if (is_Const(sub_left)) {
5492 tarval *tv = get_Const_tarval(sub_left);
5493 tarval *tv_mod = get_modulo_tv_value(tv, modulo);
5498 newconst = new_r_Const(irg, tv_mod);
5499 newop = new_r_Sub(block, newconst, get_Sub_right(right),
5506 if (newop != NULL) {
5507 dbg_info *dbgi = get_irn_dbg_info(n);
5508 ir_node *left = get_binop_left(n);
5509 return new_shift(dbgi, block, left, newop, mode);
5517 static ir_node *transform_node_Shr(ir_node *n)
5519 ir_node *c, *oldn = n;
5520 ir_node *left = get_Shr_left(n);
5521 ir_node *right = get_Shr_right(n);
5522 ir_mode *mode = get_irn_mode(n);
5524 HANDLE_BINOP_PHI((eval_func) tarval_shr, left, right, c, mode);
5525 n = transform_node_shift(n);
5528 n = transform_node_shift_modulo(n, new_rd_Shr);
5530 n = transform_node_shl_shr(n);
5532 n = transform_node_bitop_shift(n);
5535 } /* transform_node_Shr */
5540 static ir_node *transform_node_Shrs(ir_node *n)
5542 ir_node *c, *oldn = n;
5543 ir_node *a = get_Shrs_left(n);
5544 ir_node *b = get_Shrs_right(n);
5545 ir_mode *mode = get_irn_mode(n);
5547 HANDLE_BINOP_PHI((eval_func) tarval_shrs, a, b, c, mode);
5548 n = transform_node_shift(n);
5551 n = transform_node_shift_modulo(n, new_rd_Shrs);
5553 n = transform_node_bitop_shift(n);
5556 } /* transform_node_Shrs */
5561 static ir_node *transform_node_Shl(ir_node *n)
5563 ir_node *c, *oldn = n;
5564 ir_node *a = get_Shl_left(n);
5565 ir_node *b = get_Shl_right(n);
5566 ir_mode *mode = get_irn_mode(n);
5568 HANDLE_BINOP_PHI((eval_func) tarval_shl, a, b, c, mode);
5569 n = transform_node_shift(n);
5572 n = transform_node_shift_modulo(n, new_rd_Shl);
5574 n = transform_node_shl_shr(n);
5576 n = transform_node_bitop_shift(n);
5579 } /* transform_node_Shl */
5584 static ir_node *transform_node_Rotl(ir_node *n)
5586 ir_node *c, *oldn = n;
5587 ir_node *a = get_Rotl_left(n);
5588 ir_node *b = get_Rotl_right(n);
5589 ir_mode *mode = get_irn_mode(n);
5591 HANDLE_BINOP_PHI((eval_func) tarval_rotl, a, b, c, mode);
5592 n = transform_node_shift(n);
5595 n = transform_node_bitop_shift(n);
5598 } /* transform_node_Rotl */
5603 static ir_node *transform_node_Conv(ir_node *n)
5605 ir_node *c, *oldn = n;
5606 ir_mode *mode = get_irn_mode(n);
5607 ir_node *a = get_Conv_op(n);
5609 if (mode != mode_b && is_const_Phi(a)) {
5610 /* Do NOT optimize mode_b Conv's, this leads to remaining
5611 * Phib nodes later, because the conv_b_lower operation
5612 * is instantly reverted, when it tries to insert a Convb.
5614 c = apply_conv_on_phi(a, mode);
5616 DBG_OPT_ALGSIM0(oldn, c, FS_OPT_CONST_PHI);
5621 if (is_Unknown(a)) { /* Conv_A(Unknown_B) -> Unknown_A */
5622 ir_graph *irg = get_irn_irg(n);
5623 return new_r_Unknown(irg, mode);
5626 if (mode_is_reference(mode) &&
5627 get_mode_size_bits(mode) == get_mode_size_bits(get_irn_mode(a)) &&
5629 ir_node *l = get_Add_left(a);
5630 ir_node *r = get_Add_right(a);
5631 dbg_info *dbgi = get_irn_dbg_info(a);
5632 ir_node *block = get_nodes_block(n);
5634 ir_node *lop = get_Conv_op(l);
5635 if (get_irn_mode(lop) == mode) {
5636 /* ConvP(AddI(ConvI(P), x)) -> AddP(P, x) */
5637 n = new_rd_Add(dbgi, block, lop, r, mode);
5642 ir_node *rop = get_Conv_op(r);
5643 if (get_irn_mode(rop) == mode) {
5644 /* ConvP(AddI(x, ConvI(P))) -> AddP(x, P) */
5645 n = new_rd_Add(dbgi, block, l, rop, mode);
5652 } /* transform_node_Conv */
5655 * Remove dead blocks and nodes in dead blocks
5656 * in keep alive list. We do not generate a new End node.
5658 static ir_node *transform_node_End(ir_node *n)
5660 int i, j, n_keepalives = get_End_n_keepalives(n);
5663 NEW_ARR_A(ir_node *, in, n_keepalives);
5665 for (i = j = 0; i < n_keepalives; ++i) {
5666 ir_node *ka = get_End_keepalive(n, i);
5668 if (! is_Block_dead(ka)) {
5672 } else if (is_irn_pinned_in_irg(ka) && is_Block_dead(get_nodes_block(ka))) {
5674 } else if (is_Bad(ka)) {
5675 /* no need to keep Bad */
5680 if (j != n_keepalives)
5681 set_End_keepalives(n, j, in);
5683 } /* transform_node_End */
5685 bool is_negated_value(ir_node *a, ir_node *b)
5687 if (is_Minus(a) && get_Minus_op(a) == b)
5689 if (is_Minus(b) && get_Minus_op(b) == a)
5691 if (is_Sub(a) && is_Sub(b)) {
5692 ir_node *a_left = get_Sub_left(a);
5693 ir_node *a_right = get_Sub_right(a);
5694 ir_node *b_left = get_Sub_left(b);
5695 ir_node *b_right = get_Sub_right(b);
5697 if (a_left == b_right && a_right == b_left)
5705 * Optimize a Mux into some simpler cases.
5707 static ir_node *transform_node_Mux(ir_node *n)
5709 ir_node *oldn = n, *sel = get_Mux_sel(n);
5710 ir_mode *mode = get_irn_mode(n);
5711 ir_node *t = get_Mux_true(n);
5712 ir_node *f = get_Mux_false(n);
5713 ir_graph *irg = get_irn_irg(n);
5715 if (is_irg_state(irg, IR_GRAPH_STATE_KEEP_MUX))
5719 ir_node* block = get_nodes_block(n);
5721 ir_node* c1 = get_Mux_sel(t);
5722 ir_node* t1 = get_Mux_true(t);
5723 ir_node* f1 = get_Mux_false(t);
5725 /* Mux(cond0, Mux(cond1, x, y), y) -> typical if (cond0 && cond1) x else y */
5726 ir_node* and_ = new_r_And(block, c0, c1, mode_b);
5727 ir_node* new_mux = new_r_Mux(block, and_, f1, t1, mode);
5732 DBG_OPT_ALGSIM0(oldn, t, FS_OPT_MUX_COMBINE);
5733 } else if (f == t1) {
5734 /* Mux(cond0, Mux(cond1, x, y), x) */
5735 ir_node* not_c1 = new_r_Not(block, c1, mode_b);
5736 ir_node* and_ = new_r_And(block, c0, not_c1, mode_b);
5737 ir_node* new_mux = new_r_Mux(block, and_, t1, f1, mode);
5742 DBG_OPT_ALGSIM0(oldn, t, FS_OPT_MUX_COMBINE);
5744 } else if (is_Mux(f)) {
5745 ir_node* block = get_nodes_block(n);
5747 ir_node* c1 = get_Mux_sel(f);
5748 ir_node* t1 = get_Mux_true(f);
5749 ir_node* f1 = get_Mux_false(f);
5751 /* Mux(cond0, x, Mux(cond1, x, y)) -> typical if (cond0 || cond1) x else y */
5752 ir_node* or_ = new_r_Or(block, c0, c1, mode_b);
5753 ir_node* new_mux = new_r_Mux(block, or_, f1, t1, mode);
5758 DBG_OPT_ALGSIM0(oldn, f, FS_OPT_MUX_COMBINE);
5759 } else if (t == f1) {
5760 /* Mux(cond0, x, Mux(cond1, y, x)) */
5761 ir_node* not_c1 = new_r_Not(block, c1, mode_b);
5762 ir_node* or_ = new_r_Or(block, c0, not_c1, mode_b);
5763 ir_node* new_mux = new_r_Mux(block, or_, t1, f1, mode);
5768 DBG_OPT_ALGSIM0(oldn, f, FS_OPT_MUX_COMBINE);
5772 /* first normalization step: try to move a constant to the false side,
5773 * 0 preferred on false side too */
5775 ir_node *cmp = get_Proj_pred(sel);
5777 if (is_Cmp(cmp) && is_Const(t) &&
5778 (!is_Const(f) || (is_Const_null(t) && !is_Const_null(f)))) {
5779 pn_Cmp pnc = get_Proj_proj(sel);
5784 /* Mux(x, a, b) => Mux(not(x), b, a) */
5785 sel = new_r_Proj(cmp, mode_b,
5786 get_negated_pnc(pnc, get_irn_mode(get_Cmp_left(cmp))));
5787 n = new_rd_Mux(get_irn_dbg_info(n), get_nodes_block(n), sel, f, t, mode);
5791 /* note: after normalization, false can only happen on default */
5792 if (mode == mode_b) {
5793 dbg_info *dbg = get_irn_dbg_info(n);
5794 ir_node *block = get_nodes_block(n);
5797 tarval *tv_t = get_Const_tarval(t);
5798 if (tv_t == tarval_b_true) {
5800 /* Muxb(sel, true, false) = sel */
5801 assert(get_Const_tarval(f) == tarval_b_false);
5802 DBG_OPT_ALGSIM0(oldn, sel, FS_OPT_MUX_BOOL);
5805 /* Muxb(sel, true, x) = Or(sel, x) */
5806 n = new_rd_Or(dbg, block, sel, f, mode_b);
5807 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_OR_BOOL);
5811 } else if (is_Const(f)) {
5812 tarval *tv_f = get_Const_tarval(f);
5813 if (tv_f == tarval_b_true) {
5814 /* Muxb(sel, x, true) = Or(Not(sel), x) */
5815 ir_node* not_sel = new_rd_Not(dbg, block, sel, mode_b);
5816 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_ORNOT_BOOL);
5817 n = new_rd_Or(dbg, block, not_sel, t, mode_b);
5820 /* Muxb(sel, x, false) = And(sel, x) */
5821 assert(tv_f == tarval_b_false);
5822 n = new_rd_And(dbg, block, sel, t, mode_b);
5823 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_AND_BOOL);
5829 /* more normalization: Mux(sel, 0, 1) is simply a conv from the mode_b
5830 * value to integer. */
5831 if (is_Const(t) && is_Const(f) && mode_is_int(mode)) {
5832 tarval *a = get_Const_tarval(t);
5833 tarval *b = get_Const_tarval(f);
5835 if (tarval_is_one(a) && tarval_is_null(b)) {
5836 ir_node *block = get_nodes_block(n);
5837 ir_node *conv = new_r_Conv(block, sel, mode);
5839 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_CONV);
5841 } else if (tarval_is_null(a) && tarval_is_one(b)) {
5842 ir_node *block = get_nodes_block(n);
5843 ir_node *not_ = new_r_Not(block, sel, mode_b);
5844 ir_node *conv = new_r_Conv(block, not_, mode);
5846 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_CONV);
5852 ir_node *cmp = get_Proj_pred(sel);
5853 long pn = get_Proj_proj(sel);
5856 * Note: normalization puts the constant on the right side,
5857 * so we check only one case.
5860 ir_node *cmp_r = get_Cmp_right(cmp);
5861 if (is_Const(cmp_r) && is_Const_null(cmp_r)) {
5862 ir_node *block = get_nodes_block(n);
5863 ir_node *cmp_l = get_Cmp_left(cmp);
5865 if (mode_is_int(mode)) {
5867 if ((pn == pn_Cmp_Lg || pn == pn_Cmp_Eq) && is_And(cmp_l)) {
5868 /* Mux((a & b) != 0, c, 0) */
5869 ir_node *and_r = get_And_right(cmp_l);
5872 if (and_r == t && f == cmp_r) {
5873 if (is_Const(t) && tarval_is_single_bit(get_Const_tarval(t))) {
5874 if (pn == pn_Cmp_Lg) {
5875 /* Mux((a & 2^C) != 0, 2^C, 0) */
5877 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_BITOP);
5879 /* Mux((a & 2^C) == 0, 2^C, 0) */
5880 n = new_rd_Eor(get_irn_dbg_info(n),
5881 block, cmp_l, t, mode);
5882 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_BITOP);
5887 if (is_Shl(and_r)) {
5888 ir_node *shl_l = get_Shl_left(and_r);
5889 if (is_Const(shl_l) && is_Const_one(shl_l)) {
5890 if (and_r == t && f == cmp_r) {
5891 if (pn == pn_Cmp_Lg) {
5892 /* (a & (1 << n)) != 0, (1 << n), 0) */
5894 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_BITOP);
5896 /* (a & (1 << n)) == 0, (1 << n), 0) */
5897 n = new_rd_Eor(get_irn_dbg_info(n),
5898 block, cmp_l, t, mode);
5899 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_BITOP);
5905 and_l = get_And_left(cmp_l);
5906 if (is_Shl(and_l)) {
5907 ir_node *shl_l = get_Shl_left(and_l);
5908 if (is_Const(shl_l) && is_Const_one(shl_l)) {
5909 if (and_l == t && f == cmp_r) {
5910 if (pn == pn_Cmp_Lg) {
5911 /* ((1 << n) & a) != 0, (1 << n), 0) */
5913 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_BITOP);
5915 /* ((1 << n) & a) == 0, (1 << n), 0) */
5916 n = new_rd_Eor(get_irn_dbg_info(n),
5917 block, cmp_l, t, mode);
5918 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_BITOP);
5931 } /* transform_node_Mux */
5934 * optimize Sync nodes that have other syncs as input we simply add the inputs
5935 * of the other sync to our own inputs
5937 static ir_node *transform_node_Sync(ir_node *n)
5939 int arity = get_Sync_n_preds(n);
5942 for (i = 0; i < arity;) {
5943 ir_node *pred = get_Sync_pred(n, i);
5947 if (!is_Sync(pred)) {
5955 pred_arity = get_Sync_n_preds(pred);
5956 for (j = 0; j < pred_arity; ++j) {
5957 ir_node *pred_pred = get_Sync_pred(pred, j);
5962 add_irn_n(n, pred_pred);
5966 if (get_Sync_pred(n, k) == pred_pred) break;
5971 /* rehash the sync node */
5975 } /* transform_node_Sync */
5978 * optimize a trampoline Call into a direct Call
5980 static ir_node *transform_node_Call(ir_node *call)
5982 ir_node *callee = get_Call_ptr(call);
5983 ir_node *adr, *mem, *res, *bl, **in;
5984 ir_type *ctp, *mtp, *tp;
5988 int i, n_res, n_param;
5991 if (! is_Proj(callee))
5993 callee = get_Proj_pred(callee);
5994 if (! is_Builtin(callee))
5996 if (get_Builtin_kind(callee) != ir_bk_inner_trampoline)
5999 mem = get_Call_mem(call);
6001 if (skip_Proj(mem) == callee) {
6002 /* memory is routed to the trampoline, skip */
6003 mem = get_Builtin_mem(callee);
6006 /* build a new call type */
6007 mtp = get_Call_type(call);
6008 tdb = get_type_dbg_info(mtp);
6010 n_res = get_method_n_ress(mtp);
6011 n_param = get_method_n_params(mtp);
6012 ctp = new_d_type_method(n_param + 1, n_res, tdb);
6014 for (i = 0; i < n_res; ++i)
6015 set_method_res_type(ctp, i, get_method_res_type(mtp, i));
6017 NEW_ARR_A(ir_node *, in, n_param + 1);
6019 /* FIXME: we don't need a new pointer type in every step */
6020 irg = get_irn_irg(call);
6021 tp = get_irg_frame_type(irg);
6022 tp = new_type_pointer(tp);
6023 set_method_param_type(ctp, 0, tp);
6025 in[0] = get_Builtin_param(callee, 2);
6026 for (i = 0; i < n_param; ++i) {
6027 set_method_param_type(ctp, i + 1, get_method_param_type(mtp, i));
6028 in[i + 1] = get_Call_param(call, i);
6030 var = get_method_variadicity(mtp);
6031 set_method_variadicity(ctp, var);
6032 if (var == variadicity_variadic) {
6033 set_method_first_variadic_param_index(ctp, get_method_first_variadic_param_index(mtp) + 1);
6035 /* When we resolve a trampoline, the function must be called by a this-call */
6036 set_method_calling_convention(ctp, get_method_calling_convention(mtp) | cc_this_call);
6037 set_method_additional_properties(ctp, get_method_additional_properties(mtp));
6039 adr = get_Builtin_param(callee, 1);
6041 db = get_irn_dbg_info(call);
6042 bl = get_nodes_block(call);
6044 res = new_rd_Call(db, bl, mem, adr, n_param + 1, in, ctp);
6045 if (get_irn_pinned(call) == op_pin_state_floats)
6046 set_irn_pinned(res, op_pin_state_floats);
6048 } /* transform_node_Call */
6051 * Tries several [inplace] [optimizing] transformations and returns an
6052 * equivalent node. The difference to equivalent_node() is that these
6053 * transformations _do_ generate new nodes, and thus the old node must
6054 * not be freed even if the equivalent node isn't the old one.
6056 static ir_node *transform_node(ir_node *n)
6061 * Transform_node is the only "optimizing transformation" that might
6062 * return a node with a different opcode. We iterate HERE until fixpoint
6063 * to get the final result.
6067 if (n->op->ops.transform_node != NULL)
6068 n = n->op->ops.transform_node(n);
6069 } while (oldn != n);
6072 } /* transform_node */
6075 * Sets the default transform node operation for an ir_op_ops.
6077 * @param code the opcode for the default operation
6078 * @param ops the operations initialized
6083 static ir_op_ops *firm_set_default_transform_node(ir_opcode code, ir_op_ops *ops)
6087 ops->transform_node = transform_node_##a; \
6089 #define CASE_PROJ(a) \
6091 ops->transform_node_Proj = transform_node_Proj_##a; \
6093 #define CASE_PROJ_EX(a) \
6095 ops->transform_node = transform_node_##a; \
6096 ops->transform_node_Proj = transform_node_Proj_##a; \
6105 CASE_PROJ_EX(DivMod);
6139 } /* firm_set_default_transform_node */
6142 /* **************** Common Subexpression Elimination **************** */
6144 /** The size of the hash table used, should estimate the number of nodes
6146 #define N_IR_NODES 512
6148 /** Compares the attributes of two Const nodes. */
6149 static int node_cmp_attr_Const(ir_node *a, ir_node *b)
6151 return (get_Const_tarval(a) != get_Const_tarval(b))
6152 || (get_Const_type(a) != get_Const_type(b));
6153 } /* node_cmp_attr_Const */
6155 /** Compares the attributes of two Proj nodes. */
6156 static int node_cmp_attr_Proj(ir_node *a, ir_node *b)
6158 return a->attr.proj != b->attr.proj;
6159 } /* node_cmp_attr_Proj */
6161 /** Compares the attributes of two Alloc nodes. */
6162 static int node_cmp_attr_Alloc(ir_node *a, ir_node *b)
6164 const alloc_attr *pa = &a->attr.alloc;
6165 const alloc_attr *pb = &b->attr.alloc;
6166 return (pa->where != pb->where) || (pa->type != pb->type);
6167 } /* node_cmp_attr_Alloc */
6169 /** Compares the attributes of two Free nodes. */
6170 static int node_cmp_attr_Free(ir_node *a, ir_node *b)
6172 const free_attr *pa = &a->attr.free;
6173 const free_attr *pb = &b->attr.free;
6174 return (pa->where != pb->where) || (pa->type != pb->type);
6175 } /* node_cmp_attr_Free */
6177 /** Compares the attributes of two SymConst nodes. */
6178 static int node_cmp_attr_SymConst(ir_node *a, ir_node *b)
6180 const symconst_attr *pa = &a->attr.symc;
6181 const symconst_attr *pb = &b->attr.symc;
6182 return (pa->kind != pb->kind)
6183 || (pa->sym.type_p != pb->sym.type_p)
6184 || (pa->tp != pb->tp);
6185 } /* node_cmp_attr_SymConst */
6187 /** Compares the attributes of two Call nodes. */
6188 static int node_cmp_attr_Call(ir_node *a, ir_node *b)
6190 const call_attr *pa = &a->attr.call;
6191 const call_attr *pb = &b->attr.call;
6192 return (pa->type != pb->type)
6193 || (pa->tail_call != pb->tail_call);
6194 } /* node_cmp_attr_Call */
6196 /** Compares the attributes of two Sel nodes. */
6197 static int node_cmp_attr_Sel(ir_node *a, ir_node *b)
6199 const ir_entity *a_ent = get_Sel_entity(a);
6200 const ir_entity *b_ent = get_Sel_entity(b);
6201 return a_ent != b_ent;
6202 } /* node_cmp_attr_Sel */
6204 /** Compares the attributes of two Phi nodes. */
6205 static int node_cmp_attr_Phi(ir_node *a, ir_node *b)
6207 /* we can only enter this function if both nodes have the same number of inputs,
6208 hence it is enough to check if one of them is a Phi0 */
6210 /* check the Phi0 pos attribute */
6211 return a->attr.phi.u.pos != b->attr.phi.u.pos;
6214 } /* node_cmp_attr_Phi */
6216 /** Compares the attributes of two Conv nodes. */
6217 static int node_cmp_attr_Conv(ir_node *a, ir_node *b)
6219 return get_Conv_strict(a) != get_Conv_strict(b);
6220 } /* node_cmp_attr_Conv */
6222 /** Compares the attributes of two Cast nodes. */
6223 static int node_cmp_attr_Cast(ir_node *a, ir_node *b)
6225 return get_Cast_type(a) != get_Cast_type(b);
6226 } /* node_cmp_attr_Cast */
6228 /** Compares the attributes of two Load nodes. */
6229 static int node_cmp_attr_Load(ir_node *a, ir_node *b)
6231 if (get_Load_volatility(a) == volatility_is_volatile ||
6232 get_Load_volatility(b) == volatility_is_volatile)
6233 /* NEVER do CSE on volatile Loads */
6235 /* do not CSE Loads with different alignment. Be conservative. */
6236 if (get_Load_align(a) != get_Load_align(b))
6239 return get_Load_mode(a) != get_Load_mode(b);
6240 } /* node_cmp_attr_Load */
6242 /** Compares the attributes of two Store nodes. */
6243 static int node_cmp_attr_Store(ir_node *a, ir_node *b)
6245 /* do not CSE Stores with different alignment. Be conservative. */
6246 if (get_Store_align(a) != get_Store_align(b))
6249 /* NEVER do CSE on volatile Stores */
6250 return (get_Store_volatility(a) == volatility_is_volatile ||
6251 get_Store_volatility(b) == volatility_is_volatile);
6252 } /* node_cmp_attr_Store */
6254 /** Compares two exception attributes */
6255 static int node_cmp_exception(ir_node *a, ir_node *b)
6257 const except_attr *ea = &a->attr.except;
6258 const except_attr *eb = &b->attr.except;
6260 return ea->pin_state != eb->pin_state;
6263 #define node_cmp_attr_Bound node_cmp_exception
6265 /** Compares the attributes of two Div nodes. */
6266 static int node_cmp_attr_Div(ir_node *a, ir_node *b)
6268 const divmod_attr *ma = &a->attr.divmod;
6269 const divmod_attr *mb = &b->attr.divmod;
6270 return ma->exc.pin_state != mb->exc.pin_state ||
6271 ma->resmode != mb->resmode ||
6272 ma->no_remainder != mb->no_remainder;
6273 } /* node_cmp_attr_Div */
6275 /** Compares the attributes of two DivMod nodes. */
6276 static int node_cmp_attr_DivMod(ir_node *a, ir_node *b)
6278 const divmod_attr *ma = &a->attr.divmod;
6279 const divmod_attr *mb = &b->attr.divmod;
6280 return ma->exc.pin_state != mb->exc.pin_state ||
6281 ma->resmode != mb->resmode;
6282 } /* node_cmp_attr_DivMod */
6284 /** Compares the attributes of two Mod nodes. */
6285 static int node_cmp_attr_Mod(ir_node *a, ir_node *b)
6287 return node_cmp_attr_DivMod(a, b);
6288 } /* node_cmp_attr_Mod */
6290 /** Compares the attributes of two Quot nodes. */
6291 static int node_cmp_attr_Quot(ir_node *a, ir_node *b)
6293 return node_cmp_attr_DivMod(a, b);
6294 } /* node_cmp_attr_Quot */
6296 /** Compares the attributes of two Confirm nodes. */
6297 static int node_cmp_attr_Confirm(ir_node *a, ir_node *b)
6299 /* no need to compare the bound, as this is a input */
6300 return (get_Confirm_cmp(a) != get_Confirm_cmp(b));
6301 } /* node_cmp_attr_Confirm */
6303 /** Compares the attributes of two Builtin nodes. */
6304 static int node_cmp_attr_Builtin(ir_node *a, ir_node *b)
6306 /* no need to compare the type, equal kind means equal type */
6307 return get_Builtin_kind(a) != get_Builtin_kind(b);
6308 } /* node_cmp_attr_Builtin */
6310 /** Compares the attributes of two ASM nodes. */
6311 static int node_cmp_attr_ASM(ir_node *a, ir_node *b)
6314 const ir_asm_constraint *ca;
6315 const ir_asm_constraint *cb;
6318 if (get_ASM_text(a) != get_ASM_text(b))
6321 /* Should we really check the constraints here? Should be better, but is strange. */
6322 n = get_ASM_n_input_constraints(a);
6323 if (n != get_ASM_n_input_constraints(b))
6326 ca = get_ASM_input_constraints(a);
6327 cb = get_ASM_input_constraints(b);
6328 for (i = 0; i < n; ++i) {
6329 if (ca[i].pos != cb[i].pos || ca[i].constraint != cb[i].constraint)
6333 n = get_ASM_n_output_constraints(a);
6334 if (n != get_ASM_n_output_constraints(b))
6337 ca = get_ASM_output_constraints(a);
6338 cb = get_ASM_output_constraints(b);
6339 for (i = 0; i < n; ++i) {
6340 if (ca[i].pos != cb[i].pos || ca[i].constraint != cb[i].constraint)
6344 n = get_ASM_n_clobbers(a);
6345 if (n != get_ASM_n_clobbers(b))
6348 cla = get_ASM_clobbers(a);
6349 clb = get_ASM_clobbers(b);
6350 for (i = 0; i < n; ++i) {
6351 if (cla[i] != clb[i])
6355 } /* node_cmp_attr_ASM */
6357 /** Compares the inexistent attributes of two Dummy nodes. */
6358 static int node_cmp_attr_Dummy(ir_node *a, ir_node *b)
6366 * Set the default node attribute compare operation for an ir_op_ops.
6368 * @param code the opcode for the default operation
6369 * @param ops the operations initialized
6374 static ir_op_ops *firm_set_default_node_cmp_attr(ir_opcode code, ir_op_ops *ops)
6378 ops->node_cmp_attr = node_cmp_attr_##a; \
6411 } /* firm_set_default_node_cmp_attr */
6414 * Compare function for two nodes in the value table. Gets two
6415 * nodes as parameters. Returns 0 if the nodes are a Common Sub Expression.
6417 int identities_cmp(const void *elt, const void *key)
6419 ir_node *a = (ir_node *)elt;
6420 ir_node *b = (ir_node *)key;
6423 if (a == b) return 0;
6425 if ((get_irn_op(a) != get_irn_op(b)) ||
6426 (get_irn_mode(a) != get_irn_mode(b))) return 1;
6428 /* compare if a's in and b's in are of equal length */
6429 irn_arity_a = get_irn_arity(a);
6430 if (irn_arity_a != get_irn_arity(b))
6433 /* blocks are never the same */
6437 if (get_irn_pinned(a) == op_pin_state_pinned) {
6438 /* for pinned nodes, the block inputs must be equal */
6439 if (get_irn_n(a, -1) != get_irn_n(b, -1))
6441 } else if (! get_opt_global_cse()) {
6442 /* for block-local CSE both nodes must be in the same Block */
6443 if (get_nodes_block(a) != get_nodes_block(b))
6447 /* compare a->in[0..ins] with b->in[0..ins] */
6448 for (i = 0; i < irn_arity_a; ++i) {
6449 ir_node *pred_a = get_irn_n(a, i);
6450 ir_node *pred_b = get_irn_n(b, i);
6451 if (pred_a != pred_b) {
6452 /* if both predecessors are CSE neutral they might be different */
6453 if (!is_irn_cse_neutral(pred_a) || !is_irn_cse_neutral(pred_b))
6459 * here, we already now that the nodes are identical except their
6462 if (a->op->ops.node_cmp_attr)
6463 return a->op->ops.node_cmp_attr(a, b);
6466 } /* identities_cmp */
6469 * Calculate a hash value of a node.
6471 * @param node The IR-node
6473 unsigned ir_node_hash(const ir_node *node)
6475 return node->op->ops.hash(node);
6476 } /* ir_node_hash */
6479 void new_identities(ir_graph *irg)
6481 if (irg->value_table != NULL)
6482 del_pset(irg->value_table);
6483 irg->value_table = new_pset(identities_cmp, N_IR_NODES);
6484 } /* new_identities */
6486 void del_identities(ir_graph *irg)
6488 if (irg->value_table != NULL)
6489 del_pset(irg->value_table);
6490 } /* del_identities */
6492 /* Normalize a node by putting constants (and operands with larger
6493 * node index) on the right (operator side). */
6494 void ir_normalize_node(ir_node *n)
6496 if (is_op_commutative(get_irn_op(n))) {
6497 ir_node *l = get_binop_left(n);
6498 ir_node *r = get_binop_right(n);
6500 /* For commutative operators perform a OP b == b OP a but keep
6501 * constants on the RIGHT side. This helps greatly in some
6502 * optimizations. Moreover we use the idx number to make the form
6504 if (!operands_are_normalized(l, r)) {
6505 set_binop_left(n, r);
6506 set_binop_right(n, l);
6510 } /* ir_normalize_node */
6513 * Return the canonical node computing the same value as n.
6514 * Looks up the node in a hash table, enters it in the table
6515 * if it isn't there yet.
6517 * @param n the node to look up
6519 * @return a node that computes the same value as n or n if no such
6520 * node could be found
6522 ir_node *identify_remember(ir_node *n)
6524 ir_graph *irg = get_irn_irg(n);
6525 pset *value_table = irg->value_table;
6528 if (value_table == NULL)
6531 ir_normalize_node(n);
6532 /* lookup or insert in hash table with given hash key. */
6533 nn = pset_insert(value_table, n, ir_node_hash(n));
6536 /* n is reachable again */
6537 edges_node_revival(nn, get_irn_irg(nn));
6541 } /* identify_remember */
6544 * During construction we set the op_pin_state_pinned flag in the graph right
6545 * when the optimization is performed. The flag turning on procedure global
6546 * cse could be changed between two allocations. This way we are safe.
6548 * @param n The node to lookup
6550 static inline ir_node *identify_cons(ir_node *n)
6554 n = identify_remember(n);
6555 if (n != old && get_nodes_block(old) != get_nodes_block(n)) {
6556 ir_graph *irg = get_irn_irg(n);
6557 set_irg_pinned(irg, op_pin_state_floats);
6560 } /* identify_cons */
6562 /* Add a node to the identities value table. */
6563 void add_identities(ir_node *node)
6570 identify_remember(node);
6573 /* Visit each node in the value table of a graph. */
6574 void visit_all_identities(ir_graph *irg, irg_walk_func visit, void *env)
6577 ir_graph *rem = current_ir_graph;
6579 current_ir_graph = irg;
6580 foreach_pset(irg->value_table, node) {
6583 current_ir_graph = rem;
6584 } /* visit_all_identities */
6587 * Garbage in, garbage out. If a node has a dead input, i.e., the
6588 * Bad node is input to the node, return the Bad node.
6590 static ir_node *gigo(ir_node *node)
6593 ir_op *op = get_irn_op(node);
6595 /* remove garbage blocks by looking at control flow that leaves the block
6596 and replacing the control flow by Bad. */
6597 if (get_irn_mode(node) == mode_X) {
6598 ir_node *block = get_nodes_block(skip_Proj(node));
6600 /* Don't optimize nodes in immature blocks. */
6601 if (!get_Block_matured(block))
6603 /* Don't optimize End, may have Bads. */
6604 if (op == op_End) return node;
6606 if (is_Block(block)) {
6607 if (is_Block_dead(block)) {
6608 /* control flow from dead block is dead */
6612 for (i = get_irn_arity(block) - 1; i >= 0; --i) {
6613 if (!is_Bad(get_irn_n(block, i)))
6617 ir_graph *irg = get_irn_irg(block);
6618 /* the start block is never dead */
6619 if (block != get_irg_start_block(irg)
6620 && block != get_irg_end_block(irg)) {
6622 * Do NOT kill control flow without setting
6623 * the block to dead of bad things can happen:
6624 * We get a Block that is not reachable be irg_block_walk()
6625 * but can be found by irg_walk()!
6627 set_Block_dead(block);
6634 /* Blocks, Phis and Tuples may have dead inputs, e.g., if one of the
6635 blocks predecessors is dead. */
6636 if (op != op_Block && op != op_Phi && op != op_Tuple) {
6637 irn_arity = get_irn_arity(node);
6640 * Beware: we can only read the block of a non-floating node.
6642 if (is_irn_pinned_in_irg(node) &&
6643 is_Block_dead(get_nodes_block(skip_Proj(node))))
6646 for (i = 0; i < irn_arity; i++) {
6647 ir_node *pred = get_irn_n(node, i);
6652 /* Propagating Unknowns here seems to be a bad idea, because
6653 sometimes we need a node as a input and did not want that
6655 However, it might be useful to move this into a later phase
6656 (if you think that optimizing such code is useful). */
6657 if (is_Unknown(pred) && mode_is_data(get_irn_mode(node)))
6658 return new_Unknown(get_irn_mode(node));
6663 /* With this code we violate the agreement that local_optimize
6664 only leaves Bads in Block, Phi and Tuple nodes. */
6665 /* If Block has only Bads as predecessors it's garbage. */
6666 /* If Phi has only Bads as predecessors it's garbage. */
6667 if ((op == op_Block && get_Block_matured(node)) || op == op_Phi) {
6668 irn_arity = get_irn_arity(node);
6669 for (i = 0; i < irn_arity; i++) {
6670 if (!is_Bad(get_irn_n(node, i))) break;
6672 if (i == irn_arity) node = new_Bad();
6679 * These optimizations deallocate nodes from the obstack.
6680 * It can only be called if it is guaranteed that no other nodes
6681 * reference this one, i.e., right after construction of a node.
6683 * @param n The node to optimize
6685 ir_node *optimize_node(ir_node *n)
6689 ir_graph *irg = get_irn_irg(n);
6690 ir_opcode iro = get_irn_opcode(n);
6692 /* Always optimize Phi nodes: part of the construction. */
6693 if ((!get_opt_optimize()) && (iro != iro_Phi)) return n;
6695 /* constant expression evaluation / constant folding */
6696 if (get_opt_constant_folding()) {
6697 /* neither constants nor Tuple values can be evaluated */
6698 if (iro != iro_Const && (get_irn_mode(n) != mode_T)) {
6699 /* try to evaluate */
6700 tv = computed_value(n);
6701 if (tv != tarval_bad) {
6703 ir_type *old_tp = get_irn_type(n);
6704 int i, arity = get_irn_arity(n);
6708 * Try to recover the type of the new expression.
6710 for (i = 0; i < arity && !old_tp; ++i)
6711 old_tp = get_irn_type(get_irn_n(n, i));
6714 * we MUST copy the node here temporary, because it's still needed
6715 * for DBG_OPT_CSTEVAL
6717 node_size = offsetof(ir_node, attr) + n->op->attr_size;
6718 oldn = alloca(node_size);
6720 memcpy(oldn, n, node_size);
6721 CLONE_ARR_A(ir_node *, oldn->in, n->in);
6723 /* ARG, copy the in array, we need it for statistics */
6724 memcpy(oldn->in, n->in, ARR_LEN(n->in) * sizeof(n->in[0]));
6726 /* note the inplace edges module */
6727 edges_node_deleted(n, irg);
6729 /* evaluation was successful -- replace the node. */
6730 irg_kill_node(irg, n);
6733 if (old_tp && get_type_mode(old_tp) == get_tarval_mode(tv))
6734 set_Const_type(nw, old_tp);
6735 DBG_OPT_CSTEVAL(oldn, nw);
6741 /* remove unnecessary nodes */
6742 if (get_opt_algebraic_simplification() ||
6743 (iro == iro_Phi) || /* always optimize these nodes. */
6745 (iro == iro_Proj) ||
6746 (iro == iro_Block) ) /* Flags tested local. */
6747 n = equivalent_node(n);
6749 /* Common Subexpression Elimination.
6751 * Checks whether n is already available.
6752 * The block input is used to distinguish different subexpressions. Right
6753 * now all nodes are op_pin_state_pinned to blocks, i.e., the CSE only finds common
6754 * subexpressions within a block.
6757 n = identify_cons(n);
6760 edges_node_deleted(oldn, irg);
6762 /* We found an existing, better node, so we can deallocate the old node. */
6763 irg_kill_node(irg, oldn);
6767 /* Some more constant expression evaluation that does not allow to
6769 iro = get_irn_opcode(n);
6770 if (get_opt_algebraic_simplification() ||
6771 (iro == iro_Cond) ||
6772 (iro == iro_Proj)) /* Flags tested local. */
6773 n = transform_node(n);
6775 /* Remove nodes with dead (Bad) input.
6776 Run always for transformation induced Bads. */
6779 /* Now we have a legal, useful node. Enter it in hash table for CSE */
6780 if (get_opt_cse() && (get_irn_opcode(n) != iro_Block)) {
6782 n = identify_remember(o);
6788 } /* optimize_node */
6792 * These optimizations never deallocate nodes (in place). This can cause dead
6793 * nodes lying on the obstack. Remove these by a dead node elimination,
6794 * i.e., a copying garbage collection.
6796 ir_node *optimize_in_place_2(ir_node *n)
6800 ir_opcode iro = get_irn_opcode(n);
6802 if (!get_opt_optimize() && !is_Phi(n)) return n;
6804 if (iro == iro_Deleted)
6807 /* constant expression evaluation / constant folding */
6808 if (get_opt_constant_folding()) {
6809 /* neither constants nor Tuple values can be evaluated */
6810 if (iro != iro_Const && get_irn_mode(n) != mode_T) {
6811 /* try to evaluate */
6812 tv = computed_value(n);
6813 if (tv != tarval_bad) {
6814 /* evaluation was successful -- replace the node. */
6815 ir_type *old_tp = get_irn_type(n);
6816 int i, arity = get_irn_arity(n);
6819 * Try to recover the type of the new expression.
6821 for (i = 0; i < arity && !old_tp; ++i)
6822 old_tp = get_irn_type(get_irn_n(n, i));
6826 if (old_tp && get_type_mode(old_tp) == get_tarval_mode(tv))
6827 set_Const_type(n, old_tp);
6829 DBG_OPT_CSTEVAL(oldn, n);
6835 /* remove unnecessary nodes */
6836 if (get_opt_constant_folding() ||
6837 (iro == iro_Phi) || /* always optimize these nodes. */
6838 (iro == iro_Id) || /* ... */
6839 (iro == iro_Proj) || /* ... */
6840 (iro == iro_Block) ) /* Flags tested local. */
6841 n = equivalent_node(n);
6843 /** common subexpression elimination **/
6844 /* Checks whether n is already available. */
6845 /* The block input is used to distinguish different subexpressions. Right
6846 now all nodes are op_pin_state_pinned to blocks, i.e., the cse only finds common
6847 subexpressions within a block. */
6848 if (get_opt_cse()) {
6850 n = identify_remember(o);
6855 /* Some more constant expression evaluation. */
6856 iro = get_irn_opcode(n);
6857 if (get_opt_constant_folding() ||
6858 (iro == iro_Cond) ||
6859 (iro == iro_Proj)) /* Flags tested local. */
6860 n = transform_node(n);
6862 /* Remove nodes with dead (Bad) input.
6863 Run always for transformation induced Bads. */
6866 /* Now we can verify the node, as it has no dead inputs any more. */
6869 /* Now we have a legal, useful node. Enter it in hash table for cse.
6870 Blocks should be unique anyways. (Except the successor of start:
6871 is cse with the start block!) */
6872 if (get_opt_cse() && (get_irn_opcode(n) != iro_Block)) {
6874 n = identify_remember(o);
6880 } /* optimize_in_place_2 */
6883 * Wrapper for external use, set proper status bits after optimization.
6885 ir_node *optimize_in_place(ir_node *n)
6887 ir_graph *irg = get_irn_irg(n);
6888 /* Handle graph state */
6889 assert(get_irg_phase_state(irg) != phase_building);
6891 if (get_opt_global_cse())
6892 set_irg_pinned(irg, op_pin_state_floats);
6893 if (get_irg_outs_state(irg) == outs_consistent)
6894 set_irg_outs_inconsistent(irg);
6896 /* FIXME: Maybe we could also test whether optimizing the node can
6897 change the control graph. */
6898 set_irg_doms_inconsistent(irg);
6899 return optimize_in_place_2(n);
6900 } /* optimize_in_place */
6903 * Calculate a hash value of a Const node.
6905 static unsigned hash_Const(const ir_node *node)
6909 /* special value for const, as they only differ in their tarval. */
6910 h = HASH_PTR(node->attr.con.tarval);
6916 * Calculate a hash value of a SymConst node.
6918 static unsigned hash_SymConst(const ir_node *node)
6922 /* all others are pointers */
6923 h = HASH_PTR(node->attr.symc.sym.type_p);
6926 } /* hash_SymConst */
6929 * Set the default hash operation in an ir_op_ops.
6931 * @param code the opcode for the default operation
6932 * @param ops the operations initialized
6937 static ir_op_ops *firm_set_default_hash(ir_opcode code, ir_op_ops *ops)
6941 ops->hash = hash_##a; \
6944 /* hash function already set */
6945 if (ops->hash != NULL)
6952 /* use input/mode default hash if no function was given */
6953 ops->hash = firm_default_hash;
6961 * Sets the default operation for an ir_ops.
6963 ir_op_ops *firm_set_default_operations(ir_opcode code, ir_op_ops *ops)
6965 ops = firm_set_default_hash(code, ops);
6966 ops = firm_set_default_computed_value(code, ops);
6967 ops = firm_set_default_equivalent_node(code, ops);
6968 ops = firm_set_default_transform_node(code, ops);
6969 ops = firm_set_default_node_cmp_attr(code, ops);
6970 ops = firm_set_default_get_type(code, ops);
6971 ops = firm_set_default_get_type_attr(code, ops);
6972 ops = firm_set_default_get_entity_attr(code, ops);
6975 } /* firm_set_default_operations */