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) {
61 return get_Const_tarval(n); /* might return tarval_bad */
66 value_of_func value_of_ptr = default_value_of;
68 /* * Set a new value_of function. */
69 void set_value_of_func(value_of_func func) {
73 value_of_ptr = default_value_of;
77 * Return the value of a Constant.
79 static tarval *computed_value_Const(const ir_node *n) {
80 return get_Const_tarval(n);
81 } /* computed_value_Const */
84 * Return the value of a 'sizeof', 'alignof' or 'offsetof' SymConst.
86 static tarval *computed_value_SymConst(const ir_node *n) {
90 switch (get_SymConst_kind(n)) {
91 case symconst_type_size:
92 type = get_SymConst_type(n);
93 if (get_type_state(type) == layout_fixed)
94 return new_tarval_from_long(get_type_size_bytes(type), get_irn_mode(n));
96 case symconst_type_align:
97 type = get_SymConst_type(n);
98 if (get_type_state(type) == layout_fixed)
99 return new_tarval_from_long(get_type_alignment_bytes(type), get_irn_mode(n));
101 case symconst_ofs_ent:
102 ent = get_SymConst_entity(n);
103 type = get_entity_owner(ent);
104 if (get_type_state(type) == layout_fixed)
105 return new_tarval_from_long(get_entity_offset(ent), get_irn_mode(n));
111 } /* computed_value_SymConst */
114 * Return the value of an Add.
116 static tarval *computed_value_Add(const ir_node *n) {
117 ir_node *a = get_Add_left(n);
118 ir_node *b = get_Add_right(n);
120 tarval *ta = value_of(a);
121 tarval *tb = value_of(b);
123 if ((ta != tarval_bad) && (tb != tarval_bad))
124 return tarval_add(ta, tb);
127 } /* computed_value_Add */
130 * Return the value of a Sub.
131 * Special case: a - a
133 static tarval *computed_value_Sub(const ir_node *n) {
134 ir_mode *mode = get_irn_mode(n);
135 ir_node *a = get_Sub_left(n);
136 ir_node *b = get_Sub_right(n);
141 if (! mode_is_float(mode)) {
144 return get_mode_null(mode);
150 if ((ta != tarval_bad) && (tb != tarval_bad))
151 return tarval_sub(ta, tb, mode);
154 } /* computed_value_Sub */
157 * Return the value of a Carry.
158 * Special : a op 0, 0 op b
160 static tarval *computed_value_Carry(const ir_node *n) {
161 ir_node *a = get_binop_left(n);
162 ir_node *b = get_binop_right(n);
163 ir_mode *m = get_irn_mode(n);
165 tarval *ta = value_of(a);
166 tarval *tb = value_of(b);
168 if ((ta != tarval_bad) && (tb != tarval_bad)) {
170 return tarval_carry() ? get_mode_one(m) : get_mode_null(m);
172 if (tarval_is_null(ta) || tarval_is_null(tb))
173 return get_mode_null(m);
176 } /* computed_value_Carry */
179 * Return the value of a Borrow.
182 static tarval *computed_value_Borrow(const ir_node *n) {
183 ir_node *a = get_binop_left(n);
184 ir_node *b = get_binop_right(n);
185 ir_mode *m = get_irn_mode(n);
187 tarval *ta = value_of(a);
188 tarval *tb = value_of(b);
190 if ((ta != tarval_bad) && (tb != tarval_bad)) {
191 return tarval_cmp(ta, tb) == pn_Cmp_Lt ? get_mode_one(m) : get_mode_null(m);
192 } else if (tarval_is_null(ta)) {
193 return get_mode_null(m);
196 } /* computed_value_Borrow */
199 * Return the value of an unary Minus.
201 static tarval *computed_value_Minus(const ir_node *n) {
202 ir_node *a = get_Minus_op(n);
203 tarval *ta = value_of(a);
205 if (ta != tarval_bad)
206 return tarval_neg(ta);
209 } /* computed_value_Minus */
212 * Return the value of a Mul.
214 static tarval *computed_value_Mul(const ir_node *n) {
215 ir_node *a = get_Mul_left(n);
216 ir_node *b = get_Mul_right(n);
219 tarval *ta = value_of(a);
220 tarval *tb = value_of(b);
222 mode = get_irn_mode(n);
223 if (mode != get_irn_mode(a)) {
224 /* n * n = 2n bit multiplication */
225 ta = tarval_convert_to(ta, mode);
226 tb = tarval_convert_to(tb, mode);
229 if (ta != tarval_bad && tb != tarval_bad) {
230 return tarval_mul(ta, tb);
232 /* a * 0 != 0 if a == NaN or a == Inf */
233 if (!mode_is_float(mode)) {
234 /* a*0 = 0 or 0*b = 0 */
235 if (ta == get_mode_null(mode))
237 if (tb == get_mode_null(mode))
242 } /* computed_value_Mul */
245 * Return the value of an Abs.
247 static tarval *computed_value_Abs(const ir_node *n) {
248 ir_node *a = get_Abs_op(n);
249 tarval *ta = value_of(a);
251 if (ta != tarval_bad)
252 return tarval_abs(ta);
255 } /* computed_value_Abs */
258 * Return the value of an And.
259 * Special case: a & 0, 0 & b
261 static tarval *computed_value_And(const ir_node *n) {
262 ir_node *a = get_And_left(n);
263 ir_node *b = get_And_right(n);
265 tarval *ta = value_of(a);
266 tarval *tb = value_of(b);
268 if ((ta != tarval_bad) && (tb != tarval_bad)) {
269 return tarval_and (ta, tb);
271 if (tarval_is_null(ta)) return ta;
272 if (tarval_is_null(tb)) return tb;
275 } /* computed_value_And */
278 * Return the value of an Or.
279 * Special case: a | 1...1, 1...1 | b
281 static tarval *computed_value_Or(const ir_node *n) {
282 ir_node *a = get_Or_left(n);
283 ir_node *b = get_Or_right(n);
285 tarval *ta = value_of(a);
286 tarval *tb = value_of(b);
288 if ((ta != tarval_bad) && (tb != tarval_bad)) {
289 return tarval_or (ta, tb);
291 if (tarval_is_all_one(ta)) return ta;
292 if (tarval_is_all_one(tb)) return tb;
295 } /* computed_value_Or */
298 * Return the value of an Eor.
300 static tarval *computed_value_Eor(const ir_node *n) {
301 ir_node *a = get_Eor_left(n);
302 ir_node *b = get_Eor_right(n);
307 return get_mode_null(get_irn_mode(n));
312 if ((ta != tarval_bad) && (tb != tarval_bad)) {
313 return tarval_eor(ta, tb);
316 } /* computed_value_Eor */
319 * Return the value of a Not.
321 static tarval *computed_value_Not(const ir_node *n) {
322 ir_node *a = get_Not_op(n);
323 tarval *ta = value_of(a);
325 if (ta != tarval_bad)
326 return tarval_not(ta);
329 } /* computed_value_Not */
332 * Return the value of a Shl.
334 static tarval *computed_value_Shl(const ir_node *n) {
335 ir_node *a = get_Shl_left(n);
336 ir_node *b = get_Shl_right(n);
338 tarval *ta = value_of(a);
339 tarval *tb = value_of(b);
341 if ((ta != tarval_bad) && (tb != tarval_bad)) {
342 return tarval_shl(ta, tb);
345 } /* computed_value_Shl */
348 * Return the value of a Shr.
350 static tarval *computed_value_Shr(const ir_node *n) {
351 ir_node *a = get_Shr_left(n);
352 ir_node *b = get_Shr_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_shr(ta, tb);
361 } /* computed_value_Shr */
364 * Return the value of a Shrs.
366 static tarval *computed_value_Shrs(const ir_node *n) {
367 ir_node *a = get_Shrs_left(n);
368 ir_node *b = get_Shrs_right(n);
370 tarval *ta = value_of(a);
371 tarval *tb = value_of(b);
373 if ((ta != tarval_bad) && (tb != tarval_bad)) {
374 return tarval_shrs(ta, tb);
377 } /* computed_value_Shrs */
380 * Return the value of a Rotl.
382 static tarval *computed_value_Rotl(const ir_node *n) {
383 ir_node *a = get_Rotl_left(n);
384 ir_node *b = get_Rotl_right(n);
386 tarval *ta = value_of(a);
387 tarval *tb = value_of(b);
389 if ((ta != tarval_bad) && (tb != tarval_bad)) {
390 return tarval_rotl(ta, tb);
393 } /* computed_value_Rotl */
396 * Return the value of a Conv.
398 static tarval *computed_value_Conv(const ir_node *n) {
399 ir_node *a = get_Conv_op(n);
400 tarval *ta = value_of(a);
402 if (ta != tarval_bad)
403 return tarval_convert_to(ta, get_irn_mode(n));
406 } /* computed_value_Conv */
409 * Calculate the value of a Mux: can be evaluated, if the
410 * sel and the right input are known.
412 static tarval *computed_value_Mux(const ir_node *n) {
413 ir_node *sel = get_Mux_sel(n);
414 tarval *ts = value_of(sel);
416 if (ts == get_tarval_b_true()) {
417 ir_node *v = get_Mux_true(n);
420 else if (ts == get_tarval_b_false()) {
421 ir_node *v = get_Mux_false(n);
425 } /* computed_value_Mux */
428 * Calculate the value of a Confirm: can be evaluated,
429 * if it has the form Confirm(x, '=', Const).
431 static tarval *computed_value_Confirm(const ir_node *n) {
433 * Beware: we might produce Phi(Confirm(x == true), Confirm(x == false)).
434 * Do NOT optimize them away (jump threading wants them), so wait until
435 * remove_confirm is activated.
437 if (get_opt_remove_confirm()) {
438 if (get_Confirm_cmp(n) == pn_Cmp_Eq) {
439 tarval *tv = value_of(get_Confirm_bound(n));
440 if (tv != tarval_bad)
444 return value_of(get_Confirm_value(n));
445 } /* computed_value_Confirm */
448 * Return the value of a Proj(Cmp).
450 * This performs a first step of unreachable code elimination.
451 * Proj can not be computed, but folding a Cmp above the Proj here is
452 * not as wasteful as folding a Cmp into a Tuple of 16 Consts of which
454 * There are several case where we can evaluate a Cmp node, see later.
456 static tarval *computed_value_Proj_Cmp(const ir_node *n) {
457 ir_node *a = get_Proj_pred(n);
458 ir_node *aa = get_Cmp_left(a);
459 ir_node *ab = get_Cmp_right(a);
460 long proj_nr = get_Proj_proj(n);
463 * BEWARE: a == a is NOT always True for floating Point values, as
464 * NaN != NaN is defined, so we must check this here.
467 !mode_is_float(get_irn_mode(aa)) || proj_nr == pn_Cmp_Lt || proj_nr == pn_Cmp_Gt)
470 /* This is a trick with the bits used for encoding the Cmp
471 Proj numbers, the following statement is not the same:
472 return new_tarval_from_long (proj_nr == pn_Cmp_Eq, mode_b) */
473 return new_tarval_from_long (proj_nr & pn_Cmp_Eq, mode_b);
475 tarval *taa = value_of(aa);
476 tarval *tab = value_of(ab);
477 ir_mode *mode = get_irn_mode(aa);
480 * The predecessors of Cmp are target values. We can evaluate
483 if ((taa != tarval_bad) && (tab != tarval_bad)) {
484 /* strange checks... */
485 pn_Cmp flags = tarval_cmp(taa, tab);
486 if (flags != pn_Cmp_False) {
487 return new_tarval_from_long (proj_nr & flags, mode_b);
490 /* for integer values, we can check against MIN/MAX */
491 else if (mode_is_int(mode)) {
492 /* MIN <=/> x. This results in true/false. */
493 if (taa == get_mode_min(mode)) {
494 /* a compare with the MIN value */
495 if (proj_nr == pn_Cmp_Le)
496 return get_tarval_b_true();
497 else if (proj_nr == pn_Cmp_Gt)
498 return get_tarval_b_false();
500 /* x >=/< MIN. This results in true/false. */
502 if (tab == get_mode_min(mode)) {
503 /* a compare with the MIN value */
504 if (proj_nr == pn_Cmp_Ge)
505 return get_tarval_b_true();
506 else if (proj_nr == pn_Cmp_Lt)
507 return get_tarval_b_false();
509 /* MAX >=/< x. This results in true/false. */
510 else if (taa == get_mode_max(mode)) {
511 if (proj_nr == pn_Cmp_Ge)
512 return get_tarval_b_true();
513 else if (proj_nr == pn_Cmp_Lt)
514 return get_tarval_b_false();
516 /* x <=/> MAX. This results in true/false. */
517 else if (tab == get_mode_max(mode)) {
518 if (proj_nr == pn_Cmp_Le)
519 return get_tarval_b_true();
520 else if (proj_nr == pn_Cmp_Gt)
521 return get_tarval_b_false();
524 pn_Cmp cmp_result = vrp_cmp(aa, ab);
525 if (cmp_result != pn_Cmp_False) {
526 if (cmp_result == pn_Cmp_Lg) {
527 if( proj_nr == pn_Cmp_Eq) {
528 return get_tarval_b_false();
529 } else if(proj_nr == pn_Cmp_Lg) {
530 return get_tarval_b_true();
533 return new_tarval_from_long(cmp_result & proj_nr, mode_b);
538 * The predecessors are Allocs or (void*)(0) constants. Allocs never
539 * return NULL, they raise an exception. Therefore we can predict
543 ir_node *aaa = skip_Proj(aa);
544 ir_node *aba = skip_Proj(ab);
546 if ( ( (/* aa is ProjP and aaa is Alloc */
548 && mode_is_reference(get_irn_mode(aa))
550 && ( (/* ab is NULL */
551 mode_is_reference(get_irn_mode(ab))
552 && tarval_is_null(tab))
553 || (/* ab is other Alloc */
555 && mode_is_reference(get_irn_mode(ab))
558 || (/* aa is NULL and aba is Alloc */
559 mode_is_reference(get_irn_mode(aa))
560 && tarval_is_null(taa)
562 && mode_is_reference(get_irn_mode(ab))
565 return new_tarval_from_long(proj_nr & pn_Cmp_Lg, mode_b);
567 return computed_value_Cmp_Confirm(a, aa, ab, proj_nr);
568 } /* computed_value_Proj_Cmp */
571 * Return the value of a floating point Quot.
573 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) {
588 tarval *ta = value_of(a);
590 const ir_node *dummy;
592 /* Compute c1 / c2 or 0 / a, a != 0 */
593 if (tarval_is_null(ta) && value_not_zero(b, &dummy))
594 return ta; /* 0 / b == 0 */
596 if (ta != tarval_bad && tb != tarval_bad)
597 return tarval_div(ta, tb);
599 } /* do_computed_value_Div */
602 * Calculate the value of an integer Mod of two nodes.
603 * Special case: a % 1
605 static tarval *do_computed_value_Mod(const ir_node *a, const ir_node *b) {
606 tarval *ta = value_of(a);
607 tarval *tb = value_of(b);
609 /* Compute a % 1 or c1 % c2 */
610 if (tarval_is_one(tb))
611 return get_mode_null(get_irn_mode(a));
612 if (ta != tarval_bad && tb != tarval_bad)
613 return tarval_mod(ta, tb);
615 } /* do_computed_value_Mod */
618 * Return the value of a Proj(DivMod).
620 static tarval *computed_value_Proj_DivMod(const ir_node *n) {
621 long proj_nr = get_Proj_proj(n);
623 /* compute either the Div or the Mod part */
624 if (proj_nr == pn_DivMod_res_div) {
625 const ir_node *a = get_Proj_pred(n);
626 return do_computed_value_Div(get_DivMod_left(a), get_DivMod_right(a));
627 } else if (proj_nr == pn_DivMod_res_mod) {
628 const ir_node *a = get_Proj_pred(n);
629 return do_computed_value_Mod(get_DivMod_left(a), get_DivMod_right(a));
632 } /* computed_value_Proj_DivMod */
635 * Return the value of a Proj(Div).
637 static tarval *computed_value_Proj_Div(const ir_node *n) {
638 long proj_nr = get_Proj_proj(n);
640 if (proj_nr == pn_Div_res) {
641 const ir_node *a = get_Proj_pred(n);
642 return do_computed_value_Div(get_Div_left(a), get_Div_right(a));
645 } /* computed_value_Proj_Div */
648 * Return the value of a Proj(Mod).
650 static tarval *computed_value_Proj_Mod(const ir_node *n) {
651 long proj_nr = get_Proj_proj(n);
653 if (proj_nr == pn_Mod_res) {
654 const ir_node *a = get_Proj_pred(n);
655 return do_computed_value_Mod(get_Mod_left(a), get_Mod_right(a));
658 } /* computed_value_Proj_Mod */
661 * Return the value of a Proj(Quot).
663 static tarval *computed_value_Proj_Quot(const ir_node *n) {
664 long proj_nr = get_Proj_proj(n);
666 if (proj_nr == pn_Quot_res) {
667 const ir_node *a = get_Proj_pred(n);
668 return do_computed_value_Quot(get_Quot_left(a), get_Quot_right(a));
671 } /* computed_value_Proj_Quot */
674 * Return the value of a Proj.
676 static tarval *computed_value_Proj(const ir_node *proj) {
677 ir_node *n = get_Proj_pred(proj);
679 if (n->op->ops.computed_value_Proj != NULL)
680 return n->op->ops.computed_value_Proj(proj);
682 } /* computed_value_Proj */
685 * If the parameter n can be computed, return its value, else tarval_bad.
686 * Performs constant folding.
688 * @param n The node this should be evaluated
690 tarval *computed_value(const ir_node *n) {
691 if(mode_is_int(get_irn_mode(n)) && n->vrp.valid && tarval_is_all_one(
692 tarval_or(n->vrp.bits_set, n->vrp.bits_not_set))) {
693 return n->vrp.bits_set;
695 if (n->op->ops.computed_value)
696 return n->op->ops.computed_value(n);
698 } /* computed_value */
701 * Set the default computed_value evaluator in an ir_op_ops.
703 * @param code the opcode for the default operation
704 * @param ops the operations initialized
709 static ir_op_ops *firm_set_default_computed_value(ir_opcode code, ir_op_ops *ops)
713 ops->computed_value = computed_value_##a; \
715 #define CASE_PROJ(a) \
717 ops->computed_value_Proj = computed_value_Proj_##a; \
754 } /* firm_set_default_computed_value */
757 * Returns a equivalent block for another block.
758 * If the block has only one predecessor, this is
759 * the equivalent one. If the only predecessor of a block is
760 * the block itself, this is a dead block.
762 * If both predecessors of a block are the branches of a binary
763 * Cond, the equivalent block is Cond's block.
765 * If all predecessors of a block are bad or lies in a dead
766 * block, the current block is dead as well.
768 * Note, that blocks are NEVER turned into Bad's, instead
769 * the dead_block flag is set. So, never test for is_Bad(block),
770 * always use is_dead_Block(block).
772 static ir_node *equivalent_node_Block(ir_node *n)
777 /* don't optimize dead or labeled blocks */
778 if (is_Block_dead(n) || has_Block_entity(n))
781 n_preds = get_Block_n_cfgpreds(n);
783 /* The Block constructor does not call optimize, but mature_immBlock()
784 calls the optimization. */
785 assert(get_Block_matured(n));
787 /* Straightening: a single entry Block following a single exit Block
788 can be merged, if it is not the Start block. */
789 /* !!! Beware, all Phi-nodes of n must have been optimized away.
790 This should be true, as the block is matured before optimize is called.
791 But what about Phi-cycles with the Phi0/Id that could not be resolved?
792 Remaining Phi nodes are just Ids. */
794 ir_node *pred = skip_Proj(get_Block_cfgpred(n, 0));
797 ir_node *predblock = get_nodes_block(pred);
798 if (predblock == oldn) {
799 /* Jmp jumps into the block it is in -- deal self cycle. */
800 n = set_Block_dead(n);
801 DBG_OPT_DEAD_BLOCK(oldn, n);
802 } else if (get_opt_control_flow_straightening()) {
804 DBG_OPT_STG(oldn, n);
806 } else if (is_Cond(pred)) {
807 ir_node *predblock = get_nodes_block(pred);
808 if (predblock == oldn) {
809 /* Jmp jumps into the block it is in -- deal self cycle. */
810 n = set_Block_dead(n);
811 DBG_OPT_DEAD_BLOCK(oldn, n);
814 } else if ((n_preds == 2) &&
815 (get_opt_control_flow_weak_simplification())) {
816 /* Test whether Cond jumps twice to this block
817 * The more general case which more than 2 predecessors is handles
818 * in optimize_cf(), we handle only this special case for speed here.
820 ir_node *a = get_Block_cfgpred(n, 0);
821 ir_node *b = get_Block_cfgpred(n, 1);
823 if (is_Proj(a) && is_Proj(b)) {
824 ir_node *cond = get_Proj_pred(a);
826 if (cond == get_Proj_pred(b) && is_Cond(cond) &&
827 get_irn_mode(get_Cond_selector(cond)) == mode_b) {
828 /* Also a single entry Block following a single exit Block. Phis have
829 twice the same operand and will be optimized away. */
830 n = get_nodes_block(cond);
831 DBG_OPT_IFSIM1(oldn, a, b, n);
834 } else if (get_opt_unreachable_code() &&
835 (n != get_irg_start_block(current_ir_graph)) &&
836 (n != get_irg_end_block(current_ir_graph)) ) {
839 /* If all inputs are dead, this block is dead too, except if it is
840 the start or end block. This is one step of unreachable code
842 for (i = get_Block_n_cfgpreds(n) - 1; i >= 0; --i) {
843 ir_node *pred = get_Block_cfgpred(n, i);
846 if (is_Bad(pred)) continue;
847 pred_blk = get_nodes_block(skip_Proj(pred));
849 if (is_Block_dead(pred_blk)) continue;
852 /* really found a living input */
857 n = set_Block_dead(n);
858 DBG_OPT_DEAD_BLOCK(oldn, n);
863 } /* equivalent_node_Block */
866 * Returns a equivalent node for a Jmp, a Bad :-)
867 * Of course this only happens if the Block of the Jmp is dead.
869 static ir_node *equivalent_node_Jmp(ir_node *n) {
872 /* unreachable code elimination */
873 if (is_Block_dead(get_nodes_block(n))) {
874 n = get_irg_bad(current_ir_graph);
875 DBG_OPT_DEAD_BLOCK(oldn, n);
878 } /* equivalent_node_Jmp */
880 /** Raise is handled in the same way as Jmp. */
881 #define equivalent_node_Raise equivalent_node_Jmp
884 /* We do not evaluate Cond here as we replace it by a new node, a Jmp.
885 See transform_node_Proj_Cond(). */
888 * Optimize operations that are commutative and have neutral 0,
889 * so a op 0 = 0 op a = a.
891 static ir_node *equivalent_node_neutral_zero(ir_node *n) {
894 ir_node *a = get_binop_left(n);
895 ir_node *b = get_binop_right(n);
900 /* After running compute_node there is only one constant predecessor.
901 Find this predecessors value and remember the other node: */
902 if ((tv = value_of(a)) != tarval_bad) {
904 } else if ((tv = value_of(b)) != tarval_bad) {
909 /* If this predecessors constant value is zero, the operation is
910 * unnecessary. Remove it.
912 * Beware: If n is a Add, the mode of on and n might be different
913 * which happens in this rare construction: NULL + 3.
914 * Then, a Conv would be needed which we cannot include here.
916 if (tarval_is_null(tv) && get_irn_mode(on) == get_irn_mode(n)) {
919 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_0);
923 } /* equivalent_node_neutral_zero */
926 * Eor is commutative and has neutral 0.
928 static ir_node *equivalent_node_Eor(ir_node *n) {
933 n = equivalent_node_neutral_zero(n);
934 if (n != oldn) return n;
937 b = get_Eor_right(n);
940 ir_node *aa = get_Eor_left(a);
941 ir_node *ab = get_Eor_right(a);
944 /* (a ^ b) ^ a -> b */
946 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_EOR_A_B_A);
948 } else if (ab == b) {
949 /* (a ^ b) ^ b -> a */
951 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_EOR_A_B_A);
956 ir_node *ba = get_Eor_left(b);
957 ir_node *bb = get_Eor_right(b);
960 /* a ^ (a ^ b) -> b */
962 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_EOR_A_B_A);
964 } else if (bb == a) {
965 /* a ^ (b ^ a) -> b */
967 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_EOR_A_B_A);
975 * Optimize a - 0 and (a - x) + x (for modes with wrap-around).
977 * The second one looks strange, but this construct
978 * is used heavily in the LCC sources :-).
980 * Beware: The Mode of an Add may be different than the mode of its
981 * predecessors, so we could not return a predecessors in all cases.
983 static ir_node *equivalent_node_Add(ir_node *n) {
985 ir_node *left, *right;
986 ir_mode *mode = get_irn_mode(n);
988 n = equivalent_node_neutral_zero(n);
992 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
993 if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
996 left = get_Add_left(n);
997 right = get_Add_right(n);
1000 if (get_Sub_right(left) == right) {
1003 n = get_Sub_left(left);
1004 if (mode == get_irn_mode(n)) {
1005 DBG_OPT_ALGSIM1(oldn, left, right, n, FS_OPT_ADD_SUB);
1010 if (is_Sub(right)) {
1011 if (get_Sub_right(right) == left) {
1014 n = get_Sub_left(right);
1015 if (mode == get_irn_mode(n)) {
1016 DBG_OPT_ALGSIM1(oldn, left, right, n, FS_OPT_ADD_SUB);
1022 } /* equivalent_node_Add */
1025 * optimize operations that are not commutative but have neutral 0 on left,
1028 static ir_node *equivalent_node_left_zero(ir_node *n) {
1031 ir_node *a = get_binop_left(n);
1032 ir_node *b = get_binop_right(n);
1033 tarval *tb = value_of(b);
1035 if (tarval_is_null(tb)) {
1038 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_0);
1041 } /* equivalent_node_left_zero */
1043 #define equivalent_node_Shl equivalent_node_left_zero
1044 #define equivalent_node_Shr equivalent_node_left_zero
1045 #define equivalent_node_Shrs equivalent_node_left_zero
1046 #define equivalent_node_Rotl equivalent_node_left_zero
1049 * Optimize a - 0 and (a + x) - x (for modes with wrap-around).
1051 * The second one looks strange, but this construct
1052 * is used heavily in the LCC sources :-).
1054 * Beware: The Mode of a Sub may be different than the mode of its
1055 * predecessors, so we could not return a predecessors in all cases.
1057 static ir_node *equivalent_node_Sub(ir_node *n) {
1060 ir_mode *mode = get_irn_mode(n);
1063 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
1064 if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
1067 b = get_Sub_right(n);
1070 /* Beware: modes might be different */
1071 if (tarval_is_null(tb)) {
1072 ir_node *a = get_Sub_left(n);
1073 if (mode == get_irn_mode(a)) {
1076 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_0);
1080 } /* equivalent_node_Sub */
1084 * Optimize an "self-inverse unary op", ie op(op(n)) = n.
1087 * -(-a) == a, but might overflow two times.
1088 * We handle it anyway here but the better way would be a
1089 * flag. This would be needed for Pascal for instance.
1091 static ir_node *equivalent_node_idempotent_unop(ir_node *n) {
1093 ir_node *pred = get_unop_op(n);
1095 /* optimize symmetric unop */
1096 if (get_irn_op(pred) == get_irn_op(n)) {
1097 n = get_unop_op(pred);
1098 DBG_OPT_ALGSIM2(oldn, pred, n, FS_OPT_IDEM_UNARY);
1101 } /* equivalent_node_idempotent_unop */
1103 /** Optimize Not(Not(x)) == x. */
1104 #define equivalent_node_Not equivalent_node_idempotent_unop
1106 /** -(-x) == x ??? Is this possible or can --x raise an
1107 out of bounds exception if min =! max? */
1108 #define equivalent_node_Minus equivalent_node_idempotent_unop
1111 * Optimize a * 1 = 1 * a = a.
1113 static ir_node *equivalent_node_Mul(ir_node *n) {
1115 ir_node *a = get_Mul_left(n);
1117 /* we can handle here only the n * n = n bit cases */
1118 if (get_irn_mode(n) == get_irn_mode(a)) {
1119 ir_node *b = get_Mul_right(n);
1123 * Mul is commutative and has again an other neutral element.
1124 * Constants are place right, so check this case first.
1127 if (tarval_is_one(tv)) {
1129 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
1132 if (tarval_is_one(tv)) {
1134 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
1139 } /* equivalent_node_Mul */
1142 * Use algebraic simplification a | a = a | 0 = 0 | a = a.
1144 static ir_node *equivalent_node_Or(ir_node *n) {
1147 ir_node *a = get_Or_left(n);
1148 ir_node *b = get_Or_right(n);
1152 n = a; /* Or has it's own neutral element */
1153 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_OR);
1156 /* constants are cormalized to right, check this site first */
1158 if (tarval_is_null(tv)) {
1160 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_OR);
1164 if (tarval_is_null(tv)) {
1166 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_OR);
1171 } /* equivalent_node_Or */
1174 * Optimize a & 0b1...1 = 0b1...1 & a = a & a = (a|X) & a = a.
1176 static ir_node *equivalent_node_And(ir_node *n) {
1179 ir_node *a = get_And_left(n);
1180 ir_node *b = get_And_right(n);
1184 n = a; /* And has it's own neutral element */
1185 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_AND);
1188 /* constants are normalized to right, check this site first */
1190 if (tarval_is_all_one(tv)) {
1192 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1195 if (tv != get_tarval_bad()) {
1196 ir_mode *mode = get_irn_mode(n);
1197 if (!mode_is_signed(mode) && is_Conv(a)) {
1198 ir_node *convop = get_Conv_op(a);
1199 ir_mode *convopmode = get_irn_mode(convop);
1200 if (!mode_is_signed(convopmode)) {
1201 if (tarval_is_all_one(tarval_convert_to(tv, convopmode))) {
1202 /* Conv(X) & all_one(mode(X)) = Conv(X) */
1204 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1211 if (tarval_is_all_one(tv)) {
1213 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1217 if (b == get_Or_left(a) || b == get_Or_right(a)) {
1220 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1225 if (a == get_Or_left(b) || a == get_Or_right(b)) {
1228 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1233 } /* equivalent_node_And */
1236 * Try to remove useless Conv's:
1238 static ir_node *equivalent_node_Conv(ir_node *n) {
1240 ir_node *a = get_Conv_op(n);
1242 ir_mode *n_mode = get_irn_mode(n);
1243 ir_mode *a_mode = get_irn_mode(a);
1246 if (n_mode == a_mode) { /* No Conv necessary */
1247 if (get_Conv_strict(n)) {
1250 /* neither Minus nor Abs nor Confirm change the precision,
1251 so we can "look-through" */
1254 p = get_Minus_op(p);
1255 } else if (is_Abs(p)) {
1257 } else if (is_Confirm(p)) {
1258 p = get_Confirm_value(p);
1264 if (is_Conv(p) && get_Conv_strict(p)) {
1265 /* we known already, that a_mode == n_mode, and neither
1266 Abs nor Minus change the mode, so the second Conv
1268 assert(get_irn_mode(p) == n_mode);
1270 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CONV);
1274 ir_node *pred = get_Proj_pred(p);
1275 if (is_Load(pred)) {
1276 /* Loads always return with the exact precision of n_mode */
1277 assert(get_Load_mode(pred) == n_mode);
1279 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CONV);
1282 if (is_Proj(pred) && get_Proj_proj(pred) == pn_Start_T_args) {
1283 pred = get_Proj_pred(pred);
1284 if (is_Start(pred)) {
1285 /* Arguments always return with the exact precision,
1286 as strictConv's are place before Call -- if the
1287 caller was compiled with the same setting.
1288 Otherwise, the semantics is probably still right. */
1289 assert(get_irn_mode(p) == n_mode);
1291 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CONV);
1297 /* special case: the immediate predecessor is also a Conv */
1298 if (! get_Conv_strict(a)) {
1299 /* first one is not strict, kick it */
1301 a_mode = get_irn_mode(a);
1305 /* else both are strict conv, second is superfluous */
1307 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CONV);
1312 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CONV);
1315 } else if (is_Conv(a)) { /* Conv(Conv(b)) */
1316 ir_node *b = get_Conv_op(a);
1317 ir_mode *b_mode = get_irn_mode(b);
1319 if (get_Conv_strict(n) && get_Conv_strict(a)) {
1320 /* both are strict conv */
1321 if (smaller_mode(a_mode, n_mode)) {
1322 /* both are strict, but the first is smaller, so
1323 the second cannot remove more precision, remove the
1325 set_Conv_strict(n, 0);
1328 if (n_mode == b_mode) {
1329 if (! get_Conv_strict(n) && ! get_Conv_strict(a)) {
1330 if (n_mode == mode_b) {
1331 n = b; /* Convb(Conv*(xxxb(...))) == xxxb(...) */
1332 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
1334 } else if (get_mode_arithmetic(n_mode) == get_mode_arithmetic(a_mode)) {
1335 if (values_in_mode(b_mode, a_mode)) {
1336 n = b; /* ConvS(ConvL(xxxS(...))) == xxxS(...) */
1337 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
1342 if (mode_is_int(n_mode) && get_mode_arithmetic(a_mode) == irma_ieee754) {
1343 /* ConvI(ConvF(I)) -> I, iff float mantissa >= int mode */
1344 unsigned int_mantissa = get_mode_size_bits(n_mode) - (mode_is_signed(n_mode) ? 1 : 0);
1345 unsigned float_mantissa = tarval_ieee754_get_mantissa_size(a_mode);
1347 if (float_mantissa >= int_mantissa) {
1349 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
1354 if (smaller_mode(b_mode, a_mode)) {
1355 if (get_Conv_strict(n))
1356 set_Conv_strict(b, 1);
1357 n = b; /* ConvA(ConvB(ConvA(...))) == ConvA(...) */
1358 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
1365 } /* equivalent_node_Conv */
1368 * A Cast may be removed if the type of the previous node
1369 * is already the type of the Cast.
1371 static ir_node *equivalent_node_Cast(ir_node *n) {
1373 ir_node *pred = get_Cast_op(n);
1375 if (get_irn_type(pred) == get_Cast_type(n)) {
1377 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CAST);
1380 } /* equivalent_node_Cast */
1383 * - fold Phi-nodes, iff they have only one predecessor except
1386 static ir_node *equivalent_node_Phi(ir_node *n) {
1391 ir_node *first_val = NULL; /* to shutup gcc */
1393 if (!get_opt_normalize()) return n;
1395 n_preds = get_Phi_n_preds(n);
1397 block = get_nodes_block(n);
1398 if (is_Block_dead(block)) /* Control dead */
1399 return get_irg_bad(current_ir_graph);
1401 if (n_preds == 0) return n; /* Phi of dead Region without predecessors. */
1403 /* Find first non-self-referencing input */
1404 for (i = 0; i < n_preds; ++i) {
1405 first_val = get_Phi_pred(n, i);
1406 if ( (first_val != n) /* not self pointer */
1408 /* BEWARE: when the if is changed to 1, Phi's will ignore it's Bad
1409 * predecessors. Then, Phi nodes in dead code might be removed, causing
1410 * nodes pointing to themself (Add's for instance).
1411 * This is really bad and causes endless recursions in several
1412 * code pathes, so we do NOT optimize such a code.
1413 * This is not that bad as it sounds, optimize_cf() removes bad control flow
1414 * (and bad Phi predecessors), so live code is optimized later.
1416 && (! is_Bad(get_Block_cfgpred(block, i)))
1418 ) { /* value not dead */
1419 break; /* then found first value. */
1424 /* A totally Bad or self-referencing Phi (we didn't break the above loop) */
1425 return get_irg_bad(current_ir_graph);
1428 /* search for rest of inputs, determine if any of these
1429 are non-self-referencing */
1430 while (++i < n_preds) {
1431 ir_node *scnd_val = get_Phi_pred(n, i);
1432 if ( (scnd_val != n)
1433 && (scnd_val != first_val)
1436 && (! is_Bad(get_Block_cfgpred(block, i)))
1444 /* Fold, if no multiple distinct non-self-referencing inputs */
1446 DBG_OPT_PHI(oldn, n);
1449 } /* equivalent_node_Phi */
1452 * Several optimizations:
1453 * - fold Sync-nodes, iff they have only one predecessor except
1456 static ir_node *equivalent_node_Sync(ir_node *n) {
1457 int arity = get_Sync_n_preds(n);
1460 for (i = 0; i < arity;) {
1461 ir_node *pred = get_Sync_pred(n, i);
1464 /* Remove Bad predecessors */
1471 /* Remove duplicate predecessors */
1477 if (get_Sync_pred(n, j) == pred) {
1485 if (arity == 0) return get_irg_bad(current_ir_graph);
1486 if (arity == 1) return get_Sync_pred(n, 0);
1488 } /* equivalent_node_Sync */
1491 * Optimize Proj(Tuple).
1493 static ir_node *equivalent_node_Proj_Tuple(ir_node *proj) {
1494 ir_node *oldn = proj;
1495 ir_node *tuple = get_Proj_pred(proj);
1497 /* Remove the Tuple/Proj combination. */
1498 proj = get_Tuple_pred(tuple, get_Proj_proj(proj));
1499 DBG_OPT_TUPLE(oldn, tuple, proj);
1502 } /* equivalent_node_Proj_Tuple */
1505 * Optimize a / 1 = a.
1507 static ir_node *equivalent_node_Proj_Div(ir_node *proj) {
1508 ir_node *oldn = proj;
1509 ir_node *div = get_Proj_pred(proj);
1510 ir_node *b = get_Div_right(div);
1511 tarval *tb = value_of(b);
1513 /* Div is not commutative. */
1514 if (tarval_is_one(tb)) { /* div(x, 1) == x */
1515 switch (get_Proj_proj(proj)) {
1517 proj = get_Div_mem(div);
1518 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NEUTRAL_1);
1522 proj = get_Div_left(div);
1523 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NEUTRAL_1);
1527 /* we cannot replace the exception Proj's here, this is done in
1528 transform_node_Proj_Div() */
1533 } /* equivalent_node_Proj_Div */
1536 * Optimize a / 1.0 = a.
1538 static ir_node *equivalent_node_Proj_Quot(ir_node *proj) {
1539 ir_node *oldn = proj;
1540 ir_node *quot = get_Proj_pred(proj);
1541 ir_node *b = get_Quot_right(quot);
1542 tarval *tb = value_of(b);
1544 /* Div is not commutative. */
1545 if (tarval_is_one(tb)) { /* Quot(x, 1) == x */
1546 switch (get_Proj_proj(proj)) {
1548 proj = get_Quot_mem(quot);
1549 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NEUTRAL_1);
1553 proj = get_Quot_left(quot);
1554 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NEUTRAL_1);
1558 /* we cannot replace the exception Proj's here, this is done in
1559 transform_node_Proj_Quot() */
1564 } /* equivalent_node_Proj_Quot */
1567 * Optimize a / 1 = a.
1569 static ir_node *equivalent_node_Proj_DivMod(ir_node *proj) {
1570 ir_node *oldn = proj;
1571 ir_node *divmod = get_Proj_pred(proj);
1572 ir_node *b = get_DivMod_right(divmod);
1573 tarval *tb = value_of(b);
1575 /* Div is not commutative. */
1576 if (tarval_is_one(tb)) { /* div(x, 1) == x */
1577 switch (get_Proj_proj(proj)) {
1579 proj = get_DivMod_mem(divmod);
1580 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NEUTRAL_1);
1583 case pn_DivMod_res_div:
1584 proj = get_DivMod_left(divmod);
1585 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NEUTRAL_1);
1589 /* we cannot replace the exception Proj's here, this is done in
1590 transform_node_Proj_DivMod().
1591 Note further that the pn_DivMod_res_div case is handled in
1592 computed_value_Proj(). */
1597 } /* equivalent_node_Proj_DivMod */
1600 * Optimize CopyB(mem, x, x) into a Nop.
1602 static ir_node *equivalent_node_Proj_CopyB(ir_node *proj) {
1603 ir_node *oldn = proj;
1604 ir_node *copyb = get_Proj_pred(proj);
1605 ir_node *a = get_CopyB_dst(copyb);
1606 ir_node *b = get_CopyB_src(copyb);
1609 /* Turn CopyB into a tuple (mem, jmp, bad, bad) */
1610 switch (get_Proj_proj(proj)) {
1611 case pn_CopyB_M_regular:
1612 proj = get_CopyB_mem(copyb);
1613 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NOP);
1616 case pn_CopyB_X_except:
1617 DBG_OPT_EXC_REM(proj);
1618 proj = get_irg_bad(current_ir_graph);
1623 } /* equivalent_node_Proj_CopyB */
1626 * Optimize Bounds(idx, idx, upper) into idx.
1628 static ir_node *equivalent_node_Proj_Bound(ir_node *proj) {
1629 ir_node *oldn = proj;
1630 ir_node *bound = get_Proj_pred(proj);
1631 ir_node *idx = get_Bound_index(bound);
1632 ir_node *pred = skip_Proj(idx);
1635 if (idx == get_Bound_lower(bound))
1637 else if (is_Bound(pred)) {
1639 * idx was Bounds checked in the same MacroBlock previously,
1640 * it is still valid if lower <= pred_lower && pred_upper <= upper.
1642 ir_node *lower = get_Bound_lower(bound);
1643 ir_node *upper = get_Bound_upper(bound);
1644 if (get_Bound_lower(pred) == lower &&
1645 get_Bound_upper(pred) == upper &&
1646 get_irn_MacroBlock(bound) == get_irn_MacroBlock(pred)) {
1648 * One could expect that we simply return the previous
1649 * Bound here. However, this would be wrong, as we could
1650 * add an exception Proj to a new location then.
1651 * So, we must turn in into a tuple.
1657 /* Turn Bound into a tuple (mem, jmp, bad, idx) */
1658 switch (get_Proj_proj(proj)) {
1660 DBG_OPT_EXC_REM(proj);
1661 proj = get_Bound_mem(bound);
1663 case pn_Bound_X_except:
1664 DBG_OPT_EXC_REM(proj);
1665 proj = get_irg_bad(current_ir_graph);
1669 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NOP);
1672 /* cannot optimize pn_Bound_X_regular, handled in transform ... */
1677 } /* equivalent_node_Proj_Bound */
1680 * Optimize an Exception Proj(Load) with a non-null address.
1682 static ir_node *equivalent_node_Proj_Load(ir_node *proj) {
1683 if (get_opt_ldst_only_null_ptr_exceptions()) {
1684 if (get_irn_mode(proj) == mode_X) {
1685 ir_node *load = get_Proj_pred(proj);
1687 /* get the Load address */
1688 const ir_node *addr = get_Load_ptr(load);
1689 const ir_node *confirm;
1691 if (value_not_null(addr, &confirm)) {
1692 if (get_Proj_proj(proj) == pn_Load_X_except) {
1693 DBG_OPT_EXC_REM(proj);
1694 return get_irg_bad(current_ir_graph);
1700 } /* equivalent_node_Proj_Load */
1703 * Optimize an Exception Proj(Store) with a non-null address.
1705 static ir_node *equivalent_node_Proj_Store(ir_node *proj) {
1706 if (get_opt_ldst_only_null_ptr_exceptions()) {
1707 if (get_irn_mode(proj) == mode_X) {
1708 ir_node *store = get_Proj_pred(proj);
1710 /* get the load/store address */
1711 const ir_node *addr = get_Store_ptr(store);
1712 const ir_node *confirm;
1714 if (value_not_null(addr, &confirm)) {
1715 if (get_Proj_proj(proj) == pn_Store_X_except) {
1716 DBG_OPT_EXC_REM(proj);
1717 return get_irg_bad(current_ir_graph);
1723 } /* equivalent_node_Proj_Store */
1726 * Does all optimizations on nodes that must be done on it's Proj's
1727 * because of creating new nodes.
1729 static ir_node *equivalent_node_Proj(ir_node *proj) {
1730 ir_node *n = get_Proj_pred(proj);
1732 if (get_irn_mode(proj) == mode_X) {
1733 if (is_Block_dead(get_nodes_block(n))) {
1734 /* Remove dead control flow -- early gigo(). */
1735 return get_irg_bad(current_ir_graph);
1738 if (n->op->ops.equivalent_node_Proj)
1739 return n->op->ops.equivalent_node_Proj(proj);
1741 } /* equivalent_node_Proj */
1746 static ir_node *equivalent_node_Id(ir_node *n) {
1753 DBG_OPT_ID(oldn, n);
1755 } /* equivalent_node_Id */
1760 static ir_node *equivalent_node_Mux(ir_node *n)
1762 ir_node *oldn = n, *sel = get_Mux_sel(n);
1764 tarval *ts = value_of(sel);
1766 /* Mux(true, f, t) == t */
1767 if (ts == tarval_b_true) {
1768 n = get_Mux_true(n);
1769 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_C);
1772 /* Mux(false, f, t) == f */
1773 if (ts == tarval_b_false) {
1774 n = get_Mux_false(n);
1775 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_C);
1778 n_t = get_Mux_true(n);
1779 n_f = get_Mux_false(n);
1781 /* Mux(v, x, T) == x */
1782 if (is_Unknown(n_f)) {
1784 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_EQ);
1787 /* Mux(v, T, x) == x */
1788 if (is_Unknown(n_t)) {
1790 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_EQ);
1794 /* Mux(v, x, x) == x */
1797 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_EQ);
1800 if (is_Proj(sel) && !mode_honor_signed_zeros(get_irn_mode(n))) {
1801 ir_node *cmp = get_Proj_pred(sel);
1802 long proj_nr = get_Proj_proj(sel);
1803 ir_node *f = get_Mux_false(n);
1804 ir_node *t = get_Mux_true(n);
1807 * Note further that these optimization work even for floating point
1808 * with NaN's because -NaN == NaN.
1809 * However, if +0 and -0 is handled differently, we cannot use the first one.
1812 ir_node *const cmp_l = get_Cmp_left(cmp);
1813 ir_node *const cmp_r = get_Cmp_right(cmp);
1817 if ((cmp_l == t && cmp_r == f) || /* Mux(t == f, t, f) -> f */
1818 (cmp_l == f && cmp_r == t)) { /* Mux(f == t, t, f) -> f */
1820 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1827 if ((cmp_l == t && cmp_r == f) || /* Mux(t != f, t, f) -> t */
1828 (cmp_l == f && cmp_r == t)) { /* Mux(f != t, t, f) -> t */
1830 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1837 * Note: normalization puts the constant on the right side,
1838 * so we check only one case.
1840 if (cmp_l == t && tarval_is_null(value_of(cmp_r))) {
1841 /* Mux(t CMP 0, X, t) */
1842 if (is_Minus(f) && get_Minus_op(f) == t) {
1843 /* Mux(t CMP 0, -t, t) */
1844 if (proj_nr == pn_Cmp_Eq) {
1845 /* Mux(t == 0, -t, t) ==> -t */
1847 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1848 } else if (proj_nr == pn_Cmp_Lg || proj_nr == pn_Cmp_Ne) {
1849 /* Mux(t != 0, -t, t) ==> t */
1851 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1858 } /* equivalent_node_Mux */
1861 * Remove Confirm nodes if setting is on.
1862 * Replace Confirms(x, '=', Constlike) by Constlike.
1864 static ir_node *equivalent_node_Confirm(ir_node *n) {
1865 ir_node *pred = get_Confirm_value(n);
1866 pn_Cmp pnc = get_Confirm_cmp(n);
1868 while (is_Confirm(pred) && pnc == get_Confirm_cmp(pred)) {
1870 * rare case: two identical Confirms one after another,
1871 * replace the second one with the first.
1874 pred = get_Confirm_value(n);
1875 pnc = get_Confirm_cmp(n);
1877 if (get_opt_remove_confirm())
1878 return get_Confirm_value(n);
1883 * equivalent_node() returns a node equivalent to input n. It skips all nodes that
1884 * perform no actual computation, as, e.g., the Id nodes. It does not create
1885 * new nodes. It is therefore safe to free n if the node returned is not n.
1886 * If a node returns a Tuple we can not just skip it. If the size of the
1887 * in array fits, we transform n into a tuple (e.g., Div).
1889 ir_node *equivalent_node(ir_node *n) {
1890 if (n->op->ops.equivalent_node)
1891 return n->op->ops.equivalent_node(n);
1893 } /* equivalent_node */
1896 * Sets the default equivalent node operation for an ir_op_ops.
1898 * @param code the opcode for the default operation
1899 * @param ops the operations initialized
1904 static ir_op_ops *firm_set_default_equivalent_node(ir_opcode code, ir_op_ops *ops)
1908 ops->equivalent_node = equivalent_node_##a; \
1910 #define CASE_PROJ(a) \
1912 ops->equivalent_node_Proj = equivalent_node_Proj_##a; \
1954 } /* firm_set_default_equivalent_node */
1957 * Returns non-zero if a node is a Phi node
1958 * with all predecessors constant.
1960 static int is_const_Phi(ir_node *n) {
1963 if (! is_Phi(n) || get_irn_arity(n) == 0)
1965 for (i = get_irn_arity(n) - 1; i >= 0; --i) {
1966 if (! is_Const(get_irn_n(n, i)))
1970 } /* is_const_Phi */
1972 typedef tarval *(*tarval_sub_type)(tarval *a, tarval *b, ir_mode *mode);
1973 typedef tarval *(*tarval_binop_type)(tarval *a, tarval *b);
1976 * in reality eval_func should be tarval (*eval_func)() but incomplete
1977 * declarations are bad style and generate noisy warnings
1979 typedef void (*eval_func)(void);
1982 * Wrapper for the tarval binop evaluation, tarval_sub has one more parameter.
1984 static tarval *do_eval(eval_func eval, tarval *a, tarval *b, ir_mode *mode)
1986 if (eval == (eval_func) tarval_sub) {
1987 tarval_sub_type func = (tarval_sub_type)eval;
1989 return func(a, b, mode);
1991 tarval_binop_type func = (tarval_binop_type)eval;
1998 * Apply an evaluator on a binop with a constant operators (and one Phi).
2000 * @param phi the Phi node
2001 * @param other the other operand
2002 * @param eval an evaluator function
2003 * @param mode the mode of the result, may be different from the mode of the Phi!
2004 * @param left if non-zero, other is the left operand, else the right
2006 * @return a new Phi node if the conversion was successful, NULL else
2008 static ir_node *apply_binop_on_phi(ir_node *phi, tarval *other, eval_func eval, ir_mode *mode, int left) {
2013 int i, n = get_irn_arity(phi);
2015 NEW_ARR_A(void *, res, n);
2017 for (i = 0; i < n; ++i) {
2018 pred = get_irn_n(phi, i);
2019 tv = get_Const_tarval(pred);
2020 tv = do_eval(eval, other, tv, mode);
2022 if (tv == tarval_bad) {
2023 /* folding failed, bad */
2029 for (i = 0; i < n; ++i) {
2030 pred = get_irn_n(phi, i);
2031 tv = get_Const_tarval(pred);
2032 tv = do_eval(eval, tv, other, mode);
2034 if (tv == tarval_bad) {
2035 /* folding failed, bad */
2041 irg = current_ir_graph;
2042 for (i = 0; i < n; ++i) {
2043 pred = get_irn_n(phi, i);
2044 res[i] = new_r_Const_type(irg, res[i], get_Const_type(pred));
2046 return new_r_Phi(get_nodes_block(phi), n, (ir_node **)res, mode);
2047 } /* apply_binop_on_phi */
2050 * Apply an evaluator on a binop with two constant Phi.
2052 * @param a the left Phi node
2053 * @param b the right Phi node
2054 * @param eval an evaluator function
2055 * @param mode the mode of the result, may be different from the mode of the Phi!
2057 * @return a new Phi node if the conversion was successful, NULL else
2059 static ir_node *apply_binop_on_2_phis(ir_node *a, ir_node *b, eval_func eval, ir_mode *mode) {
2060 tarval *tv_l, *tv_r, *tv;
2066 if (get_nodes_block(a) != get_nodes_block(b))
2069 n = get_irn_arity(a);
2070 NEW_ARR_A(void *, res, n);
2072 for (i = 0; i < n; ++i) {
2073 pred = get_irn_n(a, i);
2074 tv_l = get_Const_tarval(pred);
2075 pred = get_irn_n(b, i);
2076 tv_r = get_Const_tarval(pred);
2077 tv = do_eval(eval, tv_l, tv_r, mode);
2079 if (tv == tarval_bad) {
2080 /* folding failed, bad */
2085 irg = current_ir_graph;
2086 for (i = 0; i < n; ++i) {
2087 pred = get_irn_n(a, i);
2088 res[i] = new_r_Const_type(irg, res[i], get_Const_type(pred));
2090 return new_r_Phi(get_nodes_block(a), n, (ir_node **)res, mode);
2091 } /* apply_binop_on_2_phis */
2094 * Apply an evaluator on a unop with a constant operator (a Phi).
2096 * @param phi the Phi node
2097 * @param eval an evaluator function
2099 * @return a new Phi node if the conversion was successful, NULL else
2101 static ir_node *apply_unop_on_phi(ir_node *phi, tarval *(*eval)(tarval *)) {
2107 int i, n = get_irn_arity(phi);
2109 NEW_ARR_A(void *, res, n);
2110 for (i = 0; i < n; ++i) {
2111 pred = get_irn_n(phi, i);
2112 tv = get_Const_tarval(pred);
2115 if (tv == tarval_bad) {
2116 /* folding failed, bad */
2121 mode = get_irn_mode(phi);
2122 irg = current_ir_graph;
2123 for (i = 0; i < n; ++i) {
2124 pred = get_irn_n(phi, i);
2125 res[i] = new_r_Const_type(irg, res[i], get_Const_type(pred));
2127 return new_r_Phi(get_nodes_block(phi), n, (ir_node **)res, mode);
2128 } /* apply_unop_on_phi */
2131 * Apply a conversion on a constant operator (a Phi).
2133 * @param phi the Phi node
2135 * @return a new Phi node if the conversion was successful, NULL else
2137 static ir_node *apply_conv_on_phi(ir_node *phi, ir_mode *mode) {
2142 int i, n = get_irn_arity(phi);
2144 NEW_ARR_A(void *, res, n);
2145 for (i = 0; i < n; ++i) {
2146 pred = get_irn_n(phi, i);
2147 tv = get_Const_tarval(pred);
2148 tv = tarval_convert_to(tv, mode);
2150 if (tv == tarval_bad) {
2151 /* folding failed, bad */
2156 irg = current_ir_graph;
2157 for (i = 0; i < n; ++i) {
2158 pred = get_irn_n(phi, i);
2159 res[i] = new_r_Const_type(irg, res[i], get_Const_type(pred));
2161 return new_r_Phi(get_nodes_block(phi), n, (ir_node **)res, mode);
2162 } /* apply_conv_on_phi */
2165 * Transform AddP(P, ConvIs(Iu)), AddP(P, ConvIu(Is)) and
2166 * SubP(P, ConvIs(Iu)), SubP(P, ConvIu(Is)).
2167 * If possible, remove the Conv's.
2169 static ir_node *transform_node_AddSub(ir_node *n) {
2170 ir_mode *mode = get_irn_mode(n);
2172 if (mode_is_reference(mode)) {
2173 ir_node *left = get_binop_left(n);
2174 ir_node *right = get_binop_right(n);
2175 unsigned ref_bits = get_mode_size_bits(mode);
2177 if (is_Conv(left)) {
2178 ir_mode *lmode = get_irn_mode(left);
2179 unsigned bits = get_mode_size_bits(lmode);
2181 if (ref_bits == bits &&
2182 mode_is_int(lmode) &&
2183 get_mode_arithmetic(lmode) == irma_twos_complement) {
2184 ir_node *pre = get_Conv_op(left);
2185 ir_mode *pre_mode = get_irn_mode(pre);
2187 if (mode_is_int(pre_mode) &&
2188 get_mode_size_bits(pre_mode) == bits &&
2189 get_mode_arithmetic(pre_mode) == irma_twos_complement) {
2190 /* ok, this conv just changes to sign, moreover the calculation
2191 * is done with same number of bits as our address mode, so
2192 * we can ignore the conv as address calculation can be viewed
2193 * as either signed or unsigned
2195 set_binop_left(n, pre);
2200 if (is_Conv(right)) {
2201 ir_mode *rmode = get_irn_mode(right);
2202 unsigned bits = get_mode_size_bits(rmode);
2204 if (ref_bits == bits &&
2205 mode_is_int(rmode) &&
2206 get_mode_arithmetic(rmode) == irma_twos_complement) {
2207 ir_node *pre = get_Conv_op(right);
2208 ir_mode *pre_mode = get_irn_mode(pre);
2210 if (mode_is_int(pre_mode) &&
2211 get_mode_size_bits(pre_mode) == bits &&
2212 get_mode_arithmetic(pre_mode) == irma_twos_complement) {
2213 /* ok, this conv just changes to sign, moreover the calculation
2214 * is done with same number of bits as our address mode, so
2215 * we can ignore the conv as address calculation can be viewed
2216 * as either signed or unsigned
2218 set_binop_right(n, pre);
2223 /* let address arithmetic use unsigned modes */
2224 if (is_Const(right)) {
2225 ir_mode *rmode = get_irn_mode(right);
2227 if (mode_is_signed(rmode) && get_mode_arithmetic(rmode) == irma_twos_complement) {
2228 /* convert a AddP(P, *s) into AddP(P, *u) */
2229 ir_mode *nm = get_reference_mode_unsigned_eq(mode);
2231 ir_node *pre = new_r_Conv(get_nodes_block(n), right, nm);
2232 set_binop_right(n, pre);
2238 } /* transform_node_AddSub */
2240 #define HANDLE_BINOP_PHI(eval, a, b, c, mode) \
2242 if (is_Const(b) && is_const_Phi(a)) { \
2243 /* check for Op(Phi, Const) */ \
2244 c = apply_binop_on_phi(a, get_Const_tarval(b), eval, mode, 0);\
2246 else if (is_Const(a) && is_const_Phi(b)) { \
2247 /* check for Op(Const, Phi) */ \
2248 c = apply_binop_on_phi(b, get_Const_tarval(a), eval, mode, 1);\
2250 else if (is_const_Phi(a) && is_const_Phi(b)) { \
2251 /* check for Op(Phi, Phi) */ \
2252 c = apply_binop_on_2_phis(a, b, eval, mode); \
2255 DBG_OPT_ALGSIM0(oldn, c, FS_OPT_CONST_PHI); \
2259 #define HANDLE_UNOP_PHI(eval, a, c) \
2261 if (is_const_Phi(a)) { \
2262 /* check for Op(Phi) */ \
2263 c = apply_unop_on_phi(a, eval); \
2265 DBG_OPT_ALGSIM0(oldn, c, FS_OPT_CONST_PHI); \
2271 * Do the AddSub optimization, then Transform
2272 * Constant folding on Phi
2273 * Add(a,a) -> Mul(a, 2)
2274 * Add(Mul(a, x), a) -> Mul(a, x+1)
2275 * if the mode is integer or float.
2276 * Transform Add(a,-b) into Sub(a,b).
2277 * Reassociation might fold this further.
2279 static ir_node *transform_node_Add(ir_node *n) {
2281 ir_node *a, *b, *c, *oldn = n;
2283 n = transform_node_AddSub(n);
2285 a = get_Add_left(n);
2286 b = get_Add_right(n);
2288 mode = get_irn_mode(n);
2290 if (mode_is_reference(mode)) {
2291 ir_mode *lmode = get_irn_mode(a);
2293 if (is_Const(b) && is_Const_null(b) && mode_is_int(lmode)) {
2294 /* an Add(a, NULL) is a hidden Conv */
2295 dbg_info *dbg = get_irn_dbg_info(n);
2296 return new_rd_Conv(dbg, get_nodes_block(n), a, mode);
2300 HANDLE_BINOP_PHI((eval_func) tarval_add, a, b, c, mode);
2302 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
2303 if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
2306 if (mode_is_num(mode)) {
2307 /* the following code leads to endless recursion when Mul are replaced by a simple instruction chain */
2308 if (!is_irg_state(current_ir_graph, IR_GRAPH_STATE_ARCH_DEP)
2309 && a == b && mode_is_int(mode)) {
2310 ir_node *block = get_nodes_block(n);
2313 get_irn_dbg_info(n),
2316 new_Const_long(mode, 2),
2318 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_A);
2323 get_irn_dbg_info(n),
2328 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_MINUS_B);
2333 get_irn_dbg_info(n),
2338 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_MINUS_B);
2341 if (get_mode_arithmetic(mode) == irma_twos_complement) {
2342 /* Here we rely on constants be on the RIGHT side */
2344 ir_node *op = get_Not_op(a);
2346 if (is_Const(b) && is_Const_one(b)) {
2348 ir_node *blk = get_nodes_block(n);
2349 n = new_rd_Minus(get_irn_dbg_info(n), blk, op, mode);
2350 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_PLUS_1);
2355 n = new_Const(get_mode_minus_one(mode));
2356 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_X_NOT_X);
2361 ir_node *op = get_Not_op(b);
2365 n = new_Const(get_mode_minus_one(mode));
2366 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_X_NOT_X);
2372 if (mode_is_int(mode) && a->vrp.valid && b->vrp.valid) {
2373 tarval *c = tarval_and(
2374 tarval_not(a->vrp.bits_not_set),
2375 tarval_not(b->vrp.bits_not_set)
2378 if(tarval_is_null(c)) {
2379 dbg_info *dbgi = get_irn_dbg_info(n);
2380 return new_rd_Or(dbgi, get_nodes_block(n),
2385 } /* transform_node_Add */
2388 * returns -cnst or NULL if impossible
2390 static ir_node *const_negate(ir_node *cnst) {
2391 tarval *tv = tarval_neg(get_Const_tarval(cnst));
2392 dbg_info *dbgi = get_irn_dbg_info(cnst);
2393 ir_graph *irg = get_irn_irg(cnst);
2394 if (tv == tarval_bad) return NULL;
2395 return new_rd_Const(dbgi, irg, tv);
2399 * Do the AddSub optimization, then Transform
2400 * Constant folding on Phi
2401 * Sub(0,a) -> Minus(a)
2402 * Sub(Mul(a, x), a) -> Mul(a, x-1)
2403 * Sub(Sub(x, y), b) -> Sub(x, Add(y,b))
2404 * Sub(Add(a, x), x) -> a
2405 * Sub(x, Add(x, a)) -> -a
2406 * Sub(x, Const) -> Add(x, -Const)
2408 static ir_node *transform_node_Sub(ir_node *n) {
2413 n = transform_node_AddSub(n);
2415 a = get_Sub_left(n);
2416 b = get_Sub_right(n);
2418 mode = get_irn_mode(n);
2420 if (mode_is_int(mode)) {
2421 ir_mode *lmode = get_irn_mode(a);
2423 if (is_Const(b) && is_Const_null(b) && mode_is_reference(lmode)) {
2424 /* a Sub(a, NULL) is a hidden Conv */
2425 dbg_info *dbg = get_irn_dbg_info(n);
2426 n = new_rd_Conv(dbg, get_nodes_block(n), a, mode);
2427 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_CONV);
2431 if (mode == lmode &&
2432 get_mode_arithmetic(mode) == irma_twos_complement &&
2434 get_Const_tarval(a) == get_mode_minus_one(mode)) {
2436 dbg_info *dbg = get_irn_dbg_info(n);
2437 n = new_rd_Not(dbg, get_nodes_block(n), b, mode);
2438 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_NOT);
2444 HANDLE_BINOP_PHI((eval_func) tarval_sub, a, b, c, mode);
2446 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
2447 if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
2450 if (is_Const(b) && !mode_is_reference(get_irn_mode(b))) {
2451 /* a - C -> a + (-C) */
2452 ir_node *cnst = const_negate(b);
2454 ir_node *block = get_nodes_block(n);
2455 dbg_info *dbgi = get_irn_dbg_info(n);
2457 n = new_rd_Add(dbgi, block, a, cnst, mode);
2458 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2463 if (is_Minus(a)) { /* (-a) - b -> -(a + b) */
2464 dbg_info *dbg = get_irn_dbg_info(n);
2465 ir_node *block = get_nodes_block(n);
2466 ir_node *left = get_Minus_op(a);
2467 ir_node *add = new_rd_Add(dbg, block, left, b, mode);
2469 n = new_rd_Minus(dbg, block, add, mode);
2470 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2472 } else if (is_Minus(b)) { /* a - (-b) -> a + b */
2473 dbg_info *dbg = get_irn_dbg_info(n);
2474 ir_node *block = get_nodes_block(n);
2475 ir_node *right = get_Minus_op(b);
2477 n = new_rd_Add(dbg, block, a, right, mode);
2478 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MINUS);
2480 } else if (is_Sub(b)) {
2481 /* a - (b - c) -> a + (c - b)
2482 * -> (a - b) + c iff (b - c) is a pointer */
2483 dbg_info *s_dbg = get_irn_dbg_info(b);
2484 ir_node *s_block = get_nodes_block(b);
2485 ir_node *s_left = get_Sub_left(b);
2486 ir_node *s_right = get_Sub_right(b);
2487 ir_mode *s_mode = get_irn_mode(b);
2488 if (mode_is_reference(s_mode)) {
2489 ir_node *sub = new_rd_Sub(s_dbg, s_block, a, s_left, mode);
2490 dbg_info *a_dbg = get_irn_dbg_info(n);
2491 ir_node *a_block = get_nodes_block(n);
2494 s_right = new_r_Conv(a_block, s_right, mode);
2495 n = new_rd_Add(a_dbg, a_block, sub, s_right, mode);
2497 ir_node *sub = new_rd_Sub(s_dbg, s_block, s_right, s_left, s_mode);
2498 dbg_info *a_dbg = get_irn_dbg_info(n);
2499 ir_node *a_block = get_nodes_block(n);
2501 n = new_rd_Add(a_dbg, a_block, a, sub, mode);
2503 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2505 } else if (is_Mul(b)) { /* a - (b * C) -> a + (b * -C) */
2506 ir_node *m_right = get_Mul_right(b);
2507 if (is_Const(m_right)) {
2508 ir_node *cnst2 = const_negate(m_right);
2509 if (cnst2 != NULL) {
2510 dbg_info *m_dbg = get_irn_dbg_info(b);
2511 ir_node *m_block = get_nodes_block(b);
2512 ir_node *m_left = get_Mul_left(b);
2513 ir_mode *m_mode = get_irn_mode(b);
2514 ir_node *mul = new_rd_Mul(m_dbg, m_block, m_left, cnst2, m_mode);
2515 dbg_info *a_dbg = get_irn_dbg_info(n);
2516 ir_node *a_block = get_nodes_block(n);
2518 n = new_rd_Add(a_dbg, a_block, a, mul, mode);
2519 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2525 /* Beware of Sub(P, P) which cannot be optimized into a simple Minus ... */
2526 if (mode_is_num(mode) && mode == get_irn_mode(a) && is_Const(a) && is_Const_null(a)) {
2528 get_irn_dbg_info(n),
2532 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_0_A);
2536 if (mode_wrap_around(mode)) {
2537 ir_node *left = get_Add_left(a);
2538 ir_node *right = get_Add_right(a);
2540 /* FIXME: Does the Conv's work only for two complement or generally? */
2542 if (mode != get_irn_mode(right)) {
2543 /* This Sub is an effective Cast */
2544 right = new_r_Conv(get_nodes_block(n), right, mode);
2547 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2549 } else if (right == b) {
2550 if (mode != get_irn_mode(left)) {
2551 /* This Sub is an effective Cast */
2552 left = new_r_Conv(get_nodes_block(n), left, mode);
2555 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2561 if (mode_wrap_around(mode)) {
2562 ir_node *left = get_Add_left(b);
2563 ir_node *right = get_Add_right(b);
2565 /* FIXME: Does the Conv's work only for two complement or generally? */
2567 ir_mode *r_mode = get_irn_mode(right);
2569 n = new_r_Minus(get_nodes_block(n), right, r_mode);
2570 if (mode != r_mode) {
2571 /* This Sub is an effective Cast */
2572 n = new_r_Conv(get_nodes_block(n), n, mode);
2574 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2576 } else if (right == a) {
2577 ir_mode *l_mode = get_irn_mode(left);
2579 n = new_r_Minus(get_nodes_block(n), left, l_mode);
2580 if (mode != l_mode) {
2581 /* This Sub is an effective Cast */
2582 n = new_r_Conv(get_nodes_block(n), n, mode);
2584 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2589 if (mode_is_int(mode) && is_Conv(a) && is_Conv(b)) {
2590 ir_mode *mode = get_irn_mode(a);
2592 if (mode == get_irn_mode(b)) {
2594 ir_node *op_a = get_Conv_op(a);
2595 ir_node *op_b = get_Conv_op(b);
2597 /* check if it's allowed to skip the conv */
2598 ma = get_irn_mode(op_a);
2599 mb = get_irn_mode(op_b);
2601 if (mode_is_reference(ma) && mode_is_reference(mb)) {
2602 /* SubInt(ConvInt(aP), ConvInt(bP)) -> SubInt(aP,bP) */
2605 set_Sub_right(n, b);
2611 /* do NOT execute this code if reassociation is enabled, it does the inverse! */
2612 if (!is_reassoc_running() && is_Mul(a)) {
2613 ir_node *ma = get_Mul_left(a);
2614 ir_node *mb = get_Mul_right(a);
2617 ir_node *blk = get_nodes_block(n);
2619 get_irn_dbg_info(n),
2623 get_irn_dbg_info(n),
2626 new_Const_long(mode, 1),
2629 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MUL_A_X_A);
2631 } else if (mb == b) {
2632 ir_node *blk = get_nodes_block(n);
2634 get_irn_dbg_info(n),
2638 get_irn_dbg_info(n),
2641 new_Const_long(mode, 1),
2644 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MUL_A_X_A);
2648 if (is_Sub(a)) { /* (x - y) - b -> x - (y + b) */
2649 ir_node *x = get_Sub_left(a);
2650 ir_node *y = get_Sub_right(a);
2651 ir_node *blk = get_nodes_block(n);
2652 ir_mode *m_b = get_irn_mode(b);
2653 ir_mode *m_y = get_irn_mode(y);
2657 /* Determine the right mode for the Add. */
2660 else if (mode_is_reference(m_b))
2662 else if (mode_is_reference(m_y))
2666 * Both modes are different but none is reference,
2667 * happens for instance in SubP(SubP(P, Iu), Is).
2668 * We have two possibilities here: Cast or ignore.
2669 * Currently we ignore this case.
2674 add = new_r_Add(blk, y, b, add_mode);
2676 n = new_rd_Sub(get_irn_dbg_info(n), blk, x, add, mode);
2677 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_SUB_X_Y_Z);
2681 if (get_mode_arithmetic(mode) == irma_twos_complement) {
2682 if (is_Const(a) && is_Not(b)) {
2683 /* c - ~X = X + (c+1) */
2684 tarval *tv = get_Const_tarval(a);
2686 tv = tarval_add(tv, get_mode_one(mode));
2687 if (tv != tarval_bad) {
2688 ir_node *blk = get_nodes_block(n);
2689 ir_node *c = new_Const(tv);
2690 n = new_rd_Add(get_irn_dbg_info(n), blk, get_Not_op(b), c, mode);
2691 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_C_NOT_X);
2697 } /* transform_node_Sub */
2700 * Several transformation done on n*n=2n bits mul.
2701 * These transformations must be done here because new nodes may be produced.
2703 static ir_node *transform_node_Mul2n(ir_node *n, ir_mode *mode) {
2705 ir_node *a = get_Mul_left(n);
2706 ir_node *b = get_Mul_right(n);
2707 tarval *ta = value_of(a);
2708 tarval *tb = value_of(b);
2709 ir_mode *smode = get_irn_mode(a);
2711 if (ta == get_mode_one(smode)) {
2712 /* (L)1 * (L)b = (L)b */
2713 ir_node *blk = get_nodes_block(n);
2714 n = new_rd_Conv(get_irn_dbg_info(n), blk, b, mode);
2715 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
2718 else if (ta == get_mode_minus_one(smode)) {
2719 /* (L)-1 * (L)b = (L)b */
2720 ir_node *blk = get_nodes_block(n);
2721 n = new_rd_Minus(get_irn_dbg_info(n), blk, b, smode);
2722 n = new_rd_Conv(get_irn_dbg_info(n), blk, n, mode);
2723 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2726 if (tb == get_mode_one(smode)) {
2727 /* (L)a * (L)1 = (L)a */
2728 ir_node *blk = get_irn_n(a, -1);
2729 n = new_rd_Conv(get_irn_dbg_info(n), blk, a, mode);
2730 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
2733 else if (tb == get_mode_minus_one(smode)) {
2734 /* (L)a * (L)-1 = (L)-a */
2735 ir_node *blk = get_nodes_block(n);
2736 n = new_rd_Minus(get_irn_dbg_info(n), blk, a, smode);
2737 n = new_rd_Conv(get_irn_dbg_info(n), blk, n, mode);
2738 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2745 * Transform Mul(a,-1) into -a.
2746 * Do constant evaluation of Phi nodes.
2747 * Do architecture dependent optimizations on Mul nodes
2749 static ir_node *transform_node_Mul(ir_node *n) {
2750 ir_node *c, *oldn = n;
2751 ir_mode *mode = get_irn_mode(n);
2752 ir_node *a = get_Mul_left(n);
2753 ir_node *b = get_Mul_right(n);
2755 if (is_Bad(a) || is_Bad(b))
2758 if (mode != get_irn_mode(a))
2759 return transform_node_Mul2n(n, mode);
2761 HANDLE_BINOP_PHI((eval_func) tarval_mul, a, b, c, mode);
2763 if (mode_is_signed(mode)) {
2766 if (value_of(a) == get_mode_minus_one(mode))
2768 else if (value_of(b) == get_mode_minus_one(mode))
2771 n = new_rd_Minus(get_irn_dbg_info(n), get_nodes_block(n), r, mode);
2772 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2777 if (is_Const(b)) { /* (-a) * const -> a * -const */
2778 ir_node *cnst = const_negate(b);
2780 dbg_info *dbgi = get_irn_dbg_info(n);
2781 ir_node *block = get_nodes_block(n);
2782 n = new_rd_Mul(dbgi, block, get_Minus_op(a), cnst, mode);
2783 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2786 } else if (is_Minus(b)) { /* (-a) * (-b) -> a * b */
2787 dbg_info *dbgi = get_irn_dbg_info(n);
2788 ir_node *block = get_nodes_block(n);
2789 n = new_rd_Mul(dbgi, block, get_Minus_op(a), get_Minus_op(b), mode);
2790 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_MINUS);
2792 } else if (is_Sub(b)) { /* (-a) * (b - c) -> a * (c - b) */
2793 ir_node *sub_l = get_Sub_left(b);
2794 ir_node *sub_r = get_Sub_right(b);
2795 dbg_info *dbgi = get_irn_dbg_info(n);
2796 ir_node *block = get_nodes_block(n);
2797 ir_node *new_b = new_rd_Sub(dbgi, block, sub_r, sub_l, mode);
2798 n = new_rd_Mul(dbgi, block, get_Minus_op(a), new_b, mode);
2799 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS);
2802 } else if (is_Minus(b)) {
2803 if (is_Sub(a)) { /* (a - b) * (-c) -> (b - a) * c */
2804 ir_node *sub_l = get_Sub_left(a);
2805 ir_node *sub_r = get_Sub_right(a);
2806 dbg_info *dbgi = get_irn_dbg_info(n);
2807 ir_node *block = get_nodes_block(n);
2808 ir_node *new_a = new_rd_Sub(dbgi, block, sub_r, sub_l, mode);
2809 n = new_rd_Mul(dbgi, block, new_a, get_Minus_op(b), mode);
2810 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS);
2813 } else if (is_Shl(a)) {
2814 ir_node *const shl_l = get_Shl_left(a);
2815 if (is_Const(shl_l) && is_Const_one(shl_l)) {
2816 /* (1 << x) * b -> b << x */
2817 dbg_info *const dbgi = get_irn_dbg_info(n);
2818 ir_node *const block = get_nodes_block(n);
2819 ir_node *const shl_r = get_Shl_right(a);
2820 n = new_rd_Shl(dbgi, block, b, shl_r, mode);
2821 // TODO add me DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_SHIFT);
2824 } else if (is_Shl(b)) {
2825 ir_node *const shl_l = get_Shl_left(b);
2826 if (is_Const(shl_l) && is_Const_one(shl_l)) {
2827 /* a * (1 << x) -> a << x */
2828 dbg_info *const dbgi = get_irn_dbg_info(n);
2829 ir_node *const block = get_nodes_block(n);
2830 ir_node *const shl_r = get_Shl_right(b);
2831 n = new_rd_Shl(dbgi, block, a, shl_r, mode);
2832 // TODO add me DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_SHIFT);
2836 if (get_mode_arithmetic(mode) == irma_ieee754) {
2838 tarval *tv = get_Const_tarval(a);
2839 if (tarval_ieee754_get_exponent(tv) == 1 && tarval_ieee754_zero_mantissa(tv)
2840 && !tarval_is_negative(tv)) {
2841 /* 2.0 * b = b + b */
2842 n = new_rd_Add(get_irn_dbg_info(n), get_nodes_block(n), b, b, mode);
2843 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_A_A);
2847 else if (is_Const(b)) {
2848 tarval *tv = get_Const_tarval(b);
2849 if (tarval_ieee754_get_exponent(tv) == 1 && tarval_ieee754_zero_mantissa(tv)
2850 && !tarval_is_negative(tv)) {
2851 /* a * 2.0 = a + a */
2852 n = new_rd_Add(get_irn_dbg_info(n), get_nodes_block(n), a, a, mode);
2853 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_A_A);
2858 return arch_dep_replace_mul_with_shifts(n);
2859 } /* transform_node_Mul */
2862 * Transform a Div Node.
2864 static ir_node *transform_node_Div(ir_node *n) {
2865 ir_mode *mode = get_Div_resmode(n);
2866 ir_node *a = get_Div_left(n);
2867 ir_node *b = get_Div_right(n);
2869 const ir_node *dummy;
2871 if (is_Const(b) && is_const_Phi(a)) {
2872 /* check for Div(Phi, Const) */
2873 value = apply_binop_on_phi(a, get_Const_tarval(b), (eval_func) tarval_div, mode, 0);
2875 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2879 else if (is_Const(a) && is_const_Phi(b)) {
2880 /* check for Div(Const, Phi) */
2881 value = apply_binop_on_phi(b, get_Const_tarval(a), (eval_func) tarval_div, mode, 1);
2883 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2887 else if (is_const_Phi(a) && is_const_Phi(b)) {
2888 /* check for Div(Phi, Phi) */
2889 value = apply_binop_on_2_phis(a, b, (eval_func) tarval_div, mode);
2891 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2898 if (a == b && value_not_zero(a, &dummy)) {
2899 /* BEWARE: we can optimize a/a to 1 only if this cannot cause a exception */
2900 value = new_Const(get_mode_one(mode));
2901 DBG_OPT_CSTEVAL(n, value);
2904 if (mode_is_signed(mode) && is_Const(b)) {
2905 tarval *tv = get_Const_tarval(b);
2907 if (tv == get_mode_minus_one(mode)) {
2909 value = new_rd_Minus(get_irn_dbg_info(n), get_nodes_block(n), a, mode);
2910 DBG_OPT_CSTEVAL(n, value);
2914 /* Try architecture dependent optimization */
2915 value = arch_dep_replace_div_by_const(n);
2922 /* Turn Div into a tuple (mem, jmp, bad, value) */
2923 mem = get_Div_mem(n);
2924 blk = get_nodes_block(n);
2926 /* skip a potential Pin */
2927 mem = skip_Pin(mem);
2928 turn_into_tuple(n, pn_Div_max);
2929 set_Tuple_pred(n, pn_Div_M, mem);
2930 set_Tuple_pred(n, pn_Div_X_regular, new_r_Jmp(blk));
2931 set_Tuple_pred(n, pn_Div_X_except, new_Bad());
2932 set_Tuple_pred(n, pn_Div_res, value);
2935 } /* transform_node_Div */
2938 * Transform a Mod node.
2940 static ir_node *transform_node_Mod(ir_node *n) {
2941 ir_mode *mode = get_Mod_resmode(n);
2942 ir_node *a = get_Mod_left(n);
2943 ir_node *b = get_Mod_right(n);
2947 if (is_Const(b) && is_const_Phi(a)) {
2948 /* check for Div(Phi, Const) */
2949 value = apply_binop_on_phi(a, get_Const_tarval(b), (eval_func) tarval_mod, mode, 0);
2951 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2955 else if (is_Const(a) && is_const_Phi(b)) {
2956 /* check for Div(Const, Phi) */
2957 value = apply_binop_on_phi(b, get_Const_tarval(a), (eval_func) tarval_mod, mode, 1);
2959 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2963 else if (is_const_Phi(a) && is_const_Phi(b)) {
2964 /* check for Div(Phi, Phi) */
2965 value = apply_binop_on_2_phis(a, b, (eval_func) tarval_mod, mode);
2967 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2974 if (tv != tarval_bad) {
2975 value = new_Const(tv);
2977 DBG_OPT_CSTEVAL(n, value);
2980 ir_node *a = get_Mod_left(n);
2981 ir_node *b = get_Mod_right(n);
2982 const ir_node *dummy;
2984 if (a == b && value_not_zero(a, &dummy)) {
2985 /* BEWARE: we can optimize a%a to 0 only if this cannot cause a exception */
2986 value = new_Const(get_mode_null(mode));
2987 DBG_OPT_CSTEVAL(n, value);
2990 if (mode_is_signed(mode) && is_Const(b)) {
2991 tarval *tv = get_Const_tarval(b);
2993 if (tv == get_mode_minus_one(mode)) {
2995 value = new_Const(get_mode_null(mode));
2996 DBG_OPT_CSTEVAL(n, value);
3000 /* Try architecture dependent optimization */
3001 value = arch_dep_replace_mod_by_const(n);
3009 /* Turn Mod into a tuple (mem, jmp, bad, value) */
3010 mem = get_Mod_mem(n);
3011 blk = get_nodes_block(n);
3013 /* skip a potential Pin */
3014 mem = skip_Pin(mem);
3015 turn_into_tuple(n, pn_Mod_max);
3016 set_Tuple_pred(n, pn_Mod_M, mem);
3017 set_Tuple_pred(n, pn_Mod_X_regular, new_r_Jmp(blk));
3018 set_Tuple_pred(n, pn_Mod_X_except, new_Bad());
3019 set_Tuple_pred(n, pn_Mod_res, value);
3022 } /* transform_node_Mod */
3025 * Transform a DivMod node.
3027 static ir_node *transform_node_DivMod(ir_node *n) {
3028 const ir_node *dummy;
3029 ir_node *a = get_DivMod_left(n);
3030 ir_node *b = get_DivMod_right(n);
3031 ir_mode *mode = get_DivMod_resmode(n);
3036 if (is_Const(b) && is_const_Phi(a)) {
3037 /* check for Div(Phi, Const) */
3038 va = apply_binop_on_phi(a, get_Const_tarval(b), (eval_func) tarval_div, mode, 0);
3039 vb = apply_binop_on_phi(a, get_Const_tarval(b), (eval_func) tarval_mod, mode, 0);
3041 DBG_OPT_ALGSIM0(n, va, FS_OPT_CONST_PHI);
3042 DBG_OPT_ALGSIM0(n, vb, FS_OPT_CONST_PHI);
3046 else if (is_Const(a) && is_const_Phi(b)) {
3047 /* check for Div(Const, Phi) */
3048 va = apply_binop_on_phi(b, get_Const_tarval(a), (eval_func) tarval_div, mode, 1);
3049 vb = apply_binop_on_phi(b, get_Const_tarval(a), (eval_func) tarval_mod, mode, 1);
3051 DBG_OPT_ALGSIM0(n, va, FS_OPT_CONST_PHI);
3052 DBG_OPT_ALGSIM0(n, vb, FS_OPT_CONST_PHI);
3056 else if (is_const_Phi(a) && is_const_Phi(b)) {
3057 /* check for Div(Phi, Phi) */
3058 va = apply_binop_on_2_phis(a, b, (eval_func) tarval_div, mode);
3059 vb = apply_binop_on_2_phis(a, b, (eval_func) tarval_mod, mode);
3061 DBG_OPT_ALGSIM0(n, va, FS_OPT_CONST_PHI);
3062 DBG_OPT_ALGSIM0(n, vb, FS_OPT_CONST_PHI);
3069 if (tb != tarval_bad) {
3070 if (tb == get_mode_one(get_tarval_mode(tb))) {
3072 vb = new_Const(get_mode_null(mode));
3073 DBG_OPT_CSTEVAL(n, vb);
3075 } else if (ta != tarval_bad) {
3076 tarval *resa, *resb;
3077 resa = tarval_div(ta, tb);
3078 if (resa == tarval_bad) return n; /* Causes exception!!! Model by replacing through
3079 Jmp for X result!? */
3080 resb = tarval_mod(ta, tb);
3081 if (resb == tarval_bad) return n; /* Causes exception! */
3082 va = new_Const(resa);
3083 vb = new_Const(resb);
3084 DBG_OPT_CSTEVAL(n, va);
3085 DBG_OPT_CSTEVAL(n, vb);
3087 } else if (mode_is_signed(mode) && tb == get_mode_minus_one(mode)) {
3088 va = new_rd_Minus(get_irn_dbg_info(n), get_nodes_block(n), a, mode);
3089 vb = new_Const(get_mode_null(mode));
3090 DBG_OPT_CSTEVAL(n, va);
3091 DBG_OPT_CSTEVAL(n, vb);
3093 } else { /* Try architecture dependent optimization */
3096 arch_dep_replace_divmod_by_const(&va, &vb, n);
3097 evaluated = va != NULL;
3099 } else if (a == b) {
3100 if (value_not_zero(a, &dummy)) {
3102 va = new_Const(get_mode_one(mode));
3103 vb = new_Const(get_mode_null(mode));
3104 DBG_OPT_CSTEVAL(n, va);
3105 DBG_OPT_CSTEVAL(n, vb);
3108 /* BEWARE: it is NOT possible to optimize a/a to 1, as this may cause a exception */
3111 } else if (ta == get_mode_null(mode) && value_not_zero(b, &dummy)) {
3112 /* 0 / non-Const = 0 */
3117 if (evaluated) { /* replace by tuple */
3121 mem = get_DivMod_mem(n);
3122 /* skip a potential Pin */
3123 mem = skip_Pin(mem);
3125 blk = get_nodes_block(n);
3126 turn_into_tuple(n, pn_DivMod_max);
3127 set_Tuple_pred(n, pn_DivMod_M, mem);
3128 set_Tuple_pred(n, pn_DivMod_X_regular, new_r_Jmp(blk));
3129 set_Tuple_pred(n, pn_DivMod_X_except, new_Bad()); /* no exception */
3130 set_Tuple_pred(n, pn_DivMod_res_div, va);
3131 set_Tuple_pred(n, pn_DivMod_res_mod, vb);
3135 } /* transform_node_DivMod */
3138 * Optimize x / c to x * (1/c)
3140 static ir_node *transform_node_Quot(ir_node *n) {
3141 ir_mode *mode = get_Quot_resmode(n);
3144 if (get_mode_arithmetic(mode) == irma_ieee754) {
3145 ir_node *b = get_Quot_right(n);
3146 tarval *tv = value_of(b);
3148 if (tv != tarval_bad) {
3149 int rem = tarval_fp_ops_enabled();
3152 * Floating point constant folding might be disabled here to
3154 * However, as we check for exact result, doing it is safe.
3157 tarval_enable_fp_ops(1);
3158 tv = tarval_quo(get_mode_one(mode), tv);
3159 tarval_enable_fp_ops(rem);
3161 /* Do the transformation if the result is either exact or we are not
3162 using strict rules. */
3163 if (tv != tarval_bad &&
3164 (tarval_ieee754_get_exact() || (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic) == 0)) {
3165 ir_node *blk = get_nodes_block(n);
3166 ir_node *c = new_Const(tv);
3167 ir_node *a = get_Quot_left(n);
3168 ir_node *m = new_rd_Mul(get_irn_dbg_info(n), blk, a, c, mode);
3169 ir_node *mem = get_Quot_mem(n);
3171 /* skip a potential Pin */
3172 mem = skip_Pin(mem);
3173 turn_into_tuple(n, pn_Quot_max);
3174 set_Tuple_pred(n, pn_Quot_M, mem);
3175 set_Tuple_pred(n, pn_Quot_X_regular, new_r_Jmp(blk));
3176 set_Tuple_pred(n, pn_Quot_X_except, new_Bad());
3177 set_Tuple_pred(n, pn_Quot_res, m);
3178 DBG_OPT_ALGSIM1(oldn, a, b, m, FS_OPT_FP_INV_MUL);
3183 } /* transform_node_Quot */
3186 * Optimize Abs(x) into x if x is Confirmed >= 0
3187 * Optimize Abs(x) into -x if x is Confirmed <= 0
3188 * Optimize Abs(-x) int Abs(x)
3190 static ir_node *transform_node_Abs(ir_node *n) {
3191 ir_node *c, *oldn = n;
3192 ir_node *a = get_Abs_op(n);
3195 HANDLE_UNOP_PHI(tarval_abs, a, c);
3197 switch (classify_value_sign(a)) {
3198 case value_classified_negative:
3199 mode = get_irn_mode(n);
3202 * We can replace the Abs by -x here.
3203 * We even could add a new Confirm here
3204 * (if not twos complement)
3206 * Note that -x would create a new node, so we could
3207 * not run it in the equivalent_node() context.
3209 n = new_rd_Minus(get_irn_dbg_info(n), get_nodes_block(n), a, mode);
3211 DBG_OPT_CONFIRM(oldn, n);
3213 case value_classified_positive:
3214 /* n is positive, Abs is not needed */
3217 DBG_OPT_CONFIRM(oldn, n);
3223 /* Abs(-x) = Abs(x) */
3224 mode = get_irn_mode(n);
3225 n = new_rd_Abs(get_irn_dbg_info(n), get_nodes_block(n), get_Minus_op(a), mode);
3226 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ABS_MINUS_X);
3230 } /* transform_node_Abs */
3233 * Optimize -a CMP -b into b CMP a.
3234 * This works only for for modes where unary Minus
3236 * Note that two-complement integers can Overflow
3237 * so it will NOT work.
3239 * For == and != can be handled in Proj(Cmp)
3241 static ir_node *transform_node_Cmp(ir_node *n) {
3243 ir_node *left = get_Cmp_left(n);
3244 ir_node *right = get_Cmp_right(n);
3246 if (is_Minus(left) && is_Minus(right) &&
3247 !mode_overflow_on_unary_Minus(get_irn_mode(left))) {
3248 ir_node *const new_left = get_Minus_op(right);
3249 ir_node *const new_right = get_Minus_op(left);
3250 n = new_rd_Cmp(get_irn_dbg_info(n), get_nodes_block(n), new_left, new_right);
3251 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CMP_OP_OP);
3254 } /* transform_node_Cmp */
3258 * Transform a Cond node.
3260 * Replace the Cond by a Jmp if it branches on a constant
3263 static ir_node *transform_node_Cond(ir_node *n) {
3266 ir_node *a = get_Cond_selector(n);
3267 tarval *ta = value_of(a);
3269 /* we need block info which is not available in floating irgs */
3270 if (get_irg_pinned(current_ir_graph) == op_pin_state_floats)
3273 if ((ta != tarval_bad) &&
3274 (get_irn_mode(a) == mode_b) &&
3275 (get_opt_unreachable_code())) {
3276 /* It's a boolean Cond, branching on a boolean constant.
3277 Replace it by a tuple (Bad, Jmp) or (Jmp, Bad) */
3278 ir_node *blk = get_nodes_block(n);
3279 jmp = new_r_Jmp(blk);
3280 turn_into_tuple(n, pn_Cond_max);
3281 if (ta == tarval_b_true) {
3282 set_Tuple_pred(n, pn_Cond_false, new_Bad());
3283 set_Tuple_pred(n, pn_Cond_true, jmp);
3285 set_Tuple_pred(n, pn_Cond_false, jmp);
3286 set_Tuple_pred(n, pn_Cond_true, new_Bad());
3288 /* We might generate an endless loop, so keep it alive. */
3289 add_End_keepalive(get_irg_end(current_ir_graph), blk);
3292 } /* transform_node_Cond */
3295 * Prototype of a recursive transform function
3296 * for bitwise distributive transformations.
3298 typedef ir_node* (*recursive_transform)(ir_node *n);
3301 * makes use of distributive laws for and, or, eor
3302 * and(a OP c, b OP c) -> and(a, b) OP c
3303 * note, might return a different op than n
3305 static ir_node *transform_bitwise_distributive(ir_node *n,
3306 recursive_transform trans_func)
3309 ir_node *a = get_binop_left(n);
3310 ir_node *b = get_binop_right(n);
3311 ir_op *op = get_irn_op(a);
3312 ir_op *op_root = get_irn_op(n);
3314 if(op != get_irn_op(b))
3317 /* and(conv(a), conv(b)) -> conv(and(a,b)) */
3318 if (op == op_Conv) {
3319 ir_node *a_op = get_Conv_op(a);
3320 ir_node *b_op = get_Conv_op(b);
3321 ir_mode *a_mode = get_irn_mode(a_op);
3322 ir_mode *b_mode = get_irn_mode(b_op);
3323 if(a_mode == b_mode && (mode_is_int(a_mode) || a_mode == mode_b)) {
3324 ir_node *blk = get_nodes_block(n);
3327 set_binop_left(n, a_op);
3328 set_binop_right(n, b_op);
3329 set_irn_mode(n, a_mode);
3331 n = new_r_Conv(blk, n, get_irn_mode(oldn));
3333 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
3339 /* nothing to gain here */
3343 if (op == op_Shrs || op == op_Shr || op == op_Shl
3344 || op == op_And || op == op_Or || op == op_Eor) {
3345 ir_node *a_left = get_binop_left(a);
3346 ir_node *a_right = get_binop_right(a);
3347 ir_node *b_left = get_binop_left(b);
3348 ir_node *b_right = get_binop_right(b);
3350 ir_node *op1 = NULL;
3351 ir_node *op2 = NULL;
3353 if (is_op_commutative(op)) {
3354 if (a_left == b_left) {
3358 } else if(a_left == b_right) {
3362 } else if(a_right == b_left) {
3368 if(a_right == b_right) {
3375 /* (a sop c) & (b sop c) => (a & b) sop c */
3376 ir_node *blk = get_nodes_block(n);
3378 ir_node *new_n = exact_copy(n);
3379 set_binop_left(new_n, op1);
3380 set_binop_right(new_n, op2);
3381 new_n = trans_func(new_n);
3383 if(op_root == op_Eor && op == op_Or) {
3384 dbg_info *dbgi = get_irn_dbg_info(n);
3385 ir_mode *mode = get_irn_mode(c);
3387 c = new_rd_Not(dbgi, blk, c, mode);
3388 n = new_rd_And(dbgi, blk, new_n, c, mode);
3391 set_nodes_block(n, blk);
3392 set_binop_left(n, new_n);
3393 set_binop_right(n, c);
3394 add_identities(current_ir_graph->value_table, n);
3397 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_SHIFT_AND);
3408 static ir_node *transform_node_And(ir_node *n) {
3409 ir_node *c, *oldn = n;
3410 ir_node *a = get_And_left(n);
3411 ir_node *b = get_And_right(n);
3414 mode = get_irn_mode(n);
3415 HANDLE_BINOP_PHI((eval_func) tarval_and, a, b, c, mode);
3417 /* we can evaluate 2 Projs of the same Cmp */
3418 if (mode == mode_b && is_Proj(a) && is_Proj(b)) {
3419 ir_node *pred_a = get_Proj_pred(a);
3420 ir_node *pred_b = get_Proj_pred(b);
3421 if (pred_a == pred_b) {
3422 dbg_info *dbgi = get_irn_dbg_info(n);
3423 ir_node *block = get_nodes_block(pred_a);
3424 pn_Cmp pn_a = get_Proj_proj(a);
3425 pn_Cmp pn_b = get_Proj_proj(b);
3426 /* yes, we can simply calculate with pncs */
3427 pn_Cmp new_pnc = pn_a & pn_b;
3429 return new_rd_Proj(dbgi, block, pred_a, mode_b, new_pnc);
3434 ir_node *op = get_Not_op(b);
3436 ir_node *ba = get_And_left(op);
3437 ir_node *bb = get_And_right(op);
3439 /* it's enough to test the following cases due to normalization! */
3440 if (get_Or_left(a) == ba && get_Or_right(a) == bb) {
3441 /* (a|b) & ~(a&b) = a^b */
3442 ir_node *block = get_nodes_block(n);
3444 n = new_rd_Eor(get_irn_dbg_info(n), block, ba, bb, mode);
3445 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_TO_EOR);
3453 ir_node *op = get_Not_op(a);
3455 ir_node *aa = get_And_left(op);
3456 ir_node *ab = get_And_right(op);
3458 /* it's enough to test the following cases due to normalization! */
3459 if (get_Or_left(b) == aa && get_Or_right(b) == ab) {
3460 /* (a|b) & ~(a&b) = a^b */
3461 ir_node *block = get_nodes_block(n);
3463 n = new_rd_Eor(get_irn_dbg_info(n), block, aa, ab, mode);
3464 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_TO_EOR);
3471 ir_node *al = get_Eor_left(a);
3472 ir_node *ar = get_Eor_right(a);
3475 /* (b ^ a) & b -> ~a & b */
3476 dbg_info *dbg = get_irn_dbg_info(n);
3477 ir_node *block = get_nodes_block(n);
3479 ar = new_rd_Not(dbg, block, ar, mode);
3480 n = new_rd_And(dbg, block, ar, b, mode);
3481 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3485 /* (a ^ b) & b -> ~a & b */
3486 dbg_info *dbg = get_irn_dbg_info(n);
3487 ir_node *block = get_nodes_block(n);
3489 al = new_rd_Not(dbg, block, al, mode);
3490 n = new_rd_And(dbg, block, al, b, mode);
3491 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3496 ir_node *bl = get_Eor_left(b);
3497 ir_node *br = get_Eor_right(b);
3500 /* a & (a ^ b) -> a & ~b */
3501 dbg_info *dbg = get_irn_dbg_info(n);
3502 ir_node *block = get_nodes_block(n);
3504 br = new_rd_Not(dbg, block, br, mode);
3505 n = new_rd_And(dbg, block, br, a, mode);
3506 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3510 /* a & (b ^ a) -> a & ~b */
3511 dbg_info *dbg = get_irn_dbg_info(n);
3512 ir_node *block = get_nodes_block(n);
3514 bl = new_rd_Not(dbg, block, bl, mode);
3515 n = new_rd_And(dbg, block, bl, a, mode);
3516 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3520 if (is_Not(a) && is_Not(b)) {
3521 /* ~a & ~b = ~(a|b) */
3522 ir_node *block = get_nodes_block(n);
3523 ir_mode *mode = get_irn_mode(n);
3527 n = new_rd_Or(get_irn_dbg_info(n), block, a, b, mode);
3528 n = new_rd_Not(get_irn_dbg_info(n), block, n, mode);
3529 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_DEMORGAN);
3533 if (is_Const(a) && b->vrp.valid && (tarval_is_all_one(tarval_or(get_Const_tarval(a),
3534 b->vrp.bits_not_set)))) {
3535 return new_rd_Id(get_irn_dbg_info(n), get_nodes_block(n),
3536 b, get_irn_mode(n));
3540 if (is_Const(b) && a->vrp.valid && (tarval_is_all_one(tarval_or(get_Const_tarval(b),
3541 a->vrp.bits_not_set)))) {
3542 return new_rd_Id(get_irn_dbg_info(n), get_nodes_block(n),
3543 a, get_irn_mode(n));
3547 n = transform_bitwise_distributive(n, transform_node_And);
3550 } /* transform_node_And */
3555 static ir_node *transform_node_Eor(ir_node *n) {
3556 ir_node *c, *oldn = n;
3557 ir_node *a = get_Eor_left(n);
3558 ir_node *b = get_Eor_right(n);
3559 ir_mode *mode = get_irn_mode(n);
3561 HANDLE_BINOP_PHI((eval_func) tarval_eor, a, b, c, mode);
3563 /* we can evaluate 2 Projs of the same Cmp */
3564 if (mode == mode_b && is_Proj(a) && is_Proj(b)) {
3565 ir_node *pred_a = get_Proj_pred(a);
3566 ir_node *pred_b = get_Proj_pred(b);
3567 if(pred_a == pred_b) {
3568 dbg_info *dbgi = get_irn_dbg_info(n);
3569 ir_node *block = get_nodes_block(pred_a);
3570 pn_Cmp pn_a = get_Proj_proj(a);
3571 pn_Cmp pn_b = get_Proj_proj(b);
3572 /* yes, we can simply calculate with pncs */
3573 pn_Cmp new_pnc = pn_a ^ pn_b;
3575 return new_rd_Proj(dbgi, block, pred_a, mode_b, new_pnc);
3581 n = new_rd_Const(get_irn_dbg_info(n), current_ir_graph,
3582 get_mode_null(mode));
3583 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_A_A);
3584 } else if (is_Const(b)) {
3585 if (is_Not(a)) { /* ~x ^ const -> x ^ ~const */
3586 ir_node *cnst = new_Const(tarval_not(get_Const_tarval(b)));
3587 ir_node *not_op = get_Not_op(a);
3588 dbg_info *dbg = get_irn_dbg_info(n);
3589 ir_node *block = get_nodes_block(n);
3590 ir_mode *mode = get_irn_mode(n);
3591 n = new_rd_Eor(dbg, block, not_op, cnst, mode);
3593 } else if (is_Const_all_one(b)) { /* x ^ 1...1 -> ~1 */
3594 n = new_r_Not(get_nodes_block(n), a, mode);
3595 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3598 n = transform_bitwise_distributive(n, transform_node_Eor);
3602 } /* transform_node_Eor */
3607 static ir_node *transform_node_Not(ir_node *n) {
3608 ir_node *c, *oldn = n;
3609 ir_node *a = get_Not_op(n);
3610 ir_mode *mode = get_irn_mode(n);
3612 HANDLE_UNOP_PHI(tarval_not,a,c);
3614 /* check for a boolean Not */
3615 if (mode == mode_b && is_Proj(a)) {
3616 ir_node *a_pred = get_Proj_pred(a);
3617 if (is_Cmp(a_pred)) {
3618 ir_node *cmp_block = get_nodes_block(a_pred);
3619 /* We negate a Cmp. The Cmp has the negated result anyways! */
3620 n = new_r_Proj(cmp_block, get_Proj_pred(a),
3621 mode_b, get_negated_pnc(get_Proj_proj(a), mode_b));
3622 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_CMP);
3627 ir_node *eor_b = get_Eor_right(a);
3628 if (is_Const(eor_b)) { /* ~(x ^ const) -> x ^ ~const */
3629 ir_node *cnst = new_Const(tarval_not(get_Const_tarval(eor_b)));
3630 ir_node *eor_a = get_Eor_left(a);
3631 dbg_info *dbg = get_irn_dbg_info(n);
3632 ir_node *block = get_nodes_block(n);
3633 ir_mode *mode = get_irn_mode(n);
3634 n = new_rd_Eor(dbg, block, eor_a, cnst, mode);
3638 if (get_mode_arithmetic(mode) == irma_twos_complement) {
3639 if (is_Minus(a)) { /* ~-x -> x + -1 */
3640 dbg_info *dbg = get_irn_dbg_info(n);
3641 ir_graph *irg = current_ir_graph;
3642 ir_node *block = get_nodes_block(n);
3643 ir_node *add_l = get_Minus_op(a);
3644 ir_node *add_r = new_rd_Const(dbg, irg, get_mode_minus_one(mode));
3645 n = new_rd_Add(dbg, block, add_l, add_r, mode);
3646 } else if (is_Add(a)) {
3647 ir_node *add_r = get_Add_right(a);
3648 if (is_Const(add_r) && is_Const_all_one(add_r)) {
3649 /* ~(x + -1) = -x */
3650 ir_node *op = get_Add_left(a);
3651 ir_node *blk = get_nodes_block(n);
3652 n = new_rd_Minus(get_irn_dbg_info(n), blk, op, get_irn_mode(n));
3653 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_MINUS_1);
3658 } /* transform_node_Not */
3661 * Transform a Minus.
3665 * -(a >>u (size-1)) = a >>s (size-1)
3666 * -(a >>s (size-1)) = a >>u (size-1)
3667 * -(a * const) -> a * -const
3669 static ir_node *transform_node_Minus(ir_node *n) {
3670 ir_node *c, *oldn = n;
3671 ir_node *a = get_Minus_op(n);
3674 HANDLE_UNOP_PHI(tarval_neg,a,c);
3676 mode = get_irn_mode(a);
3677 if (get_mode_arithmetic(mode) == irma_twos_complement) {
3678 /* the following rules are only to twos-complement */
3681 ir_node *op = get_Not_op(a);
3682 tarval *tv = get_mode_one(mode);
3683 ir_node *blk = get_nodes_block(n);
3684 ir_node *c = new_Const(tv);
3685 n = new_rd_Add(get_irn_dbg_info(n), blk, op, c, mode);
3686 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_MINUS_NOT);
3690 ir_node *c = get_Shr_right(a);
3693 tarval *tv = get_Const_tarval(c);
3695 if (tarval_is_long(tv) && get_tarval_long(tv) == (int) get_mode_size_bits(mode) - 1) {
3696 /* -(a >>u (size-1)) = a >>s (size-1) */
3697 ir_node *v = get_Shr_left(a);
3699 n = new_rd_Shrs(get_irn_dbg_info(n), get_nodes_block(n), v, c, mode);
3700 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_PREDICATE);
3706 ir_node *c = get_Shrs_right(a);
3709 tarval *tv = get_Const_tarval(c);
3711 if (tarval_is_long(tv) && get_tarval_long(tv) == (int) get_mode_size_bits(mode) - 1) {
3712 /* -(a >>s (size-1)) = a >>u (size-1) */
3713 ir_node *v = get_Shrs_left(a);
3715 n = new_rd_Shr(get_irn_dbg_info(n), get_nodes_block(n), v, c, mode);
3716 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_PREDICATE);
3723 /* - (a-b) = b - a */
3724 ir_node *la = get_Sub_left(a);
3725 ir_node *ra = get_Sub_right(a);
3726 ir_node *blk = get_nodes_block(n);
3728 n = new_rd_Sub(get_irn_dbg_info(n), blk, ra, la, mode);
3729 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_MINUS_SUB);
3733 if (is_Mul(a)) { /* -(a * const) -> a * -const */
3734 ir_node *mul_l = get_Mul_left(a);
3735 ir_node *mul_r = get_Mul_right(a);
3736 tarval *tv = value_of(mul_r);
3737 if (tv != tarval_bad) {
3738 tv = tarval_neg(tv);
3739 if (tv != tarval_bad) {
3740 ir_node *cnst = new_Const(tv);
3741 dbg_info *dbg = get_irn_dbg_info(a);
3742 ir_node *block = get_nodes_block(a);
3743 n = new_rd_Mul(dbg, block, mul_l, cnst, mode);
3744 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_MINUS_MUL_C);
3751 } /* transform_node_Minus */
3754 * Transform a Cast_type(Const) into a new Const_type
3756 static ir_node *transform_node_Cast(ir_node *n) {
3758 ir_node *pred = get_Cast_op(n);
3759 ir_type *tp = get_irn_type(n);
3761 if (is_Const(pred) && get_Const_type(pred) != tp) {
3762 n = new_rd_Const_type(NULL, current_ir_graph, get_Const_tarval(pred), tp);
3763 DBG_OPT_CSTEVAL(oldn, n);
3764 } else if (is_SymConst(pred) && get_SymConst_value_type(pred) != tp) {
3765 n = new_rd_SymConst_type(NULL, current_ir_graph, get_irn_mode(pred),
3766 get_SymConst_symbol(pred), get_SymConst_kind(pred), tp);
3767 DBG_OPT_CSTEVAL(oldn, n);
3771 } /* transform_node_Cast */
3774 * Transform a Proj(Load) with a non-null address.
3776 static ir_node *transform_node_Proj_Load(ir_node *proj) {
3777 if (get_opt_ldst_only_null_ptr_exceptions()) {
3778 if (get_irn_mode(proj) == mode_X) {
3779 ir_node *load = get_Proj_pred(proj);
3781 /* get the Load address */
3782 const ir_node *addr = get_Load_ptr(load);
3783 const ir_node *confirm;
3785 if (value_not_null(addr, &confirm)) {
3786 if (confirm == NULL) {
3787 /* this node may float if it did not depend on a Confirm */
3788 set_irn_pinned(load, op_pin_state_floats);
3790 if (get_Proj_proj(proj) == pn_Load_X_except) {
3791 DBG_OPT_EXC_REM(proj);
3792 return get_irg_bad(current_ir_graph);
3794 ir_node *blk = get_nodes_block(load);
3795 return new_r_Jmp(blk);
3801 } /* transform_node_Proj_Load */
3804 * Transform a Proj(Store) with a non-null address.
3806 static ir_node *transform_node_Proj_Store(ir_node *proj) {
3807 if (get_opt_ldst_only_null_ptr_exceptions()) {
3808 if (get_irn_mode(proj) == mode_X) {
3809 ir_node *store = get_Proj_pred(proj);
3811 /* get the load/store address */
3812 const ir_node *addr = get_Store_ptr(store);
3813 const ir_node *confirm;
3815 if (value_not_null(addr, &confirm)) {
3816 if (confirm == NULL) {
3817 /* this node may float if it did not depend on a Confirm */
3818 set_irn_pinned(store, op_pin_state_floats);
3820 if (get_Proj_proj(proj) == pn_Store_X_except) {
3821 DBG_OPT_EXC_REM(proj);
3822 return get_irg_bad(current_ir_graph);
3824 ir_node *blk = get_nodes_block(store);
3825 return new_r_Jmp(blk);
3831 } /* transform_node_Proj_Store */
3834 * Transform a Proj(Div) with a non-zero value.
3835 * Removes the exceptions and routes the memory to the NoMem node.
3837 static ir_node *transform_node_Proj_Div(ir_node *proj) {
3838 ir_node *div = get_Proj_pred(proj);
3839 ir_node *b = get_Div_right(div);
3840 ir_node *res, *new_mem;
3841 const ir_node *confirm;
3844 if (value_not_zero(b, &confirm)) {
3845 /* div(x, y) && y != 0 */
3846 if (confirm == NULL) {
3847 /* we are sure we have a Const != 0 */
3848 new_mem = get_Div_mem(div);
3849 new_mem = skip_Pin(new_mem);
3850 set_Div_mem(div, new_mem);
3851 set_irn_pinned(div, op_pin_state_floats);
3854 proj_nr = get_Proj_proj(proj);
3856 case pn_Div_X_regular:
3857 return new_r_Jmp(get_nodes_block(div));
3859 case pn_Div_X_except:
3860 /* we found an exception handler, remove it */
3861 DBG_OPT_EXC_REM(proj);
3865 res = get_Div_mem(div);
3866 new_mem = get_irg_no_mem(current_ir_graph);
3869 /* This node can only float up to the Confirm block */
3870 new_mem = new_r_Pin(get_nodes_block(confirm), new_mem);
3872 set_irn_pinned(div, op_pin_state_floats);
3873 /* this is a Div without exception, we can remove the memory edge */
3874 set_Div_mem(div, new_mem);
3879 } /* transform_node_Proj_Div */
3882 * Transform a Proj(Mod) with a non-zero value.
3883 * Removes the exceptions and routes the memory to the NoMem node.
3885 static ir_node *transform_node_Proj_Mod(ir_node *proj) {
3886 ir_node *mod = get_Proj_pred(proj);
3887 ir_node *b = get_Mod_right(mod);
3888 ir_node *res, *new_mem;
3889 const ir_node *confirm;
3892 if (value_not_zero(b, &confirm)) {
3893 /* mod(x, y) && y != 0 */
3894 proj_nr = get_Proj_proj(proj);
3896 if (confirm == NULL) {
3897 /* we are sure we have a Const != 0 */
3898 new_mem = get_Mod_mem(mod);
3899 new_mem = skip_Pin(new_mem);
3900 set_Mod_mem(mod, new_mem);
3901 set_irn_pinned(mod, op_pin_state_floats);
3906 case pn_Mod_X_regular:
3907 return new_r_Jmp(get_irn_n(mod, -1));
3909 case pn_Mod_X_except:
3910 /* we found an exception handler, remove it */
3911 DBG_OPT_EXC_REM(proj);
3915 res = get_Mod_mem(mod);
3916 new_mem = get_irg_no_mem(current_ir_graph);
3919 /* This node can only float up to the Confirm block */
3920 new_mem = new_r_Pin(get_nodes_block(confirm), new_mem);
3922 /* this is a Mod without exception, we can remove the memory edge */
3923 set_Mod_mem(mod, new_mem);
3926 if (get_Mod_left(mod) == b) {
3927 /* a % a = 0 if a != 0 */
3928 ir_mode *mode = get_irn_mode(proj);
3929 ir_node *res = new_Const(get_mode_null(mode));
3931 DBG_OPT_CSTEVAL(mod, res);
3937 } /* transform_node_Proj_Mod */
3940 * Transform a Proj(DivMod) with a non-zero value.
3941 * Removes the exceptions and routes the memory to the NoMem node.
3943 static ir_node *transform_node_Proj_DivMod(ir_node *proj) {
3944 ir_node *divmod = get_Proj_pred(proj);
3945 ir_node *b = get_DivMod_right(divmod);
3946 ir_node *res, *new_mem;
3947 const ir_node *confirm;
3950 if (value_not_zero(b, &confirm)) {
3951 /* DivMod(x, y) && y != 0 */
3952 proj_nr = get_Proj_proj(proj);
3954 if (confirm == NULL) {
3955 /* we are sure we have a Const != 0 */
3956 new_mem = get_DivMod_mem(divmod);
3957 new_mem = skip_Pin(new_mem);
3958 set_DivMod_mem(divmod, new_mem);
3959 set_irn_pinned(divmod, op_pin_state_floats);
3964 case pn_DivMod_X_regular:
3965 return new_r_Jmp(get_nodes_block(divmod));
3967 case pn_DivMod_X_except:
3968 /* we found an exception handler, remove it */
3969 DBG_OPT_EXC_REM(proj);
3973 res = get_DivMod_mem(divmod);
3974 new_mem = get_irg_no_mem(current_ir_graph);
3977 /* This node can only float up to the Confirm block */
3978 new_mem = new_r_Pin(get_nodes_block(confirm), new_mem);
3980 /* this is a DivMod without exception, we can remove the memory edge */
3981 set_DivMod_mem(divmod, new_mem);
3984 case pn_DivMod_res_mod:
3985 if (get_DivMod_left(divmod) == b) {
3986 /* a % a = 0 if a != 0 */
3987 ir_mode *mode = get_irn_mode(proj);
3988 ir_node *res = new_Const(get_mode_null(mode));
3990 DBG_OPT_CSTEVAL(divmod, res);
3996 } /* transform_node_Proj_DivMod */
3999 * Optimizes jump tables (CondIs or CondIu) by removing all impossible cases.
4001 static ir_node *transform_node_Proj_Cond(ir_node *proj) {
4002 if (get_opt_unreachable_code()) {
4003 ir_node *n = get_Proj_pred(proj);
4004 ir_node *b = get_Cond_selector(n);
4006 if (mode_is_int(get_irn_mode(b))) {
4007 tarval *tb = value_of(b);
4009 if (tb != tarval_bad) {
4010 /* we have a constant switch */
4011 long num = get_Proj_proj(proj);
4013 if (num != get_Cond_default_proj(n)) { /* we cannot optimize default Proj's yet */
4014 if (get_tarval_long(tb) == num) {
4015 /* Do NOT create a jump here, or we will have 2 control flow ops
4016 * in a block. This case is optimized away in optimize_cf(). */
4019 /* this case will NEVER be taken, kill it */
4020 return get_irg_bad(current_ir_graph);
4024 long num = get_Proj_proj(proj);
4025 if (num != get_Cond_default_proj(n) && b->vrp.valid) {
4026 /* Try handling with vrp data. We only remove dead parts. */
4027 tarval *tp = new_tarval_from_long(num, get_irn_mode(b));
4029 if (b->vrp.range_type == VRP_RANGE) {
4030 pn_Cmp cmp_result = tarval_cmp(b->vrp.range_bottom, tp);
4031 pn_Cmp cmp_result2 = tarval_cmp(b->vrp.range_top, tp);
4033 if ((cmp_result & pn_Cmp_Lt) == cmp_result && (cmp_result2
4034 & pn_Cmp_Gt) == cmp_result2) {
4035 return get_irg_bad(current_ir_graph);
4037 } else if (b->vrp.range_type == VRP_ANTIRANGE) {
4038 pn_Cmp cmp_result = tarval_cmp(b->vrp.range_bottom, tp);
4039 pn_Cmp cmp_result2 = tarval_cmp(b->vrp.range_top, tp);
4041 if ((cmp_result & pn_Cmp_Ge) == cmp_result && (cmp_result2
4042 & pn_Cmp_Le) == cmp_result2) {
4043 return get_irg_bad(current_ir_graph);
4048 tarval_and( b->vrp.bits_set, tp),
4052 return get_irg_bad(current_ir_graph);
4058 b->vrp.bits_not_set),
4059 b->vrp.bits_not_set)
4062 return get_irg_bad(current_ir_graph);
4071 } /* transform_node_Proj_Cond */
4074 * Create a 0 constant of given mode.
4076 static ir_node *create_zero_const(ir_mode *mode) {
4077 tarval *tv = get_mode_null(mode);
4078 ir_node *cnst = new_Const(tv);
4083 /* the order of the values is important! */
4084 typedef enum const_class {
4090 static const_class classify_const(const ir_node* n)
4092 if (is_Const(n)) return const_const;
4093 if (is_irn_constlike(n)) return const_like;
4098 * Determines whether r is more constlike or has a larger index (in that order)
4101 static int operands_are_normalized(const ir_node *l, const ir_node *r)
4103 const const_class l_order = classify_const(l);
4104 const const_class r_order = classify_const(r);
4106 l_order > r_order ||
4107 (l_order == r_order && get_irn_idx(l) <= get_irn_idx(r));
4111 * Normalizes and optimizes Cmp nodes.
4113 static ir_node *transform_node_Proj_Cmp(ir_node *proj) {
4114 ir_node *n = get_Proj_pred(proj);
4115 ir_node *left = get_Cmp_left(n);
4116 ir_node *right = get_Cmp_right(n);
4119 ir_mode *mode = NULL;
4120 long proj_nr = get_Proj_proj(proj);
4122 /* we can evaluate some cases directly */
4125 return new_Const(get_tarval_b_false());
4127 return new_Const(get_tarval_b_true());
4129 if (!mode_is_float(get_irn_mode(left)))
4130 return new_Const(get_tarval_b_true());
4136 /* remove Casts of both sides */
4137 left = skip_Cast(left);
4138 right = skip_Cast(right);
4140 /* Remove unnecessary conversions */
4141 /* TODO handle constants */
4142 if (is_Conv(left) && is_Conv(right)) {
4143 ir_mode *mode = get_irn_mode(left);
4144 ir_node *op_left = get_Conv_op(left);
4145 ir_node *op_right = get_Conv_op(right);
4146 ir_mode *mode_left = get_irn_mode(op_left);
4147 ir_mode *mode_right = get_irn_mode(op_right);
4149 if (smaller_mode(mode_left, mode) && smaller_mode(mode_right, mode)
4150 && mode_left != mode_b && mode_right != mode_b) {
4151 ir_node *block = get_nodes_block(n);
4153 if (mode_left == mode_right) {
4157 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV_CONV);
4158 } else if (smaller_mode(mode_left, mode_right)) {
4159 left = new_r_Conv(block, op_left, mode_right);
4162 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV);
4163 } else if (smaller_mode(mode_right, mode_left)) {
4165 right = new_r_Conv(block, op_right, mode_left);
4167 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV);
4172 /* remove operation on both sides if possible */
4173 if (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) {
4175 * The following operations are NOT safe for floating point operations, for instance
4176 * 1.0 + inf == 2.0 + inf, =/=> x == y
4178 if (mode_is_int(get_irn_mode(left))) {
4179 unsigned lop = get_irn_opcode(left);
4181 if (lop == get_irn_opcode(right)) {
4182 ir_node *ll, *lr, *rl, *rr;
4184 /* same operation on both sides, try to remove */
4188 /* ~a CMP ~b => a CMP b, -a CMP -b ==> a CMP b */
4189 left = get_unop_op(left);
4190 right = get_unop_op(right);
4192 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4195 ll = get_Add_left(left);
4196 lr = get_Add_right(left);
4197 rl = get_Add_left(right);
4198 rr = get_Add_right(right);
4201 /* X + a CMP X + b ==> a CMP b */
4205 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4206 } else if (ll == rr) {
4207 /* X + a CMP b + X ==> a CMP b */
4211 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4212 } else if (lr == rl) {
4213 /* a + X CMP X + b ==> a CMP b */
4217 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4218 } else if (lr == rr) {
4219 /* a + X CMP b + X ==> a CMP b */
4223 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4227 ll = get_Sub_left(left);
4228 lr = get_Sub_right(left);
4229 rl = get_Sub_left(right);
4230 rr = get_Sub_right(right);
4233 /* X - a CMP X - b ==> a CMP b */
4237 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4238 } else if (lr == rr) {
4239 /* a - X CMP b - X ==> a CMP b */
4243 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4247 if (get_Rotl_right(left) == get_Rotl_right(right)) {
4248 /* a ROTL X CMP b ROTL X ==> a CMP b */
4249 left = get_Rotl_left(left);
4250 right = get_Rotl_left(right);
4252 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4260 /* X+A == A, A+X == A, A-X == A -> X == 0 */
4261 if (is_Add(left) || is_Sub(left)) {
4262 ir_node *ll = get_binop_left(left);
4263 ir_node *lr = get_binop_right(left);
4265 if (lr == right && is_Add(left)) {
4272 right = create_zero_const(get_irn_mode(left));
4274 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4277 if (is_Add(right) || is_Sub(right)) {
4278 ir_node *rl = get_binop_left(right);
4279 ir_node *rr = get_binop_right(right);
4281 if (rr == left && is_Add(right)) {
4288 right = create_zero_const(get_irn_mode(left));
4290 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4293 if (is_And(left) && is_Const(right)) {
4294 ir_node *ll = get_binop_left(left);
4295 ir_node *lr = get_binop_right(left);
4296 if (is_Shr(ll) && is_Const(lr)) {
4297 /* Cmp((x >>u c1) & c2, c3) = Cmp(x & (c2 << c1), c3 << c1) */
4298 ir_node *block = get_nodes_block(n);
4299 ir_mode *mode = get_irn_mode(left);
4301 ir_node *llr = get_Shr_right(ll);
4302 if (is_Const(llr)) {
4303 dbg_info *dbg = get_irn_dbg_info(left);
4305 tarval *c1 = get_Const_tarval(llr);
4306 tarval *c2 = get_Const_tarval(lr);
4307 tarval *c3 = get_Const_tarval(right);
4308 tarval *mask = tarval_shl(c2, c1);
4309 tarval *value = tarval_shl(c3, c1);
4311 left = new_rd_And(dbg, block, get_Shr_left(ll), new_Const(mask), mode);
4312 right = new_Const(value);
4317 } /* mode_is_int(...) */
4318 } /* proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg */
4320 /* replace mode_b compares with ands/ors */
4321 if (get_irn_mode(left) == mode_b) {
4322 ir_node *block = get_nodes_block(n);
4326 case pn_Cmp_Le: bres = new_r_Or( block, new_r_Not(block, left, mode_b), right, mode_b); break;
4327 case pn_Cmp_Lt: bres = new_r_And(block, new_r_Not(block, left, mode_b), right, mode_b); break;
4328 case pn_Cmp_Ge: bres = new_r_Or( block, left, new_r_Not(block, right, mode_b), mode_b); break;
4329 case pn_Cmp_Gt: bres = new_r_And(block, left, new_r_Not(block, right, mode_b), mode_b); break;
4330 case pn_Cmp_Lg: bres = new_r_Eor(block, left, right, mode_b); break;
4331 case pn_Cmp_Eq: bres = new_r_Not(block, new_r_Eor(block, left, right, mode_b), mode_b); break;
4332 default: bres = NULL;
4335 DBG_OPT_ALGSIM0(n, bres, FS_OPT_CMP_TO_BOOL);
4341 * First step: normalize the compare op
4342 * by placing the constant on the right side
4343 * or moving the lower address node to the left.
4345 if (!operands_are_normalized(left, right)) {
4351 proj_nr = get_inversed_pnc(proj_nr);
4356 * Second step: Try to reduce the magnitude
4357 * of a constant. This may help to generate better code
4358 * later and may help to normalize more compares.
4359 * Of course this is only possible for integer values.
4361 tv = value_of(right);
4362 if (tv != tarval_bad) {
4363 mode = get_irn_mode(right);
4365 /* TODO extend to arbitrary constants */
4366 if (is_Conv(left) && tarval_is_null(tv)) {
4367 ir_node *op = get_Conv_op(left);
4368 ir_mode *op_mode = get_irn_mode(op);
4371 * UpConv(x) REL 0 ==> x REL 0
4372 * Don't do this for float values as it's unclear whether it is a
4373 * win. (on the other side it makes detection/creation of fabs hard)
4375 if (get_mode_size_bits(mode) > get_mode_size_bits(op_mode) &&
4376 ((proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) ||
4377 mode_is_signed(mode) || !mode_is_signed(op_mode)) &&
4378 !mode_is_float(mode)) {
4379 tv = get_mode_null(op_mode);
4383 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV);
4387 if (tv != tarval_bad) {
4388 /* the following optimization is possible on modes without Overflow
4389 * on Unary Minus or on == and !=:
4390 * -a CMP c ==> a swap(CMP) -c
4392 * Beware: for two-complement Overflow may occur, so only == and != can
4393 * be optimized, see this:
4394 * -MININT < 0 =/=> MININT > 0 !!!
4396 if (is_Minus(left) &&
4397 (!mode_overflow_on_unary_Minus(mode) ||
4398 (mode_is_int(mode) && (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg)))) {
4399 tv = tarval_neg(tv);
4401 if (tv != tarval_bad) {
4402 left = get_Minus_op(left);
4403 proj_nr = get_inversed_pnc(proj_nr);
4405 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4407 } else if (is_Not(left) && (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg)) {
4408 /* Not(a) ==/!= c ==> a ==/!= Not(c) */
4409 tv = tarval_not(tv);
4411 if (tv != tarval_bad) {
4412 left = get_Not_op(left);
4414 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4418 /* for integer modes, we have more */
4419 if (mode_is_int(mode)) {
4420 /* Ne includes Unordered which is not possible on integers.
4421 * However, frontends often use this wrong, so fix it here */
4422 if (proj_nr & pn_Cmp_Uo) {
4423 proj_nr &= ~pn_Cmp_Uo;
4424 set_Proj_proj(proj, proj_nr);
4427 /* c > 0 : a < c ==> a <= (c-1) a >= c ==> a > (c-1) */
4428 if ((proj_nr == pn_Cmp_Lt || proj_nr == pn_Cmp_Ge) &&
4429 tarval_cmp(tv, get_mode_null(mode)) == pn_Cmp_Gt) {
4430 tv = tarval_sub(tv, get_mode_one(mode), NULL);
4432 if (tv != tarval_bad) {
4433 proj_nr ^= pn_Cmp_Eq;
4435 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CNST_MAGN);
4438 /* c < 0 : a > c ==> a >= (c+1) a <= c ==> a < (c+1) */
4439 else if ((proj_nr == pn_Cmp_Gt || proj_nr == pn_Cmp_Le) &&
4440 tarval_cmp(tv, get_mode_null(mode)) == pn_Cmp_Lt) {
4441 tv = tarval_add(tv, get_mode_one(mode));
4443 if (tv != tarval_bad) {
4444 proj_nr ^= pn_Cmp_Eq;
4446 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CNST_MAGN);
4450 /* the following reassociations work only for == and != */
4451 if (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) {
4453 #if 0 /* Might be not that good in general */
4454 /* a-b == 0 ==> a == b, a-b != 0 ==> a != b */
4455 if (tarval_is_null(tv) && is_Sub(left)) {
4456 right = get_Sub_right(left);
4457 left = get_Sub_left(left);
4459 tv = value_of(right);
4461 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4465 if (tv != tarval_bad) {
4466 /* a-c1 == c2 ==> a == c2+c1, a-c1 != c2 ==> a != c2+c1 */
4468 ir_node *c1 = get_Sub_right(left);
4469 tarval *tv2 = value_of(c1);
4471 if (tv2 != tarval_bad) {
4472 tv2 = tarval_add(tv, value_of(c1));
4474 if (tv2 != tarval_bad) {
4475 left = get_Sub_left(left);
4478 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4482 /* a+c1 == c2 ==> a == c2-c1, a+c1 != c2 ==> a != c2-c1 */
4483 else if (is_Add(left)) {
4484 ir_node *a_l = get_Add_left(left);
4485 ir_node *a_r = get_Add_right(left);
4489 if (is_Const(a_l)) {
4491 tv2 = value_of(a_l);
4494 tv2 = value_of(a_r);
4497 if (tv2 != tarval_bad) {
4498 tv2 = tarval_sub(tv, tv2, NULL);
4500 if (tv2 != tarval_bad) {
4504 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4508 /* -a == c ==> a == -c, -a != c ==> a != -c */
4509 else if (is_Minus(left)) {
4510 tarval *tv2 = tarval_sub(get_mode_null(mode), tv, NULL);
4512 if (tv2 != tarval_bad) {
4513 left = get_Minus_op(left);
4516 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4521 /* the following reassociations work only for <= */
4522 else if (proj_nr == pn_Cmp_Le || proj_nr == pn_Cmp_Lt) {
4523 if (tv != tarval_bad) {
4524 /* c >= 0 : Abs(a) <= c ==> (unsigned)(a + c) <= 2*c */
4525 if (is_Abs(left)) { // TODO something is missing here
4531 if (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) {
4532 switch (get_irn_opcode(left)) {
4536 c1 = get_And_right(left);
4539 * And(x, C1) == C2 ==> FALSE if C2 & C1 != C2
4540 * And(x, C1) != C2 ==> TRUE if C2 & C1 != C2
4542 tarval *mask = tarval_and(get_Const_tarval(c1), tv);
4544 /* TODO: move to constant evaluation */
4545 tv = proj_nr == pn_Cmp_Eq ? get_tarval_b_false() : get_tarval_b_true();
4547 DBG_OPT_CSTEVAL(proj, c1);
4551 if (tarval_is_single_bit(tv)) {
4553 * optimization for AND:
4555 * And(x, C) == C ==> And(x, C) != 0
4556 * And(x, C) != C ==> And(X, C) == 0
4558 * if C is a single Bit constant.
4561 /* check for Constant's match. We have check hare the tarvals,
4562 because our const might be changed */
4563 if (get_Const_tarval(c1) == tv) {
4564 /* fine: do the transformation */
4565 tv = get_mode_null(get_tarval_mode(tv));
4566 proj_nr ^= pn_Cmp_Leg;
4568 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CNST_MAGN);
4574 c1 = get_Or_right(left);
4575 if (is_Const(c1) && tarval_is_null(tv)) {
4577 * Or(x, C) == 0 && C != 0 ==> FALSE
4578 * Or(x, C) != 0 && C != 0 ==> TRUE
4580 if (! tarval_is_null(get_Const_tarval(c1))) {
4581 /* TODO: move to constant evaluation */
4582 tv = proj_nr == pn_Cmp_Eq ? get_tarval_b_false() : get_tarval_b_true();
4584 DBG_OPT_CSTEVAL(proj, c1);
4591 * optimize x << c1 == c into x & (-1 >>u c1) == c >> c1 if c & (-1 << c1) == c
4593 * optimize x << c1 != c into x & (-1 >>u c1) != c >> c1 if c & (-1 << c1) == c
4596 c1 = get_Shl_right(left);
4598 tarval *tv1 = get_Const_tarval(c1);
4599 ir_mode *mode = get_irn_mode(left);
4600 tarval *minus1 = get_mode_all_one(mode);
4601 tarval *amask = tarval_shr(minus1, tv1);
4602 tarval *cmask = tarval_shl(minus1, tv1);
4605 if (tarval_and(tv, cmask) != tv) {
4606 /* condition not met */
4607 tv = proj_nr == pn_Cmp_Eq ? get_tarval_b_false() : get_tarval_b_true();
4609 DBG_OPT_CSTEVAL(proj, c1);
4612 sl = get_Shl_left(left);
4613 blk = get_nodes_block(n);
4614 left = new_rd_And(get_irn_dbg_info(left), blk, sl, new_Const(amask), mode);
4615 tv = tarval_shr(tv, tv1);
4617 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_SHF_TO_AND);
4622 * optimize x >>u c1 == c into x & (-1 << c1) == c << c1 if c & (-1 >>u c1) == c
4624 * optimize x >>u c1 != c into x & (-1 << c1) != c << c1 if c & (-1 >>u c1) == c
4627 c1 = get_Shr_right(left);
4629 tarval *tv1 = get_Const_tarval(c1);
4630 ir_mode *mode = get_irn_mode(left);
4631 tarval *minus1 = get_mode_all_one(mode);
4632 tarval *amask = tarval_shl(minus1, tv1);
4633 tarval *cmask = tarval_shr(minus1, tv1);
4636 if (tarval_and(tv, cmask) != tv) {
4637 /* condition not met */
4638 tv = proj_nr == pn_Cmp_Eq ? get_tarval_b_false() : get_tarval_b_true();
4640 DBG_OPT_CSTEVAL(proj, c1);
4643 sl = get_Shr_left(left);
4644 blk = get_nodes_block(n);
4645 left = new_rd_And(get_irn_dbg_info(left), blk, sl, new_Const(amask), mode);
4646 tv = tarval_shl(tv, tv1);
4648 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_SHF_TO_AND);
4653 * optimize x >>s c1 == c into x & (-1 << c1) == c << c1 if (c >>s (BITS - c1)) \in {0,-1}
4655 * optimize x >>s c1 != c into x & (-1 << c1) != c << c1 if (c >>s (BITS - c1)) \in {0,-1}
4658 c1 = get_Shrs_right(left);
4660 tarval *tv1 = get_Const_tarval(c1);
4661 ir_mode *mode = get_irn_mode(left);
4662 tarval *minus1 = get_mode_all_one(mode);
4663 tarval *amask = tarval_shl(minus1, tv1);
4664 tarval *cond = new_tarval_from_long(get_mode_size_bits(mode), get_tarval_mode(tv1));
4667 cond = tarval_sub(cond, tv1, NULL);
4668 cond = tarval_shrs(tv, cond);
4670 if (!tarval_is_all_one(cond) && !tarval_is_null(cond)) {
4671 /* condition not met */
4672 tv = proj_nr == pn_Cmp_Eq ? get_tarval_b_false() : get_tarval_b_true();
4674 DBG_OPT_CSTEVAL(proj, c1);
4677 sl = get_Shrs_left(left);
4678 blk = get_nodes_block(n);
4679 left = new_rd_And(get_irn_dbg_info(left), blk, sl, new_Const(amask), mode);
4680 tv = tarval_shl(tv, tv1);
4682 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_SHF_TO_AND);
4687 } /* tarval != bad */
4690 if (changed & 2) /* need a new Const */
4691 right = new_Const(tv);
4693 if ((proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) && is_Const(right) && is_Const_null(right) && is_Proj(left)) {
4694 ir_node *op = get_Proj_pred(left);
4696 if ((is_Mod(op) && get_Proj_proj(left) == pn_Mod_res) ||
4697 (is_DivMod(op) && get_Proj_proj(left) == pn_DivMod_res_mod)) {
4698 ir_node *c = get_binop_right(op);
4701 tarval *tv = get_Const_tarval(c);
4703 if (tarval_is_single_bit(tv)) {
4704 /* special case: (x % 2^n) CMP 0 ==> x & (2^n-1) CMP 0 */
4705 ir_node *v = get_binop_left(op);
4706 ir_node *blk = get_irn_n(op, -1);
4707 ir_mode *mode = get_irn_mode(v);
4709 tv = tarval_sub(tv, get_mode_one(mode), NULL);
4710 left = new_rd_And(get_irn_dbg_info(op), blk, v, new_Const(tv), mode);
4712 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_MOD_TO_AND);
4719 ir_node *block = get_nodes_block(n);
4721 /* create a new compare */
4722 n = new_rd_Cmp(get_irn_dbg_info(n), block, left, right);
4723 proj = new_rd_Proj(get_irn_dbg_info(proj), block, n, get_irn_mode(proj), proj_nr);
4727 } /* transform_node_Proj_Cmp */
4730 * Optimize CopyB(mem, x, x) into a Nop.
4732 static ir_node *transform_node_Proj_CopyB(ir_node *proj) {
4733 ir_node *copyb = get_Proj_pred(proj);
4734 ir_node *a = get_CopyB_dst(copyb);
4735 ir_node *b = get_CopyB_src(copyb);
4738 switch (get_Proj_proj(proj)) {
4739 case pn_CopyB_X_regular:
4740 /* Turn CopyB into a tuple (mem, jmp, bad, bad) */
4741 DBG_OPT_EXC_REM(proj);
4742 proj = new_r_Jmp(get_nodes_block(copyb));
4744 case pn_CopyB_X_except:
4745 DBG_OPT_EXC_REM(proj);
4746 proj = get_irg_bad(get_irn_irg(proj));
4753 } /* transform_node_Proj_CopyB */
4756 * Optimize Bounds(idx, idx, upper) into idx.
4758 static ir_node *transform_node_Proj_Bound(ir_node *proj) {
4759 ir_node *oldn = proj;
4760 ir_node *bound = get_Proj_pred(proj);
4761 ir_node *idx = get_Bound_index(bound);
4762 ir_node *pred = skip_Proj(idx);
4765 if (idx == get_Bound_lower(bound))
4767 else if (is_Bound(pred)) {
4769 * idx was Bounds checked in the same MacroBlock previously,
4770 * it is still valid if lower <= pred_lower && pred_upper <= upper.
4772 ir_node *lower = get_Bound_lower(bound);
4773 ir_node *upper = get_Bound_upper(bound);
4774 if (get_Bound_lower(pred) == lower &&
4775 get_Bound_upper(pred) == upper &&
4776 get_irn_MacroBlock(bound) == get_irn_MacroBlock(pred)) {
4778 * One could expect that we simply return the previous
4779 * Bound here. However, this would be wrong, as we could
4780 * add an exception Proj to a new location then.
4781 * So, we must turn in into a tuple.
4787 /* Turn Bound into a tuple (mem, jmp, bad, idx) */
4788 switch (get_Proj_proj(proj)) {
4790 DBG_OPT_EXC_REM(proj);
4791 proj = get_Bound_mem(bound);
4793 case pn_Bound_X_except:
4794 DBG_OPT_EXC_REM(proj);
4795 proj = get_irg_bad(get_irn_irg(proj));
4799 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NOP);
4801 case pn_Bound_X_regular:
4802 DBG_OPT_EXC_REM(proj);
4803 proj = new_r_Jmp(get_nodes_block(bound));
4810 } /* transform_node_Proj_Bound */
4813 * Does all optimizations on nodes that must be done on it's Proj's
4814 * because of creating new nodes.
4816 static ir_node *transform_node_Proj(ir_node *proj) {
4817 ir_node *n = get_Proj_pred(proj);
4819 if (n->op->ops.transform_node_Proj)
4820 return n->op->ops.transform_node_Proj(proj);
4822 } /* transform_node_Proj */
4825 * Move Confirms down through Phi nodes.
4827 static ir_node *transform_node_Phi(ir_node *phi) {
4829 ir_mode *mode = get_irn_mode(phi);
4831 if (mode_is_reference(mode)) {
4832 n = get_irn_arity(phi);
4834 /* Beware of Phi0 */
4836 ir_node *pred = get_irn_n(phi, 0);
4837 ir_node *bound, *new_Phi, *block, **in;
4840 if (! is_Confirm(pred))
4843 bound = get_Confirm_bound(pred);
4844 pnc = get_Confirm_cmp(pred);
4846 NEW_ARR_A(ir_node *, in, n);
4847 in[0] = get_Confirm_value(pred);
4849 for (i = 1; i < n; ++i) {
4850 pred = get_irn_n(phi, i);
4852 if (! is_Confirm(pred) ||
4853 get_Confirm_bound(pred) != bound ||
4854 get_Confirm_cmp(pred) != pnc)
4856 in[i] = get_Confirm_value(pred);
4858 /* move the Confirm nodes "behind" the Phi */
4859 block = get_irn_n(phi, -1);
4860 new_Phi = new_r_Phi(block, n, in, get_irn_mode(phi));
4861 return new_r_Confirm(block, new_Phi, bound, pnc);
4865 } /* transform_node_Phi */
4868 * Returns the operands of a commutative bin-op, if one operand is
4869 * a const, it is returned as the second one.
4871 static void get_comm_Binop_Ops(ir_node *binop, ir_node **a, ir_node **c) {
4872 ir_node *op_a = get_binop_left(binop);
4873 ir_node *op_b = get_binop_right(binop);
4875 assert(is_op_commutative(get_irn_op(binop)));
4877 if (is_Const(op_a)) {
4884 } /* get_comm_Binop_Ops */
4887 * Optimize a Or(And(Or(And(v,c4),c3),c2),c1) pattern if possible.
4888 * Such pattern may arise in bitfield stores.
4890 * value c4 value c4 & c2
4891 * AND c3 AND c1 | c3
4898 * AND c1 ===> OR if (c1 | c2) == 0x111..11
4901 static ir_node *transform_node_Or_bf_store(ir_node *or) {
4904 ir_node *and_l, *c3;
4905 ir_node *value, *c4;
4906 ir_node *new_and, *new_const, *block;
4907 ir_mode *mode = get_irn_mode(or);
4909 tarval *tv1, *tv2, *tv3, *tv4, *tv;
4912 get_comm_Binop_Ops(or, &and, &c1);
4913 if (!is_Const(c1) || !is_And(and))
4916 get_comm_Binop_Ops(and, &or_l, &c2);
4920 tv1 = get_Const_tarval(c1);
4921 tv2 = get_Const_tarval(c2);
4923 tv = tarval_or(tv1, tv2);
4924 if (tarval_is_all_one(tv)) {
4925 /* the AND does NOT clear a bit with isn't set by the OR */
4926 set_Or_left(or, or_l);
4927 set_Or_right(or, c1);
4929 /* check for more */
4936 get_comm_Binop_Ops(or_l, &and_l, &c3);
4937 if (!is_Const(c3) || !is_And(and_l))
4940 get_comm_Binop_Ops(and_l, &value, &c4);
4944 /* ok, found the pattern, check for conditions */
4945 assert(mode == get_irn_mode(and));
4946 assert(mode == get_irn_mode(or_l));
4947 assert(mode == get_irn_mode(and_l));
4949 tv3 = get_Const_tarval(c3);
4950 tv4 = get_Const_tarval(c4);
4952 tv = tarval_or(tv4, tv2);
4953 if (!tarval_is_all_one(tv)) {
4954 /* have at least one 0 at the same bit position */
4958 if (tv3 != tarval_andnot(tv3, tv4)) {
4959 /* bit in the or_mask is outside the and_mask */
4963 if (tv1 != tarval_andnot(tv1, tv2)) {
4964 /* bit in the or_mask is outside the and_mask */
4968 /* ok, all conditions met */
4969 block = get_irn_n(or, -1);
4971 new_and = new_r_And(block, value, new_Const(tarval_and(tv4, tv2)), mode);
4973 new_const = new_Const(tarval_or(tv3, tv1));
4975 set_Or_left(or, new_and);
4976 set_Or_right(or, new_const);
4978 /* check for more */
4980 } /* transform_node_Or_bf_store */
4983 * Optimize an Or(shl(x, c), shr(x, bits - c)) into a Rotl
4985 static ir_node *transform_node_Or_Rotl(ir_node *or) {
4986 ir_mode *mode = get_irn_mode(or);
4987 ir_node *shl, *shr, *block;
4988 ir_node *irn, *x, *c1, *c2, *v, *sub, *n, *rotval;
4991 if (! mode_is_int(mode))
4994 shl = get_binop_left(or);
4995 shr = get_binop_right(or);
5004 } else if (!is_Shl(shl)) {
5006 } else if (!is_Shr(shr)) {
5009 x = get_Shl_left(shl);
5010 if (x != get_Shr_left(shr))
5013 c1 = get_Shl_right(shl);
5014 c2 = get_Shr_right(shr);
5015 if (is_Const(c1) && is_Const(c2)) {
5016 tv1 = get_Const_tarval(c1);
5017 if (! tarval_is_long(tv1))
5020 tv2 = get_Const_tarval(c2);
5021 if (! tarval_is_long(tv2))
5024 if (get_tarval_long(tv1) + get_tarval_long(tv2)
5025 != (int) get_mode_size_bits(mode))
5028 /* yet, condition met */
5029 block = get_nodes_block(or);
5031 n = new_r_Rotl(block, x, c1, mode);
5033 DBG_OPT_ALGSIM1(or, shl, shr, n, FS_OPT_OR_SHFT_TO_ROTL);
5040 rotval = sub; /* a Rot right is not supported, so use a rot left */
5041 } else if (is_Sub(c2)) {
5047 if (get_Sub_right(sub) != v)
5050 c1 = get_Sub_left(sub);
5054 tv1 = get_Const_tarval(c1);
5055 if (! tarval_is_long(tv1))
5058 if (get_tarval_long(tv1) != (int) get_mode_size_bits(mode))
5061 /* yet, condition met */
5062 block = get_nodes_block(or);
5064 n = new_r_Rotl(block, x, rotval, mode);
5066 DBG_OPT_ALGSIM0(or, n, FS_OPT_OR_SHFT_TO_ROTL);
5068 } /* transform_node_Or_Rotl */
5073 static ir_node *transform_node_Or(ir_node *n) {
5074 ir_node *c, *oldn = n;
5075 ir_node *a = get_Or_left(n);
5076 ir_node *b = get_Or_right(n);
5079 if (is_Not(a) && is_Not(b)) {
5080 /* ~a | ~b = ~(a&b) */
5081 ir_node *block = get_nodes_block(n);
5083 mode = get_irn_mode(n);
5086 n = new_rd_And(get_irn_dbg_info(n), block, a, b, mode);
5087 n = new_rd_Not(get_irn_dbg_info(n), block, n, mode);
5088 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_DEMORGAN);
5092 /* we can evaluate 2 Projs of the same Cmp */
5093 if (get_irn_mode(n) == mode_b && is_Proj(a) && is_Proj(b)) {
5094 ir_node *pred_a = get_Proj_pred(a);
5095 ir_node *pred_b = get_Proj_pred(b);
5096 if (pred_a == pred_b) {
5097 dbg_info *dbgi = get_irn_dbg_info(n);
5098 ir_node *block = get_nodes_block(pred_a);
5099 pn_Cmp pn_a = get_Proj_proj(a);
5100 pn_Cmp pn_b = get_Proj_proj(b);
5101 /* yes, we can simply calculate with pncs */
5102 pn_Cmp new_pnc = pn_a | pn_b;
5104 return new_rd_Proj(dbgi, block, pred_a, mode_b, new_pnc);
5108 mode = get_irn_mode(n);
5109 HANDLE_BINOP_PHI((eval_func) tarval_or, a, b, c, mode);
5111 n = transform_node_Or_bf_store(n);
5112 n = transform_node_Or_Rotl(n);
5116 n = transform_bitwise_distributive(n, transform_node_Or);
5119 } /* transform_node_Or */
5123 static ir_node *transform_node(ir_node *n);
5126 * Optimize (a >> c1) >> c2), works for Shr, Shrs, Shl, Rotl.
5128 * Should be moved to reassociation?
5130 static ir_node *transform_node_shift(ir_node *n) {
5131 ir_node *left, *right;
5133 tarval *tv1, *tv2, *res;
5134 ir_node *in[2], *irn, *block;
5136 left = get_binop_left(n);
5138 /* different operations */
5139 if (get_irn_op(left) != get_irn_op(n))
5142 right = get_binop_right(n);
5143 tv1 = value_of(right);
5144 if (tv1 == tarval_bad)
5147 tv2 = value_of(get_binop_right(left));
5148 if (tv2 == tarval_bad)
5151 res = tarval_add(tv1, tv2);
5152 mode = get_irn_mode(n);
5154 /* beware: a simple replacement works only, if res < modulo shift */
5156 int modulo_shf = get_mode_modulo_shift(mode);
5157 if (modulo_shf > 0) {
5158 tarval *modulo = new_tarval_from_long(modulo_shf,
5159 get_tarval_mode(res));
5161 assert(modulo_shf >= (int) get_mode_size_bits(mode));
5163 /* shifting too much */
5164 if (!(tarval_cmp(res, modulo) & pn_Cmp_Lt)) {
5166 ir_node *block = get_nodes_block(n);
5167 dbg_info *dbgi = get_irn_dbg_info(n);
5168 ir_mode *smode = get_irn_mode(right);
5169 ir_node *cnst = new_Const_long(smode, get_mode_size_bits(mode) - 1);
5170 return new_rd_Shrs(dbgi, block, get_binop_left(left), cnst, mode);
5173 return new_Const(get_mode_null(mode));
5177 res = tarval_mod(res, new_tarval_from_long(get_mode_size_bits(mode), get_tarval_mode(res)));
5180 /* ok, we can replace it */
5181 block = get_nodes_block(n);
5183 in[0] = get_binop_left(left);
5184 in[1] = new_Const(res);
5186 irn = new_ir_node(NULL, get_Block_irg(block), block, get_irn_op(n), mode, 2, in);
5188 DBG_OPT_ALGSIM0(n, irn, FS_OPT_REASSOC_SHIFT);
5190 return transform_node(irn);
5191 } /* transform_node_shift */
5194 * normalisation: (x & c1) >> c2 to (x >> c2) & (c1 >> c2)
5196 * - and, or, xor instead of &
5197 * - Shl, Shr, Shrs, rotl instead of >>
5198 * (with a special case for Or/Xor + Shrs)
5200 static ir_node *transform_node_bitop_shift(ir_node *n) {
5202 ir_node *right = get_binop_right(n);
5203 ir_mode *mode = get_irn_mode(n);
5204 ir_node *bitop_left;
5205 ir_node *bitop_right;
5216 assert(is_Shrs(n) || is_Shr(n) || is_Shl(n) || is_Rotl(n));
5218 if (!is_Const(right))
5221 left = get_binop_left(n);
5222 op_left = get_irn_op(left);
5223 if (op_left != op_And && op_left != op_Or && op_left != op_Eor)
5226 /* doing it with Shrs is not legal if the Or/Eor affects the topmost bit */
5227 if (is_Shrs(n) && (op_left == op_Or || op_left == op_Eor)) {
5228 /* TODO: test if sign bit is affectes */
5232 bitop_right = get_binop_right(left);
5233 if (!is_Const(bitop_right))
5236 bitop_left = get_binop_left(left);
5238 block = get_nodes_block(n);
5239 dbgi = get_irn_dbg_info(n);
5240 tv1 = get_Const_tarval(bitop_right);
5241 tv2 = get_Const_tarval(right);
5243 assert(get_tarval_mode(tv1) == mode);
5246 new_shift = new_rd_Shl(dbgi, block, bitop_left, right, mode);
5247 tv_shift = tarval_shl(tv1, tv2);
5248 } else if(is_Shr(n)) {
5249 new_shift = new_rd_Shr(dbgi, block, bitop_left, right, mode);
5250 tv_shift = tarval_shr(tv1, tv2);
5251 } else if(is_Shrs(n)) {
5252 new_shift = new_rd_Shrs(dbgi, block, bitop_left, right, mode);
5253 tv_shift = tarval_shrs(tv1, tv2);
5256 new_shift = new_rd_Rotl(dbgi, block, bitop_left, right, mode);
5257 tv_shift = tarval_rotl(tv1, tv2);
5260 assert(get_tarval_mode(tv_shift) == mode);
5261 new_const = new_Const(tv_shift);
5263 if (op_left == op_And) {
5264 new_bitop = new_rd_And(dbgi, block, new_shift, new_const, mode);
5265 } else if(op_left == op_Or) {
5266 new_bitop = new_rd_Or(dbgi, block, new_shift, new_const, mode);
5268 assert(op_left == op_Eor);
5269 new_bitop = new_rd_Eor(dbgi, block, new_shift, new_const, mode);
5277 * (x << c1) >> c2 <=> x OP (c2-c1) & ((-1 << c1) >> c2)
5279 * (x >> c1) << c2 <=> x OP (c2-c1) & ((-1 >> c1) << c2)
5280 * (also with x >>s c1 when c1>=c2)
5282 static ir_node *transform_node_shl_shr(ir_node *n) {
5284 ir_node *right = get_binop_right(n);
5300 assert(is_Shl(n) || is_Shr(n) || is_Shrs(n));
5302 if (!is_Const(right))
5305 left = get_binop_left(n);
5306 mode = get_irn_mode(n);
5307 if (is_Shl(n) && (is_Shr(left) || is_Shrs(left))) {
5308 ir_node *shr_right = get_binop_right(left);
5310 if (!is_Const(shr_right))
5313 x = get_binop_left(left);
5314 tv_shr = get_Const_tarval(shr_right);
5315 tv_shl = get_Const_tarval(right);
5317 if (is_Shrs(left)) {
5318 /* shrs variant only allowed if c1 >= c2 */
5319 if (! (tarval_cmp(tv_shl, tv_shr) & pn_Cmp_Ge))
5322 tv_mask = tarval_shrs(get_mode_all_one(mode), tv_shr);
5325 tv_mask = tarval_shr(get_mode_all_one(mode), tv_shr);
5327 tv_mask = tarval_shl(tv_mask, tv_shl);
5328 } else if(is_Shr(n) && is_Shl(left)) {
5329 ir_node *shl_right = get_Shl_right(left);
5331 if (!is_Const(shl_right))
5334 x = get_Shl_left(left);
5335 tv_shr = get_Const_tarval(right);
5336 tv_shl = get_Const_tarval(shl_right);
5338 tv_mask = tarval_shl(get_mode_all_one(mode), tv_shl);
5339 tv_mask = tarval_shr(tv_mask, tv_shr);
5344 if (get_tarval_mode(tv_shl) != get_tarval_mode(tv_shr)) {
5345 tv_shl = tarval_convert_to(tv_shl, get_tarval_mode(tv_shr));
5348 assert(tv_mask != tarval_bad);
5349 assert(get_tarval_mode(tv_mask) == mode);
5351 irg = get_irn_irg(n);
5352 block = get_nodes_block(n);
5353 dbgi = get_irn_dbg_info(n);
5355 pnc = tarval_cmp(tv_shl, tv_shr);
5356 if (pnc == pn_Cmp_Lt || pnc == pn_Cmp_Eq) {
5357 tv_shift = tarval_sub(tv_shr, tv_shl, NULL);
5358 new_const = new_Const(tv_shift);
5360 new_shift = new_rd_Shrs(dbgi, block, x, new_const, mode);
5362 new_shift = new_rd_Shr(dbgi, block, x, new_const, mode);
5365 assert(pnc == pn_Cmp_Gt);
5366 tv_shift = tarval_sub(tv_shl, tv_shr, NULL);
5367 new_const = new_Const(tv_shift);
5368 new_shift = new_rd_Shl(dbgi, block, x, new_const, mode);
5371 new_const = new_Const(tv_mask);
5372 new_and = new_rd_And(dbgi, block, new_shift, new_const, mode);
5380 static ir_node *transform_node_Shr(ir_node *n) {
5381 ir_node *c, *oldn = n;
5382 ir_node *left = get_Shr_left(n);
5383 ir_node *right = get_Shr_right(n);
5384 ir_mode *mode = get_irn_mode(n);
5386 HANDLE_BINOP_PHI((eval_func) tarval_shr, left, right, c, mode);
5387 n = transform_node_shift(n);
5390 n = transform_node_shl_shr(n);
5392 n = transform_node_bitop_shift(n);
5395 } /* transform_node_Shr */
5400 static ir_node *transform_node_Shrs(ir_node *n) {
5401 ir_node *c, *oldn = n;
5402 ir_node *a = get_Shrs_left(n);
5403 ir_node *b = get_Shrs_right(n);
5404 ir_mode *mode = get_irn_mode(n);
5406 HANDLE_BINOP_PHI((eval_func) tarval_shrs, a, b, c, mode);
5407 n = transform_node_shift(n);
5410 n = transform_node_bitop_shift(n);
5413 } /* transform_node_Shrs */
5418 static ir_node *transform_node_Shl(ir_node *n) {
5419 ir_node *c, *oldn = n;
5420 ir_node *a = get_Shl_left(n);
5421 ir_node *b = get_Shl_right(n);
5422 ir_mode *mode = get_irn_mode(n);
5424 HANDLE_BINOP_PHI((eval_func) tarval_shl, a, b, c, mode);
5425 n = transform_node_shift(n);
5428 n = transform_node_shl_shr(n);
5430 n = transform_node_bitop_shift(n);
5433 } /* transform_node_Shl */
5438 static ir_node *transform_node_Rotl(ir_node *n) {
5439 ir_node *c, *oldn = n;
5440 ir_node *a = get_Rotl_left(n);
5441 ir_node *b = get_Rotl_right(n);
5442 ir_mode *mode = get_irn_mode(n);
5444 HANDLE_BINOP_PHI((eval_func) tarval_rotl, a, b, c, mode);
5445 n = transform_node_shift(n);
5448 n = transform_node_bitop_shift(n);
5451 } /* transform_node_Rotl */
5456 static ir_node *transform_node_Conv(ir_node *n) {
5457 ir_node *c, *oldn = n;
5458 ir_mode *mode = get_irn_mode(n);
5459 ir_node *a = get_Conv_op(n);
5461 if (mode != mode_b && is_const_Phi(a)) {
5462 /* Do NOT optimize mode_b Conv's, this leads to remaining
5463 * Phib nodes later, because the conv_b_lower operation
5464 * is instantly reverted, when it tries to insert a Convb.
5466 c = apply_conv_on_phi(a, mode);
5468 DBG_OPT_ALGSIM0(oldn, c, FS_OPT_CONST_PHI);
5473 if (is_Unknown(a)) { /* Conv_A(Unknown_B) -> Unknown_A */
5474 return new_r_Unknown(current_ir_graph, mode);
5477 if (mode_is_reference(mode) &&
5478 get_mode_size_bits(mode) == get_mode_size_bits(get_irn_mode(a)) &&
5480 ir_node *l = get_Add_left(a);
5481 ir_node *r = get_Add_right(a);
5482 dbg_info *dbgi = get_irn_dbg_info(a);
5483 ir_node *block = get_nodes_block(n);
5485 ir_node *lop = get_Conv_op(l);
5486 if(get_irn_mode(lop) == mode) {
5487 /* ConvP(AddI(ConvI(P), x)) -> AddP(P, x) */
5488 n = new_rd_Add(dbgi, block, lop, r, mode);
5493 ir_node *rop = get_Conv_op(r);
5494 if(get_irn_mode(rop) == mode) {
5495 /* ConvP(AddI(x, ConvI(P))) -> AddP(x, P) */
5496 n = new_rd_Add(dbgi, block, l, rop, mode);
5503 } /* transform_node_Conv */
5506 * Remove dead blocks and nodes in dead blocks
5507 * in keep alive list. We do not generate a new End node.
5509 static ir_node *transform_node_End(ir_node *n) {
5510 int i, j, n_keepalives = get_End_n_keepalives(n);
5513 NEW_ARR_A(ir_node *, in, n_keepalives);
5515 for (i = j = 0; i < n_keepalives; ++i) {
5516 ir_node *ka = get_End_keepalive(n, i);
5518 if (! is_Block_dead(ka)) {
5522 } else if (is_irn_pinned_in_irg(ka) && is_Block_dead(get_nodes_block(ka))) {
5524 } else if (is_Bad(ka)) {
5525 /* no need to keep Bad */
5530 if (j != n_keepalives)
5531 set_End_keepalives(n, j, in);
5533 } /* transform_node_End */
5535 bool is_negated_value(ir_node *a, ir_node *b)
5537 if (is_Minus(a) && get_Minus_op(a) == b)
5539 if (is_Minus(b) && get_Minus_op(b) == a)
5541 if (is_Sub(a) && is_Sub(b)) {
5542 ir_node *a_left = get_Sub_left(a);
5543 ir_node *a_right = get_Sub_right(a);
5544 ir_node *b_left = get_Sub_left(b);
5545 ir_node *b_right = get_Sub_right(b);
5547 if (a_left == b_right && a_right == b_left)
5555 * Optimize a Mux into some simpler cases.
5557 static ir_node *transform_node_Mux(ir_node *n) {
5558 ir_node *oldn = n, *sel = get_Mux_sel(n);
5559 ir_mode *mode = get_irn_mode(n);
5560 ir_node *t = get_Mux_true(n);
5561 ir_node *f = get_Mux_false(n);
5562 ir_graph *irg = current_ir_graph;
5564 if (is_irg_state(irg, IR_GRAPH_STATE_KEEP_MUX))
5568 ir_node* block = get_nodes_block(n);
5570 ir_node* c1 = get_Mux_sel(t);
5571 ir_node* t1 = get_Mux_true(t);
5572 ir_node* f1 = get_Mux_false(t);
5574 /* Mux(cond0, Mux(cond1, x, y), y) -> typical if (cond0 && cond1) x else y */
5575 ir_node* and_ = new_r_And(block, c0, c1, mode_b);
5576 ir_node* new_mux = new_r_Mux(block, and_, f1, t1, mode);
5581 DBG_OPT_ALGSIM0(oldn, t, FS_OPT_MUX_COMBINE);
5582 } else if (f == t1) {
5583 /* Mux(cond0, Mux(cond1, x, y), x) */
5584 ir_node* not_c1 = new_r_Not(block, c1, mode_b);
5585 ir_node* and_ = new_r_And(block, c0, not_c1, mode_b);
5586 ir_node* new_mux = new_r_Mux(block, and_, t1, f1, mode);
5591 DBG_OPT_ALGSIM0(oldn, t, FS_OPT_MUX_COMBINE);
5593 } else if (is_Mux(f)) {
5594 ir_node* block = get_nodes_block(n);
5596 ir_node* c1 = get_Mux_sel(f);
5597 ir_node* t1 = get_Mux_true(f);
5598 ir_node* f1 = get_Mux_false(f);
5600 /* Mux(cond0, x, Mux(cond1, x, y)) -> typical if (cond0 || cond1) x else y */
5601 ir_node* or_ = new_r_Or(block, c0, c1, mode_b);
5602 ir_node* new_mux = new_r_Mux(block, or_, f1, t1, mode);
5607 DBG_OPT_ALGSIM0(oldn, f, FS_OPT_MUX_COMBINE);
5608 } else if (t == f1) {
5609 /* Mux(cond0, x, Mux(cond1, y, x)) */
5610 ir_node* not_c1 = new_r_Not(block, c1, mode_b);
5611 ir_node* or_ = new_r_Or(block, c0, not_c1, mode_b);
5612 ir_node* new_mux = new_r_Mux(block, or_, t1, f1, mode);
5617 DBG_OPT_ALGSIM0(oldn, f, FS_OPT_MUX_COMBINE);
5621 /* first normalization step: move a possible zero to the false case */
5623 ir_node *cmp = get_Proj_pred(sel);
5626 if (is_Const(t) && is_Const_null(t)) {
5629 /* Mux(x, 0, y) => Mux(x, y, 0) */
5630 pn_Cmp pnc = get_Proj_proj(sel);
5631 sel = new_r_Proj(get_nodes_block(cmp), cmp, mode_b,
5632 get_negated_pnc(pnc, get_irn_mode(get_Cmp_left(cmp))));
5633 n = new_rd_Mux(get_irn_dbg_info(n), get_nodes_block(n), sel, t, f, mode);
5641 /* note: after normalization, false can only happen on default */
5642 if (mode == mode_b) {
5643 dbg_info *dbg = get_irn_dbg_info(n);
5644 ir_node *block = get_nodes_block(n);
5647 tarval *tv_t = get_Const_tarval(t);
5648 if (tv_t == tarval_b_true) {
5650 /* Muxb(sel, true, false) = sel */
5651 assert(get_Const_tarval(f) == tarval_b_false);
5652 DBG_OPT_ALGSIM0(oldn, sel, FS_OPT_MUX_BOOL);
5655 /* Muxb(sel, true, x) = Or(sel, x) */
5656 n = new_rd_Or(dbg, block, sel, f, mode_b);
5657 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_OR_BOOL);
5661 } else if (is_Const(f)) {
5662 tarval *tv_f = get_Const_tarval(f);
5663 if (tv_f == tarval_b_true) {
5664 /* Muxb(sel, x, true) = Or(Not(sel), x) */
5665 ir_node* not_sel = new_rd_Not(dbg, block, sel, mode_b);
5666 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_ORNOT_BOOL);
5667 n = new_rd_Or(dbg, block, not_sel, t, mode_b);
5670 /* Muxb(sel, x, false) = And(sel, x) */
5671 assert(tv_f == tarval_b_false);
5672 n = new_rd_And(dbg, block, sel, t, mode_b);
5673 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_AND_BOOL);
5679 /* more normalization: try to normalize Mux(x, C1, C2) into Mux(x, +1/-1, 0) op C2 */
5680 if (is_Const(t) && is_Const(f) && mode_is_int(mode)) {
5681 tarval *a = get_Const_tarval(t);
5682 tarval *b = get_Const_tarval(f);
5685 if (tarval_is_one(a) && tarval_is_null(b)) {
5686 ir_node *block = get_nodes_block(n);
5687 ir_node *conv = new_r_Conv(block, sel, mode);
5689 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_CONV);
5691 } else if (tarval_is_null(a) && tarval_is_one(b)) {
5692 ir_node *block = get_nodes_block(n);
5693 ir_node *not_ = new_r_Not(block, sel, mode_b);
5694 ir_node *conv = new_r_Conv(block, not_, mode);
5696 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_CONV);
5699 /* TODO: it's not really clear if that helps in general or should be moved
5700 * to backend, especially with the MUX->Conv transformation above */
5701 if (tarval_cmp(a, b) & pn_Cmp_Gt) {
5702 diff = tarval_sub(a, b, NULL);
5705 diff = tarval_sub(b, a, NULL);
5709 if (diff == get_tarval_one(mode)) {
5710 dbg_info *dbg = get_irn_dbg_info(n);
5711 ir_node *block = get_nodes_block(n);
5712 ir_node *t = new_Const(tarval_sub(a, min, NULL));
5713 ir_node *f = new_Const(tarval_sub(b, min, NULL));
5714 n = new_rd_Mux(dbg, block, sel, f, t, mode);
5715 n = new_rd_Add(dbg, block, n, new_Const(min), mode);
5721 ir_node *cmp = get_Proj_pred(sel);
5722 long pn = get_Proj_proj(sel);
5725 * Note: normalization puts the constant on the right side,
5726 * so we check only one case.
5728 * Note further that these optimization work even for floating point
5729 * with NaN's because -NaN == NaN.
5730 * However, if +0 and -0 is handled differently, we cannot use the Abs/-Abs
5734 ir_node *cmp_r = get_Cmp_right(cmp);
5735 if (is_Const(cmp_r) && is_Const_null(cmp_r)) {
5736 ir_node *block = get_nodes_block(n);
5737 ir_node *cmp_l = get_Cmp_left(cmp);
5739 if (!mode_honor_signed_zeros(mode) && is_negated_value(f, t)) {
5742 /* NaN's work fine with abs, so it is ok to remove Uo */
5743 long pnc = pn & ~pn_Cmp_Uo;
5745 if ( (cmp_l == t && (pnc == pn_Cmp_Ge || pnc == pn_Cmp_Gt))
5746 || (cmp_l == f && (pnc == pn_Cmp_Le || pnc == pn_Cmp_Lt)))
5748 /* Mux(a >/>= 0, a, -a) = Mux(a </<= 0, -a, a) ==> Abs(a) */
5749 n = new_rd_Abs(get_irn_dbg_info(n), block, cmp_l, mode);
5750 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_ABS);
5752 } else if ((cmp_l == t && (pnc == pn_Cmp_Le || pnc == pn_Cmp_Lt))
5753 || (cmp_l == f && (pnc == pn_Cmp_Ge || pnc == pn_Cmp_Gt)))
5755 /* Mux(a </<= 0, a, -a) = Mux(a >/>= 0, -a, a) ==> -Abs(a) */
5756 n = new_rd_Abs(get_irn_dbg_info(n), block, cmp_l, mode);
5757 n = new_rd_Minus(get_irn_dbg_info(n), block, n, mode);
5758 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_ABS);
5763 if (mode_is_int(mode)) {
5765 if ((pn == pn_Cmp_Lg || pn == pn_Cmp_Eq) && is_And(cmp_l)) {
5766 /* Mux((a & b) != 0, c, 0) */
5767 ir_node *and_r = get_And_right(cmp_l);
5770 if (and_r == t && f == cmp_r) {
5771 if (is_Const(t) && tarval_is_single_bit(get_Const_tarval(t))) {
5772 if (pn == pn_Cmp_Lg) {
5773 /* Mux((a & 2^C) != 0, 2^C, 0) */
5775 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_BITOP);
5777 /* Mux((a & 2^C) == 0, 2^C, 0) */
5778 n = new_rd_Eor(get_irn_dbg_info(n),
5779 block, cmp_l, t, mode);
5780 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_BITOP);
5785 if (is_Shl(and_r)) {
5786 ir_node *shl_l = get_Shl_left(and_r);
5787 if (is_Const(shl_l) && is_Const_one(shl_l)) {
5788 if (and_r == t && f == cmp_r) {
5789 if (pn == pn_Cmp_Lg) {
5790 /* (a & (1 << n)) != 0, (1 << n), 0) */
5792 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_BITOP);
5794 /* (a & (1 << n)) == 0, (1 << n), 0) */
5795 n = new_rd_Eor(get_irn_dbg_info(n),
5796 block, cmp_l, t, mode);
5797 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_BITOP);
5803 and_l = get_And_left(cmp_l);
5804 if (is_Shl(and_l)) {
5805 ir_node *shl_l = get_Shl_left(and_l);
5806 if (is_Const(shl_l) && is_Const_one(shl_l)) {
5807 if (and_l == t && f == cmp_r) {
5808 if (pn == pn_Cmp_Lg) {
5809 /* ((1 << n) & a) != 0, (1 << n), 0) */
5811 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_BITOP);
5813 /* ((1 << n) & a) == 0, (1 << n), 0) */
5814 n = new_rd_Eor(get_irn_dbg_info(n),
5815 block, cmp_l, t, mode);
5816 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_BITOP);
5829 } /* transform_node_Mux */
5832 * optimize Sync nodes that have other syncs as input we simply add the inputs
5833 * of the other sync to our own inputs
5835 static ir_node *transform_node_Sync(ir_node *n) {
5836 int arity = get_Sync_n_preds(n);
5839 for (i = 0; i < arity;) {
5840 ir_node *pred = get_Sync_pred(n, i);
5844 if (!is_Sync(pred)) {
5852 pred_arity = get_Sync_n_preds(pred);
5853 for (j = 0; j < pred_arity; ++j) {
5854 ir_node *pred_pred = get_Sync_pred(pred, j);
5859 add_irn_n(n, pred_pred);
5863 if (get_Sync_pred(n, k) == pred_pred) break;
5868 /* rehash the sync node */
5869 add_identities(current_ir_graph->value_table, n);
5872 } /* transform_node_Sync */
5875 * optimize a trampoline Call into a direct Call
5877 static ir_node *transform_node_Call(ir_node *call) {
5878 ir_node *callee = get_Call_ptr(call);
5879 ir_node *adr, *mem, *res, *bl, **in;
5880 ir_type *ctp, *mtp, *tp;
5883 int i, n_res, n_param;
5886 if (! is_Proj(callee))
5888 callee = get_Proj_pred(callee);
5889 if (! is_Builtin(callee))
5891 if (get_Builtin_kind(callee) != ir_bk_inner_trampoline)
5894 mem = get_Call_mem(call);
5896 if (skip_Proj(mem) == callee) {
5897 /* memory is routed to the trampoline, skip */
5898 mem = get_Builtin_mem(callee);
5901 /* build a new call type */
5902 mtp = get_Call_type(call);
5903 tdb = get_type_dbg_info(mtp);
5905 n_res = get_method_n_ress(mtp);
5906 n_param = get_method_n_params(mtp);
5907 ctp = new_d_type_method(n_param + 1, n_res, tdb);
5909 for (i = 0; i < n_res; ++i)
5910 set_method_res_type(ctp, i, get_method_res_type(mtp, i));
5912 NEW_ARR_A(ir_node *, in, n_param + 1);
5914 /* FIXME: we don't need a new pointer type in every step */
5915 tp = get_irg_frame_type(current_ir_graph);
5916 tp = new_type_pointer(tp);
5917 set_method_param_type(ctp, 0, tp);
5919 in[0] = get_Builtin_param(callee, 2);
5920 for (i = 0; i < n_param; ++i) {
5921 set_method_param_type(ctp, i + 1, get_method_param_type(mtp, i));
5922 in[i + 1] = get_Call_param(call, i);
5924 var = get_method_variadicity(mtp);
5925 set_method_variadicity(ctp, var);
5926 if (var == variadicity_variadic) {
5927 set_method_first_variadic_param_index(ctp, get_method_first_variadic_param_index(mtp) + 1);
5929 /* When we resolve a trampoline, the function must be called by a this-call */
5930 set_method_calling_convention(ctp, get_method_calling_convention(mtp) | cc_this_call);
5931 set_method_additional_properties(ctp, get_method_additional_properties(mtp));
5933 adr = get_Builtin_param(callee, 1);
5935 db = get_irn_dbg_info(call);
5936 bl = get_nodes_block(call);
5938 res = new_rd_Call(db, bl, mem, adr, n_param + 1, in, ctp);
5939 if (get_irn_pinned(call) == op_pin_state_floats)
5940 set_irn_pinned(res, op_pin_state_floats);
5942 } /* transform_node_Call */
5945 * Tries several [inplace] [optimizing] transformations and returns an
5946 * equivalent node. The difference to equivalent_node() is that these
5947 * transformations _do_ generate new nodes, and thus the old node must
5948 * not be freed even if the equivalent node isn't the old one.
5950 static ir_node *transform_node(ir_node *n) {
5954 * Transform_node is the only "optimizing transformation" that might
5955 * return a node with a different opcode. We iterate HERE until fixpoint
5956 * to get the final result.
5960 if (n->op->ops.transform_node != NULL)
5961 n = n->op->ops.transform_node(n);
5962 } while (oldn != n);
5965 } /* transform_node */
5968 * Sets the default transform node operation for an ir_op_ops.
5970 * @param code the opcode for the default operation
5971 * @param ops the operations initialized
5976 static ir_op_ops *firm_set_default_transform_node(ir_opcode code, ir_op_ops *ops)
5980 ops->transform_node = transform_node_##a; \
5982 #define CASE_PROJ(a) \
5984 ops->transform_node_Proj = transform_node_Proj_##a; \
5986 #define CASE_PROJ_EX(a) \
5988 ops->transform_node = transform_node_##a; \
5989 ops->transform_node_Proj = transform_node_Proj_##a; \
5998 CASE_PROJ_EX(DivMod);
6033 } /* firm_set_default_transform_node */
6036 /* **************** Common Subexpression Elimination **************** */
6038 /** The size of the hash table used, should estimate the number of nodes
6040 #define N_IR_NODES 512
6042 /** Compares the attributes of two Const nodes. */
6043 static int node_cmp_attr_Const(ir_node *a, ir_node *b) {
6044 return (get_Const_tarval(a) != get_Const_tarval(b))
6045 || (get_Const_type(a) != get_Const_type(b));
6046 } /* node_cmp_attr_Const */
6048 /** Compares the attributes of two Proj nodes. */
6049 static int node_cmp_attr_Proj(ir_node *a, ir_node *b) {
6050 return get_irn_proj_attr(a) != get_irn_proj_attr(b);
6051 } /* node_cmp_attr_Proj */
6053 /** Compares the attributes of two Filter nodes. */
6054 static int node_cmp_attr_Filter(ir_node *a, ir_node *b) {
6055 return get_Filter_proj(a) != get_Filter_proj(b);
6056 } /* node_cmp_attr_Filter */
6058 /** Compares the attributes of two Alloc nodes. */
6059 static int node_cmp_attr_Alloc(ir_node *a, ir_node *b) {
6060 const alloc_attr *pa = get_irn_alloc_attr(a);
6061 const alloc_attr *pb = get_irn_alloc_attr(b);
6062 return (pa->where != pb->where) || (pa->type != pb->type);
6063 } /* node_cmp_attr_Alloc */
6065 /** Compares the attributes of two Free nodes. */
6066 static int node_cmp_attr_Free(ir_node *a, ir_node *b) {
6067 const free_attr *pa = get_irn_free_attr(a);
6068 const free_attr *pb = get_irn_free_attr(b);
6069 return (pa->where != pb->where) || (pa->type != pb->type);
6070 } /* node_cmp_attr_Free */
6072 /** Compares the attributes of two SymConst nodes. */
6073 static int node_cmp_attr_SymConst(ir_node *a, ir_node *b) {
6074 const symconst_attr *pa = get_irn_symconst_attr(a);
6075 const symconst_attr *pb = get_irn_symconst_attr(b);
6076 return (pa->kind != pb->kind)
6077 || (pa->sym.type_p != pb->sym.type_p)
6078 || (pa->tp != pb->tp);
6079 } /* node_cmp_attr_SymConst */
6081 /** Compares the attributes of two Call nodes. */
6082 static int node_cmp_attr_Call(ir_node *a, ir_node *b) {
6083 const call_attr *pa = get_irn_call_attr(a);
6084 const call_attr *pb = get_irn_call_attr(b);
6085 return (pa->type != pb->type)
6086 || (pa->tail_call != pb->tail_call);
6087 } /* node_cmp_attr_Call */
6089 /** Compares the attributes of two Sel nodes. */
6090 static int node_cmp_attr_Sel(ir_node *a, ir_node *b) {
6091 const ir_entity *a_ent = get_Sel_entity(a);
6092 const ir_entity *b_ent = get_Sel_entity(b);
6095 (a_ent->kind != b_ent->kind) ||
6096 (a_ent->name != b_ent->name) ||
6097 (a_ent->owner != b_ent->owner) ||
6098 (a_ent->ld_name != b_ent->ld_name) ||
6099 (a_ent->type != b_ent->type);
6101 /* Matze: inlining of functions can produce 2 entities with same type,
6103 return a_ent != b_ent;
6104 } /* node_cmp_attr_Sel */
6106 /** Compares the attributes of two Phi nodes. */
6107 static int node_cmp_attr_Phi(ir_node *a, ir_node *b) {
6108 /* we can only enter this function if both nodes have the same number of inputs,
6109 hence it is enough to check if one of them is a Phi0 */
6111 /* check the Phi0 pos attribute */
6112 return get_irn_phi_attr(a)->u.pos != get_irn_phi_attr(b)->u.pos;
6115 } /* node_cmp_attr_Phi */
6117 /** Compares the attributes of two Conv nodes. */
6118 static int node_cmp_attr_Conv(ir_node *a, ir_node *b) {
6119 return get_Conv_strict(a) != get_Conv_strict(b);
6120 } /* node_cmp_attr_Conv */
6122 /** Compares the attributes of two Cast nodes. */
6123 static int node_cmp_attr_Cast(ir_node *a, ir_node *b) {
6124 return get_Cast_type(a) != get_Cast_type(b);
6125 } /* node_cmp_attr_Cast */
6127 /** Compares the attributes of two Load nodes. */
6128 static int node_cmp_attr_Load(ir_node *a, ir_node *b) {
6129 if (get_Load_volatility(a) == volatility_is_volatile ||
6130 get_Load_volatility(b) == volatility_is_volatile)
6131 /* NEVER do CSE on volatile Loads */
6133 /* do not CSE Loads with different alignment. Be conservative. */
6134 if (get_Load_align(a) != get_Load_align(b))
6137 return get_Load_mode(a) != get_Load_mode(b);
6138 } /* node_cmp_attr_Load */
6140 /** Compares the attributes of two Store nodes. */
6141 static int node_cmp_attr_Store(ir_node *a, ir_node *b) {
6142 /* do not CSE Stores with different alignment. Be conservative. */
6143 if (get_Store_align(a) != get_Store_align(b))
6146 /* NEVER do CSE on volatile Stores */
6147 return (get_Store_volatility(a) == volatility_is_volatile ||
6148 get_Store_volatility(b) == volatility_is_volatile);
6149 } /* node_cmp_attr_Store */
6151 /** Compares two exception attributes */
6152 static int node_cmp_exception(ir_node *a, ir_node *b) {
6153 const except_attr *ea = get_irn_except_attr(a);
6154 const except_attr *eb = get_irn_except_attr(b);
6156 return ea->pin_state != eb->pin_state;
6159 #define node_cmp_attr_Bound node_cmp_exception
6161 /** Compares the attributes of two Div nodes. */
6162 static int node_cmp_attr_Div(ir_node *a, ir_node *b) {
6163 const divmod_attr *ma = get_irn_divmod_attr(a);
6164 const divmod_attr *mb = get_irn_divmod_attr(b);
6165 return ma->exc.pin_state != mb->exc.pin_state ||
6166 ma->resmode != mb->resmode ||
6167 ma->no_remainder != mb->no_remainder;
6168 } /* node_cmp_attr_Div */
6170 /** Compares the attributes of two DivMod nodes. */
6171 static int node_cmp_attr_DivMod(ir_node *a, ir_node *b) {
6172 const divmod_attr *ma = get_irn_divmod_attr(a);
6173 const divmod_attr *mb = get_irn_divmod_attr(b);
6174 return ma->exc.pin_state != mb->exc.pin_state ||
6175 ma->resmode != mb->resmode;
6176 } /* node_cmp_attr_DivMod */
6178 /** Compares the attributes of two Mod nodes. */
6179 static int node_cmp_attr_Mod(ir_node *a, ir_node *b) {
6180 const divmod_attr *ma = get_irn_divmod_attr(a);
6181 const divmod_attr *mb = get_irn_divmod_attr(b);
6182 return ma->exc.pin_state != mb->exc.pin_state ||
6183 ma->resmode != mb->resmode;
6184 } /* node_cmp_attr_Mod */
6186 /** Compares the attributes of two Quot nodes. */
6187 static int node_cmp_attr_Quot(ir_node *a, ir_node *b) {
6188 const divmod_attr *ma = get_irn_divmod_attr(a);
6189 const divmod_attr *mb = get_irn_divmod_attr(b);
6190 return ma->exc.pin_state != mb->exc.pin_state ||
6191 ma->resmode != mb->resmode;
6192 } /* node_cmp_attr_Quot */
6194 /** Compares the attributes of two Confirm nodes. */
6195 static int node_cmp_attr_Confirm(ir_node *a, ir_node *b) {
6196 /* no need to compare the bound, as this is a input */
6197 return (get_Confirm_cmp(a) != get_Confirm_cmp(b));
6198 } /* node_cmp_attr_Confirm */
6200 /** Compares the attributes of two Builtin nodes. */
6201 static int node_cmp_attr_Builtin(ir_node *a, ir_node *b) {
6202 const builtin_attr *ma = get_irn_builtin_attr(a);
6203 const builtin_attr *mb = get_irn_builtin_attr(b);
6205 /* no need to compare the type, equal kind means equal type */
6206 return ma->kind != mb->kind;
6207 } /* node_cmp_attr_Builtin */
6209 /** Compares the attributes of two ASM nodes. */
6210 static int node_cmp_attr_ASM(ir_node *a, ir_node *b) {
6212 const ir_asm_constraint *ca;
6213 const ir_asm_constraint *cb;
6216 if (get_ASM_text(a) != get_ASM_text(b))
6219 /* Should we really check the constraints here? Should be better, but is strange. */
6220 n = get_ASM_n_input_constraints(a);
6221 if (n != get_ASM_n_input_constraints(b))
6224 ca = get_ASM_input_constraints(a);
6225 cb = get_ASM_input_constraints(b);
6226 for (i = 0; i < n; ++i) {
6227 if (ca[i].pos != cb[i].pos || ca[i].constraint != cb[i].constraint)
6231 n = get_ASM_n_output_constraints(a);
6232 if (n != get_ASM_n_output_constraints(b))
6235 ca = get_ASM_output_constraints(a);
6236 cb = get_ASM_output_constraints(b);
6237 for (i = 0; i < n; ++i) {
6238 if (ca[i].pos != cb[i].pos || ca[i].constraint != cb[i].constraint)
6242 n = get_ASM_n_clobbers(a);
6243 if (n != get_ASM_n_clobbers(b))
6246 cla = get_ASM_clobbers(a);
6247 clb = get_ASM_clobbers(b);
6248 for (i = 0; i < n; ++i) {
6249 if (cla[i] != clb[i])
6253 } /* node_cmp_attr_ASM */
6255 /** Compares the inexistent attributes of two Dummy nodes. */
6256 static int node_cmp_attr_Dummy(ir_node *a, ir_node *b)
6264 * Set the default node attribute compare operation for an ir_op_ops.
6266 * @param code the opcode for the default operation
6267 * @param ops the operations initialized
6272 static ir_op_ops *firm_set_default_node_cmp_attr(ir_opcode code, ir_op_ops *ops)
6276 ops->node_cmp_attr = node_cmp_attr_##a; \
6309 } /* firm_set_default_node_cmp_attr */
6312 * Compare function for two nodes in the value table. Gets two
6313 * nodes as parameters. Returns 0 if the nodes are a Common Sub Expression.
6315 int identities_cmp(const void *elt, const void *key) {
6316 ir_node *a = (ir_node *)elt;
6317 ir_node *b = (ir_node *)key;
6320 if (a == b) return 0;
6322 if ((get_irn_op(a) != get_irn_op(b)) ||
6323 (get_irn_mode(a) != get_irn_mode(b))) return 1;
6325 /* compare if a's in and b's in are of equal length */
6326 irn_arity_a = get_irn_intra_arity(a);
6327 if (irn_arity_a != get_irn_intra_arity(b))
6330 if (get_irn_pinned(a) == op_pin_state_pinned) {
6331 /* for pinned nodes, the block inputs must be equal */
6332 if (get_irn_intra_n(a, -1) != get_irn_intra_n(b, -1))
6334 } else if (! get_opt_global_cse()) {
6335 /* for block-local CSE both nodes must be in the same MacroBlock */
6336 if (get_irn_MacroBlock(a) != get_irn_MacroBlock(b))
6340 /* compare a->in[0..ins] with b->in[0..ins] */
6341 for (i = 0; i < irn_arity_a; ++i) {
6342 ir_node *pred_a = get_irn_intra_n(a, i);
6343 ir_node *pred_b = get_irn_intra_n(b, i);
6344 if (pred_a != pred_b) {
6345 /* if both predecessors are CSE neutral they might be different */
6346 if (!is_irn_cse_neutral(pred_a) || !is_irn_cse_neutral(pred_b))
6352 * here, we already now that the nodes are identical except their
6355 if (a->op->ops.node_cmp_attr)
6356 return a->op->ops.node_cmp_attr(a, b);
6359 } /* identities_cmp */
6362 * Calculate a hash value of a node.
6364 * @param node The IR-node
6366 unsigned ir_node_hash(const ir_node *node) {
6367 return node->op->ops.hash(node);
6368 } /* ir_node_hash */
6371 pset *new_identities(void) {
6372 return new_pset(identities_cmp, N_IR_NODES);
6373 } /* new_identities */
6375 void del_identities(pset *value_table) {
6376 del_pset(value_table);
6377 } /* del_identities */
6379 /* Normalize a node by putting constants (and operands with larger
6380 * node index) on the right (operator side). */
6381 void ir_normalize_node(ir_node *n) {
6382 if (is_op_commutative(get_irn_op(n))) {
6383 ir_node *l = get_binop_left(n);
6384 ir_node *r = get_binop_right(n);
6386 /* For commutative operators perform a OP b == b OP a but keep
6387 * constants on the RIGHT side. This helps greatly in some
6388 * optimizations. Moreover we use the idx number to make the form
6390 if (!operands_are_normalized(l, r)) {
6391 set_binop_left(n, r);
6392 set_binop_right(n, l);
6396 } /* ir_normalize_node */
6399 * Update the nodes after a match in the value table. If both nodes have
6400 * the same MacroBlock but different Blocks, we must ensure that the node
6401 * with the dominating Block (the node that is near to the MacroBlock header
6402 * is stored in the table.
6403 * Because a MacroBlock has only one "non-exception" flow, we don't need
6404 * dominance info here: We known, that one block must dominate the other and
6405 * following the only block input will allow to find it.
6407 static void update_known_irn(ir_node *known_irn, const ir_node *new_ir_node) {
6408 ir_node *known_blk, *new_block, *block, *mbh;
6410 if (get_opt_global_cse()) {
6411 /* Block inputs are meaning less */
6414 known_blk = get_irn_n(known_irn, -1);
6415 new_block = get_irn_n(new_ir_node, -1);
6416 if (known_blk == new_block) {
6417 /* already in the same block */
6421 * We expect the typical case when we built the graph. In that case, the
6422 * known_irn is already the upper one, so checking this should be faster.
6425 mbh = get_Block_MacroBlock(new_block);
6427 if (block == known_blk) {
6428 /* ok, we have found it: known_block dominates new_block as expected */
6433 * We have reached the MacroBlock header NOT founding
6434 * the known_block. new_block must dominate known_block.
6437 set_irn_n(known_irn, -1, new_block);
6440 assert(get_Block_n_cfgpreds(block) == 1);
6441 block = get_Block_cfgpred_block(block, 0);
6443 } /* update_value_table */
6446 * Return the canonical node computing the same value as n.
6447 * Looks up the node in a hash table, enters it in the table
6448 * if it isn't there yet.
6450 * @param value_table the HashSet containing all nodes in the
6452 * @param n the node to look up
6454 * @return a node that computes the same value as n or n if no such
6455 * node could be found
6457 ir_node *identify_remember(pset *value_table, ir_node *n) {
6460 if (!value_table) return n;
6462 ir_normalize_node(n);
6463 /* lookup or insert in hash table with given hash key. */
6464 nn = pset_insert(value_table, n, ir_node_hash(n));
6467 update_known_irn(nn, n);
6469 /* n is reachable again */
6470 edges_node_revival(nn, get_irn_irg(nn));
6474 } /* identify_remember */
6477 * During construction we set the op_pin_state_pinned flag in the graph right when the
6478 * optimization is performed. The flag turning on procedure global cse could
6479 * be changed between two allocations. This way we are safe.
6481 * @param value_table The value table
6482 * @param n The node to lookup
6484 static inline ir_node *identify_cons(pset *value_table, ir_node *n) {
6487 n = identify_remember(value_table, n);
6488 if (n != old && get_irn_MacroBlock(old) != get_irn_MacroBlock(n))
6489 set_irg_pinned(current_ir_graph, op_pin_state_floats);
6491 } /* identify_cons */
6493 /* Add a node to the identities value table. */
6494 void add_identities(pset *value_table, ir_node *node) {
6495 if (get_opt_cse() && is_no_Block(node))
6496 identify_remember(value_table, node);
6497 } /* add_identities */
6499 /* Visit each node in the value table of a graph. */
6500 void visit_all_identities(ir_graph *irg, irg_walk_func visit, void *env) {
6502 ir_graph *rem = current_ir_graph;
6504 current_ir_graph = irg;
6505 foreach_pset(irg->value_table, node)
6507 current_ir_graph = rem;
6508 } /* visit_all_identities */
6511 * Garbage in, garbage out. If a node has a dead input, i.e., the
6512 * Bad node is input to the node, return the Bad node.
6514 static ir_node *gigo(ir_node *node) {
6516 ir_op *op = get_irn_op(node);
6518 /* remove garbage blocks by looking at control flow that leaves the block
6519 and replacing the control flow by Bad. */
6520 if (get_irn_mode(node) == mode_X) {
6521 ir_node *block = get_nodes_block(skip_Proj(node));
6523 /* Don't optimize nodes in immature blocks. */
6524 if (!get_Block_matured(block))
6526 /* Don't optimize End, may have Bads. */
6527 if (op == op_End) return node;
6529 if (is_Block(block)) {
6530 if (is_Block_dead(block)) {
6531 /* control flow from dead block is dead */
6535 for (i = get_irn_arity(block) - 1; i >= 0; --i) {
6536 if (!is_Bad(get_irn_n(block, i)))
6540 ir_graph *irg = get_irn_irg(block);
6541 /* the start block is never dead */
6542 if (block != get_irg_start_block(irg)
6543 && block != get_irg_end_block(irg)) {
6545 * Do NOT kill control flow without setting
6546 * the block to dead of bad things can happen:
6547 * We get a Block that is not reachable be irg_block_walk()
6548 * but can be found by irg_walk()!
6550 set_Block_dead(block);
6557 /* Blocks, Phis and Tuples may have dead inputs, e.g., if one of the
6558 blocks predecessors is dead. */
6559 if (op != op_Block && op != op_Phi && op != op_Tuple) {
6560 irn_arity = get_irn_arity(node);
6563 * Beware: we can only read the block of a non-floating node.
6565 if (is_irn_pinned_in_irg(node) &&
6566 is_Block_dead(get_nodes_block(skip_Proj(node))))
6569 for (i = 0; i < irn_arity; i++) {
6570 ir_node *pred = get_irn_n(node, i);
6575 /* Propagating Unknowns here seems to be a bad idea, because
6576 sometimes we need a node as a input and did not want that
6578 However, it might be useful to move this into a later phase
6579 (if you think that optimizing such code is useful). */
6580 if (is_Unknown(pred) && mode_is_data(get_irn_mode(node)))
6581 return new_Unknown(get_irn_mode(node));
6586 /* With this code we violate the agreement that local_optimize
6587 only leaves Bads in Block, Phi and Tuple nodes. */
6588 /* If Block has only Bads as predecessors it's garbage. */
6589 /* If Phi has only Bads as predecessors it's garbage. */
6590 if ((op == op_Block && get_Block_matured(node)) || op == op_Phi) {
6591 irn_arity = get_irn_arity(node);
6592 for (i = 0; i < irn_arity; i++) {
6593 if (!is_Bad(get_irn_n(node, i))) break;
6595 if (i == irn_arity) node = new_Bad();
6602 * These optimizations deallocate nodes from the obstack.
6603 * It can only be called if it is guaranteed that no other nodes
6604 * reference this one, i.e., right after construction of a node.
6606 * @param n The node to optimize
6608 * current_ir_graph must be set to the graph of the node!
6610 ir_node *optimize_node(ir_node *n) {
6613 ir_opcode iro = get_irn_opcode(n);
6615 /* Always optimize Phi nodes: part of the construction. */
6616 if ((!get_opt_optimize()) && (iro != iro_Phi)) return n;
6618 /* constant expression evaluation / constant folding */
6619 if (get_opt_constant_folding()) {
6620 /* neither constants nor Tuple values can be evaluated */
6621 if (iro != iro_Const && (get_irn_mode(n) != mode_T)) {
6622 unsigned fp_model = get_irg_fp_model(current_ir_graph);
6623 int old_fp_mode = tarval_fp_ops_enabled();
6625 tarval_enable_fp_ops(! (fp_model & fp_no_float_fold));
6627 /* try to evaluate */
6628 tv = computed_value(n);
6629 if (tv != tarval_bad) {
6631 ir_type *old_tp = get_irn_type(n);
6632 int i, arity = get_irn_arity(n);
6636 * Try to recover the type of the new expression.
6638 for (i = 0; i < arity && !old_tp; ++i)
6639 old_tp = get_irn_type(get_irn_n(n, i));
6642 * we MUST copy the node here temporary, because it's still needed
6643 * for DBG_OPT_CSTEVAL
6645 node_size = offsetof(ir_node, attr) + n->op->attr_size;
6646 oldn = alloca(node_size);
6648 memcpy(oldn, n, node_size);
6649 CLONE_ARR_A(ir_node *, oldn->in, n->in);
6651 /* ARG, copy the in array, we need it for statistics */
6652 memcpy(oldn->in, n->in, ARR_LEN(n->in) * sizeof(n->in[0]));
6654 /* note the inplace edges module */
6655 edges_node_deleted(n, current_ir_graph);
6657 /* evaluation was successful -- replace the node. */
6658 irg_kill_node(current_ir_graph, n);
6661 if (old_tp && get_type_mode(old_tp) == get_tarval_mode(tv))
6662 set_Const_type(nw, old_tp);
6663 DBG_OPT_CSTEVAL(oldn, nw);
6664 tarval_enable_fp_ops(old_fp_mode);
6667 tarval_enable_fp_ops(old_fp_mode);
6671 /* remove unnecessary nodes */
6672 if (get_opt_algebraic_simplification() ||
6673 (iro == iro_Phi) || /* always optimize these nodes. */
6675 (iro == iro_Proj) ||
6676 (iro == iro_Block) ) /* Flags tested local. */
6677 n = equivalent_node(n);
6679 /* Common Subexpression Elimination.
6681 * Checks whether n is already available.
6682 * The block input is used to distinguish different subexpressions. Right
6683 * now all nodes are op_pin_state_pinned to blocks, i.e., the CSE only finds common
6684 * subexpressions within a block.
6687 n = identify_cons(current_ir_graph->value_table, n);
6690 edges_node_deleted(oldn, current_ir_graph);
6692 /* We found an existing, better node, so we can deallocate the old node. */
6693 irg_kill_node(current_ir_graph, oldn);
6697 /* Some more constant expression evaluation that does not allow to
6699 iro = get_irn_opcode(n);
6700 if (get_opt_algebraic_simplification() ||
6701 (iro == iro_Cond) ||
6702 (iro == iro_Proj)) /* Flags tested local. */
6703 n = transform_node(n);
6705 /* Remove nodes with dead (Bad) input.
6706 Run always for transformation induced Bads. */
6709 /* Now we have a legal, useful node. Enter it in hash table for CSE */
6710 if (get_opt_cse() && (get_irn_opcode(n) != iro_Block)) {
6712 n = identify_remember(current_ir_graph->value_table, o);
6718 } /* optimize_node */
6722 * These optimizations never deallocate nodes (in place). This can cause dead
6723 * nodes lying on the obstack. Remove these by a dead node elimination,
6724 * i.e., a copying garbage collection.
6726 ir_node *optimize_in_place_2(ir_node *n) {
6729 ir_opcode iro = get_irn_opcode(n);
6731 if (!get_opt_optimize() && !is_Phi(n)) return n;
6733 /* constant expression evaluation / constant folding */
6734 if (get_opt_constant_folding()) {
6735 /* neither constants nor Tuple values can be evaluated */
6736 if (iro != iro_Const && get_irn_mode(n) != mode_T) {
6737 unsigned fp_model = get_irg_fp_model(current_ir_graph);
6738 int old_fp_mode = tarval_fp_ops_enabled();
6740 tarval_enable_fp_ops((fp_model & fp_strict_algebraic) == 0);
6741 /* try to evaluate */
6742 tv = computed_value(n);
6743 if (tv != tarval_bad) {
6744 /* evaluation was successful -- replace the node. */
6745 ir_type *old_tp = get_irn_type(n);
6746 int i, arity = get_irn_arity(n);
6749 * Try to recover the type of the new expression.
6751 for (i = 0; i < arity && !old_tp; ++i)
6752 old_tp = get_irn_type(get_irn_n(n, i));
6756 if (old_tp && get_type_mode(old_tp) == get_tarval_mode(tv))
6757 set_Const_type(n, old_tp);
6759 DBG_OPT_CSTEVAL(oldn, n);
6760 tarval_enable_fp_ops(old_fp_mode);
6763 tarval_enable_fp_ops(old_fp_mode);
6767 /* remove unnecessary nodes */
6768 if (get_opt_constant_folding() ||
6769 (iro == iro_Phi) || /* always optimize these nodes. */
6770 (iro == iro_Id) || /* ... */
6771 (iro == iro_Proj) || /* ... */
6772 (iro == iro_Block) ) /* Flags tested local. */
6773 n = equivalent_node(n);
6775 /** common subexpression elimination **/
6776 /* Checks whether n is already available. */
6777 /* The block input is used to distinguish different subexpressions. Right
6778 now all nodes are op_pin_state_pinned to blocks, i.e., the cse only finds common
6779 subexpressions within a block. */
6780 if (get_opt_cse()) {
6782 n = identify_remember(current_ir_graph->value_table, o);
6787 /* Some more constant expression evaluation. */
6788 iro = get_irn_opcode(n);
6789 if (get_opt_constant_folding() ||
6790 (iro == iro_Cond) ||
6791 (iro == iro_Proj)) /* Flags tested local. */
6792 n = transform_node(n);
6794 /* Remove nodes with dead (Bad) input.
6795 Run always for transformation induced Bads. */
6798 /* Now we can verify the node, as it has no dead inputs any more. */
6801 /* Now we have a legal, useful node. Enter it in hash table for cse.
6802 Blocks should be unique anyways. (Except the successor of start:
6803 is cse with the start block!) */
6804 if (get_opt_cse() && (get_irn_opcode(n) != iro_Block)) {
6806 n = identify_remember(current_ir_graph->value_table, o);
6812 } /* optimize_in_place_2 */
6815 * Wrapper for external use, set proper status bits after optimization.
6817 ir_node *optimize_in_place(ir_node *n) {
6818 /* Handle graph state */
6819 assert(get_irg_phase_state(current_ir_graph) != phase_building);
6821 if (get_opt_global_cse())
6822 set_irg_pinned(current_ir_graph, op_pin_state_floats);
6823 if (get_irg_outs_state(current_ir_graph) == outs_consistent)
6824 set_irg_outs_inconsistent(current_ir_graph);
6826 /* FIXME: Maybe we could also test whether optimizing the node can
6827 change the control graph. */
6828 set_irg_doms_inconsistent(current_ir_graph);
6829 return optimize_in_place_2(n);
6830 } /* optimize_in_place */
6833 * Calculate a hash value of a Const node.
6835 static unsigned hash_Const(const ir_node *node) {
6838 /* special value for const, as they only differ in their tarval. */
6839 h = HASH_PTR(node->attr.con.tv);
6845 * Calculate a hash value of a SymConst node.
6847 static unsigned hash_SymConst(const ir_node *node) {
6850 /* all others are pointers */
6851 h = HASH_PTR(node->attr.symc.sym.type_p);
6854 } /* hash_SymConst */
6857 * Set the default hash operation in an ir_op_ops.
6859 * @param code the opcode for the default operation
6860 * @param ops the operations initialized
6865 static ir_op_ops *firm_set_default_hash(ir_opcode code, ir_op_ops *ops)
6869 ops->hash = hash_##a; \
6872 /* hash function already set */
6873 if (ops->hash != NULL)
6880 /* use input/mode default hash if no function was given */
6881 ops->hash = firm_default_hash;
6889 * Sets the default operation for an ir_ops.
6891 ir_op_ops *firm_set_default_operations(ir_opcode code, ir_op_ops *ops) {
6892 ops = firm_set_default_hash(code, ops);
6893 ops = firm_set_default_computed_value(code, ops);
6894 ops = firm_set_default_equivalent_node(code, ops);
6895 ops = firm_set_default_transform_node(code, ops);
6896 ops = firm_set_default_node_cmp_attr(code, ops);
6897 ops = firm_set_default_get_type(code, ops);
6898 ops = firm_set_default_get_type_attr(code, ops);
6899 ops = firm_set_default_get_entity_attr(code, ops);
6902 } /* firm_set_default_operations */