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))
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_mode *mode = get_irn_mode(n);
134 ir_node *a = get_Sub_left(n);
135 ir_node *b = get_Sub_right(n);
140 if (a == b && !is_Bad(a))
141 return get_mode_null(mode);
146 if ((ta != tarval_bad) && (tb != tarval_bad))
147 return tarval_sub(ta, tb, mode);
150 } /* computed_value_Sub */
153 * Return the value of a Carry.
154 * Special : a op 0, 0 op b
156 static tarval *computed_value_Carry(ir_node *n) {
157 ir_node *a = get_binop_left(n);
158 ir_node *b = get_binop_right(n);
159 ir_mode *m = get_irn_mode(n);
161 tarval *ta = value_of(a);
162 tarval *tb = value_of(b);
164 if ((ta != tarval_bad) && (tb != tarval_bad)) {
166 return tarval_carry() ? get_mode_one(m) : get_mode_null(m);
168 if (tarval_is_null(ta) || tarval_is_null(tb))
169 return get_mode_null(m);
172 } /* computed_value_Carry */
175 * Return the value of a Borrow.
178 static tarval *computed_value_Borrow(ir_node *n) {
179 ir_node *a = get_binop_left(n);
180 ir_node *b = get_binop_right(n);
181 ir_mode *m = get_irn_mode(n);
183 tarval *ta = value_of(a);
184 tarval *tb = value_of(b);
186 if ((ta != tarval_bad) && (tb != tarval_bad)) {
187 return tarval_cmp(ta, tb) == pn_Cmp_Lt ? get_mode_one(m) : get_mode_null(m);
188 } else if (tarval_is_null(ta)) {
189 return get_mode_null(m);
192 } /* computed_value_Borrow */
195 * Return the value of an unary Minus.
197 static tarval *computed_value_Minus(ir_node *n) {
198 ir_node *a = get_Minus_op(n);
199 tarval *ta = value_of(a);
201 if (ta != tarval_bad)
202 return tarval_neg(ta);
205 } /* computed_value_Minus */
208 * Return the value of a Mul.
210 static tarval *computed_value_Mul(ir_node *n) {
211 ir_node *a = get_Mul_left(n);
212 ir_node *b = get_Mul_right(n);
215 tarval *ta = value_of(a);
216 tarval *tb = value_of(b);
218 mode = get_irn_mode(n);
219 if (mode != get_irn_mode(a)) {
220 /* n * n = 2n bit multiplication */
221 ta = tarval_convert_to(ta, mode);
222 tb = tarval_convert_to(tb, mode);
225 if (ta != tarval_bad && tb != tarval_bad) {
226 return tarval_mul(ta, tb);
228 /* a*0 = 0 or 0*b = 0 */
229 if (ta == get_mode_null(mode))
231 if (tb == get_mode_null(mode))
235 } /* computed_value_Mul */
238 * Return the value of a floating point Quot.
240 static tarval *computed_value_Quot(ir_node *n) {
241 ir_node *a = get_Quot_left(n);
242 ir_node *b = get_Quot_right(n);
244 tarval *ta = value_of(a);
245 tarval *tb = value_of(b);
247 if ((ta != tarval_bad) && (tb != tarval_bad)) {
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);
1297 if (mode_is_int(n_mode) && mode_is_float(a_mode)) {
1298 /* ConvI(ConvF(I)) -> I, iff float mantissa >= int mode */
1299 size_t int_mantissa = get_mode_size_bits(n_mode) - (mode_is_signed(n_mode) ? 1 : 0);
1300 size_t float_mantissa;
1301 /* FIXME There is no way to get the mantissa size of a mode */
1302 switch (get_mode_size_bits(a_mode)) {
1303 case 32: float_mantissa = 23 + 1; break; // + 1 for implicit 1
1304 case 64: float_mantissa = 52 + 1; break;
1305 case 80: float_mantissa = 64 + 1; break;
1306 default: float_mantissa = 0; break;
1308 if (float_mantissa != 0 && float_mantissa >= int_mantissa) {
1310 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
1315 if (smaller_mode(b_mode, a_mode)) {
1316 if (get_Conv_strict(n))
1317 set_Conv_strict(b, 1);
1318 n = b; /* ConvA(ConvB(ConvA(...))) == ConvA(...) */
1319 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
1325 } /* equivalent_node_Conv */
1328 * A Cast may be removed if the type of the previous node
1329 * is already the type of the Cast.
1331 static ir_node *equivalent_node_Cast(ir_node *n) {
1333 ir_node *pred = get_Cast_op(n);
1335 if (get_irn_type(pred) == get_Cast_type(n)) {
1337 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CAST);
1340 } /* equivalent_node_Cast */
1343 * Several optimizations:
1344 * - no Phi in start block.
1345 * - remove Id operators that are inputs to Phi
1346 * - fold Phi-nodes, iff they have only one predecessor except
1349 static ir_node *equivalent_node_Phi(ir_node *n) {
1354 ir_node *first_val = NULL; /* to shutup gcc */
1356 if (!get_opt_normalize()) return n;
1358 n_preds = get_Phi_n_preds(n);
1360 block = get_nodes_block(n);
1361 if ((is_Block_dead(block)) || /* Control dead */
1362 (block == get_irg_start_block(current_ir_graph))) /* There should be no Phi nodes */
1363 return new_Bad(); /* in the Start Block. */
1365 if (n_preds == 0) return n; /* Phi of dead Region without predecessors. */
1367 /* If the Block has a Bad pred, we also have one. */
1368 for (i = 0; i < n_preds; ++i)
1369 if (is_Bad(get_Block_cfgpred(block, i)))
1370 set_Phi_pred(n, i, new_Bad());
1372 /* Find first non-self-referencing input */
1373 for (i = 0; i < n_preds; ++i) {
1374 first_val = get_Phi_pred(n, i);
1375 if ( (first_val != n) /* not self pointer */
1377 /* BEWARE: when the if is changed to 1, Phi's will ignore it's Bad
1378 * predecessors. Then, Phi nodes in dead code might be removed, causing
1379 * nodes pointing to themself (Add's for instance).
1380 * This is really bad and causes endless recursions in several
1381 * code pathes, so we do NOT optimize such a code.
1382 * This is not that bad as it sounds, optimize_cf() removes bad control flow
1383 * (and bad Phi predecessors), so live code is optimized later.
1385 && (! is_Bad(first_val))
1387 ) { /* value not dead */
1388 break; /* then found first value. */
1393 /* A totally Bad or self-referencing Phi (we didn't break the above loop) */
1397 /* search for rest of inputs, determine if any of these
1398 are non-self-referencing */
1399 while (++i < n_preds) {
1400 ir_node *scnd_val = get_Phi_pred(n, i);
1401 if ( (scnd_val != n)
1402 && (scnd_val != first_val)
1405 && (! is_Bad(scnd_val))
1413 /* Fold, if no multiple distinct non-self-referencing inputs */
1415 DBG_OPT_PHI(oldn, n);
1418 } /* equivalent_node_Phi */
1421 * Several optimizations:
1422 * - no Sync in start block.
1423 * - fold Sync-nodes, iff they have only one predecessor except
1426 static ir_node *equivalent_node_Sync(ir_node *n) {
1427 int arity = get_Sync_n_preds(n);
1430 for (i = 0; i < arity;) {
1431 ir_node *pred = get_Sync_pred(n, i);
1434 /* Remove Bad predecessors */
1441 /* Remove duplicate predecessors */
1447 if (get_Sync_pred(n, j) == pred) {
1455 if (arity == 0) return new_Bad();
1456 if (arity == 1) return get_Sync_pred(n, 0);
1458 } /* equivalent_node_Sync */
1461 * Optimize Proj(Tuple) and gigo() for ProjX in Bad block,
1462 * ProjX(Load) and ProjX(Store).
1464 static ir_node *equivalent_node_Proj(ir_node *proj) {
1465 ir_node *oldn = proj;
1466 ir_node *a = get_Proj_pred(proj);
1469 /* Remove the Tuple/Proj combination. */
1470 if ( get_Proj_proj(proj) <= get_Tuple_n_preds(a) ) {
1471 proj = get_Tuple_pred(a, get_Proj_proj(proj));
1472 DBG_OPT_TUPLE(oldn, a, proj);
1474 /* This should not happen! */
1475 assert(! "found a Proj with higher number than Tuple predecessors");
1478 } else if (get_irn_mode(proj) == mode_X) {
1479 if (is_Block_dead(get_nodes_block(skip_Proj(proj)))) {
1480 /* Remove dead control flow -- early gigo(). */
1482 } else if (get_opt_ldst_only_null_ptr_exceptions()) {
1484 /* get the Load address */
1485 ir_node *addr = get_Load_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_Load_X_except) {
1495 DBG_OPT_EXC_REM(proj);
1498 return new_r_Jmp(current_ir_graph, blk);
1500 } else if (is_Store(a)) {
1501 /* get the load/store address */
1502 ir_node *addr = get_Store_ptr(a);
1503 ir_node *blk = get_irn_n(a, -1);
1506 if (value_not_null(addr, &confirm)) {
1507 if (confirm == NULL) {
1508 /* this node may float if it did not depend on a Confirm */
1509 set_irn_pinned(a, op_pin_state_floats);
1511 if (get_Proj_proj(proj) == pn_Store_X_except) {
1512 DBG_OPT_EXC_REM(proj);
1515 return new_r_Jmp(current_ir_graph, blk);
1522 } /* equivalent_node_Proj */
1527 static ir_node *equivalent_node_Id(ir_node *n) {
1532 } while (get_irn_op(n) == op_Id);
1534 DBG_OPT_ID(oldn, n);
1536 } /* equivalent_node_Id */
1541 static ir_node *equivalent_node_Mux(ir_node *n)
1543 ir_node *oldn = n, *sel = get_Mux_sel(n);
1544 tarval *ts = value_of(sel);
1546 /* Mux(true, f, t) == t */
1547 if (ts == tarval_b_true) {
1548 n = get_Mux_true(n);
1549 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_C);
1551 /* Mux(false, f, t) == f */
1552 else if (ts == tarval_b_false) {
1553 n = get_Mux_false(n);
1554 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_C);
1556 /* Mux(v, x, x) == x */
1557 else if (get_Mux_false(n) == get_Mux_true(n)) {
1558 n = get_Mux_true(n);
1559 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_EQ);
1561 else if (is_Proj(sel) && !mode_honor_signed_zeros(get_irn_mode(n))) {
1562 ir_node *cmp = get_Proj_pred(sel);
1563 long proj_nr = get_Proj_proj(sel);
1564 ir_node *f = get_Mux_false(n);
1565 ir_node *t = get_Mux_true(n);
1568 * Note further that these optimization work even for floating point
1569 * with NaN's because -NaN == NaN.
1570 * However, if +0 and -0 is handled differently, we cannot use the first one.
1573 ir_node *const cmp_l = get_Cmp_left(cmp);
1574 ir_node *const cmp_r = get_Cmp_right(cmp);
1578 if ((cmp_l == t && cmp_r == f) || /* Psi(t == f, t, f) -> f */
1579 (cmp_l == f && cmp_r == t)) { /* Psi(f == t, t, f) -> f */
1581 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1588 if ((cmp_l == t && cmp_r == f) || /* Psi(t != f, t, f) -> t */
1589 (cmp_l == f && cmp_r == t)) { /* Psi(f != t, t, f) -> t */
1591 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1598 * Note: normalization puts the constant on the right side,
1599 * so we check only one case.
1601 if (cmp_l == t && is_Const(cmp_r) && is_Const_null(cmp_r)) {
1602 /* Mux(t CMP 0, X, t) */
1603 if (is_Minus(f) && get_Minus_op(f) == t) {
1604 /* Mux(t CMP 0, -t, t) */
1605 if (proj_nr == pn_Cmp_Eq) {
1606 /* Mux(t == 0, -t, t) ==> -t */
1608 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1609 } else if (proj_nr == pn_Cmp_Lg || proj_nr == pn_Cmp_Ne) {
1610 /* Mux(t != 0, -t, t) ==> t */
1612 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1619 } /* equivalent_node_Mux */
1622 * Returns a equivalent node of a Psi: if a condition is true
1623 * and all previous conditions are false we know its value.
1624 * If all conditions are false its value is the default one.
1626 static ir_node *equivalent_node_Psi(ir_node *n) {
1628 return equivalent_node_Mux(n);
1630 } /* equivalent_node_Psi */
1633 * Optimize -a CMP -b into b CMP a.
1634 * This works only for for modes where unary Minus
1636 * Note that two-complement integers can Overflow
1637 * so it will NOT work.
1639 * For == and != can be handled in Proj(Cmp)
1641 static ir_node *equivalent_node_Cmp(ir_node *n) {
1642 ir_node *left = get_Cmp_left(n);
1643 ir_node *right = get_Cmp_right(n);
1645 if (is_Minus(left) && is_Minus(right) &&
1646 !mode_overflow_on_unary_Minus(get_irn_mode(left))) {
1647 left = get_Minus_op(left);
1648 right = get_Minus_op(right);
1649 set_Cmp_left(n, right);
1650 set_Cmp_right(n, left);
1653 } /* equivalent_node_Cmp */
1656 * Remove Confirm nodes if setting is on.
1657 * Replace Confirms(x, '=', Constlike) by Constlike.
1659 static ir_node *equivalent_node_Confirm(ir_node *n) {
1660 ir_node *pred = get_Confirm_value(n);
1661 pn_Cmp pnc = get_Confirm_cmp(n);
1663 if (is_Confirm(pred) && pnc == get_Confirm_cmp(pred)) {
1665 * rare case: two identical Confirms one after another,
1666 * replace the second one with the first.
1670 if (get_opt_remove_confirm())
1671 return get_Confirm_value(n);
1676 * Optimize CopyB(mem, x, x) into a Nop.
1678 static ir_node *equivalent_node_CopyB(ir_node *n) {
1679 ir_node *a = get_CopyB_dst(n);
1680 ir_node *b = get_CopyB_src(n);
1683 /* Turn CopyB into a tuple (mem, jmp, bad, bad) */
1684 ir_node *mem = get_CopyB_mem(n);
1685 ir_node *blk = get_nodes_block(n);
1686 turn_into_tuple(n, pn_CopyB_max);
1687 set_Tuple_pred(n, pn_CopyB_M, mem);
1688 set_Tuple_pred(n, pn_CopyB_X_regular, new_r_Jmp(current_ir_graph, blk));
1689 set_Tuple_pred(n, pn_CopyB_X_except, new_Bad()); /* no exception */
1690 set_Tuple_pred(n, pn_CopyB_M_except, new_Bad());
1693 } /* equivalent_node_CopyB */
1696 * Optimize Bounds(idx, idx, upper) into idx.
1698 static ir_node *equivalent_node_Bound(ir_node *n) {
1699 ir_node *idx = get_Bound_index(n);
1700 ir_node *pred = skip_Proj(idx);
1703 if (is_Bound(pred)) {
1705 * idx was Bounds checked in the same MacroBlock previously,
1706 * it is still valid if lower <= pred_lower && pred_upper <= upper.
1708 ir_node *lower = get_Bound_lower(n);
1709 ir_node *upper = get_Bound_upper(n);
1710 if (get_Bound_lower(pred) == lower &&
1711 get_Bound_upper(pred) == upper &&
1712 get_irn_MacroBlock(n) == get_irn_MacroBlock(pred)) {
1714 * One could expect that we simply return the previous
1715 * Bound here. However, this would be wrong, as we could
1716 * add an exception Proj to a new location then.
1717 * So, we must turn in into a tuple.
1723 /* Turn Bound into a tuple (mem, jmp, bad, idx) */
1724 ir_node *mem = get_Bound_mem(n);
1725 ir_node *blk = get_nodes_block(n);
1726 turn_into_tuple(n, pn_Bound_max);
1727 set_Tuple_pred(n, pn_Bound_M, mem);
1728 set_Tuple_pred(n, pn_Bound_X_regular, new_r_Jmp(current_ir_graph, blk)); /* no exception */
1729 set_Tuple_pred(n, pn_Bound_X_except, new_Bad()); /* no exception */
1730 set_Tuple_pred(n, pn_Bound_res, idx);
1733 } /* equivalent_node_Bound */
1736 * equivalent_node() returns a node equivalent to input n. It skips all nodes that
1737 * perform no actual computation, as, e.g., the Id nodes. It does not create
1738 * new nodes. It is therefore safe to free n if the node returned is not n.
1739 * If a node returns a Tuple we can not just skip it. If the size of the
1740 * in array fits, we transform n into a tuple (e.g., Div).
1742 ir_node *equivalent_node(ir_node *n) {
1743 if (n->op->ops.equivalent_node)
1744 return n->op->ops.equivalent_node(n);
1746 } /* equivalent_node */
1749 * Sets the default equivalent node operation for an ir_op_ops.
1751 * @param code the opcode for the default operation
1752 * @param ops the operations initialized
1757 static ir_op_ops *firm_set_default_equivalent_node(ir_opcode code, ir_op_ops *ops)
1761 ops->equivalent_node = equivalent_node_##a; \
1801 } /* firm_set_default_equivalent_node */
1804 * Returns non-zero if a node is a Phi node
1805 * with all predecessors constant.
1807 static int is_const_Phi(ir_node *n) {
1810 if (! is_Phi(n) || get_irn_arity(n) == 0)
1812 for (i = get_irn_arity(n) - 1; i >= 0; --i)
1813 if (! is_Const(get_irn_n(n, i)))
1816 } /* is_const_Phi */
1818 typedef tarval *(*tarval_sub_type)(tarval *a, tarval *b, ir_mode *mode);
1819 typedef tarval *(*tarval_binop_type)(tarval *a, tarval *b);
1822 * Wrapper for the tarval binop evaluation, tarval_sub has one more parameter.
1824 static tarval *do_eval(tarval *(*eval)(), tarval *a, tarval *b, ir_mode *mode) {
1825 if (eval == tarval_sub) {
1826 tarval_sub_type func = (tarval_sub_type)eval;
1828 return func(a, b, mode);
1830 tarval_binop_type func = (tarval_binop_type)eval;
1837 * Apply an evaluator on a binop with a constant operators (and one Phi).
1839 * @param phi the Phi node
1840 * @param other the other operand
1841 * @param eval an evaluator function
1842 * @param mode the mode of the result, may be different from the mode of the Phi!
1843 * @param left if non-zero, other is the left operand, else the right
1845 * @return a new Phi node if the conversion was successful, NULL else
1847 static ir_node *apply_binop_on_phi(ir_node *phi, tarval *other, tarval *(*eval)(), ir_mode *mode, int left) {
1852 int i, n = get_irn_arity(phi);
1854 NEW_ARR_A(void *, res, n);
1856 for (i = 0; i < n; ++i) {
1857 pred = get_irn_n(phi, i);
1858 tv = get_Const_tarval(pred);
1859 tv = do_eval(eval, other, tv, mode);
1861 if (tv == tarval_bad) {
1862 /* folding failed, bad */
1868 for (i = 0; i < n; ++i) {
1869 pred = get_irn_n(phi, i);
1870 tv = get_Const_tarval(pred);
1871 tv = do_eval(eval, tv, other, mode);
1873 if (tv == tarval_bad) {
1874 /* folding failed, bad */
1880 irg = current_ir_graph;
1881 for (i = 0; i < n; ++i) {
1882 pred = get_irn_n(phi, i);
1883 res[i] = new_r_Const_type(irg, get_irg_start_block(irg),
1884 mode, res[i], get_Const_type(pred));
1886 return new_r_Phi(irg, get_nodes_block(phi), n, (ir_node **)res, mode);
1887 } /* apply_binop_on_phi */
1890 * Apply an evaluator on a binop with two constant Phi.
1892 * @param a the left Phi node
1893 * @param b the right Phi node
1894 * @param eval an evaluator function
1895 * @param mode the mode of the result, may be different from the mode of the Phi!
1897 * @return a new Phi node if the conversion was successful, NULL else
1899 static ir_node *apply_binop_on_2_phis(ir_node *a, ir_node *b, tarval *(*eval)(), ir_mode *mode) {
1900 tarval *tv_l, *tv_r, *tv;
1906 if (get_nodes_block(a) != get_nodes_block(b))
1909 n = get_irn_arity(a);
1910 NEW_ARR_A(void *, res, n);
1912 for (i = 0; i < n; ++i) {
1913 pred = get_irn_n(a, i);
1914 tv_l = get_Const_tarval(pred);
1915 pred = get_irn_n(b, i);
1916 tv_r = get_Const_tarval(pred);
1917 tv = do_eval(eval, tv_l, tv_r, mode);
1919 if (tv == tarval_bad) {
1920 /* folding failed, bad */
1925 irg = current_ir_graph;
1926 for (i = 0; i < n; ++i) {
1927 pred = get_irn_n(a, i);
1928 res[i] = new_r_Const_type(irg, get_irg_start_block(irg), mode, res[i], get_Const_type(pred));
1930 return new_r_Phi(irg, get_nodes_block(a), n, (ir_node **)res, mode);
1931 } /* apply_binop_on_2_phis */
1934 * Apply an evaluator on a unop with a constant operator (a Phi).
1936 * @param phi the Phi node
1937 * @param eval an evaluator function
1939 * @return a new Phi node if the conversion was successful, NULL else
1941 static ir_node *apply_unop_on_phi(ir_node *phi, tarval *(*eval)(tarval *)) {
1947 int i, n = get_irn_arity(phi);
1949 NEW_ARR_A(void *, res, n);
1950 for (i = 0; i < n; ++i) {
1951 pred = get_irn_n(phi, i);
1952 tv = get_Const_tarval(pred);
1955 if (tv == tarval_bad) {
1956 /* folding failed, bad */
1961 mode = get_irn_mode(phi);
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_unop_on_phi */
1972 * Apply a conversion on a constant operator (a Phi).
1974 * @param phi the Phi node
1976 * @return a new Phi node if the conversion was successful, NULL else
1978 static ir_node *apply_conv_on_phi(ir_node *phi, ir_mode *mode) {
1983 int i, n = get_irn_arity(phi);
1985 NEW_ARR_A(void *, res, n);
1986 for (i = 0; i < n; ++i) {
1987 pred = get_irn_n(phi, i);
1988 tv = get_Const_tarval(pred);
1989 tv = tarval_convert_to(tv, mode);
1991 if (tv == tarval_bad) {
1992 /* folding failed, bad */
1997 irg = current_ir_graph;
1998 for (i = 0; i < n; ++i) {
1999 pred = get_irn_n(phi, i);
2000 res[i] = new_r_Const_type(irg, get_irg_start_block(irg),
2001 mode, res[i], get_Const_type(pred));
2003 return new_r_Phi(irg, get_nodes_block(phi), n, (ir_node **)res, mode);
2004 } /* apply_conv_on_phi */
2007 * Transform AddP(P, ConvIs(Iu)), AddP(P, ConvIu(Is)) and
2008 * SubP(P, ConvIs(Iu)), SubP(P, ConvIu(Is)).
2009 * If possible, remove the Conv's.
2011 static ir_node *transform_node_AddSub(ir_node *n) {
2012 ir_mode *mode = get_irn_mode(n);
2014 if (mode_is_reference(mode)) {
2015 ir_node *left = get_binop_left(n);
2016 ir_node *right = get_binop_right(n);
2017 unsigned ref_bits = get_mode_size_bits(mode);
2019 if (is_Conv(left)) {
2020 ir_mode *lmode = get_irn_mode(left);
2021 unsigned bits = get_mode_size_bits(lmode);
2023 if (ref_bits == bits &&
2024 mode_is_int(lmode) &&
2025 get_mode_arithmetic(lmode) == irma_twos_complement) {
2026 ir_node *pre = get_Conv_op(left);
2027 ir_mode *pre_mode = get_irn_mode(pre);
2029 if (mode_is_int(pre_mode) &&
2030 get_mode_size_bits(pre_mode) == bits &&
2031 get_mode_arithmetic(pre_mode) == irma_twos_complement) {
2032 /* ok, this conv just changes to sign, moreover the calculation
2033 * is done with same number of bits as our address mode, so
2034 * we can ignore the conv as address calculation can be viewed
2035 * as either signed or unsigned
2037 set_binop_left(n, pre);
2042 if (is_Conv(right)) {
2043 ir_mode *rmode = get_irn_mode(right);
2044 unsigned bits = get_mode_size_bits(rmode);
2046 if (ref_bits == bits &&
2047 mode_is_int(rmode) &&
2048 get_mode_arithmetic(rmode) == irma_twos_complement) {
2049 ir_node *pre = get_Conv_op(right);
2050 ir_mode *pre_mode = get_irn_mode(pre);
2052 if (mode_is_int(pre_mode) &&
2053 get_mode_size_bits(pre_mode) == bits &&
2054 get_mode_arithmetic(pre_mode) == irma_twos_complement) {
2055 /* ok, this conv just changes to sign, moreover the calculation
2056 * is done with same number of bits as our address mode, so
2057 * we can ignore the conv as address calculation can be viewed
2058 * as either signed or unsigned
2060 set_binop_right(n, pre);
2065 /* let address arithmetic use unsigned modes */
2066 if (is_Const(right)) {
2067 ir_mode *rmode = get_irn_mode(right);
2069 if (mode_is_signed(rmode) && get_mode_arithmetic(rmode) == irma_twos_complement) {
2070 /* convert a AddP(P, *s) into AddP(P, *u) */
2071 ir_mode *nm = get_reference_mode_unsigned_eq(mode);
2073 ir_node *pre = new_r_Conv(current_ir_graph, get_nodes_block(n), right, nm);
2074 set_binop_right(n, pre);
2080 } /* transform_node_AddSub */
2082 #define HANDLE_BINOP_PHI(eval, a, b, c, mode) \
2084 if (is_Const(b) && is_const_Phi(a)) { \
2085 /* check for Op(Phi, Const) */ \
2086 c = apply_binop_on_phi(a, get_Const_tarval(b), eval, mode, 0);\
2088 else if (is_Const(a) && is_const_Phi(b)) { \
2089 /* check for Op(Const, Phi) */ \
2090 c = apply_binop_on_phi(b, get_Const_tarval(a), eval, mode, 1);\
2092 else if (is_const_Phi(a) && is_const_Phi(b)) { \
2093 /* check for Op(Phi, Phi) */ \
2094 c = apply_binop_on_2_phis(a, b, eval, mode); \
2097 DBG_OPT_ALGSIM0(oldn, c, FS_OPT_CONST_PHI); \
2101 #define HANDLE_UNOP_PHI(eval, a, c) \
2103 if (is_const_Phi(a)) { \
2104 /* check for Op(Phi) */ \
2105 c = apply_unop_on_phi(a, eval); \
2107 DBG_OPT_ALGSIM0(oldn, c, FS_OPT_CONST_PHI); \
2113 * Do the AddSub optimization, then Transform
2114 * Constant folding on Phi
2115 * Add(a,a) -> Mul(a, 2)
2116 * Add(Mul(a, x), a) -> Mul(a, x+1)
2117 * if the mode is integer or float.
2118 * Transform Add(a,-b) into Sub(a,b).
2119 * Reassociation might fold this further.
2121 static ir_node *transform_node_Add(ir_node *n) {
2123 ir_node *a, *b, *c, *oldn = n;
2125 n = transform_node_AddSub(n);
2127 a = get_Add_left(n);
2128 b = get_Add_right(n);
2130 mode = get_irn_mode(n);
2132 if (mode_is_reference(mode)) {
2133 ir_mode *lmode = get_irn_mode(a);
2135 if (is_Const(b) && is_Const_null(b) && mode_is_int(lmode)) {
2136 /* an Add(a, NULL) is a hidden Conv */
2137 dbg_info *dbg = get_irn_dbg_info(n);
2138 return new_rd_Conv(dbg, current_ir_graph, get_nodes_block(n), a, mode);
2142 HANDLE_BINOP_PHI(tarval_add, a, b, c, mode);
2144 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
2145 if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
2148 if (mode_is_num(mode)) {
2149 /* the following code leads to endless recursion when Mul are replaced by a simple instruction chain */
2150 if (!is_arch_dep_running() && a == b && mode_is_int(mode)) {
2151 ir_node *block = get_nodes_block(n);
2154 get_irn_dbg_info(n),
2158 new_r_Const_long(current_ir_graph, block, mode, 2),
2160 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_A);
2165 get_irn_dbg_info(n),
2171 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_MINUS_B);
2176 get_irn_dbg_info(n),
2182 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_MINUS_B);
2185 if (get_mode_arithmetic(mode) == irma_twos_complement) {
2186 /* Here we rely on constants be on the RIGHT side */
2188 ir_node *op = get_Not_op(a);
2190 if (is_Const(b) && is_Const_one(b)) {
2192 ir_node *blk = get_irn_n(n, -1);
2193 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, blk, op, mode);
2194 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_PLUS_1);
2199 ir_node *blk = get_irn_n(n, -1);
2200 n = new_r_Const(current_ir_graph, blk, mode, get_mode_minus_one(mode));
2201 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_X_NOT_X);
2206 ir_node *op = get_Not_op(b);
2210 ir_node *blk = get_irn_n(n, -1);
2211 n = new_r_Const(current_ir_graph, blk, mode, get_mode_minus_one(mode));
2212 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_X_NOT_X);
2219 } /* transform_node_Add */
2222 * returns -cnst or NULL if impossible
2224 static ir_node *const_negate(ir_node *cnst) {
2225 tarval *tv = tarval_neg(get_Const_tarval(cnst));
2226 dbg_info *dbgi = get_irn_dbg_info(cnst);
2227 ir_graph *irg = get_irn_irg(cnst);
2228 ir_node *block = get_nodes_block(cnst);
2229 ir_mode *mode = get_irn_mode(cnst);
2230 if (tv == tarval_bad) return NULL;
2231 return new_rd_Const(dbgi, irg, block, mode, tv);
2235 * Do the AddSub optimization, then Transform
2236 * Constant folding on Phi
2237 * Sub(0,a) -> Minus(a)
2238 * Sub(Mul(a, x), a) -> Mul(a, x-1)
2239 * Sub(Sub(x, y), b) -> Sub(x, Add(y,b))
2240 * Sub(Add(a, x), x) -> a
2241 * Sub(x, Add(x, a)) -> -a
2242 * Sub(x, Const) -> Add(x, -Const)
2244 static ir_node *transform_node_Sub(ir_node *n) {
2249 n = transform_node_AddSub(n);
2251 a = get_Sub_left(n);
2252 b = get_Sub_right(n);
2254 mode = get_irn_mode(n);
2256 if (mode_is_int(mode)) {
2257 ir_mode *lmode = get_irn_mode(a);
2259 if (is_Const(b) && is_Const_null(b) && mode_is_reference(lmode)) {
2260 /* a Sub(a, NULL) is a hidden Conv */
2261 dbg_info *dbg = get_irn_dbg_info(n);
2262 return new_rd_Conv(dbg, current_ir_graph, get_nodes_block(n), a, mode);
2267 HANDLE_BINOP_PHI(tarval_sub, a, b, c, mode);
2269 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
2270 if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
2273 if (is_Const(b) && get_irn_mode(b) != mode_P) {
2274 /* a - C -> a + (-C) */
2275 ir_node *cnst = const_negate(b);
2277 ir_node *block = get_nodes_block(n);
2278 dbg_info *dbgi = get_irn_dbg_info(n);
2279 ir_graph *irg = get_irn_irg(n);
2281 n = new_rd_Add(dbgi, irg, block, a, cnst, mode);
2282 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2287 if (is_Minus(a)) { /* (-a) - b -> -(a + b) */
2288 ir_graph *irg = current_ir_graph;
2289 dbg_info *dbg = get_irn_dbg_info(n);
2290 ir_node *block = get_nodes_block(n);
2291 ir_node *left = get_Minus_op(a);
2292 ir_node *add = new_rd_Add(dbg, irg, block, left, b, mode);
2294 n = new_rd_Minus(dbg, irg, block, add, mode);
2295 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2297 } else if (is_Minus(b)) { /* a - (-b) -> a + b */
2298 ir_graph *irg = current_ir_graph;
2299 dbg_info *dbg = get_irn_dbg_info(n);
2300 ir_node *block = get_nodes_block(n);
2301 ir_node *right = get_Minus_op(b);
2303 n = new_rd_Add(dbg, irg, block, a, right, mode);
2304 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MINUS);
2306 } else if (is_Sub(b)) { /* a - (b - c) -> a + (c - b) */
2307 ir_graph *irg = current_ir_graph;
2308 dbg_info *s_dbg = get_irn_dbg_info(b);
2309 ir_node *s_block = get_nodes_block(b);
2310 ir_node *s_left = get_Sub_right(b);
2311 ir_node *s_right = get_Sub_left(b);
2312 ir_mode *s_mode = get_irn_mode(b);
2313 ir_node *sub = new_rd_Sub(s_dbg, irg, s_block, s_left, s_right, s_mode);
2314 dbg_info *a_dbg = get_irn_dbg_info(n);
2315 ir_node *a_block = get_nodes_block(n);
2317 n = new_rd_Add(a_dbg, irg, a_block, a, sub, mode);
2318 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2320 } else if (is_Mul(b)) { /* a - (b * C) -> a + (b * -C) */
2321 ir_node *m_right = get_Mul_right(b);
2322 if (is_Const(m_right)) {
2323 ir_node *cnst2 = const_negate(m_right);
2324 if (cnst2 != NULL) {
2325 ir_graph *irg = current_ir_graph;
2326 dbg_info *m_dbg = get_irn_dbg_info(b);
2327 ir_node *m_block = get_nodes_block(b);
2328 ir_node *m_left = get_Mul_left(b);
2329 ir_mode *m_mode = get_irn_mode(b);
2330 ir_node *mul = new_rd_Mul(m_dbg, irg, m_block, m_left, cnst2, m_mode);
2331 dbg_info *a_dbg = get_irn_dbg_info(n);
2332 ir_node *a_block = get_nodes_block(n);
2334 n = new_rd_Add(a_dbg, irg, a_block, a, mul, mode);
2335 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2341 /* Beware of Sub(P, P) which cannot be optimized into a simple Minus ... */
2342 if (mode_is_num(mode) && mode == get_irn_mode(a) && is_Const(a) && is_Const_null(a)) {
2344 get_irn_dbg_info(n),
2349 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_0_A);
2353 if (mode_wrap_around(mode)) {
2354 ir_node *left = get_Add_left(a);
2355 ir_node *right = get_Add_right(a);
2357 /* FIXME: Does the Conv's work only for two complement or generally? */
2359 if (mode != get_irn_mode(right)) {
2360 /* This Sub is an effective Cast */
2361 right = new_r_Conv(get_irn_irg(n), get_irn_n(n, -1), right, mode);
2364 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2366 } else if (right == b) {
2367 if (mode != get_irn_mode(left)) {
2368 /* This Sub is an effective Cast */
2369 left = new_r_Conv(get_irn_irg(n), get_irn_n(n, -1), left, mode);
2372 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2378 if (mode_wrap_around(mode)) {
2379 ir_node *left = get_Add_left(b);
2380 ir_node *right = get_Add_right(b);
2382 /* FIXME: Does the Conv's work only for two complement or generally? */
2384 ir_mode *r_mode = get_irn_mode(right);
2386 n = new_r_Minus(get_irn_irg(n), get_irn_n(n, -1), right, r_mode);
2387 if (mode != r_mode) {
2388 /* This Sub is an effective Cast */
2389 n = new_r_Conv(get_irn_irg(n), get_irn_n(n, -1), n, mode);
2391 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2393 } else if (right == a) {
2394 ir_mode *l_mode = get_irn_mode(left);
2396 n = new_r_Minus(get_irn_irg(n), get_irn_n(n, -1), left, l_mode);
2397 if (mode != l_mode) {
2398 /* This Sub is an effective Cast */
2399 n = new_r_Conv(get_irn_irg(n), get_irn_n(n, -1), n, mode);
2401 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2406 if (mode_is_int(mode) && is_Conv(a) && is_Conv(b)) {
2407 ir_mode *mode = get_irn_mode(a);
2409 if (mode == get_irn_mode(b)) {
2411 ir_node *op_a = get_Conv_op(a);
2412 ir_node *op_b = get_Conv_op(b);
2414 /* check if it's allowed to skip the conv */
2415 ma = get_irn_mode(op_a);
2416 mb = get_irn_mode(op_b);
2418 if (mode_is_reference(ma) && mode_is_reference(mb)) {
2419 /* SubInt(ConvInt(aP), ConvInt(bP)) -> SubInt(aP,bP) */
2422 set_Sub_right(n, b);
2428 /* do NOT execute this code if reassociation is enabled, it does the inverse! */
2429 if (!is_reassoc_running() && is_Mul(a)) {
2430 ir_node *ma = get_Mul_left(a);
2431 ir_node *mb = get_Mul_right(a);
2434 ir_node *blk = get_irn_n(n, -1);
2436 get_irn_dbg_info(n),
2437 current_ir_graph, blk,
2440 get_irn_dbg_info(n),
2441 current_ir_graph, blk,
2443 new_r_Const_long(current_ir_graph, blk, mode, 1),
2446 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MUL_A_X_A);
2448 } else if (mb == b) {
2449 ir_node *blk = get_irn_n(n, -1);
2451 get_irn_dbg_info(n),
2452 current_ir_graph, blk,
2455 get_irn_dbg_info(n),
2456 current_ir_graph, blk,
2458 new_r_Const_long(current_ir_graph, blk, mode, 1),
2461 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MUL_A_X_A);
2465 if (is_Sub(a)) { /* (x - y) - b -> x - (y + b) */
2466 ir_node *x = get_Sub_left(a);
2467 ir_node *y = get_Sub_right(a);
2468 ir_node *blk = get_irn_n(n, -1);
2469 ir_mode *m_b = get_irn_mode(b);
2470 ir_mode *m_y = get_irn_mode(y);
2474 /* Determine the right mode for the Add. */
2477 else if (mode_is_reference(m_b))
2479 else if (mode_is_reference(m_y))
2483 * Both modes are different but none is reference,
2484 * happens for instance in SubP(SubP(P, Iu), Is).
2485 * We have two possibilities here: Cast or ignore.
2486 * Currently we ignore this case.
2491 add = new_r_Add(current_ir_graph, blk, y, b, add_mode);
2493 n = new_rd_Sub(get_irn_dbg_info(n), current_ir_graph, blk, x, add, mode);
2494 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_SUB_X_Y_Z);
2498 if (get_mode_arithmetic(mode) == irma_twos_complement) {
2499 if (is_Const(a) && is_Not(b)) {
2500 /* c - ~X = X + (c+1) */
2501 tarval *tv = get_Const_tarval(a);
2503 tv = tarval_add(tv, get_mode_one(mode));
2504 if (tv != tarval_bad) {
2505 ir_node *blk = get_irn_n(n, -1);
2506 ir_node *c = new_r_Const(current_ir_graph, blk, mode, tv);
2507 n = new_rd_Add(get_irn_dbg_info(n), current_ir_graph, blk, get_Not_op(b), c, mode);
2508 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_C_NOT_X);
2514 } /* transform_node_Sub */
2517 * Several transformation done on n*n=2n bits mul.
2518 * These transformations must be done here because new nodes may be produced.
2520 static ir_node *transform_node_Mul2n(ir_node *n, ir_mode *mode) {
2522 ir_node *a = get_Mul_left(n);
2523 ir_node *b = get_Mul_right(n);
2524 tarval *ta = value_of(a);
2525 tarval *tb = value_of(b);
2526 ir_mode *smode = get_irn_mode(a);
2528 if (ta == get_mode_one(smode)) {
2529 /* (L)1 * (L)b = (L)b */
2530 ir_node *blk = get_irn_n(n, -1);
2531 n = new_rd_Conv(get_irn_dbg_info(n), current_ir_graph, blk, b, mode);
2532 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
2535 else if (ta == get_mode_minus_one(smode)) {
2536 /* (L)-1 * (L)b = (L)b */
2537 ir_node *blk = get_irn_n(n, -1);
2538 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, blk, b, smode);
2539 n = new_rd_Conv(get_irn_dbg_info(n), current_ir_graph, blk, n, mode);
2540 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2543 if (tb == get_mode_one(smode)) {
2544 /* (L)a * (L)1 = (L)a */
2545 ir_node *blk = get_irn_n(a, -1);
2546 n = new_rd_Conv(get_irn_dbg_info(n), current_ir_graph, blk, a, mode);
2547 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
2550 else if (tb == get_mode_minus_one(smode)) {
2551 /* (L)a * (L)-1 = (L)-a */
2552 ir_node *blk = get_irn_n(n, -1);
2553 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, blk, a, smode);
2554 n = new_rd_Conv(get_irn_dbg_info(n), current_ir_graph, blk, n, mode);
2555 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2562 * Transform Mul(a,-1) into -a.
2563 * Do constant evaluation of Phi nodes.
2564 * Do architecture dependent optimizations on Mul nodes
2566 static ir_node *transform_node_Mul(ir_node *n) {
2567 ir_node *c, *oldn = n;
2568 ir_mode *mode = get_irn_mode(n);
2569 ir_node *a = get_Mul_left(n);
2570 ir_node *b = get_Mul_right(n);
2572 if (is_Bad(a) || is_Bad(b))
2575 if (mode != get_irn_mode(a))
2576 return transform_node_Mul2n(n, mode);
2578 HANDLE_BINOP_PHI(tarval_mul, a, b, c, mode);
2580 if (mode_is_signed(mode)) {
2583 if (value_of(a) == get_mode_minus_one(mode))
2585 else if (value_of(b) == get_mode_minus_one(mode))
2588 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1), r, mode);
2589 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2594 if (is_Const(b)) { /* (-a) * const -> a * -const */
2595 ir_node *cnst = const_negate(b);
2597 dbg_info *dbgi = get_irn_dbg_info(n);
2598 ir_node *block = get_nodes_block(n);
2599 n = new_rd_Mul(dbgi, current_ir_graph, block, get_Minus_op(a), cnst, mode);
2600 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2603 } else if (is_Minus(b)) { /* (-a) * (-b) -> a * b */
2604 dbg_info *dbgi = get_irn_dbg_info(n);
2605 ir_node *block = get_nodes_block(n);
2606 n = new_rd_Mul(dbgi, current_ir_graph, block, get_Minus_op(a), get_Minus_op(b), mode);
2607 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_MINUS);
2609 } else if (is_Sub(b)) { /* (-a) * (b - c) -> a * (c - b) */
2610 ir_node *sub_l = get_Sub_left(b);
2611 ir_node *sub_r = get_Sub_right(b);
2612 dbg_info *dbgi = get_irn_dbg_info(n);
2613 ir_graph *irg = current_ir_graph;
2614 ir_node *block = get_nodes_block(n);
2615 ir_node *new_b = new_rd_Sub(dbgi, irg, block, sub_r, sub_l, mode);
2616 n = new_rd_Mul(dbgi, irg, block, get_Minus_op(a), new_b, mode);
2617 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS);
2620 } else if (is_Minus(b)) {
2621 if (is_Sub(a)) { /* (a - b) * (-c) -> (b - a) * c */
2622 ir_node *sub_l = get_Sub_left(a);
2623 ir_node *sub_r = get_Sub_right(a);
2624 dbg_info *dbgi = get_irn_dbg_info(n);
2625 ir_graph *irg = current_ir_graph;
2626 ir_node *block = get_nodes_block(n);
2627 ir_node *new_a = new_rd_Sub(dbgi, irg, block, sub_r, sub_l, mode);
2628 n = new_rd_Mul(dbgi, irg, block, new_a, get_Minus_op(b), mode);
2629 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS);
2633 if (get_mode_arithmetic(mode) == irma_ieee754) {
2635 tarval *tv = get_Const_tarval(a);
2636 if (tarval_ieee754_get_exponent(tv) == 1 && tarval_ieee754_zero_mantissa(tv)) {
2637 /* 2.0 * b = b + b */
2638 n = new_rd_Add(get_irn_dbg_info(n), current_ir_graph, get_nodes_block(n), b, b, mode);
2639 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_A_A);
2643 else if (is_Const(b)) {
2644 tarval *tv = get_Const_tarval(b);
2645 if (tarval_ieee754_get_exponent(tv) == 1 && tarval_ieee754_zero_mantissa(tv)) {
2646 /* a * 2.0 = a + a */
2647 n = new_rd_Add(get_irn_dbg_info(n), current_ir_graph, get_nodes_block(n), a, a, mode);
2648 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_A_A);
2653 return arch_dep_replace_mul_with_shifts(n);
2654 } /* transform_node_Mul */
2657 * Transform a Div Node.
2659 static ir_node *transform_node_Div(ir_node *n) {
2660 ir_mode *mode = get_Div_resmode(n);
2661 ir_node *a = get_Div_left(n);
2662 ir_node *b = get_Div_right(n);
2666 if (is_Const(b) && is_const_Phi(a)) {
2667 /* check for Div(Phi, Const) */
2668 value = apply_binop_on_phi(a, get_Const_tarval(b), tarval_div, mode, 0);
2670 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2674 else if (is_Const(a) && is_const_Phi(b)) {
2675 /* check for Div(Const, Phi) */
2676 value = apply_binop_on_phi(b, get_Const_tarval(a), tarval_div, mode, 1);
2678 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2682 else if (is_const_Phi(a) && is_const_Phi(b)) {
2683 /* check for Div(Phi, Phi) */
2684 value = apply_binop_on_2_phis(a, b, tarval_div, mode);
2686 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2693 if (tv != tarval_bad) {
2694 value = new_Const(get_tarval_mode(tv), tv);
2696 DBG_OPT_CSTEVAL(n, value);
2699 ir_node *a = get_Div_left(n);
2700 ir_node *b = get_Div_right(n);
2703 if (a == b && value_not_zero(a, &dummy)) {
2704 /* BEWARE: we can optimize a/a to 1 only if this cannot cause a exception */
2705 value = new_Const(mode, get_mode_one(mode));
2706 DBG_OPT_CSTEVAL(n, value);
2709 if (mode_is_signed(mode) && is_Const(b)) {
2710 tarval *tv = get_Const_tarval(b);
2712 if (tv == get_mode_minus_one(mode)) {
2714 value = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1), a, mode);
2715 DBG_OPT_CSTEVAL(n, value);
2719 /* Try architecture dependent optimization */
2720 value = arch_dep_replace_div_by_const(n);
2728 /* Turn Div into a tuple (mem, jmp, bad, value) */
2729 mem = get_Div_mem(n);
2730 blk = get_irn_n(n, -1);
2732 /* skip a potential Pin */
2734 mem = get_Pin_op(mem);
2735 turn_into_tuple(n, pn_Div_max);
2736 set_Tuple_pred(n, pn_Div_M, mem);
2737 set_Tuple_pred(n, pn_Div_X_regular, new_r_Jmp(current_ir_graph, blk));
2738 set_Tuple_pred(n, pn_Div_X_except, new_Bad());
2739 set_Tuple_pred(n, pn_Div_res, value);
2742 } /* transform_node_Div */
2745 * Transform a Mod node.
2747 static ir_node *transform_node_Mod(ir_node *n) {
2748 ir_mode *mode = get_Mod_resmode(n);
2749 ir_node *a = get_Mod_left(n);
2750 ir_node *b = get_Mod_right(n);
2754 if (is_Const(b) && is_const_Phi(a)) {
2755 /* check for Div(Phi, Const) */
2756 value = apply_binop_on_phi(a, get_Const_tarval(b), tarval_mod, mode, 0);
2758 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2762 else if (is_Const(a) && is_const_Phi(b)) {
2763 /* check for Div(Const, Phi) */
2764 value = apply_binop_on_phi(b, get_Const_tarval(a), tarval_mod, mode, 1);
2766 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2770 else if (is_const_Phi(a) && is_const_Phi(b)) {
2771 /* check for Div(Phi, Phi) */
2772 value = apply_binop_on_2_phis(a, b, tarval_mod, mode);
2774 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2781 if (tv != tarval_bad) {
2782 value = new_Const(get_tarval_mode(tv), tv);
2784 DBG_OPT_CSTEVAL(n, value);
2787 ir_node *a = get_Mod_left(n);
2788 ir_node *b = get_Mod_right(n);
2791 if (a == b && value_not_zero(a, &dummy)) {
2792 /* BEWARE: we can optimize a%a to 0 only if this cannot cause a exception */
2793 value = new_Const(mode, get_mode_null(mode));
2794 DBG_OPT_CSTEVAL(n, value);
2797 if (mode_is_signed(mode) && is_Const(b)) {
2798 tarval *tv = get_Const_tarval(b);
2800 if (tv == get_mode_minus_one(mode)) {
2802 value = new_Const(mode, get_mode_null(mode));
2803 DBG_OPT_CSTEVAL(n, value);
2807 /* Try architecture dependent optimization */
2808 value = arch_dep_replace_mod_by_const(n);
2816 /* Turn Mod into a tuple (mem, jmp, bad, value) */
2817 mem = get_Mod_mem(n);
2818 blk = get_irn_n(n, -1);
2820 /* skip a potential Pin */
2822 mem = get_Pin_op(mem);
2823 turn_into_tuple(n, pn_Mod_max);
2824 set_Tuple_pred(n, pn_Mod_M, mem);
2825 set_Tuple_pred(n, pn_Mod_X_regular, new_r_Jmp(current_ir_graph, blk));
2826 set_Tuple_pred(n, pn_Mod_X_except, new_Bad());
2827 set_Tuple_pred(n, pn_Mod_res, value);
2830 } /* transform_node_Mod */
2833 * Transform a DivMod node.
2835 static ir_node *transform_node_DivMod(ir_node *n) {
2837 ir_node *a = get_DivMod_left(n);
2838 ir_node *b = get_DivMod_right(n);
2839 ir_mode *mode = get_DivMod_resmode(n);
2844 if (is_Const(b) && is_const_Phi(a)) {
2845 /* check for Div(Phi, Const) */
2846 va = apply_binop_on_phi(a, get_Const_tarval(b), tarval_div, mode, 0);
2847 vb = apply_binop_on_phi(a, get_Const_tarval(b), tarval_mod, mode, 0);
2849 DBG_OPT_ALGSIM0(n, va, FS_OPT_CONST_PHI);
2850 DBG_OPT_ALGSIM0(n, vb, FS_OPT_CONST_PHI);
2854 else if (is_Const(a) && is_const_Phi(b)) {
2855 /* check for Div(Const, Phi) */
2856 va = apply_binop_on_phi(b, get_Const_tarval(a), tarval_div, mode, 1);
2857 vb = apply_binop_on_phi(b, get_Const_tarval(a), tarval_mod, mode, 1);
2859 DBG_OPT_ALGSIM0(n, va, FS_OPT_CONST_PHI);
2860 DBG_OPT_ALGSIM0(n, vb, FS_OPT_CONST_PHI);
2864 else if (is_const_Phi(a) && is_const_Phi(b)) {
2865 /* check for Div(Phi, Phi) */
2866 va = apply_binop_on_2_phis(a, b, tarval_div, mode);
2867 vb = apply_binop_on_2_phis(a, b, tarval_mod, mode);
2869 DBG_OPT_ALGSIM0(n, va, FS_OPT_CONST_PHI);
2870 DBG_OPT_ALGSIM0(n, vb, FS_OPT_CONST_PHI);
2877 if (tb != tarval_bad) {
2878 if (tb == get_mode_one(get_tarval_mode(tb))) {
2880 vb = new_Const(mode, get_mode_null(mode));
2881 DBG_OPT_CSTEVAL(n, vb);
2883 } else if (ta != tarval_bad) {
2884 tarval *resa, *resb;
2885 resa = tarval_div(ta, tb);
2886 if (resa == tarval_bad) return n; /* Causes exception!!! Model by replacing through
2887 Jmp for X result!? */
2888 resb = tarval_mod(ta, tb);
2889 if (resb == tarval_bad) return n; /* Causes exception! */
2890 va = new_Const(mode, resa);
2891 vb = new_Const(mode, resb);
2892 DBG_OPT_CSTEVAL(n, va);
2893 DBG_OPT_CSTEVAL(n, vb);
2895 } else if (mode_is_signed(mode) && tb == get_mode_minus_one(mode)) {
2896 va = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1), a, mode);
2897 vb = new_Const(mode, get_mode_null(mode));
2898 DBG_OPT_CSTEVAL(n, va);
2899 DBG_OPT_CSTEVAL(n, vb);
2901 } else { /* Try architecture dependent optimization */
2904 arch_dep_replace_divmod_by_const(&va, &vb, n);
2905 evaluated = va != NULL;
2907 } else if (a == b) {
2908 if (value_not_zero(a, &dummy)) {
2910 va = new_Const(mode, get_mode_one(mode));
2911 vb = new_Const(mode, get_mode_null(mode));
2912 DBG_OPT_CSTEVAL(n, va);
2913 DBG_OPT_CSTEVAL(n, vb);
2916 /* BEWARE: it is NOT possible to optimize a/a to 1, as this may cause a exception */
2919 } else if (ta == get_mode_null(mode) && value_not_zero(b, &dummy)) {
2920 /* 0 / non-Const = 0 */
2925 if (evaluated) { /* replace by tuple */
2929 mem = get_DivMod_mem(n);
2930 /* skip a potential Pin */
2932 mem = get_Pin_op(mem);
2934 blk = get_irn_n(n, -1);
2935 turn_into_tuple(n, pn_DivMod_max);
2936 set_Tuple_pred(n, pn_DivMod_M, mem);
2937 set_Tuple_pred(n, pn_DivMod_X_regular, new_r_Jmp(current_ir_graph, blk));
2938 set_Tuple_pred(n, pn_DivMod_X_except, new_Bad()); /* no exception */
2939 set_Tuple_pred(n, pn_DivMod_res_div, va);
2940 set_Tuple_pred(n, pn_DivMod_res_mod, vb);
2944 } /* transform_node_DivMod */
2947 * Optimize x / c to x * (1/c)
2949 static ir_node *transform_node_Quot(ir_node *n) {
2950 ir_mode *mode = get_Quot_resmode(n);
2953 if (get_mode_arithmetic(mode) == irma_ieee754) {
2954 ir_node *b = get_Quot_right(n);
2957 tarval *tv = get_Const_tarval(b);
2961 * Floating point constant folding might be disabled here to
2963 * However, as we check for exact result, doing it is safe.
2966 rem = tarval_enable_fp_ops(1);
2967 tv = tarval_quo(get_mode_one(mode), tv);
2968 (void)tarval_enable_fp_ops(rem);
2970 /* Do the transformation if the result is either exact or we are not
2971 using strict rules. */
2972 if (tv != tarval_bad &&
2973 (tarval_ieee754_get_exact() || (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic) == 0)) {
2974 ir_node *blk = get_irn_n(n, -1);
2975 ir_node *c = new_r_Const(current_ir_graph, blk, mode, tv);
2976 ir_node *a = get_Quot_left(n);
2977 ir_node *m = new_rd_Mul(get_irn_dbg_info(n), current_ir_graph, blk, a, c, mode);
2978 ir_node *mem = get_Quot_mem(n);
2980 /* skip a potential Pin */
2982 mem = get_Pin_op(mem);
2983 turn_into_tuple(n, pn_Quot_max);
2984 set_Tuple_pred(n, pn_Quot_M, mem);
2985 set_Tuple_pred(n, pn_Quot_X_regular, new_r_Jmp(current_ir_graph, blk));
2986 set_Tuple_pred(n, pn_Quot_X_except, new_r_Bad(current_ir_graph));
2987 set_Tuple_pred(n, pn_Quot_res, m);
2988 DBG_OPT_ALGSIM1(oldn, a, b, m, FS_OPT_FP_INV_MUL);
2993 } /* transform_node_Quot */
2996 * Optimize Abs(x) into x if x is Confirmed >= 0
2997 * Optimize Abs(x) into -x if x is Confirmed <= 0
2998 * Optimize Abs(-x) int Abs(x)
3000 static ir_node *transform_node_Abs(ir_node *n) {
3001 ir_node *c, *oldn = n;
3002 ir_node *a = get_Abs_op(n);
3005 HANDLE_UNOP_PHI(tarval_abs, a, c);
3007 switch (classify_value_sign(a)) {
3008 case value_classified_negative:
3009 mode = get_irn_mode(n);
3012 * We can replace the Abs by -x here.
3013 * We even could add a new Confirm here
3014 * (if not twos complement)
3016 * Note that -x would create a new node, so we could
3017 * not run it in the equivalent_node() context.
3019 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph,
3020 get_nodes_block(n), a, mode);
3022 DBG_OPT_CONFIRM(oldn, n);
3024 case value_classified_positive:
3025 /* n is positive, Abs is not needed */
3028 DBG_OPT_CONFIRM(oldn, n);
3034 /* Abs(-x) = Abs(x) */
3035 mode = get_irn_mode(n);
3036 n = new_rd_Abs(get_irn_dbg_info(n), current_ir_graph,
3037 get_nodes_block(n), get_Minus_op(a), mode);
3038 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ABS_MINUS_X);
3042 } /* transform_node_Abs */
3045 * Transform a Cond node.
3047 * Replace the Cond by a Jmp if it branches on a constant
3050 static ir_node *transform_node_Cond(ir_node *n) {
3053 ir_node *a = get_Cond_selector(n);
3054 tarval *ta = value_of(a);
3056 /* we need block info which is not available in floating irgs */
3057 if (get_irg_pinned(current_ir_graph) == op_pin_state_floats)
3060 if ((ta != tarval_bad) &&
3061 (get_irn_mode(a) == mode_b) &&
3062 (get_opt_unreachable_code())) {
3063 /* It's a boolean Cond, branching on a boolean constant.
3064 Replace it by a tuple (Bad, Jmp) or (Jmp, Bad) */
3065 ir_node *blk = get_nodes_block(n);
3066 jmp = new_r_Jmp(current_ir_graph, blk);
3067 turn_into_tuple(n, pn_Cond_max);
3068 if (ta == tarval_b_true) {
3069 set_Tuple_pred(n, pn_Cond_false, new_Bad());
3070 set_Tuple_pred(n, pn_Cond_true, jmp);
3072 set_Tuple_pred(n, pn_Cond_false, jmp);
3073 set_Tuple_pred(n, pn_Cond_true, new_Bad());
3075 /* We might generate an endless loop, so keep it alive. */
3076 add_End_keepalive(get_irg_end(current_ir_graph), blk);
3079 } /* transform_node_Cond */
3082 * Prototype of a recursive transform function
3083 * for bitwise distributive transformations.
3085 typedef ir_node* (*recursive_transform)(ir_node *n);
3088 * makes use of distributive laws for and, or, eor
3089 * and(a OP c, b OP c) -> and(a, b) OP c
3090 * note, might return a different op than n
3092 static ir_node *transform_bitwise_distributive(ir_node *n,
3093 recursive_transform trans_func)
3096 ir_node *a = get_binop_left(n);
3097 ir_node *b = get_binop_right(n);
3098 ir_op *op = get_irn_op(a);
3099 ir_op *op_root = get_irn_op(n);
3101 if(op != get_irn_op(b))
3104 if (op == op_Conv) {
3105 ir_node *a_op = get_Conv_op(a);
3106 ir_node *b_op = get_Conv_op(b);
3107 ir_mode *a_mode = get_irn_mode(a_op);
3108 ir_mode *b_mode = get_irn_mode(b_op);
3109 if(a_mode == b_mode && (mode_is_int(a_mode) || a_mode == mode_b)) {
3110 ir_node *blk = get_irn_n(n, -1);
3113 set_binop_left(n, a_op);
3114 set_binop_right(n, b_op);
3115 set_irn_mode(n, a_mode);
3117 n = new_r_Conv(current_ir_graph, blk, n, get_irn_mode(oldn));
3119 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_SHIFT_AND);
3125 /* nothing to gain here */
3129 if (op == op_Shrs || op == op_Shr || op == op_Shl
3130 || op == op_And || op == op_Or || op == op_Eor) {
3131 ir_node *a_left = get_binop_left(a);
3132 ir_node *a_right = get_binop_right(a);
3133 ir_node *b_left = get_binop_left(b);
3134 ir_node *b_right = get_binop_right(b);
3136 ir_node *op1 = NULL;
3137 ir_node *op2 = NULL;
3139 if (is_op_commutative(op)) {
3140 if (a_left == b_left) {
3144 } else if(a_left == b_right) {
3148 } else if(a_right == b_left) {
3154 if(a_right == b_right) {
3161 /* (a sop c) & (b sop c) => (a & b) sop c */
3162 ir_node *blk = get_irn_n(n, -1);
3164 ir_node *new_n = exact_copy(n);
3165 set_binop_left(new_n, op1);
3166 set_binop_right(new_n, op2);
3167 new_n = trans_func(new_n);
3169 if(op_root == op_Eor && op == op_Or) {
3170 dbg_info *dbgi = get_irn_dbg_info(n);
3171 ir_graph *irg = current_ir_graph;
3172 ir_mode *mode = get_irn_mode(c);
3174 c = new_rd_Not(dbgi, irg, blk, c, mode);
3175 n = new_rd_And(dbgi, irg, blk, new_n, c, mode);
3178 set_nodes_block(n, blk);
3179 set_binop_left(n, new_n);
3180 set_binop_right(n, c);
3181 add_identities(current_ir_graph->value_table, n);
3184 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_SHIFT_AND);
3195 static ir_node *transform_node_And(ir_node *n) {
3196 ir_node *c, *oldn = n;
3197 ir_node *a = get_And_left(n);
3198 ir_node *b = get_And_right(n);
3201 mode = get_irn_mode(n);
3202 HANDLE_BINOP_PHI(tarval_and, a, b, c, mode);
3204 /* we can evaluate 2 Projs of the same Cmp */
3205 if (mode == mode_b && is_Proj(a) && is_Proj(b)) {
3206 ir_node *pred_a = get_Proj_pred(a);
3207 ir_node *pred_b = get_Proj_pred(b);
3208 if (pred_a == pred_b) {
3209 dbg_info *dbgi = get_irn_dbg_info(n);
3210 ir_node *block = get_nodes_block(pred_a);
3211 pn_Cmp pn_a = get_Proj_proj(a);
3212 pn_Cmp pn_b = get_Proj_proj(b);
3213 /* yes, we can simply calculate with pncs */
3214 pn_Cmp new_pnc = pn_a & pn_b;
3216 return new_rd_Proj(dbgi, current_ir_graph, block, pred_a, mode_b, new_pnc);
3221 ir_node *op = get_Not_op(b);
3223 ir_node *ba = get_And_left(op);
3224 ir_node *bb = get_And_right(op);
3226 /* it's enough to test the following cases due to normalization! */
3227 if (get_Or_left(a) == ba && get_Or_right(a) == bb) {
3228 /* (a|b) & ~(a&b) = a^b */
3229 ir_node *block = get_nodes_block(n);
3231 n = new_rd_Eor(get_irn_dbg_info(n), current_ir_graph, block, ba, bb, mode);
3232 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_TO_EOR);
3240 ir_node *op = get_Not_op(a);
3242 ir_node *aa = get_And_left(op);
3243 ir_node *ab = get_And_right(op);
3245 /* it's enough to test the following cases due to normalization! */
3246 if (get_Or_left(b) == aa && get_Or_right(b) == ab) {
3247 /* (a|b) & ~(a&b) = a^b */
3248 ir_node *block = get_nodes_block(n);
3250 n = new_rd_Eor(get_irn_dbg_info(n), current_ir_graph, block, aa, ab, mode);
3251 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_TO_EOR);
3258 ir_node *al = get_Eor_left(a);
3259 ir_node *ar = get_Eor_right(a);
3262 /* (b ^ a) & b -> ~a & b */
3263 dbg_info *dbg = get_irn_dbg_info(n);
3264 ir_node *block = get_nodes_block(n);
3266 ar = new_rd_Not(dbg, current_ir_graph, block, ar, mode);
3267 n = new_rd_And(dbg, current_ir_graph, block, ar, b, mode);
3268 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3272 /* (a ^ b) & b -> ~a & b */
3273 dbg_info *dbg = get_irn_dbg_info(n);
3274 ir_node *block = get_nodes_block(n);
3276 al = new_rd_Not(dbg, current_ir_graph, block, al, mode);
3277 n = new_rd_And(dbg, current_ir_graph, block, al, b, mode);
3278 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3283 ir_node *bl = get_Eor_left(b);
3284 ir_node *br = get_Eor_right(b);
3287 /* a & (a ^ b) -> a & ~b */
3288 dbg_info *dbg = get_irn_dbg_info(n);
3289 ir_node *block = get_nodes_block(n);
3291 br = new_rd_Not(dbg, current_ir_graph, block, br, mode);
3292 n = new_rd_And(dbg, current_ir_graph, block, br, a, mode);
3293 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3297 /* a & (b ^ a) -> a & ~b */
3298 dbg_info *dbg = get_irn_dbg_info(n);
3299 ir_node *block = get_nodes_block(n);
3301 bl = new_rd_Not(dbg, current_ir_graph, block, bl, mode);
3302 n = new_rd_And(dbg, current_ir_graph, block, bl, a, mode);
3303 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3307 if (is_Not(a) && is_Not(b)) {
3308 /* ~a & ~b = ~(a|b) */
3309 ir_node *block = get_nodes_block(n);
3310 ir_mode *mode = get_irn_mode(n);
3314 n = new_rd_Or(get_irn_dbg_info(n), current_ir_graph, block, a, b, mode);
3315 n = new_rd_Not(get_irn_dbg_info(n), current_ir_graph, block, n, mode);
3316 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_DEMORGAN);
3320 n = transform_bitwise_distributive(n, transform_node_And);
3323 } /* transform_node_And */
3328 static ir_node *transform_node_Eor(ir_node *n) {
3329 ir_node *c, *oldn = n;
3330 ir_node *a = get_Eor_left(n);
3331 ir_node *b = get_Eor_right(n);
3332 ir_mode *mode = get_irn_mode(n);
3334 HANDLE_BINOP_PHI(tarval_eor, a, b, c, mode);
3336 /* we can evaluate 2 Projs of the same Cmp */
3337 if (mode == mode_b && is_Proj(a) && is_Proj(b)) {
3338 ir_node *pred_a = get_Proj_pred(a);
3339 ir_node *pred_b = get_Proj_pred(b);
3340 if(pred_a == pred_b) {
3341 dbg_info *dbgi = get_irn_dbg_info(n);
3342 ir_node *block = get_nodes_block(pred_a);
3343 pn_Cmp pn_a = get_Proj_proj(a);
3344 pn_Cmp pn_b = get_Proj_proj(b);
3345 /* yes, we can simply calculate with pncs */
3346 pn_Cmp new_pnc = pn_a ^ pn_b;
3348 return new_rd_Proj(dbgi, current_ir_graph, block, pred_a, mode_b,
3355 n = new_rd_Const(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1),
3356 mode, get_mode_null(mode));
3357 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_A_A);
3358 } else if (mode == mode_b &&
3360 is_Const(b) && is_Const_one(b) &&
3361 is_Cmp(get_Proj_pred(a))) {
3362 /* The Eor negates a Cmp. The Cmp has the negated result anyways! */
3363 n = new_r_Proj(current_ir_graph, get_irn_n(n, -1), get_Proj_pred(a),
3364 mode_b, get_negated_pnc(get_Proj_proj(a), mode));
3366 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT_BOOL);
3367 } else if (is_Const(b)) {
3368 if (is_Not(a)) { /* ~x ^ const -> x ^ ~const */
3369 ir_node *cnst = new_Const(mode, tarval_not(get_Const_tarval(b)));
3370 ir_node *not_op = get_Not_op(a);
3371 dbg_info *dbg = get_irn_dbg_info(n);
3372 ir_graph *irg = current_ir_graph;
3373 ir_node *block = get_nodes_block(n);
3374 ir_mode *mode = get_irn_mode(n);
3375 n = new_rd_Eor(dbg, irg, block, not_op, cnst, mode);
3377 } else if (is_Const_all_one(b)) { /* x ^ 1...1 -> ~1 */
3378 n = new_r_Not(current_ir_graph, get_nodes_block(n), a, mode);
3379 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3382 n = transform_bitwise_distributive(n, transform_node_Eor);
3386 } /* transform_node_Eor */
3391 static ir_node *transform_node_Not(ir_node *n) {
3392 ir_node *c, *oldn = n;
3393 ir_node *a = get_Not_op(n);
3394 ir_mode *mode = get_irn_mode(n);
3396 HANDLE_UNOP_PHI(tarval_not,a,c);
3398 /* check for a boolean Not */
3399 if (mode == mode_b &&
3401 is_Cmp(get_Proj_pred(a))) {
3402 /* We negate a Cmp. The Cmp has the negated result anyways! */
3403 n = new_r_Proj(current_ir_graph, get_irn_n(n, -1), get_Proj_pred(a),
3404 mode_b, get_negated_pnc(get_Proj_proj(a), mode_b));
3405 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_CMP);
3409 ir_node *eor_b = get_Eor_right(a);
3410 if (is_Const(eor_b)) { /* ~(x ^ const) -> x ^ ~const */
3411 ir_node *cnst = new_Const(mode, tarval_not(get_Const_tarval(eor_b)));
3412 ir_node *eor_a = get_Eor_left(a);
3413 dbg_info *dbg = get_irn_dbg_info(n);
3414 ir_graph *irg = current_ir_graph;
3415 ir_node *block = get_nodes_block(n);
3416 ir_mode *mode = get_irn_mode(n);
3417 n = new_rd_Eor(dbg, irg, block, eor_a, cnst, mode);
3421 if (get_mode_arithmetic(mode) == irma_twos_complement) {
3422 if (is_Minus(a)) { /* ~-x -> x + -1 */
3423 dbg_info *dbg = get_irn_dbg_info(n);
3424 ir_graph *irg = current_ir_graph;
3425 ir_node *block = get_nodes_block(n);
3426 ir_node *add_l = get_Minus_op(a);
3427 ir_node *add_r = new_rd_Const(dbg, irg, block, mode, get_mode_minus_one(mode));
3428 n = new_rd_Add(dbg, irg, block, add_l, add_r, mode);
3429 } else if (is_Add(a)) {
3430 ir_node *add_r = get_Add_right(a);
3431 if (is_Const(add_r) && is_Const_all_one(add_r)) {
3432 /* ~(x + -1) = -x */
3433 ir_node *op = get_Add_left(a);
3434 ir_node *blk = get_irn_n(n, -1);
3435 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, blk, op, get_irn_mode(n));
3436 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_MINUS_1);
3441 } /* transform_node_Not */
3444 * Transform a Minus.
3448 * -(a >>u (size-1)) = a >>s (size-1)
3449 * -(a >>s (size-1)) = a >>u (size-1)
3450 * -(a * const) -> a * -const
3452 static ir_node *transform_node_Minus(ir_node *n) {
3453 ir_node *c, *oldn = n;
3454 ir_node *a = get_Minus_op(n);
3457 HANDLE_UNOP_PHI(tarval_neg,a,c);
3459 mode = get_irn_mode(a);
3460 if (get_mode_arithmetic(mode) == irma_twos_complement) {
3461 /* the following rules are only to twos-complement */
3464 ir_node *op = get_Not_op(a);
3465 tarval *tv = get_mode_one(mode);
3466 ir_node *blk = get_irn_n(n, -1);
3467 ir_node *c = new_r_Const(current_ir_graph, blk, mode, tv);
3468 n = new_rd_Add(get_irn_dbg_info(n), current_ir_graph, blk, op, c, mode);
3469 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_MINUS_NOT);
3473 ir_node *c = get_Shr_right(a);
3476 tarval *tv = get_Const_tarval(c);
3478 if (tarval_is_long(tv) && get_tarval_long(tv) == (int) get_mode_size_bits(mode) - 1) {
3479 /* -(a >>u (size-1)) = a >>s (size-1) */
3480 ir_node *v = get_Shr_left(a);
3482 n = new_rd_Shrs(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1), v, c, mode);
3483 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_PREDICATE);
3489 ir_node *c = get_Shrs_right(a);
3492 tarval *tv = get_Const_tarval(c);
3494 if (tarval_is_long(tv) && get_tarval_long(tv) == (int) get_mode_size_bits(mode) - 1) {
3495 /* -(a >>s (size-1)) = a >>u (size-1) */
3496 ir_node *v = get_Shrs_left(a);
3498 n = new_rd_Shr(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1), v, c, mode);
3499 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_PREDICATE);
3506 /* - (a-b) = b - a */
3507 ir_node *la = get_Sub_left(a);
3508 ir_node *ra = get_Sub_right(a);
3509 ir_node *blk = get_irn_n(n, -1);
3511 n = new_rd_Sub(get_irn_dbg_info(n), current_ir_graph, blk, ra, la, mode);
3512 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_MINUS_SUB);
3516 if (is_Mul(a)) { /* -(a * const) -> a * -const */
3517 ir_node *mul_l = get_Mul_left(a);
3518 ir_node *mul_r = get_Mul_right(a);
3519 if (is_Const(mul_r)) {
3520 tarval *tv = tarval_neg(get_Const_tarval(mul_r));
3521 if(tv != tarval_bad) {
3522 ir_node *cnst = new_Const(mode, tv);
3523 dbg_info *dbg = get_irn_dbg_info(a);
3524 ir_graph *irg = current_ir_graph;
3525 ir_node *block = get_nodes_block(a);
3526 n = new_rd_Mul(dbg, irg, block, mul_l, cnst, mode);
3527 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_MINUS_MUL_C);
3534 } /* transform_node_Minus */
3537 * Transform a Cast_type(Const) into a new Const_type
3539 static ir_node *transform_node_Cast(ir_node *n) {
3541 ir_node *pred = get_Cast_op(n);
3542 ir_type *tp = get_irn_type(n);
3544 if (is_Const(pred) && get_Const_type(pred) != tp) {
3545 n = new_rd_Const_type(NULL, current_ir_graph, get_irn_n(pred, -1), get_irn_mode(pred),
3546 get_Const_tarval(pred), tp);
3547 DBG_OPT_CSTEVAL(oldn, n);
3548 } else if (is_SymConst(pred) && get_SymConst_value_type(pred) != tp) {
3549 n = new_rd_SymConst_type(NULL, current_ir_graph, get_irn_n(pred, -1), get_irn_mode(pred),
3550 get_SymConst_symbol(pred), get_SymConst_kind(pred), tp);
3551 DBG_OPT_CSTEVAL(oldn, n);
3555 } /* transform_node_Cast */
3558 * Transform a Proj(Div) with a non-zero value.
3559 * Removes the exceptions and routes the memory to the NoMem node.
3561 static ir_node *transform_node_Proj_Div(ir_node *proj) {
3562 ir_node *div = get_Proj_pred(proj);
3563 ir_node *b = get_Div_right(div);
3564 ir_node *confirm, *res, *new_mem;
3567 if (value_not_zero(b, &confirm)) {
3568 /* div(x, y) && y != 0 */
3569 if (confirm == NULL) {
3570 /* we are sure we have a Const != 0 */
3571 new_mem = get_Div_mem(div);
3572 if (is_Pin(new_mem))
3573 new_mem = get_Pin_op(new_mem);
3574 set_Div_mem(div, new_mem);
3575 set_irn_pinned(div, op_pin_state_floats);
3578 proj_nr = get_Proj_proj(proj);
3580 case pn_Div_X_regular:
3581 return new_r_Jmp(current_ir_graph, get_irn_n(div, -1));
3583 case pn_Div_X_except:
3584 /* we found an exception handler, remove it */
3585 DBG_OPT_EXC_REM(proj);
3589 res = get_Div_mem(div);
3590 new_mem = get_irg_no_mem(current_ir_graph);
3593 /* This node can only float up to the Confirm block */
3594 new_mem = new_r_Pin(current_ir_graph, get_nodes_block(confirm), new_mem);
3596 set_irn_pinned(div, op_pin_state_floats);
3597 /* this is a Div without exception, we can remove the memory edge */
3598 set_Div_mem(div, new_mem);
3603 } /* transform_node_Proj_Div */
3606 * Transform a Proj(Mod) with a non-zero value.
3607 * Removes the exceptions and routes the memory to the NoMem node.
3609 static ir_node *transform_node_Proj_Mod(ir_node *proj) {
3610 ir_node *mod = get_Proj_pred(proj);
3611 ir_node *b = get_Mod_right(mod);
3612 ir_node *confirm, *res, *new_mem;
3615 if (value_not_zero(b, &confirm)) {
3616 /* mod(x, y) && y != 0 */
3617 proj_nr = get_Proj_proj(proj);
3619 if (confirm == NULL) {
3620 /* we are sure we have a Const != 0 */
3621 new_mem = get_Mod_mem(mod);
3622 if (is_Pin(new_mem))
3623 new_mem = get_Pin_op(new_mem);
3624 set_Mod_mem(mod, new_mem);
3625 set_irn_pinned(mod, op_pin_state_floats);
3630 case pn_Mod_X_regular:
3631 return new_r_Jmp(current_ir_graph, get_irn_n(mod, -1));
3633 case pn_Mod_X_except:
3634 /* we found an exception handler, remove it */
3635 DBG_OPT_EXC_REM(proj);
3639 res = get_Mod_mem(mod);
3640 new_mem = get_irg_no_mem(current_ir_graph);
3643 /* This node can only float up to the Confirm block */
3644 new_mem = new_r_Pin(current_ir_graph, get_nodes_block(confirm), new_mem);
3646 /* this is a Mod without exception, we can remove the memory edge */
3647 set_Mod_mem(mod, new_mem);
3650 if (get_Mod_left(mod) == b) {
3651 /* a % a = 0 if a != 0 */
3652 ir_mode *mode = get_irn_mode(proj);
3653 ir_node *res = new_Const(mode, get_mode_null(mode));
3655 DBG_OPT_CSTEVAL(mod, res);
3661 } /* transform_node_Proj_Mod */
3664 * Transform a Proj(DivMod) with a non-zero value.
3665 * Removes the exceptions and routes the memory to the NoMem node.
3667 static ir_node *transform_node_Proj_DivMod(ir_node *proj) {
3668 ir_node *divmod = get_Proj_pred(proj);
3669 ir_node *b = get_DivMod_right(divmod);
3670 ir_node *confirm, *res, *new_mem;
3673 if (value_not_zero(b, &confirm)) {
3674 /* DivMod(x, y) && y != 0 */
3675 proj_nr = get_Proj_proj(proj);
3677 if (confirm == NULL) {
3678 /* we are sure we have a Const != 0 */
3679 new_mem = get_DivMod_mem(divmod);
3680 if (is_Pin(new_mem))
3681 new_mem = get_Pin_op(new_mem);
3682 set_DivMod_mem(divmod, new_mem);
3683 set_irn_pinned(divmod, op_pin_state_floats);
3688 case pn_DivMod_X_regular:
3689 return new_r_Jmp(current_ir_graph, get_irn_n(divmod, -1));
3691 case pn_DivMod_X_except:
3692 /* we found an exception handler, remove it */
3693 DBG_OPT_EXC_REM(proj);
3697 res = get_DivMod_mem(divmod);
3698 new_mem = get_irg_no_mem(current_ir_graph);
3701 /* This node can only float up to the Confirm block */
3702 new_mem = new_r_Pin(current_ir_graph, get_nodes_block(confirm), new_mem);
3704 /* this is a DivMod without exception, we can remove the memory edge */
3705 set_DivMod_mem(divmod, new_mem);
3708 case pn_DivMod_res_mod:
3709 if (get_DivMod_left(divmod) == b) {
3710 /* a % a = 0 if a != 0 */
3711 ir_mode *mode = get_irn_mode(proj);
3712 ir_node *res = new_Const(mode, get_mode_null(mode));
3714 DBG_OPT_CSTEVAL(divmod, res);
3720 } /* transform_node_Proj_DivMod */
3723 * Optimizes jump tables (CondIs or CondIu) by removing all impossible cases.
3725 static ir_node *transform_node_Proj_Cond(ir_node *proj) {
3726 if (get_opt_unreachable_code()) {
3727 ir_node *n = get_Proj_pred(proj);
3728 ir_node *b = get_Cond_selector(n);
3730 if (mode_is_int(get_irn_mode(b))) {
3731 tarval *tb = value_of(b);
3733 if (tb != tarval_bad) {
3734 /* we have a constant switch */
3735 long num = get_Proj_proj(proj);
3737 if (num != get_Cond_defaultProj(n)) { /* we cannot optimize default Proj's yet */
3738 if (get_tarval_long(tb) == num) {
3739 /* Do NOT create a jump here, or we will have 2 control flow ops
3740 * in a block. This case is optimized away in optimize_cf(). */
3743 /* this case will NEVER be taken, kill it */
3751 } /* transform_node_Proj_Cond */
3754 * Create a 0 constant of given mode.
3756 static ir_node *create_zero_const(ir_mode *mode) {
3757 tarval *tv = get_mode_null(mode);
3758 ir_node *cnst = new_Const(mode, tv);
3763 /* the order of the values is important! */
3764 typedef enum const_class {
3770 static const_class classify_const(const ir_node* n)
3772 if (is_Const(n)) return const_const;
3773 if (is_irn_constlike(n)) return const_like;
3778 * Determines whether r is more constlike or has a larger index (in that order)
3781 static int operands_are_normalized(const ir_node *l, const ir_node *r)
3783 const const_class l_order = classify_const(l);
3784 const const_class r_order = classify_const(r);
3786 l_order > r_order ||
3787 (l_order == r_order && get_irn_idx(l) <= get_irn_idx(r));
3791 * Normalizes and optimizes Cmp nodes.
3793 static ir_node *transform_node_Proj_Cmp(ir_node *proj) {
3794 ir_node *n = get_Proj_pred(proj);
3795 ir_node *left = get_Cmp_left(n);
3796 ir_node *right = get_Cmp_right(n);
3799 ir_mode *mode = NULL;
3800 long proj_nr = get_Proj_proj(proj);
3802 /* we can evaluate some cases directly */
3805 return new_Const(mode_b, get_tarval_b_false());
3807 return new_Const(mode_b, get_tarval_b_true());
3809 if (!mode_is_float(get_irn_mode(left)))
3810 return new_Const(mode_b, get_tarval_b_true());
3816 /* remove Casts of both sides */
3817 left = skip_Cast(left);
3818 right = skip_Cast(right);
3820 /* Remove unnecessary conversions */
3821 /* TODO handle constants */
3822 if (is_Conv(left) && is_Conv(right)) {
3823 ir_mode *mode = get_irn_mode(left);
3824 ir_node *op_left = get_Conv_op(left);
3825 ir_node *op_right = get_Conv_op(right);
3826 ir_mode *mode_left = get_irn_mode(op_left);
3827 ir_mode *mode_right = get_irn_mode(op_right);
3829 if (smaller_mode(mode_left, mode) && smaller_mode(mode_right, mode)
3830 && mode_left != mode_b && mode_right != mode_b) {
3831 ir_graph *irg = current_ir_graph;
3832 ir_node *block = get_nodes_block(n);
3834 if (mode_left == mode_right) {
3838 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV_CONV);
3839 } else if (smaller_mode(mode_left, mode_right)) {
3840 left = new_r_Conv(irg, block, op_left, mode_right);
3843 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV);
3844 } else if (smaller_mode(mode_right, mode_left)) {
3846 right = new_r_Conv(irg, block, op_right, mode_left);
3848 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV);
3853 /* remove operation on both sides if possible */
3854 if (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) {
3856 * The following operations are NOT safe for floating point operations, for instance
3857 * 1.0 + inf == 2.0 + inf, =/=> x == y
3859 if (mode_is_int(get_irn_mode(left))) {
3860 unsigned lop = get_irn_opcode(left);
3862 if (lop == get_irn_opcode(right)) {
3863 ir_node *ll, *lr, *rl, *rr;
3865 /* same operation on both sides, try to remove */
3869 /* ~a CMP ~b => a CMP b, -a CMP -b ==> a CMP b */
3870 left = get_unop_op(left);
3871 right = get_unop_op(right);
3873 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3876 ll = get_Add_left(left);
3877 lr = get_Add_right(left);
3878 rl = get_Add_left(right);
3879 rr = get_Add_right(right);
3882 /* X + a CMP X + b ==> a CMP b */
3886 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3887 } else if (ll == rr) {
3888 /* X + a CMP b + X ==> a CMP b */
3892 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3893 } else if (lr == rl) {
3894 /* a + X CMP X + b ==> a CMP b */
3898 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3899 } else if (lr == rr) {
3900 /* a + X CMP b + X ==> a CMP b */
3904 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3908 ll = get_Sub_left(left);
3909 lr = get_Sub_right(left);
3910 rl = get_Sub_left(right);
3911 rr = get_Sub_right(right);
3914 /* X - a CMP X - b ==> a CMP b */
3918 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3919 } else if (lr == rr) {
3920 /* a - X CMP b - X ==> a CMP b */
3924 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3928 if (get_Rotl_right(left) == get_Rotl_right(right)) {
3929 /* a ROTL X CMP b ROTL X ==> a CMP b */
3930 left = get_Rotl_left(left);
3931 right = get_Rotl_left(right);
3933 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3941 /* X+A == A, A+X == A, A-X == A -> X == 0 */
3942 if (is_Add(left) || is_Sub(left)) {
3943 ir_node *ll = get_binop_left(left);
3944 ir_node *lr = get_binop_right(left);
3946 if (lr == right && is_Add(left)) {
3953 right = create_zero_const(get_irn_mode(left));
3955 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3958 if (is_Add(right) || is_Sub(right)) {
3959 ir_node *rl = get_binop_left(right);
3960 ir_node *rr = get_binop_right(right);
3962 if (rr == left && is_Add(right)) {
3969 right = create_zero_const(get_irn_mode(left));
3971 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3974 } /* mode_is_int(...) */
3975 } /* proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg */
3977 /* replace mode_b compares with ands/ors */
3978 if (get_irn_mode(left) == mode_b) {
3979 ir_graph *irg = current_ir_graph;
3980 ir_node *block = get_nodes_block(n);
3984 case pn_Cmp_Le: bres = new_r_Or( irg, block, new_r_Not(irg, block, left, mode_b), right, mode_b); break;
3985 case pn_Cmp_Lt: bres = new_r_And(irg, block, new_r_Not(irg, block, left, mode_b), right, mode_b); break;
3986 case pn_Cmp_Ge: bres = new_r_Or( irg, block, left, new_r_Not(irg, block, right, mode_b), mode_b); break;
3987 case pn_Cmp_Gt: bres = new_r_And(irg, block, left, new_r_Not(irg, block, right, mode_b), mode_b); break;
3988 case pn_Cmp_Lg: bres = new_r_Eor(irg, block, left, right, mode_b); break;
3989 case pn_Cmp_Eq: bres = new_r_Not(irg, block, new_r_Eor(irg, block, left, right, mode_b), mode_b); break;
3990 default: bres = NULL;
3993 DBG_OPT_ALGSIM0(n, bres, FS_OPT_CMP_TO_BOOL);
3999 * First step: normalize the compare op
4000 * by placing the constant on the right side
4001 * or moving the lower address node to the left.
4003 if (!operands_are_normalized(left, right)) {
4009 proj_nr = get_inversed_pnc(proj_nr);
4014 * Second step: Try to reduce the magnitude
4015 * of a constant. This may help to generate better code
4016 * later and may help to normalize more compares.
4017 * Of course this is only possible for integer values.
4019 if (is_Const(right)) {
4020 mode = get_irn_mode(right);
4021 tv = get_Const_tarval(right);
4023 /* TODO extend to arbitrary constants */
4024 if (is_Conv(left) && tarval_is_null(tv)) {
4025 ir_node *op = get_Conv_op(left);
4026 ir_mode *op_mode = get_irn_mode(op);
4029 * UpConv(x) REL 0 ==> x REL 0
4031 if (get_mode_size_bits(mode) > get_mode_size_bits(op_mode) &&
4032 ((proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) ||
4033 mode_is_signed(mode) || !mode_is_signed(op_mode))) {
4034 tv = get_mode_null(op_mode);
4038 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV);
4042 if (tv != tarval_bad) {
4043 /* the following optimization is possible on modes without Overflow
4044 * on Unary Minus or on == and !=:
4045 * -a CMP c ==> a swap(CMP) -c
4047 * Beware: for two-complement Overflow may occur, so only == and != can
4048 * be optimized, see this:
4049 * -MININT < 0 =/=> MININT > 0 !!!
4051 if (is_Minus(left) &&
4052 (!mode_overflow_on_unary_Minus(mode) ||
4053 (mode_is_int(mode) && (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg)))) {
4054 tv = tarval_neg(tv);
4056 if (tv != tarval_bad) {
4057 left = get_Minus_op(left);
4058 proj_nr = get_inversed_pnc(proj_nr);
4060 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4062 } else if (is_Not(left) && (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg)) {
4063 /* Not(a) ==/!= c ==> a ==/!= Not(c) */
4064 tv = tarval_not(tv);
4066 if (tv != tarval_bad) {
4067 left = get_Not_op(left);
4069 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4073 /* for integer modes, we have more */
4074 if (mode_is_int(mode)) {
4075 /* Ne includes Unordered which is not possible on integers.
4076 * However, frontends often use this wrong, so fix it here */
4077 if (proj_nr & pn_Cmp_Uo) {
4078 proj_nr &= ~pn_Cmp_Uo;
4079 set_Proj_proj(proj, proj_nr);
4082 /* c > 0 : a < c ==> a <= (c-1) a >= c ==> a > (c-1) */
4083 if ((proj_nr == pn_Cmp_Lt || proj_nr == pn_Cmp_Ge) &&
4084 tarval_cmp(tv, get_mode_null(mode)) == pn_Cmp_Gt) {
4085 tv = tarval_sub(tv, get_mode_one(mode), NULL);
4087 if (tv != tarval_bad) {
4088 proj_nr ^= pn_Cmp_Eq;
4090 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CNST_MAGN);
4093 /* c < 0 : a > c ==> a >= (c+1) a <= c ==> a < (c+1) */
4094 else if ((proj_nr == pn_Cmp_Gt || proj_nr == pn_Cmp_Le) &&
4095 tarval_cmp(tv, get_mode_null(mode)) == pn_Cmp_Lt) {
4096 tv = tarval_add(tv, get_mode_one(mode));
4098 if (tv != tarval_bad) {
4099 proj_nr ^= pn_Cmp_Eq;
4101 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CNST_MAGN);
4105 /* the following reassociations work only for == and != */
4106 if (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) {
4108 #if 0 /* Might be not that good in general */
4109 /* a-b == 0 ==> a == b, a-b != 0 ==> a != b */
4110 if (tarval_is_null(tv) && is_Sub(left)) {
4111 right = get_Sub_right(left);
4112 left = get_Sub_left(left);
4114 tv = value_of(right);
4116 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4120 if (tv != tarval_bad) {
4121 /* a-c1 == c2 ==> a == c2+c1, a-c1 != c2 ==> a != c2+c1 */
4123 ir_node *c1 = get_Sub_right(left);
4124 tarval *tv2 = value_of(c1);
4126 if (tv2 != tarval_bad) {
4127 tv2 = tarval_add(tv, value_of(c1));
4129 if (tv2 != tarval_bad) {
4130 left = get_Sub_left(left);
4133 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4137 /* a+c1 == c2 ==> a == c2-c1, a+c1 != c2 ==> a != c2-c1 */
4138 else if (is_Add(left)) {
4139 ir_node *a_l = get_Add_left(left);
4140 ir_node *a_r = get_Add_right(left);
4144 if (is_Const(a_l)) {
4146 tv2 = value_of(a_l);
4149 tv2 = value_of(a_r);
4152 if (tv2 != tarval_bad) {
4153 tv2 = tarval_sub(tv, tv2, NULL);
4155 if (tv2 != tarval_bad) {
4159 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4163 /* -a == c ==> a == -c, -a != c ==> a != -c */
4164 else if (is_Minus(left)) {
4165 tarval *tv2 = tarval_sub(get_mode_null(mode), tv, NULL);
4167 if (tv2 != tarval_bad) {
4168 left = get_Minus_op(left);
4171 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4176 /* the following reassociations work only for <= */
4177 else if (proj_nr == pn_Cmp_Le || proj_nr == pn_Cmp_Lt) {
4178 if (tv != tarval_bad) {
4179 /* c >= 0 : Abs(a) <= c ==> (unsigned)(a + c) <= 2*c */
4180 if (get_irn_op(left) == op_Abs) { // TODO something is missing here
4186 if (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) {
4187 switch (get_irn_opcode(left)) {
4191 c1 = get_And_right(left);
4194 * And(x, C1) == C2 ==> FALSE if C2 & C1 != C2
4195 * And(x, C1) != C2 ==> TRUE if C2 & C1 != C2
4197 tarval *mask = tarval_and(get_Const_tarval(c1), tv);
4199 /* TODO: move to constant evaluation */
4200 tv = proj_nr == pn_Cmp_Eq ? get_tarval_b_false() : get_tarval_b_true();
4201 c1 = new_Const(mode_b, tv);
4202 DBG_OPT_CSTEVAL(proj, c1);
4206 if (tarval_is_single_bit(tv)) {
4208 * optimization for AND:
4210 * And(x, C) == C ==> And(x, C) != 0
4211 * And(x, C) != C ==> And(X, C) == 0
4213 * if C is a single Bit constant.
4216 /* check for Constant's match. We have check hare the tarvals,
4217 because our const might be changed */
4218 if (get_Const_tarval(c1) == tv) {
4219 /* fine: do the transformation */
4220 tv = get_mode_null(get_tarval_mode(tv));
4221 proj_nr ^= pn_Cmp_Leg;
4223 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CNST_MAGN);
4229 c1 = get_Or_right(left);
4230 if (is_Const(c1) && tarval_is_null(tv)) {
4232 * Or(x, C) == 0 && C != 0 ==> FALSE
4233 * Or(x, C) != 0 && C != 0 ==> TRUE
4235 if (! tarval_is_null(get_Const_tarval(c1))) {
4236 /* TODO: move to constant evaluation */
4237 tv = proj_nr == pn_Cmp_Eq ? get_tarval_b_false() : get_tarval_b_true();
4238 c1 = new_Const(mode_b, tv);
4239 DBG_OPT_CSTEVAL(proj, c1);
4246 * optimize x << c1 == c into x & (-1 >>u c1) == c >> c1 if c & (-1 << c1) == c
4248 * optimize x << c1 != c into x & (-1 >>u c1) != c >> c1 if c & (-1 << c1) == c
4251 c1 = get_Shl_right(left);
4253 tarval *tv1 = get_Const_tarval(c1);
4254 ir_mode *mode = get_irn_mode(left);
4255 tarval *minus1 = get_mode_all_one(mode);
4256 tarval *amask = tarval_shr(minus1, tv1);
4257 tarval *cmask = tarval_shl(minus1, tv1);
4260 if (tarval_and(tv, cmask) != tv) {
4261 /* condition not met */
4262 tv = proj_nr == pn_Cmp_Eq ? get_tarval_b_false() : get_tarval_b_true();
4263 c1 = new_Const(mode_b, tv);
4264 DBG_OPT_CSTEVAL(proj, c1);
4267 sl = get_Shl_left(left);
4268 blk = get_nodes_block(n);
4269 left = new_rd_And(get_irn_dbg_info(left), current_ir_graph, blk, sl, new_Const(mode, amask), mode);
4270 tv = tarval_shr(tv, tv1);
4272 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_SHF_TO_AND);
4277 * optimize x >>u c1 == c into x & (-1 << c1) == c << c1 if c & (-1 >>u c1) == c
4279 * optimize x >>u c1 != c into x & (-1 << c1) != c << c1 if c & (-1 >>u c1) == c
4282 c1 = get_Shr_right(left);
4284 tarval *tv1 = get_Const_tarval(c1);
4285 ir_mode *mode = get_irn_mode(left);
4286 tarval *minus1 = get_mode_all_one(mode);
4287 tarval *amask = tarval_shl(minus1, tv1);
4288 tarval *cmask = tarval_shr(minus1, tv1);
4291 if (tarval_and(tv, cmask) != tv) {
4292 /* condition not met */
4293 tv = proj_nr == pn_Cmp_Eq ? get_tarval_b_false() : get_tarval_b_true();
4294 c1 = new_Const(mode_b, tv);
4295 DBG_OPT_CSTEVAL(proj, c1);
4298 sl = get_Shr_left(left);
4299 blk = get_nodes_block(n);
4300 left = new_rd_And(get_irn_dbg_info(left), current_ir_graph, blk, sl, new_Const(mode, amask), mode);
4301 tv = tarval_shl(tv, tv1);
4303 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_SHF_TO_AND);
4308 * optimize x >>s c1 == c into x & (-1 << c1) == c << c1 if (c >>s (BITS - c1)) \in {0,-1}
4310 * optimize x >>s c1 != c into x & (-1 << c1) != c << c1 if (c >>s (BITS - c1)) \in {0,-1}
4313 c1 = get_Shrs_right(left);
4315 tarval *tv1 = get_Const_tarval(c1);
4316 ir_mode *mode = get_irn_mode(left);
4317 tarval *minus1 = get_mode_all_one(mode);
4318 tarval *amask = tarval_shl(minus1, tv1);
4319 tarval *cond = new_tarval_from_long(get_mode_size_bits(mode), get_tarval_mode(tv1));
4322 cond = tarval_sub(cond, tv1, NULL);
4323 cond = tarval_shrs(tv, cond);
4325 if (!tarval_is_all_one(cond) && !tarval_is_null(cond)) {
4326 /* condition not met */
4327 tv = proj_nr == pn_Cmp_Eq ? get_tarval_b_false() : get_tarval_b_true();
4328 c1 = new_Const(mode_b, tv);
4329 DBG_OPT_CSTEVAL(proj, c1);
4332 sl = get_Shrs_left(left);
4333 blk = get_nodes_block(n);
4334 left = new_rd_And(get_irn_dbg_info(left), current_ir_graph, blk, sl, new_Const(mode, amask), mode);
4335 tv = tarval_shl(tv, tv1);
4337 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_SHF_TO_AND);
4342 } /* tarval != bad */
4345 if (changed & 2) /* need a new Const */
4346 right = new_Const(mode, tv);
4348 if ((proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) && is_Const(right) && is_Const_null(right) && is_Proj(left)) {
4349 ir_node *op = get_Proj_pred(left);
4351 if ((is_Mod(op) && get_Proj_proj(left) == pn_Mod_res) ||
4352 (is_DivMod(op) && get_Proj_proj(left) == pn_DivMod_res_mod)) {
4353 ir_node *c = get_binop_right(op);
4356 tarval *tv = get_Const_tarval(c);
4358 if (tarval_is_single_bit(tv)) {
4359 /* special case: (x % 2^n) CMP 0 ==> x & (2^n-1) CMP 0 */
4360 ir_node *v = get_binop_left(op);
4361 ir_node *blk = get_irn_n(op, -1);
4362 ir_mode *mode = get_irn_mode(v);
4364 tv = tarval_sub(tv, get_mode_one(mode), NULL);
4365 left = new_rd_And(get_irn_dbg_info(op), current_ir_graph, blk, v, new_Const(mode, tv), mode);
4367 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_MOD_TO_AND);
4374 ir_node *block = get_irn_n(n, -1); /* Beware of get_nodes_Block() */
4376 /* create a new compare */
4377 n = new_rd_Cmp(get_irn_dbg_info(n), current_ir_graph, block, left, right);
4378 proj = new_rd_Proj(get_irn_dbg_info(proj), current_ir_graph, block, n, get_irn_mode(proj), proj_nr);
4382 } /* transform_node_Proj_Cmp */
4385 * Does all optimizations on nodes that must be done on it's Proj's
4386 * because of creating new nodes.
4388 static ir_node *transform_node_Proj(ir_node *proj) {
4389 ir_node *n = get_Proj_pred(proj);
4391 switch (get_irn_opcode(n)) {
4393 return transform_node_Proj_Div(proj);
4396 return transform_node_Proj_Mod(proj);
4399 return transform_node_Proj_DivMod(proj);
4402 return transform_node_Proj_Cond(proj);
4405 return transform_node_Proj_Cmp(proj);
4408 /* should not happen, but if it does will be optimized away */
4409 return equivalent_node_Proj(proj);
4415 } /* transform_node_Proj */
4418 * Move Confirms down through Phi nodes.
4420 static ir_node *transform_node_Phi(ir_node *phi) {
4422 ir_mode *mode = get_irn_mode(phi);
4424 if (mode_is_reference(mode)) {
4425 n = get_irn_arity(phi);
4427 /* Beware of Phi0 */
4429 ir_node *pred = get_irn_n(phi, 0);
4430 ir_node *bound, *new_Phi, *block, **in;
4433 if (! is_Confirm(pred))
4436 bound = get_Confirm_bound(pred);
4437 pnc = get_Confirm_cmp(pred);
4439 NEW_ARR_A(ir_node *, in, n);
4440 in[0] = get_Confirm_value(pred);
4442 for (i = 1; i < n; ++i) {
4443 pred = get_irn_n(phi, i);
4445 if (! is_Confirm(pred) ||
4446 get_Confirm_bound(pred) != bound ||
4447 get_Confirm_cmp(pred) != pnc)
4449 in[i] = get_Confirm_value(pred);
4451 /* move the Confirm nodes "behind" the Phi */
4452 block = get_irn_n(phi, -1);
4453 new_Phi = new_r_Phi(current_ir_graph, block, n, in, get_irn_mode(phi));
4454 return new_r_Confirm(current_ir_graph, block, new_Phi, bound, pnc);
4458 } /* transform_node_Phi */
4461 * Returns the operands of a commutative bin-op, if one operand is
4462 * a const, it is returned as the second one.
4464 static void get_comm_Binop_Ops(ir_node *binop, ir_node **a, ir_node **c) {
4465 ir_node *op_a = get_binop_left(binop);
4466 ir_node *op_b = get_binop_right(binop);
4468 assert(is_op_commutative(get_irn_op(binop)));
4470 if (is_Const(op_a)) {
4477 } /* get_comm_Binop_Ops */
4480 * Optimize a Or(And(Or(And(v,c4),c3),c2),c1) pattern if possible.
4481 * Such pattern may arise in bitfield stores.
4483 * value c4 value c4 & c2
4484 * AND c3 AND c1 | c3
4491 * AND c1 ===> OR if (c1 | c2) == 0x111..11
4494 static ir_node *transform_node_Or_bf_store(ir_node *or) {
4497 ir_node *and_l, *c3;
4498 ir_node *value, *c4;
4499 ir_node *new_and, *new_const, *block;
4500 ir_mode *mode = get_irn_mode(or);
4502 tarval *tv1, *tv2, *tv3, *tv4, *tv, *n_tv4, *n_tv2;
4505 get_comm_Binop_Ops(or, &and, &c1);
4506 if (!is_Const(c1) || !is_And(and))
4509 get_comm_Binop_Ops(and, &or_l, &c2);
4513 tv1 = get_Const_tarval(c1);
4514 tv2 = get_Const_tarval(c2);
4516 tv = tarval_or(tv1, tv2);
4517 if (tarval_is_all_one(tv)) {
4518 /* the AND does NOT clear a bit with isn't set by the OR */
4519 set_Or_left(or, or_l);
4520 set_Or_right(or, c1);
4522 /* check for more */
4529 get_comm_Binop_Ops(or_l, &and_l, &c3);
4530 if (!is_Const(c3) || !is_And(and_l))
4533 get_comm_Binop_Ops(and_l, &value, &c4);
4537 /* ok, found the pattern, check for conditions */
4538 assert(mode == get_irn_mode(and));
4539 assert(mode == get_irn_mode(or_l));
4540 assert(mode == get_irn_mode(and_l));
4542 tv3 = get_Const_tarval(c3);
4543 tv4 = get_Const_tarval(c4);
4545 tv = tarval_or(tv4, tv2);
4546 if (!tarval_is_all_one(tv)) {
4547 /* have at least one 0 at the same bit position */
4551 n_tv4 = tarval_not(tv4);
4552 if (tv3 != tarval_and(tv3, n_tv4)) {
4553 /* bit in the or_mask is outside the and_mask */
4557 n_tv2 = tarval_not(tv2);
4558 if (tv1 != tarval_and(tv1, n_tv2)) {
4559 /* bit in the or_mask is outside the and_mask */
4563 /* ok, all conditions met */
4564 block = get_irn_n(or, -1);
4566 new_and = new_r_And(current_ir_graph, block,
4567 value, new_r_Const(current_ir_graph, block, mode, tarval_and(tv4, tv2)), mode);
4569 new_const = new_r_Const(current_ir_graph, block, mode, tarval_or(tv3, tv1));
4571 set_Or_left(or, new_and);
4572 set_Or_right(or, new_const);
4574 /* check for more */
4576 } /* transform_node_Or_bf_store */
4579 * Optimize an Or(shl(x, c), shr(x, bits - c)) into a Rotl
4581 static ir_node *transform_node_Or_Rotl(ir_node *or) {
4582 ir_mode *mode = get_irn_mode(or);
4583 ir_node *shl, *shr, *block;
4584 ir_node *irn, *x, *c1, *c2, *v, *sub, *n, *rotval;
4587 if (! mode_is_int(mode))
4590 shl = get_binop_left(or);
4591 shr = get_binop_right(or);
4600 } else if (!is_Shl(shl)) {
4602 } else if (!is_Shr(shr)) {
4605 x = get_Shl_left(shl);
4606 if (x != get_Shr_left(shr))
4609 c1 = get_Shl_right(shl);
4610 c2 = get_Shr_right(shr);
4611 if (is_Const(c1) && is_Const(c2)) {
4612 tv1 = get_Const_tarval(c1);
4613 if (! tarval_is_long(tv1))
4616 tv2 = get_Const_tarval(c2);
4617 if (! tarval_is_long(tv2))
4620 if (get_tarval_long(tv1) + get_tarval_long(tv2)
4621 != (int) get_mode_size_bits(mode))
4624 /* yet, condition met */
4625 block = get_nodes_block(or);
4627 n = new_r_Rotl(current_ir_graph, block, x, c1, mode);
4629 DBG_OPT_ALGSIM1(or, shl, shr, n, FS_OPT_OR_SHFT_TO_ROTL);
4636 rotval = sub; /* a Rot right is not supported, so use a rot left */
4637 } else if (is_Sub(c2)) {
4643 if (get_Sub_right(sub) != v)
4646 c1 = get_Sub_left(sub);
4650 tv1 = get_Const_tarval(c1);
4651 if (! tarval_is_long(tv1))
4654 if (get_tarval_long(tv1) != (int) get_mode_size_bits(mode))
4657 /* yet, condition met */
4658 block = get_nodes_block(or);
4660 n = new_r_Rotl(current_ir_graph, block, x, rotval, mode);
4662 DBG_OPT_ALGSIM0(or, n, FS_OPT_OR_SHFT_TO_ROTL);
4664 } /* transform_node_Or_Rotl */
4669 static ir_node *transform_node_Or(ir_node *n) {
4670 ir_node *c, *oldn = n;
4671 ir_node *a = get_Or_left(n);
4672 ir_node *b = get_Or_right(n);
4675 if (is_Not(a) && is_Not(b)) {
4676 /* ~a | ~b = ~(a&b) */
4677 ir_node *block = get_nodes_block(n);
4679 mode = get_irn_mode(n);
4682 n = new_rd_And(get_irn_dbg_info(n), current_ir_graph, block, a, b, mode);
4683 n = new_rd_Not(get_irn_dbg_info(n), current_ir_graph, block, n, mode);
4684 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_DEMORGAN);
4688 /* we can evaluate 2 Projs of the same Cmp */
4689 if (get_irn_mode(n) == mode_b && is_Proj(a) && is_Proj(b)) {
4690 ir_node *pred_a = get_Proj_pred(a);
4691 ir_node *pred_b = get_Proj_pred(b);
4692 if (pred_a == pred_b) {
4693 dbg_info *dbgi = get_irn_dbg_info(n);
4694 ir_node *block = get_nodes_block(pred_a);
4695 pn_Cmp pn_a = get_Proj_proj(a);
4696 pn_Cmp pn_b = get_Proj_proj(b);
4697 /* yes, we can simply calculate with pncs */
4698 pn_Cmp new_pnc = pn_a | pn_b;
4700 return new_rd_Proj(dbgi, current_ir_graph, block, pred_a, mode_b,
4705 mode = get_irn_mode(n);
4706 HANDLE_BINOP_PHI(tarval_or, a, b, c, mode);
4708 n = transform_node_Or_bf_store(n);
4709 n = transform_node_Or_Rotl(n);
4713 n = transform_bitwise_distributive(n, transform_node_Or);
4716 } /* transform_node_Or */
4720 static ir_node *transform_node(ir_node *n);
4723 * Optimize (a >> c1) >> c2), works for Shr, Shrs, Shl, Rotl.
4725 * Should be moved to reassociation?
4727 static ir_node *transform_node_shift(ir_node *n) {
4728 ir_node *left, *right;
4730 tarval *tv1, *tv2, *res;
4731 ir_node *in[2], *irn, *block;
4733 left = get_binop_left(n);
4735 /* different operations */
4736 if (get_irn_op(left) != get_irn_op(n))
4739 right = get_binop_right(n);
4740 tv1 = value_of(right);
4741 if (tv1 == tarval_bad)
4744 tv2 = value_of(get_binop_right(left));
4745 if (tv2 == tarval_bad)
4748 res = tarval_add(tv1, tv2);
4749 mode = get_irn_mode(n);
4751 /* beware: a simple replacement works only, if res < modulo shift */
4753 int modulo_shf = get_mode_modulo_shift(mode);
4754 assert(modulo_shf >= (int) get_mode_size_bits(mode));
4755 if (modulo_shf > 0) {
4756 tarval *modulo = new_tarval_from_long(modulo_shf,
4757 get_tarval_mode(res));
4759 /* shifting too much */
4760 if (!(tarval_cmp(res, modulo) & pn_Cmp_Lt)) {
4762 ir_graph *irg = get_irn_irg(n);
4763 ir_node *block = get_nodes_block(n);
4764 dbg_info *dbgi = get_irn_dbg_info(n);
4765 ir_node *cnst = new_Const(mode_Iu, new_tarval_from_long(get_mode_size_bits(mode)-1, mode_Iu));
4766 return new_rd_Shrs(dbgi, irg, block, get_binop_left(left),
4770 return new_Const(mode, get_mode_null(mode));
4774 res = tarval_mod(res, new_tarval_from_long(get_mode_size_bits(mode), get_tarval_mode(res)));
4777 /* ok, we can replace it */
4778 block = get_nodes_block(n);
4780 in[0] = get_binop_left(left);
4781 in[1] = new_r_Const(current_ir_graph, block, get_tarval_mode(res), res);
4783 irn = new_ir_node(NULL, current_ir_graph, block, get_irn_op(n), mode, 2, in);
4785 DBG_OPT_ALGSIM0(n, irn, FS_OPT_REASSOC_SHIFT);
4787 return transform_node(irn);
4788 } /* transform_node_shift */
4791 * normalisation: (x & c1) >> c2 to (x >> c2) & (c1 >> c2)
4793 * - and, or, xor instead of &
4794 * - Shl, Shr, Shrs, rotl instead of >>
4795 * (with a special case for Or/Xor + Shrs)
4797 static ir_node *transform_node_bitop_shift(ir_node *n) {
4799 ir_node *right = get_binop_right(n);
4800 ir_mode *mode = get_irn_mode(n);
4801 ir_node *bitop_left;
4802 ir_node *bitop_right;
4814 assert(is_Shrs(n) || is_Shr(n) || is_Shl(n) || is_Rotl(n));
4816 if (!is_Const(right))
4819 left = get_binop_left(n);
4820 op_left = get_irn_op(left);
4821 if (op_left != op_And && op_left != op_Or && op_left != op_Eor)
4824 /* doing it with Shrs is not legal if the Or/Eor affects the topmost bit */
4825 if (is_Shrs(n) && (op_left == op_Or || op_left == op_Eor)) {
4826 /* TODO: test if sign bit is affectes */
4830 bitop_right = get_binop_right(left);
4831 if (!is_Const(bitop_right))
4834 bitop_left = get_binop_left(left);
4836 irg = get_irn_irg(n);
4837 block = get_nodes_block(n);
4838 dbgi = get_irn_dbg_info(n);
4839 tv1 = get_Const_tarval(bitop_right);
4840 tv2 = get_Const_tarval(right);
4842 assert(get_tarval_mode(tv1) == mode);
4845 new_shift = new_rd_Shl(dbgi, irg, block, bitop_left, right, mode);
4846 tv_shift = tarval_shl(tv1, tv2);
4847 } else if(is_Shr(n)) {
4848 new_shift = new_rd_Shr(dbgi, irg, block, bitop_left, right, mode);
4849 tv_shift = tarval_shr(tv1, tv2);
4850 } else if(is_Shrs(n)) {
4851 new_shift = new_rd_Shrs(dbgi, irg, block, bitop_left, right, mode);
4852 tv_shift = tarval_shrs(tv1, tv2);
4855 new_shift = new_rd_Rotl(dbgi, irg, block, bitop_left, right, mode);
4856 tv_shift = tarval_rotl(tv1, tv2);
4859 assert(get_tarval_mode(tv_shift) == mode);
4860 new_const = new_Const(mode, tv_shift);
4862 if (op_left == op_And) {
4863 new_bitop = new_rd_And(dbgi, irg, block, new_shift, new_const, mode);
4864 } else if(op_left == op_Or) {
4865 new_bitop = new_rd_Or(dbgi, irg, block, new_shift, new_const, mode);
4867 assert(op_left == op_Eor);
4868 new_bitop = new_rd_Eor(dbgi, irg, block, new_shift, new_const, mode);
4876 * (x << c1) >> c2 <=> x OP (c2-c1) & ((-1 << c1) >> c2)
4878 * (x >> c1) << c2 <=> x OP (c2-c1) & ((-1 >> c1) << c2)
4879 * (also with x >>s c1 when c1>=c2)
4881 static ir_node *transform_node_shl_shr(ir_node *n) {
4883 ir_node *right = get_binop_right(n);
4899 assert(is_Shl(n) || is_Shr(n) || is_Shrs(n));
4901 if (!is_Const(right))
4904 left = get_binop_left(n);
4905 mode = get_irn_mode(n);
4906 if (is_Shl(n) && (is_Shr(left) || is_Shrs(left))) {
4907 ir_node *shr_right = get_binop_right(left);
4909 if (!is_Const(shr_right))
4912 x = get_binop_left(left);
4913 tv_shr = get_Const_tarval(shr_right);
4914 tv_shl = get_Const_tarval(right);
4916 if (is_Shrs(left)) {
4917 /* shrs variant only allowed if c1 >= c2 */
4918 if (! (tarval_cmp(tv_shl, tv_shr) & pn_Cmp_Ge))
4921 tv_mask = tarval_shrs(get_mode_all_one(mode), tv_shr);
4924 tv_mask = tarval_shr(get_mode_all_one(mode), tv_shr);
4926 tv_mask = tarval_shl(tv_mask, tv_shl);
4927 } else if(is_Shr(n) && is_Shl(left)) {
4928 ir_node *shl_right = get_Shl_right(left);
4930 if (!is_Const(shl_right))
4933 x = get_Shl_left(left);
4934 tv_shr = get_Const_tarval(right);
4935 tv_shl = get_Const_tarval(shl_right);
4937 tv_mask = tarval_shl(get_mode_all_one(mode), tv_shl);
4938 tv_mask = tarval_shr(tv_mask, tv_shr);
4943 assert(get_tarval_mode(tv_shl) == get_tarval_mode(tv_shr));
4944 assert(tv_mask != tarval_bad);
4945 assert(get_tarval_mode(tv_mask) == mode);
4947 irg = get_irn_irg(n);
4948 block = get_nodes_block(n);
4949 dbgi = get_irn_dbg_info(n);
4951 pnc = tarval_cmp(tv_shl, tv_shr);
4952 if (pnc == pn_Cmp_Lt || pnc == pn_Cmp_Eq) {
4953 tv_shift = tarval_sub(tv_shr, tv_shl, NULL);
4954 new_const = new_Const(get_tarval_mode(tv_shift), tv_shift);
4956 new_shift = new_rd_Shrs(dbgi, irg, block, x, new_const, mode);
4958 new_shift = new_rd_Shr(dbgi, irg, block, x, new_const, mode);
4961 assert(pnc == pn_Cmp_Gt);
4962 tv_shift = tarval_sub(tv_shl, tv_shr, NULL);
4963 new_const = new_Const(get_tarval_mode(tv_shift), tv_shift);
4964 new_shift = new_rd_Shl(dbgi, irg, block, x, new_const, mode);
4967 new_const = new_Const(mode, tv_mask);
4968 new_and = new_rd_And(dbgi, irg, block, new_shift, new_const, mode);
4976 static ir_node *transform_node_Shr(ir_node *n) {
4977 ir_node *c, *oldn = n;
4978 ir_node *left = get_Shr_left(n);
4979 ir_node *right = get_Shr_right(n);
4980 ir_mode *mode = get_irn_mode(n);
4982 HANDLE_BINOP_PHI(tarval_shr, left, right, c, mode);
4983 n = transform_node_shift(n);
4986 n = transform_node_shl_shr(n);
4988 n = transform_node_bitop_shift(n);
4991 } /* transform_node_Shr */
4996 static ir_node *transform_node_Shrs(ir_node *n) {
4997 ir_node *c, *oldn = n;
4998 ir_node *a = get_Shrs_left(n);
4999 ir_node *b = get_Shrs_right(n);
5000 ir_mode *mode = get_irn_mode(n);
5002 HANDLE_BINOP_PHI(tarval_shrs, a, b, c, mode);
5003 n = transform_node_shift(n);
5006 n = transform_node_bitop_shift(n);
5009 } /* transform_node_Shrs */
5014 static ir_node *transform_node_Shl(ir_node *n) {
5015 ir_node *c, *oldn = n;
5016 ir_node *a = get_Shl_left(n);
5017 ir_node *b = get_Shl_right(n);
5018 ir_mode *mode = get_irn_mode(n);
5020 HANDLE_BINOP_PHI(tarval_shl, a, b, c, mode);
5021 n = transform_node_shift(n);
5024 n = transform_node_shl_shr(n);
5026 n = transform_node_bitop_shift(n);
5029 } /* transform_node_Shl */
5034 static ir_node *transform_node_Rotl(ir_node *n) {
5035 ir_node *c, *oldn = n;
5036 ir_node *a = get_Rotl_left(n);
5037 ir_node *b = get_Rotl_right(n);
5038 ir_mode *mode = get_irn_mode(n);
5040 HANDLE_BINOP_PHI(tarval_rotl, a, b, c, mode);
5041 n = transform_node_shift(n);
5044 n = transform_node_bitop_shift(n);
5047 } /* transform_node_Rotl */
5052 static ir_node *transform_node_Conv(ir_node *n) {
5053 ir_node *c, *oldn = n;
5054 ir_node *a = get_Conv_op(n);
5056 if (is_const_Phi(a)) {
5057 c = apply_conv_on_phi(a, get_irn_mode(n));
5059 DBG_OPT_ALGSIM0(oldn, c, FS_OPT_CONST_PHI);
5064 if (is_Unknown(a)) { /* Conv_A(Unknown_B) -> Unknown_A */
5065 ir_mode *mode = get_irn_mode(n);
5066 return new_r_Unknown(current_ir_graph, mode);
5070 } /* transform_node_Conv */
5073 * Remove dead blocks and nodes in dead blocks
5074 * in keep alive list. We do not generate a new End node.
5076 static ir_node *transform_node_End(ir_node *n) {
5077 int i, j, n_keepalives = get_End_n_keepalives(n);
5080 NEW_ARR_A(ir_node *, in, n_keepalives);
5082 for (i = j = 0; i < n_keepalives; ++i) {
5083 ir_node *ka = get_End_keepalive(n, i);
5085 if (! is_Block_dead(ka)) {
5089 } else if (is_irn_pinned_in_irg(ka) && is_Block_dead(get_nodes_block(ka))) {
5092 /* FIXME: beabi need to keep a Proj(M) */
5093 if (is_Phi(ka) || is_irn_keep(ka) || is_Proj(ka))
5096 if (j != n_keepalives)
5097 set_End_keepalives(n, j, in);
5099 } /* transform_node_End */
5101 /** returns 1 if a == -b */
5102 static int is_negated_value(ir_node *a, ir_node *b) {
5103 if (is_Minus(a) && get_Minus_op(a) == b)
5105 if (is_Minus(b) && get_Minus_op(b) == a)
5107 if (is_Sub(a) && is_Sub(b)) {
5108 ir_node *a_left = get_Sub_left(a);
5109 ir_node *a_right = get_Sub_right(a);
5110 ir_node *b_left = get_Sub_left(b);
5111 ir_node *b_right = get_Sub_right(b);
5113 if (a_left == b_right && a_right == b_left)
5121 * Optimize a Mux into some simpler cases.
5123 static ir_node *transform_node_Mux(ir_node *n) {
5124 ir_node *oldn = n, *sel = get_Mux_sel(n);
5125 ir_mode *mode = get_irn_mode(n);
5126 ir_node *t = get_Mux_true(n);
5127 ir_node *f = get_Mux_false(n);
5128 ir_graph *irg = current_ir_graph;
5129 ir_node *conds[1], *vals[2];
5131 /* first normalization step: move a possible zero to the false case */
5133 ir_node *cmp = get_Proj_pred(sel);
5136 if (is_Const(t) && is_Const_null(t)) {
5137 /* Psi(x, 0, y) => Psi(x, y, 0) */
5138 pn_Cmp pnc = get_Proj_proj(sel);
5139 sel = new_r_Proj(irg, get_nodes_block(cmp), cmp, mode_b,
5140 get_negated_pnc(pnc, get_irn_mode(get_Cmp_left(cmp))));
5144 n = new_rd_Psi(get_irn_dbg_info(n), irg, get_nodes_block(n), 1, conds, vals, mode);
5151 /* note: after normalization, false can only happen on default */
5152 if (mode == mode_b) {
5153 dbg_info *dbg = get_irn_dbg_info(n);
5154 ir_node *block = get_nodes_block(n);
5155 ir_graph *irg = current_ir_graph;
5158 tarval *tv_t = get_Const_tarval(t);
5159 if (tv_t == tarval_b_true) {
5161 /* Muxb(sel, true, false) = sel */
5162 assert(get_Const_tarval(f) == tarval_b_false);
5163 DBG_OPT_ALGSIM0(oldn, sel, FS_OPT_MUX_BOOL);
5166 /* Muxb(sel, true, x) = Or(sel, x) */
5167 n = new_rd_Or(dbg, irg, block, sel, f, mode_b);
5168 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_OR_BOOL);
5172 } else if (is_Const(f)) {
5173 tarval *tv_f = get_Const_tarval(f);
5174 if (tv_f == tarval_b_true) {
5175 /* Muxb(sel, x, true) = Or(Not(sel), x) */
5176 ir_node* not_sel = new_rd_Not(dbg, irg, block, sel, mode_b);
5177 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_ORNOT_BOOL);
5178 n = new_rd_Or(dbg, irg, block, not_sel, t, mode_b);
5181 /* Muxb(sel, x, false) = And(sel, x) */
5182 assert(tv_f == tarval_b_false);
5183 n = new_rd_And(dbg, irg, block, sel, t, mode_b);
5184 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_AND_BOOL);
5190 /* more normalization: try to normalize Mux(x, C1, C2) into Mux(x, +1/-1, 0) op C2 */
5191 if (is_Const(t) && is_Const(f) && mode_is_int(mode)) {
5192 tarval *a = get_Const_tarval(t);
5193 tarval *b = get_Const_tarval(f);
5194 tarval *null = get_tarval_null(mode);
5197 if (tarval_cmp(a, b) & pn_Cmp_Gt) {
5198 diff = tarval_sub(a, b, NULL);
5201 diff = tarval_sub(b, a, NULL);
5205 if (diff == get_tarval_one(mode) && min != null) {
5206 dbg_info *dbg = get_irn_dbg_info(n);
5207 ir_node *block = get_nodes_block(n);
5208 ir_graph *irg = current_ir_graph;
5212 vals[0] = new_Const(mode, tarval_sub(a, min, NULL));
5213 vals[1] = new_Const(mode, tarval_sub(b, min, NULL));
5214 n = new_rd_Psi(dbg, irg, block, 1, conds, vals, mode);
5215 n = new_rd_Add(dbg, irg, block, n, new_Const(mode, min), mode);
5221 ir_node *cmp = get_Proj_pred(sel);
5222 long pn = get_Proj_proj(sel);
5225 * Note: normalization puts the constant on the right side,
5226 * so we check only one case.
5228 * Note further that these optimization work even for floating point
5229 * with NaN's because -NaN == NaN.
5230 * However, if +0 and -0 is handled differently, we cannot use the Abs/-Abs
5234 ir_node *cmp_r = get_Cmp_right(cmp);
5235 if (is_Const(cmp_r) && is_Const_null(cmp_r)) {
5236 ir_node *block = get_nodes_block(n);
5237 ir_node *cmp_l = get_Cmp_left(cmp);
5239 if (!mode_honor_signed_zeros(mode) && is_negated_value(f, t)) {
5242 if ( (cmp_l == t && (pn == pn_Cmp_Ge || pn == pn_Cmp_Gt))
5243 || (cmp_l == f && (pn == pn_Cmp_Le || pn == pn_Cmp_Lt)))
5245 /* Psi(a >/>= 0, a, -a) = Psi(a </<= 0, -a, a) ==> Abs(a) */
5246 n = new_rd_Abs(get_irn_dbg_info(n), current_ir_graph, block,
5248 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_ABS);
5250 } else if ((cmp_l == t && (pn == pn_Cmp_Le || pn == pn_Cmp_Lt))
5251 || (cmp_l == f && (pn == pn_Cmp_Ge || pn == pn_Cmp_Gt)))
5253 /* Psi(a </<= 0, a, -a) = Psi(a >/>= 0, -a, a) ==> -Abs(a) */
5254 n = new_rd_Abs(get_irn_dbg_info(n), current_ir_graph, block,
5256 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph,
5258 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_ABS);
5263 if (mode_is_int(mode)) {
5265 if ((pn == pn_Cmp_Lg || pn == pn_Cmp_Eq) && is_And(cmp_l)) {
5266 /* Psi((a & b) != 0, c, 0) */
5267 ir_node *and_r = get_And_right(cmp_l);
5270 if (and_r == t && f == cmp_r) {
5271 if (is_Const(t) && tarval_is_single_bit(get_Const_tarval(t))) {
5272 if (pn == pn_Cmp_Lg) {
5273 /* Psi((a & 2^C) != 0, 2^C, 0) */
5276 /* Psi((a & 2^C) == 0, 2^C, 0) */
5277 n = new_rd_Eor(get_irn_dbg_info(n), current_ir_graph,
5278 block, cmp_l, t, mode);
5283 if (is_Shl(and_r)) {
5284 ir_node *shl_l = get_Shl_left(and_r);
5285 if (is_Const(shl_l) && is_Const_one(shl_l)) {
5286 if (and_r == t && f == cmp_r) {
5287 if (pn == pn_Cmp_Lg) {
5288 /* (a & (1 << n)) != 0, (1 << n), 0) */
5291 /* (a & (1 << n)) == 0, (1 << n), 0) */
5292 n = new_rd_Eor(get_irn_dbg_info(n), current_ir_graph,
5293 block, cmp_l, t, mode);
5299 and_l = get_And_left(cmp_l);
5300 if (is_Shl(and_l)) {
5301 ir_node *shl_l = get_Shl_left(and_l);
5302 if (is_Const(shl_l) && is_Const_one(shl_l)) {
5303 if (and_l == t && f == cmp_r) {
5304 if (pn == pn_Cmp_Lg) {
5305 /* ((1 << n) & a) != 0, (1 << n), 0) */
5308 /* ((1 << n) & a) == 0, (1 << n), 0) */
5309 n = new_rd_Eor(get_irn_dbg_info(n), current_ir_graph,
5310 block, cmp_l, t, mode);
5321 return arch_transform_node_Mux(n);
5322 } /* transform_node_Mux */
5325 * Optimize a Psi into some simpler cases.
5327 static ir_node *transform_node_Psi(ir_node *n) {
5329 return transform_node_Mux(n);
5332 } /* transform_node_Psi */
5335 * optimize sync nodes that have other syncs as input we simply add the inputs
5336 * of the other sync to our own inputs
5338 static ir_node *transform_node_Sync(ir_node *n) {
5339 int arity = get_Sync_n_preds(n);
5342 for (i = 0; i < arity;) {
5343 ir_node *pred = get_Sync_pred(n, i);
5347 if (!is_Sync(pred)) {
5355 pred_arity = get_Sync_n_preds(pred);
5356 for (j = 0; j < pred_arity; ++j) {
5357 ir_node *pred_pred = get_Sync_pred(pred, j);
5362 add_irn_n(n, pred_pred);
5366 if (get_Sync_pred(n, k) == pred_pred) break;
5371 /* rehash the sync node */
5372 add_identities(current_ir_graph->value_table, n);
5378 * Tries several [inplace] [optimizing] transformations and returns an
5379 * equivalent node. The difference to equivalent_node() is that these
5380 * transformations _do_ generate new nodes, and thus the old node must
5381 * not be freed even if the equivalent node isn't the old one.
5383 static ir_node *transform_node(ir_node *n) {
5387 * Transform_node is the only "optimizing transformation" that might
5388 * return a node with a different opcode. We iterate HERE until fixpoint
5389 * to get the final result.
5393 if (n->op->ops.transform_node)
5394 n = n->op->ops.transform_node(n);
5395 } while (oldn != n);
5398 } /* transform_node */
5401 * Sets the default transform node operation for an ir_op_ops.
5403 * @param code the opcode for the default operation
5404 * @param ops the operations initialized
5409 static ir_op_ops *firm_set_default_transform_node(ir_opcode code, ir_op_ops *ops)
5413 ops->transform_node = transform_node_##a; \
5450 } /* firm_set_default_transform_node */
5453 /* **************** Common Subexpression Elimination **************** */
5455 /** The size of the hash table used, should estimate the number of nodes
5457 #define N_IR_NODES 512
5459 /** Compares the attributes of two Const nodes. */
5460 static int node_cmp_attr_Const(ir_node *a, ir_node *b) {
5461 return (get_Const_tarval(a) != get_Const_tarval(b))
5462 || (get_Const_type(a) != get_Const_type(b));
5463 } /* node_cmp_attr_Const */
5465 /** Compares the attributes of two Proj nodes. */
5466 static int node_cmp_attr_Proj(ir_node *a, ir_node *b) {
5467 return get_irn_proj_attr(a) != get_irn_proj_attr(b);
5468 } /* node_cmp_attr_Proj */
5470 /** Compares the attributes of two Filter nodes. */
5471 static int node_cmp_attr_Filter(ir_node *a, ir_node *b) {
5472 return get_Filter_proj(a) != get_Filter_proj(b);
5473 } /* node_cmp_attr_Filter */
5475 /** Compares the attributes of two Alloc nodes. */
5476 static int node_cmp_attr_Alloc(ir_node *a, ir_node *b) {
5477 const alloc_attr *pa = get_irn_alloc_attr(a);
5478 const alloc_attr *pb = get_irn_alloc_attr(b);
5479 return (pa->where != pb->where) || (pa->type != pb->type);
5480 } /* node_cmp_attr_Alloc */
5482 /** Compares the attributes of two Free nodes. */
5483 static int node_cmp_attr_Free(ir_node *a, ir_node *b) {
5484 const free_attr *pa = get_irn_free_attr(a);
5485 const free_attr *pb = get_irn_free_attr(b);
5486 return (pa->where != pb->where) || (pa->type != pb->type);
5487 } /* node_cmp_attr_Free */
5489 /** Compares the attributes of two SymConst nodes. */
5490 static int node_cmp_attr_SymConst(ir_node *a, ir_node *b) {
5491 const symconst_attr *pa = get_irn_symconst_attr(a);
5492 const symconst_attr *pb = get_irn_symconst_attr(b);
5493 return (pa->kind != pb->kind)
5494 || (pa->sym.type_p != pb->sym.type_p)
5495 || (pa->tp != pb->tp);
5496 } /* node_cmp_attr_SymConst */
5498 /** Compares the attributes of two Call nodes. */
5499 static int node_cmp_attr_Call(ir_node *a, ir_node *b) {
5500 return get_irn_call_attr(a) != get_irn_call_attr(b);
5501 } /* node_cmp_attr_Call */
5503 /** Compares the attributes of two Sel nodes. */
5504 static int node_cmp_attr_Sel(ir_node *a, ir_node *b) {
5505 const ir_entity *a_ent = get_Sel_entity(a);
5506 const ir_entity *b_ent = get_Sel_entity(b);
5508 (a_ent->kind != b_ent->kind) ||
5509 (a_ent->name != b_ent->name) ||
5510 (a_ent->owner != b_ent->owner) ||
5511 (a_ent->ld_name != b_ent->ld_name) ||
5512 (a_ent->type != b_ent->type);
5513 } /* node_cmp_attr_Sel */
5515 /** Compares the attributes of two Phi nodes. */
5516 static int node_cmp_attr_Phi(ir_node *a, ir_node *b) {
5517 /* we can only enter this function if both nodes have the same number of inputs,
5518 hence it is enough to check if one of them is a Phi0 */
5520 /* check the Phi0 pos attribute */
5521 return get_irn_phi_attr(a)->u.pos != get_irn_phi_attr(b)->u.pos;
5524 } /* node_cmp_attr_Phi */
5526 /** Compares the attributes of two Conv nodes. */
5527 static int node_cmp_attr_Conv(ir_node *a, ir_node *b) {
5528 return get_Conv_strict(a) != get_Conv_strict(b);
5529 } /* node_cmp_attr_Conv */
5531 /** Compares the attributes of two Cast nodes. */
5532 static int node_cmp_attr_Cast(ir_node *a, ir_node *b) {
5533 return get_Cast_type(a) != get_Cast_type(b);
5534 } /* node_cmp_attr_Cast */
5536 /** Compares the attributes of two Load nodes. */
5537 static int node_cmp_attr_Load(ir_node *a, ir_node *b) {
5538 if (get_Load_volatility(a) == volatility_is_volatile ||
5539 get_Load_volatility(b) == volatility_is_volatile)
5540 /* NEVER do CSE on volatile Loads */
5542 /* do not CSE Loads with different alignment. Be conservative. */
5543 if (get_Load_align(a) != get_Load_align(b))
5546 return get_Load_mode(a) != get_Load_mode(b);
5547 } /* node_cmp_attr_Load */
5549 /** Compares the attributes of two Store nodes. */
5550 static int node_cmp_attr_Store(ir_node *a, ir_node *b) {
5551 /* do not CSE Stores with different alignment. Be conservative. */
5552 if (get_Store_align(a) != get_Store_align(b))
5555 /* NEVER do CSE on volatile Stores */
5556 return (get_Store_volatility(a) == volatility_is_volatile ||
5557 get_Store_volatility(b) == volatility_is_volatile);
5558 } /* node_cmp_attr_Store */
5560 /** Compares two exception attributes */
5561 static int node_cmp_exception(ir_node *a, ir_node *b) {
5562 const except_attr *ea = get_irn_except_attr(a);
5563 const except_attr *eb = get_irn_except_attr(b);
5565 return ea->pin_state != eb->pin_state;
5568 #define node_cmp_attr_Bound node_cmp_exception
5570 /** Compares the attributes of two Div nodes. */
5571 static int node_cmp_attr_Div(ir_node *a, ir_node *b) {
5572 const divmod_attr *ma = get_irn_divmod_attr(a);
5573 const divmod_attr *mb = get_irn_divmod_attr(b);
5574 return ma->exc.pin_state != mb->exc.pin_state ||
5575 ma->res_mode != mb->res_mode ||
5576 ma->no_remainder != mb->no_remainder;
5577 } /* node_cmp_attr_Div */
5579 /** Compares the attributes of two DivMod nodes. */
5580 static int node_cmp_attr_DivMod(ir_node *a, ir_node *b) {
5581 const divmod_attr *ma = get_irn_divmod_attr(a);
5582 const divmod_attr *mb = get_irn_divmod_attr(b);
5583 return ma->exc.pin_state != mb->exc.pin_state ||
5584 ma->res_mode != mb->res_mode;
5585 } /* node_cmp_attr_DivMod */
5587 /** Compares the attributes of two Mod nodes. */
5588 static int node_cmp_attr_Mod(ir_node *a, ir_node *b) {
5589 const divmod_attr *ma = get_irn_divmod_attr(a);
5590 const divmod_attr *mb = get_irn_divmod_attr(b);
5591 return ma->exc.pin_state != mb->exc.pin_state ||
5592 ma->res_mode != mb->res_mode;
5593 } /* node_cmp_attr_Mod */
5595 /** Compares the attributes of two Quot nodes. */
5596 static int node_cmp_attr_Quot(ir_node *a, ir_node *b) {
5597 const divmod_attr *ma = get_irn_divmod_attr(a);
5598 const divmod_attr *mb = get_irn_divmod_attr(b);
5599 return ma->exc.pin_state != mb->exc.pin_state ||
5600 ma->res_mode != mb->res_mode;
5601 } /* node_cmp_attr_Quot */
5603 /** Compares the attributes of two Confirm nodes. */
5604 static int node_cmp_attr_Confirm(ir_node *a, ir_node *b) {
5605 return (get_Confirm_cmp(a) != get_Confirm_cmp(b));
5606 } /* node_cmp_attr_Confirm */
5608 /** Compares the attributes of two ASM nodes. */
5609 static int node_cmp_attr_ASM(ir_node *a, ir_node *b) {
5611 const ir_asm_constraint *ca;
5612 const ir_asm_constraint *cb;
5615 if (get_ASM_text(a) != get_ASM_text(b))
5618 /* Should we really check the constraints here? Should be better, but is strange. */
5619 n = get_ASM_n_input_constraints(a);
5620 if (n != get_ASM_n_input_constraints(b))
5623 ca = get_ASM_input_constraints(a);
5624 cb = get_ASM_input_constraints(b);
5625 for (i = 0; i < n; ++i) {
5626 if (ca[i].pos != cb[i].pos || ca[i].constraint != cb[i].constraint)
5630 n = get_ASM_n_output_constraints(a);
5631 if (n != get_ASM_n_output_constraints(b))
5634 ca = get_ASM_output_constraints(a);
5635 cb = get_ASM_output_constraints(b);
5636 for (i = 0; i < n; ++i) {
5637 if (ca[i].pos != cb[i].pos || ca[i].constraint != cb[i].constraint)
5641 n = get_ASM_n_clobbers(a);
5642 if (n != get_ASM_n_clobbers(b))
5645 cla = get_ASM_clobbers(a);
5646 clb = get_ASM_clobbers(b);
5647 for (i = 0; i < n; ++i) {
5648 if (cla[i] != clb[i])
5652 } /* node_cmp_attr_ASM */
5655 * Set the default node attribute compare operation for an ir_op_ops.
5657 * @param code the opcode for the default operation
5658 * @param ops the operations initialized
5663 static ir_op_ops *firm_set_default_node_cmp_attr(ir_opcode code, ir_op_ops *ops)
5667 ops->node_cmp_attr = node_cmp_attr_##a; \
5698 } /* firm_set_default_node_cmp_attr */
5701 * Compare function for two nodes in the value table. Gets two
5702 * nodes as parameters. Returns 0 if the nodes are a Common Sub Expression.
5704 int identities_cmp(const void *elt, const void *key) {
5705 ir_node *a = (ir_node *)elt;
5706 ir_node *b = (ir_node *)key;
5709 if (a == b) return 0;
5711 if ((get_irn_op(a) != get_irn_op(b)) ||
5712 (get_irn_mode(a) != get_irn_mode(b))) return 1;
5714 /* compare if a's in and b's in are of equal length */
5715 irn_arity_a = get_irn_intra_arity(a);
5716 if (irn_arity_a != get_irn_intra_arity(b))
5719 if (get_irn_pinned(a) == op_pin_state_pinned) {
5720 /* for pinned nodes, the block inputs must be equal */
5721 if (get_irn_intra_n(a, -1) != get_irn_intra_n(b, -1))
5723 } else if (! get_opt_global_cse()) {
5724 /* for block-local CSE both nodes must be in the same MacroBlock */
5725 if (get_irn_MacroBlock(a) != get_irn_MacroBlock(b))
5729 /* compare a->in[0..ins] with b->in[0..ins] */
5730 for (i = 0; i < irn_arity_a; i++)
5731 if (get_irn_intra_n(a, i) != get_irn_intra_n(b, i))
5735 * here, we already now that the nodes are identical except their
5738 if (a->op->ops.node_cmp_attr)
5739 return a->op->ops.node_cmp_attr(a, b);
5742 } /* identities_cmp */
5745 * Calculate a hash value of a node.
5747 * @param node The IR-node
5749 unsigned ir_node_hash(const ir_node *node) {
5750 return node->op->ops.hash(node);
5751 } /* ir_node_hash */
5754 pset *new_identities(void) {
5755 return new_pset(identities_cmp, N_IR_NODES);
5756 } /* new_identities */
5758 void del_identities(pset *value_table) {
5759 del_pset(value_table);
5760 } /* del_identities */
5763 * Normalize a node by putting constants (and operands with larger
5764 * node index) on the right (operator side).
5766 * @param n The node to normalize
5768 static void normalize_node(ir_node *n) {
5769 if (is_op_commutative(get_irn_op(n))) {
5770 ir_node *l = get_binop_left(n);
5771 ir_node *r = get_binop_right(n);
5773 /* For commutative operators perform a OP b == b OP a but keep
5774 * constants on the RIGHT side. This helps greatly in some
5775 * optimizations. Moreover we use the idx number to make the form
5777 if (!operands_are_normalized(l, r)) {
5778 set_binop_left(n, r);
5779 set_binop_right(n, l);
5782 } /* normalize_node */
5785 * Update the nodes after a match in the value table. If both nodes have
5786 * the same MacroBlock but different Blocks, we must ensure that the node
5787 * with the dominating Block (the node that is near to the MacroBlock header
5788 * is stored in the table.
5789 * Because a MacroBlock has only one "non-exception" flow, we don't need
5790 * dominance info here: We known, that one block must dominate the other and
5791 * following the only block input will allow to find it.
5793 static void update_known_irn(ir_node *known_irn, const ir_node *new_ir_node) {
5794 ir_node *known_blk, *new_block, *block, *mbh;
5796 if (get_opt_global_cse()) {
5797 /* Block inputs are meaning less */
5800 known_blk = get_irn_n(known_irn, -1);
5801 new_block = get_irn_n(new_ir_node, -1);
5802 if (known_blk == new_block) {
5803 /* already in the same block */
5807 * We expect the typical case when we built the graph. In that case, the
5808 * known_irn is already the upper one, so checking this should be faster.
5811 mbh = get_Block_MacroBlock(new_block);
5813 if (block == known_blk) {
5814 /* ok, we have found it: known_block dominates new_block as expected */
5819 * We have reached the MacroBlock header NOT founding
5820 * the known_block. new_block must dominate known_block.
5823 set_irn_n(known_irn, -1, new_block);
5826 assert(get_Block_n_cfgpreds(block) == 1);
5827 block = get_Block_cfgpred_block(block, 0);
5829 } /* update_value_table */
5832 * Return the canonical node computing the same value as n.
5833 * Looks up the node in a hash table, enters it in the table
5834 * if it isn't there yet.
5836 * @param value_table the HashSet containing all nodes in the
5838 * @param n the node to look up
5840 * @return a node that computes the same value as n or n if no such
5841 * node could be found
5843 ir_node *identify_remember(pset *value_table, ir_node *n) {
5846 if (!value_table) return n;
5849 /* lookup or insert in hash table with given hash key. */
5850 o = pset_insert(value_table, n, ir_node_hash(n));
5853 update_known_irn(o, n);
5858 } /* identify_remember */
5861 * During construction we set the op_pin_state_pinned flag in the graph right when the
5862 * optimization is performed. The flag turning on procedure global cse could
5863 * be changed between two allocations. This way we are safe.
5865 * @param value_table The value table
5866 * @param n The node to lookup
5868 static INLINE ir_node *identify_cons(pset *value_table, ir_node *n) {
5871 n = identify_remember(value_table, n);
5872 if (n != old && get_irn_MacroBlock(old) != get_irn_MacroBlock(n))
5873 set_irg_pinned(current_ir_graph, op_pin_state_floats);
5875 } /* identify_cons */
5877 /* Add a node to the identities value table. */
5878 void add_identities(pset *value_table, ir_node *node) {
5879 if (get_opt_cse() && is_no_Block(node))
5880 identify_remember(value_table, node);
5881 } /* add_identities */
5883 /* Visit each node in the value table of a graph. */
5884 void visit_all_identities(ir_graph *irg, irg_walk_func visit, void *env) {
5886 ir_graph *rem = current_ir_graph;
5888 current_ir_graph = irg;
5889 foreach_pset(irg->value_table, node)
5891 current_ir_graph = rem;
5892 } /* visit_all_identities */
5895 * Garbage in, garbage out. If a node has a dead input, i.e., the
5896 * Bad node is input to the node, return the Bad node.
5898 static ir_node *gigo(ir_node *node) {
5900 ir_op *op = get_irn_op(node);
5902 /* remove garbage blocks by looking at control flow that leaves the block
5903 and replacing the control flow by Bad. */
5904 if (get_irn_mode(node) == mode_X) {
5905 ir_node *block = get_nodes_block(skip_Proj(node));
5907 /* Don't optimize nodes in immature blocks. */
5908 if (!get_Block_matured(block))
5910 /* Don't optimize End, may have Bads. */
5911 if (op == op_End) return node;
5913 if (is_Block(block)) {
5914 if (is_Block_dead(block)) {
5915 /* control flow from dead block is dead */
5919 for (i = get_irn_arity(block) - 1; i >= 0; --i) {
5920 if (!is_Bad(get_irn_n(block, i)))
5924 ir_graph *irg = get_irn_irg(block);
5925 /* the start block is never dead */
5926 if (block != get_irg_start_block(irg)
5927 && block != get_irg_end_block(irg)) {
5929 * Do NOT kill control flow without setting
5930 * the block to dead of bad things can happen:
5931 * We get a Block that is not reachable be irg_block_walk()
5932 * but can be found by irg_walk()!
5934 set_Block_dead(block);
5941 /* Blocks, Phis and Tuples may have dead inputs, e.g., if one of the
5942 blocks predecessors is dead. */
5943 if (op != op_Block && op != op_Phi && op != op_Tuple) {
5944 irn_arity = get_irn_arity(node);
5947 * Beware: we can only read the block of a non-floating node.
5949 if (is_irn_pinned_in_irg(node) &&
5950 is_Block_dead(get_nodes_block(skip_Proj(node))))
5953 for (i = 0; i < irn_arity; i++) {
5954 ir_node *pred = get_irn_n(node, i);
5959 /* Propagating Unknowns here seems to be a bad idea, because
5960 sometimes we need a node as a input and did not want that
5962 However, it might be useful to move this into a later phase
5963 (if you think that optimizing such code is useful). */
5964 if (is_Unknown(pred) && mode_is_data(get_irn_mode(node)))
5965 return new_Unknown(get_irn_mode(node));
5970 /* With this code we violate the agreement that local_optimize
5971 only leaves Bads in Block, Phi and Tuple nodes. */
5972 /* If Block has only Bads as predecessors it's garbage. */
5973 /* If Phi has only Bads as predecessors it's garbage. */
5974 if ((op == op_Block && get_Block_matured(node)) || op == op_Phi) {
5975 irn_arity = get_irn_arity(node);
5976 for (i = 0; i < irn_arity; i++) {
5977 if (!is_Bad(get_irn_n(node, i))) break;
5979 if (i == irn_arity) node = new_Bad();
5986 * These optimizations deallocate nodes from the obstack.
5987 * It can only be called if it is guaranteed that no other nodes
5988 * reference this one, i.e., right after construction of a node.
5990 * @param n The node to optimize
5992 * current_ir_graph must be set to the graph of the node!
5994 ir_node *optimize_node(ir_node *n) {
5997 ir_opcode iro = get_irn_opcode(n);
5999 /* Always optimize Phi nodes: part of the construction. */
6000 if ((!get_opt_optimize()) && (iro != iro_Phi)) return n;
6002 /* constant expression evaluation / constant folding */
6003 if (get_opt_constant_folding()) {
6004 /* neither constants nor Tuple values can be evaluated */
6005 if (iro != iro_Const && (get_irn_mode(n) != mode_T)) {
6006 unsigned fp_model = get_irg_fp_model(current_ir_graph);
6007 int old_fp_mode = tarval_enable_fp_ops((fp_model & fp_strict_algebraic) == 0);
6008 /* try to evaluate */
6009 tv = computed_value(n);
6010 if (tv != tarval_bad) {
6012 ir_type *old_tp = get_irn_type(n);
6013 int i, arity = get_irn_arity(n);
6017 * Try to recover the type of the new expression.
6019 for (i = 0; i < arity && !old_tp; ++i)
6020 old_tp = get_irn_type(get_irn_n(n, i));
6023 * we MUST copy the node here temporary, because it's still needed
6024 * for DBG_OPT_CSTEVAL
6026 node_size = offsetof(ir_node, attr) + n->op->attr_size;
6027 oldn = alloca(node_size);
6029 memcpy(oldn, n, node_size);
6030 CLONE_ARR_A(ir_node *, oldn->in, n->in);
6032 /* ARG, copy the in array, we need it for statistics */
6033 memcpy(oldn->in, n->in, ARR_LEN(n->in) * sizeof(n->in[0]));
6035 /* note the inplace edges module */
6036 edges_node_deleted(n, current_ir_graph);
6038 /* evaluation was successful -- replace the node. */
6039 irg_kill_node(current_ir_graph, n);
6040 nw = new_Const(get_tarval_mode(tv), tv);
6042 if (old_tp && get_type_mode(old_tp) == get_tarval_mode(tv))
6043 set_Const_type(nw, old_tp);
6044 DBG_OPT_CSTEVAL(oldn, nw);
6045 tarval_enable_fp_ops(old_fp_mode);
6048 tarval_enable_fp_ops(old_fp_mode);
6052 /* remove unnecessary nodes */
6053 if (get_opt_algebraic_simplification() ||
6054 (iro == iro_Phi) || /* always optimize these nodes. */
6056 (iro == iro_Proj) ||
6057 (iro == iro_Block) ) /* Flags tested local. */
6058 n = equivalent_node(n);
6060 /* Common Subexpression Elimination.
6062 * Checks whether n is already available.
6063 * The block input is used to distinguish different subexpressions. Right
6064 * now all nodes are op_pin_state_pinned to blocks, i.e., the CSE only finds common
6065 * subexpressions within a block.
6068 n = identify_cons(current_ir_graph->value_table, n);
6071 edges_node_deleted(oldn, current_ir_graph);
6073 /* We found an existing, better node, so we can deallocate the old node. */
6074 irg_kill_node(current_ir_graph, oldn);
6078 /* Some more constant expression evaluation that does not allow to
6080 iro = get_irn_opcode(n);
6081 if (get_opt_algebraic_simplification() ||
6082 (iro == iro_Cond) ||
6083 (iro == iro_Proj)) /* Flags tested local. */
6084 n = transform_node(n);
6086 /* Remove nodes with dead (Bad) input.
6087 Run always for transformation induced Bads. */
6090 /* Now we have a legal, useful node. Enter it in hash table for CSE */
6091 if (get_opt_cse() && (get_irn_opcode(n) != iro_Block)) {
6092 n = identify_remember(current_ir_graph->value_table, n);
6096 } /* optimize_node */
6100 * These optimizations never deallocate nodes (in place). This can cause dead
6101 * nodes lying on the obstack. Remove these by a dead node elimination,
6102 * i.e., a copying garbage collection.
6104 ir_node *optimize_in_place_2(ir_node *n) {
6107 ir_opcode iro = get_irn_opcode(n);
6109 if (!get_opt_optimize() && !is_Phi(n)) return n;
6111 /* constant expression evaluation / constant folding */
6112 if (get_opt_constant_folding()) {
6113 /* neither constants nor Tuple values can be evaluated */
6114 if (iro != iro_Const && get_irn_mode(n) != mode_T) {
6115 unsigned fp_model = get_irg_fp_model(current_ir_graph);
6116 int old_fp_mode = tarval_enable_fp_ops((fp_model & fp_strict_algebraic) == 0);
6117 /* try to evaluate */
6118 tv = computed_value(n);
6119 if (tv != tarval_bad) {
6120 /* evaluation was successful -- replace the node. */
6121 ir_type *old_tp = get_irn_type(n);
6122 int i, arity = get_irn_arity(n);
6125 * Try to recover the type of the new expression.
6127 for (i = 0; i < arity && !old_tp; ++i)
6128 old_tp = get_irn_type(get_irn_n(n, i));
6130 n = new_Const(get_tarval_mode(tv), tv);
6132 if (old_tp && get_type_mode(old_tp) == get_tarval_mode(tv))
6133 set_Const_type(n, old_tp);
6135 DBG_OPT_CSTEVAL(oldn, n);
6136 tarval_enable_fp_ops(old_fp_mode);
6139 tarval_enable_fp_ops(old_fp_mode);
6143 /* remove unnecessary nodes */
6144 if (get_opt_constant_folding() ||
6145 (iro == iro_Phi) || /* always optimize these nodes. */
6146 (iro == iro_Id) || /* ... */
6147 (iro == iro_Proj) || /* ... */
6148 (iro == iro_Block) ) /* Flags tested local. */
6149 n = equivalent_node(n);
6151 /** common subexpression elimination **/
6152 /* Checks whether n is already available. */
6153 /* The block input is used to distinguish different subexpressions. Right
6154 now all nodes are op_pin_state_pinned to blocks, i.e., the cse only finds common
6155 subexpressions within a block. */
6156 if (get_opt_cse()) {
6157 n = identify_remember(current_ir_graph->value_table, n);
6160 /* Some more constant expression evaluation. */
6161 iro = get_irn_opcode(n);
6162 if (get_opt_constant_folding() ||
6163 (iro == iro_Cond) ||
6164 (iro == iro_Proj)) /* Flags tested local. */
6165 n = transform_node(n);
6167 /* Remove nodes with dead (Bad) input.
6168 Run always for transformation induced Bads. */
6171 /* Now we can verify the node, as it has no dead inputs any more. */
6174 /* Now we have a legal, useful node. Enter it in hash table for cse.
6175 Blocks should be unique anyways. (Except the successor of start:
6176 is cse with the start block!) */
6177 if (get_opt_cse() && (get_irn_opcode(n) != iro_Block))
6178 n = identify_remember(current_ir_graph->value_table, n);
6181 } /* optimize_in_place_2 */
6184 * Wrapper for external use, set proper status bits after optimization.
6186 ir_node *optimize_in_place(ir_node *n) {
6187 /* Handle graph state */
6188 assert(get_irg_phase_state(current_ir_graph) != phase_building);
6190 if (get_opt_global_cse())
6191 set_irg_pinned(current_ir_graph, op_pin_state_floats);
6192 if (get_irg_outs_state(current_ir_graph) == outs_consistent)
6193 set_irg_outs_inconsistent(current_ir_graph);
6195 /* FIXME: Maybe we could also test whether optimizing the node can
6196 change the control graph. */
6197 set_irg_doms_inconsistent(current_ir_graph);
6198 return optimize_in_place_2(n);
6199 } /* optimize_in_place */
6202 * Calculate a hash value of a Const node.
6204 static unsigned hash_Const(const ir_node *node) {
6207 /* special value for const, as they only differ in their tarval. */
6208 h = HASH_PTR(node->attr.con.tv);
6209 h = 9*h + HASH_PTR(get_irn_mode(node));
6215 * Calculate a hash value of a SymConst node.
6217 static unsigned hash_SymConst(const ir_node *node) {
6220 /* special value for const, as they only differ in their symbol. */
6221 h = HASH_PTR(node->attr.symc.sym.type_p);
6222 h = 9*h + HASH_PTR(get_irn_mode(node));
6225 } /* hash_SymConst */
6228 * Set the default hash operation in an ir_op_ops.
6230 * @param code the opcode for the default operation
6231 * @param ops the operations initialized
6236 static ir_op_ops *firm_set_default_hash(ir_opcode code, ir_op_ops *ops)
6240 ops->hash = hash_##a; \
6243 /* hash function already set */
6244 if (ops->hash != NULL)
6251 /* use input/mode default hash if no function was given */
6252 ops->hash = firm_default_hash;
6260 * Sets the default operation for an ir_ops.
6262 ir_op_ops *firm_set_default_operations(ir_opcode code, ir_op_ops *ops) {
6263 ops = firm_set_default_hash(code, ops);
6264 ops = firm_set_default_computed_value(code, ops);
6265 ops = firm_set_default_equivalent_node(code, ops);
6266 ops = firm_set_default_transform_node(code, ops);
6267 ops = firm_set_default_node_cmp_attr(code, ops);
6268 ops = firm_set_default_get_type(code, ops);
6269 ops = firm_set_default_get_type_attr(code, ops);
6270 ops = firm_set_default_get_entity_attr(code, ops);
6273 } /* firm_set_default_operations */