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
33 #include "irgraph_t.h"
34 #include "iredges_t.h"
41 #include "dbginfo_t.h"
42 #include "iropt_dbg.h"
48 #include "opt_confirms.h"
49 #include "opt_polymorphy.h"
53 /* Make types visible to allow most efficient access */
57 * Returns the tarval of a Const node or tarval_bad for all other nodes.
59 static tarval *default_value_of(const ir_node *n) {
61 return get_Const_tarval(n); /* might return tarval_bad */
66 value_of_func value_of_ptr = default_value_of;
68 /* * Set a new value_of function. */
69 void set_value_of_func(value_of_func func) {
73 value_of_ptr = default_value_of;
77 * Return the value of a Constant.
79 static tarval *computed_value_Const(const ir_node *n) {
80 return get_Const_tarval(n);
81 } /* computed_value_Const */
84 * Return the value of a 'sizeof', 'alignof' or 'offsetof' SymConst.
86 static tarval *computed_value_SymConst(const ir_node *n) {
90 switch (get_SymConst_kind(n)) {
91 case symconst_type_size:
92 type = get_SymConst_type(n);
93 if (get_type_state(type) == layout_fixed)
94 return new_tarval_from_long(get_type_size_bytes(type), get_irn_mode(n));
96 case symconst_type_align:
97 type = get_SymConst_type(n);
98 if (get_type_state(type) == layout_fixed)
99 return new_tarval_from_long(get_type_alignment_bytes(type), get_irn_mode(n));
101 case symconst_ofs_ent:
102 ent = get_SymConst_entity(n);
103 type = get_entity_owner(ent);
104 if (get_type_state(type) == layout_fixed)
105 return new_tarval_from_long(get_entity_offset(ent), get_irn_mode(n));
111 } /* computed_value_SymConst */
114 * Return the value of an Add.
116 static tarval *computed_value_Add(const ir_node *n) {
117 ir_node *a = get_Add_left(n);
118 ir_node *b = get_Add_right(n);
120 tarval *ta = value_of(a);
121 tarval *tb = value_of(b);
123 if ((ta != tarval_bad) && (tb != tarval_bad))
124 return tarval_add(ta, tb);
127 } /* computed_value_Add */
130 * Return the value of a Sub.
131 * Special case: a - a
133 static tarval *computed_value_Sub(const ir_node *n) {
134 ir_mode *mode = get_irn_mode(n);
135 ir_node *a = get_Sub_left(n);
136 ir_node *b = get_Sub_right(n);
141 if (a == b && !is_Bad(a))
142 return get_mode_null(mode);
147 if ((ta != tarval_bad) && (tb != tarval_bad))
148 return tarval_sub(ta, tb, mode);
151 } /* computed_value_Sub */
154 * Return the value of a Carry.
155 * Special : a op 0, 0 op b
157 static tarval *computed_value_Carry(const ir_node *n) {
158 ir_node *a = get_binop_left(n);
159 ir_node *b = get_binop_right(n);
160 ir_mode *m = get_irn_mode(n);
162 tarval *ta = value_of(a);
163 tarval *tb = value_of(b);
165 if ((ta != tarval_bad) && (tb != tarval_bad)) {
167 return tarval_carry() ? get_mode_one(m) : get_mode_null(m);
169 if (tarval_is_null(ta) || tarval_is_null(tb))
170 return get_mode_null(m);
173 } /* computed_value_Carry */
176 * Return the value of a Borrow.
179 static tarval *computed_value_Borrow(const ir_node *n) {
180 ir_node *a = get_binop_left(n);
181 ir_node *b = get_binop_right(n);
182 ir_mode *m = get_irn_mode(n);
184 tarval *ta = value_of(a);
185 tarval *tb = value_of(b);
187 if ((ta != tarval_bad) && (tb != tarval_bad)) {
188 return tarval_cmp(ta, tb) == pn_Cmp_Lt ? get_mode_one(m) : get_mode_null(m);
189 } else if (tarval_is_null(ta)) {
190 return get_mode_null(m);
193 } /* computed_value_Borrow */
196 * Return the value of an unary Minus.
198 static tarval *computed_value_Minus(const ir_node *n) {
199 ir_node *a = get_Minus_op(n);
200 tarval *ta = value_of(a);
202 if (ta != tarval_bad)
203 return tarval_neg(ta);
206 } /* computed_value_Minus */
209 * Return the value of a Mul.
211 static tarval *computed_value_Mul(const ir_node *n) {
212 ir_node *a = get_Mul_left(n);
213 ir_node *b = get_Mul_right(n);
216 tarval *ta = value_of(a);
217 tarval *tb = value_of(b);
219 mode = get_irn_mode(n);
220 if (mode != get_irn_mode(a)) {
221 /* n * n = 2n bit multiplication */
222 ta = tarval_convert_to(ta, mode);
223 tb = tarval_convert_to(tb, mode);
226 if (ta != tarval_bad && tb != tarval_bad) {
227 return tarval_mul(ta, tb);
229 /* a*0 = 0 or 0*b = 0 */
230 if (ta == get_mode_null(mode))
232 if (tb == get_mode_null(mode))
236 } /* computed_value_Mul */
239 * Return the value of an Abs.
241 static tarval *computed_value_Abs(const ir_node *n) {
242 ir_node *a = get_Abs_op(n);
243 tarval *ta = value_of(a);
245 if (ta != tarval_bad)
246 return tarval_abs(ta);
249 } /* computed_value_Abs */
252 * Return the value of an And.
253 * Special case: a & 0, 0 & b
255 static tarval *computed_value_And(const ir_node *n) {
256 ir_node *a = get_And_left(n);
257 ir_node *b = get_And_right(n);
259 tarval *ta = value_of(a);
260 tarval *tb = value_of(b);
262 if ((ta != tarval_bad) && (tb != tarval_bad)) {
263 return tarval_and (ta, tb);
265 if (tarval_is_null(ta)) return ta;
266 if (tarval_is_null(tb)) return tb;
269 } /* computed_value_And */
272 * Return the value of an Or.
273 * Special case: a | 1...1, 1...1 | b
275 static tarval *computed_value_Or(const ir_node *n) {
276 ir_node *a = get_Or_left(n);
277 ir_node *b = get_Or_right(n);
279 tarval *ta = value_of(a);
280 tarval *tb = value_of(b);
282 if ((ta != tarval_bad) && (tb != tarval_bad)) {
283 return tarval_or (ta, tb);
285 if (tarval_is_all_one(ta)) return ta;
286 if (tarval_is_all_one(tb)) return tb;
289 } /* computed_value_Or */
292 * Return the value of an Eor.
294 static tarval *computed_value_Eor(const ir_node *n) {
295 ir_node *a = get_Eor_left(n);
296 ir_node *b = get_Eor_right(n);
301 return get_mode_null(get_irn_mode(n));
306 if ((ta != tarval_bad) && (tb != tarval_bad)) {
307 return tarval_eor (ta, tb);
310 } /* computed_value_Eor */
313 * Return the value of a Not.
315 static tarval *computed_value_Not(const ir_node *n) {
316 ir_node *a = get_Not_op(n);
317 tarval *ta = value_of(a);
319 if (ta != tarval_bad)
320 return tarval_not(ta);
323 } /* computed_value_Not */
326 * Return the value of a Shl.
328 static tarval *computed_value_Shl(const ir_node *n) {
329 ir_node *a = get_Shl_left(n);
330 ir_node *b = get_Shl_right(n);
332 tarval *ta = value_of(a);
333 tarval *tb = value_of(b);
335 if ((ta != tarval_bad) && (tb != tarval_bad)) {
336 return tarval_shl (ta, tb);
339 } /* computed_value_Shl */
342 * Return the value of a Shr.
344 static tarval *computed_value_Shr(const ir_node *n) {
345 ir_node *a = get_Shr_left(n);
346 ir_node *b = get_Shr_right(n);
348 tarval *ta = value_of(a);
349 tarval *tb = value_of(b);
351 if ((ta != tarval_bad) && (tb != tarval_bad)) {
352 return tarval_shr (ta, tb);
355 } /* computed_value_Shr */
358 * Return the value of a Shrs.
360 static tarval *computed_value_Shrs(const ir_node *n) {
361 ir_node *a = get_Shrs_left(n);
362 ir_node *b = get_Shrs_right(n);
364 tarval *ta = value_of(a);
365 tarval *tb = value_of(b);
367 if ((ta != tarval_bad) && (tb != tarval_bad)) {
368 return tarval_shrs (ta, tb);
371 } /* computed_value_Shrs */
374 * Return the value of a Rotl.
376 static tarval *computed_value_Rotl(const ir_node *n) {
377 ir_node *a = get_Rotl_left(n);
378 ir_node *b = get_Rotl_right(n);
380 tarval *ta = value_of(a);
381 tarval *tb = value_of(b);
383 if ((ta != tarval_bad) && (tb != tarval_bad)) {
384 return tarval_rotl(ta, tb);
387 } /* computed_value_Rotl */
390 * Return the value of a Conv.
392 static tarval *computed_value_Conv(const ir_node *n) {
393 ir_node *a = get_Conv_op(n);
394 tarval *ta = value_of(a);
396 if (ta != tarval_bad)
397 return tarval_convert_to(ta, get_irn_mode(n));
400 } /* computed_value_Conv */
403 * Calculate the value of a Mux: can be evaluated, if the
404 * sel and the right input are known.
406 static tarval *computed_value_Mux(const ir_node *n) {
407 ir_node *sel = get_Mux_sel(n);
408 tarval *ts = value_of(sel);
410 if (ts == get_tarval_b_true()) {
411 ir_node *v = get_Mux_true(n);
414 else if (ts == get_tarval_b_false()) {
415 ir_node *v = get_Mux_false(n);
419 } /* computed_value_Mux */
422 * Calculate the value of a Confirm: can be evaluated,
423 * if it has the form Confirm(x, '=', Const).
425 static tarval *computed_value_Confirm(const ir_node *n) {
427 * Beware: we might produce Phi(Confirm(x == true), Confirm(x == false)).
428 * Do NOT optimize them away (CondEval wants them), so wait until
429 * remove_confirm is activated.
431 if (get_opt_remove_confirm()) {
432 if (get_Confirm_cmp(n) == pn_Cmp_Eq) {
433 tarval *tv = value_of(get_Confirm_bound(n));
434 if (tv != tarval_bad)
438 return value_of(get_Confirm_value(n));
439 } /* computed_value_Confirm */
442 * Return the value of a Proj(Cmp).
444 * This performs a first step of unreachable code elimination.
445 * Proj can not be computed, but folding a Cmp above the Proj here is
446 * not as wasteful as folding a Cmp into a Tuple of 16 Consts of which
448 * There are several case where we can evaluate a Cmp node, see later.
450 static tarval *computed_value_Proj_Cmp(const ir_node *n) {
451 ir_node *a = get_Proj_pred(n);
452 ir_node *aa = get_Cmp_left(a);
453 ir_node *ab = get_Cmp_right(a);
454 long proj_nr = get_Proj_proj(n);
457 * BEWARE: a == a is NOT always True for floating Point values, as
458 * NaN != NaN is defined, so we must check this here.
461 !mode_is_float(get_irn_mode(aa)) || proj_nr == pn_Cmp_Lt || proj_nr == pn_Cmp_Gt)
464 /* This is a trick with the bits used for encoding the Cmp
465 Proj numbers, the following statement is not the same:
466 return new_tarval_from_long (proj_nr == pn_Cmp_Eq, mode_b) */
467 return new_tarval_from_long (proj_nr & pn_Cmp_Eq, mode_b);
470 tarval *taa = value_of(aa);
471 tarval *tab = value_of(ab);
472 ir_mode *mode = get_irn_mode(aa);
475 * The predecessors of Cmp are target values. We can evaluate
478 if ((taa != tarval_bad) && (tab != tarval_bad)) {
479 /* strange checks... */
480 pn_Cmp flags = tarval_cmp(taa, tab);
481 if (flags != pn_Cmp_False) {
482 return new_tarval_from_long (proj_nr & flags, mode_b);
485 /* for integer values, we can check against MIN/MAX */
486 else if (mode_is_int(mode)) {
487 /* MIN <=/> x. This results in true/false. */
488 if (taa == get_mode_min(mode)) {
489 /* a compare with the MIN value */
490 if (proj_nr == pn_Cmp_Le)
491 return get_tarval_b_true();
492 else if (proj_nr == pn_Cmp_Gt)
493 return get_tarval_b_false();
495 /* x >=/< MIN. This results in true/false. */
497 if (tab == get_mode_min(mode)) {
498 /* a compare with the MIN value */
499 if (proj_nr == pn_Cmp_Ge)
500 return get_tarval_b_true();
501 else if (proj_nr == pn_Cmp_Lt)
502 return get_tarval_b_false();
504 /* MAX >=/< x. This results in true/false. */
505 else if (taa == get_mode_max(mode)) {
506 if (proj_nr == pn_Cmp_Ge)
507 return get_tarval_b_true();
508 else if (proj_nr == pn_Cmp_Lt)
509 return get_tarval_b_false();
511 /* x <=/> MAX. This results in true/false. */
512 else if (tab == get_mode_max(mode)) {
513 if (proj_nr == pn_Cmp_Le)
514 return get_tarval_b_true();
515 else if (proj_nr == pn_Cmp_Gt)
516 return get_tarval_b_false();
520 * The predecessors are Allocs or (void*)(0) constants. Allocs never
521 * return NULL, they raise an exception. Therefore we can predict
525 ir_node *aaa = skip_Id(skip_Proj(aa));
526 ir_node *aba = skip_Id(skip_Proj(ab));
528 if ( ( (/* aa is ProjP and aaa is Alloc */
530 && mode_is_reference(get_irn_mode(aa))
532 && ( (/* ab is NULL */
534 && mode_is_reference(get_irn_mode(ab))
535 && is_Const_null(ab))
536 || (/* ab is other Alloc */
538 && mode_is_reference(get_irn_mode(ab))
541 || (/* aa is NULL and aba is Alloc */
543 && mode_is_reference(get_irn_mode(aa))
546 && mode_is_reference(get_irn_mode(ab))
549 return new_tarval_from_long(proj_nr & pn_Cmp_Ne, mode_b);
552 return computed_value_Cmp_Confirm(a, aa, ab, proj_nr);
553 } /* computed_value_Proj_Cmp */
556 * Return the value of a floating point Quot.
558 static tarval *do_computed_value_Quot(const ir_node *a, const ir_node *b) {
559 tarval *ta = value_of(a);
560 tarval *tb = value_of(b);
562 /* cannot optimize 0 / b = 0 because of NaN */
563 if (ta != tarval_bad && tb != tarval_bad)
564 return tarval_quo(ta, tb);
566 } /* do_computed_value_Quot */
569 * Calculate the value of an integer Div of two nodes.
570 * Special case: 0 / b
572 static tarval *do_computed_value_Div(const ir_node *a, const ir_node *b) {
573 tarval *ta = value_of(a);
575 const ir_node *dummy;
577 /* Compute c1 / c2 or 0 / a, a != 0 */
578 if (tarval_is_null(ta) && value_not_zero(b, &dummy))
579 return ta; /* 0 / b == 0 */
581 if (ta != tarval_bad && tb != tarval_bad)
582 return tarval_div(ta, tb);
584 } /* do_computed_value_Div */
587 * Calculate the value of an integer Mod of two nodes.
588 * Special case: a % 1
590 static tarval *do_computed_value_Mod(const ir_node *a, const ir_node *b) {
591 tarval *ta = value_of(a);
592 tarval *tb = value_of(b);
594 /* Compute a % 1 or c1 % c2 */
595 if (tarval_is_one(tb))
596 return get_mode_null(get_irn_mode(a));
597 if (ta != tarval_bad && tb != tarval_bad)
598 return tarval_mod(ta, tb);
600 } /* do_computed_value_Mod */
603 * Return the value of a Proj(DivMod).
605 static tarval *computed_value_Proj_DivMod(const ir_node *n) {
606 long proj_nr = get_Proj_proj(n);
608 /* compute either the Div or the Mod part */
609 if (proj_nr == pn_DivMod_res_div) {
610 const ir_node *a = get_Proj_pred(n);
611 return do_computed_value_Div(get_DivMod_left(a), get_DivMod_right(a));
612 } else if (proj_nr == pn_DivMod_res_mod) {
613 const ir_node *a = get_Proj_pred(n);
614 return do_computed_value_Mod(get_DivMod_left(a), get_DivMod_right(a));
617 } /* computed_value_Proj_DivMod */
620 * Return the value of a Proj(Div).
622 static tarval *computed_value_Proj_Div(const ir_node *n) {
623 long proj_nr = get_Proj_proj(n);
625 if (proj_nr == pn_Div_res) {
626 const ir_node *a = get_Proj_pred(n);
627 return do_computed_value_Div(get_Div_left(a), get_Div_right(a));
630 } /* computed_value_Proj_Div */
633 * Return the value of a Proj(Mod).
635 static tarval *computed_value_Proj_Mod(const ir_node *n) {
636 long proj_nr = get_Proj_proj(n);
638 if (proj_nr == pn_Mod_res) {
639 const ir_node *a = get_Proj_pred(n);
640 return do_computed_value_Mod(get_Mod_left(a), get_Mod_right(a));
643 } /* computed_value_Proj_Mod */
646 * Return the value of a Proj(Quot).
648 static tarval *computed_value_Proj_Quot(const ir_node *n) {
649 long proj_nr = get_Proj_proj(n);
651 if (proj_nr == pn_Quot_res) {
652 const ir_node *a = get_Proj_pred(n);
653 return do_computed_value_Quot(get_Quot_left(a), get_Quot_right(a));
656 } /* computed_value_Proj_Quot */
659 * Return the value of a Proj.
661 static tarval *computed_value_Proj(const ir_node *proj) {
662 ir_node *n = get_Proj_pred(proj);
664 if (n->op->ops.computed_value_Proj != NULL)
665 return n->op->ops.computed_value_Proj(proj);
667 } /* computed_value_Proj */
670 * If the parameter n can be computed, return its value, else tarval_bad.
671 * Performs constant folding.
673 * @param n The node this should be evaluated
675 tarval *computed_value(const ir_node *n) {
676 if (n->op->ops.computed_value)
677 return n->op->ops.computed_value(n);
679 } /* computed_value */
682 * Set the default computed_value evaluator in an ir_op_ops.
684 * @param code the opcode for the default operation
685 * @param ops the operations initialized
690 static ir_op_ops *firm_set_default_computed_value(ir_opcode code, ir_op_ops *ops)
694 ops->computed_value = computed_value_##a; \
696 #define CASE_PROJ(a) \
698 ops->computed_value_Proj = computed_value_Proj_##a; \
735 } /* firm_set_default_computed_value */
738 * Returns a equivalent block for another block.
739 * If the block has only one predecessor, this is
740 * the equivalent one. If the only predecessor of a block is
741 * the block itself, this is a dead block.
743 * If both predecessors of a block are the branches of a binary
744 * Cond, the equivalent block is Cond's block.
746 * If all predecessors of a block are bad or lies in a dead
747 * block, the current block is dead as well.
749 * Note, that blocks are NEVER turned into Bad's, instead
750 * the dead_block flag is set. So, never test for is_Bad(block),
751 * always use is_dead_Block(block).
753 static ir_node *equivalent_node_Block(ir_node *n)
758 /* don't optimize dead blocks */
759 if (is_Block_dead(n))
762 n_preds = get_Block_n_cfgpreds(n);
764 /* The Block constructor does not call optimize, but mature_immBlock()
765 calls the optimization. */
766 assert(get_Block_matured(n));
768 /* Straightening: a single entry Block following a single exit Block
769 can be merged, if it is not the Start block. */
770 /* !!! Beware, all Phi-nodes of n must have been optimized away.
771 This should be true, as the block is matured before optimize is called.
772 But what about Phi-cycles with the Phi0/Id that could not be resolved?
773 Remaining Phi nodes are just Ids. */
775 ir_node *pred = skip_Proj(get_Block_cfgpred(n, 0));
778 ir_node *predblock = get_nodes_block(pred);
779 if (predblock == oldn) {
780 /* Jmp jumps into the block it is in -- deal self cycle. */
781 n = set_Block_dead(n);
782 DBG_OPT_DEAD_BLOCK(oldn, n);
783 } else if (get_opt_control_flow_straightening()) {
785 DBG_OPT_STG(oldn, n);
787 } else if (is_Cond(pred)) {
788 ir_node *predblock = get_nodes_block(pred);
789 if (predblock == oldn) {
790 /* Jmp jumps into the block it is in -- deal self cycle. */
791 n = set_Block_dead(n);
792 DBG_OPT_DEAD_BLOCK(oldn, n);
795 } else if ((n_preds == 2) &&
796 (get_opt_control_flow_weak_simplification())) {
797 /* Test whether Cond jumps twice to this block
798 * The more general case which more than 2 predecessors is handles
799 * in optimize_cf(), we handle only this special case for speed here.
801 ir_node *a = get_Block_cfgpred(n, 0);
802 ir_node *b = get_Block_cfgpred(n, 1);
804 if (is_Proj(a) && is_Proj(b)) {
805 ir_node *cond = get_Proj_pred(a);
807 if (cond == get_Proj_pred(b) && is_Cond(cond) &&
808 get_irn_mode(get_Cond_selector(cond)) == mode_b) {
809 /* Also a single entry Block following a single exit Block. Phis have
810 twice the same operand and will be optimized away. */
811 n = get_nodes_block(cond);
812 DBG_OPT_IFSIM1(oldn, a, b, n);
815 } else if (get_opt_unreachable_code() &&
816 (n != get_irg_start_block(current_ir_graph)) &&
817 (n != get_irg_end_block(current_ir_graph)) ) {
820 /* If all inputs are dead, this block is dead too, except if it is
821 the start or end block. This is one step of unreachable code
823 for (i = get_Block_n_cfgpreds(n) - 1; i >= 0; --i) {
824 ir_node *pred = get_Block_cfgpred(n, i);
827 if (is_Bad(pred)) continue;
828 pred_blk = get_nodes_block(skip_Proj(pred));
830 if (is_Block_dead(pred_blk)) continue;
833 /* really found a living input */
838 n = set_Block_dead(n);
839 DBG_OPT_DEAD_BLOCK(oldn, n);
844 } /* equivalent_node_Block */
847 * Returns a equivalent node for a Jmp, a Bad :-)
848 * Of course this only happens if the Block of the Jmp is dead.
850 static ir_node *equivalent_node_Jmp(ir_node *n) {
853 /* unreachable code elimination */
854 if (is_Block_dead(get_nodes_block(n))) {
855 n = get_irg_bad(current_ir_graph);
856 DBG_OPT_DEAD_BLOCK(oldn, n);
859 } /* equivalent_node_Jmp */
861 /** Raise is handled in the same way as Jmp. */
862 #define equivalent_node_Raise equivalent_node_Jmp
865 /* We do not evaluate Cond here as we replace it by a new node, a Jmp.
866 See transform_node_Proj_Cond(). */
869 * Optimize operations that are commutative and have neutral 0,
870 * so a op 0 = 0 op a = a.
872 static ir_node *equivalent_node_neutral_zero(ir_node *n) {
875 ir_node *a = get_binop_left(n);
876 ir_node *b = get_binop_right(n);
881 /* After running compute_node there is only one constant predecessor.
882 Find this predecessors value and remember the other node: */
883 if ((tv = value_of(a)) != tarval_bad) {
885 } else if ((tv = value_of(b)) != tarval_bad) {
890 /* If this predecessors constant value is zero, the operation is
891 * unnecessary. Remove it.
893 * Beware: If n is a Add, the mode of on and n might be different
894 * which happens in this rare construction: NULL + 3.
895 * Then, a Conv would be needed which we cannot include here.
897 if (tarval_is_null(tv) && get_irn_mode(on) == get_irn_mode(n)) {
900 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_0);
904 } /* equivalent_node_neutral_zero */
907 * Eor is commutative and has neutral 0.
909 static ir_node *equivalent_node_Eor(ir_node *n) {
914 n = equivalent_node_neutral_zero(n);
915 if (n != oldn) return n;
918 b = get_Eor_right(n);
921 ir_node *aa = get_Eor_left(a);
922 ir_node *ab = get_Eor_right(a);
925 /* (a ^ b) ^ a -> b */
927 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_EOR_A_B_A);
929 } else if (ab == b) {
930 /* (a ^ b) ^ b -> a */
932 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_EOR_A_B_A);
937 ir_node *ba = get_Eor_left(b);
938 ir_node *bb = get_Eor_right(b);
941 /* a ^ (a ^ b) -> b */
943 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_EOR_A_B_A);
945 } else if (bb == a) {
946 /* a ^ (b ^ a) -> b */
948 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_EOR_A_B_A);
956 * Optimize a - 0 and (a - x) + x (for modes with wrap-around).
958 * The second one looks strange, but this construct
959 * is used heavily in the LCC sources :-).
961 * Beware: The Mode of an Add may be different than the mode of its
962 * predecessors, so we could not return a predecessors in all cases.
964 static ir_node *equivalent_node_Add(ir_node *n) {
966 ir_node *left, *right;
967 ir_mode *mode = get_irn_mode(n);
969 n = equivalent_node_neutral_zero(n);
973 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
974 if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
977 left = get_Add_left(n);
978 right = get_Add_right(n);
981 if (get_Sub_right(left) == right) {
984 n = get_Sub_left(left);
985 if (mode == get_irn_mode(n)) {
986 DBG_OPT_ALGSIM1(oldn, left, right, n, FS_OPT_ADD_SUB);
992 if (get_Sub_right(right) == left) {
995 n = get_Sub_left(right);
996 if (mode == get_irn_mode(n)) {
997 DBG_OPT_ALGSIM1(oldn, left, right, n, FS_OPT_ADD_SUB);
1003 } /* equivalent_node_Add */
1006 * optimize operations that are not commutative but have neutral 0 on left,
1009 static ir_node *equivalent_node_left_zero(ir_node *n) {
1012 ir_node *a = get_binop_left(n);
1013 ir_node *b = get_binop_right(n);
1014 tarval *tb = value_of(b);
1016 if (tarval_is_null(tb)) {
1019 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_0);
1022 } /* equivalent_node_left_zero */
1024 #define equivalent_node_Shl equivalent_node_left_zero
1025 #define equivalent_node_Shr equivalent_node_left_zero
1026 #define equivalent_node_Shrs equivalent_node_left_zero
1027 #define equivalent_node_Rotl equivalent_node_left_zero
1030 * Optimize a - 0 and (a + x) - x (for modes with wrap-around).
1032 * The second one looks strange, but this construct
1033 * is used heavily in the LCC sources :-).
1035 * Beware: The Mode of a Sub may be different than the mode of its
1036 * predecessors, so we could not return a predecessors in all cases.
1038 static ir_node *equivalent_node_Sub(ir_node *n) {
1041 ir_mode *mode = get_irn_mode(n);
1044 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
1045 if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
1048 b = get_Sub_right(n);
1051 /* Beware: modes might be different */
1052 if (tarval_is_null(tb)) {
1053 ir_node *a = get_Sub_left(n);
1054 if (mode == get_irn_mode(a)) {
1057 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_0);
1061 } /* equivalent_node_Sub */
1065 * Optimize an "self-inverse unary op", ie op(op(n)) = n.
1068 * -(-a) == a, but might overflow two times.
1069 * We handle it anyway here but the better way would be a
1070 * flag. This would be needed for Pascal for instance.
1072 static ir_node *equivalent_node_idempotent_unop(ir_node *n) {
1074 ir_node *pred = get_unop_op(n);
1076 /* optimize symmetric unop */
1077 if (get_irn_op(pred) == get_irn_op(n)) {
1078 n = get_unop_op(pred);
1079 DBG_OPT_ALGSIM2(oldn, pred, n, FS_OPT_IDEM_UNARY);
1082 } /* equivalent_node_idempotent_unop */
1084 /** Optimize Not(Not(x)) == x. */
1085 #define equivalent_node_Not equivalent_node_idempotent_unop
1087 /** -(-x) == x ??? Is this possible or can --x raise an
1088 out of bounds exception if min =! max? */
1089 #define equivalent_node_Minus equivalent_node_idempotent_unop
1092 * Optimize a * 1 = 1 * a = a.
1094 static ir_node *equivalent_node_Mul(ir_node *n) {
1096 ir_node *a = get_Mul_left(n);
1098 /* we can handle here only the n * n = n bit cases */
1099 if (get_irn_mode(n) == get_irn_mode(a)) {
1100 ir_node *b = get_Mul_right(n);
1104 * Mul is commutative and has again an other neutral element.
1105 * Constants are place right, so check this case first.
1108 if (tarval_is_one(tv)) {
1110 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
1113 if (tarval_is_one(tv)) {
1115 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
1120 } /* equivalent_node_Mul */
1123 * Use algebraic simplification a | a = a | 0 = 0 | a = a.
1125 static ir_node *equivalent_node_Or(ir_node *n) {
1128 ir_node *a = get_Or_left(n);
1129 ir_node *b = get_Or_right(n);
1133 n = a; /* Or has it's own neutral element */
1134 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_OR);
1137 /* constants are cormalized to right, check this site first */
1139 if (tarval_is_null(tv)) {
1141 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_OR);
1145 if (tarval_is_null(tv)) {
1147 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_OR);
1152 } /* equivalent_node_Or */
1155 * Optimize a & 0b1...1 = 0b1...1 & a = a & a = (a|X) & a = a.
1157 static ir_node *equivalent_node_And(ir_node *n) {
1160 ir_node *a = get_And_left(n);
1161 ir_node *b = get_And_right(n);
1165 n = a; /* And has it's own neutral element */
1166 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_AND);
1169 /* constants are cormalized to right, check this site first */
1171 if (tarval_is_all_one(tv)) {
1173 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1177 if (tarval_is_all_one(tv)) {
1179 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1183 if (b == get_Or_left(a) || b == get_Or_right(a)) {
1186 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1191 if (a == get_Or_left(b) || a == get_Or_right(b)) {
1194 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1199 } /* equivalent_node_And */
1202 * Try to remove useless Conv's:
1204 static ir_node *equivalent_node_Conv(ir_node *n) {
1206 ir_node *a = get_Conv_op(n);
1208 ir_mode *n_mode = get_irn_mode(n);
1209 ir_mode *a_mode = get_irn_mode(a);
1212 if (n_mode == a_mode) { /* No Conv necessary */
1213 if (get_Conv_strict(n)) {
1214 /* special case: the predecessor might be a also a Conv */
1216 if (! get_Conv_strict(a)) {
1217 /* first one is not strict, kick it */
1219 a_mode = get_irn_mode(a);
1223 /* else both are strict conv, second is superfluous */
1224 } else if (is_Proj(a)) {
1225 ir_node *pred = get_Proj_pred(a);
1226 if (is_Load(pred)) {
1227 /* loads always return with the exact precision of n_mode */
1228 assert(get_Load_mode(pred) == n_mode);
1231 /* leave strict floating point Conv's */
1234 /* leave strict floating point Conv's */
1239 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CONV);
1240 } else if (is_Conv(a)) { /* Conv(Conv(b)) */
1241 ir_node *b = get_Conv_op(a);
1242 ir_mode *b_mode = get_irn_mode(b);
1244 if (get_Conv_strict(n) && get_Conv_strict(a)) {
1245 /* both are strict conv */
1246 if (smaller_mode(a_mode, n_mode)) {
1247 /* both are strict, but the first is smaller, so
1248 the second cannot remove more precision, remove the
1250 set_Conv_strict(n, 0);
1253 if (n_mode == b_mode) {
1254 if (! get_Conv_strict(n) && ! get_Conv_strict(a)) {
1255 if (n_mode == mode_b) {
1256 n = b; /* Convb(Conv*(xxxb(...))) == xxxb(...) */
1257 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
1258 } else if (get_mode_arithmetic(n_mode) == get_mode_arithmetic(a_mode)) {
1259 if (smaller_mode(b_mode, a_mode)) {
1260 n = b; /* ConvS(ConvL(xxxS(...))) == xxxS(...) */
1261 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
1265 if (mode_is_int(n_mode) && mode_is_float(a_mode)) {
1266 /* ConvI(ConvF(I)) -> I, iff float mantissa >= int mode */
1267 size_t int_mantissa = get_mode_size_bits(n_mode) - (mode_is_signed(n_mode) ? 1 : 0);
1268 size_t float_mantissa;
1269 /* FIXME There is no way to get the mantissa size of a mode */
1270 switch (get_mode_size_bits(a_mode)) {
1271 case 32: float_mantissa = 23 + 1; break; // + 1 for implicit 1
1272 case 64: float_mantissa = 52 + 1; break;
1273 case 80: float_mantissa = 64 + 1; break;
1274 default: float_mantissa = 0; break;
1276 if (float_mantissa != 0 && float_mantissa >= int_mantissa) {
1278 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
1283 if (smaller_mode(b_mode, a_mode)) {
1284 if (get_Conv_strict(n))
1285 set_Conv_strict(b, 1);
1286 n = b; /* ConvA(ConvB(ConvA(...))) == ConvA(...) */
1287 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
1293 } /* equivalent_node_Conv */
1296 * A Cast may be removed if the type of the previous node
1297 * is already the type of the Cast.
1299 static ir_node *equivalent_node_Cast(ir_node *n) {
1301 ir_node *pred = get_Cast_op(n);
1303 if (get_irn_type(pred) == get_Cast_type(n)) {
1305 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CAST);
1308 } /* equivalent_node_Cast */
1311 * - fold Phi-nodes, iff they have only one predecessor except
1314 static ir_node *equivalent_node_Phi(ir_node *n) {
1319 ir_node *first_val = NULL; /* to shutup gcc */
1321 if (!get_opt_normalize()) return n;
1323 n_preds = get_Phi_n_preds(n);
1325 block = get_nodes_block(n);
1326 if (is_Block_dead(block)) /* Control dead */
1327 return get_irg_bad(current_ir_graph);
1329 if (n_preds == 0) return n; /* Phi of dead Region without predecessors. */
1331 /* Find first non-self-referencing input */
1332 for (i = 0; i < n_preds; ++i) {
1333 first_val = get_Phi_pred(n, i);
1334 if ( (first_val != n) /* not self pointer */
1336 /* BEWARE: when the if is changed to 1, Phi's will ignore it's Bad
1337 * predecessors. Then, Phi nodes in dead code might be removed, causing
1338 * nodes pointing to themself (Add's for instance).
1339 * This is really bad and causes endless recursions in several
1340 * code pathes, so we do NOT optimize such a code.
1341 * This is not that bad as it sounds, optimize_cf() removes bad control flow
1342 * (and bad Phi predecessors), so live code is optimized later.
1344 && (! is_Bad(get_Block_cfgpred(block, i)))
1346 ) { /* value not dead */
1347 break; /* then found first value. */
1352 /* A totally Bad or self-referencing Phi (we didn't break the above loop) */
1353 return get_irg_bad(current_ir_graph);
1356 /* search for rest of inputs, determine if any of these
1357 are non-self-referencing */
1358 while (++i < n_preds) {
1359 ir_node *scnd_val = get_Phi_pred(n, i);
1360 if ( (scnd_val != n)
1361 && (scnd_val != first_val)
1364 && (! is_Bad(get_Block_cfgpred(block, i)))
1372 /* Fold, if no multiple distinct non-self-referencing inputs */
1374 DBG_OPT_PHI(oldn, n);
1377 } /* equivalent_node_Phi */
1380 * Several optimizations:
1381 * - fold Sync-nodes, iff they have only one predecessor except
1384 static ir_node *equivalent_node_Sync(ir_node *n) {
1385 int arity = get_Sync_n_preds(n);
1388 for (i = 0; i < arity;) {
1389 ir_node *pred = get_Sync_pred(n, i);
1392 /* Remove Bad predecessors */
1399 /* Remove duplicate predecessors */
1405 if (get_Sync_pred(n, j) == pred) {
1413 if (arity == 0) return get_irg_bad(current_ir_graph);
1414 if (arity == 1) return get_Sync_pred(n, 0);
1416 } /* equivalent_node_Sync */
1419 * Optimize Proj(Tuple).
1421 static ir_node *equivalent_node_Proj_Tuple(ir_node *proj) {
1422 ir_node *oldn = proj;
1423 ir_node *tuple = get_Proj_pred(proj);
1425 /* Remove the Tuple/Proj combination. */
1426 proj = get_Tuple_pred(tuple, get_Proj_proj(proj));
1427 DBG_OPT_TUPLE(oldn, tuple, proj);
1430 } /* equivalent_node_Proj_Tuple */
1433 * Optimize a / 1 = a.
1435 static ir_node *equivalent_node_Proj_Div(ir_node *proj) {
1436 ir_node *oldn = proj;
1437 ir_node *div = get_Proj_pred(proj);
1438 ir_node *b = get_Div_right(div);
1439 tarval *tb = value_of(b);
1441 /* Div is not commutative. */
1442 if (tarval_is_one(tb)) { /* div(x, 1) == x */
1443 switch (get_Proj_proj(proj)) {
1445 proj = get_Div_mem(div);
1446 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NEUTRAL_1);
1450 proj = get_Div_left(div);
1451 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NEUTRAL_1);
1455 /* we cannot replace the exception Proj's here, this is done in
1456 transform_node_Proj_Div() */
1461 } /* equivalent_node_Proj_Div */
1464 * Optimize a / 1.0 = a.
1466 static ir_node *equivalent_node_Proj_Quot(ir_node *proj) {
1467 ir_node *oldn = proj;
1468 ir_node *quot = get_Proj_pred(proj);
1469 ir_node *b = get_Quot_right(quot);
1470 tarval *tb = value_of(b);
1472 /* Div is not commutative. */
1473 if (tarval_is_one(tb)) { /* Quot(x, 1) == x */
1474 switch (get_Proj_proj(proj)) {
1476 proj = get_Quot_mem(quot);
1477 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NEUTRAL_1);
1481 proj = get_Quot_left(quot);
1482 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NEUTRAL_1);
1486 /* we cannot replace the exception Proj's here, this is done in
1487 transform_node_Proj_Quot() */
1492 } /* equivalent_node_Proj_Quot */
1495 * Optimize a / 1 = a.
1497 static ir_node *equivalent_node_Proj_DivMod(ir_node *proj) {
1498 ir_node *oldn = proj;
1499 ir_node *divmod = get_Proj_pred(proj);
1500 ir_node *b = get_DivMod_right(divmod);
1501 tarval *tb = value_of(b);
1503 /* Div is not commutative. */
1504 if (tarval_is_one(tb)) { /* div(x, 1) == x */
1505 switch (get_Proj_proj(proj)) {
1507 proj = get_DivMod_mem(divmod);
1508 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NEUTRAL_1);
1511 case pn_DivMod_res_div:
1512 proj = get_DivMod_left(divmod);
1513 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NEUTRAL_1);
1517 /* we cannot replace the exception Proj's here, this is done in
1518 transform_node_Proj_DivMod().
1519 Note further that the pn_DivMod_res_div case is handled in
1520 computed_value_Proj(). */
1525 } /* equivalent_node_Proj_DivMod */
1528 * Optimize CopyB(mem, x, x) into a Nop.
1530 static ir_node *equivalent_node_Proj_CopyB(ir_node *proj) {
1531 ir_node *oldn = proj;
1532 ir_node *copyb = get_Proj_pred(proj);
1533 ir_node *a = get_CopyB_dst(copyb);
1534 ir_node *b = get_CopyB_src(copyb);
1537 /* Turn CopyB into a tuple (mem, jmp, bad, bad) */
1538 switch (get_Proj_proj(proj)) {
1539 case pn_CopyB_M_regular:
1540 proj = get_CopyB_mem(copyb);
1541 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NOP);
1544 case pn_CopyB_M_except:
1545 case pn_CopyB_X_except:
1546 DBG_OPT_EXC_REM(proj);
1547 proj = get_irg_bad(current_ir_graph);
1552 } /* equivalent_node_Proj_CopyB */
1555 * Optimize Bounds(idx, idx, upper) into idx.
1557 static ir_node *equivalent_node_Proj_Bound(ir_node *proj) {
1558 ir_node *oldn = proj;
1559 ir_node *bound = get_Proj_pred(proj);
1560 ir_node *idx = get_Bound_index(bound);
1561 ir_node *pred = skip_Proj(idx);
1564 if (idx == get_Bound_lower(bound))
1566 else if (is_Bound(pred)) {
1568 * idx was Bounds checked in the same MacroBlock previously,
1569 * it is still valid if lower <= pred_lower && pred_upper <= upper.
1571 ir_node *lower = get_Bound_lower(bound);
1572 ir_node *upper = get_Bound_upper(bound);
1573 if (get_Bound_lower(pred) == lower &&
1574 get_Bound_upper(pred) == upper &&
1575 get_irn_MacroBlock(bound) == get_irn_MacroBlock(pred)) {
1577 * One could expect that we simply return the previous
1578 * Bound here. However, this would be wrong, as we could
1579 * add an exception Proj to a new location then.
1580 * So, we must turn in into a tuple.
1586 /* Turn Bound into a tuple (mem, jmp, bad, idx) */
1587 switch (get_Proj_proj(proj)) {
1589 DBG_OPT_EXC_REM(proj);
1590 proj = get_Bound_mem(bound);
1592 case pn_Bound_X_except:
1593 DBG_OPT_EXC_REM(proj);
1594 proj = get_irg_bad(current_ir_graph);
1598 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NOP);
1601 /* cannot optimize pn_Bound_X_regular, handled in transform ... */
1606 } /* equivalent_node_Proj_Bound */
1609 * Optimize an Exception Proj(Load) with a non-null address.
1611 static ir_node *equivalent_node_Proj_Load(ir_node *proj) {
1612 if (get_opt_ldst_only_null_ptr_exceptions()) {
1613 if (get_irn_mode(proj) == mode_X) {
1614 ir_node *load = get_Proj_pred(proj);
1616 /* get the Load address */
1617 const ir_node *addr = get_Load_ptr(load);
1618 const ir_node *confirm;
1620 if (value_not_null(addr, &confirm)) {
1621 if (get_Proj_proj(proj) == pn_Load_X_except) {
1622 DBG_OPT_EXC_REM(proj);
1623 return get_irg_bad(current_ir_graph);
1629 } /* equivalent_node_Proj_Load */
1632 * Optimize an Exception Proj(Store) with a non-null address.
1634 static ir_node *equivalent_node_Proj_Store(ir_node *proj) {
1635 if (get_opt_ldst_only_null_ptr_exceptions()) {
1636 if (get_irn_mode(proj) == mode_X) {
1637 ir_node *store = get_Proj_pred(proj);
1639 /* get the load/store address */
1640 const ir_node *addr = get_Store_ptr(store);
1641 const ir_node *confirm;
1643 if (value_not_null(addr, &confirm)) {
1644 if (get_Proj_proj(proj) == pn_Store_X_except) {
1645 DBG_OPT_EXC_REM(proj);
1646 return get_irg_bad(current_ir_graph);
1652 } /* equivalent_node_Proj_Store */
1655 * Does all optimizations on nodes that must be done on it's Proj's
1656 * because of creating new nodes.
1658 static ir_node *equivalent_node_Proj(ir_node *proj) {
1659 ir_node *n = get_Proj_pred(proj);
1661 if (get_irn_mode(proj) == mode_X) {
1662 if (is_Block_dead(get_nodes_block(n))) {
1663 /* Remove dead control flow -- early gigo(). */
1664 return get_irg_bad(current_ir_graph);
1667 if (n->op->ops.equivalent_node_Proj)
1668 return n->op->ops.equivalent_node_Proj(proj);
1670 } /* equivalent_node_Proj */
1675 static ir_node *equivalent_node_Id(ir_node *n) {
1682 DBG_OPT_ID(oldn, n);
1684 } /* equivalent_node_Id */
1689 static ir_node *equivalent_node_Mux(ir_node *n)
1691 ir_node *oldn = n, *sel = get_Mux_sel(n);
1692 tarval *ts = value_of(sel);
1694 /* Mux(true, f, t) == t */
1695 if (ts == tarval_b_true) {
1696 n = get_Mux_true(n);
1697 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_C);
1699 /* Mux(false, f, t) == f */
1700 else if (ts == tarval_b_false) {
1701 n = get_Mux_false(n);
1702 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_C);
1704 /* Mux(v, x, x) == x */
1705 else if (get_Mux_false(n) == get_Mux_true(n)) {
1706 n = get_Mux_true(n);
1707 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_EQ);
1709 else if (is_Proj(sel) && !mode_honor_signed_zeros(get_irn_mode(n))) {
1710 ir_node *cmp = get_Proj_pred(sel);
1711 long proj_nr = get_Proj_proj(sel);
1712 ir_node *f = get_Mux_false(n);
1713 ir_node *t = get_Mux_true(n);
1716 * Note further that these optimization work even for floating point
1717 * with NaN's because -NaN == NaN.
1718 * However, if +0 and -0 is handled differently, we cannot use the first one.
1721 ir_node *const cmp_l = get_Cmp_left(cmp);
1722 ir_node *const cmp_r = get_Cmp_right(cmp);
1726 if ((cmp_l == t && cmp_r == f) || /* Mux(t == f, t, f) -> f */
1727 (cmp_l == f && cmp_r == t)) { /* Mux(f == t, t, f) -> f */
1729 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1736 if ((cmp_l == t && cmp_r == f) || /* Mux(t != f, t, f) -> t */
1737 (cmp_l == f && cmp_r == t)) { /* Mux(f != t, t, f) -> t */
1739 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1746 * Note: normalization puts the constant on the right side,
1747 * so we check only one case.
1749 if (cmp_l == t && tarval_is_null(value_of(cmp_r))) {
1750 /* Mux(t CMP 0, X, t) */
1751 if (is_Minus(f) && get_Minus_op(f) == t) {
1752 /* Mux(t CMP 0, -t, t) */
1753 if (proj_nr == pn_Cmp_Eq) {
1754 /* Mux(t == 0, -t, t) ==> -t */
1756 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1757 } else if (proj_nr == pn_Cmp_Lg || proj_nr == pn_Cmp_Ne) {
1758 /* Mux(t != 0, -t, t) ==> t */
1760 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1767 } /* equivalent_node_Mux */
1770 * Remove Confirm nodes if setting is on.
1771 * Replace Confirms(x, '=', Constlike) by Constlike.
1773 static ir_node *equivalent_node_Confirm(ir_node *n) {
1774 ir_node *pred = get_Confirm_value(n);
1775 pn_Cmp pnc = get_Confirm_cmp(n);
1777 while (is_Confirm(pred) && pnc == get_Confirm_cmp(pred)) {
1779 * rare case: two identical Confirms one after another,
1780 * replace the second one with the first.
1783 pred = get_Confirm_value(n);
1784 pnc = get_Confirm_cmp(n);
1786 if (get_opt_remove_confirm())
1787 return get_Confirm_value(n);
1792 * equivalent_node() returns a node equivalent to input n. It skips all nodes that
1793 * perform no actual computation, as, e.g., the Id nodes. It does not create
1794 * new nodes. It is therefore safe to free n if the node returned is not n.
1795 * If a node returns a Tuple we can not just skip it. If the size of the
1796 * in array fits, we transform n into a tuple (e.g., Div).
1798 ir_node *equivalent_node(ir_node *n) {
1799 if (n->op->ops.equivalent_node)
1800 return n->op->ops.equivalent_node(n);
1802 } /* equivalent_node */
1805 * Sets the default equivalent node operation for an ir_op_ops.
1807 * @param code the opcode for the default operation
1808 * @param ops the operations initialized
1813 static ir_op_ops *firm_set_default_equivalent_node(ir_opcode code, ir_op_ops *ops)
1817 ops->equivalent_node = equivalent_node_##a; \
1819 #define CASE_PROJ(a) \
1821 ops->equivalent_node_Proj = equivalent_node_Proj_##a; \
1863 } /* firm_set_default_equivalent_node */
1866 * Returns non-zero if a node is a Phi node
1867 * with all predecessors constant.
1869 static int is_const_Phi(ir_node *n) {
1872 if (! is_Phi(n) || get_irn_arity(n) == 0)
1874 for (i = get_irn_arity(n) - 1; i >= 0; --i)
1875 if (! is_Const(get_irn_n(n, i)))
1878 } /* is_const_Phi */
1880 typedef tarval *(*tarval_sub_type)(tarval *a, tarval *b, ir_mode *mode);
1881 typedef tarval *(*tarval_binop_type)(tarval *a, tarval *b);
1884 * Wrapper for the tarval binop evaluation, tarval_sub has one more parameter.
1886 static tarval *do_eval(tarval *(*eval)(), tarval *a, tarval *b, ir_mode *mode) {
1887 if (eval == tarval_sub) {
1888 tarval_sub_type func = (tarval_sub_type)eval;
1890 return func(a, b, mode);
1892 tarval_binop_type func = (tarval_binop_type)eval;
1899 * Apply an evaluator on a binop with a constant operators (and one Phi).
1901 * @param phi the Phi node
1902 * @param other the other operand
1903 * @param eval an evaluator function
1904 * @param mode the mode of the result, may be different from the mode of the Phi!
1905 * @param left if non-zero, other is the left operand, else the right
1907 * @return a new Phi node if the conversion was successful, NULL else
1909 static ir_node *apply_binop_on_phi(ir_node *phi, tarval *other, tarval *(*eval)(), ir_mode *mode, int left) {
1914 int i, n = get_irn_arity(phi);
1916 NEW_ARR_A(void *, res, n);
1918 for (i = 0; i < n; ++i) {
1919 pred = get_irn_n(phi, i);
1920 tv = get_Const_tarval(pred);
1921 tv = do_eval(eval, other, tv, mode);
1923 if (tv == tarval_bad) {
1924 /* folding failed, bad */
1930 for (i = 0; i < n; ++i) {
1931 pred = get_irn_n(phi, i);
1932 tv = get_Const_tarval(pred);
1933 tv = do_eval(eval, tv, other, mode);
1935 if (tv == tarval_bad) {
1936 /* folding failed, bad */
1942 irg = current_ir_graph;
1943 for (i = 0; i < n; ++i) {
1944 pred = get_irn_n(phi, i);
1945 res[i] = new_r_Const_type(irg, get_irg_start_block(irg),
1946 mode, res[i], get_Const_type(pred));
1948 return new_r_Phi(irg, get_nodes_block(phi), n, (ir_node **)res, mode);
1949 } /* apply_binop_on_phi */
1952 * Apply an evaluator on a binop with two constant Phi.
1954 * @param a the left Phi node
1955 * @param b the right Phi node
1956 * @param eval an evaluator function
1957 * @param mode the mode of the result, may be different from the mode of the Phi!
1959 * @return a new Phi node if the conversion was successful, NULL else
1961 static ir_node *apply_binop_on_2_phis(ir_node *a, ir_node *b, tarval *(*eval)(), ir_mode *mode) {
1962 tarval *tv_l, *tv_r, *tv;
1968 if (get_nodes_block(a) != get_nodes_block(b))
1971 n = get_irn_arity(a);
1972 NEW_ARR_A(void *, res, n);
1974 for (i = 0; i < n; ++i) {
1975 pred = get_irn_n(a, i);
1976 tv_l = get_Const_tarval(pred);
1977 pred = get_irn_n(b, i);
1978 tv_r = get_Const_tarval(pred);
1979 tv = do_eval(eval, tv_l, tv_r, mode);
1981 if (tv == tarval_bad) {
1982 /* folding failed, bad */
1987 irg = current_ir_graph;
1988 for (i = 0; i < n; ++i) {
1989 pred = get_irn_n(a, i);
1990 res[i] = new_r_Const_type(irg, get_irg_start_block(irg), mode, res[i], get_Const_type(pred));
1992 return new_r_Phi(irg, get_nodes_block(a), n, (ir_node **)res, mode);
1993 } /* apply_binop_on_2_phis */
1996 * Apply an evaluator on a unop with a constant operator (a Phi).
1998 * @param phi the Phi node
1999 * @param eval an evaluator function
2001 * @return a new Phi node if the conversion was successful, NULL else
2003 static ir_node *apply_unop_on_phi(ir_node *phi, tarval *(*eval)(tarval *)) {
2009 int i, n = get_irn_arity(phi);
2011 NEW_ARR_A(void *, res, n);
2012 for (i = 0; i < n; ++i) {
2013 pred = get_irn_n(phi, i);
2014 tv = get_Const_tarval(pred);
2017 if (tv == tarval_bad) {
2018 /* folding failed, bad */
2023 mode = get_irn_mode(phi);
2024 irg = current_ir_graph;
2025 for (i = 0; i < n; ++i) {
2026 pred = get_irn_n(phi, i);
2027 res[i] = new_r_Const_type(irg, get_irg_start_block(irg),
2028 mode, res[i], get_Const_type(pred));
2030 return new_r_Phi(irg, get_nodes_block(phi), n, (ir_node **)res, mode);
2031 } /* apply_unop_on_phi */
2034 * Apply a conversion on a constant operator (a Phi).
2036 * @param phi the Phi node
2038 * @return a new Phi node if the conversion was successful, NULL else
2040 static ir_node *apply_conv_on_phi(ir_node *phi, ir_mode *mode) {
2045 int i, n = get_irn_arity(phi);
2047 NEW_ARR_A(void *, res, n);
2048 for (i = 0; i < n; ++i) {
2049 pred = get_irn_n(phi, i);
2050 tv = get_Const_tarval(pred);
2051 tv = tarval_convert_to(tv, mode);
2053 if (tv == tarval_bad) {
2054 /* folding failed, bad */
2059 irg = current_ir_graph;
2060 for (i = 0; i < n; ++i) {
2061 pred = get_irn_n(phi, i);
2062 res[i] = new_r_Const_type(irg, get_irg_start_block(irg),
2063 mode, res[i], get_Const_type(pred));
2065 return new_r_Phi(irg, get_nodes_block(phi), n, (ir_node **)res, mode);
2066 } /* apply_conv_on_phi */
2069 * Transform AddP(P, ConvIs(Iu)), AddP(P, ConvIu(Is)) and
2070 * SubP(P, ConvIs(Iu)), SubP(P, ConvIu(Is)).
2071 * If possible, remove the Conv's.
2073 static ir_node *transform_node_AddSub(ir_node *n) {
2074 ir_mode *mode = get_irn_mode(n);
2076 if (mode_is_reference(mode)) {
2077 ir_node *left = get_binop_left(n);
2078 ir_node *right = get_binop_right(n);
2079 unsigned ref_bits = get_mode_size_bits(mode);
2081 if (is_Conv(left)) {
2082 ir_mode *lmode = get_irn_mode(left);
2083 unsigned bits = get_mode_size_bits(lmode);
2085 if (ref_bits == bits &&
2086 mode_is_int(lmode) &&
2087 get_mode_arithmetic(lmode) == irma_twos_complement) {
2088 ir_node *pre = get_Conv_op(left);
2089 ir_mode *pre_mode = get_irn_mode(pre);
2091 if (mode_is_int(pre_mode) &&
2092 get_mode_size_bits(pre_mode) == bits &&
2093 get_mode_arithmetic(pre_mode) == irma_twos_complement) {
2094 /* ok, this conv just changes to sign, moreover the calculation
2095 * is done with same number of bits as our address mode, so
2096 * we can ignore the conv as address calculation can be viewed
2097 * as either signed or unsigned
2099 set_binop_left(n, pre);
2104 if (is_Conv(right)) {
2105 ir_mode *rmode = get_irn_mode(right);
2106 unsigned bits = get_mode_size_bits(rmode);
2108 if (ref_bits == bits &&
2109 mode_is_int(rmode) &&
2110 get_mode_arithmetic(rmode) == irma_twos_complement) {
2111 ir_node *pre = get_Conv_op(right);
2112 ir_mode *pre_mode = get_irn_mode(pre);
2114 if (mode_is_int(pre_mode) &&
2115 get_mode_size_bits(pre_mode) == bits &&
2116 get_mode_arithmetic(pre_mode) == irma_twos_complement) {
2117 /* ok, this conv just changes to sign, moreover the calculation
2118 * is done with same number of bits as our address mode, so
2119 * we can ignore the conv as address calculation can be viewed
2120 * as either signed or unsigned
2122 set_binop_right(n, pre);
2127 /* let address arithmetic use unsigned modes */
2128 if (is_Const(right)) {
2129 ir_mode *rmode = get_irn_mode(right);
2131 if (mode_is_signed(rmode) && get_mode_arithmetic(rmode) == irma_twos_complement) {
2132 /* convert a AddP(P, *s) into AddP(P, *u) */
2133 ir_mode *nm = get_reference_mode_unsigned_eq(mode);
2135 ir_node *pre = new_r_Conv(current_ir_graph, get_nodes_block(n), right, nm);
2136 set_binop_right(n, pre);
2142 } /* transform_node_AddSub */
2144 #define HANDLE_BINOP_PHI(eval, a, b, c, mode) \
2146 if (is_Const(b) && is_const_Phi(a)) { \
2147 /* check for Op(Phi, Const) */ \
2148 c = apply_binop_on_phi(a, get_Const_tarval(b), eval, mode, 0);\
2150 else if (is_Const(a) && is_const_Phi(b)) { \
2151 /* check for Op(Const, Phi) */ \
2152 c = apply_binop_on_phi(b, get_Const_tarval(a), eval, mode, 1);\
2154 else if (is_const_Phi(a) && is_const_Phi(b)) { \
2155 /* check for Op(Phi, Phi) */ \
2156 c = apply_binop_on_2_phis(a, b, eval, mode); \
2159 DBG_OPT_ALGSIM0(oldn, c, FS_OPT_CONST_PHI); \
2163 #define HANDLE_UNOP_PHI(eval, a, c) \
2165 if (is_const_Phi(a)) { \
2166 /* check for Op(Phi) */ \
2167 c = apply_unop_on_phi(a, eval); \
2169 DBG_OPT_ALGSIM0(oldn, c, FS_OPT_CONST_PHI); \
2175 * Do the AddSub optimization, then Transform
2176 * Constant folding on Phi
2177 * Add(a,a) -> Mul(a, 2)
2178 * Add(Mul(a, x), a) -> Mul(a, x+1)
2179 * if the mode is integer or float.
2180 * Transform Add(a,-b) into Sub(a,b).
2181 * Reassociation might fold this further.
2183 static ir_node *transform_node_Add(ir_node *n) {
2185 ir_node *a, *b, *c, *oldn = n;
2187 n = transform_node_AddSub(n);
2189 a = get_Add_left(n);
2190 b = get_Add_right(n);
2192 mode = get_irn_mode(n);
2194 if (mode_is_reference(mode)) {
2195 ir_mode *lmode = get_irn_mode(a);
2197 if (is_Const(b) && is_Const_null(b) && mode_is_int(lmode)) {
2198 /* an Add(a, NULL) is a hidden Conv */
2199 dbg_info *dbg = get_irn_dbg_info(n);
2200 return new_rd_Conv(dbg, current_ir_graph, get_nodes_block(n), a, mode);
2204 HANDLE_BINOP_PHI(tarval_add, a, b, c, mode);
2206 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
2207 if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
2210 if (mode_is_num(mode)) {
2211 /* the following code leads to endless recursion when Mul are replaced by a simple instruction chain */
2212 if (!is_arch_dep_running() && a == b && mode_is_int(mode)) {
2213 ir_node *block = get_nodes_block(n);
2216 get_irn_dbg_info(n),
2220 new_r_Const_long(current_ir_graph, block, mode, 2),
2222 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_A);
2227 get_irn_dbg_info(n),
2233 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_MINUS_B);
2238 get_irn_dbg_info(n),
2244 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_MINUS_B);
2247 if (get_mode_arithmetic(mode) == irma_twos_complement) {
2248 /* Here we rely on constants be on the RIGHT side */
2250 ir_node *op = get_Not_op(a);
2252 if (is_Const(b) && is_Const_one(b)) {
2254 ir_node *blk = get_irn_n(n, -1);
2255 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, blk, op, mode);
2256 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_PLUS_1);
2261 ir_node *blk = get_irn_n(n, -1);
2262 n = new_r_Const(current_ir_graph, blk, mode, get_mode_minus_one(mode));
2263 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_X_NOT_X);
2268 ir_node *op = get_Not_op(b);
2272 ir_node *blk = get_irn_n(n, -1);
2273 n = new_r_Const(current_ir_graph, blk, mode, get_mode_minus_one(mode));
2274 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_X_NOT_X);
2281 } /* transform_node_Add */
2284 * returns -cnst or NULL if impossible
2286 static ir_node *const_negate(ir_node *cnst) {
2287 tarval *tv = tarval_neg(get_Const_tarval(cnst));
2288 dbg_info *dbgi = get_irn_dbg_info(cnst);
2289 ir_graph *irg = get_irn_irg(cnst);
2290 ir_node *block = get_nodes_block(cnst);
2291 ir_mode *mode = get_irn_mode(cnst);
2292 if (tv == tarval_bad) return NULL;
2293 return new_rd_Const(dbgi, irg, block, mode, tv);
2297 * Do the AddSub optimization, then Transform
2298 * Constant folding on Phi
2299 * Sub(0,a) -> Minus(a)
2300 * Sub(Mul(a, x), a) -> Mul(a, x-1)
2301 * Sub(Sub(x, y), b) -> Sub(x, Add(y,b))
2302 * Sub(Add(a, x), x) -> a
2303 * Sub(x, Add(x, a)) -> -a
2304 * Sub(x, Const) -> Add(x, -Const)
2306 static ir_node *transform_node_Sub(ir_node *n) {
2311 n = transform_node_AddSub(n);
2313 a = get_Sub_left(n);
2314 b = get_Sub_right(n);
2316 mode = get_irn_mode(n);
2318 if (mode_is_int(mode)) {
2319 ir_mode *lmode = get_irn_mode(a);
2321 if (is_Const(b) && is_Const_null(b) && mode_is_reference(lmode)) {
2322 /* a Sub(a, NULL) is a hidden Conv */
2323 dbg_info *dbg = get_irn_dbg_info(n);
2324 return new_rd_Conv(dbg, current_ir_graph, get_nodes_block(n), a, mode);
2329 HANDLE_BINOP_PHI(tarval_sub, a, b, c, mode);
2331 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
2332 if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
2335 if (is_Const(b) && get_irn_mode(b) != mode_P) {
2336 /* a - C -> a + (-C) */
2337 ir_node *cnst = const_negate(b);
2339 ir_node *block = get_nodes_block(n);
2340 dbg_info *dbgi = get_irn_dbg_info(n);
2341 ir_graph *irg = get_irn_irg(n);
2343 n = new_rd_Add(dbgi, irg, block, a, cnst, mode);
2344 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2349 if (is_Minus(a)) { /* (-a) - b -> -(a + b) */
2350 ir_graph *irg = current_ir_graph;
2351 dbg_info *dbg = get_irn_dbg_info(n);
2352 ir_node *block = get_nodes_block(n);
2353 ir_node *left = get_Minus_op(a);
2354 ir_node *add = new_rd_Add(dbg, irg, block, left, b, mode);
2356 n = new_rd_Minus(dbg, irg, block, add, mode);
2357 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2359 } else if (is_Minus(b)) { /* a - (-b) -> a + b */
2360 ir_graph *irg = current_ir_graph;
2361 dbg_info *dbg = get_irn_dbg_info(n);
2362 ir_node *block = get_nodes_block(n);
2363 ir_node *right = get_Minus_op(b);
2365 n = new_rd_Add(dbg, irg, block, a, right, mode);
2366 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MINUS);
2368 } else if (is_Sub(b)) { /* a - (b - c) -> a + (c - b) */
2369 ir_graph *irg = current_ir_graph;
2370 dbg_info *s_dbg = get_irn_dbg_info(b);
2371 ir_node *s_block = get_nodes_block(b);
2372 ir_node *s_left = get_Sub_right(b);
2373 ir_node *s_right = get_Sub_left(b);
2374 ir_mode *s_mode = get_irn_mode(b);
2375 ir_node *sub = new_rd_Sub(s_dbg, irg, s_block, s_left, s_right, s_mode);
2376 dbg_info *a_dbg = get_irn_dbg_info(n);
2377 ir_node *a_block = get_nodes_block(n);
2379 n = new_rd_Add(a_dbg, irg, a_block, a, sub, mode);
2380 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2382 } else if (is_Mul(b)) { /* a - (b * C) -> a + (b * -C) */
2383 ir_node *m_right = get_Mul_right(b);
2384 if (is_Const(m_right)) {
2385 ir_node *cnst2 = const_negate(m_right);
2386 if (cnst2 != NULL) {
2387 ir_graph *irg = current_ir_graph;
2388 dbg_info *m_dbg = get_irn_dbg_info(b);
2389 ir_node *m_block = get_nodes_block(b);
2390 ir_node *m_left = get_Mul_left(b);
2391 ir_mode *m_mode = get_irn_mode(b);
2392 ir_node *mul = new_rd_Mul(m_dbg, irg, m_block, m_left, cnst2, m_mode);
2393 dbg_info *a_dbg = get_irn_dbg_info(n);
2394 ir_node *a_block = get_nodes_block(n);
2396 n = new_rd_Add(a_dbg, irg, a_block, a, mul, mode);
2397 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2403 /* Beware of Sub(P, P) which cannot be optimized into a simple Minus ... */
2404 if (mode_is_num(mode) && mode == get_irn_mode(a) && is_Const(a) && is_Const_null(a)) {
2406 get_irn_dbg_info(n),
2411 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_0_A);
2415 if (mode_wrap_around(mode)) {
2416 ir_node *left = get_Add_left(a);
2417 ir_node *right = get_Add_right(a);
2419 /* FIXME: Does the Conv's work only for two complement or generally? */
2421 if (mode != get_irn_mode(right)) {
2422 /* This Sub is an effective Cast */
2423 right = new_r_Conv(get_irn_irg(n), get_irn_n(n, -1), right, mode);
2426 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2428 } else if (right == b) {
2429 if (mode != get_irn_mode(left)) {
2430 /* This Sub is an effective Cast */
2431 left = new_r_Conv(get_irn_irg(n), get_irn_n(n, -1), left, mode);
2434 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2440 if (mode_wrap_around(mode)) {
2441 ir_node *left = get_Add_left(b);
2442 ir_node *right = get_Add_right(b);
2444 /* FIXME: Does the Conv's work only for two complement or generally? */
2446 ir_mode *r_mode = get_irn_mode(right);
2448 n = new_r_Minus(get_irn_irg(n), get_irn_n(n, -1), right, r_mode);
2449 if (mode != r_mode) {
2450 /* This Sub is an effective Cast */
2451 n = new_r_Conv(get_irn_irg(n), get_irn_n(n, -1), n, mode);
2453 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2455 } else if (right == a) {
2456 ir_mode *l_mode = get_irn_mode(left);
2458 n = new_r_Minus(get_irn_irg(n), get_irn_n(n, -1), left, l_mode);
2459 if (mode != l_mode) {
2460 /* This Sub is an effective Cast */
2461 n = new_r_Conv(get_irn_irg(n), get_irn_n(n, -1), n, mode);
2463 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2468 if (mode_is_int(mode) && is_Conv(a) && is_Conv(b)) {
2469 ir_mode *mode = get_irn_mode(a);
2471 if (mode == get_irn_mode(b)) {
2473 ir_node *op_a = get_Conv_op(a);
2474 ir_node *op_b = get_Conv_op(b);
2476 /* check if it's allowed to skip the conv */
2477 ma = get_irn_mode(op_a);
2478 mb = get_irn_mode(op_b);
2480 if (mode_is_reference(ma) && mode_is_reference(mb)) {
2481 /* SubInt(ConvInt(aP), ConvInt(bP)) -> SubInt(aP,bP) */
2484 set_Sub_right(n, b);
2490 /* do NOT execute this code if reassociation is enabled, it does the inverse! */
2491 if (!is_reassoc_running() && is_Mul(a)) {
2492 ir_node *ma = get_Mul_left(a);
2493 ir_node *mb = get_Mul_right(a);
2496 ir_node *blk = get_irn_n(n, -1);
2498 get_irn_dbg_info(n),
2499 current_ir_graph, blk,
2502 get_irn_dbg_info(n),
2503 current_ir_graph, blk,
2505 new_r_Const_long(current_ir_graph, blk, mode, 1),
2508 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MUL_A_X_A);
2510 } else if (mb == b) {
2511 ir_node *blk = get_irn_n(n, -1);
2513 get_irn_dbg_info(n),
2514 current_ir_graph, blk,
2517 get_irn_dbg_info(n),
2518 current_ir_graph, blk,
2520 new_r_Const_long(current_ir_graph, blk, mode, 1),
2523 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MUL_A_X_A);
2527 if (is_Sub(a)) { /* (x - y) - b -> x - (y + b) */
2528 ir_node *x = get_Sub_left(a);
2529 ir_node *y = get_Sub_right(a);
2530 ir_node *blk = get_irn_n(n, -1);
2531 ir_mode *m_b = get_irn_mode(b);
2532 ir_mode *m_y = get_irn_mode(y);
2536 /* Determine the right mode for the Add. */
2539 else if (mode_is_reference(m_b))
2541 else if (mode_is_reference(m_y))
2545 * Both modes are different but none is reference,
2546 * happens for instance in SubP(SubP(P, Iu), Is).
2547 * We have two possibilities here: Cast or ignore.
2548 * Currently we ignore this case.
2553 add = new_r_Add(current_ir_graph, blk, y, b, add_mode);
2555 n = new_rd_Sub(get_irn_dbg_info(n), current_ir_graph, blk, x, add, mode);
2556 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_SUB_X_Y_Z);
2560 if (get_mode_arithmetic(mode) == irma_twos_complement) {
2561 if (is_Const(a) && is_Not(b)) {
2562 /* c - ~X = X + (c+1) */
2563 tarval *tv = get_Const_tarval(a);
2565 tv = tarval_add(tv, get_mode_one(mode));
2566 if (tv != tarval_bad) {
2567 ir_node *blk = get_irn_n(n, -1);
2568 ir_node *c = new_r_Const(current_ir_graph, blk, mode, tv);
2569 n = new_rd_Add(get_irn_dbg_info(n), current_ir_graph, blk, get_Not_op(b), c, mode);
2570 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_C_NOT_X);
2576 } /* transform_node_Sub */
2579 * Several transformation done on n*n=2n bits mul.
2580 * These transformations must be done here because new nodes may be produced.
2582 static ir_node *transform_node_Mul2n(ir_node *n, ir_mode *mode) {
2584 ir_node *a = get_Mul_left(n);
2585 ir_node *b = get_Mul_right(n);
2586 tarval *ta = value_of(a);
2587 tarval *tb = value_of(b);
2588 ir_mode *smode = get_irn_mode(a);
2590 if (ta == get_mode_one(smode)) {
2591 /* (L)1 * (L)b = (L)b */
2592 ir_node *blk = get_irn_n(n, -1);
2593 n = new_rd_Conv(get_irn_dbg_info(n), current_ir_graph, blk, b, mode);
2594 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
2597 else if (ta == get_mode_minus_one(smode)) {
2598 /* (L)-1 * (L)b = (L)b */
2599 ir_node *blk = get_irn_n(n, -1);
2600 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, blk, b, smode);
2601 n = new_rd_Conv(get_irn_dbg_info(n), current_ir_graph, blk, n, mode);
2602 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2605 if (tb == get_mode_one(smode)) {
2606 /* (L)a * (L)1 = (L)a */
2607 ir_node *blk = get_irn_n(a, -1);
2608 n = new_rd_Conv(get_irn_dbg_info(n), current_ir_graph, blk, a, mode);
2609 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
2612 else if (tb == get_mode_minus_one(smode)) {
2613 /* (L)a * (L)-1 = (L)-a */
2614 ir_node *blk = get_irn_n(n, -1);
2615 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, blk, a, smode);
2616 n = new_rd_Conv(get_irn_dbg_info(n), current_ir_graph, blk, n, mode);
2617 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2624 * Transform Mul(a,-1) into -a.
2625 * Do constant evaluation of Phi nodes.
2626 * Do architecture dependent optimizations on Mul nodes
2628 static ir_node *transform_node_Mul(ir_node *n) {
2629 ir_node *c, *oldn = n;
2630 ir_mode *mode = get_irn_mode(n);
2631 ir_node *a = get_Mul_left(n);
2632 ir_node *b = get_Mul_right(n);
2634 if (is_Bad(a) || is_Bad(b))
2637 if (mode != get_irn_mode(a))
2638 return transform_node_Mul2n(n, mode);
2640 HANDLE_BINOP_PHI(tarval_mul, a, b, c, mode);
2642 if (mode_is_signed(mode)) {
2645 if (value_of(a) == get_mode_minus_one(mode))
2647 else if (value_of(b) == get_mode_minus_one(mode))
2650 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1), r, mode);
2651 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2656 if (is_Const(b)) { /* (-a) * const -> a * -const */
2657 ir_node *cnst = const_negate(b);
2659 dbg_info *dbgi = get_irn_dbg_info(n);
2660 ir_node *block = get_nodes_block(n);
2661 n = new_rd_Mul(dbgi, current_ir_graph, block, get_Minus_op(a), cnst, mode);
2662 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2665 } else if (is_Minus(b)) { /* (-a) * (-b) -> a * b */
2666 dbg_info *dbgi = get_irn_dbg_info(n);
2667 ir_node *block = get_nodes_block(n);
2668 n = new_rd_Mul(dbgi, current_ir_graph, block, get_Minus_op(a), get_Minus_op(b), mode);
2669 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_MINUS);
2671 } else if (is_Sub(b)) { /* (-a) * (b - c) -> a * (c - b) */
2672 ir_node *sub_l = get_Sub_left(b);
2673 ir_node *sub_r = get_Sub_right(b);
2674 dbg_info *dbgi = get_irn_dbg_info(n);
2675 ir_graph *irg = current_ir_graph;
2676 ir_node *block = get_nodes_block(n);
2677 ir_node *new_b = new_rd_Sub(dbgi, irg, block, sub_r, sub_l, mode);
2678 n = new_rd_Mul(dbgi, irg, block, get_Minus_op(a), new_b, mode);
2679 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS);
2682 } else if (is_Minus(b)) {
2683 if (is_Sub(a)) { /* (a - b) * (-c) -> (b - a) * c */
2684 ir_node *sub_l = get_Sub_left(a);
2685 ir_node *sub_r = get_Sub_right(a);
2686 dbg_info *dbgi = get_irn_dbg_info(n);
2687 ir_graph *irg = current_ir_graph;
2688 ir_node *block = get_nodes_block(n);
2689 ir_node *new_a = new_rd_Sub(dbgi, irg, block, sub_r, sub_l, mode);
2690 n = new_rd_Mul(dbgi, irg, block, new_a, get_Minus_op(b), mode);
2691 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS);
2695 if (get_mode_arithmetic(mode) == irma_ieee754) {
2697 tarval *tv = get_Const_tarval(a);
2698 if (tarval_ieee754_get_exponent(tv) == 1 && tarval_ieee754_zero_mantissa(tv)) {
2699 /* 2.0 * b = b + b */
2700 n = new_rd_Add(get_irn_dbg_info(n), current_ir_graph, get_nodes_block(n), b, b, mode);
2701 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_A_A);
2705 else if (is_Const(b)) {
2706 tarval *tv = get_Const_tarval(b);
2707 if (tarval_ieee754_get_exponent(tv) == 1 && tarval_ieee754_zero_mantissa(tv)) {
2708 /* a * 2.0 = a + a */
2709 n = new_rd_Add(get_irn_dbg_info(n), current_ir_graph, get_nodes_block(n), a, a, mode);
2710 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_A_A);
2715 return arch_dep_replace_mul_with_shifts(n);
2716 } /* transform_node_Mul */
2719 * Transform a Div Node.
2721 static ir_node *transform_node_Div(ir_node *n) {
2722 ir_mode *mode = get_Div_resmode(n);
2723 ir_node *a = get_Div_left(n);
2724 ir_node *b = get_Div_right(n);
2728 if (is_Const(b) && is_const_Phi(a)) {
2729 /* check for Div(Phi, Const) */
2730 value = apply_binop_on_phi(a, get_Const_tarval(b), tarval_div, mode, 0);
2732 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2736 else if (is_Const(a) && is_const_Phi(b)) {
2737 /* check for Div(Const, Phi) */
2738 value = apply_binop_on_phi(b, get_Const_tarval(a), tarval_div, mode, 1);
2740 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2744 else if (is_const_Phi(a) && is_const_Phi(b)) {
2745 /* check for Div(Phi, Phi) */
2746 value = apply_binop_on_2_phis(a, b, tarval_div, mode);
2748 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2755 if (tv != tarval_bad) {
2756 value = new_Const(get_tarval_mode(tv), tv);
2758 DBG_OPT_CSTEVAL(n, value);
2761 ir_node *a = get_Div_left(n);
2762 ir_node *b = get_Div_right(n);
2763 const ir_node *dummy;
2765 if (a == b && value_not_zero(a, &dummy)) {
2766 /* BEWARE: we can optimize a/a to 1 only if this cannot cause a exception */
2767 value = new_Const(mode, get_mode_one(mode));
2768 DBG_OPT_CSTEVAL(n, value);
2771 if (mode_is_signed(mode) && is_Const(b)) {
2772 tarval *tv = get_Const_tarval(b);
2774 if (tv == get_mode_minus_one(mode)) {
2776 value = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1), a, mode);
2777 DBG_OPT_CSTEVAL(n, value);
2781 /* Try architecture dependent optimization */
2782 value = arch_dep_replace_div_by_const(n);
2790 /* Turn Div into a tuple (mem, jmp, bad, value) */
2791 mem = get_Div_mem(n);
2792 blk = get_irn_n(n, -1);
2794 /* skip a potential Pin */
2796 mem = get_Pin_op(mem);
2797 turn_into_tuple(n, pn_Div_max);
2798 set_Tuple_pred(n, pn_Div_M, mem);
2799 set_Tuple_pred(n, pn_Div_X_regular, new_r_Jmp(current_ir_graph, blk));
2800 set_Tuple_pred(n, pn_Div_X_except, new_Bad());
2801 set_Tuple_pred(n, pn_Div_res, value);
2804 } /* transform_node_Div */
2807 * Transform a Mod node.
2809 static ir_node *transform_node_Mod(ir_node *n) {
2810 ir_mode *mode = get_Mod_resmode(n);
2811 ir_node *a = get_Mod_left(n);
2812 ir_node *b = get_Mod_right(n);
2816 if (is_Const(b) && is_const_Phi(a)) {
2817 /* check for Div(Phi, Const) */
2818 value = apply_binop_on_phi(a, get_Const_tarval(b), tarval_mod, mode, 0);
2820 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2824 else if (is_Const(a) && is_const_Phi(b)) {
2825 /* check for Div(Const, Phi) */
2826 value = apply_binop_on_phi(b, get_Const_tarval(a), tarval_mod, mode, 1);
2828 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2832 else if (is_const_Phi(a) && is_const_Phi(b)) {
2833 /* check for Div(Phi, Phi) */
2834 value = apply_binop_on_2_phis(a, b, tarval_mod, mode);
2836 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2843 if (tv != tarval_bad) {
2844 value = new_Const(get_tarval_mode(tv), tv);
2846 DBG_OPT_CSTEVAL(n, value);
2849 ir_node *a = get_Mod_left(n);
2850 ir_node *b = get_Mod_right(n);
2851 const ir_node *dummy;
2853 if (a == b && value_not_zero(a, &dummy)) {
2854 /* BEWARE: we can optimize a%a to 0 only if this cannot cause a exception */
2855 value = new_Const(mode, get_mode_null(mode));
2856 DBG_OPT_CSTEVAL(n, value);
2859 if (mode_is_signed(mode) && is_Const(b)) {
2860 tarval *tv = get_Const_tarval(b);
2862 if (tv == get_mode_minus_one(mode)) {
2864 value = new_Const(mode, get_mode_null(mode));
2865 DBG_OPT_CSTEVAL(n, value);
2869 /* Try architecture dependent optimization */
2870 value = arch_dep_replace_mod_by_const(n);
2878 /* Turn Mod into a tuple (mem, jmp, bad, value) */
2879 mem = get_Mod_mem(n);
2880 blk = get_irn_n(n, -1);
2882 /* skip a potential Pin */
2884 mem = get_Pin_op(mem);
2885 turn_into_tuple(n, pn_Mod_max);
2886 set_Tuple_pred(n, pn_Mod_M, mem);
2887 set_Tuple_pred(n, pn_Mod_X_regular, new_r_Jmp(current_ir_graph, blk));
2888 set_Tuple_pred(n, pn_Mod_X_except, new_Bad());
2889 set_Tuple_pred(n, pn_Mod_res, value);
2892 } /* transform_node_Mod */
2895 * Transform a DivMod node.
2897 static ir_node *transform_node_DivMod(ir_node *n) {
2898 const ir_node *dummy;
2899 ir_node *a = get_DivMod_left(n);
2900 ir_node *b = get_DivMod_right(n);
2901 ir_mode *mode = get_DivMod_resmode(n);
2906 if (is_Const(b) && is_const_Phi(a)) {
2907 /* check for Div(Phi, Const) */
2908 va = apply_binop_on_phi(a, get_Const_tarval(b), tarval_div, mode, 0);
2909 vb = apply_binop_on_phi(a, get_Const_tarval(b), tarval_mod, mode, 0);
2911 DBG_OPT_ALGSIM0(n, va, FS_OPT_CONST_PHI);
2912 DBG_OPT_ALGSIM0(n, vb, FS_OPT_CONST_PHI);
2916 else if (is_Const(a) && is_const_Phi(b)) {
2917 /* check for Div(Const, Phi) */
2918 va = apply_binop_on_phi(b, get_Const_tarval(a), tarval_div, mode, 1);
2919 vb = apply_binop_on_phi(b, get_Const_tarval(a), tarval_mod, mode, 1);
2921 DBG_OPT_ALGSIM0(n, va, FS_OPT_CONST_PHI);
2922 DBG_OPT_ALGSIM0(n, vb, FS_OPT_CONST_PHI);
2926 else if (is_const_Phi(a) && is_const_Phi(b)) {
2927 /* check for Div(Phi, Phi) */
2928 va = apply_binop_on_2_phis(a, b, tarval_div, mode);
2929 vb = apply_binop_on_2_phis(a, b, tarval_mod, mode);
2931 DBG_OPT_ALGSIM0(n, va, FS_OPT_CONST_PHI);
2932 DBG_OPT_ALGSIM0(n, vb, FS_OPT_CONST_PHI);
2939 if (tb != tarval_bad) {
2940 if (tb == get_mode_one(get_tarval_mode(tb))) {
2942 vb = new_Const(mode, get_mode_null(mode));
2943 DBG_OPT_CSTEVAL(n, vb);
2945 } else if (ta != tarval_bad) {
2946 tarval *resa, *resb;
2947 resa = tarval_div(ta, tb);
2948 if (resa == tarval_bad) return n; /* Causes exception!!! Model by replacing through
2949 Jmp for X result!? */
2950 resb = tarval_mod(ta, tb);
2951 if (resb == tarval_bad) return n; /* Causes exception! */
2952 va = new_Const(mode, resa);
2953 vb = new_Const(mode, resb);
2954 DBG_OPT_CSTEVAL(n, va);
2955 DBG_OPT_CSTEVAL(n, vb);
2957 } else if (mode_is_signed(mode) && tb == get_mode_minus_one(mode)) {
2958 va = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1), a, mode);
2959 vb = new_Const(mode, get_mode_null(mode));
2960 DBG_OPT_CSTEVAL(n, va);
2961 DBG_OPT_CSTEVAL(n, vb);
2963 } else { /* Try architecture dependent optimization */
2966 arch_dep_replace_divmod_by_const(&va, &vb, n);
2967 evaluated = va != NULL;
2969 } else if (a == b) {
2970 if (value_not_zero(a, &dummy)) {
2972 va = new_Const(mode, get_mode_one(mode));
2973 vb = new_Const(mode, get_mode_null(mode));
2974 DBG_OPT_CSTEVAL(n, va);
2975 DBG_OPT_CSTEVAL(n, vb);
2978 /* BEWARE: it is NOT possible to optimize a/a to 1, as this may cause a exception */
2981 } else if (ta == get_mode_null(mode) && value_not_zero(b, &dummy)) {
2982 /* 0 / non-Const = 0 */
2987 if (evaluated) { /* replace by tuple */
2991 mem = get_DivMod_mem(n);
2992 /* skip a potential Pin */
2994 mem = get_Pin_op(mem);
2996 blk = get_irn_n(n, -1);
2997 turn_into_tuple(n, pn_DivMod_max);
2998 set_Tuple_pred(n, pn_DivMod_M, mem);
2999 set_Tuple_pred(n, pn_DivMod_X_regular, new_r_Jmp(current_ir_graph, blk));
3000 set_Tuple_pred(n, pn_DivMod_X_except, new_Bad()); /* no exception */
3001 set_Tuple_pred(n, pn_DivMod_res_div, va);
3002 set_Tuple_pred(n, pn_DivMod_res_mod, vb);
3006 } /* transform_node_DivMod */
3009 * Optimize x / c to x * (1/c)
3011 static ir_node *transform_node_Quot(ir_node *n) {
3012 ir_mode *mode = get_Quot_resmode(n);
3015 if (get_mode_arithmetic(mode) == irma_ieee754) {
3016 ir_node *b = get_Quot_right(n);
3019 tarval *tv = get_Const_tarval(b);
3023 * Floating point constant folding might be disabled here to
3025 * However, as we check for exact result, doing it is safe.
3028 rem = tarval_enable_fp_ops(1);
3029 tv = tarval_quo(get_mode_one(mode), tv);
3030 (void)tarval_enable_fp_ops(rem);
3032 /* Do the transformation if the result is either exact or we are not
3033 using strict rules. */
3034 if (tv != tarval_bad &&
3035 (tarval_ieee754_get_exact() || (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic) == 0)) {
3036 ir_node *blk = get_irn_n(n, -1);
3037 ir_node *c = new_r_Const(current_ir_graph, blk, mode, tv);
3038 ir_node *a = get_Quot_left(n);
3039 ir_node *m = new_rd_Mul(get_irn_dbg_info(n), current_ir_graph, blk, a, c, mode);
3040 ir_node *mem = get_Quot_mem(n);
3042 /* skip a potential Pin */
3044 mem = get_Pin_op(mem);
3045 turn_into_tuple(n, pn_Quot_max);
3046 set_Tuple_pred(n, pn_Quot_M, mem);
3047 set_Tuple_pred(n, pn_Quot_X_regular, new_r_Jmp(current_ir_graph, blk));
3048 set_Tuple_pred(n, pn_Quot_X_except, new_r_Bad(current_ir_graph));
3049 set_Tuple_pred(n, pn_Quot_res, m);
3050 DBG_OPT_ALGSIM1(oldn, a, b, m, FS_OPT_FP_INV_MUL);
3055 } /* transform_node_Quot */
3058 * Optimize Abs(x) into x if x is Confirmed >= 0
3059 * Optimize Abs(x) into -x if x is Confirmed <= 0
3060 * Optimize Abs(-x) int Abs(x)
3062 static ir_node *transform_node_Abs(ir_node *n) {
3063 ir_node *c, *oldn = n;
3064 ir_node *a = get_Abs_op(n);
3067 HANDLE_UNOP_PHI(tarval_abs, a, c);
3069 switch (classify_value_sign(a)) {
3070 case value_classified_negative:
3071 mode = get_irn_mode(n);
3074 * We can replace the Abs by -x here.
3075 * We even could add a new Confirm here
3076 * (if not twos complement)
3078 * Note that -x would create a new node, so we could
3079 * not run it in the equivalent_node() context.
3081 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph,
3082 get_nodes_block(n), a, mode);
3084 DBG_OPT_CONFIRM(oldn, n);
3086 case value_classified_positive:
3087 /* n is positive, Abs is not needed */
3090 DBG_OPT_CONFIRM(oldn, n);
3096 /* Abs(-x) = Abs(x) */
3097 mode = get_irn_mode(n);
3098 n = new_rd_Abs(get_irn_dbg_info(n), current_ir_graph,
3099 get_nodes_block(n), get_Minus_op(a), mode);
3100 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ABS_MINUS_X);
3104 } /* transform_node_Abs */
3107 * Optimize -a CMP -b into b CMP a.
3108 * This works only for for modes where unary Minus
3110 * Note that two-complement integers can Overflow
3111 * so it will NOT work.
3113 * For == and != can be handled in Proj(Cmp)
3115 static ir_node *transform_node_Cmp(ir_node *n) {
3117 ir_node *left = get_Cmp_left(n);
3118 ir_node *right = get_Cmp_right(n);
3120 if (is_Minus(left) && is_Minus(right) &&
3121 !mode_overflow_on_unary_Minus(get_irn_mode(left))) {
3122 ir_node *const new_left = get_Minus_op(right);
3123 ir_node *const new_right = get_Minus_op(left);
3124 n = new_rd_Cmp(get_irn_dbg_info(n), current_ir_graph,
3125 get_nodes_block(n), new_left, new_right);
3126 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CMP_OP_OP);
3129 } /* transform_node_Cmp */
3133 * Transform a Cond node.
3135 * Replace the Cond by a Jmp if it branches on a constant
3138 static ir_node *transform_node_Cond(ir_node *n) {
3141 ir_node *a = get_Cond_selector(n);
3142 tarval *ta = value_of(a);
3144 /* we need block info which is not available in floating irgs */
3145 if (get_irg_pinned(current_ir_graph) == op_pin_state_floats)
3148 if ((ta != tarval_bad) &&
3149 (get_irn_mode(a) == mode_b) &&
3150 (get_opt_unreachable_code())) {
3151 /* It's a boolean Cond, branching on a boolean constant.
3152 Replace it by a tuple (Bad, Jmp) or (Jmp, Bad) */
3153 ir_node *blk = get_nodes_block(n);
3154 jmp = new_r_Jmp(current_ir_graph, blk);
3155 turn_into_tuple(n, pn_Cond_max);
3156 if (ta == tarval_b_true) {
3157 set_Tuple_pred(n, pn_Cond_false, new_Bad());
3158 set_Tuple_pred(n, pn_Cond_true, jmp);
3160 set_Tuple_pred(n, pn_Cond_false, jmp);
3161 set_Tuple_pred(n, pn_Cond_true, new_Bad());
3163 /* We might generate an endless loop, so keep it alive. */
3164 add_End_keepalive(get_irg_end(current_ir_graph), blk);
3167 } /* transform_node_Cond */
3170 * Prototype of a recursive transform function
3171 * for bitwise distributive transformations.
3173 typedef ir_node* (*recursive_transform)(ir_node *n);
3176 * makes use of distributive laws for and, or, eor
3177 * and(a OP c, b OP c) -> and(a, b) OP c
3178 * note, might return a different op than n
3180 static ir_node *transform_bitwise_distributive(ir_node *n,
3181 recursive_transform trans_func)
3184 ir_node *a = get_binop_left(n);
3185 ir_node *b = get_binop_right(n);
3186 ir_op *op = get_irn_op(a);
3187 ir_op *op_root = get_irn_op(n);
3189 if(op != get_irn_op(b))
3192 if (op == op_Conv) {
3193 ir_node *a_op = get_Conv_op(a);
3194 ir_node *b_op = get_Conv_op(b);
3195 ir_mode *a_mode = get_irn_mode(a_op);
3196 ir_mode *b_mode = get_irn_mode(b_op);
3197 if(a_mode == b_mode && (mode_is_int(a_mode) || a_mode == mode_b)) {
3198 ir_node *blk = get_irn_n(n, -1);
3201 set_binop_left(n, a_op);
3202 set_binop_right(n, b_op);
3203 set_irn_mode(n, a_mode);
3205 n = new_r_Conv(current_ir_graph, blk, n, get_irn_mode(oldn));
3207 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_SHIFT_AND);
3213 /* nothing to gain here */
3217 if (op == op_Shrs || op == op_Shr || op == op_Shl
3218 || op == op_And || op == op_Or || op == op_Eor) {
3219 ir_node *a_left = get_binop_left(a);
3220 ir_node *a_right = get_binop_right(a);
3221 ir_node *b_left = get_binop_left(b);
3222 ir_node *b_right = get_binop_right(b);
3224 ir_node *op1 = NULL;
3225 ir_node *op2 = NULL;
3227 if (is_op_commutative(op)) {
3228 if (a_left == b_left) {
3232 } else if(a_left == b_right) {
3236 } else if(a_right == b_left) {
3242 if(a_right == b_right) {
3249 /* (a sop c) & (b sop c) => (a & b) sop c */
3250 ir_node *blk = get_irn_n(n, -1);
3252 ir_node *new_n = exact_copy(n);
3253 set_binop_left(new_n, op1);
3254 set_binop_right(new_n, op2);
3255 new_n = trans_func(new_n);
3257 if(op_root == op_Eor && op == op_Or) {
3258 dbg_info *dbgi = get_irn_dbg_info(n);
3259 ir_graph *irg = current_ir_graph;
3260 ir_mode *mode = get_irn_mode(c);
3262 c = new_rd_Not(dbgi, irg, blk, c, mode);
3263 n = new_rd_And(dbgi, irg, blk, new_n, c, mode);
3266 set_nodes_block(n, blk);
3267 set_binop_left(n, new_n);
3268 set_binop_right(n, c);
3269 add_identities(current_ir_graph->value_table, n);
3272 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_SHIFT_AND);
3283 static ir_node *transform_node_And(ir_node *n) {
3284 ir_node *c, *oldn = n;
3285 ir_node *a = get_And_left(n);
3286 ir_node *b = get_And_right(n);
3289 mode = get_irn_mode(n);
3290 HANDLE_BINOP_PHI(tarval_and, a, b, c, mode);
3292 /* we can evaluate 2 Projs of the same Cmp */
3293 if (mode == mode_b && is_Proj(a) && is_Proj(b)) {
3294 ir_node *pred_a = get_Proj_pred(a);
3295 ir_node *pred_b = get_Proj_pred(b);
3296 if (pred_a == pred_b) {
3297 dbg_info *dbgi = get_irn_dbg_info(n);
3298 ir_node *block = get_nodes_block(pred_a);
3299 pn_Cmp pn_a = get_Proj_proj(a);
3300 pn_Cmp pn_b = get_Proj_proj(b);
3301 /* yes, we can simply calculate with pncs */
3302 pn_Cmp new_pnc = pn_a & pn_b;
3304 return new_rd_Proj(dbgi, current_ir_graph, block, pred_a, mode_b, new_pnc);
3309 ir_node *op = get_Not_op(b);
3311 ir_node *ba = get_And_left(op);
3312 ir_node *bb = get_And_right(op);
3314 /* it's enough to test the following cases due to normalization! */
3315 if (get_Or_left(a) == ba && get_Or_right(a) == bb) {
3316 /* (a|b) & ~(a&b) = a^b */
3317 ir_node *block = get_nodes_block(n);
3319 n = new_rd_Eor(get_irn_dbg_info(n), current_ir_graph, block, ba, bb, mode);
3320 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_TO_EOR);
3328 ir_node *op = get_Not_op(a);
3330 ir_node *aa = get_And_left(op);
3331 ir_node *ab = get_And_right(op);
3333 /* it's enough to test the following cases due to normalization! */
3334 if (get_Or_left(b) == aa && get_Or_right(b) == ab) {
3335 /* (a|b) & ~(a&b) = a^b */
3336 ir_node *block = get_nodes_block(n);
3338 n = new_rd_Eor(get_irn_dbg_info(n), current_ir_graph, block, aa, ab, mode);
3339 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_TO_EOR);
3346 ir_node *al = get_Eor_left(a);
3347 ir_node *ar = get_Eor_right(a);
3350 /* (b ^ a) & b -> ~a & b */
3351 dbg_info *dbg = get_irn_dbg_info(n);
3352 ir_node *block = get_nodes_block(n);
3354 ar = new_rd_Not(dbg, current_ir_graph, block, ar, mode);
3355 n = new_rd_And(dbg, current_ir_graph, block, ar, b, mode);
3356 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3360 /* (a ^ b) & b -> ~a & b */
3361 dbg_info *dbg = get_irn_dbg_info(n);
3362 ir_node *block = get_nodes_block(n);
3364 al = new_rd_Not(dbg, current_ir_graph, block, al, mode);
3365 n = new_rd_And(dbg, current_ir_graph, block, al, b, mode);
3366 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3371 ir_node *bl = get_Eor_left(b);
3372 ir_node *br = get_Eor_right(b);
3375 /* a & (a ^ b) -> a & ~b */
3376 dbg_info *dbg = get_irn_dbg_info(n);
3377 ir_node *block = get_nodes_block(n);
3379 br = new_rd_Not(dbg, current_ir_graph, block, br, mode);
3380 n = new_rd_And(dbg, current_ir_graph, block, br, a, mode);
3381 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3385 /* a & (b ^ a) -> a & ~b */
3386 dbg_info *dbg = get_irn_dbg_info(n);
3387 ir_node *block = get_nodes_block(n);
3389 bl = new_rd_Not(dbg, current_ir_graph, block, bl, mode);
3390 n = new_rd_And(dbg, current_ir_graph, block, bl, a, mode);
3391 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3395 if (is_Not(a) && is_Not(b)) {
3396 /* ~a & ~b = ~(a|b) */
3397 ir_node *block = get_nodes_block(n);
3398 ir_mode *mode = get_irn_mode(n);
3402 n = new_rd_Or(get_irn_dbg_info(n), current_ir_graph, block, a, b, mode);
3403 n = new_rd_Not(get_irn_dbg_info(n), current_ir_graph, block, n, mode);
3404 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_DEMORGAN);
3408 n = transform_bitwise_distributive(n, transform_node_And);
3411 } /* transform_node_And */
3416 static ir_node *transform_node_Eor(ir_node *n) {
3417 ir_node *c, *oldn = n;
3418 ir_node *a = get_Eor_left(n);
3419 ir_node *b = get_Eor_right(n);
3420 ir_mode *mode = get_irn_mode(n);
3422 HANDLE_BINOP_PHI(tarval_eor, a, b, c, mode);
3424 /* we can evaluate 2 Projs of the same Cmp */
3425 if (mode == mode_b && is_Proj(a) && is_Proj(b)) {
3426 ir_node *pred_a = get_Proj_pred(a);
3427 ir_node *pred_b = get_Proj_pred(b);
3428 if(pred_a == pred_b) {
3429 dbg_info *dbgi = get_irn_dbg_info(n);
3430 ir_node *block = get_nodes_block(pred_a);
3431 pn_Cmp pn_a = get_Proj_proj(a);
3432 pn_Cmp pn_b = get_Proj_proj(b);
3433 /* yes, we can simply calculate with pncs */
3434 pn_Cmp new_pnc = pn_a ^ pn_b;
3436 return new_rd_Proj(dbgi, current_ir_graph, block, pred_a, mode_b,
3443 n = new_rd_Const(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1),
3444 mode, get_mode_null(mode));
3445 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_A_A);
3446 } else if (mode == mode_b &&
3448 is_Const(b) && is_Const_one(b) &&
3449 is_Cmp(get_Proj_pred(a))) {
3450 /* The Eor negates a Cmp. The Cmp has the negated result anyways! */
3451 n = new_r_Proj(current_ir_graph, get_irn_n(n, -1), get_Proj_pred(a),
3452 mode_b, get_negated_pnc(get_Proj_proj(a), mode));
3454 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT_BOOL);
3455 } else if (is_Const(b)) {
3456 if (is_Not(a)) { /* ~x ^ const -> x ^ ~const */
3457 ir_node *cnst = new_Const(mode, tarval_not(get_Const_tarval(b)));
3458 ir_node *not_op = get_Not_op(a);
3459 dbg_info *dbg = get_irn_dbg_info(n);
3460 ir_graph *irg = current_ir_graph;
3461 ir_node *block = get_nodes_block(n);
3462 ir_mode *mode = get_irn_mode(n);
3463 n = new_rd_Eor(dbg, irg, block, not_op, cnst, mode);
3465 } else if (is_Const_all_one(b)) { /* x ^ 1...1 -> ~1 */
3466 n = new_r_Not(current_ir_graph, get_nodes_block(n), a, mode);
3467 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3470 n = transform_bitwise_distributive(n, transform_node_Eor);
3474 } /* transform_node_Eor */
3479 static ir_node *transform_node_Not(ir_node *n) {
3480 ir_node *c, *oldn = n;
3481 ir_node *a = get_Not_op(n);
3482 ir_mode *mode = get_irn_mode(n);
3484 HANDLE_UNOP_PHI(tarval_not,a,c);
3486 /* check for a boolean Not */
3487 if (mode == mode_b &&
3489 is_Cmp(get_Proj_pred(a))) {
3490 /* We negate a Cmp. The Cmp has the negated result anyways! */
3491 n = new_r_Proj(current_ir_graph, get_irn_n(n, -1), get_Proj_pred(a),
3492 mode_b, get_negated_pnc(get_Proj_proj(a), mode_b));
3493 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_CMP);
3497 ir_node *eor_b = get_Eor_right(a);
3498 if (is_Const(eor_b)) { /* ~(x ^ const) -> x ^ ~const */
3499 ir_node *cnst = new_Const(mode, tarval_not(get_Const_tarval(eor_b)));
3500 ir_node *eor_a = get_Eor_left(a);
3501 dbg_info *dbg = get_irn_dbg_info(n);
3502 ir_graph *irg = current_ir_graph;
3503 ir_node *block = get_nodes_block(n);
3504 ir_mode *mode = get_irn_mode(n);
3505 n = new_rd_Eor(dbg, irg, block, eor_a, cnst, mode);
3509 if (get_mode_arithmetic(mode) == irma_twos_complement) {
3510 if (is_Minus(a)) { /* ~-x -> x + -1 */
3511 dbg_info *dbg = get_irn_dbg_info(n);
3512 ir_graph *irg = current_ir_graph;
3513 ir_node *block = get_nodes_block(n);
3514 ir_node *add_l = get_Minus_op(a);
3515 ir_node *add_r = new_rd_Const(dbg, irg, block, mode, get_mode_minus_one(mode));
3516 n = new_rd_Add(dbg, irg, block, add_l, add_r, mode);
3517 } else if (is_Add(a)) {
3518 ir_node *add_r = get_Add_right(a);
3519 if (is_Const(add_r) && is_Const_all_one(add_r)) {
3520 /* ~(x + -1) = -x */
3521 ir_node *op = get_Add_left(a);
3522 ir_node *blk = get_irn_n(n, -1);
3523 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, blk, op, get_irn_mode(n));
3524 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_MINUS_1);
3529 } /* transform_node_Not */
3532 * Transform a Minus.
3536 * -(a >>u (size-1)) = a >>s (size-1)
3537 * -(a >>s (size-1)) = a >>u (size-1)
3538 * -(a * const) -> a * -const
3540 static ir_node *transform_node_Minus(ir_node *n) {
3541 ir_node *c, *oldn = n;
3542 ir_node *a = get_Minus_op(n);
3545 HANDLE_UNOP_PHI(tarval_neg,a,c);
3547 mode = get_irn_mode(a);
3548 if (get_mode_arithmetic(mode) == irma_twos_complement) {
3549 /* the following rules are only to twos-complement */
3552 ir_node *op = get_Not_op(a);
3553 tarval *tv = get_mode_one(mode);
3554 ir_node *blk = get_irn_n(n, -1);
3555 ir_node *c = new_r_Const(current_ir_graph, blk, mode, tv);
3556 n = new_rd_Add(get_irn_dbg_info(n), current_ir_graph, blk, op, c, mode);
3557 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_MINUS_NOT);
3561 ir_node *c = get_Shr_right(a);
3564 tarval *tv = get_Const_tarval(c);
3566 if (tarval_is_long(tv) && get_tarval_long(tv) == (int) get_mode_size_bits(mode) - 1) {
3567 /* -(a >>u (size-1)) = a >>s (size-1) */
3568 ir_node *v = get_Shr_left(a);
3570 n = new_rd_Shrs(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1), v, c, mode);
3571 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_PREDICATE);
3577 ir_node *c = get_Shrs_right(a);
3580 tarval *tv = get_Const_tarval(c);
3582 if (tarval_is_long(tv) && get_tarval_long(tv) == (int) get_mode_size_bits(mode) - 1) {
3583 /* -(a >>s (size-1)) = a >>u (size-1) */
3584 ir_node *v = get_Shrs_left(a);
3586 n = new_rd_Shr(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1), v, c, mode);
3587 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_PREDICATE);
3594 /* - (a-b) = b - a */
3595 ir_node *la = get_Sub_left(a);
3596 ir_node *ra = get_Sub_right(a);
3597 ir_node *blk = get_irn_n(n, -1);
3599 n = new_rd_Sub(get_irn_dbg_info(n), current_ir_graph, blk, ra, la, mode);
3600 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_MINUS_SUB);
3604 if (is_Mul(a)) { /* -(a * const) -> a * -const */
3605 ir_node *mul_l = get_Mul_left(a);
3606 ir_node *mul_r = get_Mul_right(a);
3607 if (is_Const(mul_r)) {
3608 tarval *tv = tarval_neg(get_Const_tarval(mul_r));
3609 if(tv != tarval_bad) {
3610 ir_node *cnst = new_Const(mode, tv);
3611 dbg_info *dbg = get_irn_dbg_info(a);
3612 ir_graph *irg = current_ir_graph;
3613 ir_node *block = get_nodes_block(a);
3614 n = new_rd_Mul(dbg, irg, block, mul_l, cnst, mode);
3615 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_MINUS_MUL_C);
3622 } /* transform_node_Minus */
3625 * Transform a Cast_type(Const) into a new Const_type
3627 static ir_node *transform_node_Cast(ir_node *n) {
3629 ir_node *pred = get_Cast_op(n);
3630 ir_type *tp = get_irn_type(n);
3632 if (is_Const(pred) && get_Const_type(pred) != tp) {
3633 n = new_rd_Const_type(NULL, current_ir_graph, get_irn_n(pred, -1), get_irn_mode(pred),
3634 get_Const_tarval(pred), tp);
3635 DBG_OPT_CSTEVAL(oldn, n);
3636 } else if (is_SymConst(pred) && get_SymConst_value_type(pred) != tp) {
3637 n = new_rd_SymConst_type(NULL, current_ir_graph, get_irn_n(pred, -1), get_irn_mode(pred),
3638 get_SymConst_symbol(pred), get_SymConst_kind(pred), tp);
3639 DBG_OPT_CSTEVAL(oldn, n);
3643 } /* transform_node_Cast */
3646 * Transform a Proj(Load) with a non-null address.
3648 static ir_node *transform_node_Proj_Load(ir_node *proj) {
3649 if (get_opt_ldst_only_null_ptr_exceptions()) {
3650 if (get_irn_mode(proj) == mode_X) {
3651 ir_node *load = get_Proj_pred(proj);
3653 /* get the Load address */
3654 const ir_node *addr = get_Load_ptr(load);
3655 const ir_node *confirm;
3657 if (value_not_null(addr, &confirm)) {
3658 if (confirm == NULL) {
3659 /* this node may float if it did not depend on a Confirm */
3660 set_irn_pinned(load, op_pin_state_floats);
3662 if (get_Proj_proj(proj) == pn_Load_X_except) {
3663 DBG_OPT_EXC_REM(proj);
3664 return get_irg_bad(current_ir_graph);
3666 ir_node *blk = get_nodes_block(load);
3667 return new_r_Jmp(current_ir_graph, blk);
3673 } /* transform_node_Proj_Load */
3676 * Transform a Proj(Store) with a non-null address.
3678 static ir_node *transform_node_Proj_Store(ir_node *proj) {
3679 if (get_opt_ldst_only_null_ptr_exceptions()) {
3680 if (get_irn_mode(proj) == mode_X) {
3681 ir_node *store = get_Proj_pred(proj);
3683 /* get the load/store address */
3684 const ir_node *addr = get_Store_ptr(store);
3685 const ir_node *confirm;
3687 if (value_not_null(addr, &confirm)) {
3688 if (confirm == NULL) {
3689 /* this node may float if it did not depend on a Confirm */
3690 set_irn_pinned(store, op_pin_state_floats);
3692 if (get_Proj_proj(proj) == pn_Store_X_except) {
3693 DBG_OPT_EXC_REM(proj);
3694 return get_irg_bad(current_ir_graph);
3696 ir_node *blk = get_nodes_block(store);
3697 return new_r_Jmp(current_ir_graph, blk);
3703 } /* transform_node_Proj_Store */
3706 * Transform a Proj(Div) with a non-zero value.
3707 * Removes the exceptions and routes the memory to the NoMem node.
3709 static ir_node *transform_node_Proj_Div(ir_node *proj) {
3710 ir_node *div = get_Proj_pred(proj);
3711 ir_node *b = get_Div_right(div);
3712 ir_node *res, *new_mem;
3713 const ir_node *confirm;
3716 if (value_not_zero(b, &confirm)) {
3717 /* div(x, y) && y != 0 */
3718 if (confirm == NULL) {
3719 /* we are sure we have a Const != 0 */
3720 new_mem = get_Div_mem(div);
3721 if (is_Pin(new_mem))
3722 new_mem = get_Pin_op(new_mem);
3723 set_Div_mem(div, new_mem);
3724 set_irn_pinned(div, op_pin_state_floats);
3727 proj_nr = get_Proj_proj(proj);
3729 case pn_Div_X_regular:
3730 return new_r_Jmp(current_ir_graph, get_irn_n(div, -1));
3732 case pn_Div_X_except:
3733 /* we found an exception handler, remove it */
3734 DBG_OPT_EXC_REM(proj);
3738 res = get_Div_mem(div);
3739 new_mem = get_irg_no_mem(current_ir_graph);
3742 /* This node can only float up to the Confirm block */
3743 new_mem = new_r_Pin(current_ir_graph, get_nodes_block(confirm), new_mem);
3745 set_irn_pinned(div, op_pin_state_floats);
3746 /* this is a Div without exception, we can remove the memory edge */
3747 set_Div_mem(div, new_mem);
3752 } /* transform_node_Proj_Div */
3755 * Transform a Proj(Mod) with a non-zero value.
3756 * Removes the exceptions and routes the memory to the NoMem node.
3758 static ir_node *transform_node_Proj_Mod(ir_node *proj) {
3759 ir_node *mod = get_Proj_pred(proj);
3760 ir_node *b = get_Mod_right(mod);
3761 ir_node *res, *new_mem;
3762 const ir_node *confirm;
3765 if (value_not_zero(b, &confirm)) {
3766 /* mod(x, y) && y != 0 */
3767 proj_nr = get_Proj_proj(proj);
3769 if (confirm == NULL) {
3770 /* we are sure we have a Const != 0 */
3771 new_mem = get_Mod_mem(mod);
3772 if (is_Pin(new_mem))
3773 new_mem = get_Pin_op(new_mem);
3774 set_Mod_mem(mod, new_mem);
3775 set_irn_pinned(mod, op_pin_state_floats);
3780 case pn_Mod_X_regular:
3781 return new_r_Jmp(current_ir_graph, get_irn_n(mod, -1));
3783 case pn_Mod_X_except:
3784 /* we found an exception handler, remove it */
3785 DBG_OPT_EXC_REM(proj);
3789 res = get_Mod_mem(mod);
3790 new_mem = get_irg_no_mem(current_ir_graph);
3793 /* This node can only float up to the Confirm block */
3794 new_mem = new_r_Pin(current_ir_graph, get_nodes_block(confirm), new_mem);
3796 /* this is a Mod without exception, we can remove the memory edge */
3797 set_Mod_mem(mod, new_mem);
3800 if (get_Mod_left(mod) == b) {
3801 /* a % a = 0 if a != 0 */
3802 ir_mode *mode = get_irn_mode(proj);
3803 ir_node *res = new_Const(mode, get_mode_null(mode));
3805 DBG_OPT_CSTEVAL(mod, res);
3811 } /* transform_node_Proj_Mod */
3814 * Transform a Proj(DivMod) with a non-zero value.
3815 * Removes the exceptions and routes the memory to the NoMem node.
3817 static ir_node *transform_node_Proj_DivMod(ir_node *proj) {
3818 ir_node *divmod = get_Proj_pred(proj);
3819 ir_node *b = get_DivMod_right(divmod);
3820 ir_node *res, *new_mem;
3821 const ir_node *confirm;
3824 if (value_not_zero(b, &confirm)) {
3825 /* DivMod(x, y) && y != 0 */
3826 proj_nr = get_Proj_proj(proj);
3828 if (confirm == NULL) {
3829 /* we are sure we have a Const != 0 */
3830 new_mem = get_DivMod_mem(divmod);
3831 if (is_Pin(new_mem))
3832 new_mem = get_Pin_op(new_mem);
3833 set_DivMod_mem(divmod, new_mem);
3834 set_irn_pinned(divmod, op_pin_state_floats);
3839 case pn_DivMod_X_regular:
3840 return new_r_Jmp(current_ir_graph, get_irn_n(divmod, -1));
3842 case pn_DivMod_X_except:
3843 /* we found an exception handler, remove it */
3844 DBG_OPT_EXC_REM(proj);
3848 res = get_DivMod_mem(divmod);
3849 new_mem = get_irg_no_mem(current_ir_graph);
3852 /* This node can only float up to the Confirm block */
3853 new_mem = new_r_Pin(current_ir_graph, get_nodes_block(confirm), new_mem);
3855 /* this is a DivMod without exception, we can remove the memory edge */
3856 set_DivMod_mem(divmod, new_mem);
3859 case pn_DivMod_res_mod:
3860 if (get_DivMod_left(divmod) == b) {
3861 /* a % a = 0 if a != 0 */
3862 ir_mode *mode = get_irn_mode(proj);
3863 ir_node *res = new_Const(mode, get_mode_null(mode));
3865 DBG_OPT_CSTEVAL(divmod, res);
3871 } /* transform_node_Proj_DivMod */
3874 * Optimizes jump tables (CondIs or CondIu) by removing all impossible cases.
3876 static ir_node *transform_node_Proj_Cond(ir_node *proj) {
3877 if (get_opt_unreachable_code()) {
3878 ir_node *n = get_Proj_pred(proj);
3879 ir_node *b = get_Cond_selector(n);
3881 if (mode_is_int(get_irn_mode(b))) {
3882 tarval *tb = value_of(b);
3884 if (tb != tarval_bad) {
3885 /* we have a constant switch */
3886 long num = get_Proj_proj(proj);
3888 if (num != get_Cond_defaultProj(n)) { /* we cannot optimize default Proj's yet */
3889 if (get_tarval_long(tb) == num) {
3890 /* Do NOT create a jump here, or we will have 2 control flow ops
3891 * in a block. This case is optimized away in optimize_cf(). */
3894 /* this case will NEVER be taken, kill it */
3895 return get_irg_bad(current_ir_graph);
3902 } /* transform_node_Proj_Cond */
3905 * Create a 0 constant of given mode.
3907 static ir_node *create_zero_const(ir_mode *mode) {
3908 tarval *tv = get_mode_null(mode);
3909 ir_node *cnst = new_Const(mode, tv);
3914 /* the order of the values is important! */
3915 typedef enum const_class {
3921 static const_class classify_const(const ir_node* n)
3923 if (is_Const(n)) return const_const;
3924 if (is_irn_constlike(n)) return const_like;
3929 * Determines whether r is more constlike or has a larger index (in that order)
3932 static int operands_are_normalized(const ir_node *l, const ir_node *r)
3934 const const_class l_order = classify_const(l);
3935 const const_class r_order = classify_const(r);
3937 l_order > r_order ||
3938 (l_order == r_order && get_irn_idx(l) <= get_irn_idx(r));
3942 * Normalizes and optimizes Cmp nodes.
3944 static ir_node *transform_node_Proj_Cmp(ir_node *proj) {
3945 ir_node *n = get_Proj_pred(proj);
3946 ir_node *left = get_Cmp_left(n);
3947 ir_node *right = get_Cmp_right(n);
3950 ir_mode *mode = NULL;
3951 long proj_nr = get_Proj_proj(proj);
3953 /* we can evaluate some cases directly */
3956 return new_Const(mode_b, get_tarval_b_false());
3958 return new_Const(mode_b, get_tarval_b_true());
3960 if (!mode_is_float(get_irn_mode(left)))
3961 return new_Const(mode_b, get_tarval_b_true());
3967 /* remove Casts of both sides */
3968 left = skip_Cast(left);
3969 right = skip_Cast(right);
3971 /* Remove unnecessary conversions */
3972 /* TODO handle constants */
3973 if (is_Conv(left) && is_Conv(right)) {
3974 ir_mode *mode = get_irn_mode(left);
3975 ir_node *op_left = get_Conv_op(left);
3976 ir_node *op_right = get_Conv_op(right);
3977 ir_mode *mode_left = get_irn_mode(op_left);
3978 ir_mode *mode_right = get_irn_mode(op_right);
3980 if (smaller_mode(mode_left, mode) && smaller_mode(mode_right, mode)
3981 && mode_left != mode_b && mode_right != mode_b) {
3982 ir_graph *irg = current_ir_graph;
3983 ir_node *block = get_nodes_block(n);
3985 if (mode_left == mode_right) {
3989 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV_CONV);
3990 } else if (smaller_mode(mode_left, mode_right)) {
3991 left = new_r_Conv(irg, block, op_left, mode_right);
3994 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV);
3995 } else if (smaller_mode(mode_right, mode_left)) {
3997 right = new_r_Conv(irg, block, op_right, mode_left);
3999 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV);
4004 /* remove operation on both sides if possible */
4005 if (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) {
4007 * The following operations are NOT safe for floating point operations, for instance
4008 * 1.0 + inf == 2.0 + inf, =/=> x == y
4010 if (mode_is_int(get_irn_mode(left))) {
4011 unsigned lop = get_irn_opcode(left);
4013 if (lop == get_irn_opcode(right)) {
4014 ir_node *ll, *lr, *rl, *rr;
4016 /* same operation on both sides, try to remove */
4020 /* ~a CMP ~b => a CMP b, -a CMP -b ==> a CMP b */
4021 left = get_unop_op(left);
4022 right = get_unop_op(right);
4024 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4027 ll = get_Add_left(left);
4028 lr = get_Add_right(left);
4029 rl = get_Add_left(right);
4030 rr = get_Add_right(right);
4033 /* X + a CMP X + b ==> a CMP b */
4037 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4038 } else if (ll == rr) {
4039 /* X + a CMP b + X ==> a CMP b */
4043 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4044 } else if (lr == rl) {
4045 /* a + X CMP X + b ==> a CMP b */
4049 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4050 } else if (lr == rr) {
4051 /* a + X CMP b + X ==> a CMP b */
4055 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4059 ll = get_Sub_left(left);
4060 lr = get_Sub_right(left);
4061 rl = get_Sub_left(right);
4062 rr = get_Sub_right(right);
4065 /* X - a CMP X - b ==> a CMP b */
4069 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4070 } else if (lr == rr) {
4071 /* a - X CMP b - X ==> a CMP b */
4075 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4079 if (get_Rotl_right(left) == get_Rotl_right(right)) {
4080 /* a ROTL X CMP b ROTL X ==> a CMP b */
4081 left = get_Rotl_left(left);
4082 right = get_Rotl_left(right);
4084 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4092 /* X+A == A, A+X == A, A-X == A -> X == 0 */
4093 if (is_Add(left) || is_Sub(left)) {
4094 ir_node *ll = get_binop_left(left);
4095 ir_node *lr = get_binop_right(left);
4097 if (lr == right && is_Add(left)) {
4104 right = create_zero_const(get_irn_mode(left));
4106 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4109 if (is_Add(right) || is_Sub(right)) {
4110 ir_node *rl = get_binop_left(right);
4111 ir_node *rr = get_binop_right(right);
4113 if (rr == left && is_Add(right)) {
4120 right = create_zero_const(get_irn_mode(left));
4122 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4125 } /* mode_is_int(...) */
4126 } /* proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg */
4128 /* replace mode_b compares with ands/ors */
4129 if (get_irn_mode(left) == mode_b) {
4130 ir_graph *irg = current_ir_graph;
4131 ir_node *block = get_nodes_block(n);
4135 case pn_Cmp_Le: bres = new_r_Or( irg, block, new_r_Not(irg, block, left, mode_b), right, mode_b); break;
4136 case pn_Cmp_Lt: bres = new_r_And(irg, block, new_r_Not(irg, block, left, mode_b), right, mode_b); break;
4137 case pn_Cmp_Ge: bres = new_r_Or( irg, block, left, new_r_Not(irg, block, right, mode_b), mode_b); break;
4138 case pn_Cmp_Gt: bres = new_r_And(irg, block, left, new_r_Not(irg, block, right, mode_b), mode_b); break;
4139 case pn_Cmp_Lg: bres = new_r_Eor(irg, block, left, right, mode_b); break;
4140 case pn_Cmp_Eq: bres = new_r_Not(irg, block, new_r_Eor(irg, block, left, right, mode_b), mode_b); break;
4141 default: bres = NULL;
4144 DBG_OPT_ALGSIM0(n, bres, FS_OPT_CMP_TO_BOOL);
4150 * First step: normalize the compare op
4151 * by placing the constant on the right side
4152 * or moving the lower address node to the left.
4154 if (!operands_are_normalized(left, right)) {
4160 proj_nr = get_inversed_pnc(proj_nr);
4165 * Second step: Try to reduce the magnitude
4166 * of a constant. This may help to generate better code
4167 * later and may help to normalize more compares.
4168 * Of course this is only possible for integer values.
4170 if (is_Const(right)) {
4171 mode = get_irn_mode(right);
4172 tv = get_Const_tarval(right);
4174 /* TODO extend to arbitrary constants */
4175 if (is_Conv(left) && tarval_is_null(tv)) {
4176 ir_node *op = get_Conv_op(left);
4177 ir_mode *op_mode = get_irn_mode(op);
4180 * UpConv(x) REL 0 ==> x REL 0
4182 if (get_mode_size_bits(mode) > get_mode_size_bits(op_mode) &&
4183 ((proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) ||
4184 mode_is_signed(mode) || !mode_is_signed(op_mode))) {
4185 tv = get_mode_null(op_mode);
4189 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV);
4193 if (tv != tarval_bad) {
4194 /* the following optimization is possible on modes without Overflow
4195 * on Unary Minus or on == and !=:
4196 * -a CMP c ==> a swap(CMP) -c
4198 * Beware: for two-complement Overflow may occur, so only == and != can
4199 * be optimized, see this:
4200 * -MININT < 0 =/=> MININT > 0 !!!
4202 if (is_Minus(left) &&
4203 (!mode_overflow_on_unary_Minus(mode) ||
4204 (mode_is_int(mode) && (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg)))) {
4205 tv = tarval_neg(tv);
4207 if (tv != tarval_bad) {
4208 left = get_Minus_op(left);
4209 proj_nr = get_inversed_pnc(proj_nr);
4211 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4213 } else if (is_Not(left) && (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg)) {
4214 /* Not(a) ==/!= c ==> a ==/!= Not(c) */
4215 tv = tarval_not(tv);
4217 if (tv != tarval_bad) {
4218 left = get_Not_op(left);
4220 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4224 /* for integer modes, we have more */
4225 if (mode_is_int(mode)) {
4226 /* Ne includes Unordered which is not possible on integers.
4227 * However, frontends often use this wrong, so fix it here */
4228 if (proj_nr & pn_Cmp_Uo) {
4229 proj_nr &= ~pn_Cmp_Uo;
4230 set_Proj_proj(proj, proj_nr);
4233 /* c > 0 : a < c ==> a <= (c-1) a >= c ==> a > (c-1) */
4234 if ((proj_nr == pn_Cmp_Lt || proj_nr == pn_Cmp_Ge) &&
4235 tarval_cmp(tv, get_mode_null(mode)) == pn_Cmp_Gt) {
4236 tv = tarval_sub(tv, get_mode_one(mode), NULL);
4238 if (tv != tarval_bad) {
4239 proj_nr ^= pn_Cmp_Eq;
4241 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CNST_MAGN);
4244 /* c < 0 : a > c ==> a >= (c+1) a <= c ==> a < (c+1) */
4245 else if ((proj_nr == pn_Cmp_Gt || proj_nr == pn_Cmp_Le) &&
4246 tarval_cmp(tv, get_mode_null(mode)) == pn_Cmp_Lt) {
4247 tv = tarval_add(tv, get_mode_one(mode));
4249 if (tv != tarval_bad) {
4250 proj_nr ^= pn_Cmp_Eq;
4252 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CNST_MAGN);
4256 /* the following reassociations work only for == and != */
4257 if (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) {
4259 #if 0 /* Might be not that good in general */
4260 /* a-b == 0 ==> a == b, a-b != 0 ==> a != b */
4261 if (tarval_is_null(tv) && is_Sub(left)) {
4262 right = get_Sub_right(left);
4263 left = get_Sub_left(left);
4265 tv = value_of(right);
4267 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4271 if (tv != tarval_bad) {
4272 /* a-c1 == c2 ==> a == c2+c1, a-c1 != c2 ==> a != c2+c1 */
4274 ir_node *c1 = get_Sub_right(left);
4275 tarval *tv2 = value_of(c1);
4277 if (tv2 != tarval_bad) {
4278 tv2 = tarval_add(tv, value_of(c1));
4280 if (tv2 != tarval_bad) {
4281 left = get_Sub_left(left);
4284 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4288 /* a+c1 == c2 ==> a == c2-c1, a+c1 != c2 ==> a != c2-c1 */
4289 else if (is_Add(left)) {
4290 ir_node *a_l = get_Add_left(left);
4291 ir_node *a_r = get_Add_right(left);
4295 if (is_Const(a_l)) {
4297 tv2 = value_of(a_l);
4300 tv2 = value_of(a_r);
4303 if (tv2 != tarval_bad) {
4304 tv2 = tarval_sub(tv, tv2, NULL);
4306 if (tv2 != tarval_bad) {
4310 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4314 /* -a == c ==> a == -c, -a != c ==> a != -c */
4315 else if (is_Minus(left)) {
4316 tarval *tv2 = tarval_sub(get_mode_null(mode), tv, NULL);
4318 if (tv2 != tarval_bad) {
4319 left = get_Minus_op(left);
4322 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4327 /* the following reassociations work only for <= */
4328 else if (proj_nr == pn_Cmp_Le || proj_nr == pn_Cmp_Lt) {
4329 if (tv != tarval_bad) {
4330 /* c >= 0 : Abs(a) <= c ==> (unsigned)(a + c) <= 2*c */
4331 if (get_irn_op(left) == op_Abs) { // TODO something is missing here
4337 if (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) {
4338 switch (get_irn_opcode(left)) {
4342 c1 = get_And_right(left);
4345 * And(x, C1) == C2 ==> FALSE if C2 & C1 != C2
4346 * And(x, C1) != C2 ==> TRUE if C2 & C1 != C2
4348 tarval *mask = tarval_and(get_Const_tarval(c1), tv);
4350 /* TODO: move to constant evaluation */
4351 tv = proj_nr == pn_Cmp_Eq ? get_tarval_b_false() : get_tarval_b_true();
4352 c1 = new_Const(mode_b, tv);
4353 DBG_OPT_CSTEVAL(proj, c1);
4357 if (tarval_is_single_bit(tv)) {
4359 * optimization for AND:
4361 * And(x, C) == C ==> And(x, C) != 0
4362 * And(x, C) != C ==> And(X, C) == 0
4364 * if C is a single Bit constant.
4367 /* check for Constant's match. We have check hare the tarvals,
4368 because our const might be changed */
4369 if (get_Const_tarval(c1) == tv) {
4370 /* fine: do the transformation */
4371 tv = get_mode_null(get_tarval_mode(tv));
4372 proj_nr ^= pn_Cmp_Leg;
4374 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CNST_MAGN);
4380 c1 = get_Or_right(left);
4381 if (is_Const(c1) && tarval_is_null(tv)) {
4383 * Or(x, C) == 0 && C != 0 ==> FALSE
4384 * Or(x, C) != 0 && C != 0 ==> TRUE
4386 if (! tarval_is_null(get_Const_tarval(c1))) {
4387 /* TODO: move to constant evaluation */
4388 tv = proj_nr == pn_Cmp_Eq ? get_tarval_b_false() : get_tarval_b_true();
4389 c1 = new_Const(mode_b, tv);
4390 DBG_OPT_CSTEVAL(proj, c1);
4397 * optimize x << c1 == c into x & (-1 >>u c1) == c >> c1 if c & (-1 << c1) == c
4399 * optimize x << c1 != c into x & (-1 >>u c1) != c >> c1 if c & (-1 << c1) == c
4402 c1 = get_Shl_right(left);
4404 tarval *tv1 = get_Const_tarval(c1);
4405 ir_mode *mode = get_irn_mode(left);
4406 tarval *minus1 = get_mode_all_one(mode);
4407 tarval *amask = tarval_shr(minus1, tv1);
4408 tarval *cmask = tarval_shl(minus1, tv1);
4411 if (tarval_and(tv, cmask) != tv) {
4412 /* condition not met */
4413 tv = proj_nr == pn_Cmp_Eq ? get_tarval_b_false() : get_tarval_b_true();
4414 c1 = new_Const(mode_b, tv);
4415 DBG_OPT_CSTEVAL(proj, c1);
4418 sl = get_Shl_left(left);
4419 blk = get_nodes_block(n);
4420 left = new_rd_And(get_irn_dbg_info(left), current_ir_graph, blk, sl, new_Const(mode, amask), mode);
4421 tv = tarval_shr(tv, tv1);
4423 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_SHF_TO_AND);
4428 * optimize x >>u c1 == c into x & (-1 << c1) == c << c1 if c & (-1 >>u c1) == c
4430 * optimize x >>u c1 != c into x & (-1 << c1) != c << c1 if c & (-1 >>u c1) == c
4433 c1 = get_Shr_right(left);
4435 tarval *tv1 = get_Const_tarval(c1);
4436 ir_mode *mode = get_irn_mode(left);
4437 tarval *minus1 = get_mode_all_one(mode);
4438 tarval *amask = tarval_shl(minus1, tv1);
4439 tarval *cmask = tarval_shr(minus1, tv1);
4442 if (tarval_and(tv, cmask) != tv) {
4443 /* condition not met */
4444 tv = proj_nr == pn_Cmp_Eq ? get_tarval_b_false() : get_tarval_b_true();
4445 c1 = new_Const(mode_b, tv);
4446 DBG_OPT_CSTEVAL(proj, c1);
4449 sl = get_Shr_left(left);
4450 blk = get_nodes_block(n);
4451 left = new_rd_And(get_irn_dbg_info(left), current_ir_graph, blk, sl, new_Const(mode, amask), mode);
4452 tv = tarval_shl(tv, tv1);
4454 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_SHF_TO_AND);
4459 * optimize x >>s c1 == c into x & (-1 << c1) == c << c1 if (c >>s (BITS - c1)) \in {0,-1}
4461 * optimize x >>s c1 != c into x & (-1 << c1) != c << c1 if (c >>s (BITS - c1)) \in {0,-1}
4464 c1 = get_Shrs_right(left);
4466 tarval *tv1 = get_Const_tarval(c1);
4467 ir_mode *mode = get_irn_mode(left);
4468 tarval *minus1 = get_mode_all_one(mode);
4469 tarval *amask = tarval_shl(minus1, tv1);
4470 tarval *cond = new_tarval_from_long(get_mode_size_bits(mode), get_tarval_mode(tv1));
4473 cond = tarval_sub(cond, tv1, NULL);
4474 cond = tarval_shrs(tv, cond);
4476 if (!tarval_is_all_one(cond) && !tarval_is_null(cond)) {
4477 /* condition not met */
4478 tv = proj_nr == pn_Cmp_Eq ? get_tarval_b_false() : get_tarval_b_true();
4479 c1 = new_Const(mode_b, tv);
4480 DBG_OPT_CSTEVAL(proj, c1);
4483 sl = get_Shrs_left(left);
4484 blk = get_nodes_block(n);
4485 left = new_rd_And(get_irn_dbg_info(left), current_ir_graph, blk, sl, new_Const(mode, amask), mode);
4486 tv = tarval_shl(tv, tv1);
4488 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_SHF_TO_AND);
4493 } /* tarval != bad */
4496 if (changed & 2) /* need a new Const */
4497 right = new_Const(mode, tv);
4499 if ((proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) && is_Const(right) && is_Const_null(right) && is_Proj(left)) {
4500 ir_node *op = get_Proj_pred(left);
4502 if ((is_Mod(op) && get_Proj_proj(left) == pn_Mod_res) ||
4503 (is_DivMod(op) && get_Proj_proj(left) == pn_DivMod_res_mod)) {
4504 ir_node *c = get_binop_right(op);
4507 tarval *tv = get_Const_tarval(c);
4509 if (tarval_is_single_bit(tv)) {
4510 /* special case: (x % 2^n) CMP 0 ==> x & (2^n-1) CMP 0 */
4511 ir_node *v = get_binop_left(op);
4512 ir_node *blk = get_irn_n(op, -1);
4513 ir_mode *mode = get_irn_mode(v);
4515 tv = tarval_sub(tv, get_mode_one(mode), NULL);
4516 left = new_rd_And(get_irn_dbg_info(op), current_ir_graph, blk, v, new_Const(mode, tv), mode);
4518 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_MOD_TO_AND);
4525 ir_node *block = get_irn_n(n, -1); /* Beware of get_nodes_Block() */
4527 /* create a new compare */
4528 n = new_rd_Cmp(get_irn_dbg_info(n), current_ir_graph, block, left, right);
4529 proj = new_rd_Proj(get_irn_dbg_info(proj), current_ir_graph, block, n, get_irn_mode(proj), proj_nr);
4533 } /* transform_node_Proj_Cmp */
4536 * Optimize CopyB(mem, x, x) into a Nop.
4538 static ir_node *transform_node_Proj_CopyB(ir_node *proj) {
4539 ir_node *copyb = get_Proj_pred(proj);
4540 ir_node *a = get_CopyB_dst(copyb);
4541 ir_node *b = get_CopyB_src(copyb);
4544 switch (get_Proj_proj(proj)) {
4545 case pn_CopyB_X_regular:
4546 /* Turn CopyB into a tuple (mem, jmp, bad, bad) */
4547 DBG_OPT_EXC_REM(proj);
4548 proj = new_r_Jmp(current_ir_graph, get_nodes_block(copyb));
4550 case pn_CopyB_M_except:
4551 case pn_CopyB_X_except:
4552 DBG_OPT_EXC_REM(proj);
4553 proj = get_irg_bad(current_ir_graph);
4560 } /* transform_node_Proj_CopyB */
4563 * Optimize Bounds(idx, idx, upper) into idx.
4565 static ir_node *transform_node_Proj_Bound(ir_node *proj) {
4566 ir_node *oldn = proj;
4567 ir_node *bound = get_Proj_pred(proj);
4568 ir_node *idx = get_Bound_index(bound);
4569 ir_node *pred = skip_Proj(idx);
4572 if (idx == get_Bound_lower(bound))
4574 else if (is_Bound(pred)) {
4576 * idx was Bounds checked in the same MacroBlock previously,
4577 * it is still valid if lower <= pred_lower && pred_upper <= upper.
4579 ir_node *lower = get_Bound_lower(bound);
4580 ir_node *upper = get_Bound_upper(bound);
4581 if (get_Bound_lower(pred) == lower &&
4582 get_Bound_upper(pred) == upper &&
4583 get_irn_MacroBlock(bound) == get_irn_MacroBlock(pred)) {
4585 * One could expect that we simply return the previous
4586 * Bound here. However, this would be wrong, as we could
4587 * add an exception Proj to a new location then.
4588 * So, we must turn in into a tuple.
4594 /* Turn Bound into a tuple (mem, jmp, bad, idx) */
4595 switch (get_Proj_proj(proj)) {
4597 DBG_OPT_EXC_REM(proj);
4598 proj = get_Bound_mem(bound);
4600 case pn_Bound_X_except:
4601 DBG_OPT_EXC_REM(proj);
4602 proj = get_irg_bad(current_ir_graph);
4606 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NOP);
4608 case pn_Bound_X_regular:
4609 DBG_OPT_EXC_REM(proj);
4610 proj = new_r_Jmp(current_ir_graph, get_nodes_block(bound));
4617 } /* transform_node_Proj_Bound */
4620 * Does all optimizations on nodes that must be done on it's Proj's
4621 * because of creating new nodes.
4623 static ir_node *transform_node_Proj(ir_node *proj) {
4624 ir_node *n = get_Proj_pred(proj);
4626 if (n->op->ops.transform_node_Proj)
4627 return n->op->ops.transform_node_Proj(proj);
4629 } /* transform_node_Proj */
4632 * Move Confirms down through Phi nodes.
4634 static ir_node *transform_node_Phi(ir_node *phi) {
4636 ir_mode *mode = get_irn_mode(phi);
4638 if (mode_is_reference(mode)) {
4639 n = get_irn_arity(phi);
4641 /* Beware of Phi0 */
4643 ir_node *pred = get_irn_n(phi, 0);
4644 ir_node *bound, *new_Phi, *block, **in;
4647 if (! is_Confirm(pred))
4650 bound = get_Confirm_bound(pred);
4651 pnc = get_Confirm_cmp(pred);
4653 NEW_ARR_A(ir_node *, in, n);
4654 in[0] = get_Confirm_value(pred);
4656 for (i = 1; i < n; ++i) {
4657 pred = get_irn_n(phi, i);
4659 if (! is_Confirm(pred) ||
4660 get_Confirm_bound(pred) != bound ||
4661 get_Confirm_cmp(pred) != pnc)
4663 in[i] = get_Confirm_value(pred);
4665 /* move the Confirm nodes "behind" the Phi */
4666 block = get_irn_n(phi, -1);
4667 new_Phi = new_r_Phi(current_ir_graph, block, n, in, get_irn_mode(phi));
4668 return new_r_Confirm(current_ir_graph, block, new_Phi, bound, pnc);
4672 } /* transform_node_Phi */
4675 * Returns the operands of a commutative bin-op, if one operand is
4676 * a const, it is returned as the second one.
4678 static void get_comm_Binop_Ops(ir_node *binop, ir_node **a, ir_node **c) {
4679 ir_node *op_a = get_binop_left(binop);
4680 ir_node *op_b = get_binop_right(binop);
4682 assert(is_op_commutative(get_irn_op(binop)));
4684 if (is_Const(op_a)) {
4691 } /* get_comm_Binop_Ops */
4694 * Optimize a Or(And(Or(And(v,c4),c3),c2),c1) pattern if possible.
4695 * Such pattern may arise in bitfield stores.
4697 * value c4 value c4 & c2
4698 * AND c3 AND c1 | c3
4705 * AND c1 ===> OR if (c1 | c2) == 0x111..11
4708 static ir_node *transform_node_Or_bf_store(ir_node *or) {
4711 ir_node *and_l, *c3;
4712 ir_node *value, *c4;
4713 ir_node *new_and, *new_const, *block;
4714 ir_mode *mode = get_irn_mode(or);
4716 tarval *tv1, *tv2, *tv3, *tv4, *tv, *n_tv4, *n_tv2;
4719 get_comm_Binop_Ops(or, &and, &c1);
4720 if (!is_Const(c1) || !is_And(and))
4723 get_comm_Binop_Ops(and, &or_l, &c2);
4727 tv1 = get_Const_tarval(c1);
4728 tv2 = get_Const_tarval(c2);
4730 tv = tarval_or(tv1, tv2);
4731 if (tarval_is_all_one(tv)) {
4732 /* the AND does NOT clear a bit with isn't set by the OR */
4733 set_Or_left(or, or_l);
4734 set_Or_right(or, c1);
4736 /* check for more */
4743 get_comm_Binop_Ops(or_l, &and_l, &c3);
4744 if (!is_Const(c3) || !is_And(and_l))
4747 get_comm_Binop_Ops(and_l, &value, &c4);
4751 /* ok, found the pattern, check for conditions */
4752 assert(mode == get_irn_mode(and));
4753 assert(mode == get_irn_mode(or_l));
4754 assert(mode == get_irn_mode(and_l));
4756 tv3 = get_Const_tarval(c3);
4757 tv4 = get_Const_tarval(c4);
4759 tv = tarval_or(tv4, tv2);
4760 if (!tarval_is_all_one(tv)) {
4761 /* have at least one 0 at the same bit position */
4765 n_tv4 = tarval_not(tv4);
4766 if (tv3 != tarval_and(tv3, n_tv4)) {
4767 /* bit in the or_mask is outside the and_mask */
4771 n_tv2 = tarval_not(tv2);
4772 if (tv1 != tarval_and(tv1, n_tv2)) {
4773 /* bit in the or_mask is outside the and_mask */
4777 /* ok, all conditions met */
4778 block = get_irn_n(or, -1);
4780 new_and = new_r_And(current_ir_graph, block,
4781 value, new_r_Const(current_ir_graph, block, mode, tarval_and(tv4, tv2)), mode);
4783 new_const = new_r_Const(current_ir_graph, block, mode, tarval_or(tv3, tv1));
4785 set_Or_left(or, new_and);
4786 set_Or_right(or, new_const);
4788 /* check for more */
4790 } /* transform_node_Or_bf_store */
4793 * Optimize an Or(shl(x, c), shr(x, bits - c)) into a Rotl
4795 static ir_node *transform_node_Or_Rotl(ir_node *or) {
4796 ir_mode *mode = get_irn_mode(or);
4797 ir_node *shl, *shr, *block;
4798 ir_node *irn, *x, *c1, *c2, *v, *sub, *n, *rotval;
4801 if (! mode_is_int(mode))
4804 shl = get_binop_left(or);
4805 shr = get_binop_right(or);
4814 } else if (!is_Shl(shl)) {
4816 } else if (!is_Shr(shr)) {
4819 x = get_Shl_left(shl);
4820 if (x != get_Shr_left(shr))
4823 c1 = get_Shl_right(shl);
4824 c2 = get_Shr_right(shr);
4825 if (is_Const(c1) && is_Const(c2)) {
4826 tv1 = get_Const_tarval(c1);
4827 if (! tarval_is_long(tv1))
4830 tv2 = get_Const_tarval(c2);
4831 if (! tarval_is_long(tv2))
4834 if (get_tarval_long(tv1) + get_tarval_long(tv2)
4835 != (int) get_mode_size_bits(mode))
4838 /* yet, condition met */
4839 block = get_nodes_block(or);
4841 n = new_r_Rotl(current_ir_graph, block, x, c1, mode);
4843 DBG_OPT_ALGSIM1(or, shl, shr, n, FS_OPT_OR_SHFT_TO_ROTL);
4850 rotval = sub; /* a Rot right is not supported, so use a rot left */
4851 } else if (is_Sub(c2)) {
4857 if (get_Sub_right(sub) != v)
4860 c1 = get_Sub_left(sub);
4864 tv1 = get_Const_tarval(c1);
4865 if (! tarval_is_long(tv1))
4868 if (get_tarval_long(tv1) != (int) get_mode_size_bits(mode))
4871 /* yet, condition met */
4872 block = get_nodes_block(or);
4874 n = new_r_Rotl(current_ir_graph, block, x, rotval, mode);
4876 DBG_OPT_ALGSIM0(or, n, FS_OPT_OR_SHFT_TO_ROTL);
4878 } /* transform_node_Or_Rotl */
4883 static ir_node *transform_node_Or(ir_node *n) {
4884 ir_node *c, *oldn = n;
4885 ir_node *a = get_Or_left(n);
4886 ir_node *b = get_Or_right(n);
4889 if (is_Not(a) && is_Not(b)) {
4890 /* ~a | ~b = ~(a&b) */
4891 ir_node *block = get_nodes_block(n);
4893 mode = get_irn_mode(n);
4896 n = new_rd_And(get_irn_dbg_info(n), current_ir_graph, block, a, b, mode);
4897 n = new_rd_Not(get_irn_dbg_info(n), current_ir_graph, block, n, mode);
4898 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_DEMORGAN);
4902 /* we can evaluate 2 Projs of the same Cmp */
4903 if (get_irn_mode(n) == mode_b && is_Proj(a) && is_Proj(b)) {
4904 ir_node *pred_a = get_Proj_pred(a);
4905 ir_node *pred_b = get_Proj_pred(b);
4906 if (pred_a == pred_b) {
4907 dbg_info *dbgi = get_irn_dbg_info(n);
4908 ir_node *block = get_nodes_block(pred_a);
4909 pn_Cmp pn_a = get_Proj_proj(a);
4910 pn_Cmp pn_b = get_Proj_proj(b);
4911 /* yes, we can simply calculate with pncs */
4912 pn_Cmp new_pnc = pn_a | pn_b;
4914 return new_rd_Proj(dbgi, current_ir_graph, block, pred_a, mode_b,
4919 mode = get_irn_mode(n);
4920 HANDLE_BINOP_PHI(tarval_or, a, b, c, mode);
4922 n = transform_node_Or_bf_store(n);
4923 n = transform_node_Or_Rotl(n);
4927 n = transform_bitwise_distributive(n, transform_node_Or);
4930 } /* transform_node_Or */
4934 static ir_node *transform_node(ir_node *n);
4937 * Optimize (a >> c1) >> c2), works for Shr, Shrs, Shl, Rotl.
4939 * Should be moved to reassociation?
4941 static ir_node *transform_node_shift(ir_node *n) {
4942 ir_node *left, *right;
4944 tarval *tv1, *tv2, *res;
4945 ir_node *in[2], *irn, *block;
4947 left = get_binop_left(n);
4949 /* different operations */
4950 if (get_irn_op(left) != get_irn_op(n))
4953 right = get_binop_right(n);
4954 tv1 = value_of(right);
4955 if (tv1 == tarval_bad)
4958 tv2 = value_of(get_binop_right(left));
4959 if (tv2 == tarval_bad)
4962 res = tarval_add(tv1, tv2);
4963 mode = get_irn_mode(n);
4965 /* beware: a simple replacement works only, if res < modulo shift */
4967 int modulo_shf = get_mode_modulo_shift(mode);
4968 assert(modulo_shf >= (int) get_mode_size_bits(mode));
4969 if (modulo_shf > 0) {
4970 tarval *modulo = new_tarval_from_long(modulo_shf,
4971 get_tarval_mode(res));
4973 /* shifting too much */
4974 if (!(tarval_cmp(res, modulo) & pn_Cmp_Lt)) {
4976 ir_graph *irg = get_irn_irg(n);
4977 ir_node *block = get_nodes_block(n);
4978 dbg_info *dbgi = get_irn_dbg_info(n);
4979 ir_node *cnst = new_Const(mode_Iu, new_tarval_from_long(get_mode_size_bits(mode)-1, mode_Iu));
4980 return new_rd_Shrs(dbgi, irg, block, get_binop_left(left),
4984 return new_Const(mode, get_mode_null(mode));
4988 res = tarval_mod(res, new_tarval_from_long(get_mode_size_bits(mode), get_tarval_mode(res)));
4991 /* ok, we can replace it */
4992 block = get_nodes_block(n);
4994 in[0] = get_binop_left(left);
4995 in[1] = new_r_Const(current_ir_graph, block, get_tarval_mode(res), res);
4997 irn = new_ir_node(NULL, current_ir_graph, block, get_irn_op(n), mode, 2, in);
4999 DBG_OPT_ALGSIM0(n, irn, FS_OPT_REASSOC_SHIFT);
5001 return transform_node(irn);
5002 } /* transform_node_shift */
5005 * normalisation: (x & c1) >> c2 to (x >> c2) & (c1 >> c2)
5007 * - and, or, xor instead of &
5008 * - Shl, Shr, Shrs, rotl instead of >>
5009 * (with a special case for Or/Xor + Shrs)
5011 static ir_node *transform_node_bitop_shift(ir_node *n) {
5013 ir_node *right = get_binop_right(n);
5014 ir_mode *mode = get_irn_mode(n);
5015 ir_node *bitop_left;
5016 ir_node *bitop_right;
5028 assert(is_Shrs(n) || is_Shr(n) || is_Shl(n) || is_Rotl(n));
5030 if (!is_Const(right))
5033 left = get_binop_left(n);
5034 op_left = get_irn_op(left);
5035 if (op_left != op_And && op_left != op_Or && op_left != op_Eor)
5038 /* doing it with Shrs is not legal if the Or/Eor affects the topmost bit */
5039 if (is_Shrs(n) && (op_left == op_Or || op_left == op_Eor)) {
5040 /* TODO: test if sign bit is affectes */
5044 bitop_right = get_binop_right(left);
5045 if (!is_Const(bitop_right))
5048 bitop_left = get_binop_left(left);
5050 irg = get_irn_irg(n);
5051 block = get_nodes_block(n);
5052 dbgi = get_irn_dbg_info(n);
5053 tv1 = get_Const_tarval(bitop_right);
5054 tv2 = get_Const_tarval(right);
5056 assert(get_tarval_mode(tv1) == mode);
5059 new_shift = new_rd_Shl(dbgi, irg, block, bitop_left, right, mode);
5060 tv_shift = tarval_shl(tv1, tv2);
5061 } else if(is_Shr(n)) {
5062 new_shift = new_rd_Shr(dbgi, irg, block, bitop_left, right, mode);
5063 tv_shift = tarval_shr(tv1, tv2);
5064 } else if(is_Shrs(n)) {
5065 new_shift = new_rd_Shrs(dbgi, irg, block, bitop_left, right, mode);
5066 tv_shift = tarval_shrs(tv1, tv2);
5069 new_shift = new_rd_Rotl(dbgi, irg, block, bitop_left, right, mode);
5070 tv_shift = tarval_rotl(tv1, tv2);
5073 assert(get_tarval_mode(tv_shift) == mode);
5074 new_const = new_Const(mode, tv_shift);
5076 if (op_left == op_And) {
5077 new_bitop = new_rd_And(dbgi, irg, block, new_shift, new_const, mode);
5078 } else if(op_left == op_Or) {
5079 new_bitop = new_rd_Or(dbgi, irg, block, new_shift, new_const, mode);
5081 assert(op_left == op_Eor);
5082 new_bitop = new_rd_Eor(dbgi, irg, block, new_shift, new_const, mode);
5090 * (x << c1) >> c2 <=> x OP (c2-c1) & ((-1 << c1) >> c2)
5092 * (x >> c1) << c2 <=> x OP (c2-c1) & ((-1 >> c1) << c2)
5093 * (also with x >>s c1 when c1>=c2)
5095 static ir_node *transform_node_shl_shr(ir_node *n) {
5097 ir_node *right = get_binop_right(n);
5113 assert(is_Shl(n) || is_Shr(n) || is_Shrs(n));
5115 if (!is_Const(right))
5118 left = get_binop_left(n);
5119 mode = get_irn_mode(n);
5120 if (is_Shl(n) && (is_Shr(left) || is_Shrs(left))) {
5121 ir_node *shr_right = get_binop_right(left);
5123 if (!is_Const(shr_right))
5126 x = get_binop_left(left);
5127 tv_shr = get_Const_tarval(shr_right);
5128 tv_shl = get_Const_tarval(right);
5130 if (is_Shrs(left)) {
5131 /* shrs variant only allowed if c1 >= c2 */
5132 if (! (tarval_cmp(tv_shl, tv_shr) & pn_Cmp_Ge))
5135 tv_mask = tarval_shrs(get_mode_all_one(mode), tv_shr);
5138 tv_mask = tarval_shr(get_mode_all_one(mode), tv_shr);
5140 tv_mask = tarval_shl(tv_mask, tv_shl);
5141 } else if(is_Shr(n) && is_Shl(left)) {
5142 ir_node *shl_right = get_Shl_right(left);
5144 if (!is_Const(shl_right))
5147 x = get_Shl_left(left);
5148 tv_shr = get_Const_tarval(right);
5149 tv_shl = get_Const_tarval(shl_right);
5151 tv_mask = tarval_shl(get_mode_all_one(mode), tv_shl);
5152 tv_mask = tarval_shr(tv_mask, tv_shr);
5157 assert(get_tarval_mode(tv_shl) == get_tarval_mode(tv_shr));
5158 assert(tv_mask != tarval_bad);
5159 assert(get_tarval_mode(tv_mask) == mode);
5161 irg = get_irn_irg(n);
5162 block = get_nodes_block(n);
5163 dbgi = get_irn_dbg_info(n);
5165 pnc = tarval_cmp(tv_shl, tv_shr);
5166 if (pnc == pn_Cmp_Lt || pnc == pn_Cmp_Eq) {
5167 tv_shift = tarval_sub(tv_shr, tv_shl, NULL);
5168 new_const = new_Const(get_tarval_mode(tv_shift), tv_shift);
5170 new_shift = new_rd_Shrs(dbgi, irg, block, x, new_const, mode);
5172 new_shift = new_rd_Shr(dbgi, irg, block, x, new_const, mode);
5175 assert(pnc == pn_Cmp_Gt);
5176 tv_shift = tarval_sub(tv_shl, tv_shr, NULL);
5177 new_const = new_Const(get_tarval_mode(tv_shift), tv_shift);
5178 new_shift = new_rd_Shl(dbgi, irg, block, x, new_const, mode);
5181 new_const = new_Const(mode, tv_mask);
5182 new_and = new_rd_And(dbgi, irg, block, new_shift, new_const, mode);
5190 static ir_node *transform_node_Shr(ir_node *n) {
5191 ir_node *c, *oldn = n;
5192 ir_node *left = get_Shr_left(n);
5193 ir_node *right = get_Shr_right(n);
5194 ir_mode *mode = get_irn_mode(n);
5196 HANDLE_BINOP_PHI(tarval_shr, left, right, c, mode);
5197 n = transform_node_shift(n);
5200 n = transform_node_shl_shr(n);
5202 n = transform_node_bitop_shift(n);
5205 } /* transform_node_Shr */
5210 static ir_node *transform_node_Shrs(ir_node *n) {
5211 ir_node *c, *oldn = n;
5212 ir_node *a = get_Shrs_left(n);
5213 ir_node *b = get_Shrs_right(n);
5214 ir_mode *mode = get_irn_mode(n);
5216 HANDLE_BINOP_PHI(tarval_shrs, a, b, c, mode);
5217 n = transform_node_shift(n);
5220 n = transform_node_bitop_shift(n);
5223 } /* transform_node_Shrs */
5228 static ir_node *transform_node_Shl(ir_node *n) {
5229 ir_node *c, *oldn = n;
5230 ir_node *a = get_Shl_left(n);
5231 ir_node *b = get_Shl_right(n);
5232 ir_mode *mode = get_irn_mode(n);
5234 HANDLE_BINOP_PHI(tarval_shl, a, b, c, mode);
5235 n = transform_node_shift(n);
5238 n = transform_node_shl_shr(n);
5240 n = transform_node_bitop_shift(n);
5243 } /* transform_node_Shl */
5248 static ir_node *transform_node_Rotl(ir_node *n) {
5249 ir_node *c, *oldn = n;
5250 ir_node *a = get_Rotl_left(n);
5251 ir_node *b = get_Rotl_right(n);
5252 ir_mode *mode = get_irn_mode(n);
5254 HANDLE_BINOP_PHI(tarval_rotl, a, b, c, mode);
5255 n = transform_node_shift(n);
5258 n = transform_node_bitop_shift(n);
5261 } /* transform_node_Rotl */
5266 static ir_node *transform_node_Conv(ir_node *n) {
5267 ir_node *c, *oldn = n;
5268 ir_node *a = get_Conv_op(n);
5270 if (is_const_Phi(a)) {
5271 c = apply_conv_on_phi(a, get_irn_mode(n));
5273 DBG_OPT_ALGSIM0(oldn, c, FS_OPT_CONST_PHI);
5278 if (is_Unknown(a)) { /* Conv_A(Unknown_B) -> Unknown_A */
5279 ir_mode *mode = get_irn_mode(n);
5280 return new_r_Unknown(current_ir_graph, mode);
5284 } /* transform_node_Conv */
5287 * Remove dead blocks and nodes in dead blocks
5288 * in keep alive list. We do not generate a new End node.
5290 static ir_node *transform_node_End(ir_node *n) {
5291 int i, j, n_keepalives = get_End_n_keepalives(n);
5294 NEW_ARR_A(ir_node *, in, n_keepalives);
5296 for (i = j = 0; i < n_keepalives; ++i) {
5297 ir_node *ka = get_End_keepalive(n, i);
5299 if (! is_Block_dead(ka)) {
5303 } else if (is_irn_pinned_in_irg(ka) && is_Block_dead(get_nodes_block(ka))) {
5306 /* FIXME: beabi need to keep a Proj(M) */
5307 if (is_Phi(ka) || is_irn_keep(ka) || is_Proj(ka))
5310 if (j != n_keepalives)
5311 set_End_keepalives(n, j, in);
5313 } /* transform_node_End */
5315 /** returns 1 if a == -b */
5316 static int is_negated_value(ir_node *a, ir_node *b) {
5317 if (is_Minus(a) && get_Minus_op(a) == b)
5319 if (is_Minus(b) && get_Minus_op(b) == a)
5321 if (is_Sub(a) && is_Sub(b)) {
5322 ir_node *a_left = get_Sub_left(a);
5323 ir_node *a_right = get_Sub_right(a);
5324 ir_node *b_left = get_Sub_left(b);
5325 ir_node *b_right = get_Sub_right(b);
5327 if (a_left == b_right && a_right == b_left)
5335 * Optimize a Mux into some simpler cases.
5337 static ir_node *transform_node_Mux(ir_node *n) {
5338 ir_node *oldn = n, *sel = get_Mux_sel(n);
5339 ir_mode *mode = get_irn_mode(n);
5340 ir_node *t = get_Mux_true(n);
5341 ir_node *f = get_Mux_false(n);
5342 ir_graph *irg = current_ir_graph;
5344 /* first normalization step: move a possible zero to the false case */
5346 ir_node *cmp = get_Proj_pred(sel);
5349 if (is_Const(t) && is_Const_null(t)) {
5352 /* Mux(x, 0, y) => Mux(x, y, 0) */
5353 pn_Cmp pnc = get_Proj_proj(sel);
5354 sel = new_r_Proj(irg, get_nodes_block(cmp), cmp, mode_b,
5355 get_negated_pnc(pnc, get_irn_mode(get_Cmp_left(cmp))));
5356 n = new_rd_Mux(get_irn_dbg_info(n), irg, get_nodes_block(n), sel, t, f, mode);
5364 /* note: after normalization, false can only happen on default */
5365 if (mode == mode_b) {
5366 dbg_info *dbg = get_irn_dbg_info(n);
5367 ir_node *block = get_nodes_block(n);
5368 ir_graph *irg = current_ir_graph;
5371 tarval *tv_t = get_Const_tarval(t);
5372 if (tv_t == tarval_b_true) {
5374 /* Muxb(sel, true, false) = sel */
5375 assert(get_Const_tarval(f) == tarval_b_false);
5376 DBG_OPT_ALGSIM0(oldn, sel, FS_OPT_MUX_BOOL);
5379 /* Muxb(sel, true, x) = Or(sel, x) */
5380 n = new_rd_Or(dbg, irg, block, sel, f, mode_b);
5381 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_OR_BOOL);
5385 } else if (is_Const(f)) {
5386 tarval *tv_f = get_Const_tarval(f);
5387 if (tv_f == tarval_b_true) {
5388 /* Muxb(sel, x, true) = Or(Not(sel), x) */
5389 ir_node* not_sel = new_rd_Not(dbg, irg, block, sel, mode_b);
5390 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_ORNOT_BOOL);
5391 n = new_rd_Or(dbg, irg, block, not_sel, t, mode_b);
5394 /* Muxb(sel, x, false) = And(sel, x) */
5395 assert(tv_f == tarval_b_false);
5396 n = new_rd_And(dbg, irg, block, sel, t, mode_b);
5397 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_AND_BOOL);
5403 /* more normalization: try to normalize Mux(x, C1, C2) into Mux(x, +1/-1, 0) op C2 */
5404 if (is_Const(t) && is_Const(f) && mode_is_int(mode)) {
5405 tarval *a = get_Const_tarval(t);
5406 tarval *b = get_Const_tarval(f);
5407 tarval *null = get_tarval_null(mode);
5410 if (tarval_cmp(a, b) & pn_Cmp_Gt) {
5411 diff = tarval_sub(a, b, NULL);
5414 diff = tarval_sub(b, a, NULL);
5418 if (diff == get_tarval_one(mode) && min != null) {
5419 dbg_info *dbg = get_irn_dbg_info(n);
5420 ir_node *block = get_nodes_block(n);
5421 ir_graph *irg = current_ir_graph;
5422 ir_node *t = new_Const(mode, tarval_sub(a, min, NULL));
5423 ir_node *f = new_Const(mode, tarval_sub(b, min, NULL));
5424 n = new_rd_Mux(dbg, irg, block, sel, f, t, mode);
5425 n = new_rd_Add(dbg, irg, block, n, new_Const(mode, min), mode);
5431 ir_node *cmp = get_Proj_pred(sel);
5432 long pn = get_Proj_proj(sel);
5435 * Note: normalization puts the constant on the right side,
5436 * so we check only one case.
5438 * Note further that these optimization work even for floating point
5439 * with NaN's because -NaN == NaN.
5440 * However, if +0 and -0 is handled differently, we cannot use the Abs/-Abs
5444 ir_node *cmp_r = get_Cmp_right(cmp);
5445 if (is_Const(cmp_r) && is_Const_null(cmp_r)) {
5446 ir_node *block = get_nodes_block(n);
5447 ir_node *cmp_l = get_Cmp_left(cmp);
5449 if (!mode_honor_signed_zeros(mode) && is_negated_value(f, t)) {
5452 if ( (cmp_l == t && (pn == pn_Cmp_Ge || pn == pn_Cmp_Gt))
5453 || (cmp_l == f && (pn == pn_Cmp_Le || pn == pn_Cmp_Lt)))
5455 /* Mux(a >/>= 0, a, -a) = Mux(a </<= 0, -a, a) ==> Abs(a) */
5456 n = new_rd_Abs(get_irn_dbg_info(n), current_ir_graph, block,
5458 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_ABS);
5460 } else if ((cmp_l == t && (pn == pn_Cmp_Le || pn == pn_Cmp_Lt))
5461 || (cmp_l == f && (pn == pn_Cmp_Ge || pn == pn_Cmp_Gt)))
5463 /* Mux(a </<= 0, a, -a) = Mux(a >/>= 0, -a, a) ==> -Abs(a) */
5464 n = new_rd_Abs(get_irn_dbg_info(n), current_ir_graph, block,
5466 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph,
5468 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_ABS);
5473 if (mode_is_int(mode)) {
5475 if ((pn == pn_Cmp_Lg || pn == pn_Cmp_Eq) && is_And(cmp_l)) {
5476 /* Mux((a & b) != 0, c, 0) */
5477 ir_node *and_r = get_And_right(cmp_l);
5480 if (and_r == t && f == cmp_r) {
5481 if (is_Const(t) && tarval_is_single_bit(get_Const_tarval(t))) {
5482 if (pn == pn_Cmp_Lg) {
5483 /* Mux((a & 2^C) != 0, 2^C, 0) */
5486 /* Mux((a & 2^C) == 0, 2^C, 0) */
5487 n = new_rd_Eor(get_irn_dbg_info(n), current_ir_graph,
5488 block, cmp_l, t, mode);
5493 if (is_Shl(and_r)) {
5494 ir_node *shl_l = get_Shl_left(and_r);
5495 if (is_Const(shl_l) && is_Const_one(shl_l)) {
5496 if (and_r == t && f == cmp_r) {
5497 if (pn == pn_Cmp_Lg) {
5498 /* (a & (1 << n)) != 0, (1 << n), 0) */
5501 /* (a & (1 << n)) == 0, (1 << n), 0) */
5502 n = new_rd_Eor(get_irn_dbg_info(n), current_ir_graph,
5503 block, cmp_l, t, mode);
5509 and_l = get_And_left(cmp_l);
5510 if (is_Shl(and_l)) {
5511 ir_node *shl_l = get_Shl_left(and_l);
5512 if (is_Const(shl_l) && is_Const_one(shl_l)) {
5513 if (and_l == t && f == cmp_r) {
5514 if (pn == pn_Cmp_Lg) {
5515 /* ((1 << n) & a) != 0, (1 << n), 0) */
5518 /* ((1 << n) & a) == 0, (1 << n), 0) */
5519 n = new_rd_Eor(get_irn_dbg_info(n), current_ir_graph,
5520 block, cmp_l, t, mode);
5531 return arch_transform_node_Mux(n);
5532 } /* transform_node_Mux */
5535 * optimize Sync nodes that have other syncs as input we simply add the inputs
5536 * of the other sync to our own inputs
5538 static ir_node *transform_node_Sync(ir_node *n) {
5539 int arity = get_Sync_n_preds(n);
5542 for (i = 0; i < arity;) {
5543 ir_node *pred = get_Sync_pred(n, i);
5547 if (!is_Sync(pred)) {
5555 pred_arity = get_Sync_n_preds(pred);
5556 for (j = 0; j < pred_arity; ++j) {
5557 ir_node *pred_pred = get_Sync_pred(pred, j);
5562 add_irn_n(n, pred_pred);
5566 if (get_Sync_pred(n, k) == pred_pred) break;
5571 /* rehash the sync node */
5572 add_identities(current_ir_graph->value_table, n);
5578 * Tries several [inplace] [optimizing] transformations and returns an
5579 * equivalent node. The difference to equivalent_node() is that these
5580 * transformations _do_ generate new nodes, and thus the old node must
5581 * not be freed even if the equivalent node isn't the old one.
5583 static ir_node *transform_node(ir_node *n) {
5587 * Transform_node is the only "optimizing transformation" that might
5588 * return a node with a different opcode. We iterate HERE until fixpoint
5589 * to get the final result.
5593 if (n->op->ops.transform_node)
5594 n = n->op->ops.transform_node(n);
5595 } while (oldn != n);
5598 } /* transform_node */
5601 * Sets the default transform node operation for an ir_op_ops.
5603 * @param code the opcode for the default operation
5604 * @param ops the operations initialized
5609 static ir_op_ops *firm_set_default_transform_node(ir_opcode code, ir_op_ops *ops)
5613 ops->transform_node = transform_node_##a; \
5615 #define CASE_PROJ(a) \
5617 ops->transform_node_Proj = transform_node_Proj_##a; \
5619 #define CASE_PROJ_EX(a) \
5621 ops->transform_node = transform_node_##a; \
5622 ops->transform_node_Proj = transform_node_Proj_##a; \
5631 CASE_PROJ_EX(DivMod);
5665 } /* firm_set_default_transform_node */
5668 /* **************** Common Subexpression Elimination **************** */
5670 /** The size of the hash table used, should estimate the number of nodes
5672 #define N_IR_NODES 512
5674 /** Compares the attributes of two Const nodes. */
5675 static int node_cmp_attr_Const(ir_node *a, ir_node *b) {
5676 return (get_Const_tarval(a) != get_Const_tarval(b))
5677 || (get_Const_type(a) != get_Const_type(b));
5678 } /* node_cmp_attr_Const */
5680 /** Compares the attributes of two Proj nodes. */
5681 static int node_cmp_attr_Proj(ir_node *a, ir_node *b) {
5682 return get_irn_proj_attr(a) != get_irn_proj_attr(b);
5683 } /* node_cmp_attr_Proj */
5685 /** Compares the attributes of two Filter nodes. */
5686 static int node_cmp_attr_Filter(ir_node *a, ir_node *b) {
5687 return get_Filter_proj(a) != get_Filter_proj(b);
5688 } /* node_cmp_attr_Filter */
5690 /** Compares the attributes of two Alloc nodes. */
5691 static int node_cmp_attr_Alloc(ir_node *a, ir_node *b) {
5692 const alloc_attr *pa = get_irn_alloc_attr(a);
5693 const alloc_attr *pb = get_irn_alloc_attr(b);
5694 return (pa->where != pb->where) || (pa->type != pb->type);
5695 } /* node_cmp_attr_Alloc */
5697 /** Compares the attributes of two Free nodes. */
5698 static int node_cmp_attr_Free(ir_node *a, ir_node *b) {
5699 const free_attr *pa = get_irn_free_attr(a);
5700 const free_attr *pb = get_irn_free_attr(b);
5701 return (pa->where != pb->where) || (pa->type != pb->type);
5702 } /* node_cmp_attr_Free */
5704 /** Compares the attributes of two SymConst nodes. */
5705 static int node_cmp_attr_SymConst(ir_node *a, ir_node *b) {
5706 const symconst_attr *pa = get_irn_symconst_attr(a);
5707 const symconst_attr *pb = get_irn_symconst_attr(b);
5708 return (pa->kind != pb->kind)
5709 || (pa->sym.type_p != pb->sym.type_p)
5710 || (pa->tp != pb->tp);
5711 } /* node_cmp_attr_SymConst */
5713 /** Compares the attributes of two Call nodes. */
5714 static int node_cmp_attr_Call(ir_node *a, ir_node *b) {
5715 return get_irn_call_attr(a) != get_irn_call_attr(b);
5716 } /* node_cmp_attr_Call */
5718 /** Compares the attributes of two Sel nodes. */
5719 static int node_cmp_attr_Sel(ir_node *a, ir_node *b) {
5720 const ir_entity *a_ent = get_Sel_entity(a);
5721 const ir_entity *b_ent = get_Sel_entity(b);
5723 (a_ent->kind != b_ent->kind) ||
5724 (a_ent->name != b_ent->name) ||
5725 (a_ent->owner != b_ent->owner) ||
5726 (a_ent->ld_name != b_ent->ld_name) ||
5727 (a_ent->type != b_ent->type);
5728 } /* node_cmp_attr_Sel */
5730 /** Compares the attributes of two Phi nodes. */
5731 static int node_cmp_attr_Phi(ir_node *a, ir_node *b) {
5732 /* we can only enter this function if both nodes have the same number of inputs,
5733 hence it is enough to check if one of them is a Phi0 */
5735 /* check the Phi0 pos attribute */
5736 return get_irn_phi_attr(a)->u.pos != get_irn_phi_attr(b)->u.pos;
5739 } /* node_cmp_attr_Phi */
5741 /** Compares the attributes of two Conv nodes. */
5742 static int node_cmp_attr_Conv(ir_node *a, ir_node *b) {
5743 return get_Conv_strict(a) != get_Conv_strict(b);
5744 } /* node_cmp_attr_Conv */
5746 /** Compares the attributes of two Cast nodes. */
5747 static int node_cmp_attr_Cast(ir_node *a, ir_node *b) {
5748 return get_Cast_type(a) != get_Cast_type(b);
5749 } /* node_cmp_attr_Cast */
5751 /** Compares the attributes of two Load nodes. */
5752 static int node_cmp_attr_Load(ir_node *a, ir_node *b) {
5753 if (get_Load_volatility(a) == volatility_is_volatile ||
5754 get_Load_volatility(b) == volatility_is_volatile)
5755 /* NEVER do CSE on volatile Loads */
5757 /* do not CSE Loads with different alignment. Be conservative. */
5758 if (get_Load_align(a) != get_Load_align(b))
5761 return get_Load_mode(a) != get_Load_mode(b);
5762 } /* node_cmp_attr_Load */
5764 /** Compares the attributes of two Store nodes. */
5765 static int node_cmp_attr_Store(ir_node *a, ir_node *b) {
5766 /* do not CSE Stores with different alignment. Be conservative. */
5767 if (get_Store_align(a) != get_Store_align(b))
5770 /* NEVER do CSE on volatile Stores */
5771 return (get_Store_volatility(a) == volatility_is_volatile ||
5772 get_Store_volatility(b) == volatility_is_volatile);
5773 } /* node_cmp_attr_Store */
5775 /** Compares two exception attributes */
5776 static int node_cmp_exception(ir_node *a, ir_node *b) {
5777 const except_attr *ea = get_irn_except_attr(a);
5778 const except_attr *eb = get_irn_except_attr(b);
5780 return ea->pin_state != eb->pin_state;
5783 #define node_cmp_attr_Bound node_cmp_exception
5785 /** Compares the attributes of two Div nodes. */
5786 static int node_cmp_attr_Div(ir_node *a, ir_node *b) {
5787 const divmod_attr *ma = get_irn_divmod_attr(a);
5788 const divmod_attr *mb = get_irn_divmod_attr(b);
5789 return ma->exc.pin_state != mb->exc.pin_state ||
5790 ma->res_mode != mb->res_mode ||
5791 ma->no_remainder != mb->no_remainder;
5792 } /* node_cmp_attr_Div */
5794 /** Compares the attributes of two DivMod nodes. */
5795 static int node_cmp_attr_DivMod(ir_node *a, ir_node *b) {
5796 const divmod_attr *ma = get_irn_divmod_attr(a);
5797 const divmod_attr *mb = get_irn_divmod_attr(b);
5798 return ma->exc.pin_state != mb->exc.pin_state ||
5799 ma->res_mode != mb->res_mode;
5800 } /* node_cmp_attr_DivMod */
5802 /** Compares the attributes of two Mod nodes. */
5803 static int node_cmp_attr_Mod(ir_node *a, ir_node *b) {
5804 const divmod_attr *ma = get_irn_divmod_attr(a);
5805 const divmod_attr *mb = get_irn_divmod_attr(b);
5806 return ma->exc.pin_state != mb->exc.pin_state ||
5807 ma->res_mode != mb->res_mode;
5808 } /* node_cmp_attr_Mod */
5810 /** Compares the attributes of two Quot nodes. */
5811 static int node_cmp_attr_Quot(ir_node *a, ir_node *b) {
5812 const divmod_attr *ma = get_irn_divmod_attr(a);
5813 const divmod_attr *mb = get_irn_divmod_attr(b);
5814 return ma->exc.pin_state != mb->exc.pin_state ||
5815 ma->res_mode != mb->res_mode;
5816 } /* node_cmp_attr_Quot */
5818 /** Compares the attributes of two Confirm nodes. */
5819 static int node_cmp_attr_Confirm(ir_node *a, ir_node *b) {
5820 return (get_Confirm_cmp(a) != get_Confirm_cmp(b));
5821 } /* node_cmp_attr_Confirm */
5823 /** Compares the attributes of two ASM nodes. */
5824 static int node_cmp_attr_ASM(ir_node *a, ir_node *b) {
5826 const ir_asm_constraint *ca;
5827 const ir_asm_constraint *cb;
5830 if (get_ASM_text(a) != get_ASM_text(b))
5833 /* Should we really check the constraints here? Should be better, but is strange. */
5834 n = get_ASM_n_input_constraints(a);
5835 if (n != get_ASM_n_input_constraints(b))
5838 ca = get_ASM_input_constraints(a);
5839 cb = get_ASM_input_constraints(b);
5840 for (i = 0; i < n; ++i) {
5841 if (ca[i].pos != cb[i].pos || ca[i].constraint != cb[i].constraint)
5845 n = get_ASM_n_output_constraints(a);
5846 if (n != get_ASM_n_output_constraints(b))
5849 ca = get_ASM_output_constraints(a);
5850 cb = get_ASM_output_constraints(b);
5851 for (i = 0; i < n; ++i) {
5852 if (ca[i].pos != cb[i].pos || ca[i].constraint != cb[i].constraint)
5856 n = get_ASM_n_clobbers(a);
5857 if (n != get_ASM_n_clobbers(b))
5860 cla = get_ASM_clobbers(a);
5861 clb = get_ASM_clobbers(b);
5862 for (i = 0; i < n; ++i) {
5863 if (cla[i] != clb[i])
5867 } /* node_cmp_attr_ASM */
5870 * Set the default node attribute compare operation for an ir_op_ops.
5872 * @param code the opcode for the default operation
5873 * @param ops the operations initialized
5878 static ir_op_ops *firm_set_default_node_cmp_attr(ir_opcode code, ir_op_ops *ops)
5882 ops->node_cmp_attr = node_cmp_attr_##a; \
5913 } /* firm_set_default_node_cmp_attr */
5916 * Compare function for two nodes in the value table. Gets two
5917 * nodes as parameters. Returns 0 if the nodes are a Common Sub Expression.
5919 int identities_cmp(const void *elt, const void *key) {
5920 ir_node *a = (ir_node *)elt;
5921 ir_node *b = (ir_node *)key;
5924 if (a == b) return 0;
5926 if ((get_irn_op(a) != get_irn_op(b)) ||
5927 (get_irn_mode(a) != get_irn_mode(b))) return 1;
5929 /* compare if a's in and b's in are of equal length */
5930 irn_arity_a = get_irn_intra_arity(a);
5931 if (irn_arity_a != get_irn_intra_arity(b))
5934 if (get_irn_pinned(a) == op_pin_state_pinned) {
5935 /* for pinned nodes, the block inputs must be equal */
5936 if (get_irn_intra_n(a, -1) != get_irn_intra_n(b, -1))
5938 } else if (! get_opt_global_cse()) {
5939 /* for block-local CSE both nodes must be in the same MacroBlock */
5940 if (get_irn_MacroBlock(a) != get_irn_MacroBlock(b))
5944 /* compare a->in[0..ins] with b->in[0..ins] */
5945 for (i = 0; i < irn_arity_a; i++)
5946 if (get_irn_intra_n(a, i) != get_irn_intra_n(b, i))
5950 * here, we already now that the nodes are identical except their
5953 if (a->op->ops.node_cmp_attr)
5954 return a->op->ops.node_cmp_attr(a, b);
5957 } /* identities_cmp */
5960 * Calculate a hash value of a node.
5962 * @param node The IR-node
5964 unsigned ir_node_hash(const ir_node *node) {
5965 return node->op->ops.hash(node);
5966 } /* ir_node_hash */
5969 pset *new_identities(void) {
5970 return new_pset(identities_cmp, N_IR_NODES);
5971 } /* new_identities */
5973 void del_identities(pset *value_table) {
5974 del_pset(value_table);
5975 } /* del_identities */
5978 * Normalize a node by putting constants (and operands with larger
5979 * node index) on the right (operator side).
5981 * @param n The node to normalize
5983 static void normalize_node(ir_node *n) {
5984 if (is_op_commutative(get_irn_op(n))) {
5985 ir_node *l = get_binop_left(n);
5986 ir_node *r = get_binop_right(n);
5988 /* For commutative operators perform a OP b == b OP a but keep
5989 * constants on the RIGHT side. This helps greatly in some
5990 * optimizations. Moreover we use the idx number to make the form
5992 if (!operands_are_normalized(l, r)) {
5993 set_binop_left(n, r);
5994 set_binop_right(n, l);
5997 } /* normalize_node */
6000 * Update the nodes after a match in the value table. If both nodes have
6001 * the same MacroBlock but different Blocks, we must ensure that the node
6002 * with the dominating Block (the node that is near to the MacroBlock header
6003 * is stored in the table.
6004 * Because a MacroBlock has only one "non-exception" flow, we don't need
6005 * dominance info here: We known, that one block must dominate the other and
6006 * following the only block input will allow to find it.
6008 static void update_known_irn(ir_node *known_irn, const ir_node *new_ir_node) {
6009 ir_node *known_blk, *new_block, *block, *mbh;
6011 if (get_opt_global_cse()) {
6012 /* Block inputs are meaning less */
6015 known_blk = get_irn_n(known_irn, -1);
6016 new_block = get_irn_n(new_ir_node, -1);
6017 if (known_blk == new_block) {
6018 /* already in the same block */
6022 * We expect the typical case when we built the graph. In that case, the
6023 * known_irn is already the upper one, so checking this should be faster.
6026 mbh = get_Block_MacroBlock(new_block);
6028 if (block == known_blk) {
6029 /* ok, we have found it: known_block dominates new_block as expected */
6034 * We have reached the MacroBlock header NOT founding
6035 * the known_block. new_block must dominate known_block.
6038 set_irn_n(known_irn, -1, new_block);
6041 assert(get_Block_n_cfgpreds(block) == 1);
6042 block = get_Block_cfgpred_block(block, 0);
6044 } /* update_value_table */
6047 * Return the canonical node computing the same value as n.
6048 * Looks up the node in a hash table, enters it in the table
6049 * if it isn't there yet.
6051 * @param value_table the HashSet containing all nodes in the
6053 * @param n the node to look up
6055 * @return a node that computes the same value as n or n if no such
6056 * node could be found
6058 ir_node *identify_remember(pset *value_table, ir_node *n) {
6061 if (!value_table) return n;
6064 /* lookup or insert in hash table with given hash key. */
6065 o = pset_insert(value_table, n, ir_node_hash(n));
6068 update_known_irn(o, n);
6073 } /* identify_remember */
6076 * During construction we set the op_pin_state_pinned flag in the graph right when the
6077 * optimization is performed. The flag turning on procedure global cse could
6078 * be changed between two allocations. This way we are safe.
6080 * @param value_table The value table
6081 * @param n The node to lookup
6083 static INLINE ir_node *identify_cons(pset *value_table, ir_node *n) {
6086 n = identify_remember(value_table, n);
6087 if (n != old && get_irn_MacroBlock(old) != get_irn_MacroBlock(n))
6088 set_irg_pinned(current_ir_graph, op_pin_state_floats);
6090 } /* identify_cons */
6092 /* Add a node to the identities value table. */
6093 void add_identities(pset *value_table, ir_node *node) {
6094 if (get_opt_cse() && is_no_Block(node))
6095 identify_remember(value_table, node);
6096 } /* add_identities */
6098 /* Visit each node in the value table of a graph. */
6099 void visit_all_identities(ir_graph *irg, irg_walk_func visit, void *env) {
6101 ir_graph *rem = current_ir_graph;
6103 current_ir_graph = irg;
6104 foreach_pset(irg->value_table, node)
6106 current_ir_graph = rem;
6107 } /* visit_all_identities */
6110 * Garbage in, garbage out. If a node has a dead input, i.e., the
6111 * Bad node is input to the node, return the Bad node.
6113 static ir_node *gigo(ir_node *node) {
6115 ir_op *op = get_irn_op(node);
6117 /* remove garbage blocks by looking at control flow that leaves the block
6118 and replacing the control flow by Bad. */
6119 if (get_irn_mode(node) == mode_X) {
6120 ir_node *block = get_nodes_block(skip_Proj(node));
6122 /* Don't optimize nodes in immature blocks. */
6123 if (!get_Block_matured(block))
6125 /* Don't optimize End, may have Bads. */
6126 if (op == op_End) return node;
6128 if (is_Block(block)) {
6129 if (is_Block_dead(block)) {
6130 /* control flow from dead block is dead */
6134 for (i = get_irn_arity(block) - 1; i >= 0; --i) {
6135 if (!is_Bad(get_irn_n(block, i)))
6139 ir_graph *irg = get_irn_irg(block);
6140 /* the start block is never dead */
6141 if (block != get_irg_start_block(irg)
6142 && block != get_irg_end_block(irg)) {
6144 * Do NOT kill control flow without setting
6145 * the block to dead of bad things can happen:
6146 * We get a Block that is not reachable be irg_block_walk()
6147 * but can be found by irg_walk()!
6149 set_Block_dead(block);
6156 /* Blocks, Phis and Tuples may have dead inputs, e.g., if one of the
6157 blocks predecessors is dead. */
6158 if (op != op_Block && op != op_Phi && op != op_Tuple) {
6159 irn_arity = get_irn_arity(node);
6162 * Beware: we can only read the block of a non-floating node.
6164 if (is_irn_pinned_in_irg(node) &&
6165 is_Block_dead(get_nodes_block(skip_Proj(node))))
6168 for (i = 0; i < irn_arity; i++) {
6169 ir_node *pred = get_irn_n(node, i);
6174 /* Propagating Unknowns here seems to be a bad idea, because
6175 sometimes we need a node as a input and did not want that
6177 However, it might be useful to move this into a later phase
6178 (if you think that optimizing such code is useful). */
6179 if (is_Unknown(pred) && mode_is_data(get_irn_mode(node)))
6180 return new_Unknown(get_irn_mode(node));
6185 /* With this code we violate the agreement that local_optimize
6186 only leaves Bads in Block, Phi and Tuple nodes. */
6187 /* If Block has only Bads as predecessors it's garbage. */
6188 /* If Phi has only Bads as predecessors it's garbage. */
6189 if ((op == op_Block && get_Block_matured(node)) || op == op_Phi) {
6190 irn_arity = get_irn_arity(node);
6191 for (i = 0; i < irn_arity; i++) {
6192 if (!is_Bad(get_irn_n(node, i))) break;
6194 if (i == irn_arity) node = new_Bad();
6201 * These optimizations deallocate nodes from the obstack.
6202 * It can only be called if it is guaranteed that no other nodes
6203 * reference this one, i.e., right after construction of a node.
6205 * @param n The node to optimize
6207 * current_ir_graph must be set to the graph of the node!
6209 ir_node *optimize_node(ir_node *n) {
6212 ir_opcode iro = get_irn_opcode(n);
6214 /* Always optimize Phi nodes: part of the construction. */
6215 if ((!get_opt_optimize()) && (iro != iro_Phi)) return n;
6217 /* constant expression evaluation / constant folding */
6218 if (get_opt_constant_folding()) {
6219 /* neither constants nor Tuple values can be evaluated */
6220 if (iro != iro_Const && (get_irn_mode(n) != mode_T)) {
6221 unsigned fp_model = get_irg_fp_model(current_ir_graph);
6222 int old_fp_mode = tarval_enable_fp_ops((fp_model & fp_strict_algebraic) == 0);
6223 /* try to evaluate */
6224 tv = computed_value(n);
6225 if (tv != tarval_bad) {
6227 ir_type *old_tp = get_irn_type(n);
6228 int i, arity = get_irn_arity(n);
6232 * Try to recover the type of the new expression.
6234 for (i = 0; i < arity && !old_tp; ++i)
6235 old_tp = get_irn_type(get_irn_n(n, i));
6238 * we MUST copy the node here temporary, because it's still needed
6239 * for DBG_OPT_CSTEVAL
6241 node_size = offsetof(ir_node, attr) + n->op->attr_size;
6242 oldn = alloca(node_size);
6244 memcpy(oldn, n, node_size);
6245 CLONE_ARR_A(ir_node *, oldn->in, n->in);
6247 /* ARG, copy the in array, we need it for statistics */
6248 memcpy(oldn->in, n->in, ARR_LEN(n->in) * sizeof(n->in[0]));
6250 /* note the inplace edges module */
6251 edges_node_deleted(n, current_ir_graph);
6253 /* evaluation was successful -- replace the node. */
6254 irg_kill_node(current_ir_graph, n);
6255 nw = new_Const(get_tarval_mode(tv), tv);
6257 if (old_tp && get_type_mode(old_tp) == get_tarval_mode(tv))
6258 set_Const_type(nw, old_tp);
6259 DBG_OPT_CSTEVAL(oldn, nw);
6260 tarval_enable_fp_ops(old_fp_mode);
6263 tarval_enable_fp_ops(old_fp_mode);
6267 /* remove unnecessary nodes */
6268 if (get_opt_algebraic_simplification() ||
6269 (iro == iro_Phi) || /* always optimize these nodes. */
6271 (iro == iro_Proj) ||
6272 (iro == iro_Block) ) /* Flags tested local. */
6273 n = equivalent_node(n);
6275 /* Common Subexpression Elimination.
6277 * Checks whether n is already available.
6278 * The block input is used to distinguish different subexpressions. Right
6279 * now all nodes are op_pin_state_pinned to blocks, i.e., the CSE only finds common
6280 * subexpressions within a block.
6283 n = identify_cons(current_ir_graph->value_table, n);
6286 edges_node_deleted(oldn, current_ir_graph);
6288 /* We found an existing, better node, so we can deallocate the old node. */
6289 irg_kill_node(current_ir_graph, oldn);
6293 /* Some more constant expression evaluation that does not allow to
6295 iro = get_irn_opcode(n);
6296 if (get_opt_algebraic_simplification() ||
6297 (iro == iro_Cond) ||
6298 (iro == iro_Proj)) /* Flags tested local. */
6299 n = transform_node(n);
6301 /* Remove nodes with dead (Bad) input.
6302 Run always for transformation induced Bads. */
6305 /* Now we have a legal, useful node. Enter it in hash table for CSE */
6306 if (get_opt_cse() && (get_irn_opcode(n) != iro_Block)) {
6307 n = identify_remember(current_ir_graph->value_table, n);
6311 } /* optimize_node */
6315 * These optimizations never deallocate nodes (in place). This can cause dead
6316 * nodes lying on the obstack. Remove these by a dead node elimination,
6317 * i.e., a copying garbage collection.
6319 ir_node *optimize_in_place_2(ir_node *n) {
6322 ir_opcode iro = get_irn_opcode(n);
6324 if (!get_opt_optimize() && !is_Phi(n)) return n;
6326 /* constant expression evaluation / constant folding */
6327 if (get_opt_constant_folding()) {
6328 /* neither constants nor Tuple values can be evaluated */
6329 if (iro != iro_Const && get_irn_mode(n) != mode_T) {
6330 unsigned fp_model = get_irg_fp_model(current_ir_graph);
6331 int old_fp_mode = tarval_enable_fp_ops((fp_model & fp_strict_algebraic) == 0);
6332 /* try to evaluate */
6333 tv = computed_value(n);
6334 if (tv != tarval_bad) {
6335 /* evaluation was successful -- replace the node. */
6336 ir_type *old_tp = get_irn_type(n);
6337 int i, arity = get_irn_arity(n);
6340 * Try to recover the type of the new expression.
6342 for (i = 0; i < arity && !old_tp; ++i)
6343 old_tp = get_irn_type(get_irn_n(n, i));
6345 n = new_Const(get_tarval_mode(tv), tv);
6347 if (old_tp && get_type_mode(old_tp) == get_tarval_mode(tv))
6348 set_Const_type(n, old_tp);
6350 DBG_OPT_CSTEVAL(oldn, n);
6351 tarval_enable_fp_ops(old_fp_mode);
6354 tarval_enable_fp_ops(old_fp_mode);
6358 /* remove unnecessary nodes */
6359 if (get_opt_constant_folding() ||
6360 (iro == iro_Phi) || /* always optimize these nodes. */
6361 (iro == iro_Id) || /* ... */
6362 (iro == iro_Proj) || /* ... */
6363 (iro == iro_Block) ) /* Flags tested local. */
6364 n = equivalent_node(n);
6366 /** common subexpression elimination **/
6367 /* Checks whether n is already available. */
6368 /* The block input is used to distinguish different subexpressions. Right
6369 now all nodes are op_pin_state_pinned to blocks, i.e., the cse only finds common
6370 subexpressions within a block. */
6371 if (get_opt_cse()) {
6372 n = identify_remember(current_ir_graph->value_table, n);
6375 /* Some more constant expression evaluation. */
6376 iro = get_irn_opcode(n);
6377 if (get_opt_constant_folding() ||
6378 (iro == iro_Cond) ||
6379 (iro == iro_Proj)) /* Flags tested local. */
6380 n = transform_node(n);
6382 /* Remove nodes with dead (Bad) input.
6383 Run always for transformation induced Bads. */
6386 /* Now we can verify the node, as it has no dead inputs any more. */
6389 /* Now we have a legal, useful node. Enter it in hash table for cse.
6390 Blocks should be unique anyways. (Except the successor of start:
6391 is cse with the start block!) */
6392 if (get_opt_cse() && (get_irn_opcode(n) != iro_Block))
6393 n = identify_remember(current_ir_graph->value_table, n);
6396 } /* optimize_in_place_2 */
6399 * Wrapper for external use, set proper status bits after optimization.
6401 ir_node *optimize_in_place(ir_node *n) {
6402 /* Handle graph state */
6403 assert(get_irg_phase_state(current_ir_graph) != phase_building);
6405 if (get_opt_global_cse())
6406 set_irg_pinned(current_ir_graph, op_pin_state_floats);
6407 if (get_irg_outs_state(current_ir_graph) == outs_consistent)
6408 set_irg_outs_inconsistent(current_ir_graph);
6410 /* FIXME: Maybe we could also test whether optimizing the node can
6411 change the control graph. */
6412 set_irg_doms_inconsistent(current_ir_graph);
6413 return optimize_in_place_2(n);
6414 } /* optimize_in_place */
6417 * Calculate a hash value of a Const node.
6419 static unsigned hash_Const(const ir_node *node) {
6422 /* special value for const, as they only differ in their tarval. */
6423 h = HASH_PTR(node->attr.con.tv);
6424 h = 9*h + HASH_PTR(get_irn_mode(node));
6430 * Calculate a hash value of a SymConst node.
6432 static unsigned hash_SymConst(const ir_node *node) {
6435 /* special value for const, as they only differ in their symbol. */
6436 h = HASH_PTR(node->attr.symc.sym.type_p);
6437 h = 9*h + HASH_PTR(get_irn_mode(node));
6440 } /* hash_SymConst */
6443 * Set the default hash operation in an ir_op_ops.
6445 * @param code the opcode for the default operation
6446 * @param ops the operations initialized
6451 static ir_op_ops *firm_set_default_hash(ir_opcode code, ir_op_ops *ops)
6455 ops->hash = hash_##a; \
6458 /* hash function already set */
6459 if (ops->hash != NULL)
6466 /* use input/mode default hash if no function was given */
6467 ops->hash = firm_default_hash;
6475 * Sets the default operation for an ir_ops.
6477 ir_op_ops *firm_set_default_operations(ir_opcode code, ir_op_ops *ops) {
6478 ops = firm_set_default_hash(code, ops);
6479 ops = firm_set_default_computed_value(code, ops);
6480 ops = firm_set_default_equivalent_node(code, ops);
6481 ops = firm_set_default_transform_node(code, ops);
6482 ops = firm_set_default_node_cmp_attr(code, ops);
6483 ops = firm_set_default_get_type(code, ops);
6484 ops = firm_set_default_get_type_attr(code, ops);
6485 ops = firm_set_default_get_entity_attr(code, ops);
6488 } /* firm_set_default_operations */