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"
46 #include "opt_confirms.h"
47 #include "opt_polymorphy.h"
52 /* Make types visible to allow most efficient access */
56 * Returns the tarval of a Const node or tarval_bad for all other nodes.
58 static tarval *default_value_of(const ir_node *n) {
60 return get_Const_tarval(n); /* might return tarval_bad */
65 value_of_func value_of_ptr = default_value_of;
67 /* * Set a new value_of function. */
68 void set_value_of_func(value_of_func func) {
72 value_of_ptr = default_value_of;
76 * Return the value of a Constant.
78 static tarval *computed_value_Const(const ir_node *n) {
79 return get_Const_tarval(n);
80 } /* computed_value_Const */
83 * Return the value of a 'sizeof', 'alignof' or 'offsetof' SymConst.
85 static tarval *computed_value_SymConst(const ir_node *n) {
89 switch (get_SymConst_kind(n)) {
90 case symconst_type_size:
91 type = get_SymConst_type(n);
92 if (get_type_state(type) == layout_fixed)
93 return new_tarval_from_long(get_type_size_bytes(type), get_irn_mode(n));
95 case symconst_type_align:
96 type = get_SymConst_type(n);
97 if (get_type_state(type) == layout_fixed)
98 return new_tarval_from_long(get_type_alignment_bytes(type), get_irn_mode(n));
100 case symconst_ofs_ent:
101 ent = get_SymConst_entity(n);
102 type = get_entity_owner(ent);
103 if (get_type_state(type) == layout_fixed)
104 return new_tarval_from_long(get_entity_offset(ent), get_irn_mode(n));
110 } /* computed_value_SymConst */
113 * Return the value of an Add.
115 static tarval *computed_value_Add(const ir_node *n) {
116 ir_node *a = get_Add_left(n);
117 ir_node *b = get_Add_right(n);
119 tarval *ta = value_of(a);
120 tarval *tb = value_of(b);
122 if ((ta != tarval_bad) && (tb != tarval_bad))
123 return tarval_add(ta, tb);
126 } /* computed_value_Add */
129 * Return the value of a Sub.
130 * Special case: a - a
132 static tarval *computed_value_Sub(const ir_node *n) {
133 ir_mode *mode = get_irn_mode(n);
134 ir_node *a = get_Sub_left(n);
135 ir_node *b = get_Sub_right(n);
140 if (! mode_is_float(mode)) {
143 return get_mode_null(mode);
149 if ((ta != tarval_bad) && (tb != tarval_bad))
150 return tarval_sub(ta, tb, mode);
153 } /* computed_value_Sub */
156 * Return the value of a Carry.
157 * Special : a op 0, 0 op b
159 static tarval *computed_value_Carry(const ir_node *n) {
160 ir_node *a = get_binop_left(n);
161 ir_node *b = get_binop_right(n);
162 ir_mode *m = get_irn_mode(n);
164 tarval *ta = value_of(a);
165 tarval *tb = value_of(b);
167 if ((ta != tarval_bad) && (tb != tarval_bad)) {
169 return tarval_carry() ? get_mode_one(m) : get_mode_null(m);
171 if (tarval_is_null(ta) || tarval_is_null(tb))
172 return get_mode_null(m);
175 } /* computed_value_Carry */
178 * Return the value of a Borrow.
181 static tarval *computed_value_Borrow(const ir_node *n) {
182 ir_node *a = get_binop_left(n);
183 ir_node *b = get_binop_right(n);
184 ir_mode *m = get_irn_mode(n);
186 tarval *ta = value_of(a);
187 tarval *tb = value_of(b);
189 if ((ta != tarval_bad) && (tb != tarval_bad)) {
190 return tarval_cmp(ta, tb) == pn_Cmp_Lt ? get_mode_one(m) : get_mode_null(m);
191 } else if (tarval_is_null(ta)) {
192 return get_mode_null(m);
195 } /* computed_value_Borrow */
198 * Return the value of an unary Minus.
200 static tarval *computed_value_Minus(const ir_node *n) {
201 ir_node *a = get_Minus_op(n);
202 tarval *ta = value_of(a);
204 if (ta != tarval_bad)
205 return tarval_neg(ta);
208 } /* computed_value_Minus */
211 * Return the value of a Mul.
213 static tarval *computed_value_Mul(const ir_node *n) {
214 ir_node *a = get_Mul_left(n);
215 ir_node *b = get_Mul_right(n);
218 tarval *ta = value_of(a);
219 tarval *tb = value_of(b);
221 mode = get_irn_mode(n);
222 if (mode != get_irn_mode(a)) {
223 /* n * n = 2n bit multiplication */
224 ta = tarval_convert_to(ta, mode);
225 tb = tarval_convert_to(tb, mode);
228 if (ta != tarval_bad && tb != tarval_bad) {
229 return tarval_mul(ta, tb);
231 /* a * 0 != 0 if a == NaN or a == Inf */
232 if (!mode_is_float(mode)) {
233 /* a*0 = 0 or 0*b = 0 */
234 if (ta == get_mode_null(mode))
236 if (tb == get_mode_null(mode))
241 } /* computed_value_Mul */
244 * Return the value of an Abs.
246 static tarval *computed_value_Abs(const ir_node *n) {
247 ir_node *a = get_Abs_op(n);
248 tarval *ta = value_of(a);
250 if (ta != tarval_bad)
251 return tarval_abs(ta);
254 } /* computed_value_Abs */
257 * Return the value of an And.
258 * Special case: a & 0, 0 & b
260 static tarval *computed_value_And(const ir_node *n) {
261 ir_node *a = get_And_left(n);
262 ir_node *b = get_And_right(n);
264 tarval *ta = value_of(a);
265 tarval *tb = value_of(b);
267 if ((ta != tarval_bad) && (tb != tarval_bad)) {
268 return tarval_and (ta, tb);
270 if (tarval_is_null(ta)) return ta;
271 if (tarval_is_null(tb)) return tb;
274 } /* computed_value_And */
277 * Return the value of an Or.
278 * Special case: a | 1...1, 1...1 | b
280 static tarval *computed_value_Or(const ir_node *n) {
281 ir_node *a = get_Or_left(n);
282 ir_node *b = get_Or_right(n);
284 tarval *ta = value_of(a);
285 tarval *tb = value_of(b);
287 if ((ta != tarval_bad) && (tb != tarval_bad)) {
288 return tarval_or (ta, tb);
290 if (tarval_is_all_one(ta)) return ta;
291 if (tarval_is_all_one(tb)) return tb;
294 } /* computed_value_Or */
297 * Return the value of an Eor.
299 static tarval *computed_value_Eor(const ir_node *n) {
300 ir_node *a = get_Eor_left(n);
301 ir_node *b = get_Eor_right(n);
306 return get_mode_null(get_irn_mode(n));
311 if ((ta != tarval_bad) && (tb != tarval_bad)) {
312 return tarval_eor (ta, tb);
315 } /* computed_value_Eor */
318 * Return the value of a Not.
320 static tarval *computed_value_Not(const ir_node *n) {
321 ir_node *a = get_Not_op(n);
322 tarval *ta = value_of(a);
324 if (ta != tarval_bad)
325 return tarval_not(ta);
328 } /* computed_value_Not */
331 * Return the value of a Shl.
333 static tarval *computed_value_Shl(const ir_node *n) {
334 ir_node *a = get_Shl_left(n);
335 ir_node *b = get_Shl_right(n);
337 tarval *ta = value_of(a);
338 tarval *tb = value_of(b);
340 if ((ta != tarval_bad) && (tb != tarval_bad)) {
341 return tarval_shl (ta, tb);
344 } /* computed_value_Shl */
347 * Return the value of a Shr.
349 static tarval *computed_value_Shr(const ir_node *n) {
350 ir_node *a = get_Shr_left(n);
351 ir_node *b = get_Shr_right(n);
353 tarval *ta = value_of(a);
354 tarval *tb = value_of(b);
356 if ((ta != tarval_bad) && (tb != tarval_bad)) {
357 return tarval_shr (ta, tb);
360 } /* computed_value_Shr */
363 * Return the value of a Shrs.
365 static tarval *computed_value_Shrs(const ir_node *n) {
366 ir_node *a = get_Shrs_left(n);
367 ir_node *b = get_Shrs_right(n);
369 tarval *ta = value_of(a);
370 tarval *tb = value_of(b);
372 if ((ta != tarval_bad) && (tb != tarval_bad)) {
373 return tarval_shrs (ta, tb);
376 } /* computed_value_Shrs */
379 * Return the value of a Rotl.
381 static tarval *computed_value_Rotl(const ir_node *n) {
382 ir_node *a = get_Rotl_left(n);
383 ir_node *b = get_Rotl_right(n);
385 tarval *ta = value_of(a);
386 tarval *tb = value_of(b);
388 if ((ta != tarval_bad) && (tb != tarval_bad)) {
389 return tarval_rotl(ta, tb);
392 } /* computed_value_Rotl */
395 * Return the value of a Conv.
397 static tarval *computed_value_Conv(const ir_node *n) {
398 ir_node *a = get_Conv_op(n);
399 tarval *ta = value_of(a);
401 if (ta != tarval_bad)
402 return tarval_convert_to(ta, get_irn_mode(n));
405 } /* computed_value_Conv */
408 * Calculate the value of a Mux: can be evaluated, if the
409 * sel and the right input are known.
411 static tarval *computed_value_Mux(const ir_node *n) {
412 ir_node *sel = get_Mux_sel(n);
413 tarval *ts = value_of(sel);
415 if (ts == get_tarval_b_true()) {
416 ir_node *v = get_Mux_true(n);
419 else if (ts == get_tarval_b_false()) {
420 ir_node *v = get_Mux_false(n);
424 } /* computed_value_Mux */
427 * Calculate the value of a Confirm: can be evaluated,
428 * if it has the form Confirm(x, '=', Const).
430 static tarval *computed_value_Confirm(const ir_node *n) {
432 * Beware: we might produce Phi(Confirm(x == true), Confirm(x == false)).
433 * Do NOT optimize them away (CondEval wants them), so wait until
434 * remove_confirm is activated.
436 if (get_opt_remove_confirm()) {
437 if (get_Confirm_cmp(n) == pn_Cmp_Eq) {
438 tarval *tv = value_of(get_Confirm_bound(n));
439 if (tv != tarval_bad)
443 return value_of(get_Confirm_value(n));
444 } /* computed_value_Confirm */
447 * Return the value of a Proj(Cmp).
449 * This performs a first step of unreachable code elimination.
450 * Proj can not be computed, but folding a Cmp above the Proj here is
451 * not as wasteful as folding a Cmp into a Tuple of 16 Consts of which
453 * There are several case where we can evaluate a Cmp node, see later.
455 static tarval *computed_value_Proj_Cmp(const ir_node *n) {
456 ir_node *a = get_Proj_pred(n);
457 ir_node *aa = get_Cmp_left(a);
458 ir_node *ab = get_Cmp_right(a);
459 long proj_nr = get_Proj_proj(n);
462 * BEWARE: a == a is NOT always True for floating Point values, as
463 * NaN != NaN is defined, so we must check this here.
466 !mode_is_float(get_irn_mode(aa)) || proj_nr == pn_Cmp_Lt || proj_nr == pn_Cmp_Gt)
469 /* This is a trick with the bits used for encoding the Cmp
470 Proj numbers, the following statement is not the same:
471 return new_tarval_from_long (proj_nr == pn_Cmp_Eq, mode_b) */
472 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();
525 * The predecessors are Allocs or (void*)(0) constants. Allocs never
526 * return NULL, they raise an exception. Therefore we can predict
530 ir_node *aaa = skip_Proj(aa);
531 ir_node *aba = skip_Proj(ab);
533 if ( ( (/* aa is ProjP and aaa is Alloc */
535 && mode_is_reference(get_irn_mode(aa))
537 && ( (/* ab is NULL */
538 mode_is_reference(get_irn_mode(ab))
539 && tarval_is_null(tab))
540 || (/* ab is other Alloc */
542 && mode_is_reference(get_irn_mode(ab))
545 || (/* aa is NULL and aba is Alloc */
546 mode_is_reference(get_irn_mode(aa))
547 && tarval_is_null(taa)
549 && mode_is_reference(get_irn_mode(ab))
552 return new_tarval_from_long(proj_nr & pn_Cmp_Lg, mode_b);
555 return computed_value_Cmp_Confirm(a, aa, ab, proj_nr);
556 } /* computed_value_Proj_Cmp */
559 * Return the value of a floating point Quot.
561 static tarval *do_computed_value_Quot(const ir_node *a, const ir_node *b) {
562 tarval *ta = value_of(a);
563 tarval *tb = value_of(b);
565 /* cannot optimize 0 / b = 0 because of NaN */
566 if (ta != tarval_bad && tb != tarval_bad)
567 return tarval_quo(ta, tb);
569 } /* do_computed_value_Quot */
572 * Calculate the value of an integer Div of two nodes.
573 * Special case: 0 / b
575 static tarval *do_computed_value_Div(const ir_node *a, const ir_node *b) {
576 tarval *ta = value_of(a);
578 const ir_node *dummy;
580 /* Compute c1 / c2 or 0 / a, a != 0 */
581 if (tarval_is_null(ta) && value_not_zero(b, &dummy))
582 return ta; /* 0 / b == 0 */
584 if (ta != tarval_bad && tb != tarval_bad)
585 return tarval_div(ta, tb);
587 } /* do_computed_value_Div */
590 * Calculate the value of an integer Mod of two nodes.
591 * Special case: a % 1
593 static tarval *do_computed_value_Mod(const ir_node *a, const ir_node *b) {
594 tarval *ta = value_of(a);
595 tarval *tb = value_of(b);
597 /* Compute a % 1 or c1 % c2 */
598 if (tarval_is_one(tb))
599 return get_mode_null(get_irn_mode(a));
600 if (ta != tarval_bad && tb != tarval_bad)
601 return tarval_mod(ta, tb);
603 } /* do_computed_value_Mod */
606 * Return the value of a Proj(DivMod).
608 static tarval *computed_value_Proj_DivMod(const ir_node *n) {
609 long proj_nr = get_Proj_proj(n);
611 /* compute either the Div or the Mod part */
612 if (proj_nr == pn_DivMod_res_div) {
613 const ir_node *a = get_Proj_pred(n);
614 return do_computed_value_Div(get_DivMod_left(a), get_DivMod_right(a));
615 } else if (proj_nr == pn_DivMod_res_mod) {
616 const ir_node *a = get_Proj_pred(n);
617 return do_computed_value_Mod(get_DivMod_left(a), get_DivMod_right(a));
620 } /* computed_value_Proj_DivMod */
623 * Return the value of a Proj(Div).
625 static tarval *computed_value_Proj_Div(const ir_node *n) {
626 long proj_nr = get_Proj_proj(n);
628 if (proj_nr == pn_Div_res) {
629 const ir_node *a = get_Proj_pred(n);
630 return do_computed_value_Div(get_Div_left(a), get_Div_right(a));
633 } /* computed_value_Proj_Div */
636 * Return the value of a Proj(Mod).
638 static tarval *computed_value_Proj_Mod(const ir_node *n) {
639 long proj_nr = get_Proj_proj(n);
641 if (proj_nr == pn_Mod_res) {
642 const ir_node *a = get_Proj_pred(n);
643 return do_computed_value_Mod(get_Mod_left(a), get_Mod_right(a));
646 } /* computed_value_Proj_Mod */
649 * Return the value of a Proj(Quot).
651 static tarval *computed_value_Proj_Quot(const ir_node *n) {
652 long proj_nr = get_Proj_proj(n);
654 if (proj_nr == pn_Quot_res) {
655 const ir_node *a = get_Proj_pred(n);
656 return do_computed_value_Quot(get_Quot_left(a), get_Quot_right(a));
659 } /* computed_value_Proj_Quot */
662 * Return the value of a Proj.
664 static tarval *computed_value_Proj(const ir_node *proj) {
665 ir_node *n = get_Proj_pred(proj);
667 if (n->op->ops.computed_value_Proj != NULL)
668 return n->op->ops.computed_value_Proj(proj);
670 } /* computed_value_Proj */
673 * If the parameter n can be computed, return its value, else tarval_bad.
674 * Performs constant folding.
676 * @param n The node this should be evaluated
678 tarval *computed_value(const ir_node *n) {
679 if (n->op->ops.computed_value)
680 return n->op->ops.computed_value(n);
682 } /* computed_value */
685 * Set the default computed_value evaluator in an ir_op_ops.
687 * @param code the opcode for the default operation
688 * @param ops the operations initialized
693 static ir_op_ops *firm_set_default_computed_value(ir_opcode code, ir_op_ops *ops)
697 ops->computed_value = computed_value_##a; \
699 #define CASE_PROJ(a) \
701 ops->computed_value_Proj = computed_value_Proj_##a; \
738 } /* firm_set_default_computed_value */
741 * Returns a equivalent block for another block.
742 * If the block has only one predecessor, this is
743 * the equivalent one. If the only predecessor of a block is
744 * the block itself, this is a dead block.
746 * If both predecessors of a block are the branches of a binary
747 * Cond, the equivalent block is Cond's block.
749 * If all predecessors of a block are bad or lies in a dead
750 * block, the current block is dead as well.
752 * Note, that blocks are NEVER turned into Bad's, instead
753 * the dead_block flag is set. So, never test for is_Bad(block),
754 * always use is_dead_Block(block).
756 static ir_node *equivalent_node_Block(ir_node *n)
761 /* don't optimize dead blocks */
762 if (is_Block_dead(n))
765 n_preds = get_Block_n_cfgpreds(n);
767 /* The Block constructor does not call optimize, but mature_immBlock()
768 calls the optimization. */
769 assert(get_Block_matured(n));
771 /* Straightening: a single entry Block following a single exit Block
772 can be merged, if it is not the Start block. */
773 /* !!! Beware, all Phi-nodes of n must have been optimized away.
774 This should be true, as the block is matured before optimize is called.
775 But what about Phi-cycles with the Phi0/Id that could not be resolved?
776 Remaining Phi nodes are just Ids. */
778 ir_node *pred = skip_Proj(get_Block_cfgpred(n, 0));
781 ir_node *predblock = get_nodes_block(pred);
782 if (predblock == oldn) {
783 /* Jmp jumps into the block it is in -- deal self cycle. */
784 n = set_Block_dead(n);
785 DBG_OPT_DEAD_BLOCK(oldn, n);
786 } else if (get_opt_control_flow_straightening()) {
788 DBG_OPT_STG(oldn, n);
790 } else if (is_Cond(pred)) {
791 ir_node *predblock = get_nodes_block(pred);
792 if (predblock == oldn) {
793 /* Jmp jumps into the block it is in -- deal self cycle. */
794 n = set_Block_dead(n);
795 DBG_OPT_DEAD_BLOCK(oldn, n);
798 } else if ((n_preds == 2) &&
799 (get_opt_control_flow_weak_simplification())) {
800 /* Test whether Cond jumps twice to this block
801 * The more general case which more than 2 predecessors is handles
802 * in optimize_cf(), we handle only this special case for speed here.
804 ir_node *a = get_Block_cfgpred(n, 0);
805 ir_node *b = get_Block_cfgpred(n, 1);
807 if (is_Proj(a) && is_Proj(b)) {
808 ir_node *cond = get_Proj_pred(a);
810 if (cond == get_Proj_pred(b) && is_Cond(cond) &&
811 get_irn_mode(get_Cond_selector(cond)) == mode_b) {
812 /* Also a single entry Block following a single exit Block. Phis have
813 twice the same operand and will be optimized away. */
814 n = get_nodes_block(cond);
815 DBG_OPT_IFSIM1(oldn, a, b, n);
818 } else if (get_opt_unreachable_code() &&
819 (n != get_irg_start_block(current_ir_graph)) &&
820 (n != get_irg_end_block(current_ir_graph)) ) {
823 /* If all inputs are dead, this block is dead too, except if it is
824 the start or end block. This is one step of unreachable code
826 for (i = get_Block_n_cfgpreds(n) - 1; i >= 0; --i) {
827 ir_node *pred = get_Block_cfgpred(n, i);
830 if (is_Bad(pred)) continue;
831 pred_blk = get_nodes_block(skip_Proj(pred));
833 if (is_Block_dead(pred_blk)) continue;
836 /* really found a living input */
841 n = set_Block_dead(n);
842 DBG_OPT_DEAD_BLOCK(oldn, n);
847 } /* equivalent_node_Block */
850 * Returns a equivalent node for a Jmp, a Bad :-)
851 * Of course this only happens if the Block of the Jmp is dead.
853 static ir_node *equivalent_node_Jmp(ir_node *n) {
856 /* unreachable code elimination */
857 if (is_Block_dead(get_nodes_block(n))) {
858 n = get_irg_bad(current_ir_graph);
859 DBG_OPT_DEAD_BLOCK(oldn, n);
862 } /* equivalent_node_Jmp */
864 /** Raise is handled in the same way as Jmp. */
865 #define equivalent_node_Raise equivalent_node_Jmp
868 /* We do not evaluate Cond here as we replace it by a new node, a Jmp.
869 See transform_node_Proj_Cond(). */
872 * Optimize operations that are commutative and have neutral 0,
873 * so a op 0 = 0 op a = a.
875 static ir_node *equivalent_node_neutral_zero(ir_node *n) {
878 ir_node *a = get_binop_left(n);
879 ir_node *b = get_binop_right(n);
884 /* After running compute_node there is only one constant predecessor.
885 Find this predecessors value and remember the other node: */
886 if ((tv = value_of(a)) != tarval_bad) {
888 } else if ((tv = value_of(b)) != tarval_bad) {
893 /* If this predecessors constant value is zero, the operation is
894 * unnecessary. Remove it.
896 * Beware: If n is a Add, the mode of on and n might be different
897 * which happens in this rare construction: NULL + 3.
898 * Then, a Conv would be needed which we cannot include here.
900 if (tarval_is_null(tv) && get_irn_mode(on) == get_irn_mode(n)) {
903 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_0);
907 } /* equivalent_node_neutral_zero */
910 * Eor is commutative and has neutral 0.
912 static ir_node *equivalent_node_Eor(ir_node *n) {
917 n = equivalent_node_neutral_zero(n);
918 if (n != oldn) return n;
921 b = get_Eor_right(n);
924 ir_node *aa = get_Eor_left(a);
925 ir_node *ab = get_Eor_right(a);
928 /* (a ^ b) ^ a -> b */
930 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_EOR_A_B_A);
932 } else if (ab == b) {
933 /* (a ^ b) ^ b -> a */
935 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_EOR_A_B_A);
940 ir_node *ba = get_Eor_left(b);
941 ir_node *bb = get_Eor_right(b);
944 /* a ^ (a ^ b) -> b */
946 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_EOR_A_B_A);
948 } else if (bb == a) {
949 /* a ^ (b ^ a) -> b */
951 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_EOR_A_B_A);
959 * Optimize a - 0 and (a - x) + x (for modes with wrap-around).
961 * The second one looks strange, but this construct
962 * is used heavily in the LCC sources :-).
964 * Beware: The Mode of an Add may be different than the mode of its
965 * predecessors, so we could not return a predecessors in all cases.
967 static ir_node *equivalent_node_Add(ir_node *n) {
969 ir_node *left, *right;
970 ir_mode *mode = get_irn_mode(n);
972 n = equivalent_node_neutral_zero(n);
976 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
977 if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
980 left = get_Add_left(n);
981 right = get_Add_right(n);
984 if (get_Sub_right(left) == right) {
987 n = get_Sub_left(left);
988 if (mode == get_irn_mode(n)) {
989 DBG_OPT_ALGSIM1(oldn, left, right, n, FS_OPT_ADD_SUB);
995 if (get_Sub_right(right) == left) {
998 n = get_Sub_left(right);
999 if (mode == get_irn_mode(n)) {
1000 DBG_OPT_ALGSIM1(oldn, left, right, n, FS_OPT_ADD_SUB);
1006 } /* equivalent_node_Add */
1009 * optimize operations that are not commutative but have neutral 0 on left,
1012 static ir_node *equivalent_node_left_zero(ir_node *n) {
1015 ir_node *a = get_binop_left(n);
1016 ir_node *b = get_binop_right(n);
1017 tarval *tb = value_of(b);
1019 if (tarval_is_null(tb)) {
1022 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_0);
1025 } /* equivalent_node_left_zero */
1027 #define equivalent_node_Shl equivalent_node_left_zero
1028 #define equivalent_node_Shr equivalent_node_left_zero
1029 #define equivalent_node_Shrs equivalent_node_left_zero
1030 #define equivalent_node_Rotl equivalent_node_left_zero
1033 * Optimize a - 0 and (a + x) - x (for modes with wrap-around).
1035 * The second one looks strange, but this construct
1036 * is used heavily in the LCC sources :-).
1038 * Beware: The Mode of a Sub may be different than the mode of its
1039 * predecessors, so we could not return a predecessors in all cases.
1041 static ir_node *equivalent_node_Sub(ir_node *n) {
1044 ir_mode *mode = get_irn_mode(n);
1047 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
1048 if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
1051 b = get_Sub_right(n);
1054 /* Beware: modes might be different */
1055 if (tarval_is_null(tb)) {
1056 ir_node *a = get_Sub_left(n);
1057 if (mode == get_irn_mode(a)) {
1060 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_0);
1064 } /* equivalent_node_Sub */
1068 * Optimize an "self-inverse unary op", ie op(op(n)) = n.
1071 * -(-a) == a, but might overflow two times.
1072 * We handle it anyway here but the better way would be a
1073 * flag. This would be needed for Pascal for instance.
1075 static ir_node *equivalent_node_idempotent_unop(ir_node *n) {
1077 ir_node *pred = get_unop_op(n);
1079 /* optimize symmetric unop */
1080 if (get_irn_op(pred) == get_irn_op(n)) {
1081 n = get_unop_op(pred);
1082 DBG_OPT_ALGSIM2(oldn, pred, n, FS_OPT_IDEM_UNARY);
1085 } /* equivalent_node_idempotent_unop */
1087 /** Optimize Not(Not(x)) == x. */
1088 #define equivalent_node_Not equivalent_node_idempotent_unop
1090 /** -(-x) == x ??? Is this possible or can --x raise an
1091 out of bounds exception if min =! max? */
1092 #define equivalent_node_Minus equivalent_node_idempotent_unop
1095 * Optimize a * 1 = 1 * a = a.
1097 static ir_node *equivalent_node_Mul(ir_node *n) {
1099 ir_node *a = get_Mul_left(n);
1101 /* we can handle here only the n * n = n bit cases */
1102 if (get_irn_mode(n) == get_irn_mode(a)) {
1103 ir_node *b = get_Mul_right(n);
1107 * Mul is commutative and has again an other neutral element.
1108 * Constants are place right, so check this case first.
1111 if (tarval_is_one(tv)) {
1113 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
1116 if (tarval_is_one(tv)) {
1118 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
1123 } /* equivalent_node_Mul */
1126 * Use algebraic simplification a | a = a | 0 = 0 | a = a.
1128 static ir_node *equivalent_node_Or(ir_node *n) {
1131 ir_node *a = get_Or_left(n);
1132 ir_node *b = get_Or_right(n);
1136 n = a; /* Or has it's own neutral element */
1137 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_OR);
1140 /* constants are cormalized to right, check this site first */
1142 if (tarval_is_null(tv)) {
1144 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_OR);
1148 if (tarval_is_null(tv)) {
1150 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_OR);
1155 } /* equivalent_node_Or */
1158 * Optimize a & 0b1...1 = 0b1...1 & a = a & a = (a|X) & a = a.
1160 static ir_node *equivalent_node_And(ir_node *n) {
1163 ir_node *a = get_And_left(n);
1164 ir_node *b = get_And_right(n);
1168 n = a; /* And has it's own neutral element */
1169 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_AND);
1172 /* constants are cormalized to right, check this site first */
1174 if (tarval_is_all_one(tv)) {
1176 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1180 if (tarval_is_all_one(tv)) {
1182 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1186 if (b == get_Or_left(a) || b == get_Or_right(a)) {
1189 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1194 if (a == get_Or_left(b) || a == get_Or_right(b)) {
1197 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1202 } /* equivalent_node_And */
1205 * Try to remove useless Conv's:
1207 static ir_node *equivalent_node_Conv(ir_node *n) {
1209 ir_node *a = get_Conv_op(n);
1211 ir_mode *n_mode = get_irn_mode(n);
1212 ir_mode *a_mode = get_irn_mode(a);
1215 if (n_mode == a_mode) { /* No Conv necessary */
1216 if (get_Conv_strict(n)) {
1219 /* neither Minus nor Abs nor Confirm change the precision,
1220 so we can "look-through" */
1223 p = get_Minus_op(p);
1224 } else if (is_Abs(p)) {
1226 } else if (is_Confirm(p)) {
1227 p = get_Confirm_value(p);
1233 if (is_Conv(p) && get_Conv_strict(p)) {
1234 /* we known already, that a_mode == n_mode, and neither
1235 Abs nor Minus change the mode, so the second Conv
1237 assert(get_irn_mode(p) == n_mode);
1239 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CONV);
1243 ir_node *pred = get_Proj_pred(p);
1244 if (is_Load(pred)) {
1245 /* Loads always return with the exact precision of n_mode */
1246 assert(get_Load_mode(pred) == n_mode);
1248 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CONV);
1251 if (is_Proj(pred) && get_Proj_proj(pred) == pn_Start_T_args) {
1252 pred = get_Proj_pred(pred);
1253 if (is_Start(pred)) {
1254 /* Arguments always return with the exact precision,
1255 as strictConv's are place before Call -- if the
1256 caller was compiled with the same setting.
1257 Otherwise, the semantics is probably still right. */
1258 assert(get_irn_mode(p) == n_mode);
1260 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CONV);
1266 /* special case: the immediate predecessor is also a Conv */
1267 if (! get_Conv_strict(a)) {
1268 /* first one is not strict, kick it */
1270 a_mode = get_irn_mode(a);
1274 /* else both are strict conv, second is superfluous */
1276 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CONV);
1281 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CONV);
1284 } else if (is_Conv(a)) { /* Conv(Conv(b)) */
1285 ir_node *b = get_Conv_op(a);
1286 ir_mode *b_mode = get_irn_mode(b);
1288 if (get_Conv_strict(n) && get_Conv_strict(a)) {
1289 /* both are strict conv */
1290 if (smaller_mode(a_mode, n_mode)) {
1291 /* both are strict, but the first is smaller, so
1292 the second cannot remove more precision, remove the
1294 set_Conv_strict(n, 0);
1297 if (n_mode == b_mode) {
1298 if (! get_Conv_strict(n) && ! get_Conv_strict(a)) {
1299 if (n_mode == mode_b) {
1300 n = b; /* Convb(Conv*(xxxb(...))) == xxxb(...) */
1301 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
1303 } else if (get_mode_arithmetic(n_mode) == get_mode_arithmetic(a_mode)) {
1304 if (values_in_mode(b_mode, a_mode)) {
1305 n = b; /* ConvS(ConvL(xxxS(...))) == xxxS(...) */
1306 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
1311 if (mode_is_int(n_mode) && get_mode_arithmetic(a_mode) == irma_ieee754) {
1312 /* ConvI(ConvF(I)) -> I, iff float mantissa >= int mode */
1313 unsigned int_mantissa = get_mode_size_bits(n_mode) - (mode_is_signed(n_mode) ? 1 : 0);
1314 unsigned float_mantissa = tarval_ieee754_get_mantissa_size(a_mode);
1316 if (float_mantissa >= int_mantissa) {
1318 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
1323 if (smaller_mode(b_mode, a_mode)) {
1324 if (get_Conv_strict(n))
1325 set_Conv_strict(b, 1);
1326 n = b; /* ConvA(ConvB(ConvA(...))) == ConvA(...) */
1327 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
1334 } /* equivalent_node_Conv */
1337 * A Cast may be removed if the type of the previous node
1338 * is already the type of the Cast.
1340 static ir_node *equivalent_node_Cast(ir_node *n) {
1342 ir_node *pred = get_Cast_op(n);
1344 if (get_irn_type(pred) == get_Cast_type(n)) {
1346 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CAST);
1349 } /* equivalent_node_Cast */
1352 * - fold Phi-nodes, iff they have only one predecessor except
1355 static ir_node *equivalent_node_Phi(ir_node *n) {
1360 ir_node *first_val = NULL; /* to shutup gcc */
1362 if (!get_opt_normalize()) return n;
1364 n_preds = get_Phi_n_preds(n);
1366 block = get_nodes_block(n);
1367 if (is_Block_dead(block)) /* Control dead */
1368 return get_irg_bad(current_ir_graph);
1370 if (n_preds == 0) return n; /* Phi of dead Region without predecessors. */
1372 /* Find first non-self-referencing input */
1373 for (i = 0; i < n_preds; ++i) {
1374 first_val = get_Phi_pred(n, i);
1375 if ( (first_val != n) /* not self pointer */
1377 /* BEWARE: when the if is changed to 1, Phi's will ignore it's Bad
1378 * predecessors. Then, Phi nodes in dead code might be removed, causing
1379 * nodes pointing to themself (Add's for instance).
1380 * This is really bad and causes endless recursions in several
1381 * code pathes, so we do NOT optimize such a code.
1382 * This is not that bad as it sounds, optimize_cf() removes bad control flow
1383 * (and bad Phi predecessors), so live code is optimized later.
1385 && (! is_Bad(get_Block_cfgpred(block, i)))
1387 ) { /* value not dead */
1388 break; /* then found first value. */
1393 /* A totally Bad or self-referencing Phi (we didn't break the above loop) */
1394 return get_irg_bad(current_ir_graph);
1397 /* search for rest of inputs, determine if any of these
1398 are non-self-referencing */
1399 while (++i < n_preds) {
1400 ir_node *scnd_val = get_Phi_pred(n, i);
1401 if ( (scnd_val != n)
1402 && (scnd_val != first_val)
1405 && (! is_Bad(get_Block_cfgpred(block, i)))
1413 /* Fold, if no multiple distinct non-self-referencing inputs */
1415 DBG_OPT_PHI(oldn, n);
1418 } /* equivalent_node_Phi */
1421 * Several optimizations:
1422 * - fold Sync-nodes, iff they have only one predecessor except
1425 static ir_node *equivalent_node_Sync(ir_node *n) {
1426 int arity = get_Sync_n_preds(n);
1429 for (i = 0; i < arity;) {
1430 ir_node *pred = get_Sync_pred(n, i);
1433 /* Remove Bad predecessors */
1440 /* Remove duplicate predecessors */
1446 if (get_Sync_pred(n, j) == pred) {
1454 if (arity == 0) return get_irg_bad(current_ir_graph);
1455 if (arity == 1) return get_Sync_pred(n, 0);
1457 } /* equivalent_node_Sync */
1460 * Optimize Proj(Tuple).
1462 static ir_node *equivalent_node_Proj_Tuple(ir_node *proj) {
1463 ir_node *oldn = proj;
1464 ir_node *tuple = get_Proj_pred(proj);
1466 /* Remove the Tuple/Proj combination. */
1467 proj = get_Tuple_pred(tuple, get_Proj_proj(proj));
1468 DBG_OPT_TUPLE(oldn, tuple, proj);
1471 } /* equivalent_node_Proj_Tuple */
1474 * Optimize a / 1 = a.
1476 static ir_node *equivalent_node_Proj_Div(ir_node *proj) {
1477 ir_node *oldn = proj;
1478 ir_node *div = get_Proj_pred(proj);
1479 ir_node *b = get_Div_right(div);
1480 tarval *tb = value_of(b);
1482 /* Div is not commutative. */
1483 if (tarval_is_one(tb)) { /* div(x, 1) == x */
1484 switch (get_Proj_proj(proj)) {
1486 proj = get_Div_mem(div);
1487 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NEUTRAL_1);
1491 proj = get_Div_left(div);
1492 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NEUTRAL_1);
1496 /* we cannot replace the exception Proj's here, this is done in
1497 transform_node_Proj_Div() */
1502 } /* equivalent_node_Proj_Div */
1505 * Optimize a / 1.0 = a.
1507 static ir_node *equivalent_node_Proj_Quot(ir_node *proj) {
1508 ir_node *oldn = proj;
1509 ir_node *quot = get_Proj_pred(proj);
1510 ir_node *b = get_Quot_right(quot);
1511 tarval *tb = value_of(b);
1513 /* Div is not commutative. */
1514 if (tarval_is_one(tb)) { /* Quot(x, 1) == x */
1515 switch (get_Proj_proj(proj)) {
1517 proj = get_Quot_mem(quot);
1518 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NEUTRAL_1);
1522 proj = get_Quot_left(quot);
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_Quot() */
1533 } /* equivalent_node_Proj_Quot */
1536 * Optimize a / 1 = a.
1538 static ir_node *equivalent_node_Proj_DivMod(ir_node *proj) {
1539 ir_node *oldn = proj;
1540 ir_node *divmod = get_Proj_pred(proj);
1541 ir_node *b = get_DivMod_right(divmod);
1542 tarval *tb = value_of(b);
1544 /* Div is not commutative. */
1545 if (tarval_is_one(tb)) { /* div(x, 1) == x */
1546 switch (get_Proj_proj(proj)) {
1548 proj = get_DivMod_mem(divmod);
1549 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NEUTRAL_1);
1552 case pn_DivMod_res_div:
1553 proj = get_DivMod_left(divmod);
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_DivMod().
1560 Note further that the pn_DivMod_res_div case is handled in
1561 computed_value_Proj(). */
1566 } /* equivalent_node_Proj_DivMod */
1569 * Optimize CopyB(mem, x, x) into a Nop.
1571 static ir_node *equivalent_node_Proj_CopyB(ir_node *proj) {
1572 ir_node *oldn = proj;
1573 ir_node *copyb = get_Proj_pred(proj);
1574 ir_node *a = get_CopyB_dst(copyb);
1575 ir_node *b = get_CopyB_src(copyb);
1578 /* Turn CopyB into a tuple (mem, jmp, bad, bad) */
1579 switch (get_Proj_proj(proj)) {
1580 case pn_CopyB_M_regular:
1581 proj = get_CopyB_mem(copyb);
1582 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NOP);
1585 case pn_CopyB_M_except:
1586 case pn_CopyB_X_except:
1587 DBG_OPT_EXC_REM(proj);
1588 proj = get_irg_bad(current_ir_graph);
1593 } /* equivalent_node_Proj_CopyB */
1596 * Optimize Bounds(idx, idx, upper) into idx.
1598 static ir_node *equivalent_node_Proj_Bound(ir_node *proj) {
1599 ir_node *oldn = proj;
1600 ir_node *bound = get_Proj_pred(proj);
1601 ir_node *idx = get_Bound_index(bound);
1602 ir_node *pred = skip_Proj(idx);
1605 if (idx == get_Bound_lower(bound))
1607 else if (is_Bound(pred)) {
1609 * idx was Bounds checked in the same MacroBlock previously,
1610 * it is still valid if lower <= pred_lower && pred_upper <= upper.
1612 ir_node *lower = get_Bound_lower(bound);
1613 ir_node *upper = get_Bound_upper(bound);
1614 if (get_Bound_lower(pred) == lower &&
1615 get_Bound_upper(pred) == upper &&
1616 get_irn_MacroBlock(bound) == get_irn_MacroBlock(pred)) {
1618 * One could expect that we simply return the previous
1619 * Bound here. However, this would be wrong, as we could
1620 * add an exception Proj to a new location then.
1621 * So, we must turn in into a tuple.
1627 /* Turn Bound into a tuple (mem, jmp, bad, idx) */
1628 switch (get_Proj_proj(proj)) {
1630 DBG_OPT_EXC_REM(proj);
1631 proj = get_Bound_mem(bound);
1633 case pn_Bound_X_except:
1634 DBG_OPT_EXC_REM(proj);
1635 proj = get_irg_bad(current_ir_graph);
1639 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NOP);
1642 /* cannot optimize pn_Bound_X_regular, handled in transform ... */
1647 } /* equivalent_node_Proj_Bound */
1650 * Optimize an Exception Proj(Load) with a non-null address.
1652 static ir_node *equivalent_node_Proj_Load(ir_node *proj) {
1653 if (get_opt_ldst_only_null_ptr_exceptions()) {
1654 if (get_irn_mode(proj) == mode_X) {
1655 ir_node *load = get_Proj_pred(proj);
1657 /* get the Load address */
1658 const ir_node *addr = get_Load_ptr(load);
1659 const ir_node *confirm;
1661 if (value_not_null(addr, &confirm)) {
1662 if (get_Proj_proj(proj) == pn_Load_X_except) {
1663 DBG_OPT_EXC_REM(proj);
1664 return get_irg_bad(current_ir_graph);
1670 } /* equivalent_node_Proj_Load */
1673 * Optimize an Exception Proj(Store) with a non-null address.
1675 static ir_node *equivalent_node_Proj_Store(ir_node *proj) {
1676 if (get_opt_ldst_only_null_ptr_exceptions()) {
1677 if (get_irn_mode(proj) == mode_X) {
1678 ir_node *store = get_Proj_pred(proj);
1680 /* get the load/store address */
1681 const ir_node *addr = get_Store_ptr(store);
1682 const ir_node *confirm;
1684 if (value_not_null(addr, &confirm)) {
1685 if (get_Proj_proj(proj) == pn_Store_X_except) {
1686 DBG_OPT_EXC_REM(proj);
1687 return get_irg_bad(current_ir_graph);
1693 } /* equivalent_node_Proj_Store */
1696 * Does all optimizations on nodes that must be done on it's Proj's
1697 * because of creating new nodes.
1699 static ir_node *equivalent_node_Proj(ir_node *proj) {
1700 ir_node *n = get_Proj_pred(proj);
1702 if (get_irn_mode(proj) == mode_X) {
1703 if (is_Block_dead(get_nodes_block(n))) {
1704 /* Remove dead control flow -- early gigo(). */
1705 return get_irg_bad(current_ir_graph);
1708 if (n->op->ops.equivalent_node_Proj)
1709 return n->op->ops.equivalent_node_Proj(proj);
1711 } /* equivalent_node_Proj */
1716 static ir_node *equivalent_node_Id(ir_node *n) {
1723 DBG_OPT_ID(oldn, n);
1725 } /* equivalent_node_Id */
1730 static ir_node *equivalent_node_Mux(ir_node *n)
1732 ir_node *oldn = n, *sel = get_Mux_sel(n);
1733 tarval *ts = value_of(sel);
1735 /* Mux(true, f, t) == t */
1736 if (ts == tarval_b_true) {
1737 n = get_Mux_true(n);
1738 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_C);
1740 /* Mux(false, f, t) == f */
1741 else if (ts == tarval_b_false) {
1742 n = get_Mux_false(n);
1743 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_C);
1745 /* Mux(v, x, x) == x */
1746 else if (get_Mux_false(n) == get_Mux_true(n)) {
1747 n = get_Mux_true(n);
1748 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_EQ);
1750 else if (is_Proj(sel) && !mode_honor_signed_zeros(get_irn_mode(n))) {
1751 ir_node *cmp = get_Proj_pred(sel);
1752 long proj_nr = get_Proj_proj(sel);
1753 ir_node *f = get_Mux_false(n);
1754 ir_node *t = get_Mux_true(n);
1757 * Note further that these optimization work even for floating point
1758 * with NaN's because -NaN == NaN.
1759 * However, if +0 and -0 is handled differently, we cannot use the first one.
1762 ir_node *const cmp_l = get_Cmp_left(cmp);
1763 ir_node *const cmp_r = get_Cmp_right(cmp);
1767 if ((cmp_l == t && cmp_r == f) || /* Mux(t == f, t, f) -> f */
1768 (cmp_l == f && cmp_r == t)) { /* Mux(f == t, t, f) -> f */
1770 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1777 if ((cmp_l == t && cmp_r == f) || /* Mux(t != f, t, f) -> t */
1778 (cmp_l == f && cmp_r == t)) { /* Mux(f != t, t, f) -> t */
1780 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1787 * Note: normalization puts the constant on the right side,
1788 * so we check only one case.
1790 if (cmp_l == t && tarval_is_null(value_of(cmp_r))) {
1791 /* Mux(t CMP 0, X, t) */
1792 if (is_Minus(f) && get_Minus_op(f) == t) {
1793 /* Mux(t CMP 0, -t, t) */
1794 if (proj_nr == pn_Cmp_Eq) {
1795 /* Mux(t == 0, -t, t) ==> -t */
1797 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1798 } else if (proj_nr == pn_Cmp_Lg || proj_nr == pn_Cmp_Ne) {
1799 /* Mux(t != 0, -t, t) ==> t */
1801 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1808 } /* equivalent_node_Mux */
1811 * Remove Confirm nodes if setting is on.
1812 * Replace Confirms(x, '=', Constlike) by Constlike.
1814 static ir_node *equivalent_node_Confirm(ir_node *n) {
1815 ir_node *pred = get_Confirm_value(n);
1816 pn_Cmp pnc = get_Confirm_cmp(n);
1818 while (is_Confirm(pred) && pnc == get_Confirm_cmp(pred)) {
1820 * rare case: two identical Confirms one after another,
1821 * replace the second one with the first.
1824 pred = get_Confirm_value(n);
1825 pnc = get_Confirm_cmp(n);
1827 if (get_opt_remove_confirm())
1828 return get_Confirm_value(n);
1833 * equivalent_node() returns a node equivalent to input n. It skips all nodes that
1834 * perform no actual computation, as, e.g., the Id nodes. It does not create
1835 * new nodes. It is therefore safe to free n if the node returned is not n.
1836 * If a node returns a Tuple we can not just skip it. If the size of the
1837 * in array fits, we transform n into a tuple (e.g., Div).
1839 ir_node *equivalent_node(ir_node *n) {
1840 if (n->op->ops.equivalent_node)
1841 return n->op->ops.equivalent_node(n);
1843 } /* equivalent_node */
1846 * Sets the default equivalent node operation for an ir_op_ops.
1848 * @param code the opcode for the default operation
1849 * @param ops the operations initialized
1854 static ir_op_ops *firm_set_default_equivalent_node(ir_opcode code, ir_op_ops *ops)
1858 ops->equivalent_node = equivalent_node_##a; \
1860 #define CASE_PROJ(a) \
1862 ops->equivalent_node_Proj = equivalent_node_Proj_##a; \
1904 } /* firm_set_default_equivalent_node */
1907 * Returns non-zero if a node is a Phi node
1908 * with all predecessors constant.
1910 static int is_const_Phi(ir_node *n) {
1913 if (! is_Phi(n) || get_irn_arity(n) == 0)
1915 for (i = get_irn_arity(n) - 1; i >= 0; --i) {
1916 if (! is_Const(get_irn_n(n, i)))
1920 } /* is_const_Phi */
1922 typedef tarval *(*tarval_sub_type)(tarval *a, tarval *b, ir_mode *mode);
1923 typedef tarval *(*tarval_binop_type)(tarval *a, tarval *b);
1926 * Wrapper for the tarval binop evaluation, tarval_sub has one more parameter.
1928 static tarval *do_eval(tarval *(*eval)(), tarval *a, tarval *b, ir_mode *mode) {
1929 if (eval == tarval_sub) {
1930 tarval_sub_type func = (tarval_sub_type)eval;
1932 return func(a, b, mode);
1934 tarval_binop_type func = (tarval_binop_type)eval;
1941 * Apply an evaluator on a binop with a constant operators (and one Phi).
1943 * @param phi the Phi node
1944 * @param other the other operand
1945 * @param eval an evaluator function
1946 * @param mode the mode of the result, may be different from the mode of the Phi!
1947 * @param left if non-zero, other is the left operand, else the right
1949 * @return a new Phi node if the conversion was successful, NULL else
1951 static ir_node *apply_binop_on_phi(ir_node *phi, tarval *other, tarval *(*eval)(), ir_mode *mode, int left) {
1956 int i, n = get_irn_arity(phi);
1958 NEW_ARR_A(void *, res, n);
1960 for (i = 0; i < n; ++i) {
1961 pred = get_irn_n(phi, i);
1962 tv = get_Const_tarval(pred);
1963 tv = do_eval(eval, other, tv, mode);
1965 if (tv == tarval_bad) {
1966 /* folding failed, bad */
1972 for (i = 0; i < n; ++i) {
1973 pred = get_irn_n(phi, i);
1974 tv = get_Const_tarval(pred);
1975 tv = do_eval(eval, tv, other, mode);
1977 if (tv == tarval_bad) {
1978 /* folding failed, bad */
1984 irg = current_ir_graph;
1985 for (i = 0; i < n; ++i) {
1986 pred = get_irn_n(phi, i);
1987 res[i] = new_r_Const_type(irg, get_irg_start_block(irg),
1988 mode, res[i], get_Const_type(pred));
1990 return new_r_Phi(irg, get_nodes_block(phi), n, (ir_node **)res, mode);
1991 } /* apply_binop_on_phi */
1994 * Apply an evaluator on a binop with two constant Phi.
1996 * @param a the left Phi node
1997 * @param b the right Phi node
1998 * @param eval an evaluator function
1999 * @param mode the mode of the result, may be different from the mode of the Phi!
2001 * @return a new Phi node if the conversion was successful, NULL else
2003 static ir_node *apply_binop_on_2_phis(ir_node *a, ir_node *b, tarval *(*eval)(), ir_mode *mode) {
2004 tarval *tv_l, *tv_r, *tv;
2010 if (get_nodes_block(a) != get_nodes_block(b))
2013 n = get_irn_arity(a);
2014 NEW_ARR_A(void *, res, n);
2016 for (i = 0; i < n; ++i) {
2017 pred = get_irn_n(a, i);
2018 tv_l = get_Const_tarval(pred);
2019 pred = get_irn_n(b, i);
2020 tv_r = get_Const_tarval(pred);
2021 tv = do_eval(eval, tv_l, tv_r, mode);
2023 if (tv == tarval_bad) {
2024 /* folding failed, bad */
2029 irg = current_ir_graph;
2030 for (i = 0; i < n; ++i) {
2031 pred = get_irn_n(a, i);
2032 res[i] = new_r_Const_type(irg, get_irg_start_block(irg), mode, res[i], get_Const_type(pred));
2034 return new_r_Phi(irg, get_nodes_block(a), n, (ir_node **)res, mode);
2035 } /* apply_binop_on_2_phis */
2038 * Apply an evaluator on a unop with a constant operator (a Phi).
2040 * @param phi the Phi node
2041 * @param eval an evaluator function
2043 * @return a new Phi node if the conversion was successful, NULL else
2045 static ir_node *apply_unop_on_phi(ir_node *phi, tarval *(*eval)(tarval *)) {
2051 int i, n = get_irn_arity(phi);
2053 NEW_ARR_A(void *, res, n);
2054 for (i = 0; i < n; ++i) {
2055 pred = get_irn_n(phi, i);
2056 tv = get_Const_tarval(pred);
2059 if (tv == tarval_bad) {
2060 /* folding failed, bad */
2065 mode = get_irn_mode(phi);
2066 irg = current_ir_graph;
2067 for (i = 0; i < n; ++i) {
2068 pred = get_irn_n(phi, i);
2069 res[i] = new_r_Const_type(irg, get_irg_start_block(irg),
2070 mode, res[i], get_Const_type(pred));
2072 return new_r_Phi(irg, get_nodes_block(phi), n, (ir_node **)res, mode);
2073 } /* apply_unop_on_phi */
2076 * Apply a conversion on a constant operator (a Phi).
2078 * @param phi the Phi node
2080 * @return a new Phi node if the conversion was successful, NULL else
2082 static ir_node *apply_conv_on_phi(ir_node *phi, ir_mode *mode) {
2087 int i, n = get_irn_arity(phi);
2089 NEW_ARR_A(void *, res, n);
2090 for (i = 0; i < n; ++i) {
2091 pred = get_irn_n(phi, i);
2092 tv = get_Const_tarval(pred);
2093 tv = tarval_convert_to(tv, mode);
2095 if (tv == tarval_bad) {
2096 /* folding failed, bad */
2101 irg = current_ir_graph;
2102 for (i = 0; i < n; ++i) {
2103 pred = get_irn_n(phi, i);
2104 res[i] = new_r_Const_type(irg, get_irg_start_block(irg),
2105 mode, res[i], get_Const_type(pred));
2107 return new_r_Phi(irg, get_nodes_block(phi), n, (ir_node **)res, mode);
2108 } /* apply_conv_on_phi */
2111 * Transform AddP(P, ConvIs(Iu)), AddP(P, ConvIu(Is)) and
2112 * SubP(P, ConvIs(Iu)), SubP(P, ConvIu(Is)).
2113 * If possible, remove the Conv's.
2115 static ir_node *transform_node_AddSub(ir_node *n) {
2116 ir_mode *mode = get_irn_mode(n);
2118 if (mode_is_reference(mode)) {
2119 ir_node *left = get_binop_left(n);
2120 ir_node *right = get_binop_right(n);
2121 unsigned ref_bits = get_mode_size_bits(mode);
2123 if (is_Conv(left)) {
2124 ir_mode *lmode = get_irn_mode(left);
2125 unsigned bits = get_mode_size_bits(lmode);
2127 if (ref_bits == bits &&
2128 mode_is_int(lmode) &&
2129 get_mode_arithmetic(lmode) == irma_twos_complement) {
2130 ir_node *pre = get_Conv_op(left);
2131 ir_mode *pre_mode = get_irn_mode(pre);
2133 if (mode_is_int(pre_mode) &&
2134 get_mode_size_bits(pre_mode) == bits &&
2135 get_mode_arithmetic(pre_mode) == irma_twos_complement) {
2136 /* ok, this conv just changes to sign, moreover the calculation
2137 * is done with same number of bits as our address mode, so
2138 * we can ignore the conv as address calculation can be viewed
2139 * as either signed or unsigned
2141 set_binop_left(n, pre);
2146 if (is_Conv(right)) {
2147 ir_mode *rmode = get_irn_mode(right);
2148 unsigned bits = get_mode_size_bits(rmode);
2150 if (ref_bits == bits &&
2151 mode_is_int(rmode) &&
2152 get_mode_arithmetic(rmode) == irma_twos_complement) {
2153 ir_node *pre = get_Conv_op(right);
2154 ir_mode *pre_mode = get_irn_mode(pre);
2156 if (mode_is_int(pre_mode) &&
2157 get_mode_size_bits(pre_mode) == bits &&
2158 get_mode_arithmetic(pre_mode) == irma_twos_complement) {
2159 /* ok, this conv just changes to sign, moreover the calculation
2160 * is done with same number of bits as our address mode, so
2161 * we can ignore the conv as address calculation can be viewed
2162 * as either signed or unsigned
2164 set_binop_right(n, pre);
2169 /* let address arithmetic use unsigned modes */
2170 if (is_Const(right)) {
2171 ir_mode *rmode = get_irn_mode(right);
2173 if (mode_is_signed(rmode) && get_mode_arithmetic(rmode) == irma_twos_complement) {
2174 /* convert a AddP(P, *s) into AddP(P, *u) */
2175 ir_mode *nm = get_reference_mode_unsigned_eq(mode);
2177 ir_node *pre = new_r_Conv(current_ir_graph, get_nodes_block(n), right, nm);
2178 set_binop_right(n, pre);
2184 } /* transform_node_AddSub */
2186 #define HANDLE_BINOP_PHI(eval, a, b, c, mode) \
2188 if (is_Const(b) && is_const_Phi(a)) { \
2189 /* check for Op(Phi, Const) */ \
2190 c = apply_binop_on_phi(a, get_Const_tarval(b), eval, mode, 0);\
2192 else if (is_Const(a) && is_const_Phi(b)) { \
2193 /* check for Op(Const, Phi) */ \
2194 c = apply_binop_on_phi(b, get_Const_tarval(a), eval, mode, 1);\
2196 else if (is_const_Phi(a) && is_const_Phi(b)) { \
2197 /* check for Op(Phi, Phi) */ \
2198 c = apply_binop_on_2_phis(a, b, eval, mode); \
2201 DBG_OPT_ALGSIM0(oldn, c, FS_OPT_CONST_PHI); \
2205 #define HANDLE_UNOP_PHI(eval, a, c) \
2207 if (is_const_Phi(a)) { \
2208 /* check for Op(Phi) */ \
2209 c = apply_unop_on_phi(a, eval); \
2211 DBG_OPT_ALGSIM0(oldn, c, FS_OPT_CONST_PHI); \
2217 * Do the AddSub optimization, then Transform
2218 * Constant folding on Phi
2219 * Add(a,a) -> Mul(a, 2)
2220 * Add(Mul(a, x), a) -> Mul(a, x+1)
2221 * if the mode is integer or float.
2222 * Transform Add(a,-b) into Sub(a,b).
2223 * Reassociation might fold this further.
2225 static ir_node *transform_node_Add(ir_node *n) {
2227 ir_node *a, *b, *c, *oldn = n;
2229 n = transform_node_AddSub(n);
2231 a = get_Add_left(n);
2232 b = get_Add_right(n);
2234 mode = get_irn_mode(n);
2236 if (mode_is_reference(mode)) {
2237 ir_mode *lmode = get_irn_mode(a);
2239 if (is_Const(b) && is_Const_null(b) && mode_is_int(lmode)) {
2240 /* an Add(a, NULL) is a hidden Conv */
2241 dbg_info *dbg = get_irn_dbg_info(n);
2242 return new_rd_Conv(dbg, current_ir_graph, get_nodes_block(n), a, mode);
2246 HANDLE_BINOP_PHI(tarval_add, a, b, c, mode);
2248 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
2249 if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
2252 if (mode_is_num(mode)) {
2253 /* the following code leads to endless recursion when Mul are replaced by a simple instruction chain */
2254 if (!is_arch_dep_running() && a == b && mode_is_int(mode)) {
2255 ir_node *block = get_nodes_block(n);
2258 get_irn_dbg_info(n),
2262 new_r_Const_long(current_ir_graph, block, mode, 2),
2264 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_A);
2269 get_irn_dbg_info(n),
2275 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_MINUS_B);
2280 get_irn_dbg_info(n),
2286 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_MINUS_B);
2289 if (get_mode_arithmetic(mode) == irma_twos_complement) {
2290 /* Here we rely on constants be on the RIGHT side */
2292 ir_node *op = get_Not_op(a);
2294 if (is_Const(b) && is_Const_one(b)) {
2296 ir_node *blk = get_nodes_block(n);
2297 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, blk, op, mode);
2298 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_PLUS_1);
2303 ir_node *blk = get_nodes_block(n);
2304 n = new_r_Const(current_ir_graph, blk, mode, get_mode_minus_one(mode));
2305 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_X_NOT_X);
2310 ir_node *op = get_Not_op(b);
2314 ir_node *blk = get_nodes_block(n);
2315 n = new_r_Const(current_ir_graph, blk, mode, get_mode_minus_one(mode));
2316 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_X_NOT_X);
2323 } /* transform_node_Add */
2326 * returns -cnst or NULL if impossible
2328 static ir_node *const_negate(ir_node *cnst) {
2329 tarval *tv = tarval_neg(get_Const_tarval(cnst));
2330 dbg_info *dbgi = get_irn_dbg_info(cnst);
2331 ir_graph *irg = get_irn_irg(cnst);
2332 ir_node *block = get_nodes_block(cnst);
2333 ir_mode *mode = get_irn_mode(cnst);
2334 if (tv == tarval_bad) return NULL;
2335 return new_rd_Const(dbgi, irg, block, mode, tv);
2339 * Do the AddSub optimization, then Transform
2340 * Constant folding on Phi
2341 * Sub(0,a) -> Minus(a)
2342 * Sub(Mul(a, x), a) -> Mul(a, x-1)
2343 * Sub(Sub(x, y), b) -> Sub(x, Add(y,b))
2344 * Sub(Add(a, x), x) -> a
2345 * Sub(x, Add(x, a)) -> -a
2346 * Sub(x, Const) -> Add(x, -Const)
2348 static ir_node *transform_node_Sub(ir_node *n) {
2353 n = transform_node_AddSub(n);
2355 a = get_Sub_left(n);
2356 b = get_Sub_right(n);
2358 mode = get_irn_mode(n);
2360 if (mode_is_int(mode)) {
2361 ir_mode *lmode = get_irn_mode(a);
2363 if (is_Const(b) && is_Const_null(b) && mode_is_reference(lmode)) {
2364 /* a Sub(a, NULL) is a hidden Conv */
2365 dbg_info *dbg = get_irn_dbg_info(n);
2366 n = new_rd_Conv(dbg, current_ir_graph, get_nodes_block(n), a, mode);
2367 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_CONV);
2371 if (mode == lmode &&
2372 get_mode_arithmetic(mode) == irma_twos_complement &&
2374 get_Const_tarval(a) == get_mode_minus_one(mode)) {
2376 dbg_info *dbg = get_irn_dbg_info(n);
2377 n = new_rd_Not(dbg, current_ir_graph, get_nodes_block(n), b, mode);
2378 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_NOT);
2384 HANDLE_BINOP_PHI(tarval_sub, a, b, c, mode);
2386 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
2387 if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
2390 if (is_Const(b) && get_irn_mode(b) != mode_P) {
2391 /* a - C -> a + (-C) */
2392 ir_node *cnst = const_negate(b);
2394 ir_node *block = get_nodes_block(n);
2395 dbg_info *dbgi = get_irn_dbg_info(n);
2396 ir_graph *irg = get_irn_irg(n);
2398 n = new_rd_Add(dbgi, irg, block, a, cnst, mode);
2399 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2404 if (is_Minus(a)) { /* (-a) - b -> -(a + b) */
2405 ir_graph *irg = current_ir_graph;
2406 dbg_info *dbg = get_irn_dbg_info(n);
2407 ir_node *block = get_nodes_block(n);
2408 ir_node *left = get_Minus_op(a);
2409 ir_node *add = new_rd_Add(dbg, irg, block, left, b, mode);
2411 n = new_rd_Minus(dbg, irg, block, add, mode);
2412 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2414 } else if (is_Minus(b)) { /* a - (-b) -> a + b */
2415 ir_graph *irg = current_ir_graph;
2416 dbg_info *dbg = get_irn_dbg_info(n);
2417 ir_node *block = get_nodes_block(n);
2418 ir_node *right = get_Minus_op(b);
2420 n = new_rd_Add(dbg, irg, block, a, right, mode);
2421 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MINUS);
2423 } else if (is_Sub(b)) {
2424 /* a - (b - c) -> a + (c - b)
2425 * -> (a - b) + c iff (b - c) is a pointer */
2426 ir_graph *irg = current_ir_graph;
2427 dbg_info *s_dbg = get_irn_dbg_info(b);
2428 ir_node *s_block = get_nodes_block(b);
2429 ir_node *s_left = get_Sub_left(b);
2430 ir_node *s_right = get_Sub_right(b);
2431 ir_mode *s_mode = get_irn_mode(b);
2432 if (s_mode == mode_P) {
2433 ir_node *sub = new_rd_Sub(s_dbg, irg, s_block, a, s_left, mode);
2434 dbg_info *a_dbg = get_irn_dbg_info(n);
2435 ir_node *a_block = get_nodes_block(n);
2438 s_right = new_r_Conv(irg, a_block, s_right, mode);
2439 n = new_rd_Add(a_dbg, irg, a_block, sub, s_right, mode);
2441 ir_node *sub = new_rd_Sub(s_dbg, irg, s_block, s_right, s_left, s_mode);
2442 dbg_info *a_dbg = get_irn_dbg_info(n);
2443 ir_node *a_block = get_nodes_block(n);
2445 n = new_rd_Add(a_dbg, irg, a_block, a, sub, mode);
2447 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2449 } else if (is_Mul(b)) { /* a - (b * C) -> a + (b * -C) */
2450 ir_node *m_right = get_Mul_right(b);
2451 if (is_Const(m_right)) {
2452 ir_node *cnst2 = const_negate(m_right);
2453 if (cnst2 != NULL) {
2454 ir_graph *irg = current_ir_graph;
2455 dbg_info *m_dbg = get_irn_dbg_info(b);
2456 ir_node *m_block = get_nodes_block(b);
2457 ir_node *m_left = get_Mul_left(b);
2458 ir_mode *m_mode = get_irn_mode(b);
2459 ir_node *mul = new_rd_Mul(m_dbg, irg, m_block, m_left, cnst2, m_mode);
2460 dbg_info *a_dbg = get_irn_dbg_info(n);
2461 ir_node *a_block = get_nodes_block(n);
2463 n = new_rd_Add(a_dbg, irg, a_block, a, mul, mode);
2464 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2470 /* Beware of Sub(P, P) which cannot be optimized into a simple Minus ... */
2471 if (mode_is_num(mode) && mode == get_irn_mode(a) && is_Const(a) && is_Const_null(a)) {
2473 get_irn_dbg_info(n),
2478 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_0_A);
2482 if (mode_wrap_around(mode)) {
2483 ir_node *left = get_Add_left(a);
2484 ir_node *right = get_Add_right(a);
2486 /* FIXME: Does the Conv's work only for two complement or generally? */
2488 if (mode != get_irn_mode(right)) {
2489 /* This Sub is an effective Cast */
2490 right = new_r_Conv(get_irn_irg(n), get_nodes_block(n), right, mode);
2493 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2495 } else if (right == b) {
2496 if (mode != get_irn_mode(left)) {
2497 /* This Sub is an effective Cast */
2498 left = new_r_Conv(get_irn_irg(n), get_nodes_block(n), left, mode);
2501 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2507 if (mode_wrap_around(mode)) {
2508 ir_node *left = get_Add_left(b);
2509 ir_node *right = get_Add_right(b);
2511 /* FIXME: Does the Conv's work only for two complement or generally? */
2513 ir_mode *r_mode = get_irn_mode(right);
2515 n = new_r_Minus(get_irn_irg(n), get_nodes_block(n), right, r_mode);
2516 if (mode != r_mode) {
2517 /* This Sub is an effective Cast */
2518 n = new_r_Conv(get_irn_irg(n), get_nodes_block(n), n, mode);
2520 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2522 } else if (right == a) {
2523 ir_mode *l_mode = get_irn_mode(left);
2525 n = new_r_Minus(get_irn_irg(n), get_nodes_block(n), left, l_mode);
2526 if (mode != l_mode) {
2527 /* This Sub is an effective Cast */
2528 n = new_r_Conv(get_irn_irg(n), get_nodes_block(n), n, mode);
2530 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2535 if (mode_is_int(mode) && is_Conv(a) && is_Conv(b)) {
2536 ir_mode *mode = get_irn_mode(a);
2538 if (mode == get_irn_mode(b)) {
2540 ir_node *op_a = get_Conv_op(a);
2541 ir_node *op_b = get_Conv_op(b);
2543 /* check if it's allowed to skip the conv */
2544 ma = get_irn_mode(op_a);
2545 mb = get_irn_mode(op_b);
2547 if (mode_is_reference(ma) && mode_is_reference(mb)) {
2548 /* SubInt(ConvInt(aP), ConvInt(bP)) -> SubInt(aP,bP) */
2551 set_Sub_right(n, b);
2557 /* do NOT execute this code if reassociation is enabled, it does the inverse! */
2558 if (!is_reassoc_running() && is_Mul(a)) {
2559 ir_node *ma = get_Mul_left(a);
2560 ir_node *mb = get_Mul_right(a);
2563 ir_node *blk = get_nodes_block(n);
2565 get_irn_dbg_info(n),
2566 current_ir_graph, blk,
2569 get_irn_dbg_info(n),
2570 current_ir_graph, blk,
2572 new_r_Const_long(current_ir_graph, blk, mode, 1),
2575 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MUL_A_X_A);
2577 } else if (mb == b) {
2578 ir_node *blk = get_nodes_block(n);
2580 get_irn_dbg_info(n),
2581 current_ir_graph, blk,
2584 get_irn_dbg_info(n),
2585 current_ir_graph, blk,
2587 new_r_Const_long(current_ir_graph, blk, mode, 1),
2590 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MUL_A_X_A);
2594 if (is_Sub(a)) { /* (x - y) - b -> x - (y + b) */
2595 ir_node *x = get_Sub_left(a);
2596 ir_node *y = get_Sub_right(a);
2597 ir_node *blk = get_nodes_block(n);
2598 ir_mode *m_b = get_irn_mode(b);
2599 ir_mode *m_y = get_irn_mode(y);
2603 /* Determine the right mode for the Add. */
2606 else if (mode_is_reference(m_b))
2608 else if (mode_is_reference(m_y))
2612 * Both modes are different but none is reference,
2613 * happens for instance in SubP(SubP(P, Iu), Is).
2614 * We have two possibilities here: Cast or ignore.
2615 * Currently we ignore this case.
2620 add = new_r_Add(current_ir_graph, blk, y, b, add_mode);
2622 n = new_rd_Sub(get_irn_dbg_info(n), current_ir_graph, blk, x, add, mode);
2623 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_SUB_X_Y_Z);
2627 if (get_mode_arithmetic(mode) == irma_twos_complement) {
2628 if (is_Const(a) && is_Not(b)) {
2629 /* c - ~X = X + (c+1) */
2630 tarval *tv = get_Const_tarval(a);
2632 tv = tarval_add(tv, get_mode_one(mode));
2633 if (tv != tarval_bad) {
2634 ir_node *blk = get_nodes_block(n);
2635 ir_node *c = new_r_Const(current_ir_graph, blk, mode, tv);
2636 n = new_rd_Add(get_irn_dbg_info(n), current_ir_graph, blk, get_Not_op(b), c, mode);
2637 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_C_NOT_X);
2643 } /* transform_node_Sub */
2646 * Several transformation done on n*n=2n bits mul.
2647 * These transformations must be done here because new nodes may be produced.
2649 static ir_node *transform_node_Mul2n(ir_node *n, ir_mode *mode) {
2651 ir_node *a = get_Mul_left(n);
2652 ir_node *b = get_Mul_right(n);
2653 tarval *ta = value_of(a);
2654 tarval *tb = value_of(b);
2655 ir_mode *smode = get_irn_mode(a);
2657 if (ta == get_mode_one(smode)) {
2658 /* (L)1 * (L)b = (L)b */
2659 ir_node *blk = get_nodes_block(n);
2660 n = new_rd_Conv(get_irn_dbg_info(n), current_ir_graph, blk, b, mode);
2661 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
2664 else if (ta == get_mode_minus_one(smode)) {
2665 /* (L)-1 * (L)b = (L)b */
2666 ir_node *blk = get_nodes_block(n);
2667 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, blk, b, smode);
2668 n = new_rd_Conv(get_irn_dbg_info(n), current_ir_graph, blk, n, mode);
2669 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2672 if (tb == get_mode_one(smode)) {
2673 /* (L)a * (L)1 = (L)a */
2674 ir_node *blk = get_irn_n(a, -1);
2675 n = new_rd_Conv(get_irn_dbg_info(n), current_ir_graph, blk, a, mode);
2676 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
2679 else if (tb == get_mode_minus_one(smode)) {
2680 /* (L)a * (L)-1 = (L)-a */
2681 ir_node *blk = get_nodes_block(n);
2682 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, blk, a, smode);
2683 n = new_rd_Conv(get_irn_dbg_info(n), current_ir_graph, blk, n, mode);
2684 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2691 * Transform Mul(a,-1) into -a.
2692 * Do constant evaluation of Phi nodes.
2693 * Do architecture dependent optimizations on Mul nodes
2695 static ir_node *transform_node_Mul(ir_node *n) {
2696 ir_node *c, *oldn = n;
2697 ir_mode *mode = get_irn_mode(n);
2698 ir_node *a = get_Mul_left(n);
2699 ir_node *b = get_Mul_right(n);
2701 if (is_Bad(a) || is_Bad(b))
2704 if (mode != get_irn_mode(a))
2705 return transform_node_Mul2n(n, mode);
2707 HANDLE_BINOP_PHI(tarval_mul, a, b, c, mode);
2709 if (mode_is_signed(mode)) {
2712 if (value_of(a) == get_mode_minus_one(mode))
2714 else if (value_of(b) == get_mode_minus_one(mode))
2717 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, get_nodes_block(n), r, mode);
2718 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2723 if (is_Const(b)) { /* (-a) * const -> a * -const */
2724 ir_node *cnst = const_negate(b);
2726 dbg_info *dbgi = get_irn_dbg_info(n);
2727 ir_node *block = get_nodes_block(n);
2728 n = new_rd_Mul(dbgi, current_ir_graph, block, get_Minus_op(a), cnst, mode);
2729 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2732 } else if (is_Minus(b)) { /* (-a) * (-b) -> a * b */
2733 dbg_info *dbgi = get_irn_dbg_info(n);
2734 ir_node *block = get_nodes_block(n);
2735 n = new_rd_Mul(dbgi, current_ir_graph, block, get_Minus_op(a), get_Minus_op(b), mode);
2736 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_MINUS);
2738 } else if (is_Sub(b)) { /* (-a) * (b - c) -> a * (c - b) */
2739 ir_node *sub_l = get_Sub_left(b);
2740 ir_node *sub_r = get_Sub_right(b);
2741 dbg_info *dbgi = get_irn_dbg_info(n);
2742 ir_graph *irg = current_ir_graph;
2743 ir_node *block = get_nodes_block(n);
2744 ir_node *new_b = new_rd_Sub(dbgi, irg, block, sub_r, sub_l, mode);
2745 n = new_rd_Mul(dbgi, irg, block, get_Minus_op(a), new_b, mode);
2746 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS);
2749 } else if (is_Minus(b)) {
2750 if (is_Sub(a)) { /* (a - b) * (-c) -> (b - a) * c */
2751 ir_node *sub_l = get_Sub_left(a);
2752 ir_node *sub_r = get_Sub_right(a);
2753 dbg_info *dbgi = get_irn_dbg_info(n);
2754 ir_graph *irg = current_ir_graph;
2755 ir_node *block = get_nodes_block(n);
2756 ir_node *new_a = new_rd_Sub(dbgi, irg, block, sub_r, sub_l, mode);
2757 n = new_rd_Mul(dbgi, irg, block, new_a, get_Minus_op(b), mode);
2758 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS);
2762 if (get_mode_arithmetic(mode) == irma_ieee754) {
2764 tarval *tv = get_Const_tarval(a);
2765 if (tarval_ieee754_get_exponent(tv) == 1 && tarval_ieee754_zero_mantissa(tv)
2766 && !tarval_is_negative(tv)) {
2767 /* 2.0 * b = b + b */
2768 n = new_rd_Add(get_irn_dbg_info(n), current_ir_graph, get_nodes_block(n), b, b, mode);
2769 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_A_A);
2773 else if (is_Const(b)) {
2774 tarval *tv = get_Const_tarval(b);
2775 if (tarval_ieee754_get_exponent(tv) == 1 && tarval_ieee754_zero_mantissa(tv)
2776 && !tarval_is_negative(tv)) {
2777 /* a * 2.0 = a + a */
2778 n = new_rd_Add(get_irn_dbg_info(n), current_ir_graph, get_nodes_block(n), a, a, mode);
2779 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_A_A);
2784 return arch_dep_replace_mul_with_shifts(n);
2785 } /* transform_node_Mul */
2788 * Transform a Div Node.
2790 static ir_node *transform_node_Div(ir_node *n) {
2791 ir_mode *mode = get_Div_resmode(n);
2792 ir_node *a = get_Div_left(n);
2793 ir_node *b = get_Div_right(n);
2797 if (is_Const(b) && is_const_Phi(a)) {
2798 /* check for Div(Phi, Const) */
2799 value = apply_binop_on_phi(a, get_Const_tarval(b), tarval_div, mode, 0);
2801 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2805 else if (is_Const(a) && is_const_Phi(b)) {
2806 /* check for Div(Const, Phi) */
2807 value = apply_binop_on_phi(b, get_Const_tarval(a), tarval_div, mode, 1);
2809 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2813 else if (is_const_Phi(a) && is_const_Phi(b)) {
2814 /* check for Div(Phi, Phi) */
2815 value = apply_binop_on_2_phis(a, b, tarval_div, mode);
2817 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2824 if (tv != tarval_bad) {
2825 value = new_Const(get_tarval_mode(tv), tv);
2827 DBG_OPT_CSTEVAL(n, value);
2830 ir_node *a = get_Div_left(n);
2831 ir_node *b = get_Div_right(n);
2832 const ir_node *dummy;
2834 if (a == b && value_not_zero(a, &dummy)) {
2835 /* BEWARE: we can optimize a/a to 1 only if this cannot cause a exception */
2836 value = new_Const(mode, get_mode_one(mode));
2837 DBG_OPT_CSTEVAL(n, value);
2840 if (mode_is_signed(mode) && is_Const(b)) {
2841 tarval *tv = get_Const_tarval(b);
2843 if (tv == get_mode_minus_one(mode)) {
2845 value = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, get_nodes_block(n), a, mode);
2846 DBG_OPT_CSTEVAL(n, value);
2850 /* Try architecture dependent optimization */
2851 value = arch_dep_replace_div_by_const(n);
2859 /* Turn Div into a tuple (mem, jmp, bad, value) */
2860 mem = get_Div_mem(n);
2861 blk = get_nodes_block(n);
2863 /* skip a potential Pin */
2864 mem = skip_Pin(mem);
2865 turn_into_tuple(n, pn_Div_max);
2866 set_Tuple_pred(n, pn_Div_M, mem);
2867 set_Tuple_pred(n, pn_Div_X_regular, new_r_Jmp(current_ir_graph, blk));
2868 set_Tuple_pred(n, pn_Div_X_except, new_Bad());
2869 set_Tuple_pred(n, pn_Div_res, value);
2872 } /* transform_node_Div */
2875 * Transform a Mod node.
2877 static ir_node *transform_node_Mod(ir_node *n) {
2878 ir_mode *mode = get_Mod_resmode(n);
2879 ir_node *a = get_Mod_left(n);
2880 ir_node *b = get_Mod_right(n);
2884 if (is_Const(b) && is_const_Phi(a)) {
2885 /* check for Div(Phi, Const) */
2886 value = apply_binop_on_phi(a, get_Const_tarval(b), tarval_mod, mode, 0);
2888 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2892 else if (is_Const(a) && is_const_Phi(b)) {
2893 /* check for Div(Const, Phi) */
2894 value = apply_binop_on_phi(b, get_Const_tarval(a), tarval_mod, mode, 1);
2896 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2900 else if (is_const_Phi(a) && is_const_Phi(b)) {
2901 /* check for Div(Phi, Phi) */
2902 value = apply_binop_on_2_phis(a, b, tarval_mod, mode);
2904 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2911 if (tv != tarval_bad) {
2912 value = new_Const(get_tarval_mode(tv), tv);
2914 DBG_OPT_CSTEVAL(n, value);
2917 ir_node *a = get_Mod_left(n);
2918 ir_node *b = get_Mod_right(n);
2919 const ir_node *dummy;
2921 if (a == b && value_not_zero(a, &dummy)) {
2922 /* BEWARE: we can optimize a%a to 0 only if this cannot cause a exception */
2923 value = new_Const(mode, get_mode_null(mode));
2924 DBG_OPT_CSTEVAL(n, value);
2927 if (mode_is_signed(mode) && is_Const(b)) {
2928 tarval *tv = get_Const_tarval(b);
2930 if (tv == get_mode_minus_one(mode)) {
2932 value = new_Const(mode, get_mode_null(mode));
2933 DBG_OPT_CSTEVAL(n, value);
2937 /* Try architecture dependent optimization */
2938 value = arch_dep_replace_mod_by_const(n);
2946 /* Turn Mod into a tuple (mem, jmp, bad, value) */
2947 mem = get_Mod_mem(n);
2948 blk = get_nodes_block(n);
2950 /* skip a potential Pin */
2951 mem = skip_Pin(mem);
2952 turn_into_tuple(n, pn_Mod_max);
2953 set_Tuple_pred(n, pn_Mod_M, mem);
2954 set_Tuple_pred(n, pn_Mod_X_regular, new_r_Jmp(current_ir_graph, blk));
2955 set_Tuple_pred(n, pn_Mod_X_except, new_Bad());
2956 set_Tuple_pred(n, pn_Mod_res, value);
2959 } /* transform_node_Mod */
2962 * Transform a DivMod node.
2964 static ir_node *transform_node_DivMod(ir_node *n) {
2965 const ir_node *dummy;
2966 ir_node *a = get_DivMod_left(n);
2967 ir_node *b = get_DivMod_right(n);
2968 ir_mode *mode = get_DivMod_resmode(n);
2973 if (is_Const(b) && is_const_Phi(a)) {
2974 /* check for Div(Phi, Const) */
2975 va = apply_binop_on_phi(a, get_Const_tarval(b), tarval_div, mode, 0);
2976 vb = apply_binop_on_phi(a, get_Const_tarval(b), tarval_mod, mode, 0);
2978 DBG_OPT_ALGSIM0(n, va, FS_OPT_CONST_PHI);
2979 DBG_OPT_ALGSIM0(n, vb, FS_OPT_CONST_PHI);
2983 else if (is_Const(a) && is_const_Phi(b)) {
2984 /* check for Div(Const, Phi) */
2985 va = apply_binop_on_phi(b, get_Const_tarval(a), tarval_div, mode, 1);
2986 vb = apply_binop_on_phi(b, get_Const_tarval(a), tarval_mod, mode, 1);
2988 DBG_OPT_ALGSIM0(n, va, FS_OPT_CONST_PHI);
2989 DBG_OPT_ALGSIM0(n, vb, FS_OPT_CONST_PHI);
2993 else if (is_const_Phi(a) && is_const_Phi(b)) {
2994 /* check for Div(Phi, Phi) */
2995 va = apply_binop_on_2_phis(a, b, tarval_div, mode);
2996 vb = apply_binop_on_2_phis(a, b, tarval_mod, mode);
2998 DBG_OPT_ALGSIM0(n, va, FS_OPT_CONST_PHI);
2999 DBG_OPT_ALGSIM0(n, vb, FS_OPT_CONST_PHI);
3006 if (tb != tarval_bad) {
3007 if (tb == get_mode_one(get_tarval_mode(tb))) {
3009 vb = new_Const(mode, get_mode_null(mode));
3010 DBG_OPT_CSTEVAL(n, vb);
3012 } else if (ta != tarval_bad) {
3013 tarval *resa, *resb;
3014 resa = tarval_div(ta, tb);
3015 if (resa == tarval_bad) return n; /* Causes exception!!! Model by replacing through
3016 Jmp for X result!? */
3017 resb = tarval_mod(ta, tb);
3018 if (resb == tarval_bad) return n; /* Causes exception! */
3019 va = new_Const(mode, resa);
3020 vb = new_Const(mode, resb);
3021 DBG_OPT_CSTEVAL(n, va);
3022 DBG_OPT_CSTEVAL(n, vb);
3024 } else if (mode_is_signed(mode) && tb == get_mode_minus_one(mode)) {
3025 va = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, get_nodes_block(n), a, mode);
3026 vb = new_Const(mode, get_mode_null(mode));
3027 DBG_OPT_CSTEVAL(n, va);
3028 DBG_OPT_CSTEVAL(n, vb);
3030 } else { /* Try architecture dependent optimization */
3033 arch_dep_replace_divmod_by_const(&va, &vb, n);
3034 evaluated = va != NULL;
3036 } else if (a == b) {
3037 if (value_not_zero(a, &dummy)) {
3039 va = new_Const(mode, get_mode_one(mode));
3040 vb = new_Const(mode, get_mode_null(mode));
3041 DBG_OPT_CSTEVAL(n, va);
3042 DBG_OPT_CSTEVAL(n, vb);
3045 /* BEWARE: it is NOT possible to optimize a/a to 1, as this may cause a exception */
3048 } else if (ta == get_mode_null(mode) && value_not_zero(b, &dummy)) {
3049 /* 0 / non-Const = 0 */
3054 if (evaluated) { /* replace by tuple */
3058 mem = get_DivMod_mem(n);
3059 /* skip a potential Pin */
3060 mem = skip_Pin(mem);
3062 blk = get_nodes_block(n);
3063 turn_into_tuple(n, pn_DivMod_max);
3064 set_Tuple_pred(n, pn_DivMod_M, mem);
3065 set_Tuple_pred(n, pn_DivMod_X_regular, new_r_Jmp(current_ir_graph, blk));
3066 set_Tuple_pred(n, pn_DivMod_X_except, new_Bad()); /* no exception */
3067 set_Tuple_pred(n, pn_DivMod_res_div, va);
3068 set_Tuple_pred(n, pn_DivMod_res_mod, vb);
3072 } /* transform_node_DivMod */
3075 * Optimize x / c to x * (1/c)
3077 static ir_node *transform_node_Quot(ir_node *n) {
3078 ir_mode *mode = get_Quot_resmode(n);
3081 if (get_mode_arithmetic(mode) == irma_ieee754) {
3082 ir_node *b = get_Quot_right(n);
3083 tarval *tv = value_of(b);
3085 if (tv != tarval_bad) {
3089 * Floating point constant folding might be disabled here to
3091 * However, as we check for exact result, doing it is safe.
3094 rem = tarval_enable_fp_ops(1);
3095 tv = tarval_quo(get_mode_one(mode), tv);
3096 (void)tarval_enable_fp_ops(rem);
3098 /* Do the transformation if the result is either exact or we are not
3099 using strict rules. */
3100 if (tv != tarval_bad &&
3101 (tarval_ieee754_get_exact() || (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic) == 0)) {
3102 ir_node *blk = get_nodes_block(n);
3103 ir_node *c = new_r_Const(current_ir_graph, blk, mode, tv);
3104 ir_node *a = get_Quot_left(n);
3105 ir_node *m = new_rd_Mul(get_irn_dbg_info(n), current_ir_graph, blk, a, c, mode);
3106 ir_node *mem = get_Quot_mem(n);
3108 /* skip a potential Pin */
3109 mem = skip_Pin(mem);
3110 turn_into_tuple(n, pn_Quot_max);
3111 set_Tuple_pred(n, pn_Quot_M, mem);
3112 set_Tuple_pred(n, pn_Quot_X_regular, new_r_Jmp(current_ir_graph, blk));
3113 set_Tuple_pred(n, pn_Quot_X_except, new_r_Bad(current_ir_graph));
3114 set_Tuple_pred(n, pn_Quot_res, m);
3115 DBG_OPT_ALGSIM1(oldn, a, b, m, FS_OPT_FP_INV_MUL);
3120 } /* transform_node_Quot */
3123 * Optimize Abs(x) into x if x is Confirmed >= 0
3124 * Optimize Abs(x) into -x if x is Confirmed <= 0
3125 * Optimize Abs(-x) int Abs(x)
3127 static ir_node *transform_node_Abs(ir_node *n) {
3128 ir_node *c, *oldn = n;
3129 ir_node *a = get_Abs_op(n);
3132 HANDLE_UNOP_PHI(tarval_abs, a, c);
3134 switch (classify_value_sign(a)) {
3135 case value_classified_negative:
3136 mode = get_irn_mode(n);
3139 * We can replace the Abs by -x here.
3140 * We even could add a new Confirm here
3141 * (if not twos complement)
3143 * Note that -x would create a new node, so we could
3144 * not run it in the equivalent_node() context.
3146 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph,
3147 get_nodes_block(n), a, mode);
3149 DBG_OPT_CONFIRM(oldn, n);
3151 case value_classified_positive:
3152 /* n is positive, Abs is not needed */
3155 DBG_OPT_CONFIRM(oldn, n);
3161 /* Abs(-x) = Abs(x) */
3162 mode = get_irn_mode(n);
3163 n = new_rd_Abs(get_irn_dbg_info(n), current_ir_graph,
3164 get_nodes_block(n), get_Minus_op(a), mode);
3165 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ABS_MINUS_X);
3169 } /* transform_node_Abs */
3172 * Optimize -a CMP -b into b CMP a.
3173 * This works only for for modes where unary Minus
3175 * Note that two-complement integers can Overflow
3176 * so it will NOT work.
3178 * For == and != can be handled in Proj(Cmp)
3180 static ir_node *transform_node_Cmp(ir_node *n) {
3182 ir_node *left = get_Cmp_left(n);
3183 ir_node *right = get_Cmp_right(n);
3185 if (is_Minus(left) && is_Minus(right) &&
3186 !mode_overflow_on_unary_Minus(get_irn_mode(left))) {
3187 ir_node *const new_left = get_Minus_op(right);
3188 ir_node *const new_right = get_Minus_op(left);
3189 n = new_rd_Cmp(get_irn_dbg_info(n), current_ir_graph,
3190 get_nodes_block(n), new_left, new_right);
3191 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CMP_OP_OP);
3194 } /* transform_node_Cmp */
3198 * Transform a Cond node.
3200 * Replace the Cond by a Jmp if it branches on a constant
3203 static ir_node *transform_node_Cond(ir_node *n) {
3206 ir_node *a = get_Cond_selector(n);
3207 tarval *ta = value_of(a);
3209 /* we need block info which is not available in floating irgs */
3210 if (get_irg_pinned(current_ir_graph) == op_pin_state_floats)
3213 if ((ta != tarval_bad) &&
3214 (get_irn_mode(a) == mode_b) &&
3215 (get_opt_unreachable_code())) {
3216 /* It's a boolean Cond, branching on a boolean constant.
3217 Replace it by a tuple (Bad, Jmp) or (Jmp, Bad) */
3218 ir_node *blk = get_nodes_block(n);
3219 jmp = new_r_Jmp(current_ir_graph, blk);
3220 turn_into_tuple(n, pn_Cond_max);
3221 if (ta == tarval_b_true) {
3222 set_Tuple_pred(n, pn_Cond_false, new_Bad());
3223 set_Tuple_pred(n, pn_Cond_true, jmp);
3225 set_Tuple_pred(n, pn_Cond_false, jmp);
3226 set_Tuple_pred(n, pn_Cond_true, new_Bad());
3228 /* We might generate an endless loop, so keep it alive. */
3229 add_End_keepalive(get_irg_end(current_ir_graph), blk);
3232 } /* transform_node_Cond */
3235 * Prototype of a recursive transform function
3236 * for bitwise distributive transformations.
3238 typedef ir_node* (*recursive_transform)(ir_node *n);
3241 * makes use of distributive laws for and, or, eor
3242 * and(a OP c, b OP c) -> and(a, b) OP c
3243 * note, might return a different op than n
3245 static ir_node *transform_bitwise_distributive(ir_node *n,
3246 recursive_transform trans_func)
3249 ir_node *a = get_binop_left(n);
3250 ir_node *b = get_binop_right(n);
3251 ir_op *op = get_irn_op(a);
3252 ir_op *op_root = get_irn_op(n);
3254 if(op != get_irn_op(b))
3257 if (op == op_Conv) {
3258 ir_node *a_op = get_Conv_op(a);
3259 ir_node *b_op = get_Conv_op(b);
3260 ir_mode *a_mode = get_irn_mode(a_op);
3261 ir_mode *b_mode = get_irn_mode(b_op);
3262 if(a_mode == b_mode && (mode_is_int(a_mode) || a_mode == mode_b)) {
3263 ir_node *blk = get_nodes_block(n);
3266 set_binop_left(n, a_op);
3267 set_binop_right(n, b_op);
3268 set_irn_mode(n, a_mode);
3270 n = new_r_Conv(current_ir_graph, blk, n, get_irn_mode(oldn));
3272 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_SHIFT_AND);
3278 /* nothing to gain here */
3282 if (op == op_Shrs || op == op_Shr || op == op_Shl
3283 || op == op_And || op == op_Or || op == op_Eor) {
3284 ir_node *a_left = get_binop_left(a);
3285 ir_node *a_right = get_binop_right(a);
3286 ir_node *b_left = get_binop_left(b);
3287 ir_node *b_right = get_binop_right(b);
3289 ir_node *op1 = NULL;
3290 ir_node *op2 = NULL;
3292 if (is_op_commutative(op)) {
3293 if (a_left == b_left) {
3297 } else if(a_left == b_right) {
3301 } else if(a_right == b_left) {
3307 if(a_right == b_right) {
3314 /* (a sop c) & (b sop c) => (a & b) sop c */
3315 ir_node *blk = get_nodes_block(n);
3317 ir_node *new_n = exact_copy(n);
3318 set_binop_left(new_n, op1);
3319 set_binop_right(new_n, op2);
3320 new_n = trans_func(new_n);
3322 if(op_root == op_Eor && op == op_Or) {
3323 dbg_info *dbgi = get_irn_dbg_info(n);
3324 ir_graph *irg = current_ir_graph;
3325 ir_mode *mode = get_irn_mode(c);
3327 c = new_rd_Not(dbgi, irg, blk, c, mode);
3328 n = new_rd_And(dbgi, irg, blk, new_n, c, mode);
3331 set_nodes_block(n, blk);
3332 set_binop_left(n, new_n);
3333 set_binop_right(n, c);
3334 add_identities(current_ir_graph->value_table, n);
3337 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_SHIFT_AND);
3348 static ir_node *transform_node_And(ir_node *n) {
3349 ir_node *c, *oldn = n;
3350 ir_node *a = get_And_left(n);
3351 ir_node *b = get_And_right(n);
3354 mode = get_irn_mode(n);
3355 HANDLE_BINOP_PHI(tarval_and, a, b, c, mode);
3357 /* we can evaluate 2 Projs of the same Cmp */
3358 if (mode == mode_b && is_Proj(a) && is_Proj(b)) {
3359 ir_node *pred_a = get_Proj_pred(a);
3360 ir_node *pred_b = get_Proj_pred(b);
3361 if (pred_a == pred_b) {
3362 dbg_info *dbgi = get_irn_dbg_info(n);
3363 ir_node *block = get_nodes_block(pred_a);
3364 pn_Cmp pn_a = get_Proj_proj(a);
3365 pn_Cmp pn_b = get_Proj_proj(b);
3366 /* yes, we can simply calculate with pncs */
3367 pn_Cmp new_pnc = pn_a & pn_b;
3369 return new_rd_Proj(dbgi, current_ir_graph, block, pred_a, mode_b, new_pnc);
3374 ir_node *op = get_Not_op(b);
3376 ir_node *ba = get_And_left(op);
3377 ir_node *bb = get_And_right(op);
3379 /* it's enough to test the following cases due to normalization! */
3380 if (get_Or_left(a) == ba && get_Or_right(a) == bb) {
3381 /* (a|b) & ~(a&b) = a^b */
3382 ir_node *block = get_nodes_block(n);
3384 n = new_rd_Eor(get_irn_dbg_info(n), current_ir_graph, block, ba, bb, mode);
3385 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_TO_EOR);
3393 ir_node *op = get_Not_op(a);
3395 ir_node *aa = get_And_left(op);
3396 ir_node *ab = get_And_right(op);
3398 /* it's enough to test the following cases due to normalization! */
3399 if (get_Or_left(b) == aa && get_Or_right(b) == ab) {
3400 /* (a|b) & ~(a&b) = a^b */
3401 ir_node *block = get_nodes_block(n);
3403 n = new_rd_Eor(get_irn_dbg_info(n), current_ir_graph, block, aa, ab, mode);
3404 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_TO_EOR);
3411 ir_node *al = get_Eor_left(a);
3412 ir_node *ar = get_Eor_right(a);
3415 /* (b ^ a) & b -> ~a & b */
3416 dbg_info *dbg = get_irn_dbg_info(n);
3417 ir_node *block = get_nodes_block(n);
3419 ar = new_rd_Not(dbg, current_ir_graph, block, ar, mode);
3420 n = new_rd_And(dbg, current_ir_graph, block, ar, b, mode);
3421 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3425 /* (a ^ b) & b -> ~a & b */
3426 dbg_info *dbg = get_irn_dbg_info(n);
3427 ir_node *block = get_nodes_block(n);
3429 al = new_rd_Not(dbg, current_ir_graph, block, al, mode);
3430 n = new_rd_And(dbg, current_ir_graph, block, al, b, mode);
3431 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3436 ir_node *bl = get_Eor_left(b);
3437 ir_node *br = get_Eor_right(b);
3440 /* a & (a ^ b) -> a & ~b */
3441 dbg_info *dbg = get_irn_dbg_info(n);
3442 ir_node *block = get_nodes_block(n);
3444 br = new_rd_Not(dbg, current_ir_graph, block, br, mode);
3445 n = new_rd_And(dbg, current_ir_graph, block, br, a, mode);
3446 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3450 /* a & (b ^ a) -> a & ~b */
3451 dbg_info *dbg = get_irn_dbg_info(n);
3452 ir_node *block = get_nodes_block(n);
3454 bl = new_rd_Not(dbg, current_ir_graph, block, bl, mode);
3455 n = new_rd_And(dbg, current_ir_graph, block, bl, a, mode);
3456 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3460 if (is_Not(a) && is_Not(b)) {
3461 /* ~a & ~b = ~(a|b) */
3462 ir_node *block = get_nodes_block(n);
3463 ir_mode *mode = get_irn_mode(n);
3467 n = new_rd_Or(get_irn_dbg_info(n), current_ir_graph, block, a, b, mode);
3468 n = new_rd_Not(get_irn_dbg_info(n), current_ir_graph, block, n, mode);
3469 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_DEMORGAN);
3473 n = transform_bitwise_distributive(n, transform_node_And);
3476 } /* transform_node_And */
3481 static ir_node *transform_node_Eor(ir_node *n) {
3482 ir_node *c, *oldn = n;
3483 ir_node *a = get_Eor_left(n);
3484 ir_node *b = get_Eor_right(n);
3485 ir_mode *mode = get_irn_mode(n);
3487 HANDLE_BINOP_PHI(tarval_eor, a, b, c, mode);
3489 /* we can evaluate 2 Projs of the same Cmp */
3490 if (mode == mode_b && is_Proj(a) && is_Proj(b)) {
3491 ir_node *pred_a = get_Proj_pred(a);
3492 ir_node *pred_b = get_Proj_pred(b);
3493 if(pred_a == pred_b) {
3494 dbg_info *dbgi = get_irn_dbg_info(n);
3495 ir_node *block = get_nodes_block(pred_a);
3496 pn_Cmp pn_a = get_Proj_proj(a);
3497 pn_Cmp pn_b = get_Proj_proj(b);
3498 /* yes, we can simply calculate with pncs */
3499 pn_Cmp new_pnc = pn_a ^ pn_b;
3501 return new_rd_Proj(dbgi, current_ir_graph, block, pred_a, mode_b,
3508 n = new_rd_Const(get_irn_dbg_info(n), current_ir_graph, get_nodes_block(n),
3509 mode, get_mode_null(mode));
3510 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_A_A);
3511 } else if (mode == mode_b &&
3513 is_Const(b) && is_Const_one(b) &&
3514 is_Cmp(get_Proj_pred(a))) {
3515 /* The Eor negates a Cmp. The Cmp has the negated result anyways! */
3516 n = new_r_Proj(current_ir_graph, get_nodes_block(n), get_Proj_pred(a),
3517 mode_b, get_negated_pnc(get_Proj_proj(a), mode));
3519 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT_BOOL);
3520 } else if (is_Const(b)) {
3521 if (is_Not(a)) { /* ~x ^ const -> x ^ ~const */
3522 ir_node *cnst = new_Const(mode, tarval_not(get_Const_tarval(b)));
3523 ir_node *not_op = get_Not_op(a);
3524 dbg_info *dbg = get_irn_dbg_info(n);
3525 ir_graph *irg = current_ir_graph;
3526 ir_node *block = get_nodes_block(n);
3527 ir_mode *mode = get_irn_mode(n);
3528 n = new_rd_Eor(dbg, irg, block, not_op, cnst, mode);
3530 } else if (is_Const_all_one(b)) { /* x ^ 1...1 -> ~1 */
3531 n = new_r_Not(current_ir_graph, get_nodes_block(n), a, mode);
3532 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3535 n = transform_bitwise_distributive(n, transform_node_Eor);
3539 } /* transform_node_Eor */
3544 static ir_node *transform_node_Not(ir_node *n) {
3545 ir_node *c, *oldn = n;
3546 ir_node *a = get_Not_op(n);
3547 ir_mode *mode = get_irn_mode(n);
3549 HANDLE_UNOP_PHI(tarval_not,a,c);
3551 /* check for a boolean Not */
3552 if (mode == mode_b &&
3554 is_Cmp(get_Proj_pred(a))) {
3555 /* We negate a Cmp. The Cmp has the negated result anyways! */
3556 n = new_r_Proj(current_ir_graph, get_nodes_block(n), get_Proj_pred(a),
3557 mode_b, get_negated_pnc(get_Proj_proj(a), mode_b));
3558 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_CMP);
3562 ir_node *eor_b = get_Eor_right(a);
3563 if (is_Const(eor_b)) { /* ~(x ^ const) -> x ^ ~const */
3564 ir_node *cnst = new_Const(mode, tarval_not(get_Const_tarval(eor_b)));
3565 ir_node *eor_a = get_Eor_left(a);
3566 dbg_info *dbg = get_irn_dbg_info(n);
3567 ir_graph *irg = current_ir_graph;
3568 ir_node *block = get_nodes_block(n);
3569 ir_mode *mode = get_irn_mode(n);
3570 n = new_rd_Eor(dbg, irg, block, eor_a, cnst, mode);
3574 if (get_mode_arithmetic(mode) == irma_twos_complement) {
3575 if (is_Minus(a)) { /* ~-x -> x + -1 */
3576 dbg_info *dbg = get_irn_dbg_info(n);
3577 ir_graph *irg = current_ir_graph;
3578 ir_node *block = get_nodes_block(n);
3579 ir_node *add_l = get_Minus_op(a);
3580 ir_node *add_r = new_rd_Const(dbg, irg, block, mode, get_mode_minus_one(mode));
3581 n = new_rd_Add(dbg, irg, block, add_l, add_r, mode);
3582 } else if (is_Add(a)) {
3583 ir_node *add_r = get_Add_right(a);
3584 if (is_Const(add_r) && is_Const_all_one(add_r)) {
3585 /* ~(x + -1) = -x */
3586 ir_node *op = get_Add_left(a);
3587 ir_node *blk = get_nodes_block(n);
3588 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, blk, op, get_irn_mode(n));
3589 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_MINUS_1);
3594 } /* transform_node_Not */
3597 * Transform a Minus.
3601 * -(a >>u (size-1)) = a >>s (size-1)
3602 * -(a >>s (size-1)) = a >>u (size-1)
3603 * -(a * const) -> a * -const
3605 static ir_node *transform_node_Minus(ir_node *n) {
3606 ir_node *c, *oldn = n;
3607 ir_node *a = get_Minus_op(n);
3610 HANDLE_UNOP_PHI(tarval_neg,a,c);
3612 mode = get_irn_mode(a);
3613 if (get_mode_arithmetic(mode) == irma_twos_complement) {
3614 /* the following rules are only to twos-complement */
3617 ir_node *op = get_Not_op(a);
3618 tarval *tv = get_mode_one(mode);
3619 ir_node *blk = get_nodes_block(n);
3620 ir_node *c = new_r_Const(current_ir_graph, blk, mode, tv);
3621 n = new_rd_Add(get_irn_dbg_info(n), current_ir_graph, blk, op, c, mode);
3622 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_MINUS_NOT);
3626 ir_node *c = get_Shr_right(a);
3629 tarval *tv = get_Const_tarval(c);
3631 if (tarval_is_long(tv) && get_tarval_long(tv) == (int) get_mode_size_bits(mode) - 1) {
3632 /* -(a >>u (size-1)) = a >>s (size-1) */
3633 ir_node *v = get_Shr_left(a);
3635 n = new_rd_Shrs(get_irn_dbg_info(n), current_ir_graph, get_nodes_block(n), v, c, mode);
3636 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_PREDICATE);
3642 ir_node *c = get_Shrs_right(a);
3645 tarval *tv = get_Const_tarval(c);
3647 if (tarval_is_long(tv) && get_tarval_long(tv) == (int) get_mode_size_bits(mode) - 1) {
3648 /* -(a >>s (size-1)) = a >>u (size-1) */
3649 ir_node *v = get_Shrs_left(a);
3651 n = new_rd_Shr(get_irn_dbg_info(n), current_ir_graph, get_nodes_block(n), v, c, mode);
3652 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_PREDICATE);
3659 /* - (a-b) = b - a */
3660 ir_node *la = get_Sub_left(a);
3661 ir_node *ra = get_Sub_right(a);
3662 ir_node *blk = get_nodes_block(n);
3664 n = new_rd_Sub(get_irn_dbg_info(n), current_ir_graph, blk, ra, la, mode);
3665 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_MINUS_SUB);
3669 if (is_Mul(a)) { /* -(a * const) -> a * -const */
3670 ir_node *mul_l = get_Mul_left(a);
3671 ir_node *mul_r = get_Mul_right(a);
3672 tarval *tv = value_of(mul_r);
3673 if (tv != tarval_bad) {
3674 tv = tarval_neg(tv);
3675 if (tv != tarval_bad) {
3676 ir_node *cnst = new_Const(mode, tv);
3677 dbg_info *dbg = get_irn_dbg_info(a);
3678 ir_graph *irg = current_ir_graph;
3679 ir_node *block = get_nodes_block(a);
3680 n = new_rd_Mul(dbg, irg, block, mul_l, cnst, mode);
3681 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_MINUS_MUL_C);
3688 } /* transform_node_Minus */
3691 * Transform a Cast_type(Const) into a new Const_type
3693 static ir_node *transform_node_Cast(ir_node *n) {
3695 ir_node *pred = get_Cast_op(n);
3696 ir_type *tp = get_irn_type(n);
3698 if (is_Const(pred) && get_Const_type(pred) != tp) {
3699 n = new_rd_Const_type(NULL, current_ir_graph, get_irn_n(pred, -1), get_irn_mode(pred),
3700 get_Const_tarval(pred), tp);
3701 DBG_OPT_CSTEVAL(oldn, n);
3702 } else if (is_SymConst(pred) && get_SymConst_value_type(pred) != tp) {
3703 n = new_rd_SymConst_type(NULL, current_ir_graph, get_irn_n(pred, -1), get_irn_mode(pred),
3704 get_SymConst_symbol(pred), get_SymConst_kind(pred), tp);
3705 DBG_OPT_CSTEVAL(oldn, n);
3709 } /* transform_node_Cast */
3712 * Transform a Proj(Load) with a non-null address.
3714 static ir_node *transform_node_Proj_Load(ir_node *proj) {
3715 if (get_opt_ldst_only_null_ptr_exceptions()) {
3716 if (get_irn_mode(proj) == mode_X) {
3717 ir_node *load = get_Proj_pred(proj);
3719 /* get the Load address */
3720 const ir_node *addr = get_Load_ptr(load);
3721 const ir_node *confirm;
3723 if (value_not_null(addr, &confirm)) {
3724 if (confirm == NULL) {
3725 /* this node may float if it did not depend on a Confirm */
3726 set_irn_pinned(load, op_pin_state_floats);
3728 if (get_Proj_proj(proj) == pn_Load_X_except) {
3729 DBG_OPT_EXC_REM(proj);
3730 return get_irg_bad(current_ir_graph);
3732 ir_node *blk = get_nodes_block(load);
3733 return new_r_Jmp(current_ir_graph, blk);
3739 } /* transform_node_Proj_Load */
3742 * Transform a Proj(Store) with a non-null address.
3744 static ir_node *transform_node_Proj_Store(ir_node *proj) {
3745 if (get_opt_ldst_only_null_ptr_exceptions()) {
3746 if (get_irn_mode(proj) == mode_X) {
3747 ir_node *store = get_Proj_pred(proj);
3749 /* get the load/store address */
3750 const ir_node *addr = get_Store_ptr(store);
3751 const ir_node *confirm;
3753 if (value_not_null(addr, &confirm)) {
3754 if (confirm == NULL) {
3755 /* this node may float if it did not depend on a Confirm */
3756 set_irn_pinned(store, op_pin_state_floats);
3758 if (get_Proj_proj(proj) == pn_Store_X_except) {
3759 DBG_OPT_EXC_REM(proj);
3760 return get_irg_bad(current_ir_graph);
3762 ir_node *blk = get_nodes_block(store);
3763 return new_r_Jmp(current_ir_graph, blk);
3769 } /* transform_node_Proj_Store */
3772 * Transform a Proj(Div) with a non-zero value.
3773 * Removes the exceptions and routes the memory to the NoMem node.
3775 static ir_node *transform_node_Proj_Div(ir_node *proj) {
3776 ir_node *div = get_Proj_pred(proj);
3777 ir_node *b = get_Div_right(div);
3778 ir_node *res, *new_mem;
3779 const ir_node *confirm;
3782 if (value_not_zero(b, &confirm)) {
3783 /* div(x, y) && y != 0 */
3784 if (confirm == NULL) {
3785 /* we are sure we have a Const != 0 */
3786 new_mem = get_Div_mem(div);
3787 new_mem = skip_Pin(new_mem);
3788 set_Div_mem(div, new_mem);
3789 set_irn_pinned(div, op_pin_state_floats);
3792 proj_nr = get_Proj_proj(proj);
3794 case pn_Div_X_regular:
3795 return new_r_Jmp(current_ir_graph, get_irn_n(div, -1));
3797 case pn_Div_X_except:
3798 /* we found an exception handler, remove it */
3799 DBG_OPT_EXC_REM(proj);
3803 res = get_Div_mem(div);
3804 new_mem = get_irg_no_mem(current_ir_graph);
3807 /* This node can only float up to the Confirm block */
3808 new_mem = new_r_Pin(current_ir_graph, get_nodes_block(confirm), new_mem);
3810 set_irn_pinned(div, op_pin_state_floats);
3811 /* this is a Div without exception, we can remove the memory edge */
3812 set_Div_mem(div, new_mem);
3817 } /* transform_node_Proj_Div */
3820 * Transform a Proj(Mod) with a non-zero value.
3821 * Removes the exceptions and routes the memory to the NoMem node.
3823 static ir_node *transform_node_Proj_Mod(ir_node *proj) {
3824 ir_node *mod = get_Proj_pred(proj);
3825 ir_node *b = get_Mod_right(mod);
3826 ir_node *res, *new_mem;
3827 const ir_node *confirm;
3830 if (value_not_zero(b, &confirm)) {
3831 /* mod(x, y) && y != 0 */
3832 proj_nr = get_Proj_proj(proj);
3834 if (confirm == NULL) {
3835 /* we are sure we have a Const != 0 */
3836 new_mem = get_Mod_mem(mod);
3837 new_mem = skip_Pin(new_mem);
3838 set_Mod_mem(mod, new_mem);
3839 set_irn_pinned(mod, op_pin_state_floats);
3844 case pn_Mod_X_regular:
3845 return new_r_Jmp(current_ir_graph, get_irn_n(mod, -1));
3847 case pn_Mod_X_except:
3848 /* we found an exception handler, remove it */
3849 DBG_OPT_EXC_REM(proj);
3853 res = get_Mod_mem(mod);
3854 new_mem = get_irg_no_mem(current_ir_graph);
3857 /* This node can only float up to the Confirm block */
3858 new_mem = new_r_Pin(current_ir_graph, get_nodes_block(confirm), new_mem);
3860 /* this is a Mod without exception, we can remove the memory edge */
3861 set_Mod_mem(mod, new_mem);
3864 if (get_Mod_left(mod) == b) {
3865 /* a % a = 0 if a != 0 */
3866 ir_mode *mode = get_irn_mode(proj);
3867 ir_node *res = new_Const(mode, get_mode_null(mode));
3869 DBG_OPT_CSTEVAL(mod, res);
3875 } /* transform_node_Proj_Mod */
3878 * Transform a Proj(DivMod) with a non-zero value.
3879 * Removes the exceptions and routes the memory to the NoMem node.
3881 static ir_node *transform_node_Proj_DivMod(ir_node *proj) {
3882 ir_node *divmod = get_Proj_pred(proj);
3883 ir_node *b = get_DivMod_right(divmod);
3884 ir_node *res, *new_mem;
3885 const ir_node *confirm;
3888 if (value_not_zero(b, &confirm)) {
3889 /* DivMod(x, y) && y != 0 */
3890 proj_nr = get_Proj_proj(proj);
3892 if (confirm == NULL) {
3893 /* we are sure we have a Const != 0 */
3894 new_mem = get_DivMod_mem(divmod);
3895 new_mem = skip_Pin(new_mem);
3896 set_DivMod_mem(divmod, new_mem);
3897 set_irn_pinned(divmod, op_pin_state_floats);
3902 case pn_DivMod_X_regular:
3903 return new_r_Jmp(current_ir_graph, get_irn_n(divmod, -1));
3905 case pn_DivMod_X_except:
3906 /* we found an exception handler, remove it */
3907 DBG_OPT_EXC_REM(proj);
3911 res = get_DivMod_mem(divmod);
3912 new_mem = get_irg_no_mem(current_ir_graph);
3915 /* This node can only float up to the Confirm block */
3916 new_mem = new_r_Pin(current_ir_graph, get_nodes_block(confirm), new_mem);
3918 /* this is a DivMod without exception, we can remove the memory edge */
3919 set_DivMod_mem(divmod, new_mem);
3922 case pn_DivMod_res_mod:
3923 if (get_DivMod_left(divmod) == b) {
3924 /* a % a = 0 if a != 0 */
3925 ir_mode *mode = get_irn_mode(proj);
3926 ir_node *res = new_Const(mode, get_mode_null(mode));
3928 DBG_OPT_CSTEVAL(divmod, res);
3934 } /* transform_node_Proj_DivMod */
3937 * Optimizes jump tables (CondIs or CondIu) by removing all impossible cases.
3939 static ir_node *transform_node_Proj_Cond(ir_node *proj) {
3940 if (get_opt_unreachable_code()) {
3941 ir_node *n = get_Proj_pred(proj);
3942 ir_node *b = get_Cond_selector(n);
3944 if (mode_is_int(get_irn_mode(b))) {
3945 tarval *tb = value_of(b);
3947 if (tb != tarval_bad) {
3948 /* we have a constant switch */
3949 long num = get_Proj_proj(proj);
3951 if (num != get_Cond_defaultProj(n)) { /* we cannot optimize default Proj's yet */
3952 if (get_tarval_long(tb) == num) {
3953 /* Do NOT create a jump here, or we will have 2 control flow ops
3954 * in a block. This case is optimized away in optimize_cf(). */
3957 /* this case will NEVER be taken, kill it */
3958 return get_irg_bad(current_ir_graph);
3965 } /* transform_node_Proj_Cond */
3968 * Create a 0 constant of given mode.
3970 static ir_node *create_zero_const(ir_mode *mode) {
3971 tarval *tv = get_mode_null(mode);
3972 ir_node *cnst = new_Const(mode, tv);
3977 /* the order of the values is important! */
3978 typedef enum const_class {
3984 static const_class classify_const(const ir_node* n)
3986 if (is_Const(n)) return const_const;
3987 if (is_irn_constlike(n)) return const_like;
3992 * Determines whether r is more constlike or has a larger index (in that order)
3995 static int operands_are_normalized(const ir_node *l, const ir_node *r)
3997 const const_class l_order = classify_const(l);
3998 const const_class r_order = classify_const(r);
4000 l_order > r_order ||
4001 (l_order == r_order && get_irn_idx(l) <= get_irn_idx(r));
4005 * Normalizes and optimizes Cmp nodes.
4007 static ir_node *transform_node_Proj_Cmp(ir_node *proj) {
4008 ir_node *n = get_Proj_pred(proj);
4009 ir_node *left = get_Cmp_left(n);
4010 ir_node *right = get_Cmp_right(n);
4013 ir_mode *mode = NULL;
4014 long proj_nr = get_Proj_proj(proj);
4016 /* we can evaluate some cases directly */
4019 return new_Const(mode_b, get_tarval_b_false());
4021 return new_Const(mode_b, get_tarval_b_true());
4023 if (!mode_is_float(get_irn_mode(left)))
4024 return new_Const(mode_b, get_tarval_b_true());
4030 /* remove Casts of both sides */
4031 left = skip_Cast(left);
4032 right = skip_Cast(right);
4034 /* Remove unnecessary conversions */
4035 /* TODO handle constants */
4036 if (is_Conv(left) && is_Conv(right)) {
4037 ir_mode *mode = get_irn_mode(left);
4038 ir_node *op_left = get_Conv_op(left);
4039 ir_node *op_right = get_Conv_op(right);
4040 ir_mode *mode_left = get_irn_mode(op_left);
4041 ir_mode *mode_right = get_irn_mode(op_right);
4043 if (smaller_mode(mode_left, mode) && smaller_mode(mode_right, mode)
4044 && mode_left != mode_b && mode_right != mode_b) {
4045 ir_graph *irg = current_ir_graph;
4046 ir_node *block = get_nodes_block(n);
4048 if (mode_left == mode_right) {
4052 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV_CONV);
4053 } else if (smaller_mode(mode_left, mode_right)) {
4054 left = new_r_Conv(irg, block, op_left, mode_right);
4057 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV);
4058 } else if (smaller_mode(mode_right, mode_left)) {
4060 right = new_r_Conv(irg, block, op_right, mode_left);
4062 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV);
4067 /* remove operation on both sides if possible */
4068 if (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) {
4070 * The following operations are NOT safe for floating point operations, for instance
4071 * 1.0 + inf == 2.0 + inf, =/=> x == y
4073 if (mode_is_int(get_irn_mode(left))) {
4074 unsigned lop = get_irn_opcode(left);
4076 if (lop == get_irn_opcode(right)) {
4077 ir_node *ll, *lr, *rl, *rr;
4079 /* same operation on both sides, try to remove */
4083 /* ~a CMP ~b => a CMP b, -a CMP -b ==> a CMP b */
4084 left = get_unop_op(left);
4085 right = get_unop_op(right);
4087 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4090 ll = get_Add_left(left);
4091 lr = get_Add_right(left);
4092 rl = get_Add_left(right);
4093 rr = get_Add_right(right);
4096 /* X + a CMP X + b ==> a CMP b */
4100 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4101 } else if (ll == rr) {
4102 /* X + a CMP b + X ==> a CMP b */
4106 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4107 } else if (lr == rl) {
4108 /* a + X CMP X + b ==> a CMP b */
4112 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4113 } else if (lr == rr) {
4114 /* a + X CMP b + X ==> a CMP b */
4118 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4122 ll = get_Sub_left(left);
4123 lr = get_Sub_right(left);
4124 rl = get_Sub_left(right);
4125 rr = get_Sub_right(right);
4128 /* X - a CMP X - b ==> a CMP b */
4132 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4133 } else if (lr == rr) {
4134 /* a - X CMP b - X ==> a CMP b */
4138 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4142 if (get_Rotl_right(left) == get_Rotl_right(right)) {
4143 /* a ROTL X CMP b ROTL X ==> a CMP b */
4144 left = get_Rotl_left(left);
4145 right = get_Rotl_left(right);
4147 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4155 /* X+A == A, A+X == A, A-X == A -> X == 0 */
4156 if (is_Add(left) || is_Sub(left)) {
4157 ir_node *ll = get_binop_left(left);
4158 ir_node *lr = get_binop_right(left);
4160 if (lr == right && is_Add(left)) {
4167 right = create_zero_const(get_irn_mode(left));
4169 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4172 if (is_Add(right) || is_Sub(right)) {
4173 ir_node *rl = get_binop_left(right);
4174 ir_node *rr = get_binop_right(right);
4176 if (rr == left && is_Add(right)) {
4183 right = create_zero_const(get_irn_mode(left));
4185 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4188 if (is_And(left) && is_Const(right)) {
4189 ir_node *ll = get_binop_left(left);
4190 ir_node *lr = get_binop_right(left);
4191 if (is_Shr(ll) && is_Const(lr)) {
4192 /* Cmp((x >>u c1) & c2, c3) = Cmp(x & (c2 << c1), c3 << c1) */
4193 ir_node *block = get_nodes_block(n);
4194 ir_mode *mode = get_irn_mode(left);
4196 ir_node *llr = get_Shr_right(ll);
4197 if (is_Const(llr)) {
4198 ir_graph *irg = current_ir_graph;
4199 dbg_info *dbg = get_irn_dbg_info(left);
4201 tarval *c1 = get_Const_tarval(llr);
4202 tarval *c2 = get_Const_tarval(lr);
4203 tarval *c3 = get_Const_tarval(right);
4204 tarval *mask = tarval_shl(c2, c1);
4205 tarval *value = tarval_shl(c3, c1);
4207 left = new_rd_And(dbg, irg, block, get_Shr_left(ll), new_Const(mode, mask), mode);
4208 right = new_Const(mode, value);
4213 } /* mode_is_int(...) */
4214 } /* proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg */
4216 /* replace mode_b compares with ands/ors */
4217 if (get_irn_mode(left) == mode_b) {
4218 ir_graph *irg = current_ir_graph;
4219 ir_node *block = get_nodes_block(n);
4223 case pn_Cmp_Le: bres = new_r_Or( irg, block, new_r_Not(irg, block, left, mode_b), right, mode_b); break;
4224 case pn_Cmp_Lt: bres = new_r_And(irg, block, new_r_Not(irg, block, left, mode_b), right, mode_b); break;
4225 case pn_Cmp_Ge: bres = new_r_Or( irg, block, left, new_r_Not(irg, block, right, mode_b), mode_b); break;
4226 case pn_Cmp_Gt: bres = new_r_And(irg, block, left, new_r_Not(irg, block, right, mode_b), mode_b); break;
4227 case pn_Cmp_Lg: bres = new_r_Eor(irg, block, left, right, mode_b); break;
4228 case pn_Cmp_Eq: bres = new_r_Not(irg, block, new_r_Eor(irg, block, left, right, mode_b), mode_b); break;
4229 default: bres = NULL;
4232 DBG_OPT_ALGSIM0(n, bres, FS_OPT_CMP_TO_BOOL);
4238 * First step: normalize the compare op
4239 * by placing the constant on the right side
4240 * or moving the lower address node to the left.
4242 if (!operands_are_normalized(left, right)) {
4248 proj_nr = get_inversed_pnc(proj_nr);
4253 * Second step: Try to reduce the magnitude
4254 * of a constant. This may help to generate better code
4255 * later and may help to normalize more compares.
4256 * Of course this is only possible for integer values.
4258 tv = value_of(right);
4259 if (tv != tarval_bad) {
4260 mode = get_irn_mode(right);
4262 /* TODO extend to arbitrary constants */
4263 if (is_Conv(left) && tarval_is_null(tv)) {
4264 ir_node *op = get_Conv_op(left);
4265 ir_mode *op_mode = get_irn_mode(op);
4268 * UpConv(x) REL 0 ==> x REL 0
4270 if (get_mode_size_bits(mode) > get_mode_size_bits(op_mode) &&
4271 ((proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) ||
4272 mode_is_signed(mode) || !mode_is_signed(op_mode))) {
4273 tv = get_mode_null(op_mode);
4277 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV);
4281 if (tv != tarval_bad) {
4282 /* the following optimization is possible on modes without Overflow
4283 * on Unary Minus or on == and !=:
4284 * -a CMP c ==> a swap(CMP) -c
4286 * Beware: for two-complement Overflow may occur, so only == and != can
4287 * be optimized, see this:
4288 * -MININT < 0 =/=> MININT > 0 !!!
4290 if (is_Minus(left) &&
4291 (!mode_overflow_on_unary_Minus(mode) ||
4292 (mode_is_int(mode) && (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg)))) {
4293 tv = tarval_neg(tv);
4295 if (tv != tarval_bad) {
4296 left = get_Minus_op(left);
4297 proj_nr = get_inversed_pnc(proj_nr);
4299 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4301 } else if (is_Not(left) && (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg)) {
4302 /* Not(a) ==/!= c ==> a ==/!= Not(c) */
4303 tv = tarval_not(tv);
4305 if (tv != tarval_bad) {
4306 left = get_Not_op(left);
4308 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4312 /* for integer modes, we have more */
4313 if (mode_is_int(mode)) {
4314 /* Ne includes Unordered which is not possible on integers.
4315 * However, frontends often use this wrong, so fix it here */
4316 if (proj_nr & pn_Cmp_Uo) {
4317 proj_nr &= ~pn_Cmp_Uo;
4318 set_Proj_proj(proj, proj_nr);
4321 /* c > 0 : a < c ==> a <= (c-1) a >= c ==> a > (c-1) */
4322 if ((proj_nr == pn_Cmp_Lt || proj_nr == pn_Cmp_Ge) &&
4323 tarval_cmp(tv, get_mode_null(mode)) == pn_Cmp_Gt) {
4324 tv = tarval_sub(tv, get_mode_one(mode), NULL);
4326 if (tv != tarval_bad) {
4327 proj_nr ^= pn_Cmp_Eq;
4329 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CNST_MAGN);
4332 /* c < 0 : a > c ==> a >= (c+1) a <= c ==> a < (c+1) */
4333 else if ((proj_nr == pn_Cmp_Gt || proj_nr == pn_Cmp_Le) &&
4334 tarval_cmp(tv, get_mode_null(mode)) == pn_Cmp_Lt) {
4335 tv = tarval_add(tv, get_mode_one(mode));
4337 if (tv != tarval_bad) {
4338 proj_nr ^= pn_Cmp_Eq;
4340 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CNST_MAGN);
4344 /* the following reassociations work only for == and != */
4345 if (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) {
4347 #if 0 /* Might be not that good in general */
4348 /* a-b == 0 ==> a == b, a-b != 0 ==> a != b */
4349 if (tarval_is_null(tv) && is_Sub(left)) {
4350 right = get_Sub_right(left);
4351 left = get_Sub_left(left);
4353 tv = value_of(right);
4355 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4359 if (tv != tarval_bad) {
4360 /* a-c1 == c2 ==> a == c2+c1, a-c1 != c2 ==> a != c2+c1 */
4362 ir_node *c1 = get_Sub_right(left);
4363 tarval *tv2 = value_of(c1);
4365 if (tv2 != tarval_bad) {
4366 tv2 = tarval_add(tv, value_of(c1));
4368 if (tv2 != tarval_bad) {
4369 left = get_Sub_left(left);
4372 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4376 /* a+c1 == c2 ==> a == c2-c1, a+c1 != c2 ==> a != c2-c1 */
4377 else if (is_Add(left)) {
4378 ir_node *a_l = get_Add_left(left);
4379 ir_node *a_r = get_Add_right(left);
4383 if (is_Const(a_l)) {
4385 tv2 = value_of(a_l);
4388 tv2 = value_of(a_r);
4391 if (tv2 != tarval_bad) {
4392 tv2 = tarval_sub(tv, tv2, NULL);
4394 if (tv2 != tarval_bad) {
4398 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4402 /* -a == c ==> a == -c, -a != c ==> a != -c */
4403 else if (is_Minus(left)) {
4404 tarval *tv2 = tarval_sub(get_mode_null(mode), tv, NULL);
4406 if (tv2 != tarval_bad) {
4407 left = get_Minus_op(left);
4410 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4415 /* the following reassociations work only for <= */
4416 else if (proj_nr == pn_Cmp_Le || proj_nr == pn_Cmp_Lt) {
4417 if (tv != tarval_bad) {
4418 /* c >= 0 : Abs(a) <= c ==> (unsigned)(a + c) <= 2*c */
4419 if (is_Abs(left)) { // TODO something is missing here
4425 if (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) {
4426 switch (get_irn_opcode(left)) {
4430 c1 = get_And_right(left);
4433 * And(x, C1) == C2 ==> FALSE if C2 & C1 != C2
4434 * And(x, C1) != C2 ==> TRUE if C2 & C1 != C2
4436 tarval *mask = tarval_and(get_Const_tarval(c1), tv);
4438 /* TODO: move to constant evaluation */
4439 tv = proj_nr == pn_Cmp_Eq ? get_tarval_b_false() : get_tarval_b_true();
4440 c1 = new_Const(mode_b, tv);
4441 DBG_OPT_CSTEVAL(proj, c1);
4445 if (tarval_is_single_bit(tv)) {
4447 * optimization for AND:
4449 * And(x, C) == C ==> And(x, C) != 0
4450 * And(x, C) != C ==> And(X, C) == 0
4452 * if C is a single Bit constant.
4455 /* check for Constant's match. We have check hare the tarvals,
4456 because our const might be changed */
4457 if (get_Const_tarval(c1) == tv) {
4458 /* fine: do the transformation */
4459 tv = get_mode_null(get_tarval_mode(tv));
4460 proj_nr ^= pn_Cmp_Leg;
4462 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CNST_MAGN);
4468 c1 = get_Or_right(left);
4469 if (is_Const(c1) && tarval_is_null(tv)) {
4471 * Or(x, C) == 0 && C != 0 ==> FALSE
4472 * Or(x, C) != 0 && C != 0 ==> TRUE
4474 if (! tarval_is_null(get_Const_tarval(c1))) {
4475 /* TODO: move to constant evaluation */
4476 tv = proj_nr == pn_Cmp_Eq ? get_tarval_b_false() : get_tarval_b_true();
4477 c1 = new_Const(mode_b, tv);
4478 DBG_OPT_CSTEVAL(proj, c1);
4485 * optimize x << c1 == c into x & (-1 >>u c1) == c >> c1 if c & (-1 << c1) == c
4487 * optimize x << c1 != c into x & (-1 >>u c1) != c >> c1 if c & (-1 << c1) == c
4490 c1 = get_Shl_right(left);
4492 tarval *tv1 = get_Const_tarval(c1);
4493 ir_mode *mode = get_irn_mode(left);
4494 tarval *minus1 = get_mode_all_one(mode);
4495 tarval *amask = tarval_shr(minus1, tv1);
4496 tarval *cmask = tarval_shl(minus1, tv1);
4499 if (tarval_and(tv, cmask) != tv) {
4500 /* condition not met */
4501 tv = proj_nr == pn_Cmp_Eq ? get_tarval_b_false() : get_tarval_b_true();
4502 c1 = new_Const(mode_b, tv);
4503 DBG_OPT_CSTEVAL(proj, c1);
4506 sl = get_Shl_left(left);
4507 blk = get_nodes_block(n);
4508 left = new_rd_And(get_irn_dbg_info(left), current_ir_graph, blk, sl, new_Const(mode, amask), mode);
4509 tv = tarval_shr(tv, tv1);
4511 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_SHF_TO_AND);
4516 * optimize x >>u c1 == c into x & (-1 << c1) == c << c1 if c & (-1 >>u c1) == c
4518 * optimize x >>u c1 != c into x & (-1 << c1) != c << c1 if c & (-1 >>u c1) == c
4521 c1 = get_Shr_right(left);
4523 tarval *tv1 = get_Const_tarval(c1);
4524 ir_mode *mode = get_irn_mode(left);
4525 tarval *minus1 = get_mode_all_one(mode);
4526 tarval *amask = tarval_shl(minus1, tv1);
4527 tarval *cmask = tarval_shr(minus1, tv1);
4530 if (tarval_and(tv, cmask) != tv) {
4531 /* condition not met */
4532 tv = proj_nr == pn_Cmp_Eq ? get_tarval_b_false() : get_tarval_b_true();
4533 c1 = new_Const(mode_b, tv);
4534 DBG_OPT_CSTEVAL(proj, c1);
4537 sl = get_Shr_left(left);
4538 blk = get_nodes_block(n);
4539 left = new_rd_And(get_irn_dbg_info(left), current_ir_graph, blk, sl, new_Const(mode, amask), mode);
4540 tv = tarval_shl(tv, tv1);
4542 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_SHF_TO_AND);
4547 * optimize x >>s c1 == c into x & (-1 << c1) == c << c1 if (c >>s (BITS - c1)) \in {0,-1}
4549 * optimize x >>s c1 != c into x & (-1 << c1) != c << c1 if (c >>s (BITS - c1)) \in {0,-1}
4552 c1 = get_Shrs_right(left);
4554 tarval *tv1 = get_Const_tarval(c1);
4555 ir_mode *mode = get_irn_mode(left);
4556 tarval *minus1 = get_mode_all_one(mode);
4557 tarval *amask = tarval_shl(minus1, tv1);
4558 tarval *cond = new_tarval_from_long(get_mode_size_bits(mode), get_tarval_mode(tv1));
4561 cond = tarval_sub(cond, tv1, NULL);
4562 cond = tarval_shrs(tv, cond);
4564 if (!tarval_is_all_one(cond) && !tarval_is_null(cond)) {
4565 /* condition not met */
4566 tv = proj_nr == pn_Cmp_Eq ? get_tarval_b_false() : get_tarval_b_true();
4567 c1 = new_Const(mode_b, tv);
4568 DBG_OPT_CSTEVAL(proj, c1);
4571 sl = get_Shrs_left(left);
4572 blk = get_nodes_block(n);
4573 left = new_rd_And(get_irn_dbg_info(left), current_ir_graph, blk, sl, new_Const(mode, amask), mode);
4574 tv = tarval_shl(tv, tv1);
4576 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_SHF_TO_AND);
4581 } /* tarval != bad */
4584 if (changed & 2) /* need a new Const */
4585 right = new_Const(mode, tv);
4587 if ((proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) && is_Const(right) && is_Const_null(right) && is_Proj(left)) {
4588 ir_node *op = get_Proj_pred(left);
4590 if ((is_Mod(op) && get_Proj_proj(left) == pn_Mod_res) ||
4591 (is_DivMod(op) && get_Proj_proj(left) == pn_DivMod_res_mod)) {
4592 ir_node *c = get_binop_right(op);
4595 tarval *tv = get_Const_tarval(c);
4597 if (tarval_is_single_bit(tv)) {
4598 /* special case: (x % 2^n) CMP 0 ==> x & (2^n-1) CMP 0 */
4599 ir_node *v = get_binop_left(op);
4600 ir_node *blk = get_irn_n(op, -1);
4601 ir_mode *mode = get_irn_mode(v);
4603 tv = tarval_sub(tv, get_mode_one(mode), NULL);
4604 left = new_rd_And(get_irn_dbg_info(op), current_ir_graph, blk, v, new_Const(mode, tv), mode);
4606 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_MOD_TO_AND);
4613 ir_node *block = get_nodes_block(n);
4615 /* create a new compare */
4616 n = new_rd_Cmp(get_irn_dbg_info(n), current_ir_graph, block, left, right);
4617 proj = new_rd_Proj(get_irn_dbg_info(proj), current_ir_graph, block, n, get_irn_mode(proj), proj_nr);
4621 } /* transform_node_Proj_Cmp */
4624 * Optimize CopyB(mem, x, x) into a Nop.
4626 static ir_node *transform_node_Proj_CopyB(ir_node *proj) {
4627 ir_node *copyb = get_Proj_pred(proj);
4628 ir_node *a = get_CopyB_dst(copyb);
4629 ir_node *b = get_CopyB_src(copyb);
4632 switch (get_Proj_proj(proj)) {
4633 case pn_CopyB_X_regular:
4634 /* Turn CopyB into a tuple (mem, jmp, bad, bad) */
4635 DBG_OPT_EXC_REM(proj);
4636 proj = new_r_Jmp(current_ir_graph, get_nodes_block(copyb));
4638 case pn_CopyB_M_except:
4639 case pn_CopyB_X_except:
4640 DBG_OPT_EXC_REM(proj);
4641 proj = get_irg_bad(current_ir_graph);
4648 } /* transform_node_Proj_CopyB */
4651 * Optimize Bounds(idx, idx, upper) into idx.
4653 static ir_node *transform_node_Proj_Bound(ir_node *proj) {
4654 ir_node *oldn = proj;
4655 ir_node *bound = get_Proj_pred(proj);
4656 ir_node *idx = get_Bound_index(bound);
4657 ir_node *pred = skip_Proj(idx);
4660 if (idx == get_Bound_lower(bound))
4662 else if (is_Bound(pred)) {
4664 * idx was Bounds checked in the same MacroBlock previously,
4665 * it is still valid if lower <= pred_lower && pred_upper <= upper.
4667 ir_node *lower = get_Bound_lower(bound);
4668 ir_node *upper = get_Bound_upper(bound);
4669 if (get_Bound_lower(pred) == lower &&
4670 get_Bound_upper(pred) == upper &&
4671 get_irn_MacroBlock(bound) == get_irn_MacroBlock(pred)) {
4673 * One could expect that we simply return the previous
4674 * Bound here. However, this would be wrong, as we could
4675 * add an exception Proj to a new location then.
4676 * So, we must turn in into a tuple.
4682 /* Turn Bound into a tuple (mem, jmp, bad, idx) */
4683 switch (get_Proj_proj(proj)) {
4685 DBG_OPT_EXC_REM(proj);
4686 proj = get_Bound_mem(bound);
4688 case pn_Bound_X_except:
4689 DBG_OPT_EXC_REM(proj);
4690 proj = get_irg_bad(current_ir_graph);
4694 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NOP);
4696 case pn_Bound_X_regular:
4697 DBG_OPT_EXC_REM(proj);
4698 proj = new_r_Jmp(current_ir_graph, get_nodes_block(bound));
4705 } /* transform_node_Proj_Bound */
4708 * Does all optimizations on nodes that must be done on it's Proj's
4709 * because of creating new nodes.
4711 static ir_node *transform_node_Proj(ir_node *proj) {
4712 ir_node *n = get_Proj_pred(proj);
4714 if (n->op->ops.transform_node_Proj)
4715 return n->op->ops.transform_node_Proj(proj);
4717 } /* transform_node_Proj */
4720 * Move Confirms down through Phi nodes.
4722 static ir_node *transform_node_Phi(ir_node *phi) {
4724 ir_mode *mode = get_irn_mode(phi);
4726 if (mode_is_reference(mode)) {
4727 n = get_irn_arity(phi);
4729 /* Beware of Phi0 */
4731 ir_node *pred = get_irn_n(phi, 0);
4732 ir_node *bound, *new_Phi, *block, **in;
4735 if (! is_Confirm(pred))
4738 bound = get_Confirm_bound(pred);
4739 pnc = get_Confirm_cmp(pred);
4741 NEW_ARR_A(ir_node *, in, n);
4742 in[0] = get_Confirm_value(pred);
4744 for (i = 1; i < n; ++i) {
4745 pred = get_irn_n(phi, i);
4747 if (! is_Confirm(pred) ||
4748 get_Confirm_bound(pred) != bound ||
4749 get_Confirm_cmp(pred) != pnc)
4751 in[i] = get_Confirm_value(pred);
4753 /* move the Confirm nodes "behind" the Phi */
4754 block = get_irn_n(phi, -1);
4755 new_Phi = new_r_Phi(current_ir_graph, block, n, in, get_irn_mode(phi));
4756 return new_r_Confirm(current_ir_graph, block, new_Phi, bound, pnc);
4760 } /* transform_node_Phi */
4763 * Returns the operands of a commutative bin-op, if one operand is
4764 * a const, it is returned as the second one.
4766 static void get_comm_Binop_Ops(ir_node *binop, ir_node **a, ir_node **c) {
4767 ir_node *op_a = get_binop_left(binop);
4768 ir_node *op_b = get_binop_right(binop);
4770 assert(is_op_commutative(get_irn_op(binop)));
4772 if (is_Const(op_a)) {
4779 } /* get_comm_Binop_Ops */
4782 * Optimize a Or(And(Or(And(v,c4),c3),c2),c1) pattern if possible.
4783 * Such pattern may arise in bitfield stores.
4785 * value c4 value c4 & c2
4786 * AND c3 AND c1 | c3
4793 * AND c1 ===> OR if (c1 | c2) == 0x111..11
4796 static ir_node *transform_node_Or_bf_store(ir_node *or) {
4799 ir_node *and_l, *c3;
4800 ir_node *value, *c4;
4801 ir_node *new_and, *new_const, *block;
4802 ir_mode *mode = get_irn_mode(or);
4804 tarval *tv1, *tv2, *tv3, *tv4, *tv, *n_tv4, *n_tv2;
4807 get_comm_Binop_Ops(or, &and, &c1);
4808 if (!is_Const(c1) || !is_And(and))
4811 get_comm_Binop_Ops(and, &or_l, &c2);
4815 tv1 = get_Const_tarval(c1);
4816 tv2 = get_Const_tarval(c2);
4818 tv = tarval_or(tv1, tv2);
4819 if (tarval_is_all_one(tv)) {
4820 /* the AND does NOT clear a bit with isn't set by the OR */
4821 set_Or_left(or, or_l);
4822 set_Or_right(or, c1);
4824 /* check for more */
4831 get_comm_Binop_Ops(or_l, &and_l, &c3);
4832 if (!is_Const(c3) || !is_And(and_l))
4835 get_comm_Binop_Ops(and_l, &value, &c4);
4839 /* ok, found the pattern, check for conditions */
4840 assert(mode == get_irn_mode(and));
4841 assert(mode == get_irn_mode(or_l));
4842 assert(mode == get_irn_mode(and_l));
4844 tv3 = get_Const_tarval(c3);
4845 tv4 = get_Const_tarval(c4);
4847 tv = tarval_or(tv4, tv2);
4848 if (!tarval_is_all_one(tv)) {
4849 /* have at least one 0 at the same bit position */
4853 n_tv4 = tarval_not(tv4);
4854 if (tv3 != tarval_and(tv3, n_tv4)) {
4855 /* bit in the or_mask is outside the and_mask */
4859 n_tv2 = tarval_not(tv2);
4860 if (tv1 != tarval_and(tv1, n_tv2)) {
4861 /* bit in the or_mask is outside the and_mask */
4865 /* ok, all conditions met */
4866 block = get_irn_n(or, -1);
4868 new_and = new_r_And(current_ir_graph, block,
4869 value, new_r_Const(current_ir_graph, block, mode, tarval_and(tv4, tv2)), mode);
4871 new_const = new_r_Const(current_ir_graph, block, mode, tarval_or(tv3, tv1));
4873 set_Or_left(or, new_and);
4874 set_Or_right(or, new_const);
4876 /* check for more */
4878 } /* transform_node_Or_bf_store */
4881 * Optimize an Or(shl(x, c), shr(x, bits - c)) into a Rotl
4883 static ir_node *transform_node_Or_Rotl(ir_node *or) {
4884 ir_mode *mode = get_irn_mode(or);
4885 ir_node *shl, *shr, *block;
4886 ir_node *irn, *x, *c1, *c2, *v, *sub, *n, *rotval;
4889 if (! mode_is_int(mode))
4892 shl = get_binop_left(or);
4893 shr = get_binop_right(or);
4902 } else if (!is_Shl(shl)) {
4904 } else if (!is_Shr(shr)) {
4907 x = get_Shl_left(shl);
4908 if (x != get_Shr_left(shr))
4911 c1 = get_Shl_right(shl);
4912 c2 = get_Shr_right(shr);
4913 if (is_Const(c1) && is_Const(c2)) {
4914 tv1 = get_Const_tarval(c1);
4915 if (! tarval_is_long(tv1))
4918 tv2 = get_Const_tarval(c2);
4919 if (! tarval_is_long(tv2))
4922 if (get_tarval_long(tv1) + get_tarval_long(tv2)
4923 != (int) get_mode_size_bits(mode))
4926 /* yet, condition met */
4927 block = get_nodes_block(or);
4929 n = new_r_Rotl(current_ir_graph, block, x, c1, mode);
4931 DBG_OPT_ALGSIM1(or, shl, shr, n, FS_OPT_OR_SHFT_TO_ROTL);
4938 rotval = sub; /* a Rot right is not supported, so use a rot left */
4939 } else if (is_Sub(c2)) {
4945 if (get_Sub_right(sub) != v)
4948 c1 = get_Sub_left(sub);
4952 tv1 = get_Const_tarval(c1);
4953 if (! tarval_is_long(tv1))
4956 if (get_tarval_long(tv1) != (int) get_mode_size_bits(mode))
4959 /* yet, condition met */
4960 block = get_nodes_block(or);
4962 n = new_r_Rotl(current_ir_graph, block, x, rotval, mode);
4964 DBG_OPT_ALGSIM0(or, n, FS_OPT_OR_SHFT_TO_ROTL);
4966 } /* transform_node_Or_Rotl */
4971 static ir_node *transform_node_Or(ir_node *n) {
4972 ir_node *c, *oldn = n;
4973 ir_node *a = get_Or_left(n);
4974 ir_node *b = get_Or_right(n);
4977 if (is_Not(a) && is_Not(b)) {
4978 /* ~a | ~b = ~(a&b) */
4979 ir_node *block = get_nodes_block(n);
4981 mode = get_irn_mode(n);
4984 n = new_rd_And(get_irn_dbg_info(n), current_ir_graph, block, a, b, mode);
4985 n = new_rd_Not(get_irn_dbg_info(n), current_ir_graph, block, n, mode);
4986 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_DEMORGAN);
4990 /* we can evaluate 2 Projs of the same Cmp */
4991 if (get_irn_mode(n) == mode_b && is_Proj(a) && is_Proj(b)) {
4992 ir_node *pred_a = get_Proj_pred(a);
4993 ir_node *pred_b = get_Proj_pred(b);
4994 if (pred_a == pred_b) {
4995 dbg_info *dbgi = get_irn_dbg_info(n);
4996 ir_node *block = get_nodes_block(pred_a);
4997 pn_Cmp pn_a = get_Proj_proj(a);
4998 pn_Cmp pn_b = get_Proj_proj(b);
4999 /* yes, we can simply calculate with pncs */
5000 pn_Cmp new_pnc = pn_a | pn_b;
5002 return new_rd_Proj(dbgi, current_ir_graph, block, pred_a, mode_b,
5007 mode = get_irn_mode(n);
5008 HANDLE_BINOP_PHI(tarval_or, a, b, c, mode);
5010 n = transform_node_Or_bf_store(n);
5011 n = transform_node_Or_Rotl(n);
5015 n = transform_bitwise_distributive(n, transform_node_Or);
5018 } /* transform_node_Or */
5022 static ir_node *transform_node(ir_node *n);
5025 * Optimize (a >> c1) >> c2), works for Shr, Shrs, Shl, Rotl.
5027 * Should be moved to reassociation?
5029 static ir_node *transform_node_shift(ir_node *n) {
5030 ir_node *left, *right;
5032 tarval *tv1, *tv2, *res;
5033 ir_node *in[2], *irn, *block;
5035 left = get_binop_left(n);
5037 /* different operations */
5038 if (get_irn_op(left) != get_irn_op(n))
5041 right = get_binop_right(n);
5042 tv1 = value_of(right);
5043 if (tv1 == tarval_bad)
5046 tv2 = value_of(get_binop_right(left));
5047 if (tv2 == tarval_bad)
5050 res = tarval_add(tv1, tv2);
5051 mode = get_irn_mode(n);
5053 /* beware: a simple replacement works only, if res < modulo shift */
5055 int modulo_shf = get_mode_modulo_shift(mode);
5056 if (modulo_shf > 0) {
5057 tarval *modulo = new_tarval_from_long(modulo_shf,
5058 get_tarval_mode(res));
5060 assert(modulo_shf >= (int) get_mode_size_bits(mode));
5062 /* shifting too much */
5063 if (!(tarval_cmp(res, modulo) & pn_Cmp_Lt)) {
5065 ir_graph *irg = get_irn_irg(n);
5066 ir_node *block = get_nodes_block(n);
5067 dbg_info *dbgi = get_irn_dbg_info(n);
5068 ir_mode *smode = get_irn_mode(right);
5069 ir_node *cnst = new_Const(smode, new_tarval_from_long(get_mode_size_bits(mode) - 1, smode));
5070 return new_rd_Shrs(dbgi, irg, block, get_binop_left(left),
5074 return new_Const(mode, get_mode_null(mode));
5078 res = tarval_mod(res, new_tarval_from_long(get_mode_size_bits(mode), get_tarval_mode(res)));
5081 /* ok, we can replace it */
5082 block = get_nodes_block(n);
5084 in[0] = get_binop_left(left);
5085 in[1] = new_r_Const(current_ir_graph, block, get_tarval_mode(res), res);
5087 irn = new_ir_node(NULL, current_ir_graph, block, get_irn_op(n), mode, 2, in);
5089 DBG_OPT_ALGSIM0(n, irn, FS_OPT_REASSOC_SHIFT);
5091 return transform_node(irn);
5092 } /* transform_node_shift */
5095 * normalisation: (x & c1) >> c2 to (x >> c2) & (c1 >> c2)
5097 * - and, or, xor instead of &
5098 * - Shl, Shr, Shrs, rotl instead of >>
5099 * (with a special case for Or/Xor + Shrs)
5101 static ir_node *transform_node_bitop_shift(ir_node *n) {
5103 ir_node *right = get_binop_right(n);
5104 ir_mode *mode = get_irn_mode(n);
5105 ir_node *bitop_left;
5106 ir_node *bitop_right;
5118 assert(is_Shrs(n) || is_Shr(n) || is_Shl(n) || is_Rotl(n));
5120 if (!is_Const(right))
5123 left = get_binop_left(n);
5124 op_left = get_irn_op(left);
5125 if (op_left != op_And && op_left != op_Or && op_left != op_Eor)
5128 /* doing it with Shrs is not legal if the Or/Eor affects the topmost bit */
5129 if (is_Shrs(n) && (op_left == op_Or || op_left == op_Eor)) {
5130 /* TODO: test if sign bit is affectes */
5134 bitop_right = get_binop_right(left);
5135 if (!is_Const(bitop_right))
5138 bitop_left = get_binop_left(left);
5140 irg = get_irn_irg(n);
5141 block = get_nodes_block(n);
5142 dbgi = get_irn_dbg_info(n);
5143 tv1 = get_Const_tarval(bitop_right);
5144 tv2 = get_Const_tarval(right);
5146 assert(get_tarval_mode(tv1) == mode);
5149 new_shift = new_rd_Shl(dbgi, irg, block, bitop_left, right, mode);
5150 tv_shift = tarval_shl(tv1, tv2);
5151 } else if(is_Shr(n)) {
5152 new_shift = new_rd_Shr(dbgi, irg, block, bitop_left, right, mode);
5153 tv_shift = tarval_shr(tv1, tv2);
5154 } else if(is_Shrs(n)) {
5155 new_shift = new_rd_Shrs(dbgi, irg, block, bitop_left, right, mode);
5156 tv_shift = tarval_shrs(tv1, tv2);
5159 new_shift = new_rd_Rotl(dbgi, irg, block, bitop_left, right, mode);
5160 tv_shift = tarval_rotl(tv1, tv2);
5163 assert(get_tarval_mode(tv_shift) == mode);
5164 new_const = new_Const(mode, tv_shift);
5166 if (op_left == op_And) {
5167 new_bitop = new_rd_And(dbgi, irg, block, new_shift, new_const, mode);
5168 } else if(op_left == op_Or) {
5169 new_bitop = new_rd_Or(dbgi, irg, block, new_shift, new_const, mode);
5171 assert(op_left == op_Eor);
5172 new_bitop = new_rd_Eor(dbgi, irg, block, new_shift, new_const, mode);
5180 * (x << c1) >> c2 <=> x OP (c2-c1) & ((-1 << c1) >> c2)
5182 * (x >> c1) << c2 <=> x OP (c2-c1) & ((-1 >> c1) << c2)
5183 * (also with x >>s c1 when c1>=c2)
5185 static ir_node *transform_node_shl_shr(ir_node *n) {
5187 ir_node *right = get_binop_right(n);
5203 assert(is_Shl(n) || is_Shr(n) || is_Shrs(n));
5205 if (!is_Const(right))
5208 left = get_binop_left(n);
5209 mode = get_irn_mode(n);
5210 if (is_Shl(n) && (is_Shr(left) || is_Shrs(left))) {
5211 ir_node *shr_right = get_binop_right(left);
5213 if (!is_Const(shr_right))
5216 x = get_binop_left(left);
5217 tv_shr = get_Const_tarval(shr_right);
5218 tv_shl = get_Const_tarval(right);
5220 if (is_Shrs(left)) {
5221 /* shrs variant only allowed if c1 >= c2 */
5222 if (! (tarval_cmp(tv_shl, tv_shr) & pn_Cmp_Ge))
5225 tv_mask = tarval_shrs(get_mode_all_one(mode), tv_shr);
5228 tv_mask = tarval_shr(get_mode_all_one(mode), tv_shr);
5230 tv_mask = tarval_shl(tv_mask, tv_shl);
5231 } else if(is_Shr(n) && is_Shl(left)) {
5232 ir_node *shl_right = get_Shl_right(left);
5234 if (!is_Const(shl_right))
5237 x = get_Shl_left(left);
5238 tv_shr = get_Const_tarval(right);
5239 tv_shl = get_Const_tarval(shl_right);
5241 tv_mask = tarval_shl(get_mode_all_one(mode), tv_shl);
5242 tv_mask = tarval_shr(tv_mask, tv_shr);
5247 if (get_tarval_mode(tv_shl) != get_tarval_mode(tv_shr)) {
5248 tv_shl = tarval_convert_to(tv_shl, get_tarval_mode(tv_shr));
5251 assert(tv_mask != tarval_bad);
5252 assert(get_tarval_mode(tv_mask) == mode);
5254 irg = get_irn_irg(n);
5255 block = get_nodes_block(n);
5256 dbgi = get_irn_dbg_info(n);
5258 pnc = tarval_cmp(tv_shl, tv_shr);
5259 if (pnc == pn_Cmp_Lt || pnc == pn_Cmp_Eq) {
5260 tv_shift = tarval_sub(tv_shr, tv_shl, NULL);
5261 new_const = new_Const(get_tarval_mode(tv_shift), tv_shift);
5263 new_shift = new_rd_Shrs(dbgi, irg, block, x, new_const, mode);
5265 new_shift = new_rd_Shr(dbgi, irg, block, x, new_const, mode);
5268 assert(pnc == pn_Cmp_Gt);
5269 tv_shift = tarval_sub(tv_shl, tv_shr, NULL);
5270 new_const = new_Const(get_tarval_mode(tv_shift), tv_shift);
5271 new_shift = new_rd_Shl(dbgi, irg, block, x, new_const, mode);
5274 new_const = new_Const(mode, tv_mask);
5275 new_and = new_rd_And(dbgi, irg, block, new_shift, new_const, mode);
5283 static ir_node *transform_node_Shr(ir_node *n) {
5284 ir_node *c, *oldn = n;
5285 ir_node *left = get_Shr_left(n);
5286 ir_node *right = get_Shr_right(n);
5287 ir_mode *mode = get_irn_mode(n);
5289 HANDLE_BINOP_PHI(tarval_shr, left, right, c, mode);
5290 n = transform_node_shift(n);
5293 n = transform_node_shl_shr(n);
5295 n = transform_node_bitop_shift(n);
5298 } /* transform_node_Shr */
5303 static ir_node *transform_node_Shrs(ir_node *n) {
5304 ir_node *c, *oldn = n;
5305 ir_node *a = get_Shrs_left(n);
5306 ir_node *b = get_Shrs_right(n);
5307 ir_mode *mode = get_irn_mode(n);
5309 HANDLE_BINOP_PHI(tarval_shrs, a, b, c, mode);
5310 n = transform_node_shift(n);
5313 n = transform_node_bitop_shift(n);
5316 } /* transform_node_Shrs */
5321 static ir_node *transform_node_Shl(ir_node *n) {
5322 ir_node *c, *oldn = n;
5323 ir_node *a = get_Shl_left(n);
5324 ir_node *b = get_Shl_right(n);
5325 ir_mode *mode = get_irn_mode(n);
5327 HANDLE_BINOP_PHI(tarval_shl, a, b, c, mode);
5328 n = transform_node_shift(n);
5331 n = transform_node_shl_shr(n);
5333 n = transform_node_bitop_shift(n);
5336 } /* transform_node_Shl */
5341 static ir_node *transform_node_Rotl(ir_node *n) {
5342 ir_node *c, *oldn = n;
5343 ir_node *a = get_Rotl_left(n);
5344 ir_node *b = get_Rotl_right(n);
5345 ir_mode *mode = get_irn_mode(n);
5347 HANDLE_BINOP_PHI(tarval_rotl, a, b, c, mode);
5348 n = transform_node_shift(n);
5351 n = transform_node_bitop_shift(n);
5354 } /* transform_node_Rotl */
5359 static ir_node *transform_node_Conv(ir_node *n) {
5360 ir_node *c, *oldn = n;
5361 ir_mode *mode = get_irn_mode(n);
5362 ir_node *a = get_Conv_op(n);
5364 if (mode != mode_b && is_const_Phi(a)) {
5365 /* Do NOT optimize mode_b Conv's, this leads to remaining
5366 * Phib nodes later, because the conv_b_lower operation
5367 * is instantly reverted, when it tries to insert a Convb.
5369 c = apply_conv_on_phi(a, mode);
5371 DBG_OPT_ALGSIM0(oldn, c, FS_OPT_CONST_PHI);
5376 if (is_Unknown(a)) { /* Conv_A(Unknown_B) -> Unknown_A */
5377 return new_r_Unknown(current_ir_graph, mode);
5380 if (mode_is_reference(mode) &&
5381 get_mode_size_bits(mode) == get_mode_size_bits(get_irn_mode(a)) &&
5383 ir_node *l = get_Add_left(a);
5384 ir_node *r = get_Add_right(a);
5385 dbg_info *dbgi = get_irn_dbg_info(a);
5386 ir_node *block = get_nodes_block(n);
5388 ir_node *lop = get_Conv_op(l);
5389 if(get_irn_mode(lop) == mode) {
5390 /* ConvP(AddI(ConvI(P), x)) -> AddP(P, x) */
5391 n = new_rd_Add(dbgi, current_ir_graph, block, lop, r, mode);
5396 ir_node *rop = get_Conv_op(r);
5397 if(get_irn_mode(rop) == mode) {
5398 /* ConvP(AddI(x, ConvI(P))) -> AddP(x, P) */
5399 n = new_rd_Add(dbgi, current_ir_graph, block, l, rop, mode);
5406 } /* transform_node_Conv */
5409 * Remove dead blocks and nodes in dead blocks
5410 * in keep alive list. We do not generate a new End node.
5412 static ir_node *transform_node_End(ir_node *n) {
5413 int i, j, n_keepalives = get_End_n_keepalives(n);
5416 NEW_ARR_A(ir_node *, in, n_keepalives);
5418 for (i = j = 0; i < n_keepalives; ++i) {
5419 ir_node *ka = get_End_keepalive(n, i);
5421 if (! is_Block_dead(ka)) {
5425 } else if (is_irn_pinned_in_irg(ka) && is_Block_dead(get_nodes_block(ka))) {
5427 } else if (is_Bad(ka)) {
5428 /* no need to keep Bad */
5433 if (j != n_keepalives)
5434 set_End_keepalives(n, j, in);
5436 } /* transform_node_End */
5438 /** returns 1 if a == -b */
5439 static int is_negated_value(ir_node *a, ir_node *b) {
5440 if (is_Minus(a) && get_Minus_op(a) == b)
5442 if (is_Minus(b) && get_Minus_op(b) == a)
5444 if (is_Sub(a) && is_Sub(b)) {
5445 ir_node *a_left = get_Sub_left(a);
5446 ir_node *a_right = get_Sub_right(a);
5447 ir_node *b_left = get_Sub_left(b);
5448 ir_node *b_right = get_Sub_right(b);
5450 if (a_left == b_right && a_right == b_left)
5458 * Optimize a Mux into some simpler cases.
5460 static ir_node *transform_node_Mux(ir_node *n) {
5461 ir_node *oldn = n, *sel = get_Mux_sel(n);
5462 ir_mode *mode = get_irn_mode(n);
5463 ir_node *t = get_Mux_true(n);
5464 ir_node *f = get_Mux_false(n);
5465 ir_graph *irg = current_ir_graph;
5467 /* first normalization step: move a possible zero to the false case */
5469 ir_node *cmp = get_Proj_pred(sel);
5472 if (is_Const(t) && is_Const_null(t)) {
5475 /* Mux(x, 0, y) => Mux(x, y, 0) */
5476 pn_Cmp pnc = get_Proj_proj(sel);
5477 sel = new_r_Proj(irg, get_nodes_block(cmp), cmp, mode_b,
5478 get_negated_pnc(pnc, get_irn_mode(get_Cmp_left(cmp))));
5479 n = new_rd_Mux(get_irn_dbg_info(n), irg, get_nodes_block(n), sel, t, f, mode);
5487 /* note: after normalization, false can only happen on default */
5488 if (mode == mode_b) {
5489 dbg_info *dbg = get_irn_dbg_info(n);
5490 ir_node *block = get_nodes_block(n);
5491 ir_graph *irg = current_ir_graph;
5494 tarval *tv_t = get_Const_tarval(t);
5495 if (tv_t == tarval_b_true) {
5497 /* Muxb(sel, true, false) = sel */
5498 assert(get_Const_tarval(f) == tarval_b_false);
5499 DBG_OPT_ALGSIM0(oldn, sel, FS_OPT_MUX_BOOL);
5502 /* Muxb(sel, true, x) = Or(sel, x) */
5503 n = new_rd_Or(dbg, irg, block, sel, f, mode_b);
5504 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_OR_BOOL);
5508 } else if (is_Const(f)) {
5509 tarval *tv_f = get_Const_tarval(f);
5510 if (tv_f == tarval_b_true) {
5511 /* Muxb(sel, x, true) = Or(Not(sel), x) */
5512 ir_node* not_sel = new_rd_Not(dbg, irg, block, sel, mode_b);
5513 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_ORNOT_BOOL);
5514 n = new_rd_Or(dbg, irg, block, not_sel, t, mode_b);
5517 /* Muxb(sel, x, false) = And(sel, x) */
5518 assert(tv_f == tarval_b_false);
5519 n = new_rd_And(dbg, irg, block, sel, t, mode_b);
5520 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_AND_BOOL);
5526 /* more normalization: try to normalize Mux(x, C1, C2) into Mux(x, +1/-1, 0) op C2 */
5527 if (is_Const(t) && is_Const(f) && mode_is_int(mode)) {
5528 tarval *a = get_Const_tarval(t);
5529 tarval *b = get_Const_tarval(f);
5530 tarval *null = get_tarval_null(mode);
5533 if (tarval_cmp(a, b) & pn_Cmp_Gt) {
5534 diff = tarval_sub(a, b, NULL);
5537 diff = tarval_sub(b, a, NULL);
5541 if (diff == get_tarval_one(mode) && min != null) {
5542 dbg_info *dbg = get_irn_dbg_info(n);
5543 ir_node *block = get_nodes_block(n);
5544 ir_graph *irg = current_ir_graph;
5545 ir_node *t = new_Const(mode, tarval_sub(a, min, NULL));
5546 ir_node *f = new_Const(mode, tarval_sub(b, min, NULL));
5547 n = new_rd_Mux(dbg, irg, block, sel, f, t, mode);
5548 n = new_rd_Add(dbg, irg, block, n, new_Const(mode, min), mode);
5554 ir_node *cmp = get_Proj_pred(sel);
5555 long pn = get_Proj_proj(sel);
5558 * Note: normalization puts the constant on the right side,
5559 * so we check only one case.
5561 * Note further that these optimization work even for floating point
5562 * with NaN's because -NaN == NaN.
5563 * However, if +0 and -0 is handled differently, we cannot use the Abs/-Abs
5567 ir_node *cmp_r = get_Cmp_right(cmp);
5568 if (is_Const(cmp_r) && is_Const_null(cmp_r)) {
5569 ir_node *block = get_nodes_block(n);
5570 ir_node *cmp_l = get_Cmp_left(cmp);
5572 if (!mode_honor_signed_zeros(mode) && is_negated_value(f, t)) {
5575 if ( (cmp_l == t && (pn == pn_Cmp_Ge || pn == pn_Cmp_Gt))
5576 || (cmp_l == f && (pn == pn_Cmp_Le || pn == pn_Cmp_Lt)))
5578 /* Mux(a >/>= 0, a, -a) = Mux(a </<= 0, -a, a) ==> Abs(a) */
5579 n = new_rd_Abs(get_irn_dbg_info(n), current_ir_graph, block,
5581 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_ABS);
5583 } else if ((cmp_l == t && (pn == pn_Cmp_Le || pn == pn_Cmp_Lt))
5584 || (cmp_l == f && (pn == pn_Cmp_Ge || pn == pn_Cmp_Gt)))
5586 /* Mux(a </<= 0, a, -a) = Mux(a >/>= 0, -a, a) ==> -Abs(a) */
5587 n = new_rd_Abs(get_irn_dbg_info(n), current_ir_graph, block,
5589 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph,
5591 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_ABS);
5596 if (mode_is_int(mode)) {
5598 if ((pn == pn_Cmp_Lg || pn == pn_Cmp_Eq) && is_And(cmp_l)) {
5599 /* Mux((a & b) != 0, c, 0) */
5600 ir_node *and_r = get_And_right(cmp_l);
5603 if (and_r == t && f == cmp_r) {
5604 if (is_Const(t) && tarval_is_single_bit(get_Const_tarval(t))) {
5605 if (pn == pn_Cmp_Lg) {
5606 /* Mux((a & 2^C) != 0, 2^C, 0) */
5608 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_BITOP);
5610 /* Mux((a & 2^C) == 0, 2^C, 0) */
5611 n = new_rd_Eor(get_irn_dbg_info(n), current_ir_graph,
5612 block, cmp_l, t, mode);
5613 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_BITOP);
5618 if (is_Shl(and_r)) {
5619 ir_node *shl_l = get_Shl_left(and_r);
5620 if (is_Const(shl_l) && is_Const_one(shl_l)) {
5621 if (and_r == t && f == cmp_r) {
5622 if (pn == pn_Cmp_Lg) {
5623 /* (a & (1 << n)) != 0, (1 << n), 0) */
5625 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_BITOP);
5627 /* (a & (1 << n)) == 0, (1 << n), 0) */
5628 n = new_rd_Eor(get_irn_dbg_info(n), current_ir_graph,
5629 block, cmp_l, t, mode);
5630 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_BITOP);
5636 and_l = get_And_left(cmp_l);
5637 if (is_Shl(and_l)) {
5638 ir_node *shl_l = get_Shl_left(and_l);
5639 if (is_Const(shl_l) && is_Const_one(shl_l)) {
5640 if (and_l == t && f == cmp_r) {
5641 if (pn == pn_Cmp_Lg) {
5642 /* ((1 << n) & a) != 0, (1 << n), 0) */
5644 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_BITOP);
5646 /* ((1 << n) & a) == 0, (1 << n), 0) */
5647 n = new_rd_Eor(get_irn_dbg_info(n), current_ir_graph,
5648 block, cmp_l, t, mode);
5649 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_BITOP);
5660 return arch_transform_node_Mux(n);
5661 } /* transform_node_Mux */
5664 * optimize Sync nodes that have other syncs as input we simply add the inputs
5665 * of the other sync to our own inputs
5667 static ir_node *transform_node_Sync(ir_node *n) {
5668 int arity = get_Sync_n_preds(n);
5671 for (i = 0; i < arity;) {
5672 ir_node *pred = get_Sync_pred(n, i);
5676 if (!is_Sync(pred)) {
5684 pred_arity = get_Sync_n_preds(pred);
5685 for (j = 0; j < pred_arity; ++j) {
5686 ir_node *pred_pred = get_Sync_pred(pred, j);
5691 add_irn_n(n, pred_pred);
5695 if (get_Sync_pred(n, k) == pred_pred) break;
5700 /* rehash the sync node */
5701 add_identities(current_ir_graph->value_table, n);
5707 * Tries several [inplace] [optimizing] transformations and returns an
5708 * equivalent node. The difference to equivalent_node() is that these
5709 * transformations _do_ generate new nodes, and thus the old node must
5710 * not be freed even if the equivalent node isn't the old one.
5712 static ir_node *transform_node(ir_node *n) {
5716 * Transform_node is the only "optimizing transformation" that might
5717 * return a node with a different opcode. We iterate HERE until fixpoint
5718 * to get the final result.
5722 if (n->op->ops.transform_node)
5723 n = n->op->ops.transform_node(n);
5724 } while (oldn != n);
5727 } /* transform_node */
5730 * Sets the default transform node operation for an ir_op_ops.
5732 * @param code the opcode for the default operation
5733 * @param ops the operations initialized
5738 static ir_op_ops *firm_set_default_transform_node(ir_opcode code, ir_op_ops *ops)
5742 ops->transform_node = transform_node_##a; \
5744 #define CASE_PROJ(a) \
5746 ops->transform_node_Proj = transform_node_Proj_##a; \
5748 #define CASE_PROJ_EX(a) \
5750 ops->transform_node = transform_node_##a; \
5751 ops->transform_node_Proj = transform_node_Proj_##a; \
5760 CASE_PROJ_EX(DivMod);
5794 } /* firm_set_default_transform_node */
5797 /* **************** Common Subexpression Elimination **************** */
5799 /** The size of the hash table used, should estimate the number of nodes
5801 #define N_IR_NODES 512
5803 /** Compares the attributes of two Const nodes. */
5804 static int node_cmp_attr_Const(ir_node *a, ir_node *b) {
5805 return (get_Const_tarval(a) != get_Const_tarval(b))
5806 || (get_Const_type(a) != get_Const_type(b));
5807 } /* node_cmp_attr_Const */
5809 /** Compares the attributes of two Proj nodes. */
5810 static int node_cmp_attr_Proj(ir_node *a, ir_node *b) {
5811 return get_irn_proj_attr(a) != get_irn_proj_attr(b);
5812 } /* node_cmp_attr_Proj */
5814 /** Compares the attributes of two Filter nodes. */
5815 static int node_cmp_attr_Filter(ir_node *a, ir_node *b) {
5816 return get_Filter_proj(a) != get_Filter_proj(b);
5817 } /* node_cmp_attr_Filter */
5819 /** Compares the attributes of two Alloc nodes. */
5820 static int node_cmp_attr_Alloc(ir_node *a, ir_node *b) {
5821 const alloc_attr *pa = get_irn_alloc_attr(a);
5822 const alloc_attr *pb = get_irn_alloc_attr(b);
5823 return (pa->where != pb->where) || (pa->type != pb->type);
5824 } /* node_cmp_attr_Alloc */
5826 /** Compares the attributes of two Free nodes. */
5827 static int node_cmp_attr_Free(ir_node *a, ir_node *b) {
5828 const free_attr *pa = get_irn_free_attr(a);
5829 const free_attr *pb = get_irn_free_attr(b);
5830 return (pa->where != pb->where) || (pa->type != pb->type);
5831 } /* node_cmp_attr_Free */
5833 /** Compares the attributes of two SymConst nodes. */
5834 static int node_cmp_attr_SymConst(ir_node *a, ir_node *b) {
5835 const symconst_attr *pa = get_irn_symconst_attr(a);
5836 const symconst_attr *pb = get_irn_symconst_attr(b);
5837 return (pa->kind != pb->kind)
5838 || (pa->sym.type_p != pb->sym.type_p)
5839 || (pa->tp != pb->tp);
5840 } /* node_cmp_attr_SymConst */
5842 /** Compares the attributes of two Call nodes. */
5843 static int node_cmp_attr_Call(ir_node *a, ir_node *b) {
5844 return get_irn_call_attr(a) != get_irn_call_attr(b);
5845 } /* node_cmp_attr_Call */
5847 /** Compares the attributes of two Sel nodes. */
5848 static int node_cmp_attr_Sel(ir_node *a, ir_node *b) {
5849 const ir_entity *a_ent = get_Sel_entity(a);
5850 const ir_entity *b_ent = get_Sel_entity(b);
5853 (a_ent->kind != b_ent->kind) ||
5854 (a_ent->name != b_ent->name) ||
5855 (a_ent->owner != b_ent->owner) ||
5856 (a_ent->ld_name != b_ent->ld_name) ||
5857 (a_ent->type != b_ent->type);
5859 /* Matze: inlining of functions can produce 2 entities with same type,
5861 return a_ent != b_ent;
5862 } /* node_cmp_attr_Sel */
5864 /** Compares the attributes of two Phi nodes. */
5865 static int node_cmp_attr_Phi(ir_node *a, ir_node *b) {
5866 /* we can only enter this function if both nodes have the same number of inputs,
5867 hence it is enough to check if one of them is a Phi0 */
5869 /* check the Phi0 pos attribute */
5870 return get_irn_phi_attr(a)->u.pos != get_irn_phi_attr(b)->u.pos;
5873 } /* node_cmp_attr_Phi */
5875 /** Compares the attributes of two Conv nodes. */
5876 static int node_cmp_attr_Conv(ir_node *a, ir_node *b) {
5877 return get_Conv_strict(a) != get_Conv_strict(b);
5878 } /* node_cmp_attr_Conv */
5880 /** Compares the attributes of two Cast nodes. */
5881 static int node_cmp_attr_Cast(ir_node *a, ir_node *b) {
5882 return get_Cast_type(a) != get_Cast_type(b);
5883 } /* node_cmp_attr_Cast */
5885 /** Compares the attributes of two Load nodes. */
5886 static int node_cmp_attr_Load(ir_node *a, ir_node *b) {
5887 if (get_Load_volatility(a) == volatility_is_volatile ||
5888 get_Load_volatility(b) == volatility_is_volatile)
5889 /* NEVER do CSE on volatile Loads */
5891 /* do not CSE Loads with different alignment. Be conservative. */
5892 if (get_Load_align(a) != get_Load_align(b))
5895 return get_Load_mode(a) != get_Load_mode(b);
5896 } /* node_cmp_attr_Load */
5898 /** Compares the attributes of two Store nodes. */
5899 static int node_cmp_attr_Store(ir_node *a, ir_node *b) {
5900 /* do not CSE Stores with different alignment. Be conservative. */
5901 if (get_Store_align(a) != get_Store_align(b))
5904 /* NEVER do CSE on volatile Stores */
5905 return (get_Store_volatility(a) == volatility_is_volatile ||
5906 get_Store_volatility(b) == volatility_is_volatile);
5907 } /* node_cmp_attr_Store */
5909 /** Compares two exception attributes */
5910 static int node_cmp_exception(ir_node *a, ir_node *b) {
5911 const except_attr *ea = get_irn_except_attr(a);
5912 const except_attr *eb = get_irn_except_attr(b);
5914 return ea->pin_state != eb->pin_state;
5917 #define node_cmp_attr_Bound node_cmp_exception
5919 /** Compares the attributes of two Div nodes. */
5920 static int node_cmp_attr_Div(ir_node *a, ir_node *b) {
5921 const divmod_attr *ma = get_irn_divmod_attr(a);
5922 const divmod_attr *mb = get_irn_divmod_attr(b);
5923 return ma->exc.pin_state != mb->exc.pin_state ||
5924 ma->res_mode != mb->res_mode ||
5925 ma->no_remainder != mb->no_remainder;
5926 } /* node_cmp_attr_Div */
5928 /** Compares the attributes of two DivMod nodes. */
5929 static int node_cmp_attr_DivMod(ir_node *a, ir_node *b) {
5930 const divmod_attr *ma = get_irn_divmod_attr(a);
5931 const divmod_attr *mb = get_irn_divmod_attr(b);
5932 return ma->exc.pin_state != mb->exc.pin_state ||
5933 ma->res_mode != mb->res_mode;
5934 } /* node_cmp_attr_DivMod */
5936 /** Compares the attributes of two Mod nodes. */
5937 static int node_cmp_attr_Mod(ir_node *a, ir_node *b) {
5938 const divmod_attr *ma = get_irn_divmod_attr(a);
5939 const divmod_attr *mb = get_irn_divmod_attr(b);
5940 return ma->exc.pin_state != mb->exc.pin_state ||
5941 ma->res_mode != mb->res_mode;
5942 } /* node_cmp_attr_Mod */
5944 /** Compares the attributes of two Quot nodes. */
5945 static int node_cmp_attr_Quot(ir_node *a, ir_node *b) {
5946 const divmod_attr *ma = get_irn_divmod_attr(a);
5947 const divmod_attr *mb = get_irn_divmod_attr(b);
5948 return ma->exc.pin_state != mb->exc.pin_state ||
5949 ma->res_mode != mb->res_mode;
5950 } /* node_cmp_attr_Quot */
5952 /** Compares the attributes of two Confirm nodes. */
5953 static int node_cmp_attr_Confirm(ir_node *a, ir_node *b) {
5954 return (get_Confirm_cmp(a) != get_Confirm_cmp(b));
5955 } /* node_cmp_attr_Confirm */
5957 /** Compares the attributes of two ASM nodes. */
5958 static int node_cmp_attr_ASM(ir_node *a, ir_node *b) {
5960 const ir_asm_constraint *ca;
5961 const ir_asm_constraint *cb;
5964 if (get_ASM_text(a) != get_ASM_text(b))
5967 /* Should we really check the constraints here? Should be better, but is strange. */
5968 n = get_ASM_n_input_constraints(a);
5969 if (n != get_ASM_n_input_constraints(b))
5972 ca = get_ASM_input_constraints(a);
5973 cb = get_ASM_input_constraints(b);
5974 for (i = 0; i < n; ++i) {
5975 if (ca[i].pos != cb[i].pos || ca[i].constraint != cb[i].constraint)
5979 n = get_ASM_n_output_constraints(a);
5980 if (n != get_ASM_n_output_constraints(b))
5983 ca = get_ASM_output_constraints(a);
5984 cb = get_ASM_output_constraints(b);
5985 for (i = 0; i < n; ++i) {
5986 if (ca[i].pos != cb[i].pos || ca[i].constraint != cb[i].constraint)
5990 n = get_ASM_n_clobbers(a);
5991 if (n != get_ASM_n_clobbers(b))
5994 cla = get_ASM_clobbers(a);
5995 clb = get_ASM_clobbers(b);
5996 for (i = 0; i < n; ++i) {
5997 if (cla[i] != clb[i])
6001 } /* node_cmp_attr_ASM */
6004 * Set the default node attribute compare operation for an ir_op_ops.
6006 * @param code the opcode for the default operation
6007 * @param ops the operations initialized
6012 static ir_op_ops *firm_set_default_node_cmp_attr(ir_opcode code, ir_op_ops *ops)
6016 ops->node_cmp_attr = node_cmp_attr_##a; \
6047 } /* firm_set_default_node_cmp_attr */
6050 * Compare function for two nodes in the value table. Gets two
6051 * nodes as parameters. Returns 0 if the nodes are a Common Sub Expression.
6053 int identities_cmp(const void *elt, const void *key) {
6054 ir_node *a = (ir_node *)elt;
6055 ir_node *b = (ir_node *)key;
6058 if (a == b) return 0;
6060 if ((get_irn_op(a) != get_irn_op(b)) ||
6061 (get_irn_mode(a) != get_irn_mode(b))) return 1;
6063 /* compare if a's in and b's in are of equal length */
6064 irn_arity_a = get_irn_intra_arity(a);
6065 if (irn_arity_a != get_irn_intra_arity(b))
6068 if (get_irn_pinned(a) == op_pin_state_pinned) {
6069 /* for pinned nodes, the block inputs must be equal */
6070 if (get_irn_intra_n(a, -1) != get_irn_intra_n(b, -1))
6072 } else if (! get_opt_global_cse()) {
6073 /* for block-local CSE both nodes must be in the same MacroBlock */
6074 if (get_irn_MacroBlock(a) != get_irn_MacroBlock(b))
6078 /* compare a->in[0..ins] with b->in[0..ins] */
6079 for (i = 0; i < irn_arity_a; i++)
6080 if (get_irn_intra_n(a, i) != get_irn_intra_n(b, i))
6084 * here, we already now that the nodes are identical except their
6087 if (a->op->ops.node_cmp_attr)
6088 return a->op->ops.node_cmp_attr(a, b);
6091 } /* identities_cmp */
6094 * Calculate a hash value of a node.
6096 * @param node The IR-node
6098 unsigned ir_node_hash(const ir_node *node) {
6099 return node->op->ops.hash(node);
6100 } /* ir_node_hash */
6103 pset *new_identities(void) {
6104 return new_pset(identities_cmp, N_IR_NODES);
6105 } /* new_identities */
6107 void del_identities(pset *value_table) {
6108 del_pset(value_table);
6109 } /* del_identities */
6111 /* Normalize a node by putting constants (and operands with larger
6112 * node index) on the right (operator side). */
6113 void ir_normalize_node(ir_node *n) {
6114 if (is_op_commutative(get_irn_op(n))) {
6115 ir_node *l = get_binop_left(n);
6116 ir_node *r = get_binop_right(n);
6118 /* For commutative operators perform a OP b == b OP a but keep
6119 * constants on the RIGHT side. This helps greatly in some
6120 * optimizations. Moreover we use the idx number to make the form
6122 if (!operands_are_normalized(l, r)) {
6123 set_binop_left(n, r);
6124 set_binop_right(n, l);
6128 } /* ir_normalize_node */
6131 * Update the nodes after a match in the value table. If both nodes have
6132 * the same MacroBlock but different Blocks, we must ensure that the node
6133 * with the dominating Block (the node that is near to the MacroBlock header
6134 * is stored in the table.
6135 * Because a MacroBlock has only one "non-exception" flow, we don't need
6136 * dominance info here: We known, that one block must dominate the other and
6137 * following the only block input will allow to find it.
6139 static void update_known_irn(ir_node *known_irn, const ir_node *new_ir_node) {
6140 ir_node *known_blk, *new_block, *block, *mbh;
6142 if (get_opt_global_cse()) {
6143 /* Block inputs are meaning less */
6146 known_blk = get_irn_n(known_irn, -1);
6147 new_block = get_irn_n(new_ir_node, -1);
6148 if (known_blk == new_block) {
6149 /* already in the same block */
6153 * We expect the typical case when we built the graph. In that case, the
6154 * known_irn is already the upper one, so checking this should be faster.
6157 mbh = get_Block_MacroBlock(new_block);
6159 if (block == known_blk) {
6160 /* ok, we have found it: known_block dominates new_block as expected */
6165 * We have reached the MacroBlock header NOT founding
6166 * the known_block. new_block must dominate known_block.
6169 set_irn_n(known_irn, -1, new_block);
6172 assert(get_Block_n_cfgpreds(block) == 1);
6173 block = get_Block_cfgpred_block(block, 0);
6175 } /* update_value_table */
6178 * Return the canonical node computing the same value as n.
6179 * Looks up the node in a hash table, enters it in the table
6180 * if it isn't there yet.
6182 * @param value_table the HashSet containing all nodes in the
6184 * @param n the node to look up
6186 * @return a node that computes the same value as n or n if no such
6187 * node could be found
6189 ir_node *identify_remember(pset *value_table, ir_node *n) {
6192 if (!value_table) return n;
6194 ir_normalize_node(n);
6195 /* lookup or insert in hash table with given hash key. */
6196 o = pset_insert(value_table, n, ir_node_hash(n));
6199 update_known_irn(o, n);
6203 } /* identify_remember */
6206 * During construction we set the op_pin_state_pinned flag in the graph right when the
6207 * optimization is performed. The flag turning on procedure global cse could
6208 * be changed between two allocations. This way we are safe.
6210 * @param value_table The value table
6211 * @param n The node to lookup
6213 static inline ir_node *identify_cons(pset *value_table, ir_node *n) {
6216 n = identify_remember(value_table, n);
6217 if (n != old && get_irn_MacroBlock(old) != get_irn_MacroBlock(n))
6218 set_irg_pinned(current_ir_graph, op_pin_state_floats);
6220 } /* identify_cons */
6222 /* Add a node to the identities value table. */
6223 void add_identities(pset *value_table, ir_node *node) {
6224 if (get_opt_cse() && is_no_Block(node))
6225 identify_remember(value_table, node);
6226 } /* add_identities */
6228 /* Visit each node in the value table of a graph. */
6229 void visit_all_identities(ir_graph *irg, irg_walk_func visit, void *env) {
6231 ir_graph *rem = current_ir_graph;
6233 current_ir_graph = irg;
6234 foreach_pset(irg->value_table, node)
6236 current_ir_graph = rem;
6237 } /* visit_all_identities */
6240 * Garbage in, garbage out. If a node has a dead input, i.e., the
6241 * Bad node is input to the node, return the Bad node.
6243 static ir_node *gigo(ir_node *node) {
6245 ir_op *op = get_irn_op(node);
6247 /* remove garbage blocks by looking at control flow that leaves the block
6248 and replacing the control flow by Bad. */
6249 if (get_irn_mode(node) == mode_X) {
6250 ir_node *block = get_nodes_block(skip_Proj(node));
6252 /* Don't optimize nodes in immature blocks. */
6253 if (!get_Block_matured(block))
6255 /* Don't optimize End, may have Bads. */
6256 if (op == op_End) return node;
6258 if (is_Block(block)) {
6259 if (is_Block_dead(block)) {
6260 /* control flow from dead block is dead */
6264 for (i = get_irn_arity(block) - 1; i >= 0; --i) {
6265 if (!is_Bad(get_irn_n(block, i)))
6269 ir_graph *irg = get_irn_irg(block);
6270 /* the start block is never dead */
6271 if (block != get_irg_start_block(irg)
6272 && block != get_irg_end_block(irg)) {
6274 * Do NOT kill control flow without setting
6275 * the block to dead of bad things can happen:
6276 * We get a Block that is not reachable be irg_block_walk()
6277 * but can be found by irg_walk()!
6279 set_Block_dead(block);
6286 /* Blocks, Phis and Tuples may have dead inputs, e.g., if one of the
6287 blocks predecessors is dead. */
6288 if (op != op_Block && op != op_Phi && op != op_Tuple) {
6289 irn_arity = get_irn_arity(node);
6292 * Beware: we can only read the block of a non-floating node.
6294 if (is_irn_pinned_in_irg(node) &&
6295 is_Block_dead(get_nodes_block(skip_Proj(node))))
6298 for (i = 0; i < irn_arity; i++) {
6299 ir_node *pred = get_irn_n(node, i);
6304 /* Propagating Unknowns here seems to be a bad idea, because
6305 sometimes we need a node as a input and did not want that
6307 However, it might be useful to move this into a later phase
6308 (if you think that optimizing such code is useful). */
6309 if (is_Unknown(pred) && mode_is_data(get_irn_mode(node)))
6310 return new_Unknown(get_irn_mode(node));
6315 /* With this code we violate the agreement that local_optimize
6316 only leaves Bads in Block, Phi and Tuple nodes. */
6317 /* If Block has only Bads as predecessors it's garbage. */
6318 /* If Phi has only Bads as predecessors it's garbage. */
6319 if ((op == op_Block && get_Block_matured(node)) || op == op_Phi) {
6320 irn_arity = get_irn_arity(node);
6321 for (i = 0; i < irn_arity; i++) {
6322 if (!is_Bad(get_irn_n(node, i))) break;
6324 if (i == irn_arity) node = new_Bad();
6331 * These optimizations deallocate nodes from the obstack.
6332 * It can only be called if it is guaranteed that no other nodes
6333 * reference this one, i.e., right after construction of a node.
6335 * @param n The node to optimize
6337 * current_ir_graph must be set to the graph of the node!
6339 ir_node *optimize_node(ir_node *n) {
6342 ir_opcode iro = get_irn_opcode(n);
6344 /* Always optimize Phi nodes: part of the construction. */
6345 if ((!get_opt_optimize()) && (iro != iro_Phi)) return n;
6347 /* constant expression evaluation / constant folding */
6348 if (get_opt_constant_folding()) {
6349 /* neither constants nor Tuple values can be evaluated */
6350 if (iro != iro_Const && (get_irn_mode(n) != mode_T)) {
6351 unsigned fp_model = get_irg_fp_model(current_ir_graph);
6352 int old_fp_mode = tarval_enable_fp_ops((fp_model & fp_strict_algebraic) == 0);
6353 /* try to evaluate */
6354 tv = computed_value(n);
6355 if (tv != tarval_bad) {
6357 ir_type *old_tp = get_irn_type(n);
6358 int i, arity = get_irn_arity(n);
6362 * Try to recover the type of the new expression.
6364 for (i = 0; i < arity && !old_tp; ++i)
6365 old_tp = get_irn_type(get_irn_n(n, i));
6368 * we MUST copy the node here temporary, because it's still needed
6369 * for DBG_OPT_CSTEVAL
6371 node_size = offsetof(ir_node, attr) + n->op->attr_size;
6372 oldn = alloca(node_size);
6374 memcpy(oldn, n, node_size);
6375 CLONE_ARR_A(ir_node *, oldn->in, n->in);
6377 /* ARG, copy the in array, we need it for statistics */
6378 memcpy(oldn->in, n->in, ARR_LEN(n->in) * sizeof(n->in[0]));
6380 /* note the inplace edges module */
6381 edges_node_deleted(n, current_ir_graph);
6383 /* evaluation was successful -- replace the node. */
6384 irg_kill_node(current_ir_graph, n);
6385 nw = new_Const(get_tarval_mode(tv), tv);
6387 if (old_tp && get_type_mode(old_tp) == get_tarval_mode(tv))
6388 set_Const_type(nw, old_tp);
6389 DBG_OPT_CSTEVAL(oldn, nw);
6390 tarval_enable_fp_ops(old_fp_mode);
6393 tarval_enable_fp_ops(old_fp_mode);
6397 /* remove unnecessary nodes */
6398 if (get_opt_algebraic_simplification() ||
6399 (iro == iro_Phi) || /* always optimize these nodes. */
6401 (iro == iro_Proj) ||
6402 (iro == iro_Block) ) /* Flags tested local. */
6403 n = equivalent_node(n);
6405 /* Common Subexpression Elimination.
6407 * Checks whether n is already available.
6408 * The block input is used to distinguish different subexpressions. Right
6409 * now all nodes are op_pin_state_pinned to blocks, i.e., the CSE only finds common
6410 * subexpressions within a block.
6413 n = identify_cons(current_ir_graph->value_table, n);
6416 edges_node_deleted(oldn, current_ir_graph);
6418 /* We found an existing, better node, so we can deallocate the old node. */
6419 irg_kill_node(current_ir_graph, oldn);
6423 /* Some more constant expression evaluation that does not allow to
6425 iro = get_irn_opcode(n);
6426 if (get_opt_algebraic_simplification() ||
6427 (iro == iro_Cond) ||
6428 (iro == iro_Proj)) /* Flags tested local. */
6429 n = transform_node(n);
6431 /* Remove nodes with dead (Bad) input.
6432 Run always for transformation induced Bads. */
6435 /* Now we have a legal, useful node. Enter it in hash table for CSE */
6436 if (get_opt_cse() && (get_irn_opcode(n) != iro_Block)) {
6438 n = identify_remember(current_ir_graph->value_table, o);
6444 } /* optimize_node */
6448 * These optimizations never deallocate nodes (in place). This can cause dead
6449 * nodes lying on the obstack. Remove these by a dead node elimination,
6450 * i.e., a copying garbage collection.
6452 ir_node *optimize_in_place_2(ir_node *n) {
6455 ir_opcode iro = get_irn_opcode(n);
6457 if (!get_opt_optimize() && !is_Phi(n)) return n;
6459 /* constant expression evaluation / constant folding */
6460 if (get_opt_constant_folding()) {
6461 /* neither constants nor Tuple values can be evaluated */
6462 if (iro != iro_Const && get_irn_mode(n) != mode_T) {
6463 unsigned fp_model = get_irg_fp_model(current_ir_graph);
6464 int old_fp_mode = tarval_enable_fp_ops((fp_model & fp_strict_algebraic) == 0);
6465 /* try to evaluate */
6466 tv = computed_value(n);
6467 if (tv != tarval_bad) {
6468 /* evaluation was successful -- replace the node. */
6469 ir_type *old_tp = get_irn_type(n);
6470 int i, arity = get_irn_arity(n);
6473 * Try to recover the type of the new expression.
6475 for (i = 0; i < arity && !old_tp; ++i)
6476 old_tp = get_irn_type(get_irn_n(n, i));
6478 n = new_Const(get_tarval_mode(tv), tv);
6480 if (old_tp && get_type_mode(old_tp) == get_tarval_mode(tv))
6481 set_Const_type(n, old_tp);
6483 DBG_OPT_CSTEVAL(oldn, n);
6484 tarval_enable_fp_ops(old_fp_mode);
6487 tarval_enable_fp_ops(old_fp_mode);
6491 /* remove unnecessary nodes */
6492 if (get_opt_constant_folding() ||
6493 (iro == iro_Phi) || /* always optimize these nodes. */
6494 (iro == iro_Id) || /* ... */
6495 (iro == iro_Proj) || /* ... */
6496 (iro == iro_Block) ) /* Flags tested local. */
6497 n = equivalent_node(n);
6499 /** common subexpression elimination **/
6500 /* Checks whether n is already available. */
6501 /* The block input is used to distinguish different subexpressions. Right
6502 now all nodes are op_pin_state_pinned to blocks, i.e., the cse only finds common
6503 subexpressions within a block. */
6504 if (get_opt_cse()) {
6506 n = identify_remember(current_ir_graph->value_table, o);
6511 /* Some more constant expression evaluation. */
6512 iro = get_irn_opcode(n);
6513 if (get_opt_constant_folding() ||
6514 (iro == iro_Cond) ||
6515 (iro == iro_Proj)) /* Flags tested local. */
6516 n = transform_node(n);
6518 /* Remove nodes with dead (Bad) input.
6519 Run always for transformation induced Bads. */
6522 /* Now we can verify the node, as it has no dead inputs any more. */
6525 /* Now we have a legal, useful node. Enter it in hash table for cse.
6526 Blocks should be unique anyways. (Except the successor of start:
6527 is cse with the start block!) */
6528 if (get_opt_cse() && (get_irn_opcode(n) != iro_Block)) {
6530 n = identify_remember(current_ir_graph->value_table, o);
6536 } /* optimize_in_place_2 */
6539 * Wrapper for external use, set proper status bits after optimization.
6541 ir_node *optimize_in_place(ir_node *n) {
6542 /* Handle graph state */
6543 assert(get_irg_phase_state(current_ir_graph) != phase_building);
6545 if (get_opt_global_cse())
6546 set_irg_pinned(current_ir_graph, op_pin_state_floats);
6547 if (get_irg_outs_state(current_ir_graph) == outs_consistent)
6548 set_irg_outs_inconsistent(current_ir_graph);
6550 /* FIXME: Maybe we could also test whether optimizing the node can
6551 change the control graph. */
6552 set_irg_doms_inconsistent(current_ir_graph);
6553 return optimize_in_place_2(n);
6554 } /* optimize_in_place */
6557 * Calculate a hash value of a Const node.
6559 static unsigned hash_Const(const ir_node *node) {
6562 /* special value for const, as they only differ in their tarval. */
6563 h = HASH_PTR(node->attr.con.tv);
6564 h = 9*h + HASH_PTR(get_irn_mode(node));
6570 * Calculate a hash value of a SymConst node.
6572 static unsigned hash_SymConst(const ir_node *node) {
6575 /* special value for const, as they only differ in their symbol. */
6576 h = HASH_PTR(node->attr.symc.sym.type_p);
6577 h = 9*h + HASH_PTR(get_irn_mode(node));
6580 } /* hash_SymConst */
6583 * Set the default hash operation in an ir_op_ops.
6585 * @param code the opcode for the default operation
6586 * @param ops the operations initialized
6591 static ir_op_ops *firm_set_default_hash(ir_opcode code, ir_op_ops *ops)
6595 ops->hash = hash_##a; \
6598 /* hash function already set */
6599 if (ops->hash != NULL)
6606 /* use input/mode default hash if no function was given */
6607 ops->hash = firm_default_hash;
6615 * Sets the default operation for an ir_ops.
6617 ir_op_ops *firm_set_default_operations(ir_opcode code, ir_op_ops *ops) {
6618 ops = firm_set_default_hash(code, ops);
6619 ops = firm_set_default_computed_value(code, ops);
6620 ops = firm_set_default_equivalent_node(code, ops);
6621 ops = firm_set_default_transform_node(code, ops);
6622 ops = firm_set_default_node_cmp_attr(code, ops);
6623 ops = firm_set_default_get_type(code, ops);
6624 ops = firm_set_default_get_type_attr(code, ops);
6625 ops = firm_set_default_get_entity_attr(code, ops);
6628 } /* firm_set_default_operations */