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
31 #include "irgraph_t.h"
32 #include "iredges_t.h"
39 #include "dbginfo_t.h"
40 #include "iropt_dbg.h"
45 #include "opt_confirms.h"
46 #include "opt_polymorphy.h"
51 #include "firm_types.h"
53 /* Make types visible to allow most efficient access */
57 * Returns the tarval of a Const node or tarval_bad for all other nodes.
59 static tarval *default_value_of(const ir_node *n)
62 return get_Const_tarval(n); /* might return tarval_bad */
67 value_of_func value_of_ptr = default_value_of;
69 /* * Set a new value_of function. */
70 void set_value_of_func(value_of_func func)
75 value_of_ptr = default_value_of;
79 * Return the value of a Constant.
81 static tarval *computed_value_Const(const ir_node *n)
83 return get_Const_tarval(n);
84 } /* computed_value_Const */
87 * Return the value of a 'sizeof', 'alignof' or 'offsetof' SymConst.
89 static tarval *computed_value_SymConst(const ir_node *n)
94 switch (get_SymConst_kind(n)) {
95 case symconst_type_size:
96 type = get_SymConst_type(n);
97 if (get_type_state(type) == layout_fixed)
98 return new_tarval_from_long(get_type_size_bytes(type), get_irn_mode(n));
100 case symconst_type_align:
101 type = get_SymConst_type(n);
102 if (get_type_state(type) == layout_fixed)
103 return new_tarval_from_long(get_type_alignment_bytes(type), get_irn_mode(n));
105 case symconst_ofs_ent:
106 ent = get_SymConst_entity(n);
107 type = get_entity_owner(ent);
108 if (get_type_state(type) == layout_fixed)
109 return new_tarval_from_long(get_entity_offset(ent), get_irn_mode(n));
115 } /* computed_value_SymConst */
118 * Return the value of an Add.
120 static tarval *computed_value_Add(const ir_node *n)
122 ir_node *a = get_Add_left(n);
123 ir_node *b = get_Add_right(n);
125 tarval *ta = value_of(a);
126 tarval *tb = value_of(b);
128 if ((ta != tarval_bad) && (tb != tarval_bad))
129 return tarval_add(ta, tb);
132 } /* computed_value_Add */
135 * Return the value of a Sub.
136 * Special case: a - a
138 static tarval *computed_value_Sub(const ir_node *n)
140 ir_mode *mode = get_irn_mode(n);
141 ir_node *a = get_Sub_left(n);
142 ir_node *b = get_Sub_right(n);
147 if (! mode_is_float(mode)) {
150 return get_mode_null(mode);
156 if ((ta != tarval_bad) && (tb != tarval_bad))
157 return tarval_sub(ta, tb, mode);
160 } /* computed_value_Sub */
163 * Return the value of a Carry.
164 * Special : a op 0, 0 op b
166 static tarval *computed_value_Carry(const ir_node *n)
168 ir_node *a = get_binop_left(n);
169 ir_node *b = get_binop_right(n);
170 ir_mode *m = get_irn_mode(n);
172 tarval *ta = value_of(a);
173 tarval *tb = value_of(b);
175 if ((ta != tarval_bad) && (tb != tarval_bad)) {
177 return tarval_carry() ? get_mode_one(m) : get_mode_null(m);
179 if (tarval_is_null(ta) || tarval_is_null(tb))
180 return get_mode_null(m);
183 } /* computed_value_Carry */
186 * Return the value of a Borrow.
189 static tarval *computed_value_Borrow(const ir_node *n)
191 ir_node *a = get_binop_left(n);
192 ir_node *b = get_binop_right(n);
193 ir_mode *m = get_irn_mode(n);
195 tarval *ta = value_of(a);
196 tarval *tb = value_of(b);
198 if ((ta != tarval_bad) && (tb != tarval_bad)) {
199 return tarval_cmp(ta, tb) == pn_Cmp_Lt ? get_mode_one(m) : get_mode_null(m);
200 } else if (tarval_is_null(ta)) {
201 return get_mode_null(m);
204 } /* computed_value_Borrow */
207 * Return the value of an unary Minus.
209 static tarval *computed_value_Minus(const ir_node *n)
211 ir_node *a = get_Minus_op(n);
212 tarval *ta = value_of(a);
214 if (ta != tarval_bad)
215 return tarval_neg(ta);
218 } /* computed_value_Minus */
221 * Return the value of a Mul.
223 static tarval *computed_value_Mul(const ir_node *n)
225 ir_node *a = get_Mul_left(n);
226 ir_node *b = get_Mul_right(n);
229 tarval *ta = value_of(a);
230 tarval *tb = value_of(b);
232 mode = get_irn_mode(n);
233 if (mode != get_irn_mode(a)) {
234 /* n * n = 2n bit multiplication */
235 ta = tarval_convert_to(ta, mode);
236 tb = tarval_convert_to(tb, mode);
239 if (ta != tarval_bad && tb != tarval_bad) {
240 return tarval_mul(ta, tb);
242 /* a * 0 != 0 if a == NaN or a == Inf */
243 if (!mode_is_float(mode)) {
244 /* a*0 = 0 or 0*b = 0 */
245 if (ta == get_mode_null(mode))
247 if (tb == get_mode_null(mode))
252 } /* computed_value_Mul */
255 * Return the value of an Abs.
257 static tarval *computed_value_Abs(const ir_node *n)
259 ir_node *a = get_Abs_op(n);
260 tarval *ta = value_of(a);
262 if (ta != tarval_bad)
263 return tarval_abs(ta);
266 } /* computed_value_Abs */
269 * Return the value of an And.
270 * Special case: a & 0, 0 & b
272 static tarval *computed_value_And(const ir_node *n)
274 ir_node *a = get_And_left(n);
275 ir_node *b = get_And_right(n);
277 tarval *ta = value_of(a);
278 tarval *tb = value_of(b);
280 if ((ta != tarval_bad) && (tb != tarval_bad)) {
281 return tarval_and (ta, tb);
283 if (tarval_is_null(ta)) return ta;
284 if (tarval_is_null(tb)) return tb;
287 } /* computed_value_And */
290 * Return the value of an Or.
291 * Special case: a | 1...1, 1...1 | b
293 static tarval *computed_value_Or(const ir_node *n)
295 ir_node *a = get_Or_left(n);
296 ir_node *b = get_Or_right(n);
298 tarval *ta = value_of(a);
299 tarval *tb = value_of(b);
301 if ((ta != tarval_bad) && (tb != tarval_bad)) {
302 return tarval_or (ta, tb);
304 if (tarval_is_all_one(ta)) return ta;
305 if (tarval_is_all_one(tb)) return tb;
308 } /* computed_value_Or */
311 * Return the value of an Eor.
313 static tarval *computed_value_Eor(const ir_node *n)
315 ir_node *a = get_Eor_left(n);
316 ir_node *b = get_Eor_right(n);
321 return get_mode_null(get_irn_mode(n));
326 if ((ta != tarval_bad) && (tb != tarval_bad)) {
327 return tarval_eor(ta, tb);
330 } /* computed_value_Eor */
333 * Return the value of a Not.
335 static tarval *computed_value_Not(const ir_node *n)
337 ir_node *a = get_Not_op(n);
338 tarval *ta = value_of(a);
340 if (ta != tarval_bad)
341 return tarval_not(ta);
344 } /* computed_value_Not */
347 * Return the value of a Shl.
349 static tarval *computed_value_Shl(const ir_node *n)
351 ir_node *a = get_Shl_left(n);
352 ir_node *b = get_Shl_right(n);
354 tarval *ta = value_of(a);
355 tarval *tb = value_of(b);
357 if ((ta != tarval_bad) && (tb != tarval_bad)) {
358 return tarval_shl(ta, tb);
361 } /* computed_value_Shl */
364 * Return the value of a Shr.
366 static tarval *computed_value_Shr(const ir_node *n)
368 ir_node *a = get_Shr_left(n);
369 ir_node *b = get_Shr_right(n);
371 tarval *ta = value_of(a);
372 tarval *tb = value_of(b);
374 if ((ta != tarval_bad) && (tb != tarval_bad)) {
375 return tarval_shr(ta, tb);
378 } /* computed_value_Shr */
381 * Return the value of a Shrs.
383 static tarval *computed_value_Shrs(const ir_node *n)
385 ir_node *a = get_Shrs_left(n);
386 ir_node *b = get_Shrs_right(n);
388 tarval *ta = value_of(a);
389 tarval *tb = value_of(b);
391 if ((ta != tarval_bad) && (tb != tarval_bad)) {
392 return tarval_shrs(ta, tb);
395 } /* computed_value_Shrs */
398 * Return the value of a Rotl.
400 static tarval *computed_value_Rotl(const ir_node *n)
402 ir_node *a = get_Rotl_left(n);
403 ir_node *b = get_Rotl_right(n);
405 tarval *ta = value_of(a);
406 tarval *tb = value_of(b);
408 if ((ta != tarval_bad) && (tb != tarval_bad)) {
409 return tarval_rotl(ta, tb);
412 } /* computed_value_Rotl */
415 * Return the value of a Conv.
417 static tarval *computed_value_Conv(const ir_node *n)
419 ir_node *a = get_Conv_op(n);
420 tarval *ta = value_of(a);
422 if (ta != tarval_bad)
423 return tarval_convert_to(ta, get_irn_mode(n));
426 } /* computed_value_Conv */
429 * Calculate the value of a Mux: can be evaluated, if the
430 * sel and the right input are known.
432 static tarval *computed_value_Mux(const ir_node *n)
434 ir_node *sel = get_Mux_sel(n);
435 tarval *ts = value_of(sel);
437 if (ts == get_tarval_b_true()) {
438 ir_node *v = get_Mux_true(n);
441 else if (ts == get_tarval_b_false()) {
442 ir_node *v = get_Mux_false(n);
446 } /* computed_value_Mux */
449 * Calculate the value of a Confirm: can be evaluated,
450 * if it has the form Confirm(x, '=', Const).
452 static tarval *computed_value_Confirm(const ir_node *n)
455 * Beware: we might produce Phi(Confirm(x == true), Confirm(x == false)).
456 * Do NOT optimize them away (jump threading wants them), so wait until
457 * remove_confirm is activated.
459 if (get_opt_remove_confirm()) {
460 if (get_Confirm_cmp(n) == pn_Cmp_Eq) {
461 tarval *tv = value_of(get_Confirm_bound(n));
462 if (tv != tarval_bad)
466 return value_of(get_Confirm_value(n));
467 } /* computed_value_Confirm */
470 * Return the value of a Proj(Cmp).
472 * This performs a first step of unreachable code elimination.
473 * Proj can not be computed, but folding a Cmp above the Proj here is
474 * not as wasteful as folding a Cmp into a Tuple of 16 Consts of which
476 * There are several case where we can evaluate a Cmp node, see later.
478 static tarval *computed_value_Proj_Cmp(const ir_node *n)
480 ir_node *cmp = get_Proj_pred(n);
481 ir_node *left = get_Cmp_left(cmp);
482 ir_node *right = get_Cmp_right(cmp);
483 long pn_cmp = get_Proj_proj(n);
484 ir_mode *mode = get_irn_mode(left);
488 * BEWARE: a == a is NOT always True for floating Point values, as
489 * NaN != NaN is defined, so we must check this here.
491 if (left == right && (!mode_is_float(mode) || pn_cmp == pn_Cmp_Lt || pn_cmp == pn_Cmp_Gt)) {
492 /* This is a trick with the bits used for encoding the Cmp
493 Proj numbers, the following statement is not the same:
494 return new_tarval_from_long(pn_cmp == pn_Cmp_Eq, mode_b) */
495 return new_tarval_from_long(pn_cmp & pn_Cmp_Eq, mode_b);
497 tv_l = value_of(left);
498 tv_r = value_of(right);
500 if ((tv_l != tarval_bad) && (tv_r != tarval_bad)) {
502 * The predecessors of Cmp are target values. We can evaluate
505 pn_Cmp flags = tarval_cmp(tv_l, tv_r);
506 if (flags != pn_Cmp_False) {
507 return new_tarval_from_long (pn_cmp & flags, mode_b);
509 } else if (mode_is_int(mode)) {
510 /* for integer values, we can check against MIN/MAX */
513 if (tv_l == get_mode_min(mode)) {
514 /* MIN <=/> x. This results in true/false. */
515 if (pn_cmp == pn_Cmp_Le)
516 return tarval_b_true;
517 else if (pn_cmp == pn_Cmp_Gt)
518 return tarval_b_false;
519 } else if (tv_r == get_mode_min(mode)) {
520 /* x >=/< MIN. This results in true/false. */
521 if (pn_cmp == pn_Cmp_Ge)
522 return tarval_b_true;
523 else if (pn_cmp == pn_Cmp_Lt)
524 return tarval_b_false;
525 } else if (tv_l == get_mode_max(mode)) {
526 /* MAX >=/< x. This results in true/false. */
527 if (pn_cmp == pn_Cmp_Ge)
528 return tarval_b_true;
529 else if (pn_cmp == pn_Cmp_Lt)
530 return tarval_b_false;
531 } else if (tv_r == get_mode_max(mode)) {
532 /* x <=/> MAX. This results in true/false. */
533 if (pn_cmp == pn_Cmp_Le)
534 return tarval_b_true;
535 else if (pn_cmp == pn_Cmp_Gt)
536 return tarval_b_false;
539 cmp_result = vrp_cmp(left, right);
540 if (cmp_result != pn_Cmp_False) {
541 if (cmp_result == pn_Cmp_Lg) {
542 if (pn_cmp == pn_Cmp_Eq) {
543 return tarval_b_false;
544 } else if (pn_cmp == pn_Cmp_Lg) {
545 return tarval_b_true;
548 return new_tarval_from_long(cmp_result & pn_cmp, mode_b);
551 } else if (mode_is_reference(mode)) {
552 /* pointer compare */
553 ir_node *s_l = skip_Proj(left);
554 ir_node *s_r = skip_Proj(right);
556 if ((is_Alloc(s_l) && tarval_is_null(tv_r)) ||
557 (tarval_is_null(tv_l) && is_Alloc(s_r))) {
559 * The predecessors are Allocs and (void*)(0) constants. In Firm Allocs never
560 * return NULL, they raise an exception. Therefore we can predict
563 return new_tarval_from_long(pn_cmp & pn_Cmp_Lg, mode_b);
566 return computed_value_Cmp_Confirm(cmp, left, right, pn_cmp);
567 } /* computed_value_Proj_Cmp */
570 * Return the value of a floating point Quot.
572 static tarval *do_computed_value_Quot(const ir_node *a, const ir_node *b)
574 tarval *ta = value_of(a);
575 tarval *tb = value_of(b);
577 /* cannot optimize 0 / b = 0 because of NaN */
578 if (ta != tarval_bad && tb != tarval_bad)
579 return tarval_quo(ta, tb);
581 } /* do_computed_value_Quot */
584 * Calculate the value of an integer Div of two nodes.
585 * Special case: 0 / b
587 static tarval *do_computed_value_Div(const ir_node *a, const ir_node *b)
589 tarval *ta = value_of(a);
591 const ir_node *dummy;
593 /* Compute c1 / c2 or 0 / a, a != 0 */
594 if (tarval_is_null(ta) && value_not_zero(b, &dummy))
595 return ta; /* 0 / b == 0 */
597 if (ta != tarval_bad && tb != tarval_bad)
598 return tarval_div(ta, tb);
600 } /* do_computed_value_Div */
603 * Calculate the value of an integer Mod of two nodes.
604 * Special case: a % 1
606 static tarval *do_computed_value_Mod(const ir_node *a, const ir_node *b)
608 tarval *ta = value_of(a);
609 tarval *tb = value_of(b);
611 /* Compute a % 1 or c1 % c2 */
612 if (tarval_is_one(tb))
613 return get_mode_null(get_irn_mode(a));
614 if (ta != tarval_bad && tb != tarval_bad)
615 return tarval_mod(ta, tb);
617 } /* do_computed_value_Mod */
620 * Return the value of a Proj(DivMod).
622 static tarval *computed_value_Proj_DivMod(const ir_node *n)
624 long proj_nr = get_Proj_proj(n);
626 /* compute either the Div or the Mod part */
627 if (proj_nr == pn_DivMod_res_div) {
628 const ir_node *a = get_Proj_pred(n);
629 return do_computed_value_Div(get_DivMod_left(a), get_DivMod_right(a));
630 } else if (proj_nr == pn_DivMod_res_mod) {
631 const ir_node *a = get_Proj_pred(n);
632 return do_computed_value_Mod(get_DivMod_left(a), get_DivMod_right(a));
635 } /* computed_value_Proj_DivMod */
638 * Return the value of a Proj(Div).
640 static tarval *computed_value_Proj_Div(const ir_node *n)
642 long proj_nr = get_Proj_proj(n);
644 if (proj_nr == pn_Div_res) {
645 const ir_node *a = get_Proj_pred(n);
646 return do_computed_value_Div(get_Div_left(a), get_Div_right(a));
649 } /* computed_value_Proj_Div */
652 * Return the value of a Proj(Mod).
654 static tarval *computed_value_Proj_Mod(const ir_node *n)
656 long proj_nr = get_Proj_proj(n);
658 if (proj_nr == pn_Mod_res) {
659 const ir_node *a = get_Proj_pred(n);
660 return do_computed_value_Mod(get_Mod_left(a), get_Mod_right(a));
663 } /* computed_value_Proj_Mod */
666 * Return the value of a Proj(Quot).
668 static tarval *computed_value_Proj_Quot(const ir_node *n)
670 long proj_nr = get_Proj_proj(n);
672 if (proj_nr == pn_Quot_res) {
673 const ir_node *a = get_Proj_pred(n);
674 return do_computed_value_Quot(get_Quot_left(a), get_Quot_right(a));
677 } /* computed_value_Proj_Quot */
680 * Return the value of a Proj.
682 static tarval *computed_value_Proj(const ir_node *proj)
684 ir_node *n = get_Proj_pred(proj);
686 if (n->op->ops.computed_value_Proj != NULL)
687 return n->op->ops.computed_value_Proj(proj);
689 } /* computed_value_Proj */
692 * If the parameter n can be computed, return its value, else tarval_bad.
693 * Performs constant folding.
695 * @param n The node this should be evaluated
697 tarval *computed_value(const ir_node *n)
699 if (mode_is_int(get_irn_mode(n)) && n->vrp.valid && tarval_is_all_one(
700 tarval_or(n->vrp.bits_set, n->vrp.bits_not_set))) {
701 return n->vrp.bits_set;
703 if (n->op->ops.computed_value)
704 return n->op->ops.computed_value(n);
706 } /* computed_value */
709 * Set the default computed_value evaluator in an ir_op_ops.
711 * @param code the opcode for the default operation
712 * @param ops the operations initialized
717 static ir_op_ops *firm_set_default_computed_value(ir_opcode code, ir_op_ops *ops)
721 ops->computed_value = computed_value_##a; \
723 #define CASE_PROJ(a) \
725 ops->computed_value_Proj = computed_value_Proj_##a; \
762 } /* firm_set_default_computed_value */
765 * Returns a equivalent block for another block.
766 * If the block has only one predecessor, this is
767 * the equivalent one. If the only predecessor of a block is
768 * the block itself, this is a dead block.
770 * If both predecessors of a block are the branches of a binary
771 * Cond, the equivalent block is Cond's block.
773 * If all predecessors of a block are bad or lies in a dead
774 * block, the current block is dead as well.
776 * Note, that blocks are NEVER turned into Bad's, instead
777 * the dead_block flag is set. So, never test for is_Bad(block),
778 * always use is_dead_Block(block).
780 static ir_node *equivalent_node_Block(ir_node *n)
785 /* don't optimize dead or labeled blocks */
786 if (is_Block_dead(n) || has_Block_entity(n))
789 n_preds = get_Block_n_cfgpreds(n);
791 /* The Block constructor does not call optimize, but mature_immBlock()
792 calls the optimization. */
793 assert(get_Block_matured(n));
795 /* Straightening: a single entry Block following a single exit Block
796 can be merged, if it is not the Start block. */
797 /* !!! Beware, all Phi-nodes of n must have been optimized away.
798 This should be true, as the block is matured before optimize is called.
799 But what about Phi-cycles with the Phi0/Id that could not be resolved?
800 Remaining Phi nodes are just Ids. */
802 ir_node *pred = skip_Proj(get_Block_cfgpred(n, 0));
805 ir_node *predblock = get_nodes_block(pred);
806 if (predblock == oldn) {
807 /* Jmp jumps into the block it is in -- deal self cycle. */
808 n = set_Block_dead(n);
809 DBG_OPT_DEAD_BLOCK(oldn, n);
810 } else if (get_opt_control_flow_straightening()) {
812 DBG_OPT_STG(oldn, n);
814 } else if (is_Cond(pred)) {
815 ir_node *predblock = get_nodes_block(pred);
816 if (predblock == oldn) {
817 /* Jmp jumps into the block it is in -- deal self cycle. */
818 n = set_Block_dead(n);
819 DBG_OPT_DEAD_BLOCK(oldn, n);
822 } else if ((n_preds == 2) &&
823 (get_opt_control_flow_weak_simplification())) {
824 /* Test whether Cond jumps twice to this block
825 * The more general case which more than 2 predecessors is handles
826 * in optimize_cf(), we handle only this special case for speed here.
828 ir_node *a = get_Block_cfgpred(n, 0);
829 ir_node *b = get_Block_cfgpred(n, 1);
831 if (is_Proj(a) && is_Proj(b)) {
832 ir_node *cond = get_Proj_pred(a);
834 if (cond == get_Proj_pred(b) && is_Cond(cond) &&
835 get_irn_mode(get_Cond_selector(cond)) == mode_b) {
836 /* Also a single entry Block following a single exit Block. Phis have
837 twice the same operand and will be optimized away. */
838 n = get_nodes_block(cond);
839 DBG_OPT_IFSIM1(oldn, a, b, n);
842 } else if (get_opt_unreachable_code() &&
843 (n != get_irg_start_block(current_ir_graph)) &&
844 (n != get_irg_end_block(current_ir_graph)) ) {
847 /* If all inputs are dead, this block is dead too, except if it is
848 the start or end block. This is one step of unreachable code
850 for (i = get_Block_n_cfgpreds(n) - 1; i >= 0; --i) {
851 ir_node *pred = get_Block_cfgpred(n, i);
854 if (is_Bad(pred)) continue;
855 pred_blk = get_nodes_block(skip_Proj(pred));
857 if (is_Block_dead(pred_blk)) continue;
860 /* really found a living input */
865 n = set_Block_dead(n);
866 DBG_OPT_DEAD_BLOCK(oldn, n);
871 } /* equivalent_node_Block */
874 * Returns a equivalent node for a Jmp, a Bad :-)
875 * Of course this only happens if the Block of the Jmp is dead.
877 static ir_node *equivalent_node_Jmp(ir_node *n)
881 /* unreachable code elimination */
882 if (is_Block_dead(get_nodes_block(n))) {
883 n = get_irg_bad(current_ir_graph);
884 DBG_OPT_DEAD_BLOCK(oldn, n);
887 } /* equivalent_node_Jmp */
889 /** Raise is handled in the same way as Jmp. */
890 #define equivalent_node_Raise equivalent_node_Jmp
893 /* We do not evaluate Cond here as we replace it by a new node, a Jmp.
894 See transform_node_Proj_Cond(). */
897 * Optimize operations that are commutative and have neutral 0,
898 * so a op 0 = 0 op a = a.
900 static ir_node *equivalent_node_neutral_zero(ir_node *n)
904 ir_node *a = get_binop_left(n);
905 ir_node *b = get_binop_right(n);
910 /* After running compute_node there is only one constant predecessor.
911 Find this predecessors value and remember the other node: */
912 if ((tv = value_of(a)) != tarval_bad) {
914 } else if ((tv = value_of(b)) != tarval_bad) {
919 /* If this predecessors constant value is zero, the operation is
920 * unnecessary. Remove it.
922 * Beware: If n is a Add, the mode of on and n might be different
923 * which happens in this rare construction: NULL + 3.
924 * Then, a Conv would be needed which we cannot include here.
926 if (tarval_is_null(tv) && get_irn_mode(on) == get_irn_mode(n)) {
929 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_0);
933 } /* equivalent_node_neutral_zero */
936 * Eor is commutative and has neutral 0.
938 static ir_node *equivalent_node_Eor(ir_node *n)
944 n = equivalent_node_neutral_zero(n);
945 if (n != oldn) return n;
948 b = get_Eor_right(n);
951 ir_node *aa = get_Eor_left(a);
952 ir_node *ab = get_Eor_right(a);
955 /* (a ^ b) ^ a -> b */
957 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_EOR_A_B_A);
959 } else if (ab == b) {
960 /* (a ^ b) ^ b -> a */
962 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_EOR_A_B_A);
967 ir_node *ba = get_Eor_left(b);
968 ir_node *bb = get_Eor_right(b);
971 /* a ^ (a ^ b) -> b */
973 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_EOR_A_B_A);
975 } else if (bb == a) {
976 /* a ^ (b ^ a) -> b */
978 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_EOR_A_B_A);
986 * Optimize a - 0 and (a - x) + x (for modes with wrap-around).
988 * The second one looks strange, but this construct
989 * is used heavily in the LCC sources :-).
991 * Beware: The Mode of an Add may be different than the mode of its
992 * predecessors, so we could not return a predecessors in all cases.
994 static ir_node *equivalent_node_Add(ir_node *n)
997 ir_node *left, *right;
998 ir_mode *mode = get_irn_mode(n);
1000 n = equivalent_node_neutral_zero(n);
1004 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
1005 if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
1008 left = get_Add_left(n);
1009 right = get_Add_right(n);
1012 if (get_Sub_right(left) == right) {
1015 n = get_Sub_left(left);
1016 if (mode == get_irn_mode(n)) {
1017 DBG_OPT_ALGSIM1(oldn, left, right, n, FS_OPT_ADD_SUB);
1022 if (is_Sub(right)) {
1023 if (get_Sub_right(right) == left) {
1026 n = get_Sub_left(right);
1027 if (mode == get_irn_mode(n)) {
1028 DBG_OPT_ALGSIM1(oldn, left, right, n, FS_OPT_ADD_SUB);
1034 } /* equivalent_node_Add */
1037 * optimize operations that are not commutative but have neutral 0 on left,
1040 static ir_node *equivalent_node_left_zero(ir_node *n)
1044 ir_node *a = get_binop_left(n);
1045 ir_node *b = get_binop_right(n);
1046 tarval *tb = value_of(b);
1048 if (tarval_is_null(tb)) {
1051 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_0);
1054 } /* equivalent_node_left_zero */
1056 #define equivalent_node_Shl equivalent_node_left_zero
1057 #define equivalent_node_Shr equivalent_node_left_zero
1058 #define equivalent_node_Shrs equivalent_node_left_zero
1059 #define equivalent_node_Rotl equivalent_node_left_zero
1062 * Optimize a - 0 and (a + x) - x (for modes with wrap-around).
1064 * The second one looks strange, but this construct
1065 * is used heavily in the LCC sources :-).
1067 * Beware: The Mode of a Sub may be different than the mode of its
1068 * predecessors, so we could not return a predecessors in all cases.
1070 static ir_node *equivalent_node_Sub(ir_node *n)
1074 ir_mode *mode = get_irn_mode(n);
1077 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
1078 if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
1081 b = get_Sub_right(n);
1084 /* Beware: modes might be different */
1085 if (tarval_is_null(tb)) {
1086 ir_node *a = get_Sub_left(n);
1087 if (mode == get_irn_mode(a)) {
1090 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_0);
1094 } /* equivalent_node_Sub */
1098 * Optimize an "self-inverse unary op", ie op(op(n)) = n.
1101 * -(-a) == a, but might overflow two times.
1102 * We handle it anyway here but the better way would be a
1103 * flag. This would be needed for Pascal for instance.
1105 static ir_node *equivalent_node_idempotent_unop(ir_node *n)
1108 ir_node *pred = get_unop_op(n);
1110 /* optimize symmetric unop */
1111 if (get_irn_op(pred) == get_irn_op(n)) {
1112 n = get_unop_op(pred);
1113 DBG_OPT_ALGSIM2(oldn, pred, n, FS_OPT_IDEM_UNARY);
1116 } /* equivalent_node_idempotent_unop */
1118 /** Optimize Not(Not(x)) == x. */
1119 #define equivalent_node_Not equivalent_node_idempotent_unop
1121 /** -(-x) == x ??? Is this possible or can --x raise an
1122 out of bounds exception if min =! max? */
1123 #define equivalent_node_Minus equivalent_node_idempotent_unop
1126 * Optimize a * 1 = 1 * a = a.
1128 static ir_node *equivalent_node_Mul(ir_node *n)
1131 ir_node *a = get_Mul_left(n);
1133 /* we can handle here only the n * n = n bit cases */
1134 if (get_irn_mode(n) == get_irn_mode(a)) {
1135 ir_node *b = get_Mul_right(n);
1139 * Mul is commutative and has again an other neutral element.
1140 * Constants are place right, so check this case first.
1143 if (tarval_is_one(tv)) {
1145 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
1148 if (tarval_is_one(tv)) {
1150 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
1155 } /* equivalent_node_Mul */
1158 * Use algebraic simplification a | a = a | 0 = 0 | a = a.
1160 static ir_node *equivalent_node_Or(ir_node *n)
1164 ir_node *a = get_Or_left(n);
1165 ir_node *b = get_Or_right(n);
1169 n = a; /* Or has it's own neutral element */
1170 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_OR);
1173 /* constants are cormalized to right, check this site first */
1175 if (tarval_is_null(tv)) {
1177 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_OR);
1181 if (tarval_is_null(tv)) {
1183 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_OR);
1188 } /* equivalent_node_Or */
1191 * Optimize a & 0b1...1 = 0b1...1 & a = a & a = (a|X) & a = a.
1193 static ir_node *equivalent_node_And(ir_node *n)
1197 ir_node *a = get_And_left(n);
1198 ir_node *b = get_And_right(n);
1202 n = a; /* And has it's own neutral element */
1203 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_AND);
1206 /* constants are normalized to right, check this site first */
1208 if (tarval_is_all_one(tv)) {
1210 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1213 if (tv != get_tarval_bad()) {
1214 ir_mode *mode = get_irn_mode(n);
1215 if (!mode_is_signed(mode) && is_Conv(a)) {
1216 ir_node *convop = get_Conv_op(a);
1217 ir_mode *convopmode = get_irn_mode(convop);
1218 if (!mode_is_signed(convopmode)) {
1219 if (tarval_is_all_one(tarval_convert_to(tv, convopmode))) {
1220 /* Conv(X) & all_one(mode(X)) = Conv(X) */
1222 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1229 if (tarval_is_all_one(tv)) {
1231 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1235 if (b == get_Or_left(a) || b == get_Or_right(a)) {
1238 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1243 if (a == get_Or_left(b) || a == get_Or_right(b)) {
1246 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1251 } /* equivalent_node_And */
1254 * Try to remove useless Conv's:
1256 static ir_node *equivalent_node_Conv(ir_node *n)
1259 ir_node *a = get_Conv_op(n);
1261 ir_mode *n_mode = get_irn_mode(n);
1262 ir_mode *a_mode = get_irn_mode(a);
1265 if (n_mode == a_mode) { /* No Conv necessary */
1266 if (get_Conv_strict(n)) {
1269 /* neither Minus nor Abs nor Confirm change the precision,
1270 so we can "look-through" */
1273 p = get_Minus_op(p);
1274 } else if (is_Abs(p)) {
1276 } else if (is_Confirm(p)) {
1277 p = get_Confirm_value(p);
1283 if (is_Conv(p) && get_Conv_strict(p)) {
1284 /* we known already, that a_mode == n_mode, and neither
1285 Abs nor Minus change the mode, so the second Conv
1287 assert(get_irn_mode(p) == n_mode);
1289 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CONV);
1293 ir_node *pred = get_Proj_pred(p);
1294 if (is_Load(pred)) {
1295 /* Loads always return with the exact precision of n_mode */
1296 assert(get_Load_mode(pred) == n_mode);
1298 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CONV);
1301 if (is_Proj(pred) && get_Proj_proj(pred) == pn_Start_T_args) {
1302 pred = get_Proj_pred(pred);
1303 if (is_Start(pred)) {
1304 /* Arguments always return with the exact precision,
1305 as strictConv's are place before Call -- if the
1306 caller was compiled with the same setting.
1307 Otherwise, the semantics is probably still right. */
1308 assert(get_irn_mode(p) == n_mode);
1310 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CONV);
1316 /* special case: the immediate predecessor is also a Conv */
1317 if (! get_Conv_strict(a)) {
1318 /* first one is not strict, kick it */
1320 a_mode = get_irn_mode(a);
1324 /* else both are strict conv, second is superfluous */
1326 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CONV);
1331 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CONV);
1334 } else if (is_Conv(a)) { /* Conv(Conv(b)) */
1335 ir_node *b = get_Conv_op(a);
1336 ir_mode *b_mode = get_irn_mode(b);
1338 if (get_Conv_strict(n) && get_Conv_strict(a)) {
1339 /* both are strict conv */
1340 if (smaller_mode(a_mode, n_mode)) {
1341 /* both are strict, but the first is smaller, so
1342 the second cannot remove more precision, remove the
1344 set_Conv_strict(n, 0);
1347 if (n_mode == b_mode) {
1348 if (! get_Conv_strict(n) && ! get_Conv_strict(a)) {
1349 if (n_mode == mode_b) {
1350 n = b; /* Convb(Conv*(xxxb(...))) == xxxb(...) */
1351 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
1353 } else if (get_mode_arithmetic(n_mode) == get_mode_arithmetic(a_mode)) {
1354 if (values_in_mode(b_mode, a_mode)) {
1355 n = b; /* ConvS(ConvL(xxxS(...))) == xxxS(...) */
1356 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
1361 if (mode_is_int(n_mode) && get_mode_arithmetic(a_mode) == irma_ieee754) {
1362 /* ConvI(ConvF(I)) -> I, iff float mantissa >= int mode */
1363 unsigned int_mantissa = get_mode_size_bits(n_mode) - (mode_is_signed(n_mode) ? 1 : 0);
1364 unsigned float_mantissa = tarval_ieee754_get_mantissa_size(a_mode);
1366 if (float_mantissa >= int_mantissa) {
1368 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
1373 if (smaller_mode(b_mode, a_mode)) {
1374 if (get_Conv_strict(n))
1375 set_Conv_strict(b, 1);
1376 n = b; /* ConvA(ConvB(ConvA(...))) == ConvA(...) */
1377 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
1384 } /* equivalent_node_Conv */
1387 * A Cast may be removed if the type of the previous node
1388 * is already the type of the Cast.
1390 static ir_node *equivalent_node_Cast(ir_node *n)
1393 ir_node *pred = get_Cast_op(n);
1395 if (get_irn_type(pred) == get_Cast_type(n)) {
1397 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CAST);
1400 } /* equivalent_node_Cast */
1403 * - fold Phi-nodes, iff they have only one predecessor except
1406 static ir_node *equivalent_node_Phi(ir_node *n)
1412 ir_node *first_val = NULL; /* to shutup gcc */
1414 if (!get_opt_normalize()) return n;
1416 n_preds = get_Phi_n_preds(n);
1418 block = get_nodes_block(n);
1419 if (is_Block_dead(block)) /* Control dead */
1420 return get_irg_bad(current_ir_graph);
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 /* A totally Bad or self-referencing Phi (we didn't break the above loop) */
1446 return get_irg_bad(current_ir_graph);
1449 /* search for rest of inputs, determine if any of these
1450 are non-self-referencing */
1451 while (++i < n_preds) {
1452 ir_node *scnd_val = get_Phi_pred(n, i);
1453 if ( (scnd_val != n)
1454 && (scnd_val != first_val)
1457 && (! is_Bad(get_Block_cfgpred(block, i)))
1465 /* Fold, if no multiple distinct non-self-referencing inputs */
1467 DBG_OPT_PHI(oldn, n);
1470 } /* equivalent_node_Phi */
1473 * Several optimizations:
1474 * - fold Sync-nodes, iff they have only one predecessor except
1477 static ir_node *equivalent_node_Sync(ir_node *n)
1479 int arity = get_Sync_n_preds(n);
1482 for (i = 0; i < arity;) {
1483 ir_node *pred = get_Sync_pred(n, i);
1486 /* Remove Bad predecessors */
1493 /* Remove duplicate predecessors */
1499 if (get_Sync_pred(n, j) == pred) {
1507 if (arity == 0) return get_irg_bad(current_ir_graph);
1508 if (arity == 1) return get_Sync_pred(n, 0);
1510 } /* equivalent_node_Sync */
1513 * Optimize Proj(Tuple).
1515 static ir_node *equivalent_node_Proj_Tuple(ir_node *proj)
1517 ir_node *oldn = proj;
1518 ir_node *tuple = get_Proj_pred(proj);
1520 /* Remove the Tuple/Proj combination. */
1521 proj = get_Tuple_pred(tuple, get_Proj_proj(proj));
1522 DBG_OPT_TUPLE(oldn, tuple, proj);
1525 } /* equivalent_node_Proj_Tuple */
1528 * Optimize a / 1 = a.
1530 static ir_node *equivalent_node_Proj_Div(ir_node *proj)
1532 ir_node *oldn = proj;
1533 ir_node *div = get_Proj_pred(proj);
1534 ir_node *b = get_Div_right(div);
1535 tarval *tb = value_of(b);
1537 /* Div is not commutative. */
1538 if (tarval_is_one(tb)) { /* div(x, 1) == x */
1539 switch (get_Proj_proj(proj)) {
1541 proj = get_Div_mem(div);
1542 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NEUTRAL_1);
1546 proj = get_Div_left(div);
1547 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NEUTRAL_1);
1551 /* we cannot replace the exception Proj's here, this is done in
1552 transform_node_Proj_Div() */
1557 } /* equivalent_node_Proj_Div */
1560 * Optimize a / 1.0 = a.
1562 static ir_node *equivalent_node_Proj_Quot(ir_node *proj)
1564 ir_node *oldn = proj;
1565 ir_node *quot = get_Proj_pred(proj);
1566 ir_node *b = get_Quot_right(quot);
1567 tarval *tb = value_of(b);
1569 /* Div is not commutative. */
1570 if (tarval_is_one(tb)) { /* Quot(x, 1) == x */
1571 switch (get_Proj_proj(proj)) {
1573 proj = get_Quot_mem(quot);
1574 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NEUTRAL_1);
1578 proj = get_Quot_left(quot);
1579 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NEUTRAL_1);
1583 /* we cannot replace the exception Proj's here, this is done in
1584 transform_node_Proj_Quot() */
1589 } /* equivalent_node_Proj_Quot */
1592 * Optimize a / 1 = a.
1594 static ir_node *equivalent_node_Proj_DivMod(ir_node *proj)
1596 ir_node *oldn = proj;
1597 ir_node *divmod = get_Proj_pred(proj);
1598 ir_node *b = get_DivMod_right(divmod);
1599 tarval *tb = value_of(b);
1601 /* Div is not commutative. */
1602 if (tarval_is_one(tb)) { /* div(x, 1) == x */
1603 switch (get_Proj_proj(proj)) {
1605 proj = get_DivMod_mem(divmod);
1606 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NEUTRAL_1);
1609 case pn_DivMod_res_div:
1610 proj = get_DivMod_left(divmod);
1611 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NEUTRAL_1);
1615 /* we cannot replace the exception Proj's here, this is done in
1616 transform_node_Proj_DivMod().
1617 Note further that the pn_DivMod_res_div case is handled in
1618 computed_value_Proj(). */
1623 } /* equivalent_node_Proj_DivMod */
1626 * Optimize CopyB(mem, x, x) into a Nop.
1628 static ir_node *equivalent_node_Proj_CopyB(ir_node *proj)
1630 ir_node *oldn = proj;
1631 ir_node *copyb = get_Proj_pred(proj);
1632 ir_node *a = get_CopyB_dst(copyb);
1633 ir_node *b = get_CopyB_src(copyb);
1636 /* Turn CopyB into a tuple (mem, jmp, bad, bad) */
1637 switch (get_Proj_proj(proj)) {
1638 case pn_CopyB_M_regular:
1639 proj = get_CopyB_mem(copyb);
1640 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NOP);
1643 case pn_CopyB_X_except:
1644 DBG_OPT_EXC_REM(proj);
1645 proj = get_irg_bad(current_ir_graph);
1650 } /* equivalent_node_Proj_CopyB */
1653 * Optimize Bounds(idx, idx, upper) into idx.
1655 static ir_node *equivalent_node_Proj_Bound(ir_node *proj)
1657 ir_node *oldn = proj;
1658 ir_node *bound = get_Proj_pred(proj);
1659 ir_node *idx = get_Bound_index(bound);
1660 ir_node *pred = skip_Proj(idx);
1663 if (idx == get_Bound_lower(bound))
1665 else if (is_Bound(pred)) {
1667 * idx was Bounds checked in the same MacroBlock previously,
1668 * it is still valid if lower <= pred_lower && pred_upper <= upper.
1670 ir_node *lower = get_Bound_lower(bound);
1671 ir_node *upper = get_Bound_upper(bound);
1672 if (get_Bound_lower(pred) == lower &&
1673 get_Bound_upper(pred) == upper &&
1674 get_irn_MacroBlock(bound) == get_irn_MacroBlock(pred)) {
1676 * One could expect that we simply return the previous
1677 * Bound here. However, this would be wrong, as we could
1678 * add an exception Proj to a new location then.
1679 * So, we must turn in into a tuple.
1685 /* Turn Bound into a tuple (mem, jmp, bad, idx) */
1686 switch (get_Proj_proj(proj)) {
1688 DBG_OPT_EXC_REM(proj);
1689 proj = get_Bound_mem(bound);
1691 case pn_Bound_X_except:
1692 DBG_OPT_EXC_REM(proj);
1693 proj = get_irg_bad(current_ir_graph);
1697 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NOP);
1700 /* cannot optimize pn_Bound_X_regular, handled in transform ... */
1705 } /* equivalent_node_Proj_Bound */
1708 * Optimize an Exception Proj(Load) with a non-null address.
1710 static ir_node *equivalent_node_Proj_Load(ir_node *proj)
1712 if (get_opt_ldst_only_null_ptr_exceptions()) {
1713 if (get_irn_mode(proj) == mode_X) {
1714 ir_node *load = get_Proj_pred(proj);
1716 /* get the Load address */
1717 const ir_node *addr = get_Load_ptr(load);
1718 const ir_node *confirm;
1720 if (value_not_null(addr, &confirm)) {
1721 if (get_Proj_proj(proj) == pn_Load_X_except) {
1722 DBG_OPT_EXC_REM(proj);
1723 return get_irg_bad(current_ir_graph);
1729 } /* equivalent_node_Proj_Load */
1732 * Optimize an Exception Proj(Store) with a non-null address.
1734 static ir_node *equivalent_node_Proj_Store(ir_node *proj)
1736 if (get_opt_ldst_only_null_ptr_exceptions()) {
1737 if (get_irn_mode(proj) == mode_X) {
1738 ir_node *store = get_Proj_pred(proj);
1740 /* get the load/store address */
1741 const ir_node *addr = get_Store_ptr(store);
1742 const ir_node *confirm;
1744 if (value_not_null(addr, &confirm)) {
1745 if (get_Proj_proj(proj) == pn_Store_X_except) {
1746 DBG_OPT_EXC_REM(proj);
1747 return get_irg_bad(current_ir_graph);
1753 } /* equivalent_node_Proj_Store */
1756 * Does all optimizations on nodes that must be done on it's Proj's
1757 * because of creating new nodes.
1759 static ir_node *equivalent_node_Proj(ir_node *proj)
1761 ir_node *n = get_Proj_pred(proj);
1763 if (get_irn_mode(proj) == mode_X) {
1764 if (is_Block_dead(get_nodes_block(n))) {
1765 /* Remove dead control flow -- early gigo(). */
1766 return get_irg_bad(current_ir_graph);
1769 if (n->op->ops.equivalent_node_Proj)
1770 return n->op->ops.equivalent_node_Proj(proj);
1772 } /* equivalent_node_Proj */
1777 static ir_node *equivalent_node_Id(ir_node *n)
1785 DBG_OPT_ID(oldn, n);
1787 } /* equivalent_node_Id */
1792 static ir_node *equivalent_node_Mux(ir_node *n)
1794 ir_node *oldn = n, *sel = get_Mux_sel(n);
1796 tarval *ts = value_of(sel);
1798 /* Mux(true, f, t) == t */
1799 if (ts == tarval_b_true) {
1800 n = get_Mux_true(n);
1801 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_C);
1804 /* Mux(false, f, t) == f */
1805 if (ts == tarval_b_false) {
1806 n = get_Mux_false(n);
1807 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_C);
1810 n_t = get_Mux_true(n);
1811 n_f = get_Mux_false(n);
1813 /* Mux(v, x, T) == x */
1814 if (is_Unknown(n_f)) {
1816 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_EQ);
1819 /* Mux(v, T, x) == x */
1820 if (is_Unknown(n_t)) {
1822 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_EQ);
1826 /* Mux(v, x, x) == x */
1829 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_EQ);
1832 if (is_Proj(sel) && !mode_honor_signed_zeros(get_irn_mode(n))) {
1833 ir_node *cmp = get_Proj_pred(sel);
1834 long proj_nr = get_Proj_proj(sel);
1835 ir_node *f = get_Mux_false(n);
1836 ir_node *t = get_Mux_true(n);
1839 * Note further that these optimization work even for floating point
1840 * with NaN's because -NaN == NaN.
1841 * However, if +0 and -0 is handled differently, we cannot use the first one.
1844 ir_node *const cmp_l = get_Cmp_left(cmp);
1845 ir_node *const cmp_r = get_Cmp_right(cmp);
1849 if ((cmp_l == t && cmp_r == f) || /* Mux(t == f, t, f) -> f */
1850 (cmp_l == f && cmp_r == t)) { /* Mux(f == t, t, f) -> f */
1852 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1859 if ((cmp_l == t && cmp_r == f) || /* Mux(t != f, t, f) -> t */
1860 (cmp_l == f && cmp_r == t)) { /* Mux(f != t, t, f) -> t */
1862 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1869 * Note: normalization puts the constant on the right side,
1870 * so we check only one case.
1872 if (cmp_l == t && tarval_is_null(value_of(cmp_r))) {
1873 /* Mux(t CMP 0, X, t) */
1874 if (is_Minus(f) && get_Minus_op(f) == t) {
1875 /* Mux(t CMP 0, -t, t) */
1876 if (proj_nr == pn_Cmp_Eq) {
1877 /* Mux(t == 0, -t, t) ==> -t */
1879 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1880 } else if (proj_nr == pn_Cmp_Lg || proj_nr == pn_Cmp_Ne) {
1881 /* Mux(t != 0, -t, t) ==> t */
1883 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1890 } /* equivalent_node_Mux */
1893 * Remove Confirm nodes if setting is on.
1894 * Replace Confirms(x, '=', Constlike) by Constlike.
1896 static ir_node *equivalent_node_Confirm(ir_node *n)
1898 ir_node *pred = get_Confirm_value(n);
1899 pn_Cmp pnc = get_Confirm_cmp(n);
1901 while (is_Confirm(pred) && pnc == get_Confirm_cmp(pred)) {
1903 * rare case: two identical Confirms one after another,
1904 * replace the second one with the first.
1907 pred = get_Confirm_value(n);
1908 pnc = get_Confirm_cmp(n);
1910 if (get_opt_remove_confirm())
1911 return get_Confirm_value(n);
1916 * equivalent_node() returns a node equivalent to input n. It skips all nodes that
1917 * perform no actual computation, as, e.g., the Id nodes. It does not create
1918 * new nodes. It is therefore safe to free n if the node returned is not n.
1919 * If a node returns a Tuple we can not just skip it. If the size of the
1920 * in array fits, we transform n into a tuple (e.g., Div).
1922 ir_node *equivalent_node(ir_node *n)
1924 if (n->op->ops.equivalent_node)
1925 return n->op->ops.equivalent_node(n);
1927 } /* equivalent_node */
1930 * Sets the default equivalent node operation for an ir_op_ops.
1932 * @param code the opcode for the default operation
1933 * @param ops the operations initialized
1938 static ir_op_ops *firm_set_default_equivalent_node(ir_opcode code, ir_op_ops *ops)
1942 ops->equivalent_node = equivalent_node_##a; \
1944 #define CASE_PROJ(a) \
1946 ops->equivalent_node_Proj = equivalent_node_Proj_##a; \
1988 } /* firm_set_default_equivalent_node */
1991 * Returns non-zero if a node is a Phi node
1992 * with all predecessors constant.
1994 static int is_const_Phi(ir_node *n)
1998 if (! is_Phi(n) || get_irn_arity(n) == 0)
2000 for (i = get_irn_arity(n) - 1; i >= 0; --i) {
2001 if (! is_Const(get_irn_n(n, i)))
2005 } /* is_const_Phi */
2007 typedef tarval *(*tarval_sub_type)(tarval *a, tarval *b, ir_mode *mode);
2008 typedef tarval *(*tarval_binop_type)(tarval *a, tarval *b);
2011 * in reality eval_func should be tarval (*eval_func)() but incomplete
2012 * declarations are bad style and generate noisy warnings
2014 typedef void (*eval_func)(void);
2017 * Wrapper for the tarval binop evaluation, tarval_sub has one more parameter.
2019 static tarval *do_eval(eval_func eval, tarval *a, tarval *b, ir_mode *mode)
2021 if (eval == (eval_func) tarval_sub) {
2022 tarval_sub_type func = (tarval_sub_type)eval;
2024 return func(a, b, mode);
2026 tarval_binop_type func = (tarval_binop_type)eval;
2033 * Apply an evaluator on a binop with a constant operators (and one Phi).
2035 * @param phi the Phi node
2036 * @param other the other operand
2037 * @param eval an evaluator function
2038 * @param mode the mode of the result, may be different from the mode of the Phi!
2039 * @param left if non-zero, other is the left operand, else the right
2041 * @return a new Phi node if the conversion was successful, NULL else
2043 static ir_node *apply_binop_on_phi(ir_node *phi, tarval *other, eval_func eval, ir_mode *mode, int left)
2049 int i, n = get_irn_arity(phi);
2051 NEW_ARR_A(void *, res, n);
2053 for (i = 0; i < n; ++i) {
2054 pred = get_irn_n(phi, i);
2055 tv = get_Const_tarval(pred);
2056 tv = do_eval(eval, other, tv, mode);
2058 if (tv == tarval_bad) {
2059 /* folding failed, bad */
2065 for (i = 0; i < n; ++i) {
2066 pred = get_irn_n(phi, i);
2067 tv = get_Const_tarval(pred);
2068 tv = do_eval(eval, tv, other, mode);
2070 if (tv == tarval_bad) {
2071 /* folding failed, bad */
2077 irg = current_ir_graph;
2078 for (i = 0; i < n; ++i) {
2079 pred = get_irn_n(phi, i);
2080 res[i] = new_r_Const_type(irg, res[i], get_Const_type(pred));
2082 return new_r_Phi(get_nodes_block(phi), n, (ir_node **)res, mode);
2083 } /* apply_binop_on_phi */
2086 * Apply an evaluator on a binop with two constant Phi.
2088 * @param a the left Phi node
2089 * @param b the right Phi node
2090 * @param eval an evaluator function
2091 * @param mode the mode of the result, may be different from the mode of the Phi!
2093 * @return a new Phi node if the conversion was successful, NULL else
2095 static ir_node *apply_binop_on_2_phis(ir_node *a, ir_node *b, eval_func eval, ir_mode *mode)
2097 tarval *tv_l, *tv_r, *tv;
2103 if (get_nodes_block(a) != get_nodes_block(b))
2106 n = get_irn_arity(a);
2107 NEW_ARR_A(void *, res, n);
2109 for (i = 0; i < n; ++i) {
2110 pred = get_irn_n(a, i);
2111 tv_l = get_Const_tarval(pred);
2112 pred = get_irn_n(b, i);
2113 tv_r = get_Const_tarval(pred);
2114 tv = do_eval(eval, tv_l, tv_r, mode);
2116 if (tv == tarval_bad) {
2117 /* folding failed, bad */
2122 irg = current_ir_graph;
2123 for (i = 0; i < n; ++i) {
2124 pred = get_irn_n(a, i);
2125 res[i] = new_r_Const_type(irg, res[i], get_Const_type(pred));
2127 return new_r_Phi(get_nodes_block(a), n, (ir_node **)res, mode);
2128 } /* apply_binop_on_2_phis */
2131 * Apply an evaluator on a unop with a constant operator (a Phi).
2133 * @param phi the Phi node
2134 * @param eval an evaluator function
2136 * @return a new Phi node if the conversion was successful, NULL else
2138 static ir_node *apply_unop_on_phi(ir_node *phi, tarval *(*eval)(tarval *))
2145 int i, n = get_irn_arity(phi);
2147 NEW_ARR_A(void *, res, n);
2148 for (i = 0; i < n; ++i) {
2149 pred = get_irn_n(phi, i);
2150 tv = get_Const_tarval(pred);
2153 if (tv == tarval_bad) {
2154 /* folding failed, bad */
2159 mode = get_irn_mode(phi);
2160 irg = current_ir_graph;
2161 for (i = 0; i < n; ++i) {
2162 pred = get_irn_n(phi, i);
2163 res[i] = new_r_Const_type(irg, res[i], get_Const_type(pred));
2165 return new_r_Phi(get_nodes_block(phi), n, (ir_node **)res, mode);
2166 } /* apply_unop_on_phi */
2169 * Apply a conversion on a constant operator (a Phi).
2171 * @param phi the Phi node
2173 * @return a new Phi node if the conversion was successful, NULL else
2175 static ir_node *apply_conv_on_phi(ir_node *phi, ir_mode *mode)
2181 int i, n = get_irn_arity(phi);
2183 NEW_ARR_A(void *, res, n);
2184 for (i = 0; i < n; ++i) {
2185 pred = get_irn_n(phi, i);
2186 tv = get_Const_tarval(pred);
2187 tv = tarval_convert_to(tv, mode);
2189 if (tv == tarval_bad) {
2190 /* folding failed, bad */
2195 irg = current_ir_graph;
2196 for (i = 0; i < n; ++i) {
2197 pred = get_irn_n(phi, i);
2198 res[i] = new_r_Const_type(irg, res[i], get_Const_type(pred));
2200 return new_r_Phi(get_nodes_block(phi), n, (ir_node **)res, mode);
2201 } /* apply_conv_on_phi */
2204 * Transform AddP(P, ConvIs(Iu)), AddP(P, ConvIu(Is)) and
2205 * SubP(P, ConvIs(Iu)), SubP(P, ConvIu(Is)).
2206 * If possible, remove the Conv's.
2208 static ir_node *transform_node_AddSub(ir_node *n)
2210 ir_mode *mode = get_irn_mode(n);
2212 if (mode_is_reference(mode)) {
2213 ir_node *left = get_binop_left(n);
2214 ir_node *right = get_binop_right(n);
2215 unsigned ref_bits = get_mode_size_bits(mode);
2217 if (is_Conv(left)) {
2218 ir_mode *lmode = get_irn_mode(left);
2219 unsigned bits = get_mode_size_bits(lmode);
2221 if (ref_bits == bits &&
2222 mode_is_int(lmode) &&
2223 get_mode_arithmetic(lmode) == irma_twos_complement) {
2224 ir_node *pre = get_Conv_op(left);
2225 ir_mode *pre_mode = get_irn_mode(pre);
2227 if (mode_is_int(pre_mode) &&
2228 get_mode_size_bits(pre_mode) == bits &&
2229 get_mode_arithmetic(pre_mode) == irma_twos_complement) {
2230 /* ok, this conv just changes to sign, moreover the calculation
2231 * is done with same number of bits as our address mode, so
2232 * we can ignore the conv as address calculation can be viewed
2233 * as either signed or unsigned
2235 set_binop_left(n, pre);
2240 if (is_Conv(right)) {
2241 ir_mode *rmode = get_irn_mode(right);
2242 unsigned bits = get_mode_size_bits(rmode);
2244 if (ref_bits == bits &&
2245 mode_is_int(rmode) &&
2246 get_mode_arithmetic(rmode) == irma_twos_complement) {
2247 ir_node *pre = get_Conv_op(right);
2248 ir_mode *pre_mode = get_irn_mode(pre);
2250 if (mode_is_int(pre_mode) &&
2251 get_mode_size_bits(pre_mode) == bits &&
2252 get_mode_arithmetic(pre_mode) == irma_twos_complement) {
2253 /* ok, this conv just changes to sign, moreover the calculation
2254 * is done with same number of bits as our address mode, so
2255 * we can ignore the conv as address calculation can be viewed
2256 * as either signed or unsigned
2258 set_binop_right(n, pre);
2263 /* let address arithmetic use unsigned modes */
2264 if (is_Const(right)) {
2265 ir_mode *rmode = get_irn_mode(right);
2267 if (mode_is_signed(rmode) && get_mode_arithmetic(rmode) == irma_twos_complement) {
2268 /* convert a AddP(P, *s) into AddP(P, *u) */
2269 ir_mode *nm = get_reference_mode_unsigned_eq(mode);
2271 ir_node *pre = new_r_Conv(get_nodes_block(n), right, nm);
2272 set_binop_right(n, pre);
2278 } /* transform_node_AddSub */
2280 #define HANDLE_BINOP_PHI(eval, a, b, c, mode) \
2282 if (is_Const(b) && is_const_Phi(a)) { \
2283 /* check for Op(Phi, Const) */ \
2284 c = apply_binop_on_phi(a, get_Const_tarval(b), eval, mode, 0);\
2286 else if (is_Const(a) && is_const_Phi(b)) { \
2287 /* check for Op(Const, Phi) */ \
2288 c = apply_binop_on_phi(b, get_Const_tarval(a), eval, mode, 1);\
2290 else if (is_const_Phi(a) && is_const_Phi(b)) { \
2291 /* check for Op(Phi, Phi) */ \
2292 c = apply_binop_on_2_phis(a, b, eval, mode); \
2295 DBG_OPT_ALGSIM0(oldn, c, FS_OPT_CONST_PHI); \
2299 #define HANDLE_UNOP_PHI(eval, a, c) \
2301 if (is_const_Phi(a)) { \
2302 /* check for Op(Phi) */ \
2303 c = apply_unop_on_phi(a, eval); \
2305 DBG_OPT_ALGSIM0(oldn, c, FS_OPT_CONST_PHI); \
2311 * Do the AddSub optimization, then Transform
2312 * Constant folding on Phi
2313 * Add(a,a) -> Mul(a, 2)
2314 * Add(Mul(a, x), a) -> Mul(a, x+1)
2315 * if the mode is integer or float.
2316 * Transform Add(a,-b) into Sub(a,b).
2317 * Reassociation might fold this further.
2319 static ir_node *transform_node_Add(ir_node *n)
2322 ir_node *a, *b, *c, *oldn = n;
2324 n = transform_node_AddSub(n);
2326 a = get_Add_left(n);
2327 b = get_Add_right(n);
2329 mode = get_irn_mode(n);
2331 if (mode_is_reference(mode)) {
2332 ir_mode *lmode = get_irn_mode(a);
2334 if (is_Const(b) && is_Const_null(b) && mode_is_int(lmode)) {
2335 /* an Add(a, NULL) is a hidden Conv */
2336 dbg_info *dbg = get_irn_dbg_info(n);
2337 return new_rd_Conv(dbg, get_nodes_block(n), a, mode);
2341 HANDLE_BINOP_PHI((eval_func) tarval_add, a, b, c, mode);
2343 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
2344 if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
2347 if (mode_is_num(mode)) {
2348 /* the following code leads to endless recursion when Mul are replaced by a simple instruction chain */
2349 if (!is_irg_state(current_ir_graph, IR_GRAPH_STATE_ARCH_DEP)
2350 && a == b && mode_is_int(mode)) {
2351 ir_node *block = get_nodes_block(n);
2354 get_irn_dbg_info(n),
2357 new_Const_long(mode, 2),
2359 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_A);
2364 get_irn_dbg_info(n),
2369 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_MINUS_B);
2374 get_irn_dbg_info(n),
2379 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_MINUS_B);
2382 if (get_mode_arithmetic(mode) == irma_twos_complement) {
2383 /* Here we rely on constants be on the RIGHT side */
2385 ir_node *op = get_Not_op(a);
2387 if (is_Const(b) && is_Const_one(b)) {
2389 ir_node *blk = get_nodes_block(n);
2390 n = new_rd_Minus(get_irn_dbg_info(n), blk, op, mode);
2391 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_PLUS_1);
2396 n = new_Const(get_mode_minus_one(mode));
2397 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_X_NOT_X);
2402 ir_node *op = get_Not_op(b);
2406 n = new_Const(get_mode_minus_one(mode));
2407 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_X_NOT_X);
2413 if (mode_is_int(mode) && a->vrp.valid && b->vrp.valid) {
2414 tarval *c = tarval_and(
2415 tarval_not(a->vrp.bits_not_set),
2416 tarval_not(b->vrp.bits_not_set)
2419 if (tarval_is_null(c)) {
2420 dbg_info *dbgi = get_irn_dbg_info(n);
2421 return new_rd_Or(dbgi, get_nodes_block(n),
2426 } /* transform_node_Add */
2429 * returns -cnst or NULL if impossible
2431 static ir_node *const_negate(ir_node *cnst)
2433 tarval *tv = tarval_neg(get_Const_tarval(cnst));
2434 dbg_info *dbgi = get_irn_dbg_info(cnst);
2435 ir_graph *irg = get_irn_irg(cnst);
2436 if (tv == tarval_bad) return NULL;
2437 return new_rd_Const(dbgi, irg, tv);
2441 * Do the AddSub optimization, then Transform
2442 * Constant folding on Phi
2443 * Sub(0,a) -> Minus(a)
2444 * Sub(Mul(a, x), a) -> Mul(a, x-1)
2445 * Sub(Sub(x, y), b) -> Sub(x, Add(y,b))
2446 * Sub(Add(a, x), x) -> a
2447 * Sub(x, Add(x, a)) -> -a
2448 * Sub(x, Const) -> Add(x, -Const)
2450 static ir_node *transform_node_Sub(ir_node *n)
2456 n = transform_node_AddSub(n);
2458 a = get_Sub_left(n);
2459 b = get_Sub_right(n);
2461 mode = get_irn_mode(n);
2463 if (mode_is_int(mode)) {
2464 ir_mode *lmode = get_irn_mode(a);
2466 if (is_Const(b) && is_Const_null(b) && mode_is_reference(lmode)) {
2467 /* a Sub(a, NULL) is a hidden Conv */
2468 dbg_info *dbg = get_irn_dbg_info(n);
2469 n = new_rd_Conv(dbg, get_nodes_block(n), a, mode);
2470 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_CONV);
2474 if (mode == lmode &&
2475 get_mode_arithmetic(mode) == irma_twos_complement &&
2477 get_Const_tarval(a) == get_mode_minus_one(mode)) {
2479 dbg_info *dbg = get_irn_dbg_info(n);
2480 n = new_rd_Not(dbg, get_nodes_block(n), b, mode);
2481 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_NOT);
2487 HANDLE_BINOP_PHI((eval_func) tarval_sub, a, b, c, mode);
2489 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
2490 if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
2493 if (is_Const(b) && !mode_is_reference(get_irn_mode(b))) {
2494 /* a - C -> a + (-C) */
2495 ir_node *cnst = const_negate(b);
2497 ir_node *block = get_nodes_block(n);
2498 dbg_info *dbgi = get_irn_dbg_info(n);
2500 n = new_rd_Add(dbgi, block, a, cnst, mode);
2501 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2506 if (is_Minus(a)) { /* (-a) - b -> -(a + b) */
2507 dbg_info *dbg = get_irn_dbg_info(n);
2508 ir_node *block = get_nodes_block(n);
2509 ir_node *left = get_Minus_op(a);
2510 ir_node *add = new_rd_Add(dbg, block, left, b, mode);
2512 n = new_rd_Minus(dbg, block, add, mode);
2513 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2515 } else if (is_Minus(b)) { /* a - (-b) -> a + b */
2516 dbg_info *dbg = get_irn_dbg_info(n);
2517 ir_node *block = get_nodes_block(n);
2518 ir_node *right = get_Minus_op(b);
2520 n = new_rd_Add(dbg, block, a, right, mode);
2521 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MINUS);
2523 } else if (is_Sub(b)) {
2524 /* a - (b - c) -> a + (c - b)
2525 * -> (a - b) + c iff (b - c) is a pointer */
2526 dbg_info *s_dbg = get_irn_dbg_info(b);
2527 ir_node *s_block = get_nodes_block(b);
2528 ir_node *s_left = get_Sub_left(b);
2529 ir_node *s_right = get_Sub_right(b);
2530 ir_mode *s_mode = get_irn_mode(b);
2531 if (mode_is_reference(s_mode)) {
2532 ir_node *sub = new_rd_Sub(s_dbg, s_block, a, s_left, mode);
2533 dbg_info *a_dbg = get_irn_dbg_info(n);
2534 ir_node *a_block = get_nodes_block(n);
2537 s_right = new_r_Conv(a_block, s_right, mode);
2538 n = new_rd_Add(a_dbg, a_block, sub, s_right, mode);
2540 ir_node *sub = new_rd_Sub(s_dbg, s_block, s_right, s_left, s_mode);
2541 dbg_info *a_dbg = get_irn_dbg_info(n);
2542 ir_node *a_block = get_nodes_block(n);
2544 n = new_rd_Add(a_dbg, a_block, a, sub, mode);
2546 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2548 } else if (is_Mul(b)) { /* a - (b * C) -> a + (b * -C) */
2549 ir_node *m_right = get_Mul_right(b);
2550 if (is_Const(m_right)) {
2551 ir_node *cnst2 = const_negate(m_right);
2552 if (cnst2 != NULL) {
2553 dbg_info *m_dbg = get_irn_dbg_info(b);
2554 ir_node *m_block = get_nodes_block(b);
2555 ir_node *m_left = get_Mul_left(b);
2556 ir_mode *m_mode = get_irn_mode(b);
2557 ir_node *mul = new_rd_Mul(m_dbg, m_block, m_left, cnst2, m_mode);
2558 dbg_info *a_dbg = get_irn_dbg_info(n);
2559 ir_node *a_block = get_nodes_block(n);
2561 n = new_rd_Add(a_dbg, a_block, a, mul, mode);
2562 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2568 /* Beware of Sub(P, P) which cannot be optimized into a simple Minus ... */
2569 if (mode_is_num(mode) && mode == get_irn_mode(a) && is_Const(a) && is_Const_null(a)) {
2571 get_irn_dbg_info(n),
2575 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_0_A);
2579 if (mode_wrap_around(mode)) {
2580 ir_node *left = get_Add_left(a);
2581 ir_node *right = get_Add_right(a);
2583 /* FIXME: Does the Conv's work only for two complement or generally? */
2585 if (mode != get_irn_mode(right)) {
2586 /* This Sub is an effective Cast */
2587 right = new_r_Conv(get_nodes_block(n), right, mode);
2590 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2592 } else if (right == b) {
2593 if (mode != get_irn_mode(left)) {
2594 /* This Sub is an effective Cast */
2595 left = new_r_Conv(get_nodes_block(n), left, mode);
2598 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2604 if (mode_wrap_around(mode)) {
2605 ir_node *left = get_Add_left(b);
2606 ir_node *right = get_Add_right(b);
2608 /* FIXME: Does the Conv's work only for two complement or generally? */
2610 ir_mode *r_mode = get_irn_mode(right);
2612 n = new_r_Minus(get_nodes_block(n), right, r_mode);
2613 if (mode != r_mode) {
2614 /* This Sub is an effective Cast */
2615 n = new_r_Conv(get_nodes_block(n), n, mode);
2617 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2619 } else if (right == a) {
2620 ir_mode *l_mode = get_irn_mode(left);
2622 n = new_r_Minus(get_nodes_block(n), left, l_mode);
2623 if (mode != l_mode) {
2624 /* This Sub is an effective Cast */
2625 n = new_r_Conv(get_nodes_block(n), n, mode);
2627 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2632 if (mode_is_int(mode) && is_Conv(a) && is_Conv(b)) {
2633 ir_mode *mode = get_irn_mode(a);
2635 if (mode == get_irn_mode(b)) {
2637 ir_node *op_a = get_Conv_op(a);
2638 ir_node *op_b = get_Conv_op(b);
2640 /* check if it's allowed to skip the conv */
2641 ma = get_irn_mode(op_a);
2642 mb = get_irn_mode(op_b);
2644 if (mode_is_reference(ma) && mode_is_reference(mb)) {
2645 /* SubInt(ConvInt(aP), ConvInt(bP)) -> SubInt(aP,bP) */
2648 set_Sub_right(n, b);
2654 /* do NOT execute this code if reassociation is enabled, it does the inverse! */
2655 if (!is_reassoc_running() && is_Mul(a)) {
2656 ir_node *ma = get_Mul_left(a);
2657 ir_node *mb = get_Mul_right(a);
2660 ir_node *blk = get_nodes_block(n);
2662 get_irn_dbg_info(n),
2666 get_irn_dbg_info(n),
2669 new_Const_long(mode, 1),
2672 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MUL_A_X_A);
2674 } else if (mb == b) {
2675 ir_node *blk = get_nodes_block(n);
2677 get_irn_dbg_info(n),
2681 get_irn_dbg_info(n),
2684 new_Const_long(mode, 1),
2687 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MUL_A_X_A);
2691 if (is_Sub(a)) { /* (x - y) - b -> x - (y + b) */
2692 ir_node *x = get_Sub_left(a);
2693 ir_node *y = get_Sub_right(a);
2694 ir_node *blk = get_nodes_block(n);
2695 ir_mode *m_b = get_irn_mode(b);
2696 ir_mode *m_y = get_irn_mode(y);
2700 /* Determine the right mode for the Add. */
2703 else if (mode_is_reference(m_b))
2705 else if (mode_is_reference(m_y))
2709 * Both modes are different but none is reference,
2710 * happens for instance in SubP(SubP(P, Iu), Is).
2711 * We have two possibilities here: Cast or ignore.
2712 * Currently we ignore this case.
2717 add = new_r_Add(blk, y, b, add_mode);
2719 n = new_rd_Sub(get_irn_dbg_info(n), blk, x, add, mode);
2720 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_SUB_X_Y_Z);
2724 if (get_mode_arithmetic(mode) == irma_twos_complement) {
2725 if (is_Const(a) && is_Not(b)) {
2726 /* c - ~X = X + (c+1) */
2727 tarval *tv = get_Const_tarval(a);
2729 tv = tarval_add(tv, get_mode_one(mode));
2730 if (tv != tarval_bad) {
2731 ir_node *blk = get_nodes_block(n);
2732 ir_node *c = new_Const(tv);
2733 n = new_rd_Add(get_irn_dbg_info(n), blk, get_Not_op(b), c, mode);
2734 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_C_NOT_X);
2740 } /* transform_node_Sub */
2743 * Several transformation done on n*n=2n bits mul.
2744 * These transformations must be done here because new nodes may be produced.
2746 static ir_node *transform_node_Mul2n(ir_node *n, ir_mode *mode)
2749 ir_node *a = get_Mul_left(n);
2750 ir_node *b = get_Mul_right(n);
2751 tarval *ta = value_of(a);
2752 tarval *tb = value_of(b);
2753 ir_mode *smode = get_irn_mode(a);
2755 if (ta == get_mode_one(smode)) {
2756 /* (L)1 * (L)b = (L)b */
2757 ir_node *blk = get_nodes_block(n);
2758 n = new_rd_Conv(get_irn_dbg_info(n), blk, b, mode);
2759 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
2762 else if (ta == get_mode_minus_one(smode)) {
2763 /* (L)-1 * (L)b = (L)b */
2764 ir_node *blk = get_nodes_block(n);
2765 n = new_rd_Minus(get_irn_dbg_info(n), blk, b, smode);
2766 n = new_rd_Conv(get_irn_dbg_info(n), blk, n, mode);
2767 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2770 if (tb == get_mode_one(smode)) {
2771 /* (L)a * (L)1 = (L)a */
2772 ir_node *blk = get_irn_n(a, -1);
2773 n = new_rd_Conv(get_irn_dbg_info(n), blk, a, mode);
2774 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
2777 else if (tb == get_mode_minus_one(smode)) {
2778 /* (L)a * (L)-1 = (L)-a */
2779 ir_node *blk = get_nodes_block(n);
2780 n = new_rd_Minus(get_irn_dbg_info(n), blk, a, smode);
2781 n = new_rd_Conv(get_irn_dbg_info(n), blk, n, mode);
2782 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2789 * Transform Mul(a,-1) into -a.
2790 * Do constant evaluation of Phi nodes.
2791 * Do architecture dependent optimizations on Mul nodes
2793 static ir_node *transform_node_Mul(ir_node *n)
2795 ir_node *c, *oldn = n;
2796 ir_mode *mode = get_irn_mode(n);
2797 ir_node *a = get_Mul_left(n);
2798 ir_node *b = get_Mul_right(n);
2800 if (is_Bad(a) || is_Bad(b))
2803 if (mode != get_irn_mode(a))
2804 return transform_node_Mul2n(n, mode);
2806 HANDLE_BINOP_PHI((eval_func) tarval_mul, a, b, c, mode);
2808 if (mode_is_signed(mode)) {
2811 if (value_of(a) == get_mode_minus_one(mode))
2813 else if (value_of(b) == get_mode_minus_one(mode))
2816 n = new_rd_Minus(get_irn_dbg_info(n), get_nodes_block(n), r, mode);
2817 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2822 if (is_Const(b)) { /* (-a) * const -> a * -const */
2823 ir_node *cnst = const_negate(b);
2825 dbg_info *dbgi = get_irn_dbg_info(n);
2826 ir_node *block = get_nodes_block(n);
2827 n = new_rd_Mul(dbgi, block, get_Minus_op(a), cnst, mode);
2828 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2831 } else if (is_Minus(b)) { /* (-a) * (-b) -> a * b */
2832 dbg_info *dbgi = get_irn_dbg_info(n);
2833 ir_node *block = get_nodes_block(n);
2834 n = new_rd_Mul(dbgi, block, get_Minus_op(a), get_Minus_op(b), mode);
2835 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_MINUS);
2837 } else if (is_Sub(b)) { /* (-a) * (b - c) -> a * (c - b) */
2838 ir_node *sub_l = get_Sub_left(b);
2839 ir_node *sub_r = get_Sub_right(b);
2840 dbg_info *dbgi = get_irn_dbg_info(n);
2841 ir_node *block = get_nodes_block(n);
2842 ir_node *new_b = new_rd_Sub(dbgi, block, sub_r, sub_l, mode);
2843 n = new_rd_Mul(dbgi, block, get_Minus_op(a), new_b, mode);
2844 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS);
2847 } else if (is_Minus(b)) {
2848 if (is_Sub(a)) { /* (a - b) * (-c) -> (b - a) * c */
2849 ir_node *sub_l = get_Sub_left(a);
2850 ir_node *sub_r = get_Sub_right(a);
2851 dbg_info *dbgi = get_irn_dbg_info(n);
2852 ir_node *block = get_nodes_block(n);
2853 ir_node *new_a = new_rd_Sub(dbgi, block, sub_r, sub_l, mode);
2854 n = new_rd_Mul(dbgi, block, new_a, get_Minus_op(b), mode);
2855 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS);
2858 } else if (is_Shl(a)) {
2859 ir_node *const shl_l = get_Shl_left(a);
2860 if (is_Const(shl_l) && is_Const_one(shl_l)) {
2861 /* (1 << x) * b -> b << x */
2862 dbg_info *const dbgi = get_irn_dbg_info(n);
2863 ir_node *const block = get_nodes_block(n);
2864 ir_node *const shl_r = get_Shl_right(a);
2865 n = new_rd_Shl(dbgi, block, b, shl_r, mode);
2866 // TODO add me DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_SHIFT);
2869 } else if (is_Shl(b)) {
2870 ir_node *const shl_l = get_Shl_left(b);
2871 if (is_Const(shl_l) && is_Const_one(shl_l)) {
2872 /* a * (1 << x) -> a << x */
2873 dbg_info *const dbgi = get_irn_dbg_info(n);
2874 ir_node *const block = get_nodes_block(n);
2875 ir_node *const shl_r = get_Shl_right(b);
2876 n = new_rd_Shl(dbgi, block, a, shl_r, mode);
2877 // TODO add me DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_SHIFT);
2881 if (get_mode_arithmetic(mode) == irma_ieee754) {
2883 tarval *tv = get_Const_tarval(a);
2884 if (tarval_ieee754_get_exponent(tv) == 1 && tarval_ieee754_zero_mantissa(tv)
2885 && !tarval_is_negative(tv)) {
2886 /* 2.0 * b = b + b */
2887 n = new_rd_Add(get_irn_dbg_info(n), get_nodes_block(n), b, b, mode);
2888 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_A_A);
2892 else if (is_Const(b)) {
2893 tarval *tv = get_Const_tarval(b);
2894 if (tarval_ieee754_get_exponent(tv) == 1 && tarval_ieee754_zero_mantissa(tv)
2895 && !tarval_is_negative(tv)) {
2896 /* a * 2.0 = a + a */
2897 n = new_rd_Add(get_irn_dbg_info(n), get_nodes_block(n), a, a, mode);
2898 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_A_A);
2903 return arch_dep_replace_mul_with_shifts(n);
2904 } /* transform_node_Mul */
2907 * Transform a Div Node.
2909 static ir_node *transform_node_Div(ir_node *n)
2911 ir_mode *mode = get_Div_resmode(n);
2912 ir_node *a = get_Div_left(n);
2913 ir_node *b = get_Div_right(n);
2915 const ir_node *dummy;
2917 if (is_Const(b) && is_const_Phi(a)) {
2918 /* check for Div(Phi, Const) */
2919 value = apply_binop_on_phi(a, get_Const_tarval(b), (eval_func) tarval_div, mode, 0);
2921 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2925 else if (is_Const(a) && is_const_Phi(b)) {
2926 /* check for Div(Const, Phi) */
2927 value = apply_binop_on_phi(b, get_Const_tarval(a), (eval_func) tarval_div, mode, 1);
2929 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2933 else if (is_const_Phi(a) && is_const_Phi(b)) {
2934 /* check for Div(Phi, Phi) */
2935 value = apply_binop_on_2_phis(a, b, (eval_func) tarval_div, mode);
2937 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2944 if (a == b && value_not_zero(a, &dummy)) {
2945 /* BEWARE: we can optimize a/a to 1 only if this cannot cause a exception */
2946 value = new_Const(get_mode_one(mode));
2947 DBG_OPT_CSTEVAL(n, value);
2950 if (mode_is_signed(mode) && is_Const(b)) {
2951 tarval *tv = get_Const_tarval(b);
2953 if (tv == get_mode_minus_one(mode)) {
2955 value = new_rd_Minus(get_irn_dbg_info(n), get_nodes_block(n), a, mode);
2956 DBG_OPT_CSTEVAL(n, value);
2960 /* Try architecture dependent optimization */
2961 value = arch_dep_replace_div_by_const(n);
2968 /* Turn Div into a tuple (mem, jmp, bad, value) */
2969 mem = get_Div_mem(n);
2970 blk = get_nodes_block(n);
2972 /* skip a potential Pin */
2973 mem = skip_Pin(mem);
2974 turn_into_tuple(n, pn_Div_max);
2975 set_Tuple_pred(n, pn_Div_M, mem);
2976 set_Tuple_pred(n, pn_Div_X_regular, new_r_Jmp(blk));
2977 set_Tuple_pred(n, pn_Div_X_except, new_Bad());
2978 set_Tuple_pred(n, pn_Div_res, value);
2981 } /* transform_node_Div */
2984 * Transform a Mod node.
2986 static ir_node *transform_node_Mod(ir_node *n)
2988 ir_mode *mode = get_Mod_resmode(n);
2989 ir_node *a = get_Mod_left(n);
2990 ir_node *b = get_Mod_right(n);
2994 if (is_Const(b) && is_const_Phi(a)) {
2995 /* check for Div(Phi, Const) */
2996 value = apply_binop_on_phi(a, get_Const_tarval(b), (eval_func) tarval_mod, mode, 0);
2998 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
3002 else if (is_Const(a) && is_const_Phi(b)) {
3003 /* check for Div(Const, Phi) */
3004 value = apply_binop_on_phi(b, get_Const_tarval(a), (eval_func) tarval_mod, mode, 1);
3006 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
3010 else if (is_const_Phi(a) && is_const_Phi(b)) {
3011 /* check for Div(Phi, Phi) */
3012 value = apply_binop_on_2_phis(a, b, (eval_func) tarval_mod, mode);
3014 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
3021 if (tv != tarval_bad) {
3022 value = new_Const(tv);
3024 DBG_OPT_CSTEVAL(n, value);
3027 ir_node *a = get_Mod_left(n);
3028 ir_node *b = get_Mod_right(n);
3029 const ir_node *dummy;
3031 if (a == b && value_not_zero(a, &dummy)) {
3032 /* BEWARE: we can optimize a%a to 0 only if this cannot cause a exception */
3033 value = new_Const(get_mode_null(mode));
3034 DBG_OPT_CSTEVAL(n, value);
3037 if (mode_is_signed(mode) && is_Const(b)) {
3038 tarval *tv = get_Const_tarval(b);
3040 if (tv == get_mode_minus_one(mode)) {
3042 value = new_Const(get_mode_null(mode));
3043 DBG_OPT_CSTEVAL(n, value);
3047 /* Try architecture dependent optimization */
3048 value = arch_dep_replace_mod_by_const(n);
3056 /* Turn Mod into a tuple (mem, jmp, bad, value) */
3057 mem = get_Mod_mem(n);
3058 blk = get_nodes_block(n);
3060 /* skip a potential Pin */
3061 mem = skip_Pin(mem);
3062 turn_into_tuple(n, pn_Mod_max);
3063 set_Tuple_pred(n, pn_Mod_M, mem);
3064 set_Tuple_pred(n, pn_Mod_X_regular, new_r_Jmp(blk));
3065 set_Tuple_pred(n, pn_Mod_X_except, new_Bad());
3066 set_Tuple_pred(n, pn_Mod_res, value);
3069 } /* transform_node_Mod */
3072 * Transform a DivMod node.
3074 static ir_node *transform_node_DivMod(ir_node *n)
3076 const ir_node *dummy;
3077 ir_node *a = get_DivMod_left(n);
3078 ir_node *b = get_DivMod_right(n);
3079 ir_mode *mode = get_DivMod_resmode(n);
3084 if (is_Const(b) && is_const_Phi(a)) {
3085 /* check for Div(Phi, Const) */
3086 va = apply_binop_on_phi(a, get_Const_tarval(b), (eval_func) tarval_div, mode, 0);
3087 vb = apply_binop_on_phi(a, get_Const_tarval(b), (eval_func) tarval_mod, mode, 0);
3089 DBG_OPT_ALGSIM0(n, va, FS_OPT_CONST_PHI);
3090 DBG_OPT_ALGSIM0(n, vb, FS_OPT_CONST_PHI);
3094 else if (is_Const(a) && is_const_Phi(b)) {
3095 /* check for Div(Const, Phi) */
3096 va = apply_binop_on_phi(b, get_Const_tarval(a), (eval_func) tarval_div, mode, 1);
3097 vb = apply_binop_on_phi(b, get_Const_tarval(a), (eval_func) tarval_mod, mode, 1);
3099 DBG_OPT_ALGSIM0(n, va, FS_OPT_CONST_PHI);
3100 DBG_OPT_ALGSIM0(n, vb, FS_OPT_CONST_PHI);
3104 else if (is_const_Phi(a) && is_const_Phi(b)) {
3105 /* check for Div(Phi, Phi) */
3106 va = apply_binop_on_2_phis(a, b, (eval_func) tarval_div, mode);
3107 vb = apply_binop_on_2_phis(a, b, (eval_func) tarval_mod, mode);
3109 DBG_OPT_ALGSIM0(n, va, FS_OPT_CONST_PHI);
3110 DBG_OPT_ALGSIM0(n, vb, FS_OPT_CONST_PHI);
3117 if (tb != tarval_bad) {
3118 if (tb == get_mode_one(get_tarval_mode(tb))) {
3120 vb = new_Const(get_mode_null(mode));
3121 DBG_OPT_CSTEVAL(n, vb);
3123 } else if (ta != tarval_bad) {
3124 tarval *resa, *resb;
3125 resa = tarval_div(ta, tb);
3126 if (resa == tarval_bad) return n; /* Causes exception!!! Model by replacing through
3127 Jmp for X result!? */
3128 resb = tarval_mod(ta, tb);
3129 if (resb == tarval_bad) return n; /* Causes exception! */
3130 va = new_Const(resa);
3131 vb = new_Const(resb);
3132 DBG_OPT_CSTEVAL(n, va);
3133 DBG_OPT_CSTEVAL(n, vb);
3135 } else if (mode_is_signed(mode) && tb == get_mode_minus_one(mode)) {
3136 va = new_rd_Minus(get_irn_dbg_info(n), get_nodes_block(n), a, mode);
3137 vb = new_Const(get_mode_null(mode));
3138 DBG_OPT_CSTEVAL(n, va);
3139 DBG_OPT_CSTEVAL(n, vb);
3141 } else { /* Try architecture dependent optimization */
3144 arch_dep_replace_divmod_by_const(&va, &vb, n);
3145 evaluated = va != NULL;
3147 } else if (a == b) {
3148 if (value_not_zero(a, &dummy)) {
3150 va = new_Const(get_mode_one(mode));
3151 vb = new_Const(get_mode_null(mode));
3152 DBG_OPT_CSTEVAL(n, va);
3153 DBG_OPT_CSTEVAL(n, vb);
3156 /* BEWARE: it is NOT possible to optimize a/a to 1, as this may cause a exception */
3159 } else if (ta == get_mode_null(mode) && value_not_zero(b, &dummy)) {
3160 /* 0 / non-Const = 0 */
3165 if (evaluated) { /* replace by tuple */
3169 mem = get_DivMod_mem(n);
3170 /* skip a potential Pin */
3171 mem = skip_Pin(mem);
3173 blk = get_nodes_block(n);
3174 turn_into_tuple(n, pn_DivMod_max);
3175 set_Tuple_pred(n, pn_DivMod_M, mem);
3176 set_Tuple_pred(n, pn_DivMod_X_regular, new_r_Jmp(blk));
3177 set_Tuple_pred(n, pn_DivMod_X_except, new_Bad()); /* no exception */
3178 set_Tuple_pred(n, pn_DivMod_res_div, va);
3179 set_Tuple_pred(n, pn_DivMod_res_mod, vb);
3183 } /* transform_node_DivMod */
3186 * Optimize x / c to x * (1/c)
3188 static ir_node *transform_node_Quot(ir_node *n)
3190 ir_mode *mode = get_Quot_resmode(n);
3193 if (get_mode_arithmetic(mode) == irma_ieee754) {
3194 ir_node *b = get_Quot_right(n);
3195 tarval *tv = value_of(b);
3197 if (tv != tarval_bad) {
3198 int rem = tarval_fp_ops_enabled();
3201 * Floating point constant folding might be disabled here to
3203 * However, as we check for exact result, doing it is safe.
3206 tarval_enable_fp_ops(1);
3207 tv = tarval_quo(get_mode_one(mode), tv);
3208 tarval_enable_fp_ops(rem);
3210 /* Do the transformation if the result is either exact or we are not
3211 using strict rules. */
3212 if (tv != tarval_bad &&
3213 (tarval_ieee754_get_exact() || (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic) == 0)) {
3214 ir_node *blk = get_nodes_block(n);
3215 ir_node *c = new_Const(tv);
3216 ir_node *a = get_Quot_left(n);
3217 ir_node *m = new_rd_Mul(get_irn_dbg_info(n), blk, a, c, mode);
3218 ir_node *mem = get_Quot_mem(n);
3220 /* skip a potential Pin */
3221 mem = skip_Pin(mem);
3222 turn_into_tuple(n, pn_Quot_max);
3223 set_Tuple_pred(n, pn_Quot_M, mem);
3224 set_Tuple_pred(n, pn_Quot_X_regular, new_r_Jmp(blk));
3225 set_Tuple_pred(n, pn_Quot_X_except, new_Bad());
3226 set_Tuple_pred(n, pn_Quot_res, m);
3227 DBG_OPT_ALGSIM1(oldn, a, b, m, FS_OPT_FP_INV_MUL);
3232 } /* transform_node_Quot */
3235 * Optimize Abs(x) into x if x is Confirmed >= 0
3236 * Optimize Abs(x) into -x if x is Confirmed <= 0
3237 * Optimize Abs(-x) int Abs(x)
3239 static ir_node *transform_node_Abs(ir_node *n)
3241 ir_node *c, *oldn = n;
3242 ir_node *a = get_Abs_op(n);
3245 HANDLE_UNOP_PHI(tarval_abs, a, c);
3247 switch (classify_value_sign(a)) {
3248 case value_classified_negative:
3249 mode = get_irn_mode(n);
3252 * We can replace the Abs by -x here.
3253 * We even could add a new Confirm here
3254 * (if not twos complement)
3256 * Note that -x would create a new node, so we could
3257 * not run it in the equivalent_node() context.
3259 n = new_rd_Minus(get_irn_dbg_info(n), get_nodes_block(n), a, mode);
3261 DBG_OPT_CONFIRM(oldn, n);
3263 case value_classified_positive:
3264 /* n is positive, Abs is not needed */
3267 DBG_OPT_CONFIRM(oldn, n);
3273 /* Abs(-x) = Abs(x) */
3274 mode = get_irn_mode(n);
3275 n = new_rd_Abs(get_irn_dbg_info(n), get_nodes_block(n), get_Minus_op(a), mode);
3276 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ABS_MINUS_X);
3280 } /* transform_node_Abs */
3283 * Optimize -a CMP -b into b CMP a.
3284 * This works only for for modes where unary Minus
3286 * Note that two-complement integers can Overflow
3287 * so it will NOT work.
3289 * For == and != can be handled in Proj(Cmp)
3291 static ir_node *transform_node_Cmp(ir_node *n)
3294 ir_node *left = get_Cmp_left(n);
3295 ir_node *right = get_Cmp_right(n);
3297 if (is_Minus(left) && is_Minus(right) &&
3298 !mode_overflow_on_unary_Minus(get_irn_mode(left))) {
3299 ir_node *const new_left = get_Minus_op(right);
3300 ir_node *const new_right = get_Minus_op(left);
3301 n = new_rd_Cmp(get_irn_dbg_info(n), get_nodes_block(n), new_left, new_right);
3302 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CMP_OP_OP);
3305 } /* transform_node_Cmp */
3309 * Transform a Cond node.
3311 * Replace the Cond by a Jmp if it branches on a constant
3314 static ir_node *transform_node_Cond(ir_node *n)
3318 ir_node *a = get_Cond_selector(n);
3319 tarval *ta = value_of(a);
3321 /* we need block info which is not available in floating irgs */
3322 if (get_irg_pinned(current_ir_graph) == op_pin_state_floats)
3325 if ((ta != tarval_bad) &&
3326 (get_irn_mode(a) == mode_b) &&
3327 (get_opt_unreachable_code())) {
3328 /* It's a boolean Cond, branching on a boolean constant.
3329 Replace it by a tuple (Bad, Jmp) or (Jmp, Bad) */
3330 ir_node *blk = get_nodes_block(n);
3331 jmp = new_r_Jmp(blk);
3332 turn_into_tuple(n, pn_Cond_max);
3333 if (ta == tarval_b_true) {
3334 set_Tuple_pred(n, pn_Cond_false, new_Bad());
3335 set_Tuple_pred(n, pn_Cond_true, jmp);
3337 set_Tuple_pred(n, pn_Cond_false, jmp);
3338 set_Tuple_pred(n, pn_Cond_true, new_Bad());
3340 /* We might generate an endless loop, so keep it alive. */
3341 add_End_keepalive(get_irg_end(current_ir_graph), blk);
3344 } /* transform_node_Cond */
3347 * Prototype of a recursive transform function
3348 * for bitwise distributive transformations.
3350 typedef ir_node* (*recursive_transform)(ir_node *n);
3353 * makes use of distributive laws for and, or, eor
3354 * and(a OP c, b OP c) -> and(a, b) OP c
3355 * note, might return a different op than n
3357 static ir_node *transform_bitwise_distributive(ir_node *n,
3358 recursive_transform trans_func)
3361 ir_node *a = get_binop_left(n);
3362 ir_node *b = get_binop_right(n);
3363 ir_op *op = get_irn_op(a);
3364 ir_op *op_root = get_irn_op(n);
3366 if (op != get_irn_op(b))
3369 /* and(conv(a), conv(b)) -> conv(and(a,b)) */
3370 if (op == op_Conv) {
3371 ir_node *a_op = get_Conv_op(a);
3372 ir_node *b_op = get_Conv_op(b);
3373 ir_mode *a_mode = get_irn_mode(a_op);
3374 ir_mode *b_mode = get_irn_mode(b_op);
3375 if (a_mode == b_mode && (mode_is_int(a_mode) || a_mode == mode_b)) {
3376 ir_node *blk = get_nodes_block(n);
3379 set_binop_left(n, a_op);
3380 set_binop_right(n, b_op);
3381 set_irn_mode(n, a_mode);
3383 n = new_r_Conv(blk, n, get_irn_mode(oldn));
3385 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
3391 /* nothing to gain here */
3395 if (op == op_Shrs || op == op_Shr || op == op_Shl
3396 || op == op_And || op == op_Or || op == op_Eor) {
3397 ir_node *a_left = get_binop_left(a);
3398 ir_node *a_right = get_binop_right(a);
3399 ir_node *b_left = get_binop_left(b);
3400 ir_node *b_right = get_binop_right(b);
3402 ir_node *op1 = NULL;
3403 ir_node *op2 = NULL;
3405 if (is_op_commutative(op)) {
3406 if (a_left == b_left) {
3410 } else if (a_left == b_right) {
3414 } else if (a_right == b_left) {
3420 if (a_right == b_right) {
3427 /* (a sop c) & (b sop c) => (a & b) sop c */
3428 ir_node *blk = get_nodes_block(n);
3430 ir_node *new_n = exact_copy(n);
3431 set_binop_left(new_n, op1);
3432 set_binop_right(new_n, op2);
3433 new_n = trans_func(new_n);
3435 if (op_root == op_Eor && op == op_Or) {
3436 dbg_info *dbgi = get_irn_dbg_info(n);
3437 ir_mode *mode = get_irn_mode(c);
3439 c = new_rd_Not(dbgi, blk, c, mode);
3440 n = new_rd_And(dbgi, blk, new_n, c, mode);
3443 set_nodes_block(n, blk);
3444 set_binop_left(n, new_n);
3445 set_binop_right(n, c);
3446 add_identities(current_ir_graph->value_table, n);
3449 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_SHIFT_AND);
3460 static ir_node *transform_node_And(ir_node *n)
3462 ir_node *c, *oldn = n;
3463 ir_node *a = get_And_left(n);
3464 ir_node *b = get_And_right(n);
3467 mode = get_irn_mode(n);
3468 HANDLE_BINOP_PHI((eval_func) tarval_and, a, b, c, mode);
3470 /* we can evaluate 2 Projs of the same Cmp */
3471 if (mode == mode_b && is_Proj(a) && is_Proj(b)) {
3472 ir_node *pred_a = get_Proj_pred(a);
3473 ir_node *pred_b = get_Proj_pred(b);
3474 if (pred_a == pred_b) {
3475 dbg_info *dbgi = get_irn_dbg_info(n);
3476 ir_node *block = get_nodes_block(pred_a);
3477 pn_Cmp pn_a = get_Proj_proj(a);
3478 pn_Cmp pn_b = get_Proj_proj(b);
3479 /* yes, we can simply calculate with pncs */
3480 pn_Cmp new_pnc = pn_a & pn_b;
3482 return new_rd_Proj(dbgi, block, pred_a, mode_b, new_pnc);
3487 ir_node *op = get_Not_op(b);
3489 ir_node *ba = get_And_left(op);
3490 ir_node *bb = get_And_right(op);
3492 /* it's enough to test the following cases due to normalization! */
3493 if (get_Or_left(a) == ba && get_Or_right(a) == bb) {
3494 /* (a|b) & ~(a&b) = a^b */
3495 ir_node *block = get_nodes_block(n);
3497 n = new_rd_Eor(get_irn_dbg_info(n), block, ba, bb, mode);
3498 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_TO_EOR);
3506 ir_node *op = get_Not_op(a);
3508 ir_node *aa = get_And_left(op);
3509 ir_node *ab = get_And_right(op);
3511 /* it's enough to test the following cases due to normalization! */
3512 if (get_Or_left(b) == aa && get_Or_right(b) == ab) {
3513 /* (a|b) & ~(a&b) = a^b */
3514 ir_node *block = get_nodes_block(n);
3516 n = new_rd_Eor(get_irn_dbg_info(n), block, aa, ab, mode);
3517 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_TO_EOR);
3524 ir_node *al = get_Eor_left(a);
3525 ir_node *ar = get_Eor_right(a);
3528 /* (b ^ a) & b -> ~a & b */
3529 dbg_info *dbg = get_irn_dbg_info(n);
3530 ir_node *block = get_nodes_block(n);
3532 ar = new_rd_Not(dbg, block, ar, mode);
3533 n = new_rd_And(dbg, block, ar, b, mode);
3534 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3538 /* (a ^ b) & b -> ~a & b */
3539 dbg_info *dbg = get_irn_dbg_info(n);
3540 ir_node *block = get_nodes_block(n);
3542 al = new_rd_Not(dbg, block, al, mode);
3543 n = new_rd_And(dbg, block, al, b, mode);
3544 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3549 ir_node *bl = get_Eor_left(b);
3550 ir_node *br = get_Eor_right(b);
3553 /* a & (a ^ b) -> a & ~b */
3554 dbg_info *dbg = get_irn_dbg_info(n);
3555 ir_node *block = get_nodes_block(n);
3557 br = new_rd_Not(dbg, block, br, mode);
3558 n = new_rd_And(dbg, block, br, a, mode);
3559 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3563 /* a & (b ^ a) -> a & ~b */
3564 dbg_info *dbg = get_irn_dbg_info(n);
3565 ir_node *block = get_nodes_block(n);
3567 bl = new_rd_Not(dbg, block, bl, mode);
3568 n = new_rd_And(dbg, block, bl, a, mode);
3569 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3573 if (is_Not(a) && is_Not(b)) {
3574 /* ~a & ~b = ~(a|b) */
3575 ir_node *block = get_nodes_block(n);
3576 ir_mode *mode = get_irn_mode(n);
3580 n = new_rd_Or(get_irn_dbg_info(n), block, a, b, mode);
3581 n = new_rd_Not(get_irn_dbg_info(n), block, n, mode);
3582 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_DEMORGAN);
3586 if (is_Const(a) && b->vrp.valid && (tarval_is_all_one(tarval_or(get_Const_tarval(a),
3587 b->vrp.bits_not_set)))) {
3588 return new_rd_Id(get_irn_dbg_info(n), get_nodes_block(n),
3589 b, get_irn_mode(n));
3593 if (is_Const(b) && a->vrp.valid && (tarval_is_all_one(tarval_or(get_Const_tarval(b),
3594 a->vrp.bits_not_set)))) {
3595 return new_rd_Id(get_irn_dbg_info(n), get_nodes_block(n),
3596 a, get_irn_mode(n));
3600 n = transform_bitwise_distributive(n, transform_node_And);
3603 } /* transform_node_And */
3608 static ir_node *transform_node_Eor(ir_node *n)
3610 ir_node *c, *oldn = n;
3611 ir_node *a = get_Eor_left(n);
3612 ir_node *b = get_Eor_right(n);
3613 ir_mode *mode = get_irn_mode(n);
3615 HANDLE_BINOP_PHI((eval_func) tarval_eor, a, b, c, mode);
3617 /* we can evaluate 2 Projs of the same Cmp */
3618 if (mode == mode_b && is_Proj(a) && is_Proj(b)) {
3619 ir_node *pred_a = get_Proj_pred(a);
3620 ir_node *pred_b = get_Proj_pred(b);
3621 if (pred_a == pred_b) {
3622 dbg_info *dbgi = get_irn_dbg_info(n);
3623 ir_node *block = get_nodes_block(pred_a);
3624 pn_Cmp pn_a = get_Proj_proj(a);
3625 pn_Cmp pn_b = get_Proj_proj(b);
3626 /* yes, we can simply calculate with pncs */
3627 pn_Cmp new_pnc = pn_a ^ pn_b;
3629 return new_rd_Proj(dbgi, block, pred_a, mode_b, new_pnc);
3635 n = new_rd_Const(get_irn_dbg_info(n), current_ir_graph,
3636 get_mode_null(mode));
3637 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_A_A);
3638 } else if (is_Const(b)) {
3639 if (is_Not(a)) { /* ~x ^ const -> x ^ ~const */
3640 ir_node *cnst = new_Const(tarval_not(get_Const_tarval(b)));
3641 ir_node *not_op = get_Not_op(a);
3642 dbg_info *dbg = get_irn_dbg_info(n);
3643 ir_node *block = get_nodes_block(n);
3644 ir_mode *mode = get_irn_mode(n);
3645 n = new_rd_Eor(dbg, block, not_op, cnst, mode);
3647 } else if (is_Const_all_one(b)) { /* x ^ 1...1 -> ~1 */
3648 n = new_r_Not(get_nodes_block(n), a, mode);
3649 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3652 n = transform_bitwise_distributive(n, transform_node_Eor);
3656 } /* transform_node_Eor */
3661 static ir_node *transform_node_Not(ir_node *n)
3663 ir_node *c, *oldn = n;
3664 ir_node *a = get_Not_op(n);
3665 ir_mode *mode = get_irn_mode(n);
3667 HANDLE_UNOP_PHI(tarval_not,a,c);
3669 /* check for a boolean Not */
3670 if (mode == mode_b && is_Proj(a)) {
3671 ir_node *a_pred = get_Proj_pred(a);
3672 if (is_Cmp(a_pred)) {
3673 ir_node *cmp_block = get_nodes_block(a_pred);
3674 /* We negate a Cmp. The Cmp has the negated result anyways! */
3675 n = new_r_Proj(cmp_block, get_Proj_pred(a),
3676 mode_b, get_negated_pnc(get_Proj_proj(a), mode_b));
3677 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_CMP);
3682 ir_node *eor_b = get_Eor_right(a);
3683 if (is_Const(eor_b)) { /* ~(x ^ const) -> x ^ ~const */
3684 ir_node *cnst = new_Const(tarval_not(get_Const_tarval(eor_b)));
3685 ir_node *eor_a = get_Eor_left(a);
3686 dbg_info *dbg = get_irn_dbg_info(n);
3687 ir_node *block = get_nodes_block(n);
3688 ir_mode *mode = get_irn_mode(n);
3689 n = new_rd_Eor(dbg, block, eor_a, cnst, mode);
3693 if (get_mode_arithmetic(mode) == irma_twos_complement) {
3694 if (is_Minus(a)) { /* ~-x -> x + -1 */
3695 dbg_info *dbg = get_irn_dbg_info(n);
3696 ir_graph *irg = current_ir_graph;
3697 ir_node *block = get_nodes_block(n);
3698 ir_node *add_l = get_Minus_op(a);
3699 ir_node *add_r = new_rd_Const(dbg, irg, get_mode_minus_one(mode));
3700 n = new_rd_Add(dbg, block, add_l, add_r, mode);
3701 } else if (is_Add(a)) {
3702 ir_node *add_r = get_Add_right(a);
3703 if (is_Const(add_r) && is_Const_all_one(add_r)) {
3704 /* ~(x + -1) = -x */
3705 ir_node *op = get_Add_left(a);
3706 ir_node *blk = get_nodes_block(n);
3707 n = new_rd_Minus(get_irn_dbg_info(n), blk, op, get_irn_mode(n));
3708 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_MINUS_1);
3713 } /* transform_node_Not */
3716 * Transform a Minus.
3720 * -(a >>u (size-1)) = a >>s (size-1)
3721 * -(a >>s (size-1)) = a >>u (size-1)
3722 * -(a * const) -> a * -const
3724 static ir_node *transform_node_Minus(ir_node *n)
3726 ir_node *c, *oldn = n;
3727 ir_node *a = get_Minus_op(n);
3730 HANDLE_UNOP_PHI(tarval_neg,a,c);
3732 mode = get_irn_mode(a);
3733 if (get_mode_arithmetic(mode) == irma_twos_complement) {
3734 /* the following rules are only to twos-complement */
3737 ir_node *op = get_Not_op(a);
3738 tarval *tv = get_mode_one(mode);
3739 ir_node *blk = get_nodes_block(n);
3740 ir_node *c = new_Const(tv);
3741 n = new_rd_Add(get_irn_dbg_info(n), blk, op, c, mode);
3742 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_MINUS_NOT);
3746 ir_node *c = get_Shr_right(a);
3749 tarval *tv = get_Const_tarval(c);
3751 if (tarval_is_long(tv) && get_tarval_long(tv) == (int) get_mode_size_bits(mode) - 1) {
3752 /* -(a >>u (size-1)) = a >>s (size-1) */
3753 ir_node *v = get_Shr_left(a);
3755 n = new_rd_Shrs(get_irn_dbg_info(n), get_nodes_block(n), v, c, mode);
3756 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_PREDICATE);
3762 ir_node *c = get_Shrs_right(a);
3765 tarval *tv = get_Const_tarval(c);
3767 if (tarval_is_long(tv) && get_tarval_long(tv) == (int) get_mode_size_bits(mode) - 1) {
3768 /* -(a >>s (size-1)) = a >>u (size-1) */
3769 ir_node *v = get_Shrs_left(a);
3771 n = new_rd_Shr(get_irn_dbg_info(n), get_nodes_block(n), v, c, mode);
3772 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_PREDICATE);
3779 /* - (a-b) = b - a */
3780 ir_node *la = get_Sub_left(a);
3781 ir_node *ra = get_Sub_right(a);
3782 ir_node *blk = get_nodes_block(n);
3784 n = new_rd_Sub(get_irn_dbg_info(n), blk, ra, la, mode);
3785 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_MINUS_SUB);
3789 if (is_Mul(a)) { /* -(a * const) -> a * -const */
3790 ir_node *mul_l = get_Mul_left(a);
3791 ir_node *mul_r = get_Mul_right(a);
3792 tarval *tv = value_of(mul_r);
3793 if (tv != tarval_bad) {
3794 tv = tarval_neg(tv);
3795 if (tv != tarval_bad) {
3796 ir_node *cnst = new_Const(tv);
3797 dbg_info *dbg = get_irn_dbg_info(a);
3798 ir_node *block = get_nodes_block(a);
3799 n = new_rd_Mul(dbg, block, mul_l, cnst, mode);
3800 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_MINUS_MUL_C);
3807 } /* transform_node_Minus */
3810 * Transform a Cast_type(Const) into a new Const_type
3812 static ir_node *transform_node_Cast(ir_node *n)
3815 ir_node *pred = get_Cast_op(n);
3816 ir_type *tp = get_irn_type(n);
3818 if (is_Const(pred) && get_Const_type(pred) != tp) {
3819 n = new_rd_Const_type(NULL, current_ir_graph, get_Const_tarval(pred), tp);
3820 DBG_OPT_CSTEVAL(oldn, n);
3821 } else if (is_SymConst(pred) && get_SymConst_value_type(pred) != tp) {
3822 n = new_rd_SymConst_type(NULL, current_ir_graph, get_irn_mode(pred),
3823 get_SymConst_symbol(pred), get_SymConst_kind(pred), tp);
3824 DBG_OPT_CSTEVAL(oldn, n);
3828 } /* transform_node_Cast */
3831 * Transform a Proj(Load) with a non-null address.
3833 static ir_node *transform_node_Proj_Load(ir_node *proj)
3835 if (get_opt_ldst_only_null_ptr_exceptions()) {
3836 if (get_irn_mode(proj) == mode_X) {
3837 ir_node *load = get_Proj_pred(proj);
3839 /* get the Load address */
3840 const ir_node *addr = get_Load_ptr(load);
3841 const ir_node *confirm;
3843 if (value_not_null(addr, &confirm)) {
3844 if (confirm == NULL) {
3845 /* this node may float if it did not depend on a Confirm */
3846 set_irn_pinned(load, op_pin_state_floats);
3848 if (get_Proj_proj(proj) == pn_Load_X_except) {
3849 DBG_OPT_EXC_REM(proj);
3850 return get_irg_bad(current_ir_graph);
3852 ir_node *blk = get_nodes_block(load);
3853 return new_r_Jmp(blk);
3859 } /* transform_node_Proj_Load */
3862 * Transform a Proj(Store) with a non-null address.
3864 static ir_node *transform_node_Proj_Store(ir_node *proj)
3866 if (get_opt_ldst_only_null_ptr_exceptions()) {
3867 if (get_irn_mode(proj) == mode_X) {
3868 ir_node *store = get_Proj_pred(proj);
3870 /* get the load/store address */
3871 const ir_node *addr = get_Store_ptr(store);
3872 const ir_node *confirm;
3874 if (value_not_null(addr, &confirm)) {
3875 if (confirm == NULL) {
3876 /* this node may float if it did not depend on a Confirm */
3877 set_irn_pinned(store, op_pin_state_floats);
3879 if (get_Proj_proj(proj) == pn_Store_X_except) {
3880 DBG_OPT_EXC_REM(proj);
3881 return get_irg_bad(current_ir_graph);
3883 ir_node *blk = get_nodes_block(store);
3884 return new_r_Jmp(blk);
3890 } /* transform_node_Proj_Store */
3893 * Transform a Proj(Div) with a non-zero value.
3894 * Removes the exceptions and routes the memory to the NoMem node.
3896 static ir_node *transform_node_Proj_Div(ir_node *proj)
3898 ir_node *div = get_Proj_pred(proj);
3899 ir_node *b = get_Div_right(div);
3900 ir_node *res, *new_mem;
3901 const ir_node *confirm;
3904 if (value_not_zero(b, &confirm)) {
3905 /* div(x, y) && y != 0 */
3906 if (confirm == NULL) {
3907 /* we are sure we have a Const != 0 */
3908 new_mem = get_Div_mem(div);
3909 new_mem = skip_Pin(new_mem);
3910 set_Div_mem(div, new_mem);
3911 set_irn_pinned(div, op_pin_state_floats);
3914 proj_nr = get_Proj_proj(proj);
3916 case pn_Div_X_regular:
3917 return new_r_Jmp(get_nodes_block(div));
3919 case pn_Div_X_except:
3920 /* we found an exception handler, remove it */
3921 DBG_OPT_EXC_REM(proj);
3925 res = get_Div_mem(div);
3926 new_mem = get_irg_no_mem(current_ir_graph);
3929 /* This node can only float up to the Confirm block */
3930 new_mem = new_r_Pin(get_nodes_block(confirm), new_mem);
3932 set_irn_pinned(div, op_pin_state_floats);
3933 /* this is a Div without exception, we can remove the memory edge */
3934 set_Div_mem(div, new_mem);
3939 } /* transform_node_Proj_Div */
3942 * Transform a Proj(Mod) with a non-zero value.
3943 * Removes the exceptions and routes the memory to the NoMem node.
3945 static ir_node *transform_node_Proj_Mod(ir_node *proj)
3947 ir_node *mod = get_Proj_pred(proj);
3948 ir_node *b = get_Mod_right(mod);
3949 ir_node *res, *new_mem;
3950 const ir_node *confirm;
3953 if (value_not_zero(b, &confirm)) {
3954 /* mod(x, y) && y != 0 */
3955 proj_nr = get_Proj_proj(proj);
3957 if (confirm == NULL) {
3958 /* we are sure we have a Const != 0 */
3959 new_mem = get_Mod_mem(mod);
3960 new_mem = skip_Pin(new_mem);
3961 set_Mod_mem(mod, new_mem);
3962 set_irn_pinned(mod, op_pin_state_floats);
3967 case pn_Mod_X_regular:
3968 return new_r_Jmp(get_irn_n(mod, -1));
3970 case pn_Mod_X_except:
3971 /* we found an exception handler, remove it */
3972 DBG_OPT_EXC_REM(proj);
3976 res = get_Mod_mem(mod);
3977 new_mem = get_irg_no_mem(current_ir_graph);
3980 /* This node can only float up to the Confirm block */
3981 new_mem = new_r_Pin(get_nodes_block(confirm), new_mem);
3983 /* this is a Mod without exception, we can remove the memory edge */
3984 set_Mod_mem(mod, new_mem);
3987 if (get_Mod_left(mod) == b) {
3988 /* a % a = 0 if a != 0 */
3989 ir_mode *mode = get_irn_mode(proj);
3990 ir_node *res = new_Const(get_mode_null(mode));
3992 DBG_OPT_CSTEVAL(mod, res);
3998 } /* transform_node_Proj_Mod */
4001 * Transform a Proj(DivMod) with a non-zero value.
4002 * Removes the exceptions and routes the memory to the NoMem node.
4004 static ir_node *transform_node_Proj_DivMod(ir_node *proj)
4006 ir_node *divmod = get_Proj_pred(proj);
4007 ir_node *b = get_DivMod_right(divmod);
4008 ir_node *res, *new_mem;
4009 const ir_node *confirm;
4012 if (value_not_zero(b, &confirm)) {
4013 /* DivMod(x, y) && y != 0 */
4014 proj_nr = get_Proj_proj(proj);
4016 if (confirm == NULL) {
4017 /* we are sure we have a Const != 0 */
4018 new_mem = get_DivMod_mem(divmod);
4019 new_mem = skip_Pin(new_mem);
4020 set_DivMod_mem(divmod, new_mem);
4021 set_irn_pinned(divmod, op_pin_state_floats);
4026 case pn_DivMod_X_regular:
4027 return new_r_Jmp(get_nodes_block(divmod));
4029 case pn_DivMod_X_except:
4030 /* we found an exception handler, remove it */
4031 DBG_OPT_EXC_REM(proj);
4035 res = get_DivMod_mem(divmod);
4036 new_mem = get_irg_no_mem(current_ir_graph);
4039 /* This node can only float up to the Confirm block */
4040 new_mem = new_r_Pin(get_nodes_block(confirm), new_mem);
4042 /* this is a DivMod without exception, we can remove the memory edge */
4043 set_DivMod_mem(divmod, new_mem);
4046 case pn_DivMod_res_mod:
4047 if (get_DivMod_left(divmod) == b) {
4048 /* a % a = 0 if a != 0 */
4049 ir_mode *mode = get_irn_mode(proj);
4050 ir_node *res = new_Const(get_mode_null(mode));
4052 DBG_OPT_CSTEVAL(divmod, res);
4058 } /* transform_node_Proj_DivMod */
4061 * Optimizes jump tables (CondIs or CondIu) by removing all impossible cases.
4063 static ir_node *transform_node_Proj_Cond(ir_node *proj)
4065 if (get_opt_unreachable_code()) {
4066 ir_node *n = get_Proj_pred(proj);
4067 ir_node *b = get_Cond_selector(n);
4069 if (mode_is_int(get_irn_mode(b))) {
4070 tarval *tb = value_of(b);
4072 if (tb != tarval_bad) {
4073 /* we have a constant switch */
4074 long num = get_Proj_proj(proj);
4076 if (num != get_Cond_default_proj(n)) { /* we cannot optimize default Proj's yet */
4077 if (get_tarval_long(tb) == num) {
4078 /* Do NOT create a jump here, or we will have 2 control flow ops
4079 * in a block. This case is optimized away in optimize_cf(). */
4082 /* this case will NEVER be taken, kill it */
4083 return get_irg_bad(current_ir_graph);
4087 long num = get_Proj_proj(proj);
4088 if (num != get_Cond_default_proj(n) && b->vrp.valid) {
4089 /* Try handling with vrp data. We only remove dead parts. */
4090 tarval *tp = new_tarval_from_long(num, get_irn_mode(b));
4092 if (b->vrp.range_type == VRP_RANGE) {
4093 pn_Cmp cmp_result = tarval_cmp(b->vrp.range_bottom, tp);
4094 pn_Cmp cmp_result2 = tarval_cmp(b->vrp.range_top, tp);
4096 if ((cmp_result & pn_Cmp_Lt) == cmp_result && (cmp_result2
4097 & pn_Cmp_Gt) == cmp_result2) {
4098 return get_irg_bad(current_ir_graph);
4100 } else if (b->vrp.range_type == VRP_ANTIRANGE) {
4101 pn_Cmp cmp_result = tarval_cmp(b->vrp.range_bottom, tp);
4102 pn_Cmp cmp_result2 = tarval_cmp(b->vrp.range_top, tp);
4104 if ((cmp_result & pn_Cmp_Ge) == cmp_result && (cmp_result2
4105 & pn_Cmp_Le) == cmp_result2) {
4106 return get_irg_bad(current_ir_graph);
4111 tarval_and( b->vrp.bits_set, tp),
4115 return get_irg_bad(current_ir_graph);
4121 b->vrp.bits_not_set),
4122 b->vrp.bits_not_set)
4125 return get_irg_bad(current_ir_graph);
4134 } /* transform_node_Proj_Cond */
4137 * Create a 0 constant of given mode.
4139 static ir_node *create_zero_const(ir_mode *mode)
4141 tarval *tv = get_mode_null(mode);
4142 ir_node *cnst = new_Const(tv);
4147 /* the order of the values is important! */
4148 typedef enum const_class {
4154 static const_class classify_const(const ir_node* n)
4156 if (is_Const(n)) return const_const;
4157 if (is_irn_constlike(n)) return const_like;
4162 * Determines whether r is more constlike or has a larger index (in that order)
4165 static int operands_are_normalized(const ir_node *l, const ir_node *r)
4167 const const_class l_order = classify_const(l);
4168 const const_class r_order = classify_const(r);
4170 l_order > r_order ||
4171 (l_order == r_order && get_irn_idx(l) <= get_irn_idx(r));
4175 * Normalizes and optimizes Cmp nodes.
4177 static ir_node *transform_node_Proj_Cmp(ir_node *proj)
4179 ir_node *n = get_Proj_pred(proj);
4180 ir_node *left = get_Cmp_left(n);
4181 ir_node *right = get_Cmp_right(n);
4184 ir_mode *mode = NULL;
4185 long proj_nr = get_Proj_proj(proj);
4187 /* we can evaluate some cases directly */
4190 return new_Const(get_tarval_b_false());
4192 return new_Const(get_tarval_b_true());
4194 if (!mode_is_float(get_irn_mode(left)))
4195 return new_Const(get_tarval_b_true());
4201 /* remove Casts of both sides */
4202 left = skip_Cast(left);
4203 right = skip_Cast(right);
4205 /* Remove unnecessary conversions */
4206 /* TODO handle constants */
4207 if (is_Conv(left) && is_Conv(right)) {
4208 ir_mode *mode = get_irn_mode(left);
4209 ir_node *op_left = get_Conv_op(left);
4210 ir_node *op_right = get_Conv_op(right);
4211 ir_mode *mode_left = get_irn_mode(op_left);
4212 ir_mode *mode_right = get_irn_mode(op_right);
4214 if (smaller_mode(mode_left, mode) && smaller_mode(mode_right, mode)
4215 && mode_left != mode_b && mode_right != mode_b) {
4216 ir_node *block = get_nodes_block(n);
4218 if (mode_left == mode_right) {
4222 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV_CONV);
4223 } else if (smaller_mode(mode_left, mode_right)) {
4224 left = new_r_Conv(block, op_left, mode_right);
4227 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV);
4228 } else if (smaller_mode(mode_right, mode_left)) {
4230 right = new_r_Conv(block, op_right, mode_left);
4232 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV);
4237 /* remove operation on both sides if possible */
4238 if (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) {
4240 * The following operations are NOT safe for floating point operations, for instance
4241 * 1.0 + inf == 2.0 + inf, =/=> x == y
4243 if (mode_is_int(get_irn_mode(left))) {
4244 unsigned lop = get_irn_opcode(left);
4246 if (lop == get_irn_opcode(right)) {
4247 ir_node *ll, *lr, *rl, *rr;
4249 /* same operation on both sides, try to remove */
4253 /* ~a CMP ~b => a CMP b, -a CMP -b ==> a CMP b */
4254 left = get_unop_op(left);
4255 right = get_unop_op(right);
4257 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4260 ll = get_Add_left(left);
4261 lr = get_Add_right(left);
4262 rl = get_Add_left(right);
4263 rr = get_Add_right(right);
4266 /* X + a CMP X + b ==> a CMP b */
4270 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4271 } else if (ll == rr) {
4272 /* X + a CMP b + X ==> a CMP b */
4276 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4277 } else if (lr == rl) {
4278 /* a + X CMP X + b ==> a CMP b */
4282 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4283 } else if (lr == rr) {
4284 /* a + X CMP b + X ==> a CMP b */
4288 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4292 ll = get_Sub_left(left);
4293 lr = get_Sub_right(left);
4294 rl = get_Sub_left(right);
4295 rr = get_Sub_right(right);
4298 /* X - a CMP X - b ==> a CMP b */
4302 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4303 } else if (lr == rr) {
4304 /* a - X CMP b - X ==> a CMP b */
4308 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4312 if (get_Rotl_right(left) == get_Rotl_right(right)) {
4313 /* a ROTL X CMP b ROTL X ==> a CMP b */
4314 left = get_Rotl_left(left);
4315 right = get_Rotl_left(right);
4317 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4325 /* X+A == A, A+X == A, A-X == A -> X == 0 */
4326 if (is_Add(left) || is_Sub(left)) {
4327 ir_node *ll = get_binop_left(left);
4328 ir_node *lr = get_binop_right(left);
4330 if (lr == right && is_Add(left)) {
4337 right = create_zero_const(get_irn_mode(left));
4339 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4342 if (is_Add(right) || is_Sub(right)) {
4343 ir_node *rl = get_binop_left(right);
4344 ir_node *rr = get_binop_right(right);
4346 if (rr == left && is_Add(right)) {
4353 right = create_zero_const(get_irn_mode(left));
4355 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4358 if (is_And(left) && is_Const(right)) {
4359 ir_node *ll = get_binop_left(left);
4360 ir_node *lr = get_binop_right(left);
4361 if (is_Shr(ll) && is_Const(lr)) {
4362 /* Cmp((x >>u c1) & c2, c3) = Cmp(x & (c2 << c1), c3 << c1) */
4363 ir_node *block = get_nodes_block(n);
4364 ir_mode *mode = get_irn_mode(left);
4366 ir_node *llr = get_Shr_right(ll);
4367 if (is_Const(llr)) {
4368 dbg_info *dbg = get_irn_dbg_info(left);
4370 tarval *c1 = get_Const_tarval(llr);
4371 tarval *c2 = get_Const_tarval(lr);
4372 tarval *c3 = get_Const_tarval(right);
4373 tarval *mask = tarval_shl(c2, c1);
4374 tarval *value = tarval_shl(c3, c1);
4376 left = new_rd_And(dbg, block, get_Shr_left(ll), new_Const(mask), mode);
4377 right = new_Const(value);
4382 } /* mode_is_int(...) */
4383 } /* proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg */
4385 /* replace mode_b compares with ands/ors */
4386 if (get_irn_mode(left) == mode_b) {
4387 ir_node *block = get_nodes_block(n);
4391 case pn_Cmp_Le: bres = new_r_Or( block, new_r_Not(block, left, mode_b), right, mode_b); break;
4392 case pn_Cmp_Lt: bres = new_r_And(block, new_r_Not(block, left, mode_b), right, mode_b); break;
4393 case pn_Cmp_Ge: bres = new_r_Or( block, left, new_r_Not(block, right, mode_b), mode_b); break;
4394 case pn_Cmp_Gt: bres = new_r_And(block, left, new_r_Not(block, right, mode_b), mode_b); break;
4395 case pn_Cmp_Lg: bres = new_r_Eor(block, left, right, mode_b); break;
4396 case pn_Cmp_Eq: bres = new_r_Not(block, new_r_Eor(block, left, right, mode_b), mode_b); break;
4397 default: bres = NULL;
4400 DBG_OPT_ALGSIM0(n, bres, FS_OPT_CMP_TO_BOOL);
4406 * First step: normalize the compare op
4407 * by placing the constant on the right side
4408 * or moving the lower address node to the left.
4410 if (!operands_are_normalized(left, right)) {
4416 proj_nr = get_inversed_pnc(proj_nr);
4421 * Second step: Try to reduce the magnitude
4422 * of a constant. This may help to generate better code
4423 * later and may help to normalize more compares.
4424 * Of course this is only possible for integer values.
4426 tv = value_of(right);
4427 if (tv != tarval_bad) {
4428 mode = get_irn_mode(right);
4430 /* TODO extend to arbitrary constants */
4431 if (is_Conv(left) && tarval_is_null(tv)) {
4432 ir_node *op = get_Conv_op(left);
4433 ir_mode *op_mode = get_irn_mode(op);
4436 * UpConv(x) REL 0 ==> x REL 0
4437 * Don't do this for float values as it's unclear whether it is a
4438 * win. (on the other side it makes detection/creation of fabs hard)
4440 if (get_mode_size_bits(mode) > get_mode_size_bits(op_mode) &&
4441 ((proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) ||
4442 mode_is_signed(mode) || !mode_is_signed(op_mode)) &&
4443 !mode_is_float(mode)) {
4444 tv = get_mode_null(op_mode);
4448 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV);
4452 if (tv != tarval_bad) {
4453 /* the following optimization is possible on modes without Overflow
4454 * on Unary Minus or on == and !=:
4455 * -a CMP c ==> a swap(CMP) -c
4457 * Beware: for two-complement Overflow may occur, so only == and != can
4458 * be optimized, see this:
4459 * -MININT < 0 =/=> MININT > 0 !!!
4461 if (is_Minus(left) &&
4462 (!mode_overflow_on_unary_Minus(mode) ||
4463 (mode_is_int(mode) && (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg)))) {
4464 tv = tarval_neg(tv);
4466 if (tv != tarval_bad) {
4467 left = get_Minus_op(left);
4468 proj_nr = get_inversed_pnc(proj_nr);
4470 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4472 } else if (is_Not(left) && (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg)) {
4473 /* Not(a) ==/!= c ==> a ==/!= Not(c) */
4474 tv = tarval_not(tv);
4476 if (tv != tarval_bad) {
4477 left = get_Not_op(left);
4479 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4483 /* for integer modes, we have more */
4484 if (mode_is_int(mode)) {
4485 /* Ne includes Unordered which is not possible on integers.
4486 * However, frontends often use this wrong, so fix it here */
4487 if (proj_nr & pn_Cmp_Uo) {
4488 proj_nr &= ~pn_Cmp_Uo;
4489 set_Proj_proj(proj, proj_nr);
4492 /* c > 0 : a < c ==> a <= (c-1) a >= c ==> a > (c-1) */
4493 if ((proj_nr == pn_Cmp_Lt || proj_nr == pn_Cmp_Ge) &&
4494 tarval_cmp(tv, get_mode_null(mode)) == pn_Cmp_Gt) {
4495 tv = tarval_sub(tv, get_mode_one(mode), NULL);
4497 if (tv != tarval_bad) {
4498 proj_nr ^= pn_Cmp_Eq;
4500 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CNST_MAGN);
4503 /* c < 0 : a > c ==> a >= (c+1) a <= c ==> a < (c+1) */
4504 else if ((proj_nr == pn_Cmp_Gt || proj_nr == pn_Cmp_Le) &&
4505 tarval_cmp(tv, get_mode_null(mode)) == pn_Cmp_Lt) {
4506 tv = tarval_add(tv, get_mode_one(mode));
4508 if (tv != tarval_bad) {
4509 proj_nr ^= pn_Cmp_Eq;
4511 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CNST_MAGN);
4515 /* the following reassociations work only for == and != */
4516 if (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) {
4518 #if 0 /* Might be not that good in general */
4519 /* a-b == 0 ==> a == b, a-b != 0 ==> a != b */
4520 if (tarval_is_null(tv) && is_Sub(left)) {
4521 right = get_Sub_right(left);
4522 left = get_Sub_left(left);
4524 tv = value_of(right);
4526 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4530 if (tv != tarval_bad) {
4531 /* a-c1 == c2 ==> a == c2+c1, a-c1 != c2 ==> a != c2+c1 */
4533 ir_node *c1 = get_Sub_right(left);
4534 tarval *tv2 = value_of(c1);
4536 if (tv2 != tarval_bad) {
4537 tv2 = tarval_add(tv, value_of(c1));
4539 if (tv2 != tarval_bad) {
4540 left = get_Sub_left(left);
4543 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4547 /* a+c1 == c2 ==> a == c2-c1, a+c1 != c2 ==> a != c2-c1 */
4548 else if (is_Add(left)) {
4549 ir_node *a_l = get_Add_left(left);
4550 ir_node *a_r = get_Add_right(left);
4554 if (is_Const(a_l)) {
4556 tv2 = value_of(a_l);
4559 tv2 = value_of(a_r);
4562 if (tv2 != tarval_bad) {
4563 tv2 = tarval_sub(tv, tv2, NULL);
4565 if (tv2 != tarval_bad) {
4569 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4573 /* -a == c ==> a == -c, -a != c ==> a != -c */
4574 else if (is_Minus(left)) {
4575 tarval *tv2 = tarval_sub(get_mode_null(mode), tv, NULL);
4577 if (tv2 != tarval_bad) {
4578 left = get_Minus_op(left);
4581 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4586 /* the following reassociations work only for <= */
4587 else if (proj_nr == pn_Cmp_Le || proj_nr == pn_Cmp_Lt) {
4588 if (tv != tarval_bad) {
4589 /* c >= 0 : Abs(a) <= c ==> (unsigned)(a + c) <= 2*c */
4590 if (is_Abs(left)) { // TODO something is missing here
4596 if (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) {
4597 switch (get_irn_opcode(left)) {
4601 c1 = get_And_right(left);
4604 * And(x, C1) == C2 ==> FALSE if C2 & C1 != C2
4605 * And(x, C1) != C2 ==> TRUE if C2 & C1 != C2
4607 tarval *mask = tarval_and(get_Const_tarval(c1), tv);
4609 /* TODO: move to constant evaluation */
4610 tv = proj_nr == pn_Cmp_Eq ? get_tarval_b_false() : get_tarval_b_true();
4612 DBG_OPT_CSTEVAL(proj, c1);
4616 if (tarval_is_single_bit(tv)) {
4618 * optimization for AND:
4620 * And(x, C) == C ==> And(x, C) != 0
4621 * And(x, C) != C ==> And(X, C) == 0
4623 * if C is a single Bit constant.
4626 /* check for Constant's match. We have check hare the tarvals,
4627 because our const might be changed */
4628 if (get_Const_tarval(c1) == tv) {
4629 /* fine: do the transformation */
4630 tv = get_mode_null(get_tarval_mode(tv));
4631 proj_nr ^= pn_Cmp_Leg;
4633 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CNST_MAGN);
4639 c1 = get_Or_right(left);
4640 if (is_Const(c1) && tarval_is_null(tv)) {
4642 * Or(x, C) == 0 && C != 0 ==> FALSE
4643 * Or(x, C) != 0 && C != 0 ==> TRUE
4645 if (! tarval_is_null(get_Const_tarval(c1))) {
4646 /* TODO: move to constant evaluation */
4647 tv = proj_nr == pn_Cmp_Eq ? get_tarval_b_false() : get_tarval_b_true();
4649 DBG_OPT_CSTEVAL(proj, c1);
4656 * optimize x << c1 == c into x & (-1 >>u c1) == c >> c1 if c & (-1 << c1) == c
4658 * optimize x << c1 != c into x & (-1 >>u c1) != c >> c1 if c & (-1 << c1) == c
4661 c1 = get_Shl_right(left);
4663 tarval *tv1 = get_Const_tarval(c1);
4664 ir_mode *mode = get_irn_mode(left);
4665 tarval *minus1 = get_mode_all_one(mode);
4666 tarval *amask = tarval_shr(minus1, tv1);
4667 tarval *cmask = tarval_shl(minus1, tv1);
4670 if (tarval_and(tv, cmask) != tv) {
4671 /* condition not met */
4672 tv = proj_nr == pn_Cmp_Eq ? get_tarval_b_false() : get_tarval_b_true();
4674 DBG_OPT_CSTEVAL(proj, c1);
4677 sl = get_Shl_left(left);
4678 blk = get_nodes_block(n);
4679 left = new_rd_And(get_irn_dbg_info(left), blk, sl, new_Const(amask), mode);
4680 tv = tarval_shr(tv, tv1);
4682 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_SHF_TO_AND);
4687 * optimize x >>u c1 == c into x & (-1 << c1) == c << c1 if c & (-1 >>u c1) == c
4689 * optimize x >>u c1 != c into x & (-1 << c1) != c << c1 if c & (-1 >>u c1) == c
4692 c1 = get_Shr_right(left);
4694 tarval *tv1 = get_Const_tarval(c1);
4695 ir_mode *mode = get_irn_mode(left);
4696 tarval *minus1 = get_mode_all_one(mode);
4697 tarval *amask = tarval_shl(minus1, tv1);
4698 tarval *cmask = tarval_shr(minus1, tv1);
4701 if (tarval_and(tv, cmask) != tv) {
4702 /* condition not met */
4703 tv = proj_nr == pn_Cmp_Eq ? get_tarval_b_false() : get_tarval_b_true();
4705 DBG_OPT_CSTEVAL(proj, c1);
4708 sl = get_Shr_left(left);
4709 blk = get_nodes_block(n);
4710 left = new_rd_And(get_irn_dbg_info(left), blk, sl, new_Const(amask), mode);
4711 tv = tarval_shl(tv, tv1);
4713 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_SHF_TO_AND);
4718 * optimize x >>s c1 == c into x & (-1 << c1) == c << c1 if (c >>s (BITS - c1)) \in {0,-1}
4720 * optimize x >>s c1 != c into x & (-1 << c1) != c << c1 if (c >>s (BITS - c1)) \in {0,-1}
4723 c1 = get_Shrs_right(left);
4725 tarval *tv1 = get_Const_tarval(c1);
4726 ir_mode *mode = get_irn_mode(left);
4727 tarval *minus1 = get_mode_all_one(mode);
4728 tarval *amask = tarval_shl(minus1, tv1);
4729 tarval *cond = new_tarval_from_long(get_mode_size_bits(mode), get_tarval_mode(tv1));
4732 cond = tarval_sub(cond, tv1, NULL);
4733 cond = tarval_shrs(tv, cond);
4735 if (!tarval_is_all_one(cond) && !tarval_is_null(cond)) {
4736 /* condition not met */
4737 tv = proj_nr == pn_Cmp_Eq ? get_tarval_b_false() : get_tarval_b_true();
4739 DBG_OPT_CSTEVAL(proj, c1);
4742 sl = get_Shrs_left(left);
4743 blk = get_nodes_block(n);
4744 left = new_rd_And(get_irn_dbg_info(left), blk, sl, new_Const(amask), mode);
4745 tv = tarval_shl(tv, tv1);
4747 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_SHF_TO_AND);
4752 } /* tarval != bad */
4755 if (changed & 2) /* need a new Const */
4756 right = new_Const(tv);
4758 if ((proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) && is_Const(right) && is_Const_null(right) && is_Proj(left)) {
4759 ir_node *op = get_Proj_pred(left);
4761 if ((is_Mod(op) && get_Proj_proj(left) == pn_Mod_res) ||
4762 (is_DivMod(op) && get_Proj_proj(left) == pn_DivMod_res_mod)) {
4763 ir_node *c = get_binop_right(op);
4766 tarval *tv = get_Const_tarval(c);
4768 if (tarval_is_single_bit(tv)) {
4769 /* special case: (x % 2^n) CMP 0 ==> x & (2^n-1) CMP 0 */
4770 ir_node *v = get_binop_left(op);
4771 ir_node *blk = get_irn_n(op, -1);
4772 ir_mode *mode = get_irn_mode(v);
4774 tv = tarval_sub(tv, get_mode_one(mode), NULL);
4775 left = new_rd_And(get_irn_dbg_info(op), blk, v, new_Const(tv), mode);
4777 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_MOD_TO_AND);
4784 ir_node *block = get_nodes_block(n);
4786 /* create a new compare */
4787 n = new_rd_Cmp(get_irn_dbg_info(n), block, left, right);
4788 proj = new_rd_Proj(get_irn_dbg_info(proj), block, n, get_irn_mode(proj), proj_nr);
4792 } /* transform_node_Proj_Cmp */
4795 * Optimize CopyB(mem, x, x) into a Nop.
4797 static ir_node *transform_node_Proj_CopyB(ir_node *proj)
4799 ir_node *copyb = get_Proj_pred(proj);
4800 ir_node *a = get_CopyB_dst(copyb);
4801 ir_node *b = get_CopyB_src(copyb);
4804 switch (get_Proj_proj(proj)) {
4805 case pn_CopyB_X_regular:
4806 /* Turn CopyB into a tuple (mem, jmp, bad, bad) */
4807 DBG_OPT_EXC_REM(proj);
4808 proj = new_r_Jmp(get_nodes_block(copyb));
4810 case pn_CopyB_X_except:
4811 DBG_OPT_EXC_REM(proj);
4812 proj = get_irg_bad(get_irn_irg(proj));
4819 } /* transform_node_Proj_CopyB */
4822 * Optimize Bounds(idx, idx, upper) into idx.
4824 static ir_node *transform_node_Proj_Bound(ir_node *proj)
4826 ir_node *oldn = proj;
4827 ir_node *bound = get_Proj_pred(proj);
4828 ir_node *idx = get_Bound_index(bound);
4829 ir_node *pred = skip_Proj(idx);
4832 if (idx == get_Bound_lower(bound))
4834 else if (is_Bound(pred)) {
4836 * idx was Bounds checked in the same MacroBlock previously,
4837 * it is still valid if lower <= pred_lower && pred_upper <= upper.
4839 ir_node *lower = get_Bound_lower(bound);
4840 ir_node *upper = get_Bound_upper(bound);
4841 if (get_Bound_lower(pred) == lower &&
4842 get_Bound_upper(pred) == upper &&
4843 get_irn_MacroBlock(bound) == get_irn_MacroBlock(pred)) {
4845 * One could expect that we simply return the previous
4846 * Bound here. However, this would be wrong, as we could
4847 * add an exception Proj to a new location then.
4848 * So, we must turn in into a tuple.
4854 /* Turn Bound into a tuple (mem, jmp, bad, idx) */
4855 switch (get_Proj_proj(proj)) {
4857 DBG_OPT_EXC_REM(proj);
4858 proj = get_Bound_mem(bound);
4860 case pn_Bound_X_except:
4861 DBG_OPT_EXC_REM(proj);
4862 proj = get_irg_bad(get_irn_irg(proj));
4866 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NOP);
4868 case pn_Bound_X_regular:
4869 DBG_OPT_EXC_REM(proj);
4870 proj = new_r_Jmp(get_nodes_block(bound));
4877 } /* transform_node_Proj_Bound */
4880 * Does all optimizations on nodes that must be done on it's Proj's
4881 * because of creating new nodes.
4883 static ir_node *transform_node_Proj(ir_node *proj)
4885 ir_node *n = get_Proj_pred(proj);
4887 if (n->op->ops.transform_node_Proj)
4888 return n->op->ops.transform_node_Proj(proj);
4890 } /* transform_node_Proj */
4893 * Move Confirms down through Phi nodes.
4895 static ir_node *transform_node_Phi(ir_node *phi)
4898 ir_mode *mode = get_irn_mode(phi);
4900 if (mode_is_reference(mode)) {
4901 n = get_irn_arity(phi);
4903 /* Beware of Phi0 */
4905 ir_node *pred = get_irn_n(phi, 0);
4906 ir_node *bound, *new_Phi, *block, **in;
4909 if (! is_Confirm(pred))
4912 bound = get_Confirm_bound(pred);
4913 pnc = get_Confirm_cmp(pred);
4915 NEW_ARR_A(ir_node *, in, n);
4916 in[0] = get_Confirm_value(pred);
4918 for (i = 1; i < n; ++i) {
4919 pred = get_irn_n(phi, i);
4921 if (! is_Confirm(pred) ||
4922 get_Confirm_bound(pred) != bound ||
4923 get_Confirm_cmp(pred) != pnc)
4925 in[i] = get_Confirm_value(pred);
4927 /* move the Confirm nodes "behind" the Phi */
4928 block = get_irn_n(phi, -1);
4929 new_Phi = new_r_Phi(block, n, in, get_irn_mode(phi));
4930 return new_r_Confirm(block, new_Phi, bound, pnc);
4934 } /* transform_node_Phi */
4937 * Returns the operands of a commutative bin-op, if one operand is
4938 * a const, it is returned as the second one.
4940 static void get_comm_Binop_Ops(ir_node *binop, ir_node **a, ir_node **c)
4942 ir_node *op_a = get_binop_left(binop);
4943 ir_node *op_b = get_binop_right(binop);
4945 assert(is_op_commutative(get_irn_op(binop)));
4947 if (is_Const(op_a)) {
4954 } /* get_comm_Binop_Ops */
4957 * Optimize a Or(And(Or(And(v,c4),c3),c2),c1) pattern if possible.
4958 * Such pattern may arise in bitfield stores.
4960 * value c4 value c4 & c2
4961 * AND c3 AND c1 | c3
4968 * AND c1 ===> OR if (c1 | c2) == 0x111..11
4971 static ir_node *transform_node_Or_bf_store(ir_node *or)
4975 ir_node *and_l, *c3;
4976 ir_node *value, *c4;
4977 ir_node *new_and, *new_const, *block;
4978 ir_mode *mode = get_irn_mode(or);
4980 tarval *tv1, *tv2, *tv3, *tv4, *tv;
4983 get_comm_Binop_Ops(or, &and, &c1);
4984 if (!is_Const(c1) || !is_And(and))
4987 get_comm_Binop_Ops(and, &or_l, &c2);
4991 tv1 = get_Const_tarval(c1);
4992 tv2 = get_Const_tarval(c2);
4994 tv = tarval_or(tv1, tv2);
4995 if (tarval_is_all_one(tv)) {
4996 /* the AND does NOT clear a bit with isn't set by the OR */
4997 set_Or_left(or, or_l);
4998 set_Or_right(or, c1);
5000 /* check for more */
5007 get_comm_Binop_Ops(or_l, &and_l, &c3);
5008 if (!is_Const(c3) || !is_And(and_l))
5011 get_comm_Binop_Ops(and_l, &value, &c4);
5015 /* ok, found the pattern, check for conditions */
5016 assert(mode == get_irn_mode(and));
5017 assert(mode == get_irn_mode(or_l));
5018 assert(mode == get_irn_mode(and_l));
5020 tv3 = get_Const_tarval(c3);
5021 tv4 = get_Const_tarval(c4);
5023 tv = tarval_or(tv4, tv2);
5024 if (!tarval_is_all_one(tv)) {
5025 /* have at least one 0 at the same bit position */
5029 if (tv3 != tarval_andnot(tv3, tv4)) {
5030 /* bit in the or_mask is outside the and_mask */
5034 if (tv1 != tarval_andnot(tv1, tv2)) {
5035 /* bit in the or_mask is outside the and_mask */
5039 /* ok, all conditions met */
5040 block = get_irn_n(or, -1);
5042 new_and = new_r_And(block, value, new_Const(tarval_and(tv4, tv2)), mode);
5044 new_const = new_Const(tarval_or(tv3, tv1));
5046 set_Or_left(or, new_and);
5047 set_Or_right(or, new_const);
5049 /* check for more */
5051 } /* transform_node_Or_bf_store */
5054 * Optimize an Or(shl(x, c), shr(x, bits - c)) into a Rotl
5056 static ir_node *transform_node_Or_Rotl(ir_node *or)
5058 ir_mode *mode = get_irn_mode(or);
5059 ir_node *shl, *shr, *block;
5060 ir_node *irn, *x, *c1, *c2, *v, *sub, *n, *rotval;
5063 if (! mode_is_int(mode))
5066 shl = get_binop_left(or);
5067 shr = get_binop_right(or);
5076 } else if (!is_Shl(shl)) {
5078 } else if (!is_Shr(shr)) {
5081 x = get_Shl_left(shl);
5082 if (x != get_Shr_left(shr))
5085 c1 = get_Shl_right(shl);
5086 c2 = get_Shr_right(shr);
5087 if (is_Const(c1) && is_Const(c2)) {
5088 tv1 = get_Const_tarval(c1);
5089 if (! tarval_is_long(tv1))
5092 tv2 = get_Const_tarval(c2);
5093 if (! tarval_is_long(tv2))
5096 if (get_tarval_long(tv1) + get_tarval_long(tv2)
5097 != (int) get_mode_size_bits(mode))
5100 /* yet, condition met */
5101 block = get_nodes_block(or);
5103 n = new_r_Rotl(block, x, c1, mode);
5105 DBG_OPT_ALGSIM1(or, shl, shr, n, FS_OPT_OR_SHFT_TO_ROTL);
5112 rotval = sub; /* a Rot right is not supported, so use a rot left */
5113 } else if (is_Sub(c2)) {
5119 if (get_Sub_right(sub) != v)
5122 c1 = get_Sub_left(sub);
5126 tv1 = get_Const_tarval(c1);
5127 if (! tarval_is_long(tv1))
5130 if (get_tarval_long(tv1) != (int) get_mode_size_bits(mode))
5133 /* yet, condition met */
5134 block = get_nodes_block(or);
5136 n = new_r_Rotl(block, x, rotval, mode);
5138 DBG_OPT_ALGSIM0(or, n, FS_OPT_OR_SHFT_TO_ROTL);
5140 } /* transform_node_Or_Rotl */
5145 static ir_node *transform_node_Or(ir_node *n)
5147 ir_node *c, *oldn = n;
5148 ir_node *a = get_Or_left(n);
5149 ir_node *b = get_Or_right(n);
5152 if (is_Not(a) && is_Not(b)) {
5153 /* ~a | ~b = ~(a&b) */
5154 ir_node *block = get_nodes_block(n);
5156 mode = get_irn_mode(n);
5159 n = new_rd_And(get_irn_dbg_info(n), block, a, b, mode);
5160 n = new_rd_Not(get_irn_dbg_info(n), block, n, mode);
5161 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_DEMORGAN);
5165 /* we can evaluate 2 Projs of the same Cmp */
5166 if (get_irn_mode(n) == mode_b && is_Proj(a) && is_Proj(b)) {
5167 ir_node *pred_a = get_Proj_pred(a);
5168 ir_node *pred_b = get_Proj_pred(b);
5169 if (pred_a == pred_b) {
5170 dbg_info *dbgi = get_irn_dbg_info(n);
5171 ir_node *block = get_nodes_block(pred_a);
5172 pn_Cmp pn_a = get_Proj_proj(a);
5173 pn_Cmp pn_b = get_Proj_proj(b);
5174 /* yes, we can simply calculate with pncs */
5175 pn_Cmp new_pnc = pn_a | pn_b;
5177 return new_rd_Proj(dbgi, block, pred_a, mode_b, new_pnc);
5181 mode = get_irn_mode(n);
5182 HANDLE_BINOP_PHI((eval_func) tarval_or, a, b, c, mode);
5184 n = transform_node_Or_bf_store(n);
5185 n = transform_node_Or_Rotl(n);
5189 n = transform_bitwise_distributive(n, transform_node_Or);
5192 } /* transform_node_Or */
5196 static ir_node *transform_node(ir_node *n);
5199 * Optimize (a >> c1) >> c2), works for Shr, Shrs, Shl, Rotl.
5201 * Should be moved to reassociation?
5203 static ir_node *transform_node_shift(ir_node *n)
5205 ir_node *left, *right;
5207 tarval *tv1, *tv2, *res;
5208 ir_node *in[2], *irn, *block;
5210 left = get_binop_left(n);
5212 /* different operations */
5213 if (get_irn_op(left) != get_irn_op(n))
5216 right = get_binop_right(n);
5217 tv1 = value_of(right);
5218 if (tv1 == tarval_bad)
5221 tv2 = value_of(get_binop_right(left));
5222 if (tv2 == tarval_bad)
5225 res = tarval_add(tv1, tv2);
5226 mode = get_irn_mode(n);
5228 /* beware: a simple replacement works only, if res < modulo shift */
5230 int modulo_shf = get_mode_modulo_shift(mode);
5231 if (modulo_shf > 0) {
5232 tarval *modulo = new_tarval_from_long(modulo_shf,
5233 get_tarval_mode(res));
5235 assert(modulo_shf >= (int) get_mode_size_bits(mode));
5237 /* shifting too much */
5238 if (!(tarval_cmp(res, modulo) & pn_Cmp_Lt)) {
5240 ir_node *block = get_nodes_block(n);
5241 dbg_info *dbgi = get_irn_dbg_info(n);
5242 ir_mode *smode = get_irn_mode(right);
5243 ir_node *cnst = new_Const_long(smode, get_mode_size_bits(mode) - 1);
5244 return new_rd_Shrs(dbgi, block, get_binop_left(left), cnst, mode);
5247 return new_Const(get_mode_null(mode));
5251 res = tarval_mod(res, new_tarval_from_long(get_mode_size_bits(mode), get_tarval_mode(res)));
5254 /* ok, we can replace it */
5255 block = get_nodes_block(n);
5257 in[0] = get_binop_left(left);
5258 in[1] = new_Const(res);
5260 irn = new_ir_node(NULL, get_Block_irg(block), block, get_irn_op(n), mode, 2, in);
5262 DBG_OPT_ALGSIM0(n, irn, FS_OPT_REASSOC_SHIFT);
5264 return transform_node(irn);
5265 } /* transform_node_shift */
5268 * normalisation: (x & c1) >> c2 to (x >> c2) & (c1 >> c2)
5270 * - and, or, xor instead of &
5271 * - Shl, Shr, Shrs, rotl instead of >>
5272 * (with a special case for Or/Xor + Shrs)
5274 static ir_node *transform_node_bitop_shift(ir_node *n)
5277 ir_node *right = get_binop_right(n);
5278 ir_mode *mode = get_irn_mode(n);
5279 ir_node *bitop_left;
5280 ir_node *bitop_right;
5291 assert(is_Shrs(n) || is_Shr(n) || is_Shl(n) || is_Rotl(n));
5293 if (!is_Const(right))
5296 left = get_binop_left(n);
5297 op_left = get_irn_op(left);
5298 if (op_left != op_And && op_left != op_Or && op_left != op_Eor)
5301 /* doing it with Shrs is not legal if the Or/Eor affects the topmost bit */
5302 if (is_Shrs(n) && (op_left == op_Or || op_left == op_Eor)) {
5303 /* TODO: test if sign bit is affectes */
5307 bitop_right = get_binop_right(left);
5308 if (!is_Const(bitop_right))
5311 bitop_left = get_binop_left(left);
5313 block = get_nodes_block(n);
5314 dbgi = get_irn_dbg_info(n);
5315 tv1 = get_Const_tarval(bitop_right);
5316 tv2 = get_Const_tarval(right);
5318 assert(get_tarval_mode(tv1) == mode);
5321 new_shift = new_rd_Shl(dbgi, block, bitop_left, right, mode);
5322 tv_shift = tarval_shl(tv1, tv2);
5323 } else if (is_Shr(n)) {
5324 new_shift = new_rd_Shr(dbgi, block, bitop_left, right, mode);
5325 tv_shift = tarval_shr(tv1, tv2);
5326 } else if (is_Shrs(n)) {
5327 new_shift = new_rd_Shrs(dbgi, block, bitop_left, right, mode);
5328 tv_shift = tarval_shrs(tv1, tv2);
5331 new_shift = new_rd_Rotl(dbgi, block, bitop_left, right, mode);
5332 tv_shift = tarval_rotl(tv1, tv2);
5335 assert(get_tarval_mode(tv_shift) == mode);
5336 new_const = new_Const(tv_shift);
5338 if (op_left == op_And) {
5339 new_bitop = new_rd_And(dbgi, block, new_shift, new_const, mode);
5340 } else if (op_left == op_Or) {
5341 new_bitop = new_rd_Or(dbgi, block, new_shift, new_const, mode);
5343 assert(op_left == op_Eor);
5344 new_bitop = new_rd_Eor(dbgi, block, new_shift, new_const, mode);
5352 * (x << c1) >> c2 <=> x OP (c2-c1) & ((-1 << c1) >> c2)
5354 * (x >> c1) << c2 <=> x OP (c2-c1) & ((-1 >> c1) << c2)
5355 * (also with x >>s c1 when c1>=c2)
5357 static ir_node *transform_node_shl_shr(ir_node *n)
5360 ir_node *right = get_binop_right(n);
5376 assert(is_Shl(n) || is_Shr(n) || is_Shrs(n));
5378 if (!is_Const(right))
5381 left = get_binop_left(n);
5382 mode = get_irn_mode(n);
5383 if (is_Shl(n) && (is_Shr(left) || is_Shrs(left))) {
5384 ir_node *shr_right = get_binop_right(left);
5386 if (!is_Const(shr_right))
5389 x = get_binop_left(left);
5390 tv_shr = get_Const_tarval(shr_right);
5391 tv_shl = get_Const_tarval(right);
5393 if (is_Shrs(left)) {
5394 /* shrs variant only allowed if c1 >= c2 */
5395 if (! (tarval_cmp(tv_shl, tv_shr) & pn_Cmp_Ge))
5398 tv_mask = tarval_shrs(get_mode_all_one(mode), tv_shr);
5401 tv_mask = tarval_shr(get_mode_all_one(mode), tv_shr);
5403 tv_mask = tarval_shl(tv_mask, tv_shl);
5404 } else if (is_Shr(n) && is_Shl(left)) {
5405 ir_node *shl_right = get_Shl_right(left);
5407 if (!is_Const(shl_right))
5410 x = get_Shl_left(left);
5411 tv_shr = get_Const_tarval(right);
5412 tv_shl = get_Const_tarval(shl_right);
5414 tv_mask = tarval_shl(get_mode_all_one(mode), tv_shl);
5415 tv_mask = tarval_shr(tv_mask, tv_shr);
5420 if (get_tarval_mode(tv_shl) != get_tarval_mode(tv_shr)) {
5421 tv_shl = tarval_convert_to(tv_shl, get_tarval_mode(tv_shr));
5424 assert(tv_mask != tarval_bad);
5425 assert(get_tarval_mode(tv_mask) == mode);
5427 irg = get_irn_irg(n);
5428 block = get_nodes_block(n);
5429 dbgi = get_irn_dbg_info(n);
5431 pnc = tarval_cmp(tv_shl, tv_shr);
5432 if (pnc == pn_Cmp_Lt || pnc == pn_Cmp_Eq) {
5433 tv_shift = tarval_sub(tv_shr, tv_shl, NULL);
5434 new_const = new_Const(tv_shift);
5436 new_shift = new_rd_Shrs(dbgi, block, x, new_const, mode);
5438 new_shift = new_rd_Shr(dbgi, block, x, new_const, mode);
5441 assert(pnc == pn_Cmp_Gt);
5442 tv_shift = tarval_sub(tv_shl, tv_shr, NULL);
5443 new_const = new_Const(tv_shift);
5444 new_shift = new_rd_Shl(dbgi, block, x, new_const, mode);
5447 new_const = new_Const(tv_mask);
5448 new_and = new_rd_And(dbgi, block, new_shift, new_const, mode);
5456 static ir_node *transform_node_Shr(ir_node *n)
5458 ir_node *c, *oldn = n;
5459 ir_node *left = get_Shr_left(n);
5460 ir_node *right = get_Shr_right(n);
5461 ir_mode *mode = get_irn_mode(n);
5463 HANDLE_BINOP_PHI((eval_func) tarval_shr, left, right, c, mode);
5464 n = transform_node_shift(n);
5467 n = transform_node_shl_shr(n);
5469 n = transform_node_bitop_shift(n);
5472 } /* transform_node_Shr */
5477 static ir_node *transform_node_Shrs(ir_node *n)
5479 ir_node *c, *oldn = n;
5480 ir_node *a = get_Shrs_left(n);
5481 ir_node *b = get_Shrs_right(n);
5482 ir_mode *mode = get_irn_mode(n);
5484 HANDLE_BINOP_PHI((eval_func) tarval_shrs, a, b, c, mode);
5485 n = transform_node_shift(n);
5488 n = transform_node_bitop_shift(n);
5491 } /* transform_node_Shrs */
5496 static ir_node *transform_node_Shl(ir_node *n)
5498 ir_node *c, *oldn = n;
5499 ir_node *a = get_Shl_left(n);
5500 ir_node *b = get_Shl_right(n);
5501 ir_mode *mode = get_irn_mode(n);
5503 HANDLE_BINOP_PHI((eval_func) tarval_shl, a, b, c, mode);
5504 n = transform_node_shift(n);
5507 n = transform_node_shl_shr(n);
5509 n = transform_node_bitop_shift(n);
5512 } /* transform_node_Shl */
5517 static ir_node *transform_node_Rotl(ir_node *n)
5519 ir_node *c, *oldn = n;
5520 ir_node *a = get_Rotl_left(n);
5521 ir_node *b = get_Rotl_right(n);
5522 ir_mode *mode = get_irn_mode(n);
5524 HANDLE_BINOP_PHI((eval_func) tarval_rotl, a, b, c, mode);
5525 n = transform_node_shift(n);
5528 n = transform_node_bitop_shift(n);
5531 } /* transform_node_Rotl */
5536 static ir_node *transform_node_Conv(ir_node *n)
5538 ir_node *c, *oldn = n;
5539 ir_mode *mode = get_irn_mode(n);
5540 ir_node *a = get_Conv_op(n);
5542 if (mode != mode_b && is_const_Phi(a)) {
5543 /* Do NOT optimize mode_b Conv's, this leads to remaining
5544 * Phib nodes later, because the conv_b_lower operation
5545 * is instantly reverted, when it tries to insert a Convb.
5547 c = apply_conv_on_phi(a, mode);
5549 DBG_OPT_ALGSIM0(oldn, c, FS_OPT_CONST_PHI);
5554 if (is_Unknown(a)) { /* Conv_A(Unknown_B) -> Unknown_A */
5555 return new_r_Unknown(current_ir_graph, mode);
5558 if (mode_is_reference(mode) &&
5559 get_mode_size_bits(mode) == get_mode_size_bits(get_irn_mode(a)) &&
5561 ir_node *l = get_Add_left(a);
5562 ir_node *r = get_Add_right(a);
5563 dbg_info *dbgi = get_irn_dbg_info(a);
5564 ir_node *block = get_nodes_block(n);
5566 ir_node *lop = get_Conv_op(l);
5567 if (get_irn_mode(lop) == mode) {
5568 /* ConvP(AddI(ConvI(P), x)) -> AddP(P, x) */
5569 n = new_rd_Add(dbgi, block, lop, r, mode);
5574 ir_node *rop = get_Conv_op(r);
5575 if (get_irn_mode(rop) == mode) {
5576 /* ConvP(AddI(x, ConvI(P))) -> AddP(x, P) */
5577 n = new_rd_Add(dbgi, block, l, rop, mode);
5584 } /* transform_node_Conv */
5587 * Remove dead blocks and nodes in dead blocks
5588 * in keep alive list. We do not generate a new End node.
5590 static ir_node *transform_node_End(ir_node *n)
5592 int i, j, n_keepalives = get_End_n_keepalives(n);
5595 NEW_ARR_A(ir_node *, in, n_keepalives);
5597 for (i = j = 0; i < n_keepalives; ++i) {
5598 ir_node *ka = get_End_keepalive(n, i);
5600 if (! is_Block_dead(ka)) {
5604 } else if (is_irn_pinned_in_irg(ka) && is_Block_dead(get_nodes_block(ka))) {
5606 } else if (is_Bad(ka)) {
5607 /* no need to keep Bad */
5612 if (j != n_keepalives)
5613 set_End_keepalives(n, j, in);
5615 } /* transform_node_End */
5617 bool is_negated_value(ir_node *a, ir_node *b)
5619 if (is_Minus(a) && get_Minus_op(a) == b)
5621 if (is_Minus(b) && get_Minus_op(b) == a)
5623 if (is_Sub(a) && is_Sub(b)) {
5624 ir_node *a_left = get_Sub_left(a);
5625 ir_node *a_right = get_Sub_right(a);
5626 ir_node *b_left = get_Sub_left(b);
5627 ir_node *b_right = get_Sub_right(b);
5629 if (a_left == b_right && a_right == b_left)
5637 * Optimize a Mux into some simpler cases.
5639 static ir_node *transform_node_Mux(ir_node *n)
5641 ir_node *oldn = n, *sel = get_Mux_sel(n);
5642 ir_mode *mode = get_irn_mode(n);
5643 ir_node *t = get_Mux_true(n);
5644 ir_node *f = get_Mux_false(n);
5645 ir_graph *irg = current_ir_graph;
5647 if (is_irg_state(irg, IR_GRAPH_STATE_KEEP_MUX))
5651 ir_node* block = get_nodes_block(n);
5653 ir_node* c1 = get_Mux_sel(t);
5654 ir_node* t1 = get_Mux_true(t);
5655 ir_node* f1 = get_Mux_false(t);
5657 /* Mux(cond0, Mux(cond1, x, y), y) -> typical if (cond0 && cond1) x else y */
5658 ir_node* and_ = new_r_And(block, c0, c1, mode_b);
5659 ir_node* new_mux = new_r_Mux(block, and_, f1, t1, mode);
5664 DBG_OPT_ALGSIM0(oldn, t, FS_OPT_MUX_COMBINE);
5665 } else if (f == t1) {
5666 /* Mux(cond0, Mux(cond1, x, y), x) */
5667 ir_node* not_c1 = new_r_Not(block, c1, mode_b);
5668 ir_node* and_ = new_r_And(block, c0, not_c1, mode_b);
5669 ir_node* new_mux = new_r_Mux(block, and_, t1, f1, mode);
5674 DBG_OPT_ALGSIM0(oldn, t, FS_OPT_MUX_COMBINE);
5676 } else if (is_Mux(f)) {
5677 ir_node* block = get_nodes_block(n);
5679 ir_node* c1 = get_Mux_sel(f);
5680 ir_node* t1 = get_Mux_true(f);
5681 ir_node* f1 = get_Mux_false(f);
5683 /* Mux(cond0, x, Mux(cond1, x, y)) -> typical if (cond0 || cond1) x else y */
5684 ir_node* or_ = new_r_Or(block, c0, c1, mode_b);
5685 ir_node* new_mux = new_r_Mux(block, or_, f1, t1, mode);
5690 DBG_OPT_ALGSIM0(oldn, f, FS_OPT_MUX_COMBINE);
5691 } else if (t == f1) {
5692 /* Mux(cond0, x, Mux(cond1, y, x)) */
5693 ir_node* not_c1 = new_r_Not(block, c1, mode_b);
5694 ir_node* or_ = new_r_Or(block, c0, not_c1, mode_b);
5695 ir_node* new_mux = new_r_Mux(block, or_, t1, f1, mode);
5700 DBG_OPT_ALGSIM0(oldn, f, FS_OPT_MUX_COMBINE);
5704 /* first normalization step: move a possible zero to the false case */
5706 ir_node *cmp = get_Proj_pred(sel);
5709 if (is_Const(t) && is_Const_null(t)) {
5712 /* Mux(x, 0, y) => Mux(x, y, 0) */
5713 pn_Cmp pnc = get_Proj_proj(sel);
5714 sel = new_r_Proj(get_nodes_block(cmp), cmp, mode_b,
5715 get_negated_pnc(pnc, get_irn_mode(get_Cmp_left(cmp))));
5716 n = new_rd_Mux(get_irn_dbg_info(n), get_nodes_block(n), sel, t, f, mode);
5724 /* note: after normalization, false can only happen on default */
5725 if (mode == mode_b) {
5726 dbg_info *dbg = get_irn_dbg_info(n);
5727 ir_node *block = get_nodes_block(n);
5730 tarval *tv_t = get_Const_tarval(t);
5731 if (tv_t == tarval_b_true) {
5733 /* Muxb(sel, true, false) = sel */
5734 assert(get_Const_tarval(f) == tarval_b_false);
5735 DBG_OPT_ALGSIM0(oldn, sel, FS_OPT_MUX_BOOL);
5738 /* Muxb(sel, true, x) = Or(sel, x) */
5739 n = new_rd_Or(dbg, block, sel, f, mode_b);
5740 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_OR_BOOL);
5744 } else if (is_Const(f)) {
5745 tarval *tv_f = get_Const_tarval(f);
5746 if (tv_f == tarval_b_true) {
5747 /* Muxb(sel, x, true) = Or(Not(sel), x) */
5748 ir_node* not_sel = new_rd_Not(dbg, block, sel, mode_b);
5749 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_ORNOT_BOOL);
5750 n = new_rd_Or(dbg, block, not_sel, t, mode_b);
5753 /* Muxb(sel, x, false) = And(sel, x) */
5754 assert(tv_f == tarval_b_false);
5755 n = new_rd_And(dbg, block, sel, t, mode_b);
5756 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_AND_BOOL);
5762 /* more normalization: try to normalize Mux(x, C1, C2) into Mux(x, +1/-1, 0) op C2 */
5763 if (is_Const(t) && is_Const(f) && mode_is_int(mode)) {
5764 tarval *a = get_Const_tarval(t);
5765 tarval *b = get_Const_tarval(f);
5768 if (tarval_is_one(a) && tarval_is_null(b)) {
5769 ir_node *block = get_nodes_block(n);
5770 ir_node *conv = new_r_Conv(block, sel, mode);
5772 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_CONV);
5774 } else if (tarval_is_null(a) && tarval_is_one(b)) {
5775 ir_node *block = get_nodes_block(n);
5776 ir_node *not_ = new_r_Not(block, sel, mode_b);
5777 ir_node *conv = new_r_Conv(block, not_, mode);
5779 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_CONV);
5782 /* TODO: it's not really clear if that helps in general or should be moved
5783 * to backend, especially with the MUX->Conv transformation above */
5784 if (tarval_cmp(a, b) & pn_Cmp_Gt) {
5785 diff = tarval_sub(a, b, NULL);
5788 diff = tarval_sub(b, a, NULL);
5792 if (diff == get_tarval_one(mode)) {
5793 dbg_info *dbg = get_irn_dbg_info(n);
5794 ir_node *block = get_nodes_block(n);
5795 ir_node *t = new_Const(tarval_sub(a, min, NULL));
5796 ir_node *f = new_Const(tarval_sub(b, min, NULL));
5797 n = new_rd_Mux(dbg, block, sel, f, t, mode);
5798 n = new_rd_Add(dbg, block, n, new_Const(min), mode);
5804 ir_node *cmp = get_Proj_pred(sel);
5805 long pn = get_Proj_proj(sel);
5808 * Note: normalization puts the constant on the right side,
5809 * so we check only one case.
5811 * Note further that these optimization work even for floating point
5812 * with NaN's because -NaN == NaN.
5813 * However, if +0 and -0 is handled differently, we cannot use the Abs/-Abs
5817 ir_node *cmp_r = get_Cmp_right(cmp);
5818 if (is_Const(cmp_r) && is_Const_null(cmp_r)) {
5819 ir_node *block = get_nodes_block(n);
5820 ir_node *cmp_l = get_Cmp_left(cmp);
5822 if (!mode_honor_signed_zeros(mode) && is_negated_value(f, t)) {
5825 /* NaN's work fine with abs, so it is ok to remove Uo */
5826 long pnc = pn & ~pn_Cmp_Uo;
5828 if ( (cmp_l == t && (pnc == pn_Cmp_Ge || pnc == pn_Cmp_Gt))
5829 || (cmp_l == f && (pnc == pn_Cmp_Le || pnc == pn_Cmp_Lt)))
5831 /* Mux(a >/>= 0, a, -a) = Mux(a </<= 0, -a, a) ==> Abs(a) */
5832 n = new_rd_Abs(get_irn_dbg_info(n), block, cmp_l, mode);
5833 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_ABS);
5835 } else if ((cmp_l == t && (pnc == pn_Cmp_Le || pnc == pn_Cmp_Lt))
5836 || (cmp_l == f && (pnc == pn_Cmp_Ge || pnc == pn_Cmp_Gt)))
5838 /* Mux(a </<= 0, a, -a) = Mux(a >/>= 0, -a, a) ==> -Abs(a) */
5839 n = new_rd_Abs(get_irn_dbg_info(n), block, cmp_l, mode);
5840 n = new_rd_Minus(get_irn_dbg_info(n), block, n, mode);
5841 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_ABS);
5846 if (mode_is_int(mode)) {
5848 if ((pn == pn_Cmp_Lg || pn == pn_Cmp_Eq) && is_And(cmp_l)) {
5849 /* Mux((a & b) != 0, c, 0) */
5850 ir_node *and_r = get_And_right(cmp_l);
5853 if (and_r == t && f == cmp_r) {
5854 if (is_Const(t) && tarval_is_single_bit(get_Const_tarval(t))) {
5855 if (pn == pn_Cmp_Lg) {
5856 /* Mux((a & 2^C) != 0, 2^C, 0) */
5858 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_BITOP);
5860 /* Mux((a & 2^C) == 0, 2^C, 0) */
5861 n = new_rd_Eor(get_irn_dbg_info(n),
5862 block, cmp_l, t, mode);
5863 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_BITOP);
5868 if (is_Shl(and_r)) {
5869 ir_node *shl_l = get_Shl_left(and_r);
5870 if (is_Const(shl_l) && is_Const_one(shl_l)) {
5871 if (and_r == t && f == cmp_r) {
5872 if (pn == pn_Cmp_Lg) {
5873 /* (a & (1 << n)) != 0, (1 << n), 0) */
5875 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_BITOP);
5877 /* (a & (1 << n)) == 0, (1 << n), 0) */
5878 n = new_rd_Eor(get_irn_dbg_info(n),
5879 block, cmp_l, t, mode);
5880 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_BITOP);
5886 and_l = get_And_left(cmp_l);
5887 if (is_Shl(and_l)) {
5888 ir_node *shl_l = get_Shl_left(and_l);
5889 if (is_Const(shl_l) && is_Const_one(shl_l)) {
5890 if (and_l == t && f == cmp_r) {
5891 if (pn == pn_Cmp_Lg) {
5892 /* ((1 << n) & a) != 0, (1 << n), 0) */
5894 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_BITOP);
5896 /* ((1 << n) & a) == 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);
5912 } /* transform_node_Mux */
5915 * optimize Sync nodes that have other syncs as input we simply add the inputs
5916 * of the other sync to our own inputs
5918 static ir_node *transform_node_Sync(ir_node *n)
5920 int arity = get_Sync_n_preds(n);
5923 for (i = 0; i < arity;) {
5924 ir_node *pred = get_Sync_pred(n, i);
5928 if (!is_Sync(pred)) {
5936 pred_arity = get_Sync_n_preds(pred);
5937 for (j = 0; j < pred_arity; ++j) {
5938 ir_node *pred_pred = get_Sync_pred(pred, j);
5943 add_irn_n(n, pred_pred);
5947 if (get_Sync_pred(n, k) == pred_pred) break;
5952 /* rehash the sync node */
5953 add_identities(current_ir_graph->value_table, n);
5956 } /* transform_node_Sync */
5959 * optimize a trampoline Call into a direct Call
5961 static ir_node *transform_node_Call(ir_node *call)
5963 ir_node *callee = get_Call_ptr(call);
5964 ir_node *adr, *mem, *res, *bl, **in;
5965 ir_type *ctp, *mtp, *tp;
5968 int i, n_res, n_param;
5971 if (! is_Proj(callee))
5973 callee = get_Proj_pred(callee);
5974 if (! is_Builtin(callee))
5976 if (get_Builtin_kind(callee) != ir_bk_inner_trampoline)
5979 mem = get_Call_mem(call);
5981 if (skip_Proj(mem) == callee) {
5982 /* memory is routed to the trampoline, skip */
5983 mem = get_Builtin_mem(callee);
5986 /* build a new call type */
5987 mtp = get_Call_type(call);
5988 tdb = get_type_dbg_info(mtp);
5990 n_res = get_method_n_ress(mtp);
5991 n_param = get_method_n_params(mtp);
5992 ctp = new_d_type_method(n_param + 1, n_res, tdb);
5994 for (i = 0; i < n_res; ++i)
5995 set_method_res_type(ctp, i, get_method_res_type(mtp, i));
5997 NEW_ARR_A(ir_node *, in, n_param + 1);
5999 /* FIXME: we don't need a new pointer type in every step */
6000 tp = get_irg_frame_type(current_ir_graph);
6001 tp = new_type_pointer(tp);
6002 set_method_param_type(ctp, 0, tp);
6004 in[0] = get_Builtin_param(callee, 2);
6005 for (i = 0; i < n_param; ++i) {
6006 set_method_param_type(ctp, i + 1, get_method_param_type(mtp, i));
6007 in[i + 1] = get_Call_param(call, i);
6009 var = get_method_variadicity(mtp);
6010 set_method_variadicity(ctp, var);
6011 if (var == variadicity_variadic) {
6012 set_method_first_variadic_param_index(ctp, get_method_first_variadic_param_index(mtp) + 1);
6014 /* When we resolve a trampoline, the function must be called by a this-call */
6015 set_method_calling_convention(ctp, get_method_calling_convention(mtp) | cc_this_call);
6016 set_method_additional_properties(ctp, get_method_additional_properties(mtp));
6018 adr = get_Builtin_param(callee, 1);
6020 db = get_irn_dbg_info(call);
6021 bl = get_nodes_block(call);
6023 res = new_rd_Call(db, bl, mem, adr, n_param + 1, in, ctp);
6024 if (get_irn_pinned(call) == op_pin_state_floats)
6025 set_irn_pinned(res, op_pin_state_floats);
6027 } /* transform_node_Call */
6030 * Tries several [inplace] [optimizing] transformations and returns an
6031 * equivalent node. The difference to equivalent_node() is that these
6032 * transformations _do_ generate new nodes, and thus the old node must
6033 * not be freed even if the equivalent node isn't the old one.
6035 static ir_node *transform_node(ir_node *n)
6040 * Transform_node is the only "optimizing transformation" that might
6041 * return a node with a different opcode. We iterate HERE until fixpoint
6042 * to get the final result.
6046 if (n->op->ops.transform_node != NULL)
6047 n = n->op->ops.transform_node(n);
6048 } while (oldn != n);
6051 } /* transform_node */
6054 * Sets the default transform node operation for an ir_op_ops.
6056 * @param code the opcode for the default operation
6057 * @param ops the operations initialized
6062 static ir_op_ops *firm_set_default_transform_node(ir_opcode code, ir_op_ops *ops)
6066 ops->transform_node = transform_node_##a; \
6068 #define CASE_PROJ(a) \
6070 ops->transform_node_Proj = transform_node_Proj_##a; \
6072 #define CASE_PROJ_EX(a) \
6074 ops->transform_node = transform_node_##a; \
6075 ops->transform_node_Proj = transform_node_Proj_##a; \
6084 CASE_PROJ_EX(DivMod);
6119 } /* firm_set_default_transform_node */
6122 /* **************** Common Subexpression Elimination **************** */
6124 /** The size of the hash table used, should estimate the number of nodes
6126 #define N_IR_NODES 512
6128 /** Compares the attributes of two Const nodes. */
6129 static int node_cmp_attr_Const(ir_node *a, ir_node *b)
6131 return (get_Const_tarval(a) != get_Const_tarval(b))
6132 || (get_Const_type(a) != get_Const_type(b));
6133 } /* node_cmp_attr_Const */
6135 /** Compares the attributes of two Proj nodes. */
6136 static int node_cmp_attr_Proj(ir_node *a, ir_node *b)
6138 return get_irn_proj_attr(a) != get_irn_proj_attr(b);
6139 } /* node_cmp_attr_Proj */
6141 /** Compares the attributes of two Filter nodes. */
6142 static int node_cmp_attr_Filter(ir_node *a, ir_node *b)
6144 return get_Filter_proj(a) != get_Filter_proj(b);
6145 } /* node_cmp_attr_Filter */
6147 /** Compares the attributes of two Alloc nodes. */
6148 static int node_cmp_attr_Alloc(ir_node *a, ir_node *b)
6150 const alloc_attr *pa = get_irn_alloc_attr(a);
6151 const alloc_attr *pb = get_irn_alloc_attr(b);
6152 return (pa->where != pb->where) || (pa->type != pb->type);
6153 } /* node_cmp_attr_Alloc */
6155 /** Compares the attributes of two Free nodes. */
6156 static int node_cmp_attr_Free(ir_node *a, ir_node *b)
6158 const free_attr *pa = get_irn_free_attr(a);
6159 const free_attr *pb = get_irn_free_attr(b);
6160 return (pa->where != pb->where) || (pa->type != pb->type);
6161 } /* node_cmp_attr_Free */
6163 /** Compares the attributes of two SymConst nodes. */
6164 static int node_cmp_attr_SymConst(ir_node *a, ir_node *b)
6166 const symconst_attr *pa = get_irn_symconst_attr(a);
6167 const symconst_attr *pb = get_irn_symconst_attr(b);
6168 return (pa->kind != pb->kind)
6169 || (pa->sym.type_p != pb->sym.type_p)
6170 || (pa->tp != pb->tp);
6171 } /* node_cmp_attr_SymConst */
6173 /** Compares the attributes of two Call nodes. */
6174 static int node_cmp_attr_Call(ir_node *a, ir_node *b)
6176 const call_attr *pa = get_irn_call_attr(a);
6177 const call_attr *pb = get_irn_call_attr(b);
6178 return (pa->type != pb->type)
6179 || (pa->tail_call != pb->tail_call);
6180 } /* node_cmp_attr_Call */
6182 /** Compares the attributes of two Sel nodes. */
6183 static int node_cmp_attr_Sel(ir_node *a, ir_node *b)
6185 const ir_entity *a_ent = get_Sel_entity(a);
6186 const ir_entity *b_ent = get_Sel_entity(b);
6189 (a_ent->kind != b_ent->kind) ||
6190 (a_ent->name != b_ent->name) ||
6191 (a_ent->owner != b_ent->owner) ||
6192 (a_ent->ld_name != b_ent->ld_name) ||
6193 (a_ent->type != b_ent->type);
6195 /* Matze: inlining of functions can produce 2 entities with same type,
6197 return a_ent != b_ent;
6198 } /* node_cmp_attr_Sel */
6200 /** Compares the attributes of two Phi nodes. */
6201 static int node_cmp_attr_Phi(ir_node *a, ir_node *b)
6203 /* we can only enter this function if both nodes have the same number of inputs,
6204 hence it is enough to check if one of them is a Phi0 */
6206 /* check the Phi0 pos attribute */
6207 return get_irn_phi_attr(a)->u.pos != get_irn_phi_attr(b)->u.pos;
6210 } /* node_cmp_attr_Phi */
6212 /** Compares the attributes of two Conv nodes. */
6213 static int node_cmp_attr_Conv(ir_node *a, ir_node *b)
6215 return get_Conv_strict(a) != get_Conv_strict(b);
6216 } /* node_cmp_attr_Conv */
6218 /** Compares the attributes of two Cast nodes. */
6219 static int node_cmp_attr_Cast(ir_node *a, ir_node *b)
6221 return get_Cast_type(a) != get_Cast_type(b);
6222 } /* node_cmp_attr_Cast */
6224 /** Compares the attributes of two Load nodes. */
6225 static int node_cmp_attr_Load(ir_node *a, ir_node *b)
6227 if (get_Load_volatility(a) == volatility_is_volatile ||
6228 get_Load_volatility(b) == volatility_is_volatile)
6229 /* NEVER do CSE on volatile Loads */
6231 /* do not CSE Loads with different alignment. Be conservative. */
6232 if (get_Load_align(a) != get_Load_align(b))
6235 return get_Load_mode(a) != get_Load_mode(b);
6236 } /* node_cmp_attr_Load */
6238 /** Compares the attributes of two Store nodes. */
6239 static int node_cmp_attr_Store(ir_node *a, ir_node *b)
6241 /* do not CSE Stores with different alignment. Be conservative. */
6242 if (get_Store_align(a) != get_Store_align(b))
6245 /* NEVER do CSE on volatile Stores */
6246 return (get_Store_volatility(a) == volatility_is_volatile ||
6247 get_Store_volatility(b) == volatility_is_volatile);
6248 } /* node_cmp_attr_Store */
6250 /** Compares two exception attributes */
6251 static int node_cmp_exception(ir_node *a, ir_node *b)
6253 const except_attr *ea = get_irn_except_attr(a);
6254 const except_attr *eb = get_irn_except_attr(b);
6256 return ea->pin_state != eb->pin_state;
6259 #define node_cmp_attr_Bound node_cmp_exception
6261 /** Compares the attributes of two Div nodes. */
6262 static int node_cmp_attr_Div(ir_node *a, ir_node *b)
6264 const divmod_attr *ma = get_irn_divmod_attr(a);
6265 const divmod_attr *mb = get_irn_divmod_attr(b);
6266 return ma->exc.pin_state != mb->exc.pin_state ||
6267 ma->resmode != mb->resmode ||
6268 ma->no_remainder != mb->no_remainder;
6269 } /* node_cmp_attr_Div */
6271 /** Compares the attributes of two DivMod nodes. */
6272 static int node_cmp_attr_DivMod(ir_node *a, ir_node *b)
6274 const divmod_attr *ma = get_irn_divmod_attr(a);
6275 const divmod_attr *mb = get_irn_divmod_attr(b);
6276 return ma->exc.pin_state != mb->exc.pin_state ||
6277 ma->resmode != mb->resmode;
6278 } /* node_cmp_attr_DivMod */
6280 /** Compares the attributes of two Mod nodes. */
6281 static int node_cmp_attr_Mod(ir_node *a, ir_node *b)
6283 const divmod_attr *ma = get_irn_divmod_attr(a);
6284 const divmod_attr *mb = get_irn_divmod_attr(b);
6285 return ma->exc.pin_state != mb->exc.pin_state ||
6286 ma->resmode != mb->resmode;
6287 } /* node_cmp_attr_Mod */
6289 /** Compares the attributes of two Quot nodes. */
6290 static int node_cmp_attr_Quot(ir_node *a, ir_node *b)
6292 const divmod_attr *ma = get_irn_divmod_attr(a);
6293 const divmod_attr *mb = get_irn_divmod_attr(b);
6294 return ma->exc.pin_state != mb->exc.pin_state ||
6295 ma->resmode != mb->resmode;
6296 } /* node_cmp_attr_Quot */
6298 /** Compares the attributes of two Confirm nodes. */
6299 static int node_cmp_attr_Confirm(ir_node *a, ir_node *b)
6301 /* no need to compare the bound, as this is a input */
6302 return (get_Confirm_cmp(a) != get_Confirm_cmp(b));
6303 } /* node_cmp_attr_Confirm */
6305 /** Compares the attributes of two Builtin nodes. */
6306 static int node_cmp_attr_Builtin(ir_node *a, ir_node *b)
6308 const builtin_attr *ma = get_irn_builtin_attr(a);
6309 const builtin_attr *mb = get_irn_builtin_attr(b);
6311 /* no need to compare the type, equal kind means equal type */
6312 return ma->kind != mb->kind;
6313 } /* node_cmp_attr_Builtin */
6315 /** Compares the attributes of two ASM nodes. */
6316 static int node_cmp_attr_ASM(ir_node *a, ir_node *b)
6319 const ir_asm_constraint *ca;
6320 const ir_asm_constraint *cb;
6323 if (get_ASM_text(a) != get_ASM_text(b))
6326 /* Should we really check the constraints here? Should be better, but is strange. */
6327 n = get_ASM_n_input_constraints(a);
6328 if (n != get_ASM_n_input_constraints(b))
6331 ca = get_ASM_input_constraints(a);
6332 cb = get_ASM_input_constraints(b);
6333 for (i = 0; i < n; ++i) {
6334 if (ca[i].pos != cb[i].pos || ca[i].constraint != cb[i].constraint)
6338 n = get_ASM_n_output_constraints(a);
6339 if (n != get_ASM_n_output_constraints(b))
6342 ca = get_ASM_output_constraints(a);
6343 cb = get_ASM_output_constraints(b);
6344 for (i = 0; i < n; ++i) {
6345 if (ca[i].pos != cb[i].pos || ca[i].constraint != cb[i].constraint)
6349 n = get_ASM_n_clobbers(a);
6350 if (n != get_ASM_n_clobbers(b))
6353 cla = get_ASM_clobbers(a);
6354 clb = get_ASM_clobbers(b);
6355 for (i = 0; i < n; ++i) {
6356 if (cla[i] != clb[i])
6360 } /* node_cmp_attr_ASM */
6362 /** Compares the inexistent attributes of two Dummy nodes. */
6363 static int node_cmp_attr_Dummy(ir_node *a, ir_node *b)
6371 * Set the default node attribute compare operation for an ir_op_ops.
6373 * @param code the opcode for the default operation
6374 * @param ops the operations initialized
6379 static ir_op_ops *firm_set_default_node_cmp_attr(ir_opcode code, ir_op_ops *ops)
6383 ops->node_cmp_attr = node_cmp_attr_##a; \
6416 } /* firm_set_default_node_cmp_attr */
6419 * Compare function for two nodes in the value table. Gets two
6420 * nodes as parameters. Returns 0 if the nodes are a Common Sub Expression.
6422 int identities_cmp(const void *elt, const void *key)
6424 ir_node *a = (ir_node *)elt;
6425 ir_node *b = (ir_node *)key;
6428 if (a == b) return 0;
6430 if ((get_irn_op(a) != get_irn_op(b)) ||
6431 (get_irn_mode(a) != get_irn_mode(b))) return 1;
6433 /* compare if a's in and b's in are of equal length */
6434 irn_arity_a = get_irn_intra_arity(a);
6435 if (irn_arity_a != get_irn_intra_arity(b))
6438 if (get_irn_pinned(a) == op_pin_state_pinned) {
6439 /* for pinned nodes, the block inputs must be equal */
6440 if (get_irn_intra_n(a, -1) != get_irn_intra_n(b, -1))
6442 } else if (! get_opt_global_cse()) {
6443 /* for block-local CSE both nodes must be in the same MacroBlock */
6444 if (get_irn_MacroBlock(a) != get_irn_MacroBlock(b))
6448 /* compare a->in[0..ins] with b->in[0..ins] */
6449 for (i = 0; i < irn_arity_a; ++i) {
6450 ir_node *pred_a = get_irn_intra_n(a, i);
6451 ir_node *pred_b = get_irn_intra_n(b, i);
6452 if (pred_a != pred_b) {
6453 /* if both predecessors are CSE neutral they might be different */
6454 if (!is_irn_cse_neutral(pred_a) || !is_irn_cse_neutral(pred_b))
6460 * here, we already now that the nodes are identical except their
6463 if (a->op->ops.node_cmp_attr)
6464 return a->op->ops.node_cmp_attr(a, b);
6467 } /* identities_cmp */
6470 * Calculate a hash value of a node.
6472 * @param node The IR-node
6474 unsigned ir_node_hash(const ir_node *node)
6476 return node->op->ops.hash(node);
6477 } /* ir_node_hash */
6480 pset *new_identities(void)
6482 return new_pset(identities_cmp, N_IR_NODES);
6483 } /* new_identities */
6485 void del_identities(pset *value_table)
6487 del_pset(value_table);
6488 } /* del_identities */
6490 /* Normalize a node by putting constants (and operands with larger
6491 * node index) on the right (operator side). */
6492 void ir_normalize_node(ir_node *n)
6494 if (is_op_commutative(get_irn_op(n))) {
6495 ir_node *l = get_binop_left(n);
6496 ir_node *r = get_binop_right(n);
6498 /* For commutative operators perform a OP b == b OP a but keep
6499 * constants on the RIGHT side. This helps greatly in some
6500 * optimizations. Moreover we use the idx number to make the form
6502 if (!operands_are_normalized(l, r)) {
6503 set_binop_left(n, r);
6504 set_binop_right(n, l);
6508 } /* ir_normalize_node */
6511 * Update the nodes after a match in the value table. If both nodes have
6512 * the same MacroBlock but different Blocks, we must ensure that the node
6513 * with the dominating Block (the node that is near to the MacroBlock header
6514 * is stored in the table.
6515 * Because a MacroBlock has only one "non-exception" flow, we don't need
6516 * dominance info here: We known, that one block must dominate the other and
6517 * following the only block input will allow to find it.
6519 static void update_known_irn(ir_node *known_irn, const ir_node *new_ir_node)
6521 ir_node *known_blk, *new_block, *block, *mbh;
6523 if (get_opt_global_cse()) {
6524 /* Block inputs are meaning less */
6527 known_blk = get_irn_n(known_irn, -1);
6528 new_block = get_irn_n(new_ir_node, -1);
6529 if (known_blk == new_block) {
6530 /* already in the same block */
6534 * We expect the typical case when we built the graph. In that case, the
6535 * known_irn is already the upper one, so checking this should be faster.
6538 mbh = get_Block_MacroBlock(new_block);
6540 if (block == known_blk) {
6541 /* ok, we have found it: known_block dominates new_block as expected */
6546 * We have reached the MacroBlock header NOT founding
6547 * the known_block. new_block must dominate known_block.
6550 set_irn_n(known_irn, -1, new_block);
6553 assert(get_Block_n_cfgpreds(block) == 1);
6554 block = get_Block_cfgpred_block(block, 0);
6556 } /* update_value_table */
6559 * Return the canonical node computing the same value as n.
6560 * Looks up the node in a hash table, enters it in the table
6561 * if it isn't there yet.
6563 * @param value_table the HashSet containing all nodes in the
6565 * @param n the node to look up
6567 * @return a node that computes the same value as n or n if no such
6568 * node could be found
6570 ir_node *identify_remember(pset *value_table, ir_node *n)
6574 if (!value_table) return n;
6576 ir_normalize_node(n);
6577 /* lookup or insert in hash table with given hash key. */
6578 nn = pset_insert(value_table, n, ir_node_hash(n));
6581 update_known_irn(nn, n);
6583 /* n is reachable again */
6584 edges_node_revival(nn, get_irn_irg(nn));
6588 } /* identify_remember */
6591 * During construction we set the op_pin_state_pinned flag in the graph right when the
6592 * optimization is performed. The flag turning on procedure global cse could
6593 * be changed between two allocations. This way we are safe.
6595 * @param value_table The value table
6596 * @param n The node to lookup
6598 static inline ir_node *identify_cons(pset *value_table, ir_node *n)
6602 n = identify_remember(value_table, n);
6603 if (n != old && get_irn_MacroBlock(old) != get_irn_MacroBlock(n))
6604 set_irg_pinned(current_ir_graph, op_pin_state_floats);
6606 } /* identify_cons */
6608 /* Add a node to the identities value table. */
6609 void add_identities(pset *value_table, ir_node *node)
6611 if (get_opt_cse() && is_no_Block(node))
6612 identify_remember(value_table, node);
6613 } /* add_identities */
6615 /* Visit each node in the value table of a graph. */
6616 void visit_all_identities(ir_graph *irg, irg_walk_func visit, void *env)
6619 ir_graph *rem = current_ir_graph;
6621 current_ir_graph = irg;
6622 foreach_pset(irg->value_table, node)
6624 current_ir_graph = rem;
6625 } /* visit_all_identities */
6628 * Garbage in, garbage out. If a node has a dead input, i.e., the
6629 * Bad node is input to the node, return the Bad node.
6631 static ir_node *gigo(ir_node *node)
6634 ir_op *op = get_irn_op(node);
6636 /* remove garbage blocks by looking at control flow that leaves the block
6637 and replacing the control flow by Bad. */
6638 if (get_irn_mode(node) == mode_X) {
6639 ir_node *block = get_nodes_block(skip_Proj(node));
6641 /* Don't optimize nodes in immature blocks. */
6642 if (!get_Block_matured(block))
6644 /* Don't optimize End, may have Bads. */
6645 if (op == op_End) return node;
6647 if (is_Block(block)) {
6648 if (is_Block_dead(block)) {
6649 /* control flow from dead block is dead */
6653 for (i = get_irn_arity(block) - 1; i >= 0; --i) {
6654 if (!is_Bad(get_irn_n(block, i)))
6658 ir_graph *irg = get_irn_irg(block);
6659 /* the start block is never dead */
6660 if (block != get_irg_start_block(irg)
6661 && block != get_irg_end_block(irg)) {
6663 * Do NOT kill control flow without setting
6664 * the block to dead of bad things can happen:
6665 * We get a Block that is not reachable be irg_block_walk()
6666 * but can be found by irg_walk()!
6668 set_Block_dead(block);
6675 /* Blocks, Phis and Tuples may have dead inputs, e.g., if one of the
6676 blocks predecessors is dead. */
6677 if (op != op_Block && op != op_Phi && op != op_Tuple) {
6678 irn_arity = get_irn_arity(node);
6681 * Beware: we can only read the block of a non-floating node.
6683 if (is_irn_pinned_in_irg(node) &&
6684 is_Block_dead(get_nodes_block(skip_Proj(node))))
6687 for (i = 0; i < irn_arity; i++) {
6688 ir_node *pred = get_irn_n(node, i);
6693 /* Propagating Unknowns here seems to be a bad idea, because
6694 sometimes we need a node as a input and did not want that
6696 However, it might be useful to move this into a later phase
6697 (if you think that optimizing such code is useful). */
6698 if (is_Unknown(pred) && mode_is_data(get_irn_mode(node)))
6699 return new_Unknown(get_irn_mode(node));
6704 /* With this code we violate the agreement that local_optimize
6705 only leaves Bads in Block, Phi and Tuple nodes. */
6706 /* If Block has only Bads as predecessors it's garbage. */
6707 /* If Phi has only Bads as predecessors it's garbage. */
6708 if ((op == op_Block && get_Block_matured(node)) || op == op_Phi) {
6709 irn_arity = get_irn_arity(node);
6710 for (i = 0; i < irn_arity; i++) {
6711 if (!is_Bad(get_irn_n(node, i))) break;
6713 if (i == irn_arity) node = new_Bad();
6720 * These optimizations deallocate nodes from the obstack.
6721 * It can only be called if it is guaranteed that no other nodes
6722 * reference this one, i.e., right after construction of a node.
6724 * @param n The node to optimize
6726 * current_ir_graph must be set to the graph of the node!
6728 ir_node *optimize_node(ir_node *n)
6732 ir_opcode iro = get_irn_opcode(n);
6734 /* Always optimize Phi nodes: part of the construction. */
6735 if ((!get_opt_optimize()) && (iro != iro_Phi)) return n;
6737 /* constant expression evaluation / constant folding */
6738 if (get_opt_constant_folding()) {
6739 /* neither constants nor Tuple values can be evaluated */
6740 if (iro != iro_Const && (get_irn_mode(n) != mode_T)) {
6741 unsigned fp_model = get_irg_fp_model(current_ir_graph);
6742 int old_fp_mode = tarval_fp_ops_enabled();
6744 tarval_enable_fp_ops(! (fp_model & fp_no_float_fold));
6746 /* try to evaluate */
6747 tv = computed_value(n);
6748 if (tv != tarval_bad) {
6750 ir_type *old_tp = get_irn_type(n);
6751 int i, arity = get_irn_arity(n);
6755 * Try to recover the type of the new expression.
6757 for (i = 0; i < arity && !old_tp; ++i)
6758 old_tp = get_irn_type(get_irn_n(n, i));
6761 * we MUST copy the node here temporary, because it's still needed
6762 * for DBG_OPT_CSTEVAL
6764 node_size = offsetof(ir_node, attr) + n->op->attr_size;
6765 oldn = alloca(node_size);
6767 memcpy(oldn, n, node_size);
6768 CLONE_ARR_A(ir_node *, oldn->in, n->in);
6770 /* ARG, copy the in array, we need it for statistics */
6771 memcpy(oldn->in, n->in, ARR_LEN(n->in) * sizeof(n->in[0]));
6773 /* note the inplace edges module */
6774 edges_node_deleted(n, current_ir_graph);
6776 /* evaluation was successful -- replace the node. */
6777 irg_kill_node(current_ir_graph, n);
6780 if (old_tp && get_type_mode(old_tp) == get_tarval_mode(tv))
6781 set_Const_type(nw, old_tp);
6782 DBG_OPT_CSTEVAL(oldn, nw);
6783 tarval_enable_fp_ops(old_fp_mode);
6786 tarval_enable_fp_ops(old_fp_mode);
6790 /* remove unnecessary nodes */
6791 if (get_opt_algebraic_simplification() ||
6792 (iro == iro_Phi) || /* always optimize these nodes. */
6794 (iro == iro_Proj) ||
6795 (iro == iro_Block) ) /* Flags tested local. */
6796 n = equivalent_node(n);
6798 /* Common Subexpression Elimination.
6800 * Checks whether n is already available.
6801 * The block input is used to distinguish different subexpressions. Right
6802 * now all nodes are op_pin_state_pinned to blocks, i.e., the CSE only finds common
6803 * subexpressions within a block.
6806 n = identify_cons(current_ir_graph->value_table, n);
6809 edges_node_deleted(oldn, current_ir_graph);
6811 /* We found an existing, better node, so we can deallocate the old node. */
6812 irg_kill_node(current_ir_graph, oldn);
6816 /* Some more constant expression evaluation that does not allow to
6818 iro = get_irn_opcode(n);
6819 if (get_opt_algebraic_simplification() ||
6820 (iro == iro_Cond) ||
6821 (iro == iro_Proj)) /* Flags tested local. */
6822 n = transform_node(n);
6824 /* Remove nodes with dead (Bad) input.
6825 Run always for transformation induced Bads. */
6828 /* Now we have a legal, useful node. Enter it in hash table for CSE */
6829 if (get_opt_cse() && (get_irn_opcode(n) != iro_Block)) {
6831 n = identify_remember(current_ir_graph->value_table, o);
6837 } /* optimize_node */
6841 * These optimizations never deallocate nodes (in place). This can cause dead
6842 * nodes lying on the obstack. Remove these by a dead node elimination,
6843 * i.e., a copying garbage collection.
6845 ir_node *optimize_in_place_2(ir_node *n)
6849 ir_opcode iro = get_irn_opcode(n);
6851 if (!get_opt_optimize() && !is_Phi(n)) return n;
6853 /* constant expression evaluation / constant folding */
6854 if (get_opt_constant_folding()) {
6855 /* neither constants nor Tuple values can be evaluated */
6856 if (iro != iro_Const && get_irn_mode(n) != mode_T) {
6857 unsigned fp_model = get_irg_fp_model(current_ir_graph);
6858 int old_fp_mode = tarval_fp_ops_enabled();
6860 tarval_enable_fp_ops((fp_model & fp_strict_algebraic) == 0);
6861 /* try to evaluate */
6862 tv = computed_value(n);
6863 if (tv != tarval_bad) {
6864 /* evaluation was successful -- replace the node. */
6865 ir_type *old_tp = get_irn_type(n);
6866 int i, arity = get_irn_arity(n);
6869 * Try to recover the type of the new expression.
6871 for (i = 0; i < arity && !old_tp; ++i)
6872 old_tp = get_irn_type(get_irn_n(n, i));
6876 if (old_tp && get_type_mode(old_tp) == get_tarval_mode(tv))
6877 set_Const_type(n, old_tp);
6879 DBG_OPT_CSTEVAL(oldn, n);
6880 tarval_enable_fp_ops(old_fp_mode);
6883 tarval_enable_fp_ops(old_fp_mode);
6887 /* remove unnecessary nodes */
6888 if (get_opt_constant_folding() ||
6889 (iro == iro_Phi) || /* always optimize these nodes. */
6890 (iro == iro_Id) || /* ... */
6891 (iro == iro_Proj) || /* ... */
6892 (iro == iro_Block) ) /* Flags tested local. */
6893 n = equivalent_node(n);
6895 /** common subexpression elimination **/
6896 /* Checks whether n is already available. */
6897 /* The block input is used to distinguish different subexpressions. Right
6898 now all nodes are op_pin_state_pinned to blocks, i.e., the cse only finds common
6899 subexpressions within a block. */
6900 if (get_opt_cse()) {
6902 n = identify_remember(current_ir_graph->value_table, o);
6907 /* Some more constant expression evaluation. */
6908 iro = get_irn_opcode(n);
6909 if (get_opt_constant_folding() ||
6910 (iro == iro_Cond) ||
6911 (iro == iro_Proj)) /* Flags tested local. */
6912 n = transform_node(n);
6914 /* Remove nodes with dead (Bad) input.
6915 Run always for transformation induced Bads. */
6918 /* Now we can verify the node, as it has no dead inputs any more. */
6921 /* Now we have a legal, useful node. Enter it in hash table for cse.
6922 Blocks should be unique anyways. (Except the successor of start:
6923 is cse with the start block!) */
6924 if (get_opt_cse() && (get_irn_opcode(n) != iro_Block)) {
6926 n = identify_remember(current_ir_graph->value_table, o);
6932 } /* optimize_in_place_2 */
6935 * Wrapper for external use, set proper status bits after optimization.
6937 ir_node *optimize_in_place(ir_node *n)
6939 /* Handle graph state */
6940 assert(get_irg_phase_state(current_ir_graph) != phase_building);
6942 if (get_opt_global_cse())
6943 set_irg_pinned(current_ir_graph, op_pin_state_floats);
6944 if (get_irg_outs_state(current_ir_graph) == outs_consistent)
6945 set_irg_outs_inconsistent(current_ir_graph);
6947 /* FIXME: Maybe we could also test whether optimizing the node can
6948 change the control graph. */
6949 set_irg_doms_inconsistent(current_ir_graph);
6950 return optimize_in_place_2(n);
6951 } /* optimize_in_place */
6954 * Calculate a hash value of a Const node.
6956 static unsigned hash_Const(const ir_node *node)
6960 /* special value for const, as they only differ in their tarval. */
6961 h = HASH_PTR(node->attr.con.tv);
6967 * Calculate a hash value of a SymConst node.
6969 static unsigned hash_SymConst(const ir_node *node)
6973 /* all others are pointers */
6974 h = HASH_PTR(node->attr.symc.sym.type_p);
6977 } /* hash_SymConst */
6980 * Set the default hash operation in an ir_op_ops.
6982 * @param code the opcode for the default operation
6983 * @param ops the operations initialized
6988 static ir_op_ops *firm_set_default_hash(ir_opcode code, ir_op_ops *ops)
6992 ops->hash = hash_##a; \
6995 /* hash function already set */
6996 if (ops->hash != NULL)
7003 /* use input/mode default hash if no function was given */
7004 ops->hash = firm_default_hash;
7012 * Sets the default operation for an ir_ops.
7014 ir_op_ops *firm_set_default_operations(ir_opcode code, ir_op_ops *ops)
7016 ops = firm_set_default_hash(code, ops);
7017 ops = firm_set_default_computed_value(code, ops);
7018 ops = firm_set_default_equivalent_node(code, ops);
7019 ops = firm_set_default_transform_node(code, ops);
7020 ops = firm_set_default_node_cmp_attr(code, ops);
7021 ops = firm_set_default_get_type(code, ops);
7022 ops = firm_set_default_get_type_attr(code, ops);
7023 ops = firm_set_default_get_entity_attr(code, ops);
7026 } /* firm_set_default_operations */