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);
1298 if (smaller_mode(b_mode, a_mode)) {
1299 if (get_Conv_strict(n))
1300 set_Conv_strict(b, 1);
1301 n = b; /* ConvA(ConvB(ConvA(...))) == ConvA(...) */
1302 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
1308 } /* equivalent_node_Conv */
1311 * A Cast may be removed if the type of the previous node
1312 * is already the type of the Cast.
1314 static ir_node *equivalent_node_Cast(ir_node *n) {
1316 ir_node *pred = get_Cast_op(n);
1318 if (get_irn_type(pred) == get_Cast_type(n)) {
1320 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CAST);
1323 } /* equivalent_node_Cast */
1326 * Several optimizations:
1327 * - no Phi in start block.
1328 * - remove Id operators that are inputs to Phi
1329 * - fold Phi-nodes, iff they have only one predecessor except
1332 static ir_node *equivalent_node_Phi(ir_node *n) {
1337 ir_node *first_val = NULL; /* to shutup gcc */
1339 if (!get_opt_normalize()) return n;
1341 n_preds = get_Phi_n_preds(n);
1343 block = get_nodes_block(n);
1344 if ((is_Block_dead(block)) || /* Control dead */
1345 (block == get_irg_start_block(current_ir_graph))) /* There should be no Phi nodes */
1346 return new_Bad(); /* in the Start Block. */
1348 if (n_preds == 0) return n; /* Phi of dead Region without predecessors. */
1350 /* If the Block has a Bad pred, we also have one. */
1351 for (i = 0; i < n_preds; ++i)
1352 if (is_Bad(get_Block_cfgpred(block, i)))
1353 set_Phi_pred(n, i, new_Bad());
1355 /* Find first non-self-referencing input */
1356 for (i = 0; i < n_preds; ++i) {
1357 first_val = get_Phi_pred(n, i);
1358 if ( (first_val != n) /* not self pointer */
1360 /* BEWARE: when the if is changed to 1, Phi's will ignore it's Bad
1361 * predecessors. Then, Phi nodes in dead code might be removed, causing
1362 * nodes pointing to themself (Add's for instance).
1363 * This is really bad and causes endless recursions in several
1364 * code pathes, so we do NOT optimize such a code.
1365 * This is not that bad as it sounds, optimize_cf() removes bad control flow
1366 * (and bad Phi predecessors), so live code is optimized later.
1368 && (! is_Bad(first_val))
1370 ) { /* value not dead */
1371 break; /* then found first value. */
1376 /* A totally Bad or self-referencing Phi (we didn't break the above loop) */
1380 /* search for rest of inputs, determine if any of these
1381 are non-self-referencing */
1382 while (++i < n_preds) {
1383 ir_node *scnd_val = get_Phi_pred(n, i);
1384 if ( (scnd_val != n)
1385 && (scnd_val != first_val)
1388 && (! is_Bad(scnd_val))
1396 /* Fold, if no multiple distinct non-self-referencing inputs */
1398 DBG_OPT_PHI(oldn, n);
1401 } /* equivalent_node_Phi */
1404 * Several optimizations:
1405 * - no Sync in start block.
1406 * - fold Sync-nodes, iff they have only one predecessor except
1409 static ir_node *equivalent_node_Sync(ir_node *n) {
1410 int arity = get_Sync_n_preds(n);
1413 for (i = 0; i < arity;) {
1414 ir_node *pred = get_Sync_pred(n, i);
1417 /* Remove Bad predecessors */
1424 /* Remove duplicate predecessors */
1430 if (get_Sync_pred(n, j) == pred) {
1438 if (arity == 0) return new_Bad();
1439 if (arity == 1) return get_Sync_pred(n, 0);
1441 } /* equivalent_node_Sync */
1444 * Optimize Proj(Tuple) and gigo() for ProjX in Bad block,
1445 * ProjX(Load) and ProjX(Store).
1447 static ir_node *equivalent_node_Proj(ir_node *proj) {
1448 ir_node *oldn = proj;
1449 ir_node *a = get_Proj_pred(proj);
1452 /* Remove the Tuple/Proj combination. */
1453 if ( get_Proj_proj(proj) <= get_Tuple_n_preds(a) ) {
1454 proj = get_Tuple_pred(a, get_Proj_proj(proj));
1455 DBG_OPT_TUPLE(oldn, a, proj);
1457 /* This should not happen! */
1458 assert(! "found a Proj with higher number than Tuple predecessors");
1461 } else if (get_irn_mode(proj) == mode_X) {
1462 if (is_Block_dead(get_nodes_block(skip_Proj(proj)))) {
1463 /* Remove dead control flow -- early gigo(). */
1465 } else if (get_opt_ldst_only_null_ptr_exceptions()) {
1467 /* get the Load address */
1468 ir_node *addr = get_Load_ptr(a);
1469 ir_node *blk = get_irn_n(a, -1);
1472 if (value_not_null(addr, &confirm)) {
1473 if (confirm == NULL) {
1474 /* this node may float if it did not depend on a Confirm */
1475 set_irn_pinned(a, op_pin_state_floats);
1477 if (get_Proj_proj(proj) == pn_Load_X_except) {
1478 DBG_OPT_EXC_REM(proj);
1481 return new_r_Jmp(current_ir_graph, blk);
1483 } else if (is_Store(a)) {
1484 /* get the load/store address */
1485 ir_node *addr = get_Store_ptr(a);
1486 ir_node *blk = get_irn_n(a, -1);
1489 if (value_not_null(addr, &confirm)) {
1490 if (confirm == NULL) {
1491 /* this node may float if it did not depend on a Confirm */
1492 set_irn_pinned(a, op_pin_state_floats);
1494 if (get_Proj_proj(proj) == pn_Store_X_except) {
1495 DBG_OPT_EXC_REM(proj);
1498 return new_r_Jmp(current_ir_graph, blk);
1505 } /* equivalent_node_Proj */
1510 static ir_node *equivalent_node_Id(ir_node *n) {
1515 } while (get_irn_op(n) == op_Id);
1517 DBG_OPT_ID(oldn, n);
1519 } /* equivalent_node_Id */
1524 static ir_node *equivalent_node_Mux(ir_node *n)
1526 ir_node *oldn = n, *sel = get_Mux_sel(n);
1527 tarval *ts = value_of(sel);
1529 /* Mux(true, f, t) == t */
1530 if (ts == tarval_b_true) {
1531 n = get_Mux_true(n);
1532 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_C);
1534 /* Mux(false, f, t) == f */
1535 else if (ts == tarval_b_false) {
1536 n = get_Mux_false(n);
1537 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_C);
1539 /* Mux(v, x, x) == x */
1540 else if (get_Mux_false(n) == get_Mux_true(n)) {
1541 n = get_Mux_true(n);
1542 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_EQ);
1544 else if (is_Proj(sel) && !mode_honor_signed_zeros(get_irn_mode(n))) {
1545 ir_node *cmp = get_Proj_pred(sel);
1546 long proj_nr = get_Proj_proj(sel);
1547 ir_node *f = get_Mux_false(n);
1548 ir_node *t = get_Mux_true(n);
1551 * Note further that these optimization work even for floating point
1552 * with NaN's because -NaN == NaN.
1553 * However, if +0 and -0 is handled differently, we cannot use the first one.
1556 ir_node *const cmp_l = get_Cmp_left(cmp);
1557 ir_node *const cmp_r = get_Cmp_right(cmp);
1561 if ((cmp_l == t && cmp_r == f) || /* Psi(t == f, t, f) -> f */
1562 (cmp_l == f && cmp_r == t)) { /* Psi(f == t, t, f) -> f */
1564 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1571 if ((cmp_l == t && cmp_r == f) || /* Psi(t != f, t, f) -> t */
1572 (cmp_l == f && cmp_r == t)) { /* Psi(f != t, t, f) -> t */
1574 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1581 * Note: normalization puts the constant on the right side,
1582 * so we check only one case.
1584 if (cmp_l == t && is_Const(cmp_r) && is_Const_null(cmp_r)) {
1585 /* Mux(t CMP 0, X, t) */
1586 if (is_Minus(f) && get_Minus_op(f) == t) {
1587 /* Mux(t CMP 0, -t, t) */
1588 if (proj_nr == pn_Cmp_Eq) {
1589 /* Mux(t == 0, -t, t) ==> -t */
1591 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1592 } else if (proj_nr == pn_Cmp_Lg || proj_nr == pn_Cmp_Ne) {
1593 /* Mux(t != 0, -t, t) ==> t */
1595 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1602 } /* equivalent_node_Mux */
1605 * Returns a equivalent node of a Psi: if a condition is true
1606 * and all previous conditions are false we know its value.
1607 * If all conditions are false its value is the default one.
1609 static ir_node *equivalent_node_Psi(ir_node *n) {
1611 return equivalent_node_Mux(n);
1613 } /* equivalent_node_Psi */
1616 * Optimize -a CMP -b into b CMP a.
1617 * This works only for for modes where unary Minus
1619 * Note that two-complement integers can Overflow
1620 * so it will NOT work.
1622 * For == and != can be handled in Proj(Cmp)
1624 static ir_node *equivalent_node_Cmp(ir_node *n) {
1625 ir_node *left = get_Cmp_left(n);
1626 ir_node *right = get_Cmp_right(n);
1628 if (is_Minus(left) && is_Minus(right) &&
1629 !mode_overflow_on_unary_Minus(get_irn_mode(left))) {
1630 left = get_Minus_op(left);
1631 right = get_Minus_op(right);
1632 set_Cmp_left(n, right);
1633 set_Cmp_right(n, left);
1636 } /* equivalent_node_Cmp */
1639 * Remove Confirm nodes if setting is on.
1640 * Replace Confirms(x, '=', Constlike) by Constlike.
1642 static ir_node *equivalent_node_Confirm(ir_node *n) {
1643 ir_node *pred = get_Confirm_value(n);
1644 pn_Cmp pnc = get_Confirm_cmp(n);
1646 if (is_Confirm(pred) && pnc == get_Confirm_cmp(pred)) {
1648 * rare case: two identical Confirms one after another,
1649 * replace the second one with the first.
1653 if (get_opt_remove_confirm())
1654 return get_Confirm_value(n);
1659 * Optimize CopyB(mem, x, x) into a Nop.
1661 static ir_node *equivalent_node_CopyB(ir_node *n) {
1662 ir_node *a = get_CopyB_dst(n);
1663 ir_node *b = get_CopyB_src(n);
1666 /* Turn CopyB into a tuple (mem, jmp, bad, bad) */
1667 ir_node *mem = get_CopyB_mem(n);
1668 ir_node *blk = get_nodes_block(n);
1669 turn_into_tuple(n, pn_CopyB_max);
1670 set_Tuple_pred(n, pn_CopyB_M, mem);
1671 set_Tuple_pred(n, pn_CopyB_X_regular, new_r_Jmp(current_ir_graph, blk));
1672 set_Tuple_pred(n, pn_CopyB_X_except, new_Bad()); /* no exception */
1673 set_Tuple_pred(n, pn_CopyB_M_except, new_Bad());
1676 } /* equivalent_node_CopyB */
1679 * Optimize Bounds(idx, idx, upper) into idx.
1681 static ir_node *equivalent_node_Bound(ir_node *n) {
1682 ir_node *idx = get_Bound_index(n);
1683 ir_node *pred = skip_Proj(idx);
1686 if (is_Bound(pred)) {
1688 * idx was Bounds checked in the same MacroBlock previously,
1689 * it is still valid if lower <= pred_lower && pred_upper <= upper.
1691 ir_node *lower = get_Bound_lower(n);
1692 ir_node *upper = get_Bound_upper(n);
1693 if (get_Bound_lower(pred) == lower &&
1694 get_Bound_upper(pred) == upper &&
1695 get_irn_MacroBlock(n) == get_irn_MacroBlock(pred)) {
1697 * One could expect that we simply return the previous
1698 * Bound here. However, this would be wrong, as we could
1699 * add an exception Proj to a new location then.
1700 * So, we must turn in into a tuple.
1706 /* Turn Bound into a tuple (mem, jmp, bad, idx) */
1707 ir_node *mem = get_Bound_mem(n);
1708 ir_node *blk = get_nodes_block(n);
1709 turn_into_tuple(n, pn_Bound_max);
1710 set_Tuple_pred(n, pn_Bound_M, mem);
1711 set_Tuple_pred(n, pn_Bound_X_regular, new_r_Jmp(current_ir_graph, blk)); /* no exception */
1712 set_Tuple_pred(n, pn_Bound_X_except, new_Bad()); /* no exception */
1713 set_Tuple_pred(n, pn_Bound_res, idx);
1716 } /* equivalent_node_Bound */
1719 * equivalent_node() returns a node equivalent to input n. It skips all nodes that
1720 * perform no actual computation, as, e.g., the Id nodes. It does not create
1721 * new nodes. It is therefore safe to free n if the node returned is not n.
1722 * If a node returns a Tuple we can not just skip it. If the size of the
1723 * in array fits, we transform n into a tuple (e.g., Div).
1725 ir_node *equivalent_node(ir_node *n) {
1726 if (n->op->ops.equivalent_node)
1727 return n->op->ops.equivalent_node(n);
1729 } /* equivalent_node */
1732 * Sets the default equivalent node operation for an ir_op_ops.
1734 * @param code the opcode for the default operation
1735 * @param ops the operations initialized
1740 static ir_op_ops *firm_set_default_equivalent_node(ir_opcode code, ir_op_ops *ops)
1744 ops->equivalent_node = equivalent_node_##a; \
1784 } /* firm_set_default_equivalent_node */
1787 * Returns non-zero if a node is a Phi node
1788 * with all predecessors constant.
1790 static int is_const_Phi(ir_node *n) {
1793 if (! is_Phi(n) || get_irn_arity(n) == 0)
1795 for (i = get_irn_arity(n) - 1; i >= 0; --i)
1796 if (! is_Const(get_irn_n(n, i)))
1799 } /* is_const_Phi */
1801 typedef tarval *(*tarval_sub_type)(tarval *a, tarval *b, ir_mode *mode);
1802 typedef tarval *(*tarval_binop_type)(tarval *a, tarval *b);
1805 * Wrapper for the tarval binop evaluation, tarval_sub has one more parameter.
1807 static tarval *do_eval(tarval *(*eval)(), tarval *a, tarval *b, ir_mode *mode) {
1808 if (eval == tarval_sub) {
1809 tarval_sub_type func = (tarval_sub_type)eval;
1811 return func(a, b, mode);
1813 tarval_binop_type func = (tarval_binop_type)eval;
1820 * Apply an evaluator on a binop with a constant operators (and one Phi).
1822 * @param phi the Phi node
1823 * @param other the other operand
1824 * @param eval an evaluator function
1825 * @param mode the mode of the result, may be different from the mode of the Phi!
1826 * @param left if non-zero, other is the left operand, else the right
1828 * @return a new Phi node if the conversion was successful, NULL else
1830 static ir_node *apply_binop_on_phi(ir_node *phi, tarval *other, tarval *(*eval)(), ir_mode *mode, int left) {
1835 int i, n = get_irn_arity(phi);
1837 NEW_ARR_A(void *, res, n);
1839 for (i = 0; i < n; ++i) {
1840 pred = get_irn_n(phi, i);
1841 tv = get_Const_tarval(pred);
1842 tv = do_eval(eval, other, tv, mode);
1844 if (tv == tarval_bad) {
1845 /* folding failed, bad */
1851 for (i = 0; i < n; ++i) {
1852 pred = get_irn_n(phi, i);
1853 tv = get_Const_tarval(pred);
1854 tv = do_eval(eval, tv, other, mode);
1856 if (tv == tarval_bad) {
1857 /* folding failed, bad */
1863 irg = current_ir_graph;
1864 for (i = 0; i < n; ++i) {
1865 pred = get_irn_n(phi, i);
1866 res[i] = new_r_Const_type(irg, get_irg_start_block(irg),
1867 mode, res[i], get_Const_type(pred));
1869 return new_r_Phi(irg, get_nodes_block(phi), n, (ir_node **)res, mode);
1870 } /* apply_binop_on_phi */
1873 * Apply an evaluator on a binop with two constant Phi.
1875 * @param a the left Phi node
1876 * @param b the right Phi node
1877 * @param eval an evaluator function
1878 * @param mode the mode of the result, may be different from the mode of the Phi!
1880 * @return a new Phi node if the conversion was successful, NULL else
1882 static ir_node *apply_binop_on_2_phis(ir_node *a, ir_node *b, tarval *(*eval)(), ir_mode *mode) {
1883 tarval *tv_l, *tv_r, *tv;
1889 if (get_nodes_block(a) != get_nodes_block(b))
1892 n = get_irn_arity(a);
1893 NEW_ARR_A(void *, res, n);
1895 for (i = 0; i < n; ++i) {
1896 pred = get_irn_n(a, i);
1897 tv_l = get_Const_tarval(pred);
1898 pred = get_irn_n(b, i);
1899 tv_r = get_Const_tarval(pred);
1900 tv = do_eval(eval, tv_l, tv_r, mode);
1902 if (tv == tarval_bad) {
1903 /* folding failed, bad */
1908 irg = current_ir_graph;
1909 for (i = 0; i < n; ++i) {
1910 pred = get_irn_n(a, i);
1911 res[i] = new_r_Const_type(irg, get_irg_start_block(irg), mode, res[i], get_Const_type(pred));
1913 return new_r_Phi(irg, get_nodes_block(a), n, (ir_node **)res, mode);
1914 } /* apply_binop_on_2_phis */
1917 * Apply an evaluator on a unop with a constant operator (a Phi).
1919 * @param phi the Phi node
1920 * @param eval an evaluator function
1922 * @return a new Phi node if the conversion was successful, NULL else
1924 static ir_node *apply_unop_on_phi(ir_node *phi, tarval *(*eval)(tarval *)) {
1930 int i, n = get_irn_arity(phi);
1932 NEW_ARR_A(void *, res, n);
1933 for (i = 0; i < n; ++i) {
1934 pred = get_irn_n(phi, i);
1935 tv = get_Const_tarval(pred);
1938 if (tv == tarval_bad) {
1939 /* folding failed, bad */
1944 mode = get_irn_mode(phi);
1945 irg = current_ir_graph;
1946 for (i = 0; i < n; ++i) {
1947 pred = get_irn_n(phi, i);
1948 res[i] = new_r_Const_type(irg, get_irg_start_block(irg),
1949 mode, res[i], get_Const_type(pred));
1951 return new_r_Phi(irg, get_nodes_block(phi), n, (ir_node **)res, mode);
1952 } /* apply_unop_on_phi */
1955 * Apply a conversion on a constant operator (a Phi).
1957 * @param phi the Phi node
1959 * @return a new Phi node if the conversion was successful, NULL else
1961 static ir_node *apply_conv_on_phi(ir_node *phi, ir_mode *mode) {
1966 int i, n = get_irn_arity(phi);
1968 NEW_ARR_A(void *, res, n);
1969 for (i = 0; i < n; ++i) {
1970 pred = get_irn_n(phi, i);
1971 tv = get_Const_tarval(pred);
1972 tv = tarval_convert_to(tv, mode);
1974 if (tv == tarval_bad) {
1975 /* folding failed, bad */
1980 irg = current_ir_graph;
1981 for (i = 0; i < n; ++i) {
1982 pred = get_irn_n(phi, i);
1983 res[i] = new_r_Const_type(irg, get_irg_start_block(irg),
1984 mode, res[i], get_Const_type(pred));
1986 return new_r_Phi(irg, get_nodes_block(phi), n, (ir_node **)res, mode);
1987 } /* apply_conv_on_phi */
1990 * Transform AddP(P, ConvIs(Iu)), AddP(P, ConvIu(Is)) and
1991 * SubP(P, ConvIs(Iu)), SubP(P, ConvIu(Is)).
1992 * If possible, remove the Conv's.
1994 static ir_node *transform_node_AddSub(ir_node *n) {
1995 ir_mode *mode = get_irn_mode(n);
1997 if (mode_is_reference(mode)) {
1998 ir_node *left = get_binop_left(n);
1999 ir_node *right = get_binop_right(n);
2000 unsigned ref_bits = get_mode_size_bits(mode);
2002 if (is_Conv(left)) {
2003 ir_mode *lmode = get_irn_mode(left);
2004 unsigned bits = get_mode_size_bits(lmode);
2006 if (ref_bits == bits &&
2007 mode_is_int(lmode) &&
2008 get_mode_arithmetic(lmode) == irma_twos_complement) {
2009 ir_node *pre = get_Conv_op(left);
2010 ir_mode *pre_mode = get_irn_mode(pre);
2012 if (mode_is_int(pre_mode) &&
2013 get_mode_size_bits(pre_mode) == bits &&
2014 get_mode_arithmetic(pre_mode) == irma_twos_complement) {
2015 /* ok, this conv just changes to sign, moreover the calculation
2016 * is done with same number of bits as our address mode, so
2017 * we can ignore the conv as address calculation can be viewed
2018 * as either signed or unsigned
2020 set_binop_left(n, pre);
2025 if (is_Conv(right)) {
2026 ir_mode *rmode = get_irn_mode(right);
2027 unsigned bits = get_mode_size_bits(rmode);
2029 if (ref_bits == bits &&
2030 mode_is_int(rmode) &&
2031 get_mode_arithmetic(rmode) == irma_twos_complement) {
2032 ir_node *pre = get_Conv_op(right);
2033 ir_mode *pre_mode = get_irn_mode(pre);
2035 if (mode_is_int(pre_mode) &&
2036 get_mode_size_bits(pre_mode) == bits &&
2037 get_mode_arithmetic(pre_mode) == irma_twos_complement) {
2038 /* ok, this conv just changes to sign, moreover the calculation
2039 * is done with same number of bits as our address mode, so
2040 * we can ignore the conv as address calculation can be viewed
2041 * as either signed or unsigned
2043 set_binop_right(n, pre);
2048 /* let address arithmetic use unsigned modes */
2049 if (is_Const(right)) {
2050 ir_mode *rmode = get_irn_mode(right);
2052 if (mode_is_signed(rmode) && get_mode_arithmetic(rmode) == irma_twos_complement) {
2053 /* convert a AddP(P, *s) into AddP(P, *u) */
2054 ir_mode *nm = get_reference_mode_unsigned_eq(mode);
2056 ir_node *pre = new_r_Conv(current_ir_graph, get_nodes_block(n), right, nm);
2057 set_binop_right(n, pre);
2063 } /* transform_node_AddSub */
2065 #define HANDLE_BINOP_PHI(eval, a, b, c, mode) \
2067 if (is_Const(b) && is_const_Phi(a)) { \
2068 /* check for Op(Phi, Const) */ \
2069 c = apply_binop_on_phi(a, get_Const_tarval(b), eval, mode, 0);\
2071 else if (is_Const(a) && is_const_Phi(b)) { \
2072 /* check for Op(Const, Phi) */ \
2073 c = apply_binop_on_phi(b, get_Const_tarval(a), eval, mode, 1);\
2075 else if (is_const_Phi(a) && is_const_Phi(b)) { \
2076 /* check for Op(Phi, Phi) */ \
2077 c = apply_binop_on_2_phis(a, b, eval, mode); \
2080 DBG_OPT_ALGSIM0(oldn, c, FS_OPT_CONST_PHI); \
2084 #define HANDLE_UNOP_PHI(eval, a, c) \
2086 if (is_const_Phi(a)) { \
2087 /* check for Op(Phi) */ \
2088 c = apply_unop_on_phi(a, eval); \
2090 DBG_OPT_ALGSIM0(oldn, c, FS_OPT_CONST_PHI); \
2096 * Do the AddSub optimization, then Transform
2097 * Constant folding on Phi
2098 * Add(a,a) -> Mul(a, 2)
2099 * Add(Mul(a, x), a) -> Mul(a, x+1)
2100 * if the mode is integer or float.
2101 * Transform Add(a,-b) into Sub(a,b).
2102 * Reassociation might fold this further.
2104 static ir_node *transform_node_Add(ir_node *n) {
2106 ir_node *a, *b, *c, *oldn = n;
2108 n = transform_node_AddSub(n);
2110 a = get_Add_left(n);
2111 b = get_Add_right(n);
2113 mode = get_irn_mode(n);
2115 if (mode_is_reference(mode)) {
2116 ir_mode *lmode = get_irn_mode(a);
2118 if (is_Const(b) && is_Const_null(b) && mode_is_int(lmode)) {
2119 /* an Add(a, NULL) is a hidden Conv */
2120 dbg_info *dbg = get_irn_dbg_info(n);
2121 return new_rd_Conv(dbg, current_ir_graph, get_nodes_block(n), a, mode);
2125 HANDLE_BINOP_PHI(tarval_add, a, b, c, mode);
2127 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
2128 if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
2131 if (mode_is_num(mode)) {
2132 /* the following code leads to endless recursion when Mul are replaced by a simple instruction chain */
2133 if (!is_arch_dep_running() && a == b && mode_is_int(mode)) {
2134 ir_node *block = get_nodes_block(n);
2137 get_irn_dbg_info(n),
2141 new_r_Const_long(current_ir_graph, block, mode, 2),
2143 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_A);
2148 get_irn_dbg_info(n),
2154 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_MINUS_B);
2159 get_irn_dbg_info(n),
2165 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_MINUS_B);
2168 if (get_mode_arithmetic(mode) == irma_twos_complement) {
2169 /* Here we rely on constants be on the RIGHT side */
2171 ir_node *op = get_Not_op(a);
2173 if (is_Const(b) && is_Const_one(b)) {
2175 ir_node *blk = get_irn_n(n, -1);
2176 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, blk, op, mode);
2177 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_PLUS_1);
2182 ir_node *blk = get_irn_n(n, -1);
2183 n = new_r_Const(current_ir_graph, blk, mode, get_mode_minus_one(mode));
2184 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_X_NOT_X);
2189 ir_node *op = get_Not_op(b);
2193 ir_node *blk = get_irn_n(n, -1);
2194 n = new_r_Const(current_ir_graph, blk, mode, get_mode_minus_one(mode));
2195 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_X_NOT_X);
2202 } /* transform_node_Add */
2205 * returns -cnst or NULL if impossible
2207 static ir_node *const_negate(ir_node *cnst) {
2208 tarval *tv = tarval_neg(get_Const_tarval(cnst));
2209 dbg_info *dbgi = get_irn_dbg_info(cnst);
2210 ir_graph *irg = get_irn_irg(cnst);
2211 ir_node *block = get_nodes_block(cnst);
2212 ir_mode *mode = get_irn_mode(cnst);
2213 if (tv == tarval_bad) return NULL;
2214 return new_rd_Const(dbgi, irg, block, mode, tv);
2218 * Do the AddSub optimization, then Transform
2219 * Constant folding on Phi
2220 * Sub(0,a) -> Minus(a)
2221 * Sub(Mul(a, x), a) -> Mul(a, x-1)
2222 * Sub(Sub(x, y), b) -> Sub(x, Add(y,b))
2223 * Sub(Add(a, x), x) -> a
2224 * Sub(x, Add(x, a)) -> -a
2225 * Sub(x, Const) -> Add(x, -Const)
2227 static ir_node *transform_node_Sub(ir_node *n) {
2232 n = transform_node_AddSub(n);
2234 a = get_Sub_left(n);
2235 b = get_Sub_right(n);
2237 mode = get_irn_mode(n);
2239 if (mode_is_int(mode)) {
2240 ir_mode *lmode = get_irn_mode(a);
2242 if (is_Const(b) && is_Const_null(b) && mode_is_reference(lmode)) {
2243 /* a Sub(a, NULL) is a hidden Conv */
2244 dbg_info *dbg = get_irn_dbg_info(n);
2245 return new_rd_Conv(dbg, current_ir_graph, get_nodes_block(n), a, mode);
2250 HANDLE_BINOP_PHI(tarval_sub, a, b, c, mode);
2252 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
2253 if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
2256 if (is_Const(b) && get_irn_mode(b) != mode_P) {
2257 /* a - C -> a + (-C) */
2258 ir_node *cnst = const_negate(b);
2260 ir_node *block = get_nodes_block(n);
2261 dbg_info *dbgi = get_irn_dbg_info(n);
2262 ir_graph *irg = get_irn_irg(n);
2264 n = new_rd_Add(dbgi, irg, block, a, cnst, mode);
2265 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2270 if (is_Minus(a)) { /* (-a) - b -> -(a + b) */
2271 ir_graph *irg = current_ir_graph;
2272 dbg_info *dbg = get_irn_dbg_info(n);
2273 ir_node *block = get_nodes_block(n);
2274 ir_node *left = get_Minus_op(a);
2275 ir_node *add = new_rd_Add(dbg, irg, block, left, b, mode);
2277 n = new_rd_Minus(dbg, irg, block, add, mode);
2278 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2280 } else if (is_Minus(b)) { /* a - (-b) -> a + b */
2281 ir_graph *irg = current_ir_graph;
2282 dbg_info *dbg = get_irn_dbg_info(n);
2283 ir_node *block = get_nodes_block(n);
2284 ir_node *right = get_Minus_op(b);
2286 n = new_rd_Add(dbg, irg, block, a, right, mode);
2287 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MINUS);
2289 } else if (is_Sub(b)) { /* a - (b - c) -> a + (c - b) */
2290 ir_graph *irg = current_ir_graph;
2291 dbg_info *s_dbg = get_irn_dbg_info(b);
2292 ir_node *s_block = get_nodes_block(b);
2293 ir_node *s_left = get_Sub_right(b);
2294 ir_node *s_right = get_Sub_left(b);
2295 ir_mode *s_mode = get_irn_mode(b);
2296 ir_node *sub = new_rd_Sub(s_dbg, irg, s_block, s_left, s_right, s_mode);
2297 dbg_info *a_dbg = get_irn_dbg_info(n);
2298 ir_node *a_block = get_nodes_block(n);
2300 n = new_rd_Add(a_dbg, irg, a_block, a, sub, mode);
2301 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2303 } else if (is_Mul(b)) { /* a - (b * C) -> a + (b * -C) */
2304 ir_node *m_right = get_Mul_right(b);
2305 if (is_Const(m_right)) {
2306 ir_node *cnst2 = const_negate(m_right);
2307 if (cnst2 != NULL) {
2308 ir_graph *irg = current_ir_graph;
2309 dbg_info *m_dbg = get_irn_dbg_info(b);
2310 ir_node *m_block = get_nodes_block(b);
2311 ir_node *m_left = get_Mul_left(b);
2312 ir_mode *m_mode = get_irn_mode(b);
2313 ir_node *mul = new_rd_Mul(m_dbg, irg, m_block, m_left, cnst2, m_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, mul, mode);
2318 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2324 /* Beware of Sub(P, P) which cannot be optimized into a simple Minus ... */
2325 if (mode_is_num(mode) && mode == get_irn_mode(a) && is_Const(a) && is_Const_null(a)) {
2327 get_irn_dbg_info(n),
2332 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_0_A);
2336 if (mode_wrap_around(mode)) {
2337 ir_node *left = get_Add_left(a);
2338 ir_node *right = get_Add_right(a);
2340 /* FIXME: Does the Conv's work only for two complement or generally? */
2342 if (mode != get_irn_mode(right)) {
2343 /* This Sub is an effective Cast */
2344 right = new_r_Conv(get_irn_irg(n), get_irn_n(n, -1), right, mode);
2347 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2349 } else if (right == b) {
2350 if (mode != get_irn_mode(left)) {
2351 /* This Sub is an effective Cast */
2352 left = new_r_Conv(get_irn_irg(n), get_irn_n(n, -1), left, mode);
2355 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2361 if (mode_wrap_around(mode)) {
2362 ir_node *left = get_Add_left(b);
2363 ir_node *right = get_Add_right(b);
2365 /* FIXME: Does the Conv's work only for two complement or generally? */
2367 ir_mode *r_mode = get_irn_mode(right);
2369 n = new_r_Minus(get_irn_irg(n), get_irn_n(n, -1), right, r_mode);
2370 if (mode != r_mode) {
2371 /* This Sub is an effective Cast */
2372 n = new_r_Conv(get_irn_irg(n), get_irn_n(n, -1), n, mode);
2374 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2376 } else if (right == a) {
2377 ir_mode *l_mode = get_irn_mode(left);
2379 n = new_r_Minus(get_irn_irg(n), get_irn_n(n, -1), left, l_mode);
2380 if (mode != l_mode) {
2381 /* This Sub is an effective Cast */
2382 n = new_r_Conv(get_irn_irg(n), get_irn_n(n, -1), n, mode);
2384 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2389 if (mode_is_int(mode) && is_Conv(a) && is_Conv(b)) {
2390 ir_mode *mode = get_irn_mode(a);
2392 if (mode == get_irn_mode(b)) {
2394 ir_node *op_a = get_Conv_op(a);
2395 ir_node *op_b = get_Conv_op(b);
2397 /* check if it's allowed to skip the conv */
2398 ma = get_irn_mode(op_a);
2399 mb = get_irn_mode(op_b);
2401 if (mode_is_reference(ma) && mode_is_reference(mb)) {
2402 /* SubInt(ConvInt(aP), ConvInt(bP)) -> SubInt(aP,bP) */
2405 set_Sub_right(n, b);
2411 /* do NOT execute this code if reassociation is enabled, it does the inverse! */
2412 if (!is_reassoc_running() && is_Mul(a)) {
2413 ir_node *ma = get_Mul_left(a);
2414 ir_node *mb = get_Mul_right(a);
2417 ir_node *blk = get_irn_n(n, -1);
2419 get_irn_dbg_info(n),
2420 current_ir_graph, blk,
2423 get_irn_dbg_info(n),
2424 current_ir_graph, blk,
2426 new_r_Const_long(current_ir_graph, blk, mode, 1),
2429 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MUL_A_X_A);
2431 } else if (mb == b) {
2432 ir_node *blk = get_irn_n(n, -1);
2434 get_irn_dbg_info(n),
2435 current_ir_graph, blk,
2438 get_irn_dbg_info(n),
2439 current_ir_graph, blk,
2441 new_r_Const_long(current_ir_graph, blk, mode, 1),
2444 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MUL_A_X_A);
2448 if (is_Sub(a)) { /* (x - y) - b -> x - (y + b) */
2449 ir_node *x = get_Sub_left(a);
2450 ir_node *y = get_Sub_right(a);
2451 ir_node *blk = get_irn_n(n, -1);
2452 ir_mode *m_b = get_irn_mode(b);
2453 ir_mode *m_y = get_irn_mode(y);
2457 /* Determine the right mode for the Add. */
2460 else if (mode_is_reference(m_b))
2462 else if (mode_is_reference(m_y))
2466 * Both modes are different but none is reference,
2467 * happens for instance in SubP(SubP(P, Iu), Is).
2468 * We have two possibilities here: Cast or ignore.
2469 * Currently we ignore this case.
2474 add = new_r_Add(current_ir_graph, blk, y, b, add_mode);
2476 n = new_rd_Sub(get_irn_dbg_info(n), current_ir_graph, blk, x, add, mode);
2477 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_SUB_X_Y_Z);
2481 if (get_mode_arithmetic(mode) == irma_twos_complement) {
2482 if (is_Const(a) && is_Not(b)) {
2483 /* c - ~X = X + (c+1) */
2484 tarval *tv = get_Const_tarval(a);
2486 tv = tarval_add(tv, get_mode_one(mode));
2487 if (tv != tarval_bad) {
2488 ir_node *blk = get_irn_n(n, -1);
2489 ir_node *c = new_r_Const(current_ir_graph, blk, mode, tv);
2490 n = new_rd_Add(get_irn_dbg_info(n), current_ir_graph, blk, get_Not_op(b), c, mode);
2491 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_C_NOT_X);
2497 } /* transform_node_Sub */
2500 * Several transformation done on n*n=2n bits mul.
2501 * These transformations must be done here because new nodes may be produced.
2503 static ir_node *transform_node_Mul2n(ir_node *n, ir_mode *mode) {
2505 ir_node *a = get_Mul_left(n);
2506 ir_node *b = get_Mul_right(n);
2507 tarval *ta = value_of(a);
2508 tarval *tb = value_of(b);
2509 ir_mode *smode = get_irn_mode(a);
2511 if (ta == get_mode_one(smode)) {
2512 /* (L)1 * (L)b = (L)b */
2513 ir_node *blk = get_irn_n(n, -1);
2514 n = new_rd_Conv(get_irn_dbg_info(n), current_ir_graph, blk, b, mode);
2515 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
2518 else if (ta == get_mode_minus_one(smode)) {
2519 /* (L)-1 * (L)b = (L)b */
2520 ir_node *blk = get_irn_n(n, -1);
2521 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, blk, b, smode);
2522 n = new_rd_Conv(get_irn_dbg_info(n), current_ir_graph, blk, n, mode);
2523 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2526 if (tb == get_mode_one(smode)) {
2527 /* (L)a * (L)1 = (L)a */
2528 ir_node *blk = get_irn_n(a, -1);
2529 n = new_rd_Conv(get_irn_dbg_info(n), current_ir_graph, blk, a, mode);
2530 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
2533 else if (tb == get_mode_minus_one(smode)) {
2534 /* (L)a * (L)-1 = (L)-a */
2535 ir_node *blk = get_irn_n(n, -1);
2536 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, blk, a, smode);
2537 n = new_rd_Conv(get_irn_dbg_info(n), current_ir_graph, blk, n, mode);
2538 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2545 * Transform Mul(a,-1) into -a.
2546 * Do constant evaluation of Phi nodes.
2547 * Do architecture dependent optimizations on Mul nodes
2549 static ir_node *transform_node_Mul(ir_node *n) {
2550 ir_node *c, *oldn = n;
2551 ir_mode *mode = get_irn_mode(n);
2552 ir_node *a = get_Mul_left(n);
2553 ir_node *b = get_Mul_right(n);
2555 if (is_Bad(a) || is_Bad(b))
2558 if (mode != get_irn_mode(a))
2559 return transform_node_Mul2n(n, mode);
2561 HANDLE_BINOP_PHI(tarval_mul, a, b, c, mode);
2563 if (mode_is_signed(mode)) {
2566 if (value_of(a) == get_mode_minus_one(mode))
2568 else if (value_of(b) == get_mode_minus_one(mode))
2571 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1), r, mode);
2572 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2577 if (is_Const(b)) { /* (-a) * const -> a * -const */
2578 ir_node *cnst = const_negate(b);
2580 dbg_info *dbgi = get_irn_dbg_info(n);
2581 ir_node *block = get_nodes_block(n);
2582 n = new_rd_Mul(dbgi, current_ir_graph, block, get_Minus_op(a), cnst, mode);
2583 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2586 } else if (is_Minus(b)) { /* (-a) * (-b) -> a * b */
2587 dbg_info *dbgi = get_irn_dbg_info(n);
2588 ir_node *block = get_nodes_block(n);
2589 n = new_rd_Mul(dbgi, current_ir_graph, block, get_Minus_op(a), get_Minus_op(b), mode);
2590 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_MINUS);
2592 } else if (is_Sub(b)) { /* (-a) * (b - c) -> a * (c - b) */
2593 ir_node *sub_l = get_Sub_left(b);
2594 ir_node *sub_r = get_Sub_right(b);
2595 dbg_info *dbgi = get_irn_dbg_info(n);
2596 ir_graph *irg = current_ir_graph;
2597 ir_node *block = get_nodes_block(n);
2598 ir_node *new_b = new_rd_Sub(dbgi, irg, block, sub_r, sub_l, mode);
2599 n = new_rd_Mul(dbgi, irg, block, get_Minus_op(a), new_b, mode);
2600 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS);
2603 } else if (is_Minus(b)) {
2604 if (is_Sub(a)) { /* (a - b) * (-c) -> (b - a) * c */
2605 ir_node *sub_l = get_Sub_left(a);
2606 ir_node *sub_r = get_Sub_right(a);
2607 dbg_info *dbgi = get_irn_dbg_info(n);
2608 ir_graph *irg = current_ir_graph;
2609 ir_node *block = get_nodes_block(n);
2610 ir_node *new_a = new_rd_Sub(dbgi, irg, block, sub_r, sub_l, mode);
2611 n = new_rd_Mul(dbgi, irg, block, new_a, get_Minus_op(b), mode);
2612 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS);
2616 if (get_mode_arithmetic(mode) == irma_ieee754) {
2618 tarval *tv = get_Const_tarval(a);
2619 if (tarval_ieee754_get_exponent(tv) == 1 && tarval_ieee754_zero_mantissa(tv)) {
2620 /* 2.0 * b = b + b */
2621 n = new_rd_Add(get_irn_dbg_info(n), current_ir_graph, get_nodes_block(n), b, b, mode);
2622 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_A_A);
2626 else if (is_Const(b)) {
2627 tarval *tv = get_Const_tarval(b);
2628 if (tarval_ieee754_get_exponent(tv) == 1 && tarval_ieee754_zero_mantissa(tv)) {
2629 /* a * 2.0 = a + a */
2630 n = new_rd_Add(get_irn_dbg_info(n), current_ir_graph, get_nodes_block(n), a, a, mode);
2631 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_A_A);
2636 return arch_dep_replace_mul_with_shifts(n);
2637 } /* transform_node_Mul */
2640 * Transform a Div Node.
2642 static ir_node *transform_node_Div(ir_node *n) {
2643 ir_mode *mode = get_Div_resmode(n);
2644 ir_node *a = get_Div_left(n);
2645 ir_node *b = get_Div_right(n);
2649 if (is_Const(b) && is_const_Phi(a)) {
2650 /* check for Div(Phi, Const) */
2651 value = apply_binop_on_phi(a, get_Const_tarval(b), tarval_div, mode, 0);
2653 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2657 else if (is_Const(a) && is_const_Phi(b)) {
2658 /* check for Div(Const, Phi) */
2659 value = apply_binop_on_phi(b, get_Const_tarval(a), tarval_div, mode, 1);
2661 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2665 else if (is_const_Phi(a) && is_const_Phi(b)) {
2666 /* check for Div(Phi, Phi) */
2667 value = apply_binop_on_2_phis(a, b, tarval_div, mode);
2669 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2676 if (tv != tarval_bad) {
2677 value = new_Const(get_tarval_mode(tv), tv);
2679 DBG_OPT_CSTEVAL(n, value);
2682 ir_node *a = get_Div_left(n);
2683 ir_node *b = get_Div_right(n);
2686 if (a == b && value_not_zero(a, &dummy)) {
2687 /* BEWARE: we can optimize a/a to 1 only if this cannot cause a exception */
2688 value = new_Const(mode, get_mode_one(mode));
2689 DBG_OPT_CSTEVAL(n, value);
2692 if (mode_is_signed(mode) && is_Const(b)) {
2693 tarval *tv = get_Const_tarval(b);
2695 if (tv == get_mode_minus_one(mode)) {
2697 value = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1), a, mode);
2698 DBG_OPT_CSTEVAL(n, value);
2702 /* Try architecture dependent optimization */
2703 value = arch_dep_replace_div_by_const(n);
2711 /* Turn Div into a tuple (mem, jmp, bad, value) */
2712 mem = get_Div_mem(n);
2713 blk = get_irn_n(n, -1);
2715 /* skip a potential Pin */
2717 mem = get_Pin_op(mem);
2718 turn_into_tuple(n, pn_Div_max);
2719 set_Tuple_pred(n, pn_Div_M, mem);
2720 set_Tuple_pred(n, pn_Div_X_regular, new_r_Jmp(current_ir_graph, blk));
2721 set_Tuple_pred(n, pn_Div_X_except, new_Bad());
2722 set_Tuple_pred(n, pn_Div_res, value);
2725 } /* transform_node_Div */
2728 * Transform a Mod node.
2730 static ir_node *transform_node_Mod(ir_node *n) {
2731 ir_mode *mode = get_Mod_resmode(n);
2732 ir_node *a = get_Mod_left(n);
2733 ir_node *b = get_Mod_right(n);
2737 if (is_Const(b) && is_const_Phi(a)) {
2738 /* check for Div(Phi, Const) */
2739 value = apply_binop_on_phi(a, get_Const_tarval(b), tarval_mod, mode, 0);
2741 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2745 else if (is_Const(a) && is_const_Phi(b)) {
2746 /* check for Div(Const, Phi) */
2747 value = apply_binop_on_phi(b, get_Const_tarval(a), tarval_mod, mode, 1);
2749 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2753 else if (is_const_Phi(a) && is_const_Phi(b)) {
2754 /* check for Div(Phi, Phi) */
2755 value = apply_binop_on_2_phis(a, b, tarval_mod, mode);
2757 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2764 if (tv != tarval_bad) {
2765 value = new_Const(get_tarval_mode(tv), tv);
2767 DBG_OPT_CSTEVAL(n, value);
2770 ir_node *a = get_Mod_left(n);
2771 ir_node *b = get_Mod_right(n);
2774 if (a == b && value_not_zero(a, &dummy)) {
2775 /* BEWARE: we can optimize a%a to 0 only if this cannot cause a exception */
2776 value = new_Const(mode, get_mode_null(mode));
2777 DBG_OPT_CSTEVAL(n, value);
2780 if (mode_is_signed(mode) && is_Const(b)) {
2781 tarval *tv = get_Const_tarval(b);
2783 if (tv == get_mode_minus_one(mode)) {
2785 value = new_Const(mode, get_mode_null(mode));
2786 DBG_OPT_CSTEVAL(n, value);
2790 /* Try architecture dependent optimization */
2791 value = arch_dep_replace_mod_by_const(n);
2799 /* Turn Mod into a tuple (mem, jmp, bad, value) */
2800 mem = get_Mod_mem(n);
2801 blk = get_irn_n(n, -1);
2803 /* skip a potential Pin */
2805 mem = get_Pin_op(mem);
2806 turn_into_tuple(n, pn_Mod_max);
2807 set_Tuple_pred(n, pn_Mod_M, mem);
2808 set_Tuple_pred(n, pn_Mod_X_regular, new_r_Jmp(current_ir_graph, blk));
2809 set_Tuple_pred(n, pn_Mod_X_except, new_Bad());
2810 set_Tuple_pred(n, pn_Mod_res, value);
2813 } /* transform_node_Mod */
2816 * Transform a DivMod node.
2818 static ir_node *transform_node_DivMod(ir_node *n) {
2820 ir_node *a = get_DivMod_left(n);
2821 ir_node *b = get_DivMod_right(n);
2822 ir_mode *mode = get_DivMod_resmode(n);
2827 if (is_Const(b) && is_const_Phi(a)) {
2828 /* check for Div(Phi, Const) */
2829 va = apply_binop_on_phi(a, get_Const_tarval(b), tarval_div, mode, 0);
2830 vb = apply_binop_on_phi(a, get_Const_tarval(b), tarval_mod, mode, 0);
2832 DBG_OPT_ALGSIM0(n, va, FS_OPT_CONST_PHI);
2833 DBG_OPT_ALGSIM0(n, vb, FS_OPT_CONST_PHI);
2837 else if (is_Const(a) && is_const_Phi(b)) {
2838 /* check for Div(Const, Phi) */
2839 va = apply_binop_on_phi(b, get_Const_tarval(a), tarval_div, mode, 1);
2840 vb = apply_binop_on_phi(b, get_Const_tarval(a), tarval_mod, mode, 1);
2842 DBG_OPT_ALGSIM0(n, va, FS_OPT_CONST_PHI);
2843 DBG_OPT_ALGSIM0(n, vb, FS_OPT_CONST_PHI);
2847 else if (is_const_Phi(a) && is_const_Phi(b)) {
2848 /* check for Div(Phi, Phi) */
2849 va = apply_binop_on_2_phis(a, b, tarval_div, mode);
2850 vb = apply_binop_on_2_phis(a, b, tarval_mod, mode);
2852 DBG_OPT_ALGSIM0(n, va, FS_OPT_CONST_PHI);
2853 DBG_OPT_ALGSIM0(n, vb, FS_OPT_CONST_PHI);
2860 if (tb != tarval_bad) {
2861 if (tb == get_mode_one(get_tarval_mode(tb))) {
2863 vb = new_Const(mode, get_mode_null(mode));
2864 DBG_OPT_CSTEVAL(n, vb);
2866 } else if (ta != tarval_bad) {
2867 tarval *resa, *resb;
2868 resa = tarval_div(ta, tb);
2869 if (resa == tarval_bad) return n; /* Causes exception!!! Model by replacing through
2870 Jmp for X result!? */
2871 resb = tarval_mod(ta, tb);
2872 if (resb == tarval_bad) return n; /* Causes exception! */
2873 va = new_Const(mode, resa);
2874 vb = new_Const(mode, resb);
2875 DBG_OPT_CSTEVAL(n, va);
2876 DBG_OPT_CSTEVAL(n, vb);
2878 } else if (mode_is_signed(mode) && tb == get_mode_minus_one(mode)) {
2879 va = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1), a, mode);
2880 vb = new_Const(mode, get_mode_null(mode));
2881 DBG_OPT_CSTEVAL(n, va);
2882 DBG_OPT_CSTEVAL(n, vb);
2884 } else { /* Try architecture dependent optimization */
2887 arch_dep_replace_divmod_by_const(&va, &vb, n);
2888 evaluated = va != NULL;
2890 } else if (a == b) {
2891 if (value_not_zero(a, &dummy)) {
2893 va = new_Const(mode, get_mode_one(mode));
2894 vb = new_Const(mode, get_mode_null(mode));
2895 DBG_OPT_CSTEVAL(n, va);
2896 DBG_OPT_CSTEVAL(n, vb);
2899 /* BEWARE: it is NOT possible to optimize a/a to 1, as this may cause a exception */
2902 } else if (ta == get_mode_null(mode) && value_not_zero(b, &dummy)) {
2903 /* 0 / non-Const = 0 */
2908 if (evaluated) { /* replace by tuple */
2912 mem = get_DivMod_mem(n);
2913 /* skip a potential Pin */
2915 mem = get_Pin_op(mem);
2917 blk = get_irn_n(n, -1);
2918 turn_into_tuple(n, pn_DivMod_max);
2919 set_Tuple_pred(n, pn_DivMod_M, mem);
2920 set_Tuple_pred(n, pn_DivMod_X_regular, new_r_Jmp(current_ir_graph, blk));
2921 set_Tuple_pred(n, pn_DivMod_X_except, new_Bad()); /* no exception */
2922 set_Tuple_pred(n, pn_DivMod_res_div, va);
2923 set_Tuple_pred(n, pn_DivMod_res_mod, vb);
2927 } /* transform_node_DivMod */
2930 * Optimize x / c to x * (1/c)
2932 static ir_node *transform_node_Quot(ir_node *n) {
2933 ir_mode *mode = get_Quot_resmode(n);
2936 if (get_mode_arithmetic(mode) == irma_ieee754) {
2937 ir_node *b = get_Quot_right(n);
2940 tarval *tv = get_Const_tarval(b);
2944 * Floating point constant folding might be disabled here to
2946 * However, as we check for exact result, doing it is safe.
2949 rem = tarval_enable_fp_ops(1);
2950 tv = tarval_quo(get_mode_one(mode), tv);
2951 (void)tarval_enable_fp_ops(rem);
2953 /* Do the transformation if the result is either exact or we are not
2954 using strict rules. */
2955 if (tv != tarval_bad &&
2956 (tarval_ieee754_get_exact() || (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic) == 0)) {
2957 ir_node *blk = get_irn_n(n, -1);
2958 ir_node *c = new_r_Const(current_ir_graph, blk, mode, tv);
2959 ir_node *a = get_Quot_left(n);
2960 ir_node *m = new_rd_Mul(get_irn_dbg_info(n), current_ir_graph, blk, a, c, mode);
2961 ir_node *mem = get_Quot_mem(n);
2963 /* skip a potential Pin */
2965 mem = get_Pin_op(mem);
2966 turn_into_tuple(n, pn_Quot_max);
2967 set_Tuple_pred(n, pn_Quot_M, mem);
2968 set_Tuple_pred(n, pn_Quot_X_regular, new_r_Jmp(current_ir_graph, blk));
2969 set_Tuple_pred(n, pn_Quot_X_except, new_r_Bad(current_ir_graph));
2970 set_Tuple_pred(n, pn_Quot_res, m);
2971 DBG_OPT_ALGSIM1(oldn, a, b, m, FS_OPT_FP_INV_MUL);
2976 } /* transform_node_Quot */
2979 * Optimize Abs(x) into x if x is Confirmed >= 0
2980 * Optimize Abs(x) into -x if x is Confirmed <= 0
2981 * Optimize Abs(-x) int Abs(x)
2983 static ir_node *transform_node_Abs(ir_node *n) {
2984 ir_node *c, *oldn = n;
2985 ir_node *a = get_Abs_op(n);
2988 HANDLE_UNOP_PHI(tarval_abs, a, c);
2990 switch (classify_value_sign(a)) {
2991 case value_classified_negative:
2992 mode = get_irn_mode(n);
2995 * We can replace the Abs by -x here.
2996 * We even could add a new Confirm here
2997 * (if not twos complement)
2999 * Note that -x would create a new node, so we could
3000 * not run it in the equivalent_node() context.
3002 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph,
3003 get_nodes_block(n), a, mode);
3005 DBG_OPT_CONFIRM(oldn, n);
3007 case value_classified_positive:
3008 /* n is positive, Abs is not needed */
3011 DBG_OPT_CONFIRM(oldn, n);
3017 /* Abs(-x) = Abs(x) */
3018 mode = get_irn_mode(n);
3019 n = new_rd_Abs(get_irn_dbg_info(n), current_ir_graph,
3020 get_nodes_block(n), get_Minus_op(a), mode);
3021 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ABS_MINUS_X);
3025 } /* transform_node_Abs */
3028 * Transform a Cond node.
3030 * Replace the Cond by a Jmp if it branches on a constant
3033 static ir_node *transform_node_Cond(ir_node *n) {
3036 ir_node *a = get_Cond_selector(n);
3037 tarval *ta = value_of(a);
3039 /* we need block info which is not available in floating irgs */
3040 if (get_irg_pinned(current_ir_graph) == op_pin_state_floats)
3043 if ((ta != tarval_bad) &&
3044 (get_irn_mode(a) == mode_b) &&
3045 (get_opt_unreachable_code())) {
3046 /* It's a boolean Cond, branching on a boolean constant.
3047 Replace it by a tuple (Bad, Jmp) or (Jmp, Bad) */
3048 ir_node *blk = get_nodes_block(n);
3049 jmp = new_r_Jmp(current_ir_graph, blk);
3050 turn_into_tuple(n, pn_Cond_max);
3051 if (ta == tarval_b_true) {
3052 set_Tuple_pred(n, pn_Cond_false, new_Bad());
3053 set_Tuple_pred(n, pn_Cond_true, jmp);
3055 set_Tuple_pred(n, pn_Cond_false, jmp);
3056 set_Tuple_pred(n, pn_Cond_true, new_Bad());
3058 /* We might generate an endless loop, so keep it alive. */
3059 add_End_keepalive(get_irg_end(current_ir_graph), blk);
3062 } /* transform_node_Cond */
3065 * Prototype of a recursive transform function
3066 * for bitwise distributive transformations.
3068 typedef ir_node* (*recursive_transform)(ir_node *n);
3071 * makes use of distributive laws for and, or, eor
3072 * and(a OP c, b OP c) -> and(a, b) OP c
3073 * note, might return a different op than n
3075 static ir_node *transform_bitwise_distributive(ir_node *n,
3076 recursive_transform trans_func)
3079 ir_node *a = get_binop_left(n);
3080 ir_node *b = get_binop_right(n);
3081 ir_op *op = get_irn_op(a);
3082 ir_op *op_root = get_irn_op(n);
3084 if(op != get_irn_op(b))
3087 if (op == op_Conv) {
3088 ir_node *a_op = get_Conv_op(a);
3089 ir_node *b_op = get_Conv_op(b);
3090 ir_mode *a_mode = get_irn_mode(a_op);
3091 ir_mode *b_mode = get_irn_mode(b_op);
3092 if(a_mode == b_mode && (mode_is_int(a_mode) || a_mode == mode_b)) {
3093 ir_node *blk = get_irn_n(n, -1);
3096 set_binop_left(n, a_op);
3097 set_binop_right(n, b_op);
3098 set_irn_mode(n, a_mode);
3100 n = new_r_Conv(current_ir_graph, blk, n, get_irn_mode(oldn));
3102 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_SHIFT_AND);
3108 /* nothing to gain here */
3112 if (op == op_Shrs || op == op_Shr || op == op_Shl
3113 || op == op_And || op == op_Or || op == op_Eor) {
3114 ir_node *a_left = get_binop_left(a);
3115 ir_node *a_right = get_binop_right(a);
3116 ir_node *b_left = get_binop_left(b);
3117 ir_node *b_right = get_binop_right(b);
3119 ir_node *op1 = NULL;
3120 ir_node *op2 = NULL;
3122 if (is_op_commutative(op)) {
3123 if (a_left == b_left) {
3127 } else if(a_left == b_right) {
3131 } else if(a_right == b_left) {
3137 if(a_right == b_right) {
3144 /* (a sop c) & (b sop c) => (a & b) sop c */
3145 ir_node *blk = get_irn_n(n, -1);
3147 ir_node *new_n = exact_copy(n);
3148 set_binop_left(new_n, op1);
3149 set_binop_right(new_n, op2);
3150 new_n = trans_func(new_n);
3152 if(op_root == op_Eor && op == op_Or) {
3153 dbg_info *dbgi = get_irn_dbg_info(n);
3154 ir_graph *irg = current_ir_graph;
3155 ir_mode *mode = get_irn_mode(c);
3157 c = new_rd_Not(dbgi, irg, blk, c, mode);
3158 n = new_rd_And(dbgi, irg, blk, new_n, c, mode);
3161 set_nodes_block(n, blk);
3162 set_binop_left(n, new_n);
3163 set_binop_right(n, c);
3164 add_identities(current_ir_graph->value_table, n);
3167 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_SHIFT_AND);
3178 static ir_node *transform_node_And(ir_node *n) {
3179 ir_node *c, *oldn = n;
3180 ir_node *a = get_And_left(n);
3181 ir_node *b = get_And_right(n);
3184 mode = get_irn_mode(n);
3185 HANDLE_BINOP_PHI(tarval_and, a, b, c, mode);
3187 /* we can evaluate 2 Projs of the same Cmp */
3188 if (mode == mode_b && is_Proj(a) && is_Proj(b)) {
3189 ir_node *pred_a = get_Proj_pred(a);
3190 ir_node *pred_b = get_Proj_pred(b);
3191 if (pred_a == pred_b) {
3192 dbg_info *dbgi = get_irn_dbg_info(n);
3193 ir_node *block = get_nodes_block(pred_a);
3194 pn_Cmp pn_a = get_Proj_proj(a);
3195 pn_Cmp pn_b = get_Proj_proj(b);
3196 /* yes, we can simply calculate with pncs */
3197 pn_Cmp new_pnc = pn_a & pn_b;
3199 return new_rd_Proj(dbgi, current_ir_graph, block, pred_a, mode_b, new_pnc);
3204 ir_node *op = get_Not_op(b);
3206 ir_node *ba = get_And_left(op);
3207 ir_node *bb = get_And_right(op);
3209 /* it's enough to test the following cases due to normalization! */
3210 if (get_Or_left(a) == ba && get_Or_right(a) == bb) {
3211 /* (a|b) & ~(a&b) = a^b */
3212 ir_node *block = get_nodes_block(n);
3214 n = new_rd_Eor(get_irn_dbg_info(n), current_ir_graph, block, ba, bb, mode);
3215 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_TO_EOR);
3223 ir_node *op = get_Not_op(a);
3225 ir_node *aa = get_And_left(op);
3226 ir_node *ab = get_And_right(op);
3228 /* it's enough to test the following cases due to normalization! */
3229 if (get_Or_left(b) == aa && get_Or_right(b) == ab) {
3230 /* (a|b) & ~(a&b) = a^b */
3231 ir_node *block = get_nodes_block(n);
3233 n = new_rd_Eor(get_irn_dbg_info(n), current_ir_graph, block, aa, ab, mode);
3234 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_TO_EOR);
3241 ir_node *al = get_Eor_left(a);
3242 ir_node *ar = get_Eor_right(a);
3245 /* (b ^ a) & b -> ~a & b */
3246 dbg_info *dbg = get_irn_dbg_info(n);
3247 ir_node *block = get_nodes_block(n);
3249 ar = new_rd_Not(dbg, current_ir_graph, block, ar, mode);
3250 n = new_rd_And(dbg, current_ir_graph, block, ar, b, mode);
3251 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3255 /* (a ^ b) & b -> ~a & b */
3256 dbg_info *dbg = get_irn_dbg_info(n);
3257 ir_node *block = get_nodes_block(n);
3259 al = new_rd_Not(dbg, current_ir_graph, block, al, mode);
3260 n = new_rd_And(dbg, current_ir_graph, block, al, b, mode);
3261 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3266 ir_node *bl = get_Eor_left(b);
3267 ir_node *br = get_Eor_right(b);
3270 /* a & (a ^ b) -> a & ~b */
3271 dbg_info *dbg = get_irn_dbg_info(n);
3272 ir_node *block = get_nodes_block(n);
3274 br = new_rd_Not(dbg, current_ir_graph, block, br, mode);
3275 n = new_rd_And(dbg, current_ir_graph, block, br, a, mode);
3276 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3280 /* a & (b ^ a) -> a & ~b */
3281 dbg_info *dbg = get_irn_dbg_info(n);
3282 ir_node *block = get_nodes_block(n);
3284 bl = new_rd_Not(dbg, current_ir_graph, block, bl, mode);
3285 n = new_rd_And(dbg, current_ir_graph, block, bl, a, mode);
3286 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3290 if (is_Not(a) && is_Not(b)) {
3291 /* ~a & ~b = ~(a|b) */
3292 ir_node *block = get_nodes_block(n);
3293 ir_mode *mode = get_irn_mode(n);
3297 n = new_rd_Or(get_irn_dbg_info(n), current_ir_graph, block, a, b, mode);
3298 n = new_rd_Not(get_irn_dbg_info(n), current_ir_graph, block, n, mode);
3299 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_DEMORGAN);
3303 n = transform_bitwise_distributive(n, transform_node_And);
3306 } /* transform_node_And */
3311 static ir_node *transform_node_Eor(ir_node *n) {
3312 ir_node *c, *oldn = n;
3313 ir_node *a = get_Eor_left(n);
3314 ir_node *b = get_Eor_right(n);
3315 ir_mode *mode = get_irn_mode(n);
3317 HANDLE_BINOP_PHI(tarval_eor, a, b, c, mode);
3319 /* we can evaluate 2 Projs of the same Cmp */
3320 if (mode == mode_b && is_Proj(a) && is_Proj(b)) {
3321 ir_node *pred_a = get_Proj_pred(a);
3322 ir_node *pred_b = get_Proj_pred(b);
3323 if(pred_a == pred_b) {
3324 dbg_info *dbgi = get_irn_dbg_info(n);
3325 ir_node *block = get_nodes_block(pred_a);
3326 pn_Cmp pn_a = get_Proj_proj(a);
3327 pn_Cmp pn_b = get_Proj_proj(b);
3328 /* yes, we can simply calculate with pncs */
3329 pn_Cmp new_pnc = pn_a ^ pn_b;
3331 return new_rd_Proj(dbgi, current_ir_graph, block, pred_a, mode_b,
3338 n = new_rd_Const(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1),
3339 mode, get_mode_null(mode));
3340 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_A_A);
3341 } else if (mode == mode_b &&
3343 is_Const(b) && is_Const_one(b) &&
3344 is_Cmp(get_Proj_pred(a))) {
3345 /* The Eor negates a Cmp. The Cmp has the negated result anyways! */
3346 n = new_r_Proj(current_ir_graph, get_irn_n(n, -1), get_Proj_pred(a),
3347 mode_b, get_negated_pnc(get_Proj_proj(a), mode));
3349 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT_BOOL);
3350 } else if (is_Const(b)) {
3351 if (is_Not(a)) { /* ~x ^ const -> x ^ ~const */
3352 ir_node *cnst = new_Const(mode, tarval_not(get_Const_tarval(b)));
3353 ir_node *not_op = get_Not_op(a);
3354 dbg_info *dbg = get_irn_dbg_info(n);
3355 ir_graph *irg = current_ir_graph;
3356 ir_node *block = get_nodes_block(n);
3357 ir_mode *mode = get_irn_mode(n);
3358 n = new_rd_Eor(dbg, irg, block, not_op, cnst, mode);
3360 } else if (is_Const_all_one(b)) { /* x ^ 1...1 -> ~1 */
3361 n = new_r_Not(current_ir_graph, get_nodes_block(n), a, mode);
3362 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3365 n = transform_bitwise_distributive(n, transform_node_Eor);
3369 } /* transform_node_Eor */
3374 static ir_node *transform_node_Not(ir_node *n) {
3375 ir_node *c, *oldn = n;
3376 ir_node *a = get_Not_op(n);
3377 ir_mode *mode = get_irn_mode(n);
3379 HANDLE_UNOP_PHI(tarval_not,a,c);
3381 /* check for a boolean Not */
3382 if (mode == mode_b &&
3384 is_Cmp(get_Proj_pred(a))) {
3385 /* We negate a Cmp. The Cmp has the negated result anyways! */
3386 n = new_r_Proj(current_ir_graph, get_irn_n(n, -1), get_Proj_pred(a),
3387 mode_b, get_negated_pnc(get_Proj_proj(a), mode_b));
3388 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_CMP);
3392 ir_node *eor_b = get_Eor_right(a);
3393 if (is_Const(eor_b)) { /* ~(x ^ const) -> x ^ ~const */
3394 ir_node *cnst = new_Const(mode, tarval_not(get_Const_tarval(eor_b)));
3395 ir_node *eor_a = get_Eor_left(a);
3396 dbg_info *dbg = get_irn_dbg_info(n);
3397 ir_graph *irg = current_ir_graph;
3398 ir_node *block = get_nodes_block(n);
3399 ir_mode *mode = get_irn_mode(n);
3400 n = new_rd_Eor(dbg, irg, block, eor_a, cnst, mode);
3404 if (get_mode_arithmetic(mode) == irma_twos_complement) {
3405 if (is_Minus(a)) { /* ~-x -> x + -1 */
3406 dbg_info *dbg = get_irn_dbg_info(n);
3407 ir_graph *irg = current_ir_graph;
3408 ir_node *block = get_nodes_block(n);
3409 ir_node *add_l = get_Minus_op(a);
3410 ir_node *add_r = new_rd_Const(dbg, irg, block, mode, get_mode_minus_one(mode));
3411 n = new_rd_Add(dbg, irg, block, add_l, add_r, mode);
3412 } else if (is_Add(a)) {
3413 ir_node *add_r = get_Add_right(a);
3414 if (is_Const(add_r) && is_Const_all_one(add_r)) {
3415 /* ~(x + -1) = -x */
3416 ir_node *op = get_Add_left(a);
3417 ir_node *blk = get_irn_n(n, -1);
3418 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, blk, op, get_irn_mode(n));
3419 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_MINUS_1);
3424 } /* transform_node_Not */
3427 * Transform a Minus.
3431 * -(a >>u (size-1)) = a >>s (size-1)
3432 * -(a >>s (size-1)) = a >>u (size-1)
3433 * -(a * const) -> a * -const
3435 static ir_node *transform_node_Minus(ir_node *n) {
3436 ir_node *c, *oldn = n;
3437 ir_node *a = get_Minus_op(n);
3440 HANDLE_UNOP_PHI(tarval_neg,a,c);
3442 mode = get_irn_mode(a);
3443 if (get_mode_arithmetic(mode) == irma_twos_complement) {
3444 /* the following rules are only to twos-complement */
3447 ir_node *op = get_Not_op(a);
3448 tarval *tv = get_mode_one(mode);
3449 ir_node *blk = get_irn_n(n, -1);
3450 ir_node *c = new_r_Const(current_ir_graph, blk, mode, tv);
3451 n = new_rd_Add(get_irn_dbg_info(n), current_ir_graph, blk, op, c, mode);
3452 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_MINUS_NOT);
3456 ir_node *c = get_Shr_right(a);
3459 tarval *tv = get_Const_tarval(c);
3461 if (tarval_is_long(tv) && get_tarval_long(tv) == (int) get_mode_size_bits(mode) - 1) {
3462 /* -(a >>u (size-1)) = a >>s (size-1) */
3463 ir_node *v = get_Shr_left(a);
3465 n = new_rd_Shrs(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1), v, c, mode);
3466 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_PREDICATE);
3472 ir_node *c = get_Shrs_right(a);
3475 tarval *tv = get_Const_tarval(c);
3477 if (tarval_is_long(tv) && get_tarval_long(tv) == (int) get_mode_size_bits(mode) - 1) {
3478 /* -(a >>s (size-1)) = a >>u (size-1) */
3479 ir_node *v = get_Shrs_left(a);
3481 n = new_rd_Shr(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1), v, c, mode);
3482 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_PREDICATE);
3489 /* - (a-b) = b - a */
3490 ir_node *la = get_Sub_left(a);
3491 ir_node *ra = get_Sub_right(a);
3492 ir_node *blk = get_irn_n(n, -1);
3494 n = new_rd_Sub(get_irn_dbg_info(n), current_ir_graph, blk, ra, la, mode);
3495 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_MINUS_SUB);
3499 if (is_Mul(a)) { /* -(a * const) -> a * -const */
3500 ir_node *mul_l = get_Mul_left(a);
3501 ir_node *mul_r = get_Mul_right(a);
3502 if (is_Const(mul_r)) {
3503 tarval *tv = tarval_neg(get_Const_tarval(mul_r));
3504 if(tv != tarval_bad) {
3505 ir_node *cnst = new_Const(mode, tv);
3506 dbg_info *dbg = get_irn_dbg_info(a);
3507 ir_graph *irg = current_ir_graph;
3508 ir_node *block = get_nodes_block(a);
3509 n = new_rd_Mul(dbg, irg, block, mul_l, cnst, mode);
3510 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_MINUS_MUL_C);
3517 } /* transform_node_Minus */
3520 * Transform a Cast_type(Const) into a new Const_type
3522 static ir_node *transform_node_Cast(ir_node *n) {
3524 ir_node *pred = get_Cast_op(n);
3525 ir_type *tp = get_irn_type(n);
3527 if (is_Const(pred) && get_Const_type(pred) != tp) {
3528 n = new_rd_Const_type(NULL, current_ir_graph, get_irn_n(pred, -1), get_irn_mode(pred),
3529 get_Const_tarval(pred), tp);
3530 DBG_OPT_CSTEVAL(oldn, n);
3531 } else if (is_SymConst(pred) && get_SymConst_value_type(pred) != tp) {
3532 n = new_rd_SymConst_type(NULL, current_ir_graph, get_irn_n(pred, -1), get_irn_mode(pred),
3533 get_SymConst_symbol(pred), get_SymConst_kind(pred), tp);
3534 DBG_OPT_CSTEVAL(oldn, n);
3538 } /* transform_node_Cast */
3541 * Transform a Proj(Div) with a non-zero value.
3542 * Removes the exceptions and routes the memory to the NoMem node.
3544 static ir_node *transform_node_Proj_Div(ir_node *proj) {
3545 ir_node *div = get_Proj_pred(proj);
3546 ir_node *b = get_Div_right(div);
3547 ir_node *confirm, *res, *new_mem;
3550 if (value_not_zero(b, &confirm)) {
3551 /* div(x, y) && y != 0 */
3552 if (confirm == NULL) {
3553 /* we are sure we have a Const != 0 */
3554 new_mem = get_Div_mem(div);
3555 if (is_Pin(new_mem))
3556 new_mem = get_Pin_op(new_mem);
3557 set_Div_mem(div, new_mem);
3558 set_irn_pinned(div, op_pin_state_floats);
3561 proj_nr = get_Proj_proj(proj);
3563 case pn_Div_X_regular:
3564 return new_r_Jmp(current_ir_graph, get_irn_n(div, -1));
3566 case pn_Div_X_except:
3567 /* we found an exception handler, remove it */
3568 DBG_OPT_EXC_REM(proj);
3572 res = get_Div_mem(div);
3573 new_mem = get_irg_no_mem(current_ir_graph);
3576 /* This node can only float up to the Confirm block */
3577 new_mem = new_r_Pin(current_ir_graph, get_nodes_block(confirm), new_mem);
3579 set_irn_pinned(div, op_pin_state_floats);
3580 /* this is a Div without exception, we can remove the memory edge */
3581 set_Div_mem(div, new_mem);
3586 } /* transform_node_Proj_Div */
3589 * Transform a Proj(Mod) with a non-zero value.
3590 * Removes the exceptions and routes the memory to the NoMem node.
3592 static ir_node *transform_node_Proj_Mod(ir_node *proj) {
3593 ir_node *mod = get_Proj_pred(proj);
3594 ir_node *b = get_Mod_right(mod);
3595 ir_node *confirm, *res, *new_mem;
3598 if (value_not_zero(b, &confirm)) {
3599 /* mod(x, y) && y != 0 */
3600 proj_nr = get_Proj_proj(proj);
3602 if (confirm == NULL) {
3603 /* we are sure we have a Const != 0 */
3604 new_mem = get_Mod_mem(mod);
3605 if (is_Pin(new_mem))
3606 new_mem = get_Pin_op(new_mem);
3607 set_Mod_mem(mod, new_mem);
3608 set_irn_pinned(mod, op_pin_state_floats);
3613 case pn_Mod_X_regular:
3614 return new_r_Jmp(current_ir_graph, get_irn_n(mod, -1));
3616 case pn_Mod_X_except:
3617 /* we found an exception handler, remove it */
3618 DBG_OPT_EXC_REM(proj);
3622 res = get_Mod_mem(mod);
3623 new_mem = get_irg_no_mem(current_ir_graph);
3626 /* This node can only float up to the Confirm block */
3627 new_mem = new_r_Pin(current_ir_graph, get_nodes_block(confirm), new_mem);
3629 /* this is a Mod without exception, we can remove the memory edge */
3630 set_Mod_mem(mod, new_mem);
3633 if (get_Mod_left(mod) == b) {
3634 /* a % a = 0 if a != 0 */
3635 ir_mode *mode = get_irn_mode(proj);
3636 ir_node *res = new_Const(mode, get_mode_null(mode));
3638 DBG_OPT_CSTEVAL(mod, res);
3644 } /* transform_node_Proj_Mod */
3647 * Transform a Proj(DivMod) with a non-zero value.
3648 * Removes the exceptions and routes the memory to the NoMem node.
3650 static ir_node *transform_node_Proj_DivMod(ir_node *proj) {
3651 ir_node *divmod = get_Proj_pred(proj);
3652 ir_node *b = get_DivMod_right(divmod);
3653 ir_node *confirm, *res, *new_mem;
3656 if (value_not_zero(b, &confirm)) {
3657 /* DivMod(x, y) && y != 0 */
3658 proj_nr = get_Proj_proj(proj);
3660 if (confirm == NULL) {
3661 /* we are sure we have a Const != 0 */
3662 new_mem = get_DivMod_mem(divmod);
3663 if (is_Pin(new_mem))
3664 new_mem = get_Pin_op(new_mem);
3665 set_DivMod_mem(divmod, new_mem);
3666 set_irn_pinned(divmod, op_pin_state_floats);
3671 case pn_DivMod_X_regular:
3672 return new_r_Jmp(current_ir_graph, get_irn_n(divmod, -1));
3674 case pn_DivMod_X_except:
3675 /* we found an exception handler, remove it */
3676 DBG_OPT_EXC_REM(proj);
3680 res = get_DivMod_mem(divmod);
3681 new_mem = get_irg_no_mem(current_ir_graph);
3684 /* This node can only float up to the Confirm block */
3685 new_mem = new_r_Pin(current_ir_graph, get_nodes_block(confirm), new_mem);
3687 /* this is a DivMod without exception, we can remove the memory edge */
3688 set_DivMod_mem(divmod, new_mem);
3691 case pn_DivMod_res_mod:
3692 if (get_DivMod_left(divmod) == b) {
3693 /* a % a = 0 if a != 0 */
3694 ir_mode *mode = get_irn_mode(proj);
3695 ir_node *res = new_Const(mode, get_mode_null(mode));
3697 DBG_OPT_CSTEVAL(divmod, res);
3703 } /* transform_node_Proj_DivMod */
3706 * Optimizes jump tables (CondIs or CondIu) by removing all impossible cases.
3708 static ir_node *transform_node_Proj_Cond(ir_node *proj) {
3709 if (get_opt_unreachable_code()) {
3710 ir_node *n = get_Proj_pred(proj);
3711 ir_node *b = get_Cond_selector(n);
3713 if (mode_is_int(get_irn_mode(b))) {
3714 tarval *tb = value_of(b);
3716 if (tb != tarval_bad) {
3717 /* we have a constant switch */
3718 long num = get_Proj_proj(proj);
3720 if (num != get_Cond_defaultProj(n)) { /* we cannot optimize default Proj's yet */
3721 if (get_tarval_long(tb) == num) {
3722 /* Do NOT create a jump here, or we will have 2 control flow ops
3723 * in a block. This case is optimized away in optimize_cf(). */
3726 /* this case will NEVER be taken, kill it */
3734 } /* transform_node_Proj_Cond */
3737 * Create a 0 constant of given mode.
3739 static ir_node *create_zero_const(ir_mode *mode) {
3740 tarval *tv = get_mode_null(mode);
3741 ir_node *cnst = new_Const(mode, tv);
3746 /* the order of the values is important! */
3747 typedef enum const_class {
3753 static const_class classify_const(const ir_node* n)
3755 if (is_Const(n)) return const_const;
3756 if (is_irn_constlike(n)) return const_like;
3761 * Determines whether r is more constlike or has a larger index (in that order)
3764 static int operands_are_normalized(const ir_node *l, const ir_node *r)
3766 const const_class l_order = classify_const(l);
3767 const const_class r_order = classify_const(r);
3769 l_order > r_order ||
3770 (l_order == r_order && get_irn_idx(l) <= get_irn_idx(r));
3774 * Normalizes and optimizes Cmp nodes.
3776 static ir_node *transform_node_Proj_Cmp(ir_node *proj) {
3777 ir_node *n = get_Proj_pred(proj);
3778 ir_node *left = get_Cmp_left(n);
3779 ir_node *right = get_Cmp_right(n);
3782 ir_mode *mode = NULL;
3783 long proj_nr = get_Proj_proj(proj);
3785 /* we can evaluate some cases directly */
3788 return new_Const(mode_b, get_tarval_b_false());
3790 return new_Const(mode_b, get_tarval_b_true());
3792 if (!mode_is_float(get_irn_mode(left)))
3793 return new_Const(mode_b, get_tarval_b_true());
3799 /* remove Casts of both sides */
3800 left = skip_Cast(left);
3801 right = skip_Cast(right);
3803 /* Remove unnecessary conversions */
3804 /* TODO handle constants */
3805 if (is_Conv(left) && is_Conv(right)) {
3806 ir_mode *mode = get_irn_mode(left);
3807 ir_node *op_left = get_Conv_op(left);
3808 ir_node *op_right = get_Conv_op(right);
3809 ir_mode *mode_left = get_irn_mode(op_left);
3810 ir_mode *mode_right = get_irn_mode(op_right);
3812 if (smaller_mode(mode_left, mode) && smaller_mode(mode_right, mode)
3813 && mode_left != mode_b && mode_right != mode_b) {
3814 ir_graph *irg = current_ir_graph;
3815 ir_node *block = get_nodes_block(n);
3817 if (mode_left == mode_right) {
3821 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV_CONV);
3822 } else if (smaller_mode(mode_left, mode_right)) {
3823 left = new_r_Conv(irg, block, op_left, mode_right);
3826 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV);
3827 } else if (smaller_mode(mode_right, mode_left)) {
3829 right = new_r_Conv(irg, block, op_right, mode_left);
3831 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV);
3836 /* remove operation on both sides if possible */
3837 if (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) {
3839 * The following operations are NOT safe for floating point operations, for instance
3840 * 1.0 + inf == 2.0 + inf, =/=> x == y
3842 if (mode_is_int(get_irn_mode(left))) {
3843 unsigned lop = get_irn_opcode(left);
3845 if (lop == get_irn_opcode(right)) {
3846 ir_node *ll, *lr, *rl, *rr;
3848 /* same operation on both sides, try to remove */
3852 /* ~a CMP ~b => a CMP b, -a CMP -b ==> a CMP b */
3853 left = get_unop_op(left);
3854 right = get_unop_op(right);
3856 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3859 ll = get_Add_left(left);
3860 lr = get_Add_right(left);
3861 rl = get_Add_left(right);
3862 rr = get_Add_right(right);
3865 /* X + a CMP X + b ==> a CMP b */
3869 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3870 } else if (ll == rr) {
3871 /* X + a CMP b + X ==> a CMP b */
3875 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3876 } else if (lr == rl) {
3877 /* a + X CMP X + b ==> a CMP b */
3881 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3882 } else if (lr == rr) {
3883 /* a + X CMP b + X ==> a CMP b */
3887 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3891 ll = get_Sub_left(left);
3892 lr = get_Sub_right(left);
3893 rl = get_Sub_left(right);
3894 rr = get_Sub_right(right);
3897 /* X - a CMP X - b ==> a CMP b */
3901 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3902 } else if (lr == rr) {
3903 /* a - X CMP b - X ==> a CMP b */
3907 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3911 if (get_Rotl_right(left) == get_Rotl_right(right)) {
3912 /* a ROTL X CMP b ROTL X ==> a CMP b */
3913 left = get_Rotl_left(left);
3914 right = get_Rotl_left(right);
3916 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3924 /* X+A == A, A+X == A, A-X == A -> X == 0 */
3925 if (is_Add(left) || is_Sub(left)) {
3926 ir_node *ll = get_binop_left(left);
3927 ir_node *lr = get_binop_right(left);
3929 if (lr == right && is_Add(left)) {
3936 right = create_zero_const(get_irn_mode(left));
3938 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3941 if (is_Add(right) || is_Sub(right)) {
3942 ir_node *rl = get_binop_left(right);
3943 ir_node *rr = get_binop_right(right);
3945 if (rr == left && is_Add(right)) {
3952 right = create_zero_const(get_irn_mode(left));
3954 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3957 } /* mode_is_int(...) */
3958 } /* proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg */
3960 /* replace mode_b compares with ands/ors */
3961 if (get_irn_mode(left) == mode_b) {
3962 ir_graph *irg = current_ir_graph;
3963 ir_node *block = get_nodes_block(n);
3967 case pn_Cmp_Le: bres = new_r_Or( irg, block, new_r_Not(irg, block, left, mode_b), right, mode_b); break;
3968 case pn_Cmp_Lt: bres = new_r_And(irg, block, new_r_Not(irg, block, left, mode_b), right, mode_b); break;
3969 case pn_Cmp_Ge: bres = new_r_Or( irg, block, left, new_r_Not(irg, block, right, mode_b), mode_b); break;
3970 case pn_Cmp_Gt: bres = new_r_And(irg, block, left, new_r_Not(irg, block, right, mode_b), mode_b); break;
3971 case pn_Cmp_Lg: bres = new_r_Eor(irg, block, left, right, mode_b); break;
3972 case pn_Cmp_Eq: bres = new_r_Not(irg, block, new_r_Eor(irg, block, left, right, mode_b), mode_b); break;
3973 default: bres = NULL;
3976 DBG_OPT_ALGSIM0(n, bres, FS_OPT_CMP_TO_BOOL);
3982 * First step: normalize the compare op
3983 * by placing the constant on the right side
3984 * or moving the lower address node to the left.
3986 if (!operands_are_normalized(left, right)) {
3992 proj_nr = get_inversed_pnc(proj_nr);
3997 * Second step: Try to reduce the magnitude
3998 * of a constant. This may help to generate better code
3999 * later and may help to normalize more compares.
4000 * Of course this is only possible for integer values.
4002 if (is_Const(right)) {
4003 mode = get_irn_mode(right);
4004 tv = get_Const_tarval(right);
4006 /* TODO extend to arbitrary constants */
4007 if (is_Conv(left) && tarval_is_null(tv)) {
4008 ir_node *op = get_Conv_op(left);
4009 ir_mode *op_mode = get_irn_mode(op);
4012 * UpConv(x) REL 0 ==> x REL 0
4014 if (get_mode_size_bits(mode) > get_mode_size_bits(op_mode) &&
4015 ((proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) ||
4016 mode_is_signed(mode) || !mode_is_signed(op_mode))) {
4017 tv = get_mode_null(op_mode);
4021 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV);
4025 if (tv != tarval_bad) {
4026 /* the following optimization is possible on modes without Overflow
4027 * on Unary Minus or on == and !=:
4028 * -a CMP c ==> a swap(CMP) -c
4030 * Beware: for two-complement Overflow may occur, so only == and != can
4031 * be optimized, see this:
4032 * -MININT < 0 =/=> MININT > 0 !!!
4034 if (is_Minus(left) &&
4035 (!mode_overflow_on_unary_Minus(mode) ||
4036 (mode_is_int(mode) && (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg)))) {
4037 tv = tarval_neg(tv);
4039 if (tv != tarval_bad) {
4040 left = get_Minus_op(left);
4041 proj_nr = get_inversed_pnc(proj_nr);
4043 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4045 } else if (is_Not(left) && (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg)) {
4046 /* Not(a) ==/!= c ==> a ==/!= Not(c) */
4047 tv = tarval_not(tv);
4049 if (tv != tarval_bad) {
4050 left = get_Not_op(left);
4052 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4056 /* for integer modes, we have more */
4057 if (mode_is_int(mode)) {
4058 /* Ne includes Unordered which is not possible on integers.
4059 * However, frontends often use this wrong, so fix it here */
4060 if (proj_nr & pn_Cmp_Uo) {
4061 proj_nr &= ~pn_Cmp_Uo;
4062 set_Proj_proj(proj, proj_nr);
4065 /* c > 0 : a < c ==> a <= (c-1) a >= c ==> a > (c-1) */
4066 if ((proj_nr == pn_Cmp_Lt || proj_nr == pn_Cmp_Ge) &&
4067 tarval_cmp(tv, get_mode_null(mode)) == pn_Cmp_Gt) {
4068 tv = tarval_sub(tv, get_mode_one(mode), NULL);
4070 if (tv != tarval_bad) {
4071 proj_nr ^= pn_Cmp_Eq;
4073 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CNST_MAGN);
4076 /* c < 0 : a > c ==> a >= (c+1) a <= c ==> a < (c+1) */
4077 else if ((proj_nr == pn_Cmp_Gt || proj_nr == pn_Cmp_Le) &&
4078 tarval_cmp(tv, get_mode_null(mode)) == pn_Cmp_Lt) {
4079 tv = tarval_add(tv, get_mode_one(mode));
4081 if (tv != tarval_bad) {
4082 proj_nr ^= pn_Cmp_Eq;
4084 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CNST_MAGN);
4088 /* the following reassociations work only for == and != */
4089 if (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) {
4091 #if 0 /* Might be not that good in general */
4092 /* a-b == 0 ==> a == b, a-b != 0 ==> a != b */
4093 if (tarval_is_null(tv) && is_Sub(left)) {
4094 right = get_Sub_right(left);
4095 left = get_Sub_left(left);
4097 tv = value_of(right);
4099 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4103 if (tv != tarval_bad) {
4104 /* a-c1 == c2 ==> a == c2+c1, a-c1 != c2 ==> a != c2+c1 */
4106 ir_node *c1 = get_Sub_right(left);
4107 tarval *tv2 = value_of(c1);
4109 if (tv2 != tarval_bad) {
4110 tv2 = tarval_add(tv, value_of(c1));
4112 if (tv2 != tarval_bad) {
4113 left = get_Sub_left(left);
4116 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4120 /* a+c1 == c2 ==> a == c2-c1, a+c1 != c2 ==> a != c2-c1 */
4121 else if (is_Add(left)) {
4122 ir_node *a_l = get_Add_left(left);
4123 ir_node *a_r = get_Add_right(left);
4127 if (is_Const(a_l)) {
4129 tv2 = value_of(a_l);
4132 tv2 = value_of(a_r);
4135 if (tv2 != tarval_bad) {
4136 tv2 = tarval_sub(tv, tv2, NULL);
4138 if (tv2 != tarval_bad) {
4142 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4146 /* -a == c ==> a == -c, -a != c ==> a != -c */
4147 else if (is_Minus(left)) {
4148 tarval *tv2 = tarval_sub(get_mode_null(mode), tv, NULL);
4150 if (tv2 != tarval_bad) {
4151 left = get_Minus_op(left);
4154 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4159 /* the following reassociations work only for <= */
4160 else if (proj_nr == pn_Cmp_Le || proj_nr == pn_Cmp_Lt) {
4161 if (tv != tarval_bad) {
4162 /* c >= 0 : Abs(a) <= c ==> (unsigned)(a + c) <= 2*c */
4163 if (get_irn_op(left) == op_Abs) { // TODO something is missing here
4169 if (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) {
4170 switch (get_irn_opcode(left)) {
4174 c1 = get_And_right(left);
4177 * And(x, C1) == C2 ==> FALSE if C2 & C1 != C2
4178 * And(x, C1) != C2 ==> TRUE if C2 & C1 != C2
4180 tarval *mask = tarval_and(get_Const_tarval(c1), tv);
4182 /* TODO: move to constant evaluation */
4183 tv = proj_nr == pn_Cmp_Eq ? get_tarval_b_false() : get_tarval_b_true();
4184 c1 = new_Const(mode_b, tv);
4185 DBG_OPT_CSTEVAL(proj, c1);
4189 if (tarval_is_single_bit(tv)) {
4191 * optimization for AND:
4193 * And(x, C) == C ==> And(x, C) != 0
4194 * And(x, C) != C ==> And(X, C) == 0
4196 * if C is a single Bit constant.
4199 /* check for Constant's match. We have check hare the tarvals,
4200 because our const might be changed */
4201 if (get_Const_tarval(c1) == tv) {
4202 /* fine: do the transformation */
4203 tv = get_mode_null(get_tarval_mode(tv));
4204 proj_nr ^= pn_Cmp_Leg;
4206 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CNST_MAGN);
4212 c1 = get_Or_right(left);
4213 if (is_Const(c1) && tarval_is_null(tv)) {
4215 * Or(x, C) == 0 && C != 0 ==> FALSE
4216 * Or(x, C) != 0 && C != 0 ==> TRUE
4218 if (! tarval_is_null(get_Const_tarval(c1))) {
4219 /* TODO: move to constant evaluation */
4220 tv = proj_nr == pn_Cmp_Eq ? get_tarval_b_false() : get_tarval_b_true();
4221 c1 = new_Const(mode_b, tv);
4222 DBG_OPT_CSTEVAL(proj, c1);
4229 * optimize x << c1 == c into x & (-1 >>u c1) == c >> c1 if c & (-1 << c1) == c
4231 * optimize x << c1 != c into x & (-1 >>u c1) != c >> c1 if c & (-1 << c1) == c
4234 c1 = get_Shl_right(left);
4236 tarval *tv1 = get_Const_tarval(c1);
4237 ir_mode *mode = get_irn_mode(left);
4238 tarval *minus1 = get_mode_all_one(mode);
4239 tarval *amask = tarval_shr(minus1, tv1);
4240 tarval *cmask = tarval_shl(minus1, tv1);
4243 if (tarval_and(tv, cmask) != tv) {
4244 /* condition not met */
4245 tv = proj_nr == pn_Cmp_Eq ? get_tarval_b_false() : get_tarval_b_true();
4246 c1 = new_Const(mode_b, tv);
4247 DBG_OPT_CSTEVAL(proj, c1);
4250 sl = get_Shl_left(left);
4251 blk = get_nodes_block(n);
4252 left = new_rd_And(get_irn_dbg_info(left), current_ir_graph, blk, sl, new_Const(mode, amask), mode);
4253 tv = tarval_shr(tv, tv1);
4255 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_SHF_TO_AND);
4260 * optimize x >>u c1 == c into x & (-1 << c1) == c << c1 if c & (-1 >>u c1) == c
4262 * optimize x >>u c1 != c into x & (-1 << c1) != c << c1 if c & (-1 >>u c1) == c
4265 c1 = get_Shr_right(left);
4267 tarval *tv1 = get_Const_tarval(c1);
4268 ir_mode *mode = get_irn_mode(left);
4269 tarval *minus1 = get_mode_all_one(mode);
4270 tarval *amask = tarval_shl(minus1, tv1);
4271 tarval *cmask = tarval_shr(minus1, tv1);
4274 if (tarval_and(tv, cmask) != tv) {
4275 /* condition not met */
4276 tv = proj_nr == pn_Cmp_Eq ? get_tarval_b_false() : get_tarval_b_true();
4277 c1 = new_Const(mode_b, tv);
4278 DBG_OPT_CSTEVAL(proj, c1);
4281 sl = get_Shr_left(left);
4282 blk = get_nodes_block(n);
4283 left = new_rd_And(get_irn_dbg_info(left), current_ir_graph, blk, sl, new_Const(mode, amask), mode);
4284 tv = tarval_shl(tv, tv1);
4286 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_SHF_TO_AND);
4291 * optimize x >>s c1 == c into x & (-1 << c1) == c << c1 if (c >>s (BITS - c1)) \in {0,-1}
4293 * optimize x >>s c1 != c into x & (-1 << c1) != c << c1 if (c >>s (BITS - c1)) \in {0,-1}
4296 c1 = get_Shrs_right(left);
4298 tarval *tv1 = get_Const_tarval(c1);
4299 ir_mode *mode = get_irn_mode(left);
4300 tarval *minus1 = get_mode_all_one(mode);
4301 tarval *amask = tarval_shl(minus1, tv1);
4302 tarval *cond = new_tarval_from_long(get_mode_size_bits(mode), get_tarval_mode(tv1));
4305 cond = tarval_sub(cond, tv1, NULL);
4306 cond = tarval_shrs(tv, cond);
4308 if (!tarval_is_all_one(cond) && !tarval_is_null(cond)) {
4309 /* condition not met */
4310 tv = proj_nr == pn_Cmp_Eq ? get_tarval_b_false() : get_tarval_b_true();
4311 c1 = new_Const(mode_b, tv);
4312 DBG_OPT_CSTEVAL(proj, c1);
4315 sl = get_Shrs_left(left);
4316 blk = get_nodes_block(n);
4317 left = new_rd_And(get_irn_dbg_info(left), current_ir_graph, blk, sl, new_Const(mode, amask), mode);
4318 tv = tarval_shl(tv, tv1);
4320 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_SHF_TO_AND);
4325 } /* tarval != bad */
4328 if (changed & 2) /* need a new Const */
4329 right = new_Const(mode, tv);
4331 if ((proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) && is_Const(right) && is_Const_null(right) && is_Proj(left)) {
4332 ir_node *op = get_Proj_pred(left);
4334 if ((is_Mod(op) && get_Proj_proj(left) == pn_Mod_res) ||
4335 (is_DivMod(op) && get_Proj_proj(left) == pn_DivMod_res_mod)) {
4336 ir_node *c = get_binop_right(op);
4339 tarval *tv = get_Const_tarval(c);
4341 if (tarval_is_single_bit(tv)) {
4342 /* special case: (x % 2^n) CMP 0 ==> x & (2^n-1) CMP 0 */
4343 ir_node *v = get_binop_left(op);
4344 ir_node *blk = get_irn_n(op, -1);
4345 ir_mode *mode = get_irn_mode(v);
4347 tv = tarval_sub(tv, get_mode_one(mode), NULL);
4348 left = new_rd_And(get_irn_dbg_info(op), current_ir_graph, blk, v, new_Const(mode, tv), mode);
4350 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_MOD_TO_AND);
4357 ir_node *block = get_irn_n(n, -1); /* Beware of get_nodes_Block() */
4359 /* create a new compare */
4360 n = new_rd_Cmp(get_irn_dbg_info(n), current_ir_graph, block, left, right);
4361 proj = new_rd_Proj(get_irn_dbg_info(proj), current_ir_graph, block, n, get_irn_mode(proj), proj_nr);
4365 } /* transform_node_Proj_Cmp */
4368 * Does all optimizations on nodes that must be done on it's Proj's
4369 * because of creating new nodes.
4371 static ir_node *transform_node_Proj(ir_node *proj) {
4372 ir_node *n = get_Proj_pred(proj);
4374 switch (get_irn_opcode(n)) {
4376 return transform_node_Proj_Div(proj);
4379 return transform_node_Proj_Mod(proj);
4382 return transform_node_Proj_DivMod(proj);
4385 return transform_node_Proj_Cond(proj);
4388 return transform_node_Proj_Cmp(proj);
4391 /* should not happen, but if it does will be optimized away */
4392 return equivalent_node_Proj(proj);
4398 } /* transform_node_Proj */
4401 * Move Confirms down through Phi nodes.
4403 static ir_node *transform_node_Phi(ir_node *phi) {
4405 ir_mode *mode = get_irn_mode(phi);
4407 if (mode_is_reference(mode)) {
4408 n = get_irn_arity(phi);
4410 /* Beware of Phi0 */
4412 ir_node *pred = get_irn_n(phi, 0);
4413 ir_node *bound, *new_Phi, *block, **in;
4416 if (! is_Confirm(pred))
4419 bound = get_Confirm_bound(pred);
4420 pnc = get_Confirm_cmp(pred);
4422 NEW_ARR_A(ir_node *, in, n);
4423 in[0] = get_Confirm_value(pred);
4425 for (i = 1; i < n; ++i) {
4426 pred = get_irn_n(phi, i);
4428 if (! is_Confirm(pred) ||
4429 get_Confirm_bound(pred) != bound ||
4430 get_Confirm_cmp(pred) != pnc)
4432 in[i] = get_Confirm_value(pred);
4434 /* move the Confirm nodes "behind" the Phi */
4435 block = get_irn_n(phi, -1);
4436 new_Phi = new_r_Phi(current_ir_graph, block, n, in, get_irn_mode(phi));
4437 return new_r_Confirm(current_ir_graph, block, new_Phi, bound, pnc);
4441 } /* transform_node_Phi */
4444 * Returns the operands of a commutative bin-op, if one operand is
4445 * a const, it is returned as the second one.
4447 static void get_comm_Binop_Ops(ir_node *binop, ir_node **a, ir_node **c) {
4448 ir_node *op_a = get_binop_left(binop);
4449 ir_node *op_b = get_binop_right(binop);
4451 assert(is_op_commutative(get_irn_op(binop)));
4453 if (is_Const(op_a)) {
4460 } /* get_comm_Binop_Ops */
4463 * Optimize a Or(And(Or(And(v,c4),c3),c2),c1) pattern if possible.
4464 * Such pattern may arise in bitfield stores.
4466 * value c4 value c4 & c2
4467 * AND c3 AND c1 | c3
4474 * AND c1 ===> OR if (c1 | c2) == 0x111..11
4477 static ir_node *transform_node_Or_bf_store(ir_node *or) {
4480 ir_node *and_l, *c3;
4481 ir_node *value, *c4;
4482 ir_node *new_and, *new_const, *block;
4483 ir_mode *mode = get_irn_mode(or);
4485 tarval *tv1, *tv2, *tv3, *tv4, *tv, *n_tv4, *n_tv2;
4488 get_comm_Binop_Ops(or, &and, &c1);
4489 if (!is_Const(c1) || !is_And(and))
4492 get_comm_Binop_Ops(and, &or_l, &c2);
4496 tv1 = get_Const_tarval(c1);
4497 tv2 = get_Const_tarval(c2);
4499 tv = tarval_or(tv1, tv2);
4500 if (tarval_is_all_one(tv)) {
4501 /* the AND does NOT clear a bit with isn't set by the OR */
4502 set_Or_left(or, or_l);
4503 set_Or_right(or, c1);
4505 /* check for more */
4512 get_comm_Binop_Ops(or_l, &and_l, &c3);
4513 if (!is_Const(c3) || !is_And(and_l))
4516 get_comm_Binop_Ops(and_l, &value, &c4);
4520 /* ok, found the pattern, check for conditions */
4521 assert(mode == get_irn_mode(and));
4522 assert(mode == get_irn_mode(or_l));
4523 assert(mode == get_irn_mode(and_l));
4525 tv3 = get_Const_tarval(c3);
4526 tv4 = get_Const_tarval(c4);
4528 tv = tarval_or(tv4, tv2);
4529 if (!tarval_is_all_one(tv)) {
4530 /* have at least one 0 at the same bit position */
4534 n_tv4 = tarval_not(tv4);
4535 if (tv3 != tarval_and(tv3, n_tv4)) {
4536 /* bit in the or_mask is outside the and_mask */
4540 n_tv2 = tarval_not(tv2);
4541 if (tv1 != tarval_and(tv1, n_tv2)) {
4542 /* bit in the or_mask is outside the and_mask */
4546 /* ok, all conditions met */
4547 block = get_irn_n(or, -1);
4549 new_and = new_r_And(current_ir_graph, block,
4550 value, new_r_Const(current_ir_graph, block, mode, tarval_and(tv4, tv2)), mode);
4552 new_const = new_r_Const(current_ir_graph, block, mode, tarval_or(tv3, tv1));
4554 set_Or_left(or, new_and);
4555 set_Or_right(or, new_const);
4557 /* check for more */
4559 } /* transform_node_Or_bf_store */
4562 * Optimize an Or(shl(x, c), shr(x, bits - c)) into a Rotl
4564 static ir_node *transform_node_Or_Rotl(ir_node *or) {
4565 ir_mode *mode = get_irn_mode(or);
4566 ir_node *shl, *shr, *block;
4567 ir_node *irn, *x, *c1, *c2, *v, *sub, *n, *rotval;
4570 if (! mode_is_int(mode))
4573 shl = get_binop_left(or);
4574 shr = get_binop_right(or);
4583 } else if (!is_Shl(shl)) {
4585 } else if (!is_Shr(shr)) {
4588 x = get_Shl_left(shl);
4589 if (x != get_Shr_left(shr))
4592 c1 = get_Shl_right(shl);
4593 c2 = get_Shr_right(shr);
4594 if (is_Const(c1) && is_Const(c2)) {
4595 tv1 = get_Const_tarval(c1);
4596 if (! tarval_is_long(tv1))
4599 tv2 = get_Const_tarval(c2);
4600 if (! tarval_is_long(tv2))
4603 if (get_tarval_long(tv1) + get_tarval_long(tv2)
4604 != (int) get_mode_size_bits(mode))
4607 /* yet, condition met */
4608 block = get_nodes_block(or);
4610 n = new_r_Rotl(current_ir_graph, block, x, c1, mode);
4612 DBG_OPT_ALGSIM1(or, shl, shr, n, FS_OPT_OR_SHFT_TO_ROTL);
4619 rotval = sub; /* a Rot right is not supported, so use a rot left */
4620 } else if (is_Sub(c2)) {
4626 if (get_Sub_right(sub) != v)
4629 c1 = get_Sub_left(sub);
4633 tv1 = get_Const_tarval(c1);
4634 if (! tarval_is_long(tv1))
4637 if (get_tarval_long(tv1) != (int) get_mode_size_bits(mode))
4640 /* yet, condition met */
4641 block = get_nodes_block(or);
4643 n = new_r_Rotl(current_ir_graph, block, x, rotval, mode);
4645 DBG_OPT_ALGSIM0(or, n, FS_OPT_OR_SHFT_TO_ROTL);
4647 } /* transform_node_Or_Rotl */
4652 static ir_node *transform_node_Or(ir_node *n) {
4653 ir_node *c, *oldn = n;
4654 ir_node *a = get_Or_left(n);
4655 ir_node *b = get_Or_right(n);
4658 if (is_Not(a) && is_Not(b)) {
4659 /* ~a | ~b = ~(a&b) */
4660 ir_node *block = get_nodes_block(n);
4662 mode = get_irn_mode(n);
4665 n = new_rd_And(get_irn_dbg_info(n), current_ir_graph, block, a, b, mode);
4666 n = new_rd_Not(get_irn_dbg_info(n), current_ir_graph, block, n, mode);
4667 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_DEMORGAN);
4671 /* we can evaluate 2 Projs of the same Cmp */
4672 if (get_irn_mode(n) == mode_b && is_Proj(a) && is_Proj(b)) {
4673 ir_node *pred_a = get_Proj_pred(a);
4674 ir_node *pred_b = get_Proj_pred(b);
4675 if (pred_a == pred_b) {
4676 dbg_info *dbgi = get_irn_dbg_info(n);
4677 ir_node *block = get_nodes_block(pred_a);
4678 pn_Cmp pn_a = get_Proj_proj(a);
4679 pn_Cmp pn_b = get_Proj_proj(b);
4680 /* yes, we can simply calculate with pncs */
4681 pn_Cmp new_pnc = pn_a | pn_b;
4683 return new_rd_Proj(dbgi, current_ir_graph, block, pred_a, mode_b,
4688 mode = get_irn_mode(n);
4689 HANDLE_BINOP_PHI(tarval_or, a, b, c, mode);
4691 n = transform_node_Or_bf_store(n);
4692 n = transform_node_Or_Rotl(n);
4696 n = transform_bitwise_distributive(n, transform_node_Or);
4699 } /* transform_node_Or */
4703 static ir_node *transform_node(ir_node *n);
4706 * Optimize (a >> c1) >> c2), works for Shr, Shrs, Shl, Rotl.
4708 * Should be moved to reassociation?
4710 static ir_node *transform_node_shift(ir_node *n) {
4711 ir_node *left, *right;
4713 tarval *tv1, *tv2, *res;
4714 ir_node *in[2], *irn, *block;
4716 left = get_binop_left(n);
4718 /* different operations */
4719 if (get_irn_op(left) != get_irn_op(n))
4722 right = get_binop_right(n);
4723 tv1 = value_of(right);
4724 if (tv1 == tarval_bad)
4727 tv2 = value_of(get_binop_right(left));
4728 if (tv2 == tarval_bad)
4731 res = tarval_add(tv1, tv2);
4732 mode = get_irn_mode(n);
4734 /* beware: a simple replacement works only, if res < modulo shift */
4736 int modulo_shf = get_mode_modulo_shift(mode);
4737 assert(modulo_shf >= (int) get_mode_size_bits(mode));
4738 if (modulo_shf > 0) {
4739 tarval *modulo = new_tarval_from_long(modulo_shf,
4740 get_tarval_mode(res));
4742 /* shifting too much */
4743 if (!(tarval_cmp(res, modulo) & pn_Cmp_Lt)) {
4745 ir_graph *irg = get_irn_irg(n);
4746 ir_node *block = get_nodes_block(n);
4747 dbg_info *dbgi = get_irn_dbg_info(n);
4748 ir_node *cnst = new_Const(mode_Iu, new_tarval_from_long(get_mode_size_bits(mode)-1, mode_Iu));
4749 return new_rd_Shrs(dbgi, irg, block, get_binop_left(left),
4753 return new_Const(mode, get_mode_null(mode));
4757 res = tarval_mod(res, new_tarval_from_long(get_mode_size_bits(mode), get_tarval_mode(res)));
4760 /* ok, we can replace it */
4761 block = get_nodes_block(n);
4763 in[0] = get_binop_left(left);
4764 in[1] = new_r_Const(current_ir_graph, block, get_tarval_mode(res), res);
4766 irn = new_ir_node(NULL, current_ir_graph, block, get_irn_op(n), mode, 2, in);
4768 DBG_OPT_ALGSIM0(n, irn, FS_OPT_REASSOC_SHIFT);
4770 return transform_node(irn);
4771 } /* transform_node_shift */
4774 * normalisation: (x & c1) >> c2 to (x >> c2) & (c1 >> c2)
4776 * - and, or, xor instead of &
4777 * - Shl, Shr, Shrs, rotl instead of >>
4778 * (with a special case for Or/Xor + Shrs)
4780 static ir_node *transform_node_bitop_shift(ir_node *n) {
4782 ir_node *right = get_binop_right(n);
4783 ir_mode *mode = get_irn_mode(n);
4784 ir_node *bitop_left;
4785 ir_node *bitop_right;
4797 assert(is_Shrs(n) || is_Shr(n) || is_Shl(n) || is_Rotl(n));
4799 if (!is_Const(right))
4802 left = get_binop_left(n);
4803 op_left = get_irn_op(left);
4804 if (op_left != op_And && op_left != op_Or && op_left != op_Eor)
4807 /* doing it with Shrs is not legal if the Or/Eor affects the topmost bit */
4808 if (is_Shrs(n) && (op_left == op_Or || op_left == op_Eor)) {
4809 /* TODO: test if sign bit is affectes */
4813 bitop_right = get_binop_right(left);
4814 if (!is_Const(bitop_right))
4817 bitop_left = get_binop_left(left);
4819 irg = get_irn_irg(n);
4820 block = get_nodes_block(n);
4821 dbgi = get_irn_dbg_info(n);
4822 tv1 = get_Const_tarval(bitop_right);
4823 tv2 = get_Const_tarval(right);
4825 assert(get_tarval_mode(tv1) == mode);
4828 new_shift = new_rd_Shl(dbgi, irg, block, bitop_left, right, mode);
4829 tv_shift = tarval_shl(tv1, tv2);
4830 } else if(is_Shr(n)) {
4831 new_shift = new_rd_Shr(dbgi, irg, block, bitop_left, right, mode);
4832 tv_shift = tarval_shr(tv1, tv2);
4833 } else if(is_Shrs(n)) {
4834 new_shift = new_rd_Shrs(dbgi, irg, block, bitop_left, right, mode);
4835 tv_shift = tarval_shrs(tv1, tv2);
4838 new_shift = new_rd_Rotl(dbgi, irg, block, bitop_left, right, mode);
4839 tv_shift = tarval_rotl(tv1, tv2);
4842 assert(get_tarval_mode(tv_shift) == mode);
4843 new_const = new_Const(mode, tv_shift);
4845 if (op_left == op_And) {
4846 new_bitop = new_rd_And(dbgi, irg, block, new_shift, new_const, mode);
4847 } else if(op_left == op_Or) {
4848 new_bitop = new_rd_Or(dbgi, irg, block, new_shift, new_const, mode);
4850 assert(op_left == op_Eor);
4851 new_bitop = new_rd_Eor(dbgi, irg, block, new_shift, new_const, mode);
4859 * (x << c1) >> c2 <=> x OP (c2-c1) & ((-1 << c1) >> c2)
4861 * (x >> c1) << c2 <=> x OP (c2-c1) & ((-1 >> c1) << c2)
4862 * (also with x >>s c1 when c1>=c2)
4864 static ir_node *transform_node_shl_shr(ir_node *n) {
4866 ir_node *right = get_binop_right(n);
4882 assert(is_Shl(n) || is_Shr(n) || is_Shrs(n));
4884 if (!is_Const(right))
4887 left = get_binop_left(n);
4888 mode = get_irn_mode(n);
4889 if (is_Shl(n) && (is_Shr(left) || is_Shrs(left))) {
4890 ir_node *shr_right = get_binop_right(left);
4892 if (!is_Const(shr_right))
4895 x = get_binop_left(left);
4896 tv_shr = get_Const_tarval(shr_right);
4897 tv_shl = get_Const_tarval(right);
4899 if (is_Shrs(left)) {
4900 /* shrs variant only allowed if c1 >= c2 */
4901 if (! (tarval_cmp(tv_shl, tv_shr) & pn_Cmp_Ge))
4904 tv_mask = tarval_shrs(get_mode_all_one(mode), tv_shr);
4907 tv_mask = tarval_shr(get_mode_all_one(mode), tv_shr);
4909 tv_mask = tarval_shl(tv_mask, tv_shl);
4910 } else if(is_Shr(n) && is_Shl(left)) {
4911 ir_node *shl_right = get_Shl_right(left);
4913 if (!is_Const(shl_right))
4916 x = get_Shl_left(left);
4917 tv_shr = get_Const_tarval(right);
4918 tv_shl = get_Const_tarval(shl_right);
4920 tv_mask = tarval_shl(get_mode_all_one(mode), tv_shl);
4921 tv_mask = tarval_shr(tv_mask, tv_shr);
4926 assert(get_tarval_mode(tv_shl) == get_tarval_mode(tv_shr));
4927 assert(tv_mask != tarval_bad);
4928 assert(get_tarval_mode(tv_mask) == mode);
4930 irg = get_irn_irg(n);
4931 block = get_nodes_block(n);
4932 dbgi = get_irn_dbg_info(n);
4934 pnc = tarval_cmp(tv_shl, tv_shr);
4935 if (pnc == pn_Cmp_Lt || pnc == pn_Cmp_Eq) {
4936 tv_shift = tarval_sub(tv_shr, tv_shl, NULL);
4937 new_const = new_Const(get_tarval_mode(tv_shift), tv_shift);
4939 new_shift = new_rd_Shrs(dbgi, irg, block, x, new_const, mode);
4941 new_shift = new_rd_Shr(dbgi, irg, block, x, new_const, mode);
4944 assert(pnc == pn_Cmp_Gt);
4945 tv_shift = tarval_sub(tv_shl, tv_shr, NULL);
4946 new_const = new_Const(get_tarval_mode(tv_shift), tv_shift);
4947 new_shift = new_rd_Shl(dbgi, irg, block, x, new_const, mode);
4950 new_const = new_Const(mode, tv_mask);
4951 new_and = new_rd_And(dbgi, irg, block, new_shift, new_const, mode);
4959 static ir_node *transform_node_Shr(ir_node *n) {
4960 ir_node *c, *oldn = n;
4961 ir_node *left = get_Shr_left(n);
4962 ir_node *right = get_Shr_right(n);
4963 ir_mode *mode = get_irn_mode(n);
4965 HANDLE_BINOP_PHI(tarval_shr, left, right, c, mode);
4966 n = transform_node_shift(n);
4969 n = transform_node_shl_shr(n);
4971 n = transform_node_bitop_shift(n);
4974 } /* transform_node_Shr */
4979 static ir_node *transform_node_Shrs(ir_node *n) {
4980 ir_node *c, *oldn = n;
4981 ir_node *a = get_Shrs_left(n);
4982 ir_node *b = get_Shrs_right(n);
4983 ir_mode *mode = get_irn_mode(n);
4985 HANDLE_BINOP_PHI(tarval_shrs, a, b, c, mode);
4986 n = transform_node_shift(n);
4989 n = transform_node_bitop_shift(n);
4992 } /* transform_node_Shrs */
4997 static ir_node *transform_node_Shl(ir_node *n) {
4998 ir_node *c, *oldn = n;
4999 ir_node *a = get_Shl_left(n);
5000 ir_node *b = get_Shl_right(n);
5001 ir_mode *mode = get_irn_mode(n);
5003 HANDLE_BINOP_PHI(tarval_shl, a, b, c, mode);
5004 n = transform_node_shift(n);
5007 n = transform_node_shl_shr(n);
5009 n = transform_node_bitop_shift(n);
5012 } /* transform_node_Shl */
5017 static ir_node *transform_node_Rotl(ir_node *n) {
5018 ir_node *c, *oldn = n;
5019 ir_node *a = get_Rotl_left(n);
5020 ir_node *b = get_Rotl_right(n);
5021 ir_mode *mode = get_irn_mode(n);
5023 HANDLE_BINOP_PHI(tarval_rotl, a, b, c, mode);
5024 n = transform_node_shift(n);
5027 n = transform_node_bitop_shift(n);
5030 } /* transform_node_Rotl */
5035 static ir_node *transform_node_Conv(ir_node *n) {
5036 ir_node *c, *oldn = n;
5037 ir_node *a = get_Conv_op(n);
5039 if (is_const_Phi(a)) {
5040 c = apply_conv_on_phi(a, get_irn_mode(n));
5042 DBG_OPT_ALGSIM0(oldn, c, FS_OPT_CONST_PHI);
5047 if (is_Unknown(a)) { /* Conv_A(Unknown_B) -> Unknown_A */
5048 ir_mode *mode = get_irn_mode(n);
5049 return new_r_Unknown(current_ir_graph, mode);
5053 } /* transform_node_Conv */
5056 * Remove dead blocks and nodes in dead blocks
5057 * in keep alive list. We do not generate a new End node.
5059 static ir_node *transform_node_End(ir_node *n) {
5060 int i, j, n_keepalives = get_End_n_keepalives(n);
5063 NEW_ARR_A(ir_node *, in, n_keepalives);
5065 for (i = j = 0; i < n_keepalives; ++i) {
5066 ir_node *ka = get_End_keepalive(n, i);
5068 if (! is_Block_dead(ka)) {
5072 } else if (is_irn_pinned_in_irg(ka) && is_Block_dead(get_nodes_block(ka))) {
5075 /* FIXME: beabi need to keep a Proj(M) */
5076 if (is_Phi(ka) || is_irn_keep(ka) || is_Proj(ka))
5079 if (j != n_keepalives)
5080 set_End_keepalives(n, j, in);
5082 } /* transform_node_End */
5084 /** returns 1 if a == -b */
5085 static int is_negated_value(ir_node *a, ir_node *b) {
5086 if (is_Minus(a) && get_Minus_op(a) == b)
5088 if (is_Minus(b) && get_Minus_op(b) == a)
5090 if (is_Sub(a) && is_Sub(b)) {
5091 ir_node *a_left = get_Sub_left(a);
5092 ir_node *a_right = get_Sub_right(a);
5093 ir_node *b_left = get_Sub_left(b);
5094 ir_node *b_right = get_Sub_right(b);
5096 if (a_left == b_right && a_right == b_left)
5104 * Optimize a Mux into some simpler cases.
5106 static ir_node *transform_node_Mux(ir_node *n) {
5107 ir_node *oldn = n, *sel = get_Mux_sel(n);
5108 ir_mode *mode = get_irn_mode(n);
5109 ir_node *t = get_Mux_true(n);
5110 ir_node *f = get_Mux_false(n);
5111 ir_graph *irg = current_ir_graph;
5112 ir_node *conds[1], *vals[2];
5114 /* first normalization step: move a possible zero to the false case */
5116 ir_node *cmp = get_Proj_pred(sel);
5119 if (is_Const(t) && is_Const_null(t)) {
5120 /* Psi(x, 0, y) => Psi(x, y, 0) */
5121 pn_Cmp pnc = get_Proj_proj(sel);
5122 sel = new_r_Proj(irg, get_nodes_block(cmp), cmp, mode_b,
5123 get_negated_pnc(pnc, get_irn_mode(get_Cmp_left(cmp))));
5127 n = new_rd_Psi(get_irn_dbg_info(n), irg, get_nodes_block(n), 1, conds, vals, mode);
5134 /* note: after normalization, false can only happen on default */
5135 if (mode == mode_b) {
5136 dbg_info *dbg = get_irn_dbg_info(n);
5137 ir_node *block = get_nodes_block(n);
5138 ir_graph *irg = current_ir_graph;
5141 tarval *tv_t = get_Const_tarval(t);
5142 if (tv_t == tarval_b_true) {
5144 /* Muxb(sel, true, false) = sel */
5145 assert(get_Const_tarval(f) == tarval_b_false);
5146 DBG_OPT_ALGSIM0(oldn, sel, FS_OPT_MUX_BOOL);
5149 /* Muxb(sel, true, x) = Or(sel, x) */
5150 n = new_rd_Or(dbg, irg, block, sel, f, mode_b);
5151 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_OR_BOOL);
5155 } else if (is_Const(f)) {
5156 tarval *tv_f = get_Const_tarval(f);
5157 if (tv_f == tarval_b_true) {
5158 /* Muxb(sel, x, true) = Or(Not(sel), x) */
5159 ir_node* not_sel = new_rd_Not(dbg, irg, block, sel, mode_b);
5160 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_ORNOT_BOOL);
5161 n = new_rd_Or(dbg, irg, block, not_sel, t, mode_b);
5164 /* Muxb(sel, x, false) = And(sel, x) */
5165 assert(tv_f == tarval_b_false);
5166 n = new_rd_And(dbg, irg, block, sel, t, mode_b);
5167 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_AND_BOOL);
5173 /* more normalization: try to normalize Mux(x, C1, C2) into Mux(x, +1/-1, 0) op C2 */
5174 if (is_Const(t) && is_Const(f) && mode_is_int(mode)) {
5175 tarval *a = get_Const_tarval(t);
5176 tarval *b = get_Const_tarval(f);
5177 tarval *null = get_tarval_null(mode);
5180 if (tarval_cmp(a, b) & pn_Cmp_Gt) {
5181 diff = tarval_sub(a, b, NULL);
5184 diff = tarval_sub(b, a, NULL);
5188 if (diff == get_tarval_one(mode) && min != null) {
5189 dbg_info *dbg = get_irn_dbg_info(n);
5190 ir_node *block = get_nodes_block(n);
5191 ir_graph *irg = current_ir_graph;
5195 vals[0] = new_Const(mode, tarval_sub(a, min, NULL));
5196 vals[1] = new_Const(mode, tarval_sub(b, min, NULL));
5197 n = new_rd_Psi(dbg, irg, block, 1, conds, vals, mode);
5198 n = new_rd_Add(dbg, irg, block, n, new_Const(mode, min), mode);
5204 ir_node *cmp = get_Proj_pred(sel);
5205 long pn = get_Proj_proj(sel);
5208 * Note: normalization puts the constant on the right side,
5209 * so we check only one case.
5211 * Note further that these optimization work even for floating point
5212 * with NaN's because -NaN == NaN.
5213 * However, if +0 and -0 is handled differently, we cannot use the Abs/-Abs
5217 ir_node *cmp_r = get_Cmp_right(cmp);
5218 if (is_Const(cmp_r) && is_Const_null(cmp_r)) {
5219 ir_node *block = get_nodes_block(n);
5220 ir_node *cmp_l = get_Cmp_left(cmp);
5222 if (!mode_honor_signed_zeros(mode) && is_negated_value(f, t)) {
5225 if ( (cmp_l == t && (pn == pn_Cmp_Ge || pn == pn_Cmp_Gt))
5226 || (cmp_l == f && (pn == pn_Cmp_Le || pn == pn_Cmp_Lt)))
5228 /* Psi(a >/>= 0, a, -a) = Psi(a </<= 0, -a, a) ==> Abs(a) */
5229 n = new_rd_Abs(get_irn_dbg_info(n), current_ir_graph, block,
5231 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_ABS);
5233 } else if ((cmp_l == t && (pn == pn_Cmp_Le || pn == pn_Cmp_Lt))
5234 || (cmp_l == f && (pn == pn_Cmp_Ge || pn == pn_Cmp_Gt)))
5236 /* Psi(a </<= 0, a, -a) = Psi(a >/>= 0, -a, a) ==> -Abs(a) */
5237 n = new_rd_Abs(get_irn_dbg_info(n), current_ir_graph, block,
5239 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph,
5241 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_ABS);
5246 if (mode_is_int(mode)) {
5248 if ((pn == pn_Cmp_Lg || pn == pn_Cmp_Eq) && is_And(cmp_l)) {
5249 /* Psi((a & b) != 0, c, 0) */
5250 ir_node *and_r = get_And_right(cmp_l);
5253 if (and_r == t && f == cmp_r) {
5254 if (is_Const(t) && tarval_is_single_bit(get_Const_tarval(t))) {
5255 if (pn == pn_Cmp_Lg) {
5256 /* Psi((a & 2^C) != 0, 2^C, 0) */
5259 /* Psi((a & 2^C) == 0, 2^C, 0) */
5260 n = new_rd_Eor(get_irn_dbg_info(n), current_ir_graph,
5261 block, cmp_l, t, mode);
5266 if (is_Shl(and_r)) {
5267 ir_node *shl_l = get_Shl_left(and_r);
5268 if (is_Const(shl_l) && is_Const_one(shl_l)) {
5269 if (and_r == t && f == cmp_r) {
5270 if (pn == pn_Cmp_Lg) {
5271 /* (a & (1 << n)) != 0, (1 << n), 0) */
5274 /* (a & (1 << n)) == 0, (1 << n), 0) */
5275 n = new_rd_Eor(get_irn_dbg_info(n), current_ir_graph,
5276 block, cmp_l, t, mode);
5282 and_l = get_And_left(cmp_l);
5283 if (is_Shl(and_l)) {
5284 ir_node *shl_l = get_Shl_left(and_l);
5285 if (is_Const(shl_l) && is_Const_one(shl_l)) {
5286 if (and_l == t && f == cmp_r) {
5287 if (pn == pn_Cmp_Lg) {
5288 /* ((1 << n) & a) != 0, (1 << n), 0) */
5291 /* ((1 << n) & a) == 0, (1 << n), 0) */
5292 n = new_rd_Eor(get_irn_dbg_info(n), current_ir_graph,
5293 block, cmp_l, t, mode);
5304 return arch_transform_node_Mux(n);
5305 } /* transform_node_Mux */
5308 * Optimize a Psi into some simpler cases.
5310 static ir_node *transform_node_Psi(ir_node *n) {
5312 return transform_node_Mux(n);
5315 } /* transform_node_Psi */
5318 * optimize sync nodes that have other syncs as input we simply add the inputs
5319 * of the other sync to our own inputs
5321 static ir_node *transform_node_Sync(ir_node *n) {
5322 int arity = get_Sync_n_preds(n);
5325 for (i = 0; i < arity;) {
5326 ir_node *pred = get_Sync_pred(n, i);
5330 if (!is_Sync(pred)) {
5338 pred_arity = get_Sync_n_preds(pred);
5339 for (j = 0; j < pred_arity; ++j) {
5340 ir_node *pred_pred = get_Sync_pred(pred, j);
5345 add_irn_n(n, pred_pred);
5349 if (get_Sync_pred(n, k) == pred_pred) break;
5354 /* rehash the sync node */
5355 add_identities(current_ir_graph->value_table, n);
5361 * Tries several [inplace] [optimizing] transformations and returns an
5362 * equivalent node. The difference to equivalent_node() is that these
5363 * transformations _do_ generate new nodes, and thus the old node must
5364 * not be freed even if the equivalent node isn't the old one.
5366 static ir_node *transform_node(ir_node *n) {
5370 * Transform_node is the only "optimizing transformation" that might
5371 * return a node with a different opcode. We iterate HERE until fixpoint
5372 * to get the final result.
5376 if (n->op->ops.transform_node)
5377 n = n->op->ops.transform_node(n);
5378 } while (oldn != n);
5381 } /* transform_node */
5384 * Sets the default transform node operation for an ir_op_ops.
5386 * @param code the opcode for the default operation
5387 * @param ops the operations initialized
5392 static ir_op_ops *firm_set_default_transform_node(ir_opcode code, ir_op_ops *ops)
5396 ops->transform_node = transform_node_##a; \
5433 } /* firm_set_default_transform_node */
5436 /* **************** Common Subexpression Elimination **************** */
5438 /** The size of the hash table used, should estimate the number of nodes
5440 #define N_IR_NODES 512
5442 /** Compares the attributes of two Const nodes. */
5443 static int node_cmp_attr_Const(ir_node *a, ir_node *b) {
5444 return (get_Const_tarval(a) != get_Const_tarval(b))
5445 || (get_Const_type(a) != get_Const_type(b));
5446 } /* node_cmp_attr_Const */
5448 /** Compares the attributes of two Proj nodes. */
5449 static int node_cmp_attr_Proj(ir_node *a, ir_node *b) {
5450 return get_irn_proj_attr(a) != get_irn_proj_attr(b);
5451 } /* node_cmp_attr_Proj */
5453 /** Compares the attributes of two Filter nodes. */
5454 static int node_cmp_attr_Filter(ir_node *a, ir_node *b) {
5455 return get_Filter_proj(a) != get_Filter_proj(b);
5456 } /* node_cmp_attr_Filter */
5458 /** Compares the attributes of two Alloc nodes. */
5459 static int node_cmp_attr_Alloc(ir_node *a, ir_node *b) {
5460 const alloc_attr *pa = get_irn_alloc_attr(a);
5461 const alloc_attr *pb = get_irn_alloc_attr(b);
5462 return (pa->where != pb->where) || (pa->type != pb->type);
5463 } /* node_cmp_attr_Alloc */
5465 /** Compares the attributes of two Free nodes. */
5466 static int node_cmp_attr_Free(ir_node *a, ir_node *b) {
5467 const free_attr *pa = get_irn_free_attr(a);
5468 const free_attr *pb = get_irn_free_attr(b);
5469 return (pa->where != pb->where) || (pa->type != pb->type);
5470 } /* node_cmp_attr_Free */
5472 /** Compares the attributes of two SymConst nodes. */
5473 static int node_cmp_attr_SymConst(ir_node *a, ir_node *b) {
5474 const symconst_attr *pa = get_irn_symconst_attr(a);
5475 const symconst_attr *pb = get_irn_symconst_attr(b);
5476 return (pa->kind != pb->kind)
5477 || (pa->sym.type_p != pb->sym.type_p)
5478 || (pa->tp != pb->tp);
5479 } /* node_cmp_attr_SymConst */
5481 /** Compares the attributes of two Call nodes. */
5482 static int node_cmp_attr_Call(ir_node *a, ir_node *b) {
5483 return get_irn_call_attr(a) != get_irn_call_attr(b);
5484 } /* node_cmp_attr_Call */
5486 /** Compares the attributes of two Sel nodes. */
5487 static int node_cmp_attr_Sel(ir_node *a, ir_node *b) {
5488 const ir_entity *a_ent = get_Sel_entity(a);
5489 const ir_entity *b_ent = get_Sel_entity(b);
5491 (a_ent->kind != b_ent->kind) ||
5492 (a_ent->name != b_ent->name) ||
5493 (a_ent->owner != b_ent->owner) ||
5494 (a_ent->ld_name != b_ent->ld_name) ||
5495 (a_ent->type != b_ent->type);
5496 } /* node_cmp_attr_Sel */
5498 /** Compares the attributes of two Phi nodes. */
5499 static int node_cmp_attr_Phi(ir_node *a, ir_node *b) {
5500 /* we can only enter this function if both nodes have the same number of inputs,
5501 hence it is enough to check if one of them is a Phi0 */
5503 /* check the Phi0 pos attribute */
5504 return get_irn_phi_attr(a)->u.pos != get_irn_phi_attr(b)->u.pos;
5507 } /* node_cmp_attr_Phi */
5509 /** Compares the attributes of two Conv nodes. */
5510 static int node_cmp_attr_Conv(ir_node *a, ir_node *b) {
5511 return get_Conv_strict(a) != get_Conv_strict(b);
5512 } /* node_cmp_attr_Conv */
5514 /** Compares the attributes of two Cast nodes. */
5515 static int node_cmp_attr_Cast(ir_node *a, ir_node *b) {
5516 return get_Cast_type(a) != get_Cast_type(b);
5517 } /* node_cmp_attr_Cast */
5519 /** Compares the attributes of two Load nodes. */
5520 static int node_cmp_attr_Load(ir_node *a, ir_node *b) {
5521 if (get_Load_volatility(a) == volatility_is_volatile ||
5522 get_Load_volatility(b) == volatility_is_volatile)
5523 /* NEVER do CSE on volatile Loads */
5525 /* do not CSE Loads with different alignment. Be conservative. */
5526 if (get_Load_align(a) != get_Load_align(b))
5529 return get_Load_mode(a) != get_Load_mode(b);
5530 } /* node_cmp_attr_Load */
5532 /** Compares the attributes of two Store nodes. */
5533 static int node_cmp_attr_Store(ir_node *a, ir_node *b) {
5534 /* do not CSE Stores with different alignment. Be conservative. */
5535 if (get_Store_align(a) != get_Store_align(b))
5538 /* NEVER do CSE on volatile Stores */
5539 return (get_Store_volatility(a) == volatility_is_volatile ||
5540 get_Store_volatility(b) == volatility_is_volatile);
5541 } /* node_cmp_attr_Store */
5543 /** Compares two exception attributes */
5544 static int node_cmp_exception(ir_node *a, ir_node *b) {
5545 const except_attr *ea = get_irn_except_attr(a);
5546 const except_attr *eb = get_irn_except_attr(b);
5548 return ea->pin_state != eb->pin_state;
5551 #define node_cmp_attr_Bound node_cmp_exception
5553 /** Compares the attributes of two Div nodes. */
5554 static int node_cmp_attr_Div(ir_node *a, ir_node *b) {
5555 const divmod_attr *ma = get_irn_divmod_attr(a);
5556 const divmod_attr *mb = get_irn_divmod_attr(b);
5557 return ma->exc.pin_state != mb->exc.pin_state ||
5558 ma->res_mode != mb->res_mode ||
5559 ma->no_remainder != mb->no_remainder;
5560 } /* node_cmp_attr_Div */
5562 /** Compares the attributes of two DivMod nodes. */
5563 static int node_cmp_attr_DivMod(ir_node *a, ir_node *b) {
5564 const divmod_attr *ma = get_irn_divmod_attr(a);
5565 const divmod_attr *mb = get_irn_divmod_attr(b);
5566 return ma->exc.pin_state != mb->exc.pin_state ||
5567 ma->res_mode != mb->res_mode;
5568 } /* node_cmp_attr_DivMod */
5570 /** Compares the attributes of two Mod nodes. */
5571 static int node_cmp_attr_Mod(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 } /* node_cmp_attr_Mod */
5578 /** Compares the attributes of two Quot nodes. */
5579 static int node_cmp_attr_Quot(ir_node *a, ir_node *b) {
5580 const divmod_attr *ma = get_irn_divmod_attr(a);
5581 const divmod_attr *mb = get_irn_divmod_attr(b);
5582 return ma->exc.pin_state != mb->exc.pin_state ||
5583 ma->res_mode != mb->res_mode;
5584 } /* node_cmp_attr_Quot */
5586 /** Compares the attributes of two Confirm nodes. */
5587 static int node_cmp_attr_Confirm(ir_node *a, ir_node *b) {
5588 return (get_Confirm_cmp(a) != get_Confirm_cmp(b));
5589 } /* node_cmp_attr_Confirm */
5591 /** Compares the attributes of two ASM nodes. */
5592 static int node_cmp_attr_ASM(ir_node *a, ir_node *b) {
5594 const ir_asm_constraint *ca;
5595 const ir_asm_constraint *cb;
5598 if (get_ASM_text(a) != get_ASM_text(b))
5601 /* Should we really check the constraints here? Should be better, but is strange. */
5602 n = get_ASM_n_input_constraints(a);
5603 if (n != get_ASM_n_input_constraints(b))
5606 ca = get_ASM_input_constraints(a);
5607 cb = get_ASM_input_constraints(b);
5608 for (i = 0; i < n; ++i) {
5609 if (ca[i].pos != cb[i].pos || ca[i].constraint != cb[i].constraint)
5613 n = get_ASM_n_output_constraints(a);
5614 if (n != get_ASM_n_output_constraints(b))
5617 ca = get_ASM_output_constraints(a);
5618 cb = get_ASM_output_constraints(b);
5619 for (i = 0; i < n; ++i) {
5620 if (ca[i].pos != cb[i].pos || ca[i].constraint != cb[i].constraint)
5624 n = get_ASM_n_clobbers(a);
5625 if (n != get_ASM_n_clobbers(b))
5628 cla = get_ASM_clobbers(a);
5629 clb = get_ASM_clobbers(b);
5630 for (i = 0; i < n; ++i) {
5631 if (cla[i] != clb[i])
5635 } /* node_cmp_attr_ASM */
5638 * Set the default node attribute compare operation for an ir_op_ops.
5640 * @param code the opcode for the default operation
5641 * @param ops the operations initialized
5646 static ir_op_ops *firm_set_default_node_cmp_attr(ir_opcode code, ir_op_ops *ops)
5650 ops->node_cmp_attr = node_cmp_attr_##a; \
5681 } /* firm_set_default_node_cmp_attr */
5684 * Compare function for two nodes in the value table. Gets two
5685 * nodes as parameters. Returns 0 if the nodes are a Common Sub Expression.
5687 int identities_cmp(const void *elt, const void *key) {
5688 ir_node *a = (ir_node *)elt;
5689 ir_node *b = (ir_node *)key;
5692 if (a == b) return 0;
5694 if ((get_irn_op(a) != get_irn_op(b)) ||
5695 (get_irn_mode(a) != get_irn_mode(b))) return 1;
5697 /* compare if a's in and b's in are of equal length */
5698 irn_arity_a = get_irn_intra_arity(a);
5699 if (irn_arity_a != get_irn_intra_arity(b))
5702 if (get_irn_pinned(a) == op_pin_state_pinned) {
5703 /* for pinned nodes, the block inputs must be equal */
5704 if (get_irn_intra_n(a, -1) != get_irn_intra_n(b, -1))
5706 } else if (! get_opt_global_cse()) {
5707 /* for block-local CSE both nodes must be in the same MacroBlock */
5708 if (get_irn_MacroBlock(a) != get_irn_MacroBlock(b))
5712 /* compare a->in[0..ins] with b->in[0..ins] */
5713 for (i = 0; i < irn_arity_a; i++)
5714 if (get_irn_intra_n(a, i) != get_irn_intra_n(b, i))
5718 * here, we already now that the nodes are identical except their
5721 if (a->op->ops.node_cmp_attr)
5722 return a->op->ops.node_cmp_attr(a, b);
5725 } /* identities_cmp */
5728 * Calculate a hash value of a node.
5730 * @param node The IR-node
5732 unsigned ir_node_hash(const ir_node *node) {
5733 return node->op->ops.hash(node);
5734 } /* ir_node_hash */
5737 pset *new_identities(void) {
5738 return new_pset(identities_cmp, N_IR_NODES);
5739 } /* new_identities */
5741 void del_identities(pset *value_table) {
5742 del_pset(value_table);
5743 } /* del_identities */
5746 * Normalize a node by putting constants (and operands with larger
5747 * node index) on the right (operator side).
5749 * @param n The node to normalize
5751 static void normalize_node(ir_node *n) {
5752 if (is_op_commutative(get_irn_op(n))) {
5753 ir_node *l = get_binop_left(n);
5754 ir_node *r = get_binop_right(n);
5756 /* For commutative operators perform a OP b == b OP a but keep
5757 * constants on the RIGHT side. This helps greatly in some
5758 * optimizations. Moreover we use the idx number to make the form
5760 if (!operands_are_normalized(l, r)) {
5761 set_binop_left(n, r);
5762 set_binop_right(n, l);
5765 } /* normalize_node */
5768 * Update the nodes after a match in the value table. If both nodes have
5769 * the same MacroBlock but different Blocks, we must ensure that the node
5770 * with the dominating Block (the node that is near to the MacroBlock header
5771 * is stored in the table.
5772 * Because a MacroBlock has only one "non-exception" flow, we don't need
5773 * dominance info here: We known, that one block must dominate the other and
5774 * following the only block input will allow to find it.
5776 static void update_known_irn(ir_node *known_irn, const ir_node *new_ir_node) {
5777 ir_node *known_blk, *new_block, *block, *mbh;
5779 if (get_opt_global_cse()) {
5780 /* Block inputs are meaning less */
5783 known_blk = get_irn_n(known_irn, -1);
5784 new_block = get_irn_n(new_ir_node, -1);
5785 if (known_blk == new_block) {
5786 /* already in the same block */
5790 * We expect the typical case when we built the graph. In that case, the
5791 * known_irn is already the upper one, so checking this should be faster.
5794 mbh = get_Block_MacroBlock(new_block);
5796 if (block == known_blk) {
5797 /* ok, we have found it: known_block dominates new_block as expected */
5802 * We have reached the MacroBlock header NOT founding
5803 * the known_block. new_block must dominate known_block.
5806 set_irn_n(known_irn, -1, new_block);
5809 assert(get_Block_n_cfgpreds(block) == 1);
5810 block = get_Block_cfgpred_block(block, 0);
5812 } /* update_value_table */
5815 * Return the canonical node computing the same value as n.
5816 * Looks up the node in a hash table, enters it in the table
5817 * if it isn't there yet.
5819 * @param value_table the HashSet containing all nodes in the
5821 * @param n the node to look up
5823 * @return a node that computes the same value as n or n if no such
5824 * node could be found
5826 ir_node *identify_remember(pset *value_table, ir_node *n) {
5829 if (!value_table) return n;
5832 /* lookup or insert in hash table with given hash key. */
5833 o = pset_insert(value_table, n, ir_node_hash(n));
5836 update_known_irn(o, n);
5841 } /* identify_remember */
5844 * During construction we set the op_pin_state_pinned flag in the graph right when the
5845 * optimization is performed. The flag turning on procedure global cse could
5846 * be changed between two allocations. This way we are safe.
5848 * @param value_table The value table
5849 * @param n The node to lookup
5851 static INLINE ir_node *identify_cons(pset *value_table, ir_node *n) {
5854 n = identify_remember(value_table, n);
5855 if (n != old && get_irn_MacroBlock(old) != get_irn_MacroBlock(n))
5856 set_irg_pinned(current_ir_graph, op_pin_state_floats);
5858 } /* identify_cons */
5860 /* Add a node to the identities value table. */
5861 void add_identities(pset *value_table, ir_node *node) {
5862 if (get_opt_cse() && is_no_Block(node))
5863 identify_remember(value_table, node);
5864 } /* add_identities */
5866 /* Visit each node in the value table of a graph. */
5867 void visit_all_identities(ir_graph *irg, irg_walk_func visit, void *env) {
5869 ir_graph *rem = current_ir_graph;
5871 current_ir_graph = irg;
5872 foreach_pset(irg->value_table, node)
5874 current_ir_graph = rem;
5875 } /* visit_all_identities */
5878 * Garbage in, garbage out. If a node has a dead input, i.e., the
5879 * Bad node is input to the node, return the Bad node.
5881 static ir_node *gigo(ir_node *node) {
5883 ir_op *op = get_irn_op(node);
5885 /* remove garbage blocks by looking at control flow that leaves the block
5886 and replacing the control flow by Bad. */
5887 if (get_irn_mode(node) == mode_X) {
5888 ir_node *block = get_nodes_block(skip_Proj(node));
5890 /* Don't optimize nodes in immature blocks. */
5891 if (!get_Block_matured(block))
5893 /* Don't optimize End, may have Bads. */
5894 if (op == op_End) return node;
5896 if (is_Block(block)) {
5897 if (is_Block_dead(block)) {
5898 /* control flow from dead block is dead */
5902 for (i = get_irn_arity(block) - 1; i >= 0; --i) {
5903 if (!is_Bad(get_irn_n(block, i)))
5907 ir_graph *irg = get_irn_irg(block);
5908 /* the start block is never dead */
5909 if (block != get_irg_start_block(irg)
5910 && block != get_irg_end_block(irg)) {
5912 * Do NOT kill control flow without setting
5913 * the block to dead of bad things can happen:
5914 * We get a Block that is not reachable be irg_block_walk()
5915 * but can be found by irg_walk()!
5917 set_Block_dead(block);
5924 /* Blocks, Phis and Tuples may have dead inputs, e.g., if one of the
5925 blocks predecessors is dead. */
5926 if (op != op_Block && op != op_Phi && op != op_Tuple) {
5927 irn_arity = get_irn_arity(node);
5930 * Beware: we can only read the block of a non-floating node.
5932 if (is_irn_pinned_in_irg(node) &&
5933 is_Block_dead(get_nodes_block(skip_Proj(node))))
5936 for (i = 0; i < irn_arity; i++) {
5937 ir_node *pred = get_irn_n(node, i);
5942 /* Propagating Unknowns here seems to be a bad idea, because
5943 sometimes we need a node as a input and did not want that
5945 However, it might be useful to move this into a later phase
5946 (if you think that optimizing such code is useful). */
5947 if (is_Unknown(pred) && mode_is_data(get_irn_mode(node)))
5948 return new_Unknown(get_irn_mode(node));
5953 /* With this code we violate the agreement that local_optimize
5954 only leaves Bads in Block, Phi and Tuple nodes. */
5955 /* If Block has only Bads as predecessors it's garbage. */
5956 /* If Phi has only Bads as predecessors it's garbage. */
5957 if ((op == op_Block && get_Block_matured(node)) || op == op_Phi) {
5958 irn_arity = get_irn_arity(node);
5959 for (i = 0; i < irn_arity; i++) {
5960 if (!is_Bad(get_irn_n(node, i))) break;
5962 if (i == irn_arity) node = new_Bad();
5969 * These optimizations deallocate nodes from the obstack.
5970 * It can only be called if it is guaranteed that no other nodes
5971 * reference this one, i.e., right after construction of a node.
5973 * @param n The node to optimize
5975 * current_ir_graph must be set to the graph of the node!
5977 ir_node *optimize_node(ir_node *n) {
5980 ir_opcode iro = get_irn_opcode(n);
5982 /* Always optimize Phi nodes: part of the construction. */
5983 if ((!get_opt_optimize()) && (iro != iro_Phi)) return n;
5985 /* constant expression evaluation / constant folding */
5986 if (get_opt_constant_folding()) {
5987 /* neither constants nor Tuple values can be evaluated */
5988 if (iro != iro_Const && (get_irn_mode(n) != mode_T)) {
5989 unsigned fp_model = get_irg_fp_model(current_ir_graph);
5990 int old_fp_mode = tarval_enable_fp_ops((fp_model & fp_strict_algebraic) == 0);
5991 /* try to evaluate */
5992 tv = computed_value(n);
5993 if (tv != tarval_bad) {
5995 ir_type *old_tp = get_irn_type(n);
5996 int i, arity = get_irn_arity(n);
6000 * Try to recover the type of the new expression.
6002 for (i = 0; i < arity && !old_tp; ++i)
6003 old_tp = get_irn_type(get_irn_n(n, i));
6006 * we MUST copy the node here temporary, because it's still needed
6007 * for DBG_OPT_CSTEVAL
6009 node_size = offsetof(ir_node, attr) + n->op->attr_size;
6010 oldn = alloca(node_size);
6012 memcpy(oldn, n, node_size);
6013 CLONE_ARR_A(ir_node *, oldn->in, n->in);
6015 /* ARG, copy the in array, we need it for statistics */
6016 memcpy(oldn->in, n->in, ARR_LEN(n->in) * sizeof(n->in[0]));
6018 /* note the inplace edges module */
6019 edges_node_deleted(n, current_ir_graph);
6021 /* evaluation was successful -- replace the node. */
6022 irg_kill_node(current_ir_graph, n);
6023 nw = new_Const(get_tarval_mode(tv), tv);
6025 if (old_tp && get_type_mode(old_tp) == get_tarval_mode(tv))
6026 set_Const_type(nw, old_tp);
6027 DBG_OPT_CSTEVAL(oldn, nw);
6028 tarval_enable_fp_ops(old_fp_mode);
6031 tarval_enable_fp_ops(old_fp_mode);
6035 /* remove unnecessary nodes */
6036 if (get_opt_algebraic_simplification() ||
6037 (iro == iro_Phi) || /* always optimize these nodes. */
6039 (iro == iro_Proj) ||
6040 (iro == iro_Block) ) /* Flags tested local. */
6041 n = equivalent_node(n);
6043 /* Common Subexpression Elimination.
6045 * Checks whether n is already available.
6046 * The block input is used to distinguish different subexpressions. Right
6047 * now all nodes are op_pin_state_pinned to blocks, i.e., the CSE only finds common
6048 * subexpressions within a block.
6051 n = identify_cons(current_ir_graph->value_table, n);
6054 edges_node_deleted(oldn, current_ir_graph);
6056 /* We found an existing, better node, so we can deallocate the old node. */
6057 irg_kill_node(current_ir_graph, oldn);
6061 /* Some more constant expression evaluation that does not allow to
6063 iro = get_irn_opcode(n);
6064 if (get_opt_algebraic_simplification() ||
6065 (iro == iro_Cond) ||
6066 (iro == iro_Proj)) /* Flags tested local. */
6067 n = transform_node(n);
6069 /* Remove nodes with dead (Bad) input.
6070 Run always for transformation induced Bads. */
6073 /* Now we have a legal, useful node. Enter it in hash table for CSE */
6074 if (get_opt_cse() && (get_irn_opcode(n) != iro_Block)) {
6075 n = identify_remember(current_ir_graph->value_table, n);
6079 } /* optimize_node */
6083 * These optimizations never deallocate nodes (in place). This can cause dead
6084 * nodes lying on the obstack. Remove these by a dead node elimination,
6085 * i.e., a copying garbage collection.
6087 ir_node *optimize_in_place_2(ir_node *n) {
6090 ir_opcode iro = get_irn_opcode(n);
6092 if (!get_opt_optimize() && !is_Phi(n)) return n;
6094 /* constant expression evaluation / constant folding */
6095 if (get_opt_constant_folding()) {
6096 /* neither constants nor Tuple values can be evaluated */
6097 if (iro != iro_Const && get_irn_mode(n) != mode_T) {
6098 unsigned fp_model = get_irg_fp_model(current_ir_graph);
6099 int old_fp_mode = tarval_enable_fp_ops((fp_model & fp_strict_algebraic) == 0);
6100 /* try to evaluate */
6101 tv = computed_value(n);
6102 if (tv != tarval_bad) {
6103 /* evaluation was successful -- replace the node. */
6104 ir_type *old_tp = get_irn_type(n);
6105 int i, arity = get_irn_arity(n);
6108 * Try to recover the type of the new expression.
6110 for (i = 0; i < arity && !old_tp; ++i)
6111 old_tp = get_irn_type(get_irn_n(n, i));
6113 n = new_Const(get_tarval_mode(tv), tv);
6115 if (old_tp && get_type_mode(old_tp) == get_tarval_mode(tv))
6116 set_Const_type(n, old_tp);
6118 DBG_OPT_CSTEVAL(oldn, n);
6119 tarval_enable_fp_ops(old_fp_mode);
6122 tarval_enable_fp_ops(old_fp_mode);
6126 /* remove unnecessary nodes */
6127 if (get_opt_constant_folding() ||
6128 (iro == iro_Phi) || /* always optimize these nodes. */
6129 (iro == iro_Id) || /* ... */
6130 (iro == iro_Proj) || /* ... */
6131 (iro == iro_Block) ) /* Flags tested local. */
6132 n = equivalent_node(n);
6134 /** common subexpression elimination **/
6135 /* Checks whether n is already available. */
6136 /* The block input is used to distinguish different subexpressions. Right
6137 now all nodes are op_pin_state_pinned to blocks, i.e., the cse only finds common
6138 subexpressions within a block. */
6139 if (get_opt_cse()) {
6140 n = identify_remember(current_ir_graph->value_table, n);
6143 /* Some more constant expression evaluation. */
6144 iro = get_irn_opcode(n);
6145 if (get_opt_constant_folding() ||
6146 (iro == iro_Cond) ||
6147 (iro == iro_Proj)) /* Flags tested local. */
6148 n = transform_node(n);
6150 /* Remove nodes with dead (Bad) input.
6151 Run always for transformation induced Bads. */
6154 /* Now we can verify the node, as it has no dead inputs any more. */
6157 /* Now we have a legal, useful node. Enter it in hash table for cse.
6158 Blocks should be unique anyways. (Except the successor of start:
6159 is cse with the start block!) */
6160 if (get_opt_cse() && (get_irn_opcode(n) != iro_Block))
6161 n = identify_remember(current_ir_graph->value_table, n);
6164 } /* optimize_in_place_2 */
6167 * Wrapper for external use, set proper status bits after optimization.
6169 ir_node *optimize_in_place(ir_node *n) {
6170 /* Handle graph state */
6171 assert(get_irg_phase_state(current_ir_graph) != phase_building);
6173 if (get_opt_global_cse())
6174 set_irg_pinned(current_ir_graph, op_pin_state_floats);
6175 if (get_irg_outs_state(current_ir_graph) == outs_consistent)
6176 set_irg_outs_inconsistent(current_ir_graph);
6178 /* FIXME: Maybe we could also test whether optimizing the node can
6179 change the control graph. */
6180 set_irg_doms_inconsistent(current_ir_graph);
6181 return optimize_in_place_2(n);
6182 } /* optimize_in_place */
6185 * Calculate a hash value of a Const node.
6187 static unsigned hash_Const(const ir_node *node) {
6190 /* special value for const, as they only differ in their tarval. */
6191 h = HASH_PTR(node->attr.con.tv);
6192 h = 9*h + HASH_PTR(get_irn_mode(node));
6198 * Calculate a hash value of a SymConst node.
6200 static unsigned hash_SymConst(const ir_node *node) {
6203 /* special value for const, as they only differ in their symbol. */
6204 h = HASH_PTR(node->attr.symc.sym.type_p);
6205 h = 9*h + HASH_PTR(get_irn_mode(node));
6208 } /* hash_SymConst */
6211 * Set the default hash operation in an ir_op_ops.
6213 * @param code the opcode for the default operation
6214 * @param ops the operations initialized
6219 static ir_op_ops *firm_set_default_hash(ir_opcode code, ir_op_ops *ops)
6223 ops->hash = hash_##a; \
6226 /* hash function already set */
6227 if (ops->hash != NULL)
6234 /* use input/mode default hash if no function was given */
6235 ops->hash = firm_default_hash;
6243 * Sets the default operation for an ir_ops.
6245 ir_op_ops *firm_set_default_operations(ir_opcode code, ir_op_ops *ops) {
6246 ops = firm_set_default_hash(code, ops);
6247 ops = firm_set_default_computed_value(code, ops);
6248 ops = firm_set_default_equivalent_node(code, ops);
6249 ops = firm_set_default_transform_node(code, ops);
6250 ops = firm_set_default_node_cmp_attr(code, ops);
6251 ops = firm_set_default_get_type(code, ops);
6252 ops = firm_set_default_get_type_attr(code, ops);
6253 ops = firm_set_default_get_entity_attr(code, ops);
6256 } /* firm_set_default_operations */