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)) {
1217 /* special case: the predecessor might be a also a Conv */
1219 if (! get_Conv_strict(a)) {
1220 /* first one is not strict, kick it */
1222 a_mode = get_irn_mode(a);
1226 /* else both are strict conv, second is superfluous */
1229 ir_node *pred = get_Proj_pred(a);
1230 if (is_Load(pred)) {
1231 /* loads always return with the exact precision of n_mode */
1232 assert(get_Load_mode(pred) == n_mode);
1236 /* leave strict floating point Conv's */
1241 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CONV);
1242 } else if (is_Conv(a)) { /* Conv(Conv(b)) */
1243 ir_node *b = get_Conv_op(a);
1244 ir_mode *b_mode = get_irn_mode(b);
1246 if (get_Conv_strict(n) && get_Conv_strict(a)) {
1247 /* both are strict conv */
1248 if (smaller_mode(a_mode, n_mode)) {
1249 /* both are strict, but the first is smaller, so
1250 the second cannot remove more precision, remove the
1252 set_Conv_strict(n, 0);
1255 if (n_mode == b_mode) {
1256 if (! get_Conv_strict(n) && ! get_Conv_strict(a)) {
1257 if (n_mode == mode_b) {
1258 n = b; /* Convb(Conv*(xxxb(...))) == xxxb(...) */
1259 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
1261 } else if (get_mode_arithmetic(n_mode) == get_mode_arithmetic(a_mode)) {
1262 if (values_in_mode(b_mode, a_mode)) {
1263 n = b; /* ConvS(ConvL(xxxS(...))) == xxxS(...) */
1264 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
1269 if (mode_is_int(n_mode) && get_mode_arithmetic(a_mode) == irma_ieee754) {
1270 /* ConvI(ConvF(I)) -> I, iff float mantissa >= int mode */
1271 unsigned int_mantissa = get_mode_size_bits(n_mode) - (mode_is_signed(n_mode) ? 1 : 0);
1272 unsigned float_mantissa = tarval_ieee754_get_mantissa_size(a_mode);
1274 if (float_mantissa >= int_mantissa) {
1276 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
1281 if (smaller_mode(b_mode, a_mode)) {
1282 if (get_Conv_strict(n))
1283 set_Conv_strict(b, 1);
1284 n = b; /* ConvA(ConvB(ConvA(...))) == ConvA(...) */
1285 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
1292 } /* equivalent_node_Conv */
1295 * A Cast may be removed if the type of the previous node
1296 * is already the type of the Cast.
1298 static ir_node *equivalent_node_Cast(ir_node *n) {
1300 ir_node *pred = get_Cast_op(n);
1302 if (get_irn_type(pred) == get_Cast_type(n)) {
1304 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CAST);
1307 } /* equivalent_node_Cast */
1310 * - fold Phi-nodes, iff they have only one predecessor except
1313 static ir_node *equivalent_node_Phi(ir_node *n) {
1318 ir_node *first_val = NULL; /* to shutup gcc */
1320 if (!get_opt_normalize()) return n;
1322 n_preds = get_Phi_n_preds(n);
1324 block = get_nodes_block(n);
1325 if (is_Block_dead(block)) /* Control dead */
1326 return get_irg_bad(current_ir_graph);
1328 if (n_preds == 0) return n; /* Phi of dead Region without predecessors. */
1330 /* Find first non-self-referencing input */
1331 for (i = 0; i < n_preds; ++i) {
1332 first_val = get_Phi_pred(n, i);
1333 if ( (first_val != n) /* not self pointer */
1335 /* BEWARE: when the if is changed to 1, Phi's will ignore it's Bad
1336 * predecessors. Then, Phi nodes in dead code might be removed, causing
1337 * nodes pointing to themself (Add's for instance).
1338 * This is really bad and causes endless recursions in several
1339 * code pathes, so we do NOT optimize such a code.
1340 * This is not that bad as it sounds, optimize_cf() removes bad control flow
1341 * (and bad Phi predecessors), so live code is optimized later.
1343 && (! is_Bad(get_Block_cfgpred(block, i)))
1345 ) { /* value not dead */
1346 break; /* then found first value. */
1351 /* A totally Bad or self-referencing Phi (we didn't break the above loop) */
1352 return get_irg_bad(current_ir_graph);
1355 /* search for rest of inputs, determine if any of these
1356 are non-self-referencing */
1357 while (++i < n_preds) {
1358 ir_node *scnd_val = get_Phi_pred(n, i);
1359 if ( (scnd_val != n)
1360 && (scnd_val != first_val)
1363 && (! is_Bad(get_Block_cfgpred(block, i)))
1371 /* Fold, if no multiple distinct non-self-referencing inputs */
1373 DBG_OPT_PHI(oldn, n);
1376 } /* equivalent_node_Phi */
1379 * Several optimizations:
1380 * - fold Sync-nodes, iff they have only one predecessor except
1383 static ir_node *equivalent_node_Sync(ir_node *n) {
1384 int arity = get_Sync_n_preds(n);
1387 for (i = 0; i < arity;) {
1388 ir_node *pred = get_Sync_pred(n, i);
1391 /* Remove Bad predecessors */
1398 /* Remove duplicate predecessors */
1404 if (get_Sync_pred(n, j) == pred) {
1412 if (arity == 0) return get_irg_bad(current_ir_graph);
1413 if (arity == 1) return get_Sync_pred(n, 0);
1415 } /* equivalent_node_Sync */
1418 * Optimize Proj(Tuple).
1420 static ir_node *equivalent_node_Proj_Tuple(ir_node *proj) {
1421 ir_node *oldn = proj;
1422 ir_node *tuple = get_Proj_pred(proj);
1424 /* Remove the Tuple/Proj combination. */
1425 proj = get_Tuple_pred(tuple, get_Proj_proj(proj));
1426 DBG_OPT_TUPLE(oldn, tuple, proj);
1429 } /* equivalent_node_Proj_Tuple */
1432 * Optimize a / 1 = a.
1434 static ir_node *equivalent_node_Proj_Div(ir_node *proj) {
1435 ir_node *oldn = proj;
1436 ir_node *div = get_Proj_pred(proj);
1437 ir_node *b = get_Div_right(div);
1438 tarval *tb = value_of(b);
1440 /* Div is not commutative. */
1441 if (tarval_is_one(tb)) { /* div(x, 1) == x */
1442 switch (get_Proj_proj(proj)) {
1444 proj = get_Div_mem(div);
1445 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NEUTRAL_1);
1449 proj = get_Div_left(div);
1450 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NEUTRAL_1);
1454 /* we cannot replace the exception Proj's here, this is done in
1455 transform_node_Proj_Div() */
1460 } /* equivalent_node_Proj_Div */
1463 * Optimize a / 1.0 = a.
1465 static ir_node *equivalent_node_Proj_Quot(ir_node *proj) {
1466 ir_node *oldn = proj;
1467 ir_node *quot = get_Proj_pred(proj);
1468 ir_node *b = get_Quot_right(quot);
1469 tarval *tb = value_of(b);
1471 /* Div is not commutative. */
1472 if (tarval_is_one(tb)) { /* Quot(x, 1) == x */
1473 switch (get_Proj_proj(proj)) {
1475 proj = get_Quot_mem(quot);
1476 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NEUTRAL_1);
1480 proj = get_Quot_left(quot);
1481 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NEUTRAL_1);
1485 /* we cannot replace the exception Proj's here, this is done in
1486 transform_node_Proj_Quot() */
1491 } /* equivalent_node_Proj_Quot */
1494 * Optimize a / 1 = a.
1496 static ir_node *equivalent_node_Proj_DivMod(ir_node *proj) {
1497 ir_node *oldn = proj;
1498 ir_node *divmod = get_Proj_pred(proj);
1499 ir_node *b = get_DivMod_right(divmod);
1500 tarval *tb = value_of(b);
1502 /* Div is not commutative. */
1503 if (tarval_is_one(tb)) { /* div(x, 1) == x */
1504 switch (get_Proj_proj(proj)) {
1506 proj = get_DivMod_mem(divmod);
1507 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NEUTRAL_1);
1510 case pn_DivMod_res_div:
1511 proj = get_DivMod_left(divmod);
1512 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NEUTRAL_1);
1516 /* we cannot replace the exception Proj's here, this is done in
1517 transform_node_Proj_DivMod().
1518 Note further that the pn_DivMod_res_div case is handled in
1519 computed_value_Proj(). */
1524 } /* equivalent_node_Proj_DivMod */
1527 * Optimize CopyB(mem, x, x) into a Nop.
1529 static ir_node *equivalent_node_Proj_CopyB(ir_node *proj) {
1530 ir_node *oldn = proj;
1531 ir_node *copyb = get_Proj_pred(proj);
1532 ir_node *a = get_CopyB_dst(copyb);
1533 ir_node *b = get_CopyB_src(copyb);
1536 /* Turn CopyB into a tuple (mem, jmp, bad, bad) */
1537 switch (get_Proj_proj(proj)) {
1538 case pn_CopyB_M_regular:
1539 proj = get_CopyB_mem(copyb);
1540 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NOP);
1543 case pn_CopyB_M_except:
1544 case pn_CopyB_X_except:
1545 DBG_OPT_EXC_REM(proj);
1546 proj = get_irg_bad(current_ir_graph);
1551 } /* equivalent_node_Proj_CopyB */
1554 * Optimize Bounds(idx, idx, upper) into idx.
1556 static ir_node *equivalent_node_Proj_Bound(ir_node *proj) {
1557 ir_node *oldn = proj;
1558 ir_node *bound = get_Proj_pred(proj);
1559 ir_node *idx = get_Bound_index(bound);
1560 ir_node *pred = skip_Proj(idx);
1563 if (idx == get_Bound_lower(bound))
1565 else if (is_Bound(pred)) {
1567 * idx was Bounds checked in the same MacroBlock previously,
1568 * it is still valid if lower <= pred_lower && pred_upper <= upper.
1570 ir_node *lower = get_Bound_lower(bound);
1571 ir_node *upper = get_Bound_upper(bound);
1572 if (get_Bound_lower(pred) == lower &&
1573 get_Bound_upper(pred) == upper &&
1574 get_irn_MacroBlock(bound) == get_irn_MacroBlock(pred)) {
1576 * One could expect that we simply return the previous
1577 * Bound here. However, this would be wrong, as we could
1578 * add an exception Proj to a new location then.
1579 * So, we must turn in into a tuple.
1585 /* Turn Bound into a tuple (mem, jmp, bad, idx) */
1586 switch (get_Proj_proj(proj)) {
1588 DBG_OPT_EXC_REM(proj);
1589 proj = get_Bound_mem(bound);
1591 case pn_Bound_X_except:
1592 DBG_OPT_EXC_REM(proj);
1593 proj = get_irg_bad(current_ir_graph);
1597 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NOP);
1600 /* cannot optimize pn_Bound_X_regular, handled in transform ... */
1605 } /* equivalent_node_Proj_Bound */
1608 * Optimize an Exception Proj(Load) with a non-null address.
1610 static ir_node *equivalent_node_Proj_Load(ir_node *proj) {
1611 if (get_opt_ldst_only_null_ptr_exceptions()) {
1612 if (get_irn_mode(proj) == mode_X) {
1613 ir_node *load = get_Proj_pred(proj);
1615 /* get the Load address */
1616 const ir_node *addr = get_Load_ptr(load);
1617 const ir_node *confirm;
1619 if (value_not_null(addr, &confirm)) {
1620 if (get_Proj_proj(proj) == pn_Load_X_except) {
1621 DBG_OPT_EXC_REM(proj);
1622 return get_irg_bad(current_ir_graph);
1628 } /* equivalent_node_Proj_Load */
1631 * Optimize an Exception Proj(Store) with a non-null address.
1633 static ir_node *equivalent_node_Proj_Store(ir_node *proj) {
1634 if (get_opt_ldst_only_null_ptr_exceptions()) {
1635 if (get_irn_mode(proj) == mode_X) {
1636 ir_node *store = get_Proj_pred(proj);
1638 /* get the load/store address */
1639 const ir_node *addr = get_Store_ptr(store);
1640 const ir_node *confirm;
1642 if (value_not_null(addr, &confirm)) {
1643 if (get_Proj_proj(proj) == pn_Store_X_except) {
1644 DBG_OPT_EXC_REM(proj);
1645 return get_irg_bad(current_ir_graph);
1651 } /* equivalent_node_Proj_Store */
1654 * Does all optimizations on nodes that must be done on it's Proj's
1655 * because of creating new nodes.
1657 static ir_node *equivalent_node_Proj(ir_node *proj) {
1658 ir_node *n = get_Proj_pred(proj);
1660 if (get_irn_mode(proj) == mode_X) {
1661 if (is_Block_dead(get_nodes_block(n))) {
1662 /* Remove dead control flow -- early gigo(). */
1663 return get_irg_bad(current_ir_graph);
1666 if (n->op->ops.equivalent_node_Proj)
1667 return n->op->ops.equivalent_node_Proj(proj);
1669 } /* equivalent_node_Proj */
1674 static ir_node *equivalent_node_Id(ir_node *n) {
1681 DBG_OPT_ID(oldn, n);
1683 } /* equivalent_node_Id */
1688 static ir_node *equivalent_node_Mux(ir_node *n)
1690 ir_node *oldn = n, *sel = get_Mux_sel(n);
1691 tarval *ts = value_of(sel);
1693 /* Mux(true, f, t) == t */
1694 if (ts == tarval_b_true) {
1695 n = get_Mux_true(n);
1696 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_C);
1698 /* Mux(false, f, t) == f */
1699 else if (ts == tarval_b_false) {
1700 n = get_Mux_false(n);
1701 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_C);
1703 /* Mux(v, x, x) == x */
1704 else if (get_Mux_false(n) == get_Mux_true(n)) {
1705 n = get_Mux_true(n);
1706 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_EQ);
1708 else if (is_Proj(sel) && !mode_honor_signed_zeros(get_irn_mode(n))) {
1709 ir_node *cmp = get_Proj_pred(sel);
1710 long proj_nr = get_Proj_proj(sel);
1711 ir_node *f = get_Mux_false(n);
1712 ir_node *t = get_Mux_true(n);
1715 * Note further that these optimization work even for floating point
1716 * with NaN's because -NaN == NaN.
1717 * However, if +0 and -0 is handled differently, we cannot use the first one.
1720 ir_node *const cmp_l = get_Cmp_left(cmp);
1721 ir_node *const cmp_r = get_Cmp_right(cmp);
1725 if ((cmp_l == t && cmp_r == f) || /* Mux(t == f, t, f) -> f */
1726 (cmp_l == f && cmp_r == t)) { /* Mux(f == t, t, f) -> f */
1728 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1735 if ((cmp_l == t && cmp_r == f) || /* Mux(t != f, t, f) -> t */
1736 (cmp_l == f && cmp_r == t)) { /* Mux(f != t, t, f) -> t */
1738 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1745 * Note: normalization puts the constant on the right side,
1746 * so we check only one case.
1748 if (cmp_l == t && tarval_is_null(value_of(cmp_r))) {
1749 /* Mux(t CMP 0, X, t) */
1750 if (is_Minus(f) && get_Minus_op(f) == t) {
1751 /* Mux(t CMP 0, -t, t) */
1752 if (proj_nr == pn_Cmp_Eq) {
1753 /* Mux(t == 0, -t, t) ==> -t */
1755 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1756 } else if (proj_nr == pn_Cmp_Lg || proj_nr == pn_Cmp_Ne) {
1757 /* Mux(t != 0, -t, t) ==> t */
1759 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1766 } /* equivalent_node_Mux */
1769 * Remove Confirm nodes if setting is on.
1770 * Replace Confirms(x, '=', Constlike) by Constlike.
1772 static ir_node *equivalent_node_Confirm(ir_node *n) {
1773 ir_node *pred = get_Confirm_value(n);
1774 pn_Cmp pnc = get_Confirm_cmp(n);
1776 while (is_Confirm(pred) && pnc == get_Confirm_cmp(pred)) {
1778 * rare case: two identical Confirms one after another,
1779 * replace the second one with the first.
1782 pred = get_Confirm_value(n);
1783 pnc = get_Confirm_cmp(n);
1785 if (get_opt_remove_confirm())
1786 return get_Confirm_value(n);
1791 * equivalent_node() returns a node equivalent to input n. It skips all nodes that
1792 * perform no actual computation, as, e.g., the Id nodes. It does not create
1793 * new nodes. It is therefore safe to free n if the node returned is not n.
1794 * If a node returns a Tuple we can not just skip it. If the size of the
1795 * in array fits, we transform n into a tuple (e.g., Div).
1797 ir_node *equivalent_node(ir_node *n) {
1798 if (n->op->ops.equivalent_node)
1799 return n->op->ops.equivalent_node(n);
1801 } /* equivalent_node */
1804 * Sets the default equivalent node operation for an ir_op_ops.
1806 * @param code the opcode for the default operation
1807 * @param ops the operations initialized
1812 static ir_op_ops *firm_set_default_equivalent_node(ir_opcode code, ir_op_ops *ops)
1816 ops->equivalent_node = equivalent_node_##a; \
1818 #define CASE_PROJ(a) \
1820 ops->equivalent_node_Proj = equivalent_node_Proj_##a; \
1862 } /* firm_set_default_equivalent_node */
1865 * Returns non-zero if a node is a Phi node
1866 * with all predecessors constant.
1868 static int is_const_Phi(ir_node *n) {
1871 if (! is_Phi(n) || get_irn_arity(n) == 0)
1873 for (i = get_irn_arity(n) - 1; i >= 0; --i) {
1874 if (! is_Const(get_irn_n(n, i)))
1878 } /* is_const_Phi */
1880 typedef tarval *(*tarval_sub_type)(tarval *a, tarval *b, ir_mode *mode);
1881 typedef tarval *(*tarval_binop_type)(tarval *a, tarval *b);
1884 * Wrapper for the tarval binop evaluation, tarval_sub has one more parameter.
1886 static tarval *do_eval(tarval *(*eval)(), tarval *a, tarval *b, ir_mode *mode) {
1887 if (eval == tarval_sub) {
1888 tarval_sub_type func = (tarval_sub_type)eval;
1890 return func(a, b, mode);
1892 tarval_binop_type func = (tarval_binop_type)eval;
1899 * Apply an evaluator on a binop with a constant operators (and one Phi).
1901 * @param phi the Phi node
1902 * @param other the other operand
1903 * @param eval an evaluator function
1904 * @param mode the mode of the result, may be different from the mode of the Phi!
1905 * @param left if non-zero, other is the left operand, else the right
1907 * @return a new Phi node if the conversion was successful, NULL else
1909 static ir_node *apply_binop_on_phi(ir_node *phi, tarval *other, tarval *(*eval)(), ir_mode *mode, int left) {
1914 int i, n = get_irn_arity(phi);
1916 NEW_ARR_A(void *, res, n);
1918 for (i = 0; i < n; ++i) {
1919 pred = get_irn_n(phi, i);
1920 tv = get_Const_tarval(pred);
1921 tv = do_eval(eval, other, tv, mode);
1923 if (tv == tarval_bad) {
1924 /* folding failed, bad */
1930 for (i = 0; i < n; ++i) {
1931 pred = get_irn_n(phi, i);
1932 tv = get_Const_tarval(pred);
1933 tv = do_eval(eval, tv, other, mode);
1935 if (tv == tarval_bad) {
1936 /* folding failed, bad */
1942 irg = current_ir_graph;
1943 for (i = 0; i < n; ++i) {
1944 pred = get_irn_n(phi, i);
1945 res[i] = new_r_Const_type(irg, get_irg_start_block(irg),
1946 mode, res[i], get_Const_type(pred));
1948 return new_r_Phi(irg, get_nodes_block(phi), n, (ir_node **)res, mode);
1949 } /* apply_binop_on_phi */
1952 * Apply an evaluator on a binop with two constant Phi.
1954 * @param a the left Phi node
1955 * @param b the right Phi node
1956 * @param eval an evaluator function
1957 * @param mode the mode of the result, may be different from the mode of the Phi!
1959 * @return a new Phi node if the conversion was successful, NULL else
1961 static ir_node *apply_binop_on_2_phis(ir_node *a, ir_node *b, tarval *(*eval)(), ir_mode *mode) {
1962 tarval *tv_l, *tv_r, *tv;
1968 if (get_nodes_block(a) != get_nodes_block(b))
1971 n = get_irn_arity(a);
1972 NEW_ARR_A(void *, res, n);
1974 for (i = 0; i < n; ++i) {
1975 pred = get_irn_n(a, i);
1976 tv_l = get_Const_tarval(pred);
1977 pred = get_irn_n(b, i);
1978 tv_r = get_Const_tarval(pred);
1979 tv = do_eval(eval, tv_l, tv_r, mode);
1981 if (tv == tarval_bad) {
1982 /* folding failed, bad */
1987 irg = current_ir_graph;
1988 for (i = 0; i < n; ++i) {
1989 pred = get_irn_n(a, i);
1990 res[i] = new_r_Const_type(irg, get_irg_start_block(irg), mode, res[i], get_Const_type(pred));
1992 return new_r_Phi(irg, get_nodes_block(a), n, (ir_node **)res, mode);
1993 } /* apply_binop_on_2_phis */
1996 * Apply an evaluator on a unop with a constant operator (a Phi).
1998 * @param phi the Phi node
1999 * @param eval an evaluator function
2001 * @return a new Phi node if the conversion was successful, NULL else
2003 static ir_node *apply_unop_on_phi(ir_node *phi, tarval *(*eval)(tarval *)) {
2009 int i, n = get_irn_arity(phi);
2011 NEW_ARR_A(void *, res, n);
2012 for (i = 0; i < n; ++i) {
2013 pred = get_irn_n(phi, i);
2014 tv = get_Const_tarval(pred);
2017 if (tv == tarval_bad) {
2018 /* folding failed, bad */
2023 mode = get_irn_mode(phi);
2024 irg = current_ir_graph;
2025 for (i = 0; i < n; ++i) {
2026 pred = get_irn_n(phi, i);
2027 res[i] = new_r_Const_type(irg, get_irg_start_block(irg),
2028 mode, res[i], get_Const_type(pred));
2030 return new_r_Phi(irg, get_nodes_block(phi), n, (ir_node **)res, mode);
2031 } /* apply_unop_on_phi */
2034 * Apply a conversion on a constant operator (a Phi).
2036 * @param phi the Phi node
2038 * @return a new Phi node if the conversion was successful, NULL else
2040 static ir_node *apply_conv_on_phi(ir_node *phi, ir_mode *mode) {
2045 int i, n = get_irn_arity(phi);
2047 NEW_ARR_A(void *, res, n);
2048 for (i = 0; i < n; ++i) {
2049 pred = get_irn_n(phi, i);
2050 tv = get_Const_tarval(pred);
2051 tv = tarval_convert_to(tv, mode);
2053 if (tv == tarval_bad) {
2054 /* folding failed, bad */
2059 irg = current_ir_graph;
2060 for (i = 0; i < n; ++i) {
2061 pred = get_irn_n(phi, i);
2062 res[i] = new_r_Const_type(irg, get_irg_start_block(irg),
2063 mode, res[i], get_Const_type(pred));
2065 return new_r_Phi(irg, get_nodes_block(phi), n, (ir_node **)res, mode);
2066 } /* apply_conv_on_phi */
2069 * Transform AddP(P, ConvIs(Iu)), AddP(P, ConvIu(Is)) and
2070 * SubP(P, ConvIs(Iu)), SubP(P, ConvIu(Is)).
2071 * If possible, remove the Conv's.
2073 static ir_node *transform_node_AddSub(ir_node *n) {
2074 ir_mode *mode = get_irn_mode(n);
2076 if (mode_is_reference(mode)) {
2077 ir_node *left = get_binop_left(n);
2078 ir_node *right = get_binop_right(n);
2079 unsigned ref_bits = get_mode_size_bits(mode);
2081 if (is_Conv(left)) {
2082 ir_mode *lmode = get_irn_mode(left);
2083 unsigned bits = get_mode_size_bits(lmode);
2085 if (ref_bits == bits &&
2086 mode_is_int(lmode) &&
2087 get_mode_arithmetic(lmode) == irma_twos_complement) {
2088 ir_node *pre = get_Conv_op(left);
2089 ir_mode *pre_mode = get_irn_mode(pre);
2091 if (mode_is_int(pre_mode) &&
2092 get_mode_size_bits(pre_mode) == bits &&
2093 get_mode_arithmetic(pre_mode) == irma_twos_complement) {
2094 /* ok, this conv just changes to sign, moreover the calculation
2095 * is done with same number of bits as our address mode, so
2096 * we can ignore the conv as address calculation can be viewed
2097 * as either signed or unsigned
2099 set_binop_left(n, pre);
2104 if (is_Conv(right)) {
2105 ir_mode *rmode = get_irn_mode(right);
2106 unsigned bits = get_mode_size_bits(rmode);
2108 if (ref_bits == bits &&
2109 mode_is_int(rmode) &&
2110 get_mode_arithmetic(rmode) == irma_twos_complement) {
2111 ir_node *pre = get_Conv_op(right);
2112 ir_mode *pre_mode = get_irn_mode(pre);
2114 if (mode_is_int(pre_mode) &&
2115 get_mode_size_bits(pre_mode) == bits &&
2116 get_mode_arithmetic(pre_mode) == irma_twos_complement) {
2117 /* ok, this conv just changes to sign, moreover the calculation
2118 * is done with same number of bits as our address mode, so
2119 * we can ignore the conv as address calculation can be viewed
2120 * as either signed or unsigned
2122 set_binop_right(n, pre);
2127 /* let address arithmetic use unsigned modes */
2128 if (is_Const(right)) {
2129 ir_mode *rmode = get_irn_mode(right);
2131 if (mode_is_signed(rmode) && get_mode_arithmetic(rmode) == irma_twos_complement) {
2132 /* convert a AddP(P, *s) into AddP(P, *u) */
2133 ir_mode *nm = get_reference_mode_unsigned_eq(mode);
2135 ir_node *pre = new_r_Conv(current_ir_graph, get_nodes_block(n), right, nm);
2136 set_binop_right(n, pre);
2142 } /* transform_node_AddSub */
2144 #define HANDLE_BINOP_PHI(eval, a, b, c, mode) \
2146 if (is_Const(b) && is_const_Phi(a)) { \
2147 /* check for Op(Phi, Const) */ \
2148 c = apply_binop_on_phi(a, get_Const_tarval(b), eval, mode, 0);\
2150 else if (is_Const(a) && is_const_Phi(b)) { \
2151 /* check for Op(Const, Phi) */ \
2152 c = apply_binop_on_phi(b, get_Const_tarval(a), eval, mode, 1);\
2154 else if (is_const_Phi(a) && is_const_Phi(b)) { \
2155 /* check for Op(Phi, Phi) */ \
2156 c = apply_binop_on_2_phis(a, b, eval, mode); \
2159 DBG_OPT_ALGSIM0(oldn, c, FS_OPT_CONST_PHI); \
2163 #define HANDLE_UNOP_PHI(eval, a, c) \
2165 if (is_const_Phi(a)) { \
2166 /* check for Op(Phi) */ \
2167 c = apply_unop_on_phi(a, eval); \
2169 DBG_OPT_ALGSIM0(oldn, c, FS_OPT_CONST_PHI); \
2175 * Do the AddSub optimization, then Transform
2176 * Constant folding on Phi
2177 * Add(a,a) -> Mul(a, 2)
2178 * Add(Mul(a, x), a) -> Mul(a, x+1)
2179 * if the mode is integer or float.
2180 * Transform Add(a,-b) into Sub(a,b).
2181 * Reassociation might fold this further.
2183 static ir_node *transform_node_Add(ir_node *n) {
2185 ir_node *a, *b, *c, *oldn = n;
2187 n = transform_node_AddSub(n);
2189 a = get_Add_left(n);
2190 b = get_Add_right(n);
2192 mode = get_irn_mode(n);
2194 if (mode_is_reference(mode)) {
2195 ir_mode *lmode = get_irn_mode(a);
2197 if (is_Const(b) && is_Const_null(b) && mode_is_int(lmode)) {
2198 /* an Add(a, NULL) is a hidden Conv */
2199 dbg_info *dbg = get_irn_dbg_info(n);
2200 return new_rd_Conv(dbg, current_ir_graph, get_nodes_block(n), a, mode);
2204 HANDLE_BINOP_PHI(tarval_add, a, b, c, mode);
2206 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
2207 if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
2210 if (mode_is_num(mode)) {
2211 /* the following code leads to endless recursion when Mul are replaced by a simple instruction chain */
2212 if (!is_arch_dep_running() && a == b && mode_is_int(mode)) {
2213 ir_node *block = get_nodes_block(n);
2216 get_irn_dbg_info(n),
2220 new_r_Const_long(current_ir_graph, block, mode, 2),
2222 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_A);
2227 get_irn_dbg_info(n),
2233 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_MINUS_B);
2238 get_irn_dbg_info(n),
2244 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_MINUS_B);
2247 if (get_mode_arithmetic(mode) == irma_twos_complement) {
2248 /* Here we rely on constants be on the RIGHT side */
2250 ir_node *op = get_Not_op(a);
2252 if (is_Const(b) && is_Const_one(b)) {
2254 ir_node *blk = get_nodes_block(n);
2255 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, blk, op, mode);
2256 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_PLUS_1);
2261 ir_node *blk = get_nodes_block(n);
2262 n = new_r_Const(current_ir_graph, blk, mode, get_mode_minus_one(mode));
2263 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_X_NOT_X);
2268 ir_node *op = get_Not_op(b);
2272 ir_node *blk = get_nodes_block(n);
2273 n = new_r_Const(current_ir_graph, blk, mode, get_mode_minus_one(mode));
2274 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_X_NOT_X);
2281 } /* transform_node_Add */
2284 * returns -cnst or NULL if impossible
2286 static ir_node *const_negate(ir_node *cnst) {
2287 tarval *tv = tarval_neg(get_Const_tarval(cnst));
2288 dbg_info *dbgi = get_irn_dbg_info(cnst);
2289 ir_graph *irg = get_irn_irg(cnst);
2290 ir_node *block = get_nodes_block(cnst);
2291 ir_mode *mode = get_irn_mode(cnst);
2292 if (tv == tarval_bad) return NULL;
2293 return new_rd_Const(dbgi, irg, block, mode, tv);
2297 * Do the AddSub optimization, then Transform
2298 * Constant folding on Phi
2299 * Sub(0,a) -> Minus(a)
2300 * Sub(Mul(a, x), a) -> Mul(a, x-1)
2301 * Sub(Sub(x, y), b) -> Sub(x, Add(y,b))
2302 * Sub(Add(a, x), x) -> a
2303 * Sub(x, Add(x, a)) -> -a
2304 * Sub(x, Const) -> Add(x, -Const)
2306 static ir_node *transform_node_Sub(ir_node *n) {
2311 n = transform_node_AddSub(n);
2313 a = get_Sub_left(n);
2314 b = get_Sub_right(n);
2316 mode = get_irn_mode(n);
2318 if (mode_is_int(mode)) {
2319 ir_mode *lmode = get_irn_mode(a);
2321 if (is_Const(b) && is_Const_null(b) && mode_is_reference(lmode)) {
2322 /* a Sub(a, NULL) is a hidden Conv */
2323 dbg_info *dbg = get_irn_dbg_info(n);
2324 n = new_rd_Conv(dbg, current_ir_graph, get_nodes_block(n), a, mode);
2325 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_CONV);
2329 if (mode == lmode &&
2330 get_mode_arithmetic(mode) == irma_twos_complement &&
2332 get_Const_tarval(a) == get_mode_minus_one(mode)) {
2334 dbg_info *dbg = get_irn_dbg_info(n);
2335 n = new_rd_Not(dbg, current_ir_graph, get_nodes_block(n), b, mode);
2336 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_NOT);
2342 HANDLE_BINOP_PHI(tarval_sub, a, b, c, mode);
2344 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
2345 if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
2348 if (is_Const(b) && get_irn_mode(b) != mode_P) {
2349 /* a - C -> a + (-C) */
2350 ir_node *cnst = const_negate(b);
2352 ir_node *block = get_nodes_block(n);
2353 dbg_info *dbgi = get_irn_dbg_info(n);
2354 ir_graph *irg = get_irn_irg(n);
2356 n = new_rd_Add(dbgi, irg, block, a, cnst, mode);
2357 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2362 if (is_Minus(a)) { /* (-a) - b -> -(a + b) */
2363 ir_graph *irg = current_ir_graph;
2364 dbg_info *dbg = get_irn_dbg_info(n);
2365 ir_node *block = get_nodes_block(n);
2366 ir_node *left = get_Minus_op(a);
2367 ir_node *add = new_rd_Add(dbg, irg, block, left, b, mode);
2369 n = new_rd_Minus(dbg, irg, block, add, mode);
2370 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2372 } else if (is_Minus(b)) { /* a - (-b) -> a + b */
2373 ir_graph *irg = current_ir_graph;
2374 dbg_info *dbg = get_irn_dbg_info(n);
2375 ir_node *block = get_nodes_block(n);
2376 ir_node *right = get_Minus_op(b);
2378 n = new_rd_Add(dbg, irg, block, a, right, mode);
2379 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MINUS);
2381 } else if (is_Sub(b)) {
2382 /* a - (b - c) -> a + (c - b)
2383 * -> (a - b) + c iff (b - c) is a pointer */
2384 ir_graph *irg = current_ir_graph;
2385 dbg_info *s_dbg = get_irn_dbg_info(b);
2386 ir_node *s_block = get_nodes_block(b);
2387 ir_node *s_left = get_Sub_left(b);
2388 ir_node *s_right = get_Sub_right(b);
2389 ir_mode *s_mode = get_irn_mode(b);
2390 if (s_mode == mode_P) {
2391 ir_node *sub = new_rd_Sub(s_dbg, irg, s_block, a, s_left, mode);
2392 dbg_info *a_dbg = get_irn_dbg_info(n);
2393 ir_node *a_block = get_nodes_block(n);
2396 s_right = new_r_Conv(irg, a_block, s_right, mode);
2397 n = new_rd_Add(a_dbg, irg, a_block, sub, s_right, mode);
2399 ir_node *sub = new_rd_Sub(s_dbg, irg, s_block, s_right, s_left, s_mode);
2400 dbg_info *a_dbg = get_irn_dbg_info(n);
2401 ir_node *a_block = get_nodes_block(n);
2403 n = new_rd_Add(a_dbg, irg, a_block, a, sub, mode);
2405 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2407 } else if (is_Mul(b)) { /* a - (b * C) -> a + (b * -C) */
2408 ir_node *m_right = get_Mul_right(b);
2409 if (is_Const(m_right)) {
2410 ir_node *cnst2 = const_negate(m_right);
2411 if (cnst2 != NULL) {
2412 ir_graph *irg = current_ir_graph;
2413 dbg_info *m_dbg = get_irn_dbg_info(b);
2414 ir_node *m_block = get_nodes_block(b);
2415 ir_node *m_left = get_Mul_left(b);
2416 ir_mode *m_mode = get_irn_mode(b);
2417 ir_node *mul = new_rd_Mul(m_dbg, irg, m_block, m_left, cnst2, m_mode);
2418 dbg_info *a_dbg = get_irn_dbg_info(n);
2419 ir_node *a_block = get_nodes_block(n);
2421 n = new_rd_Add(a_dbg, irg, a_block, a, mul, mode);
2422 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2428 /* Beware of Sub(P, P) which cannot be optimized into a simple Minus ... */
2429 if (mode_is_num(mode) && mode == get_irn_mode(a) && is_Const(a) && is_Const_null(a)) {
2431 get_irn_dbg_info(n),
2436 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_0_A);
2440 if (mode_wrap_around(mode)) {
2441 ir_node *left = get_Add_left(a);
2442 ir_node *right = get_Add_right(a);
2444 /* FIXME: Does the Conv's work only for two complement or generally? */
2446 if (mode != get_irn_mode(right)) {
2447 /* This Sub is an effective Cast */
2448 right = new_r_Conv(get_irn_irg(n), get_nodes_block(n), right, mode);
2451 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2453 } else if (right == b) {
2454 if (mode != get_irn_mode(left)) {
2455 /* This Sub is an effective Cast */
2456 left = new_r_Conv(get_irn_irg(n), get_nodes_block(n), left, mode);
2459 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2465 if (mode_wrap_around(mode)) {
2466 ir_node *left = get_Add_left(b);
2467 ir_node *right = get_Add_right(b);
2469 /* FIXME: Does the Conv's work only for two complement or generally? */
2471 ir_mode *r_mode = get_irn_mode(right);
2473 n = new_r_Minus(get_irn_irg(n), get_nodes_block(n), right, r_mode);
2474 if (mode != r_mode) {
2475 /* This Sub is an effective Cast */
2476 n = new_r_Conv(get_irn_irg(n), get_nodes_block(n), n, mode);
2478 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2480 } else if (right == a) {
2481 ir_mode *l_mode = get_irn_mode(left);
2483 n = new_r_Minus(get_irn_irg(n), get_nodes_block(n), left, l_mode);
2484 if (mode != l_mode) {
2485 /* This Sub is an effective Cast */
2486 n = new_r_Conv(get_irn_irg(n), get_nodes_block(n), n, mode);
2488 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2493 if (mode_is_int(mode) && is_Conv(a) && is_Conv(b)) {
2494 ir_mode *mode = get_irn_mode(a);
2496 if (mode == get_irn_mode(b)) {
2498 ir_node *op_a = get_Conv_op(a);
2499 ir_node *op_b = get_Conv_op(b);
2501 /* check if it's allowed to skip the conv */
2502 ma = get_irn_mode(op_a);
2503 mb = get_irn_mode(op_b);
2505 if (mode_is_reference(ma) && mode_is_reference(mb)) {
2506 /* SubInt(ConvInt(aP), ConvInt(bP)) -> SubInt(aP,bP) */
2509 set_Sub_right(n, b);
2515 /* do NOT execute this code if reassociation is enabled, it does the inverse! */
2516 if (!is_reassoc_running() && is_Mul(a)) {
2517 ir_node *ma = get_Mul_left(a);
2518 ir_node *mb = get_Mul_right(a);
2521 ir_node *blk = get_nodes_block(n);
2523 get_irn_dbg_info(n),
2524 current_ir_graph, blk,
2527 get_irn_dbg_info(n),
2528 current_ir_graph, blk,
2530 new_r_Const_long(current_ir_graph, blk, mode, 1),
2533 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MUL_A_X_A);
2535 } else if (mb == b) {
2536 ir_node *blk = get_nodes_block(n);
2538 get_irn_dbg_info(n),
2539 current_ir_graph, blk,
2542 get_irn_dbg_info(n),
2543 current_ir_graph, blk,
2545 new_r_Const_long(current_ir_graph, blk, mode, 1),
2548 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MUL_A_X_A);
2552 if (is_Sub(a)) { /* (x - y) - b -> x - (y + b) */
2553 ir_node *x = get_Sub_left(a);
2554 ir_node *y = get_Sub_right(a);
2555 ir_node *blk = get_nodes_block(n);
2556 ir_mode *m_b = get_irn_mode(b);
2557 ir_mode *m_y = get_irn_mode(y);
2561 /* Determine the right mode for the Add. */
2564 else if (mode_is_reference(m_b))
2566 else if (mode_is_reference(m_y))
2570 * Both modes are different but none is reference,
2571 * happens for instance in SubP(SubP(P, Iu), Is).
2572 * We have two possibilities here: Cast or ignore.
2573 * Currently we ignore this case.
2578 add = new_r_Add(current_ir_graph, blk, y, b, add_mode);
2580 n = new_rd_Sub(get_irn_dbg_info(n), current_ir_graph, blk, x, add, mode);
2581 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_SUB_X_Y_Z);
2585 if (get_mode_arithmetic(mode) == irma_twos_complement) {
2586 if (is_Const(a) && is_Not(b)) {
2587 /* c - ~X = X + (c+1) */
2588 tarval *tv = get_Const_tarval(a);
2590 tv = tarval_add(tv, get_mode_one(mode));
2591 if (tv != tarval_bad) {
2592 ir_node *blk = get_nodes_block(n);
2593 ir_node *c = new_r_Const(current_ir_graph, blk, mode, tv);
2594 n = new_rd_Add(get_irn_dbg_info(n), current_ir_graph, blk, get_Not_op(b), c, mode);
2595 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_C_NOT_X);
2601 } /* transform_node_Sub */
2604 * Several transformation done on n*n=2n bits mul.
2605 * These transformations must be done here because new nodes may be produced.
2607 static ir_node *transform_node_Mul2n(ir_node *n, ir_mode *mode) {
2609 ir_node *a = get_Mul_left(n);
2610 ir_node *b = get_Mul_right(n);
2611 tarval *ta = value_of(a);
2612 tarval *tb = value_of(b);
2613 ir_mode *smode = get_irn_mode(a);
2615 if (ta == get_mode_one(smode)) {
2616 /* (L)1 * (L)b = (L)b */
2617 ir_node *blk = get_nodes_block(n);
2618 n = new_rd_Conv(get_irn_dbg_info(n), current_ir_graph, blk, b, mode);
2619 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
2622 else if (ta == get_mode_minus_one(smode)) {
2623 /* (L)-1 * (L)b = (L)b */
2624 ir_node *blk = get_nodes_block(n);
2625 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, blk, b, smode);
2626 n = new_rd_Conv(get_irn_dbg_info(n), current_ir_graph, blk, n, mode);
2627 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2630 if (tb == get_mode_one(smode)) {
2631 /* (L)a * (L)1 = (L)a */
2632 ir_node *blk = get_irn_n(a, -1);
2633 n = new_rd_Conv(get_irn_dbg_info(n), current_ir_graph, blk, a, mode);
2634 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
2637 else if (tb == get_mode_minus_one(smode)) {
2638 /* (L)a * (L)-1 = (L)-a */
2639 ir_node *blk = get_nodes_block(n);
2640 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, blk, a, smode);
2641 n = new_rd_Conv(get_irn_dbg_info(n), current_ir_graph, blk, n, mode);
2642 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2649 * Transform Mul(a,-1) into -a.
2650 * Do constant evaluation of Phi nodes.
2651 * Do architecture dependent optimizations on Mul nodes
2653 static ir_node *transform_node_Mul(ir_node *n) {
2654 ir_node *c, *oldn = n;
2655 ir_mode *mode = get_irn_mode(n);
2656 ir_node *a = get_Mul_left(n);
2657 ir_node *b = get_Mul_right(n);
2659 if (is_Bad(a) || is_Bad(b))
2662 if (mode != get_irn_mode(a))
2663 return transform_node_Mul2n(n, mode);
2665 HANDLE_BINOP_PHI(tarval_mul, a, b, c, mode);
2667 if (mode_is_signed(mode)) {
2670 if (value_of(a) == get_mode_minus_one(mode))
2672 else if (value_of(b) == get_mode_minus_one(mode))
2675 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, get_nodes_block(n), r, mode);
2676 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2681 if (is_Const(b)) { /* (-a) * const -> a * -const */
2682 ir_node *cnst = const_negate(b);
2684 dbg_info *dbgi = get_irn_dbg_info(n);
2685 ir_node *block = get_nodes_block(n);
2686 n = new_rd_Mul(dbgi, current_ir_graph, block, get_Minus_op(a), cnst, mode);
2687 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2690 } else if (is_Minus(b)) { /* (-a) * (-b) -> a * b */
2691 dbg_info *dbgi = get_irn_dbg_info(n);
2692 ir_node *block = get_nodes_block(n);
2693 n = new_rd_Mul(dbgi, current_ir_graph, block, get_Minus_op(a), get_Minus_op(b), mode);
2694 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_MINUS);
2696 } else if (is_Sub(b)) { /* (-a) * (b - c) -> a * (c - b) */
2697 ir_node *sub_l = get_Sub_left(b);
2698 ir_node *sub_r = get_Sub_right(b);
2699 dbg_info *dbgi = get_irn_dbg_info(n);
2700 ir_graph *irg = current_ir_graph;
2701 ir_node *block = get_nodes_block(n);
2702 ir_node *new_b = new_rd_Sub(dbgi, irg, block, sub_r, sub_l, mode);
2703 n = new_rd_Mul(dbgi, irg, block, get_Minus_op(a), new_b, mode);
2704 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS);
2707 } else if (is_Minus(b)) {
2708 if (is_Sub(a)) { /* (a - b) * (-c) -> (b - a) * c */
2709 ir_node *sub_l = get_Sub_left(a);
2710 ir_node *sub_r = get_Sub_right(a);
2711 dbg_info *dbgi = get_irn_dbg_info(n);
2712 ir_graph *irg = current_ir_graph;
2713 ir_node *block = get_nodes_block(n);
2714 ir_node *new_a = new_rd_Sub(dbgi, irg, block, sub_r, sub_l, mode);
2715 n = new_rd_Mul(dbgi, irg, block, new_a, get_Minus_op(b), mode);
2716 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS);
2720 if (get_mode_arithmetic(mode) == irma_ieee754) {
2722 tarval *tv = get_Const_tarval(a);
2723 if (tarval_ieee754_get_exponent(tv) == 1 && tarval_ieee754_zero_mantissa(tv)
2724 && !tarval_is_negative(tv)) {
2725 /* 2.0 * b = b + b */
2726 n = new_rd_Add(get_irn_dbg_info(n), current_ir_graph, get_nodes_block(n), b, b, mode);
2727 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_A_A);
2731 else if (is_Const(b)) {
2732 tarval *tv = get_Const_tarval(b);
2733 if (tarval_ieee754_get_exponent(tv) == 1 && tarval_ieee754_zero_mantissa(tv)
2734 && !tarval_is_negative(tv)) {
2735 /* a * 2.0 = a + a */
2736 n = new_rd_Add(get_irn_dbg_info(n), current_ir_graph, get_nodes_block(n), a, a, mode);
2737 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_A_A);
2742 return arch_dep_replace_mul_with_shifts(n);
2743 } /* transform_node_Mul */
2746 * Transform a Div Node.
2748 static ir_node *transform_node_Div(ir_node *n) {
2749 ir_mode *mode = get_Div_resmode(n);
2750 ir_node *a = get_Div_left(n);
2751 ir_node *b = get_Div_right(n);
2755 if (is_Const(b) && is_const_Phi(a)) {
2756 /* check for Div(Phi, Const) */
2757 value = apply_binop_on_phi(a, get_Const_tarval(b), tarval_div, mode, 0);
2759 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2763 else if (is_Const(a) && is_const_Phi(b)) {
2764 /* check for Div(Const, Phi) */
2765 value = apply_binop_on_phi(b, get_Const_tarval(a), tarval_div, mode, 1);
2767 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2771 else if (is_const_Phi(a) && is_const_Phi(b)) {
2772 /* check for Div(Phi, Phi) */
2773 value = apply_binop_on_2_phis(a, b, tarval_div, mode);
2775 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2782 if (tv != tarval_bad) {
2783 value = new_Const(get_tarval_mode(tv), tv);
2785 DBG_OPT_CSTEVAL(n, value);
2788 ir_node *a = get_Div_left(n);
2789 ir_node *b = get_Div_right(n);
2790 const ir_node *dummy;
2792 if (a == b && value_not_zero(a, &dummy)) {
2793 /* BEWARE: we can optimize a/a to 1 only if this cannot cause a exception */
2794 value = new_Const(mode, get_mode_one(mode));
2795 DBG_OPT_CSTEVAL(n, value);
2798 if (mode_is_signed(mode) && is_Const(b)) {
2799 tarval *tv = get_Const_tarval(b);
2801 if (tv == get_mode_minus_one(mode)) {
2803 value = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, get_nodes_block(n), a, mode);
2804 DBG_OPT_CSTEVAL(n, value);
2808 /* Try architecture dependent optimization */
2809 value = arch_dep_replace_div_by_const(n);
2817 /* Turn Div into a tuple (mem, jmp, bad, value) */
2818 mem = get_Div_mem(n);
2819 blk = get_nodes_block(n);
2821 /* skip a potential Pin */
2822 mem = skip_Pin(mem);
2823 turn_into_tuple(n, pn_Div_max);
2824 set_Tuple_pred(n, pn_Div_M, mem);
2825 set_Tuple_pred(n, pn_Div_X_regular, new_r_Jmp(current_ir_graph, blk));
2826 set_Tuple_pred(n, pn_Div_X_except, new_Bad());
2827 set_Tuple_pred(n, pn_Div_res, value);
2830 } /* transform_node_Div */
2833 * Transform a Mod node.
2835 static ir_node *transform_node_Mod(ir_node *n) {
2836 ir_mode *mode = get_Mod_resmode(n);
2837 ir_node *a = get_Mod_left(n);
2838 ir_node *b = get_Mod_right(n);
2842 if (is_Const(b) && is_const_Phi(a)) {
2843 /* check for Div(Phi, Const) */
2844 value = apply_binop_on_phi(a, get_Const_tarval(b), tarval_mod, mode, 0);
2846 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2850 else if (is_Const(a) && is_const_Phi(b)) {
2851 /* check for Div(Const, Phi) */
2852 value = apply_binop_on_phi(b, get_Const_tarval(a), tarval_mod, mode, 1);
2854 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2858 else if (is_const_Phi(a) && is_const_Phi(b)) {
2859 /* check for Div(Phi, Phi) */
2860 value = apply_binop_on_2_phis(a, b, tarval_mod, mode);
2862 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2869 if (tv != tarval_bad) {
2870 value = new_Const(get_tarval_mode(tv), tv);
2872 DBG_OPT_CSTEVAL(n, value);
2875 ir_node *a = get_Mod_left(n);
2876 ir_node *b = get_Mod_right(n);
2877 const ir_node *dummy;
2879 if (a == b && value_not_zero(a, &dummy)) {
2880 /* BEWARE: we can optimize a%a to 0 only if this cannot cause a exception */
2881 value = new_Const(mode, get_mode_null(mode));
2882 DBG_OPT_CSTEVAL(n, value);
2885 if (mode_is_signed(mode) && is_Const(b)) {
2886 tarval *tv = get_Const_tarval(b);
2888 if (tv == get_mode_minus_one(mode)) {
2890 value = new_Const(mode, get_mode_null(mode));
2891 DBG_OPT_CSTEVAL(n, value);
2895 /* Try architecture dependent optimization */
2896 value = arch_dep_replace_mod_by_const(n);
2904 /* Turn Mod into a tuple (mem, jmp, bad, value) */
2905 mem = get_Mod_mem(n);
2906 blk = get_nodes_block(n);
2908 /* skip a potential Pin */
2909 mem = skip_Pin(mem);
2910 turn_into_tuple(n, pn_Mod_max);
2911 set_Tuple_pred(n, pn_Mod_M, mem);
2912 set_Tuple_pred(n, pn_Mod_X_regular, new_r_Jmp(current_ir_graph, blk));
2913 set_Tuple_pred(n, pn_Mod_X_except, new_Bad());
2914 set_Tuple_pred(n, pn_Mod_res, value);
2917 } /* transform_node_Mod */
2920 * Transform a DivMod node.
2922 static ir_node *transform_node_DivMod(ir_node *n) {
2923 const ir_node *dummy;
2924 ir_node *a = get_DivMod_left(n);
2925 ir_node *b = get_DivMod_right(n);
2926 ir_mode *mode = get_DivMod_resmode(n);
2931 if (is_Const(b) && is_const_Phi(a)) {
2932 /* check for Div(Phi, Const) */
2933 va = apply_binop_on_phi(a, get_Const_tarval(b), tarval_div, mode, 0);
2934 vb = apply_binop_on_phi(a, get_Const_tarval(b), tarval_mod, mode, 0);
2936 DBG_OPT_ALGSIM0(n, va, FS_OPT_CONST_PHI);
2937 DBG_OPT_ALGSIM0(n, vb, FS_OPT_CONST_PHI);
2941 else if (is_Const(a) && is_const_Phi(b)) {
2942 /* check for Div(Const, Phi) */
2943 va = apply_binop_on_phi(b, get_Const_tarval(a), tarval_div, mode, 1);
2944 vb = apply_binop_on_phi(b, get_Const_tarval(a), tarval_mod, mode, 1);
2946 DBG_OPT_ALGSIM0(n, va, FS_OPT_CONST_PHI);
2947 DBG_OPT_ALGSIM0(n, vb, FS_OPT_CONST_PHI);
2951 else if (is_const_Phi(a) && is_const_Phi(b)) {
2952 /* check for Div(Phi, Phi) */
2953 va = apply_binop_on_2_phis(a, b, tarval_div, mode);
2954 vb = apply_binop_on_2_phis(a, b, tarval_mod, mode);
2956 DBG_OPT_ALGSIM0(n, va, FS_OPT_CONST_PHI);
2957 DBG_OPT_ALGSIM0(n, vb, FS_OPT_CONST_PHI);
2964 if (tb != tarval_bad) {
2965 if (tb == get_mode_one(get_tarval_mode(tb))) {
2967 vb = new_Const(mode, get_mode_null(mode));
2968 DBG_OPT_CSTEVAL(n, vb);
2970 } else if (ta != tarval_bad) {
2971 tarval *resa, *resb;
2972 resa = tarval_div(ta, tb);
2973 if (resa == tarval_bad) return n; /* Causes exception!!! Model by replacing through
2974 Jmp for X result!? */
2975 resb = tarval_mod(ta, tb);
2976 if (resb == tarval_bad) return n; /* Causes exception! */
2977 va = new_Const(mode, resa);
2978 vb = new_Const(mode, resb);
2979 DBG_OPT_CSTEVAL(n, va);
2980 DBG_OPT_CSTEVAL(n, vb);
2982 } else if (mode_is_signed(mode) && tb == get_mode_minus_one(mode)) {
2983 va = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, get_nodes_block(n), a, mode);
2984 vb = new_Const(mode, get_mode_null(mode));
2985 DBG_OPT_CSTEVAL(n, va);
2986 DBG_OPT_CSTEVAL(n, vb);
2988 } else { /* Try architecture dependent optimization */
2991 arch_dep_replace_divmod_by_const(&va, &vb, n);
2992 evaluated = va != NULL;
2994 } else if (a == b) {
2995 if (value_not_zero(a, &dummy)) {
2997 va = new_Const(mode, get_mode_one(mode));
2998 vb = new_Const(mode, get_mode_null(mode));
2999 DBG_OPT_CSTEVAL(n, va);
3000 DBG_OPT_CSTEVAL(n, vb);
3003 /* BEWARE: it is NOT possible to optimize a/a to 1, as this may cause a exception */
3006 } else if (ta == get_mode_null(mode) && value_not_zero(b, &dummy)) {
3007 /* 0 / non-Const = 0 */
3012 if (evaluated) { /* replace by tuple */
3016 mem = get_DivMod_mem(n);
3017 /* skip a potential Pin */
3018 mem = skip_Pin(mem);
3020 blk = get_nodes_block(n);
3021 turn_into_tuple(n, pn_DivMod_max);
3022 set_Tuple_pred(n, pn_DivMod_M, mem);
3023 set_Tuple_pred(n, pn_DivMod_X_regular, new_r_Jmp(current_ir_graph, blk));
3024 set_Tuple_pred(n, pn_DivMod_X_except, new_Bad()); /* no exception */
3025 set_Tuple_pred(n, pn_DivMod_res_div, va);
3026 set_Tuple_pred(n, pn_DivMod_res_mod, vb);
3030 } /* transform_node_DivMod */
3033 * Optimize x / c to x * (1/c)
3035 static ir_node *transform_node_Quot(ir_node *n) {
3036 ir_mode *mode = get_Quot_resmode(n);
3039 if (get_mode_arithmetic(mode) == irma_ieee754) {
3040 ir_node *b = get_Quot_right(n);
3041 tarval *tv = value_of(b);
3043 if (tv != tarval_bad) {
3047 * Floating point constant folding might be disabled here to
3049 * However, as we check for exact result, doing it is safe.
3052 rem = tarval_enable_fp_ops(1);
3053 tv = tarval_quo(get_mode_one(mode), tv);
3054 (void)tarval_enable_fp_ops(rem);
3056 /* Do the transformation if the result is either exact or we are not
3057 using strict rules. */
3058 if (tv != tarval_bad &&
3059 (tarval_ieee754_get_exact() || (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic) == 0)) {
3060 ir_node *blk = get_nodes_block(n);
3061 ir_node *c = new_r_Const(current_ir_graph, blk, mode, tv);
3062 ir_node *a = get_Quot_left(n);
3063 ir_node *m = new_rd_Mul(get_irn_dbg_info(n), current_ir_graph, blk, a, c, mode);
3064 ir_node *mem = get_Quot_mem(n);
3066 /* skip a potential Pin */
3067 mem = skip_Pin(mem);
3068 turn_into_tuple(n, pn_Quot_max);
3069 set_Tuple_pred(n, pn_Quot_M, mem);
3070 set_Tuple_pred(n, pn_Quot_X_regular, new_r_Jmp(current_ir_graph, blk));
3071 set_Tuple_pred(n, pn_Quot_X_except, new_r_Bad(current_ir_graph));
3072 set_Tuple_pred(n, pn_Quot_res, m);
3073 DBG_OPT_ALGSIM1(oldn, a, b, m, FS_OPT_FP_INV_MUL);
3078 } /* transform_node_Quot */
3081 * Optimize Abs(x) into x if x is Confirmed >= 0
3082 * Optimize Abs(x) into -x if x is Confirmed <= 0
3083 * Optimize Abs(-x) int Abs(x)
3085 static ir_node *transform_node_Abs(ir_node *n) {
3086 ir_node *c, *oldn = n;
3087 ir_node *a = get_Abs_op(n);
3090 HANDLE_UNOP_PHI(tarval_abs, a, c);
3092 switch (classify_value_sign(a)) {
3093 case value_classified_negative:
3094 mode = get_irn_mode(n);
3097 * We can replace the Abs by -x here.
3098 * We even could add a new Confirm here
3099 * (if not twos complement)
3101 * Note that -x would create a new node, so we could
3102 * not run it in the equivalent_node() context.
3104 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph,
3105 get_nodes_block(n), a, mode);
3107 DBG_OPT_CONFIRM(oldn, n);
3109 case value_classified_positive:
3110 /* n is positive, Abs is not needed */
3113 DBG_OPT_CONFIRM(oldn, n);
3119 /* Abs(-x) = Abs(x) */
3120 mode = get_irn_mode(n);
3121 n = new_rd_Abs(get_irn_dbg_info(n), current_ir_graph,
3122 get_nodes_block(n), get_Minus_op(a), mode);
3123 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ABS_MINUS_X);
3127 } /* transform_node_Abs */
3130 * Optimize -a CMP -b into b CMP a.
3131 * This works only for for modes where unary Minus
3133 * Note that two-complement integers can Overflow
3134 * so it will NOT work.
3136 * For == and != can be handled in Proj(Cmp)
3138 static ir_node *transform_node_Cmp(ir_node *n) {
3140 ir_node *left = get_Cmp_left(n);
3141 ir_node *right = get_Cmp_right(n);
3143 if (is_Minus(left) && is_Minus(right) &&
3144 !mode_overflow_on_unary_Minus(get_irn_mode(left))) {
3145 ir_node *const new_left = get_Minus_op(right);
3146 ir_node *const new_right = get_Minus_op(left);
3147 n = new_rd_Cmp(get_irn_dbg_info(n), current_ir_graph,
3148 get_nodes_block(n), new_left, new_right);
3149 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CMP_OP_OP);
3152 } /* transform_node_Cmp */
3156 * Transform a Cond node.
3158 * Replace the Cond by a Jmp if it branches on a constant
3161 static ir_node *transform_node_Cond(ir_node *n) {
3164 ir_node *a = get_Cond_selector(n);
3165 tarval *ta = value_of(a);
3167 /* we need block info which is not available in floating irgs */
3168 if (get_irg_pinned(current_ir_graph) == op_pin_state_floats)
3171 if ((ta != tarval_bad) &&
3172 (get_irn_mode(a) == mode_b) &&
3173 (get_opt_unreachable_code())) {
3174 /* It's a boolean Cond, branching on a boolean constant.
3175 Replace it by a tuple (Bad, Jmp) or (Jmp, Bad) */
3176 ir_node *blk = get_nodes_block(n);
3177 jmp = new_r_Jmp(current_ir_graph, blk);
3178 turn_into_tuple(n, pn_Cond_max);
3179 if (ta == tarval_b_true) {
3180 set_Tuple_pred(n, pn_Cond_false, new_Bad());
3181 set_Tuple_pred(n, pn_Cond_true, jmp);
3183 set_Tuple_pred(n, pn_Cond_false, jmp);
3184 set_Tuple_pred(n, pn_Cond_true, new_Bad());
3186 /* We might generate an endless loop, so keep it alive. */
3187 add_End_keepalive(get_irg_end(current_ir_graph), blk);
3190 } /* transform_node_Cond */
3193 * Prototype of a recursive transform function
3194 * for bitwise distributive transformations.
3196 typedef ir_node* (*recursive_transform)(ir_node *n);
3199 * makes use of distributive laws for and, or, eor
3200 * and(a OP c, b OP c) -> and(a, b) OP c
3201 * note, might return a different op than n
3203 static ir_node *transform_bitwise_distributive(ir_node *n,
3204 recursive_transform trans_func)
3207 ir_node *a = get_binop_left(n);
3208 ir_node *b = get_binop_right(n);
3209 ir_op *op = get_irn_op(a);
3210 ir_op *op_root = get_irn_op(n);
3212 if(op != get_irn_op(b))
3215 if (op == op_Conv) {
3216 ir_node *a_op = get_Conv_op(a);
3217 ir_node *b_op = get_Conv_op(b);
3218 ir_mode *a_mode = get_irn_mode(a_op);
3219 ir_mode *b_mode = get_irn_mode(b_op);
3220 if(a_mode == b_mode && (mode_is_int(a_mode) || a_mode == mode_b)) {
3221 ir_node *blk = get_nodes_block(n);
3224 set_binop_left(n, a_op);
3225 set_binop_right(n, b_op);
3226 set_irn_mode(n, a_mode);
3228 n = new_r_Conv(current_ir_graph, blk, n, get_irn_mode(oldn));
3230 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_SHIFT_AND);
3236 /* nothing to gain here */
3240 if (op == op_Shrs || op == op_Shr || op == op_Shl
3241 || op == op_And || op == op_Or || op == op_Eor) {
3242 ir_node *a_left = get_binop_left(a);
3243 ir_node *a_right = get_binop_right(a);
3244 ir_node *b_left = get_binop_left(b);
3245 ir_node *b_right = get_binop_right(b);
3247 ir_node *op1 = NULL;
3248 ir_node *op2 = NULL;
3250 if (is_op_commutative(op)) {
3251 if (a_left == b_left) {
3255 } else if(a_left == b_right) {
3259 } else if(a_right == b_left) {
3265 if(a_right == b_right) {
3272 /* (a sop c) & (b sop c) => (a & b) sop c */
3273 ir_node *blk = get_nodes_block(n);
3275 ir_node *new_n = exact_copy(n);
3276 set_binop_left(new_n, op1);
3277 set_binop_right(new_n, op2);
3278 new_n = trans_func(new_n);
3280 if(op_root == op_Eor && op == op_Or) {
3281 dbg_info *dbgi = get_irn_dbg_info(n);
3282 ir_graph *irg = current_ir_graph;
3283 ir_mode *mode = get_irn_mode(c);
3285 c = new_rd_Not(dbgi, irg, blk, c, mode);
3286 n = new_rd_And(dbgi, irg, blk, new_n, c, mode);
3289 set_nodes_block(n, blk);
3290 set_binop_left(n, new_n);
3291 set_binop_right(n, c);
3292 add_identities(current_ir_graph->value_table, n);
3295 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_SHIFT_AND);
3306 static ir_node *transform_node_And(ir_node *n) {
3307 ir_node *c, *oldn = n;
3308 ir_node *a = get_And_left(n);
3309 ir_node *b = get_And_right(n);
3312 mode = get_irn_mode(n);
3313 HANDLE_BINOP_PHI(tarval_and, a, b, c, mode);
3315 /* we can evaluate 2 Projs of the same Cmp */
3316 if (mode == mode_b && is_Proj(a) && is_Proj(b)) {
3317 ir_node *pred_a = get_Proj_pred(a);
3318 ir_node *pred_b = get_Proj_pred(b);
3319 if (pred_a == pred_b) {
3320 dbg_info *dbgi = get_irn_dbg_info(n);
3321 ir_node *block = get_nodes_block(pred_a);
3322 pn_Cmp pn_a = get_Proj_proj(a);
3323 pn_Cmp pn_b = get_Proj_proj(b);
3324 /* yes, we can simply calculate with pncs */
3325 pn_Cmp new_pnc = pn_a & pn_b;
3327 return new_rd_Proj(dbgi, current_ir_graph, block, pred_a, mode_b, new_pnc);
3332 ir_node *op = get_Not_op(b);
3334 ir_node *ba = get_And_left(op);
3335 ir_node *bb = get_And_right(op);
3337 /* it's enough to test the following cases due to normalization! */
3338 if (get_Or_left(a) == ba && get_Or_right(a) == bb) {
3339 /* (a|b) & ~(a&b) = a^b */
3340 ir_node *block = get_nodes_block(n);
3342 n = new_rd_Eor(get_irn_dbg_info(n), current_ir_graph, block, ba, bb, mode);
3343 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_TO_EOR);
3351 ir_node *op = get_Not_op(a);
3353 ir_node *aa = get_And_left(op);
3354 ir_node *ab = get_And_right(op);
3356 /* it's enough to test the following cases due to normalization! */
3357 if (get_Or_left(b) == aa && get_Or_right(b) == ab) {
3358 /* (a|b) & ~(a&b) = a^b */
3359 ir_node *block = get_nodes_block(n);
3361 n = new_rd_Eor(get_irn_dbg_info(n), current_ir_graph, block, aa, ab, mode);
3362 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_TO_EOR);
3369 ir_node *al = get_Eor_left(a);
3370 ir_node *ar = get_Eor_right(a);
3373 /* (b ^ a) & b -> ~a & b */
3374 dbg_info *dbg = get_irn_dbg_info(n);
3375 ir_node *block = get_nodes_block(n);
3377 ar = new_rd_Not(dbg, current_ir_graph, block, ar, mode);
3378 n = new_rd_And(dbg, current_ir_graph, block, ar, b, mode);
3379 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3383 /* (a ^ b) & b -> ~a & b */
3384 dbg_info *dbg = get_irn_dbg_info(n);
3385 ir_node *block = get_nodes_block(n);
3387 al = new_rd_Not(dbg, current_ir_graph, block, al, mode);
3388 n = new_rd_And(dbg, current_ir_graph, block, al, b, mode);
3389 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3394 ir_node *bl = get_Eor_left(b);
3395 ir_node *br = get_Eor_right(b);
3398 /* a & (a ^ b) -> a & ~b */
3399 dbg_info *dbg = get_irn_dbg_info(n);
3400 ir_node *block = get_nodes_block(n);
3402 br = new_rd_Not(dbg, current_ir_graph, block, br, mode);
3403 n = new_rd_And(dbg, current_ir_graph, block, br, a, mode);
3404 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3408 /* a & (b ^ a) -> a & ~b */
3409 dbg_info *dbg = get_irn_dbg_info(n);
3410 ir_node *block = get_nodes_block(n);
3412 bl = new_rd_Not(dbg, current_ir_graph, block, bl, mode);
3413 n = new_rd_And(dbg, current_ir_graph, block, bl, a, mode);
3414 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3418 if (is_Not(a) && is_Not(b)) {
3419 /* ~a & ~b = ~(a|b) */
3420 ir_node *block = get_nodes_block(n);
3421 ir_mode *mode = get_irn_mode(n);
3425 n = new_rd_Or(get_irn_dbg_info(n), current_ir_graph, block, a, b, mode);
3426 n = new_rd_Not(get_irn_dbg_info(n), current_ir_graph, block, n, mode);
3427 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_DEMORGAN);
3431 n = transform_bitwise_distributive(n, transform_node_And);
3434 } /* transform_node_And */
3439 static ir_node *transform_node_Eor(ir_node *n) {
3440 ir_node *c, *oldn = n;
3441 ir_node *a = get_Eor_left(n);
3442 ir_node *b = get_Eor_right(n);
3443 ir_mode *mode = get_irn_mode(n);
3445 HANDLE_BINOP_PHI(tarval_eor, a, b, c, mode);
3447 /* we can evaluate 2 Projs of the same Cmp */
3448 if (mode == mode_b && is_Proj(a) && is_Proj(b)) {
3449 ir_node *pred_a = get_Proj_pred(a);
3450 ir_node *pred_b = get_Proj_pred(b);
3451 if(pred_a == pred_b) {
3452 dbg_info *dbgi = get_irn_dbg_info(n);
3453 ir_node *block = get_nodes_block(pred_a);
3454 pn_Cmp pn_a = get_Proj_proj(a);
3455 pn_Cmp pn_b = get_Proj_proj(b);
3456 /* yes, we can simply calculate with pncs */
3457 pn_Cmp new_pnc = pn_a ^ pn_b;
3459 return new_rd_Proj(dbgi, current_ir_graph, block, pred_a, mode_b,
3466 n = new_rd_Const(get_irn_dbg_info(n), current_ir_graph, get_nodes_block(n),
3467 mode, get_mode_null(mode));
3468 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_A_A);
3469 } else if (mode == mode_b &&
3471 is_Const(b) && is_Const_one(b) &&
3472 is_Cmp(get_Proj_pred(a))) {
3473 /* The Eor negates a Cmp. The Cmp has the negated result anyways! */
3474 n = new_r_Proj(current_ir_graph, get_nodes_block(n), get_Proj_pred(a),
3475 mode_b, get_negated_pnc(get_Proj_proj(a), mode));
3477 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT_BOOL);
3478 } else if (is_Const(b)) {
3479 if (is_Not(a)) { /* ~x ^ const -> x ^ ~const */
3480 ir_node *cnst = new_Const(mode, tarval_not(get_Const_tarval(b)));
3481 ir_node *not_op = get_Not_op(a);
3482 dbg_info *dbg = get_irn_dbg_info(n);
3483 ir_graph *irg = current_ir_graph;
3484 ir_node *block = get_nodes_block(n);
3485 ir_mode *mode = get_irn_mode(n);
3486 n = new_rd_Eor(dbg, irg, block, not_op, cnst, mode);
3488 } else if (is_Const_all_one(b)) { /* x ^ 1...1 -> ~1 */
3489 n = new_r_Not(current_ir_graph, get_nodes_block(n), a, mode);
3490 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3493 n = transform_bitwise_distributive(n, transform_node_Eor);
3497 } /* transform_node_Eor */
3502 static ir_node *transform_node_Not(ir_node *n) {
3503 ir_node *c, *oldn = n;
3504 ir_node *a = get_Not_op(n);
3505 ir_mode *mode = get_irn_mode(n);
3507 HANDLE_UNOP_PHI(tarval_not,a,c);
3509 /* check for a boolean Not */
3510 if (mode == mode_b &&
3512 is_Cmp(get_Proj_pred(a))) {
3513 /* We negate a Cmp. The Cmp has the negated result anyways! */
3514 n = new_r_Proj(current_ir_graph, get_nodes_block(n), get_Proj_pred(a),
3515 mode_b, get_negated_pnc(get_Proj_proj(a), mode_b));
3516 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_CMP);
3520 ir_node *eor_b = get_Eor_right(a);
3521 if (is_Const(eor_b)) { /* ~(x ^ const) -> x ^ ~const */
3522 ir_node *cnst = new_Const(mode, tarval_not(get_Const_tarval(eor_b)));
3523 ir_node *eor_a = get_Eor_left(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, eor_a, cnst, mode);
3532 if (get_mode_arithmetic(mode) == irma_twos_complement) {
3533 if (is_Minus(a)) { /* ~-x -> x + -1 */
3534 dbg_info *dbg = get_irn_dbg_info(n);
3535 ir_graph *irg = current_ir_graph;
3536 ir_node *block = get_nodes_block(n);
3537 ir_node *add_l = get_Minus_op(a);
3538 ir_node *add_r = new_rd_Const(dbg, irg, block, mode, get_mode_minus_one(mode));
3539 n = new_rd_Add(dbg, irg, block, add_l, add_r, mode);
3540 } else if (is_Add(a)) {
3541 ir_node *add_r = get_Add_right(a);
3542 if (is_Const(add_r) && is_Const_all_one(add_r)) {
3543 /* ~(x + -1) = -x */
3544 ir_node *op = get_Add_left(a);
3545 ir_node *blk = get_nodes_block(n);
3546 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, blk, op, get_irn_mode(n));
3547 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_MINUS_1);
3552 } /* transform_node_Not */
3555 * Transform a Minus.
3559 * -(a >>u (size-1)) = a >>s (size-1)
3560 * -(a >>s (size-1)) = a >>u (size-1)
3561 * -(a * const) -> a * -const
3563 static ir_node *transform_node_Minus(ir_node *n) {
3564 ir_node *c, *oldn = n;
3565 ir_node *a = get_Minus_op(n);
3568 HANDLE_UNOP_PHI(tarval_neg,a,c);
3570 mode = get_irn_mode(a);
3571 if (get_mode_arithmetic(mode) == irma_twos_complement) {
3572 /* the following rules are only to twos-complement */
3575 ir_node *op = get_Not_op(a);
3576 tarval *tv = get_mode_one(mode);
3577 ir_node *blk = get_nodes_block(n);
3578 ir_node *c = new_r_Const(current_ir_graph, blk, mode, tv);
3579 n = new_rd_Add(get_irn_dbg_info(n), current_ir_graph, blk, op, c, mode);
3580 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_MINUS_NOT);
3584 ir_node *c = get_Shr_right(a);
3587 tarval *tv = get_Const_tarval(c);
3589 if (tarval_is_long(tv) && get_tarval_long(tv) == (int) get_mode_size_bits(mode) - 1) {
3590 /* -(a >>u (size-1)) = a >>s (size-1) */
3591 ir_node *v = get_Shr_left(a);
3593 n = new_rd_Shrs(get_irn_dbg_info(n), current_ir_graph, get_nodes_block(n), v, c, mode);
3594 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_PREDICATE);
3600 ir_node *c = get_Shrs_right(a);
3603 tarval *tv = get_Const_tarval(c);
3605 if (tarval_is_long(tv) && get_tarval_long(tv) == (int) get_mode_size_bits(mode) - 1) {
3606 /* -(a >>s (size-1)) = a >>u (size-1) */
3607 ir_node *v = get_Shrs_left(a);
3609 n = new_rd_Shr(get_irn_dbg_info(n), current_ir_graph, get_nodes_block(n), v, c, mode);
3610 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_PREDICATE);
3617 /* - (a-b) = b - a */
3618 ir_node *la = get_Sub_left(a);
3619 ir_node *ra = get_Sub_right(a);
3620 ir_node *blk = get_nodes_block(n);
3622 n = new_rd_Sub(get_irn_dbg_info(n), current_ir_graph, blk, ra, la, mode);
3623 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_MINUS_SUB);
3627 if (is_Mul(a)) { /* -(a * const) -> a * -const */
3628 ir_node *mul_l = get_Mul_left(a);
3629 ir_node *mul_r = get_Mul_right(a);
3630 tarval *tv = value_of(mul_r);
3631 if (tv != tarval_bad) {
3632 tv = tarval_neg(tv);
3633 if (tv != tarval_bad) {
3634 ir_node *cnst = new_Const(mode, tv);
3635 dbg_info *dbg = get_irn_dbg_info(a);
3636 ir_graph *irg = current_ir_graph;
3637 ir_node *block = get_nodes_block(a);
3638 n = new_rd_Mul(dbg, irg, block, mul_l, cnst, mode);
3639 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_MINUS_MUL_C);
3646 } /* transform_node_Minus */
3649 * Transform a Cast_type(Const) into a new Const_type
3651 static ir_node *transform_node_Cast(ir_node *n) {
3653 ir_node *pred = get_Cast_op(n);
3654 ir_type *tp = get_irn_type(n);
3656 if (is_Const(pred) && get_Const_type(pred) != tp) {
3657 n = new_rd_Const_type(NULL, current_ir_graph, get_irn_n(pred, -1), get_irn_mode(pred),
3658 get_Const_tarval(pred), tp);
3659 DBG_OPT_CSTEVAL(oldn, n);
3660 } else if (is_SymConst(pred) && get_SymConst_value_type(pred) != tp) {
3661 n = new_rd_SymConst_type(NULL, current_ir_graph, get_irn_n(pred, -1), get_irn_mode(pred),
3662 get_SymConst_symbol(pred), get_SymConst_kind(pred), tp);
3663 DBG_OPT_CSTEVAL(oldn, n);
3667 } /* transform_node_Cast */
3670 * Transform a Proj(Load) with a non-null address.
3672 static ir_node *transform_node_Proj_Load(ir_node *proj) {
3673 if (get_opt_ldst_only_null_ptr_exceptions()) {
3674 if (get_irn_mode(proj) == mode_X) {
3675 ir_node *load = get_Proj_pred(proj);
3677 /* get the Load address */
3678 const ir_node *addr = get_Load_ptr(load);
3679 const ir_node *confirm;
3681 if (value_not_null(addr, &confirm)) {
3682 if (confirm == NULL) {
3683 /* this node may float if it did not depend on a Confirm */
3684 set_irn_pinned(load, op_pin_state_floats);
3686 if (get_Proj_proj(proj) == pn_Load_X_except) {
3687 DBG_OPT_EXC_REM(proj);
3688 return get_irg_bad(current_ir_graph);
3690 ir_node *blk = get_nodes_block(load);
3691 return new_r_Jmp(current_ir_graph, blk);
3697 } /* transform_node_Proj_Load */
3700 * Transform a Proj(Store) with a non-null address.
3702 static ir_node *transform_node_Proj_Store(ir_node *proj) {
3703 if (get_opt_ldst_only_null_ptr_exceptions()) {
3704 if (get_irn_mode(proj) == mode_X) {
3705 ir_node *store = get_Proj_pred(proj);
3707 /* get the load/store address */
3708 const ir_node *addr = get_Store_ptr(store);
3709 const ir_node *confirm;
3711 if (value_not_null(addr, &confirm)) {
3712 if (confirm == NULL) {
3713 /* this node may float if it did not depend on a Confirm */
3714 set_irn_pinned(store, op_pin_state_floats);
3716 if (get_Proj_proj(proj) == pn_Store_X_except) {
3717 DBG_OPT_EXC_REM(proj);
3718 return get_irg_bad(current_ir_graph);
3720 ir_node *blk = get_nodes_block(store);
3721 return new_r_Jmp(current_ir_graph, blk);
3727 } /* transform_node_Proj_Store */
3730 * Transform a Proj(Div) with a non-zero value.
3731 * Removes the exceptions and routes the memory to the NoMem node.
3733 static ir_node *transform_node_Proj_Div(ir_node *proj) {
3734 ir_node *div = get_Proj_pred(proj);
3735 ir_node *b = get_Div_right(div);
3736 ir_node *res, *new_mem;
3737 const ir_node *confirm;
3740 if (value_not_zero(b, &confirm)) {
3741 /* div(x, y) && y != 0 */
3742 if (confirm == NULL) {
3743 /* we are sure we have a Const != 0 */
3744 new_mem = get_Div_mem(div);
3745 new_mem = skip_Pin(new_mem);
3746 set_Div_mem(div, new_mem);
3747 set_irn_pinned(div, op_pin_state_floats);
3750 proj_nr = get_Proj_proj(proj);
3752 case pn_Div_X_regular:
3753 return new_r_Jmp(current_ir_graph, get_irn_n(div, -1));
3755 case pn_Div_X_except:
3756 /* we found an exception handler, remove it */
3757 DBG_OPT_EXC_REM(proj);
3761 res = get_Div_mem(div);
3762 new_mem = get_irg_no_mem(current_ir_graph);
3765 /* This node can only float up to the Confirm block */
3766 new_mem = new_r_Pin(current_ir_graph, get_nodes_block(confirm), new_mem);
3768 set_irn_pinned(div, op_pin_state_floats);
3769 /* this is a Div without exception, we can remove the memory edge */
3770 set_Div_mem(div, new_mem);
3775 } /* transform_node_Proj_Div */
3778 * Transform a Proj(Mod) with a non-zero value.
3779 * Removes the exceptions and routes the memory to the NoMem node.
3781 static ir_node *transform_node_Proj_Mod(ir_node *proj) {
3782 ir_node *mod = get_Proj_pred(proj);
3783 ir_node *b = get_Mod_right(mod);
3784 ir_node *res, *new_mem;
3785 const ir_node *confirm;
3788 if (value_not_zero(b, &confirm)) {
3789 /* mod(x, y) && y != 0 */
3790 proj_nr = get_Proj_proj(proj);
3792 if (confirm == NULL) {
3793 /* we are sure we have a Const != 0 */
3794 new_mem = get_Mod_mem(mod);
3795 new_mem = skip_Pin(new_mem);
3796 set_Mod_mem(mod, new_mem);
3797 set_irn_pinned(mod, op_pin_state_floats);
3802 case pn_Mod_X_regular:
3803 return new_r_Jmp(current_ir_graph, get_irn_n(mod, -1));
3805 case pn_Mod_X_except:
3806 /* we found an exception handler, remove it */
3807 DBG_OPT_EXC_REM(proj);
3811 res = get_Mod_mem(mod);
3812 new_mem = get_irg_no_mem(current_ir_graph);
3815 /* This node can only float up to the Confirm block */
3816 new_mem = new_r_Pin(current_ir_graph, get_nodes_block(confirm), new_mem);
3818 /* this is a Mod without exception, we can remove the memory edge */
3819 set_Mod_mem(mod, new_mem);
3822 if (get_Mod_left(mod) == b) {
3823 /* a % a = 0 if a != 0 */
3824 ir_mode *mode = get_irn_mode(proj);
3825 ir_node *res = new_Const(mode, get_mode_null(mode));
3827 DBG_OPT_CSTEVAL(mod, res);
3833 } /* transform_node_Proj_Mod */
3836 * Transform a Proj(DivMod) with a non-zero value.
3837 * Removes the exceptions and routes the memory to the NoMem node.
3839 static ir_node *transform_node_Proj_DivMod(ir_node *proj) {
3840 ir_node *divmod = get_Proj_pred(proj);
3841 ir_node *b = get_DivMod_right(divmod);
3842 ir_node *res, *new_mem;
3843 const ir_node *confirm;
3846 if (value_not_zero(b, &confirm)) {
3847 /* DivMod(x, y) && y != 0 */
3848 proj_nr = get_Proj_proj(proj);
3850 if (confirm == NULL) {
3851 /* we are sure we have a Const != 0 */
3852 new_mem = get_DivMod_mem(divmod);
3853 new_mem = skip_Pin(new_mem);
3854 set_DivMod_mem(divmod, new_mem);
3855 set_irn_pinned(divmod, op_pin_state_floats);
3860 case pn_DivMod_X_regular:
3861 return new_r_Jmp(current_ir_graph, get_irn_n(divmod, -1));
3863 case pn_DivMod_X_except:
3864 /* we found an exception handler, remove it */
3865 DBG_OPT_EXC_REM(proj);
3869 res = get_DivMod_mem(divmod);
3870 new_mem = get_irg_no_mem(current_ir_graph);
3873 /* This node can only float up to the Confirm block */
3874 new_mem = new_r_Pin(current_ir_graph, get_nodes_block(confirm), new_mem);
3876 /* this is a DivMod without exception, we can remove the memory edge */
3877 set_DivMod_mem(divmod, new_mem);
3880 case pn_DivMod_res_mod:
3881 if (get_DivMod_left(divmod) == b) {
3882 /* a % a = 0 if a != 0 */
3883 ir_mode *mode = get_irn_mode(proj);
3884 ir_node *res = new_Const(mode, get_mode_null(mode));
3886 DBG_OPT_CSTEVAL(divmod, res);
3892 } /* transform_node_Proj_DivMod */
3895 * Optimizes jump tables (CondIs or CondIu) by removing all impossible cases.
3897 static ir_node *transform_node_Proj_Cond(ir_node *proj) {
3898 if (get_opt_unreachable_code()) {
3899 ir_node *n = get_Proj_pred(proj);
3900 ir_node *b = get_Cond_selector(n);
3902 if (mode_is_int(get_irn_mode(b))) {
3903 tarval *tb = value_of(b);
3905 if (tb != tarval_bad) {
3906 /* we have a constant switch */
3907 long num = get_Proj_proj(proj);
3909 if (num != get_Cond_defaultProj(n)) { /* we cannot optimize default Proj's yet */
3910 if (get_tarval_long(tb) == num) {
3911 /* Do NOT create a jump here, or we will have 2 control flow ops
3912 * in a block. This case is optimized away in optimize_cf(). */
3915 /* this case will NEVER be taken, kill it */
3916 return get_irg_bad(current_ir_graph);
3923 } /* transform_node_Proj_Cond */
3926 * Create a 0 constant of given mode.
3928 static ir_node *create_zero_const(ir_mode *mode) {
3929 tarval *tv = get_mode_null(mode);
3930 ir_node *cnst = new_Const(mode, tv);
3935 /* the order of the values is important! */
3936 typedef enum const_class {
3942 static const_class classify_const(const ir_node* n)
3944 if (is_Const(n)) return const_const;
3945 if (is_irn_constlike(n)) return const_like;
3950 * Determines whether r is more constlike or has a larger index (in that order)
3953 static int operands_are_normalized(const ir_node *l, const ir_node *r)
3955 const const_class l_order = classify_const(l);
3956 const const_class r_order = classify_const(r);
3958 l_order > r_order ||
3959 (l_order == r_order && get_irn_idx(l) <= get_irn_idx(r));
3963 * Normalizes and optimizes Cmp nodes.
3965 static ir_node *transform_node_Proj_Cmp(ir_node *proj) {
3966 ir_node *n = get_Proj_pred(proj);
3967 ir_node *left = get_Cmp_left(n);
3968 ir_node *right = get_Cmp_right(n);
3971 ir_mode *mode = NULL;
3972 long proj_nr = get_Proj_proj(proj);
3974 /* we can evaluate some cases directly */
3977 return new_Const(mode_b, get_tarval_b_false());
3979 return new_Const(mode_b, get_tarval_b_true());
3981 if (!mode_is_float(get_irn_mode(left)))
3982 return new_Const(mode_b, get_tarval_b_true());
3988 /* remove Casts of both sides */
3989 left = skip_Cast(left);
3990 right = skip_Cast(right);
3992 /* Remove unnecessary conversions */
3993 /* TODO handle constants */
3994 if (is_Conv(left) && is_Conv(right)) {
3995 ir_mode *mode = get_irn_mode(left);
3996 ir_node *op_left = get_Conv_op(left);
3997 ir_node *op_right = get_Conv_op(right);
3998 ir_mode *mode_left = get_irn_mode(op_left);
3999 ir_mode *mode_right = get_irn_mode(op_right);
4001 if (smaller_mode(mode_left, mode) && smaller_mode(mode_right, mode)
4002 && mode_left != mode_b && mode_right != mode_b) {
4003 ir_graph *irg = current_ir_graph;
4004 ir_node *block = get_nodes_block(n);
4006 if (mode_left == mode_right) {
4010 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV_CONV);
4011 } else if (smaller_mode(mode_left, mode_right)) {
4012 left = new_r_Conv(irg, block, op_left, mode_right);
4015 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV);
4016 } else if (smaller_mode(mode_right, mode_left)) {
4018 right = new_r_Conv(irg, block, op_right, mode_left);
4020 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV);
4025 /* remove operation on both sides if possible */
4026 if (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) {
4028 * The following operations are NOT safe for floating point operations, for instance
4029 * 1.0 + inf == 2.0 + inf, =/=> x == y
4031 if (mode_is_int(get_irn_mode(left))) {
4032 unsigned lop = get_irn_opcode(left);
4034 if (lop == get_irn_opcode(right)) {
4035 ir_node *ll, *lr, *rl, *rr;
4037 /* same operation on both sides, try to remove */
4041 /* ~a CMP ~b => a CMP b, -a CMP -b ==> a CMP b */
4042 left = get_unop_op(left);
4043 right = get_unop_op(right);
4045 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4048 ll = get_Add_left(left);
4049 lr = get_Add_right(left);
4050 rl = get_Add_left(right);
4051 rr = get_Add_right(right);
4054 /* X + a CMP X + b ==> a CMP b */
4058 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4059 } else if (ll == rr) {
4060 /* X + a CMP b + X ==> a CMP b */
4064 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4065 } else if (lr == rl) {
4066 /* a + X CMP X + b ==> a CMP b */
4070 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4071 } else if (lr == rr) {
4072 /* a + X CMP b + X ==> a CMP b */
4076 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4080 ll = get_Sub_left(left);
4081 lr = get_Sub_right(left);
4082 rl = get_Sub_left(right);
4083 rr = get_Sub_right(right);
4086 /* X - a CMP X - b ==> a CMP b */
4090 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4091 } else if (lr == rr) {
4092 /* a - X CMP b - X ==> a CMP b */
4096 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4100 if (get_Rotl_right(left) == get_Rotl_right(right)) {
4101 /* a ROTL X CMP b ROTL X ==> a CMP b */
4102 left = get_Rotl_left(left);
4103 right = get_Rotl_left(right);
4105 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4113 /* X+A == A, A+X == A, A-X == A -> X == 0 */
4114 if (is_Add(left) || is_Sub(left)) {
4115 ir_node *ll = get_binop_left(left);
4116 ir_node *lr = get_binop_right(left);
4118 if (lr == right && is_Add(left)) {
4125 right = create_zero_const(get_irn_mode(left));
4127 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4130 if (is_Add(right) || is_Sub(right)) {
4131 ir_node *rl = get_binop_left(right);
4132 ir_node *rr = get_binop_right(right);
4134 if (rr == left && is_Add(right)) {
4141 right = create_zero_const(get_irn_mode(left));
4143 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4146 } /* mode_is_int(...) */
4147 } /* proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg */
4149 /* replace mode_b compares with ands/ors */
4150 if (get_irn_mode(left) == mode_b) {
4151 ir_graph *irg = current_ir_graph;
4152 ir_node *block = get_nodes_block(n);
4156 case pn_Cmp_Le: bres = new_r_Or( irg, block, new_r_Not(irg, block, left, mode_b), right, mode_b); break;
4157 case pn_Cmp_Lt: bres = new_r_And(irg, block, new_r_Not(irg, block, left, mode_b), right, mode_b); break;
4158 case pn_Cmp_Ge: bres = new_r_Or( irg, block, left, new_r_Not(irg, block, right, mode_b), mode_b); break;
4159 case pn_Cmp_Gt: bres = new_r_And(irg, block, left, new_r_Not(irg, block, right, mode_b), mode_b); break;
4160 case pn_Cmp_Lg: bres = new_r_Eor(irg, block, left, right, mode_b); break;
4161 case pn_Cmp_Eq: bres = new_r_Not(irg, block, new_r_Eor(irg, block, left, right, mode_b), mode_b); break;
4162 default: bres = NULL;
4165 DBG_OPT_ALGSIM0(n, bres, FS_OPT_CMP_TO_BOOL);
4171 * First step: normalize the compare op
4172 * by placing the constant on the right side
4173 * or moving the lower address node to the left.
4175 if (!operands_are_normalized(left, right)) {
4181 proj_nr = get_inversed_pnc(proj_nr);
4186 * Second step: Try to reduce the magnitude
4187 * of a constant. This may help to generate better code
4188 * later and may help to normalize more compares.
4189 * Of course this is only possible for integer values.
4191 tv = value_of(right);
4192 if (tv != tarval_bad) {
4193 mode = get_irn_mode(right);
4195 /* TODO extend to arbitrary constants */
4196 if (is_Conv(left) && tarval_is_null(tv)) {
4197 ir_node *op = get_Conv_op(left);
4198 ir_mode *op_mode = get_irn_mode(op);
4201 * UpConv(x) REL 0 ==> x REL 0
4203 if (get_mode_size_bits(mode) > get_mode_size_bits(op_mode) &&
4204 ((proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) ||
4205 mode_is_signed(mode) || !mode_is_signed(op_mode))) {
4206 tv = get_mode_null(op_mode);
4210 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV);
4214 if (tv != tarval_bad) {
4215 /* the following optimization is possible on modes without Overflow
4216 * on Unary Minus or on == and !=:
4217 * -a CMP c ==> a swap(CMP) -c
4219 * Beware: for two-complement Overflow may occur, so only == and != can
4220 * be optimized, see this:
4221 * -MININT < 0 =/=> MININT > 0 !!!
4223 if (is_Minus(left) &&
4224 (!mode_overflow_on_unary_Minus(mode) ||
4225 (mode_is_int(mode) && (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg)))) {
4226 tv = tarval_neg(tv);
4228 if (tv != tarval_bad) {
4229 left = get_Minus_op(left);
4230 proj_nr = get_inversed_pnc(proj_nr);
4232 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4234 } else if (is_Not(left) && (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg)) {
4235 /* Not(a) ==/!= c ==> a ==/!= Not(c) */
4236 tv = tarval_not(tv);
4238 if (tv != tarval_bad) {
4239 left = get_Not_op(left);
4241 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4245 /* for integer modes, we have more */
4246 if (mode_is_int(mode)) {
4247 /* Ne includes Unordered which is not possible on integers.
4248 * However, frontends often use this wrong, so fix it here */
4249 if (proj_nr & pn_Cmp_Uo) {
4250 proj_nr &= ~pn_Cmp_Uo;
4251 set_Proj_proj(proj, proj_nr);
4254 /* c > 0 : a < c ==> a <= (c-1) a >= c ==> a > (c-1) */
4255 if ((proj_nr == pn_Cmp_Lt || proj_nr == pn_Cmp_Ge) &&
4256 tarval_cmp(tv, get_mode_null(mode)) == pn_Cmp_Gt) {
4257 tv = tarval_sub(tv, get_mode_one(mode), NULL);
4259 if (tv != tarval_bad) {
4260 proj_nr ^= pn_Cmp_Eq;
4262 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CNST_MAGN);
4265 /* c < 0 : a > c ==> a >= (c+1) a <= c ==> a < (c+1) */
4266 else if ((proj_nr == pn_Cmp_Gt || proj_nr == pn_Cmp_Le) &&
4267 tarval_cmp(tv, get_mode_null(mode)) == pn_Cmp_Lt) {
4268 tv = tarval_add(tv, get_mode_one(mode));
4270 if (tv != tarval_bad) {
4271 proj_nr ^= pn_Cmp_Eq;
4273 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CNST_MAGN);
4277 /* the following reassociations work only for == and != */
4278 if (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) {
4280 #if 0 /* Might be not that good in general */
4281 /* a-b == 0 ==> a == b, a-b != 0 ==> a != b */
4282 if (tarval_is_null(tv) && is_Sub(left)) {
4283 right = get_Sub_right(left);
4284 left = get_Sub_left(left);
4286 tv = value_of(right);
4288 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4292 if (tv != tarval_bad) {
4293 /* a-c1 == c2 ==> a == c2+c1, a-c1 != c2 ==> a != c2+c1 */
4295 ir_node *c1 = get_Sub_right(left);
4296 tarval *tv2 = value_of(c1);
4298 if (tv2 != tarval_bad) {
4299 tv2 = tarval_add(tv, value_of(c1));
4301 if (tv2 != tarval_bad) {
4302 left = get_Sub_left(left);
4305 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4309 /* a+c1 == c2 ==> a == c2-c1, a+c1 != c2 ==> a != c2-c1 */
4310 else if (is_Add(left)) {
4311 ir_node *a_l = get_Add_left(left);
4312 ir_node *a_r = get_Add_right(left);
4316 if (is_Const(a_l)) {
4318 tv2 = value_of(a_l);
4321 tv2 = value_of(a_r);
4324 if (tv2 != tarval_bad) {
4325 tv2 = tarval_sub(tv, tv2, NULL);
4327 if (tv2 != tarval_bad) {
4331 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4335 /* -a == c ==> a == -c, -a != c ==> a != -c */
4336 else if (is_Minus(left)) {
4337 tarval *tv2 = tarval_sub(get_mode_null(mode), tv, NULL);
4339 if (tv2 != tarval_bad) {
4340 left = get_Minus_op(left);
4343 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4348 /* the following reassociations work only for <= */
4349 else if (proj_nr == pn_Cmp_Le || proj_nr == pn_Cmp_Lt) {
4350 if (tv != tarval_bad) {
4351 /* c >= 0 : Abs(a) <= c ==> (unsigned)(a + c) <= 2*c */
4352 if (is_Abs(left)) { // TODO something is missing here
4358 if (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) {
4359 switch (get_irn_opcode(left)) {
4363 c1 = get_And_right(left);
4366 * And(x, C1) == C2 ==> FALSE if C2 & C1 != C2
4367 * And(x, C1) != C2 ==> TRUE if C2 & C1 != C2
4369 tarval *mask = tarval_and(get_Const_tarval(c1), tv);
4371 /* TODO: move to constant evaluation */
4372 tv = proj_nr == pn_Cmp_Eq ? get_tarval_b_false() : get_tarval_b_true();
4373 c1 = new_Const(mode_b, tv);
4374 DBG_OPT_CSTEVAL(proj, c1);
4378 if (tarval_is_single_bit(tv)) {
4380 * optimization for AND:
4382 * And(x, C) == C ==> And(x, C) != 0
4383 * And(x, C) != C ==> And(X, C) == 0
4385 * if C is a single Bit constant.
4388 /* check for Constant's match. We have check hare the tarvals,
4389 because our const might be changed */
4390 if (get_Const_tarval(c1) == tv) {
4391 /* fine: do the transformation */
4392 tv = get_mode_null(get_tarval_mode(tv));
4393 proj_nr ^= pn_Cmp_Leg;
4395 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CNST_MAGN);
4401 c1 = get_Or_right(left);
4402 if (is_Const(c1) && tarval_is_null(tv)) {
4404 * Or(x, C) == 0 && C != 0 ==> FALSE
4405 * Or(x, C) != 0 && C != 0 ==> TRUE
4407 if (! tarval_is_null(get_Const_tarval(c1))) {
4408 /* TODO: move to constant evaluation */
4409 tv = proj_nr == pn_Cmp_Eq ? get_tarval_b_false() : get_tarval_b_true();
4410 c1 = new_Const(mode_b, tv);
4411 DBG_OPT_CSTEVAL(proj, c1);
4418 * optimize x << c1 == c into x & (-1 >>u c1) == c >> c1 if c & (-1 << c1) == c
4420 * optimize x << c1 != c into x & (-1 >>u c1) != c >> c1 if c & (-1 << c1) == c
4423 c1 = get_Shl_right(left);
4425 tarval *tv1 = get_Const_tarval(c1);
4426 ir_mode *mode = get_irn_mode(left);
4427 tarval *minus1 = get_mode_all_one(mode);
4428 tarval *amask = tarval_shr(minus1, tv1);
4429 tarval *cmask = tarval_shl(minus1, tv1);
4432 if (tarval_and(tv, cmask) != tv) {
4433 /* condition not met */
4434 tv = proj_nr == pn_Cmp_Eq ? get_tarval_b_false() : get_tarval_b_true();
4435 c1 = new_Const(mode_b, tv);
4436 DBG_OPT_CSTEVAL(proj, c1);
4439 sl = get_Shl_left(left);
4440 blk = get_nodes_block(n);
4441 left = new_rd_And(get_irn_dbg_info(left), current_ir_graph, blk, sl, new_Const(mode, amask), mode);
4442 tv = tarval_shr(tv, tv1);
4444 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_SHF_TO_AND);
4449 * optimize x >>u c1 == c into x & (-1 << c1) == c << c1 if c & (-1 >>u c1) == c
4451 * optimize x >>u c1 != c into x & (-1 << c1) != c << c1 if c & (-1 >>u c1) == c
4454 c1 = get_Shr_right(left);
4456 tarval *tv1 = get_Const_tarval(c1);
4457 ir_mode *mode = get_irn_mode(left);
4458 tarval *minus1 = get_mode_all_one(mode);
4459 tarval *amask = tarval_shl(minus1, tv1);
4460 tarval *cmask = tarval_shr(minus1, tv1);
4463 if (tarval_and(tv, cmask) != tv) {
4464 /* condition not met */
4465 tv = proj_nr == pn_Cmp_Eq ? get_tarval_b_false() : get_tarval_b_true();
4466 c1 = new_Const(mode_b, tv);
4467 DBG_OPT_CSTEVAL(proj, c1);
4470 sl = get_Shr_left(left);
4471 blk = get_nodes_block(n);
4472 left = new_rd_And(get_irn_dbg_info(left), current_ir_graph, blk, sl, new_Const(mode, amask), mode);
4473 tv = tarval_shl(tv, tv1);
4475 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_SHF_TO_AND);
4480 * optimize x >>s c1 == c into x & (-1 << c1) == c << c1 if (c >>s (BITS - c1)) \in {0,-1}
4482 * optimize x >>s c1 != c into x & (-1 << c1) != c << c1 if (c >>s (BITS - c1)) \in {0,-1}
4485 c1 = get_Shrs_right(left);
4487 tarval *tv1 = get_Const_tarval(c1);
4488 ir_mode *mode = get_irn_mode(left);
4489 tarval *minus1 = get_mode_all_one(mode);
4490 tarval *amask = tarval_shl(minus1, tv1);
4491 tarval *cond = new_tarval_from_long(get_mode_size_bits(mode), get_tarval_mode(tv1));
4494 cond = tarval_sub(cond, tv1, NULL);
4495 cond = tarval_shrs(tv, cond);
4497 if (!tarval_is_all_one(cond) && !tarval_is_null(cond)) {
4498 /* condition not met */
4499 tv = proj_nr == pn_Cmp_Eq ? get_tarval_b_false() : get_tarval_b_true();
4500 c1 = new_Const(mode_b, tv);
4501 DBG_OPT_CSTEVAL(proj, c1);
4504 sl = get_Shrs_left(left);
4505 blk = get_nodes_block(n);
4506 left = new_rd_And(get_irn_dbg_info(left), current_ir_graph, blk, sl, new_Const(mode, amask), mode);
4507 tv = tarval_shl(tv, tv1);
4509 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_SHF_TO_AND);
4514 } /* tarval != bad */
4517 if (changed & 2) /* need a new Const */
4518 right = new_Const(mode, tv);
4520 if ((proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) && is_Const(right) && is_Const_null(right) && is_Proj(left)) {
4521 ir_node *op = get_Proj_pred(left);
4523 if ((is_Mod(op) && get_Proj_proj(left) == pn_Mod_res) ||
4524 (is_DivMod(op) && get_Proj_proj(left) == pn_DivMod_res_mod)) {
4525 ir_node *c = get_binop_right(op);
4528 tarval *tv = get_Const_tarval(c);
4530 if (tarval_is_single_bit(tv)) {
4531 /* special case: (x % 2^n) CMP 0 ==> x & (2^n-1) CMP 0 */
4532 ir_node *v = get_binop_left(op);
4533 ir_node *blk = get_irn_n(op, -1);
4534 ir_mode *mode = get_irn_mode(v);
4536 tv = tarval_sub(tv, get_mode_one(mode), NULL);
4537 left = new_rd_And(get_irn_dbg_info(op), current_ir_graph, blk, v, new_Const(mode, tv), mode);
4539 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_MOD_TO_AND);
4546 ir_node *block = get_nodes_block(n);
4548 /* create a new compare */
4549 n = new_rd_Cmp(get_irn_dbg_info(n), current_ir_graph, block, left, right);
4550 proj = new_rd_Proj(get_irn_dbg_info(proj), current_ir_graph, block, n, get_irn_mode(proj), proj_nr);
4554 } /* transform_node_Proj_Cmp */
4557 * Optimize CopyB(mem, x, x) into a Nop.
4559 static ir_node *transform_node_Proj_CopyB(ir_node *proj) {
4560 ir_node *copyb = get_Proj_pred(proj);
4561 ir_node *a = get_CopyB_dst(copyb);
4562 ir_node *b = get_CopyB_src(copyb);
4565 switch (get_Proj_proj(proj)) {
4566 case pn_CopyB_X_regular:
4567 /* Turn CopyB into a tuple (mem, jmp, bad, bad) */
4568 DBG_OPT_EXC_REM(proj);
4569 proj = new_r_Jmp(current_ir_graph, get_nodes_block(copyb));
4571 case pn_CopyB_M_except:
4572 case pn_CopyB_X_except:
4573 DBG_OPT_EXC_REM(proj);
4574 proj = get_irg_bad(current_ir_graph);
4581 } /* transform_node_Proj_CopyB */
4584 * Optimize Bounds(idx, idx, upper) into idx.
4586 static ir_node *transform_node_Proj_Bound(ir_node *proj) {
4587 ir_node *oldn = proj;
4588 ir_node *bound = get_Proj_pred(proj);
4589 ir_node *idx = get_Bound_index(bound);
4590 ir_node *pred = skip_Proj(idx);
4593 if (idx == get_Bound_lower(bound))
4595 else if (is_Bound(pred)) {
4597 * idx was Bounds checked in the same MacroBlock previously,
4598 * it is still valid if lower <= pred_lower && pred_upper <= upper.
4600 ir_node *lower = get_Bound_lower(bound);
4601 ir_node *upper = get_Bound_upper(bound);
4602 if (get_Bound_lower(pred) == lower &&
4603 get_Bound_upper(pred) == upper &&
4604 get_irn_MacroBlock(bound) == get_irn_MacroBlock(pred)) {
4606 * One could expect that we simply return the previous
4607 * Bound here. However, this would be wrong, as we could
4608 * add an exception Proj to a new location then.
4609 * So, we must turn in into a tuple.
4615 /* Turn Bound into a tuple (mem, jmp, bad, idx) */
4616 switch (get_Proj_proj(proj)) {
4618 DBG_OPT_EXC_REM(proj);
4619 proj = get_Bound_mem(bound);
4621 case pn_Bound_X_except:
4622 DBG_OPT_EXC_REM(proj);
4623 proj = get_irg_bad(current_ir_graph);
4627 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NOP);
4629 case pn_Bound_X_regular:
4630 DBG_OPT_EXC_REM(proj);
4631 proj = new_r_Jmp(current_ir_graph, get_nodes_block(bound));
4638 } /* transform_node_Proj_Bound */
4641 * Does all optimizations on nodes that must be done on it's Proj's
4642 * because of creating new nodes.
4644 static ir_node *transform_node_Proj(ir_node *proj) {
4645 ir_node *n = get_Proj_pred(proj);
4647 if (n->op->ops.transform_node_Proj)
4648 return n->op->ops.transform_node_Proj(proj);
4650 } /* transform_node_Proj */
4653 * Move Confirms down through Phi nodes.
4655 static ir_node *transform_node_Phi(ir_node *phi) {
4657 ir_mode *mode = get_irn_mode(phi);
4659 if (mode_is_reference(mode)) {
4660 n = get_irn_arity(phi);
4662 /* Beware of Phi0 */
4664 ir_node *pred = get_irn_n(phi, 0);
4665 ir_node *bound, *new_Phi, *block, **in;
4668 if (! is_Confirm(pred))
4671 bound = get_Confirm_bound(pred);
4672 pnc = get_Confirm_cmp(pred);
4674 NEW_ARR_A(ir_node *, in, n);
4675 in[0] = get_Confirm_value(pred);
4677 for (i = 1; i < n; ++i) {
4678 pred = get_irn_n(phi, i);
4680 if (! is_Confirm(pred) ||
4681 get_Confirm_bound(pred) != bound ||
4682 get_Confirm_cmp(pred) != pnc)
4684 in[i] = get_Confirm_value(pred);
4686 /* move the Confirm nodes "behind" the Phi */
4687 block = get_irn_n(phi, -1);
4688 new_Phi = new_r_Phi(current_ir_graph, block, n, in, get_irn_mode(phi));
4689 return new_r_Confirm(current_ir_graph, block, new_Phi, bound, pnc);
4693 } /* transform_node_Phi */
4696 * Returns the operands of a commutative bin-op, if one operand is
4697 * a const, it is returned as the second one.
4699 static void get_comm_Binop_Ops(ir_node *binop, ir_node **a, ir_node **c) {
4700 ir_node *op_a = get_binop_left(binop);
4701 ir_node *op_b = get_binop_right(binop);
4703 assert(is_op_commutative(get_irn_op(binop)));
4705 if (is_Const(op_a)) {
4712 } /* get_comm_Binop_Ops */
4715 * Optimize a Or(And(Or(And(v,c4),c3),c2),c1) pattern if possible.
4716 * Such pattern may arise in bitfield stores.
4718 * value c4 value c4 & c2
4719 * AND c3 AND c1 | c3
4726 * AND c1 ===> OR if (c1 | c2) == 0x111..11
4729 static ir_node *transform_node_Or_bf_store(ir_node *or) {
4732 ir_node *and_l, *c3;
4733 ir_node *value, *c4;
4734 ir_node *new_and, *new_const, *block;
4735 ir_mode *mode = get_irn_mode(or);
4737 tarval *tv1, *tv2, *tv3, *tv4, *tv, *n_tv4, *n_tv2;
4740 get_comm_Binop_Ops(or, &and, &c1);
4741 if (!is_Const(c1) || !is_And(and))
4744 get_comm_Binop_Ops(and, &or_l, &c2);
4748 tv1 = get_Const_tarval(c1);
4749 tv2 = get_Const_tarval(c2);
4751 tv = tarval_or(tv1, tv2);
4752 if (tarval_is_all_one(tv)) {
4753 /* the AND does NOT clear a bit with isn't set by the OR */
4754 set_Or_left(or, or_l);
4755 set_Or_right(or, c1);
4757 /* check for more */
4764 get_comm_Binop_Ops(or_l, &and_l, &c3);
4765 if (!is_Const(c3) || !is_And(and_l))
4768 get_comm_Binop_Ops(and_l, &value, &c4);
4772 /* ok, found the pattern, check for conditions */
4773 assert(mode == get_irn_mode(and));
4774 assert(mode == get_irn_mode(or_l));
4775 assert(mode == get_irn_mode(and_l));
4777 tv3 = get_Const_tarval(c3);
4778 tv4 = get_Const_tarval(c4);
4780 tv = tarval_or(tv4, tv2);
4781 if (!tarval_is_all_one(tv)) {
4782 /* have at least one 0 at the same bit position */
4786 n_tv4 = tarval_not(tv4);
4787 if (tv3 != tarval_and(tv3, n_tv4)) {
4788 /* bit in the or_mask is outside the and_mask */
4792 n_tv2 = tarval_not(tv2);
4793 if (tv1 != tarval_and(tv1, n_tv2)) {
4794 /* bit in the or_mask is outside the and_mask */
4798 /* ok, all conditions met */
4799 block = get_irn_n(or, -1);
4801 new_and = new_r_And(current_ir_graph, block,
4802 value, new_r_Const(current_ir_graph, block, mode, tarval_and(tv4, tv2)), mode);
4804 new_const = new_r_Const(current_ir_graph, block, mode, tarval_or(tv3, tv1));
4806 set_Or_left(or, new_and);
4807 set_Or_right(or, new_const);
4809 /* check for more */
4811 } /* transform_node_Or_bf_store */
4814 * Optimize an Or(shl(x, c), shr(x, bits - c)) into a Rotl
4816 static ir_node *transform_node_Or_Rotl(ir_node *or) {
4817 ir_mode *mode = get_irn_mode(or);
4818 ir_node *shl, *shr, *block;
4819 ir_node *irn, *x, *c1, *c2, *v, *sub, *n, *rotval;
4822 if (! mode_is_int(mode))
4825 shl = get_binop_left(or);
4826 shr = get_binop_right(or);
4835 } else if (!is_Shl(shl)) {
4837 } else if (!is_Shr(shr)) {
4840 x = get_Shl_left(shl);
4841 if (x != get_Shr_left(shr))
4844 c1 = get_Shl_right(shl);
4845 c2 = get_Shr_right(shr);
4846 if (is_Const(c1) && is_Const(c2)) {
4847 tv1 = get_Const_tarval(c1);
4848 if (! tarval_is_long(tv1))
4851 tv2 = get_Const_tarval(c2);
4852 if (! tarval_is_long(tv2))
4855 if (get_tarval_long(tv1) + get_tarval_long(tv2)
4856 != (int) get_mode_size_bits(mode))
4859 /* yet, condition met */
4860 block = get_nodes_block(or);
4862 n = new_r_Rotl(current_ir_graph, block, x, c1, mode);
4864 DBG_OPT_ALGSIM1(or, shl, shr, n, FS_OPT_OR_SHFT_TO_ROTL);
4871 rotval = sub; /* a Rot right is not supported, so use a rot left */
4872 } else if (is_Sub(c2)) {
4878 if (get_Sub_right(sub) != v)
4881 c1 = get_Sub_left(sub);
4885 tv1 = get_Const_tarval(c1);
4886 if (! tarval_is_long(tv1))
4889 if (get_tarval_long(tv1) != (int) get_mode_size_bits(mode))
4892 /* yet, condition met */
4893 block = get_nodes_block(or);
4895 n = new_r_Rotl(current_ir_graph, block, x, rotval, mode);
4897 DBG_OPT_ALGSIM0(or, n, FS_OPT_OR_SHFT_TO_ROTL);
4899 } /* transform_node_Or_Rotl */
4904 static ir_node *transform_node_Or(ir_node *n) {
4905 ir_node *c, *oldn = n;
4906 ir_node *a = get_Or_left(n);
4907 ir_node *b = get_Or_right(n);
4910 if (is_Not(a) && is_Not(b)) {
4911 /* ~a | ~b = ~(a&b) */
4912 ir_node *block = get_nodes_block(n);
4914 mode = get_irn_mode(n);
4917 n = new_rd_And(get_irn_dbg_info(n), current_ir_graph, block, a, b, mode);
4918 n = new_rd_Not(get_irn_dbg_info(n), current_ir_graph, block, n, mode);
4919 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_DEMORGAN);
4923 /* we can evaluate 2 Projs of the same Cmp */
4924 if (get_irn_mode(n) == mode_b && is_Proj(a) && is_Proj(b)) {
4925 ir_node *pred_a = get_Proj_pred(a);
4926 ir_node *pred_b = get_Proj_pred(b);
4927 if (pred_a == pred_b) {
4928 dbg_info *dbgi = get_irn_dbg_info(n);
4929 ir_node *block = get_nodes_block(pred_a);
4930 pn_Cmp pn_a = get_Proj_proj(a);
4931 pn_Cmp pn_b = get_Proj_proj(b);
4932 /* yes, we can simply calculate with pncs */
4933 pn_Cmp new_pnc = pn_a | pn_b;
4935 return new_rd_Proj(dbgi, current_ir_graph, block, pred_a, mode_b,
4940 mode = get_irn_mode(n);
4941 HANDLE_BINOP_PHI(tarval_or, a, b, c, mode);
4943 n = transform_node_Or_bf_store(n);
4944 n = transform_node_Or_Rotl(n);
4948 n = transform_bitwise_distributive(n, transform_node_Or);
4951 } /* transform_node_Or */
4955 static ir_node *transform_node(ir_node *n);
4958 * Optimize (a >> c1) >> c2), works for Shr, Shrs, Shl, Rotl.
4960 * Should be moved to reassociation?
4962 static ir_node *transform_node_shift(ir_node *n) {
4963 ir_node *left, *right;
4965 tarval *tv1, *tv2, *res;
4966 ir_node *in[2], *irn, *block;
4968 left = get_binop_left(n);
4970 /* different operations */
4971 if (get_irn_op(left) != get_irn_op(n))
4974 right = get_binop_right(n);
4975 tv1 = value_of(right);
4976 if (tv1 == tarval_bad)
4979 tv2 = value_of(get_binop_right(left));
4980 if (tv2 == tarval_bad)
4983 res = tarval_add(tv1, tv2);
4984 mode = get_irn_mode(n);
4986 /* beware: a simple replacement works only, if res < modulo shift */
4988 int modulo_shf = get_mode_modulo_shift(mode);
4989 assert(modulo_shf >= (int) get_mode_size_bits(mode));
4990 if (modulo_shf > 0) {
4991 tarval *modulo = new_tarval_from_long(modulo_shf,
4992 get_tarval_mode(res));
4994 /* shifting too much */
4995 if (!(tarval_cmp(res, modulo) & pn_Cmp_Lt)) {
4997 ir_graph *irg = get_irn_irg(n);
4998 ir_node *block = get_nodes_block(n);
4999 dbg_info *dbgi = get_irn_dbg_info(n);
5000 ir_node *cnst = new_Const(mode_Iu, new_tarval_from_long(get_mode_size_bits(mode)-1, mode_Iu));
5001 return new_rd_Shrs(dbgi, irg, block, get_binop_left(left),
5005 return new_Const(mode, get_mode_null(mode));
5009 res = tarval_mod(res, new_tarval_from_long(get_mode_size_bits(mode), get_tarval_mode(res)));
5012 /* ok, we can replace it */
5013 block = get_nodes_block(n);
5015 in[0] = get_binop_left(left);
5016 in[1] = new_r_Const(current_ir_graph, block, get_tarval_mode(res), res);
5018 irn = new_ir_node(NULL, current_ir_graph, block, get_irn_op(n), mode, 2, in);
5020 DBG_OPT_ALGSIM0(n, irn, FS_OPT_REASSOC_SHIFT);
5022 return transform_node(irn);
5023 } /* transform_node_shift */
5026 * normalisation: (x & c1) >> c2 to (x >> c2) & (c1 >> c2)
5028 * - and, or, xor instead of &
5029 * - Shl, Shr, Shrs, rotl instead of >>
5030 * (with a special case for Or/Xor + Shrs)
5032 static ir_node *transform_node_bitop_shift(ir_node *n) {
5034 ir_node *right = get_binop_right(n);
5035 ir_mode *mode = get_irn_mode(n);
5036 ir_node *bitop_left;
5037 ir_node *bitop_right;
5049 assert(is_Shrs(n) || is_Shr(n) || is_Shl(n) || is_Rotl(n));
5051 if (!is_Const(right))
5054 left = get_binop_left(n);
5055 op_left = get_irn_op(left);
5056 if (op_left != op_And && op_left != op_Or && op_left != op_Eor)
5059 /* doing it with Shrs is not legal if the Or/Eor affects the topmost bit */
5060 if (is_Shrs(n) && (op_left == op_Or || op_left == op_Eor)) {
5061 /* TODO: test if sign bit is affectes */
5065 bitop_right = get_binop_right(left);
5066 if (!is_Const(bitop_right))
5069 bitop_left = get_binop_left(left);
5071 irg = get_irn_irg(n);
5072 block = get_nodes_block(n);
5073 dbgi = get_irn_dbg_info(n);
5074 tv1 = get_Const_tarval(bitop_right);
5075 tv2 = get_Const_tarval(right);
5077 assert(get_tarval_mode(tv1) == mode);
5080 new_shift = new_rd_Shl(dbgi, irg, block, bitop_left, right, mode);
5081 tv_shift = tarval_shl(tv1, tv2);
5082 } else if(is_Shr(n)) {
5083 new_shift = new_rd_Shr(dbgi, irg, block, bitop_left, right, mode);
5084 tv_shift = tarval_shr(tv1, tv2);
5085 } else if(is_Shrs(n)) {
5086 new_shift = new_rd_Shrs(dbgi, irg, block, bitop_left, right, mode);
5087 tv_shift = tarval_shrs(tv1, tv2);
5090 new_shift = new_rd_Rotl(dbgi, irg, block, bitop_left, right, mode);
5091 tv_shift = tarval_rotl(tv1, tv2);
5094 assert(get_tarval_mode(tv_shift) == mode);
5095 new_const = new_Const(mode, tv_shift);
5097 if (op_left == op_And) {
5098 new_bitop = new_rd_And(dbgi, irg, block, new_shift, new_const, mode);
5099 } else if(op_left == op_Or) {
5100 new_bitop = new_rd_Or(dbgi, irg, block, new_shift, new_const, mode);
5102 assert(op_left == op_Eor);
5103 new_bitop = new_rd_Eor(dbgi, irg, block, new_shift, new_const, mode);
5111 * (x << c1) >> c2 <=> x OP (c2-c1) & ((-1 << c1) >> c2)
5113 * (x >> c1) << c2 <=> x OP (c2-c1) & ((-1 >> c1) << c2)
5114 * (also with x >>s c1 when c1>=c2)
5116 static ir_node *transform_node_shl_shr(ir_node *n) {
5118 ir_node *right = get_binop_right(n);
5134 assert(is_Shl(n) || is_Shr(n) || is_Shrs(n));
5136 if (!is_Const(right))
5139 left = get_binop_left(n);
5140 mode = get_irn_mode(n);
5141 if (is_Shl(n) && (is_Shr(left) || is_Shrs(left))) {
5142 ir_node *shr_right = get_binop_right(left);
5144 if (!is_Const(shr_right))
5147 x = get_binop_left(left);
5148 tv_shr = get_Const_tarval(shr_right);
5149 tv_shl = get_Const_tarval(right);
5151 if (is_Shrs(left)) {
5152 /* shrs variant only allowed if c1 >= c2 */
5153 if (! (tarval_cmp(tv_shl, tv_shr) & pn_Cmp_Ge))
5156 tv_mask = tarval_shrs(get_mode_all_one(mode), tv_shr);
5159 tv_mask = tarval_shr(get_mode_all_one(mode), tv_shr);
5161 tv_mask = tarval_shl(tv_mask, tv_shl);
5162 } else if(is_Shr(n) && is_Shl(left)) {
5163 ir_node *shl_right = get_Shl_right(left);
5165 if (!is_Const(shl_right))
5168 x = get_Shl_left(left);
5169 tv_shr = get_Const_tarval(right);
5170 tv_shl = get_Const_tarval(shl_right);
5172 tv_mask = tarval_shl(get_mode_all_one(mode), tv_shl);
5173 tv_mask = tarval_shr(tv_mask, tv_shr);
5178 assert(get_tarval_mode(tv_shl) == get_tarval_mode(tv_shr));
5179 assert(tv_mask != tarval_bad);
5180 assert(get_tarval_mode(tv_mask) == mode);
5182 irg = get_irn_irg(n);
5183 block = get_nodes_block(n);
5184 dbgi = get_irn_dbg_info(n);
5186 pnc = tarval_cmp(tv_shl, tv_shr);
5187 if (pnc == pn_Cmp_Lt || pnc == pn_Cmp_Eq) {
5188 tv_shift = tarval_sub(tv_shr, tv_shl, NULL);
5189 new_const = new_Const(get_tarval_mode(tv_shift), tv_shift);
5191 new_shift = new_rd_Shrs(dbgi, irg, block, x, new_const, mode);
5193 new_shift = new_rd_Shr(dbgi, irg, block, x, new_const, mode);
5196 assert(pnc == pn_Cmp_Gt);
5197 tv_shift = tarval_sub(tv_shl, tv_shr, NULL);
5198 new_const = new_Const(get_tarval_mode(tv_shift), tv_shift);
5199 new_shift = new_rd_Shl(dbgi, irg, block, x, new_const, mode);
5202 new_const = new_Const(mode, tv_mask);
5203 new_and = new_rd_And(dbgi, irg, block, new_shift, new_const, mode);
5211 static ir_node *transform_node_Shr(ir_node *n) {
5212 ir_node *c, *oldn = n;
5213 ir_node *left = get_Shr_left(n);
5214 ir_node *right = get_Shr_right(n);
5215 ir_mode *mode = get_irn_mode(n);
5217 HANDLE_BINOP_PHI(tarval_shr, left, right, c, mode);
5218 n = transform_node_shift(n);
5221 n = transform_node_shl_shr(n);
5223 n = transform_node_bitop_shift(n);
5226 } /* transform_node_Shr */
5231 static ir_node *transform_node_Shrs(ir_node *n) {
5232 ir_node *c, *oldn = n;
5233 ir_node *a = get_Shrs_left(n);
5234 ir_node *b = get_Shrs_right(n);
5235 ir_mode *mode = get_irn_mode(n);
5237 HANDLE_BINOP_PHI(tarval_shrs, a, b, c, mode);
5238 n = transform_node_shift(n);
5241 n = transform_node_bitop_shift(n);
5244 } /* transform_node_Shrs */
5249 static ir_node *transform_node_Shl(ir_node *n) {
5250 ir_node *c, *oldn = n;
5251 ir_node *a = get_Shl_left(n);
5252 ir_node *b = get_Shl_right(n);
5253 ir_mode *mode = get_irn_mode(n);
5255 HANDLE_BINOP_PHI(tarval_shl, a, b, c, mode);
5256 n = transform_node_shift(n);
5259 n = transform_node_shl_shr(n);
5261 n = transform_node_bitop_shift(n);
5264 } /* transform_node_Shl */
5269 static ir_node *transform_node_Rotl(ir_node *n) {
5270 ir_node *c, *oldn = n;
5271 ir_node *a = get_Rotl_left(n);
5272 ir_node *b = get_Rotl_right(n);
5273 ir_mode *mode = get_irn_mode(n);
5275 HANDLE_BINOP_PHI(tarval_rotl, a, b, c, mode);
5276 n = transform_node_shift(n);
5279 n = transform_node_bitop_shift(n);
5282 } /* transform_node_Rotl */
5287 static ir_node *transform_node_Conv(ir_node *n) {
5288 ir_node *c, *oldn = n;
5289 ir_mode *mode = get_irn_mode(n);
5290 ir_node *a = get_Conv_op(n);
5292 if (mode != mode_b && is_const_Phi(a)) {
5293 /* Do NOT optimize mode_b Conv's, this leads to remaining
5294 * Phib nodes later, because the conv_b_lower operation
5295 * is instantly reverted, when it tries to insert a Convb.
5297 c = apply_conv_on_phi(a, mode);
5299 DBG_OPT_ALGSIM0(oldn, c, FS_OPT_CONST_PHI);
5304 if (is_Unknown(a)) { /* Conv_A(Unknown_B) -> Unknown_A */
5305 return new_r_Unknown(current_ir_graph, mode);
5308 if (mode_is_reference(mode) &&
5309 get_mode_size_bits(mode) == get_mode_size_bits(get_irn_mode(a)) &&
5311 ir_node *l = get_Add_left(a);
5312 ir_node *r = get_Add_right(a);
5313 dbg_info *dbgi = get_irn_dbg_info(a);
5314 ir_node *block = get_nodes_block(n);
5316 ir_node *lop = get_Conv_op(l);
5317 if(get_irn_mode(lop) == mode) {
5318 /* ConvP(AddI(ConvI(P), x)) -> AddP(P, x) */
5319 n = new_rd_Add(dbgi, current_ir_graph, block, lop, r, mode);
5324 ir_node *rop = get_Conv_op(r);
5325 if(get_irn_mode(rop) == mode) {
5326 /* ConvP(AddI(x, ConvI(P))) -> AddP(x, P) */
5327 n = new_rd_Add(dbgi, current_ir_graph, block, l, rop, mode);
5334 } /* transform_node_Conv */
5337 * Remove dead blocks and nodes in dead blocks
5338 * in keep alive list. We do not generate a new End node.
5340 static ir_node *transform_node_End(ir_node *n) {
5341 int i, j, n_keepalives = get_End_n_keepalives(n);
5344 NEW_ARR_A(ir_node *, in, n_keepalives);
5346 for (i = j = 0; i < n_keepalives; ++i) {
5347 ir_node *ka = get_End_keepalive(n, i);
5349 if (! is_Block_dead(ka)) {
5353 } else if (is_irn_pinned_in_irg(ka) && is_Block_dead(get_nodes_block(ka))) {
5355 } else if (is_Bad(ka)) {
5356 /* no need to keep Bad */
5361 if (j != n_keepalives)
5362 set_End_keepalives(n, j, in);
5364 } /* transform_node_End */
5366 /** returns 1 if a == -b */
5367 static int is_negated_value(ir_node *a, ir_node *b) {
5368 if (is_Minus(a) && get_Minus_op(a) == b)
5370 if (is_Minus(b) && get_Minus_op(b) == a)
5372 if (is_Sub(a) && is_Sub(b)) {
5373 ir_node *a_left = get_Sub_left(a);
5374 ir_node *a_right = get_Sub_right(a);
5375 ir_node *b_left = get_Sub_left(b);
5376 ir_node *b_right = get_Sub_right(b);
5378 if (a_left == b_right && a_right == b_left)
5386 * Optimize a Mux into some simpler cases.
5388 static ir_node *transform_node_Mux(ir_node *n) {
5389 ir_node *oldn = n, *sel = get_Mux_sel(n);
5390 ir_mode *mode = get_irn_mode(n);
5391 ir_node *t = get_Mux_true(n);
5392 ir_node *f = get_Mux_false(n);
5393 ir_graph *irg = current_ir_graph;
5395 /* first normalization step: move a possible zero to the false case */
5397 ir_node *cmp = get_Proj_pred(sel);
5400 if (is_Const(t) && is_Const_null(t)) {
5403 /* Mux(x, 0, y) => Mux(x, y, 0) */
5404 pn_Cmp pnc = get_Proj_proj(sel);
5405 sel = new_r_Proj(irg, get_nodes_block(cmp), cmp, mode_b,
5406 get_negated_pnc(pnc, get_irn_mode(get_Cmp_left(cmp))));
5407 n = new_rd_Mux(get_irn_dbg_info(n), irg, get_nodes_block(n), sel, t, f, mode);
5415 /* note: after normalization, false can only happen on default */
5416 if (mode == mode_b) {
5417 dbg_info *dbg = get_irn_dbg_info(n);
5418 ir_node *block = get_nodes_block(n);
5419 ir_graph *irg = current_ir_graph;
5422 tarval *tv_t = get_Const_tarval(t);
5423 if (tv_t == tarval_b_true) {
5425 /* Muxb(sel, true, false) = sel */
5426 assert(get_Const_tarval(f) == tarval_b_false);
5427 DBG_OPT_ALGSIM0(oldn, sel, FS_OPT_MUX_BOOL);
5430 /* Muxb(sel, true, x) = Or(sel, x) */
5431 n = new_rd_Or(dbg, irg, block, sel, f, mode_b);
5432 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_OR_BOOL);
5436 } else if (is_Const(f)) {
5437 tarval *tv_f = get_Const_tarval(f);
5438 if (tv_f == tarval_b_true) {
5439 /* Muxb(sel, x, true) = Or(Not(sel), x) */
5440 ir_node* not_sel = new_rd_Not(dbg, irg, block, sel, mode_b);
5441 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_ORNOT_BOOL);
5442 n = new_rd_Or(dbg, irg, block, not_sel, t, mode_b);
5445 /* Muxb(sel, x, false) = And(sel, x) */
5446 assert(tv_f == tarval_b_false);
5447 n = new_rd_And(dbg, irg, block, sel, t, mode_b);
5448 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_AND_BOOL);
5454 /* more normalization: try to normalize Mux(x, C1, C2) into Mux(x, +1/-1, 0) op C2 */
5455 if (is_Const(t) && is_Const(f) && mode_is_int(mode)) {
5456 tarval *a = get_Const_tarval(t);
5457 tarval *b = get_Const_tarval(f);
5458 tarval *null = get_tarval_null(mode);
5461 if (tarval_cmp(a, b) & pn_Cmp_Gt) {
5462 diff = tarval_sub(a, b, NULL);
5465 diff = tarval_sub(b, a, NULL);
5469 if (diff == get_tarval_one(mode) && min != null) {
5470 dbg_info *dbg = get_irn_dbg_info(n);
5471 ir_node *block = get_nodes_block(n);
5472 ir_graph *irg = current_ir_graph;
5473 ir_node *t = new_Const(mode, tarval_sub(a, min, NULL));
5474 ir_node *f = new_Const(mode, tarval_sub(b, min, NULL));
5475 n = new_rd_Mux(dbg, irg, block, sel, f, t, mode);
5476 n = new_rd_Add(dbg, irg, block, n, new_Const(mode, min), mode);
5482 ir_node *cmp = get_Proj_pred(sel);
5483 long pn = get_Proj_proj(sel);
5486 * Note: normalization puts the constant on the right side,
5487 * so we check only one case.
5489 * Note further that these optimization work even for floating point
5490 * with NaN's because -NaN == NaN.
5491 * However, if +0 and -0 is handled differently, we cannot use the Abs/-Abs
5495 ir_node *cmp_r = get_Cmp_right(cmp);
5496 if (is_Const(cmp_r) && is_Const_null(cmp_r)) {
5497 ir_node *block = get_nodes_block(n);
5498 ir_node *cmp_l = get_Cmp_left(cmp);
5500 if (!mode_honor_signed_zeros(mode) && is_negated_value(f, t)) {
5503 if ( (cmp_l == t && (pn == pn_Cmp_Ge || pn == pn_Cmp_Gt))
5504 || (cmp_l == f && (pn == pn_Cmp_Le || pn == pn_Cmp_Lt)))
5506 /* Mux(a >/>= 0, a, -a) = Mux(a </<= 0, -a, a) ==> Abs(a) */
5507 n = new_rd_Abs(get_irn_dbg_info(n), current_ir_graph, block,
5509 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_ABS);
5511 } else if ((cmp_l == t && (pn == pn_Cmp_Le || pn == pn_Cmp_Lt))
5512 || (cmp_l == f && (pn == pn_Cmp_Ge || pn == pn_Cmp_Gt)))
5514 /* Mux(a </<= 0, a, -a) = Mux(a >/>= 0, -a, a) ==> -Abs(a) */
5515 n = new_rd_Abs(get_irn_dbg_info(n), current_ir_graph, block,
5517 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph,
5519 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_ABS);
5524 if (mode_is_int(mode)) {
5526 if ((pn == pn_Cmp_Lg || pn == pn_Cmp_Eq) && is_And(cmp_l)) {
5527 /* Mux((a & b) != 0, c, 0) */
5528 ir_node *and_r = get_And_right(cmp_l);
5531 if (and_r == t && f == cmp_r) {
5532 if (is_Const(t) && tarval_is_single_bit(get_Const_tarval(t))) {
5533 if (pn == pn_Cmp_Lg) {
5534 /* Mux((a & 2^C) != 0, 2^C, 0) */
5536 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_BITOP);
5538 /* Mux((a & 2^C) == 0, 2^C, 0) */
5539 n = new_rd_Eor(get_irn_dbg_info(n), current_ir_graph,
5540 block, cmp_l, t, mode);
5541 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_BITOP);
5546 if (is_Shl(and_r)) {
5547 ir_node *shl_l = get_Shl_left(and_r);
5548 if (is_Const(shl_l) && is_Const_one(shl_l)) {
5549 if (and_r == t && f == cmp_r) {
5550 if (pn == pn_Cmp_Lg) {
5551 /* (a & (1 << n)) != 0, (1 << n), 0) */
5553 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_BITOP);
5555 /* (a & (1 << n)) == 0, (1 << n), 0) */
5556 n = new_rd_Eor(get_irn_dbg_info(n), current_ir_graph,
5557 block, cmp_l, t, mode);
5558 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_BITOP);
5564 and_l = get_And_left(cmp_l);
5565 if (is_Shl(and_l)) {
5566 ir_node *shl_l = get_Shl_left(and_l);
5567 if (is_Const(shl_l) && is_Const_one(shl_l)) {
5568 if (and_l == t && f == cmp_r) {
5569 if (pn == pn_Cmp_Lg) {
5570 /* ((1 << n) & a) != 0, (1 << n), 0) */
5572 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_BITOP);
5574 /* ((1 << n) & a) == 0, (1 << n), 0) */
5575 n = new_rd_Eor(get_irn_dbg_info(n), current_ir_graph,
5576 block, cmp_l, t, mode);
5577 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_BITOP);
5588 return arch_transform_node_Mux(n);
5589 } /* transform_node_Mux */
5592 * optimize Sync nodes that have other syncs as input we simply add the inputs
5593 * of the other sync to our own inputs
5595 static ir_node *transform_node_Sync(ir_node *n) {
5596 int arity = get_Sync_n_preds(n);
5599 for (i = 0; i < arity;) {
5600 ir_node *pred = get_Sync_pred(n, i);
5604 if (!is_Sync(pred)) {
5612 pred_arity = get_Sync_n_preds(pred);
5613 for (j = 0; j < pred_arity; ++j) {
5614 ir_node *pred_pred = get_Sync_pred(pred, j);
5619 add_irn_n(n, pred_pred);
5623 if (get_Sync_pred(n, k) == pred_pred) break;
5628 /* rehash the sync node */
5629 add_identities(current_ir_graph->value_table, n);
5635 * Tries several [inplace] [optimizing] transformations and returns an
5636 * equivalent node. The difference to equivalent_node() is that these
5637 * transformations _do_ generate new nodes, and thus the old node must
5638 * not be freed even if the equivalent node isn't the old one.
5640 static ir_node *transform_node(ir_node *n) {
5644 * Transform_node is the only "optimizing transformation" that might
5645 * return a node with a different opcode. We iterate HERE until fixpoint
5646 * to get the final result.
5650 if (n->op->ops.transform_node)
5651 n = n->op->ops.transform_node(n);
5652 } while (oldn != n);
5655 } /* transform_node */
5658 * Sets the default transform node operation for an ir_op_ops.
5660 * @param code the opcode for the default operation
5661 * @param ops the operations initialized
5666 static ir_op_ops *firm_set_default_transform_node(ir_opcode code, ir_op_ops *ops)
5670 ops->transform_node = transform_node_##a; \
5672 #define CASE_PROJ(a) \
5674 ops->transform_node_Proj = transform_node_Proj_##a; \
5676 #define CASE_PROJ_EX(a) \
5678 ops->transform_node = transform_node_##a; \
5679 ops->transform_node_Proj = transform_node_Proj_##a; \
5688 CASE_PROJ_EX(DivMod);
5722 } /* firm_set_default_transform_node */
5725 /* **************** Common Subexpression Elimination **************** */
5727 /** The size of the hash table used, should estimate the number of nodes
5729 #define N_IR_NODES 512
5731 /** Compares the attributes of two Const nodes. */
5732 static int node_cmp_attr_Const(ir_node *a, ir_node *b) {
5733 return (get_Const_tarval(a) != get_Const_tarval(b))
5734 || (get_Const_type(a) != get_Const_type(b));
5735 } /* node_cmp_attr_Const */
5737 /** Compares the attributes of two Proj nodes. */
5738 static int node_cmp_attr_Proj(ir_node *a, ir_node *b) {
5739 return get_irn_proj_attr(a) != get_irn_proj_attr(b);
5740 } /* node_cmp_attr_Proj */
5742 /** Compares the attributes of two Filter nodes. */
5743 static int node_cmp_attr_Filter(ir_node *a, ir_node *b) {
5744 return get_Filter_proj(a) != get_Filter_proj(b);
5745 } /* node_cmp_attr_Filter */
5747 /** Compares the attributes of two Alloc nodes. */
5748 static int node_cmp_attr_Alloc(ir_node *a, ir_node *b) {
5749 const alloc_attr *pa = get_irn_alloc_attr(a);
5750 const alloc_attr *pb = get_irn_alloc_attr(b);
5751 return (pa->where != pb->where) || (pa->type != pb->type);
5752 } /* node_cmp_attr_Alloc */
5754 /** Compares the attributes of two Free nodes. */
5755 static int node_cmp_attr_Free(ir_node *a, ir_node *b) {
5756 const free_attr *pa = get_irn_free_attr(a);
5757 const free_attr *pb = get_irn_free_attr(b);
5758 return (pa->where != pb->where) || (pa->type != pb->type);
5759 } /* node_cmp_attr_Free */
5761 /** Compares the attributes of two SymConst nodes. */
5762 static int node_cmp_attr_SymConst(ir_node *a, ir_node *b) {
5763 const symconst_attr *pa = get_irn_symconst_attr(a);
5764 const symconst_attr *pb = get_irn_symconst_attr(b);
5765 return (pa->kind != pb->kind)
5766 || (pa->sym.type_p != pb->sym.type_p)
5767 || (pa->tp != pb->tp);
5768 } /* node_cmp_attr_SymConst */
5770 /** Compares the attributes of two Call nodes. */
5771 static int node_cmp_attr_Call(ir_node *a, ir_node *b) {
5772 return get_irn_call_attr(a) != get_irn_call_attr(b);
5773 } /* node_cmp_attr_Call */
5775 /** Compares the attributes of two Sel nodes. */
5776 static int node_cmp_attr_Sel(ir_node *a, ir_node *b) {
5777 const ir_entity *a_ent = get_Sel_entity(a);
5778 const ir_entity *b_ent = get_Sel_entity(b);
5781 (a_ent->kind != b_ent->kind) ||
5782 (a_ent->name != b_ent->name) ||
5783 (a_ent->owner != b_ent->owner) ||
5784 (a_ent->ld_name != b_ent->ld_name) ||
5785 (a_ent->type != b_ent->type);
5787 /* Matze: inlining of functions can produce 2 entities with same type,
5789 return a_ent != b_ent;
5790 } /* node_cmp_attr_Sel */
5792 /** Compares the attributes of two Phi nodes. */
5793 static int node_cmp_attr_Phi(ir_node *a, ir_node *b) {
5794 /* we can only enter this function if both nodes have the same number of inputs,
5795 hence it is enough to check if one of them is a Phi0 */
5797 /* check the Phi0 pos attribute */
5798 return get_irn_phi_attr(a)->u.pos != get_irn_phi_attr(b)->u.pos;
5801 } /* node_cmp_attr_Phi */
5803 /** Compares the attributes of two Conv nodes. */
5804 static int node_cmp_attr_Conv(ir_node *a, ir_node *b) {
5805 return get_Conv_strict(a) != get_Conv_strict(b);
5806 } /* node_cmp_attr_Conv */
5808 /** Compares the attributes of two Cast nodes. */
5809 static int node_cmp_attr_Cast(ir_node *a, ir_node *b) {
5810 return get_Cast_type(a) != get_Cast_type(b);
5811 } /* node_cmp_attr_Cast */
5813 /** Compares the attributes of two Load nodes. */
5814 static int node_cmp_attr_Load(ir_node *a, ir_node *b) {
5815 if (get_Load_volatility(a) == volatility_is_volatile ||
5816 get_Load_volatility(b) == volatility_is_volatile)
5817 /* NEVER do CSE on volatile Loads */
5819 /* do not CSE Loads with different alignment. Be conservative. */
5820 if (get_Load_align(a) != get_Load_align(b))
5823 return get_Load_mode(a) != get_Load_mode(b);
5824 } /* node_cmp_attr_Load */
5826 /** Compares the attributes of two Store nodes. */
5827 static int node_cmp_attr_Store(ir_node *a, ir_node *b) {
5828 /* do not CSE Stores with different alignment. Be conservative. */
5829 if (get_Store_align(a) != get_Store_align(b))
5832 /* NEVER do CSE on volatile Stores */
5833 return (get_Store_volatility(a) == volatility_is_volatile ||
5834 get_Store_volatility(b) == volatility_is_volatile);
5835 } /* node_cmp_attr_Store */
5837 /** Compares two exception attributes */
5838 static int node_cmp_exception(ir_node *a, ir_node *b) {
5839 const except_attr *ea = get_irn_except_attr(a);
5840 const except_attr *eb = get_irn_except_attr(b);
5842 return ea->pin_state != eb->pin_state;
5845 #define node_cmp_attr_Bound node_cmp_exception
5847 /** Compares the attributes of two Div nodes. */
5848 static int node_cmp_attr_Div(ir_node *a, ir_node *b) {
5849 const divmod_attr *ma = get_irn_divmod_attr(a);
5850 const divmod_attr *mb = get_irn_divmod_attr(b);
5851 return ma->exc.pin_state != mb->exc.pin_state ||
5852 ma->res_mode != mb->res_mode ||
5853 ma->no_remainder != mb->no_remainder;
5854 } /* node_cmp_attr_Div */
5856 /** Compares the attributes of two DivMod nodes. */
5857 static int node_cmp_attr_DivMod(ir_node *a, ir_node *b) {
5858 const divmod_attr *ma = get_irn_divmod_attr(a);
5859 const divmod_attr *mb = get_irn_divmod_attr(b);
5860 return ma->exc.pin_state != mb->exc.pin_state ||
5861 ma->res_mode != mb->res_mode;
5862 } /* node_cmp_attr_DivMod */
5864 /** Compares the attributes of two Mod nodes. */
5865 static int node_cmp_attr_Mod(ir_node *a, ir_node *b) {
5866 const divmod_attr *ma = get_irn_divmod_attr(a);
5867 const divmod_attr *mb = get_irn_divmod_attr(b);
5868 return ma->exc.pin_state != mb->exc.pin_state ||
5869 ma->res_mode != mb->res_mode;
5870 } /* node_cmp_attr_Mod */
5872 /** Compares the attributes of two Quot nodes. */
5873 static int node_cmp_attr_Quot(ir_node *a, ir_node *b) {
5874 const divmod_attr *ma = get_irn_divmod_attr(a);
5875 const divmod_attr *mb = get_irn_divmod_attr(b);
5876 return ma->exc.pin_state != mb->exc.pin_state ||
5877 ma->res_mode != mb->res_mode;
5878 } /* node_cmp_attr_Quot */
5880 /** Compares the attributes of two Confirm nodes. */
5881 static int node_cmp_attr_Confirm(ir_node *a, ir_node *b) {
5882 return (get_Confirm_cmp(a) != get_Confirm_cmp(b));
5883 } /* node_cmp_attr_Confirm */
5885 /** Compares the attributes of two ASM nodes. */
5886 static int node_cmp_attr_ASM(ir_node *a, ir_node *b) {
5888 const ir_asm_constraint *ca;
5889 const ir_asm_constraint *cb;
5892 if (get_ASM_text(a) != get_ASM_text(b))
5895 /* Should we really check the constraints here? Should be better, but is strange. */
5896 n = get_ASM_n_input_constraints(a);
5897 if (n != get_ASM_n_input_constraints(b))
5900 ca = get_ASM_input_constraints(a);
5901 cb = get_ASM_input_constraints(b);
5902 for (i = 0; i < n; ++i) {
5903 if (ca[i].pos != cb[i].pos || ca[i].constraint != cb[i].constraint)
5907 n = get_ASM_n_output_constraints(a);
5908 if (n != get_ASM_n_output_constraints(b))
5911 ca = get_ASM_output_constraints(a);
5912 cb = get_ASM_output_constraints(b);
5913 for (i = 0; i < n; ++i) {
5914 if (ca[i].pos != cb[i].pos || ca[i].constraint != cb[i].constraint)
5918 n = get_ASM_n_clobbers(a);
5919 if (n != get_ASM_n_clobbers(b))
5922 cla = get_ASM_clobbers(a);
5923 clb = get_ASM_clobbers(b);
5924 for (i = 0; i < n; ++i) {
5925 if (cla[i] != clb[i])
5929 } /* node_cmp_attr_ASM */
5932 * Set the default node attribute compare operation for an ir_op_ops.
5934 * @param code the opcode for the default operation
5935 * @param ops the operations initialized
5940 static ir_op_ops *firm_set_default_node_cmp_attr(ir_opcode code, ir_op_ops *ops)
5944 ops->node_cmp_attr = node_cmp_attr_##a; \
5975 } /* firm_set_default_node_cmp_attr */
5978 * Compare function for two nodes in the value table. Gets two
5979 * nodes as parameters. Returns 0 if the nodes are a Common Sub Expression.
5981 int identities_cmp(const void *elt, const void *key) {
5982 ir_node *a = (ir_node *)elt;
5983 ir_node *b = (ir_node *)key;
5986 if (a == b) return 0;
5988 if ((get_irn_op(a) != get_irn_op(b)) ||
5989 (get_irn_mode(a) != get_irn_mode(b))) return 1;
5991 /* compare if a's in and b's in are of equal length */
5992 irn_arity_a = get_irn_intra_arity(a);
5993 if (irn_arity_a != get_irn_intra_arity(b))
5996 if (get_irn_pinned(a) == op_pin_state_pinned) {
5997 /* for pinned nodes, the block inputs must be equal */
5998 if (get_irn_intra_n(a, -1) != get_irn_intra_n(b, -1))
6000 } else if (! get_opt_global_cse()) {
6001 /* for block-local CSE both nodes must be in the same MacroBlock */
6002 if (get_irn_MacroBlock(a) != get_irn_MacroBlock(b))
6006 /* compare a->in[0..ins] with b->in[0..ins] */
6007 for (i = 0; i < irn_arity_a; i++)
6008 if (get_irn_intra_n(a, i) != get_irn_intra_n(b, i))
6012 * here, we already now that the nodes are identical except their
6015 if (a->op->ops.node_cmp_attr)
6016 return a->op->ops.node_cmp_attr(a, b);
6019 } /* identities_cmp */
6022 * Calculate a hash value of a node.
6024 * @param node The IR-node
6026 unsigned ir_node_hash(const ir_node *node) {
6027 return node->op->ops.hash(node);
6028 } /* ir_node_hash */
6031 pset *new_identities(void) {
6032 return new_pset(identities_cmp, N_IR_NODES);
6033 } /* new_identities */
6035 void del_identities(pset *value_table) {
6036 del_pset(value_table);
6037 } /* del_identities */
6039 /* Normalize a node by putting constants (and operands with larger
6040 * node index) on the right (operator side). */
6041 void ir_normalize_node(ir_node *n) {
6042 if (is_op_commutative(get_irn_op(n))) {
6043 ir_node *l = get_binop_left(n);
6044 ir_node *r = get_binop_right(n);
6046 /* For commutative operators perform a OP b == b OP a but keep
6047 * constants on the RIGHT side. This helps greatly in some
6048 * optimizations. Moreover we use the idx number to make the form
6050 if (!operands_are_normalized(l, r)) {
6051 set_binop_left(n, r);
6052 set_binop_right(n, l);
6056 } /* ir_normalize_node */
6059 * Update the nodes after a match in the value table. If both nodes have
6060 * the same MacroBlock but different Blocks, we must ensure that the node
6061 * with the dominating Block (the node that is near to the MacroBlock header
6062 * is stored in the table.
6063 * Because a MacroBlock has only one "non-exception" flow, we don't need
6064 * dominance info here: We known, that one block must dominate the other and
6065 * following the only block input will allow to find it.
6067 static void update_known_irn(ir_node *known_irn, const ir_node *new_ir_node) {
6068 ir_node *known_blk, *new_block, *block, *mbh;
6070 if (get_opt_global_cse()) {
6071 /* Block inputs are meaning less */
6074 known_blk = get_irn_n(known_irn, -1);
6075 new_block = get_irn_n(new_ir_node, -1);
6076 if (known_blk == new_block) {
6077 /* already in the same block */
6081 * We expect the typical case when we built the graph. In that case, the
6082 * known_irn is already the upper one, so checking this should be faster.
6085 mbh = get_Block_MacroBlock(new_block);
6087 if (block == known_blk) {
6088 /* ok, we have found it: known_block dominates new_block as expected */
6093 * We have reached the MacroBlock header NOT founding
6094 * the known_block. new_block must dominate known_block.
6097 set_irn_n(known_irn, -1, new_block);
6100 assert(get_Block_n_cfgpreds(block) == 1);
6101 block = get_Block_cfgpred_block(block, 0);
6103 } /* update_value_table */
6106 * Return the canonical node computing the same value as n.
6107 * Looks up the node in a hash table, enters it in the table
6108 * if it isn't there yet.
6110 * @param value_table the HashSet containing all nodes in the
6112 * @param n the node to look up
6114 * @return a node that computes the same value as n or n if no such
6115 * node could be found
6117 ir_node *identify_remember(pset *value_table, ir_node *n) {
6120 if (!value_table) return n;
6122 ir_normalize_node(n);
6123 /* lookup or insert in hash table with given hash key. */
6124 o = pset_insert(value_table, n, ir_node_hash(n));
6127 update_known_irn(o, n);
6131 } /* identify_remember */
6134 * During construction we set the op_pin_state_pinned flag in the graph right when the
6135 * optimization is performed. The flag turning on procedure global cse could
6136 * be changed between two allocations. This way we are safe.
6138 * @param value_table The value table
6139 * @param n The node to lookup
6141 static inline ir_node *identify_cons(pset *value_table, ir_node *n) {
6144 n = identify_remember(value_table, n);
6145 if (n != old && get_irn_MacroBlock(old) != get_irn_MacroBlock(n))
6146 set_irg_pinned(current_ir_graph, op_pin_state_floats);
6148 } /* identify_cons */
6150 /* Add a node to the identities value table. */
6151 void add_identities(pset *value_table, ir_node *node) {
6152 if (get_opt_cse() && is_no_Block(node))
6153 identify_remember(value_table, node);
6154 } /* add_identities */
6156 /* Visit each node in the value table of a graph. */
6157 void visit_all_identities(ir_graph *irg, irg_walk_func visit, void *env) {
6159 ir_graph *rem = current_ir_graph;
6161 current_ir_graph = irg;
6162 foreach_pset(irg->value_table, node)
6164 current_ir_graph = rem;
6165 } /* visit_all_identities */
6168 * Garbage in, garbage out. If a node has a dead input, i.e., the
6169 * Bad node is input to the node, return the Bad node.
6171 static ir_node *gigo(ir_node *node) {
6173 ir_op *op = get_irn_op(node);
6175 /* remove garbage blocks by looking at control flow that leaves the block
6176 and replacing the control flow by Bad. */
6177 if (get_irn_mode(node) == mode_X) {
6178 ir_node *block = get_nodes_block(skip_Proj(node));
6180 /* Don't optimize nodes in immature blocks. */
6181 if (!get_Block_matured(block))
6183 /* Don't optimize End, may have Bads. */
6184 if (op == op_End) return node;
6186 if (is_Block(block)) {
6187 if (is_Block_dead(block)) {
6188 /* control flow from dead block is dead */
6192 for (i = get_irn_arity(block) - 1; i >= 0; --i) {
6193 if (!is_Bad(get_irn_n(block, i)))
6197 ir_graph *irg = get_irn_irg(block);
6198 /* the start block is never dead */
6199 if (block != get_irg_start_block(irg)
6200 && block != get_irg_end_block(irg)) {
6202 * Do NOT kill control flow without setting
6203 * the block to dead of bad things can happen:
6204 * We get a Block that is not reachable be irg_block_walk()
6205 * but can be found by irg_walk()!
6207 set_Block_dead(block);
6214 /* Blocks, Phis and Tuples may have dead inputs, e.g., if one of the
6215 blocks predecessors is dead. */
6216 if (op != op_Block && op != op_Phi && op != op_Tuple) {
6217 irn_arity = get_irn_arity(node);
6220 * Beware: we can only read the block of a non-floating node.
6222 if (is_irn_pinned_in_irg(node) &&
6223 is_Block_dead(get_nodes_block(skip_Proj(node))))
6226 for (i = 0; i < irn_arity; i++) {
6227 ir_node *pred = get_irn_n(node, i);
6232 /* Propagating Unknowns here seems to be a bad idea, because
6233 sometimes we need a node as a input and did not want that
6235 However, it might be useful to move this into a later phase
6236 (if you think that optimizing such code is useful). */
6237 if (is_Unknown(pred) && mode_is_data(get_irn_mode(node)))
6238 return new_Unknown(get_irn_mode(node));
6243 /* With this code we violate the agreement that local_optimize
6244 only leaves Bads in Block, Phi and Tuple nodes. */
6245 /* If Block has only Bads as predecessors it's garbage. */
6246 /* If Phi has only Bads as predecessors it's garbage. */
6247 if ((op == op_Block && get_Block_matured(node)) || op == op_Phi) {
6248 irn_arity = get_irn_arity(node);
6249 for (i = 0; i < irn_arity; i++) {
6250 if (!is_Bad(get_irn_n(node, i))) break;
6252 if (i == irn_arity) node = new_Bad();
6259 * These optimizations deallocate nodes from the obstack.
6260 * It can only be called if it is guaranteed that no other nodes
6261 * reference this one, i.e., right after construction of a node.
6263 * @param n The node to optimize
6265 * current_ir_graph must be set to the graph of the node!
6267 ir_node *optimize_node(ir_node *n) {
6270 ir_opcode iro = get_irn_opcode(n);
6272 /* Always optimize Phi nodes: part of the construction. */
6273 if ((!get_opt_optimize()) && (iro != iro_Phi)) return n;
6275 /* constant expression evaluation / constant folding */
6276 if (get_opt_constant_folding()) {
6277 /* neither constants nor Tuple values can be evaluated */
6278 if (iro != iro_Const && (get_irn_mode(n) != mode_T)) {
6279 unsigned fp_model = get_irg_fp_model(current_ir_graph);
6280 int old_fp_mode = tarval_enable_fp_ops((fp_model & fp_strict_algebraic) == 0);
6281 /* try to evaluate */
6282 tv = computed_value(n);
6283 if (tv != tarval_bad) {
6285 ir_type *old_tp = get_irn_type(n);
6286 int i, arity = get_irn_arity(n);
6290 * Try to recover the type of the new expression.
6292 for (i = 0; i < arity && !old_tp; ++i)
6293 old_tp = get_irn_type(get_irn_n(n, i));
6296 * we MUST copy the node here temporary, because it's still needed
6297 * for DBG_OPT_CSTEVAL
6299 node_size = offsetof(ir_node, attr) + n->op->attr_size;
6300 oldn = alloca(node_size);
6302 memcpy(oldn, n, node_size);
6303 CLONE_ARR_A(ir_node *, oldn->in, n->in);
6305 /* ARG, copy the in array, we need it for statistics */
6306 memcpy(oldn->in, n->in, ARR_LEN(n->in) * sizeof(n->in[0]));
6308 /* note the inplace edges module */
6309 edges_node_deleted(n, current_ir_graph);
6311 /* evaluation was successful -- replace the node. */
6312 irg_kill_node(current_ir_graph, n);
6313 nw = new_Const(get_tarval_mode(tv), tv);
6315 if (old_tp && get_type_mode(old_tp) == get_tarval_mode(tv))
6316 set_Const_type(nw, old_tp);
6317 DBG_OPT_CSTEVAL(oldn, nw);
6318 tarval_enable_fp_ops(old_fp_mode);
6321 tarval_enable_fp_ops(old_fp_mode);
6325 /* remove unnecessary nodes */
6326 if (get_opt_algebraic_simplification() ||
6327 (iro == iro_Phi) || /* always optimize these nodes. */
6329 (iro == iro_Proj) ||
6330 (iro == iro_Block) ) /* Flags tested local. */
6331 n = equivalent_node(n);
6333 /* Common Subexpression Elimination.
6335 * Checks whether n is already available.
6336 * The block input is used to distinguish different subexpressions. Right
6337 * now all nodes are op_pin_state_pinned to blocks, i.e., the CSE only finds common
6338 * subexpressions within a block.
6341 n = identify_cons(current_ir_graph->value_table, n);
6344 edges_node_deleted(oldn, current_ir_graph);
6346 /* We found an existing, better node, so we can deallocate the old node. */
6347 irg_kill_node(current_ir_graph, oldn);
6351 /* Some more constant expression evaluation that does not allow to
6353 iro = get_irn_opcode(n);
6354 if (get_opt_algebraic_simplification() ||
6355 (iro == iro_Cond) ||
6356 (iro == iro_Proj)) /* Flags tested local. */
6357 n = transform_node(n);
6359 /* Remove nodes with dead (Bad) input.
6360 Run always for transformation induced Bads. */
6363 /* Now we have a legal, useful node. Enter it in hash table for CSE */
6364 if (get_opt_cse() && (get_irn_opcode(n) != iro_Block)) {
6366 n = identify_remember(current_ir_graph->value_table, o);
6372 } /* optimize_node */
6376 * These optimizations never deallocate nodes (in place). This can cause dead
6377 * nodes lying on the obstack. Remove these by a dead node elimination,
6378 * i.e., a copying garbage collection.
6380 ir_node *optimize_in_place_2(ir_node *n) {
6383 ir_opcode iro = get_irn_opcode(n);
6385 if (!get_opt_optimize() && !is_Phi(n)) return n;
6387 /* constant expression evaluation / constant folding */
6388 if (get_opt_constant_folding()) {
6389 /* neither constants nor Tuple values can be evaluated */
6390 if (iro != iro_Const && get_irn_mode(n) != mode_T) {
6391 unsigned fp_model = get_irg_fp_model(current_ir_graph);
6392 int old_fp_mode = tarval_enable_fp_ops((fp_model & fp_strict_algebraic) == 0);
6393 /* try to evaluate */
6394 tv = computed_value(n);
6395 if (tv != tarval_bad) {
6396 /* evaluation was successful -- replace the node. */
6397 ir_type *old_tp = get_irn_type(n);
6398 int i, arity = get_irn_arity(n);
6401 * Try to recover the type of the new expression.
6403 for (i = 0; i < arity && !old_tp; ++i)
6404 old_tp = get_irn_type(get_irn_n(n, i));
6406 n = new_Const(get_tarval_mode(tv), tv);
6408 if (old_tp && get_type_mode(old_tp) == get_tarval_mode(tv))
6409 set_Const_type(n, old_tp);
6411 DBG_OPT_CSTEVAL(oldn, n);
6412 tarval_enable_fp_ops(old_fp_mode);
6415 tarval_enable_fp_ops(old_fp_mode);
6419 /* remove unnecessary nodes */
6420 if (get_opt_constant_folding() ||
6421 (iro == iro_Phi) || /* always optimize these nodes. */
6422 (iro == iro_Id) || /* ... */
6423 (iro == iro_Proj) || /* ... */
6424 (iro == iro_Block) ) /* Flags tested local. */
6425 n = equivalent_node(n);
6427 /** common subexpression elimination **/
6428 /* Checks whether n is already available. */
6429 /* The block input is used to distinguish different subexpressions. Right
6430 now all nodes are op_pin_state_pinned to blocks, i.e., the cse only finds common
6431 subexpressions within a block. */
6432 if (get_opt_cse()) {
6434 n = identify_remember(current_ir_graph->value_table, o);
6439 /* Some more constant expression evaluation. */
6440 iro = get_irn_opcode(n);
6441 if (get_opt_constant_folding() ||
6442 (iro == iro_Cond) ||
6443 (iro == iro_Proj)) /* Flags tested local. */
6444 n = transform_node(n);
6446 /* Remove nodes with dead (Bad) input.
6447 Run always for transformation induced Bads. */
6450 /* Now we can verify the node, as it has no dead inputs any more. */
6453 /* Now we have a legal, useful node. Enter it in hash table for cse.
6454 Blocks should be unique anyways. (Except the successor of start:
6455 is cse with the start block!) */
6456 if (get_opt_cse() && (get_irn_opcode(n) != iro_Block)) {
6458 n = identify_remember(current_ir_graph->value_table, o);
6464 } /* optimize_in_place_2 */
6467 * Wrapper for external use, set proper status bits after optimization.
6469 ir_node *optimize_in_place(ir_node *n) {
6470 /* Handle graph state */
6471 assert(get_irg_phase_state(current_ir_graph) != phase_building);
6473 if (get_opt_global_cse())
6474 set_irg_pinned(current_ir_graph, op_pin_state_floats);
6475 if (get_irg_outs_state(current_ir_graph) == outs_consistent)
6476 set_irg_outs_inconsistent(current_ir_graph);
6478 /* FIXME: Maybe we could also test whether optimizing the node can
6479 change the control graph. */
6480 set_irg_doms_inconsistent(current_ir_graph);
6481 return optimize_in_place_2(n);
6482 } /* optimize_in_place */
6485 * Calculate a hash value of a Const node.
6487 static unsigned hash_Const(const ir_node *node) {
6490 /* special value for const, as they only differ in their tarval. */
6491 h = HASH_PTR(node->attr.con.tv);
6492 h = 9*h + HASH_PTR(get_irn_mode(node));
6498 * Calculate a hash value of a SymConst node.
6500 static unsigned hash_SymConst(const ir_node *node) {
6503 /* special value for const, as they only differ in their symbol. */
6504 h = HASH_PTR(node->attr.symc.sym.type_p);
6505 h = 9*h + HASH_PTR(get_irn_mode(node));
6508 } /* hash_SymConst */
6511 * Set the default hash operation in an ir_op_ops.
6513 * @param code the opcode for the default operation
6514 * @param ops the operations initialized
6519 static ir_op_ops *firm_set_default_hash(ir_opcode code, ir_op_ops *ops)
6523 ops->hash = hash_##a; \
6526 /* hash function already set */
6527 if (ops->hash != NULL)
6534 /* use input/mode default hash if no function was given */
6535 ops->hash = firm_default_hash;
6543 * Sets the default operation for an ir_ops.
6545 ir_op_ops *firm_set_default_operations(ir_opcode code, ir_op_ops *ops) {
6546 ops = firm_set_default_hash(code, ops);
6547 ops = firm_set_default_computed_value(code, ops);
6548 ops = firm_set_default_equivalent_node(code, ops);
6549 ops = firm_set_default_transform_node(code, ops);
6550 ops = firm_set_default_node_cmp_attr(code, ops);
6551 ops = firm_set_default_get_type(code, ops);
6552 ops = firm_set_default_get_type_attr(code, ops);
6553 ops = firm_set_default_get_entity_attr(code, ops);
6556 } /* firm_set_default_operations */