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 ir_node *blk = get_nodes_block(n);
1055 turn_into_tuple(n, pn_Div_max);
1056 set_Tuple_pred(n, pn_Div_M, mem);
1057 set_Tuple_pred(n, pn_Div_X_regular, new_r_Jmp(current_ir_graph, blk));
1058 set_Tuple_pred(n, pn_Div_X_except, new_Bad()); /* no exception */
1059 set_Tuple_pred(n, pn_Div_res, a);
1062 } /* equivalent_node_Div */
1065 * Optimize a / 1.0 = a.
1067 static ir_node *equivalent_node_Quot(ir_node *n) {
1068 ir_node *a = get_Quot_left(n);
1069 ir_node *b = get_Quot_right(n);
1071 /* Div is not commutative. */
1072 if (classify_tarval(value_of(b)) == TV_CLASSIFY_ONE) { /* Quot(x, 1) == x */
1073 /* Turn Quot into a tuple (mem, bad, a) */
1074 ir_node *mem = get_Quot_mem(n);
1075 ir_node *blk = get_nodes_block(n);
1076 turn_into_tuple(n, pn_Quot_max);
1077 set_Tuple_pred(n, pn_Quot_M, mem);
1078 set_Tuple_pred(n, pn_Quot_X_regular, new_r_Jmp(current_ir_graph, blk));
1079 set_Tuple_pred(n, pn_Quot_X_except, new_Bad()); /* no exception */
1080 set_Tuple_pred(n, pn_Quot_res, a);
1083 } /* equivalent_node_Quot */
1086 * Optimize a / 1 = a.
1088 static ir_node *equivalent_node_DivMod(ir_node *n) {
1089 ir_node *b = get_DivMod_right(n);
1091 /* Div is not commutative. */
1092 if (classify_tarval(value_of(b)) == TV_CLASSIFY_ONE) { /* div(x, 1) == x */
1093 /* Turn DivMod into a tuple (mem, bad, a, 0) */
1094 ir_node *a = get_DivMod_left(n);
1095 ir_node *mem = get_Div_mem(n);
1096 ir_node *blk = get_nodes_block(n);
1097 ir_mode *mode = get_DivMod_resmode(n);
1099 turn_into_tuple(n, pn_DivMod_max);
1100 set_Tuple_pred(n, pn_DivMod_M, mem);
1101 set_Tuple_pred(n, pn_DivMod_X_regular, new_r_Jmp(current_ir_graph, blk));
1102 set_Tuple_pred(n, pn_DivMod_X_except, new_Bad()); /* no exception */
1103 set_Tuple_pred(n, pn_DivMod_res_div, a);
1104 set_Tuple_pred(n, pn_DivMod_res_mod, new_Const(mode, get_mode_null(mode)));
1107 } /* equivalent_node_DivMod */
1110 * Use algebraic simplification a | a = a | 0 = 0 | a = a.
1112 static ir_node *equivalent_node_Or(ir_node *n) {
1115 ir_node *a = get_Or_left(n);
1116 ir_node *b = get_Or_right(n);
1119 n = a; /* Or has it's own neutral element */
1120 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_OR);
1121 } else if (classify_tarval(value_of(a)) == TV_CLASSIFY_NULL) {
1123 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_OR);
1124 } else if (classify_tarval(value_of(b)) == TV_CLASSIFY_NULL) {
1126 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_OR);
1130 } /* equivalent_node_Or */
1133 * Optimize a & 0b1...1 = 0b1...1 & a = a & a = a.
1135 static ir_node *equivalent_node_And(ir_node *n) {
1138 ir_node *a = get_And_left(n);
1139 ir_node *b = get_And_right(n);
1142 n = a; /* And has it's own neutral element */
1143 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_AND);
1144 } else if (classify_tarval(value_of(a)) == TV_CLASSIFY_ALL_ONE) {
1146 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1147 } else if (classify_tarval(value_of(b)) == TV_CLASSIFY_ALL_ONE) {
1149 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1152 } /* equivalent_node_And */
1155 * Try to remove useless Conv's:
1157 static ir_node *equivalent_node_Conv(ir_node *n) {
1159 ir_node *a = get_Conv_op(n);
1162 ir_mode *n_mode = get_irn_mode(n);
1163 ir_mode *a_mode = get_irn_mode(a);
1165 if (n_mode == a_mode) { /* No Conv necessary */
1166 /* leave strict floating point Conv's */
1167 if (get_Conv_strict(n))
1170 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CONV);
1171 } else if (get_irn_op(a) == op_Conv) { /* Conv(Conv(b)) */
1175 n_mode = get_irn_mode(n);
1176 b_mode = get_irn_mode(b);
1178 if (n_mode == b_mode) {
1179 if (n_mode == mode_b) {
1180 n = b; /* Convb(Conv*(xxxb(...))) == xxxb(...) */
1181 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
1182 } else if (mode_is_int(n_mode) || mode_is_character(n_mode)) {
1183 if (smaller_mode(b_mode, a_mode)){
1184 n = b; /* ConvS(ConvL(xxxS(...))) == xxxS(...) */
1185 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
1191 } /* equivalent_node_Conv */
1194 * A Cast may be removed if the type of the previous node
1195 * is already the type of the Cast.
1197 static ir_node *equivalent_node_Cast(ir_node *n) {
1199 ir_node *pred = get_Cast_op(n);
1201 if (get_irn_type(pred) == get_Cast_type(n)) {
1203 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CAST);
1206 } /* equivalent_node_Cast */
1209 * Several optimizations:
1210 * - no Phi in start block.
1211 * - remove Id operators that are inputs to Phi
1212 * - fold Phi-nodes, iff they have only one predecessor except
1215 static ir_node *equivalent_node_Phi(ir_node *n) {
1219 ir_node *block = NULL; /* to shutup gcc */
1220 ir_node *first_val = NULL; /* to shutup gcc */
1222 if (!get_opt_normalize()) return n;
1224 n_preds = get_Phi_n_preds(n);
1226 block = get_nodes_block(n);
1227 /* @@@ fliegt 'raus, sollte aber doch immer wahr sein!!!
1228 assert(get_irn_arity(block) == n_preds && "phi in wrong block!"); */
1229 if ((is_Block_dead(block)) || /* Control dead */
1230 (block == get_irg_start_block(current_ir_graph))) /* There should be no Phi nodes */
1231 return new_Bad(); /* in the Start Block. */
1233 if (n_preds == 0) return n; /* Phi of dead Region without predecessors. */
1235 /* If the Block has a Bad pred, we also have one. */
1236 for (i = 0; i < n_preds; ++i)
1237 if (is_Bad(get_Block_cfgpred(block, i)))
1238 set_Phi_pred(n, i, new_Bad());
1240 /* Find first non-self-referencing input */
1241 for (i = 0; i < n_preds; ++i) {
1242 first_val = get_Phi_pred(n, i);
1243 if ( (first_val != n) /* not self pointer */
1245 && (! is_Bad(first_val))
1247 ) { /* value not dead */
1248 break; /* then found first value. */
1253 /* A totally Bad or self-referencing Phi (we didn't break the above loop) */
1257 /* search for rest of inputs, determine if any of these
1258 are non-self-referencing */
1259 while (++i < n_preds) {
1260 ir_node *scnd_val = get_Phi_pred(n, i);
1261 if ( (scnd_val != n)
1262 && (scnd_val != first_val)
1264 && (! is_Bad(scnd_val))
1272 /* Fold, if no multiple distinct non-self-referencing inputs */
1274 DBG_OPT_PHI(oldn, n);
1277 } /* equivalent_node_Phi */
1280 * Several optimizations:
1281 * - no Sync in start block.
1282 * - fold Sync-nodes, iff they have only one predecessor except
1285 static ir_node *equivalent_node_Sync(ir_node *n) {
1289 ir_node *first_val = NULL; /* to shutup gcc */
1291 if (!get_opt_normalize()) return n;
1293 n_preds = get_Sync_n_preds(n);
1295 /* Find first non-self-referencing input */
1296 for (i = 0; i < n_preds; ++i) {
1297 first_val = get_Sync_pred(n, i);
1298 if ((first_val != n) /* not self pointer */ &&
1299 (! is_Bad(first_val))
1300 ) { /* value not dead */
1301 break; /* then found first value. */
1306 /* A totally Bad or self-referencing Sync (we didn't break the above loop) */
1309 /* search the rest of inputs, determine if any of these
1310 are non-self-referencing */
1311 while (++i < n_preds) {
1312 ir_node *scnd_val = get_Sync_pred(n, i);
1313 if ((scnd_val != n) &&
1314 (scnd_val != first_val) &&
1315 (! is_Bad(scnd_val))
1321 /* Fold, if no multiple distinct non-self-referencing inputs */
1323 DBG_OPT_SYNC(oldn, n);
1326 } /* equivalent_node_Sync */
1329 * Optimize Proj(Tuple) and gigo() for ProjX in Bad block,
1330 * ProjX(Load) and ProjX(Store).
1332 static ir_node *equivalent_node_Proj(ir_node *n) {
1334 ir_node *a = get_Proj_pred(n);
1336 if ( get_irn_op(a) == op_Tuple) {
1337 /* Remove the Tuple/Proj combination. */
1338 if ( get_Proj_proj(n) <= get_Tuple_n_preds(a) ) {
1339 n = get_Tuple_pred(a, get_Proj_proj(n));
1340 DBG_OPT_TUPLE(oldn, a, n);
1342 assert(0); /* This should not happen! */
1345 } else if (get_irn_mode(n) == mode_X) {
1346 if (is_Block_dead(get_nodes_block(skip_Proj(n)))) {
1347 /* Remove dead control flow -- early gigo(). */
1349 } else if (get_opt_ldst_only_null_ptr_exceptions()) {
1350 ir_op *op = get_irn_op(a);
1352 if (op == op_Load || op == op_Store) {
1353 /* get the load/store address */
1354 ir_node *addr = get_irn_n(a, 1);
1357 if (value_not_null(addr, &confirm)) {
1358 if (confirm == NULL) {
1359 /* this node may float if it did not depend on a Confirm */
1360 set_irn_pinned(a, op_pin_state_floats);
1370 } /* equivalent_node_Proj */
1375 static ir_node *equivalent_node_Id(ir_node *n) {
1380 } while (get_irn_op(n) == op_Id);
1382 DBG_OPT_ID(oldn, n);
1384 } /* equivalent_node_Id */
1389 static ir_node *equivalent_node_Mux(ir_node *n)
1391 ir_node *oldn = n, *sel = get_Mux_sel(n);
1392 tarval *ts = value_of(sel);
1394 /* Mux(true, f, t) == t */
1395 if (ts == tarval_b_true) {
1396 n = get_Mux_true(n);
1397 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_C);
1399 /* Mux(false, f, t) == f */
1400 else if (ts == tarval_b_false) {
1401 n = get_Mux_false(n);
1402 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_C);
1404 /* Mux(v, x, x) == x */
1405 else if (get_Mux_false(n) == get_Mux_true(n)) {
1406 n = get_Mux_true(n);
1407 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_EQ);
1409 else if (get_irn_op(sel) == op_Proj && !mode_honor_signed_zeros(get_irn_mode(n))) {
1410 ir_node *cmp = get_Proj_pred(sel);
1411 long proj_nr = get_Proj_proj(sel);
1412 ir_node *b = get_Mux_false(n);
1413 ir_node *a = get_Mux_true(n);
1416 * Note: normalization puts the constant on the right site,
1417 * so we check only one case.
1419 * Note further that these optimization work even for floating point
1420 * with NaN's because -NaN == NaN.
1421 * However, if +0 and -0 is handled differently, we cannot use the first one.
1423 if (get_irn_op(cmp) == op_Cmp && get_Cmp_left(cmp) == a) {
1424 if (classify_Const(get_Cmp_right(cmp)) == CNST_NULL) {
1425 /* Mux(a CMP 0, X, a) */
1426 if (get_irn_op(b) == op_Minus && get_Minus_op(b) == a) {
1427 /* Mux(a CMP 0, -a, a) */
1428 if (proj_nr == pn_Cmp_Eq) {
1429 /* Mux(a == 0, -a, a) ==> -a */
1431 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1432 } else if (proj_nr == pn_Cmp_Lg || proj_nr == pn_Cmp_Ne) {
1433 /* Mux(a != 0, -a, a) ==> a */
1435 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1437 } else if (classify_Const(b) == CNST_NULL) {
1438 /* Mux(a CMP 0, 0, a) */
1439 if (proj_nr == pn_Cmp_Lg || proj_nr == pn_Cmp_Ne) {
1440 /* Mux(a != 0, 0, a) ==> a */
1442 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1443 } else if (proj_nr == pn_Cmp_Eq) {
1444 /* Mux(a == 0, 0, a) ==> 0 */
1446 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1453 } /* equivalent_node_Mux */
1456 * Returns a equivalent node of a Psi: if a condition is true
1457 * and all previous conditions are false we know its value.
1458 * If all conditions are false its value is the default one.
1460 static ir_node *equivalent_node_Psi(ir_node *n) {
1462 return equivalent_node_Mux(n);
1464 } /* equivalent_node_Psi */
1467 * Optimize -a CMP -b into b CMP a.
1468 * This works only for for modes where unary Minus
1470 * Note that two-complement integers can Overflow
1471 * so it will NOT work.
1473 * For == and != can be handled in Proj(Cmp)
1475 static ir_node *equivalent_node_Cmp(ir_node *n) {
1476 ir_node *left = get_Cmp_left(n);
1477 ir_node *right = get_Cmp_right(n);
1479 if (get_irn_op(left) == op_Minus && get_irn_op(right) == op_Minus &&
1480 !mode_overflow_on_unary_Minus(get_irn_mode(left))) {
1481 left = get_Minus_op(left);
1482 right = get_Minus_op(right);
1483 set_Cmp_left(n, right);
1484 set_Cmp_right(n, left);
1487 } /* equivalent_node_Cmp */
1490 * Remove Confirm nodes if setting is on.
1491 * Replace Confirms(x, '=', Constlike) by Constlike.
1493 static ir_node *equivalent_node_Confirm(ir_node *n) {
1494 ir_node *pred = get_Confirm_value(n);
1495 pn_Cmp pnc = get_Confirm_cmp(n);
1497 if (get_irn_op(pred) == op_Confirm && pnc == get_Confirm_cmp(pred)) {
1499 * rare case: two identical Confirms one after another,
1500 * replace the second one with the first.
1504 if (pnc == pn_Cmp_Eq) {
1505 ir_node *bound = get_Confirm_bound(n);
1508 * Optimize a rare case:
1509 * Confirm(x, '=', Constlike) ==> Constlike
1511 if (is_irn_constlike(bound)) {
1512 DBG_OPT_CONFIRM(n, bound);
1516 return get_opt_remove_confirm() ? get_Confirm_value(n) : n;
1520 * Optimize CopyB(mem, x, x) into a Nop.
1522 static ir_node *equivalent_node_CopyB(ir_node *n) {
1523 ir_node *a = get_CopyB_dst(n);
1524 ir_node *b = get_CopyB_src(n);
1527 /* Turn CopyB into a tuple (mem, bad, bad) */
1528 ir_node *mem = get_CopyB_mem(n);
1529 ir_node *blk = get_nodes_block(n);
1530 turn_into_tuple(n, pn_CopyB_max);
1531 set_Tuple_pred(n, pn_CopyB_M, mem);
1532 set_Tuple_pred(n, pn_CopyB_X_regular, new_r_Jmp(current_ir_graph, blk));
1533 set_Tuple_pred(n, pn_CopyB_X_except, new_Bad()); /* no exception */
1534 set_Tuple_pred(n, pn_CopyB_M_except, new_Bad());
1537 } /* equivalent_node_CopyB */
1540 * Optimize Bounds(idx, idx, upper) into idx.
1542 static ir_node *equivalent_node_Bound(ir_node *n) {
1543 ir_node *idx = get_Bound_index(n);
1544 ir_node *lower = get_Bound_lower(n);
1547 /* By definition lower < upper, so if idx == lower -->
1548 lower <= idx && idx < upper */
1550 /* Turn Bound into a tuple (mem, bad, idx) */
1553 ir_node *pred = skip_Proj(idx);
1555 if (get_irn_op(pred) == op_Bound) {
1557 * idx was Bounds_check previously, it is still valid if
1558 * lower <= pred_lower && pred_upper <= upper.
1560 ir_node *upper = get_Bound_upper(n);
1561 if (get_Bound_lower(pred) == lower &&
1562 get_Bound_upper(pred) == upper) {
1564 * One could expect that we simply return the previous
1565 * Bound here. However, this would be wrong, as we could
1566 * add an exception Proj to a new location than.
1567 * So, we must turn in into a tuple
1574 /* Turn Bound into a tuple (mem, bad, idx) */
1575 ir_node *mem = get_Bound_mem(n);
1576 ir_node *blk = get_nodes_block(n);
1577 turn_into_tuple(n, pn_Bound_max);
1578 set_Tuple_pred(n, pn_Bound_M, mem);
1579 set_Tuple_pred(n, pn_Bound_X_regular, new_r_Jmp(current_ir_graph, blk)); /* no exception */
1580 set_Tuple_pred(n, pn_Bound_X_except, new_Bad()); /* no exception */
1581 set_Tuple_pred(n, pn_Bound_res, idx);
1584 } /* equivalent_node_Bound */
1587 * equivalent_node() returns a node equivalent to input n. It skips all nodes that
1588 * perform no actual computation, as, e.g., the Id nodes. It does not create
1589 * new nodes. It is therefore safe to free n if the node returned is not n.
1590 * If a node returns a Tuple we can not just skip it. If the size of the
1591 * in array fits, we transform n into a tuple (e.g., Div).
1593 ir_node *equivalent_node(ir_node *n) {
1594 if (n->op->ops.equivalent_node)
1595 return n->op->ops.equivalent_node(n);
1597 } /* equivalent_node */
1600 * Sets the default equivalent node operation for an ir_op_ops.
1602 * @param code the opcode for the default operation
1603 * @param ops the operations initialized
1608 static ir_op_ops *firm_set_default_equivalent_node(ir_opcode code, ir_op_ops *ops)
1612 ops->equivalent_node = equivalent_node_##a; \
1652 } /* firm_set_default_equivalent_node */
1655 * Do node specific optimizations of nodes predecessors.
1657 static void optimize_preds(ir_node *n) {
1658 switch (get_irn_opcode(n)) {
1660 case iro_Cmp: { /* We don't want Cast as input to Cmp. */
1661 ir_node *a = get_Cmp_left(n), *b = get_Cmp_right(n);
1663 if (get_irn_op(a) == op_Cast) {
1667 if (get_irn_op(b) == op_Cast) {
1669 set_Cmp_right(n, b);
1676 } /* optimize_preds */
1679 * Returns non-zero if a node is a Phi node
1680 * with all predecessors constant.
1682 static int is_const_Phi(ir_node *n) {
1687 for (i = get_irn_arity(n) - 1; i >= 0; --i)
1688 if (! is_Const(get_irn_n(n, i)))
1691 } /* is_const_Phi */
1694 * Apply an evaluator on a binop with a constant operators (and one Phi).
1696 * @param phi the Phi node
1697 * @param other the other operand
1698 * @param eval an evaluator function
1699 * @param left if non-zero, other is the left operand, else the right
1701 * @return a new Phi node if the conversion was successful, NULL else
1703 static ir_node *apply_binop_on_phi(ir_node *phi, tarval *other, tarval *(*eval)(tarval *, tarval *), int left) {
1709 int i, n = get_irn_arity(phi);
1711 NEW_ARR_A(void *, res, n);
1713 for (i = 0; i < n; ++i) {
1714 pred = get_irn_n(phi, i);
1715 tv = get_Const_tarval(pred);
1716 tv = eval(other, tv);
1718 if (tv == tarval_bad) {
1719 /* folding failed, bad */
1725 for (i = 0; i < n; ++i) {
1726 pred = get_irn_n(phi, i);
1727 tv = get_Const_tarval(pred);
1728 tv = eval(tv, other);
1730 if (tv == tarval_bad) {
1731 /* folding failed, bad */
1737 mode = get_irn_mode(phi);
1738 irg = current_ir_graph;
1739 for (i = 0; i < n; ++i) {
1740 pred = get_irn_n(phi, i);
1741 res[i] = new_r_Const_type(irg, get_irg_start_block(irg),
1742 mode, res[i], get_Const_type(pred));
1744 return new_r_Phi(irg, get_nodes_block(phi), n, (ir_node **)res, mode);
1745 } /* apply_binop_on_phi */
1748 * Apply an evaluator on a unop with a constant operator (a Phi).
1750 * @param phi the Phi node
1751 * @param eval an evaluator function
1753 * @return a new Phi node if the conversion was successful, NULL else
1755 static ir_node *apply_unop_on_phi(ir_node *phi, tarval *(*eval)(tarval *)) {
1761 int i, n = get_irn_arity(phi);
1763 NEW_ARR_A(void *, res, n);
1764 for (i = 0; i < n; ++i) {
1765 pred = get_irn_n(phi, i);
1766 tv = get_Const_tarval(pred);
1769 if (tv == tarval_bad) {
1770 /* folding failed, bad */
1775 mode = get_irn_mode(phi);
1776 irg = current_ir_graph;
1777 for (i = 0; i < n; ++i) {
1778 pred = get_irn_n(phi, i);
1779 res[i] = new_r_Const_type(irg, get_irg_start_block(irg),
1780 mode, res[i], get_Const_type(pred));
1782 return new_r_Phi(irg, get_nodes_block(phi), n, (ir_node **)res, mode);
1783 } /* apply_unop_on_phi */
1786 * Transform AddP(P, ConvIs(Iu)), AddP(P, ConvIu(Is)) and
1787 * SubP(P, ConvIs(Iu)), SubP(P, ConvIu(Is)).
1788 * If possible, remove the Conv's.
1790 static ir_node *transform_node_AddSub(ir_node *n) {
1791 ir_mode *mode = get_irn_mode(n);
1793 if (mode_is_reference(mode)) {
1794 ir_node *left = get_binop_left(n);
1795 ir_node *right = get_binop_right(n);
1796 int ref_bits = get_mode_size_bits(mode);
1798 if (get_irn_op(left) == op_Conv) {
1799 ir_mode *mode = get_irn_mode(left);
1800 int bits = get_mode_size_bits(mode);
1802 if (ref_bits == bits &&
1803 mode_is_int(mode) &&
1804 get_mode_arithmetic(mode) == irma_twos_complement) {
1805 ir_node *pre = get_Conv_op(left);
1806 ir_mode *pre_mode = get_irn_mode(pre);
1808 if (mode_is_int(pre_mode) &&
1809 get_mode_size_bits(pre_mode) == bits &&
1810 get_mode_arithmetic(pre_mode) == irma_twos_complement) {
1811 /* ok, this conv just changes to sign, moreover the calculation
1812 * is done with same number of bits as our address mode, so
1813 * we can ignore the conv as address calculation can be viewed
1814 * as either signed or unsigned
1816 set_binop_left(n, pre);
1821 if (get_irn_op(right) == op_Conv) {
1822 ir_mode *mode = get_irn_mode(right);
1823 int bits = get_mode_size_bits(mode);
1825 if (ref_bits == bits &&
1826 mode_is_int(mode) &&
1827 get_mode_arithmetic(mode) == irma_twos_complement) {
1828 ir_node *pre = get_Conv_op(right);
1829 ir_mode *pre_mode = get_irn_mode(pre);
1831 if (mode_is_int(pre_mode) &&
1832 get_mode_size_bits(pre_mode) == bits &&
1833 get_mode_arithmetic(pre_mode) == irma_twos_complement) {
1834 /* ok, this conv just changes to sign, moreover the calculation
1835 * is done with same number of bits as our address mode, so
1836 * we can ignore the conv as address calculation can be viewed
1837 * as either signed or unsigned
1839 set_binop_right(n, pre);
1845 } /* transform_node_AddSub */
1847 #define HANDLE_BINOP_PHI(op,a,b,c) \
1849 if (is_Const(b) && is_const_Phi(a)) { \
1850 /* check for Op(Phi, Const) */ \
1851 c = apply_binop_on_phi(a, get_Const_tarval(b), op, 0); \
1853 else if (is_Const(a) && is_const_Phi(b)) { \
1854 /* check for Op(Const, Phi) */ \
1855 c = apply_binop_on_phi(b, get_Const_tarval(a), op, 1); \
1858 DBG_OPT_ALGSIM0(oldn, c, FS_OPT_CONST_PHI); \
1862 #define HANDLE_UNOP_PHI(op,a,c) \
1864 if (is_const_Phi(a)) { \
1865 /* check for Op(Phi) */ \
1866 c = apply_unop_on_phi(a, op); \
1869 DBG_OPT_ALGSIM0(oldn, c, FS_OPT_CONST_PHI); \
1875 * Do the AddSub optimization, then Transform
1876 * Constant folding on Phi
1877 * Add(a,a) -> Mul(a, 2)
1878 * Add(Mul(a, x), a) -> Mul(a, x+1)
1879 * if the mode is integer or float.
1880 * Transform Add(a,-b) into Sub(a,b).
1881 * Reassociation might fold this further.
1883 static ir_node *transform_node_Add(ir_node *n) {
1885 ir_node *a, *b, *c, *oldn = n;
1887 n = transform_node_AddSub(n);
1889 a = get_Add_left(n);
1890 b = get_Add_right(n);
1892 HANDLE_BINOP_PHI(tarval_add, a,b,c);
1894 mode = get_irn_mode(n);
1896 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
1897 if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
1900 if (mode_is_num(mode)) {
1902 ir_node *block = get_irn_n(n, -1);
1905 get_irn_dbg_info(n),
1909 new_r_Const_long(current_ir_graph, block, mode, 2),
1911 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_A);
1912 } else if (get_irn_op(a) == op_Minus) {
1914 get_irn_dbg_info(n),
1920 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_MINUS_B);
1921 } else if (get_irn_op(b) == op_Minus) {
1923 get_irn_dbg_info(n),
1929 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_MINUS_B);
1931 /* do NOT execute this code if reassociation is enabled, it does the inverse! */
1932 else if (!get_opt_reassociation() && get_irn_op(a) == op_Mul) {
1933 ir_node *ma = get_Mul_left(a);
1934 ir_node *mb = get_Mul_right(a);
1937 ir_node *blk = get_irn_n(n, -1);
1939 get_irn_dbg_info(n), current_ir_graph, blk,
1942 get_irn_dbg_info(n), current_ir_graph, blk,
1944 new_r_Const_long(current_ir_graph, blk, mode, 1),
1947 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_MUL_A_X_A);
1948 } else if (b == mb) {
1949 ir_node *blk = get_irn_n(n, -1);
1951 get_irn_dbg_info(n), current_ir_graph, blk,
1954 get_irn_dbg_info(n), current_ir_graph, blk,
1956 new_r_Const_long(current_ir_graph, blk, mode, 1),
1959 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_MUL_A_X_A);
1962 /* do NOT execute this code if reassociation is enabled, it does the inverse! */
1963 else if (!get_opt_reassociation() && get_irn_op(b) == op_Mul) {
1964 ir_node *ma = get_Mul_left(b);
1965 ir_node *mb = get_Mul_right(b);
1968 ir_node *blk = get_irn_n(n, -1);
1970 get_irn_dbg_info(n), current_ir_graph, blk,
1973 get_irn_dbg_info(n), current_ir_graph, blk,
1975 new_r_Const_long(current_ir_graph, blk, mode, 1),
1978 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_MUL_A_X_A);
1979 } else if (a == mb) {
1980 ir_node *blk = get_irn_n(n, -1);
1982 get_irn_dbg_info(n), current_ir_graph, blk,
1985 get_irn_dbg_info(n), current_ir_graph, blk,
1987 new_r_Const_long(current_ir_graph, blk, mode, 1),
1990 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_MUL_A_X_A);
1995 } /* transform_node_Add */
1998 * Do the AddSub optimization, then Transform
1999 * Constant folding on Phi
2000 * Sub(0,a) -> Minus(a)
2001 * Sub(Mul(a, x), a) -> Mul(a, x-1)
2002 * Sub(Sub(x, y), b) -> Sub(x, Add(y,b))
2004 static ir_node *transform_node_Sub(ir_node *n) {
2009 n = transform_node_AddSub(n);
2011 a = get_Sub_left(n);
2012 b = get_Sub_right(n);
2014 HANDLE_BINOP_PHI(tarval_sub, a,b,c);
2016 mode = get_irn_mode(n);
2018 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
2019 if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
2022 /* Beware of Sub(P, P) which cannot be optimized into a simple Minus ... */
2023 if (mode_is_num(mode) && mode == get_irn_mode(a) && (classify_Const(a) == CNST_NULL)) {
2025 get_irn_dbg_info(n),
2030 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_0_A);
2032 /* do NOT execute this code if reassociation is enabled, it does the inverse! */
2033 else if (get_opt_reassociation() && get_irn_op(a) == op_Mul) {
2034 ir_node *ma = get_Mul_left(a);
2035 ir_node *mb = get_Mul_right(a);
2038 ir_node *blk = get_irn_n(n, -1);
2040 get_irn_dbg_info(n),
2041 current_ir_graph, blk,
2044 get_irn_dbg_info(n),
2045 current_ir_graph, blk,
2047 new_r_Const_long(current_ir_graph, blk, mode, 1),
2050 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MUL_A_X_A);
2051 } else if (mb == b) {
2052 ir_node *blk = get_irn_n(n, -1);
2054 get_irn_dbg_info(n),
2055 current_ir_graph, blk,
2058 get_irn_dbg_info(n),
2059 current_ir_graph, blk,
2061 new_r_Const_long(current_ir_graph, blk, mode, 1),
2064 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MUL_A_X_A);
2066 } else if (get_irn_op(a) == op_Sub) {
2067 ir_node *x = get_Sub_left(a);
2068 ir_node *y = get_Sub_right(a);
2069 ir_node *blk = get_irn_n(n, -1);
2070 ir_mode *m_b = get_irn_mode(b);
2071 ir_mode *m_y = get_irn_mode(y);
2074 /* Determine the right mode for the Add. */
2077 else if (mode_is_reference(m_b))
2079 else if (mode_is_reference(m_y))
2083 * Both modes are different but none is reference,
2084 * happens for instance in SubP(SubP(P, Iu), Is).
2085 * We have two possibilities here: Cast or ignore.
2086 * Currently we ignore this case.
2091 add = new_r_Add(current_ir_graph, blk, y, b, mode);
2094 set_Sub_right(n, add);
2095 DBG_OPT_ALGSIM0(n, n, FS_OPT_SUB_SUB_X_Y_Z);
2099 } /* transform_node_Sub */
2102 * Transform Mul(a,-1) into -a.
2103 * Do constant evaluation of Phi nodes.
2104 * Do architecture dependent optimizations on Mul nodes
2106 static ir_node *transform_node_Mul(ir_node *n) {
2107 ir_node *c, *oldn = n;
2108 ir_node *a = get_Mul_left(n);
2109 ir_node *b = get_Mul_right(n);
2112 HANDLE_BINOP_PHI(tarval_mul, a,b,c);
2114 mode = get_irn_mode(n);
2115 if (mode_is_signed(mode)) {
2118 if (value_of(a) == get_mode_minus_one(mode))
2120 else if (value_of(b) == get_mode_minus_one(mode))
2123 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1), r, mode);
2124 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2128 return arch_dep_replace_mul_with_shifts(n);
2129 } /* transform_node_Mul */
2132 * Transform a Div Node.
2134 static ir_node *transform_node_Div(ir_node *n) {
2135 tarval *tv = value_of(n);
2138 /* BEWARE: it is NOT possible to optimize a/a to 1, as this may cause a exception */
2140 if (tv != tarval_bad) {
2141 value = new_Const(get_tarval_mode(tv), tv);
2143 DBG_OPT_CSTEVAL(n, value);
2144 } else /* Try architecture dependent optimization */
2145 value = arch_dep_replace_div_by_const(n);
2148 /* Turn Div into a tuple (mem, bad, value) */
2149 ir_node *mem = get_Div_mem(n);
2150 ir_node *blk = get_nodes_block(n);
2152 turn_into_tuple(n, pn_Div_max);
2153 set_Tuple_pred(n, pn_Div_M, mem);
2154 set_Tuple_pred(n, pn_Div_X_regular, new_r_Jmp(current_ir_graph, blk));
2155 set_Tuple_pred(n, pn_Div_X_except, new_Bad());
2156 set_Tuple_pred(n, pn_Div_res, value);
2159 } /* transform_node_Div */
2162 * Transform a Mod node.
2164 static ir_node *transform_node_Mod(ir_node *n) {
2165 tarval *tv = value_of(n);
2168 /* BEWARE: it is NOT possible to optimize a%a to 0, as this may cause a exception */
2170 if (tv != tarval_bad) {
2171 value = new_Const(get_tarval_mode(tv), tv);
2173 DBG_OPT_CSTEVAL(n, value);
2174 } else /* Try architecture dependent optimization */
2175 value = arch_dep_replace_mod_by_const(n);
2178 /* Turn Mod into a tuple (mem, bad, value) */
2179 ir_node *mem = get_Mod_mem(n);
2180 ir_node *blk = get_nodes_block(n);
2182 turn_into_tuple(n, pn_Mod_max);
2183 set_Tuple_pred(n, pn_Mod_M, mem);
2184 set_Tuple_pred(n, pn_Mod_X_regular, new_r_Jmp(current_ir_graph, blk));
2185 set_Tuple_pred(n, pn_Mod_X_except, new_Bad());
2186 set_Tuple_pred(n, pn_Mod_res, value);
2189 } /* transform_node_Mod */
2192 * Transform a DivMod node.
2194 static ir_node *transform_node_DivMod(ir_node *n) {
2197 ir_node *a = get_DivMod_left(n);
2198 ir_node *b = get_DivMod_right(n);
2199 ir_mode *mode = get_irn_mode(a);
2200 tarval *ta = value_of(a);
2201 tarval *tb = value_of(b);
2203 if (!(mode_is_int(mode) && mode_is_int(get_irn_mode(b))))
2206 /* BEWARE: it is NOT possible to optimize a/a to 1, as this may cause a exception */
2208 if (tb != tarval_bad) {
2209 if (tb == get_mode_one(get_tarval_mode(tb))) {
2210 b = new_Const (mode, get_mode_null(mode));
2213 DBG_OPT_CSTEVAL(n, b);
2214 } else if (ta != tarval_bad) {
2215 tarval *resa, *resb;
2216 resa = tarval_div (ta, tb);
2217 if (resa == tarval_bad) return n; /* Causes exception!!! Model by replacing through
2218 Jmp for X result!? */
2219 resb = tarval_mod (ta, tb);
2220 if (resb == tarval_bad) return n; /* Causes exception! */
2221 a = new_Const (mode, resa);
2222 b = new_Const (mode, resb);
2225 DBG_OPT_CSTEVAL(n, a);
2226 DBG_OPT_CSTEVAL(n, b);
2227 } else { /* Try architecture dependent optimization */
2228 arch_dep_replace_divmod_by_const(&a, &b, n);
2229 evaluated = a != NULL;
2231 } else if (ta == get_mode_null(mode)) {
2232 /* 0 / non-Const = 0 */
2237 if (evaluated) { /* replace by tuple */
2238 ir_node *mem = get_DivMod_mem(n);
2239 ir_node *blk = get_nodes_block(n);
2240 turn_into_tuple(n, pn_DivMod_max);
2241 set_Tuple_pred(n, pn_DivMod_M, mem);
2242 set_Tuple_pred(n, pn_DivMod_X_regular, new_r_Jmp(current_ir_graph, blk));
2243 set_Tuple_pred(n, pn_DivMod_X_except, new_Bad()); /* no exception */
2244 set_Tuple_pred(n, pn_DivMod_res_div, a);
2245 set_Tuple_pred(n, pn_DivMod_res_mod, b);
2249 } /* transform_node_DivMod */
2252 * Optimize Abs(x) into x if x is Confirmed >= 0
2253 * Optimize Abs(x) into -x if x is Confirmed <= 0
2255 static ir_node *transform_node_Abs(ir_node *n) {
2257 ir_node *a = get_Abs_op(n);
2258 value_classify_sign sign = classify_value_sign(a);
2260 if (sign == value_classified_negative) {
2261 ir_mode *mode = get_irn_mode(n);
2264 * We can replace the Abs by -x here.
2265 * We even could add a new Confirm here.
2267 * Note that -x would create a new node, so we could
2268 * not run it in the equivalent_node() context.
2270 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph,
2271 get_irn_n(n, -1), a, mode);
2273 DBG_OPT_CONFIRM(oldn, n);
2274 } else if (sign == value_classified_positive) {
2275 /* n is positive, Abs is not needed */
2278 DBG_OPT_CONFIRM(oldn, n);
2282 } /* transform_node_Abs */
2285 * Transform a Cond node.
2287 * Replace the Cond by a Jmp if it branches on a constant
2290 static ir_node *transform_node_Cond(ir_node *n) {
2293 ir_node *a = get_Cond_selector(n);
2294 tarval *ta = value_of(a);
2296 /* we need block info which is not available in floating irgs */
2297 if (get_irg_pinned(current_ir_graph) == op_pin_state_floats)
2300 if ((ta != tarval_bad) &&
2301 (get_irn_mode(a) == mode_b) &&
2302 (get_opt_unreachable_code())) {
2303 /* It's a boolean Cond, branching on a boolean constant.
2304 Replace it by a tuple (Bad, Jmp) or (Jmp, Bad) */
2305 jmp = new_r_Jmp(current_ir_graph, get_nodes_block(n));
2306 turn_into_tuple(n, pn_Cond_max);
2307 if (ta == tarval_b_true) {
2308 set_Tuple_pred(n, pn_Cond_false, new_Bad());
2309 set_Tuple_pred(n, pn_Cond_true, jmp);
2311 set_Tuple_pred(n, pn_Cond_false, jmp);
2312 set_Tuple_pred(n, pn_Cond_true, new_Bad());
2314 /* We might generate an endless loop, so keep it alive. */
2315 add_End_keepalive(get_irg_end(current_ir_graph), get_nodes_block(n));
2318 } /* transform_node_Cond */
2323 static ir_node *transform_node_And(ir_node *n) {
2324 ir_node *c, *oldn = n;
2325 ir_node *a = get_And_left(n);
2326 ir_node *b = get_And_right(n);
2328 HANDLE_BINOP_PHI(tarval_and, a,b,c);
2330 } /* transform_node_And */
2335 static ir_node *transform_node_Eor(ir_node *n) {
2336 ir_node *c, *oldn = n;
2337 ir_node *a = get_Eor_left(n);
2338 ir_node *b = get_Eor_right(n);
2339 ir_mode *mode = get_irn_mode(n);
2341 HANDLE_BINOP_PHI(tarval_eor, a,b,c);
2345 n = new_rd_Const(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1),
2346 mode, get_mode_null(mode));
2347 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_A_A);
2348 } else if ((mode == mode_b)
2349 && (get_irn_op(a) == op_Proj)
2350 && (get_irn_mode(a) == mode_b)
2351 && (classify_tarval (value_of(b)) == TV_CLASSIFY_ONE)
2352 && (get_irn_op(get_Proj_pred(a)) == op_Cmp)) {
2353 /* The Eor negates a Cmp. The Cmp has the negated result anyways! */
2354 n = new_r_Proj(current_ir_graph, get_irn_n(n, -1), get_Proj_pred(a),
2355 mode_b, get_negated_pnc(get_Proj_proj(a), mode));
2357 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT_BOOL);
2358 } else if ((mode == mode_b)
2359 && (classify_tarval (value_of(b)) == TV_CLASSIFY_ONE)) {
2360 /* The Eor is a Not. Replace it by a Not. */
2361 /* ????!!!Extend to bitfield 1111111. */
2362 n = new_r_Not(current_ir_graph, get_irn_n(n, -1), a, mode_b);
2364 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
2368 } /* transform_node_Eor */
2373 static ir_node *transform_node_Not(ir_node *n) {
2374 ir_node *c, *oldn = n;
2375 ir_node *a = get_Not_op(n);
2377 HANDLE_UNOP_PHI(tarval_not,a,c);
2379 /* check for a boolean Not */
2380 if ( (get_irn_mode(n) == mode_b)
2381 && (get_irn_op(a) == op_Proj)
2382 && (get_irn_mode(a) == mode_b)
2383 && (get_irn_op(get_Proj_pred(a)) == op_Cmp)) {
2384 /* We negate a Cmp. The Cmp has the negated result anyways! */
2385 n = new_r_Proj(current_ir_graph, get_irn_n(n, -1), get_Proj_pred(a),
2386 mode_b, get_negated_pnc(get_Proj_proj(a), mode_b));
2387 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_CMP);
2390 } /* transform_node_Not */
2393 * Transform a Minus.
2395 static ir_node *transform_node_Minus(ir_node *n) {
2396 ir_node *c, *oldn = n;
2397 ir_node *a = get_Minus_op(n);
2399 HANDLE_UNOP_PHI(tarval_neg,a,c);
2401 } /* transform_node_Minus */
2404 * Transform a Cast_type(Const) into a new Const_type
2406 static ir_node *transform_node_Cast(ir_node *n) {
2408 ir_node *pred = get_Cast_op(n);
2409 ir_type *tp = get_irn_type(n);
2411 if (get_irn_op(pred) == op_Const && get_Const_type(pred) != tp) {
2412 n = new_rd_Const_type(NULL, current_ir_graph, get_irn_n(pred, -1), get_irn_mode(pred),
2413 get_Const_tarval(pred), tp);
2414 DBG_OPT_CSTEVAL(oldn, n);
2415 } else if ((get_irn_op(pred) == op_SymConst) && (get_SymConst_value_type(pred) != tp)) {
2416 n = new_rd_SymConst_type(NULL, current_ir_graph, get_irn_n(pred, -1), get_SymConst_symbol(pred),
2417 get_SymConst_kind(pred), tp);
2418 DBG_OPT_CSTEVAL(oldn, n);
2422 } /* transform_node_Cast */
2425 * Transform a Proj(Div) with a non-zero value.
2426 * Removes the exceptions and routes the memory to the NoMem node.
2428 static ir_node *transform_node_Proj_Div(ir_node *proj) {
2429 ir_node *n = get_Proj_pred(proj);
2430 ir_node *b = get_Div_right(n);
2434 if (value_not_zero(b, &confirm)) {
2435 /* div(x, y) && y != 0 */
2436 proj_nr = get_Proj_proj(proj);
2437 if (proj_nr == pn_Div_X_except) {
2438 /* we found an exception handler, remove it */
2439 DBG_OPT_EXC_REM(proj);
2441 } else if (proj_nr == pn_Div_M) {
2442 ir_node *res = get_Div_mem(n);
2443 ir_node *new_mem = get_irg_no_mem(current_ir_graph);
2446 /* This node can only float up to the Confirm block */
2447 new_mem = new_r_Pin(current_ir_graph, get_nodes_block(confirm), new_mem);
2449 set_irn_pinned(n, op_pin_state_floats);
2450 /* this is a Div without exception, we can remove the memory edge */
2451 set_Div_mem(n, new_mem);
2456 } /* transform_node_Proj_Div */
2459 * Transform a Proj(Mod) with a non-zero value.
2460 * Removes the exceptions and routes the memory to the NoMem node.
2462 static ir_node *transform_node_Proj_Mod(ir_node *proj) {
2463 ir_node *n = get_Proj_pred(proj);
2464 ir_node *b = get_Mod_right(n);
2468 if (value_not_zero(b, &confirm)) {
2469 /* mod(x, y) && y != 0 */
2470 proj_nr = get_Proj_proj(proj);
2472 if (proj_nr == pn_Mod_X_except) {
2473 /* we found an exception handler, remove it */
2474 DBG_OPT_EXC_REM(proj);
2476 } else if (proj_nr == pn_Mod_M) {
2477 ir_node *res = get_Mod_mem(n);
2478 ir_node *new_mem = get_irg_no_mem(current_ir_graph);
2481 /* This node can only float up to the Confirm block */
2482 new_mem = new_r_Pin(current_ir_graph, get_nodes_block(confirm), new_mem);
2484 set_irn_pinned(n, op_pin_state_floats);
2485 /* this is a Mod without exception, we can remove the memory edge */
2486 set_Mod_mem(n, get_irg_no_mem(current_ir_graph));
2488 } else if (proj_nr == pn_Mod_res && get_Mod_left(n) == b) {
2489 /* a % a = 0 if a != 0 */
2490 ir_mode *mode = get_irn_mode(proj);
2491 ir_node *res = new_Const(mode, get_mode_null(mode));
2493 DBG_OPT_CSTEVAL(n, res);
2498 } /* transform_node_Proj_Mod */
2501 * Transform a Proj(DivMod) with a non-zero value.
2502 * Removes the exceptions and routes the memory to the NoMem node.
2504 static ir_node *transform_node_Proj_DivMod(ir_node *proj) {
2505 ir_node *n = get_Proj_pred(proj);
2506 ir_node *b = get_DivMod_right(n);
2510 if (value_not_zero(b, &confirm)) {
2511 /* DivMod(x, y) && y != 0 */
2512 proj_nr = get_Proj_proj(proj);
2514 if (proj_nr == pn_DivMod_X_except) {
2515 /* we found an exception handler, remove it */
2516 DBG_OPT_EXC_REM(proj);
2518 } else if (proj_nr == pn_DivMod_M) {
2519 ir_node *res = get_DivMod_mem(n);
2520 ir_node *new_mem = get_irg_no_mem(current_ir_graph);
2523 /* This node can only float up to the Confirm block */
2524 new_mem = new_r_Pin(current_ir_graph, get_nodes_block(confirm), new_mem);
2526 set_irn_pinned(n, op_pin_state_floats);
2527 /* this is a DivMod without exception, we can remove the memory edge */
2528 set_DivMod_mem(n, get_irg_no_mem(current_ir_graph));
2530 } else if (proj_nr == pn_DivMod_res_mod && get_DivMod_left(n) == b) {
2531 /* a % a = 0 if a != 0 */
2532 ir_mode *mode = get_irn_mode(proj);
2533 ir_node *res = new_Const(mode, get_mode_null(mode));
2535 DBG_OPT_CSTEVAL(n, res);
2540 } /* transform_node_Proj_DivMod */
2543 * Optimizes jump tables (CondIs or CondIu) by removing all impossible cases.
2545 static ir_node *transform_node_Proj_Cond(ir_node *proj) {
2546 if (get_opt_unreachable_code()) {
2547 ir_node *n = get_Proj_pred(proj);
2548 ir_node *b = get_Cond_selector(n);
2550 if (mode_is_int(get_irn_mode(b))) {
2551 tarval *tb = value_of(b);
2553 if (tb != tarval_bad) {
2554 /* we have a constant switch */
2555 long num = get_Proj_proj(proj);
2557 if (num != get_Cond_defaultProj(n)) { /* we cannot optimize default Proj's yet */
2558 if (get_tarval_long(tb) == num) {
2559 /* Do NOT create a jump here, or we will have 2 control flow ops
2560 * in a block. This case is optimized away in optimize_cf(). */
2563 /* this case will NEVER be taken, kill it */
2571 } /* transform_node_Proj_Cond */
2574 * Normalizes and optimizes Cmp nodes.
2576 static ir_node *transform_node_Proj_Cmp(ir_node *proj) {
2577 if (get_opt_reassociation()) {
2578 ir_node *n = get_Proj_pred(proj);
2579 ir_node *left = get_Cmp_left(n);
2580 ir_node *right = get_Cmp_right(n);
2584 ir_mode *mode = NULL;
2585 long proj_nr = get_Proj_proj(proj);
2588 * First step: normalize the compare op
2589 * by placing the constant on the right site
2590 * or moving the lower address node to the left.
2591 * We ignore the case that both are constants
2592 * this case should be optimized away.
2594 if (get_irn_op(right) == op_Const) {
2596 } else if (get_irn_op(left) == op_Const) {
2601 proj_nr = get_inversed_pnc(proj_nr);
2603 } else if (get_irn_idx(left) > get_irn_idx(right)) {
2609 proj_nr = get_inversed_pnc(proj_nr);
2614 * Second step: Try to reduce the magnitude
2615 * of a constant. This may help to generate better code
2616 * later and may help to normalize more compares.
2617 * Of course this is only possible for integer values.
2620 mode = get_irn_mode(c);
2621 tv = get_Const_tarval(c);
2623 if (tv != tarval_bad) {
2624 /* the following optimization is possible on modes without Overflow
2625 * on Unary Minus or on == and !=:
2626 * -a CMP c ==> a swap(CMP) -c
2628 * Beware: for two-complement Overflow may occur, so only == and != can
2629 * be optimized, see this:
2630 * -MININT < 0 =/=> MININT > 0 !!!
2632 if (get_opt_constant_folding() && get_irn_op(left) == op_Minus &&
2633 (!mode_overflow_on_unary_Minus(mode) ||
2634 (mode_is_int(mode) && (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg)))) {
2635 left = get_Minus_op(left);
2636 tv = tarval_sub(get_mode_null(mode), tv);
2638 proj_nr = get_inversed_pnc(proj_nr);
2642 /* for integer modes, we have more */
2643 if (mode_is_int(mode)) {
2644 /* Ne includes Unordered which is not possible on integers.
2645 * However, frontends often use this wrong, so fix it here */
2646 if (proj_nr & pn_Cmp_Uo) {
2647 proj_nr &= ~pn_Cmp_Uo;
2648 set_Proj_proj(proj, proj_nr);
2651 /* c > 0 : a < c ==> a <= (c-1) a >= c ==> a > (c-1) */
2652 if ((proj_nr == pn_Cmp_Lt || proj_nr == pn_Cmp_Ge) &&
2653 tarval_cmp(tv, get_mode_null(mode)) == pn_Cmp_Gt) {
2654 tv = tarval_sub(tv, get_mode_one(mode));
2656 proj_nr ^= pn_Cmp_Eq;
2659 /* c < 0 : a > c ==> a >= (c+1) a <= c ==> a < (c+1) */
2660 else if ((proj_nr == pn_Cmp_Gt || proj_nr == pn_Cmp_Le) &&
2661 tarval_cmp(tv, get_mode_null(mode)) == pn_Cmp_Lt) {
2662 tv = tarval_add(tv, get_mode_one(mode));
2664 proj_nr ^= pn_Cmp_Eq;
2668 /* the following reassociations work only for == and != */
2669 if (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) {
2671 /* a-b == 0 ==> a == b, a-b != 0 ==> a != b */
2672 if (classify_tarval(tv) == TV_CLASSIFY_NULL && get_irn_op(left) == op_Sub) {
2673 right = get_Sub_right(left);
2674 left = get_Sub_left(left);
2676 tv = value_of(right);
2680 if (tv != tarval_bad) {
2681 ir_op *op = get_irn_op(left);
2683 /* a-c1 == c2 ==> a == c2+c1, a-c1 != c2 ==> a != c2+c1 */
2685 ir_node *c1 = get_Sub_right(left);
2686 tarval *tv2 = value_of(c1);
2688 if (tv2 != tarval_bad) {
2689 tv2 = tarval_add(tv, value_of(c1));
2691 if (tv2 != tarval_bad) {
2692 left = get_Sub_left(left);
2698 /* a+c1 == c2 ==> a == c2-c1, a+c1 != c2 ==> a != c2-c1 */
2699 else if (op == op_Add) {
2700 ir_node *a_l = get_Add_left(left);
2701 ir_node *a_r = get_Add_right(left);
2705 if (get_irn_op(a_l) == op_Const) {
2707 tv2 = value_of(a_l);
2710 tv2 = value_of(a_r);
2713 if (tv2 != tarval_bad) {
2714 tv2 = tarval_sub(tv, tv2);
2716 if (tv2 != tarval_bad) {
2723 /* -a == c ==> a == -c, -a != c ==> a != -c */
2724 else if (op == op_Minus) {
2725 tarval *tv2 = tarval_sub(get_mode_null(mode), tv);
2727 if (tv2 != tarval_bad) {
2728 left = get_Minus_op(left);
2735 /* the following reassociations work only for <= */
2736 else if (proj_nr == pn_Cmp_Le || proj_nr == pn_Cmp_Lt) {
2737 if (tv != tarval_bad) {
2738 ir_op *op = get_irn_op(left);
2740 /* c >= 0 : Abs(a) <= c ==> (unsigned)(a + c) <= 2*c */
2748 * optimization for AND:
2750 * And(x, C) == C ==> And(x, C) != 0
2751 * And(x, C) != C ==> And(X, C) == 0
2753 * if C is a single Bit constant.
2755 if ((proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) &&
2756 (get_irn_op(left) == op_And)) {
2757 if (is_single_bit_tarval(tv)) {
2758 /* check for Constant's match. We have check hare the tarvals,
2759 because our const might be changed */
2760 ir_node *la = get_And_left(left);
2761 ir_node *ra = get_And_right(left);
2762 if ((is_Const(la) && get_Const_tarval(la) == tv) ||
2763 (is_Const(ra) && get_Const_tarval(ra) == tv)) {
2764 /* fine: do the transformation */
2765 tv = get_mode_null(get_tarval_mode(tv));
2766 proj_nr ^= pn_Cmp_Leg;
2771 } /* tarval != bad */
2775 ir_node *block = get_irn_n(n, -1); /* Beware of get_nodes_Block() */
2777 if (changed & 2) /* need a new Const */
2778 right = new_Const(mode, tv);
2780 /* create a new compare */
2781 n = new_rd_Cmp(get_irn_dbg_info(n), current_ir_graph, block,
2784 set_Proj_pred(proj, n);
2785 set_Proj_proj(proj, proj_nr);
2789 } /* transform_node_Proj_Cmp */
2792 * Does all optimizations on nodes that must be done on it's Proj's
2793 * because of creating new nodes.
2795 static ir_node *transform_node_Proj(ir_node *proj) {
2796 ir_node *n = get_Proj_pred(proj);
2798 switch (get_irn_opcode(n)) {
2800 return transform_node_Proj_Div(proj);
2803 return transform_node_Proj_Mod(proj);
2806 return transform_node_Proj_DivMod(proj);
2809 return transform_node_Proj_Cond(proj);
2812 return transform_node_Proj_Cmp(proj);
2815 /* should not happen, but if it does will be optimized away */
2816 return equivalent_node_Proj(proj);
2822 } /* transform_node_Proj */
2825 * Move Confirms down through Phi nodes.
2827 static ir_node *transform_node_Phi(ir_node *phi) {
2829 ir_mode *mode = get_irn_mode(phi);
2831 if (mode_is_reference(mode)) {
2832 n = get_irn_arity(phi);
2834 /* Beware of Phi0 */
2836 ir_node *pred = get_irn_n(phi, 0);
2837 ir_node *bound, *new_Phi, *block, **in;
2840 if (! is_Confirm(pred))
2843 bound = get_Confirm_bound(pred);
2844 pnc = get_Confirm_cmp(pred);
2846 NEW_ARR_A(ir_node *, in, n);
2847 in[0] = get_Confirm_value(pred);
2849 for (i = 1; i < n; ++i) {
2850 pred = get_irn_n(phi, i);
2852 if (! is_Confirm(pred) ||
2853 get_Confirm_bound(pred) != bound ||
2854 get_Confirm_cmp(pred) != pnc)
2856 in[i] = get_Confirm_value(pred);
2858 /* move the Confirm nodes "behind" the Phi */
2859 block = get_irn_n(phi, -1);
2860 new_Phi = new_r_Phi(current_ir_graph, block, n, in, get_irn_mode(phi));
2861 return new_r_Confirm(current_ir_graph, block, new_Phi, bound, pnc);
2865 } /* transform_node_Phi */
2868 * Returns the operands of a commutative bin-op, if one operand is
2869 * a const, it is returned as the second one.
2871 static void get_comm_Binop_Ops(ir_node *binop, ir_node **a, ir_node **c) {
2872 ir_node *op_a = get_binop_left(binop);
2873 ir_node *op_b = get_binop_right(binop);
2875 assert(is_op_commutative(get_irn_op(binop)));
2877 if (get_irn_op(op_a) == op_Const) {
2884 } /* get_comm_Binop_Ops */
2887 * Optimize a Or(And(Or(And(v,c4),c3),c2),c1) pattern if possible.
2888 * Such pattern may arise in bitfield stores.
2890 * value c4 value c4 & c2
2891 * AND c3 AND c1 | c3
2896 static ir_node *transform_node_Or_bf_store(ir_node *or) {
2899 ir_node *and_l, *c3;
2900 ir_node *value, *c4;
2901 ir_node *new_and, *new_const, *block;
2902 ir_mode *mode = get_irn_mode(or);
2904 tarval *tv1, *tv2, *tv3, *tv4, *tv, *n_tv4, *n_tv2;
2906 get_comm_Binop_Ops(or, &and, &c1);
2907 if ((get_irn_op(c1) != op_Const) || (get_irn_op(and) != op_And))
2910 get_comm_Binop_Ops(and, &or_l, &c2);
2911 if ((get_irn_op(c2) != op_Const) || (get_irn_op(or_l) != op_Or))
2914 get_comm_Binop_Ops(or_l, &and_l, &c3);
2915 if ((get_irn_op(c3) != op_Const) || (get_irn_op(and_l) != op_And))
2918 get_comm_Binop_Ops(and_l, &value, &c4);
2919 if (get_irn_op(c4) != op_Const)
2922 /* ok, found the pattern, check for conditions */
2923 assert(mode == get_irn_mode(and));
2924 assert(mode == get_irn_mode(or_l));
2925 assert(mode == get_irn_mode(and_l));
2927 tv1 = get_Const_tarval(c1);
2928 tv2 = get_Const_tarval(c2);
2929 tv3 = get_Const_tarval(c3);
2930 tv4 = get_Const_tarval(c4);
2932 tv = tarval_or(tv4, tv2);
2933 if (classify_tarval(tv) != TV_CLASSIFY_ALL_ONE) {
2934 /* have at least one 0 at the same bit position */
2938 n_tv4 = tarval_not(tv4);
2939 if (tv3 != tarval_and(tv3, n_tv4)) {
2940 /* bit in the or_mask is outside the and_mask */
2944 n_tv2 = tarval_not(tv2);
2945 if (tv1 != tarval_and(tv1, n_tv2)) {
2946 /* bit in the or_mask is outside the and_mask */
2950 /* ok, all conditions met */
2951 block = get_irn_n(or, -1);
2953 new_and = new_r_And(current_ir_graph, block,
2954 value, new_r_Const(current_ir_graph, block, mode, tarval_and(tv4, tv2)), mode);
2956 new_const = new_r_Const(current_ir_graph, block, mode, tarval_or(tv3, tv1));
2958 set_Or_left(or, new_and);
2959 set_Or_right(or, new_const);
2961 /* check for more */
2962 return transform_node_Or_bf_store(or);
2963 } /* transform_node_Or_bf_store */
2966 * Optimize an Or(shl(x, c), shr(x, bits - c)) into a Rot
2968 static ir_node *transform_node_Or_Rot(ir_node *or) {
2969 ir_mode *mode = get_irn_mode(or);
2970 ir_node *shl, *shr, *block;
2971 ir_node *irn, *x, *c1, *c2, *v, *sub, *n;
2974 if (! mode_is_int(mode))
2977 shl = get_binop_left(or);
2978 shr = get_binop_right(or);
2980 if (get_irn_op(shl) == op_Shr) {
2981 if (get_irn_op(shr) != op_Shl)
2987 } else if (get_irn_op(shl) != op_Shl) {
2989 } else if (get_irn_op(shr) != op_Shr) {
2992 x = get_Shl_left(shl);
2993 if (x != get_Shr_left(shr))
2996 c1 = get_Shl_right(shl);
2997 c2 = get_Shr_right(shr);
2998 if (get_irn_op(c1) == op_Const && get_irn_op(c2) == op_Const) {
2999 tv1 = get_Const_tarval(c1);
3000 if (! tarval_is_long(tv1))
3003 tv2 = get_Const_tarval(c2);
3004 if (! tarval_is_long(tv2))
3007 if (get_tarval_long(tv1) + get_tarval_long(tv2)
3008 != get_mode_size_bits(mode))
3011 /* yet, condition met */
3012 block = get_irn_n(or, -1);
3014 n = new_r_Rot(current_ir_graph, block, x, c1, mode);
3016 DBG_OPT_ALGSIM1(or, shl, shr, n, FS_OPT_OR_SHFT_TO_ROT);
3018 } else if (get_irn_op(c1) == op_Sub) {
3022 if (get_Sub_right(sub) != v)
3025 c1 = get_Sub_left(sub);
3026 if (get_irn_op(c1) != op_Const)
3029 tv1 = get_Const_tarval(c1);
3030 if (! tarval_is_long(tv1))
3033 if (get_tarval_long(tv1) != get_mode_size_bits(mode))
3036 /* yet, condition met */
3037 block = get_nodes_block(or);
3039 /* a Rot right is not supported, so use a rot left */
3040 n = new_r_Rot(current_ir_graph, block, x, sub, mode);
3042 DBG_OPT_ALGSIM0(or, n, FS_OPT_OR_SHFT_TO_ROT);
3044 } else if (get_irn_op(c2) == op_Sub) {
3048 c1 = get_Sub_left(sub);
3049 if (get_irn_op(c1) != op_Const)
3052 tv1 = get_Const_tarval(c1);
3053 if (! tarval_is_long(tv1))
3056 if (get_tarval_long(tv1) != get_mode_size_bits(mode))
3059 /* yet, condition met */
3060 block = get_irn_n(or, -1);
3063 n = new_r_Rot(current_ir_graph, block, x, v, mode);
3065 DBG_OPT_ALGSIM0(or, n, FS_OPT_OR_SHFT_TO_ROT);
3070 } /* transform_node_Or_Rot */
3075 static ir_node *transform_node_Or(ir_node *n) {
3076 ir_node *c, *oldn = n;
3077 ir_node *a = get_Or_left(n);
3078 ir_node *b = get_Or_right(n);
3080 HANDLE_BINOP_PHI(tarval_or, a,b,c);
3082 n = transform_node_Or_bf_store(n);
3083 n = transform_node_Or_Rot(n);
3086 } /* transform_node_Or */
3090 static ir_node *transform_node(ir_node *n);
3093 * Optimize (a >> c1) >> c2), works for Shr, Shrs, Shl.
3095 * Should be moved to reassociation?
3097 static ir_node *transform_node_shift(ir_node *n) {
3098 ir_node *left, *right;
3099 tarval *tv1, *tv2, *res;
3101 int modulo_shf, flag;
3103 left = get_binop_left(n);
3105 /* different operations */
3106 if (get_irn_op(left) != get_irn_op(n))
3109 right = get_binop_right(n);
3110 tv1 = value_of(right);
3111 if (tv1 == tarval_bad)
3114 tv2 = value_of(get_binop_right(left));
3115 if (tv2 == tarval_bad)
3118 res = tarval_add(tv1, tv2);
3120 /* beware: a simple replacement works only, if res < modulo shift */
3121 mode = get_irn_mode(n);
3125 modulo_shf = get_mode_modulo_shift(mode);
3126 if (modulo_shf > 0) {
3127 tarval *modulo = new_tarval_from_long(modulo_shf, get_tarval_mode(res));
3129 if (tarval_cmp(res, modulo) & pn_Cmp_Lt)
3135 /* ok, we can replace it */
3136 ir_node *in[2], *irn, *block = get_irn_n(n, -1);
3138 in[0] = get_binop_left(left);
3139 in[1] = new_r_Const(current_ir_graph, block, get_tarval_mode(res), res);
3141 irn = new_ir_node(NULL, current_ir_graph, block, get_irn_op(n), mode, 2, in);
3143 DBG_OPT_ALGSIM0(n, irn, FS_OPT_REASSOC_SHIFT);
3145 return transform_node(irn);
3148 } /* transform_node_shift */
3153 static ir_node *transform_node_Shr(ir_node *n) {
3154 ir_node *c, *oldn = n;
3155 ir_node *a = get_Shr_left(n);
3156 ir_node *b = get_Shr_right(n);
3158 HANDLE_BINOP_PHI(tarval_shr, a, b, c);
3159 return transform_node_shift(n);
3160 } /* transform_node_Shr */
3165 static ir_node *transform_node_Shrs(ir_node *n) {
3166 ir_node *c, *oldn = n;
3167 ir_node *a = get_Shrs_left(n);
3168 ir_node *b = get_Shrs_right(n);
3170 HANDLE_BINOP_PHI(tarval_shrs, a, b, c);
3171 return transform_node_shift(n);
3172 } /* transform_node_Shrs */
3177 static ir_node *transform_node_Shl(ir_node *n) {
3178 ir_node *c, *oldn = n;
3179 ir_node *a = get_Shl_left(n);
3180 ir_node *b = get_Shl_right(n);
3182 HANDLE_BINOP_PHI(tarval_shl, a, b, c);
3183 return transform_node_shift(n);
3184 } /* transform_node_Shl */
3187 * Remove dead blocks and nodes in dead blocks
3188 * in keep alive list. We do not generate a new End node.
3190 static ir_node *transform_node_End(ir_node *n) {
3191 int i, j, n_keepalives = get_End_n_keepalives(n);
3194 NEW_ARR_A(ir_node *, in, n_keepalives);
3196 for (i = j = 0; i < n_keepalives; ++i) {
3197 ir_node *ka = get_End_keepalive(n, i);
3199 if (! is_Block_dead(ka)) {
3203 } else if (is_irn_pinned_in_irg(ka) && is_Block_dead(get_nodes_block(ka))) {
3206 /* FIXME: beabi need to keep a Proj(M) */
3207 if (is_Phi(ka) || is_irn_keep(ka) || is_Proj(ka))
3210 if (j != n_keepalives)
3211 set_End_keepalives(n, j, in);
3213 } /* transform_node_End */
3216 * Optimize a Mux into some simpler cases.
3218 static ir_node *transform_node_Mux(ir_node *n) {
3219 ir_node *oldn = n, *sel = get_Mux_sel(n);
3220 ir_mode *mode = get_irn_mode(n);
3222 if (get_irn_op(sel) == op_Proj && !mode_honor_signed_zeros(mode)) {
3223 ir_node *cmp = get_Proj_pred(sel);
3224 long proj_nr = get_Proj_proj(sel);
3225 ir_node *f = get_Mux_false(n);
3226 ir_node *t = get_Mux_true(n);
3228 if (get_irn_op(cmp) == op_Cmp && classify_Const(get_Cmp_right(cmp)) == CNST_NULL) {
3229 ir_node *block = get_irn_n(n, -1);
3232 * Note: normalization puts the constant on the right site,
3233 * so we check only one case.
3235 * Note further that these optimization work even for floating point
3236 * with NaN's because -NaN == NaN.
3237 * However, if +0 and -0 is handled differently, we cannot use the first one.
3239 if (get_irn_op(f) == op_Minus &&
3240 get_Minus_op(f) == t &&
3241 get_Cmp_left(cmp) == t) {
3243 if (proj_nr == pn_Cmp_Ge || proj_nr == pn_Cmp_Gt) {
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_Le || proj_nr == pn_Cmp_Lt) {
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);
3265 } else if (get_irn_op(t) == op_Minus &&
3266 get_Minus_op(t) == f &&
3267 get_Cmp_left(cmp) == f) {
3269 if (proj_nr == pn_Cmp_Le || proj_nr == pn_Cmp_Lt) {
3270 /* Mux(a <=/< 0, a, -a) ==> Abs(a) */
3271 n = new_rd_Abs(get_irn_dbg_info(n),
3275 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_ABS);
3277 } else if (proj_nr == pn_Cmp_Ge || proj_nr == pn_Cmp_Gt) {
3278 /* Mux(a >=/> 0, a, -a) ==> Minus(Abs(a)) */
3279 n = new_rd_Abs(get_irn_dbg_info(n),
3283 n = new_rd_Minus(get_irn_dbg_info(n),
3288 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_ABS);
3293 if (mode_is_int(mode) && mode_is_signed(mode) &&
3294 get_mode_arithmetic(mode) == irma_twos_complement) {
3295 ir_node *x = get_Cmp_left(cmp);
3297 /* the following optimization works only with signed integer two-complement mode */
3299 if (mode == get_irn_mode(x)) {
3301 * FIXME: this restriction is two rigid, as it would still
3302 * work if mode(x) = Hs and mode == Is, but at least it removes
3305 if ((proj_nr == pn_Cmp_Lt || proj_nr == pn_Cmp_Le) &&
3306 classify_Const(t) == CNST_ALL_ONE &&
3307 classify_Const(f) == CNST_NULL) {
3309 * Mux(x:T </<= 0, 0, -1) -> Shrs(x, sizeof_bits(T) - 1)
3313 n = new_rd_Shrs(get_irn_dbg_info(n),
3314 current_ir_graph, block, x,
3315 new_r_Const_long(current_ir_graph, block, mode_Iu,
3316 get_mode_size_bits(mode) - 1),
3318 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_SHR);
3320 } else if ((proj_nr == pn_Cmp_Gt || proj_nr == pn_Cmp_Ge) &&
3321 classify_Const(t) == CNST_ONE &&
3322 classify_Const(f) == CNST_NULL) {
3324 * Mux(x:T >/>= 0, 0, 1) -> Shr(-x, sizeof_bits(T) - 1)
3328 n = new_rd_Shr(get_irn_dbg_info(n),
3329 current_ir_graph, block,
3330 new_r_Minus(current_ir_graph, block, x, mode),
3331 new_r_Const_long(current_ir_graph, block, mode_Iu,
3332 get_mode_size_bits(mode) - 1),
3334 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_SHR);
3341 return arch_transform_node_Mux(n);
3342 } /* transform_node_Mux */
3345 * Optimize a Psi into some simpler cases.
3347 static ir_node *transform_node_Psi(ir_node *n) {
3349 return transform_node_Mux(n);
3352 } /* transform_node_Psi */
3355 * Tries several [inplace] [optimizing] transformations and returns an
3356 * equivalent node. The difference to equivalent_node() is that these
3357 * transformations _do_ generate new nodes, and thus the old node must
3358 * not be freed even if the equivalent node isn't the old one.
3360 static ir_node *transform_node(ir_node *n) {
3361 if (n->op->ops.transform_node)
3362 n = n->op->ops.transform_node(n);
3364 } /* transform_node */
3367 * Sets the default transform node operation for an ir_op_ops.
3369 * @param code the opcode for the default operation
3370 * @param ops the operations initialized
3375 static ir_op_ops *firm_set_default_transform_node(ir_opcode code, ir_op_ops *ops)
3379 ops->transform_node = transform_node_##a; \
3412 } /* firm_set_default_transform_node */
3415 /* **************** Common Subexpression Elimination **************** */
3417 /** The size of the hash table used, should estimate the number of nodes
3419 #define N_IR_NODES 512
3421 /** Compares the attributes of two Const nodes. */
3422 static int node_cmp_attr_Const(ir_node *a, ir_node *b) {
3423 return (get_Const_tarval(a) != get_Const_tarval(b))
3424 || (get_Const_type(a) != get_Const_type(b));
3425 } /* node_cmp_attr_Const */
3427 /** Compares the attributes of two Proj nodes. */
3428 static int node_cmp_attr_Proj(ir_node *a, ir_node *b) {
3429 return get_irn_proj_attr (a) != get_irn_proj_attr (b);
3430 } /* node_cmp_attr_Proj */
3432 /** Compares the attributes of two Filter nodes. */
3433 static int node_cmp_attr_Filter(ir_node *a, ir_node *b) {
3434 return get_Filter_proj(a) != get_Filter_proj(b);
3435 } /* node_cmp_attr_Filter */
3437 /** Compares the attributes of two Alloc nodes. */
3438 static int node_cmp_attr_Alloc(ir_node *a, ir_node *b) {
3439 return (get_irn_alloc_attr(a).where != get_irn_alloc_attr(b).where)
3440 || (get_irn_alloc_attr(a).type != get_irn_alloc_attr(b).type);
3441 } /* node_cmp_attr_Alloc */
3443 /** Compares the attributes of two Free nodes. */
3444 static int node_cmp_attr_Free(ir_node *a, ir_node *b) {
3445 return (get_irn_free_attr(a).where != get_irn_free_attr(b).where)
3446 || (get_irn_free_attr(a).type != get_irn_free_attr(b).type);
3447 } /* node_cmp_attr_Free */
3449 /** Compares the attributes of two SymConst nodes. */
3450 static int node_cmp_attr_SymConst(ir_node *a, ir_node *b) {
3451 return (get_irn_symconst_attr(a).num != get_irn_symconst_attr(b).num)
3452 || (get_irn_symconst_attr(a).sym.type_p != get_irn_symconst_attr(b).sym.type_p)
3453 || (get_irn_symconst_attr(a).tp != get_irn_symconst_attr(b).tp);
3454 } /* node_cmp_attr_SymConst */
3456 /** Compares the attributes of two Call nodes. */
3457 static int node_cmp_attr_Call(ir_node *a, ir_node *b) {
3458 return (get_irn_call_attr(a) != get_irn_call_attr(b));
3459 } /* node_cmp_attr_Call */
3461 /** Compares the attributes of two Sel nodes. */
3462 static int node_cmp_attr_Sel(ir_node *a, ir_node *b) {
3463 return (get_irn_sel_attr(a).ent->kind != get_irn_sel_attr(b).ent->kind)
3464 || (get_irn_sel_attr(a).ent->name != get_irn_sel_attr(b).ent->name)
3465 || (get_irn_sel_attr(a).ent->owner != get_irn_sel_attr(b).ent->owner)
3466 || (get_irn_sel_attr(a).ent->ld_name != get_irn_sel_attr(b).ent->ld_name)
3467 || (get_irn_sel_attr(a).ent->type != get_irn_sel_attr(b).ent->type);
3468 } /* node_cmp_attr_Sel */
3470 /** Compares the attributes of two Phi nodes. */
3471 static int node_cmp_attr_Phi(ir_node *a, ir_node *b) {
3472 /* we can only enter this function if both nodes have the same number of inputs,
3473 hence it is enough to check if one of them is a Phi0 */
3475 /* check the Phi0 attribute */
3476 return get_irn_phi0_attr(a) != get_irn_phi0_attr(b);
3479 } /* node_cmp_attr_Phi */
3481 /** Compares the attributes of two Conv nodes. */
3482 static int node_cmp_attr_Conv(ir_node *a, ir_node *b) {
3483 return get_Conv_strict(a) != get_Conv_strict(b);
3484 } /* node_cmp_attr_Conv */
3486 /** Compares the attributes of two Cast nodes. */
3487 static int node_cmp_attr_Cast(ir_node *a, ir_node *b) {
3488 return get_Cast_type(a) != get_Cast_type(b);
3489 } /* node_cmp_attr_Cast */
3491 /** Compares the attributes of two Load nodes. */
3492 static int node_cmp_attr_Load(ir_node *a, ir_node *b) {
3493 if (get_Load_volatility(a) == volatility_is_volatile ||
3494 get_Load_volatility(b) == volatility_is_volatile)
3495 /* NEVER do CSE on volatile Loads */
3498 return get_Load_mode(a) != get_Load_mode(b);
3499 } /* node_cmp_attr_Load */
3501 /** Compares the attributes of two Store nodes. */
3502 static int node_cmp_attr_Store(ir_node *a, ir_node *b) {
3503 /* NEVER do CSE on volatile Stores */
3504 return (get_Store_volatility(a) == volatility_is_volatile ||
3505 get_Store_volatility(b) == volatility_is_volatile);
3506 } /* node_cmp_attr_Store */
3508 /** Compares the attributes of two Confirm nodes. */
3509 static int node_cmp_attr_Confirm(ir_node *a, ir_node *b) {
3510 return (get_Confirm_cmp(a) != get_Confirm_cmp(b));
3511 } /* node_cmp_attr_Confirm */
3513 /** Compares the attributes of two ASM nodes. */
3514 static int node_cmp_attr_ASM(ir_node *a, ir_node *b) {
3516 const ir_asm_constraint *ca;
3517 const ir_asm_constraint *cb;
3520 if (get_ASM_text(a) != get_ASM_text(b));
3523 /* Should we really check the constraints here? Should be better, but is strange. */
3524 n = get_ASM_n_input_constraints(a);
3525 if (n != get_ASM_n_input_constraints(b))
3528 ca = get_ASM_input_constraints(a);
3529 cb = get_ASM_input_constraints(b);
3530 for (i = 0; i < n; ++i) {
3531 if (ca[i].pos != cb[i].pos || ca[i].constraint != cb[i].constraint)
3535 n = get_ASM_n_output_constraints(a);
3536 if (n != get_ASM_n_output_constraints(b))
3539 ca = get_ASM_output_constraints(a);
3540 cb = get_ASM_output_constraints(b);
3541 for (i = 0; i < n; ++i) {
3542 if (ca[i].pos != cb[i].pos || ca[i].constraint != cb[i].constraint)
3546 n = get_ASM_n_clobbers(a);
3547 if (n != get_ASM_n_clobbers(b))
3550 cla = get_ASM_clobbers(a);
3551 clb = get_ASM_clobbers(b);
3552 for (i = 0; i < n; ++i) {
3553 if (cla[i] != clb[i])
3557 } /* node_cmp_attr_ASM */
3560 * Set the default node attribute compare operation for an ir_op_ops.
3562 * @param code the opcode for the default operation
3563 * @param ops the operations initialized
3568 static ir_op_ops *firm_set_default_node_cmp_attr(ir_opcode code, ir_op_ops *ops)
3572 ops->node_cmp_attr = node_cmp_attr_##a; \
3597 } /* firm_set_default_node_cmp_attr */
3600 * Compare function for two nodes in the hash table. Gets two
3601 * nodes as parameters. Returns 0 if the nodes are a cse.
3603 int identities_cmp(const void *elt, const void *key) {
3610 if (a == b) return 0;
3612 if ((get_irn_op(a) != get_irn_op(b)) ||
3613 (get_irn_mode(a) != get_irn_mode(b))) return 1;
3615 /* compare if a's in and b's in are of equal length */
3616 irn_arity_a = get_irn_intra_arity (a);
3617 if (irn_arity_a != get_irn_intra_arity(b))
3620 /* for block-local cse and op_pin_state_pinned nodes: */
3621 if (!get_opt_global_cse() || (get_irn_pinned(a) == op_pin_state_pinned)) {
3622 if (get_irn_intra_n(a, -1) != get_irn_intra_n(b, -1))
3626 /* compare a->in[0..ins] with b->in[0..ins] */
3627 for (i = 0; i < irn_arity_a; i++)
3628 if (get_irn_intra_n(a, i) != get_irn_intra_n(b, i))
3632 * here, we already now that the nodes are identical except their
3635 if (a->op->ops.node_cmp_attr)
3636 return a->op->ops.node_cmp_attr(a, b);
3639 } /* identities_cmp */
3642 * Calculate a hash value of a node.
3644 unsigned ir_node_hash(ir_node *node) {
3648 if (node->op == op_Const) {
3649 /* special value for const, as they only differ in their tarval. */
3650 h = HASH_PTR(node->attr.con.tv);
3651 h = 9*h + HASH_PTR(get_irn_mode(node));
3652 } else if (node->op == op_SymConst) {
3653 /* special value for const, as they only differ in their symbol. */
3654 h = HASH_PTR(node->attr.symc.sym.type_p);
3655 h = 9*h + HASH_PTR(get_irn_mode(node));
3658 /* hash table value = 9*(9*(9*(9*(9*arity+in[0])+in[1])+ ...)+mode)+code */
3659 h = irn_arity = get_irn_intra_arity(node);
3661 /* consider all in nodes... except the block if not a control flow. */
3662 for (i = is_cfop(node) ? -1 : 0; i < irn_arity; i++) {
3663 h = 9*h + HASH_PTR(get_irn_intra_n(node, i));
3667 h = 9*h + HASH_PTR(get_irn_mode(node));
3669 h = 9*h + HASH_PTR(get_irn_op(node));
3673 } /* ir_node_hash */
3675 pset *new_identities(void) {
3676 return new_pset(identities_cmp, N_IR_NODES);
3677 } /* new_identities */
3679 void del_identities(pset *value_table) {
3680 del_pset(value_table);
3681 } /* del_identities */
3684 * Return the canonical node computing the same value as n.
3686 * @param value_table The value table
3687 * @param n The node to lookup
3689 * Looks up the node in a hash table.
3691 * For Const nodes this is performed in the constructor, too. Const
3692 * nodes are extremely time critical because of their frequent use in
3693 * constant string arrays.
3695 static INLINE ir_node *identify(pset *value_table, ir_node *n) {
3698 if (!value_table) return n;
3700 if (get_opt_reassociation()) {
3701 if (is_op_commutative(get_irn_op(n))) {
3702 ir_node *l = get_binop_left(n);
3703 ir_node *r = get_binop_right(n);
3705 /* for commutative operators perform a OP b == b OP a */
3706 if (get_irn_idx(l) > get_irn_idx(r)) {
3707 set_binop_left(n, r);
3708 set_binop_right(n, l);
3713 o = pset_find(value_table, n, ir_node_hash(n));
3722 * During construction we set the op_pin_state_pinned flag in the graph right when the
3723 * optimization is performed. The flag turning on procedure global cse could
3724 * be changed between two allocations. This way we are safe.
3726 static INLINE ir_node *identify_cons(pset *value_table, ir_node *n) {
3729 n = identify(value_table, n);
3730 if (get_irn_n(old, -1) != get_irn_n(n, -1))
3731 set_irg_pinned(current_ir_graph, op_pin_state_floats);
3733 } /* identify_cons */
3736 * Return the canonical node computing the same value as n.
3737 * Looks up the node in a hash table, enters it in the table
3738 * if it isn't there yet.
3740 ir_node *identify_remember(pset *value_table, ir_node *n) {
3743 if (!value_table) return n;
3745 if (get_opt_reassociation()) {
3746 if (is_op_commutative(get_irn_op(n))) {
3747 ir_node *l = get_binop_left(n);
3748 ir_node *r = get_binop_right(n);
3749 int l_idx = get_irn_idx(l);
3750 int r_idx = get_irn_idx(r);
3752 /* For commutative operators perform a OP b == b OP a but keep
3753 constants on the RIGHT side. This helps greatly in some optimizations.
3754 Moreover we use the idx number to make the form deterministic. */
3755 if (is_irn_constlike(l))
3757 if (is_irn_constlike(r))
3759 if (l_idx < r_idx) {
3760 set_binop_left(n, r);
3761 set_binop_right(n, l);
3766 /* lookup or insert in hash table with given hash key. */
3767 o = pset_insert(value_table, n, ir_node_hash(n));
3774 } /* identify_remember */
3776 /* Add a node to the identities value table. */
3777 void add_identities(pset *value_table, ir_node *node) {
3778 if (get_opt_cse() && is_no_Block(node))
3779 identify_remember(value_table, node);
3780 } /* add_identities */
3782 /* Visit each node in the value table of a graph. */
3783 void visit_all_identities(ir_graph *irg, irg_walk_func visit, void *env) {
3785 ir_graph *rem = current_ir_graph;
3787 current_ir_graph = irg;
3788 foreach_pset(irg->value_table, node)
3790 current_ir_graph = rem;
3791 } /* visit_all_identities */
3794 * Garbage in, garbage out. If a node has a dead input, i.e., the
3795 * Bad node is input to the node, return the Bad node.
3797 static INLINE ir_node *gigo(ir_node *node) {
3799 ir_op *op = get_irn_op(node);
3801 /* remove garbage blocks by looking at control flow that leaves the block
3802 and replacing the control flow by Bad. */
3803 if (get_irn_mode(node) == mode_X) {
3804 ir_node *block = get_nodes_block(skip_Proj(node));
3806 /* Don't optimize nodes in immature blocks. */
3807 if (!get_Block_matured(block)) return node;
3808 /* Don't optimize End, may have Bads. */
3809 if (op == op_End) return node;
3811 if (is_Block(block)) {
3812 irn_arity = get_irn_arity(block);
3813 for (i = 0; i < irn_arity; i++) {
3814 if (!is_Bad(get_irn_n(block, i)))
3817 if (i == irn_arity) {
3818 ir_graph *irg = get_irn_irg(block);
3819 /* the start block is never dead */
3820 if (block != get_irg_start_block(irg)
3821 && block != get_irg_end_block(irg))
3827 /* Blocks, Phis and Tuples may have dead inputs, e.g., if one of the
3828 blocks predecessors is dead. */
3829 if (op != op_Block && op != op_Phi && op != op_Tuple) {
3830 irn_arity = get_irn_arity(node);
3833 * Beware: we can only read the block of a non-floating node.
3835 if (is_irn_pinned_in_irg(node) &&
3836 is_Block_dead(get_nodes_block(node)))
3839 for (i = 0; i < irn_arity; i++) {
3840 ir_node *pred = get_irn_n(node, i);
3845 /* Propagating Unknowns here seems to be a bad idea, because
3846 sometimes we need a node as a input and did not want that
3848 However, it might be useful to move this into a later phase
3849 (if you think that optimizing such code is useful). */
3850 if (is_Unknown(pred) && mode_is_data(get_irn_mode(node)))
3851 return new_Unknown(get_irn_mode(node));
3856 /* With this code we violate the agreement that local_optimize
3857 only leaves Bads in Block, Phi and Tuple nodes. */
3858 /* If Block has only Bads as predecessors it's garbage. */
3859 /* If Phi has only Bads as predecessors it's garbage. */
3860 if ((op == op_Block && get_Block_matured(node)) || op == op_Phi) {
3861 irn_arity = get_irn_arity(node);
3862 for (i = 0; i < irn_arity; i++) {
3863 if (!is_Bad(get_irn_n(node, i))) break;
3865 if (i == irn_arity) node = new_Bad();
3872 * These optimizations deallocate nodes from the obstack.
3873 * It can only be called if it is guaranteed that no other nodes
3874 * reference this one, i.e., right after construction of a node.
3876 * @param n The node to optimize
3878 * current_ir_graph must be set to the graph of the node!
3880 ir_node *optimize_node(ir_node *n) {
3883 ir_opcode iro = get_irn_opcode(n);
3885 /* Always optimize Phi nodes: part of the construction. */
3886 if ((!get_opt_optimize()) && (iro != iro_Phi)) return n;
3888 /* constant expression evaluation / constant folding */
3889 if (get_opt_constant_folding()) {
3890 /* neither constants nor Tuple values can be evaluated */
3891 if (iro != iro_Const && (get_irn_mode(n) != mode_T)) {
3892 /* try to evaluate */
3893 tv = computed_value(n);
3894 if (tv != tarval_bad) {
3896 ir_type *old_tp = get_irn_type(n);
3897 int i, arity = get_irn_arity(n);
3901 * Try to recover the type of the new expression.
3903 for (i = 0; i < arity && !old_tp; ++i)
3904 old_tp = get_irn_type(get_irn_n(n, i));
3907 * we MUST copy the node here temporary, because it's still needed
3908 * for DBG_OPT_CSTEVAL
3910 node_size = offsetof(ir_node, attr) + n->op->attr_size;
3911 oldn = alloca(node_size);
3913 memcpy(oldn, n, node_size);
3914 CLONE_ARR_A(ir_node *, oldn->in, n->in);
3916 /* ARG, copy the in array, we need it for statistics */
3917 memcpy(oldn->in, n->in, ARR_LEN(n->in) * sizeof(n->in[0]));
3919 /* note the inplace edges module */
3920 edges_node_deleted(n, current_ir_graph);
3922 /* evaluation was successful -- replace the node. */
3923 irg_kill_node(current_ir_graph, n);
3924 nw = new_Const(get_tarval_mode (tv), tv);
3926 if (old_tp && get_type_mode(old_tp) == get_tarval_mode (tv))
3927 set_Const_type(nw, old_tp);
3928 DBG_OPT_CSTEVAL(oldn, nw);
3934 /* remove unnecessary nodes */
3935 if (get_opt_constant_folding() ||
3936 (iro == iro_Phi) || /* always optimize these nodes. */
3938 (iro == iro_Proj) ||
3939 (iro == iro_Block) ) /* Flags tested local. */
3940 n = equivalent_node(n);
3942 optimize_preds(n); /* do node specific optimizations of nodes predecessors. */
3944 /* Common Subexpression Elimination.
3946 * Checks whether n is already available.
3947 * The block input is used to distinguish different subexpressions. Right
3948 * now all nodes are op_pin_state_pinned to blocks, i.e., the CSE only finds common
3949 * subexpressions within a block.
3952 n = identify_cons(current_ir_graph->value_table, n);
3955 edges_node_deleted(oldn, current_ir_graph);
3957 /* We found an existing, better node, so we can deallocate the old node. */
3958 irg_kill_node(current_ir_graph, oldn);
3962 /* Some more constant expression evaluation that does not allow to
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 have a legal, useful node. Enter it in hash table for CSE */
3975 if (get_opt_cse() && (get_irn_opcode(n) != iro_Block)) {
3976 n = identify_remember(current_ir_graph->value_table, n);
3980 } /* optimize_node */
3984 * These optimizations never deallocate nodes (in place). This can cause dead
3985 * nodes lying on the obstack. Remove these by a dead node elimination,
3986 * i.e., a copying garbage collection.
3988 ir_node *optimize_in_place_2(ir_node *n) {
3991 ir_opcode iro = get_irn_opcode(n);
3993 if (!get_opt_optimize() && (get_irn_op(n) != op_Phi)) return n;
3995 /* constant expression evaluation / constant folding */
3996 if (get_opt_constant_folding()) {
3997 /* neither constants nor Tuple values can be evaluated */
3998 if (iro != iro_Const && get_irn_mode(n) != mode_T) {
3999 /* try to evaluate */
4000 tv = computed_value(n);
4001 if (tv != tarval_bad) {
4002 /* evaluation was successful -- replace the node. */
4003 ir_type *old_tp = get_irn_type(n);
4004 int i, arity = get_irn_arity(n);
4007 * Try to recover the type of the new expression.
4009 for (i = 0; i < arity && !old_tp; ++i)
4010 old_tp = get_irn_type(get_irn_n(n, i));
4012 n = new_Const(get_tarval_mode(tv), tv);
4014 if (old_tp && get_type_mode(old_tp) == get_tarval_mode(tv))
4015 set_Const_type(n, old_tp);
4017 DBG_OPT_CSTEVAL(oldn, n);
4023 /* remove unnecessary nodes */
4024 if (get_opt_constant_folding() ||
4025 (iro == iro_Phi) || /* always optimize these nodes. */
4026 (iro == iro_Id) || /* ... */
4027 (iro == iro_Proj) || /* ... */
4028 (iro == iro_Block) ) /* Flags tested local. */
4029 n = equivalent_node(n);
4031 optimize_preds(n); /* do node specific optimizations of nodes predecessors. */
4033 /** common subexpression elimination **/
4034 /* Checks whether n is already available. */
4035 /* The block input is used to distinguish different subexpressions. Right
4036 now all nodes are op_pin_state_pinned to blocks, i.e., the cse only finds common
4037 subexpressions within a block. */
4038 if (get_opt_cse()) {
4039 n = identify(current_ir_graph->value_table, n);
4042 /* Some more constant expression evaluation. */
4043 iro = get_irn_opcode(n);
4044 if (get_opt_constant_folding() ||
4045 (iro == iro_Cond) ||
4046 (iro == iro_Proj)) /* Flags tested local. */
4047 n = transform_node(n);
4049 /* Remove nodes with dead (Bad) input.
4050 Run always for transformation induced Bads. */
4053 /* Now we can verify the node, as it has no dead inputs any more. */
4056 /* Now we have a legal, useful node. Enter it in hash table for cse.
4057 Blocks should be unique anyways. (Except the successor of start:
4058 is cse with the start block!) */
4059 if (get_opt_cse() && (get_irn_opcode(n) != iro_Block))
4060 n = identify_remember(current_ir_graph->value_table, n);
4063 } /* optimize_in_place_2 */
4066 * Wrapper for external use, set proper status bits after optimization.
4068 ir_node *optimize_in_place(ir_node *n) {
4069 /* Handle graph state */
4070 assert(get_irg_phase_state(current_ir_graph) != phase_building);
4072 if (get_opt_global_cse())
4073 set_irg_pinned(current_ir_graph, op_pin_state_floats);
4074 if (get_irg_outs_state(current_ir_graph) == outs_consistent)
4075 set_irg_outs_inconsistent(current_ir_graph);
4077 /* FIXME: Maybe we could also test whether optimizing the node can
4078 change the control graph. */
4079 set_irg_doms_inconsistent(current_ir_graph);
4080 return optimize_in_place_2(n);
4081 } /* optimize_in_place */
4084 * Sets the default operation for an ir_ops.
4086 ir_op_ops *firm_set_default_operations(ir_opcode code, ir_op_ops *ops) {
4087 ops = firm_set_default_computed_value(code, ops);
4088 ops = firm_set_default_equivalent_node(code, ops);
4089 ops = firm_set_default_transform_node(code, ops);
4090 ops = firm_set_default_node_cmp_attr(code, ops);
4091 ops = firm_set_default_get_type(code, ops);
4092 ops = firm_set_default_get_type_attr(code, ops);
4093 ops = firm_set_default_get_entity_attr(code, ops);
4096 } /* firm_set_default_operations */