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-2005 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' or 'alignof' SymConst.
60 static tarval *computed_value_SymConst(ir_node *n) {
63 switch (get_SymConst_kind(n)) {
64 case symconst_type_size:
65 type = get_SymConst_type(n);
66 if (get_type_state(type) == layout_fixed)
67 return new_tarval_from_long(get_type_size_bytes(type), get_irn_mode(n));
69 case symconst_type_align:
70 type = get_SymConst_type(n);
71 if (get_type_state(type) == layout_fixed)
72 return new_tarval_from_long(get_type_alignment_bytes(type), get_irn_mode(n));
78 } /* computed_value_SymConst */
81 * Return the value of an Add.
83 static tarval *computed_value_Add(ir_node *n) {
84 ir_node *a = get_Add_left(n);
85 ir_node *b = get_Add_right(n);
87 tarval *ta = value_of(a);
88 tarval *tb = value_of(b);
90 if ((ta != tarval_bad) && (tb != tarval_bad) && (get_irn_mode(a) == get_irn_mode(b)))
91 return tarval_add(ta, tb);
94 } /* computed_value_Add */
97 * Return the value of a Sub.
100 static tarval *computed_value_Sub(ir_node *n) {
101 ir_node *a = get_Sub_left(n);
102 ir_node *b = get_Sub_right(n);
107 if (a == b && !is_Bad(a))
108 return get_mode_null(get_irn_mode(n));
113 if ((ta != tarval_bad) && (tb != tarval_bad) && (get_irn_mode(a) == get_irn_mode(b)))
114 return tarval_sub(ta, tb);
117 } /* computed_value_Sub */
120 * Return the value of a Carry.
121 * Special : a op 0, 0 op b
123 static tarval *computed_value_Carry(ir_node *n) {
124 ir_node *a = get_binop_left(n);
125 ir_node *b = get_binop_right(n);
126 ir_mode *m = get_irn_mode(n);
128 tarval *ta = value_of(a);
129 tarval *tb = value_of(b);
131 if ((ta != tarval_bad) && (tb != tarval_bad)) {
133 return tarval_carry() ? get_mode_one(m) : get_mode_null(m);
135 if ( (classify_tarval(ta) == TV_CLASSIFY_NULL)
136 || (classify_tarval(tb) == TV_CLASSIFY_NULL))
137 return get_mode_null(m);
140 } /* computed_value_Carry */
143 * Return the value of a Borrow.
146 static tarval *computed_value_Borrow(ir_node *n) {
147 ir_node *a = get_binop_left(n);
148 ir_node *b = get_binop_right(n);
149 ir_mode *m = get_irn_mode(n);
151 tarval *ta = value_of(a);
152 tarval *tb = value_of(b);
154 if ((ta != tarval_bad) && (tb != tarval_bad)) {
155 return tarval_cmp(ta, tb) == pn_Cmp_Lt ? get_mode_one(m) : get_mode_null(m);
156 } else if (classify_tarval(ta) == TV_CLASSIFY_NULL) {
157 return get_mode_null(m);
160 } /* computed_value_Borrow */
163 * Return the value of an unary Minus.
165 static tarval *computed_value_Minus(ir_node *n) {
166 ir_node *a = get_Minus_op(n);
167 tarval *ta = value_of(a);
169 if ((ta != tarval_bad) && mode_is_signed(get_irn_mode(a)))
170 return tarval_neg(ta);
173 } /* computed_value_Minus */
176 * Return the value of a Mul.
178 static tarval *computed_value_Mul(ir_node *n) {
179 ir_node *a = get_Mul_left(n);
180 ir_node *b = get_Mul_right(n);
182 tarval *ta = value_of(a);
183 tarval *tb = value_of(b);
185 if ((ta != tarval_bad) && (tb != tarval_bad) && (get_irn_mode(a) == get_irn_mode(b))) {
186 return tarval_mul(ta, tb);
188 /* a*0 = 0 or 0*b = 0:
189 calls computed_value recursive and returns the 0 with proper
191 if ((ta != tarval_bad) && (ta == get_mode_null(get_tarval_mode(ta))))
193 if ((tb != tarval_bad) && (tb == get_mode_null(get_tarval_mode(tb))))
197 } /* computed_value_Mul */
200 * Return the value of a floating point Quot.
202 static tarval *computed_value_Quot(ir_node *n) {
203 ir_node *a = get_Quot_left(n);
204 ir_node *b = get_Quot_right(n);
206 tarval *ta = value_of(a);
207 tarval *tb = value_of(b);
209 /* This was missing in original implementation. Why? */
210 if ((ta != tarval_bad) && (tb != tarval_bad) && (get_irn_mode(a) == get_irn_mode(b))) {
211 if (tb != get_mode_null(get_tarval_mode(tb))) /* div by zero: return tarval_bad */
212 return tarval_quo(ta, tb);
215 } /* computed_value_Quot */
218 * Calculate the value of an integer Div of two nodes.
219 * Special case: 0 / b
221 static tarval *do_computed_value_Div(ir_node *a, ir_node *b) {
222 tarval *ta = value_of(a);
223 tarval *tb = value_of(b);
225 /* Compute c1 / c2 or 0 / a, a != 0 */
226 if (ta != tarval_bad) {
227 if ((tb != tarval_bad) && (tb != get_mode_null(get_irn_mode(b)))) /* div by zero: return tarval_bad */
228 return tarval_div(ta, tb);
229 else if (ta == get_mode_null(get_tarval_mode(ta))) /* 0 / b == 0 */
233 } /* do_computed_value_Div */
236 * Return the value of an integer Div.
238 static tarval *computed_value_Div(ir_node *n) {
239 return do_computed_value_Div(get_Div_left(n), get_Div_right(n));
240 } /* computed_value_Div */
243 * Calculate the value of an integer Mod of two nodes.
244 * Special case: a % 1
246 static tarval *do_computed_value_Mod(ir_node *a, ir_node *b) {
247 tarval *ta = value_of(a);
248 tarval *tb = value_of(b);
250 /* Compute c1 % c2 or a % 1 */
251 if (tb != tarval_bad) {
252 if ((ta != tarval_bad) && (tb != get_mode_null(get_tarval_mode(tb)))) /* div by zero: return tarval_bad */
253 return tarval_mod(ta, tb);
254 else if (tb == get_mode_one(get_tarval_mode(tb))) /* x mod 1 == 0 */
255 return get_mode_null(get_irn_mode(a));
258 } /* do_computed_value_Mod */
261 * Return the value of an integer Mod.
263 static tarval *computed_value_Mod(ir_node *n) {
264 return do_computed_value_Mod(get_Mod_left(n), get_Mod_right(n));
265 } /* computed_value_Mod */
268 * Return the value of an Abs.
270 static tarval *computed_value_Abs(ir_node *n) {
271 ir_node *a = get_Abs_op(n);
272 tarval *ta = value_of(a);
274 if (ta != tarval_bad)
275 return tarval_abs(ta);
278 } /* computed_value_Abs */
281 * Return the value of an And.
282 * Special case: a & 0, 0 & b
284 static tarval *computed_value_And(ir_node *n) {
285 ir_node *a = get_And_left(n);
286 ir_node *b = get_And_right(n);
288 tarval *ta = value_of(a);
289 tarval *tb = value_of(b);
291 if ((ta != tarval_bad) && (tb != tarval_bad)) {
292 return tarval_and (ta, tb);
296 if ( (classify_tarval ((v = ta)) == TV_CLASSIFY_NULL)
297 || (classify_tarval ((v = tb)) == TV_CLASSIFY_NULL)) {
302 } /* computed_value_And */
305 * Return the value of an Or.
306 * Special case: a | 1...1, 1...1 | b
308 static tarval *computed_value_Or(ir_node *n) {
309 ir_node *a = get_Or_left(n);
310 ir_node *b = get_Or_right(n);
312 tarval *ta = value_of(a);
313 tarval *tb = value_of(b);
315 if ((ta != tarval_bad) && (tb != tarval_bad)) {
316 return tarval_or (ta, tb);
319 if ( (classify_tarval ((v = ta)) == TV_CLASSIFY_ALL_ONE)
320 || (classify_tarval ((v = tb)) == TV_CLASSIFY_ALL_ONE)) {
325 } /* computed_value_Or */
328 * Return the value of an Eor.
330 static tarval *computed_value_Eor(ir_node *n) {
331 ir_node *a = get_Eor_left(n);
332 ir_node *b = get_Eor_right(n);
337 return get_mode_null(get_irn_mode(n));
342 if ((ta != tarval_bad) && (tb != tarval_bad)) {
343 return tarval_eor (ta, tb);
346 } /* computed_value_Eor */
349 * Return the value of a Not.
351 static tarval *computed_value_Not(ir_node *n) {
352 ir_node *a = get_Not_op(n);
353 tarval *ta = value_of(a);
355 if (ta != tarval_bad)
356 return tarval_not(ta);
359 } /* computed_value_Not */
362 * Return the value of a Shl.
364 static tarval *computed_value_Shl(ir_node *n) {
365 ir_node *a = get_Shl_left(n);
366 ir_node *b = get_Shl_right(n);
368 tarval *ta = value_of(a);
369 tarval *tb = value_of(b);
371 if ((ta != tarval_bad) && (tb != tarval_bad)) {
372 return tarval_shl (ta, tb);
375 } /* computed_value_Shl */
378 * Return the value of a Shr.
380 static tarval *computed_value_Shr(ir_node *n) {
381 ir_node *a = get_Shr_left(n);
382 ir_node *b = get_Shr_right(n);
384 tarval *ta = value_of(a);
385 tarval *tb = value_of(b);
387 if ((ta != tarval_bad) && (tb != tarval_bad)) {
388 return tarval_shr (ta, tb);
391 } /* computed_value_Shr */
394 * Return the value of a Shrs.
396 static tarval *computed_value_Shrs(ir_node *n) {
397 ir_node *a = get_Shrs_left(n);
398 ir_node *b = get_Shrs_right(n);
400 tarval *ta = value_of(a);
401 tarval *tb = value_of(b);
403 if ((ta != tarval_bad) && (tb != tarval_bad)) {
404 return tarval_shrs (ta, tb);
407 } /* computed_value_Shrs */
410 * Return the value of a Rot.
412 static tarval *computed_value_Rot(ir_node *n)
414 ir_node *a = get_Rot_left(n);
415 ir_node *b = get_Rot_right(n);
417 tarval *ta = value_of(a);
418 tarval *tb = value_of(b);
420 if ((ta != tarval_bad) && (tb != tarval_bad)) {
421 return tarval_rot (ta, tb);
424 } /* computed_value_Rot */
427 * Return the value of a Conv.
429 static tarval *computed_value_Conv(ir_node *n)
431 ir_node *a = get_Conv_op(n);
432 tarval *ta = value_of(a);
434 if (ta != tarval_bad)
435 return tarval_convert_to(ta, get_irn_mode(n));
438 } /* computed_value_Conv */
441 * Return the value of a Proj(Cmp).
443 * This performs a first step of unreachable code elimination.
444 * Proj can not be computed, but folding a Cmp above the Proj here is
445 * not as wasteful as folding a Cmp into a Tuple of 16 Consts of which
447 * There are several case where we can evaluate a Cmp node, see later.
449 static tarval *computed_value_Proj_Cmp(ir_node *n)
451 ir_node *a = get_Proj_pred(n);
452 ir_node *aa = get_Cmp_left(a);
453 ir_node *ab = get_Cmp_right(a);
454 long proj_nr = get_Proj_proj(n);
457 * BEWARE: a == a is NOT always True for floating Point values, as
458 * NaN != NaN is defined, so we must check this here.
461 !mode_is_float(get_irn_mode(aa)) || proj_nr == pn_Cmp_Lt || proj_nr == pn_Cmp_Gt)
464 /* This is a trick with the bits used for encoding the Cmp
465 Proj numbers, the following statement is not the same:
466 return new_tarval_from_long (proj_nr == pn_Cmp_Eq, mode_b) */
467 return new_tarval_from_long (proj_nr & pn_Cmp_Eq, mode_b);
470 tarval *taa = value_of(aa);
471 tarval *tab = value_of(ab);
472 ir_mode *mode = get_irn_mode(aa);
475 * The predecessors of Cmp are target values. We can evaluate
478 if ((taa != tarval_bad) && (tab != tarval_bad)) {
479 /* strange checks... */
480 pn_Cmp flags = tarval_cmp(taa, tab);
481 if (flags != pn_Cmp_False) {
482 return new_tarval_from_long (proj_nr & flags, mode_b);
485 /* for integer values, we can check against MIN/MAX */
486 else if (mode_is_int(mode)) {
487 /* MIN <=/> x. This results in true/false. */
488 if (taa == get_mode_min(mode)) {
489 /* a compare with the MIN value */
490 if (proj_nr == pn_Cmp_Le)
491 return get_tarval_b_true();
492 else if (proj_nr == pn_Cmp_Gt)
493 return get_tarval_b_false();
495 /* x >=/< MIN. This results in true/false. */
497 if (tab == get_mode_min(mode)) {
498 /* a compare with the MIN value */
499 if (proj_nr == pn_Cmp_Ge)
500 return get_tarval_b_true();
501 else if (proj_nr == pn_Cmp_Lt)
502 return get_tarval_b_false();
504 /* MAX >=/< x. This results in true/false. */
505 else if (taa == get_mode_max(mode)) {
506 if (proj_nr == pn_Cmp_Ge)
507 return get_tarval_b_true();
508 else if (proj_nr == pn_Cmp_Lt)
509 return get_tarval_b_false();
511 /* x <=/> MAX. This results in true/false. */
512 else if (tab == get_mode_max(mode)) {
513 if (proj_nr == pn_Cmp_Le)
514 return get_tarval_b_true();
515 else if (proj_nr == pn_Cmp_Gt)
516 return get_tarval_b_false();
520 * The predecessors are Allocs or (void*)(0) constants. Allocs never
521 * return NULL, they raise an exception. Therefore we can predict
525 ir_node *aaa = skip_Id(skip_Proj(aa));
526 ir_node *aba = skip_Id(skip_Proj(ab));
528 if ( ( (/* aa is ProjP and aaa is Alloc */
529 (get_irn_op(aa) == op_Proj)
530 && (mode_is_reference(get_irn_mode(aa)))
531 && (get_irn_op(aaa) == op_Alloc))
532 && ( (/* ab is NULL */
533 (get_irn_op(ab) == op_Const)
534 && (mode_is_reference(get_irn_mode(ab)))
535 && (get_Const_tarval(ab) == get_mode_null(get_irn_mode(ab))))
536 || (/* ab is other Alloc */
537 (get_irn_op(ab) == op_Proj)
538 && (mode_is_reference(get_irn_mode(ab)))
539 && (get_irn_op(aba) == op_Alloc)
541 || (/* aa is NULL and aba is Alloc */
542 (get_irn_op(aa) == op_Const)
543 && (mode_is_reference(get_irn_mode(aa)))
544 && (get_Const_tarval(aa) == get_mode_null(get_irn_mode(aa)))
545 && (get_irn_op(ab) == op_Proj)
546 && (mode_is_reference(get_irn_mode(ab)))
547 && (get_irn_op(aba) == op_Alloc)))
549 return new_tarval_from_long(proj_nr & pn_Cmp_Ne, mode_b);
552 return computed_value_Cmp_Confirm(a, aa, ab, proj_nr);
553 } /* computed_value_Proj_Cmp */
556 * Return the value of a Proj, handle Proj(Cmp), Proj(Div), Proj(Mod),
557 * Proj(DivMod) and Proj(Quot).
559 static tarval *computed_value_Proj(ir_node *n) {
560 ir_node *a = get_Proj_pred(n);
563 switch (get_irn_opcode(a)) {
565 return computed_value_Proj_Cmp(n);
568 /* compute either the Div or the Mod part */
569 proj_nr = get_Proj_proj(n);
570 if (proj_nr == pn_DivMod_res_div)
571 return do_computed_value_Div(get_DivMod_left(a), get_DivMod_right(a));
572 else if (proj_nr == pn_DivMod_res_mod)
573 return do_computed_value_Mod(get_DivMod_left(a), get_DivMod_right(a));
577 if (get_Proj_proj(n) == pn_Div_res)
578 return computed_value(a);
582 if (get_Proj_proj(n) == pn_Mod_res)
583 return computed_value(a);
587 if (get_Proj_proj(n) == pn_Quot_res)
588 return computed_value(a);
595 } /* computed_value_Proj */
598 * Calculate the value of a Mux: can be evaluated, if the
599 * sel and the right input are known.
601 static tarval *computed_value_Mux(ir_node *n) {
602 ir_node *sel = get_Mux_sel(n);
603 tarval *ts = value_of(sel);
605 if (ts == get_tarval_b_true()) {
606 ir_node *v = get_Mux_true(n);
609 else if (ts == get_tarval_b_false()) {
610 ir_node *v = get_Mux_false(n);
614 } /* computed_value_Mux */
617 * Calculate the value of a Psi: can be evaluated, if a condition is true
618 * and all previous conditions are false. If all conditions are false
619 * we evaluate to the default one.
621 static tarval *computed_value_Psi(ir_node *n) {
623 return computed_value_Mux(n);
625 } /* computed_value_Psi */
628 * Calculate the value of a Confirm: can be evaluated,
629 * if it has the form Confirm(x, '=', Const).
631 static tarval *computed_value_Confirm(ir_node *n) {
632 return get_Confirm_cmp(n) == pn_Cmp_Eq ?
633 value_of(get_Confirm_bound(n)) : tarval_bad;
634 } /* computed_value_Confirm */
637 * If the parameter n can be computed, return its value, else tarval_bad.
638 * Performs constant folding.
640 * @param n The node this should be evaluated
642 tarval *computed_value(ir_node *n) {
643 if (n->op->ops.computed_value)
644 return n->op->ops.computed_value(n);
646 } /* computed_value */
649 * Set the default computed_value evaluator in an ir_op_ops.
651 * @param code the opcode for the default operation
652 * @param ops the operations initialized
657 static ir_op_ops *firm_set_default_computed_value(opcode code, ir_op_ops *ops)
661 ops->computed_value = computed_value_##a; \
696 } /* firm_set_default_computed_value */
699 * Returns a equivalent block for another block.
700 * If the block has only one predecessor, this is
701 * the equivalent one. If the only predecessor of a block is
702 * the block itself, this is a dead block.
704 * If both predecessors of a block are the branches of a binary
705 * Cond, the equivalent block is Cond's block.
707 * If all predecessors of a block are bad or lies in a dead
708 * block, the current block is dead as well.
710 * Note, that blocks are NEVER turned into Bad's, instead
711 * the dead_block flag is set. So, never test for is_Bad(block),
712 * always use is_dead_Block(block).
714 static ir_node *equivalent_node_Block(ir_node *n)
717 int n_preds = get_Block_n_cfgpreds(n);
719 /* The Block constructor does not call optimize, but mature_immBlock
720 calls the optimization. */
721 assert(get_Block_matured(n));
723 /* Straightening: a single entry Block following a single exit Block
724 can be merged, if it is not the Start block. */
725 /* !!! Beware, all Phi-nodes of n must have been optimized away.
726 This should be true, as the block is matured before optimize is called.
727 But what about Phi-cycles with the Phi0/Id that could not be resolved?
728 Remaining Phi nodes are just Ids. */
729 if ((n_preds == 1) && (get_irn_op(get_Block_cfgpred(n, 0)) == op_Jmp)) {
730 ir_node *predblock = get_nodes_block(get_Block_cfgpred(n, 0));
731 if (predblock == oldn) {
732 /* Jmp jumps into the block it is in -- deal self cycle. */
733 n = set_Block_dead(n);
734 DBG_OPT_DEAD_BLOCK(oldn, n);
735 } else if (get_opt_control_flow_straightening()) {
737 DBG_OPT_STG(oldn, n);
740 else if ((n_preds == 1) &&
741 (get_irn_op(skip_Proj(get_Block_cfgpred(n, 0))) == op_Cond)) {
742 ir_node *predblock = get_Block_cfgpred_block(n, 0);
743 if (predblock == oldn) {
744 /* Jmp jumps into the block it is in -- deal self cycle. */
745 n = set_Block_dead(n);
746 DBG_OPT_DEAD_BLOCK(oldn, n);
749 else if ((n_preds == 2) &&
750 (get_opt_control_flow_weak_simplification())) {
751 /* Test whether Cond jumps twice to this block
752 * The more general case which more than 2 predecessors is handles
753 * in optimize_cf(), we handle only this special case for speed here.
755 ir_node *a = get_Block_cfgpred(n, 0);
756 ir_node *b = get_Block_cfgpred(n, 1);
758 if ((get_irn_op(a) == op_Proj) &&
759 (get_irn_op(b) == op_Proj) &&
760 (get_Proj_pred(a) == get_Proj_pred(b)) &&
761 (get_irn_op(get_Proj_pred(a)) == op_Cond) &&
762 (get_irn_mode(get_Cond_selector(get_Proj_pred(a))) == mode_b)) {
763 /* Also a single entry Block following a single exit Block. Phis have
764 twice the same operand and will be optimized away. */
765 n = get_nodes_block(get_Proj_pred(a));
766 DBG_OPT_IFSIM1(oldn, a, b, n);
769 else if (get_opt_unreachable_code() &&
770 (n != get_irg_start_block(current_ir_graph)) &&
771 (n != get_irg_end_block(current_ir_graph)) ) {
774 /* If all inputs are dead, this block is dead too, except if it is
775 the start or end block. This is one step of unreachable code
777 for (i = get_Block_n_cfgpreds(n) - 1; i >= 0; --i) {
778 ir_node *pred = get_Block_cfgpred(n, i);
781 if (is_Bad(pred)) continue;
782 pred_blk = get_nodes_block(skip_Proj(pred));
784 if (is_Block_dead(pred_blk)) continue;
787 /* really found a living input */
792 n = set_Block_dead(n);
793 DBG_OPT_DEAD_BLOCK(oldn, n);
798 } /* equivalent_node_Block */
801 * Returns a equivalent node for a Jmp, a Bad :-)
802 * Of course this only happens if the Block of the Jmp is dead.
804 static ir_node *equivalent_node_Jmp(ir_node *n) {
805 /* unreachable code elimination */
806 if (is_Block_dead(get_nodes_block(n)))
810 } /* equivalent_node_Jmp */
812 /** Raise is handled in the same way as Jmp. */
813 #define equivalent_node_Raise equivalent_node_Jmp
816 /* We do not evaluate Cond here as we replace it by a new node, a Jmp.
817 See transform_node_Proj_Cond(). */
820 * Optimize operations that are commutative and have neutral 0,
821 * so a op 0 = 0 op a = a.
823 static ir_node *equivalent_node_neutral_zero(ir_node *n)
827 ir_node *a = get_binop_left(n);
828 ir_node *b = get_binop_right(n);
833 /* After running compute_node there is only one constant predecessor.
834 Find this predecessors value and remember the other node: */
835 if ((tv = value_of(a)) != tarval_bad) {
837 } else if ((tv = value_of(b)) != tarval_bad) {
842 /* If this predecessors constant value is zero, the operation is
843 * unnecessary. Remove it.
845 * Beware: If n is a Add, the mode of on and n might be different
846 * which happens in this rare construction: NULL + 3.
847 * Then, a Conv would be needed which we cannot include here.
849 if (classify_tarval (tv) == TV_CLASSIFY_NULL) {
850 if (get_irn_mode(on) == get_irn_mode(n)) {
853 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_0);
858 } /* equivalent_node_neutral_zero */
861 * Eor is commutative and has neutral 0.
863 #define equivalent_node_Eor equivalent_node_neutral_zero
866 * Optimize a - 0 and (a - x) + x (for modes with wrap-around).
868 * The second one looks strange, but this construct
869 * is used heavily in the LCC sources :-).
871 * Beware: The Mode of an Add may be different than the mode of its
872 * predecessors, so we could not return a predecessors in all cases.
874 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)
952 ir_mode *mode = get_irn_mode(n);
954 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
955 if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
959 b = get_Sub_right(n);
961 /* Beware: modes might be different */
962 if (classify_tarval(value_of(b)) == TV_CLASSIFY_NULL) {
963 if (mode == get_irn_mode(a)) {
966 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_0);
969 else if (get_irn_op(a) == op_Add) {
970 if (mode_wrap_around(mode)) {
971 ir_node *left = get_Add_left(a);
972 ir_node *right = get_Add_right(a);
975 if (mode == get_irn_mode(right)) {
977 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
980 else if (right == b) {
981 if (mode == get_irn_mode(left)) {
983 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
989 } /* equivalent_node_Sub */
993 * Optimize an "idempotent unary op", ie op(op(n)) = n.
996 * -(-a) == a, but might overflow two times.
997 * We handle it anyway here but the better way would be a
998 * flag. This would be needed for Pascal for instance.
1000 static ir_node *equivalent_node_idempotent_unop(ir_node *n)
1003 ir_node *pred = get_unop_op(n);
1005 /* optimize symmetric unop */
1006 if (get_irn_op(pred) == get_irn_op(n)) {
1007 n = get_unop_op(pred);
1008 DBG_OPT_ALGSIM2(oldn, pred, n);
1011 } /* equivalent_node_idempotent_unop */
1013 /** Optimize Not(Not(x)) == x. */
1014 #define equivalent_node_Not equivalent_node_idempotent_unop
1016 /** --x == x ??? Is this possible or can --x raise an
1017 out of bounds exception if min =! max? */
1018 #define equivalent_node_Minus equivalent_node_idempotent_unop
1021 * Optimize a * 1 = 1 * a = a.
1023 static ir_node *equivalent_node_Mul(ir_node *n)
1026 ir_node *a = get_Mul_left(n);
1027 ir_node *b = get_Mul_right(n);
1029 /* Mul is commutative and has again an other neutral element. */
1030 if (classify_tarval(value_of(a)) == TV_CLASSIFY_ONE) {
1032 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
1033 } else if (classify_tarval(value_of(b)) == TV_CLASSIFY_ONE) {
1035 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
1038 } /* equivalent_node_Mul */
1041 * Optimize a / 1 = a.
1043 static ir_node *equivalent_node_Div(ir_node *n) {
1044 ir_node *a = get_Div_left(n);
1045 ir_node *b = get_Div_right(n);
1047 /* Div is not commutative. */
1048 if (classify_tarval(value_of(b)) == TV_CLASSIFY_ONE) { /* div(x, 1) == x */
1049 /* Turn Div into a tuple (mem, bad, a) */
1050 ir_node *mem = get_Div_mem(n);
1051 turn_into_tuple(n, pn_Div_max);
1052 set_Tuple_pred(n, pn_Div_M, mem);
1053 set_Tuple_pred(n, pn_Div_X_except, new_Bad()); /* no exception */
1054 set_Tuple_pred(n, pn_Div_res, a);
1057 } /* equivalent_node_Div */
1060 * Optimize a / 1.0 = a.
1062 static ir_node *equivalent_node_Quot(ir_node *n) {
1063 ir_node *a = get_Quot_left(n);
1064 ir_node *b = get_Quot_right(n);
1066 /* Div is not commutative. */
1067 if (classify_tarval(value_of(b)) == TV_CLASSIFY_ONE) { /* Quot(x, 1) == x */
1068 /* Turn Quot into a tuple (mem, bad, a) */
1069 ir_node *mem = get_Quot_mem(n);
1070 turn_into_tuple(n, pn_Quot_max);
1071 set_Tuple_pred(n, pn_Quot_M, mem);
1072 set_Tuple_pred(n, pn_Quot_X_except, new_Bad()); /* no exception */
1073 set_Tuple_pred(n, pn_Quot_res, a);
1076 } /* equivalent_node_Quot */
1079 * Optimize a / 1 = a.
1081 static ir_node *equivalent_node_DivMod(ir_node *n) {
1082 ir_node *a = get_DivMod_left(n);
1083 ir_node *b = get_DivMod_right(n);
1085 /* Div is not commutative. */
1086 if (classify_tarval(value_of(b)) == TV_CLASSIFY_ONE) { /* div(x, 1) == x */
1087 /* Turn DivMod into a tuple (mem, bad, a, 0) */
1088 ir_node *mem = get_Div_mem(n);
1089 ir_mode *mode = get_irn_mode(b);
1091 turn_into_tuple(n, pn_DivMod_max);
1092 set_Tuple_pred(n, pn_DivMod_M, mem);
1093 set_Tuple_pred(n, pn_DivMod_X_except, new_Bad()); /* no exception */
1094 set_Tuple_pred(n, pn_DivMod_res_div, a);
1095 set_Tuple_pred(n, pn_DivMod_res_mod, new_Const(mode, get_mode_null(mode)));
1098 } /* equivalent_node_DivMod */
1101 * Use algebraic simplification a | a = a | 0 = 0 | a = a.
1103 static ir_node *equivalent_node_Or(ir_node *n) {
1106 ir_node *a = get_Or_left(n);
1107 ir_node *b = get_Or_right(n);
1110 n = a; /* Or has it's own neutral element */
1111 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_OR);
1112 } else if (classify_tarval(value_of(a)) == TV_CLASSIFY_NULL) {
1114 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_OR);
1115 } else if (classify_tarval(value_of(b)) == TV_CLASSIFY_NULL) {
1117 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_OR);
1121 } /* equivalent_node_Or */
1124 * Optimize a & 0b1...1 = 0b1...1 & a = a & a = a.
1126 static ir_node *equivalent_node_And(ir_node *n) {
1129 ir_node *a = get_And_left(n);
1130 ir_node *b = get_And_right(n);
1133 n = a; /* And has it's own neutral element */
1134 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_AND);
1135 } else if (classify_tarval(value_of(a)) == TV_CLASSIFY_ALL_ONE) {
1137 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1138 } else if (classify_tarval(value_of(b)) == TV_CLASSIFY_ALL_ONE) {
1140 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1143 } /* equivalent_node_And */
1146 * Try to remove useless Conv's:
1148 static ir_node *equivalent_node_Conv(ir_node *n) {
1150 ir_node *a = get_Conv_op(n);
1153 ir_mode *n_mode = get_irn_mode(n);
1154 ir_mode *a_mode = get_irn_mode(a);
1156 if (n_mode == a_mode) { /* No Conv necessary */
1157 /* leave strict floating point Conv's */
1158 if (get_Conv_strict(n))
1161 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CONV);
1162 } else if (get_irn_op(a) == op_Conv) { /* Conv(Conv(b)) */
1166 n_mode = get_irn_mode(n);
1167 b_mode = get_irn_mode(b);
1169 if (n_mode == b_mode) {
1170 if (n_mode == mode_b) {
1171 n = b; /* Convb(Conv*(xxxb(...))) == xxxb(...) */
1172 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
1174 else if (mode_is_int(n_mode) || mode_is_character(n_mode)) {
1175 if (smaller_mode(b_mode, a_mode)){
1176 n = b; /* ConvS(ConvL(xxxS(...))) == xxxS(...) */
1177 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
1183 } /* equivalent_node_Conv */
1186 * A Cast may be removed if the type of the previous node
1187 * is already the type of the Cast.
1189 static ir_node *equivalent_node_Cast(ir_node *n) {
1191 ir_node *pred = get_Cast_op(n);
1193 if (get_irn_type(pred) == get_Cast_type(n)) {
1195 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CAST);
1198 } /* equivalent_node_Cast */
1201 * Several optimizations:
1202 * - no Phi in start block.
1203 * - remove Id operators that are inputs to Phi
1204 * - fold Phi-nodes, iff they have only one predecessor except
1207 static ir_node *equivalent_node_Phi(ir_node *n)
1212 ir_node *block = NULL; /* to shutup gcc */
1213 ir_node *first_val = NULL; /* to shutup gcc */
1215 if (!get_opt_normalize()) return n;
1217 n_preds = get_Phi_n_preds(n);
1219 block = get_nodes_block(n);
1220 /* @@@ fliegt 'raus, sollte aber doch immer wahr sein!!!
1221 assert(get_irn_arity(block) == n_preds && "phi in wrong block!"); */
1222 if ((is_Block_dead(block)) || /* Control dead */
1223 (block == get_irg_start_block(current_ir_graph))) /* There should be no Phi nodes */
1224 return new_Bad(); /* in the Start Block. */
1226 if (n_preds == 0) return n; /* Phi of dead Region without predecessors. */
1228 /* If the Block has a Bad pred, we also have one. */
1229 for (i = 0; i < n_preds; ++i)
1230 if (is_Bad(get_Block_cfgpred(block, i)))
1231 set_Phi_pred(n, i, new_Bad());
1233 /* Find first non-self-referencing input */
1234 for (i = 0; i < n_preds; ++i) {
1235 first_val = get_Phi_pred(n, i);
1236 if ( (first_val != n) /* not self pointer */
1238 && (! is_Bad(first_val))
1240 ) { /* value not dead */
1241 break; /* then found first value. */
1246 /* A totally Bad or self-referencing Phi (we didn't break the above loop) */
1250 /* search for rest of inputs, determine if any of these
1251 are non-self-referencing */
1252 while (++i < n_preds) {
1253 ir_node *scnd_val = get_Phi_pred(n, i);
1254 if ( (scnd_val != n)
1255 && (scnd_val != first_val)
1257 && (! is_Bad(scnd_val))
1265 /* Fold, if no multiple distinct non-self-referencing inputs */
1267 DBG_OPT_PHI(oldn, n);
1270 } /* equivalent_node_Phi */
1273 * Several optimizations:
1274 * - no Sync in start block.
1275 * - fold Sync-nodes, iff they have only one predecessor except
1278 static ir_node *equivalent_node_Sync(ir_node *n)
1283 ir_node *first_val = NULL; /* to shutup gcc */
1285 if (!get_opt_normalize()) return n;
1287 n_preds = get_Sync_n_preds(n);
1289 /* Find first non-self-referencing input */
1290 for (i = 0; i < n_preds; ++i) {
1291 first_val = get_Sync_pred(n, i);
1292 if ((first_val != n) /* not self pointer */ &&
1293 (! is_Bad(first_val))
1294 ) { /* value not dead */
1295 break; /* then found first value. */
1300 /* A totally Bad or self-referencing Sync (we didn't break the above loop) */
1303 /* search the rest of inputs, determine if any of these
1304 are non-self-referencing */
1305 while (++i < n_preds) {
1306 ir_node *scnd_val = get_Sync_pred(n, i);
1307 if ((scnd_val != n) &&
1308 (scnd_val != first_val) &&
1309 (! is_Bad(scnd_val))
1315 /* Fold, if no multiple distinct non-self-referencing inputs */
1317 DBG_OPT_SYNC(oldn, n);
1320 } /* equivalent_node_Sync */
1323 * Optimize Proj(Tuple) and gigo() for ProjX in Bad block,
1324 * ProjX(Load) and ProjX(Store).
1326 static ir_node *equivalent_node_Proj(ir_node *n)
1330 ir_node *a = get_Proj_pred(n);
1332 if ( get_irn_op(a) == op_Tuple) {
1333 /* Remove the Tuple/Proj combination. */
1334 if ( get_Proj_proj(n) <= get_Tuple_n_preds(a) ) {
1335 n = get_Tuple_pred(a, get_Proj_proj(n));
1336 DBG_OPT_TUPLE(oldn, a, n);
1338 assert(0); /* This should not happen! */
1342 else if (get_irn_mode(n) == mode_X) {
1343 if (is_Block_dead(get_nodes_block(skip_Proj(n)))) {
1344 /* Remove dead control flow -- early gigo(). */
1347 else if (get_opt_ldst_only_null_ptr_exceptions()) {
1348 ir_op *op = get_irn_op(a);
1350 if (op == op_Load || op == op_Store) {
1351 /* get the load/store address */
1352 ir_node *addr = get_irn_n(a, 1);
1355 if (value_not_null(addr, &confirm)) {
1356 if (confirm == NULL) {
1357 /* this node may float if it did not depend on a Confirm */
1358 set_irn_pinned(a, op_pin_state_floats);
1368 } /* equivalent_node_Proj */
1373 static ir_node *equivalent_node_Id(ir_node *n) {
1378 } while (get_irn_op(n) == op_Id);
1380 DBG_OPT_ID(oldn, n);
1382 } /* equivalent_node_Id */
1387 static ir_node *equivalent_node_Mux(ir_node *n)
1389 ir_node *oldn = n, *sel = get_Mux_sel(n);
1390 tarval *ts = value_of(sel);
1392 /* Mux(true, f, t) == t */
1393 if (ts == tarval_b_true) {
1394 n = get_Mux_true(n);
1395 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_C);
1397 /* Mux(false, f, t) == f */
1398 else if (ts == tarval_b_false) {
1399 n = get_Mux_false(n);
1400 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_C);
1402 /* Mux(v, x, x) == x */
1403 else if (get_Mux_false(n) == get_Mux_true(n)) {
1404 n = get_Mux_true(n);
1405 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_EQ);
1407 else if (get_irn_op(sel) == op_Proj && !mode_honor_signed_zeros(get_irn_mode(n))) {
1408 ir_node *cmp = get_Proj_pred(sel);
1409 long proj_nr = get_Proj_proj(sel);
1410 ir_node *b = get_Mux_false(n);
1411 ir_node *a = get_Mux_true(n);
1414 * Note: normalization puts the constant on the right site,
1415 * so we check only one case.
1417 * Note further that these optimization work even for floating point
1418 * with NaN's because -NaN == NaN.
1419 * However, if +0 and -0 is handled differently, we cannot use the first one.
1421 if (get_irn_op(cmp) == op_Cmp && get_Cmp_left(cmp) == a) {
1422 if (classify_Const(get_Cmp_right(cmp)) == CNST_NULL) {
1423 /* Mux(a CMP 0, X, a) */
1424 if (get_irn_op(b) == op_Minus && get_Minus_op(b) == a) {
1425 /* Mux(a CMP 0, -a, a) */
1426 if (proj_nr == pn_Cmp_Eq) {
1427 /* Mux(a == 0, -a, a) ==> -a */
1429 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1431 else if (proj_nr == pn_Cmp_Lg || proj_nr == pn_Cmp_Ne) {
1432 /* Mux(a != 0, -a, a) ==> a */
1434 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1437 else if (classify_Const(b) == CNST_NULL) {
1438 /* Mux(a CMP 0, 0, a) */
1439 if (proj_nr == pn_Cmp_Lg || proj_nr == pn_Cmp_Ne) {
1440 /* Mux(a != 0, 0, a) ==> a */
1442 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1444 else if (proj_nr == pn_Cmp_Eq) {
1445 /* Mux(a == 0, 0, a) ==> 0 */
1447 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1454 } /* equivalent_node_Mux */
1457 * Returns a equivalent node of a Psi: if a condition is true
1458 * and all previous conditions are false we know its value.
1459 * If all conditions are false its value is the default one.
1461 static ir_node *equivalent_node_Psi(ir_node *n) {
1463 return equivalent_node_Mux(n);
1465 } /* equivalent_node_Psi */
1468 * Optimize -a CMP -b into b CMP a.
1469 * This works only for for modes where unary Minus
1471 * Note that two-complement integers can Overflow
1472 * so it will NOT work.
1474 static ir_node *equivalent_node_Cmp(ir_node *n)
1476 ir_node *left = get_Cmp_left(n);
1477 ir_node *right = get_Cmp_right(n);
1479 if (get_irn_op(left) == op_Minus && get_irn_op(right) == op_Minus &&
1480 !mode_overflow_on_unary_Minus(get_irn_mode(left))) {
1481 left = get_Minus_op(left);
1482 right = get_Minus_op(right);
1483 set_Cmp_left(n, right);
1484 set_Cmp_right(n, left);
1487 } /* equivalent_node_Cmp */
1490 * Remove Confirm nodes if setting is on.
1491 * Replace Confirms(x, '=', Constlike) by Constlike.
1493 static ir_node *equivalent_node_Confirm(ir_node *n) {
1494 ir_node *pred = get_Confirm_value(n);
1495 pn_Cmp pnc = get_Confirm_cmp(n);
1497 if (get_irn_op(pred) == op_Confirm && pnc == get_Confirm_cmp(pred)) {
1499 * rare case: two identical Confirms one after another,
1500 * replace the second one with the first.
1504 if (pnc == pn_Cmp_Eq) {
1505 ir_node *bound = get_Confirm_bound(n);
1508 * Optimize a rare case:
1509 * Confirm(x, '=', Constlike) ==> Constlike
1511 if (is_irn_constlike(bound)) {
1512 DBG_OPT_CONFIRM(n, bound);
1516 return get_opt_remove_confirm() ? get_Confirm_value(n) : n;
1520 * Optimize CopyB(mem, x, x) into a Nop.
1522 static ir_node *equivalent_node_CopyB(ir_node *n) {
1523 ir_node *a = get_CopyB_dst(n);
1524 ir_node *b = get_CopyB_src(n);
1527 /* Turn CopyB into a tuple (mem, bad, bad) */
1528 ir_node *mem = get_CopyB_mem(n);
1529 turn_into_tuple(n, pn_CopyB_max);
1530 set_Tuple_pred(n, pn_CopyB_M, mem);
1531 set_Tuple_pred(n, pn_CopyB_X_except, new_Bad()); /* no exception */
1532 set_Tuple_pred(n, pn_CopyB_M_except, new_Bad());
1535 } /* equivalent_node_CopyB */
1538 * Optimize Bounds(idx, idx, upper) into idx.
1540 static ir_node *equivalent_node_Bound(ir_node *n) {
1541 ir_node *idx = get_Bound_index(n);
1542 ir_node *lower = get_Bound_lower(n);
1545 /* By definition lower < upper, so if idx == lower -->
1546 lower <= idx && idx < upper */
1548 /* Turn Bound into a tuple (mem, bad, idx) */
1552 ir_node *pred = skip_Proj(idx);
1554 if (get_irn_op(pred) == op_Bound) {
1556 * idx was Bounds_check previously, it is still valid if
1557 * lower <= pred_lower && pred_upper <= upper.
1559 ir_node *upper = get_Bound_upper(n);
1560 if (get_Bound_lower(pred) == lower &&
1561 get_Bound_upper(pred) == upper) {
1563 * One could expect that we simply return the previous
1564 * Bound here. However, this would be wrong, as we could
1565 * add an exception Proj to a new location than.
1566 * So, we must turn in into a tuple
1573 /* Turn Bound into a tuple (mem, bad, idx) */
1574 ir_node *mem = get_Bound_mem(n);
1575 turn_into_tuple(n, pn_Bound_max);
1576 set_Tuple_pred(n, pn_Bound_M, mem);
1577 set_Tuple_pred(n, pn_Bound_X_except, new_Bad()); /* no exception */
1578 set_Tuple_pred(n, pn_Bound_res, idx);
1581 } /* equivalent_node_Bound */
1584 * equivalent_node() returns a node equivalent to input n. It skips all nodes that
1585 * perform no actual computation, as, e.g., the Id nodes. It does not create
1586 * new nodes. It is therefore safe to free n if the node returned is not n.
1587 * If a node returns a Tuple we can not just skip it. If the size of the
1588 * in array fits, we transform n into a tuple (e.g., Div).
1590 ir_node *equivalent_node(ir_node *n) {
1591 if (n->op->ops.equivalent_node)
1592 return n->op->ops.equivalent_node(n);
1594 } /* equivalent_node */
1597 * Sets the default equivalent node operation for an ir_op_ops.
1599 * @param code the opcode for the default operation
1600 * @param ops the operations initialized
1605 static ir_op_ops *firm_set_default_equivalent_node(opcode code, ir_op_ops *ops)
1609 ops->equivalent_node = equivalent_node_##a; \
1649 } /* firm_set_default_equivalent_node */
1652 * Do node specific optimizations of nodes predecessors.
1654 static void optimize_preds(ir_node *n) {
1655 ir_node *a = NULL, *b = NULL;
1657 /* get the operands we will work on for simple cases. */
1659 a = get_binop_left(n);
1660 b = get_binop_right(n);
1661 } else if (is_unop(n)) {
1665 switch (get_irn_opcode(n)) {
1668 /* We don't want Cast as input to Cmp. */
1669 if (get_irn_op(a) == op_Cast) {
1673 if (get_irn_op(b) == op_Cast) {
1675 set_Cmp_right(n, b);
1681 } /* optimize_preds */
1684 * Returns non-zero if a node is a Phi node
1685 * with all predecessors constant.
1687 static int is_const_Phi(ir_node *n) {
1692 for (i = get_irn_arity(n) - 1; i >= 0; --i)
1693 if (! is_Const(get_irn_n(n, i)))
1696 } /* is_const_Phi */
1699 * Apply an evaluator on a binop with a constant operators (and one Phi).
1701 * @param phi the Phi node
1702 * @param other the other operand
1703 * @param eval an evaluator function
1704 * @param left if non-zero, other is the left operand, else the right
1706 * @return a new Phi node if the conversion was successful, NULL else
1708 static ir_node *apply_binop_on_phi(ir_node *phi, tarval *other, tarval *(*eval)(tarval *, tarval *), int left) {
1714 int i, n = get_irn_arity(phi);
1716 NEW_ARR_A(void *, res, n);
1718 for (i = 0; i < n; ++i) {
1719 pred = get_irn_n(phi, i);
1720 tv = get_Const_tarval(pred);
1721 tv = eval(other, tv);
1723 if (tv == tarval_bad) {
1724 /* folding failed, bad */
1731 for (i = 0; i < n; ++i) {
1732 pred = get_irn_n(phi, i);
1733 tv = get_Const_tarval(pred);
1734 tv = eval(tv, other);
1736 if (tv == tarval_bad) {
1737 /* folding failed, bad */
1743 mode = get_irn_mode(phi);
1744 irg = current_ir_graph;
1745 for (i = 0; i < n; ++i) {
1746 pred = get_irn_n(phi, i);
1747 res[i] = new_r_Const_type(irg, get_irg_start_block(irg),
1748 mode, res[i], get_Const_type(pred));
1750 return new_r_Phi(irg, get_nodes_block(phi), n, (ir_node **)res, mode);
1751 } /* apply_binop_on_phi */
1754 * Apply an evaluator on a unop with a constant operator (a Phi).
1756 * @param phi the Phi node
1757 * @param eval an evaluator function
1759 * @return a new Phi node if the conversion was successful, NULL else
1761 static ir_node *apply_unop_on_phi(ir_node *phi, tarval *(*eval)(tarval *)) {
1767 int i, n = get_irn_arity(phi);
1769 NEW_ARR_A(void *, res, n);
1770 for (i = 0; i < n; ++i) {
1771 pred = get_irn_n(phi, i);
1772 tv = get_Const_tarval(pred);
1775 if (tv == tarval_bad) {
1776 /* folding failed, bad */
1781 mode = get_irn_mode(phi);
1782 irg = current_ir_graph;
1783 for (i = 0; i < n; ++i) {
1784 pred = get_irn_n(phi, i);
1785 res[i] = new_r_Const_type(irg, get_irg_start_block(irg),
1786 mode, res[i], get_Const_type(pred));
1788 return new_r_Phi(irg, get_nodes_block(phi), n, (ir_node **)res, mode);
1789 } /* apply_unop_on_phi */
1792 * Transform AddP(P, ConvIs(Iu)), AddP(P, ConvIu(Is)) and
1793 * SubP(P, ConvIs(Iu)), SubP(P, ConvIu(Is)).
1794 * If possible, remove the Conv's.
1796 static ir_node *transform_node_AddSub(ir_node *n)
1798 ir_mode *mode = get_irn_mode(n);
1800 if (mode_is_reference(mode)) {
1801 ir_node *left = get_binop_left(n);
1802 ir_node *right = get_binop_right(n);
1803 int ref_bits = get_mode_size_bits(mode);
1805 if (get_irn_op(left) == op_Conv) {
1806 ir_mode *mode = get_irn_mode(left);
1807 int bits = get_mode_size_bits(mode);
1809 if (ref_bits == bits &&
1810 mode_is_int(mode) &&
1811 get_mode_arithmetic(mode) == irma_twos_complement) {
1812 ir_node *pre = get_Conv_op(left);
1813 ir_mode *pre_mode = get_irn_mode(pre);
1815 if (mode_is_int(pre_mode) &&
1816 get_mode_size_bits(pre_mode) == bits &&
1817 get_mode_arithmetic(pre_mode) == irma_twos_complement) {
1818 /* ok, this conv just changes to sign, moreover the calculation
1819 * is done with same number of bits as our address mode, so
1820 * we can ignore the conv as address calculation can be viewed
1821 * as either signed or unsigned
1823 set_binop_left(n, pre);
1828 if (get_irn_op(right) == op_Conv) {
1829 ir_mode *mode = get_irn_mode(right);
1830 int bits = get_mode_size_bits(mode);
1832 if (ref_bits == bits &&
1833 mode_is_int(mode) &&
1834 get_mode_arithmetic(mode) == irma_twos_complement) {
1835 ir_node *pre = get_Conv_op(right);
1836 ir_mode *pre_mode = get_irn_mode(pre);
1838 if (mode_is_int(pre_mode) &&
1839 get_mode_size_bits(pre_mode) == bits &&
1840 get_mode_arithmetic(pre_mode) == irma_twos_complement) {
1841 /* ok, this conv just changes to sign, moreover the calculation
1842 * is done with same number of bits as our address mode, so
1843 * we can ignore the conv as address calculation can be viewed
1844 * as either signed or unsigned
1846 set_binop_right(n, pre);
1852 } /* transform_node_AddSub */
1854 #define HANDLE_BINOP_PHI(op,a,b,c) \
1856 if (is_Const(b) && is_const_Phi(a)) { \
1857 /* check for Op(Phi, Const) */ \
1858 c = apply_binop_on_phi(a, get_Const_tarval(b), op, 0); \
1860 else if (is_Const(a) && is_const_Phi(b)) { \
1861 /* check for Op(Const, Phi) */ \
1862 c = apply_binop_on_phi(b, get_Const_tarval(a), op, 1); \
1865 DBG_OPT_ALGSIM0(oldn, c, FS_OPT_CONST_PHI); \
1869 #define HANDLE_UNOP_PHI(op,a,c) \
1871 if (is_const_Phi(a)) { \
1872 /* check for Op(Phi) */ \
1873 c = apply_unop_on_phi(a, op); \
1876 DBG_OPT_ALGSIM0(oldn, c, FS_OPT_CONST_PHI); \
1882 * Do the AddSub optimization, then Transform
1883 * Constant folding on Phi
1884 * Add(a,a) -> Mul(a, 2)
1885 * Add(Mul(a, x), a) -> Mul(a, x+1)
1886 * if the mode is integer or float.
1887 * Transform Add(a,-b) into Sub(a,b).
1888 * Reassociation might fold this further.
1890 static ir_node *transform_node_Add(ir_node *n)
1893 ir_node *a, *b, *c, *oldn = n;
1895 n = transform_node_AddSub(n);
1897 a = get_Add_left(n);
1898 b = get_Add_right(n);
1900 HANDLE_BINOP_PHI(tarval_add, a,b,c);
1902 mode = get_irn_mode(n);
1904 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
1905 if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
1908 if (mode_is_num(mode)) {
1910 ir_node *block = get_irn_n(n, -1);
1913 get_irn_dbg_info(n),
1917 new_r_Const_long(current_ir_graph, block, mode, 2),
1919 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_A);
1921 else if (get_irn_op(a) == op_Minus) {
1923 get_irn_dbg_info(n),
1929 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_MINUS_B);
1931 else if (get_irn_op(b) == op_Minus) {
1933 get_irn_dbg_info(n),
1939 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_MINUS_B);
1941 /* do NOT execute this code if reassociation is enabled, it does the inverse! */
1942 else if (!get_opt_reassociation() && get_irn_op(a) == op_Mul) {
1943 ir_node *ma = get_Mul_left(a);
1944 ir_node *mb = get_Mul_right(a);
1947 ir_node *blk = get_irn_n(n, -1);
1949 get_irn_dbg_info(n), current_ir_graph, blk,
1952 get_irn_dbg_info(n), current_ir_graph, blk,
1954 new_r_Const_long(current_ir_graph, blk, mode, 1),
1957 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_MUL_A_X_A);
1960 ir_node *blk = get_irn_n(n, -1);
1962 get_irn_dbg_info(n), current_ir_graph, blk,
1965 get_irn_dbg_info(n), current_ir_graph, blk,
1967 new_r_Const_long(current_ir_graph, blk, mode, 1),
1970 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_MUL_A_X_A);
1973 /* do NOT execute this code if reassociation is enabled, it does the inverse! */
1974 else if (!get_opt_reassociation() && get_irn_op(b) == op_Mul) {
1975 ir_node *ma = get_Mul_left(b);
1976 ir_node *mb = get_Mul_right(b);
1979 ir_node *blk = get_irn_n(n, -1);
1981 get_irn_dbg_info(n), current_ir_graph, blk,
1984 get_irn_dbg_info(n), current_ir_graph, blk,
1986 new_r_Const_long(current_ir_graph, blk, mode, 1),
1989 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_MUL_A_X_A);
1992 ir_node *blk = get_irn_n(n, -1);
1994 get_irn_dbg_info(n), current_ir_graph, blk,
1997 get_irn_dbg_info(n), current_ir_graph, blk,
1999 new_r_Const_long(current_ir_graph, blk, mode, 1),
2002 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_MUL_A_X_A);
2007 } /* transform_node_Add */
2010 * Do the AddSub optimization, then Transform
2011 * Constant folding on Phi
2012 * Sub(0,a) -> Minus(a)
2013 * Sub(Mul(a, x), a) -> Mul(a, x-1)
2014 * Sub(Sub(x, y), b) -> Sub(x, Add(y,b))
2016 static ir_node *transform_node_Sub(ir_node *n)
2022 n = transform_node_AddSub(n);
2024 a = get_Sub_left(n);
2025 b = get_Sub_right(n);
2027 HANDLE_BINOP_PHI(tarval_sub, a,b,c);
2029 mode = get_irn_mode(n);
2031 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
2032 if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
2035 if (mode_is_num(mode) && (classify_Const(a) == CNST_NULL)) {
2037 get_irn_dbg_info(n),
2042 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_0_A);
2044 /* do NOT execute this code if reassociation is enabled, it does the inverse! */
2045 else if (get_opt_reassociation() && get_irn_op(a) == op_Mul) {
2046 ir_node *ma = get_Mul_left(a);
2047 ir_node *mb = get_Mul_right(a);
2050 ir_node *blk = get_irn_n(n, -1);
2052 get_irn_dbg_info(n),
2053 current_ir_graph, blk,
2056 get_irn_dbg_info(n),
2057 current_ir_graph, blk,
2059 new_r_Const_long(current_ir_graph, blk, mode, 1),
2062 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MUL_A_X_A);
2065 ir_node *blk = get_irn_n(n, -1);
2067 get_irn_dbg_info(n),
2068 current_ir_graph, blk,
2071 get_irn_dbg_info(n),
2072 current_ir_graph, blk,
2074 new_r_Const_long(current_ir_graph, blk, mode, 1),
2077 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MUL_A_X_A);
2080 else if (get_irn_op(a) == op_Sub) {
2081 ir_node *x = get_Sub_left(a);
2082 ir_node *y = get_Sub_right(a);
2083 ir_node *blk = get_irn_n(n, -1);
2084 ir_mode *m_b = get_irn_mode(b);
2085 ir_mode *m_y = get_irn_mode(y);
2088 /* Determine the right mode for the Add. */
2091 else if (mode_is_reference(m_b))
2093 else if (mode_is_reference(m_y))
2097 * Both modes are different but none is reference,
2098 * happens for instance in SubP(SubP(P, Iu), Is).
2099 * We have two possibilities here: Cast or ignore.
2100 * Currently we ignore this case.
2105 add = new_r_Add(current_ir_graph, blk, y, b, mode);
2108 set_Sub_right(n, add);
2109 DBG_OPT_ALGSIM0(n, n, FS_OPT_SUB_SUB_X_Y_Z);
2113 } /* transform_node_Sub */
2116 * Transform Mul(a,-1) into -a.
2117 * Do constant evaluation of Phi nodes.
2118 * Do architecture dependent optimizations on Mul nodes
2120 static ir_node *transform_node_Mul(ir_node *n) {
2121 ir_node *c, *oldn = n;
2122 ir_node *a = get_Mul_left(n);
2123 ir_node *b = get_Mul_right(n);
2126 HANDLE_BINOP_PHI(tarval_mul, a,b,c);
2128 mode = get_irn_mode(n);
2129 if (mode_is_signed(mode)) {
2132 if (value_of(a) == get_mode_minus_one(mode))
2134 else if (value_of(b) == get_mode_minus_one(mode))
2137 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1), r, mode);
2138 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2142 return arch_dep_replace_mul_with_shifts(n);
2143 } /* transform_node_Mul */
2146 * Transform a Div Node.
2148 static ir_node *transform_node_Div(ir_node *n)
2150 tarval *tv = value_of(n);
2153 /* BEWARE: it is NOT possible to optimize a/a to 1, as this may cause a exception */
2155 if (tv != tarval_bad) {
2156 value = new_Const(get_tarval_mode(tv), tv);
2158 DBG_OPT_CSTEVAL(n, value);
2160 else /* Try architecture dependent optimization */
2161 value = arch_dep_replace_div_by_const(n);
2164 /* Turn Div into a tuple (mem, bad, value) */
2165 ir_node *mem = get_Div_mem(n);
2167 turn_into_tuple(n, pn_Div_max);
2168 set_Tuple_pred(n, pn_Div_M, mem);
2169 set_Tuple_pred(n, pn_Div_X_except, new_Bad());
2170 set_Tuple_pred(n, pn_Div_res, value);
2173 } /* transform_node_Div */
2176 * Transform a Mod node.
2178 static ir_node *transform_node_Mod(ir_node *n)
2180 tarval *tv = value_of(n);
2183 /* BEWARE: it is NOT possible to optimize a%a to 0, as this may cause a exception */
2185 if (tv != tarval_bad) {
2186 value = new_Const(get_tarval_mode(tv), tv);
2188 DBG_OPT_CSTEVAL(n, value);
2190 else /* Try architecture dependent optimization */
2191 value = arch_dep_replace_mod_by_const(n);
2194 /* Turn Mod into a tuple (mem, bad, value) */
2195 ir_node *mem = get_Mod_mem(n);
2197 turn_into_tuple(n, pn_Mod_max);
2198 set_Tuple_pred(n, pn_Mod_M, mem);
2199 set_Tuple_pred(n, pn_Mod_X_except, new_Bad());
2200 set_Tuple_pred(n, pn_Mod_res, value);
2203 } /* transform_node_Mod */
2206 * Transform a DivMod node.
2208 static ir_node *transform_node_DivMod(ir_node *n)
2212 ir_node *a = get_DivMod_left(n);
2213 ir_node *b = get_DivMod_right(n);
2214 ir_mode *mode = get_irn_mode(a);
2215 tarval *ta = value_of(a);
2216 tarval *tb = value_of(b);
2218 if (!(mode_is_int(mode) && mode_is_int(get_irn_mode(b))))
2221 /* BEWARE: it is NOT possible to optimize a/a to 1, as this may cause a exception */
2223 if (tb != tarval_bad) {
2224 if (tb == get_mode_one(get_tarval_mode(tb))) {
2225 b = new_Const (mode, get_mode_null(mode));
2228 DBG_OPT_CSTEVAL(n, b);
2230 else if (ta != tarval_bad) {
2231 tarval *resa, *resb;
2232 resa = tarval_div (ta, tb);
2233 if (resa == tarval_bad) return n; /* Causes exception!!! Model by replacing through
2234 Jmp for X result!? */
2235 resb = tarval_mod (ta, tb);
2236 if (resb == tarval_bad) return n; /* Causes exception! */
2237 a = new_Const (mode, resa);
2238 b = new_Const (mode, resb);
2241 DBG_OPT_CSTEVAL(n, a);
2242 DBG_OPT_CSTEVAL(n, b);
2244 else { /* Try architecture dependent optimization */
2245 arch_dep_replace_divmod_by_const(&a, &b, n);
2246 evaluated = a != NULL;
2248 } else if (ta == get_mode_null(mode)) {
2249 /* 0 / non-Const = 0 */
2254 if (evaluated) { /* replace by tuple */
2255 ir_node *mem = get_DivMod_mem(n);
2256 turn_into_tuple(n, pn_DivMod_max);
2257 set_Tuple_pred(n, pn_DivMod_M, mem);
2258 set_Tuple_pred(n, pn_DivMod_X_except, new_Bad()); /* no exception */
2259 set_Tuple_pred(n, pn_DivMod_res_div, a);
2260 set_Tuple_pred(n, pn_DivMod_res_mod, b);
2264 } /* transform_node_DivMod */
2267 * Optimize Abs(x) into x if x is Confirmed >= 0
2268 * Optimize Abs(x) into -x if x is Confirmed <= 0
2270 static ir_node *transform_node_Abs(ir_node *n)
2273 ir_node *a = get_Abs_op(n);
2274 value_classify_sign sign = classify_value_sign(a);
2276 if (sign == value_classified_negative) {
2277 ir_mode *mode = get_irn_mode(n);
2280 * We can replace the Abs by -x here.
2281 * We even could add a new Confirm here.
2283 * Note that -x would create a new node, so we could
2284 * not run it in the equivalent_node() context.
2286 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph,
2287 get_irn_n(n, -1), a, mode);
2289 DBG_OPT_CONFIRM(oldn, n);
2291 else if (sign == value_classified_positive) {
2292 /* n is positive, Abs is not needed */
2295 DBG_OPT_CONFIRM(oldn, n);
2299 } /* transform_node_Abs */
2302 * Transform a Cond node.
2304 static ir_node *transform_node_Cond(ir_node *n)
2306 /* Replace the Cond by a Jmp if it branches on a constant
2309 ir_node *a = get_Cond_selector(n);
2310 tarval *ta = value_of(a);
2312 /* we need block info which is not available in floating irgs */
2313 if (get_irg_pinned(current_ir_graph) == op_pin_state_floats)
2316 if ((ta != tarval_bad) &&
2317 (get_irn_mode(a) == mode_b) &&
2318 (get_opt_unreachable_code())) {
2319 /* It's a boolean Cond, branching on a boolean constant.
2320 Replace it by a tuple (Bad, Jmp) or (Jmp, Bad) */
2321 jmp = new_r_Jmp(current_ir_graph, get_nodes_block(n));
2322 turn_into_tuple(n, pn_Cond_max);
2323 if (ta == tarval_b_true) {
2324 set_Tuple_pred(n, pn_Cond_false, new_Bad());
2325 set_Tuple_pred(n, pn_Cond_true, jmp);
2327 set_Tuple_pred(n, pn_Cond_false, jmp);
2328 set_Tuple_pred(n, pn_Cond_true, new_Bad());
2330 /* We might generate an endless loop, so keep it alive. */
2331 add_End_keepalive(get_irg_end(current_ir_graph), get_nodes_block(n));
2334 } /* transform_node_Cond */
2339 static ir_node *transform_node_And(ir_node *n)
2341 ir_node *c, *oldn = n;
2342 ir_node *a = get_And_left(n);
2343 ir_node *b = get_And_right(n);
2345 HANDLE_BINOP_PHI(tarval_and, a,b,c);
2347 } /* transform_node_And */
2352 static ir_node *transform_node_Eor(ir_node *n)
2354 ir_node *c, *oldn = n;
2355 ir_node *a = get_Eor_left(n);
2356 ir_node *b = get_Eor_right(n);
2357 ir_mode *mode = get_irn_mode(n);
2359 HANDLE_BINOP_PHI(tarval_eor, a,b,c);
2363 n = new_rd_Const(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1),
2364 mode, get_mode_null(mode));
2365 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_A_A);
2367 else if ((mode == mode_b)
2368 && (get_irn_op(a) == op_Proj)
2369 && (get_irn_mode(a) == mode_b)
2370 && (classify_tarval (value_of(b)) == TV_CLASSIFY_ONE)
2371 && (get_irn_op(get_Proj_pred(a)) == op_Cmp)) {
2372 /* The Eor negates a Cmp. The Cmp has the negated result anyways! */
2373 n = new_r_Proj(current_ir_graph, get_irn_n(n, -1), get_Proj_pred(a),
2374 mode_b, get_negated_pnc(get_Proj_proj(a), mode));
2376 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT_BOOL);
2378 else if ((mode == mode_b)
2379 && (classify_tarval (value_of(b)) == TV_CLASSIFY_ONE)) {
2380 /* The Eor is a Not. Replace it by a Not. */
2381 /* ????!!!Extend to bitfield 1111111. */
2382 n = new_r_Not(current_ir_graph, get_irn_n(n, -1), a, mode_b);
2384 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
2388 } /* transform_node_Eor */
2393 static ir_node *transform_node_Not(ir_node *n)
2395 ir_node *c, *oldn = n;
2396 ir_node *a = get_Not_op(n);
2398 HANDLE_UNOP_PHI(tarval_not,a,c);
2400 /* check for a boolean Not */
2401 if ( (get_irn_mode(n) == mode_b)
2402 && (get_irn_op(a) == op_Proj)
2403 && (get_irn_mode(a) == mode_b)
2404 && (get_irn_op(get_Proj_pred(a)) == op_Cmp)) {
2405 /* We negate a Cmp. The Cmp has the negated result anyways! */
2406 n = new_r_Proj(current_ir_graph, get_irn_n(n, -1), get_Proj_pred(a),
2407 mode_b, get_negated_pnc(get_Proj_proj(a), mode_b));
2408 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_CMP);
2411 } /* transform_node_Not */
2414 * Transform a Minus.
2416 static ir_node *transform_node_Minus(ir_node *n)
2418 ir_node *c, *oldn = n;
2419 ir_node *a = get_Minus_op(n);
2421 HANDLE_UNOP_PHI(tarval_neg,a,c);
2423 } /* transform_node_Minus */
2426 * Transform a Cast_type(Const) into a new Const_type
2428 static ir_node *transform_node_Cast(ir_node *n) {
2430 ir_node *pred = get_Cast_op(n);
2431 ir_type *tp = get_irn_type(n);
2433 if (get_irn_op(pred) == op_Const && get_Const_type(pred) != tp) {
2434 n = new_rd_Const_type(NULL, current_ir_graph, get_irn_n(pred, -1), get_irn_mode(pred),
2435 get_Const_tarval(pred), tp);
2436 DBG_OPT_CSTEVAL(oldn, n);
2437 } else if ((get_irn_op(pred) == op_SymConst) && (get_SymConst_value_type(pred) != tp)) {
2438 n = new_rd_SymConst_type(NULL, current_ir_graph, get_irn_n(pred, -1), get_SymConst_symbol(pred),
2439 get_SymConst_kind(pred), tp);
2440 DBG_OPT_CSTEVAL(oldn, n);
2444 } /* transform_node_Cast */
2447 * Transform a Proj(Div) with a non-zero value.
2448 * Removes the exceptions and routes the memory to the NoMem node.
2450 static ir_node *transform_node_Proj_Div(ir_node *proj)
2452 ir_node *n = get_Proj_pred(proj);
2453 ir_node *b = get_Div_right(n);
2457 if (value_not_zero(b, &confirm)) {
2458 /* div(x, y) && y != 0 */
2459 proj_nr = get_Proj_proj(proj);
2460 if (proj_nr == pn_Div_X_except) {
2461 /* we found an exception handler, remove it */
2462 DBG_OPT_EXC_REM(proj);
2465 else if (proj_nr == pn_Div_M) {
2466 ir_node *res = get_Div_mem(n);
2467 ir_node *new_mem = get_irg_no_mem(current_ir_graph);
2470 /* This node can only float up to the Confirm block */
2471 new_mem = new_r_Pin(current_ir_graph, get_nodes_block(confirm), new_mem);
2473 set_irn_pinned(n, op_pin_state_floats);
2474 /* this is a Div without exception, we can remove the memory edge */
2475 set_Div_mem(n, new_mem);
2480 } /* transform_node_Proj_Div */
2483 * Transform a Proj(Mod) with a non-zero value.
2484 * Removes the exceptions and routes the memory to the NoMem node.
2486 static ir_node *transform_node_Proj_Mod(ir_node *proj)
2488 ir_node *n = get_Proj_pred(proj);
2489 ir_node *b = get_Mod_right(n);
2493 if (value_not_zero(b, &confirm)) {
2494 /* mod(x, y) && y != 0 */
2495 proj_nr = get_Proj_proj(proj);
2497 if (proj_nr == pn_Mod_X_except) {
2498 /* we found an exception handler, remove it */
2499 DBG_OPT_EXC_REM(proj);
2501 } else if (proj_nr == pn_Mod_M) {
2502 ir_node *res = get_Mod_mem(n);
2503 ir_node *new_mem = get_irg_no_mem(current_ir_graph);
2506 /* This node can only float up to the Confirm block */
2507 new_mem = new_r_Pin(current_ir_graph, get_nodes_block(confirm), new_mem);
2509 set_irn_pinned(n, op_pin_state_floats);
2510 /* this is a Mod without exception, we can remove the memory edge */
2511 set_Mod_mem(n, get_irg_no_mem(current_ir_graph));
2514 else if (proj_nr == pn_Mod_res && get_Mod_left(n) == b) {
2515 /* a % a = 0 if a != 0 */
2516 ir_mode *mode = get_irn_mode(proj);
2517 ir_node *res = new_Const(mode, get_mode_null(mode));
2519 DBG_OPT_CSTEVAL(n, res);
2524 } /* transform_node_Proj_Mod */
2527 * Transform a Proj(DivMod) with a non-zero value.
2528 * Removes the exceptions and routes the memory to the NoMem node.
2530 static ir_node *transform_node_Proj_DivMod(ir_node *proj)
2532 ir_node *n = get_Proj_pred(proj);
2533 ir_node *b = get_DivMod_right(n);
2537 if (value_not_zero(b, &confirm)) {
2538 /* DivMod(x, y) && y != 0 */
2539 proj_nr = get_Proj_proj(proj);
2541 if (proj_nr == pn_DivMod_X_except) {
2542 /* we found an exception handler, remove it */
2543 DBG_OPT_EXC_REM(proj);
2546 else if (proj_nr == pn_DivMod_M) {
2547 ir_node *res = get_DivMod_mem(n);
2548 ir_node *new_mem = get_irg_no_mem(current_ir_graph);
2551 /* This node can only float up to the Confirm block */
2552 new_mem = new_r_Pin(current_ir_graph, get_nodes_block(confirm), new_mem);
2554 set_irn_pinned(n, op_pin_state_floats);
2555 /* this is a DivMod without exception, we can remove the memory edge */
2556 set_DivMod_mem(n, get_irg_no_mem(current_ir_graph));
2559 else if (proj_nr == pn_DivMod_res_mod && get_DivMod_left(n) == b) {
2560 /* a % a = 0 if a != 0 */
2561 ir_mode *mode = get_irn_mode(proj);
2562 ir_node *res = new_Const(mode, get_mode_null(mode));
2564 DBG_OPT_CSTEVAL(n, res);
2569 } /* transform_node_Proj_DivMod */
2572 * Optimizes jump tables (CondIs or CondIu) by removing all impossible cases.
2574 static ir_node *transform_node_Proj_Cond(ir_node *proj)
2576 if (get_opt_unreachable_code()) {
2577 ir_node *n = get_Proj_pred(proj);
2578 ir_node *b = get_Cond_selector(n);
2580 if (mode_is_int(get_irn_mode(b))) {
2581 tarval *tb = value_of(b);
2583 if (tb != tarval_bad) {
2584 /* we have a constant switch */
2585 long num = get_Proj_proj(proj);
2587 if (num != get_Cond_defaultProj(n)) { /* we cannot optimize default Proj's yet */
2588 if (get_tarval_long(tb) == num) {
2589 /* Do NOT create a jump here, or we will have 2 control flow ops
2590 * in a block. This case is optimized away in optimize_cf(). */
2594 /* this case will NEVER be taken, kill it */
2602 } /* transform_node_Proj_Cond */
2605 * Normalizes and optimizes Cmp nodes.
2607 static ir_node *transform_node_Proj_Cmp(ir_node *proj)
2609 if (get_opt_reassociation()) {
2610 ir_node *n = get_Proj_pred(proj);
2611 ir_node *left = get_Cmp_left(n);
2612 ir_node *right = get_Cmp_right(n);
2616 ir_mode *mode = NULL;
2617 long proj_nr = get_Proj_proj(proj);
2620 * First step: normalize the compare op
2621 * by placing the constant on the right site
2622 * or moving the lower address node to the left.
2623 * We ignore the case that both are constants
2624 * this case should be optimized away.
2626 if (get_irn_op(right) == op_Const)
2628 else if (get_irn_op(left) == op_Const) {
2633 proj_nr = get_inversed_pnc(proj_nr);
2636 else if (get_irn_idx(left) > get_irn_idx(right)) {
2642 proj_nr = get_inversed_pnc(proj_nr);
2647 * Second step: Try to reduce the magnitude
2648 * of a constant. This may help to generate better code
2649 * later and may help to normalize more compares.
2650 * Of course this is only possible for integer values.
2653 mode = get_irn_mode(c);
2654 tv = get_Const_tarval(c);
2656 if (tv != tarval_bad) {
2657 /* the following optimization is possible on modes without Overflow
2658 * on Unary Minus or on == and !=:
2659 * -a CMP c ==> a swap(CMP) -c
2661 * Beware: for two-complement Overflow may occur, so only == and != can
2662 * be optimized, see this:
2663 * -MININT < 0 =/=> MININT > 0 !!!
2665 if (get_opt_constant_folding() && get_irn_op(left) == op_Minus &&
2666 (!mode_overflow_on_unary_Minus(mode) ||
2667 (mode_is_int(mode) && (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg)))) {
2668 left = get_Minus_op(left);
2669 tv = tarval_sub(get_mode_null(mode), tv);
2671 proj_nr = get_inversed_pnc(proj_nr);
2675 /* for integer modes, we have more */
2676 if (mode_is_int(mode)) {
2677 /* Ne includes Unordered which is not possible on integers.
2678 * However, frontends often use this wrong, so fix it here */
2679 if (proj_nr & pn_Cmp_Uo) {
2680 proj_nr &= ~pn_Cmp_Uo;
2681 set_Proj_proj(proj, proj_nr);
2684 /* c > 0 : a < c ==> a <= (c-1) a >= c ==> a > (c-1) */
2685 if ((proj_nr == pn_Cmp_Lt || proj_nr == pn_Cmp_Ge) &&
2686 tarval_cmp(tv, get_mode_null(mode)) == pn_Cmp_Gt) {
2687 tv = tarval_sub(tv, get_mode_one(mode));
2689 proj_nr ^= pn_Cmp_Eq;
2692 /* c < 0 : a > c ==> a >= (c+1) a <= c ==> a < (c+1) */
2693 else if ((proj_nr == pn_Cmp_Gt || proj_nr == pn_Cmp_Le) &&
2694 tarval_cmp(tv, get_mode_null(mode)) == pn_Cmp_Lt) {
2695 tv = tarval_add(tv, get_mode_one(mode));
2697 proj_nr ^= pn_Cmp_Eq;
2701 /* the following reassociations work only for == and != */
2702 if (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) {
2704 /* a-b == 0 ==> a == b, a-b != 0 ==> a != b */
2705 if (classify_tarval(tv) == TV_CLASSIFY_NULL && get_irn_op(left) == op_Sub) {
2706 right = get_Sub_right(left);
2707 left = get_Sub_left(left);
2709 tv = value_of(right);
2713 if (tv != tarval_bad) {
2714 ir_op *op = get_irn_op(left);
2716 /* a-c1 == c2 ==> a == c2+c1, a-c1 != c2 ==> a != c2+c1 */
2718 ir_node *c1 = get_Sub_right(left);
2719 tarval *tv2 = value_of(c1);
2721 if (tv2 != tarval_bad) {
2722 tv2 = tarval_add(tv, value_of(c1));
2724 if (tv2 != tarval_bad) {
2725 left = get_Sub_left(left);
2731 /* a+c1 == c2 ==> a == c2-c1, a+c1 != c2 ==> a != c2-c1 */
2732 else if (op == op_Add) {
2733 ir_node *a_l = get_Add_left(left);
2734 ir_node *a_r = get_Add_right(left);
2738 if (get_irn_op(a_l) == op_Const) {
2740 tv2 = value_of(a_l);
2744 tv2 = value_of(a_r);
2747 if (tv2 != tarval_bad) {
2748 tv2 = tarval_sub(tv, tv2);
2750 if (tv2 != tarval_bad) {
2757 /* -a == c ==> a == -c, -a != c ==> a != -c */
2758 else if (op == op_Minus) {
2759 tarval *tv2 = tarval_sub(get_mode_null(mode), tv);
2761 if (tv2 != tarval_bad) {
2762 left = get_Minus_op(left);
2769 /* the following reassociations work only for <= */
2770 else if (proj_nr == pn_Cmp_Le || proj_nr == pn_Cmp_Lt) {
2771 if (tv != tarval_bad) {
2772 ir_op *op = get_irn_op(left);
2774 /* c >= 0 : Abs(a) <= c ==> (unsigned)(a + c) <= 2*c */
2782 * optimization for AND:
2784 * And(x, C) == C ==> And(x, C) != 0
2785 * And(x, C) != C ==> And(X, C) == 0
2787 * if C is a single Bit constant.
2789 if ((proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) &&
2790 (get_irn_op(left) == op_And)) {
2791 if (is_single_bit_tarval(tv)) {
2792 /* check for Constant's match. We have check hare the tarvals,
2793 because our const might be changed */
2794 ir_node *la = get_And_left(left);
2795 ir_node *ra = get_And_right(left);
2796 if ((is_Const(la) && get_Const_tarval(la) == tv) ||
2797 (is_Const(ra) && get_Const_tarval(ra) == tv)) {
2798 /* fine: do the transformation */
2799 tv = get_mode_null(get_tarval_mode(tv));
2800 proj_nr ^= pn_Cmp_Leg;
2805 } /* tarval != bad */
2809 ir_node *block = get_irn_n(n, -1); /* Beware of get_nodes_Block() */
2811 if (changed & 2) /* need a new Const */
2812 right = new_Const(mode, tv);
2814 /* create a new compare */
2815 n = new_rd_Cmp(get_irn_dbg_info(n), current_ir_graph, block,
2818 set_Proj_pred(proj, n);
2819 set_Proj_proj(proj, proj_nr);
2823 } /* transform_node_Proj_Cmp */
2826 * Does all optimizations on nodes that must be done on it's Proj's
2827 * because of creating new nodes.
2829 static ir_node *transform_node_Proj(ir_node *proj)
2831 ir_node *n = get_Proj_pred(proj);
2833 switch (get_irn_opcode(n)) {
2835 return transform_node_Proj_Div(proj);
2838 return transform_node_Proj_Mod(proj);
2841 return transform_node_Proj_DivMod(proj);
2844 return transform_node_Proj_Cond(proj);
2847 return transform_node_Proj_Cmp(proj);
2850 /* should not happen, but if it does will be optimized away */
2851 return equivalent_node_Proj(proj);
2857 } /* transform_node_Proj */
2860 * Move Confirms down through Phi nodes.
2862 static ir_node *transform_node_Phi(ir_node *phi) {
2864 ir_mode *mode = get_irn_mode(phi);
2866 if (mode_is_reference(mode)) {
2867 n = get_irn_arity(phi);
2869 /* Beware of Phi0 */
2871 ir_node *pred = get_irn_n(phi, 0);
2872 ir_node *bound, *new_Phi, *block, **in;
2875 if (! is_Confirm(pred))
2878 bound = get_Confirm_bound(pred);
2879 pnc = get_Confirm_cmp(pred);
2881 NEW_ARR_A(ir_node *, in, n);
2882 in[0] = get_Confirm_value(pred);
2884 for (i = 1; i < n; ++i) {
2885 pred = get_irn_n(phi, i);
2887 if (! is_Confirm(pred) ||
2888 get_Confirm_bound(pred) != bound ||
2889 get_Confirm_cmp(pred) != pnc)
2891 in[i] = get_Confirm_value(pred);
2893 /* move the Confirm nodes "behind" the Phi */
2894 block = get_irn_n(phi, -1);
2895 new_Phi = new_r_Phi(current_ir_graph, block, n, in, get_irn_mode(phi));
2896 return new_r_Confirm(current_ir_graph, block, new_Phi, bound, pnc);
2900 } /* transform_node_Phi */
2903 * Returns the operands of a commutative bin-op, if one operand is
2904 * a const, it is returned as the second one.
2906 static void get_comm_Binop_Ops(ir_node *binop, ir_node **a, ir_node **c)
2908 ir_node *op_a = get_binop_left(binop);
2909 ir_node *op_b = get_binop_right(binop);
2911 assert(is_op_commutative(get_irn_op(binop)));
2913 if (get_irn_op(op_a) == op_Const) {
2921 } /* get_comm_Binop_Ops */
2924 * Optimize a Or(And(Or(And(v,c4),c3),c2),c1) pattern if possible.
2925 * Such pattern may arise in bitfield stores.
2927 * value c4 value c4 & c2
2928 * AND c3 AND c1 | c3
2933 static ir_node *transform_node_Or_bf_store(ir_node *or)
2937 ir_node *and_l, *c3;
2938 ir_node *value, *c4;
2939 ir_node *new_and, *new_const, *block;
2940 ir_mode *mode = get_irn_mode(or);
2942 tarval *tv1, *tv2, *tv3, *tv4, *tv, *n_tv4, *n_tv2;
2944 get_comm_Binop_Ops(or, &and, &c1);
2945 if ((get_irn_op(c1) != op_Const) || (get_irn_op(and) != op_And))
2948 get_comm_Binop_Ops(and, &or_l, &c2);
2949 if ((get_irn_op(c2) != op_Const) || (get_irn_op(or_l) != op_Or))
2952 get_comm_Binop_Ops(or_l, &and_l, &c3);
2953 if ((get_irn_op(c3) != op_Const) || (get_irn_op(and_l) != op_And))
2956 get_comm_Binop_Ops(and_l, &value, &c4);
2957 if (get_irn_op(c4) != op_Const)
2960 /* ok, found the pattern, check for conditions */
2961 assert(mode == get_irn_mode(and));
2962 assert(mode == get_irn_mode(or_l));
2963 assert(mode == get_irn_mode(and_l));
2965 tv1 = get_Const_tarval(c1);
2966 tv2 = get_Const_tarval(c2);
2967 tv3 = get_Const_tarval(c3);
2968 tv4 = get_Const_tarval(c4);
2970 tv = tarval_or(tv4, tv2);
2971 if (classify_tarval(tv) != TV_CLASSIFY_ALL_ONE) {
2972 /* have at least one 0 at the same bit position */
2976 n_tv4 = tarval_not(tv4);
2977 if (tv3 != tarval_and(tv3, n_tv4)) {
2978 /* bit in the or_mask is outside the and_mask */
2982 n_tv2 = tarval_not(tv2);
2983 if (tv1 != tarval_and(tv1, n_tv2)) {
2984 /* bit in the or_mask is outside the and_mask */
2988 /* ok, all conditions met */
2989 block = get_irn_n(or, -1);
2991 new_and = new_r_And(current_ir_graph, block,
2992 value, new_r_Const(current_ir_graph, block, mode, tarval_and(tv4, tv2)), mode);
2994 new_const = new_r_Const(current_ir_graph, block, mode, tarval_or(tv3, tv1));
2996 set_Or_left(or, new_and);
2997 set_Or_right(or, new_const);
2999 /* check for more */
3000 return transform_node_Or_bf_store(or);
3001 } /* transform_node_Or_bf_store */
3004 * Optimize an Or(shl(x, c), shr(x, bits - c)) into a Rot
3006 static ir_node *transform_node_Or_Rot(ir_node *or)
3008 ir_mode *mode = get_irn_mode(or);
3009 ir_node *shl, *shr, *block;
3010 ir_node *irn, *x, *c1, *c2, *v, *sub, *n;
3013 if (! mode_is_int(mode))
3016 shl = get_binop_left(or);
3017 shr = get_binop_right(or);
3019 if (get_irn_op(shl) == op_Shr) {
3020 if (get_irn_op(shr) != op_Shl)
3027 else if (get_irn_op(shl) != op_Shl)
3029 else if (get_irn_op(shr) != op_Shr)
3032 x = get_Shl_left(shl);
3033 if (x != get_Shr_left(shr))
3036 c1 = get_Shl_right(shl);
3037 c2 = get_Shr_right(shr);
3038 if (get_irn_op(c1) == op_Const && get_irn_op(c2) == op_Const) {
3039 tv1 = get_Const_tarval(c1);
3040 if (! tarval_is_long(tv1))
3043 tv2 = get_Const_tarval(c2);
3044 if (! tarval_is_long(tv2))
3047 if (get_tarval_long(tv1) + get_tarval_long(tv2)
3048 != get_mode_size_bits(mode))
3051 /* yet, condition met */
3052 block = get_irn_n(or, -1);
3054 n = new_r_Rot(current_ir_graph, block, x, c1, mode);
3056 DBG_OPT_ALGSIM1(or, shl, shr, n, FS_OPT_OR_SHFT_TO_ROT);
3059 else if (get_irn_op(c1) == op_Sub) {
3063 if (get_Sub_right(sub) != v)
3066 c1 = get_Sub_left(sub);
3067 if (get_irn_op(c1) != op_Const)
3070 tv1 = get_Const_tarval(c1);
3071 if (! tarval_is_long(tv1))
3074 if (get_tarval_long(tv1) != get_mode_size_bits(mode))
3077 /* yet, condition met */
3078 block = get_nodes_block(or);
3080 /* a Rot right is not supported, so use a rot left */
3081 n = new_r_Rot(current_ir_graph, block, x, sub, mode);
3083 DBG_OPT_ALGSIM0(or, n, FS_OPT_OR_SHFT_TO_ROT);
3086 else if (get_irn_op(c2) == op_Sub) {
3090 c1 = get_Sub_left(sub);
3091 if (get_irn_op(c1) != op_Const)
3094 tv1 = get_Const_tarval(c1);
3095 if (! tarval_is_long(tv1))
3098 if (get_tarval_long(tv1) != get_mode_size_bits(mode))
3101 /* yet, condition met */
3102 block = get_irn_n(or, -1);
3105 n = new_r_Rot(current_ir_graph, block, x, v, mode);
3107 DBG_OPT_ALGSIM0(or, n, FS_OPT_OR_SHFT_TO_ROT);
3112 } /* transform_node_Or_Rot */
3117 static ir_node *transform_node_Or(ir_node *n)
3119 ir_node *c, *oldn = n;
3120 ir_node *a = get_Or_left(n);
3121 ir_node *b = get_Or_right(n);
3123 HANDLE_BINOP_PHI(tarval_or, a,b,c);
3125 n = transform_node_Or_bf_store(n);
3126 n = transform_node_Or_Rot(n);
3129 } /* transform_node_Or */
3133 static ir_node *transform_node(ir_node *n);
3136 * Optimize (a >> c1) >> c2), works for Shr, Shrs, Shl.
3138 * Should be moved to reassociation?
3140 static ir_node *transform_node_shift(ir_node *n)
3142 ir_node *left, *right;
3143 tarval *tv1, *tv2, *res;
3145 int modulo_shf, flag;
3147 left = get_binop_left(n);
3149 /* different operations */
3150 if (get_irn_op(left) != get_irn_op(n))
3153 right = get_binop_right(n);
3154 tv1 = value_of(right);
3155 if (tv1 == tarval_bad)
3158 tv2 = value_of(get_binop_right(left));
3159 if (tv2 == tarval_bad)
3162 res = tarval_add(tv1, tv2);
3164 /* beware: a simple replacement works only, if res < modulo shift */
3165 mode = get_irn_mode(n);
3169 modulo_shf = get_mode_modulo_shift(mode);
3170 if (modulo_shf > 0) {
3171 tarval *modulo = new_tarval_from_long(modulo_shf, get_tarval_mode(res));
3173 if (tarval_cmp(res, modulo) & pn_Cmp_Lt)
3180 /* ok, we can replace it */
3181 ir_node *in[2], *irn, *block = get_irn_n(n, -1);
3183 in[0] = get_binop_left(left);
3184 in[1] = new_r_Const(current_ir_graph, block, get_tarval_mode(res), res);
3186 irn = new_ir_node(NULL, current_ir_graph, block, get_irn_op(n), mode, 2, in);
3188 DBG_OPT_ALGSIM0(n, irn, FS_OPT_REASSOC_SHIFT);
3190 return transform_node(irn);
3193 } /* transform_node_shift */
3198 static ir_node *transform_node_Shr(ir_node *n)
3200 ir_node *c, *oldn = n;
3201 ir_node *a = get_Shr_left(n);
3202 ir_node *b = get_Shr_right(n);
3204 HANDLE_BINOP_PHI(tarval_shr, a, b, c);
3205 return transform_node_shift(n);
3206 } /* transform_node_Shr */
3211 static ir_node *transform_node_Shrs(ir_node *n)
3213 ir_node *c, *oldn = n;
3214 ir_node *a = get_Shrs_left(n);
3215 ir_node *b = get_Shrs_right(n);
3217 HANDLE_BINOP_PHI(tarval_shrs, a, b, c);
3218 return transform_node_shift(n);
3219 } /* transform_node_Shrs */
3224 static ir_node *transform_node_Shl(ir_node *n)
3226 ir_node *c, *oldn = n;
3227 ir_node *a = get_Shl_left(n);
3228 ir_node *b = get_Shl_right(n);
3230 HANDLE_BINOP_PHI(tarval_shl, a, b, c);
3231 return transform_node_shift(n);
3232 } /* transform_node_Shl */
3235 * Remove dead blocks and nodes in dead blocks
3236 * in keep alive list. We do not generate a new End node.
3238 static ir_node *transform_node_End(ir_node *n) {
3239 int i, n_keepalives = get_End_n_keepalives(n);
3241 for (i = 0; i < n_keepalives; ++i) {
3242 ir_node *ka = get_End_keepalive(n, i);
3244 if (is_Block_dead(ka)) {
3245 set_End_keepalive(n, i, new_Bad());
3248 else if (is_irn_pinned_in_irg(ka) && is_Block_dead(get_nodes_block(ka)))
3249 set_End_keepalive(n, i, new_Bad());
3252 } /* transform_node_End */
3255 * Optimize a Mux into some simpler cases.
3257 static ir_node *transform_node_Mux(ir_node *n)
3259 ir_node *oldn = n, *sel = get_Mux_sel(n);
3260 ir_mode *mode = get_irn_mode(n);
3262 if (get_irn_op(sel) == op_Proj && !mode_honor_signed_zeros(mode)) {
3263 ir_node *cmp = get_Proj_pred(sel);
3264 long proj_nr = get_Proj_proj(sel);
3265 ir_node *f = get_Mux_false(n);
3266 ir_node *t = get_Mux_true(n);
3268 if (get_irn_op(cmp) == op_Cmp && classify_Const(get_Cmp_right(cmp)) == CNST_NULL) {
3269 ir_node *block = get_irn_n(n, -1);
3272 * Note: normalization puts the constant on the right site,
3273 * so we check only one case.
3275 * Note further that these optimization work even for floating point
3276 * with NaN's because -NaN == NaN.
3277 * However, if +0 and -0 is handled differently, we cannot use the first one.
3279 if (get_irn_op(f) == op_Minus &&
3280 get_Minus_op(f) == t &&
3281 get_Cmp_left(cmp) == t) {
3283 if (proj_nr == pn_Cmp_Ge || proj_nr == pn_Cmp_Gt) {
3284 /* Mux(a >=/> 0, -a, a) ==> Abs(a) */
3285 n = new_rd_Abs(get_irn_dbg_info(n),
3289 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_ABS);
3292 else if (proj_nr == pn_Cmp_Le || proj_nr == pn_Cmp_Lt) {
3293 /* Mux(a <=/< 0, -a, a) ==> Minus(Abs(a)) */
3294 n = new_rd_Abs(get_irn_dbg_info(n),
3298 n = new_rd_Minus(get_irn_dbg_info(n),
3303 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_ABS);
3307 else if (get_irn_op(t) == op_Minus &&
3308 get_Minus_op(t) == f &&
3309 get_Cmp_left(cmp) == f) {
3311 if (proj_nr == pn_Cmp_Le || proj_nr == pn_Cmp_Lt) {
3312 /* Mux(a <=/< 0, a, -a) ==> Abs(a) */
3313 n = new_rd_Abs(get_irn_dbg_info(n),
3317 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_ABS);
3320 else if (proj_nr == pn_Cmp_Ge || proj_nr == pn_Cmp_Gt) {
3321 /* Mux(a >=/> 0, a, -a) ==> Minus(Abs(a)) */
3322 n = new_rd_Abs(get_irn_dbg_info(n),
3326 n = new_rd_Minus(get_irn_dbg_info(n),
3331 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_ABS);
3336 if (mode_is_int(mode) && mode_is_signed(mode) &&
3337 get_mode_arithmetic(mode) == irma_twos_complement) {
3338 ir_node *x = get_Cmp_left(cmp);
3340 /* the following optimization works only with signed integer two-complement mode */
3342 if (mode == get_irn_mode(x)) {
3344 * FIXME: this restriction is two rigid, as it would still
3345 * work if mode(x) = Hs and mode == Is, but at least it removes
3348 if ((proj_nr == pn_Cmp_Lt || proj_nr == pn_Cmp_Le) &&
3349 classify_Const(t) == CNST_ALL_ONE &&
3350 classify_Const(f) == CNST_NULL) {
3352 * Mux(x:T </<= 0, 0, -1) -> Shrs(x, sizeof_bits(T) - 1)
3356 n = new_rd_Shrs(get_irn_dbg_info(n),
3357 current_ir_graph, block, x,
3358 new_r_Const_long(current_ir_graph, block, mode_Iu,
3359 get_mode_size_bits(mode) - 1),
3361 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_SHR);
3364 else if ((proj_nr == pn_Cmp_Gt || proj_nr == pn_Cmp_Ge) &&
3365 classify_Const(t) == CNST_ONE &&
3366 classify_Const(f) == CNST_NULL) {
3368 * Mux(x:T >/>= 0, 0, 1) -> Shr(-x, sizeof_bits(T) - 1)
3372 n = new_rd_Shr(get_irn_dbg_info(n),
3373 current_ir_graph, block,
3374 new_r_Minus(current_ir_graph, block, x, mode),
3375 new_r_Const_long(current_ir_graph, block, mode_Iu,
3376 get_mode_size_bits(mode) - 1),
3378 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_SHR);
3385 return arch_transform_node_Mux(n);
3386 } /* transform_node_Mux */
3389 * Optimize a Psi into some simpler cases.
3391 static ir_node *transform_node_Psi(ir_node *n) {
3393 return transform_node_Mux(n);
3396 } /* transform_node_Psi */
3399 * Tries several [inplace] [optimizing] transformations and returns an
3400 * equivalent node. The difference to equivalent_node() is that these
3401 * transformations _do_ generate new nodes, and thus the old node must
3402 * not be freed even if the equivalent node isn't the old one.
3404 static ir_node *transform_node(ir_node *n)
3406 if (n->op->ops.transform_node)
3407 n = n->op->ops.transform_node(n);
3409 } /* transform_node */
3412 * sSets the default transform node operation for an ir_op_ops.
3414 * @param code the opcode for the default operation
3415 * @param ops the operations initialized
3420 static ir_op_ops *firm_set_default_transform_node(opcode code, ir_op_ops *ops)
3424 ops->transform_node = transform_node_##a; \
3457 } /* firm_set_default_transform_node */
3460 /* **************** Common Subexpression Elimination **************** */
3462 /** The size of the hash table used, should estimate the number of nodes
3464 #define N_IR_NODES 512
3466 /** Compares the attributes of two Const nodes. */
3467 static int node_cmp_attr_Const(ir_node *a, ir_node *b) {
3468 return (get_Const_tarval(a) != get_Const_tarval(b))
3469 || (get_Const_type(a) != get_Const_type(b));
3470 } /* node_cmp_attr_Const */
3472 /** Compares the attributes of two Proj nodes. */
3473 static int node_cmp_attr_Proj(ir_node *a, ir_node *b) {
3474 return get_irn_proj_attr (a) != get_irn_proj_attr (b);
3475 } /* node_cmp_attr_Proj */
3477 /** Compares the attributes of two Filter nodes. */
3478 static int node_cmp_attr_Filter(ir_node *a, ir_node *b) {
3479 return get_Filter_proj(a) != get_Filter_proj(b);
3480 } /* node_cmp_attr_Filter */
3482 /** Compares the attributes of two Alloc nodes. */
3483 static int node_cmp_attr_Alloc(ir_node *a, ir_node *b) {
3484 return (get_irn_alloc_attr(a).where != get_irn_alloc_attr(b).where)
3485 || (get_irn_alloc_attr(a).type != get_irn_alloc_attr(b).type);
3486 } /* node_cmp_attr_Alloc */
3488 /** Compares the attributes of two Free nodes. */
3489 static int node_cmp_attr_Free(ir_node *a, ir_node *b) {
3490 return (get_irn_free_attr(a).where != get_irn_free_attr(b).where)
3491 || (get_irn_free_attr(a).type != get_irn_free_attr(b).type);
3492 } /* node_cmp_attr_Free */
3494 /** Compares the attributes of two SymConst nodes. */
3495 static int node_cmp_attr_SymConst(ir_node *a, ir_node *b) {
3496 return (get_irn_symconst_attr(a).num != get_irn_symconst_attr(b).num)
3497 || (get_irn_symconst_attr(a).sym.type_p != get_irn_symconst_attr(b).sym.type_p)
3498 || (get_irn_symconst_attr(a).tp != get_irn_symconst_attr(b).tp);
3499 } /* node_cmp_attr_SymConst */
3501 /** Compares the attributes of two Call nodes. */
3502 static int node_cmp_attr_Call(ir_node *a, ir_node *b) {
3503 return (get_irn_call_attr(a) != get_irn_call_attr(b));
3504 } /* node_cmp_attr_Call */
3506 /** Compares the attributes of two Sel nodes. */
3507 static int node_cmp_attr_Sel(ir_node *a, ir_node *b) {
3508 return (get_irn_sel_attr(a).ent->kind != get_irn_sel_attr(b).ent->kind)
3509 || (get_irn_sel_attr(a).ent->name != get_irn_sel_attr(b).ent->name)
3510 || (get_irn_sel_attr(a).ent->owner != get_irn_sel_attr(b).ent->owner)
3511 || (get_irn_sel_attr(a).ent->ld_name != get_irn_sel_attr(b).ent->ld_name)
3512 || (get_irn_sel_attr(a).ent->type != get_irn_sel_attr(b).ent->type);
3513 } /* node_cmp_attr_Sel */
3515 /** Compares the attributes of two Phi nodes. */
3516 static int node_cmp_attr_Phi(ir_node *a, ir_node *b) {
3517 return get_irn_phi_attr (a) != get_irn_phi_attr (b);
3518 } /* node_cmp_attr_Phi */
3520 /** Compares the attributes of two Conv nodes. */
3521 static int node_cmp_attr_Conv(ir_node *a, ir_node *b) {
3522 return get_Conv_strict(a) != get_Conv_strict(b);
3523 } /* node_cmp_attr_Conv */
3525 /** Compares the attributes of two Cast nodes. */
3526 static int node_cmp_attr_Cast(ir_node *a, ir_node *b) {
3527 return get_Cast_type(a) != get_Cast_type(b);
3528 } /* node_cmp_attr_Cast */
3530 /** Compares the attributes of two Load nodes. */
3531 static int node_cmp_attr_Load(ir_node *a, ir_node *b) {
3532 if (get_Load_volatility(a) == volatility_is_volatile ||
3533 get_Load_volatility(b) == volatility_is_volatile)
3534 /* NEVER do CSE on volatile Loads */
3537 return get_Load_mode(a) != get_Load_mode(b);
3538 } /* node_cmp_attr_Load */
3540 /** Compares the attributes of two Store nodes. */
3541 static int node_cmp_attr_Store(ir_node *a, ir_node *b) {
3542 /* NEVER do CSE on volatile Stores */
3543 return (get_Store_volatility(a) == volatility_is_volatile ||
3544 get_Store_volatility(b) == volatility_is_volatile);
3545 } /* node_cmp_attr_Store */
3547 /** Compares the attributes of two Confirm nodes. */
3548 static int node_cmp_attr_Confirm(ir_node *a, ir_node *b) {
3549 return (get_Confirm_cmp(a) != get_Confirm_cmp(b));
3550 } /* node_cmp_attr_Confirm */
3553 * Set the default node attribute compare operation for an ir_op_ops.
3555 * @param code the opcode for the default operation
3556 * @param ops the operations initialized
3561 static ir_op_ops *firm_set_default_node_cmp_attr(opcode code, ir_op_ops *ops)
3565 ops->node_cmp_attr = node_cmp_attr_##a; \
3589 } /* firm_set_default_node_cmp_attr */
3592 * Compare function for two nodes in the hash table. Gets two
3593 * nodes as parameters. Returns 0 if the nodes are a cse.
3595 int identities_cmp(const void *elt, const void *key)
3603 if (a == b) return 0;
3605 if ((get_irn_op(a) != get_irn_op(b)) ||
3606 (get_irn_mode(a) != get_irn_mode(b))) return 1;
3608 /* compare if a's in and b's in are of equal length */
3609 irn_arity_a = get_irn_intra_arity (a);
3610 if (irn_arity_a != get_irn_intra_arity(b))
3613 /* for block-local cse and op_pin_state_pinned nodes: */
3614 if (!get_opt_global_cse() || (get_irn_pinned(a) == op_pin_state_pinned)) {
3615 if (get_irn_intra_n(a, -1) != get_irn_intra_n(b, -1))
3619 /* compare a->in[0..ins] with b->in[0..ins] */
3620 for (i = 0; i < irn_arity_a; i++)
3621 if (get_irn_intra_n(a, i) != get_irn_intra_n(b, i))
3625 * here, we already now that the nodes are identical except their
3628 if (a->op->ops.node_cmp_attr)
3629 return a->op->ops.node_cmp_attr(a, b);
3632 } /* identities_cmp */
3635 * Calculate a hash value of a node.
3637 unsigned ir_node_hash(ir_node *node)
3642 if (node->op == op_Const) {
3643 /* special value for const, as they only differ in their tarval. */
3644 h = HASH_PTR(node->attr.con.tv);
3645 h = 9*h + HASH_PTR(get_irn_mode(node));
3646 } else if (node->op == op_SymConst) {
3647 /* special value for const, as they only differ in their symbol. */
3648 h = HASH_PTR(node->attr.symc.sym.type_p);
3649 h = 9*h + HASH_PTR(get_irn_mode(node));
3652 /* hash table value = 9*(9*(9*(9*(9*arity+in[0])+in[1])+ ...)+mode)+code */
3653 h = irn_arity = get_irn_intra_arity(node);
3655 /* consider all in nodes... except the block if not a control flow. */
3656 for (i = is_cfop(node) ? -1 : 0; i < irn_arity; i++) {
3657 h = 9*h + HASH_PTR(get_irn_intra_n(node, i));
3661 h = 9*h + HASH_PTR(get_irn_mode(node));
3663 h = 9*h + HASH_PTR(get_irn_op(node));
3667 } /* ir_node_hash */
3669 pset *new_identities(void) {
3670 return new_pset(identities_cmp, N_IR_NODES);
3671 } /* new_identities */
3673 void del_identities(pset *value_table) {
3674 del_pset(value_table);
3675 } /* del_identities */
3678 * Return the canonical node computing the same value as n.
3679 * Looks up the node in a hash table.
3681 * For Const nodes this is performed in the constructor, too. Const
3682 * nodes are extremely time critical because of their frequent use in
3683 * constant string arrays.
3685 static INLINE ir_node *identify(pset *value_table, ir_node *n)
3689 if (!value_table) return n;
3691 if (get_opt_reassociation()) {
3692 if (is_op_commutative(get_irn_op(n))) {
3693 ir_node *l = get_binop_left(n);
3694 ir_node *r = get_binop_right(n);
3696 /* for commutative operators perform a OP b == b OP a */
3697 if (get_irn_idx(l) > get_irn_idx(r)) {
3698 set_binop_left(n, r);
3699 set_binop_right(n, l);
3704 o = pset_find(value_table, n, ir_node_hash(n));
3713 * During construction we set the op_pin_state_pinned flag in the graph right when the
3714 * optimization is performed. The flag turning on procedure global cse could
3715 * be changed between two allocations. This way we are safe.
3717 static INLINE ir_node *identify_cons(pset *value_table, ir_node *n) {
3720 n = identify(value_table, n);
3721 if (get_irn_n(old, -1) != get_irn_n(n, -1))
3722 set_irg_pinned(current_ir_graph, op_pin_state_floats);
3724 } /* identify_cons */
3727 * Return the canonical node computing the same value as n.
3728 * Looks up the node in a hash table, enters it in the table
3729 * if it isn't there yet.
3731 ir_node *identify_remember(pset *value_table, ir_node *n)
3735 if (!value_table) return n;
3737 if (get_opt_reassociation()) {
3738 if (is_op_commutative(get_irn_op(n))) {
3739 ir_node *l = get_binop_left(n);
3740 ir_node *r = get_binop_right(n);
3742 /* for commutative operators perform a OP b == b OP a */
3744 set_binop_left(n, r);
3745 set_binop_right(n, l);
3750 /* lookup or insert in hash table with given hash key. */
3751 o = pset_insert (value_table, n, ir_node_hash (n));
3758 } /* identify_remember */
3760 /* Add a node to the identities value table. */
3761 void add_identities(pset *value_table, ir_node *node) {
3762 if (get_opt_cse() && is_no_Block(node))
3763 identify_remember(value_table, node);
3764 } /* add_identities */
3766 /* Visit each node in the value table of a graph. */
3767 void visit_all_identities(ir_graph *irg, irg_walk_func visit, void *env) {
3769 ir_graph *rem = current_ir_graph;
3771 current_ir_graph = irg;
3772 foreach_pset(irg->value_table, node)
3774 current_ir_graph = rem;
3775 } /* visit_all_identities */
3778 * Garbage in, garbage out. If a node has a dead input, i.e., the
3779 * Bad node is input to the node, return the Bad node.
3781 static INLINE ir_node *gigo(ir_node *node)
3784 ir_op *op = get_irn_op(node);
3786 /* remove garbage blocks by looking at control flow that leaves the block
3787 and replacing the control flow by Bad. */
3788 if (get_irn_mode(node) == mode_X) {
3789 ir_node *block = get_nodes_block(skip_Proj(node));
3791 /* Don't optimize nodes in immature blocks. */
3792 if (!get_Block_matured(block)) return node;
3793 /* Don't optimize End, may have Bads. */
3794 if (op == op_End) return node;
3796 if (is_Block(block)) {
3797 irn_arity = get_irn_arity(block);
3798 for (i = 0; i < irn_arity; i++) {
3799 if (!is_Bad(get_irn_n(block, i)))
3802 if (i == irn_arity) return new_Bad();
3806 /* Blocks, Phis and Tuples may have dead inputs, e.g., if one of the
3807 blocks predecessors is dead. */
3808 if (op != op_Block && op != op_Phi && op != op_Tuple) {
3809 irn_arity = get_irn_arity(node);
3812 * Beware: we can only read the block of a non-floating node.
3814 if (is_irn_pinned_in_irg(node) &&
3815 is_Block_dead(get_nodes_block(node)))
3818 for (i = 0; i < irn_arity; i++) {
3819 ir_node *pred = get_irn_n(node, i);
3824 /* Propagating Unknowns here seems to be a bad idea, because
3825 sometimes we need a node as a input and did not want that
3827 However, it might be useful to move this into a later phase
3828 (if you think that optimizing such code is useful). */
3829 if (is_Unknown(pred) && mode_is_data(get_irn_mode(node)))
3830 return new_Unknown(get_irn_mode(node));
3835 /* With this code we violate the agreement that local_optimize
3836 only leaves Bads in Block, Phi and Tuple nodes. */
3837 /* If Block has only Bads as predecessors it's garbage. */
3838 /* If Phi has only Bads as predecessors it's garbage. */
3839 if ((op == op_Block && get_Block_matured(node)) || op == op_Phi) {
3840 irn_arity = get_irn_arity(node);
3841 for (i = 0; i < irn_arity; i++) {
3842 if (!is_Bad(get_irn_n(node, i))) break;
3844 if (i == irn_arity) node = new_Bad();
3851 * These optimizations deallocate nodes from the obstack.
3852 * It can only be called if it is guaranteed that no other nodes
3853 * reference this one, i.e., right after construction of a node.
3855 * current_ir_graph must be set to the graph of the node!
3857 ir_node *optimize_node(ir_node *n)
3861 opcode iro = get_irn_opcode(n);
3863 /* Always optimize Phi nodes: part of the construction. */
3864 if ((!get_opt_optimize()) && (iro != iro_Phi)) return n;
3866 /* constant expression evaluation / constant folding */
3867 if (get_opt_constant_folding()) {
3868 /* neither constants nor Tuple values can be evaluated */
3869 if (iro != iro_Const && (get_irn_mode(n) != mode_T)) {
3870 /* try to evaluate */
3871 tv = computed_value(n);
3872 if (tv != tarval_bad) {
3874 ir_type *old_tp = get_irn_type(n);
3875 int i, arity = get_irn_arity(n);
3879 * Try to recover the type of the new expression.
3881 for (i = 0; i < arity && !old_tp; ++i)
3882 old_tp = get_irn_type(get_irn_n(n, i));
3885 * we MUST copy the node here temporary, because it's still needed
3886 * for DBG_OPT_CSTEVAL
3888 node_size = offsetof(ir_node, attr) + n->op->attr_size;
3889 oldn = alloca(node_size);
3891 memcpy(oldn, n, node_size);
3892 CLONE_ARR_A(ir_node *, oldn->in, n->in);
3894 /* ARG, copy the in array, we need it for statistics */
3895 memcpy(oldn->in, n->in, ARR_LEN(n->in) * sizeof(n->in[0]));
3897 /* note the inplace edges module */
3898 edges_node_deleted(n, current_ir_graph);
3900 /* evaluation was successful -- replace the node. */
3901 irg_kill_node(current_ir_graph, n);
3902 nw = new_Const(get_tarval_mode (tv), tv);
3904 if (old_tp && get_type_mode(old_tp) == get_tarval_mode (tv))
3905 set_Const_type(nw, old_tp);
3906 DBG_OPT_CSTEVAL(oldn, nw);
3912 /* remove unnecessary nodes */
3913 if (get_opt_constant_folding() ||
3914 (iro == iro_Phi) || /* always optimize these nodes. */
3916 (iro == iro_Proj) ||
3917 (iro == iro_Block) ) /* Flags tested local. */
3918 n = equivalent_node(n);
3920 optimize_preds(n); /* do node specific optimizations of nodes predecessors. */
3922 /* Common Subexpression Elimination.
3924 * Checks whether n is already available.
3925 * The block input is used to distinguish different subexpressions. Right
3926 * now all nodes are op_pin_state_pinned to blocks, i.e., the CSE only finds common
3927 * subexpressions within a block.
3930 n = identify_cons(current_ir_graph->value_table, n);
3933 edges_node_deleted(oldn, current_ir_graph);
3935 /* We found an existing, better node, so we can deallocate the old node. */
3936 irg_kill_node(current_ir_graph, oldn);
3940 /* Some more constant expression evaluation that does not allow to
3942 iro = get_irn_opcode(n);
3943 if (get_opt_constant_folding() ||
3944 (iro == iro_Cond) ||
3945 (iro == iro_Proj)) /* Flags tested local. */
3946 n = transform_node(n);
3948 /* Remove nodes with dead (Bad) input.
3949 Run always for transformation induced Bads. */
3952 /* Now we have a legal, useful node. Enter it in hash table for CSE */
3953 if (get_opt_cse() && (get_irn_opcode(n) != iro_Block)) {
3954 n = identify_remember(current_ir_graph->value_table, n);
3958 } /* optimize_node */
3962 * These optimizations never deallocate nodes (in place). This can cause dead
3963 * nodes lying on the obstack. Remove these by a dead node elimination,
3964 * i.e., a copying garbage collection.
3966 ir_node *optimize_in_place_2(ir_node *n)
3970 opcode iro = get_irn_opcode(n);
3972 if (!get_opt_optimize() && (get_irn_op(n) != op_Phi)) return n;
3974 /* constant expression evaluation / constant folding */
3975 if (get_opt_constant_folding()) {
3976 /* neither constants nor Tuple values can be evaluated */
3977 if (iro != iro_Const && get_irn_mode(n) != mode_T) {
3978 /* try to evaluate */
3979 tv = computed_value(n);
3980 if (tv != tarval_bad) {
3981 /* evaluation was successful -- replace the node. */
3982 ir_type *old_tp = get_irn_type(n);
3983 int i, arity = get_irn_arity(n);
3986 * Try to recover the type of the new expression.
3988 for (i = 0; i < arity && !old_tp; ++i)
3989 old_tp = get_irn_type(get_irn_n(n, i));
3991 n = new_Const(get_tarval_mode(tv), tv);
3993 if (old_tp && get_type_mode(old_tp) == get_tarval_mode(tv))
3994 set_Const_type(n, old_tp);
3996 DBG_OPT_CSTEVAL(oldn, n);
4002 /* remove unnecessary nodes */
4003 if (get_opt_constant_folding() ||
4004 (iro == iro_Phi) || /* always optimize these nodes. */
4005 (iro == iro_Id) || /* ... */
4006 (iro == iro_Proj) || /* ... */
4007 (iro == iro_Block) ) /* Flags tested local. */
4008 n = equivalent_node(n);
4010 optimize_preds(n); /* do node specific optimizations of nodes predecessors. */
4012 /** common subexpression elimination **/
4013 /* Checks whether n is already available. */
4014 /* The block input is used to distinguish different subexpressions. Right
4015 now all nodes are op_pin_state_pinned to blocks, i.e., the cse only finds common
4016 subexpressions within a block. */
4017 if (get_opt_cse()) {
4018 n = identify(current_ir_graph->value_table, n);
4021 /* Some more constant expression evaluation. */
4022 iro = get_irn_opcode(n);
4023 if (get_opt_constant_folding() ||
4024 (iro == iro_Cond) ||
4025 (iro == iro_Proj)) /* Flags tested local. */
4026 n = transform_node(n);
4028 /* Remove nodes with dead (Bad) input.
4029 Run always for transformation induced Bads. */
4032 /* Now we can verify the node, as it has no dead inputs any more. */
4035 /* Now we have a legal, useful node. Enter it in hash table for cse.
4036 Blocks should be unique anyways. (Except the successor of start:
4037 is cse with the start block!) */
4038 if (get_opt_cse() && (get_irn_opcode(n) != iro_Block))
4039 n = identify_remember(current_ir_graph->value_table, n);
4042 } /* optimize_in_place_2 */
4045 * Wrapper for external use, set proper status bits after optimization.
4047 ir_node *optimize_in_place(ir_node *n)
4049 /* Handle graph state */
4050 assert(get_irg_phase_state(current_ir_graph) != phase_building);
4052 if (get_opt_global_cse())
4053 set_irg_pinned(current_ir_graph, op_pin_state_floats);
4054 if (get_irg_outs_state(current_ir_graph) == outs_consistent)
4055 set_irg_outs_inconsistent(current_ir_graph);
4057 /* FIXME: Maybe we could also test whether optimizing the node can
4058 change the control graph. */
4059 set_irg_doms_inconsistent(current_ir_graph);
4060 return optimize_in_place_2 (n);
4061 } /* optimize_in_place */
4064 * Sets the default operation for an ir_ops.
4066 ir_op_ops *firm_set_default_operations(opcode code, ir_op_ops *ops)
4068 ops = firm_set_default_computed_value(code, ops);
4069 ops = firm_set_default_equivalent_node(code, ops);
4070 ops = firm_set_default_transform_node(code, ops);
4071 ops = firm_set_default_node_cmp_attr(code, ops);
4072 ops = firm_set_default_get_type(code, ops);
4073 ops = firm_set_default_get_type_attr(code, ops);
4074 ops = firm_set_default_get_entity_attr(code, ops);
4077 } /* firm_set_default_operations */