3 * File name: ir/ir/iropt.c
4 * Purpose: iropt --- optimizations intertwined with IR construction.
5 * Author: Christian Schaefer
6 * Modified by: Goetz Lindenmaier, Michael Beck
9 * Copyright: (c) 1998-2006 Universität Karlsruhe
10 * Licence: This file protected by GPL - GNU GENERAL PUBLIC LICENSE.
28 #include "irgraph_t.h"
29 #include "iredges_t.h"
36 #include "dbginfo_t.h"
37 #include "iropt_dbg.h"
43 #include "opt_polymorphy.h"
44 #include "opt_confirms.h"
47 /* Make types visible to allow most efficient access */
51 * Return the value of a Constant.
53 static tarval *computed_value_Const(ir_node *n) {
54 return get_Const_tarval(n);
55 } /* computed_value_Const */
58 * Return the value of a 'sizeof', 'alignof' or 'offsetof' SymConst.
60 static tarval *computed_value_SymConst(ir_node *n) {
64 switch (get_SymConst_kind(n)) {
65 case symconst_type_size:
66 type = get_SymConst_type(n);
67 if (get_type_state(type) == layout_fixed)
68 return new_tarval_from_long(get_type_size_bytes(type), get_irn_mode(n));
70 case symconst_type_align:
71 type = get_SymConst_type(n);
72 if (get_type_state(type) == layout_fixed)
73 return new_tarval_from_long(get_type_alignment_bytes(type), get_irn_mode(n));
75 case symconst_ofs_ent:
76 ent = get_SymConst_entity(n);
77 type = get_entity_owner(ent);
78 if (get_type_state(type) == layout_fixed)
79 return new_tarval_from_long(get_entity_offset(ent), get_irn_mode(n));
85 } /* computed_value_SymConst */
88 * Return the value of an Add.
90 static tarval *computed_value_Add(ir_node *n) {
91 ir_node *a = get_Add_left(n);
92 ir_node *b = get_Add_right(n);
94 tarval *ta = value_of(a);
95 tarval *tb = value_of(b);
97 if ((ta != tarval_bad) && (tb != tarval_bad) && (get_irn_mode(a) == get_irn_mode(b)))
98 return tarval_add(ta, tb);
101 } /* computed_value_Add */
104 * Return the value of a Sub.
105 * Special case: a - a
107 static tarval *computed_value_Sub(ir_node *n) {
108 ir_node *a = get_Sub_left(n);
109 ir_node *b = get_Sub_right(n);
114 if (a == b && !is_Bad(a))
115 return get_mode_null(get_irn_mode(n));
120 if ((ta != tarval_bad) && (tb != tarval_bad) && (get_irn_mode(a) == get_irn_mode(b)))
121 return tarval_sub(ta, tb);
124 } /* computed_value_Sub */
127 * Return the value of a Carry.
128 * Special : a op 0, 0 op b
130 static tarval *computed_value_Carry(ir_node *n) {
131 ir_node *a = get_binop_left(n);
132 ir_node *b = get_binop_right(n);
133 ir_mode *m = get_irn_mode(n);
135 tarval *ta = value_of(a);
136 tarval *tb = value_of(b);
138 if ((ta != tarval_bad) && (tb != tarval_bad)) {
140 return tarval_carry() ? get_mode_one(m) : get_mode_null(m);
142 if ( (classify_tarval(ta) == TV_CLASSIFY_NULL)
143 || (classify_tarval(tb) == TV_CLASSIFY_NULL))
144 return get_mode_null(m);
147 } /* computed_value_Carry */
150 * Return the value of a Borrow.
153 static tarval *computed_value_Borrow(ir_node *n) {
154 ir_node *a = get_binop_left(n);
155 ir_node *b = get_binop_right(n);
156 ir_mode *m = get_irn_mode(n);
158 tarval *ta = value_of(a);
159 tarval *tb = value_of(b);
161 if ((ta != tarval_bad) && (tb != tarval_bad)) {
162 return tarval_cmp(ta, tb) == pn_Cmp_Lt ? get_mode_one(m) : get_mode_null(m);
163 } else if (classify_tarval(ta) == TV_CLASSIFY_NULL) {
164 return get_mode_null(m);
167 } /* computed_value_Borrow */
170 * Return the value of an unary Minus.
172 static tarval *computed_value_Minus(ir_node *n) {
173 ir_node *a = get_Minus_op(n);
174 tarval *ta = value_of(a);
176 if ((ta != tarval_bad) && mode_is_signed(get_irn_mode(a)))
177 return tarval_neg(ta);
180 } /* computed_value_Minus */
183 * Return the value of a Mul.
185 static tarval *computed_value_Mul(ir_node *n) {
186 ir_node *a = get_Mul_left(n);
187 ir_node *b = get_Mul_right(n);
189 tarval *ta = value_of(a);
190 tarval *tb = value_of(b);
192 if ((ta != tarval_bad) && (tb != tarval_bad) && (get_irn_mode(a) == get_irn_mode(b))) {
193 return tarval_mul(ta, tb);
195 /* a*0 = 0 or 0*b = 0:
196 calls computed_value recursive and returns the 0 with proper
198 if ((ta != tarval_bad) && (ta == get_mode_null(get_tarval_mode(ta))))
200 if ((tb != tarval_bad) && (tb == get_mode_null(get_tarval_mode(tb))))
204 } /* computed_value_Mul */
207 * Return the value of a floating point Quot.
209 static tarval *computed_value_Quot(ir_node *n) {
210 ir_node *a = get_Quot_left(n);
211 ir_node *b = get_Quot_right(n);
213 tarval *ta = value_of(a);
214 tarval *tb = value_of(b);
216 /* This was missing in original implementation. Why? */
217 if ((ta != tarval_bad) && (tb != tarval_bad) && (get_irn_mode(a) == get_irn_mode(b))) {
218 if (tb != get_mode_null(get_tarval_mode(tb))) /* div by zero: return tarval_bad */
219 return tarval_quo(ta, tb);
222 } /* computed_value_Quot */
225 * Calculate the value of an integer Div of two nodes.
226 * Special case: 0 / b
228 static tarval *do_computed_value_Div(ir_node *a, ir_node *b) {
229 tarval *ta = value_of(a);
230 tarval *tb = value_of(b);
232 /* Compute c1 / c2 or 0 / a, a != 0 */
233 if (ta != tarval_bad) {
234 if ((tb != tarval_bad) && (tb != get_mode_null(get_irn_mode(b)))) /* div by zero: return tarval_bad */
235 return tarval_div(ta, tb);
236 else if (ta == get_mode_null(get_tarval_mode(ta))) /* 0 / b == 0 */
240 } /* do_computed_value_Div */
243 * Return the value of an integer Div.
245 static tarval *computed_value_Div(ir_node *n) {
246 return do_computed_value_Div(get_Div_left(n), get_Div_right(n));
247 } /* computed_value_Div */
250 * Calculate the value of an integer Mod of two nodes.
251 * Special case: a % 1
253 static tarval *do_computed_value_Mod(ir_node *a, ir_node *b) {
254 tarval *ta = value_of(a);
255 tarval *tb = value_of(b);
257 /* Compute c1 % c2 or a % 1 */
258 if (tb != tarval_bad) {
259 if ((ta != tarval_bad) && (tb != get_mode_null(get_tarval_mode(tb)))) /* div by zero: return tarval_bad */
260 return tarval_mod(ta, tb);
261 else if (tb == get_mode_one(get_tarval_mode(tb))) /* x mod 1 == 0 */
262 return get_mode_null(get_irn_mode(a));
265 } /* do_computed_value_Mod */
268 * Return the value of an integer Mod.
270 static tarval *computed_value_Mod(ir_node *n) {
271 return do_computed_value_Mod(get_Mod_left(n), get_Mod_right(n));
272 } /* computed_value_Mod */
275 * Return the value of an Abs.
277 static tarval *computed_value_Abs(ir_node *n) {
278 ir_node *a = get_Abs_op(n);
279 tarval *ta = value_of(a);
281 if (ta != tarval_bad)
282 return tarval_abs(ta);
285 } /* computed_value_Abs */
288 * Return the value of an And.
289 * Special case: a & 0, 0 & b
291 static tarval *computed_value_And(ir_node *n) {
292 ir_node *a = get_And_left(n);
293 ir_node *b = get_And_right(n);
295 tarval *ta = value_of(a);
296 tarval *tb = value_of(b);
298 if ((ta != tarval_bad) && (tb != tarval_bad)) {
299 return tarval_and (ta, tb);
303 if ( (classify_tarval ((v = ta)) == TV_CLASSIFY_NULL)
304 || (classify_tarval ((v = tb)) == TV_CLASSIFY_NULL)) {
309 } /* computed_value_And */
312 * Return the value of an Or.
313 * Special case: a | 1...1, 1...1 | b
315 static tarval *computed_value_Or(ir_node *n) {
316 ir_node *a = get_Or_left(n);
317 ir_node *b = get_Or_right(n);
319 tarval *ta = value_of(a);
320 tarval *tb = value_of(b);
322 if ((ta != tarval_bad) && (tb != tarval_bad)) {
323 return tarval_or (ta, tb);
326 if ( (classify_tarval ((v = ta)) == TV_CLASSIFY_ALL_ONE)
327 || (classify_tarval ((v = tb)) == TV_CLASSIFY_ALL_ONE)) {
332 } /* computed_value_Or */
335 * Return the value of an Eor.
337 static tarval *computed_value_Eor(ir_node *n) {
338 ir_node *a = get_Eor_left(n);
339 ir_node *b = get_Eor_right(n);
344 return get_mode_null(get_irn_mode(n));
349 if ((ta != tarval_bad) && (tb != tarval_bad)) {
350 return tarval_eor (ta, tb);
353 } /* computed_value_Eor */
356 * Return the value of a Not.
358 static tarval *computed_value_Not(ir_node *n) {
359 ir_node *a = get_Not_op(n);
360 tarval *ta = value_of(a);
362 if (ta != tarval_bad)
363 return tarval_not(ta);
366 } /* computed_value_Not */
369 * Return the value of a Shl.
371 static tarval *computed_value_Shl(ir_node *n) {
372 ir_node *a = get_Shl_left(n);
373 ir_node *b = get_Shl_right(n);
375 tarval *ta = value_of(a);
376 tarval *tb = value_of(b);
378 if ((ta != tarval_bad) && (tb != tarval_bad)) {
379 return tarval_shl (ta, tb);
382 } /* computed_value_Shl */
385 * Return the value of a Shr.
387 static tarval *computed_value_Shr(ir_node *n) {
388 ir_node *a = get_Shr_left(n);
389 ir_node *b = get_Shr_right(n);
391 tarval *ta = value_of(a);
392 tarval *tb = value_of(b);
394 if ((ta != tarval_bad) && (tb != tarval_bad)) {
395 return tarval_shr (ta, tb);
398 } /* computed_value_Shr */
401 * Return the value of a Shrs.
403 static tarval *computed_value_Shrs(ir_node *n) {
404 ir_node *a = get_Shrs_left(n);
405 ir_node *b = get_Shrs_right(n);
407 tarval *ta = value_of(a);
408 tarval *tb = value_of(b);
410 if ((ta != tarval_bad) && (tb != tarval_bad)) {
411 return tarval_shrs (ta, tb);
414 } /* computed_value_Shrs */
417 * Return the value of a Rot.
419 static tarval *computed_value_Rot(ir_node *n) {
420 ir_node *a = get_Rot_left(n);
421 ir_node *b = get_Rot_right(n);
423 tarval *ta = value_of(a);
424 tarval *tb = value_of(b);
426 if ((ta != tarval_bad) && (tb != tarval_bad)) {
427 return tarval_rot (ta, tb);
430 } /* computed_value_Rot */
433 * Return the value of a Conv.
435 static tarval *computed_value_Conv(ir_node *n) {
436 ir_node *a = get_Conv_op(n);
437 tarval *ta = value_of(a);
439 if (ta != tarval_bad)
440 return tarval_convert_to(ta, get_irn_mode(n));
443 } /* computed_value_Conv */
446 * Return the value of a Proj(Cmp).
448 * This performs a first step of unreachable code elimination.
449 * Proj can not be computed, but folding a Cmp above the Proj here is
450 * not as wasteful as folding a Cmp into a Tuple of 16 Consts of which
452 * There are several case where we can evaluate a Cmp node, see later.
454 static tarval *computed_value_Proj_Cmp(ir_node *n) {
455 ir_node *a = get_Proj_pred(n);
456 ir_node *aa = get_Cmp_left(a);
457 ir_node *ab = get_Cmp_right(a);
458 long proj_nr = get_Proj_proj(n);
461 * BEWARE: a == a is NOT always True for floating Point values, as
462 * NaN != NaN is defined, so we must check this here.
465 !mode_is_float(get_irn_mode(aa)) || proj_nr == pn_Cmp_Lt || proj_nr == pn_Cmp_Gt)
468 /* This is a trick with the bits used for encoding the Cmp
469 Proj numbers, the following statement is not the same:
470 return new_tarval_from_long (proj_nr == pn_Cmp_Eq, mode_b) */
471 return new_tarval_from_long (proj_nr & pn_Cmp_Eq, mode_b);
474 tarval *taa = value_of(aa);
475 tarval *tab = value_of(ab);
476 ir_mode *mode = get_irn_mode(aa);
479 * The predecessors of Cmp are target values. We can evaluate
482 if ((taa != tarval_bad) && (tab != tarval_bad)) {
483 /* strange checks... */
484 pn_Cmp flags = tarval_cmp(taa, tab);
485 if (flags != pn_Cmp_False) {
486 return new_tarval_from_long (proj_nr & flags, mode_b);
489 /* for integer values, we can check against MIN/MAX */
490 else if (mode_is_int(mode)) {
491 /* MIN <=/> x. This results in true/false. */
492 if (taa == get_mode_min(mode)) {
493 /* a compare with the MIN value */
494 if (proj_nr == pn_Cmp_Le)
495 return get_tarval_b_true();
496 else if (proj_nr == pn_Cmp_Gt)
497 return get_tarval_b_false();
499 /* x >=/< MIN. This results in true/false. */
501 if (tab == get_mode_min(mode)) {
502 /* a compare with the MIN value */
503 if (proj_nr == pn_Cmp_Ge)
504 return get_tarval_b_true();
505 else if (proj_nr == pn_Cmp_Lt)
506 return get_tarval_b_false();
508 /* MAX >=/< x. This results in true/false. */
509 else if (taa == get_mode_max(mode)) {
510 if (proj_nr == pn_Cmp_Ge)
511 return get_tarval_b_true();
512 else if (proj_nr == pn_Cmp_Lt)
513 return get_tarval_b_false();
515 /* x <=/> MAX. This results in true/false. */
516 else if (tab == get_mode_max(mode)) {
517 if (proj_nr == pn_Cmp_Le)
518 return get_tarval_b_true();
519 else if (proj_nr == pn_Cmp_Gt)
520 return get_tarval_b_false();
524 * The predecessors are Allocs or (void*)(0) constants. Allocs never
525 * return NULL, they raise an exception. Therefore we can predict
529 ir_node *aaa = skip_Id(skip_Proj(aa));
530 ir_node *aba = skip_Id(skip_Proj(ab));
532 if ( ( (/* aa is ProjP and aaa is Alloc */
533 (get_irn_op(aa) == op_Proj)
534 && (mode_is_reference(get_irn_mode(aa)))
535 && (get_irn_op(aaa) == op_Alloc))
536 && ( (/* ab is NULL */
537 (get_irn_op(ab) == op_Const)
538 && (mode_is_reference(get_irn_mode(ab)))
539 && (get_Const_tarval(ab) == get_mode_null(get_irn_mode(ab))))
540 || (/* ab is other Alloc */
541 (get_irn_op(ab) == op_Proj)
542 && (mode_is_reference(get_irn_mode(ab)))
543 && (get_irn_op(aba) == op_Alloc)
545 || (/* aa is NULL and aba is Alloc */
546 (get_irn_op(aa) == op_Const)
547 && (mode_is_reference(get_irn_mode(aa)))
548 && (get_Const_tarval(aa) == get_mode_null(get_irn_mode(aa)))
549 && (get_irn_op(ab) == op_Proj)
550 && (mode_is_reference(get_irn_mode(ab)))
551 && (get_irn_op(aba) == op_Alloc)))
553 return new_tarval_from_long(proj_nr & pn_Cmp_Ne, mode_b);
556 return computed_value_Cmp_Confirm(a, aa, ab, proj_nr);
557 } /* computed_value_Proj_Cmp */
560 * Return the value of a Proj, handle Proj(Cmp), Proj(Div), Proj(Mod),
561 * Proj(DivMod) and Proj(Quot).
563 static tarval *computed_value_Proj(ir_node *n) {
564 ir_node *a = get_Proj_pred(n);
567 switch (get_irn_opcode(a)) {
569 return computed_value_Proj_Cmp(n);
572 /* compute either the Div or the Mod part */
573 proj_nr = get_Proj_proj(n);
574 if (proj_nr == pn_DivMod_res_div)
575 return do_computed_value_Div(get_DivMod_left(a), get_DivMod_right(a));
576 else if (proj_nr == pn_DivMod_res_mod)
577 return do_computed_value_Mod(get_DivMod_left(a), get_DivMod_right(a));
581 if (get_Proj_proj(n) == pn_Div_res)
582 return computed_value(a);
586 if (get_Proj_proj(n) == pn_Mod_res)
587 return computed_value(a);
591 if (get_Proj_proj(n) == pn_Quot_res)
592 return computed_value(a);
599 } /* computed_value_Proj */
602 * Calculate the value of a Mux: can be evaluated, if the
603 * sel and the right input are known.
605 static tarval *computed_value_Mux(ir_node *n) {
606 ir_node *sel = get_Mux_sel(n);
607 tarval *ts = value_of(sel);
609 if (ts == get_tarval_b_true()) {
610 ir_node *v = get_Mux_true(n);
613 else if (ts == get_tarval_b_false()) {
614 ir_node *v = get_Mux_false(n);
618 } /* computed_value_Mux */
621 * Calculate the value of a Psi: can be evaluated, if a condition is true
622 * and all previous conditions are false. If all conditions are false
623 * we evaluate to the default one.
625 static tarval *computed_value_Psi(ir_node *n) {
627 return computed_value_Mux(n);
629 } /* computed_value_Psi */
632 * Calculate the value of a Confirm: can be evaluated,
633 * if it has the form Confirm(x, '=', Const).
635 static tarval *computed_value_Confirm(ir_node *n) {
636 return get_Confirm_cmp(n) == pn_Cmp_Eq ?
637 value_of(get_Confirm_bound(n)) : tarval_bad;
638 } /* computed_value_Confirm */
641 * If the parameter n can be computed, return its value, else tarval_bad.
642 * Performs constant folding.
644 * @param n The node this should be evaluated
646 tarval *computed_value(ir_node *n) {
647 if (n->op->ops.computed_value)
648 return n->op->ops.computed_value(n);
650 } /* computed_value */
653 * Set the default computed_value evaluator in an ir_op_ops.
655 * @param code the opcode for the default operation
656 * @param ops the operations initialized
661 static ir_op_ops *firm_set_default_computed_value(ir_opcode code, ir_op_ops *ops)
665 ops->computed_value = computed_value_##a; \
700 } /* firm_set_default_computed_value */
703 * Returns a equivalent block for another block.
704 * If the block has only one predecessor, this is
705 * the equivalent one. If the only predecessor of a block is
706 * the block itself, this is a dead block.
708 * If both predecessors of a block are the branches of a binary
709 * Cond, the equivalent block is Cond's block.
711 * If all predecessors of a block are bad or lies in a dead
712 * block, the current block is dead as well.
714 * Note, that blocks are NEVER turned into Bad's, instead
715 * the dead_block flag is set. So, never test for is_Bad(block),
716 * always use is_dead_Block(block).
718 static ir_node *equivalent_node_Block(ir_node *n)
721 int n_preds = get_Block_n_cfgpreds(n);
723 /* The Block constructor does not call optimize, but mature_immBlock
724 calls the optimization. */
725 assert(get_Block_matured(n));
727 /* Straightening: a single entry Block following a single exit Block
728 can be merged, if it is not the Start block. */
729 /* !!! Beware, all Phi-nodes of n must have been optimized away.
730 This should be true, as the block is matured before optimize is called.
731 But what about Phi-cycles with the Phi0/Id that could not be resolved?
732 Remaining Phi nodes are just Ids. */
733 if ((n_preds == 1) && (get_irn_op(get_Block_cfgpred(n, 0)) == op_Jmp)) {
734 ir_node *predblock = get_nodes_block(get_Block_cfgpred(n, 0));
735 if (predblock == oldn) {
736 /* Jmp jumps into the block it is in -- deal self cycle. */
737 n = set_Block_dead(n);
738 DBG_OPT_DEAD_BLOCK(oldn, n);
739 } else if (get_opt_control_flow_straightening()) {
741 DBG_OPT_STG(oldn, n);
743 } else if ((n_preds == 1) &&
744 (get_irn_op(skip_Proj(get_Block_cfgpred(n, 0))) == op_Cond)) {
745 ir_node *predblock = get_Block_cfgpred_block(n, 0);
746 if (predblock == oldn) {
747 /* Jmp jumps into the block it is in -- deal self cycle. */
748 n = set_Block_dead(n);
749 DBG_OPT_DEAD_BLOCK(oldn, n);
751 } else if ((n_preds == 2) &&
752 (get_opt_control_flow_weak_simplification())) {
753 /* Test whether Cond jumps twice to this block
754 * The more general case which more than 2 predecessors is handles
755 * in optimize_cf(), we handle only this special case for speed here.
757 ir_node *a = get_Block_cfgpred(n, 0);
758 ir_node *b = get_Block_cfgpred(n, 1);
760 if ((get_irn_op(a) == op_Proj) &&
761 (get_irn_op(b) == op_Proj) &&
762 (get_Proj_pred(a) == get_Proj_pred(b)) &&
763 (get_irn_op(get_Proj_pred(a)) == op_Cond) &&
764 (get_irn_mode(get_Cond_selector(get_Proj_pred(a))) == mode_b)) {
765 /* Also a single entry Block following a single exit Block. Phis have
766 twice the same operand and will be optimized away. */
767 n = get_nodes_block(get_Proj_pred(a));
768 DBG_OPT_IFSIM1(oldn, a, b, n);
770 } else if (get_opt_unreachable_code() &&
771 (n != get_irg_start_block(current_ir_graph)) &&
772 (n != get_irg_end_block(current_ir_graph)) ) {
775 /* If all inputs are dead, this block is dead too, except if it is
776 the start or end block. This is one step of unreachable code
778 for (i = get_Block_n_cfgpreds(n) - 1; i >= 0; --i) {
779 ir_node *pred = get_Block_cfgpred(n, i);
782 if (is_Bad(pred)) continue;
783 pred_blk = get_nodes_block(skip_Proj(pred));
785 if (is_Block_dead(pred_blk)) continue;
788 /* really found a living input */
793 n = set_Block_dead(n);
794 DBG_OPT_DEAD_BLOCK(oldn, n);
799 } /* equivalent_node_Block */
802 * Returns a equivalent node for a Jmp, a Bad :-)
803 * Of course this only happens if the Block of the Jmp is dead.
805 static ir_node *equivalent_node_Jmp(ir_node *n) {
806 /* unreachable code elimination */
807 if (is_Block_dead(get_nodes_block(n)))
811 } /* equivalent_node_Jmp */
813 /** Raise is handled in the same way as Jmp. */
814 #define equivalent_node_Raise equivalent_node_Jmp
817 /* We do not evaluate Cond here as we replace it by a new node, a Jmp.
818 See transform_node_Proj_Cond(). */
821 * Optimize operations that are commutative and have neutral 0,
822 * so a op 0 = 0 op a = a.
824 static ir_node *equivalent_node_neutral_zero(ir_node *n)
828 ir_node *a = get_binop_left(n);
829 ir_node *b = get_binop_right(n);
834 /* After running compute_node there is only one constant predecessor.
835 Find this predecessors value and remember the other node: */
836 if ((tv = value_of(a)) != tarval_bad) {
838 } else if ((tv = value_of(b)) != tarval_bad) {
843 /* If this predecessors constant value is zero, the operation is
844 * unnecessary. Remove it.
846 * Beware: If n is a Add, the mode of on and n might be different
847 * which happens in this rare construction: NULL + 3.
848 * Then, a Conv would be needed which we cannot include here.
850 if (classify_tarval (tv) == TV_CLASSIFY_NULL) {
851 if (get_irn_mode(on) == get_irn_mode(n)) {
854 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_0);
859 } /* equivalent_node_neutral_zero */
862 * Eor is commutative and has neutral 0.
864 #define equivalent_node_Eor equivalent_node_neutral_zero
867 * Optimize a - 0 and (a - x) + x (for modes with wrap-around).
869 * The second one looks strange, but this construct
870 * is used heavily in the LCC sources :-).
872 * Beware: The Mode of an Add may be different than the mode of its
873 * predecessors, so we could not return a predecessors in all cases.
875 static ir_node *equivalent_node_Add(ir_node *n) {
877 ir_node *left, *right;
878 ir_mode *mode = get_irn_mode(n);
880 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
881 if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
884 n = equivalent_node_neutral_zero(n);
888 left = get_Add_left(n);
889 right = get_Add_right(n);
891 if (get_irn_op(left) == op_Sub) {
892 if (get_Sub_right(left) == right) {
895 n = get_Sub_left(left);
896 if (mode == get_irn_mode(n)) {
897 DBG_OPT_ALGSIM1(oldn, left, right, n, FS_OPT_ADD_SUB);
902 if (get_irn_op(right) == op_Sub) {
903 if (get_Sub_right(right) == left) {
906 n = get_Sub_left(right);
907 if (mode == get_irn_mode(n)) {
908 DBG_OPT_ALGSIM1(oldn, left, right, n, FS_OPT_ADD_SUB);
914 } /* equivalent_node_Add */
917 * optimize operations that are not commutative but have neutral 0 on left,
920 static ir_node *equivalent_node_left_zero(ir_node *n) {
923 ir_node *a = get_binop_left(n);
924 ir_node *b = get_binop_right(n);
926 if (classify_tarval(value_of(b)) == TV_CLASSIFY_NULL) {
929 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_0);
932 } /* equivalent_node_left_zero */
934 #define equivalent_node_Shl equivalent_node_left_zero
935 #define equivalent_node_Shr equivalent_node_left_zero
936 #define equivalent_node_Shrs equivalent_node_left_zero
937 #define equivalent_node_Rot equivalent_node_left_zero
940 * Optimize a - 0 and (a + x) - x (for modes with wrap-around).
942 * The second one looks strange, but this construct
943 * is used heavily in the LCC sources :-).
945 * Beware: The Mode of a Sub may be different than the mode of its
946 * predecessors, so we could not return a predecessors in all cases.
948 static ir_node *equivalent_node_Sub(ir_node *n) {
951 ir_mode *mode = get_irn_mode(n);
953 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
954 if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
958 b = get_Sub_right(n);
960 /* Beware: modes might be different */
961 if (classify_tarval(value_of(b)) == TV_CLASSIFY_NULL) {
962 if (mode == get_irn_mode(a)) {
965 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_0);
967 } else if (get_irn_op(a) == op_Add) {
968 if (mode_wrap_around(mode)) {
969 ir_node *left = get_Add_left(a);
970 ir_node *right = get_Add_right(a);
973 if (mode == get_irn_mode(right)) {
975 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
977 } else if (right == b) {
978 if (mode == get_irn_mode(left)) {
980 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
986 } /* equivalent_node_Sub */
990 * Optimize an "idempotent unary op", ie op(op(n)) = n.
993 * -(-a) == a, but might overflow two times.
994 * We handle it anyway here but the better way would be a
995 * flag. This would be needed for Pascal for instance.
997 static ir_node *equivalent_node_idempotent_unop(ir_node *n) {
999 ir_node *pred = get_unop_op(n);
1001 /* optimize symmetric unop */
1002 if (get_irn_op(pred) == get_irn_op(n)) {
1003 n = get_unop_op(pred);
1004 DBG_OPT_ALGSIM2(oldn, pred, n);
1007 } /* equivalent_node_idempotent_unop */
1009 /** Optimize Not(Not(x)) == x. */
1010 #define equivalent_node_Not equivalent_node_idempotent_unop
1012 /** --x == x ??? Is this possible or can --x raise an
1013 out of bounds exception if min =! max? */
1014 #define equivalent_node_Minus equivalent_node_idempotent_unop
1017 * Optimize a * 1 = 1 * a = a.
1019 static ir_node *equivalent_node_Mul(ir_node *n) {
1021 ir_node *a = get_Mul_left(n);
1022 ir_node *b = get_Mul_right(n);
1024 /* Mul is commutative and has again an other neutral element. */
1025 if (classify_tarval(value_of(a)) == TV_CLASSIFY_ONE) {
1027 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
1028 } else if (classify_tarval(value_of(b)) == TV_CLASSIFY_ONE) {
1030 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
1033 } /* equivalent_node_Mul */
1036 * Optimize a / 1 = a.
1038 static ir_node *equivalent_node_Div(ir_node *n) {
1039 ir_node *a = get_Div_left(n);
1040 ir_node *b = get_Div_right(n);
1042 /* Div is not commutative. */
1043 if (classify_tarval(value_of(b)) == TV_CLASSIFY_ONE) { /* div(x, 1) == x */
1044 /* Turn Div into a tuple (mem, bad, a) */
1045 ir_node *mem = get_Div_mem(n);
1046 turn_into_tuple(n, pn_Div_max);
1047 set_Tuple_pred(n, pn_Div_M, mem);
1048 set_Tuple_pred(n, pn_Div_X_except, new_Bad()); /* no exception */
1049 set_Tuple_pred(n, pn_Div_res, a);
1052 } /* equivalent_node_Div */
1055 * Optimize a / 1.0 = a.
1057 static ir_node *equivalent_node_Quot(ir_node *n) {
1058 ir_node *a = get_Quot_left(n);
1059 ir_node *b = get_Quot_right(n);
1061 /* Div is not commutative. */
1062 if (classify_tarval(value_of(b)) == TV_CLASSIFY_ONE) { /* Quot(x, 1) == x */
1063 /* Turn Quot into a tuple (mem, bad, a) */
1064 ir_node *mem = get_Quot_mem(n);
1065 turn_into_tuple(n, pn_Quot_max);
1066 set_Tuple_pred(n, pn_Quot_M, mem);
1067 set_Tuple_pred(n, pn_Quot_X_except, new_Bad()); /* no exception */
1068 set_Tuple_pred(n, pn_Quot_res, a);
1071 } /* equivalent_node_Quot */
1074 * Optimize a / 1 = a.
1076 static ir_node *equivalent_node_DivMod(ir_node *n) {
1077 ir_node *a = get_DivMod_left(n);
1078 ir_node *b = get_DivMod_right(n);
1080 /* Div is not commutative. */
1081 if (classify_tarval(value_of(b)) == TV_CLASSIFY_ONE) { /* div(x, 1) == x */
1082 /* Turn DivMod into a tuple (mem, bad, a, 0) */
1083 ir_node *mem = get_Div_mem(n);
1084 ir_mode *mode = get_irn_mode(b);
1086 turn_into_tuple(n, pn_DivMod_max);
1087 set_Tuple_pred(n, pn_DivMod_M, mem);
1088 set_Tuple_pred(n, pn_DivMod_X_except, new_Bad()); /* no exception */
1089 set_Tuple_pred(n, pn_DivMod_res_div, a);
1090 set_Tuple_pred(n, pn_DivMod_res_mod, new_Const(mode, get_mode_null(mode)));
1093 } /* equivalent_node_DivMod */
1096 * Use algebraic simplification a | a = a | 0 = 0 | a = a.
1098 static ir_node *equivalent_node_Or(ir_node *n) {
1101 ir_node *a = get_Or_left(n);
1102 ir_node *b = get_Or_right(n);
1105 n = a; /* Or has it's own neutral element */
1106 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_OR);
1107 } else if (classify_tarval(value_of(a)) == TV_CLASSIFY_NULL) {
1109 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_OR);
1110 } else if (classify_tarval(value_of(b)) == TV_CLASSIFY_NULL) {
1112 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_OR);
1116 } /* equivalent_node_Or */
1119 * Optimize a & 0b1...1 = 0b1...1 & a = a & a = a.
1121 static ir_node *equivalent_node_And(ir_node *n) {
1124 ir_node *a = get_And_left(n);
1125 ir_node *b = get_And_right(n);
1128 n = a; /* And has it's own neutral element */
1129 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_AND);
1130 } else if (classify_tarval(value_of(a)) == TV_CLASSIFY_ALL_ONE) {
1132 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1133 } else if (classify_tarval(value_of(b)) == TV_CLASSIFY_ALL_ONE) {
1135 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1138 } /* equivalent_node_And */
1141 * Try to remove useless Conv's:
1143 static ir_node *equivalent_node_Conv(ir_node *n) {
1145 ir_node *a = get_Conv_op(n);
1148 ir_mode *n_mode = get_irn_mode(n);
1149 ir_mode *a_mode = get_irn_mode(a);
1151 if (n_mode == a_mode) { /* No Conv necessary */
1152 /* leave strict floating point Conv's */
1153 if (get_Conv_strict(n))
1156 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CONV);
1157 } else if (get_irn_op(a) == op_Conv) { /* Conv(Conv(b)) */
1161 n_mode = get_irn_mode(n);
1162 b_mode = get_irn_mode(b);
1164 if (n_mode == b_mode) {
1165 if (n_mode == mode_b) {
1166 n = b; /* Convb(Conv*(xxxb(...))) == xxxb(...) */
1167 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
1168 } else if (mode_is_int(n_mode) || mode_is_character(n_mode)) {
1169 if (smaller_mode(b_mode, a_mode)){
1170 n = b; /* ConvS(ConvL(xxxS(...))) == xxxS(...) */
1171 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
1177 } /* equivalent_node_Conv */
1180 * A Cast may be removed if the type of the previous node
1181 * is already the type of the Cast.
1183 static ir_node *equivalent_node_Cast(ir_node *n) {
1185 ir_node *pred = get_Cast_op(n);
1187 if (get_irn_type(pred) == get_Cast_type(n)) {
1189 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CAST);
1192 } /* equivalent_node_Cast */
1195 * Several optimizations:
1196 * - no Phi in start block.
1197 * - remove Id operators that are inputs to Phi
1198 * - fold Phi-nodes, iff they have only one predecessor except
1201 static ir_node *equivalent_node_Phi(ir_node *n) {
1205 ir_node *block = NULL; /* to shutup gcc */
1206 ir_node *first_val = NULL; /* to shutup gcc */
1208 if (!get_opt_normalize()) return n;
1210 n_preds = get_Phi_n_preds(n);
1212 block = get_nodes_block(n);
1213 /* @@@ fliegt 'raus, sollte aber doch immer wahr sein!!!
1214 assert(get_irn_arity(block) == n_preds && "phi in wrong block!"); */
1215 if ((is_Block_dead(block)) || /* Control dead */
1216 (block == get_irg_start_block(current_ir_graph))) /* There should be no Phi nodes */
1217 return new_Bad(); /* in the Start Block. */
1219 if (n_preds == 0) return n; /* Phi of dead Region without predecessors. */
1221 /* If the Block has a Bad pred, we also have one. */
1222 for (i = 0; i < n_preds; ++i)
1223 if (is_Bad(get_Block_cfgpred(block, i)))
1224 set_Phi_pred(n, i, new_Bad());
1226 /* Find first non-self-referencing input */
1227 for (i = 0; i < n_preds; ++i) {
1228 first_val = get_Phi_pred(n, i);
1229 if ( (first_val != n) /* not self pointer */
1231 && (! is_Bad(first_val))
1233 ) { /* value not dead */
1234 break; /* then found first value. */
1239 /* A totally Bad or self-referencing Phi (we didn't break the above loop) */
1243 /* search for rest of inputs, determine if any of these
1244 are non-self-referencing */
1245 while (++i < n_preds) {
1246 ir_node *scnd_val = get_Phi_pred(n, i);
1247 if ( (scnd_val != n)
1248 && (scnd_val != first_val)
1250 && (! is_Bad(scnd_val))
1258 /* Fold, if no multiple distinct non-self-referencing inputs */
1260 DBG_OPT_PHI(oldn, n);
1263 } /* equivalent_node_Phi */
1266 * Several optimizations:
1267 * - no Sync in start block.
1268 * - fold Sync-nodes, iff they have only one predecessor except
1271 static ir_node *equivalent_node_Sync(ir_node *n) {
1275 ir_node *first_val = NULL; /* to shutup gcc */
1277 if (!get_opt_normalize()) return n;
1279 n_preds = get_Sync_n_preds(n);
1281 /* Find first non-self-referencing input */
1282 for (i = 0; i < n_preds; ++i) {
1283 first_val = get_Sync_pred(n, i);
1284 if ((first_val != n) /* not self pointer */ &&
1285 (! is_Bad(first_val))
1286 ) { /* value not dead */
1287 break; /* then found first value. */
1292 /* A totally Bad or self-referencing Sync (we didn't break the above loop) */
1295 /* search the rest of inputs, determine if any of these
1296 are non-self-referencing */
1297 while (++i < n_preds) {
1298 ir_node *scnd_val = get_Sync_pred(n, i);
1299 if ((scnd_val != n) &&
1300 (scnd_val != first_val) &&
1301 (! is_Bad(scnd_val))
1307 /* Fold, if no multiple distinct non-self-referencing inputs */
1309 DBG_OPT_SYNC(oldn, n);
1312 } /* equivalent_node_Sync */
1315 * Optimize Proj(Tuple) and gigo() for ProjX in Bad block,
1316 * ProjX(Load) and ProjX(Store).
1318 static ir_node *equivalent_node_Proj(ir_node *n) {
1320 ir_node *a = get_Proj_pred(n);
1322 if ( get_irn_op(a) == op_Tuple) {
1323 /* Remove the Tuple/Proj combination. */
1324 if ( get_Proj_proj(n) <= get_Tuple_n_preds(a) ) {
1325 n = get_Tuple_pred(a, get_Proj_proj(n));
1326 DBG_OPT_TUPLE(oldn, a, n);
1328 assert(0); /* This should not happen! */
1331 } else if (get_irn_mode(n) == mode_X) {
1332 if (is_Block_dead(get_nodes_block(skip_Proj(n)))) {
1333 /* Remove dead control flow -- early gigo(). */
1335 } else if (get_opt_ldst_only_null_ptr_exceptions()) {
1336 ir_op *op = get_irn_op(a);
1338 if (op == op_Load || op == op_Store) {
1339 /* get the load/store address */
1340 ir_node *addr = get_irn_n(a, 1);
1343 if (value_not_null(addr, &confirm)) {
1344 if (confirm == NULL) {
1345 /* this node may float if it did not depend on a Confirm */
1346 set_irn_pinned(a, op_pin_state_floats);
1356 } /* equivalent_node_Proj */
1361 static ir_node *equivalent_node_Id(ir_node *n) {
1366 } while (get_irn_op(n) == op_Id);
1368 DBG_OPT_ID(oldn, n);
1370 } /* equivalent_node_Id */
1375 static ir_node *equivalent_node_Mux(ir_node *n)
1377 ir_node *oldn = n, *sel = get_Mux_sel(n);
1378 tarval *ts = value_of(sel);
1380 /* Mux(true, f, t) == t */
1381 if (ts == tarval_b_true) {
1382 n = get_Mux_true(n);
1383 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_C);
1385 /* Mux(false, f, t) == f */
1386 else if (ts == tarval_b_false) {
1387 n = get_Mux_false(n);
1388 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_C);
1390 /* Mux(v, x, x) == x */
1391 else if (get_Mux_false(n) == get_Mux_true(n)) {
1392 n = get_Mux_true(n);
1393 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_EQ);
1395 else if (get_irn_op(sel) == op_Proj && !mode_honor_signed_zeros(get_irn_mode(n))) {
1396 ir_node *cmp = get_Proj_pred(sel);
1397 long proj_nr = get_Proj_proj(sel);
1398 ir_node *b = get_Mux_false(n);
1399 ir_node *a = get_Mux_true(n);
1402 * Note: normalization puts the constant on the right site,
1403 * so we check only one case.
1405 * Note further that these optimization work even for floating point
1406 * with NaN's because -NaN == NaN.
1407 * However, if +0 and -0 is handled differently, we cannot use the first one.
1409 if (get_irn_op(cmp) == op_Cmp && get_Cmp_left(cmp) == a) {
1410 if (classify_Const(get_Cmp_right(cmp)) == CNST_NULL) {
1411 /* Mux(a CMP 0, X, a) */
1412 if (get_irn_op(b) == op_Minus && get_Minus_op(b) == a) {
1413 /* Mux(a CMP 0, -a, a) */
1414 if (proj_nr == pn_Cmp_Eq) {
1415 /* Mux(a == 0, -a, a) ==> -a */
1417 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1418 } else if (proj_nr == pn_Cmp_Lg || proj_nr == pn_Cmp_Ne) {
1419 /* Mux(a != 0, -a, a) ==> a */
1421 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1423 } else if (classify_Const(b) == CNST_NULL) {
1424 /* Mux(a CMP 0, 0, a) */
1425 if (proj_nr == pn_Cmp_Lg || proj_nr == pn_Cmp_Ne) {
1426 /* Mux(a != 0, 0, a) ==> a */
1428 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1429 } else if (proj_nr == pn_Cmp_Eq) {
1430 /* Mux(a == 0, 0, a) ==> 0 */
1432 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1439 } /* equivalent_node_Mux */
1442 * Returns a equivalent node of a Psi: if a condition is true
1443 * and all previous conditions are false we know its value.
1444 * If all conditions are false its value is the default one.
1446 static ir_node *equivalent_node_Psi(ir_node *n) {
1448 return equivalent_node_Mux(n);
1450 } /* equivalent_node_Psi */
1453 * Optimize -a CMP -b into b CMP a.
1454 * This works only for for modes where unary Minus
1456 * Note that two-complement integers can Overflow
1457 * so it will NOT work.
1459 * For == and != can be handled in Proj(Cmp)
1461 static ir_node *equivalent_node_Cmp(ir_node *n) {
1462 ir_node *left = get_Cmp_left(n);
1463 ir_node *right = get_Cmp_right(n);
1465 if (get_irn_op(left) == op_Minus && get_irn_op(right) == op_Minus &&
1466 !mode_overflow_on_unary_Minus(get_irn_mode(left))) {
1467 left = get_Minus_op(left);
1468 right = get_Minus_op(right);
1469 set_Cmp_left(n, right);
1470 set_Cmp_right(n, left);
1473 } /* equivalent_node_Cmp */
1476 * Remove Confirm nodes if setting is on.
1477 * Replace Confirms(x, '=', Constlike) by Constlike.
1479 static ir_node *equivalent_node_Confirm(ir_node *n) {
1480 ir_node *pred = get_Confirm_value(n);
1481 pn_Cmp pnc = get_Confirm_cmp(n);
1483 if (get_irn_op(pred) == op_Confirm && pnc == get_Confirm_cmp(pred)) {
1485 * rare case: two identical Confirms one after another,
1486 * replace the second one with the first.
1490 if (pnc == pn_Cmp_Eq) {
1491 ir_node *bound = get_Confirm_bound(n);
1494 * Optimize a rare case:
1495 * Confirm(x, '=', Constlike) ==> Constlike
1497 if (is_irn_constlike(bound)) {
1498 DBG_OPT_CONFIRM(n, bound);
1502 return get_opt_remove_confirm() ? get_Confirm_value(n) : n;
1506 * Optimize CopyB(mem, x, x) into a Nop.
1508 static ir_node *equivalent_node_CopyB(ir_node *n) {
1509 ir_node *a = get_CopyB_dst(n);
1510 ir_node *b = get_CopyB_src(n);
1513 /* Turn CopyB into a tuple (mem, bad, bad) */
1514 ir_node *mem = get_CopyB_mem(n);
1515 turn_into_tuple(n, pn_CopyB_max);
1516 set_Tuple_pred(n, pn_CopyB_M, mem);
1517 set_Tuple_pred(n, pn_CopyB_X_except, new_Bad()); /* no exception */
1518 set_Tuple_pred(n, pn_CopyB_M_except, new_Bad());
1521 } /* equivalent_node_CopyB */
1524 * Optimize Bounds(idx, idx, upper) into idx.
1526 static ir_node *equivalent_node_Bound(ir_node *n) {
1527 ir_node *idx = get_Bound_index(n);
1528 ir_node *lower = get_Bound_lower(n);
1531 /* By definition lower < upper, so if idx == lower -->
1532 lower <= idx && idx < upper */
1534 /* Turn Bound into a tuple (mem, bad, idx) */
1537 ir_node *pred = skip_Proj(idx);
1539 if (get_irn_op(pred) == op_Bound) {
1541 * idx was Bounds_check previously, it is still valid if
1542 * lower <= pred_lower && pred_upper <= upper.
1544 ir_node *upper = get_Bound_upper(n);
1545 if (get_Bound_lower(pred) == lower &&
1546 get_Bound_upper(pred) == upper) {
1548 * One could expect that we simply return the previous
1549 * Bound here. However, this would be wrong, as we could
1550 * add an exception Proj to a new location than.
1551 * So, we must turn in into a tuple
1558 /* Turn Bound into a tuple (mem, bad, idx) */
1559 ir_node *mem = get_Bound_mem(n);
1560 turn_into_tuple(n, pn_Bound_max);
1561 set_Tuple_pred(n, pn_Bound_M, mem);
1562 set_Tuple_pred(n, pn_Bound_X_except, new_Bad()); /* no exception */
1563 set_Tuple_pred(n, pn_Bound_res, idx);
1566 } /* equivalent_node_Bound */
1569 * equivalent_node() returns a node equivalent to input n. It skips all nodes that
1570 * perform no actual computation, as, e.g., the Id nodes. It does not create
1571 * new nodes. It is therefore safe to free n if the node returned is not n.
1572 * If a node returns a Tuple we can not just skip it. If the size of the
1573 * in array fits, we transform n into a tuple (e.g., Div).
1575 ir_node *equivalent_node(ir_node *n) {
1576 if (n->op->ops.equivalent_node)
1577 return n->op->ops.equivalent_node(n);
1579 } /* equivalent_node */
1582 * Sets the default equivalent node operation for an ir_op_ops.
1584 * @param code the opcode for the default operation
1585 * @param ops the operations initialized
1590 static ir_op_ops *firm_set_default_equivalent_node(ir_opcode code, ir_op_ops *ops)
1594 ops->equivalent_node = equivalent_node_##a; \
1634 } /* firm_set_default_equivalent_node */
1637 * Do node specific optimizations of nodes predecessors.
1639 static void optimize_preds(ir_node *n) {
1640 switch (get_irn_opcode(n)) {
1642 case iro_Cmp: { /* We don't want Cast as input to Cmp. */
1643 ir_node *a = get_Cmp_left(n), *b = get_Cmp_right(n);
1645 if (get_irn_op(a) == op_Cast) {
1649 if (get_irn_op(b) == op_Cast) {
1651 set_Cmp_right(n, b);
1658 } /* optimize_preds */
1661 * Returns non-zero if a node is a Phi node
1662 * with all predecessors constant.
1664 static int is_const_Phi(ir_node *n) {
1669 for (i = get_irn_arity(n) - 1; i >= 0; --i)
1670 if (! is_Const(get_irn_n(n, i)))
1673 } /* is_const_Phi */
1676 * Apply an evaluator on a binop with a constant operators (and one Phi).
1678 * @param phi the Phi node
1679 * @param other the other operand
1680 * @param eval an evaluator function
1681 * @param left if non-zero, other is the left operand, else the right
1683 * @return a new Phi node if the conversion was successful, NULL else
1685 static ir_node *apply_binop_on_phi(ir_node *phi, tarval *other, tarval *(*eval)(tarval *, tarval *), int left) {
1691 int i, n = get_irn_arity(phi);
1693 NEW_ARR_A(void *, res, n);
1695 for (i = 0; i < n; ++i) {
1696 pred = get_irn_n(phi, i);
1697 tv = get_Const_tarval(pred);
1698 tv = eval(other, tv);
1700 if (tv == tarval_bad) {
1701 /* folding failed, bad */
1707 for (i = 0; i < n; ++i) {
1708 pred = get_irn_n(phi, i);
1709 tv = get_Const_tarval(pred);
1710 tv = eval(tv, other);
1712 if (tv == tarval_bad) {
1713 /* folding failed, bad */
1719 mode = get_irn_mode(phi);
1720 irg = current_ir_graph;
1721 for (i = 0; i < n; ++i) {
1722 pred = get_irn_n(phi, i);
1723 res[i] = new_r_Const_type(irg, get_irg_start_block(irg),
1724 mode, res[i], get_Const_type(pred));
1726 return new_r_Phi(irg, get_nodes_block(phi), n, (ir_node **)res, mode);
1727 } /* apply_binop_on_phi */
1730 * Apply an evaluator on a unop with a constant operator (a Phi).
1732 * @param phi the Phi node
1733 * @param eval an evaluator function
1735 * @return a new Phi node if the conversion was successful, NULL else
1737 static ir_node *apply_unop_on_phi(ir_node *phi, tarval *(*eval)(tarval *)) {
1743 int i, n = get_irn_arity(phi);
1745 NEW_ARR_A(void *, res, n);
1746 for (i = 0; i < n; ++i) {
1747 pred = get_irn_n(phi, i);
1748 tv = get_Const_tarval(pred);
1751 if (tv == tarval_bad) {
1752 /* folding failed, bad */
1757 mode = get_irn_mode(phi);
1758 irg = current_ir_graph;
1759 for (i = 0; i < n; ++i) {
1760 pred = get_irn_n(phi, i);
1761 res[i] = new_r_Const_type(irg, get_irg_start_block(irg),
1762 mode, res[i], get_Const_type(pred));
1764 return new_r_Phi(irg, get_nodes_block(phi), n, (ir_node **)res, mode);
1765 } /* apply_unop_on_phi */
1768 * Transform AddP(P, ConvIs(Iu)), AddP(P, ConvIu(Is)) and
1769 * SubP(P, ConvIs(Iu)), SubP(P, ConvIu(Is)).
1770 * If possible, remove the Conv's.
1772 static ir_node *transform_node_AddSub(ir_node *n) {
1773 ir_mode *mode = get_irn_mode(n);
1775 if (mode_is_reference(mode)) {
1776 ir_node *left = get_binop_left(n);
1777 ir_node *right = get_binop_right(n);
1778 int ref_bits = get_mode_size_bits(mode);
1780 if (get_irn_op(left) == op_Conv) {
1781 ir_mode *mode = get_irn_mode(left);
1782 int bits = get_mode_size_bits(mode);
1784 if (ref_bits == bits &&
1785 mode_is_int(mode) &&
1786 get_mode_arithmetic(mode) == irma_twos_complement) {
1787 ir_node *pre = get_Conv_op(left);
1788 ir_mode *pre_mode = get_irn_mode(pre);
1790 if (mode_is_int(pre_mode) &&
1791 get_mode_size_bits(pre_mode) == bits &&
1792 get_mode_arithmetic(pre_mode) == irma_twos_complement) {
1793 /* ok, this conv just changes to sign, moreover the calculation
1794 * is done with same number of bits as our address mode, so
1795 * we can ignore the conv as address calculation can be viewed
1796 * as either signed or unsigned
1798 set_binop_left(n, pre);
1803 if (get_irn_op(right) == op_Conv) {
1804 ir_mode *mode = get_irn_mode(right);
1805 int bits = get_mode_size_bits(mode);
1807 if (ref_bits == bits &&
1808 mode_is_int(mode) &&
1809 get_mode_arithmetic(mode) == irma_twos_complement) {
1810 ir_node *pre = get_Conv_op(right);
1811 ir_mode *pre_mode = get_irn_mode(pre);
1813 if (mode_is_int(pre_mode) &&
1814 get_mode_size_bits(pre_mode) == bits &&
1815 get_mode_arithmetic(pre_mode) == irma_twos_complement) {
1816 /* ok, this conv just changes to sign, moreover the calculation
1817 * is done with same number of bits as our address mode, so
1818 * we can ignore the conv as address calculation can be viewed
1819 * as either signed or unsigned
1821 set_binop_right(n, pre);
1827 } /* transform_node_AddSub */
1829 #define HANDLE_BINOP_PHI(op,a,b,c) \
1831 if (is_Const(b) && is_const_Phi(a)) { \
1832 /* check for Op(Phi, Const) */ \
1833 c = apply_binop_on_phi(a, get_Const_tarval(b), op, 0); \
1835 else if (is_Const(a) && is_const_Phi(b)) { \
1836 /* check for Op(Const, Phi) */ \
1837 c = apply_binop_on_phi(b, get_Const_tarval(a), op, 1); \
1840 DBG_OPT_ALGSIM0(oldn, c, FS_OPT_CONST_PHI); \
1844 #define HANDLE_UNOP_PHI(op,a,c) \
1846 if (is_const_Phi(a)) { \
1847 /* check for Op(Phi) */ \
1848 c = apply_unop_on_phi(a, op); \
1851 DBG_OPT_ALGSIM0(oldn, c, FS_OPT_CONST_PHI); \
1857 * Do the AddSub optimization, then Transform
1858 * Constant folding on Phi
1859 * Add(a,a) -> Mul(a, 2)
1860 * Add(Mul(a, x), a) -> Mul(a, x+1)
1861 * if the mode is integer or float.
1862 * Transform Add(a,-b) into Sub(a,b).
1863 * Reassociation might fold this further.
1865 static ir_node *transform_node_Add(ir_node *n) {
1867 ir_node *a, *b, *c, *oldn = n;
1869 n = transform_node_AddSub(n);
1871 a = get_Add_left(n);
1872 b = get_Add_right(n);
1874 HANDLE_BINOP_PHI(tarval_add, a,b,c);
1876 mode = get_irn_mode(n);
1878 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
1879 if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
1882 if (mode_is_num(mode)) {
1884 ir_node *block = get_irn_n(n, -1);
1887 get_irn_dbg_info(n),
1891 new_r_Const_long(current_ir_graph, block, mode, 2),
1893 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_A);
1894 } else if (get_irn_op(a) == op_Minus) {
1896 get_irn_dbg_info(n),
1902 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_MINUS_B);
1903 } else if (get_irn_op(b) == op_Minus) {
1905 get_irn_dbg_info(n),
1911 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_MINUS_B);
1913 /* do NOT execute this code if reassociation is enabled, it does the inverse! */
1914 else if (!get_opt_reassociation() && get_irn_op(a) == op_Mul) {
1915 ir_node *ma = get_Mul_left(a);
1916 ir_node *mb = get_Mul_right(a);
1919 ir_node *blk = get_irn_n(n, -1);
1921 get_irn_dbg_info(n), current_ir_graph, blk,
1924 get_irn_dbg_info(n), current_ir_graph, blk,
1926 new_r_Const_long(current_ir_graph, blk, mode, 1),
1929 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_MUL_A_X_A);
1930 } else if (b == mb) {
1931 ir_node *blk = get_irn_n(n, -1);
1933 get_irn_dbg_info(n), current_ir_graph, blk,
1936 get_irn_dbg_info(n), current_ir_graph, blk,
1938 new_r_Const_long(current_ir_graph, blk, mode, 1),
1941 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_MUL_A_X_A);
1944 /* do NOT execute this code if reassociation is enabled, it does the inverse! */
1945 else if (!get_opt_reassociation() && get_irn_op(b) == op_Mul) {
1946 ir_node *ma = get_Mul_left(b);
1947 ir_node *mb = get_Mul_right(b);
1950 ir_node *blk = get_irn_n(n, -1);
1952 get_irn_dbg_info(n), current_ir_graph, blk,
1955 get_irn_dbg_info(n), current_ir_graph, blk,
1957 new_r_Const_long(current_ir_graph, blk, mode, 1),
1960 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_MUL_A_X_A);
1961 } else if (a == mb) {
1962 ir_node *blk = get_irn_n(n, -1);
1964 get_irn_dbg_info(n), current_ir_graph, blk,
1967 get_irn_dbg_info(n), current_ir_graph, blk,
1969 new_r_Const_long(current_ir_graph, blk, mode, 1),
1972 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_MUL_A_X_A);
1977 } /* transform_node_Add */
1980 * Do the AddSub optimization, then Transform
1981 * Constant folding on Phi
1982 * Sub(0,a) -> Minus(a)
1983 * Sub(Mul(a, x), a) -> Mul(a, x-1)
1984 * Sub(Sub(x, y), b) -> Sub(x, Add(y,b))
1986 static ir_node *transform_node_Sub(ir_node *n) {
1991 n = transform_node_AddSub(n);
1993 a = get_Sub_left(n);
1994 b = get_Sub_right(n);
1996 HANDLE_BINOP_PHI(tarval_sub, a,b,c);
1998 mode = get_irn_mode(n);
2000 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
2001 if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
2004 /* Beware of Sub(P, P) which cannot be optimized into a simple Minus ... */
2005 if (mode_is_num(mode) && mode == get_irn_mode(a) && (classify_Const(a) == CNST_NULL)) {
2007 get_irn_dbg_info(n),
2012 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_0_A);
2014 /* do NOT execute this code if reassociation is enabled, it does the inverse! */
2015 else if (get_opt_reassociation() && get_irn_op(a) == op_Mul) {
2016 ir_node *ma = get_Mul_left(a);
2017 ir_node *mb = get_Mul_right(a);
2020 ir_node *blk = get_irn_n(n, -1);
2022 get_irn_dbg_info(n),
2023 current_ir_graph, blk,
2026 get_irn_dbg_info(n),
2027 current_ir_graph, blk,
2029 new_r_Const_long(current_ir_graph, blk, mode, 1),
2032 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MUL_A_X_A);
2033 } else if (mb == b) {
2034 ir_node *blk = get_irn_n(n, -1);
2036 get_irn_dbg_info(n),
2037 current_ir_graph, blk,
2040 get_irn_dbg_info(n),
2041 current_ir_graph, blk,
2043 new_r_Const_long(current_ir_graph, blk, mode, 1),
2046 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MUL_A_X_A);
2048 } else if (get_irn_op(a) == op_Sub) {
2049 ir_node *x = get_Sub_left(a);
2050 ir_node *y = get_Sub_right(a);
2051 ir_node *blk = get_irn_n(n, -1);
2052 ir_mode *m_b = get_irn_mode(b);
2053 ir_mode *m_y = get_irn_mode(y);
2056 /* Determine the right mode for the Add. */
2059 else if (mode_is_reference(m_b))
2061 else if (mode_is_reference(m_y))
2065 * Both modes are different but none is reference,
2066 * happens for instance in SubP(SubP(P, Iu), Is).
2067 * We have two possibilities here: Cast or ignore.
2068 * Currently we ignore this case.
2073 add = new_r_Add(current_ir_graph, blk, y, b, mode);
2076 set_Sub_right(n, add);
2077 DBG_OPT_ALGSIM0(n, n, FS_OPT_SUB_SUB_X_Y_Z);
2081 } /* transform_node_Sub */
2084 * Transform Mul(a,-1) into -a.
2085 * Do constant evaluation of Phi nodes.
2086 * Do architecture dependent optimizations on Mul nodes
2088 static ir_node *transform_node_Mul(ir_node *n) {
2089 ir_node *c, *oldn = n;
2090 ir_node *a = get_Mul_left(n);
2091 ir_node *b = get_Mul_right(n);
2094 HANDLE_BINOP_PHI(tarval_mul, a,b,c);
2096 mode = get_irn_mode(n);
2097 if (mode_is_signed(mode)) {
2100 if (value_of(a) == get_mode_minus_one(mode))
2102 else if (value_of(b) == get_mode_minus_one(mode))
2105 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1), r, mode);
2106 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2110 return arch_dep_replace_mul_with_shifts(n);
2111 } /* transform_node_Mul */
2114 * Transform a Div Node.
2116 static ir_node *transform_node_Div(ir_node *n) {
2117 tarval *tv = value_of(n);
2120 /* BEWARE: it is NOT possible to optimize a/a to 1, as this may cause a exception */
2122 if (tv != tarval_bad) {
2123 value = new_Const(get_tarval_mode(tv), tv);
2125 DBG_OPT_CSTEVAL(n, value);
2126 } else /* Try architecture dependent optimization */
2127 value = arch_dep_replace_div_by_const(n);
2130 /* Turn Div into a tuple (mem, bad, value) */
2131 ir_node *mem = get_Div_mem(n);
2133 turn_into_tuple(n, pn_Div_max);
2134 set_Tuple_pred(n, pn_Div_M, mem);
2135 set_Tuple_pred(n, pn_Div_X_except, new_Bad());
2136 set_Tuple_pred(n, pn_Div_res, value);
2139 } /* transform_node_Div */
2142 * Transform a Mod node.
2144 static ir_node *transform_node_Mod(ir_node *n) {
2145 tarval *tv = value_of(n);
2148 /* BEWARE: it is NOT possible to optimize a%a to 0, as this may cause a exception */
2150 if (tv != tarval_bad) {
2151 value = new_Const(get_tarval_mode(tv), tv);
2153 DBG_OPT_CSTEVAL(n, value);
2154 } else /* Try architecture dependent optimization */
2155 value = arch_dep_replace_mod_by_const(n);
2158 /* Turn Mod into a tuple (mem, bad, value) */
2159 ir_node *mem = get_Mod_mem(n);
2161 turn_into_tuple(n, pn_Mod_max);
2162 set_Tuple_pred(n, pn_Mod_M, mem);
2163 set_Tuple_pred(n, pn_Mod_X_except, new_Bad());
2164 set_Tuple_pred(n, pn_Mod_res, value);
2167 } /* transform_node_Mod */
2170 * Transform a DivMod node.
2172 static ir_node *transform_node_DivMod(ir_node *n) {
2175 ir_node *a = get_DivMod_left(n);
2176 ir_node *b = get_DivMod_right(n);
2177 ir_mode *mode = get_irn_mode(a);
2178 tarval *ta = value_of(a);
2179 tarval *tb = value_of(b);
2181 if (!(mode_is_int(mode) && mode_is_int(get_irn_mode(b))))
2184 /* BEWARE: it is NOT possible to optimize a/a to 1, as this may cause a exception */
2186 if (tb != tarval_bad) {
2187 if (tb == get_mode_one(get_tarval_mode(tb))) {
2188 b = new_Const (mode, get_mode_null(mode));
2191 DBG_OPT_CSTEVAL(n, b);
2192 } else if (ta != tarval_bad) {
2193 tarval *resa, *resb;
2194 resa = tarval_div (ta, tb);
2195 if (resa == tarval_bad) return n; /* Causes exception!!! Model by replacing through
2196 Jmp for X result!? */
2197 resb = tarval_mod (ta, tb);
2198 if (resb == tarval_bad) return n; /* Causes exception! */
2199 a = new_Const (mode, resa);
2200 b = new_Const (mode, resb);
2203 DBG_OPT_CSTEVAL(n, a);
2204 DBG_OPT_CSTEVAL(n, b);
2205 } else { /* Try architecture dependent optimization */
2206 arch_dep_replace_divmod_by_const(&a, &b, n);
2207 evaluated = a != NULL;
2209 } else if (ta == get_mode_null(mode)) {
2210 /* 0 / non-Const = 0 */
2215 if (evaluated) { /* replace by tuple */
2216 ir_node *mem = get_DivMod_mem(n);
2217 turn_into_tuple(n, pn_DivMod_max);
2218 set_Tuple_pred(n, pn_DivMod_M, mem);
2219 set_Tuple_pred(n, pn_DivMod_X_except, new_Bad()); /* no exception */
2220 set_Tuple_pred(n, pn_DivMod_res_div, a);
2221 set_Tuple_pred(n, pn_DivMod_res_mod, b);
2225 } /* transform_node_DivMod */
2228 * Optimize Abs(x) into x if x is Confirmed >= 0
2229 * Optimize Abs(x) into -x if x is Confirmed <= 0
2231 static ir_node *transform_node_Abs(ir_node *n) {
2233 ir_node *a = get_Abs_op(n);
2234 value_classify_sign sign = classify_value_sign(a);
2236 if (sign == value_classified_negative) {
2237 ir_mode *mode = get_irn_mode(n);
2240 * We can replace the Abs by -x here.
2241 * We even could add a new Confirm here.
2243 * Note that -x would create a new node, so we could
2244 * not run it in the equivalent_node() context.
2246 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph,
2247 get_irn_n(n, -1), a, mode);
2249 DBG_OPT_CONFIRM(oldn, n);
2250 } else if (sign == value_classified_positive) {
2251 /* n is positive, Abs is not needed */
2254 DBG_OPT_CONFIRM(oldn, n);
2258 } /* transform_node_Abs */
2261 * Transform a Cond node.
2263 * Replace the Cond by a Jmp if it branches on a constant
2266 static ir_node *transform_node_Cond(ir_node *n) {
2269 ir_node *a = get_Cond_selector(n);
2270 tarval *ta = value_of(a);
2272 /* we need block info which is not available in floating irgs */
2273 if (get_irg_pinned(current_ir_graph) == op_pin_state_floats)
2276 if ((ta != tarval_bad) &&
2277 (get_irn_mode(a) == mode_b) &&
2278 (get_opt_unreachable_code())) {
2279 /* It's a boolean Cond, branching on a boolean constant.
2280 Replace it by a tuple (Bad, Jmp) or (Jmp, Bad) */
2281 jmp = new_r_Jmp(current_ir_graph, get_nodes_block(n));
2282 turn_into_tuple(n, pn_Cond_max);
2283 if (ta == tarval_b_true) {
2284 set_Tuple_pred(n, pn_Cond_false, new_Bad());
2285 set_Tuple_pred(n, pn_Cond_true, jmp);
2287 set_Tuple_pred(n, pn_Cond_false, jmp);
2288 set_Tuple_pred(n, pn_Cond_true, new_Bad());
2290 /* We might generate an endless loop, so keep it alive. */
2291 add_End_keepalive(get_irg_end(current_ir_graph), get_nodes_block(n));
2294 } /* transform_node_Cond */
2299 static ir_node *transform_node_And(ir_node *n) {
2300 ir_node *c, *oldn = n;
2301 ir_node *a = get_And_left(n);
2302 ir_node *b = get_And_right(n);
2304 HANDLE_BINOP_PHI(tarval_and, a,b,c);
2306 } /* transform_node_And */
2311 static ir_node *transform_node_Eor(ir_node *n) {
2312 ir_node *c, *oldn = n;
2313 ir_node *a = get_Eor_left(n);
2314 ir_node *b = get_Eor_right(n);
2315 ir_mode *mode = get_irn_mode(n);
2317 HANDLE_BINOP_PHI(tarval_eor, a,b,c);
2321 n = new_rd_Const(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1),
2322 mode, get_mode_null(mode));
2323 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_A_A);
2324 } else if ((mode == mode_b)
2325 && (get_irn_op(a) == op_Proj)
2326 && (get_irn_mode(a) == mode_b)
2327 && (classify_tarval (value_of(b)) == TV_CLASSIFY_ONE)
2328 && (get_irn_op(get_Proj_pred(a)) == op_Cmp)) {
2329 /* The Eor negates a Cmp. The Cmp has the negated result anyways! */
2330 n = new_r_Proj(current_ir_graph, get_irn_n(n, -1), get_Proj_pred(a),
2331 mode_b, get_negated_pnc(get_Proj_proj(a), mode));
2333 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT_BOOL);
2334 } else if ((mode == mode_b)
2335 && (classify_tarval (value_of(b)) == TV_CLASSIFY_ONE)) {
2336 /* The Eor is a Not. Replace it by a Not. */
2337 /* ????!!!Extend to bitfield 1111111. */
2338 n = new_r_Not(current_ir_graph, get_irn_n(n, -1), a, mode_b);
2340 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
2344 } /* transform_node_Eor */
2349 static ir_node *transform_node_Not(ir_node *n) {
2350 ir_node *c, *oldn = n;
2351 ir_node *a = get_Not_op(n);
2353 HANDLE_UNOP_PHI(tarval_not,a,c);
2355 /* check for a boolean Not */
2356 if ( (get_irn_mode(n) == mode_b)
2357 && (get_irn_op(a) == op_Proj)
2358 && (get_irn_mode(a) == mode_b)
2359 && (get_irn_op(get_Proj_pred(a)) == op_Cmp)) {
2360 /* We negate a Cmp. The Cmp has the negated result anyways! */
2361 n = new_r_Proj(current_ir_graph, get_irn_n(n, -1), get_Proj_pred(a),
2362 mode_b, get_negated_pnc(get_Proj_proj(a), mode_b));
2363 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_CMP);
2366 } /* transform_node_Not */
2369 * Transform a Minus.
2371 static ir_node *transform_node_Minus(ir_node *n) {
2372 ir_node *c, *oldn = n;
2373 ir_node *a = get_Minus_op(n);
2375 HANDLE_UNOP_PHI(tarval_neg,a,c);
2377 } /* transform_node_Minus */
2380 * Transform a Cast_type(Const) into a new Const_type
2382 static ir_node *transform_node_Cast(ir_node *n) {
2384 ir_node *pred = get_Cast_op(n);
2385 ir_type *tp = get_irn_type(n);
2387 if (get_irn_op(pred) == op_Const && get_Const_type(pred) != tp) {
2388 n = new_rd_Const_type(NULL, current_ir_graph, get_irn_n(pred, -1), get_irn_mode(pred),
2389 get_Const_tarval(pred), tp);
2390 DBG_OPT_CSTEVAL(oldn, n);
2391 } else if ((get_irn_op(pred) == op_SymConst) && (get_SymConst_value_type(pred) != tp)) {
2392 n = new_rd_SymConst_type(NULL, current_ir_graph, get_irn_n(pred, -1), get_SymConst_symbol(pred),
2393 get_SymConst_kind(pred), tp);
2394 DBG_OPT_CSTEVAL(oldn, n);
2398 } /* transform_node_Cast */
2401 * Transform a Proj(Div) with a non-zero value.
2402 * Removes the exceptions and routes the memory to the NoMem node.
2404 static ir_node *transform_node_Proj_Div(ir_node *proj) {
2405 ir_node *n = get_Proj_pred(proj);
2406 ir_node *b = get_Div_right(n);
2410 if (value_not_zero(b, &confirm)) {
2411 /* div(x, y) && y != 0 */
2412 proj_nr = get_Proj_proj(proj);
2413 if (proj_nr == pn_Div_X_except) {
2414 /* we found an exception handler, remove it */
2415 DBG_OPT_EXC_REM(proj);
2417 } else if (proj_nr == pn_Div_M) {
2418 ir_node *res = get_Div_mem(n);
2419 ir_node *new_mem = get_irg_no_mem(current_ir_graph);
2422 /* This node can only float up to the Confirm block */
2423 new_mem = new_r_Pin(current_ir_graph, get_nodes_block(confirm), new_mem);
2425 set_irn_pinned(n, op_pin_state_floats);
2426 /* this is a Div without exception, we can remove the memory edge */
2427 set_Div_mem(n, new_mem);
2432 } /* transform_node_Proj_Div */
2435 * Transform a Proj(Mod) with a non-zero value.
2436 * Removes the exceptions and routes the memory to the NoMem node.
2438 static ir_node *transform_node_Proj_Mod(ir_node *proj) {
2439 ir_node *n = get_Proj_pred(proj);
2440 ir_node *b = get_Mod_right(n);
2444 if (value_not_zero(b, &confirm)) {
2445 /* mod(x, y) && y != 0 */
2446 proj_nr = get_Proj_proj(proj);
2448 if (proj_nr == pn_Mod_X_except) {
2449 /* we found an exception handler, remove it */
2450 DBG_OPT_EXC_REM(proj);
2452 } else if (proj_nr == pn_Mod_M) {
2453 ir_node *res = get_Mod_mem(n);
2454 ir_node *new_mem = get_irg_no_mem(current_ir_graph);
2457 /* This node can only float up to the Confirm block */
2458 new_mem = new_r_Pin(current_ir_graph, get_nodes_block(confirm), new_mem);
2460 set_irn_pinned(n, op_pin_state_floats);
2461 /* this is a Mod without exception, we can remove the memory edge */
2462 set_Mod_mem(n, get_irg_no_mem(current_ir_graph));
2464 } else if (proj_nr == pn_Mod_res && get_Mod_left(n) == b) {
2465 /* a % a = 0 if a != 0 */
2466 ir_mode *mode = get_irn_mode(proj);
2467 ir_node *res = new_Const(mode, get_mode_null(mode));
2469 DBG_OPT_CSTEVAL(n, res);
2474 } /* transform_node_Proj_Mod */
2477 * Transform a Proj(DivMod) with a non-zero value.
2478 * Removes the exceptions and routes the memory to the NoMem node.
2480 static ir_node *transform_node_Proj_DivMod(ir_node *proj) {
2481 ir_node *n = get_Proj_pred(proj);
2482 ir_node *b = get_DivMod_right(n);
2486 if (value_not_zero(b, &confirm)) {
2487 /* DivMod(x, y) && y != 0 */
2488 proj_nr = get_Proj_proj(proj);
2490 if (proj_nr == pn_DivMod_X_except) {
2491 /* we found an exception handler, remove it */
2492 DBG_OPT_EXC_REM(proj);
2494 } else if (proj_nr == pn_DivMod_M) {
2495 ir_node *res = get_DivMod_mem(n);
2496 ir_node *new_mem = get_irg_no_mem(current_ir_graph);
2499 /* This node can only float up to the Confirm block */
2500 new_mem = new_r_Pin(current_ir_graph, get_nodes_block(confirm), new_mem);
2502 set_irn_pinned(n, op_pin_state_floats);
2503 /* this is a DivMod without exception, we can remove the memory edge */
2504 set_DivMod_mem(n, get_irg_no_mem(current_ir_graph));
2506 } else if (proj_nr == pn_DivMod_res_mod && get_DivMod_left(n) == b) {
2507 /* a % a = 0 if a != 0 */
2508 ir_mode *mode = get_irn_mode(proj);
2509 ir_node *res = new_Const(mode, get_mode_null(mode));
2511 DBG_OPT_CSTEVAL(n, res);
2516 } /* transform_node_Proj_DivMod */
2519 * Optimizes jump tables (CondIs or CondIu) by removing all impossible cases.
2521 static ir_node *transform_node_Proj_Cond(ir_node *proj) {
2522 if (get_opt_unreachable_code()) {
2523 ir_node *n = get_Proj_pred(proj);
2524 ir_node *b = get_Cond_selector(n);
2526 if (mode_is_int(get_irn_mode(b))) {
2527 tarval *tb = value_of(b);
2529 if (tb != tarval_bad) {
2530 /* we have a constant switch */
2531 long num = get_Proj_proj(proj);
2533 if (num != get_Cond_defaultProj(n)) { /* we cannot optimize default Proj's yet */
2534 if (get_tarval_long(tb) == num) {
2535 /* Do NOT create a jump here, or we will have 2 control flow ops
2536 * in a block. This case is optimized away in optimize_cf(). */
2539 /* this case will NEVER be taken, kill it */
2547 } /* transform_node_Proj_Cond */
2550 * Normalizes and optimizes Cmp nodes.
2552 static ir_node *transform_node_Proj_Cmp(ir_node *proj) {
2553 if (get_opt_reassociation()) {
2554 ir_node *n = get_Proj_pred(proj);
2555 ir_node *left = get_Cmp_left(n);
2556 ir_node *right = get_Cmp_right(n);
2560 ir_mode *mode = NULL;
2561 long proj_nr = get_Proj_proj(proj);
2564 * First step: normalize the compare op
2565 * by placing the constant on the right site
2566 * or moving the lower address node to the left.
2567 * We ignore the case that both are constants
2568 * this case should be optimized away.
2570 if (get_irn_op(right) == op_Const) {
2572 } else if (get_irn_op(left) == op_Const) {
2577 proj_nr = get_inversed_pnc(proj_nr);
2579 } else if (get_irn_idx(left) > get_irn_idx(right)) {
2585 proj_nr = get_inversed_pnc(proj_nr);
2590 * Second step: Try to reduce the magnitude
2591 * of a constant. This may help to generate better code
2592 * later and may help to normalize more compares.
2593 * Of course this is only possible for integer values.
2596 mode = get_irn_mode(c);
2597 tv = get_Const_tarval(c);
2599 if (tv != tarval_bad) {
2600 /* the following optimization is possible on modes without Overflow
2601 * on Unary Minus or on == and !=:
2602 * -a CMP c ==> a swap(CMP) -c
2604 * Beware: for two-complement Overflow may occur, so only == and != can
2605 * be optimized, see this:
2606 * -MININT < 0 =/=> MININT > 0 !!!
2608 if (get_opt_constant_folding() && get_irn_op(left) == op_Minus &&
2609 (!mode_overflow_on_unary_Minus(mode) ||
2610 (mode_is_int(mode) && (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg)))) {
2611 left = get_Minus_op(left);
2612 tv = tarval_sub(get_mode_null(mode), tv);
2614 proj_nr = get_inversed_pnc(proj_nr);
2618 /* for integer modes, we have more */
2619 if (mode_is_int(mode)) {
2620 /* Ne includes Unordered which is not possible on integers.
2621 * However, frontends often use this wrong, so fix it here */
2622 if (proj_nr & pn_Cmp_Uo) {
2623 proj_nr &= ~pn_Cmp_Uo;
2624 set_Proj_proj(proj, proj_nr);
2627 /* c > 0 : a < c ==> a <= (c-1) a >= c ==> a > (c-1) */
2628 if ((proj_nr == pn_Cmp_Lt || proj_nr == pn_Cmp_Ge) &&
2629 tarval_cmp(tv, get_mode_null(mode)) == pn_Cmp_Gt) {
2630 tv = tarval_sub(tv, get_mode_one(mode));
2632 proj_nr ^= pn_Cmp_Eq;
2635 /* c < 0 : a > c ==> a >= (c+1) a <= c ==> a < (c+1) */
2636 else if ((proj_nr == pn_Cmp_Gt || proj_nr == pn_Cmp_Le) &&
2637 tarval_cmp(tv, get_mode_null(mode)) == pn_Cmp_Lt) {
2638 tv = tarval_add(tv, get_mode_one(mode));
2640 proj_nr ^= pn_Cmp_Eq;
2644 /* the following reassociations work only for == and != */
2645 if (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) {
2647 /* a-b == 0 ==> a == b, a-b != 0 ==> a != b */
2648 if (classify_tarval(tv) == TV_CLASSIFY_NULL && get_irn_op(left) == op_Sub) {
2649 right = get_Sub_right(left);
2650 left = get_Sub_left(left);
2652 tv = value_of(right);
2656 if (tv != tarval_bad) {
2657 ir_op *op = get_irn_op(left);
2659 /* a-c1 == c2 ==> a == c2+c1, a-c1 != c2 ==> a != c2+c1 */
2661 ir_node *c1 = get_Sub_right(left);
2662 tarval *tv2 = value_of(c1);
2664 if (tv2 != tarval_bad) {
2665 tv2 = tarval_add(tv, value_of(c1));
2667 if (tv2 != tarval_bad) {
2668 left = get_Sub_left(left);
2674 /* a+c1 == c2 ==> a == c2-c1, a+c1 != c2 ==> a != c2-c1 */
2675 else if (op == op_Add) {
2676 ir_node *a_l = get_Add_left(left);
2677 ir_node *a_r = get_Add_right(left);
2681 if (get_irn_op(a_l) == op_Const) {
2683 tv2 = value_of(a_l);
2686 tv2 = value_of(a_r);
2689 if (tv2 != tarval_bad) {
2690 tv2 = tarval_sub(tv, tv2);
2692 if (tv2 != tarval_bad) {
2699 /* -a == c ==> a == -c, -a != c ==> a != -c */
2700 else if (op == op_Minus) {
2701 tarval *tv2 = tarval_sub(get_mode_null(mode), tv);
2703 if (tv2 != tarval_bad) {
2704 left = get_Minus_op(left);
2711 /* the following reassociations work only for <= */
2712 else if (proj_nr == pn_Cmp_Le || proj_nr == pn_Cmp_Lt) {
2713 if (tv != tarval_bad) {
2714 ir_op *op = get_irn_op(left);
2716 /* c >= 0 : Abs(a) <= c ==> (unsigned)(a + c) <= 2*c */
2724 * optimization for AND:
2726 * And(x, C) == C ==> And(x, C) != 0
2727 * And(x, C) != C ==> And(X, C) == 0
2729 * if C is a single Bit constant.
2731 if ((proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) &&
2732 (get_irn_op(left) == op_And)) {
2733 if (is_single_bit_tarval(tv)) {
2734 /* check for Constant's match. We have check hare the tarvals,
2735 because our const might be changed */
2736 ir_node *la = get_And_left(left);
2737 ir_node *ra = get_And_right(left);
2738 if ((is_Const(la) && get_Const_tarval(la) == tv) ||
2739 (is_Const(ra) && get_Const_tarval(ra) == tv)) {
2740 /* fine: do the transformation */
2741 tv = get_mode_null(get_tarval_mode(tv));
2742 proj_nr ^= pn_Cmp_Leg;
2747 } /* tarval != bad */
2751 ir_node *block = get_irn_n(n, -1); /* Beware of get_nodes_Block() */
2753 if (changed & 2) /* need a new Const */
2754 right = new_Const(mode, tv);
2756 /* create a new compare */
2757 n = new_rd_Cmp(get_irn_dbg_info(n), current_ir_graph, block,
2760 set_Proj_pred(proj, n);
2761 set_Proj_proj(proj, proj_nr);
2765 } /* transform_node_Proj_Cmp */
2768 * Does all optimizations on nodes that must be done on it's Proj's
2769 * because of creating new nodes.
2771 static ir_node *transform_node_Proj(ir_node *proj) {
2772 ir_node *n = get_Proj_pred(proj);
2774 switch (get_irn_opcode(n)) {
2776 return transform_node_Proj_Div(proj);
2779 return transform_node_Proj_Mod(proj);
2782 return transform_node_Proj_DivMod(proj);
2785 return transform_node_Proj_Cond(proj);
2788 return transform_node_Proj_Cmp(proj);
2791 /* should not happen, but if it does will be optimized away */
2792 return equivalent_node_Proj(proj);
2798 } /* transform_node_Proj */
2801 * Move Confirms down through Phi nodes.
2803 static ir_node *transform_node_Phi(ir_node *phi) {
2805 ir_mode *mode = get_irn_mode(phi);
2807 if (mode_is_reference(mode)) {
2808 n = get_irn_arity(phi);
2810 /* Beware of Phi0 */
2812 ir_node *pred = get_irn_n(phi, 0);
2813 ir_node *bound, *new_Phi, *block, **in;
2816 if (! is_Confirm(pred))
2819 bound = get_Confirm_bound(pred);
2820 pnc = get_Confirm_cmp(pred);
2822 NEW_ARR_A(ir_node *, in, n);
2823 in[0] = get_Confirm_value(pred);
2825 for (i = 1; i < n; ++i) {
2826 pred = get_irn_n(phi, i);
2828 if (! is_Confirm(pred) ||
2829 get_Confirm_bound(pred) != bound ||
2830 get_Confirm_cmp(pred) != pnc)
2832 in[i] = get_Confirm_value(pred);
2834 /* move the Confirm nodes "behind" the Phi */
2835 block = get_irn_n(phi, -1);
2836 new_Phi = new_r_Phi(current_ir_graph, block, n, in, get_irn_mode(phi));
2837 return new_r_Confirm(current_ir_graph, block, new_Phi, bound, pnc);
2841 } /* transform_node_Phi */
2844 * Returns the operands of a commutative bin-op, if one operand is
2845 * a const, it is returned as the second one.
2847 static void get_comm_Binop_Ops(ir_node *binop, ir_node **a, ir_node **c) {
2848 ir_node *op_a = get_binop_left(binop);
2849 ir_node *op_b = get_binop_right(binop);
2851 assert(is_op_commutative(get_irn_op(binop)));
2853 if (get_irn_op(op_a) == op_Const) {
2860 } /* get_comm_Binop_Ops */
2863 * Optimize a Or(And(Or(And(v,c4),c3),c2),c1) pattern if possible.
2864 * Such pattern may arise in bitfield stores.
2866 * value c4 value c4 & c2
2867 * AND c3 AND c1 | c3
2872 static ir_node *transform_node_Or_bf_store(ir_node *or) {
2875 ir_node *and_l, *c3;
2876 ir_node *value, *c4;
2877 ir_node *new_and, *new_const, *block;
2878 ir_mode *mode = get_irn_mode(or);
2880 tarval *tv1, *tv2, *tv3, *tv4, *tv, *n_tv4, *n_tv2;
2882 get_comm_Binop_Ops(or, &and, &c1);
2883 if ((get_irn_op(c1) != op_Const) || (get_irn_op(and) != op_And))
2886 get_comm_Binop_Ops(and, &or_l, &c2);
2887 if ((get_irn_op(c2) != op_Const) || (get_irn_op(or_l) != op_Or))
2890 get_comm_Binop_Ops(or_l, &and_l, &c3);
2891 if ((get_irn_op(c3) != op_Const) || (get_irn_op(and_l) != op_And))
2894 get_comm_Binop_Ops(and_l, &value, &c4);
2895 if (get_irn_op(c4) != op_Const)
2898 /* ok, found the pattern, check for conditions */
2899 assert(mode == get_irn_mode(and));
2900 assert(mode == get_irn_mode(or_l));
2901 assert(mode == get_irn_mode(and_l));
2903 tv1 = get_Const_tarval(c1);
2904 tv2 = get_Const_tarval(c2);
2905 tv3 = get_Const_tarval(c3);
2906 tv4 = get_Const_tarval(c4);
2908 tv = tarval_or(tv4, tv2);
2909 if (classify_tarval(tv) != TV_CLASSIFY_ALL_ONE) {
2910 /* have at least one 0 at the same bit position */
2914 n_tv4 = tarval_not(tv4);
2915 if (tv3 != tarval_and(tv3, n_tv4)) {
2916 /* bit in the or_mask is outside the and_mask */
2920 n_tv2 = tarval_not(tv2);
2921 if (tv1 != tarval_and(tv1, n_tv2)) {
2922 /* bit in the or_mask is outside the and_mask */
2926 /* ok, all conditions met */
2927 block = get_irn_n(or, -1);
2929 new_and = new_r_And(current_ir_graph, block,
2930 value, new_r_Const(current_ir_graph, block, mode, tarval_and(tv4, tv2)), mode);
2932 new_const = new_r_Const(current_ir_graph, block, mode, tarval_or(tv3, tv1));
2934 set_Or_left(or, new_and);
2935 set_Or_right(or, new_const);
2937 /* check for more */
2938 return transform_node_Or_bf_store(or);
2939 } /* transform_node_Or_bf_store */
2942 * Optimize an Or(shl(x, c), shr(x, bits - c)) into a Rot
2944 static ir_node *transform_node_Or_Rot(ir_node *or) {
2945 ir_mode *mode = get_irn_mode(or);
2946 ir_node *shl, *shr, *block;
2947 ir_node *irn, *x, *c1, *c2, *v, *sub, *n;
2950 if (! mode_is_int(mode))
2953 shl = get_binop_left(or);
2954 shr = get_binop_right(or);
2956 if (get_irn_op(shl) == op_Shr) {
2957 if (get_irn_op(shr) != op_Shl)
2963 } else if (get_irn_op(shl) != op_Shl) {
2965 } else if (get_irn_op(shr) != op_Shr) {
2968 x = get_Shl_left(shl);
2969 if (x != get_Shr_left(shr))
2972 c1 = get_Shl_right(shl);
2973 c2 = get_Shr_right(shr);
2974 if (get_irn_op(c1) == op_Const && get_irn_op(c2) == op_Const) {
2975 tv1 = get_Const_tarval(c1);
2976 if (! tarval_is_long(tv1))
2979 tv2 = get_Const_tarval(c2);
2980 if (! tarval_is_long(tv2))
2983 if (get_tarval_long(tv1) + get_tarval_long(tv2)
2984 != get_mode_size_bits(mode))
2987 /* yet, condition met */
2988 block = get_irn_n(or, -1);
2990 n = new_r_Rot(current_ir_graph, block, x, c1, mode);
2992 DBG_OPT_ALGSIM1(or, shl, shr, n, FS_OPT_OR_SHFT_TO_ROT);
2994 } else if (get_irn_op(c1) == op_Sub) {
2998 if (get_Sub_right(sub) != v)
3001 c1 = get_Sub_left(sub);
3002 if (get_irn_op(c1) != op_Const)
3005 tv1 = get_Const_tarval(c1);
3006 if (! tarval_is_long(tv1))
3009 if (get_tarval_long(tv1) != get_mode_size_bits(mode))
3012 /* yet, condition met */
3013 block = get_nodes_block(or);
3015 /* a Rot right is not supported, so use a rot left */
3016 n = new_r_Rot(current_ir_graph, block, x, sub, mode);
3018 DBG_OPT_ALGSIM0(or, n, FS_OPT_OR_SHFT_TO_ROT);
3020 } else if (get_irn_op(c2) == op_Sub) {
3024 c1 = get_Sub_left(sub);
3025 if (get_irn_op(c1) != op_Const)
3028 tv1 = get_Const_tarval(c1);
3029 if (! tarval_is_long(tv1))
3032 if (get_tarval_long(tv1) != get_mode_size_bits(mode))
3035 /* yet, condition met */
3036 block = get_irn_n(or, -1);
3039 n = new_r_Rot(current_ir_graph, block, x, v, mode);
3041 DBG_OPT_ALGSIM0(or, n, FS_OPT_OR_SHFT_TO_ROT);
3046 } /* transform_node_Or_Rot */
3051 static ir_node *transform_node_Or(ir_node *n) {
3052 ir_node *c, *oldn = n;
3053 ir_node *a = get_Or_left(n);
3054 ir_node *b = get_Or_right(n);
3056 HANDLE_BINOP_PHI(tarval_or, a,b,c);
3058 n = transform_node_Or_bf_store(n);
3059 n = transform_node_Or_Rot(n);
3062 } /* transform_node_Or */
3066 static ir_node *transform_node(ir_node *n);
3069 * Optimize (a >> c1) >> c2), works for Shr, Shrs, Shl.
3071 * Should be moved to reassociation?
3073 static ir_node *transform_node_shift(ir_node *n) {
3074 ir_node *left, *right;
3075 tarval *tv1, *tv2, *res;
3077 int modulo_shf, flag;
3079 left = get_binop_left(n);
3081 /* different operations */
3082 if (get_irn_op(left) != get_irn_op(n))
3085 right = get_binop_right(n);
3086 tv1 = value_of(right);
3087 if (tv1 == tarval_bad)
3090 tv2 = value_of(get_binop_right(left));
3091 if (tv2 == tarval_bad)
3094 res = tarval_add(tv1, tv2);
3096 /* beware: a simple replacement works only, if res < modulo shift */
3097 mode = get_irn_mode(n);
3101 modulo_shf = get_mode_modulo_shift(mode);
3102 if (modulo_shf > 0) {
3103 tarval *modulo = new_tarval_from_long(modulo_shf, get_tarval_mode(res));
3105 if (tarval_cmp(res, modulo) & pn_Cmp_Lt)
3111 /* ok, we can replace it */
3112 ir_node *in[2], *irn, *block = get_irn_n(n, -1);
3114 in[0] = get_binop_left(left);
3115 in[1] = new_r_Const(current_ir_graph, block, get_tarval_mode(res), res);
3117 irn = new_ir_node(NULL, current_ir_graph, block, get_irn_op(n), mode, 2, in);
3119 DBG_OPT_ALGSIM0(n, irn, FS_OPT_REASSOC_SHIFT);
3121 return transform_node(irn);
3124 } /* transform_node_shift */
3129 static ir_node *transform_node_Shr(ir_node *n) {
3130 ir_node *c, *oldn = n;
3131 ir_node *a = get_Shr_left(n);
3132 ir_node *b = get_Shr_right(n);
3134 HANDLE_BINOP_PHI(tarval_shr, a, b, c);
3135 return transform_node_shift(n);
3136 } /* transform_node_Shr */
3141 static ir_node *transform_node_Shrs(ir_node *n) {
3142 ir_node *c, *oldn = n;
3143 ir_node *a = get_Shrs_left(n);
3144 ir_node *b = get_Shrs_right(n);
3146 HANDLE_BINOP_PHI(tarval_shrs, a, b, c);
3147 return transform_node_shift(n);
3148 } /* transform_node_Shrs */
3153 static ir_node *transform_node_Shl(ir_node *n) {
3154 ir_node *c, *oldn = n;
3155 ir_node *a = get_Shl_left(n);
3156 ir_node *b = get_Shl_right(n);
3158 HANDLE_BINOP_PHI(tarval_shl, a, b, c);
3159 return transform_node_shift(n);
3160 } /* transform_node_Shl */
3163 * Remove dead blocks and nodes in dead blocks
3164 * in keep alive list. We do not generate a new End node.
3166 static ir_node *transform_node_End(ir_node *n) {
3167 int i, n_keepalives = get_End_n_keepalives(n);
3169 for (i = 0; i < n_keepalives; ++i) {
3170 ir_node *ka = get_End_keepalive(n, i);
3172 if (is_Block_dead(ka)) {
3173 set_End_keepalive(n, i, new_Bad());
3175 } else if (is_irn_pinned_in_irg(ka) && is_Block_dead(get_nodes_block(ka)))
3176 set_End_keepalive(n, i, new_Bad());
3179 } /* transform_node_End */
3182 * Optimize a Mux into some simpler cases.
3184 static ir_node *transform_node_Mux(ir_node *n) {
3185 ir_node *oldn = n, *sel = get_Mux_sel(n);
3186 ir_mode *mode = get_irn_mode(n);
3188 if (get_irn_op(sel) == op_Proj && !mode_honor_signed_zeros(mode)) {
3189 ir_node *cmp = get_Proj_pred(sel);
3190 long proj_nr = get_Proj_proj(sel);
3191 ir_node *f = get_Mux_false(n);
3192 ir_node *t = get_Mux_true(n);
3194 if (get_irn_op(cmp) == op_Cmp && classify_Const(get_Cmp_right(cmp)) == CNST_NULL) {
3195 ir_node *block = get_irn_n(n, -1);
3198 * Note: normalization puts the constant on the right site,
3199 * so we check only one case.
3201 * Note further that these optimization work even for floating point
3202 * with NaN's because -NaN == NaN.
3203 * However, if +0 and -0 is handled differently, we cannot use the first one.
3205 if (get_irn_op(f) == op_Minus &&
3206 get_Minus_op(f) == t &&
3207 get_Cmp_left(cmp) == t) {
3209 if (proj_nr == pn_Cmp_Ge || proj_nr == pn_Cmp_Gt) {
3210 /* Mux(a >=/> 0, -a, a) ==> Abs(a) */
3211 n = new_rd_Abs(get_irn_dbg_info(n),
3215 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_ABS);
3217 } else if (proj_nr == pn_Cmp_Le || proj_nr == pn_Cmp_Lt) {
3218 /* Mux(a <=/< 0, -a, a) ==> Minus(Abs(a)) */
3219 n = new_rd_Abs(get_irn_dbg_info(n),
3223 n = new_rd_Minus(get_irn_dbg_info(n),
3228 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_ABS);
3231 } else if (get_irn_op(t) == op_Minus &&
3232 get_Minus_op(t) == f &&
3233 get_Cmp_left(cmp) == f) {
3235 if (proj_nr == pn_Cmp_Le || proj_nr == pn_Cmp_Lt) {
3236 /* Mux(a <=/< 0, a, -a) ==> Abs(a) */
3237 n = new_rd_Abs(get_irn_dbg_info(n),
3241 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_ABS);
3243 } else if (proj_nr == pn_Cmp_Ge || proj_nr == pn_Cmp_Gt) {
3244 /* Mux(a >=/> 0, a, -a) ==> Minus(Abs(a)) */
3245 n = new_rd_Abs(get_irn_dbg_info(n),
3249 n = new_rd_Minus(get_irn_dbg_info(n),
3254 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_ABS);
3259 if (mode_is_int(mode) && mode_is_signed(mode) &&
3260 get_mode_arithmetic(mode) == irma_twos_complement) {
3261 ir_node *x = get_Cmp_left(cmp);
3263 /* the following optimization works only with signed integer two-complement mode */
3265 if (mode == get_irn_mode(x)) {
3267 * FIXME: this restriction is two rigid, as it would still
3268 * work if mode(x) = Hs and mode == Is, but at least it removes
3271 if ((proj_nr == pn_Cmp_Lt || proj_nr == pn_Cmp_Le) &&
3272 classify_Const(t) == CNST_ALL_ONE &&
3273 classify_Const(f) == CNST_NULL) {
3275 * Mux(x:T </<= 0, 0, -1) -> Shrs(x, sizeof_bits(T) - 1)
3279 n = new_rd_Shrs(get_irn_dbg_info(n),
3280 current_ir_graph, block, x,
3281 new_r_Const_long(current_ir_graph, block, mode_Iu,
3282 get_mode_size_bits(mode) - 1),
3284 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_SHR);
3286 } else if ((proj_nr == pn_Cmp_Gt || proj_nr == pn_Cmp_Ge) &&
3287 classify_Const(t) == CNST_ONE &&
3288 classify_Const(f) == CNST_NULL) {
3290 * Mux(x:T >/>= 0, 0, 1) -> Shr(-x, sizeof_bits(T) - 1)
3294 n = new_rd_Shr(get_irn_dbg_info(n),
3295 current_ir_graph, block,
3296 new_r_Minus(current_ir_graph, block, x, mode),
3297 new_r_Const_long(current_ir_graph, block, mode_Iu,
3298 get_mode_size_bits(mode) - 1),
3300 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_SHR);
3307 return arch_transform_node_Mux(n);
3308 } /* transform_node_Mux */
3311 * Optimize a Psi into some simpler cases.
3313 static ir_node *transform_node_Psi(ir_node *n) {
3315 return transform_node_Mux(n);
3318 } /* transform_node_Psi */
3321 * Tries several [inplace] [optimizing] transformations and returns an
3322 * equivalent node. The difference to equivalent_node() is that these
3323 * transformations _do_ generate new nodes, and thus the old node must
3324 * not be freed even if the equivalent node isn't the old one.
3326 static ir_node *transform_node(ir_node *n) {
3327 if (n->op->ops.transform_node)
3328 n = n->op->ops.transform_node(n);
3330 } /* transform_node */
3333 * Sets the default transform node operation for an ir_op_ops.
3335 * @param code the opcode for the default operation
3336 * @param ops the operations initialized
3341 static ir_op_ops *firm_set_default_transform_node(ir_opcode code, ir_op_ops *ops)
3345 ops->transform_node = transform_node_##a; \
3378 } /* firm_set_default_transform_node */
3381 /* **************** Common Subexpression Elimination **************** */
3383 /** The size of the hash table used, should estimate the number of nodes
3385 #define N_IR_NODES 512
3387 /** Compares the attributes of two Const nodes. */
3388 static int node_cmp_attr_Const(ir_node *a, ir_node *b) {
3389 return (get_Const_tarval(a) != get_Const_tarval(b))
3390 || (get_Const_type(a) != get_Const_type(b));
3391 } /* node_cmp_attr_Const */
3393 /** Compares the attributes of two Proj nodes. */
3394 static int node_cmp_attr_Proj(ir_node *a, ir_node *b) {
3395 return get_irn_proj_attr (a) != get_irn_proj_attr (b);
3396 } /* node_cmp_attr_Proj */
3398 /** Compares the attributes of two Filter nodes. */
3399 static int node_cmp_attr_Filter(ir_node *a, ir_node *b) {
3400 return get_Filter_proj(a) != get_Filter_proj(b);
3401 } /* node_cmp_attr_Filter */
3403 /** Compares the attributes of two Alloc nodes. */
3404 static int node_cmp_attr_Alloc(ir_node *a, ir_node *b) {
3405 return (get_irn_alloc_attr(a).where != get_irn_alloc_attr(b).where)
3406 || (get_irn_alloc_attr(a).type != get_irn_alloc_attr(b).type);
3407 } /* node_cmp_attr_Alloc */
3409 /** Compares the attributes of two Free nodes. */
3410 static int node_cmp_attr_Free(ir_node *a, ir_node *b) {
3411 return (get_irn_free_attr(a).where != get_irn_free_attr(b).where)
3412 || (get_irn_free_attr(a).type != get_irn_free_attr(b).type);
3413 } /* node_cmp_attr_Free */
3415 /** Compares the attributes of two SymConst nodes. */
3416 static int node_cmp_attr_SymConst(ir_node *a, ir_node *b) {
3417 return (get_irn_symconst_attr(a).num != get_irn_symconst_attr(b).num)
3418 || (get_irn_symconst_attr(a).sym.type_p != get_irn_symconst_attr(b).sym.type_p)
3419 || (get_irn_symconst_attr(a).tp != get_irn_symconst_attr(b).tp);
3420 } /* node_cmp_attr_SymConst */
3422 /** Compares the attributes of two Call nodes. */
3423 static int node_cmp_attr_Call(ir_node *a, ir_node *b) {
3424 return (get_irn_call_attr(a) != get_irn_call_attr(b));
3425 } /* node_cmp_attr_Call */
3427 /** Compares the attributes of two Sel nodes. */
3428 static int node_cmp_attr_Sel(ir_node *a, ir_node *b) {
3429 return (get_irn_sel_attr(a).ent->kind != get_irn_sel_attr(b).ent->kind)
3430 || (get_irn_sel_attr(a).ent->name != get_irn_sel_attr(b).ent->name)
3431 || (get_irn_sel_attr(a).ent->owner != get_irn_sel_attr(b).ent->owner)
3432 || (get_irn_sel_attr(a).ent->ld_name != get_irn_sel_attr(b).ent->ld_name)
3433 || (get_irn_sel_attr(a).ent->type != get_irn_sel_attr(b).ent->type);
3434 } /* node_cmp_attr_Sel */
3436 /** Compares the attributes of two Phi nodes. */
3437 static int node_cmp_attr_Phi(ir_node *a, ir_node *b) {
3438 return get_irn_phi_attr (a) != get_irn_phi_attr (b);
3439 } /* node_cmp_attr_Phi */
3441 /** Compares the attributes of two Conv nodes. */
3442 static int node_cmp_attr_Conv(ir_node *a, ir_node *b) {
3443 return get_Conv_strict(a) != get_Conv_strict(b);
3444 } /* node_cmp_attr_Conv */
3446 /** Compares the attributes of two Cast nodes. */
3447 static int node_cmp_attr_Cast(ir_node *a, ir_node *b) {
3448 return get_Cast_type(a) != get_Cast_type(b);
3449 } /* node_cmp_attr_Cast */
3451 /** Compares the attributes of two Load nodes. */
3452 static int node_cmp_attr_Load(ir_node *a, ir_node *b) {
3453 if (get_Load_volatility(a) == volatility_is_volatile ||
3454 get_Load_volatility(b) == volatility_is_volatile)
3455 /* NEVER do CSE on volatile Loads */
3458 return get_Load_mode(a) != get_Load_mode(b);
3459 } /* node_cmp_attr_Load */
3461 /** Compares the attributes of two Store nodes. */
3462 static int node_cmp_attr_Store(ir_node *a, ir_node *b) {
3463 /* NEVER do CSE on volatile Stores */
3464 return (get_Store_volatility(a) == volatility_is_volatile ||
3465 get_Store_volatility(b) == volatility_is_volatile);
3466 } /* node_cmp_attr_Store */
3468 /** Compares the attributes of two Confirm nodes. */
3469 static int node_cmp_attr_Confirm(ir_node *a, ir_node *b) {
3470 return (get_Confirm_cmp(a) != get_Confirm_cmp(b));
3471 } /* node_cmp_attr_Confirm */
3474 * Set the default node attribute compare operation for an ir_op_ops.
3476 * @param code the opcode for the default operation
3477 * @param ops the operations initialized
3482 static ir_op_ops *firm_set_default_node_cmp_attr(ir_opcode code, ir_op_ops *ops)
3486 ops->node_cmp_attr = node_cmp_attr_##a; \
3510 } /* firm_set_default_node_cmp_attr */
3513 * Compare function for two nodes in the hash table. Gets two
3514 * nodes as parameters. Returns 0 if the nodes are a cse.
3516 int identities_cmp(const void *elt, const void *key) {
3523 if (a == b) return 0;
3525 if ((get_irn_op(a) != get_irn_op(b)) ||
3526 (get_irn_mode(a) != get_irn_mode(b))) return 1;
3528 /* compare if a's in and b's in are of equal length */
3529 irn_arity_a = get_irn_intra_arity (a);
3530 if (irn_arity_a != get_irn_intra_arity(b))
3533 /* for block-local cse and op_pin_state_pinned nodes: */
3534 if (!get_opt_global_cse() || (get_irn_pinned(a) == op_pin_state_pinned)) {
3535 if (get_irn_intra_n(a, -1) != get_irn_intra_n(b, -1))
3539 /* compare a->in[0..ins] with b->in[0..ins] */
3540 for (i = 0; i < irn_arity_a; i++)
3541 if (get_irn_intra_n(a, i) != get_irn_intra_n(b, i))
3545 * here, we already now that the nodes are identical except their
3548 if (a->op->ops.node_cmp_attr)
3549 return a->op->ops.node_cmp_attr(a, b);
3552 } /* identities_cmp */
3555 * Calculate a hash value of a node.
3557 unsigned ir_node_hash(ir_node *node) {
3561 if (node->op == op_Const) {
3562 /* special value for const, as they only differ in their tarval. */
3563 h = HASH_PTR(node->attr.con.tv);
3564 h = 9*h + HASH_PTR(get_irn_mode(node));
3565 } else if (node->op == op_SymConst) {
3566 /* special value for const, as they only differ in their symbol. */
3567 h = HASH_PTR(node->attr.symc.sym.type_p);
3568 h = 9*h + HASH_PTR(get_irn_mode(node));
3571 /* hash table value = 9*(9*(9*(9*(9*arity+in[0])+in[1])+ ...)+mode)+code */
3572 h = irn_arity = get_irn_intra_arity(node);
3574 /* consider all in nodes... except the block if not a control flow. */
3575 for (i = is_cfop(node) ? -1 : 0; i < irn_arity; i++) {
3576 h = 9*h + HASH_PTR(get_irn_intra_n(node, i));
3580 h = 9*h + HASH_PTR(get_irn_mode(node));
3582 h = 9*h + HASH_PTR(get_irn_op(node));
3586 } /* ir_node_hash */
3588 pset *new_identities(void) {
3589 return new_pset(identities_cmp, N_IR_NODES);
3590 } /* new_identities */
3592 void del_identities(pset *value_table) {
3593 del_pset(value_table);
3594 } /* del_identities */
3597 * Return the canonical node computing the same value as n.
3599 * @param value_table The value table
3600 * @param n The node to lookup
3602 * Looks up the node in a hash table.
3604 * For Const nodes this is performed in the constructor, too. Const
3605 * nodes are extremely time critical because of their frequent use in
3606 * constant string arrays.
3608 static INLINE ir_node *identify(pset *value_table, ir_node *n) {
3611 if (!value_table) return n;
3613 if (get_opt_reassociation()) {
3614 if (is_op_commutative(get_irn_op(n))) {
3615 ir_node *l = get_binop_left(n);
3616 ir_node *r = get_binop_right(n);
3618 /* for commutative operators perform a OP b == b OP a */
3619 if (get_irn_idx(l) > get_irn_idx(r)) {
3620 set_binop_left(n, r);
3621 set_binop_right(n, l);
3626 o = pset_find(value_table, n, ir_node_hash(n));
3635 * During construction we set the op_pin_state_pinned flag in the graph right when the
3636 * optimization is performed. The flag turning on procedure global cse could
3637 * be changed between two allocations. This way we are safe.
3639 static INLINE ir_node *identify_cons(pset *value_table, ir_node *n) {
3642 n = identify(value_table, n);
3643 if (get_irn_n(old, -1) != get_irn_n(n, -1))
3644 set_irg_pinned(current_ir_graph, op_pin_state_floats);
3646 } /* identify_cons */
3649 * Return the canonical node computing the same value as n.
3650 * Looks up the node in a hash table, enters it in the table
3651 * if it isn't there yet.
3653 ir_node *identify_remember(pset *value_table, ir_node *n) {
3656 if (!value_table) return n;
3658 if (get_opt_reassociation()) {
3659 if (is_op_commutative(get_irn_op(n))) {
3660 ir_node *l = get_binop_left(n);
3661 ir_node *r = get_binop_right(n);
3663 /* for commutative operators perform a OP b == b OP a */
3665 set_binop_left(n, r);
3666 set_binop_right(n, l);
3671 /* lookup or insert in hash table with given hash key. */
3672 o = pset_insert (value_table, n, ir_node_hash (n));
3679 } /* identify_remember */
3681 /* Add a node to the identities value table. */
3682 void add_identities(pset *value_table, ir_node *node) {
3683 if (get_opt_cse() && is_no_Block(node))
3684 identify_remember(value_table, node);
3685 } /* add_identities */
3687 /* Visit each node in the value table of a graph. */
3688 void visit_all_identities(ir_graph *irg, irg_walk_func visit, void *env) {
3690 ir_graph *rem = current_ir_graph;
3692 current_ir_graph = irg;
3693 foreach_pset(irg->value_table, node)
3695 current_ir_graph = rem;
3696 } /* visit_all_identities */
3699 * Garbage in, garbage out. If a node has a dead input, i.e., the
3700 * Bad node is input to the node, return the Bad node.
3702 static INLINE ir_node *gigo(ir_node *node) {
3704 ir_op *op = get_irn_op(node);
3706 /* remove garbage blocks by looking at control flow that leaves the block
3707 and replacing the control flow by Bad. */
3708 if (get_irn_mode(node) == mode_X) {
3709 ir_node *block = get_nodes_block(skip_Proj(node));
3711 /* Don't optimize nodes in immature blocks. */
3712 if (!get_Block_matured(block)) return node;
3713 /* Don't optimize End, may have Bads. */
3714 if (op == op_End) return node;
3716 if (is_Block(block)) {
3717 irn_arity = get_irn_arity(block);
3718 for (i = 0; i < irn_arity; i++) {
3719 if (!is_Bad(get_irn_n(block, i)))
3722 if (i == irn_arity) {
3723 ir_graph *irg = get_irn_irg(block);
3724 /* the start block is never dead */
3725 if (block != get_irg_start_block(irg)
3726 && block != get_irg_end_block(irg))
3732 /* Blocks, Phis and Tuples may have dead inputs, e.g., if one of the
3733 blocks predecessors is dead. */
3734 if (op != op_Block && op != op_Phi && op != op_Tuple) {
3735 irn_arity = get_irn_arity(node);
3738 * Beware: we can only read the block of a non-floating node.
3740 if (is_irn_pinned_in_irg(node) &&
3741 is_Block_dead(get_nodes_block(node)))
3744 for (i = 0; i < irn_arity; i++) {
3745 ir_node *pred = get_irn_n(node, i);
3750 /* Propagating Unknowns here seems to be a bad idea, because
3751 sometimes we need a node as a input and did not want that
3753 However, it might be useful to move this into a later phase
3754 (if you think that optimizing such code is useful). */
3755 if (is_Unknown(pred) && mode_is_data(get_irn_mode(node)))
3756 return new_Unknown(get_irn_mode(node));
3761 /* With this code we violate the agreement that local_optimize
3762 only leaves Bads in Block, Phi and Tuple nodes. */
3763 /* If Block has only Bads as predecessors it's garbage. */
3764 /* If Phi has only Bads as predecessors it's garbage. */
3765 if ((op == op_Block && get_Block_matured(node)) || op == op_Phi) {
3766 irn_arity = get_irn_arity(node);
3767 for (i = 0; i < irn_arity; i++) {
3768 if (!is_Bad(get_irn_n(node, i))) break;
3770 if (i == irn_arity) node = new_Bad();
3777 * These optimizations deallocate nodes from the obstack.
3778 * It can only be called if it is guaranteed that no other nodes
3779 * reference this one, i.e., right after construction of a node.
3781 * @param n The node to optimize
3783 * current_ir_graph must be set to the graph of the node!
3785 ir_node *optimize_node(ir_node *n) {
3788 ir_opcode iro = get_irn_opcode(n);
3790 /* Always optimize Phi nodes: part of the construction. */
3791 if ((!get_opt_optimize()) && (iro != iro_Phi)) return n;
3793 /* constant expression evaluation / constant folding */
3794 if (get_opt_constant_folding()) {
3795 /* neither constants nor Tuple values can be evaluated */
3796 if (iro != iro_Const && (get_irn_mode(n) != mode_T)) {
3797 /* try to evaluate */
3798 tv = computed_value(n);
3799 if (tv != tarval_bad) {
3801 ir_type *old_tp = get_irn_type(n);
3802 int i, arity = get_irn_arity(n);
3806 * Try to recover the type of the new expression.
3808 for (i = 0; i < arity && !old_tp; ++i)
3809 old_tp = get_irn_type(get_irn_n(n, i));
3812 * we MUST copy the node here temporary, because it's still needed
3813 * for DBG_OPT_CSTEVAL
3815 node_size = offsetof(ir_node, attr) + n->op->attr_size;
3816 oldn = alloca(node_size);
3818 memcpy(oldn, n, node_size);
3819 CLONE_ARR_A(ir_node *, oldn->in, n->in);
3821 /* ARG, copy the in array, we need it for statistics */
3822 memcpy(oldn->in, n->in, ARR_LEN(n->in) * sizeof(n->in[0]));
3824 /* note the inplace edges module */
3825 edges_node_deleted(n, current_ir_graph);
3827 /* evaluation was successful -- replace the node. */
3828 irg_kill_node(current_ir_graph, n);
3829 nw = new_Const(get_tarval_mode (tv), tv);
3831 if (old_tp && get_type_mode(old_tp) == get_tarval_mode (tv))
3832 set_Const_type(nw, old_tp);
3833 DBG_OPT_CSTEVAL(oldn, nw);
3839 /* remove unnecessary nodes */
3840 if (get_opt_constant_folding() ||
3841 (iro == iro_Phi) || /* always optimize these nodes. */
3843 (iro == iro_Proj) ||
3844 (iro == iro_Block) ) /* Flags tested local. */
3845 n = equivalent_node(n);
3847 optimize_preds(n); /* do node specific optimizations of nodes predecessors. */
3849 /* Common Subexpression Elimination.
3851 * Checks whether n is already available.
3852 * The block input is used to distinguish different subexpressions. Right
3853 * now all nodes are op_pin_state_pinned to blocks, i.e., the CSE only finds common
3854 * subexpressions within a block.
3857 n = identify_cons(current_ir_graph->value_table, n);
3860 edges_node_deleted(oldn, current_ir_graph);
3862 /* We found an existing, better node, so we can deallocate the old node. */
3863 irg_kill_node(current_ir_graph, oldn);
3867 /* Some more constant expression evaluation that does not allow to
3869 iro = get_irn_opcode(n);
3870 if (get_opt_constant_folding() ||
3871 (iro == iro_Cond) ||
3872 (iro == iro_Proj)) /* Flags tested local. */
3873 n = transform_node(n);
3875 /* Remove nodes with dead (Bad) input.
3876 Run always for transformation induced Bads. */
3879 /* Now we have a legal, useful node. Enter it in hash table for CSE */
3880 if (get_opt_cse() && (get_irn_opcode(n) != iro_Block)) {
3881 n = identify_remember(current_ir_graph->value_table, n);
3885 } /* optimize_node */
3889 * These optimizations never deallocate nodes (in place). This can cause dead
3890 * nodes lying on the obstack. Remove these by a dead node elimination,
3891 * i.e., a copying garbage collection.
3893 ir_node *optimize_in_place_2(ir_node *n) {
3896 ir_opcode iro = get_irn_opcode(n);
3898 if (!get_opt_optimize() && (get_irn_op(n) != op_Phi)) return n;
3900 /* constant expression evaluation / constant folding */
3901 if (get_opt_constant_folding()) {
3902 /* neither constants nor Tuple values can be evaluated */
3903 if (iro != iro_Const && get_irn_mode(n) != mode_T) {
3904 /* try to evaluate */
3905 tv = computed_value(n);
3906 if (tv != tarval_bad) {
3907 /* evaluation was successful -- replace the node. */
3908 ir_type *old_tp = get_irn_type(n);
3909 int i, arity = get_irn_arity(n);
3912 * Try to recover the type of the new expression.
3914 for (i = 0; i < arity && !old_tp; ++i)
3915 old_tp = get_irn_type(get_irn_n(n, i));
3917 n = new_Const(get_tarval_mode(tv), tv);
3919 if (old_tp && get_type_mode(old_tp) == get_tarval_mode(tv))
3920 set_Const_type(n, old_tp);
3922 DBG_OPT_CSTEVAL(oldn, n);
3928 /* remove unnecessary nodes */
3929 if (get_opt_constant_folding() ||
3930 (iro == iro_Phi) || /* always optimize these nodes. */
3931 (iro == iro_Id) || /* ... */
3932 (iro == iro_Proj) || /* ... */
3933 (iro == iro_Block) ) /* Flags tested local. */
3934 n = equivalent_node(n);
3936 optimize_preds(n); /* do node specific optimizations of nodes predecessors. */
3938 /** common subexpression elimination **/
3939 /* Checks whether n is already available. */
3940 /* The block input is used to distinguish different subexpressions. Right
3941 now all nodes are op_pin_state_pinned to blocks, i.e., the cse only finds common
3942 subexpressions within a block. */
3943 if (get_opt_cse()) {
3944 n = identify(current_ir_graph->value_table, n);
3947 /* Some more constant expression evaluation. */
3948 iro = get_irn_opcode(n);
3949 if (get_opt_constant_folding() ||
3950 (iro == iro_Cond) ||
3951 (iro == iro_Proj)) /* Flags tested local. */
3952 n = transform_node(n);
3954 /* Remove nodes with dead (Bad) input.
3955 Run always for transformation induced Bads. */
3958 /* Now we can verify the node, as it has no dead inputs any more. */
3961 /* Now we have a legal, useful node. Enter it in hash table for cse.
3962 Blocks should be unique anyways. (Except the successor of start:
3963 is cse with the start block!) */
3964 if (get_opt_cse() && (get_irn_opcode(n) != iro_Block))
3965 n = identify_remember(current_ir_graph->value_table, n);
3968 } /* optimize_in_place_2 */
3971 * Wrapper for external use, set proper status bits after optimization.
3973 ir_node *optimize_in_place(ir_node *n) {
3974 /* Handle graph state */
3975 assert(get_irg_phase_state(current_ir_graph) != phase_building);
3977 if (get_opt_global_cse())
3978 set_irg_pinned(current_ir_graph, op_pin_state_floats);
3979 if (get_irg_outs_state(current_ir_graph) == outs_consistent)
3980 set_irg_outs_inconsistent(current_ir_graph);
3982 /* FIXME: Maybe we could also test whether optimizing the node can
3983 change the control graph. */
3984 set_irg_doms_inconsistent(current_ir_graph);
3985 return optimize_in_place_2(n);
3986 } /* optimize_in_place */
3989 * Sets the default operation for an ir_ops.
3991 ir_op_ops *firm_set_default_operations(ir_opcode code, ir_op_ops *ops) {
3992 ops = firm_set_default_computed_value(code, ops);
3993 ops = firm_set_default_equivalent_node(code, ops);
3994 ops = firm_set_default_transform_node(code, ops);
3995 ops = firm_set_default_node_cmp_attr(code, ops);
3996 ops = firm_set_default_get_type(code, ops);
3997 ops = firm_set_default_get_type_attr(code, ops);
3998 ops = firm_set_default_get_entity_attr(code, ops);
4001 } /* firm_set_default_operations */