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);
195 tarval *ta = value_of(a);
196 tarval *tb = value_of(b);
198 if ((ta != tarval_bad) && (tb != tarval_bad) && (get_irn_mode(a) == get_irn_mode(b))) {
199 return tarval_mul(ta, tb);
201 /* a*0 = 0 or 0*b = 0:
202 calls computed_value recursive and returns the 0 with proper
204 if ((ta != tarval_bad) && (ta == get_mode_null(get_tarval_mode(ta))))
206 if ((tb != tarval_bad) && (tb == get_mode_null(get_tarval_mode(tb))))
210 } /* computed_value_Mul */
213 * Return the value of a floating point Quot.
215 static tarval *computed_value_Quot(ir_node *n) {
216 ir_node *a = get_Quot_left(n);
217 ir_node *b = get_Quot_right(n);
219 tarval *ta = value_of(a);
220 tarval *tb = value_of(b);
222 /* This was missing in original implementation. Why? */
223 if ((ta != tarval_bad) && (tb != tarval_bad) && (get_irn_mode(a) == get_irn_mode(b))) {
224 if (tb != get_mode_null(get_tarval_mode(tb))) /* div by zero: return tarval_bad */
225 return tarval_quo(ta, tb);
228 } /* computed_value_Quot */
231 * Calculate the value of an integer Div of two nodes.
232 * Special case: 0 / b
234 static tarval *do_computed_value_Div(ir_node *a, ir_node *b) {
235 tarval *ta = value_of(a);
236 tarval *tb = value_of(b);
238 /* Compute c1 / c2 or 0 / a, a != 0 */
239 if (ta != tarval_bad) {
240 if ((tb != tarval_bad) && (tb != get_mode_null(get_irn_mode(b)))) /* div by zero: return tarval_bad */
241 return tarval_div(ta, tb);
242 else if (ta == get_mode_null(get_tarval_mode(ta))) /* 0 / b == 0 */
246 } /* do_computed_value_Div */
249 * Return the value of an integer Div.
251 static tarval *computed_value_Div(ir_node *n) {
252 return do_computed_value_Div(get_Div_left(n), get_Div_right(n));
253 } /* computed_value_Div */
256 * Calculate the value of an integer Mod of two nodes.
257 * Special case: a % 1
259 static tarval *do_computed_value_Mod(ir_node *a, ir_node *b) {
260 tarval *ta = value_of(a);
261 tarval *tb = value_of(b);
263 /* Compute c1 % c2 or a % 1 */
264 if (tb != tarval_bad) {
265 if ((ta != tarval_bad) && (tb != get_mode_null(get_tarval_mode(tb)))) /* div by zero: return tarval_bad */
266 return tarval_mod(ta, tb);
267 else if (tb == get_mode_one(get_tarval_mode(tb))) /* x mod 1 == 0 */
268 return get_mode_null(get_irn_mode(a));
271 } /* do_computed_value_Mod */
274 * Return the value of an integer Mod.
276 static tarval *computed_value_Mod(ir_node *n) {
277 return do_computed_value_Mod(get_Mod_left(n), get_Mod_right(n));
278 } /* computed_value_Mod */
281 * Return the value of an Abs.
283 static tarval *computed_value_Abs(ir_node *n) {
284 ir_node *a = get_Abs_op(n);
285 tarval *ta = value_of(a);
287 if (ta != tarval_bad)
288 return tarval_abs(ta);
291 } /* computed_value_Abs */
294 * Return the value of an And.
295 * Special case: a & 0, 0 & b
297 static tarval *computed_value_And(ir_node *n) {
298 ir_node *a = get_And_left(n);
299 ir_node *b = get_And_right(n);
301 tarval *ta = value_of(a);
302 tarval *tb = value_of(b);
304 if ((ta != tarval_bad) && (tb != tarval_bad)) {
305 return tarval_and (ta, tb);
309 if ( (classify_tarval ((v = ta)) == TV_CLASSIFY_NULL)
310 || (classify_tarval ((v = tb)) == TV_CLASSIFY_NULL)) {
315 } /* computed_value_And */
318 * Return the value of an Or.
319 * Special case: a | 1...1, 1...1 | b
321 static tarval *computed_value_Or(ir_node *n) {
322 ir_node *a = get_Or_left(n);
323 ir_node *b = get_Or_right(n);
325 tarval *ta = value_of(a);
326 tarval *tb = value_of(b);
328 if ((ta != tarval_bad) && (tb != tarval_bad)) {
329 return tarval_or (ta, tb);
332 if ( (classify_tarval ((v = ta)) == TV_CLASSIFY_ALL_ONE)
333 || (classify_tarval ((v = tb)) == TV_CLASSIFY_ALL_ONE)) {
338 } /* computed_value_Or */
341 * Return the value of an Eor.
343 static tarval *computed_value_Eor(ir_node *n) {
344 ir_node *a = get_Eor_left(n);
345 ir_node *b = get_Eor_right(n);
350 return get_mode_null(get_irn_mode(n));
355 if ((ta != tarval_bad) && (tb != tarval_bad)) {
356 return tarval_eor (ta, tb);
359 } /* computed_value_Eor */
362 * Return the value of a Not.
364 static tarval *computed_value_Not(ir_node *n) {
365 ir_node *a = get_Not_op(n);
366 tarval *ta = value_of(a);
368 if (ta != tarval_bad)
369 return tarval_not(ta);
372 } /* computed_value_Not */
375 * Return the value of a Shl.
377 static tarval *computed_value_Shl(ir_node *n) {
378 ir_node *a = get_Shl_left(n);
379 ir_node *b = get_Shl_right(n);
381 tarval *ta = value_of(a);
382 tarval *tb = value_of(b);
384 if ((ta != tarval_bad) && (tb != tarval_bad)) {
385 return tarval_shl (ta, tb);
388 } /* computed_value_Shl */
391 * Return the value of a Shr.
393 static tarval *computed_value_Shr(ir_node *n) {
394 ir_node *a = get_Shr_left(n);
395 ir_node *b = get_Shr_right(n);
397 tarval *ta = value_of(a);
398 tarval *tb = value_of(b);
400 if ((ta != tarval_bad) && (tb != tarval_bad)) {
401 return tarval_shr (ta, tb);
404 } /* computed_value_Shr */
407 * Return the value of a Shrs.
409 static tarval *computed_value_Shrs(ir_node *n) {
410 ir_node *a = get_Shrs_left(n);
411 ir_node *b = get_Shrs_right(n);
413 tarval *ta = value_of(a);
414 tarval *tb = value_of(b);
416 if ((ta != tarval_bad) && (tb != tarval_bad)) {
417 return tarval_shrs (ta, tb);
420 } /* computed_value_Shrs */
423 * Return the value of a Rot.
425 static tarval *computed_value_Rot(ir_node *n) {
426 ir_node *a = get_Rot_left(n);
427 ir_node *b = get_Rot_right(n);
429 tarval *ta = value_of(a);
430 tarval *tb = value_of(b);
432 if ((ta != tarval_bad) && (tb != tarval_bad)) {
433 return tarval_rot (ta, tb);
436 } /* computed_value_Rot */
439 * Return the value of a Conv.
441 static tarval *computed_value_Conv(ir_node *n) {
442 ir_node *a = get_Conv_op(n);
443 tarval *ta = value_of(a);
445 if (ta != tarval_bad)
446 return tarval_convert_to(ta, get_irn_mode(n));
449 } /* computed_value_Conv */
452 * Return the value of a Proj(Cmp).
454 * This performs a first step of unreachable code elimination.
455 * Proj can not be computed, but folding a Cmp above the Proj here is
456 * not as wasteful as folding a Cmp into a Tuple of 16 Consts of which
458 * There are several case where we can evaluate a Cmp node, see later.
460 static tarval *computed_value_Proj_Cmp(ir_node *n) {
461 ir_node *a = get_Proj_pred(n);
462 ir_node *aa = get_Cmp_left(a);
463 ir_node *ab = get_Cmp_right(a);
464 long proj_nr = get_Proj_proj(n);
467 * BEWARE: a == a is NOT always True for floating Point values, as
468 * NaN != NaN is defined, so we must check this here.
471 !mode_is_float(get_irn_mode(aa)) || proj_nr == pn_Cmp_Lt || proj_nr == pn_Cmp_Gt)
474 /* This is a trick with the bits used for encoding the Cmp
475 Proj numbers, the following statement is not the same:
476 return new_tarval_from_long (proj_nr == pn_Cmp_Eq, mode_b) */
477 return new_tarval_from_long (proj_nr & pn_Cmp_Eq, mode_b);
480 tarval *taa = value_of(aa);
481 tarval *tab = value_of(ab);
482 ir_mode *mode = get_irn_mode(aa);
485 * The predecessors of Cmp are target values. We can evaluate
488 if ((taa != tarval_bad) && (tab != tarval_bad)) {
489 /* strange checks... */
490 pn_Cmp flags = tarval_cmp(taa, tab);
491 if (flags != pn_Cmp_False) {
492 return new_tarval_from_long (proj_nr & flags, mode_b);
495 /* for integer values, we can check against MIN/MAX */
496 else if (mode_is_int(mode)) {
497 /* MIN <=/> x. This results in true/false. */
498 if (taa == get_mode_min(mode)) {
499 /* a compare with the MIN value */
500 if (proj_nr == pn_Cmp_Le)
501 return get_tarval_b_true();
502 else if (proj_nr == pn_Cmp_Gt)
503 return get_tarval_b_false();
505 /* x >=/< MIN. This results in true/false. */
507 if (tab == get_mode_min(mode)) {
508 /* a compare with the MIN value */
509 if (proj_nr == pn_Cmp_Ge)
510 return get_tarval_b_true();
511 else if (proj_nr == pn_Cmp_Lt)
512 return get_tarval_b_false();
514 /* MAX >=/< x. This results in true/false. */
515 else if (taa == get_mode_max(mode)) {
516 if (proj_nr == pn_Cmp_Ge)
517 return get_tarval_b_true();
518 else if (proj_nr == pn_Cmp_Lt)
519 return get_tarval_b_false();
521 /* x <=/> MAX. This results in true/false. */
522 else if (tab == get_mode_max(mode)) {
523 if (proj_nr == pn_Cmp_Le)
524 return get_tarval_b_true();
525 else if (proj_nr == pn_Cmp_Gt)
526 return get_tarval_b_false();
530 * The predecessors are Allocs or (void*)(0) constants. Allocs never
531 * return NULL, they raise an exception. Therefore we can predict
535 ir_node *aaa = skip_Id(skip_Proj(aa));
536 ir_node *aba = skip_Id(skip_Proj(ab));
538 if ( ( (/* aa is ProjP and aaa is Alloc */
539 (get_irn_op(aa) == op_Proj)
540 && (mode_is_reference(get_irn_mode(aa)))
541 && (get_irn_op(aaa) == op_Alloc))
542 && ( (/* ab is NULL */
543 (get_irn_op(ab) == op_Const)
544 && (mode_is_reference(get_irn_mode(ab)))
545 && (get_Const_tarval(ab) == get_mode_null(get_irn_mode(ab))))
546 || (/* ab is other Alloc */
547 (get_irn_op(ab) == op_Proj)
548 && (mode_is_reference(get_irn_mode(ab)))
549 && (get_irn_op(aba) == op_Alloc)
551 || (/* aa is NULL and aba is Alloc */
552 (get_irn_op(aa) == op_Const)
553 && (mode_is_reference(get_irn_mode(aa)))
554 && (get_Const_tarval(aa) == get_mode_null(get_irn_mode(aa)))
555 && (get_irn_op(ab) == op_Proj)
556 && (mode_is_reference(get_irn_mode(ab)))
557 && (get_irn_op(aba) == op_Alloc)))
559 return new_tarval_from_long(proj_nr & pn_Cmp_Ne, mode_b);
562 return computed_value_Cmp_Confirm(a, aa, ab, proj_nr);
563 } /* computed_value_Proj_Cmp */
566 * Return the value of a Proj, handle Proj(Cmp), Proj(Div), Proj(Mod),
567 * Proj(DivMod) and Proj(Quot).
569 static tarval *computed_value_Proj(ir_node *n) {
570 ir_node *a = get_Proj_pred(n);
573 switch (get_irn_opcode(a)) {
575 return computed_value_Proj_Cmp(n);
578 /* compute either the Div or the Mod part */
579 proj_nr = get_Proj_proj(n);
580 if (proj_nr == pn_DivMod_res_div)
581 return do_computed_value_Div(get_DivMod_left(a), get_DivMod_right(a));
582 else if (proj_nr == pn_DivMod_res_mod)
583 return do_computed_value_Mod(get_DivMod_left(a), get_DivMod_right(a));
587 if (get_Proj_proj(n) == pn_Div_res)
588 return computed_value(a);
592 if (get_Proj_proj(n) == pn_Mod_res)
593 return computed_value(a);
597 if (get_Proj_proj(n) == pn_Quot_res)
598 return computed_value(a);
605 } /* computed_value_Proj */
608 * Calculate the value of a Mux: can be evaluated, if the
609 * sel and the right input are known.
611 static tarval *computed_value_Mux(ir_node *n) {
612 ir_node *sel = get_Mux_sel(n);
613 tarval *ts = value_of(sel);
615 if (ts == get_tarval_b_true()) {
616 ir_node *v = get_Mux_true(n);
619 else if (ts == get_tarval_b_false()) {
620 ir_node *v = get_Mux_false(n);
624 } /* computed_value_Mux */
627 * Calculate the value of a Psi: can be evaluated, if a condition is true
628 * and all previous conditions are false. If all conditions are false
629 * we evaluate to the default one.
631 static tarval *computed_value_Psi(ir_node *n) {
633 return computed_value_Mux(n);
635 } /* computed_value_Psi */
638 * Calculate the value of a Confirm: can be evaluated,
639 * if it has the form Confirm(x, '=', Const).
641 static tarval *computed_value_Confirm(ir_node *n) {
642 return get_Confirm_cmp(n) == pn_Cmp_Eq ?
643 value_of(get_Confirm_bound(n)) : tarval_bad;
644 } /* computed_value_Confirm */
647 * If the parameter n can be computed, return its value, else tarval_bad.
648 * Performs constant folding.
650 * @param n The node this should be evaluated
652 tarval *computed_value(ir_node *n) {
653 if (n->op->ops.computed_value)
654 return n->op->ops.computed_value(n);
656 } /* computed_value */
659 * Set the default computed_value evaluator in an ir_op_ops.
661 * @param code the opcode for the default operation
662 * @param ops the operations initialized
667 static ir_op_ops *firm_set_default_computed_value(ir_opcode code, ir_op_ops *ops)
671 ops->computed_value = computed_value_##a; \
706 } /* firm_set_default_computed_value */
709 * Returns a equivalent block for another block.
710 * If the block has only one predecessor, this is
711 * the equivalent one. If the only predecessor of a block is
712 * the block itself, this is a dead block.
714 * If both predecessors of a block are the branches of a binary
715 * Cond, the equivalent block is Cond's block.
717 * If all predecessors of a block are bad or lies in a dead
718 * block, the current block is dead as well.
720 * Note, that blocks are NEVER turned into Bad's, instead
721 * the dead_block flag is set. So, never test for is_Bad(block),
722 * always use is_dead_Block(block).
724 static ir_node *equivalent_node_Block(ir_node *n)
727 int n_preds = get_Block_n_cfgpreds(n);
729 /* The Block constructor does not call optimize, but mature_immBlock
730 calls the optimization. */
731 assert(get_Block_matured(n));
733 /* Straightening: a single entry Block following a single exit Block
734 can be merged, if it is not the Start block. */
735 /* !!! Beware, all Phi-nodes of n must have been optimized away.
736 This should be true, as the block is matured before optimize is called.
737 But what about Phi-cycles with the Phi0/Id that could not be resolved?
738 Remaining Phi nodes are just Ids. */
739 if ((n_preds == 1) && (get_irn_op(get_Block_cfgpred(n, 0)) == op_Jmp)) {
740 ir_node *predblock = get_nodes_block(get_Block_cfgpred(n, 0));
741 if (predblock == oldn) {
742 /* Jmp jumps into the block it is in -- deal self cycle. */
743 n = set_Block_dead(n);
744 DBG_OPT_DEAD_BLOCK(oldn, n);
745 } else if (get_opt_control_flow_straightening()) {
747 DBG_OPT_STG(oldn, n);
749 } else if ((n_preds == 1) &&
750 (get_irn_op(skip_Proj(get_Block_cfgpred(n, 0))) == op_Cond)) {
751 ir_node *predblock = get_Block_cfgpred_block(n, 0);
752 if (predblock == oldn) {
753 /* Jmp jumps into the block it is in -- deal self cycle. */
754 n = set_Block_dead(n);
755 DBG_OPT_DEAD_BLOCK(oldn, n);
757 } else if ((n_preds == 2) &&
758 (get_opt_control_flow_weak_simplification())) {
759 /* Test whether Cond jumps twice to this block
760 * The more general case which more than 2 predecessors is handles
761 * in optimize_cf(), we handle only this special case for speed here.
763 ir_node *a = get_Block_cfgpred(n, 0);
764 ir_node *b = get_Block_cfgpred(n, 1);
766 if ((get_irn_op(a) == op_Proj) &&
767 (get_irn_op(b) == op_Proj) &&
768 (get_Proj_pred(a) == get_Proj_pred(b)) &&
769 (get_irn_op(get_Proj_pred(a)) == op_Cond) &&
770 (get_irn_mode(get_Cond_selector(get_Proj_pred(a))) == mode_b)) {
771 /* Also a single entry Block following a single exit Block. Phis have
772 twice the same operand and will be optimized away. */
773 n = get_nodes_block(get_Proj_pred(a));
774 DBG_OPT_IFSIM1(oldn, a, b, n);
776 } else if (get_opt_unreachable_code() &&
777 (n != get_irg_start_block(current_ir_graph)) &&
778 (n != get_irg_end_block(current_ir_graph)) ) {
781 /* If all inputs are dead, this block is dead too, except if it is
782 the start or end block. This is one step of unreachable code
784 for (i = get_Block_n_cfgpreds(n) - 1; i >= 0; --i) {
785 ir_node *pred = get_Block_cfgpred(n, i);
788 if (is_Bad(pred)) continue;
789 pred_blk = get_nodes_block(skip_Proj(pred));
791 if (is_Block_dead(pred_blk)) continue;
794 /* really found a living input */
799 n = set_Block_dead(n);
800 DBG_OPT_DEAD_BLOCK(oldn, n);
805 } /* equivalent_node_Block */
808 * Returns a equivalent node for a Jmp, a Bad :-)
809 * Of course this only happens if the Block of the Jmp is dead.
811 static ir_node *equivalent_node_Jmp(ir_node *n) {
812 /* unreachable code elimination */
813 if (is_Block_dead(get_nodes_block(n)))
817 } /* equivalent_node_Jmp */
819 /** Raise is handled in the same way as Jmp. */
820 #define equivalent_node_Raise equivalent_node_Jmp
823 /* We do not evaluate Cond here as we replace it by a new node, a Jmp.
824 See transform_node_Proj_Cond(). */
827 * Optimize operations that are commutative and have neutral 0,
828 * so a op 0 = 0 op a = a.
830 static ir_node *equivalent_node_neutral_zero(ir_node *n)
834 ir_node *a = get_binop_left(n);
835 ir_node *b = get_binop_right(n);
840 /* After running compute_node there is only one constant predecessor.
841 Find this predecessors value and remember the other node: */
842 if ((tv = value_of(a)) != tarval_bad) {
844 } else if ((tv = value_of(b)) != tarval_bad) {
849 /* If this predecessors constant value is zero, the operation is
850 * unnecessary. Remove it.
852 * Beware: If n is a Add, the mode of on and n might be different
853 * which happens in this rare construction: NULL + 3.
854 * Then, a Conv would be needed which we cannot include here.
856 if (classify_tarval (tv) == TV_CLASSIFY_NULL) {
857 if (get_irn_mode(on) == get_irn_mode(n)) {
860 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_0);
865 } /* equivalent_node_neutral_zero */
868 * Eor is commutative and has neutral 0.
870 #define equivalent_node_Eor equivalent_node_neutral_zero
873 * Optimize a - 0 and (a - x) + x (for modes with wrap-around).
875 * The second one looks strange, but this construct
876 * is used heavily in the LCC sources :-).
878 * Beware: The Mode of an Add may be different than the mode of its
879 * predecessors, so we could not return a predecessors in all cases.
881 static ir_node *equivalent_node_Add(ir_node *n) {
883 ir_node *left, *right;
884 ir_mode *mode = get_irn_mode(n);
886 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
887 if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
890 n = equivalent_node_neutral_zero(n);
894 left = get_Add_left(n);
895 right = get_Add_right(n);
897 if (get_irn_op(left) == op_Sub) {
898 if (get_Sub_right(left) == right) {
901 n = get_Sub_left(left);
902 if (mode == get_irn_mode(n)) {
903 DBG_OPT_ALGSIM1(oldn, left, right, n, FS_OPT_ADD_SUB);
908 if (get_irn_op(right) == op_Sub) {
909 if (get_Sub_right(right) == left) {
912 n = get_Sub_left(right);
913 if (mode == get_irn_mode(n)) {
914 DBG_OPT_ALGSIM1(oldn, left, right, n, FS_OPT_ADD_SUB);
920 } /* equivalent_node_Add */
923 * optimize operations that are not commutative but have neutral 0 on left,
926 static ir_node *equivalent_node_left_zero(ir_node *n) {
929 ir_node *a = get_binop_left(n);
930 ir_node *b = get_binop_right(n);
932 if (classify_tarval(value_of(b)) == TV_CLASSIFY_NULL) {
935 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_0);
938 } /* equivalent_node_left_zero */
940 #define equivalent_node_Shl equivalent_node_left_zero
941 #define equivalent_node_Shr equivalent_node_left_zero
942 #define equivalent_node_Shrs equivalent_node_left_zero
943 #define equivalent_node_Rot equivalent_node_left_zero
946 * Optimize a - 0 and (a + x) - x (for modes with wrap-around).
948 * The second one looks strange, but this construct
949 * is used heavily in the LCC sources :-).
951 * Beware: The Mode of a Sub may be different than the mode of its
952 * predecessors, so we could not return a predecessors in all cases.
954 static ir_node *equivalent_node_Sub(ir_node *n) {
957 ir_mode *mode = get_irn_mode(n);
959 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
960 if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
963 b = get_Sub_right(n);
965 /* Beware: modes might be different */
966 if (classify_tarval(value_of(b)) == TV_CLASSIFY_NULL) {
967 ir_node *a = get_Sub_left(n);
968 if (mode == get_irn_mode(a)) {
971 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_0);
975 } /* equivalent_node_Sub */
979 * Optimize an "idempotent unary op", ie op(op(n)) = n.
982 * -(-a) == a, but might overflow two times.
983 * We handle it anyway here but the better way would be a
984 * flag. This would be needed for Pascal for instance.
986 static ir_node *equivalent_node_idempotent_unop(ir_node *n) {
988 ir_node *pred = get_unop_op(n);
990 /* optimize symmetric unop */
991 if (get_irn_op(pred) == get_irn_op(n)) {
992 n = get_unop_op(pred);
993 DBG_OPT_ALGSIM2(oldn, pred, n, FS_OPT_IDEM_UNARY);
996 } /* equivalent_node_idempotent_unop */
998 /** Optimize Not(Not(x)) == x. */
999 #define equivalent_node_Not equivalent_node_idempotent_unop
1001 /** -(-x) == x ??? Is this possible or can --x raise an
1002 out of bounds exception if min =! max? */
1003 #define equivalent_node_Minus equivalent_node_idempotent_unop
1006 * Optimize a * 1 = 1 * a = a.
1008 static ir_node *equivalent_node_Mul(ir_node *n) {
1010 ir_node *a = get_Mul_left(n);
1011 ir_node *b = get_Mul_right(n);
1013 /* Mul is commutative and has again an other neutral element. */
1014 if (classify_tarval(value_of(a)) == TV_CLASSIFY_ONE) {
1016 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
1017 } else if (classify_tarval(value_of(b)) == TV_CLASSIFY_ONE) {
1019 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
1022 } /* equivalent_node_Mul */
1025 * Optimize a / 1 = a.
1027 static ir_node *equivalent_node_Div(ir_node *n) {
1028 ir_node *a = get_Div_left(n);
1029 ir_node *b = get_Div_right(n);
1031 /* Div is not commutative. */
1032 if (classify_tarval(value_of(b)) == TV_CLASSIFY_ONE) { /* div(x, 1) == x */
1033 /* Turn Div into a tuple (mem, bad, a) */
1034 ir_node *mem = get_Div_mem(n);
1035 ir_node *blk = get_irn_n(n, -1);
1036 turn_into_tuple(n, pn_Div_max);
1037 set_Tuple_pred(n, pn_Div_M, mem);
1038 set_Tuple_pred(n, pn_Div_X_regular, new_r_Jmp(current_ir_graph, blk));
1039 set_Tuple_pred(n, pn_Div_X_except, new_Bad()); /* no exception */
1040 set_Tuple_pred(n, pn_Div_res, a);
1043 } /* equivalent_node_Div */
1046 * Optimize a / 1.0 = a.
1048 static ir_node *equivalent_node_Quot(ir_node *n) {
1049 ir_node *a = get_Quot_left(n);
1050 ir_node *b = get_Quot_right(n);
1052 /* Div is not commutative. */
1053 if (classify_tarval(value_of(b)) == TV_CLASSIFY_ONE) { /* Quot(x, 1) == x */
1054 /* Turn Quot into a tuple (mem, jmp, bad, a) */
1055 ir_node *mem = get_Quot_mem(n);
1056 ir_node *blk = get_irn_n(n, -1);
1057 turn_into_tuple(n, pn_Quot_max);
1058 set_Tuple_pred(n, pn_Quot_M, mem);
1059 set_Tuple_pred(n, pn_Quot_X_regular, new_r_Jmp(current_ir_graph, blk));
1060 set_Tuple_pred(n, pn_Quot_X_except, new_Bad()); /* no exception */
1061 set_Tuple_pred(n, pn_Quot_res, a);
1064 } /* equivalent_node_Quot */
1067 * Optimize a / 1 = a.
1069 static ir_node *equivalent_node_DivMod(ir_node *n) {
1070 ir_node *b = get_DivMod_right(n);
1072 /* Div is not commutative. */
1073 if (classify_tarval(value_of(b)) == TV_CLASSIFY_ONE) { /* div(x, 1) == x */
1074 /* Turn DivMod into a tuple (mem, jmp, bad, a, 0) */
1075 ir_node *a = get_DivMod_left(n);
1076 ir_node *mem = get_Div_mem(n);
1077 ir_node *blk = get_irn_n(n, -1);
1078 ir_mode *mode = get_DivMod_resmode(n);
1080 turn_into_tuple(n, pn_DivMod_max);
1081 set_Tuple_pred(n, pn_DivMod_M, mem);
1082 set_Tuple_pred(n, pn_DivMod_X_regular, new_r_Jmp(current_ir_graph, blk));
1083 set_Tuple_pred(n, pn_DivMod_X_except, new_Bad()); /* no exception */
1084 set_Tuple_pred(n, pn_DivMod_res_div, a);
1085 set_Tuple_pred(n, pn_DivMod_res_mod, new_Const(mode, get_mode_null(mode)));
1088 } /* equivalent_node_DivMod */
1091 * Use algebraic simplification a | a = a | 0 = 0 | a = a.
1093 static ir_node *equivalent_node_Or(ir_node *n) {
1096 ir_node *a = get_Or_left(n);
1097 ir_node *b = get_Or_right(n);
1100 n = a; /* Or has it's own neutral element */
1101 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_OR);
1102 } else if (classify_tarval(value_of(a)) == TV_CLASSIFY_NULL) {
1104 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_OR);
1105 } else if (classify_tarval(value_of(b)) == TV_CLASSIFY_NULL) {
1107 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_OR);
1111 } /* equivalent_node_Or */
1114 * Optimize a & 0b1...1 = 0b1...1 & a = a & a = a.
1116 static ir_node *equivalent_node_And(ir_node *n) {
1119 ir_node *a = get_And_left(n);
1120 ir_node *b = get_And_right(n);
1123 n = a; /* And has it's own neutral element */
1124 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_AND);
1125 } else if (classify_tarval(value_of(a)) == TV_CLASSIFY_ALL_ONE) {
1127 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1128 } else if (classify_tarval(value_of(b)) == TV_CLASSIFY_ALL_ONE) {
1130 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1133 } /* equivalent_node_And */
1136 * Try to remove useless Conv's:
1138 static ir_node *equivalent_node_Conv(ir_node *n) {
1140 ir_node *a = get_Conv_op(n);
1143 ir_mode *n_mode = get_irn_mode(n);
1144 ir_mode *a_mode = get_irn_mode(a);
1146 if (n_mode == a_mode) { /* No Conv necessary */
1147 if (get_Conv_strict(n)) {
1148 /* special case: the predecessor might be a also a Conv */
1150 if (! get_Conv_strict(a)) {
1151 /* first one is not strict, kick it */
1152 set_Conv_op(n, get_Conv_op(a));
1155 /* else both are strict conv, second is superflous */
1157 /* leave strict floating point Conv's */
1162 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CONV);
1163 } else if (get_irn_op(a) == op_Conv) { /* Conv(Conv(b)) */
1167 n_mode = get_irn_mode(n);
1168 b_mode = get_irn_mode(b);
1170 if (n_mode == b_mode) {
1171 if (n_mode == mode_b) {
1172 n = b; /* Convb(Conv*(xxxb(...))) == xxxb(...) */
1173 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
1174 } else if (mode_is_int(n_mode)) {
1175 if (smaller_mode(b_mode, a_mode)){
1176 n = b; /* ConvS(ConvL(xxxS(...))) == xxxS(...) */
1177 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
1183 } /* equivalent_node_Conv */
1186 * A Cast may be removed if the type of the previous node
1187 * is already the type of the Cast.
1189 static ir_node *equivalent_node_Cast(ir_node *n) {
1191 ir_node *pred = get_Cast_op(n);
1193 if (get_irn_type(pred) == get_Cast_type(n)) {
1195 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CAST);
1198 } /* equivalent_node_Cast */
1201 * Several optimizations:
1202 * - no Phi in start block.
1203 * - remove Id operators that are inputs to Phi
1204 * - fold Phi-nodes, iff they have only one predecessor except
1207 static ir_node *equivalent_node_Phi(ir_node *n) {
1211 ir_node *block = NULL; /* to shutup gcc */
1212 ir_node *first_val = NULL; /* to shutup gcc */
1214 if (!get_opt_normalize()) return n;
1216 n_preds = get_Phi_n_preds(n);
1218 block = get_nodes_block(n);
1219 /* @@@ fliegt 'raus, sollte aber doch immer wahr sein!!!
1220 assert(get_irn_arity(block) == n_preds && "phi in wrong block!"); */
1221 if ((is_Block_dead(block)) || /* Control dead */
1222 (block == get_irg_start_block(current_ir_graph))) /* There should be no Phi nodes */
1223 return new_Bad(); /* in the Start Block. */
1225 if (n_preds == 0) return n; /* Phi of dead Region without predecessors. */
1227 /* If the Block has a Bad pred, we also have one. */
1228 for (i = 0; i < n_preds; ++i)
1229 if (is_Bad(get_Block_cfgpred(block, i)))
1230 set_Phi_pred(n, i, new_Bad());
1232 /* Find first non-self-referencing input */
1233 for (i = 0; i < n_preds; ++i) {
1234 first_val = get_Phi_pred(n, i);
1235 if ( (first_val != n) /* not self pointer */
1237 && (! is_Bad(first_val))
1239 ) { /* value not dead */
1240 break; /* then found first value. */
1245 /* A totally Bad or self-referencing Phi (we didn't break the above loop) */
1249 /* search for rest of inputs, determine if any of these
1250 are non-self-referencing */
1251 while (++i < n_preds) {
1252 ir_node *scnd_val = get_Phi_pred(n, i);
1253 if ( (scnd_val != n)
1254 && (scnd_val != first_val)
1256 && (! is_Bad(scnd_val))
1264 /* Fold, if no multiple distinct non-self-referencing inputs */
1266 DBG_OPT_PHI(oldn, n);
1269 } /* equivalent_node_Phi */
1272 * Several optimizations:
1273 * - no Sync in start block.
1274 * - fold Sync-nodes, iff they have only one predecessor except
1277 static ir_node *equivalent_node_Sync(ir_node *n) {
1281 ir_node *first_val = NULL; /* to shutup gcc */
1283 if (!get_opt_normalize()) return n;
1285 n_preds = get_Sync_n_preds(n);
1287 /* Find first non-self-referencing input */
1288 for (i = 0; i < n_preds; ++i) {
1289 first_val = get_Sync_pred(n, i);
1290 if ((first_val != n) /* not self pointer */ &&
1291 (! is_Bad(first_val))
1292 ) { /* value not dead */
1293 break; /* then found first value. */
1298 /* A totally Bad or self-referencing Sync (we didn't break the above loop) */
1301 /* search the rest of inputs, determine if any of these
1302 are non-self-referencing */
1303 while (++i < n_preds) {
1304 ir_node *scnd_val = get_Sync_pred(n, i);
1305 if ((scnd_val != n) &&
1306 (scnd_val != first_val) &&
1307 (! is_Bad(scnd_val))
1313 /* Fold, if no multiple distinct non-self-referencing inputs */
1315 DBG_OPT_SYNC(oldn, n);
1318 } /* equivalent_node_Sync */
1321 * Optimize Proj(Tuple) and gigo() for ProjX in Bad block,
1322 * ProjX(Load) and ProjX(Store).
1324 static ir_node *equivalent_node_Proj(ir_node *proj) {
1325 ir_node *oldn = proj;
1326 ir_node *a = get_Proj_pred(proj);
1328 if (get_irn_op(a) == op_Tuple) {
1329 /* Remove the Tuple/Proj combination. */
1330 if ( get_Proj_proj(proj) <= get_Tuple_n_preds(a) ) {
1331 proj = get_Tuple_pred(a, get_Proj_proj(proj));
1332 DBG_OPT_TUPLE(oldn, a, proj);
1334 /* This should not happen! */
1335 assert(! "found a Proj with higher number than Tuple predecessors");
1338 } else if (get_irn_mode(proj) == mode_X) {
1339 if (is_Block_dead(get_nodes_block(skip_Proj(proj)))) {
1340 /* Remove dead control flow -- early gigo(). */
1342 } else if (get_opt_ldst_only_null_ptr_exceptions()) {
1343 ir_op *op = get_irn_op(a);
1345 if (op == op_Load) {
1346 /* get the Load address */
1347 ir_node *addr = get_Load_ptr(a);
1348 ir_node *blk = get_irn_n(a, -1);
1351 if (value_not_null(addr, &confirm)) {
1352 if (confirm == NULL) {
1353 /* this node may float if it did not depend on a Confirm */
1354 set_irn_pinned(a, op_pin_state_floats);
1356 if (get_Proj_proj(proj) == pn_Load_X_except) {
1357 DBG_OPT_EXC_REM(proj);
1360 return new_r_Jmp(current_ir_graph, blk);
1362 } else if (op == op_Store) {
1363 /* get the load/store address */
1364 ir_node *addr = get_Store_ptr(a);
1365 ir_node *blk = get_irn_n(a, -1);
1368 if (value_not_null(addr, &confirm)) {
1369 if (confirm == NULL) {
1370 /* this node may float if it did not depend on a Confirm */
1371 set_irn_pinned(a, op_pin_state_floats);
1373 if (get_Proj_proj(proj) == pn_Store_X_except) {
1374 DBG_OPT_EXC_REM(proj);
1377 return new_r_Jmp(current_ir_graph, blk);
1384 } /* equivalent_node_Proj */
1389 static ir_node *equivalent_node_Id(ir_node *n) {
1394 } while (get_irn_op(n) == op_Id);
1396 DBG_OPT_ID(oldn, n);
1398 } /* equivalent_node_Id */
1403 static ir_node *equivalent_node_Mux(ir_node *n)
1405 ir_node *oldn = n, *sel = get_Mux_sel(n);
1406 tarval *ts = value_of(sel);
1408 /* Mux(true, f, t) == t */
1409 if (ts == tarval_b_true) {
1410 n = get_Mux_true(n);
1411 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_C);
1413 /* Mux(false, f, t) == f */
1414 else if (ts == tarval_b_false) {
1415 n = get_Mux_false(n);
1416 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_C);
1418 /* Mux(v, x, x) == x */
1419 else if (get_Mux_false(n) == get_Mux_true(n)) {
1420 n = get_Mux_true(n);
1421 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_EQ);
1423 else if (get_irn_op(sel) == op_Proj && !mode_honor_signed_zeros(get_irn_mode(n))) {
1424 ir_node *cmp = get_Proj_pred(sel);
1425 long proj_nr = get_Proj_proj(sel);
1426 ir_node *b = get_Mux_false(n);
1427 ir_node *a = get_Mux_true(n);
1430 * Note: normalization puts the constant on the right site,
1431 * so we check only one case.
1433 * Note further that these optimization work even for floating point
1434 * with NaN's because -NaN == NaN.
1435 * However, if +0 and -0 is handled differently, we cannot use the first one.
1437 if (get_irn_op(cmp) == op_Cmp && get_Cmp_left(cmp) == a) {
1438 if (classify_Const(get_Cmp_right(cmp)) == CNST_NULL) {
1439 /* Mux(a CMP 0, X, a) */
1440 if (get_irn_op(b) == op_Minus && get_Minus_op(b) == a) {
1441 /* Mux(a CMP 0, -a, a) */
1442 if (proj_nr == pn_Cmp_Eq) {
1443 /* Mux(a == 0, -a, a) ==> -a */
1445 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1446 } else if (proj_nr == pn_Cmp_Lg || proj_nr == pn_Cmp_Ne) {
1447 /* Mux(a != 0, -a, a) ==> a */
1449 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1451 } else if (classify_Const(b) == CNST_NULL) {
1452 /* Mux(a CMP 0, 0, a) */
1453 if (proj_nr == pn_Cmp_Lg || proj_nr == pn_Cmp_Ne) {
1454 /* Mux(a != 0, 0, a) ==> a */
1456 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1457 } else if (proj_nr == pn_Cmp_Eq) {
1458 /* Mux(a == 0, 0, a) ==> 0 */
1460 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1467 } /* equivalent_node_Mux */
1470 * Returns a equivalent node of a Psi: if a condition is true
1471 * and all previous conditions are false we know its value.
1472 * If all conditions are false its value is the default one.
1474 static ir_node *equivalent_node_Psi(ir_node *n) {
1476 return equivalent_node_Mux(n);
1478 } /* equivalent_node_Psi */
1481 * Optimize -a CMP -b into b CMP a.
1482 * This works only for for modes where unary Minus
1484 * Note that two-complement integers can Overflow
1485 * so it will NOT work.
1487 * For == and != can be handled in Proj(Cmp)
1489 static ir_node *equivalent_node_Cmp(ir_node *n) {
1490 ir_node *left = get_Cmp_left(n);
1491 ir_node *right = get_Cmp_right(n);
1493 if (get_irn_op(left) == op_Minus && get_irn_op(right) == op_Minus &&
1494 !mode_overflow_on_unary_Minus(get_irn_mode(left))) {
1495 left = get_Minus_op(left);
1496 right = get_Minus_op(right);
1497 set_Cmp_left(n, right);
1498 set_Cmp_right(n, left);
1501 } /* equivalent_node_Cmp */
1504 * Remove Confirm nodes if setting is on.
1505 * Replace Confirms(x, '=', Constlike) by Constlike.
1507 static ir_node *equivalent_node_Confirm(ir_node *n) {
1508 ir_node *pred = get_Confirm_value(n);
1509 pn_Cmp pnc = get_Confirm_cmp(n);
1511 if (get_irn_op(pred) == op_Confirm && pnc == get_Confirm_cmp(pred)) {
1513 * rare case: two identical Confirms one after another,
1514 * replace the second one with the first.
1518 if (pnc == pn_Cmp_Eq) {
1519 ir_node *bound = get_Confirm_bound(n);
1522 * Optimize a rare case:
1523 * Confirm(x, '=', Constlike) ==> Constlike
1525 if (is_irn_constlike(bound)) {
1526 DBG_OPT_CONFIRM(n, bound);
1530 return get_opt_remove_confirm() ? get_Confirm_value(n) : n;
1534 * Optimize CopyB(mem, x, x) into a Nop.
1536 static ir_node *equivalent_node_CopyB(ir_node *n) {
1537 ir_node *a = get_CopyB_dst(n);
1538 ir_node *b = get_CopyB_src(n);
1541 /* Turn CopyB into a tuple (mem, jmp, bad, bad) */
1542 ir_node *mem = get_CopyB_mem(n);
1543 ir_node *blk = get_nodes_block(n);
1544 turn_into_tuple(n, pn_CopyB_max);
1545 set_Tuple_pred(n, pn_CopyB_M, mem);
1546 set_Tuple_pred(n, pn_CopyB_X_regular, new_r_Jmp(current_ir_graph, blk));
1547 set_Tuple_pred(n, pn_CopyB_X_except, new_Bad()); /* no exception */
1548 set_Tuple_pred(n, pn_CopyB_M_except, new_Bad());
1551 } /* equivalent_node_CopyB */
1554 * Optimize Bounds(idx, idx, upper) into idx.
1556 static ir_node *equivalent_node_Bound(ir_node *n) {
1557 ir_node *idx = get_Bound_index(n);
1558 ir_node *lower = get_Bound_lower(n);
1561 /* By definition lower < upper, so if idx == lower -->
1562 lower <= idx && idx < upper */
1564 /* Turn Bound into a tuple (mem, jmp, bad, idx) */
1567 ir_node *pred = skip_Proj(idx);
1569 if (get_irn_op(pred) == op_Bound) {
1571 * idx was Bounds_check previously, it is still valid if
1572 * lower <= pred_lower && pred_upper <= upper.
1574 ir_node *upper = get_Bound_upper(n);
1575 if (get_Bound_lower(pred) == lower &&
1576 get_Bound_upper(pred) == upper) {
1578 * One could expect that we simply return the previous
1579 * Bound here. However, this would be wrong, as we could
1580 * add an exception Proj to a new location then.
1581 * So, we must turn in into a tuple.
1588 /* Turn Bound into a tuple (mem, jmp, bad, idx) */
1589 ir_node *mem = get_Bound_mem(n);
1590 ir_node *blk = get_nodes_block(n);
1591 turn_into_tuple(n, pn_Bound_max);
1592 set_Tuple_pred(n, pn_Bound_M, mem);
1593 set_Tuple_pred(n, pn_Bound_X_regular, new_r_Jmp(current_ir_graph, blk)); /* no exception */
1594 set_Tuple_pred(n, pn_Bound_X_except, new_Bad()); /* no exception */
1595 set_Tuple_pred(n, pn_Bound_res, idx);
1598 } /* equivalent_node_Bound */
1601 * equivalent_node() returns a node equivalent to input n. It skips all nodes that
1602 * perform no actual computation, as, e.g., the Id nodes. It does not create
1603 * new nodes. It is therefore safe to free n if the node returned is not n.
1604 * If a node returns a Tuple we can not just skip it. If the size of the
1605 * in array fits, we transform n into a tuple (e.g., Div).
1607 ir_node *equivalent_node(ir_node *n) {
1608 if (n->op->ops.equivalent_node)
1609 return n->op->ops.equivalent_node(n);
1611 } /* equivalent_node */
1614 * Sets the default equivalent node operation for an ir_op_ops.
1616 * @param code the opcode for the default operation
1617 * @param ops the operations initialized
1622 static ir_op_ops *firm_set_default_equivalent_node(ir_opcode code, ir_op_ops *ops)
1626 ops->equivalent_node = equivalent_node_##a; \
1666 } /* firm_set_default_equivalent_node */
1669 * Do node specific optimizations of nodes predecessors.
1671 static void optimize_preds(ir_node *n) {
1672 switch (get_irn_opcode(n)) {
1674 case iro_Cmp: { /* We don't want Cast as input to Cmp. */
1675 ir_node *a = get_Cmp_left(n), *b = get_Cmp_right(n);
1677 if (get_irn_op(a) == op_Cast) {
1681 if (get_irn_op(b) == op_Cast) {
1683 set_Cmp_right(n, b);
1690 } /* optimize_preds */
1693 * Returns non-zero if a node is a Phi node
1694 * with all predecessors constant.
1696 static int is_const_Phi(ir_node *n) {
1701 for (i = get_irn_arity(n) - 1; i >= 0; --i)
1702 if (! is_Const(get_irn_n(n, i)))
1705 } /* is_const_Phi */
1708 * Apply an evaluator on a binop with a constant operators (and one Phi).
1710 * @param phi the Phi node
1711 * @param other the other operand
1712 * @param eval an evaluator function
1713 * @param left if non-zero, other is the left operand, else the right
1715 * @return a new Phi node if the conversion was successful, NULL else
1717 static ir_node *apply_binop_on_phi(ir_node *phi, tarval *other, tarval *(*eval)(tarval *, tarval *), int left) {
1723 int i, n = get_irn_arity(phi);
1725 NEW_ARR_A(void *, res, n);
1727 for (i = 0; i < n; ++i) {
1728 pred = get_irn_n(phi, i);
1729 tv = get_Const_tarval(pred);
1730 tv = eval(other, tv);
1732 if (tv == tarval_bad) {
1733 /* folding failed, bad */
1739 for (i = 0; i < n; ++i) {
1740 pred = get_irn_n(phi, i);
1741 tv = get_Const_tarval(pred);
1742 tv = eval(tv, other);
1744 if (tv == tarval_bad) {
1745 /* folding failed, bad */
1751 mode = get_irn_mode(phi);
1752 irg = current_ir_graph;
1753 for (i = 0; i < n; ++i) {
1754 pred = get_irn_n(phi, i);
1755 res[i] = new_r_Const_type(irg, get_irg_start_block(irg),
1756 mode, res[i], get_Const_type(pred));
1758 return new_r_Phi(irg, get_nodes_block(phi), n, (ir_node **)res, mode);
1759 } /* apply_binop_on_phi */
1762 * Apply an evaluator on a unop with a constant operator (a Phi).
1764 * @param phi the Phi node
1765 * @param eval an evaluator function
1767 * @return a new Phi node if the conversion was successful, NULL else
1769 static ir_node *apply_unop_on_phi(ir_node *phi, tarval *(*eval)(tarval *)) {
1775 int i, n = get_irn_arity(phi);
1777 NEW_ARR_A(void *, res, n);
1778 for (i = 0; i < n; ++i) {
1779 pred = get_irn_n(phi, i);
1780 tv = get_Const_tarval(pred);
1783 if (tv == tarval_bad) {
1784 /* folding failed, bad */
1789 mode = get_irn_mode(phi);
1790 irg = current_ir_graph;
1791 for (i = 0; i < n; ++i) {
1792 pred = get_irn_n(phi, i);
1793 res[i] = new_r_Const_type(irg, get_irg_start_block(irg),
1794 mode, res[i], get_Const_type(pred));
1796 return new_r_Phi(irg, get_nodes_block(phi), n, (ir_node **)res, mode);
1797 } /* apply_unop_on_phi */
1800 * Transform AddP(P, ConvIs(Iu)), AddP(P, ConvIu(Is)) and
1801 * SubP(P, ConvIs(Iu)), SubP(P, ConvIu(Is)).
1802 * If possible, remove the Conv's.
1804 static ir_node *transform_node_AddSub(ir_node *n) {
1805 ir_mode *mode = get_irn_mode(n);
1807 if (mode_is_reference(mode)) {
1808 ir_node *left = get_binop_left(n);
1809 ir_node *right = get_binop_right(n);
1810 int ref_bits = get_mode_size_bits(mode);
1812 if (get_irn_op(left) == op_Conv) {
1813 ir_mode *mode = get_irn_mode(left);
1814 int bits = get_mode_size_bits(mode);
1816 if (ref_bits == bits &&
1817 mode_is_int(mode) &&
1818 get_mode_arithmetic(mode) == irma_twos_complement) {
1819 ir_node *pre = get_Conv_op(left);
1820 ir_mode *pre_mode = get_irn_mode(pre);
1822 if (mode_is_int(pre_mode) &&
1823 get_mode_size_bits(pre_mode) == bits &&
1824 get_mode_arithmetic(pre_mode) == irma_twos_complement) {
1825 /* ok, this conv just changes to sign, moreover the calculation
1826 * is done with same number of bits as our address mode, so
1827 * we can ignore the conv as address calculation can be viewed
1828 * as either signed or unsigned
1830 set_binop_left(n, pre);
1835 if (get_irn_op(right) == op_Conv) {
1836 ir_mode *mode = get_irn_mode(right);
1837 int bits = get_mode_size_bits(mode);
1839 if (ref_bits == bits &&
1840 mode_is_int(mode) &&
1841 get_mode_arithmetic(mode) == irma_twos_complement) {
1842 ir_node *pre = get_Conv_op(right);
1843 ir_mode *pre_mode = get_irn_mode(pre);
1845 if (mode_is_int(pre_mode) &&
1846 get_mode_size_bits(pre_mode) == bits &&
1847 get_mode_arithmetic(pre_mode) == irma_twos_complement) {
1848 /* ok, this conv just changes to sign, moreover the calculation
1849 * is done with same number of bits as our address mode, so
1850 * we can ignore the conv as address calculation can be viewed
1851 * as either signed or unsigned
1853 set_binop_right(n, pre);
1859 } /* transform_node_AddSub */
1861 #define HANDLE_BINOP_PHI(op,a,b,c) \
1863 if (is_Const(b) && is_const_Phi(a)) { \
1864 /* check for Op(Phi, Const) */ \
1865 c = apply_binop_on_phi(a, get_Const_tarval(b), op, 0); \
1867 else if (is_Const(a) && is_const_Phi(b)) { \
1868 /* check for Op(Const, Phi) */ \
1869 c = apply_binop_on_phi(b, get_Const_tarval(a), op, 1); \
1872 DBG_OPT_ALGSIM0(oldn, c, FS_OPT_CONST_PHI); \
1876 #define HANDLE_UNOP_PHI(op,a,c) \
1878 if (is_const_Phi(a)) { \
1879 /* check for Op(Phi) */ \
1880 c = apply_unop_on_phi(a, op); \
1883 DBG_OPT_ALGSIM0(oldn, c, FS_OPT_CONST_PHI); \
1889 * Do the AddSub optimization, then Transform
1890 * Constant folding on Phi
1891 * Add(a,a) -> Mul(a, 2)
1892 * Add(Mul(a, x), a) -> Mul(a, x+1)
1893 * if the mode is integer or float.
1894 * Transform Add(a,-b) into Sub(a,b).
1895 * Reassociation might fold this further.
1897 static ir_node *transform_node_Add(ir_node *n) {
1899 ir_node *a, *b, *c, *oldn = n;
1901 n = transform_node_AddSub(n);
1903 a = get_Add_left(n);
1904 b = get_Add_right(n);
1906 HANDLE_BINOP_PHI(tarval_add, a,b,c);
1908 mode = get_irn_mode(n);
1910 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
1911 if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
1914 if (mode_is_num(mode)) {
1915 /* the following code leads to endless recursion when Mul are replaced by a simple instruction chain */
1916 if (!get_opt_arch_dep_running() && a == b && mode_is_int(mode)) {
1917 ir_node *block = get_irn_n(n, -1);
1920 get_irn_dbg_info(n),
1924 new_r_Const_long(current_ir_graph, block, mode, 2),
1926 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_A);
1931 get_irn_dbg_info(n),
1937 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_MINUS_B);
1942 get_irn_dbg_info(n),
1948 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_MINUS_B);
1951 if (! get_opt_reassociation()) {
1952 /* do NOT execute this code if reassociation is enabled, it does the inverse! */
1954 ir_node *ma = get_Mul_left(a);
1955 ir_node *mb = get_Mul_right(a);
1958 ir_node *blk = get_irn_n(n, -1);
1960 get_irn_dbg_info(n), current_ir_graph, blk,
1963 get_irn_dbg_info(n), current_ir_graph, blk,
1965 new_r_Const_long(current_ir_graph, blk, mode, 1),
1968 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_MUL_A_X_A);
1970 } else if (b == mb) {
1971 ir_node *blk = get_irn_n(n, -1);
1973 get_irn_dbg_info(n), current_ir_graph, blk,
1976 get_irn_dbg_info(n), current_ir_graph, blk,
1978 new_r_Const_long(current_ir_graph, blk, mode, 1),
1981 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_MUL_A_X_A);
1986 ir_node *ma = get_Mul_left(b);
1987 ir_node *mb = get_Mul_right(b);
1990 ir_node *blk = get_irn_n(n, -1);
1992 get_irn_dbg_info(n), current_ir_graph, blk,
1995 get_irn_dbg_info(n), current_ir_graph, blk,
1997 new_r_Const_long(current_ir_graph, blk, mode, 1),
2000 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_MUL_A_X_A);
2004 ir_node *blk = get_irn_n(n, -1);
2006 get_irn_dbg_info(n), current_ir_graph, blk,
2009 get_irn_dbg_info(n), current_ir_graph, blk,
2011 new_r_Const_long(current_ir_graph, blk, mode, 1),
2014 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_MUL_A_X_A);
2019 /* Here we rely on constants be on the RIGHT side */
2020 if (get_mode_arithmetic(mode) == irma_twos_complement &&
2021 is_Not(a) && classify_Const(b) == CNST_ONE) {
2023 ir_node *op = get_Not_op(a);
2024 ir_node *blk = get_irn_n(n, -1);
2025 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, blk, op, mode);
2026 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_PLUS_1);
2031 } /* transform_node_Add */
2034 * Do the AddSub optimization, then Transform
2035 * Constant folding on Phi
2036 * Sub(0,a) -> Minus(a)
2037 * Sub(Mul(a, x), a) -> Mul(a, x-1)
2038 * Sub(Sub(x, y), b) -> Sub(x, Add(y,b))
2039 * Sub(Add(a, x), x) -> a
2040 * Sub(x, Add(x, a)) -> -a
2041 * Sub(x, Const) -> Add(x, -Const)
2043 static ir_node *transform_node_Sub(ir_node *n) {
2048 n = transform_node_AddSub(n);
2050 a = get_Sub_left(n);
2051 b = get_Sub_right(n);
2053 mode = get_irn_mode(n);
2056 HANDLE_BINOP_PHI(tarval_sub, a,b,c);
2058 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
2059 if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
2062 /* Sub(a, Const) -> Add(a, -Const) */
2063 if (is_Const(b) && get_irn_mode(b) != mode_P) {
2064 tarval *tv = get_Const_tarval(b);
2066 tv = tarval_neg(tv);
2067 if(tv != tarval_bad) {
2068 ir_node *cnst = new_Const(get_irn_mode(b), tv);
2069 ir_node *block = get_nodes_block(n);
2070 dbg_info *dbgi = get_irn_dbg_info(n);
2071 ir_graph *irg = get_irn_irg(n);
2072 ir_node *add = new_rd_Add(dbgi, irg, block, a, cnst, mode);
2078 /* Beware of Sub(P, P) which cannot be optimized into a simple Minus ... */
2079 if (mode_is_num(mode) && mode == get_irn_mode(a) && (classify_Const(a) == CNST_NULL)) {
2081 get_irn_dbg_info(n),
2086 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_0_A);
2090 if (mode_wrap_around(mode)) {
2091 ir_node *left = get_Add_left(a);
2092 ir_node *right = get_Add_right(a);
2094 /* FIXME: Does the Conv's work only for two complement or generally? */
2096 if (mode != get_irn_mode(right)) {
2097 /* This Sub is an effective Cast */
2098 right = new_r_Conv(get_irn_irg(n), get_irn_n(n, -1), right, mode);
2101 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2103 } else if (right == b) {
2104 if (mode != get_irn_mode(left)) {
2105 /* This Sub is an effective Cast */
2106 left = new_r_Conv(get_irn_irg(n), get_irn_n(n, -1), left, mode);
2109 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2115 if (mode_wrap_around(mode)) {
2116 ir_node *left = get_Add_left(b);
2117 ir_node *right = get_Add_right(b);
2119 /* FIXME: Does the Conv's work only for two complement or generally? */
2121 ir_mode *r_mode = get_irn_mode(right);
2123 n = new_r_Minus(get_irn_irg(n), get_irn_n(n, -1), right, r_mode);
2124 if (mode != r_mode) {
2125 /* This Sub is an effective Cast */
2126 n = new_r_Conv(get_irn_irg(n), get_irn_n(n, -1), n, mode);
2128 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2130 } else if (right == a) {
2131 ir_mode *l_mode = get_irn_mode(left);
2133 n = new_r_Minus(get_irn_irg(n), get_irn_n(n, -1), left, l_mode);
2134 if (mode != l_mode) {
2135 /* This Sub is an effective Cast */
2136 n = new_r_Conv(get_irn_irg(n), get_irn_n(n, -1), n, mode);
2138 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2143 if (mode_is_int(mode) && is_Conv(a) && is_Conv(b)) {
2144 ir_mode *mode = get_irn_mode(a);
2146 if (mode == get_irn_mode(b)) {
2152 /* check if it's allowed to skip the conv */
2153 ma = get_irn_mode(a);
2154 mb = get_irn_mode(b);
2156 if (mode_is_reference(ma) && mode_is_reference(mb)) {
2157 /* SubInt(ConvInt(aP), ConvInt(bP)) -> SubInt(aP,bP) */
2159 set_Sub_right(n, b);
2165 /* do NOT execute this code if reassociation is enabled, it does the inverse! */
2166 if (get_opt_reassociation() && get_irn_op(a) == op_Mul) {
2167 ir_node *ma = get_Mul_left(a);
2168 ir_node *mb = get_Mul_right(a);
2171 ir_node *blk = get_irn_n(n, -1);
2173 get_irn_dbg_info(n),
2174 current_ir_graph, blk,
2177 get_irn_dbg_info(n),
2178 current_ir_graph, blk,
2180 new_r_Const_long(current_ir_graph, blk, mode, 1),
2183 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MUL_A_X_A);
2185 } else if (mb == b) {
2186 ir_node *blk = get_irn_n(n, -1);
2188 get_irn_dbg_info(n),
2189 current_ir_graph, blk,
2192 get_irn_dbg_info(n),
2193 current_ir_graph, blk,
2195 new_r_Const_long(current_ir_graph, blk, mode, 1),
2198 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MUL_A_X_A);
2203 ir_node *x = get_Sub_left(a);
2204 ir_node *y = get_Sub_right(a);
2205 ir_node *blk = get_irn_n(n, -1);
2206 ir_mode *m_b = get_irn_mode(b);
2207 ir_mode *m_y = get_irn_mode(y);
2210 /* Determine the right mode for the Add. */
2213 else if (mode_is_reference(m_b))
2215 else if (mode_is_reference(m_y))
2219 * Both modes are different but none is reference,
2220 * happens for instance in SubP(SubP(P, Iu), Is).
2221 * We have two possibilities here: Cast or ignore.
2222 * Currently we ignore this case.
2227 add = new_r_Add(current_ir_graph, blk, y, b, mode);
2230 set_Sub_right(n, add);
2231 DBG_OPT_ALGSIM0(n, n, FS_OPT_SUB_SUB_X_Y_Z);
2235 } /* transform_node_Sub */
2238 * Transform Mul(a,-1) into -a.
2239 * Do constant evaluation of Phi nodes.
2240 * Do architecture dependent optimizations on Mul nodes
2242 static ir_node *transform_node_Mul(ir_node *n) {
2243 ir_node *c, *oldn = n;
2244 ir_node *a = get_Mul_left(n);
2245 ir_node *b = get_Mul_right(n);
2248 HANDLE_BINOP_PHI(tarval_mul, a,b,c);
2250 mode = get_irn_mode(n);
2251 if (mode_is_signed(mode)) {
2254 if (value_of(a) == get_mode_minus_one(mode))
2256 else if (value_of(b) == get_mode_minus_one(mode))
2259 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1), r, mode);
2260 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2264 if (get_mode_arithmetic(mode) == irma_ieee754) {
2266 tarval *tv = get_Const_tarval(a);
2267 if (tarval_ieee754_get_exponent(tv) == 1 && tarval_ieee754_zero_mantissa(tv)) {
2268 n = new_rd_Add(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1), b, b, mode);
2269 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_A_A);
2273 else if (is_Const(b)) {
2274 tarval *tv = get_Const_tarval(b);
2275 if (tarval_ieee754_get_exponent(tv) == 1 && tarval_ieee754_zero_mantissa(tv)) {
2276 n = new_rd_Add(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1), a, a, mode);
2277 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_A_A);
2282 return arch_dep_replace_mul_with_shifts(n);
2283 } /* transform_node_Mul */
2286 * Transform a Div Node.
2288 static ir_node *transform_node_Div(ir_node *n) {
2289 tarval *tv = value_of(n);
2290 ir_mode *mode = get_Div_resmode(n);
2293 if (tv != tarval_bad) {
2294 value = new_Const(get_tarval_mode(tv), tv);
2296 DBG_OPT_CSTEVAL(n, value);
2299 ir_node *a = get_Div_left(n);
2300 ir_node *b = get_Div_right(n);
2303 if (a == b && value_not_zero(a, &dummy)) {
2304 /* BEWARE: we can optimize a/a to 1 only if this cannot cause a exception */
2305 value = new_Const(mode, get_mode_one(mode));
2306 DBG_OPT_CSTEVAL(n, value);
2309 if (mode_is_signed(mode) && is_Const(b)) {
2310 tarval *tv = get_Const_tarval(b);
2312 if (tv == get_mode_minus_one(mode)) {
2314 value = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1), a, mode);
2315 DBG_OPT_CSTEVAL(n, value);
2319 /* Try architecture dependent optimization */
2320 value = arch_dep_replace_div_by_const(n);
2328 /* Turn Div into a tuple (mem, jmp, bad, value) */
2329 mem = get_Div_mem(n);
2330 blk = get_irn_n(n, -1);
2332 /* skip a potential Pin */
2334 mem = get_Pin_op(mem);
2335 turn_into_tuple(n, pn_Div_max);
2336 set_Tuple_pred(n, pn_Div_M, mem);
2337 set_Tuple_pred(n, pn_Div_X_regular, new_r_Jmp(current_ir_graph, blk));
2338 set_Tuple_pred(n, pn_Div_X_except, new_Bad());
2339 set_Tuple_pred(n, pn_Div_res, value);
2342 } /* transform_node_Div */
2345 * Transform a Mod node.
2347 static ir_node *transform_node_Mod(ir_node *n) {
2348 tarval *tv = value_of(n);
2349 ir_mode *mode = get_Mod_resmode(n);
2352 if (tv != tarval_bad) {
2353 value = new_Const(get_tarval_mode(tv), tv);
2355 DBG_OPT_CSTEVAL(n, value);
2358 ir_node *a = get_Mod_left(n);
2359 ir_node *b = get_Mod_right(n);
2362 if (a == b && value_not_zero(a, &dummy)) {
2363 /* BEWARE: we can optimize a%a to 0 only if this cannot cause a exception */
2364 value = new_Const(mode, get_mode_null(mode));
2365 DBG_OPT_CSTEVAL(n, value);
2368 if (mode_is_signed(mode) && is_Const(b)) {
2369 tarval *tv = get_Const_tarval(b);
2371 if (tv == get_mode_minus_one(mode)) {
2373 value = new_Const(mode, get_mode_null(mode));
2374 DBG_OPT_CSTEVAL(n, value);
2378 /* Try architecture dependent optimization */
2379 value = arch_dep_replace_mod_by_const(n);
2387 /* Turn Mod into a tuple (mem, jmp, bad, value) */
2388 mem = get_Mod_mem(n);
2389 blk = get_irn_n(n, -1);
2391 /* skip a potential Pin */
2393 mem = get_Pin_op(mem);
2394 turn_into_tuple(n, pn_Mod_max);
2395 set_Tuple_pred(n, pn_Mod_M, mem);
2396 set_Tuple_pred(n, pn_Mod_X_regular, new_r_Jmp(current_ir_graph, blk));
2397 set_Tuple_pred(n, pn_Mod_X_except, new_Bad());
2398 set_Tuple_pred(n, pn_Mod_res, value);
2401 } /* transform_node_Mod */
2404 * Transform a DivMod node.
2406 static ir_node *transform_node_DivMod(ir_node *n) {
2408 ir_node *a = get_DivMod_left(n);
2409 ir_node *b = get_DivMod_right(n);
2410 ir_mode *mode = get_DivMod_resmode(n);
2411 tarval *ta = value_of(a);
2412 tarval *tb = value_of(b);
2415 if (tb != tarval_bad) {
2416 if (tb == get_mode_one(get_tarval_mode(tb))) {
2417 b = new_Const(mode, get_mode_null(mode));
2418 DBG_OPT_CSTEVAL(n, b);
2420 } else if (ta != tarval_bad) {
2421 tarval *resa, *resb;
2422 resa = tarval_div(ta, tb);
2423 if (resa == tarval_bad) return n; /* Causes exception!!! Model by replacing through
2424 Jmp for X result!? */
2425 resb = tarval_mod(ta, tb);
2426 if (resb == tarval_bad) return n; /* Causes exception! */
2427 a = new_Const(mode, resa);
2428 b = new_Const(mode, resb);
2429 DBG_OPT_CSTEVAL(n, a);
2430 DBG_OPT_CSTEVAL(n, b);
2432 } else if (mode_is_signed(mode) && tb == get_mode_minus_one(mode)) {
2433 a = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1), a, mode);
2434 b = new_Const(mode, get_mode_null(mode));
2435 DBG_OPT_CSTEVAL(n, a);
2436 DBG_OPT_CSTEVAL(n, b);
2438 } else { /* Try architecture dependent optimization */
2439 arch_dep_replace_divmod_by_const(&a, &b, n);
2440 evaluated = a != NULL;
2442 } else if (a == b) {
2443 if (value_not_zero(a, &dummy)) {
2445 a = new_Const(mode, get_mode_one(mode));
2446 b = new_Const(mode, get_mode_null(mode));
2447 DBG_OPT_CSTEVAL(n, a);
2448 DBG_OPT_CSTEVAL(n, b);
2451 /* BEWARE: it is NOT possible to optimize a/a to 1, as this may cause a exception */
2454 } else if (ta == get_mode_null(mode) && value_not_zero(b, &dummy)) {
2455 /* 0 / non-Const = 0 */
2460 if (evaluated) { /* replace by tuple */
2464 mem = get_DivMod_mem(n);
2465 /* skip a potential Pin */
2467 mem = get_Pin_op(mem);
2469 blk = get_irn_n(n, -1);
2470 turn_into_tuple(n, pn_DivMod_max);
2471 set_Tuple_pred(n, pn_DivMod_M, mem);
2472 set_Tuple_pred(n, pn_DivMod_X_regular, new_r_Jmp(current_ir_graph, blk));
2473 set_Tuple_pred(n, pn_DivMod_X_except, new_Bad()); /* no exception */
2474 set_Tuple_pred(n, pn_DivMod_res_div, a);
2475 set_Tuple_pred(n, pn_DivMod_res_mod, b);
2479 } /* transform_node_DivMod */
2482 * Optimize x / c to x * (1/c)
2484 static ir_node *transform_node_Quot(ir_node *n) {
2485 ir_mode *mode = get_Quot_resmode(n);
2488 if (get_mode_arithmetic(mode) == irma_ieee754) {
2489 ir_node *b = get_Quot_right(n);
2492 tarval *tv = get_Const_tarval(b);
2494 tv = tarval_quo(get_mode_one(mode), tv);
2496 /* Do the transformation if the result is either exact or we are not
2497 using strict rules. */
2498 if (tv != tarval_bad &&
2499 (tarval_ieee754_get_exact() || (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic) == 0)) {
2500 ir_node *blk = get_irn_n(n, -1);
2501 ir_node *c = new_r_Const(current_ir_graph, blk, mode, tv);
2502 ir_node *a = get_Quot_left(n);
2503 ir_node *m = new_rd_Mul(get_irn_dbg_info(n), current_ir_graph, blk, a, c, mode);
2504 ir_node *mem = get_Quot_mem(n);
2506 /* skip a potential Pin */
2508 mem = get_Pin_op(mem);
2509 turn_into_tuple(n, pn_Quot_max);
2510 set_Tuple_pred(n, pn_Quot_M, mem);
2511 set_Tuple_pred(n, pn_Quot_X_regular, new_r_Jmp(current_ir_graph, blk));
2512 set_Tuple_pred(n, pn_Quot_X_except, new_r_Bad(current_ir_graph));
2513 set_Tuple_pred(n, pn_Quot_res, m);
2514 DBG_OPT_ALGSIM1(oldn, a, b, m, FS_OPT_FP_INV_MUL);
2519 } /* transform_node_Quot */
2522 * Optimize Abs(x) into x if x is Confirmed >= 0
2523 * Optimize Abs(x) into -x if x is Confirmed <= 0
2525 static ir_node *transform_node_Abs(ir_node *n) {
2527 ir_node *a = get_Abs_op(n);
2528 value_classify_sign sign = classify_value_sign(a);
2530 if (sign == value_classified_negative) {
2531 ir_mode *mode = get_irn_mode(n);
2534 * We can replace the Abs by -x here.
2535 * We even could add a new Confirm here.
2537 * Note that -x would create a new node, so we could
2538 * not run it in the equivalent_node() context.
2540 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph,
2541 get_irn_n(n, -1), a, mode);
2543 DBG_OPT_CONFIRM(oldn, n);
2544 } else if (sign == value_classified_positive) {
2545 /* n is positive, Abs is not needed */
2548 DBG_OPT_CONFIRM(oldn, n);
2552 } /* transform_node_Abs */
2555 * Transform a Cond node.
2557 * Replace the Cond by a Jmp if it branches on a constant
2560 static ir_node *transform_node_Cond(ir_node *n) {
2563 ir_node *a = get_Cond_selector(n);
2564 tarval *ta = value_of(a);
2566 /* we need block info which is not available in floating irgs */
2567 if (get_irg_pinned(current_ir_graph) == op_pin_state_floats)
2570 if ((ta != tarval_bad) &&
2571 (get_irn_mode(a) == mode_b) &&
2572 (get_opt_unreachable_code())) {
2573 /* It's a boolean Cond, branching on a boolean constant.
2574 Replace it by a tuple (Bad, Jmp) or (Jmp, Bad) */
2575 jmp = new_r_Jmp(current_ir_graph, get_nodes_block(n));
2576 turn_into_tuple(n, pn_Cond_max);
2577 if (ta == tarval_b_true) {
2578 set_Tuple_pred(n, pn_Cond_false, new_Bad());
2579 set_Tuple_pred(n, pn_Cond_true, jmp);
2581 set_Tuple_pred(n, pn_Cond_false, jmp);
2582 set_Tuple_pred(n, pn_Cond_true, new_Bad());
2584 /* We might generate an endless loop, so keep it alive. */
2585 add_End_keepalive(get_irg_end(current_ir_graph), get_nodes_block(n));
2588 } /* transform_node_Cond */
2590 typedef ir_node* (*recursive_transform) (ir_node *n);
2593 * makes use of distributive laws for and, or, eor
2594 * and(a OP c, b OP c) -> and(a, b) OP c
2595 * note, might return a different op than n
2597 static ir_node *transform_bitwise_distributive(ir_node *n,
2598 recursive_transform trans_func)
2601 ir_node *a = get_binop_left(n);
2602 ir_node *b = get_binop_right(n);
2603 ir_op *op = get_irn_op(a);
2604 ir_op *op_root = get_irn_op(n);
2606 if(op != get_irn_op(b))
2609 if (op == op_Conv) {
2610 ir_node *a_op = get_Conv_op(a);
2611 ir_node *b_op = get_Conv_op(b);
2612 ir_mode *a_mode = get_irn_mode(a_op);
2613 ir_mode *b_mode = get_irn_mode(b_op);
2614 if(a_mode == b_mode && (mode_is_int(a_mode) || a_mode == mode_b)) {
2615 ir_node *blk = get_irn_n(n, -1);
2618 set_binop_left(n, a_op);
2619 set_binop_right(n, b_op);
2620 set_irn_mode(n, a_mode);
2622 n = new_r_Conv(current_ir_graph, blk, n, get_irn_mode(oldn));
2624 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_SHIFT_AND);
2630 /* nothing to gain here */
2634 if (op == op_Shrs || op == op_Shr || op == op_Shl
2635 || op == op_And || op == op_Or || op == op_Eor) {
2636 ir_node *a_left = get_binop_left(a);
2637 ir_node *a_right = get_binop_right(a);
2638 ir_node *b_left = get_binop_left(b);
2639 ir_node *b_right = get_binop_right(b);
2643 if (is_op_commutative(op)) {
2644 if (a_left == b_left) {
2648 } else if(a_left == b_right) {
2652 } else if(a_right == b_left) {
2658 if(a_right == b_right) {
2665 /* (a sop c) & (b sop c) => (a & b) sop c */
2666 ir_node *blk = get_irn_n(n, -1);
2668 ir_node *new_n = exact_copy(n);
2669 set_binop_left(new_n, op1);
2670 set_binop_right(new_n, op2);
2671 new_n = trans_func(new_n);
2673 if(op_root == op_Eor && op == op_Or) {
2674 dbg_info *dbgi = get_irn_dbg_info(n);
2675 ir_graph *irg = current_ir_graph;
2676 ir_mode *mode = get_irn_mode(c);
2678 c = new_rd_Not(dbgi, irg, blk, c, mode);
2679 n = new_rd_And(dbgi, irg, blk, new_n, c, mode);
2682 set_irn_n(n, -1, blk);
2683 set_binop_left(n, new_n);
2684 set_binop_right(n, c);
2688 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_SHIFT_AND);
2699 static ir_node *transform_node_And(ir_node *n) {
2701 ir_node *a = get_And_left(n);
2702 ir_node *b = get_And_right(n);
2704 HANDLE_BINOP_PHI(tarval_and, a,b,c);
2706 /* we can evaluate 2 Projs of the same Cmp */
2707 if(get_irn_mode(n) == mode_b && is_Proj(a) && is_Proj(b)) {
2708 ir_node *pred_a = get_Proj_pred(a);
2709 ir_node *pred_b = get_Proj_pred(b);
2710 if(pred_a == pred_b) {
2711 dbg_info *dbgi = get_irn_dbg_info(n);
2712 ir_node *block = get_nodes_block(pred_a);
2713 pn_Cmp pn_a = get_Proj_proj(a);
2714 pn_Cmp pn_b = get_Proj_proj(b);
2715 /* yes, we can simply calculate with pncs */
2716 pn_Cmp new_pnc = pn_a & pn_b;
2718 return new_rd_Proj(dbgi, current_ir_graph, block, pred_a, mode_b,
2723 n = transform_bitwise_distributive(n, transform_node_And);
2726 } /* transform_node_And */
2731 static ir_node *transform_node_Eor(ir_node *n) {
2732 ir_node *c, *oldn = n;
2733 ir_node *a = get_Eor_left(n);
2734 ir_node *b = get_Eor_right(n);
2735 ir_mode *mode = get_irn_mode(n);
2737 HANDLE_BINOP_PHI(tarval_eor, a,b,c);
2739 /* we can evaluate 2 Projs of the same Cmp */
2740 if(get_irn_mode(n) == mode_b && is_Proj(a) && is_Proj(b)) {
2741 ir_node *pred_a = get_Proj_pred(a);
2742 ir_node *pred_b = get_Proj_pred(b);
2743 if(pred_a == pred_b) {
2744 dbg_info *dbgi = get_irn_dbg_info(n);
2745 ir_node *block = get_nodes_block(pred_a);
2746 pn_Cmp pn_a = get_Proj_proj(a);
2747 pn_Cmp pn_b = get_Proj_proj(b);
2748 /* yes, we can simply calculate with pncs */
2749 pn_Cmp new_pnc = pn_a ^ pn_b;
2751 return new_rd_Proj(dbgi, current_ir_graph, block, pred_a, mode_b,
2758 n = new_rd_Const(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1),
2759 mode, get_mode_null(mode));
2760 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_A_A);
2761 } else if ((mode == mode_b)
2762 && (get_irn_op(a) == op_Proj)
2763 && (get_irn_mode(a) == mode_b)
2764 && (classify_tarval (value_of(b)) == TV_CLASSIFY_ONE)
2765 && (get_irn_op(get_Proj_pred(a)) == op_Cmp)) {
2766 /* The Eor negates a Cmp. The Cmp has the negated result anyways! */
2767 n = new_r_Proj(current_ir_graph, get_irn_n(n, -1), get_Proj_pred(a),
2768 mode_b, get_negated_pnc(get_Proj_proj(a), mode));
2770 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT_BOOL);
2771 } else if ((mode == mode_b)
2772 && (classify_tarval (value_of(b)) == TV_CLASSIFY_ONE)) {
2773 /* The Eor is a Not. Replace it by a Not. */
2774 /* ????!!!Extend to bitfield 1111111. */
2775 n = new_r_Not(current_ir_graph, get_irn_n(n, -1), a, mode_b);
2777 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
2779 n = transform_bitwise_distributive(n, transform_node_Eor);
2783 } /* transform_node_Eor */
2788 static ir_node *transform_node_Not(ir_node *n) {
2789 ir_node *c, *oldn = n;
2790 ir_node *a = get_Not_op(n);
2791 ir_op *op_a = get_irn_op(a);
2793 HANDLE_UNOP_PHI(tarval_not,a,c);
2795 /* check for a boolean Not */
2796 if ( (get_irn_mode(n) == mode_b)
2797 && (op_a == op_Proj)
2798 && (get_irn_mode(a) == mode_b)
2799 && (get_irn_op(get_Proj_pred(a)) == op_Cmp)) {
2800 /* We negate a Cmp. The Cmp has the negated result anyways! */
2801 n = new_r_Proj(current_ir_graph, get_irn_n(n, -1), get_Proj_pred(a),
2802 mode_b, get_negated_pnc(get_Proj_proj(a), mode_b));
2803 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_CMP);
2806 if (op_a == op_Sub && classify_Const(get_Sub_right(a)) == CNST_ONE) {
2808 ir_node *op = get_Sub_left(a);
2809 ir_node *blk = get_irn_n(n, -1);
2810 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, blk, op, get_irn_mode(n));
2811 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_MINUS_1);
2814 } /* transform_node_Not */
2817 * Transform a Minus.
2821 static ir_node *transform_node_Minus(ir_node *n) {
2822 ir_node *c, *oldn = n;
2823 ir_node *a = get_Minus_op(n);
2826 HANDLE_UNOP_PHI(tarval_neg,a,c);
2828 mode = get_irn_mode(a);
2829 if (get_mode_arithmetic(mode) == irma_twos_complement && is_Not(a)) {
2831 ir_node *op = get_Not_op(a);
2832 tarval *tv = get_mode_one(mode);
2833 ir_node *blk = get_irn_n(n, -1);
2834 ir_node *c = new_r_Const(current_ir_graph, blk, mode, tv);
2835 n = new_rd_Add(get_irn_dbg_info(n), current_ir_graph, blk, op, c, mode);
2836 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_MINUS_NOT);
2837 } else if (is_Sub(a)) {
2838 /* - (a-b) = b - a */
2839 ir_node *la = get_Sub_left(a);
2840 ir_node *ra = get_Sub_right(a);
2841 ir_node *blk = get_irn_n(n, -1);
2843 n = new_rd_Sub(get_irn_dbg_info(n), current_ir_graph, blk, ra, la, mode);
2844 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_MINUS_SUB);
2848 } /* transform_node_Minus */
2851 * Transform a Cast_type(Const) into a new Const_type
2853 static ir_node *transform_node_Cast(ir_node *n) {
2855 ir_node *pred = get_Cast_op(n);
2856 ir_type *tp = get_irn_type(n);
2858 if (get_irn_op(pred) == op_Const && get_Const_type(pred) != tp) {
2859 n = new_rd_Const_type(NULL, current_ir_graph, get_irn_n(pred, -1), get_irn_mode(pred),
2860 get_Const_tarval(pred), tp);
2861 DBG_OPT_CSTEVAL(oldn, n);
2862 } else if ((get_irn_op(pred) == op_SymConst) && (get_SymConst_value_type(pred) != tp)) {
2863 n = new_rd_SymConst_type(NULL, current_ir_graph, get_irn_n(pred, -1), get_SymConst_symbol(pred),
2864 get_SymConst_kind(pred), tp);
2865 DBG_OPT_CSTEVAL(oldn, n);
2869 } /* transform_node_Cast */
2872 * Transform a Proj(Div) with a non-zero value.
2873 * Removes the exceptions and routes the memory to the NoMem node.
2875 static ir_node *transform_node_Proj_Div(ir_node *proj) {
2876 ir_node *div = get_Proj_pred(proj);
2877 ir_node *b = get_Div_right(div);
2878 ir_node *confirm, *res, *new_mem;
2881 if (value_not_zero(b, &confirm)) {
2882 /* div(x, y) && y != 0 */
2883 proj_nr = get_Proj_proj(proj);
2885 case pn_Div_X_regular:
2886 return new_r_Jmp(current_ir_graph, get_irn_n(div, -1));
2888 case pn_Div_X_except:
2889 /* we found an exception handler, remove it */
2890 DBG_OPT_EXC_REM(proj);
2894 res = get_Div_mem(div);
2895 new_mem = get_irg_no_mem(current_ir_graph);
2898 /* This node can only float up to the Confirm block */
2899 new_mem = new_r_Pin(current_ir_graph, get_nodes_block(confirm), new_mem);
2901 set_irn_pinned(div, op_pin_state_floats);
2902 /* this is a Div without exception, we can remove the memory edge */
2903 set_Div_mem(div, new_mem);
2908 } /* transform_node_Proj_Div */
2911 * Transform a Proj(Mod) with a non-zero value.
2912 * Removes the exceptions and routes the memory to the NoMem node.
2914 static ir_node *transform_node_Proj_Mod(ir_node *proj) {
2915 ir_node *mod = get_Proj_pred(proj);
2916 ir_node *b = get_Mod_right(mod);
2917 ir_node *confirm, *res, *new_mem;
2920 if (value_not_zero(b, &confirm)) {
2921 /* mod(x, y) && y != 0 */
2922 proj_nr = get_Proj_proj(proj);
2926 case pn_Mod_X_regular:
2927 return new_r_Jmp(current_ir_graph, get_irn_n(mod, -1));
2929 case pn_Mod_X_except:
2930 /* we found an exception handler, remove it */
2931 DBG_OPT_EXC_REM(proj);
2935 res = get_Mod_mem(mod);
2936 new_mem = get_irg_no_mem(current_ir_graph);
2939 /* This node can only float up to the Confirm block */
2940 new_mem = new_r_Pin(current_ir_graph, get_nodes_block(confirm), new_mem);
2942 set_irn_pinned(mod, op_pin_state_floats);
2943 /* this is a Mod without exception, we can remove the memory edge */
2944 set_Mod_mem(mod, get_irg_no_mem(current_ir_graph));
2947 if (get_Mod_left(mod) == b) {
2948 /* a % a = 0 if a != 0 */
2949 ir_mode *mode = get_irn_mode(proj);
2950 ir_node *res = new_Const(mode, get_mode_null(mode));
2952 DBG_OPT_CSTEVAL(mod, res);
2958 } /* transform_node_Proj_Mod */
2961 * Transform a Proj(DivMod) with a non-zero value.
2962 * Removes the exceptions and routes the memory to the NoMem node.
2964 static ir_node *transform_node_Proj_DivMod(ir_node *proj) {
2965 ir_node *divmod = get_Proj_pred(proj);
2966 ir_node *b = get_DivMod_right(divmod);
2967 ir_node *confirm, *res, *new_mem;
2970 if (value_not_zero(b, &confirm)) {
2971 /* DivMod(x, y) && y != 0 */
2972 proj_nr = get_Proj_proj(proj);
2976 case pn_DivMod_X_regular:
2977 return new_r_Jmp(current_ir_graph, get_irn_n(divmod, -1));
2979 case pn_DivMod_X_except:
2980 /* we found an exception handler, remove it */
2981 DBG_OPT_EXC_REM(proj);
2985 res = get_DivMod_mem(divmod);
2986 new_mem = get_irg_no_mem(current_ir_graph);
2989 /* This node can only float up to the Confirm block */
2990 new_mem = new_r_Pin(current_ir_graph, get_nodes_block(confirm), new_mem);
2992 set_irn_pinned(divmod, op_pin_state_floats);
2993 /* this is a DivMod without exception, we can remove the memory edge */
2994 set_DivMod_mem(divmod, get_irg_no_mem(current_ir_graph));
2997 case pn_DivMod_res_mod:
2998 if (get_DivMod_left(divmod) == b) {
2999 /* a % a = 0 if a != 0 */
3000 ir_mode *mode = get_irn_mode(proj);
3001 ir_node *res = new_Const(mode, get_mode_null(mode));
3003 DBG_OPT_CSTEVAL(divmod, res);
3009 } /* transform_node_Proj_DivMod */
3012 * Optimizes jump tables (CondIs or CondIu) by removing all impossible cases.
3014 static ir_node *transform_node_Proj_Cond(ir_node *proj) {
3015 if (get_opt_unreachable_code()) {
3016 ir_node *n = get_Proj_pred(proj);
3017 ir_node *b = get_Cond_selector(n);
3019 if (mode_is_int(get_irn_mode(b))) {
3020 tarval *tb = value_of(b);
3022 if (tb != tarval_bad) {
3023 /* we have a constant switch */
3024 long num = get_Proj_proj(proj);
3026 if (num != get_Cond_defaultProj(n)) { /* we cannot optimize default Proj's yet */
3027 if (get_tarval_long(tb) == num) {
3028 /* Do NOT create a jump here, or we will have 2 control flow ops
3029 * in a block. This case is optimized away in optimize_cf(). */
3032 /* this case will NEVER be taken, kill it */
3040 } /* transform_node_Proj_Cond */
3043 * Normalizes and optimizes Cmp nodes.
3045 static ir_node *transform_node_Proj_Cmp(ir_node *proj) {
3046 ir_node *n = get_Proj_pred(proj);
3047 ir_node *left = get_Cmp_left(n);
3048 ir_node *right = get_Cmp_right(n);
3052 ir_mode *mode = NULL;
3053 long proj_nr = get_Proj_proj(proj);
3055 /* we can evaluate this direct */
3058 return new_Const(mode_b, get_tarval_b_false());
3060 return new_Const(mode_b, get_tarval_b_true());
3062 if(!mode_is_float(get_irn_mode(left)))
3063 return new_Const(mode_b, get_tarval_b_true());
3069 /* Remove unnecessary conversions */
3070 /* TODO handle constants */
3071 if (is_Conv(left) && is_Conv(right)) {
3072 ir_mode* mode = get_irn_mode(left);
3073 ir_node* op_left = get_Conv_op(left);
3074 ir_node* op_right = get_Conv_op(right);
3075 ir_mode* mode_left = get_irn_mode(op_left);
3076 ir_mode* mode_right = get_irn_mode(op_right);
3078 if (smaller_mode(mode_left, mode) && smaller_mode(mode_right, mode)) {
3079 ir_graph* irg = current_ir_graph;
3080 ir_node* block = get_nodes_block(n);
3084 if (mode_left == mode_right) {
3086 new_right = op_right;
3087 } else if (smaller_mode(mode_left, mode_right)) {
3088 new_left = new_r_Conv(irg, block, op_left, mode_right);
3089 new_right = op_right;
3090 } else if (smaller_mode(mode_right, mode_left)) {
3092 new_right = new_r_Conv(irg, block, op_right, mode_left);
3094 goto no_remove_conv;
3098 set_Cmp_left( n, left);
3099 set_Cmp_right(n, right);
3104 if (get_irn_mode(left) == mode_b) {
3105 ir_graph* irg = current_ir_graph;
3106 ir_node* block = get_nodes_block(n);
3109 case pn_Cmp_Le: return new_r_Or( irg, block, new_r_Not(irg, block, left, mode_b), right, mode_b);
3110 case pn_Cmp_Lt: return new_r_And(irg, block, new_r_Not(irg, block, left, mode_b), right, mode_b);
3111 case pn_Cmp_Ge: return new_r_Or( irg, block, left, new_r_Not(irg, block, right, mode_b), mode_b);
3112 case pn_Cmp_Gt: return new_r_And(irg, block, left, new_r_Not(irg, block, right, mode_b), mode_b);
3113 case pn_Cmp_Lg: return new_r_Eor(irg, block, left, right, mode_b);
3114 case pn_Cmp_Eq: return new_r_Not(irg, block, new_r_Eor(irg, block, left, right, mode_b), mode_b);
3118 if (!get_opt_reassociation())
3122 * First step: normalize the compare op
3123 * by placing the constant on the right site
3124 * or moving the lower address node to the left.
3125 * We ignore the case that both are constants
3126 * this case should be optimized away.
3128 if (get_irn_op(right) == op_Const) {
3130 } else if (get_irn_op(left) == op_Const) {
3135 proj_nr = get_inversed_pnc(proj_nr);
3137 } else if (get_irn_idx(left) > get_irn_idx(right)) {
3143 proj_nr = get_inversed_pnc(proj_nr);
3148 * Second step: Try to reduce the magnitude
3149 * of a constant. This may help to generate better code
3150 * later and may help to normalize more compares.
3151 * Of course this is only possible for integer values.
3154 mode = get_irn_mode(c);
3155 tv = get_Const_tarval(c);
3157 if (tv != tarval_bad) {
3158 /* the following optimization is possible on modes without Overflow
3159 * on Unary Minus or on == and !=:
3160 * -a CMP c ==> a swap(CMP) -c
3162 * Beware: for two-complement Overflow may occur, so only == and != can
3163 * be optimized, see this:
3164 * -MININT < 0 =/=> MININT > 0 !!!
3166 if (get_opt_constant_folding() && get_irn_op(left) == op_Minus &&
3167 (!mode_overflow_on_unary_Minus(mode) ||
3168 (mode_is_int(mode) && (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg)))) {
3169 left = get_Minus_op(left);
3170 tv = tarval_neg(tv);
3172 if (tv != tarval_bad) {
3173 proj_nr = get_inversed_pnc(proj_nr);
3178 /* for integer modes, we have more */
3179 if (mode_is_int(mode)) {
3180 /* Ne includes Unordered which is not possible on integers.
3181 * However, frontends often use this wrong, so fix it here */
3182 if (proj_nr & pn_Cmp_Uo) {
3183 proj_nr &= ~pn_Cmp_Uo;
3184 set_Proj_proj(proj, proj_nr);
3187 /* c > 0 : a < c ==> a <= (c-1) a >= c ==> a > (c-1) */
3188 if ((proj_nr == pn_Cmp_Lt || proj_nr == pn_Cmp_Ge) &&
3189 tarval_cmp(tv, get_mode_null(mode)) == pn_Cmp_Gt) {
3190 tv = tarval_sub(tv, get_mode_one(mode));
3192 if (tv != tarval_bad) {
3193 proj_nr ^= pn_Cmp_Eq;
3197 /* c < 0 : a > c ==> a >= (c+1) a <= c ==> a < (c+1) */
3198 else if ((proj_nr == pn_Cmp_Gt || proj_nr == pn_Cmp_Le) &&
3199 tarval_cmp(tv, get_mode_null(mode)) == pn_Cmp_Lt) {
3200 tv = tarval_add(tv, get_mode_one(mode));
3202 if (tv != tarval_bad) {
3203 proj_nr ^= pn_Cmp_Eq;
3208 /* the following reassociations work only for == and != */
3209 if (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) {
3211 /* a-b == 0 ==> a == b, a-b != 0 ==> a != b */
3212 if (classify_tarval(tv) == TV_CLASSIFY_NULL && get_irn_op(left) == op_Sub) {
3213 right = get_Sub_right(left);
3214 left = get_Sub_left(left);
3216 tv = value_of(right);
3217 if (tv != tarval_bad) {
3222 if (tv != tarval_bad) {
3223 ir_op *op = get_irn_op(left);
3225 /* a-c1 == c2 ==> a == c2+c1, a-c1 != c2 ==> a != c2+c1 */
3227 ir_node *c1 = get_Sub_right(left);
3228 tarval *tv2 = value_of(c1);
3230 if (tv2 != tarval_bad) {
3231 tv2 = tarval_add(tv, value_of(c1));
3233 if (tv2 != tarval_bad) {
3234 left = get_Sub_left(left);
3240 /* a+c1 == c2 ==> a == c2-c1, a+c1 != c2 ==> a != c2-c1 */
3241 else if (op == op_Add) {
3242 ir_node *a_l = get_Add_left(left);
3243 ir_node *a_r = get_Add_right(left);
3247 if (get_irn_op(a_l) == op_Const) {
3249 tv2 = value_of(a_l);
3252 tv2 = value_of(a_r);
3255 if (tv2 != tarval_bad) {
3256 tv2 = tarval_sub(tv, tv2);
3258 if (tv2 != tarval_bad) {
3265 /* -a == c ==> a == -c, -a != c ==> a != -c */
3266 else if (op == op_Minus) {
3267 tarval *tv2 = tarval_sub(get_mode_null(mode), tv);
3269 if (tv2 != tarval_bad) {
3270 left = get_Minus_op(left);
3277 /* the following reassociations work only for <= */
3278 else if (proj_nr == pn_Cmp_Le || proj_nr == pn_Cmp_Lt) {
3279 if (tv != tarval_bad) {
3280 ir_op *op = get_irn_op(left);
3282 /* c >= 0 : Abs(a) <= c ==> (unsigned)(a + c) <= 2*c */
3290 * optimization for AND:
3292 * And(x, C) == C ==> And(x, C) != 0
3293 * And(x, C) != C ==> And(X, C) == 0
3295 * if C is a single Bit constant.
3297 if ((proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) &&
3298 (get_irn_op(left) == op_And)) {
3299 if (tarval_is_single_bit(tv)) {
3300 /* check for Constant's match. We have check hare the tarvals,
3301 because our const might be changed */
3302 ir_node *la = get_And_left(left);
3303 ir_node *ra = get_And_right(left);
3304 if ((is_Const(la) && get_Const_tarval(la) == tv) ||
3305 (is_Const(ra) && get_Const_tarval(ra) == tv)) {
3306 /* fine: do the transformation */
3307 tv = get_mode_null(get_tarval_mode(tv));
3308 proj_nr ^= pn_Cmp_Leg;
3313 } /* tarval != bad */
3317 ir_node *block = get_irn_n(n, -1); /* Beware of get_nodes_Block() */
3319 if (changed & 2) /* need a new Const */
3320 right = new_Const(mode, tv);
3322 /* create a new compare */
3323 n = new_rd_Cmp(get_irn_dbg_info(n), current_ir_graph, block,
3326 set_Proj_pred(proj, n);
3327 set_Proj_proj(proj, proj_nr);
3331 } /* transform_node_Proj_Cmp */
3334 * Does all optimizations on nodes that must be done on it's Proj's
3335 * because of creating new nodes.
3337 static ir_node *transform_node_Proj(ir_node *proj) {
3338 ir_node *n = get_Proj_pred(proj);
3340 switch (get_irn_opcode(n)) {
3342 return transform_node_Proj_Div(proj);
3345 return transform_node_Proj_Mod(proj);
3348 return transform_node_Proj_DivMod(proj);
3351 return transform_node_Proj_Cond(proj);
3354 return transform_node_Proj_Cmp(proj);
3357 /* should not happen, but if it does will be optimized away */
3358 return equivalent_node_Proj(proj);
3364 } /* transform_node_Proj */
3367 * Move Confirms down through Phi nodes.
3369 static ir_node *transform_node_Phi(ir_node *phi) {
3371 ir_mode *mode = get_irn_mode(phi);
3373 if (mode_is_reference(mode)) {
3374 n = get_irn_arity(phi);
3376 /* Beware of Phi0 */
3378 ir_node *pred = get_irn_n(phi, 0);
3379 ir_node *bound, *new_Phi, *block, **in;
3382 if (! is_Confirm(pred))
3385 bound = get_Confirm_bound(pred);
3386 pnc = get_Confirm_cmp(pred);
3388 NEW_ARR_A(ir_node *, in, n);
3389 in[0] = get_Confirm_value(pred);
3391 for (i = 1; i < n; ++i) {
3392 pred = get_irn_n(phi, i);
3394 if (! is_Confirm(pred) ||
3395 get_Confirm_bound(pred) != bound ||
3396 get_Confirm_cmp(pred) != pnc)
3398 in[i] = get_Confirm_value(pred);
3400 /* move the Confirm nodes "behind" the Phi */
3401 block = get_irn_n(phi, -1);
3402 new_Phi = new_r_Phi(current_ir_graph, block, n, in, get_irn_mode(phi));
3403 return new_r_Confirm(current_ir_graph, block, new_Phi, bound, pnc);
3407 } /* transform_node_Phi */
3410 * Returns the operands of a commutative bin-op, if one operand is
3411 * a const, it is returned as the second one.
3413 static void get_comm_Binop_Ops(ir_node *binop, ir_node **a, ir_node **c) {
3414 ir_node *op_a = get_binop_left(binop);
3415 ir_node *op_b = get_binop_right(binop);
3417 assert(is_op_commutative(get_irn_op(binop)));
3419 if (get_irn_op(op_a) == op_Const) {
3426 } /* get_comm_Binop_Ops */
3429 * Optimize a Or(And(Or(And(v,c4),c3),c2),c1) pattern if possible.
3430 * Such pattern may arise in bitfield stores.
3432 * value c4 value c4 & c2
3433 * AND c3 AND c1 | c3
3440 * AND c1 ===> OR if (c1 | c2) == 0x111..11
3443 static ir_node *transform_node_Or_bf_store(ir_node *or) {
3446 ir_node *and_l, *c3;
3447 ir_node *value, *c4;
3448 ir_node *new_and, *new_const, *block;
3449 ir_mode *mode = get_irn_mode(or);
3451 tarval *tv1, *tv2, *tv3, *tv4, *tv, *n_tv4, *n_tv2;
3454 get_comm_Binop_Ops(or, &and, &c1);
3455 if (!is_Const(c1) || !is_And(and))
3458 get_comm_Binop_Ops(and, &or_l, &c2);
3462 tv1 = get_Const_tarval(c1);
3463 tv2 = get_Const_tarval(c2);
3465 tv = tarval_or(tv1, tv2);
3466 if (classify_tarval(tv) == TV_CLASSIFY_ALL_ONE) {
3467 /* the AND does NOT clear a bit with isn't set be the OR */
3468 set_Or_left(or, or_l);
3469 set_Or_right(or, c1);
3471 /* check for more */
3478 get_comm_Binop_Ops(or_l, &and_l, &c3);
3479 if (!is_Const(c3) || !is_And(and_l))
3482 get_comm_Binop_Ops(and_l, &value, &c4);
3486 /* ok, found the pattern, check for conditions */
3487 assert(mode == get_irn_mode(and));
3488 assert(mode == get_irn_mode(or_l));
3489 assert(mode == get_irn_mode(and_l));
3491 tv3 = get_Const_tarval(c3);
3492 tv4 = get_Const_tarval(c4);
3494 tv = tarval_or(tv4, tv2);
3495 if (classify_tarval(tv) != TV_CLASSIFY_ALL_ONE) {
3496 /* have at least one 0 at the same bit position */
3500 n_tv4 = tarval_not(tv4);
3501 if (tv3 != tarval_and(tv3, n_tv4)) {
3502 /* bit in the or_mask is outside the and_mask */
3506 n_tv2 = tarval_not(tv2);
3507 if (tv1 != tarval_and(tv1, n_tv2)) {
3508 /* bit in the or_mask is outside the and_mask */
3512 /* ok, all conditions met */
3513 block = get_irn_n(or, -1);
3515 new_and = new_r_And(current_ir_graph, block,
3516 value, new_r_Const(current_ir_graph, block, mode, tarval_and(tv4, tv2)), mode);
3518 new_const = new_r_Const(current_ir_graph, block, mode, tarval_or(tv3, tv1));
3520 set_Or_left(or, new_and);
3521 set_Or_right(or, new_const);
3523 /* check for more */
3525 } /* transform_node_Or_bf_store */
3528 * Optimize an Or(shl(x, c), shr(x, bits - c)) into a Rot
3530 static ir_node *transform_node_Or_Rot(ir_node *or) {
3531 ir_mode *mode = get_irn_mode(or);
3532 ir_node *shl, *shr, *block;
3533 ir_node *irn, *x, *c1, *c2, *v, *sub, *n;
3536 if (! mode_is_int(mode))
3539 shl = get_binop_left(or);
3540 shr = get_binop_right(or);
3542 if (get_irn_op(shl) == op_Shr) {
3543 if (get_irn_op(shr) != op_Shl)
3549 } else if (get_irn_op(shl) != op_Shl) {
3551 } else if (get_irn_op(shr) != op_Shr) {
3554 x = get_Shl_left(shl);
3555 if (x != get_Shr_left(shr))
3558 c1 = get_Shl_right(shl);
3559 c2 = get_Shr_right(shr);
3560 if (get_irn_op(c1) == op_Const && get_irn_op(c2) == op_Const) {
3561 tv1 = get_Const_tarval(c1);
3562 if (! tarval_is_long(tv1))
3565 tv2 = get_Const_tarval(c2);
3566 if (! tarval_is_long(tv2))
3569 if (get_tarval_long(tv1) + get_tarval_long(tv2)
3570 != get_mode_size_bits(mode))
3573 /* yet, condition met */
3574 block = get_irn_n(or, -1);
3576 n = new_r_Rot(current_ir_graph, block, x, c1, mode);
3578 DBG_OPT_ALGSIM1(or, shl, shr, n, FS_OPT_OR_SHFT_TO_ROT);
3580 } else if (get_irn_op(c1) == op_Sub) {
3584 if (get_Sub_right(sub) != v)
3587 c1 = get_Sub_left(sub);
3588 if (get_irn_op(c1) != op_Const)
3591 tv1 = get_Const_tarval(c1);
3592 if (! tarval_is_long(tv1))
3595 if (get_tarval_long(tv1) != get_mode_size_bits(mode))
3598 /* yet, condition met */
3599 block = get_nodes_block(or);
3601 /* a Rot right is not supported, so use a rot left */
3602 n = new_r_Rot(current_ir_graph, block, x, sub, mode);
3604 DBG_OPT_ALGSIM0(or, n, FS_OPT_OR_SHFT_TO_ROT);
3606 } else if (get_irn_op(c2) == op_Sub) {
3610 c1 = get_Sub_left(sub);
3611 if (get_irn_op(c1) != op_Const)
3614 tv1 = get_Const_tarval(c1);
3615 if (! tarval_is_long(tv1))
3618 if (get_tarval_long(tv1) != get_mode_size_bits(mode))
3621 /* yet, condition met */
3622 block = get_irn_n(or, -1);
3625 n = new_r_Rot(current_ir_graph, block, x, v, mode);
3627 DBG_OPT_ALGSIM0(or, n, FS_OPT_OR_SHFT_TO_ROT);
3632 } /* transform_node_Or_Rot */
3637 static ir_node *transform_node_Or(ir_node *n) {
3638 ir_node *c, *oldn = n;
3639 ir_node *a = get_Or_left(n);
3640 ir_node *b = get_Or_right(n);
3642 /* we can evaluate 2 Projs of the same Cmp */
3643 if(get_irn_mode(n) == mode_b && is_Proj(a) && is_Proj(b)) {
3644 ir_node *pred_a = get_Proj_pred(a);
3645 ir_node *pred_b = get_Proj_pred(b);
3646 if(pred_a == pred_b) {
3647 dbg_info *dbgi = get_irn_dbg_info(n);
3648 ir_node *block = get_nodes_block(pred_a);
3649 pn_Cmp pn_a = get_Proj_proj(a);
3650 pn_Cmp pn_b = get_Proj_proj(b);
3651 /* yes, we can simply calculate with pncs */
3652 pn_Cmp new_pnc = pn_a | pn_b;
3654 return new_rd_Proj(dbgi, current_ir_graph, block, pred_a, mode_b,
3659 HANDLE_BINOP_PHI(tarval_or, a,b,c);
3661 n = transform_node_Or_bf_store(n);
3662 n = transform_node_Or_Rot(n);
3666 n = transform_bitwise_distributive(n, transform_node_Or);
3669 } /* transform_node_Or */
3673 static ir_node *transform_node(ir_node *n);
3676 * Optimize (a >> c1) >> c2), works for Shr, Shrs, Shl.
3678 * Should be moved to reassociation?
3680 static ir_node *transform_node_shift(ir_node *n) {
3681 ir_node *left, *right;
3682 tarval *tv1, *tv2, *res;
3684 int modulo_shf, flag;
3686 left = get_binop_left(n);
3688 /* different operations */
3689 if (get_irn_op(left) != get_irn_op(n))
3692 right = get_binop_right(n);
3693 tv1 = value_of(right);
3694 if (tv1 == tarval_bad)
3697 tv2 = value_of(get_binop_right(left));
3698 if (tv2 == tarval_bad)
3701 res = tarval_add(tv1, tv2);
3703 /* beware: a simple replacement works only, if res < modulo shift */
3704 mode = get_irn_mode(n);
3708 modulo_shf = get_mode_modulo_shift(mode);
3709 if (modulo_shf > 0) {
3710 tarval *modulo = new_tarval_from_long(modulo_shf, get_tarval_mode(res));
3712 if (tarval_cmp(res, modulo) & pn_Cmp_Lt)
3718 /* ok, we can replace it */
3719 ir_node *in[2], *irn, *block = get_irn_n(n, -1);
3721 in[0] = get_binop_left(left);
3722 in[1] = new_r_Const(current_ir_graph, block, get_tarval_mode(res), res);
3724 irn = new_ir_node(NULL, current_ir_graph, block, get_irn_op(n), mode, 2, in);
3726 DBG_OPT_ALGSIM0(n, irn, FS_OPT_REASSOC_SHIFT);
3728 return transform_node(irn);
3731 } /* transform_node_shift */
3736 static ir_node *transform_node_Shr(ir_node *n) {
3737 ir_node *c, *oldn = n;
3738 ir_node *a = get_Shr_left(n);
3739 ir_node *b = get_Shr_right(n);
3741 HANDLE_BINOP_PHI(tarval_shr, a, b, c);
3742 return transform_node_shift(n);
3743 } /* transform_node_Shr */
3748 static ir_node *transform_node_Shrs(ir_node *n) {
3749 ir_node *c, *oldn = n;
3750 ir_node *a = get_Shrs_left(n);
3751 ir_node *b = get_Shrs_right(n);
3753 HANDLE_BINOP_PHI(tarval_shrs, a, b, c);
3754 return transform_node_shift(n);
3755 } /* transform_node_Shrs */
3760 static ir_node *transform_node_Shl(ir_node *n) {
3761 ir_node *c, *oldn = n;
3762 ir_node *a = get_Shl_left(n);
3763 ir_node *b = get_Shl_right(n);
3765 HANDLE_BINOP_PHI(tarval_shl, a, b, c);
3766 return transform_node_shift(n);
3767 } /* transform_node_Shl */
3770 * Remove dead blocks and nodes in dead blocks
3771 * in keep alive list. We do not generate a new End node.
3773 static ir_node *transform_node_End(ir_node *n) {
3774 int i, j, n_keepalives = get_End_n_keepalives(n);
3777 NEW_ARR_A(ir_node *, in, n_keepalives);
3779 for (i = j = 0; i < n_keepalives; ++i) {
3780 ir_node *ka = get_End_keepalive(n, i);
3782 if (! is_Block_dead(ka)) {
3786 } else if (is_irn_pinned_in_irg(ka) && is_Block_dead(get_nodes_block(ka))) {
3789 /* FIXME: beabi need to keep a Proj(M) */
3790 if (is_Phi(ka) || is_irn_keep(ka) || is_Proj(ka))
3793 if (j != n_keepalives)
3794 set_End_keepalives(n, j, in);
3796 } /* transform_node_End */
3798 /** returns 1 if a == -b */
3799 static int is_negated_value(ir_node *a, ir_node *b) {
3800 if(is_Minus(a) && get_Minus_op(a) == b)
3802 if(is_Minus(b) && get_Minus_op(b) == a)
3804 if(is_Sub(a) && is_Sub(b)) {
3805 ir_node *a_left = get_Sub_left(a);
3806 ir_node *a_right = get_Sub_right(a);
3807 ir_node *b_left = get_Sub_left(b);
3808 ir_node *b_right = get_Sub_right(b);
3810 if(a_left == b_right && a_right == b_left)
3818 * Optimize a Mux into some simpler cases.
3820 static ir_node *transform_node_Mux(ir_node *n) {
3821 ir_node *oldn = n, *sel = get_Mux_sel(n);
3822 ir_mode *mode = get_irn_mode(n);
3824 if (mode == mode_b) {
3825 ir_node *t = get_Mux_true(n);
3826 ir_node *f = get_Mux_false(n);
3827 dbg_info *dbg = get_irn_dbg_info(n);
3828 ir_node *block = get_irn_n(n, -1);
3829 ir_graph *irg = current_ir_graph;
3832 tarval *tv_t = get_Const_tarval(t);
3833 if (tv_t == tarval_b_true) {
3835 assert(get_Const_tarval(f) == tarval_b_false);
3838 return new_rd_Or(dbg, irg, block, sel, f, mode_b);
3841 ir_node* not_sel = new_rd_Not(dbg, irg, block, sel, mode_b);
3842 assert(tv_t == tarval_b_false);
3844 assert(get_Const_tarval(f) == tarval_b_true);
3847 return new_rd_And(dbg, irg, block, not_sel, f, mode_b);
3850 } else if (is_Const(f)) {
3851 tarval *tv_f = get_Const_tarval(f);
3852 if (tv_f == tarval_b_true) {
3853 ir_node* not_sel = new_rd_Not(dbg, irg, block, sel, mode_b);
3854 return new_rd_Or(dbg, irg, block, not_sel, t, mode_b);
3856 assert(tv_f == tarval_b_false);
3857 return new_rd_And(dbg, irg, block, sel, t, mode_b);
3862 if (get_irn_op(sel) == op_Proj && !mode_honor_signed_zeros(mode)) {
3863 ir_node *cmp = get_Proj_pred(sel);
3864 long pn = get_Proj_proj(sel);
3865 ir_node *f = get_Mux_false(n);
3866 ir_node *t = get_Mux_true(n);
3869 * Note: normalization puts the constant on the right site,
3870 * so we check only one case.
3872 * Note further that these optimization work even for floating point
3873 * with NaN's because -NaN == NaN.
3874 * However, if +0 and -0 is handled differently, we cannot use the first
3877 if (get_irn_op(cmp) == op_Cmp
3878 && classify_Const(get_Cmp_right(cmp)) == CNST_NULL) {
3879 ir_node *block = get_irn_n(n, -1);
3881 if(is_negated_value(f, t)) {
3882 ir_node *cmp_left = get_Cmp_left(cmp);
3884 /* Psi(a >= 0, a, -a) = Psi(a <= 0, -a, a) ==> Abs(a) */
3885 if ( (cmp_left == t && (pn == pn_Cmp_Ge || pn == pn_Cmp_Gt))
3886 || (cmp_left == f && (pn == pn_Cmp_Le || pn == pn_Cmp_Lt)))
3888 n = new_rd_Abs(get_irn_dbg_info(n), current_ir_graph, block,
3890 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_ABS);
3892 /* Psi(a <= 0, a, -a) = Psi(a >= 0, -a, a) ==> -Abs(a) */
3893 } else if ((cmp_left == t && (pn == pn_Cmp_Le || pn == pn_Cmp_Lt))
3894 || (cmp_left == f && (pn == pn_Cmp_Ge || pn == pn_Cmp_Gt)))
3896 n = new_rd_Abs(get_irn_dbg_info(n), current_ir_graph, block,
3898 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph,
3900 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_ABS);
3905 if (mode_is_int(mode) && mode_is_signed(mode) &&
3906 get_mode_arithmetic(mode) == irma_twos_complement) {
3907 ir_node *x = get_Cmp_left(cmp);
3909 /* the following optimization works only with signed integer two-complement mode */
3911 if (mode == get_irn_mode(x)) {
3913 * FIXME: this restriction is two rigid, as it would still
3914 * work if mode(x) = Hs and mode == Is, but at least it removes
3917 if ((pn == pn_Cmp_Lt || pn == pn_Cmp_Le) &&
3918 classify_Const(t) == CNST_ALL_ONE &&
3919 classify_Const(f) == CNST_NULL) {
3921 * Mux(x:T </<= 0, 0, -1) -> Shrs(x, sizeof_bits(T) - 1)
3925 n = new_rd_Shrs(get_irn_dbg_info(n),
3926 current_ir_graph, block, x,
3927 new_r_Const_long(current_ir_graph, block, mode_Iu,
3928 get_mode_size_bits(mode) - 1),
3930 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_SHR);
3932 } else if ((pn == pn_Cmp_Gt || pn == pn_Cmp_Ge) &&
3933 classify_Const(t) == CNST_ONE &&
3934 classify_Const(f) == CNST_NULL) {
3936 * Mux(x:T >/>= 0, 0, 1) -> Shr(-x, sizeof_bits(T) - 1)
3940 n = new_rd_Shr(get_irn_dbg_info(n),
3941 current_ir_graph, block,
3942 new_r_Minus(current_ir_graph, block, x, mode),
3943 new_r_Const_long(current_ir_graph, block, mode_Iu,
3944 get_mode_size_bits(mode) - 1),
3946 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_SHR);
3953 return arch_transform_node_Mux(n);
3954 } /* transform_node_Mux */
3957 * Optimize a Psi into some simpler cases.
3959 static ir_node *transform_node_Psi(ir_node *n) {
3961 return transform_node_Mux(n);
3964 } /* transform_node_Psi */
3967 * Tries several [inplace] [optimizing] transformations and returns an
3968 * equivalent node. The difference to equivalent_node() is that these
3969 * transformations _do_ generate new nodes, and thus the old node must
3970 * not be freed even if the equivalent node isn't the old one.
3972 static ir_node *transform_node(ir_node *n) {
3976 * Transform_node is the only "optimizing transformation" that might
3977 * return a node with a different opcode. We iterate HERE until fixpoint
3978 * to get the final result.
3982 if (n->op->ops.transform_node)
3983 n = n->op->ops.transform_node(n);
3984 } while (oldn != n);
3987 } /* transform_node */
3990 * Sets the default transform node operation for an ir_op_ops.
3992 * @param code the opcode for the default operation
3993 * @param ops the operations initialized
3998 static ir_op_ops *firm_set_default_transform_node(ir_opcode code, ir_op_ops *ops)
4002 ops->transform_node = transform_node_##a; \
4036 } /* firm_set_default_transform_node */
4039 /* **************** Common Subexpression Elimination **************** */
4041 /** The size of the hash table used, should estimate the number of nodes
4043 #define N_IR_NODES 512
4045 /** Compares the attributes of two Const nodes. */
4046 static int node_cmp_attr_Const(ir_node *a, ir_node *b) {
4047 return (get_Const_tarval(a) != get_Const_tarval(b))
4048 || (get_Const_type(a) != get_Const_type(b));
4049 } /* node_cmp_attr_Const */
4051 /** Compares the attributes of two Proj nodes. */
4052 static int node_cmp_attr_Proj(ir_node *a, ir_node *b) {
4053 return get_irn_proj_attr(a) != get_irn_proj_attr(b);
4054 } /* node_cmp_attr_Proj */
4056 /** Compares the attributes of two Filter nodes. */
4057 static int node_cmp_attr_Filter(ir_node *a, ir_node *b) {
4058 return get_Filter_proj(a) != get_Filter_proj(b);
4059 } /* node_cmp_attr_Filter */
4061 /** Compares the attributes of two Alloc nodes. */
4062 static int node_cmp_attr_Alloc(ir_node *a, ir_node *b) {
4063 const alloc_attr *pa = get_irn_alloc_attr(a);
4064 const alloc_attr *pb = get_irn_alloc_attr(b);
4065 return (pa->where != pb->where) || (pa->type != pb->type);
4066 } /* node_cmp_attr_Alloc */
4068 /** Compares the attributes of two Free nodes. */
4069 static int node_cmp_attr_Free(ir_node *a, ir_node *b) {
4070 const free_attr *pa = get_irn_free_attr(a);
4071 const free_attr *pb = get_irn_free_attr(b);
4072 return (pa->where != pb->where) || (pa->type != pb->type);
4073 } /* node_cmp_attr_Free */
4075 /** Compares the attributes of two SymConst nodes. */
4076 static int node_cmp_attr_SymConst(ir_node *a, ir_node *b) {
4077 const symconst_attr *pa = get_irn_symconst_attr(a);
4078 const symconst_attr *pb = get_irn_symconst_attr(b);
4079 return (pa->num != pb->num)
4080 || (pa->sym.type_p != pb->sym.type_p)
4081 || (pa->tp != pb->tp);
4082 } /* node_cmp_attr_SymConst */
4084 /** Compares the attributes of two Call nodes. */
4085 static int node_cmp_attr_Call(ir_node *a, ir_node *b) {
4086 return (get_irn_call_attr(a) != get_irn_call_attr(b));
4087 } /* node_cmp_attr_Call */
4089 /** Compares the attributes of two Sel nodes. */
4090 static int node_cmp_attr_Sel(ir_node *a, ir_node *b) {
4091 const ir_entity *a_ent = get_Sel_entity(a);
4092 const ir_entity *b_ent = get_Sel_entity(b);
4094 (a_ent->kind != b_ent->kind) ||
4095 (a_ent->name != b_ent->name) ||
4096 (a_ent->owner != b_ent->owner) ||
4097 (a_ent->ld_name != b_ent->ld_name) ||
4098 (a_ent->type != b_ent->type);
4099 } /* node_cmp_attr_Sel */
4101 /** Compares the attributes of two Phi nodes. */
4102 static int node_cmp_attr_Phi(ir_node *a, ir_node *b) {
4103 /* we can only enter this function if both nodes have the same number of inputs,
4104 hence it is enough to check if one of them is a Phi0 */
4106 /* check the Phi0 attribute */
4107 return get_irn_phi0_attr(a) != get_irn_phi0_attr(b);
4110 } /* node_cmp_attr_Phi */
4112 /** Compares the attributes of two Conv nodes. */
4113 static int node_cmp_attr_Conv(ir_node *a, ir_node *b) {
4114 return get_Conv_strict(a) != get_Conv_strict(b);
4115 } /* node_cmp_attr_Conv */
4117 /** Compares the attributes of two Cast nodes. */
4118 static int node_cmp_attr_Cast(ir_node *a, ir_node *b) {
4119 return get_Cast_type(a) != get_Cast_type(b);
4120 } /* node_cmp_attr_Cast */
4122 /** Compares the attributes of two Load nodes. */
4123 static int node_cmp_attr_Load(ir_node *a, ir_node *b) {
4124 if (get_Load_volatility(a) == volatility_is_volatile ||
4125 get_Load_volatility(b) == volatility_is_volatile)
4126 /* NEVER do CSE on volatile Loads */
4128 /* do not CSE Loads with different alignment. Be conservative. */
4129 if (get_Load_align(a) != get_Load_align(b))
4132 return get_Load_mode(a) != get_Load_mode(b);
4133 } /* node_cmp_attr_Load */
4135 /** Compares the attributes of two Store nodes. */
4136 static int node_cmp_attr_Store(ir_node *a, ir_node *b) {
4137 /* do not CSE Stores with different alignment. Be conservative. */
4138 if (get_Store_align(a) != get_Store_align(b))
4141 /* NEVER do CSE on volatile Stores */
4142 return (get_Store_volatility(a) == volatility_is_volatile ||
4143 get_Store_volatility(b) == volatility_is_volatile);
4144 } /* node_cmp_attr_Store */
4146 /** Compares the attributes of two Confirm nodes. */
4147 static int node_cmp_attr_Confirm(ir_node *a, ir_node *b) {
4148 return (get_Confirm_cmp(a) != get_Confirm_cmp(b));
4149 } /* node_cmp_attr_Confirm */
4151 /** Compares the attributes of two ASM nodes. */
4152 static int node_cmp_attr_ASM(ir_node *a, ir_node *b) {
4154 const ir_asm_constraint *ca;
4155 const ir_asm_constraint *cb;
4158 if (get_ASM_text(a) != get_ASM_text(b))
4161 /* Should we really check the constraints here? Should be better, but is strange. */
4162 n = get_ASM_n_input_constraints(a);
4163 if (n != get_ASM_n_input_constraints(b))
4166 ca = get_ASM_input_constraints(a);
4167 cb = get_ASM_input_constraints(b);
4168 for (i = 0; i < n; ++i) {
4169 if (ca[i].pos != cb[i].pos || ca[i].constraint != cb[i].constraint)
4173 n = get_ASM_n_output_constraints(a);
4174 if (n != get_ASM_n_output_constraints(b))
4177 ca = get_ASM_output_constraints(a);
4178 cb = get_ASM_output_constraints(b);
4179 for (i = 0; i < n; ++i) {
4180 if (ca[i].pos != cb[i].pos || ca[i].constraint != cb[i].constraint)
4184 n = get_ASM_n_clobbers(a);
4185 if (n != get_ASM_n_clobbers(b))
4188 cla = get_ASM_clobbers(a);
4189 clb = get_ASM_clobbers(b);
4190 for (i = 0; i < n; ++i) {
4191 if (cla[i] != clb[i])
4195 } /* node_cmp_attr_ASM */
4198 * Set the default node attribute compare operation for an ir_op_ops.
4200 * @param code the opcode for the default operation
4201 * @param ops the operations initialized
4206 static ir_op_ops *firm_set_default_node_cmp_attr(ir_opcode code, ir_op_ops *ops)
4210 ops->node_cmp_attr = node_cmp_attr_##a; \
4235 } /* firm_set_default_node_cmp_attr */
4238 * Compare function for two nodes in the hash table. Gets two
4239 * nodes as parameters. Returns 0 if the nodes are a cse.
4241 int identities_cmp(const void *elt, const void *key) {
4248 if (a == b) return 0;
4250 if ((get_irn_op(a) != get_irn_op(b)) ||
4251 (get_irn_mode(a) != get_irn_mode(b))) return 1;
4253 /* compare if a's in and b's in are of equal length */
4254 irn_arity_a = get_irn_intra_arity (a);
4255 if (irn_arity_a != get_irn_intra_arity(b))
4258 /* for block-local cse and op_pin_state_pinned nodes: */
4259 if (!get_opt_global_cse() || (get_irn_pinned(a) == op_pin_state_pinned)) {
4260 if (get_irn_intra_n(a, -1) != get_irn_intra_n(b, -1))
4264 /* compare a->in[0..ins] with b->in[0..ins] */
4265 for (i = 0; i < irn_arity_a; i++)
4266 if (get_irn_intra_n(a, i) != get_irn_intra_n(b, i))
4270 * here, we already now that the nodes are identical except their
4273 if (a->op->ops.node_cmp_attr)
4274 return a->op->ops.node_cmp_attr(a, b);
4277 } /* identities_cmp */
4280 * Calculate a hash value of a node.
4282 unsigned ir_node_hash(ir_node *node) {
4286 if (node->op == op_Const) {
4287 /* special value for const, as they only differ in their tarval. */
4288 h = HASH_PTR(node->attr.con.tv);
4289 h = 9*h + HASH_PTR(get_irn_mode(node));
4290 } else if (node->op == op_SymConst) {
4291 /* special value for const, as they only differ in their symbol. */
4292 h = HASH_PTR(node->attr.symc.sym.type_p);
4293 h = 9*h + HASH_PTR(get_irn_mode(node));
4296 /* hash table value = 9*(9*(9*(9*(9*arity+in[0])+in[1])+ ...)+mode)+code */
4297 h = irn_arity = get_irn_intra_arity(node);
4299 /* consider all in nodes... except the block if not a control flow. */
4300 for (i = is_cfop(node) ? -1 : 0; i < irn_arity; i++) {
4301 h = 9*h + HASH_PTR(get_irn_intra_n(node, i));
4305 h = 9*h + HASH_PTR(get_irn_mode(node));
4307 h = 9*h + HASH_PTR(get_irn_op(node));
4311 } /* ir_node_hash */
4313 pset *new_identities(void) {
4314 return new_pset(identities_cmp, N_IR_NODES);
4315 } /* new_identities */
4317 void del_identities(pset *value_table) {
4318 del_pset(value_table);
4319 } /* del_identities */
4322 * Return the canonical node computing the same value as n.
4324 * @param value_table The value table
4325 * @param n The node to lookup
4327 * Looks up the node in a hash table.
4329 * For Const nodes this is performed in the constructor, too. Const
4330 * nodes are extremely time critical because of their frequent use in
4331 * constant string arrays.
4333 static INLINE ir_node *identify(pset *value_table, ir_node *n) {
4336 if (!value_table) return n;
4338 if (get_opt_reassociation()) {
4339 if (is_op_commutative(get_irn_op(n))) {
4340 ir_node *l = get_binop_left(n);
4341 ir_node *r = get_binop_right(n);
4343 /* for commutative operators perform a OP b == b OP a */
4344 if (get_irn_idx(l) > get_irn_idx(r)) {
4345 set_binop_left(n, r);
4346 set_binop_right(n, l);
4351 o = pset_find(value_table, n, ir_node_hash(n));
4360 * During construction we set the op_pin_state_pinned flag in the graph right when the
4361 * optimization is performed. The flag turning on procedure global cse could
4362 * be changed between two allocations. This way we are safe.
4364 static INLINE ir_node *identify_cons(pset *value_table, ir_node *n) {
4367 n = identify(value_table, n);
4368 if (get_irn_n(old, -1) != get_irn_n(n, -1))
4369 set_irg_pinned(current_ir_graph, op_pin_state_floats);
4371 } /* identify_cons */
4374 * Return the canonical node computing the same value as n.
4375 * Looks up the node in a hash table, enters it in the table
4376 * if it isn't there yet.
4378 ir_node *identify_remember(pset *value_table, ir_node *n) {
4381 if (!value_table) return n;
4383 if (get_opt_reassociation()) {
4384 if (is_op_commutative(get_irn_op(n))) {
4385 ir_node *l = get_binop_left(n);
4386 ir_node *r = get_binop_right(n);
4387 int l_idx = get_irn_idx(l);
4388 int r_idx = get_irn_idx(r);
4390 /* For commutative operators perform a OP b == b OP a but keep
4391 constants on the RIGHT side. This helps greatly in some optimizations.
4392 Moreover we use the idx number to make the form deterministic. */
4393 if (is_irn_constlike(l))
4395 if (is_irn_constlike(r))
4397 if (l_idx < r_idx) {
4398 set_binop_left(n, r);
4399 set_binop_right(n, l);
4404 /* lookup or insert in hash table with given hash key. */
4405 o = pset_insert(value_table, n, ir_node_hash(n));
4412 } /* identify_remember */
4414 /* Add a node to the identities value table. */
4415 void add_identities(pset *value_table, ir_node *node) {
4416 if (get_opt_cse() && is_no_Block(node))
4417 identify_remember(value_table, node);
4418 } /* add_identities */
4420 /* Visit each node in the value table of a graph. */
4421 void visit_all_identities(ir_graph *irg, irg_walk_func visit, void *env) {
4423 ir_graph *rem = current_ir_graph;
4425 current_ir_graph = irg;
4426 foreach_pset(irg->value_table, node)
4428 current_ir_graph = rem;
4429 } /* visit_all_identities */
4432 * Garbage in, garbage out. If a node has a dead input, i.e., the
4433 * Bad node is input to the node, return the Bad node.
4435 static INLINE ir_node *gigo(ir_node *node) {
4437 ir_op *op = get_irn_op(node);
4439 /* remove garbage blocks by looking at control flow that leaves the block
4440 and replacing the control flow by Bad. */
4441 if (get_irn_mode(node) == mode_X) {
4442 ir_node *block = get_nodes_block(skip_Proj(node));
4444 /* Don't optimize nodes in immature blocks. */
4445 if (!get_Block_matured(block)) return node;
4446 /* Don't optimize End, may have Bads. */
4447 if (op == op_End) return node;
4449 if (is_Block(block)) {
4450 irn_arity = get_irn_arity(block);
4451 for (i = 0; i < irn_arity; i++) {
4452 if (!is_Bad(get_irn_n(block, i)))
4455 if (i == irn_arity) {
4456 ir_graph *irg = get_irn_irg(block);
4457 /* the start block is never dead */
4458 if (block != get_irg_start_block(irg)
4459 && block != get_irg_end_block(irg))
4465 /* Blocks, Phis and Tuples may have dead inputs, e.g., if one of the
4466 blocks predecessors is dead. */
4467 if (op != op_Block && op != op_Phi && op != op_Tuple) {
4468 irn_arity = get_irn_arity(node);
4471 * Beware: we can only read the block of a non-floating node.
4473 if (is_irn_pinned_in_irg(node) &&
4474 is_Block_dead(get_nodes_block(node)))
4477 for (i = 0; i < irn_arity; i++) {
4478 ir_node *pred = get_irn_n(node, i);
4483 /* Propagating Unknowns here seems to be a bad idea, because
4484 sometimes we need a node as a input and did not want that
4486 However, it might be useful to move this into a later phase
4487 (if you think that optimizing such code is useful). */
4488 if (is_Unknown(pred) && mode_is_data(get_irn_mode(node)))
4489 return new_Unknown(get_irn_mode(node));
4494 /* With this code we violate the agreement that local_optimize
4495 only leaves Bads in Block, Phi and Tuple nodes. */
4496 /* If Block has only Bads as predecessors it's garbage. */
4497 /* If Phi has only Bads as predecessors it's garbage. */
4498 if ((op == op_Block && get_Block_matured(node)) || op == op_Phi) {
4499 irn_arity = get_irn_arity(node);
4500 for (i = 0; i < irn_arity; i++) {
4501 if (!is_Bad(get_irn_n(node, i))) break;
4503 if (i == irn_arity) node = new_Bad();
4510 * These optimizations deallocate nodes from the obstack.
4511 * It can only be called if it is guaranteed that no other nodes
4512 * reference this one, i.e., right after construction of a node.
4514 * @param n The node to optimize
4516 * current_ir_graph must be set to the graph of the node!
4518 ir_node *optimize_node(ir_node *n) {
4521 ir_opcode iro = get_irn_opcode(n);
4523 /* Always optimize Phi nodes: part of the construction. */
4524 if ((!get_opt_optimize()) && (iro != iro_Phi)) return n;
4526 /* constant expression evaluation / constant folding */
4527 if (get_opt_constant_folding()) {
4528 /* neither constants nor Tuple values can be evaluated */
4529 if (iro != iro_Const && (get_irn_mode(n) != mode_T)) {
4530 unsigned fp_model = get_irg_fp_model(current_ir_graph);
4531 int old_fp_mode = tarval_enable_fp_ops((fp_model & fp_strict_algebraic) == 0);
4532 /* try to evaluate */
4533 tv = computed_value(n);
4534 if (tv != tarval_bad) {
4536 ir_type *old_tp = get_irn_type(n);
4537 int i, arity = get_irn_arity(n);
4541 * Try to recover the type of the new expression.
4543 for (i = 0; i < arity && !old_tp; ++i)
4544 old_tp = get_irn_type(get_irn_n(n, i));
4547 * we MUST copy the node here temporary, because it's still needed
4548 * for DBG_OPT_CSTEVAL
4550 node_size = offsetof(ir_node, attr) + n->op->attr_size;
4551 oldn = alloca(node_size);
4553 memcpy(oldn, n, node_size);
4554 CLONE_ARR_A(ir_node *, oldn->in, n->in);
4556 /* ARG, copy the in array, we need it for statistics */
4557 memcpy(oldn->in, n->in, ARR_LEN(n->in) * sizeof(n->in[0]));
4559 /* note the inplace edges module */
4560 edges_node_deleted(n, current_ir_graph);
4562 /* evaluation was successful -- replace the node. */
4563 irg_kill_node(current_ir_graph, n);
4564 nw = new_Const(get_tarval_mode (tv), tv);
4566 if (old_tp && get_type_mode(old_tp) == get_tarval_mode (tv))
4567 set_Const_type(nw, old_tp);
4568 DBG_OPT_CSTEVAL(oldn, nw);
4569 tarval_enable_fp_ops(old_fp_mode);
4572 tarval_enable_fp_ops(old_fp_mode);
4576 /* remove unnecessary nodes */
4577 if (get_opt_constant_folding() ||
4578 (iro == iro_Phi) || /* always optimize these nodes. */
4580 (iro == iro_Proj) ||
4581 (iro == iro_Block) ) /* Flags tested local. */
4582 n = equivalent_node(n);
4584 optimize_preds(n); /* do node specific optimizations of nodes predecessors. */
4586 /* Common Subexpression Elimination.
4588 * Checks whether n is already available.
4589 * The block input is used to distinguish different subexpressions. Right
4590 * now all nodes are op_pin_state_pinned to blocks, i.e., the CSE only finds common
4591 * subexpressions within a block.
4594 n = identify_cons(current_ir_graph->value_table, n);
4597 edges_node_deleted(oldn, current_ir_graph);
4599 /* We found an existing, better node, so we can deallocate the old node. */
4600 irg_kill_node(current_ir_graph, oldn);
4604 /* Some more constant expression evaluation that does not allow to
4606 iro = get_irn_opcode(n);
4607 if (get_opt_constant_folding() ||
4608 (iro == iro_Cond) ||
4609 (iro == iro_Proj)) /* Flags tested local. */
4610 n = transform_node(n);
4612 /* Remove nodes with dead (Bad) input.
4613 Run always for transformation induced Bads. */
4616 /* Now we have a legal, useful node. Enter it in hash table for CSE */
4617 if (get_opt_cse() && (get_irn_opcode(n) != iro_Block)) {
4618 n = identify_remember(current_ir_graph->value_table, n);
4622 } /* optimize_node */
4626 * These optimizations never deallocate nodes (in place). This can cause dead
4627 * nodes lying on the obstack. Remove these by a dead node elimination,
4628 * i.e., a copying garbage collection.
4630 ir_node *optimize_in_place_2(ir_node *n) {
4633 ir_opcode iro = get_irn_opcode(n);
4635 if (!get_opt_optimize() && (get_irn_op(n) != op_Phi)) return n;
4637 /* constant expression evaluation / constant folding */
4638 if (get_opt_constant_folding()) {
4639 /* neither constants nor Tuple values can be evaluated */
4640 if (iro != iro_Const && get_irn_mode(n) != mode_T) {
4641 unsigned fp_model = get_irg_fp_model(current_ir_graph);
4642 int old_fp_mode = tarval_enable_fp_ops((fp_model & fp_strict_algebraic) == 0);
4643 /* try to evaluate */
4644 tv = computed_value(n);
4645 if (tv != tarval_bad) {
4646 /* evaluation was successful -- replace the node. */
4647 ir_type *old_tp = get_irn_type(n);
4648 int i, arity = get_irn_arity(n);
4651 * Try to recover the type of the new expression.
4653 for (i = 0; i < arity && !old_tp; ++i)
4654 old_tp = get_irn_type(get_irn_n(n, i));
4656 n = new_Const(get_tarval_mode(tv), tv);
4658 if (old_tp && get_type_mode(old_tp) == get_tarval_mode(tv))
4659 set_Const_type(n, old_tp);
4661 DBG_OPT_CSTEVAL(oldn, n);
4662 tarval_enable_fp_ops(old_fp_mode);
4665 tarval_enable_fp_ops(old_fp_mode);
4669 /* remove unnecessary nodes */
4670 if (get_opt_constant_folding() ||
4671 (iro == iro_Phi) || /* always optimize these nodes. */
4672 (iro == iro_Id) || /* ... */
4673 (iro == iro_Proj) || /* ... */
4674 (iro == iro_Block) ) /* Flags tested local. */
4675 n = equivalent_node(n);
4677 optimize_preds(n); /* do node specific optimizations of nodes predecessors. */
4679 /** common subexpression elimination **/
4680 /* Checks whether n is already available. */
4681 /* The block input is used to distinguish different subexpressions. Right
4682 now all nodes are op_pin_state_pinned to blocks, i.e., the cse only finds common
4683 subexpressions within a block. */
4684 if (get_opt_cse()) {
4685 n = identify(current_ir_graph->value_table, n);
4688 /* Some more constant expression evaluation. */
4689 iro = get_irn_opcode(n);
4690 if (get_opt_constant_folding() ||
4691 (iro == iro_Cond) ||
4692 (iro == iro_Proj)) /* Flags tested local. */
4693 n = transform_node(n);
4695 /* Remove nodes with dead (Bad) input.
4696 Run always for transformation induced Bads. */
4699 /* Now we can verify the node, as it has no dead inputs any more. */
4702 /* Now we have a legal, useful node. Enter it in hash table for cse.
4703 Blocks should be unique anyways. (Except the successor of start:
4704 is cse with the start block!) */
4705 if (get_opt_cse() && (get_irn_opcode(n) != iro_Block))
4706 n = identify_remember(current_ir_graph->value_table, n);
4709 } /* optimize_in_place_2 */
4712 * Wrapper for external use, set proper status bits after optimization.
4714 ir_node *optimize_in_place(ir_node *n) {
4715 /* Handle graph state */
4716 assert(get_irg_phase_state(current_ir_graph) != phase_building);
4718 if (get_opt_global_cse())
4719 set_irg_pinned(current_ir_graph, op_pin_state_floats);
4720 if (get_irg_outs_state(current_ir_graph) == outs_consistent)
4721 set_irg_outs_inconsistent(current_ir_graph);
4723 /* FIXME: Maybe we could also test whether optimizing the node can
4724 change the control graph. */
4725 set_irg_doms_inconsistent(current_ir_graph);
4726 return optimize_in_place_2(n);
4727 } /* optimize_in_place */
4730 * Sets the default operation for an ir_ops.
4732 ir_op_ops *firm_set_default_operations(ir_opcode code, ir_op_ops *ops) {
4733 ops = firm_set_default_computed_value(code, ops);
4734 ops = firm_set_default_equivalent_node(code, ops);
4735 ops = firm_set_default_transform_node(code, ops);
4736 ops = firm_set_default_node_cmp_attr(code, ops);
4737 ops = firm_set_default_get_type(code, ops);
4738 ops = firm_set_default_get_type_attr(code, ops);
4739 ops = firm_set_default_get_entity_attr(code, ops);
4742 } /* firm_set_default_operations */