2 * Copyright (C) 1995-2007 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
35 #include "irgraph_t.h"
36 #include "iredges_t.h"
43 #include "dbginfo_t.h"
44 #include "iropt_dbg.h"
50 #include "opt_polymorphy.h"
51 #include "opt_confirms.h"
55 /* Make types visible to allow most efficient access */
59 * Return the value of a Constant.
61 static tarval *computed_value_Const(ir_node *n) {
62 return get_Const_tarval(n);
63 } /* computed_value_Const */
66 * Return the value of a 'sizeof', 'alignof' or 'offsetof' SymConst.
68 static tarval *computed_value_SymConst(ir_node *n) {
72 switch (get_SymConst_kind(n)) {
73 case symconst_type_size:
74 type = get_SymConst_type(n);
75 if (get_type_state(type) == layout_fixed)
76 return new_tarval_from_long(get_type_size_bytes(type), get_irn_mode(n));
78 case symconst_type_align:
79 type = get_SymConst_type(n);
80 if (get_type_state(type) == layout_fixed)
81 return new_tarval_from_long(get_type_alignment_bytes(type), get_irn_mode(n));
83 case symconst_ofs_ent:
84 ent = get_SymConst_entity(n);
85 type = get_entity_owner(ent);
86 if (get_type_state(type) == layout_fixed)
87 return new_tarval_from_long(get_entity_offset(ent), get_irn_mode(n));
93 } /* computed_value_SymConst */
96 * Return the value of an Add.
98 static tarval *computed_value_Add(ir_node *n) {
99 ir_node *a = get_Add_left(n);
100 ir_node *b = get_Add_right(n);
102 tarval *ta = value_of(a);
103 tarval *tb = value_of(b);
105 if ((ta != tarval_bad) && (tb != tarval_bad) && (get_irn_mode(a) == get_irn_mode(b)))
106 return tarval_add(ta, tb);
109 } /* computed_value_Add */
112 * Return the value of a Sub.
113 * Special case: a - a
115 static tarval *computed_value_Sub(ir_node *n) {
116 ir_node *a = get_Sub_left(n);
117 ir_node *b = get_Sub_right(n);
122 if (a == b && !is_Bad(a))
123 return get_mode_null(get_irn_mode(n));
128 if ((ta != tarval_bad) && (tb != tarval_bad) && (get_irn_mode(a) == get_irn_mode(b)))
129 return tarval_sub(ta, tb);
132 } /* computed_value_Sub */
135 * Return the value of a Carry.
136 * Special : a op 0, 0 op b
138 static tarval *computed_value_Carry(ir_node *n) {
139 ir_node *a = get_binop_left(n);
140 ir_node *b = get_binop_right(n);
141 ir_mode *m = get_irn_mode(n);
143 tarval *ta = value_of(a);
144 tarval *tb = value_of(b);
146 if ((ta != tarval_bad) && (tb != tarval_bad)) {
148 return tarval_carry() ? get_mode_one(m) : get_mode_null(m);
150 if ( (classify_tarval(ta) == TV_CLASSIFY_NULL)
151 || (classify_tarval(tb) == TV_CLASSIFY_NULL))
152 return get_mode_null(m);
155 } /* computed_value_Carry */
158 * Return the value of a Borrow.
161 static tarval *computed_value_Borrow(ir_node *n) {
162 ir_node *a = get_binop_left(n);
163 ir_node *b = get_binop_right(n);
164 ir_mode *m = get_irn_mode(n);
166 tarval *ta = value_of(a);
167 tarval *tb = value_of(b);
169 if ((ta != tarval_bad) && (tb != tarval_bad)) {
170 return tarval_cmp(ta, tb) == pn_Cmp_Lt ? get_mode_one(m) : get_mode_null(m);
171 } else if (classify_tarval(ta) == TV_CLASSIFY_NULL) {
172 return get_mode_null(m);
175 } /* computed_value_Borrow */
178 * Return the value of an unary Minus.
180 static tarval *computed_value_Minus(ir_node *n) {
181 ir_node *a = get_Minus_op(n);
182 tarval *ta = value_of(a);
184 if ((ta != tarval_bad) && mode_is_signed(get_irn_mode(a)))
185 return tarval_neg(ta);
188 } /* computed_value_Minus */
191 * Return the value of a Mul.
193 static tarval *computed_value_Mul(ir_node *n) {
194 ir_node *a = get_Mul_left(n);
195 ir_node *b = get_Mul_right(n);
197 tarval *ta = value_of(a);
198 tarval *tb = value_of(b);
200 if ((ta != tarval_bad) && (tb != tarval_bad) && (get_irn_mode(a) == get_irn_mode(b))) {
201 return tarval_mul(ta, tb);
203 /* a*0 = 0 or 0*b = 0:
204 calls computed_value recursive and returns the 0 with proper
206 if ((ta != tarval_bad) && (ta == get_mode_null(get_tarval_mode(ta))))
208 if ((tb != tarval_bad) && (tb == get_mode_null(get_tarval_mode(tb))))
212 } /* computed_value_Mul */
215 * Return the value of a floating point Quot.
217 static tarval *computed_value_Quot(ir_node *n) {
218 ir_node *a = get_Quot_left(n);
219 ir_node *b = get_Quot_right(n);
221 tarval *ta = value_of(a);
222 tarval *tb = value_of(b);
224 /* This was missing in original implementation. Why? */
225 if ((ta != tarval_bad) && (tb != tarval_bad) && (get_irn_mode(a) == get_irn_mode(b))) {
226 if (tb != get_mode_null(get_tarval_mode(tb))) /* div by zero: return tarval_bad */
227 return tarval_quo(ta, tb);
230 } /* computed_value_Quot */
233 * Calculate the value of an integer Div of two nodes.
234 * Special case: 0 / b
236 static tarval *do_computed_value_Div(ir_node *a, ir_node *b) {
237 tarval *ta = value_of(a);
238 tarval *tb = value_of(b);
240 /* Compute c1 / c2 or 0 / a, a != 0 */
241 if (ta != tarval_bad) {
242 if ((tb != tarval_bad) && (tb != get_mode_null(get_irn_mode(b)))) /* div by zero: return tarval_bad */
243 return tarval_div(ta, tb);
244 else if (ta == get_mode_null(get_tarval_mode(ta))) /* 0 / b == 0 */
248 } /* do_computed_value_Div */
251 * Return the value of an integer Div.
253 static tarval *computed_value_Div(ir_node *n) {
254 return do_computed_value_Div(get_Div_left(n), get_Div_right(n));
255 } /* computed_value_Div */
258 * Calculate the value of an integer Mod of two nodes.
259 * Special case: a % 1
261 static tarval *do_computed_value_Mod(ir_node *a, ir_node *b) {
262 tarval *ta = value_of(a);
263 tarval *tb = value_of(b);
265 /* Compute c1 % c2 or a % 1 */
266 if (tb != tarval_bad) {
267 if ((ta != tarval_bad) && (tb != get_mode_null(get_tarval_mode(tb)))) /* div by zero: return tarval_bad */
268 return tarval_mod(ta, tb);
269 else if (tb == get_mode_one(get_tarval_mode(tb))) /* x mod 1 == 0 */
270 return get_mode_null(get_irn_mode(a));
273 } /* do_computed_value_Mod */
276 * Return the value of an integer Mod.
278 static tarval *computed_value_Mod(ir_node *n) {
279 return do_computed_value_Mod(get_Mod_left(n), get_Mod_right(n));
280 } /* computed_value_Mod */
283 * Return the value of an Abs.
285 static tarval *computed_value_Abs(ir_node *n) {
286 ir_node *a = get_Abs_op(n);
287 tarval *ta = value_of(a);
289 if (ta != tarval_bad)
290 return tarval_abs(ta);
293 } /* computed_value_Abs */
296 * Return the value of an And.
297 * Special case: a & 0, 0 & b
299 static tarval *computed_value_And(ir_node *n) {
300 ir_node *a = get_And_left(n);
301 ir_node *b = get_And_right(n);
303 tarval *ta = value_of(a);
304 tarval *tb = value_of(b);
306 if ((ta != tarval_bad) && (tb != tarval_bad)) {
307 return tarval_and (ta, tb);
311 if ( (classify_tarval ((v = ta)) == TV_CLASSIFY_NULL)
312 || (classify_tarval ((v = tb)) == TV_CLASSIFY_NULL)) {
317 } /* computed_value_And */
320 * Return the value of an Or.
321 * Special case: a | 1...1, 1...1 | b
323 static tarval *computed_value_Or(ir_node *n) {
324 ir_node *a = get_Or_left(n);
325 ir_node *b = get_Or_right(n);
327 tarval *ta = value_of(a);
328 tarval *tb = value_of(b);
330 if ((ta != tarval_bad) && (tb != tarval_bad)) {
331 return tarval_or (ta, tb);
334 if ( (classify_tarval ((v = ta)) == TV_CLASSIFY_ALL_ONE)
335 || (classify_tarval ((v = tb)) == TV_CLASSIFY_ALL_ONE)) {
340 } /* computed_value_Or */
343 * Return the value of an Eor.
345 static tarval *computed_value_Eor(ir_node *n) {
346 ir_node *a = get_Eor_left(n);
347 ir_node *b = get_Eor_right(n);
352 return get_mode_null(get_irn_mode(n));
357 if ((ta != tarval_bad) && (tb != tarval_bad)) {
358 return tarval_eor (ta, tb);
361 } /* computed_value_Eor */
364 * Return the value of a Not.
366 static tarval *computed_value_Not(ir_node *n) {
367 ir_node *a = get_Not_op(n);
368 tarval *ta = value_of(a);
370 if (ta != tarval_bad)
371 return tarval_not(ta);
374 } /* computed_value_Not */
377 * Return the value of a Shl.
379 static tarval *computed_value_Shl(ir_node *n) {
380 ir_node *a = get_Shl_left(n);
381 ir_node *b = get_Shl_right(n);
383 tarval *ta = value_of(a);
384 tarval *tb = value_of(b);
386 if ((ta != tarval_bad) && (tb != tarval_bad)) {
387 return tarval_shl (ta, tb);
390 } /* computed_value_Shl */
393 * Return the value of a Shr.
395 static tarval *computed_value_Shr(ir_node *n) {
396 ir_node *a = get_Shr_left(n);
397 ir_node *b = get_Shr_right(n);
399 tarval *ta = value_of(a);
400 tarval *tb = value_of(b);
402 if ((ta != tarval_bad) && (tb != tarval_bad)) {
403 return tarval_shr (ta, tb);
406 } /* computed_value_Shr */
409 * Return the value of a Shrs.
411 static tarval *computed_value_Shrs(ir_node *n) {
412 ir_node *a = get_Shrs_left(n);
413 ir_node *b = get_Shrs_right(n);
415 tarval *ta = value_of(a);
416 tarval *tb = value_of(b);
418 if ((ta != tarval_bad) && (tb != tarval_bad)) {
419 return tarval_shrs (ta, tb);
422 } /* computed_value_Shrs */
425 * Return the value of a Rot.
427 static tarval *computed_value_Rot(ir_node *n) {
428 ir_node *a = get_Rot_left(n);
429 ir_node *b = get_Rot_right(n);
431 tarval *ta = value_of(a);
432 tarval *tb = value_of(b);
434 if ((ta != tarval_bad) && (tb != tarval_bad)) {
435 return tarval_rot (ta, tb);
438 } /* computed_value_Rot */
441 * Return the value of a Conv.
443 static tarval *computed_value_Conv(ir_node *n) {
444 ir_node *a = get_Conv_op(n);
445 tarval *ta = value_of(a);
447 if (ta != tarval_bad)
448 return tarval_convert_to(ta, get_irn_mode(n));
451 } /* computed_value_Conv */
454 * Return the value of a Proj(Cmp).
456 * This performs a first step of unreachable code elimination.
457 * Proj can not be computed, but folding a Cmp above the Proj here is
458 * not as wasteful as folding a Cmp into a Tuple of 16 Consts of which
460 * There are several case where we can evaluate a Cmp node, see later.
462 static tarval *computed_value_Proj_Cmp(ir_node *n) {
463 ir_node *a = get_Proj_pred(n);
464 ir_node *aa = get_Cmp_left(a);
465 ir_node *ab = get_Cmp_right(a);
466 long proj_nr = get_Proj_proj(n);
469 * BEWARE: a == a is NOT always True for floating Point values, as
470 * NaN != NaN is defined, so we must check this here.
473 !mode_is_float(get_irn_mode(aa)) || proj_nr == pn_Cmp_Lt || proj_nr == pn_Cmp_Gt)
476 /* This is a trick with the bits used for encoding the Cmp
477 Proj numbers, the following statement is not the same:
478 return new_tarval_from_long (proj_nr == pn_Cmp_Eq, mode_b) */
479 return new_tarval_from_long (proj_nr & pn_Cmp_Eq, mode_b);
482 tarval *taa = value_of(aa);
483 tarval *tab = value_of(ab);
484 ir_mode *mode = get_irn_mode(aa);
487 * The predecessors of Cmp are target values. We can evaluate
490 if ((taa != tarval_bad) && (tab != tarval_bad)) {
491 /* strange checks... */
492 pn_Cmp flags = tarval_cmp(taa, tab);
493 if (flags != pn_Cmp_False) {
494 return new_tarval_from_long (proj_nr & flags, mode_b);
497 /* for integer values, we can check against MIN/MAX */
498 else if (mode_is_int(mode)) {
499 /* MIN <=/> x. This results in true/false. */
500 if (taa == get_mode_min(mode)) {
501 /* a compare with the MIN value */
502 if (proj_nr == pn_Cmp_Le)
503 return get_tarval_b_true();
504 else if (proj_nr == pn_Cmp_Gt)
505 return get_tarval_b_false();
507 /* x >=/< MIN. This results in true/false. */
509 if (tab == get_mode_min(mode)) {
510 /* a compare with the MIN value */
511 if (proj_nr == pn_Cmp_Ge)
512 return get_tarval_b_true();
513 else if (proj_nr == pn_Cmp_Lt)
514 return get_tarval_b_false();
516 /* MAX >=/< x. This results in true/false. */
517 else if (taa == get_mode_max(mode)) {
518 if (proj_nr == pn_Cmp_Ge)
519 return get_tarval_b_true();
520 else if (proj_nr == pn_Cmp_Lt)
521 return get_tarval_b_false();
523 /* x <=/> MAX. This results in true/false. */
524 else if (tab == get_mode_max(mode)) {
525 if (proj_nr == pn_Cmp_Le)
526 return get_tarval_b_true();
527 else if (proj_nr == pn_Cmp_Gt)
528 return get_tarval_b_false();
532 * The predecessors are Allocs or (void*)(0) constants. Allocs never
533 * return NULL, they raise an exception. Therefore we can predict
537 ir_node *aaa = skip_Id(skip_Proj(aa));
538 ir_node *aba = skip_Id(skip_Proj(ab));
540 if ( ( (/* aa is ProjP and aaa is Alloc */
541 (get_irn_op(aa) == op_Proj)
542 && (mode_is_reference(get_irn_mode(aa)))
543 && (get_irn_op(aaa) == op_Alloc))
544 && ( (/* ab is NULL */
545 (get_irn_op(ab) == op_Const)
546 && (mode_is_reference(get_irn_mode(ab)))
547 && (get_Const_tarval(ab) == get_mode_null(get_irn_mode(ab))))
548 || (/* ab is other Alloc */
549 (get_irn_op(ab) == op_Proj)
550 && (mode_is_reference(get_irn_mode(ab)))
551 && (get_irn_op(aba) == op_Alloc)
553 || (/* aa is NULL and aba is Alloc */
554 (get_irn_op(aa) == op_Const)
555 && (mode_is_reference(get_irn_mode(aa)))
556 && (get_Const_tarval(aa) == get_mode_null(get_irn_mode(aa)))
557 && (get_irn_op(ab) == op_Proj)
558 && (mode_is_reference(get_irn_mode(ab)))
559 && (get_irn_op(aba) == op_Alloc)))
561 return new_tarval_from_long(proj_nr & pn_Cmp_Ne, mode_b);
564 return computed_value_Cmp_Confirm(a, aa, ab, proj_nr);
565 } /* computed_value_Proj_Cmp */
568 * Return the value of a Proj, handle Proj(Cmp), Proj(Div), Proj(Mod),
569 * Proj(DivMod) and Proj(Quot).
571 static tarval *computed_value_Proj(ir_node *n) {
572 ir_node *a = get_Proj_pred(n);
575 switch (get_irn_opcode(a)) {
577 return computed_value_Proj_Cmp(n);
580 /* compute either the Div or the Mod part */
581 proj_nr = get_Proj_proj(n);
582 if (proj_nr == pn_DivMod_res_div)
583 return do_computed_value_Div(get_DivMod_left(a), get_DivMod_right(a));
584 else if (proj_nr == pn_DivMod_res_mod)
585 return do_computed_value_Mod(get_DivMod_left(a), get_DivMod_right(a));
589 if (get_Proj_proj(n) == pn_Div_res)
590 return computed_value(a);
594 if (get_Proj_proj(n) == pn_Mod_res)
595 return computed_value(a);
599 if (get_Proj_proj(n) == pn_Quot_res)
600 return computed_value(a);
607 } /* computed_value_Proj */
610 * Calculate the value of a Mux: can be evaluated, if the
611 * sel and the right input are known.
613 static tarval *computed_value_Mux(ir_node *n) {
614 ir_node *sel = get_Mux_sel(n);
615 tarval *ts = value_of(sel);
617 if (ts == get_tarval_b_true()) {
618 ir_node *v = get_Mux_true(n);
621 else if (ts == get_tarval_b_false()) {
622 ir_node *v = get_Mux_false(n);
626 } /* computed_value_Mux */
629 * Calculate the value of a Psi: can be evaluated, if a condition is true
630 * and all previous conditions are false. If all conditions are false
631 * we evaluate to the default one.
633 static tarval *computed_value_Psi(ir_node *n) {
635 return computed_value_Mux(n);
637 } /* computed_value_Psi */
640 * Calculate the value of a Confirm: can be evaluated,
641 * if it has the form Confirm(x, '=', Const).
643 static tarval *computed_value_Confirm(ir_node *n) {
644 return get_Confirm_cmp(n) == pn_Cmp_Eq ?
645 value_of(get_Confirm_bound(n)) : tarval_bad;
646 } /* computed_value_Confirm */
649 * If the parameter n can be computed, return its value, else tarval_bad.
650 * Performs constant folding.
652 * @param n The node this should be evaluated
654 tarval *computed_value(ir_node *n) {
655 if (n->op->ops.computed_value)
656 return n->op->ops.computed_value(n);
658 } /* computed_value */
661 * Set the default computed_value evaluator in an ir_op_ops.
663 * @param code the opcode for the default operation
664 * @param ops the operations initialized
669 static ir_op_ops *firm_set_default_computed_value(ir_opcode code, ir_op_ops *ops)
673 ops->computed_value = computed_value_##a; \
708 } /* firm_set_default_computed_value */
711 * Returns a equivalent block for another block.
712 * If the block has only one predecessor, this is
713 * the equivalent one. If the only predecessor of a block is
714 * the block itself, this is a dead block.
716 * If both predecessors of a block are the branches of a binary
717 * Cond, the equivalent block is Cond's block.
719 * If all predecessors of a block are bad or lies in a dead
720 * block, the current block is dead as well.
722 * Note, that blocks are NEVER turned into Bad's, instead
723 * the dead_block flag is set. So, never test for is_Bad(block),
724 * always use is_dead_Block(block).
726 static ir_node *equivalent_node_Block(ir_node *n)
729 int n_preds = get_Block_n_cfgpreds(n);
731 /* The Block constructor does not call optimize, but mature_immBlock
732 calls the optimization. */
733 assert(get_Block_matured(n));
735 /* Straightening: a single entry Block following a single exit Block
736 can be merged, if it is not the Start block. */
737 /* !!! Beware, all Phi-nodes of n must have been optimized away.
738 This should be true, as the block is matured before optimize is called.
739 But what about Phi-cycles with the Phi0/Id that could not be resolved?
740 Remaining Phi nodes are just Ids. */
741 if ((n_preds == 1) && (get_irn_op(get_Block_cfgpred(n, 0)) == op_Jmp)) {
742 ir_node *predblock = get_nodes_block(get_Block_cfgpred(n, 0));
743 if (predblock == oldn) {
744 /* Jmp jumps into the block it is in -- deal self cycle. */
745 n = set_Block_dead(n);
746 DBG_OPT_DEAD_BLOCK(oldn, n);
747 } else if (get_opt_control_flow_straightening()) {
749 DBG_OPT_STG(oldn, n);
751 } else if ((n_preds == 1) &&
752 (get_irn_op(skip_Proj(get_Block_cfgpred(n, 0))) == op_Cond)) {
753 ir_node *predblock = get_Block_cfgpred_block(n, 0);
754 if (predblock == oldn) {
755 /* Jmp jumps into the block it is in -- deal self cycle. */
756 n = set_Block_dead(n);
757 DBG_OPT_DEAD_BLOCK(oldn, n);
759 } else if ((n_preds == 2) &&
760 (get_opt_control_flow_weak_simplification())) {
761 /* Test whether Cond jumps twice to this block
762 * The more general case which more than 2 predecessors is handles
763 * in optimize_cf(), we handle only this special case for speed here.
765 ir_node *a = get_Block_cfgpred(n, 0);
766 ir_node *b = get_Block_cfgpred(n, 1);
768 if ((get_irn_op(a) == op_Proj) &&
769 (get_irn_op(b) == op_Proj) &&
770 (get_Proj_pred(a) == get_Proj_pred(b)) &&
771 (get_irn_op(get_Proj_pred(a)) == op_Cond) &&
772 (get_irn_mode(get_Cond_selector(get_Proj_pred(a))) == mode_b)) {
773 /* Also a single entry Block following a single exit Block. Phis have
774 twice the same operand and will be optimized away. */
775 n = get_nodes_block(get_Proj_pred(a));
776 DBG_OPT_IFSIM1(oldn, a, b, n);
778 } else if (get_opt_unreachable_code() &&
779 (n != get_irg_start_block(current_ir_graph)) &&
780 (n != get_irg_end_block(current_ir_graph)) ) {
783 /* If all inputs are dead, this block is dead too, except if it is
784 the start or end block. This is one step of unreachable code
786 for (i = get_Block_n_cfgpreds(n) - 1; i >= 0; --i) {
787 ir_node *pred = get_Block_cfgpred(n, i);
790 if (is_Bad(pred)) continue;
791 pred_blk = get_nodes_block(skip_Proj(pred));
793 if (is_Block_dead(pred_blk)) continue;
796 /* really found a living input */
801 n = set_Block_dead(n);
802 DBG_OPT_DEAD_BLOCK(oldn, n);
807 } /* equivalent_node_Block */
810 * Returns a equivalent node for a Jmp, a Bad :-)
811 * Of course this only happens if the Block of the Jmp is dead.
813 static ir_node *equivalent_node_Jmp(ir_node *n) {
814 /* unreachable code elimination */
815 if (is_Block_dead(get_nodes_block(n)))
819 } /* equivalent_node_Jmp */
821 /** Raise is handled in the same way as Jmp. */
822 #define equivalent_node_Raise equivalent_node_Jmp
825 /* We do not evaluate Cond here as we replace it by a new node, a Jmp.
826 See transform_node_Proj_Cond(). */
829 * Optimize operations that are commutative and have neutral 0,
830 * so a op 0 = 0 op a = a.
832 static ir_node *equivalent_node_neutral_zero(ir_node *n)
836 ir_node *a = get_binop_left(n);
837 ir_node *b = get_binop_right(n);
842 /* After running compute_node there is only one constant predecessor.
843 Find this predecessors value and remember the other node: */
844 if ((tv = value_of(a)) != tarval_bad) {
846 } else if ((tv = value_of(b)) != tarval_bad) {
851 /* If this predecessors constant value is zero, the operation is
852 * unnecessary. Remove it.
854 * Beware: If n is a Add, the mode of on and n might be different
855 * which happens in this rare construction: NULL + 3.
856 * Then, a Conv would be needed which we cannot include here.
858 if (classify_tarval (tv) == TV_CLASSIFY_NULL) {
859 if (get_irn_mode(on) == get_irn_mode(n)) {
862 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_0);
867 } /* equivalent_node_neutral_zero */
870 * Eor is commutative and has neutral 0.
872 #define equivalent_node_Eor equivalent_node_neutral_zero
875 * Optimize a - 0 and (a - x) + x (for modes with wrap-around).
877 * The second one looks strange, but this construct
878 * is used heavily in the LCC sources :-).
880 * Beware: The Mode of an Add may be different than the mode of its
881 * predecessors, so we could not return a predecessors in all cases.
883 static ir_node *equivalent_node_Add(ir_node *n) {
885 ir_node *left, *right;
886 ir_mode *mode = get_irn_mode(n);
888 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
889 if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
892 n = equivalent_node_neutral_zero(n);
896 left = get_Add_left(n);
897 right = get_Add_right(n);
899 if (get_irn_op(left) == op_Sub) {
900 if (get_Sub_right(left) == right) {
903 n = get_Sub_left(left);
904 if (mode == get_irn_mode(n)) {
905 DBG_OPT_ALGSIM1(oldn, left, right, n, FS_OPT_ADD_SUB);
910 if (get_irn_op(right) == op_Sub) {
911 if (get_Sub_right(right) == left) {
914 n = get_Sub_left(right);
915 if (mode == get_irn_mode(n)) {
916 DBG_OPT_ALGSIM1(oldn, left, right, n, FS_OPT_ADD_SUB);
922 } /* equivalent_node_Add */
925 * optimize operations that are not commutative but have neutral 0 on left,
928 static ir_node *equivalent_node_left_zero(ir_node *n) {
931 ir_node *a = get_binop_left(n);
932 ir_node *b = get_binop_right(n);
934 if (classify_tarval(value_of(b)) == TV_CLASSIFY_NULL) {
937 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_0);
940 } /* equivalent_node_left_zero */
942 #define equivalent_node_Shl equivalent_node_left_zero
943 #define equivalent_node_Shr equivalent_node_left_zero
944 #define equivalent_node_Shrs equivalent_node_left_zero
945 #define equivalent_node_Rot equivalent_node_left_zero
948 * Optimize a - 0 and (a + x) - x (for modes with wrap-around).
950 * The second one looks strange, but this construct
951 * is used heavily in the LCC sources :-).
953 * Beware: The Mode of a Sub may be different than the mode of its
954 * predecessors, so we could not return a predecessors in all cases.
956 static ir_node *equivalent_node_Sub(ir_node *n) {
959 ir_mode *mode = get_irn_mode(n);
961 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
962 if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
966 b = get_Sub_right(n);
968 /* Beware: modes might be different */
969 if (classify_tarval(value_of(b)) == TV_CLASSIFY_NULL) {
970 if (mode == get_irn_mode(a)) {
973 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_0);
975 } else if (get_irn_op(a) == op_Add) {
976 if (mode_wrap_around(mode)) {
977 ir_node *left = get_Add_left(a);
978 ir_node *right = get_Add_right(a);
981 if (mode == get_irn_mode(right)) {
983 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
985 } else if (right == b) {
986 if (mode == get_irn_mode(left)) {
988 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
994 } /* equivalent_node_Sub */
998 * Optimize an "idempotent unary op", ie op(op(n)) = n.
1001 * -(-a) == a, but might overflow two times.
1002 * We handle it anyway here but the better way would be a
1003 * flag. This would be needed for Pascal for instance.
1005 static ir_node *equivalent_node_idempotent_unop(ir_node *n) {
1007 ir_node *pred = get_unop_op(n);
1009 /* optimize symmetric unop */
1010 if (get_irn_op(pred) == get_irn_op(n)) {
1011 n = get_unop_op(pred);
1012 DBG_OPT_ALGSIM2(oldn, pred, n);
1015 } /* equivalent_node_idempotent_unop */
1017 /** Optimize Not(Not(x)) == x. */
1018 #define equivalent_node_Not equivalent_node_idempotent_unop
1020 /** --x == x ??? Is this possible or can --x raise an
1021 out of bounds exception if min =! max? */
1022 #define equivalent_node_Minus equivalent_node_idempotent_unop
1025 * Optimize a * 1 = 1 * a = a.
1027 static ir_node *equivalent_node_Mul(ir_node *n) {
1029 ir_node *a = get_Mul_left(n);
1030 ir_node *b = get_Mul_right(n);
1032 /* Mul is commutative and has again an other neutral element. */
1033 if (classify_tarval(value_of(a)) == TV_CLASSIFY_ONE) {
1035 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
1036 } else if (classify_tarval(value_of(b)) == TV_CLASSIFY_ONE) {
1038 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
1041 } /* equivalent_node_Mul */
1044 * Optimize a / 1 = a.
1046 static ir_node *equivalent_node_Div(ir_node *n) {
1047 ir_node *a = get_Div_left(n);
1048 ir_node *b = get_Div_right(n);
1050 /* Div is not commutative. */
1051 if (classify_tarval(value_of(b)) == TV_CLASSIFY_ONE) { /* div(x, 1) == x */
1052 /* Turn Div into a tuple (mem, bad, a) */
1053 ir_node *mem = get_Div_mem(n);
1054 turn_into_tuple(n, pn_Div_max);
1055 set_Tuple_pred(n, pn_Div_M, mem);
1056 set_Tuple_pred(n, pn_Div_X_except, new_Bad()); /* no exception */
1057 set_Tuple_pred(n, pn_Div_res, a);
1060 } /* equivalent_node_Div */
1063 * Optimize a / 1.0 = a.
1065 static ir_node *equivalent_node_Quot(ir_node *n) {
1066 ir_node *a = get_Quot_left(n);
1067 ir_node *b = get_Quot_right(n);
1069 /* Div is not commutative. */
1070 if (classify_tarval(value_of(b)) == TV_CLASSIFY_ONE) { /* Quot(x, 1) == x */
1071 /* Turn Quot into a tuple (mem, bad, a) */
1072 ir_node *mem = get_Quot_mem(n);
1073 turn_into_tuple(n, pn_Quot_max);
1074 set_Tuple_pred(n, pn_Quot_M, mem);
1075 set_Tuple_pred(n, pn_Quot_X_except, new_Bad()); /* no exception */
1076 set_Tuple_pred(n, pn_Quot_res, a);
1079 } /* equivalent_node_Quot */
1082 * Optimize a / 1 = a.
1084 static ir_node *equivalent_node_DivMod(ir_node *n) {
1085 ir_node *a = get_DivMod_left(n);
1086 ir_node *b = get_DivMod_right(n);
1088 /* Div is not commutative. */
1089 if (classify_tarval(value_of(b)) == TV_CLASSIFY_ONE) { /* div(x, 1) == x */
1090 /* Turn DivMod into a tuple (mem, bad, a, 0) */
1091 ir_node *mem = get_Div_mem(n);
1092 ir_mode *mode = get_irn_mode(b);
1094 turn_into_tuple(n, pn_DivMod_max);
1095 set_Tuple_pred(n, pn_DivMod_M, mem);
1096 set_Tuple_pred(n, pn_DivMod_X_except, new_Bad()); /* no exception */
1097 set_Tuple_pred(n, pn_DivMod_res_div, a);
1098 set_Tuple_pred(n, pn_DivMod_res_mod, new_Const(mode, get_mode_null(mode)));
1101 } /* equivalent_node_DivMod */
1104 * Use algebraic simplification a | a = a | 0 = 0 | a = a.
1106 static ir_node *equivalent_node_Or(ir_node *n) {
1109 ir_node *a = get_Or_left(n);
1110 ir_node *b = get_Or_right(n);
1113 n = a; /* Or has it's own neutral element */
1114 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_OR);
1115 } else if (classify_tarval(value_of(a)) == TV_CLASSIFY_NULL) {
1117 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_OR);
1118 } else if (classify_tarval(value_of(b)) == TV_CLASSIFY_NULL) {
1120 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_OR);
1124 } /* equivalent_node_Or */
1127 * Optimize a & 0b1...1 = 0b1...1 & a = a & a = a.
1129 static ir_node *equivalent_node_And(ir_node *n) {
1132 ir_node *a = get_And_left(n);
1133 ir_node *b = get_And_right(n);
1136 n = a; /* And has it's own neutral element */
1137 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_AND);
1138 } else if (classify_tarval(value_of(a)) == TV_CLASSIFY_ALL_ONE) {
1140 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1141 } else if (classify_tarval(value_of(b)) == TV_CLASSIFY_ALL_ONE) {
1143 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1146 } /* equivalent_node_And */
1149 * Try to remove useless Conv's:
1151 static ir_node *equivalent_node_Conv(ir_node *n) {
1153 ir_node *a = get_Conv_op(n);
1156 ir_mode *n_mode = get_irn_mode(n);
1157 ir_mode *a_mode = get_irn_mode(a);
1159 if (n_mode == a_mode) { /* No Conv necessary */
1160 /* leave strict floating point Conv's */
1161 if (get_Conv_strict(n))
1164 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CONV);
1165 } else if (get_irn_op(a) == op_Conv) { /* Conv(Conv(b)) */
1169 n_mode = get_irn_mode(n);
1170 b_mode = get_irn_mode(b);
1172 if (n_mode == b_mode) {
1173 if (n_mode == mode_b) {
1174 n = b; /* Convb(Conv*(xxxb(...))) == xxxb(...) */
1175 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
1176 } else if (mode_is_int(n_mode) || mode_is_character(n_mode)) {
1177 if (smaller_mode(b_mode, a_mode)){
1178 n = b; /* ConvS(ConvL(xxxS(...))) == xxxS(...) */
1179 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
1185 } /* equivalent_node_Conv */
1188 * A Cast may be removed if the type of the previous node
1189 * is already the type of the Cast.
1191 static ir_node *equivalent_node_Cast(ir_node *n) {
1193 ir_node *pred = get_Cast_op(n);
1195 if (get_irn_type(pred) == get_Cast_type(n)) {
1197 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CAST);
1200 } /* equivalent_node_Cast */
1203 * Several optimizations:
1204 * - no Phi in start block.
1205 * - remove Id operators that are inputs to Phi
1206 * - fold Phi-nodes, iff they have only one predecessor except
1209 static ir_node *equivalent_node_Phi(ir_node *n) {
1213 ir_node *block = NULL; /* to shutup gcc */
1214 ir_node *first_val = NULL; /* to shutup gcc */
1216 if (!get_opt_normalize()) return n;
1218 n_preds = get_Phi_n_preds(n);
1220 block = get_nodes_block(n);
1221 /* @@@ fliegt 'raus, sollte aber doch immer wahr sein!!!
1222 assert(get_irn_arity(block) == n_preds && "phi in wrong block!"); */
1223 if ((is_Block_dead(block)) || /* Control dead */
1224 (block == get_irg_start_block(current_ir_graph))) /* There should be no Phi nodes */
1225 return new_Bad(); /* in the Start Block. */
1227 if (n_preds == 0) return n; /* Phi of dead Region without predecessors. */
1229 /* If the Block has a Bad pred, we also have one. */
1230 for (i = 0; i < n_preds; ++i)
1231 if (is_Bad(get_Block_cfgpred(block, i)))
1232 set_Phi_pred(n, i, new_Bad());
1234 /* Find first non-self-referencing input */
1235 for (i = 0; i < n_preds; ++i) {
1236 first_val = get_Phi_pred(n, i);
1237 if ( (first_val != n) /* not self pointer */
1239 && (! is_Bad(first_val))
1241 ) { /* value not dead */
1242 break; /* then found first value. */
1247 /* A totally Bad or self-referencing Phi (we didn't break the above loop) */
1251 /* search for rest of inputs, determine if any of these
1252 are non-self-referencing */
1253 while (++i < n_preds) {
1254 ir_node *scnd_val = get_Phi_pred(n, i);
1255 if ( (scnd_val != n)
1256 && (scnd_val != first_val)
1258 && (! is_Bad(scnd_val))
1266 /* Fold, if no multiple distinct non-self-referencing inputs */
1268 DBG_OPT_PHI(oldn, n);
1271 } /* equivalent_node_Phi */
1274 * Several optimizations:
1275 * - no Sync in start block.
1276 * - fold Sync-nodes, iff they have only one predecessor except
1279 static ir_node *equivalent_node_Sync(ir_node *n) {
1283 ir_node *first_val = NULL; /* to shutup gcc */
1285 if (!get_opt_normalize()) return n;
1287 n_preds = get_Sync_n_preds(n);
1289 /* Find first non-self-referencing input */
1290 for (i = 0; i < n_preds; ++i) {
1291 first_val = get_Sync_pred(n, i);
1292 if ((first_val != n) /* not self pointer */ &&
1293 (! is_Bad(first_val))
1294 ) { /* value not dead */
1295 break; /* then found first value. */
1300 /* A totally Bad or self-referencing Sync (we didn't break the above loop) */
1303 /* search the rest of inputs, determine if any of these
1304 are non-self-referencing */
1305 while (++i < n_preds) {
1306 ir_node *scnd_val = get_Sync_pred(n, i);
1307 if ((scnd_val != n) &&
1308 (scnd_val != first_val) &&
1309 (! is_Bad(scnd_val))
1315 /* Fold, if no multiple distinct non-self-referencing inputs */
1317 DBG_OPT_SYNC(oldn, n);
1320 } /* equivalent_node_Sync */
1323 * Optimize Proj(Tuple) and gigo() for ProjX in Bad block,
1324 * ProjX(Load) and ProjX(Store).
1326 static ir_node *equivalent_node_Proj(ir_node *n) {
1328 ir_node *a = get_Proj_pred(n);
1330 if ( get_irn_op(a) == op_Tuple) {
1331 /* Remove the Tuple/Proj combination. */
1332 if ( get_Proj_proj(n) <= get_Tuple_n_preds(a) ) {
1333 n = get_Tuple_pred(a, get_Proj_proj(n));
1334 DBG_OPT_TUPLE(oldn, a, n);
1336 assert(0); /* This should not happen! */
1339 } else if (get_irn_mode(n) == mode_X) {
1340 if (is_Block_dead(get_nodes_block(skip_Proj(n)))) {
1341 /* Remove dead control flow -- early gigo(). */
1343 } else if (get_opt_ldst_only_null_ptr_exceptions()) {
1344 ir_op *op = get_irn_op(a);
1346 if (op == op_Load || op == op_Store) {
1347 /* get the load/store address */
1348 ir_node *addr = get_irn_n(a, 1);
1351 if (value_not_null(addr, &confirm)) {
1352 if (confirm == NULL) {
1353 /* this node may float if it did not depend on a Confirm */
1354 set_irn_pinned(a, op_pin_state_floats);
1364 } /* equivalent_node_Proj */
1369 static ir_node *equivalent_node_Id(ir_node *n) {
1374 } while (get_irn_op(n) == op_Id);
1376 DBG_OPT_ID(oldn, n);
1378 } /* equivalent_node_Id */
1383 static ir_node *equivalent_node_Mux(ir_node *n)
1385 ir_node *oldn = n, *sel = get_Mux_sel(n);
1386 tarval *ts = value_of(sel);
1388 /* Mux(true, f, t) == t */
1389 if (ts == tarval_b_true) {
1390 n = get_Mux_true(n);
1391 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_C);
1393 /* Mux(false, f, t) == f */
1394 else if (ts == tarval_b_false) {
1395 n = get_Mux_false(n);
1396 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_C);
1398 /* Mux(v, x, x) == x */
1399 else if (get_Mux_false(n) == get_Mux_true(n)) {
1400 n = get_Mux_true(n);
1401 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_EQ);
1403 else if (get_irn_op(sel) == op_Proj && !mode_honor_signed_zeros(get_irn_mode(n))) {
1404 ir_node *cmp = get_Proj_pred(sel);
1405 long proj_nr = get_Proj_proj(sel);
1406 ir_node *b = get_Mux_false(n);
1407 ir_node *a = get_Mux_true(n);
1410 * Note: normalization puts the constant on the right site,
1411 * so we check only one case.
1413 * Note further that these optimization work even for floating point
1414 * with NaN's because -NaN == NaN.
1415 * However, if +0 and -0 is handled differently, we cannot use the first one.
1417 if (get_irn_op(cmp) == op_Cmp && get_Cmp_left(cmp) == a) {
1418 if (classify_Const(get_Cmp_right(cmp)) == CNST_NULL) {
1419 /* Mux(a CMP 0, X, a) */
1420 if (get_irn_op(b) == op_Minus && get_Minus_op(b) == a) {
1421 /* Mux(a CMP 0, -a, a) */
1422 if (proj_nr == pn_Cmp_Eq) {
1423 /* Mux(a == 0, -a, a) ==> -a */
1425 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1426 } else if (proj_nr == pn_Cmp_Lg || proj_nr == pn_Cmp_Ne) {
1427 /* Mux(a != 0, -a, a) ==> a */
1429 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1431 } else if (classify_Const(b) == CNST_NULL) {
1432 /* Mux(a CMP 0, 0, a) */
1433 if (proj_nr == pn_Cmp_Lg || proj_nr == pn_Cmp_Ne) {
1434 /* Mux(a != 0, 0, a) ==> a */
1436 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1437 } else if (proj_nr == pn_Cmp_Eq) {
1438 /* Mux(a == 0, 0, a) ==> 0 */
1440 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1447 } /* equivalent_node_Mux */
1450 * Returns a equivalent node of a Psi: if a condition is true
1451 * and all previous conditions are false we know its value.
1452 * If all conditions are false its value is the default one.
1454 static ir_node *equivalent_node_Psi(ir_node *n) {
1456 return equivalent_node_Mux(n);
1458 } /* equivalent_node_Psi */
1461 * Optimize -a CMP -b into b CMP a.
1462 * This works only for for modes where unary Minus
1464 * Note that two-complement integers can Overflow
1465 * so it will NOT work.
1467 * For == and != can be handled in Proj(Cmp)
1469 static ir_node *equivalent_node_Cmp(ir_node *n) {
1470 ir_node *left = get_Cmp_left(n);
1471 ir_node *right = get_Cmp_right(n);
1473 if (get_irn_op(left) == op_Minus && get_irn_op(right) == op_Minus &&
1474 !mode_overflow_on_unary_Minus(get_irn_mode(left))) {
1475 left = get_Minus_op(left);
1476 right = get_Minus_op(right);
1477 set_Cmp_left(n, right);
1478 set_Cmp_right(n, left);
1481 } /* equivalent_node_Cmp */
1484 * Remove Confirm nodes if setting is on.
1485 * Replace Confirms(x, '=', Constlike) by Constlike.
1487 static ir_node *equivalent_node_Confirm(ir_node *n) {
1488 ir_node *pred = get_Confirm_value(n);
1489 pn_Cmp pnc = get_Confirm_cmp(n);
1491 if (get_irn_op(pred) == op_Confirm && pnc == get_Confirm_cmp(pred)) {
1493 * rare case: two identical Confirms one after another,
1494 * replace the second one with the first.
1498 if (pnc == pn_Cmp_Eq) {
1499 ir_node *bound = get_Confirm_bound(n);
1502 * Optimize a rare case:
1503 * Confirm(x, '=', Constlike) ==> Constlike
1505 if (is_irn_constlike(bound)) {
1506 DBG_OPT_CONFIRM(n, bound);
1510 return get_opt_remove_confirm() ? get_Confirm_value(n) : n;
1514 * Optimize CopyB(mem, x, x) into a Nop.
1516 static ir_node *equivalent_node_CopyB(ir_node *n) {
1517 ir_node *a = get_CopyB_dst(n);
1518 ir_node *b = get_CopyB_src(n);
1521 /* Turn CopyB into a tuple (mem, bad, bad) */
1522 ir_node *mem = get_CopyB_mem(n);
1523 turn_into_tuple(n, pn_CopyB_max);
1524 set_Tuple_pred(n, pn_CopyB_M, mem);
1525 set_Tuple_pred(n, pn_CopyB_X_except, new_Bad()); /* no exception */
1526 set_Tuple_pred(n, pn_CopyB_M_except, new_Bad());
1529 } /* equivalent_node_CopyB */
1532 * Optimize Bounds(idx, idx, upper) into idx.
1534 static ir_node *equivalent_node_Bound(ir_node *n) {
1535 ir_node *idx = get_Bound_index(n);
1536 ir_node *lower = get_Bound_lower(n);
1539 /* By definition lower < upper, so if idx == lower -->
1540 lower <= idx && idx < upper */
1542 /* Turn Bound into a tuple (mem, bad, idx) */
1545 ir_node *pred = skip_Proj(idx);
1547 if (get_irn_op(pred) == op_Bound) {
1549 * idx was Bounds_check previously, it is still valid if
1550 * lower <= pred_lower && pred_upper <= upper.
1552 ir_node *upper = get_Bound_upper(n);
1553 if (get_Bound_lower(pred) == lower &&
1554 get_Bound_upper(pred) == upper) {
1556 * One could expect that we simply return the previous
1557 * Bound here. However, this would be wrong, as we could
1558 * add an exception Proj to a new location than.
1559 * So, we must turn in into a tuple
1566 /* Turn Bound into a tuple (mem, bad, idx) */
1567 ir_node *mem = get_Bound_mem(n);
1568 turn_into_tuple(n, pn_Bound_max);
1569 set_Tuple_pred(n, pn_Bound_M, mem);
1570 set_Tuple_pred(n, pn_Bound_X_except, new_Bad()); /* no exception */
1571 set_Tuple_pred(n, pn_Bound_res, idx);
1574 } /* equivalent_node_Bound */
1577 * equivalent_node() returns a node equivalent to input n. It skips all nodes that
1578 * perform no actual computation, as, e.g., the Id nodes. It does not create
1579 * new nodes. It is therefore safe to free n if the node returned is not n.
1580 * If a node returns a Tuple we can not just skip it. If the size of the
1581 * in array fits, we transform n into a tuple (e.g., Div).
1583 ir_node *equivalent_node(ir_node *n) {
1584 if (n->op->ops.equivalent_node)
1585 return n->op->ops.equivalent_node(n);
1587 } /* equivalent_node */
1590 * Sets the default equivalent node operation for an ir_op_ops.
1592 * @param code the opcode for the default operation
1593 * @param ops the operations initialized
1598 static ir_op_ops *firm_set_default_equivalent_node(ir_opcode code, ir_op_ops *ops)
1602 ops->equivalent_node = equivalent_node_##a; \
1642 } /* firm_set_default_equivalent_node */
1645 * Do node specific optimizations of nodes predecessors.
1647 static void optimize_preds(ir_node *n) {
1648 switch (get_irn_opcode(n)) {
1650 case iro_Cmp: { /* We don't want Cast as input to Cmp. */
1651 ir_node *a = get_Cmp_left(n), *b = get_Cmp_right(n);
1653 if (get_irn_op(a) == op_Cast) {
1657 if (get_irn_op(b) == op_Cast) {
1659 set_Cmp_right(n, b);
1666 } /* optimize_preds */
1669 * Returns non-zero if a node is a Phi node
1670 * with all predecessors constant.
1672 static int is_const_Phi(ir_node *n) {
1677 for (i = get_irn_arity(n) - 1; i >= 0; --i)
1678 if (! is_Const(get_irn_n(n, i)))
1681 } /* is_const_Phi */
1684 * Apply an evaluator on a binop with a constant operators (and one Phi).
1686 * @param phi the Phi node
1687 * @param other the other operand
1688 * @param eval an evaluator function
1689 * @param left if non-zero, other is the left operand, else the right
1691 * @return a new Phi node if the conversion was successful, NULL else
1693 static ir_node *apply_binop_on_phi(ir_node *phi, tarval *other, tarval *(*eval)(tarval *, tarval *), int left) {
1699 int i, n = get_irn_arity(phi);
1701 NEW_ARR_A(void *, res, n);
1703 for (i = 0; i < n; ++i) {
1704 pred = get_irn_n(phi, i);
1705 tv = get_Const_tarval(pred);
1706 tv = eval(other, tv);
1708 if (tv == tarval_bad) {
1709 /* folding failed, bad */
1715 for (i = 0; i < n; ++i) {
1716 pred = get_irn_n(phi, i);
1717 tv = get_Const_tarval(pred);
1718 tv = eval(tv, other);
1720 if (tv == tarval_bad) {
1721 /* folding failed, bad */
1727 mode = get_irn_mode(phi);
1728 irg = current_ir_graph;
1729 for (i = 0; i < n; ++i) {
1730 pred = get_irn_n(phi, i);
1731 res[i] = new_r_Const_type(irg, get_irg_start_block(irg),
1732 mode, res[i], get_Const_type(pred));
1734 return new_r_Phi(irg, get_nodes_block(phi), n, (ir_node **)res, mode);
1735 } /* apply_binop_on_phi */
1738 * Apply an evaluator on a unop with a constant operator (a Phi).
1740 * @param phi the Phi node
1741 * @param eval an evaluator function
1743 * @return a new Phi node if the conversion was successful, NULL else
1745 static ir_node *apply_unop_on_phi(ir_node *phi, tarval *(*eval)(tarval *)) {
1751 int i, n = get_irn_arity(phi);
1753 NEW_ARR_A(void *, res, n);
1754 for (i = 0; i < n; ++i) {
1755 pred = get_irn_n(phi, i);
1756 tv = get_Const_tarval(pred);
1759 if (tv == tarval_bad) {
1760 /* folding failed, bad */
1765 mode = get_irn_mode(phi);
1766 irg = current_ir_graph;
1767 for (i = 0; i < n; ++i) {
1768 pred = get_irn_n(phi, i);
1769 res[i] = new_r_Const_type(irg, get_irg_start_block(irg),
1770 mode, res[i], get_Const_type(pred));
1772 return new_r_Phi(irg, get_nodes_block(phi), n, (ir_node **)res, mode);
1773 } /* apply_unop_on_phi */
1776 * Transform AddP(P, ConvIs(Iu)), AddP(P, ConvIu(Is)) and
1777 * SubP(P, ConvIs(Iu)), SubP(P, ConvIu(Is)).
1778 * If possible, remove the Conv's.
1780 static ir_node *transform_node_AddSub(ir_node *n) {
1781 ir_mode *mode = get_irn_mode(n);
1783 if (mode_is_reference(mode)) {
1784 ir_node *left = get_binop_left(n);
1785 ir_node *right = get_binop_right(n);
1786 int ref_bits = get_mode_size_bits(mode);
1788 if (get_irn_op(left) == op_Conv) {
1789 ir_mode *mode = get_irn_mode(left);
1790 int bits = get_mode_size_bits(mode);
1792 if (ref_bits == bits &&
1793 mode_is_int(mode) &&
1794 get_mode_arithmetic(mode) == irma_twos_complement) {
1795 ir_node *pre = get_Conv_op(left);
1796 ir_mode *pre_mode = get_irn_mode(pre);
1798 if (mode_is_int(pre_mode) &&
1799 get_mode_size_bits(pre_mode) == bits &&
1800 get_mode_arithmetic(pre_mode) == irma_twos_complement) {
1801 /* ok, this conv just changes to sign, moreover the calculation
1802 * is done with same number of bits as our address mode, so
1803 * we can ignore the conv as address calculation can be viewed
1804 * as either signed or unsigned
1806 set_binop_left(n, pre);
1811 if (get_irn_op(right) == op_Conv) {
1812 ir_mode *mode = get_irn_mode(right);
1813 int bits = get_mode_size_bits(mode);
1815 if (ref_bits == bits &&
1816 mode_is_int(mode) &&
1817 get_mode_arithmetic(mode) == irma_twos_complement) {
1818 ir_node *pre = get_Conv_op(right);
1819 ir_mode *pre_mode = get_irn_mode(pre);
1821 if (mode_is_int(pre_mode) &&
1822 get_mode_size_bits(pre_mode) == bits &&
1823 get_mode_arithmetic(pre_mode) == irma_twos_complement) {
1824 /* ok, this conv just changes to sign, moreover the calculation
1825 * is done with same number of bits as our address mode, so
1826 * we can ignore the conv as address calculation can be viewed
1827 * as either signed or unsigned
1829 set_binop_right(n, pre);
1835 } /* transform_node_AddSub */
1837 #define HANDLE_BINOP_PHI(op,a,b,c) \
1839 if (is_Const(b) && is_const_Phi(a)) { \
1840 /* check for Op(Phi, Const) */ \
1841 c = apply_binop_on_phi(a, get_Const_tarval(b), op, 0); \
1843 else if (is_Const(a) && is_const_Phi(b)) { \
1844 /* check for Op(Const, Phi) */ \
1845 c = apply_binop_on_phi(b, get_Const_tarval(a), op, 1); \
1848 DBG_OPT_ALGSIM0(oldn, c, FS_OPT_CONST_PHI); \
1852 #define HANDLE_UNOP_PHI(op,a,c) \
1854 if (is_const_Phi(a)) { \
1855 /* check for Op(Phi) */ \
1856 c = apply_unop_on_phi(a, op); \
1859 DBG_OPT_ALGSIM0(oldn, c, FS_OPT_CONST_PHI); \
1865 * Do the AddSub optimization, then Transform
1866 * Constant folding on Phi
1867 * Add(a,a) -> Mul(a, 2)
1868 * Add(Mul(a, x), a) -> Mul(a, x+1)
1869 * if the mode is integer or float.
1870 * Transform Add(a,-b) into Sub(a,b).
1871 * Reassociation might fold this further.
1873 static ir_node *transform_node_Add(ir_node *n) {
1875 ir_node *a, *b, *c, *oldn = n;
1877 n = transform_node_AddSub(n);
1879 a = get_Add_left(n);
1880 b = get_Add_right(n);
1882 HANDLE_BINOP_PHI(tarval_add, a,b,c);
1884 mode = get_irn_mode(n);
1886 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
1887 if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
1890 if (mode_is_num(mode)) {
1892 ir_node *block = get_irn_n(n, -1);
1895 get_irn_dbg_info(n),
1899 new_r_Const_long(current_ir_graph, block, mode, 2),
1901 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_A);
1902 } else if (get_irn_op(a) == op_Minus) {
1904 get_irn_dbg_info(n),
1910 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_MINUS_B);
1911 } else if (get_irn_op(b) == op_Minus) {
1913 get_irn_dbg_info(n),
1919 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_MINUS_B);
1921 /* do NOT execute this code if reassociation is enabled, it does the inverse! */
1922 else if (!get_opt_reassociation() && get_irn_op(a) == op_Mul) {
1923 ir_node *ma = get_Mul_left(a);
1924 ir_node *mb = get_Mul_right(a);
1927 ir_node *blk = get_irn_n(n, -1);
1929 get_irn_dbg_info(n), current_ir_graph, blk,
1932 get_irn_dbg_info(n), current_ir_graph, blk,
1934 new_r_Const_long(current_ir_graph, blk, mode, 1),
1937 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_MUL_A_X_A);
1938 } else if (b == mb) {
1939 ir_node *blk = get_irn_n(n, -1);
1941 get_irn_dbg_info(n), current_ir_graph, blk,
1944 get_irn_dbg_info(n), current_ir_graph, blk,
1946 new_r_Const_long(current_ir_graph, blk, mode, 1),
1949 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_MUL_A_X_A);
1952 /* do NOT execute this code if reassociation is enabled, it does the inverse! */
1953 else if (!get_opt_reassociation() && get_irn_op(b) == op_Mul) {
1954 ir_node *ma = get_Mul_left(b);
1955 ir_node *mb = get_Mul_right(b);
1958 ir_node *blk = get_irn_n(n, -1);
1960 get_irn_dbg_info(n), current_ir_graph, blk,
1963 get_irn_dbg_info(n), current_ir_graph, blk,
1965 new_r_Const_long(current_ir_graph, blk, mode, 1),
1968 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_MUL_A_X_A);
1969 } else if (a == mb) {
1970 ir_node *blk = get_irn_n(n, -1);
1972 get_irn_dbg_info(n), current_ir_graph, blk,
1975 get_irn_dbg_info(n), current_ir_graph, blk,
1977 new_r_Const_long(current_ir_graph, blk, mode, 1),
1980 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_MUL_A_X_A);
1985 } /* transform_node_Add */
1988 * Do the AddSub optimization, then Transform
1989 * Constant folding on Phi
1990 * Sub(0,a) -> Minus(a)
1991 * Sub(Mul(a, x), a) -> Mul(a, x-1)
1992 * Sub(Sub(x, y), b) -> Sub(x, Add(y,b))
1994 static ir_node *transform_node_Sub(ir_node *n) {
1999 n = transform_node_AddSub(n);
2001 a = get_Sub_left(n);
2002 b = get_Sub_right(n);
2004 HANDLE_BINOP_PHI(tarval_sub, a,b,c);
2006 mode = get_irn_mode(n);
2008 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
2009 if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
2012 /* Beware of Sub(P, P) which cannot be optimized into a simple Minus ... */
2013 if (mode_is_num(mode) && mode == get_irn_mode(a) && (classify_Const(a) == CNST_NULL)) {
2015 get_irn_dbg_info(n),
2020 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_0_A);
2022 /* do NOT execute this code if reassociation is enabled, it does the inverse! */
2023 else if (get_opt_reassociation() && get_irn_op(a) == op_Mul) {
2024 ir_node *ma = get_Mul_left(a);
2025 ir_node *mb = get_Mul_right(a);
2028 ir_node *blk = get_irn_n(n, -1);
2030 get_irn_dbg_info(n),
2031 current_ir_graph, blk,
2034 get_irn_dbg_info(n),
2035 current_ir_graph, blk,
2037 new_r_Const_long(current_ir_graph, blk, mode, 1),
2040 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MUL_A_X_A);
2041 } else if (mb == b) {
2042 ir_node *blk = get_irn_n(n, -1);
2044 get_irn_dbg_info(n),
2045 current_ir_graph, blk,
2048 get_irn_dbg_info(n),
2049 current_ir_graph, blk,
2051 new_r_Const_long(current_ir_graph, blk, mode, 1),
2054 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MUL_A_X_A);
2056 } else if (get_irn_op(a) == op_Sub) {
2057 ir_node *x = get_Sub_left(a);
2058 ir_node *y = get_Sub_right(a);
2059 ir_node *blk = get_irn_n(n, -1);
2060 ir_mode *m_b = get_irn_mode(b);
2061 ir_mode *m_y = get_irn_mode(y);
2064 /* Determine the right mode for the Add. */
2067 else if (mode_is_reference(m_b))
2069 else if (mode_is_reference(m_y))
2073 * Both modes are different but none is reference,
2074 * happens for instance in SubP(SubP(P, Iu), Is).
2075 * We have two possibilities here: Cast or ignore.
2076 * Currently we ignore this case.
2081 add = new_r_Add(current_ir_graph, blk, y, b, mode);
2084 set_Sub_right(n, add);
2085 DBG_OPT_ALGSIM0(n, n, FS_OPT_SUB_SUB_X_Y_Z);
2089 } /* transform_node_Sub */
2092 * Transform Mul(a,-1) into -a.
2093 * Do constant evaluation of Phi nodes.
2094 * Do architecture dependent optimizations on Mul nodes
2096 static ir_node *transform_node_Mul(ir_node *n) {
2097 ir_node *c, *oldn = n;
2098 ir_node *a = get_Mul_left(n);
2099 ir_node *b = get_Mul_right(n);
2102 HANDLE_BINOP_PHI(tarval_mul, a,b,c);
2104 mode = get_irn_mode(n);
2105 if (mode_is_signed(mode)) {
2108 if (value_of(a) == get_mode_minus_one(mode))
2110 else if (value_of(b) == get_mode_minus_one(mode))
2113 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1), r, mode);
2114 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2118 return arch_dep_replace_mul_with_shifts(n);
2119 } /* transform_node_Mul */
2122 * Transform a Div Node.
2124 static ir_node *transform_node_Div(ir_node *n) {
2125 tarval *tv = value_of(n);
2128 /* BEWARE: it is NOT possible to optimize a/a to 1, as this may cause a exception */
2130 if (tv != tarval_bad) {
2131 value = new_Const(get_tarval_mode(tv), tv);
2133 DBG_OPT_CSTEVAL(n, value);
2134 } else /* Try architecture dependent optimization */
2135 value = arch_dep_replace_div_by_const(n);
2138 /* Turn Div into a tuple (mem, bad, value) */
2139 ir_node *mem = get_Div_mem(n);
2141 turn_into_tuple(n, pn_Div_max);
2142 set_Tuple_pred(n, pn_Div_M, mem);
2143 set_Tuple_pred(n, pn_Div_X_except, new_Bad());
2144 set_Tuple_pred(n, pn_Div_res, value);
2147 } /* transform_node_Div */
2150 * Transform a Mod node.
2152 static ir_node *transform_node_Mod(ir_node *n) {
2153 tarval *tv = value_of(n);
2156 /* BEWARE: it is NOT possible to optimize a%a to 0, as this may cause a exception */
2158 if (tv != tarval_bad) {
2159 value = new_Const(get_tarval_mode(tv), tv);
2161 DBG_OPT_CSTEVAL(n, value);
2162 } else /* Try architecture dependent optimization */
2163 value = arch_dep_replace_mod_by_const(n);
2166 /* Turn Mod into a tuple (mem, bad, value) */
2167 ir_node *mem = get_Mod_mem(n);
2169 turn_into_tuple(n, pn_Mod_max);
2170 set_Tuple_pred(n, pn_Mod_M, mem);
2171 set_Tuple_pred(n, pn_Mod_X_except, new_Bad());
2172 set_Tuple_pred(n, pn_Mod_res, value);
2175 } /* transform_node_Mod */
2178 * Transform a DivMod node.
2180 static ir_node *transform_node_DivMod(ir_node *n) {
2183 ir_node *a = get_DivMod_left(n);
2184 ir_node *b = get_DivMod_right(n);
2185 ir_mode *mode = get_irn_mode(a);
2186 tarval *ta = value_of(a);
2187 tarval *tb = value_of(b);
2189 if (!(mode_is_int(mode) && mode_is_int(get_irn_mode(b))))
2192 /* BEWARE: it is NOT possible to optimize a/a to 1, as this may cause a exception */
2194 if (tb != tarval_bad) {
2195 if (tb == get_mode_one(get_tarval_mode(tb))) {
2196 b = new_Const (mode, get_mode_null(mode));
2199 DBG_OPT_CSTEVAL(n, b);
2200 } else if (ta != tarval_bad) {
2201 tarval *resa, *resb;
2202 resa = tarval_div (ta, tb);
2203 if (resa == tarval_bad) return n; /* Causes exception!!! Model by replacing through
2204 Jmp for X result!? */
2205 resb = tarval_mod (ta, tb);
2206 if (resb == tarval_bad) return n; /* Causes exception! */
2207 a = new_Const (mode, resa);
2208 b = new_Const (mode, resb);
2211 DBG_OPT_CSTEVAL(n, a);
2212 DBG_OPT_CSTEVAL(n, b);
2213 } else { /* Try architecture dependent optimization */
2214 arch_dep_replace_divmod_by_const(&a, &b, n);
2215 evaluated = a != NULL;
2217 } else if (ta == get_mode_null(mode)) {
2218 /* 0 / non-Const = 0 */
2223 if (evaluated) { /* replace by tuple */
2224 ir_node *mem = get_DivMod_mem(n);
2225 turn_into_tuple(n, pn_DivMod_max);
2226 set_Tuple_pred(n, pn_DivMod_M, mem);
2227 set_Tuple_pred(n, pn_DivMod_X_except, new_Bad()); /* no exception */
2228 set_Tuple_pred(n, pn_DivMod_res_div, a);
2229 set_Tuple_pred(n, pn_DivMod_res_mod, b);
2233 } /* transform_node_DivMod */
2236 * Optimize Abs(x) into x if x is Confirmed >= 0
2237 * Optimize Abs(x) into -x if x is Confirmed <= 0
2239 static ir_node *transform_node_Abs(ir_node *n) {
2241 ir_node *a = get_Abs_op(n);
2242 value_classify_sign sign = classify_value_sign(a);
2244 if (sign == value_classified_negative) {
2245 ir_mode *mode = get_irn_mode(n);
2248 * We can replace the Abs by -x here.
2249 * We even could add a new Confirm here.
2251 * Note that -x would create a new node, so we could
2252 * not run it in the equivalent_node() context.
2254 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph,
2255 get_irn_n(n, -1), a, mode);
2257 DBG_OPT_CONFIRM(oldn, n);
2258 } else if (sign == value_classified_positive) {
2259 /* n is positive, Abs is not needed */
2262 DBG_OPT_CONFIRM(oldn, n);
2266 } /* transform_node_Abs */
2269 * Transform a Cond node.
2271 * Replace the Cond by a Jmp if it branches on a constant
2274 static ir_node *transform_node_Cond(ir_node *n) {
2277 ir_node *a = get_Cond_selector(n);
2278 tarval *ta = value_of(a);
2280 /* we need block info which is not available in floating irgs */
2281 if (get_irg_pinned(current_ir_graph) == op_pin_state_floats)
2284 if ((ta != tarval_bad) &&
2285 (get_irn_mode(a) == mode_b) &&
2286 (get_opt_unreachable_code())) {
2287 /* It's a boolean Cond, branching on a boolean constant.
2288 Replace it by a tuple (Bad, Jmp) or (Jmp, Bad) */
2289 jmp = new_r_Jmp(current_ir_graph, get_nodes_block(n));
2290 turn_into_tuple(n, pn_Cond_max);
2291 if (ta == tarval_b_true) {
2292 set_Tuple_pred(n, pn_Cond_false, new_Bad());
2293 set_Tuple_pred(n, pn_Cond_true, jmp);
2295 set_Tuple_pred(n, pn_Cond_false, jmp);
2296 set_Tuple_pred(n, pn_Cond_true, new_Bad());
2298 /* We might generate an endless loop, so keep it alive. */
2299 add_End_keepalive(get_irg_end(current_ir_graph), get_nodes_block(n));
2302 } /* transform_node_Cond */
2307 static ir_node *transform_node_And(ir_node *n) {
2308 ir_node *c, *oldn = n;
2309 ir_node *a = get_And_left(n);
2310 ir_node *b = get_And_right(n);
2312 HANDLE_BINOP_PHI(tarval_and, a,b,c);
2314 } /* transform_node_And */
2319 static ir_node *transform_node_Eor(ir_node *n) {
2320 ir_node *c, *oldn = n;
2321 ir_node *a = get_Eor_left(n);
2322 ir_node *b = get_Eor_right(n);
2323 ir_mode *mode = get_irn_mode(n);
2325 HANDLE_BINOP_PHI(tarval_eor, a,b,c);
2329 n = new_rd_Const(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1),
2330 mode, get_mode_null(mode));
2331 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_A_A);
2332 } else if ((mode == mode_b)
2333 && (get_irn_op(a) == op_Proj)
2334 && (get_irn_mode(a) == mode_b)
2335 && (classify_tarval (value_of(b)) == TV_CLASSIFY_ONE)
2336 && (get_irn_op(get_Proj_pred(a)) == op_Cmp)) {
2337 /* The Eor negates a Cmp. The Cmp has the negated result anyways! */
2338 n = new_r_Proj(current_ir_graph, get_irn_n(n, -1), get_Proj_pred(a),
2339 mode_b, get_negated_pnc(get_Proj_proj(a), mode));
2341 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT_BOOL);
2342 } else if ((mode == mode_b)
2343 && (classify_tarval (value_of(b)) == TV_CLASSIFY_ONE)) {
2344 /* The Eor is a Not. Replace it by a Not. */
2345 /* ????!!!Extend to bitfield 1111111. */
2346 n = new_r_Not(current_ir_graph, get_irn_n(n, -1), a, mode_b);
2348 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
2352 } /* transform_node_Eor */
2357 static ir_node *transform_node_Not(ir_node *n) {
2358 ir_node *c, *oldn = n;
2359 ir_node *a = get_Not_op(n);
2361 HANDLE_UNOP_PHI(tarval_not,a,c);
2363 /* check for a boolean Not */
2364 if ( (get_irn_mode(n) == mode_b)
2365 && (get_irn_op(a) == op_Proj)
2366 && (get_irn_mode(a) == mode_b)
2367 && (get_irn_op(get_Proj_pred(a)) == op_Cmp)) {
2368 /* We negate a Cmp. The Cmp has the negated result anyways! */
2369 n = new_r_Proj(current_ir_graph, get_irn_n(n, -1), get_Proj_pred(a),
2370 mode_b, get_negated_pnc(get_Proj_proj(a), mode_b));
2371 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_CMP);
2374 } /* transform_node_Not */
2377 * Transform a Minus.
2379 static ir_node *transform_node_Minus(ir_node *n) {
2380 ir_node *c, *oldn = n;
2381 ir_node *a = get_Minus_op(n);
2383 HANDLE_UNOP_PHI(tarval_neg,a,c);
2385 } /* transform_node_Minus */
2388 * Transform a Cast_type(Const) into a new Const_type
2390 static ir_node *transform_node_Cast(ir_node *n) {
2392 ir_node *pred = get_Cast_op(n);
2393 ir_type *tp = get_irn_type(n);
2395 if (get_irn_op(pred) == op_Const && get_Const_type(pred) != tp) {
2396 n = new_rd_Const_type(NULL, current_ir_graph, get_irn_n(pred, -1), get_irn_mode(pred),
2397 get_Const_tarval(pred), tp);
2398 DBG_OPT_CSTEVAL(oldn, n);
2399 } else if ((get_irn_op(pred) == op_SymConst) && (get_SymConst_value_type(pred) != tp)) {
2400 n = new_rd_SymConst_type(NULL, current_ir_graph, get_irn_n(pred, -1), get_SymConst_symbol(pred),
2401 get_SymConst_kind(pred), tp);
2402 DBG_OPT_CSTEVAL(oldn, n);
2406 } /* transform_node_Cast */
2409 * Transform a Proj(Div) with a non-zero value.
2410 * Removes the exceptions and routes the memory to the NoMem node.
2412 static ir_node *transform_node_Proj_Div(ir_node *proj) {
2413 ir_node *n = get_Proj_pred(proj);
2414 ir_node *b = get_Div_right(n);
2418 if (value_not_zero(b, &confirm)) {
2419 /* div(x, y) && y != 0 */
2420 proj_nr = get_Proj_proj(proj);
2421 if (proj_nr == pn_Div_X_except) {
2422 /* we found an exception handler, remove it */
2423 DBG_OPT_EXC_REM(proj);
2425 } else if (proj_nr == pn_Div_M) {
2426 ir_node *res = get_Div_mem(n);
2427 ir_node *new_mem = get_irg_no_mem(current_ir_graph);
2430 /* This node can only float up to the Confirm block */
2431 new_mem = new_r_Pin(current_ir_graph, get_nodes_block(confirm), new_mem);
2433 set_irn_pinned(n, op_pin_state_floats);
2434 /* this is a Div without exception, we can remove the memory edge */
2435 set_Div_mem(n, new_mem);
2440 } /* transform_node_Proj_Div */
2443 * Transform a Proj(Mod) with a non-zero value.
2444 * Removes the exceptions and routes the memory to the NoMem node.
2446 static ir_node *transform_node_Proj_Mod(ir_node *proj) {
2447 ir_node *n = get_Proj_pred(proj);
2448 ir_node *b = get_Mod_right(n);
2452 if (value_not_zero(b, &confirm)) {
2453 /* mod(x, y) && y != 0 */
2454 proj_nr = get_Proj_proj(proj);
2456 if (proj_nr == pn_Mod_X_except) {
2457 /* we found an exception handler, remove it */
2458 DBG_OPT_EXC_REM(proj);
2460 } else if (proj_nr == pn_Mod_M) {
2461 ir_node *res = get_Mod_mem(n);
2462 ir_node *new_mem = get_irg_no_mem(current_ir_graph);
2465 /* This node can only float up to the Confirm block */
2466 new_mem = new_r_Pin(current_ir_graph, get_nodes_block(confirm), new_mem);
2468 set_irn_pinned(n, op_pin_state_floats);
2469 /* this is a Mod without exception, we can remove the memory edge */
2470 set_Mod_mem(n, get_irg_no_mem(current_ir_graph));
2472 } else if (proj_nr == pn_Mod_res && get_Mod_left(n) == b) {
2473 /* a % a = 0 if a != 0 */
2474 ir_mode *mode = get_irn_mode(proj);
2475 ir_node *res = new_Const(mode, get_mode_null(mode));
2477 DBG_OPT_CSTEVAL(n, res);
2482 } /* transform_node_Proj_Mod */
2485 * Transform a Proj(DivMod) with a non-zero value.
2486 * Removes the exceptions and routes the memory to the NoMem node.
2488 static ir_node *transform_node_Proj_DivMod(ir_node *proj) {
2489 ir_node *n = get_Proj_pred(proj);
2490 ir_node *b = get_DivMod_right(n);
2494 if (value_not_zero(b, &confirm)) {
2495 /* DivMod(x, y) && y != 0 */
2496 proj_nr = get_Proj_proj(proj);
2498 if (proj_nr == pn_DivMod_X_except) {
2499 /* we found an exception handler, remove it */
2500 DBG_OPT_EXC_REM(proj);
2502 } else if (proj_nr == pn_DivMod_M) {
2503 ir_node *res = get_DivMod_mem(n);
2504 ir_node *new_mem = get_irg_no_mem(current_ir_graph);
2507 /* This node can only float up to the Confirm block */
2508 new_mem = new_r_Pin(current_ir_graph, get_nodes_block(confirm), new_mem);
2510 set_irn_pinned(n, op_pin_state_floats);
2511 /* this is a DivMod without exception, we can remove the memory edge */
2512 set_DivMod_mem(n, get_irg_no_mem(current_ir_graph));
2514 } else if (proj_nr == pn_DivMod_res_mod && get_DivMod_left(n) == b) {
2515 /* a % a = 0 if a != 0 */
2516 ir_mode *mode = get_irn_mode(proj);
2517 ir_node *res = new_Const(mode, get_mode_null(mode));
2519 DBG_OPT_CSTEVAL(n, res);
2524 } /* transform_node_Proj_DivMod */
2527 * Optimizes jump tables (CondIs or CondIu) by removing all impossible cases.
2529 static ir_node *transform_node_Proj_Cond(ir_node *proj) {
2530 if (get_opt_unreachable_code()) {
2531 ir_node *n = get_Proj_pred(proj);
2532 ir_node *b = get_Cond_selector(n);
2534 if (mode_is_int(get_irn_mode(b))) {
2535 tarval *tb = value_of(b);
2537 if (tb != tarval_bad) {
2538 /* we have a constant switch */
2539 long num = get_Proj_proj(proj);
2541 if (num != get_Cond_defaultProj(n)) { /* we cannot optimize default Proj's yet */
2542 if (get_tarval_long(tb) == num) {
2543 /* Do NOT create a jump here, or we will have 2 control flow ops
2544 * in a block. This case is optimized away in optimize_cf(). */
2547 /* this case will NEVER be taken, kill it */
2555 } /* transform_node_Proj_Cond */
2558 * Normalizes and optimizes Cmp nodes.
2560 static ir_node *transform_node_Proj_Cmp(ir_node *proj) {
2561 if (get_opt_reassociation()) {
2562 ir_node *n = get_Proj_pred(proj);
2563 ir_node *left = get_Cmp_left(n);
2564 ir_node *right = get_Cmp_right(n);
2568 ir_mode *mode = NULL;
2569 long proj_nr = get_Proj_proj(proj);
2572 * First step: normalize the compare op
2573 * by placing the constant on the right site
2574 * or moving the lower address node to the left.
2575 * We ignore the case that both are constants
2576 * this case should be optimized away.
2578 if (get_irn_op(right) == op_Const) {
2580 } else if (get_irn_op(left) == op_Const) {
2585 proj_nr = get_inversed_pnc(proj_nr);
2587 } else if (get_irn_idx(left) > get_irn_idx(right)) {
2593 proj_nr = get_inversed_pnc(proj_nr);
2598 * Second step: Try to reduce the magnitude
2599 * of a constant. This may help to generate better code
2600 * later and may help to normalize more compares.
2601 * Of course this is only possible for integer values.
2604 mode = get_irn_mode(c);
2605 tv = get_Const_tarval(c);
2607 if (tv != tarval_bad) {
2608 /* the following optimization is possible on modes without Overflow
2609 * on Unary Minus or on == and !=:
2610 * -a CMP c ==> a swap(CMP) -c
2612 * Beware: for two-complement Overflow may occur, so only == and != can
2613 * be optimized, see this:
2614 * -MININT < 0 =/=> MININT > 0 !!!
2616 if (get_opt_constant_folding() && get_irn_op(left) == op_Minus &&
2617 (!mode_overflow_on_unary_Minus(mode) ||
2618 (mode_is_int(mode) && (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg)))) {
2619 left = get_Minus_op(left);
2620 tv = tarval_sub(get_mode_null(mode), tv);
2622 proj_nr = get_inversed_pnc(proj_nr);
2626 /* for integer modes, we have more */
2627 if (mode_is_int(mode)) {
2628 /* Ne includes Unordered which is not possible on integers.
2629 * However, frontends often use this wrong, so fix it here */
2630 if (proj_nr & pn_Cmp_Uo) {
2631 proj_nr &= ~pn_Cmp_Uo;
2632 set_Proj_proj(proj, proj_nr);
2635 /* c > 0 : a < c ==> a <= (c-1) a >= c ==> a > (c-1) */
2636 if ((proj_nr == pn_Cmp_Lt || proj_nr == pn_Cmp_Ge) &&
2637 tarval_cmp(tv, get_mode_null(mode)) == pn_Cmp_Gt) {
2638 tv = tarval_sub(tv, get_mode_one(mode));
2640 proj_nr ^= pn_Cmp_Eq;
2643 /* c < 0 : a > c ==> a >= (c+1) a <= c ==> a < (c+1) */
2644 else if ((proj_nr == pn_Cmp_Gt || proj_nr == pn_Cmp_Le) &&
2645 tarval_cmp(tv, get_mode_null(mode)) == pn_Cmp_Lt) {
2646 tv = tarval_add(tv, get_mode_one(mode));
2648 proj_nr ^= pn_Cmp_Eq;
2652 /* the following reassociations work only for == and != */
2653 if (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) {
2655 /* a-b == 0 ==> a == b, a-b != 0 ==> a != b */
2656 if (classify_tarval(tv) == TV_CLASSIFY_NULL && get_irn_op(left) == op_Sub) {
2657 right = get_Sub_right(left);
2658 left = get_Sub_left(left);
2660 tv = value_of(right);
2664 if (tv != tarval_bad) {
2665 ir_op *op = get_irn_op(left);
2667 /* a-c1 == c2 ==> a == c2+c1, a-c1 != c2 ==> a != c2+c1 */
2669 ir_node *c1 = get_Sub_right(left);
2670 tarval *tv2 = value_of(c1);
2672 if (tv2 != tarval_bad) {
2673 tv2 = tarval_add(tv, value_of(c1));
2675 if (tv2 != tarval_bad) {
2676 left = get_Sub_left(left);
2682 /* a+c1 == c2 ==> a == c2-c1, a+c1 != c2 ==> a != c2-c1 */
2683 else if (op == op_Add) {
2684 ir_node *a_l = get_Add_left(left);
2685 ir_node *a_r = get_Add_right(left);
2689 if (get_irn_op(a_l) == op_Const) {
2691 tv2 = value_of(a_l);
2694 tv2 = value_of(a_r);
2697 if (tv2 != tarval_bad) {
2698 tv2 = tarval_sub(tv, tv2);
2700 if (tv2 != tarval_bad) {
2707 /* -a == c ==> a == -c, -a != c ==> a != -c */
2708 else if (op == op_Minus) {
2709 tarval *tv2 = tarval_sub(get_mode_null(mode), tv);
2711 if (tv2 != tarval_bad) {
2712 left = get_Minus_op(left);
2719 /* the following reassociations work only for <= */
2720 else if (proj_nr == pn_Cmp_Le || proj_nr == pn_Cmp_Lt) {
2721 if (tv != tarval_bad) {
2722 ir_op *op = get_irn_op(left);
2724 /* c >= 0 : Abs(a) <= c ==> (unsigned)(a + c) <= 2*c */
2732 * optimization for AND:
2734 * And(x, C) == C ==> And(x, C) != 0
2735 * And(x, C) != C ==> And(X, C) == 0
2737 * if C is a single Bit constant.
2739 if ((proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) &&
2740 (get_irn_op(left) == op_And)) {
2741 if (is_single_bit_tarval(tv)) {
2742 /* check for Constant's match. We have check hare the tarvals,
2743 because our const might be changed */
2744 ir_node *la = get_And_left(left);
2745 ir_node *ra = get_And_right(left);
2746 if ((is_Const(la) && get_Const_tarval(la) == tv) ||
2747 (is_Const(ra) && get_Const_tarval(ra) == tv)) {
2748 /* fine: do the transformation */
2749 tv = get_mode_null(get_tarval_mode(tv));
2750 proj_nr ^= pn_Cmp_Leg;
2755 } /* tarval != bad */
2759 ir_node *block = get_irn_n(n, -1); /* Beware of get_nodes_Block() */
2761 if (changed & 2) /* need a new Const */
2762 right = new_Const(mode, tv);
2764 /* create a new compare */
2765 n = new_rd_Cmp(get_irn_dbg_info(n), current_ir_graph, block,
2768 set_Proj_pred(proj, n);
2769 set_Proj_proj(proj, proj_nr);
2773 } /* transform_node_Proj_Cmp */
2776 * Does all optimizations on nodes that must be done on it's Proj's
2777 * because of creating new nodes.
2779 static ir_node *transform_node_Proj(ir_node *proj) {
2780 ir_node *n = get_Proj_pred(proj);
2782 switch (get_irn_opcode(n)) {
2784 return transform_node_Proj_Div(proj);
2787 return transform_node_Proj_Mod(proj);
2790 return transform_node_Proj_DivMod(proj);
2793 return transform_node_Proj_Cond(proj);
2796 return transform_node_Proj_Cmp(proj);
2799 /* should not happen, but if it does will be optimized away */
2800 return equivalent_node_Proj(proj);
2806 } /* transform_node_Proj */
2809 * Move Confirms down through Phi nodes.
2811 static ir_node *transform_node_Phi(ir_node *phi) {
2813 ir_mode *mode = get_irn_mode(phi);
2815 if (mode_is_reference(mode)) {
2816 n = get_irn_arity(phi);
2818 /* Beware of Phi0 */
2820 ir_node *pred = get_irn_n(phi, 0);
2821 ir_node *bound, *new_Phi, *block, **in;
2824 if (! is_Confirm(pred))
2827 bound = get_Confirm_bound(pred);
2828 pnc = get_Confirm_cmp(pred);
2830 NEW_ARR_A(ir_node *, in, n);
2831 in[0] = get_Confirm_value(pred);
2833 for (i = 1; i < n; ++i) {
2834 pred = get_irn_n(phi, i);
2836 if (! is_Confirm(pred) ||
2837 get_Confirm_bound(pred) != bound ||
2838 get_Confirm_cmp(pred) != pnc)
2840 in[i] = get_Confirm_value(pred);
2842 /* move the Confirm nodes "behind" the Phi */
2843 block = get_irn_n(phi, -1);
2844 new_Phi = new_r_Phi(current_ir_graph, block, n, in, get_irn_mode(phi));
2845 return new_r_Confirm(current_ir_graph, block, new_Phi, bound, pnc);
2849 } /* transform_node_Phi */
2852 * Returns the operands of a commutative bin-op, if one operand is
2853 * a const, it is returned as the second one.
2855 static void get_comm_Binop_Ops(ir_node *binop, ir_node **a, ir_node **c) {
2856 ir_node *op_a = get_binop_left(binop);
2857 ir_node *op_b = get_binop_right(binop);
2859 assert(is_op_commutative(get_irn_op(binop)));
2861 if (get_irn_op(op_a) == op_Const) {
2868 } /* get_comm_Binop_Ops */
2871 * Optimize a Or(And(Or(And(v,c4),c3),c2),c1) pattern if possible.
2872 * Such pattern may arise in bitfield stores.
2874 * value c4 value c4 & c2
2875 * AND c3 AND c1 | c3
2880 static ir_node *transform_node_Or_bf_store(ir_node *or) {
2883 ir_node *and_l, *c3;
2884 ir_node *value, *c4;
2885 ir_node *new_and, *new_const, *block;
2886 ir_mode *mode = get_irn_mode(or);
2888 tarval *tv1, *tv2, *tv3, *tv4, *tv, *n_tv4, *n_tv2;
2890 get_comm_Binop_Ops(or, &and, &c1);
2891 if ((get_irn_op(c1) != op_Const) || (get_irn_op(and) != op_And))
2894 get_comm_Binop_Ops(and, &or_l, &c2);
2895 if ((get_irn_op(c2) != op_Const) || (get_irn_op(or_l) != op_Or))
2898 get_comm_Binop_Ops(or_l, &and_l, &c3);
2899 if ((get_irn_op(c3) != op_Const) || (get_irn_op(and_l) != op_And))
2902 get_comm_Binop_Ops(and_l, &value, &c4);
2903 if (get_irn_op(c4) != op_Const)
2906 /* ok, found the pattern, check for conditions */
2907 assert(mode == get_irn_mode(and));
2908 assert(mode == get_irn_mode(or_l));
2909 assert(mode == get_irn_mode(and_l));
2911 tv1 = get_Const_tarval(c1);
2912 tv2 = get_Const_tarval(c2);
2913 tv3 = get_Const_tarval(c3);
2914 tv4 = get_Const_tarval(c4);
2916 tv = tarval_or(tv4, tv2);
2917 if (classify_tarval(tv) != TV_CLASSIFY_ALL_ONE) {
2918 /* have at least one 0 at the same bit position */
2922 n_tv4 = tarval_not(tv4);
2923 if (tv3 != tarval_and(tv3, n_tv4)) {
2924 /* bit in the or_mask is outside the and_mask */
2928 n_tv2 = tarval_not(tv2);
2929 if (tv1 != tarval_and(tv1, n_tv2)) {
2930 /* bit in the or_mask is outside the and_mask */
2934 /* ok, all conditions met */
2935 block = get_irn_n(or, -1);
2937 new_and = new_r_And(current_ir_graph, block,
2938 value, new_r_Const(current_ir_graph, block, mode, tarval_and(tv4, tv2)), mode);
2940 new_const = new_r_Const(current_ir_graph, block, mode, tarval_or(tv3, tv1));
2942 set_Or_left(or, new_and);
2943 set_Or_right(or, new_const);
2945 /* check for more */
2946 return transform_node_Or_bf_store(or);
2947 } /* transform_node_Or_bf_store */
2950 * Optimize an Or(shl(x, c), shr(x, bits - c)) into a Rot
2952 static ir_node *transform_node_Or_Rot(ir_node *or) {
2953 ir_mode *mode = get_irn_mode(or);
2954 ir_node *shl, *shr, *block;
2955 ir_node *irn, *x, *c1, *c2, *v, *sub, *n;
2958 if (! mode_is_int(mode))
2961 shl = get_binop_left(or);
2962 shr = get_binop_right(or);
2964 if (get_irn_op(shl) == op_Shr) {
2965 if (get_irn_op(shr) != op_Shl)
2971 } else if (get_irn_op(shl) != op_Shl) {
2973 } else if (get_irn_op(shr) != op_Shr) {
2976 x = get_Shl_left(shl);
2977 if (x != get_Shr_left(shr))
2980 c1 = get_Shl_right(shl);
2981 c2 = get_Shr_right(shr);
2982 if (get_irn_op(c1) == op_Const && get_irn_op(c2) == op_Const) {
2983 tv1 = get_Const_tarval(c1);
2984 if (! tarval_is_long(tv1))
2987 tv2 = get_Const_tarval(c2);
2988 if (! tarval_is_long(tv2))
2991 if (get_tarval_long(tv1) + get_tarval_long(tv2)
2992 != get_mode_size_bits(mode))
2995 /* yet, condition met */
2996 block = get_irn_n(or, -1);
2998 n = new_r_Rot(current_ir_graph, block, x, c1, mode);
3000 DBG_OPT_ALGSIM1(or, shl, shr, n, FS_OPT_OR_SHFT_TO_ROT);
3002 } else if (get_irn_op(c1) == op_Sub) {
3006 if (get_Sub_right(sub) != v)
3009 c1 = get_Sub_left(sub);
3010 if (get_irn_op(c1) != op_Const)
3013 tv1 = get_Const_tarval(c1);
3014 if (! tarval_is_long(tv1))
3017 if (get_tarval_long(tv1) != get_mode_size_bits(mode))
3020 /* yet, condition met */
3021 block = get_nodes_block(or);
3023 /* a Rot right is not supported, so use a rot left */
3024 n = new_r_Rot(current_ir_graph, block, x, sub, mode);
3026 DBG_OPT_ALGSIM0(or, n, FS_OPT_OR_SHFT_TO_ROT);
3028 } else if (get_irn_op(c2) == op_Sub) {
3032 c1 = get_Sub_left(sub);
3033 if (get_irn_op(c1) != op_Const)
3036 tv1 = get_Const_tarval(c1);
3037 if (! tarval_is_long(tv1))
3040 if (get_tarval_long(tv1) != get_mode_size_bits(mode))
3043 /* yet, condition met */
3044 block = get_irn_n(or, -1);
3047 n = new_r_Rot(current_ir_graph, block, x, v, mode);
3049 DBG_OPT_ALGSIM0(or, n, FS_OPT_OR_SHFT_TO_ROT);
3054 } /* transform_node_Or_Rot */
3059 static ir_node *transform_node_Or(ir_node *n) {
3060 ir_node *c, *oldn = n;
3061 ir_node *a = get_Or_left(n);
3062 ir_node *b = get_Or_right(n);
3064 HANDLE_BINOP_PHI(tarval_or, a,b,c);
3066 n = transform_node_Or_bf_store(n);
3067 n = transform_node_Or_Rot(n);
3070 } /* transform_node_Or */
3074 static ir_node *transform_node(ir_node *n);
3077 * Optimize (a >> c1) >> c2), works for Shr, Shrs, Shl.
3079 * Should be moved to reassociation?
3081 static ir_node *transform_node_shift(ir_node *n) {
3082 ir_node *left, *right;
3083 tarval *tv1, *tv2, *res;
3085 int modulo_shf, flag;
3087 left = get_binop_left(n);
3089 /* different operations */
3090 if (get_irn_op(left) != get_irn_op(n))
3093 right = get_binop_right(n);
3094 tv1 = value_of(right);
3095 if (tv1 == tarval_bad)
3098 tv2 = value_of(get_binop_right(left));
3099 if (tv2 == tarval_bad)
3102 res = tarval_add(tv1, tv2);
3104 /* beware: a simple replacement works only, if res < modulo shift */
3105 mode = get_irn_mode(n);
3109 modulo_shf = get_mode_modulo_shift(mode);
3110 if (modulo_shf > 0) {
3111 tarval *modulo = new_tarval_from_long(modulo_shf, get_tarval_mode(res));
3113 if (tarval_cmp(res, modulo) & pn_Cmp_Lt)
3119 /* ok, we can replace it */
3120 ir_node *in[2], *irn, *block = get_irn_n(n, -1);
3122 in[0] = get_binop_left(left);
3123 in[1] = new_r_Const(current_ir_graph, block, get_tarval_mode(res), res);
3125 irn = new_ir_node(NULL, current_ir_graph, block, get_irn_op(n), mode, 2, in);
3127 DBG_OPT_ALGSIM0(n, irn, FS_OPT_REASSOC_SHIFT);
3129 return transform_node(irn);
3132 } /* transform_node_shift */
3137 static ir_node *transform_node_Shr(ir_node *n) {
3138 ir_node *c, *oldn = n;
3139 ir_node *a = get_Shr_left(n);
3140 ir_node *b = get_Shr_right(n);
3142 HANDLE_BINOP_PHI(tarval_shr, a, b, c);
3143 return transform_node_shift(n);
3144 } /* transform_node_Shr */
3149 static ir_node *transform_node_Shrs(ir_node *n) {
3150 ir_node *c, *oldn = n;
3151 ir_node *a = get_Shrs_left(n);
3152 ir_node *b = get_Shrs_right(n);
3154 HANDLE_BINOP_PHI(tarval_shrs, a, b, c);
3155 return transform_node_shift(n);
3156 } /* transform_node_Shrs */
3161 static ir_node *transform_node_Shl(ir_node *n) {
3162 ir_node *c, *oldn = n;
3163 ir_node *a = get_Shl_left(n);
3164 ir_node *b = get_Shl_right(n);
3166 HANDLE_BINOP_PHI(tarval_shl, a, b, c);
3167 return transform_node_shift(n);
3168 } /* transform_node_Shl */
3171 * Remove dead blocks and nodes in dead blocks
3172 * in keep alive list. We do not generate a new End node.
3174 static ir_node *transform_node_End(ir_node *n) {
3175 int i, n_keepalives = get_End_n_keepalives(n);
3177 for (i = 0; i < n_keepalives; ++i) {
3178 ir_node *ka = get_End_keepalive(n, i);
3180 if (is_Block_dead(ka)) {
3181 set_End_keepalive(n, i, new_Bad());
3183 } else if (is_irn_pinned_in_irg(ka) && is_Block_dead(get_nodes_block(ka)))
3184 set_End_keepalive(n, i, new_Bad());
3187 } /* transform_node_End */
3190 * Optimize a Mux into some simpler cases.
3192 static ir_node *transform_node_Mux(ir_node *n) {
3193 ir_node *oldn = n, *sel = get_Mux_sel(n);
3194 ir_mode *mode = get_irn_mode(n);
3196 if (get_irn_op(sel) == op_Proj && !mode_honor_signed_zeros(mode)) {
3197 ir_node *cmp = get_Proj_pred(sel);
3198 long proj_nr = get_Proj_proj(sel);
3199 ir_node *f = get_Mux_false(n);
3200 ir_node *t = get_Mux_true(n);
3202 if (get_irn_op(cmp) == op_Cmp && classify_Const(get_Cmp_right(cmp)) == CNST_NULL) {
3203 ir_node *block = get_irn_n(n, -1);
3206 * Note: normalization puts the constant on the right site,
3207 * so we check only one case.
3209 * Note further that these optimization work even for floating point
3210 * with NaN's because -NaN == NaN.
3211 * However, if +0 and -0 is handled differently, we cannot use the first one.
3213 if (get_irn_op(f) == op_Minus &&
3214 get_Minus_op(f) == t &&
3215 get_Cmp_left(cmp) == t) {
3217 if (proj_nr == pn_Cmp_Ge || proj_nr == pn_Cmp_Gt) {
3218 /* Mux(a >=/> 0, -a, a) ==> Abs(a) */
3219 n = new_rd_Abs(get_irn_dbg_info(n),
3223 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_ABS);
3225 } else if (proj_nr == pn_Cmp_Le || proj_nr == pn_Cmp_Lt) {
3226 /* Mux(a <=/< 0, -a, a) ==> Minus(Abs(a)) */
3227 n = new_rd_Abs(get_irn_dbg_info(n),
3231 n = new_rd_Minus(get_irn_dbg_info(n),
3236 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_ABS);
3239 } else if (get_irn_op(t) == op_Minus &&
3240 get_Minus_op(t) == f &&
3241 get_Cmp_left(cmp) == f) {
3243 if (proj_nr == pn_Cmp_Le || proj_nr == pn_Cmp_Lt) {
3244 /* Mux(a <=/< 0, a, -a) ==> Abs(a) */
3245 n = new_rd_Abs(get_irn_dbg_info(n),
3249 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_ABS);
3251 } else if (proj_nr == pn_Cmp_Ge || proj_nr == pn_Cmp_Gt) {
3252 /* Mux(a >=/> 0, a, -a) ==> Minus(Abs(a)) */
3253 n = new_rd_Abs(get_irn_dbg_info(n),
3257 n = new_rd_Minus(get_irn_dbg_info(n),
3262 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_ABS);
3267 if (mode_is_int(mode) && mode_is_signed(mode) &&
3268 get_mode_arithmetic(mode) == irma_twos_complement) {
3269 ir_node *x = get_Cmp_left(cmp);
3271 /* the following optimization works only with signed integer two-complement mode */
3273 if (mode == get_irn_mode(x)) {
3275 * FIXME: this restriction is two rigid, as it would still
3276 * work if mode(x) = Hs and mode == Is, but at least it removes
3279 if ((proj_nr == pn_Cmp_Lt || proj_nr == pn_Cmp_Le) &&
3280 classify_Const(t) == CNST_ALL_ONE &&
3281 classify_Const(f) == CNST_NULL) {
3283 * Mux(x:T </<= 0, 0, -1) -> Shrs(x, sizeof_bits(T) - 1)
3287 n = new_rd_Shrs(get_irn_dbg_info(n),
3288 current_ir_graph, block, x,
3289 new_r_Const_long(current_ir_graph, block, mode_Iu,
3290 get_mode_size_bits(mode) - 1),
3292 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_SHR);
3294 } else if ((proj_nr == pn_Cmp_Gt || proj_nr == pn_Cmp_Ge) &&
3295 classify_Const(t) == CNST_ONE &&
3296 classify_Const(f) == CNST_NULL) {
3298 * Mux(x:T >/>= 0, 0, 1) -> Shr(-x, sizeof_bits(T) - 1)
3302 n = new_rd_Shr(get_irn_dbg_info(n),
3303 current_ir_graph, block,
3304 new_r_Minus(current_ir_graph, block, x, mode),
3305 new_r_Const_long(current_ir_graph, block, mode_Iu,
3306 get_mode_size_bits(mode) - 1),
3308 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_SHR);
3315 return arch_transform_node_Mux(n);
3316 } /* transform_node_Mux */
3319 * Optimize a Psi into some simpler cases.
3321 static ir_node *transform_node_Psi(ir_node *n) {
3323 return transform_node_Mux(n);
3326 } /* transform_node_Psi */
3329 * Tries several [inplace] [optimizing] transformations and returns an
3330 * equivalent node. The difference to equivalent_node() is that these
3331 * transformations _do_ generate new nodes, and thus the old node must
3332 * not be freed even if the equivalent node isn't the old one.
3334 static ir_node *transform_node(ir_node *n) {
3335 if (n->op->ops.transform_node)
3336 n = n->op->ops.transform_node(n);
3338 } /* transform_node */
3341 * Sets the default transform node operation for an ir_op_ops.
3343 * @param code the opcode for the default operation
3344 * @param ops the operations initialized
3349 static ir_op_ops *firm_set_default_transform_node(ir_opcode code, ir_op_ops *ops)
3353 ops->transform_node = transform_node_##a; \
3386 } /* firm_set_default_transform_node */
3389 /* **************** Common Subexpression Elimination **************** */
3391 /** The size of the hash table used, should estimate the number of nodes
3393 #define N_IR_NODES 512
3395 /** Compares the attributes of two Const nodes. */
3396 static int node_cmp_attr_Const(ir_node *a, ir_node *b) {
3397 return (get_Const_tarval(a) != get_Const_tarval(b))
3398 || (get_Const_type(a) != get_Const_type(b));
3399 } /* node_cmp_attr_Const */
3401 /** Compares the attributes of two Proj nodes. */
3402 static int node_cmp_attr_Proj(ir_node *a, ir_node *b) {
3403 return get_irn_proj_attr (a) != get_irn_proj_attr (b);
3404 } /* node_cmp_attr_Proj */
3406 /** Compares the attributes of two Filter nodes. */
3407 static int node_cmp_attr_Filter(ir_node *a, ir_node *b) {
3408 return get_Filter_proj(a) != get_Filter_proj(b);
3409 } /* node_cmp_attr_Filter */
3411 /** Compares the attributes of two Alloc nodes. */
3412 static int node_cmp_attr_Alloc(ir_node *a, ir_node *b) {
3413 return (get_irn_alloc_attr(a).where != get_irn_alloc_attr(b).where)
3414 || (get_irn_alloc_attr(a).type != get_irn_alloc_attr(b).type);
3415 } /* node_cmp_attr_Alloc */
3417 /** Compares the attributes of two Free nodes. */
3418 static int node_cmp_attr_Free(ir_node *a, ir_node *b) {
3419 return (get_irn_free_attr(a).where != get_irn_free_attr(b).where)
3420 || (get_irn_free_attr(a).type != get_irn_free_attr(b).type);
3421 } /* node_cmp_attr_Free */
3423 /** Compares the attributes of two SymConst nodes. */
3424 static int node_cmp_attr_SymConst(ir_node *a, ir_node *b) {
3425 return (get_irn_symconst_attr(a).num != get_irn_symconst_attr(b).num)
3426 || (get_irn_symconst_attr(a).sym.type_p != get_irn_symconst_attr(b).sym.type_p)
3427 || (get_irn_symconst_attr(a).tp != get_irn_symconst_attr(b).tp);
3428 } /* node_cmp_attr_SymConst */
3430 /** Compares the attributes of two Call nodes. */
3431 static int node_cmp_attr_Call(ir_node *a, ir_node *b) {
3432 return (get_irn_call_attr(a) != get_irn_call_attr(b));
3433 } /* node_cmp_attr_Call */
3435 /** Compares the attributes of two Sel nodes. */
3436 static int node_cmp_attr_Sel(ir_node *a, ir_node *b) {
3437 return (get_irn_sel_attr(a).ent->kind != get_irn_sel_attr(b).ent->kind)
3438 || (get_irn_sel_attr(a).ent->name != get_irn_sel_attr(b).ent->name)
3439 || (get_irn_sel_attr(a).ent->owner != get_irn_sel_attr(b).ent->owner)
3440 || (get_irn_sel_attr(a).ent->ld_name != get_irn_sel_attr(b).ent->ld_name)
3441 || (get_irn_sel_attr(a).ent->type != get_irn_sel_attr(b).ent->type);
3442 } /* node_cmp_attr_Sel */
3444 /** Compares the attributes of two Phi nodes. */
3445 static int node_cmp_attr_Phi(ir_node *a, ir_node *b) {
3446 return get_irn_phi_attr (a) != get_irn_phi_attr (b);
3447 } /* node_cmp_attr_Phi */
3449 /** Compares the attributes of two Conv nodes. */
3450 static int node_cmp_attr_Conv(ir_node *a, ir_node *b) {
3451 return get_Conv_strict(a) != get_Conv_strict(b);
3452 } /* node_cmp_attr_Conv */
3454 /** Compares the attributes of two Cast nodes. */
3455 static int node_cmp_attr_Cast(ir_node *a, ir_node *b) {
3456 return get_Cast_type(a) != get_Cast_type(b);
3457 } /* node_cmp_attr_Cast */
3459 /** Compares the attributes of two Load nodes. */
3460 static int node_cmp_attr_Load(ir_node *a, ir_node *b) {
3461 if (get_Load_volatility(a) == volatility_is_volatile ||
3462 get_Load_volatility(b) == volatility_is_volatile)
3463 /* NEVER do CSE on volatile Loads */
3466 return get_Load_mode(a) != get_Load_mode(b);
3467 } /* node_cmp_attr_Load */
3469 /** Compares the attributes of two Store nodes. */
3470 static int node_cmp_attr_Store(ir_node *a, ir_node *b) {
3471 /* NEVER do CSE on volatile Stores */
3472 return (get_Store_volatility(a) == volatility_is_volatile ||
3473 get_Store_volatility(b) == volatility_is_volatile);
3474 } /* node_cmp_attr_Store */
3476 /** Compares the attributes of two Confirm nodes. */
3477 static int node_cmp_attr_Confirm(ir_node *a, ir_node *b) {
3478 return (get_Confirm_cmp(a) != get_Confirm_cmp(b));
3479 } /* node_cmp_attr_Confirm */
3482 * Set the default node attribute compare operation for an ir_op_ops.
3484 * @param code the opcode for the default operation
3485 * @param ops the operations initialized
3490 static ir_op_ops *firm_set_default_node_cmp_attr(ir_opcode code, ir_op_ops *ops)
3494 ops->node_cmp_attr = node_cmp_attr_##a; \
3518 } /* firm_set_default_node_cmp_attr */
3521 * Compare function for two nodes in the hash table. Gets two
3522 * nodes as parameters. Returns 0 if the nodes are a cse.
3524 int identities_cmp(const void *elt, const void *key) {
3531 if (a == b) return 0;
3533 if ((get_irn_op(a) != get_irn_op(b)) ||
3534 (get_irn_mode(a) != get_irn_mode(b))) return 1;
3536 /* compare if a's in and b's in are of equal length */
3537 irn_arity_a = get_irn_intra_arity (a);
3538 if (irn_arity_a != get_irn_intra_arity(b))
3541 /* for block-local cse and op_pin_state_pinned nodes: */
3542 if (!get_opt_global_cse() || (get_irn_pinned(a) == op_pin_state_pinned)) {
3543 if (get_irn_intra_n(a, -1) != get_irn_intra_n(b, -1))
3547 /* compare a->in[0..ins] with b->in[0..ins] */
3548 for (i = 0; i < irn_arity_a; i++)
3549 if (get_irn_intra_n(a, i) != get_irn_intra_n(b, i))
3553 * here, we already now that the nodes are identical except their
3556 if (a->op->ops.node_cmp_attr)
3557 return a->op->ops.node_cmp_attr(a, b);
3560 } /* identities_cmp */
3563 * Calculate a hash value of a node.
3565 unsigned ir_node_hash(ir_node *node) {
3569 if (node->op == op_Const) {
3570 /* special value for const, as they only differ in their tarval. */
3571 h = HASH_PTR(node->attr.con.tv);
3572 h = 9*h + HASH_PTR(get_irn_mode(node));
3573 } else if (node->op == op_SymConst) {
3574 /* special value for const, as they only differ in their symbol. */
3575 h = HASH_PTR(node->attr.symc.sym.type_p);
3576 h = 9*h + HASH_PTR(get_irn_mode(node));
3579 /* hash table value = 9*(9*(9*(9*(9*arity+in[0])+in[1])+ ...)+mode)+code */
3580 h = irn_arity = get_irn_intra_arity(node);
3582 /* consider all in nodes... except the block if not a control flow. */
3583 for (i = is_cfop(node) ? -1 : 0; i < irn_arity; i++) {
3584 h = 9*h + HASH_PTR(get_irn_intra_n(node, i));
3588 h = 9*h + HASH_PTR(get_irn_mode(node));
3590 h = 9*h + HASH_PTR(get_irn_op(node));
3594 } /* ir_node_hash */
3596 pset *new_identities(void) {
3597 return new_pset(identities_cmp, N_IR_NODES);
3598 } /* new_identities */
3600 void del_identities(pset *value_table) {
3601 del_pset(value_table);
3602 } /* del_identities */
3605 * Return the canonical node computing the same value as n.
3607 * @param value_table The value table
3608 * @param n The node to lookup
3610 * Looks up the node in a hash table.
3612 * For Const nodes this is performed in the constructor, too. Const
3613 * nodes are extremely time critical because of their frequent use in
3614 * constant string arrays.
3616 static INLINE ir_node *identify(pset *value_table, ir_node *n) {
3619 if (!value_table) return n;
3621 if (get_opt_reassociation()) {
3622 if (is_op_commutative(get_irn_op(n))) {
3623 ir_node *l = get_binop_left(n);
3624 ir_node *r = get_binop_right(n);
3626 /* for commutative operators perform a OP b == b OP a */
3627 if (get_irn_idx(l) > get_irn_idx(r)) {
3628 set_binop_left(n, r);
3629 set_binop_right(n, l);
3634 o = pset_find(value_table, n, ir_node_hash(n));
3643 * During construction we set the op_pin_state_pinned flag in the graph right when the
3644 * optimization is performed. The flag turning on procedure global cse could
3645 * be changed between two allocations. This way we are safe.
3647 static INLINE ir_node *identify_cons(pset *value_table, ir_node *n) {
3650 n = identify(value_table, n);
3651 if (get_irn_n(old, -1) != get_irn_n(n, -1))
3652 set_irg_pinned(current_ir_graph, op_pin_state_floats);
3654 } /* identify_cons */
3657 * Return the canonical node computing the same value as n.
3658 * Looks up the node in a hash table, enters it in the table
3659 * if it isn't there yet.
3661 ir_node *identify_remember(pset *value_table, ir_node *n) {
3664 if (!value_table) return n;
3666 if (get_opt_reassociation()) {
3667 if (is_op_commutative(get_irn_op(n))) {
3668 ir_node *l = get_binop_left(n);
3669 ir_node *r = get_binop_right(n);
3670 int l_idx = get_irn_idx(l);
3671 int r_idx = get_irn_idx(r);
3673 /* For commutative operators perform a OP b == b OP a but keep
3674 constants on the RIGHT side. This helps greatly in some optimizations.
3675 Moreover we use the idx number to make the form deterministic. */
3676 if (is_irn_constlike(l))
3678 if (is_irn_constlike(r))
3680 if (l_idx < r_idx) {
3681 set_binop_left(n, r);
3682 set_binop_right(n, l);
3687 /* lookup or insert in hash table with given hash key. */
3688 o = pset_insert(value_table, n, ir_node_hash(n));
3695 } /* identify_remember */
3697 /* Add a node to the identities value table. */
3698 void add_identities(pset *value_table, ir_node *node) {
3699 if (get_opt_cse() && is_no_Block(node))
3700 identify_remember(value_table, node);
3701 } /* add_identities */
3703 /* Visit each node in the value table of a graph. */
3704 void visit_all_identities(ir_graph *irg, irg_walk_func visit, void *env) {
3706 ir_graph *rem = current_ir_graph;
3708 current_ir_graph = irg;
3709 foreach_pset(irg->value_table, node)
3711 current_ir_graph = rem;
3712 } /* visit_all_identities */
3715 * Garbage in, garbage out. If a node has a dead input, i.e., the
3716 * Bad node is input to the node, return the Bad node.
3718 static INLINE ir_node *gigo(ir_node *node) {
3720 ir_op *op = get_irn_op(node);
3722 /* remove garbage blocks by looking at control flow that leaves the block
3723 and replacing the control flow by Bad. */
3724 if (get_irn_mode(node) == mode_X) {
3725 ir_node *block = get_nodes_block(skip_Proj(node));
3727 /* Don't optimize nodes in immature blocks. */
3728 if (!get_Block_matured(block)) return node;
3729 /* Don't optimize End, may have Bads. */
3730 if (op == op_End) return node;
3732 if (is_Block(block)) {
3733 irn_arity = get_irn_arity(block);
3734 for (i = 0; i < irn_arity; i++) {
3735 if (!is_Bad(get_irn_n(block, i)))
3738 if (i == irn_arity) {
3739 ir_graph *irg = get_irn_irg(block);
3740 /* the start block is never dead */
3741 if (block != get_irg_start_block(irg)
3742 && block != get_irg_end_block(irg))
3748 /* Blocks, Phis and Tuples may have dead inputs, e.g., if one of the
3749 blocks predecessors is dead. */
3750 if (op != op_Block && op != op_Phi && op != op_Tuple) {
3751 irn_arity = get_irn_arity(node);
3754 * Beware: we can only read the block of a non-floating node.
3756 if (is_irn_pinned_in_irg(node) &&
3757 is_Block_dead(get_nodes_block(node)))
3760 for (i = 0; i < irn_arity; i++) {
3761 ir_node *pred = get_irn_n(node, i);
3766 /* Propagating Unknowns here seems to be a bad idea, because
3767 sometimes we need a node as a input and did not want that
3769 However, it might be useful to move this into a later phase
3770 (if you think that optimizing such code is useful). */
3771 if (is_Unknown(pred) && mode_is_data(get_irn_mode(node)))
3772 return new_Unknown(get_irn_mode(node));
3777 /* With this code we violate the agreement that local_optimize
3778 only leaves Bads in Block, Phi and Tuple nodes. */
3779 /* If Block has only Bads as predecessors it's garbage. */
3780 /* If Phi has only Bads as predecessors it's garbage. */
3781 if ((op == op_Block && get_Block_matured(node)) || op == op_Phi) {
3782 irn_arity = get_irn_arity(node);
3783 for (i = 0; i < irn_arity; i++) {
3784 if (!is_Bad(get_irn_n(node, i))) break;
3786 if (i == irn_arity) node = new_Bad();
3793 * These optimizations deallocate nodes from the obstack.
3794 * It can only be called if it is guaranteed that no other nodes
3795 * reference this one, i.e., right after construction of a node.
3797 * @param n The node to optimize
3799 * current_ir_graph must be set to the graph of the node!
3801 ir_node *optimize_node(ir_node *n) {
3804 ir_opcode iro = get_irn_opcode(n);
3806 /* Always optimize Phi nodes: part of the construction. */
3807 if ((!get_opt_optimize()) && (iro != iro_Phi)) return n;
3809 /* constant expression evaluation / constant folding */
3810 if (get_opt_constant_folding()) {
3811 /* neither constants nor Tuple values can be evaluated */
3812 if (iro != iro_Const && (get_irn_mode(n) != mode_T)) {
3813 /* try to evaluate */
3814 tv = computed_value(n);
3815 if (tv != tarval_bad) {
3817 ir_type *old_tp = get_irn_type(n);
3818 int i, arity = get_irn_arity(n);
3822 * Try to recover the type of the new expression.
3824 for (i = 0; i < arity && !old_tp; ++i)
3825 old_tp = get_irn_type(get_irn_n(n, i));
3828 * we MUST copy the node here temporary, because it's still needed
3829 * for DBG_OPT_CSTEVAL
3831 node_size = offsetof(ir_node, attr) + n->op->attr_size;
3832 oldn = alloca(node_size);
3834 memcpy(oldn, n, node_size);
3835 CLONE_ARR_A(ir_node *, oldn->in, n->in);
3837 /* ARG, copy the in array, we need it for statistics */
3838 memcpy(oldn->in, n->in, ARR_LEN(n->in) * sizeof(n->in[0]));
3840 /* note the inplace edges module */
3841 edges_node_deleted(n, current_ir_graph);
3843 /* evaluation was successful -- replace the node. */
3844 irg_kill_node(current_ir_graph, n);
3845 nw = new_Const(get_tarval_mode (tv), tv);
3847 if (old_tp && get_type_mode(old_tp) == get_tarval_mode (tv))
3848 set_Const_type(nw, old_tp);
3849 DBG_OPT_CSTEVAL(oldn, nw);
3855 /* remove unnecessary nodes */
3856 if (get_opt_constant_folding() ||
3857 (iro == iro_Phi) || /* always optimize these nodes. */
3859 (iro == iro_Proj) ||
3860 (iro == iro_Block) ) /* Flags tested local. */
3861 n = equivalent_node(n);
3863 optimize_preds(n); /* do node specific optimizations of nodes predecessors. */
3865 /* Common Subexpression Elimination.
3867 * Checks whether n is already available.
3868 * The block input is used to distinguish different subexpressions. Right
3869 * now all nodes are op_pin_state_pinned to blocks, i.e., the CSE only finds common
3870 * subexpressions within a block.
3873 n = identify_cons(current_ir_graph->value_table, n);
3876 edges_node_deleted(oldn, current_ir_graph);
3878 /* We found an existing, better node, so we can deallocate the old node. */
3879 irg_kill_node(current_ir_graph, oldn);
3883 /* Some more constant expression evaluation that does not allow to
3885 iro = get_irn_opcode(n);
3886 if (get_opt_constant_folding() ||
3887 (iro == iro_Cond) ||
3888 (iro == iro_Proj)) /* Flags tested local. */
3889 n = transform_node(n);
3891 /* Remove nodes with dead (Bad) input.
3892 Run always for transformation induced Bads. */
3895 /* Now we have a legal, useful node. Enter it in hash table for CSE */
3896 if (get_opt_cse() && (get_irn_opcode(n) != iro_Block)) {
3897 n = identify_remember(current_ir_graph->value_table, n);
3901 } /* optimize_node */
3905 * These optimizations never deallocate nodes (in place). This can cause dead
3906 * nodes lying on the obstack. Remove these by a dead node elimination,
3907 * i.e., a copying garbage collection.
3909 ir_node *optimize_in_place_2(ir_node *n) {
3912 ir_opcode iro = get_irn_opcode(n);
3914 if (!get_opt_optimize() && (get_irn_op(n) != op_Phi)) return n;
3916 /* constant expression evaluation / constant folding */
3917 if (get_opt_constant_folding()) {
3918 /* neither constants nor Tuple values can be evaluated */
3919 if (iro != iro_Const && get_irn_mode(n) != mode_T) {
3920 /* try to evaluate */
3921 tv = computed_value(n);
3922 if (tv != tarval_bad) {
3923 /* evaluation was successful -- replace the node. */
3924 ir_type *old_tp = get_irn_type(n);
3925 int i, arity = get_irn_arity(n);
3928 * Try to recover the type of the new expression.
3930 for (i = 0; i < arity && !old_tp; ++i)
3931 old_tp = get_irn_type(get_irn_n(n, i));
3933 n = new_Const(get_tarval_mode(tv), tv);
3935 if (old_tp && get_type_mode(old_tp) == get_tarval_mode(tv))
3936 set_Const_type(n, old_tp);
3938 DBG_OPT_CSTEVAL(oldn, n);
3944 /* remove unnecessary nodes */
3945 if (get_opt_constant_folding() ||
3946 (iro == iro_Phi) || /* always optimize these nodes. */
3947 (iro == iro_Id) || /* ... */
3948 (iro == iro_Proj) || /* ... */
3949 (iro == iro_Block) ) /* Flags tested local. */
3950 n = equivalent_node(n);
3952 optimize_preds(n); /* do node specific optimizations of nodes predecessors. */
3954 /** common subexpression elimination **/
3955 /* Checks whether n is already available. */
3956 /* The block input is used to distinguish different subexpressions. Right
3957 now all nodes are op_pin_state_pinned to blocks, i.e., the cse only finds common
3958 subexpressions within a block. */
3959 if (get_opt_cse()) {
3960 n = identify(current_ir_graph->value_table, n);
3963 /* Some more constant expression evaluation. */
3964 iro = get_irn_opcode(n);
3965 if (get_opt_constant_folding() ||
3966 (iro == iro_Cond) ||
3967 (iro == iro_Proj)) /* Flags tested local. */
3968 n = transform_node(n);
3970 /* Remove nodes with dead (Bad) input.
3971 Run always for transformation induced Bads. */
3974 /* Now we can verify the node, as it has no dead inputs any more. */
3977 /* Now we have a legal, useful node. Enter it in hash table for cse.
3978 Blocks should be unique anyways. (Except the successor of start:
3979 is cse with the start block!) */
3980 if (get_opt_cse() && (get_irn_opcode(n) != iro_Block))
3981 n = identify_remember(current_ir_graph->value_table, n);
3984 } /* optimize_in_place_2 */
3987 * Wrapper for external use, set proper status bits after optimization.
3989 ir_node *optimize_in_place(ir_node *n) {
3990 /* Handle graph state */
3991 assert(get_irg_phase_state(current_ir_graph) != phase_building);
3993 if (get_opt_global_cse())
3994 set_irg_pinned(current_ir_graph, op_pin_state_floats);
3995 if (get_irg_outs_state(current_ir_graph) == outs_consistent)
3996 set_irg_outs_inconsistent(current_ir_graph);
3998 /* FIXME: Maybe we could also test whether optimizing the node can
3999 change the control graph. */
4000 set_irg_doms_inconsistent(current_ir_graph);
4001 return optimize_in_place_2(n);
4002 } /* optimize_in_place */
4005 * Sets the default operation for an ir_ops.
4007 ir_op_ops *firm_set_default_operations(ir_opcode code, ir_op_ops *ops) {
4008 ops = firm_set_default_computed_value(code, ops);
4009 ops = firm_set_default_equivalent_node(code, ops);
4010 ops = firm_set_default_transform_node(code, ops);
4011 ops = firm_set_default_node_cmp_attr(code, ops);
4012 ops = firm_set_default_get_type(code, ops);
4013 ops = firm_set_default_get_type_attr(code, ops);
4014 ops = firm_set_default_get_entity_attr(code, ops);
4017 } /* firm_set_default_operations */