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 (a == b && !is_Bad(a))
141 return get_mode_null(mode);
146 if ((ta != tarval_bad) && (tb != tarval_bad))
147 return tarval_sub(ta, tb, mode);
150 } /* computed_value_Sub */
153 * Return the value of a Carry.
154 * Special : a op 0, 0 op b
156 static tarval *computed_value_Carry(const ir_node *n) {
157 ir_node *a = get_binop_left(n);
158 ir_node *b = get_binop_right(n);
159 ir_mode *m = get_irn_mode(n);
161 tarval *ta = value_of(a);
162 tarval *tb = value_of(b);
164 if ((ta != tarval_bad) && (tb != tarval_bad)) {
166 return tarval_carry() ? get_mode_one(m) : get_mode_null(m);
168 if (tarval_is_null(ta) || tarval_is_null(tb))
169 return get_mode_null(m);
172 } /* computed_value_Carry */
175 * Return the value of a Borrow.
178 static tarval *computed_value_Borrow(const ir_node *n) {
179 ir_node *a = get_binop_left(n);
180 ir_node *b = get_binop_right(n);
181 ir_mode *m = get_irn_mode(n);
183 tarval *ta = value_of(a);
184 tarval *tb = value_of(b);
186 if ((ta != tarval_bad) && (tb != tarval_bad)) {
187 return tarval_cmp(ta, tb) == pn_Cmp_Lt ? get_mode_one(m) : get_mode_null(m);
188 } else if (tarval_is_null(ta)) {
189 return get_mode_null(m);
192 } /* computed_value_Borrow */
195 * Return the value of an unary Minus.
197 static tarval *computed_value_Minus(const ir_node *n) {
198 ir_node *a = get_Minus_op(n);
199 tarval *ta = value_of(a);
201 if (ta != tarval_bad)
202 return tarval_neg(ta);
205 } /* computed_value_Minus */
208 * Return the value of a Mul.
210 static tarval *computed_value_Mul(const ir_node *n) {
211 ir_node *a = get_Mul_left(n);
212 ir_node *b = get_Mul_right(n);
215 tarval *ta = value_of(a);
216 tarval *tb = value_of(b);
218 mode = get_irn_mode(n);
219 if (mode != get_irn_mode(a)) {
220 /* n * n = 2n bit multiplication */
221 ta = tarval_convert_to(ta, mode);
222 tb = tarval_convert_to(tb, mode);
225 if (ta != tarval_bad && tb != tarval_bad) {
226 return tarval_mul(ta, tb);
228 /* a*0 = 0 or 0*b = 0 */
229 if (ta == get_mode_null(mode))
231 if (tb == get_mode_null(mode))
235 } /* computed_value_Mul */
238 * Return the value of an Abs.
240 static tarval *computed_value_Abs(const ir_node *n) {
241 ir_node *a = get_Abs_op(n);
242 tarval *ta = value_of(a);
244 if (ta != tarval_bad)
245 return tarval_abs(ta);
248 } /* computed_value_Abs */
251 * Return the value of an And.
252 * Special case: a & 0, 0 & b
254 static tarval *computed_value_And(const ir_node *n) {
255 ir_node *a = get_And_left(n);
256 ir_node *b = get_And_right(n);
258 tarval *ta = value_of(a);
259 tarval *tb = value_of(b);
261 if ((ta != tarval_bad) && (tb != tarval_bad)) {
262 return tarval_and (ta, tb);
264 if (tarval_is_null(ta)) return ta;
265 if (tarval_is_null(tb)) return tb;
268 } /* computed_value_And */
271 * Return the value of an Or.
272 * Special case: a | 1...1, 1...1 | b
274 static tarval *computed_value_Or(const ir_node *n) {
275 ir_node *a = get_Or_left(n);
276 ir_node *b = get_Or_right(n);
278 tarval *ta = value_of(a);
279 tarval *tb = value_of(b);
281 if ((ta != tarval_bad) && (tb != tarval_bad)) {
282 return tarval_or (ta, tb);
284 if (tarval_is_all_one(ta)) return ta;
285 if (tarval_is_all_one(tb)) return tb;
288 } /* computed_value_Or */
291 * Return the value of an Eor.
293 static tarval *computed_value_Eor(const ir_node *n) {
294 ir_node *a = get_Eor_left(n);
295 ir_node *b = get_Eor_right(n);
300 return get_mode_null(get_irn_mode(n));
305 if ((ta != tarval_bad) && (tb != tarval_bad)) {
306 return tarval_eor (ta, tb);
309 } /* computed_value_Eor */
312 * Return the value of a Not.
314 static tarval *computed_value_Not(const ir_node *n) {
315 ir_node *a = get_Not_op(n);
316 tarval *ta = value_of(a);
318 if (ta != tarval_bad)
319 return tarval_not(ta);
322 } /* computed_value_Not */
325 * Return the value of a Shl.
327 static tarval *computed_value_Shl(const ir_node *n) {
328 ir_node *a = get_Shl_left(n);
329 ir_node *b = get_Shl_right(n);
331 tarval *ta = value_of(a);
332 tarval *tb = value_of(b);
334 if ((ta != tarval_bad) && (tb != tarval_bad)) {
335 return tarval_shl (ta, tb);
338 } /* computed_value_Shl */
341 * Return the value of a Shr.
343 static tarval *computed_value_Shr(const ir_node *n) {
344 ir_node *a = get_Shr_left(n);
345 ir_node *b = get_Shr_right(n);
347 tarval *ta = value_of(a);
348 tarval *tb = value_of(b);
350 if ((ta != tarval_bad) && (tb != tarval_bad)) {
351 return tarval_shr (ta, tb);
354 } /* computed_value_Shr */
357 * Return the value of a Shrs.
359 static tarval *computed_value_Shrs(const ir_node *n) {
360 ir_node *a = get_Shrs_left(n);
361 ir_node *b = get_Shrs_right(n);
363 tarval *ta = value_of(a);
364 tarval *tb = value_of(b);
366 if ((ta != tarval_bad) && (tb != tarval_bad)) {
367 return tarval_shrs (ta, tb);
370 } /* computed_value_Shrs */
373 * Return the value of a Rotl.
375 static tarval *computed_value_Rotl(const ir_node *n) {
376 ir_node *a = get_Rotl_left(n);
377 ir_node *b = get_Rotl_right(n);
379 tarval *ta = value_of(a);
380 tarval *tb = value_of(b);
382 if ((ta != tarval_bad) && (tb != tarval_bad)) {
383 return tarval_rotl(ta, tb);
386 } /* computed_value_Rotl */
389 * Return the value of a Conv.
391 static tarval *computed_value_Conv(const ir_node *n) {
392 ir_node *a = get_Conv_op(n);
393 tarval *ta = value_of(a);
395 if (ta != tarval_bad)
396 return tarval_convert_to(ta, get_irn_mode(n));
399 } /* computed_value_Conv */
402 * Calculate the value of a Mux: can be evaluated, if the
403 * sel and the right input are known.
405 static tarval *computed_value_Mux(const ir_node *n) {
406 ir_node *sel = get_Mux_sel(n);
407 tarval *ts = value_of(sel);
409 if (ts == get_tarval_b_true()) {
410 ir_node *v = get_Mux_true(n);
413 else if (ts == get_tarval_b_false()) {
414 ir_node *v = get_Mux_false(n);
418 } /* computed_value_Mux */
421 * Calculate the value of a Confirm: can be evaluated,
422 * if it has the form Confirm(x, '=', Const).
424 static tarval *computed_value_Confirm(const ir_node *n) {
426 * Beware: we might produce Phi(Confirm(x == true), Confirm(x == false)).
427 * Do NOT optimize them away (CondEval wants them), so wait until
428 * remove_confirm is activated.
430 if (get_opt_remove_confirm()) {
431 if (get_Confirm_cmp(n) == pn_Cmp_Eq) {
432 tarval *tv = value_of(get_Confirm_bound(n));
433 if (tv != tarval_bad)
437 return value_of(get_Confirm_value(n));
438 } /* computed_value_Confirm */
441 * Return the value of a Proj(Cmp).
443 * This performs a first step of unreachable code elimination.
444 * Proj can not be computed, but folding a Cmp above the Proj here is
445 * not as wasteful as folding a Cmp into a Tuple of 16 Consts of which
447 * There are several case where we can evaluate a Cmp node, see later.
449 static tarval *computed_value_Proj_Cmp(const ir_node *n) {
450 ir_node *a = get_Proj_pred(n);
451 ir_node *aa = get_Cmp_left(a);
452 ir_node *ab = get_Cmp_right(a);
453 long proj_nr = get_Proj_proj(n);
456 * BEWARE: a == a is NOT always True for floating Point values, as
457 * NaN != NaN is defined, so we must check this here.
460 !mode_is_float(get_irn_mode(aa)) || proj_nr == pn_Cmp_Lt || proj_nr == pn_Cmp_Gt)
463 /* This is a trick with the bits used for encoding the Cmp
464 Proj numbers, the following statement is not the same:
465 return new_tarval_from_long (proj_nr == pn_Cmp_Eq, mode_b) */
466 return new_tarval_from_long (proj_nr & pn_Cmp_Eq, mode_b);
469 tarval *taa = value_of(aa);
470 tarval *tab = value_of(ab);
471 ir_mode *mode = get_irn_mode(aa);
474 * The predecessors of Cmp are target values. We can evaluate
477 if ((taa != tarval_bad) && (tab != tarval_bad)) {
478 /* strange checks... */
479 pn_Cmp flags = tarval_cmp(taa, tab);
480 if (flags != pn_Cmp_False) {
481 return new_tarval_from_long (proj_nr & flags, mode_b);
484 /* for integer values, we can check against MIN/MAX */
485 else if (mode_is_int(mode)) {
486 /* MIN <=/> x. This results in true/false. */
487 if (taa == get_mode_min(mode)) {
488 /* a compare with the MIN value */
489 if (proj_nr == pn_Cmp_Le)
490 return get_tarval_b_true();
491 else if (proj_nr == pn_Cmp_Gt)
492 return get_tarval_b_false();
494 /* x >=/< MIN. This results in true/false. */
496 if (tab == get_mode_min(mode)) {
497 /* a compare with the MIN value */
498 if (proj_nr == pn_Cmp_Ge)
499 return get_tarval_b_true();
500 else if (proj_nr == pn_Cmp_Lt)
501 return get_tarval_b_false();
503 /* MAX >=/< x. This results in true/false. */
504 else if (taa == get_mode_max(mode)) {
505 if (proj_nr == pn_Cmp_Ge)
506 return get_tarval_b_true();
507 else if (proj_nr == pn_Cmp_Lt)
508 return get_tarval_b_false();
510 /* x <=/> MAX. This results in true/false. */
511 else if (tab == get_mode_max(mode)) {
512 if (proj_nr == pn_Cmp_Le)
513 return get_tarval_b_true();
514 else if (proj_nr == pn_Cmp_Gt)
515 return get_tarval_b_false();
519 * The predecessors are Allocs or (void*)(0) constants. Allocs never
520 * return NULL, they raise an exception. Therefore we can predict
524 ir_node *aaa = skip_Proj(aa);
525 ir_node *aba = skip_Proj(ab);
527 if ( ( (/* aa is ProjP and aaa is Alloc */
529 && mode_is_reference(get_irn_mode(aa))
531 && ( (/* ab is NULL */
532 mode_is_reference(get_irn_mode(ab))
533 && tarval_is_null(tab))
534 || (/* ab is other Alloc */
536 && mode_is_reference(get_irn_mode(ab))
539 || (/* aa is NULL and aba is Alloc */
540 mode_is_reference(get_irn_mode(aa))
541 && tarval_is_null(taa)
543 && mode_is_reference(get_irn_mode(ab))
546 return new_tarval_from_long(proj_nr & pn_Cmp_Lg, mode_b);
549 return computed_value_Cmp_Confirm(a, aa, ab, proj_nr);
550 } /* computed_value_Proj_Cmp */
553 * Return the value of a floating point Quot.
555 static tarval *do_computed_value_Quot(const ir_node *a, const ir_node *b) {
556 tarval *ta = value_of(a);
557 tarval *tb = value_of(b);
559 /* cannot optimize 0 / b = 0 because of NaN */
560 if (ta != tarval_bad && tb != tarval_bad)
561 return tarval_quo(ta, tb);
563 } /* do_computed_value_Quot */
566 * Calculate the value of an integer Div of two nodes.
567 * Special case: 0 / b
569 static tarval *do_computed_value_Div(const ir_node *a, const ir_node *b) {
570 tarval *ta = value_of(a);
572 const ir_node *dummy;
574 /* Compute c1 / c2 or 0 / a, a != 0 */
575 if (tarval_is_null(ta) && value_not_zero(b, &dummy))
576 return ta; /* 0 / b == 0 */
578 if (ta != tarval_bad && tb != tarval_bad)
579 return tarval_div(ta, tb);
581 } /* do_computed_value_Div */
584 * Calculate the value of an integer Mod of two nodes.
585 * Special case: a % 1
587 static tarval *do_computed_value_Mod(const ir_node *a, const ir_node *b) {
588 tarval *ta = value_of(a);
589 tarval *tb = value_of(b);
591 /* Compute a % 1 or c1 % c2 */
592 if (tarval_is_one(tb))
593 return get_mode_null(get_irn_mode(a));
594 if (ta != tarval_bad && tb != tarval_bad)
595 return tarval_mod(ta, tb);
597 } /* do_computed_value_Mod */
600 * Return the value of a Proj(DivMod).
602 static tarval *computed_value_Proj_DivMod(const ir_node *n) {
603 long proj_nr = get_Proj_proj(n);
605 /* compute either the Div or the Mod part */
606 if (proj_nr == pn_DivMod_res_div) {
607 const ir_node *a = get_Proj_pred(n);
608 return do_computed_value_Div(get_DivMod_left(a), get_DivMod_right(a));
609 } else if (proj_nr == pn_DivMod_res_mod) {
610 const ir_node *a = get_Proj_pred(n);
611 return do_computed_value_Mod(get_DivMod_left(a), get_DivMod_right(a));
614 } /* computed_value_Proj_DivMod */
617 * Return the value of a Proj(Div).
619 static tarval *computed_value_Proj_Div(const ir_node *n) {
620 long proj_nr = get_Proj_proj(n);
622 if (proj_nr == pn_Div_res) {
623 const ir_node *a = get_Proj_pred(n);
624 return do_computed_value_Div(get_Div_left(a), get_Div_right(a));
627 } /* computed_value_Proj_Div */
630 * Return the value of a Proj(Mod).
632 static tarval *computed_value_Proj_Mod(const ir_node *n) {
633 long proj_nr = get_Proj_proj(n);
635 if (proj_nr == pn_Mod_res) {
636 const ir_node *a = get_Proj_pred(n);
637 return do_computed_value_Mod(get_Mod_left(a), get_Mod_right(a));
640 } /* computed_value_Proj_Mod */
643 * Return the value of a Proj(Quot).
645 static tarval *computed_value_Proj_Quot(const ir_node *n) {
646 long proj_nr = get_Proj_proj(n);
648 if (proj_nr == pn_Quot_res) {
649 const ir_node *a = get_Proj_pred(n);
650 return do_computed_value_Quot(get_Quot_left(a), get_Quot_right(a));
653 } /* computed_value_Proj_Quot */
656 * Return the value of a Proj.
658 static tarval *computed_value_Proj(const ir_node *proj) {
659 ir_node *n = get_Proj_pred(proj);
661 if (n->op->ops.computed_value_Proj != NULL)
662 return n->op->ops.computed_value_Proj(proj);
664 } /* computed_value_Proj */
667 * If the parameter n can be computed, return its value, else tarval_bad.
668 * Performs constant folding.
670 * @param n The node this should be evaluated
672 tarval *computed_value(const ir_node *n) {
673 if (n->op->ops.computed_value)
674 return n->op->ops.computed_value(n);
676 } /* computed_value */
679 * Set the default computed_value evaluator in an ir_op_ops.
681 * @param code the opcode for the default operation
682 * @param ops the operations initialized
687 static ir_op_ops *firm_set_default_computed_value(ir_opcode code, ir_op_ops *ops)
691 ops->computed_value = computed_value_##a; \
693 #define CASE_PROJ(a) \
695 ops->computed_value_Proj = computed_value_Proj_##a; \
732 } /* firm_set_default_computed_value */
735 * Returns a equivalent block for another block.
736 * If the block has only one predecessor, this is
737 * the equivalent one. If the only predecessor of a block is
738 * the block itself, this is a dead block.
740 * If both predecessors of a block are the branches of a binary
741 * Cond, the equivalent block is Cond's block.
743 * If all predecessors of a block are bad or lies in a dead
744 * block, the current block is dead as well.
746 * Note, that blocks are NEVER turned into Bad's, instead
747 * the dead_block flag is set. So, never test for is_Bad(block),
748 * always use is_dead_Block(block).
750 static ir_node *equivalent_node_Block(ir_node *n)
755 /* don't optimize dead blocks */
756 if (is_Block_dead(n))
759 n_preds = get_Block_n_cfgpreds(n);
761 /* The Block constructor does not call optimize, but mature_immBlock()
762 calls the optimization. */
763 assert(get_Block_matured(n));
765 /* Straightening: a single entry Block following a single exit Block
766 can be merged, if it is not the Start block. */
767 /* !!! Beware, all Phi-nodes of n must have been optimized away.
768 This should be true, as the block is matured before optimize is called.
769 But what about Phi-cycles with the Phi0/Id that could not be resolved?
770 Remaining Phi nodes are just Ids. */
772 ir_node *pred = skip_Proj(get_Block_cfgpred(n, 0));
775 ir_node *predblock = get_nodes_block(pred);
776 if (predblock == oldn) {
777 /* Jmp jumps into the block it is in -- deal self cycle. */
778 n = set_Block_dead(n);
779 DBG_OPT_DEAD_BLOCK(oldn, n);
780 } else if (get_opt_control_flow_straightening()) {
782 DBG_OPT_STG(oldn, n);
784 } else if (is_Cond(pred)) {
785 ir_node *predblock = get_nodes_block(pred);
786 if (predblock == oldn) {
787 /* Jmp jumps into the block it is in -- deal self cycle. */
788 n = set_Block_dead(n);
789 DBG_OPT_DEAD_BLOCK(oldn, n);
792 } else if ((n_preds == 2) &&
793 (get_opt_control_flow_weak_simplification())) {
794 /* Test whether Cond jumps twice to this block
795 * The more general case which more than 2 predecessors is handles
796 * in optimize_cf(), we handle only this special case for speed here.
798 ir_node *a = get_Block_cfgpred(n, 0);
799 ir_node *b = get_Block_cfgpred(n, 1);
801 if (is_Proj(a) && is_Proj(b)) {
802 ir_node *cond = get_Proj_pred(a);
804 if (cond == get_Proj_pred(b) && is_Cond(cond) &&
805 get_irn_mode(get_Cond_selector(cond)) == mode_b) {
806 /* Also a single entry Block following a single exit Block. Phis have
807 twice the same operand and will be optimized away. */
808 n = get_nodes_block(cond);
809 DBG_OPT_IFSIM1(oldn, a, b, n);
812 } else if (get_opt_unreachable_code() &&
813 (n != get_irg_start_block(current_ir_graph)) &&
814 (n != get_irg_end_block(current_ir_graph)) ) {
817 /* If all inputs are dead, this block is dead too, except if it is
818 the start or end block. This is one step of unreachable code
820 for (i = get_Block_n_cfgpreds(n) - 1; i >= 0; --i) {
821 ir_node *pred = get_Block_cfgpred(n, i);
824 if (is_Bad(pred)) continue;
825 pred_blk = get_nodes_block(skip_Proj(pred));
827 if (is_Block_dead(pred_blk)) continue;
830 /* really found a living input */
835 n = set_Block_dead(n);
836 DBG_OPT_DEAD_BLOCK(oldn, n);
841 } /* equivalent_node_Block */
844 * Returns a equivalent node for a Jmp, a Bad :-)
845 * Of course this only happens if the Block of the Jmp is dead.
847 static ir_node *equivalent_node_Jmp(ir_node *n) {
850 /* unreachable code elimination */
851 if (is_Block_dead(get_nodes_block(n))) {
852 n = get_irg_bad(current_ir_graph);
853 DBG_OPT_DEAD_BLOCK(oldn, n);
856 } /* equivalent_node_Jmp */
858 /** Raise is handled in the same way as Jmp. */
859 #define equivalent_node_Raise equivalent_node_Jmp
862 /* We do not evaluate Cond here as we replace it by a new node, a Jmp.
863 See transform_node_Proj_Cond(). */
866 * Optimize operations that are commutative and have neutral 0,
867 * so a op 0 = 0 op a = a.
869 static ir_node *equivalent_node_neutral_zero(ir_node *n) {
872 ir_node *a = get_binop_left(n);
873 ir_node *b = get_binop_right(n);
878 /* After running compute_node there is only one constant predecessor.
879 Find this predecessors value and remember the other node: */
880 if ((tv = value_of(a)) != tarval_bad) {
882 } else if ((tv = value_of(b)) != tarval_bad) {
887 /* If this predecessors constant value is zero, the operation is
888 * unnecessary. Remove it.
890 * Beware: If n is a Add, the mode of on and n might be different
891 * which happens in this rare construction: NULL + 3.
892 * Then, a Conv would be needed which we cannot include here.
894 if (tarval_is_null(tv) && get_irn_mode(on) == get_irn_mode(n)) {
897 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_0);
901 } /* equivalent_node_neutral_zero */
904 * Eor is commutative and has neutral 0.
906 static ir_node *equivalent_node_Eor(ir_node *n) {
911 n = equivalent_node_neutral_zero(n);
912 if (n != oldn) return n;
915 b = get_Eor_right(n);
918 ir_node *aa = get_Eor_left(a);
919 ir_node *ab = get_Eor_right(a);
922 /* (a ^ b) ^ a -> b */
924 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_EOR_A_B_A);
926 } else if (ab == b) {
927 /* (a ^ b) ^ b -> a */
929 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_EOR_A_B_A);
934 ir_node *ba = get_Eor_left(b);
935 ir_node *bb = get_Eor_right(b);
938 /* a ^ (a ^ b) -> b */
940 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_EOR_A_B_A);
942 } else if (bb == a) {
943 /* a ^ (b ^ a) -> b */
945 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_EOR_A_B_A);
953 * Optimize a - 0 and (a - x) + x (for modes with wrap-around).
955 * The second one looks strange, but this construct
956 * is used heavily in the LCC sources :-).
958 * Beware: The Mode of an Add may be different than the mode of its
959 * predecessors, so we could not return a predecessors in all cases.
961 static ir_node *equivalent_node_Add(ir_node *n) {
963 ir_node *left, *right;
964 ir_mode *mode = get_irn_mode(n);
966 n = equivalent_node_neutral_zero(n);
970 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
971 if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
974 left = get_Add_left(n);
975 right = get_Add_right(n);
978 if (get_Sub_right(left) == right) {
981 n = get_Sub_left(left);
982 if (mode == get_irn_mode(n)) {
983 DBG_OPT_ALGSIM1(oldn, left, right, n, FS_OPT_ADD_SUB);
989 if (get_Sub_right(right) == left) {
992 n = get_Sub_left(right);
993 if (mode == get_irn_mode(n)) {
994 DBG_OPT_ALGSIM1(oldn, left, right, n, FS_OPT_ADD_SUB);
1000 } /* equivalent_node_Add */
1003 * optimize operations that are not commutative but have neutral 0 on left,
1006 static ir_node *equivalent_node_left_zero(ir_node *n) {
1009 ir_node *a = get_binop_left(n);
1010 ir_node *b = get_binop_right(n);
1011 tarval *tb = value_of(b);
1013 if (tarval_is_null(tb)) {
1016 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_0);
1019 } /* equivalent_node_left_zero */
1021 #define equivalent_node_Shl equivalent_node_left_zero
1022 #define equivalent_node_Shr equivalent_node_left_zero
1023 #define equivalent_node_Shrs equivalent_node_left_zero
1024 #define equivalent_node_Rotl equivalent_node_left_zero
1027 * Optimize a - 0 and (a + x) - x (for modes with wrap-around).
1029 * The second one looks strange, but this construct
1030 * is used heavily in the LCC sources :-).
1032 * Beware: The Mode of a Sub may be different than the mode of its
1033 * predecessors, so we could not return a predecessors in all cases.
1035 static ir_node *equivalent_node_Sub(ir_node *n) {
1038 ir_mode *mode = get_irn_mode(n);
1041 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
1042 if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
1045 b = get_Sub_right(n);
1048 /* Beware: modes might be different */
1049 if (tarval_is_null(tb)) {
1050 ir_node *a = get_Sub_left(n);
1051 if (mode == get_irn_mode(a)) {
1054 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_0);
1058 } /* equivalent_node_Sub */
1062 * Optimize an "self-inverse unary op", ie op(op(n)) = n.
1065 * -(-a) == a, but might overflow two times.
1066 * We handle it anyway here but the better way would be a
1067 * flag. This would be needed for Pascal for instance.
1069 static ir_node *equivalent_node_idempotent_unop(ir_node *n) {
1071 ir_node *pred = get_unop_op(n);
1073 /* optimize symmetric unop */
1074 if (get_irn_op(pred) == get_irn_op(n)) {
1075 n = get_unop_op(pred);
1076 DBG_OPT_ALGSIM2(oldn, pred, n, FS_OPT_IDEM_UNARY);
1079 } /* equivalent_node_idempotent_unop */
1081 /** Optimize Not(Not(x)) == x. */
1082 #define equivalent_node_Not equivalent_node_idempotent_unop
1084 /** -(-x) == x ??? Is this possible or can --x raise an
1085 out of bounds exception if min =! max? */
1086 #define equivalent_node_Minus equivalent_node_idempotent_unop
1089 * Optimize a * 1 = 1 * a = a.
1091 static ir_node *equivalent_node_Mul(ir_node *n) {
1093 ir_node *a = get_Mul_left(n);
1095 /* we can handle here only the n * n = n bit cases */
1096 if (get_irn_mode(n) == get_irn_mode(a)) {
1097 ir_node *b = get_Mul_right(n);
1101 * Mul is commutative and has again an other neutral element.
1102 * Constants are place right, so check this case first.
1105 if (tarval_is_one(tv)) {
1107 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
1110 if (tarval_is_one(tv)) {
1112 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
1117 } /* equivalent_node_Mul */
1120 * Use algebraic simplification a | a = a | 0 = 0 | a = a.
1122 static ir_node *equivalent_node_Or(ir_node *n) {
1125 ir_node *a = get_Or_left(n);
1126 ir_node *b = get_Or_right(n);
1130 n = a; /* Or has it's own neutral element */
1131 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_OR);
1134 /* constants are cormalized to right, check this site first */
1136 if (tarval_is_null(tv)) {
1138 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_OR);
1142 if (tarval_is_null(tv)) {
1144 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_OR);
1149 } /* equivalent_node_Or */
1152 * Optimize a & 0b1...1 = 0b1...1 & a = a & a = (a|X) & a = a.
1154 static ir_node *equivalent_node_And(ir_node *n) {
1157 ir_node *a = get_And_left(n);
1158 ir_node *b = get_And_right(n);
1162 n = a; /* And has it's own neutral element */
1163 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_AND);
1166 /* constants are cormalized to right, check this site first */
1168 if (tarval_is_all_one(tv)) {
1170 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1174 if (tarval_is_all_one(tv)) {
1176 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1180 if (b == get_Or_left(a) || b == get_Or_right(a)) {
1183 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1188 if (a == get_Or_left(b) || a == get_Or_right(b)) {
1191 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1196 } /* equivalent_node_And */
1199 * Try to remove useless Conv's:
1201 static ir_node *equivalent_node_Conv(ir_node *n) {
1203 ir_node *a = get_Conv_op(n);
1205 ir_mode *n_mode = get_irn_mode(n);
1206 ir_mode *a_mode = get_irn_mode(a);
1209 if (n_mode == a_mode) { /* No Conv necessary */
1210 if (get_Conv_strict(n)) {
1211 /* special case: the predecessor might be a also a Conv */
1213 if (! get_Conv_strict(a)) {
1214 /* first one is not strict, kick it */
1216 a_mode = get_irn_mode(a);
1220 /* else both are strict conv, second is superfluous */
1223 ir_node *pred = get_Proj_pred(a);
1224 if (is_Load(pred)) {
1225 /* loads always return with the exact precision of n_mode */
1226 assert(get_Load_mode(pred) == n_mode);
1230 /* leave strict floating point Conv's */
1235 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CONV);
1236 } else if (is_Conv(a)) { /* Conv(Conv(b)) */
1237 ir_node *b = get_Conv_op(a);
1238 ir_mode *b_mode = get_irn_mode(b);
1240 if (get_Conv_strict(n) && get_Conv_strict(a)) {
1241 /* both are strict conv */
1242 if (smaller_mode(a_mode, n_mode)) {
1243 /* both are strict, but the first is smaller, so
1244 the second cannot remove more precision, remove the
1246 set_Conv_strict(n, 0);
1249 if (n_mode == b_mode) {
1250 if (! get_Conv_strict(n) && ! get_Conv_strict(a)) {
1251 if (n_mode == mode_b) {
1252 n = b; /* Convb(Conv*(xxxb(...))) == xxxb(...) */
1253 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
1255 } else if (get_mode_arithmetic(n_mode) == get_mode_arithmetic(a_mode)) {
1256 if (values_in_mode(b_mode, a_mode)) {
1257 n = b; /* ConvS(ConvL(xxxS(...))) == xxxS(...) */
1258 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
1263 if (mode_is_int(n_mode) && get_mode_arithmetic(a_mode) == irma_ieee754) {
1264 /* ConvI(ConvF(I)) -> I, iff float mantissa >= int mode */
1265 unsigned int_mantissa = get_mode_size_bits(n_mode) - (mode_is_signed(n_mode) ? 1 : 0);
1266 unsigned float_mantissa = tarval_ieee754_get_mantissa_size(a_mode);
1268 if (float_mantissa >= int_mantissa) {
1270 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
1275 if (smaller_mode(b_mode, a_mode)) {
1276 if (get_Conv_strict(n))
1277 set_Conv_strict(b, 1);
1278 n = b; /* ConvA(ConvB(ConvA(...))) == ConvA(...) */
1279 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
1286 } /* equivalent_node_Conv */
1289 * A Cast may be removed if the type of the previous node
1290 * is already the type of the Cast.
1292 static ir_node *equivalent_node_Cast(ir_node *n) {
1294 ir_node *pred = get_Cast_op(n);
1296 if (get_irn_type(pred) == get_Cast_type(n)) {
1298 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CAST);
1301 } /* equivalent_node_Cast */
1304 * - fold Phi-nodes, iff they have only one predecessor except
1307 static ir_node *equivalent_node_Phi(ir_node *n) {
1312 ir_node *first_val = NULL; /* to shutup gcc */
1314 if (!get_opt_normalize()) return n;
1316 n_preds = get_Phi_n_preds(n);
1318 block = get_nodes_block(n);
1319 if (is_Block_dead(block)) /* Control dead */
1320 return get_irg_bad(current_ir_graph);
1322 if (n_preds == 0) return n; /* Phi of dead Region without predecessors. */
1324 /* Find first non-self-referencing input */
1325 for (i = 0; i < n_preds; ++i) {
1326 first_val = get_Phi_pred(n, i);
1327 if ( (first_val != n) /* not self pointer */
1329 /* BEWARE: when the if is changed to 1, Phi's will ignore it's Bad
1330 * predecessors. Then, Phi nodes in dead code might be removed, causing
1331 * nodes pointing to themself (Add's for instance).
1332 * This is really bad and causes endless recursions in several
1333 * code pathes, so we do NOT optimize such a code.
1334 * This is not that bad as it sounds, optimize_cf() removes bad control flow
1335 * (and bad Phi predecessors), so live code is optimized later.
1337 && (! is_Bad(get_Block_cfgpred(block, i)))
1339 ) { /* value not dead */
1340 break; /* then found first value. */
1345 /* A totally Bad or self-referencing Phi (we didn't break the above loop) */
1346 return get_irg_bad(current_ir_graph);
1349 /* search for rest of inputs, determine if any of these
1350 are non-self-referencing */
1351 while (++i < n_preds) {
1352 ir_node *scnd_val = get_Phi_pred(n, i);
1353 if ( (scnd_val != n)
1354 && (scnd_val != first_val)
1357 && (! is_Bad(get_Block_cfgpred(block, i)))
1365 /* Fold, if no multiple distinct non-self-referencing inputs */
1367 DBG_OPT_PHI(oldn, n);
1370 } /* equivalent_node_Phi */
1373 * Several optimizations:
1374 * - fold Sync-nodes, iff they have only one predecessor except
1377 static ir_node *equivalent_node_Sync(ir_node *n) {
1378 int arity = get_Sync_n_preds(n);
1381 for (i = 0; i < arity;) {
1382 ir_node *pred = get_Sync_pred(n, i);
1385 /* Remove Bad predecessors */
1392 /* Remove duplicate predecessors */
1398 if (get_Sync_pred(n, j) == pred) {
1406 if (arity == 0) return get_irg_bad(current_ir_graph);
1407 if (arity == 1) return get_Sync_pred(n, 0);
1409 } /* equivalent_node_Sync */
1412 * Optimize Proj(Tuple).
1414 static ir_node *equivalent_node_Proj_Tuple(ir_node *proj) {
1415 ir_node *oldn = proj;
1416 ir_node *tuple = get_Proj_pred(proj);
1418 /* Remove the Tuple/Proj combination. */
1419 proj = get_Tuple_pred(tuple, get_Proj_proj(proj));
1420 DBG_OPT_TUPLE(oldn, tuple, proj);
1423 } /* equivalent_node_Proj_Tuple */
1426 * Optimize a / 1 = a.
1428 static ir_node *equivalent_node_Proj_Div(ir_node *proj) {
1429 ir_node *oldn = proj;
1430 ir_node *div = get_Proj_pred(proj);
1431 ir_node *b = get_Div_right(div);
1432 tarval *tb = value_of(b);
1434 /* Div is not commutative. */
1435 if (tarval_is_one(tb)) { /* div(x, 1) == x */
1436 switch (get_Proj_proj(proj)) {
1438 proj = get_Div_mem(div);
1439 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NEUTRAL_1);
1443 proj = get_Div_left(div);
1444 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NEUTRAL_1);
1448 /* we cannot replace the exception Proj's here, this is done in
1449 transform_node_Proj_Div() */
1454 } /* equivalent_node_Proj_Div */
1457 * Optimize a / 1.0 = a.
1459 static ir_node *equivalent_node_Proj_Quot(ir_node *proj) {
1460 ir_node *oldn = proj;
1461 ir_node *quot = get_Proj_pred(proj);
1462 ir_node *b = get_Quot_right(quot);
1463 tarval *tb = value_of(b);
1465 /* Div is not commutative. */
1466 if (tarval_is_one(tb)) { /* Quot(x, 1) == x */
1467 switch (get_Proj_proj(proj)) {
1469 proj = get_Quot_mem(quot);
1470 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NEUTRAL_1);
1474 proj = get_Quot_left(quot);
1475 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NEUTRAL_1);
1479 /* we cannot replace the exception Proj's here, this is done in
1480 transform_node_Proj_Quot() */
1485 } /* equivalent_node_Proj_Quot */
1488 * Optimize a / 1 = a.
1490 static ir_node *equivalent_node_Proj_DivMod(ir_node *proj) {
1491 ir_node *oldn = proj;
1492 ir_node *divmod = get_Proj_pred(proj);
1493 ir_node *b = get_DivMod_right(divmod);
1494 tarval *tb = value_of(b);
1496 /* Div is not commutative. */
1497 if (tarval_is_one(tb)) { /* div(x, 1) == x */
1498 switch (get_Proj_proj(proj)) {
1500 proj = get_DivMod_mem(divmod);
1501 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NEUTRAL_1);
1504 case pn_DivMod_res_div:
1505 proj = get_DivMod_left(divmod);
1506 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NEUTRAL_1);
1510 /* we cannot replace the exception Proj's here, this is done in
1511 transform_node_Proj_DivMod().
1512 Note further that the pn_DivMod_res_div case is handled in
1513 computed_value_Proj(). */
1518 } /* equivalent_node_Proj_DivMod */
1521 * Optimize CopyB(mem, x, x) into a Nop.
1523 static ir_node *equivalent_node_Proj_CopyB(ir_node *proj) {
1524 ir_node *oldn = proj;
1525 ir_node *copyb = get_Proj_pred(proj);
1526 ir_node *a = get_CopyB_dst(copyb);
1527 ir_node *b = get_CopyB_src(copyb);
1530 /* Turn CopyB into a tuple (mem, jmp, bad, bad) */
1531 switch (get_Proj_proj(proj)) {
1532 case pn_CopyB_M_regular:
1533 proj = get_CopyB_mem(copyb);
1534 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NOP);
1537 case pn_CopyB_M_except:
1538 case pn_CopyB_X_except:
1539 DBG_OPT_EXC_REM(proj);
1540 proj = get_irg_bad(current_ir_graph);
1545 } /* equivalent_node_Proj_CopyB */
1548 * Optimize Bounds(idx, idx, upper) into idx.
1550 static ir_node *equivalent_node_Proj_Bound(ir_node *proj) {
1551 ir_node *oldn = proj;
1552 ir_node *bound = get_Proj_pred(proj);
1553 ir_node *idx = get_Bound_index(bound);
1554 ir_node *pred = skip_Proj(idx);
1557 if (idx == get_Bound_lower(bound))
1559 else if (is_Bound(pred)) {
1561 * idx was Bounds checked in the same MacroBlock previously,
1562 * it is still valid if lower <= pred_lower && pred_upper <= upper.
1564 ir_node *lower = get_Bound_lower(bound);
1565 ir_node *upper = get_Bound_upper(bound);
1566 if (get_Bound_lower(pred) == lower &&
1567 get_Bound_upper(pred) == upper &&
1568 get_irn_MacroBlock(bound) == get_irn_MacroBlock(pred)) {
1570 * One could expect that we simply return the previous
1571 * Bound here. However, this would be wrong, as we could
1572 * add an exception Proj to a new location then.
1573 * So, we must turn in into a tuple.
1579 /* Turn Bound into a tuple (mem, jmp, bad, idx) */
1580 switch (get_Proj_proj(proj)) {
1582 DBG_OPT_EXC_REM(proj);
1583 proj = get_Bound_mem(bound);
1585 case pn_Bound_X_except:
1586 DBG_OPT_EXC_REM(proj);
1587 proj = get_irg_bad(current_ir_graph);
1591 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NOP);
1594 /* cannot optimize pn_Bound_X_regular, handled in transform ... */
1599 } /* equivalent_node_Proj_Bound */
1602 * Optimize an Exception Proj(Load) with a non-null address.
1604 static ir_node *equivalent_node_Proj_Load(ir_node *proj) {
1605 if (get_opt_ldst_only_null_ptr_exceptions()) {
1606 if (get_irn_mode(proj) == mode_X) {
1607 ir_node *load = get_Proj_pred(proj);
1609 /* get the Load address */
1610 const ir_node *addr = get_Load_ptr(load);
1611 const ir_node *confirm;
1613 if (value_not_null(addr, &confirm)) {
1614 if (get_Proj_proj(proj) == pn_Load_X_except) {
1615 DBG_OPT_EXC_REM(proj);
1616 return get_irg_bad(current_ir_graph);
1622 } /* equivalent_node_Proj_Load */
1625 * Optimize an Exception Proj(Store) with a non-null address.
1627 static ir_node *equivalent_node_Proj_Store(ir_node *proj) {
1628 if (get_opt_ldst_only_null_ptr_exceptions()) {
1629 if (get_irn_mode(proj) == mode_X) {
1630 ir_node *store = get_Proj_pred(proj);
1632 /* get the load/store address */
1633 const ir_node *addr = get_Store_ptr(store);
1634 const ir_node *confirm;
1636 if (value_not_null(addr, &confirm)) {
1637 if (get_Proj_proj(proj) == pn_Store_X_except) {
1638 DBG_OPT_EXC_REM(proj);
1639 return get_irg_bad(current_ir_graph);
1645 } /* equivalent_node_Proj_Store */
1648 * Does all optimizations on nodes that must be done on it's Proj's
1649 * because of creating new nodes.
1651 static ir_node *equivalent_node_Proj(ir_node *proj) {
1652 ir_node *n = get_Proj_pred(proj);
1654 if (get_irn_mode(proj) == mode_X) {
1655 if (is_Block_dead(get_nodes_block(n))) {
1656 /* Remove dead control flow -- early gigo(). */
1657 return get_irg_bad(current_ir_graph);
1660 if (n->op->ops.equivalent_node_Proj)
1661 return n->op->ops.equivalent_node_Proj(proj);
1663 } /* equivalent_node_Proj */
1668 static ir_node *equivalent_node_Id(ir_node *n) {
1675 DBG_OPT_ID(oldn, n);
1677 } /* equivalent_node_Id */
1682 static ir_node *equivalent_node_Mux(ir_node *n)
1684 ir_node *oldn = n, *sel = get_Mux_sel(n);
1685 tarval *ts = value_of(sel);
1687 /* Mux(true, f, t) == t */
1688 if (ts == tarval_b_true) {
1689 n = get_Mux_true(n);
1690 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_C);
1692 /* Mux(false, f, t) == f */
1693 else if (ts == tarval_b_false) {
1694 n = get_Mux_false(n);
1695 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_C);
1697 /* Mux(v, x, x) == x */
1698 else if (get_Mux_false(n) == get_Mux_true(n)) {
1699 n = get_Mux_true(n);
1700 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_EQ);
1702 else if (is_Proj(sel) && !mode_honor_signed_zeros(get_irn_mode(n))) {
1703 ir_node *cmp = get_Proj_pred(sel);
1704 long proj_nr = get_Proj_proj(sel);
1705 ir_node *f = get_Mux_false(n);
1706 ir_node *t = get_Mux_true(n);
1709 * Note further that these optimization work even for floating point
1710 * with NaN's because -NaN == NaN.
1711 * However, if +0 and -0 is handled differently, we cannot use the first one.
1714 ir_node *const cmp_l = get_Cmp_left(cmp);
1715 ir_node *const cmp_r = get_Cmp_right(cmp);
1719 if ((cmp_l == t && cmp_r == f) || /* Mux(t == f, t, f) -> f */
1720 (cmp_l == f && cmp_r == t)) { /* Mux(f == t, t, f) -> f */
1722 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1729 if ((cmp_l == t && cmp_r == f) || /* Mux(t != f, t, f) -> t */
1730 (cmp_l == f && cmp_r == t)) { /* Mux(f != t, t, f) -> t */
1732 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1739 * Note: normalization puts the constant on the right side,
1740 * so we check only one case.
1742 if (cmp_l == t && tarval_is_null(value_of(cmp_r))) {
1743 /* Mux(t CMP 0, X, t) */
1744 if (is_Minus(f) && get_Minus_op(f) == t) {
1745 /* Mux(t CMP 0, -t, t) */
1746 if (proj_nr == pn_Cmp_Eq) {
1747 /* Mux(t == 0, -t, t) ==> -t */
1749 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1750 } else if (proj_nr == pn_Cmp_Lg || proj_nr == pn_Cmp_Ne) {
1751 /* Mux(t != 0, -t, t) ==> t */
1753 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1760 } /* equivalent_node_Mux */
1763 * Remove Confirm nodes if setting is on.
1764 * Replace Confirms(x, '=', Constlike) by Constlike.
1766 static ir_node *equivalent_node_Confirm(ir_node *n) {
1767 ir_node *pred = get_Confirm_value(n);
1768 pn_Cmp pnc = get_Confirm_cmp(n);
1770 while (is_Confirm(pred) && pnc == get_Confirm_cmp(pred)) {
1772 * rare case: two identical Confirms one after another,
1773 * replace the second one with the first.
1776 pred = get_Confirm_value(n);
1777 pnc = get_Confirm_cmp(n);
1779 if (get_opt_remove_confirm())
1780 return get_Confirm_value(n);
1785 * equivalent_node() returns a node equivalent to input n. It skips all nodes that
1786 * perform no actual computation, as, e.g., the Id nodes. It does not create
1787 * new nodes. It is therefore safe to free n if the node returned is not n.
1788 * If a node returns a Tuple we can not just skip it. If the size of the
1789 * in array fits, we transform n into a tuple (e.g., Div).
1791 ir_node *equivalent_node(ir_node *n) {
1792 if (n->op->ops.equivalent_node)
1793 return n->op->ops.equivalent_node(n);
1795 } /* equivalent_node */
1798 * Sets the default equivalent node operation for an ir_op_ops.
1800 * @param code the opcode for the default operation
1801 * @param ops the operations initialized
1806 static ir_op_ops *firm_set_default_equivalent_node(ir_opcode code, ir_op_ops *ops)
1810 ops->equivalent_node = equivalent_node_##a; \
1812 #define CASE_PROJ(a) \
1814 ops->equivalent_node_Proj = equivalent_node_Proj_##a; \
1856 } /* firm_set_default_equivalent_node */
1859 * Returns non-zero if a node is a Phi node
1860 * with all predecessors constant.
1862 static int is_const_Phi(ir_node *n) {
1865 if (! is_Phi(n) || get_irn_arity(n) == 0)
1867 for (i = get_irn_arity(n) - 1; i >= 0; --i) {
1868 if (! is_Const(get_irn_n(n, i)))
1872 } /* is_const_Phi */
1874 typedef tarval *(*tarval_sub_type)(tarval *a, tarval *b, ir_mode *mode);
1875 typedef tarval *(*tarval_binop_type)(tarval *a, tarval *b);
1878 * Wrapper for the tarval binop evaluation, tarval_sub has one more parameter.
1880 static tarval *do_eval(tarval *(*eval)(), tarval *a, tarval *b, ir_mode *mode) {
1881 if (eval == tarval_sub) {
1882 tarval_sub_type func = (tarval_sub_type)eval;
1884 return func(a, b, mode);
1886 tarval_binop_type func = (tarval_binop_type)eval;
1893 * Apply an evaluator on a binop with a constant operators (and one Phi).
1895 * @param phi the Phi node
1896 * @param other the other operand
1897 * @param eval an evaluator function
1898 * @param mode the mode of the result, may be different from the mode of the Phi!
1899 * @param left if non-zero, other is the left operand, else the right
1901 * @return a new Phi node if the conversion was successful, NULL else
1903 static ir_node *apply_binop_on_phi(ir_node *phi, tarval *other, tarval *(*eval)(), ir_mode *mode, int left) {
1908 int i, n = get_irn_arity(phi);
1910 NEW_ARR_A(void *, res, n);
1912 for (i = 0; i < n; ++i) {
1913 pred = get_irn_n(phi, i);
1914 tv = get_Const_tarval(pred);
1915 tv = do_eval(eval, other, tv, mode);
1917 if (tv == tarval_bad) {
1918 /* folding failed, bad */
1924 for (i = 0; i < n; ++i) {
1925 pred = get_irn_n(phi, i);
1926 tv = get_Const_tarval(pred);
1927 tv = do_eval(eval, tv, other, mode);
1929 if (tv == tarval_bad) {
1930 /* folding failed, bad */
1936 irg = current_ir_graph;
1937 for (i = 0; i < n; ++i) {
1938 pred = get_irn_n(phi, i);
1939 res[i] = new_r_Const_type(irg, get_irg_start_block(irg),
1940 mode, res[i], get_Const_type(pred));
1942 return new_r_Phi(irg, get_nodes_block(phi), n, (ir_node **)res, mode);
1943 } /* apply_binop_on_phi */
1946 * Apply an evaluator on a binop with two constant Phi.
1948 * @param a the left Phi node
1949 * @param b the right Phi node
1950 * @param eval an evaluator function
1951 * @param mode the mode of the result, may be different from the mode of the Phi!
1953 * @return a new Phi node if the conversion was successful, NULL else
1955 static ir_node *apply_binop_on_2_phis(ir_node *a, ir_node *b, tarval *(*eval)(), ir_mode *mode) {
1956 tarval *tv_l, *tv_r, *tv;
1962 if (get_nodes_block(a) != get_nodes_block(b))
1965 n = get_irn_arity(a);
1966 NEW_ARR_A(void *, res, n);
1968 for (i = 0; i < n; ++i) {
1969 pred = get_irn_n(a, i);
1970 tv_l = get_Const_tarval(pred);
1971 pred = get_irn_n(b, i);
1972 tv_r = get_Const_tarval(pred);
1973 tv = do_eval(eval, tv_l, tv_r, mode);
1975 if (tv == tarval_bad) {
1976 /* folding failed, bad */
1981 irg = current_ir_graph;
1982 for (i = 0; i < n; ++i) {
1983 pred = get_irn_n(a, i);
1984 res[i] = new_r_Const_type(irg, get_irg_start_block(irg), mode, res[i], get_Const_type(pred));
1986 return new_r_Phi(irg, get_nodes_block(a), n, (ir_node **)res, mode);
1987 } /* apply_binop_on_2_phis */
1990 * Apply an evaluator on a unop with a constant operator (a Phi).
1992 * @param phi the Phi node
1993 * @param eval an evaluator function
1995 * @return a new Phi node if the conversion was successful, NULL else
1997 static ir_node *apply_unop_on_phi(ir_node *phi, tarval *(*eval)(tarval *)) {
2003 int i, n = get_irn_arity(phi);
2005 NEW_ARR_A(void *, res, n);
2006 for (i = 0; i < n; ++i) {
2007 pred = get_irn_n(phi, i);
2008 tv = get_Const_tarval(pred);
2011 if (tv == tarval_bad) {
2012 /* folding failed, bad */
2017 mode = get_irn_mode(phi);
2018 irg = current_ir_graph;
2019 for (i = 0; i < n; ++i) {
2020 pred = get_irn_n(phi, i);
2021 res[i] = new_r_Const_type(irg, get_irg_start_block(irg),
2022 mode, res[i], get_Const_type(pred));
2024 return new_r_Phi(irg, get_nodes_block(phi), n, (ir_node **)res, mode);
2025 } /* apply_unop_on_phi */
2028 * Apply a conversion on a constant operator (a Phi).
2030 * @param phi the Phi node
2032 * @return a new Phi node if the conversion was successful, NULL else
2034 static ir_node *apply_conv_on_phi(ir_node *phi, ir_mode *mode) {
2039 int i, n = get_irn_arity(phi);
2041 NEW_ARR_A(void *, res, n);
2042 for (i = 0; i < n; ++i) {
2043 pred = get_irn_n(phi, i);
2044 tv = get_Const_tarval(pred);
2045 tv = tarval_convert_to(tv, mode);
2047 if (tv == tarval_bad) {
2048 /* folding failed, bad */
2053 irg = current_ir_graph;
2054 for (i = 0; i < n; ++i) {
2055 pred = get_irn_n(phi, i);
2056 res[i] = new_r_Const_type(irg, get_irg_start_block(irg),
2057 mode, res[i], get_Const_type(pred));
2059 return new_r_Phi(irg, get_nodes_block(phi), n, (ir_node **)res, mode);
2060 } /* apply_conv_on_phi */
2063 * Transform AddP(P, ConvIs(Iu)), AddP(P, ConvIu(Is)) and
2064 * SubP(P, ConvIs(Iu)), SubP(P, ConvIu(Is)).
2065 * If possible, remove the Conv's.
2067 static ir_node *transform_node_AddSub(ir_node *n) {
2068 ir_mode *mode = get_irn_mode(n);
2070 if (mode_is_reference(mode)) {
2071 ir_node *left = get_binop_left(n);
2072 ir_node *right = get_binop_right(n);
2073 unsigned ref_bits = get_mode_size_bits(mode);
2075 if (is_Conv(left)) {
2076 ir_mode *lmode = get_irn_mode(left);
2077 unsigned bits = get_mode_size_bits(lmode);
2079 if (ref_bits == bits &&
2080 mode_is_int(lmode) &&
2081 get_mode_arithmetic(lmode) == irma_twos_complement) {
2082 ir_node *pre = get_Conv_op(left);
2083 ir_mode *pre_mode = get_irn_mode(pre);
2085 if (mode_is_int(pre_mode) &&
2086 get_mode_size_bits(pre_mode) == bits &&
2087 get_mode_arithmetic(pre_mode) == irma_twos_complement) {
2088 /* ok, this conv just changes to sign, moreover the calculation
2089 * is done with same number of bits as our address mode, so
2090 * we can ignore the conv as address calculation can be viewed
2091 * as either signed or unsigned
2093 set_binop_left(n, pre);
2098 if (is_Conv(right)) {
2099 ir_mode *rmode = get_irn_mode(right);
2100 unsigned bits = get_mode_size_bits(rmode);
2102 if (ref_bits == bits &&
2103 mode_is_int(rmode) &&
2104 get_mode_arithmetic(rmode) == irma_twos_complement) {
2105 ir_node *pre = get_Conv_op(right);
2106 ir_mode *pre_mode = get_irn_mode(pre);
2108 if (mode_is_int(pre_mode) &&
2109 get_mode_size_bits(pre_mode) == bits &&
2110 get_mode_arithmetic(pre_mode) == irma_twos_complement) {
2111 /* ok, this conv just changes to sign, moreover the calculation
2112 * is done with same number of bits as our address mode, so
2113 * we can ignore the conv as address calculation can be viewed
2114 * as either signed or unsigned
2116 set_binop_right(n, pre);
2121 /* let address arithmetic use unsigned modes */
2122 if (is_Const(right)) {
2123 ir_mode *rmode = get_irn_mode(right);
2125 if (mode_is_signed(rmode) && get_mode_arithmetic(rmode) == irma_twos_complement) {
2126 /* convert a AddP(P, *s) into AddP(P, *u) */
2127 ir_mode *nm = get_reference_mode_unsigned_eq(mode);
2129 ir_node *pre = new_r_Conv(current_ir_graph, get_nodes_block(n), right, nm);
2130 set_binop_right(n, pre);
2136 } /* transform_node_AddSub */
2138 #define HANDLE_BINOP_PHI(eval, a, b, c, mode) \
2140 if (is_Const(b) && is_const_Phi(a)) { \
2141 /* check for Op(Phi, Const) */ \
2142 c = apply_binop_on_phi(a, get_Const_tarval(b), eval, mode, 0);\
2144 else if (is_Const(a) && is_const_Phi(b)) { \
2145 /* check for Op(Const, Phi) */ \
2146 c = apply_binop_on_phi(b, get_Const_tarval(a), eval, mode, 1);\
2148 else if (is_const_Phi(a) && is_const_Phi(b)) { \
2149 /* check for Op(Phi, Phi) */ \
2150 c = apply_binop_on_2_phis(a, b, eval, mode); \
2153 DBG_OPT_ALGSIM0(oldn, c, FS_OPT_CONST_PHI); \
2157 #define HANDLE_UNOP_PHI(eval, a, c) \
2159 if (is_const_Phi(a)) { \
2160 /* check for Op(Phi) */ \
2161 c = apply_unop_on_phi(a, eval); \
2163 DBG_OPT_ALGSIM0(oldn, c, FS_OPT_CONST_PHI); \
2169 * Do the AddSub optimization, then Transform
2170 * Constant folding on Phi
2171 * Add(a,a) -> Mul(a, 2)
2172 * Add(Mul(a, x), a) -> Mul(a, x+1)
2173 * if the mode is integer or float.
2174 * Transform Add(a,-b) into Sub(a,b).
2175 * Reassociation might fold this further.
2177 static ir_node *transform_node_Add(ir_node *n) {
2179 ir_node *a, *b, *c, *oldn = n;
2181 n = transform_node_AddSub(n);
2183 a = get_Add_left(n);
2184 b = get_Add_right(n);
2186 mode = get_irn_mode(n);
2188 if (mode_is_reference(mode)) {
2189 ir_mode *lmode = get_irn_mode(a);
2191 if (is_Const(b) && is_Const_null(b) && mode_is_int(lmode)) {
2192 /* an Add(a, NULL) is a hidden Conv */
2193 dbg_info *dbg = get_irn_dbg_info(n);
2194 return new_rd_Conv(dbg, current_ir_graph, get_nodes_block(n), a, mode);
2198 HANDLE_BINOP_PHI(tarval_add, a, b, c, mode);
2200 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
2201 if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
2204 if (mode_is_num(mode)) {
2205 /* the following code leads to endless recursion when Mul are replaced by a simple instruction chain */
2206 if (!is_arch_dep_running() && a == b && mode_is_int(mode)) {
2207 ir_node *block = get_nodes_block(n);
2210 get_irn_dbg_info(n),
2214 new_r_Const_long(current_ir_graph, block, mode, 2),
2216 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_A);
2221 get_irn_dbg_info(n),
2227 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_MINUS_B);
2232 get_irn_dbg_info(n),
2238 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_MINUS_B);
2241 if (get_mode_arithmetic(mode) == irma_twos_complement) {
2242 /* Here we rely on constants be on the RIGHT side */
2244 ir_node *op = get_Not_op(a);
2246 if (is_Const(b) && is_Const_one(b)) {
2248 ir_node *blk = get_nodes_block(n);
2249 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, blk, op, mode);
2250 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_PLUS_1);
2255 ir_node *blk = get_nodes_block(n);
2256 n = new_r_Const(current_ir_graph, blk, mode, get_mode_minus_one(mode));
2257 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_X_NOT_X);
2262 ir_node *op = get_Not_op(b);
2266 ir_node *blk = get_nodes_block(n);
2267 n = new_r_Const(current_ir_graph, blk, mode, get_mode_minus_one(mode));
2268 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_X_NOT_X);
2275 } /* transform_node_Add */
2278 * returns -cnst or NULL if impossible
2280 static ir_node *const_negate(ir_node *cnst) {
2281 tarval *tv = tarval_neg(get_Const_tarval(cnst));
2282 dbg_info *dbgi = get_irn_dbg_info(cnst);
2283 ir_graph *irg = get_irn_irg(cnst);
2284 ir_node *block = get_nodes_block(cnst);
2285 ir_mode *mode = get_irn_mode(cnst);
2286 if (tv == tarval_bad) return NULL;
2287 return new_rd_Const(dbgi, irg, block, mode, tv);
2291 * Do the AddSub optimization, then Transform
2292 * Constant folding on Phi
2293 * Sub(0,a) -> Minus(a)
2294 * Sub(Mul(a, x), a) -> Mul(a, x-1)
2295 * Sub(Sub(x, y), b) -> Sub(x, Add(y,b))
2296 * Sub(Add(a, x), x) -> a
2297 * Sub(x, Add(x, a)) -> -a
2298 * Sub(x, Const) -> Add(x, -Const)
2300 static ir_node *transform_node_Sub(ir_node *n) {
2305 n = transform_node_AddSub(n);
2307 a = get_Sub_left(n);
2308 b = get_Sub_right(n);
2310 mode = get_irn_mode(n);
2312 if (mode_is_int(mode)) {
2313 ir_mode *lmode = get_irn_mode(a);
2315 if (is_Const(b) && is_Const_null(b) && mode_is_reference(lmode)) {
2316 /* a Sub(a, NULL) is a hidden Conv */
2317 dbg_info *dbg = get_irn_dbg_info(n);
2318 n = new_rd_Conv(dbg, current_ir_graph, get_nodes_block(n), a, mode);
2319 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_CONV);
2323 if (mode == lmode &&
2324 get_mode_arithmetic(mode) == irma_twos_complement &&
2326 get_Const_tarval(a) == get_mode_minus_one(mode)) {
2328 dbg_info *dbg = get_irn_dbg_info(n);
2329 n = new_rd_Not(dbg, current_ir_graph, get_nodes_block(n), b, mode);
2330 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_NOT);
2336 HANDLE_BINOP_PHI(tarval_sub, a, b, c, mode);
2338 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
2339 if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
2342 if (is_Const(b) && get_irn_mode(b) != mode_P) {
2343 /* a - C -> a + (-C) */
2344 ir_node *cnst = const_negate(b);
2346 ir_node *block = get_nodes_block(n);
2347 dbg_info *dbgi = get_irn_dbg_info(n);
2348 ir_graph *irg = get_irn_irg(n);
2350 n = new_rd_Add(dbgi, irg, block, a, cnst, mode);
2351 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2356 if (is_Minus(a)) { /* (-a) - b -> -(a + b) */
2357 ir_graph *irg = current_ir_graph;
2358 dbg_info *dbg = get_irn_dbg_info(n);
2359 ir_node *block = get_nodes_block(n);
2360 ir_node *left = get_Minus_op(a);
2361 ir_node *add = new_rd_Add(dbg, irg, block, left, b, mode);
2363 n = new_rd_Minus(dbg, irg, block, add, mode);
2364 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2366 } else if (is_Minus(b)) { /* a - (-b) -> a + b */
2367 ir_graph *irg = current_ir_graph;
2368 dbg_info *dbg = get_irn_dbg_info(n);
2369 ir_node *block = get_nodes_block(n);
2370 ir_node *right = get_Minus_op(b);
2372 n = new_rd_Add(dbg, irg, block, a, right, mode);
2373 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MINUS);
2375 } else if (is_Sub(b)) {
2376 /* a - (b - c) -> a + (c - b)
2377 * -> (a - b) + c iff (b - c) is a pointer */
2378 ir_graph *irg = current_ir_graph;
2379 dbg_info *s_dbg = get_irn_dbg_info(b);
2380 ir_node *s_block = get_nodes_block(b);
2381 ir_node *s_left = get_Sub_left(b);
2382 ir_node *s_right = get_Sub_right(b);
2383 ir_mode *s_mode = get_irn_mode(b);
2384 if (s_mode == mode_P) {
2385 ir_node *sub = new_rd_Sub(s_dbg, irg, s_block, a, s_left, mode);
2386 dbg_info *a_dbg = get_irn_dbg_info(n);
2387 ir_node *a_block = get_nodes_block(n);
2390 s_right = new_r_Conv(irg, a_block, s_right, mode);
2391 n = new_rd_Add(a_dbg, irg, a_block, sub, s_right, mode);
2393 ir_node *sub = new_rd_Sub(s_dbg, irg, s_block, s_right, s_left, s_mode);
2394 dbg_info *a_dbg = get_irn_dbg_info(n);
2395 ir_node *a_block = get_nodes_block(n);
2397 n = new_rd_Add(a_dbg, irg, a_block, a, sub, mode);
2399 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2401 } else if (is_Mul(b)) { /* a - (b * C) -> a + (b * -C) */
2402 ir_node *m_right = get_Mul_right(b);
2403 if (is_Const(m_right)) {
2404 ir_node *cnst2 = const_negate(m_right);
2405 if (cnst2 != NULL) {
2406 ir_graph *irg = current_ir_graph;
2407 dbg_info *m_dbg = get_irn_dbg_info(b);
2408 ir_node *m_block = get_nodes_block(b);
2409 ir_node *m_left = get_Mul_left(b);
2410 ir_mode *m_mode = get_irn_mode(b);
2411 ir_node *mul = new_rd_Mul(m_dbg, irg, m_block, m_left, cnst2, m_mode);
2412 dbg_info *a_dbg = get_irn_dbg_info(n);
2413 ir_node *a_block = get_nodes_block(n);
2415 n = new_rd_Add(a_dbg, irg, a_block, a, mul, mode);
2416 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2422 /* Beware of Sub(P, P) which cannot be optimized into a simple Minus ... */
2423 if (mode_is_num(mode) && mode == get_irn_mode(a) && is_Const(a) && is_Const_null(a)) {
2425 get_irn_dbg_info(n),
2430 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_0_A);
2434 if (mode_wrap_around(mode)) {
2435 ir_node *left = get_Add_left(a);
2436 ir_node *right = get_Add_right(a);
2438 /* FIXME: Does the Conv's work only for two complement or generally? */
2440 if (mode != get_irn_mode(right)) {
2441 /* This Sub is an effective Cast */
2442 right = new_r_Conv(get_irn_irg(n), get_nodes_block(n), right, mode);
2445 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2447 } else if (right == b) {
2448 if (mode != get_irn_mode(left)) {
2449 /* This Sub is an effective Cast */
2450 left = new_r_Conv(get_irn_irg(n), get_nodes_block(n), left, mode);
2453 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2459 if (mode_wrap_around(mode)) {
2460 ir_node *left = get_Add_left(b);
2461 ir_node *right = get_Add_right(b);
2463 /* FIXME: Does the Conv's work only for two complement or generally? */
2465 ir_mode *r_mode = get_irn_mode(right);
2467 n = new_r_Minus(get_irn_irg(n), get_nodes_block(n), right, r_mode);
2468 if (mode != r_mode) {
2469 /* This Sub is an effective Cast */
2470 n = new_r_Conv(get_irn_irg(n), get_nodes_block(n), n, mode);
2472 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2474 } else if (right == a) {
2475 ir_mode *l_mode = get_irn_mode(left);
2477 n = new_r_Minus(get_irn_irg(n), get_nodes_block(n), left, l_mode);
2478 if (mode != l_mode) {
2479 /* This Sub is an effective Cast */
2480 n = new_r_Conv(get_irn_irg(n), get_nodes_block(n), n, mode);
2482 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2487 if (mode_is_int(mode) && is_Conv(a) && is_Conv(b)) {
2488 ir_mode *mode = get_irn_mode(a);
2490 if (mode == get_irn_mode(b)) {
2492 ir_node *op_a = get_Conv_op(a);
2493 ir_node *op_b = get_Conv_op(b);
2495 /* check if it's allowed to skip the conv */
2496 ma = get_irn_mode(op_a);
2497 mb = get_irn_mode(op_b);
2499 if (mode_is_reference(ma) && mode_is_reference(mb)) {
2500 /* SubInt(ConvInt(aP), ConvInt(bP)) -> SubInt(aP,bP) */
2503 set_Sub_right(n, b);
2509 /* do NOT execute this code if reassociation is enabled, it does the inverse! */
2510 if (!is_reassoc_running() && is_Mul(a)) {
2511 ir_node *ma = get_Mul_left(a);
2512 ir_node *mb = get_Mul_right(a);
2515 ir_node *blk = get_nodes_block(n);
2517 get_irn_dbg_info(n),
2518 current_ir_graph, blk,
2521 get_irn_dbg_info(n),
2522 current_ir_graph, blk,
2524 new_r_Const_long(current_ir_graph, blk, mode, 1),
2527 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MUL_A_X_A);
2529 } else if (mb == b) {
2530 ir_node *blk = get_nodes_block(n);
2532 get_irn_dbg_info(n),
2533 current_ir_graph, blk,
2536 get_irn_dbg_info(n),
2537 current_ir_graph, blk,
2539 new_r_Const_long(current_ir_graph, blk, mode, 1),
2542 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MUL_A_X_A);
2546 if (is_Sub(a)) { /* (x - y) - b -> x - (y + b) */
2547 ir_node *x = get_Sub_left(a);
2548 ir_node *y = get_Sub_right(a);
2549 ir_node *blk = get_nodes_block(n);
2550 ir_mode *m_b = get_irn_mode(b);
2551 ir_mode *m_y = get_irn_mode(y);
2555 /* Determine the right mode for the Add. */
2558 else if (mode_is_reference(m_b))
2560 else if (mode_is_reference(m_y))
2564 * Both modes are different but none is reference,
2565 * happens for instance in SubP(SubP(P, Iu), Is).
2566 * We have two possibilities here: Cast or ignore.
2567 * Currently we ignore this case.
2572 add = new_r_Add(current_ir_graph, blk, y, b, add_mode);
2574 n = new_rd_Sub(get_irn_dbg_info(n), current_ir_graph, blk, x, add, mode);
2575 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_SUB_X_Y_Z);
2579 if (get_mode_arithmetic(mode) == irma_twos_complement) {
2580 if (is_Const(a) && is_Not(b)) {
2581 /* c - ~X = X + (c+1) */
2582 tarval *tv = get_Const_tarval(a);
2584 tv = tarval_add(tv, get_mode_one(mode));
2585 if (tv != tarval_bad) {
2586 ir_node *blk = get_nodes_block(n);
2587 ir_node *c = new_r_Const(current_ir_graph, blk, mode, tv);
2588 n = new_rd_Add(get_irn_dbg_info(n), current_ir_graph, blk, get_Not_op(b), c, mode);
2589 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_C_NOT_X);
2595 } /* transform_node_Sub */
2598 * Several transformation done on n*n=2n bits mul.
2599 * These transformations must be done here because new nodes may be produced.
2601 static ir_node *transform_node_Mul2n(ir_node *n, ir_mode *mode) {
2603 ir_node *a = get_Mul_left(n);
2604 ir_node *b = get_Mul_right(n);
2605 tarval *ta = value_of(a);
2606 tarval *tb = value_of(b);
2607 ir_mode *smode = get_irn_mode(a);
2609 if (ta == get_mode_one(smode)) {
2610 /* (L)1 * (L)b = (L)b */
2611 ir_node *blk = get_nodes_block(n);
2612 n = new_rd_Conv(get_irn_dbg_info(n), current_ir_graph, blk, b, mode);
2613 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
2616 else if (ta == get_mode_minus_one(smode)) {
2617 /* (L)-1 * (L)b = (L)b */
2618 ir_node *blk = get_nodes_block(n);
2619 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, blk, b, smode);
2620 n = new_rd_Conv(get_irn_dbg_info(n), current_ir_graph, blk, n, mode);
2621 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2624 if (tb == get_mode_one(smode)) {
2625 /* (L)a * (L)1 = (L)a */
2626 ir_node *blk = get_irn_n(a, -1);
2627 n = new_rd_Conv(get_irn_dbg_info(n), current_ir_graph, blk, a, mode);
2628 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
2631 else if (tb == get_mode_minus_one(smode)) {
2632 /* (L)a * (L)-1 = (L)-a */
2633 ir_node *blk = get_nodes_block(n);
2634 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, blk, a, smode);
2635 n = new_rd_Conv(get_irn_dbg_info(n), current_ir_graph, blk, n, mode);
2636 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2643 * Transform Mul(a,-1) into -a.
2644 * Do constant evaluation of Phi nodes.
2645 * Do architecture dependent optimizations on Mul nodes
2647 static ir_node *transform_node_Mul(ir_node *n) {
2648 ir_node *c, *oldn = n;
2649 ir_mode *mode = get_irn_mode(n);
2650 ir_node *a = get_Mul_left(n);
2651 ir_node *b = get_Mul_right(n);
2653 if (is_Bad(a) || is_Bad(b))
2656 if (mode != get_irn_mode(a))
2657 return transform_node_Mul2n(n, mode);
2659 HANDLE_BINOP_PHI(tarval_mul, a, b, c, mode);
2661 if (mode_is_signed(mode)) {
2664 if (value_of(a) == get_mode_minus_one(mode))
2666 else if (value_of(b) == get_mode_minus_one(mode))
2669 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, get_nodes_block(n), r, mode);
2670 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2675 if (is_Const(b)) { /* (-a) * const -> a * -const */
2676 ir_node *cnst = const_negate(b);
2678 dbg_info *dbgi = get_irn_dbg_info(n);
2679 ir_node *block = get_nodes_block(n);
2680 n = new_rd_Mul(dbgi, current_ir_graph, block, get_Minus_op(a), cnst, mode);
2681 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2684 } else if (is_Minus(b)) { /* (-a) * (-b) -> a * b */
2685 dbg_info *dbgi = get_irn_dbg_info(n);
2686 ir_node *block = get_nodes_block(n);
2687 n = new_rd_Mul(dbgi, current_ir_graph, block, get_Minus_op(a), get_Minus_op(b), mode);
2688 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_MINUS);
2690 } else if (is_Sub(b)) { /* (-a) * (b - c) -> a * (c - b) */
2691 ir_node *sub_l = get_Sub_left(b);
2692 ir_node *sub_r = get_Sub_right(b);
2693 dbg_info *dbgi = get_irn_dbg_info(n);
2694 ir_graph *irg = current_ir_graph;
2695 ir_node *block = get_nodes_block(n);
2696 ir_node *new_b = new_rd_Sub(dbgi, irg, block, sub_r, sub_l, mode);
2697 n = new_rd_Mul(dbgi, irg, block, get_Minus_op(a), new_b, mode);
2698 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS);
2701 } else if (is_Minus(b)) {
2702 if (is_Sub(a)) { /* (a - b) * (-c) -> (b - a) * c */
2703 ir_node *sub_l = get_Sub_left(a);
2704 ir_node *sub_r = get_Sub_right(a);
2705 dbg_info *dbgi = get_irn_dbg_info(n);
2706 ir_graph *irg = current_ir_graph;
2707 ir_node *block = get_nodes_block(n);
2708 ir_node *new_a = new_rd_Sub(dbgi, irg, block, sub_r, sub_l, mode);
2709 n = new_rd_Mul(dbgi, irg, block, new_a, get_Minus_op(b), mode);
2710 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS);
2714 if (get_mode_arithmetic(mode) == irma_ieee754) {
2716 tarval *tv = get_Const_tarval(a);
2717 if (tarval_ieee754_get_exponent(tv) == 1 && tarval_ieee754_zero_mantissa(tv)
2718 && !tarval_is_negative(tv)) {
2719 /* 2.0 * b = b + b */
2720 n = new_rd_Add(get_irn_dbg_info(n), current_ir_graph, get_nodes_block(n), b, b, mode);
2721 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_A_A);
2725 else if (is_Const(b)) {
2726 tarval *tv = get_Const_tarval(b);
2727 if (tarval_ieee754_get_exponent(tv) == 1 && tarval_ieee754_zero_mantissa(tv)
2728 && !tarval_is_negative(tv)) {
2729 /* a * 2.0 = a + a */
2730 n = new_rd_Add(get_irn_dbg_info(n), current_ir_graph, get_nodes_block(n), a, a, mode);
2731 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_A_A);
2736 return arch_dep_replace_mul_with_shifts(n);
2737 } /* transform_node_Mul */
2740 * Transform a Div Node.
2742 static ir_node *transform_node_Div(ir_node *n) {
2743 ir_mode *mode = get_Div_resmode(n);
2744 ir_node *a = get_Div_left(n);
2745 ir_node *b = get_Div_right(n);
2749 if (is_Const(b) && is_const_Phi(a)) {
2750 /* check for Div(Phi, Const) */
2751 value = apply_binop_on_phi(a, get_Const_tarval(b), tarval_div, mode, 0);
2753 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2757 else if (is_Const(a) && is_const_Phi(b)) {
2758 /* check for Div(Const, Phi) */
2759 value = apply_binop_on_phi(b, get_Const_tarval(a), tarval_div, mode, 1);
2761 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2765 else if (is_const_Phi(a) && is_const_Phi(b)) {
2766 /* check for Div(Phi, Phi) */
2767 value = apply_binop_on_2_phis(a, b, tarval_div, mode);
2769 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2776 if (tv != tarval_bad) {
2777 value = new_Const(get_tarval_mode(tv), tv);
2779 DBG_OPT_CSTEVAL(n, value);
2782 ir_node *a = get_Div_left(n);
2783 ir_node *b = get_Div_right(n);
2784 const ir_node *dummy;
2786 if (a == b && value_not_zero(a, &dummy)) {
2787 /* BEWARE: we can optimize a/a to 1 only if this cannot cause a exception */
2788 value = new_Const(mode, get_mode_one(mode));
2789 DBG_OPT_CSTEVAL(n, value);
2792 if (mode_is_signed(mode) && is_Const(b)) {
2793 tarval *tv = get_Const_tarval(b);
2795 if (tv == get_mode_minus_one(mode)) {
2797 value = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, get_nodes_block(n), a, mode);
2798 DBG_OPT_CSTEVAL(n, value);
2802 /* Try architecture dependent optimization */
2803 value = arch_dep_replace_div_by_const(n);
2811 /* Turn Div into a tuple (mem, jmp, bad, value) */
2812 mem = get_Div_mem(n);
2813 blk = get_nodes_block(n);
2815 /* skip a potential Pin */
2816 mem = skip_Pin(mem);
2817 turn_into_tuple(n, pn_Div_max);
2818 set_Tuple_pred(n, pn_Div_M, mem);
2819 set_Tuple_pred(n, pn_Div_X_regular, new_r_Jmp(current_ir_graph, blk));
2820 set_Tuple_pred(n, pn_Div_X_except, new_Bad());
2821 set_Tuple_pred(n, pn_Div_res, value);
2824 } /* transform_node_Div */
2827 * Transform a Mod node.
2829 static ir_node *transform_node_Mod(ir_node *n) {
2830 ir_mode *mode = get_Mod_resmode(n);
2831 ir_node *a = get_Mod_left(n);
2832 ir_node *b = get_Mod_right(n);
2836 if (is_Const(b) && is_const_Phi(a)) {
2837 /* check for Div(Phi, Const) */
2838 value = apply_binop_on_phi(a, get_Const_tarval(b), tarval_mod, mode, 0);
2840 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2844 else if (is_Const(a) && is_const_Phi(b)) {
2845 /* check for Div(Const, Phi) */
2846 value = apply_binop_on_phi(b, get_Const_tarval(a), tarval_mod, mode, 1);
2848 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2852 else if (is_const_Phi(a) && is_const_Phi(b)) {
2853 /* check for Div(Phi, Phi) */
2854 value = apply_binop_on_2_phis(a, b, tarval_mod, mode);
2856 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2863 if (tv != tarval_bad) {
2864 value = new_Const(get_tarval_mode(tv), tv);
2866 DBG_OPT_CSTEVAL(n, value);
2869 ir_node *a = get_Mod_left(n);
2870 ir_node *b = get_Mod_right(n);
2871 const ir_node *dummy;
2873 if (a == b && value_not_zero(a, &dummy)) {
2874 /* BEWARE: we can optimize a%a to 0 only if this cannot cause a exception */
2875 value = new_Const(mode, get_mode_null(mode));
2876 DBG_OPT_CSTEVAL(n, value);
2879 if (mode_is_signed(mode) && is_Const(b)) {
2880 tarval *tv = get_Const_tarval(b);
2882 if (tv == get_mode_minus_one(mode)) {
2884 value = new_Const(mode, get_mode_null(mode));
2885 DBG_OPT_CSTEVAL(n, value);
2889 /* Try architecture dependent optimization */
2890 value = arch_dep_replace_mod_by_const(n);
2898 /* Turn Mod into a tuple (mem, jmp, bad, value) */
2899 mem = get_Mod_mem(n);
2900 blk = get_nodes_block(n);
2902 /* skip a potential Pin */
2903 mem = skip_Pin(mem);
2904 turn_into_tuple(n, pn_Mod_max);
2905 set_Tuple_pred(n, pn_Mod_M, mem);
2906 set_Tuple_pred(n, pn_Mod_X_regular, new_r_Jmp(current_ir_graph, blk));
2907 set_Tuple_pred(n, pn_Mod_X_except, new_Bad());
2908 set_Tuple_pred(n, pn_Mod_res, value);
2911 } /* transform_node_Mod */
2914 * Transform a DivMod node.
2916 static ir_node *transform_node_DivMod(ir_node *n) {
2917 const ir_node *dummy;
2918 ir_node *a = get_DivMod_left(n);
2919 ir_node *b = get_DivMod_right(n);
2920 ir_mode *mode = get_DivMod_resmode(n);
2925 if (is_Const(b) && is_const_Phi(a)) {
2926 /* check for Div(Phi, Const) */
2927 va = apply_binop_on_phi(a, get_Const_tarval(b), tarval_div, mode, 0);
2928 vb = apply_binop_on_phi(a, get_Const_tarval(b), tarval_mod, mode, 0);
2930 DBG_OPT_ALGSIM0(n, va, FS_OPT_CONST_PHI);
2931 DBG_OPT_ALGSIM0(n, vb, FS_OPT_CONST_PHI);
2935 else if (is_Const(a) && is_const_Phi(b)) {
2936 /* check for Div(Const, Phi) */
2937 va = apply_binop_on_phi(b, get_Const_tarval(a), tarval_div, mode, 1);
2938 vb = apply_binop_on_phi(b, get_Const_tarval(a), tarval_mod, mode, 1);
2940 DBG_OPT_ALGSIM0(n, va, FS_OPT_CONST_PHI);
2941 DBG_OPT_ALGSIM0(n, vb, FS_OPT_CONST_PHI);
2945 else if (is_const_Phi(a) && is_const_Phi(b)) {
2946 /* check for Div(Phi, Phi) */
2947 va = apply_binop_on_2_phis(a, b, tarval_div, mode);
2948 vb = apply_binop_on_2_phis(a, b, tarval_mod, mode);
2950 DBG_OPT_ALGSIM0(n, va, FS_OPT_CONST_PHI);
2951 DBG_OPT_ALGSIM0(n, vb, FS_OPT_CONST_PHI);
2958 if (tb != tarval_bad) {
2959 if (tb == get_mode_one(get_tarval_mode(tb))) {
2961 vb = new_Const(mode, get_mode_null(mode));
2962 DBG_OPT_CSTEVAL(n, vb);
2964 } else if (ta != tarval_bad) {
2965 tarval *resa, *resb;
2966 resa = tarval_div(ta, tb);
2967 if (resa == tarval_bad) return n; /* Causes exception!!! Model by replacing through
2968 Jmp for X result!? */
2969 resb = tarval_mod(ta, tb);
2970 if (resb == tarval_bad) return n; /* Causes exception! */
2971 va = new_Const(mode, resa);
2972 vb = new_Const(mode, resb);
2973 DBG_OPT_CSTEVAL(n, va);
2974 DBG_OPT_CSTEVAL(n, vb);
2976 } else if (mode_is_signed(mode) && tb == get_mode_minus_one(mode)) {
2977 va = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, get_nodes_block(n), a, mode);
2978 vb = new_Const(mode, get_mode_null(mode));
2979 DBG_OPT_CSTEVAL(n, va);
2980 DBG_OPT_CSTEVAL(n, vb);
2982 } else { /* Try architecture dependent optimization */
2985 arch_dep_replace_divmod_by_const(&va, &vb, n);
2986 evaluated = va != NULL;
2988 } else if (a == b) {
2989 if (value_not_zero(a, &dummy)) {
2991 va = new_Const(mode, get_mode_one(mode));
2992 vb = new_Const(mode, get_mode_null(mode));
2993 DBG_OPT_CSTEVAL(n, va);
2994 DBG_OPT_CSTEVAL(n, vb);
2997 /* BEWARE: it is NOT possible to optimize a/a to 1, as this may cause a exception */
3000 } else if (ta == get_mode_null(mode) && value_not_zero(b, &dummy)) {
3001 /* 0 / non-Const = 0 */
3006 if (evaluated) { /* replace by tuple */
3010 mem = get_DivMod_mem(n);
3011 /* skip a potential Pin */
3012 mem = skip_Pin(mem);
3014 blk = get_nodes_block(n);
3015 turn_into_tuple(n, pn_DivMod_max);
3016 set_Tuple_pred(n, pn_DivMod_M, mem);
3017 set_Tuple_pred(n, pn_DivMod_X_regular, new_r_Jmp(current_ir_graph, blk));
3018 set_Tuple_pred(n, pn_DivMod_X_except, new_Bad()); /* no exception */
3019 set_Tuple_pred(n, pn_DivMod_res_div, va);
3020 set_Tuple_pred(n, pn_DivMod_res_mod, vb);
3024 } /* transform_node_DivMod */
3027 * Optimize x / c to x * (1/c)
3029 static ir_node *transform_node_Quot(ir_node *n) {
3030 ir_mode *mode = get_Quot_resmode(n);
3033 if (get_mode_arithmetic(mode) == irma_ieee754) {
3034 ir_node *b = get_Quot_right(n);
3035 tarval *tv = value_of(b);
3037 if (tv != tarval_bad) {
3041 * Floating point constant folding might be disabled here to
3043 * However, as we check for exact result, doing it is safe.
3046 rem = tarval_enable_fp_ops(1);
3047 tv = tarval_quo(get_mode_one(mode), tv);
3048 (void)tarval_enable_fp_ops(rem);
3050 /* Do the transformation if the result is either exact or we are not
3051 using strict rules. */
3052 if (tv != tarval_bad &&
3053 (tarval_ieee754_get_exact() || (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic) == 0)) {
3054 ir_node *blk = get_nodes_block(n);
3055 ir_node *c = new_r_Const(current_ir_graph, blk, mode, tv);
3056 ir_node *a = get_Quot_left(n);
3057 ir_node *m = new_rd_Mul(get_irn_dbg_info(n), current_ir_graph, blk, a, c, mode);
3058 ir_node *mem = get_Quot_mem(n);
3060 /* skip a potential Pin */
3061 mem = skip_Pin(mem);
3062 turn_into_tuple(n, pn_Quot_max);
3063 set_Tuple_pred(n, pn_Quot_M, mem);
3064 set_Tuple_pred(n, pn_Quot_X_regular, new_r_Jmp(current_ir_graph, blk));
3065 set_Tuple_pred(n, pn_Quot_X_except, new_r_Bad(current_ir_graph));
3066 set_Tuple_pred(n, pn_Quot_res, m);
3067 DBG_OPT_ALGSIM1(oldn, a, b, m, FS_OPT_FP_INV_MUL);
3072 } /* transform_node_Quot */
3075 * Optimize Abs(x) into x if x is Confirmed >= 0
3076 * Optimize Abs(x) into -x if x is Confirmed <= 0
3077 * Optimize Abs(-x) int Abs(x)
3079 static ir_node *transform_node_Abs(ir_node *n) {
3080 ir_node *c, *oldn = n;
3081 ir_node *a = get_Abs_op(n);
3084 HANDLE_UNOP_PHI(tarval_abs, a, c);
3086 switch (classify_value_sign(a)) {
3087 case value_classified_negative:
3088 mode = get_irn_mode(n);
3091 * We can replace the Abs by -x here.
3092 * We even could add a new Confirm here
3093 * (if not twos complement)
3095 * Note that -x would create a new node, so we could
3096 * not run it in the equivalent_node() context.
3098 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph,
3099 get_nodes_block(n), a, mode);
3101 DBG_OPT_CONFIRM(oldn, n);
3103 case value_classified_positive:
3104 /* n is positive, Abs is not needed */
3107 DBG_OPT_CONFIRM(oldn, n);
3113 /* Abs(-x) = Abs(x) */
3114 mode = get_irn_mode(n);
3115 n = new_rd_Abs(get_irn_dbg_info(n), current_ir_graph,
3116 get_nodes_block(n), get_Minus_op(a), mode);
3117 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ABS_MINUS_X);
3121 } /* transform_node_Abs */
3124 * Optimize -a CMP -b into b CMP a.
3125 * This works only for for modes where unary Minus
3127 * Note that two-complement integers can Overflow
3128 * so it will NOT work.
3130 * For == and != can be handled in Proj(Cmp)
3132 static ir_node *transform_node_Cmp(ir_node *n) {
3134 ir_node *left = get_Cmp_left(n);
3135 ir_node *right = get_Cmp_right(n);
3137 if (is_Minus(left) && is_Minus(right) &&
3138 !mode_overflow_on_unary_Minus(get_irn_mode(left))) {
3139 ir_node *const new_left = get_Minus_op(right);
3140 ir_node *const new_right = get_Minus_op(left);
3141 n = new_rd_Cmp(get_irn_dbg_info(n), current_ir_graph,
3142 get_nodes_block(n), new_left, new_right);
3143 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CMP_OP_OP);
3146 } /* transform_node_Cmp */
3150 * Transform a Cond node.
3152 * Replace the Cond by a Jmp if it branches on a constant
3155 static ir_node *transform_node_Cond(ir_node *n) {
3158 ir_node *a = get_Cond_selector(n);
3159 tarval *ta = value_of(a);
3161 /* we need block info which is not available in floating irgs */
3162 if (get_irg_pinned(current_ir_graph) == op_pin_state_floats)
3165 if ((ta != tarval_bad) &&
3166 (get_irn_mode(a) == mode_b) &&
3167 (get_opt_unreachable_code())) {
3168 /* It's a boolean Cond, branching on a boolean constant.
3169 Replace it by a tuple (Bad, Jmp) or (Jmp, Bad) */
3170 ir_node *blk = get_nodes_block(n);
3171 jmp = new_r_Jmp(current_ir_graph, blk);
3172 turn_into_tuple(n, pn_Cond_max);
3173 if (ta == tarval_b_true) {
3174 set_Tuple_pred(n, pn_Cond_false, new_Bad());
3175 set_Tuple_pred(n, pn_Cond_true, jmp);
3177 set_Tuple_pred(n, pn_Cond_false, jmp);
3178 set_Tuple_pred(n, pn_Cond_true, new_Bad());
3180 /* We might generate an endless loop, so keep it alive. */
3181 add_End_keepalive(get_irg_end(current_ir_graph), blk);
3184 } /* transform_node_Cond */
3187 * Prototype of a recursive transform function
3188 * for bitwise distributive transformations.
3190 typedef ir_node* (*recursive_transform)(ir_node *n);
3193 * makes use of distributive laws for and, or, eor
3194 * and(a OP c, b OP c) -> and(a, b) OP c
3195 * note, might return a different op than n
3197 static ir_node *transform_bitwise_distributive(ir_node *n,
3198 recursive_transform trans_func)
3201 ir_node *a = get_binop_left(n);
3202 ir_node *b = get_binop_right(n);
3203 ir_op *op = get_irn_op(a);
3204 ir_op *op_root = get_irn_op(n);
3206 if(op != get_irn_op(b))
3209 if (op == op_Conv) {
3210 ir_node *a_op = get_Conv_op(a);
3211 ir_node *b_op = get_Conv_op(b);
3212 ir_mode *a_mode = get_irn_mode(a_op);
3213 ir_mode *b_mode = get_irn_mode(b_op);
3214 if(a_mode == b_mode && (mode_is_int(a_mode) || a_mode == mode_b)) {
3215 ir_node *blk = get_nodes_block(n);
3218 set_binop_left(n, a_op);
3219 set_binop_right(n, b_op);
3220 set_irn_mode(n, a_mode);
3222 n = new_r_Conv(current_ir_graph, blk, n, get_irn_mode(oldn));
3224 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_SHIFT_AND);
3230 /* nothing to gain here */
3234 if (op == op_Shrs || op == op_Shr || op == op_Shl
3235 || op == op_And || op == op_Or || op == op_Eor) {
3236 ir_node *a_left = get_binop_left(a);
3237 ir_node *a_right = get_binop_right(a);
3238 ir_node *b_left = get_binop_left(b);
3239 ir_node *b_right = get_binop_right(b);
3241 ir_node *op1 = NULL;
3242 ir_node *op2 = NULL;
3244 if (is_op_commutative(op)) {
3245 if (a_left == b_left) {
3249 } else if(a_left == b_right) {
3253 } else if(a_right == b_left) {
3259 if(a_right == b_right) {
3266 /* (a sop c) & (b sop c) => (a & b) sop c */
3267 ir_node *blk = get_nodes_block(n);
3269 ir_node *new_n = exact_copy(n);
3270 set_binop_left(new_n, op1);
3271 set_binop_right(new_n, op2);
3272 new_n = trans_func(new_n);
3274 if(op_root == op_Eor && op == op_Or) {
3275 dbg_info *dbgi = get_irn_dbg_info(n);
3276 ir_graph *irg = current_ir_graph;
3277 ir_mode *mode = get_irn_mode(c);
3279 c = new_rd_Not(dbgi, irg, blk, c, mode);
3280 n = new_rd_And(dbgi, irg, blk, new_n, c, mode);
3283 set_nodes_block(n, blk);
3284 set_binop_left(n, new_n);
3285 set_binop_right(n, c);
3286 add_identities(current_ir_graph->value_table, n);
3289 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_SHIFT_AND);
3300 static ir_node *transform_node_And(ir_node *n) {
3301 ir_node *c, *oldn = n;
3302 ir_node *a = get_And_left(n);
3303 ir_node *b = get_And_right(n);
3306 mode = get_irn_mode(n);
3307 HANDLE_BINOP_PHI(tarval_and, a, b, c, mode);
3309 /* we can evaluate 2 Projs of the same Cmp */
3310 if (mode == mode_b && is_Proj(a) && is_Proj(b)) {
3311 ir_node *pred_a = get_Proj_pred(a);
3312 ir_node *pred_b = get_Proj_pred(b);
3313 if (pred_a == pred_b) {
3314 dbg_info *dbgi = get_irn_dbg_info(n);
3315 ir_node *block = get_nodes_block(pred_a);
3316 pn_Cmp pn_a = get_Proj_proj(a);
3317 pn_Cmp pn_b = get_Proj_proj(b);
3318 /* yes, we can simply calculate with pncs */
3319 pn_Cmp new_pnc = pn_a & pn_b;
3321 return new_rd_Proj(dbgi, current_ir_graph, block, pred_a, mode_b, new_pnc);
3326 ir_node *op = get_Not_op(b);
3328 ir_node *ba = get_And_left(op);
3329 ir_node *bb = get_And_right(op);
3331 /* it's enough to test the following cases due to normalization! */
3332 if (get_Or_left(a) == ba && get_Or_right(a) == bb) {
3333 /* (a|b) & ~(a&b) = a^b */
3334 ir_node *block = get_nodes_block(n);
3336 n = new_rd_Eor(get_irn_dbg_info(n), current_ir_graph, block, ba, bb, mode);
3337 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_TO_EOR);
3345 ir_node *op = get_Not_op(a);
3347 ir_node *aa = get_And_left(op);
3348 ir_node *ab = get_And_right(op);
3350 /* it's enough to test the following cases due to normalization! */
3351 if (get_Or_left(b) == aa && get_Or_right(b) == ab) {
3352 /* (a|b) & ~(a&b) = a^b */
3353 ir_node *block = get_nodes_block(n);
3355 n = new_rd_Eor(get_irn_dbg_info(n), current_ir_graph, block, aa, ab, mode);
3356 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_TO_EOR);
3363 ir_node *al = get_Eor_left(a);
3364 ir_node *ar = get_Eor_right(a);
3367 /* (b ^ a) & b -> ~a & b */
3368 dbg_info *dbg = get_irn_dbg_info(n);
3369 ir_node *block = get_nodes_block(n);
3371 ar = new_rd_Not(dbg, current_ir_graph, block, ar, mode);
3372 n = new_rd_And(dbg, current_ir_graph, block, ar, b, mode);
3373 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3377 /* (a ^ b) & b -> ~a & b */
3378 dbg_info *dbg = get_irn_dbg_info(n);
3379 ir_node *block = get_nodes_block(n);
3381 al = new_rd_Not(dbg, current_ir_graph, block, al, mode);
3382 n = new_rd_And(dbg, current_ir_graph, block, al, b, mode);
3383 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3388 ir_node *bl = get_Eor_left(b);
3389 ir_node *br = get_Eor_right(b);
3392 /* a & (a ^ b) -> a & ~b */
3393 dbg_info *dbg = get_irn_dbg_info(n);
3394 ir_node *block = get_nodes_block(n);
3396 br = new_rd_Not(dbg, current_ir_graph, block, br, mode);
3397 n = new_rd_And(dbg, current_ir_graph, block, br, a, mode);
3398 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3402 /* a & (b ^ a) -> a & ~b */
3403 dbg_info *dbg = get_irn_dbg_info(n);
3404 ir_node *block = get_nodes_block(n);
3406 bl = new_rd_Not(dbg, current_ir_graph, block, bl, mode);
3407 n = new_rd_And(dbg, current_ir_graph, block, bl, a, mode);
3408 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3412 if (is_Not(a) && is_Not(b)) {
3413 /* ~a & ~b = ~(a|b) */
3414 ir_node *block = get_nodes_block(n);
3415 ir_mode *mode = get_irn_mode(n);
3419 n = new_rd_Or(get_irn_dbg_info(n), current_ir_graph, block, a, b, mode);
3420 n = new_rd_Not(get_irn_dbg_info(n), current_ir_graph, block, n, mode);
3421 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_DEMORGAN);
3425 n = transform_bitwise_distributive(n, transform_node_And);
3428 } /* transform_node_And */
3433 static ir_node *transform_node_Eor(ir_node *n) {
3434 ir_node *c, *oldn = n;
3435 ir_node *a = get_Eor_left(n);
3436 ir_node *b = get_Eor_right(n);
3437 ir_mode *mode = get_irn_mode(n);
3439 HANDLE_BINOP_PHI(tarval_eor, a, b, c, mode);
3441 /* we can evaluate 2 Projs of the same Cmp */
3442 if (mode == mode_b && is_Proj(a) && is_Proj(b)) {
3443 ir_node *pred_a = get_Proj_pred(a);
3444 ir_node *pred_b = get_Proj_pred(b);
3445 if(pred_a == pred_b) {
3446 dbg_info *dbgi = get_irn_dbg_info(n);
3447 ir_node *block = get_nodes_block(pred_a);
3448 pn_Cmp pn_a = get_Proj_proj(a);
3449 pn_Cmp pn_b = get_Proj_proj(b);
3450 /* yes, we can simply calculate with pncs */
3451 pn_Cmp new_pnc = pn_a ^ pn_b;
3453 return new_rd_Proj(dbgi, current_ir_graph, block, pred_a, mode_b,
3460 n = new_rd_Const(get_irn_dbg_info(n), current_ir_graph, get_nodes_block(n),
3461 mode, get_mode_null(mode));
3462 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_A_A);
3463 } else if (mode == mode_b &&
3465 is_Const(b) && is_Const_one(b) &&
3466 is_Cmp(get_Proj_pred(a))) {
3467 /* The Eor negates a Cmp. The Cmp has the negated result anyways! */
3468 n = new_r_Proj(current_ir_graph, get_nodes_block(n), get_Proj_pred(a),
3469 mode_b, get_negated_pnc(get_Proj_proj(a), mode));
3471 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT_BOOL);
3472 } else if (is_Const(b)) {
3473 if (is_Not(a)) { /* ~x ^ const -> x ^ ~const */
3474 ir_node *cnst = new_Const(mode, tarval_not(get_Const_tarval(b)));
3475 ir_node *not_op = get_Not_op(a);
3476 dbg_info *dbg = get_irn_dbg_info(n);
3477 ir_graph *irg = current_ir_graph;
3478 ir_node *block = get_nodes_block(n);
3479 ir_mode *mode = get_irn_mode(n);
3480 n = new_rd_Eor(dbg, irg, block, not_op, cnst, mode);
3482 } else if (is_Const_all_one(b)) { /* x ^ 1...1 -> ~1 */
3483 n = new_r_Not(current_ir_graph, get_nodes_block(n), a, mode);
3484 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3487 n = transform_bitwise_distributive(n, transform_node_Eor);
3491 } /* transform_node_Eor */
3496 static ir_node *transform_node_Not(ir_node *n) {
3497 ir_node *c, *oldn = n;
3498 ir_node *a = get_Not_op(n);
3499 ir_mode *mode = get_irn_mode(n);
3501 HANDLE_UNOP_PHI(tarval_not,a,c);
3503 /* check for a boolean Not */
3504 if (mode == mode_b &&
3506 is_Cmp(get_Proj_pred(a))) {
3507 /* We negate a Cmp. The Cmp has the negated result anyways! */
3508 n = new_r_Proj(current_ir_graph, get_nodes_block(n), get_Proj_pred(a),
3509 mode_b, get_negated_pnc(get_Proj_proj(a), mode_b));
3510 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_CMP);
3514 ir_node *eor_b = get_Eor_right(a);
3515 if (is_Const(eor_b)) { /* ~(x ^ const) -> x ^ ~const */
3516 ir_node *cnst = new_Const(mode, tarval_not(get_Const_tarval(eor_b)));
3517 ir_node *eor_a = get_Eor_left(a);
3518 dbg_info *dbg = get_irn_dbg_info(n);
3519 ir_graph *irg = current_ir_graph;
3520 ir_node *block = get_nodes_block(n);
3521 ir_mode *mode = get_irn_mode(n);
3522 n = new_rd_Eor(dbg, irg, block, eor_a, cnst, mode);
3526 if (get_mode_arithmetic(mode) == irma_twos_complement) {
3527 if (is_Minus(a)) { /* ~-x -> x + -1 */
3528 dbg_info *dbg = get_irn_dbg_info(n);
3529 ir_graph *irg = current_ir_graph;
3530 ir_node *block = get_nodes_block(n);
3531 ir_node *add_l = get_Minus_op(a);
3532 ir_node *add_r = new_rd_Const(dbg, irg, block, mode, get_mode_minus_one(mode));
3533 n = new_rd_Add(dbg, irg, block, add_l, add_r, mode);
3534 } else if (is_Add(a)) {
3535 ir_node *add_r = get_Add_right(a);
3536 if (is_Const(add_r) && is_Const_all_one(add_r)) {
3537 /* ~(x + -1) = -x */
3538 ir_node *op = get_Add_left(a);
3539 ir_node *blk = get_nodes_block(n);
3540 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, blk, op, get_irn_mode(n));
3541 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_MINUS_1);
3546 } /* transform_node_Not */
3549 * Transform a Minus.
3553 * -(a >>u (size-1)) = a >>s (size-1)
3554 * -(a >>s (size-1)) = a >>u (size-1)
3555 * -(a * const) -> a * -const
3557 static ir_node *transform_node_Minus(ir_node *n) {
3558 ir_node *c, *oldn = n;
3559 ir_node *a = get_Minus_op(n);
3562 HANDLE_UNOP_PHI(tarval_neg,a,c);
3564 mode = get_irn_mode(a);
3565 if (get_mode_arithmetic(mode) == irma_twos_complement) {
3566 /* the following rules are only to twos-complement */
3569 ir_node *op = get_Not_op(a);
3570 tarval *tv = get_mode_one(mode);
3571 ir_node *blk = get_nodes_block(n);
3572 ir_node *c = new_r_Const(current_ir_graph, blk, mode, tv);
3573 n = new_rd_Add(get_irn_dbg_info(n), current_ir_graph, blk, op, c, mode);
3574 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_MINUS_NOT);
3578 ir_node *c = get_Shr_right(a);
3581 tarval *tv = get_Const_tarval(c);
3583 if (tarval_is_long(tv) && get_tarval_long(tv) == (int) get_mode_size_bits(mode) - 1) {
3584 /* -(a >>u (size-1)) = a >>s (size-1) */
3585 ir_node *v = get_Shr_left(a);
3587 n = new_rd_Shrs(get_irn_dbg_info(n), current_ir_graph, get_nodes_block(n), v, c, mode);
3588 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_PREDICATE);
3594 ir_node *c = get_Shrs_right(a);
3597 tarval *tv = get_Const_tarval(c);
3599 if (tarval_is_long(tv) && get_tarval_long(tv) == (int) get_mode_size_bits(mode) - 1) {
3600 /* -(a >>s (size-1)) = a >>u (size-1) */
3601 ir_node *v = get_Shrs_left(a);
3603 n = new_rd_Shr(get_irn_dbg_info(n), current_ir_graph, get_nodes_block(n), v, c, mode);
3604 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_PREDICATE);
3611 /* - (a-b) = b - a */
3612 ir_node *la = get_Sub_left(a);
3613 ir_node *ra = get_Sub_right(a);
3614 ir_node *blk = get_nodes_block(n);
3616 n = new_rd_Sub(get_irn_dbg_info(n), current_ir_graph, blk, ra, la, mode);
3617 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_MINUS_SUB);
3621 if (is_Mul(a)) { /* -(a * const) -> a * -const */
3622 ir_node *mul_l = get_Mul_left(a);
3623 ir_node *mul_r = get_Mul_right(a);
3624 tarval *tv = value_of(mul_r);
3625 if (tv != tarval_bad) {
3626 tv = tarval_neg(tv);
3627 if (tv != tarval_bad) {
3628 ir_node *cnst = new_Const(mode, tv);
3629 dbg_info *dbg = get_irn_dbg_info(a);
3630 ir_graph *irg = current_ir_graph;
3631 ir_node *block = get_nodes_block(a);
3632 n = new_rd_Mul(dbg, irg, block, mul_l, cnst, mode);
3633 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_MINUS_MUL_C);
3640 } /* transform_node_Minus */
3643 * Transform a Cast_type(Const) into a new Const_type
3645 static ir_node *transform_node_Cast(ir_node *n) {
3647 ir_node *pred = get_Cast_op(n);
3648 ir_type *tp = get_irn_type(n);
3650 if (is_Const(pred) && get_Const_type(pred) != tp) {
3651 n = new_rd_Const_type(NULL, current_ir_graph, get_irn_n(pred, -1), get_irn_mode(pred),
3652 get_Const_tarval(pred), tp);
3653 DBG_OPT_CSTEVAL(oldn, n);
3654 } else if (is_SymConst(pred) && get_SymConst_value_type(pred) != tp) {
3655 n = new_rd_SymConst_type(NULL, current_ir_graph, get_irn_n(pred, -1), get_irn_mode(pred),
3656 get_SymConst_symbol(pred), get_SymConst_kind(pred), tp);
3657 DBG_OPT_CSTEVAL(oldn, n);
3661 } /* transform_node_Cast */
3664 * Transform a Proj(Load) with a non-null address.
3666 static ir_node *transform_node_Proj_Load(ir_node *proj) {
3667 if (get_opt_ldst_only_null_ptr_exceptions()) {
3668 if (get_irn_mode(proj) == mode_X) {
3669 ir_node *load = get_Proj_pred(proj);
3671 /* get the Load address */
3672 const ir_node *addr = get_Load_ptr(load);
3673 const ir_node *confirm;
3675 if (value_not_null(addr, &confirm)) {
3676 if (confirm == NULL) {
3677 /* this node may float if it did not depend on a Confirm */
3678 set_irn_pinned(load, op_pin_state_floats);
3680 if (get_Proj_proj(proj) == pn_Load_X_except) {
3681 DBG_OPT_EXC_REM(proj);
3682 return get_irg_bad(current_ir_graph);
3684 ir_node *blk = get_nodes_block(load);
3685 return new_r_Jmp(current_ir_graph, blk);
3691 } /* transform_node_Proj_Load */
3694 * Transform a Proj(Store) with a non-null address.
3696 static ir_node *transform_node_Proj_Store(ir_node *proj) {
3697 if (get_opt_ldst_only_null_ptr_exceptions()) {
3698 if (get_irn_mode(proj) == mode_X) {
3699 ir_node *store = get_Proj_pred(proj);
3701 /* get the load/store address */
3702 const ir_node *addr = get_Store_ptr(store);
3703 const ir_node *confirm;
3705 if (value_not_null(addr, &confirm)) {
3706 if (confirm == NULL) {
3707 /* this node may float if it did not depend on a Confirm */
3708 set_irn_pinned(store, op_pin_state_floats);
3710 if (get_Proj_proj(proj) == pn_Store_X_except) {
3711 DBG_OPT_EXC_REM(proj);
3712 return get_irg_bad(current_ir_graph);
3714 ir_node *blk = get_nodes_block(store);
3715 return new_r_Jmp(current_ir_graph, blk);
3721 } /* transform_node_Proj_Store */
3724 * Transform a Proj(Div) with a non-zero value.
3725 * Removes the exceptions and routes the memory to the NoMem node.
3727 static ir_node *transform_node_Proj_Div(ir_node *proj) {
3728 ir_node *div = get_Proj_pred(proj);
3729 ir_node *b = get_Div_right(div);
3730 ir_node *res, *new_mem;
3731 const ir_node *confirm;
3734 if (value_not_zero(b, &confirm)) {
3735 /* div(x, y) && y != 0 */
3736 if (confirm == NULL) {
3737 /* we are sure we have a Const != 0 */
3738 new_mem = get_Div_mem(div);
3739 new_mem = skip_Pin(new_mem);
3740 set_Div_mem(div, new_mem);
3741 set_irn_pinned(div, op_pin_state_floats);
3744 proj_nr = get_Proj_proj(proj);
3746 case pn_Div_X_regular:
3747 return new_r_Jmp(current_ir_graph, get_irn_n(div, -1));
3749 case pn_Div_X_except:
3750 /* we found an exception handler, remove it */
3751 DBG_OPT_EXC_REM(proj);
3755 res = get_Div_mem(div);
3756 new_mem = get_irg_no_mem(current_ir_graph);
3759 /* This node can only float up to the Confirm block */
3760 new_mem = new_r_Pin(current_ir_graph, get_nodes_block(confirm), new_mem);
3762 set_irn_pinned(div, op_pin_state_floats);
3763 /* this is a Div without exception, we can remove the memory edge */
3764 set_Div_mem(div, new_mem);
3769 } /* transform_node_Proj_Div */
3772 * Transform a Proj(Mod) with a non-zero value.
3773 * Removes the exceptions and routes the memory to the NoMem node.
3775 static ir_node *transform_node_Proj_Mod(ir_node *proj) {
3776 ir_node *mod = get_Proj_pred(proj);
3777 ir_node *b = get_Mod_right(mod);
3778 ir_node *res, *new_mem;
3779 const ir_node *confirm;
3782 if (value_not_zero(b, &confirm)) {
3783 /* mod(x, y) && y != 0 */
3784 proj_nr = get_Proj_proj(proj);
3786 if (confirm == NULL) {
3787 /* we are sure we have a Const != 0 */
3788 new_mem = get_Mod_mem(mod);
3789 new_mem = skip_Pin(new_mem);
3790 set_Mod_mem(mod, new_mem);
3791 set_irn_pinned(mod, op_pin_state_floats);
3796 case pn_Mod_X_regular:
3797 return new_r_Jmp(current_ir_graph, get_irn_n(mod, -1));
3799 case pn_Mod_X_except:
3800 /* we found an exception handler, remove it */
3801 DBG_OPT_EXC_REM(proj);
3805 res = get_Mod_mem(mod);
3806 new_mem = get_irg_no_mem(current_ir_graph);
3809 /* This node can only float up to the Confirm block */
3810 new_mem = new_r_Pin(current_ir_graph, get_nodes_block(confirm), new_mem);
3812 /* this is a Mod without exception, we can remove the memory edge */
3813 set_Mod_mem(mod, new_mem);
3816 if (get_Mod_left(mod) == b) {
3817 /* a % a = 0 if a != 0 */
3818 ir_mode *mode = get_irn_mode(proj);
3819 ir_node *res = new_Const(mode, get_mode_null(mode));
3821 DBG_OPT_CSTEVAL(mod, res);
3827 } /* transform_node_Proj_Mod */
3830 * Transform a Proj(DivMod) with a non-zero value.
3831 * Removes the exceptions and routes the memory to the NoMem node.
3833 static ir_node *transform_node_Proj_DivMod(ir_node *proj) {
3834 ir_node *divmod = get_Proj_pred(proj);
3835 ir_node *b = get_DivMod_right(divmod);
3836 ir_node *res, *new_mem;
3837 const ir_node *confirm;
3840 if (value_not_zero(b, &confirm)) {
3841 /* DivMod(x, y) && y != 0 */
3842 proj_nr = get_Proj_proj(proj);
3844 if (confirm == NULL) {
3845 /* we are sure we have a Const != 0 */
3846 new_mem = get_DivMod_mem(divmod);
3847 new_mem = skip_Pin(new_mem);
3848 set_DivMod_mem(divmod, new_mem);
3849 set_irn_pinned(divmod, op_pin_state_floats);
3854 case pn_DivMod_X_regular:
3855 return new_r_Jmp(current_ir_graph, get_irn_n(divmod, -1));
3857 case pn_DivMod_X_except:
3858 /* we found an exception handler, remove it */
3859 DBG_OPT_EXC_REM(proj);
3863 res = get_DivMod_mem(divmod);
3864 new_mem = get_irg_no_mem(current_ir_graph);
3867 /* This node can only float up to the Confirm block */
3868 new_mem = new_r_Pin(current_ir_graph, get_nodes_block(confirm), new_mem);
3870 /* this is a DivMod without exception, we can remove the memory edge */
3871 set_DivMod_mem(divmod, new_mem);
3874 case pn_DivMod_res_mod:
3875 if (get_DivMod_left(divmod) == b) {
3876 /* a % a = 0 if a != 0 */
3877 ir_mode *mode = get_irn_mode(proj);
3878 ir_node *res = new_Const(mode, get_mode_null(mode));
3880 DBG_OPT_CSTEVAL(divmod, res);
3886 } /* transform_node_Proj_DivMod */
3889 * Optimizes jump tables (CondIs or CondIu) by removing all impossible cases.
3891 static ir_node *transform_node_Proj_Cond(ir_node *proj) {
3892 if (get_opt_unreachable_code()) {
3893 ir_node *n = get_Proj_pred(proj);
3894 ir_node *b = get_Cond_selector(n);
3896 if (mode_is_int(get_irn_mode(b))) {
3897 tarval *tb = value_of(b);
3899 if (tb != tarval_bad) {
3900 /* we have a constant switch */
3901 long num = get_Proj_proj(proj);
3903 if (num != get_Cond_defaultProj(n)) { /* we cannot optimize default Proj's yet */
3904 if (get_tarval_long(tb) == num) {
3905 /* Do NOT create a jump here, or we will have 2 control flow ops
3906 * in a block. This case is optimized away in optimize_cf(). */
3909 /* this case will NEVER be taken, kill it */
3910 return get_irg_bad(current_ir_graph);
3917 } /* transform_node_Proj_Cond */
3920 * Create a 0 constant of given mode.
3922 static ir_node *create_zero_const(ir_mode *mode) {
3923 tarval *tv = get_mode_null(mode);
3924 ir_node *cnst = new_Const(mode, tv);
3929 /* the order of the values is important! */
3930 typedef enum const_class {
3936 static const_class classify_const(const ir_node* n)
3938 if (is_Const(n)) return const_const;
3939 if (is_irn_constlike(n)) return const_like;
3944 * Determines whether r is more constlike or has a larger index (in that order)
3947 static int operands_are_normalized(const ir_node *l, const ir_node *r)
3949 const const_class l_order = classify_const(l);
3950 const const_class r_order = classify_const(r);
3952 l_order > r_order ||
3953 (l_order == r_order && get_irn_idx(l) <= get_irn_idx(r));
3957 * Normalizes and optimizes Cmp nodes.
3959 static ir_node *transform_node_Proj_Cmp(ir_node *proj) {
3960 ir_node *n = get_Proj_pred(proj);
3961 ir_node *left = get_Cmp_left(n);
3962 ir_node *right = get_Cmp_right(n);
3965 ir_mode *mode = NULL;
3966 long proj_nr = get_Proj_proj(proj);
3968 /* we can evaluate some cases directly */
3971 return new_Const(mode_b, get_tarval_b_false());
3973 return new_Const(mode_b, get_tarval_b_true());
3975 if (!mode_is_float(get_irn_mode(left)))
3976 return new_Const(mode_b, get_tarval_b_true());
3982 /* remove Casts of both sides */
3983 left = skip_Cast(left);
3984 right = skip_Cast(right);
3986 /* Remove unnecessary conversions */
3987 /* TODO handle constants */
3988 if (is_Conv(left) && is_Conv(right)) {
3989 ir_mode *mode = get_irn_mode(left);
3990 ir_node *op_left = get_Conv_op(left);
3991 ir_node *op_right = get_Conv_op(right);
3992 ir_mode *mode_left = get_irn_mode(op_left);
3993 ir_mode *mode_right = get_irn_mode(op_right);
3995 if (smaller_mode(mode_left, mode) && smaller_mode(mode_right, mode)
3996 && mode_left != mode_b && mode_right != mode_b) {
3997 ir_graph *irg = current_ir_graph;
3998 ir_node *block = get_nodes_block(n);
4000 if (mode_left == mode_right) {
4004 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV_CONV);
4005 } else if (smaller_mode(mode_left, mode_right)) {
4006 left = new_r_Conv(irg, block, op_left, mode_right);
4009 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV);
4010 } else if (smaller_mode(mode_right, mode_left)) {
4012 right = new_r_Conv(irg, block, op_right, mode_left);
4014 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV);
4019 /* remove operation on both sides if possible */
4020 if (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) {
4022 * The following operations are NOT safe for floating point operations, for instance
4023 * 1.0 + inf == 2.0 + inf, =/=> x == y
4025 if (mode_is_int(get_irn_mode(left))) {
4026 unsigned lop = get_irn_opcode(left);
4028 if (lop == get_irn_opcode(right)) {
4029 ir_node *ll, *lr, *rl, *rr;
4031 /* same operation on both sides, try to remove */
4035 /* ~a CMP ~b => a CMP b, -a CMP -b ==> a CMP b */
4036 left = get_unop_op(left);
4037 right = get_unop_op(right);
4039 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4042 ll = get_Add_left(left);
4043 lr = get_Add_right(left);
4044 rl = get_Add_left(right);
4045 rr = get_Add_right(right);
4048 /* X + a CMP X + b ==> a CMP b */
4052 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4053 } else if (ll == rr) {
4054 /* X + a CMP b + X ==> a CMP b */
4058 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4059 } else if (lr == rl) {
4060 /* a + X CMP X + b ==> a CMP b */
4064 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4065 } else if (lr == rr) {
4066 /* a + X CMP b + X ==> a CMP b */
4070 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4074 ll = get_Sub_left(left);
4075 lr = get_Sub_right(left);
4076 rl = get_Sub_left(right);
4077 rr = get_Sub_right(right);
4080 /* X - a CMP X - b ==> a CMP b */
4084 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4085 } else if (lr == rr) {
4086 /* a - X CMP b - X ==> a CMP b */
4090 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4094 if (get_Rotl_right(left) == get_Rotl_right(right)) {
4095 /* a ROTL X CMP b ROTL X ==> a CMP b */
4096 left = get_Rotl_left(left);
4097 right = get_Rotl_left(right);
4099 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4107 /* X+A == A, A+X == A, A-X == A -> X == 0 */
4108 if (is_Add(left) || is_Sub(left)) {
4109 ir_node *ll = get_binop_left(left);
4110 ir_node *lr = get_binop_right(left);
4112 if (lr == right && is_Add(left)) {
4119 right = create_zero_const(get_irn_mode(left));
4121 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4124 if (is_Add(right) || is_Sub(right)) {
4125 ir_node *rl = get_binop_left(right);
4126 ir_node *rr = get_binop_right(right);
4128 if (rr == left && is_Add(right)) {
4135 right = create_zero_const(get_irn_mode(left));
4137 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4140 } /* mode_is_int(...) */
4141 } /* proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg */
4143 /* replace mode_b compares with ands/ors */
4144 if (get_irn_mode(left) == mode_b) {
4145 ir_graph *irg = current_ir_graph;
4146 ir_node *block = get_nodes_block(n);
4150 case pn_Cmp_Le: bres = new_r_Or( irg, block, new_r_Not(irg, block, left, mode_b), right, mode_b); break;
4151 case pn_Cmp_Lt: bres = new_r_And(irg, block, new_r_Not(irg, block, left, mode_b), right, mode_b); break;
4152 case pn_Cmp_Ge: bres = new_r_Or( irg, block, left, new_r_Not(irg, block, right, mode_b), mode_b); break;
4153 case pn_Cmp_Gt: bres = new_r_And(irg, block, left, new_r_Not(irg, block, right, mode_b), mode_b); break;
4154 case pn_Cmp_Lg: bres = new_r_Eor(irg, block, left, right, mode_b); break;
4155 case pn_Cmp_Eq: bres = new_r_Not(irg, block, new_r_Eor(irg, block, left, right, mode_b), mode_b); break;
4156 default: bres = NULL;
4159 DBG_OPT_ALGSIM0(n, bres, FS_OPT_CMP_TO_BOOL);
4165 * First step: normalize the compare op
4166 * by placing the constant on the right side
4167 * or moving the lower address node to the left.
4169 if (!operands_are_normalized(left, right)) {
4175 proj_nr = get_inversed_pnc(proj_nr);
4180 * Second step: Try to reduce the magnitude
4181 * of a constant. This may help to generate better code
4182 * later and may help to normalize more compares.
4183 * Of course this is only possible for integer values.
4185 tv = value_of(right);
4186 if (tv != tarval_bad) {
4187 mode = get_irn_mode(right);
4189 /* TODO extend to arbitrary constants */
4190 if (is_Conv(left) && tarval_is_null(tv)) {
4191 ir_node *op = get_Conv_op(left);
4192 ir_mode *op_mode = get_irn_mode(op);
4195 * UpConv(x) REL 0 ==> x REL 0
4197 if (get_mode_size_bits(mode) > get_mode_size_bits(op_mode) &&
4198 ((proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) ||
4199 mode_is_signed(mode) || !mode_is_signed(op_mode))) {
4200 tv = get_mode_null(op_mode);
4204 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV);
4208 if (tv != tarval_bad) {
4209 /* the following optimization is possible on modes without Overflow
4210 * on Unary Minus or on == and !=:
4211 * -a CMP c ==> a swap(CMP) -c
4213 * Beware: for two-complement Overflow may occur, so only == and != can
4214 * be optimized, see this:
4215 * -MININT < 0 =/=> MININT > 0 !!!
4217 if (is_Minus(left) &&
4218 (!mode_overflow_on_unary_Minus(mode) ||
4219 (mode_is_int(mode) && (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg)))) {
4220 tv = tarval_neg(tv);
4222 if (tv != tarval_bad) {
4223 left = get_Minus_op(left);
4224 proj_nr = get_inversed_pnc(proj_nr);
4226 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4228 } else if (is_Not(left) && (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg)) {
4229 /* Not(a) ==/!= c ==> a ==/!= Not(c) */
4230 tv = tarval_not(tv);
4232 if (tv != tarval_bad) {
4233 left = get_Not_op(left);
4235 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4239 /* for integer modes, we have more */
4240 if (mode_is_int(mode)) {
4241 /* Ne includes Unordered which is not possible on integers.
4242 * However, frontends often use this wrong, so fix it here */
4243 if (proj_nr & pn_Cmp_Uo) {
4244 proj_nr &= ~pn_Cmp_Uo;
4245 set_Proj_proj(proj, proj_nr);
4248 /* c > 0 : a < c ==> a <= (c-1) a >= c ==> a > (c-1) */
4249 if ((proj_nr == pn_Cmp_Lt || proj_nr == pn_Cmp_Ge) &&
4250 tarval_cmp(tv, get_mode_null(mode)) == pn_Cmp_Gt) {
4251 tv = tarval_sub(tv, get_mode_one(mode), NULL);
4253 if (tv != tarval_bad) {
4254 proj_nr ^= pn_Cmp_Eq;
4256 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CNST_MAGN);
4259 /* c < 0 : a > c ==> a >= (c+1) a <= c ==> a < (c+1) */
4260 else if ((proj_nr == pn_Cmp_Gt || proj_nr == pn_Cmp_Le) &&
4261 tarval_cmp(tv, get_mode_null(mode)) == pn_Cmp_Lt) {
4262 tv = tarval_add(tv, get_mode_one(mode));
4264 if (tv != tarval_bad) {
4265 proj_nr ^= pn_Cmp_Eq;
4267 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CNST_MAGN);
4271 /* the following reassociations work only for == and != */
4272 if (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) {
4274 #if 0 /* Might be not that good in general */
4275 /* a-b == 0 ==> a == b, a-b != 0 ==> a != b */
4276 if (tarval_is_null(tv) && is_Sub(left)) {
4277 right = get_Sub_right(left);
4278 left = get_Sub_left(left);
4280 tv = value_of(right);
4282 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4286 if (tv != tarval_bad) {
4287 /* a-c1 == c2 ==> a == c2+c1, a-c1 != c2 ==> a != c2+c1 */
4289 ir_node *c1 = get_Sub_right(left);
4290 tarval *tv2 = value_of(c1);
4292 if (tv2 != tarval_bad) {
4293 tv2 = tarval_add(tv, value_of(c1));
4295 if (tv2 != tarval_bad) {
4296 left = get_Sub_left(left);
4299 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4303 /* a+c1 == c2 ==> a == c2-c1, a+c1 != c2 ==> a != c2-c1 */
4304 else if (is_Add(left)) {
4305 ir_node *a_l = get_Add_left(left);
4306 ir_node *a_r = get_Add_right(left);
4310 if (is_Const(a_l)) {
4312 tv2 = value_of(a_l);
4315 tv2 = value_of(a_r);
4318 if (tv2 != tarval_bad) {
4319 tv2 = tarval_sub(tv, tv2, NULL);
4321 if (tv2 != tarval_bad) {
4325 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4329 /* -a == c ==> a == -c, -a != c ==> a != -c */
4330 else if (is_Minus(left)) {
4331 tarval *tv2 = tarval_sub(get_mode_null(mode), tv, NULL);
4333 if (tv2 != tarval_bad) {
4334 left = get_Minus_op(left);
4337 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4342 /* the following reassociations work only for <= */
4343 else if (proj_nr == pn_Cmp_Le || proj_nr == pn_Cmp_Lt) {
4344 if (tv != tarval_bad) {
4345 /* c >= 0 : Abs(a) <= c ==> (unsigned)(a + c) <= 2*c */
4346 if (is_Abs(left)) { // TODO something is missing here
4352 if (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) {
4353 switch (get_irn_opcode(left)) {
4357 c1 = get_And_right(left);
4360 * And(x, C1) == C2 ==> FALSE if C2 & C1 != C2
4361 * And(x, C1) != C2 ==> TRUE if C2 & C1 != C2
4363 tarval *mask = tarval_and(get_Const_tarval(c1), tv);
4365 /* TODO: move to constant evaluation */
4366 tv = proj_nr == pn_Cmp_Eq ? get_tarval_b_false() : get_tarval_b_true();
4367 c1 = new_Const(mode_b, tv);
4368 DBG_OPT_CSTEVAL(proj, c1);
4372 if (tarval_is_single_bit(tv)) {
4374 * optimization for AND:
4376 * And(x, C) == C ==> And(x, C) != 0
4377 * And(x, C) != C ==> And(X, C) == 0
4379 * if C is a single Bit constant.
4382 /* check for Constant's match. We have check hare the tarvals,
4383 because our const might be changed */
4384 if (get_Const_tarval(c1) == tv) {
4385 /* fine: do the transformation */
4386 tv = get_mode_null(get_tarval_mode(tv));
4387 proj_nr ^= pn_Cmp_Leg;
4389 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CNST_MAGN);
4395 c1 = get_Or_right(left);
4396 if (is_Const(c1) && tarval_is_null(tv)) {
4398 * Or(x, C) == 0 && C != 0 ==> FALSE
4399 * Or(x, C) != 0 && C != 0 ==> TRUE
4401 if (! tarval_is_null(get_Const_tarval(c1))) {
4402 /* TODO: move to constant evaluation */
4403 tv = proj_nr == pn_Cmp_Eq ? get_tarval_b_false() : get_tarval_b_true();
4404 c1 = new_Const(mode_b, tv);
4405 DBG_OPT_CSTEVAL(proj, c1);
4412 * optimize x << c1 == c into x & (-1 >>u c1) == c >> c1 if c & (-1 << c1) == c
4414 * optimize x << c1 != c into x & (-1 >>u c1) != c >> c1 if c & (-1 << c1) == c
4417 c1 = get_Shl_right(left);
4419 tarval *tv1 = get_Const_tarval(c1);
4420 ir_mode *mode = get_irn_mode(left);
4421 tarval *minus1 = get_mode_all_one(mode);
4422 tarval *amask = tarval_shr(minus1, tv1);
4423 tarval *cmask = tarval_shl(minus1, tv1);
4426 if (tarval_and(tv, cmask) != tv) {
4427 /* condition not met */
4428 tv = proj_nr == pn_Cmp_Eq ? get_tarval_b_false() : get_tarval_b_true();
4429 c1 = new_Const(mode_b, tv);
4430 DBG_OPT_CSTEVAL(proj, c1);
4433 sl = get_Shl_left(left);
4434 blk = get_nodes_block(n);
4435 left = new_rd_And(get_irn_dbg_info(left), current_ir_graph, blk, sl, new_Const(mode, amask), mode);
4436 tv = tarval_shr(tv, tv1);
4438 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_SHF_TO_AND);
4443 * optimize x >>u c1 == c into x & (-1 << c1) == c << c1 if c & (-1 >>u c1) == c
4445 * optimize x >>u c1 != c into x & (-1 << c1) != c << c1 if c & (-1 >>u c1) == c
4448 c1 = get_Shr_right(left);
4450 tarval *tv1 = get_Const_tarval(c1);
4451 ir_mode *mode = get_irn_mode(left);
4452 tarval *minus1 = get_mode_all_one(mode);
4453 tarval *amask = tarval_shl(minus1, tv1);
4454 tarval *cmask = tarval_shr(minus1, tv1);
4457 if (tarval_and(tv, cmask) != tv) {
4458 /* condition not met */
4459 tv = proj_nr == pn_Cmp_Eq ? get_tarval_b_false() : get_tarval_b_true();
4460 c1 = new_Const(mode_b, tv);
4461 DBG_OPT_CSTEVAL(proj, c1);
4464 sl = get_Shr_left(left);
4465 blk = get_nodes_block(n);
4466 left = new_rd_And(get_irn_dbg_info(left), current_ir_graph, blk, sl, new_Const(mode, amask), mode);
4467 tv = tarval_shl(tv, tv1);
4469 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_SHF_TO_AND);
4474 * optimize x >>s c1 == c into x & (-1 << c1) == c << c1 if (c >>s (BITS - c1)) \in {0,-1}
4476 * optimize x >>s c1 != c into x & (-1 << c1) != c << c1 if (c >>s (BITS - c1)) \in {0,-1}
4479 c1 = get_Shrs_right(left);
4481 tarval *tv1 = get_Const_tarval(c1);
4482 ir_mode *mode = get_irn_mode(left);
4483 tarval *minus1 = get_mode_all_one(mode);
4484 tarval *amask = tarval_shl(minus1, tv1);
4485 tarval *cond = new_tarval_from_long(get_mode_size_bits(mode), get_tarval_mode(tv1));
4488 cond = tarval_sub(cond, tv1, NULL);
4489 cond = tarval_shrs(tv, cond);
4491 if (!tarval_is_all_one(cond) && !tarval_is_null(cond)) {
4492 /* condition not met */
4493 tv = proj_nr == pn_Cmp_Eq ? get_tarval_b_false() : get_tarval_b_true();
4494 c1 = new_Const(mode_b, tv);
4495 DBG_OPT_CSTEVAL(proj, c1);
4498 sl = get_Shrs_left(left);
4499 blk = get_nodes_block(n);
4500 left = new_rd_And(get_irn_dbg_info(left), current_ir_graph, blk, sl, new_Const(mode, amask), mode);
4501 tv = tarval_shl(tv, tv1);
4503 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_SHF_TO_AND);
4508 } /* tarval != bad */
4511 if (changed & 2) /* need a new Const */
4512 right = new_Const(mode, tv);
4514 if ((proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) && is_Const(right) && is_Const_null(right) && is_Proj(left)) {
4515 ir_node *op = get_Proj_pred(left);
4517 if ((is_Mod(op) && get_Proj_proj(left) == pn_Mod_res) ||
4518 (is_DivMod(op) && get_Proj_proj(left) == pn_DivMod_res_mod)) {
4519 ir_node *c = get_binop_right(op);
4522 tarval *tv = get_Const_tarval(c);
4524 if (tarval_is_single_bit(tv)) {
4525 /* special case: (x % 2^n) CMP 0 ==> x & (2^n-1) CMP 0 */
4526 ir_node *v = get_binop_left(op);
4527 ir_node *blk = get_irn_n(op, -1);
4528 ir_mode *mode = get_irn_mode(v);
4530 tv = tarval_sub(tv, get_mode_one(mode), NULL);
4531 left = new_rd_And(get_irn_dbg_info(op), current_ir_graph, blk, v, new_Const(mode, tv), mode);
4533 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_MOD_TO_AND);
4540 ir_node *block = get_nodes_block(n);
4542 /* create a new compare */
4543 n = new_rd_Cmp(get_irn_dbg_info(n), current_ir_graph, block, left, right);
4544 proj = new_rd_Proj(get_irn_dbg_info(proj), current_ir_graph, block, n, get_irn_mode(proj), proj_nr);
4548 } /* transform_node_Proj_Cmp */
4551 * Optimize CopyB(mem, x, x) into a Nop.
4553 static ir_node *transform_node_Proj_CopyB(ir_node *proj) {
4554 ir_node *copyb = get_Proj_pred(proj);
4555 ir_node *a = get_CopyB_dst(copyb);
4556 ir_node *b = get_CopyB_src(copyb);
4559 switch (get_Proj_proj(proj)) {
4560 case pn_CopyB_X_regular:
4561 /* Turn CopyB into a tuple (mem, jmp, bad, bad) */
4562 DBG_OPT_EXC_REM(proj);
4563 proj = new_r_Jmp(current_ir_graph, get_nodes_block(copyb));
4565 case pn_CopyB_M_except:
4566 case pn_CopyB_X_except:
4567 DBG_OPT_EXC_REM(proj);
4568 proj = get_irg_bad(current_ir_graph);
4575 } /* transform_node_Proj_CopyB */
4578 * Optimize Bounds(idx, idx, upper) into idx.
4580 static ir_node *transform_node_Proj_Bound(ir_node *proj) {
4581 ir_node *oldn = proj;
4582 ir_node *bound = get_Proj_pred(proj);
4583 ir_node *idx = get_Bound_index(bound);
4584 ir_node *pred = skip_Proj(idx);
4587 if (idx == get_Bound_lower(bound))
4589 else if (is_Bound(pred)) {
4591 * idx was Bounds checked in the same MacroBlock previously,
4592 * it is still valid if lower <= pred_lower && pred_upper <= upper.
4594 ir_node *lower = get_Bound_lower(bound);
4595 ir_node *upper = get_Bound_upper(bound);
4596 if (get_Bound_lower(pred) == lower &&
4597 get_Bound_upper(pred) == upper &&
4598 get_irn_MacroBlock(bound) == get_irn_MacroBlock(pred)) {
4600 * One could expect that we simply return the previous
4601 * Bound here. However, this would be wrong, as we could
4602 * add an exception Proj to a new location then.
4603 * So, we must turn in into a tuple.
4609 /* Turn Bound into a tuple (mem, jmp, bad, idx) */
4610 switch (get_Proj_proj(proj)) {
4612 DBG_OPT_EXC_REM(proj);
4613 proj = get_Bound_mem(bound);
4615 case pn_Bound_X_except:
4616 DBG_OPT_EXC_REM(proj);
4617 proj = get_irg_bad(current_ir_graph);
4621 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NOP);
4623 case pn_Bound_X_regular:
4624 DBG_OPT_EXC_REM(proj);
4625 proj = new_r_Jmp(current_ir_graph, get_nodes_block(bound));
4632 } /* transform_node_Proj_Bound */
4635 * Does all optimizations on nodes that must be done on it's Proj's
4636 * because of creating new nodes.
4638 static ir_node *transform_node_Proj(ir_node *proj) {
4639 ir_node *n = get_Proj_pred(proj);
4641 if (n->op->ops.transform_node_Proj)
4642 return n->op->ops.transform_node_Proj(proj);
4644 } /* transform_node_Proj */
4647 * Move Confirms down through Phi nodes.
4649 static ir_node *transform_node_Phi(ir_node *phi) {
4651 ir_mode *mode = get_irn_mode(phi);
4653 if (mode_is_reference(mode)) {
4654 n = get_irn_arity(phi);
4656 /* Beware of Phi0 */
4658 ir_node *pred = get_irn_n(phi, 0);
4659 ir_node *bound, *new_Phi, *block, **in;
4662 if (! is_Confirm(pred))
4665 bound = get_Confirm_bound(pred);
4666 pnc = get_Confirm_cmp(pred);
4668 NEW_ARR_A(ir_node *, in, n);
4669 in[0] = get_Confirm_value(pred);
4671 for (i = 1; i < n; ++i) {
4672 pred = get_irn_n(phi, i);
4674 if (! is_Confirm(pred) ||
4675 get_Confirm_bound(pred) != bound ||
4676 get_Confirm_cmp(pred) != pnc)
4678 in[i] = get_Confirm_value(pred);
4680 /* move the Confirm nodes "behind" the Phi */
4681 block = get_irn_n(phi, -1);
4682 new_Phi = new_r_Phi(current_ir_graph, block, n, in, get_irn_mode(phi));
4683 return new_r_Confirm(current_ir_graph, block, new_Phi, bound, pnc);
4687 } /* transform_node_Phi */
4690 * Returns the operands of a commutative bin-op, if one operand is
4691 * a const, it is returned as the second one.
4693 static void get_comm_Binop_Ops(ir_node *binop, ir_node **a, ir_node **c) {
4694 ir_node *op_a = get_binop_left(binop);
4695 ir_node *op_b = get_binop_right(binop);
4697 assert(is_op_commutative(get_irn_op(binop)));
4699 if (is_Const(op_a)) {
4706 } /* get_comm_Binop_Ops */
4709 * Optimize a Or(And(Or(And(v,c4),c3),c2),c1) pattern if possible.
4710 * Such pattern may arise in bitfield stores.
4712 * value c4 value c4 & c2
4713 * AND c3 AND c1 | c3
4720 * AND c1 ===> OR if (c1 | c2) == 0x111..11
4723 static ir_node *transform_node_Or_bf_store(ir_node *or) {
4726 ir_node *and_l, *c3;
4727 ir_node *value, *c4;
4728 ir_node *new_and, *new_const, *block;
4729 ir_mode *mode = get_irn_mode(or);
4731 tarval *tv1, *tv2, *tv3, *tv4, *tv, *n_tv4, *n_tv2;
4734 get_comm_Binop_Ops(or, &and, &c1);
4735 if (!is_Const(c1) || !is_And(and))
4738 get_comm_Binop_Ops(and, &or_l, &c2);
4742 tv1 = get_Const_tarval(c1);
4743 tv2 = get_Const_tarval(c2);
4745 tv = tarval_or(tv1, tv2);
4746 if (tarval_is_all_one(tv)) {
4747 /* the AND does NOT clear a bit with isn't set by the OR */
4748 set_Or_left(or, or_l);
4749 set_Or_right(or, c1);
4751 /* check for more */
4758 get_comm_Binop_Ops(or_l, &and_l, &c3);
4759 if (!is_Const(c3) || !is_And(and_l))
4762 get_comm_Binop_Ops(and_l, &value, &c4);
4766 /* ok, found the pattern, check for conditions */
4767 assert(mode == get_irn_mode(and));
4768 assert(mode == get_irn_mode(or_l));
4769 assert(mode == get_irn_mode(and_l));
4771 tv3 = get_Const_tarval(c3);
4772 tv4 = get_Const_tarval(c4);
4774 tv = tarval_or(tv4, tv2);
4775 if (!tarval_is_all_one(tv)) {
4776 /* have at least one 0 at the same bit position */
4780 n_tv4 = tarval_not(tv4);
4781 if (tv3 != tarval_and(tv3, n_tv4)) {
4782 /* bit in the or_mask is outside the and_mask */
4786 n_tv2 = tarval_not(tv2);
4787 if (tv1 != tarval_and(tv1, n_tv2)) {
4788 /* bit in the or_mask is outside the and_mask */
4792 /* ok, all conditions met */
4793 block = get_irn_n(or, -1);
4795 new_and = new_r_And(current_ir_graph, block,
4796 value, new_r_Const(current_ir_graph, block, mode, tarval_and(tv4, tv2)), mode);
4798 new_const = new_r_Const(current_ir_graph, block, mode, tarval_or(tv3, tv1));
4800 set_Or_left(or, new_and);
4801 set_Or_right(or, new_const);
4803 /* check for more */
4805 } /* transform_node_Or_bf_store */
4808 * Optimize an Or(shl(x, c), shr(x, bits - c)) into a Rotl
4810 static ir_node *transform_node_Or_Rotl(ir_node *or) {
4811 ir_mode *mode = get_irn_mode(or);
4812 ir_node *shl, *shr, *block;
4813 ir_node *irn, *x, *c1, *c2, *v, *sub, *n, *rotval;
4816 if (! mode_is_int(mode))
4819 shl = get_binop_left(or);
4820 shr = get_binop_right(or);
4829 } else if (!is_Shl(shl)) {
4831 } else if (!is_Shr(shr)) {
4834 x = get_Shl_left(shl);
4835 if (x != get_Shr_left(shr))
4838 c1 = get_Shl_right(shl);
4839 c2 = get_Shr_right(shr);
4840 if (is_Const(c1) && is_Const(c2)) {
4841 tv1 = get_Const_tarval(c1);
4842 if (! tarval_is_long(tv1))
4845 tv2 = get_Const_tarval(c2);
4846 if (! tarval_is_long(tv2))
4849 if (get_tarval_long(tv1) + get_tarval_long(tv2)
4850 != (int) get_mode_size_bits(mode))
4853 /* yet, condition met */
4854 block = get_nodes_block(or);
4856 n = new_r_Rotl(current_ir_graph, block, x, c1, mode);
4858 DBG_OPT_ALGSIM1(or, shl, shr, n, FS_OPT_OR_SHFT_TO_ROTL);
4865 rotval = sub; /* a Rot right is not supported, so use a rot left */
4866 } else if (is_Sub(c2)) {
4872 if (get_Sub_right(sub) != v)
4875 c1 = get_Sub_left(sub);
4879 tv1 = get_Const_tarval(c1);
4880 if (! tarval_is_long(tv1))
4883 if (get_tarval_long(tv1) != (int) get_mode_size_bits(mode))
4886 /* yet, condition met */
4887 block = get_nodes_block(or);
4889 n = new_r_Rotl(current_ir_graph, block, x, rotval, mode);
4891 DBG_OPT_ALGSIM0(or, n, FS_OPT_OR_SHFT_TO_ROTL);
4893 } /* transform_node_Or_Rotl */
4898 static ir_node *transform_node_Or(ir_node *n) {
4899 ir_node *c, *oldn = n;
4900 ir_node *a = get_Or_left(n);
4901 ir_node *b = get_Or_right(n);
4904 if (is_Not(a) && is_Not(b)) {
4905 /* ~a | ~b = ~(a&b) */
4906 ir_node *block = get_nodes_block(n);
4908 mode = get_irn_mode(n);
4911 n = new_rd_And(get_irn_dbg_info(n), current_ir_graph, block, a, b, mode);
4912 n = new_rd_Not(get_irn_dbg_info(n), current_ir_graph, block, n, mode);
4913 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_DEMORGAN);
4917 /* we can evaluate 2 Projs of the same Cmp */
4918 if (get_irn_mode(n) == mode_b && is_Proj(a) && is_Proj(b)) {
4919 ir_node *pred_a = get_Proj_pred(a);
4920 ir_node *pred_b = get_Proj_pred(b);
4921 if (pred_a == pred_b) {
4922 dbg_info *dbgi = get_irn_dbg_info(n);
4923 ir_node *block = get_nodes_block(pred_a);
4924 pn_Cmp pn_a = get_Proj_proj(a);
4925 pn_Cmp pn_b = get_Proj_proj(b);
4926 /* yes, we can simply calculate with pncs */
4927 pn_Cmp new_pnc = pn_a | pn_b;
4929 return new_rd_Proj(dbgi, current_ir_graph, block, pred_a, mode_b,
4934 mode = get_irn_mode(n);
4935 HANDLE_BINOP_PHI(tarval_or, a, b, c, mode);
4937 n = transform_node_Or_bf_store(n);
4938 n = transform_node_Or_Rotl(n);
4942 n = transform_bitwise_distributive(n, transform_node_Or);
4945 } /* transform_node_Or */
4949 static ir_node *transform_node(ir_node *n);
4952 * Optimize (a >> c1) >> c2), works for Shr, Shrs, Shl, Rotl.
4954 * Should be moved to reassociation?
4956 static ir_node *transform_node_shift(ir_node *n) {
4957 ir_node *left, *right;
4959 tarval *tv1, *tv2, *res;
4960 ir_node *in[2], *irn, *block;
4962 left = get_binop_left(n);
4964 /* different operations */
4965 if (get_irn_op(left) != get_irn_op(n))
4968 right = get_binop_right(n);
4969 tv1 = value_of(right);
4970 if (tv1 == tarval_bad)
4973 tv2 = value_of(get_binop_right(left));
4974 if (tv2 == tarval_bad)
4977 res = tarval_add(tv1, tv2);
4978 mode = get_irn_mode(n);
4980 /* beware: a simple replacement works only, if res < modulo shift */
4982 int modulo_shf = get_mode_modulo_shift(mode);
4983 assert(modulo_shf >= (int) get_mode_size_bits(mode));
4984 if (modulo_shf > 0) {
4985 tarval *modulo = new_tarval_from_long(modulo_shf,
4986 get_tarval_mode(res));
4988 /* shifting too much */
4989 if (!(tarval_cmp(res, modulo) & pn_Cmp_Lt)) {
4991 ir_graph *irg = get_irn_irg(n);
4992 ir_node *block = get_nodes_block(n);
4993 dbg_info *dbgi = get_irn_dbg_info(n);
4994 ir_node *cnst = new_Const(mode_Iu, new_tarval_from_long(get_mode_size_bits(mode)-1, mode_Iu));
4995 return new_rd_Shrs(dbgi, irg, block, get_binop_left(left),
4999 return new_Const(mode, get_mode_null(mode));
5003 res = tarval_mod(res, new_tarval_from_long(get_mode_size_bits(mode), get_tarval_mode(res)));
5006 /* ok, we can replace it */
5007 block = get_nodes_block(n);
5009 in[0] = get_binop_left(left);
5010 in[1] = new_r_Const(current_ir_graph, block, get_tarval_mode(res), res);
5012 irn = new_ir_node(NULL, current_ir_graph, block, get_irn_op(n), mode, 2, in);
5014 DBG_OPT_ALGSIM0(n, irn, FS_OPT_REASSOC_SHIFT);
5016 return transform_node(irn);
5017 } /* transform_node_shift */
5020 * normalisation: (x & c1) >> c2 to (x >> c2) & (c1 >> c2)
5022 * - and, or, xor instead of &
5023 * - Shl, Shr, Shrs, rotl instead of >>
5024 * (with a special case for Or/Xor + Shrs)
5026 static ir_node *transform_node_bitop_shift(ir_node *n) {
5028 ir_node *right = get_binop_right(n);
5029 ir_mode *mode = get_irn_mode(n);
5030 ir_node *bitop_left;
5031 ir_node *bitop_right;
5043 assert(is_Shrs(n) || is_Shr(n) || is_Shl(n) || is_Rotl(n));
5045 if (!is_Const(right))
5048 left = get_binop_left(n);
5049 op_left = get_irn_op(left);
5050 if (op_left != op_And && op_left != op_Or && op_left != op_Eor)
5053 /* doing it with Shrs is not legal if the Or/Eor affects the topmost bit */
5054 if (is_Shrs(n) && (op_left == op_Or || op_left == op_Eor)) {
5055 /* TODO: test if sign bit is affectes */
5059 bitop_right = get_binop_right(left);
5060 if (!is_Const(bitop_right))
5063 bitop_left = get_binop_left(left);
5065 irg = get_irn_irg(n);
5066 block = get_nodes_block(n);
5067 dbgi = get_irn_dbg_info(n);
5068 tv1 = get_Const_tarval(bitop_right);
5069 tv2 = get_Const_tarval(right);
5071 assert(get_tarval_mode(tv1) == mode);
5074 new_shift = new_rd_Shl(dbgi, irg, block, bitop_left, right, mode);
5075 tv_shift = tarval_shl(tv1, tv2);
5076 } else if(is_Shr(n)) {
5077 new_shift = new_rd_Shr(dbgi, irg, block, bitop_left, right, mode);
5078 tv_shift = tarval_shr(tv1, tv2);
5079 } else if(is_Shrs(n)) {
5080 new_shift = new_rd_Shrs(dbgi, irg, block, bitop_left, right, mode);
5081 tv_shift = tarval_shrs(tv1, tv2);
5084 new_shift = new_rd_Rotl(dbgi, irg, block, bitop_left, right, mode);
5085 tv_shift = tarval_rotl(tv1, tv2);
5088 assert(get_tarval_mode(tv_shift) == mode);
5089 new_const = new_Const(mode, tv_shift);
5091 if (op_left == op_And) {
5092 new_bitop = new_rd_And(dbgi, irg, block, new_shift, new_const, mode);
5093 } else if(op_left == op_Or) {
5094 new_bitop = new_rd_Or(dbgi, irg, block, new_shift, new_const, mode);
5096 assert(op_left == op_Eor);
5097 new_bitop = new_rd_Eor(dbgi, irg, block, new_shift, new_const, mode);
5105 * (x << c1) >> c2 <=> x OP (c2-c1) & ((-1 << c1) >> c2)
5107 * (x >> c1) << c2 <=> x OP (c2-c1) & ((-1 >> c1) << c2)
5108 * (also with x >>s c1 when c1>=c2)
5110 static ir_node *transform_node_shl_shr(ir_node *n) {
5112 ir_node *right = get_binop_right(n);
5128 assert(is_Shl(n) || is_Shr(n) || is_Shrs(n));
5130 if (!is_Const(right))
5133 left = get_binop_left(n);
5134 mode = get_irn_mode(n);
5135 if (is_Shl(n) && (is_Shr(left) || is_Shrs(left))) {
5136 ir_node *shr_right = get_binop_right(left);
5138 if (!is_Const(shr_right))
5141 x = get_binop_left(left);
5142 tv_shr = get_Const_tarval(shr_right);
5143 tv_shl = get_Const_tarval(right);
5145 if (is_Shrs(left)) {
5146 /* shrs variant only allowed if c1 >= c2 */
5147 if (! (tarval_cmp(tv_shl, tv_shr) & pn_Cmp_Ge))
5150 tv_mask = tarval_shrs(get_mode_all_one(mode), tv_shr);
5153 tv_mask = tarval_shr(get_mode_all_one(mode), tv_shr);
5155 tv_mask = tarval_shl(tv_mask, tv_shl);
5156 } else if(is_Shr(n) && is_Shl(left)) {
5157 ir_node *shl_right = get_Shl_right(left);
5159 if (!is_Const(shl_right))
5162 x = get_Shl_left(left);
5163 tv_shr = get_Const_tarval(right);
5164 tv_shl = get_Const_tarval(shl_right);
5166 tv_mask = tarval_shl(get_mode_all_one(mode), tv_shl);
5167 tv_mask = tarval_shr(tv_mask, tv_shr);
5172 assert(get_tarval_mode(tv_shl) == get_tarval_mode(tv_shr));
5173 assert(tv_mask != tarval_bad);
5174 assert(get_tarval_mode(tv_mask) == mode);
5176 irg = get_irn_irg(n);
5177 block = get_nodes_block(n);
5178 dbgi = get_irn_dbg_info(n);
5180 pnc = tarval_cmp(tv_shl, tv_shr);
5181 if (pnc == pn_Cmp_Lt || pnc == pn_Cmp_Eq) {
5182 tv_shift = tarval_sub(tv_shr, tv_shl, NULL);
5183 new_const = new_Const(get_tarval_mode(tv_shift), tv_shift);
5185 new_shift = new_rd_Shrs(dbgi, irg, block, x, new_const, mode);
5187 new_shift = new_rd_Shr(dbgi, irg, block, x, new_const, mode);
5190 assert(pnc == pn_Cmp_Gt);
5191 tv_shift = tarval_sub(tv_shl, tv_shr, NULL);
5192 new_const = new_Const(get_tarval_mode(tv_shift), tv_shift);
5193 new_shift = new_rd_Shl(dbgi, irg, block, x, new_const, mode);
5196 new_const = new_Const(mode, tv_mask);
5197 new_and = new_rd_And(dbgi, irg, block, new_shift, new_const, mode);
5205 static ir_node *transform_node_Shr(ir_node *n) {
5206 ir_node *c, *oldn = n;
5207 ir_node *left = get_Shr_left(n);
5208 ir_node *right = get_Shr_right(n);
5209 ir_mode *mode = get_irn_mode(n);
5211 HANDLE_BINOP_PHI(tarval_shr, left, right, c, mode);
5212 n = transform_node_shift(n);
5215 n = transform_node_shl_shr(n);
5217 n = transform_node_bitop_shift(n);
5220 } /* transform_node_Shr */
5225 static ir_node *transform_node_Shrs(ir_node *n) {
5226 ir_node *c, *oldn = n;
5227 ir_node *a = get_Shrs_left(n);
5228 ir_node *b = get_Shrs_right(n);
5229 ir_mode *mode = get_irn_mode(n);
5231 HANDLE_BINOP_PHI(tarval_shrs, a, b, c, mode);
5232 n = transform_node_shift(n);
5235 n = transform_node_bitop_shift(n);
5238 } /* transform_node_Shrs */
5243 static ir_node *transform_node_Shl(ir_node *n) {
5244 ir_node *c, *oldn = n;
5245 ir_node *a = get_Shl_left(n);
5246 ir_node *b = get_Shl_right(n);
5247 ir_mode *mode = get_irn_mode(n);
5249 HANDLE_BINOP_PHI(tarval_shl, a, b, c, mode);
5250 n = transform_node_shift(n);
5253 n = transform_node_shl_shr(n);
5255 n = transform_node_bitop_shift(n);
5258 } /* transform_node_Shl */
5263 static ir_node *transform_node_Rotl(ir_node *n) {
5264 ir_node *c, *oldn = n;
5265 ir_node *a = get_Rotl_left(n);
5266 ir_node *b = get_Rotl_right(n);
5267 ir_mode *mode = get_irn_mode(n);
5269 HANDLE_BINOP_PHI(tarval_rotl, a, b, c, mode);
5270 n = transform_node_shift(n);
5273 n = transform_node_bitop_shift(n);
5276 } /* transform_node_Rotl */
5281 static ir_node *transform_node_Conv(ir_node *n) {
5282 ir_node *c, *oldn = n;
5283 ir_mode *mode = get_irn_mode(n);
5284 ir_node *a = get_Conv_op(n);
5286 if (mode != mode_b && is_const_Phi(a)) {
5287 /* Do NOT optimize mode_b Conv's, this leads to remaining
5288 * Phib nodes later, because the conv_b_lower operation
5289 * is instantly reverted, when it tries to insert a Convb.
5291 c = apply_conv_on_phi(a, mode);
5293 DBG_OPT_ALGSIM0(oldn, c, FS_OPT_CONST_PHI);
5298 if (is_Unknown(a)) { /* Conv_A(Unknown_B) -> Unknown_A */
5299 return new_r_Unknown(current_ir_graph, mode);
5302 if (mode_is_reference(mode) &&
5303 get_mode_size_bits(mode) == get_mode_size_bits(get_irn_mode(a)) &&
5305 ir_node *l = get_Add_left(a);
5306 ir_node *r = get_Add_right(a);
5307 dbg_info *dbgi = get_irn_dbg_info(a);
5308 ir_node *block = get_nodes_block(n);
5310 ir_node *lop = get_Conv_op(l);
5311 if(get_irn_mode(lop) == mode) {
5312 /* ConvP(AddI(ConvI(P), x)) -> AddP(P, x) */
5313 n = new_rd_Add(dbgi, current_ir_graph, block, lop, r, mode);
5318 ir_node *rop = get_Conv_op(r);
5319 if(get_irn_mode(rop) == mode) {
5320 /* ConvP(AddI(x, ConvI(P))) -> AddP(x, P) */
5321 n = new_rd_Add(dbgi, current_ir_graph, block, l, rop, mode);
5328 } /* transform_node_Conv */
5331 * Remove dead blocks and nodes in dead blocks
5332 * in keep alive list. We do not generate a new End node.
5334 static ir_node *transform_node_End(ir_node *n) {
5335 int i, j, n_keepalives = get_End_n_keepalives(n);
5338 NEW_ARR_A(ir_node *, in, n_keepalives);
5340 for (i = j = 0; i < n_keepalives; ++i) {
5341 ir_node *ka = get_End_keepalive(n, i);
5343 if (! is_Block_dead(ka)) {
5347 } else if (is_irn_pinned_in_irg(ka) && is_Block_dead(get_nodes_block(ka))) {
5349 } else if (is_Bad(ka)) {
5350 /* no need to keep Bad */
5355 if (j != n_keepalives)
5356 set_End_keepalives(n, j, in);
5358 } /* transform_node_End */
5360 /** returns 1 if a == -b */
5361 static int is_negated_value(ir_node *a, ir_node *b) {
5362 if (is_Minus(a) && get_Minus_op(a) == b)
5364 if (is_Minus(b) && get_Minus_op(b) == a)
5366 if (is_Sub(a) && is_Sub(b)) {
5367 ir_node *a_left = get_Sub_left(a);
5368 ir_node *a_right = get_Sub_right(a);
5369 ir_node *b_left = get_Sub_left(b);
5370 ir_node *b_right = get_Sub_right(b);
5372 if (a_left == b_right && a_right == b_left)
5380 * Optimize a Mux into some simpler cases.
5382 static ir_node *transform_node_Mux(ir_node *n) {
5383 ir_node *oldn = n, *sel = get_Mux_sel(n);
5384 ir_mode *mode = get_irn_mode(n);
5385 ir_node *t = get_Mux_true(n);
5386 ir_node *f = get_Mux_false(n);
5387 ir_graph *irg = current_ir_graph;
5389 /* first normalization step: move a possible zero to the false case */
5391 ir_node *cmp = get_Proj_pred(sel);
5394 if (is_Const(t) && is_Const_null(t)) {
5397 /* Mux(x, 0, y) => Mux(x, y, 0) */
5398 pn_Cmp pnc = get_Proj_proj(sel);
5399 sel = new_r_Proj(irg, get_nodes_block(cmp), cmp, mode_b,
5400 get_negated_pnc(pnc, get_irn_mode(get_Cmp_left(cmp))));
5401 n = new_rd_Mux(get_irn_dbg_info(n), irg, get_nodes_block(n), sel, t, f, mode);
5409 /* note: after normalization, false can only happen on default */
5410 if (mode == mode_b) {
5411 dbg_info *dbg = get_irn_dbg_info(n);
5412 ir_node *block = get_nodes_block(n);
5413 ir_graph *irg = current_ir_graph;
5416 tarval *tv_t = get_Const_tarval(t);
5417 if (tv_t == tarval_b_true) {
5419 /* Muxb(sel, true, false) = sel */
5420 assert(get_Const_tarval(f) == tarval_b_false);
5421 DBG_OPT_ALGSIM0(oldn, sel, FS_OPT_MUX_BOOL);
5424 /* Muxb(sel, true, x) = Or(sel, x) */
5425 n = new_rd_Or(dbg, irg, block, sel, f, mode_b);
5426 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_OR_BOOL);
5430 } else if (is_Const(f)) {
5431 tarval *tv_f = get_Const_tarval(f);
5432 if (tv_f == tarval_b_true) {
5433 /* Muxb(sel, x, true) = Or(Not(sel), x) */
5434 ir_node* not_sel = new_rd_Not(dbg, irg, block, sel, mode_b);
5435 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_ORNOT_BOOL);
5436 n = new_rd_Or(dbg, irg, block, not_sel, t, mode_b);
5439 /* Muxb(sel, x, false) = And(sel, x) */
5440 assert(tv_f == tarval_b_false);
5441 n = new_rd_And(dbg, irg, block, sel, t, mode_b);
5442 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_AND_BOOL);
5448 /* more normalization: try to normalize Mux(x, C1, C2) into Mux(x, +1/-1, 0) op C2 */
5449 if (is_Const(t) && is_Const(f) && mode_is_int(mode)) {
5450 tarval *a = get_Const_tarval(t);
5451 tarval *b = get_Const_tarval(f);
5452 tarval *null = get_tarval_null(mode);
5455 if (tarval_cmp(a, b) & pn_Cmp_Gt) {
5456 diff = tarval_sub(a, b, NULL);
5459 diff = tarval_sub(b, a, NULL);
5463 if (diff == get_tarval_one(mode) && min != null) {
5464 dbg_info *dbg = get_irn_dbg_info(n);
5465 ir_node *block = get_nodes_block(n);
5466 ir_graph *irg = current_ir_graph;
5467 ir_node *t = new_Const(mode, tarval_sub(a, min, NULL));
5468 ir_node *f = new_Const(mode, tarval_sub(b, min, NULL));
5469 n = new_rd_Mux(dbg, irg, block, sel, f, t, mode);
5470 n = new_rd_Add(dbg, irg, block, n, new_Const(mode, min), mode);
5476 ir_node *cmp = get_Proj_pred(sel);
5477 long pn = get_Proj_proj(sel);
5480 * Note: normalization puts the constant on the right side,
5481 * so we check only one case.
5483 * Note further that these optimization work even for floating point
5484 * with NaN's because -NaN == NaN.
5485 * However, if +0 and -0 is handled differently, we cannot use the Abs/-Abs
5489 ir_node *cmp_r = get_Cmp_right(cmp);
5490 if (is_Const(cmp_r) && is_Const_null(cmp_r)) {
5491 ir_node *block = get_nodes_block(n);
5492 ir_node *cmp_l = get_Cmp_left(cmp);
5494 if (!mode_honor_signed_zeros(mode) && is_negated_value(f, t)) {
5497 if ( (cmp_l == t && (pn == pn_Cmp_Ge || pn == pn_Cmp_Gt))
5498 || (cmp_l == f && (pn == pn_Cmp_Le || pn == pn_Cmp_Lt)))
5500 /* Mux(a >/>= 0, a, -a) = Mux(a </<= 0, -a, a) ==> Abs(a) */
5501 n = new_rd_Abs(get_irn_dbg_info(n), current_ir_graph, block,
5503 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_ABS);
5505 } else if ((cmp_l == t && (pn == pn_Cmp_Le || pn == pn_Cmp_Lt))
5506 || (cmp_l == f && (pn == pn_Cmp_Ge || pn == pn_Cmp_Gt)))
5508 /* Mux(a </<= 0, a, -a) = Mux(a >/>= 0, -a, a) ==> -Abs(a) */
5509 n = new_rd_Abs(get_irn_dbg_info(n), current_ir_graph, block,
5511 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph,
5513 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_ABS);
5518 if (mode_is_int(mode)) {
5520 if ((pn == pn_Cmp_Lg || pn == pn_Cmp_Eq) && is_And(cmp_l)) {
5521 /* Mux((a & b) != 0, c, 0) */
5522 ir_node *and_r = get_And_right(cmp_l);
5525 if (and_r == t && f == cmp_r) {
5526 if (is_Const(t) && tarval_is_single_bit(get_Const_tarval(t))) {
5527 if (pn == pn_Cmp_Lg) {
5528 /* Mux((a & 2^C) != 0, 2^C, 0) */
5530 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_BITOP);
5532 /* Mux((a & 2^C) == 0, 2^C, 0) */
5533 n = new_rd_Eor(get_irn_dbg_info(n), current_ir_graph,
5534 block, cmp_l, t, mode);
5535 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_BITOP);
5540 if (is_Shl(and_r)) {
5541 ir_node *shl_l = get_Shl_left(and_r);
5542 if (is_Const(shl_l) && is_Const_one(shl_l)) {
5543 if (and_r == t && f == cmp_r) {
5544 if (pn == pn_Cmp_Lg) {
5545 /* (a & (1 << n)) != 0, (1 << n), 0) */
5547 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_BITOP);
5549 /* (a & (1 << n)) == 0, (1 << n), 0) */
5550 n = new_rd_Eor(get_irn_dbg_info(n), current_ir_graph,
5551 block, cmp_l, t, mode);
5552 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_BITOP);
5558 and_l = get_And_left(cmp_l);
5559 if (is_Shl(and_l)) {
5560 ir_node *shl_l = get_Shl_left(and_l);
5561 if (is_Const(shl_l) && is_Const_one(shl_l)) {
5562 if (and_l == t && f == cmp_r) {
5563 if (pn == pn_Cmp_Lg) {
5564 /* ((1 << n) & a) != 0, (1 << n), 0) */
5566 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_BITOP);
5568 /* ((1 << n) & a) == 0, (1 << n), 0) */
5569 n = new_rd_Eor(get_irn_dbg_info(n), current_ir_graph,
5570 block, cmp_l, t, mode);
5571 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_BITOP);
5582 return arch_transform_node_Mux(n);
5583 } /* transform_node_Mux */
5586 * optimize Sync nodes that have other syncs as input we simply add the inputs
5587 * of the other sync to our own inputs
5589 static ir_node *transform_node_Sync(ir_node *n) {
5590 int arity = get_Sync_n_preds(n);
5593 for (i = 0; i < arity;) {
5594 ir_node *pred = get_Sync_pred(n, i);
5598 if (!is_Sync(pred)) {
5606 pred_arity = get_Sync_n_preds(pred);
5607 for (j = 0; j < pred_arity; ++j) {
5608 ir_node *pred_pred = get_Sync_pred(pred, j);
5613 add_irn_n(n, pred_pred);
5617 if (get_Sync_pred(n, k) == pred_pred) break;
5622 /* rehash the sync node */
5623 add_identities(current_ir_graph->value_table, n);
5629 * Tries several [inplace] [optimizing] transformations and returns an
5630 * equivalent node. The difference to equivalent_node() is that these
5631 * transformations _do_ generate new nodes, and thus the old node must
5632 * not be freed even if the equivalent node isn't the old one.
5634 static ir_node *transform_node(ir_node *n) {
5638 * Transform_node is the only "optimizing transformation" that might
5639 * return a node with a different opcode. We iterate HERE until fixpoint
5640 * to get the final result.
5644 if (n->op->ops.transform_node)
5645 n = n->op->ops.transform_node(n);
5646 } while (oldn != n);
5649 } /* transform_node */
5652 * Sets the default transform node operation for an ir_op_ops.
5654 * @param code the opcode for the default operation
5655 * @param ops the operations initialized
5660 static ir_op_ops *firm_set_default_transform_node(ir_opcode code, ir_op_ops *ops)
5664 ops->transform_node = transform_node_##a; \
5666 #define CASE_PROJ(a) \
5668 ops->transform_node_Proj = transform_node_Proj_##a; \
5670 #define CASE_PROJ_EX(a) \
5672 ops->transform_node = transform_node_##a; \
5673 ops->transform_node_Proj = transform_node_Proj_##a; \
5682 CASE_PROJ_EX(DivMod);
5716 } /* firm_set_default_transform_node */
5719 /* **************** Common Subexpression Elimination **************** */
5721 /** The size of the hash table used, should estimate the number of nodes
5723 #define N_IR_NODES 512
5725 /** Compares the attributes of two Const nodes. */
5726 static int node_cmp_attr_Const(ir_node *a, ir_node *b) {
5727 return (get_Const_tarval(a) != get_Const_tarval(b))
5728 || (get_Const_type(a) != get_Const_type(b));
5729 } /* node_cmp_attr_Const */
5731 /** Compares the attributes of two Proj nodes. */
5732 static int node_cmp_attr_Proj(ir_node *a, ir_node *b) {
5733 return get_irn_proj_attr(a) != get_irn_proj_attr(b);
5734 } /* node_cmp_attr_Proj */
5736 /** Compares the attributes of two Filter nodes. */
5737 static int node_cmp_attr_Filter(ir_node *a, ir_node *b) {
5738 return get_Filter_proj(a) != get_Filter_proj(b);
5739 } /* node_cmp_attr_Filter */
5741 /** Compares the attributes of two Alloc nodes. */
5742 static int node_cmp_attr_Alloc(ir_node *a, ir_node *b) {
5743 const alloc_attr *pa = get_irn_alloc_attr(a);
5744 const alloc_attr *pb = get_irn_alloc_attr(b);
5745 return (pa->where != pb->where) || (pa->type != pb->type);
5746 } /* node_cmp_attr_Alloc */
5748 /** Compares the attributes of two Free nodes. */
5749 static int node_cmp_attr_Free(ir_node *a, ir_node *b) {
5750 const free_attr *pa = get_irn_free_attr(a);
5751 const free_attr *pb = get_irn_free_attr(b);
5752 return (pa->where != pb->where) || (pa->type != pb->type);
5753 } /* node_cmp_attr_Free */
5755 /** Compares the attributes of two SymConst nodes. */
5756 static int node_cmp_attr_SymConst(ir_node *a, ir_node *b) {
5757 const symconst_attr *pa = get_irn_symconst_attr(a);
5758 const symconst_attr *pb = get_irn_symconst_attr(b);
5759 return (pa->kind != pb->kind)
5760 || (pa->sym.type_p != pb->sym.type_p)
5761 || (pa->tp != pb->tp);
5762 } /* node_cmp_attr_SymConst */
5764 /** Compares the attributes of two Call nodes. */
5765 static int node_cmp_attr_Call(ir_node *a, ir_node *b) {
5766 return get_irn_call_attr(a) != get_irn_call_attr(b);
5767 } /* node_cmp_attr_Call */
5769 /** Compares the attributes of two Sel nodes. */
5770 static int node_cmp_attr_Sel(ir_node *a, ir_node *b) {
5771 const ir_entity *a_ent = get_Sel_entity(a);
5772 const ir_entity *b_ent = get_Sel_entity(b);
5775 (a_ent->kind != b_ent->kind) ||
5776 (a_ent->name != b_ent->name) ||
5777 (a_ent->owner != b_ent->owner) ||
5778 (a_ent->ld_name != b_ent->ld_name) ||
5779 (a_ent->type != b_ent->type);
5781 /* Matze: inlining of functions can produce 2 entities with same type,
5783 return a_ent != b_ent;
5784 } /* node_cmp_attr_Sel */
5786 /** Compares the attributes of two Phi nodes. */
5787 static int node_cmp_attr_Phi(ir_node *a, ir_node *b) {
5788 /* we can only enter this function if both nodes have the same number of inputs,
5789 hence it is enough to check if one of them is a Phi0 */
5791 /* check the Phi0 pos attribute */
5792 return get_irn_phi_attr(a)->u.pos != get_irn_phi_attr(b)->u.pos;
5795 } /* node_cmp_attr_Phi */
5797 /** Compares the attributes of two Conv nodes. */
5798 static int node_cmp_attr_Conv(ir_node *a, ir_node *b) {
5799 return get_Conv_strict(a) != get_Conv_strict(b);
5800 } /* node_cmp_attr_Conv */
5802 /** Compares the attributes of two Cast nodes. */
5803 static int node_cmp_attr_Cast(ir_node *a, ir_node *b) {
5804 return get_Cast_type(a) != get_Cast_type(b);
5805 } /* node_cmp_attr_Cast */
5807 /** Compares the attributes of two Load nodes. */
5808 static int node_cmp_attr_Load(ir_node *a, ir_node *b) {
5809 if (get_Load_volatility(a) == volatility_is_volatile ||
5810 get_Load_volatility(b) == volatility_is_volatile)
5811 /* NEVER do CSE on volatile Loads */
5813 /* do not CSE Loads with different alignment. Be conservative. */
5814 if (get_Load_align(a) != get_Load_align(b))
5817 return get_Load_mode(a) != get_Load_mode(b);
5818 } /* node_cmp_attr_Load */
5820 /** Compares the attributes of two Store nodes. */
5821 static int node_cmp_attr_Store(ir_node *a, ir_node *b) {
5822 /* do not CSE Stores with different alignment. Be conservative. */
5823 if (get_Store_align(a) != get_Store_align(b))
5826 /* NEVER do CSE on volatile Stores */
5827 return (get_Store_volatility(a) == volatility_is_volatile ||
5828 get_Store_volatility(b) == volatility_is_volatile);
5829 } /* node_cmp_attr_Store */
5831 /** Compares two exception attributes */
5832 static int node_cmp_exception(ir_node *a, ir_node *b) {
5833 const except_attr *ea = get_irn_except_attr(a);
5834 const except_attr *eb = get_irn_except_attr(b);
5836 return ea->pin_state != eb->pin_state;
5839 #define node_cmp_attr_Bound node_cmp_exception
5841 /** Compares the attributes of two Div nodes. */
5842 static int node_cmp_attr_Div(ir_node *a, ir_node *b) {
5843 const divmod_attr *ma = get_irn_divmod_attr(a);
5844 const divmod_attr *mb = get_irn_divmod_attr(b);
5845 return ma->exc.pin_state != mb->exc.pin_state ||
5846 ma->res_mode != mb->res_mode ||
5847 ma->no_remainder != mb->no_remainder;
5848 } /* node_cmp_attr_Div */
5850 /** Compares the attributes of two DivMod nodes. */
5851 static int node_cmp_attr_DivMod(ir_node *a, ir_node *b) {
5852 const divmod_attr *ma = get_irn_divmod_attr(a);
5853 const divmod_attr *mb = get_irn_divmod_attr(b);
5854 return ma->exc.pin_state != mb->exc.pin_state ||
5855 ma->res_mode != mb->res_mode;
5856 } /* node_cmp_attr_DivMod */
5858 /** Compares the attributes of two Mod nodes. */
5859 static int node_cmp_attr_Mod(ir_node *a, ir_node *b) {
5860 const divmod_attr *ma = get_irn_divmod_attr(a);
5861 const divmod_attr *mb = get_irn_divmod_attr(b);
5862 return ma->exc.pin_state != mb->exc.pin_state ||
5863 ma->res_mode != mb->res_mode;
5864 } /* node_cmp_attr_Mod */
5866 /** Compares the attributes of two Quot nodes. */
5867 static int node_cmp_attr_Quot(ir_node *a, ir_node *b) {
5868 const divmod_attr *ma = get_irn_divmod_attr(a);
5869 const divmod_attr *mb = get_irn_divmod_attr(b);
5870 return ma->exc.pin_state != mb->exc.pin_state ||
5871 ma->res_mode != mb->res_mode;
5872 } /* node_cmp_attr_Quot */
5874 /** Compares the attributes of two Confirm nodes. */
5875 static int node_cmp_attr_Confirm(ir_node *a, ir_node *b) {
5876 return (get_Confirm_cmp(a) != get_Confirm_cmp(b));
5877 } /* node_cmp_attr_Confirm */
5879 /** Compares the attributes of two ASM nodes. */
5880 static int node_cmp_attr_ASM(ir_node *a, ir_node *b) {
5882 const ir_asm_constraint *ca;
5883 const ir_asm_constraint *cb;
5886 if (get_ASM_text(a) != get_ASM_text(b))
5889 /* Should we really check the constraints here? Should be better, but is strange. */
5890 n = get_ASM_n_input_constraints(a);
5891 if (n != get_ASM_n_input_constraints(b))
5894 ca = get_ASM_input_constraints(a);
5895 cb = get_ASM_input_constraints(b);
5896 for (i = 0; i < n; ++i) {
5897 if (ca[i].pos != cb[i].pos || ca[i].constraint != cb[i].constraint)
5901 n = get_ASM_n_output_constraints(a);
5902 if (n != get_ASM_n_output_constraints(b))
5905 ca = get_ASM_output_constraints(a);
5906 cb = get_ASM_output_constraints(b);
5907 for (i = 0; i < n; ++i) {
5908 if (ca[i].pos != cb[i].pos || ca[i].constraint != cb[i].constraint)
5912 n = get_ASM_n_clobbers(a);
5913 if (n != get_ASM_n_clobbers(b))
5916 cla = get_ASM_clobbers(a);
5917 clb = get_ASM_clobbers(b);
5918 for (i = 0; i < n; ++i) {
5919 if (cla[i] != clb[i])
5923 } /* node_cmp_attr_ASM */
5926 * Set the default node attribute compare operation for an ir_op_ops.
5928 * @param code the opcode for the default operation
5929 * @param ops the operations initialized
5934 static ir_op_ops *firm_set_default_node_cmp_attr(ir_opcode code, ir_op_ops *ops)
5938 ops->node_cmp_attr = node_cmp_attr_##a; \
5969 } /* firm_set_default_node_cmp_attr */
5972 * Compare function for two nodes in the value table. Gets two
5973 * nodes as parameters. Returns 0 if the nodes are a Common Sub Expression.
5975 int identities_cmp(const void *elt, const void *key) {
5976 ir_node *a = (ir_node *)elt;
5977 ir_node *b = (ir_node *)key;
5980 if (a == b) return 0;
5982 if ((get_irn_op(a) != get_irn_op(b)) ||
5983 (get_irn_mode(a) != get_irn_mode(b))) return 1;
5985 /* compare if a's in and b's in are of equal length */
5986 irn_arity_a = get_irn_intra_arity(a);
5987 if (irn_arity_a != get_irn_intra_arity(b))
5990 if (get_irn_pinned(a) == op_pin_state_pinned) {
5991 /* for pinned nodes, the block inputs must be equal */
5992 if (get_irn_intra_n(a, -1) != get_irn_intra_n(b, -1))
5994 } else if (! get_opt_global_cse()) {
5995 /* for block-local CSE both nodes must be in the same MacroBlock */
5996 if (get_irn_MacroBlock(a) != get_irn_MacroBlock(b))
6000 /* compare a->in[0..ins] with b->in[0..ins] */
6001 for (i = 0; i < irn_arity_a; i++)
6002 if (get_irn_intra_n(a, i) != get_irn_intra_n(b, i))
6006 * here, we already now that the nodes are identical except their
6009 if (a->op->ops.node_cmp_attr)
6010 return a->op->ops.node_cmp_attr(a, b);
6013 } /* identities_cmp */
6016 * Calculate a hash value of a node.
6018 * @param node The IR-node
6020 unsigned ir_node_hash(const ir_node *node) {
6021 return node->op->ops.hash(node);
6022 } /* ir_node_hash */
6025 pset *new_identities(void) {
6026 return new_pset(identities_cmp, N_IR_NODES);
6027 } /* new_identities */
6029 void del_identities(pset *value_table) {
6030 del_pset(value_table);
6031 } /* del_identities */
6034 * Normalize a node by putting constants (and operands with larger
6035 * node index) on the right (operator side).
6037 * @param n The node to normalize
6039 static void normalize_node(ir_node *n) {
6040 if (is_op_commutative(get_irn_op(n))) {
6041 ir_node *l = get_binop_left(n);
6042 ir_node *r = get_binop_right(n);
6044 /* For commutative operators perform a OP b == b OP a but keep
6045 * constants on the RIGHT side. This helps greatly in some
6046 * optimizations. Moreover we use the idx number to make the form
6048 if (!operands_are_normalized(l, r)) {
6049 set_binop_left(n, r);
6050 set_binop_right(n, l);
6054 } /* normalize_node */
6057 * Update the nodes after a match in the value table. If both nodes have
6058 * the same MacroBlock but different Blocks, we must ensure that the node
6059 * with the dominating Block (the node that is near to the MacroBlock header
6060 * is stored in the table.
6061 * Because a MacroBlock has only one "non-exception" flow, we don't need
6062 * dominance info here: We known, that one block must dominate the other and
6063 * following the only block input will allow to find it.
6065 static void update_known_irn(ir_node *known_irn, const ir_node *new_ir_node) {
6066 ir_node *known_blk, *new_block, *block, *mbh;
6068 if (get_opt_global_cse()) {
6069 /* Block inputs are meaning less */
6072 known_blk = get_irn_n(known_irn, -1);
6073 new_block = get_irn_n(new_ir_node, -1);
6074 if (known_blk == new_block) {
6075 /* already in the same block */
6079 * We expect the typical case when we built the graph. In that case, the
6080 * known_irn is already the upper one, so checking this should be faster.
6083 mbh = get_Block_MacroBlock(new_block);
6085 if (block == known_blk) {
6086 /* ok, we have found it: known_block dominates new_block as expected */
6091 * We have reached the MacroBlock header NOT founding
6092 * the known_block. new_block must dominate known_block.
6095 set_irn_n(known_irn, -1, new_block);
6098 assert(get_Block_n_cfgpreds(block) == 1);
6099 block = get_Block_cfgpred_block(block, 0);
6101 } /* update_value_table */
6104 * Return the canonical node computing the same value as n.
6105 * Looks up the node in a hash table, enters it in the table
6106 * if it isn't there yet.
6108 * @param value_table the HashSet containing all nodes in the
6110 * @param n the node to look up
6112 * @return a node that computes the same value as n or n if no such
6113 * node could be found
6115 ir_node *identify_remember(pset *value_table, ir_node *n) {
6118 if (!value_table) return n;
6121 /* lookup or insert in hash table with given hash key. */
6122 o = pset_insert(value_table, n, ir_node_hash(n));
6125 update_known_irn(o, n);
6129 } /* identify_remember */
6132 * During construction we set the op_pin_state_pinned flag in the graph right when the
6133 * optimization is performed. The flag turning on procedure global cse could
6134 * be changed between two allocations. This way we are safe.
6136 * @param value_table The value table
6137 * @param n The node to lookup
6139 static inline ir_node *identify_cons(pset *value_table, ir_node *n) {
6142 n = identify_remember(value_table, n);
6143 if (n != old && get_irn_MacroBlock(old) != get_irn_MacroBlock(n))
6144 set_irg_pinned(current_ir_graph, op_pin_state_floats);
6146 } /* identify_cons */
6148 /* Add a node to the identities value table. */
6149 void add_identities(pset *value_table, ir_node *node) {
6150 if (get_opt_cse() && is_no_Block(node))
6151 identify_remember(value_table, node);
6152 } /* add_identities */
6154 /* Visit each node in the value table of a graph. */
6155 void visit_all_identities(ir_graph *irg, irg_walk_func visit, void *env) {
6157 ir_graph *rem = current_ir_graph;
6159 current_ir_graph = irg;
6160 foreach_pset(irg->value_table, node)
6162 current_ir_graph = rem;
6163 } /* visit_all_identities */
6166 * Garbage in, garbage out. If a node has a dead input, i.e., the
6167 * Bad node is input to the node, return the Bad node.
6169 static ir_node *gigo(ir_node *node) {
6171 ir_op *op = get_irn_op(node);
6173 /* remove garbage blocks by looking at control flow that leaves the block
6174 and replacing the control flow by Bad. */
6175 if (get_irn_mode(node) == mode_X) {
6176 ir_node *block = get_nodes_block(skip_Proj(node));
6178 /* Don't optimize nodes in immature blocks. */
6179 if (!get_Block_matured(block))
6181 /* Don't optimize End, may have Bads. */
6182 if (op == op_End) return node;
6184 if (is_Block(block)) {
6185 if (is_Block_dead(block)) {
6186 /* control flow from dead block is dead */
6190 for (i = get_irn_arity(block) - 1; i >= 0; --i) {
6191 if (!is_Bad(get_irn_n(block, i)))
6195 ir_graph *irg = get_irn_irg(block);
6196 /* the start block is never dead */
6197 if (block != get_irg_start_block(irg)
6198 && block != get_irg_end_block(irg)) {
6200 * Do NOT kill control flow without setting
6201 * the block to dead of bad things can happen:
6202 * We get a Block that is not reachable be irg_block_walk()
6203 * but can be found by irg_walk()!
6205 set_Block_dead(block);
6212 /* Blocks, Phis and Tuples may have dead inputs, e.g., if one of the
6213 blocks predecessors is dead. */
6214 if (op != op_Block && op != op_Phi && op != op_Tuple) {
6215 irn_arity = get_irn_arity(node);
6218 * Beware: we can only read the block of a non-floating node.
6220 if (is_irn_pinned_in_irg(node) &&
6221 is_Block_dead(get_nodes_block(skip_Proj(node))))
6224 for (i = 0; i < irn_arity; i++) {
6225 ir_node *pred = get_irn_n(node, i);
6230 /* Propagating Unknowns here seems to be a bad idea, because
6231 sometimes we need a node as a input and did not want that
6233 However, it might be useful to move this into a later phase
6234 (if you think that optimizing such code is useful). */
6235 if (is_Unknown(pred) && mode_is_data(get_irn_mode(node)))
6236 return new_Unknown(get_irn_mode(node));
6241 /* With this code we violate the agreement that local_optimize
6242 only leaves Bads in Block, Phi and Tuple nodes. */
6243 /* If Block has only Bads as predecessors it's garbage. */
6244 /* If Phi has only Bads as predecessors it's garbage. */
6245 if ((op == op_Block && get_Block_matured(node)) || op == op_Phi) {
6246 irn_arity = get_irn_arity(node);
6247 for (i = 0; i < irn_arity; i++) {
6248 if (!is_Bad(get_irn_n(node, i))) break;
6250 if (i == irn_arity) node = new_Bad();
6257 * These optimizations deallocate nodes from the obstack.
6258 * It can only be called if it is guaranteed that no other nodes
6259 * reference this one, i.e., right after construction of a node.
6261 * @param n The node to optimize
6263 * current_ir_graph must be set to the graph of the node!
6265 ir_node *optimize_node(ir_node *n) {
6268 ir_opcode iro = get_irn_opcode(n);
6270 /* Always optimize Phi nodes: part of the construction. */
6271 if ((!get_opt_optimize()) && (iro != iro_Phi)) return n;
6273 /* constant expression evaluation / constant folding */
6274 if (get_opt_constant_folding()) {
6275 /* neither constants nor Tuple values can be evaluated */
6276 if (iro != iro_Const && (get_irn_mode(n) != mode_T)) {
6277 unsigned fp_model = get_irg_fp_model(current_ir_graph);
6278 int old_fp_mode = tarval_enable_fp_ops((fp_model & fp_strict_algebraic) == 0);
6279 /* try to evaluate */
6280 tv = computed_value(n);
6281 if (tv != tarval_bad) {
6283 ir_type *old_tp = get_irn_type(n);
6284 int i, arity = get_irn_arity(n);
6288 * Try to recover the type of the new expression.
6290 for (i = 0; i < arity && !old_tp; ++i)
6291 old_tp = get_irn_type(get_irn_n(n, i));
6294 * we MUST copy the node here temporary, because it's still needed
6295 * for DBG_OPT_CSTEVAL
6297 node_size = offsetof(ir_node, attr) + n->op->attr_size;
6298 oldn = alloca(node_size);
6300 memcpy(oldn, n, node_size);
6301 CLONE_ARR_A(ir_node *, oldn->in, n->in);
6303 /* ARG, copy the in array, we need it for statistics */
6304 memcpy(oldn->in, n->in, ARR_LEN(n->in) * sizeof(n->in[0]));
6306 /* note the inplace edges module */
6307 edges_node_deleted(n, current_ir_graph);
6309 /* evaluation was successful -- replace the node. */
6310 irg_kill_node(current_ir_graph, n);
6311 nw = new_Const(get_tarval_mode(tv), tv);
6313 if (old_tp && get_type_mode(old_tp) == get_tarval_mode(tv))
6314 set_Const_type(nw, old_tp);
6315 DBG_OPT_CSTEVAL(oldn, nw);
6316 tarval_enable_fp_ops(old_fp_mode);
6319 tarval_enable_fp_ops(old_fp_mode);
6323 /* remove unnecessary nodes */
6324 if (get_opt_algebraic_simplification() ||
6325 (iro == iro_Phi) || /* always optimize these nodes. */
6327 (iro == iro_Proj) ||
6328 (iro == iro_Block) ) /* Flags tested local. */
6329 n = equivalent_node(n);
6331 /* Common Subexpression Elimination.
6333 * Checks whether n is already available.
6334 * The block input is used to distinguish different subexpressions. Right
6335 * now all nodes are op_pin_state_pinned to blocks, i.e., the CSE only finds common
6336 * subexpressions within a block.
6339 n = identify_cons(current_ir_graph->value_table, n);
6342 edges_node_deleted(oldn, current_ir_graph);
6344 /* We found an existing, better node, so we can deallocate the old node. */
6345 irg_kill_node(current_ir_graph, oldn);
6349 /* Some more constant expression evaluation that does not allow to
6351 iro = get_irn_opcode(n);
6352 if (get_opt_algebraic_simplification() ||
6353 (iro == iro_Cond) ||
6354 (iro == iro_Proj)) /* Flags tested local. */
6355 n = transform_node(n);
6357 /* Remove nodes with dead (Bad) input.
6358 Run always for transformation induced Bads. */
6361 /* Now we have a legal, useful node. Enter it in hash table for CSE */
6362 if (get_opt_cse() && (get_irn_opcode(n) != iro_Block)) {
6364 n = identify_remember(current_ir_graph->value_table, o);
6370 } /* optimize_node */
6374 * These optimizations never deallocate nodes (in place). This can cause dead
6375 * nodes lying on the obstack. Remove these by a dead node elimination,
6376 * i.e., a copying garbage collection.
6378 ir_node *optimize_in_place_2(ir_node *n) {
6381 ir_opcode iro = get_irn_opcode(n);
6383 if (!get_opt_optimize() && !is_Phi(n)) return n;
6385 /* constant expression evaluation / constant folding */
6386 if (get_opt_constant_folding()) {
6387 /* neither constants nor Tuple values can be evaluated */
6388 if (iro != iro_Const && get_irn_mode(n) != mode_T) {
6389 unsigned fp_model = get_irg_fp_model(current_ir_graph);
6390 int old_fp_mode = tarval_enable_fp_ops((fp_model & fp_strict_algebraic) == 0);
6391 /* try to evaluate */
6392 tv = computed_value(n);
6393 if (tv != tarval_bad) {
6394 /* evaluation was successful -- replace the node. */
6395 ir_type *old_tp = get_irn_type(n);
6396 int i, arity = get_irn_arity(n);
6399 * Try to recover the type of the new expression.
6401 for (i = 0; i < arity && !old_tp; ++i)
6402 old_tp = get_irn_type(get_irn_n(n, i));
6404 n = new_Const(get_tarval_mode(tv), tv);
6406 if (old_tp && get_type_mode(old_tp) == get_tarval_mode(tv))
6407 set_Const_type(n, old_tp);
6409 DBG_OPT_CSTEVAL(oldn, n);
6410 tarval_enable_fp_ops(old_fp_mode);
6413 tarval_enable_fp_ops(old_fp_mode);
6417 /* remove unnecessary nodes */
6418 if (get_opt_constant_folding() ||
6419 (iro == iro_Phi) || /* always optimize these nodes. */
6420 (iro == iro_Id) || /* ... */
6421 (iro == iro_Proj) || /* ... */
6422 (iro == iro_Block) ) /* Flags tested local. */
6423 n = equivalent_node(n);
6425 /** common subexpression elimination **/
6426 /* Checks whether n is already available. */
6427 /* The block input is used to distinguish different subexpressions. Right
6428 now all nodes are op_pin_state_pinned to blocks, i.e., the cse only finds common
6429 subexpressions within a block. */
6430 if (get_opt_cse()) {
6432 n = identify_remember(current_ir_graph->value_table, o);
6437 /* Some more constant expression evaluation. */
6438 iro = get_irn_opcode(n);
6439 if (get_opt_constant_folding() ||
6440 (iro == iro_Cond) ||
6441 (iro == iro_Proj)) /* Flags tested local. */
6442 n = transform_node(n);
6444 /* Remove nodes with dead (Bad) input.
6445 Run always for transformation induced Bads. */
6448 /* Now we can verify the node, as it has no dead inputs any more. */
6451 /* Now we have a legal, useful node. Enter it in hash table for cse.
6452 Blocks should be unique anyways. (Except the successor of start:
6453 is cse with the start block!) */
6454 if (get_opt_cse() && (get_irn_opcode(n) != iro_Block)) {
6456 n = identify_remember(current_ir_graph->value_table, o);
6462 } /* optimize_in_place_2 */
6465 * Wrapper for external use, set proper status bits after optimization.
6467 ir_node *optimize_in_place(ir_node *n) {
6468 /* Handle graph state */
6469 assert(get_irg_phase_state(current_ir_graph) != phase_building);
6471 if (get_opt_global_cse())
6472 set_irg_pinned(current_ir_graph, op_pin_state_floats);
6473 if (get_irg_outs_state(current_ir_graph) == outs_consistent)
6474 set_irg_outs_inconsistent(current_ir_graph);
6476 /* FIXME: Maybe we could also test whether optimizing the node can
6477 change the control graph. */
6478 set_irg_doms_inconsistent(current_ir_graph);
6479 return optimize_in_place_2(n);
6480 } /* optimize_in_place */
6483 * Calculate a hash value of a Const node.
6485 static unsigned hash_Const(const ir_node *node) {
6488 /* special value for const, as they only differ in their tarval. */
6489 h = HASH_PTR(node->attr.con.tv);
6490 h = 9*h + HASH_PTR(get_irn_mode(node));
6496 * Calculate a hash value of a SymConst node.
6498 static unsigned hash_SymConst(const ir_node *node) {
6501 /* special value for const, as they only differ in their symbol. */
6502 h = HASH_PTR(node->attr.symc.sym.type_p);
6503 h = 9*h + HASH_PTR(get_irn_mode(node));
6506 } /* hash_SymConst */
6509 * Set the default hash operation in an ir_op_ops.
6511 * @param code the opcode for the default operation
6512 * @param ops the operations initialized
6517 static ir_op_ops *firm_set_default_hash(ir_opcode code, ir_op_ops *ops)
6521 ops->hash = hash_##a; \
6524 /* hash function already set */
6525 if (ops->hash != NULL)
6532 /* use input/mode default hash if no function was given */
6533 ops->hash = firm_default_hash;
6541 * Sets the default operation for an ir_ops.
6543 ir_op_ops *firm_set_default_operations(ir_opcode code, ir_op_ops *ops) {
6544 ops = firm_set_default_hash(code, ops);
6545 ops = firm_set_default_computed_value(code, ops);
6546 ops = firm_set_default_equivalent_node(code, ops);
6547 ops = firm_set_default_transform_node(code, ops);
6548 ops = firm_set_default_node_cmp_attr(code, ops);
6549 ops = firm_set_default_get_type(code, ops);
6550 ops = firm_set_default_get_type_attr(code, ops);
6551 ops = firm_set_default_get_entity_attr(code, ops);
6554 } /* firm_set_default_operations */