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 /* leave strict floating point Conv's */
1148 if (get_Conv_strict(n))
1151 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CONV);
1152 } else if (get_irn_op(a) == op_Conv) { /* Conv(Conv(b)) */
1156 n_mode = get_irn_mode(n);
1157 b_mode = get_irn_mode(b);
1159 if (n_mode == b_mode) {
1160 if (n_mode == mode_b) {
1161 n = b; /* Convb(Conv*(xxxb(...))) == xxxb(...) */
1162 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
1163 } else if (mode_is_int(n_mode) || mode_is_character(n_mode)) {
1164 if (smaller_mode(b_mode, a_mode)){
1165 n = b; /* ConvS(ConvL(xxxS(...))) == xxxS(...) */
1166 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
1172 } /* equivalent_node_Conv */
1175 * A Cast may be removed if the type of the previous node
1176 * is already the type of the Cast.
1178 static ir_node *equivalent_node_Cast(ir_node *n) {
1180 ir_node *pred = get_Cast_op(n);
1182 if (get_irn_type(pred) == get_Cast_type(n)) {
1184 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CAST);
1187 } /* equivalent_node_Cast */
1190 * Several optimizations:
1191 * - no Phi in start block.
1192 * - remove Id operators that are inputs to Phi
1193 * - fold Phi-nodes, iff they have only one predecessor except
1196 static ir_node *equivalent_node_Phi(ir_node *n) {
1200 ir_node *block = NULL; /* to shutup gcc */
1201 ir_node *first_val = NULL; /* to shutup gcc */
1203 if (!get_opt_normalize()) return n;
1205 n_preds = get_Phi_n_preds(n);
1207 block = get_nodes_block(n);
1208 /* @@@ fliegt 'raus, sollte aber doch immer wahr sein!!!
1209 assert(get_irn_arity(block) == n_preds && "phi in wrong block!"); */
1210 if ((is_Block_dead(block)) || /* Control dead */
1211 (block == get_irg_start_block(current_ir_graph))) /* There should be no Phi nodes */
1212 return new_Bad(); /* in the Start Block. */
1214 if (n_preds == 0) return n; /* Phi of dead Region without predecessors. */
1216 /* If the Block has a Bad pred, we also have one. */
1217 for (i = 0; i < n_preds; ++i)
1218 if (is_Bad(get_Block_cfgpred(block, i)))
1219 set_Phi_pred(n, i, new_Bad());
1221 /* Find first non-self-referencing input */
1222 for (i = 0; i < n_preds; ++i) {
1223 first_val = get_Phi_pred(n, i);
1224 if ( (first_val != n) /* not self pointer */
1226 && (! is_Bad(first_val))
1228 ) { /* value not dead */
1229 break; /* then found first value. */
1234 /* A totally Bad or self-referencing Phi (we didn't break the above loop) */
1238 /* search for rest of inputs, determine if any of these
1239 are non-self-referencing */
1240 while (++i < n_preds) {
1241 ir_node *scnd_val = get_Phi_pred(n, i);
1242 if ( (scnd_val != n)
1243 && (scnd_val != first_val)
1245 && (! is_Bad(scnd_val))
1253 /* Fold, if no multiple distinct non-self-referencing inputs */
1255 DBG_OPT_PHI(oldn, n);
1258 } /* equivalent_node_Phi */
1261 * Several optimizations:
1262 * - no Sync in start block.
1263 * - fold Sync-nodes, iff they have only one predecessor except
1266 static ir_node *equivalent_node_Sync(ir_node *n) {
1270 ir_node *first_val = NULL; /* to shutup gcc */
1272 if (!get_opt_normalize()) return n;
1274 n_preds = get_Sync_n_preds(n);
1276 /* Find first non-self-referencing input */
1277 for (i = 0; i < n_preds; ++i) {
1278 first_val = get_Sync_pred(n, i);
1279 if ((first_val != n) /* not self pointer */ &&
1280 (! is_Bad(first_val))
1281 ) { /* value not dead */
1282 break; /* then found first value. */
1287 /* A totally Bad or self-referencing Sync (we didn't break the above loop) */
1290 /* search the rest of inputs, determine if any of these
1291 are non-self-referencing */
1292 while (++i < n_preds) {
1293 ir_node *scnd_val = get_Sync_pred(n, i);
1294 if ((scnd_val != n) &&
1295 (scnd_val != first_val) &&
1296 (! is_Bad(scnd_val))
1302 /* Fold, if no multiple distinct non-self-referencing inputs */
1304 DBG_OPT_SYNC(oldn, n);
1307 } /* equivalent_node_Sync */
1310 * Optimize Proj(Tuple) and gigo() for ProjX in Bad block,
1311 * ProjX(Load) and ProjX(Store).
1313 static ir_node *equivalent_node_Proj(ir_node *proj) {
1314 ir_node *oldn = proj;
1315 ir_node *a = get_Proj_pred(proj);
1317 if (get_irn_op(a) == op_Tuple) {
1318 /* Remove the Tuple/Proj combination. */
1319 if ( get_Proj_proj(proj) <= get_Tuple_n_preds(a) ) {
1320 proj = get_Tuple_pred(a, get_Proj_proj(proj));
1321 DBG_OPT_TUPLE(oldn, a, proj);
1323 /* This should not happen! */
1324 assert(! "found a Proj with higher number than Tuple predecessors");
1327 } else if (get_irn_mode(proj) == mode_X) {
1328 if (is_Block_dead(get_nodes_block(skip_Proj(proj)))) {
1329 /* Remove dead control flow -- early gigo(). */
1331 } else if (get_opt_ldst_only_null_ptr_exceptions()) {
1332 ir_op *op = get_irn_op(a);
1334 if (op == op_Load) {
1335 /* get the Load address */
1336 ir_node *addr = get_Load_ptr(a);
1337 ir_node *blk = get_irn_n(a, -1);
1340 if (value_not_null(addr, &confirm)) {
1341 if (confirm == NULL) {
1342 /* this node may float if it did not depend on a Confirm */
1343 set_irn_pinned(a, op_pin_state_floats);
1345 if (get_Proj_proj(proj) == pn_Load_X_except) {
1346 DBG_OPT_EXC_REM(proj);
1349 return new_r_Jmp(current_ir_graph, blk);
1351 } else if (op == op_Store) {
1352 /* get the load/store address */
1353 ir_node *addr = get_Store_ptr(a);
1354 ir_node *blk = get_irn_n(a, -1);
1357 if (value_not_null(addr, &confirm)) {
1358 if (confirm == NULL) {
1359 /* this node may float if it did not depend on a Confirm */
1360 set_irn_pinned(a, op_pin_state_floats);
1362 if (get_Proj_proj(proj) == pn_Store_X_except) {
1363 DBG_OPT_EXC_REM(proj);
1366 return new_r_Jmp(current_ir_graph, blk);
1373 } /* equivalent_node_Proj */
1378 static ir_node *equivalent_node_Id(ir_node *n) {
1383 } while (get_irn_op(n) == op_Id);
1385 DBG_OPT_ID(oldn, n);
1387 } /* equivalent_node_Id */
1392 static ir_node *equivalent_node_Mux(ir_node *n)
1394 ir_node *oldn = n, *sel = get_Mux_sel(n);
1395 tarval *ts = value_of(sel);
1397 /* Mux(true, f, t) == t */
1398 if (ts == tarval_b_true) {
1399 n = get_Mux_true(n);
1400 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_C);
1402 /* Mux(false, f, t) == f */
1403 else if (ts == tarval_b_false) {
1404 n = get_Mux_false(n);
1405 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_C);
1407 /* Mux(v, x, x) == x */
1408 else if (get_Mux_false(n) == get_Mux_true(n)) {
1409 n = get_Mux_true(n);
1410 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_EQ);
1412 else if (get_irn_op(sel) == op_Proj && !mode_honor_signed_zeros(get_irn_mode(n))) {
1413 ir_node *cmp = get_Proj_pred(sel);
1414 long proj_nr = get_Proj_proj(sel);
1415 ir_node *b = get_Mux_false(n);
1416 ir_node *a = get_Mux_true(n);
1419 * Note: normalization puts the constant on the right site,
1420 * so we check only one case.
1422 * Note further that these optimization work even for floating point
1423 * with NaN's because -NaN == NaN.
1424 * However, if +0 and -0 is handled differently, we cannot use the first one.
1426 if (get_irn_op(cmp) == op_Cmp && get_Cmp_left(cmp) == a) {
1427 if (classify_Const(get_Cmp_right(cmp)) == CNST_NULL) {
1428 /* Mux(a CMP 0, X, a) */
1429 if (get_irn_op(b) == op_Minus && get_Minus_op(b) == a) {
1430 /* Mux(a CMP 0, -a, a) */
1431 if (proj_nr == pn_Cmp_Eq) {
1432 /* Mux(a == 0, -a, a) ==> -a */
1434 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1435 } else if (proj_nr == pn_Cmp_Lg || proj_nr == pn_Cmp_Ne) {
1436 /* Mux(a != 0, -a, a) ==> a */
1438 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1440 } else if (classify_Const(b) == CNST_NULL) {
1441 /* Mux(a CMP 0, 0, a) */
1442 if (proj_nr == pn_Cmp_Lg || proj_nr == pn_Cmp_Ne) {
1443 /* Mux(a != 0, 0, a) ==> a */
1445 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1446 } else if (proj_nr == pn_Cmp_Eq) {
1447 /* Mux(a == 0, 0, a) ==> 0 */
1449 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1456 } /* equivalent_node_Mux */
1459 * Returns a equivalent node of a Psi: if a condition is true
1460 * and all previous conditions are false we know its value.
1461 * If all conditions are false its value is the default one.
1463 static ir_node *equivalent_node_Psi(ir_node *n) {
1465 return equivalent_node_Mux(n);
1467 } /* equivalent_node_Psi */
1470 * Optimize -a CMP -b into b CMP a.
1471 * This works only for for modes where unary Minus
1473 * Note that two-complement integers can Overflow
1474 * so it will NOT work.
1476 * For == and != can be handled in Proj(Cmp)
1478 static ir_node *equivalent_node_Cmp(ir_node *n) {
1479 ir_node *left = get_Cmp_left(n);
1480 ir_node *right = get_Cmp_right(n);
1482 if (get_irn_op(left) == op_Minus && get_irn_op(right) == op_Minus &&
1483 !mode_overflow_on_unary_Minus(get_irn_mode(left))) {
1484 left = get_Minus_op(left);
1485 right = get_Minus_op(right);
1486 set_Cmp_left(n, right);
1487 set_Cmp_right(n, left);
1490 } /* equivalent_node_Cmp */
1493 * Remove Confirm nodes if setting is on.
1494 * Replace Confirms(x, '=', Constlike) by Constlike.
1496 static ir_node *equivalent_node_Confirm(ir_node *n) {
1497 ir_node *pred = get_Confirm_value(n);
1498 pn_Cmp pnc = get_Confirm_cmp(n);
1500 if (get_irn_op(pred) == op_Confirm && pnc == get_Confirm_cmp(pred)) {
1502 * rare case: two identical Confirms one after another,
1503 * replace the second one with the first.
1507 if (pnc == pn_Cmp_Eq) {
1508 ir_node *bound = get_Confirm_bound(n);
1511 * Optimize a rare case:
1512 * Confirm(x, '=', Constlike) ==> Constlike
1514 if (is_irn_constlike(bound)) {
1515 DBG_OPT_CONFIRM(n, bound);
1519 return get_opt_remove_confirm() ? get_Confirm_value(n) : n;
1523 * Optimize CopyB(mem, x, x) into a Nop.
1525 static ir_node *equivalent_node_CopyB(ir_node *n) {
1526 ir_node *a = get_CopyB_dst(n);
1527 ir_node *b = get_CopyB_src(n);
1530 /* Turn CopyB into a tuple (mem, jmp, bad, bad) */
1531 ir_node *mem = get_CopyB_mem(n);
1532 ir_node *blk = get_nodes_block(n);
1533 turn_into_tuple(n, pn_CopyB_max);
1534 set_Tuple_pred(n, pn_CopyB_M, mem);
1535 set_Tuple_pred(n, pn_CopyB_X_regular, new_r_Jmp(current_ir_graph, blk));
1536 set_Tuple_pred(n, pn_CopyB_X_except, new_Bad()); /* no exception */
1537 set_Tuple_pred(n, pn_CopyB_M_except, new_Bad());
1540 } /* equivalent_node_CopyB */
1543 * Optimize Bounds(idx, idx, upper) into idx.
1545 static ir_node *equivalent_node_Bound(ir_node *n) {
1546 ir_node *idx = get_Bound_index(n);
1547 ir_node *lower = get_Bound_lower(n);
1550 /* By definition lower < upper, so if idx == lower -->
1551 lower <= idx && idx < upper */
1553 /* Turn Bound into a tuple (mem, jmp, bad, idx) */
1556 ir_node *pred = skip_Proj(idx);
1558 if (get_irn_op(pred) == op_Bound) {
1560 * idx was Bounds_check previously, it is still valid if
1561 * lower <= pred_lower && pred_upper <= upper.
1563 ir_node *upper = get_Bound_upper(n);
1564 if (get_Bound_lower(pred) == lower &&
1565 get_Bound_upper(pred) == upper) {
1567 * One could expect that we simply return the previous
1568 * Bound here. However, this would be wrong, as we could
1569 * add an exception Proj to a new location than.
1570 * So, we must turn in into a tuple
1577 /* Turn Bound into a tuple (mem, jmp, bad, idx) */
1578 ir_node *mem = get_Bound_mem(n);
1579 ir_node *blk = get_nodes_block(n);
1580 turn_into_tuple(n, pn_Bound_max);
1581 set_Tuple_pred(n, pn_Bound_M, mem);
1582 set_Tuple_pred(n, pn_Bound_X_regular, new_r_Jmp(current_ir_graph, blk)); /* no exception */
1583 set_Tuple_pred(n, pn_Bound_X_except, new_Bad()); /* no exception */
1584 set_Tuple_pred(n, pn_Bound_res, idx);
1587 } /* equivalent_node_Bound */
1590 * equivalent_node() returns a node equivalent to input n. It skips all nodes that
1591 * perform no actual computation, as, e.g., the Id nodes. It does not create
1592 * new nodes. It is therefore safe to free n if the node returned is not n.
1593 * If a node returns a Tuple we can not just skip it. If the size of the
1594 * in array fits, we transform n into a tuple (e.g., Div).
1596 ir_node *equivalent_node(ir_node *n) {
1597 if (n->op->ops.equivalent_node)
1598 return n->op->ops.equivalent_node(n);
1600 } /* equivalent_node */
1603 * Sets the default equivalent node operation for an ir_op_ops.
1605 * @param code the opcode for the default operation
1606 * @param ops the operations initialized
1611 static ir_op_ops *firm_set_default_equivalent_node(ir_opcode code, ir_op_ops *ops)
1615 ops->equivalent_node = equivalent_node_##a; \
1655 } /* firm_set_default_equivalent_node */
1658 * Do node specific optimizations of nodes predecessors.
1660 static void optimize_preds(ir_node *n) {
1661 switch (get_irn_opcode(n)) {
1663 case iro_Cmp: { /* We don't want Cast as input to Cmp. */
1664 ir_node *a = get_Cmp_left(n), *b = get_Cmp_right(n);
1666 if (get_irn_op(a) == op_Cast) {
1670 if (get_irn_op(b) == op_Cast) {
1672 set_Cmp_right(n, b);
1679 } /* optimize_preds */
1682 * Returns non-zero if a node is a Phi node
1683 * with all predecessors constant.
1685 static int is_const_Phi(ir_node *n) {
1690 for (i = get_irn_arity(n) - 1; i >= 0; --i)
1691 if (! is_Const(get_irn_n(n, i)))
1694 } /* is_const_Phi */
1697 * Apply an evaluator on a binop with a constant operators (and one Phi).
1699 * @param phi the Phi node
1700 * @param other the other operand
1701 * @param eval an evaluator function
1702 * @param left if non-zero, other is the left operand, else the right
1704 * @return a new Phi node if the conversion was successful, NULL else
1706 static ir_node *apply_binop_on_phi(ir_node *phi, tarval *other, tarval *(*eval)(tarval *, tarval *), int left) {
1712 int i, n = get_irn_arity(phi);
1714 NEW_ARR_A(void *, res, n);
1716 for (i = 0; i < n; ++i) {
1717 pred = get_irn_n(phi, i);
1718 tv = get_Const_tarval(pred);
1719 tv = eval(other, tv);
1721 if (tv == tarval_bad) {
1722 /* folding failed, bad */
1728 for (i = 0; i < n; ++i) {
1729 pred = get_irn_n(phi, i);
1730 tv = get_Const_tarval(pred);
1731 tv = eval(tv, other);
1733 if (tv == tarval_bad) {
1734 /* folding failed, bad */
1740 mode = get_irn_mode(phi);
1741 irg = current_ir_graph;
1742 for (i = 0; i < n; ++i) {
1743 pred = get_irn_n(phi, i);
1744 res[i] = new_r_Const_type(irg, get_irg_start_block(irg),
1745 mode, res[i], get_Const_type(pred));
1747 return new_r_Phi(irg, get_nodes_block(phi), n, (ir_node **)res, mode);
1748 } /* apply_binop_on_phi */
1751 * Apply an evaluator on a unop with a constant operator (a Phi).
1753 * @param phi the Phi node
1754 * @param eval an evaluator function
1756 * @return a new Phi node if the conversion was successful, NULL else
1758 static ir_node *apply_unop_on_phi(ir_node *phi, tarval *(*eval)(tarval *)) {
1764 int i, n = get_irn_arity(phi);
1766 NEW_ARR_A(void *, res, n);
1767 for (i = 0; i < n; ++i) {
1768 pred = get_irn_n(phi, i);
1769 tv = get_Const_tarval(pred);
1772 if (tv == tarval_bad) {
1773 /* folding failed, bad */
1778 mode = get_irn_mode(phi);
1779 irg = current_ir_graph;
1780 for (i = 0; i < n; ++i) {
1781 pred = get_irn_n(phi, i);
1782 res[i] = new_r_Const_type(irg, get_irg_start_block(irg),
1783 mode, res[i], get_Const_type(pred));
1785 return new_r_Phi(irg, get_nodes_block(phi), n, (ir_node **)res, mode);
1786 } /* apply_unop_on_phi */
1789 * Transform AddP(P, ConvIs(Iu)), AddP(P, ConvIu(Is)) and
1790 * SubP(P, ConvIs(Iu)), SubP(P, ConvIu(Is)).
1791 * If possible, remove the Conv's.
1793 static ir_node *transform_node_AddSub(ir_node *n) {
1794 ir_mode *mode = get_irn_mode(n);
1796 if (mode_is_reference(mode)) {
1797 ir_node *left = get_binop_left(n);
1798 ir_node *right = get_binop_right(n);
1799 int ref_bits = get_mode_size_bits(mode);
1801 if (get_irn_op(left) == op_Conv) {
1802 ir_mode *mode = get_irn_mode(left);
1803 int bits = get_mode_size_bits(mode);
1805 if (ref_bits == bits &&
1806 mode_is_int(mode) &&
1807 get_mode_arithmetic(mode) == irma_twos_complement) {
1808 ir_node *pre = get_Conv_op(left);
1809 ir_mode *pre_mode = get_irn_mode(pre);
1811 if (mode_is_int(pre_mode) &&
1812 get_mode_size_bits(pre_mode) == bits &&
1813 get_mode_arithmetic(pre_mode) == irma_twos_complement) {
1814 /* ok, this conv just changes to sign, moreover the calculation
1815 * is done with same number of bits as our address mode, so
1816 * we can ignore the conv as address calculation can be viewed
1817 * as either signed or unsigned
1819 set_binop_left(n, pre);
1824 if (get_irn_op(right) == op_Conv) {
1825 ir_mode *mode = get_irn_mode(right);
1826 int bits = get_mode_size_bits(mode);
1828 if (ref_bits == bits &&
1829 mode_is_int(mode) &&
1830 get_mode_arithmetic(mode) == irma_twos_complement) {
1831 ir_node *pre = get_Conv_op(right);
1832 ir_mode *pre_mode = get_irn_mode(pre);
1834 if (mode_is_int(pre_mode) &&
1835 get_mode_size_bits(pre_mode) == bits &&
1836 get_mode_arithmetic(pre_mode) == irma_twos_complement) {
1837 /* ok, this conv just changes to sign, moreover the calculation
1838 * is done with same number of bits as our address mode, so
1839 * we can ignore the conv as address calculation can be viewed
1840 * as either signed or unsigned
1842 set_binop_right(n, pre);
1848 } /* transform_node_AddSub */
1850 #define HANDLE_BINOP_PHI(op,a,b,c) \
1852 if (is_Const(b) && is_const_Phi(a)) { \
1853 /* check for Op(Phi, Const) */ \
1854 c = apply_binop_on_phi(a, get_Const_tarval(b), op, 0); \
1856 else if (is_Const(a) && is_const_Phi(b)) { \
1857 /* check for Op(Const, Phi) */ \
1858 c = apply_binop_on_phi(b, get_Const_tarval(a), op, 1); \
1861 DBG_OPT_ALGSIM0(oldn, c, FS_OPT_CONST_PHI); \
1865 #define HANDLE_UNOP_PHI(op,a,c) \
1867 if (is_const_Phi(a)) { \
1868 /* check for Op(Phi) */ \
1869 c = apply_unop_on_phi(a, op); \
1872 DBG_OPT_ALGSIM0(oldn, c, FS_OPT_CONST_PHI); \
1878 * Do the AddSub optimization, then Transform
1879 * Constant folding on Phi
1880 * Add(a,a) -> Mul(a, 2)
1881 * Add(Mul(a, x), a) -> Mul(a, x+1)
1882 * if the mode is integer or float.
1883 * Transform Add(a,-b) into Sub(a,b).
1884 * Reassociation might fold this further.
1886 static ir_node *transform_node_Add(ir_node *n) {
1888 ir_node *a, *b, *c, *oldn = n;
1890 n = transform_node_AddSub(n);
1892 a = get_Add_left(n);
1893 b = get_Add_right(n);
1895 HANDLE_BINOP_PHI(tarval_add, a,b,c);
1897 mode = get_irn_mode(n);
1899 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
1900 if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
1903 if (mode_is_num(mode)) {
1905 ir_node *block = get_irn_n(n, -1);
1908 get_irn_dbg_info(n),
1912 new_r_Const_long(current_ir_graph, block, mode, 2),
1914 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_A);
1915 } else if (get_irn_op(a) == op_Minus) {
1917 get_irn_dbg_info(n),
1923 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_MINUS_B);
1924 } else if (get_irn_op(b) == op_Minus) {
1926 get_irn_dbg_info(n),
1932 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_MINUS_B);
1934 /* do NOT execute this code if reassociation is enabled, it does the inverse! */
1935 else if (!get_opt_reassociation() && get_irn_op(a) == op_Mul) {
1936 ir_node *ma = get_Mul_left(a);
1937 ir_node *mb = get_Mul_right(a);
1940 ir_node *blk = get_irn_n(n, -1);
1942 get_irn_dbg_info(n), current_ir_graph, blk,
1945 get_irn_dbg_info(n), current_ir_graph, blk,
1947 new_r_Const_long(current_ir_graph, blk, mode, 1),
1950 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_MUL_A_X_A);
1951 } else if (b == mb) {
1952 ir_node *blk = get_irn_n(n, -1);
1954 get_irn_dbg_info(n), current_ir_graph, blk,
1957 get_irn_dbg_info(n), current_ir_graph, blk,
1959 new_r_Const_long(current_ir_graph, blk, mode, 1),
1962 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_MUL_A_X_A);
1965 /* do NOT execute this code if reassociation is enabled, it does the inverse! */
1966 else if (!get_opt_reassociation() && get_irn_op(b) == op_Mul) {
1967 ir_node *ma = get_Mul_left(b);
1968 ir_node *mb = get_Mul_right(b);
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);
1982 } else if (a == mb) {
1983 ir_node *blk = get_irn_n(n, -1);
1985 get_irn_dbg_info(n), current_ir_graph, blk,
1988 get_irn_dbg_info(n), current_ir_graph, blk,
1990 new_r_Const_long(current_ir_graph, blk, mode, 1),
1993 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_MUL_A_X_A);
1998 } /* transform_node_Add */
2001 * Do the AddSub optimization, then Transform
2002 * Constant folding on Phi
2003 * Sub(0,a) -> Minus(a)
2004 * Sub(Mul(a, x), a) -> Mul(a, x-1)
2005 * Sub(Sub(x, y), b) -> Sub(x, Add(y,b))
2007 static ir_node *transform_node_Sub(ir_node *n) {
2012 n = transform_node_AddSub(n);
2014 a = get_Sub_left(n);
2015 b = get_Sub_right(n);
2017 mode = get_irn_mode(n);
2020 HANDLE_BINOP_PHI(tarval_sub, a,b,c);
2022 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
2023 if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
2027 if (mode_wrap_around(mode)) {
2028 ir_node *left = get_Add_left(a);
2029 ir_node *right = get_Add_right(a);
2031 /* FIXME: Does the Conv's word only for two complement or generally? */
2033 if (mode != get_irn_mode(right)) {
2034 /* This Sub is an effective Cast */
2035 right = new_r_Conv(get_irn_irg(n), get_irn_n(n, -1), right, mode);
2038 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2039 } else if (right == b) {
2040 if (mode != get_irn_mode(left)) {
2041 /* This Sub is an effective Cast */
2042 left = new_r_Conv(get_irn_irg(n), get_irn_n(n, -1), left, mode);
2045 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2048 } else if (mode_is_int(mode) && is_Conv(a) && is_Conv(b)) {
2049 ir_mode *mode = get_irn_mode(a);
2051 if (mode == get_irn_mode(b)) {
2057 /* check if it's allowed to skip the conv */
2058 ma = get_irn_mode(a);
2059 mb = get_irn_mode(b);
2061 if (mode_is_reference(ma) && mode_is_reference(mb)) {
2062 /* SubInt(ConvInt(aP), ConvInt(bP)) -> SubInt(aP,bP) */
2064 set_Sub_right(n, b);
2070 /* Beware of Sub(P, P) which cannot be optimized into a simple Minus ... */
2071 else if (mode_is_num(mode) && mode == get_irn_mode(a) && (classify_Const(a) == CNST_NULL)) {
2073 get_irn_dbg_info(n),
2078 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_0_A);
2080 /* do NOT execute this code if reassociation is enabled, it does the inverse! */
2081 else if (get_opt_reassociation() && get_irn_op(a) == op_Mul) {
2082 ir_node *ma = get_Mul_left(a);
2083 ir_node *mb = get_Mul_right(a);
2086 ir_node *blk = get_irn_n(n, -1);
2088 get_irn_dbg_info(n),
2089 current_ir_graph, blk,
2092 get_irn_dbg_info(n),
2093 current_ir_graph, blk,
2095 new_r_Const_long(current_ir_graph, blk, mode, 1),
2098 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MUL_A_X_A);
2099 } else if (mb == b) {
2100 ir_node *blk = get_irn_n(n, -1);
2102 get_irn_dbg_info(n),
2103 current_ir_graph, blk,
2106 get_irn_dbg_info(n),
2107 current_ir_graph, blk,
2109 new_r_Const_long(current_ir_graph, blk, mode, 1),
2112 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MUL_A_X_A);
2114 } else if (get_irn_op(a) == op_Sub) {
2115 ir_node *x = get_Sub_left(a);
2116 ir_node *y = get_Sub_right(a);
2117 ir_node *blk = get_irn_n(n, -1);
2118 ir_mode *m_b = get_irn_mode(b);
2119 ir_mode *m_y = get_irn_mode(y);
2122 /* Determine the right mode for the Add. */
2125 else if (mode_is_reference(m_b))
2127 else if (mode_is_reference(m_y))
2131 * Both modes are different but none is reference,
2132 * happens for instance in SubP(SubP(P, Iu), Is).
2133 * We have two possibilities here: Cast or ignore.
2134 * Currently we ignore this case.
2139 add = new_r_Add(current_ir_graph, blk, y, b, mode);
2142 set_Sub_right(n, add);
2143 DBG_OPT_ALGSIM0(n, n, FS_OPT_SUB_SUB_X_Y_Z);
2146 } /* transform_node_Sub */
2149 * Transform Mul(a,-1) into -a.
2150 * Do constant evaluation of Phi nodes.
2151 * Do architecture dependent optimizations on Mul nodes
2153 static ir_node *transform_node_Mul(ir_node *n) {
2154 ir_node *c, *oldn = n;
2155 ir_node *a = get_Mul_left(n);
2156 ir_node *b = get_Mul_right(n);
2159 HANDLE_BINOP_PHI(tarval_mul, a,b,c);
2161 mode = get_irn_mode(n);
2162 if (mode_is_signed(mode)) {
2165 if (value_of(a) == get_mode_minus_one(mode))
2167 else if (value_of(b) == get_mode_minus_one(mode))
2170 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1), r, mode);
2171 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2175 return arch_dep_replace_mul_with_shifts(n);
2176 } /* transform_node_Mul */
2179 * Transform a Div Node.
2181 static ir_node *transform_node_Div(ir_node *n) {
2182 tarval *tv = value_of(n);
2185 /* BEWARE: it is NOT possible to optimize a/a to 1, as this may cause a exception */
2187 if (tv != tarval_bad) {
2188 value = new_Const(get_tarval_mode(tv), tv);
2190 DBG_OPT_CSTEVAL(n, value);
2191 } else /* Try architecture dependent optimization */
2192 value = arch_dep_replace_div_by_const(n);
2195 /* Turn Div into a tuple (mem, jmp, bad, value) */
2196 ir_node *mem = get_Div_mem(n);
2197 ir_node *blk = get_irn_n(n, -1);
2199 turn_into_tuple(n, pn_Div_max);
2200 set_Tuple_pred(n, pn_Div_M, mem);
2201 set_Tuple_pred(n, pn_Div_X_regular, new_r_Jmp(current_ir_graph, blk));
2202 set_Tuple_pred(n, pn_Div_X_except, new_Bad());
2203 set_Tuple_pred(n, pn_Div_res, value);
2206 } /* transform_node_Div */
2209 * Transform a Mod node.
2211 static ir_node *transform_node_Mod(ir_node *n) {
2212 tarval *tv = value_of(n);
2215 /* BEWARE: it is NOT possible to optimize a%a to 0, as this may cause a exception */
2217 if (tv != tarval_bad) {
2218 value = new_Const(get_tarval_mode(tv), tv);
2220 DBG_OPT_CSTEVAL(n, value);
2221 } else /* Try architecture dependent optimization */
2222 value = arch_dep_replace_mod_by_const(n);
2225 /* Turn Mod into a tuple (mem, jmp, bad, value) */
2226 ir_node *mem = get_Mod_mem(n);
2227 ir_node *blk = get_irn_n(n, -1);
2229 turn_into_tuple(n, pn_Mod_max);
2230 set_Tuple_pred(n, pn_Mod_M, mem);
2231 set_Tuple_pred(n, pn_Mod_X_regular, new_r_Jmp(current_ir_graph, blk));
2232 set_Tuple_pred(n, pn_Mod_X_except, new_Bad());
2233 set_Tuple_pred(n, pn_Mod_res, value);
2236 } /* transform_node_Mod */
2239 * Transform a DivMod node.
2241 static ir_node *transform_node_DivMod(ir_node *n) {
2244 ir_node *a = get_DivMod_left(n);
2245 ir_node *b = get_DivMod_right(n);
2246 ir_mode *mode = get_irn_mode(a);
2247 tarval *ta = value_of(a);
2248 tarval *tb = value_of(b);
2250 if (!(mode_is_int(mode) && mode_is_int(get_irn_mode(b))))
2253 /* BEWARE: it is NOT possible to optimize a/a to 1, as this may cause a exception */
2255 if (tb != tarval_bad) {
2256 if (tb == get_mode_one(get_tarval_mode(tb))) {
2257 b = new_Const (mode, get_mode_null(mode));
2260 DBG_OPT_CSTEVAL(n, b);
2261 } else if (ta != tarval_bad) {
2262 tarval *resa, *resb;
2263 resa = tarval_div (ta, tb);
2264 if (resa == tarval_bad) return n; /* Causes exception!!! Model by replacing through
2265 Jmp for X result!? */
2266 resb = tarval_mod (ta, tb);
2267 if (resb == tarval_bad) return n; /* Causes exception! */
2268 a = new_Const (mode, resa);
2269 b = new_Const (mode, resb);
2272 DBG_OPT_CSTEVAL(n, a);
2273 DBG_OPT_CSTEVAL(n, b);
2274 } else { /* Try architecture dependent optimization */
2275 arch_dep_replace_divmod_by_const(&a, &b, n);
2276 evaluated = a != NULL;
2278 } else if (ta == get_mode_null(mode)) {
2279 /* 0 / non-Const = 0 */
2284 if (evaluated) { /* replace by tuple */
2285 ir_node *mem = get_DivMod_mem(n);
2286 ir_node *blk = get_irn_n(n, -1);
2287 turn_into_tuple(n, pn_DivMod_max);
2288 set_Tuple_pred(n, pn_DivMod_M, mem);
2289 set_Tuple_pred(n, pn_DivMod_X_regular, new_r_Jmp(current_ir_graph, blk));
2290 set_Tuple_pred(n, pn_DivMod_X_except, new_Bad()); /* no exception */
2291 set_Tuple_pred(n, pn_DivMod_res_div, a);
2292 set_Tuple_pred(n, pn_DivMod_res_mod, b);
2296 } /* transform_node_DivMod */
2299 * Optimize Abs(x) into x if x is Confirmed >= 0
2300 * Optimize Abs(x) into -x if x is Confirmed <= 0
2302 static ir_node *transform_node_Abs(ir_node *n) {
2304 ir_node *a = get_Abs_op(n);
2305 value_classify_sign sign = classify_value_sign(a);
2307 if (sign == value_classified_negative) {
2308 ir_mode *mode = get_irn_mode(n);
2311 * We can replace the Abs by -x here.
2312 * We even could add a new Confirm here.
2314 * Note that -x would create a new node, so we could
2315 * not run it in the equivalent_node() context.
2317 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph,
2318 get_irn_n(n, -1), a, mode);
2320 DBG_OPT_CONFIRM(oldn, n);
2321 } else if (sign == value_classified_positive) {
2322 /* n is positive, Abs is not needed */
2325 DBG_OPT_CONFIRM(oldn, n);
2329 } /* transform_node_Abs */
2332 * Transform a Cond node.
2334 * Replace the Cond by a Jmp if it branches on a constant
2337 static ir_node *transform_node_Cond(ir_node *n) {
2340 ir_node *a = get_Cond_selector(n);
2341 tarval *ta = value_of(a);
2343 /* we need block info which is not available in floating irgs */
2344 if (get_irg_pinned(current_ir_graph) == op_pin_state_floats)
2347 if ((ta != tarval_bad) &&
2348 (get_irn_mode(a) == mode_b) &&
2349 (get_opt_unreachable_code())) {
2350 /* It's a boolean Cond, branching on a boolean constant.
2351 Replace it by a tuple (Bad, Jmp) or (Jmp, Bad) */
2352 jmp = new_r_Jmp(current_ir_graph, get_nodes_block(n));
2353 turn_into_tuple(n, pn_Cond_max);
2354 if (ta == tarval_b_true) {
2355 set_Tuple_pred(n, pn_Cond_false, new_Bad());
2356 set_Tuple_pred(n, pn_Cond_true, jmp);
2358 set_Tuple_pred(n, pn_Cond_false, jmp);
2359 set_Tuple_pred(n, pn_Cond_true, new_Bad());
2361 /* We might generate an endless loop, so keep it alive. */
2362 add_End_keepalive(get_irg_end(current_ir_graph), get_nodes_block(n));
2365 } /* transform_node_Cond */
2367 typedef ir_node* (*recursive_transform) (ir_node *n);
2370 * makes use of distributive laws for and, or, eor
2371 * and(a OP c, b OP c) -> and(a, b) OP c
2373 static ir_node *transform_bitwise_distributive(ir_node *n,
2374 recursive_transform trans_func)
2377 ir_node *a = get_binop_left(n);
2378 ir_node *b = get_binop_right(n);
2379 ir_op *op = get_irn_op(a);
2380 ir_op *op_root = get_irn_op(n);
2382 if(op != get_irn_op(b))
2385 if (op == op_Conv) {
2386 ir_node *a_op = get_Conv_op(a);
2387 ir_node *b_op = get_Conv_op(b);
2388 ir_mode *a_mode = get_irn_mode(a_op);
2389 ir_mode *b_mode = get_irn_mode(b_op);
2390 if(a_mode == b_mode && (mode_is_int(a_mode) || a_mode == mode_b)) {
2391 ir_node *blk = get_irn_n(n, -1);
2394 set_binop_left(n, a_op);
2395 set_binop_right(n, b_op);
2396 set_irn_mode(n, a_mode);
2398 n = new_r_Conv(current_ir_graph, blk, n, get_irn_mode(oldn));
2400 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_SHIFT_AND);
2406 /* nothing to gain here */
2410 if (op == op_Shrs || op == op_Shr || op == op_Shl
2411 || op == op_And || op == op_Or || op == op_Eor) {
2412 ir_node *a_left = get_binop_left(a);
2413 ir_node *a_right = get_binop_right(a);
2414 ir_node *b_left = get_binop_left(b);
2415 ir_node *b_right = get_binop_right(b);
2419 if (is_op_commutative(op)) {
2420 if (a_left == b_left) {
2424 } else if(a_left == b_right) {
2428 } else if(a_right == b_left) {
2434 if(a_right == b_right) {
2441 /* (a sop c) & (b sop c) => (a & b) sop c */
2442 ir_node *blk = get_irn_n(n, -1);
2444 ir_node *new_n = exact_copy(n);
2445 set_binop_left(new_n, op1);
2446 set_binop_right(new_n, op2);
2447 new_n = trans_func(new_n);
2449 if(op_root == op_Eor && op == op_Or) {
2450 dbg_info *dbgi = get_irn_dbg_info(n);
2451 ir_graph *irg = current_ir_graph;
2452 ir_mode *mode = get_irn_mode(c);
2454 c = new_rd_Not(dbgi, irg, blk, c, mode);
2455 n = new_rd_And(dbgi, irg, blk, new_n, c, mode);
2458 set_irn_n(n, -1, blk);
2459 set_binop_left(n, new_n);
2460 set_binop_right(n, c);
2464 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_SHIFT_AND);
2475 static ir_node *transform_node_And(ir_node *n) {
2477 ir_node *a = get_And_left(n);
2478 ir_node *b = get_And_right(n);
2480 HANDLE_BINOP_PHI(tarval_and, a,b,c);
2482 n = transform_bitwise_distributive(n, transform_node_And);
2485 } /* transform_node_And */
2490 static ir_node *transform_node_Eor(ir_node *n) {
2491 ir_node *c, *oldn = n;
2492 ir_node *a = get_Eor_left(n);
2493 ir_node *b = get_Eor_right(n);
2494 ir_mode *mode = get_irn_mode(n);
2496 HANDLE_BINOP_PHI(tarval_eor, a,b,c);
2500 n = new_rd_Const(get_irn_dbg_info(n), current_ir_graph, get_irn_n(n, -1),
2501 mode, get_mode_null(mode));
2502 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_A_A);
2503 } else if ((mode == mode_b)
2504 && (get_irn_op(a) == op_Proj)
2505 && (get_irn_mode(a) == mode_b)
2506 && (classify_tarval (value_of(b)) == TV_CLASSIFY_ONE)
2507 && (get_irn_op(get_Proj_pred(a)) == op_Cmp)) {
2508 /* The Eor negates a Cmp. The Cmp has the negated result anyways! */
2509 n = new_r_Proj(current_ir_graph, get_irn_n(n, -1), get_Proj_pred(a),
2510 mode_b, get_negated_pnc(get_Proj_proj(a), mode));
2512 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT_BOOL);
2513 } else if ((mode == mode_b)
2514 && (classify_tarval (value_of(b)) == TV_CLASSIFY_ONE)) {
2515 /* The Eor is a Not. Replace it by a Not. */
2516 /* ????!!!Extend to bitfield 1111111. */
2517 n = new_r_Not(current_ir_graph, get_irn_n(n, -1), a, mode_b);
2519 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
2521 n = transform_bitwise_distributive(n, transform_node_Eor);
2525 } /* transform_node_Eor */
2530 static ir_node *transform_node_Not(ir_node *n) {
2531 ir_node *c, *oldn = n;
2532 ir_node *a = get_Not_op(n);
2533 ir_op *op_a = get_irn_op(a);
2535 HANDLE_UNOP_PHI(tarval_not,a,c);
2537 /* check for a boolean Not */
2538 if ( (get_irn_mode(n) == mode_b)
2539 && (op_a == op_Proj)
2540 && (get_irn_mode(a) == mode_b)
2541 && (get_irn_op(get_Proj_pred(a)) == op_Cmp)) {
2542 /* We negate a Cmp. The Cmp has the negated result anyways! */
2543 n = new_r_Proj(current_ir_graph, get_irn_n(n, -1), get_Proj_pred(a),
2544 mode_b, get_negated_pnc(get_Proj_proj(a), mode_b));
2545 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_CMP);
2548 if (op_a == op_Sub && classify_Const(get_Sub_right(a)) == CNST_ONE) {
2550 ir_node *op = get_Sub_left(a);
2551 ir_node *blk = get_irn_n(n, -1);
2552 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, blk, op, get_irn_mode(n));
2553 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_MINUS_1);
2556 } /* transform_node_Not */
2559 * Transform a Minus.
2563 static ir_node *transform_node_Minus(ir_node *n) {
2564 ir_node *c, *oldn = n;
2565 ir_node *a = get_Minus_op(n);
2567 HANDLE_UNOP_PHI(tarval_neg,a,c);
2571 ir_node *op = get_Not_op(a);
2572 ir_mode *mode = get_irn_mode(op);
2573 tarval *tv = get_mode_one(mode);
2574 ir_node *blk = get_irn_n(n, -1);
2575 ir_node *c = new_r_Const(current_ir_graph, blk, mode, tv);
2576 n = new_rd_Add(get_irn_dbg_info(n), current_ir_graph, blk, op, c, mode);
2577 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_MINUS_NOT);
2581 } /* transform_node_Minus */
2584 * Transform a Cast_type(Const) into a new Const_type
2586 static ir_node *transform_node_Cast(ir_node *n) {
2588 ir_node *pred = get_Cast_op(n);
2589 ir_type *tp = get_irn_type(n);
2591 if (get_irn_op(pred) == op_Const && get_Const_type(pred) != tp) {
2592 n = new_rd_Const_type(NULL, current_ir_graph, get_irn_n(pred, -1), get_irn_mode(pred),
2593 get_Const_tarval(pred), tp);
2594 DBG_OPT_CSTEVAL(oldn, n);
2595 } else if ((get_irn_op(pred) == op_SymConst) && (get_SymConst_value_type(pred) != tp)) {
2596 n = new_rd_SymConst_type(NULL, current_ir_graph, get_irn_n(pred, -1), get_SymConst_symbol(pred),
2597 get_SymConst_kind(pred), tp);
2598 DBG_OPT_CSTEVAL(oldn, n);
2602 } /* transform_node_Cast */
2605 * Transform a Proj(Div) with a non-zero value.
2606 * Removes the exceptions and routes the memory to the NoMem node.
2608 static ir_node *transform_node_Proj_Div(ir_node *proj) {
2609 ir_node *div = get_Proj_pred(proj);
2610 ir_node *b = get_Div_right(div);
2611 ir_node *confirm, *res, *new_mem;
2614 if (value_not_zero(b, &confirm)) {
2615 /* div(x, y) && y != 0 */
2616 proj_nr = get_Proj_proj(proj);
2618 case pn_Div_X_regular:
2619 return new_r_Jmp(current_ir_graph, get_irn_n(div, -1));
2621 case pn_Div_X_except:
2622 /* we found an exception handler, remove it */
2623 DBG_OPT_EXC_REM(proj);
2627 res = get_Div_mem(div);
2628 new_mem = get_irg_no_mem(current_ir_graph);
2631 /* This node can only float up to the Confirm block */
2632 new_mem = new_r_Pin(current_ir_graph, get_nodes_block(confirm), new_mem);
2634 set_irn_pinned(div, op_pin_state_floats);
2635 /* this is a Div without exception, we can remove the memory edge */
2636 set_Div_mem(div, new_mem);
2641 } /* transform_node_Proj_Div */
2644 * Transform a Proj(Mod) with a non-zero value.
2645 * Removes the exceptions and routes the memory to the NoMem node.
2647 static ir_node *transform_node_Proj_Mod(ir_node *proj) {
2648 ir_node *mod = get_Proj_pred(proj);
2649 ir_node *b = get_Mod_right(mod);
2650 ir_node *confirm, *res, *new_mem;
2653 if (value_not_zero(b, &confirm)) {
2654 /* mod(x, y) && y != 0 */
2655 proj_nr = get_Proj_proj(proj);
2659 case pn_Mod_X_regular:
2660 return new_r_Jmp(current_ir_graph, get_irn_n(mod, -1));
2662 case pn_Mod_X_except:
2663 /* we found an exception handler, remove it */
2664 DBG_OPT_EXC_REM(proj);
2668 res = get_Mod_mem(mod);
2669 new_mem = get_irg_no_mem(current_ir_graph);
2672 /* This node can only float up to the Confirm block */
2673 new_mem = new_r_Pin(current_ir_graph, get_nodes_block(confirm), new_mem);
2675 set_irn_pinned(mod, op_pin_state_floats);
2676 /* this is a Mod without exception, we can remove the memory edge */
2677 set_Mod_mem(mod, get_irg_no_mem(current_ir_graph));
2680 if (get_Mod_left(mod) == b) {
2681 /* a % a = 0 if a != 0 */
2682 ir_mode *mode = get_irn_mode(proj);
2683 ir_node *res = new_Const(mode, get_mode_null(mode));
2685 DBG_OPT_CSTEVAL(mod, res);
2691 } /* transform_node_Proj_Mod */
2694 * Transform a Proj(DivMod) with a non-zero value.
2695 * Removes the exceptions and routes the memory to the NoMem node.
2697 static ir_node *transform_node_Proj_DivMod(ir_node *proj) {
2698 ir_node *divmod = get_Proj_pred(proj);
2699 ir_node *b = get_DivMod_right(divmod);
2700 ir_node *confirm, *res, *new_mem;
2703 if (value_not_zero(b, &confirm)) {
2704 /* DivMod(x, y) && y != 0 */
2705 proj_nr = get_Proj_proj(proj);
2709 case pn_DivMod_X_regular:
2710 return new_r_Jmp(current_ir_graph, get_irn_n(divmod, -1));
2712 case pn_DivMod_X_except:
2713 /* we found an exception handler, remove it */
2714 DBG_OPT_EXC_REM(proj);
2718 res = get_DivMod_mem(divmod);
2719 new_mem = get_irg_no_mem(current_ir_graph);
2722 /* This node can only float up to the Confirm block */
2723 new_mem = new_r_Pin(current_ir_graph, get_nodes_block(confirm), new_mem);
2725 set_irn_pinned(divmod, op_pin_state_floats);
2726 /* this is a DivMod without exception, we can remove the memory edge */
2727 set_DivMod_mem(divmod, get_irg_no_mem(current_ir_graph));
2730 case pn_DivMod_res_mod:
2731 if (get_DivMod_left(divmod) == b) {
2732 /* a % a = 0 if a != 0 */
2733 ir_mode *mode = get_irn_mode(proj);
2734 ir_node *res = new_Const(mode, get_mode_null(mode));
2736 DBG_OPT_CSTEVAL(divmod, res);
2742 } /* transform_node_Proj_DivMod */
2745 * Optimizes jump tables (CondIs or CondIu) by removing all impossible cases.
2747 static ir_node *transform_node_Proj_Cond(ir_node *proj) {
2748 if (get_opt_unreachable_code()) {
2749 ir_node *n = get_Proj_pred(proj);
2750 ir_node *b = get_Cond_selector(n);
2752 if (mode_is_int(get_irn_mode(b))) {
2753 tarval *tb = value_of(b);
2755 if (tb != tarval_bad) {
2756 /* we have a constant switch */
2757 long num = get_Proj_proj(proj);
2759 if (num != get_Cond_defaultProj(n)) { /* we cannot optimize default Proj's yet */
2760 if (get_tarval_long(tb) == num) {
2761 /* Do NOT create a jump here, or we will have 2 control flow ops
2762 * in a block. This case is optimized away in optimize_cf(). */
2765 /* this case will NEVER be taken, kill it */
2773 } /* transform_node_Proj_Cond */
2776 * Normalizes and optimizes Cmp nodes.
2778 static ir_node *transform_node_Proj_Cmp(ir_node *proj) {
2779 if (get_opt_reassociation()) {
2780 ir_node *n = get_Proj_pred(proj);
2781 ir_node *left = get_Cmp_left(n);
2782 ir_node *right = get_Cmp_right(n);
2786 ir_mode *mode = NULL;
2787 long proj_nr = get_Proj_proj(proj);
2790 * First step: normalize the compare op
2791 * by placing the constant on the right site
2792 * or moving the lower address node to the left.
2793 * We ignore the case that both are constants
2794 * this case should be optimized away.
2796 if (get_irn_op(right) == op_Const) {
2798 } else if (get_irn_op(left) == op_Const) {
2803 proj_nr = get_inversed_pnc(proj_nr);
2805 } else if (get_irn_idx(left) > get_irn_idx(right)) {
2811 proj_nr = get_inversed_pnc(proj_nr);
2816 * Second step: Try to reduce the magnitude
2817 * of a constant. This may help to generate better code
2818 * later and may help to normalize more compares.
2819 * Of course this is only possible for integer values.
2822 mode = get_irn_mode(c);
2823 tv = get_Const_tarval(c);
2825 if (tv != tarval_bad) {
2826 /* the following optimization is possible on modes without Overflow
2827 * on Unary Minus or on == and !=:
2828 * -a CMP c ==> a swap(CMP) -c
2830 * Beware: for two-complement Overflow may occur, so only == and != can
2831 * be optimized, see this:
2832 * -MININT < 0 =/=> MININT > 0 !!!
2834 if (get_opt_constant_folding() && get_irn_op(left) == op_Minus &&
2835 (!mode_overflow_on_unary_Minus(mode) ||
2836 (mode_is_int(mode) && (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg)))) {
2837 left = get_Minus_op(left);
2838 tv = tarval_neg(tv);
2840 if (tv != tarval_bad) {
2841 proj_nr = get_inversed_pnc(proj_nr);
2846 /* for integer modes, we have more */
2847 if (mode_is_int(mode)) {
2848 /* Ne includes Unordered which is not possible on integers.
2849 * However, frontends often use this wrong, so fix it here */
2850 if (proj_nr & pn_Cmp_Uo) {
2851 proj_nr &= ~pn_Cmp_Uo;
2852 set_Proj_proj(proj, proj_nr);
2855 /* c > 0 : a < c ==> a <= (c-1) a >= c ==> a > (c-1) */
2856 if ((proj_nr == pn_Cmp_Lt || proj_nr == pn_Cmp_Ge) &&
2857 tarval_cmp(tv, get_mode_null(mode)) == pn_Cmp_Gt) {
2858 tv = tarval_sub(tv, get_mode_one(mode));
2860 if (tv != tarval_bad) {
2861 proj_nr ^= pn_Cmp_Eq;
2865 /* c < 0 : a > c ==> a >= (c+1) a <= c ==> a < (c+1) */
2866 else if ((proj_nr == pn_Cmp_Gt || proj_nr == pn_Cmp_Le) &&
2867 tarval_cmp(tv, get_mode_null(mode)) == pn_Cmp_Lt) {
2868 tv = tarval_add(tv, get_mode_one(mode));
2870 if (tv != tarval_bad) {
2871 proj_nr ^= pn_Cmp_Eq;
2876 /* the following reassociations work only for == and != */
2877 if (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) {
2879 /* a-b == 0 ==> a == b, a-b != 0 ==> a != b */
2880 if (classify_tarval(tv) == TV_CLASSIFY_NULL && get_irn_op(left) == op_Sub) {
2881 right = get_Sub_right(left);
2882 left = get_Sub_left(left);
2884 tv = value_of(right);
2885 if (tv != tarval_bad) {
2890 if (tv != tarval_bad) {
2891 ir_op *op = get_irn_op(left);
2893 /* a-c1 == c2 ==> a == c2+c1, a-c1 != c2 ==> a != c2+c1 */
2895 ir_node *c1 = get_Sub_right(left);
2896 tarval *tv2 = value_of(c1);
2898 if (tv2 != tarval_bad) {
2899 tv2 = tarval_add(tv, value_of(c1));
2901 if (tv2 != tarval_bad) {
2902 left = get_Sub_left(left);
2908 /* a+c1 == c2 ==> a == c2-c1, a+c1 != c2 ==> a != c2-c1 */
2909 else if (op == op_Add) {
2910 ir_node *a_l = get_Add_left(left);
2911 ir_node *a_r = get_Add_right(left);
2915 if (get_irn_op(a_l) == op_Const) {
2917 tv2 = value_of(a_l);
2920 tv2 = value_of(a_r);
2923 if (tv2 != tarval_bad) {
2924 tv2 = tarval_sub(tv, tv2);
2926 if (tv2 != tarval_bad) {
2933 /* -a == c ==> a == -c, -a != c ==> a != -c */
2934 else if (op == op_Minus) {
2935 tarval *tv2 = tarval_sub(get_mode_null(mode), tv);
2937 if (tv2 != tarval_bad) {
2938 left = get_Minus_op(left);
2945 /* the following reassociations work only for <= */
2946 else if (proj_nr == pn_Cmp_Le || proj_nr == pn_Cmp_Lt) {
2947 if (tv != tarval_bad) {
2948 ir_op *op = get_irn_op(left);
2950 /* c >= 0 : Abs(a) <= c ==> (unsigned)(a + c) <= 2*c */
2958 * optimization for AND:
2960 * And(x, C) == C ==> And(x, C) != 0
2961 * And(x, C) != C ==> And(X, C) == 0
2963 * if C is a single Bit constant.
2965 if ((proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) &&
2966 (get_irn_op(left) == op_And)) {
2967 if (is_single_bit_tarval(tv)) {
2968 /* check for Constant's match. We have check hare the tarvals,
2969 because our const might be changed */
2970 ir_node *la = get_And_left(left);
2971 ir_node *ra = get_And_right(left);
2972 if ((is_Const(la) && get_Const_tarval(la) == tv) ||
2973 (is_Const(ra) && get_Const_tarval(ra) == tv)) {
2974 /* fine: do the transformation */
2975 tv = get_mode_null(get_tarval_mode(tv));
2976 proj_nr ^= pn_Cmp_Leg;
2981 } /* tarval != bad */
2985 ir_node *block = get_irn_n(n, -1); /* Beware of get_nodes_Block() */
2987 if (changed & 2) /* need a new Const */
2988 right = new_Const(mode, tv);
2990 /* create a new compare */
2991 n = new_rd_Cmp(get_irn_dbg_info(n), current_ir_graph, block,
2994 set_Proj_pred(proj, n);
2995 set_Proj_proj(proj, proj_nr);
2999 } /* transform_node_Proj_Cmp */
3002 * Does all optimizations on nodes that must be done on it's Proj's
3003 * because of creating new nodes.
3005 static ir_node *transform_node_Proj(ir_node *proj) {
3006 ir_node *n = get_Proj_pred(proj);
3008 switch (get_irn_opcode(n)) {
3010 return transform_node_Proj_Div(proj);
3013 return transform_node_Proj_Mod(proj);
3016 return transform_node_Proj_DivMod(proj);
3019 return transform_node_Proj_Cond(proj);
3022 return transform_node_Proj_Cmp(proj);
3025 /* should not happen, but if it does will be optimized away */
3026 return equivalent_node_Proj(proj);
3032 } /* transform_node_Proj */
3035 * Move Confirms down through Phi nodes.
3037 static ir_node *transform_node_Phi(ir_node *phi) {
3039 ir_mode *mode = get_irn_mode(phi);
3041 if (mode_is_reference(mode)) {
3042 n = get_irn_arity(phi);
3044 /* Beware of Phi0 */
3046 ir_node *pred = get_irn_n(phi, 0);
3047 ir_node *bound, *new_Phi, *block, **in;
3050 if (! is_Confirm(pred))
3053 bound = get_Confirm_bound(pred);
3054 pnc = get_Confirm_cmp(pred);
3056 NEW_ARR_A(ir_node *, in, n);
3057 in[0] = get_Confirm_value(pred);
3059 for (i = 1; i < n; ++i) {
3060 pred = get_irn_n(phi, i);
3062 if (! is_Confirm(pred) ||
3063 get_Confirm_bound(pred) != bound ||
3064 get_Confirm_cmp(pred) != pnc)
3066 in[i] = get_Confirm_value(pred);
3068 /* move the Confirm nodes "behind" the Phi */
3069 block = get_irn_n(phi, -1);
3070 new_Phi = new_r_Phi(current_ir_graph, block, n, in, get_irn_mode(phi));
3071 return new_r_Confirm(current_ir_graph, block, new_Phi, bound, pnc);
3075 } /* transform_node_Phi */
3078 * Returns the operands of a commutative bin-op, if one operand is
3079 * a const, it is returned as the second one.
3081 static void get_comm_Binop_Ops(ir_node *binop, ir_node **a, ir_node **c) {
3082 ir_node *op_a = get_binop_left(binop);
3083 ir_node *op_b = get_binop_right(binop);
3085 assert(is_op_commutative(get_irn_op(binop)));
3087 if (get_irn_op(op_a) == op_Const) {
3094 } /* get_comm_Binop_Ops */
3097 * Optimize a Or(And(Or(And(v,c4),c3),c2),c1) pattern if possible.
3098 * Such pattern may arise in bitfield stores.
3100 * value c4 value c4 & c2
3101 * AND c3 AND c1 | c3
3106 static ir_node *transform_node_Or_bf_store(ir_node *or) {
3109 ir_node *and_l, *c3;
3110 ir_node *value, *c4;
3111 ir_node *new_and, *new_const, *block;
3112 ir_mode *mode = get_irn_mode(or);
3114 tarval *tv1, *tv2, *tv3, *tv4, *tv, *n_tv4, *n_tv2;
3116 get_comm_Binop_Ops(or, &and, &c1);
3117 if ((get_irn_op(c1) != op_Const) || (get_irn_op(and) != op_And))
3120 get_comm_Binop_Ops(and, &or_l, &c2);
3121 if ((get_irn_op(c2) != op_Const) || (get_irn_op(or_l) != op_Or))
3124 get_comm_Binop_Ops(or_l, &and_l, &c3);
3125 if ((get_irn_op(c3) != op_Const) || (get_irn_op(and_l) != op_And))
3128 get_comm_Binop_Ops(and_l, &value, &c4);
3129 if (get_irn_op(c4) != op_Const)
3132 /* ok, found the pattern, check for conditions */
3133 assert(mode == get_irn_mode(and));
3134 assert(mode == get_irn_mode(or_l));
3135 assert(mode == get_irn_mode(and_l));
3137 tv1 = get_Const_tarval(c1);
3138 tv2 = get_Const_tarval(c2);
3139 tv3 = get_Const_tarval(c3);
3140 tv4 = get_Const_tarval(c4);
3142 tv = tarval_or(tv4, tv2);
3143 if (classify_tarval(tv) != TV_CLASSIFY_ALL_ONE) {
3144 /* have at least one 0 at the same bit position */
3148 n_tv4 = tarval_not(tv4);
3149 if (tv3 != tarval_and(tv3, n_tv4)) {
3150 /* bit in the or_mask is outside the and_mask */
3154 n_tv2 = tarval_not(tv2);
3155 if (tv1 != tarval_and(tv1, n_tv2)) {
3156 /* bit in the or_mask is outside the and_mask */
3160 /* ok, all conditions met */
3161 block = get_irn_n(or, -1);
3163 new_and = new_r_And(current_ir_graph, block,
3164 value, new_r_Const(current_ir_graph, block, mode, tarval_and(tv4, tv2)), mode);
3166 new_const = new_r_Const(current_ir_graph, block, mode, tarval_or(tv3, tv1));
3168 set_Or_left(or, new_and);
3169 set_Or_right(or, new_const);
3171 /* check for more */
3172 return transform_node_Or_bf_store(or);
3173 } /* transform_node_Or_bf_store */
3176 * Optimize an Or(shl(x, c), shr(x, bits - c)) into a Rot
3178 static ir_node *transform_node_Or_Rot(ir_node *or) {
3179 ir_mode *mode = get_irn_mode(or);
3180 ir_node *shl, *shr, *block;
3181 ir_node *irn, *x, *c1, *c2, *v, *sub, *n;
3184 if (! mode_is_int(mode))
3187 shl = get_binop_left(or);
3188 shr = get_binop_right(or);
3190 if (get_irn_op(shl) == op_Shr) {
3191 if (get_irn_op(shr) != op_Shl)
3197 } else if (get_irn_op(shl) != op_Shl) {
3199 } else if (get_irn_op(shr) != op_Shr) {
3202 x = get_Shl_left(shl);
3203 if (x != get_Shr_left(shr))
3206 c1 = get_Shl_right(shl);
3207 c2 = get_Shr_right(shr);
3208 if (get_irn_op(c1) == op_Const && get_irn_op(c2) == op_Const) {
3209 tv1 = get_Const_tarval(c1);
3210 if (! tarval_is_long(tv1))
3213 tv2 = get_Const_tarval(c2);
3214 if (! tarval_is_long(tv2))
3217 if (get_tarval_long(tv1) + get_tarval_long(tv2)
3218 != get_mode_size_bits(mode))
3221 /* yet, condition met */
3222 block = get_irn_n(or, -1);
3224 n = new_r_Rot(current_ir_graph, block, x, c1, mode);
3226 DBG_OPT_ALGSIM1(or, shl, shr, n, FS_OPT_OR_SHFT_TO_ROT);
3228 } else if (get_irn_op(c1) == op_Sub) {
3232 if (get_Sub_right(sub) != v)
3235 c1 = get_Sub_left(sub);
3236 if (get_irn_op(c1) != op_Const)
3239 tv1 = get_Const_tarval(c1);
3240 if (! tarval_is_long(tv1))
3243 if (get_tarval_long(tv1) != get_mode_size_bits(mode))
3246 /* yet, condition met */
3247 block = get_nodes_block(or);
3249 /* a Rot right is not supported, so use a rot left */
3250 n = new_r_Rot(current_ir_graph, block, x, sub, mode);
3252 DBG_OPT_ALGSIM0(or, n, FS_OPT_OR_SHFT_TO_ROT);
3254 } else if (get_irn_op(c2) == op_Sub) {
3258 c1 = get_Sub_left(sub);
3259 if (get_irn_op(c1) != op_Const)
3262 tv1 = get_Const_tarval(c1);
3263 if (! tarval_is_long(tv1))
3266 if (get_tarval_long(tv1) != get_mode_size_bits(mode))
3269 /* yet, condition met */
3270 block = get_irn_n(or, -1);
3273 n = new_r_Rot(current_ir_graph, block, x, v, mode);
3275 DBG_OPT_ALGSIM0(or, n, FS_OPT_OR_SHFT_TO_ROT);
3280 } /* transform_node_Or_Rot */
3285 static ir_node *transform_node_Or(ir_node *n) {
3286 ir_node *c, *oldn = n;
3287 ir_node *a = get_Or_left(n);
3288 ir_node *b = get_Or_right(n);
3290 HANDLE_BINOP_PHI(tarval_or, a,b,c);
3292 n = transform_node_Or_bf_store(n);
3293 n = transform_node_Or_Rot(n);
3297 n = transform_bitwise_distributive(n, transform_node_Or);
3300 } /* transform_node_Or */
3304 static ir_node *transform_node(ir_node *n);
3307 * Optimize (a >> c1) >> c2), works for Shr, Shrs, Shl.
3309 * Should be moved to reassociation?
3311 static ir_node *transform_node_shift(ir_node *n) {
3312 ir_node *left, *right;
3313 tarval *tv1, *tv2, *res;
3315 int modulo_shf, flag;
3317 left = get_binop_left(n);
3319 /* different operations */
3320 if (get_irn_op(left) != get_irn_op(n))
3323 right = get_binop_right(n);
3324 tv1 = value_of(right);
3325 if (tv1 == tarval_bad)
3328 tv2 = value_of(get_binop_right(left));
3329 if (tv2 == tarval_bad)
3332 res = tarval_add(tv1, tv2);
3334 /* beware: a simple replacement works only, if res < modulo shift */
3335 mode = get_irn_mode(n);
3339 modulo_shf = get_mode_modulo_shift(mode);
3340 if (modulo_shf > 0) {
3341 tarval *modulo = new_tarval_from_long(modulo_shf, get_tarval_mode(res));
3343 if (tarval_cmp(res, modulo) & pn_Cmp_Lt)
3349 /* ok, we can replace it */
3350 ir_node *in[2], *irn, *block = get_irn_n(n, -1);
3352 in[0] = get_binop_left(left);
3353 in[1] = new_r_Const(current_ir_graph, block, get_tarval_mode(res), res);
3355 irn = new_ir_node(NULL, current_ir_graph, block, get_irn_op(n), mode, 2, in);
3357 DBG_OPT_ALGSIM0(n, irn, FS_OPT_REASSOC_SHIFT);
3359 return transform_node(irn);
3362 } /* transform_node_shift */
3367 static ir_node *transform_node_Shr(ir_node *n) {
3368 ir_node *c, *oldn = n;
3369 ir_node *a = get_Shr_left(n);
3370 ir_node *b = get_Shr_right(n);
3372 HANDLE_BINOP_PHI(tarval_shr, a, b, c);
3373 return transform_node_shift(n);
3374 } /* transform_node_Shr */
3379 static ir_node *transform_node_Shrs(ir_node *n) {
3380 ir_node *c, *oldn = n;
3381 ir_node *a = get_Shrs_left(n);
3382 ir_node *b = get_Shrs_right(n);
3384 HANDLE_BINOP_PHI(tarval_shrs, a, b, c);
3385 return transform_node_shift(n);
3386 } /* transform_node_Shrs */
3391 static ir_node *transform_node_Shl(ir_node *n) {
3392 ir_node *c, *oldn = n;
3393 ir_node *a = get_Shl_left(n);
3394 ir_node *b = get_Shl_right(n);
3396 HANDLE_BINOP_PHI(tarval_shl, a, b, c);
3397 return transform_node_shift(n);
3398 } /* transform_node_Shl */
3401 * Remove dead blocks and nodes in dead blocks
3402 * in keep alive list. We do not generate a new End node.
3404 static ir_node *transform_node_End(ir_node *n) {
3405 int i, j, n_keepalives = get_End_n_keepalives(n);
3408 NEW_ARR_A(ir_node *, in, n_keepalives);
3410 for (i = j = 0; i < n_keepalives; ++i) {
3411 ir_node *ka = get_End_keepalive(n, i);
3413 if (! is_Block_dead(ka)) {
3417 } else if (is_irn_pinned_in_irg(ka) && is_Block_dead(get_nodes_block(ka))) {
3420 /* FIXME: beabi need to keep a Proj(M) */
3421 if (is_Phi(ka) || is_irn_keep(ka) || is_Proj(ka))
3424 if (j != n_keepalives)
3425 set_End_keepalives(n, j, in);
3427 } /* transform_node_End */
3430 * Optimize a Mux into some simpler cases.
3432 static ir_node *transform_node_Mux(ir_node *n) {
3433 ir_node *oldn = n, *sel = get_Mux_sel(n);
3434 ir_mode *mode = get_irn_mode(n);
3436 if (get_irn_op(sel) == op_Proj && !mode_honor_signed_zeros(mode)) {
3437 ir_node *cmp = get_Proj_pred(sel);
3438 long proj_nr = get_Proj_proj(sel);
3439 ir_node *f = get_Mux_false(n);
3440 ir_node *t = get_Mux_true(n);
3442 if (get_irn_op(cmp) == op_Cmp && classify_Const(get_Cmp_right(cmp)) == CNST_NULL) {
3443 ir_node *block = get_irn_n(n, -1);
3446 * Note: normalization puts the constant on the right site,
3447 * so we check only one case.
3449 * Note further that these optimization work even for floating point
3450 * with NaN's because -NaN == NaN.
3451 * However, if +0 and -0 is handled differently, we cannot use the first one.
3453 if (get_irn_op(f) == op_Minus &&
3454 get_Minus_op(f) == t &&
3455 get_Cmp_left(cmp) == t) {
3457 if (proj_nr == pn_Cmp_Ge || proj_nr == pn_Cmp_Gt) {
3458 /* Mux(a >=/> 0, -a, a) ==> Abs(a) */
3459 n = new_rd_Abs(get_irn_dbg_info(n),
3463 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_ABS);
3465 } else if (proj_nr == pn_Cmp_Le || proj_nr == pn_Cmp_Lt) {
3466 /* Mux(a <=/< 0, -a, a) ==> Minus(Abs(a)) */
3467 n = new_rd_Abs(get_irn_dbg_info(n),
3471 n = new_rd_Minus(get_irn_dbg_info(n),
3476 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_ABS);
3479 } else if (get_irn_op(t) == op_Minus &&
3480 get_Minus_op(t) == f &&
3481 get_Cmp_left(cmp) == f) {
3483 if (proj_nr == pn_Cmp_Le || proj_nr == pn_Cmp_Lt) {
3484 /* Mux(a <=/< 0, a, -a) ==> Abs(a) */
3485 n = new_rd_Abs(get_irn_dbg_info(n),
3489 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_ABS);
3491 } else if (proj_nr == pn_Cmp_Ge || proj_nr == pn_Cmp_Gt) {
3492 /* Mux(a >=/> 0, a, -a) ==> Minus(Abs(a)) */
3493 n = new_rd_Abs(get_irn_dbg_info(n),
3497 n = new_rd_Minus(get_irn_dbg_info(n),
3502 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_ABS);
3507 if (mode_is_int(mode) && mode_is_signed(mode) &&
3508 get_mode_arithmetic(mode) == irma_twos_complement) {
3509 ir_node *x = get_Cmp_left(cmp);
3511 /* the following optimization works only with signed integer two-complement mode */
3513 if (mode == get_irn_mode(x)) {
3515 * FIXME: this restriction is two rigid, as it would still
3516 * work if mode(x) = Hs and mode == Is, but at least it removes
3519 if ((proj_nr == pn_Cmp_Lt || proj_nr == pn_Cmp_Le) &&
3520 classify_Const(t) == CNST_ALL_ONE &&
3521 classify_Const(f) == CNST_NULL) {
3523 * Mux(x:T </<= 0, 0, -1) -> Shrs(x, sizeof_bits(T) - 1)
3527 n = new_rd_Shrs(get_irn_dbg_info(n),
3528 current_ir_graph, block, x,
3529 new_r_Const_long(current_ir_graph, block, mode_Iu,
3530 get_mode_size_bits(mode) - 1),
3532 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_SHR);
3534 } else if ((proj_nr == pn_Cmp_Gt || proj_nr == pn_Cmp_Ge) &&
3535 classify_Const(t) == CNST_ONE &&
3536 classify_Const(f) == CNST_NULL) {
3538 * Mux(x:T >/>= 0, 0, 1) -> Shr(-x, sizeof_bits(T) - 1)
3542 n = new_rd_Shr(get_irn_dbg_info(n),
3543 current_ir_graph, block,
3544 new_r_Minus(current_ir_graph, block, x, mode),
3545 new_r_Const_long(current_ir_graph, block, mode_Iu,
3546 get_mode_size_bits(mode) - 1),
3548 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_SHR);
3555 return arch_transform_node_Mux(n);
3556 } /* transform_node_Mux */
3559 * Optimize a Psi into some simpler cases.
3561 static ir_node *transform_node_Psi(ir_node *n) {
3563 return transform_node_Mux(n);
3566 } /* transform_node_Psi */
3569 * Tries several [inplace] [optimizing] transformations and returns an
3570 * equivalent node. The difference to equivalent_node() is that these
3571 * transformations _do_ generate new nodes, and thus the old node must
3572 * not be freed even if the equivalent node isn't the old one.
3574 static ir_node *transform_node(ir_node *n) {
3575 if (n->op->ops.transform_node)
3576 n = n->op->ops.transform_node(n);
3578 } /* transform_node */
3581 * Sets the default transform node operation for an ir_op_ops.
3583 * @param code the opcode for the default operation
3584 * @param ops the operations initialized
3589 static ir_op_ops *firm_set_default_transform_node(ir_opcode code, ir_op_ops *ops)
3593 ops->transform_node = transform_node_##a; \
3626 } /* firm_set_default_transform_node */
3629 /* **************** Common Subexpression Elimination **************** */
3631 /** The size of the hash table used, should estimate the number of nodes
3633 #define N_IR_NODES 512
3635 /** Compares the attributes of two Const nodes. */
3636 static int node_cmp_attr_Const(ir_node *a, ir_node *b) {
3637 return (get_Const_tarval(a) != get_Const_tarval(b))
3638 || (get_Const_type(a) != get_Const_type(b));
3639 } /* node_cmp_attr_Const */
3641 /** Compares the attributes of two Proj nodes. */
3642 static int node_cmp_attr_Proj(ir_node *a, ir_node *b) {
3643 return get_irn_proj_attr(a) != get_irn_proj_attr(b);
3644 } /* node_cmp_attr_Proj */
3646 /** Compares the attributes of two Filter nodes. */
3647 static int node_cmp_attr_Filter(ir_node *a, ir_node *b) {
3648 return get_Filter_proj(a) != get_Filter_proj(b);
3649 } /* node_cmp_attr_Filter */
3651 /** Compares the attributes of two Alloc nodes. */
3652 static int node_cmp_attr_Alloc(ir_node *a, ir_node *b) {
3653 const alloc_attr *pa = get_irn_alloc_attr(a);
3654 const alloc_attr *pb = get_irn_alloc_attr(b);
3655 return (pa->where != pb->where) || (pa->type != pb->type);
3656 } /* node_cmp_attr_Alloc */
3658 /** Compares the attributes of two Free nodes. */
3659 static int node_cmp_attr_Free(ir_node *a, ir_node *b) {
3660 const free_attr *pa = get_irn_free_attr(a);
3661 const free_attr *pb = get_irn_free_attr(b);
3662 return (pa->where != pb->where) || (pa->type != pb->type);
3663 } /* node_cmp_attr_Free */
3665 /** Compares the attributes of two SymConst nodes. */
3666 static int node_cmp_attr_SymConst(ir_node *a, ir_node *b) {
3667 const symconst_attr *pa = get_irn_symconst_attr(a);
3668 const symconst_attr *pb = get_irn_symconst_attr(b);
3669 return (pa->num != pb->num)
3670 || (pa->sym.type_p != pb->sym.type_p)
3671 || (pa->tp != pb->tp);
3672 } /* node_cmp_attr_SymConst */
3674 /** Compares the attributes of two Call nodes. */
3675 static int node_cmp_attr_Call(ir_node *a, ir_node *b) {
3676 return (get_irn_call_attr(a) != get_irn_call_attr(b));
3677 } /* node_cmp_attr_Call */
3679 /** Compares the attributes of two Sel nodes. */
3680 static int node_cmp_attr_Sel(ir_node *a, ir_node *b) {
3681 const ir_entity *a_ent = get_Sel_entity(a);
3682 const ir_entity *b_ent = get_Sel_entity(b);
3684 (a_ent->kind != b_ent->kind) ||
3685 (a_ent->name != b_ent->name) ||
3686 (a_ent->owner != b_ent->owner) ||
3687 (a_ent->ld_name != b_ent->ld_name) ||
3688 (a_ent->type != b_ent->type);
3689 } /* node_cmp_attr_Sel */
3691 /** Compares the attributes of two Phi nodes. */
3692 static int node_cmp_attr_Phi(ir_node *a, ir_node *b) {
3693 /* we can only enter this function if both nodes have the same number of inputs,
3694 hence it is enough to check if one of them is a Phi0 */
3696 /* check the Phi0 attribute */
3697 return get_irn_phi0_attr(a) != get_irn_phi0_attr(b);
3700 } /* node_cmp_attr_Phi */
3702 /** Compares the attributes of two Conv nodes. */
3703 static int node_cmp_attr_Conv(ir_node *a, ir_node *b) {
3704 return get_Conv_strict(a) != get_Conv_strict(b);
3705 } /* node_cmp_attr_Conv */
3707 /** Compares the attributes of two Cast nodes. */
3708 static int node_cmp_attr_Cast(ir_node *a, ir_node *b) {
3709 return get_Cast_type(a) != get_Cast_type(b);
3710 } /* node_cmp_attr_Cast */
3712 /** Compares the attributes of two Load nodes. */
3713 static int node_cmp_attr_Load(ir_node *a, ir_node *b) {
3714 if (get_Load_volatility(a) == volatility_is_volatile ||
3715 get_Load_volatility(b) == volatility_is_volatile)
3716 /* NEVER do CSE on volatile Loads */
3719 return get_Load_mode(a) != get_Load_mode(b);
3720 } /* node_cmp_attr_Load */
3722 /** Compares the attributes of two Store nodes. */
3723 static int node_cmp_attr_Store(ir_node *a, ir_node *b) {
3724 /* NEVER do CSE on volatile Stores */
3725 return (get_Store_volatility(a) == volatility_is_volatile ||
3726 get_Store_volatility(b) == volatility_is_volatile);
3727 } /* node_cmp_attr_Store */
3729 /** Compares the attributes of two Confirm nodes. */
3730 static int node_cmp_attr_Confirm(ir_node *a, ir_node *b) {
3731 return (get_Confirm_cmp(a) != get_Confirm_cmp(b));
3732 } /* node_cmp_attr_Confirm */
3734 /** Compares the attributes of two ASM nodes. */
3735 static int node_cmp_attr_ASM(ir_node *a, ir_node *b) {
3737 const ir_asm_constraint *ca;
3738 const ir_asm_constraint *cb;
3741 if (get_ASM_text(a) != get_ASM_text(b))
3744 /* Should we really check the constraints here? Should be better, but is strange. */
3745 n = get_ASM_n_input_constraints(a);
3746 if (n != get_ASM_n_input_constraints(b))
3749 ca = get_ASM_input_constraints(a);
3750 cb = get_ASM_input_constraints(b);
3751 for (i = 0; i < n; ++i) {
3752 if (ca[i].pos != cb[i].pos || ca[i].constraint != cb[i].constraint)
3756 n = get_ASM_n_output_constraints(a);
3757 if (n != get_ASM_n_output_constraints(b))
3760 ca = get_ASM_output_constraints(a);
3761 cb = get_ASM_output_constraints(b);
3762 for (i = 0; i < n; ++i) {
3763 if (ca[i].pos != cb[i].pos || ca[i].constraint != cb[i].constraint)
3767 n = get_ASM_n_clobbers(a);
3768 if (n != get_ASM_n_clobbers(b))
3771 cla = get_ASM_clobbers(a);
3772 clb = get_ASM_clobbers(b);
3773 for (i = 0; i < n; ++i) {
3774 if (cla[i] != clb[i])
3778 } /* node_cmp_attr_ASM */
3781 * Set the default node attribute compare operation for an ir_op_ops.
3783 * @param code the opcode for the default operation
3784 * @param ops the operations initialized
3789 static ir_op_ops *firm_set_default_node_cmp_attr(ir_opcode code, ir_op_ops *ops)
3793 ops->node_cmp_attr = node_cmp_attr_##a; \
3818 } /* firm_set_default_node_cmp_attr */
3821 * Compare function for two nodes in the hash table. Gets two
3822 * nodes as parameters. Returns 0 if the nodes are a cse.
3824 int identities_cmp(const void *elt, const void *key) {
3831 if (a == b) return 0;
3833 if ((get_irn_op(a) != get_irn_op(b)) ||
3834 (get_irn_mode(a) != get_irn_mode(b))) return 1;
3836 /* compare if a's in and b's in are of equal length */
3837 irn_arity_a = get_irn_intra_arity (a);
3838 if (irn_arity_a != get_irn_intra_arity(b))
3841 /* for block-local cse and op_pin_state_pinned nodes: */
3842 if (!get_opt_global_cse() || (get_irn_pinned(a) == op_pin_state_pinned)) {
3843 if (get_irn_intra_n(a, -1) != get_irn_intra_n(b, -1))
3847 /* compare a->in[0..ins] with b->in[0..ins] */
3848 for (i = 0; i < irn_arity_a; i++)
3849 if (get_irn_intra_n(a, i) != get_irn_intra_n(b, i))
3853 * here, we already now that the nodes are identical except their
3856 if (a->op->ops.node_cmp_attr)
3857 return a->op->ops.node_cmp_attr(a, b);
3860 } /* identities_cmp */
3863 * Calculate a hash value of a node.
3865 unsigned ir_node_hash(ir_node *node) {
3869 if (node->op == op_Const) {
3870 /* special value for const, as they only differ in their tarval. */
3871 h = HASH_PTR(node->attr.con.tv);
3872 h = 9*h + HASH_PTR(get_irn_mode(node));
3873 } else if (node->op == op_SymConst) {
3874 /* special value for const, as they only differ in their symbol. */
3875 h = HASH_PTR(node->attr.symc.sym.type_p);
3876 h = 9*h + HASH_PTR(get_irn_mode(node));
3879 /* hash table value = 9*(9*(9*(9*(9*arity+in[0])+in[1])+ ...)+mode)+code */
3880 h = irn_arity = get_irn_intra_arity(node);
3882 /* consider all in nodes... except the block if not a control flow. */
3883 for (i = is_cfop(node) ? -1 : 0; i < irn_arity; i++) {
3884 h = 9*h + HASH_PTR(get_irn_intra_n(node, i));
3888 h = 9*h + HASH_PTR(get_irn_mode(node));
3890 h = 9*h + HASH_PTR(get_irn_op(node));
3894 } /* ir_node_hash */
3896 pset *new_identities(void) {
3897 return new_pset(identities_cmp, N_IR_NODES);
3898 } /* new_identities */
3900 void del_identities(pset *value_table) {
3901 del_pset(value_table);
3902 } /* del_identities */
3905 * Return the canonical node computing the same value as n.
3907 * @param value_table The value table
3908 * @param n The node to lookup
3910 * Looks up the node in a hash table.
3912 * For Const nodes this is performed in the constructor, too. Const
3913 * nodes are extremely time critical because of their frequent use in
3914 * constant string arrays.
3916 static INLINE ir_node *identify(pset *value_table, ir_node *n) {
3919 if (!value_table) return n;
3921 if (get_opt_reassociation()) {
3922 if (is_op_commutative(get_irn_op(n))) {
3923 ir_node *l = get_binop_left(n);
3924 ir_node *r = get_binop_right(n);
3926 /* for commutative operators perform a OP b == b OP a */
3927 if (get_irn_idx(l) > get_irn_idx(r)) {
3928 set_binop_left(n, r);
3929 set_binop_right(n, l);
3934 o = pset_find(value_table, n, ir_node_hash(n));
3943 * During construction we set the op_pin_state_pinned flag in the graph right when the
3944 * optimization is performed. The flag turning on procedure global cse could
3945 * be changed between two allocations. This way we are safe.
3947 static INLINE ir_node *identify_cons(pset *value_table, ir_node *n) {
3950 n = identify(value_table, n);
3951 if (get_irn_n(old, -1) != get_irn_n(n, -1))
3952 set_irg_pinned(current_ir_graph, op_pin_state_floats);
3954 } /* identify_cons */
3957 * Return the canonical node computing the same value as n.
3958 * Looks up the node in a hash table, enters it in the table
3959 * if it isn't there yet.
3961 ir_node *identify_remember(pset *value_table, ir_node *n) {
3964 if (!value_table) return n;
3966 if (get_opt_reassociation()) {
3967 if (is_op_commutative(get_irn_op(n))) {
3968 ir_node *l = get_binop_left(n);
3969 ir_node *r = get_binop_right(n);
3970 int l_idx = get_irn_idx(l);
3971 int r_idx = get_irn_idx(r);
3973 /* For commutative operators perform a OP b == b OP a but keep
3974 constants on the RIGHT side. This helps greatly in some optimizations.
3975 Moreover we use the idx number to make the form deterministic. */
3976 if (is_irn_constlike(l))
3978 if (is_irn_constlike(r))
3980 if (l_idx < r_idx) {
3981 set_binop_left(n, r);
3982 set_binop_right(n, l);
3987 /* lookup or insert in hash table with given hash key. */
3988 o = pset_insert(value_table, n, ir_node_hash(n));
3995 } /* identify_remember */
3997 /* Add a node to the identities value table. */
3998 void add_identities(pset *value_table, ir_node *node) {
3999 if (get_opt_cse() && is_no_Block(node))
4000 identify_remember(value_table, node);
4001 } /* add_identities */
4003 /* Visit each node in the value table of a graph. */
4004 void visit_all_identities(ir_graph *irg, irg_walk_func visit, void *env) {
4006 ir_graph *rem = current_ir_graph;
4008 current_ir_graph = irg;
4009 foreach_pset(irg->value_table, node)
4011 current_ir_graph = rem;
4012 } /* visit_all_identities */
4015 * Garbage in, garbage out. If a node has a dead input, i.e., the
4016 * Bad node is input to the node, return the Bad node.
4018 static INLINE ir_node *gigo(ir_node *node) {
4020 ir_op *op = get_irn_op(node);
4022 /* remove garbage blocks by looking at control flow that leaves the block
4023 and replacing the control flow by Bad. */
4024 if (get_irn_mode(node) == mode_X) {
4025 ir_node *block = get_nodes_block(skip_Proj(node));
4027 /* Don't optimize nodes in immature blocks. */
4028 if (!get_Block_matured(block)) return node;
4029 /* Don't optimize End, may have Bads. */
4030 if (op == op_End) return node;
4032 if (is_Block(block)) {
4033 irn_arity = get_irn_arity(block);
4034 for (i = 0; i < irn_arity; i++) {
4035 if (!is_Bad(get_irn_n(block, i)))
4038 if (i == irn_arity) {
4039 ir_graph *irg = get_irn_irg(block);
4040 /* the start block is never dead */
4041 if (block != get_irg_start_block(irg)
4042 && block != get_irg_end_block(irg))
4048 /* Blocks, Phis and Tuples may have dead inputs, e.g., if one of the
4049 blocks predecessors is dead. */
4050 if (op != op_Block && op != op_Phi && op != op_Tuple) {
4051 irn_arity = get_irn_arity(node);
4054 * Beware: we can only read the block of a non-floating node.
4056 if (is_irn_pinned_in_irg(node) &&
4057 is_Block_dead(get_nodes_block(node)))
4060 for (i = 0; i < irn_arity; i++) {
4061 ir_node *pred = get_irn_n(node, i);
4066 /* Propagating Unknowns here seems to be a bad idea, because
4067 sometimes we need a node as a input and did not want that
4069 However, it might be useful to move this into a later phase
4070 (if you think that optimizing such code is useful). */
4071 if (is_Unknown(pred) && mode_is_data(get_irn_mode(node)))
4072 return new_Unknown(get_irn_mode(node));
4077 /* With this code we violate the agreement that local_optimize
4078 only leaves Bads in Block, Phi and Tuple nodes. */
4079 /* If Block has only Bads as predecessors it's garbage. */
4080 /* If Phi has only Bads as predecessors it's garbage. */
4081 if ((op == op_Block && get_Block_matured(node)) || op == op_Phi) {
4082 irn_arity = get_irn_arity(node);
4083 for (i = 0; i < irn_arity; i++) {
4084 if (!is_Bad(get_irn_n(node, i))) break;
4086 if (i == irn_arity) node = new_Bad();
4093 * These optimizations deallocate nodes from the obstack.
4094 * It can only be called if it is guaranteed that no other nodes
4095 * reference this one, i.e., right after construction of a node.
4097 * @param n The node to optimize
4099 * current_ir_graph must be set to the graph of the node!
4101 ir_node *optimize_node(ir_node *n) {
4104 ir_opcode iro = get_irn_opcode(n);
4106 /* Always optimize Phi nodes: part of the construction. */
4107 if ((!get_opt_optimize()) && (iro != iro_Phi)) return n;
4109 /* constant expression evaluation / constant folding */
4110 if (get_opt_constant_folding()) {
4111 /* neither constants nor Tuple values can be evaluated */
4112 if (iro != iro_Const && (get_irn_mode(n) != mode_T)) {
4113 /* try to evaluate */
4114 tv = computed_value(n);
4115 if (tv != tarval_bad) {
4117 ir_type *old_tp = get_irn_type(n);
4118 int i, arity = get_irn_arity(n);
4122 * Try to recover the type of the new expression.
4124 for (i = 0; i < arity && !old_tp; ++i)
4125 old_tp = get_irn_type(get_irn_n(n, i));
4128 * we MUST copy the node here temporary, because it's still needed
4129 * for DBG_OPT_CSTEVAL
4131 node_size = offsetof(ir_node, attr) + n->op->attr_size;
4132 oldn = alloca(node_size);
4134 memcpy(oldn, n, node_size);
4135 CLONE_ARR_A(ir_node *, oldn->in, n->in);
4137 /* ARG, copy the in array, we need it for statistics */
4138 memcpy(oldn->in, n->in, ARR_LEN(n->in) * sizeof(n->in[0]));
4140 /* note the inplace edges module */
4141 edges_node_deleted(n, current_ir_graph);
4143 /* evaluation was successful -- replace the node. */
4144 irg_kill_node(current_ir_graph, n);
4145 nw = new_Const(get_tarval_mode (tv), tv);
4147 if (old_tp && get_type_mode(old_tp) == get_tarval_mode (tv))
4148 set_Const_type(nw, old_tp);
4149 DBG_OPT_CSTEVAL(oldn, nw);
4155 /* remove unnecessary nodes */
4156 if (get_opt_constant_folding() ||
4157 (iro == iro_Phi) || /* always optimize these nodes. */
4159 (iro == iro_Proj) ||
4160 (iro == iro_Block) ) /* Flags tested local. */
4161 n = equivalent_node(n);
4163 optimize_preds(n); /* do node specific optimizations of nodes predecessors. */
4165 /* Common Subexpression Elimination.
4167 * Checks whether n is already available.
4168 * The block input is used to distinguish different subexpressions. Right
4169 * now all nodes are op_pin_state_pinned to blocks, i.e., the CSE only finds common
4170 * subexpressions within a block.
4173 n = identify_cons(current_ir_graph->value_table, n);
4176 edges_node_deleted(oldn, current_ir_graph);
4178 /* We found an existing, better node, so we can deallocate the old node. */
4179 irg_kill_node(current_ir_graph, oldn);
4183 /* Some more constant expression evaluation that does not allow to
4185 iro = get_irn_opcode(n);
4186 if (get_opt_constant_folding() ||
4187 (iro == iro_Cond) ||
4188 (iro == iro_Proj)) /* Flags tested local. */
4189 n = transform_node(n);
4191 /* Remove nodes with dead (Bad) input.
4192 Run always for transformation induced Bads. */
4195 /* Now we have a legal, useful node. Enter it in hash table for CSE */
4196 if (get_opt_cse() && (get_irn_opcode(n) != iro_Block)) {
4197 n = identify_remember(current_ir_graph->value_table, n);
4201 } /* optimize_node */
4205 * These optimizations never deallocate nodes (in place). This can cause dead
4206 * nodes lying on the obstack. Remove these by a dead node elimination,
4207 * i.e., a copying garbage collection.
4209 ir_node *optimize_in_place_2(ir_node *n) {
4212 ir_opcode iro = get_irn_opcode(n);
4214 if (!get_opt_optimize() && (get_irn_op(n) != op_Phi)) return n;
4216 /* constant expression evaluation / constant folding */
4217 if (get_opt_constant_folding()) {
4218 /* neither constants nor Tuple values can be evaluated */
4219 if (iro != iro_Const && get_irn_mode(n) != mode_T) {
4220 /* try to evaluate */
4221 tv = computed_value(n);
4222 if (tv != tarval_bad) {
4223 /* evaluation was successful -- replace the node. */
4224 ir_type *old_tp = get_irn_type(n);
4225 int i, arity = get_irn_arity(n);
4228 * Try to recover the type of the new expression.
4230 for (i = 0; i < arity && !old_tp; ++i)
4231 old_tp = get_irn_type(get_irn_n(n, i));
4233 n = new_Const(get_tarval_mode(tv), tv);
4235 if (old_tp && get_type_mode(old_tp) == get_tarval_mode(tv))
4236 set_Const_type(n, old_tp);
4238 DBG_OPT_CSTEVAL(oldn, n);
4244 /* remove unnecessary nodes */
4245 if (get_opt_constant_folding() ||
4246 (iro == iro_Phi) || /* always optimize these nodes. */
4247 (iro == iro_Id) || /* ... */
4248 (iro == iro_Proj) || /* ... */
4249 (iro == iro_Block) ) /* Flags tested local. */
4250 n = equivalent_node(n);
4252 optimize_preds(n); /* do node specific optimizations of nodes predecessors. */
4254 /** common subexpression elimination **/
4255 /* Checks whether n is already available. */
4256 /* The block input is used to distinguish different subexpressions. Right
4257 now all nodes are op_pin_state_pinned to blocks, i.e., the cse only finds common
4258 subexpressions within a block. */
4259 if (get_opt_cse()) {
4260 n = identify(current_ir_graph->value_table, n);
4263 /* Some more constant expression evaluation. */
4264 iro = get_irn_opcode(n);
4265 if (get_opt_constant_folding() ||
4266 (iro == iro_Cond) ||
4267 (iro == iro_Proj)) /* Flags tested local. */
4268 n = transform_node(n);
4270 /* Remove nodes with dead (Bad) input.
4271 Run always for transformation induced Bads. */
4274 /* Now we can verify the node, as it has no dead inputs any more. */
4277 /* Now we have a legal, useful node. Enter it in hash table for cse.
4278 Blocks should be unique anyways. (Except the successor of start:
4279 is cse with the start block!) */
4280 if (get_opt_cse() && (get_irn_opcode(n) != iro_Block))
4281 n = identify_remember(current_ir_graph->value_table, n);
4284 } /* optimize_in_place_2 */
4287 * Wrapper for external use, set proper status bits after optimization.
4289 ir_node *optimize_in_place(ir_node *n) {
4290 /* Handle graph state */
4291 assert(get_irg_phase_state(current_ir_graph) != phase_building);
4293 if (get_opt_global_cse())
4294 set_irg_pinned(current_ir_graph, op_pin_state_floats);
4295 if (get_irg_outs_state(current_ir_graph) == outs_consistent)
4296 set_irg_outs_inconsistent(current_ir_graph);
4298 /* FIXME: Maybe we could also test whether optimizing the node can
4299 change the control graph. */
4300 set_irg_doms_inconsistent(current_ir_graph);
4301 return optimize_in_place_2(n);
4302 } /* optimize_in_place */
4305 * Sets the default operation for an ir_ops.
4307 ir_op_ops *firm_set_default_operations(ir_opcode code, ir_op_ops *ops) {
4308 ops = firm_set_default_computed_value(code, ops);
4309 ops = firm_set_default_equivalent_node(code, ops);
4310 ops = firm_set_default_transform_node(code, ops);
4311 ops = firm_set_default_node_cmp_attr(code, ops);
4312 ops = firm_set_default_get_type(code, ops);
4313 ops = firm_set_default_get_type_attr(code, ops);
4314 ops = firm_set_default_get_entity_attr(code, ops);
4317 } /* firm_set_default_operations */