2 * Copyright (C) 1995-2007 University of Karlsruhe. All right reserved.
4 * This file is part of libFirm.
6 * This file may be distributed and/or modified under the terms of the
7 * GNU General Public License version 2 as published by the Free Software
8 * Foundation and appearing in the file LICENSE.GPL included in the
9 * packaging of this file.
11 * Licensees holding valid libFirm Professional Edition licenses may use
12 * this file in accordance with the libFirm Commercial License.
13 * Agreement provided with the Software.
15 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * @brief iropt --- optimizations intertwined with IR construction.
23 * @author Christian Schaefer, Goetz Lindenmaier, Michael Beck
33 #include "irgraph_t.h"
34 #include "iredges_t.h"
41 #include "dbginfo_t.h"
42 #include "iropt_dbg.h"
48 #include "opt_confirms.h"
49 #include "opt_polymorphy.h"
53 /* Make types visible to allow most efficient access */
57 * Return the value of a Constant.
59 static tarval *computed_value_Const(ir_node *n) {
60 return get_Const_tarval(n);
61 } /* computed_value_Const */
64 * Return the value of a 'sizeof', 'alignof' or 'offsetof' SymConst.
66 static tarval *computed_value_SymConst(ir_node *n) {
70 switch (get_SymConst_kind(n)) {
71 case symconst_type_size:
72 type = get_SymConst_type(n);
73 if (get_type_state(type) == layout_fixed)
74 return new_tarval_from_long(get_type_size_bytes(type), get_irn_mode(n));
76 case symconst_type_align:
77 type = get_SymConst_type(n);
78 if (get_type_state(type) == layout_fixed)
79 return new_tarval_from_long(get_type_alignment_bytes(type), get_irn_mode(n));
81 case symconst_ofs_ent:
82 ent = get_SymConst_entity(n);
83 type = get_entity_owner(ent);
84 if (get_type_state(type) == layout_fixed)
85 return new_tarval_from_long(get_entity_offset(ent), get_irn_mode(n));
91 } /* computed_value_SymConst */
94 * Return the value of an Add.
96 static tarval *computed_value_Add(ir_node *n) {
97 ir_node *a = get_Add_left(n);
98 ir_node *b = get_Add_right(n);
100 tarval *ta = value_of(a);
101 tarval *tb = value_of(b);
103 if ((ta != tarval_bad) && (tb != tarval_bad) && (get_irn_mode(a) == get_irn_mode(b)))
104 return tarval_add(ta, tb);
107 } /* computed_value_Add */
110 * Return the value of a Sub.
111 * Special case: a - a
113 static tarval *computed_value_Sub(ir_node *n) {
114 ir_node *a = get_Sub_left(n);
115 ir_node *b = get_Sub_right(n);
120 if (a == b && !is_Bad(a))
121 return get_mode_null(get_irn_mode(n));
126 if ((ta != tarval_bad) && (tb != tarval_bad) && (get_irn_mode(a) == get_irn_mode(b)))
127 return tarval_sub(ta, tb);
130 } /* computed_value_Sub */
133 * Return the value of a Carry.
134 * Special : a op 0, 0 op b
136 static tarval *computed_value_Carry(ir_node *n) {
137 ir_node *a = get_binop_left(n);
138 ir_node *b = get_binop_right(n);
139 ir_mode *m = get_irn_mode(n);
141 tarval *ta = value_of(a);
142 tarval *tb = value_of(b);
144 if ((ta != tarval_bad) && (tb != tarval_bad)) {
146 return tarval_carry() ? get_mode_one(m) : get_mode_null(m);
148 if ( (classify_tarval(ta) == TV_CLASSIFY_NULL)
149 || (classify_tarval(tb) == TV_CLASSIFY_NULL))
150 return get_mode_null(m);
153 } /* computed_value_Carry */
156 * Return the value of a Borrow.
159 static tarval *computed_value_Borrow(ir_node *n) {
160 ir_node *a = get_binop_left(n);
161 ir_node *b = get_binop_right(n);
162 ir_mode *m = get_irn_mode(n);
164 tarval *ta = value_of(a);
165 tarval *tb = value_of(b);
167 if ((ta != tarval_bad) && (tb != tarval_bad)) {
168 return tarval_cmp(ta, tb) == pn_Cmp_Lt ? get_mode_one(m) : get_mode_null(m);
169 } else if (classify_tarval(ta) == TV_CLASSIFY_NULL) {
170 return get_mode_null(m);
173 } /* computed_value_Borrow */
176 * Return the value of an unary Minus.
178 static tarval *computed_value_Minus(ir_node *n) {
179 ir_node *a = get_Minus_op(n);
180 tarval *ta = value_of(a);
182 if ((ta != tarval_bad) && mode_is_signed(get_irn_mode(a)))
183 return tarval_neg(ta);
186 } /* computed_value_Minus */
189 * Return the value of a Mul.
191 static tarval *computed_value_Mul(ir_node *n) {
192 ir_node *a = get_Mul_left(n);
193 ir_node *b = get_Mul_right(n);
196 tarval *ta = value_of(a);
197 tarval *tb = value_of(b);
199 mode = get_irn_mode(n);
200 if (mode != get_irn_mode(a)) {
201 /* n * n = 2n bit multiplication */
202 ta = tarval_convert_to(ta, mode);
203 tb = tarval_convert_to(tb, mode);
206 if (ta != tarval_bad && tb != tarval_bad) {
207 return tarval_mul(ta, tb);
209 /* a*0 = 0 or 0*b = 0 */
210 if (ta == get_mode_null(mode))
212 if (tb == get_mode_null(mode))
216 } /* computed_value_Mul */
219 * Return the value of a floating point Quot.
221 static tarval *computed_value_Quot(ir_node *n) {
222 ir_node *a = get_Quot_left(n);
223 ir_node *b = get_Quot_right(n);
225 tarval *ta = value_of(a);
226 tarval *tb = value_of(b);
228 /* This was missing in original implementation. Why? */
229 if ((ta != tarval_bad) && (tb != tarval_bad) && (get_irn_mode(a) == get_irn_mode(b))) {
230 if (tb != get_mode_null(get_tarval_mode(tb))) /* div by zero: return tarval_bad */
231 return tarval_quo(ta, tb);
234 } /* computed_value_Quot */
237 * Calculate the value of an integer Div of two nodes.
238 * Special case: 0 / b
240 static tarval *do_computed_value_Div(ir_node *a, ir_node *b) {
241 tarval *ta = value_of(a);
242 tarval *tb = value_of(b);
244 /* Compute c1 / c2 or 0 / a, a != 0 */
245 if (ta != tarval_bad) {
246 if ((tb != tarval_bad) && (tb != get_mode_null(get_irn_mode(b)))) /* div by zero: return tarval_bad */
247 return tarval_div(ta, tb);
248 else if (ta == get_mode_null(get_tarval_mode(ta))) /* 0 / b == 0 */
252 } /* do_computed_value_Div */
255 * Return the value of an integer Div.
257 static tarval *computed_value_Div(ir_node *n) {
258 return do_computed_value_Div(get_Div_left(n), get_Div_right(n));
259 } /* computed_value_Div */
262 * Calculate the value of an integer Mod of two nodes.
263 * Special case: a % 1
265 static tarval *do_computed_value_Mod(ir_node *a, ir_node *b) {
266 tarval *ta = value_of(a);
267 tarval *tb = value_of(b);
269 /* Compute c1 % c2 or a % 1 */
270 if (tb != tarval_bad) {
271 if ((ta != tarval_bad) && (tb != get_mode_null(get_tarval_mode(tb)))) /* div by zero: return tarval_bad */
272 return tarval_mod(ta, tb);
273 else if (tb == get_mode_one(get_tarval_mode(tb))) /* x mod 1 == 0 */
274 return get_mode_null(get_irn_mode(a));
277 } /* do_computed_value_Mod */
280 * Return the value of an integer Mod.
282 static tarval *computed_value_Mod(ir_node *n) {
283 return do_computed_value_Mod(get_Mod_left(n), get_Mod_right(n));
284 } /* computed_value_Mod */
287 * Return the value of an Abs.
289 static tarval *computed_value_Abs(ir_node *n) {
290 ir_node *a = get_Abs_op(n);
291 tarval *ta = value_of(a);
293 if (ta != tarval_bad)
294 return tarval_abs(ta);
297 } /* computed_value_Abs */
300 * Return the value of an And.
301 * Special case: a & 0, 0 & b
303 static tarval *computed_value_And(ir_node *n) {
304 ir_node *a = get_And_left(n);
305 ir_node *b = get_And_right(n);
307 tarval *ta = value_of(a);
308 tarval *tb = value_of(b);
310 if ((ta != tarval_bad) && (tb != tarval_bad)) {
311 return tarval_and (ta, tb);
315 if ( (classify_tarval ((v = ta)) == TV_CLASSIFY_NULL)
316 || (classify_tarval ((v = tb)) == TV_CLASSIFY_NULL)) {
321 } /* computed_value_And */
324 * Return the value of an Or.
325 * Special case: a | 1...1, 1...1 | b
327 static tarval *computed_value_Or(ir_node *n) {
328 ir_node *a = get_Or_left(n);
329 ir_node *b = get_Or_right(n);
331 tarval *ta = value_of(a);
332 tarval *tb = value_of(b);
334 if ((ta != tarval_bad) && (tb != tarval_bad)) {
335 return tarval_or (ta, tb);
338 if ( (classify_tarval ((v = ta)) == TV_CLASSIFY_ALL_ONE)
339 || (classify_tarval ((v = tb)) == TV_CLASSIFY_ALL_ONE)) {
344 } /* computed_value_Or */
347 * Return the value of an Eor.
349 static tarval *computed_value_Eor(ir_node *n) {
350 ir_node *a = get_Eor_left(n);
351 ir_node *b = get_Eor_right(n);
356 return get_mode_null(get_irn_mode(n));
361 if ((ta != tarval_bad) && (tb != tarval_bad)) {
362 return tarval_eor (ta, tb);
365 } /* computed_value_Eor */
368 * Return the value of a Not.
370 static tarval *computed_value_Not(ir_node *n) {
371 ir_node *a = get_Not_op(n);
372 tarval *ta = value_of(a);
374 if (ta != tarval_bad)
375 return tarval_not(ta);
378 } /* computed_value_Not */
381 * Return the value of a Shl.
383 static tarval *computed_value_Shl(ir_node *n) {
384 ir_node *a = get_Shl_left(n);
385 ir_node *b = get_Shl_right(n);
387 tarval *ta = value_of(a);
388 tarval *tb = value_of(b);
390 if ((ta != tarval_bad) && (tb != tarval_bad)) {
391 return tarval_shl (ta, tb);
394 } /* computed_value_Shl */
397 * Return the value of a Shr.
399 static tarval *computed_value_Shr(ir_node *n) {
400 ir_node *a = get_Shr_left(n);
401 ir_node *b = get_Shr_right(n);
403 tarval *ta = value_of(a);
404 tarval *tb = value_of(b);
406 if ((ta != tarval_bad) && (tb != tarval_bad)) {
407 return tarval_shr (ta, tb);
410 } /* computed_value_Shr */
413 * Return the value of a Shrs.
415 static tarval *computed_value_Shrs(ir_node *n) {
416 ir_node *a = get_Shrs_left(n);
417 ir_node *b = get_Shrs_right(n);
419 tarval *ta = value_of(a);
420 tarval *tb = value_of(b);
422 if ((ta != tarval_bad) && (tb != tarval_bad)) {
423 return tarval_shrs (ta, tb);
426 } /* computed_value_Shrs */
429 * Return the value of a Rot.
431 static tarval *computed_value_Rot(ir_node *n) {
432 ir_node *a = get_Rot_left(n);
433 ir_node *b = get_Rot_right(n);
435 tarval *ta = value_of(a);
436 tarval *tb = value_of(b);
438 if ((ta != tarval_bad) && (tb != tarval_bad)) {
439 return tarval_rot (ta, tb);
442 } /* computed_value_Rot */
445 * Return the value of a Conv.
447 static tarval *computed_value_Conv(ir_node *n) {
448 ir_node *a = get_Conv_op(n);
449 tarval *ta = value_of(a);
451 if (ta != tarval_bad)
452 return tarval_convert_to(ta, get_irn_mode(n));
455 } /* computed_value_Conv */
458 * Return the value of a Proj(Cmp).
460 * This performs a first step of unreachable code elimination.
461 * Proj can not be computed, but folding a Cmp above the Proj here is
462 * not as wasteful as folding a Cmp into a Tuple of 16 Consts of which
464 * There are several case where we can evaluate a Cmp node, see later.
466 static tarval *computed_value_Proj_Cmp(ir_node *n) {
467 ir_node *a = get_Proj_pred(n);
468 ir_node *aa = get_Cmp_left(a);
469 ir_node *ab = get_Cmp_right(a);
470 long proj_nr = get_Proj_proj(n);
473 * BEWARE: a == a is NOT always True for floating Point values, as
474 * NaN != NaN is defined, so we must check this here.
477 !mode_is_float(get_irn_mode(aa)) || proj_nr == pn_Cmp_Lt || proj_nr == pn_Cmp_Gt)
480 /* This is a trick with the bits used for encoding the Cmp
481 Proj numbers, the following statement is not the same:
482 return new_tarval_from_long (proj_nr == pn_Cmp_Eq, mode_b) */
483 return new_tarval_from_long (proj_nr & pn_Cmp_Eq, mode_b);
486 tarval *taa = value_of(aa);
487 tarval *tab = value_of(ab);
488 ir_mode *mode = get_irn_mode(aa);
491 * The predecessors of Cmp are target values. We can evaluate
494 if ((taa != tarval_bad) && (tab != tarval_bad)) {
495 /* strange checks... */
496 pn_Cmp flags = tarval_cmp(taa, tab);
497 if (flags != pn_Cmp_False) {
498 return new_tarval_from_long (proj_nr & flags, mode_b);
501 /* for integer values, we can check against MIN/MAX */
502 else if (mode_is_int(mode)) {
503 /* MIN <=/> x. This results in true/false. */
504 if (taa == get_mode_min(mode)) {
505 /* a compare with the MIN value */
506 if (proj_nr == pn_Cmp_Le)
507 return get_tarval_b_true();
508 else if (proj_nr == pn_Cmp_Gt)
509 return get_tarval_b_false();
511 /* x >=/< MIN. This results in true/false. */
513 if (tab == get_mode_min(mode)) {
514 /* a compare with the MIN value */
515 if (proj_nr == pn_Cmp_Ge)
516 return get_tarval_b_true();
517 else if (proj_nr == pn_Cmp_Lt)
518 return get_tarval_b_false();
520 /* MAX >=/< x. This results in true/false. */
521 else if (taa == get_mode_max(mode)) {
522 if (proj_nr == pn_Cmp_Ge)
523 return get_tarval_b_true();
524 else if (proj_nr == pn_Cmp_Lt)
525 return get_tarval_b_false();
527 /* x <=/> MAX. This results in true/false. */
528 else if (tab == get_mode_max(mode)) {
529 if (proj_nr == pn_Cmp_Le)
530 return get_tarval_b_true();
531 else if (proj_nr == pn_Cmp_Gt)
532 return get_tarval_b_false();
536 * The predecessors are Allocs or (void*)(0) constants. Allocs never
537 * return NULL, they raise an exception. Therefore we can predict
541 ir_node *aaa = skip_Id(skip_Proj(aa));
542 ir_node *aba = skip_Id(skip_Proj(ab));
544 if ( ( (/* aa is ProjP and aaa is Alloc */
545 (get_irn_op(aa) == op_Proj)
546 && (mode_is_reference(get_irn_mode(aa)))
547 && (get_irn_op(aaa) == op_Alloc))
548 && ( (/* ab is NULL */
549 (get_irn_op(ab) == op_Const)
550 && (mode_is_reference(get_irn_mode(ab)))
551 && (get_Const_tarval(ab) == get_mode_null(get_irn_mode(ab))))
552 || (/* ab is other Alloc */
553 (get_irn_op(ab) == op_Proj)
554 && (mode_is_reference(get_irn_mode(ab)))
555 && (get_irn_op(aba) == op_Alloc)
557 || (/* aa is NULL and aba is Alloc */
558 (get_irn_op(aa) == op_Const)
559 && (mode_is_reference(get_irn_mode(aa)))
560 && (get_Const_tarval(aa) == get_mode_null(get_irn_mode(aa)))
561 && (get_irn_op(ab) == op_Proj)
562 && (mode_is_reference(get_irn_mode(ab)))
563 && (get_irn_op(aba) == op_Alloc)))
565 return new_tarval_from_long(proj_nr & pn_Cmp_Ne, mode_b);
568 return computed_value_Cmp_Confirm(a, aa, ab, proj_nr);
569 } /* computed_value_Proj_Cmp */
572 * Return the value of a Proj, handle Proj(Cmp), Proj(Div), Proj(Mod),
573 * Proj(DivMod) and Proj(Quot).
575 static tarval *computed_value_Proj(ir_node *n) {
576 ir_node *a = get_Proj_pred(n);
579 switch (get_irn_opcode(a)) {
581 return computed_value_Proj_Cmp(n);
584 /* compute either the Div or the Mod part */
585 proj_nr = get_Proj_proj(n);
586 if (proj_nr == pn_DivMod_res_div)
587 return do_computed_value_Div(get_DivMod_left(a), get_DivMod_right(a));
588 else if (proj_nr == pn_DivMod_res_mod)
589 return do_computed_value_Mod(get_DivMod_left(a), get_DivMod_right(a));
593 if (get_Proj_proj(n) == pn_Div_res)
594 return computed_value(a);
598 if (get_Proj_proj(n) == pn_Mod_res)
599 return computed_value(a);
603 if (get_Proj_proj(n) == pn_Quot_res)
604 return computed_value(a);
611 } /* computed_value_Proj */
614 * Calculate the value of a Mux: can be evaluated, if the
615 * sel and the right input are known.
617 static tarval *computed_value_Mux(ir_node *n) {
618 ir_node *sel = get_Mux_sel(n);
619 tarval *ts = value_of(sel);
621 if (ts == get_tarval_b_true()) {
622 ir_node *v = get_Mux_true(n);
625 else if (ts == get_tarval_b_false()) {
626 ir_node *v = get_Mux_false(n);
630 } /* computed_value_Mux */
633 * Calculate the value of a Psi: can be evaluated, if a condition is true
634 * and all previous conditions are false. If all conditions are false
635 * we evaluate to the default one.
637 static tarval *computed_value_Psi(ir_node *n) {
639 return computed_value_Mux(n);
641 } /* computed_value_Psi */
644 * Calculate the value of a Confirm: can be evaluated,
645 * if it has the form Confirm(x, '=', Const).
647 static tarval *computed_value_Confirm(ir_node *n) {
648 return get_Confirm_cmp(n) == pn_Cmp_Eq ?
649 value_of(get_Confirm_bound(n)) : tarval_bad;
650 } /* computed_value_Confirm */
653 * If the parameter n can be computed, return its value, else tarval_bad.
654 * Performs constant folding.
656 * @param n The node this should be evaluated
658 tarval *computed_value(ir_node *n) {
659 if (n->op->ops.computed_value)
660 return n->op->ops.computed_value(n);
662 } /* computed_value */
665 * Set the default computed_value evaluator in an ir_op_ops.
667 * @param code the opcode for the default operation
668 * @param ops the operations initialized
673 static ir_op_ops *firm_set_default_computed_value(ir_opcode code, ir_op_ops *ops)
677 ops->computed_value = computed_value_##a; \
712 } /* firm_set_default_computed_value */
715 * Returns a equivalent block for another block.
716 * If the block has only one predecessor, this is
717 * the equivalent one. If the only predecessor of a block is
718 * the block itself, this is a dead block.
720 * If both predecessors of a block are the branches of a binary
721 * Cond, the equivalent block is Cond's block.
723 * If all predecessors of a block are bad or lies in a dead
724 * block, the current block is dead as well.
726 * Note, that blocks are NEVER turned into Bad's, instead
727 * the dead_block flag is set. So, never test for is_Bad(block),
728 * always use is_dead_Block(block).
730 static ir_node *equivalent_node_Block(ir_node *n)
733 int n_preds = get_Block_n_cfgpreds(n);
735 /* The Block constructor does not call optimize, but mature_immBlock
736 calls the optimization. */
737 assert(get_Block_matured(n));
739 /* Straightening: a single entry Block following a single exit Block
740 can be merged, if it is not the Start block. */
741 /* !!! Beware, all Phi-nodes of n must have been optimized away.
742 This should be true, as the block is matured before optimize is called.
743 But what about Phi-cycles with the Phi0/Id that could not be resolved?
744 Remaining Phi nodes are just Ids. */
745 if ((n_preds == 1) && (get_irn_op(get_Block_cfgpred(n, 0)) == op_Jmp)) {
746 ir_node *predblock = get_nodes_block(get_Block_cfgpred(n, 0));
747 if (predblock == oldn) {
748 /* Jmp jumps into the block it is in -- deal self cycle. */
749 n = set_Block_dead(n);
750 DBG_OPT_DEAD_BLOCK(oldn, n);
751 } else if (get_opt_control_flow_straightening()) {
753 DBG_OPT_STG(oldn, n);
755 } else if ((n_preds == 1) &&
756 (get_irn_op(skip_Proj(get_Block_cfgpred(n, 0))) == op_Cond)) {
757 ir_node *predblock = get_Block_cfgpred_block(n, 0);
758 if (predblock == oldn) {
759 /* Jmp jumps into the block it is in -- deal self cycle. */
760 n = set_Block_dead(n);
761 DBG_OPT_DEAD_BLOCK(oldn, n);
763 } else if ((n_preds == 2) &&
764 (get_opt_control_flow_weak_simplification())) {
765 /* Test whether Cond jumps twice to this block
766 * The more general case which more than 2 predecessors is handles
767 * in optimize_cf(), we handle only this special case for speed here.
769 ir_node *a = get_Block_cfgpred(n, 0);
770 ir_node *b = get_Block_cfgpred(n, 1);
772 if ((get_irn_op(a) == op_Proj) &&
773 (get_irn_op(b) == op_Proj) &&
774 (get_Proj_pred(a) == get_Proj_pred(b)) &&
775 (get_irn_op(get_Proj_pred(a)) == op_Cond) &&
776 (get_irn_mode(get_Cond_selector(get_Proj_pred(a))) == mode_b)) {
777 /* Also a single entry Block following a single exit Block. Phis have
778 twice the same operand and will be optimized away. */
779 n = get_nodes_block(get_Proj_pred(a));
780 DBG_OPT_IFSIM1(oldn, a, b, n);
782 } else if (get_opt_unreachable_code() &&
783 (n != get_irg_start_block(current_ir_graph)) &&
784 (n != get_irg_end_block(current_ir_graph)) ) {
787 /* If all inputs are dead, this block is dead too, except if it is
788 the start or end block. This is one step of unreachable code
790 for (i = get_Block_n_cfgpreds(n) - 1; i >= 0; --i) {
791 ir_node *pred = get_Block_cfgpred(n, i);
794 if (is_Bad(pred)) continue;
795 pred_blk = get_nodes_block(skip_Proj(pred));
797 if (is_Block_dead(pred_blk)) continue;
800 /* really found a living input */
805 n = set_Block_dead(n);
806 DBG_OPT_DEAD_BLOCK(oldn, n);
811 } /* equivalent_node_Block */
814 * Returns a equivalent node for a Jmp, a Bad :-)
815 * Of course this only happens if the Block of the Jmp is dead.
817 static ir_node *equivalent_node_Jmp(ir_node *n) {
818 /* unreachable code elimination */
819 if (is_Block_dead(get_nodes_block(n)))
823 } /* equivalent_node_Jmp */
825 /** Raise is handled in the same way as Jmp. */
826 #define equivalent_node_Raise equivalent_node_Jmp
829 /* We do not evaluate Cond here as we replace it by a new node, a Jmp.
830 See transform_node_Proj_Cond(). */
833 * Optimize operations that are commutative and have neutral 0,
834 * so a op 0 = 0 op a = a.
836 static ir_node *equivalent_node_neutral_zero(ir_node *n)
840 ir_node *a = get_binop_left(n);
841 ir_node *b = get_binop_right(n);
846 /* After running compute_node there is only one constant predecessor.
847 Find this predecessors value and remember the other node: */
848 if ((tv = value_of(a)) != tarval_bad) {
850 } else if ((tv = value_of(b)) != tarval_bad) {
855 /* If this predecessors constant value is zero, the operation is
856 * unnecessary. Remove it.
858 * Beware: If n is a Add, the mode of on and n might be different
859 * which happens in this rare construction: NULL + 3.
860 * Then, a Conv would be needed which we cannot include here.
862 if (classify_tarval (tv) == TV_CLASSIFY_NULL) {
863 if (get_irn_mode(on) == get_irn_mode(n)) {
866 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_0);
871 } /* equivalent_node_neutral_zero */
874 * Eor is commutative and has neutral 0.
876 #define equivalent_node_Eor equivalent_node_neutral_zero
879 * Optimize a - 0 and (a - x) + x (for modes with wrap-around).
881 * The second one looks strange, but this construct
882 * is used heavily in the LCC sources :-).
884 * Beware: The Mode of an Add may be different than the mode of its
885 * predecessors, so we could not return a predecessors in all cases.
887 static ir_node *equivalent_node_Add(ir_node *n) {
889 ir_node *left, *right;
890 ir_mode *mode = get_irn_mode(n);
892 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
893 if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
896 n = equivalent_node_neutral_zero(n);
900 left = get_Add_left(n);
901 right = get_Add_right(n);
903 if (get_irn_op(left) == op_Sub) {
904 if (get_Sub_right(left) == right) {
907 n = get_Sub_left(left);
908 if (mode == get_irn_mode(n)) {
909 DBG_OPT_ALGSIM1(oldn, left, right, n, FS_OPT_ADD_SUB);
914 if (get_irn_op(right) == op_Sub) {
915 if (get_Sub_right(right) == left) {
918 n = get_Sub_left(right);
919 if (mode == get_irn_mode(n)) {
920 DBG_OPT_ALGSIM1(oldn, left, right, n, FS_OPT_ADD_SUB);
926 } /* equivalent_node_Add */
929 * optimize operations that are not commutative but have neutral 0 on left,
932 static ir_node *equivalent_node_left_zero(ir_node *n) {
935 ir_node *a = get_binop_left(n);
936 ir_node *b = get_binop_right(n);
938 if (classify_tarval(value_of(b)) == TV_CLASSIFY_NULL) {
941 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_0);
944 } /* equivalent_node_left_zero */
946 #define equivalent_node_Shl equivalent_node_left_zero
947 #define equivalent_node_Shr equivalent_node_left_zero
948 #define equivalent_node_Shrs equivalent_node_left_zero
949 #define equivalent_node_Rot equivalent_node_left_zero
952 * Optimize a - 0 and (a + x) - x (for modes with wrap-around).
954 * The second one looks strange, but this construct
955 * is used heavily in the LCC sources :-).
957 * Beware: The Mode of a Sub may be different than the mode of its
958 * predecessors, so we could not return a predecessors in all cases.
960 static ir_node *equivalent_node_Sub(ir_node *n) {
963 ir_mode *mode = get_irn_mode(n);
965 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
966 if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
969 b = get_Sub_right(n);
971 /* Beware: modes might be different */
972 if (classify_tarval(value_of(b)) == TV_CLASSIFY_NULL) {
973 ir_node *a = get_Sub_left(n);
974 if (mode == get_irn_mode(a)) {
977 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_0);
981 } /* equivalent_node_Sub */
985 * Optimize an "idempotent unary op", ie op(op(n)) = n.
988 * -(-a) == a, but might overflow two times.
989 * We handle it anyway here but the better way would be a
990 * flag. This would be needed for Pascal for instance.
992 static ir_node *equivalent_node_idempotent_unop(ir_node *n) {
994 ir_node *pred = get_unop_op(n);
996 /* optimize symmetric unop */
997 if (get_irn_op(pred) == get_irn_op(n)) {
998 n = get_unop_op(pred);
999 DBG_OPT_ALGSIM2(oldn, pred, n, FS_OPT_IDEM_UNARY);
1002 } /* equivalent_node_idempotent_unop */
1004 /** Optimize Not(Not(x)) == x. */
1005 #define equivalent_node_Not equivalent_node_idempotent_unop
1007 /** -(-x) == x ??? Is this possible or can --x raise an
1008 out of bounds exception if min =! max? */
1009 #define equivalent_node_Minus equivalent_node_idempotent_unop
1012 * Optimize a * 1 = 1 * a = a.
1014 static ir_node *equivalent_node_Mul(ir_node *n) {
1016 ir_node *a = get_Mul_left(n);
1018 /* we can handle here only the n * n = n bit cases */
1019 if (get_irn_mode(n) == get_irn_mode(a)) {
1020 ir_node *b = get_Mul_right(n);
1022 /* Mul is commutative and has again an other neutral element. */
1023 if (classify_tarval(value_of(a)) == TV_CLASSIFY_ONE) {
1025 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
1026 } else if (classify_tarval(value_of(b)) == TV_CLASSIFY_ONE) {
1028 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
1032 } /* equivalent_node_Mul */
1035 * Optimize a / 1 = a.
1037 static ir_node *equivalent_node_Div(ir_node *n) {
1038 ir_node *a = get_Div_left(n);
1039 ir_node *b = get_Div_right(n);
1041 /* Div is not commutative. */
1042 if (classify_tarval(value_of(b)) == TV_CLASSIFY_ONE) { /* div(x, 1) == x */
1043 /* Turn Div into a tuple (mem, bad, a) */
1044 ir_node *mem = get_Div_mem(n);
1045 ir_node *blk = get_irn_n(n, -1);
1046 turn_into_tuple(n, pn_Div_max);
1047 set_Tuple_pred(n, pn_Div_M, mem);
1048 set_Tuple_pred(n, pn_Div_X_regular, new_r_Jmp(current_ir_graph, blk));
1049 set_Tuple_pred(n, pn_Div_X_except, new_Bad()); /* no exception */
1050 set_Tuple_pred(n, pn_Div_res, a);
1053 } /* equivalent_node_Div */
1056 * Optimize a / 1.0 = a.
1058 static ir_node *equivalent_node_Quot(ir_node *n) {
1059 ir_node *a = get_Quot_left(n);
1060 ir_node *b = get_Quot_right(n);
1062 /* Div is not commutative. */
1063 if (classify_tarval(value_of(b)) == TV_CLASSIFY_ONE) { /* Quot(x, 1) == x */
1064 /* Turn Quot into a tuple (mem, jmp, bad, a) */
1065 ir_node *mem = get_Quot_mem(n);
1066 ir_node *blk = get_irn_n(n, -1);
1067 turn_into_tuple(n, pn_Quot_max);
1068 set_Tuple_pred(n, pn_Quot_M, mem);
1069 set_Tuple_pred(n, pn_Quot_X_regular, new_r_Jmp(current_ir_graph, blk));
1070 set_Tuple_pred(n, pn_Quot_X_except, new_Bad()); /* no exception */
1071 set_Tuple_pred(n, pn_Quot_res, a);
1074 } /* equivalent_node_Quot */
1077 * Optimize a / 1 = a.
1079 static ir_node *equivalent_node_DivMod(ir_node *n) {
1080 ir_node *b = get_DivMod_right(n);
1082 /* Div is not commutative. */
1083 if (classify_tarval(value_of(b)) == TV_CLASSIFY_ONE) { /* div(x, 1) == x */
1084 /* Turn DivMod into a tuple (mem, jmp, bad, a, 0) */
1085 ir_node *a = get_DivMod_left(n);
1086 ir_node *mem = get_Div_mem(n);
1087 ir_node *blk = get_irn_n(n, -1);
1088 ir_mode *mode = get_DivMod_resmode(n);
1090 turn_into_tuple(n, pn_DivMod_max);
1091 set_Tuple_pred(n, pn_DivMod_M, mem);
1092 set_Tuple_pred(n, pn_DivMod_X_regular, new_r_Jmp(current_ir_graph, blk));
1093 set_Tuple_pred(n, pn_DivMod_X_except, new_Bad()); /* no exception */
1094 set_Tuple_pred(n, pn_DivMod_res_div, a);
1095 set_Tuple_pred(n, pn_DivMod_res_mod, new_Const(mode, get_mode_null(mode)));
1098 } /* equivalent_node_DivMod */
1101 * Use algebraic simplification a | a = a | 0 = 0 | a = a.
1103 static ir_node *equivalent_node_Or(ir_node *n) {
1106 ir_node *a = get_Or_left(n);
1107 ir_node *b = get_Or_right(n);
1110 n = a; /* Or has it's own neutral element */
1111 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_OR);
1112 } else if (classify_tarval(value_of(a)) == TV_CLASSIFY_NULL) {
1114 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_OR);
1115 } else if (classify_tarval(value_of(b)) == TV_CLASSIFY_NULL) {
1117 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_OR);
1121 } /* equivalent_node_Or */
1124 * Optimize a & 0b1...1 = 0b1...1 & a = a & a = a.
1126 static ir_node *equivalent_node_And(ir_node *n) {
1129 ir_node *a = get_And_left(n);
1130 ir_node *b = get_And_right(n);
1133 n = a; /* And has it's own neutral element */
1134 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_AND);
1135 } else if (classify_tarval(value_of(a)) == TV_CLASSIFY_ALL_ONE) {
1137 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1138 } else if (classify_tarval(value_of(b)) == TV_CLASSIFY_ALL_ONE) {
1140 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1143 } /* equivalent_node_And */
1146 * Try to remove useless Conv's:
1148 static ir_node *equivalent_node_Conv(ir_node *n) {
1150 ir_node *a = get_Conv_op(n);
1153 ir_mode *n_mode = get_irn_mode(n);
1154 ir_mode *a_mode = get_irn_mode(a);
1156 if (n_mode == a_mode) { /* No Conv necessary */
1157 if (get_Conv_strict(n)) {
1158 /* special case: the predecessor might be a also a Conv */
1160 if (! get_Conv_strict(a)) {
1161 /* first one is not strict, kick it */
1162 set_Conv_op(n, get_Conv_op(a));
1165 /* else both are strict conv, second is superflous */
1167 /* leave strict floating point Conv's */
1172 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CONV);
1173 } else if (get_irn_op(a) == op_Conv) { /* Conv(Conv(b)) */
1177 n_mode = get_irn_mode(n);
1178 b_mode = get_irn_mode(b);
1180 if (n_mode == b_mode) {
1181 if (n_mode == mode_b) {
1182 n = b; /* Convb(Conv*(xxxb(...))) == xxxb(...) */
1183 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
1184 } else if (mode_is_int(n_mode)) {
1185 if (smaller_mode(b_mode, a_mode)){
1186 n = b; /* ConvS(ConvL(xxxS(...))) == xxxS(...) */
1187 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
1193 } /* equivalent_node_Conv */
1196 * A Cast may be removed if the type of the previous node
1197 * is already the type of the Cast.
1199 static ir_node *equivalent_node_Cast(ir_node *n) {
1201 ir_node *pred = get_Cast_op(n);
1203 if (get_irn_type(pred) == get_Cast_type(n)) {
1205 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CAST);
1208 } /* equivalent_node_Cast */
1211 * Several optimizations:
1212 * - no Phi in start block.
1213 * - remove Id operators that are inputs to Phi
1214 * - fold Phi-nodes, iff they have only one predecessor except
1217 static ir_node *equivalent_node_Phi(ir_node *n) {
1221 ir_node *block = NULL; /* to shutup gcc */
1222 ir_node *first_val = NULL; /* to shutup gcc */
1224 if (!get_opt_normalize()) return n;
1226 n_preds = get_Phi_n_preds(n);
1228 block = get_nodes_block(n);
1229 /* @@@ fliegt 'raus, sollte aber doch immer wahr sein!!!
1230 assert(get_irn_arity(block) == n_preds && "phi in wrong block!"); */
1231 if ((is_Block_dead(block)) || /* Control dead */
1232 (block == get_irg_start_block(current_ir_graph))) /* There should be no Phi nodes */
1233 return new_Bad(); /* in the Start Block. */
1235 if (n_preds == 0) return n; /* Phi of dead Region without predecessors. */
1237 /* If the Block has a Bad pred, we also have one. */
1238 for (i = 0; i < n_preds; ++i)
1239 if (is_Bad(get_Block_cfgpred(block, i)))
1240 set_Phi_pred(n, i, new_Bad());
1242 /* Find first non-self-referencing input */
1243 for (i = 0; i < n_preds; ++i) {
1244 first_val = get_Phi_pred(n, i);
1245 if ( (first_val != n) /* not self pointer */
1247 && (! is_Bad(first_val))
1249 ) { /* value not dead */
1250 break; /* then found first value. */
1255 /* A totally Bad or self-referencing Phi (we didn't break the above loop) */
1259 /* search for rest of inputs, determine if any of these
1260 are non-self-referencing */
1261 while (++i < n_preds) {
1262 ir_node *scnd_val = get_Phi_pred(n, i);
1263 if ( (scnd_val != n)
1264 && (scnd_val != first_val)
1266 && (! is_Bad(scnd_val))
1274 /* Fold, if no multiple distinct non-self-referencing inputs */
1276 DBG_OPT_PHI(oldn, n);
1279 } /* equivalent_node_Phi */
1282 * Several optimizations:
1283 * - no Sync in start block.
1284 * - fold Sync-nodes, iff they have only one predecessor except
1287 static ir_node *equivalent_node_Sync(ir_node *n) {
1291 ir_node *first_val = NULL; /* to shutup gcc */
1293 if (!get_opt_normalize()) return n;
1295 n_preds = get_Sync_n_preds(n);
1297 /* Find first non-self-referencing input */
1298 for (i = 0; i < n_preds; ++i) {
1299 first_val = get_Sync_pred(n, i);
1300 if ((first_val != n) /* not self pointer */ &&
1301 (! is_Bad(first_val))
1302 ) { /* value not dead */
1303 break; /* then found first value. */
1308 /* A totally Bad or self-referencing Sync (we didn't break the above loop) */
1311 /* search the rest of inputs, determine if any of these
1312 are non-self-referencing */
1313 while (++i < n_preds) {
1314 ir_node *scnd_val = get_Sync_pred(n, i);
1315 if ((scnd_val != n) &&
1316 (scnd_val != first_val) &&
1317 (! is_Bad(scnd_val))
1323 /* Fold, if no multiple distinct non-self-referencing inputs */
1325 DBG_OPT_SYNC(oldn, n);
1328 } /* equivalent_node_Sync */
1331 * Optimize Proj(Tuple) and gigo() for ProjX in Bad block,
1332 * ProjX(Load) and ProjX(Store).
1334 static ir_node *equivalent_node_Proj(ir_node *proj) {
1335 ir_node *oldn = proj;
1336 ir_node *a = get_Proj_pred(proj);
1338 if (get_irn_op(a) == op_Tuple) {
1339 /* Remove the Tuple/Proj combination. */
1340 if ( get_Proj_proj(proj) <= get_Tuple_n_preds(a) ) {
1341 proj = get_Tuple_pred(a, get_Proj_proj(proj));
1342 DBG_OPT_TUPLE(oldn, a, proj);
1344 /* This should not happen! */
1345 assert(! "found a Proj with higher number than Tuple predecessors");
1348 } else if (get_irn_mode(proj) == mode_X) {
1349 if (is_Block_dead(get_nodes_block(skip_Proj(proj)))) {
1350 /* Remove dead control flow -- early gigo(). */
1352 } else if (get_opt_ldst_only_null_ptr_exceptions()) {
1353 ir_op *op = get_irn_op(a);
1355 if (op == op_Load) {
1356 /* get the Load address */
1357 ir_node *addr = get_Load_ptr(a);
1358 ir_node *blk = get_irn_n(a, -1);
1361 if (value_not_null(addr, &confirm)) {
1362 if (confirm == NULL) {
1363 /* this node may float if it did not depend on a Confirm */
1364 set_irn_pinned(a, op_pin_state_floats);
1366 if (get_Proj_proj(proj) == pn_Load_X_except) {
1367 DBG_OPT_EXC_REM(proj);
1370 return new_r_Jmp(current_ir_graph, blk);
1372 } else if (op == op_Store) {
1373 /* get the load/store address */
1374 ir_node *addr = get_Store_ptr(a);
1375 ir_node *blk = get_irn_n(a, -1);
1378 if (value_not_null(addr, &confirm)) {
1379 if (confirm == NULL) {
1380 /* this node may float if it did not depend on a Confirm */
1381 set_irn_pinned(a, op_pin_state_floats);
1383 if (get_Proj_proj(proj) == pn_Store_X_except) {
1384 DBG_OPT_EXC_REM(proj);
1387 return new_r_Jmp(current_ir_graph, blk);
1394 } /* equivalent_node_Proj */
1399 static ir_node *equivalent_node_Id(ir_node *n) {
1404 } while (get_irn_op(n) == op_Id);
1406 DBG_OPT_ID(oldn, n);
1408 } /* equivalent_node_Id */
1413 static ir_node *equivalent_node_Mux(ir_node *n)
1415 ir_node *oldn = n, *sel = get_Mux_sel(n);
1416 tarval *ts = value_of(sel);
1418 /* Mux(true, f, t) == t */
1419 if (ts == tarval_b_true) {
1420 n = get_Mux_true(n);
1421 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_C);
1423 /* Mux(false, f, t) == f */
1424 else if (ts == tarval_b_false) {
1425 n = get_Mux_false(n);
1426 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_C);
1428 /* Mux(v, x, x) == x */
1429 else if (get_Mux_false(n) == get_Mux_true(n)) {
1430 n = get_Mux_true(n);
1431 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_EQ);
1433 else if (get_irn_op(sel) == op_Proj && !mode_honor_signed_zeros(get_irn_mode(n))) {
1434 ir_node *cmp = get_Proj_pred(sel);
1435 long proj_nr = get_Proj_proj(sel);
1436 ir_node *b = get_Mux_false(n);
1437 ir_node *a = get_Mux_true(n);
1440 * Note: normalization puts the constant on the right site,
1441 * so we check only one case.
1443 * Note further that these optimization work even for floating point
1444 * with NaN's because -NaN == NaN.
1445 * However, if +0 and -0 is handled differently, we cannot use the first one.
1447 if (get_irn_op(cmp) == op_Cmp && get_Cmp_left(cmp) == a) {
1448 if (classify_Const(get_Cmp_right(cmp)) == CNST_NULL) {
1449 /* Mux(a CMP 0, X, a) */
1450 if (get_irn_op(b) == op_Minus && get_Minus_op(b) == a) {
1451 /* Mux(a CMP 0, -a, a) */
1452 if (proj_nr == pn_Cmp_Eq) {
1453 /* Mux(a == 0, -a, a) ==> -a */
1455 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1456 } else if (proj_nr == pn_Cmp_Lg || proj_nr == pn_Cmp_Ne) {
1457 /* Mux(a != 0, -a, a) ==> a */
1459 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1461 } else if (classify_Const(b) == CNST_NULL) {
1462 /* Mux(a CMP 0, 0, a) */
1463 if (proj_nr == pn_Cmp_Lg || proj_nr == pn_Cmp_Ne) {
1464 /* Mux(a != 0, 0, a) ==> a */
1466 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1467 } else if (proj_nr == pn_Cmp_Eq) {
1468 /* Mux(a == 0, 0, a) ==> 0 */
1470 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1477 } /* equivalent_node_Mux */
1480 * Returns a equivalent node of a Psi: if a condition is true
1481 * and all previous conditions are false we know its value.
1482 * If all conditions are false its value is the default one.
1484 static ir_node *equivalent_node_Psi(ir_node *n) {
1486 return equivalent_node_Mux(n);
1488 } /* equivalent_node_Psi */
1491 * Optimize -a CMP -b into b CMP a.
1492 * This works only for for modes where unary Minus
1494 * Note that two-complement integers can Overflow
1495 * so it will NOT work.
1497 * For == and != can be handled in Proj(Cmp)
1499 static ir_node *equivalent_node_Cmp(ir_node *n) {
1500 ir_node *left = get_Cmp_left(n);
1501 ir_node *right = get_Cmp_right(n);
1503 if (get_irn_op(left) == op_Minus && get_irn_op(right) == op_Minus &&
1504 !mode_overflow_on_unary_Minus(get_irn_mode(left))) {
1505 left = get_Minus_op(left);
1506 right = get_Minus_op(right);
1507 set_Cmp_left(n, right);
1508 set_Cmp_right(n, left);
1511 } /* equivalent_node_Cmp */
1514 * Remove Confirm nodes if setting is on.
1515 * Replace Confirms(x, '=', Constlike) by Constlike.
1517 static ir_node *equivalent_node_Confirm(ir_node *n) {
1518 ir_node *pred = get_Confirm_value(n);
1519 pn_Cmp pnc = get_Confirm_cmp(n);
1521 if (get_irn_op(pred) == op_Confirm && pnc == get_Confirm_cmp(pred)) {
1523 * rare case: two identical Confirms one after another,
1524 * replace the second one with the first.
1528 if (pnc == pn_Cmp_Eq) {
1529 ir_node *bound = get_Confirm_bound(n);
1532 * Optimize a rare case:
1533 * Confirm(x, '=', Constlike) ==> Constlike
1535 if (is_irn_constlike(bound)) {
1536 DBG_OPT_CONFIRM(n, bound);
1540 return get_opt_remove_confirm() ? get_Confirm_value(n) : n;
1544 * Optimize CopyB(mem, x, x) into a Nop.
1546 static ir_node *equivalent_node_CopyB(ir_node *n) {
1547 ir_node *a = get_CopyB_dst(n);
1548 ir_node *b = get_CopyB_src(n);
1551 /* Turn CopyB into a tuple (mem, jmp, bad, bad) */
1552 ir_node *mem = get_CopyB_mem(n);
1553 ir_node *blk = get_nodes_block(n);
1554 turn_into_tuple(n, pn_CopyB_max);
1555 set_Tuple_pred(n, pn_CopyB_M, mem);
1556 set_Tuple_pred(n, pn_CopyB_X_regular, new_r_Jmp(current_ir_graph, blk));
1557 set_Tuple_pred(n, pn_CopyB_X_except, new_Bad()); /* no exception */
1558 set_Tuple_pred(n, pn_CopyB_M_except, new_Bad());
1561 } /* equivalent_node_CopyB */
1564 * Optimize Bounds(idx, idx, upper) into idx.
1566 static ir_node *equivalent_node_Bound(ir_node *n) {
1567 ir_node *idx = get_Bound_index(n);
1568 ir_node *lower = get_Bound_lower(n);
1571 /* By definition lower < upper, so if idx == lower -->
1572 lower <= idx && idx < upper */
1574 /* Turn Bound into a tuple (mem, jmp, bad, idx) */
1577 ir_node *pred = skip_Proj(idx);
1579 if (get_irn_op(pred) == op_Bound) {
1581 * idx was Bounds_check previously, it is still valid if
1582 * lower <= pred_lower && pred_upper <= upper.
1584 ir_node *upper = get_Bound_upper(n);
1585 if (get_Bound_lower(pred) == lower &&
1586 get_Bound_upper(pred) == upper) {
1588 * One could expect that we simply return the previous
1589 * Bound here. However, this would be wrong, as we could
1590 * add an exception Proj to a new location then.
1591 * So, we must turn in into a tuple.
1598 /* Turn Bound into a tuple (mem, jmp, bad, idx) */
1599 ir_node *mem = get_Bound_mem(n);
1600 ir_node *blk = get_nodes_block(n);
1601 turn_into_tuple(n, pn_Bound_max);
1602 set_Tuple_pred(n, pn_Bound_M, mem);
1603 set_Tuple_pred(n, pn_Bound_X_regular, new_r_Jmp(current_ir_graph, blk)); /* no exception */
1604 set_Tuple_pred(n, pn_Bound_X_except, new_Bad()); /* no exception */
1605 set_Tuple_pred(n, pn_Bound_res, idx);
1608 } /* equivalent_node_Bound */
1611 * equivalent_node() returns a node equivalent to input n. It skips all nodes that
1612 * perform no actual computation, as, e.g., the Id nodes. It does not create
1613 * new nodes. It is therefore safe to free n if the node returned is not n.
1614 * If a node returns a Tuple we can not just skip it. If the size of the
1615 * in array fits, we transform n into a tuple (e.g., Div).
1617 ir_node *equivalent_node(ir_node *n) {
1618 if (n->op->ops.equivalent_node)
1619 return n->op->ops.equivalent_node(n);
1621 } /* equivalent_node */
1624 * Sets the default equivalent node operation for an ir_op_ops.
1626 * @param code the opcode for the default operation
1627 * @param ops the operations initialized
1632 static ir_op_ops *firm_set_default_equivalent_node(ir_opcode code, ir_op_ops *ops)
1636 ops->equivalent_node = equivalent_node_##a; \
1676 } /* firm_set_default_equivalent_node */
1679 * Do node specific optimizations of nodes predecessors.
1681 static void optimize_preds(ir_node *n) {
1682 switch (get_irn_opcode(n)) {
1684 case iro_Cmp: { /* We don't want Cast as input to Cmp. */
1685 ir_node *a = get_Cmp_left(n), *b = get_Cmp_right(n);
1687 if (get_irn_op(a) == op_Cast) {
1691 if (get_irn_op(b) == op_Cast) {
1693 set_Cmp_right(n, b);
1700 } /* optimize_preds */
1703 * Returns non-zero if a node is a Phi node
1704 * with all predecessors constant.
1706 static int is_const_Phi(ir_node *n) {
1711 for (i = get_irn_arity(n) - 1; i >= 0; --i)
1712 if (! is_Const(get_irn_n(n, i)))
1715 } /* is_const_Phi */
1718 * Apply an evaluator on a binop with a constant operators (and one Phi).
1720 * @param phi the Phi node
1721 * @param other the other operand
1722 * @param eval an evaluator function
1723 * @param left if non-zero, other is the left operand, else the right
1725 * @return a new Phi node if the conversion was successful, NULL else
1727 static ir_node *apply_binop_on_phi(ir_node *phi, tarval *other, tarval *(*eval)(tarval *, tarval *), int left) {
1733 int i, n = get_irn_arity(phi);
1735 NEW_ARR_A(void *, res, n);
1737 for (i = 0; i < n; ++i) {
1738 pred = get_irn_n(phi, i);
1739 tv = get_Const_tarval(pred);
1740 tv = eval(other, tv);
1742 if (tv == tarval_bad) {
1743 /* folding failed, bad */
1749 for (i = 0; i < n; ++i) {
1750 pred = get_irn_n(phi, i);
1751 tv = get_Const_tarval(pred);
1752 tv = eval(tv, other);
1754 if (tv == tarval_bad) {
1755 /* folding failed, bad */
1761 mode = get_irn_mode(phi);
1762 irg = current_ir_graph;
1763 for (i = 0; i < n; ++i) {
1764 pred = get_irn_n(phi, i);
1765 res[i] = new_r_Const_type(irg, get_irg_start_block(irg),
1766 mode, res[i], get_Const_type(pred));
1768 return new_r_Phi(irg, get_nodes_block(phi), n, (ir_node **)res, mode);
1769 } /* apply_binop_on_phi */
1772 * Apply an evaluator on a unop with a constant operator (a Phi).
1774 * @param phi the Phi node
1775 * @param eval an evaluator function
1777 * @return a new Phi node if the conversion was successful, NULL else
1779 static ir_node *apply_unop_on_phi(ir_node *phi, tarval *(*eval)(tarval *)) {
1785 int i, n = get_irn_arity(phi);
1787 NEW_ARR_A(void *, res, n);
1788 for (i = 0; i < n; ++i) {
1789 pred = get_irn_n(phi, i);
1790 tv = get_Const_tarval(pred);
1793 if (tv == tarval_bad) {
1794 /* folding failed, bad */
1799 mode = get_irn_mode(phi);
1800 irg = current_ir_graph;
1801 for (i = 0; i < n; ++i) {
1802 pred = get_irn_n(phi, i);
1803 res[i] = new_r_Const_type(irg, get_irg_start_block(irg),
1804 mode, res[i], get_Const_type(pred));
1806 return new_r_Phi(irg, get_nodes_block(phi), n, (ir_node **)res, mode);
1807 } /* apply_unop_on_phi */
1810 * Transform AddP(P, ConvIs(Iu)), AddP(P, ConvIu(Is)) and
1811 * SubP(P, ConvIs(Iu)), SubP(P, ConvIu(Is)).
1812 * If possible, remove the Conv's.
1814 static ir_node *transform_node_AddSub(ir_node *n) {
1815 ir_mode *mode = get_irn_mode(n);
1817 if (mode_is_reference(mode)) {
1818 ir_node *left = get_binop_left(n);
1819 ir_node *right = get_binop_right(n);
1820 int ref_bits = get_mode_size_bits(mode);
1822 if (get_irn_op(left) == op_Conv) {
1823 ir_mode *mode = get_irn_mode(left);
1824 int bits = get_mode_size_bits(mode);
1826 if (ref_bits == bits &&
1827 mode_is_int(mode) &&
1828 get_mode_arithmetic(mode) == irma_twos_complement) {
1829 ir_node *pre = get_Conv_op(left);
1830 ir_mode *pre_mode = get_irn_mode(pre);
1832 if (mode_is_int(pre_mode) &&
1833 get_mode_size_bits(pre_mode) == bits &&
1834 get_mode_arithmetic(pre_mode) == irma_twos_complement) {
1835 /* ok, this conv just changes to sign, moreover the calculation
1836 * is done with same number of bits as our address mode, so
1837 * we can ignore the conv as address calculation can be viewed
1838 * as either signed or unsigned
1840 set_binop_left(n, pre);
1845 if (get_irn_op(right) == op_Conv) {
1846 ir_mode *mode = get_irn_mode(right);
1847 int bits = get_mode_size_bits(mode);
1849 if (ref_bits == bits &&
1850 mode_is_int(mode) &&
1851 get_mode_arithmetic(mode) == irma_twos_complement) {
1852 ir_node *pre = get_Conv_op(right);
1853 ir_mode *pre_mode = get_irn_mode(pre);
1855 if (mode_is_int(pre_mode) &&
1856 get_mode_size_bits(pre_mode) == bits &&
1857 get_mode_arithmetic(pre_mode) == irma_twos_complement) {
1858 /* ok, this conv just changes to sign, moreover the calculation
1859 * is done with same number of bits as our address mode, so
1860 * we can ignore the conv as address calculation can be viewed
1861 * as either signed or unsigned
1863 set_binop_right(n, pre);
1869 } /* transform_node_AddSub */
1871 #define HANDLE_BINOP_PHI(op,a,b,c) \
1873 if (is_Const(b) && is_const_Phi(a)) { \
1874 /* check for Op(Phi, Const) */ \
1875 c = apply_binop_on_phi(a, get_Const_tarval(b), op, 0); \
1877 else if (is_Const(a) && is_const_Phi(b)) { \
1878 /* check for Op(Const, Phi) */ \
1879 c = apply_binop_on_phi(b, get_Const_tarval(a), op, 1); \
1882 DBG_OPT_ALGSIM0(oldn, c, FS_OPT_CONST_PHI); \
1886 #define HANDLE_UNOP_PHI(op,a,c) \
1888 if (is_const_Phi(a)) { \
1889 /* check for Op(Phi) */ \
1890 c = apply_unop_on_phi(a, op); \
1892 DBG_OPT_ALGSIM0(oldn, c, FS_OPT_CONST_PHI); \
1898 * Do the AddSub optimization, then Transform
1899 * Constant folding on Phi
1900 * Add(a,a) -> Mul(a, 2)
1901 * Add(Mul(a, x), a) -> Mul(a, x+1)
1902 * if the mode is integer or float.
1903 * Transform Add(a,-b) into Sub(a,b).
1904 * Reassociation might fold this further.
1906 static ir_node *transform_node_Add(ir_node *n) {
1908 ir_node *a, *b, *c, *oldn = n;
1910 n = transform_node_AddSub(n);
1912 a = get_Add_left(n);
1913 b = get_Add_right(n);
1915 HANDLE_BINOP_PHI(tarval_add, a,b,c);
1917 mode = get_irn_mode(n);
1919 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
1920 if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
1923 if (mode_is_num(mode)) {
1924 /* the following code leads to endless recursion when Mul are replaced by a simple instruction chain */
1925 if (!get_opt_arch_dep_running() && a == b && mode_is_int(mode)) {
1926 ir_node *block = get_irn_n(n, -1);
1929 get_irn_dbg_info(n),
1933 new_r_Const_long(current_ir_graph, block, mode, 2),
1935 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_A);
1940 get_irn_dbg_info(n),
1946 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_MINUS_B);
1951 get_irn_dbg_info(n),
1957 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_MINUS_B);
1960 if (! get_opt_reassociation()) {
1961 /* do NOT execute this code if reassociation is enabled, it does the inverse! */
1963 ir_node *ma = get_Mul_left(a);
1964 ir_node *mb = get_Mul_right(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);
1979 } else if (b == mb) {
1980 ir_node *blk = get_irn_n(n, -1);
1982 get_irn_dbg_info(n), current_ir_graph, blk,
1985 get_irn_dbg_info(n), current_ir_graph, blk,
1987 new_r_Const_long(current_ir_graph, blk, mode, 1),
1990 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_MUL_A_X_A);
1995 ir_node *ma = get_Mul_left(b);
1996 ir_node *mb = get_Mul_right(b);
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);
2013 ir_node *blk = get_irn_n(n, -1);
2015 get_irn_dbg_info(n), current_ir_graph, blk,
2018 get_irn_dbg_info(n), current_ir_graph, blk,
2020 new_r_Const_long(current_ir_graph, blk, mode, 1),
2023 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_MUL_A_X_A);
2028 /* Here we rely on constants be on the RIGHT side */
2029 if (get_mode_arithmetic(mode) == irma_twos_complement &&
2030 is_Not(a) && classify_Const(b) == CNST_ONE) {
2032 ir_node *op = get_Not_op(a);
2033 ir_node *blk = get_irn_n(n, -1);
2034 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, blk, op, mode);
2035 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_PLUS_1);
2040 } /* transform_node_Add */
2043 * Do the AddSub optimization, then Transform
2044 * Constant folding on Phi
2045 * Sub(0,a) -> Minus(a)
2046 * Sub(Mul(a, x), a) -> Mul(a, x-1)
2047 * Sub(Sub(x, y), b) -> Sub(x, Add(y,b))
2048 * Sub(Add(a, x), x) -> a
2049 * Sub(x, Add(x, a)) -> -a
2050 * Sub(x, Const) -> Add(x, -Const)
2052 static ir_node *transform_node_Sub(ir_node *n) {
2057 n = transform_node_AddSub(n);
2059 a = get_Sub_left(n);
2060 b = get_Sub_right(n);
2062 mode = get_irn_mode(n);
2065 HANDLE_BINOP_PHI(tarval_sub, a,b,c);
2067 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
2068 if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
2071 /* Sub(a, Const) -> Add(a, -Const) */
2072 if (is_Const(b) && get_irn_mode(b) != mode_P) {
2073 tarval *tv = get_Const_tarval(b);
2075 tv = tarval_neg(tv);
2076 if(tv != tarval_bad) {
2077 ir_node *cnst = new_Const(get_irn_mode(b), tv);
2078 ir_node *block = get_nodes_block(n);
2079 dbg_info *dbgi = get_irn_dbg_info(n);
2080 ir_graph *irg = get_irn_irg(n);
2081 ir_node *add = new_rd_Add(dbgi, irg, block, a, cnst, mode);
2087 /* Beware of Sub(P, P) which cannot be optimized into a simple Minus ... */
2088 if (mode_is_num(mode) && mode == get_irn_mode(a) && (classify_Const(a) == CNST_NULL)) {
2090 get_irn_dbg_info(n),
2095 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_0_A);
2099 if (mode_wrap_around(mode)) {
2100 ir_node *left = get_Add_left(a);
2101 ir_node *right = get_Add_right(a);
2103 /* FIXME: Does the Conv's work only for two complement or generally? */
2105 if (mode != get_irn_mode(right)) {
2106 /* This Sub is an effective Cast */
2107 right = new_r_Conv(get_irn_irg(n), get_irn_n(n, -1), right, mode);
2110 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2112 } else if (right == b) {
2113 if (mode != get_irn_mode(left)) {
2114 /* This Sub is an effective Cast */
2115 left = new_r_Conv(get_irn_irg(n), get_irn_n(n, -1), left, mode);
2118 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2124 if (mode_wrap_around(mode)) {
2125 ir_node *left = get_Add_left(b);
2126 ir_node *right = get_Add_right(b);
2128 /* FIXME: Does the Conv's work only for two complement or generally? */
2130 ir_mode *r_mode = get_irn_mode(right);
2132 n = new_r_Minus(get_irn_irg(n), get_irn_n(n, -1), right, r_mode);
2133 if (mode != r_mode) {
2134 /* This Sub is an effective Cast */
2135 n = new_r_Conv(get_irn_irg(n), get_irn_n(n, -1), n, mode);
2137 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2139 } else if (right == a) {
2140 ir_mode *l_mode = get_irn_mode(left);
2142 n = new_r_Minus(get_irn_irg(n), get_irn_n(n, -1), left, l_mode);
2143 if (mode != l_mode) {
2144 /* This Sub is an effective Cast */
2145 n = new_r_Conv(get_irn_irg(n), get_irn_n(n, -1), n, mode);
2147 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2152 if (mode_is_int(mode) && is_Conv(a) && is_Conv(b)) {
2153 ir_mode *mode = get_irn_mode(a);
2155 if (mode == get_irn_mode(b)) {
2161 /* check if it's allowed to skip the conv */
2162 ma = get_irn_mode(a);
2163 mb = get_irn_mode(b);
2165 if (mode_is_reference(ma) && mode_is_reference(mb)) {
2166 /* SubInt(ConvInt(aP), ConvInt(bP)) -> SubInt(aP,bP) */
2168 set_Sub_right(n, b);
2174 /* do NOT execute this code if reassociation is enabled, it does the inverse! */
2175 if (get_opt_reassociation() && get_irn_op(a) == op_Mul) {
2176 ir_node *ma = get_Mul_left(a);
2177 ir_node *mb = get_Mul_right(a);
2180 ir_node *blk = get_irn_n(n, -1);
2182 get_irn_dbg_info(n),
2183 current_ir_graph, blk,
2186 get_irn_dbg_info(n),
2187 current_ir_graph, blk,
2189 new_r_Const_long(current_ir_graph, blk, mode, 1),
2192 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MUL_A_X_A);
2194 } else if (mb == b) {
2195 ir_node *blk = get_irn_n(n, -1);
2197 get_irn_dbg_info(n),
2198 current_ir_graph, blk,
2201 get_irn_dbg_info(n),
2202 current_ir_graph, blk,
2204 new_r_Const_long(current_ir_graph, blk, mode, 1),
2207 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MUL_A_X_A);
2212 ir_node *x = get_Sub_left(a);
2213 ir_node *y = get_Sub_right(a);
2214 ir_node *blk = get_irn_n(n, -1);
2215 ir_mode *m_b = get_irn_mode(b);
2216 ir_mode *m_y = get_irn_mode(y);
2219 /* Determine the right mode for the Add. */
2222 else if (mode_is_reference(m_b))
2224 else if (mode_is_reference(m_y))
2228 * Both modes are different but none is reference,
2229 * happens for instance in SubP(SubP(P, Iu), Is).
2230 * We have two possibilities here: Cast or ignore.
2231 * Currently we ignore this case.
2236 add = new_r_Add(current_ir_graph, blk, y, b, mode);
2239 set_Sub_right(n, add);
2240 DBG_OPT_ALGSIM0(n, n, FS_OPT_SUB_SUB_X_Y_Z);
2244 } /* transform_node_Sub */
2247 * Several transformation done on n*n=2n bits mul.
2248 * These transformations must be done here because new nodes may be produced.
2250 static ir_node *transform_node_Mul2n(ir_node *n, ir_mode *mode) {
2252 ir_node *a = get_Mul_left(n);
2253 ir_node *b = get_Mul_right(n);
2254 tarval *ta = value_of(a);
2255 tarval *tb = value_of(b);
2256 ir_mode *smode = get_irn_mode(a);
2258 if (ta == get_mode_one(smode)) {
2259 ir_node *blk = get_irn_n(n, -1);
2260 n = new_rd_Conv(get_irn_dbg_info(n), current_ir_graph, blk, b, mode);
2261 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
2264 else if (ta == get_mode_minus_one(smode)) {
2265 ir_node *blk = get_irn_n(n, -1);
2266 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, blk, b, smode);
2267 n = new_rd_Conv(get_irn_dbg_info(n), current_ir_graph, blk, n, mode);
2268 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2271 if (tb == get_mode_one(smode)) {
2272 ir_node *blk = get_irn_n(a, -1);
2273 n = new_rd_Conv(get_irn_dbg_info(n), current_ir_graph, blk, a, mode);
2274 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
2277 else if (tb == get_mode_minus_one(smode)) {
2278 ir_node *blk = get_irn_n(n, -1);
2279 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, blk, a, smode);
2280 n = new_rd_Conv(get_irn_dbg_info(n), current_ir_graph, blk, n, mode);
2281 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2288 * Transform Mul(a,-1) into -a.
2289 * Do constant evaluation of Phi nodes.
2290 * Do architecture dependent optimizations on Mul nodes
2292 static ir_node *transform_node_Mul(ir_node *n) {
2293 ir_node *c, *oldn = n;
2294 ir_mode *mode = get_irn_mode(n);
2295 ir_node *a = get_Mul_left(n);
2296 ir_node *b = get_Mul_right(n);
2298 if (is_Bad(a) || is_Bad(b))
2301 if (mode != get_irn_mode(a))
2302 return transform_node_Mul2n(n, mode);
2304 HANDLE_BINOP_PHI(tarval_mul, a,b,c);
2306 if (mode_is_signed(mode)) {
2309 if (value_of(a) == get_mode_minus_one(mode))
2311 else if (value_of(b) == get_mode_minus_one(mode))
2314 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1), r, mode);
2315 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2320 if (is_Const(b)) { /* -a * const -> a * -const */
2321 tarval *tv = tarval_neg(get_Const_tarval(b));
2322 dbg_info *dbgi = get_irn_dbg_info(b);
2323 ir_graph *irg = current_ir_graph;
2324 ir_node *block = get_nodes_block(b);
2325 ir_mode *mode = get_irn_mode(b);
2326 ir_node *cnst = new_rd_Const(dbgi, irg, block, mode, tv);
2327 set_Mul_left( n, get_Minus_op(a));
2328 set_Mul_right(n, cnst);
2330 } else if (is_Minus(b)) { /* -a * -b -> a * b */
2331 set_Mul_left( n, get_Minus_op(a));
2332 set_Mul_right(n, get_Minus_op(b));
2334 } else if (is_Sub(b)) { /* -a * (b - c) -> a * (c - b) */
2335 ir_node *sub_l = get_Sub_left(b);
2336 ir_node *sub_r = get_Sub_right(b);
2337 dbg_info *dbgi = get_irn_dbg_info(b);
2338 ir_graph *irg = current_ir_graph;
2339 ir_mode *mode = get_irn_mode(b);
2340 ir_node *block = get_nodes_block(b);
2341 ir_node *new_b = new_rd_Sub(dbgi, irg, block, sub_r, sub_l, mode);
2342 set_Mul_left( n, get_Minus_op(a));
2343 set_Mul_right(n, new_b);
2346 } else if (is_Minus(b)) {
2347 if (is_Sub(a)) { /* (a - b) * -c -> (b - a) * c */
2348 ir_node *sub_l = get_Sub_left(a);
2349 ir_node *sub_r = get_Sub_right(a);
2350 dbg_info *dbgi = get_irn_dbg_info(a);
2351 ir_graph *irg = current_ir_graph;
2352 ir_mode *mode = get_irn_mode(a);
2353 ir_node *block = get_nodes_block(a);
2354 ir_node *new_a = new_rd_Sub(dbgi, irg, block, sub_r, sub_l, mode);
2355 set_Mul_left (n, new_a);
2356 set_Mul_right(n, get_Minus_op(b));
2360 if (get_mode_arithmetic(mode) == irma_ieee754) {
2362 tarval *tv = get_Const_tarval(a);
2363 if (tarval_ieee754_get_exponent(tv) == 1 && tarval_ieee754_zero_mantissa(tv)) {
2364 n = new_rd_Add(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1), b, b, mode);
2365 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_A_A);
2369 else if (is_Const(b)) {
2370 tarval *tv = get_Const_tarval(b);
2371 if (tarval_ieee754_get_exponent(tv) == 1 && tarval_ieee754_zero_mantissa(tv)) {
2372 n = new_rd_Add(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1), a, a, mode);
2373 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_A_A);
2378 return arch_dep_replace_mul_with_shifts(n);
2379 } /* transform_node_Mul */
2382 * Transform a Div Node.
2384 static ir_node *transform_node_Div(ir_node *n) {
2385 tarval *tv = value_of(n);
2386 ir_mode *mode = get_Div_resmode(n);
2389 if (tv != tarval_bad) {
2390 value = new_Const(get_tarval_mode(tv), tv);
2392 DBG_OPT_CSTEVAL(n, value);
2395 ir_node *a = get_Div_left(n);
2396 ir_node *b = get_Div_right(n);
2399 if (a == b && value_not_zero(a, &dummy)) {
2400 /* BEWARE: we can optimize a/a to 1 only if this cannot cause a exception */
2401 value = new_Const(mode, get_mode_one(mode));
2402 DBG_OPT_CSTEVAL(n, value);
2405 if (mode_is_signed(mode) && is_Const(b)) {
2406 tarval *tv = get_Const_tarval(b);
2408 if (tv == get_mode_minus_one(mode)) {
2410 value = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1), a, mode);
2411 DBG_OPT_CSTEVAL(n, value);
2415 /* Try architecture dependent optimization */
2416 value = arch_dep_replace_div_by_const(n);
2424 /* Turn Div into a tuple (mem, jmp, bad, value) */
2425 mem = get_Div_mem(n);
2426 blk = get_irn_n(n, -1);
2428 /* skip a potential Pin */
2430 mem = get_Pin_op(mem);
2431 turn_into_tuple(n, pn_Div_max);
2432 set_Tuple_pred(n, pn_Div_M, mem);
2433 set_Tuple_pred(n, pn_Div_X_regular, new_r_Jmp(current_ir_graph, blk));
2434 set_Tuple_pred(n, pn_Div_X_except, new_Bad());
2435 set_Tuple_pred(n, pn_Div_res, value);
2438 } /* transform_node_Div */
2441 * Transform a Mod node.
2443 static ir_node *transform_node_Mod(ir_node *n) {
2444 tarval *tv = value_of(n);
2445 ir_mode *mode = get_Mod_resmode(n);
2448 if (tv != tarval_bad) {
2449 value = new_Const(get_tarval_mode(tv), tv);
2451 DBG_OPT_CSTEVAL(n, value);
2454 ir_node *a = get_Mod_left(n);
2455 ir_node *b = get_Mod_right(n);
2458 if (a == b && value_not_zero(a, &dummy)) {
2459 /* BEWARE: we can optimize a%a to 0 only if this cannot cause a exception */
2460 value = new_Const(mode, get_mode_null(mode));
2461 DBG_OPT_CSTEVAL(n, value);
2464 if (mode_is_signed(mode) && is_Const(b)) {
2465 tarval *tv = get_Const_tarval(b);
2467 if (tv == get_mode_minus_one(mode)) {
2469 value = new_Const(mode, get_mode_null(mode));
2470 DBG_OPT_CSTEVAL(n, value);
2474 /* Try architecture dependent optimization */
2475 value = arch_dep_replace_mod_by_const(n);
2483 /* Turn Mod into a tuple (mem, jmp, bad, value) */
2484 mem = get_Mod_mem(n);
2485 blk = get_irn_n(n, -1);
2487 /* skip a potential Pin */
2489 mem = get_Pin_op(mem);
2490 turn_into_tuple(n, pn_Mod_max);
2491 set_Tuple_pred(n, pn_Mod_M, mem);
2492 set_Tuple_pred(n, pn_Mod_X_regular, new_r_Jmp(current_ir_graph, blk));
2493 set_Tuple_pred(n, pn_Mod_X_except, new_Bad());
2494 set_Tuple_pred(n, pn_Mod_res, value);
2497 } /* transform_node_Mod */
2500 * Transform a DivMod node.
2502 static ir_node *transform_node_DivMod(ir_node *n) {
2504 ir_node *a = get_DivMod_left(n);
2505 ir_node *b = get_DivMod_right(n);
2506 ir_mode *mode = get_DivMod_resmode(n);
2507 tarval *ta = value_of(a);
2508 tarval *tb = value_of(b);
2511 if (tb != tarval_bad) {
2512 if (tb == get_mode_one(get_tarval_mode(tb))) {
2513 b = new_Const(mode, get_mode_null(mode));
2514 DBG_OPT_CSTEVAL(n, b);
2516 } else if (ta != tarval_bad) {
2517 tarval *resa, *resb;
2518 resa = tarval_div(ta, tb);
2519 if (resa == tarval_bad) return n; /* Causes exception!!! Model by replacing through
2520 Jmp for X result!? */
2521 resb = tarval_mod(ta, tb);
2522 if (resb == tarval_bad) return n; /* Causes exception! */
2523 a = new_Const(mode, resa);
2524 b = new_Const(mode, resb);
2525 DBG_OPT_CSTEVAL(n, a);
2526 DBG_OPT_CSTEVAL(n, b);
2528 } else if (mode_is_signed(mode) && tb == get_mode_minus_one(mode)) {
2529 a = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1), a, mode);
2530 b = new_Const(mode, get_mode_null(mode));
2531 DBG_OPT_CSTEVAL(n, a);
2532 DBG_OPT_CSTEVAL(n, b);
2534 } else { /* Try architecture dependent optimization */
2535 arch_dep_replace_divmod_by_const(&a, &b, n);
2536 evaluated = a != NULL;
2538 } else if (a == b) {
2539 if (value_not_zero(a, &dummy)) {
2541 a = new_Const(mode, get_mode_one(mode));
2542 b = new_Const(mode, get_mode_null(mode));
2543 DBG_OPT_CSTEVAL(n, a);
2544 DBG_OPT_CSTEVAL(n, b);
2547 /* BEWARE: it is NOT possible to optimize a/a to 1, as this may cause a exception */
2550 } else if (ta == get_mode_null(mode) && value_not_zero(b, &dummy)) {
2551 /* 0 / non-Const = 0 */
2556 if (evaluated) { /* replace by tuple */
2560 mem = get_DivMod_mem(n);
2561 /* skip a potential Pin */
2563 mem = get_Pin_op(mem);
2565 blk = get_irn_n(n, -1);
2566 turn_into_tuple(n, pn_DivMod_max);
2567 set_Tuple_pred(n, pn_DivMod_M, mem);
2568 set_Tuple_pred(n, pn_DivMod_X_regular, new_r_Jmp(current_ir_graph, blk));
2569 set_Tuple_pred(n, pn_DivMod_X_except, new_Bad()); /* no exception */
2570 set_Tuple_pred(n, pn_DivMod_res_div, a);
2571 set_Tuple_pred(n, pn_DivMod_res_mod, b);
2575 } /* transform_node_DivMod */
2578 * Optimize x / c to x * (1/c)
2580 static ir_node *transform_node_Quot(ir_node *n) {
2581 ir_mode *mode = get_Quot_resmode(n);
2584 if (get_mode_arithmetic(mode) == irma_ieee754) {
2585 ir_node *b = get_Quot_right(n);
2588 tarval *tv = get_Const_tarval(b);
2590 tv = tarval_quo(get_mode_one(mode), tv);
2592 /* Do the transformation if the result is either exact or we are not
2593 using strict rules. */
2594 if (tv != tarval_bad &&
2595 (tarval_ieee754_get_exact() || (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic) == 0)) {
2596 ir_node *blk = get_irn_n(n, -1);
2597 ir_node *c = new_r_Const(current_ir_graph, blk, mode, tv);
2598 ir_node *a = get_Quot_left(n);
2599 ir_node *m = new_rd_Mul(get_irn_dbg_info(n), current_ir_graph, blk, a, c, mode);
2600 ir_node *mem = get_Quot_mem(n);
2602 /* skip a potential Pin */
2604 mem = get_Pin_op(mem);
2605 turn_into_tuple(n, pn_Quot_max);
2606 set_Tuple_pred(n, pn_Quot_M, mem);
2607 set_Tuple_pred(n, pn_Quot_X_regular, new_r_Jmp(current_ir_graph, blk));
2608 set_Tuple_pred(n, pn_Quot_X_except, new_r_Bad(current_ir_graph));
2609 set_Tuple_pred(n, pn_Quot_res, m);
2610 DBG_OPT_ALGSIM1(oldn, a, b, m, FS_OPT_FP_INV_MUL);
2615 } /* transform_node_Quot */
2618 * Optimize Abs(x) into x if x is Confirmed >= 0
2619 * Optimize Abs(x) into -x if x is Confirmed <= 0
2621 static ir_node *transform_node_Abs(ir_node *n) {
2623 ir_node *a = get_Abs_op(n);
2624 value_classify_sign sign = classify_value_sign(a);
2626 if (sign == value_classified_negative) {
2627 ir_mode *mode = get_irn_mode(n);
2630 * We can replace the Abs by -x here.
2631 * We even could add a new Confirm here.
2633 * Note that -x would create a new node, so we could
2634 * not run it in the equivalent_node() context.
2636 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph,
2637 get_irn_n(n, -1), a, mode);
2639 DBG_OPT_CONFIRM(oldn, n);
2640 } else if (sign == value_classified_positive) {
2641 /* n is positive, Abs is not needed */
2644 DBG_OPT_CONFIRM(oldn, n);
2648 } /* transform_node_Abs */
2651 * Transform a Cond node.
2653 * Replace the Cond by a Jmp if it branches on a constant
2656 static ir_node *transform_node_Cond(ir_node *n) {
2659 ir_node *a = get_Cond_selector(n);
2660 tarval *ta = value_of(a);
2662 /* we need block info which is not available in floating irgs */
2663 if (get_irg_pinned(current_ir_graph) == op_pin_state_floats)
2666 if ((ta != tarval_bad) &&
2667 (get_irn_mode(a) == mode_b) &&
2668 (get_opt_unreachable_code())) {
2669 /* It's a boolean Cond, branching on a boolean constant.
2670 Replace it by a tuple (Bad, Jmp) or (Jmp, Bad) */
2671 jmp = new_r_Jmp(current_ir_graph, get_nodes_block(n));
2672 turn_into_tuple(n, pn_Cond_max);
2673 if (ta == tarval_b_true) {
2674 set_Tuple_pred(n, pn_Cond_false, new_Bad());
2675 set_Tuple_pred(n, pn_Cond_true, jmp);
2677 set_Tuple_pred(n, pn_Cond_false, jmp);
2678 set_Tuple_pred(n, pn_Cond_true, new_Bad());
2680 /* We might generate an endless loop, so keep it alive. */
2681 add_End_keepalive(get_irg_end(current_ir_graph), get_nodes_block(n));
2684 } /* transform_node_Cond */
2686 typedef ir_node* (*recursive_transform) (ir_node *n);
2689 * makes use of distributive laws for and, or, eor
2690 * and(a OP c, b OP c) -> and(a, b) OP c
2691 * note, might return a different op than n
2693 static ir_node *transform_bitwise_distributive(ir_node *n,
2694 recursive_transform trans_func)
2697 ir_node *a = get_binop_left(n);
2698 ir_node *b = get_binop_right(n);
2699 ir_op *op = get_irn_op(a);
2700 ir_op *op_root = get_irn_op(n);
2702 if(op != get_irn_op(b))
2705 if (op == op_Conv) {
2706 ir_node *a_op = get_Conv_op(a);
2707 ir_node *b_op = get_Conv_op(b);
2708 ir_mode *a_mode = get_irn_mode(a_op);
2709 ir_mode *b_mode = get_irn_mode(b_op);
2710 if(a_mode == b_mode && (mode_is_int(a_mode) || a_mode == mode_b)) {
2711 ir_node *blk = get_irn_n(n, -1);
2714 set_binop_left(n, a_op);
2715 set_binop_right(n, b_op);
2716 set_irn_mode(n, a_mode);
2718 n = new_r_Conv(current_ir_graph, blk, n, get_irn_mode(oldn));
2720 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_SHIFT_AND);
2726 /* nothing to gain here */
2730 if (op == op_Shrs || op == op_Shr || op == op_Shl
2731 || op == op_And || op == op_Or || op == op_Eor) {
2732 ir_node *a_left = get_binop_left(a);
2733 ir_node *a_right = get_binop_right(a);
2734 ir_node *b_left = get_binop_left(b);
2735 ir_node *b_right = get_binop_right(b);
2739 if (is_op_commutative(op)) {
2740 if (a_left == b_left) {
2744 } else if(a_left == b_right) {
2748 } else if(a_right == b_left) {
2754 if(a_right == b_right) {
2761 /* (a sop c) & (b sop c) => (a & b) sop c */
2762 ir_node *blk = get_irn_n(n, -1);
2764 ir_node *new_n = exact_copy(n);
2765 set_binop_left(new_n, op1);
2766 set_binop_right(new_n, op2);
2767 new_n = trans_func(new_n);
2769 if(op_root == op_Eor && op == op_Or) {
2770 dbg_info *dbgi = get_irn_dbg_info(n);
2771 ir_graph *irg = current_ir_graph;
2772 ir_mode *mode = get_irn_mode(c);
2774 c = new_rd_Not(dbgi, irg, blk, c, mode);
2775 n = new_rd_And(dbgi, irg, blk, new_n, c, mode);
2778 set_irn_n(n, -1, blk);
2779 set_binop_left(n, new_n);
2780 set_binop_right(n, c);
2784 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_SHIFT_AND);
2795 static ir_node *transform_node_And(ir_node *n) {
2797 ir_node *a = get_And_left(n);
2798 ir_node *b = get_And_right(n);
2800 HANDLE_BINOP_PHI(tarval_and, a,b,c);
2802 /* we can evaluate 2 Projs of the same Cmp */
2803 if(get_irn_mode(n) == mode_b && is_Proj(a) && is_Proj(b)) {
2804 ir_node *pred_a = get_Proj_pred(a);
2805 ir_node *pred_b = get_Proj_pred(b);
2806 if(pred_a == pred_b) {
2807 dbg_info *dbgi = get_irn_dbg_info(n);
2808 ir_node *block = get_nodes_block(pred_a);
2809 pn_Cmp pn_a = get_Proj_proj(a);
2810 pn_Cmp pn_b = get_Proj_proj(b);
2811 /* yes, we can simply calculate with pncs */
2812 pn_Cmp new_pnc = pn_a & pn_b;
2814 return new_rd_Proj(dbgi, current_ir_graph, block, pred_a, mode_b,
2819 n = transform_bitwise_distributive(n, transform_node_And);
2822 } /* transform_node_And */
2827 static ir_node *transform_node_Eor(ir_node *n) {
2828 ir_node *c, *oldn = n;
2829 ir_node *a = get_Eor_left(n);
2830 ir_node *b = get_Eor_right(n);
2831 ir_mode *mode = get_irn_mode(n);
2833 HANDLE_BINOP_PHI(tarval_eor, a,b,c);
2835 /* we can evaluate 2 Projs of the same Cmp */
2836 if(get_irn_mode(n) == mode_b && is_Proj(a) && is_Proj(b)) {
2837 ir_node *pred_a = get_Proj_pred(a);
2838 ir_node *pred_b = get_Proj_pred(b);
2839 if(pred_a == pred_b) {
2840 dbg_info *dbgi = get_irn_dbg_info(n);
2841 ir_node *block = get_nodes_block(pred_a);
2842 pn_Cmp pn_a = get_Proj_proj(a);
2843 pn_Cmp pn_b = get_Proj_proj(b);
2844 /* yes, we can simply calculate with pncs */
2845 pn_Cmp new_pnc = pn_a ^ pn_b;
2847 return new_rd_Proj(dbgi, current_ir_graph, block, pred_a, mode_b,
2854 n = new_rd_Const(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1),
2855 mode, get_mode_null(mode));
2856 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_A_A);
2857 } else if ((mode == mode_b)
2858 && (get_irn_op(a) == op_Proj)
2859 && (get_irn_mode(a) == mode_b)
2860 && (classify_tarval (value_of(b)) == TV_CLASSIFY_ONE)
2861 && (get_irn_op(get_Proj_pred(a)) == op_Cmp)) {
2862 /* The Eor negates a Cmp. The Cmp has the negated result anyways! */
2863 n = new_r_Proj(current_ir_graph, get_irn_n(n, -1), get_Proj_pred(a),
2864 mode_b, get_negated_pnc(get_Proj_proj(a), mode));
2866 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT_BOOL);
2867 } else if ((mode == mode_b)
2868 && (classify_tarval (value_of(b)) == TV_CLASSIFY_ONE)) {
2869 /* The Eor is a Not. Replace it by a Not. */
2870 /* ????!!!Extend to bitfield 1111111. */
2871 n = new_r_Not(current_ir_graph, get_irn_n(n, -1), a, mode_b);
2873 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
2875 n = transform_bitwise_distributive(n, transform_node_Eor);
2879 } /* transform_node_Eor */
2884 static ir_node *transform_node_Not(ir_node *n) {
2885 ir_node *c, *oldn = n;
2886 ir_node *a = get_Not_op(n);
2887 ir_op *op_a = get_irn_op(a);
2889 HANDLE_UNOP_PHI(tarval_not,a,c);
2891 /* check for a boolean Not */
2892 if ( (get_irn_mode(n) == mode_b)
2893 && (op_a == op_Proj)
2894 && (get_irn_mode(a) == mode_b)
2895 && (get_irn_op(get_Proj_pred(a)) == op_Cmp)) {
2896 /* We negate a Cmp. The Cmp has the negated result anyways! */
2897 n = new_r_Proj(current_ir_graph, get_irn_n(n, -1), get_Proj_pred(a),
2898 mode_b, get_negated_pnc(get_Proj_proj(a), mode_b));
2899 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_CMP);
2902 if (op_a == op_Sub && classify_Const(get_Sub_right(a)) == CNST_ONE) {
2904 ir_node *op = get_Sub_left(a);
2905 ir_node *blk = get_irn_n(n, -1);
2906 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, blk, op, get_irn_mode(n));
2907 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_MINUS_1);
2910 } /* transform_node_Not */
2913 * Transform a Minus.
2917 static ir_node *transform_node_Minus(ir_node *n) {
2918 ir_node *c, *oldn = n;
2919 ir_node *a = get_Minus_op(n);
2922 HANDLE_UNOP_PHI(tarval_neg,a,c);
2924 mode = get_irn_mode(a);
2925 if (get_mode_arithmetic(mode) == irma_twos_complement && is_Not(a)) {
2927 ir_node *op = get_Not_op(a);
2928 tarval *tv = get_mode_one(mode);
2929 ir_node *blk = get_irn_n(n, -1);
2930 ir_node *c = new_r_Const(current_ir_graph, blk, mode, tv);
2931 n = new_rd_Add(get_irn_dbg_info(n), current_ir_graph, blk, op, c, mode);
2932 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_MINUS_NOT);
2933 } else if (is_Sub(a)) {
2934 /* - (a-b) = b - a */
2935 ir_node *la = get_Sub_left(a);
2936 ir_node *ra = get_Sub_right(a);
2937 ir_node *blk = get_irn_n(n, -1);
2939 n = new_rd_Sub(get_irn_dbg_info(n), current_ir_graph, blk, ra, la, mode);
2940 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_MINUS_SUB);
2944 } /* transform_node_Minus */
2947 * Transform a Cast_type(Const) into a new Const_type
2949 static ir_node *transform_node_Cast(ir_node *n) {
2951 ir_node *pred = get_Cast_op(n);
2952 ir_type *tp = get_irn_type(n);
2954 if (get_irn_op(pred) == op_Const && get_Const_type(pred) != tp) {
2955 n = new_rd_Const_type(NULL, current_ir_graph, get_irn_n(pred, -1), get_irn_mode(pred),
2956 get_Const_tarval(pred), tp);
2957 DBG_OPT_CSTEVAL(oldn, n);
2958 } else if ((get_irn_op(pred) == op_SymConst) && (get_SymConst_value_type(pred) != tp)) {
2959 n = new_rd_SymConst_type(NULL, current_ir_graph, get_irn_n(pred, -1), get_SymConst_symbol(pred),
2960 get_SymConst_kind(pred), tp);
2961 DBG_OPT_CSTEVAL(oldn, n);
2965 } /* transform_node_Cast */
2968 * Transform a Proj(Div) with a non-zero value.
2969 * Removes the exceptions and routes the memory to the NoMem node.
2971 static ir_node *transform_node_Proj_Div(ir_node *proj) {
2972 ir_node *div = get_Proj_pred(proj);
2973 ir_node *b = get_Div_right(div);
2974 ir_node *confirm, *res, *new_mem;
2977 if (value_not_zero(b, &confirm)) {
2978 /* div(x, y) && y != 0 */
2979 proj_nr = get_Proj_proj(proj);
2981 case pn_Div_X_regular:
2982 return new_r_Jmp(current_ir_graph, get_irn_n(div, -1));
2984 case pn_Div_X_except:
2985 /* we found an exception handler, remove it */
2986 DBG_OPT_EXC_REM(proj);
2990 res = get_Div_mem(div);
2991 new_mem = get_irg_no_mem(current_ir_graph);
2994 /* This node can only float up to the Confirm block */
2995 new_mem = new_r_Pin(current_ir_graph, get_nodes_block(confirm), new_mem);
2997 set_irn_pinned(div, op_pin_state_floats);
2998 /* this is a Div without exception, we can remove the memory edge */
2999 set_Div_mem(div, new_mem);
3004 } /* transform_node_Proj_Div */
3007 * Transform a Proj(Mod) with a non-zero value.
3008 * Removes the exceptions and routes the memory to the NoMem node.
3010 static ir_node *transform_node_Proj_Mod(ir_node *proj) {
3011 ir_node *mod = get_Proj_pred(proj);
3012 ir_node *b = get_Mod_right(mod);
3013 ir_node *confirm, *res, *new_mem;
3016 if (value_not_zero(b, &confirm)) {
3017 /* mod(x, y) && y != 0 */
3018 proj_nr = get_Proj_proj(proj);
3022 case pn_Mod_X_regular:
3023 return new_r_Jmp(current_ir_graph, get_irn_n(mod, -1));
3025 case pn_Mod_X_except:
3026 /* we found an exception handler, remove it */
3027 DBG_OPT_EXC_REM(proj);
3031 res = get_Mod_mem(mod);
3032 new_mem = get_irg_no_mem(current_ir_graph);
3035 /* This node can only float up to the Confirm block */
3036 new_mem = new_r_Pin(current_ir_graph, get_nodes_block(confirm), new_mem);
3038 set_irn_pinned(mod, op_pin_state_floats);
3039 /* this is a Mod without exception, we can remove the memory edge */
3040 set_Mod_mem(mod, get_irg_no_mem(current_ir_graph));
3043 if (get_Mod_left(mod) == b) {
3044 /* a % a = 0 if a != 0 */
3045 ir_mode *mode = get_irn_mode(proj);
3046 ir_node *res = new_Const(mode, get_mode_null(mode));
3048 DBG_OPT_CSTEVAL(mod, res);
3054 } /* transform_node_Proj_Mod */
3057 * Transform a Proj(DivMod) with a non-zero value.
3058 * Removes the exceptions and routes the memory to the NoMem node.
3060 static ir_node *transform_node_Proj_DivMod(ir_node *proj) {
3061 ir_node *divmod = get_Proj_pred(proj);
3062 ir_node *b = get_DivMod_right(divmod);
3063 ir_node *confirm, *res, *new_mem;
3066 if (value_not_zero(b, &confirm)) {
3067 /* DivMod(x, y) && y != 0 */
3068 proj_nr = get_Proj_proj(proj);
3072 case pn_DivMod_X_regular:
3073 return new_r_Jmp(current_ir_graph, get_irn_n(divmod, -1));
3075 case pn_DivMod_X_except:
3076 /* we found an exception handler, remove it */
3077 DBG_OPT_EXC_REM(proj);
3081 res = get_DivMod_mem(divmod);
3082 new_mem = get_irg_no_mem(current_ir_graph);
3085 /* This node can only float up to the Confirm block */
3086 new_mem = new_r_Pin(current_ir_graph, get_nodes_block(confirm), new_mem);
3088 set_irn_pinned(divmod, op_pin_state_floats);
3089 /* this is a DivMod without exception, we can remove the memory edge */
3090 set_DivMod_mem(divmod, get_irg_no_mem(current_ir_graph));
3093 case pn_DivMod_res_mod:
3094 if (get_DivMod_left(divmod) == b) {
3095 /* a % a = 0 if a != 0 */
3096 ir_mode *mode = get_irn_mode(proj);
3097 ir_node *res = new_Const(mode, get_mode_null(mode));
3099 DBG_OPT_CSTEVAL(divmod, res);
3105 } /* transform_node_Proj_DivMod */
3108 * Optimizes jump tables (CondIs or CondIu) by removing all impossible cases.
3110 static ir_node *transform_node_Proj_Cond(ir_node *proj) {
3111 if (get_opt_unreachable_code()) {
3112 ir_node *n = get_Proj_pred(proj);
3113 ir_node *b = get_Cond_selector(n);
3115 if (mode_is_int(get_irn_mode(b))) {
3116 tarval *tb = value_of(b);
3118 if (tb != tarval_bad) {
3119 /* we have a constant switch */
3120 long num = get_Proj_proj(proj);
3122 if (num != get_Cond_defaultProj(n)) { /* we cannot optimize default Proj's yet */
3123 if (get_tarval_long(tb) == num) {
3124 /* Do NOT create a jump here, or we will have 2 control flow ops
3125 * in a block. This case is optimized away in optimize_cf(). */
3128 /* this case will NEVER be taken, kill it */
3136 } /* transform_node_Proj_Cond */
3139 * Normalizes and optimizes Cmp nodes.
3141 static ir_node *transform_node_Proj_Cmp(ir_node *proj) {
3142 ir_node *n = get_Proj_pred(proj);
3143 ir_node *left = get_Cmp_left(n);
3144 ir_node *right = get_Cmp_right(n);
3148 ir_mode *mode = NULL;
3149 long proj_nr = get_Proj_proj(proj);
3151 /* we can evaluate this direct */
3154 return new_Const(mode_b, get_tarval_b_false());
3156 return new_Const(mode_b, get_tarval_b_true());
3158 if(!mode_is_float(get_irn_mode(left)))
3159 return new_Const(mode_b, get_tarval_b_true());
3165 /* Remove unnecessary conversions */
3166 /* TODO handle constants */
3167 if (is_Conv(left) && is_Conv(right)) {
3168 ir_mode* mode = get_irn_mode(left);
3169 ir_node* op_left = get_Conv_op(left);
3170 ir_node* op_right = get_Conv_op(right);
3171 ir_mode* mode_left = get_irn_mode(op_left);
3172 ir_mode* mode_right = get_irn_mode(op_right);
3174 if (smaller_mode(mode_left, mode) && smaller_mode(mode_right, mode)) {
3175 ir_graph* irg = current_ir_graph;
3176 ir_node* block = get_nodes_block(n);
3180 if (mode_left == mode_right) {
3182 new_right = op_right;
3183 } else if (smaller_mode(mode_left, mode_right)) {
3184 new_left = new_r_Conv(irg, block, op_left, mode_right);
3185 new_right = op_right;
3186 } else if (smaller_mode(mode_right, mode_left)) {
3188 new_right = new_r_Conv(irg, block, op_right, mode_left);
3190 goto no_remove_conv;
3194 set_Cmp_left( n, left);
3195 set_Cmp_right(n, right);
3200 if (get_irn_mode(left) == mode_b) {
3201 ir_graph* irg = current_ir_graph;
3202 ir_node* block = get_nodes_block(n);
3205 case pn_Cmp_Le: return new_r_Or( irg, block, new_r_Not(irg, block, left, mode_b), right, mode_b);
3206 case pn_Cmp_Lt: return new_r_And(irg, block, new_r_Not(irg, block, left, mode_b), right, mode_b);
3207 case pn_Cmp_Ge: return new_r_Or( irg, block, left, new_r_Not(irg, block, right, mode_b), mode_b);
3208 case pn_Cmp_Gt: return new_r_And(irg, block, left, new_r_Not(irg, block, right, mode_b), mode_b);
3209 case pn_Cmp_Lg: return new_r_Eor(irg, block, left, right, mode_b);
3210 case pn_Cmp_Eq: return new_r_Not(irg, block, new_r_Eor(irg, block, left, right, mode_b), mode_b);
3214 if (!get_opt_reassociation())
3218 * First step: normalize the compare op
3219 * by placing the constant on the right site
3220 * or moving the lower address node to the left.
3221 * We ignore the case that both are constants
3222 * this case should be optimized away.
3224 if (get_irn_op(right) == op_Const) {
3226 } else if (get_irn_op(left) == op_Const) {
3231 proj_nr = get_inversed_pnc(proj_nr);
3233 } else if (get_irn_idx(left) > get_irn_idx(right)) {
3239 proj_nr = get_inversed_pnc(proj_nr);
3244 * Second step: Try to reduce the magnitude
3245 * of a constant. This may help to generate better code
3246 * later and may help to normalize more compares.
3247 * Of course this is only possible for integer values.
3250 mode = get_irn_mode(c);
3251 tv = get_Const_tarval(c);
3253 if (tv != tarval_bad) {
3254 /* the following optimization is possible on modes without Overflow
3255 * on Unary Minus or on == and !=:
3256 * -a CMP c ==> a swap(CMP) -c
3258 * Beware: for two-complement Overflow may occur, so only == and != can
3259 * be optimized, see this:
3260 * -MININT < 0 =/=> MININT > 0 !!!
3262 if (get_opt_constant_folding() && get_irn_op(left) == op_Minus &&
3263 (!mode_overflow_on_unary_Minus(mode) ||
3264 (mode_is_int(mode) && (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg)))) {
3265 left = get_Minus_op(left);
3266 tv = tarval_neg(tv);
3268 if (tv != tarval_bad) {
3269 proj_nr = get_inversed_pnc(proj_nr);
3274 /* for integer modes, we have more */
3275 if (mode_is_int(mode)) {
3276 /* Ne includes Unordered which is not possible on integers.
3277 * However, frontends often use this wrong, so fix it here */
3278 if (proj_nr & pn_Cmp_Uo) {
3279 proj_nr &= ~pn_Cmp_Uo;
3280 set_Proj_proj(proj, proj_nr);
3283 /* c > 0 : a < c ==> a <= (c-1) a >= c ==> a > (c-1) */
3284 if ((proj_nr == pn_Cmp_Lt || proj_nr == pn_Cmp_Ge) &&
3285 tarval_cmp(tv, get_mode_null(mode)) == pn_Cmp_Gt) {
3286 tv = tarval_sub(tv, get_mode_one(mode));
3288 if (tv != tarval_bad) {
3289 proj_nr ^= pn_Cmp_Eq;
3293 /* c < 0 : a > c ==> a >= (c+1) a <= c ==> a < (c+1) */
3294 else if ((proj_nr == pn_Cmp_Gt || proj_nr == pn_Cmp_Le) &&
3295 tarval_cmp(tv, get_mode_null(mode)) == pn_Cmp_Lt) {
3296 tv = tarval_add(tv, get_mode_one(mode));
3298 if (tv != tarval_bad) {
3299 proj_nr ^= pn_Cmp_Eq;
3304 /* the following reassociations work only for == and != */
3305 if (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) {
3307 /* a-b == 0 ==> a == b, a-b != 0 ==> a != b */
3308 if (classify_tarval(tv) == TV_CLASSIFY_NULL && get_irn_op(left) == op_Sub) {
3309 right = get_Sub_right(left);
3310 left = get_Sub_left(left);
3312 tv = value_of(right);
3313 if (tv != tarval_bad) {
3318 if (tv != tarval_bad) {
3319 ir_op *op = get_irn_op(left);
3321 /* a-c1 == c2 ==> a == c2+c1, a-c1 != c2 ==> a != c2+c1 */
3323 ir_node *c1 = get_Sub_right(left);
3324 tarval *tv2 = value_of(c1);
3326 if (tv2 != tarval_bad) {
3327 tv2 = tarval_add(tv, value_of(c1));
3329 if (tv2 != tarval_bad) {
3330 left = get_Sub_left(left);
3336 /* a+c1 == c2 ==> a == c2-c1, a+c1 != c2 ==> a != c2-c1 */
3337 else if (op == op_Add) {
3338 ir_node *a_l = get_Add_left(left);
3339 ir_node *a_r = get_Add_right(left);
3343 if (get_irn_op(a_l) == op_Const) {
3345 tv2 = value_of(a_l);
3348 tv2 = value_of(a_r);
3351 if (tv2 != tarval_bad) {
3352 tv2 = tarval_sub(tv, tv2);
3354 if (tv2 != tarval_bad) {
3361 /* -a == c ==> a == -c, -a != c ==> a != -c */
3362 else if (op == op_Minus) {
3363 tarval *tv2 = tarval_sub(get_mode_null(mode), tv);
3365 if (tv2 != tarval_bad) {
3366 left = get_Minus_op(left);
3373 /* the following reassociations work only for <= */
3374 else if (proj_nr == pn_Cmp_Le || proj_nr == pn_Cmp_Lt) {
3375 if (tv != tarval_bad) {
3376 ir_op *op = get_irn_op(left);
3378 /* c >= 0 : Abs(a) <= c ==> (unsigned)(a + c) <= 2*c */
3386 * optimization for AND:
3388 * And(x, C) == C ==> And(x, C) != 0
3389 * And(x, C) != C ==> And(X, C) == 0
3391 * if C is a single Bit constant.
3393 if ((proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) &&
3394 (get_irn_op(left) == op_And)) {
3395 if (tarval_is_single_bit(tv)) {
3396 /* check for Constant's match. We have check hare the tarvals,
3397 because our const might be changed */
3398 ir_node *la = get_And_left(left);
3399 ir_node *ra = get_And_right(left);
3400 if ((is_Const(la) && get_Const_tarval(la) == tv) ||
3401 (is_Const(ra) && get_Const_tarval(ra) == tv)) {
3402 /* fine: do the transformation */
3403 tv = get_mode_null(get_tarval_mode(tv));
3404 proj_nr ^= pn_Cmp_Leg;
3409 } /* tarval != bad */
3413 ir_node *block = get_irn_n(n, -1); /* Beware of get_nodes_Block() */
3415 if (changed & 2) /* need a new Const */
3416 right = new_Const(mode, tv);
3418 /* create a new compare */
3419 n = new_rd_Cmp(get_irn_dbg_info(n), current_ir_graph, block,
3422 set_Proj_pred(proj, n);
3423 set_Proj_proj(proj, proj_nr);
3427 } /* transform_node_Proj_Cmp */
3430 * Does all optimizations on nodes that must be done on it's Proj's
3431 * because of creating new nodes.
3433 static ir_node *transform_node_Proj(ir_node *proj) {
3434 ir_node *n = get_Proj_pred(proj);
3436 switch (get_irn_opcode(n)) {
3438 return transform_node_Proj_Div(proj);
3441 return transform_node_Proj_Mod(proj);
3444 return transform_node_Proj_DivMod(proj);
3447 return transform_node_Proj_Cond(proj);
3450 return transform_node_Proj_Cmp(proj);
3453 /* should not happen, but if it does will be optimized away */
3454 return equivalent_node_Proj(proj);
3460 } /* transform_node_Proj */
3463 * Move Confirms down through Phi nodes.
3465 static ir_node *transform_node_Phi(ir_node *phi) {
3467 ir_mode *mode = get_irn_mode(phi);
3469 if (mode_is_reference(mode)) {
3470 n = get_irn_arity(phi);
3472 /* Beware of Phi0 */
3474 ir_node *pred = get_irn_n(phi, 0);
3475 ir_node *bound, *new_Phi, *block, **in;
3478 if (! is_Confirm(pred))
3481 bound = get_Confirm_bound(pred);
3482 pnc = get_Confirm_cmp(pred);
3484 NEW_ARR_A(ir_node *, in, n);
3485 in[0] = get_Confirm_value(pred);
3487 for (i = 1; i < n; ++i) {
3488 pred = get_irn_n(phi, i);
3490 if (! is_Confirm(pred) ||
3491 get_Confirm_bound(pred) != bound ||
3492 get_Confirm_cmp(pred) != pnc)
3494 in[i] = get_Confirm_value(pred);
3496 /* move the Confirm nodes "behind" the Phi */
3497 block = get_irn_n(phi, -1);
3498 new_Phi = new_r_Phi(current_ir_graph, block, n, in, get_irn_mode(phi));
3499 return new_r_Confirm(current_ir_graph, block, new_Phi, bound, pnc);
3503 } /* transform_node_Phi */
3506 * Returns the operands of a commutative bin-op, if one operand is
3507 * a const, it is returned as the second one.
3509 static void get_comm_Binop_Ops(ir_node *binop, ir_node **a, ir_node **c) {
3510 ir_node *op_a = get_binop_left(binop);
3511 ir_node *op_b = get_binop_right(binop);
3513 assert(is_op_commutative(get_irn_op(binop)));
3515 if (get_irn_op(op_a) == op_Const) {
3522 } /* get_comm_Binop_Ops */
3525 * Optimize a Or(And(Or(And(v,c4),c3),c2),c1) pattern if possible.
3526 * Such pattern may arise in bitfield stores.
3528 * value c4 value c4 & c2
3529 * AND c3 AND c1 | c3
3536 * AND c1 ===> OR if (c1 | c2) == 0x111..11
3539 static ir_node *transform_node_Or_bf_store(ir_node *or) {
3542 ir_node *and_l, *c3;
3543 ir_node *value, *c4;
3544 ir_node *new_and, *new_const, *block;
3545 ir_mode *mode = get_irn_mode(or);
3547 tarval *tv1, *tv2, *tv3, *tv4, *tv, *n_tv4, *n_tv2;
3550 get_comm_Binop_Ops(or, &and, &c1);
3551 if (!is_Const(c1) || !is_And(and))
3554 get_comm_Binop_Ops(and, &or_l, &c2);
3558 tv1 = get_Const_tarval(c1);
3559 tv2 = get_Const_tarval(c2);
3561 tv = tarval_or(tv1, tv2);
3562 if (classify_tarval(tv) == TV_CLASSIFY_ALL_ONE) {
3563 /* the AND does NOT clear a bit with isn't set be the OR */
3564 set_Or_left(or, or_l);
3565 set_Or_right(or, c1);
3567 /* check for more */
3574 get_comm_Binop_Ops(or_l, &and_l, &c3);
3575 if (!is_Const(c3) || !is_And(and_l))
3578 get_comm_Binop_Ops(and_l, &value, &c4);
3582 /* ok, found the pattern, check for conditions */
3583 assert(mode == get_irn_mode(and));
3584 assert(mode == get_irn_mode(or_l));
3585 assert(mode == get_irn_mode(and_l));
3587 tv3 = get_Const_tarval(c3);
3588 tv4 = get_Const_tarval(c4);
3590 tv = tarval_or(tv4, tv2);
3591 if (classify_tarval(tv) != TV_CLASSIFY_ALL_ONE) {
3592 /* have at least one 0 at the same bit position */
3596 n_tv4 = tarval_not(tv4);
3597 if (tv3 != tarval_and(tv3, n_tv4)) {
3598 /* bit in the or_mask is outside the and_mask */
3602 n_tv2 = tarval_not(tv2);
3603 if (tv1 != tarval_and(tv1, n_tv2)) {
3604 /* bit in the or_mask is outside the and_mask */
3608 /* ok, all conditions met */
3609 block = get_irn_n(or, -1);
3611 new_and = new_r_And(current_ir_graph, block,
3612 value, new_r_Const(current_ir_graph, block, mode, tarval_and(tv4, tv2)), mode);
3614 new_const = new_r_Const(current_ir_graph, block, mode, tarval_or(tv3, tv1));
3616 set_Or_left(or, new_and);
3617 set_Or_right(or, new_const);
3619 /* check for more */
3621 } /* transform_node_Or_bf_store */
3624 * Optimize an Or(shl(x, c), shr(x, bits - c)) into a Rot
3626 static ir_node *transform_node_Or_Rot(ir_node *or) {
3627 ir_mode *mode = get_irn_mode(or);
3628 ir_node *shl, *shr, *block;
3629 ir_node *irn, *x, *c1, *c2, *v, *sub, *n;
3632 if (! mode_is_int(mode))
3635 shl = get_binop_left(or);
3636 shr = get_binop_right(or);
3638 if (get_irn_op(shl) == op_Shr) {
3639 if (get_irn_op(shr) != op_Shl)
3645 } else if (get_irn_op(shl) != op_Shl) {
3647 } else if (get_irn_op(shr) != op_Shr) {
3650 x = get_Shl_left(shl);
3651 if (x != get_Shr_left(shr))
3654 c1 = get_Shl_right(shl);
3655 c2 = get_Shr_right(shr);
3656 if (get_irn_op(c1) == op_Const && get_irn_op(c2) == op_Const) {
3657 tv1 = get_Const_tarval(c1);
3658 if (! tarval_is_long(tv1))
3661 tv2 = get_Const_tarval(c2);
3662 if (! tarval_is_long(tv2))
3665 if (get_tarval_long(tv1) + get_tarval_long(tv2)
3666 != get_mode_size_bits(mode))
3669 /* yet, condition met */
3670 block = get_irn_n(or, -1);
3672 n = new_r_Rot(current_ir_graph, block, x, c1, mode);
3674 DBG_OPT_ALGSIM1(or, shl, shr, n, FS_OPT_OR_SHFT_TO_ROT);
3676 } else if (get_irn_op(c1) == op_Sub) {
3680 if (get_Sub_right(sub) != v)
3683 c1 = get_Sub_left(sub);
3684 if (get_irn_op(c1) != op_Const)
3687 tv1 = get_Const_tarval(c1);
3688 if (! tarval_is_long(tv1))
3691 if (get_tarval_long(tv1) != get_mode_size_bits(mode))
3694 /* yet, condition met */
3695 block = get_nodes_block(or);
3697 /* a Rot right is not supported, so use a rot left */
3698 n = new_r_Rot(current_ir_graph, block, x, sub, mode);
3700 DBG_OPT_ALGSIM0(or, n, FS_OPT_OR_SHFT_TO_ROT);
3702 } else if (get_irn_op(c2) == op_Sub) {
3706 c1 = get_Sub_left(sub);
3707 if (get_irn_op(c1) != op_Const)
3710 tv1 = get_Const_tarval(c1);
3711 if (! tarval_is_long(tv1))
3714 if (get_tarval_long(tv1) != get_mode_size_bits(mode))
3717 /* yet, condition met */
3718 block = get_irn_n(or, -1);
3721 n = new_r_Rot(current_ir_graph, block, x, v, mode);
3723 DBG_OPT_ALGSIM0(or, n, FS_OPT_OR_SHFT_TO_ROT);
3728 } /* transform_node_Or_Rot */
3733 static ir_node *transform_node_Or(ir_node *n) {
3734 ir_node *c, *oldn = n;
3735 ir_node *a = get_Or_left(n);
3736 ir_node *b = get_Or_right(n);
3738 /* we can evaluate 2 Projs of the same Cmp */
3739 if(get_irn_mode(n) == mode_b && is_Proj(a) && is_Proj(b)) {
3740 ir_node *pred_a = get_Proj_pred(a);
3741 ir_node *pred_b = get_Proj_pred(b);
3742 if(pred_a == pred_b) {
3743 dbg_info *dbgi = get_irn_dbg_info(n);
3744 ir_node *block = get_nodes_block(pred_a);
3745 pn_Cmp pn_a = get_Proj_proj(a);
3746 pn_Cmp pn_b = get_Proj_proj(b);
3747 /* yes, we can simply calculate with pncs */
3748 pn_Cmp new_pnc = pn_a | pn_b;
3750 return new_rd_Proj(dbgi, current_ir_graph, block, pred_a, mode_b,
3755 HANDLE_BINOP_PHI(tarval_or, a,b,c);
3757 n = transform_node_Or_bf_store(n);
3758 n = transform_node_Or_Rot(n);
3762 n = transform_bitwise_distributive(n, transform_node_Or);
3765 } /* transform_node_Or */
3769 static ir_node *transform_node(ir_node *n);
3772 * Optimize (a >> c1) >> c2), works for Shr, Shrs, Shl.
3774 * Should be moved to reassociation?
3776 static ir_node *transform_node_shift(ir_node *n) {
3777 ir_node *left, *right;
3778 tarval *tv1, *tv2, *res;
3780 int modulo_shf, flag;
3782 left = get_binop_left(n);
3784 /* different operations */
3785 if (get_irn_op(left) != get_irn_op(n))
3788 right = get_binop_right(n);
3789 tv1 = value_of(right);
3790 if (tv1 == tarval_bad)
3793 tv2 = value_of(get_binop_right(left));
3794 if (tv2 == tarval_bad)
3797 res = tarval_add(tv1, tv2);
3799 /* beware: a simple replacement works only, if res < modulo shift */
3800 mode = get_irn_mode(n);
3804 modulo_shf = get_mode_modulo_shift(mode);
3805 if (modulo_shf > 0) {
3806 tarval *modulo = new_tarval_from_long(modulo_shf, get_tarval_mode(res));
3808 if (tarval_cmp(res, modulo) & pn_Cmp_Lt)
3814 /* ok, we can replace it */
3815 ir_node *in[2], *irn, *block = get_irn_n(n, -1);
3817 in[0] = get_binop_left(left);
3818 in[1] = new_r_Const(current_ir_graph, block, get_tarval_mode(res), res);
3820 irn = new_ir_node(NULL, current_ir_graph, block, get_irn_op(n), mode, 2, in);
3822 DBG_OPT_ALGSIM0(n, irn, FS_OPT_REASSOC_SHIFT);
3824 return transform_node(irn);
3827 } /* transform_node_shift */
3832 static ir_node *transform_node_Shr(ir_node *n) {
3833 ir_node *c, *oldn = n;
3834 ir_node *a = get_Shr_left(n);
3835 ir_node *b = get_Shr_right(n);
3837 HANDLE_BINOP_PHI(tarval_shr, a, b, c);
3838 return transform_node_shift(n);
3839 } /* transform_node_Shr */
3844 static ir_node *transform_node_Shrs(ir_node *n) {
3845 ir_node *c, *oldn = n;
3846 ir_node *a = get_Shrs_left(n);
3847 ir_node *b = get_Shrs_right(n);
3849 HANDLE_BINOP_PHI(tarval_shrs, a, b, c);
3850 return transform_node_shift(n);
3851 } /* transform_node_Shrs */
3856 static ir_node *transform_node_Shl(ir_node *n) {
3857 ir_node *c, *oldn = n;
3858 ir_node *a = get_Shl_left(n);
3859 ir_node *b = get_Shl_right(n);
3861 HANDLE_BINOP_PHI(tarval_shl, a, b, c);
3862 return transform_node_shift(n);
3863 } /* transform_node_Shl */
3866 * Remove dead blocks and nodes in dead blocks
3867 * in keep alive list. We do not generate a new End node.
3869 static ir_node *transform_node_End(ir_node *n) {
3870 int i, j, n_keepalives = get_End_n_keepalives(n);
3873 NEW_ARR_A(ir_node *, in, n_keepalives);
3875 for (i = j = 0; i < n_keepalives; ++i) {
3876 ir_node *ka = get_End_keepalive(n, i);
3878 if (! is_Block_dead(ka)) {
3882 } else if (is_irn_pinned_in_irg(ka) && is_Block_dead(get_nodes_block(ka))) {
3885 /* FIXME: beabi need to keep a Proj(M) */
3886 if (is_Phi(ka) || is_irn_keep(ka) || is_Proj(ka))
3889 if (j != n_keepalives)
3890 set_End_keepalives(n, j, in);
3892 } /* transform_node_End */
3894 /** returns 1 if a == -b */
3895 static int is_negated_value(ir_node *a, ir_node *b) {
3896 if(is_Minus(a) && get_Minus_op(a) == b)
3898 if(is_Minus(b) && get_Minus_op(b) == a)
3900 if(is_Sub(a) && is_Sub(b)) {
3901 ir_node *a_left = get_Sub_left(a);
3902 ir_node *a_right = get_Sub_right(a);
3903 ir_node *b_left = get_Sub_left(b);
3904 ir_node *b_right = get_Sub_right(b);
3906 if(a_left == b_right && a_right == b_left)
3914 * Optimize a Mux into some simpler cases.
3916 static ir_node *transform_node_Mux(ir_node *n) {
3917 ir_node *oldn = n, *sel = get_Mux_sel(n);
3918 ir_mode *mode = get_irn_mode(n);
3920 if (mode == mode_b) {
3921 ir_node *t = get_Mux_true(n);
3922 ir_node *f = get_Mux_false(n);
3923 dbg_info *dbg = get_irn_dbg_info(n);
3924 ir_node *block = get_irn_n(n, -1);
3925 ir_graph *irg = current_ir_graph;
3928 tarval *tv_t = get_Const_tarval(t);
3929 if (tv_t == tarval_b_true) {
3931 assert(get_Const_tarval(f) == tarval_b_false);
3934 return new_rd_Or(dbg, irg, block, sel, f, mode_b);
3937 ir_node* not_sel = new_rd_Not(dbg, irg, block, sel, mode_b);
3938 assert(tv_t == tarval_b_false);
3940 assert(get_Const_tarval(f) == tarval_b_true);
3943 return new_rd_And(dbg, irg, block, not_sel, f, mode_b);
3946 } else if (is_Const(f)) {
3947 tarval *tv_f = get_Const_tarval(f);
3948 if (tv_f == tarval_b_true) {
3949 ir_node* not_sel = new_rd_Not(dbg, irg, block, sel, mode_b);
3950 return new_rd_Or(dbg, irg, block, not_sel, t, mode_b);
3952 assert(tv_f == tarval_b_false);
3953 return new_rd_And(dbg, irg, block, sel, t, mode_b);
3958 if (get_irn_op(sel) == op_Proj && !mode_honor_signed_zeros(mode)) {
3959 ir_node *cmp = get_Proj_pred(sel);
3960 long pn = get_Proj_proj(sel);
3961 ir_node *f = get_Mux_false(n);
3962 ir_node *t = get_Mux_true(n);
3965 * Note: normalization puts the constant on the right side,
3966 * so we check only one case.
3968 * Note further that these optimization work even for floating point
3969 * with NaN's because -NaN == NaN.
3970 * However, if +0 and -0 is handled differently, we cannot use the first
3973 if (get_irn_op(cmp) == op_Cmp
3974 && classify_Const(get_Cmp_right(cmp)) == CNST_NULL) {
3975 ir_node *block = get_irn_n(n, -1);
3977 if(is_negated_value(f, t)) {
3978 ir_node *cmp_left = get_Cmp_left(cmp);
3980 /* Psi(a >= 0, a, -a) = Psi(a <= 0, -a, a) ==> Abs(a) */
3981 if ( (cmp_left == t && (pn == pn_Cmp_Ge || pn == pn_Cmp_Gt))
3982 || (cmp_left == f && (pn == pn_Cmp_Le || pn == pn_Cmp_Lt)))
3984 n = new_rd_Abs(get_irn_dbg_info(n), current_ir_graph, block,
3986 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_ABS);
3988 /* Psi(a <= 0, a, -a) = Psi(a >= 0, -a, a) ==> -Abs(a) */
3989 } else if ((cmp_left == t && (pn == pn_Cmp_Le || pn == pn_Cmp_Lt))
3990 || (cmp_left == f && (pn == pn_Cmp_Ge || pn == pn_Cmp_Gt)))
3992 n = new_rd_Abs(get_irn_dbg_info(n), current_ir_graph, block,
3994 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph,
3996 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_ABS);
4001 if (mode_is_int(mode) && mode_is_signed(mode) &&
4002 get_mode_arithmetic(mode) == irma_twos_complement) {
4003 ir_node *x = get_Cmp_left(cmp);
4005 /* the following optimization works only with signed integer two-complement mode */
4007 if (mode == get_irn_mode(x)) {
4009 * FIXME: this restriction is two rigid, as it would still
4010 * work if mode(x) = Hs and mode == Is, but at least it removes
4013 if ((pn == pn_Cmp_Lt || pn == pn_Cmp_Le) &&
4014 classify_Const(t) == CNST_ALL_ONE &&
4015 classify_Const(f) == CNST_NULL) {
4017 * Mux(x:T </<= 0, 0, -1) -> Shrs(x, sizeof_bits(T) - 1)
4021 n = new_rd_Shrs(get_irn_dbg_info(n),
4022 current_ir_graph, block, x,
4023 new_r_Const_long(current_ir_graph, block, mode_Iu,
4024 get_mode_size_bits(mode) - 1),
4026 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_SHR);
4028 } else if ((pn == pn_Cmp_Gt || pn == pn_Cmp_Ge) &&
4029 classify_Const(t) == CNST_ONE &&
4030 classify_Const(f) == CNST_NULL) {
4032 * Mux(x:T >/>= 0, 0, 1) -> Shr(-x, sizeof_bits(T) - 1)
4036 n = new_rd_Shr(get_irn_dbg_info(n),
4037 current_ir_graph, block,
4038 new_r_Minus(current_ir_graph, block, x, mode),
4039 new_r_Const_long(current_ir_graph, block, mode_Iu,
4040 get_mode_size_bits(mode) - 1),
4042 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_SHR);
4049 return arch_transform_node_Mux(n);
4050 } /* transform_node_Mux */
4053 * Optimize a Psi into some simpler cases.
4055 static ir_node *transform_node_Psi(ir_node *n) {
4057 return transform_node_Mux(n);
4060 } /* transform_node_Psi */
4063 * Tries several [inplace] [optimizing] transformations and returns an
4064 * equivalent node. The difference to equivalent_node() is that these
4065 * transformations _do_ generate new nodes, and thus the old node must
4066 * not be freed even if the equivalent node isn't the old one.
4068 static ir_node *transform_node(ir_node *n) {
4072 * Transform_node is the only "optimizing transformation" that might
4073 * return a node with a different opcode. We iterate HERE until fixpoint
4074 * to get the final result.
4078 if (n->op->ops.transform_node)
4079 n = n->op->ops.transform_node(n);
4080 } while (oldn != n);
4083 } /* transform_node */
4086 * Sets the default transform node operation for an ir_op_ops.
4088 * @param code the opcode for the default operation
4089 * @param ops the operations initialized
4094 static ir_op_ops *firm_set_default_transform_node(ir_opcode code, ir_op_ops *ops)
4098 ops->transform_node = transform_node_##a; \
4132 } /* firm_set_default_transform_node */
4135 /* **************** Common Subexpression Elimination **************** */
4137 /** The size of the hash table used, should estimate the number of nodes
4139 #define N_IR_NODES 512
4141 /** Compares the attributes of two Const nodes. */
4142 static int node_cmp_attr_Const(ir_node *a, ir_node *b) {
4143 return (get_Const_tarval(a) != get_Const_tarval(b))
4144 || (get_Const_type(a) != get_Const_type(b));
4145 } /* node_cmp_attr_Const */
4147 /** Compares the attributes of two Proj nodes. */
4148 static int node_cmp_attr_Proj(ir_node *a, ir_node *b) {
4149 return get_irn_proj_attr(a) != get_irn_proj_attr(b);
4150 } /* node_cmp_attr_Proj */
4152 /** Compares the attributes of two Filter nodes. */
4153 static int node_cmp_attr_Filter(ir_node *a, ir_node *b) {
4154 return get_Filter_proj(a) != get_Filter_proj(b);
4155 } /* node_cmp_attr_Filter */
4157 /** Compares the attributes of two Alloc nodes. */
4158 static int node_cmp_attr_Alloc(ir_node *a, ir_node *b) {
4159 const alloc_attr *pa = get_irn_alloc_attr(a);
4160 const alloc_attr *pb = get_irn_alloc_attr(b);
4161 return (pa->where != pb->where) || (pa->type != pb->type);
4162 } /* node_cmp_attr_Alloc */
4164 /** Compares the attributes of two Free nodes. */
4165 static int node_cmp_attr_Free(ir_node *a, ir_node *b) {
4166 const free_attr *pa = get_irn_free_attr(a);
4167 const free_attr *pb = get_irn_free_attr(b);
4168 return (pa->where != pb->where) || (pa->type != pb->type);
4169 } /* node_cmp_attr_Free */
4171 /** Compares the attributes of two SymConst nodes. */
4172 static int node_cmp_attr_SymConst(ir_node *a, ir_node *b) {
4173 const symconst_attr *pa = get_irn_symconst_attr(a);
4174 const symconst_attr *pb = get_irn_symconst_attr(b);
4175 return (pa->num != pb->num)
4176 || (pa->sym.type_p != pb->sym.type_p)
4177 || (pa->tp != pb->tp);
4178 } /* node_cmp_attr_SymConst */
4180 /** Compares the attributes of two Call nodes. */
4181 static int node_cmp_attr_Call(ir_node *a, ir_node *b) {
4182 return (get_irn_call_attr(a) != get_irn_call_attr(b));
4183 } /* node_cmp_attr_Call */
4185 /** Compares the attributes of two Sel nodes. */
4186 static int node_cmp_attr_Sel(ir_node *a, ir_node *b) {
4187 const ir_entity *a_ent = get_Sel_entity(a);
4188 const ir_entity *b_ent = get_Sel_entity(b);
4190 (a_ent->kind != b_ent->kind) ||
4191 (a_ent->name != b_ent->name) ||
4192 (a_ent->owner != b_ent->owner) ||
4193 (a_ent->ld_name != b_ent->ld_name) ||
4194 (a_ent->type != b_ent->type);
4195 } /* node_cmp_attr_Sel */
4197 /** Compares the attributes of two Phi nodes. */
4198 static int node_cmp_attr_Phi(ir_node *a, ir_node *b) {
4199 /* we can only enter this function if both nodes have the same number of inputs,
4200 hence it is enough to check if one of them is a Phi0 */
4202 /* check the Phi0 attribute */
4203 return get_irn_phi0_attr(a) != get_irn_phi0_attr(b);
4206 } /* node_cmp_attr_Phi */
4208 /** Compares the attributes of two Conv nodes. */
4209 static int node_cmp_attr_Conv(ir_node *a, ir_node *b) {
4210 return get_Conv_strict(a) != get_Conv_strict(b);
4211 } /* node_cmp_attr_Conv */
4213 /** Compares the attributes of two Cast nodes. */
4214 static int node_cmp_attr_Cast(ir_node *a, ir_node *b) {
4215 return get_Cast_type(a) != get_Cast_type(b);
4216 } /* node_cmp_attr_Cast */
4218 /** Compares the attributes of two Load nodes. */
4219 static int node_cmp_attr_Load(ir_node *a, ir_node *b) {
4220 if (get_Load_volatility(a) == volatility_is_volatile ||
4221 get_Load_volatility(b) == volatility_is_volatile)
4222 /* NEVER do CSE on volatile Loads */
4224 /* do not CSE Loads with different alignment. Be conservative. */
4225 if (get_Load_align(a) != get_Load_align(b))
4228 return get_Load_mode(a) != get_Load_mode(b);
4229 } /* node_cmp_attr_Load */
4231 /** Compares the attributes of two Store nodes. */
4232 static int node_cmp_attr_Store(ir_node *a, ir_node *b) {
4233 /* do not CSE Stores with different alignment. Be conservative. */
4234 if (get_Store_align(a) != get_Store_align(b))
4237 /* NEVER do CSE on volatile Stores */
4238 return (get_Store_volatility(a) == volatility_is_volatile ||
4239 get_Store_volatility(b) == volatility_is_volatile);
4240 } /* node_cmp_attr_Store */
4242 /** Compares the attributes of two Confirm nodes. */
4243 static int node_cmp_attr_Confirm(ir_node *a, ir_node *b) {
4244 return (get_Confirm_cmp(a) != get_Confirm_cmp(b));
4245 } /* node_cmp_attr_Confirm */
4247 /** Compares the attributes of two ASM nodes. */
4248 static int node_cmp_attr_ASM(ir_node *a, ir_node *b) {
4250 const ir_asm_constraint *ca;
4251 const ir_asm_constraint *cb;
4254 if (get_ASM_text(a) != get_ASM_text(b))
4257 /* Should we really check the constraints here? Should be better, but is strange. */
4258 n = get_ASM_n_input_constraints(a);
4259 if (n != get_ASM_n_input_constraints(b))
4262 ca = get_ASM_input_constraints(a);
4263 cb = get_ASM_input_constraints(b);
4264 for (i = 0; i < n; ++i) {
4265 if (ca[i].pos != cb[i].pos || ca[i].constraint != cb[i].constraint)
4269 n = get_ASM_n_output_constraints(a);
4270 if (n != get_ASM_n_output_constraints(b))
4273 ca = get_ASM_output_constraints(a);
4274 cb = get_ASM_output_constraints(b);
4275 for (i = 0; i < n; ++i) {
4276 if (ca[i].pos != cb[i].pos || ca[i].constraint != cb[i].constraint)
4280 n = get_ASM_n_clobbers(a);
4281 if (n != get_ASM_n_clobbers(b))
4284 cla = get_ASM_clobbers(a);
4285 clb = get_ASM_clobbers(b);
4286 for (i = 0; i < n; ++i) {
4287 if (cla[i] != clb[i])
4291 } /* node_cmp_attr_ASM */
4294 * Set the default node attribute compare operation for an ir_op_ops.
4296 * @param code the opcode for the default operation
4297 * @param ops the operations initialized
4302 static ir_op_ops *firm_set_default_node_cmp_attr(ir_opcode code, ir_op_ops *ops)
4306 ops->node_cmp_attr = node_cmp_attr_##a; \
4331 } /* firm_set_default_node_cmp_attr */
4334 * Compare function for two nodes in the hash table. Gets two
4335 * nodes as parameters. Returns 0 if the nodes are a cse.
4337 int identities_cmp(const void *elt, const void *key) {
4344 if (a == b) return 0;
4346 if ((get_irn_op(a) != get_irn_op(b)) ||
4347 (get_irn_mode(a) != get_irn_mode(b))) return 1;
4349 /* compare if a's in and b's in are of equal length */
4350 irn_arity_a = get_irn_intra_arity (a);
4351 if (irn_arity_a != get_irn_intra_arity(b))
4354 /* for block-local cse and op_pin_state_pinned nodes: */
4355 if (!get_opt_global_cse() || (get_irn_pinned(a) == op_pin_state_pinned)) {
4356 if (get_irn_intra_n(a, -1) != get_irn_intra_n(b, -1))
4360 /* compare a->in[0..ins] with b->in[0..ins] */
4361 for (i = 0; i < irn_arity_a; i++)
4362 if (get_irn_intra_n(a, i) != get_irn_intra_n(b, i))
4366 * here, we already now that the nodes are identical except their
4369 if (a->op->ops.node_cmp_attr)
4370 return a->op->ops.node_cmp_attr(a, b);
4373 } /* identities_cmp */
4376 * Calculate a hash value of a node.
4378 unsigned ir_node_hash(ir_node *node) {
4382 if (node->op == op_Const) {
4383 /* special value for const, as they only differ in their tarval. */
4384 h = HASH_PTR(node->attr.con.tv);
4385 h = 9*h + HASH_PTR(get_irn_mode(node));
4386 } else if (node->op == op_SymConst) {
4387 /* special value for const, as they only differ in their symbol. */
4388 h = HASH_PTR(node->attr.symc.sym.type_p);
4389 h = 9*h + HASH_PTR(get_irn_mode(node));
4392 /* hash table value = 9*(9*(9*(9*(9*arity+in[0])+in[1])+ ...)+mode)+code */
4393 h = irn_arity = get_irn_intra_arity(node);
4395 /* consider all in nodes... except the block if not a control flow. */
4396 for (i = is_cfop(node) ? -1 : 0; i < irn_arity; i++) {
4397 h = 9*h + HASH_PTR(get_irn_intra_n(node, i));
4401 h = 9*h + HASH_PTR(get_irn_mode(node));
4403 h = 9*h + HASH_PTR(get_irn_op(node));
4407 } /* ir_node_hash */
4409 pset *new_identities(void) {
4410 return new_pset(identities_cmp, N_IR_NODES);
4411 } /* new_identities */
4413 void del_identities(pset *value_table) {
4414 del_pset(value_table);
4415 } /* del_identities */
4418 * Return the canonical node computing the same value as n.
4420 * @param value_table The value table
4421 * @param n The node to lookup
4423 * Looks up the node in a hash table.
4425 * For Const nodes this is performed in the constructor, too. Const
4426 * nodes are extremely time critical because of their frequent use in
4427 * constant string arrays.
4429 static INLINE ir_node *identify(pset *value_table, ir_node *n) {
4432 if (!value_table) return n;
4434 if (get_opt_reassociation()) {
4435 if (is_op_commutative(get_irn_op(n))) {
4436 ir_node *l = get_binop_left(n);
4437 ir_node *r = get_binop_right(n);
4439 /* for commutative operators perform a OP b == b OP a */
4440 if (get_irn_idx(l) > get_irn_idx(r)) {
4441 set_binop_left(n, r);
4442 set_binop_right(n, l);
4447 o = pset_find(value_table, n, ir_node_hash(n));
4456 * During construction we set the op_pin_state_pinned flag in the graph right when the
4457 * optimization is performed. The flag turning on procedure global cse could
4458 * be changed between two allocations. This way we are safe.
4460 static INLINE ir_node *identify_cons(pset *value_table, ir_node *n) {
4463 n = identify(value_table, n);
4464 if (get_irn_n(old, -1) != get_irn_n(n, -1))
4465 set_irg_pinned(current_ir_graph, op_pin_state_floats);
4467 } /* identify_cons */
4470 * Return the canonical node computing the same value as n.
4471 * Looks up the node in a hash table, enters it in the table
4472 * if it isn't there yet.
4474 ir_node *identify_remember(pset *value_table, ir_node *n) {
4477 if (!value_table) return n;
4479 if (get_opt_reassociation()) {
4480 if (is_op_commutative(get_irn_op(n))) {
4481 ir_node *l = get_binop_left(n);
4482 ir_node *r = get_binop_right(n);
4483 int l_idx = get_irn_idx(l);
4484 int r_idx = get_irn_idx(r);
4486 /* For commutative operators perform a OP b == b OP a but keep
4487 constants on the RIGHT side. This helps greatly in some optimizations.
4488 Moreover we use the idx number to make the form deterministic. */
4489 if (is_irn_constlike(l))
4491 if (is_irn_constlike(r))
4493 if (l_idx < r_idx) {
4494 set_binop_left(n, r);
4495 set_binop_right(n, l);
4500 /* lookup or insert in hash table with given hash key. */
4501 o = pset_insert(value_table, n, ir_node_hash(n));
4508 } /* identify_remember */
4510 /* Add a node to the identities value table. */
4511 void add_identities(pset *value_table, ir_node *node) {
4512 if (get_opt_cse() && is_no_Block(node))
4513 identify_remember(value_table, node);
4514 } /* add_identities */
4516 /* Visit each node in the value table of a graph. */
4517 void visit_all_identities(ir_graph *irg, irg_walk_func visit, void *env) {
4519 ir_graph *rem = current_ir_graph;
4521 current_ir_graph = irg;
4522 foreach_pset(irg->value_table, node)
4524 current_ir_graph = rem;
4525 } /* visit_all_identities */
4528 * Garbage in, garbage out. If a node has a dead input, i.e., the
4529 * Bad node is input to the node, return the Bad node.
4531 static INLINE ir_node *gigo(ir_node *node) {
4533 ir_op *op = get_irn_op(node);
4535 /* remove garbage blocks by looking at control flow that leaves the block
4536 and replacing the control flow by Bad. */
4537 if (get_irn_mode(node) == mode_X) {
4538 ir_node *block = get_nodes_block(skip_Proj(node));
4540 /* Don't optimize nodes in immature blocks. */
4541 if (!get_Block_matured(block)) return node;
4542 /* Don't optimize End, may have Bads. */
4543 if (op == op_End) return node;
4545 if (is_Block(block)) {
4546 irn_arity = get_irn_arity(block);
4547 for (i = 0; i < irn_arity; i++) {
4548 if (!is_Bad(get_irn_n(block, i)))
4551 if (i == irn_arity) {
4552 ir_graph *irg = get_irn_irg(block);
4553 /* the start block is never dead */
4554 if (block != get_irg_start_block(irg)
4555 && block != get_irg_end_block(irg))
4561 /* Blocks, Phis and Tuples may have dead inputs, e.g., if one of the
4562 blocks predecessors is dead. */
4563 if (op != op_Block && op != op_Phi && op != op_Tuple) {
4564 irn_arity = get_irn_arity(node);
4567 * Beware: we can only read the block of a non-floating node.
4569 if (is_irn_pinned_in_irg(node) &&
4570 is_Block_dead(get_nodes_block(node)))
4573 for (i = 0; i < irn_arity; i++) {
4574 ir_node *pred = get_irn_n(node, i);
4579 /* Propagating Unknowns here seems to be a bad idea, because
4580 sometimes we need a node as a input and did not want that
4582 However, it might be useful to move this into a later phase
4583 (if you think that optimizing such code is useful). */
4584 if (is_Unknown(pred) && mode_is_data(get_irn_mode(node)))
4585 return new_Unknown(get_irn_mode(node));
4590 /* With this code we violate the agreement that local_optimize
4591 only leaves Bads in Block, Phi and Tuple nodes. */
4592 /* If Block has only Bads as predecessors it's garbage. */
4593 /* If Phi has only Bads as predecessors it's garbage. */
4594 if ((op == op_Block && get_Block_matured(node)) || op == op_Phi) {
4595 irn_arity = get_irn_arity(node);
4596 for (i = 0; i < irn_arity; i++) {
4597 if (!is_Bad(get_irn_n(node, i))) break;
4599 if (i == irn_arity) node = new_Bad();
4606 * These optimizations deallocate nodes from the obstack.
4607 * It can only be called if it is guaranteed that no other nodes
4608 * reference this one, i.e., right after construction of a node.
4610 * @param n The node to optimize
4612 * current_ir_graph must be set to the graph of the node!
4614 ir_node *optimize_node(ir_node *n) {
4617 ir_opcode iro = get_irn_opcode(n);
4619 /* Always optimize Phi nodes: part of the construction. */
4620 if ((!get_opt_optimize()) && (iro != iro_Phi)) return n;
4622 /* constant expression evaluation / constant folding */
4623 if (get_opt_constant_folding()) {
4624 /* neither constants nor Tuple values can be evaluated */
4625 if (iro != iro_Const && (get_irn_mode(n) != mode_T)) {
4626 unsigned fp_model = get_irg_fp_model(current_ir_graph);
4627 int old_fp_mode = tarval_enable_fp_ops((fp_model & fp_strict_algebraic) == 0);
4628 /* try to evaluate */
4629 tv = computed_value(n);
4630 if (tv != tarval_bad) {
4632 ir_type *old_tp = get_irn_type(n);
4633 int i, arity = get_irn_arity(n);
4637 * Try to recover the type of the new expression.
4639 for (i = 0; i < arity && !old_tp; ++i)
4640 old_tp = get_irn_type(get_irn_n(n, i));
4643 * we MUST copy the node here temporary, because it's still needed
4644 * for DBG_OPT_CSTEVAL
4646 node_size = offsetof(ir_node, attr) + n->op->attr_size;
4647 oldn = alloca(node_size);
4649 memcpy(oldn, n, node_size);
4650 CLONE_ARR_A(ir_node *, oldn->in, n->in);
4652 /* ARG, copy the in array, we need it for statistics */
4653 memcpy(oldn->in, n->in, ARR_LEN(n->in) * sizeof(n->in[0]));
4655 /* note the inplace edges module */
4656 edges_node_deleted(n, current_ir_graph);
4658 /* evaluation was successful -- replace the node. */
4659 irg_kill_node(current_ir_graph, n);
4660 nw = new_Const(get_tarval_mode (tv), tv);
4662 if (old_tp && get_type_mode(old_tp) == get_tarval_mode (tv))
4663 set_Const_type(nw, old_tp);
4664 DBG_OPT_CSTEVAL(oldn, nw);
4665 tarval_enable_fp_ops(old_fp_mode);
4668 tarval_enable_fp_ops(old_fp_mode);
4672 /* remove unnecessary nodes */
4673 if (get_opt_constant_folding() ||
4674 (iro == iro_Phi) || /* always optimize these nodes. */
4676 (iro == iro_Proj) ||
4677 (iro == iro_Block) ) /* Flags tested local. */
4678 n = equivalent_node(n);
4680 optimize_preds(n); /* do node specific optimizations of nodes predecessors. */
4682 /* Common Subexpression Elimination.
4684 * Checks whether n is already available.
4685 * The block input is used to distinguish different subexpressions. Right
4686 * now all nodes are op_pin_state_pinned to blocks, i.e., the CSE only finds common
4687 * subexpressions within a block.
4690 n = identify_cons(current_ir_graph->value_table, n);
4693 edges_node_deleted(oldn, current_ir_graph);
4695 /* We found an existing, better node, so we can deallocate the old node. */
4696 irg_kill_node(current_ir_graph, oldn);
4700 /* Some more constant expression evaluation that does not allow to
4702 iro = get_irn_opcode(n);
4703 if (get_opt_constant_folding() ||
4704 (iro == iro_Cond) ||
4705 (iro == iro_Proj)) /* Flags tested local. */
4706 n = transform_node(n);
4708 /* Remove nodes with dead (Bad) input.
4709 Run always for transformation induced Bads. */
4712 /* Now we have a legal, useful node. Enter it in hash table for CSE */
4713 if (get_opt_cse() && (get_irn_opcode(n) != iro_Block)) {
4714 n = identify_remember(current_ir_graph->value_table, n);
4718 } /* optimize_node */
4722 * These optimizations never deallocate nodes (in place). This can cause dead
4723 * nodes lying on the obstack. Remove these by a dead node elimination,
4724 * i.e., a copying garbage collection.
4726 ir_node *optimize_in_place_2(ir_node *n) {
4729 ir_opcode iro = get_irn_opcode(n);
4731 if (!get_opt_optimize() && (get_irn_op(n) != op_Phi)) return n;
4733 /* constant expression evaluation / constant folding */
4734 if (get_opt_constant_folding()) {
4735 /* neither constants nor Tuple values can be evaluated */
4736 if (iro != iro_Const && get_irn_mode(n) != mode_T) {
4737 unsigned fp_model = get_irg_fp_model(current_ir_graph);
4738 int old_fp_mode = tarval_enable_fp_ops((fp_model & fp_strict_algebraic) == 0);
4739 /* try to evaluate */
4740 tv = computed_value(n);
4741 if (tv != tarval_bad) {
4742 /* evaluation was successful -- replace the node. */
4743 ir_type *old_tp = get_irn_type(n);
4744 int i, arity = get_irn_arity(n);
4747 * Try to recover the type of the new expression.
4749 for (i = 0; i < arity && !old_tp; ++i)
4750 old_tp = get_irn_type(get_irn_n(n, i));
4752 n = new_Const(get_tarval_mode(tv), tv);
4754 if (old_tp && get_type_mode(old_tp) == get_tarval_mode(tv))
4755 set_Const_type(n, old_tp);
4757 DBG_OPT_CSTEVAL(oldn, n);
4758 tarval_enable_fp_ops(old_fp_mode);
4761 tarval_enable_fp_ops(old_fp_mode);
4765 /* remove unnecessary nodes */
4766 if (get_opt_constant_folding() ||
4767 (iro == iro_Phi) || /* always optimize these nodes. */
4768 (iro == iro_Id) || /* ... */
4769 (iro == iro_Proj) || /* ... */
4770 (iro == iro_Block) ) /* Flags tested local. */
4771 n = equivalent_node(n);
4773 optimize_preds(n); /* do node specific optimizations of nodes predecessors. */
4775 /** common subexpression elimination **/
4776 /* Checks whether n is already available. */
4777 /* The block input is used to distinguish different subexpressions. Right
4778 now all nodes are op_pin_state_pinned to blocks, i.e., the cse only finds common
4779 subexpressions within a block. */
4780 if (get_opt_cse()) {
4781 n = identify(current_ir_graph->value_table, n);
4784 /* Some more constant expression evaluation. */
4785 iro = get_irn_opcode(n);
4786 if (get_opt_constant_folding() ||
4787 (iro == iro_Cond) ||
4788 (iro == iro_Proj)) /* Flags tested local. */
4789 n = transform_node(n);
4791 /* Remove nodes with dead (Bad) input.
4792 Run always for transformation induced Bads. */
4795 /* Now we can verify the node, as it has no dead inputs any more. */
4798 /* Now we have a legal, useful node. Enter it in hash table for cse.
4799 Blocks should be unique anyways. (Except the successor of start:
4800 is cse with the start block!) */
4801 if (get_opt_cse() && (get_irn_opcode(n) != iro_Block))
4802 n = identify_remember(current_ir_graph->value_table, n);
4805 } /* optimize_in_place_2 */
4808 * Wrapper for external use, set proper status bits after optimization.
4810 ir_node *optimize_in_place(ir_node *n) {
4811 /* Handle graph state */
4812 assert(get_irg_phase_state(current_ir_graph) != phase_building);
4814 if (get_opt_global_cse())
4815 set_irg_pinned(current_ir_graph, op_pin_state_floats);
4816 if (get_irg_outs_state(current_ir_graph) == outs_consistent)
4817 set_irg_outs_inconsistent(current_ir_graph);
4819 /* FIXME: Maybe we could also test whether optimizing the node can
4820 change the control graph. */
4821 set_irg_doms_inconsistent(current_ir_graph);
4822 return optimize_in_place_2(n);
4823 } /* optimize_in_place */
4826 * Sets the default operation for an ir_ops.
4828 ir_op_ops *firm_set_default_operations(ir_opcode code, ir_op_ops *ops) {
4829 ops = firm_set_default_computed_value(code, ops);
4830 ops = firm_set_default_equivalent_node(code, ops);
4831 ops = firm_set_default_transform_node(code, ops);
4832 ops = firm_set_default_node_cmp_attr(code, ops);
4833 ops = firm_set_default_get_type(code, ops);
4834 ops = firm_set_default_get_type_attr(code, ops);
4835 ops = firm_set_default_get_entity_attr(code, ops);
4838 } /* firm_set_default_operations */