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);
1233 /* leave strict floating point Conv's */
1237 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CONV);
1238 } else if (is_Conv(a)) { /* Conv(Conv(b)) */
1239 ir_node *b = get_Conv_op(a);
1240 ir_mode *b_mode = get_irn_mode(b);
1242 if (get_Conv_strict(n) && get_Conv_strict(a)) {
1243 /* both are strict conv */
1244 if (smaller_mode(a_mode, n_mode)) {
1245 /* both are strict, but the first is smaller, so
1246 the second cannot remove more precision, remove the
1248 set_Conv_strict(n, 0);
1251 if (n_mode == b_mode) {
1252 if (! get_Conv_strict(n) && ! get_Conv_strict(a)) {
1253 if (n_mode == mode_b) {
1254 n = b; /* Convb(Conv*(xxxb(...))) == xxxb(...) */
1255 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
1256 } else if (get_mode_arithmetic(n_mode) == get_mode_arithmetic(a_mode)) {
1257 if (smaller_mode(b_mode, a_mode)) {
1258 n = b; /* ConvS(ConvL(xxxS(...))) == xxxS(...) */
1259 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
1263 if (mode_is_int(n_mode) && mode_is_float(a_mode)) {
1264 /* ConvI(ConvF(I)) -> I, iff float mantissa >= int mode */
1265 size_t int_mantissa = get_mode_size_bits(n_mode) - (mode_is_signed(n_mode) ? 1 : 0);
1266 size_t float_mantissa;
1267 /* FIXME There is no way to get the mantissa size of a mode */
1268 switch (get_mode_size_bits(a_mode)) {
1269 case 32: float_mantissa = 23 + 1; break; // + 1 for implicit 1
1270 case 64: float_mantissa = 52 + 1; break;
1271 case 80: float_mantissa = 64 + 1; break;
1272 default: float_mantissa = 0; break;
1274 if (float_mantissa != 0 && float_mantissa >= int_mantissa) {
1276 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
1281 if (smaller_mode(b_mode, a_mode)) {
1282 if (get_Conv_strict(n))
1283 set_Conv_strict(b, 1);
1284 n = b; /* ConvA(ConvB(ConvA(...))) == ConvA(...) */
1285 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
1291 } /* equivalent_node_Conv */
1294 * A Cast may be removed if the type of the previous node
1295 * is already the type of the Cast.
1297 static ir_node *equivalent_node_Cast(ir_node *n) {
1299 ir_node *pred = get_Cast_op(n);
1301 if (get_irn_type(pred) == get_Cast_type(n)) {
1303 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CAST);
1306 } /* equivalent_node_Cast */
1309 * - fold Phi-nodes, iff they have only one predecessor except
1312 static ir_node *equivalent_node_Phi(ir_node *n) {
1317 ir_node *first_val = NULL; /* to shutup gcc */
1319 if (!get_opt_normalize()) return n;
1321 n_preds = get_Phi_n_preds(n);
1323 block = get_nodes_block(n);
1324 if (is_Block_dead(block)) /* Control dead */
1325 return get_irg_bad(current_ir_graph);
1327 if (n_preds == 0) return n; /* Phi of dead Region without predecessors. */
1329 /* Find first non-self-referencing input */
1330 for (i = 0; i < n_preds; ++i) {
1331 first_val = get_Phi_pred(n, i);
1332 if ( (first_val != n) /* not self pointer */
1334 /* BEWARE: when the if is changed to 1, Phi's will ignore it's Bad
1335 * predecessors. Then, Phi nodes in dead code might be removed, causing
1336 * nodes pointing to themself (Add's for instance).
1337 * This is really bad and causes endless recursions in several
1338 * code pathes, so we do NOT optimize such a code.
1339 * This is not that bad as it sounds, optimize_cf() removes bad control flow
1340 * (and bad Phi predecessors), so live code is optimized later.
1342 && (! is_Bad(get_Block_cfgpred(block, i)))
1344 ) { /* value not dead */
1345 break; /* then found first value. */
1350 /* A totally Bad or self-referencing Phi (we didn't break the above loop) */
1351 return get_irg_bad(current_ir_graph);
1354 /* search for rest of inputs, determine if any of these
1355 are non-self-referencing */
1356 while (++i < n_preds) {
1357 ir_node *scnd_val = get_Phi_pred(n, i);
1358 if ( (scnd_val != n)
1359 && (scnd_val != first_val)
1362 && (! is_Bad(get_Block_cfgpred(block, i)))
1370 /* Fold, if no multiple distinct non-self-referencing inputs */
1372 DBG_OPT_PHI(oldn, n);
1375 } /* equivalent_node_Phi */
1378 * Several optimizations:
1379 * - fold Sync-nodes, iff they have only one predecessor except
1382 static ir_node *equivalent_node_Sync(ir_node *n) {
1383 int arity = get_Sync_n_preds(n);
1386 for (i = 0; i < arity;) {
1387 ir_node *pred = get_Sync_pred(n, i);
1390 /* Remove Bad predecessors */
1397 /* Remove duplicate predecessors */
1403 if (get_Sync_pred(n, j) == pred) {
1411 if (arity == 0) return get_irg_bad(current_ir_graph);
1412 if (arity == 1) return get_Sync_pred(n, 0);
1414 } /* equivalent_node_Sync */
1417 * Optimize Proj(Tuple).
1419 static ir_node *equivalent_node_Proj_Tuple(ir_node *proj) {
1420 ir_node *oldn = proj;
1421 ir_node *tuple = get_Proj_pred(proj);
1423 /* Remove the Tuple/Proj combination. */
1424 proj = get_Tuple_pred(tuple, get_Proj_proj(proj));
1425 DBG_OPT_TUPLE(oldn, tuple, proj);
1428 } /* equivalent_node_Proj_Tuple */
1431 * Optimize a / 1 = a.
1433 static ir_node *equivalent_node_Proj_Div(ir_node *proj) {
1434 ir_node *oldn = proj;
1435 ir_node *div = get_Proj_pred(proj);
1436 ir_node *b = get_Div_right(div);
1437 tarval *tb = value_of(b);
1439 /* Div is not commutative. */
1440 if (tarval_is_one(tb)) { /* div(x, 1) == x */
1441 switch (get_Proj_proj(proj)) {
1443 proj = get_Div_mem(div);
1444 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NEUTRAL_1);
1448 proj = get_Div_left(div);
1449 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NEUTRAL_1);
1453 /* we cannot replace the exception Proj's here, this is done in
1454 transform_node_Proj_Div() */
1459 } /* equivalent_node_Proj_Div */
1462 * Optimize a / 1.0 = a.
1464 static ir_node *equivalent_node_Proj_Quot(ir_node *proj) {
1465 ir_node *oldn = proj;
1466 ir_node *quot = get_Proj_pred(proj);
1467 ir_node *b = get_Quot_right(quot);
1468 tarval *tb = value_of(b);
1470 /* Div is not commutative. */
1471 if (tarval_is_one(tb)) { /* Quot(x, 1) == x */
1472 switch (get_Proj_proj(proj)) {
1474 proj = get_Quot_mem(quot);
1475 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NEUTRAL_1);
1479 proj = get_Quot_left(quot);
1480 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NEUTRAL_1);
1484 /* we cannot replace the exception Proj's here, this is done in
1485 transform_node_Proj_Quot() */
1490 } /* equivalent_node_Proj_Quot */
1493 * Optimize a / 1 = a.
1495 static ir_node *equivalent_node_Proj_DivMod(ir_node *proj) {
1496 ir_node *oldn = proj;
1497 ir_node *divmod = get_Proj_pred(proj);
1498 ir_node *b = get_DivMod_right(divmod);
1499 tarval *tb = value_of(b);
1501 /* Div is not commutative. */
1502 if (tarval_is_one(tb)) { /* div(x, 1) == x */
1503 switch (get_Proj_proj(proj)) {
1505 proj = get_DivMod_mem(divmod);
1506 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NEUTRAL_1);
1509 case pn_DivMod_res_div:
1510 proj = get_DivMod_left(divmod);
1511 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NEUTRAL_1);
1515 /* we cannot replace the exception Proj's here, this is done in
1516 transform_node_Proj_DivMod().
1517 Note further that the pn_DivMod_res_div case is handled in
1518 computed_value_Proj(). */
1523 } /* equivalent_node_Proj_DivMod */
1526 * Optimize CopyB(mem, x, x) into a Nop.
1528 static ir_node *equivalent_node_Proj_CopyB(ir_node *proj) {
1529 ir_node *oldn = proj;
1530 ir_node *copyb = get_Proj_pred(proj);
1531 ir_node *a = get_CopyB_dst(copyb);
1532 ir_node *b = get_CopyB_src(copyb);
1535 /* Turn CopyB into a tuple (mem, jmp, bad, bad) */
1536 switch (get_Proj_proj(proj)) {
1537 case pn_CopyB_M_regular:
1538 proj = get_CopyB_mem(copyb);
1539 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NOP);
1542 case pn_CopyB_M_except:
1543 case pn_CopyB_X_except:
1544 DBG_OPT_EXC_REM(proj);
1545 proj = get_irg_bad(current_ir_graph);
1550 } /* equivalent_node_Proj_CopyB */
1553 * Optimize Bounds(idx, idx, upper) into idx.
1555 static ir_node *equivalent_node_Proj_Bound(ir_node *proj) {
1556 ir_node *oldn = proj;
1557 ir_node *bound = get_Proj_pred(proj);
1558 ir_node *idx = get_Bound_index(bound);
1559 ir_node *pred = skip_Proj(idx);
1562 if (idx == get_Bound_lower(bound))
1564 else if (is_Bound(pred)) {
1566 * idx was Bounds checked in the same MacroBlock previously,
1567 * it is still valid if lower <= pred_lower && pred_upper <= upper.
1569 ir_node *lower = get_Bound_lower(bound);
1570 ir_node *upper = get_Bound_upper(bound);
1571 if (get_Bound_lower(pred) == lower &&
1572 get_Bound_upper(pred) == upper &&
1573 get_irn_MacroBlock(bound) == get_irn_MacroBlock(pred)) {
1575 * One could expect that we simply return the previous
1576 * Bound here. However, this would be wrong, as we could
1577 * add an exception Proj to a new location then.
1578 * So, we must turn in into a tuple.
1584 /* Turn Bound into a tuple (mem, jmp, bad, idx) */
1585 switch (get_Proj_proj(proj)) {
1587 DBG_OPT_EXC_REM(proj);
1588 proj = get_Bound_mem(bound);
1590 case pn_Bound_X_except:
1591 DBG_OPT_EXC_REM(proj);
1592 proj = get_irg_bad(current_ir_graph);
1596 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NOP);
1599 /* cannot optimize pn_Bound_X_regular, handled in transform ... */
1604 } /* equivalent_node_Proj_Bound */
1607 * Optimize an Exception Proj(Load) with a non-null address.
1609 static ir_node *equivalent_node_Proj_Load(ir_node *proj) {
1610 if (get_opt_ldst_only_null_ptr_exceptions()) {
1611 if (get_irn_mode(proj) == mode_X) {
1612 ir_node *load = get_Proj_pred(proj);
1614 /* get the Load address */
1615 const ir_node *addr = get_Load_ptr(load);
1616 const ir_node *confirm;
1618 if (value_not_null(addr, &confirm)) {
1619 if (get_Proj_proj(proj) == pn_Load_X_except) {
1620 DBG_OPT_EXC_REM(proj);
1621 return get_irg_bad(current_ir_graph);
1627 } /* equivalent_node_Proj_Load */
1630 * Optimize an Exception Proj(Store) with a non-null address.
1632 static ir_node *equivalent_node_Proj_Store(ir_node *proj) {
1633 if (get_opt_ldst_only_null_ptr_exceptions()) {
1634 if (get_irn_mode(proj) == mode_X) {
1635 ir_node *store = get_Proj_pred(proj);
1637 /* get the load/store address */
1638 const ir_node *addr = get_Store_ptr(store);
1639 const ir_node *confirm;
1641 if (value_not_null(addr, &confirm)) {
1642 if (get_Proj_proj(proj) == pn_Store_X_except) {
1643 DBG_OPT_EXC_REM(proj);
1644 return get_irg_bad(current_ir_graph);
1650 } /* equivalent_node_Proj_Store */
1653 * Does all optimizations on nodes that must be done on it's Proj's
1654 * because of creating new nodes.
1656 static ir_node *equivalent_node_Proj(ir_node *proj) {
1657 ir_node *n = get_Proj_pred(proj);
1659 if (get_irn_mode(proj) == mode_X) {
1660 if (is_Block_dead(get_nodes_block(n))) {
1661 /* Remove dead control flow -- early gigo(). */
1662 return get_irg_bad(current_ir_graph);
1665 if (n->op->ops.equivalent_node_Proj)
1666 return n->op->ops.equivalent_node_Proj(proj);
1668 } /* equivalent_node_Proj */
1673 static ir_node *equivalent_node_Id(ir_node *n) {
1680 DBG_OPT_ID(oldn, n);
1682 } /* equivalent_node_Id */
1687 static ir_node *equivalent_node_Mux(ir_node *n)
1689 ir_node *oldn = n, *sel = get_Mux_sel(n);
1690 tarval *ts = value_of(sel);
1692 /* Mux(true, f, t) == t */
1693 if (ts == tarval_b_true) {
1694 n = get_Mux_true(n);
1695 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_C);
1697 /* Mux(false, f, t) == f */
1698 else if (ts == tarval_b_false) {
1699 n = get_Mux_false(n);
1700 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_C);
1702 /* Mux(v, x, x) == x */
1703 else if (get_Mux_false(n) == get_Mux_true(n)) {
1704 n = get_Mux_true(n);
1705 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_EQ);
1707 else if (is_Proj(sel) && !mode_honor_signed_zeros(get_irn_mode(n))) {
1708 ir_node *cmp = get_Proj_pred(sel);
1709 long proj_nr = get_Proj_proj(sel);
1710 ir_node *f = get_Mux_false(n);
1711 ir_node *t = get_Mux_true(n);
1714 * Note further that these optimization work even for floating point
1715 * with NaN's because -NaN == NaN.
1716 * However, if +0 and -0 is handled differently, we cannot use the first one.
1719 ir_node *const cmp_l = get_Cmp_left(cmp);
1720 ir_node *const cmp_r = get_Cmp_right(cmp);
1724 if ((cmp_l == t && cmp_r == f) || /* Mux(t == f, t, f) -> f */
1725 (cmp_l == f && cmp_r == t)) { /* Mux(f == t, t, f) -> f */
1727 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1734 if ((cmp_l == t && cmp_r == f) || /* Mux(t != f, t, f) -> t */
1735 (cmp_l == f && cmp_r == t)) { /* Mux(f != t, t, f) -> t */
1737 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1744 * Note: normalization puts the constant on the right side,
1745 * so we check only one case.
1747 if (cmp_l == t && tarval_is_null(value_of(cmp_r))) {
1748 /* Mux(t CMP 0, X, t) */
1749 if (is_Minus(f) && get_Minus_op(f) == t) {
1750 /* Mux(t CMP 0, -t, t) */
1751 if (proj_nr == pn_Cmp_Eq) {
1752 /* Mux(t == 0, -t, t) ==> -t */
1754 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1755 } else if (proj_nr == pn_Cmp_Lg || proj_nr == pn_Cmp_Ne) {
1756 /* Mux(t != 0, -t, t) ==> t */
1758 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1765 } /* equivalent_node_Mux */
1768 * Remove Confirm nodes if setting is on.
1769 * Replace Confirms(x, '=', Constlike) by Constlike.
1771 static ir_node *equivalent_node_Confirm(ir_node *n) {
1772 ir_node *pred = get_Confirm_value(n);
1773 pn_Cmp pnc = get_Confirm_cmp(n);
1775 while (is_Confirm(pred) && pnc == get_Confirm_cmp(pred)) {
1777 * rare case: two identical Confirms one after another,
1778 * replace the second one with the first.
1781 pred = get_Confirm_value(n);
1782 pnc = get_Confirm_cmp(n);
1784 if (get_opt_remove_confirm())
1785 return get_Confirm_value(n);
1790 * equivalent_node() returns a node equivalent to input n. It skips all nodes that
1791 * perform no actual computation, as, e.g., the Id nodes. It does not create
1792 * new nodes. It is therefore safe to free n if the node returned is not n.
1793 * If a node returns a Tuple we can not just skip it. If the size of the
1794 * in array fits, we transform n into a tuple (e.g., Div).
1796 ir_node *equivalent_node(ir_node *n) {
1797 if (n->op->ops.equivalent_node)
1798 return n->op->ops.equivalent_node(n);
1800 } /* equivalent_node */
1803 * Sets the default equivalent node operation for an ir_op_ops.
1805 * @param code the opcode for the default operation
1806 * @param ops the operations initialized
1811 static ir_op_ops *firm_set_default_equivalent_node(ir_opcode code, ir_op_ops *ops)
1815 ops->equivalent_node = equivalent_node_##a; \
1817 #define CASE_PROJ(a) \
1819 ops->equivalent_node_Proj = equivalent_node_Proj_##a; \
1861 } /* firm_set_default_equivalent_node */
1864 * Returns non-zero if a node is a Phi node
1865 * with all predecessors constant.
1867 static int is_const_Phi(ir_node *n) {
1870 if (! is_Phi(n) || get_irn_arity(n) == 0)
1872 for (i = get_irn_arity(n) - 1; i >= 0; --i)
1873 if (! is_Const(get_irn_n(n, i)))
1876 } /* is_const_Phi */
1878 typedef tarval *(*tarval_sub_type)(tarval *a, tarval *b, ir_mode *mode);
1879 typedef tarval *(*tarval_binop_type)(tarval *a, tarval *b);
1882 * Wrapper for the tarval binop evaluation, tarval_sub has one more parameter.
1884 static tarval *do_eval(tarval *(*eval)(), tarval *a, tarval *b, ir_mode *mode) {
1885 if (eval == tarval_sub) {
1886 tarval_sub_type func = (tarval_sub_type)eval;
1888 return func(a, b, mode);
1890 tarval_binop_type func = (tarval_binop_type)eval;
1897 * Apply an evaluator on a binop with a constant operators (and one Phi).
1899 * @param phi the Phi node
1900 * @param other the other operand
1901 * @param eval an evaluator function
1902 * @param mode the mode of the result, may be different from the mode of the Phi!
1903 * @param left if non-zero, other is the left operand, else the right
1905 * @return a new Phi node if the conversion was successful, NULL else
1907 static ir_node *apply_binop_on_phi(ir_node *phi, tarval *other, tarval *(*eval)(), ir_mode *mode, int left) {
1912 int i, n = get_irn_arity(phi);
1914 NEW_ARR_A(void *, res, n);
1916 for (i = 0; i < n; ++i) {
1917 pred = get_irn_n(phi, i);
1918 tv = get_Const_tarval(pred);
1919 tv = do_eval(eval, other, tv, mode);
1921 if (tv == tarval_bad) {
1922 /* folding failed, bad */
1928 for (i = 0; i < n; ++i) {
1929 pred = get_irn_n(phi, i);
1930 tv = get_Const_tarval(pred);
1931 tv = do_eval(eval, tv, other, mode);
1933 if (tv == tarval_bad) {
1934 /* folding failed, bad */
1940 irg = current_ir_graph;
1941 for (i = 0; i < n; ++i) {
1942 pred = get_irn_n(phi, i);
1943 res[i] = new_r_Const_type(irg, get_irg_start_block(irg),
1944 mode, res[i], get_Const_type(pred));
1946 return new_r_Phi(irg, get_nodes_block(phi), n, (ir_node **)res, mode);
1947 } /* apply_binop_on_phi */
1950 * Apply an evaluator on a binop with two constant Phi.
1952 * @param a the left Phi node
1953 * @param b the right Phi node
1954 * @param eval an evaluator function
1955 * @param mode the mode of the result, may be different from the mode of the Phi!
1957 * @return a new Phi node if the conversion was successful, NULL else
1959 static ir_node *apply_binop_on_2_phis(ir_node *a, ir_node *b, tarval *(*eval)(), ir_mode *mode) {
1960 tarval *tv_l, *tv_r, *tv;
1966 if (get_nodes_block(a) != get_nodes_block(b))
1969 n = get_irn_arity(a);
1970 NEW_ARR_A(void *, res, n);
1972 for (i = 0; i < n; ++i) {
1973 pred = get_irn_n(a, i);
1974 tv_l = get_Const_tarval(pred);
1975 pred = get_irn_n(b, i);
1976 tv_r = get_Const_tarval(pred);
1977 tv = do_eval(eval, tv_l, tv_r, mode);
1979 if (tv == tarval_bad) {
1980 /* folding failed, bad */
1985 irg = current_ir_graph;
1986 for (i = 0; i < n; ++i) {
1987 pred = get_irn_n(a, i);
1988 res[i] = new_r_Const_type(irg, get_irg_start_block(irg), mode, res[i], get_Const_type(pred));
1990 return new_r_Phi(irg, get_nodes_block(a), n, (ir_node **)res, mode);
1991 } /* apply_binop_on_2_phis */
1994 * Apply an evaluator on a unop with a constant operator (a Phi).
1996 * @param phi the Phi node
1997 * @param eval an evaluator function
1999 * @return a new Phi node if the conversion was successful, NULL else
2001 static ir_node *apply_unop_on_phi(ir_node *phi, tarval *(*eval)(tarval *)) {
2007 int i, n = get_irn_arity(phi);
2009 NEW_ARR_A(void *, res, n);
2010 for (i = 0; i < n; ++i) {
2011 pred = get_irn_n(phi, i);
2012 tv = get_Const_tarval(pred);
2015 if (tv == tarval_bad) {
2016 /* folding failed, bad */
2021 mode = get_irn_mode(phi);
2022 irg = current_ir_graph;
2023 for (i = 0; i < n; ++i) {
2024 pred = get_irn_n(phi, i);
2025 res[i] = new_r_Const_type(irg, get_irg_start_block(irg),
2026 mode, res[i], get_Const_type(pred));
2028 return new_r_Phi(irg, get_nodes_block(phi), n, (ir_node **)res, mode);
2029 } /* apply_unop_on_phi */
2032 * Apply a conversion on a constant operator (a Phi).
2034 * @param phi the Phi node
2036 * @return a new Phi node if the conversion was successful, NULL else
2038 static ir_node *apply_conv_on_phi(ir_node *phi, ir_mode *mode) {
2043 int i, n = get_irn_arity(phi);
2045 NEW_ARR_A(void *, res, n);
2046 for (i = 0; i < n; ++i) {
2047 pred = get_irn_n(phi, i);
2048 tv = get_Const_tarval(pred);
2049 tv = tarval_convert_to(tv, mode);
2051 if (tv == tarval_bad) {
2052 /* folding failed, bad */
2057 irg = current_ir_graph;
2058 for (i = 0; i < n; ++i) {
2059 pred = get_irn_n(phi, i);
2060 res[i] = new_r_Const_type(irg, get_irg_start_block(irg),
2061 mode, res[i], get_Const_type(pred));
2063 return new_r_Phi(irg, get_nodes_block(phi), n, (ir_node **)res, mode);
2064 } /* apply_conv_on_phi */
2067 * Transform AddP(P, ConvIs(Iu)), AddP(P, ConvIu(Is)) and
2068 * SubP(P, ConvIs(Iu)), SubP(P, ConvIu(Is)).
2069 * If possible, remove the Conv's.
2071 static ir_node *transform_node_AddSub(ir_node *n) {
2072 ir_mode *mode = get_irn_mode(n);
2074 if (mode_is_reference(mode)) {
2075 ir_node *left = get_binop_left(n);
2076 ir_node *right = get_binop_right(n);
2077 unsigned ref_bits = get_mode_size_bits(mode);
2079 if (is_Conv(left)) {
2080 ir_mode *lmode = get_irn_mode(left);
2081 unsigned bits = get_mode_size_bits(lmode);
2083 if (ref_bits == bits &&
2084 mode_is_int(lmode) &&
2085 get_mode_arithmetic(lmode) == irma_twos_complement) {
2086 ir_node *pre = get_Conv_op(left);
2087 ir_mode *pre_mode = get_irn_mode(pre);
2089 if (mode_is_int(pre_mode) &&
2090 get_mode_size_bits(pre_mode) == bits &&
2091 get_mode_arithmetic(pre_mode) == irma_twos_complement) {
2092 /* ok, this conv just changes to sign, moreover the calculation
2093 * is done with same number of bits as our address mode, so
2094 * we can ignore the conv as address calculation can be viewed
2095 * as either signed or unsigned
2097 set_binop_left(n, pre);
2102 if (is_Conv(right)) {
2103 ir_mode *rmode = get_irn_mode(right);
2104 unsigned bits = get_mode_size_bits(rmode);
2106 if (ref_bits == bits &&
2107 mode_is_int(rmode) &&
2108 get_mode_arithmetic(rmode) == irma_twos_complement) {
2109 ir_node *pre = get_Conv_op(right);
2110 ir_mode *pre_mode = get_irn_mode(pre);
2112 if (mode_is_int(pre_mode) &&
2113 get_mode_size_bits(pre_mode) == bits &&
2114 get_mode_arithmetic(pre_mode) == irma_twos_complement) {
2115 /* ok, this conv just changes to sign, moreover the calculation
2116 * is done with same number of bits as our address mode, so
2117 * we can ignore the conv as address calculation can be viewed
2118 * as either signed or unsigned
2120 set_binop_right(n, pre);
2125 /* let address arithmetic use unsigned modes */
2126 if (is_Const(right)) {
2127 ir_mode *rmode = get_irn_mode(right);
2129 if (mode_is_signed(rmode) && get_mode_arithmetic(rmode) == irma_twos_complement) {
2130 /* convert a AddP(P, *s) into AddP(P, *u) */
2131 ir_mode *nm = get_reference_mode_unsigned_eq(mode);
2133 ir_node *pre = new_r_Conv(current_ir_graph, get_nodes_block(n), right, nm);
2134 set_binop_right(n, pre);
2140 } /* transform_node_AddSub */
2142 #define HANDLE_BINOP_PHI(eval, a, b, c, mode) \
2144 if (is_Const(b) && is_const_Phi(a)) { \
2145 /* check for Op(Phi, Const) */ \
2146 c = apply_binop_on_phi(a, get_Const_tarval(b), eval, mode, 0);\
2148 else if (is_Const(a) && is_const_Phi(b)) { \
2149 /* check for Op(Const, Phi) */ \
2150 c = apply_binop_on_phi(b, get_Const_tarval(a), eval, mode, 1);\
2152 else if (is_const_Phi(a) && is_const_Phi(b)) { \
2153 /* check for Op(Phi, Phi) */ \
2154 c = apply_binop_on_2_phis(a, b, eval, mode); \
2157 DBG_OPT_ALGSIM0(oldn, c, FS_OPT_CONST_PHI); \
2161 #define HANDLE_UNOP_PHI(eval, a, c) \
2163 if (is_const_Phi(a)) { \
2164 /* check for Op(Phi) */ \
2165 c = apply_unop_on_phi(a, eval); \
2167 DBG_OPT_ALGSIM0(oldn, c, FS_OPT_CONST_PHI); \
2173 * Do the AddSub optimization, then Transform
2174 * Constant folding on Phi
2175 * Add(a,a) -> Mul(a, 2)
2176 * Add(Mul(a, x), a) -> Mul(a, x+1)
2177 * if the mode is integer or float.
2178 * Transform Add(a,-b) into Sub(a,b).
2179 * Reassociation might fold this further.
2181 static ir_node *transform_node_Add(ir_node *n) {
2183 ir_node *a, *b, *c, *oldn = n;
2185 n = transform_node_AddSub(n);
2187 a = get_Add_left(n);
2188 b = get_Add_right(n);
2190 mode = get_irn_mode(n);
2192 if (mode_is_reference(mode)) {
2193 ir_mode *lmode = get_irn_mode(a);
2195 if (is_Const(b) && is_Const_null(b) && mode_is_int(lmode)) {
2196 /* an Add(a, NULL) is a hidden Conv */
2197 dbg_info *dbg = get_irn_dbg_info(n);
2198 return new_rd_Conv(dbg, current_ir_graph, get_nodes_block(n), a, mode);
2202 HANDLE_BINOP_PHI(tarval_add, a, b, c, mode);
2204 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
2205 if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
2208 if (mode_is_num(mode)) {
2209 /* the following code leads to endless recursion when Mul are replaced by a simple instruction chain */
2210 if (!is_arch_dep_running() && a == b && mode_is_int(mode)) {
2211 ir_node *block = get_nodes_block(n);
2214 get_irn_dbg_info(n),
2218 new_r_Const_long(current_ir_graph, block, mode, 2),
2220 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_A);
2225 get_irn_dbg_info(n),
2231 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_MINUS_B);
2236 get_irn_dbg_info(n),
2242 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_MINUS_B);
2245 if (get_mode_arithmetic(mode) == irma_twos_complement) {
2246 /* Here we rely on constants be on the RIGHT side */
2248 ir_node *op = get_Not_op(a);
2250 if (is_Const(b) && is_Const_one(b)) {
2252 ir_node *blk = get_irn_n(n, -1);
2253 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, blk, op, mode);
2254 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_PLUS_1);
2259 ir_node *blk = get_irn_n(n, -1);
2260 n = new_r_Const(current_ir_graph, blk, mode, get_mode_minus_one(mode));
2261 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_X_NOT_X);
2266 ir_node *op = get_Not_op(b);
2270 ir_node *blk = get_irn_n(n, -1);
2271 n = new_r_Const(current_ir_graph, blk, mode, get_mode_minus_one(mode));
2272 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_X_NOT_X);
2279 } /* transform_node_Add */
2282 * returns -cnst or NULL if impossible
2284 static ir_node *const_negate(ir_node *cnst) {
2285 tarval *tv = tarval_neg(get_Const_tarval(cnst));
2286 dbg_info *dbgi = get_irn_dbg_info(cnst);
2287 ir_graph *irg = get_irn_irg(cnst);
2288 ir_node *block = get_nodes_block(cnst);
2289 ir_mode *mode = get_irn_mode(cnst);
2290 if (tv == tarval_bad) return NULL;
2291 return new_rd_Const(dbgi, irg, block, mode, tv);
2295 * Do the AddSub optimization, then Transform
2296 * Constant folding on Phi
2297 * Sub(0,a) -> Minus(a)
2298 * Sub(Mul(a, x), a) -> Mul(a, x-1)
2299 * Sub(Sub(x, y), b) -> Sub(x, Add(y,b))
2300 * Sub(Add(a, x), x) -> a
2301 * Sub(x, Add(x, a)) -> -a
2302 * Sub(x, Const) -> Add(x, -Const)
2304 static ir_node *transform_node_Sub(ir_node *n) {
2309 n = transform_node_AddSub(n);
2311 a = get_Sub_left(n);
2312 b = get_Sub_right(n);
2314 mode = get_irn_mode(n);
2316 if (mode_is_int(mode)) {
2317 ir_mode *lmode = get_irn_mode(a);
2319 if (is_Const(b) && is_Const_null(b) && mode_is_reference(lmode)) {
2320 /* a Sub(a, NULL) is a hidden Conv */
2321 dbg_info *dbg = get_irn_dbg_info(n);
2322 return new_rd_Conv(dbg, current_ir_graph, get_nodes_block(n), a, mode);
2327 HANDLE_BINOP_PHI(tarval_sub, a, b, c, mode);
2329 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
2330 if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
2333 if (is_Const(b) && get_irn_mode(b) != mode_P) {
2334 /* a - C -> a + (-C) */
2335 ir_node *cnst = const_negate(b);
2337 ir_node *block = get_nodes_block(n);
2338 dbg_info *dbgi = get_irn_dbg_info(n);
2339 ir_graph *irg = get_irn_irg(n);
2341 n = new_rd_Add(dbgi, irg, block, a, cnst, mode);
2342 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2347 if (is_Minus(a)) { /* (-a) - b -> -(a + b) */
2348 ir_graph *irg = current_ir_graph;
2349 dbg_info *dbg = get_irn_dbg_info(n);
2350 ir_node *block = get_nodes_block(n);
2351 ir_node *left = get_Minus_op(a);
2352 ir_node *add = new_rd_Add(dbg, irg, block, left, b, mode);
2354 n = new_rd_Minus(dbg, irg, block, add, mode);
2355 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2357 } else if (is_Minus(b)) { /* a - (-b) -> a + b */
2358 ir_graph *irg = current_ir_graph;
2359 dbg_info *dbg = get_irn_dbg_info(n);
2360 ir_node *block = get_nodes_block(n);
2361 ir_node *right = get_Minus_op(b);
2363 n = new_rd_Add(dbg, irg, block, a, right, mode);
2364 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MINUS);
2366 } else if (is_Sub(b)) { /* a - (b - c) -> a + (c - b) */
2367 ir_graph *irg = current_ir_graph;
2368 dbg_info *s_dbg = get_irn_dbg_info(b);
2369 ir_node *s_block = get_nodes_block(b);
2370 ir_node *s_left = get_Sub_right(b);
2371 ir_node *s_right = get_Sub_left(b);
2372 ir_mode *s_mode = get_irn_mode(b);
2373 ir_node *sub = new_rd_Sub(s_dbg, irg, s_block, s_left, s_right, s_mode);
2374 dbg_info *a_dbg = get_irn_dbg_info(n);
2375 ir_node *a_block = get_nodes_block(n);
2377 n = new_rd_Add(a_dbg, irg, a_block, a, sub, mode);
2378 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2380 } else if (is_Mul(b)) { /* a - (b * C) -> a + (b * -C) */
2381 ir_node *m_right = get_Mul_right(b);
2382 if (is_Const(m_right)) {
2383 ir_node *cnst2 = const_negate(m_right);
2384 if (cnst2 != NULL) {
2385 ir_graph *irg = current_ir_graph;
2386 dbg_info *m_dbg = get_irn_dbg_info(b);
2387 ir_node *m_block = get_nodes_block(b);
2388 ir_node *m_left = get_Mul_left(b);
2389 ir_mode *m_mode = get_irn_mode(b);
2390 ir_node *mul = new_rd_Mul(m_dbg, irg, m_block, m_left, cnst2, m_mode);
2391 dbg_info *a_dbg = get_irn_dbg_info(n);
2392 ir_node *a_block = get_nodes_block(n);
2394 n = new_rd_Add(a_dbg, irg, a_block, a, mul, mode);
2395 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2401 /* Beware of Sub(P, P) which cannot be optimized into a simple Minus ... */
2402 if (mode_is_num(mode) && mode == get_irn_mode(a) && is_Const(a) && is_Const_null(a)) {
2404 get_irn_dbg_info(n),
2409 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_0_A);
2413 if (mode_wrap_around(mode)) {
2414 ir_node *left = get_Add_left(a);
2415 ir_node *right = get_Add_right(a);
2417 /* FIXME: Does the Conv's work only for two complement or generally? */
2419 if (mode != get_irn_mode(right)) {
2420 /* This Sub is an effective Cast */
2421 right = new_r_Conv(get_irn_irg(n), get_irn_n(n, -1), right, mode);
2424 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2426 } else if (right == b) {
2427 if (mode != get_irn_mode(left)) {
2428 /* This Sub is an effective Cast */
2429 left = new_r_Conv(get_irn_irg(n), get_irn_n(n, -1), left, mode);
2432 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2438 if (mode_wrap_around(mode)) {
2439 ir_node *left = get_Add_left(b);
2440 ir_node *right = get_Add_right(b);
2442 /* FIXME: Does the Conv's work only for two complement or generally? */
2444 ir_mode *r_mode = get_irn_mode(right);
2446 n = new_r_Minus(get_irn_irg(n), get_irn_n(n, -1), right, r_mode);
2447 if (mode != r_mode) {
2448 /* This Sub is an effective Cast */
2449 n = new_r_Conv(get_irn_irg(n), get_irn_n(n, -1), n, mode);
2451 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2453 } else if (right == a) {
2454 ir_mode *l_mode = get_irn_mode(left);
2456 n = new_r_Minus(get_irn_irg(n), get_irn_n(n, -1), left, l_mode);
2457 if (mode != l_mode) {
2458 /* This Sub is an effective Cast */
2459 n = new_r_Conv(get_irn_irg(n), get_irn_n(n, -1), n, mode);
2461 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2466 if (mode_is_int(mode) && is_Conv(a) && is_Conv(b)) {
2467 ir_mode *mode = get_irn_mode(a);
2469 if (mode == get_irn_mode(b)) {
2471 ir_node *op_a = get_Conv_op(a);
2472 ir_node *op_b = get_Conv_op(b);
2474 /* check if it's allowed to skip the conv */
2475 ma = get_irn_mode(op_a);
2476 mb = get_irn_mode(op_b);
2478 if (mode_is_reference(ma) && mode_is_reference(mb)) {
2479 /* SubInt(ConvInt(aP), ConvInt(bP)) -> SubInt(aP,bP) */
2482 set_Sub_right(n, b);
2488 /* do NOT execute this code if reassociation is enabled, it does the inverse! */
2489 if (!is_reassoc_running() && is_Mul(a)) {
2490 ir_node *ma = get_Mul_left(a);
2491 ir_node *mb = get_Mul_right(a);
2494 ir_node *blk = get_irn_n(n, -1);
2496 get_irn_dbg_info(n),
2497 current_ir_graph, blk,
2500 get_irn_dbg_info(n),
2501 current_ir_graph, blk,
2503 new_r_Const_long(current_ir_graph, blk, mode, 1),
2506 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MUL_A_X_A);
2508 } else if (mb == b) {
2509 ir_node *blk = get_irn_n(n, -1);
2511 get_irn_dbg_info(n),
2512 current_ir_graph, blk,
2515 get_irn_dbg_info(n),
2516 current_ir_graph, blk,
2518 new_r_Const_long(current_ir_graph, blk, mode, 1),
2521 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MUL_A_X_A);
2525 if (is_Sub(a)) { /* (x - y) - b -> x - (y + b) */
2526 ir_node *x = get_Sub_left(a);
2527 ir_node *y = get_Sub_right(a);
2528 ir_node *blk = get_irn_n(n, -1);
2529 ir_mode *m_b = get_irn_mode(b);
2530 ir_mode *m_y = get_irn_mode(y);
2534 /* Determine the right mode for the Add. */
2537 else if (mode_is_reference(m_b))
2539 else if (mode_is_reference(m_y))
2543 * Both modes are different but none is reference,
2544 * happens for instance in SubP(SubP(P, Iu), Is).
2545 * We have two possibilities here: Cast or ignore.
2546 * Currently we ignore this case.
2551 add = new_r_Add(current_ir_graph, blk, y, b, add_mode);
2553 n = new_rd_Sub(get_irn_dbg_info(n), current_ir_graph, blk, x, add, mode);
2554 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_SUB_X_Y_Z);
2558 if (get_mode_arithmetic(mode) == irma_twos_complement) {
2559 if (is_Const(a) && is_Not(b)) {
2560 /* c - ~X = X + (c+1) */
2561 tarval *tv = get_Const_tarval(a);
2563 tv = tarval_add(tv, get_mode_one(mode));
2564 if (tv != tarval_bad) {
2565 ir_node *blk = get_irn_n(n, -1);
2566 ir_node *c = new_r_Const(current_ir_graph, blk, mode, tv);
2567 n = new_rd_Add(get_irn_dbg_info(n), current_ir_graph, blk, get_Not_op(b), c, mode);
2568 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_C_NOT_X);
2574 } /* transform_node_Sub */
2577 * Several transformation done on n*n=2n bits mul.
2578 * These transformations must be done here because new nodes may be produced.
2580 static ir_node *transform_node_Mul2n(ir_node *n, ir_mode *mode) {
2582 ir_node *a = get_Mul_left(n);
2583 ir_node *b = get_Mul_right(n);
2584 tarval *ta = value_of(a);
2585 tarval *tb = value_of(b);
2586 ir_mode *smode = get_irn_mode(a);
2588 if (ta == get_mode_one(smode)) {
2589 /* (L)1 * (L)b = (L)b */
2590 ir_node *blk = get_irn_n(n, -1);
2591 n = new_rd_Conv(get_irn_dbg_info(n), current_ir_graph, blk, b, mode);
2592 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
2595 else if (ta == get_mode_minus_one(smode)) {
2596 /* (L)-1 * (L)b = (L)b */
2597 ir_node *blk = get_irn_n(n, -1);
2598 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, blk, b, smode);
2599 n = new_rd_Conv(get_irn_dbg_info(n), current_ir_graph, blk, n, mode);
2600 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2603 if (tb == get_mode_one(smode)) {
2604 /* (L)a * (L)1 = (L)a */
2605 ir_node *blk = get_irn_n(a, -1);
2606 n = new_rd_Conv(get_irn_dbg_info(n), current_ir_graph, blk, a, mode);
2607 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
2610 else if (tb == get_mode_minus_one(smode)) {
2611 /* (L)a * (L)-1 = (L)-a */
2612 ir_node *blk = get_irn_n(n, -1);
2613 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, blk, a, smode);
2614 n = new_rd_Conv(get_irn_dbg_info(n), current_ir_graph, blk, n, mode);
2615 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2622 * Transform Mul(a,-1) into -a.
2623 * Do constant evaluation of Phi nodes.
2624 * Do architecture dependent optimizations on Mul nodes
2626 static ir_node *transform_node_Mul(ir_node *n) {
2627 ir_node *c, *oldn = n;
2628 ir_mode *mode = get_irn_mode(n);
2629 ir_node *a = get_Mul_left(n);
2630 ir_node *b = get_Mul_right(n);
2632 if (is_Bad(a) || is_Bad(b))
2635 if (mode != get_irn_mode(a))
2636 return transform_node_Mul2n(n, mode);
2638 HANDLE_BINOP_PHI(tarval_mul, a, b, c, mode);
2640 if (mode_is_signed(mode)) {
2643 if (value_of(a) == get_mode_minus_one(mode))
2645 else if (value_of(b) == get_mode_minus_one(mode))
2648 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1), r, mode);
2649 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2654 if (is_Const(b)) { /* (-a) * const -> a * -const */
2655 ir_node *cnst = const_negate(b);
2657 dbg_info *dbgi = get_irn_dbg_info(n);
2658 ir_node *block = get_nodes_block(n);
2659 n = new_rd_Mul(dbgi, current_ir_graph, block, get_Minus_op(a), cnst, mode);
2660 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2663 } else if (is_Minus(b)) { /* (-a) * (-b) -> a * b */
2664 dbg_info *dbgi = get_irn_dbg_info(n);
2665 ir_node *block = get_nodes_block(n);
2666 n = new_rd_Mul(dbgi, current_ir_graph, block, get_Minus_op(a), get_Minus_op(b), mode);
2667 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_MINUS);
2669 } else if (is_Sub(b)) { /* (-a) * (b - c) -> a * (c - b) */
2670 ir_node *sub_l = get_Sub_left(b);
2671 ir_node *sub_r = get_Sub_right(b);
2672 dbg_info *dbgi = get_irn_dbg_info(n);
2673 ir_graph *irg = current_ir_graph;
2674 ir_node *block = get_nodes_block(n);
2675 ir_node *new_b = new_rd_Sub(dbgi, irg, block, sub_r, sub_l, mode);
2676 n = new_rd_Mul(dbgi, irg, block, get_Minus_op(a), new_b, mode);
2677 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS);
2680 } else if (is_Minus(b)) {
2681 if (is_Sub(a)) { /* (a - b) * (-c) -> (b - a) * c */
2682 ir_node *sub_l = get_Sub_left(a);
2683 ir_node *sub_r = get_Sub_right(a);
2684 dbg_info *dbgi = get_irn_dbg_info(n);
2685 ir_graph *irg = current_ir_graph;
2686 ir_node *block = get_nodes_block(n);
2687 ir_node *new_a = new_rd_Sub(dbgi, irg, block, sub_r, sub_l, mode);
2688 n = new_rd_Mul(dbgi, irg, block, new_a, get_Minus_op(b), mode);
2689 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS);
2693 if (get_mode_arithmetic(mode) == irma_ieee754) {
2695 tarval *tv = get_Const_tarval(a);
2696 if (tarval_ieee754_get_exponent(tv) == 1 && tarval_ieee754_zero_mantissa(tv)) {
2697 /* 2.0 * b = b + b */
2698 n = new_rd_Add(get_irn_dbg_info(n), current_ir_graph, get_nodes_block(n), b, b, mode);
2699 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_A_A);
2703 else if (is_Const(b)) {
2704 tarval *tv = get_Const_tarval(b);
2705 if (tarval_ieee754_get_exponent(tv) == 1 && tarval_ieee754_zero_mantissa(tv)) {
2706 /* a * 2.0 = a + a */
2707 n = new_rd_Add(get_irn_dbg_info(n), current_ir_graph, get_nodes_block(n), a, a, mode);
2708 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_A_A);
2713 return arch_dep_replace_mul_with_shifts(n);
2714 } /* transform_node_Mul */
2717 * Transform a Div Node.
2719 static ir_node *transform_node_Div(ir_node *n) {
2720 ir_mode *mode = get_Div_resmode(n);
2721 ir_node *a = get_Div_left(n);
2722 ir_node *b = get_Div_right(n);
2726 if (is_Const(b) && is_const_Phi(a)) {
2727 /* check for Div(Phi, Const) */
2728 value = apply_binop_on_phi(a, get_Const_tarval(b), tarval_div, mode, 0);
2730 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2734 else if (is_Const(a) && is_const_Phi(b)) {
2735 /* check for Div(Const, Phi) */
2736 value = apply_binop_on_phi(b, get_Const_tarval(a), tarval_div, mode, 1);
2738 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2742 else if (is_const_Phi(a) && is_const_Phi(b)) {
2743 /* check for Div(Phi, Phi) */
2744 value = apply_binop_on_2_phis(a, b, tarval_div, mode);
2746 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2753 if (tv != tarval_bad) {
2754 value = new_Const(get_tarval_mode(tv), tv);
2756 DBG_OPT_CSTEVAL(n, value);
2759 ir_node *a = get_Div_left(n);
2760 ir_node *b = get_Div_right(n);
2761 const ir_node *dummy;
2763 if (a == b && value_not_zero(a, &dummy)) {
2764 /* BEWARE: we can optimize a/a to 1 only if this cannot cause a exception */
2765 value = new_Const(mode, get_mode_one(mode));
2766 DBG_OPT_CSTEVAL(n, value);
2769 if (mode_is_signed(mode) && is_Const(b)) {
2770 tarval *tv = get_Const_tarval(b);
2772 if (tv == get_mode_minus_one(mode)) {
2774 value = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1), a, mode);
2775 DBG_OPT_CSTEVAL(n, value);
2779 /* Try architecture dependent optimization */
2780 value = arch_dep_replace_div_by_const(n);
2788 /* Turn Div into a tuple (mem, jmp, bad, value) */
2789 mem = get_Div_mem(n);
2790 blk = get_irn_n(n, -1);
2792 /* skip a potential Pin */
2794 mem = get_Pin_op(mem);
2795 turn_into_tuple(n, pn_Div_max);
2796 set_Tuple_pred(n, pn_Div_M, mem);
2797 set_Tuple_pred(n, pn_Div_X_regular, new_r_Jmp(current_ir_graph, blk));
2798 set_Tuple_pred(n, pn_Div_X_except, new_Bad());
2799 set_Tuple_pred(n, pn_Div_res, value);
2802 } /* transform_node_Div */
2805 * Transform a Mod node.
2807 static ir_node *transform_node_Mod(ir_node *n) {
2808 ir_mode *mode = get_Mod_resmode(n);
2809 ir_node *a = get_Mod_left(n);
2810 ir_node *b = get_Mod_right(n);
2814 if (is_Const(b) && is_const_Phi(a)) {
2815 /* check for Div(Phi, Const) */
2816 value = apply_binop_on_phi(a, get_Const_tarval(b), tarval_mod, mode, 0);
2818 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2822 else if (is_Const(a) && is_const_Phi(b)) {
2823 /* check for Div(Const, Phi) */
2824 value = apply_binop_on_phi(b, get_Const_tarval(a), tarval_mod, mode, 1);
2826 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2830 else if (is_const_Phi(a) && is_const_Phi(b)) {
2831 /* check for Div(Phi, Phi) */
2832 value = apply_binop_on_2_phis(a, b, tarval_mod, mode);
2834 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2841 if (tv != tarval_bad) {
2842 value = new_Const(get_tarval_mode(tv), tv);
2844 DBG_OPT_CSTEVAL(n, value);
2847 ir_node *a = get_Mod_left(n);
2848 ir_node *b = get_Mod_right(n);
2849 const ir_node *dummy;
2851 if (a == b && value_not_zero(a, &dummy)) {
2852 /* BEWARE: we can optimize a%a to 0 only if this cannot cause a exception */
2853 value = new_Const(mode, get_mode_null(mode));
2854 DBG_OPT_CSTEVAL(n, value);
2857 if (mode_is_signed(mode) && is_Const(b)) {
2858 tarval *tv = get_Const_tarval(b);
2860 if (tv == get_mode_minus_one(mode)) {
2862 value = new_Const(mode, get_mode_null(mode));
2863 DBG_OPT_CSTEVAL(n, value);
2867 /* Try architecture dependent optimization */
2868 value = arch_dep_replace_mod_by_const(n);
2876 /* Turn Mod into a tuple (mem, jmp, bad, value) */
2877 mem = get_Mod_mem(n);
2878 blk = get_irn_n(n, -1);
2880 /* skip a potential Pin */
2882 mem = get_Pin_op(mem);
2883 turn_into_tuple(n, pn_Mod_max);
2884 set_Tuple_pred(n, pn_Mod_M, mem);
2885 set_Tuple_pred(n, pn_Mod_X_regular, new_r_Jmp(current_ir_graph, blk));
2886 set_Tuple_pred(n, pn_Mod_X_except, new_Bad());
2887 set_Tuple_pred(n, pn_Mod_res, value);
2890 } /* transform_node_Mod */
2893 * Transform a DivMod node.
2895 static ir_node *transform_node_DivMod(ir_node *n) {
2896 const ir_node *dummy;
2897 ir_node *a = get_DivMod_left(n);
2898 ir_node *b = get_DivMod_right(n);
2899 ir_mode *mode = get_DivMod_resmode(n);
2904 if (is_Const(b) && is_const_Phi(a)) {
2905 /* check for Div(Phi, Const) */
2906 va = apply_binop_on_phi(a, get_Const_tarval(b), tarval_div, mode, 0);
2907 vb = apply_binop_on_phi(a, get_Const_tarval(b), tarval_mod, mode, 0);
2909 DBG_OPT_ALGSIM0(n, va, FS_OPT_CONST_PHI);
2910 DBG_OPT_ALGSIM0(n, vb, FS_OPT_CONST_PHI);
2914 else if (is_Const(a) && is_const_Phi(b)) {
2915 /* check for Div(Const, Phi) */
2916 va = apply_binop_on_phi(b, get_Const_tarval(a), tarval_div, mode, 1);
2917 vb = apply_binop_on_phi(b, get_Const_tarval(a), tarval_mod, mode, 1);
2919 DBG_OPT_ALGSIM0(n, va, FS_OPT_CONST_PHI);
2920 DBG_OPT_ALGSIM0(n, vb, FS_OPT_CONST_PHI);
2924 else if (is_const_Phi(a) && is_const_Phi(b)) {
2925 /* check for Div(Phi, Phi) */
2926 va = apply_binop_on_2_phis(a, b, tarval_div, mode);
2927 vb = apply_binop_on_2_phis(a, b, tarval_mod, mode);
2929 DBG_OPT_ALGSIM0(n, va, FS_OPT_CONST_PHI);
2930 DBG_OPT_ALGSIM0(n, vb, FS_OPT_CONST_PHI);
2937 if (tb != tarval_bad) {
2938 if (tb == get_mode_one(get_tarval_mode(tb))) {
2940 vb = new_Const(mode, get_mode_null(mode));
2941 DBG_OPT_CSTEVAL(n, vb);
2943 } else if (ta != tarval_bad) {
2944 tarval *resa, *resb;
2945 resa = tarval_div(ta, tb);
2946 if (resa == tarval_bad) return n; /* Causes exception!!! Model by replacing through
2947 Jmp for X result!? */
2948 resb = tarval_mod(ta, tb);
2949 if (resb == tarval_bad) return n; /* Causes exception! */
2950 va = new_Const(mode, resa);
2951 vb = new_Const(mode, resb);
2952 DBG_OPT_CSTEVAL(n, va);
2953 DBG_OPT_CSTEVAL(n, vb);
2955 } else if (mode_is_signed(mode) && tb == get_mode_minus_one(mode)) {
2956 va = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1), a, mode);
2957 vb = new_Const(mode, get_mode_null(mode));
2958 DBG_OPT_CSTEVAL(n, va);
2959 DBG_OPT_CSTEVAL(n, vb);
2961 } else { /* Try architecture dependent optimization */
2964 arch_dep_replace_divmod_by_const(&va, &vb, n);
2965 evaluated = va != NULL;
2967 } else if (a == b) {
2968 if (value_not_zero(a, &dummy)) {
2970 va = new_Const(mode, get_mode_one(mode));
2971 vb = new_Const(mode, get_mode_null(mode));
2972 DBG_OPT_CSTEVAL(n, va);
2973 DBG_OPT_CSTEVAL(n, vb);
2976 /* BEWARE: it is NOT possible to optimize a/a to 1, as this may cause a exception */
2979 } else if (ta == get_mode_null(mode) && value_not_zero(b, &dummy)) {
2980 /* 0 / non-Const = 0 */
2985 if (evaluated) { /* replace by tuple */
2989 mem = get_DivMod_mem(n);
2990 /* skip a potential Pin */
2992 mem = get_Pin_op(mem);
2994 blk = get_irn_n(n, -1);
2995 turn_into_tuple(n, pn_DivMod_max);
2996 set_Tuple_pred(n, pn_DivMod_M, mem);
2997 set_Tuple_pred(n, pn_DivMod_X_regular, new_r_Jmp(current_ir_graph, blk));
2998 set_Tuple_pred(n, pn_DivMod_X_except, new_Bad()); /* no exception */
2999 set_Tuple_pred(n, pn_DivMod_res_div, va);
3000 set_Tuple_pred(n, pn_DivMod_res_mod, vb);
3004 } /* transform_node_DivMod */
3007 * Optimize x / c to x * (1/c)
3009 static ir_node *transform_node_Quot(ir_node *n) {
3010 ir_mode *mode = get_Quot_resmode(n);
3013 if (get_mode_arithmetic(mode) == irma_ieee754) {
3014 ir_node *b = get_Quot_right(n);
3017 tarval *tv = get_Const_tarval(b);
3021 * Floating point constant folding might be disabled here to
3023 * However, as we check for exact result, doing it is safe.
3026 rem = tarval_enable_fp_ops(1);
3027 tv = tarval_quo(get_mode_one(mode), tv);
3028 (void)tarval_enable_fp_ops(rem);
3030 /* Do the transformation if the result is either exact or we are not
3031 using strict rules. */
3032 if (tv != tarval_bad &&
3033 (tarval_ieee754_get_exact() || (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic) == 0)) {
3034 ir_node *blk = get_irn_n(n, -1);
3035 ir_node *c = new_r_Const(current_ir_graph, blk, mode, tv);
3036 ir_node *a = get_Quot_left(n);
3037 ir_node *m = new_rd_Mul(get_irn_dbg_info(n), current_ir_graph, blk, a, c, mode);
3038 ir_node *mem = get_Quot_mem(n);
3040 /* skip a potential Pin */
3042 mem = get_Pin_op(mem);
3043 turn_into_tuple(n, pn_Quot_max);
3044 set_Tuple_pred(n, pn_Quot_M, mem);
3045 set_Tuple_pred(n, pn_Quot_X_regular, new_r_Jmp(current_ir_graph, blk));
3046 set_Tuple_pred(n, pn_Quot_X_except, new_r_Bad(current_ir_graph));
3047 set_Tuple_pred(n, pn_Quot_res, m);
3048 DBG_OPT_ALGSIM1(oldn, a, b, m, FS_OPT_FP_INV_MUL);
3053 } /* transform_node_Quot */
3056 * Optimize Abs(x) into x if x is Confirmed >= 0
3057 * Optimize Abs(x) into -x if x is Confirmed <= 0
3058 * Optimize Abs(-x) int Abs(x)
3060 static ir_node *transform_node_Abs(ir_node *n) {
3061 ir_node *c, *oldn = n;
3062 ir_node *a = get_Abs_op(n);
3065 HANDLE_UNOP_PHI(tarval_abs, a, c);
3067 switch (classify_value_sign(a)) {
3068 case value_classified_negative:
3069 mode = get_irn_mode(n);
3072 * We can replace the Abs by -x here.
3073 * We even could add a new Confirm here
3074 * (if not twos complement)
3076 * Note that -x would create a new node, so we could
3077 * not run it in the equivalent_node() context.
3079 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph,
3080 get_nodes_block(n), a, mode);
3082 DBG_OPT_CONFIRM(oldn, n);
3084 case value_classified_positive:
3085 /* n is positive, Abs is not needed */
3088 DBG_OPT_CONFIRM(oldn, n);
3094 /* Abs(-x) = Abs(x) */
3095 mode = get_irn_mode(n);
3096 n = new_rd_Abs(get_irn_dbg_info(n), current_ir_graph,
3097 get_nodes_block(n), get_Minus_op(a), mode);
3098 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ABS_MINUS_X);
3102 } /* transform_node_Abs */
3105 * Optimize -a CMP -b into b CMP a.
3106 * This works only for for modes where unary Minus
3108 * Note that two-complement integers can Overflow
3109 * so it will NOT work.
3111 * For == and != can be handled in Proj(Cmp)
3113 static ir_node *transform_node_Cmp(ir_node *n) {
3115 ir_node *left = get_Cmp_left(n);
3116 ir_node *right = get_Cmp_right(n);
3118 if (is_Minus(left) && is_Minus(right) &&
3119 !mode_overflow_on_unary_Minus(get_irn_mode(left))) {
3120 left = get_Minus_op(left);
3121 right = get_Minus_op(right);
3122 n = new_rd_Cmp(get_irn_dbg_info(n), current_ir_graph,
3123 get_nodes_block(n), left, right);
3124 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CMP_OP_OP);
3127 } /* transform_node_Cmp */
3131 * Transform a Cond node.
3133 * Replace the Cond by a Jmp if it branches on a constant
3136 static ir_node *transform_node_Cond(ir_node *n) {
3139 ir_node *a = get_Cond_selector(n);
3140 tarval *ta = value_of(a);
3142 /* we need block info which is not available in floating irgs */
3143 if (get_irg_pinned(current_ir_graph) == op_pin_state_floats)
3146 if ((ta != tarval_bad) &&
3147 (get_irn_mode(a) == mode_b) &&
3148 (get_opt_unreachable_code())) {
3149 /* It's a boolean Cond, branching on a boolean constant.
3150 Replace it by a tuple (Bad, Jmp) or (Jmp, Bad) */
3151 ir_node *blk = get_nodes_block(n);
3152 jmp = new_r_Jmp(current_ir_graph, blk);
3153 turn_into_tuple(n, pn_Cond_max);
3154 if (ta == tarval_b_true) {
3155 set_Tuple_pred(n, pn_Cond_false, new_Bad());
3156 set_Tuple_pred(n, pn_Cond_true, jmp);
3158 set_Tuple_pred(n, pn_Cond_false, jmp);
3159 set_Tuple_pred(n, pn_Cond_true, new_Bad());
3161 /* We might generate an endless loop, so keep it alive. */
3162 add_End_keepalive(get_irg_end(current_ir_graph), blk);
3165 } /* transform_node_Cond */
3168 * Prototype of a recursive transform function
3169 * for bitwise distributive transformations.
3171 typedef ir_node* (*recursive_transform)(ir_node *n);
3174 * makes use of distributive laws for and, or, eor
3175 * and(a OP c, b OP c) -> and(a, b) OP c
3176 * note, might return a different op than n
3178 static ir_node *transform_bitwise_distributive(ir_node *n,
3179 recursive_transform trans_func)
3182 ir_node *a = get_binop_left(n);
3183 ir_node *b = get_binop_right(n);
3184 ir_op *op = get_irn_op(a);
3185 ir_op *op_root = get_irn_op(n);
3187 if(op != get_irn_op(b))
3190 if (op == op_Conv) {
3191 ir_node *a_op = get_Conv_op(a);
3192 ir_node *b_op = get_Conv_op(b);
3193 ir_mode *a_mode = get_irn_mode(a_op);
3194 ir_mode *b_mode = get_irn_mode(b_op);
3195 if(a_mode == b_mode && (mode_is_int(a_mode) || a_mode == mode_b)) {
3196 ir_node *blk = get_irn_n(n, -1);
3199 set_binop_left(n, a_op);
3200 set_binop_right(n, b_op);
3201 set_irn_mode(n, a_mode);
3203 n = new_r_Conv(current_ir_graph, blk, n, get_irn_mode(oldn));
3205 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_SHIFT_AND);
3211 /* nothing to gain here */
3215 if (op == op_Shrs || op == op_Shr || op == op_Shl
3216 || op == op_And || op == op_Or || op == op_Eor) {
3217 ir_node *a_left = get_binop_left(a);
3218 ir_node *a_right = get_binop_right(a);
3219 ir_node *b_left = get_binop_left(b);
3220 ir_node *b_right = get_binop_right(b);
3222 ir_node *op1 = NULL;
3223 ir_node *op2 = NULL;
3225 if (is_op_commutative(op)) {
3226 if (a_left == b_left) {
3230 } else if(a_left == b_right) {
3234 } else if(a_right == b_left) {
3240 if(a_right == b_right) {
3247 /* (a sop c) & (b sop c) => (a & b) sop c */
3248 ir_node *blk = get_irn_n(n, -1);
3250 ir_node *new_n = exact_copy(n);
3251 set_binop_left(new_n, op1);
3252 set_binop_right(new_n, op2);
3253 new_n = trans_func(new_n);
3255 if(op_root == op_Eor && op == op_Or) {
3256 dbg_info *dbgi = get_irn_dbg_info(n);
3257 ir_graph *irg = current_ir_graph;
3258 ir_mode *mode = get_irn_mode(c);
3260 c = new_rd_Not(dbgi, irg, blk, c, mode);
3261 n = new_rd_And(dbgi, irg, blk, new_n, c, mode);
3264 set_nodes_block(n, blk);
3265 set_binop_left(n, new_n);
3266 set_binop_right(n, c);
3267 add_identities(current_ir_graph->value_table, n);
3270 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_SHIFT_AND);
3281 static ir_node *transform_node_And(ir_node *n) {
3282 ir_node *c, *oldn = n;
3283 ir_node *a = get_And_left(n);
3284 ir_node *b = get_And_right(n);
3287 mode = get_irn_mode(n);
3288 HANDLE_BINOP_PHI(tarval_and, a, b, c, mode);
3290 /* we can evaluate 2 Projs of the same Cmp */
3291 if (mode == mode_b && is_Proj(a) && is_Proj(b)) {
3292 ir_node *pred_a = get_Proj_pred(a);
3293 ir_node *pred_b = get_Proj_pred(b);
3294 if (pred_a == pred_b) {
3295 dbg_info *dbgi = get_irn_dbg_info(n);
3296 ir_node *block = get_nodes_block(pred_a);
3297 pn_Cmp pn_a = get_Proj_proj(a);
3298 pn_Cmp pn_b = get_Proj_proj(b);
3299 /* yes, we can simply calculate with pncs */
3300 pn_Cmp new_pnc = pn_a & pn_b;
3302 return new_rd_Proj(dbgi, current_ir_graph, block, pred_a, mode_b, new_pnc);
3307 ir_node *op = get_Not_op(b);
3309 ir_node *ba = get_And_left(op);
3310 ir_node *bb = get_And_right(op);
3312 /* it's enough to test the following cases due to normalization! */
3313 if (get_Or_left(a) == ba && get_Or_right(a) == bb) {
3314 /* (a|b) & ~(a&b) = a^b */
3315 ir_node *block = get_nodes_block(n);
3317 n = new_rd_Eor(get_irn_dbg_info(n), current_ir_graph, block, ba, bb, mode);
3318 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_TO_EOR);
3326 ir_node *op = get_Not_op(a);
3328 ir_node *aa = get_And_left(op);
3329 ir_node *ab = get_And_right(op);
3331 /* it's enough to test the following cases due to normalization! */
3332 if (get_Or_left(b) == aa && get_Or_right(b) == ab) {
3333 /* (a|b) & ~(a&b) = a^b */
3334 ir_node *block = get_nodes_block(n);
3336 n = new_rd_Eor(get_irn_dbg_info(n), current_ir_graph, block, aa, ab, mode);
3337 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_TO_EOR);
3344 ir_node *al = get_Eor_left(a);
3345 ir_node *ar = get_Eor_right(a);
3348 /* (b ^ a) & b -> ~a & b */
3349 dbg_info *dbg = get_irn_dbg_info(n);
3350 ir_node *block = get_nodes_block(n);
3352 ar = new_rd_Not(dbg, current_ir_graph, block, ar, mode);
3353 n = new_rd_And(dbg, current_ir_graph, block, ar, b, mode);
3354 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3358 /* (a ^ b) & b -> ~a & b */
3359 dbg_info *dbg = get_irn_dbg_info(n);
3360 ir_node *block = get_nodes_block(n);
3362 al = new_rd_Not(dbg, current_ir_graph, block, al, mode);
3363 n = new_rd_And(dbg, current_ir_graph, block, al, b, mode);
3364 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3369 ir_node *bl = get_Eor_left(b);
3370 ir_node *br = get_Eor_right(b);
3373 /* a & (a ^ b) -> a & ~b */
3374 dbg_info *dbg = get_irn_dbg_info(n);
3375 ir_node *block = get_nodes_block(n);
3377 br = new_rd_Not(dbg, current_ir_graph, block, br, mode);
3378 n = new_rd_And(dbg, current_ir_graph, block, br, a, mode);
3379 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3383 /* a & (b ^ a) -> a & ~b */
3384 dbg_info *dbg = get_irn_dbg_info(n);
3385 ir_node *block = get_nodes_block(n);
3387 bl = new_rd_Not(dbg, current_ir_graph, block, bl, mode);
3388 n = new_rd_And(dbg, current_ir_graph, block, bl, a, mode);
3389 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3393 if (is_Not(a) && is_Not(b)) {
3394 /* ~a & ~b = ~(a|b) */
3395 ir_node *block = get_nodes_block(n);
3396 ir_mode *mode = get_irn_mode(n);
3400 n = new_rd_Or(get_irn_dbg_info(n), current_ir_graph, block, a, b, mode);
3401 n = new_rd_Not(get_irn_dbg_info(n), current_ir_graph, block, n, mode);
3402 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_DEMORGAN);
3406 n = transform_bitwise_distributive(n, transform_node_And);
3409 } /* transform_node_And */
3414 static ir_node *transform_node_Eor(ir_node *n) {
3415 ir_node *c, *oldn = n;
3416 ir_node *a = get_Eor_left(n);
3417 ir_node *b = get_Eor_right(n);
3418 ir_mode *mode = get_irn_mode(n);
3420 HANDLE_BINOP_PHI(tarval_eor, a, b, c, mode);
3422 /* we can evaluate 2 Projs of the same Cmp */
3423 if (mode == mode_b && is_Proj(a) && is_Proj(b)) {
3424 ir_node *pred_a = get_Proj_pred(a);
3425 ir_node *pred_b = get_Proj_pred(b);
3426 if(pred_a == pred_b) {
3427 dbg_info *dbgi = get_irn_dbg_info(n);
3428 ir_node *block = get_nodes_block(pred_a);
3429 pn_Cmp pn_a = get_Proj_proj(a);
3430 pn_Cmp pn_b = get_Proj_proj(b);
3431 /* yes, we can simply calculate with pncs */
3432 pn_Cmp new_pnc = pn_a ^ pn_b;
3434 return new_rd_Proj(dbgi, current_ir_graph, block, pred_a, mode_b,
3441 n = new_rd_Const(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1),
3442 mode, get_mode_null(mode));
3443 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_A_A);
3444 } else if (mode == mode_b &&
3446 is_Const(b) && is_Const_one(b) &&
3447 is_Cmp(get_Proj_pred(a))) {
3448 /* The Eor negates a Cmp. The Cmp has the negated result anyways! */
3449 n = new_r_Proj(current_ir_graph, get_irn_n(n, -1), get_Proj_pred(a),
3450 mode_b, get_negated_pnc(get_Proj_proj(a), mode));
3452 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT_BOOL);
3453 } else if (is_Const(b)) {
3454 if (is_Not(a)) { /* ~x ^ const -> x ^ ~const */
3455 ir_node *cnst = new_Const(mode, tarval_not(get_Const_tarval(b)));
3456 ir_node *not_op = get_Not_op(a);
3457 dbg_info *dbg = get_irn_dbg_info(n);
3458 ir_graph *irg = current_ir_graph;
3459 ir_node *block = get_nodes_block(n);
3460 ir_mode *mode = get_irn_mode(n);
3461 n = new_rd_Eor(dbg, irg, block, not_op, cnst, mode);
3463 } else if (is_Const_all_one(b)) { /* x ^ 1...1 -> ~1 */
3464 n = new_r_Not(current_ir_graph, get_nodes_block(n), a, mode);
3465 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3468 n = transform_bitwise_distributive(n, transform_node_Eor);
3472 } /* transform_node_Eor */
3477 static ir_node *transform_node_Not(ir_node *n) {
3478 ir_node *c, *oldn = n;
3479 ir_node *a = get_Not_op(n);
3480 ir_mode *mode = get_irn_mode(n);
3482 HANDLE_UNOP_PHI(tarval_not,a,c);
3484 /* check for a boolean Not */
3485 if (mode == mode_b &&
3487 is_Cmp(get_Proj_pred(a))) {
3488 /* We negate a Cmp. The Cmp has the negated result anyways! */
3489 n = new_r_Proj(current_ir_graph, get_irn_n(n, -1), get_Proj_pred(a),
3490 mode_b, get_negated_pnc(get_Proj_proj(a), mode_b));
3491 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_CMP);
3495 ir_node *eor_b = get_Eor_right(a);
3496 if (is_Const(eor_b)) { /* ~(x ^ const) -> x ^ ~const */
3497 ir_node *cnst = new_Const(mode, tarval_not(get_Const_tarval(eor_b)));
3498 ir_node *eor_a = get_Eor_left(a);
3499 dbg_info *dbg = get_irn_dbg_info(n);
3500 ir_graph *irg = current_ir_graph;
3501 ir_node *block = get_nodes_block(n);
3502 ir_mode *mode = get_irn_mode(n);
3503 n = new_rd_Eor(dbg, irg, block, eor_a, cnst, mode);
3507 if (get_mode_arithmetic(mode) == irma_twos_complement) {
3508 if (is_Minus(a)) { /* ~-x -> x + -1 */
3509 dbg_info *dbg = get_irn_dbg_info(n);
3510 ir_graph *irg = current_ir_graph;
3511 ir_node *block = get_nodes_block(n);
3512 ir_node *add_l = get_Minus_op(a);
3513 ir_node *add_r = new_rd_Const(dbg, irg, block, mode, get_mode_minus_one(mode));
3514 n = new_rd_Add(dbg, irg, block, add_l, add_r, mode);
3515 } else if (is_Add(a)) {
3516 ir_node *add_r = get_Add_right(a);
3517 if (is_Const(add_r) && is_Const_all_one(add_r)) {
3518 /* ~(x + -1) = -x */
3519 ir_node *op = get_Add_left(a);
3520 ir_node *blk = get_irn_n(n, -1);
3521 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, blk, op, get_irn_mode(n));
3522 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_MINUS_1);
3527 } /* transform_node_Not */
3530 * Transform a Minus.
3534 * -(a >>u (size-1)) = a >>s (size-1)
3535 * -(a >>s (size-1)) = a >>u (size-1)
3536 * -(a * const) -> a * -const
3538 static ir_node *transform_node_Minus(ir_node *n) {
3539 ir_node *c, *oldn = n;
3540 ir_node *a = get_Minus_op(n);
3543 HANDLE_UNOP_PHI(tarval_neg,a,c);
3545 mode = get_irn_mode(a);
3546 if (get_mode_arithmetic(mode) == irma_twos_complement) {
3547 /* the following rules are only to twos-complement */
3550 ir_node *op = get_Not_op(a);
3551 tarval *tv = get_mode_one(mode);
3552 ir_node *blk = get_irn_n(n, -1);
3553 ir_node *c = new_r_Const(current_ir_graph, blk, mode, tv);
3554 n = new_rd_Add(get_irn_dbg_info(n), current_ir_graph, blk, op, c, mode);
3555 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_MINUS_NOT);
3559 ir_node *c = get_Shr_right(a);
3562 tarval *tv = get_Const_tarval(c);
3564 if (tarval_is_long(tv) && get_tarval_long(tv) == (int) get_mode_size_bits(mode) - 1) {
3565 /* -(a >>u (size-1)) = a >>s (size-1) */
3566 ir_node *v = get_Shr_left(a);
3568 n = new_rd_Shrs(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1), v, c, mode);
3569 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_PREDICATE);
3575 ir_node *c = get_Shrs_right(a);
3578 tarval *tv = get_Const_tarval(c);
3580 if (tarval_is_long(tv) && get_tarval_long(tv) == (int) get_mode_size_bits(mode) - 1) {
3581 /* -(a >>s (size-1)) = a >>u (size-1) */
3582 ir_node *v = get_Shrs_left(a);
3584 n = new_rd_Shr(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1), v, c, mode);
3585 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_PREDICATE);
3592 /* - (a-b) = b - a */
3593 ir_node *la = get_Sub_left(a);
3594 ir_node *ra = get_Sub_right(a);
3595 ir_node *blk = get_irn_n(n, -1);
3597 n = new_rd_Sub(get_irn_dbg_info(n), current_ir_graph, blk, ra, la, mode);
3598 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_MINUS_SUB);
3602 if (is_Mul(a)) { /* -(a * const) -> a * -const */
3603 ir_node *mul_l = get_Mul_left(a);
3604 ir_node *mul_r = get_Mul_right(a);
3605 if (is_Const(mul_r)) {
3606 tarval *tv = tarval_neg(get_Const_tarval(mul_r));
3607 if(tv != tarval_bad) {
3608 ir_node *cnst = new_Const(mode, tv);
3609 dbg_info *dbg = get_irn_dbg_info(a);
3610 ir_graph *irg = current_ir_graph;
3611 ir_node *block = get_nodes_block(a);
3612 n = new_rd_Mul(dbg, irg, block, mul_l, cnst, mode);
3613 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_MINUS_MUL_C);
3620 } /* transform_node_Minus */
3623 * Transform a Cast_type(Const) into a new Const_type
3625 static ir_node *transform_node_Cast(ir_node *n) {
3627 ir_node *pred = get_Cast_op(n);
3628 ir_type *tp = get_irn_type(n);
3630 if (is_Const(pred) && get_Const_type(pred) != tp) {
3631 n = new_rd_Const_type(NULL, current_ir_graph, get_irn_n(pred, -1), get_irn_mode(pred),
3632 get_Const_tarval(pred), tp);
3633 DBG_OPT_CSTEVAL(oldn, n);
3634 } else if (is_SymConst(pred) && get_SymConst_value_type(pred) != tp) {
3635 n = new_rd_SymConst_type(NULL, current_ir_graph, get_irn_n(pred, -1), get_irn_mode(pred),
3636 get_SymConst_symbol(pred), get_SymConst_kind(pred), tp);
3637 DBG_OPT_CSTEVAL(oldn, n);
3641 } /* transform_node_Cast */
3644 * Transform a Proj(Load) with a non-null address.
3646 static ir_node *transform_node_Proj_Load(ir_node *proj) {
3647 if (get_opt_ldst_only_null_ptr_exceptions()) {
3648 if (get_irn_mode(proj) == mode_X) {
3649 ir_node *load = get_Proj_pred(proj);
3651 /* get the Load address */
3652 const ir_node *addr = get_Load_ptr(load);
3653 const ir_node *confirm;
3655 if (value_not_null(addr, &confirm)) {
3656 if (confirm == NULL) {
3657 /* this node may float if it did not depend on a Confirm */
3658 set_irn_pinned(load, op_pin_state_floats);
3660 if (get_Proj_proj(proj) == pn_Load_X_except) {
3661 DBG_OPT_EXC_REM(proj);
3662 return get_irg_bad(current_ir_graph);
3664 ir_node *blk = get_nodes_block(load);
3665 return new_r_Jmp(current_ir_graph, blk);
3671 } /* transform_node_Proj_Load */
3674 * Transform a Proj(Store) with a non-null address.
3676 static ir_node *transform_node_Proj_Store(ir_node *proj) {
3677 if (get_opt_ldst_only_null_ptr_exceptions()) {
3678 if (get_irn_mode(proj) == mode_X) {
3679 ir_node *store = get_Proj_pred(proj);
3681 /* get the load/store address */
3682 const ir_node *addr = get_Store_ptr(store);
3683 const ir_node *confirm;
3685 if (value_not_null(addr, &confirm)) {
3686 if (confirm == NULL) {
3687 /* this node may float if it did not depend on a Confirm */
3688 set_irn_pinned(store, op_pin_state_floats);
3690 if (get_Proj_proj(proj) == pn_Store_X_except) {
3691 DBG_OPT_EXC_REM(proj);
3692 return get_irg_bad(current_ir_graph);
3694 ir_node *blk = get_nodes_block(store);
3695 return new_r_Jmp(current_ir_graph, blk);
3701 } /* transform_node_Proj_Store */
3704 * Transform a Proj(Div) with a non-zero value.
3705 * Removes the exceptions and routes the memory to the NoMem node.
3707 static ir_node *transform_node_Proj_Div(ir_node *proj) {
3708 ir_node *div = get_Proj_pred(proj);
3709 ir_node *b = get_Div_right(div);
3710 ir_node *res, *new_mem;
3711 const ir_node *confirm;
3714 if (value_not_zero(b, &confirm)) {
3715 /* div(x, y) && y != 0 */
3716 if (confirm == NULL) {
3717 /* we are sure we have a Const != 0 */
3718 new_mem = get_Div_mem(div);
3719 if (is_Pin(new_mem))
3720 new_mem = get_Pin_op(new_mem);
3721 set_Div_mem(div, new_mem);
3722 set_irn_pinned(div, op_pin_state_floats);
3725 proj_nr = get_Proj_proj(proj);
3727 case pn_Div_X_regular:
3728 return new_r_Jmp(current_ir_graph, get_irn_n(div, -1));
3730 case pn_Div_X_except:
3731 /* we found an exception handler, remove it */
3732 DBG_OPT_EXC_REM(proj);
3736 res = get_Div_mem(div);
3737 new_mem = get_irg_no_mem(current_ir_graph);
3740 /* This node can only float up to the Confirm block */
3741 new_mem = new_r_Pin(current_ir_graph, get_nodes_block(confirm), new_mem);
3743 set_irn_pinned(div, op_pin_state_floats);
3744 /* this is a Div without exception, we can remove the memory edge */
3745 set_Div_mem(div, new_mem);
3750 } /* transform_node_Proj_Div */
3753 * Transform a Proj(Mod) with a non-zero value.
3754 * Removes the exceptions and routes the memory to the NoMem node.
3756 static ir_node *transform_node_Proj_Mod(ir_node *proj) {
3757 ir_node *mod = get_Proj_pred(proj);
3758 ir_node *b = get_Mod_right(mod);
3759 ir_node *res, *new_mem;
3760 const ir_node *confirm;
3763 if (value_not_zero(b, &confirm)) {
3764 /* mod(x, y) && y != 0 */
3765 proj_nr = get_Proj_proj(proj);
3767 if (confirm == NULL) {
3768 /* we are sure we have a Const != 0 */
3769 new_mem = get_Mod_mem(mod);
3770 if (is_Pin(new_mem))
3771 new_mem = get_Pin_op(new_mem);
3772 set_Mod_mem(mod, new_mem);
3773 set_irn_pinned(mod, op_pin_state_floats);
3778 case pn_Mod_X_regular:
3779 return new_r_Jmp(current_ir_graph, get_irn_n(mod, -1));
3781 case pn_Mod_X_except:
3782 /* we found an exception handler, remove it */
3783 DBG_OPT_EXC_REM(proj);
3787 res = get_Mod_mem(mod);
3788 new_mem = get_irg_no_mem(current_ir_graph);
3791 /* This node can only float up to the Confirm block */
3792 new_mem = new_r_Pin(current_ir_graph, get_nodes_block(confirm), new_mem);
3794 /* this is a Mod without exception, we can remove the memory edge */
3795 set_Mod_mem(mod, new_mem);
3798 if (get_Mod_left(mod) == b) {
3799 /* a % a = 0 if a != 0 */
3800 ir_mode *mode = get_irn_mode(proj);
3801 ir_node *res = new_Const(mode, get_mode_null(mode));
3803 DBG_OPT_CSTEVAL(mod, res);
3809 } /* transform_node_Proj_Mod */
3812 * Transform a Proj(DivMod) with a non-zero value.
3813 * Removes the exceptions and routes the memory to the NoMem node.
3815 static ir_node *transform_node_Proj_DivMod(ir_node *proj) {
3816 ir_node *divmod = get_Proj_pred(proj);
3817 ir_node *b = get_DivMod_right(divmod);
3818 ir_node *res, *new_mem;
3819 const ir_node *confirm;
3822 if (value_not_zero(b, &confirm)) {
3823 /* DivMod(x, y) && y != 0 */
3824 proj_nr = get_Proj_proj(proj);
3826 if (confirm == NULL) {
3827 /* we are sure we have a Const != 0 */
3828 new_mem = get_DivMod_mem(divmod);
3829 if (is_Pin(new_mem))
3830 new_mem = get_Pin_op(new_mem);
3831 set_DivMod_mem(divmod, new_mem);
3832 set_irn_pinned(divmod, op_pin_state_floats);
3837 case pn_DivMod_X_regular:
3838 return new_r_Jmp(current_ir_graph, get_irn_n(divmod, -1));
3840 case pn_DivMod_X_except:
3841 /* we found an exception handler, remove it */
3842 DBG_OPT_EXC_REM(proj);
3846 res = get_DivMod_mem(divmod);
3847 new_mem = get_irg_no_mem(current_ir_graph);
3850 /* This node can only float up to the Confirm block */
3851 new_mem = new_r_Pin(current_ir_graph, get_nodes_block(confirm), new_mem);
3853 /* this is a DivMod without exception, we can remove the memory edge */
3854 set_DivMod_mem(divmod, new_mem);
3857 case pn_DivMod_res_mod:
3858 if (get_DivMod_left(divmod) == b) {
3859 /* a % a = 0 if a != 0 */
3860 ir_mode *mode = get_irn_mode(proj);
3861 ir_node *res = new_Const(mode, get_mode_null(mode));
3863 DBG_OPT_CSTEVAL(divmod, res);
3869 } /* transform_node_Proj_DivMod */
3872 * Optimizes jump tables (CondIs or CondIu) by removing all impossible cases.
3874 static ir_node *transform_node_Proj_Cond(ir_node *proj) {
3875 if (get_opt_unreachable_code()) {
3876 ir_node *n = get_Proj_pred(proj);
3877 ir_node *b = get_Cond_selector(n);
3879 if (mode_is_int(get_irn_mode(b))) {
3880 tarval *tb = value_of(b);
3882 if (tb != tarval_bad) {
3883 /* we have a constant switch */
3884 long num = get_Proj_proj(proj);
3886 if (num != get_Cond_defaultProj(n)) { /* we cannot optimize default Proj's yet */
3887 if (get_tarval_long(tb) == num) {
3888 /* Do NOT create a jump here, or we will have 2 control flow ops
3889 * in a block. This case is optimized away in optimize_cf(). */
3892 /* this case will NEVER be taken, kill it */
3893 return get_irg_bad(current_ir_graph);
3900 } /* transform_node_Proj_Cond */
3903 * Create a 0 constant of given mode.
3905 static ir_node *create_zero_const(ir_mode *mode) {
3906 tarval *tv = get_mode_null(mode);
3907 ir_node *cnst = new_Const(mode, tv);
3912 /* the order of the values is important! */
3913 typedef enum const_class {
3919 static const_class classify_const(const ir_node* n)
3921 if (is_Const(n)) return const_const;
3922 if (is_irn_constlike(n)) return const_like;
3927 * Determines whether r is more constlike or has a larger index (in that order)
3930 static int operands_are_normalized(const ir_node *l, const ir_node *r)
3932 const const_class l_order = classify_const(l);
3933 const const_class r_order = classify_const(r);
3935 l_order > r_order ||
3936 (l_order == r_order && get_irn_idx(l) <= get_irn_idx(r));
3940 * Normalizes and optimizes Cmp nodes.
3942 static ir_node *transform_node_Proj_Cmp(ir_node *proj) {
3943 ir_node *n = get_Proj_pred(proj);
3944 ir_node *left = get_Cmp_left(n);
3945 ir_node *right = get_Cmp_right(n);
3948 ir_mode *mode = NULL;
3949 long proj_nr = get_Proj_proj(proj);
3951 /* we can evaluate some cases directly */
3954 return new_Const(mode_b, get_tarval_b_false());
3956 return new_Const(mode_b, get_tarval_b_true());
3958 if (!mode_is_float(get_irn_mode(left)))
3959 return new_Const(mode_b, get_tarval_b_true());
3965 /* remove Casts of both sides */
3966 left = skip_Cast(left);
3967 right = skip_Cast(right);
3969 /* Remove unnecessary conversions */
3970 /* TODO handle constants */
3971 if (is_Conv(left) && is_Conv(right)) {
3972 ir_mode *mode = get_irn_mode(left);
3973 ir_node *op_left = get_Conv_op(left);
3974 ir_node *op_right = get_Conv_op(right);
3975 ir_mode *mode_left = get_irn_mode(op_left);
3976 ir_mode *mode_right = get_irn_mode(op_right);
3978 if (smaller_mode(mode_left, mode) && smaller_mode(mode_right, mode)
3979 && mode_left != mode_b && mode_right != mode_b) {
3980 ir_graph *irg = current_ir_graph;
3981 ir_node *block = get_nodes_block(n);
3983 if (mode_left == mode_right) {
3987 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV_CONV);
3988 } else if (smaller_mode(mode_left, mode_right)) {
3989 left = new_r_Conv(irg, block, op_left, mode_right);
3992 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV);
3993 } else if (smaller_mode(mode_right, mode_left)) {
3995 right = new_r_Conv(irg, block, op_right, mode_left);
3997 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV);
4002 /* remove operation on both sides if possible */
4003 if (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) {
4005 * The following operations are NOT safe for floating point operations, for instance
4006 * 1.0 + inf == 2.0 + inf, =/=> x == y
4008 if (mode_is_int(get_irn_mode(left))) {
4009 unsigned lop = get_irn_opcode(left);
4011 if (lop == get_irn_opcode(right)) {
4012 ir_node *ll, *lr, *rl, *rr;
4014 /* same operation on both sides, try to remove */
4018 /* ~a CMP ~b => a CMP b, -a CMP -b ==> a CMP b */
4019 left = get_unop_op(left);
4020 right = get_unop_op(right);
4022 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4025 ll = get_Add_left(left);
4026 lr = get_Add_right(left);
4027 rl = get_Add_left(right);
4028 rr = get_Add_right(right);
4031 /* X + a CMP X + b ==> a CMP b */
4035 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4036 } else if (ll == rr) {
4037 /* X + a CMP b + X ==> a CMP b */
4041 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4042 } else if (lr == rl) {
4043 /* a + X CMP X + b ==> a CMP b */
4047 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4048 } else if (lr == rr) {
4049 /* a + X CMP b + X ==> a CMP b */
4053 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4057 ll = get_Sub_left(left);
4058 lr = get_Sub_right(left);
4059 rl = get_Sub_left(right);
4060 rr = get_Sub_right(right);
4063 /* X - a CMP X - b ==> a CMP b */
4067 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4068 } else if (lr == rr) {
4069 /* a - X CMP b - X ==> a CMP b */
4073 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4077 if (get_Rotl_right(left) == get_Rotl_right(right)) {
4078 /* a ROTL X CMP b ROTL X ==> a CMP b */
4079 left = get_Rotl_left(left);
4080 right = get_Rotl_left(right);
4082 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4090 /* X+A == A, A+X == A, A-X == A -> X == 0 */
4091 if (is_Add(left) || is_Sub(left)) {
4092 ir_node *ll = get_binop_left(left);
4093 ir_node *lr = get_binop_right(left);
4095 if (lr == right && is_Add(left)) {
4102 right = create_zero_const(get_irn_mode(left));
4104 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4107 if (is_Add(right) || is_Sub(right)) {
4108 ir_node *rl = get_binop_left(right);
4109 ir_node *rr = get_binop_right(right);
4111 if (rr == left && is_Add(right)) {
4118 right = create_zero_const(get_irn_mode(left));
4120 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4123 } /* mode_is_int(...) */
4124 } /* proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg */
4126 /* replace mode_b compares with ands/ors */
4127 if (get_irn_mode(left) == mode_b) {
4128 ir_graph *irg = current_ir_graph;
4129 ir_node *block = get_nodes_block(n);
4133 case pn_Cmp_Le: bres = new_r_Or( irg, block, new_r_Not(irg, block, left, mode_b), right, mode_b); break;
4134 case pn_Cmp_Lt: bres = new_r_And(irg, block, new_r_Not(irg, block, left, mode_b), right, mode_b); break;
4135 case pn_Cmp_Ge: bres = new_r_Or( irg, block, left, new_r_Not(irg, block, right, mode_b), mode_b); break;
4136 case pn_Cmp_Gt: bres = new_r_And(irg, block, left, new_r_Not(irg, block, right, mode_b), mode_b); break;
4137 case pn_Cmp_Lg: bres = new_r_Eor(irg, block, left, right, mode_b); break;
4138 case pn_Cmp_Eq: bres = new_r_Not(irg, block, new_r_Eor(irg, block, left, right, mode_b), mode_b); break;
4139 default: bres = NULL;
4142 DBG_OPT_ALGSIM0(n, bres, FS_OPT_CMP_TO_BOOL);
4148 * First step: normalize the compare op
4149 * by placing the constant on the right side
4150 * or moving the lower address node to the left.
4152 if (!operands_are_normalized(left, right)) {
4158 proj_nr = get_inversed_pnc(proj_nr);
4163 * Second step: Try to reduce the magnitude
4164 * of a constant. This may help to generate better code
4165 * later and may help to normalize more compares.
4166 * Of course this is only possible for integer values.
4168 if (is_Const(right)) {
4169 mode = get_irn_mode(right);
4170 tv = get_Const_tarval(right);
4172 /* TODO extend to arbitrary constants */
4173 if (is_Conv(left) && tarval_is_null(tv)) {
4174 ir_node *op = get_Conv_op(left);
4175 ir_mode *op_mode = get_irn_mode(op);
4178 * UpConv(x) REL 0 ==> x REL 0
4180 if (get_mode_size_bits(mode) > get_mode_size_bits(op_mode) &&
4181 ((proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) ||
4182 mode_is_signed(mode) || !mode_is_signed(op_mode))) {
4183 tv = get_mode_null(op_mode);
4187 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV);
4191 if (tv != tarval_bad) {
4192 /* the following optimization is possible on modes without Overflow
4193 * on Unary Minus or on == and !=:
4194 * -a CMP c ==> a swap(CMP) -c
4196 * Beware: for two-complement Overflow may occur, so only == and != can
4197 * be optimized, see this:
4198 * -MININT < 0 =/=> MININT > 0 !!!
4200 if (is_Minus(left) &&
4201 (!mode_overflow_on_unary_Minus(mode) ||
4202 (mode_is_int(mode) && (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg)))) {
4203 tv = tarval_neg(tv);
4205 if (tv != tarval_bad) {
4206 left = get_Minus_op(left);
4207 proj_nr = get_inversed_pnc(proj_nr);
4209 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4211 } else if (is_Not(left) && (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg)) {
4212 /* Not(a) ==/!= c ==> a ==/!= Not(c) */
4213 tv = tarval_not(tv);
4215 if (tv != tarval_bad) {
4216 left = get_Not_op(left);
4218 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4222 /* for integer modes, we have more */
4223 if (mode_is_int(mode)) {
4224 /* Ne includes Unordered which is not possible on integers.
4225 * However, frontends often use this wrong, so fix it here */
4226 if (proj_nr & pn_Cmp_Uo) {
4227 proj_nr &= ~pn_Cmp_Uo;
4228 set_Proj_proj(proj, proj_nr);
4231 /* c > 0 : a < c ==> a <= (c-1) a >= c ==> a > (c-1) */
4232 if ((proj_nr == pn_Cmp_Lt || proj_nr == pn_Cmp_Ge) &&
4233 tarval_cmp(tv, get_mode_null(mode)) == pn_Cmp_Gt) {
4234 tv = tarval_sub(tv, get_mode_one(mode), NULL);
4236 if (tv != tarval_bad) {
4237 proj_nr ^= pn_Cmp_Eq;
4239 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CNST_MAGN);
4242 /* c < 0 : a > c ==> a >= (c+1) a <= c ==> a < (c+1) */
4243 else if ((proj_nr == pn_Cmp_Gt || proj_nr == pn_Cmp_Le) &&
4244 tarval_cmp(tv, get_mode_null(mode)) == pn_Cmp_Lt) {
4245 tv = tarval_add(tv, get_mode_one(mode));
4247 if (tv != tarval_bad) {
4248 proj_nr ^= pn_Cmp_Eq;
4250 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CNST_MAGN);
4254 /* the following reassociations work only for == and != */
4255 if (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) {
4257 #if 0 /* Might be not that good in general */
4258 /* a-b == 0 ==> a == b, a-b != 0 ==> a != b */
4259 if (tarval_is_null(tv) && is_Sub(left)) {
4260 right = get_Sub_right(left);
4261 left = get_Sub_left(left);
4263 tv = value_of(right);
4265 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4269 if (tv != tarval_bad) {
4270 /* a-c1 == c2 ==> a == c2+c1, a-c1 != c2 ==> a != c2+c1 */
4272 ir_node *c1 = get_Sub_right(left);
4273 tarval *tv2 = value_of(c1);
4275 if (tv2 != tarval_bad) {
4276 tv2 = tarval_add(tv, value_of(c1));
4278 if (tv2 != tarval_bad) {
4279 left = get_Sub_left(left);
4282 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4286 /* a+c1 == c2 ==> a == c2-c1, a+c1 != c2 ==> a != c2-c1 */
4287 else if (is_Add(left)) {
4288 ir_node *a_l = get_Add_left(left);
4289 ir_node *a_r = get_Add_right(left);
4293 if (is_Const(a_l)) {
4295 tv2 = value_of(a_l);
4298 tv2 = value_of(a_r);
4301 if (tv2 != tarval_bad) {
4302 tv2 = tarval_sub(tv, tv2, NULL);
4304 if (tv2 != tarval_bad) {
4308 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4312 /* -a == c ==> a == -c, -a != c ==> a != -c */
4313 else if (is_Minus(left)) {
4314 tarval *tv2 = tarval_sub(get_mode_null(mode), tv, NULL);
4316 if (tv2 != tarval_bad) {
4317 left = get_Minus_op(left);
4320 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4325 /* the following reassociations work only for <= */
4326 else if (proj_nr == pn_Cmp_Le || proj_nr == pn_Cmp_Lt) {
4327 if (tv != tarval_bad) {
4328 /* c >= 0 : Abs(a) <= c ==> (unsigned)(a + c) <= 2*c */
4329 if (get_irn_op(left) == op_Abs) { // TODO something is missing here
4335 if (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) {
4336 switch (get_irn_opcode(left)) {
4340 c1 = get_And_right(left);
4343 * And(x, C1) == C2 ==> FALSE if C2 & C1 != C2
4344 * And(x, C1) != C2 ==> TRUE if C2 & C1 != C2
4346 tarval *mask = tarval_and(get_Const_tarval(c1), tv);
4348 /* TODO: move to constant evaluation */
4349 tv = proj_nr == pn_Cmp_Eq ? get_tarval_b_false() : get_tarval_b_true();
4350 c1 = new_Const(mode_b, tv);
4351 DBG_OPT_CSTEVAL(proj, c1);
4355 if (tarval_is_single_bit(tv)) {
4357 * optimization for AND:
4359 * And(x, C) == C ==> And(x, C) != 0
4360 * And(x, C) != C ==> And(X, C) == 0
4362 * if C is a single Bit constant.
4365 /* check for Constant's match. We have check hare the tarvals,
4366 because our const might be changed */
4367 if (get_Const_tarval(c1) == tv) {
4368 /* fine: do the transformation */
4369 tv = get_mode_null(get_tarval_mode(tv));
4370 proj_nr ^= pn_Cmp_Leg;
4372 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CNST_MAGN);
4378 c1 = get_Or_right(left);
4379 if (is_Const(c1) && tarval_is_null(tv)) {
4381 * Or(x, C) == 0 && C != 0 ==> FALSE
4382 * Or(x, C) != 0 && C != 0 ==> TRUE
4384 if (! tarval_is_null(get_Const_tarval(c1))) {
4385 /* TODO: move to constant evaluation */
4386 tv = proj_nr == pn_Cmp_Eq ? get_tarval_b_false() : get_tarval_b_true();
4387 c1 = new_Const(mode_b, tv);
4388 DBG_OPT_CSTEVAL(proj, c1);
4395 * optimize x << c1 == c into x & (-1 >>u c1) == c >> c1 if c & (-1 << c1) == c
4397 * optimize x << c1 != c into x & (-1 >>u c1) != c >> c1 if c & (-1 << c1) == c
4400 c1 = get_Shl_right(left);
4402 tarval *tv1 = get_Const_tarval(c1);
4403 ir_mode *mode = get_irn_mode(left);
4404 tarval *minus1 = get_mode_all_one(mode);
4405 tarval *amask = tarval_shr(minus1, tv1);
4406 tarval *cmask = tarval_shl(minus1, tv1);
4409 if (tarval_and(tv, cmask) != tv) {
4410 /* condition not met */
4411 tv = proj_nr == pn_Cmp_Eq ? get_tarval_b_false() : get_tarval_b_true();
4412 c1 = new_Const(mode_b, tv);
4413 DBG_OPT_CSTEVAL(proj, c1);
4416 sl = get_Shl_left(left);
4417 blk = get_nodes_block(n);
4418 left = new_rd_And(get_irn_dbg_info(left), current_ir_graph, blk, sl, new_Const(mode, amask), mode);
4419 tv = tarval_shr(tv, tv1);
4421 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_SHF_TO_AND);
4426 * optimize x >>u c1 == c into x & (-1 << c1) == c << c1 if c & (-1 >>u c1) == c
4428 * optimize x >>u c1 != c into x & (-1 << c1) != c << c1 if c & (-1 >>u c1) == c
4431 c1 = get_Shr_right(left);
4433 tarval *tv1 = get_Const_tarval(c1);
4434 ir_mode *mode = get_irn_mode(left);
4435 tarval *minus1 = get_mode_all_one(mode);
4436 tarval *amask = tarval_shl(minus1, tv1);
4437 tarval *cmask = tarval_shr(minus1, tv1);
4440 if (tarval_and(tv, cmask) != tv) {
4441 /* condition not met */
4442 tv = proj_nr == pn_Cmp_Eq ? get_tarval_b_false() : get_tarval_b_true();
4443 c1 = new_Const(mode_b, tv);
4444 DBG_OPT_CSTEVAL(proj, c1);
4447 sl = get_Shr_left(left);
4448 blk = get_nodes_block(n);
4449 left = new_rd_And(get_irn_dbg_info(left), current_ir_graph, blk, sl, new_Const(mode, amask), mode);
4450 tv = tarval_shl(tv, tv1);
4452 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_SHF_TO_AND);
4457 * optimize x >>s c1 == c into x & (-1 << c1) == c << c1 if (c >>s (BITS - c1)) \in {0,-1}
4459 * optimize x >>s c1 != c into x & (-1 << c1) != c << c1 if (c >>s (BITS - c1)) \in {0,-1}
4462 c1 = get_Shrs_right(left);
4464 tarval *tv1 = get_Const_tarval(c1);
4465 ir_mode *mode = get_irn_mode(left);
4466 tarval *minus1 = get_mode_all_one(mode);
4467 tarval *amask = tarval_shl(minus1, tv1);
4468 tarval *cond = new_tarval_from_long(get_mode_size_bits(mode), get_tarval_mode(tv1));
4471 cond = tarval_sub(cond, tv1, NULL);
4472 cond = tarval_shrs(tv, cond);
4474 if (!tarval_is_all_one(cond) && !tarval_is_null(cond)) {
4475 /* condition not met */
4476 tv = proj_nr == pn_Cmp_Eq ? get_tarval_b_false() : get_tarval_b_true();
4477 c1 = new_Const(mode_b, tv);
4478 DBG_OPT_CSTEVAL(proj, c1);
4481 sl = get_Shrs_left(left);
4482 blk = get_nodes_block(n);
4483 left = new_rd_And(get_irn_dbg_info(left), current_ir_graph, blk, sl, new_Const(mode, amask), mode);
4484 tv = tarval_shl(tv, tv1);
4486 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_SHF_TO_AND);
4491 } /* tarval != bad */
4494 if (changed & 2) /* need a new Const */
4495 right = new_Const(mode, tv);
4497 if ((proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) && is_Const(right) && is_Const_null(right) && is_Proj(left)) {
4498 ir_node *op = get_Proj_pred(left);
4500 if ((is_Mod(op) && get_Proj_proj(left) == pn_Mod_res) ||
4501 (is_DivMod(op) && get_Proj_proj(left) == pn_DivMod_res_mod)) {
4502 ir_node *c = get_binop_right(op);
4505 tarval *tv = get_Const_tarval(c);
4507 if (tarval_is_single_bit(tv)) {
4508 /* special case: (x % 2^n) CMP 0 ==> x & (2^n-1) CMP 0 */
4509 ir_node *v = get_binop_left(op);
4510 ir_node *blk = get_irn_n(op, -1);
4511 ir_mode *mode = get_irn_mode(v);
4513 tv = tarval_sub(tv, get_mode_one(mode), NULL);
4514 left = new_rd_And(get_irn_dbg_info(op), current_ir_graph, blk, v, new_Const(mode, tv), mode);
4516 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_MOD_TO_AND);
4523 ir_node *block = get_irn_n(n, -1); /* Beware of get_nodes_Block() */
4525 /* create a new compare */
4526 n = new_rd_Cmp(get_irn_dbg_info(n), current_ir_graph, block, left, right);
4527 proj = new_rd_Proj(get_irn_dbg_info(proj), current_ir_graph, block, n, get_irn_mode(proj), proj_nr);
4531 } /* transform_node_Proj_Cmp */
4534 * Optimize CopyB(mem, x, x) into a Nop.
4536 static ir_node *transform_node_Proj_CopyB(ir_node *proj) {
4537 ir_node *copyb = get_Proj_pred(proj);
4538 ir_node *a = get_CopyB_dst(copyb);
4539 ir_node *b = get_CopyB_src(copyb);
4542 switch (get_Proj_proj(proj)) {
4543 case pn_CopyB_X_regular:
4544 /* Turn CopyB into a tuple (mem, jmp, bad, bad) */
4545 DBG_OPT_EXC_REM(proj);
4546 proj = new_r_Jmp(current_ir_graph, get_nodes_block(copyb));
4548 case pn_CopyB_M_except:
4549 case pn_CopyB_X_except:
4550 DBG_OPT_EXC_REM(proj);
4551 proj = get_irg_bad(current_ir_graph);
4558 } /* transform_node_Proj_CopyB */
4561 * Optimize Bounds(idx, idx, upper) into idx.
4563 static ir_node *transform_node_Proj_Bound(ir_node *proj) {
4564 ir_node *oldn = proj;
4565 ir_node *bound = get_Proj_pred(proj);
4566 ir_node *idx = get_Bound_index(bound);
4567 ir_node *pred = skip_Proj(idx);
4570 if (idx == get_Bound_lower(bound))
4572 else if (is_Bound(pred)) {
4574 * idx was Bounds checked in the same MacroBlock previously,
4575 * it is still valid if lower <= pred_lower && pred_upper <= upper.
4577 ir_node *lower = get_Bound_lower(bound);
4578 ir_node *upper = get_Bound_upper(bound);
4579 if (get_Bound_lower(pred) == lower &&
4580 get_Bound_upper(pred) == upper &&
4581 get_irn_MacroBlock(bound) == get_irn_MacroBlock(pred)) {
4583 * One could expect that we simply return the previous
4584 * Bound here. However, this would be wrong, as we could
4585 * add an exception Proj to a new location then.
4586 * So, we must turn in into a tuple.
4592 /* Turn Bound into a tuple (mem, jmp, bad, idx) */
4593 switch (get_Proj_proj(proj)) {
4595 DBG_OPT_EXC_REM(proj);
4596 proj = get_Bound_mem(bound);
4598 case pn_Bound_X_except:
4599 DBG_OPT_EXC_REM(proj);
4600 proj = get_irg_bad(current_ir_graph);
4604 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NOP);
4606 case pn_Bound_X_regular:
4607 DBG_OPT_EXC_REM(proj);
4608 proj = new_r_Jmp(current_ir_graph, get_nodes_block(bound));
4615 } /* transform_node_Proj_Bound */
4618 * Does all optimizations on nodes that must be done on it's Proj's
4619 * because of creating new nodes.
4621 static ir_node *transform_node_Proj(ir_node *proj) {
4622 ir_node *n = get_Proj_pred(proj);
4624 if (n->op->ops.transform_node_Proj)
4625 return n->op->ops.transform_node_Proj(proj);
4627 } /* transform_node_Proj */
4630 * Move Confirms down through Phi nodes.
4632 static ir_node *transform_node_Phi(ir_node *phi) {
4634 ir_mode *mode = get_irn_mode(phi);
4636 if (mode_is_reference(mode)) {
4637 n = get_irn_arity(phi);
4639 /* Beware of Phi0 */
4641 ir_node *pred = get_irn_n(phi, 0);
4642 ir_node *bound, *new_Phi, *block, **in;
4645 if (! is_Confirm(pred))
4648 bound = get_Confirm_bound(pred);
4649 pnc = get_Confirm_cmp(pred);
4651 NEW_ARR_A(ir_node *, in, n);
4652 in[0] = get_Confirm_value(pred);
4654 for (i = 1; i < n; ++i) {
4655 pred = get_irn_n(phi, i);
4657 if (! is_Confirm(pred) ||
4658 get_Confirm_bound(pred) != bound ||
4659 get_Confirm_cmp(pred) != pnc)
4661 in[i] = get_Confirm_value(pred);
4663 /* move the Confirm nodes "behind" the Phi */
4664 block = get_irn_n(phi, -1);
4665 new_Phi = new_r_Phi(current_ir_graph, block, n, in, get_irn_mode(phi));
4666 return new_r_Confirm(current_ir_graph, block, new_Phi, bound, pnc);
4670 } /* transform_node_Phi */
4673 * Returns the operands of a commutative bin-op, if one operand is
4674 * a const, it is returned as the second one.
4676 static void get_comm_Binop_Ops(ir_node *binop, ir_node **a, ir_node **c) {
4677 ir_node *op_a = get_binop_left(binop);
4678 ir_node *op_b = get_binop_right(binop);
4680 assert(is_op_commutative(get_irn_op(binop)));
4682 if (is_Const(op_a)) {
4689 } /* get_comm_Binop_Ops */
4692 * Optimize a Or(And(Or(And(v,c4),c3),c2),c1) pattern if possible.
4693 * Such pattern may arise in bitfield stores.
4695 * value c4 value c4 & c2
4696 * AND c3 AND c1 | c3
4703 * AND c1 ===> OR if (c1 | c2) == 0x111..11
4706 static ir_node *transform_node_Or_bf_store(ir_node *or) {
4709 ir_node *and_l, *c3;
4710 ir_node *value, *c4;
4711 ir_node *new_and, *new_const, *block;
4712 ir_mode *mode = get_irn_mode(or);
4714 tarval *tv1, *tv2, *tv3, *tv4, *tv, *n_tv4, *n_tv2;
4717 get_comm_Binop_Ops(or, &and, &c1);
4718 if (!is_Const(c1) || !is_And(and))
4721 get_comm_Binop_Ops(and, &or_l, &c2);
4725 tv1 = get_Const_tarval(c1);
4726 tv2 = get_Const_tarval(c2);
4728 tv = tarval_or(tv1, tv2);
4729 if (tarval_is_all_one(tv)) {
4730 /* the AND does NOT clear a bit with isn't set by the OR */
4731 set_Or_left(or, or_l);
4732 set_Or_right(or, c1);
4734 /* check for more */
4741 get_comm_Binop_Ops(or_l, &and_l, &c3);
4742 if (!is_Const(c3) || !is_And(and_l))
4745 get_comm_Binop_Ops(and_l, &value, &c4);
4749 /* ok, found the pattern, check for conditions */
4750 assert(mode == get_irn_mode(and));
4751 assert(mode == get_irn_mode(or_l));
4752 assert(mode == get_irn_mode(and_l));
4754 tv3 = get_Const_tarval(c3);
4755 tv4 = get_Const_tarval(c4);
4757 tv = tarval_or(tv4, tv2);
4758 if (!tarval_is_all_one(tv)) {
4759 /* have at least one 0 at the same bit position */
4763 n_tv4 = tarval_not(tv4);
4764 if (tv3 != tarval_and(tv3, n_tv4)) {
4765 /* bit in the or_mask is outside the and_mask */
4769 n_tv2 = tarval_not(tv2);
4770 if (tv1 != tarval_and(tv1, n_tv2)) {
4771 /* bit in the or_mask is outside the and_mask */
4775 /* ok, all conditions met */
4776 block = get_irn_n(or, -1);
4778 new_and = new_r_And(current_ir_graph, block,
4779 value, new_r_Const(current_ir_graph, block, mode, tarval_and(tv4, tv2)), mode);
4781 new_const = new_r_Const(current_ir_graph, block, mode, tarval_or(tv3, tv1));
4783 set_Or_left(or, new_and);
4784 set_Or_right(or, new_const);
4786 /* check for more */
4788 } /* transform_node_Or_bf_store */
4791 * Optimize an Or(shl(x, c), shr(x, bits - c)) into a Rotl
4793 static ir_node *transform_node_Or_Rotl(ir_node *or) {
4794 ir_mode *mode = get_irn_mode(or);
4795 ir_node *shl, *shr, *block;
4796 ir_node *irn, *x, *c1, *c2, *v, *sub, *n, *rotval;
4799 if (! mode_is_int(mode))
4802 shl = get_binop_left(or);
4803 shr = get_binop_right(or);
4812 } else if (!is_Shl(shl)) {
4814 } else if (!is_Shr(shr)) {
4817 x = get_Shl_left(shl);
4818 if (x != get_Shr_left(shr))
4821 c1 = get_Shl_right(shl);
4822 c2 = get_Shr_right(shr);
4823 if (is_Const(c1) && is_Const(c2)) {
4824 tv1 = get_Const_tarval(c1);
4825 if (! tarval_is_long(tv1))
4828 tv2 = get_Const_tarval(c2);
4829 if (! tarval_is_long(tv2))
4832 if (get_tarval_long(tv1) + get_tarval_long(tv2)
4833 != (int) get_mode_size_bits(mode))
4836 /* yet, condition met */
4837 block = get_nodes_block(or);
4839 n = new_r_Rotl(current_ir_graph, block, x, c1, mode);
4841 DBG_OPT_ALGSIM1(or, shl, shr, n, FS_OPT_OR_SHFT_TO_ROTL);
4848 rotval = sub; /* a Rot right is not supported, so use a rot left */
4849 } else if (is_Sub(c2)) {
4855 if (get_Sub_right(sub) != v)
4858 c1 = get_Sub_left(sub);
4862 tv1 = get_Const_tarval(c1);
4863 if (! tarval_is_long(tv1))
4866 if (get_tarval_long(tv1) != (int) get_mode_size_bits(mode))
4869 /* yet, condition met */
4870 block = get_nodes_block(or);
4872 n = new_r_Rotl(current_ir_graph, block, x, rotval, mode);
4874 DBG_OPT_ALGSIM0(or, n, FS_OPT_OR_SHFT_TO_ROTL);
4876 } /* transform_node_Or_Rotl */
4881 static ir_node *transform_node_Or(ir_node *n) {
4882 ir_node *c, *oldn = n;
4883 ir_node *a = get_Or_left(n);
4884 ir_node *b = get_Or_right(n);
4887 if (is_Not(a) && is_Not(b)) {
4888 /* ~a | ~b = ~(a&b) */
4889 ir_node *block = get_nodes_block(n);
4891 mode = get_irn_mode(n);
4894 n = new_rd_And(get_irn_dbg_info(n), current_ir_graph, block, a, b, mode);
4895 n = new_rd_Not(get_irn_dbg_info(n), current_ir_graph, block, n, mode);
4896 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_DEMORGAN);
4900 /* we can evaluate 2 Projs of the same Cmp */
4901 if (get_irn_mode(n) == mode_b && is_Proj(a) && is_Proj(b)) {
4902 ir_node *pred_a = get_Proj_pred(a);
4903 ir_node *pred_b = get_Proj_pred(b);
4904 if (pred_a == pred_b) {
4905 dbg_info *dbgi = get_irn_dbg_info(n);
4906 ir_node *block = get_nodes_block(pred_a);
4907 pn_Cmp pn_a = get_Proj_proj(a);
4908 pn_Cmp pn_b = get_Proj_proj(b);
4909 /* yes, we can simply calculate with pncs */
4910 pn_Cmp new_pnc = pn_a | pn_b;
4912 return new_rd_Proj(dbgi, current_ir_graph, block, pred_a, mode_b,
4917 mode = get_irn_mode(n);
4918 HANDLE_BINOP_PHI(tarval_or, a, b, c, mode);
4920 n = transform_node_Or_bf_store(n);
4921 n = transform_node_Or_Rotl(n);
4925 n = transform_bitwise_distributive(n, transform_node_Or);
4928 } /* transform_node_Or */
4932 static ir_node *transform_node(ir_node *n);
4935 * Optimize (a >> c1) >> c2), works for Shr, Shrs, Shl, Rotl.
4937 * Should be moved to reassociation?
4939 static ir_node *transform_node_shift(ir_node *n) {
4940 ir_node *left, *right;
4942 tarval *tv1, *tv2, *res;
4943 ir_node *in[2], *irn, *block;
4945 left = get_binop_left(n);
4947 /* different operations */
4948 if (get_irn_op(left) != get_irn_op(n))
4951 right = get_binop_right(n);
4952 tv1 = value_of(right);
4953 if (tv1 == tarval_bad)
4956 tv2 = value_of(get_binop_right(left));
4957 if (tv2 == tarval_bad)
4960 res = tarval_add(tv1, tv2);
4961 mode = get_irn_mode(n);
4963 /* beware: a simple replacement works only, if res < modulo shift */
4965 int modulo_shf = get_mode_modulo_shift(mode);
4966 assert(modulo_shf >= (int) get_mode_size_bits(mode));
4967 if (modulo_shf > 0) {
4968 tarval *modulo = new_tarval_from_long(modulo_shf,
4969 get_tarval_mode(res));
4971 /* shifting too much */
4972 if (!(tarval_cmp(res, modulo) & pn_Cmp_Lt)) {
4974 ir_graph *irg = get_irn_irg(n);
4975 ir_node *block = get_nodes_block(n);
4976 dbg_info *dbgi = get_irn_dbg_info(n);
4977 ir_node *cnst = new_Const(mode_Iu, new_tarval_from_long(get_mode_size_bits(mode)-1, mode_Iu));
4978 return new_rd_Shrs(dbgi, irg, block, get_binop_left(left),
4982 return new_Const(mode, get_mode_null(mode));
4986 res = tarval_mod(res, new_tarval_from_long(get_mode_size_bits(mode), get_tarval_mode(res)));
4989 /* ok, we can replace it */
4990 block = get_nodes_block(n);
4992 in[0] = get_binop_left(left);
4993 in[1] = new_r_Const(current_ir_graph, block, get_tarval_mode(res), res);
4995 irn = new_ir_node(NULL, current_ir_graph, block, get_irn_op(n), mode, 2, in);
4997 DBG_OPT_ALGSIM0(n, irn, FS_OPT_REASSOC_SHIFT);
4999 return transform_node(irn);
5000 } /* transform_node_shift */
5003 * normalisation: (x & c1) >> c2 to (x >> c2) & (c1 >> c2)
5005 * - and, or, xor instead of &
5006 * - Shl, Shr, Shrs, rotl instead of >>
5007 * (with a special case for Or/Xor + Shrs)
5009 static ir_node *transform_node_bitop_shift(ir_node *n) {
5011 ir_node *right = get_binop_right(n);
5012 ir_mode *mode = get_irn_mode(n);
5013 ir_node *bitop_left;
5014 ir_node *bitop_right;
5026 assert(is_Shrs(n) || is_Shr(n) || is_Shl(n) || is_Rotl(n));
5028 if (!is_Const(right))
5031 left = get_binop_left(n);
5032 op_left = get_irn_op(left);
5033 if (op_left != op_And && op_left != op_Or && op_left != op_Eor)
5036 /* doing it with Shrs is not legal if the Or/Eor affects the topmost bit */
5037 if (is_Shrs(n) && (op_left == op_Or || op_left == op_Eor)) {
5038 /* TODO: test if sign bit is affectes */
5042 bitop_right = get_binop_right(left);
5043 if (!is_Const(bitop_right))
5046 bitop_left = get_binop_left(left);
5048 irg = get_irn_irg(n);
5049 block = get_nodes_block(n);
5050 dbgi = get_irn_dbg_info(n);
5051 tv1 = get_Const_tarval(bitop_right);
5052 tv2 = get_Const_tarval(right);
5054 assert(get_tarval_mode(tv1) == mode);
5057 new_shift = new_rd_Shl(dbgi, irg, block, bitop_left, right, mode);
5058 tv_shift = tarval_shl(tv1, tv2);
5059 } else if(is_Shr(n)) {
5060 new_shift = new_rd_Shr(dbgi, irg, block, bitop_left, right, mode);
5061 tv_shift = tarval_shr(tv1, tv2);
5062 } else if(is_Shrs(n)) {
5063 new_shift = new_rd_Shrs(dbgi, irg, block, bitop_left, right, mode);
5064 tv_shift = tarval_shrs(tv1, tv2);
5067 new_shift = new_rd_Rotl(dbgi, irg, block, bitop_left, right, mode);
5068 tv_shift = tarval_rotl(tv1, tv2);
5071 assert(get_tarval_mode(tv_shift) == mode);
5072 new_const = new_Const(mode, tv_shift);
5074 if (op_left == op_And) {
5075 new_bitop = new_rd_And(dbgi, irg, block, new_shift, new_const, mode);
5076 } else if(op_left == op_Or) {
5077 new_bitop = new_rd_Or(dbgi, irg, block, new_shift, new_const, mode);
5079 assert(op_left == op_Eor);
5080 new_bitop = new_rd_Eor(dbgi, irg, block, new_shift, new_const, mode);
5088 * (x << c1) >> c2 <=> x OP (c2-c1) & ((-1 << c1) >> c2)
5090 * (x >> c1) << c2 <=> x OP (c2-c1) & ((-1 >> c1) << c2)
5091 * (also with x >>s c1 when c1>=c2)
5093 static ir_node *transform_node_shl_shr(ir_node *n) {
5095 ir_node *right = get_binop_right(n);
5111 assert(is_Shl(n) || is_Shr(n) || is_Shrs(n));
5113 if (!is_Const(right))
5116 left = get_binop_left(n);
5117 mode = get_irn_mode(n);
5118 if (is_Shl(n) && (is_Shr(left) || is_Shrs(left))) {
5119 ir_node *shr_right = get_binop_right(left);
5121 if (!is_Const(shr_right))
5124 x = get_binop_left(left);
5125 tv_shr = get_Const_tarval(shr_right);
5126 tv_shl = get_Const_tarval(right);
5128 if (is_Shrs(left)) {
5129 /* shrs variant only allowed if c1 >= c2 */
5130 if (! (tarval_cmp(tv_shl, tv_shr) & pn_Cmp_Ge))
5133 tv_mask = tarval_shrs(get_mode_all_one(mode), tv_shr);
5136 tv_mask = tarval_shr(get_mode_all_one(mode), tv_shr);
5138 tv_mask = tarval_shl(tv_mask, tv_shl);
5139 } else if(is_Shr(n) && is_Shl(left)) {
5140 ir_node *shl_right = get_Shl_right(left);
5142 if (!is_Const(shl_right))
5145 x = get_Shl_left(left);
5146 tv_shr = get_Const_tarval(right);
5147 tv_shl = get_Const_tarval(shl_right);
5149 tv_mask = tarval_shl(get_mode_all_one(mode), tv_shl);
5150 tv_mask = tarval_shr(tv_mask, tv_shr);
5155 assert(get_tarval_mode(tv_shl) == get_tarval_mode(tv_shr));
5156 assert(tv_mask != tarval_bad);
5157 assert(get_tarval_mode(tv_mask) == mode);
5159 irg = get_irn_irg(n);
5160 block = get_nodes_block(n);
5161 dbgi = get_irn_dbg_info(n);
5163 pnc = tarval_cmp(tv_shl, tv_shr);
5164 if (pnc == pn_Cmp_Lt || pnc == pn_Cmp_Eq) {
5165 tv_shift = tarval_sub(tv_shr, tv_shl, NULL);
5166 new_const = new_Const(get_tarval_mode(tv_shift), tv_shift);
5168 new_shift = new_rd_Shrs(dbgi, irg, block, x, new_const, mode);
5170 new_shift = new_rd_Shr(dbgi, irg, block, x, new_const, mode);
5173 assert(pnc == pn_Cmp_Gt);
5174 tv_shift = tarval_sub(tv_shl, tv_shr, NULL);
5175 new_const = new_Const(get_tarval_mode(tv_shift), tv_shift);
5176 new_shift = new_rd_Shl(dbgi, irg, block, x, new_const, mode);
5179 new_const = new_Const(mode, tv_mask);
5180 new_and = new_rd_And(dbgi, irg, block, new_shift, new_const, mode);
5188 static ir_node *transform_node_Shr(ir_node *n) {
5189 ir_node *c, *oldn = n;
5190 ir_node *left = get_Shr_left(n);
5191 ir_node *right = get_Shr_right(n);
5192 ir_mode *mode = get_irn_mode(n);
5194 HANDLE_BINOP_PHI(tarval_shr, left, right, c, mode);
5195 n = transform_node_shift(n);
5198 n = transform_node_shl_shr(n);
5200 n = transform_node_bitop_shift(n);
5203 } /* transform_node_Shr */
5208 static ir_node *transform_node_Shrs(ir_node *n) {
5209 ir_node *c, *oldn = n;
5210 ir_node *a = get_Shrs_left(n);
5211 ir_node *b = get_Shrs_right(n);
5212 ir_mode *mode = get_irn_mode(n);
5214 HANDLE_BINOP_PHI(tarval_shrs, a, b, c, mode);
5215 n = transform_node_shift(n);
5218 n = transform_node_bitop_shift(n);
5221 } /* transform_node_Shrs */
5226 static ir_node *transform_node_Shl(ir_node *n) {
5227 ir_node *c, *oldn = n;
5228 ir_node *a = get_Shl_left(n);
5229 ir_node *b = get_Shl_right(n);
5230 ir_mode *mode = get_irn_mode(n);
5232 HANDLE_BINOP_PHI(tarval_shl, a, b, c, mode);
5233 n = transform_node_shift(n);
5236 n = transform_node_shl_shr(n);
5238 n = transform_node_bitop_shift(n);
5241 } /* transform_node_Shl */
5246 static ir_node *transform_node_Rotl(ir_node *n) {
5247 ir_node *c, *oldn = n;
5248 ir_node *a = get_Rotl_left(n);
5249 ir_node *b = get_Rotl_right(n);
5250 ir_mode *mode = get_irn_mode(n);
5252 HANDLE_BINOP_PHI(tarval_rotl, a, b, c, mode);
5253 n = transform_node_shift(n);
5256 n = transform_node_bitop_shift(n);
5259 } /* transform_node_Rotl */
5264 static ir_node *transform_node_Conv(ir_node *n) {
5265 ir_node *c, *oldn = n;
5266 ir_node *a = get_Conv_op(n);
5268 if (is_const_Phi(a)) {
5269 c = apply_conv_on_phi(a, get_irn_mode(n));
5271 DBG_OPT_ALGSIM0(oldn, c, FS_OPT_CONST_PHI);
5276 if (is_Unknown(a)) { /* Conv_A(Unknown_B) -> Unknown_A */
5277 ir_mode *mode = get_irn_mode(n);
5278 return new_r_Unknown(current_ir_graph, mode);
5282 } /* transform_node_Conv */
5285 * Remove dead blocks and nodes in dead blocks
5286 * in keep alive list. We do not generate a new End node.
5288 static ir_node *transform_node_End(ir_node *n) {
5289 int i, j, n_keepalives = get_End_n_keepalives(n);
5292 NEW_ARR_A(ir_node *, in, n_keepalives);
5294 for (i = j = 0; i < n_keepalives; ++i) {
5295 ir_node *ka = get_End_keepalive(n, i);
5297 if (! is_Block_dead(ka)) {
5301 } else if (is_irn_pinned_in_irg(ka) && is_Block_dead(get_nodes_block(ka))) {
5304 /* FIXME: beabi need to keep a Proj(M) */
5305 if (is_Phi(ka) || is_irn_keep(ka) || is_Proj(ka))
5308 if (j != n_keepalives)
5309 set_End_keepalives(n, j, in);
5311 } /* transform_node_End */
5313 /** returns 1 if a == -b */
5314 static int is_negated_value(ir_node *a, ir_node *b) {
5315 if (is_Minus(a) && get_Minus_op(a) == b)
5317 if (is_Minus(b) && get_Minus_op(b) == a)
5319 if (is_Sub(a) && is_Sub(b)) {
5320 ir_node *a_left = get_Sub_left(a);
5321 ir_node *a_right = get_Sub_right(a);
5322 ir_node *b_left = get_Sub_left(b);
5323 ir_node *b_right = get_Sub_right(b);
5325 if (a_left == b_right && a_right == b_left)
5333 * Optimize a Mux into some simpler cases.
5335 static ir_node *transform_node_Mux(ir_node *n) {
5336 ir_node *oldn = n, *sel = get_Mux_sel(n);
5337 ir_mode *mode = get_irn_mode(n);
5338 ir_node *t = get_Mux_true(n);
5339 ir_node *f = get_Mux_false(n);
5340 ir_graph *irg = current_ir_graph;
5342 /* first normalization step: move a possible zero to the false case */
5344 ir_node *cmp = get_Proj_pred(sel);
5347 if (is_Const(t) && is_Const_null(t)) {
5350 /* Mux(x, 0, y) => Mux(x, y, 0) */
5351 pn_Cmp pnc = get_Proj_proj(sel);
5352 sel = new_r_Proj(irg, get_nodes_block(cmp), cmp, mode_b,
5353 get_negated_pnc(pnc, get_irn_mode(get_Cmp_left(cmp))));
5354 n = new_rd_Mux(get_irn_dbg_info(n), irg, get_nodes_block(n), sel, t, f, mode);
5362 /* note: after normalization, false can only happen on default */
5363 if (mode == mode_b) {
5364 dbg_info *dbg = get_irn_dbg_info(n);
5365 ir_node *block = get_nodes_block(n);
5366 ir_graph *irg = current_ir_graph;
5369 tarval *tv_t = get_Const_tarval(t);
5370 if (tv_t == tarval_b_true) {
5372 /* Muxb(sel, true, false) = sel */
5373 assert(get_Const_tarval(f) == tarval_b_false);
5374 DBG_OPT_ALGSIM0(oldn, sel, FS_OPT_MUX_BOOL);
5377 /* Muxb(sel, true, x) = Or(sel, x) */
5378 n = new_rd_Or(dbg, irg, block, sel, f, mode_b);
5379 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_OR_BOOL);
5383 } else if (is_Const(f)) {
5384 tarval *tv_f = get_Const_tarval(f);
5385 if (tv_f == tarval_b_true) {
5386 /* Muxb(sel, x, true) = Or(Not(sel), x) */
5387 ir_node* not_sel = new_rd_Not(dbg, irg, block, sel, mode_b);
5388 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_ORNOT_BOOL);
5389 n = new_rd_Or(dbg, irg, block, not_sel, t, mode_b);
5392 /* Muxb(sel, x, false) = And(sel, x) */
5393 assert(tv_f == tarval_b_false);
5394 n = new_rd_And(dbg, irg, block, sel, t, mode_b);
5395 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_AND_BOOL);
5401 /* more normalization: try to normalize Mux(x, C1, C2) into Mux(x, +1/-1, 0) op C2 */
5402 if (is_Const(t) && is_Const(f) && mode_is_int(mode)) {
5403 tarval *a = get_Const_tarval(t);
5404 tarval *b = get_Const_tarval(f);
5405 tarval *null = get_tarval_null(mode);
5408 if (tarval_cmp(a, b) & pn_Cmp_Gt) {
5409 diff = tarval_sub(a, b, NULL);
5412 diff = tarval_sub(b, a, NULL);
5416 if (diff == get_tarval_one(mode) && min != null) {
5417 dbg_info *dbg = get_irn_dbg_info(n);
5418 ir_node *block = get_nodes_block(n);
5419 ir_graph *irg = current_ir_graph;
5420 ir_node *t = new_Const(mode, tarval_sub(a, min, NULL));
5421 ir_node *f = new_Const(mode, tarval_sub(b, min, NULL));
5422 n = new_rd_Mux(dbg, irg, block, sel, f, t, mode);
5423 n = new_rd_Add(dbg, irg, block, n, new_Const(mode, min), mode);
5429 ir_node *cmp = get_Proj_pred(sel);
5430 long pn = get_Proj_proj(sel);
5433 * Note: normalization puts the constant on the right side,
5434 * so we check only one case.
5436 * Note further that these optimization work even for floating point
5437 * with NaN's because -NaN == NaN.
5438 * However, if +0 and -0 is handled differently, we cannot use the Abs/-Abs
5442 ir_node *cmp_r = get_Cmp_right(cmp);
5443 if (is_Const(cmp_r) && is_Const_null(cmp_r)) {
5444 ir_node *block = get_nodes_block(n);
5445 ir_node *cmp_l = get_Cmp_left(cmp);
5447 if (!mode_honor_signed_zeros(mode) && is_negated_value(f, t)) {
5450 if ( (cmp_l == t && (pn == pn_Cmp_Ge || pn == pn_Cmp_Gt))
5451 || (cmp_l == f && (pn == pn_Cmp_Le || pn == pn_Cmp_Lt)))
5453 /* Mux(a >/>= 0, a, -a) = Mux(a </<= 0, -a, a) ==> Abs(a) */
5454 n = new_rd_Abs(get_irn_dbg_info(n), current_ir_graph, block,
5456 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_ABS);
5458 } else if ((cmp_l == t && (pn == pn_Cmp_Le || pn == pn_Cmp_Lt))
5459 || (cmp_l == f && (pn == pn_Cmp_Ge || pn == pn_Cmp_Gt)))
5461 /* Mux(a </<= 0, a, -a) = Mux(a >/>= 0, -a, a) ==> -Abs(a) */
5462 n = new_rd_Abs(get_irn_dbg_info(n), current_ir_graph, block,
5464 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph,
5466 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_ABS);
5471 if (mode_is_int(mode)) {
5473 if ((pn == pn_Cmp_Lg || pn == pn_Cmp_Eq) && is_And(cmp_l)) {
5474 /* Mux((a & b) != 0, c, 0) */
5475 ir_node *and_r = get_And_right(cmp_l);
5478 if (and_r == t && f == cmp_r) {
5479 if (is_Const(t) && tarval_is_single_bit(get_Const_tarval(t))) {
5480 if (pn == pn_Cmp_Lg) {
5481 /* Mux((a & 2^C) != 0, 2^C, 0) */
5484 /* Mux((a & 2^C) == 0, 2^C, 0) */
5485 n = new_rd_Eor(get_irn_dbg_info(n), current_ir_graph,
5486 block, cmp_l, t, mode);
5491 if (is_Shl(and_r)) {
5492 ir_node *shl_l = get_Shl_left(and_r);
5493 if (is_Const(shl_l) && is_Const_one(shl_l)) {
5494 if (and_r == t && f == cmp_r) {
5495 if (pn == pn_Cmp_Lg) {
5496 /* (a & (1 << n)) != 0, (1 << n), 0) */
5499 /* (a & (1 << n)) == 0, (1 << n), 0) */
5500 n = new_rd_Eor(get_irn_dbg_info(n), current_ir_graph,
5501 block, cmp_l, t, mode);
5507 and_l = get_And_left(cmp_l);
5508 if (is_Shl(and_l)) {
5509 ir_node *shl_l = get_Shl_left(and_l);
5510 if (is_Const(shl_l) && is_Const_one(shl_l)) {
5511 if (and_l == t && f == cmp_r) {
5512 if (pn == pn_Cmp_Lg) {
5513 /* ((1 << n) & a) != 0, (1 << n), 0) */
5516 /* ((1 << n) & a) == 0, (1 << n), 0) */
5517 n = new_rd_Eor(get_irn_dbg_info(n), current_ir_graph,
5518 block, cmp_l, t, mode);
5529 return arch_transform_node_Mux(n);
5530 } /* transform_node_Mux */
5533 * optimize Sync nodes that have other syncs as input we simply add the inputs
5534 * of the other sync to our own inputs
5536 static ir_node *transform_node_Sync(ir_node *n) {
5537 int arity = get_Sync_n_preds(n);
5540 for (i = 0; i < arity;) {
5541 ir_node *pred = get_Sync_pred(n, i);
5545 if (!is_Sync(pred)) {
5553 pred_arity = get_Sync_n_preds(pred);
5554 for (j = 0; j < pred_arity; ++j) {
5555 ir_node *pred_pred = get_Sync_pred(pred, j);
5560 add_irn_n(n, pred_pred);
5564 if (get_Sync_pred(n, k) == pred_pred) break;
5569 /* rehash the sync node */
5570 add_identities(current_ir_graph->value_table, n);
5576 * Tries several [inplace] [optimizing] transformations and returns an
5577 * equivalent node. The difference to equivalent_node() is that these
5578 * transformations _do_ generate new nodes, and thus the old node must
5579 * not be freed even if the equivalent node isn't the old one.
5581 static ir_node *transform_node(ir_node *n) {
5585 * Transform_node is the only "optimizing transformation" that might
5586 * return a node with a different opcode. We iterate HERE until fixpoint
5587 * to get the final result.
5591 if (n->op->ops.transform_node)
5592 n = n->op->ops.transform_node(n);
5593 } while (oldn != n);
5596 } /* transform_node */
5599 * Sets the default transform node operation for an ir_op_ops.
5601 * @param code the opcode for the default operation
5602 * @param ops the operations initialized
5607 static ir_op_ops *firm_set_default_transform_node(ir_opcode code, ir_op_ops *ops)
5611 ops->transform_node = transform_node_##a; \
5613 #define CASE_PROJ(a) \
5615 ops->transform_node_Proj = transform_node_Proj_##a; \
5617 #define CASE_PROJ_EX(a) \
5619 ops->transform_node = transform_node_##a; \
5620 ops->transform_node_Proj = transform_node_Proj_##a; \
5629 CASE_PROJ_EX(DivMod);
5663 } /* firm_set_default_transform_node */
5666 /* **************** Common Subexpression Elimination **************** */
5668 /** The size of the hash table used, should estimate the number of nodes
5670 #define N_IR_NODES 512
5672 /** Compares the attributes of two Const nodes. */
5673 static int node_cmp_attr_Const(ir_node *a, ir_node *b) {
5674 return (get_Const_tarval(a) != get_Const_tarval(b))
5675 || (get_Const_type(a) != get_Const_type(b));
5676 } /* node_cmp_attr_Const */
5678 /** Compares the attributes of two Proj nodes. */
5679 static int node_cmp_attr_Proj(ir_node *a, ir_node *b) {
5680 return get_irn_proj_attr(a) != get_irn_proj_attr(b);
5681 } /* node_cmp_attr_Proj */
5683 /** Compares the attributes of two Filter nodes. */
5684 static int node_cmp_attr_Filter(ir_node *a, ir_node *b) {
5685 return get_Filter_proj(a) != get_Filter_proj(b);
5686 } /* node_cmp_attr_Filter */
5688 /** Compares the attributes of two Alloc nodes. */
5689 static int node_cmp_attr_Alloc(ir_node *a, ir_node *b) {
5690 const alloc_attr *pa = get_irn_alloc_attr(a);
5691 const alloc_attr *pb = get_irn_alloc_attr(b);
5692 return (pa->where != pb->where) || (pa->type != pb->type);
5693 } /* node_cmp_attr_Alloc */
5695 /** Compares the attributes of two Free nodes. */
5696 static int node_cmp_attr_Free(ir_node *a, ir_node *b) {
5697 const free_attr *pa = get_irn_free_attr(a);
5698 const free_attr *pb = get_irn_free_attr(b);
5699 return (pa->where != pb->where) || (pa->type != pb->type);
5700 } /* node_cmp_attr_Free */
5702 /** Compares the attributes of two SymConst nodes. */
5703 static int node_cmp_attr_SymConst(ir_node *a, ir_node *b) {
5704 const symconst_attr *pa = get_irn_symconst_attr(a);
5705 const symconst_attr *pb = get_irn_symconst_attr(b);
5706 return (pa->kind != pb->kind)
5707 || (pa->sym.type_p != pb->sym.type_p)
5708 || (pa->tp != pb->tp);
5709 } /* node_cmp_attr_SymConst */
5711 /** Compares the attributes of two Call nodes. */
5712 static int node_cmp_attr_Call(ir_node *a, ir_node *b) {
5713 return get_irn_call_attr(a) != get_irn_call_attr(b);
5714 } /* node_cmp_attr_Call */
5716 /** Compares the attributes of two Sel nodes. */
5717 static int node_cmp_attr_Sel(ir_node *a, ir_node *b) {
5718 const ir_entity *a_ent = get_Sel_entity(a);
5719 const ir_entity *b_ent = get_Sel_entity(b);
5721 (a_ent->kind != b_ent->kind) ||
5722 (a_ent->name != b_ent->name) ||
5723 (a_ent->owner != b_ent->owner) ||
5724 (a_ent->ld_name != b_ent->ld_name) ||
5725 (a_ent->type != b_ent->type);
5726 } /* node_cmp_attr_Sel */
5728 /** Compares the attributes of two Phi nodes. */
5729 static int node_cmp_attr_Phi(ir_node *a, ir_node *b) {
5730 /* we can only enter this function if both nodes have the same number of inputs,
5731 hence it is enough to check if one of them is a Phi0 */
5733 /* check the Phi0 pos attribute */
5734 return get_irn_phi_attr(a)->u.pos != get_irn_phi_attr(b)->u.pos;
5737 } /* node_cmp_attr_Phi */
5739 /** Compares the attributes of two Conv nodes. */
5740 static int node_cmp_attr_Conv(ir_node *a, ir_node *b) {
5741 return get_Conv_strict(a) != get_Conv_strict(b);
5742 } /* node_cmp_attr_Conv */
5744 /** Compares the attributes of two Cast nodes. */
5745 static int node_cmp_attr_Cast(ir_node *a, ir_node *b) {
5746 return get_Cast_type(a) != get_Cast_type(b);
5747 } /* node_cmp_attr_Cast */
5749 /** Compares the attributes of two Load nodes. */
5750 static int node_cmp_attr_Load(ir_node *a, ir_node *b) {
5751 if (get_Load_volatility(a) == volatility_is_volatile ||
5752 get_Load_volatility(b) == volatility_is_volatile)
5753 /* NEVER do CSE on volatile Loads */
5755 /* do not CSE Loads with different alignment. Be conservative. */
5756 if (get_Load_align(a) != get_Load_align(b))
5759 return get_Load_mode(a) != get_Load_mode(b);
5760 } /* node_cmp_attr_Load */
5762 /** Compares the attributes of two Store nodes. */
5763 static int node_cmp_attr_Store(ir_node *a, ir_node *b) {
5764 /* do not CSE Stores with different alignment. Be conservative. */
5765 if (get_Store_align(a) != get_Store_align(b))
5768 /* NEVER do CSE on volatile Stores */
5769 return (get_Store_volatility(a) == volatility_is_volatile ||
5770 get_Store_volatility(b) == volatility_is_volatile);
5771 } /* node_cmp_attr_Store */
5773 /** Compares two exception attributes */
5774 static int node_cmp_exception(ir_node *a, ir_node *b) {
5775 const except_attr *ea = get_irn_except_attr(a);
5776 const except_attr *eb = get_irn_except_attr(b);
5778 return ea->pin_state != eb->pin_state;
5781 #define node_cmp_attr_Bound node_cmp_exception
5783 /** Compares the attributes of two Div nodes. */
5784 static int node_cmp_attr_Div(ir_node *a, ir_node *b) {
5785 const divmod_attr *ma = get_irn_divmod_attr(a);
5786 const divmod_attr *mb = get_irn_divmod_attr(b);
5787 return ma->exc.pin_state != mb->exc.pin_state ||
5788 ma->res_mode != mb->res_mode ||
5789 ma->no_remainder != mb->no_remainder;
5790 } /* node_cmp_attr_Div */
5792 /** Compares the attributes of two DivMod nodes. */
5793 static int node_cmp_attr_DivMod(ir_node *a, ir_node *b) {
5794 const divmod_attr *ma = get_irn_divmod_attr(a);
5795 const divmod_attr *mb = get_irn_divmod_attr(b);
5796 return ma->exc.pin_state != mb->exc.pin_state ||
5797 ma->res_mode != mb->res_mode;
5798 } /* node_cmp_attr_DivMod */
5800 /** Compares the attributes of two Mod nodes. */
5801 static int node_cmp_attr_Mod(ir_node *a, ir_node *b) {
5802 const divmod_attr *ma = get_irn_divmod_attr(a);
5803 const divmod_attr *mb = get_irn_divmod_attr(b);
5804 return ma->exc.pin_state != mb->exc.pin_state ||
5805 ma->res_mode != mb->res_mode;
5806 } /* node_cmp_attr_Mod */
5808 /** Compares the attributes of two Quot nodes. */
5809 static int node_cmp_attr_Quot(ir_node *a, ir_node *b) {
5810 const divmod_attr *ma = get_irn_divmod_attr(a);
5811 const divmod_attr *mb = get_irn_divmod_attr(b);
5812 return ma->exc.pin_state != mb->exc.pin_state ||
5813 ma->res_mode != mb->res_mode;
5814 } /* node_cmp_attr_Quot */
5816 /** Compares the attributes of two Confirm nodes. */
5817 static int node_cmp_attr_Confirm(ir_node *a, ir_node *b) {
5818 return (get_Confirm_cmp(a) != get_Confirm_cmp(b));
5819 } /* node_cmp_attr_Confirm */
5821 /** Compares the attributes of two ASM nodes. */
5822 static int node_cmp_attr_ASM(ir_node *a, ir_node *b) {
5824 const ir_asm_constraint *ca;
5825 const ir_asm_constraint *cb;
5828 if (get_ASM_text(a) != get_ASM_text(b))
5831 /* Should we really check the constraints here? Should be better, but is strange. */
5832 n = get_ASM_n_input_constraints(a);
5833 if (n != get_ASM_n_input_constraints(b))
5836 ca = get_ASM_input_constraints(a);
5837 cb = get_ASM_input_constraints(b);
5838 for (i = 0; i < n; ++i) {
5839 if (ca[i].pos != cb[i].pos || ca[i].constraint != cb[i].constraint)
5843 n = get_ASM_n_output_constraints(a);
5844 if (n != get_ASM_n_output_constraints(b))
5847 ca = get_ASM_output_constraints(a);
5848 cb = get_ASM_output_constraints(b);
5849 for (i = 0; i < n; ++i) {
5850 if (ca[i].pos != cb[i].pos || ca[i].constraint != cb[i].constraint)
5854 n = get_ASM_n_clobbers(a);
5855 if (n != get_ASM_n_clobbers(b))
5858 cla = get_ASM_clobbers(a);
5859 clb = get_ASM_clobbers(b);
5860 for (i = 0; i < n; ++i) {
5861 if (cla[i] != clb[i])
5865 } /* node_cmp_attr_ASM */
5868 * Set the default node attribute compare operation for an ir_op_ops.
5870 * @param code the opcode for the default operation
5871 * @param ops the operations initialized
5876 static ir_op_ops *firm_set_default_node_cmp_attr(ir_opcode code, ir_op_ops *ops)
5880 ops->node_cmp_attr = node_cmp_attr_##a; \
5911 } /* firm_set_default_node_cmp_attr */
5914 * Compare function for two nodes in the value table. Gets two
5915 * nodes as parameters. Returns 0 if the nodes are a Common Sub Expression.
5917 int identities_cmp(const void *elt, const void *key) {
5918 ir_node *a = (ir_node *)elt;
5919 ir_node *b = (ir_node *)key;
5922 if (a == b) return 0;
5924 if ((get_irn_op(a) != get_irn_op(b)) ||
5925 (get_irn_mode(a) != get_irn_mode(b))) return 1;
5927 /* compare if a's in and b's in are of equal length */
5928 irn_arity_a = get_irn_intra_arity(a);
5929 if (irn_arity_a != get_irn_intra_arity(b))
5932 if (get_irn_pinned(a) == op_pin_state_pinned) {
5933 /* for pinned nodes, the block inputs must be equal */
5934 if (get_irn_intra_n(a, -1) != get_irn_intra_n(b, -1))
5936 } else if (! get_opt_global_cse()) {
5937 /* for block-local CSE both nodes must be in the same MacroBlock */
5938 if (get_irn_MacroBlock(a) != get_irn_MacroBlock(b))
5942 /* compare a->in[0..ins] with b->in[0..ins] */
5943 for (i = 0; i < irn_arity_a; i++)
5944 if (get_irn_intra_n(a, i) != get_irn_intra_n(b, i))
5948 * here, we already now that the nodes are identical except their
5951 if (a->op->ops.node_cmp_attr)
5952 return a->op->ops.node_cmp_attr(a, b);
5955 } /* identities_cmp */
5958 * Calculate a hash value of a node.
5960 * @param node The IR-node
5962 unsigned ir_node_hash(const ir_node *node) {
5963 return node->op->ops.hash(node);
5964 } /* ir_node_hash */
5967 pset *new_identities(void) {
5968 return new_pset(identities_cmp, N_IR_NODES);
5969 } /* new_identities */
5971 void del_identities(pset *value_table) {
5972 del_pset(value_table);
5973 } /* del_identities */
5976 * Normalize a node by putting constants (and operands with larger
5977 * node index) on the right (operator side).
5979 * @param n The node to normalize
5981 static void normalize_node(ir_node *n) {
5982 if (is_op_commutative(get_irn_op(n))) {
5983 ir_node *l = get_binop_left(n);
5984 ir_node *r = get_binop_right(n);
5986 /* For commutative operators perform a OP b == b OP a but keep
5987 * constants on the RIGHT side. This helps greatly in some
5988 * optimizations. Moreover we use the idx number to make the form
5990 if (!operands_are_normalized(l, r)) {
5991 set_binop_left(n, r);
5992 set_binop_right(n, l);
5995 } /* normalize_node */
5998 * Update the nodes after a match in the value table. If both nodes have
5999 * the same MacroBlock but different Blocks, we must ensure that the node
6000 * with the dominating Block (the node that is near to the MacroBlock header
6001 * is stored in the table.
6002 * Because a MacroBlock has only one "non-exception" flow, we don't need
6003 * dominance info here: We known, that one block must dominate the other and
6004 * following the only block input will allow to find it.
6006 static void update_known_irn(ir_node *known_irn, const ir_node *new_ir_node) {
6007 ir_node *known_blk, *new_block, *block, *mbh;
6009 if (get_opt_global_cse()) {
6010 /* Block inputs are meaning less */
6013 known_blk = get_irn_n(known_irn, -1);
6014 new_block = get_irn_n(new_ir_node, -1);
6015 if (known_blk == new_block) {
6016 /* already in the same block */
6020 * We expect the typical case when we built the graph. In that case, the
6021 * known_irn is already the upper one, so checking this should be faster.
6024 mbh = get_Block_MacroBlock(new_block);
6026 if (block == known_blk) {
6027 /* ok, we have found it: known_block dominates new_block as expected */
6032 * We have reached the MacroBlock header NOT founding
6033 * the known_block. new_block must dominate known_block.
6036 set_irn_n(known_irn, -1, new_block);
6039 assert(get_Block_n_cfgpreds(block) == 1);
6040 block = get_Block_cfgpred_block(block, 0);
6042 } /* update_value_table */
6045 * Return the canonical node computing the same value as n.
6046 * Looks up the node in a hash table, enters it in the table
6047 * if it isn't there yet.
6049 * @param value_table the HashSet containing all nodes in the
6051 * @param n the node to look up
6053 * @return a node that computes the same value as n or n if no such
6054 * node could be found
6056 ir_node *identify_remember(pset *value_table, ir_node *n) {
6059 if (!value_table) return n;
6062 /* lookup or insert in hash table with given hash key. */
6063 o = pset_insert(value_table, n, ir_node_hash(n));
6066 update_known_irn(o, n);
6071 } /* identify_remember */
6074 * During construction we set the op_pin_state_pinned flag in the graph right when the
6075 * optimization is performed. The flag turning on procedure global cse could
6076 * be changed between two allocations. This way we are safe.
6078 * @param value_table The value table
6079 * @param n The node to lookup
6081 static INLINE ir_node *identify_cons(pset *value_table, ir_node *n) {
6084 n = identify_remember(value_table, n);
6085 if (n != old && get_irn_MacroBlock(old) != get_irn_MacroBlock(n))
6086 set_irg_pinned(current_ir_graph, op_pin_state_floats);
6088 } /* identify_cons */
6090 /* Add a node to the identities value table. */
6091 void add_identities(pset *value_table, ir_node *node) {
6092 if (get_opt_cse() && is_no_Block(node))
6093 identify_remember(value_table, node);
6094 } /* add_identities */
6096 /* Visit each node in the value table of a graph. */
6097 void visit_all_identities(ir_graph *irg, irg_walk_func visit, void *env) {
6099 ir_graph *rem = current_ir_graph;
6101 current_ir_graph = irg;
6102 foreach_pset(irg->value_table, node)
6104 current_ir_graph = rem;
6105 } /* visit_all_identities */
6108 * Garbage in, garbage out. If a node has a dead input, i.e., the
6109 * Bad node is input to the node, return the Bad node.
6111 static ir_node *gigo(ir_node *node) {
6113 ir_op *op = get_irn_op(node);
6115 /* remove garbage blocks by looking at control flow that leaves the block
6116 and replacing the control flow by Bad. */
6117 if (get_irn_mode(node) == mode_X) {
6118 ir_node *block = get_nodes_block(skip_Proj(node));
6120 /* Don't optimize nodes in immature blocks. */
6121 if (!get_Block_matured(block))
6123 /* Don't optimize End, may have Bads. */
6124 if (op == op_End) return node;
6126 if (is_Block(block)) {
6127 if (is_Block_dead(block)) {
6128 /* control flow from dead block is dead */
6132 for (i = get_irn_arity(block) - 1; i >= 0; --i) {
6133 if (!is_Bad(get_irn_n(block, i)))
6137 ir_graph *irg = get_irn_irg(block);
6138 /* the start block is never dead */
6139 if (block != get_irg_start_block(irg)
6140 && block != get_irg_end_block(irg)) {
6142 * Do NOT kill control flow without setting
6143 * the block to dead of bad things can happen:
6144 * We get a Block that is not reachable be irg_block_walk()
6145 * but can be found by irg_walk()!
6147 set_Block_dead(block);
6154 /* Blocks, Phis and Tuples may have dead inputs, e.g., if one of the
6155 blocks predecessors is dead. */
6156 if (op != op_Block && op != op_Phi && op != op_Tuple) {
6157 irn_arity = get_irn_arity(node);
6160 * Beware: we can only read the block of a non-floating node.
6162 if (is_irn_pinned_in_irg(node) &&
6163 is_Block_dead(get_nodes_block(skip_Proj(node))))
6166 for (i = 0; i < irn_arity; i++) {
6167 ir_node *pred = get_irn_n(node, i);
6172 /* Propagating Unknowns here seems to be a bad idea, because
6173 sometimes we need a node as a input and did not want that
6175 However, it might be useful to move this into a later phase
6176 (if you think that optimizing such code is useful). */
6177 if (is_Unknown(pred) && mode_is_data(get_irn_mode(node)))
6178 return new_Unknown(get_irn_mode(node));
6183 /* With this code we violate the agreement that local_optimize
6184 only leaves Bads in Block, Phi and Tuple nodes. */
6185 /* If Block has only Bads as predecessors it's garbage. */
6186 /* If Phi has only Bads as predecessors it's garbage. */
6187 if ((op == op_Block && get_Block_matured(node)) || op == op_Phi) {
6188 irn_arity = get_irn_arity(node);
6189 for (i = 0; i < irn_arity; i++) {
6190 if (!is_Bad(get_irn_n(node, i))) break;
6192 if (i == irn_arity) node = new_Bad();
6199 * These optimizations deallocate nodes from the obstack.
6200 * It can only be called if it is guaranteed that no other nodes
6201 * reference this one, i.e., right after construction of a node.
6203 * @param n The node to optimize
6205 * current_ir_graph must be set to the graph of the node!
6207 ir_node *optimize_node(ir_node *n) {
6210 ir_opcode iro = get_irn_opcode(n);
6212 /* Always optimize Phi nodes: part of the construction. */
6213 if ((!get_opt_optimize()) && (iro != iro_Phi)) return n;
6215 /* constant expression evaluation / constant folding */
6216 if (get_opt_constant_folding()) {
6217 /* neither constants nor Tuple values can be evaluated */
6218 if (iro != iro_Const && (get_irn_mode(n) != mode_T)) {
6219 unsigned fp_model = get_irg_fp_model(current_ir_graph);
6220 int old_fp_mode = tarval_enable_fp_ops((fp_model & fp_strict_algebraic) == 0);
6221 /* try to evaluate */
6222 tv = computed_value(n);
6223 if (tv != tarval_bad) {
6225 ir_type *old_tp = get_irn_type(n);
6226 int i, arity = get_irn_arity(n);
6230 * Try to recover the type of the new expression.
6232 for (i = 0; i < arity && !old_tp; ++i)
6233 old_tp = get_irn_type(get_irn_n(n, i));
6236 * we MUST copy the node here temporary, because it's still needed
6237 * for DBG_OPT_CSTEVAL
6239 node_size = offsetof(ir_node, attr) + n->op->attr_size;
6240 oldn = alloca(node_size);
6242 memcpy(oldn, n, node_size);
6243 CLONE_ARR_A(ir_node *, oldn->in, n->in);
6245 /* ARG, copy the in array, we need it for statistics */
6246 memcpy(oldn->in, n->in, ARR_LEN(n->in) * sizeof(n->in[0]));
6248 /* note the inplace edges module */
6249 edges_node_deleted(n, current_ir_graph);
6251 /* evaluation was successful -- replace the node. */
6252 irg_kill_node(current_ir_graph, n);
6253 nw = new_Const(get_tarval_mode(tv), tv);
6255 if (old_tp && get_type_mode(old_tp) == get_tarval_mode(tv))
6256 set_Const_type(nw, old_tp);
6257 DBG_OPT_CSTEVAL(oldn, nw);
6258 tarval_enable_fp_ops(old_fp_mode);
6261 tarval_enable_fp_ops(old_fp_mode);
6265 /* remove unnecessary nodes */
6266 if (get_opt_algebraic_simplification() ||
6267 (iro == iro_Phi) || /* always optimize these nodes. */
6269 (iro == iro_Proj) ||
6270 (iro == iro_Block) ) /* Flags tested local. */
6271 n = equivalent_node(n);
6273 /* Common Subexpression Elimination.
6275 * Checks whether n is already available.
6276 * The block input is used to distinguish different subexpressions. Right
6277 * now all nodes are op_pin_state_pinned to blocks, i.e., the CSE only finds common
6278 * subexpressions within a block.
6281 n = identify_cons(current_ir_graph->value_table, n);
6284 edges_node_deleted(oldn, current_ir_graph);
6286 /* We found an existing, better node, so we can deallocate the old node. */
6287 irg_kill_node(current_ir_graph, oldn);
6291 /* Some more constant expression evaluation that does not allow to
6293 iro = get_irn_opcode(n);
6294 if (get_opt_algebraic_simplification() ||
6295 (iro == iro_Cond) ||
6296 (iro == iro_Proj)) /* Flags tested local. */
6297 n = transform_node(n);
6299 /* Remove nodes with dead (Bad) input.
6300 Run always for transformation induced Bads. */
6303 /* Now we have a legal, useful node. Enter it in hash table for CSE */
6304 if (get_opt_cse() && (get_irn_opcode(n) != iro_Block)) {
6305 n = identify_remember(current_ir_graph->value_table, n);
6309 } /* optimize_node */
6313 * These optimizations never deallocate nodes (in place). This can cause dead
6314 * nodes lying on the obstack. Remove these by a dead node elimination,
6315 * i.e., a copying garbage collection.
6317 ir_node *optimize_in_place_2(ir_node *n) {
6320 ir_opcode iro = get_irn_opcode(n);
6322 if (!get_opt_optimize() && !is_Phi(n)) return n;
6324 /* constant expression evaluation / constant folding */
6325 if (get_opt_constant_folding()) {
6326 /* neither constants nor Tuple values can be evaluated */
6327 if (iro != iro_Const && get_irn_mode(n) != mode_T) {
6328 unsigned fp_model = get_irg_fp_model(current_ir_graph);
6329 int old_fp_mode = tarval_enable_fp_ops((fp_model & fp_strict_algebraic) == 0);
6330 /* try to evaluate */
6331 tv = computed_value(n);
6332 if (tv != tarval_bad) {
6333 /* evaluation was successful -- replace the node. */
6334 ir_type *old_tp = get_irn_type(n);
6335 int i, arity = get_irn_arity(n);
6338 * Try to recover the type of the new expression.
6340 for (i = 0; i < arity && !old_tp; ++i)
6341 old_tp = get_irn_type(get_irn_n(n, i));
6343 n = new_Const(get_tarval_mode(tv), tv);
6345 if (old_tp && get_type_mode(old_tp) == get_tarval_mode(tv))
6346 set_Const_type(n, old_tp);
6348 DBG_OPT_CSTEVAL(oldn, n);
6349 tarval_enable_fp_ops(old_fp_mode);
6352 tarval_enable_fp_ops(old_fp_mode);
6356 /* remove unnecessary nodes */
6357 if (get_opt_constant_folding() ||
6358 (iro == iro_Phi) || /* always optimize these nodes. */
6359 (iro == iro_Id) || /* ... */
6360 (iro == iro_Proj) || /* ... */
6361 (iro == iro_Block) ) /* Flags tested local. */
6362 n = equivalent_node(n);
6364 /** common subexpression elimination **/
6365 /* Checks whether n is already available. */
6366 /* The block input is used to distinguish different subexpressions. Right
6367 now all nodes are op_pin_state_pinned to blocks, i.e., the cse only finds common
6368 subexpressions within a block. */
6369 if (get_opt_cse()) {
6370 n = identify_remember(current_ir_graph->value_table, n);
6373 /* Some more constant expression evaluation. */
6374 iro = get_irn_opcode(n);
6375 if (get_opt_constant_folding() ||
6376 (iro == iro_Cond) ||
6377 (iro == iro_Proj)) /* Flags tested local. */
6378 n = transform_node(n);
6380 /* Remove nodes with dead (Bad) input.
6381 Run always for transformation induced Bads. */
6384 /* Now we can verify the node, as it has no dead inputs any more. */
6387 /* Now we have a legal, useful node. Enter it in hash table for cse.
6388 Blocks should be unique anyways. (Except the successor of start:
6389 is cse with the start block!) */
6390 if (get_opt_cse() && (get_irn_opcode(n) != iro_Block))
6391 n = identify_remember(current_ir_graph->value_table, n);
6394 } /* optimize_in_place_2 */
6397 * Wrapper for external use, set proper status bits after optimization.
6399 ir_node *optimize_in_place(ir_node *n) {
6400 /* Handle graph state */
6401 assert(get_irg_phase_state(current_ir_graph) != phase_building);
6403 if (get_opt_global_cse())
6404 set_irg_pinned(current_ir_graph, op_pin_state_floats);
6405 if (get_irg_outs_state(current_ir_graph) == outs_consistent)
6406 set_irg_outs_inconsistent(current_ir_graph);
6408 /* FIXME: Maybe we could also test whether optimizing the node can
6409 change the control graph. */
6410 set_irg_doms_inconsistent(current_ir_graph);
6411 return optimize_in_place_2(n);
6412 } /* optimize_in_place */
6415 * Calculate a hash value of a Const node.
6417 static unsigned hash_Const(const ir_node *node) {
6420 /* special value for const, as they only differ in their tarval. */
6421 h = HASH_PTR(node->attr.con.tv);
6422 h = 9*h + HASH_PTR(get_irn_mode(node));
6428 * Calculate a hash value of a SymConst node.
6430 static unsigned hash_SymConst(const ir_node *node) {
6433 /* special value for const, as they only differ in their symbol. */
6434 h = HASH_PTR(node->attr.symc.sym.type_p);
6435 h = 9*h + HASH_PTR(get_irn_mode(node));
6438 } /* hash_SymConst */
6441 * Set the default hash operation in an ir_op_ops.
6443 * @param code the opcode for the default operation
6444 * @param ops the operations initialized
6449 static ir_op_ops *firm_set_default_hash(ir_opcode code, ir_op_ops *ops)
6453 ops->hash = hash_##a; \
6456 /* hash function already set */
6457 if (ops->hash != NULL)
6464 /* use input/mode default hash if no function was given */
6465 ops->hash = firm_default_hash;
6473 * Sets the default operation for an ir_ops.
6475 ir_op_ops *firm_set_default_operations(ir_opcode code, ir_op_ops *ops) {
6476 ops = firm_set_default_hash(code, ops);
6477 ops = firm_set_default_computed_value(code, ops);
6478 ops = firm_set_default_equivalent_node(code, ops);
6479 ops = firm_set_default_transform_node(code, ops);
6480 ops = firm_set_default_node_cmp_attr(code, ops);
6481 ops = firm_set_default_get_type(code, ops);
6482 ops = firm_set_default_get_type_attr(code, ops);
6483 ops = firm_set_default_get_entity_attr(code, ops);
6486 } /* firm_set_default_operations */