3 * File name: ir/ir/iropt.c
4 * Purpose: iropt --- optimizations intertwined with IR construction.
5 * Author: Christian Schaefer
6 * Modified by: Goetz Lindenmaier, Michael Beck
9 * Copyright: (c) 1998-2006 Universität Karlsruhe
10 * Licence: This file protected by GPL - GNU GENERAL PUBLIC LICENSE.
28 #include "irgraph_t.h"
29 #include "iredges_t.h"
36 #include "dbginfo_t.h"
37 #include "iropt_dbg.h"
43 #include "opt_polymorphy.h"
44 #include "opt_confirms.h"
47 /* Make types visible to allow most efficient access */
51 * Return the value of a Constant.
53 static tarval *computed_value_Const(ir_node *n) {
54 return get_Const_tarval(n);
55 } /* computed_value_Const */
58 * Return the value of a 'sizeof' or 'alignof' SymConst.
60 static tarval *computed_value_SymConst(ir_node *n) {
64 switch (get_SymConst_kind(n)) {
65 case symconst_type_size:
66 type = get_SymConst_type(n);
67 if (get_type_state(type) == layout_fixed)
68 return new_tarval_from_long(get_type_size_bytes(type), get_irn_mode(n));
70 case symconst_type_align:
71 type = get_SymConst_type(n);
72 if (get_type_state(type) == layout_fixed)
73 return new_tarval_from_long(get_type_alignment_bytes(type), get_irn_mode(n));
75 case symconst_ofs_ent:
76 ent = get_SymConst_entity(n);
77 type = get_entity_owner(ent);
78 if (get_type_state(type) == layout_fixed)
79 return new_tarval_from_long(get_entity_offset(ent), get_irn_mode(n));
85 } /* computed_value_SymConst */
88 * Return the value of an Add.
90 static tarval *computed_value_Add(ir_node *n) {
91 ir_node *a = get_Add_left(n);
92 ir_node *b = get_Add_right(n);
94 tarval *ta = value_of(a);
95 tarval *tb = value_of(b);
97 if ((ta != tarval_bad) && (tb != tarval_bad) && (get_irn_mode(a) == get_irn_mode(b)))
98 return tarval_add(ta, tb);
101 } /* computed_value_Add */
104 * Return the value of a Sub.
105 * Special case: a - a
107 static tarval *computed_value_Sub(ir_node *n) {
108 ir_node *a = get_Sub_left(n);
109 ir_node *b = get_Sub_right(n);
114 if (a == b && !is_Bad(a))
115 return get_mode_null(get_irn_mode(n));
120 if ((ta != tarval_bad) && (tb != tarval_bad) && (get_irn_mode(a) == get_irn_mode(b)))
121 return tarval_sub(ta, tb);
124 } /* computed_value_Sub */
127 * Return the value of a Carry.
128 * Special : a op 0, 0 op b
130 static tarval *computed_value_Carry(ir_node *n) {
131 ir_node *a = get_binop_left(n);
132 ir_node *b = get_binop_right(n);
133 ir_mode *m = get_irn_mode(n);
135 tarval *ta = value_of(a);
136 tarval *tb = value_of(b);
138 if ((ta != tarval_bad) && (tb != tarval_bad)) {
140 return tarval_carry() ? get_mode_one(m) : get_mode_null(m);
142 if ( (classify_tarval(ta) == TV_CLASSIFY_NULL)
143 || (classify_tarval(tb) == TV_CLASSIFY_NULL))
144 return get_mode_null(m);
147 } /* computed_value_Carry */
150 * Return the value of a Borrow.
153 static tarval *computed_value_Borrow(ir_node *n) {
154 ir_node *a = get_binop_left(n);
155 ir_node *b = get_binop_right(n);
156 ir_mode *m = get_irn_mode(n);
158 tarval *ta = value_of(a);
159 tarval *tb = value_of(b);
161 if ((ta != tarval_bad) && (tb != tarval_bad)) {
162 return tarval_cmp(ta, tb) == pn_Cmp_Lt ? get_mode_one(m) : get_mode_null(m);
163 } else if (classify_tarval(ta) == TV_CLASSIFY_NULL) {
164 return get_mode_null(m);
167 } /* computed_value_Borrow */
170 * Return the value of an unary Minus.
172 static tarval *computed_value_Minus(ir_node *n) {
173 ir_node *a = get_Minus_op(n);
174 tarval *ta = value_of(a);
176 if ((ta != tarval_bad) && mode_is_signed(get_irn_mode(a)))
177 return tarval_neg(ta);
180 } /* computed_value_Minus */
183 * Return the value of a Mul.
185 static tarval *computed_value_Mul(ir_node *n) {
186 ir_node *a = get_Mul_left(n);
187 ir_node *b = get_Mul_right(n);
189 tarval *ta = value_of(a);
190 tarval *tb = value_of(b);
192 if ((ta != tarval_bad) && (tb != tarval_bad) && (get_irn_mode(a) == get_irn_mode(b))) {
193 return tarval_mul(ta, tb);
195 /* a*0 = 0 or 0*b = 0:
196 calls computed_value recursive and returns the 0 with proper
198 if ((ta != tarval_bad) && (ta == get_mode_null(get_tarval_mode(ta))))
200 if ((tb != tarval_bad) && (tb == get_mode_null(get_tarval_mode(tb))))
204 } /* computed_value_Mul */
207 * Return the value of a floating point Quot.
209 static tarval *computed_value_Quot(ir_node *n) {
210 ir_node *a = get_Quot_left(n);
211 ir_node *b = get_Quot_right(n);
213 tarval *ta = value_of(a);
214 tarval *tb = value_of(b);
216 /* This was missing in original implementation. Why? */
217 if ((ta != tarval_bad) && (tb != tarval_bad) && (get_irn_mode(a) == get_irn_mode(b))) {
218 if (tb != get_mode_null(get_tarval_mode(tb))) /* div by zero: return tarval_bad */
219 return tarval_quo(ta, tb);
222 } /* computed_value_Quot */
225 * Calculate the value of an integer Div of two nodes.
226 * Special case: 0 / b
228 static tarval *do_computed_value_Div(ir_node *a, ir_node *b) {
229 tarval *ta = value_of(a);
230 tarval *tb = value_of(b);
232 /* Compute c1 / c2 or 0 / a, a != 0 */
233 if (ta != tarval_bad) {
234 if ((tb != tarval_bad) && (tb != get_mode_null(get_irn_mode(b)))) /* div by zero: return tarval_bad */
235 return tarval_div(ta, tb);
236 else if (ta == get_mode_null(get_tarval_mode(ta))) /* 0 / b == 0 */
240 } /* do_computed_value_Div */
243 * Return the value of an integer Div.
245 static tarval *computed_value_Div(ir_node *n) {
246 return do_computed_value_Div(get_Div_left(n), get_Div_right(n));
247 } /* computed_value_Div */
250 * Calculate the value of an integer Mod of two nodes.
251 * Special case: a % 1
253 static tarval *do_computed_value_Mod(ir_node *a, ir_node *b) {
254 tarval *ta = value_of(a);
255 tarval *tb = value_of(b);
257 /* Compute c1 % c2 or a % 1 */
258 if (tb != tarval_bad) {
259 if ((ta != tarval_bad) && (tb != get_mode_null(get_tarval_mode(tb)))) /* div by zero: return tarval_bad */
260 return tarval_mod(ta, tb);
261 else if (tb == get_mode_one(get_tarval_mode(tb))) /* x mod 1 == 0 */
262 return get_mode_null(get_irn_mode(a));
265 } /* do_computed_value_Mod */
268 * Return the value of an integer Mod.
270 static tarval *computed_value_Mod(ir_node *n) {
271 return do_computed_value_Mod(get_Mod_left(n), get_Mod_right(n));
272 } /* computed_value_Mod */
275 * Return the value of an Abs.
277 static tarval *computed_value_Abs(ir_node *n) {
278 ir_node *a = get_Abs_op(n);
279 tarval *ta = value_of(a);
281 if (ta != tarval_bad)
282 return tarval_abs(ta);
285 } /* computed_value_Abs */
288 * Return the value of an And.
289 * Special case: a & 0, 0 & b
291 static tarval *computed_value_And(ir_node *n) {
292 ir_node *a = get_And_left(n);
293 ir_node *b = get_And_right(n);
295 tarval *ta = value_of(a);
296 tarval *tb = value_of(b);
298 if ((ta != tarval_bad) && (tb != tarval_bad)) {
299 return tarval_and (ta, tb);
303 if ( (classify_tarval ((v = ta)) == TV_CLASSIFY_NULL)
304 || (classify_tarval ((v = tb)) == TV_CLASSIFY_NULL)) {
309 } /* computed_value_And */
312 * Return the value of an Or.
313 * Special case: a | 1...1, 1...1 | b
315 static tarval *computed_value_Or(ir_node *n) {
316 ir_node *a = get_Or_left(n);
317 ir_node *b = get_Or_right(n);
319 tarval *ta = value_of(a);
320 tarval *tb = value_of(b);
322 if ((ta != tarval_bad) && (tb != tarval_bad)) {
323 return tarval_or (ta, tb);
326 if ( (classify_tarval ((v = ta)) == TV_CLASSIFY_ALL_ONE)
327 || (classify_tarval ((v = tb)) == TV_CLASSIFY_ALL_ONE)) {
332 } /* computed_value_Or */
335 * Return the value of an Eor.
337 static tarval *computed_value_Eor(ir_node *n) {
338 ir_node *a = get_Eor_left(n);
339 ir_node *b = get_Eor_right(n);
344 return get_mode_null(get_irn_mode(n));
349 if ((ta != tarval_bad) && (tb != tarval_bad)) {
350 return tarval_eor (ta, tb);
353 } /* computed_value_Eor */
356 * Return the value of a Not.
358 static tarval *computed_value_Not(ir_node *n) {
359 ir_node *a = get_Not_op(n);
360 tarval *ta = value_of(a);
362 if (ta != tarval_bad)
363 return tarval_not(ta);
366 } /* computed_value_Not */
369 * Return the value of a Shl.
371 static tarval *computed_value_Shl(ir_node *n) {
372 ir_node *a = get_Shl_left(n);
373 ir_node *b = get_Shl_right(n);
375 tarval *ta = value_of(a);
376 tarval *tb = value_of(b);
378 if ((ta != tarval_bad) && (tb != tarval_bad)) {
379 return tarval_shl (ta, tb);
382 } /* computed_value_Shl */
385 * Return the value of a Shr.
387 static tarval *computed_value_Shr(ir_node *n) {
388 ir_node *a = get_Shr_left(n);
389 ir_node *b = get_Shr_right(n);
391 tarval *ta = value_of(a);
392 tarval *tb = value_of(b);
394 if ((ta != tarval_bad) && (tb != tarval_bad)) {
395 return tarval_shr (ta, tb);
398 } /* computed_value_Shr */
401 * Return the value of a Shrs.
403 static tarval *computed_value_Shrs(ir_node *n) {
404 ir_node *a = get_Shrs_left(n);
405 ir_node *b = get_Shrs_right(n);
407 tarval *ta = value_of(a);
408 tarval *tb = value_of(b);
410 if ((ta != tarval_bad) && (tb != tarval_bad)) {
411 return tarval_shrs (ta, tb);
414 } /* computed_value_Shrs */
417 * Return the value of a Rot.
419 static tarval *computed_value_Rot(ir_node *n)
421 ir_node *a = get_Rot_left(n);
422 ir_node *b = get_Rot_right(n);
424 tarval *ta = value_of(a);
425 tarval *tb = value_of(b);
427 if ((ta != tarval_bad) && (tb != tarval_bad)) {
428 return tarval_rot (ta, tb);
431 } /* computed_value_Rot */
434 * Return the value of a Conv.
436 static tarval *computed_value_Conv(ir_node *n)
438 ir_node *a = get_Conv_op(n);
439 tarval *ta = value_of(a);
441 if (ta != tarval_bad)
442 return tarval_convert_to(ta, get_irn_mode(n));
445 } /* computed_value_Conv */
448 * Return the value of a Proj(Cmp).
450 * This performs a first step of unreachable code elimination.
451 * Proj can not be computed, but folding a Cmp above the Proj here is
452 * not as wasteful as folding a Cmp into a Tuple of 16 Consts of which
454 * There are several case where we can evaluate a Cmp node, see later.
456 static tarval *computed_value_Proj_Cmp(ir_node *n)
458 ir_node *a = get_Proj_pred(n);
459 ir_node *aa = get_Cmp_left(a);
460 ir_node *ab = get_Cmp_right(a);
461 long proj_nr = get_Proj_proj(n);
464 * BEWARE: a == a is NOT always True for floating Point values, as
465 * NaN != NaN is defined, so we must check this here.
468 !mode_is_float(get_irn_mode(aa)) || proj_nr == pn_Cmp_Lt || proj_nr == pn_Cmp_Gt)
471 /* This is a trick with the bits used for encoding the Cmp
472 Proj numbers, the following statement is not the same:
473 return new_tarval_from_long (proj_nr == pn_Cmp_Eq, mode_b) */
474 return new_tarval_from_long (proj_nr & pn_Cmp_Eq, mode_b);
477 tarval *taa = value_of(aa);
478 tarval *tab = value_of(ab);
479 ir_mode *mode = get_irn_mode(aa);
482 * The predecessors of Cmp are target values. We can evaluate
485 if ((taa != tarval_bad) && (tab != tarval_bad)) {
486 /* strange checks... */
487 pn_Cmp flags = tarval_cmp(taa, tab);
488 if (flags != pn_Cmp_False) {
489 return new_tarval_from_long (proj_nr & flags, mode_b);
492 /* for integer values, we can check against MIN/MAX */
493 else if (mode_is_int(mode)) {
494 /* MIN <=/> x. This results in true/false. */
495 if (taa == get_mode_min(mode)) {
496 /* a compare with the MIN value */
497 if (proj_nr == pn_Cmp_Le)
498 return get_tarval_b_true();
499 else if (proj_nr == pn_Cmp_Gt)
500 return get_tarval_b_false();
502 /* x >=/< MIN. This results in true/false. */
504 if (tab == get_mode_min(mode)) {
505 /* a compare with the MIN value */
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 /* MAX >=/< x. This results in true/false. */
512 else if (taa == get_mode_max(mode)) {
513 if (proj_nr == pn_Cmp_Ge)
514 return get_tarval_b_true();
515 else if (proj_nr == pn_Cmp_Lt)
516 return get_tarval_b_false();
518 /* x <=/> MAX. This results in true/false. */
519 else if (tab == get_mode_max(mode)) {
520 if (proj_nr == pn_Cmp_Le)
521 return get_tarval_b_true();
522 else if (proj_nr == pn_Cmp_Gt)
523 return get_tarval_b_false();
527 * The predecessors are Allocs or (void*)(0) constants. Allocs never
528 * return NULL, they raise an exception. Therefore we can predict
532 ir_node *aaa = skip_Id(skip_Proj(aa));
533 ir_node *aba = skip_Id(skip_Proj(ab));
535 if ( ( (/* aa is ProjP and aaa is Alloc */
536 (get_irn_op(aa) == op_Proj)
537 && (mode_is_reference(get_irn_mode(aa)))
538 && (get_irn_op(aaa) == op_Alloc))
539 && ( (/* ab is NULL */
540 (get_irn_op(ab) == op_Const)
541 && (mode_is_reference(get_irn_mode(ab)))
542 && (get_Const_tarval(ab) == get_mode_null(get_irn_mode(ab))))
543 || (/* ab is other Alloc */
544 (get_irn_op(ab) == op_Proj)
545 && (mode_is_reference(get_irn_mode(ab)))
546 && (get_irn_op(aba) == op_Alloc)
548 || (/* aa is NULL and aba is Alloc */
549 (get_irn_op(aa) == op_Const)
550 && (mode_is_reference(get_irn_mode(aa)))
551 && (get_Const_tarval(aa) == get_mode_null(get_irn_mode(aa)))
552 && (get_irn_op(ab) == op_Proj)
553 && (mode_is_reference(get_irn_mode(ab)))
554 && (get_irn_op(aba) == op_Alloc)))
556 return new_tarval_from_long(proj_nr & pn_Cmp_Ne, mode_b);
559 return computed_value_Cmp_Confirm(a, aa, ab, proj_nr);
560 } /* computed_value_Proj_Cmp */
563 * Return the value of a Proj, handle Proj(Cmp), Proj(Div), Proj(Mod),
564 * Proj(DivMod) and Proj(Quot).
566 static tarval *computed_value_Proj(ir_node *n) {
567 ir_node *a = get_Proj_pred(n);
570 switch (get_irn_opcode(a)) {
572 return computed_value_Proj_Cmp(n);
575 /* compute either the Div or the Mod part */
576 proj_nr = get_Proj_proj(n);
577 if (proj_nr == pn_DivMod_res_div)
578 return do_computed_value_Div(get_DivMod_left(a), get_DivMod_right(a));
579 else if (proj_nr == pn_DivMod_res_mod)
580 return do_computed_value_Mod(get_DivMod_left(a), get_DivMod_right(a));
584 if (get_Proj_proj(n) == pn_Div_res)
585 return computed_value(a);
589 if (get_Proj_proj(n) == pn_Mod_res)
590 return computed_value(a);
594 if (get_Proj_proj(n) == pn_Quot_res)
595 return computed_value(a);
602 } /* computed_value_Proj */
605 * Calculate the value of a Mux: can be evaluated, if the
606 * sel and the right input are known.
608 static tarval *computed_value_Mux(ir_node *n) {
609 ir_node *sel = get_Mux_sel(n);
610 tarval *ts = value_of(sel);
612 if (ts == get_tarval_b_true()) {
613 ir_node *v = get_Mux_true(n);
616 else if (ts == get_tarval_b_false()) {
617 ir_node *v = get_Mux_false(n);
621 } /* computed_value_Mux */
624 * Calculate the value of a Psi: can be evaluated, if a condition is true
625 * and all previous conditions are false. If all conditions are false
626 * we evaluate to the default one.
628 static tarval *computed_value_Psi(ir_node *n) {
630 return computed_value_Mux(n);
632 } /* computed_value_Psi */
635 * Calculate the value of a Confirm: can be evaluated,
636 * if it has the form Confirm(x, '=', Const).
638 static tarval *computed_value_Confirm(ir_node *n) {
639 return get_Confirm_cmp(n) == pn_Cmp_Eq ?
640 value_of(get_Confirm_bound(n)) : tarval_bad;
641 } /* computed_value_Confirm */
644 * If the parameter n can be computed, return its value, else tarval_bad.
645 * Performs constant folding.
647 * @param n The node this should be evaluated
649 tarval *computed_value(ir_node *n) {
650 if (n->op->ops.computed_value)
651 return n->op->ops.computed_value(n);
653 } /* computed_value */
656 * Set the default computed_value evaluator in an ir_op_ops.
658 * @param code the opcode for the default operation
659 * @param ops the operations initialized
664 static ir_op_ops *firm_set_default_computed_value(opcode code, ir_op_ops *ops)
668 ops->computed_value = computed_value_##a; \
703 } /* firm_set_default_computed_value */
706 * Returns a equivalent block for another block.
707 * If the block has only one predecessor, this is
708 * the equivalent one. If the only predecessor of a block is
709 * the block itself, this is a dead block.
711 * If both predecessors of a block are the branches of a binary
712 * Cond, the equivalent block is Cond's block.
714 * If all predecessors of a block are bad or lies in a dead
715 * block, the current block is dead as well.
717 * Note, that blocks are NEVER turned into Bad's, instead
718 * the dead_block flag is set. So, never test for is_Bad(block),
719 * always use is_dead_Block(block).
721 static ir_node *equivalent_node_Block(ir_node *n)
724 int n_preds = get_Block_n_cfgpreds(n);
726 /* The Block constructor does not call optimize, but mature_immBlock
727 calls the optimization. */
728 assert(get_Block_matured(n));
730 /* Straightening: a single entry Block following a single exit Block
731 can be merged, if it is not the Start block. */
732 /* !!! Beware, all Phi-nodes of n must have been optimized away.
733 This should be true, as the block is matured before optimize is called.
734 But what about Phi-cycles with the Phi0/Id that could not be resolved?
735 Remaining Phi nodes are just Ids. */
736 if ((n_preds == 1) && (get_irn_op(get_Block_cfgpred(n, 0)) == op_Jmp)) {
737 ir_node *predblock = get_nodes_block(get_Block_cfgpred(n, 0));
738 if (predblock == oldn) {
739 /* Jmp jumps into the block it is in -- deal self cycle. */
740 n = set_Block_dead(n);
741 DBG_OPT_DEAD_BLOCK(oldn, n);
742 } else if (get_opt_control_flow_straightening()) {
744 DBG_OPT_STG(oldn, n);
747 else if ((n_preds == 1) &&
748 (get_irn_op(skip_Proj(get_Block_cfgpred(n, 0))) == op_Cond)) {
749 ir_node *predblock = get_Block_cfgpred_block(n, 0);
750 if (predblock == oldn) {
751 /* Jmp jumps into the block it is in -- deal self cycle. */
752 n = set_Block_dead(n);
753 DBG_OPT_DEAD_BLOCK(oldn, n);
756 else if ((n_preds == 2) &&
757 (get_opt_control_flow_weak_simplification())) {
758 /* Test whether Cond jumps twice to this block
759 * The more general case which more than 2 predecessors is handles
760 * in optimize_cf(), we handle only this special case for speed here.
762 ir_node *a = get_Block_cfgpred(n, 0);
763 ir_node *b = get_Block_cfgpred(n, 1);
765 if ((get_irn_op(a) == op_Proj) &&
766 (get_irn_op(b) == op_Proj) &&
767 (get_Proj_pred(a) == get_Proj_pred(b)) &&
768 (get_irn_op(get_Proj_pred(a)) == op_Cond) &&
769 (get_irn_mode(get_Cond_selector(get_Proj_pred(a))) == mode_b)) {
770 /* Also a single entry Block following a single exit Block. Phis have
771 twice the same operand and will be optimized away. */
772 n = get_nodes_block(get_Proj_pred(a));
773 DBG_OPT_IFSIM1(oldn, a, b, n);
776 else if (get_opt_unreachable_code() &&
777 (n != get_irg_start_block(current_ir_graph)) &&
778 (n != get_irg_end_block(current_ir_graph)) ) {
781 /* If all inputs are dead, this block is dead too, except if it is
782 the start or end block. This is one step of unreachable code
784 for (i = get_Block_n_cfgpreds(n) - 1; i >= 0; --i) {
785 ir_node *pred = get_Block_cfgpred(n, i);
788 if (is_Bad(pred)) continue;
789 pred_blk = get_nodes_block(skip_Proj(pred));
791 if (is_Block_dead(pred_blk)) continue;
794 /* really found a living input */
799 n = set_Block_dead(n);
800 DBG_OPT_DEAD_BLOCK(oldn, n);
805 } /* equivalent_node_Block */
808 * Returns a equivalent node for a Jmp, a Bad :-)
809 * Of course this only happens if the Block of the Jmp is dead.
811 static ir_node *equivalent_node_Jmp(ir_node *n) {
812 /* unreachable code elimination */
813 if (is_Block_dead(get_nodes_block(n)))
817 } /* equivalent_node_Jmp */
819 /** Raise is handled in the same way as Jmp. */
820 #define equivalent_node_Raise equivalent_node_Jmp
823 /* We do not evaluate Cond here as we replace it by a new node, a Jmp.
824 See transform_node_Proj_Cond(). */
827 * Optimize operations that are commutative and have neutral 0,
828 * so a op 0 = 0 op a = a.
830 static ir_node *equivalent_node_neutral_zero(ir_node *n)
834 ir_node *a = get_binop_left(n);
835 ir_node *b = get_binop_right(n);
840 /* After running compute_node there is only one constant predecessor.
841 Find this predecessors value and remember the other node: */
842 if ((tv = value_of(a)) != tarval_bad) {
844 } else if ((tv = value_of(b)) != tarval_bad) {
849 /* If this predecessors constant value is zero, the operation is
850 * unnecessary. Remove it.
852 * Beware: If n is a Add, the mode of on and n might be different
853 * which happens in this rare construction: NULL + 3.
854 * Then, a Conv would be needed which we cannot include here.
856 if (classify_tarval (tv) == TV_CLASSIFY_NULL) {
857 if (get_irn_mode(on) == get_irn_mode(n)) {
860 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_0);
865 } /* equivalent_node_neutral_zero */
868 * Eor is commutative and has neutral 0.
870 #define equivalent_node_Eor equivalent_node_neutral_zero
873 * Optimize a - 0 and (a - x) + x (for modes with wrap-around).
875 * The second one looks strange, but this construct
876 * is used heavily in the LCC sources :-).
878 * Beware: The Mode of an Add may be different than the mode of its
879 * predecessors, so we could not return a predecessors in all cases.
881 static ir_node *equivalent_node_Add(ir_node *n)
884 ir_node *left, *right;
885 ir_mode *mode = get_irn_mode(n);
887 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
888 if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
891 n = equivalent_node_neutral_zero(n);
895 left = get_Add_left(n);
896 right = get_Add_right(n);
898 if (get_irn_op(left) == op_Sub) {
899 if (get_Sub_right(left) == right) {
902 n = get_Sub_left(left);
903 if (mode == get_irn_mode(n)) {
904 DBG_OPT_ALGSIM1(oldn, left, right, n, FS_OPT_ADD_SUB);
909 if (get_irn_op(right) == op_Sub) {
910 if (get_Sub_right(right) == left) {
913 n = get_Sub_left(right);
914 if (mode == get_irn_mode(n)) {
915 DBG_OPT_ALGSIM1(oldn, left, right, n, FS_OPT_ADD_SUB);
921 } /* equivalent_node_Add */
924 * optimize operations that are not commutative but have neutral 0 on left,
927 static ir_node *equivalent_node_left_zero(ir_node *n) {
930 ir_node *a = get_binop_left(n);
931 ir_node *b = get_binop_right(n);
933 if (classify_tarval(value_of(b)) == TV_CLASSIFY_NULL) {
936 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_0);
939 } /* equivalent_node_left_zero */
941 #define equivalent_node_Shl equivalent_node_left_zero
942 #define equivalent_node_Shr equivalent_node_left_zero
943 #define equivalent_node_Shrs equivalent_node_left_zero
944 #define equivalent_node_Rot equivalent_node_left_zero
947 * Optimize a - 0 and (a + x) - x (for modes with wrap-around).
949 * The second one looks strange, but this construct
950 * is used heavily in the LCC sources :-).
952 * Beware: The Mode of a Sub may be different than the mode of its
953 * predecessors, so we could not return a predecessors in all cases.
955 static ir_node *equivalent_node_Sub(ir_node *n)
959 ir_mode *mode = get_irn_mode(n);
961 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
962 if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
966 b = get_Sub_right(n);
968 /* Beware: modes might be different */
969 if (classify_tarval(value_of(b)) == TV_CLASSIFY_NULL) {
970 if (mode == get_irn_mode(a)) {
973 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_0);
976 else if (get_irn_op(a) == op_Add) {
977 if (mode_wrap_around(mode)) {
978 ir_node *left = get_Add_left(a);
979 ir_node *right = get_Add_right(a);
982 if (mode == get_irn_mode(right)) {
984 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
987 else if (right == b) {
988 if (mode == get_irn_mode(left)) {
990 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
996 } /* equivalent_node_Sub */
1000 * Optimize an "idempotent unary op", ie op(op(n)) = n.
1003 * -(-a) == a, but might overflow two times.
1004 * We handle it anyway here but the better way would be a
1005 * flag. This would be needed for Pascal for instance.
1007 static ir_node *equivalent_node_idempotent_unop(ir_node *n)
1010 ir_node *pred = get_unop_op(n);
1012 /* optimize symmetric unop */
1013 if (get_irn_op(pred) == get_irn_op(n)) {
1014 n = get_unop_op(pred);
1015 DBG_OPT_ALGSIM2(oldn, pred, n);
1018 } /* equivalent_node_idempotent_unop */
1020 /** Optimize Not(Not(x)) == x. */
1021 #define equivalent_node_Not equivalent_node_idempotent_unop
1023 /** --x == x ??? Is this possible or can --x raise an
1024 out of bounds exception if min =! max? */
1025 #define equivalent_node_Minus equivalent_node_idempotent_unop
1028 * Optimize a * 1 = 1 * a = a.
1030 static ir_node *equivalent_node_Mul(ir_node *n)
1033 ir_node *a = get_Mul_left(n);
1034 ir_node *b = get_Mul_right(n);
1036 /* Mul is commutative and has again an other neutral element. */
1037 if (classify_tarval(value_of(a)) == TV_CLASSIFY_ONE) {
1039 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
1040 } else if (classify_tarval(value_of(b)) == TV_CLASSIFY_ONE) {
1042 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
1045 } /* equivalent_node_Mul */
1048 * Optimize a / 1 = a.
1050 static ir_node *equivalent_node_Div(ir_node *n) {
1051 ir_node *a = get_Div_left(n);
1052 ir_node *b = get_Div_right(n);
1054 /* Div is not commutative. */
1055 if (classify_tarval(value_of(b)) == TV_CLASSIFY_ONE) { /* div(x, 1) == x */
1056 /* Turn Div into a tuple (mem, bad, a) */
1057 ir_node *mem = get_Div_mem(n);
1058 turn_into_tuple(n, pn_Div_max);
1059 set_Tuple_pred(n, pn_Div_M, mem);
1060 set_Tuple_pred(n, pn_Div_X_except, new_Bad()); /* no exception */
1061 set_Tuple_pred(n, pn_Div_res, a);
1064 } /* equivalent_node_Div */
1067 * Optimize a / 1.0 = a.
1069 static ir_node *equivalent_node_Quot(ir_node *n) {
1070 ir_node *a = get_Quot_left(n);
1071 ir_node *b = get_Quot_right(n);
1073 /* Div is not commutative. */
1074 if (classify_tarval(value_of(b)) == TV_CLASSIFY_ONE) { /* Quot(x, 1) == x */
1075 /* Turn Quot into a tuple (mem, bad, a) */
1076 ir_node *mem = get_Quot_mem(n);
1077 turn_into_tuple(n, pn_Quot_max);
1078 set_Tuple_pred(n, pn_Quot_M, mem);
1079 set_Tuple_pred(n, pn_Quot_X_except, new_Bad()); /* no exception */
1080 set_Tuple_pred(n, pn_Quot_res, a);
1083 } /* equivalent_node_Quot */
1086 * Optimize a / 1 = a.
1088 static ir_node *equivalent_node_DivMod(ir_node *n) {
1089 ir_node *a = get_DivMod_left(n);
1090 ir_node *b = get_DivMod_right(n);
1092 /* Div is not commutative. */
1093 if (classify_tarval(value_of(b)) == TV_CLASSIFY_ONE) { /* div(x, 1) == x */
1094 /* Turn DivMod into a tuple (mem, bad, a, 0) */
1095 ir_node *mem = get_Div_mem(n);
1096 ir_mode *mode = get_irn_mode(b);
1098 turn_into_tuple(n, pn_DivMod_max);
1099 set_Tuple_pred(n, pn_DivMod_M, mem);
1100 set_Tuple_pred(n, pn_DivMod_X_except, new_Bad()); /* no exception */
1101 set_Tuple_pred(n, pn_DivMod_res_div, a);
1102 set_Tuple_pred(n, pn_DivMod_res_mod, new_Const(mode, get_mode_null(mode)));
1105 } /* equivalent_node_DivMod */
1108 * Use algebraic simplification a | a = a | 0 = 0 | a = a.
1110 static ir_node *equivalent_node_Or(ir_node *n) {
1113 ir_node *a = get_Or_left(n);
1114 ir_node *b = get_Or_right(n);
1117 n = a; /* Or has it's own neutral element */
1118 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_OR);
1119 } else if (classify_tarval(value_of(a)) == TV_CLASSIFY_NULL) {
1121 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_OR);
1122 } else if (classify_tarval(value_of(b)) == TV_CLASSIFY_NULL) {
1124 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_OR);
1128 } /* equivalent_node_Or */
1131 * Optimize a & 0b1...1 = 0b1...1 & a = a & a = a.
1133 static ir_node *equivalent_node_And(ir_node *n) {
1136 ir_node *a = get_And_left(n);
1137 ir_node *b = get_And_right(n);
1140 n = a; /* And has it's own neutral element */
1141 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_AND);
1142 } else if (classify_tarval(value_of(a)) == TV_CLASSIFY_ALL_ONE) {
1144 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1145 } else if (classify_tarval(value_of(b)) == TV_CLASSIFY_ALL_ONE) {
1147 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1150 } /* equivalent_node_And */
1153 * Try to remove useless Conv's:
1155 static ir_node *equivalent_node_Conv(ir_node *n) {
1157 ir_node *a = get_Conv_op(n);
1160 ir_mode *n_mode = get_irn_mode(n);
1161 ir_mode *a_mode = get_irn_mode(a);
1163 if (n_mode == a_mode) { /* No Conv necessary */
1164 /* leave strict floating point Conv's */
1165 if (get_Conv_strict(n))
1168 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CONV);
1169 } else if (get_irn_op(a) == op_Conv) { /* Conv(Conv(b)) */
1173 n_mode = get_irn_mode(n);
1174 b_mode = get_irn_mode(b);
1176 if (n_mode == b_mode) {
1177 if (n_mode == mode_b) {
1178 n = b; /* Convb(Conv*(xxxb(...))) == xxxb(...) */
1179 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
1181 else if (mode_is_int(n_mode) || mode_is_character(n_mode)) {
1182 if (smaller_mode(b_mode, a_mode)){
1183 n = b; /* ConvS(ConvL(xxxS(...))) == xxxS(...) */
1184 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
1190 } /* equivalent_node_Conv */
1193 * A Cast may be removed if the type of the previous node
1194 * is already the type of the Cast.
1196 static ir_node *equivalent_node_Cast(ir_node *n) {
1198 ir_node *pred = get_Cast_op(n);
1200 if (get_irn_type(pred) == get_Cast_type(n)) {
1202 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CAST);
1205 } /* equivalent_node_Cast */
1208 * Several optimizations:
1209 * - no Phi in start block.
1210 * - remove Id operators that are inputs to Phi
1211 * - fold Phi-nodes, iff they have only one predecessor except
1214 static ir_node *equivalent_node_Phi(ir_node *n)
1219 ir_node *block = NULL; /* to shutup gcc */
1220 ir_node *first_val = NULL; /* to shutup gcc */
1222 if (!get_opt_normalize()) return n;
1224 n_preds = get_Phi_n_preds(n);
1226 block = get_nodes_block(n);
1227 /* @@@ fliegt 'raus, sollte aber doch immer wahr sein!!!
1228 assert(get_irn_arity(block) == n_preds && "phi in wrong block!"); */
1229 if ((is_Block_dead(block)) || /* Control dead */
1230 (block == get_irg_start_block(current_ir_graph))) /* There should be no Phi nodes */
1231 return new_Bad(); /* in the Start Block. */
1233 if (n_preds == 0) return n; /* Phi of dead Region without predecessors. */
1235 /* If the Block has a Bad pred, we also have one. */
1236 for (i = 0; i < n_preds; ++i)
1237 if (is_Bad(get_Block_cfgpred(block, i)))
1238 set_Phi_pred(n, i, new_Bad());
1240 /* Find first non-self-referencing input */
1241 for (i = 0; i < n_preds; ++i) {
1242 first_val = get_Phi_pred(n, i);
1243 if ( (first_val != n) /* not self pointer */
1245 && (! is_Bad(first_val))
1247 ) { /* value not dead */
1248 break; /* then found first value. */
1253 /* A totally Bad or self-referencing Phi (we didn't break the above loop) */
1257 /* search for rest of inputs, determine if any of these
1258 are non-self-referencing */
1259 while (++i < n_preds) {
1260 ir_node *scnd_val = get_Phi_pred(n, i);
1261 if ( (scnd_val != n)
1262 && (scnd_val != first_val)
1264 && (! is_Bad(scnd_val))
1272 /* Fold, if no multiple distinct non-self-referencing inputs */
1274 DBG_OPT_PHI(oldn, n);
1277 } /* equivalent_node_Phi */
1280 * Several optimizations:
1281 * - no Sync in start block.
1282 * - fold Sync-nodes, iff they have only one predecessor except
1285 static ir_node *equivalent_node_Sync(ir_node *n)
1290 ir_node *first_val = NULL; /* to shutup gcc */
1292 if (!get_opt_normalize()) return n;
1294 n_preds = get_Sync_n_preds(n);
1296 /* Find first non-self-referencing input */
1297 for (i = 0; i < n_preds; ++i) {
1298 first_val = get_Sync_pred(n, i);
1299 if ((first_val != n) /* not self pointer */ &&
1300 (! is_Bad(first_val))
1301 ) { /* value not dead */
1302 break; /* then found first value. */
1307 /* A totally Bad or self-referencing Sync (we didn't break the above loop) */
1310 /* search the rest of inputs, determine if any of these
1311 are non-self-referencing */
1312 while (++i < n_preds) {
1313 ir_node *scnd_val = get_Sync_pred(n, i);
1314 if ((scnd_val != n) &&
1315 (scnd_val != first_val) &&
1316 (! is_Bad(scnd_val))
1322 /* Fold, if no multiple distinct non-self-referencing inputs */
1324 DBG_OPT_SYNC(oldn, n);
1327 } /* equivalent_node_Sync */
1330 * Optimize Proj(Tuple) and gigo() for ProjX in Bad block,
1331 * ProjX(Load) and ProjX(Store).
1333 static ir_node *equivalent_node_Proj(ir_node *n)
1337 ir_node *a = get_Proj_pred(n);
1339 if ( get_irn_op(a) == op_Tuple) {
1340 /* Remove the Tuple/Proj combination. */
1341 if ( get_Proj_proj(n) <= get_Tuple_n_preds(a) ) {
1342 n = get_Tuple_pred(a, get_Proj_proj(n));
1343 DBG_OPT_TUPLE(oldn, a, n);
1345 assert(0); /* This should not happen! */
1349 else if (get_irn_mode(n) == mode_X) {
1350 if (is_Block_dead(get_nodes_block(skip_Proj(n)))) {
1351 /* Remove dead control flow -- early gigo(). */
1354 else if (get_opt_ldst_only_null_ptr_exceptions()) {
1355 ir_op *op = get_irn_op(a);
1357 if (op == op_Load || op == op_Store) {
1358 /* get the load/store address */
1359 ir_node *addr = get_irn_n(a, 1);
1362 if (value_not_null(addr, &confirm)) {
1363 if (confirm == NULL) {
1364 /* this node may float if it did not depend on a Confirm */
1365 set_irn_pinned(a, op_pin_state_floats);
1375 } /* equivalent_node_Proj */
1380 static ir_node *equivalent_node_Id(ir_node *n) {
1385 } while (get_irn_op(n) == op_Id);
1387 DBG_OPT_ID(oldn, n);
1389 } /* equivalent_node_Id */
1394 static ir_node *equivalent_node_Mux(ir_node *n)
1396 ir_node *oldn = n, *sel = get_Mux_sel(n);
1397 tarval *ts = value_of(sel);
1399 /* Mux(true, f, t) == t */
1400 if (ts == tarval_b_true) {
1401 n = get_Mux_true(n);
1402 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_C);
1404 /* Mux(false, f, t) == f */
1405 else if (ts == tarval_b_false) {
1406 n = get_Mux_false(n);
1407 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_C);
1409 /* Mux(v, x, x) == x */
1410 else if (get_Mux_false(n) == get_Mux_true(n)) {
1411 n = get_Mux_true(n);
1412 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_EQ);
1414 else if (get_irn_op(sel) == op_Proj && !mode_honor_signed_zeros(get_irn_mode(n))) {
1415 ir_node *cmp = get_Proj_pred(sel);
1416 long proj_nr = get_Proj_proj(sel);
1417 ir_node *b = get_Mux_false(n);
1418 ir_node *a = get_Mux_true(n);
1421 * Note: normalization puts the constant on the right site,
1422 * so we check only one case.
1424 * Note further that these optimization work even for floating point
1425 * with NaN's because -NaN == NaN.
1426 * However, if +0 and -0 is handled differently, we cannot use the first one.
1428 if (get_irn_op(cmp) == op_Cmp && get_Cmp_left(cmp) == a) {
1429 if (classify_Const(get_Cmp_right(cmp)) == CNST_NULL) {
1430 /* Mux(a CMP 0, X, a) */
1431 if (get_irn_op(b) == op_Minus && get_Minus_op(b) == a) {
1432 /* Mux(a CMP 0, -a, a) */
1433 if (proj_nr == pn_Cmp_Eq) {
1434 /* Mux(a == 0, -a, a) ==> -a */
1436 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1438 else if (proj_nr == pn_Cmp_Lg || proj_nr == pn_Cmp_Ne) {
1439 /* Mux(a != 0, -a, a) ==> a */
1441 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1444 else if (classify_Const(b) == CNST_NULL) {
1445 /* Mux(a CMP 0, 0, a) */
1446 if (proj_nr == pn_Cmp_Lg || proj_nr == pn_Cmp_Ne) {
1447 /* Mux(a != 0, 0, a) ==> a */
1449 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1451 else if (proj_nr == pn_Cmp_Eq) {
1452 /* Mux(a == 0, 0, a) ==> 0 */
1454 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1461 } /* equivalent_node_Mux */
1464 * Returns a equivalent node of a Psi: if a condition is true
1465 * and all previous conditions are false we know its value.
1466 * If all conditions are false its value is the default one.
1468 static ir_node *equivalent_node_Psi(ir_node *n) {
1470 return equivalent_node_Mux(n);
1472 } /* equivalent_node_Psi */
1475 * Optimize -a CMP -b into b CMP a.
1476 * This works only for for modes where unary Minus
1478 * Note that two-complement integers can Overflow
1479 * so it will NOT work.
1481 static ir_node *equivalent_node_Cmp(ir_node *n)
1483 ir_node *left = get_Cmp_left(n);
1484 ir_node *right = get_Cmp_right(n);
1486 if (get_irn_op(left) == op_Minus && get_irn_op(right) == op_Minus &&
1487 !mode_overflow_on_unary_Minus(get_irn_mode(left))) {
1488 left = get_Minus_op(left);
1489 right = get_Minus_op(right);
1490 set_Cmp_left(n, right);
1491 set_Cmp_right(n, left);
1494 } /* equivalent_node_Cmp */
1497 * Remove Confirm nodes if setting is on.
1498 * Replace Confirms(x, '=', Constlike) by Constlike.
1500 static ir_node *equivalent_node_Confirm(ir_node *n) {
1501 ir_node *pred = get_Confirm_value(n);
1502 pn_Cmp pnc = get_Confirm_cmp(n);
1504 if (get_irn_op(pred) == op_Confirm && pnc == get_Confirm_cmp(pred)) {
1506 * rare case: two identical Confirms one after another,
1507 * replace the second one with the first.
1511 if (pnc == pn_Cmp_Eq) {
1512 ir_node *bound = get_Confirm_bound(n);
1515 * Optimize a rare case:
1516 * Confirm(x, '=', Constlike) ==> Constlike
1518 if (is_irn_constlike(bound)) {
1519 DBG_OPT_CONFIRM(n, bound);
1523 return get_opt_remove_confirm() ? get_Confirm_value(n) : n;
1527 * Optimize CopyB(mem, x, x) into a Nop.
1529 static ir_node *equivalent_node_CopyB(ir_node *n) {
1530 ir_node *a = get_CopyB_dst(n);
1531 ir_node *b = get_CopyB_src(n);
1534 /* Turn CopyB into a tuple (mem, bad, bad) */
1535 ir_node *mem = get_CopyB_mem(n);
1536 turn_into_tuple(n, pn_CopyB_max);
1537 set_Tuple_pred(n, pn_CopyB_M, mem);
1538 set_Tuple_pred(n, pn_CopyB_X_except, new_Bad()); /* no exception */
1539 set_Tuple_pred(n, pn_CopyB_M_except, new_Bad());
1542 } /* equivalent_node_CopyB */
1545 * Optimize Bounds(idx, idx, upper) into idx.
1547 static ir_node *equivalent_node_Bound(ir_node *n) {
1548 ir_node *idx = get_Bound_index(n);
1549 ir_node *lower = get_Bound_lower(n);
1552 /* By definition lower < upper, so if idx == lower -->
1553 lower <= idx && idx < upper */
1555 /* Turn Bound into a tuple (mem, bad, idx) */
1559 ir_node *pred = skip_Proj(idx);
1561 if (get_irn_op(pred) == op_Bound) {
1563 * idx was Bounds_check previously, it is still valid if
1564 * lower <= pred_lower && pred_upper <= upper.
1566 ir_node *upper = get_Bound_upper(n);
1567 if (get_Bound_lower(pred) == lower &&
1568 get_Bound_upper(pred) == upper) {
1570 * One could expect that we simply return the previous
1571 * Bound here. However, this would be wrong, as we could
1572 * add an exception Proj to a new location than.
1573 * So, we must turn in into a tuple
1580 /* Turn Bound into a tuple (mem, bad, idx) */
1581 ir_node *mem = get_Bound_mem(n);
1582 turn_into_tuple(n, pn_Bound_max);
1583 set_Tuple_pred(n, pn_Bound_M, mem);
1584 set_Tuple_pred(n, pn_Bound_X_except, new_Bad()); /* no exception */
1585 set_Tuple_pred(n, pn_Bound_res, idx);
1588 } /* equivalent_node_Bound */
1591 * equivalent_node() returns a node equivalent to input n. It skips all nodes that
1592 * perform no actual computation, as, e.g., the Id nodes. It does not create
1593 * new nodes. It is therefore safe to free n if the node returned is not n.
1594 * If a node returns a Tuple we can not just skip it. If the size of the
1595 * in array fits, we transform n into a tuple (e.g., Div).
1597 ir_node *equivalent_node(ir_node *n) {
1598 if (n->op->ops.equivalent_node)
1599 return n->op->ops.equivalent_node(n);
1601 } /* equivalent_node */
1604 * Sets the default equivalent node operation for an ir_op_ops.
1606 * @param code the opcode for the default operation
1607 * @param ops the operations initialized
1612 static ir_op_ops *firm_set_default_equivalent_node(opcode code, ir_op_ops *ops)
1616 ops->equivalent_node = equivalent_node_##a; \
1656 } /* firm_set_default_equivalent_node */
1659 * Do node specific optimizations of nodes predecessors.
1661 static void optimize_preds(ir_node *n) {
1662 ir_node *a = NULL, *b = NULL;
1664 /* get the operands we will work on for simple cases. */
1666 a = get_binop_left(n);
1667 b = get_binop_right(n);
1668 } else if (is_unop(n)) {
1672 switch (get_irn_opcode(n)) {
1675 /* We don't want Cast as input to Cmp. */
1676 if (get_irn_op(a) == op_Cast) {
1680 if (get_irn_op(b) == op_Cast) {
1682 set_Cmp_right(n, b);
1688 } /* optimize_preds */
1691 * Returns non-zero if a node is a Phi node
1692 * with all predecessors constant.
1694 static int is_const_Phi(ir_node *n) {
1699 for (i = get_irn_arity(n) - 1; i >= 0; --i)
1700 if (! is_Const(get_irn_n(n, i)))
1703 } /* is_const_Phi */
1706 * Apply an evaluator on a binop with a constant operators (and one Phi).
1708 * @param phi the Phi node
1709 * @param other the other operand
1710 * @param eval an evaluator function
1711 * @param left if non-zero, other is the left operand, else the right
1713 * @return a new Phi node if the conversion was successful, NULL else
1715 static ir_node *apply_binop_on_phi(ir_node *phi, tarval *other, tarval *(*eval)(tarval *, tarval *), int left) {
1721 int i, n = get_irn_arity(phi);
1723 NEW_ARR_A(void *, res, n);
1725 for (i = 0; i < n; ++i) {
1726 pred = get_irn_n(phi, i);
1727 tv = get_Const_tarval(pred);
1728 tv = eval(other, tv);
1730 if (tv == tarval_bad) {
1731 /* folding failed, bad */
1738 for (i = 0; i < n; ++i) {
1739 pred = get_irn_n(phi, i);
1740 tv = get_Const_tarval(pred);
1741 tv = eval(tv, other);
1743 if (tv == tarval_bad) {
1744 /* folding failed, bad */
1750 mode = get_irn_mode(phi);
1751 irg = current_ir_graph;
1752 for (i = 0; i < n; ++i) {
1753 pred = get_irn_n(phi, i);
1754 res[i] = new_r_Const_type(irg, get_irg_start_block(irg),
1755 mode, res[i], get_Const_type(pred));
1757 return new_r_Phi(irg, get_nodes_block(phi), n, (ir_node **)res, mode);
1758 } /* apply_binop_on_phi */
1761 * Apply an evaluator on a unop with a constant operator (a Phi).
1763 * @param phi the Phi node
1764 * @param eval an evaluator function
1766 * @return a new Phi node if the conversion was successful, NULL else
1768 static ir_node *apply_unop_on_phi(ir_node *phi, tarval *(*eval)(tarval *)) {
1774 int i, n = get_irn_arity(phi);
1776 NEW_ARR_A(void *, res, n);
1777 for (i = 0; i < n; ++i) {
1778 pred = get_irn_n(phi, i);
1779 tv = get_Const_tarval(pred);
1782 if (tv == tarval_bad) {
1783 /* folding failed, bad */
1788 mode = get_irn_mode(phi);
1789 irg = current_ir_graph;
1790 for (i = 0; i < n; ++i) {
1791 pred = get_irn_n(phi, i);
1792 res[i] = new_r_Const_type(irg, get_irg_start_block(irg),
1793 mode, res[i], get_Const_type(pred));
1795 return new_r_Phi(irg, get_nodes_block(phi), n, (ir_node **)res, mode);
1796 } /* apply_unop_on_phi */
1799 * Transform AddP(P, ConvIs(Iu)), AddP(P, ConvIu(Is)) and
1800 * SubP(P, ConvIs(Iu)), SubP(P, ConvIu(Is)).
1801 * If possible, remove the Conv's.
1803 static ir_node *transform_node_AddSub(ir_node *n)
1805 ir_mode *mode = get_irn_mode(n);
1807 if (mode_is_reference(mode)) {
1808 ir_node *left = get_binop_left(n);
1809 ir_node *right = get_binop_right(n);
1810 int ref_bits = get_mode_size_bits(mode);
1812 if (get_irn_op(left) == op_Conv) {
1813 ir_mode *mode = get_irn_mode(left);
1814 int bits = get_mode_size_bits(mode);
1816 if (ref_bits == bits &&
1817 mode_is_int(mode) &&
1818 get_mode_arithmetic(mode) == irma_twos_complement) {
1819 ir_node *pre = get_Conv_op(left);
1820 ir_mode *pre_mode = get_irn_mode(pre);
1822 if (mode_is_int(pre_mode) &&
1823 get_mode_size_bits(pre_mode) == bits &&
1824 get_mode_arithmetic(pre_mode) == irma_twos_complement) {
1825 /* ok, this conv just changes to sign, moreover the calculation
1826 * is done with same number of bits as our address mode, so
1827 * we can ignore the conv as address calculation can be viewed
1828 * as either signed or unsigned
1830 set_binop_left(n, pre);
1835 if (get_irn_op(right) == op_Conv) {
1836 ir_mode *mode = get_irn_mode(right);
1837 int bits = get_mode_size_bits(mode);
1839 if (ref_bits == bits &&
1840 mode_is_int(mode) &&
1841 get_mode_arithmetic(mode) == irma_twos_complement) {
1842 ir_node *pre = get_Conv_op(right);
1843 ir_mode *pre_mode = get_irn_mode(pre);
1845 if (mode_is_int(pre_mode) &&
1846 get_mode_size_bits(pre_mode) == bits &&
1847 get_mode_arithmetic(pre_mode) == irma_twos_complement) {
1848 /* ok, this conv just changes to sign, moreover the calculation
1849 * is done with same number of bits as our address mode, so
1850 * we can ignore the conv as address calculation can be viewed
1851 * as either signed or unsigned
1853 set_binop_right(n, pre);
1859 } /* transform_node_AddSub */
1861 #define HANDLE_BINOP_PHI(op,a,b,c) \
1863 if (is_Const(b) && is_const_Phi(a)) { \
1864 /* check for Op(Phi, Const) */ \
1865 c = apply_binop_on_phi(a, get_Const_tarval(b), op, 0); \
1867 else if (is_Const(a) && is_const_Phi(b)) { \
1868 /* check for Op(Const, Phi) */ \
1869 c = apply_binop_on_phi(b, get_Const_tarval(a), op, 1); \
1872 DBG_OPT_ALGSIM0(oldn, c, FS_OPT_CONST_PHI); \
1876 #define HANDLE_UNOP_PHI(op,a,c) \
1878 if (is_const_Phi(a)) { \
1879 /* check for Op(Phi) */ \
1880 c = apply_unop_on_phi(a, op); \
1883 DBG_OPT_ALGSIM0(oldn, c, FS_OPT_CONST_PHI); \
1889 * Do the AddSub optimization, then Transform
1890 * Constant folding on Phi
1891 * Add(a,a) -> Mul(a, 2)
1892 * Add(Mul(a, x), a) -> Mul(a, x+1)
1893 * if the mode is integer or float.
1894 * Transform Add(a,-b) into Sub(a,b).
1895 * Reassociation might fold this further.
1897 static ir_node *transform_node_Add(ir_node *n)
1900 ir_node *a, *b, *c, *oldn = n;
1902 n = transform_node_AddSub(n);
1904 a = get_Add_left(n);
1905 b = get_Add_right(n);
1907 HANDLE_BINOP_PHI(tarval_add, a,b,c);
1909 mode = get_irn_mode(n);
1911 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
1912 if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
1915 if (mode_is_num(mode)) {
1917 ir_node *block = get_irn_n(n, -1);
1920 get_irn_dbg_info(n),
1924 new_r_Const_long(current_ir_graph, block, mode, 2),
1926 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_A);
1928 else if (get_irn_op(a) == op_Minus) {
1930 get_irn_dbg_info(n),
1936 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_MINUS_B);
1938 else if (get_irn_op(b) == op_Minus) {
1940 get_irn_dbg_info(n),
1946 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_MINUS_B);
1948 /* do NOT execute this code if reassociation is enabled, it does the inverse! */
1949 else if (!get_opt_reassociation() && get_irn_op(a) == op_Mul) {
1950 ir_node *ma = get_Mul_left(a);
1951 ir_node *mb = get_Mul_right(a);
1954 ir_node *blk = get_irn_n(n, -1);
1956 get_irn_dbg_info(n), current_ir_graph, blk,
1959 get_irn_dbg_info(n), current_ir_graph, blk,
1961 new_r_Const_long(current_ir_graph, blk, mode, 1),
1964 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_MUL_A_X_A);
1967 ir_node *blk = get_irn_n(n, -1);
1969 get_irn_dbg_info(n), current_ir_graph, blk,
1972 get_irn_dbg_info(n), current_ir_graph, blk,
1974 new_r_Const_long(current_ir_graph, blk, mode, 1),
1977 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_MUL_A_X_A);
1980 /* do NOT execute this code if reassociation is enabled, it does the inverse! */
1981 else if (!get_opt_reassociation() && get_irn_op(b) == op_Mul) {
1982 ir_node *ma = get_Mul_left(b);
1983 ir_node *mb = get_Mul_right(b);
1986 ir_node *blk = get_irn_n(n, -1);
1988 get_irn_dbg_info(n), current_ir_graph, blk,
1991 get_irn_dbg_info(n), current_ir_graph, blk,
1993 new_r_Const_long(current_ir_graph, blk, mode, 1),
1996 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_MUL_A_X_A);
1999 ir_node *blk = get_irn_n(n, -1);
2001 get_irn_dbg_info(n), current_ir_graph, blk,
2004 get_irn_dbg_info(n), current_ir_graph, blk,
2006 new_r_Const_long(current_ir_graph, blk, mode, 1),
2009 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_MUL_A_X_A);
2014 } /* transform_node_Add */
2017 * Do the AddSub optimization, then Transform
2018 * Constant folding on Phi
2019 * Sub(0,a) -> Minus(a)
2020 * Sub(Mul(a, x), a) -> Mul(a, x-1)
2021 * Sub(Sub(x, y), b) -> Sub(x, Add(y,b))
2023 static ir_node *transform_node_Sub(ir_node *n)
2029 n = transform_node_AddSub(n);
2031 a = get_Sub_left(n);
2032 b = get_Sub_right(n);
2034 HANDLE_BINOP_PHI(tarval_sub, a,b,c);
2036 mode = get_irn_mode(n);
2038 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
2039 if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
2042 /* Beware of Sub(P, P) which cannot be optimized into a simple Minus ... */
2043 if (mode_is_num(mode) && mode == get_irn_mode(a) && (classify_Const(a) == CNST_NULL)) {
2045 get_irn_dbg_info(n),
2050 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_0_A);
2052 /* do NOT execute this code if reassociation is enabled, it does the inverse! */
2053 else if (get_opt_reassociation() && get_irn_op(a) == op_Mul) {
2054 ir_node *ma = get_Mul_left(a);
2055 ir_node *mb = get_Mul_right(a);
2058 ir_node *blk = get_irn_n(n, -1);
2060 get_irn_dbg_info(n),
2061 current_ir_graph, blk,
2064 get_irn_dbg_info(n),
2065 current_ir_graph, blk,
2067 new_r_Const_long(current_ir_graph, blk, mode, 1),
2070 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MUL_A_X_A);
2073 ir_node *blk = get_irn_n(n, -1);
2075 get_irn_dbg_info(n),
2076 current_ir_graph, blk,
2079 get_irn_dbg_info(n),
2080 current_ir_graph, blk,
2082 new_r_Const_long(current_ir_graph, blk, mode, 1),
2085 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MUL_A_X_A);
2088 else if (get_irn_op(a) == op_Sub) {
2089 ir_node *x = get_Sub_left(a);
2090 ir_node *y = get_Sub_right(a);
2091 ir_node *blk = get_irn_n(n, -1);
2092 ir_mode *m_b = get_irn_mode(b);
2093 ir_mode *m_y = get_irn_mode(y);
2096 /* Determine the right mode for the Add. */
2099 else if (mode_is_reference(m_b))
2101 else if (mode_is_reference(m_y))
2105 * Both modes are different but none is reference,
2106 * happens for instance in SubP(SubP(P, Iu), Is).
2107 * We have two possibilities here: Cast or ignore.
2108 * Currently we ignore this case.
2113 add = new_r_Add(current_ir_graph, blk, y, b, mode);
2116 set_Sub_right(n, add);
2117 DBG_OPT_ALGSIM0(n, n, FS_OPT_SUB_SUB_X_Y_Z);
2121 } /* transform_node_Sub */
2124 * Transform Mul(a,-1) into -a.
2125 * Do constant evaluation of Phi nodes.
2126 * Do architecture dependent optimizations on Mul nodes
2128 static ir_node *transform_node_Mul(ir_node *n) {
2129 ir_node *c, *oldn = n;
2130 ir_node *a = get_Mul_left(n);
2131 ir_node *b = get_Mul_right(n);
2134 HANDLE_BINOP_PHI(tarval_mul, a,b,c);
2136 mode = get_irn_mode(n);
2137 if (mode_is_signed(mode)) {
2140 if (value_of(a) == get_mode_minus_one(mode))
2142 else if (value_of(b) == get_mode_minus_one(mode))
2145 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1), r, mode);
2146 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2150 return arch_dep_replace_mul_with_shifts(n);
2151 } /* transform_node_Mul */
2154 * Transform a Div Node.
2156 static ir_node *transform_node_Div(ir_node *n)
2158 tarval *tv = value_of(n);
2161 /* BEWARE: it is NOT possible to optimize a/a to 1, as this may cause a exception */
2163 if (tv != tarval_bad) {
2164 value = new_Const(get_tarval_mode(tv), tv);
2166 DBG_OPT_CSTEVAL(n, value);
2168 else /* Try architecture dependent optimization */
2169 value = arch_dep_replace_div_by_const(n);
2172 /* Turn Div into a tuple (mem, bad, value) */
2173 ir_node *mem = get_Div_mem(n);
2175 turn_into_tuple(n, pn_Div_max);
2176 set_Tuple_pred(n, pn_Div_M, mem);
2177 set_Tuple_pred(n, pn_Div_X_except, new_Bad());
2178 set_Tuple_pred(n, pn_Div_res, value);
2181 } /* transform_node_Div */
2184 * Transform a Mod node.
2186 static ir_node *transform_node_Mod(ir_node *n)
2188 tarval *tv = value_of(n);
2191 /* BEWARE: it is NOT possible to optimize a%a to 0, as this may cause a exception */
2193 if (tv != tarval_bad) {
2194 value = new_Const(get_tarval_mode(tv), tv);
2196 DBG_OPT_CSTEVAL(n, value);
2198 else /* Try architecture dependent optimization */
2199 value = arch_dep_replace_mod_by_const(n);
2202 /* Turn Mod into a tuple (mem, bad, value) */
2203 ir_node *mem = get_Mod_mem(n);
2205 turn_into_tuple(n, pn_Mod_max);
2206 set_Tuple_pred(n, pn_Mod_M, mem);
2207 set_Tuple_pred(n, pn_Mod_X_except, new_Bad());
2208 set_Tuple_pred(n, pn_Mod_res, value);
2211 } /* transform_node_Mod */
2214 * Transform a DivMod node.
2216 static ir_node *transform_node_DivMod(ir_node *n)
2220 ir_node *a = get_DivMod_left(n);
2221 ir_node *b = get_DivMod_right(n);
2222 ir_mode *mode = get_irn_mode(a);
2223 tarval *ta = value_of(a);
2224 tarval *tb = value_of(b);
2226 if (!(mode_is_int(mode) && mode_is_int(get_irn_mode(b))))
2229 /* BEWARE: it is NOT possible to optimize a/a to 1, as this may cause a exception */
2231 if (tb != tarval_bad) {
2232 if (tb == get_mode_one(get_tarval_mode(tb))) {
2233 b = new_Const (mode, get_mode_null(mode));
2236 DBG_OPT_CSTEVAL(n, b);
2238 else if (ta != tarval_bad) {
2239 tarval *resa, *resb;
2240 resa = tarval_div (ta, tb);
2241 if (resa == tarval_bad) return n; /* Causes exception!!! Model by replacing through
2242 Jmp for X result!? */
2243 resb = tarval_mod (ta, tb);
2244 if (resb == tarval_bad) return n; /* Causes exception! */
2245 a = new_Const (mode, resa);
2246 b = new_Const (mode, resb);
2249 DBG_OPT_CSTEVAL(n, a);
2250 DBG_OPT_CSTEVAL(n, b);
2252 else { /* Try architecture dependent optimization */
2253 arch_dep_replace_divmod_by_const(&a, &b, n);
2254 evaluated = a != NULL;
2256 } else if (ta == get_mode_null(mode)) {
2257 /* 0 / non-Const = 0 */
2262 if (evaluated) { /* replace by tuple */
2263 ir_node *mem = get_DivMod_mem(n);
2264 turn_into_tuple(n, pn_DivMod_max);
2265 set_Tuple_pred(n, pn_DivMod_M, mem);
2266 set_Tuple_pred(n, pn_DivMod_X_except, new_Bad()); /* no exception */
2267 set_Tuple_pred(n, pn_DivMod_res_div, a);
2268 set_Tuple_pred(n, pn_DivMod_res_mod, b);
2272 } /* transform_node_DivMod */
2275 * Optimize Abs(x) into x if x is Confirmed >= 0
2276 * Optimize Abs(x) into -x if x is Confirmed <= 0
2278 static ir_node *transform_node_Abs(ir_node *n)
2281 ir_node *a = get_Abs_op(n);
2282 value_classify_sign sign = classify_value_sign(a);
2284 if (sign == value_classified_negative) {
2285 ir_mode *mode = get_irn_mode(n);
2288 * We can replace the Abs by -x here.
2289 * We even could add a new Confirm here.
2291 * Note that -x would create a new node, so we could
2292 * not run it in the equivalent_node() context.
2294 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph,
2295 get_irn_n(n, -1), a, mode);
2297 DBG_OPT_CONFIRM(oldn, n);
2299 else if (sign == value_classified_positive) {
2300 /* n is positive, Abs is not needed */
2303 DBG_OPT_CONFIRM(oldn, n);
2307 } /* transform_node_Abs */
2310 * Transform a Cond node.
2312 static ir_node *transform_node_Cond(ir_node *n)
2314 /* Replace the Cond by a Jmp if it branches on a constant
2317 ir_node *a = get_Cond_selector(n);
2318 tarval *ta = value_of(a);
2320 /* we need block info which is not available in floating irgs */
2321 if (get_irg_pinned(current_ir_graph) == op_pin_state_floats)
2324 if ((ta != tarval_bad) &&
2325 (get_irn_mode(a) == mode_b) &&
2326 (get_opt_unreachable_code())) {
2327 /* It's a boolean Cond, branching on a boolean constant.
2328 Replace it by a tuple (Bad, Jmp) or (Jmp, Bad) */
2329 jmp = new_r_Jmp(current_ir_graph, get_nodes_block(n));
2330 turn_into_tuple(n, pn_Cond_max);
2331 if (ta == tarval_b_true) {
2332 set_Tuple_pred(n, pn_Cond_false, new_Bad());
2333 set_Tuple_pred(n, pn_Cond_true, jmp);
2335 set_Tuple_pred(n, pn_Cond_false, jmp);
2336 set_Tuple_pred(n, pn_Cond_true, new_Bad());
2338 /* We might generate an endless loop, so keep it alive. */
2339 add_End_keepalive(get_irg_end(current_ir_graph), get_nodes_block(n));
2342 } /* transform_node_Cond */
2347 static ir_node *transform_node_And(ir_node *n)
2349 ir_node *c, *oldn = n;
2350 ir_node *a = get_And_left(n);
2351 ir_node *b = get_And_right(n);
2353 HANDLE_BINOP_PHI(tarval_and, a,b,c);
2355 } /* transform_node_And */
2360 static ir_node *transform_node_Eor(ir_node *n)
2362 ir_node *c, *oldn = n;
2363 ir_node *a = get_Eor_left(n);
2364 ir_node *b = get_Eor_right(n);
2365 ir_mode *mode = get_irn_mode(n);
2367 HANDLE_BINOP_PHI(tarval_eor, a,b,c);
2371 n = new_rd_Const(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1),
2372 mode, get_mode_null(mode));
2373 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_A_A);
2375 else if ((mode == mode_b)
2376 && (get_irn_op(a) == op_Proj)
2377 && (get_irn_mode(a) == mode_b)
2378 && (classify_tarval (value_of(b)) == TV_CLASSIFY_ONE)
2379 && (get_irn_op(get_Proj_pred(a)) == op_Cmp)) {
2380 /* The Eor negates a Cmp. The Cmp has the negated result anyways! */
2381 n = new_r_Proj(current_ir_graph, get_irn_n(n, -1), get_Proj_pred(a),
2382 mode_b, get_negated_pnc(get_Proj_proj(a), mode));
2384 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT_BOOL);
2386 else if ((mode == mode_b)
2387 && (classify_tarval (value_of(b)) == TV_CLASSIFY_ONE)) {
2388 /* The Eor is a Not. Replace it by a Not. */
2389 /* ????!!!Extend to bitfield 1111111. */
2390 n = new_r_Not(current_ir_graph, get_irn_n(n, -1), a, mode_b);
2392 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
2396 } /* transform_node_Eor */
2401 static ir_node *transform_node_Not(ir_node *n)
2403 ir_node *c, *oldn = n;
2404 ir_node *a = get_Not_op(n);
2406 HANDLE_UNOP_PHI(tarval_not,a,c);
2408 /* check for a boolean Not */
2409 if ( (get_irn_mode(n) == mode_b)
2410 && (get_irn_op(a) == op_Proj)
2411 && (get_irn_mode(a) == mode_b)
2412 && (get_irn_op(get_Proj_pred(a)) == op_Cmp)) {
2413 /* We negate a Cmp. The Cmp has the negated result anyways! */
2414 n = new_r_Proj(current_ir_graph, get_irn_n(n, -1), get_Proj_pred(a),
2415 mode_b, get_negated_pnc(get_Proj_proj(a), mode_b));
2416 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_CMP);
2419 } /* transform_node_Not */
2422 * Transform a Minus.
2424 static ir_node *transform_node_Minus(ir_node *n)
2426 ir_node *c, *oldn = n;
2427 ir_node *a = get_Minus_op(n);
2429 HANDLE_UNOP_PHI(tarval_neg,a,c);
2431 } /* transform_node_Minus */
2434 * Transform a Cast_type(Const) into a new Const_type
2436 static ir_node *transform_node_Cast(ir_node *n) {
2438 ir_node *pred = get_Cast_op(n);
2439 ir_type *tp = get_irn_type(n);
2441 if (get_irn_op(pred) == op_Const && get_Const_type(pred) != tp) {
2442 n = new_rd_Const_type(NULL, current_ir_graph, get_irn_n(pred, -1), get_irn_mode(pred),
2443 get_Const_tarval(pred), tp);
2444 DBG_OPT_CSTEVAL(oldn, n);
2445 } else if ((get_irn_op(pred) == op_SymConst) && (get_SymConst_value_type(pred) != tp)) {
2446 n = new_rd_SymConst_type(NULL, current_ir_graph, get_irn_n(pred, -1), get_SymConst_symbol(pred),
2447 get_SymConst_kind(pred), tp);
2448 DBG_OPT_CSTEVAL(oldn, n);
2452 } /* transform_node_Cast */
2455 * Transform a Proj(Div) with a non-zero value.
2456 * Removes the exceptions and routes the memory to the NoMem node.
2458 static ir_node *transform_node_Proj_Div(ir_node *proj)
2460 ir_node *n = get_Proj_pred(proj);
2461 ir_node *b = get_Div_right(n);
2465 if (value_not_zero(b, &confirm)) {
2466 /* div(x, y) && y != 0 */
2467 proj_nr = get_Proj_proj(proj);
2468 if (proj_nr == pn_Div_X_except) {
2469 /* we found an exception handler, remove it */
2470 DBG_OPT_EXC_REM(proj);
2473 else if (proj_nr == pn_Div_M) {
2474 ir_node *res = get_Div_mem(n);
2475 ir_node *new_mem = get_irg_no_mem(current_ir_graph);
2478 /* This node can only float up to the Confirm block */
2479 new_mem = new_r_Pin(current_ir_graph, get_nodes_block(confirm), new_mem);
2481 set_irn_pinned(n, op_pin_state_floats);
2482 /* this is a Div without exception, we can remove the memory edge */
2483 set_Div_mem(n, new_mem);
2488 } /* transform_node_Proj_Div */
2491 * Transform a Proj(Mod) with a non-zero value.
2492 * Removes the exceptions and routes the memory to the NoMem node.
2494 static ir_node *transform_node_Proj_Mod(ir_node *proj)
2496 ir_node *n = get_Proj_pred(proj);
2497 ir_node *b = get_Mod_right(n);
2501 if (value_not_zero(b, &confirm)) {
2502 /* mod(x, y) && y != 0 */
2503 proj_nr = get_Proj_proj(proj);
2505 if (proj_nr == pn_Mod_X_except) {
2506 /* we found an exception handler, remove it */
2507 DBG_OPT_EXC_REM(proj);
2509 } else if (proj_nr == pn_Mod_M) {
2510 ir_node *res = get_Mod_mem(n);
2511 ir_node *new_mem = get_irg_no_mem(current_ir_graph);
2514 /* This node can only float up to the Confirm block */
2515 new_mem = new_r_Pin(current_ir_graph, get_nodes_block(confirm), new_mem);
2517 set_irn_pinned(n, op_pin_state_floats);
2518 /* this is a Mod without exception, we can remove the memory edge */
2519 set_Mod_mem(n, get_irg_no_mem(current_ir_graph));
2522 else if (proj_nr == pn_Mod_res && get_Mod_left(n) == b) {
2523 /* a % a = 0 if a != 0 */
2524 ir_mode *mode = get_irn_mode(proj);
2525 ir_node *res = new_Const(mode, get_mode_null(mode));
2527 DBG_OPT_CSTEVAL(n, res);
2532 } /* transform_node_Proj_Mod */
2535 * Transform a Proj(DivMod) with a non-zero value.
2536 * Removes the exceptions and routes the memory to the NoMem node.
2538 static ir_node *transform_node_Proj_DivMod(ir_node *proj)
2540 ir_node *n = get_Proj_pred(proj);
2541 ir_node *b = get_DivMod_right(n);
2545 if (value_not_zero(b, &confirm)) {
2546 /* DivMod(x, y) && y != 0 */
2547 proj_nr = get_Proj_proj(proj);
2549 if (proj_nr == pn_DivMod_X_except) {
2550 /* we found an exception handler, remove it */
2551 DBG_OPT_EXC_REM(proj);
2554 else if (proj_nr == pn_DivMod_M) {
2555 ir_node *res = get_DivMod_mem(n);
2556 ir_node *new_mem = get_irg_no_mem(current_ir_graph);
2559 /* This node can only float up to the Confirm block */
2560 new_mem = new_r_Pin(current_ir_graph, get_nodes_block(confirm), new_mem);
2562 set_irn_pinned(n, op_pin_state_floats);
2563 /* this is a DivMod without exception, we can remove the memory edge */
2564 set_DivMod_mem(n, get_irg_no_mem(current_ir_graph));
2567 else if (proj_nr == pn_DivMod_res_mod && get_DivMod_left(n) == b) {
2568 /* a % a = 0 if a != 0 */
2569 ir_mode *mode = get_irn_mode(proj);
2570 ir_node *res = new_Const(mode, get_mode_null(mode));
2572 DBG_OPT_CSTEVAL(n, res);
2577 } /* transform_node_Proj_DivMod */
2580 * Optimizes jump tables (CondIs or CondIu) by removing all impossible cases.
2582 static ir_node *transform_node_Proj_Cond(ir_node *proj)
2584 if (get_opt_unreachable_code()) {
2585 ir_node *n = get_Proj_pred(proj);
2586 ir_node *b = get_Cond_selector(n);
2588 if (mode_is_int(get_irn_mode(b))) {
2589 tarval *tb = value_of(b);
2591 if (tb != tarval_bad) {
2592 /* we have a constant switch */
2593 long num = get_Proj_proj(proj);
2595 if (num != get_Cond_defaultProj(n)) { /* we cannot optimize default Proj's yet */
2596 if (get_tarval_long(tb) == num) {
2597 /* Do NOT create a jump here, or we will have 2 control flow ops
2598 * in a block. This case is optimized away in optimize_cf(). */
2602 /* this case will NEVER be taken, kill it */
2610 } /* transform_node_Proj_Cond */
2613 * Normalizes and optimizes Cmp nodes.
2615 static ir_node *transform_node_Proj_Cmp(ir_node *proj)
2617 if (get_opt_reassociation()) {
2618 ir_node *n = get_Proj_pred(proj);
2619 ir_node *left = get_Cmp_left(n);
2620 ir_node *right = get_Cmp_right(n);
2624 ir_mode *mode = NULL;
2625 long proj_nr = get_Proj_proj(proj);
2628 * First step: normalize the compare op
2629 * by placing the constant on the right site
2630 * or moving the lower address node to the left.
2631 * We ignore the case that both are constants
2632 * this case should be optimized away.
2634 if (get_irn_op(right) == op_Const)
2636 else if (get_irn_op(left) == op_Const) {
2641 proj_nr = get_inversed_pnc(proj_nr);
2644 else if (get_irn_idx(left) > get_irn_idx(right)) {
2650 proj_nr = get_inversed_pnc(proj_nr);
2655 * Second step: Try to reduce the magnitude
2656 * of a constant. This may help to generate better code
2657 * later and may help to normalize more compares.
2658 * Of course this is only possible for integer values.
2661 mode = get_irn_mode(c);
2662 tv = get_Const_tarval(c);
2664 if (tv != tarval_bad) {
2665 /* the following optimization is possible on modes without Overflow
2666 * on Unary Minus or on == and !=:
2667 * -a CMP c ==> a swap(CMP) -c
2669 * Beware: for two-complement Overflow may occur, so only == and != can
2670 * be optimized, see this:
2671 * -MININT < 0 =/=> MININT > 0 !!!
2673 if (get_opt_constant_folding() && get_irn_op(left) == op_Minus &&
2674 (!mode_overflow_on_unary_Minus(mode) ||
2675 (mode_is_int(mode) && (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg)))) {
2676 left = get_Minus_op(left);
2677 tv = tarval_sub(get_mode_null(mode), tv);
2679 proj_nr = get_inversed_pnc(proj_nr);
2683 /* for integer modes, we have more */
2684 if (mode_is_int(mode)) {
2685 /* Ne includes Unordered which is not possible on integers.
2686 * However, frontends often use this wrong, so fix it here */
2687 if (proj_nr & pn_Cmp_Uo) {
2688 proj_nr &= ~pn_Cmp_Uo;
2689 set_Proj_proj(proj, proj_nr);
2692 /* c > 0 : a < c ==> a <= (c-1) a >= c ==> a > (c-1) */
2693 if ((proj_nr == pn_Cmp_Lt || proj_nr == pn_Cmp_Ge) &&
2694 tarval_cmp(tv, get_mode_null(mode)) == pn_Cmp_Gt) {
2695 tv = tarval_sub(tv, get_mode_one(mode));
2697 proj_nr ^= pn_Cmp_Eq;
2700 /* c < 0 : a > c ==> a >= (c+1) a <= c ==> a < (c+1) */
2701 else if ((proj_nr == pn_Cmp_Gt || proj_nr == pn_Cmp_Le) &&
2702 tarval_cmp(tv, get_mode_null(mode)) == pn_Cmp_Lt) {
2703 tv = tarval_add(tv, get_mode_one(mode));
2705 proj_nr ^= pn_Cmp_Eq;
2709 /* the following reassociations work only for == and != */
2710 if (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) {
2712 /* a-b == 0 ==> a == b, a-b != 0 ==> a != b */
2713 if (classify_tarval(tv) == TV_CLASSIFY_NULL && get_irn_op(left) == op_Sub) {
2714 right = get_Sub_right(left);
2715 left = get_Sub_left(left);
2717 tv = value_of(right);
2721 if (tv != tarval_bad) {
2722 ir_op *op = get_irn_op(left);
2724 /* a-c1 == c2 ==> a == c2+c1, a-c1 != c2 ==> a != c2+c1 */
2726 ir_node *c1 = get_Sub_right(left);
2727 tarval *tv2 = value_of(c1);
2729 if (tv2 != tarval_bad) {
2730 tv2 = tarval_add(tv, value_of(c1));
2732 if (tv2 != tarval_bad) {
2733 left = get_Sub_left(left);
2739 /* a+c1 == c2 ==> a == c2-c1, a+c1 != c2 ==> a != c2-c1 */
2740 else if (op == op_Add) {
2741 ir_node *a_l = get_Add_left(left);
2742 ir_node *a_r = get_Add_right(left);
2746 if (get_irn_op(a_l) == op_Const) {
2748 tv2 = value_of(a_l);
2752 tv2 = value_of(a_r);
2755 if (tv2 != tarval_bad) {
2756 tv2 = tarval_sub(tv, tv2);
2758 if (tv2 != tarval_bad) {
2765 /* -a == c ==> a == -c, -a != c ==> a != -c */
2766 else if (op == op_Minus) {
2767 tarval *tv2 = tarval_sub(get_mode_null(mode), tv);
2769 if (tv2 != tarval_bad) {
2770 left = get_Minus_op(left);
2777 /* the following reassociations work only for <= */
2778 else if (proj_nr == pn_Cmp_Le || proj_nr == pn_Cmp_Lt) {
2779 if (tv != tarval_bad) {
2780 ir_op *op = get_irn_op(left);
2782 /* c >= 0 : Abs(a) <= c ==> (unsigned)(a + c) <= 2*c */
2790 * optimization for AND:
2792 * And(x, C) == C ==> And(x, C) != 0
2793 * And(x, C) != C ==> And(X, C) == 0
2795 * if C is a single Bit constant.
2797 if ((proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) &&
2798 (get_irn_op(left) == op_And)) {
2799 if (is_single_bit_tarval(tv)) {
2800 /* check for Constant's match. We have check hare the tarvals,
2801 because our const might be changed */
2802 ir_node *la = get_And_left(left);
2803 ir_node *ra = get_And_right(left);
2804 if ((is_Const(la) && get_Const_tarval(la) == tv) ||
2805 (is_Const(ra) && get_Const_tarval(ra) == tv)) {
2806 /* fine: do the transformation */
2807 tv = get_mode_null(get_tarval_mode(tv));
2808 proj_nr ^= pn_Cmp_Leg;
2813 } /* tarval != bad */
2817 ir_node *block = get_irn_n(n, -1); /* Beware of get_nodes_Block() */
2819 if (changed & 2) /* need a new Const */
2820 right = new_Const(mode, tv);
2822 /* create a new compare */
2823 n = new_rd_Cmp(get_irn_dbg_info(n), current_ir_graph, block,
2826 set_Proj_pred(proj, n);
2827 set_Proj_proj(proj, proj_nr);
2831 } /* transform_node_Proj_Cmp */
2834 * Does all optimizations on nodes that must be done on it's Proj's
2835 * because of creating new nodes.
2837 static ir_node *transform_node_Proj(ir_node *proj)
2839 ir_node *n = get_Proj_pred(proj);
2841 switch (get_irn_opcode(n)) {
2843 return transform_node_Proj_Div(proj);
2846 return transform_node_Proj_Mod(proj);
2849 return transform_node_Proj_DivMod(proj);
2852 return transform_node_Proj_Cond(proj);
2855 return transform_node_Proj_Cmp(proj);
2858 /* should not happen, but if it does will be optimized away */
2859 return equivalent_node_Proj(proj);
2865 } /* transform_node_Proj */
2868 * Move Confirms down through Phi nodes.
2870 static ir_node *transform_node_Phi(ir_node *phi) {
2872 ir_mode *mode = get_irn_mode(phi);
2874 if (mode_is_reference(mode)) {
2875 n = get_irn_arity(phi);
2877 /* Beware of Phi0 */
2879 ir_node *pred = get_irn_n(phi, 0);
2880 ir_node *bound, *new_Phi, *block, **in;
2883 if (! is_Confirm(pred))
2886 bound = get_Confirm_bound(pred);
2887 pnc = get_Confirm_cmp(pred);
2889 NEW_ARR_A(ir_node *, in, n);
2890 in[0] = get_Confirm_value(pred);
2892 for (i = 1; i < n; ++i) {
2893 pred = get_irn_n(phi, i);
2895 if (! is_Confirm(pred) ||
2896 get_Confirm_bound(pred) != bound ||
2897 get_Confirm_cmp(pred) != pnc)
2899 in[i] = get_Confirm_value(pred);
2901 /* move the Confirm nodes "behind" the Phi */
2902 block = get_irn_n(phi, -1);
2903 new_Phi = new_r_Phi(current_ir_graph, block, n, in, get_irn_mode(phi));
2904 return new_r_Confirm(current_ir_graph, block, new_Phi, bound, pnc);
2908 } /* transform_node_Phi */
2911 * Returns the operands of a commutative bin-op, if one operand is
2912 * a const, it is returned as the second one.
2914 static void get_comm_Binop_Ops(ir_node *binop, ir_node **a, ir_node **c)
2916 ir_node *op_a = get_binop_left(binop);
2917 ir_node *op_b = get_binop_right(binop);
2919 assert(is_op_commutative(get_irn_op(binop)));
2921 if (get_irn_op(op_a) == op_Const) {
2929 } /* get_comm_Binop_Ops */
2932 * Optimize a Or(And(Or(And(v,c4),c3),c2),c1) pattern if possible.
2933 * Such pattern may arise in bitfield stores.
2935 * value c4 value c4 & c2
2936 * AND c3 AND c1 | c3
2941 static ir_node *transform_node_Or_bf_store(ir_node *or)
2945 ir_node *and_l, *c3;
2946 ir_node *value, *c4;
2947 ir_node *new_and, *new_const, *block;
2948 ir_mode *mode = get_irn_mode(or);
2950 tarval *tv1, *tv2, *tv3, *tv4, *tv, *n_tv4, *n_tv2;
2952 get_comm_Binop_Ops(or, &and, &c1);
2953 if ((get_irn_op(c1) != op_Const) || (get_irn_op(and) != op_And))
2956 get_comm_Binop_Ops(and, &or_l, &c2);
2957 if ((get_irn_op(c2) != op_Const) || (get_irn_op(or_l) != op_Or))
2960 get_comm_Binop_Ops(or_l, &and_l, &c3);
2961 if ((get_irn_op(c3) != op_Const) || (get_irn_op(and_l) != op_And))
2964 get_comm_Binop_Ops(and_l, &value, &c4);
2965 if (get_irn_op(c4) != op_Const)
2968 /* ok, found the pattern, check for conditions */
2969 assert(mode == get_irn_mode(and));
2970 assert(mode == get_irn_mode(or_l));
2971 assert(mode == get_irn_mode(and_l));
2973 tv1 = get_Const_tarval(c1);
2974 tv2 = get_Const_tarval(c2);
2975 tv3 = get_Const_tarval(c3);
2976 tv4 = get_Const_tarval(c4);
2978 tv = tarval_or(tv4, tv2);
2979 if (classify_tarval(tv) != TV_CLASSIFY_ALL_ONE) {
2980 /* have at least one 0 at the same bit position */
2984 n_tv4 = tarval_not(tv4);
2985 if (tv3 != tarval_and(tv3, n_tv4)) {
2986 /* bit in the or_mask is outside the and_mask */
2990 n_tv2 = tarval_not(tv2);
2991 if (tv1 != tarval_and(tv1, n_tv2)) {
2992 /* bit in the or_mask is outside the and_mask */
2996 /* ok, all conditions met */
2997 block = get_irn_n(or, -1);
2999 new_and = new_r_And(current_ir_graph, block,
3000 value, new_r_Const(current_ir_graph, block, mode, tarval_and(tv4, tv2)), mode);
3002 new_const = new_r_Const(current_ir_graph, block, mode, tarval_or(tv3, tv1));
3004 set_Or_left(or, new_and);
3005 set_Or_right(or, new_const);
3007 /* check for more */
3008 return transform_node_Or_bf_store(or);
3009 } /* transform_node_Or_bf_store */
3012 * Optimize an Or(shl(x, c), shr(x, bits - c)) into a Rot
3014 static ir_node *transform_node_Or_Rot(ir_node *or)
3016 ir_mode *mode = get_irn_mode(or);
3017 ir_node *shl, *shr, *block;
3018 ir_node *irn, *x, *c1, *c2, *v, *sub, *n;
3021 if (! mode_is_int(mode))
3024 shl = get_binop_left(or);
3025 shr = get_binop_right(or);
3027 if (get_irn_op(shl) == op_Shr) {
3028 if (get_irn_op(shr) != op_Shl)
3035 else if (get_irn_op(shl) != op_Shl)
3037 else if (get_irn_op(shr) != op_Shr)
3040 x = get_Shl_left(shl);
3041 if (x != get_Shr_left(shr))
3044 c1 = get_Shl_right(shl);
3045 c2 = get_Shr_right(shr);
3046 if (get_irn_op(c1) == op_Const && get_irn_op(c2) == op_Const) {
3047 tv1 = get_Const_tarval(c1);
3048 if (! tarval_is_long(tv1))
3051 tv2 = get_Const_tarval(c2);
3052 if (! tarval_is_long(tv2))
3055 if (get_tarval_long(tv1) + get_tarval_long(tv2)
3056 != get_mode_size_bits(mode))
3059 /* yet, condition met */
3060 block = get_irn_n(or, -1);
3062 n = new_r_Rot(current_ir_graph, block, x, c1, mode);
3064 DBG_OPT_ALGSIM1(or, shl, shr, n, FS_OPT_OR_SHFT_TO_ROT);
3067 else if (get_irn_op(c1) == op_Sub) {
3071 if (get_Sub_right(sub) != v)
3074 c1 = get_Sub_left(sub);
3075 if (get_irn_op(c1) != op_Const)
3078 tv1 = get_Const_tarval(c1);
3079 if (! tarval_is_long(tv1))
3082 if (get_tarval_long(tv1) != get_mode_size_bits(mode))
3085 /* yet, condition met */
3086 block = get_nodes_block(or);
3088 /* a Rot right is not supported, so use a rot left */
3089 n = new_r_Rot(current_ir_graph, block, x, sub, mode);
3091 DBG_OPT_ALGSIM0(or, n, FS_OPT_OR_SHFT_TO_ROT);
3094 else if (get_irn_op(c2) == op_Sub) {
3098 c1 = get_Sub_left(sub);
3099 if (get_irn_op(c1) != op_Const)
3102 tv1 = get_Const_tarval(c1);
3103 if (! tarval_is_long(tv1))
3106 if (get_tarval_long(tv1) != get_mode_size_bits(mode))
3109 /* yet, condition met */
3110 block = get_irn_n(or, -1);
3113 n = new_r_Rot(current_ir_graph, block, x, v, mode);
3115 DBG_OPT_ALGSIM0(or, n, FS_OPT_OR_SHFT_TO_ROT);
3120 } /* transform_node_Or_Rot */
3125 static ir_node *transform_node_Or(ir_node *n)
3127 ir_node *c, *oldn = n;
3128 ir_node *a = get_Or_left(n);
3129 ir_node *b = get_Or_right(n);
3131 HANDLE_BINOP_PHI(tarval_or, a,b,c);
3133 n = transform_node_Or_bf_store(n);
3134 n = transform_node_Or_Rot(n);
3137 } /* transform_node_Or */
3141 static ir_node *transform_node(ir_node *n);
3144 * Optimize (a >> c1) >> c2), works for Shr, Shrs, Shl.
3146 * Should be moved to reassociation?
3148 static ir_node *transform_node_shift(ir_node *n)
3150 ir_node *left, *right;
3151 tarval *tv1, *tv2, *res;
3153 int modulo_shf, flag;
3155 left = get_binop_left(n);
3157 /* different operations */
3158 if (get_irn_op(left) != get_irn_op(n))
3161 right = get_binop_right(n);
3162 tv1 = value_of(right);
3163 if (tv1 == tarval_bad)
3166 tv2 = value_of(get_binop_right(left));
3167 if (tv2 == tarval_bad)
3170 res = tarval_add(tv1, tv2);
3172 /* beware: a simple replacement works only, if res < modulo shift */
3173 mode = get_irn_mode(n);
3177 modulo_shf = get_mode_modulo_shift(mode);
3178 if (modulo_shf > 0) {
3179 tarval *modulo = new_tarval_from_long(modulo_shf, get_tarval_mode(res));
3181 if (tarval_cmp(res, modulo) & pn_Cmp_Lt)
3188 /* ok, we can replace it */
3189 ir_node *in[2], *irn, *block = get_irn_n(n, -1);
3191 in[0] = get_binop_left(left);
3192 in[1] = new_r_Const(current_ir_graph, block, get_tarval_mode(res), res);
3194 irn = new_ir_node(NULL, current_ir_graph, block, get_irn_op(n), mode, 2, in);
3196 DBG_OPT_ALGSIM0(n, irn, FS_OPT_REASSOC_SHIFT);
3198 return transform_node(irn);
3201 } /* transform_node_shift */
3206 static ir_node *transform_node_Shr(ir_node *n)
3208 ir_node *c, *oldn = n;
3209 ir_node *a = get_Shr_left(n);
3210 ir_node *b = get_Shr_right(n);
3212 HANDLE_BINOP_PHI(tarval_shr, a, b, c);
3213 return transform_node_shift(n);
3214 } /* transform_node_Shr */
3219 static ir_node *transform_node_Shrs(ir_node *n)
3221 ir_node *c, *oldn = n;
3222 ir_node *a = get_Shrs_left(n);
3223 ir_node *b = get_Shrs_right(n);
3225 HANDLE_BINOP_PHI(tarval_shrs, a, b, c);
3226 return transform_node_shift(n);
3227 } /* transform_node_Shrs */
3232 static ir_node *transform_node_Shl(ir_node *n)
3234 ir_node *c, *oldn = n;
3235 ir_node *a = get_Shl_left(n);
3236 ir_node *b = get_Shl_right(n);
3238 HANDLE_BINOP_PHI(tarval_shl, a, b, c);
3239 return transform_node_shift(n);
3240 } /* transform_node_Shl */
3243 * Remove dead blocks and nodes in dead blocks
3244 * in keep alive list. We do not generate a new End node.
3246 static ir_node *transform_node_End(ir_node *n) {
3247 int i, n_keepalives = get_End_n_keepalives(n);
3249 for (i = 0; i < n_keepalives; ++i) {
3250 ir_node *ka = get_End_keepalive(n, i);
3252 if (is_Block_dead(ka)) {
3253 set_End_keepalive(n, i, new_Bad());
3256 else if (is_irn_pinned_in_irg(ka) && is_Block_dead(get_nodes_block(ka)))
3257 set_End_keepalive(n, i, new_Bad());
3260 } /* transform_node_End */
3263 * Optimize a Mux into some simpler cases.
3265 static ir_node *transform_node_Mux(ir_node *n)
3267 ir_node *oldn = n, *sel = get_Mux_sel(n);
3268 ir_mode *mode = get_irn_mode(n);
3270 if (get_irn_op(sel) == op_Proj && !mode_honor_signed_zeros(mode)) {
3271 ir_node *cmp = get_Proj_pred(sel);
3272 long proj_nr = get_Proj_proj(sel);
3273 ir_node *f = get_Mux_false(n);
3274 ir_node *t = get_Mux_true(n);
3276 if (get_irn_op(cmp) == op_Cmp && classify_Const(get_Cmp_right(cmp)) == CNST_NULL) {
3277 ir_node *block = get_irn_n(n, -1);
3280 * Note: normalization puts the constant on the right site,
3281 * so we check only one case.
3283 * Note further that these optimization work even for floating point
3284 * with NaN's because -NaN == NaN.
3285 * However, if +0 and -0 is handled differently, we cannot use the first one.
3287 if (get_irn_op(f) == op_Minus &&
3288 get_Minus_op(f) == t &&
3289 get_Cmp_left(cmp) == t) {
3291 if (proj_nr == pn_Cmp_Ge || proj_nr == pn_Cmp_Gt) {
3292 /* Mux(a >=/> 0, -a, a) ==> Abs(a) */
3293 n = new_rd_Abs(get_irn_dbg_info(n),
3297 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_ABS);
3300 else if (proj_nr == pn_Cmp_Le || proj_nr == pn_Cmp_Lt) {
3301 /* Mux(a <=/< 0, -a, a) ==> Minus(Abs(a)) */
3302 n = new_rd_Abs(get_irn_dbg_info(n),
3306 n = new_rd_Minus(get_irn_dbg_info(n),
3311 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_ABS);
3315 else if (get_irn_op(t) == op_Minus &&
3316 get_Minus_op(t) == f &&
3317 get_Cmp_left(cmp) == f) {
3319 if (proj_nr == pn_Cmp_Le || proj_nr == pn_Cmp_Lt) {
3320 /* Mux(a <=/< 0, a, -a) ==> Abs(a) */
3321 n = new_rd_Abs(get_irn_dbg_info(n),
3325 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_ABS);
3328 else if (proj_nr == pn_Cmp_Ge || proj_nr == pn_Cmp_Gt) {
3329 /* Mux(a >=/> 0, a, -a) ==> Minus(Abs(a)) */
3330 n = new_rd_Abs(get_irn_dbg_info(n),
3334 n = new_rd_Minus(get_irn_dbg_info(n),
3339 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_ABS);
3344 if (mode_is_int(mode) && mode_is_signed(mode) &&
3345 get_mode_arithmetic(mode) == irma_twos_complement) {
3346 ir_node *x = get_Cmp_left(cmp);
3348 /* the following optimization works only with signed integer two-complement mode */
3350 if (mode == get_irn_mode(x)) {
3352 * FIXME: this restriction is two rigid, as it would still
3353 * work if mode(x) = Hs and mode == Is, but at least it removes
3356 if ((proj_nr == pn_Cmp_Lt || proj_nr == pn_Cmp_Le) &&
3357 classify_Const(t) == CNST_ALL_ONE &&
3358 classify_Const(f) == CNST_NULL) {
3360 * Mux(x:T </<= 0, 0, -1) -> Shrs(x, sizeof_bits(T) - 1)
3364 n = new_rd_Shrs(get_irn_dbg_info(n),
3365 current_ir_graph, block, x,
3366 new_r_Const_long(current_ir_graph, block, mode_Iu,
3367 get_mode_size_bits(mode) - 1),
3369 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_SHR);
3372 else if ((proj_nr == pn_Cmp_Gt || proj_nr == pn_Cmp_Ge) &&
3373 classify_Const(t) == CNST_ONE &&
3374 classify_Const(f) == CNST_NULL) {
3376 * Mux(x:T >/>= 0, 0, 1) -> Shr(-x, sizeof_bits(T) - 1)
3380 n = new_rd_Shr(get_irn_dbg_info(n),
3381 current_ir_graph, block,
3382 new_r_Minus(current_ir_graph, block, x, mode),
3383 new_r_Const_long(current_ir_graph, block, mode_Iu,
3384 get_mode_size_bits(mode) - 1),
3386 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_SHR);
3393 return arch_transform_node_Mux(n);
3394 } /* transform_node_Mux */
3397 * Optimize a Psi into some simpler cases.
3399 static ir_node *transform_node_Psi(ir_node *n) {
3401 return transform_node_Mux(n);
3404 } /* transform_node_Psi */
3407 * Tries several [inplace] [optimizing] transformations and returns an
3408 * equivalent node. The difference to equivalent_node() is that these
3409 * transformations _do_ generate new nodes, and thus the old node must
3410 * not be freed even if the equivalent node isn't the old one.
3412 static ir_node *transform_node(ir_node *n)
3414 if (n->op->ops.transform_node)
3415 n = n->op->ops.transform_node(n);
3417 } /* transform_node */
3420 * sSets the default transform node operation for an ir_op_ops.
3422 * @param code the opcode for the default operation
3423 * @param ops the operations initialized
3428 static ir_op_ops *firm_set_default_transform_node(opcode code, ir_op_ops *ops)
3432 ops->transform_node = transform_node_##a; \
3465 } /* firm_set_default_transform_node */
3468 /* **************** Common Subexpression Elimination **************** */
3470 /** The size of the hash table used, should estimate the number of nodes
3472 #define N_IR_NODES 512
3474 /** Compares the attributes of two Const nodes. */
3475 static int node_cmp_attr_Const(ir_node *a, ir_node *b) {
3476 return (get_Const_tarval(a) != get_Const_tarval(b))
3477 || (get_Const_type(a) != get_Const_type(b));
3478 } /* node_cmp_attr_Const */
3480 /** Compares the attributes of two Proj nodes. */
3481 static int node_cmp_attr_Proj(ir_node *a, ir_node *b) {
3482 return get_irn_proj_attr (a) != get_irn_proj_attr (b);
3483 } /* node_cmp_attr_Proj */
3485 /** Compares the attributes of two Filter nodes. */
3486 static int node_cmp_attr_Filter(ir_node *a, ir_node *b) {
3487 return get_Filter_proj(a) != get_Filter_proj(b);
3488 } /* node_cmp_attr_Filter */
3490 /** Compares the attributes of two Alloc nodes. */
3491 static int node_cmp_attr_Alloc(ir_node *a, ir_node *b) {
3492 return (get_irn_alloc_attr(a).where != get_irn_alloc_attr(b).where)
3493 || (get_irn_alloc_attr(a).type != get_irn_alloc_attr(b).type);
3494 } /* node_cmp_attr_Alloc */
3496 /** Compares the attributes of two Free nodes. */
3497 static int node_cmp_attr_Free(ir_node *a, ir_node *b) {
3498 return (get_irn_free_attr(a).where != get_irn_free_attr(b).where)
3499 || (get_irn_free_attr(a).type != get_irn_free_attr(b).type);
3500 } /* node_cmp_attr_Free */
3502 /** Compares the attributes of two SymConst nodes. */
3503 static int node_cmp_attr_SymConst(ir_node *a, ir_node *b) {
3504 return (get_irn_symconst_attr(a).num != get_irn_symconst_attr(b).num)
3505 || (get_irn_symconst_attr(a).sym.type_p != get_irn_symconst_attr(b).sym.type_p)
3506 || (get_irn_symconst_attr(a).tp != get_irn_symconst_attr(b).tp);
3507 } /* node_cmp_attr_SymConst */
3509 /** Compares the attributes of two Call nodes. */
3510 static int node_cmp_attr_Call(ir_node *a, ir_node *b) {
3511 return (get_irn_call_attr(a) != get_irn_call_attr(b));
3512 } /* node_cmp_attr_Call */
3514 /** Compares the attributes of two Sel nodes. */
3515 static int node_cmp_attr_Sel(ir_node *a, ir_node *b) {
3516 return (get_irn_sel_attr(a).ent->kind != get_irn_sel_attr(b).ent->kind)
3517 || (get_irn_sel_attr(a).ent->name != get_irn_sel_attr(b).ent->name)
3518 || (get_irn_sel_attr(a).ent->owner != get_irn_sel_attr(b).ent->owner)
3519 || (get_irn_sel_attr(a).ent->ld_name != get_irn_sel_attr(b).ent->ld_name)
3520 || (get_irn_sel_attr(a).ent->type != get_irn_sel_attr(b).ent->type);
3521 } /* node_cmp_attr_Sel */
3523 /** Compares the attributes of two Phi nodes. */
3524 static int node_cmp_attr_Phi(ir_node *a, ir_node *b) {
3525 return get_irn_phi_attr (a) != get_irn_phi_attr (b);
3526 } /* node_cmp_attr_Phi */
3528 /** Compares the attributes of two Conv nodes. */
3529 static int node_cmp_attr_Conv(ir_node *a, ir_node *b) {
3530 return get_Conv_strict(a) != get_Conv_strict(b);
3531 } /* node_cmp_attr_Conv */
3533 /** Compares the attributes of two Cast nodes. */
3534 static int node_cmp_attr_Cast(ir_node *a, ir_node *b) {
3535 return get_Cast_type(a) != get_Cast_type(b);
3536 } /* node_cmp_attr_Cast */
3538 /** Compares the attributes of two Load nodes. */
3539 static int node_cmp_attr_Load(ir_node *a, ir_node *b) {
3540 if (get_Load_volatility(a) == volatility_is_volatile ||
3541 get_Load_volatility(b) == volatility_is_volatile)
3542 /* NEVER do CSE on volatile Loads */
3545 return get_Load_mode(a) != get_Load_mode(b);
3546 } /* node_cmp_attr_Load */
3548 /** Compares the attributes of two Store nodes. */
3549 static int node_cmp_attr_Store(ir_node *a, ir_node *b) {
3550 /* NEVER do CSE on volatile Stores */
3551 return (get_Store_volatility(a) == volatility_is_volatile ||
3552 get_Store_volatility(b) == volatility_is_volatile);
3553 } /* node_cmp_attr_Store */
3555 /** Compares the attributes of two Confirm nodes. */
3556 static int node_cmp_attr_Confirm(ir_node *a, ir_node *b) {
3557 return (get_Confirm_cmp(a) != get_Confirm_cmp(b));
3558 } /* node_cmp_attr_Confirm */
3561 * Set the default node attribute compare operation for an ir_op_ops.
3563 * @param code the opcode for the default operation
3564 * @param ops the operations initialized
3569 static ir_op_ops *firm_set_default_node_cmp_attr(opcode code, ir_op_ops *ops)
3573 ops->node_cmp_attr = node_cmp_attr_##a; \
3597 } /* firm_set_default_node_cmp_attr */
3600 * Compare function for two nodes in the hash table. Gets two
3601 * nodes as parameters. Returns 0 if the nodes are a cse.
3603 int identities_cmp(const void *elt, const void *key)
3611 if (a == b) return 0;
3613 if ((get_irn_op(a) != get_irn_op(b)) ||
3614 (get_irn_mode(a) != get_irn_mode(b))) return 1;
3616 /* compare if a's in and b's in are of equal length */
3617 irn_arity_a = get_irn_intra_arity (a);
3618 if (irn_arity_a != get_irn_intra_arity(b))
3621 /* for block-local cse and op_pin_state_pinned nodes: */
3622 if (!get_opt_global_cse() || (get_irn_pinned(a) == op_pin_state_pinned)) {
3623 if (get_irn_intra_n(a, -1) != get_irn_intra_n(b, -1))
3627 /* compare a->in[0..ins] with b->in[0..ins] */
3628 for (i = 0; i < irn_arity_a; i++)
3629 if (get_irn_intra_n(a, i) != get_irn_intra_n(b, i))
3633 * here, we already now that the nodes are identical except their
3636 if (a->op->ops.node_cmp_attr)
3637 return a->op->ops.node_cmp_attr(a, b);
3640 } /* identities_cmp */
3643 * Calculate a hash value of a node.
3645 unsigned ir_node_hash(ir_node *node)
3650 if (node->op == op_Const) {
3651 /* special value for const, as they only differ in their tarval. */
3652 h = HASH_PTR(node->attr.con.tv);
3653 h = 9*h + HASH_PTR(get_irn_mode(node));
3654 } else if (node->op == op_SymConst) {
3655 /* special value for const, as they only differ in their symbol. */
3656 h = HASH_PTR(node->attr.symc.sym.type_p);
3657 h = 9*h + HASH_PTR(get_irn_mode(node));
3660 /* hash table value = 9*(9*(9*(9*(9*arity+in[0])+in[1])+ ...)+mode)+code */
3661 h = irn_arity = get_irn_intra_arity(node);
3663 /* consider all in nodes... except the block if not a control flow. */
3664 for (i = is_cfop(node) ? -1 : 0; i < irn_arity; i++) {
3665 h = 9*h + HASH_PTR(get_irn_intra_n(node, i));
3669 h = 9*h + HASH_PTR(get_irn_mode(node));
3671 h = 9*h + HASH_PTR(get_irn_op(node));
3675 } /* ir_node_hash */
3677 pset *new_identities(void) {
3678 return new_pset(identities_cmp, N_IR_NODES);
3679 } /* new_identities */
3681 void del_identities(pset *value_table) {
3682 del_pset(value_table);
3683 } /* del_identities */
3686 * Return the canonical node computing the same value as n.
3688 * @param value_table The value table
3689 * @param n The node to lookup
3691 * Looks up the node in a hash table.
3693 * For Const nodes this is performed in the constructor, too. Const
3694 * nodes are extremely time critical because of their frequent use in
3695 * constant string arrays.
3697 static INLINE ir_node *identify(pset *value_table, ir_node *n)
3701 if (!value_table) return n;
3703 if (get_opt_reassociation()) {
3704 if (is_op_commutative(get_irn_op(n))) {
3705 ir_node *l = get_binop_left(n);
3706 ir_node *r = get_binop_right(n);
3708 /* for commutative operators perform a OP b == b OP a */
3709 if (get_irn_idx(l) > get_irn_idx(r)) {
3710 set_binop_left(n, r);
3711 set_binop_right(n, l);
3716 o = pset_find(value_table, n, ir_node_hash(n));
3725 * During construction we set the op_pin_state_pinned flag in the graph right when the
3726 * optimization is performed. The flag turning on procedure global cse could
3727 * be changed between two allocations. This way we are safe.
3729 static INLINE ir_node *identify_cons(pset *value_table, ir_node *n) {
3732 n = identify(value_table, n);
3733 if (get_irn_n(old, -1) != get_irn_n(n, -1))
3734 set_irg_pinned(current_ir_graph, op_pin_state_floats);
3736 } /* identify_cons */
3739 * Return the canonical node computing the same value as n.
3740 * Looks up the node in a hash table, enters it in the table
3741 * if it isn't there yet.
3743 ir_node *identify_remember(pset *value_table, ir_node *n)
3747 if (!value_table) return n;
3749 if (get_opt_reassociation()) {
3750 if (is_op_commutative(get_irn_op(n))) {
3751 ir_node *l = get_binop_left(n);
3752 ir_node *r = get_binop_right(n);
3754 /* for commutative operators perform a OP b == b OP a */
3756 set_binop_left(n, r);
3757 set_binop_right(n, l);
3762 /* lookup or insert in hash table with given hash key. */
3763 o = pset_insert (value_table, n, ir_node_hash (n));
3770 } /* identify_remember */
3772 /* Add a node to the identities value table. */
3773 void add_identities(pset *value_table, ir_node *node) {
3774 if (get_opt_cse() && is_no_Block(node))
3775 identify_remember(value_table, node);
3776 } /* add_identities */
3778 /* Visit each node in the value table of a graph. */
3779 void visit_all_identities(ir_graph *irg, irg_walk_func visit, void *env) {
3781 ir_graph *rem = current_ir_graph;
3783 current_ir_graph = irg;
3784 foreach_pset(irg->value_table, node)
3786 current_ir_graph = rem;
3787 } /* visit_all_identities */
3790 * Garbage in, garbage out. If a node has a dead input, i.e., the
3791 * Bad node is input to the node, return the Bad node.
3793 static INLINE ir_node *gigo(ir_node *node)
3796 ir_op *op = get_irn_op(node);
3798 /* remove garbage blocks by looking at control flow that leaves the block
3799 and replacing the control flow by Bad. */
3800 if (get_irn_mode(node) == mode_X) {
3801 ir_node *block = get_nodes_block(skip_Proj(node));
3803 /* Don't optimize nodes in immature blocks. */
3804 if (!get_Block_matured(block)) return node;
3805 /* Don't optimize End, may have Bads. */
3806 if (op == op_End) return node;
3808 if (is_Block(block)) {
3809 irn_arity = get_irn_arity(block);
3810 for (i = 0; i < irn_arity; i++) {
3811 if (!is_Bad(get_irn_n(block, i)))
3814 if (i == irn_arity) return new_Bad();
3818 /* Blocks, Phis and Tuples may have dead inputs, e.g., if one of the
3819 blocks predecessors is dead. */
3820 if (op != op_Block && op != op_Phi && op != op_Tuple) {
3821 irn_arity = get_irn_arity(node);
3824 * Beware: we can only read the block of a non-floating node.
3826 if (is_irn_pinned_in_irg(node) &&
3827 is_Block_dead(get_nodes_block(node)))
3830 for (i = 0; i < irn_arity; i++) {
3831 ir_node *pred = get_irn_n(node, i);
3836 /* Propagating Unknowns here seems to be a bad idea, because
3837 sometimes we need a node as a input and did not want that
3839 However, it might be useful to move this into a later phase
3840 (if you think that optimizing such code is useful). */
3841 if (is_Unknown(pred) && mode_is_data(get_irn_mode(node)))
3842 return new_Unknown(get_irn_mode(node));
3847 /* With this code we violate the agreement that local_optimize
3848 only leaves Bads in Block, Phi and Tuple nodes. */
3849 /* If Block has only Bads as predecessors it's garbage. */
3850 /* If Phi has only Bads as predecessors it's garbage. */
3851 if ((op == op_Block && get_Block_matured(node)) || op == op_Phi) {
3852 irn_arity = get_irn_arity(node);
3853 for (i = 0; i < irn_arity; i++) {
3854 if (!is_Bad(get_irn_n(node, i))) break;
3856 if (i == irn_arity) node = new_Bad();
3863 * These optimizations deallocate nodes from the obstack.
3864 * It can only be called if it is guaranteed that no other nodes
3865 * reference this one, i.e., right after construction of a node.
3867 * @param n The node to optimize
3869 * current_ir_graph must be set to the graph of the node!
3871 ir_node *optimize_node(ir_node *n)
3875 opcode iro = get_irn_opcode(n);
3877 /* Always optimize Phi nodes: part of the construction. */
3878 if ((!get_opt_optimize()) && (iro != iro_Phi)) return n;
3880 /* constant expression evaluation / constant folding */
3881 if (get_opt_constant_folding()) {
3882 /* neither constants nor Tuple values can be evaluated */
3883 if (iro != iro_Const && (get_irn_mode(n) != mode_T)) {
3884 /* try to evaluate */
3885 tv = computed_value(n);
3886 if (tv != tarval_bad) {
3888 ir_type *old_tp = get_irn_type(n);
3889 int i, arity = get_irn_arity(n);
3893 * Try to recover the type of the new expression.
3895 for (i = 0; i < arity && !old_tp; ++i)
3896 old_tp = get_irn_type(get_irn_n(n, i));
3899 * we MUST copy the node here temporary, because it's still needed
3900 * for DBG_OPT_CSTEVAL
3902 node_size = offsetof(ir_node, attr) + n->op->attr_size;
3903 oldn = alloca(node_size);
3905 memcpy(oldn, n, node_size);
3906 CLONE_ARR_A(ir_node *, oldn->in, n->in);
3908 /* ARG, copy the in array, we need it for statistics */
3909 memcpy(oldn->in, n->in, ARR_LEN(n->in) * sizeof(n->in[0]));
3911 /* note the inplace edges module */
3912 edges_node_deleted(n, current_ir_graph);
3914 /* evaluation was successful -- replace the node. */
3915 irg_kill_node(current_ir_graph, n);
3916 nw = new_Const(get_tarval_mode (tv), tv);
3918 if (old_tp && get_type_mode(old_tp) == get_tarval_mode (tv))
3919 set_Const_type(nw, old_tp);
3920 DBG_OPT_CSTEVAL(oldn, nw);
3926 /* remove unnecessary nodes */
3927 if (get_opt_constant_folding() ||
3928 (iro == iro_Phi) || /* always optimize these nodes. */
3930 (iro == iro_Proj) ||
3931 (iro == iro_Block) ) /* Flags tested local. */
3932 n = equivalent_node(n);
3934 optimize_preds(n); /* do node specific optimizations of nodes predecessors. */
3936 /* Common Subexpression Elimination.
3938 * Checks whether n is already available.
3939 * The block input is used to distinguish different subexpressions. Right
3940 * now all nodes are op_pin_state_pinned to blocks, i.e., the CSE only finds common
3941 * subexpressions within a block.
3944 n = identify_cons(current_ir_graph->value_table, n);
3947 edges_node_deleted(oldn, current_ir_graph);
3949 /* We found an existing, better node, so we can deallocate the old node. */
3950 irg_kill_node(current_ir_graph, oldn);
3954 /* Some more constant expression evaluation that does not allow to
3956 iro = get_irn_opcode(n);
3957 if (get_opt_constant_folding() ||
3958 (iro == iro_Cond) ||
3959 (iro == iro_Proj)) /* Flags tested local. */
3960 n = transform_node(n);
3962 /* Remove nodes with dead (Bad) input.
3963 Run always for transformation induced Bads. */
3966 /* Now we have a legal, useful node. Enter it in hash table for CSE */
3967 if (get_opt_cse() && (get_irn_opcode(n) != iro_Block)) {
3968 n = identify_remember(current_ir_graph->value_table, n);
3972 } /* optimize_node */
3976 * These optimizations never deallocate nodes (in place). This can cause dead
3977 * nodes lying on the obstack. Remove these by a dead node elimination,
3978 * i.e., a copying garbage collection.
3980 ir_node *optimize_in_place_2(ir_node *n)
3984 opcode iro = get_irn_opcode(n);
3986 if (!get_opt_optimize() && (get_irn_op(n) != op_Phi)) return n;
3988 /* constant expression evaluation / constant folding */
3989 if (get_opt_constant_folding()) {
3990 /* neither constants nor Tuple values can be evaluated */
3991 if (iro != iro_Const && get_irn_mode(n) != mode_T) {
3992 /* try to evaluate */
3993 tv = computed_value(n);
3994 if (tv != tarval_bad) {
3995 /* evaluation was successful -- replace the node. */
3996 ir_type *old_tp = get_irn_type(n);
3997 int i, arity = get_irn_arity(n);
4000 * Try to recover the type of the new expression.
4002 for (i = 0; i < arity && !old_tp; ++i)
4003 old_tp = get_irn_type(get_irn_n(n, i));
4005 n = new_Const(get_tarval_mode(tv), tv);
4007 if (old_tp && get_type_mode(old_tp) == get_tarval_mode(tv))
4008 set_Const_type(n, old_tp);
4010 DBG_OPT_CSTEVAL(oldn, n);
4016 /* remove unnecessary nodes */
4017 if (get_opt_constant_folding() ||
4018 (iro == iro_Phi) || /* always optimize these nodes. */
4019 (iro == iro_Id) || /* ... */
4020 (iro == iro_Proj) || /* ... */
4021 (iro == iro_Block) ) /* Flags tested local. */
4022 n = equivalent_node(n);
4024 optimize_preds(n); /* do node specific optimizations of nodes predecessors. */
4026 /** common subexpression elimination **/
4027 /* Checks whether n is already available. */
4028 /* The block input is used to distinguish different subexpressions. Right
4029 now all nodes are op_pin_state_pinned to blocks, i.e., the cse only finds common
4030 subexpressions within a block. */
4031 if (get_opt_cse()) {
4032 n = identify(current_ir_graph->value_table, n);
4035 /* Some more constant expression evaluation. */
4036 iro = get_irn_opcode(n);
4037 if (get_opt_constant_folding() ||
4038 (iro == iro_Cond) ||
4039 (iro == iro_Proj)) /* Flags tested local. */
4040 n = transform_node(n);
4042 /* Remove nodes with dead (Bad) input.
4043 Run always for transformation induced Bads. */
4046 /* Now we can verify the node, as it has no dead inputs any more. */
4049 /* Now we have a legal, useful node. Enter it in hash table for cse.
4050 Blocks should be unique anyways. (Except the successor of start:
4051 is cse with the start block!) */
4052 if (get_opt_cse() && (get_irn_opcode(n) != iro_Block))
4053 n = identify_remember(current_ir_graph->value_table, n);
4056 } /* optimize_in_place_2 */
4059 * Wrapper for external use, set proper status bits after optimization.
4061 ir_node *optimize_in_place(ir_node *n) {
4062 /* Handle graph state */
4063 assert(get_irg_phase_state(current_ir_graph) != phase_building);
4065 if (get_opt_global_cse())
4066 set_irg_pinned(current_ir_graph, op_pin_state_floats);
4067 if (get_irg_outs_state(current_ir_graph) == outs_consistent)
4068 set_irg_outs_inconsistent(current_ir_graph);
4070 /* FIXME: Maybe we could also test whether optimizing the node can
4071 change the control graph. */
4072 set_irg_doms_inconsistent(current_ir_graph);
4073 return optimize_in_place_2 (n);
4074 } /* optimize_in_place */
4077 * Sets the default operation for an ir_ops.
4079 ir_op_ops *firm_set_default_operations(opcode code, ir_op_ops *ops) {
4080 ops = firm_set_default_computed_value(code, ops);
4081 ops = firm_set_default_equivalent_node(code, ops);
4082 ops = firm_set_default_transform_node(code, ops);
4083 ops = firm_set_default_node_cmp_attr(code, ops);
4084 ops = firm_set_default_get_type(code, ops);
4085 ops = firm_set_default_get_type_attr(code, ops);
4086 ops = firm_set_default_get_entity_attr(code, ops);
4089 } /* firm_set_default_operations */