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 vrp_attr *vrp = vrp_get_info(n);
700 if (vrp && tarval_is_all_one(
701 tarval_or(vrp->bits_set, vrp->bits_not_set))) {
702 return vrp->bits_set;
704 if (n->op->ops.computed_value)
705 return n->op->ops.computed_value(n);
707 } /* computed_value */
710 * Set the default computed_value evaluator in an ir_op_ops.
712 * @param code the opcode for the default operation
713 * @param ops the operations initialized
718 static ir_op_ops *firm_set_default_computed_value(ir_opcode code, ir_op_ops *ops)
722 ops->computed_value = computed_value_##a; \
724 #define CASE_PROJ(a) \
726 ops->computed_value_Proj = computed_value_Proj_##a; \
763 } /* firm_set_default_computed_value */
766 * Returns a equivalent block for another block.
767 * If the block has only one predecessor, this is
768 * the equivalent one. If the only predecessor of a block is
769 * the block itself, this is a dead block.
771 * If both predecessors of a block are the branches of a binary
772 * Cond, the equivalent block is Cond's block.
774 * If all predecessors of a block are bad or lies in a dead
775 * block, the current block is dead as well.
777 * Note, that blocks are NEVER turned into Bad's, instead
778 * the dead_block flag is set. So, never test for is_Bad(block),
779 * always use is_dead_Block(block).
781 static ir_node *equivalent_node_Block(ir_node *n)
786 /* don't optimize dead or labeled blocks */
787 if (is_Block_dead(n) || has_Block_entity(n))
790 n_preds = get_Block_n_cfgpreds(n);
792 /* The Block constructor does not call optimize, but mature_immBlock()
793 calls the optimization. */
794 assert(get_Block_matured(n));
796 /* Straightening: a single entry Block following a single exit Block
797 can be merged, if it is not the Start block. */
798 /* !!! Beware, all Phi-nodes of n must have been optimized away.
799 This should be true, as the block is matured before optimize is called.
800 But what about Phi-cycles with the Phi0/Id that could not be resolved?
801 Remaining Phi nodes are just Ids. */
803 ir_node *pred = skip_Proj(get_Block_cfgpred(n, 0));
806 ir_node *predblock = get_nodes_block(pred);
807 if (predblock == oldn) {
808 /* Jmp jumps into the block it is in -- deal self cycle. */
809 n = set_Block_dead(n);
810 DBG_OPT_DEAD_BLOCK(oldn, n);
811 } else if (get_opt_control_flow_straightening()) {
813 DBG_OPT_STG(oldn, n);
815 } else if (is_Cond(pred)) {
816 ir_node *predblock = get_nodes_block(pred);
817 if (predblock == oldn) {
818 /* Jmp jumps into the block it is in -- deal self cycle. */
819 n = set_Block_dead(n);
820 DBG_OPT_DEAD_BLOCK(oldn, n);
823 } else if ((n_preds == 2) &&
824 (get_opt_control_flow_weak_simplification())) {
825 /* Test whether Cond jumps twice to this block
826 * The more general case which more than 2 predecessors is handles
827 * in optimize_cf(), we handle only this special case for speed here.
829 ir_node *a = get_Block_cfgpred(n, 0);
830 ir_node *b = get_Block_cfgpred(n, 1);
832 if (is_Proj(a) && is_Proj(b)) {
833 ir_node *cond = get_Proj_pred(a);
835 if (cond == get_Proj_pred(b) && is_Cond(cond) &&
836 get_irn_mode(get_Cond_selector(cond)) == mode_b) {
837 /* Also a single entry Block following a single exit Block. Phis have
838 twice the same operand and will be optimized away. */
839 n = get_nodes_block(cond);
840 DBG_OPT_IFSIM1(oldn, a, b, n);
843 } else if (get_opt_unreachable_code() &&
844 (n != get_irg_start_block(current_ir_graph)) &&
845 (n != get_irg_end_block(current_ir_graph)) ) {
848 /* If all inputs are dead, this block is dead too, except if it is
849 the start or end block. This is one step of unreachable code
851 for (i = get_Block_n_cfgpreds(n) - 1; i >= 0; --i) {
852 ir_node *pred = get_Block_cfgpred(n, i);
855 if (is_Bad(pred)) continue;
856 pred_blk = get_nodes_block(skip_Proj(pred));
858 if (is_Block_dead(pred_blk)) continue;
861 /* really found a living input */
866 n = set_Block_dead(n);
867 DBG_OPT_DEAD_BLOCK(oldn, n);
872 } /* equivalent_node_Block */
875 * Returns a equivalent node for a Jmp, a Bad :-)
876 * Of course this only happens if the Block of the Jmp is dead.
878 static ir_node *equivalent_node_Jmp(ir_node *n)
882 /* unreachable code elimination */
883 if (is_Block_dead(get_nodes_block(n))) {
884 n = get_irg_bad(current_ir_graph);
885 DBG_OPT_DEAD_BLOCK(oldn, n);
888 } /* equivalent_node_Jmp */
890 /** Raise is handled in the same way as Jmp. */
891 #define equivalent_node_Raise equivalent_node_Jmp
894 /* We do not evaluate Cond here as we replace it by a new node, a Jmp.
895 See transform_node_Proj_Cond(). */
898 * Optimize operations that are commutative and have neutral 0,
899 * so a op 0 = 0 op a = a.
901 static ir_node *equivalent_node_neutral_zero(ir_node *n)
905 ir_node *a = get_binop_left(n);
906 ir_node *b = get_binop_right(n);
911 /* After running compute_node there is only one constant predecessor.
912 Find this predecessors value and remember the other node: */
913 if ((tv = value_of(a)) != tarval_bad) {
915 } else if ((tv = value_of(b)) != tarval_bad) {
920 /* If this predecessors constant value is zero, the operation is
921 * unnecessary. Remove it.
923 * Beware: If n is a Add, the mode of on and n might be different
924 * which happens in this rare construction: NULL + 3.
925 * Then, a Conv would be needed which we cannot include here.
927 if (tarval_is_null(tv) && get_irn_mode(on) == get_irn_mode(n)) {
930 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_0);
934 } /* equivalent_node_neutral_zero */
937 * Eor is commutative and has neutral 0.
939 static ir_node *equivalent_node_Eor(ir_node *n)
945 n = equivalent_node_neutral_zero(n);
946 if (n != oldn) return n;
949 b = get_Eor_right(n);
952 ir_node *aa = get_Eor_left(a);
953 ir_node *ab = get_Eor_right(a);
956 /* (a ^ b) ^ a -> b */
958 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_EOR_A_B_A);
960 } else if (ab == b) {
961 /* (a ^ b) ^ b -> a */
963 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_EOR_A_B_A);
968 ir_node *ba = get_Eor_left(b);
969 ir_node *bb = get_Eor_right(b);
972 /* a ^ (a ^ b) -> b */
974 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_EOR_A_B_A);
976 } else if (bb == a) {
977 /* a ^ (b ^ a) -> b */
979 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_EOR_A_B_A);
987 * Optimize a - 0 and (a - x) + x (for modes with wrap-around).
989 * The second one looks strange, but this construct
990 * is used heavily in the LCC sources :-).
992 * Beware: The Mode of an Add may be different than the mode of its
993 * predecessors, so we could not return a predecessors in all cases.
995 static ir_node *equivalent_node_Add(ir_node *n)
998 ir_node *left, *right;
999 ir_mode *mode = get_irn_mode(n);
1001 n = equivalent_node_neutral_zero(n);
1005 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
1006 if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
1009 left = get_Add_left(n);
1010 right = get_Add_right(n);
1013 if (get_Sub_right(left) == right) {
1016 n = get_Sub_left(left);
1017 if (mode == get_irn_mode(n)) {
1018 DBG_OPT_ALGSIM1(oldn, left, right, n, FS_OPT_ADD_SUB);
1023 if (is_Sub(right)) {
1024 if (get_Sub_right(right) == left) {
1027 n = get_Sub_left(right);
1028 if (mode == get_irn_mode(n)) {
1029 DBG_OPT_ALGSIM1(oldn, left, right, n, FS_OPT_ADD_SUB);
1035 } /* equivalent_node_Add */
1038 * optimize operations that are not commutative but have neutral 0 on left,
1041 static ir_node *equivalent_node_left_zero(ir_node *n)
1045 ir_node *a = get_binop_left(n);
1046 ir_node *b = get_binop_right(n);
1047 tarval *tb = value_of(b);
1049 if (tarval_is_null(tb)) {
1052 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_0);
1055 } /* equivalent_node_left_zero */
1057 #define equivalent_node_Shl equivalent_node_left_zero
1058 #define equivalent_node_Shr equivalent_node_left_zero
1059 #define equivalent_node_Shrs equivalent_node_left_zero
1060 #define equivalent_node_Rotl equivalent_node_left_zero
1063 * Optimize a - 0 and (a + x) - x (for modes with wrap-around).
1065 * The second one looks strange, but this construct
1066 * is used heavily in the LCC sources :-).
1068 * Beware: The Mode of a Sub may be different than the mode of its
1069 * predecessors, so we could not return a predecessors in all cases.
1071 static ir_node *equivalent_node_Sub(ir_node *n)
1075 ir_mode *mode = get_irn_mode(n);
1078 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
1079 if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
1082 b = get_Sub_right(n);
1085 /* Beware: modes might be different */
1086 if (tarval_is_null(tb)) {
1087 ir_node *a = get_Sub_left(n);
1088 if (mode == get_irn_mode(a)) {
1091 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_0);
1095 } /* equivalent_node_Sub */
1099 * Optimize an "self-inverse unary op", ie op(op(n)) = n.
1102 * -(-a) == a, but might overflow two times.
1103 * We handle it anyway here but the better way would be a
1104 * flag. This would be needed for Pascal for instance.
1106 static ir_node *equivalent_node_idempotent_unop(ir_node *n)
1109 ir_node *pred = get_unop_op(n);
1111 /* optimize symmetric unop */
1112 if (get_irn_op(pred) == get_irn_op(n)) {
1113 n = get_unop_op(pred);
1114 DBG_OPT_ALGSIM2(oldn, pred, n, FS_OPT_IDEM_UNARY);
1117 } /* equivalent_node_idempotent_unop */
1119 /** Optimize Not(Not(x)) == x. */
1120 #define equivalent_node_Not equivalent_node_idempotent_unop
1122 /** -(-x) == x ??? Is this possible or can --x raise an
1123 out of bounds exception if min =! max? */
1124 #define equivalent_node_Minus equivalent_node_idempotent_unop
1127 * Optimize a * 1 = 1 * a = a.
1129 static ir_node *equivalent_node_Mul(ir_node *n)
1132 ir_node *a = get_Mul_left(n);
1134 /* we can handle here only the n * n = n bit cases */
1135 if (get_irn_mode(n) == get_irn_mode(a)) {
1136 ir_node *b = get_Mul_right(n);
1140 * Mul is commutative and has again an other neutral element.
1141 * Constants are place right, so check this case first.
1144 if (tarval_is_one(tv)) {
1146 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
1149 if (tarval_is_one(tv)) {
1151 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
1156 } /* equivalent_node_Mul */
1159 * Use algebraic simplification a | a = a | 0 = 0 | a = a.
1161 static ir_node *equivalent_node_Or(ir_node *n)
1165 ir_node *a = get_Or_left(n);
1166 ir_node *b = get_Or_right(n);
1170 n = a; /* Or has it's own neutral element */
1171 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_OR);
1174 /* constants are cormalized to right, check this site first */
1176 if (tarval_is_null(tv)) {
1178 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_OR);
1182 if (tarval_is_null(tv)) {
1184 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_OR);
1189 } /* equivalent_node_Or */
1192 * Optimize a & 0b1...1 = 0b1...1 & a = a & a = (a|X) & a = a.
1194 static ir_node *equivalent_node_And(ir_node *n)
1198 ir_node *a = get_And_left(n);
1199 ir_node *b = get_And_right(n);
1203 n = a; /* And has it's own neutral element */
1204 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_AND);
1207 /* constants are normalized to right, check this site first */
1209 if (tarval_is_all_one(tv)) {
1211 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1214 if (tv != get_tarval_bad()) {
1215 ir_mode *mode = get_irn_mode(n);
1216 if (!mode_is_signed(mode) && is_Conv(a)) {
1217 ir_node *convop = get_Conv_op(a);
1218 ir_mode *convopmode = get_irn_mode(convop);
1219 if (!mode_is_signed(convopmode)) {
1220 if (tarval_is_all_one(tarval_convert_to(tv, convopmode))) {
1221 /* Conv(X) & all_one(mode(X)) = Conv(X) */
1223 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1230 if (tarval_is_all_one(tv)) {
1232 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1236 if (b == get_Or_left(a) || b == get_Or_right(a)) {
1239 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1244 if (a == get_Or_left(b) || a == get_Or_right(b)) {
1247 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1252 } /* equivalent_node_And */
1255 * Try to remove useless Conv's:
1257 static ir_node *equivalent_node_Conv(ir_node *n)
1260 ir_node *a = get_Conv_op(n);
1262 ir_mode *n_mode = get_irn_mode(n);
1263 ir_mode *a_mode = get_irn_mode(a);
1266 if (n_mode == a_mode) { /* No Conv necessary */
1267 if (get_Conv_strict(n)) {
1270 /* neither Minus nor Abs nor Confirm change the precision,
1271 so we can "look-through" */
1274 p = get_Minus_op(p);
1275 } else if (is_Abs(p)) {
1277 } else if (is_Confirm(p)) {
1278 p = get_Confirm_value(p);
1284 if (is_Conv(p) && get_Conv_strict(p)) {
1285 /* we known already, that a_mode == n_mode, and neither
1286 Abs nor Minus change the mode, so the second Conv
1288 assert(get_irn_mode(p) == n_mode);
1290 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CONV);
1294 ir_node *pred = get_Proj_pred(p);
1295 if (is_Load(pred)) {
1296 /* Loads always return with the exact precision of n_mode */
1297 assert(get_Load_mode(pred) == n_mode);
1299 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CONV);
1302 if (is_Proj(pred) && get_Proj_proj(pred) == pn_Start_T_args) {
1303 pred = get_Proj_pred(pred);
1304 if (is_Start(pred)) {
1305 /* Arguments always return with the exact precision,
1306 as strictConv's are place before Call -- if the
1307 caller was compiled with the same setting.
1308 Otherwise, the semantics is probably still right. */
1309 assert(get_irn_mode(p) == n_mode);
1311 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CONV);
1317 /* special case: the immediate predecessor is also a Conv */
1318 if (! get_Conv_strict(a)) {
1319 /* first one is not strict, kick it */
1321 a_mode = get_irn_mode(a);
1325 /* else both are strict conv, second is superfluous */
1327 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CONV);
1332 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CONV);
1335 } else if (is_Conv(a)) { /* Conv(Conv(b)) */
1336 ir_node *b = get_Conv_op(a);
1337 ir_mode *b_mode = get_irn_mode(b);
1339 if (get_Conv_strict(n) && get_Conv_strict(a)) {
1340 /* both are strict conv */
1341 if (smaller_mode(a_mode, n_mode)) {
1342 /* both are strict, but the first is smaller, so
1343 the second cannot remove more precision, remove the
1345 set_Conv_strict(n, 0);
1348 if (n_mode == b_mode) {
1349 if (! get_Conv_strict(n) && ! get_Conv_strict(a)) {
1350 if (n_mode == mode_b) {
1351 n = b; /* Convb(Conv*(xxxb(...))) == xxxb(...) */
1352 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
1354 } else if (get_mode_arithmetic(n_mode) == get_mode_arithmetic(a_mode)) {
1355 if (values_in_mode(b_mode, a_mode)) {
1356 n = b; /* ConvS(ConvL(xxxS(...))) == xxxS(...) */
1357 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
1362 if (mode_is_int(n_mode) && get_mode_arithmetic(a_mode) == irma_ieee754) {
1363 /* ConvI(ConvF(I)) -> I, iff float mantissa >= int mode */
1364 unsigned int_mantissa = get_mode_size_bits(n_mode) - (mode_is_signed(n_mode) ? 1 : 0);
1365 unsigned float_mantissa = tarval_ieee754_get_mantissa_size(a_mode);
1367 if (float_mantissa >= int_mantissa) {
1369 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
1374 if (smaller_mode(b_mode, a_mode)) {
1375 if (get_Conv_strict(n))
1376 set_Conv_strict(b, 1);
1377 n = b; /* ConvA(ConvB(ConvA(...))) == ConvA(...) */
1378 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
1385 } /* equivalent_node_Conv */
1388 * A Cast may be removed if the type of the previous node
1389 * is already the type of the Cast.
1391 static ir_node *equivalent_node_Cast(ir_node *n)
1394 ir_node *pred = get_Cast_op(n);
1396 if (get_irn_type(pred) == get_Cast_type(n)) {
1398 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CAST);
1401 } /* equivalent_node_Cast */
1404 * - fold Phi-nodes, iff they have only one predecessor except
1407 static ir_node *equivalent_node_Phi(ir_node *n)
1413 ir_node *first_val = NULL; /* to shutup gcc */
1415 if (!get_opt_normalize()) return n;
1417 n_preds = get_Phi_n_preds(n);
1419 block = get_nodes_block(n);
1420 if (is_Block_dead(block)) /* Control dead */
1421 return get_irg_bad(current_ir_graph);
1423 if (n_preds == 0) return n; /* Phi of dead Region without predecessors. */
1425 /* Find first non-self-referencing input */
1426 for (i = 0; i < n_preds; ++i) {
1427 first_val = get_Phi_pred(n, i);
1428 if ( (first_val != n) /* not self pointer */
1430 /* BEWARE: when the if is changed to 1, Phi's will ignore it's Bad
1431 * predecessors. Then, Phi nodes in dead code might be removed, causing
1432 * nodes pointing to themself (Add's for instance).
1433 * This is really bad and causes endless recursions in several
1434 * code pathes, so we do NOT optimize such a code.
1435 * This is not that bad as it sounds, optimize_cf() removes bad control flow
1436 * (and bad Phi predecessors), so live code is optimized later.
1438 && (! is_Bad(get_Block_cfgpred(block, i)))
1440 ) { /* value not dead */
1441 break; /* then found first value. */
1446 /* A totally Bad or self-referencing Phi (we didn't break the above loop) */
1447 return get_irg_bad(current_ir_graph);
1450 /* search for rest of inputs, determine if any of these
1451 are non-self-referencing */
1452 while (++i < n_preds) {
1453 ir_node *scnd_val = get_Phi_pred(n, i);
1454 if ( (scnd_val != n)
1455 && (scnd_val != first_val)
1458 && (! is_Bad(get_Block_cfgpred(block, i)))
1466 /* Fold, if no multiple distinct non-self-referencing inputs */
1468 DBG_OPT_PHI(oldn, n);
1471 } /* equivalent_node_Phi */
1474 * Several optimizations:
1475 * - fold Sync-nodes, iff they have only one predecessor except
1478 static ir_node *equivalent_node_Sync(ir_node *n)
1480 int arity = get_Sync_n_preds(n);
1483 for (i = 0; i < arity;) {
1484 ir_node *pred = get_Sync_pred(n, i);
1487 /* Remove Bad predecessors */
1494 /* Remove duplicate predecessors */
1500 if (get_Sync_pred(n, j) == pred) {
1508 if (arity == 0) return get_irg_bad(current_ir_graph);
1509 if (arity == 1) return get_Sync_pred(n, 0);
1511 } /* equivalent_node_Sync */
1514 * Optimize Proj(Tuple).
1516 static ir_node *equivalent_node_Proj_Tuple(ir_node *proj)
1518 ir_node *oldn = proj;
1519 ir_node *tuple = get_Proj_pred(proj);
1521 /* Remove the Tuple/Proj combination. */
1522 proj = get_Tuple_pred(tuple, get_Proj_proj(proj));
1523 DBG_OPT_TUPLE(oldn, tuple, proj);
1526 } /* equivalent_node_Proj_Tuple */
1529 * Optimize a / 1 = a.
1531 static ir_node *equivalent_node_Proj_Div(ir_node *proj)
1533 ir_node *oldn = proj;
1534 ir_node *div = get_Proj_pred(proj);
1535 ir_node *b = get_Div_right(div);
1536 tarval *tb = value_of(b);
1538 /* Div is not commutative. */
1539 if (tarval_is_one(tb)) { /* div(x, 1) == x */
1540 switch (get_Proj_proj(proj)) {
1542 proj = get_Div_mem(div);
1543 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NEUTRAL_1);
1547 proj = get_Div_left(div);
1548 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NEUTRAL_1);
1552 /* we cannot replace the exception Proj's here, this is done in
1553 transform_node_Proj_Div() */
1558 } /* equivalent_node_Proj_Div */
1561 * Optimize a / 1.0 = a.
1563 static ir_node *equivalent_node_Proj_Quot(ir_node *proj)
1565 ir_node *oldn = proj;
1566 ir_node *quot = get_Proj_pred(proj);
1567 ir_node *b = get_Quot_right(quot);
1568 tarval *tb = value_of(b);
1570 /* Div is not commutative. */
1571 if (tarval_is_one(tb)) { /* Quot(x, 1) == x */
1572 switch (get_Proj_proj(proj)) {
1574 proj = get_Quot_mem(quot);
1575 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NEUTRAL_1);
1579 proj = get_Quot_left(quot);
1580 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NEUTRAL_1);
1584 /* we cannot replace the exception Proj's here, this is done in
1585 transform_node_Proj_Quot() */
1590 } /* equivalent_node_Proj_Quot */
1593 * Optimize a / 1 = a.
1595 static ir_node *equivalent_node_Proj_DivMod(ir_node *proj)
1597 ir_node *oldn = proj;
1598 ir_node *divmod = get_Proj_pred(proj);
1599 ir_node *b = get_DivMod_right(divmod);
1600 tarval *tb = value_of(b);
1602 /* Div is not commutative. */
1603 if (tarval_is_one(tb)) { /* div(x, 1) == x */
1604 switch (get_Proj_proj(proj)) {
1606 proj = get_DivMod_mem(divmod);
1607 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NEUTRAL_1);
1610 case pn_DivMod_res_div:
1611 proj = get_DivMod_left(divmod);
1612 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NEUTRAL_1);
1616 /* we cannot replace the exception Proj's here, this is done in
1617 transform_node_Proj_DivMod().
1618 Note further that the pn_DivMod_res_div case is handled in
1619 computed_value_Proj(). */
1624 } /* equivalent_node_Proj_DivMod */
1627 * Optimize CopyB(mem, x, x) into a Nop.
1629 static ir_node *equivalent_node_Proj_CopyB(ir_node *proj)
1631 ir_node *oldn = proj;
1632 ir_node *copyb = get_Proj_pred(proj);
1633 ir_node *a = get_CopyB_dst(copyb);
1634 ir_node *b = get_CopyB_src(copyb);
1637 /* Turn CopyB into a tuple (mem, jmp, bad, bad) */
1638 switch (get_Proj_proj(proj)) {
1639 case pn_CopyB_M_regular:
1640 proj = get_CopyB_mem(copyb);
1641 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NOP);
1644 case pn_CopyB_X_except:
1645 DBG_OPT_EXC_REM(proj);
1646 proj = get_irg_bad(current_ir_graph);
1651 } /* equivalent_node_Proj_CopyB */
1654 * Optimize Bounds(idx, idx, upper) into idx.
1656 static ir_node *equivalent_node_Proj_Bound(ir_node *proj)
1658 ir_node *oldn = proj;
1659 ir_node *bound = get_Proj_pred(proj);
1660 ir_node *idx = get_Bound_index(bound);
1661 ir_node *pred = skip_Proj(idx);
1664 if (idx == get_Bound_lower(bound))
1666 else if (is_Bound(pred)) {
1668 * idx was Bounds checked in the same MacroBlock previously,
1669 * it is still valid if lower <= pred_lower && pred_upper <= upper.
1671 ir_node *lower = get_Bound_lower(bound);
1672 ir_node *upper = get_Bound_upper(bound);
1673 if (get_Bound_lower(pred) == lower &&
1674 get_Bound_upper(pred) == upper &&
1675 get_irn_MacroBlock(bound) == get_irn_MacroBlock(pred)) {
1677 * One could expect that we simply return the previous
1678 * Bound here. However, this would be wrong, as we could
1679 * add an exception Proj to a new location then.
1680 * So, we must turn in into a tuple.
1686 /* Turn Bound into a tuple (mem, jmp, bad, idx) */
1687 switch (get_Proj_proj(proj)) {
1689 DBG_OPT_EXC_REM(proj);
1690 proj = get_Bound_mem(bound);
1692 case pn_Bound_X_except:
1693 DBG_OPT_EXC_REM(proj);
1694 proj = get_irg_bad(current_ir_graph);
1698 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NOP);
1701 /* cannot optimize pn_Bound_X_regular, handled in transform ... */
1706 } /* equivalent_node_Proj_Bound */
1709 * Optimize an Exception Proj(Load) with a non-null address.
1711 static ir_node *equivalent_node_Proj_Load(ir_node *proj)
1713 if (get_opt_ldst_only_null_ptr_exceptions()) {
1714 if (get_irn_mode(proj) == mode_X) {
1715 ir_node *load = get_Proj_pred(proj);
1717 /* get the Load address */
1718 const ir_node *addr = get_Load_ptr(load);
1719 const ir_node *confirm;
1721 if (value_not_null(addr, &confirm)) {
1722 if (get_Proj_proj(proj) == pn_Load_X_except) {
1723 DBG_OPT_EXC_REM(proj);
1724 return get_irg_bad(current_ir_graph);
1730 } /* equivalent_node_Proj_Load */
1733 * Optimize an Exception Proj(Store) with a non-null address.
1735 static ir_node *equivalent_node_Proj_Store(ir_node *proj)
1737 if (get_opt_ldst_only_null_ptr_exceptions()) {
1738 if (get_irn_mode(proj) == mode_X) {
1739 ir_node *store = get_Proj_pred(proj);
1741 /* get the load/store address */
1742 const ir_node *addr = get_Store_ptr(store);
1743 const ir_node *confirm;
1745 if (value_not_null(addr, &confirm)) {
1746 if (get_Proj_proj(proj) == pn_Store_X_except) {
1747 DBG_OPT_EXC_REM(proj);
1748 return get_irg_bad(current_ir_graph);
1754 } /* equivalent_node_Proj_Store */
1757 * Does all optimizations on nodes that must be done on it's Proj's
1758 * because of creating new nodes.
1760 static ir_node *equivalent_node_Proj(ir_node *proj)
1762 ir_node *n = get_Proj_pred(proj);
1764 if (get_irn_mode(proj) == mode_X) {
1765 if (is_Block_dead(get_nodes_block(n))) {
1766 /* Remove dead control flow -- early gigo(). */
1767 return get_irg_bad(current_ir_graph);
1770 if (n->op->ops.equivalent_node_Proj)
1771 return n->op->ops.equivalent_node_Proj(proj);
1773 } /* equivalent_node_Proj */
1778 static ir_node *equivalent_node_Id(ir_node *n)
1786 DBG_OPT_ID(oldn, n);
1788 } /* equivalent_node_Id */
1793 static ir_node *equivalent_node_Mux(ir_node *n)
1795 ir_node *oldn = n, *sel = get_Mux_sel(n);
1797 tarval *ts = value_of(sel);
1799 /* Mux(true, f, t) == t */
1800 if (ts == tarval_b_true) {
1801 n = get_Mux_true(n);
1802 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_C);
1805 /* Mux(false, f, t) == f */
1806 if (ts == tarval_b_false) {
1807 n = get_Mux_false(n);
1808 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_C);
1811 n_t = get_Mux_true(n);
1812 n_f = get_Mux_false(n);
1814 /* Mux(v, x, T) == x */
1815 if (is_Unknown(n_f)) {
1817 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_EQ);
1820 /* Mux(v, T, x) == x */
1821 if (is_Unknown(n_t)) {
1823 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_EQ);
1827 /* Mux(v, x, x) == x */
1830 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_EQ);
1833 if (is_Proj(sel) && !mode_honor_signed_zeros(get_irn_mode(n))) {
1834 ir_node *cmp = get_Proj_pred(sel);
1835 long proj_nr = get_Proj_proj(sel);
1836 ir_node *f = get_Mux_false(n);
1837 ir_node *t = get_Mux_true(n);
1840 * Note further that these optimization work even for floating point
1841 * with NaN's because -NaN == NaN.
1842 * However, if +0 and -0 is handled differently, we cannot use the first one.
1845 ir_node *const cmp_l = get_Cmp_left(cmp);
1846 ir_node *const cmp_r = get_Cmp_right(cmp);
1850 if ((cmp_l == t && cmp_r == f) || /* Mux(t == f, t, f) -> f */
1851 (cmp_l == f && cmp_r == t)) { /* Mux(f == t, t, f) -> f */
1853 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1860 if ((cmp_l == t && cmp_r == f) || /* Mux(t != f, t, f) -> t */
1861 (cmp_l == f && cmp_r == t)) { /* Mux(f != t, t, f) -> t */
1863 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1870 * Note: normalization puts the constant on the right side,
1871 * so we check only one case.
1873 if (cmp_l == t && tarval_is_null(value_of(cmp_r))) {
1874 /* Mux(t CMP 0, X, t) */
1875 if (is_Minus(f) && get_Minus_op(f) == t) {
1876 /* Mux(t CMP 0, -t, t) */
1877 if (proj_nr == pn_Cmp_Eq) {
1878 /* Mux(t == 0, -t, t) ==> -t */
1880 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1881 } else if (proj_nr == pn_Cmp_Lg || proj_nr == pn_Cmp_Ne) {
1882 /* Mux(t != 0, -t, t) ==> t */
1884 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1891 } /* equivalent_node_Mux */
1894 * Remove Confirm nodes if setting is on.
1895 * Replace Confirms(x, '=', Constlike) by Constlike.
1897 static ir_node *equivalent_node_Confirm(ir_node *n)
1899 ir_node *pred = get_Confirm_value(n);
1900 pn_Cmp pnc = get_Confirm_cmp(n);
1902 while (is_Confirm(pred) && pnc == get_Confirm_cmp(pred)) {
1904 * rare case: two identical Confirms one after another,
1905 * replace the second one with the first.
1908 pred = get_Confirm_value(n);
1909 pnc = get_Confirm_cmp(n);
1911 if (get_opt_remove_confirm())
1912 return get_Confirm_value(n);
1917 * equivalent_node() returns a node equivalent to input n. It skips all nodes that
1918 * perform no actual computation, as, e.g., the Id nodes. It does not create
1919 * new nodes. It is therefore safe to free n if the node returned is not n.
1920 * If a node returns a Tuple we can not just skip it. If the size of the
1921 * in array fits, we transform n into a tuple (e.g., Div).
1923 ir_node *equivalent_node(ir_node *n)
1925 if (n->op->ops.equivalent_node)
1926 return n->op->ops.equivalent_node(n);
1928 } /* equivalent_node */
1931 * Sets the default equivalent node operation for an ir_op_ops.
1933 * @param code the opcode for the default operation
1934 * @param ops the operations initialized
1939 static ir_op_ops *firm_set_default_equivalent_node(ir_opcode code, ir_op_ops *ops)
1943 ops->equivalent_node = equivalent_node_##a; \
1945 #define CASE_PROJ(a) \
1947 ops->equivalent_node_Proj = equivalent_node_Proj_##a; \
1989 } /* firm_set_default_equivalent_node */
1992 * Returns non-zero if a node is a Phi node
1993 * with all predecessors constant.
1995 static int is_const_Phi(ir_node *n)
1999 if (! is_Phi(n) || get_irn_arity(n) == 0)
2001 for (i = get_irn_arity(n) - 1; i >= 0; --i) {
2002 if (! is_Const(get_irn_n(n, i)))
2006 } /* is_const_Phi */
2008 typedef tarval *(*tarval_sub_type)(tarval *a, tarval *b, ir_mode *mode);
2009 typedef tarval *(*tarval_binop_type)(tarval *a, tarval *b);
2012 * in reality eval_func should be tarval (*eval_func)() but incomplete
2013 * declarations are bad style and generate noisy warnings
2015 typedef void (*eval_func)(void);
2018 * Wrapper for the tarval binop evaluation, tarval_sub has one more parameter.
2020 static tarval *do_eval(eval_func eval, tarval *a, tarval *b, ir_mode *mode)
2022 if (eval == (eval_func) tarval_sub) {
2023 tarval_sub_type func = (tarval_sub_type)eval;
2025 return func(a, b, mode);
2027 tarval_binop_type func = (tarval_binop_type)eval;
2034 * Apply an evaluator on a binop with a constant operators (and one Phi).
2036 * @param phi the Phi node
2037 * @param other the other operand
2038 * @param eval an evaluator function
2039 * @param mode the mode of the result, may be different from the mode of the Phi!
2040 * @param left if non-zero, other is the left operand, else the right
2042 * @return a new Phi node if the conversion was successful, NULL else
2044 static ir_node *apply_binop_on_phi(ir_node *phi, tarval *other, eval_func eval, ir_mode *mode, int left)
2050 int i, n = get_irn_arity(phi);
2052 NEW_ARR_A(void *, res, n);
2054 for (i = 0; i < n; ++i) {
2055 pred = get_irn_n(phi, i);
2056 tv = get_Const_tarval(pred);
2057 tv = do_eval(eval, other, tv, mode);
2059 if (tv == tarval_bad) {
2060 /* folding failed, bad */
2066 for (i = 0; i < n; ++i) {
2067 pred = get_irn_n(phi, i);
2068 tv = get_Const_tarval(pred);
2069 tv = do_eval(eval, tv, other, mode);
2071 if (tv == tarval_bad) {
2072 /* folding failed, bad */
2078 irg = current_ir_graph;
2079 for (i = 0; i < n; ++i) {
2080 pred = get_irn_n(phi, i);
2081 res[i] = new_r_Const_type(irg, res[i], get_Const_type(pred));
2083 return new_r_Phi(get_nodes_block(phi), n, (ir_node **)res, mode);
2084 } /* apply_binop_on_phi */
2087 * Apply an evaluator on a binop with two constant Phi.
2089 * @param a the left Phi node
2090 * @param b the right Phi node
2091 * @param eval an evaluator function
2092 * @param mode the mode of the result, may be different from the mode of the Phi!
2094 * @return a new Phi node if the conversion was successful, NULL else
2096 static ir_node *apply_binop_on_2_phis(ir_node *a, ir_node *b, eval_func eval, ir_mode *mode)
2098 tarval *tv_l, *tv_r, *tv;
2104 if (get_nodes_block(a) != get_nodes_block(b))
2107 n = get_irn_arity(a);
2108 NEW_ARR_A(void *, res, n);
2110 for (i = 0; i < n; ++i) {
2111 pred = get_irn_n(a, i);
2112 tv_l = get_Const_tarval(pred);
2113 pred = get_irn_n(b, i);
2114 tv_r = get_Const_tarval(pred);
2115 tv = do_eval(eval, tv_l, tv_r, mode);
2117 if (tv == tarval_bad) {
2118 /* folding failed, bad */
2123 irg = current_ir_graph;
2124 for (i = 0; i < n; ++i) {
2125 pred = get_irn_n(a, i);
2126 res[i] = new_r_Const_type(irg, res[i], get_Const_type(pred));
2128 return new_r_Phi(get_nodes_block(a), n, (ir_node **)res, mode);
2129 } /* apply_binop_on_2_phis */
2132 * Apply an evaluator on a unop with a constant operator (a Phi).
2134 * @param phi the Phi node
2135 * @param eval an evaluator function
2137 * @return a new Phi node if the conversion was successful, NULL else
2139 static ir_node *apply_unop_on_phi(ir_node *phi, tarval *(*eval)(tarval *))
2146 int i, n = get_irn_arity(phi);
2148 NEW_ARR_A(void *, res, n);
2149 for (i = 0; i < n; ++i) {
2150 pred = get_irn_n(phi, i);
2151 tv = get_Const_tarval(pred);
2154 if (tv == tarval_bad) {
2155 /* folding failed, bad */
2160 mode = get_irn_mode(phi);
2161 irg = current_ir_graph;
2162 for (i = 0; i < n; ++i) {
2163 pred = get_irn_n(phi, i);
2164 res[i] = new_r_Const_type(irg, res[i], get_Const_type(pred));
2166 return new_r_Phi(get_nodes_block(phi), n, (ir_node **)res, mode);
2167 } /* apply_unop_on_phi */
2170 * Apply a conversion on a constant operator (a Phi).
2172 * @param phi the Phi node
2174 * @return a new Phi node if the conversion was successful, NULL else
2176 static ir_node *apply_conv_on_phi(ir_node *phi, ir_mode *mode)
2182 int i, n = get_irn_arity(phi);
2184 NEW_ARR_A(void *, res, n);
2185 for (i = 0; i < n; ++i) {
2186 pred = get_irn_n(phi, i);
2187 tv = get_Const_tarval(pred);
2188 tv = tarval_convert_to(tv, mode);
2190 if (tv == tarval_bad) {
2191 /* folding failed, bad */
2196 irg = current_ir_graph;
2197 for (i = 0; i < n; ++i) {
2198 pred = get_irn_n(phi, i);
2199 res[i] = new_r_Const_type(irg, res[i], get_Const_type(pred));
2201 return new_r_Phi(get_nodes_block(phi), n, (ir_node **)res, mode);
2202 } /* apply_conv_on_phi */
2205 * Transform AddP(P, ConvIs(Iu)), AddP(P, ConvIu(Is)) and
2206 * SubP(P, ConvIs(Iu)), SubP(P, ConvIu(Is)).
2207 * If possible, remove the Conv's.
2209 static ir_node *transform_node_AddSub(ir_node *n)
2211 ir_mode *mode = get_irn_mode(n);
2213 if (mode_is_reference(mode)) {
2214 ir_node *left = get_binop_left(n);
2215 ir_node *right = get_binop_right(n);
2216 unsigned ref_bits = get_mode_size_bits(mode);
2218 if (is_Conv(left)) {
2219 ir_mode *lmode = get_irn_mode(left);
2220 unsigned bits = get_mode_size_bits(lmode);
2222 if (ref_bits == bits &&
2223 mode_is_int(lmode) &&
2224 get_mode_arithmetic(lmode) == irma_twos_complement) {
2225 ir_node *pre = get_Conv_op(left);
2226 ir_mode *pre_mode = get_irn_mode(pre);
2228 if (mode_is_int(pre_mode) &&
2229 get_mode_size_bits(pre_mode) == bits &&
2230 get_mode_arithmetic(pre_mode) == irma_twos_complement) {
2231 /* ok, this conv just changes to sign, moreover the calculation
2232 * is done with same number of bits as our address mode, so
2233 * we can ignore the conv as address calculation can be viewed
2234 * as either signed or unsigned
2236 set_binop_left(n, pre);
2241 if (is_Conv(right)) {
2242 ir_mode *rmode = get_irn_mode(right);
2243 unsigned bits = get_mode_size_bits(rmode);
2245 if (ref_bits == bits &&
2246 mode_is_int(rmode) &&
2247 get_mode_arithmetic(rmode) == irma_twos_complement) {
2248 ir_node *pre = get_Conv_op(right);
2249 ir_mode *pre_mode = get_irn_mode(pre);
2251 if (mode_is_int(pre_mode) &&
2252 get_mode_size_bits(pre_mode) == bits &&
2253 get_mode_arithmetic(pre_mode) == irma_twos_complement) {
2254 /* ok, this conv just changes to sign, moreover the calculation
2255 * is done with same number of bits as our address mode, so
2256 * we can ignore the conv as address calculation can be viewed
2257 * as either signed or unsigned
2259 set_binop_right(n, pre);
2264 /* let address arithmetic use unsigned modes */
2265 if (is_Const(right)) {
2266 ir_mode *rmode = get_irn_mode(right);
2268 if (mode_is_signed(rmode) && get_mode_arithmetic(rmode) == irma_twos_complement) {
2269 /* convert a AddP(P, *s) into AddP(P, *u) */
2270 ir_mode *nm = get_reference_mode_unsigned_eq(mode);
2272 ir_node *pre = new_r_Conv(get_nodes_block(n), right, nm);
2273 set_binop_right(n, pre);
2279 } /* transform_node_AddSub */
2281 #define HANDLE_BINOP_PHI(eval, a, b, c, mode) \
2283 if (is_Const(b) && is_const_Phi(a)) { \
2284 /* check for Op(Phi, Const) */ \
2285 c = apply_binop_on_phi(a, get_Const_tarval(b), eval, mode, 0);\
2287 else if (is_Const(a) && is_const_Phi(b)) { \
2288 /* check for Op(Const, Phi) */ \
2289 c = apply_binop_on_phi(b, get_Const_tarval(a), eval, mode, 1);\
2291 else if (is_const_Phi(a) && is_const_Phi(b)) { \
2292 /* check for Op(Phi, Phi) */ \
2293 c = apply_binop_on_2_phis(a, b, eval, mode); \
2296 DBG_OPT_ALGSIM0(oldn, c, FS_OPT_CONST_PHI); \
2300 #define HANDLE_UNOP_PHI(eval, a, c) \
2302 if (is_const_Phi(a)) { \
2303 /* check for Op(Phi) */ \
2304 c = apply_unop_on_phi(a, eval); \
2306 DBG_OPT_ALGSIM0(oldn, c, FS_OPT_CONST_PHI); \
2312 * Do the AddSub optimization, then Transform
2313 * Constant folding on Phi
2314 * Add(a,a) -> Mul(a, 2)
2315 * Add(Mul(a, x), a) -> Mul(a, x+1)
2316 * if the mode is integer or float.
2317 * Transform Add(a,-b) into Sub(a,b).
2318 * Reassociation might fold this further.
2320 static ir_node *transform_node_Add(ir_node *n)
2323 ir_node *a, *b, *c, *oldn = n;
2325 n = transform_node_AddSub(n);
2327 a = get_Add_left(n);
2328 b = get_Add_right(n);
2330 mode = get_irn_mode(n);
2332 if (mode_is_reference(mode)) {
2333 ir_mode *lmode = get_irn_mode(a);
2335 if (is_Const(b) && is_Const_null(b) && mode_is_int(lmode)) {
2336 /* an Add(a, NULL) is a hidden Conv */
2337 dbg_info *dbg = get_irn_dbg_info(n);
2338 return new_rd_Conv(dbg, get_nodes_block(n), a, mode);
2342 HANDLE_BINOP_PHI((eval_func) tarval_add, a, b, c, mode);
2344 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
2345 if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
2348 if (mode_is_num(mode)) {
2349 /* the following code leads to endless recursion when Mul are replaced by a simple instruction chain */
2350 if (!is_irg_state(current_ir_graph, IR_GRAPH_STATE_ARCH_DEP)
2351 && a == b && mode_is_int(mode)) {
2352 ir_node *block = get_nodes_block(n);
2355 get_irn_dbg_info(n),
2358 new_Const_long(mode, 2),
2360 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_A);
2365 get_irn_dbg_info(n),
2370 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_MINUS_B);
2375 get_irn_dbg_info(n),
2380 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_MINUS_B);
2383 if (get_mode_arithmetic(mode) == irma_twos_complement) {
2384 /* Here we rely on constants be on the RIGHT side */
2386 ir_node *op = get_Not_op(a);
2388 if (is_Const(b) && is_Const_one(b)) {
2390 ir_node *blk = get_nodes_block(n);
2391 n = new_rd_Minus(get_irn_dbg_info(n), blk, op, mode);
2392 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_PLUS_1);
2397 n = new_Const(get_mode_minus_one(mode));
2398 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_X_NOT_X);
2403 ir_node *op = get_Not_op(b);
2407 n = new_Const(get_mode_minus_one(mode));
2408 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_X_NOT_X);
2415 vrp_attr *a_vrp, *b_vrp;
2416 a_vrp = vrp_get_info(a);
2417 b_vrp = vrp_get_info(b);
2420 if (a_vrp && b_vrp) {
2421 tarval *c = tarval_and(
2422 tarval_not(a_vrp->bits_not_set),
2423 tarval_not(b_vrp->bits_not_set)
2426 if (tarval_is_null(c)) {
2427 dbg_info *dbgi = get_irn_dbg_info(n);
2428 return new_rd_Or(dbgi, get_nodes_block(n),
2433 } /* transform_node_Add */
2436 * returns -cnst or NULL if impossible
2438 static ir_node *const_negate(ir_node *cnst)
2440 tarval *tv = tarval_neg(get_Const_tarval(cnst));
2441 dbg_info *dbgi = get_irn_dbg_info(cnst);
2442 ir_graph *irg = get_irn_irg(cnst);
2443 if (tv == tarval_bad) return NULL;
2444 return new_rd_Const(dbgi, irg, tv);
2448 * Do the AddSub optimization, then Transform
2449 * Constant folding on Phi
2450 * Sub(0,a) -> Minus(a)
2451 * Sub(Mul(a, x), a) -> Mul(a, x-1)
2452 * Sub(Sub(x, y), b) -> Sub(x, Add(y,b))
2453 * Sub(Add(a, x), x) -> a
2454 * Sub(x, Add(x, a)) -> -a
2455 * Sub(x, Const) -> Add(x, -Const)
2457 static ir_node *transform_node_Sub(ir_node *n)
2463 n = transform_node_AddSub(n);
2465 a = get_Sub_left(n);
2466 b = get_Sub_right(n);
2468 mode = get_irn_mode(n);
2470 if (mode_is_int(mode)) {
2471 ir_mode *lmode = get_irn_mode(a);
2473 if (is_Const(b) && is_Const_null(b) && mode_is_reference(lmode)) {
2474 /* a Sub(a, NULL) is a hidden Conv */
2475 dbg_info *dbg = get_irn_dbg_info(n);
2476 n = new_rd_Conv(dbg, get_nodes_block(n), a, mode);
2477 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_CONV);
2481 if (mode == lmode &&
2482 get_mode_arithmetic(mode) == irma_twos_complement &&
2484 get_Const_tarval(a) == get_mode_minus_one(mode)) {
2486 dbg_info *dbg = get_irn_dbg_info(n);
2487 n = new_rd_Not(dbg, get_nodes_block(n), b, mode);
2488 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_NOT);
2494 HANDLE_BINOP_PHI((eval_func) tarval_sub, a, b, c, mode);
2496 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
2497 if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
2500 if (is_Const(b) && !mode_is_reference(get_irn_mode(b))) {
2501 /* a - C -> a + (-C) */
2502 ir_node *cnst = const_negate(b);
2504 ir_node *block = get_nodes_block(n);
2505 dbg_info *dbgi = get_irn_dbg_info(n);
2507 n = new_rd_Add(dbgi, block, a, cnst, mode);
2508 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2513 if (is_Minus(a)) { /* (-a) - b -> -(a + b) */
2514 dbg_info *dbg = get_irn_dbg_info(n);
2515 ir_node *block = get_nodes_block(n);
2516 ir_node *left = get_Minus_op(a);
2517 ir_node *add = new_rd_Add(dbg, block, left, b, mode);
2519 n = new_rd_Minus(dbg, block, add, mode);
2520 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2522 } else if (is_Minus(b)) { /* a - (-b) -> a + b */
2523 dbg_info *dbg = get_irn_dbg_info(n);
2524 ir_node *block = get_nodes_block(n);
2525 ir_node *right = get_Minus_op(b);
2527 n = new_rd_Add(dbg, block, a, right, mode);
2528 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MINUS);
2530 } else if (is_Sub(b)) {
2531 /* a - (b - c) -> a + (c - b)
2532 * -> (a - b) + c iff (b - c) is a pointer */
2533 dbg_info *s_dbg = get_irn_dbg_info(b);
2534 ir_node *s_block = get_nodes_block(b);
2535 ir_node *s_left = get_Sub_left(b);
2536 ir_node *s_right = get_Sub_right(b);
2537 ir_mode *s_mode = get_irn_mode(b);
2538 if (mode_is_reference(s_mode)) {
2539 ir_node *sub = new_rd_Sub(s_dbg, s_block, a, s_left, mode);
2540 dbg_info *a_dbg = get_irn_dbg_info(n);
2541 ir_node *a_block = get_nodes_block(n);
2544 s_right = new_r_Conv(a_block, s_right, mode);
2545 n = new_rd_Add(a_dbg, a_block, sub, s_right, mode);
2547 ir_node *sub = new_rd_Sub(s_dbg, s_block, s_right, s_left, s_mode);
2548 dbg_info *a_dbg = get_irn_dbg_info(n);
2549 ir_node *a_block = get_nodes_block(n);
2551 n = new_rd_Add(a_dbg, a_block, a, sub, mode);
2553 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2555 } else if (is_Mul(b)) { /* a - (b * C) -> a + (b * -C) */
2556 ir_node *m_right = get_Mul_right(b);
2557 if (is_Const(m_right)) {
2558 ir_node *cnst2 = const_negate(m_right);
2559 if (cnst2 != NULL) {
2560 dbg_info *m_dbg = get_irn_dbg_info(b);
2561 ir_node *m_block = get_nodes_block(b);
2562 ir_node *m_left = get_Mul_left(b);
2563 ir_mode *m_mode = get_irn_mode(b);
2564 ir_node *mul = new_rd_Mul(m_dbg, m_block, m_left, cnst2, m_mode);
2565 dbg_info *a_dbg = get_irn_dbg_info(n);
2566 ir_node *a_block = get_nodes_block(n);
2568 n = new_rd_Add(a_dbg, a_block, a, mul, mode);
2569 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2575 /* Beware of Sub(P, P) which cannot be optimized into a simple Minus ... */
2576 if (mode_is_num(mode) && mode == get_irn_mode(a) && is_Const(a) && is_Const_null(a)) {
2578 get_irn_dbg_info(n),
2582 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_0_A);
2586 if (mode_wrap_around(mode)) {
2587 ir_node *left = get_Add_left(a);
2588 ir_node *right = get_Add_right(a);
2590 /* FIXME: Does the Conv's work only for two complement or generally? */
2592 if (mode != get_irn_mode(right)) {
2593 /* This Sub is an effective Cast */
2594 right = new_r_Conv(get_nodes_block(n), right, mode);
2597 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2599 } else if (right == b) {
2600 if (mode != get_irn_mode(left)) {
2601 /* This Sub is an effective Cast */
2602 left = new_r_Conv(get_nodes_block(n), left, mode);
2605 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2611 if (mode_wrap_around(mode)) {
2612 ir_node *left = get_Add_left(b);
2613 ir_node *right = get_Add_right(b);
2615 /* FIXME: Does the Conv's work only for two complement or generally? */
2617 ir_mode *r_mode = get_irn_mode(right);
2619 n = new_r_Minus(get_nodes_block(n), right, r_mode);
2620 if (mode != r_mode) {
2621 /* This Sub is an effective Cast */
2622 n = new_r_Conv(get_nodes_block(n), n, mode);
2624 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2626 } else if (right == a) {
2627 ir_mode *l_mode = get_irn_mode(left);
2629 n = new_r_Minus(get_nodes_block(n), left, l_mode);
2630 if (mode != l_mode) {
2631 /* This Sub is an effective Cast */
2632 n = new_r_Conv(get_nodes_block(n), n, mode);
2634 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2639 if (mode_is_int(mode) && is_Conv(a) && is_Conv(b)) {
2640 ir_mode *mode = get_irn_mode(a);
2642 if (mode == get_irn_mode(b)) {
2644 ir_node *op_a = get_Conv_op(a);
2645 ir_node *op_b = get_Conv_op(b);
2647 /* check if it's allowed to skip the conv */
2648 ma = get_irn_mode(op_a);
2649 mb = get_irn_mode(op_b);
2651 if (mode_is_reference(ma) && mode_is_reference(mb)) {
2652 /* SubInt(ConvInt(aP), ConvInt(bP)) -> SubInt(aP,bP) */
2655 set_Sub_right(n, b);
2661 /* do NOT execute this code if reassociation is enabled, it does the inverse! */
2662 if (!is_reassoc_running() && is_Mul(a)) {
2663 ir_node *ma = get_Mul_left(a);
2664 ir_node *mb = get_Mul_right(a);
2667 ir_node *blk = get_nodes_block(n);
2669 get_irn_dbg_info(n),
2673 get_irn_dbg_info(n),
2676 new_Const(get_mode_one(mode)),
2679 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MUL_A_X_A);
2681 } else if (mb == b) {
2682 ir_node *blk = get_nodes_block(n);
2684 get_irn_dbg_info(n),
2688 get_irn_dbg_info(n),
2691 new_Const(get_mode_one(mode)),
2694 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MUL_A_X_A);
2698 if (is_Sub(a)) { /* (x - y) - b -> x - (y + b) */
2699 ir_node *x = get_Sub_left(a);
2700 ir_node *y = get_Sub_right(a);
2701 ir_node *blk = get_nodes_block(n);
2702 ir_mode *m_b = get_irn_mode(b);
2703 ir_mode *m_y = get_irn_mode(y);
2707 /* Determine the right mode for the Add. */
2710 else if (mode_is_reference(m_b))
2712 else if (mode_is_reference(m_y))
2716 * Both modes are different but none is reference,
2717 * happens for instance in SubP(SubP(P, Iu), Is).
2718 * We have two possibilities here: Cast or ignore.
2719 * Currently we ignore this case.
2724 add = new_r_Add(blk, y, b, add_mode);
2726 n = new_rd_Sub(get_irn_dbg_info(n), blk, x, add, mode);
2727 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_SUB_X_Y_Z);
2731 if (get_mode_arithmetic(mode) == irma_twos_complement) {
2732 if (is_Const(a) && is_Not(b)) {
2733 /* c - ~X = X + (c+1) */
2734 tarval *tv = get_Const_tarval(a);
2736 tv = tarval_add(tv, get_mode_one(mode));
2737 if (tv != tarval_bad) {
2738 ir_node *blk = get_nodes_block(n);
2739 ir_node *c = new_Const(tv);
2740 n = new_rd_Add(get_irn_dbg_info(n), blk, get_Not_op(b), c, mode);
2741 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_C_NOT_X);
2747 } /* transform_node_Sub */
2750 * Several transformation done on n*n=2n bits mul.
2751 * These transformations must be done here because new nodes may be produced.
2753 static ir_node *transform_node_Mul2n(ir_node *n, ir_mode *mode)
2756 ir_node *a = get_Mul_left(n);
2757 ir_node *b = get_Mul_right(n);
2758 tarval *ta = value_of(a);
2759 tarval *tb = value_of(b);
2760 ir_mode *smode = get_irn_mode(a);
2762 if (ta == get_mode_one(smode)) {
2763 /* (L)1 * (L)b = (L)b */
2764 ir_node *blk = get_nodes_block(n);
2765 n = new_rd_Conv(get_irn_dbg_info(n), blk, b, mode);
2766 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
2769 else if (ta == get_mode_minus_one(smode)) {
2770 /* (L)-1 * (L)b = (L)b */
2771 ir_node *blk = get_nodes_block(n);
2772 n = new_rd_Minus(get_irn_dbg_info(n), blk, b, smode);
2773 n = new_rd_Conv(get_irn_dbg_info(n), blk, n, mode);
2774 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2777 if (tb == get_mode_one(smode)) {
2778 /* (L)a * (L)1 = (L)a */
2779 ir_node *blk = get_irn_n(a, -1);
2780 n = new_rd_Conv(get_irn_dbg_info(n), blk, a, mode);
2781 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
2784 else if (tb == get_mode_minus_one(smode)) {
2785 /* (L)a * (L)-1 = (L)-a */
2786 ir_node *blk = get_nodes_block(n);
2787 n = new_rd_Minus(get_irn_dbg_info(n), blk, a, smode);
2788 n = new_rd_Conv(get_irn_dbg_info(n), blk, n, mode);
2789 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2796 * Transform Mul(a,-1) into -a.
2797 * Do constant evaluation of Phi nodes.
2798 * Do architecture dependent optimizations on Mul nodes
2800 static ir_node *transform_node_Mul(ir_node *n)
2802 ir_node *c, *oldn = n;
2803 ir_mode *mode = get_irn_mode(n);
2804 ir_node *a = get_Mul_left(n);
2805 ir_node *b = get_Mul_right(n);
2807 if (is_Bad(a) || is_Bad(b))
2810 if (mode != get_irn_mode(a))
2811 return transform_node_Mul2n(n, mode);
2813 HANDLE_BINOP_PHI((eval_func) tarval_mul, a, b, c, mode);
2815 if (mode_is_signed(mode)) {
2818 if (value_of(a) == get_mode_minus_one(mode))
2820 else if (value_of(b) == get_mode_minus_one(mode))
2823 n = new_rd_Minus(get_irn_dbg_info(n), get_nodes_block(n), r, mode);
2824 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2829 if (is_Const(b)) { /* (-a) * const -> a * -const */
2830 ir_node *cnst = const_negate(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), cnst, mode);
2835 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2838 } else if (is_Minus(b)) { /* (-a) * (-b) -> a * b */
2839 dbg_info *dbgi = get_irn_dbg_info(n);
2840 ir_node *block = get_nodes_block(n);
2841 n = new_rd_Mul(dbgi, block, get_Minus_op(a), get_Minus_op(b), mode);
2842 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_MINUS);
2844 } else if (is_Sub(b)) { /* (-a) * (b - c) -> a * (c - b) */
2845 ir_node *sub_l = get_Sub_left(b);
2846 ir_node *sub_r = get_Sub_right(b);
2847 dbg_info *dbgi = get_irn_dbg_info(n);
2848 ir_node *block = get_nodes_block(n);
2849 ir_node *new_b = new_rd_Sub(dbgi, block, sub_r, sub_l, mode);
2850 n = new_rd_Mul(dbgi, block, get_Minus_op(a), new_b, mode);
2851 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS);
2854 } else if (is_Minus(b)) {
2855 if (is_Sub(a)) { /* (a - b) * (-c) -> (b - a) * c */
2856 ir_node *sub_l = get_Sub_left(a);
2857 ir_node *sub_r = get_Sub_right(a);
2858 dbg_info *dbgi = get_irn_dbg_info(n);
2859 ir_node *block = get_nodes_block(n);
2860 ir_node *new_a = new_rd_Sub(dbgi, block, sub_r, sub_l, mode);
2861 n = new_rd_Mul(dbgi, block, new_a, get_Minus_op(b), mode);
2862 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS);
2865 } else if (is_Shl(a)) {
2866 ir_node *const shl_l = get_Shl_left(a);
2867 if (is_Const(shl_l) && is_Const_one(shl_l)) {
2868 /* (1 << x) * b -> b << x */
2869 dbg_info *const dbgi = get_irn_dbg_info(n);
2870 ir_node *const block = get_nodes_block(n);
2871 ir_node *const shl_r = get_Shl_right(a);
2872 n = new_rd_Shl(dbgi, block, b, shl_r, mode);
2873 // TODO add me DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_SHIFT);
2876 } else if (is_Shl(b)) {
2877 ir_node *const shl_l = get_Shl_left(b);
2878 if (is_Const(shl_l) && is_Const_one(shl_l)) {
2879 /* a * (1 << x) -> a << x */
2880 dbg_info *const dbgi = get_irn_dbg_info(n);
2881 ir_node *const block = get_nodes_block(n);
2882 ir_node *const shl_r = get_Shl_right(b);
2883 n = new_rd_Shl(dbgi, block, a, shl_r, mode);
2884 // TODO add me DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_SHIFT);
2888 if (get_mode_arithmetic(mode) == irma_ieee754) {
2890 tarval *tv = get_Const_tarval(a);
2891 if (tarval_ieee754_get_exponent(tv) == 1 && tarval_ieee754_zero_mantissa(tv)
2892 && !tarval_is_negative(tv)) {
2893 /* 2.0 * b = b + b */
2894 n = new_rd_Add(get_irn_dbg_info(n), get_nodes_block(n), b, b, mode);
2895 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_A_A);
2899 else if (is_Const(b)) {
2900 tarval *tv = get_Const_tarval(b);
2901 if (tarval_ieee754_get_exponent(tv) == 1 && tarval_ieee754_zero_mantissa(tv)
2902 && !tarval_is_negative(tv)) {
2903 /* a * 2.0 = a + a */
2904 n = new_rd_Add(get_irn_dbg_info(n), get_nodes_block(n), a, a, mode);
2905 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_A_A);
2910 return arch_dep_replace_mul_with_shifts(n);
2911 } /* transform_node_Mul */
2914 * Transform a Div Node.
2916 static ir_node *transform_node_Div(ir_node *n)
2918 ir_mode *mode = get_Div_resmode(n);
2919 ir_node *a = get_Div_left(n);
2920 ir_node *b = get_Div_right(n);
2922 const ir_node *dummy;
2924 if (is_Const(b) && is_const_Phi(a)) {
2925 /* check for Div(Phi, Const) */
2926 value = apply_binop_on_phi(a, get_Const_tarval(b), (eval_func) tarval_div, mode, 0);
2928 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2932 else if (is_Const(a) && is_const_Phi(b)) {
2933 /* check for Div(Const, Phi) */
2934 value = apply_binop_on_phi(b, get_Const_tarval(a), (eval_func) tarval_div, mode, 1);
2936 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2940 else if (is_const_Phi(a) && is_const_Phi(b)) {
2941 /* check for Div(Phi, Phi) */
2942 value = apply_binop_on_2_phis(a, b, (eval_func) tarval_div, mode);
2944 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2951 if (a == b && value_not_zero(a, &dummy)) {
2952 /* BEWARE: we can optimize a/a to 1 only if this cannot cause a exception */
2953 value = new_Const(get_mode_one(mode));
2954 DBG_OPT_CSTEVAL(n, value);
2957 if (mode_is_signed(mode) && is_Const(b)) {
2958 tarval *tv = get_Const_tarval(b);
2960 if (tv == get_mode_minus_one(mode)) {
2962 value = new_rd_Minus(get_irn_dbg_info(n), get_nodes_block(n), a, mode);
2963 DBG_OPT_CSTEVAL(n, value);
2967 /* Try architecture dependent optimization */
2968 value = arch_dep_replace_div_by_const(n);
2975 /* Turn Div into a tuple (mem, jmp, bad, value) */
2976 mem = get_Div_mem(n);
2977 blk = get_nodes_block(n);
2979 /* skip a potential Pin */
2980 mem = skip_Pin(mem);
2981 turn_into_tuple(n, pn_Div_max);
2982 set_Tuple_pred(n, pn_Div_M, mem);
2983 set_Tuple_pred(n, pn_Div_X_regular, new_r_Jmp(blk));
2984 set_Tuple_pred(n, pn_Div_X_except, new_Bad());
2985 set_Tuple_pred(n, pn_Div_res, value);
2988 } /* transform_node_Div */
2991 * Transform a Mod node.
2993 static ir_node *transform_node_Mod(ir_node *n)
2995 ir_mode *mode = get_Mod_resmode(n);
2996 ir_node *a = get_Mod_left(n);
2997 ir_node *b = get_Mod_right(n);
3001 if (is_Const(b) && is_const_Phi(a)) {
3002 /* check for Div(Phi, Const) */
3003 value = apply_binop_on_phi(a, get_Const_tarval(b), (eval_func) tarval_mod, mode, 0);
3005 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
3009 else if (is_Const(a) && is_const_Phi(b)) {
3010 /* check for Div(Const, Phi) */
3011 value = apply_binop_on_phi(b, get_Const_tarval(a), (eval_func) tarval_mod, mode, 1);
3013 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
3017 else if (is_const_Phi(a) && is_const_Phi(b)) {
3018 /* check for Div(Phi, Phi) */
3019 value = apply_binop_on_2_phis(a, b, (eval_func) tarval_mod, mode);
3021 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
3028 if (tv != tarval_bad) {
3029 value = new_Const(tv);
3031 DBG_OPT_CSTEVAL(n, value);
3034 ir_node *a = get_Mod_left(n);
3035 ir_node *b = get_Mod_right(n);
3036 const ir_node *dummy;
3038 if (a == b && value_not_zero(a, &dummy)) {
3039 /* BEWARE: we can optimize a%a to 0 only if this cannot cause a exception */
3040 value = new_Const(get_mode_null(mode));
3041 DBG_OPT_CSTEVAL(n, value);
3044 if (mode_is_signed(mode) && is_Const(b)) {
3045 tarval *tv = get_Const_tarval(b);
3047 if (tv == get_mode_minus_one(mode)) {
3049 value = new_Const(get_mode_null(mode));
3050 DBG_OPT_CSTEVAL(n, value);
3054 /* Try architecture dependent optimization */
3055 value = arch_dep_replace_mod_by_const(n);
3063 /* Turn Mod into a tuple (mem, jmp, bad, value) */
3064 mem = get_Mod_mem(n);
3065 blk = get_nodes_block(n);
3067 /* skip a potential Pin */
3068 mem = skip_Pin(mem);
3069 turn_into_tuple(n, pn_Mod_max);
3070 set_Tuple_pred(n, pn_Mod_M, mem);
3071 set_Tuple_pred(n, pn_Mod_X_regular, new_r_Jmp(blk));
3072 set_Tuple_pred(n, pn_Mod_X_except, new_Bad());
3073 set_Tuple_pred(n, pn_Mod_res, value);
3076 } /* transform_node_Mod */
3079 * Transform a DivMod node.
3081 static ir_node *transform_node_DivMod(ir_node *n)
3083 const ir_node *dummy;
3084 ir_node *a = get_DivMod_left(n);
3085 ir_node *b = get_DivMod_right(n);
3086 ir_mode *mode = get_DivMod_resmode(n);
3091 if (is_Const(b) && is_const_Phi(a)) {
3092 /* check for Div(Phi, Const) */
3093 va = apply_binop_on_phi(a, get_Const_tarval(b), (eval_func) tarval_div, mode, 0);
3094 vb = apply_binop_on_phi(a, get_Const_tarval(b), (eval_func) tarval_mod, mode, 0);
3096 DBG_OPT_ALGSIM0(n, va, FS_OPT_CONST_PHI);
3097 DBG_OPT_ALGSIM0(n, vb, FS_OPT_CONST_PHI);
3101 else if (is_Const(a) && is_const_Phi(b)) {
3102 /* check for Div(Const, Phi) */
3103 va = apply_binop_on_phi(b, get_Const_tarval(a), (eval_func) tarval_div, mode, 1);
3104 vb = apply_binop_on_phi(b, get_Const_tarval(a), (eval_func) tarval_mod, mode, 1);
3106 DBG_OPT_ALGSIM0(n, va, FS_OPT_CONST_PHI);
3107 DBG_OPT_ALGSIM0(n, vb, FS_OPT_CONST_PHI);
3111 else if (is_const_Phi(a) && is_const_Phi(b)) {
3112 /* check for Div(Phi, Phi) */
3113 va = apply_binop_on_2_phis(a, b, (eval_func) tarval_div, mode);
3114 vb = apply_binop_on_2_phis(a, b, (eval_func) tarval_mod, mode);
3116 DBG_OPT_ALGSIM0(n, va, FS_OPT_CONST_PHI);
3117 DBG_OPT_ALGSIM0(n, vb, FS_OPT_CONST_PHI);
3124 if (tb != tarval_bad) {
3125 if (tb == get_mode_one(get_tarval_mode(tb))) {
3127 vb = new_Const(get_mode_null(mode));
3128 DBG_OPT_CSTEVAL(n, vb);
3130 } else if (ta != tarval_bad) {
3131 tarval *resa, *resb;
3132 resa = tarval_div(ta, tb);
3133 if (resa == tarval_bad) return n; /* Causes exception!!! Model by replacing through
3134 Jmp for X result!? */
3135 resb = tarval_mod(ta, tb);
3136 if (resb == tarval_bad) return n; /* Causes exception! */
3137 va = new_Const(resa);
3138 vb = new_Const(resb);
3139 DBG_OPT_CSTEVAL(n, va);
3140 DBG_OPT_CSTEVAL(n, vb);
3142 } else if (mode_is_signed(mode) && tb == get_mode_minus_one(mode)) {
3143 va = new_rd_Minus(get_irn_dbg_info(n), get_nodes_block(n), a, mode);
3144 vb = new_Const(get_mode_null(mode));
3145 DBG_OPT_CSTEVAL(n, va);
3146 DBG_OPT_CSTEVAL(n, vb);
3148 } else { /* Try architecture dependent optimization */
3151 arch_dep_replace_divmod_by_const(&va, &vb, n);
3152 evaluated = va != NULL;
3154 } else if (a == b) {
3155 if (value_not_zero(a, &dummy)) {
3157 va = new_Const(get_mode_one(mode));
3158 vb = new_Const(get_mode_null(mode));
3159 DBG_OPT_CSTEVAL(n, va);
3160 DBG_OPT_CSTEVAL(n, vb);
3163 /* BEWARE: it is NOT possible to optimize a/a to 1, as this may cause a exception */
3166 } else if (ta == get_mode_null(mode) && value_not_zero(b, &dummy)) {
3167 /* 0 / non-Const = 0 */
3172 if (evaluated) { /* replace by tuple */
3176 mem = get_DivMod_mem(n);
3177 /* skip a potential Pin */
3178 mem = skip_Pin(mem);
3180 blk = get_nodes_block(n);
3181 turn_into_tuple(n, pn_DivMod_max);
3182 set_Tuple_pred(n, pn_DivMod_M, mem);
3183 set_Tuple_pred(n, pn_DivMod_X_regular, new_r_Jmp(blk));
3184 set_Tuple_pred(n, pn_DivMod_X_except, new_Bad()); /* no exception */
3185 set_Tuple_pred(n, pn_DivMod_res_div, va);
3186 set_Tuple_pred(n, pn_DivMod_res_mod, vb);
3190 } /* transform_node_DivMod */
3193 * Optimize x / c to x * (1/c)
3195 static ir_node *transform_node_Quot(ir_node *n)
3197 ir_mode *mode = get_Quot_resmode(n);
3200 if (get_mode_arithmetic(mode) == irma_ieee754) {
3201 ir_node *b = get_Quot_right(n);
3202 tarval *tv = value_of(b);
3204 if (tv != tarval_bad) {
3205 int rem = tarval_fp_ops_enabled();
3208 * Floating point constant folding might be disabled here to
3210 * However, as we check for exact result, doing it is safe.
3213 tarval_enable_fp_ops(1);
3214 tv = tarval_quo(get_mode_one(mode), tv);
3215 tarval_enable_fp_ops(rem);
3217 /* Do the transformation if the result is either exact or we are not
3218 using strict rules. */
3219 if (tv != tarval_bad &&
3220 (tarval_ieee754_get_exact() || (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic) == 0)) {
3221 ir_node *blk = get_nodes_block(n);
3222 ir_node *c = new_Const(tv);
3223 ir_node *a = get_Quot_left(n);
3224 ir_node *m = new_rd_Mul(get_irn_dbg_info(n), blk, a, c, mode);
3225 ir_node *mem = get_Quot_mem(n);
3227 /* skip a potential Pin */
3228 mem = skip_Pin(mem);
3229 turn_into_tuple(n, pn_Quot_max);
3230 set_Tuple_pred(n, pn_Quot_M, mem);
3231 set_Tuple_pred(n, pn_Quot_X_regular, new_r_Jmp(blk));
3232 set_Tuple_pred(n, pn_Quot_X_except, new_Bad());
3233 set_Tuple_pred(n, pn_Quot_res, m);
3234 DBG_OPT_ALGSIM1(oldn, a, b, m, FS_OPT_FP_INV_MUL);
3239 } /* transform_node_Quot */
3242 * Optimize Abs(x) into x if x is Confirmed >= 0
3243 * Optimize Abs(x) into -x if x is Confirmed <= 0
3244 * Optimize Abs(-x) int Abs(x)
3246 static ir_node *transform_node_Abs(ir_node *n)
3248 ir_node *c, *oldn = n;
3249 ir_node *a = get_Abs_op(n);
3252 HANDLE_UNOP_PHI(tarval_abs, a, c);
3254 switch (classify_value_sign(a)) {
3255 case value_classified_negative:
3256 mode = get_irn_mode(n);
3259 * We can replace the Abs by -x here.
3260 * We even could add a new Confirm here
3261 * (if not twos complement)
3263 * Note that -x would create a new node, so we could
3264 * not run it in the equivalent_node() context.
3266 n = new_rd_Minus(get_irn_dbg_info(n), get_nodes_block(n), a, mode);
3268 DBG_OPT_CONFIRM(oldn, n);
3270 case value_classified_positive:
3271 /* n is positive, Abs is not needed */
3274 DBG_OPT_CONFIRM(oldn, n);
3280 /* Abs(-x) = Abs(x) */
3281 mode = get_irn_mode(n);
3282 n = new_rd_Abs(get_irn_dbg_info(n), get_nodes_block(n), get_Minus_op(a), mode);
3283 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ABS_MINUS_X);
3287 } /* transform_node_Abs */
3290 * Optimize -a CMP -b into b CMP a.
3291 * This works only for for modes where unary Minus
3293 * Note that two-complement integers can Overflow
3294 * so it will NOT work.
3296 * For == and != can be handled in Proj(Cmp)
3298 static ir_node *transform_node_Cmp(ir_node *n)
3301 ir_node *left = get_Cmp_left(n);
3302 ir_node *right = get_Cmp_right(n);
3304 if (is_Minus(left) && is_Minus(right) &&
3305 !mode_overflow_on_unary_Minus(get_irn_mode(left))) {
3306 ir_node *const new_left = get_Minus_op(right);
3307 ir_node *const new_right = get_Minus_op(left);
3308 n = new_rd_Cmp(get_irn_dbg_info(n), get_nodes_block(n), new_left, new_right);
3309 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CMP_OP_OP);
3312 } /* transform_node_Cmp */
3316 * Transform a Cond node.
3318 * Replace the Cond by a Jmp if it branches on a constant
3321 static ir_node *transform_node_Cond(ir_node *n)
3325 ir_node *a = get_Cond_selector(n);
3326 tarval *ta = value_of(a);
3328 /* we need block info which is not available in floating irgs */
3329 if (get_irg_pinned(current_ir_graph) == op_pin_state_floats)
3332 if ((ta != tarval_bad) &&
3333 (get_irn_mode(a) == mode_b) &&
3334 (get_opt_unreachable_code())) {
3335 /* It's a boolean Cond, branching on a boolean constant.
3336 Replace it by a tuple (Bad, Jmp) or (Jmp, Bad) */
3337 ir_node *blk = get_nodes_block(n);
3338 jmp = new_r_Jmp(blk);
3339 turn_into_tuple(n, pn_Cond_max);
3340 if (ta == tarval_b_true) {
3341 set_Tuple_pred(n, pn_Cond_false, new_Bad());
3342 set_Tuple_pred(n, pn_Cond_true, jmp);
3344 set_Tuple_pred(n, pn_Cond_false, jmp);
3345 set_Tuple_pred(n, pn_Cond_true, new_Bad());
3347 /* We might generate an endless loop, so keep it alive. */
3348 add_End_keepalive(get_irg_end(current_ir_graph), blk);
3351 } /* transform_node_Cond */
3354 * Prototype of a recursive transform function
3355 * for bitwise distributive transformations.
3357 typedef ir_node* (*recursive_transform)(ir_node *n);
3360 * makes use of distributive laws for and, or, eor
3361 * and(a OP c, b OP c) -> and(a, b) OP c
3362 * note, might return a different op than n
3364 static ir_node *transform_bitwise_distributive(ir_node *n,
3365 recursive_transform trans_func)
3368 ir_node *a = get_binop_left(n);
3369 ir_node *b = get_binop_right(n);
3370 ir_op *op = get_irn_op(a);
3371 ir_op *op_root = get_irn_op(n);
3373 if (op != get_irn_op(b))
3376 /* and(conv(a), conv(b)) -> conv(and(a,b)) */
3377 if (op == op_Conv) {
3378 ir_node *a_op = get_Conv_op(a);
3379 ir_node *b_op = get_Conv_op(b);
3380 ir_mode *a_mode = get_irn_mode(a_op);
3381 ir_mode *b_mode = get_irn_mode(b_op);
3382 if (a_mode == b_mode && (mode_is_int(a_mode) || a_mode == mode_b)) {
3383 ir_node *blk = get_nodes_block(n);
3386 set_binop_left(n, a_op);
3387 set_binop_right(n, b_op);
3388 set_irn_mode(n, a_mode);
3390 n = new_r_Conv(blk, n, get_irn_mode(oldn));
3392 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
3398 /* nothing to gain here */
3402 if (op == op_Shrs || op == op_Shr || op == op_Shl
3403 || op == op_And || op == op_Or || op == op_Eor) {
3404 ir_node *a_left = get_binop_left(a);
3405 ir_node *a_right = get_binop_right(a);
3406 ir_node *b_left = get_binop_left(b);
3407 ir_node *b_right = get_binop_right(b);
3409 ir_node *op1 = NULL;
3410 ir_node *op2 = NULL;
3412 if (is_op_commutative(op)) {
3413 if (a_left == b_left) {
3417 } else if (a_left == b_right) {
3421 } else if (a_right == b_left) {
3427 if (a_right == b_right) {
3434 /* (a sop c) & (b sop c) => (a & b) sop c */
3435 ir_node *blk = get_nodes_block(n);
3437 ir_node *new_n = exact_copy(n);
3438 set_binop_left(new_n, op1);
3439 set_binop_right(new_n, op2);
3440 new_n = trans_func(new_n);
3442 if (op_root == op_Eor && op == op_Or) {
3443 dbg_info *dbgi = get_irn_dbg_info(n);
3444 ir_mode *mode = get_irn_mode(c);
3446 c = new_rd_Not(dbgi, blk, c, mode);
3447 n = new_rd_And(dbgi, blk, new_n, c, mode);
3450 set_nodes_block(n, blk);
3451 set_binop_left(n, new_n);
3452 set_binop_right(n, c);
3453 add_identities(current_ir_graph->value_table, n);
3456 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_SHIFT_AND);
3467 static ir_node *transform_node_And(ir_node *n)
3469 ir_node *c, *oldn = n;
3470 ir_node *a = get_And_left(n);
3471 ir_node *b = get_And_right(n);
3474 mode = get_irn_mode(n);
3475 HANDLE_BINOP_PHI((eval_func) tarval_and, a, b, c, mode);
3477 /* we can evaluate 2 Projs of the same Cmp */
3478 if (mode == mode_b && is_Proj(a) && is_Proj(b)) {
3479 ir_node *pred_a = get_Proj_pred(a);
3480 ir_node *pred_b = get_Proj_pred(b);
3481 if (pred_a == pred_b) {
3482 dbg_info *dbgi = get_irn_dbg_info(n);
3483 ir_node *block = get_nodes_block(pred_a);
3484 pn_Cmp pn_a = get_Proj_proj(a);
3485 pn_Cmp pn_b = get_Proj_proj(b);
3486 /* yes, we can simply calculate with pncs */
3487 pn_Cmp new_pnc = pn_a & pn_b;
3489 return new_rd_Proj(dbgi, block, pred_a, mode_b, new_pnc);
3494 ir_node *op = get_Not_op(b);
3496 ir_node *ba = get_And_left(op);
3497 ir_node *bb = get_And_right(op);
3499 /* it's enough to test the following cases due to normalization! */
3500 if (get_Or_left(a) == ba && get_Or_right(a) == bb) {
3501 /* (a|b) & ~(a&b) = a^b */
3502 ir_node *block = get_nodes_block(n);
3504 n = new_rd_Eor(get_irn_dbg_info(n), block, ba, bb, mode);
3505 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_TO_EOR);
3513 ir_node *op = get_Not_op(a);
3515 ir_node *aa = get_And_left(op);
3516 ir_node *ab = get_And_right(op);
3518 /* it's enough to test the following cases due to normalization! */
3519 if (get_Or_left(b) == aa && get_Or_right(b) == ab) {
3520 /* (a|b) & ~(a&b) = a^b */
3521 ir_node *block = get_nodes_block(n);
3523 n = new_rd_Eor(get_irn_dbg_info(n), block, aa, ab, mode);
3524 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_TO_EOR);
3531 ir_node *al = get_Eor_left(a);
3532 ir_node *ar = get_Eor_right(a);
3535 /* (b ^ a) & b -> ~a & b */
3536 dbg_info *dbg = get_irn_dbg_info(n);
3537 ir_node *block = get_nodes_block(n);
3539 ar = new_rd_Not(dbg, block, ar, mode);
3540 n = new_rd_And(dbg, block, ar, b, mode);
3541 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3545 /* (a ^ b) & b -> ~a & b */
3546 dbg_info *dbg = get_irn_dbg_info(n);
3547 ir_node *block = get_nodes_block(n);
3549 al = new_rd_Not(dbg, block, al, mode);
3550 n = new_rd_And(dbg, block, al, b, mode);
3551 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3556 ir_node *bl = get_Eor_left(b);
3557 ir_node *br = get_Eor_right(b);
3560 /* a & (a ^ b) -> a & ~b */
3561 dbg_info *dbg = get_irn_dbg_info(n);
3562 ir_node *block = get_nodes_block(n);
3564 br = new_rd_Not(dbg, block, br, mode);
3565 n = new_rd_And(dbg, block, br, a, mode);
3566 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3570 /* a & (b ^ a) -> a & ~b */
3571 dbg_info *dbg = get_irn_dbg_info(n);
3572 ir_node *block = get_nodes_block(n);
3574 bl = new_rd_Not(dbg, block, bl, mode);
3575 n = new_rd_And(dbg, block, bl, a, mode);
3576 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3580 if (is_Not(a) && is_Not(b)) {
3581 /* ~a & ~b = ~(a|b) */
3582 ir_node *block = get_nodes_block(n);
3583 ir_mode *mode = get_irn_mode(n);
3587 n = new_rd_Or(get_irn_dbg_info(n), block, a, b, mode);
3588 n = new_rd_Not(get_irn_dbg_info(n), block, n, mode);
3589 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_DEMORGAN);
3593 vrp_attr *a_vrp, *b_vrp;
3594 a_vrp = vrp_get_info(a);
3595 b_vrp = vrp_get_info(b);
3597 if (is_Const(a) && b_vrp && (tarval_is_all_one(tarval_or(get_Const_tarval(a),
3598 b_vrp->bits_not_set)))) {
3599 return new_rd_Id(get_irn_dbg_info(n), get_nodes_block(n),
3600 b, get_irn_mode(n));
3604 if (is_Const(b) && a_vrp && (tarval_is_all_one(tarval_or(get_Const_tarval(b),
3605 a_vrp->bits_not_set)))) {
3606 return new_rd_Id(get_irn_dbg_info(n), get_nodes_block(n),
3607 a, get_irn_mode(n));
3611 n = transform_bitwise_distributive(n, transform_node_And);
3614 } /* transform_node_And */
3619 static ir_node *transform_node_Eor(ir_node *n)
3621 ir_node *c, *oldn = n;
3622 ir_node *a = get_Eor_left(n);
3623 ir_node *b = get_Eor_right(n);
3624 ir_mode *mode = get_irn_mode(n);
3626 HANDLE_BINOP_PHI((eval_func) tarval_eor, a, b, c, mode);
3628 /* we can evaluate 2 Projs of the same Cmp */
3629 if (mode == mode_b && is_Proj(a) && is_Proj(b)) {
3630 ir_node *pred_a = get_Proj_pred(a);
3631 ir_node *pred_b = get_Proj_pred(b);
3632 if (pred_a == pred_b) {
3633 dbg_info *dbgi = get_irn_dbg_info(n);
3634 ir_node *block = get_nodes_block(pred_a);
3635 pn_Cmp pn_a = get_Proj_proj(a);
3636 pn_Cmp pn_b = get_Proj_proj(b);
3637 /* yes, we can simply calculate with pncs */
3638 pn_Cmp new_pnc = pn_a ^ pn_b;
3640 return new_rd_Proj(dbgi, block, pred_a, mode_b, new_pnc);
3646 n = new_rd_Const(get_irn_dbg_info(n), current_ir_graph,
3647 get_mode_null(mode));
3648 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_A_A);
3649 } else if (is_Const(b)) {
3650 if (is_Not(a)) { /* ~x ^ const -> x ^ ~const */
3651 ir_node *cnst = new_Const(tarval_not(get_Const_tarval(b)));
3652 ir_node *not_op = get_Not_op(a);
3653 dbg_info *dbg = get_irn_dbg_info(n);
3654 ir_node *block = get_nodes_block(n);
3655 ir_mode *mode = get_irn_mode(n);
3656 n = new_rd_Eor(dbg, block, not_op, cnst, mode);
3658 } else if (is_Const_all_one(b)) { /* x ^ 1...1 -> ~1 */
3659 n = new_r_Not(get_nodes_block(n), a, mode);
3660 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3663 n = transform_bitwise_distributive(n, transform_node_Eor);
3667 } /* transform_node_Eor */
3672 static ir_node *transform_node_Not(ir_node *n)
3674 ir_node *c, *oldn = n;
3675 ir_node *a = get_Not_op(n);
3676 ir_mode *mode = get_irn_mode(n);
3678 HANDLE_UNOP_PHI(tarval_not,a,c);
3680 /* check for a boolean Not */
3681 if (mode == mode_b && is_Proj(a)) {
3682 ir_node *a_pred = get_Proj_pred(a);
3683 if (is_Cmp(a_pred)) {
3684 ir_node *cmp_block = get_nodes_block(a_pred);
3685 /* We negate a Cmp. The Cmp has the negated result anyways! */
3686 n = new_r_Proj(cmp_block, get_Proj_pred(a),
3687 mode_b, get_negated_pnc(get_Proj_proj(a), mode_b));
3688 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_CMP);
3693 ir_node *eor_b = get_Eor_right(a);
3694 if (is_Const(eor_b)) { /* ~(x ^ const) -> x ^ ~const */
3695 ir_node *cnst = new_Const(tarval_not(get_Const_tarval(eor_b)));
3696 ir_node *eor_a = get_Eor_left(a);
3697 dbg_info *dbg = get_irn_dbg_info(n);
3698 ir_node *block = get_nodes_block(n);
3699 ir_mode *mode = get_irn_mode(n);
3700 n = new_rd_Eor(dbg, block, eor_a, cnst, mode);
3704 if (get_mode_arithmetic(mode) == irma_twos_complement) {
3705 if (is_Minus(a)) { /* ~-x -> x + -1 */
3706 dbg_info *dbg = get_irn_dbg_info(n);
3707 ir_graph *irg = current_ir_graph;
3708 ir_node *block = get_nodes_block(n);
3709 ir_node *add_l = get_Minus_op(a);
3710 ir_node *add_r = new_rd_Const(dbg, irg, get_mode_minus_one(mode));
3711 n = new_rd_Add(dbg, block, add_l, add_r, mode);
3712 } else if (is_Add(a)) {
3713 ir_node *add_r = get_Add_right(a);
3714 if (is_Const(add_r) && is_Const_all_one(add_r)) {
3715 /* ~(x + -1) = -x */
3716 ir_node *op = get_Add_left(a);
3717 ir_node *blk = get_nodes_block(n);
3718 n = new_rd_Minus(get_irn_dbg_info(n), blk, op, get_irn_mode(n));
3719 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_MINUS_1);
3724 } /* transform_node_Not */
3727 * Transform a Minus.
3731 * -(a >>u (size-1)) = a >>s (size-1)
3732 * -(a >>s (size-1)) = a >>u (size-1)
3733 * -(a * const) -> a * -const
3735 static ir_node *transform_node_Minus(ir_node *n)
3737 ir_node *c, *oldn = n;
3738 ir_node *a = get_Minus_op(n);
3741 HANDLE_UNOP_PHI(tarval_neg,a,c);
3743 mode = get_irn_mode(a);
3744 if (get_mode_arithmetic(mode) == irma_twos_complement) {
3745 /* the following rules are only to twos-complement */
3748 ir_node *op = get_Not_op(a);
3749 tarval *tv = get_mode_one(mode);
3750 ir_node *blk = get_nodes_block(n);
3751 ir_node *c = new_Const(tv);
3752 n = new_rd_Add(get_irn_dbg_info(n), blk, op, c, mode);
3753 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_MINUS_NOT);
3757 ir_node *c = get_Shr_right(a);
3760 tarval *tv = get_Const_tarval(c);
3762 if (tarval_is_long(tv) && get_tarval_long(tv) == (int) get_mode_size_bits(mode) - 1) {
3763 /* -(a >>u (size-1)) = a >>s (size-1) */
3764 ir_node *v = get_Shr_left(a);
3766 n = new_rd_Shrs(get_irn_dbg_info(n), get_nodes_block(n), v, c, mode);
3767 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_PREDICATE);
3773 ir_node *c = get_Shrs_right(a);
3776 tarval *tv = get_Const_tarval(c);
3778 if (tarval_is_long(tv) && get_tarval_long(tv) == (int) get_mode_size_bits(mode) - 1) {
3779 /* -(a >>s (size-1)) = a >>u (size-1) */
3780 ir_node *v = get_Shrs_left(a);
3782 n = new_rd_Shr(get_irn_dbg_info(n), get_nodes_block(n), v, c, mode);
3783 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_PREDICATE);
3790 /* - (a-b) = b - a */
3791 ir_node *la = get_Sub_left(a);
3792 ir_node *ra = get_Sub_right(a);
3793 ir_node *blk = get_nodes_block(n);
3795 n = new_rd_Sub(get_irn_dbg_info(n), blk, ra, la, mode);
3796 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_MINUS_SUB);
3800 if (is_Mul(a)) { /* -(a * const) -> a * -const */
3801 ir_node *mul_l = get_Mul_left(a);
3802 ir_node *mul_r = get_Mul_right(a);
3803 tarval *tv = value_of(mul_r);
3804 if (tv != tarval_bad) {
3805 tv = tarval_neg(tv);
3806 if (tv != tarval_bad) {
3807 ir_node *cnst = new_Const(tv);
3808 dbg_info *dbg = get_irn_dbg_info(a);
3809 ir_node *block = get_nodes_block(a);
3810 n = new_rd_Mul(dbg, block, mul_l, cnst, mode);
3811 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_MINUS_MUL_C);
3818 } /* transform_node_Minus */
3821 * Transform a Cast_type(Const) into a new Const_type
3823 static ir_node *transform_node_Cast(ir_node *n)
3826 ir_node *pred = get_Cast_op(n);
3827 ir_type *tp = get_irn_type(n);
3829 if (is_Const(pred) && get_Const_type(pred) != tp) {
3830 n = new_rd_Const_type(NULL, current_ir_graph, get_Const_tarval(pred), tp);
3831 DBG_OPT_CSTEVAL(oldn, n);
3832 } else if (is_SymConst(pred) && get_SymConst_value_type(pred) != tp) {
3833 n = new_rd_SymConst_type(NULL, current_ir_graph, get_irn_mode(pred),
3834 get_SymConst_symbol(pred), get_SymConst_kind(pred), tp);
3835 DBG_OPT_CSTEVAL(oldn, n);
3839 } /* transform_node_Cast */
3842 * Transform a Proj(Load) with a non-null address.
3844 static ir_node *transform_node_Proj_Load(ir_node *proj)
3846 if (get_opt_ldst_only_null_ptr_exceptions()) {
3847 if (get_irn_mode(proj) == mode_X) {
3848 ir_node *load = get_Proj_pred(proj);
3850 /* get the Load address */
3851 const ir_node *addr = get_Load_ptr(load);
3852 const ir_node *confirm;
3854 if (value_not_null(addr, &confirm)) {
3855 if (confirm == NULL) {
3856 /* this node may float if it did not depend on a Confirm */
3857 set_irn_pinned(load, op_pin_state_floats);
3859 if (get_Proj_proj(proj) == pn_Load_X_except) {
3860 DBG_OPT_EXC_REM(proj);
3861 return get_irg_bad(current_ir_graph);
3863 ir_node *blk = get_nodes_block(load);
3864 return new_r_Jmp(blk);
3870 } /* transform_node_Proj_Load */
3873 * Transform a Proj(Store) with a non-null address.
3875 static ir_node *transform_node_Proj_Store(ir_node *proj)
3877 if (get_opt_ldst_only_null_ptr_exceptions()) {
3878 if (get_irn_mode(proj) == mode_X) {
3879 ir_node *store = get_Proj_pred(proj);
3881 /* get the load/store address */
3882 const ir_node *addr = get_Store_ptr(store);
3883 const ir_node *confirm;
3885 if (value_not_null(addr, &confirm)) {
3886 if (confirm == NULL) {
3887 /* this node may float if it did not depend on a Confirm */
3888 set_irn_pinned(store, op_pin_state_floats);
3890 if (get_Proj_proj(proj) == pn_Store_X_except) {
3891 DBG_OPT_EXC_REM(proj);
3892 return get_irg_bad(current_ir_graph);
3894 ir_node *blk = get_nodes_block(store);
3895 return new_r_Jmp(blk);
3901 } /* transform_node_Proj_Store */
3904 * Transform a Proj(Div) with a non-zero value.
3905 * Removes the exceptions and routes the memory to the NoMem node.
3907 static ir_node *transform_node_Proj_Div(ir_node *proj)
3909 ir_node *div = get_Proj_pred(proj);
3910 ir_node *b = get_Div_right(div);
3911 ir_node *res, *new_mem;
3912 const ir_node *confirm;
3915 if (value_not_zero(b, &confirm)) {
3916 /* div(x, y) && y != 0 */
3917 if (confirm == NULL) {
3918 /* we are sure we have a Const != 0 */
3919 new_mem = get_Div_mem(div);
3920 new_mem = skip_Pin(new_mem);
3921 set_Div_mem(div, new_mem);
3922 set_irn_pinned(div, op_pin_state_floats);
3925 proj_nr = get_Proj_proj(proj);
3927 case pn_Div_X_regular:
3928 return new_r_Jmp(get_nodes_block(div));
3930 case pn_Div_X_except:
3931 /* we found an exception handler, remove it */
3932 DBG_OPT_EXC_REM(proj);
3936 res = get_Div_mem(div);
3937 new_mem = get_irg_no_mem(current_ir_graph);
3940 /* This node can only float up to the Confirm block */
3941 new_mem = new_r_Pin(get_nodes_block(confirm), new_mem);
3943 set_irn_pinned(div, op_pin_state_floats);
3944 /* this is a Div without exception, we can remove the memory edge */
3945 set_Div_mem(div, new_mem);
3950 } /* transform_node_Proj_Div */
3953 * Transform a Proj(Mod) with a non-zero value.
3954 * Removes the exceptions and routes the memory to the NoMem node.
3956 static ir_node *transform_node_Proj_Mod(ir_node *proj)
3958 ir_node *mod = get_Proj_pred(proj);
3959 ir_node *b = get_Mod_right(mod);
3960 ir_node *res, *new_mem;
3961 const ir_node *confirm;
3964 if (value_not_zero(b, &confirm)) {
3965 /* mod(x, y) && y != 0 */
3966 proj_nr = get_Proj_proj(proj);
3968 if (confirm == NULL) {
3969 /* we are sure we have a Const != 0 */
3970 new_mem = get_Mod_mem(mod);
3971 new_mem = skip_Pin(new_mem);
3972 set_Mod_mem(mod, new_mem);
3973 set_irn_pinned(mod, op_pin_state_floats);
3978 case pn_Mod_X_regular:
3979 return new_r_Jmp(get_irn_n(mod, -1));
3981 case pn_Mod_X_except:
3982 /* we found an exception handler, remove it */
3983 DBG_OPT_EXC_REM(proj);
3987 res = get_Mod_mem(mod);
3988 new_mem = get_irg_no_mem(current_ir_graph);
3991 /* This node can only float up to the Confirm block */
3992 new_mem = new_r_Pin(get_nodes_block(confirm), new_mem);
3994 /* this is a Mod without exception, we can remove the memory edge */
3995 set_Mod_mem(mod, new_mem);
3998 if (get_Mod_left(mod) == b) {
3999 /* a % a = 0 if a != 0 */
4000 ir_mode *mode = get_irn_mode(proj);
4001 ir_node *res = new_Const(get_mode_null(mode));
4003 DBG_OPT_CSTEVAL(mod, res);
4009 } /* transform_node_Proj_Mod */
4012 * Transform a Proj(DivMod) with a non-zero value.
4013 * Removes the exceptions and routes the memory to the NoMem node.
4015 static ir_node *transform_node_Proj_DivMod(ir_node *proj)
4017 ir_node *divmod = get_Proj_pred(proj);
4018 ir_node *b = get_DivMod_right(divmod);
4019 ir_node *res, *new_mem;
4020 const ir_node *confirm;
4023 if (value_not_zero(b, &confirm)) {
4024 /* DivMod(x, y) && y != 0 */
4025 proj_nr = get_Proj_proj(proj);
4027 if (confirm == NULL) {
4028 /* we are sure we have a Const != 0 */
4029 new_mem = get_DivMod_mem(divmod);
4030 new_mem = skip_Pin(new_mem);
4031 set_DivMod_mem(divmod, new_mem);
4032 set_irn_pinned(divmod, op_pin_state_floats);
4037 case pn_DivMod_X_regular:
4038 return new_r_Jmp(get_nodes_block(divmod));
4040 case pn_DivMod_X_except:
4041 /* we found an exception handler, remove it */
4042 DBG_OPT_EXC_REM(proj);
4046 res = get_DivMod_mem(divmod);
4047 new_mem = get_irg_no_mem(current_ir_graph);
4050 /* This node can only float up to the Confirm block */
4051 new_mem = new_r_Pin(get_nodes_block(confirm), new_mem);
4053 /* this is a DivMod without exception, we can remove the memory edge */
4054 set_DivMod_mem(divmod, new_mem);
4057 case pn_DivMod_res_mod:
4058 if (get_DivMod_left(divmod) == b) {
4059 /* a % a = 0 if a != 0 */
4060 ir_mode *mode = get_irn_mode(proj);
4061 ir_node *res = new_Const(get_mode_null(mode));
4063 DBG_OPT_CSTEVAL(divmod, res);
4069 } /* transform_node_Proj_DivMod */
4072 * Optimizes jump tables (CondIs or CondIu) by removing all impossible cases.
4074 static ir_node *transform_node_Proj_Cond(ir_node *proj)
4076 if (get_opt_unreachable_code()) {
4077 ir_node *n = get_Proj_pred(proj);
4078 ir_node *b = get_Cond_selector(n);
4080 if (mode_is_int(get_irn_mode(b))) {
4081 tarval *tb = value_of(b);
4083 if (tb != tarval_bad) {
4084 /* we have a constant switch */
4085 long num = get_Proj_proj(proj);
4087 if (num != get_Cond_default_proj(n)) { /* we cannot optimize default Proj's yet */
4088 if (get_tarval_long(tb) == num) {
4089 /* Do NOT create a jump here, or we will have 2 control flow ops
4090 * in a block. This case is optimized away in optimize_cf(). */
4093 /* this case will NEVER be taken, kill it */
4094 return get_irg_bad(current_ir_graph);
4098 long num = get_Proj_proj(proj);
4099 vrp_attr *b_vrp = vrp_get_info(b);
4100 if (num != get_Cond_default_proj(n) && b_vrp) {
4101 /* Try handling with vrp data. We only remove dead parts. */
4102 tarval *tp = new_tarval_from_long(num, get_irn_mode(b));
4104 if (b_vrp->range_type == VRP_RANGE) {
4105 pn_Cmp cmp_result = tarval_cmp(b_vrp->range_bottom, tp);
4106 pn_Cmp cmp_result2 = tarval_cmp(b_vrp->range_top, tp);
4108 if ((cmp_result & pn_Cmp_Lt) == cmp_result && (cmp_result2
4109 & pn_Cmp_Gt) == cmp_result2) {
4110 return get_irg_bad(current_ir_graph);
4112 } else if (b_vrp->range_type == VRP_ANTIRANGE) {
4113 pn_Cmp cmp_result = tarval_cmp(b_vrp->range_bottom, tp);
4114 pn_Cmp cmp_result2 = tarval_cmp(b_vrp->range_top, tp);
4116 if ((cmp_result & pn_Cmp_Ge) == cmp_result && (cmp_result2
4117 & pn_Cmp_Le) == cmp_result2) {
4118 return get_irg_bad(current_ir_graph);
4123 tarval_and( b_vrp->bits_set, tp),
4127 return get_irg_bad(current_ir_graph);
4133 b_vrp->bits_not_set),
4134 b_vrp->bits_not_set)
4137 return get_irg_bad(current_ir_graph);
4146 } /* transform_node_Proj_Cond */
4149 * Create a 0 constant of given mode.
4151 static ir_node *create_zero_const(ir_mode *mode)
4153 tarval *tv = get_mode_null(mode);
4154 ir_node *cnst = new_Const(tv);
4159 /* the order of the values is important! */
4160 typedef enum const_class {
4166 static const_class classify_const(const ir_node* n)
4168 if (is_Const(n)) return const_const;
4169 if (is_irn_constlike(n)) return const_like;
4174 * Determines whether r is more constlike or has a larger index (in that order)
4177 static int operands_are_normalized(const ir_node *l, const ir_node *r)
4179 const const_class l_order = classify_const(l);
4180 const const_class r_order = classify_const(r);
4182 l_order > r_order ||
4183 (l_order == r_order && get_irn_idx(l) <= get_irn_idx(r));
4187 * Normalizes and optimizes Cmp nodes.
4189 static ir_node *transform_node_Proj_Cmp(ir_node *proj)
4191 ir_node *n = get_Proj_pred(proj);
4192 ir_node *left = get_Cmp_left(n);
4193 ir_node *right = get_Cmp_right(n);
4196 ir_mode *mode = NULL;
4197 long proj_nr = get_Proj_proj(proj);
4199 /* we can evaluate some cases directly */
4202 return new_Const(get_tarval_b_false());
4204 return new_Const(get_tarval_b_true());
4206 if (!mode_is_float(get_irn_mode(left)))
4207 return new_Const(get_tarval_b_true());
4213 /* remove Casts of both sides */
4214 left = skip_Cast(left);
4215 right = skip_Cast(right);
4217 /* Remove unnecessary conversions */
4218 /* TODO handle constants */
4219 if (is_Conv(left) && is_Conv(right)) {
4220 ir_mode *mode = get_irn_mode(left);
4221 ir_node *op_left = get_Conv_op(left);
4222 ir_node *op_right = get_Conv_op(right);
4223 ir_mode *mode_left = get_irn_mode(op_left);
4224 ir_mode *mode_right = get_irn_mode(op_right);
4226 if (smaller_mode(mode_left, mode) && smaller_mode(mode_right, mode)
4227 && mode_left != mode_b && mode_right != mode_b) {
4228 ir_node *block = get_nodes_block(n);
4230 if (mode_left == mode_right) {
4234 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV_CONV);
4235 } else if (smaller_mode(mode_left, mode_right)) {
4236 left = new_r_Conv(block, op_left, mode_right);
4239 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV);
4240 } else if (smaller_mode(mode_right, mode_left)) {
4242 right = new_r_Conv(block, op_right, mode_left);
4244 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV);
4249 /* remove operation on both sides if possible */
4250 if (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) {
4252 * The following operations are NOT safe for floating point operations, for instance
4253 * 1.0 + inf == 2.0 + inf, =/=> x == y
4255 if (mode_is_int(get_irn_mode(left))) {
4256 unsigned lop = get_irn_opcode(left);
4258 if (lop == get_irn_opcode(right)) {
4259 ir_node *ll, *lr, *rl, *rr;
4261 /* same operation on both sides, try to remove */
4265 /* ~a CMP ~b => a CMP b, -a CMP -b ==> a CMP b */
4266 left = get_unop_op(left);
4267 right = get_unop_op(right);
4269 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4272 ll = get_Add_left(left);
4273 lr = get_Add_right(left);
4274 rl = get_Add_left(right);
4275 rr = get_Add_right(right);
4278 /* X + a CMP X + b ==> a CMP b */
4282 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4283 } else if (ll == rr) {
4284 /* X + a CMP b + X ==> a CMP b */
4288 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4289 } else if (lr == rl) {
4290 /* a + X CMP X + b ==> a CMP b */
4294 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4295 } else if (lr == rr) {
4296 /* a + X CMP b + X ==> a CMP b */
4300 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4304 ll = get_Sub_left(left);
4305 lr = get_Sub_right(left);
4306 rl = get_Sub_left(right);
4307 rr = get_Sub_right(right);
4310 /* X - a CMP X - b ==> a CMP b */
4314 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4315 } else if (lr == rr) {
4316 /* a - X CMP b - X ==> a CMP b */
4320 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4324 if (get_Rotl_right(left) == get_Rotl_right(right)) {
4325 /* a ROTL X CMP b ROTL X ==> a CMP b */
4326 left = get_Rotl_left(left);
4327 right = get_Rotl_left(right);
4329 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4337 /* X+A == A, A+X == A, A-X == A -> X == 0 */
4338 if (is_Add(left) || is_Sub(left)) {
4339 ir_node *ll = get_binop_left(left);
4340 ir_node *lr = get_binop_right(left);
4342 if (lr == right && is_Add(left)) {
4349 right = create_zero_const(get_irn_mode(left));
4351 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4354 if (is_Add(right) || is_Sub(right)) {
4355 ir_node *rl = get_binop_left(right);
4356 ir_node *rr = get_binop_right(right);
4358 if (rr == left && is_Add(right)) {
4365 right = create_zero_const(get_irn_mode(left));
4367 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4370 if (is_And(left) && is_Const(right)) {
4371 ir_node *ll = get_binop_left(left);
4372 ir_node *lr = get_binop_right(left);
4373 if (is_Shr(ll) && is_Const(lr)) {
4374 /* Cmp((x >>u c1) & c2, c3) = Cmp(x & (c2 << c1), c3 << c1) */
4375 ir_node *block = get_nodes_block(n);
4376 ir_mode *mode = get_irn_mode(left);
4378 ir_node *llr = get_Shr_right(ll);
4379 if (is_Const(llr)) {
4380 dbg_info *dbg = get_irn_dbg_info(left);
4382 tarval *c1 = get_Const_tarval(llr);
4383 tarval *c2 = get_Const_tarval(lr);
4384 tarval *c3 = get_Const_tarval(right);
4385 tarval *mask = tarval_shl(c2, c1);
4386 tarval *value = tarval_shl(c3, c1);
4388 left = new_rd_And(dbg, block, get_Shr_left(ll), new_Const(mask), mode);
4389 right = new_Const(value);
4394 } /* mode_is_int(...) */
4395 } /* proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg */
4397 /* replace mode_b compares with ands/ors */
4398 if (get_irn_mode(left) == mode_b) {
4399 ir_node *block = get_nodes_block(n);
4403 case pn_Cmp_Le: bres = new_r_Or( block, new_r_Not(block, left, mode_b), right, mode_b); break;
4404 case pn_Cmp_Lt: bres = new_r_And(block, new_r_Not(block, left, mode_b), right, mode_b); break;
4405 case pn_Cmp_Ge: bres = new_r_Or( block, left, new_r_Not(block, right, mode_b), mode_b); break;
4406 case pn_Cmp_Gt: bres = new_r_And(block, left, new_r_Not(block, right, mode_b), mode_b); break;
4407 case pn_Cmp_Lg: bres = new_r_Eor(block, left, right, mode_b); break;
4408 case pn_Cmp_Eq: bres = new_r_Not(block, new_r_Eor(block, left, right, mode_b), mode_b); break;
4409 default: bres = NULL;
4412 DBG_OPT_ALGSIM0(n, bres, FS_OPT_CMP_TO_BOOL);
4418 * First step: normalize the compare op
4419 * by placing the constant on the right side
4420 * or moving the lower address node to the left.
4422 if (!operands_are_normalized(left, right)) {
4428 proj_nr = get_inversed_pnc(proj_nr);
4433 * Second step: Try to reduce the magnitude
4434 * of a constant. This may help to generate better code
4435 * later and may help to normalize more compares.
4436 * Of course this is only possible for integer values.
4438 tv = value_of(right);
4439 if (tv != tarval_bad) {
4440 mode = get_irn_mode(right);
4442 /* TODO extend to arbitrary constants */
4443 if (is_Conv(left) && tarval_is_null(tv)) {
4444 ir_node *op = get_Conv_op(left);
4445 ir_mode *op_mode = get_irn_mode(op);
4448 * UpConv(x) REL 0 ==> x REL 0
4449 * Don't do this for float values as it's unclear whether it is a
4450 * win. (on the other side it makes detection/creation of fabs hard)
4452 if (get_mode_size_bits(mode) > get_mode_size_bits(op_mode) &&
4453 ((proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) ||
4454 mode_is_signed(mode) || !mode_is_signed(op_mode)) &&
4455 !mode_is_float(mode)) {
4456 tv = get_mode_null(op_mode);
4460 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV);
4464 if (tv != tarval_bad) {
4465 /* the following optimization is possible on modes without Overflow
4466 * on Unary Minus or on == and !=:
4467 * -a CMP c ==> a swap(CMP) -c
4469 * Beware: for two-complement Overflow may occur, so only == and != can
4470 * be optimized, see this:
4471 * -MININT < 0 =/=> MININT > 0 !!!
4473 if (is_Minus(left) &&
4474 (!mode_overflow_on_unary_Minus(mode) ||
4475 (mode_is_int(mode) && (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg)))) {
4476 tv = tarval_neg(tv);
4478 if (tv != tarval_bad) {
4479 left = get_Minus_op(left);
4480 proj_nr = get_inversed_pnc(proj_nr);
4482 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4484 } else if (is_Not(left) && (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg)) {
4485 /* Not(a) ==/!= c ==> a ==/!= Not(c) */
4486 tv = tarval_not(tv);
4488 if (tv != tarval_bad) {
4489 left = get_Not_op(left);
4491 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4495 /* for integer modes, we have more */
4496 if (mode_is_int(mode)) {
4497 /* Ne includes Unordered which is not possible on integers.
4498 * However, frontends often use this wrong, so fix it here */
4499 if (proj_nr & pn_Cmp_Uo) {
4500 proj_nr &= ~pn_Cmp_Uo;
4501 set_Proj_proj(proj, proj_nr);
4504 /* c > 0 : a < c ==> a <= (c-1) a >= c ==> a > (c-1) */
4505 if ((proj_nr == pn_Cmp_Lt || proj_nr == pn_Cmp_Ge) &&
4506 tarval_cmp(tv, get_mode_null(mode)) == pn_Cmp_Gt) {
4507 tv = tarval_sub(tv, get_mode_one(mode), NULL);
4509 if (tv != tarval_bad) {
4510 proj_nr ^= pn_Cmp_Eq;
4512 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CNST_MAGN);
4515 /* c < 0 : a > c ==> a >= (c+1) a <= c ==> a < (c+1) */
4516 else if ((proj_nr == pn_Cmp_Gt || proj_nr == pn_Cmp_Le) &&
4517 tarval_cmp(tv, get_mode_null(mode)) == pn_Cmp_Lt) {
4518 tv = tarval_add(tv, get_mode_one(mode));
4520 if (tv != tarval_bad) {
4521 proj_nr ^= pn_Cmp_Eq;
4523 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CNST_MAGN);
4527 /* the following reassociations work only for == and != */
4528 if (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) {
4530 #if 0 /* Might be not that good in general */
4531 /* a-b == 0 ==> a == b, a-b != 0 ==> a != b */
4532 if (tarval_is_null(tv) && is_Sub(left)) {
4533 right = get_Sub_right(left);
4534 left = get_Sub_left(left);
4536 tv = value_of(right);
4538 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4542 if (tv != tarval_bad) {
4543 /* a-c1 == c2 ==> a == c2+c1, a-c1 != c2 ==> a != c2+c1 */
4545 ir_node *c1 = get_Sub_right(left);
4546 tarval *tv2 = value_of(c1);
4548 if (tv2 != tarval_bad) {
4549 tv2 = tarval_add(tv, value_of(c1));
4551 if (tv2 != tarval_bad) {
4552 left = get_Sub_left(left);
4555 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4559 /* a+c1 == c2 ==> a == c2-c1, a+c1 != c2 ==> a != c2-c1 */
4560 else if (is_Add(left)) {
4561 ir_node *a_l = get_Add_left(left);
4562 ir_node *a_r = get_Add_right(left);
4566 if (is_Const(a_l)) {
4568 tv2 = value_of(a_l);
4571 tv2 = value_of(a_r);
4574 if (tv2 != tarval_bad) {
4575 tv2 = tarval_sub(tv, tv2, NULL);
4577 if (tv2 != tarval_bad) {
4581 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4585 /* -a == c ==> a == -c, -a != c ==> a != -c */
4586 else if (is_Minus(left)) {
4587 tarval *tv2 = tarval_sub(get_mode_null(mode), tv, NULL);
4589 if (tv2 != tarval_bad) {
4590 left = get_Minus_op(left);
4593 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4598 /* the following reassociations work only for <= */
4599 else if (proj_nr == pn_Cmp_Le || proj_nr == pn_Cmp_Lt) {
4600 if (tv != tarval_bad) {
4601 /* c >= 0 : Abs(a) <= c ==> (unsigned)(a + c) <= 2*c */
4602 if (is_Abs(left)) { // TODO something is missing here
4608 if (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) {
4609 switch (get_irn_opcode(left)) {
4613 c1 = get_And_right(left);
4616 * And(x, C1) == C2 ==> FALSE if C2 & C1 != C2
4617 * And(x, C1) != C2 ==> TRUE if C2 & C1 != C2
4619 tarval *mask = tarval_and(get_Const_tarval(c1), tv);
4621 /* TODO: move to constant evaluation */
4622 tv = proj_nr == pn_Cmp_Eq ? get_tarval_b_false() : get_tarval_b_true();
4624 DBG_OPT_CSTEVAL(proj, c1);
4628 if (tarval_is_single_bit(tv)) {
4630 * optimization for AND:
4632 * And(x, C) == C ==> And(x, C) != 0
4633 * And(x, C) != C ==> And(X, C) == 0
4635 * if C is a single Bit constant.
4638 /* check for Constant's match. We have check hare the tarvals,
4639 because our const might be changed */
4640 if (get_Const_tarval(c1) == tv) {
4641 /* fine: do the transformation */
4642 tv = get_mode_null(get_tarval_mode(tv));
4643 proj_nr ^= pn_Cmp_Leg;
4645 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CNST_MAGN);
4651 c1 = get_Or_right(left);
4652 if (is_Const(c1) && tarval_is_null(tv)) {
4654 * Or(x, C) == 0 && C != 0 ==> FALSE
4655 * Or(x, C) != 0 && C != 0 ==> TRUE
4657 if (! tarval_is_null(get_Const_tarval(c1))) {
4658 /* TODO: move to constant evaluation */
4659 tv = proj_nr == pn_Cmp_Eq ? get_tarval_b_false() : get_tarval_b_true();
4661 DBG_OPT_CSTEVAL(proj, c1);
4668 * optimize x << c1 == c into x & (-1 >>u c1) == c >> c1 if c & (-1 << c1) == c
4670 * optimize x << c1 != c into x & (-1 >>u c1) != c >> c1 if c & (-1 << c1) == c
4673 c1 = get_Shl_right(left);
4675 tarval *tv1 = get_Const_tarval(c1);
4676 ir_mode *mode = get_irn_mode(left);
4677 tarval *minus1 = get_mode_all_one(mode);
4678 tarval *amask = tarval_shr(minus1, tv1);
4679 tarval *cmask = tarval_shl(minus1, tv1);
4682 if (tarval_and(tv, cmask) != tv) {
4683 /* condition not met */
4684 tv = proj_nr == pn_Cmp_Eq ? get_tarval_b_false() : get_tarval_b_true();
4686 DBG_OPT_CSTEVAL(proj, c1);
4689 sl = get_Shl_left(left);
4690 blk = get_nodes_block(n);
4691 left = new_rd_And(get_irn_dbg_info(left), blk, sl, new_Const(amask), mode);
4692 tv = tarval_shr(tv, tv1);
4694 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_SHF_TO_AND);
4699 * optimize x >>u c1 == c into x & (-1 << c1) == c << c1 if c & (-1 >>u c1) == c
4701 * optimize x >>u c1 != c into x & (-1 << c1) != c << c1 if c & (-1 >>u c1) == c
4704 c1 = get_Shr_right(left);
4706 tarval *tv1 = get_Const_tarval(c1);
4707 ir_mode *mode = get_irn_mode(left);
4708 tarval *minus1 = get_mode_all_one(mode);
4709 tarval *amask = tarval_shl(minus1, tv1);
4710 tarval *cmask = tarval_shr(minus1, tv1);
4713 if (tarval_and(tv, cmask) != tv) {
4714 /* condition not met */
4715 tv = proj_nr == pn_Cmp_Eq ? get_tarval_b_false() : get_tarval_b_true();
4717 DBG_OPT_CSTEVAL(proj, c1);
4720 sl = get_Shr_left(left);
4721 blk = get_nodes_block(n);
4722 left = new_rd_And(get_irn_dbg_info(left), blk, sl, new_Const(amask), mode);
4723 tv = tarval_shl(tv, tv1);
4725 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_SHF_TO_AND);
4730 * optimize x >>s c1 == c into x & (-1 << c1) == c << c1 if (c >>s (BITS - c1)) \in {0,-1}
4732 * optimize x >>s c1 != c into x & (-1 << c1) != c << c1 if (c >>s (BITS - c1)) \in {0,-1}
4735 c1 = get_Shrs_right(left);
4737 tarval *tv1 = get_Const_tarval(c1);
4738 ir_mode *mode = get_irn_mode(left);
4739 tarval *minus1 = get_mode_all_one(mode);
4740 tarval *amask = tarval_shl(minus1, tv1);
4741 tarval *cond = new_tarval_from_long(get_mode_size_bits(mode), get_tarval_mode(tv1));
4744 cond = tarval_sub(cond, tv1, NULL);
4745 cond = tarval_shrs(tv, cond);
4747 if (!tarval_is_all_one(cond) && !tarval_is_null(cond)) {
4748 /* condition not met */
4749 tv = proj_nr == pn_Cmp_Eq ? get_tarval_b_false() : get_tarval_b_true();
4751 DBG_OPT_CSTEVAL(proj, c1);
4754 sl = get_Shrs_left(left);
4755 blk = get_nodes_block(n);
4756 left = new_rd_And(get_irn_dbg_info(left), blk, sl, new_Const(amask), mode);
4757 tv = tarval_shl(tv, tv1);
4759 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_SHF_TO_AND);
4764 } /* tarval != bad */
4767 if (changed & 2) /* need a new Const */
4768 right = new_Const(tv);
4770 if ((proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) && is_Const(right) && is_Const_null(right) && is_Proj(left)) {
4771 ir_node *op = get_Proj_pred(left);
4773 if ((is_Mod(op) && get_Proj_proj(left) == pn_Mod_res) ||
4774 (is_DivMod(op) && get_Proj_proj(left) == pn_DivMod_res_mod)) {
4775 ir_node *c = get_binop_right(op);
4778 tarval *tv = get_Const_tarval(c);
4780 if (tarval_is_single_bit(tv)) {
4781 /* special case: (x % 2^n) CMP 0 ==> x & (2^n-1) CMP 0 */
4782 ir_node *v = get_binop_left(op);
4783 ir_node *blk = get_irn_n(op, -1);
4784 ir_mode *mode = get_irn_mode(v);
4786 tv = tarval_sub(tv, get_mode_one(mode), NULL);
4787 left = new_rd_And(get_irn_dbg_info(op), blk, v, new_Const(tv), mode);
4789 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_MOD_TO_AND);
4796 ir_node *block = get_nodes_block(n);
4798 /* create a new compare */
4799 n = new_rd_Cmp(get_irn_dbg_info(n), block, left, right);
4800 proj = new_rd_Proj(get_irn_dbg_info(proj), block, n, get_irn_mode(proj), proj_nr);
4804 } /* transform_node_Proj_Cmp */
4807 * Optimize CopyB(mem, x, x) into a Nop.
4809 static ir_node *transform_node_Proj_CopyB(ir_node *proj)
4811 ir_node *copyb = get_Proj_pred(proj);
4812 ir_node *a = get_CopyB_dst(copyb);
4813 ir_node *b = get_CopyB_src(copyb);
4816 switch (get_Proj_proj(proj)) {
4817 case pn_CopyB_X_regular:
4818 /* Turn CopyB into a tuple (mem, jmp, bad, bad) */
4819 DBG_OPT_EXC_REM(proj);
4820 proj = new_r_Jmp(get_nodes_block(copyb));
4822 case pn_CopyB_X_except:
4823 DBG_OPT_EXC_REM(proj);
4824 proj = get_irg_bad(get_irn_irg(proj));
4831 } /* transform_node_Proj_CopyB */
4834 * Optimize Bounds(idx, idx, upper) into idx.
4836 static ir_node *transform_node_Proj_Bound(ir_node *proj)
4838 ir_node *oldn = proj;
4839 ir_node *bound = get_Proj_pred(proj);
4840 ir_node *idx = get_Bound_index(bound);
4841 ir_node *pred = skip_Proj(idx);
4844 if (idx == get_Bound_lower(bound))
4846 else if (is_Bound(pred)) {
4848 * idx was Bounds checked in the same MacroBlock previously,
4849 * it is still valid if lower <= pred_lower && pred_upper <= upper.
4851 ir_node *lower = get_Bound_lower(bound);
4852 ir_node *upper = get_Bound_upper(bound);
4853 if (get_Bound_lower(pred) == lower &&
4854 get_Bound_upper(pred) == upper &&
4855 get_irn_MacroBlock(bound) == get_irn_MacroBlock(pred)) {
4857 * One could expect that we simply return the previous
4858 * Bound here. However, this would be wrong, as we could
4859 * add an exception Proj to a new location then.
4860 * So, we must turn in into a tuple.
4866 /* Turn Bound into a tuple (mem, jmp, bad, idx) */
4867 switch (get_Proj_proj(proj)) {
4869 DBG_OPT_EXC_REM(proj);
4870 proj = get_Bound_mem(bound);
4872 case pn_Bound_X_except:
4873 DBG_OPT_EXC_REM(proj);
4874 proj = get_irg_bad(get_irn_irg(proj));
4878 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NOP);
4880 case pn_Bound_X_regular:
4881 DBG_OPT_EXC_REM(proj);
4882 proj = new_r_Jmp(get_nodes_block(bound));
4889 } /* transform_node_Proj_Bound */
4892 * Does all optimizations on nodes that must be done on it's Proj's
4893 * because of creating new nodes.
4895 static ir_node *transform_node_Proj(ir_node *proj)
4897 ir_node *n = get_Proj_pred(proj);
4899 if (n->op->ops.transform_node_Proj)
4900 return n->op->ops.transform_node_Proj(proj);
4902 } /* transform_node_Proj */
4905 * Move Confirms down through Phi nodes.
4907 static ir_node *transform_node_Phi(ir_node *phi)
4910 ir_mode *mode = get_irn_mode(phi);
4912 if (mode_is_reference(mode)) {
4913 n = get_irn_arity(phi);
4915 /* Beware of Phi0 */
4917 ir_node *pred = get_irn_n(phi, 0);
4918 ir_node *bound, *new_Phi, *block, **in;
4921 if (! is_Confirm(pred))
4924 bound = get_Confirm_bound(pred);
4925 pnc = get_Confirm_cmp(pred);
4927 NEW_ARR_A(ir_node *, in, n);
4928 in[0] = get_Confirm_value(pred);
4930 for (i = 1; i < n; ++i) {
4931 pred = get_irn_n(phi, i);
4933 if (! is_Confirm(pred) ||
4934 get_Confirm_bound(pred) != bound ||
4935 get_Confirm_cmp(pred) != pnc)
4937 in[i] = get_Confirm_value(pred);
4939 /* move the Confirm nodes "behind" the Phi */
4940 block = get_irn_n(phi, -1);
4941 new_Phi = new_r_Phi(block, n, in, get_irn_mode(phi));
4942 return new_r_Confirm(block, new_Phi, bound, pnc);
4946 } /* transform_node_Phi */
4949 * Returns the operands of a commutative bin-op, if one operand is
4950 * a const, it is returned as the second one.
4952 static void get_comm_Binop_Ops(ir_node *binop, ir_node **a, ir_node **c)
4954 ir_node *op_a = get_binop_left(binop);
4955 ir_node *op_b = get_binop_right(binop);
4957 assert(is_op_commutative(get_irn_op(binop)));
4959 if (is_Const(op_a)) {
4966 } /* get_comm_Binop_Ops */
4969 * Optimize a Or(And(Or(And(v,c4),c3),c2),c1) pattern if possible.
4970 * Such pattern may arise in bitfield stores.
4972 * value c4 value c4 & c2
4973 * AND c3 AND c1 | c3
4980 * AND c1 ===> OR if (c1 | c2) == 0x111..11
4983 static ir_node *transform_node_Or_bf_store(ir_node *or)
4987 ir_node *and_l, *c3;
4988 ir_node *value, *c4;
4989 ir_node *new_and, *new_const, *block;
4990 ir_mode *mode = get_irn_mode(or);
4992 tarval *tv1, *tv2, *tv3, *tv4, *tv;
4995 get_comm_Binop_Ops(or, &and, &c1);
4996 if (!is_Const(c1) || !is_And(and))
4999 get_comm_Binop_Ops(and, &or_l, &c2);
5003 tv1 = get_Const_tarval(c1);
5004 tv2 = get_Const_tarval(c2);
5006 tv = tarval_or(tv1, tv2);
5007 if (tarval_is_all_one(tv)) {
5008 /* the AND does NOT clear a bit with isn't set by the OR */
5009 set_Or_left(or, or_l);
5010 set_Or_right(or, c1);
5012 /* check for more */
5019 get_comm_Binop_Ops(or_l, &and_l, &c3);
5020 if (!is_Const(c3) || !is_And(and_l))
5023 get_comm_Binop_Ops(and_l, &value, &c4);
5027 /* ok, found the pattern, check for conditions */
5028 assert(mode == get_irn_mode(and));
5029 assert(mode == get_irn_mode(or_l));
5030 assert(mode == get_irn_mode(and_l));
5032 tv3 = get_Const_tarval(c3);
5033 tv4 = get_Const_tarval(c4);
5035 tv = tarval_or(tv4, tv2);
5036 if (!tarval_is_all_one(tv)) {
5037 /* have at least one 0 at the same bit position */
5041 if (tv3 != tarval_andnot(tv3, tv4)) {
5042 /* bit in the or_mask is outside the and_mask */
5046 if (tv1 != tarval_andnot(tv1, tv2)) {
5047 /* bit in the or_mask is outside the and_mask */
5051 /* ok, all conditions met */
5052 block = get_irn_n(or, -1);
5054 new_and = new_r_And(block, value, new_Const(tarval_and(tv4, tv2)), mode);
5056 new_const = new_Const(tarval_or(tv3, tv1));
5058 set_Or_left(or, new_and);
5059 set_Or_right(or, new_const);
5061 /* check for more */
5063 } /* transform_node_Or_bf_store */
5066 * Optimize an Or(shl(x, c), shr(x, bits - c)) into a Rotl
5068 static ir_node *transform_node_Or_Rotl(ir_node *or)
5070 ir_mode *mode = get_irn_mode(or);
5071 ir_node *shl, *shr, *block;
5072 ir_node *irn, *x, *c1, *c2, *v, *sub, *n, *rotval;
5075 if (! mode_is_int(mode))
5078 shl = get_binop_left(or);
5079 shr = get_binop_right(or);
5088 } else if (!is_Shl(shl)) {
5090 } else if (!is_Shr(shr)) {
5093 x = get_Shl_left(shl);
5094 if (x != get_Shr_left(shr))
5097 c1 = get_Shl_right(shl);
5098 c2 = get_Shr_right(shr);
5099 if (is_Const(c1) && is_Const(c2)) {
5100 tv1 = get_Const_tarval(c1);
5101 if (! tarval_is_long(tv1))
5104 tv2 = get_Const_tarval(c2);
5105 if (! tarval_is_long(tv2))
5108 if (get_tarval_long(tv1) + get_tarval_long(tv2)
5109 != (int) get_mode_size_bits(mode))
5112 /* yet, condition met */
5113 block = get_nodes_block(or);
5115 n = new_r_Rotl(block, x, c1, mode);
5117 DBG_OPT_ALGSIM1(or, shl, shr, n, FS_OPT_OR_SHFT_TO_ROTL);
5124 rotval = sub; /* a Rot right is not supported, so use a rot left */
5125 } else if (is_Sub(c2)) {
5131 if (get_Sub_right(sub) != v)
5134 c1 = get_Sub_left(sub);
5138 tv1 = get_Const_tarval(c1);
5139 if (! tarval_is_long(tv1))
5142 if (get_tarval_long(tv1) != (int) get_mode_size_bits(mode))
5145 /* yet, condition met */
5146 block = get_nodes_block(or);
5148 n = new_r_Rotl(block, x, rotval, mode);
5150 DBG_OPT_ALGSIM0(or, n, FS_OPT_OR_SHFT_TO_ROTL);
5152 } /* transform_node_Or_Rotl */
5157 static ir_node *transform_node_Or(ir_node *n)
5159 ir_node *c, *oldn = n;
5160 ir_node *a = get_Or_left(n);
5161 ir_node *b = get_Or_right(n);
5164 if (is_Not(a) && is_Not(b)) {
5165 /* ~a | ~b = ~(a&b) */
5166 ir_node *block = get_nodes_block(n);
5168 mode = get_irn_mode(n);
5171 n = new_rd_And(get_irn_dbg_info(n), block, a, b, mode);
5172 n = new_rd_Not(get_irn_dbg_info(n), block, n, mode);
5173 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_DEMORGAN);
5177 /* we can evaluate 2 Projs of the same Cmp */
5178 if (get_irn_mode(n) == mode_b && is_Proj(a) && is_Proj(b)) {
5179 ir_node *pred_a = get_Proj_pred(a);
5180 ir_node *pred_b = get_Proj_pred(b);
5181 if (pred_a == pred_b) {
5182 dbg_info *dbgi = get_irn_dbg_info(n);
5183 ir_node *block = get_nodes_block(pred_a);
5184 pn_Cmp pn_a = get_Proj_proj(a);
5185 pn_Cmp pn_b = get_Proj_proj(b);
5186 /* yes, we can simply calculate with pncs */
5187 pn_Cmp new_pnc = pn_a | pn_b;
5189 return new_rd_Proj(dbgi, block, pred_a, mode_b, new_pnc);
5193 mode = get_irn_mode(n);
5194 HANDLE_BINOP_PHI((eval_func) tarval_or, a, b, c, mode);
5196 n = transform_node_Or_bf_store(n);
5197 n = transform_node_Or_Rotl(n);
5201 n = transform_bitwise_distributive(n, transform_node_Or);
5204 } /* transform_node_Or */
5208 static ir_node *transform_node(ir_node *n);
5211 * Optimize (a >> c1) >> c2), works for Shr, Shrs, Shl, Rotl.
5213 * Should be moved to reassociation?
5215 static ir_node *transform_node_shift(ir_node *n)
5217 ir_node *left, *right;
5219 tarval *tv1, *tv2, *res;
5220 ir_node *in[2], *irn, *block;
5222 left = get_binop_left(n);
5224 /* different operations */
5225 if (get_irn_op(left) != get_irn_op(n))
5228 right = get_binop_right(n);
5229 tv1 = value_of(right);
5230 if (tv1 == tarval_bad)
5233 tv2 = value_of(get_binop_right(left));
5234 if (tv2 == tarval_bad)
5237 res = tarval_add(tv1, tv2);
5238 mode = get_irn_mode(n);
5240 /* beware: a simple replacement works only, if res < modulo shift */
5242 int modulo_shf = get_mode_modulo_shift(mode);
5243 if (modulo_shf > 0) {
5244 tarval *modulo = new_tarval_from_long(modulo_shf,
5245 get_tarval_mode(res));
5247 assert(modulo_shf >= (int) get_mode_size_bits(mode));
5249 /* shifting too much */
5250 if (!(tarval_cmp(res, modulo) & pn_Cmp_Lt)) {
5252 ir_node *block = get_nodes_block(n);
5253 dbg_info *dbgi = get_irn_dbg_info(n);
5254 ir_mode *smode = get_irn_mode(right);
5255 ir_node *cnst = new_Const_long(smode, get_mode_size_bits(mode) - 1);
5256 return new_rd_Shrs(dbgi, block, get_binop_left(left), cnst, mode);
5259 return new_Const(get_mode_null(mode));
5263 res = tarval_mod(res, new_tarval_from_long(get_mode_size_bits(mode), get_tarval_mode(res)));
5266 /* ok, we can replace it */
5267 block = get_nodes_block(n);
5269 in[0] = get_binop_left(left);
5270 in[1] = new_Const(res);
5272 irn = new_ir_node(NULL, get_Block_irg(block), block, get_irn_op(n), mode, 2, in);
5274 DBG_OPT_ALGSIM0(n, irn, FS_OPT_REASSOC_SHIFT);
5276 return transform_node(irn);
5277 } /* transform_node_shift */
5280 * normalisation: (x & c1) >> c2 to (x >> c2) & (c1 >> c2)
5282 * - and, or, xor instead of &
5283 * - Shl, Shr, Shrs, rotl instead of >>
5284 * (with a special case for Or/Xor + Shrs)
5286 static ir_node *transform_node_bitop_shift(ir_node *n)
5289 ir_node *right = get_binop_right(n);
5290 ir_mode *mode = get_irn_mode(n);
5291 ir_node *bitop_left;
5292 ir_node *bitop_right;
5303 assert(is_Shrs(n) || is_Shr(n) || is_Shl(n) || is_Rotl(n));
5305 if (!is_Const(right))
5308 left = get_binop_left(n);
5309 op_left = get_irn_op(left);
5310 if (op_left != op_And && op_left != op_Or && op_left != op_Eor)
5313 /* doing it with Shrs is not legal if the Or/Eor affects the topmost bit */
5314 if (is_Shrs(n) && (op_left == op_Or || op_left == op_Eor)) {
5315 /* TODO: test if sign bit is affectes */
5319 bitop_right = get_binop_right(left);
5320 if (!is_Const(bitop_right))
5323 bitop_left = get_binop_left(left);
5325 block = get_nodes_block(n);
5326 dbgi = get_irn_dbg_info(n);
5327 tv1 = get_Const_tarval(bitop_right);
5328 tv2 = get_Const_tarval(right);
5330 assert(get_tarval_mode(tv1) == mode);
5333 new_shift = new_rd_Shl(dbgi, block, bitop_left, right, mode);
5334 tv_shift = tarval_shl(tv1, tv2);
5335 } else if (is_Shr(n)) {
5336 new_shift = new_rd_Shr(dbgi, block, bitop_left, right, mode);
5337 tv_shift = tarval_shr(tv1, tv2);
5338 } else if (is_Shrs(n)) {
5339 new_shift = new_rd_Shrs(dbgi, block, bitop_left, right, mode);
5340 tv_shift = tarval_shrs(tv1, tv2);
5343 new_shift = new_rd_Rotl(dbgi, block, bitop_left, right, mode);
5344 tv_shift = tarval_rotl(tv1, tv2);
5347 assert(get_tarval_mode(tv_shift) == mode);
5348 new_const = new_Const(tv_shift);
5350 if (op_left == op_And) {
5351 new_bitop = new_rd_And(dbgi, block, new_shift, new_const, mode);
5352 } else if (op_left == op_Or) {
5353 new_bitop = new_rd_Or(dbgi, block, new_shift, new_const, mode);
5355 assert(op_left == op_Eor);
5356 new_bitop = new_rd_Eor(dbgi, block, new_shift, new_const, mode);
5364 * (x << c1) >> c2 <=> x OP (c2-c1) & ((-1 << c1) >> c2)
5366 * (x >> c1) << c2 <=> x OP (c2-c1) & ((-1 >> c1) << c2)
5367 * (also with x >>s c1 when c1>=c2)
5369 static ir_node *transform_node_shl_shr(ir_node *n)
5372 ir_node *right = get_binop_right(n);
5388 assert(is_Shl(n) || is_Shr(n) || is_Shrs(n));
5390 if (!is_Const(right))
5393 left = get_binop_left(n);
5394 mode = get_irn_mode(n);
5395 if (is_Shl(n) && (is_Shr(left) || is_Shrs(left))) {
5396 ir_node *shr_right = get_binop_right(left);
5398 if (!is_Const(shr_right))
5401 x = get_binop_left(left);
5402 tv_shr = get_Const_tarval(shr_right);
5403 tv_shl = get_Const_tarval(right);
5405 if (is_Shrs(left)) {
5406 /* shrs variant only allowed if c1 >= c2 */
5407 if (! (tarval_cmp(tv_shl, tv_shr) & pn_Cmp_Ge))
5410 tv_mask = tarval_shrs(get_mode_all_one(mode), tv_shr);
5413 tv_mask = tarval_shr(get_mode_all_one(mode), tv_shr);
5415 tv_mask = tarval_shl(tv_mask, tv_shl);
5416 } else if (is_Shr(n) && is_Shl(left)) {
5417 ir_node *shl_right = get_Shl_right(left);
5419 if (!is_Const(shl_right))
5422 x = get_Shl_left(left);
5423 tv_shr = get_Const_tarval(right);
5424 tv_shl = get_Const_tarval(shl_right);
5426 tv_mask = tarval_shl(get_mode_all_one(mode), tv_shl);
5427 tv_mask = tarval_shr(tv_mask, tv_shr);
5432 if (get_tarval_mode(tv_shl) != get_tarval_mode(tv_shr)) {
5433 tv_shl = tarval_convert_to(tv_shl, get_tarval_mode(tv_shr));
5436 assert(tv_mask != tarval_bad);
5437 assert(get_tarval_mode(tv_mask) == mode);
5439 irg = get_irn_irg(n);
5440 block = get_nodes_block(n);
5441 dbgi = get_irn_dbg_info(n);
5443 pnc = tarval_cmp(tv_shl, tv_shr);
5444 if (pnc == pn_Cmp_Lt || pnc == pn_Cmp_Eq) {
5445 tv_shift = tarval_sub(tv_shr, tv_shl, NULL);
5446 new_const = new_Const(tv_shift);
5448 new_shift = new_rd_Shrs(dbgi, block, x, new_const, mode);
5450 new_shift = new_rd_Shr(dbgi, block, x, new_const, mode);
5453 assert(pnc == pn_Cmp_Gt);
5454 tv_shift = tarval_sub(tv_shl, tv_shr, NULL);
5455 new_const = new_Const(tv_shift);
5456 new_shift = new_rd_Shl(dbgi, block, x, new_const, mode);
5459 new_const = new_Const(tv_mask);
5460 new_and = new_rd_And(dbgi, block, new_shift, new_const, mode);
5468 static ir_node *transform_node_Shr(ir_node *n)
5470 ir_node *c, *oldn = n;
5471 ir_node *left = get_Shr_left(n);
5472 ir_node *right = get_Shr_right(n);
5473 ir_mode *mode = get_irn_mode(n);
5475 HANDLE_BINOP_PHI((eval_func) tarval_shr, left, right, c, mode);
5476 n = transform_node_shift(n);
5479 n = transform_node_shl_shr(n);
5481 n = transform_node_bitop_shift(n);
5484 } /* transform_node_Shr */
5489 static ir_node *transform_node_Shrs(ir_node *n)
5491 ir_node *c, *oldn = n;
5492 ir_node *a = get_Shrs_left(n);
5493 ir_node *b = get_Shrs_right(n);
5494 ir_mode *mode = get_irn_mode(n);
5496 HANDLE_BINOP_PHI((eval_func) tarval_shrs, a, b, c, mode);
5497 n = transform_node_shift(n);
5500 n = transform_node_bitop_shift(n);
5503 } /* transform_node_Shrs */
5508 static ir_node *transform_node_Shl(ir_node *n)
5510 ir_node *c, *oldn = n;
5511 ir_node *a = get_Shl_left(n);
5512 ir_node *b = get_Shl_right(n);
5513 ir_mode *mode = get_irn_mode(n);
5515 HANDLE_BINOP_PHI((eval_func) tarval_shl, a, b, c, mode);
5516 n = transform_node_shift(n);
5519 n = transform_node_shl_shr(n);
5521 n = transform_node_bitop_shift(n);
5524 } /* transform_node_Shl */
5529 static ir_node *transform_node_Rotl(ir_node *n)
5531 ir_node *c, *oldn = n;
5532 ir_node *a = get_Rotl_left(n);
5533 ir_node *b = get_Rotl_right(n);
5534 ir_mode *mode = get_irn_mode(n);
5536 HANDLE_BINOP_PHI((eval_func) tarval_rotl, a, b, c, mode);
5537 n = transform_node_shift(n);
5540 n = transform_node_bitop_shift(n);
5543 } /* transform_node_Rotl */
5548 static ir_node *transform_node_Conv(ir_node *n)
5550 ir_node *c, *oldn = n;
5551 ir_mode *mode = get_irn_mode(n);
5552 ir_node *a = get_Conv_op(n);
5554 if (mode != mode_b && is_const_Phi(a)) {
5555 /* Do NOT optimize mode_b Conv's, this leads to remaining
5556 * Phib nodes later, because the conv_b_lower operation
5557 * is instantly reverted, when it tries to insert a Convb.
5559 c = apply_conv_on_phi(a, mode);
5561 DBG_OPT_ALGSIM0(oldn, c, FS_OPT_CONST_PHI);
5566 if (is_Unknown(a)) { /* Conv_A(Unknown_B) -> Unknown_A */
5567 return new_r_Unknown(current_ir_graph, mode);
5570 if (mode_is_reference(mode) &&
5571 get_mode_size_bits(mode) == get_mode_size_bits(get_irn_mode(a)) &&
5573 ir_node *l = get_Add_left(a);
5574 ir_node *r = get_Add_right(a);
5575 dbg_info *dbgi = get_irn_dbg_info(a);
5576 ir_node *block = get_nodes_block(n);
5578 ir_node *lop = get_Conv_op(l);
5579 if (get_irn_mode(lop) == mode) {
5580 /* ConvP(AddI(ConvI(P), x)) -> AddP(P, x) */
5581 n = new_rd_Add(dbgi, block, lop, r, mode);
5586 ir_node *rop = get_Conv_op(r);
5587 if (get_irn_mode(rop) == mode) {
5588 /* ConvP(AddI(x, ConvI(P))) -> AddP(x, P) */
5589 n = new_rd_Add(dbgi, block, l, rop, mode);
5596 } /* transform_node_Conv */
5599 * Remove dead blocks and nodes in dead blocks
5600 * in keep alive list. We do not generate a new End node.
5602 static ir_node *transform_node_End(ir_node *n)
5604 int i, j, n_keepalives = get_End_n_keepalives(n);
5607 NEW_ARR_A(ir_node *, in, n_keepalives);
5609 for (i = j = 0; i < n_keepalives; ++i) {
5610 ir_node *ka = get_End_keepalive(n, i);
5612 if (! is_Block_dead(ka)) {
5616 } else if (is_irn_pinned_in_irg(ka) && is_Block_dead(get_nodes_block(ka))) {
5618 } else if (is_Bad(ka)) {
5619 /* no need to keep Bad */
5624 if (j != n_keepalives)
5625 set_End_keepalives(n, j, in);
5627 } /* transform_node_End */
5629 bool is_negated_value(ir_node *a, ir_node *b)
5631 if (is_Minus(a) && get_Minus_op(a) == b)
5633 if (is_Minus(b) && get_Minus_op(b) == a)
5635 if (is_Sub(a) && is_Sub(b)) {
5636 ir_node *a_left = get_Sub_left(a);
5637 ir_node *a_right = get_Sub_right(a);
5638 ir_node *b_left = get_Sub_left(b);
5639 ir_node *b_right = get_Sub_right(b);
5641 if (a_left == b_right && a_right == b_left)
5649 * Optimize a Mux into some simpler cases.
5651 static ir_node *transform_node_Mux(ir_node *n)
5653 ir_node *oldn = n, *sel = get_Mux_sel(n);
5654 ir_mode *mode = get_irn_mode(n);
5655 ir_node *t = get_Mux_true(n);
5656 ir_node *f = get_Mux_false(n);
5657 ir_graph *irg = current_ir_graph;
5659 if (is_irg_state(irg, IR_GRAPH_STATE_KEEP_MUX))
5663 ir_node* block = get_nodes_block(n);
5665 ir_node* c1 = get_Mux_sel(t);
5666 ir_node* t1 = get_Mux_true(t);
5667 ir_node* f1 = get_Mux_false(t);
5669 /* Mux(cond0, Mux(cond1, x, y), y) -> typical if (cond0 && cond1) x else y */
5670 ir_node* and_ = new_r_And(block, c0, c1, mode_b);
5671 ir_node* new_mux = new_r_Mux(block, and_, f1, t1, mode);
5676 DBG_OPT_ALGSIM0(oldn, t, FS_OPT_MUX_COMBINE);
5677 } else if (f == t1) {
5678 /* Mux(cond0, Mux(cond1, x, y), x) */
5679 ir_node* not_c1 = new_r_Not(block, c1, mode_b);
5680 ir_node* and_ = new_r_And(block, c0, not_c1, mode_b);
5681 ir_node* new_mux = new_r_Mux(block, and_, t1, f1, mode);
5686 DBG_OPT_ALGSIM0(oldn, t, FS_OPT_MUX_COMBINE);
5688 } else if (is_Mux(f)) {
5689 ir_node* block = get_nodes_block(n);
5691 ir_node* c1 = get_Mux_sel(f);
5692 ir_node* t1 = get_Mux_true(f);
5693 ir_node* f1 = get_Mux_false(f);
5695 /* Mux(cond0, x, Mux(cond1, x, y)) -> typical if (cond0 || cond1) x else y */
5696 ir_node* or_ = new_r_Or(block, c0, c1, mode_b);
5697 ir_node* new_mux = new_r_Mux(block, or_, f1, t1, mode);
5702 DBG_OPT_ALGSIM0(oldn, f, FS_OPT_MUX_COMBINE);
5703 } else if (t == f1) {
5704 /* Mux(cond0, x, Mux(cond1, y, x)) */
5705 ir_node* not_c1 = new_r_Not(block, c1, mode_b);
5706 ir_node* or_ = new_r_Or(block, c0, not_c1, mode_b);
5707 ir_node* new_mux = new_r_Mux(block, or_, t1, f1, mode);
5712 DBG_OPT_ALGSIM0(oldn, f, FS_OPT_MUX_COMBINE);
5716 /* first normalization step: move a possible zero to the false case */
5718 ir_node *cmp = get_Proj_pred(sel);
5721 if (is_Const(t) && is_Const_null(t)) {
5724 /* Mux(x, 0, y) => Mux(x, y, 0) */
5725 pn_Cmp pnc = get_Proj_proj(sel);
5726 sel = new_r_Proj(get_nodes_block(cmp), cmp, mode_b,
5727 get_negated_pnc(pnc, get_irn_mode(get_Cmp_left(cmp))));
5728 n = new_rd_Mux(get_irn_dbg_info(n), get_nodes_block(n), sel, t, f, mode);
5736 /* note: after normalization, false can only happen on default */
5737 if (mode == mode_b) {
5738 dbg_info *dbg = get_irn_dbg_info(n);
5739 ir_node *block = get_nodes_block(n);
5742 tarval *tv_t = get_Const_tarval(t);
5743 if (tv_t == tarval_b_true) {
5745 /* Muxb(sel, true, false) = sel */
5746 assert(get_Const_tarval(f) == tarval_b_false);
5747 DBG_OPT_ALGSIM0(oldn, sel, FS_OPT_MUX_BOOL);
5750 /* Muxb(sel, true, x) = Or(sel, x) */
5751 n = new_rd_Or(dbg, block, sel, f, mode_b);
5752 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_OR_BOOL);
5756 } else if (is_Const(f)) {
5757 tarval *tv_f = get_Const_tarval(f);
5758 if (tv_f == tarval_b_true) {
5759 /* Muxb(sel, x, true) = Or(Not(sel), x) */
5760 ir_node* not_sel = new_rd_Not(dbg, block, sel, mode_b);
5761 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_ORNOT_BOOL);
5762 n = new_rd_Or(dbg, block, not_sel, t, mode_b);
5765 /* Muxb(sel, x, false) = And(sel, x) */
5766 assert(tv_f == tarval_b_false);
5767 n = new_rd_And(dbg, block, sel, t, mode_b);
5768 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_AND_BOOL);
5774 /* more normalization: try to normalize Mux(x, C1, C2) into Mux(x, +1/-1, 0) op C2 */
5775 if (is_Const(t) && is_Const(f) && mode_is_int(mode)) {
5776 tarval *a = get_Const_tarval(t);
5777 tarval *b = get_Const_tarval(f);
5780 if (tarval_is_one(a) && tarval_is_null(b)) {
5781 ir_node *block = get_nodes_block(n);
5782 ir_node *conv = new_r_Conv(block, sel, mode);
5784 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_CONV);
5786 } else if (tarval_is_null(a) && tarval_is_one(b)) {
5787 ir_node *block = get_nodes_block(n);
5788 ir_node *not_ = new_r_Not(block, sel, mode_b);
5789 ir_node *conv = new_r_Conv(block, not_, mode);
5791 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_CONV);
5794 /* TODO: it's not really clear if that helps in general or should be moved
5795 * to backend, especially with the MUX->Conv transformation above */
5796 if (tarval_cmp(a, b) & pn_Cmp_Gt) {
5797 diff = tarval_sub(a, b, NULL);
5800 diff = tarval_sub(b, a, NULL);
5804 if (diff == get_tarval_one(mode)) {
5805 dbg_info *dbg = get_irn_dbg_info(n);
5806 ir_node *block = get_nodes_block(n);
5807 ir_node *t = new_Const(tarval_sub(a, min, NULL));
5808 ir_node *f = new_Const(tarval_sub(b, min, NULL));
5809 n = new_rd_Mux(dbg, block, sel, f, t, mode);
5810 n = new_rd_Add(dbg, block, n, new_Const(min), mode);
5816 ir_node *cmp = get_Proj_pred(sel);
5817 long pn = get_Proj_proj(sel);
5820 * Note: normalization puts the constant on the right side,
5821 * so we check only one case.
5823 * Note further that these optimization work even for floating point
5824 * with NaN's because -NaN == NaN.
5825 * However, if +0 and -0 is handled differently, we cannot use the Abs/-Abs
5829 ir_node *cmp_r = get_Cmp_right(cmp);
5830 if (is_Const(cmp_r) && is_Const_null(cmp_r)) {
5831 ir_node *block = get_nodes_block(n);
5832 ir_node *cmp_l = get_Cmp_left(cmp);
5834 if (!mode_honor_signed_zeros(mode) && is_negated_value(f, t)) {
5837 /* NaN's work fine with abs, so it is ok to remove Uo */
5838 long pnc = pn & ~pn_Cmp_Uo;
5840 if ( (cmp_l == t && (pnc == pn_Cmp_Ge || pnc == pn_Cmp_Gt))
5841 || (cmp_l == f && (pnc == pn_Cmp_Le || pnc == pn_Cmp_Lt)))
5843 /* Mux(a >/>= 0, a, -a) = Mux(a </<= 0, -a, a) ==> Abs(a) */
5844 n = new_rd_Abs(get_irn_dbg_info(n), block, cmp_l, mode);
5845 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_ABS);
5847 } else if ((cmp_l == t && (pnc == pn_Cmp_Le || pnc == pn_Cmp_Lt))
5848 || (cmp_l == f && (pnc == pn_Cmp_Ge || pnc == pn_Cmp_Gt)))
5850 /* Mux(a </<= 0, a, -a) = Mux(a >/>= 0, -a, a) ==> -Abs(a) */
5851 n = new_rd_Abs(get_irn_dbg_info(n), block, cmp_l, mode);
5852 n = new_rd_Minus(get_irn_dbg_info(n), block, n, mode);
5853 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_ABS);
5858 if (mode_is_int(mode)) {
5860 if ((pn == pn_Cmp_Lg || pn == pn_Cmp_Eq) && is_And(cmp_l)) {
5861 /* Mux((a & b) != 0, c, 0) */
5862 ir_node *and_r = get_And_right(cmp_l);
5865 if (and_r == t && f == cmp_r) {
5866 if (is_Const(t) && tarval_is_single_bit(get_Const_tarval(t))) {
5867 if (pn == pn_Cmp_Lg) {
5868 /* Mux((a & 2^C) != 0, 2^C, 0) */
5870 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_BITOP);
5872 /* Mux((a & 2^C) == 0, 2^C, 0) */
5873 n = new_rd_Eor(get_irn_dbg_info(n),
5874 block, cmp_l, t, mode);
5875 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_BITOP);
5880 if (is_Shl(and_r)) {
5881 ir_node *shl_l = get_Shl_left(and_r);
5882 if (is_Const(shl_l) && is_Const_one(shl_l)) {
5883 if (and_r == t && f == cmp_r) {
5884 if (pn == pn_Cmp_Lg) {
5885 /* (a & (1 << n)) != 0, (1 << n), 0) */
5887 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_BITOP);
5889 /* (a & (1 << n)) == 0, (1 << n), 0) */
5890 n = new_rd_Eor(get_irn_dbg_info(n),
5891 block, cmp_l, t, mode);
5892 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_BITOP);
5898 and_l = get_And_left(cmp_l);
5899 if (is_Shl(and_l)) {
5900 ir_node *shl_l = get_Shl_left(and_l);
5901 if (is_Const(shl_l) && is_Const_one(shl_l)) {
5902 if (and_l == t && f == cmp_r) {
5903 if (pn == pn_Cmp_Lg) {
5904 /* ((1 << n) & a) != 0, (1 << n), 0) */
5906 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_BITOP);
5908 /* ((1 << n) & a) == 0, (1 << n), 0) */
5909 n = new_rd_Eor(get_irn_dbg_info(n),
5910 block, cmp_l, t, mode);
5911 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_BITOP);
5924 } /* transform_node_Mux */
5927 * optimize Sync nodes that have other syncs as input we simply add the inputs
5928 * of the other sync to our own inputs
5930 static ir_node *transform_node_Sync(ir_node *n)
5932 int arity = get_Sync_n_preds(n);
5935 for (i = 0; i < arity;) {
5936 ir_node *pred = get_Sync_pred(n, i);
5940 if (!is_Sync(pred)) {
5948 pred_arity = get_Sync_n_preds(pred);
5949 for (j = 0; j < pred_arity; ++j) {
5950 ir_node *pred_pred = get_Sync_pred(pred, j);
5955 add_irn_n(n, pred_pred);
5959 if (get_Sync_pred(n, k) == pred_pred) break;
5964 /* rehash the sync node */
5965 add_identities(current_ir_graph->value_table, n);
5968 } /* transform_node_Sync */
5971 * optimize a trampoline Call into a direct Call
5973 static ir_node *transform_node_Call(ir_node *call)
5975 ir_node *callee = get_Call_ptr(call);
5976 ir_node *adr, *mem, *res, *bl, **in;
5977 ir_type *ctp, *mtp, *tp;
5980 int i, n_res, n_param;
5983 if (! is_Proj(callee))
5985 callee = get_Proj_pred(callee);
5986 if (! is_Builtin(callee))
5988 if (get_Builtin_kind(callee) != ir_bk_inner_trampoline)
5991 mem = get_Call_mem(call);
5993 if (skip_Proj(mem) == callee) {
5994 /* memory is routed to the trampoline, skip */
5995 mem = get_Builtin_mem(callee);
5998 /* build a new call type */
5999 mtp = get_Call_type(call);
6000 tdb = get_type_dbg_info(mtp);
6002 n_res = get_method_n_ress(mtp);
6003 n_param = get_method_n_params(mtp);
6004 ctp = new_d_type_method(n_param + 1, n_res, tdb);
6006 for (i = 0; i < n_res; ++i)
6007 set_method_res_type(ctp, i, get_method_res_type(mtp, i));
6009 NEW_ARR_A(ir_node *, in, n_param + 1);
6011 /* FIXME: we don't need a new pointer type in every step */
6012 tp = get_irg_frame_type(current_ir_graph);
6013 tp = new_type_pointer(tp);
6014 set_method_param_type(ctp, 0, tp);
6016 in[0] = get_Builtin_param(callee, 2);
6017 for (i = 0; i < n_param; ++i) {
6018 set_method_param_type(ctp, i + 1, get_method_param_type(mtp, i));
6019 in[i + 1] = get_Call_param(call, i);
6021 var = get_method_variadicity(mtp);
6022 set_method_variadicity(ctp, var);
6023 if (var == variadicity_variadic) {
6024 set_method_first_variadic_param_index(ctp, get_method_first_variadic_param_index(mtp) + 1);
6026 /* When we resolve a trampoline, the function must be called by a this-call */
6027 set_method_calling_convention(ctp, get_method_calling_convention(mtp) | cc_this_call);
6028 set_method_additional_properties(ctp, get_method_additional_properties(mtp));
6030 adr = get_Builtin_param(callee, 1);
6032 db = get_irn_dbg_info(call);
6033 bl = get_nodes_block(call);
6035 res = new_rd_Call(db, bl, mem, adr, n_param + 1, in, ctp);
6036 if (get_irn_pinned(call) == op_pin_state_floats)
6037 set_irn_pinned(res, op_pin_state_floats);
6039 } /* transform_node_Call */
6042 * Tries several [inplace] [optimizing] transformations and returns an
6043 * equivalent node. The difference to equivalent_node() is that these
6044 * transformations _do_ generate new nodes, and thus the old node must
6045 * not be freed even if the equivalent node isn't the old one.
6047 static ir_node *transform_node(ir_node *n)
6052 * Transform_node is the only "optimizing transformation" that might
6053 * return a node with a different opcode. We iterate HERE until fixpoint
6054 * to get the final result.
6058 if (n->op->ops.transform_node != NULL)
6059 n = n->op->ops.transform_node(n);
6060 } while (oldn != n);
6063 } /* transform_node */
6066 * Sets the default transform node operation for an ir_op_ops.
6068 * @param code the opcode for the default operation
6069 * @param ops the operations initialized
6074 static ir_op_ops *firm_set_default_transform_node(ir_opcode code, ir_op_ops *ops)
6078 ops->transform_node = transform_node_##a; \
6080 #define CASE_PROJ(a) \
6082 ops->transform_node_Proj = transform_node_Proj_##a; \
6084 #define CASE_PROJ_EX(a) \
6086 ops->transform_node = transform_node_##a; \
6087 ops->transform_node_Proj = transform_node_Proj_##a; \
6096 CASE_PROJ_EX(DivMod);
6131 } /* firm_set_default_transform_node */
6134 /* **************** Common Subexpression Elimination **************** */
6136 /** The size of the hash table used, should estimate the number of nodes
6138 #define N_IR_NODES 512
6140 /** Compares the attributes of two Const nodes. */
6141 static int node_cmp_attr_Const(ir_node *a, ir_node *b)
6143 return (get_Const_tarval(a) != get_Const_tarval(b))
6144 || (get_Const_type(a) != get_Const_type(b));
6145 } /* node_cmp_attr_Const */
6147 /** Compares the attributes of two Proj nodes. */
6148 static int node_cmp_attr_Proj(ir_node *a, ir_node *b)
6150 return get_irn_proj_attr(a) != get_irn_proj_attr(b);
6151 } /* node_cmp_attr_Proj */
6153 /** Compares the attributes of two Filter nodes. */
6154 static int node_cmp_attr_Filter(ir_node *a, ir_node *b)
6156 return get_Filter_proj(a) != get_Filter_proj(b);
6157 } /* node_cmp_attr_Filter */
6159 /** Compares the attributes of two Alloc nodes. */
6160 static int node_cmp_attr_Alloc(ir_node *a, ir_node *b)
6162 const alloc_attr *pa = get_irn_alloc_attr(a);
6163 const alloc_attr *pb = get_irn_alloc_attr(b);
6164 return (pa->where != pb->where) || (pa->type != pb->type);
6165 } /* node_cmp_attr_Alloc */
6167 /** Compares the attributes of two Free nodes. */
6168 static int node_cmp_attr_Free(ir_node *a, ir_node *b)
6170 const free_attr *pa = get_irn_free_attr(a);
6171 const free_attr *pb = get_irn_free_attr(b);
6172 return (pa->where != pb->where) || (pa->type != pb->type);
6173 } /* node_cmp_attr_Free */
6175 /** Compares the attributes of two SymConst nodes. */
6176 static int node_cmp_attr_SymConst(ir_node *a, ir_node *b)
6178 const symconst_attr *pa = get_irn_symconst_attr(a);
6179 const symconst_attr *pb = get_irn_symconst_attr(b);
6180 return (pa->kind != pb->kind)
6181 || (pa->sym.type_p != pb->sym.type_p)
6182 || (pa->tp != pb->tp);
6183 } /* node_cmp_attr_SymConst */
6185 /** Compares the attributes of two Call nodes. */
6186 static int node_cmp_attr_Call(ir_node *a, ir_node *b)
6188 const call_attr *pa = get_irn_call_attr(a);
6189 const call_attr *pb = get_irn_call_attr(b);
6190 return (pa->type != pb->type)
6191 || (pa->tail_call != pb->tail_call);
6192 } /* node_cmp_attr_Call */
6194 /** Compares the attributes of two Sel nodes. */
6195 static int node_cmp_attr_Sel(ir_node *a, ir_node *b)
6197 const ir_entity *a_ent = get_Sel_entity(a);
6198 const ir_entity *b_ent = get_Sel_entity(b);
6201 (a_ent->kind != b_ent->kind) ||
6202 (a_ent->name != b_ent->name) ||
6203 (a_ent->owner != b_ent->owner) ||
6204 (a_ent->ld_name != b_ent->ld_name) ||
6205 (a_ent->type != b_ent->type);
6207 /* Matze: inlining of functions can produce 2 entities with same type,
6209 return a_ent != b_ent;
6210 } /* node_cmp_attr_Sel */
6212 /** Compares the attributes of two Phi nodes. */
6213 static int node_cmp_attr_Phi(ir_node *a, ir_node *b)
6215 /* we can only enter this function if both nodes have the same number of inputs,
6216 hence it is enough to check if one of them is a Phi0 */
6218 /* check the Phi0 pos attribute */
6219 return get_irn_phi_attr(a)->u.pos != get_irn_phi_attr(b)->u.pos;
6222 } /* node_cmp_attr_Phi */
6224 /** Compares the attributes of two Conv nodes. */
6225 static int node_cmp_attr_Conv(ir_node *a, ir_node *b)
6227 return get_Conv_strict(a) != get_Conv_strict(b);
6228 } /* node_cmp_attr_Conv */
6230 /** Compares the attributes of two Cast nodes. */
6231 static int node_cmp_attr_Cast(ir_node *a, ir_node *b)
6233 return get_Cast_type(a) != get_Cast_type(b);
6234 } /* node_cmp_attr_Cast */
6236 /** Compares the attributes of two Load nodes. */
6237 static int node_cmp_attr_Load(ir_node *a, ir_node *b)
6239 if (get_Load_volatility(a) == volatility_is_volatile ||
6240 get_Load_volatility(b) == volatility_is_volatile)
6241 /* NEVER do CSE on volatile Loads */
6243 /* do not CSE Loads with different alignment. Be conservative. */
6244 if (get_Load_align(a) != get_Load_align(b))
6247 return get_Load_mode(a) != get_Load_mode(b);
6248 } /* node_cmp_attr_Load */
6250 /** Compares the attributes of two Store nodes. */
6251 static int node_cmp_attr_Store(ir_node *a, ir_node *b)
6253 /* do not CSE Stores with different alignment. Be conservative. */
6254 if (get_Store_align(a) != get_Store_align(b))
6257 /* NEVER do CSE on volatile Stores */
6258 return (get_Store_volatility(a) == volatility_is_volatile ||
6259 get_Store_volatility(b) == volatility_is_volatile);
6260 } /* node_cmp_attr_Store */
6262 /** Compares two exception attributes */
6263 static int node_cmp_exception(ir_node *a, ir_node *b)
6265 const except_attr *ea = get_irn_except_attr(a);
6266 const except_attr *eb = get_irn_except_attr(b);
6268 return ea->pin_state != eb->pin_state;
6271 #define node_cmp_attr_Bound node_cmp_exception
6273 /** Compares the attributes of two Div nodes. */
6274 static int node_cmp_attr_Div(ir_node *a, ir_node *b)
6276 const divmod_attr *ma = get_irn_divmod_attr(a);
6277 const divmod_attr *mb = get_irn_divmod_attr(b);
6278 return ma->exc.pin_state != mb->exc.pin_state ||
6279 ma->resmode != mb->resmode ||
6280 ma->no_remainder != mb->no_remainder;
6281 } /* node_cmp_attr_Div */
6283 /** Compares the attributes of two DivMod nodes. */
6284 static int node_cmp_attr_DivMod(ir_node *a, ir_node *b)
6286 const divmod_attr *ma = get_irn_divmod_attr(a);
6287 const divmod_attr *mb = get_irn_divmod_attr(b);
6288 return ma->exc.pin_state != mb->exc.pin_state ||
6289 ma->resmode != mb->resmode;
6290 } /* node_cmp_attr_DivMod */
6292 /** Compares the attributes of two Mod nodes. */
6293 static int node_cmp_attr_Mod(ir_node *a, ir_node *b)
6295 const divmod_attr *ma = get_irn_divmod_attr(a);
6296 const divmod_attr *mb = get_irn_divmod_attr(b);
6297 return ma->exc.pin_state != mb->exc.pin_state ||
6298 ma->resmode != mb->resmode;
6299 } /* node_cmp_attr_Mod */
6301 /** Compares the attributes of two Quot nodes. */
6302 static int node_cmp_attr_Quot(ir_node *a, ir_node *b)
6304 const divmod_attr *ma = get_irn_divmod_attr(a);
6305 const divmod_attr *mb = get_irn_divmod_attr(b);
6306 return ma->exc.pin_state != mb->exc.pin_state ||
6307 ma->resmode != mb->resmode;
6308 } /* node_cmp_attr_Quot */
6310 /** Compares the attributes of two Confirm nodes. */
6311 static int node_cmp_attr_Confirm(ir_node *a, ir_node *b)
6313 /* no need to compare the bound, as this is a input */
6314 return (get_Confirm_cmp(a) != get_Confirm_cmp(b));
6315 } /* node_cmp_attr_Confirm */
6317 /** Compares the attributes of two Builtin nodes. */
6318 static int node_cmp_attr_Builtin(ir_node *a, ir_node *b)
6320 const builtin_attr *ma = get_irn_builtin_attr(a);
6321 const builtin_attr *mb = get_irn_builtin_attr(b);
6323 /* no need to compare the type, equal kind means equal type */
6324 return ma->kind != mb->kind;
6325 } /* node_cmp_attr_Builtin */
6327 /** Compares the attributes of two ASM nodes. */
6328 static int node_cmp_attr_ASM(ir_node *a, ir_node *b)
6331 const ir_asm_constraint *ca;
6332 const ir_asm_constraint *cb;
6335 if (get_ASM_text(a) != get_ASM_text(b))
6338 /* Should we really check the constraints here? Should be better, but is strange. */
6339 n = get_ASM_n_input_constraints(a);
6340 if (n != get_ASM_n_input_constraints(b))
6343 ca = get_ASM_input_constraints(a);
6344 cb = get_ASM_input_constraints(b);
6345 for (i = 0; i < n; ++i) {
6346 if (ca[i].pos != cb[i].pos || ca[i].constraint != cb[i].constraint)
6350 n = get_ASM_n_output_constraints(a);
6351 if (n != get_ASM_n_output_constraints(b))
6354 ca = get_ASM_output_constraints(a);
6355 cb = get_ASM_output_constraints(b);
6356 for (i = 0; i < n; ++i) {
6357 if (ca[i].pos != cb[i].pos || ca[i].constraint != cb[i].constraint)
6361 n = get_ASM_n_clobbers(a);
6362 if (n != get_ASM_n_clobbers(b))
6365 cla = get_ASM_clobbers(a);
6366 clb = get_ASM_clobbers(b);
6367 for (i = 0; i < n; ++i) {
6368 if (cla[i] != clb[i])
6372 } /* node_cmp_attr_ASM */
6374 /** Compares the inexistent attributes of two Dummy nodes. */
6375 static int node_cmp_attr_Dummy(ir_node *a, ir_node *b)
6383 * Set the default node attribute compare operation for an ir_op_ops.
6385 * @param code the opcode for the default operation
6386 * @param ops the operations initialized
6391 static ir_op_ops *firm_set_default_node_cmp_attr(ir_opcode code, ir_op_ops *ops)
6395 ops->node_cmp_attr = node_cmp_attr_##a; \
6428 } /* firm_set_default_node_cmp_attr */
6431 * Compare function for two nodes in the value table. Gets two
6432 * nodes as parameters. Returns 0 if the nodes are a Common Sub Expression.
6434 int identities_cmp(const void *elt, const void *key)
6436 ir_node *a = (ir_node *)elt;
6437 ir_node *b = (ir_node *)key;
6440 if (a == b) return 0;
6442 if ((get_irn_op(a) != get_irn_op(b)) ||
6443 (get_irn_mode(a) != get_irn_mode(b))) return 1;
6445 /* compare if a's in and b's in are of equal length */
6446 irn_arity_a = get_irn_intra_arity(a);
6447 if (irn_arity_a != get_irn_intra_arity(b))
6450 if (get_irn_pinned(a) == op_pin_state_pinned) {
6451 /* for pinned nodes, the block inputs must be equal */
6452 if (get_irn_intra_n(a, -1) != get_irn_intra_n(b, -1))
6454 } else if (! get_opt_global_cse()) {
6455 /* for block-local CSE both nodes must be in the same MacroBlock */
6456 if (get_irn_MacroBlock(a) != get_irn_MacroBlock(b))
6460 /* compare a->in[0..ins] with b->in[0..ins] */
6461 for (i = 0; i < irn_arity_a; ++i) {
6462 ir_node *pred_a = get_irn_intra_n(a, i);
6463 ir_node *pred_b = get_irn_intra_n(b, i);
6464 if (pred_a != pred_b) {
6465 /* if both predecessors are CSE neutral they might be different */
6466 if (!is_irn_cse_neutral(pred_a) || !is_irn_cse_neutral(pred_b))
6472 * here, we already now that the nodes are identical except their
6475 if (a->op->ops.node_cmp_attr)
6476 return a->op->ops.node_cmp_attr(a, b);
6479 } /* identities_cmp */
6482 * Calculate a hash value of a node.
6484 * @param node The IR-node
6486 unsigned ir_node_hash(const ir_node *node)
6488 return node->op->ops.hash(node);
6489 } /* ir_node_hash */
6492 pset *new_identities(void)
6494 return new_pset(identities_cmp, N_IR_NODES);
6495 } /* new_identities */
6497 void del_identities(pset *value_table)
6499 del_pset(value_table);
6500 } /* del_identities */
6502 /* Normalize a node by putting constants (and operands with larger
6503 * node index) on the right (operator side). */
6504 void ir_normalize_node(ir_node *n)
6506 if (is_op_commutative(get_irn_op(n))) {
6507 ir_node *l = get_binop_left(n);
6508 ir_node *r = get_binop_right(n);
6510 /* For commutative operators perform a OP b == b OP a but keep
6511 * constants on the RIGHT side. This helps greatly in some
6512 * optimizations. Moreover we use the idx number to make the form
6514 if (!operands_are_normalized(l, r)) {
6515 set_binop_left(n, r);
6516 set_binop_right(n, l);
6520 } /* ir_normalize_node */
6523 * Update the nodes after a match in the value table. If both nodes have
6524 * the same MacroBlock but different Blocks, we must ensure that the node
6525 * with the dominating Block (the node that is near to the MacroBlock header
6526 * is stored in the table.
6527 * Because a MacroBlock has only one "non-exception" flow, we don't need
6528 * dominance info here: We known, that one block must dominate the other and
6529 * following the only block input will allow to find it.
6531 static void update_known_irn(ir_node *known_irn, const ir_node *new_ir_node)
6533 ir_node *known_blk, *new_block, *block, *mbh;
6535 if (get_opt_global_cse()) {
6536 /* Block inputs are meaning less */
6539 known_blk = get_irn_n(known_irn, -1);
6540 new_block = get_irn_n(new_ir_node, -1);
6541 if (known_blk == new_block) {
6542 /* already in the same block */
6546 * We expect the typical case when we built the graph. In that case, the
6547 * known_irn is already the upper one, so checking this should be faster.
6550 mbh = get_Block_MacroBlock(new_block);
6552 if (block == known_blk) {
6553 /* ok, we have found it: known_block dominates new_block as expected */
6558 * We have reached the MacroBlock header NOT founding
6559 * the known_block. new_block must dominate known_block.
6562 set_irn_n(known_irn, -1, new_block);
6565 assert(get_Block_n_cfgpreds(block) == 1);
6566 block = get_Block_cfgpred_block(block, 0);
6568 } /* update_value_table */
6571 * Return the canonical node computing the same value as n.
6572 * Looks up the node in a hash table, enters it in the table
6573 * if it isn't there yet.
6575 * @param value_table the HashSet containing all nodes in the
6577 * @param n the node to look up
6579 * @return a node that computes the same value as n or n if no such
6580 * node could be found
6582 ir_node *identify_remember(pset *value_table, ir_node *n)
6586 if (!value_table) return n;
6588 ir_normalize_node(n);
6589 /* lookup or insert in hash table with given hash key. */
6590 nn = pset_insert(value_table, n, ir_node_hash(n));
6593 update_known_irn(nn, n);
6595 /* n is reachable again */
6596 edges_node_revival(nn, get_irn_irg(nn));
6600 } /* identify_remember */
6603 * During construction we set the op_pin_state_pinned flag in the graph right when the
6604 * optimization is performed. The flag turning on procedure global cse could
6605 * be changed between two allocations. This way we are safe.
6607 * @param value_table The value table
6608 * @param n The node to lookup
6610 static inline ir_node *identify_cons(pset *value_table, ir_node *n)
6614 n = identify_remember(value_table, n);
6615 if (n != old && get_irn_MacroBlock(old) != get_irn_MacroBlock(n))
6616 set_irg_pinned(current_ir_graph, op_pin_state_floats);
6618 } /* identify_cons */
6620 /* Add a node to the identities value table. */
6621 void add_identities(pset *value_table, ir_node *node)
6623 if (get_opt_cse() && is_no_Block(node))
6624 identify_remember(value_table, node);
6625 } /* add_identities */
6627 /* Visit each node in the value table of a graph. */
6628 void visit_all_identities(ir_graph *irg, irg_walk_func visit, void *env)
6631 ir_graph *rem = current_ir_graph;
6633 current_ir_graph = irg;
6634 foreach_pset(irg->value_table, node)
6636 current_ir_graph = rem;
6637 } /* visit_all_identities */
6640 * Garbage in, garbage out. If a node has a dead input, i.e., the
6641 * Bad node is input to the node, return the Bad node.
6643 static ir_node *gigo(ir_node *node)
6646 ir_op *op = get_irn_op(node);
6648 /* remove garbage blocks by looking at control flow that leaves the block
6649 and replacing the control flow by Bad. */
6650 if (get_irn_mode(node) == mode_X) {
6651 ir_node *block = get_nodes_block(skip_Proj(node));
6653 /* Don't optimize nodes in immature blocks. */
6654 if (!get_Block_matured(block))
6656 /* Don't optimize End, may have Bads. */
6657 if (op == op_End) return node;
6659 if (is_Block(block)) {
6660 if (is_Block_dead(block)) {
6661 /* control flow from dead block is dead */
6665 for (i = get_irn_arity(block) - 1; i >= 0; --i) {
6666 if (!is_Bad(get_irn_n(block, i)))
6670 ir_graph *irg = get_irn_irg(block);
6671 /* the start block is never dead */
6672 if (block != get_irg_start_block(irg)
6673 && block != get_irg_end_block(irg)) {
6675 * Do NOT kill control flow without setting
6676 * the block to dead of bad things can happen:
6677 * We get a Block that is not reachable be irg_block_walk()
6678 * but can be found by irg_walk()!
6680 set_Block_dead(block);
6687 /* Blocks, Phis and Tuples may have dead inputs, e.g., if one of the
6688 blocks predecessors is dead. */
6689 if (op != op_Block && op != op_Phi && op != op_Tuple) {
6690 irn_arity = get_irn_arity(node);
6693 * Beware: we can only read the block of a non-floating node.
6695 if (is_irn_pinned_in_irg(node) &&
6696 is_Block_dead(get_nodes_block(skip_Proj(node))))
6699 for (i = 0; i < irn_arity; i++) {
6700 ir_node *pred = get_irn_n(node, i);
6705 /* Propagating Unknowns here seems to be a bad idea, because
6706 sometimes we need a node as a input and did not want that
6708 However, it might be useful to move this into a later phase
6709 (if you think that optimizing such code is useful). */
6710 if (is_Unknown(pred) && mode_is_data(get_irn_mode(node)))
6711 return new_Unknown(get_irn_mode(node));
6716 /* With this code we violate the agreement that local_optimize
6717 only leaves Bads in Block, Phi and Tuple nodes. */
6718 /* If Block has only Bads as predecessors it's garbage. */
6719 /* If Phi has only Bads as predecessors it's garbage. */
6720 if ((op == op_Block && get_Block_matured(node)) || op == op_Phi) {
6721 irn_arity = get_irn_arity(node);
6722 for (i = 0; i < irn_arity; i++) {
6723 if (!is_Bad(get_irn_n(node, i))) break;
6725 if (i == irn_arity) node = new_Bad();
6732 * These optimizations deallocate nodes from the obstack.
6733 * It can only be called if it is guaranteed that no other nodes
6734 * reference this one, i.e., right after construction of a node.
6736 * @param n The node to optimize
6738 * current_ir_graph must be set to the graph of the node!
6740 ir_node *optimize_node(ir_node *n)
6744 ir_opcode iro = get_irn_opcode(n);
6746 /* Always optimize Phi nodes: part of the construction. */
6747 if ((!get_opt_optimize()) && (iro != iro_Phi)) return n;
6749 /* constant expression evaluation / constant folding */
6750 if (get_opt_constant_folding()) {
6751 /* neither constants nor Tuple values can be evaluated */
6752 if (iro != iro_Const && (get_irn_mode(n) != mode_T)) {
6753 unsigned fp_model = get_irg_fp_model(current_ir_graph);
6754 int old_fp_mode = tarval_fp_ops_enabled();
6756 tarval_enable_fp_ops(! (fp_model & fp_no_float_fold));
6758 /* try to evaluate */
6759 tv = computed_value(n);
6760 if (tv != tarval_bad) {
6762 ir_type *old_tp = get_irn_type(n);
6763 int i, arity = get_irn_arity(n);
6767 * Try to recover the type of the new expression.
6769 for (i = 0; i < arity && !old_tp; ++i)
6770 old_tp = get_irn_type(get_irn_n(n, i));
6773 * we MUST copy the node here temporary, because it's still needed
6774 * for DBG_OPT_CSTEVAL
6776 node_size = offsetof(ir_node, attr) + n->op->attr_size;
6777 oldn = alloca(node_size);
6779 memcpy(oldn, n, node_size);
6780 CLONE_ARR_A(ir_node *, oldn->in, n->in);
6782 /* ARG, copy the in array, we need it for statistics */
6783 memcpy(oldn->in, n->in, ARR_LEN(n->in) * sizeof(n->in[0]));
6785 /* note the inplace edges module */
6786 edges_node_deleted(n, current_ir_graph);
6788 /* evaluation was successful -- replace the node. */
6789 irg_kill_node(current_ir_graph, n);
6792 if (old_tp && get_type_mode(old_tp) == get_tarval_mode(tv))
6793 set_Const_type(nw, old_tp);
6794 DBG_OPT_CSTEVAL(oldn, nw);
6795 tarval_enable_fp_ops(old_fp_mode);
6798 tarval_enable_fp_ops(old_fp_mode);
6802 /* remove unnecessary nodes */
6803 if (get_opt_algebraic_simplification() ||
6804 (iro == iro_Phi) || /* always optimize these nodes. */
6806 (iro == iro_Proj) ||
6807 (iro == iro_Block) ) /* Flags tested local. */
6808 n = equivalent_node(n);
6810 /* Common Subexpression Elimination.
6812 * Checks whether n is already available.
6813 * The block input is used to distinguish different subexpressions. Right
6814 * now all nodes are op_pin_state_pinned to blocks, i.e., the CSE only finds common
6815 * subexpressions within a block.
6818 n = identify_cons(current_ir_graph->value_table, n);
6821 edges_node_deleted(oldn, current_ir_graph);
6823 /* We found an existing, better node, so we can deallocate the old node. */
6824 irg_kill_node(current_ir_graph, oldn);
6828 /* Some more constant expression evaluation that does not allow to
6830 iro = get_irn_opcode(n);
6831 if (get_opt_algebraic_simplification() ||
6832 (iro == iro_Cond) ||
6833 (iro == iro_Proj)) /* Flags tested local. */
6834 n = transform_node(n);
6836 /* Remove nodes with dead (Bad) input.
6837 Run always for transformation induced Bads. */
6840 /* Now we have a legal, useful node. Enter it in hash table for CSE */
6841 if (get_opt_cse() && (get_irn_opcode(n) != iro_Block)) {
6843 n = identify_remember(current_ir_graph->value_table, o);
6849 } /* optimize_node */
6853 * These optimizations never deallocate nodes (in place). This can cause dead
6854 * nodes lying on the obstack. Remove these by a dead node elimination,
6855 * i.e., a copying garbage collection.
6857 ir_node *optimize_in_place_2(ir_node *n)
6861 ir_opcode iro = get_irn_opcode(n);
6863 if (!get_opt_optimize() && !is_Phi(n)) return n;
6865 /* constant expression evaluation / constant folding */
6866 if (get_opt_constant_folding()) {
6867 /* neither constants nor Tuple values can be evaluated */
6868 if (iro != iro_Const && get_irn_mode(n) != mode_T) {
6869 unsigned fp_model = get_irg_fp_model(current_ir_graph);
6870 int old_fp_mode = tarval_fp_ops_enabled();
6872 tarval_enable_fp_ops((fp_model & fp_strict_algebraic) == 0);
6873 /* try to evaluate */
6874 tv = computed_value(n);
6875 if (tv != tarval_bad) {
6876 /* evaluation was successful -- replace the node. */
6877 ir_type *old_tp = get_irn_type(n);
6878 int i, arity = get_irn_arity(n);
6881 * Try to recover the type of the new expression.
6883 for (i = 0; i < arity && !old_tp; ++i)
6884 old_tp = get_irn_type(get_irn_n(n, i));
6888 if (old_tp && get_type_mode(old_tp) == get_tarval_mode(tv))
6889 set_Const_type(n, old_tp);
6891 DBG_OPT_CSTEVAL(oldn, n);
6892 tarval_enable_fp_ops(old_fp_mode);
6895 tarval_enable_fp_ops(old_fp_mode);
6899 /* remove unnecessary nodes */
6900 if (get_opt_constant_folding() ||
6901 (iro == iro_Phi) || /* always optimize these nodes. */
6902 (iro == iro_Id) || /* ... */
6903 (iro == iro_Proj) || /* ... */
6904 (iro == iro_Block) ) /* Flags tested local. */
6905 n = equivalent_node(n);
6907 /** common subexpression elimination **/
6908 /* Checks whether n is already available. */
6909 /* The block input is used to distinguish different subexpressions. Right
6910 now all nodes are op_pin_state_pinned to blocks, i.e., the cse only finds common
6911 subexpressions within a block. */
6912 if (get_opt_cse()) {
6914 n = identify_remember(current_ir_graph->value_table, o);
6919 /* Some more constant expression evaluation. */
6920 iro = get_irn_opcode(n);
6921 if (get_opt_constant_folding() ||
6922 (iro == iro_Cond) ||
6923 (iro == iro_Proj)) /* Flags tested local. */
6924 n = transform_node(n);
6926 /* Remove nodes with dead (Bad) input.
6927 Run always for transformation induced Bads. */
6930 /* Now we can verify the node, as it has no dead inputs any more. */
6933 /* Now we have a legal, useful node. Enter it in hash table for cse.
6934 Blocks should be unique anyways. (Except the successor of start:
6935 is cse with the start block!) */
6936 if (get_opt_cse() && (get_irn_opcode(n) != iro_Block)) {
6938 n = identify_remember(current_ir_graph->value_table, o);
6944 } /* optimize_in_place_2 */
6947 * Wrapper for external use, set proper status bits after optimization.
6949 ir_node *optimize_in_place(ir_node *n)
6951 /* Handle graph state */
6952 assert(get_irg_phase_state(current_ir_graph) != phase_building);
6954 if (get_opt_global_cse())
6955 set_irg_pinned(current_ir_graph, op_pin_state_floats);
6956 if (get_irg_outs_state(current_ir_graph) == outs_consistent)
6957 set_irg_outs_inconsistent(current_ir_graph);
6959 /* FIXME: Maybe we could also test whether optimizing the node can
6960 change the control graph. */
6961 set_irg_doms_inconsistent(current_ir_graph);
6962 return optimize_in_place_2(n);
6963 } /* optimize_in_place */
6966 * Calculate a hash value of a Const node.
6968 static unsigned hash_Const(const ir_node *node)
6972 /* special value for const, as they only differ in their tarval. */
6973 h = HASH_PTR(node->attr.con.tv);
6979 * Calculate a hash value of a SymConst node.
6981 static unsigned hash_SymConst(const ir_node *node)
6985 /* all others are pointers */
6986 h = HASH_PTR(node->attr.symc.sym.type_p);
6989 } /* hash_SymConst */
6992 * Set the default hash operation in an ir_op_ops.
6994 * @param code the opcode for the default operation
6995 * @param ops the operations initialized
7000 static ir_op_ops *firm_set_default_hash(ir_opcode code, ir_op_ops *ops)
7004 ops->hash = hash_##a; \
7007 /* hash function already set */
7008 if (ops->hash != NULL)
7015 /* use input/mode default hash if no function was given */
7016 ops->hash = firm_default_hash;
7024 * Sets the default operation for an ir_ops.
7026 ir_op_ops *firm_set_default_operations(ir_opcode code, ir_op_ops *ops)
7028 ops = firm_set_default_hash(code, ops);
7029 ops = firm_set_default_computed_value(code, ops);
7030 ops = firm_set_default_equivalent_node(code, ops);
7031 ops = firm_set_default_transform_node(code, ops);
7032 ops = firm_set_default_node_cmp_attr(code, ops);
7033 ops = firm_set_default_get_type(code, ops);
7034 ops = firm_set_default_get_type_attr(code, ops);
7035 ops = firm_set_default_get_entity_attr(code, ops);
7038 } /* firm_set_default_operations */