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|X) & 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);
1137 if (classify_tarval(value_of(a)) == TV_CLASSIFY_ALL_ONE) {
1139 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1142 if (classify_tarval(value_of(b)) == TV_CLASSIFY_ALL_ONE) {
1144 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1148 if (b == get_Or_left(a) || b == get_Or_right(a)) {
1151 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1156 if (a == get_Or_left(b) || a == get_Or_right(b)) {
1159 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1165 } /* equivalent_node_And */
1168 * Try to remove useless Conv's:
1170 static ir_node *equivalent_node_Conv(ir_node *n) {
1172 ir_node *a = get_Conv_op(n);
1175 ir_mode *n_mode = get_irn_mode(n);
1176 ir_mode *a_mode = get_irn_mode(a);
1178 if (n_mode == a_mode) { /* No Conv necessary */
1179 if (get_Conv_strict(n)) {
1180 /* special case: the predecessor might be a also a Conv */
1182 if (! get_Conv_strict(a)) {
1183 /* first one is not strict, kick it */
1184 set_Conv_op(n, get_Conv_op(a));
1187 /* else both are strict conv, second is superflous */
1189 /* leave strict floating point Conv's */
1194 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CONV);
1195 } else if (get_irn_op(a) == op_Conv) { /* Conv(Conv(b)) */
1199 n_mode = get_irn_mode(n);
1200 b_mode = get_irn_mode(b);
1202 if (n_mode == b_mode) {
1203 if (n_mode == mode_b) {
1204 n = b; /* Convb(Conv*(xxxb(...))) == xxxb(...) */
1205 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
1206 } else if (mode_is_int(n_mode)) {
1207 if (smaller_mode(b_mode, a_mode)){
1208 n = b; /* ConvS(ConvL(xxxS(...))) == xxxS(...) */
1209 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
1215 } /* equivalent_node_Conv */
1218 * A Cast may be removed if the type of the previous node
1219 * is already the type of the Cast.
1221 static ir_node *equivalent_node_Cast(ir_node *n) {
1223 ir_node *pred = get_Cast_op(n);
1225 if (get_irn_type(pred) == get_Cast_type(n)) {
1227 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CAST);
1230 } /* equivalent_node_Cast */
1233 * Several optimizations:
1234 * - no Phi in start block.
1235 * - remove Id operators that are inputs to Phi
1236 * - fold Phi-nodes, iff they have only one predecessor except
1239 static ir_node *equivalent_node_Phi(ir_node *n) {
1243 ir_node *block = NULL; /* to shutup gcc */
1244 ir_node *first_val = NULL; /* to shutup gcc */
1246 if (!get_opt_normalize()) return n;
1248 n_preds = get_Phi_n_preds(n);
1250 block = get_nodes_block(n);
1251 /* @@@ fliegt 'raus, sollte aber doch immer wahr sein!!!
1252 assert(get_irn_arity(block) == n_preds && "phi in wrong block!"); */
1253 if ((is_Block_dead(block)) || /* Control dead */
1254 (block == get_irg_start_block(current_ir_graph))) /* There should be no Phi nodes */
1255 return new_Bad(); /* in the Start Block. */
1257 if (n_preds == 0) return n; /* Phi of dead Region without predecessors. */
1259 /* If the Block has a Bad pred, we also have one. */
1260 for (i = 0; i < n_preds; ++i)
1261 if (is_Bad(get_Block_cfgpred(block, i)))
1262 set_Phi_pred(n, i, new_Bad());
1264 /* Find first non-self-referencing input */
1265 for (i = 0; i < n_preds; ++i) {
1266 first_val = get_Phi_pred(n, i);
1267 if ( (first_val != n) /* not self pointer */
1269 && (! is_Bad(first_val))
1271 ) { /* value not dead */
1272 break; /* then found first value. */
1277 /* A totally Bad or self-referencing Phi (we didn't break the above loop) */
1281 /* search for rest of inputs, determine if any of these
1282 are non-self-referencing */
1283 while (++i < n_preds) {
1284 ir_node *scnd_val = get_Phi_pred(n, i);
1285 if ( (scnd_val != n)
1286 && (scnd_val != first_val)
1288 && (! is_Bad(scnd_val))
1296 /* Fold, if no multiple distinct non-self-referencing inputs */
1298 DBG_OPT_PHI(oldn, n);
1301 } /* equivalent_node_Phi */
1304 * Several optimizations:
1305 * - no Sync in start block.
1306 * - fold Sync-nodes, iff they have only one predecessor except
1309 static ir_node *equivalent_node_Sync(ir_node *n) {
1313 ir_node *first_val = NULL; /* to shutup gcc */
1315 if (!get_opt_normalize()) return n;
1317 n_preds = get_Sync_n_preds(n);
1319 /* Find first non-self-referencing input */
1320 for (i = 0; i < n_preds; ++i) {
1321 first_val = get_Sync_pred(n, i);
1322 if ((first_val != n) /* not self pointer */ &&
1323 (! is_Bad(first_val))
1324 ) { /* value not dead */
1325 break; /* then found first value. */
1330 /* A totally Bad or self-referencing Sync (we didn't break the above loop) */
1333 /* search the rest of inputs, determine if any of these
1334 are non-self-referencing */
1335 while (++i < n_preds) {
1336 ir_node *scnd_val = get_Sync_pred(n, i);
1337 if ((scnd_val != n) &&
1338 (scnd_val != first_val) &&
1339 (! is_Bad(scnd_val))
1345 /* Fold, if no multiple distinct non-self-referencing inputs */
1347 DBG_OPT_SYNC(oldn, n);
1350 } /* equivalent_node_Sync */
1353 * Optimize Proj(Tuple) and gigo() for ProjX in Bad block,
1354 * ProjX(Load) and ProjX(Store).
1356 static ir_node *equivalent_node_Proj(ir_node *proj) {
1357 ir_node *oldn = proj;
1358 ir_node *a = get_Proj_pred(proj);
1360 if (get_irn_op(a) == op_Tuple) {
1361 /* Remove the Tuple/Proj combination. */
1362 if ( get_Proj_proj(proj) <= get_Tuple_n_preds(a) ) {
1363 proj = get_Tuple_pred(a, get_Proj_proj(proj));
1364 DBG_OPT_TUPLE(oldn, a, proj);
1366 /* This should not happen! */
1367 assert(! "found a Proj with higher number than Tuple predecessors");
1370 } else if (get_irn_mode(proj) == mode_X) {
1371 if (is_Block_dead(get_nodes_block(skip_Proj(proj)))) {
1372 /* Remove dead control flow -- early gigo(). */
1374 } else if (get_opt_ldst_only_null_ptr_exceptions()) {
1375 ir_op *op = get_irn_op(a);
1377 if (op == op_Load) {
1378 /* get the Load address */
1379 ir_node *addr = get_Load_ptr(a);
1380 ir_node *blk = get_irn_n(a, -1);
1383 if (value_not_null(addr, &confirm)) {
1384 if (confirm == NULL) {
1385 /* this node may float if it did not depend on a Confirm */
1386 set_irn_pinned(a, op_pin_state_floats);
1388 if (get_Proj_proj(proj) == pn_Load_X_except) {
1389 DBG_OPT_EXC_REM(proj);
1392 return new_r_Jmp(current_ir_graph, blk);
1394 } else if (op == op_Store) {
1395 /* get the load/store address */
1396 ir_node *addr = get_Store_ptr(a);
1397 ir_node *blk = get_irn_n(a, -1);
1400 if (value_not_null(addr, &confirm)) {
1401 if (confirm == NULL) {
1402 /* this node may float if it did not depend on a Confirm */
1403 set_irn_pinned(a, op_pin_state_floats);
1405 if (get_Proj_proj(proj) == pn_Store_X_except) {
1406 DBG_OPT_EXC_REM(proj);
1409 return new_r_Jmp(current_ir_graph, blk);
1416 } /* equivalent_node_Proj */
1421 static ir_node *equivalent_node_Id(ir_node *n) {
1426 } while (get_irn_op(n) == op_Id);
1428 DBG_OPT_ID(oldn, n);
1430 } /* equivalent_node_Id */
1435 static ir_node *equivalent_node_Mux(ir_node *n)
1437 ir_node *oldn = n, *sel = get_Mux_sel(n);
1438 tarval *ts = value_of(sel);
1440 /* Mux(true, f, t) == t */
1441 if (ts == tarval_b_true) {
1442 n = get_Mux_true(n);
1443 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_C);
1445 /* Mux(false, f, t) == f */
1446 else if (ts == tarval_b_false) {
1447 n = get_Mux_false(n);
1448 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_C);
1450 /* Mux(v, x, x) == x */
1451 else if (get_Mux_false(n) == get_Mux_true(n)) {
1452 n = get_Mux_true(n);
1453 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_EQ);
1455 else if (get_irn_op(sel) == op_Proj && !mode_honor_signed_zeros(get_irn_mode(n))) {
1456 ir_node *cmp = get_Proj_pred(sel);
1457 long proj_nr = get_Proj_proj(sel);
1458 ir_node *b = get_Mux_false(n);
1459 ir_node *a = get_Mux_true(n);
1462 * Note: normalization puts the constant on the right site,
1463 * so we check only one case.
1465 * Note further that these optimization work even for floating point
1466 * with NaN's because -NaN == NaN.
1467 * However, if +0 and -0 is handled differently, we cannot use the first one.
1469 if (get_irn_op(cmp) == op_Cmp && get_Cmp_left(cmp) == a) {
1470 if (classify_Const(get_Cmp_right(cmp)) == CNST_NULL) {
1471 /* Mux(a CMP 0, X, a) */
1472 if (get_irn_op(b) == op_Minus && get_Minus_op(b) == a) {
1473 /* Mux(a CMP 0, -a, a) */
1474 if (proj_nr == pn_Cmp_Eq) {
1475 /* Mux(a == 0, -a, a) ==> -a */
1477 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1478 } else if (proj_nr == pn_Cmp_Lg || proj_nr == pn_Cmp_Ne) {
1479 /* Mux(a != 0, -a, a) ==> a */
1481 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1483 } else if (classify_Const(b) == CNST_NULL) {
1484 /* Mux(a CMP 0, 0, a) */
1485 if (proj_nr == pn_Cmp_Lg || proj_nr == pn_Cmp_Ne) {
1486 /* Mux(a != 0, 0, a) ==> a */
1488 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1489 } else if (proj_nr == pn_Cmp_Eq) {
1490 /* Mux(a == 0, 0, a) ==> 0 */
1492 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1499 } /* equivalent_node_Mux */
1502 * Returns a equivalent node of a Psi: if a condition is true
1503 * and all previous conditions are false we know its value.
1504 * If all conditions are false its value is the default one.
1506 static ir_node *equivalent_node_Psi(ir_node *n) {
1508 return equivalent_node_Mux(n);
1510 } /* equivalent_node_Psi */
1513 * Optimize -a CMP -b into b CMP a.
1514 * This works only for for modes where unary Minus
1516 * Note that two-complement integers can Overflow
1517 * so it will NOT work.
1519 * For == and != can be handled in Proj(Cmp)
1521 static ir_node *equivalent_node_Cmp(ir_node *n) {
1522 ir_node *left = get_Cmp_left(n);
1523 ir_node *right = get_Cmp_right(n);
1525 if (get_irn_op(left) == op_Minus && get_irn_op(right) == op_Minus &&
1526 !mode_overflow_on_unary_Minus(get_irn_mode(left))) {
1527 left = get_Minus_op(left);
1528 right = get_Minus_op(right);
1529 set_Cmp_left(n, right);
1530 set_Cmp_right(n, left);
1533 } /* equivalent_node_Cmp */
1536 * Remove Confirm nodes if setting is on.
1537 * Replace Confirms(x, '=', Constlike) by Constlike.
1539 static ir_node *equivalent_node_Confirm(ir_node *n) {
1540 ir_node *pred = get_Confirm_value(n);
1541 pn_Cmp pnc = get_Confirm_cmp(n);
1543 if (get_irn_op(pred) == op_Confirm && pnc == get_Confirm_cmp(pred)) {
1545 * rare case: two identical Confirms one after another,
1546 * replace the second one with the first.
1550 if (pnc == pn_Cmp_Eq) {
1551 ir_node *bound = get_Confirm_bound(n);
1554 * Optimize a rare case:
1555 * Confirm(x, '=', Constlike) ==> Constlike
1557 if (is_irn_constlike(bound)) {
1558 DBG_OPT_CONFIRM(n, bound);
1562 return get_opt_remove_confirm() ? get_Confirm_value(n) : n;
1566 * Optimize CopyB(mem, x, x) into a Nop.
1568 static ir_node *equivalent_node_CopyB(ir_node *n) {
1569 ir_node *a = get_CopyB_dst(n);
1570 ir_node *b = get_CopyB_src(n);
1573 /* Turn CopyB into a tuple (mem, jmp, bad, bad) */
1574 ir_node *mem = get_CopyB_mem(n);
1575 ir_node *blk = get_nodes_block(n);
1576 turn_into_tuple(n, pn_CopyB_max);
1577 set_Tuple_pred(n, pn_CopyB_M, mem);
1578 set_Tuple_pred(n, pn_CopyB_X_regular, new_r_Jmp(current_ir_graph, blk));
1579 set_Tuple_pred(n, pn_CopyB_X_except, new_Bad()); /* no exception */
1580 set_Tuple_pred(n, pn_CopyB_M_except, new_Bad());
1583 } /* equivalent_node_CopyB */
1586 * Optimize Bounds(idx, idx, upper) into idx.
1588 static ir_node *equivalent_node_Bound(ir_node *n) {
1589 ir_node *idx = get_Bound_index(n);
1590 ir_node *lower = get_Bound_lower(n);
1593 /* By definition lower < upper, so if idx == lower -->
1594 lower <= idx && idx < upper */
1596 /* Turn Bound into a tuple (mem, jmp, bad, idx) */
1599 ir_node *pred = skip_Proj(idx);
1601 if (get_irn_op(pred) == op_Bound) {
1603 * idx was Bounds_check previously, it is still valid if
1604 * lower <= pred_lower && pred_upper <= upper.
1606 ir_node *upper = get_Bound_upper(n);
1607 if (get_Bound_lower(pred) == lower &&
1608 get_Bound_upper(pred) == upper) {
1610 * One could expect that we simply return the previous
1611 * Bound here. However, this would be wrong, as we could
1612 * add an exception Proj to a new location then.
1613 * So, we must turn in into a tuple.
1620 /* Turn Bound into a tuple (mem, jmp, bad, idx) */
1621 ir_node *mem = get_Bound_mem(n);
1622 ir_node *blk = get_nodes_block(n);
1623 turn_into_tuple(n, pn_Bound_max);
1624 set_Tuple_pred(n, pn_Bound_M, mem);
1625 set_Tuple_pred(n, pn_Bound_X_regular, new_r_Jmp(current_ir_graph, blk)); /* no exception */
1626 set_Tuple_pred(n, pn_Bound_X_except, new_Bad()); /* no exception */
1627 set_Tuple_pred(n, pn_Bound_res, idx);
1630 } /* equivalent_node_Bound */
1633 * equivalent_node() returns a node equivalent to input n. It skips all nodes that
1634 * perform no actual computation, as, e.g., the Id nodes. It does not create
1635 * new nodes. It is therefore safe to free n if the node returned is not n.
1636 * If a node returns a Tuple we can not just skip it. If the size of the
1637 * in array fits, we transform n into a tuple (e.g., Div).
1639 ir_node *equivalent_node(ir_node *n) {
1640 if (n->op->ops.equivalent_node)
1641 return n->op->ops.equivalent_node(n);
1643 } /* equivalent_node */
1646 * Sets the default equivalent node operation for an ir_op_ops.
1648 * @param code the opcode for the default operation
1649 * @param ops the operations initialized
1654 static ir_op_ops *firm_set_default_equivalent_node(ir_opcode code, ir_op_ops *ops)
1658 ops->equivalent_node = equivalent_node_##a; \
1698 } /* firm_set_default_equivalent_node */
1701 * Returns non-zero if a node is a Phi node
1702 * with all predecessors constant.
1704 static int is_const_Phi(ir_node *n) {
1709 for (i = get_irn_arity(n) - 1; i >= 0; --i)
1710 if (! is_Const(get_irn_n(n, i)))
1713 } /* is_const_Phi */
1716 * Apply an evaluator on a binop with a constant operators (and one Phi).
1718 * @param phi the Phi node
1719 * @param other the other operand
1720 * @param eval an evaluator function
1721 * @param left if non-zero, other is the left operand, else the right
1723 * @return a new Phi node if the conversion was successful, NULL else
1725 static ir_node *apply_binop_on_phi(ir_node *phi, tarval *other, tarval *(*eval)(tarval *, tarval *), int left) {
1731 int i, n = get_irn_arity(phi);
1733 NEW_ARR_A(void *, res, n);
1735 for (i = 0; i < n; ++i) {
1736 pred = get_irn_n(phi, i);
1737 tv = get_Const_tarval(pred);
1738 tv = eval(other, tv);
1740 if (tv == tarval_bad) {
1741 /* folding failed, bad */
1747 for (i = 0; i < n; ++i) {
1748 pred = get_irn_n(phi, i);
1749 tv = get_Const_tarval(pred);
1750 tv = eval(tv, other);
1752 if (tv == tarval_bad) {
1753 /* folding failed, bad */
1759 mode = get_irn_mode(phi);
1760 irg = current_ir_graph;
1761 for (i = 0; i < n; ++i) {
1762 pred = get_irn_n(phi, i);
1763 res[i] = new_r_Const_type(irg, get_irg_start_block(irg),
1764 mode, res[i], get_Const_type(pred));
1766 return new_r_Phi(irg, get_nodes_block(phi), n, (ir_node **)res, mode);
1767 } /* apply_binop_on_phi */
1770 * Apply an evaluator on a unop with a constant operator (a Phi).
1772 * @param phi the Phi node
1773 * @param eval an evaluator function
1775 * @return a new Phi node if the conversion was successful, NULL else
1777 static ir_node *apply_unop_on_phi(ir_node *phi, tarval *(*eval)(tarval *)) {
1783 int i, n = get_irn_arity(phi);
1785 NEW_ARR_A(void *, res, n);
1786 for (i = 0; i < n; ++i) {
1787 pred = get_irn_n(phi, i);
1788 tv = get_Const_tarval(pred);
1791 if (tv == tarval_bad) {
1792 /* folding failed, bad */
1797 mode = get_irn_mode(phi);
1798 irg = current_ir_graph;
1799 for (i = 0; i < n; ++i) {
1800 pred = get_irn_n(phi, i);
1801 res[i] = new_r_Const_type(irg, get_irg_start_block(irg),
1802 mode, res[i], get_Const_type(pred));
1804 return new_r_Phi(irg, get_nodes_block(phi), n, (ir_node **)res, mode);
1805 } /* apply_unop_on_phi */
1808 * Transform AddP(P, ConvIs(Iu)), AddP(P, ConvIu(Is)) and
1809 * SubP(P, ConvIs(Iu)), SubP(P, ConvIu(Is)).
1810 * If possible, remove the Conv's.
1812 static ir_node *transform_node_AddSub(ir_node *n) {
1813 ir_mode *mode = get_irn_mode(n);
1815 if (mode_is_reference(mode)) {
1816 ir_node *left = get_binop_left(n);
1817 ir_node *right = get_binop_right(n);
1818 int ref_bits = get_mode_size_bits(mode);
1820 if (get_irn_op(left) == op_Conv) {
1821 ir_mode *mode = get_irn_mode(left);
1822 int bits = get_mode_size_bits(mode);
1824 if (ref_bits == bits &&
1825 mode_is_int(mode) &&
1826 get_mode_arithmetic(mode) == irma_twos_complement) {
1827 ir_node *pre = get_Conv_op(left);
1828 ir_mode *pre_mode = get_irn_mode(pre);
1830 if (mode_is_int(pre_mode) &&
1831 get_mode_size_bits(pre_mode) == bits &&
1832 get_mode_arithmetic(pre_mode) == irma_twos_complement) {
1833 /* ok, this conv just changes to sign, moreover the calculation
1834 * is done with same number of bits as our address mode, so
1835 * we can ignore the conv as address calculation can be viewed
1836 * as either signed or unsigned
1838 set_binop_left(n, pre);
1843 if (get_irn_op(right) == op_Conv) {
1844 ir_mode *mode = get_irn_mode(right);
1845 int bits = get_mode_size_bits(mode);
1847 if (ref_bits == bits &&
1848 mode_is_int(mode) &&
1849 get_mode_arithmetic(mode) == irma_twos_complement) {
1850 ir_node *pre = get_Conv_op(right);
1851 ir_mode *pre_mode = get_irn_mode(pre);
1853 if (mode_is_int(pre_mode) &&
1854 get_mode_size_bits(pre_mode) == bits &&
1855 get_mode_arithmetic(pre_mode) == irma_twos_complement) {
1856 /* ok, this conv just changes to sign, moreover the calculation
1857 * is done with same number of bits as our address mode, so
1858 * we can ignore the conv as address calculation can be viewed
1859 * as either signed or unsigned
1861 set_binop_right(n, pre);
1867 } /* transform_node_AddSub */
1869 #define HANDLE_BINOP_PHI(op,a,b,c) \
1871 if (is_Const(b) && is_const_Phi(a)) { \
1872 /* check for Op(Phi, Const) */ \
1873 c = apply_binop_on_phi(a, get_Const_tarval(b), op, 0); \
1875 else if (is_Const(a) && is_const_Phi(b)) { \
1876 /* check for Op(Const, Phi) */ \
1877 c = apply_binop_on_phi(b, get_Const_tarval(a), op, 1); \
1880 DBG_OPT_ALGSIM0(oldn, c, FS_OPT_CONST_PHI); \
1884 #define HANDLE_UNOP_PHI(op,a,c) \
1886 if (is_const_Phi(a)) { \
1887 /* check for Op(Phi) */ \
1888 c = apply_unop_on_phi(a, op); \
1890 DBG_OPT_ALGSIM0(oldn, c, FS_OPT_CONST_PHI); \
1896 * Do the AddSub optimization, then Transform
1897 * Constant folding on Phi
1898 * Add(a,a) -> Mul(a, 2)
1899 * Add(Mul(a, x), a) -> Mul(a, x+1)
1900 * if the mode is integer or float.
1901 * Transform Add(a,-b) into Sub(a,b).
1902 * Reassociation might fold this further.
1904 static ir_node *transform_node_Add(ir_node *n) {
1906 ir_node *a, *b, *c, *oldn = n;
1908 n = transform_node_AddSub(n);
1910 a = get_Add_left(n);
1911 b = get_Add_right(n);
1913 HANDLE_BINOP_PHI(tarval_add, a,b,c);
1915 mode = get_irn_mode(n);
1917 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
1918 if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
1921 if (mode_is_num(mode)) {
1922 /* the following code leads to endless recursion when Mul are replaced by a simple instruction chain */
1923 if (!get_opt_arch_dep_running() && a == b && mode_is_int(mode)) {
1924 ir_node *block = get_irn_n(n, -1);
1927 get_irn_dbg_info(n),
1931 new_r_Const_long(current_ir_graph, block, mode, 2),
1933 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_A);
1938 get_irn_dbg_info(n),
1944 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_MINUS_B);
1949 get_irn_dbg_info(n),
1955 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_MINUS_B);
1958 if (! get_opt_reassociation()) {
1959 /* do NOT execute this code if reassociation is enabled, it does the inverse! */
1961 ir_node *ma = get_Mul_left(a);
1962 ir_node *mb = get_Mul_right(a);
1965 ir_node *blk = get_irn_n(n, -1);
1967 get_irn_dbg_info(n), current_ir_graph, blk,
1970 get_irn_dbg_info(n), current_ir_graph, blk,
1972 new_r_Const_long(current_ir_graph, blk, mode, 1),
1975 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_MUL_A_X_A);
1977 } else if (b == mb) {
1978 ir_node *blk = get_irn_n(n, -1);
1980 get_irn_dbg_info(n), current_ir_graph, blk,
1983 get_irn_dbg_info(n), current_ir_graph, blk,
1985 new_r_Const_long(current_ir_graph, blk, mode, 1),
1988 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_MUL_A_X_A);
1993 ir_node *ma = get_Mul_left(b);
1994 ir_node *mb = get_Mul_right(b);
1997 ir_node *blk = get_irn_n(n, -1);
1999 get_irn_dbg_info(n), current_ir_graph, blk,
2002 get_irn_dbg_info(n), current_ir_graph, blk,
2004 new_r_Const_long(current_ir_graph, blk, mode, 1),
2007 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_MUL_A_X_A);
2011 ir_node *blk = get_irn_n(n, -1);
2013 get_irn_dbg_info(n), current_ir_graph, blk,
2016 get_irn_dbg_info(n), current_ir_graph, blk,
2018 new_r_Const_long(current_ir_graph, blk, mode, 1),
2021 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_MUL_A_X_A);
2026 if (get_mode_arithmetic(mode) == irma_twos_complement) {
2027 /* Here we rely on constants be on the RIGHT side */
2029 ir_node *op = get_Not_op(a);
2031 if (classify_Const(b) == CNST_ONE) {
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 ir_node *blk = get_irn_n(n, -1);
2041 n = new_r_Const(current_ir_graph, blk, mode, get_mode_minus_one(mode));
2042 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_X_NOT_X);
2047 ir_node *op = get_Not_op(b);
2051 ir_node *blk = get_irn_n(n, -1);
2052 n = new_r_Const(current_ir_graph, blk, mode, get_mode_minus_one(mode));
2053 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_X_NOT_X);
2060 } /* transform_node_Add */
2063 static ir_node *const_negate(ir_node *cnst) {
2064 tarval *tv = tarval_neg(get_Const_tarval(cnst));
2065 dbg_info *dbgi = get_irn_dbg_info(cnst);
2066 ir_graph *irg = get_irn_irg(cnst);
2067 ir_node *block = get_nodes_block(cnst);
2068 ir_mode *mode = get_irn_mode(cnst);
2069 if (tv == tarval_bad) return NULL;
2070 return new_rd_Const(dbgi, irg, block, mode, tv);
2074 * Do the AddSub optimization, then Transform
2075 * Constant folding on Phi
2076 * Sub(0,a) -> Minus(a)
2077 * Sub(Mul(a, x), a) -> Mul(a, x-1)
2078 * Sub(Sub(x, y), b) -> Sub(x, Add(y,b))
2079 * Sub(Add(a, x), x) -> a
2080 * Sub(x, Add(x, a)) -> -a
2081 * Sub(x, Const) -> Add(x, -Const)
2083 static ir_node *transform_node_Sub(ir_node *n) {
2088 n = transform_node_AddSub(n);
2090 a = get_Sub_left(n);
2091 b = get_Sub_right(n);
2093 mode = get_irn_mode(n);
2096 HANDLE_BINOP_PHI(tarval_sub, a,b,c);
2098 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
2099 if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
2102 /* Sub(a, Const) -> Add(a, -Const) */
2103 if (is_Const(b) && get_irn_mode(b) != mode_P) {
2104 ir_node* cnst = const_negate(b);
2106 ir_node *block = get_nodes_block(n);
2107 dbg_info *dbgi = get_irn_dbg_info(n);
2108 ir_graph *irg = get_irn_irg(n);
2109 ir_node *add = new_rd_Add(dbgi, irg, block, a, cnst, mode);
2115 if (is_Minus(a)) { /* -a - b -> -(a + b) */
2116 ir_graph *irg = current_ir_graph;
2117 dbg_info *dbg = get_irn_dbg_info(n);
2118 ir_node *block = get_nodes_block(n);
2119 ir_node *left = get_Minus_op(a);
2120 ir_mode *mode = get_irn_mode(n);
2121 ir_node *add = new_rd_Add(dbg, irg, block, left, b, mode);
2122 ir_node *neg = new_rd_Minus(dbg, irg, block, add, mode);
2124 } else if (is_Minus(b)) { /* a - -b -> a + b */
2125 ir_graph *irg = current_ir_graph;
2126 dbg_info *dbg = get_irn_dbg_info(n);
2127 ir_node *block = get_nodes_block(n);
2128 ir_node *right = get_Minus_op(b);
2129 ir_mode *mode = get_irn_mode(n);
2130 ir_node *add = new_rd_Add(dbg, irg, block, a, right, mode);
2132 } else if (is_Sub(b)) { /* a - (b - c) -> a + (c - b) */
2133 ir_graph *irg = current_ir_graph;
2134 dbg_info *s_dbg = get_irn_dbg_info(b);
2135 ir_node *s_block = get_nodes_block(b);
2136 ir_node *s_left = get_Sub_right(b);
2137 ir_node *s_right = get_Sub_left(b);
2138 ir_mode *s_mode = get_irn_mode(b);
2139 ir_node *sub = new_rd_Sub(s_dbg, irg, s_block, s_left, s_right, s_mode);
2140 dbg_info *a_dbg = get_irn_dbg_info(n);
2141 ir_node *a_block = get_nodes_block(n);
2142 ir_mode *a_mode = get_irn_mode(n);
2143 ir_node *add = new_rd_Add(a_dbg, irg, a_block, a, sub, a_mode);
2145 } else if (is_Mul(b)) { /* a - (b * const2) -> a + (b * -const2) */
2146 ir_node* m_right = get_Mul_right(b);
2147 if (is_Const(m_right)) {
2148 ir_node* cnst2 = const_negate(m_right);
2149 if (cnst2 != NULL) {
2150 ir_graph *irg = current_ir_graph;
2151 dbg_info *m_dbg = get_irn_dbg_info(b);
2152 ir_node *m_block = get_nodes_block(b);
2153 ir_node *m_left = get_Mul_left(b);
2154 ir_mode *m_mode = get_irn_mode(b);
2155 ir_node *mul = new_rd_Mul(m_dbg, irg, m_block, m_left, cnst2, m_mode);
2156 dbg_info *a_dbg = get_irn_dbg_info(n);
2157 ir_node *a_block = get_nodes_block(n);
2158 ir_mode *a_mode = get_irn_mode(n);
2159 ir_node *add = new_rd_Add(a_dbg, irg, a_block, a, mul, a_mode);
2165 /* Beware of Sub(P, P) which cannot be optimized into a simple Minus ... */
2166 if (mode_is_num(mode) && mode == get_irn_mode(a) && (classify_Const(a) == CNST_NULL)) {
2168 get_irn_dbg_info(n),
2173 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_0_A);
2177 if (mode_wrap_around(mode)) {
2178 ir_node *left = get_Add_left(a);
2179 ir_node *right = get_Add_right(a);
2181 /* FIXME: Does the Conv's work only for two complement or generally? */
2183 if (mode != get_irn_mode(right)) {
2184 /* This Sub is an effective Cast */
2185 right = new_r_Conv(get_irn_irg(n), get_irn_n(n, -1), right, mode);
2188 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2190 } else if (right == b) {
2191 if (mode != get_irn_mode(left)) {
2192 /* This Sub is an effective Cast */
2193 left = new_r_Conv(get_irn_irg(n), get_irn_n(n, -1), left, mode);
2196 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2202 if (mode_wrap_around(mode)) {
2203 ir_node *left = get_Add_left(b);
2204 ir_node *right = get_Add_right(b);
2206 /* FIXME: Does the Conv's work only for two complement or generally? */
2208 ir_mode *r_mode = get_irn_mode(right);
2210 n = new_r_Minus(get_irn_irg(n), get_irn_n(n, -1), right, r_mode);
2211 if (mode != r_mode) {
2212 /* This Sub is an effective Cast */
2213 n = new_r_Conv(get_irn_irg(n), get_irn_n(n, -1), n, mode);
2215 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2217 } else if (right == a) {
2218 ir_mode *l_mode = get_irn_mode(left);
2220 n = new_r_Minus(get_irn_irg(n), get_irn_n(n, -1), left, l_mode);
2221 if (mode != l_mode) {
2222 /* This Sub is an effective Cast */
2223 n = new_r_Conv(get_irn_irg(n), get_irn_n(n, -1), n, mode);
2225 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2230 if (mode_is_int(mode) && is_Conv(a) && is_Conv(b)) {
2231 ir_mode *mode = get_irn_mode(a);
2233 if (mode == get_irn_mode(b)) {
2239 /* check if it's allowed to skip the conv */
2240 ma = get_irn_mode(a);
2241 mb = get_irn_mode(b);
2243 if (mode_is_reference(ma) && mode_is_reference(mb)) {
2244 /* SubInt(ConvInt(aP), ConvInt(bP)) -> SubInt(aP,bP) */
2246 set_Sub_right(n, b);
2252 /* do NOT execute this code if reassociation is enabled, it does the inverse! */
2253 if (get_opt_reassociation() && get_irn_op(a) == op_Mul) {
2254 ir_node *ma = get_Mul_left(a);
2255 ir_node *mb = get_Mul_right(a);
2258 ir_node *blk = get_irn_n(n, -1);
2260 get_irn_dbg_info(n),
2261 current_ir_graph, blk,
2264 get_irn_dbg_info(n),
2265 current_ir_graph, blk,
2267 new_r_Const_long(current_ir_graph, blk, mode, 1),
2270 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MUL_A_X_A);
2272 } else if (mb == b) {
2273 ir_node *blk = get_irn_n(n, -1);
2275 get_irn_dbg_info(n),
2276 current_ir_graph, blk,
2279 get_irn_dbg_info(n),
2280 current_ir_graph, blk,
2282 new_r_Const_long(current_ir_graph, blk, mode, 1),
2285 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MUL_A_X_A);
2290 ir_node *x = get_Sub_left(a);
2291 ir_node *y = get_Sub_right(a);
2292 ir_node *blk = get_irn_n(n, -1);
2293 ir_mode *m_b = get_irn_mode(b);
2294 ir_mode *m_y = get_irn_mode(y);
2297 /* Determine the right mode for the Add. */
2300 else if (mode_is_reference(m_b))
2302 else if (mode_is_reference(m_y))
2306 * Both modes are different but none is reference,
2307 * happens for instance in SubP(SubP(P, Iu), Is).
2308 * We have two possibilities here: Cast or ignore.
2309 * Currently we ignore this case.
2314 add = new_r_Add(current_ir_graph, blk, y, b, mode);
2316 n = new_rd_Sub(get_irn_dbg_info(n), current_ir_graph, blk, x, add, mode);
2317 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_SUB_X_Y_Z);
2321 if (get_mode_arithmetic(mode) == irma_twos_complement) {
2322 if (is_Const(a) && is_Not(b)) {
2323 /* c - ~X = X + (c+1) */
2324 tarval *tv = get_Const_tarval(a);
2326 tv = tarval_add(tv, get_mode_one(mode));
2327 if (tv != tarval_bad) {
2328 ir_node *blk = get_irn_n(n, -1);
2329 ir_node *c = new_r_Const(current_ir_graph, blk, mode, tv);
2330 n = new_rd_Add(get_irn_dbg_info(n), current_ir_graph, blk, get_Not_op(b), c, mode);
2331 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_C_NOT_X);
2337 } /* transform_node_Sub */
2340 * Several transformation done on n*n=2n bits mul.
2341 * These transformations must be done here because new nodes may be produced.
2343 static ir_node *transform_node_Mul2n(ir_node *n, ir_mode *mode) {
2345 ir_node *a = get_Mul_left(n);
2346 ir_node *b = get_Mul_right(n);
2347 tarval *ta = value_of(a);
2348 tarval *tb = value_of(b);
2349 ir_mode *smode = get_irn_mode(a);
2351 if (ta == get_mode_one(smode)) {
2352 ir_node *blk = get_irn_n(n, -1);
2353 n = new_rd_Conv(get_irn_dbg_info(n), current_ir_graph, blk, b, mode);
2354 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
2357 else if (ta == get_mode_minus_one(smode)) {
2358 ir_node *blk = get_irn_n(n, -1);
2359 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, blk, b, smode);
2360 n = new_rd_Conv(get_irn_dbg_info(n), current_ir_graph, blk, n, mode);
2361 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2364 if (tb == get_mode_one(smode)) {
2365 ir_node *blk = get_irn_n(a, -1);
2366 n = new_rd_Conv(get_irn_dbg_info(n), current_ir_graph, blk, a, mode);
2367 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
2370 else if (tb == get_mode_minus_one(smode)) {
2371 ir_node *blk = get_irn_n(n, -1);
2372 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, blk, a, smode);
2373 n = new_rd_Conv(get_irn_dbg_info(n), current_ir_graph, blk, n, mode);
2374 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2381 * Transform Mul(a,-1) into -a.
2382 * Do constant evaluation of Phi nodes.
2383 * Do architecture dependent optimizations on Mul nodes
2385 static ir_node *transform_node_Mul(ir_node *n) {
2386 ir_node *c, *oldn = n;
2387 ir_mode *mode = get_irn_mode(n);
2388 ir_node *a = get_Mul_left(n);
2389 ir_node *b = get_Mul_right(n);
2391 if (is_Bad(a) || is_Bad(b))
2394 if (mode != get_irn_mode(a))
2395 return transform_node_Mul2n(n, mode);
2397 HANDLE_BINOP_PHI(tarval_mul, a,b,c);
2399 if (mode_is_signed(mode)) {
2402 if (value_of(a) == get_mode_minus_one(mode))
2404 else if (value_of(b) == get_mode_minus_one(mode))
2407 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1), r, mode);
2408 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2413 if (is_Const(b)) { /* -a * const -> a * -const */
2414 ir_node* cnst = const_negate(b);
2416 set_Mul_left( n, get_Minus_op(a));
2417 set_Mul_right(n, cnst);
2420 } else if (is_Minus(b)) { /* -a * -b -> a * b */
2421 set_Mul_left( n, get_Minus_op(a));
2422 set_Mul_right(n, get_Minus_op(b));
2424 } else if (is_Sub(b)) { /* -a * (b - c) -> a * (c - b) */
2425 ir_node *sub_l = get_Sub_left(b);
2426 ir_node *sub_r = get_Sub_right(b);
2427 dbg_info *dbgi = get_irn_dbg_info(b);
2428 ir_graph *irg = current_ir_graph;
2429 ir_mode *mode = get_irn_mode(b);
2430 ir_node *block = get_nodes_block(b);
2431 ir_node *new_b = new_rd_Sub(dbgi, irg, block, sub_r, sub_l, mode);
2432 set_Mul_left( n, get_Minus_op(a));
2433 set_Mul_right(n, new_b);
2436 } else if (is_Minus(b)) {
2437 if (is_Sub(a)) { /* (a - b) * -c -> (b - a) * c */
2438 ir_node *sub_l = get_Sub_left(a);
2439 ir_node *sub_r = get_Sub_right(a);
2440 dbg_info *dbgi = get_irn_dbg_info(a);
2441 ir_graph *irg = current_ir_graph;
2442 ir_mode *mode = get_irn_mode(a);
2443 ir_node *block = get_nodes_block(a);
2444 ir_node *new_a = new_rd_Sub(dbgi, irg, block, sub_r, sub_l, mode);
2445 set_Mul_left (n, new_a);
2446 set_Mul_right(n, get_Minus_op(b));
2450 if (get_mode_arithmetic(mode) == irma_ieee754) {
2452 tarval *tv = get_Const_tarval(a);
2453 if (tarval_ieee754_get_exponent(tv) == 1 && tarval_ieee754_zero_mantissa(tv)) {
2454 n = new_rd_Add(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1), b, b, mode);
2455 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_A_A);
2459 else if (is_Const(b)) {
2460 tarval *tv = get_Const_tarval(b);
2461 if (tarval_ieee754_get_exponent(tv) == 1 && tarval_ieee754_zero_mantissa(tv)) {
2462 n = new_rd_Add(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1), a, a, mode);
2463 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_A_A);
2468 return arch_dep_replace_mul_with_shifts(n);
2469 } /* transform_node_Mul */
2472 * Transform a Div Node.
2474 static ir_node *transform_node_Div(ir_node *n) {
2475 tarval *tv = value_of(n);
2476 ir_mode *mode = get_Div_resmode(n);
2479 if (tv != tarval_bad) {
2480 value = new_Const(get_tarval_mode(tv), tv);
2482 DBG_OPT_CSTEVAL(n, value);
2485 ir_node *a = get_Div_left(n);
2486 ir_node *b = get_Div_right(n);
2489 if (a == b && value_not_zero(a, &dummy)) {
2490 /* BEWARE: we can optimize a/a to 1 only if this cannot cause a exception */
2491 value = new_Const(mode, get_mode_one(mode));
2492 DBG_OPT_CSTEVAL(n, value);
2495 if (mode_is_signed(mode) && is_Const(b)) {
2496 tarval *tv = get_Const_tarval(b);
2498 if (tv == get_mode_minus_one(mode)) {
2500 value = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1), a, mode);
2501 DBG_OPT_CSTEVAL(n, value);
2505 /* Try architecture dependent optimization */
2506 value = arch_dep_replace_div_by_const(n);
2514 /* Turn Div into a tuple (mem, jmp, bad, value) */
2515 mem = get_Div_mem(n);
2516 blk = get_irn_n(n, -1);
2518 /* skip a potential Pin */
2520 mem = get_Pin_op(mem);
2521 turn_into_tuple(n, pn_Div_max);
2522 set_Tuple_pred(n, pn_Div_M, mem);
2523 set_Tuple_pred(n, pn_Div_X_regular, new_r_Jmp(current_ir_graph, blk));
2524 set_Tuple_pred(n, pn_Div_X_except, new_Bad());
2525 set_Tuple_pred(n, pn_Div_res, value);
2528 } /* transform_node_Div */
2531 * Transform a Mod node.
2533 static ir_node *transform_node_Mod(ir_node *n) {
2534 tarval *tv = value_of(n);
2535 ir_mode *mode = get_Mod_resmode(n);
2538 if (tv != tarval_bad) {
2539 value = new_Const(get_tarval_mode(tv), tv);
2541 DBG_OPT_CSTEVAL(n, value);
2544 ir_node *a = get_Mod_left(n);
2545 ir_node *b = get_Mod_right(n);
2548 if (a == b && value_not_zero(a, &dummy)) {
2549 /* BEWARE: we can optimize a%a to 0 only if this cannot cause a exception */
2550 value = new_Const(mode, get_mode_null(mode));
2551 DBG_OPT_CSTEVAL(n, value);
2554 if (mode_is_signed(mode) && is_Const(b)) {
2555 tarval *tv = get_Const_tarval(b);
2557 if (tv == get_mode_minus_one(mode)) {
2559 value = new_Const(mode, get_mode_null(mode));
2560 DBG_OPT_CSTEVAL(n, value);
2564 /* Try architecture dependent optimization */
2565 value = arch_dep_replace_mod_by_const(n);
2573 /* Turn Mod into a tuple (mem, jmp, bad, value) */
2574 mem = get_Mod_mem(n);
2575 blk = get_irn_n(n, -1);
2577 /* skip a potential Pin */
2579 mem = get_Pin_op(mem);
2580 turn_into_tuple(n, pn_Mod_max);
2581 set_Tuple_pred(n, pn_Mod_M, mem);
2582 set_Tuple_pred(n, pn_Mod_X_regular, new_r_Jmp(current_ir_graph, blk));
2583 set_Tuple_pred(n, pn_Mod_X_except, new_Bad());
2584 set_Tuple_pred(n, pn_Mod_res, value);
2587 } /* transform_node_Mod */
2590 * Transform a DivMod node.
2592 static ir_node *transform_node_DivMod(ir_node *n) {
2594 ir_node *a = get_DivMod_left(n);
2595 ir_node *b = get_DivMod_right(n);
2596 ir_mode *mode = get_DivMod_resmode(n);
2597 tarval *ta = value_of(a);
2598 tarval *tb = value_of(b);
2601 if (tb != tarval_bad) {
2602 if (tb == get_mode_one(get_tarval_mode(tb))) {
2603 b = new_Const(mode, get_mode_null(mode));
2604 DBG_OPT_CSTEVAL(n, b);
2606 } else if (ta != tarval_bad) {
2607 tarval *resa, *resb;
2608 resa = tarval_div(ta, tb);
2609 if (resa == tarval_bad) return n; /* Causes exception!!! Model by replacing through
2610 Jmp for X result!? */
2611 resb = tarval_mod(ta, tb);
2612 if (resb == tarval_bad) return n; /* Causes exception! */
2613 a = new_Const(mode, resa);
2614 b = new_Const(mode, resb);
2615 DBG_OPT_CSTEVAL(n, a);
2616 DBG_OPT_CSTEVAL(n, b);
2618 } else if (mode_is_signed(mode) && tb == get_mode_minus_one(mode)) {
2619 a = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1), a, mode);
2620 b = new_Const(mode, get_mode_null(mode));
2621 DBG_OPT_CSTEVAL(n, a);
2622 DBG_OPT_CSTEVAL(n, b);
2624 } else { /* Try architecture dependent optimization */
2625 arch_dep_replace_divmod_by_const(&a, &b, n);
2626 evaluated = a != NULL;
2628 } else if (a == b) {
2629 if (value_not_zero(a, &dummy)) {
2631 a = new_Const(mode, get_mode_one(mode));
2632 b = new_Const(mode, get_mode_null(mode));
2633 DBG_OPT_CSTEVAL(n, a);
2634 DBG_OPT_CSTEVAL(n, b);
2637 /* BEWARE: it is NOT possible to optimize a/a to 1, as this may cause a exception */
2640 } else if (ta == get_mode_null(mode) && value_not_zero(b, &dummy)) {
2641 /* 0 / non-Const = 0 */
2646 if (evaluated) { /* replace by tuple */
2650 mem = get_DivMod_mem(n);
2651 /* skip a potential Pin */
2653 mem = get_Pin_op(mem);
2655 blk = get_irn_n(n, -1);
2656 turn_into_tuple(n, pn_DivMod_max);
2657 set_Tuple_pred(n, pn_DivMod_M, mem);
2658 set_Tuple_pred(n, pn_DivMod_X_regular, new_r_Jmp(current_ir_graph, blk));
2659 set_Tuple_pred(n, pn_DivMod_X_except, new_Bad()); /* no exception */
2660 set_Tuple_pred(n, pn_DivMod_res_div, a);
2661 set_Tuple_pred(n, pn_DivMod_res_mod, b);
2665 } /* transform_node_DivMod */
2668 * Optimize x / c to x * (1/c)
2670 static ir_node *transform_node_Quot(ir_node *n) {
2671 ir_mode *mode = get_Quot_resmode(n);
2674 if (get_mode_arithmetic(mode) == irma_ieee754) {
2675 ir_node *b = get_Quot_right(n);
2678 tarval *tv = get_Const_tarval(b);
2680 tv = tarval_quo(get_mode_one(mode), tv);
2682 /* Do the transformation if the result is either exact or we are not
2683 using strict rules. */
2684 if (tv != tarval_bad &&
2685 (tarval_ieee754_get_exact() || (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic) == 0)) {
2686 ir_node *blk = get_irn_n(n, -1);
2687 ir_node *c = new_r_Const(current_ir_graph, blk, mode, tv);
2688 ir_node *a = get_Quot_left(n);
2689 ir_node *m = new_rd_Mul(get_irn_dbg_info(n), current_ir_graph, blk, a, c, mode);
2690 ir_node *mem = get_Quot_mem(n);
2692 /* skip a potential Pin */
2694 mem = get_Pin_op(mem);
2695 turn_into_tuple(n, pn_Quot_max);
2696 set_Tuple_pred(n, pn_Quot_M, mem);
2697 set_Tuple_pred(n, pn_Quot_X_regular, new_r_Jmp(current_ir_graph, blk));
2698 set_Tuple_pred(n, pn_Quot_X_except, new_r_Bad(current_ir_graph));
2699 set_Tuple_pred(n, pn_Quot_res, m);
2700 DBG_OPT_ALGSIM1(oldn, a, b, m, FS_OPT_FP_INV_MUL);
2705 } /* transform_node_Quot */
2708 * Optimize Abs(x) into x if x is Confirmed >= 0
2709 * Optimize Abs(x) into -x if x is Confirmed <= 0
2711 static ir_node *transform_node_Abs(ir_node *n) {
2713 ir_node *a = get_Abs_op(n);
2714 value_classify_sign sign = classify_value_sign(a);
2716 if (sign == value_classified_negative) {
2717 ir_mode *mode = get_irn_mode(n);
2720 * We can replace the Abs by -x here.
2721 * We even could add a new Confirm here.
2723 * Note that -x would create a new node, so we could
2724 * not run it in the equivalent_node() context.
2726 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph,
2727 get_irn_n(n, -1), a, mode);
2729 DBG_OPT_CONFIRM(oldn, n);
2730 } else if (sign == value_classified_positive) {
2731 /* n is positive, Abs is not needed */
2734 DBG_OPT_CONFIRM(oldn, n);
2738 } /* transform_node_Abs */
2741 * Transform a Cond node.
2743 * Replace the Cond by a Jmp if it branches on a constant
2746 static ir_node *transform_node_Cond(ir_node *n) {
2749 ir_node *a = get_Cond_selector(n);
2750 tarval *ta = value_of(a);
2752 /* we need block info which is not available in floating irgs */
2753 if (get_irg_pinned(current_ir_graph) == op_pin_state_floats)
2756 if ((ta != tarval_bad) &&
2757 (get_irn_mode(a) == mode_b) &&
2758 (get_opt_unreachable_code())) {
2759 /* It's a boolean Cond, branching on a boolean constant.
2760 Replace it by a tuple (Bad, Jmp) or (Jmp, Bad) */
2761 jmp = new_r_Jmp(current_ir_graph, get_nodes_block(n));
2762 turn_into_tuple(n, pn_Cond_max);
2763 if (ta == tarval_b_true) {
2764 set_Tuple_pred(n, pn_Cond_false, new_Bad());
2765 set_Tuple_pred(n, pn_Cond_true, jmp);
2767 set_Tuple_pred(n, pn_Cond_false, jmp);
2768 set_Tuple_pred(n, pn_Cond_true, new_Bad());
2770 /* We might generate an endless loop, so keep it alive. */
2771 add_End_keepalive(get_irg_end(current_ir_graph), get_nodes_block(n));
2774 } /* transform_node_Cond */
2776 typedef ir_node* (*recursive_transform) (ir_node *n);
2779 * makes use of distributive laws for and, or, eor
2780 * and(a OP c, b OP c) -> and(a, b) OP c
2781 * note, might return a different op than n
2783 static ir_node *transform_bitwise_distributive(ir_node *n,
2784 recursive_transform trans_func)
2787 ir_node *a = get_binop_left(n);
2788 ir_node *b = get_binop_right(n);
2789 ir_op *op = get_irn_op(a);
2790 ir_op *op_root = get_irn_op(n);
2792 if(op != get_irn_op(b))
2795 if (op == op_Conv) {
2796 ir_node *a_op = get_Conv_op(a);
2797 ir_node *b_op = get_Conv_op(b);
2798 ir_mode *a_mode = get_irn_mode(a_op);
2799 ir_mode *b_mode = get_irn_mode(b_op);
2800 if(a_mode == b_mode && (mode_is_int(a_mode) || a_mode == mode_b)) {
2801 ir_node *blk = get_irn_n(n, -1);
2804 set_binop_left(n, a_op);
2805 set_binop_right(n, b_op);
2806 set_irn_mode(n, a_mode);
2808 n = new_r_Conv(current_ir_graph, blk, n, get_irn_mode(oldn));
2810 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_SHIFT_AND);
2816 /* nothing to gain here */
2820 if (op == op_Shrs || op == op_Shr || op == op_Shl
2821 || op == op_And || op == op_Or || op == op_Eor) {
2822 ir_node *a_left = get_binop_left(a);
2823 ir_node *a_right = get_binop_right(a);
2824 ir_node *b_left = get_binop_left(b);
2825 ir_node *b_right = get_binop_right(b);
2829 if (is_op_commutative(op)) {
2830 if (a_left == b_left) {
2834 } else if(a_left == b_right) {
2838 } else if(a_right == b_left) {
2844 if(a_right == b_right) {
2851 /* (a sop c) & (b sop c) => (a & b) sop c */
2852 ir_node *blk = get_irn_n(n, -1);
2854 ir_node *new_n = exact_copy(n);
2855 set_binop_left(new_n, op1);
2856 set_binop_right(new_n, op2);
2857 new_n = trans_func(new_n);
2859 if(op_root == op_Eor && op == op_Or) {
2860 dbg_info *dbgi = get_irn_dbg_info(n);
2861 ir_graph *irg = current_ir_graph;
2862 ir_mode *mode = get_irn_mode(c);
2864 c = new_rd_Not(dbgi, irg, blk, c, mode);
2865 n = new_rd_And(dbgi, irg, blk, new_n, c, mode);
2868 set_irn_n(n, -1, blk);
2869 set_binop_left(n, new_n);
2870 set_binop_right(n, c);
2873 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_SHIFT_AND);
2884 static ir_node *transform_node_And(ir_node *n) {
2885 ir_node *c, *oldn = n;
2886 ir_node *a = get_And_left(n);
2887 ir_node *b = get_And_right(n);
2889 HANDLE_BINOP_PHI(tarval_and, a,b,c);
2891 /* we can evaluate 2 Projs of the same Cmp */
2892 if (get_irn_mode(n) == mode_b && is_Proj(a) && is_Proj(b)) {
2893 ir_node *pred_a = get_Proj_pred(a);
2894 ir_node *pred_b = get_Proj_pred(b);
2895 if(pred_a == pred_b) {
2896 dbg_info *dbgi = get_irn_dbg_info(n);
2897 ir_node *block = get_nodes_block(pred_a);
2898 pn_Cmp pn_a = get_Proj_proj(a);
2899 pn_Cmp pn_b = get_Proj_proj(b);
2900 /* yes, we can simply calculate with pncs */
2901 pn_Cmp new_pnc = pn_a & pn_b;
2903 return new_rd_Proj(dbgi, current_ir_graph, block, pred_a, mode_b,
2909 ir_node *op = get_Not_op(b);
2911 ir_node *ba = get_And_left(op);
2912 ir_node *bb = get_And_right(op);
2914 /* it's enough to test the following cases due to normalization! */
2915 if (get_Or_left(a) == ba && get_Or_right(a) == bb) {
2916 /* (a|b) & ~(a&b) = a^b */
2917 ir_node *block = get_nodes_block(n);
2919 n = new_rd_Eor(get_irn_dbg_info(n), current_ir_graph, block, ba, bb, get_irn_mode(n));
2920 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_TO_EOR);
2928 ir_node *op = get_Not_op(a);
2930 ir_node *aa = get_And_left(op);
2931 ir_node *ab = get_And_right(op);
2933 /* it's enough to test the following cases due to normalization! */
2934 if (get_Or_left(b) == aa && get_Or_right(b) == ab) {
2935 /* (a|b) & ~(a&b) = a^b */
2936 ir_node *block = get_nodes_block(n);
2938 n = new_rd_Eor(get_irn_dbg_info(n), current_ir_graph, block, aa, ab, get_irn_mode(n));
2939 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_TO_EOR);
2945 if (is_Not(a) && is_Not(b)) {
2946 /* ~a & ~b = ~(a|b) */
2947 ir_node *block = get_nodes_block(n);
2948 ir_mode *mode = get_irn_mode(n);
2952 n = new_rd_Or(get_irn_dbg_info(n), current_ir_graph, block, a, b, mode);
2953 n = new_rd_Not(get_irn_dbg_info(n), current_ir_graph, block, n, mode);
2954 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_DEMORGAN);
2958 n = transform_bitwise_distributive(n, transform_node_And);
2961 } /* transform_node_And */
2966 static ir_node *transform_node_Eor(ir_node *n) {
2967 ir_node *c, *oldn = n;
2968 ir_node *a = get_Eor_left(n);
2969 ir_node *b = get_Eor_right(n);
2970 ir_mode *mode = get_irn_mode(n);
2972 HANDLE_BINOP_PHI(tarval_eor, a,b,c);
2974 /* we can evaluate 2 Projs of the same Cmp */
2975 if(get_irn_mode(n) == mode_b && is_Proj(a) && is_Proj(b)) {
2976 ir_node *pred_a = get_Proj_pred(a);
2977 ir_node *pred_b = get_Proj_pred(b);
2978 if(pred_a == pred_b) {
2979 dbg_info *dbgi = get_irn_dbg_info(n);
2980 ir_node *block = get_nodes_block(pred_a);
2981 pn_Cmp pn_a = get_Proj_proj(a);
2982 pn_Cmp pn_b = get_Proj_proj(b);
2983 /* yes, we can simply calculate with pncs */
2984 pn_Cmp new_pnc = pn_a ^ pn_b;
2986 return new_rd_Proj(dbgi, current_ir_graph, block, pred_a, mode_b,
2993 n = new_rd_Const(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1),
2994 mode, get_mode_null(mode));
2995 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_A_A);
2996 } else if ((mode == mode_b)
2997 && (get_irn_op(a) == op_Proj)
2998 && (get_irn_mode(a) == mode_b)
2999 && (classify_tarval (value_of(b)) == TV_CLASSIFY_ONE)
3000 && (get_irn_op(get_Proj_pred(a)) == op_Cmp)) {
3001 /* The Eor negates a Cmp. The Cmp has the negated result anyways! */
3002 n = new_r_Proj(current_ir_graph, get_irn_n(n, -1), get_Proj_pred(a),
3003 mode_b, get_negated_pnc(get_Proj_proj(a), mode));
3005 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT_BOOL);
3006 } else if ((mode == mode_b)
3007 && (classify_tarval (value_of(b)) == TV_CLASSIFY_ONE)) {
3008 /* The Eor is a Not. Replace it by a Not. */
3009 /* ????!!!Extend to bitfield 1111111. */
3010 n = new_r_Not(current_ir_graph, get_irn_n(n, -1), a, mode_b);
3012 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3014 n = transform_bitwise_distributive(n, transform_node_Eor);
3018 } /* transform_node_Eor */
3023 static ir_node *transform_node_Not(ir_node *n) {
3024 ir_node *c, *oldn = n;
3025 ir_node *a = get_Not_op(n);
3026 ir_op *op_a = get_irn_op(a);
3028 HANDLE_UNOP_PHI(tarval_not,a,c);
3030 /* check for a boolean Not */
3031 if ( (get_irn_mode(n) == mode_b)
3032 && (op_a == op_Proj)
3033 && (get_irn_mode(a) == mode_b)
3034 && (get_irn_op(get_Proj_pred(a)) == op_Cmp)) {
3035 /* We negate a Cmp. The Cmp has the negated result anyways! */
3036 n = new_r_Proj(current_ir_graph, get_irn_n(n, -1), get_Proj_pred(a),
3037 mode_b, get_negated_pnc(get_Proj_proj(a), mode_b));
3038 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_CMP);
3041 if (op_a == op_Sub && classify_Const(get_Sub_right(a)) == CNST_ONE) {
3043 ir_node *op = get_Sub_left(a);
3044 ir_node *blk = get_irn_n(n, -1);
3045 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, blk, op, get_irn_mode(n));
3046 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_MINUS_1);
3049 } /* transform_node_Not */
3052 * Transform a Minus.
3057 static ir_node *transform_node_Minus(ir_node *n) {
3058 ir_node *c, *oldn = n;
3059 ir_node *a = get_Minus_op(n);
3062 HANDLE_UNOP_PHI(tarval_neg,a,c);
3064 mode = get_irn_mode(a);
3065 if (get_mode_arithmetic(mode) == irma_twos_complement) {
3066 /* the following rules are only to twos-complement */
3069 ir_node *op = get_Not_op(a);
3070 tarval *tv = get_mode_one(mode);
3071 ir_node *blk = get_irn_n(n, -1);
3072 ir_node *c = new_r_Const(current_ir_graph, blk, mode, tv);
3073 n = new_rd_Add(get_irn_dbg_info(n), current_ir_graph, blk, op, c, mode);
3074 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_MINUS_NOT);
3078 ir_node *c = get_Shr_right(a);
3081 tarval *tv = get_Const_tarval(c);
3083 if (tarval_is_long(tv) && get_tarval_long(tv) == get_mode_size_bits(mode) - 1) {
3084 /* -(a >>u (size-1)) = a >>s (size-1) */
3085 ir_node *v = get_Shr_left(a);
3087 n = new_rd_Shrs(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1), v, c, mode);
3088 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_PREDICATE);
3094 ir_node *c = get_Shrs_right(a);
3097 tarval *tv = get_Const_tarval(c);
3099 if (tarval_is_long(tv) && get_tarval_long(tv) == get_mode_size_bits(mode) - 1) {
3100 /* -(a >>s (size-1)) = a >>u (size-1) */
3101 ir_node *v = get_Shrs_left(a);
3103 n = new_rd_Shr(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1), v, c, mode);
3104 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_PREDICATE);
3111 /* - (a-b) = b - a */
3112 ir_node *la = get_Sub_left(a);
3113 ir_node *ra = get_Sub_right(a);
3114 ir_node *blk = get_irn_n(n, -1);
3116 n = new_rd_Sub(get_irn_dbg_info(n), current_ir_graph, blk, ra, la, mode);
3117 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_MINUS_SUB);
3122 } /* transform_node_Minus */
3125 * Transform a Cast_type(Const) into a new Const_type
3127 static ir_node *transform_node_Cast(ir_node *n) {
3129 ir_node *pred = get_Cast_op(n);
3130 ir_type *tp = get_irn_type(n);
3132 if (get_irn_op(pred) == op_Const && get_Const_type(pred) != tp) {
3133 n = new_rd_Const_type(NULL, current_ir_graph, get_irn_n(pred, -1), get_irn_mode(pred),
3134 get_Const_tarval(pred), tp);
3135 DBG_OPT_CSTEVAL(oldn, n);
3136 } else if ((get_irn_op(pred) == op_SymConst) && (get_SymConst_value_type(pred) != tp)) {
3137 n = new_rd_SymConst_type(NULL, current_ir_graph, get_irn_n(pred, -1), get_SymConst_symbol(pred),
3138 get_SymConst_kind(pred), tp);
3139 DBG_OPT_CSTEVAL(oldn, n);
3143 } /* transform_node_Cast */
3146 * Transform a Proj(Div) with a non-zero value.
3147 * Removes the exceptions and routes the memory to the NoMem node.
3149 static ir_node *transform_node_Proj_Div(ir_node *proj) {
3150 ir_node *div = get_Proj_pred(proj);
3151 ir_node *b = get_Div_right(div);
3152 ir_node *confirm, *res, *new_mem;
3155 if (value_not_zero(b, &confirm)) {
3156 /* div(x, y) && y != 0 */
3157 proj_nr = get_Proj_proj(proj);
3159 case pn_Div_X_regular:
3160 return new_r_Jmp(current_ir_graph, get_irn_n(div, -1));
3162 case pn_Div_X_except:
3163 /* we found an exception handler, remove it */
3164 DBG_OPT_EXC_REM(proj);
3168 res = get_Div_mem(div);
3169 new_mem = get_irg_no_mem(current_ir_graph);
3172 /* This node can only float up to the Confirm block */
3173 new_mem = new_r_Pin(current_ir_graph, get_nodes_block(confirm), new_mem);
3175 set_irn_pinned(div, op_pin_state_floats);
3176 /* this is a Div without exception, we can remove the memory edge */
3177 set_Div_mem(div, new_mem);
3182 } /* transform_node_Proj_Div */
3185 * Transform a Proj(Mod) with a non-zero value.
3186 * Removes the exceptions and routes the memory to the NoMem node.
3188 static ir_node *transform_node_Proj_Mod(ir_node *proj) {
3189 ir_node *mod = get_Proj_pred(proj);
3190 ir_node *b = get_Mod_right(mod);
3191 ir_node *confirm, *res, *new_mem;
3194 if (value_not_zero(b, &confirm)) {
3195 /* mod(x, y) && y != 0 */
3196 proj_nr = get_Proj_proj(proj);
3200 case pn_Mod_X_regular:
3201 return new_r_Jmp(current_ir_graph, get_irn_n(mod, -1));
3203 case pn_Mod_X_except:
3204 /* we found an exception handler, remove it */
3205 DBG_OPT_EXC_REM(proj);
3209 res = get_Mod_mem(mod);
3210 new_mem = get_irg_no_mem(current_ir_graph);
3213 /* This node can only float up to the Confirm block */
3214 new_mem = new_r_Pin(current_ir_graph, get_nodes_block(confirm), new_mem);
3216 set_irn_pinned(mod, op_pin_state_floats);
3217 /* this is a Mod without exception, we can remove the memory edge */
3218 set_Mod_mem(mod, get_irg_no_mem(current_ir_graph));
3221 if (get_Mod_left(mod) == b) {
3222 /* a % a = 0 if a != 0 */
3223 ir_mode *mode = get_irn_mode(proj);
3224 ir_node *res = new_Const(mode, get_mode_null(mode));
3226 DBG_OPT_CSTEVAL(mod, res);
3232 } /* transform_node_Proj_Mod */
3235 * Transform a Proj(DivMod) with a non-zero value.
3236 * Removes the exceptions and routes the memory to the NoMem node.
3238 static ir_node *transform_node_Proj_DivMod(ir_node *proj) {
3239 ir_node *divmod = get_Proj_pred(proj);
3240 ir_node *b = get_DivMod_right(divmod);
3241 ir_node *confirm, *res, *new_mem;
3244 if (value_not_zero(b, &confirm)) {
3245 /* DivMod(x, y) && y != 0 */
3246 proj_nr = get_Proj_proj(proj);
3250 case pn_DivMod_X_regular:
3251 return new_r_Jmp(current_ir_graph, get_irn_n(divmod, -1));
3253 case pn_DivMod_X_except:
3254 /* we found an exception handler, remove it */
3255 DBG_OPT_EXC_REM(proj);
3259 res = get_DivMod_mem(divmod);
3260 new_mem = get_irg_no_mem(current_ir_graph);
3263 /* This node can only float up to the Confirm block */
3264 new_mem = new_r_Pin(current_ir_graph, get_nodes_block(confirm), new_mem);
3266 set_irn_pinned(divmod, op_pin_state_floats);
3267 /* this is a DivMod without exception, we can remove the memory edge */
3268 set_DivMod_mem(divmod, get_irg_no_mem(current_ir_graph));
3271 case pn_DivMod_res_mod:
3272 if (get_DivMod_left(divmod) == b) {
3273 /* a % a = 0 if a != 0 */
3274 ir_mode *mode = get_irn_mode(proj);
3275 ir_node *res = new_Const(mode, get_mode_null(mode));
3277 DBG_OPT_CSTEVAL(divmod, res);
3283 } /* transform_node_Proj_DivMod */
3286 * Optimizes jump tables (CondIs or CondIu) by removing all impossible cases.
3288 static ir_node *transform_node_Proj_Cond(ir_node *proj) {
3289 if (get_opt_unreachable_code()) {
3290 ir_node *n = get_Proj_pred(proj);
3291 ir_node *b = get_Cond_selector(n);
3293 if (mode_is_int(get_irn_mode(b))) {
3294 tarval *tb = value_of(b);
3296 if (tb != tarval_bad) {
3297 /* we have a constant switch */
3298 long num = get_Proj_proj(proj);
3300 if (num != get_Cond_defaultProj(n)) { /* we cannot optimize default Proj's yet */
3301 if (get_tarval_long(tb) == num) {
3302 /* Do NOT create a jump here, or we will have 2 control flow ops
3303 * in a block. This case is optimized away in optimize_cf(). */
3306 /* this case will NEVER be taken, kill it */
3314 } /* transform_node_Proj_Cond */
3317 * Normalizes and optimizes Cmp nodes.
3319 static ir_node *transform_node_Proj_Cmp(ir_node *proj) {
3320 ir_node *n = get_Proj_pred(proj);
3321 ir_node *left = get_Cmp_left(n);
3322 ir_node *right = get_Cmp_right(n);
3326 ir_mode *mode = NULL;
3327 long proj_nr = get_Proj_proj(proj);
3329 /* we can evaluate this direct */
3332 return new_Const(mode_b, get_tarval_b_false());
3334 return new_Const(mode_b, get_tarval_b_true());
3336 if(!mode_is_float(get_irn_mode(left)))
3337 return new_Const(mode_b, get_tarval_b_true());
3343 /* Remove unnecessary conversions */
3344 /* TODO handle constants */
3345 if (is_Conv(left) && is_Conv(right)) {
3346 ir_mode *mode = get_irn_mode(left);
3347 ir_node *op_left = get_Conv_op(left);
3348 ir_node *op_right = get_Conv_op(right);
3349 ir_mode *mode_left = get_irn_mode(op_left);
3350 ir_mode *mode_right = get_irn_mode(op_right);
3352 if (smaller_mode(mode_left, mode) && smaller_mode(mode_right, mode)) {
3353 ir_graph *irg = current_ir_graph;
3354 ir_node *block = get_nodes_block(n);
3356 if (mode_left == mode_right) {
3360 } else if (smaller_mode(mode_left, mode_right)) {
3361 left = new_r_Conv(irg, block, op_left, mode_right);
3364 } else if (smaller_mode(mode_right, mode_left)) {
3366 right = new_r_Conv(irg, block, op_right, mode_left);
3374 left = get_Cast_op(left);
3376 right = get_Cast_op(right);
3378 /* remove operation of both sides if possible */
3379 if (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) {
3380 ir_opcode lop = get_irn_opcode(left);
3382 if (lop == get_irn_opcode(right)) {
3383 ir_node *ll, *lr, *rl, *rr;
3385 /* same operation on both sides, try to remove */
3389 /* ~a CMP ~b => a CMP b, -a CMP -b ==> a CMP b */
3390 left = get_unop_op(left);
3391 right = get_unop_op(right);
3395 ll = get_Add_left(left);
3396 lr = get_Add_right(left);
3397 rl = get_Add_left(right);
3398 rr = get_Add_right(right);
3401 /* X + a CMP X + b ==> a CMP b */
3405 } else if (ll == rr) {
3406 /* X + a CMP b + X ==> a CMP b */
3410 } else if (lr == rl) {
3411 /* a + X CMP X + b ==> a CMP b */
3415 } else if (lr == rr) {
3416 /* a + X CMP b + X ==> a CMP b */
3423 ll = get_Sub_left(left);
3424 lr = get_Sub_right(left);
3425 rl = get_Sub_left(right);
3426 rr = get_Sub_right(right);
3429 /* X - a CMP X - b ==> a CMP b */
3433 } else if (lr == rr) {
3434 /* a - X CMP b - X ==> a CMP b */
3441 if (get_Rot_right(left) == get_Rot_right(right)) {
3442 /* a ROT X CMP b ROT X */
3443 left = get_Rot_left(left);
3444 right = get_Rot_left(right);
3455 if (get_irn_mode(left) == mode_b) {
3456 ir_graph *irg = current_ir_graph;
3457 ir_node *block = get_nodes_block(n);
3460 case pn_Cmp_Le: return new_r_Or( irg, block, new_r_Not(irg, block, left, mode_b), right, mode_b);
3461 case pn_Cmp_Lt: return new_r_And(irg, block, new_r_Not(irg, block, left, mode_b), right, mode_b);
3462 case pn_Cmp_Ge: return new_r_Or( irg, block, left, new_r_Not(irg, block, right, mode_b), mode_b);
3463 case pn_Cmp_Gt: return new_r_And(irg, block, left, new_r_Not(irg, block, right, mode_b), mode_b);
3464 case pn_Cmp_Lg: return new_r_Eor(irg, block, left, right, mode_b);
3465 case pn_Cmp_Eq: return new_r_Not(irg, block, new_r_Eor(irg, block, left, right, mode_b), mode_b);
3469 if (!get_opt_reassociation())
3473 * First step: normalize the compare op
3474 * by placing the constant on the right side
3475 * or moving the lower address node to the left.
3476 * We ignore the case that both are constants
3477 * this case should be optimized away.
3479 if (is_Const(right)) {
3481 } else if (is_Const(left)) {
3486 proj_nr = get_inversed_pnc(proj_nr);
3488 } else if (get_irn_idx(left) > get_irn_idx(right)) {
3494 proj_nr = get_inversed_pnc(proj_nr);
3499 * Second step: Try to reduce the magnitude
3500 * of a constant. This may help to generate better code
3501 * later and may help to normalize more compares.
3502 * Of course this is only possible for integer values.
3505 mode = get_irn_mode(c);
3506 tv = get_Const_tarval(c);
3508 if (tv != tarval_bad) {
3509 /* the following optimization is possible on modes without Overflow
3510 * on Unary Minus or on == and !=:
3511 * -a CMP c ==> a swap(CMP) -c
3513 * Beware: for two-complement Overflow may occur, so only == and != can
3514 * be optimized, see this:
3515 * -MININT < 0 =/=> MININT > 0 !!!
3517 if (is_Minus(left) &&
3518 (!mode_overflow_on_unary_Minus(mode) ||
3519 (mode_is_int(mode) && (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg)))) {
3520 tv = tarval_neg(tv);
3522 if (tv != tarval_bad) {
3523 left = get_Minus_op(left);
3524 proj_nr = get_inversed_pnc(proj_nr);
3527 } else if (is_Not(left) && (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg)) {
3528 tv = tarval_not(tv);
3530 if (tv != tarval_bad) {
3531 left = get_Not_op(left);
3536 /* for integer modes, we have more */
3537 if (mode_is_int(mode)) {
3538 /* Ne includes Unordered which is not possible on integers.
3539 * However, frontends often use this wrong, so fix it here */
3540 if (proj_nr & pn_Cmp_Uo) {
3541 proj_nr &= ~pn_Cmp_Uo;
3542 set_Proj_proj(proj, proj_nr);
3545 /* c > 0 : a < c ==> a <= (c-1) a >= c ==> a > (c-1) */
3546 if ((proj_nr == pn_Cmp_Lt || proj_nr == pn_Cmp_Ge) &&
3547 tarval_cmp(tv, get_mode_null(mode)) == pn_Cmp_Gt) {
3548 tv = tarval_sub(tv, get_mode_one(mode));
3550 if (tv != tarval_bad) {
3551 proj_nr ^= pn_Cmp_Eq;
3555 /* c < 0 : a > c ==> a >= (c+1) a <= c ==> a < (c+1) */
3556 else if ((proj_nr == pn_Cmp_Gt || proj_nr == pn_Cmp_Le) &&
3557 tarval_cmp(tv, get_mode_null(mode)) == pn_Cmp_Lt) {
3558 tv = tarval_add(tv, get_mode_one(mode));
3560 if (tv != tarval_bad) {
3561 proj_nr ^= pn_Cmp_Eq;
3566 /* the following reassociations work only for == and != */
3567 if (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) {
3569 /* a-b == 0 ==> a == b, a-b != 0 ==> a != b */
3570 if (classify_tarval(tv) == TV_CLASSIFY_NULL && is_Sub(left)) {
3571 right =get_Sub_right(left);
3572 left = get_Sub_left(left);
3574 tv = value_of(right);
3578 if (tv != tarval_bad) {
3579 ir_op *op = get_irn_op(left);
3581 /* a-c1 == c2 ==> a == c2+c1, a-c1 != c2 ==> a != c2+c1 */
3583 ir_node *c1 = get_Sub_right(left);
3584 tarval *tv2 = value_of(c1);
3586 if (tv2 != tarval_bad) {
3587 tv2 = tarval_add(tv, value_of(c1));
3589 if (tv2 != tarval_bad) {
3590 left = get_Sub_left(left);
3596 /* a+c1 == c2 ==> a == c2-c1, a+c1 != c2 ==> a != c2-c1 */
3597 else if (op == op_Add) {
3598 ir_node *a_l = get_Add_left(left);
3599 ir_node *a_r = get_Add_right(left);
3603 if (get_irn_op(a_l) == op_Const) {
3605 tv2 = value_of(a_l);
3608 tv2 = value_of(a_r);
3611 if (tv2 != tarval_bad) {
3612 tv2 = tarval_sub(tv, tv2);
3614 if (tv2 != tarval_bad) {
3621 /* -a == c ==> a == -c, -a != c ==> a != -c */
3622 else if (op == op_Minus) {
3623 tarval *tv2 = tarval_sub(get_mode_null(mode), tv);
3625 if (tv2 != tarval_bad) {
3626 left = get_Minus_op(left);
3633 /* the following reassociations work only for <= */
3634 else if (proj_nr == pn_Cmp_Le || proj_nr == pn_Cmp_Lt) {
3635 if (tv != tarval_bad) {
3636 ir_op *op = get_irn_op(left);
3638 /* c >= 0 : Abs(a) <= c ==> (unsigned)(a + c) <= 2*c */
3646 * optimization for AND:
3648 * And(x, C) == C ==> And(x, C) != 0
3649 * And(x, C) != C ==> And(X, C) == 0
3651 * if C is a single Bit constant.
3653 if ((proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) && is_And(left)) {
3654 if (tarval_is_single_bit(tv)) {
3655 /* check for Constant's match. We have check hare the tarvals,
3656 because our const might be changed */
3657 ir_node *la = get_And_left(left);
3658 ir_node *ra = get_And_right(left);
3659 if ((is_Const(la) && get_Const_tarval(la) == tv) ||
3660 (is_Const(ra) && get_Const_tarval(ra) == tv)) {
3661 /* fine: do the transformation */
3662 tv = get_mode_null(get_tarval_mode(tv));
3663 proj_nr ^= pn_Cmp_Leg;
3668 } /* tarval != bad */
3672 ir_node *block = get_irn_n(n, -1); /* Beware of get_nodes_Block() */
3674 if (changed & 2) /* need a new Const */
3675 right = new_Const(mode, tv);
3677 /* create a new compare */
3678 n = new_rd_Cmp(get_irn_dbg_info(n), current_ir_graph, block, left, right);
3680 set_Proj_pred(proj, n);
3681 set_Proj_proj(proj, proj_nr);
3685 } /* transform_node_Proj_Cmp */
3688 * Does all optimizations on nodes that must be done on it's Proj's
3689 * because of creating new nodes.
3691 static ir_node *transform_node_Proj(ir_node *proj) {
3692 ir_node *n = get_Proj_pred(proj);
3694 switch (get_irn_opcode(n)) {
3696 return transform_node_Proj_Div(proj);
3699 return transform_node_Proj_Mod(proj);
3702 return transform_node_Proj_DivMod(proj);
3705 return transform_node_Proj_Cond(proj);
3708 return transform_node_Proj_Cmp(proj);
3711 /* should not happen, but if it does will be optimized away */
3712 return equivalent_node_Proj(proj);
3718 } /* transform_node_Proj */
3721 * Move Confirms down through Phi nodes.
3723 static ir_node *transform_node_Phi(ir_node *phi) {
3725 ir_mode *mode = get_irn_mode(phi);
3727 if (mode_is_reference(mode)) {
3728 n = get_irn_arity(phi);
3730 /* Beware of Phi0 */
3732 ir_node *pred = get_irn_n(phi, 0);
3733 ir_node *bound, *new_Phi, *block, **in;
3736 if (! is_Confirm(pred))
3739 bound = get_Confirm_bound(pred);
3740 pnc = get_Confirm_cmp(pred);
3742 NEW_ARR_A(ir_node *, in, n);
3743 in[0] = get_Confirm_value(pred);
3745 for (i = 1; i < n; ++i) {
3746 pred = get_irn_n(phi, i);
3748 if (! is_Confirm(pred) ||
3749 get_Confirm_bound(pred) != bound ||
3750 get_Confirm_cmp(pred) != pnc)
3752 in[i] = get_Confirm_value(pred);
3754 /* move the Confirm nodes "behind" the Phi */
3755 block = get_irn_n(phi, -1);
3756 new_Phi = new_r_Phi(current_ir_graph, block, n, in, get_irn_mode(phi));
3757 return new_r_Confirm(current_ir_graph, block, new_Phi, bound, pnc);
3761 } /* transform_node_Phi */
3764 * Returns the operands of a commutative bin-op, if one operand is
3765 * a const, it is returned as the second one.
3767 static void get_comm_Binop_Ops(ir_node *binop, ir_node **a, ir_node **c) {
3768 ir_node *op_a = get_binop_left(binop);
3769 ir_node *op_b = get_binop_right(binop);
3771 assert(is_op_commutative(get_irn_op(binop)));
3773 if (get_irn_op(op_a) == op_Const) {
3780 } /* get_comm_Binop_Ops */
3783 * Optimize a Or(And(Or(And(v,c4),c3),c2),c1) pattern if possible.
3784 * Such pattern may arise in bitfield stores.
3786 * value c4 value c4 & c2
3787 * AND c3 AND c1 | c3
3794 * AND c1 ===> OR if (c1 | c2) == 0x111..11
3797 static ir_node *transform_node_Or_bf_store(ir_node *or) {
3800 ir_node *and_l, *c3;
3801 ir_node *value, *c4;
3802 ir_node *new_and, *new_const, *block;
3803 ir_mode *mode = get_irn_mode(or);
3805 tarval *tv1, *tv2, *tv3, *tv4, *tv, *n_tv4, *n_tv2;
3808 get_comm_Binop_Ops(or, &and, &c1);
3809 if (!is_Const(c1) || !is_And(and))
3812 get_comm_Binop_Ops(and, &or_l, &c2);
3816 tv1 = get_Const_tarval(c1);
3817 tv2 = get_Const_tarval(c2);
3819 tv = tarval_or(tv1, tv2);
3820 if (classify_tarval(tv) == TV_CLASSIFY_ALL_ONE) {
3821 /* the AND does NOT clear a bit with isn't set be the OR */
3822 set_Or_left(or, or_l);
3823 set_Or_right(or, c1);
3825 /* check for more */
3832 get_comm_Binop_Ops(or_l, &and_l, &c3);
3833 if (!is_Const(c3) || !is_And(and_l))
3836 get_comm_Binop_Ops(and_l, &value, &c4);
3840 /* ok, found the pattern, check for conditions */
3841 assert(mode == get_irn_mode(and));
3842 assert(mode == get_irn_mode(or_l));
3843 assert(mode == get_irn_mode(and_l));
3845 tv3 = get_Const_tarval(c3);
3846 tv4 = get_Const_tarval(c4);
3848 tv = tarval_or(tv4, tv2);
3849 if (classify_tarval(tv) != TV_CLASSIFY_ALL_ONE) {
3850 /* have at least one 0 at the same bit position */
3854 n_tv4 = tarval_not(tv4);
3855 if (tv3 != tarval_and(tv3, n_tv4)) {
3856 /* bit in the or_mask is outside the and_mask */
3860 n_tv2 = tarval_not(tv2);
3861 if (tv1 != tarval_and(tv1, n_tv2)) {
3862 /* bit in the or_mask is outside the and_mask */
3866 /* ok, all conditions met */
3867 block = get_irn_n(or, -1);
3869 new_and = new_r_And(current_ir_graph, block,
3870 value, new_r_Const(current_ir_graph, block, mode, tarval_and(tv4, tv2)), mode);
3872 new_const = new_r_Const(current_ir_graph, block, mode, tarval_or(tv3, tv1));
3874 set_Or_left(or, new_and);
3875 set_Or_right(or, new_const);
3877 /* check for more */
3879 } /* transform_node_Or_bf_store */
3882 * Optimize an Or(shl(x, c), shr(x, bits - c)) into a Rot
3884 static ir_node *transform_node_Or_Rot(ir_node *or) {
3885 ir_mode *mode = get_irn_mode(or);
3886 ir_node *shl, *shr, *block;
3887 ir_node *irn, *x, *c1, *c2, *v, *sub, *n;
3890 if (! mode_is_int(mode))
3893 shl = get_binop_left(or);
3894 shr = get_binop_right(or);
3896 if (get_irn_op(shl) == op_Shr) {
3897 if (get_irn_op(shr) != op_Shl)
3903 } else if (get_irn_op(shl) != op_Shl) {
3905 } else if (get_irn_op(shr) != op_Shr) {
3908 x = get_Shl_left(shl);
3909 if (x != get_Shr_left(shr))
3912 c1 = get_Shl_right(shl);
3913 c2 = get_Shr_right(shr);
3914 if (get_irn_op(c1) == op_Const && get_irn_op(c2) == op_Const) {
3915 tv1 = get_Const_tarval(c1);
3916 if (! tarval_is_long(tv1))
3919 tv2 = get_Const_tarval(c2);
3920 if (! tarval_is_long(tv2))
3923 if (get_tarval_long(tv1) + get_tarval_long(tv2)
3924 != get_mode_size_bits(mode))
3927 /* yet, condition met */
3928 block = get_irn_n(or, -1);
3930 n = new_r_Rot(current_ir_graph, block, x, c1, mode);
3932 DBG_OPT_ALGSIM1(or, shl, shr, n, FS_OPT_OR_SHFT_TO_ROT);
3934 } else if (get_irn_op(c1) == op_Sub) {
3938 if (get_Sub_right(sub) != v)
3941 c1 = get_Sub_left(sub);
3942 if (get_irn_op(c1) != op_Const)
3945 tv1 = get_Const_tarval(c1);
3946 if (! tarval_is_long(tv1))
3949 if (get_tarval_long(tv1) != get_mode_size_bits(mode))
3952 /* yet, condition met */
3953 block = get_nodes_block(or);
3955 /* a Rot right is not supported, so use a rot left */
3956 n = new_r_Rot(current_ir_graph, block, x, sub, mode);
3958 DBG_OPT_ALGSIM0(or, n, FS_OPT_OR_SHFT_TO_ROT);
3960 } else if (get_irn_op(c2) == op_Sub) {
3964 c1 = get_Sub_left(sub);
3965 if (get_irn_op(c1) != op_Const)
3968 tv1 = get_Const_tarval(c1);
3969 if (! tarval_is_long(tv1))
3972 if (get_tarval_long(tv1) != get_mode_size_bits(mode))
3975 /* yet, condition met */
3976 block = get_irn_n(or, -1);
3979 n = new_r_Rot(current_ir_graph, block, x, v, mode);
3981 DBG_OPT_ALGSIM0(or, n, FS_OPT_OR_SHFT_TO_ROT);
3986 } /* transform_node_Or_Rot */
3991 static ir_node *transform_node_Or(ir_node *n) {
3992 ir_node *c, *oldn = n;
3993 ir_node *a = get_Or_left(n);
3994 ir_node *b = get_Or_right(n);
3996 if (is_Not(a) && is_Not(b)) {
3997 /* ~a | ~b = ~(a&b) */
3998 ir_node *block = get_nodes_block(n);
3999 ir_mode *mode = get_irn_mode(n);
4003 n = new_rd_And(get_irn_dbg_info(n), current_ir_graph, block, a, b, mode);
4004 n = new_rd_Not(get_irn_dbg_info(n), current_ir_graph, block, n, mode);
4005 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_DEMORGAN);
4009 /* we can evaluate 2 Projs of the same Cmp */
4010 if (get_irn_mode(n) == mode_b && is_Proj(a) && is_Proj(b)) {
4011 ir_node *pred_a = get_Proj_pred(a);
4012 ir_node *pred_b = get_Proj_pred(b);
4013 if (pred_a == pred_b) {
4014 dbg_info *dbgi = get_irn_dbg_info(n);
4015 ir_node *block = get_nodes_block(pred_a);
4016 pn_Cmp pn_a = get_Proj_proj(a);
4017 pn_Cmp pn_b = get_Proj_proj(b);
4018 /* yes, we can simply calculate with pncs */
4019 pn_Cmp new_pnc = pn_a | pn_b;
4021 return new_rd_Proj(dbgi, current_ir_graph, block, pred_a, mode_b,
4026 HANDLE_BINOP_PHI(tarval_or, a,b,c);
4028 n = transform_node_Or_bf_store(n);
4029 n = transform_node_Or_Rot(n);
4033 n = transform_bitwise_distributive(n, transform_node_Or);
4036 } /* transform_node_Or */
4040 static ir_node *transform_node(ir_node *n);
4043 * Optimize (a >> c1) >> c2), works for Shr, Shrs, Shl.
4045 * Should be moved to reassociation?
4047 static ir_node *transform_node_shift(ir_node *n) {
4048 ir_node *left, *right;
4049 tarval *tv1, *tv2, *res;
4051 int modulo_shf, flag;
4053 left = get_binop_left(n);
4055 /* different operations */
4056 if (get_irn_op(left) != get_irn_op(n))
4059 right = get_binop_right(n);
4060 tv1 = value_of(right);
4061 if (tv1 == tarval_bad)
4064 tv2 = value_of(get_binop_right(left));
4065 if (tv2 == tarval_bad)
4068 res = tarval_add(tv1, tv2);
4070 /* beware: a simple replacement works only, if res < modulo shift */
4071 mode = get_irn_mode(n);
4075 modulo_shf = get_mode_modulo_shift(mode);
4076 if (modulo_shf > 0) {
4077 tarval *modulo = new_tarval_from_long(modulo_shf, get_tarval_mode(res));
4079 if (tarval_cmp(res, modulo) & pn_Cmp_Lt)
4085 /* ok, we can replace it */
4086 ir_node *in[2], *irn, *block = get_irn_n(n, -1);
4088 in[0] = get_binop_left(left);
4089 in[1] = new_r_Const(current_ir_graph, block, get_tarval_mode(res), res);
4091 irn = new_ir_node(NULL, current_ir_graph, block, get_irn_op(n), mode, 2, in);
4093 DBG_OPT_ALGSIM0(n, irn, FS_OPT_REASSOC_SHIFT);
4095 return transform_node(irn);
4098 } /* transform_node_shift */
4103 static ir_node *transform_node_Shr(ir_node *n) {
4104 ir_node *c, *oldn = n;
4105 ir_node *a = get_Shr_left(n);
4106 ir_node *b = get_Shr_right(n);
4108 HANDLE_BINOP_PHI(tarval_shr, a, b, c);
4109 return transform_node_shift(n);
4110 } /* transform_node_Shr */
4115 static ir_node *transform_node_Shrs(ir_node *n) {
4116 ir_node *c, *oldn = n;
4117 ir_node *a = get_Shrs_left(n);
4118 ir_node *b = get_Shrs_right(n);
4120 HANDLE_BINOP_PHI(tarval_shrs, a, b, c);
4121 return transform_node_shift(n);
4122 } /* transform_node_Shrs */
4127 static ir_node *transform_node_Shl(ir_node *n) {
4128 ir_node *c, *oldn = n;
4129 ir_node *a = get_Shl_left(n);
4130 ir_node *b = get_Shl_right(n);
4132 HANDLE_BINOP_PHI(tarval_shl, a, b, c);
4133 return transform_node_shift(n);
4134 } /* transform_node_Shl */
4137 * Remove dead blocks and nodes in dead blocks
4138 * in keep alive list. We do not generate a new End node.
4140 static ir_node *transform_node_End(ir_node *n) {
4141 int i, j, n_keepalives = get_End_n_keepalives(n);
4144 NEW_ARR_A(ir_node *, in, n_keepalives);
4146 for (i = j = 0; i < n_keepalives; ++i) {
4147 ir_node *ka = get_End_keepalive(n, i);
4149 if (! is_Block_dead(ka)) {
4153 } else if (is_irn_pinned_in_irg(ka) && is_Block_dead(get_nodes_block(ka))) {
4156 /* FIXME: beabi need to keep a Proj(M) */
4157 if (is_Phi(ka) || is_irn_keep(ka) || is_Proj(ka))
4160 if (j != n_keepalives)
4161 set_End_keepalives(n, j, in);
4163 } /* transform_node_End */
4165 /** returns 1 if a == -b */
4166 static int is_negated_value(ir_node *a, ir_node *b) {
4167 if(is_Minus(a) && get_Minus_op(a) == b)
4169 if(is_Minus(b) && get_Minus_op(b) == a)
4171 if(is_Sub(a) && is_Sub(b)) {
4172 ir_node *a_left = get_Sub_left(a);
4173 ir_node *a_right = get_Sub_right(a);
4174 ir_node *b_left = get_Sub_left(b);
4175 ir_node *b_right = get_Sub_right(b);
4177 if(a_left == b_right && a_right == b_left)
4185 * Optimize a Mux into some simpler cases.
4187 static ir_node *transform_node_Mux(ir_node *n) {
4188 ir_node *oldn = n, *sel = get_Mux_sel(n);
4189 ir_mode *mode = get_irn_mode(n);
4191 if (mode == mode_b) {
4192 ir_node *t = get_Mux_true(n);
4193 ir_node *f = get_Mux_false(n);
4194 dbg_info *dbg = get_irn_dbg_info(n);
4195 ir_node *block = get_irn_n(n, -1);
4196 ir_graph *irg = current_ir_graph;
4199 tarval *tv_t = get_Const_tarval(t);
4200 if (tv_t == tarval_b_true) {
4202 assert(get_Const_tarval(f) == tarval_b_false);
4205 return new_rd_Or(dbg, irg, block, sel, f, mode_b);
4208 ir_node* not_sel = new_rd_Not(dbg, irg, block, sel, mode_b);
4209 assert(tv_t == tarval_b_false);
4211 assert(get_Const_tarval(f) == tarval_b_true);
4214 return new_rd_And(dbg, irg, block, not_sel, f, mode_b);
4217 } else if (is_Const(f)) {
4218 tarval *tv_f = get_Const_tarval(f);
4219 if (tv_f == tarval_b_true) {
4220 ir_node* not_sel = new_rd_Not(dbg, irg, block, sel, mode_b);
4221 return new_rd_Or(dbg, irg, block, not_sel, t, mode_b);
4223 assert(tv_f == tarval_b_false);
4224 return new_rd_And(dbg, irg, block, sel, t, mode_b);
4229 if (get_irn_op(sel) == op_Proj && !mode_honor_signed_zeros(mode)) {
4230 ir_node *cmp = get_Proj_pred(sel);
4231 long pn = get_Proj_proj(sel);
4232 ir_node *f = get_Mux_false(n);
4233 ir_node *t = get_Mux_true(n);
4236 * Note: normalization puts the constant on the right side,
4237 * so we check only one case.
4239 * Note further that these optimization work even for floating point
4240 * with NaN's because -NaN == NaN.
4241 * However, if +0 and -0 is handled differently, we cannot use the first
4244 if (get_irn_op(cmp) == op_Cmp
4245 && classify_Const(get_Cmp_right(cmp)) == CNST_NULL) {
4246 ir_node *block = get_irn_n(n, -1);
4248 if(is_negated_value(f, t)) {
4249 ir_node *cmp_left = get_Cmp_left(cmp);
4251 /* Psi(a >= 0, a, -a) = Psi(a <= 0, -a, a) ==> Abs(a) */
4252 if ( (cmp_left == t && (pn == pn_Cmp_Ge || pn == pn_Cmp_Gt))
4253 || (cmp_left == f && (pn == pn_Cmp_Le || pn == pn_Cmp_Lt)))
4255 n = new_rd_Abs(get_irn_dbg_info(n), current_ir_graph, block,
4257 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_ABS);
4259 /* Psi(a <= 0, a, -a) = Psi(a >= 0, -a, a) ==> -Abs(a) */
4260 } else if ((cmp_left == t && (pn == pn_Cmp_Le || pn == pn_Cmp_Lt))
4261 || (cmp_left == f && (pn == pn_Cmp_Ge || pn == pn_Cmp_Gt)))
4263 n = new_rd_Abs(get_irn_dbg_info(n), current_ir_graph, block,
4265 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph,
4267 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_ABS);
4272 if (mode_is_int(mode) && mode_is_signed(mode) &&
4273 get_mode_arithmetic(mode) == irma_twos_complement) {
4274 ir_node *x = get_Cmp_left(cmp);
4276 /* the following optimization works only with signed integer two-complement mode */
4278 if (mode == get_irn_mode(x)) {
4280 * FIXME: this restriction is two rigid, as it would still
4281 * work if mode(x) = Hs and mode == Is, but at least it removes
4284 if ((pn == pn_Cmp_Lt || pn == pn_Cmp_Le) &&
4285 classify_Const(t) == CNST_ALL_ONE &&
4286 classify_Const(f) == CNST_NULL) {
4288 * Mux(x:T </<= 0, 0, -1) -> Shrs(x, sizeof_bits(T) - 1)
4292 n = new_rd_Shrs(get_irn_dbg_info(n),
4293 current_ir_graph, block, x,
4294 new_r_Const_long(current_ir_graph, block, mode_Iu,
4295 get_mode_size_bits(mode) - 1),
4297 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_SHR);
4299 } else if ((pn == pn_Cmp_Gt || pn == pn_Cmp_Ge) &&
4300 classify_Const(t) == CNST_ONE &&
4301 classify_Const(f) == CNST_NULL) {
4303 * Mux(x:T >/>= 0, 0, 1) -> Shr(-x, sizeof_bits(T) - 1)
4307 n = new_rd_Shr(get_irn_dbg_info(n),
4308 current_ir_graph, block,
4309 new_r_Minus(current_ir_graph, block, x, mode),
4310 new_r_Const_long(current_ir_graph, block, mode_Iu,
4311 get_mode_size_bits(mode) - 1),
4313 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_SHR);
4320 return arch_transform_node_Mux(n);
4321 } /* transform_node_Mux */
4324 * Optimize a Psi into some simpler cases.
4326 static ir_node *transform_node_Psi(ir_node *n) {
4328 return transform_node_Mux(n);
4331 } /* transform_node_Psi */
4334 * Tries several [inplace] [optimizing] transformations and returns an
4335 * equivalent node. The difference to equivalent_node() is that these
4336 * transformations _do_ generate new nodes, and thus the old node must
4337 * not be freed even if the equivalent node isn't the old one.
4339 static ir_node *transform_node(ir_node *n) {
4343 * Transform_node is the only "optimizing transformation" that might
4344 * return a node with a different opcode. We iterate HERE until fixpoint
4345 * to get the final result.
4349 if (n->op->ops.transform_node)
4350 n = n->op->ops.transform_node(n);
4351 } while (oldn != n);
4354 } /* transform_node */
4357 * Sets the default transform node operation for an ir_op_ops.
4359 * @param code the opcode for the default operation
4360 * @param ops the operations initialized
4365 static ir_op_ops *firm_set_default_transform_node(ir_opcode code, ir_op_ops *ops)
4369 ops->transform_node = transform_node_##a; \
4403 } /* firm_set_default_transform_node */
4406 /* **************** Common Subexpression Elimination **************** */
4408 /** The size of the hash table used, should estimate the number of nodes
4410 #define N_IR_NODES 512
4412 /** Compares the attributes of two Const nodes. */
4413 static int node_cmp_attr_Const(ir_node *a, ir_node *b) {
4414 return (get_Const_tarval(a) != get_Const_tarval(b))
4415 || (get_Const_type(a) != get_Const_type(b));
4416 } /* node_cmp_attr_Const */
4418 /** Compares the attributes of two Proj nodes. */
4419 static int node_cmp_attr_Proj(ir_node *a, ir_node *b) {
4420 return get_irn_proj_attr(a) != get_irn_proj_attr(b);
4421 } /* node_cmp_attr_Proj */
4423 /** Compares the attributes of two Filter nodes. */
4424 static int node_cmp_attr_Filter(ir_node *a, ir_node *b) {
4425 return get_Filter_proj(a) != get_Filter_proj(b);
4426 } /* node_cmp_attr_Filter */
4428 /** Compares the attributes of two Alloc nodes. */
4429 static int node_cmp_attr_Alloc(ir_node *a, ir_node *b) {
4430 const alloc_attr *pa = get_irn_alloc_attr(a);
4431 const alloc_attr *pb = get_irn_alloc_attr(b);
4432 return (pa->where != pb->where) || (pa->type != pb->type);
4433 } /* node_cmp_attr_Alloc */
4435 /** Compares the attributes of two Free nodes. */
4436 static int node_cmp_attr_Free(ir_node *a, ir_node *b) {
4437 const free_attr *pa = get_irn_free_attr(a);
4438 const free_attr *pb = get_irn_free_attr(b);
4439 return (pa->where != pb->where) || (pa->type != pb->type);
4440 } /* node_cmp_attr_Free */
4442 /** Compares the attributes of two SymConst nodes. */
4443 static int node_cmp_attr_SymConst(ir_node *a, ir_node *b) {
4444 const symconst_attr *pa = get_irn_symconst_attr(a);
4445 const symconst_attr *pb = get_irn_symconst_attr(b);
4446 return (pa->num != pb->num)
4447 || (pa->sym.type_p != pb->sym.type_p)
4448 || (pa->tp != pb->tp);
4449 } /* node_cmp_attr_SymConst */
4451 /** Compares the attributes of two Call nodes. */
4452 static int node_cmp_attr_Call(ir_node *a, ir_node *b) {
4453 return (get_irn_call_attr(a) != get_irn_call_attr(b));
4454 } /* node_cmp_attr_Call */
4456 /** Compares the attributes of two Sel nodes. */
4457 static int node_cmp_attr_Sel(ir_node *a, ir_node *b) {
4458 const ir_entity *a_ent = get_Sel_entity(a);
4459 const ir_entity *b_ent = get_Sel_entity(b);
4461 (a_ent->kind != b_ent->kind) ||
4462 (a_ent->name != b_ent->name) ||
4463 (a_ent->owner != b_ent->owner) ||
4464 (a_ent->ld_name != b_ent->ld_name) ||
4465 (a_ent->type != b_ent->type);
4466 } /* node_cmp_attr_Sel */
4468 /** Compares the attributes of two Phi nodes. */
4469 static int node_cmp_attr_Phi(ir_node *a, ir_node *b) {
4470 /* we can only enter this function if both nodes have the same number of inputs,
4471 hence it is enough to check if one of them is a Phi0 */
4473 /* check the Phi0 attribute */
4474 return get_irn_phi0_attr(a) != get_irn_phi0_attr(b);
4477 } /* node_cmp_attr_Phi */
4479 /** Compares the attributes of two Conv nodes. */
4480 static int node_cmp_attr_Conv(ir_node *a, ir_node *b) {
4481 return get_Conv_strict(a) != get_Conv_strict(b);
4482 } /* node_cmp_attr_Conv */
4484 /** Compares the attributes of two Cast nodes. */
4485 static int node_cmp_attr_Cast(ir_node *a, ir_node *b) {
4486 return get_Cast_type(a) != get_Cast_type(b);
4487 } /* node_cmp_attr_Cast */
4489 /** Compares the attributes of two Load nodes. */
4490 static int node_cmp_attr_Load(ir_node *a, ir_node *b) {
4491 if (get_Load_volatility(a) == volatility_is_volatile ||
4492 get_Load_volatility(b) == volatility_is_volatile)
4493 /* NEVER do CSE on volatile Loads */
4495 /* do not CSE Loads with different alignment. Be conservative. */
4496 if (get_Load_align(a) != get_Load_align(b))
4499 return get_Load_mode(a) != get_Load_mode(b);
4500 } /* node_cmp_attr_Load */
4502 /** Compares the attributes of two Store nodes. */
4503 static int node_cmp_attr_Store(ir_node *a, ir_node *b) {
4504 /* do not CSE Stores with different alignment. Be conservative. */
4505 if (get_Store_align(a) != get_Store_align(b))
4508 /* NEVER do CSE on volatile Stores */
4509 return (get_Store_volatility(a) == volatility_is_volatile ||
4510 get_Store_volatility(b) == volatility_is_volatile);
4511 } /* node_cmp_attr_Store */
4513 /** Compares the attributes of two Confirm nodes. */
4514 static int node_cmp_attr_Confirm(ir_node *a, ir_node *b) {
4515 return (get_Confirm_cmp(a) != get_Confirm_cmp(b));
4516 } /* node_cmp_attr_Confirm */
4518 /** Compares the attributes of two ASM nodes. */
4519 static int node_cmp_attr_ASM(ir_node *a, ir_node *b) {
4521 const ir_asm_constraint *ca;
4522 const ir_asm_constraint *cb;
4525 if (get_ASM_text(a) != get_ASM_text(b))
4528 /* Should we really check the constraints here? Should be better, but is strange. */
4529 n = get_ASM_n_input_constraints(a);
4530 if (n != get_ASM_n_input_constraints(b))
4533 ca = get_ASM_input_constraints(a);
4534 cb = get_ASM_input_constraints(b);
4535 for (i = 0; i < n; ++i) {
4536 if (ca[i].pos != cb[i].pos || ca[i].constraint != cb[i].constraint)
4540 n = get_ASM_n_output_constraints(a);
4541 if (n != get_ASM_n_output_constraints(b))
4544 ca = get_ASM_output_constraints(a);
4545 cb = get_ASM_output_constraints(b);
4546 for (i = 0; i < n; ++i) {
4547 if (ca[i].pos != cb[i].pos || ca[i].constraint != cb[i].constraint)
4551 n = get_ASM_n_clobbers(a);
4552 if (n != get_ASM_n_clobbers(b))
4555 cla = get_ASM_clobbers(a);
4556 clb = get_ASM_clobbers(b);
4557 for (i = 0; i < n; ++i) {
4558 if (cla[i] != clb[i])
4562 } /* node_cmp_attr_ASM */
4565 * Set the default node attribute compare operation for an ir_op_ops.
4567 * @param code the opcode for the default operation
4568 * @param ops the operations initialized
4573 static ir_op_ops *firm_set_default_node_cmp_attr(ir_opcode code, ir_op_ops *ops)
4577 ops->node_cmp_attr = node_cmp_attr_##a; \
4602 } /* firm_set_default_node_cmp_attr */
4605 * Compare function for two nodes in the hash table. Gets two
4606 * nodes as parameters. Returns 0 if the nodes are a cse.
4608 int identities_cmp(const void *elt, const void *key) {
4615 if (a == b) return 0;
4617 if ((get_irn_op(a) != get_irn_op(b)) ||
4618 (get_irn_mode(a) != get_irn_mode(b))) return 1;
4620 /* compare if a's in and b's in are of equal length */
4621 irn_arity_a = get_irn_intra_arity (a);
4622 if (irn_arity_a != get_irn_intra_arity(b))
4625 /* for block-local cse and op_pin_state_pinned nodes: */
4626 if (!get_opt_global_cse() || (get_irn_pinned(a) == op_pin_state_pinned)) {
4627 if (get_irn_intra_n(a, -1) != get_irn_intra_n(b, -1))
4631 /* compare a->in[0..ins] with b->in[0..ins] */
4632 for (i = 0; i < irn_arity_a; i++)
4633 if (get_irn_intra_n(a, i) != get_irn_intra_n(b, i))
4637 * here, we already now that the nodes are identical except their
4640 if (a->op->ops.node_cmp_attr)
4641 return a->op->ops.node_cmp_attr(a, b);
4644 } /* identities_cmp */
4647 * Calculate a hash value of a node.
4649 unsigned ir_node_hash(ir_node *node) {
4653 if (node->op == op_Const) {
4654 /* special value for const, as they only differ in their tarval. */
4655 h = HASH_PTR(node->attr.con.tv);
4656 h = 9*h + HASH_PTR(get_irn_mode(node));
4657 } else if (node->op == op_SymConst) {
4658 /* special value for const, as they only differ in their symbol. */
4659 h = HASH_PTR(node->attr.symc.sym.type_p);
4660 h = 9*h + HASH_PTR(get_irn_mode(node));
4663 /* hash table value = 9*(9*(9*(9*(9*arity+in[0])+in[1])+ ...)+mode)+code */
4664 h = irn_arity = get_irn_intra_arity(node);
4666 /* consider all in nodes... except the block if not a control flow. */
4667 for (i = is_cfop(node) ? -1 : 0; i < irn_arity; i++) {
4668 h = 9*h + HASH_PTR(get_irn_intra_n(node, i));
4672 h = 9*h + HASH_PTR(get_irn_mode(node));
4674 h = 9*h + HASH_PTR(get_irn_op(node));
4678 } /* ir_node_hash */
4680 pset *new_identities(void) {
4681 return new_pset(identities_cmp, N_IR_NODES);
4682 } /* new_identities */
4684 void del_identities(pset *value_table) {
4685 del_pset(value_table);
4686 } /* del_identities */
4689 * Return the canonical node computing the same value as n.
4691 * @param value_table The value table
4692 * @param n The node to lookup
4694 * Looks up the node in a hash table.
4696 * For Const nodes this is performed in the constructor, too. Const
4697 * nodes are extremely time critical because of their frequent use in
4698 * constant string arrays.
4700 static INLINE ir_node *identify(pset *value_table, ir_node *n) {
4703 if (!value_table) return n;
4705 if (get_opt_reassociation()) {
4706 if (is_op_commutative(get_irn_op(n))) {
4707 ir_node *l = get_binop_left(n);
4708 ir_node *r = get_binop_right(n);
4710 /* for commutative operators perform a OP b == b OP a */
4711 if (get_irn_idx(l) > get_irn_idx(r)) {
4712 set_binop_left(n, r);
4713 set_binop_right(n, l);
4718 o = pset_find(value_table, n, ir_node_hash(n));
4727 * During construction we set the op_pin_state_pinned flag in the graph right when the
4728 * optimization is performed. The flag turning on procedure global cse could
4729 * be changed between two allocations. This way we are safe.
4731 static INLINE ir_node *identify_cons(pset *value_table, ir_node *n) {
4734 n = identify(value_table, n);
4735 if (get_irn_n(old, -1) != get_irn_n(n, -1))
4736 set_irg_pinned(current_ir_graph, op_pin_state_floats);
4738 } /* identify_cons */
4741 * Return the canonical node computing the same value as n.
4742 * Looks up the node in a hash table, enters it in the table
4743 * if it isn't there yet.
4745 ir_node *identify_remember(pset *value_table, ir_node *n) {
4748 if (!value_table) return n;
4750 if (get_opt_reassociation()) {
4751 if (is_op_commutative(get_irn_op(n))) {
4752 ir_node *l = get_binop_left(n);
4753 ir_node *r = get_binop_right(n);
4754 int l_idx = get_irn_idx(l);
4755 int r_idx = get_irn_idx(r);
4757 /* For commutative operators perform a OP b == b OP a but keep
4758 constants on the RIGHT side. This helps greatly in some optimizations.
4759 Moreover we use the idx number to make the form deterministic. */
4760 if (is_irn_constlike(l))
4762 if (is_irn_constlike(r))
4764 if (l_idx < r_idx) {
4765 set_binop_left(n, r);
4766 set_binop_right(n, l);
4771 /* lookup or insert in hash table with given hash key. */
4772 o = pset_insert(value_table, n, ir_node_hash(n));
4779 } /* identify_remember */
4781 /* Add a node to the identities value table. */
4782 void add_identities(pset *value_table, ir_node *node) {
4783 if (get_opt_cse() && is_no_Block(node))
4784 identify_remember(value_table, node);
4785 } /* add_identities */
4787 /* Visit each node in the value table of a graph. */
4788 void visit_all_identities(ir_graph *irg, irg_walk_func visit, void *env) {
4790 ir_graph *rem = current_ir_graph;
4792 current_ir_graph = irg;
4793 foreach_pset(irg->value_table, node)
4795 current_ir_graph = rem;
4796 } /* visit_all_identities */
4799 * Garbage in, garbage out. If a node has a dead input, i.e., the
4800 * Bad node is input to the node, return the Bad node.
4802 static INLINE ir_node *gigo(ir_node *node) {
4804 ir_op *op = get_irn_op(node);
4806 /* remove garbage blocks by looking at control flow that leaves the block
4807 and replacing the control flow by Bad. */
4808 if (get_irn_mode(node) == mode_X) {
4809 ir_node *block = get_nodes_block(skip_Proj(node));
4811 /* Don't optimize nodes in immature blocks. */
4812 if (!get_Block_matured(block)) return node;
4813 /* Don't optimize End, may have Bads. */
4814 if (op == op_End) return node;
4816 if (is_Block(block)) {
4817 irn_arity = get_irn_arity(block);
4818 for (i = 0; i < irn_arity; i++) {
4819 if (!is_Bad(get_irn_n(block, i)))
4822 if (i == irn_arity) {
4823 ir_graph *irg = get_irn_irg(block);
4824 /* the start block is never dead */
4825 if (block != get_irg_start_block(irg)
4826 && block != get_irg_end_block(irg))
4832 /* Blocks, Phis and Tuples may have dead inputs, e.g., if one of the
4833 blocks predecessors is dead. */
4834 if (op != op_Block && op != op_Phi && op != op_Tuple) {
4835 irn_arity = get_irn_arity(node);
4838 * Beware: we can only read the block of a non-floating node.
4840 if (is_irn_pinned_in_irg(node) &&
4841 is_Block_dead(get_nodes_block(node)))
4844 for (i = 0; i < irn_arity; i++) {
4845 ir_node *pred = get_irn_n(node, i);
4850 /* Propagating Unknowns here seems to be a bad idea, because
4851 sometimes we need a node as a input and did not want that
4853 However, it might be useful to move this into a later phase
4854 (if you think that optimizing such code is useful). */
4855 if (is_Unknown(pred) && mode_is_data(get_irn_mode(node)))
4856 return new_Unknown(get_irn_mode(node));
4861 /* With this code we violate the agreement that local_optimize
4862 only leaves Bads in Block, Phi and Tuple nodes. */
4863 /* If Block has only Bads as predecessors it's garbage. */
4864 /* If Phi has only Bads as predecessors it's garbage. */
4865 if ((op == op_Block && get_Block_matured(node)) || op == op_Phi) {
4866 irn_arity = get_irn_arity(node);
4867 for (i = 0; i < irn_arity; i++) {
4868 if (!is_Bad(get_irn_n(node, i))) break;
4870 if (i == irn_arity) node = new_Bad();
4877 * These optimizations deallocate nodes from the obstack.
4878 * It can only be called if it is guaranteed that no other nodes
4879 * reference this one, i.e., right after construction of a node.
4881 * @param n The node to optimize
4883 * current_ir_graph must be set to the graph of the node!
4885 ir_node *optimize_node(ir_node *n) {
4888 ir_opcode iro = get_irn_opcode(n);
4890 /* Always optimize Phi nodes: part of the construction. */
4891 if ((!get_opt_optimize()) && (iro != iro_Phi)) return n;
4893 /* constant expression evaluation / constant folding */
4894 if (get_opt_constant_folding()) {
4895 /* neither constants nor Tuple values can be evaluated */
4896 if (iro != iro_Const && (get_irn_mode(n) != mode_T)) {
4897 unsigned fp_model = get_irg_fp_model(current_ir_graph);
4898 int old_fp_mode = tarval_enable_fp_ops((fp_model & fp_strict_algebraic) == 0);
4899 /* try to evaluate */
4900 tv = computed_value(n);
4901 if (tv != tarval_bad) {
4903 ir_type *old_tp = get_irn_type(n);
4904 int i, arity = get_irn_arity(n);
4908 * Try to recover the type of the new expression.
4910 for (i = 0; i < arity && !old_tp; ++i)
4911 old_tp = get_irn_type(get_irn_n(n, i));
4914 * we MUST copy the node here temporary, because it's still needed
4915 * for DBG_OPT_CSTEVAL
4917 node_size = offsetof(ir_node, attr) + n->op->attr_size;
4918 oldn = alloca(node_size);
4920 memcpy(oldn, n, node_size);
4921 CLONE_ARR_A(ir_node *, oldn->in, n->in);
4923 /* ARG, copy the in array, we need it for statistics */
4924 memcpy(oldn->in, n->in, ARR_LEN(n->in) * sizeof(n->in[0]));
4926 /* note the inplace edges module */
4927 edges_node_deleted(n, current_ir_graph);
4929 /* evaluation was successful -- replace the node. */
4930 irg_kill_node(current_ir_graph, n);
4931 nw = new_Const(get_tarval_mode (tv), tv);
4933 if (old_tp && get_type_mode(old_tp) == get_tarval_mode (tv))
4934 set_Const_type(nw, old_tp);
4935 DBG_OPT_CSTEVAL(oldn, nw);
4936 tarval_enable_fp_ops(old_fp_mode);
4939 tarval_enable_fp_ops(old_fp_mode);
4943 /* remove unnecessary nodes */
4944 if (get_opt_constant_folding() ||
4945 (iro == iro_Phi) || /* always optimize these nodes. */
4947 (iro == iro_Proj) ||
4948 (iro == iro_Block) ) /* Flags tested local. */
4949 n = equivalent_node(n);
4951 /* Common Subexpression Elimination.
4953 * Checks whether n is already available.
4954 * The block input is used to distinguish different subexpressions. Right
4955 * now all nodes are op_pin_state_pinned to blocks, i.e., the CSE only finds common
4956 * subexpressions within a block.
4959 n = identify_cons(current_ir_graph->value_table, n);
4962 edges_node_deleted(oldn, current_ir_graph);
4964 /* We found an existing, better node, so we can deallocate the old node. */
4965 irg_kill_node(current_ir_graph, oldn);
4969 /* Some more constant expression evaluation that does not allow to
4971 iro = get_irn_opcode(n);
4972 if (get_opt_constant_folding() ||
4973 (iro == iro_Cond) ||
4974 (iro == iro_Proj)) /* Flags tested local. */
4975 n = transform_node(n);
4977 /* Remove nodes with dead (Bad) input.
4978 Run always for transformation induced Bads. */
4981 /* Now we have a legal, useful node. Enter it in hash table for CSE */
4982 if (get_opt_cse() && (get_irn_opcode(n) != iro_Block)) {
4983 n = identify_remember(current_ir_graph->value_table, n);
4987 } /* optimize_node */
4991 * These optimizations never deallocate nodes (in place). This can cause dead
4992 * nodes lying on the obstack. Remove these by a dead node elimination,
4993 * i.e., a copying garbage collection.
4995 ir_node *optimize_in_place_2(ir_node *n) {
4998 ir_opcode iro = get_irn_opcode(n);
5000 if (!get_opt_optimize() && (get_irn_op(n) != op_Phi)) return n;
5002 /* constant expression evaluation / constant folding */
5003 if (get_opt_constant_folding()) {
5004 /* neither constants nor Tuple values can be evaluated */
5005 if (iro != iro_Const && get_irn_mode(n) != mode_T) {
5006 unsigned fp_model = get_irg_fp_model(current_ir_graph);
5007 int old_fp_mode = tarval_enable_fp_ops((fp_model & fp_strict_algebraic) == 0);
5008 /* try to evaluate */
5009 tv = computed_value(n);
5010 if (tv != tarval_bad) {
5011 /* evaluation was successful -- replace the node. */
5012 ir_type *old_tp = get_irn_type(n);
5013 int i, arity = get_irn_arity(n);
5016 * Try to recover the type of the new expression.
5018 for (i = 0; i < arity && !old_tp; ++i)
5019 old_tp = get_irn_type(get_irn_n(n, i));
5021 n = new_Const(get_tarval_mode(tv), tv);
5023 if (old_tp && get_type_mode(old_tp) == get_tarval_mode(tv))
5024 set_Const_type(n, old_tp);
5026 DBG_OPT_CSTEVAL(oldn, n);
5027 tarval_enable_fp_ops(old_fp_mode);
5030 tarval_enable_fp_ops(old_fp_mode);
5034 /* remove unnecessary nodes */
5035 if (get_opt_constant_folding() ||
5036 (iro == iro_Phi) || /* always optimize these nodes. */
5037 (iro == iro_Id) || /* ... */
5038 (iro == iro_Proj) || /* ... */
5039 (iro == iro_Block) ) /* Flags tested local. */
5040 n = equivalent_node(n);
5042 /** common subexpression elimination **/
5043 /* Checks whether n is already available. */
5044 /* The block input is used to distinguish different subexpressions. Right
5045 now all nodes are op_pin_state_pinned to blocks, i.e., the cse only finds common
5046 subexpressions within a block. */
5047 if (get_opt_cse()) {
5048 n = identify(current_ir_graph->value_table, n);
5051 /* Some more constant expression evaluation. */
5052 iro = get_irn_opcode(n);
5053 if (get_opt_constant_folding() ||
5054 (iro == iro_Cond) ||
5055 (iro == iro_Proj)) /* Flags tested local. */
5056 n = transform_node(n);
5058 /* Remove nodes with dead (Bad) input.
5059 Run always for transformation induced Bads. */
5062 /* Now we can verify the node, as it has no dead inputs any more. */
5065 /* Now we have a legal, useful node. Enter it in hash table for cse.
5066 Blocks should be unique anyways. (Except the successor of start:
5067 is cse with the start block!) */
5068 if (get_opt_cse() && (get_irn_opcode(n) != iro_Block))
5069 n = identify_remember(current_ir_graph->value_table, n);
5072 } /* optimize_in_place_2 */
5075 * Wrapper for external use, set proper status bits after optimization.
5077 ir_node *optimize_in_place(ir_node *n) {
5078 /* Handle graph state */
5079 assert(get_irg_phase_state(current_ir_graph) != phase_building);
5081 if (get_opt_global_cse())
5082 set_irg_pinned(current_ir_graph, op_pin_state_floats);
5083 if (get_irg_outs_state(current_ir_graph) == outs_consistent)
5084 set_irg_outs_inconsistent(current_ir_graph);
5086 /* FIXME: Maybe we could also test whether optimizing the node can
5087 change the control graph. */
5088 set_irg_doms_inconsistent(current_ir_graph);
5089 return optimize_in_place_2(n);
5090 } /* optimize_in_place */
5093 * Sets the default operation for an ir_ops.
5095 ir_op_ops *firm_set_default_operations(ir_opcode code, ir_op_ops *ops) {
5096 ops = firm_set_default_computed_value(code, ops);
5097 ops = firm_set_default_equivalent_node(code, ops);
5098 ops = firm_set_default_transform_node(code, ops);
5099 ops = firm_set_default_node_cmp_attr(code, ops);
5100 ops = firm_set_default_get_type(code, ops);
5101 ops = firm_set_default_get_type_attr(code, ops);
5102 ops = firm_set_default_get_entity_attr(code, ops);
5105 } /* firm_set_default_operations */