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 void set_value_of_func(value_of_func func) {
72 value_of_ptr = default_value_of;
76 * Return the value of a Constant.
78 static tarval *computed_value_Const(ir_node *n) {
79 return get_Const_tarval(n);
80 } /* computed_value_Const */
83 * Return the value of a 'sizeof', 'alignof' or 'offsetof' SymConst.
85 static tarval *computed_value_SymConst(ir_node *n) {
89 switch (get_SymConst_kind(n)) {
90 case symconst_type_size:
91 type = get_SymConst_type(n);
92 if (get_type_state(type) == layout_fixed)
93 return new_tarval_from_long(get_type_size_bytes(type), get_irn_mode(n));
95 case symconst_type_align:
96 type = get_SymConst_type(n);
97 if (get_type_state(type) == layout_fixed)
98 return new_tarval_from_long(get_type_alignment_bytes(type), get_irn_mode(n));
100 case symconst_ofs_ent:
101 ent = get_SymConst_entity(n);
102 type = get_entity_owner(ent);
103 if (get_type_state(type) == layout_fixed)
104 return new_tarval_from_long(get_entity_offset(ent), get_irn_mode(n));
110 } /* computed_value_SymConst */
113 * Return the value of an Add.
115 static tarval *computed_value_Add(ir_node *n) {
116 ir_node *a = get_Add_left(n);
117 ir_node *b = get_Add_right(n);
119 tarval *ta = value_of(a);
120 tarval *tb = value_of(b);
122 if ((ta != tarval_bad) && (tb != tarval_bad) && (get_irn_mode(a) == get_irn_mode(b)))
123 return tarval_add(ta, tb);
126 } /* computed_value_Add */
129 * Return the value of a Sub.
130 * Special case: a - a
132 static tarval *computed_value_Sub(ir_node *n) {
133 ir_node *a = get_Sub_left(n);
134 ir_node *b = get_Sub_right(n);
139 if (a == b && !is_Bad(a))
140 return get_mode_null(get_irn_mode(n));
145 if ((ta != tarval_bad) && (tb != tarval_bad) && (get_irn_mode(a) == get_irn_mode(b)))
146 return tarval_sub(ta, tb);
149 } /* computed_value_Sub */
152 * Return the value of a Carry.
153 * Special : a op 0, 0 op b
155 static tarval *computed_value_Carry(ir_node *n) {
156 ir_node *a = get_binop_left(n);
157 ir_node *b = get_binop_right(n);
158 ir_mode *m = get_irn_mode(n);
160 tarval *ta = value_of(a);
161 tarval *tb = value_of(b);
163 if ((ta != tarval_bad) && (tb != tarval_bad)) {
165 return tarval_carry() ? get_mode_one(m) : get_mode_null(m);
167 if (tarval_is_null(ta) || tarval_is_null(tb))
168 return get_mode_null(m);
171 } /* computed_value_Carry */
174 * Return the value of a Borrow.
177 static tarval *computed_value_Borrow(ir_node *n) {
178 ir_node *a = get_binop_left(n);
179 ir_node *b = get_binop_right(n);
180 ir_mode *m = get_irn_mode(n);
182 tarval *ta = value_of(a);
183 tarval *tb = value_of(b);
185 if ((ta != tarval_bad) && (tb != tarval_bad)) {
186 return tarval_cmp(ta, tb) == pn_Cmp_Lt ? get_mode_one(m) : get_mode_null(m);
187 } else if (tarval_is_null(ta)) {
188 return get_mode_null(m);
191 } /* computed_value_Borrow */
194 * Return the value of an unary Minus.
196 static tarval *computed_value_Minus(ir_node *n) {
197 ir_node *a = get_Minus_op(n);
198 tarval *ta = value_of(a);
200 if (ta != tarval_bad)
201 return tarval_neg(ta);
204 } /* computed_value_Minus */
207 * Return the value of a Mul.
209 static tarval *computed_value_Mul(ir_node *n) {
210 ir_node *a = get_Mul_left(n);
211 ir_node *b = get_Mul_right(n);
214 tarval *ta = value_of(a);
215 tarval *tb = value_of(b);
217 mode = get_irn_mode(n);
218 if (mode != get_irn_mode(a)) {
219 /* n * n = 2n bit multiplication */
220 ta = tarval_convert_to(ta, mode);
221 tb = tarval_convert_to(tb, mode);
224 if (ta != tarval_bad && tb != tarval_bad) {
225 return tarval_mul(ta, tb);
227 /* a*0 = 0 or 0*b = 0 */
228 if (ta == get_mode_null(mode))
230 if (tb == get_mode_null(mode))
234 } /* computed_value_Mul */
237 * Return the value of a floating point Quot.
239 static tarval *computed_value_Quot(ir_node *n) {
240 ir_node *a = get_Quot_left(n);
241 ir_node *b = get_Quot_right(n);
243 tarval *ta = value_of(a);
244 tarval *tb = value_of(b);
246 /* This was missing in original implementation. Why? */
247 if ((ta != tarval_bad) && (tb != tarval_bad) && (get_irn_mode(a) == get_irn_mode(b))) {
248 if (tb != get_mode_null(get_tarval_mode(tb))) /* div by zero: return tarval_bad */
249 return tarval_quo(ta, tb);
252 } /* computed_value_Quot */
255 * Calculate the value of an integer Div of two nodes.
256 * Special case: 0 / b
258 static tarval *do_computed_value_Div(ir_node *a, ir_node *b) {
259 tarval *ta = value_of(a);
260 tarval *tb = value_of(b);
262 /* Compute c1 / c2 or 0 / a, a != 0 */
263 if (ta != tarval_bad) {
264 if ((tb != tarval_bad) && (tb != get_mode_null(get_irn_mode(b)))) /* div by zero: return tarval_bad */
265 return tarval_div(ta, tb);
266 else if (ta == get_mode_null(get_tarval_mode(ta))) /* 0 / b == 0 */
270 } /* do_computed_value_Div */
273 * Return the value of an integer Div.
275 static tarval *computed_value_Div(ir_node *n) {
276 return do_computed_value_Div(get_Div_left(n), get_Div_right(n));
277 } /* computed_value_Div */
280 * Calculate the value of an integer Mod of two nodes.
281 * Special case: a % 1
283 static tarval *do_computed_value_Mod(ir_node *a, ir_node *b) {
284 tarval *ta = value_of(a);
285 tarval *tb = value_of(b);
287 /* Compute c1 % c2 or a % 1 */
288 if (tb != tarval_bad) {
289 if ((ta != tarval_bad) && (tb != get_mode_null(get_tarval_mode(tb)))) /* div by zero: return tarval_bad */
290 return tarval_mod(ta, tb);
291 else if (tb == get_mode_one(get_tarval_mode(tb))) /* x mod 1 == 0 */
292 return get_mode_null(get_irn_mode(a));
295 } /* do_computed_value_Mod */
298 * Return the value of an integer Mod.
300 static tarval *computed_value_Mod(ir_node *n) {
301 return do_computed_value_Mod(get_Mod_left(n), get_Mod_right(n));
302 } /* computed_value_Mod */
305 * Return the value of an Abs.
307 static tarval *computed_value_Abs(ir_node *n) {
308 ir_node *a = get_Abs_op(n);
309 tarval *ta = value_of(a);
311 if (ta != tarval_bad)
312 return tarval_abs(ta);
315 } /* computed_value_Abs */
318 * Return the value of an And.
319 * Special case: a & 0, 0 & b
321 static tarval *computed_value_And(ir_node *n) {
322 ir_node *a = get_And_left(n);
323 ir_node *b = get_And_right(n);
325 tarval *ta = value_of(a);
326 tarval *tb = value_of(b);
328 if ((ta != tarval_bad) && (tb != tarval_bad)) {
329 return tarval_and (ta, tb);
331 if (tarval_is_null(ta)) return ta;
332 if (tarval_is_null(tb)) return tb;
335 } /* computed_value_And */
338 * Return the value of an Or.
339 * Special case: a | 1...1, 1...1 | b
341 static tarval *computed_value_Or(ir_node *n) {
342 ir_node *a = get_Or_left(n);
343 ir_node *b = get_Or_right(n);
345 tarval *ta = value_of(a);
346 tarval *tb = value_of(b);
348 if ((ta != tarval_bad) && (tb != tarval_bad)) {
349 return tarval_or (ta, tb);
351 if (tarval_is_all_one(ta)) return ta;
352 if (tarval_is_all_one(tb)) return tb;
355 } /* computed_value_Or */
358 * Return the value of an Eor.
360 static tarval *computed_value_Eor(ir_node *n) {
361 ir_node *a = get_Eor_left(n);
362 ir_node *b = get_Eor_right(n);
367 return get_mode_null(get_irn_mode(n));
372 if ((ta != tarval_bad) && (tb != tarval_bad)) {
373 return tarval_eor (ta, tb);
376 } /* computed_value_Eor */
379 * Return the value of a Not.
381 static tarval *computed_value_Not(ir_node *n) {
382 ir_node *a = get_Not_op(n);
383 tarval *ta = value_of(a);
385 if (ta != tarval_bad)
386 return tarval_not(ta);
389 } /* computed_value_Not */
392 * Return the value of a Shl.
394 static tarval *computed_value_Shl(ir_node *n) {
395 ir_node *a = get_Shl_left(n);
396 ir_node *b = get_Shl_right(n);
398 tarval *ta = value_of(a);
399 tarval *tb = value_of(b);
401 if ((ta != tarval_bad) && (tb != tarval_bad)) {
402 return tarval_shl (ta, tb);
405 } /* computed_value_Shl */
408 * Return the value of a Shr.
410 static tarval *computed_value_Shr(ir_node *n) {
411 ir_node *a = get_Shr_left(n);
412 ir_node *b = get_Shr_right(n);
414 tarval *ta = value_of(a);
415 tarval *tb = value_of(b);
417 if ((ta != tarval_bad) && (tb != tarval_bad)) {
418 return tarval_shr (ta, tb);
421 } /* computed_value_Shr */
424 * Return the value of a Shrs.
426 static tarval *computed_value_Shrs(ir_node *n) {
427 ir_node *a = get_Shrs_left(n);
428 ir_node *b = get_Shrs_right(n);
430 tarval *ta = value_of(a);
431 tarval *tb = value_of(b);
433 if ((ta != tarval_bad) && (tb != tarval_bad)) {
434 return tarval_shrs (ta, tb);
437 } /* computed_value_Shrs */
440 * Return the value of a Rotl.
442 static tarval *computed_value_Rotl(ir_node *n) {
443 ir_node *a = get_Rotl_left(n);
444 ir_node *b = get_Rotl_right(n);
446 tarval *ta = value_of(a);
447 tarval *tb = value_of(b);
449 if ((ta != tarval_bad) && (tb != tarval_bad)) {
450 return tarval_rotl(ta, tb);
453 } /* computed_value_Rotl */
456 * Return the value of a Conv.
458 static tarval *computed_value_Conv(ir_node *n) {
459 ir_node *a = get_Conv_op(n);
460 tarval *ta = value_of(a);
462 if (ta != tarval_bad)
463 return tarval_convert_to(ta, get_irn_mode(n));
466 } /* computed_value_Conv */
469 * Return the value of a Proj(Cmp).
471 * This performs a first step of unreachable code elimination.
472 * Proj can not be computed, but folding a Cmp above the Proj here is
473 * not as wasteful as folding a Cmp into a Tuple of 16 Consts of which
475 * There are several case where we can evaluate a Cmp node, see later.
477 static tarval *computed_value_Proj_Cmp(ir_node *n) {
478 ir_node *a = get_Proj_pred(n);
479 ir_node *aa = get_Cmp_left(a);
480 ir_node *ab = get_Cmp_right(a);
481 long proj_nr = get_Proj_proj(n);
484 * BEWARE: a == a is NOT always True for floating Point values, as
485 * NaN != NaN is defined, so we must check this here.
488 !mode_is_float(get_irn_mode(aa)) || proj_nr == pn_Cmp_Lt || proj_nr == pn_Cmp_Gt)
491 /* This is a trick with the bits used for encoding the Cmp
492 Proj numbers, the following statement is not the same:
493 return new_tarval_from_long (proj_nr == pn_Cmp_Eq, mode_b) */
494 return new_tarval_from_long (proj_nr & pn_Cmp_Eq, mode_b);
497 tarval *taa = value_of(aa);
498 tarval *tab = value_of(ab);
499 ir_mode *mode = get_irn_mode(aa);
502 * The predecessors of Cmp are target values. We can evaluate
505 if ((taa != tarval_bad) && (tab != tarval_bad)) {
506 /* strange checks... */
507 pn_Cmp flags = tarval_cmp(taa, tab);
508 if (flags != pn_Cmp_False) {
509 return new_tarval_from_long (proj_nr & flags, mode_b);
512 /* for integer values, we can check against MIN/MAX */
513 else if (mode_is_int(mode)) {
514 /* MIN <=/> x. This results in true/false. */
515 if (taa == get_mode_min(mode)) {
516 /* a compare with the MIN value */
517 if (proj_nr == pn_Cmp_Le)
518 return get_tarval_b_true();
519 else if (proj_nr == pn_Cmp_Gt)
520 return get_tarval_b_false();
522 /* x >=/< MIN. This results in true/false. */
524 if (tab == get_mode_min(mode)) {
525 /* a compare with the MIN value */
526 if (proj_nr == pn_Cmp_Ge)
527 return get_tarval_b_true();
528 else if (proj_nr == pn_Cmp_Lt)
529 return get_tarval_b_false();
531 /* MAX >=/< x. This results in true/false. */
532 else if (taa == get_mode_max(mode)) {
533 if (proj_nr == pn_Cmp_Ge)
534 return get_tarval_b_true();
535 else if (proj_nr == pn_Cmp_Lt)
536 return get_tarval_b_false();
538 /* x <=/> MAX. This results in true/false. */
539 else if (tab == get_mode_max(mode)) {
540 if (proj_nr == pn_Cmp_Le)
541 return get_tarval_b_true();
542 else if (proj_nr == pn_Cmp_Gt)
543 return get_tarval_b_false();
547 * The predecessors are Allocs or (void*)(0) constants. Allocs never
548 * return NULL, they raise an exception. Therefore we can predict
552 ir_node *aaa = skip_Id(skip_Proj(aa));
553 ir_node *aba = skip_Id(skip_Proj(ab));
555 if ( ( (/* aa is ProjP and aaa is Alloc */
557 && mode_is_reference(get_irn_mode(aa))
559 && ( (/* ab is NULL */
561 && mode_is_reference(get_irn_mode(ab))
562 && is_Const_null(ab))
563 || (/* ab is other Alloc */
565 && mode_is_reference(get_irn_mode(ab))
568 || (/* aa is NULL and aba is Alloc */
570 && mode_is_reference(get_irn_mode(aa))
573 && mode_is_reference(get_irn_mode(ab))
576 return new_tarval_from_long(proj_nr & pn_Cmp_Ne, mode_b);
579 return computed_value_Cmp_Confirm(a, aa, ab, proj_nr);
580 } /* computed_value_Proj_Cmp */
583 * Return the value of a Proj, handle Proj(Cmp), Proj(Div), Proj(Mod),
584 * Proj(DivMod) and Proj(Quot).
586 static tarval *computed_value_Proj(ir_node *n) {
587 ir_node *a = get_Proj_pred(n);
590 switch (get_irn_opcode(a)) {
592 return computed_value_Proj_Cmp(n);
595 /* compute either the Div or the Mod part */
596 proj_nr = get_Proj_proj(n);
597 if (proj_nr == pn_DivMod_res_div)
598 return do_computed_value_Div(get_DivMod_left(a), get_DivMod_right(a));
599 else if (proj_nr == pn_DivMod_res_mod)
600 return do_computed_value_Mod(get_DivMod_left(a), get_DivMod_right(a));
604 if (get_Proj_proj(n) == pn_Div_res)
605 return computed_value(a);
609 if (get_Proj_proj(n) == pn_Mod_res)
610 return computed_value(a);
614 if (get_Proj_proj(n) == pn_Quot_res)
615 return computed_value(a);
622 } /* computed_value_Proj */
625 * Calculate the value of a Mux: can be evaluated, if the
626 * sel and the right input are known.
628 static tarval *computed_value_Mux(ir_node *n) {
629 ir_node *sel = get_Mux_sel(n);
630 tarval *ts = value_of(sel);
632 if (ts == get_tarval_b_true()) {
633 ir_node *v = get_Mux_true(n);
636 else if (ts == get_tarval_b_false()) {
637 ir_node *v = get_Mux_false(n);
641 } /* computed_value_Mux */
644 * Calculate the value of a Psi: can be evaluated, if a condition is true
645 * and all previous conditions are false. If all conditions are false
646 * we evaluate to the default one.
648 static tarval *computed_value_Psi(ir_node *n) {
650 return computed_value_Mux(n);
652 } /* computed_value_Psi */
655 * Calculate the value of a Confirm: can be evaluated,
656 * if it has the form Confirm(x, '=', Const).
658 static tarval *computed_value_Confirm(ir_node *n) {
660 * Beware: we might produce Phi(Confirm(x == true), Confirm(x == false)).
661 * Do NOT optimize them away (CondEval wants them), so wait until
662 * remove_confirm is activated.
664 if (get_opt_remove_confirm()) {
665 return get_Confirm_cmp(n) == pn_Cmp_Eq ?
666 value_of(get_Confirm_bound(n)) : tarval_bad;
669 } /* computed_value_Confirm */
672 * If the parameter n can be computed, return its value, else tarval_bad.
673 * Performs constant folding.
675 * @param n The node this should be evaluated
677 tarval *computed_value(ir_node *n) {
678 if (n->op->ops.computed_value)
679 return n->op->ops.computed_value(n);
681 } /* computed_value */
684 * Set the default computed_value evaluator in an ir_op_ops.
686 * @param code the opcode for the default operation
687 * @param ops the operations initialized
692 static ir_op_ops *firm_set_default_computed_value(ir_opcode code, ir_op_ops *ops)
696 ops->computed_value = computed_value_##a; \
731 } /* firm_set_default_computed_value */
734 * Returns a equivalent block for another block.
735 * If the block has only one predecessor, this is
736 * the equivalent one. If the only predecessor of a block is
737 * the block itself, this is a dead block.
739 * If both predecessors of a block are the branches of a binary
740 * Cond, the equivalent block is Cond's block.
742 * If all predecessors of a block are bad or lies in a dead
743 * block, the current block is dead as well.
745 * Note, that blocks are NEVER turned into Bad's, instead
746 * the dead_block flag is set. So, never test for is_Bad(block),
747 * always use is_dead_Block(block).
749 static ir_node *equivalent_node_Block(ir_node *n)
754 /* don't optimize dead blocks */
755 if (is_Block_dead(n))
758 n_preds = get_Block_n_cfgpreds(n);
760 /* The Block constructor does not call optimize, but mature_immBlock()
761 calls the optimization. */
762 assert(get_Block_matured(n));
764 /* Straightening: a single entry Block following a single exit Block
765 can be merged, if it is not the Start block. */
766 /* !!! Beware, all Phi-nodes of n must have been optimized away.
767 This should be true, as the block is matured before optimize is called.
768 But what about Phi-cycles with the Phi0/Id that could not be resolved?
769 Remaining Phi nodes are just Ids. */
770 if (n_preds == 1 && is_Jmp(get_Block_cfgpred(n, 0))) {
771 ir_node *predblock = get_nodes_block(get_Block_cfgpred(n, 0));
772 if (predblock == oldn) {
773 /* Jmp jumps into the block it is in -- deal self cycle. */
774 n = set_Block_dead(n);
775 DBG_OPT_DEAD_BLOCK(oldn, n);
776 } else if (get_opt_control_flow_straightening()) {
778 DBG_OPT_STG(oldn, n);
780 } else if (n_preds == 1 && is_Cond(skip_Proj(get_Block_cfgpred(n, 0)))) {
781 ir_node *predblock = get_Block_cfgpred_block(n, 0);
782 if (predblock == oldn) {
783 /* Jmp jumps into the block it is in -- deal self cycle. */
784 n = set_Block_dead(n);
785 DBG_OPT_DEAD_BLOCK(oldn, n);
787 } else if ((n_preds == 2) &&
788 (get_opt_control_flow_weak_simplification())) {
789 /* Test whether Cond jumps twice to this block
790 * The more general case which more than 2 predecessors is handles
791 * in optimize_cf(), we handle only this special case for speed here.
793 ir_node *a = get_Block_cfgpred(n, 0);
794 ir_node *b = get_Block_cfgpred(n, 1);
798 (get_Proj_pred(a) == get_Proj_pred(b)) &&
799 is_Cond(get_Proj_pred(a)) &&
800 (get_irn_mode(get_Cond_selector(get_Proj_pred(a))) == mode_b)) {
801 /* Also a single entry Block following a single exit Block. Phis have
802 twice the same operand and will be optimized away. */
803 n = get_nodes_block(get_Proj_pred(a));
804 DBG_OPT_IFSIM1(oldn, a, b, n);
806 } else if (get_opt_unreachable_code() &&
807 (n != get_irg_start_block(current_ir_graph)) &&
808 (n != get_irg_end_block(current_ir_graph)) ) {
811 /* If all inputs are dead, this block is dead too, except if it is
812 the start or end block. This is one step of unreachable code
814 for (i = get_Block_n_cfgpreds(n) - 1; i >= 0; --i) {
815 ir_node *pred = get_Block_cfgpred(n, i);
818 if (is_Bad(pred)) continue;
819 pred_blk = get_nodes_block(skip_Proj(pred));
821 if (is_Block_dead(pred_blk)) continue;
824 /* really found a living input */
829 n = set_Block_dead(n);
830 DBG_OPT_DEAD_BLOCK(oldn, n);
835 } /* equivalent_node_Block */
838 * Returns a equivalent node for a Jmp, a Bad :-)
839 * Of course this only happens if the Block of the Jmp is dead.
841 static ir_node *equivalent_node_Jmp(ir_node *n) {
842 /* unreachable code elimination */
843 if (is_Block_dead(get_nodes_block(n)))
847 } /* equivalent_node_Jmp */
849 /** Raise is handled in the same way as Jmp. */
850 #define equivalent_node_Raise equivalent_node_Jmp
853 /* We do not evaluate Cond here as we replace it by a new node, a Jmp.
854 See transform_node_Proj_Cond(). */
857 * Optimize operations that are commutative and have neutral 0,
858 * so a op 0 = 0 op a = a.
860 static ir_node *equivalent_node_neutral_zero(ir_node *n)
864 ir_node *a = get_binop_left(n);
865 ir_node *b = get_binop_right(n);
870 /* After running compute_node there is only one constant predecessor.
871 Find this predecessors value and remember the other node: */
872 if ((tv = value_of(a)) != tarval_bad) {
874 } else if ((tv = value_of(b)) != tarval_bad) {
879 /* If this predecessors constant value is zero, the operation is
880 * unnecessary. Remove it.
882 * Beware: If n is a Add, the mode of on and n might be different
883 * which happens in this rare construction: NULL + 3.
884 * Then, a Conv would be needed which we cannot include here.
886 if (tarval_is_null(tv) && get_irn_mode(on) == get_irn_mode(n)) {
889 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_0);
893 } /* equivalent_node_neutral_zero */
896 * Eor is commutative and has neutral 0.
898 static ir_node *equivalent_node_Eor(ir_node *n)
904 n = equivalent_node_neutral_zero(n);
905 if (n != oldn) return n;
908 b = get_Eor_right(n);
911 ir_node *aa = get_Eor_left(a);
912 ir_node *ab = get_Eor_right(a);
915 /* (a ^ b) ^ a -> b */
917 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_EOR_A_B_A);
919 } else if (ab == b) {
920 /* (a ^ b) ^ b -> a */
922 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_EOR_A_B_A);
927 ir_node *ba = get_Eor_left(b);
928 ir_node *bb = get_Eor_right(b);
931 /* a ^ (a ^ b) -> b */
933 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_EOR_A_B_A);
935 } else if (bb == a) {
936 /* a ^ (b ^ a) -> b */
938 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_EOR_A_B_A);
947 * Optimize a - 0 and (a - x) + x (for modes with wrap-around).
949 * The second one looks strange, but this construct
950 * is used heavily in the LCC sources :-).
952 * Beware: The Mode of an Add may be different than the mode of its
953 * predecessors, so we could not return a predecessors in all cases.
955 static ir_node *equivalent_node_Add(ir_node *n) {
957 ir_node *left, *right;
958 ir_mode *mode = get_irn_mode(n);
960 n = equivalent_node_neutral_zero(n);
964 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
965 if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
968 left = get_Add_left(n);
969 right = get_Add_right(n);
972 if (get_Sub_right(left) == right) {
975 n = get_Sub_left(left);
976 if (mode == get_irn_mode(n)) {
977 DBG_OPT_ALGSIM1(oldn, left, right, n, FS_OPT_ADD_SUB);
983 if (get_Sub_right(right) == left) {
986 n = get_Sub_left(right);
987 if (mode == get_irn_mode(n)) {
988 DBG_OPT_ALGSIM1(oldn, left, right, n, FS_OPT_ADD_SUB);
994 } /* equivalent_node_Add */
997 * optimize operations that are not commutative but have neutral 0 on left,
1000 static ir_node *equivalent_node_left_zero(ir_node *n) {
1003 ir_node *a = get_binop_left(n);
1004 ir_node *b = get_binop_right(n);
1006 if (is_Const(b) && is_Const_null(b)) {
1009 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_0);
1012 } /* equivalent_node_left_zero */
1014 #define equivalent_node_Shl equivalent_node_left_zero
1015 #define equivalent_node_Shr equivalent_node_left_zero
1016 #define equivalent_node_Shrs equivalent_node_left_zero
1017 #define equivalent_node_Rotl equivalent_node_left_zero
1020 * Optimize a - 0 and (a + x) - x (for modes with wrap-around).
1022 * The second one looks strange, but this construct
1023 * is used heavily in the LCC sources :-).
1025 * Beware: The Mode of a Sub may be different than the mode of its
1026 * predecessors, so we could not return a predecessors in all cases.
1028 static ir_node *equivalent_node_Sub(ir_node *n) {
1031 ir_mode *mode = get_irn_mode(n);
1033 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
1034 if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
1037 b = get_Sub_right(n);
1039 /* Beware: modes might be different */
1040 if (is_Const(b) && is_Const_null(b)) {
1041 ir_node *a = get_Sub_left(n);
1042 if (mode == get_irn_mode(a)) {
1045 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_0);
1049 } /* equivalent_node_Sub */
1053 * Optimize an "self-inverse unary op", ie op(op(n)) = n.
1056 * -(-a) == a, but might overflow two times.
1057 * We handle it anyway here but the better way would be a
1058 * flag. This would be needed for Pascal for instance.
1060 static ir_node *equivalent_node_idempotent_unop(ir_node *n) {
1062 ir_node *pred = get_unop_op(n);
1064 /* optimize symmetric unop */
1065 if (get_irn_op(pred) == get_irn_op(n)) {
1066 n = get_unop_op(pred);
1067 DBG_OPT_ALGSIM2(oldn, pred, n, FS_OPT_IDEM_UNARY);
1070 } /* equivalent_node_idempotent_unop */
1072 /** Optimize Not(Not(x)) == x. */
1073 #define equivalent_node_Not equivalent_node_idempotent_unop
1075 /** -(-x) == x ??? Is this possible or can --x raise an
1076 out of bounds exception if min =! max? */
1077 #define equivalent_node_Minus equivalent_node_idempotent_unop
1080 * Optimize a * 1 = 1 * a = a.
1082 static ir_node *equivalent_node_Mul(ir_node *n) {
1084 ir_node *a = get_Mul_left(n);
1086 /* we can handle here only the n * n = n bit cases */
1087 if (get_irn_mode(n) == get_irn_mode(a)) {
1088 ir_node *b = get_Mul_right(n);
1090 /* Mul is commutative and has again an other neutral element. */
1091 if (is_Const(a) && is_Const_one(a)) {
1093 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
1094 } else if (is_Const(b) && is_Const_one(b)) {
1096 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
1100 } /* equivalent_node_Mul */
1103 * Optimize a / 1 = a.
1105 static ir_node *equivalent_node_Div(ir_node *n) {
1106 ir_node *a = get_Div_left(n);
1107 ir_node *b = get_Div_right(n);
1109 /* Div is not commutative. */
1110 if (is_Const(b) && is_Const_one(b)) { /* div(x, 1) == x */
1111 /* Turn Div into a tuple (mem, bad, a) */
1112 ir_node *mem = get_Div_mem(n);
1113 ir_node *blk = get_irn_n(n, -1);
1114 turn_into_tuple(n, pn_Div_max);
1115 set_Tuple_pred(n, pn_Div_M, mem);
1116 set_Tuple_pred(n, pn_Div_X_regular, new_r_Jmp(current_ir_graph, blk));
1117 set_Tuple_pred(n, pn_Div_X_except, new_Bad()); /* no exception */
1118 set_Tuple_pred(n, pn_Div_res, a);
1121 } /* equivalent_node_Div */
1124 * Optimize a / 1.0 = a.
1126 static ir_node *equivalent_node_Quot(ir_node *n) {
1127 ir_node *a = get_Quot_left(n);
1128 ir_node *b = get_Quot_right(n);
1130 /* Div is not commutative. */
1131 if (is_Const(b) && is_Const_one(b)) { /* Quot(x, 1) == x */
1132 /* Turn Quot into a tuple (mem, jmp, bad, a) */
1133 ir_node *mem = get_Quot_mem(n);
1134 ir_node *blk = get_irn_n(n, -1);
1135 turn_into_tuple(n, pn_Quot_max);
1136 set_Tuple_pred(n, pn_Quot_M, mem);
1137 set_Tuple_pred(n, pn_Quot_X_regular, new_r_Jmp(current_ir_graph, blk));
1138 set_Tuple_pred(n, pn_Quot_X_except, new_Bad()); /* no exception */
1139 set_Tuple_pred(n, pn_Quot_res, a);
1142 } /* equivalent_node_Quot */
1145 * Optimize a / 1 = a.
1147 static ir_node *equivalent_node_DivMod(ir_node *n) {
1148 ir_node *b = get_DivMod_right(n);
1150 /* Div is not commutative. */
1151 if (is_Const(b) && is_Const_one(b)) { /* div(x, 1) == x */
1152 /* Turn DivMod into a tuple (mem, jmp, bad, a, 0) */
1153 ir_node *a = get_DivMod_left(n);
1154 ir_node *mem = get_Div_mem(n);
1155 ir_node *blk = get_irn_n(n, -1);
1156 ir_mode *mode = get_DivMod_resmode(n);
1158 turn_into_tuple(n, pn_DivMod_max);
1159 set_Tuple_pred(n, pn_DivMod_M, mem);
1160 set_Tuple_pred(n, pn_DivMod_X_regular, new_r_Jmp(current_ir_graph, blk));
1161 set_Tuple_pred(n, pn_DivMod_X_except, new_Bad()); /* no exception */
1162 set_Tuple_pred(n, pn_DivMod_res_div, a);
1163 set_Tuple_pred(n, pn_DivMod_res_mod, new_Const(mode, get_mode_null(mode)));
1166 } /* equivalent_node_DivMod */
1169 * Use algebraic simplification a | a = a | 0 = 0 | a = a.
1171 static ir_node *equivalent_node_Or(ir_node *n) {
1174 ir_node *a = get_Or_left(n);
1175 ir_node *b = get_Or_right(n);
1178 n = a; /* Or has it's own neutral element */
1179 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_OR);
1180 } else if (is_Const(a) && is_Const_null(a)) {
1182 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_OR);
1183 } else if (is_Const(b) && is_Const_null(b)) {
1185 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_OR);
1189 } /* equivalent_node_Or */
1192 * Optimize a & 0b1...1 = 0b1...1 & a = a & a = (a|X) & a = a.
1194 static ir_node *equivalent_node_And(ir_node *n) {
1197 ir_node *a = get_And_left(n);
1198 ir_node *b = get_And_right(n);
1201 n = a; /* And has it's own neutral element */
1202 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_AND);
1205 if (is_Const(a) && is_Const_all_one(a)) {
1207 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1210 if (is_Const(b) && is_Const_all_one(b)) {
1212 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1216 if (b == get_Or_left(a) || b == get_Or_right(a)) {
1219 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1224 if (a == get_Or_left(b) || a == get_Or_right(b)) {
1227 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1233 } /* equivalent_node_And */
1236 * Try to remove useless Conv's:
1238 static ir_node *equivalent_node_Conv(ir_node *n) {
1240 ir_node *a = get_Conv_op(n);
1242 ir_mode *n_mode = get_irn_mode(n);
1243 ir_mode *a_mode = get_irn_mode(a);
1246 if (n_mode == a_mode) { /* No Conv necessary */
1247 if (get_Conv_strict(n)) {
1248 /* special case: the predecessor might be a also a Conv */
1250 if (! get_Conv_strict(a)) {
1251 /* first one is not strict, kick it */
1253 a_mode = get_irn_mode(a);
1257 /* else both are strict conv, second is superfluous */
1258 } else if (is_Proj(a)) {
1259 ir_node *pred = get_Proj_pred(a);
1260 if (is_Load(pred)) {
1261 /* loads always return with the exact precision of n_mode */
1262 assert(get_Load_mode(pred) == n_mode);
1267 /* leave strict floating point Conv's */
1271 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CONV);
1272 } else if (is_Conv(a)) { /* Conv(Conv(b)) */
1273 ir_node *b = get_Conv_op(a);
1274 ir_mode *b_mode = get_irn_mode(b);
1276 if (get_Conv_strict(n) && get_Conv_strict(a)) {
1277 /* both are strict conv */
1278 if (smaller_mode(a_mode, n_mode)) {
1279 /* both are strict, but the first is smaller, so
1280 the second cannot remove more precision, remove the
1282 set_Conv_strict(n, 0);
1285 if (n_mode == b_mode) {
1286 if (! get_Conv_strict(n) && ! get_Conv_strict(a)) {
1287 if (n_mode == mode_b) {
1288 n = b; /* Convb(Conv*(xxxb(...))) == xxxb(...) */
1289 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
1290 } else if (get_mode_arithmetic(n_mode) == get_mode_arithmetic(a_mode)) {
1291 if (smaller_mode(b_mode, a_mode)) {
1292 n = b; /* ConvS(ConvL(xxxS(...))) == xxxS(...) */
1293 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
1298 if (smaller_mode(b_mode, a_mode)) {
1299 if (get_Conv_strict(n))
1300 set_Conv_strict(b, 1);
1301 n = b; /* ConvA(ConvB(ConvA(...))) == ConvA(...) */
1302 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
1308 } /* equivalent_node_Conv */
1311 * A Cast may be removed if the type of the previous node
1312 * is already the type of the Cast.
1314 static ir_node *equivalent_node_Cast(ir_node *n) {
1316 ir_node *pred = get_Cast_op(n);
1318 if (get_irn_type(pred) == get_Cast_type(n)) {
1320 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CAST);
1323 } /* equivalent_node_Cast */
1326 * Several optimizations:
1327 * - no Phi in start block.
1328 * - remove Id operators that are inputs to Phi
1329 * - fold Phi-nodes, iff they have only one predecessor except
1332 static ir_node *equivalent_node_Phi(ir_node *n) {
1337 ir_node *first_val = NULL; /* to shutup gcc */
1339 if (!get_opt_normalize()) return n;
1341 n_preds = get_Phi_n_preds(n);
1343 block = get_nodes_block(n);
1344 if ((is_Block_dead(block)) || /* Control dead */
1345 (block == get_irg_start_block(current_ir_graph))) /* There should be no Phi nodes */
1346 return new_Bad(); /* in the Start Block. */
1348 if (n_preds == 0) return n; /* Phi of dead Region without predecessors. */
1350 /* If the Block has a Bad pred, we also have one. */
1351 for (i = 0; i < n_preds; ++i)
1352 if (is_Bad(get_Block_cfgpred(block, i)))
1353 set_Phi_pred(n, i, new_Bad());
1355 /* Find first non-self-referencing input */
1356 for (i = 0; i < n_preds; ++i) {
1357 first_val = get_Phi_pred(n, i);
1358 if ( (first_val != n) /* not self pointer */
1360 /* BEWARE: when the if is changed to 1, Phi's will ignore it's Bad
1361 * predecessors. Then, Phi nodes in dead code might be removed, causing
1362 * nodes pointing to themself (Add's for instance).
1363 * This is really bad and causes endless recursions in several
1364 * code pathes, so we do NOT optimize such a code.
1365 * This is not that bad as it sounds, optimize_cf() removes bad control flow
1366 * (and bad Phi predecessors), so live code is optimized later.
1368 && (! is_Bad(first_val))
1370 ) { /* value not dead */
1371 break; /* then found first value. */
1376 /* A totally Bad or self-referencing Phi (we didn't break the above loop) */
1380 /* search for rest of inputs, determine if any of these
1381 are non-self-referencing */
1382 while (++i < n_preds) {
1383 ir_node *scnd_val = get_Phi_pred(n, i);
1384 if ( (scnd_val != n)
1385 && (scnd_val != first_val)
1388 && (! is_Bad(scnd_val))
1396 /* Fold, if no multiple distinct non-self-referencing inputs */
1398 DBG_OPT_PHI(oldn, n);
1401 } /* equivalent_node_Phi */
1404 * Several optimizations:
1405 * - no Sync in start block.
1406 * - fold Sync-nodes, iff they have only one predecessor except
1409 static ir_node *equivalent_node_Sync(ir_node *n) {
1410 int arity = get_Sync_n_preds(n);
1413 for (i = 0; i < arity;) {
1414 ir_node *pred = get_Sync_pred(n, i);
1417 /* Remove Bad predecessors */
1424 /* Remove duplicate predecessors */
1430 if (get_Sync_pred(n, j) == pred) {
1438 if (arity == 0) return new_Bad();
1439 if (arity == 1) return get_Sync_pred(n, 0);
1441 } /* equivalent_node_Sync */
1444 * Optimize Proj(Tuple) and gigo() for ProjX in Bad block,
1445 * ProjX(Load) and ProjX(Store).
1447 static ir_node *equivalent_node_Proj(ir_node *proj) {
1448 ir_node *oldn = proj;
1449 ir_node *a = get_Proj_pred(proj);
1452 /* Remove the Tuple/Proj combination. */
1453 if ( get_Proj_proj(proj) <= get_Tuple_n_preds(a) ) {
1454 proj = get_Tuple_pred(a, get_Proj_proj(proj));
1455 DBG_OPT_TUPLE(oldn, a, proj);
1457 /* This should not happen! */
1458 assert(! "found a Proj with higher number than Tuple predecessors");
1461 } else if (get_irn_mode(proj) == mode_X) {
1462 if (is_Block_dead(get_nodes_block(skip_Proj(proj)))) {
1463 /* Remove dead control flow -- early gigo(). */
1465 } else if (get_opt_ldst_only_null_ptr_exceptions()) {
1467 /* get the Load address */
1468 ir_node *addr = get_Load_ptr(a);
1469 ir_node *blk = get_irn_n(a, -1);
1472 if (value_not_null(addr, &confirm)) {
1473 if (confirm == NULL) {
1474 /* this node may float if it did not depend on a Confirm */
1475 set_irn_pinned(a, op_pin_state_floats);
1477 if (get_Proj_proj(proj) == pn_Load_X_except) {
1478 DBG_OPT_EXC_REM(proj);
1481 return new_r_Jmp(current_ir_graph, blk);
1483 } else if (is_Store(a)) {
1484 /* get the load/store address */
1485 ir_node *addr = get_Store_ptr(a);
1486 ir_node *blk = get_irn_n(a, -1);
1489 if (value_not_null(addr, &confirm)) {
1490 if (confirm == NULL) {
1491 /* this node may float if it did not depend on a Confirm */
1492 set_irn_pinned(a, op_pin_state_floats);
1494 if (get_Proj_proj(proj) == pn_Store_X_except) {
1495 DBG_OPT_EXC_REM(proj);
1498 return new_r_Jmp(current_ir_graph, blk);
1505 } /* equivalent_node_Proj */
1510 static ir_node *equivalent_node_Id(ir_node *n) {
1515 } while (get_irn_op(n) == op_Id);
1517 DBG_OPT_ID(oldn, n);
1519 } /* equivalent_node_Id */
1524 static ir_node *equivalent_node_Mux(ir_node *n)
1526 ir_node *oldn = n, *sel = get_Mux_sel(n);
1527 tarval *ts = value_of(sel);
1529 /* Mux(true, f, t) == t */
1530 if (ts == tarval_b_true) {
1531 n = get_Mux_true(n);
1532 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_C);
1534 /* Mux(false, f, t) == f */
1535 else if (ts == tarval_b_false) {
1536 n = get_Mux_false(n);
1537 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_C);
1539 /* Mux(v, x, x) == x */
1540 else if (get_Mux_false(n) == get_Mux_true(n)) {
1541 n = get_Mux_true(n);
1542 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_EQ);
1544 else if (is_Proj(sel) && !mode_honor_signed_zeros(get_irn_mode(n))) {
1545 ir_node *cmp = get_Proj_pred(sel);
1546 long proj_nr = get_Proj_proj(sel);
1547 ir_node *f = get_Mux_false(n);
1548 ir_node *t = get_Mux_true(n);
1551 * Note further that these optimization work even for floating point
1552 * with NaN's because -NaN == NaN.
1553 * However, if +0 and -0 is handled differently, we cannot use the first one.
1556 ir_node *const cmp_l = get_Cmp_left(cmp);
1557 ir_node *const cmp_r = get_Cmp_right(cmp);
1561 if ((cmp_l == t && cmp_r == f) || /* Psi(t == f, t, f) -> f */
1562 (cmp_l == f && cmp_r == t)) { /* Psi(f == t, t, f) -> f */
1564 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1571 if ((cmp_l == t && cmp_r == f) || /* Psi(t != f, t, f) -> t */
1572 (cmp_l == f && cmp_r == t)) { /* Psi(f != t, t, f) -> t */
1574 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1581 * Note: normalization puts the constant on the right side,
1582 * so we check only one case.
1584 if (cmp_l == t && is_Const(cmp_r) && is_Const_null(cmp_r)) {
1585 /* Mux(t CMP 0, X, t) */
1586 if (is_Minus(f) && get_Minus_op(f) == t) {
1587 /* Mux(t CMP 0, -t, t) */
1588 if (proj_nr == pn_Cmp_Eq) {
1589 /* Mux(t == 0, -t, t) ==> -t */
1591 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1592 } else if (proj_nr == pn_Cmp_Lg || proj_nr == pn_Cmp_Ne) {
1593 /* Mux(t != 0, -t, t) ==> t */
1595 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1602 } /* equivalent_node_Mux */
1605 * Returns a equivalent node of a Psi: if a condition is true
1606 * and all previous conditions are false we know its value.
1607 * If all conditions are false its value is the default one.
1609 static ir_node *equivalent_node_Psi(ir_node *n) {
1611 return equivalent_node_Mux(n);
1613 } /* equivalent_node_Psi */
1616 * Optimize -a CMP -b into b CMP a.
1617 * This works only for for modes where unary Minus
1619 * Note that two-complement integers can Overflow
1620 * so it will NOT work.
1622 * For == and != can be handled in Proj(Cmp)
1624 static ir_node *equivalent_node_Cmp(ir_node *n) {
1625 ir_node *left = get_Cmp_left(n);
1626 ir_node *right = get_Cmp_right(n);
1628 if (is_Minus(left) && is_Minus(right) &&
1629 !mode_overflow_on_unary_Minus(get_irn_mode(left))) {
1630 left = get_Minus_op(left);
1631 right = get_Minus_op(right);
1632 set_Cmp_left(n, right);
1633 set_Cmp_right(n, left);
1636 } /* equivalent_node_Cmp */
1639 * Remove Confirm nodes if setting is on.
1640 * Replace Confirms(x, '=', Constlike) by Constlike.
1642 static ir_node *equivalent_node_Confirm(ir_node *n) {
1643 ir_node *pred = get_Confirm_value(n);
1644 pn_Cmp pnc = get_Confirm_cmp(n);
1646 if (is_Confirm(pred) && pnc == get_Confirm_cmp(pred)) {
1648 * rare case: two identical Confirms one after another,
1649 * replace the second one with the first.
1653 if (get_opt_remove_confirm())
1654 return get_Confirm_value(n);
1659 * Optimize CopyB(mem, x, x) into a Nop.
1661 static ir_node *equivalent_node_CopyB(ir_node *n) {
1662 ir_node *a = get_CopyB_dst(n);
1663 ir_node *b = get_CopyB_src(n);
1666 /* Turn CopyB into a tuple (mem, jmp, bad, bad) */
1667 ir_node *mem = get_CopyB_mem(n);
1668 ir_node *blk = get_nodes_block(n);
1669 turn_into_tuple(n, pn_CopyB_max);
1670 set_Tuple_pred(n, pn_CopyB_M, mem);
1671 set_Tuple_pred(n, pn_CopyB_X_regular, new_r_Jmp(current_ir_graph, blk));
1672 set_Tuple_pred(n, pn_CopyB_X_except, new_Bad()); /* no exception */
1673 set_Tuple_pred(n, pn_CopyB_M_except, new_Bad());
1676 } /* equivalent_node_CopyB */
1679 * Optimize Bounds(idx, idx, upper) into idx.
1681 static ir_node *equivalent_node_Bound(ir_node *n) {
1682 ir_node *idx = get_Bound_index(n);
1683 ir_node *pred = skip_Proj(idx);
1686 if (is_Bound(pred)) {
1688 * idx was Bounds checked in the same MacroBlock previously,
1689 * it is still valid if lower <= pred_lower && pred_upper <= upper.
1691 ir_node *lower = get_Bound_lower(n);
1692 ir_node *upper = get_Bound_upper(n);
1693 if (get_Bound_lower(pred) == lower &&
1694 get_Bound_upper(pred) == upper &&
1695 get_irn_MacroBlock(n) == get_irn_MacroBlock(pred)) {
1697 * One could expect that we simply return the previous
1698 * Bound here. However, this would be wrong, as we could
1699 * add an exception Proj to a new location then.
1700 * So, we must turn in into a tuple.
1706 /* Turn Bound into a tuple (mem, jmp, bad, idx) */
1707 ir_node *mem = get_Bound_mem(n);
1708 ir_node *blk = get_nodes_block(n);
1709 turn_into_tuple(n, pn_Bound_max);
1710 set_Tuple_pred(n, pn_Bound_M, mem);
1711 set_Tuple_pred(n, pn_Bound_X_regular, new_r_Jmp(current_ir_graph, blk)); /* no exception */
1712 set_Tuple_pred(n, pn_Bound_X_except, new_Bad()); /* no exception */
1713 set_Tuple_pred(n, pn_Bound_res, idx);
1716 } /* equivalent_node_Bound */
1719 * equivalent_node() returns a node equivalent to input n. It skips all nodes that
1720 * perform no actual computation, as, e.g., the Id nodes. It does not create
1721 * new nodes. It is therefore safe to free n if the node returned is not n.
1722 * If a node returns a Tuple we can not just skip it. If the size of the
1723 * in array fits, we transform n into a tuple (e.g., Div).
1725 ir_node *equivalent_node(ir_node *n) {
1726 if (n->op->ops.equivalent_node)
1727 return n->op->ops.equivalent_node(n);
1729 } /* equivalent_node */
1732 * Sets the default equivalent node operation for an ir_op_ops.
1734 * @param code the opcode for the default operation
1735 * @param ops the operations initialized
1740 static ir_op_ops *firm_set_default_equivalent_node(ir_opcode code, ir_op_ops *ops)
1744 ops->equivalent_node = equivalent_node_##a; \
1784 } /* firm_set_default_equivalent_node */
1787 * Returns non-zero if a node is a Phi node
1788 * with all predecessors constant.
1790 static int is_const_Phi(ir_node *n) {
1793 if (! is_Phi(n) || get_irn_arity(n) == 0)
1795 for (i = get_irn_arity(n) - 1; i >= 0; --i)
1796 if (! is_Const(get_irn_n(n, i)))
1799 } /* is_const_Phi */
1802 * Apply an evaluator on a binop with a constant operators (and one Phi).
1804 * @param phi the Phi node
1805 * @param other the other operand
1806 * @param eval an evaluator function
1807 * @param mode the mode of the result, may be different from the mode of the Phi!
1808 * @param left if non-zero, other is the left operand, else the right
1810 * @return a new Phi node if the conversion was successful, NULL else
1812 static ir_node *apply_binop_on_phi(ir_node *phi, tarval *other, tarval *(*eval)(tarval *, tarval *), ir_mode *mode, int left) {
1817 int i, n = get_irn_arity(phi);
1819 NEW_ARR_A(void *, res, n);
1821 for (i = 0; i < n; ++i) {
1822 pred = get_irn_n(phi, i);
1823 tv = get_Const_tarval(pred);
1824 tv = eval(other, tv);
1826 if (tv == tarval_bad) {
1827 /* folding failed, bad */
1833 for (i = 0; i < n; ++i) {
1834 pred = get_irn_n(phi, i);
1835 tv = get_Const_tarval(pred);
1836 tv = eval(tv, other);
1838 if (tv == tarval_bad) {
1839 /* folding failed, bad */
1845 irg = current_ir_graph;
1846 for (i = 0; i < n; ++i) {
1847 pred = get_irn_n(phi, i);
1848 res[i] = new_r_Const_type(irg, get_irg_start_block(irg),
1849 mode, res[i], get_Const_type(pred));
1851 return new_r_Phi(irg, get_nodes_block(phi), n, (ir_node **)res, mode);
1852 } /* apply_binop_on_phi */
1855 * Apply an evaluator on a binop with two constant Phi.
1857 * @param a the left Phi node
1858 * @param b the right Phi node
1859 * @param eval an evaluator function
1860 * @param mode the mode of the result, may be different from the mode of the Phi!
1862 * @return a new Phi node if the conversion was successful, NULL else
1864 static ir_node *apply_binop_on_2_phis(ir_node *a, ir_node *b, tarval *(*eval)(tarval *, tarval *), ir_mode *mode) {
1865 tarval *tv_l, *tv_r, *tv;
1871 if (get_nodes_block(a) != get_nodes_block(b))
1874 n = get_irn_arity(a);
1875 NEW_ARR_A(void *, res, n);
1877 for (i = 0; i < n; ++i) {
1878 pred = get_irn_n(a, i);
1879 tv_l = get_Const_tarval(pred);
1880 pred = get_irn_n(b, i);
1881 tv_r = get_Const_tarval(pred);
1882 tv = eval(tv_l, tv_r);
1884 if (tv == tarval_bad) {
1885 /* folding failed, bad */
1890 irg = current_ir_graph;
1891 for (i = 0; i < n; ++i) {
1892 pred = get_irn_n(a, i);
1893 res[i] = new_r_Const_type(irg, get_irg_start_block(irg), mode, res[i], get_Const_type(pred));
1895 return new_r_Phi(irg, get_nodes_block(a), n, (ir_node **)res, mode);
1896 } /* apply_binop_on_2_phis */
1899 * Apply an evaluator on a unop with a constant operator (a Phi).
1901 * @param phi the Phi node
1902 * @param eval an evaluator function
1904 * @return a new Phi node if the conversion was successful, NULL else
1906 static ir_node *apply_unop_on_phi(ir_node *phi, tarval *(*eval)(tarval *)) {
1912 int i, n = get_irn_arity(phi);
1914 NEW_ARR_A(void *, res, n);
1915 for (i = 0; i < n; ++i) {
1916 pred = get_irn_n(phi, i);
1917 tv = get_Const_tarval(pred);
1920 if (tv == tarval_bad) {
1921 /* folding failed, bad */
1926 mode = get_irn_mode(phi);
1927 irg = current_ir_graph;
1928 for (i = 0; i < n; ++i) {
1929 pred = get_irn_n(phi, i);
1930 res[i] = new_r_Const_type(irg, get_irg_start_block(irg),
1931 mode, res[i], get_Const_type(pred));
1933 return new_r_Phi(irg, get_nodes_block(phi), n, (ir_node **)res, mode);
1934 } /* apply_unop_on_phi */
1937 * Apply a conversion on a constant operator (a Phi).
1939 * @param phi the Phi node
1941 * @return a new Phi node if the conversion was successful, NULL else
1943 static ir_node *apply_conv_on_phi(ir_node *phi, ir_mode *mode) {
1948 int i, n = get_irn_arity(phi);
1950 NEW_ARR_A(void *, res, n);
1951 for (i = 0; i < n; ++i) {
1952 pred = get_irn_n(phi, i);
1953 tv = get_Const_tarval(pred);
1954 tv = tarval_convert_to(tv, mode);
1956 if (tv == tarval_bad) {
1957 /* folding failed, bad */
1962 irg = current_ir_graph;
1963 for (i = 0; i < n; ++i) {
1964 pred = get_irn_n(phi, i);
1965 res[i] = new_r_Const_type(irg, get_irg_start_block(irg),
1966 mode, res[i], get_Const_type(pred));
1968 return new_r_Phi(irg, get_nodes_block(phi), n, (ir_node **)res, mode);
1969 } /* apply_conv_on_phi */
1972 * Transform AddP(P, ConvIs(Iu)), AddP(P, ConvIu(Is)) and
1973 * SubP(P, ConvIs(Iu)), SubP(P, ConvIu(Is)).
1974 * If possible, remove the Conv's.
1976 static ir_node *transform_node_AddSub(ir_node *n) {
1977 ir_mode *mode = get_irn_mode(n);
1979 if (mode_is_reference(mode)) {
1980 ir_node *left = get_binop_left(n);
1981 ir_node *right = get_binop_right(n);
1982 unsigned ref_bits = get_mode_size_bits(mode);
1984 if (is_Conv(left)) {
1985 ir_mode *lmode = get_irn_mode(left);
1986 unsigned bits = get_mode_size_bits(lmode);
1988 if (ref_bits == bits &&
1989 mode_is_int(lmode) &&
1990 get_mode_arithmetic(lmode) == irma_twos_complement) {
1991 ir_node *pre = get_Conv_op(left);
1992 ir_mode *pre_mode = get_irn_mode(pre);
1994 if (mode_is_int(pre_mode) &&
1995 get_mode_size_bits(pre_mode) == bits &&
1996 get_mode_arithmetic(pre_mode) == irma_twos_complement) {
1997 /* ok, this conv just changes to sign, moreover the calculation
1998 * is done with same number of bits as our address mode, so
1999 * we can ignore the conv as address calculation can be viewed
2000 * as either signed or unsigned
2002 set_binop_left(n, pre);
2007 if (is_Conv(right)) {
2008 ir_mode *rmode = get_irn_mode(right);
2009 unsigned bits = get_mode_size_bits(rmode);
2011 if (ref_bits == bits &&
2012 mode_is_int(rmode) &&
2013 get_mode_arithmetic(rmode) == irma_twos_complement) {
2014 ir_node *pre = get_Conv_op(right);
2015 ir_mode *pre_mode = get_irn_mode(pre);
2017 if (mode_is_int(pre_mode) &&
2018 get_mode_size_bits(pre_mode) == bits &&
2019 get_mode_arithmetic(pre_mode) == irma_twos_complement) {
2020 /* ok, this conv just changes to sign, moreover the calculation
2021 * is done with same number of bits as our address mode, so
2022 * we can ignore the conv as address calculation can be viewed
2023 * as either signed or unsigned
2025 set_binop_right(n, pre);
2030 /* let address arithmetic use unsigned modes */
2031 if (is_Const(right)) {
2032 ir_mode *rmode = get_irn_mode(right);
2034 if (mode_is_signed(rmode) && get_mode_arithmetic(rmode) == irma_twos_complement) {
2035 /* convert a AddP(P, *s) into AddP(P, *u) */
2036 ir_mode *nm = get_reference_mode_unsigned_eq(mode);
2038 ir_node *pre = new_r_Conv(current_ir_graph, get_nodes_block(n), right, nm);
2039 set_binop_right(n, pre);
2045 } /* transform_node_AddSub */
2047 #define HANDLE_BINOP_PHI(eval, a, b, c, mode) \
2049 if (is_Const(b) && is_const_Phi(a)) { \
2050 /* check for Op(Phi, Const) */ \
2051 c = apply_binop_on_phi(a, get_Const_tarval(b), eval, mode, 0);\
2053 else if (is_Const(a) && is_const_Phi(b)) { \
2054 /* check for Op(Const, Phi) */ \
2055 c = apply_binop_on_phi(b, get_Const_tarval(a), eval, mode, 1);\
2057 else if (is_const_Phi(a) && is_const_Phi(b)) { \
2058 /* check for Op(Phi, Phi) */ \
2059 c = apply_binop_on_2_phis(a, b, eval, mode); \
2062 DBG_OPT_ALGSIM0(oldn, c, FS_OPT_CONST_PHI); \
2066 #define HANDLE_UNOP_PHI(eval, a, c) \
2068 if (is_const_Phi(a)) { \
2069 /* check for Op(Phi) */ \
2070 c = apply_unop_on_phi(a, eval); \
2072 DBG_OPT_ALGSIM0(oldn, c, FS_OPT_CONST_PHI); \
2078 * Do the AddSub optimization, then Transform
2079 * Constant folding on Phi
2080 * Add(a,a) -> Mul(a, 2)
2081 * Add(Mul(a, x), a) -> Mul(a, x+1)
2082 * if the mode is integer or float.
2083 * Transform Add(a,-b) into Sub(a,b).
2084 * Reassociation might fold this further.
2086 static ir_node *transform_node_Add(ir_node *n) {
2088 ir_node *a, *b, *c, *oldn = n;
2090 n = transform_node_AddSub(n);
2092 a = get_Add_left(n);
2093 b = get_Add_right(n);
2095 mode = get_irn_mode(n);
2097 if (mode_is_reference(mode)) {
2098 ir_mode *lmode = get_irn_mode(a);
2100 if (is_Const(b) && is_Const_null(b) && mode_is_int(lmode)) {
2101 /* an Add(a, NULL) is a hidden Conv */
2102 dbg_info *dbg = get_irn_dbg_info(n);
2103 return new_rd_Conv(dbg, current_ir_graph, get_nodes_block(n), a, mode);
2107 HANDLE_BINOP_PHI(tarval_add, a, b, c, mode);
2109 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
2110 if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
2113 if (mode_is_num(mode)) {
2114 /* the following code leads to endless recursion when Mul are replaced by a simple instruction chain */
2115 if (!is_arch_dep_running() && a == b && mode_is_int(mode)) {
2116 ir_node *block = get_nodes_block(n);
2119 get_irn_dbg_info(n),
2123 new_r_Const_long(current_ir_graph, block, mode, 2),
2125 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_A);
2130 get_irn_dbg_info(n),
2136 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_MINUS_B);
2141 get_irn_dbg_info(n),
2147 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_MINUS_B);
2150 if (get_mode_arithmetic(mode) == irma_twos_complement) {
2151 /* Here we rely on constants be on the RIGHT side */
2153 ir_node *op = get_Not_op(a);
2155 if (is_Const(b) && is_Const_one(b)) {
2157 ir_node *blk = get_irn_n(n, -1);
2158 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, blk, op, mode);
2159 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_PLUS_1);
2164 ir_node *blk = get_irn_n(n, -1);
2165 n = new_r_Const(current_ir_graph, blk, mode, get_mode_minus_one(mode));
2166 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_X_NOT_X);
2171 ir_node *op = get_Not_op(b);
2175 ir_node *blk = get_irn_n(n, -1);
2176 n = new_r_Const(current_ir_graph, blk, mode, get_mode_minus_one(mode));
2177 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_X_NOT_X);
2184 } /* transform_node_Add */
2187 * returns -cnst or NULL if impossible
2189 static ir_node *const_negate(ir_node *cnst) {
2190 tarval *tv = tarval_neg(get_Const_tarval(cnst));
2191 dbg_info *dbgi = get_irn_dbg_info(cnst);
2192 ir_graph *irg = get_irn_irg(cnst);
2193 ir_node *block = get_nodes_block(cnst);
2194 ir_mode *mode = get_irn_mode(cnst);
2195 if (tv == tarval_bad) return NULL;
2196 return new_rd_Const(dbgi, irg, block, mode, tv);
2200 * Do the AddSub optimization, then Transform
2201 * Constant folding on Phi
2202 * Sub(0,a) -> Minus(a)
2203 * Sub(Mul(a, x), a) -> Mul(a, x-1)
2204 * Sub(Sub(x, y), b) -> Sub(x, Add(y,b))
2205 * Sub(Add(a, x), x) -> a
2206 * Sub(x, Add(x, a)) -> -a
2207 * Sub(x, Const) -> Add(x, -Const)
2209 static ir_node *transform_node_Sub(ir_node *n) {
2214 n = transform_node_AddSub(n);
2216 a = get_Sub_left(n);
2217 b = get_Sub_right(n);
2219 mode = get_irn_mode(n);
2221 if (mode_is_int(mode)) {
2222 ir_mode *lmode = get_irn_mode(a);
2224 if (is_Const(b) && is_Const_null(b) && mode_is_reference(lmode)) {
2225 /* a Sub(a, NULL) is a hidden Conv */
2226 dbg_info *dbg = get_irn_dbg_info(n);
2227 return new_rd_Conv(dbg, current_ir_graph, get_nodes_block(n), a, mode);
2232 HANDLE_BINOP_PHI(tarval_sub, a, b, c, mode);
2234 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
2235 if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
2238 if (is_Const(b) && get_irn_mode(b) != mode_P) {
2239 /* a - C -> a + (-C) */
2240 ir_node *cnst = const_negate(b);
2242 ir_node *block = get_nodes_block(n);
2243 dbg_info *dbgi = get_irn_dbg_info(n);
2244 ir_graph *irg = get_irn_irg(n);
2246 n = new_rd_Add(dbgi, irg, block, a, cnst, mode);
2247 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2252 if (is_Minus(a)) { /* (-a) - b -> -(a + b) */
2253 ir_graph *irg = current_ir_graph;
2254 dbg_info *dbg = get_irn_dbg_info(n);
2255 ir_node *block = get_nodes_block(n);
2256 ir_node *left = get_Minus_op(a);
2257 ir_node *add = new_rd_Add(dbg, irg, block, left, b, mode);
2259 n = new_rd_Minus(dbg, irg, block, add, mode);
2260 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2262 } else if (is_Minus(b)) { /* a - (-b) -> a + b */
2263 ir_graph *irg = current_ir_graph;
2264 dbg_info *dbg = get_irn_dbg_info(n);
2265 ir_node *block = get_nodes_block(n);
2266 ir_node *right = get_Minus_op(b);
2268 n = new_rd_Add(dbg, irg, block, a, right, mode);
2269 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MINUS);
2271 } else if (is_Sub(b)) { /* a - (b - c) -> a + (c - b) */
2272 ir_graph *irg = current_ir_graph;
2273 dbg_info *s_dbg = get_irn_dbg_info(b);
2274 ir_node *s_block = get_nodes_block(b);
2275 ir_node *s_left = get_Sub_right(b);
2276 ir_node *s_right = get_Sub_left(b);
2277 ir_mode *s_mode = get_irn_mode(b);
2278 ir_node *sub = new_rd_Sub(s_dbg, irg, s_block, s_left, s_right, s_mode);
2279 dbg_info *a_dbg = get_irn_dbg_info(n);
2280 ir_node *a_block = get_nodes_block(n);
2282 n = new_rd_Add(a_dbg, irg, a_block, a, sub, mode);
2283 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2285 } else if (is_Mul(b)) { /* a - (b * C) -> a + (b * -C) */
2286 ir_node *m_right = get_Mul_right(b);
2287 if (is_Const(m_right)) {
2288 ir_node *cnst2 = const_negate(m_right);
2289 if (cnst2 != NULL) {
2290 ir_graph *irg = current_ir_graph;
2291 dbg_info *m_dbg = get_irn_dbg_info(b);
2292 ir_node *m_block = get_nodes_block(b);
2293 ir_node *m_left = get_Mul_left(b);
2294 ir_mode *m_mode = get_irn_mode(b);
2295 ir_node *mul = new_rd_Mul(m_dbg, irg, m_block, m_left, cnst2, m_mode);
2296 dbg_info *a_dbg = get_irn_dbg_info(n);
2297 ir_node *a_block = get_nodes_block(n);
2299 n = new_rd_Add(a_dbg, irg, a_block, a, mul, mode);
2300 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2306 /* Beware of Sub(P, P) which cannot be optimized into a simple Minus ... */
2307 if (mode_is_num(mode) && mode == get_irn_mode(a) && is_Const(a) && is_Const_null(a)) {
2309 get_irn_dbg_info(n),
2314 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_0_A);
2318 if (mode_wrap_around(mode)) {
2319 ir_node *left = get_Add_left(a);
2320 ir_node *right = get_Add_right(a);
2322 /* FIXME: Does the Conv's work only for two complement or generally? */
2324 if (mode != get_irn_mode(right)) {
2325 /* This Sub is an effective Cast */
2326 right = new_r_Conv(get_irn_irg(n), get_irn_n(n, -1), right, mode);
2329 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2331 } else if (right == b) {
2332 if (mode != get_irn_mode(left)) {
2333 /* This Sub is an effective Cast */
2334 left = new_r_Conv(get_irn_irg(n), get_irn_n(n, -1), left, mode);
2337 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2343 if (mode_wrap_around(mode)) {
2344 ir_node *left = get_Add_left(b);
2345 ir_node *right = get_Add_right(b);
2347 /* FIXME: Does the Conv's work only for two complement or generally? */
2349 ir_mode *r_mode = get_irn_mode(right);
2351 n = new_r_Minus(get_irn_irg(n), get_irn_n(n, -1), right, r_mode);
2352 if (mode != r_mode) {
2353 /* This Sub is an effective Cast */
2354 n = new_r_Conv(get_irn_irg(n), get_irn_n(n, -1), n, mode);
2356 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2358 } else if (right == a) {
2359 ir_mode *l_mode = get_irn_mode(left);
2361 n = new_r_Minus(get_irn_irg(n), get_irn_n(n, -1), left, l_mode);
2362 if (mode != l_mode) {
2363 /* This Sub is an effective Cast */
2364 n = new_r_Conv(get_irn_irg(n), get_irn_n(n, -1), n, mode);
2366 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2371 if (mode_is_int(mode) && is_Conv(a) && is_Conv(b)) {
2372 ir_mode *mode = get_irn_mode(a);
2374 if (mode == get_irn_mode(b)) {
2376 ir_node *op_a = get_Conv_op(a);
2377 ir_node *op_b = get_Conv_op(b);
2379 /* check if it's allowed to skip the conv */
2380 ma = get_irn_mode(op_a);
2381 mb = get_irn_mode(op_b);
2383 if (mode_is_reference(ma) && mode_is_reference(mb)) {
2384 /* SubInt(ConvInt(aP), ConvInt(bP)) -> SubInt(aP,bP) */
2387 set_Sub_right(n, b);
2393 /* do NOT execute this code if reassociation is enabled, it does the inverse! */
2394 if (!is_reassoc_running() && is_Mul(a)) {
2395 ir_node *ma = get_Mul_left(a);
2396 ir_node *mb = get_Mul_right(a);
2399 ir_node *blk = get_irn_n(n, -1);
2401 get_irn_dbg_info(n),
2402 current_ir_graph, blk,
2405 get_irn_dbg_info(n),
2406 current_ir_graph, blk,
2408 new_r_Const_long(current_ir_graph, blk, mode, 1),
2411 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MUL_A_X_A);
2413 } else if (mb == b) {
2414 ir_node *blk = get_irn_n(n, -1);
2416 get_irn_dbg_info(n),
2417 current_ir_graph, blk,
2420 get_irn_dbg_info(n),
2421 current_ir_graph, blk,
2423 new_r_Const_long(current_ir_graph, blk, mode, 1),
2426 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MUL_A_X_A);
2430 if (is_Sub(a)) { /* (x - y) - b -> x - (y + b) */
2431 ir_node *x = get_Sub_left(a);
2432 ir_node *y = get_Sub_right(a);
2433 ir_node *blk = get_irn_n(n, -1);
2434 ir_mode *m_b = get_irn_mode(b);
2435 ir_mode *m_y = get_irn_mode(y);
2439 /* Determine the right mode for the Add. */
2442 else if (mode_is_reference(m_b))
2444 else if (mode_is_reference(m_y))
2448 * Both modes are different but none is reference,
2449 * happens for instance in SubP(SubP(P, Iu), Is).
2450 * We have two possibilities here: Cast or ignore.
2451 * Currently we ignore this case.
2456 add = new_r_Add(current_ir_graph, blk, y, b, add_mode);
2458 n = new_rd_Sub(get_irn_dbg_info(n), current_ir_graph, blk, x, add, mode);
2459 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_SUB_X_Y_Z);
2463 if (get_mode_arithmetic(mode) == irma_twos_complement) {
2464 if (is_Const(a) && is_Not(b)) {
2465 /* c - ~X = X + (c+1) */
2466 tarval *tv = get_Const_tarval(a);
2468 tv = tarval_add(tv, get_mode_one(mode));
2469 if (tv != tarval_bad) {
2470 ir_node *blk = get_irn_n(n, -1);
2471 ir_node *c = new_r_Const(current_ir_graph, blk, mode, tv);
2472 n = new_rd_Add(get_irn_dbg_info(n), current_ir_graph, blk, get_Not_op(b), c, mode);
2473 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_C_NOT_X);
2479 } /* transform_node_Sub */
2482 * Several transformation done on n*n=2n bits mul.
2483 * These transformations must be done here because new nodes may be produced.
2485 static ir_node *transform_node_Mul2n(ir_node *n, ir_mode *mode) {
2487 ir_node *a = get_Mul_left(n);
2488 ir_node *b = get_Mul_right(n);
2489 tarval *ta = value_of(a);
2490 tarval *tb = value_of(b);
2491 ir_mode *smode = get_irn_mode(a);
2493 if (ta == get_mode_one(smode)) {
2494 /* (L)1 * (L)b = (L)b */
2495 ir_node *blk = get_irn_n(n, -1);
2496 n = new_rd_Conv(get_irn_dbg_info(n), current_ir_graph, blk, b, mode);
2497 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
2500 else if (ta == get_mode_minus_one(smode)) {
2501 /* (L)-1 * (L)b = (L)b */
2502 ir_node *blk = get_irn_n(n, -1);
2503 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, blk, b, smode);
2504 n = new_rd_Conv(get_irn_dbg_info(n), current_ir_graph, blk, n, mode);
2505 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2508 if (tb == get_mode_one(smode)) {
2509 /* (L)a * (L)1 = (L)a */
2510 ir_node *blk = get_irn_n(a, -1);
2511 n = new_rd_Conv(get_irn_dbg_info(n), current_ir_graph, blk, a, mode);
2512 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
2515 else if (tb == get_mode_minus_one(smode)) {
2516 /* (L)a * (L)-1 = (L)-a */
2517 ir_node *blk = get_irn_n(n, -1);
2518 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, blk, a, smode);
2519 n = new_rd_Conv(get_irn_dbg_info(n), current_ir_graph, blk, n, mode);
2520 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2527 * Transform Mul(a,-1) into -a.
2528 * Do constant evaluation of Phi nodes.
2529 * Do architecture dependent optimizations on Mul nodes
2531 static ir_node *transform_node_Mul(ir_node *n) {
2532 ir_node *c, *oldn = n;
2533 ir_mode *mode = get_irn_mode(n);
2534 ir_node *a = get_Mul_left(n);
2535 ir_node *b = get_Mul_right(n);
2537 if (is_Bad(a) || is_Bad(b))
2540 if (mode != get_irn_mode(a))
2541 return transform_node_Mul2n(n, mode);
2543 HANDLE_BINOP_PHI(tarval_mul, a, b, c, mode);
2545 if (mode_is_signed(mode)) {
2548 if (value_of(a) == get_mode_minus_one(mode))
2550 else if (value_of(b) == get_mode_minus_one(mode))
2553 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1), r, mode);
2554 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2559 if (is_Const(b)) { /* (-a) * const -> a * -const */
2560 ir_node *cnst = const_negate(b);
2562 dbg_info *dbgi = get_irn_dbg_info(n);
2563 ir_node *block = get_nodes_block(n);
2564 n = new_rd_Mul(dbgi, current_ir_graph, block, get_Minus_op(a), cnst, mode);
2565 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2568 } else if (is_Minus(b)) { /* (-a) * (-b) -> a * b */
2569 dbg_info *dbgi = get_irn_dbg_info(n);
2570 ir_node *block = get_nodes_block(n);
2571 n = new_rd_Mul(dbgi, current_ir_graph, block, get_Minus_op(a), get_Minus_op(b), mode);
2572 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_MINUS);
2574 } else if (is_Sub(b)) { /* (-a) * (b - c) -> a * (c - b) */
2575 ir_node *sub_l = get_Sub_left(b);
2576 ir_node *sub_r = get_Sub_right(b);
2577 dbg_info *dbgi = get_irn_dbg_info(n);
2578 ir_graph *irg = current_ir_graph;
2579 ir_node *block = get_nodes_block(n);
2580 ir_node *new_b = new_rd_Sub(dbgi, irg, block, sub_r, sub_l, mode);
2581 n = new_rd_Mul(dbgi, irg, block, get_Minus_op(a), new_b, mode);
2582 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS);
2585 } else if (is_Minus(b)) {
2586 if (is_Sub(a)) { /* (a - b) * (-c) -> (b - a) * c */
2587 ir_node *sub_l = get_Sub_left(a);
2588 ir_node *sub_r = get_Sub_right(a);
2589 dbg_info *dbgi = get_irn_dbg_info(n);
2590 ir_graph *irg = current_ir_graph;
2591 ir_node *block = get_nodes_block(n);
2592 ir_node *new_a = new_rd_Sub(dbgi, irg, block, sub_r, sub_l, mode);
2593 n = new_rd_Mul(dbgi, irg, block, new_a, get_Minus_op(b), mode);
2594 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS);
2598 if (get_mode_arithmetic(mode) == irma_ieee754) {
2600 tarval *tv = get_Const_tarval(a);
2601 if (tarval_ieee754_get_exponent(tv) == 1 && tarval_ieee754_zero_mantissa(tv)) {
2602 /* 2.0 * b = b + b */
2603 n = new_rd_Add(get_irn_dbg_info(n), current_ir_graph, get_nodes_block(n), b, b, mode);
2604 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_A_A);
2608 else if (is_Const(b)) {
2609 tarval *tv = get_Const_tarval(b);
2610 if (tarval_ieee754_get_exponent(tv) == 1 && tarval_ieee754_zero_mantissa(tv)) {
2611 /* a * 2.0 = a + a */
2612 n = new_rd_Add(get_irn_dbg_info(n), current_ir_graph, get_nodes_block(n), a, a, mode);
2613 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_A_A);
2618 return arch_dep_replace_mul_with_shifts(n);
2619 } /* transform_node_Mul */
2622 * Transform a Div Node.
2624 static ir_node *transform_node_Div(ir_node *n) {
2625 ir_mode *mode = get_Div_resmode(n);
2626 ir_node *a = get_Div_left(n);
2627 ir_node *b = get_Div_right(n);
2631 if (is_Const(b) && is_const_Phi(a)) {
2632 /* check for Div(Phi, Const) */
2633 value = apply_binop_on_phi(a, get_Const_tarval(b), tarval_div, mode, 0);
2635 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2639 else if (is_Const(a) && is_const_Phi(b)) {
2640 /* check for Div(Const, Phi) */
2641 value = apply_binop_on_phi(b, get_Const_tarval(a), tarval_div, mode, 1);
2643 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2647 else if (is_const_Phi(a) && is_const_Phi(b)) {
2648 /* check for Div(Phi, Phi) */
2649 value = apply_binop_on_2_phis(a, b, tarval_div, mode);
2651 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2658 if (tv != tarval_bad) {
2659 value = new_Const(get_tarval_mode(tv), tv);
2661 DBG_OPT_CSTEVAL(n, value);
2664 ir_node *a = get_Div_left(n);
2665 ir_node *b = get_Div_right(n);
2668 if (a == b && value_not_zero(a, &dummy)) {
2669 /* BEWARE: we can optimize a/a to 1 only if this cannot cause a exception */
2670 value = new_Const(mode, get_mode_one(mode));
2671 DBG_OPT_CSTEVAL(n, value);
2674 if (mode_is_signed(mode) && is_Const(b)) {
2675 tarval *tv = get_Const_tarval(b);
2677 if (tv == get_mode_minus_one(mode)) {
2679 value = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1), a, mode);
2680 DBG_OPT_CSTEVAL(n, value);
2684 /* Try architecture dependent optimization */
2685 value = arch_dep_replace_div_by_const(n);
2693 /* Turn Div into a tuple (mem, jmp, bad, value) */
2694 mem = get_Div_mem(n);
2695 blk = get_irn_n(n, -1);
2697 /* skip a potential Pin */
2699 mem = get_Pin_op(mem);
2700 turn_into_tuple(n, pn_Div_max);
2701 set_Tuple_pred(n, pn_Div_M, mem);
2702 set_Tuple_pred(n, pn_Div_X_regular, new_r_Jmp(current_ir_graph, blk));
2703 set_Tuple_pred(n, pn_Div_X_except, new_Bad());
2704 set_Tuple_pred(n, pn_Div_res, value);
2707 } /* transform_node_Div */
2710 * Transform a Mod node.
2712 static ir_node *transform_node_Mod(ir_node *n) {
2713 ir_mode *mode = get_Mod_resmode(n);
2714 ir_node *a = get_Mod_left(n);
2715 ir_node *b = get_Mod_right(n);
2719 if (is_Const(b) && is_const_Phi(a)) {
2720 /* check for Div(Phi, Const) */
2721 value = apply_binop_on_phi(a, get_Const_tarval(b), tarval_mod, mode, 0);
2723 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2727 else if (is_Const(a) && is_const_Phi(b)) {
2728 /* check for Div(Const, Phi) */
2729 value = apply_binop_on_phi(b, get_Const_tarval(a), tarval_mod, mode, 1);
2731 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2735 else if (is_const_Phi(a) && is_const_Phi(b)) {
2736 /* check for Div(Phi, Phi) */
2737 value = apply_binop_on_2_phis(a, b, tarval_mod, mode);
2739 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2746 if (tv != tarval_bad) {
2747 value = new_Const(get_tarval_mode(tv), tv);
2749 DBG_OPT_CSTEVAL(n, value);
2752 ir_node *a = get_Mod_left(n);
2753 ir_node *b = get_Mod_right(n);
2756 if (a == b && value_not_zero(a, &dummy)) {
2757 /* BEWARE: we can optimize a%a to 0 only if this cannot cause a exception */
2758 value = new_Const(mode, get_mode_null(mode));
2759 DBG_OPT_CSTEVAL(n, value);
2762 if (mode_is_signed(mode) && is_Const(b)) {
2763 tarval *tv = get_Const_tarval(b);
2765 if (tv == get_mode_minus_one(mode)) {
2767 value = new_Const(mode, get_mode_null(mode));
2768 DBG_OPT_CSTEVAL(n, value);
2772 /* Try architecture dependent optimization */
2773 value = arch_dep_replace_mod_by_const(n);
2781 /* Turn Mod into a tuple (mem, jmp, bad, value) */
2782 mem = get_Mod_mem(n);
2783 blk = get_irn_n(n, -1);
2785 /* skip a potential Pin */
2787 mem = get_Pin_op(mem);
2788 turn_into_tuple(n, pn_Mod_max);
2789 set_Tuple_pred(n, pn_Mod_M, mem);
2790 set_Tuple_pred(n, pn_Mod_X_regular, new_r_Jmp(current_ir_graph, blk));
2791 set_Tuple_pred(n, pn_Mod_X_except, new_Bad());
2792 set_Tuple_pred(n, pn_Mod_res, value);
2795 } /* transform_node_Mod */
2798 * Transform a DivMod node.
2800 static ir_node *transform_node_DivMod(ir_node *n) {
2802 ir_node *a = get_DivMod_left(n);
2803 ir_node *b = get_DivMod_right(n);
2804 ir_mode *mode = get_DivMod_resmode(n);
2809 if (is_Const(b) && is_const_Phi(a)) {
2810 /* check for Div(Phi, Const) */
2811 va = apply_binop_on_phi(a, get_Const_tarval(b), tarval_div, mode, 0);
2812 vb = apply_binop_on_phi(a, get_Const_tarval(b), tarval_mod, mode, 0);
2814 DBG_OPT_ALGSIM0(n, va, FS_OPT_CONST_PHI);
2815 DBG_OPT_ALGSIM0(n, vb, FS_OPT_CONST_PHI);
2819 else if (is_Const(a) && is_const_Phi(b)) {
2820 /* check for Div(Const, Phi) */
2821 va = apply_binop_on_phi(b, get_Const_tarval(a), tarval_div, mode, 1);
2822 vb = apply_binop_on_phi(b, get_Const_tarval(a), tarval_mod, mode, 1);
2824 DBG_OPT_ALGSIM0(n, va, FS_OPT_CONST_PHI);
2825 DBG_OPT_ALGSIM0(n, vb, FS_OPT_CONST_PHI);
2829 else if (is_const_Phi(a) && is_const_Phi(b)) {
2830 /* check for Div(Phi, Phi) */
2831 va = apply_binop_on_2_phis(a, b, tarval_div, mode);
2832 vb = apply_binop_on_2_phis(a, b, tarval_mod, mode);
2834 DBG_OPT_ALGSIM0(n, va, FS_OPT_CONST_PHI);
2835 DBG_OPT_ALGSIM0(n, vb, FS_OPT_CONST_PHI);
2842 if (tb != tarval_bad) {
2843 if (tb == get_mode_one(get_tarval_mode(tb))) {
2845 vb = new_Const(mode, get_mode_null(mode));
2846 DBG_OPT_CSTEVAL(n, vb);
2848 } else if (ta != tarval_bad) {
2849 tarval *resa, *resb;
2850 resa = tarval_div(ta, tb);
2851 if (resa == tarval_bad) return n; /* Causes exception!!! Model by replacing through
2852 Jmp for X result!? */
2853 resb = tarval_mod(ta, tb);
2854 if (resb == tarval_bad) return n; /* Causes exception! */
2855 va = new_Const(mode, resa);
2856 vb = new_Const(mode, resb);
2857 DBG_OPT_CSTEVAL(n, va);
2858 DBG_OPT_CSTEVAL(n, vb);
2860 } else if (mode_is_signed(mode) && tb == get_mode_minus_one(mode)) {
2861 va = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1), a, mode);
2862 vb = new_Const(mode, get_mode_null(mode));
2863 DBG_OPT_CSTEVAL(n, va);
2864 DBG_OPT_CSTEVAL(n, vb);
2866 } else { /* Try architecture dependent optimization */
2869 arch_dep_replace_divmod_by_const(&va, &vb, n);
2870 evaluated = va != NULL;
2872 } else if (a == b) {
2873 if (value_not_zero(a, &dummy)) {
2875 va = new_Const(mode, get_mode_one(mode));
2876 vb = new_Const(mode, get_mode_null(mode));
2877 DBG_OPT_CSTEVAL(n, va);
2878 DBG_OPT_CSTEVAL(n, vb);
2881 /* BEWARE: it is NOT possible to optimize a/a to 1, as this may cause a exception */
2884 } else if (ta == get_mode_null(mode) && value_not_zero(b, &dummy)) {
2885 /* 0 / non-Const = 0 */
2890 if (evaluated) { /* replace by tuple */
2894 mem = get_DivMod_mem(n);
2895 /* skip a potential Pin */
2897 mem = get_Pin_op(mem);
2899 blk = get_irn_n(n, -1);
2900 turn_into_tuple(n, pn_DivMod_max);
2901 set_Tuple_pred(n, pn_DivMod_M, mem);
2902 set_Tuple_pred(n, pn_DivMod_X_regular, new_r_Jmp(current_ir_graph, blk));
2903 set_Tuple_pred(n, pn_DivMod_X_except, new_Bad()); /* no exception */
2904 set_Tuple_pred(n, pn_DivMod_res_div, va);
2905 set_Tuple_pred(n, pn_DivMod_res_mod, vb);
2909 } /* transform_node_DivMod */
2912 * Optimize x / c to x * (1/c)
2914 static ir_node *transform_node_Quot(ir_node *n) {
2915 ir_mode *mode = get_Quot_resmode(n);
2918 if (get_mode_arithmetic(mode) == irma_ieee754) {
2919 ir_node *b = get_Quot_right(n);
2922 tarval *tv = get_Const_tarval(b);
2926 * Floating point constant folding might be disabled here to
2928 * However, as we check for exact result, doing it is safe.
2931 rem = tarval_enable_fp_ops(1);
2932 tv = tarval_quo(get_mode_one(mode), tv);
2933 (void)tarval_enable_fp_ops(rem);
2935 /* Do the transformation if the result is either exact or we are not
2936 using strict rules. */
2937 if (tv != tarval_bad &&
2938 (tarval_ieee754_get_exact() || (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic) == 0)) {
2939 ir_node *blk = get_irn_n(n, -1);
2940 ir_node *c = new_r_Const(current_ir_graph, blk, mode, tv);
2941 ir_node *a = get_Quot_left(n);
2942 ir_node *m = new_rd_Mul(get_irn_dbg_info(n), current_ir_graph, blk, a, c, mode);
2943 ir_node *mem = get_Quot_mem(n);
2945 /* skip a potential Pin */
2947 mem = get_Pin_op(mem);
2948 turn_into_tuple(n, pn_Quot_max);
2949 set_Tuple_pred(n, pn_Quot_M, mem);
2950 set_Tuple_pred(n, pn_Quot_X_regular, new_r_Jmp(current_ir_graph, blk));
2951 set_Tuple_pred(n, pn_Quot_X_except, new_r_Bad(current_ir_graph));
2952 set_Tuple_pred(n, pn_Quot_res, m);
2953 DBG_OPT_ALGSIM1(oldn, a, b, m, FS_OPT_FP_INV_MUL);
2958 } /* transform_node_Quot */
2961 * Optimize Abs(x) into x if x is Confirmed >= 0
2962 * Optimize Abs(x) into -x if x is Confirmed <= 0
2963 * Optimize Abs(-x) int Abs(x)
2965 static ir_node *transform_node_Abs(ir_node *n) {
2966 ir_node *c, *oldn = n;
2967 ir_node *a = get_Abs_op(n);
2970 HANDLE_UNOP_PHI(tarval_abs, a, c);
2972 switch (classify_value_sign(a)) {
2973 case value_classified_negative:
2974 mode = get_irn_mode(n);
2977 * We can replace the Abs by -x here.
2978 * We even could add a new Confirm here
2979 * (if not twos complement)
2981 * Note that -x would create a new node, so we could
2982 * not run it in the equivalent_node() context.
2984 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph,
2985 get_nodes_block(n), a, mode);
2987 DBG_OPT_CONFIRM(oldn, n);
2989 case value_classified_positive:
2990 /* n is positive, Abs is not needed */
2993 DBG_OPT_CONFIRM(oldn, n);
2999 /* Abs(-x) = Abs(x) */
3000 mode = get_irn_mode(n);
3001 n = new_rd_Abs(get_irn_dbg_info(n), current_ir_graph,
3002 get_nodes_block(n), get_Minus_op(a), mode);
3003 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ABS_MINUS_X);
3007 } /* transform_node_Abs */
3010 * Transform a Cond node.
3012 * Replace the Cond by a Jmp if it branches on a constant
3015 static ir_node *transform_node_Cond(ir_node *n) {
3018 ir_node *a = get_Cond_selector(n);
3019 tarval *ta = value_of(a);
3021 /* we need block info which is not available in floating irgs */
3022 if (get_irg_pinned(current_ir_graph) == op_pin_state_floats)
3025 if ((ta != tarval_bad) &&
3026 (get_irn_mode(a) == mode_b) &&
3027 (get_opt_unreachable_code())) {
3028 /* It's a boolean Cond, branching on a boolean constant.
3029 Replace it by a tuple (Bad, Jmp) or (Jmp, Bad) */
3030 ir_node *blk = get_nodes_block(n);
3031 jmp = new_r_Jmp(current_ir_graph, blk);
3032 turn_into_tuple(n, pn_Cond_max);
3033 if (ta == tarval_b_true) {
3034 set_Tuple_pred(n, pn_Cond_false, new_Bad());
3035 set_Tuple_pred(n, pn_Cond_true, jmp);
3037 set_Tuple_pred(n, pn_Cond_false, jmp);
3038 set_Tuple_pred(n, pn_Cond_true, new_Bad());
3040 /* We might generate an endless loop, so keep it alive. */
3041 add_End_keepalive(get_irg_end(current_ir_graph), blk);
3044 } /* transform_node_Cond */
3047 * Prototype of a recursive transform function
3048 * for bitwise distributive transformations.
3050 typedef ir_node* (*recursive_transform)(ir_node *n);
3053 * makes use of distributive laws for and, or, eor
3054 * and(a OP c, b OP c) -> and(a, b) OP c
3055 * note, might return a different op than n
3057 static ir_node *transform_bitwise_distributive(ir_node *n,
3058 recursive_transform trans_func)
3061 ir_node *a = get_binop_left(n);
3062 ir_node *b = get_binop_right(n);
3063 ir_op *op = get_irn_op(a);
3064 ir_op *op_root = get_irn_op(n);
3066 if(op != get_irn_op(b))
3069 if (op == op_Conv) {
3070 ir_node *a_op = get_Conv_op(a);
3071 ir_node *b_op = get_Conv_op(b);
3072 ir_mode *a_mode = get_irn_mode(a_op);
3073 ir_mode *b_mode = get_irn_mode(b_op);
3074 if(a_mode == b_mode && (mode_is_int(a_mode) || a_mode == mode_b)) {
3075 ir_node *blk = get_irn_n(n, -1);
3078 set_binop_left(n, a_op);
3079 set_binop_right(n, b_op);
3080 set_irn_mode(n, a_mode);
3082 n = new_r_Conv(current_ir_graph, blk, n, get_irn_mode(oldn));
3084 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_SHIFT_AND);
3090 /* nothing to gain here */
3094 if (op == op_Shrs || op == op_Shr || op == op_Shl
3095 || op == op_And || op == op_Or || op == op_Eor) {
3096 ir_node *a_left = get_binop_left(a);
3097 ir_node *a_right = get_binop_right(a);
3098 ir_node *b_left = get_binop_left(b);
3099 ir_node *b_right = get_binop_right(b);
3101 ir_node *op1 = NULL;
3102 ir_node *op2 = NULL;
3104 if (is_op_commutative(op)) {
3105 if (a_left == b_left) {
3109 } else if(a_left == b_right) {
3113 } else if(a_right == b_left) {
3119 if(a_right == b_right) {
3126 /* (a sop c) & (b sop c) => (a & b) sop c */
3127 ir_node *blk = get_irn_n(n, -1);
3129 ir_node *new_n = exact_copy(n);
3130 set_binop_left(new_n, op1);
3131 set_binop_right(new_n, op2);
3132 new_n = trans_func(new_n);
3134 if(op_root == op_Eor && op == op_Or) {
3135 dbg_info *dbgi = get_irn_dbg_info(n);
3136 ir_graph *irg = current_ir_graph;
3137 ir_mode *mode = get_irn_mode(c);
3139 c = new_rd_Not(dbgi, irg, blk, c, mode);
3140 n = new_rd_And(dbgi, irg, blk, new_n, c, mode);
3143 set_nodes_block(n, blk);
3144 set_binop_left(n, new_n);
3145 set_binop_right(n, c);
3146 add_identities(current_ir_graph->value_table, n);
3149 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_SHIFT_AND);
3160 static ir_node *transform_node_And(ir_node *n) {
3161 ir_node *c, *oldn = n;
3162 ir_node *a = get_And_left(n);
3163 ir_node *b = get_And_right(n);
3166 mode = get_irn_mode(n);
3167 HANDLE_BINOP_PHI(tarval_and, a, b, c, mode);
3169 /* we can evaluate 2 Projs of the same Cmp */
3170 if (mode == mode_b && is_Proj(a) && is_Proj(b)) {
3171 ir_node *pred_a = get_Proj_pred(a);
3172 ir_node *pred_b = get_Proj_pred(b);
3173 if (pred_a == pred_b) {
3174 dbg_info *dbgi = get_irn_dbg_info(n);
3175 ir_node *block = get_nodes_block(pred_a);
3176 pn_Cmp pn_a = get_Proj_proj(a);
3177 pn_Cmp pn_b = get_Proj_proj(b);
3178 /* yes, we can simply calculate with pncs */
3179 pn_Cmp new_pnc = pn_a & pn_b;
3181 return new_rd_Proj(dbgi, current_ir_graph, block, pred_a, mode_b, new_pnc);
3186 ir_node *op = get_Not_op(b);
3188 ir_node *ba = get_And_left(op);
3189 ir_node *bb = get_And_right(op);
3191 /* it's enough to test the following cases due to normalization! */
3192 if (get_Or_left(a) == ba && get_Or_right(a) == bb) {
3193 /* (a|b) & ~(a&b) = a^b */
3194 ir_node *block = get_nodes_block(n);
3196 n = new_rd_Eor(get_irn_dbg_info(n), current_ir_graph, block, ba, bb, mode);
3197 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_TO_EOR);
3205 ir_node *op = get_Not_op(a);
3207 ir_node *aa = get_And_left(op);
3208 ir_node *ab = get_And_right(op);
3210 /* it's enough to test the following cases due to normalization! */
3211 if (get_Or_left(b) == aa && get_Or_right(b) == ab) {
3212 /* (a|b) & ~(a&b) = a^b */
3213 ir_node *block = get_nodes_block(n);
3215 n = new_rd_Eor(get_irn_dbg_info(n), current_ir_graph, block, aa, ab, mode);
3216 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_TO_EOR);
3223 ir_node *al = get_Eor_left(a);
3224 ir_node *ar = get_Eor_right(a);
3227 /* (b ^ a) & b -> ~a & b */
3228 dbg_info *dbg = get_irn_dbg_info(n);
3229 ir_node *block = get_nodes_block(n);
3231 ar = new_rd_Not(dbg, current_ir_graph, block, ar, mode);
3232 n = new_rd_And(dbg, current_ir_graph, block, ar, b, mode);
3233 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3237 /* (a ^ b) & b -> ~a & b */
3238 dbg_info *dbg = get_irn_dbg_info(n);
3239 ir_node *block = get_nodes_block(n);
3241 al = new_rd_Not(dbg, current_ir_graph, block, al, mode);
3242 n = new_rd_And(dbg, current_ir_graph, block, al, b, mode);
3243 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3248 ir_node *bl = get_Eor_left(b);
3249 ir_node *br = get_Eor_right(b);
3252 /* a & (a ^ b) -> a & ~b */
3253 dbg_info *dbg = get_irn_dbg_info(n);
3254 ir_node *block = get_nodes_block(n);
3256 br = new_rd_Not(dbg, current_ir_graph, block, br, mode);
3257 n = new_rd_And(dbg, current_ir_graph, block, br, a, mode);
3258 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3262 /* a & (b ^ a) -> a & ~b */
3263 dbg_info *dbg = get_irn_dbg_info(n);
3264 ir_node *block = get_nodes_block(n);
3266 bl = new_rd_Not(dbg, current_ir_graph, block, bl, mode);
3267 n = new_rd_And(dbg, current_ir_graph, block, bl, a, mode);
3268 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3272 if (is_Not(a) && is_Not(b)) {
3273 /* ~a & ~b = ~(a|b) */
3274 ir_node *block = get_nodes_block(n);
3275 ir_mode *mode = get_irn_mode(n);
3279 n = new_rd_Or(get_irn_dbg_info(n), current_ir_graph, block, a, b, mode);
3280 n = new_rd_Not(get_irn_dbg_info(n), current_ir_graph, block, n, mode);
3281 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_DEMORGAN);
3285 n = transform_bitwise_distributive(n, transform_node_And);
3288 } /* transform_node_And */
3293 static ir_node *transform_node_Eor(ir_node *n) {
3294 ir_node *c, *oldn = n;
3295 ir_node *a = get_Eor_left(n);
3296 ir_node *b = get_Eor_right(n);
3297 ir_mode *mode = get_irn_mode(n);
3299 HANDLE_BINOP_PHI(tarval_eor, a, b, c, mode);
3301 /* we can evaluate 2 Projs of the same Cmp */
3302 if (mode == mode_b && is_Proj(a) && is_Proj(b)) {
3303 ir_node *pred_a = get_Proj_pred(a);
3304 ir_node *pred_b = get_Proj_pred(b);
3305 if(pred_a == pred_b) {
3306 dbg_info *dbgi = get_irn_dbg_info(n);
3307 ir_node *block = get_nodes_block(pred_a);
3308 pn_Cmp pn_a = get_Proj_proj(a);
3309 pn_Cmp pn_b = get_Proj_proj(b);
3310 /* yes, we can simply calculate with pncs */
3311 pn_Cmp new_pnc = pn_a ^ pn_b;
3313 return new_rd_Proj(dbgi, current_ir_graph, block, pred_a, mode_b,
3320 n = new_rd_Const(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1),
3321 mode, get_mode_null(mode));
3322 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_A_A);
3323 } else if (mode == mode_b &&
3325 is_Const(b) && is_Const_one(b) &&
3326 is_Cmp(get_Proj_pred(a))) {
3327 /* The Eor negates a Cmp. The Cmp has the negated result anyways! */
3328 n = new_r_Proj(current_ir_graph, get_irn_n(n, -1), get_Proj_pred(a),
3329 mode_b, get_negated_pnc(get_Proj_proj(a), mode));
3331 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT_BOOL);
3332 } else if (is_Const(b)) {
3333 if (is_Not(a)) { /* ~x ^ const -> x ^ ~const */
3334 ir_node *cnst = new_Const(mode, tarval_not(get_Const_tarval(b)));
3335 ir_node *not_op = get_Not_op(a);
3336 dbg_info *dbg = get_irn_dbg_info(n);
3337 ir_graph *irg = current_ir_graph;
3338 ir_node *block = get_nodes_block(n);
3339 ir_mode *mode = get_irn_mode(n);
3340 n = new_rd_Eor(dbg, irg, block, not_op, cnst, mode);
3342 } else if (is_Const_all_one(b)) { /* x ^ 1...1 -> ~1 */
3343 n = new_r_Not(current_ir_graph, get_nodes_block(n), a, mode);
3344 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3347 n = transform_bitwise_distributive(n, transform_node_Eor);
3351 } /* transform_node_Eor */
3356 static ir_node *transform_node_Not(ir_node *n) {
3357 ir_node *c, *oldn = n;
3358 ir_node *a = get_Not_op(n);
3359 ir_mode *mode = get_irn_mode(n);
3361 HANDLE_UNOP_PHI(tarval_not,a,c);
3363 /* check for a boolean Not */
3364 if (mode == mode_b &&
3366 is_Cmp(get_Proj_pred(a))) {
3367 /* We negate a Cmp. The Cmp has the negated result anyways! */
3368 n = new_r_Proj(current_ir_graph, get_irn_n(n, -1), get_Proj_pred(a),
3369 mode_b, get_negated_pnc(get_Proj_proj(a), mode_b));
3370 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_CMP);
3374 ir_node *eor_b = get_Eor_right(a);
3375 if (is_Const(eor_b)) { /* ~(x ^ const) -> x ^ ~const */
3376 ir_node *cnst = new_Const(mode, tarval_not(get_Const_tarval(eor_b)));
3377 ir_node *eor_a = get_Eor_left(a);
3378 dbg_info *dbg = get_irn_dbg_info(n);
3379 ir_graph *irg = current_ir_graph;
3380 ir_node *block = get_nodes_block(n);
3381 ir_mode *mode = get_irn_mode(n);
3382 n = new_rd_Eor(dbg, irg, block, eor_a, cnst, mode);
3386 if (get_mode_arithmetic(mode) == irma_twos_complement) {
3387 if (is_Minus(a)) { /* ~-x -> x + -1 */
3388 dbg_info *dbg = get_irn_dbg_info(n);
3389 ir_graph *irg = current_ir_graph;
3390 ir_node *block = get_nodes_block(n);
3391 ir_node *add_l = get_Minus_op(a);
3392 ir_node *add_r = new_rd_Const(dbg, irg, block, mode, get_mode_minus_one(mode));
3393 n = new_rd_Add(dbg, irg, block, add_l, add_r, mode);
3394 } else if (is_Add(a)) {
3395 ir_node *add_r = get_Add_right(a);
3396 if (is_Const(add_r) && is_Const_all_one(add_r)) {
3397 /* ~(x + -1) = -x */
3398 ir_node *op = get_Add_left(a);
3399 ir_node *blk = get_irn_n(n, -1);
3400 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, blk, op, get_irn_mode(n));
3401 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_MINUS_1);
3406 } /* transform_node_Not */
3409 * Transform a Minus.
3413 * -(a >>u (size-1)) = a >>s (size-1)
3414 * -(a >>s (size-1)) = a >>u (size-1)
3415 * -(a * const) -> a * -const
3417 static ir_node *transform_node_Minus(ir_node *n) {
3418 ir_node *c, *oldn = n;
3419 ir_node *a = get_Minus_op(n);
3422 HANDLE_UNOP_PHI(tarval_neg,a,c);
3424 mode = get_irn_mode(a);
3425 if (get_mode_arithmetic(mode) == irma_twos_complement) {
3426 /* the following rules are only to twos-complement */
3429 ir_node *op = get_Not_op(a);
3430 tarval *tv = get_mode_one(mode);
3431 ir_node *blk = get_irn_n(n, -1);
3432 ir_node *c = new_r_Const(current_ir_graph, blk, mode, tv);
3433 n = new_rd_Add(get_irn_dbg_info(n), current_ir_graph, blk, op, c, mode);
3434 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_MINUS_NOT);
3438 ir_node *c = get_Shr_right(a);
3441 tarval *tv = get_Const_tarval(c);
3443 if (tarval_is_long(tv) && get_tarval_long(tv) == (int) get_mode_size_bits(mode) - 1) {
3444 /* -(a >>u (size-1)) = a >>s (size-1) */
3445 ir_node *v = get_Shr_left(a);
3447 n = new_rd_Shrs(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1), v, c, mode);
3448 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_PREDICATE);
3454 ir_node *c = get_Shrs_right(a);
3457 tarval *tv = get_Const_tarval(c);
3459 if (tarval_is_long(tv) && get_tarval_long(tv) == (int) get_mode_size_bits(mode) - 1) {
3460 /* -(a >>s (size-1)) = a >>u (size-1) */
3461 ir_node *v = get_Shrs_left(a);
3463 n = new_rd_Shr(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1), v, c, mode);
3464 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_PREDICATE);
3471 /* - (a-b) = b - a */
3472 ir_node *la = get_Sub_left(a);
3473 ir_node *ra = get_Sub_right(a);
3474 ir_node *blk = get_irn_n(n, -1);
3476 n = new_rd_Sub(get_irn_dbg_info(n), current_ir_graph, blk, ra, la, mode);
3477 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_MINUS_SUB);
3481 if (is_Mul(a)) { /* -(a * const) -> a * -const */
3482 ir_node *mul_l = get_Mul_left(a);
3483 ir_node *mul_r = get_Mul_right(a);
3484 if (is_Const(mul_r)) {
3485 tarval *tv = tarval_neg(get_Const_tarval(mul_r));
3486 if(tv != tarval_bad) {
3487 ir_node *cnst = new_Const(mode, tv);
3488 dbg_info *dbg = get_irn_dbg_info(a);
3489 ir_graph *irg = current_ir_graph;
3490 ir_node *block = get_nodes_block(a);
3491 n = new_rd_Mul(dbg, irg, block, mul_l, cnst, mode);
3492 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_MINUS_MUL_C);
3499 } /* transform_node_Minus */
3502 * Transform a Cast_type(Const) into a new Const_type
3504 static ir_node *transform_node_Cast(ir_node *n) {
3506 ir_node *pred = get_Cast_op(n);
3507 ir_type *tp = get_irn_type(n);
3509 if (is_Const(pred) && get_Const_type(pred) != tp) {
3510 n = new_rd_Const_type(NULL, current_ir_graph, get_irn_n(pred, -1), get_irn_mode(pred),
3511 get_Const_tarval(pred), tp);
3512 DBG_OPT_CSTEVAL(oldn, n);
3513 } else if (is_SymConst(pred) && get_SymConst_value_type(pred) != tp) {
3514 n = new_rd_SymConst_type(NULL, current_ir_graph, get_irn_n(pred, -1), get_irn_mode(pred),
3515 get_SymConst_symbol(pred), get_SymConst_kind(pred), tp);
3516 DBG_OPT_CSTEVAL(oldn, n);
3520 } /* transform_node_Cast */
3523 * Transform a Proj(Div) with a non-zero value.
3524 * Removes the exceptions and routes the memory to the NoMem node.
3526 static ir_node *transform_node_Proj_Div(ir_node *proj) {
3527 ir_node *div = get_Proj_pred(proj);
3528 ir_node *b = get_Div_right(div);
3529 ir_node *confirm, *res, *new_mem;
3532 if (value_not_zero(b, &confirm)) {
3533 /* div(x, y) && y != 0 */
3534 if (confirm == NULL) {
3535 /* we are sure we have a Const != 0 */
3536 new_mem = get_Div_mem(div);
3537 if (is_Pin(new_mem))
3538 new_mem = get_Pin_op(new_mem);
3539 set_Div_mem(div, new_mem);
3540 set_irn_pinned(div, op_pin_state_floats);
3543 proj_nr = get_Proj_proj(proj);
3545 case pn_Div_X_regular:
3546 return new_r_Jmp(current_ir_graph, get_irn_n(div, -1));
3548 case pn_Div_X_except:
3549 /* we found an exception handler, remove it */
3550 DBG_OPT_EXC_REM(proj);
3554 res = get_Div_mem(div);
3555 new_mem = get_irg_no_mem(current_ir_graph);
3558 /* This node can only float up to the Confirm block */
3559 new_mem = new_r_Pin(current_ir_graph, get_nodes_block(confirm), new_mem);
3561 set_irn_pinned(div, op_pin_state_floats);
3562 /* this is a Div without exception, we can remove the memory edge */
3563 set_Div_mem(div, new_mem);
3568 } /* transform_node_Proj_Div */
3571 * Transform a Proj(Mod) with a non-zero value.
3572 * Removes the exceptions and routes the memory to the NoMem node.
3574 static ir_node *transform_node_Proj_Mod(ir_node *proj) {
3575 ir_node *mod = get_Proj_pred(proj);
3576 ir_node *b = get_Mod_right(mod);
3577 ir_node *confirm, *res, *new_mem;
3580 if (value_not_zero(b, &confirm)) {
3581 /* mod(x, y) && y != 0 */
3582 proj_nr = get_Proj_proj(proj);
3584 if (confirm == NULL) {
3585 /* we are sure we have a Const != 0 */
3586 new_mem = get_Mod_mem(mod);
3587 if (is_Pin(new_mem))
3588 new_mem = get_Pin_op(new_mem);
3589 set_Mod_mem(mod, new_mem);
3590 set_irn_pinned(mod, op_pin_state_floats);
3595 case pn_Mod_X_regular:
3596 return new_r_Jmp(current_ir_graph, get_irn_n(mod, -1));
3598 case pn_Mod_X_except:
3599 /* we found an exception handler, remove it */
3600 DBG_OPT_EXC_REM(proj);
3604 res = get_Mod_mem(mod);
3605 new_mem = get_irg_no_mem(current_ir_graph);
3608 /* This node can only float up to the Confirm block */
3609 new_mem = new_r_Pin(current_ir_graph, get_nodes_block(confirm), new_mem);
3611 /* this is a Mod without exception, we can remove the memory edge */
3612 set_Mod_mem(mod, new_mem);
3615 if (get_Mod_left(mod) == b) {
3616 /* a % a = 0 if a != 0 */
3617 ir_mode *mode = get_irn_mode(proj);
3618 ir_node *res = new_Const(mode, get_mode_null(mode));
3620 DBG_OPT_CSTEVAL(mod, res);
3626 } /* transform_node_Proj_Mod */
3629 * Transform a Proj(DivMod) with a non-zero value.
3630 * Removes the exceptions and routes the memory to the NoMem node.
3632 static ir_node *transform_node_Proj_DivMod(ir_node *proj) {
3633 ir_node *divmod = get_Proj_pred(proj);
3634 ir_node *b = get_DivMod_right(divmod);
3635 ir_node *confirm, *res, *new_mem;
3638 if (value_not_zero(b, &confirm)) {
3639 /* DivMod(x, y) && y != 0 */
3640 proj_nr = get_Proj_proj(proj);
3642 if (confirm == NULL) {
3643 /* we are sure we have a Const != 0 */
3644 new_mem = get_DivMod_mem(divmod);
3645 if (is_Pin(new_mem))
3646 new_mem = get_Pin_op(new_mem);
3647 set_DivMod_mem(divmod, new_mem);
3648 set_irn_pinned(divmod, op_pin_state_floats);
3653 case pn_DivMod_X_regular:
3654 return new_r_Jmp(current_ir_graph, get_irn_n(divmod, -1));
3656 case pn_DivMod_X_except:
3657 /* we found an exception handler, remove it */
3658 DBG_OPT_EXC_REM(proj);
3662 res = get_DivMod_mem(divmod);
3663 new_mem = get_irg_no_mem(current_ir_graph);
3666 /* This node can only float up to the Confirm block */
3667 new_mem = new_r_Pin(current_ir_graph, get_nodes_block(confirm), new_mem);
3669 /* this is a DivMod without exception, we can remove the memory edge */
3670 set_DivMod_mem(divmod, new_mem);
3673 case pn_DivMod_res_mod:
3674 if (get_DivMod_left(divmod) == b) {
3675 /* a % a = 0 if a != 0 */
3676 ir_mode *mode = get_irn_mode(proj);
3677 ir_node *res = new_Const(mode, get_mode_null(mode));
3679 DBG_OPT_CSTEVAL(divmod, res);
3685 } /* transform_node_Proj_DivMod */
3688 * Optimizes jump tables (CondIs or CondIu) by removing all impossible cases.
3690 static ir_node *transform_node_Proj_Cond(ir_node *proj) {
3691 if (get_opt_unreachable_code()) {
3692 ir_node *n = get_Proj_pred(proj);
3693 ir_node *b = get_Cond_selector(n);
3695 if (mode_is_int(get_irn_mode(b))) {
3696 tarval *tb = value_of(b);
3698 if (tb != tarval_bad) {
3699 /* we have a constant switch */
3700 long num = get_Proj_proj(proj);
3702 if (num != get_Cond_defaultProj(n)) { /* we cannot optimize default Proj's yet */
3703 if (get_tarval_long(tb) == num) {
3704 /* Do NOT create a jump here, or we will have 2 control flow ops
3705 * in a block. This case is optimized away in optimize_cf(). */
3708 /* this case will NEVER be taken, kill it */
3716 } /* transform_node_Proj_Cond */
3719 * Create a 0 constant of given mode.
3721 static ir_node *create_zero_const(ir_mode *mode) {
3722 tarval *tv = get_mode_null(mode);
3723 ir_node *cnst = new_Const(mode, tv);
3728 /* the order of the values is important! */
3729 typedef enum const_class {
3735 static const_class classify_const(const ir_node* n)
3737 if (is_Const(n)) return const_const;
3738 if (is_irn_constlike(n)) return const_like;
3743 * Determines whether r is more constlike or has a larger index (in that order)
3746 static int operands_are_normalized(const ir_node *l, const ir_node *r)
3748 const const_class l_order = classify_const(l);
3749 const const_class r_order = classify_const(r);
3751 l_order > r_order ||
3752 (l_order == r_order && get_irn_idx(l) <= get_irn_idx(r));
3756 * Normalizes and optimizes Cmp nodes.
3758 static ir_node *transform_node_Proj_Cmp(ir_node *proj) {
3759 ir_node *n = get_Proj_pred(proj);
3760 ir_node *left = get_Cmp_left(n);
3761 ir_node *right = get_Cmp_right(n);
3764 ir_mode *mode = NULL;
3765 long proj_nr = get_Proj_proj(proj);
3767 /* we can evaluate some cases directly */
3770 return new_Const(mode_b, get_tarval_b_false());
3772 return new_Const(mode_b, get_tarval_b_true());
3774 if (!mode_is_float(get_irn_mode(left)))
3775 return new_Const(mode_b, get_tarval_b_true());
3781 /* remove Casts of both sides */
3782 left = skip_Cast(left);
3783 right = skip_Cast(right);
3785 /* Remove unnecessary conversions */
3786 /* TODO handle constants */
3787 if (is_Conv(left) && is_Conv(right)) {
3788 ir_mode *mode = get_irn_mode(left);
3789 ir_node *op_left = get_Conv_op(left);
3790 ir_node *op_right = get_Conv_op(right);
3791 ir_mode *mode_left = get_irn_mode(op_left);
3792 ir_mode *mode_right = get_irn_mode(op_right);
3794 if (smaller_mode(mode_left, mode) && smaller_mode(mode_right, mode)
3795 && mode_left != mode_b && mode_right != mode_b) {
3796 ir_graph *irg = current_ir_graph;
3797 ir_node *block = get_nodes_block(n);
3799 if (mode_left == mode_right) {
3803 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV_CONV);
3804 } else if (smaller_mode(mode_left, mode_right)) {
3805 left = new_r_Conv(irg, block, op_left, mode_right);
3808 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV);
3809 } else if (smaller_mode(mode_right, mode_left)) {
3811 right = new_r_Conv(irg, block, op_right, mode_left);
3813 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV);
3818 /* remove operation on both sides if possible */
3819 if (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) {
3821 * The following operations are NOT safe for floating point operations, for instance
3822 * 1.0 + inf == 2.0 + inf, =/=> x == y
3824 if (mode_is_int(get_irn_mode(left))) {
3825 unsigned lop = get_irn_opcode(left);
3827 if (lop == get_irn_opcode(right)) {
3828 ir_node *ll, *lr, *rl, *rr;
3830 /* same operation on both sides, try to remove */
3834 /* ~a CMP ~b => a CMP b, -a CMP -b ==> a CMP b */
3835 left = get_unop_op(left);
3836 right = get_unop_op(right);
3838 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3841 ll = get_Add_left(left);
3842 lr = get_Add_right(left);
3843 rl = get_Add_left(right);
3844 rr = get_Add_right(right);
3847 /* X + a CMP X + b ==> a CMP b */
3851 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3852 } else if (ll == rr) {
3853 /* X + a CMP b + X ==> a CMP b */
3857 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3858 } else if (lr == rl) {
3859 /* a + X CMP X + b ==> a CMP b */
3863 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3864 } else if (lr == rr) {
3865 /* a + X CMP b + X ==> a CMP b */
3869 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3873 ll = get_Sub_left(left);
3874 lr = get_Sub_right(left);
3875 rl = get_Sub_left(right);
3876 rr = get_Sub_right(right);
3879 /* X - a CMP X - b ==> a CMP b */
3883 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3884 } else if (lr == rr) {
3885 /* a - X CMP b - X ==> a CMP b */
3889 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3893 if (get_Rotl_right(left) == get_Rotl_right(right)) {
3894 /* a ROTL X CMP b ROTL X ==> a CMP b */
3895 left = get_Rotl_left(left);
3896 right = get_Rotl_left(right);
3898 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3906 /* X+A == A, A+X == A, A-X == A -> X == 0 */
3907 if (is_Add(left) || is_Sub(left)) {
3908 ir_node *ll = get_binop_left(left);
3909 ir_node *lr = get_binop_right(left);
3911 if (lr == right && is_Add(left)) {
3918 right = create_zero_const(get_irn_mode(left));
3920 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3923 if (is_Add(right) || is_Sub(right)) {
3924 ir_node *rl = get_binop_left(right);
3925 ir_node *rr = get_binop_right(right);
3927 if (rr == left && is_Add(right)) {
3934 right = create_zero_const(get_irn_mode(left));
3936 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3939 } /* mode_is_int(...) */
3940 } /* proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg */
3942 /* replace mode_b compares with ands/ors */
3943 if (get_irn_mode(left) == mode_b) {
3944 ir_graph *irg = current_ir_graph;
3945 ir_node *block = get_nodes_block(n);
3949 case pn_Cmp_Le: bres = new_r_Or( irg, block, new_r_Not(irg, block, left, mode_b), right, mode_b); break;
3950 case pn_Cmp_Lt: bres = new_r_And(irg, block, new_r_Not(irg, block, left, mode_b), right, mode_b); break;
3951 case pn_Cmp_Ge: bres = new_r_Or( irg, block, left, new_r_Not(irg, block, right, mode_b), mode_b); break;
3952 case pn_Cmp_Gt: bres = new_r_And(irg, block, left, new_r_Not(irg, block, right, mode_b), mode_b); break;
3953 case pn_Cmp_Lg: bres = new_r_Eor(irg, block, left, right, mode_b); break;
3954 case pn_Cmp_Eq: bres = new_r_Not(irg, block, new_r_Eor(irg, block, left, right, mode_b), mode_b); break;
3955 default: bres = NULL;
3958 DBG_OPT_ALGSIM0(n, bres, FS_OPT_CMP_TO_BOOL);
3964 * First step: normalize the compare op
3965 * by placing the constant on the right side
3966 * or moving the lower address node to the left.
3968 if (!operands_are_normalized(left, right)) {
3974 proj_nr = get_inversed_pnc(proj_nr);
3979 * Second step: Try to reduce the magnitude
3980 * of a constant. This may help to generate better code
3981 * later and may help to normalize more compares.
3982 * Of course this is only possible for integer values.
3984 if (is_Const(right)) {
3985 mode = get_irn_mode(right);
3986 tv = get_Const_tarval(right);
3988 /* TODO extend to arbitrary constants */
3989 if (is_Conv(left) && tarval_is_null(tv)) {
3990 ir_node *op = get_Conv_op(left);
3991 ir_mode *op_mode = get_irn_mode(op);
3994 * UpConv(x) REL 0 ==> x REL 0
3996 if (get_mode_size_bits(mode) > get_mode_size_bits(op_mode) &&
3997 ((proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) ||
3998 mode_is_signed(mode) || !mode_is_signed(op_mode))) {
3999 tv = get_mode_null(op_mode);
4003 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV);
4007 if (tv != tarval_bad) {
4008 /* the following optimization is possible on modes without Overflow
4009 * on Unary Minus or on == and !=:
4010 * -a CMP c ==> a swap(CMP) -c
4012 * Beware: for two-complement Overflow may occur, so only == and != can
4013 * be optimized, see this:
4014 * -MININT < 0 =/=> MININT > 0 !!!
4016 if (is_Minus(left) &&
4017 (!mode_overflow_on_unary_Minus(mode) ||
4018 (mode_is_int(mode) && (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg)))) {
4019 tv = tarval_neg(tv);
4021 if (tv != tarval_bad) {
4022 left = get_Minus_op(left);
4023 proj_nr = get_inversed_pnc(proj_nr);
4025 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4027 } else if (is_Not(left) && (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg)) {
4028 /* Not(a) ==/!= c ==> a ==/!= Not(c) */
4029 tv = tarval_not(tv);
4031 if (tv != tarval_bad) {
4032 left = get_Not_op(left);
4034 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4038 /* for integer modes, we have more */
4039 if (mode_is_int(mode)) {
4040 /* Ne includes Unordered which is not possible on integers.
4041 * However, frontends often use this wrong, so fix it here */
4042 if (proj_nr & pn_Cmp_Uo) {
4043 proj_nr &= ~pn_Cmp_Uo;
4044 set_Proj_proj(proj, proj_nr);
4047 /* c > 0 : a < c ==> a <= (c-1) a >= c ==> a > (c-1) */
4048 if ((proj_nr == pn_Cmp_Lt || proj_nr == pn_Cmp_Ge) &&
4049 tarval_cmp(tv, get_mode_null(mode)) == pn_Cmp_Gt) {
4050 tv = tarval_sub(tv, get_mode_one(mode));
4052 if (tv != tarval_bad) {
4053 proj_nr ^= pn_Cmp_Eq;
4055 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CNST_MAGN);
4058 /* c < 0 : a > c ==> a >= (c+1) a <= c ==> a < (c+1) */
4059 else if ((proj_nr == pn_Cmp_Gt || proj_nr == pn_Cmp_Le) &&
4060 tarval_cmp(tv, get_mode_null(mode)) == pn_Cmp_Lt) {
4061 tv = tarval_add(tv, get_mode_one(mode));
4063 if (tv != tarval_bad) {
4064 proj_nr ^= pn_Cmp_Eq;
4066 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CNST_MAGN);
4070 /* the following reassociations work only for == and != */
4071 if (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) {
4073 #if 0 /* Might be not that good in general */
4074 /* a-b == 0 ==> a == b, a-b != 0 ==> a != b */
4075 if (tarval_is_null(tv) && is_Sub(left)) {
4076 right = get_Sub_right(left);
4077 left = get_Sub_left(left);
4079 tv = value_of(right);
4081 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4085 if (tv != tarval_bad) {
4086 /* a-c1 == c2 ==> a == c2+c1, a-c1 != c2 ==> a != c2+c1 */
4088 ir_node *c1 = get_Sub_right(left);
4089 tarval *tv2 = value_of(c1);
4091 if (tv2 != tarval_bad) {
4092 tv2 = tarval_add(tv, value_of(c1));
4094 if (tv2 != tarval_bad) {
4095 left = get_Sub_left(left);
4098 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4102 /* a+c1 == c2 ==> a == c2-c1, a+c1 != c2 ==> a != c2-c1 */
4103 else if (is_Add(left)) {
4104 ir_node *a_l = get_Add_left(left);
4105 ir_node *a_r = get_Add_right(left);
4109 if (is_Const(a_l)) {
4111 tv2 = value_of(a_l);
4114 tv2 = value_of(a_r);
4117 if (tv2 != tarval_bad) {
4118 tv2 = tarval_sub(tv, tv2);
4120 if (tv2 != tarval_bad) {
4124 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4128 /* -a == c ==> a == -c, -a != c ==> a != -c */
4129 else if (is_Minus(left)) {
4130 tarval *tv2 = tarval_sub(get_mode_null(mode), tv);
4132 if (tv2 != tarval_bad) {
4133 left = get_Minus_op(left);
4136 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4141 /* the following reassociations work only for <= */
4142 else if (proj_nr == pn_Cmp_Le || proj_nr == pn_Cmp_Lt) {
4143 if (tv != tarval_bad) {
4144 /* c >= 0 : Abs(a) <= c ==> (unsigned)(a + c) <= 2*c */
4145 if (get_irn_op(left) == op_Abs) { // TODO something is missing here
4151 if (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) {
4152 switch (get_irn_opcode(left)) {
4156 c1 = get_And_right(left);
4159 * And(x, C1) == C2 ==> FALSE if C2 & C1 != C2
4160 * And(x, C1) != C2 ==> TRUE if C2 & C1 != C2
4162 tarval *mask = tarval_and(get_Const_tarval(c1), tv);
4164 /* TODO: move to constant evaluation */
4165 tv = proj_nr == pn_Cmp_Eq ? get_tarval_b_false() : get_tarval_b_true();
4166 c1 = new_Const(mode_b, tv);
4167 DBG_OPT_CSTEVAL(proj, c1);
4171 if (tarval_is_single_bit(tv)) {
4173 * optimization for AND:
4175 * And(x, C) == C ==> And(x, C) != 0
4176 * And(x, C) != C ==> And(X, C) == 0
4178 * if C is a single Bit constant.
4181 /* check for Constant's match. We have check hare the tarvals,
4182 because our const might be changed */
4183 if (get_Const_tarval(c1) == tv) {
4184 /* fine: do the transformation */
4185 tv = get_mode_null(get_tarval_mode(tv));
4186 proj_nr ^= pn_Cmp_Leg;
4188 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CNST_MAGN);
4194 c1 = get_Or_right(left);
4195 if (is_Const(c1) && tarval_is_null(tv)) {
4197 * Or(x, C) == 0 && C != 0 ==> FALSE
4198 * Or(x, C) != 0 && C != 0 ==> TRUE
4200 if (! tarval_is_null(get_Const_tarval(c1))) {
4201 /* TODO: move to constant evaluation */
4202 tv = proj_nr == pn_Cmp_Eq ? get_tarval_b_false() : get_tarval_b_true();
4203 c1 = new_Const(mode_b, tv);
4204 DBG_OPT_CSTEVAL(proj, c1);
4211 * optimize x << c1 == c into x & (-1 >>u c1) == c >> c1 if c & (-1 << c1) == c
4213 * optimize x << c1 != c into x & (-1 >>u c1) != c >> c1 if c & (-1 << c1) == c
4216 c1 = get_Shl_right(left);
4218 tarval *tv1 = get_Const_tarval(c1);
4219 ir_mode *mode = get_irn_mode(left);
4220 tarval *minus1 = get_mode_all_one(mode);
4221 tarval *amask = tarval_shr(minus1, tv1);
4222 tarval *cmask = tarval_shl(minus1, tv1);
4225 if (tarval_and(tv, cmask) != tv) {
4226 /* condition not met */
4227 tv = proj_nr == pn_Cmp_Eq ? get_tarval_b_false() : get_tarval_b_true();
4228 c1 = new_Const(mode_b, tv);
4229 DBG_OPT_CSTEVAL(proj, c1);
4232 sl = get_Shl_left(left);
4233 blk = get_nodes_block(n);
4234 left = new_rd_And(get_irn_dbg_info(left), current_ir_graph, blk, sl, new_Const(mode, amask), mode);
4235 tv = tarval_shr(tv, tv1);
4237 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_SHF_TO_AND);
4242 * optimize x >>u c1 == c into x & (-1 << c1) == c << c1 if c & (-1 >>u c1) == c
4244 * optimize x >>u c1 != c into x & (-1 << c1) != c << c1 if c & (-1 >>u c1) == c
4247 c1 = get_Shr_right(left);
4249 tarval *tv1 = get_Const_tarval(c1);
4250 ir_mode *mode = get_irn_mode(left);
4251 tarval *minus1 = get_mode_all_one(mode);
4252 tarval *amask = tarval_shl(minus1, tv1);
4253 tarval *cmask = tarval_shr(minus1, tv1);
4256 if (tarval_and(tv, cmask) != tv) {
4257 /* condition not met */
4258 tv = proj_nr == pn_Cmp_Eq ? get_tarval_b_false() : get_tarval_b_true();
4259 c1 = new_Const(mode_b, tv);
4260 DBG_OPT_CSTEVAL(proj, c1);
4263 sl = get_Shr_left(left);
4264 blk = get_nodes_block(n);
4265 left = new_rd_And(get_irn_dbg_info(left), current_ir_graph, blk, sl, new_Const(mode, amask), mode);
4266 tv = tarval_shl(tv, tv1);
4268 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_SHF_TO_AND);
4273 * optimize x >>s c1 == c into x & (-1 << c1) == c << c1 if (c >>s (BITS - c1)) \in {0,-1}
4275 * optimize x >>s c1 != c into x & (-1 << c1) != c << c1 if (c >>s (BITS - c1)) \in {0,-1}
4278 c1 = get_Shrs_right(left);
4280 tarval *tv1 = get_Const_tarval(c1);
4281 ir_mode *mode = get_irn_mode(left);
4282 tarval *minus1 = get_mode_all_one(mode);
4283 tarval *amask = tarval_shl(minus1, tv1);
4284 tarval *cond = new_tarval_from_long(get_mode_size_bits(mode), get_tarval_mode(tv1));
4287 cond = tarval_sub(cond, tv1);
4288 cond = tarval_shrs(tv, cond);
4290 if (!tarval_is_all_one(cond) && !tarval_is_null(cond)) {
4291 /* condition not met */
4292 tv = proj_nr == pn_Cmp_Eq ? get_tarval_b_false() : get_tarval_b_true();
4293 c1 = new_Const(mode_b, tv);
4294 DBG_OPT_CSTEVAL(proj, c1);
4297 sl = get_Shrs_left(left);
4298 blk = get_nodes_block(n);
4299 left = new_rd_And(get_irn_dbg_info(left), current_ir_graph, blk, sl, new_Const(mode, amask), mode);
4300 tv = tarval_shl(tv, tv1);
4302 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_SHF_TO_AND);
4307 } /* tarval != bad */
4310 if (changed & 2) /* need a new Const */
4311 right = new_Const(mode, tv);
4313 if ((proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) && is_Const(right) && is_Const_null(right) && is_Proj(left)) {
4314 ir_node *op = get_Proj_pred(left);
4316 if ((is_Mod(op) && get_Proj_proj(left) == pn_Mod_res) ||
4317 (is_DivMod(op) && get_Proj_proj(left) == pn_DivMod_res_mod)) {
4318 ir_node *c = get_binop_right(op);
4321 tarval *tv = get_Const_tarval(c);
4323 if (tarval_is_single_bit(tv)) {
4324 /* special case: (x % 2^n) CMP 0 ==> x & (2^n-1) CMP 0 */
4325 ir_node *v = get_binop_left(op);
4326 ir_node *blk = get_irn_n(op, -1);
4327 ir_mode *mode = get_irn_mode(v);
4329 tv = tarval_sub(tv, get_mode_one(mode));
4330 left = new_rd_And(get_irn_dbg_info(op), current_ir_graph, blk, v, new_Const(mode, tv), mode);
4332 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_MOD_TO_AND);
4339 ir_node *block = get_irn_n(n, -1); /* Beware of get_nodes_Block() */
4341 /* create a new compare */
4342 n = new_rd_Cmp(get_irn_dbg_info(n), current_ir_graph, block, left, right);
4343 proj = new_rd_Proj(get_irn_dbg_info(proj), current_ir_graph, block, n, get_irn_mode(proj), proj_nr);
4347 } /* transform_node_Proj_Cmp */
4350 * Does all optimizations on nodes that must be done on it's Proj's
4351 * because of creating new nodes.
4353 static ir_node *transform_node_Proj(ir_node *proj) {
4354 ir_node *n = get_Proj_pred(proj);
4356 switch (get_irn_opcode(n)) {
4358 return transform_node_Proj_Div(proj);
4361 return transform_node_Proj_Mod(proj);
4364 return transform_node_Proj_DivMod(proj);
4367 return transform_node_Proj_Cond(proj);
4370 return transform_node_Proj_Cmp(proj);
4373 /* should not happen, but if it does will be optimized away */
4374 return equivalent_node_Proj(proj);
4380 } /* transform_node_Proj */
4383 * Move Confirms down through Phi nodes.
4385 static ir_node *transform_node_Phi(ir_node *phi) {
4387 ir_mode *mode = get_irn_mode(phi);
4389 if (mode_is_reference(mode)) {
4390 n = get_irn_arity(phi);
4392 /* Beware of Phi0 */
4394 ir_node *pred = get_irn_n(phi, 0);
4395 ir_node *bound, *new_Phi, *block, **in;
4398 if (! is_Confirm(pred))
4401 bound = get_Confirm_bound(pred);
4402 pnc = get_Confirm_cmp(pred);
4404 NEW_ARR_A(ir_node *, in, n);
4405 in[0] = get_Confirm_value(pred);
4407 for (i = 1; i < n; ++i) {
4408 pred = get_irn_n(phi, i);
4410 if (! is_Confirm(pred) ||
4411 get_Confirm_bound(pred) != bound ||
4412 get_Confirm_cmp(pred) != pnc)
4414 in[i] = get_Confirm_value(pred);
4416 /* move the Confirm nodes "behind" the Phi */
4417 block = get_irn_n(phi, -1);
4418 new_Phi = new_r_Phi(current_ir_graph, block, n, in, get_irn_mode(phi));
4419 return new_r_Confirm(current_ir_graph, block, new_Phi, bound, pnc);
4423 } /* transform_node_Phi */
4426 * Returns the operands of a commutative bin-op, if one operand is
4427 * a const, it is returned as the second one.
4429 static void get_comm_Binop_Ops(ir_node *binop, ir_node **a, ir_node **c) {
4430 ir_node *op_a = get_binop_left(binop);
4431 ir_node *op_b = get_binop_right(binop);
4433 assert(is_op_commutative(get_irn_op(binop)));
4435 if (is_Const(op_a)) {
4442 } /* get_comm_Binop_Ops */
4445 * Optimize a Or(And(Or(And(v,c4),c3),c2),c1) pattern if possible.
4446 * Such pattern may arise in bitfield stores.
4448 * value c4 value c4 & c2
4449 * AND c3 AND c1 | c3
4456 * AND c1 ===> OR if (c1 | c2) == 0x111..11
4459 static ir_node *transform_node_Or_bf_store(ir_node *or) {
4462 ir_node *and_l, *c3;
4463 ir_node *value, *c4;
4464 ir_node *new_and, *new_const, *block;
4465 ir_mode *mode = get_irn_mode(or);
4467 tarval *tv1, *tv2, *tv3, *tv4, *tv, *n_tv4, *n_tv2;
4470 get_comm_Binop_Ops(or, &and, &c1);
4471 if (!is_Const(c1) || !is_And(and))
4474 get_comm_Binop_Ops(and, &or_l, &c2);
4478 tv1 = get_Const_tarval(c1);
4479 tv2 = get_Const_tarval(c2);
4481 tv = tarval_or(tv1, tv2);
4482 if (tarval_is_all_one(tv)) {
4483 /* the AND does NOT clear a bit with isn't set by the OR */
4484 set_Or_left(or, or_l);
4485 set_Or_right(or, c1);
4487 /* check for more */
4494 get_comm_Binop_Ops(or_l, &and_l, &c3);
4495 if (!is_Const(c3) || !is_And(and_l))
4498 get_comm_Binop_Ops(and_l, &value, &c4);
4502 /* ok, found the pattern, check for conditions */
4503 assert(mode == get_irn_mode(and));
4504 assert(mode == get_irn_mode(or_l));
4505 assert(mode == get_irn_mode(and_l));
4507 tv3 = get_Const_tarval(c3);
4508 tv4 = get_Const_tarval(c4);
4510 tv = tarval_or(tv4, tv2);
4511 if (!tarval_is_all_one(tv)) {
4512 /* have at least one 0 at the same bit position */
4516 n_tv4 = tarval_not(tv4);
4517 if (tv3 != tarval_and(tv3, n_tv4)) {
4518 /* bit in the or_mask is outside the and_mask */
4522 n_tv2 = tarval_not(tv2);
4523 if (tv1 != tarval_and(tv1, n_tv2)) {
4524 /* bit in the or_mask is outside the and_mask */
4528 /* ok, all conditions met */
4529 block = get_irn_n(or, -1);
4531 new_and = new_r_And(current_ir_graph, block,
4532 value, new_r_Const(current_ir_graph, block, mode, tarval_and(tv4, tv2)), mode);
4534 new_const = new_r_Const(current_ir_graph, block, mode, tarval_or(tv3, tv1));
4536 set_Or_left(or, new_and);
4537 set_Or_right(or, new_const);
4539 /* check for more */
4541 } /* transform_node_Or_bf_store */
4544 * Optimize an Or(shl(x, c), shr(x, bits - c)) into a Rotl
4546 static ir_node *transform_node_Or_Rotl(ir_node *or) {
4547 ir_mode *mode = get_irn_mode(or);
4548 ir_node *shl, *shr, *block;
4549 ir_node *irn, *x, *c1, *c2, *v, *sub, *n, *rotval;
4552 if (! mode_is_int(mode))
4555 shl = get_binop_left(or);
4556 shr = get_binop_right(or);
4565 } else if (!is_Shl(shl)) {
4567 } else if (!is_Shr(shr)) {
4570 x = get_Shl_left(shl);
4571 if (x != get_Shr_left(shr))
4574 c1 = get_Shl_right(shl);
4575 c2 = get_Shr_right(shr);
4576 if (is_Const(c1) && is_Const(c2)) {
4577 tv1 = get_Const_tarval(c1);
4578 if (! tarval_is_long(tv1))
4581 tv2 = get_Const_tarval(c2);
4582 if (! tarval_is_long(tv2))
4585 if (get_tarval_long(tv1) + get_tarval_long(tv2)
4586 != (int) get_mode_size_bits(mode))
4589 /* yet, condition met */
4590 block = get_nodes_block(or);
4592 n = new_r_Rotl(current_ir_graph, block, x, c1, mode);
4594 DBG_OPT_ALGSIM1(or, shl, shr, n, FS_OPT_OR_SHFT_TO_ROTL);
4601 rotval = sub; /* a Rot right is not supported, so use a rot left */
4602 } else if (is_Sub(c2)) {
4608 if (get_Sub_right(sub) != v)
4611 c1 = get_Sub_left(sub);
4615 tv1 = get_Const_tarval(c1);
4616 if (! tarval_is_long(tv1))
4619 if (get_tarval_long(tv1) != (int) get_mode_size_bits(mode))
4622 /* yet, condition met */
4623 block = get_nodes_block(or);
4625 n = new_r_Rotl(current_ir_graph, block, x, rotval, mode);
4627 DBG_OPT_ALGSIM0(or, n, FS_OPT_OR_SHFT_TO_ROTL);
4629 } /* transform_node_Or_Rotl */
4634 static ir_node *transform_node_Or(ir_node *n) {
4635 ir_node *c, *oldn = n;
4636 ir_node *a = get_Or_left(n);
4637 ir_node *b = get_Or_right(n);
4640 if (is_Not(a) && is_Not(b)) {
4641 /* ~a | ~b = ~(a&b) */
4642 ir_node *block = get_nodes_block(n);
4644 mode = get_irn_mode(n);
4647 n = new_rd_And(get_irn_dbg_info(n), current_ir_graph, block, a, b, mode);
4648 n = new_rd_Not(get_irn_dbg_info(n), current_ir_graph, block, n, mode);
4649 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_DEMORGAN);
4653 /* we can evaluate 2 Projs of the same Cmp */
4654 if (get_irn_mode(n) == mode_b && is_Proj(a) && is_Proj(b)) {
4655 ir_node *pred_a = get_Proj_pred(a);
4656 ir_node *pred_b = get_Proj_pred(b);
4657 if (pred_a == pred_b) {
4658 dbg_info *dbgi = get_irn_dbg_info(n);
4659 ir_node *block = get_nodes_block(pred_a);
4660 pn_Cmp pn_a = get_Proj_proj(a);
4661 pn_Cmp pn_b = get_Proj_proj(b);
4662 /* yes, we can simply calculate with pncs */
4663 pn_Cmp new_pnc = pn_a | pn_b;
4665 return new_rd_Proj(dbgi, current_ir_graph, block, pred_a, mode_b,
4670 mode = get_irn_mode(n);
4671 HANDLE_BINOP_PHI(tarval_or, a, b, c, mode);
4673 n = transform_node_Or_bf_store(n);
4674 n = transform_node_Or_Rotl(n);
4678 n = transform_bitwise_distributive(n, transform_node_Or);
4681 } /* transform_node_Or */
4685 static ir_node *transform_node(ir_node *n);
4688 * Optimize (a >> c1) >> c2), works for Shr, Shrs, Shl, Rotl.
4690 * Should be moved to reassociation?
4692 static ir_node *transform_node_shift(ir_node *n) {
4693 ir_node *left, *right;
4695 tarval *tv1, *tv2, *res;
4696 ir_node *in[2], *irn, *block;
4698 left = get_binop_left(n);
4700 /* different operations */
4701 if (get_irn_op(left) != get_irn_op(n))
4704 right = get_binop_right(n);
4705 tv1 = value_of(right);
4706 if (tv1 == tarval_bad)
4709 tv2 = value_of(get_binop_right(left));
4710 if (tv2 == tarval_bad)
4713 res = tarval_add(tv1, tv2);
4714 mode = get_irn_mode(n);
4716 /* beware: a simple replacement works only, if res < modulo shift */
4718 int modulo_shf = get_mode_modulo_shift(mode);
4719 assert(modulo_shf >= (int) get_mode_size_bits(mode));
4720 if (modulo_shf > 0) {
4721 tarval *modulo = new_tarval_from_long(modulo_shf,
4722 get_tarval_mode(res));
4724 /* shifting too much */
4725 if (!(tarval_cmp(res, modulo) & pn_Cmp_Lt)) {
4727 ir_graph *irg = get_irn_irg(n);
4728 ir_node *block = get_nodes_block(n);
4729 dbg_info *dbgi = get_irn_dbg_info(n);
4730 ir_node *cnst = new_Const(mode_Iu, new_tarval_from_long(get_mode_size_bits(mode)-1, mode_Iu));
4731 return new_rd_Shrs(dbgi, irg, block, get_binop_left(left),
4735 return new_Const(mode, get_mode_null(mode));
4739 res = tarval_mod(res, new_tarval_from_long(get_mode_size_bits(mode), get_tarval_mode(res)));
4742 /* ok, we can replace it */
4743 block = get_nodes_block(n);
4745 in[0] = get_binop_left(left);
4746 in[1] = new_r_Const(current_ir_graph, block, get_tarval_mode(res), res);
4748 irn = new_ir_node(NULL, current_ir_graph, block, get_irn_op(n), mode, 2, in);
4750 DBG_OPT_ALGSIM0(n, irn, FS_OPT_REASSOC_SHIFT);
4752 return transform_node(irn);
4753 } /* transform_node_shift */
4756 * normalisation: (x & c1) >> c2 to (x >> c2) & (c1 >> c2)
4758 * - and, or, xor instead of &
4759 * - Shl, Shr, Shrs, rotl instead of >>
4760 * (with a special case for Or/Xor + Shrs)
4762 static ir_node *transform_node_bitop_shift(ir_node *n) {
4764 ir_node *right = get_binop_right(n);
4765 ir_mode *mode = get_irn_mode(n);
4766 ir_node *bitop_left;
4767 ir_node *bitop_right;
4779 assert(is_Shrs(n) || is_Shr(n) || is_Shl(n) || is_Rotl(n));
4781 if (!is_Const(right))
4784 left = get_binop_left(n);
4785 op_left = get_irn_op(left);
4786 if (op_left != op_And && op_left != op_Or && op_left != op_Eor)
4789 /* doing it with Shrs is not legal if the Or/Eor affects the topmost bit */
4790 if (is_Shrs(n) && (op_left == op_Or || op_left == op_Eor)) {
4791 /* TODO: test if sign bit is affectes */
4795 bitop_right = get_binop_right(left);
4796 if (!is_Const(bitop_right))
4799 bitop_left = get_binop_left(left);
4801 irg = get_irn_irg(n);
4802 block = get_nodes_block(n);
4803 dbgi = get_irn_dbg_info(n);
4804 tv1 = get_Const_tarval(bitop_right);
4805 tv2 = get_Const_tarval(right);
4807 assert(get_tarval_mode(tv1) == mode);
4810 new_shift = new_rd_Shl(dbgi, irg, block, bitop_left, right, mode);
4811 tv_shift = tarval_shl(tv1, tv2);
4812 } else if(is_Shr(n)) {
4813 new_shift = new_rd_Shr(dbgi, irg, block, bitop_left, right, mode);
4814 tv_shift = tarval_shr(tv1, tv2);
4815 } else if(is_Shrs(n)) {
4816 new_shift = new_rd_Shrs(dbgi, irg, block, bitop_left, right, mode);
4817 tv_shift = tarval_shrs(tv1, tv2);
4820 new_shift = new_rd_Rotl(dbgi, irg, block, bitop_left, right, mode);
4821 tv_shift = tarval_rotl(tv1, tv2);
4824 assert(get_tarval_mode(tv_shift) == mode);
4825 new_const = new_Const(mode, tv_shift);
4827 if (op_left == op_And) {
4828 new_bitop = new_rd_And(dbgi, irg, block, new_shift, new_const, mode);
4829 } else if(op_left == op_Or) {
4830 new_bitop = new_rd_Or(dbgi, irg, block, new_shift, new_const, mode);
4832 assert(op_left == op_Eor);
4833 new_bitop = new_rd_Eor(dbgi, irg, block, new_shift, new_const, mode);
4841 * (x << c1) >> c2 <=> x OP (c2-c1) & ((-1 << c1) >> c2)
4843 * (x >> c1) << c2 <=> x OP (c2-c1) & ((-1 >> c1) << c2)
4844 * (also with x >>s c1 when c1>=c2)
4846 static ir_node *transform_node_shl_shr(ir_node *n) {
4848 ir_node *right = get_binop_right(n);
4864 assert(is_Shl(n) || is_Shr(n) || is_Shrs(n));
4866 if (!is_Const(right))
4869 left = get_binop_left(n);
4870 mode = get_irn_mode(n);
4871 if (is_Shl(n) && (is_Shr(left) || is_Shrs(left))) {
4872 ir_node *shr_right = get_binop_right(left);
4874 if (!is_Const(shr_right))
4877 x = get_binop_left(left);
4878 tv_shr = get_Const_tarval(shr_right);
4879 tv_shl = get_Const_tarval(right);
4881 if (is_Shrs(left)) {
4882 /* shrs variant only allowed if c1 >= c2 */
4883 if (! (tarval_cmp(tv_shl, tv_shr) & pn_Cmp_Ge))
4886 tv_mask = tarval_shrs(get_mode_all_one(mode), tv_shr);
4889 tv_mask = tarval_shr(get_mode_all_one(mode), tv_shr);
4891 tv_mask = tarval_shl(tv_mask, tv_shl);
4892 } else if(is_Shr(n) && is_Shl(left)) {
4893 ir_node *shl_right = get_Shl_right(left);
4895 if (!is_Const(shl_right))
4898 x = get_Shl_left(left);
4899 tv_shr = get_Const_tarval(right);
4900 tv_shl = get_Const_tarval(shl_right);
4902 tv_mask = tarval_shl(get_mode_all_one(mode), tv_shl);
4903 tv_mask = tarval_shr(tv_mask, tv_shr);
4908 assert(get_tarval_mode(tv_shl) == get_tarval_mode(tv_shr));
4909 assert(tv_mask != tarval_bad);
4910 assert(get_tarval_mode(tv_mask) == mode);
4912 irg = get_irn_irg(n);
4913 block = get_nodes_block(n);
4914 dbgi = get_irn_dbg_info(n);
4916 pnc = tarval_cmp(tv_shl, tv_shr);
4917 if (pnc == pn_Cmp_Lt || pnc == pn_Cmp_Eq) {
4918 tv_shift = tarval_sub(tv_shr, tv_shl);
4919 new_const = new_Const(get_tarval_mode(tv_shift), tv_shift);
4921 new_shift = new_rd_Shrs(dbgi, irg, block, x, new_const, mode);
4923 new_shift = new_rd_Shr(dbgi, irg, block, x, new_const, mode);
4926 assert(pnc == pn_Cmp_Gt);
4927 tv_shift = tarval_sub(tv_shl, tv_shr);
4928 new_const = new_Const(get_tarval_mode(tv_shift), tv_shift);
4929 new_shift = new_rd_Shl(dbgi, irg, block, x, new_const, mode);
4932 new_const = new_Const(mode, tv_mask);
4933 new_and = new_rd_And(dbgi, irg, block, new_shift, new_const, mode);
4941 static ir_node *transform_node_Shr(ir_node *n) {
4942 ir_node *c, *oldn = n;
4943 ir_node *left = get_Shr_left(n);
4944 ir_node *right = get_Shr_right(n);
4945 ir_mode *mode = get_irn_mode(n);
4947 HANDLE_BINOP_PHI(tarval_shr, left, right, c, mode);
4948 n = transform_node_shift(n);
4951 n = transform_node_shl_shr(n);
4953 n = transform_node_bitop_shift(n);
4956 } /* transform_node_Shr */
4961 static ir_node *transform_node_Shrs(ir_node *n) {
4962 ir_node *c, *oldn = n;
4963 ir_node *a = get_Shrs_left(n);
4964 ir_node *b = get_Shrs_right(n);
4965 ir_mode *mode = get_irn_mode(n);
4967 HANDLE_BINOP_PHI(tarval_shrs, a, b, c, mode);
4968 n = transform_node_shift(n);
4971 n = transform_node_bitop_shift(n);
4974 } /* transform_node_Shrs */
4979 static ir_node *transform_node_Shl(ir_node *n) {
4980 ir_node *c, *oldn = n;
4981 ir_node *a = get_Shl_left(n);
4982 ir_node *b = get_Shl_right(n);
4983 ir_mode *mode = get_irn_mode(n);
4985 HANDLE_BINOP_PHI(tarval_shl, a, b, c, mode);
4986 n = transform_node_shift(n);
4989 n = transform_node_shl_shr(n);
4991 n = transform_node_bitop_shift(n);
4994 } /* transform_node_Shl */
4999 static ir_node *transform_node_Rotl(ir_node *n) {
5000 ir_node *c, *oldn = n;
5001 ir_node *a = get_Rotl_left(n);
5002 ir_node *b = get_Rotl_right(n);
5003 ir_mode *mode = get_irn_mode(n);
5005 HANDLE_BINOP_PHI(tarval_rotl, a, b, c, mode);
5006 n = transform_node_shift(n);
5009 n = transform_node_bitop_shift(n);
5012 } /* transform_node_Rotl */
5017 static ir_node *transform_node_Conv(ir_node *n) {
5018 ir_node *c, *oldn = n;
5019 ir_node *a = get_Conv_op(n);
5021 if (is_const_Phi(a)) {
5022 c = apply_conv_on_phi(a, get_irn_mode(n));
5024 DBG_OPT_ALGSIM0(oldn, c, FS_OPT_CONST_PHI);
5029 if (is_Unknown(a)) { /* Conv_A(Unknown_B) -> Unknown_A */
5030 ir_mode *mode = get_irn_mode(n);
5031 return new_r_Unknown(current_ir_graph, mode);
5035 } /* transform_node_Conv */
5038 * Remove dead blocks and nodes in dead blocks
5039 * in keep alive list. We do not generate a new End node.
5041 static ir_node *transform_node_End(ir_node *n) {
5042 int i, j, n_keepalives = get_End_n_keepalives(n);
5045 NEW_ARR_A(ir_node *, in, n_keepalives);
5047 for (i = j = 0; i < n_keepalives; ++i) {
5048 ir_node *ka = get_End_keepalive(n, i);
5050 if (! is_Block_dead(ka)) {
5054 } else if (is_irn_pinned_in_irg(ka) && is_Block_dead(get_nodes_block(ka))) {
5057 /* FIXME: beabi need to keep a Proj(M) */
5058 if (is_Phi(ka) || is_irn_keep(ka) || is_Proj(ka))
5061 if (j != n_keepalives)
5062 set_End_keepalives(n, j, in);
5064 } /* transform_node_End */
5066 /** returns 1 if a == -b */
5067 static int is_negated_value(ir_node *a, ir_node *b) {
5068 if (is_Minus(a) && get_Minus_op(a) == b)
5070 if (is_Minus(b) && get_Minus_op(b) == a)
5072 if (is_Sub(a) && is_Sub(b)) {
5073 ir_node *a_left = get_Sub_left(a);
5074 ir_node *a_right = get_Sub_right(a);
5075 ir_node *b_left = get_Sub_left(b);
5076 ir_node *b_right = get_Sub_right(b);
5078 if (a_left == b_right && a_right == b_left)
5086 * Optimize a Mux into some simpler cases.
5088 static ir_node *transform_node_Mux(ir_node *n) {
5089 ir_node *oldn = n, *sel = get_Mux_sel(n);
5090 ir_mode *mode = get_irn_mode(n);
5091 ir_node *t = get_Mux_true(n);
5092 ir_node *f = get_Mux_false(n);
5093 ir_graph *irg = current_ir_graph;
5094 ir_node *conds[1], *vals[2];
5096 /* first normalization step: move a possible zero to the false case */
5098 ir_node *cmp = get_Proj_pred(sel);
5101 if (is_Const(t) && is_Const_null(t)) {
5102 /* Psi(x, 0, y) => Psi(x, y, 0) */
5103 pn_Cmp pnc = get_Proj_proj(sel);
5104 sel = new_r_Proj(irg, get_nodes_block(cmp), cmp, mode_b,
5105 get_negated_pnc(pnc, get_irn_mode(get_Cmp_left(cmp))));
5109 n = new_rd_Psi(get_irn_dbg_info(n), irg, get_nodes_block(n), 1, conds, vals, mode);
5116 /* note: after normalization, false can only happen on default */
5117 if (mode == mode_b) {
5118 dbg_info *dbg = get_irn_dbg_info(n);
5119 ir_node *block = get_nodes_block(n);
5120 ir_graph *irg = current_ir_graph;
5123 tarval *tv_t = get_Const_tarval(t);
5124 if (tv_t == tarval_b_true) {
5126 /* Muxb(sel, true, false) = sel */
5127 assert(get_Const_tarval(f) == tarval_b_false);
5128 DBG_OPT_ALGSIM0(oldn, sel, FS_OPT_MUX_BOOL);
5131 /* Muxb(sel, true, x) = Or(sel, x) */
5132 n = new_rd_Or(dbg, irg, block, sel, f, mode_b);
5133 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_OR_BOOL);
5137 } else if (is_Const(f)) {
5138 tarval *tv_f = get_Const_tarval(f);
5139 if (tv_f == tarval_b_true) {
5140 /* Muxb(sel, x, true) = Or(Not(sel), x) */
5141 ir_node* not_sel = new_rd_Not(dbg, irg, block, sel, mode_b);
5142 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_ORNOT_BOOL);
5143 n = new_rd_Or(dbg, irg, block, not_sel, t, mode_b);
5146 /* Muxb(sel, x, false) = And(sel, x) */
5147 assert(tv_f == tarval_b_false);
5148 n = new_rd_And(dbg, irg, block, sel, t, mode_b);
5149 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_AND_BOOL);
5155 /* more normalization: try to normalize Mux(x, C1, C2) into Mux(x, +1/-1, 0) op C2 */
5156 if (is_Const(t) && is_Const(f) && mode_is_int(mode)) {
5157 tarval *a = get_Const_tarval(t);
5158 tarval *b = get_Const_tarval(f);
5159 tarval *null = get_tarval_null(mode);
5162 if (tarval_cmp(a, b) & pn_Cmp_Gt) {
5163 diff = tarval_sub(a, b);
5166 diff = tarval_sub(b, a);
5170 if (diff == get_tarval_one(mode) && min != null) {
5171 dbg_info *dbg = get_irn_dbg_info(n);
5172 ir_node *block = get_nodes_block(n);
5173 ir_graph *irg = current_ir_graph;
5177 vals[0] = new_Const(mode, tarval_sub(a, min));
5178 vals[1] = new_Const(mode, tarval_sub(b, min));
5179 n = new_rd_Psi(dbg, irg, block, 1, conds, vals, mode);
5180 n = new_rd_Add(dbg, irg, block, n, new_Const(mode, min), mode);
5186 ir_node *cmp = get_Proj_pred(sel);
5187 long pn = get_Proj_proj(sel);
5190 * Note: normalization puts the constant on the right side,
5191 * so we check only one case.
5193 * Note further that these optimization work even for floating point
5194 * with NaN's because -NaN == NaN.
5195 * However, if +0 and -0 is handled differently, we cannot use the Abs/-Abs
5199 ir_node *cmp_r = get_Cmp_right(cmp);
5200 if (is_Const(cmp_r) && is_Const_null(cmp_r)) {
5201 ir_node *block = get_nodes_block(n);
5202 ir_node *cmp_l = get_Cmp_left(cmp);
5204 if (!mode_honor_signed_zeros(mode) && is_negated_value(f, t)) {
5207 if ( (cmp_l == t && (pn == pn_Cmp_Ge || pn == pn_Cmp_Gt))
5208 || (cmp_l == f && (pn == pn_Cmp_Le || pn == pn_Cmp_Lt)))
5210 /* Psi(a >/>= 0, a, -a) = Psi(a </<= 0, -a, a) ==> Abs(a) */
5211 n = new_rd_Abs(get_irn_dbg_info(n), current_ir_graph, block,
5213 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_ABS);
5215 } else if ((cmp_l == t && (pn == pn_Cmp_Le || pn == pn_Cmp_Lt))
5216 || (cmp_l == f && (pn == pn_Cmp_Ge || pn == pn_Cmp_Gt)))
5218 /* Psi(a </<= 0, a, -a) = Psi(a >/>= 0, -a, a) ==> -Abs(a) */
5219 n = new_rd_Abs(get_irn_dbg_info(n), current_ir_graph, block,
5221 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph,
5223 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_ABS);
5228 if (mode_is_int(mode)) {
5230 if ((pn == pn_Cmp_Lg || pn == pn_Cmp_Eq) && is_And(cmp_l)) {
5231 /* Psi((a & b) != 0, c, 0) */
5232 ir_node *and_r = get_And_right(cmp_l);
5235 if (and_r == t && f == cmp_r) {
5236 if (is_Const(t) && tarval_is_single_bit(get_Const_tarval(t))) {
5237 if (pn == pn_Cmp_Lg) {
5238 /* Psi((a & 2^C) != 0, 2^C, 0) */
5241 /* Psi((a & 2^C) == 0, 2^C, 0) */
5242 n = new_rd_Eor(get_irn_dbg_info(n), current_ir_graph,
5243 block, cmp_l, t, mode);
5248 if (is_Shl(and_r)) {
5249 ir_node *shl_l = get_Shl_left(and_r);
5250 if (is_Const(shl_l) && is_Const_one(shl_l)) {
5251 if (and_r == t && f == cmp_r) {
5252 if (pn == pn_Cmp_Lg) {
5253 /* (a & (1 << n)) != 0, (1 << n), 0) */
5256 /* (a & (1 << n)) == 0, (1 << n), 0) */
5257 n = new_rd_Eor(get_irn_dbg_info(n), current_ir_graph,
5258 block, cmp_l, t, mode);
5264 and_l = get_And_left(cmp_l);
5265 if (is_Shl(and_l)) {
5266 ir_node *shl_l = get_Shl_left(and_l);
5267 if (is_Const(shl_l) && is_Const_one(shl_l)) {
5268 if (and_l == t && f == cmp_r) {
5269 if (pn == pn_Cmp_Lg) {
5270 /* ((1 << n) & a) != 0, (1 << n), 0) */
5273 /* ((1 << n) & a) == 0, (1 << n), 0) */
5274 n = new_rd_Eor(get_irn_dbg_info(n), current_ir_graph,
5275 block, cmp_l, t, mode);
5286 return arch_transform_node_Mux(n);
5287 } /* transform_node_Mux */
5290 * Optimize a Psi into some simpler cases.
5292 static ir_node *transform_node_Psi(ir_node *n) {
5294 return transform_node_Mux(n);
5297 } /* transform_node_Psi */
5300 * optimize sync nodes that have other syncs as input we simply add the inputs
5301 * of the other sync to our own inputs
5303 static ir_node *transform_node_Sync(ir_node *n) {
5304 int arity = get_Sync_n_preds(n);
5307 for (i = 0; i < arity;) {
5308 ir_node *pred = get_Sync_pred(n, i);
5312 if (!is_Sync(pred)) {
5320 pred_arity = get_Sync_n_preds(pred);
5321 for (j = 0; j < pred_arity; ++j) {
5322 ir_node *pred_pred = get_Sync_pred(pred, j);
5327 add_irn_n(n, pred_pred);
5331 if (get_Sync_pred(n, k) == pred_pred) break;
5336 /* rehash the sync node */
5337 add_identities(current_ir_graph->value_table, n);
5343 * Tries several [inplace] [optimizing] transformations and returns an
5344 * equivalent node. The difference to equivalent_node() is that these
5345 * transformations _do_ generate new nodes, and thus the old node must
5346 * not be freed even if the equivalent node isn't the old one.
5348 static ir_node *transform_node(ir_node *n) {
5352 * Transform_node is the only "optimizing transformation" that might
5353 * return a node with a different opcode. We iterate HERE until fixpoint
5354 * to get the final result.
5358 if (n->op->ops.transform_node)
5359 n = n->op->ops.transform_node(n);
5360 } while (oldn != n);
5363 } /* transform_node */
5366 * Sets the default transform node operation for an ir_op_ops.
5368 * @param code the opcode for the default operation
5369 * @param ops the operations initialized
5374 static ir_op_ops *firm_set_default_transform_node(ir_opcode code, ir_op_ops *ops)
5378 ops->transform_node = transform_node_##a; \
5415 } /* firm_set_default_transform_node */
5418 /* **************** Common Subexpression Elimination **************** */
5420 /** The size of the hash table used, should estimate the number of nodes
5422 #define N_IR_NODES 512
5424 /** Compares the attributes of two Const nodes. */
5425 static int node_cmp_attr_Const(ir_node *a, ir_node *b) {
5426 return (get_Const_tarval(a) != get_Const_tarval(b))
5427 || (get_Const_type(a) != get_Const_type(b));
5428 } /* node_cmp_attr_Const */
5430 /** Compares the attributes of two Proj nodes. */
5431 static int node_cmp_attr_Proj(ir_node *a, ir_node *b) {
5432 return get_irn_proj_attr(a) != get_irn_proj_attr(b);
5433 } /* node_cmp_attr_Proj */
5435 /** Compares the attributes of two Filter nodes. */
5436 static int node_cmp_attr_Filter(ir_node *a, ir_node *b) {
5437 return get_Filter_proj(a) != get_Filter_proj(b);
5438 } /* node_cmp_attr_Filter */
5440 /** Compares the attributes of two Alloc nodes. */
5441 static int node_cmp_attr_Alloc(ir_node *a, ir_node *b) {
5442 const alloc_attr *pa = get_irn_alloc_attr(a);
5443 const alloc_attr *pb = get_irn_alloc_attr(b);
5444 return (pa->where != pb->where) || (pa->type != pb->type);
5445 } /* node_cmp_attr_Alloc */
5447 /** Compares the attributes of two Free nodes. */
5448 static int node_cmp_attr_Free(ir_node *a, ir_node *b) {
5449 const free_attr *pa = get_irn_free_attr(a);
5450 const free_attr *pb = get_irn_free_attr(b);
5451 return (pa->where != pb->where) || (pa->type != pb->type);
5452 } /* node_cmp_attr_Free */
5454 /** Compares the attributes of two SymConst nodes. */
5455 static int node_cmp_attr_SymConst(ir_node *a, ir_node *b) {
5456 const symconst_attr *pa = get_irn_symconst_attr(a);
5457 const symconst_attr *pb = get_irn_symconst_attr(b);
5458 return (pa->kind != pb->kind)
5459 || (pa->sym.type_p != pb->sym.type_p)
5460 || (pa->tp != pb->tp);
5461 } /* node_cmp_attr_SymConst */
5463 /** Compares the attributes of two Call nodes. */
5464 static int node_cmp_attr_Call(ir_node *a, ir_node *b) {
5465 return get_irn_call_attr(a) != get_irn_call_attr(b);
5466 } /* node_cmp_attr_Call */
5468 /** Compares the attributes of two Sel nodes. */
5469 static int node_cmp_attr_Sel(ir_node *a, ir_node *b) {
5470 const ir_entity *a_ent = get_Sel_entity(a);
5471 const ir_entity *b_ent = get_Sel_entity(b);
5473 (a_ent->kind != b_ent->kind) ||
5474 (a_ent->name != b_ent->name) ||
5475 (a_ent->owner != b_ent->owner) ||
5476 (a_ent->ld_name != b_ent->ld_name) ||
5477 (a_ent->type != b_ent->type);
5478 } /* node_cmp_attr_Sel */
5480 /** Compares the attributes of two Phi nodes. */
5481 static int node_cmp_attr_Phi(ir_node *a, ir_node *b) {
5482 /* we can only enter this function if both nodes have the same number of inputs,
5483 hence it is enough to check if one of them is a Phi0 */
5485 /* check the Phi0 pos attribute */
5486 return get_irn_phi_attr(a)->u.pos != get_irn_phi_attr(b)->u.pos;
5489 } /* node_cmp_attr_Phi */
5491 /** Compares the attributes of two Conv nodes. */
5492 static int node_cmp_attr_Conv(ir_node *a, ir_node *b) {
5493 return get_Conv_strict(a) != get_Conv_strict(b);
5494 } /* node_cmp_attr_Conv */
5496 /** Compares the attributes of two Cast nodes. */
5497 static int node_cmp_attr_Cast(ir_node *a, ir_node *b) {
5498 return get_Cast_type(a) != get_Cast_type(b);
5499 } /* node_cmp_attr_Cast */
5501 /** Compares the attributes of two Load nodes. */
5502 static int node_cmp_attr_Load(ir_node *a, ir_node *b) {
5503 if (get_Load_volatility(a) == volatility_is_volatile ||
5504 get_Load_volatility(b) == volatility_is_volatile)
5505 /* NEVER do CSE on volatile Loads */
5507 /* do not CSE Loads with different alignment. Be conservative. */
5508 if (get_Load_align(a) != get_Load_align(b))
5511 return get_Load_mode(a) != get_Load_mode(b);
5512 } /* node_cmp_attr_Load */
5514 /** Compares the attributes of two Store nodes. */
5515 static int node_cmp_attr_Store(ir_node *a, ir_node *b) {
5516 /* do not CSE Stores with different alignment. Be conservative. */
5517 if (get_Store_align(a) != get_Store_align(b))
5520 /* NEVER do CSE on volatile Stores */
5521 return (get_Store_volatility(a) == volatility_is_volatile ||
5522 get_Store_volatility(b) == volatility_is_volatile);
5523 } /* node_cmp_attr_Store */
5525 /** Compares two exception attributes */
5526 static int node_cmp_exception(ir_node *a, ir_node *b) {
5527 const except_attr *ea = get_irn_except_attr(a);
5528 const except_attr *eb = get_irn_except_attr(b);
5530 return ea->pin_state != eb->pin_state;
5533 #define node_cmp_attr_Bound node_cmp_exception
5535 /** Compares the attributes of two Div nodes. */
5536 static int node_cmp_attr_Div(ir_node *a, ir_node *b) {
5537 const divmod_attr *ma = get_irn_divmod_attr(a);
5538 const divmod_attr *mb = get_irn_divmod_attr(b);
5539 return ma->exc.pin_state != mb->exc.pin_state ||
5540 ma->res_mode != mb->res_mode ||
5541 ma->no_remainder != mb->no_remainder;
5542 } /* node_cmp_attr_Div */
5544 /** Compares the attributes of two DivMod nodes. */
5545 static int node_cmp_attr_DivMod(ir_node *a, ir_node *b) {
5546 const divmod_attr *ma = get_irn_divmod_attr(a);
5547 const divmod_attr *mb = get_irn_divmod_attr(b);
5548 return ma->exc.pin_state != mb->exc.pin_state ||
5549 ma->res_mode != mb->res_mode;
5550 } /* node_cmp_attr_DivMod */
5552 /** Compares the attributes of two Mod nodes. */
5553 static int node_cmp_attr_Mod(ir_node *a, ir_node *b) {
5554 const divmod_attr *ma = get_irn_divmod_attr(a);
5555 const divmod_attr *mb = get_irn_divmod_attr(b);
5556 return ma->exc.pin_state != mb->exc.pin_state ||
5557 ma->res_mode != mb->res_mode;
5558 } /* node_cmp_attr_Mod */
5560 /** Compares the attributes of two Quot nodes. */
5561 static int node_cmp_attr_Quot(ir_node *a, ir_node *b) {
5562 const divmod_attr *ma = get_irn_divmod_attr(a);
5563 const divmod_attr *mb = get_irn_divmod_attr(b);
5564 return ma->exc.pin_state != mb->exc.pin_state ||
5565 ma->res_mode != mb->res_mode;
5566 } /* node_cmp_attr_Quot */
5568 /** Compares the attributes of two Confirm nodes. */
5569 static int node_cmp_attr_Confirm(ir_node *a, ir_node *b) {
5570 return (get_Confirm_cmp(a) != get_Confirm_cmp(b));
5571 } /* node_cmp_attr_Confirm */
5573 /** Compares the attributes of two ASM nodes. */
5574 static int node_cmp_attr_ASM(ir_node *a, ir_node *b) {
5576 const ir_asm_constraint *ca;
5577 const ir_asm_constraint *cb;
5580 if (get_ASM_text(a) != get_ASM_text(b))
5583 /* Should we really check the constraints here? Should be better, but is strange. */
5584 n = get_ASM_n_input_constraints(a);
5585 if (n != get_ASM_n_input_constraints(b))
5588 ca = get_ASM_input_constraints(a);
5589 cb = get_ASM_input_constraints(b);
5590 for (i = 0; i < n; ++i) {
5591 if (ca[i].pos != cb[i].pos || ca[i].constraint != cb[i].constraint)
5595 n = get_ASM_n_output_constraints(a);
5596 if (n != get_ASM_n_output_constraints(b))
5599 ca = get_ASM_output_constraints(a);
5600 cb = get_ASM_output_constraints(b);
5601 for (i = 0; i < n; ++i) {
5602 if (ca[i].pos != cb[i].pos || ca[i].constraint != cb[i].constraint)
5606 n = get_ASM_n_clobbers(a);
5607 if (n != get_ASM_n_clobbers(b))
5610 cla = get_ASM_clobbers(a);
5611 clb = get_ASM_clobbers(b);
5612 for (i = 0; i < n; ++i) {
5613 if (cla[i] != clb[i])
5617 } /* node_cmp_attr_ASM */
5620 * Set the default node attribute compare operation for an ir_op_ops.
5622 * @param code the opcode for the default operation
5623 * @param ops the operations initialized
5628 static ir_op_ops *firm_set_default_node_cmp_attr(ir_opcode code, ir_op_ops *ops)
5632 ops->node_cmp_attr = node_cmp_attr_##a; \
5663 } /* firm_set_default_node_cmp_attr */
5666 * Compare function for two nodes in the value table. Gets two
5667 * nodes as parameters. Returns 0 if the nodes are a Common Sub Expression.
5669 int identities_cmp(const void *elt, const void *key) {
5670 ir_node *a = (ir_node *)elt;
5671 ir_node *b = (ir_node *)key;
5674 if (a == b) return 0;
5676 if ((get_irn_op(a) != get_irn_op(b)) ||
5677 (get_irn_mode(a) != get_irn_mode(b))) return 1;
5679 /* compare if a's in and b's in are of equal length */
5680 irn_arity_a = get_irn_intra_arity(a);
5681 if (irn_arity_a != get_irn_intra_arity(b))
5684 if (get_irn_pinned(a) == op_pin_state_pinned) {
5685 /* for pinned nodes, the block inputs must be equal */
5686 if (get_irn_intra_n(a, -1) != get_irn_intra_n(b, -1))
5688 } else if (! get_opt_global_cse()) {
5689 /* for block-local CSE both nodes must be in the same MacroBlock */
5690 if (get_irn_MacroBlock(a) != get_irn_MacroBlock(b))
5694 /* compare a->in[0..ins] with b->in[0..ins] */
5695 for (i = 0; i < irn_arity_a; i++)
5696 if (get_irn_intra_n(a, i) != get_irn_intra_n(b, i))
5700 * here, we already now that the nodes are identical except their
5703 if (a->op->ops.node_cmp_attr)
5704 return a->op->ops.node_cmp_attr(a, b);
5707 } /* identities_cmp */
5710 * Calculate a hash value of a node.
5712 unsigned ir_node_hash(const ir_node *node) {
5716 if (node->op == op_Const) {
5717 /* special value for const, as they only differ in their tarval. */
5718 h = HASH_PTR(node->attr.con.tv);
5719 h = 9*h + HASH_PTR(get_irn_mode(node));
5720 } else if (node->op == op_SymConst) {
5721 /* special value for const, as they only differ in their symbol. */
5722 h = HASH_PTR(node->attr.symc.sym.type_p);
5723 h = 9*h + HASH_PTR(get_irn_mode(node));
5726 /* hash table value = 9*(9*(9*(9*(9*arity+in[0])+in[1])+ ...)+mode)+code */
5727 h = irn_arity = get_irn_intra_arity(node);
5729 /* consider all in nodes... except the block if not a control flow. */
5730 for (i = is_cfop(node) ? -1 : 0; i < irn_arity; i++) {
5731 h = 9*h + HASH_PTR(get_irn_intra_n(node, i));
5735 h = 9*h + HASH_PTR(get_irn_mode(node));
5737 h = 9*h + HASH_PTR(get_irn_op(node));
5741 } /* ir_node_hash */
5743 pset *new_identities(void) {
5744 return new_pset(identities_cmp, N_IR_NODES);
5745 } /* new_identities */
5747 void del_identities(pset *value_table) {
5748 del_pset(value_table);
5749 } /* del_identities */
5752 * Normalize a node by putting constants (and operands with larger
5753 * node index) on the right (operator side).
5755 * @param n The node to normalize
5757 static void normalize_node(ir_node *n) {
5758 if (is_op_commutative(get_irn_op(n))) {
5759 ir_node *l = get_binop_left(n);
5760 ir_node *r = get_binop_right(n);
5762 /* For commutative operators perform a OP b == b OP a but keep
5763 * constants on the RIGHT side. This helps greatly in some
5764 * optimizations. Moreover we use the idx number to make the form
5766 if (!operands_are_normalized(l, r)) {
5767 set_binop_left(n, r);
5768 set_binop_right(n, l);
5771 } /* normalize_node */
5774 * Update the nodes after a match in the value table. If both nodes have
5775 * the same MacroBlock but different Blocks, we must ensure that the node
5776 * with the dominating Block (the node that is near to the MacroBlock header
5777 * is stored in the table.
5778 * Because a MacroBlock has only one "non-exception" flow, we don't need
5779 * dominance info here: We known, that one block must dominate the other and
5780 * following the only block input will allow to find it.
5782 static void update_known_irn(ir_node *known_irn, const ir_node *new_ir_node) {
5783 ir_node *known_blk, *new_block, *block, *mbh;
5785 if (get_opt_global_cse()) {
5786 /* Block inputs are meaning less */
5789 known_blk = get_irn_n(known_irn, -1);
5790 new_block = get_irn_n(new_ir_node, -1);
5791 if (known_blk == new_block) {
5792 /* already in the same block */
5796 * We expect the typical case when we built the graph. In that case, the
5797 * known_irn is already the upper one, so checking this should be faster.
5800 mbh = get_Block_MacroBlock(new_block);
5802 if (block == known_blk) {
5803 /* ok, we have found it: known_block dominates new_block as expected */
5808 * We have reached the MacroBlock header NOT founding
5809 * the known_block. new_block must dominate known_block.
5812 set_irn_n(known_irn, -1, new_block);
5815 assert(get_Block_n_cfgpreds(block) == 1);
5816 block = get_Block_cfgpred_block(block, 0);
5818 } /* update_value_table */
5821 * Return the canonical node computing the same value as n.
5822 * Looks up the node in a hash table, enters it in the table
5823 * if it isn't there yet.
5825 * @param value_table the HashSet containing all nodes in the
5827 * @param n the node to look up
5829 * @return a node that computes the same value as n or n if no such
5830 * node could be found
5832 ir_node *identify_remember(pset *value_table, ir_node *n) {
5835 if (!value_table) return n;
5838 /* lookup or insert in hash table with given hash key. */
5839 o = pset_insert(value_table, n, ir_node_hash(n));
5842 update_known_irn(o, n);
5847 } /* identify_remember */
5850 * During construction we set the op_pin_state_pinned flag in the graph right when the
5851 * optimization is performed. The flag turning on procedure global cse could
5852 * be changed between two allocations. This way we are safe.
5854 * @param value_table The value table
5855 * @param n The node to lookup
5857 static INLINE ir_node *identify_cons(pset *value_table, ir_node *n) {
5860 n = identify_remember(value_table, n);
5861 if (n != old && get_irn_MacroBlock(old) != get_irn_MacroBlock(n))
5862 set_irg_pinned(current_ir_graph, op_pin_state_floats);
5864 } /* identify_cons */
5866 /* Add a node to the identities value table. */
5867 void add_identities(pset *value_table, ir_node *node) {
5868 if (get_opt_cse() && is_no_Block(node))
5869 identify_remember(value_table, node);
5870 } /* add_identities */
5872 /* Visit each node in the value table of a graph. */
5873 void visit_all_identities(ir_graph *irg, irg_walk_func visit, void *env) {
5875 ir_graph *rem = current_ir_graph;
5877 current_ir_graph = irg;
5878 foreach_pset(irg->value_table, node)
5880 current_ir_graph = rem;
5881 } /* visit_all_identities */
5884 * Garbage in, garbage out. If a node has a dead input, i.e., the
5885 * Bad node is input to the node, return the Bad node.
5887 static ir_node *gigo(ir_node *node) {
5889 ir_op *op = get_irn_op(node);
5891 /* remove garbage blocks by looking at control flow that leaves the block
5892 and replacing the control flow by Bad. */
5893 if (get_irn_mode(node) == mode_X) {
5894 ir_node *block = get_nodes_block(skip_Proj(node));
5896 /* Don't optimize nodes in immature blocks. */
5897 if (!get_Block_matured(block))
5899 /* Don't optimize End, may have Bads. */
5900 if (op == op_End) return node;
5902 if (is_Block(block)) {
5903 if (is_Block_dead(block)) {
5904 /* control flow from dead block is dead */
5908 for (i = get_irn_arity(block) - 1; i >= 0; --i) {
5909 if (!is_Bad(get_irn_n(block, i)))
5913 ir_graph *irg = get_irn_irg(block);
5914 /* the start block is never dead */
5915 if (block != get_irg_start_block(irg)
5916 && block != get_irg_end_block(irg)) {
5918 * Do NOT kill control flow without setting
5919 * the block to dead of bad things can happen:
5920 * We get a Block that is not reachable be irg_block_walk()
5921 * but can be found by irg_walk()!
5923 set_Block_dead(block);
5930 /* Blocks, Phis and Tuples may have dead inputs, e.g., if one of the
5931 blocks predecessors is dead. */
5932 if (op != op_Block && op != op_Phi && op != op_Tuple) {
5933 irn_arity = get_irn_arity(node);
5936 * Beware: we can only read the block of a non-floating node.
5938 if (is_irn_pinned_in_irg(node) &&
5939 is_Block_dead(get_nodes_block(skip_Proj(node))))
5942 for (i = 0; i < irn_arity; i++) {
5943 ir_node *pred = get_irn_n(node, i);
5948 /* Propagating Unknowns here seems to be a bad idea, because
5949 sometimes we need a node as a input and did not want that
5951 However, it might be useful to move this into a later phase
5952 (if you think that optimizing such code is useful). */
5953 if (is_Unknown(pred) && mode_is_data(get_irn_mode(node)))
5954 return new_Unknown(get_irn_mode(node));
5959 /* With this code we violate the agreement that local_optimize
5960 only leaves Bads in Block, Phi and Tuple nodes. */
5961 /* If Block has only Bads as predecessors it's garbage. */
5962 /* If Phi has only Bads as predecessors it's garbage. */
5963 if ((op == op_Block && get_Block_matured(node)) || op == op_Phi) {
5964 irn_arity = get_irn_arity(node);
5965 for (i = 0; i < irn_arity; i++) {
5966 if (!is_Bad(get_irn_n(node, i))) break;
5968 if (i == irn_arity) node = new_Bad();
5975 * These optimizations deallocate nodes from the obstack.
5976 * It can only be called if it is guaranteed that no other nodes
5977 * reference this one, i.e., right after construction of a node.
5979 * @param n The node to optimize
5981 * current_ir_graph must be set to the graph of the node!
5983 ir_node *optimize_node(ir_node *n) {
5986 ir_opcode iro = get_irn_opcode(n);
5988 /* Always optimize Phi nodes: part of the construction. */
5989 if ((!get_opt_optimize()) && (iro != iro_Phi)) return n;
5991 /* constant expression evaluation / constant folding */
5992 if (get_opt_constant_folding()) {
5993 /* neither constants nor Tuple values can be evaluated */
5994 if (iro != iro_Const && (get_irn_mode(n) != mode_T)) {
5995 unsigned fp_model = get_irg_fp_model(current_ir_graph);
5996 int old_fp_mode = tarval_enable_fp_ops((fp_model & fp_strict_algebraic) == 0);
5997 /* try to evaluate */
5998 tv = computed_value(n);
5999 if (tv != tarval_bad) {
6001 ir_type *old_tp = get_irn_type(n);
6002 int i, arity = get_irn_arity(n);
6006 * Try to recover the type of the new expression.
6008 for (i = 0; i < arity && !old_tp; ++i)
6009 old_tp = get_irn_type(get_irn_n(n, i));
6012 * we MUST copy the node here temporary, because it's still needed
6013 * for DBG_OPT_CSTEVAL
6015 node_size = offsetof(ir_node, attr) + n->op->attr_size;
6016 oldn = alloca(node_size);
6018 memcpy(oldn, n, node_size);
6019 CLONE_ARR_A(ir_node *, oldn->in, n->in);
6021 /* ARG, copy the in array, we need it for statistics */
6022 memcpy(oldn->in, n->in, ARR_LEN(n->in) * sizeof(n->in[0]));
6024 /* note the inplace edges module */
6025 edges_node_deleted(n, current_ir_graph);
6027 /* evaluation was successful -- replace the node. */
6028 irg_kill_node(current_ir_graph, n);
6029 nw = new_Const(get_tarval_mode(tv), tv);
6031 if (old_tp && get_type_mode(old_tp) == get_tarval_mode(tv))
6032 set_Const_type(nw, old_tp);
6033 DBG_OPT_CSTEVAL(oldn, nw);
6034 tarval_enable_fp_ops(old_fp_mode);
6037 tarval_enable_fp_ops(old_fp_mode);
6041 /* remove unnecessary nodes */
6042 if (get_opt_algebraic_simplification() ||
6043 (iro == iro_Phi) || /* always optimize these nodes. */
6045 (iro == iro_Proj) ||
6046 (iro == iro_Block) ) /* Flags tested local. */
6047 n = equivalent_node(n);
6049 /* Common Subexpression Elimination.
6051 * Checks whether n is already available.
6052 * The block input is used to distinguish different subexpressions. Right
6053 * now all nodes are op_pin_state_pinned to blocks, i.e., the CSE only finds common
6054 * subexpressions within a block.
6057 n = identify_cons(current_ir_graph->value_table, n);
6060 edges_node_deleted(oldn, current_ir_graph);
6062 /* We found an existing, better node, so we can deallocate the old node. */
6063 irg_kill_node(current_ir_graph, oldn);
6067 /* Some more constant expression evaluation that does not allow to
6069 iro = get_irn_opcode(n);
6070 if (get_opt_algebraic_simplification() ||
6071 (iro == iro_Cond) ||
6072 (iro == iro_Proj)) /* Flags tested local. */
6073 n = transform_node(n);
6075 /* Remove nodes with dead (Bad) input.
6076 Run always for transformation induced Bads. */
6079 /* Now we have a legal, useful node. Enter it in hash table for CSE */
6080 if (get_opt_cse() && (get_irn_opcode(n) != iro_Block)) {
6081 n = identify_remember(current_ir_graph->value_table, n);
6085 } /* optimize_node */
6089 * These optimizations never deallocate nodes (in place). This can cause dead
6090 * nodes lying on the obstack. Remove these by a dead node elimination,
6091 * i.e., a copying garbage collection.
6093 ir_node *optimize_in_place_2(ir_node *n) {
6096 ir_opcode iro = get_irn_opcode(n);
6098 if (!get_opt_optimize() && !is_Phi(n)) return n;
6100 /* constant expression evaluation / constant folding */
6101 if (get_opt_constant_folding()) {
6102 /* neither constants nor Tuple values can be evaluated */
6103 if (iro != iro_Const && get_irn_mode(n) != mode_T) {
6104 unsigned fp_model = get_irg_fp_model(current_ir_graph);
6105 int old_fp_mode = tarval_enable_fp_ops((fp_model & fp_strict_algebraic) == 0);
6106 /* try to evaluate */
6107 tv = computed_value(n);
6108 if (tv != tarval_bad) {
6109 /* evaluation was successful -- replace the node. */
6110 ir_type *old_tp = get_irn_type(n);
6111 int i, arity = get_irn_arity(n);
6114 * Try to recover the type of the new expression.
6116 for (i = 0; i < arity && !old_tp; ++i)
6117 old_tp = get_irn_type(get_irn_n(n, i));
6119 n = new_Const(get_tarval_mode(tv), tv);
6121 if (old_tp && get_type_mode(old_tp) == get_tarval_mode(tv))
6122 set_Const_type(n, old_tp);
6124 DBG_OPT_CSTEVAL(oldn, n);
6125 tarval_enable_fp_ops(old_fp_mode);
6128 tarval_enable_fp_ops(old_fp_mode);
6132 /* remove unnecessary nodes */
6133 if (get_opt_constant_folding() ||
6134 (iro == iro_Phi) || /* always optimize these nodes. */
6135 (iro == iro_Id) || /* ... */
6136 (iro == iro_Proj) || /* ... */
6137 (iro == iro_Block) ) /* Flags tested local. */
6138 n = equivalent_node(n);
6140 /** common subexpression elimination **/
6141 /* Checks whether n is already available. */
6142 /* The block input is used to distinguish different subexpressions. Right
6143 now all nodes are op_pin_state_pinned to blocks, i.e., the cse only finds common
6144 subexpressions within a block. */
6145 if (get_opt_cse()) {
6146 n = identify_remember(current_ir_graph->value_table, n);
6149 /* Some more constant expression evaluation. */
6150 iro = get_irn_opcode(n);
6151 if (get_opt_constant_folding() ||
6152 (iro == iro_Cond) ||
6153 (iro == iro_Proj)) /* Flags tested local. */
6154 n = transform_node(n);
6156 /* Remove nodes with dead (Bad) input.
6157 Run always for transformation induced Bads. */
6160 /* Now we can verify the node, as it has no dead inputs any more. */
6163 /* Now we have a legal, useful node. Enter it in hash table for cse.
6164 Blocks should be unique anyways. (Except the successor of start:
6165 is cse with the start block!) */
6166 if (get_opt_cse() && (get_irn_opcode(n) != iro_Block))
6167 n = identify_remember(current_ir_graph->value_table, n);
6170 } /* optimize_in_place_2 */
6173 * Wrapper for external use, set proper status bits after optimization.
6175 ir_node *optimize_in_place(ir_node *n) {
6176 /* Handle graph state */
6177 assert(get_irg_phase_state(current_ir_graph) != phase_building);
6179 if (get_opt_global_cse())
6180 set_irg_pinned(current_ir_graph, op_pin_state_floats);
6181 if (get_irg_outs_state(current_ir_graph) == outs_consistent)
6182 set_irg_outs_inconsistent(current_ir_graph);
6184 /* FIXME: Maybe we could also test whether optimizing the node can
6185 change the control graph. */
6186 set_irg_doms_inconsistent(current_ir_graph);
6187 return optimize_in_place_2(n);
6188 } /* optimize_in_place */
6191 * Sets the default operation for an ir_ops.
6193 ir_op_ops *firm_set_default_operations(ir_opcode code, ir_op_ops *ops) {
6194 ops = firm_set_default_computed_value(code, ops);
6195 ops = firm_set_default_equivalent_node(code, ops);
6196 ops = firm_set_default_transform_node(code, ops);
6197 ops = firm_set_default_node_cmp_attr(code, ops);
6198 ops = firm_set_default_get_type(code, ops);
6199 ops = firm_set_default_get_type_attr(code, ops);
6200 ops = firm_set_default_get_entity_attr(code, ops);
6203 } /* firm_set_default_operations */