2 * Copyright (C) 1995-2008 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
31 #include "irgraph_t.h"
32 #include "iredges_t.h"
39 #include "dbginfo_t.h"
40 #include "iropt_dbg.h"
45 #include "opt_confirms.h"
46 #include "opt_polymorphy.h"
51 /* Make types visible to allow most efficient access */
55 * Returns the tarval of a Const node or tarval_bad for all other nodes.
57 static tarval *default_value_of(const ir_node *n) {
59 return get_Const_tarval(n); /* might return tarval_bad */
64 value_of_func value_of_ptr = default_value_of;
66 /* * Set a new value_of function. */
67 void set_value_of_func(value_of_func func) {
71 value_of_ptr = default_value_of;
75 * Return the value of a Constant.
77 static tarval *computed_value_Const(const ir_node *n) {
78 return get_Const_tarval(n);
79 } /* computed_value_Const */
82 * Return the value of a 'sizeof', 'alignof' or 'offsetof' SymConst.
84 static tarval *computed_value_SymConst(const ir_node *n) {
88 switch (get_SymConst_kind(n)) {
89 case symconst_type_size:
90 type = get_SymConst_type(n);
91 if (get_type_state(type) == layout_fixed)
92 return new_tarval_from_long(get_type_size_bytes(type), get_irn_mode(n));
94 case symconst_type_align:
95 type = get_SymConst_type(n);
96 if (get_type_state(type) == layout_fixed)
97 return new_tarval_from_long(get_type_alignment_bytes(type), get_irn_mode(n));
99 case symconst_ofs_ent:
100 ent = get_SymConst_entity(n);
101 type = get_entity_owner(ent);
102 if (get_type_state(type) == layout_fixed)
103 return new_tarval_from_long(get_entity_offset(ent), get_irn_mode(n));
109 } /* computed_value_SymConst */
112 * Return the value of an Add.
114 static tarval *computed_value_Add(const ir_node *n) {
115 ir_node *a = get_Add_left(n);
116 ir_node *b = get_Add_right(n);
118 tarval *ta = value_of(a);
119 tarval *tb = value_of(b);
121 if ((ta != tarval_bad) && (tb != tarval_bad))
122 return tarval_add(ta, tb);
125 } /* computed_value_Add */
128 * Return the value of a Sub.
129 * Special case: a - a
131 static tarval *computed_value_Sub(const ir_node *n) {
132 ir_mode *mode = get_irn_mode(n);
133 ir_node *a = get_Sub_left(n);
134 ir_node *b = get_Sub_right(n);
139 if (! mode_is_float(mode)) {
142 return get_mode_null(mode);
148 if ((ta != tarval_bad) && (tb != tarval_bad))
149 return tarval_sub(ta, tb, mode);
152 } /* computed_value_Sub */
155 * Return the value of a Carry.
156 * Special : a op 0, 0 op b
158 static tarval *computed_value_Carry(const ir_node *n) {
159 ir_node *a = get_binop_left(n);
160 ir_node *b = get_binop_right(n);
161 ir_mode *m = get_irn_mode(n);
163 tarval *ta = value_of(a);
164 tarval *tb = value_of(b);
166 if ((ta != tarval_bad) && (tb != tarval_bad)) {
168 return tarval_carry() ? get_mode_one(m) : get_mode_null(m);
170 if (tarval_is_null(ta) || tarval_is_null(tb))
171 return get_mode_null(m);
174 } /* computed_value_Carry */
177 * Return the value of a Borrow.
180 static tarval *computed_value_Borrow(const ir_node *n) {
181 ir_node *a = get_binop_left(n);
182 ir_node *b = get_binop_right(n);
183 ir_mode *m = get_irn_mode(n);
185 tarval *ta = value_of(a);
186 tarval *tb = value_of(b);
188 if ((ta != tarval_bad) && (tb != tarval_bad)) {
189 return tarval_cmp(ta, tb) == pn_Cmp_Lt ? get_mode_one(m) : get_mode_null(m);
190 } else if (tarval_is_null(ta)) {
191 return get_mode_null(m);
194 } /* computed_value_Borrow */
197 * Return the value of an unary Minus.
199 static tarval *computed_value_Minus(const ir_node *n) {
200 ir_node *a = get_Minus_op(n);
201 tarval *ta = value_of(a);
203 if (ta != tarval_bad)
204 return tarval_neg(ta);
207 } /* computed_value_Minus */
210 * Return the value of a Mul.
212 static tarval *computed_value_Mul(const ir_node *n) {
213 ir_node *a = get_Mul_left(n);
214 ir_node *b = get_Mul_right(n);
217 tarval *ta = value_of(a);
218 tarval *tb = value_of(b);
220 mode = get_irn_mode(n);
221 if (mode != get_irn_mode(a)) {
222 /* n * n = 2n bit multiplication */
223 ta = tarval_convert_to(ta, mode);
224 tb = tarval_convert_to(tb, mode);
227 if (ta != tarval_bad && tb != tarval_bad) {
228 return tarval_mul(ta, tb);
230 /* a * 0 != 0 if a == NaN or a == Inf */
231 if (!mode_is_float(mode)) {
232 /* a*0 = 0 or 0*b = 0 */
233 if (ta == get_mode_null(mode))
235 if (tb == get_mode_null(mode))
240 } /* computed_value_Mul */
243 * Return the value of an Abs.
245 static tarval *computed_value_Abs(const ir_node *n) {
246 ir_node *a = get_Abs_op(n);
247 tarval *ta = value_of(a);
249 if (ta != tarval_bad)
250 return tarval_abs(ta);
253 } /* computed_value_Abs */
256 * Return the value of an And.
257 * Special case: a & 0, 0 & b
259 static tarval *computed_value_And(const ir_node *n) {
260 ir_node *a = get_And_left(n);
261 ir_node *b = get_And_right(n);
263 tarval *ta = value_of(a);
264 tarval *tb = value_of(b);
266 if ((ta != tarval_bad) && (tb != tarval_bad)) {
267 return tarval_and (ta, tb);
269 if (tarval_is_null(ta)) return ta;
270 if (tarval_is_null(tb)) return tb;
273 } /* computed_value_And */
276 * Return the value of an Or.
277 * Special case: a | 1...1, 1...1 | b
279 static tarval *computed_value_Or(const ir_node *n) {
280 ir_node *a = get_Or_left(n);
281 ir_node *b = get_Or_right(n);
283 tarval *ta = value_of(a);
284 tarval *tb = value_of(b);
286 if ((ta != tarval_bad) && (tb != tarval_bad)) {
287 return tarval_or (ta, tb);
289 if (tarval_is_all_one(ta)) return ta;
290 if (tarval_is_all_one(tb)) return tb;
293 } /* computed_value_Or */
296 * Return the value of an Eor.
298 static tarval *computed_value_Eor(const ir_node *n) {
299 ir_node *a = get_Eor_left(n);
300 ir_node *b = get_Eor_right(n);
305 return get_mode_null(get_irn_mode(n));
310 if ((ta != tarval_bad) && (tb != tarval_bad)) {
311 return tarval_eor (ta, tb);
314 } /* computed_value_Eor */
317 * Return the value of a Not.
319 static tarval *computed_value_Not(const ir_node *n) {
320 ir_node *a = get_Not_op(n);
321 tarval *ta = value_of(a);
323 if (ta != tarval_bad)
324 return tarval_not(ta);
327 } /* computed_value_Not */
330 * Return the value of a Shl.
332 static tarval *computed_value_Shl(const ir_node *n) {
333 ir_node *a = get_Shl_left(n);
334 ir_node *b = get_Shl_right(n);
336 tarval *ta = value_of(a);
337 tarval *tb = value_of(b);
339 if ((ta != tarval_bad) && (tb != tarval_bad)) {
340 return tarval_shl (ta, tb);
343 } /* computed_value_Shl */
346 * Return the value of a Shr.
348 static tarval *computed_value_Shr(const ir_node *n) {
349 ir_node *a = get_Shr_left(n);
350 ir_node *b = get_Shr_right(n);
352 tarval *ta = value_of(a);
353 tarval *tb = value_of(b);
355 if ((ta != tarval_bad) && (tb != tarval_bad)) {
356 return tarval_shr (ta, tb);
359 } /* computed_value_Shr */
362 * Return the value of a Shrs.
364 static tarval *computed_value_Shrs(const ir_node *n) {
365 ir_node *a = get_Shrs_left(n);
366 ir_node *b = get_Shrs_right(n);
368 tarval *ta = value_of(a);
369 tarval *tb = value_of(b);
371 if ((ta != tarval_bad) && (tb != tarval_bad)) {
372 return tarval_shrs (ta, tb);
375 } /* computed_value_Shrs */
378 * Return the value of a Rotl.
380 static tarval *computed_value_Rotl(const ir_node *n) {
381 ir_node *a = get_Rotl_left(n);
382 ir_node *b = get_Rotl_right(n);
384 tarval *ta = value_of(a);
385 tarval *tb = value_of(b);
387 if ((ta != tarval_bad) && (tb != tarval_bad)) {
388 return tarval_rotl(ta, tb);
391 } /* computed_value_Rotl */
394 * Return the value of a Conv.
396 static tarval *computed_value_Conv(const ir_node *n) {
397 ir_node *a = get_Conv_op(n);
398 tarval *ta = value_of(a);
400 if (ta != tarval_bad)
401 return tarval_convert_to(ta, get_irn_mode(n));
404 } /* computed_value_Conv */
407 * Calculate the value of a Mux: can be evaluated, if the
408 * sel and the right input are known.
410 static tarval *computed_value_Mux(const ir_node *n) {
411 ir_node *sel = get_Mux_sel(n);
412 tarval *ts = value_of(sel);
414 if (ts == get_tarval_b_true()) {
415 ir_node *v = get_Mux_true(n);
418 else if (ts == get_tarval_b_false()) {
419 ir_node *v = get_Mux_false(n);
423 } /* computed_value_Mux */
426 * Calculate the value of a Confirm: can be evaluated,
427 * if it has the form Confirm(x, '=', Const).
429 static tarval *computed_value_Confirm(const ir_node *n) {
431 * Beware: we might produce Phi(Confirm(x == true), Confirm(x == false)).
432 * Do NOT optimize them away (CondEval wants them), so wait until
433 * remove_confirm is activated.
435 if (get_opt_remove_confirm()) {
436 if (get_Confirm_cmp(n) == pn_Cmp_Eq) {
437 tarval *tv = value_of(get_Confirm_bound(n));
438 if (tv != tarval_bad)
442 return value_of(get_Confirm_value(n));
443 } /* computed_value_Confirm */
446 * Return the value of a Proj(Cmp).
448 * This performs a first step of unreachable code elimination.
449 * Proj can not be computed, but folding a Cmp above the Proj here is
450 * not as wasteful as folding a Cmp into a Tuple of 16 Consts of which
452 * There are several case where we can evaluate a Cmp node, see later.
454 static tarval *computed_value_Proj_Cmp(const ir_node *n) {
455 ir_node *a = get_Proj_pred(n);
456 ir_node *aa = get_Cmp_left(a);
457 ir_node *ab = get_Cmp_right(a);
458 long proj_nr = get_Proj_proj(n);
461 * BEWARE: a == a is NOT always True for floating Point values, as
462 * NaN != NaN is defined, so we must check this here.
465 !mode_is_float(get_irn_mode(aa)) || proj_nr == pn_Cmp_Lt || proj_nr == pn_Cmp_Gt)
468 /* This is a trick with the bits used for encoding the Cmp
469 Proj numbers, the following statement is not the same:
470 return new_tarval_from_long (proj_nr == pn_Cmp_Eq, mode_b) */
471 return new_tarval_from_long (proj_nr & pn_Cmp_Eq, mode_b);
474 tarval *taa = value_of(aa);
475 tarval *tab = value_of(ab);
476 ir_mode *mode = get_irn_mode(aa);
479 * The predecessors of Cmp are target values. We can evaluate
482 if ((taa != tarval_bad) && (tab != tarval_bad)) {
483 /* strange checks... */
484 pn_Cmp flags = tarval_cmp(taa, tab);
485 if (flags != pn_Cmp_False) {
486 return new_tarval_from_long (proj_nr & flags, mode_b);
489 /* for integer values, we can check against MIN/MAX */
490 else if (mode_is_int(mode)) {
491 /* MIN <=/> x. This results in true/false. */
492 if (taa == get_mode_min(mode)) {
493 /* a compare with the MIN value */
494 if (proj_nr == pn_Cmp_Le)
495 return get_tarval_b_true();
496 else if (proj_nr == pn_Cmp_Gt)
497 return get_tarval_b_false();
499 /* x >=/< MIN. This results in true/false. */
501 if (tab == get_mode_min(mode)) {
502 /* a compare with the MIN value */
503 if (proj_nr == pn_Cmp_Ge)
504 return get_tarval_b_true();
505 else if (proj_nr == pn_Cmp_Lt)
506 return get_tarval_b_false();
508 /* MAX >=/< x. This results in true/false. */
509 else if (taa == get_mode_max(mode)) {
510 if (proj_nr == pn_Cmp_Ge)
511 return get_tarval_b_true();
512 else if (proj_nr == pn_Cmp_Lt)
513 return get_tarval_b_false();
515 /* x <=/> MAX. This results in true/false. */
516 else if (tab == get_mode_max(mode)) {
517 if (proj_nr == pn_Cmp_Le)
518 return get_tarval_b_true();
519 else if (proj_nr == pn_Cmp_Gt)
520 return get_tarval_b_false();
524 * The predecessors are Allocs or (void*)(0) constants. Allocs never
525 * return NULL, they raise an exception. Therefore we can predict
529 ir_node *aaa = skip_Proj(aa);
530 ir_node *aba = skip_Proj(ab);
532 if ( ( (/* aa is ProjP and aaa is Alloc */
534 && mode_is_reference(get_irn_mode(aa))
536 && ( (/* ab is NULL */
537 mode_is_reference(get_irn_mode(ab))
538 && tarval_is_null(tab))
539 || (/* ab is other Alloc */
541 && mode_is_reference(get_irn_mode(ab))
544 || (/* aa is NULL and aba is Alloc */
545 mode_is_reference(get_irn_mode(aa))
546 && tarval_is_null(taa)
548 && mode_is_reference(get_irn_mode(ab))
551 return new_tarval_from_long(proj_nr & pn_Cmp_Lg, mode_b);
554 return computed_value_Cmp_Confirm(a, aa, ab, proj_nr);
555 } /* computed_value_Proj_Cmp */
558 * Return the value of a floating point Quot.
560 static tarval *do_computed_value_Quot(const ir_node *a, const ir_node *b) {
561 tarval *ta = value_of(a);
562 tarval *tb = value_of(b);
564 /* cannot optimize 0 / b = 0 because of NaN */
565 if (ta != tarval_bad && tb != tarval_bad)
566 return tarval_quo(ta, tb);
568 } /* do_computed_value_Quot */
571 * Calculate the value of an integer Div of two nodes.
572 * Special case: 0 / b
574 static tarval *do_computed_value_Div(const ir_node *a, const ir_node *b) {
575 tarval *ta = value_of(a);
577 const ir_node *dummy;
579 /* Compute c1 / c2 or 0 / a, a != 0 */
580 if (tarval_is_null(ta) && value_not_zero(b, &dummy))
581 return ta; /* 0 / b == 0 */
583 if (ta != tarval_bad && tb != tarval_bad)
584 return tarval_div(ta, tb);
586 } /* do_computed_value_Div */
589 * Calculate the value of an integer Mod of two nodes.
590 * Special case: a % 1
592 static tarval *do_computed_value_Mod(const ir_node *a, const ir_node *b) {
593 tarval *ta = value_of(a);
594 tarval *tb = value_of(b);
596 /* Compute a % 1 or c1 % c2 */
597 if (tarval_is_one(tb))
598 return get_mode_null(get_irn_mode(a));
599 if (ta != tarval_bad && tb != tarval_bad)
600 return tarval_mod(ta, tb);
602 } /* do_computed_value_Mod */
605 * Return the value of a Proj(DivMod).
607 static tarval *computed_value_Proj_DivMod(const ir_node *n) {
608 long proj_nr = get_Proj_proj(n);
610 /* compute either the Div or the Mod part */
611 if (proj_nr == pn_DivMod_res_div) {
612 const ir_node *a = get_Proj_pred(n);
613 return do_computed_value_Div(get_DivMod_left(a), get_DivMod_right(a));
614 } else if (proj_nr == pn_DivMod_res_mod) {
615 const ir_node *a = get_Proj_pred(n);
616 return do_computed_value_Mod(get_DivMod_left(a), get_DivMod_right(a));
619 } /* computed_value_Proj_DivMod */
622 * Return the value of a Proj(Div).
624 static tarval *computed_value_Proj_Div(const ir_node *n) {
625 long proj_nr = get_Proj_proj(n);
627 if (proj_nr == pn_Div_res) {
628 const ir_node *a = get_Proj_pred(n);
629 return do_computed_value_Div(get_Div_left(a), get_Div_right(a));
632 } /* computed_value_Proj_Div */
635 * Return the value of a Proj(Mod).
637 static tarval *computed_value_Proj_Mod(const ir_node *n) {
638 long proj_nr = get_Proj_proj(n);
640 if (proj_nr == pn_Mod_res) {
641 const ir_node *a = get_Proj_pred(n);
642 return do_computed_value_Mod(get_Mod_left(a), get_Mod_right(a));
645 } /* computed_value_Proj_Mod */
648 * Return the value of a Proj(Quot).
650 static tarval *computed_value_Proj_Quot(const ir_node *n) {
651 long proj_nr = get_Proj_proj(n);
653 if (proj_nr == pn_Quot_res) {
654 const ir_node *a = get_Proj_pred(n);
655 return do_computed_value_Quot(get_Quot_left(a), get_Quot_right(a));
658 } /* computed_value_Proj_Quot */
661 * Return the value of a Proj.
663 static tarval *computed_value_Proj(const ir_node *proj) {
664 ir_node *n = get_Proj_pred(proj);
666 if (n->op->ops.computed_value_Proj != NULL)
667 return n->op->ops.computed_value_Proj(proj);
669 } /* computed_value_Proj */
672 * If the parameter n can be computed, return its value, else tarval_bad.
673 * Performs constant folding.
675 * @param n The node this should be evaluated
677 tarval *computed_value(const ir_node *n) {
678 if (n->op->ops.computed_value)
679 return n->op->ops.computed_value(n);
681 } /* computed_value */
684 * Set the default computed_value evaluator in an ir_op_ops.
686 * @param code the opcode for the default operation
687 * @param ops the operations initialized
692 static ir_op_ops *firm_set_default_computed_value(ir_opcode code, ir_op_ops *ops)
696 ops->computed_value = computed_value_##a; \
698 #define CASE_PROJ(a) \
700 ops->computed_value_Proj = computed_value_Proj_##a; \
737 } /* firm_set_default_computed_value */
740 * Returns a equivalent block for another block.
741 * If the block has only one predecessor, this is
742 * the equivalent one. If the only predecessor of a block is
743 * the block itself, this is a dead block.
745 * If both predecessors of a block are the branches of a binary
746 * Cond, the equivalent block is Cond's block.
748 * If all predecessors of a block are bad or lies in a dead
749 * block, the current block is dead as well.
751 * Note, that blocks are NEVER turned into Bad's, instead
752 * the dead_block flag is set. So, never test for is_Bad(block),
753 * always use is_dead_Block(block).
755 static ir_node *equivalent_node_Block(ir_node *n)
760 /* don't optimize dead or labeled blocks */
761 if (is_Block_dead(n) || has_Block_entity(n))
764 n_preds = get_Block_n_cfgpreds(n);
766 /* The Block constructor does not call optimize, but mature_immBlock()
767 calls the optimization. */
768 assert(get_Block_matured(n));
770 /* Straightening: a single entry Block following a single exit Block
771 can be merged, if it is not the Start block. */
772 /* !!! Beware, all Phi-nodes of n must have been optimized away.
773 This should be true, as the block is matured before optimize is called.
774 But what about Phi-cycles with the Phi0/Id that could not be resolved?
775 Remaining Phi nodes are just Ids. */
777 ir_node *pred = skip_Proj(get_Block_cfgpred(n, 0));
780 ir_node *predblock = get_nodes_block(pred);
781 if (predblock == oldn) {
782 /* Jmp jumps into the block it is in -- deal self cycle. */
783 n = set_Block_dead(n);
784 DBG_OPT_DEAD_BLOCK(oldn, n);
785 } else if (get_opt_control_flow_straightening()) {
787 DBG_OPT_STG(oldn, n);
789 } else if (is_Cond(pred)) {
790 ir_node *predblock = get_nodes_block(pred);
791 if (predblock == oldn) {
792 /* Jmp jumps into the block it is in -- deal self cycle. */
793 n = set_Block_dead(n);
794 DBG_OPT_DEAD_BLOCK(oldn, n);
797 } else if ((n_preds == 2) &&
798 (get_opt_control_flow_weak_simplification())) {
799 /* Test whether Cond jumps twice to this block
800 * The more general case which more than 2 predecessors is handles
801 * in optimize_cf(), we handle only this special case for speed here.
803 ir_node *a = get_Block_cfgpred(n, 0);
804 ir_node *b = get_Block_cfgpred(n, 1);
806 if (is_Proj(a) && is_Proj(b)) {
807 ir_node *cond = get_Proj_pred(a);
809 if (cond == get_Proj_pred(b) && is_Cond(cond) &&
810 get_irn_mode(get_Cond_selector(cond)) == mode_b) {
811 /* Also a single entry Block following a single exit Block. Phis have
812 twice the same operand and will be optimized away. */
813 n = get_nodes_block(cond);
814 DBG_OPT_IFSIM1(oldn, a, b, n);
817 } else if (get_opt_unreachable_code() &&
818 (n != get_irg_start_block(current_ir_graph)) &&
819 (n != get_irg_end_block(current_ir_graph)) ) {
822 /* If all inputs are dead, this block is dead too, except if it is
823 the start or end block. This is one step of unreachable code
825 for (i = get_Block_n_cfgpreds(n) - 1; i >= 0; --i) {
826 ir_node *pred = get_Block_cfgpred(n, i);
829 if (is_Bad(pred)) continue;
830 pred_blk = get_nodes_block(skip_Proj(pred));
832 if (is_Block_dead(pred_blk)) continue;
835 /* really found a living input */
840 n = set_Block_dead(n);
841 DBG_OPT_DEAD_BLOCK(oldn, n);
846 } /* equivalent_node_Block */
849 * Returns a equivalent node for a Jmp, a Bad :-)
850 * Of course this only happens if the Block of the Jmp is dead.
852 static ir_node *equivalent_node_Jmp(ir_node *n) {
855 /* unreachable code elimination */
856 if (is_Block_dead(get_nodes_block(n))) {
857 n = get_irg_bad(current_ir_graph);
858 DBG_OPT_DEAD_BLOCK(oldn, n);
861 } /* equivalent_node_Jmp */
863 /** Raise is handled in the same way as Jmp. */
864 #define equivalent_node_Raise equivalent_node_Jmp
867 /* We do not evaluate Cond here as we replace it by a new node, a Jmp.
868 See transform_node_Proj_Cond(). */
871 * Optimize operations that are commutative and have neutral 0,
872 * so a op 0 = 0 op a = a.
874 static ir_node *equivalent_node_neutral_zero(ir_node *n) {
877 ir_node *a = get_binop_left(n);
878 ir_node *b = get_binop_right(n);
883 /* After running compute_node there is only one constant predecessor.
884 Find this predecessors value and remember the other node: */
885 if ((tv = value_of(a)) != tarval_bad) {
887 } else if ((tv = value_of(b)) != tarval_bad) {
892 /* If this predecessors constant value is zero, the operation is
893 * unnecessary. Remove it.
895 * Beware: If n is a Add, the mode of on and n might be different
896 * which happens in this rare construction: NULL + 3.
897 * Then, a Conv would be needed which we cannot include here.
899 if (tarval_is_null(tv) && get_irn_mode(on) == get_irn_mode(n)) {
902 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_0);
906 } /* equivalent_node_neutral_zero */
909 * Eor is commutative and has neutral 0.
911 static ir_node *equivalent_node_Eor(ir_node *n) {
916 n = equivalent_node_neutral_zero(n);
917 if (n != oldn) return n;
920 b = get_Eor_right(n);
923 ir_node *aa = get_Eor_left(a);
924 ir_node *ab = get_Eor_right(a);
927 /* (a ^ b) ^ a -> b */
929 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_EOR_A_B_A);
931 } else if (ab == b) {
932 /* (a ^ b) ^ b -> a */
934 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_EOR_A_B_A);
939 ir_node *ba = get_Eor_left(b);
940 ir_node *bb = get_Eor_right(b);
943 /* a ^ (a ^ b) -> b */
945 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_EOR_A_B_A);
947 } else if (bb == a) {
948 /* a ^ (b ^ a) -> b */
950 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_EOR_A_B_A);
958 * Optimize a - 0 and (a - x) + x (for modes with wrap-around).
960 * The second one looks strange, but this construct
961 * is used heavily in the LCC sources :-).
963 * Beware: The Mode of an Add may be different than the mode of its
964 * predecessors, so we could not return a predecessors in all cases.
966 static ir_node *equivalent_node_Add(ir_node *n) {
968 ir_node *left, *right;
969 ir_mode *mode = get_irn_mode(n);
971 n = equivalent_node_neutral_zero(n);
975 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
976 if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
979 left = get_Add_left(n);
980 right = get_Add_right(n);
983 if (get_Sub_right(left) == right) {
986 n = get_Sub_left(left);
987 if (mode == get_irn_mode(n)) {
988 DBG_OPT_ALGSIM1(oldn, left, right, n, FS_OPT_ADD_SUB);
994 if (get_Sub_right(right) == left) {
997 n = get_Sub_left(right);
998 if (mode == get_irn_mode(n)) {
999 DBG_OPT_ALGSIM1(oldn, left, right, n, FS_OPT_ADD_SUB);
1005 } /* equivalent_node_Add */
1008 * optimize operations that are not commutative but have neutral 0 on left,
1011 static ir_node *equivalent_node_left_zero(ir_node *n) {
1014 ir_node *a = get_binop_left(n);
1015 ir_node *b = get_binop_right(n);
1016 tarval *tb = value_of(b);
1018 if (tarval_is_null(tb)) {
1021 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_0);
1024 } /* equivalent_node_left_zero */
1026 #define equivalent_node_Shl equivalent_node_left_zero
1027 #define equivalent_node_Shr equivalent_node_left_zero
1028 #define equivalent_node_Shrs equivalent_node_left_zero
1029 #define equivalent_node_Rotl equivalent_node_left_zero
1032 * Optimize a - 0 and (a + x) - x (for modes with wrap-around).
1034 * The second one looks strange, but this construct
1035 * is used heavily in the LCC sources :-).
1037 * Beware: The Mode of a Sub may be different than the mode of its
1038 * predecessors, so we could not return a predecessors in all cases.
1040 static ir_node *equivalent_node_Sub(ir_node *n) {
1043 ir_mode *mode = get_irn_mode(n);
1046 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
1047 if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
1050 b = get_Sub_right(n);
1053 /* Beware: modes might be different */
1054 if (tarval_is_null(tb)) {
1055 ir_node *a = get_Sub_left(n);
1056 if (mode == get_irn_mode(a)) {
1059 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_0);
1063 } /* equivalent_node_Sub */
1067 * Optimize an "self-inverse unary op", ie op(op(n)) = n.
1070 * -(-a) == a, but might overflow two times.
1071 * We handle it anyway here but the better way would be a
1072 * flag. This would be needed for Pascal for instance.
1074 static ir_node *equivalent_node_idempotent_unop(ir_node *n) {
1076 ir_node *pred = get_unop_op(n);
1078 /* optimize symmetric unop */
1079 if (get_irn_op(pred) == get_irn_op(n)) {
1080 n = get_unop_op(pred);
1081 DBG_OPT_ALGSIM2(oldn, pred, n, FS_OPT_IDEM_UNARY);
1084 } /* equivalent_node_idempotent_unop */
1086 /** Optimize Not(Not(x)) == x. */
1087 #define equivalent_node_Not equivalent_node_idempotent_unop
1089 /** -(-x) == x ??? Is this possible or can --x raise an
1090 out of bounds exception if min =! max? */
1091 #define equivalent_node_Minus equivalent_node_idempotent_unop
1094 * Optimize a * 1 = 1 * a = a.
1096 static ir_node *equivalent_node_Mul(ir_node *n) {
1098 ir_node *a = get_Mul_left(n);
1100 /* we can handle here only the n * n = n bit cases */
1101 if (get_irn_mode(n) == get_irn_mode(a)) {
1102 ir_node *b = get_Mul_right(n);
1106 * Mul is commutative and has again an other neutral element.
1107 * Constants are place right, so check this case first.
1110 if (tarval_is_one(tv)) {
1112 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
1115 if (tarval_is_one(tv)) {
1117 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
1122 } /* equivalent_node_Mul */
1125 * Use algebraic simplification a | a = a | 0 = 0 | a = a.
1127 static ir_node *equivalent_node_Or(ir_node *n) {
1130 ir_node *a = get_Or_left(n);
1131 ir_node *b = get_Or_right(n);
1135 n = a; /* Or has it's own neutral element */
1136 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_OR);
1139 /* constants are cormalized to right, check this site first */
1141 if (tarval_is_null(tv)) {
1143 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_OR);
1147 if (tarval_is_null(tv)) {
1149 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_OR);
1154 } /* equivalent_node_Or */
1157 * Optimize a & 0b1...1 = 0b1...1 & a = a & a = (a|X) & a = a.
1159 static ir_node *equivalent_node_And(ir_node *n) {
1162 ir_node *a = get_And_left(n);
1163 ir_node *b = get_And_right(n);
1167 n = a; /* And has it's own neutral element */
1168 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_AND);
1171 /* constants are normalized to right, check this site first */
1173 if (tarval_is_all_one(tv)) {
1175 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1178 if (tv != get_tarval_bad()) {
1179 ir_mode *mode = get_irn_mode(n);
1180 if (!mode_is_signed(mode) && is_Conv(a)) {
1181 ir_node *convop = get_Conv_op(a);
1182 ir_mode *convopmode = get_irn_mode(convop);
1183 if (!mode_is_signed(convopmode)) {
1184 if (tarval_is_all_one(tarval_convert_to(tv, convopmode))) {
1185 /* Conv(X) & all_one(mode(X)) = Conv(X) */
1187 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1194 if (tarval_is_all_one(tv)) {
1196 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1200 if (b == get_Or_left(a) || b == get_Or_right(a)) {
1203 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1208 if (a == get_Or_left(b) || a == get_Or_right(b)) {
1211 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1216 } /* equivalent_node_And */
1219 * Try to remove useless Conv's:
1221 static ir_node *equivalent_node_Conv(ir_node *n) {
1223 ir_node *a = get_Conv_op(n);
1225 ir_mode *n_mode = get_irn_mode(n);
1226 ir_mode *a_mode = get_irn_mode(a);
1229 if (n_mode == a_mode) { /* No Conv necessary */
1230 if (get_Conv_strict(n)) {
1233 /* neither Minus nor Abs nor Confirm change the precision,
1234 so we can "look-through" */
1237 p = get_Minus_op(p);
1238 } else if (is_Abs(p)) {
1240 } else if (is_Confirm(p)) {
1241 p = get_Confirm_value(p);
1247 if (is_Conv(p) && get_Conv_strict(p)) {
1248 /* we known already, that a_mode == n_mode, and neither
1249 Abs nor Minus change the mode, so the second Conv
1251 assert(get_irn_mode(p) == n_mode);
1253 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CONV);
1257 ir_node *pred = get_Proj_pred(p);
1258 if (is_Load(pred)) {
1259 /* Loads always return with the exact precision of n_mode */
1260 assert(get_Load_mode(pred) == n_mode);
1262 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CONV);
1265 if (is_Proj(pred) && get_Proj_proj(pred) == pn_Start_T_args) {
1266 pred = get_Proj_pred(pred);
1267 if (is_Start(pred)) {
1268 /* Arguments always return with the exact precision,
1269 as strictConv's are place before Call -- if the
1270 caller was compiled with the same setting.
1271 Otherwise, the semantics is probably still right. */
1272 assert(get_irn_mode(p) == n_mode);
1274 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CONV);
1280 /* special case: the immediate predecessor is also a Conv */
1281 if (! get_Conv_strict(a)) {
1282 /* first one is not strict, kick it */
1284 a_mode = get_irn_mode(a);
1288 /* else both are strict conv, second is superfluous */
1290 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CONV);
1295 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CONV);
1298 } else if (is_Conv(a)) { /* Conv(Conv(b)) */
1299 ir_node *b = get_Conv_op(a);
1300 ir_mode *b_mode = get_irn_mode(b);
1302 if (get_Conv_strict(n) && get_Conv_strict(a)) {
1303 /* both are strict conv */
1304 if (smaller_mode(a_mode, n_mode)) {
1305 /* both are strict, but the first is smaller, so
1306 the second cannot remove more precision, remove the
1308 set_Conv_strict(n, 0);
1311 if (n_mode == b_mode) {
1312 if (! get_Conv_strict(n) && ! get_Conv_strict(a)) {
1313 if (n_mode == mode_b) {
1314 n = b; /* Convb(Conv*(xxxb(...))) == xxxb(...) */
1315 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
1317 } else if (get_mode_arithmetic(n_mode) == get_mode_arithmetic(a_mode)) {
1318 if (values_in_mode(b_mode, a_mode)) {
1319 n = b; /* ConvS(ConvL(xxxS(...))) == xxxS(...) */
1320 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
1325 if (mode_is_int(n_mode) && get_mode_arithmetic(a_mode) == irma_ieee754) {
1326 /* ConvI(ConvF(I)) -> I, iff float mantissa >= int mode */
1327 unsigned int_mantissa = get_mode_size_bits(n_mode) - (mode_is_signed(n_mode) ? 1 : 0);
1328 unsigned float_mantissa = tarval_ieee754_get_mantissa_size(a_mode);
1330 if (float_mantissa >= int_mantissa) {
1332 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
1337 if (smaller_mode(b_mode, a_mode)) {
1338 if (get_Conv_strict(n))
1339 set_Conv_strict(b, 1);
1340 n = b; /* ConvA(ConvB(ConvA(...))) == ConvA(...) */
1341 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
1348 } /* equivalent_node_Conv */
1351 * A Cast may be removed if the type of the previous node
1352 * is already the type of the Cast.
1354 static ir_node *equivalent_node_Cast(ir_node *n) {
1356 ir_node *pred = get_Cast_op(n);
1358 if (get_irn_type(pred) == get_Cast_type(n)) {
1360 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CAST);
1363 } /* equivalent_node_Cast */
1366 * - fold Phi-nodes, iff they have only one predecessor except
1369 static ir_node *equivalent_node_Phi(ir_node *n) {
1374 ir_node *first_val = NULL; /* to shutup gcc */
1376 if (!get_opt_normalize()) return n;
1378 n_preds = get_Phi_n_preds(n);
1380 block = get_nodes_block(n);
1381 if (is_Block_dead(block)) /* Control dead */
1382 return get_irg_bad(current_ir_graph);
1384 if (n_preds == 0) return n; /* Phi of dead Region without predecessors. */
1386 /* Find first non-self-referencing input */
1387 for (i = 0; i < n_preds; ++i) {
1388 first_val = get_Phi_pred(n, i);
1389 if ( (first_val != n) /* not self pointer */
1391 /* BEWARE: when the if is changed to 1, Phi's will ignore it's Bad
1392 * predecessors. Then, Phi nodes in dead code might be removed, causing
1393 * nodes pointing to themself (Add's for instance).
1394 * This is really bad and causes endless recursions in several
1395 * code pathes, so we do NOT optimize such a code.
1396 * This is not that bad as it sounds, optimize_cf() removes bad control flow
1397 * (and bad Phi predecessors), so live code is optimized later.
1399 && (! is_Bad(get_Block_cfgpred(block, i)))
1401 ) { /* value not dead */
1402 break; /* then found first value. */
1407 /* A totally Bad or self-referencing Phi (we didn't break the above loop) */
1408 return get_irg_bad(current_ir_graph);
1411 /* search for rest of inputs, determine if any of these
1412 are non-self-referencing */
1413 while (++i < n_preds) {
1414 ir_node *scnd_val = get_Phi_pred(n, i);
1415 if ( (scnd_val != n)
1416 && (scnd_val != first_val)
1419 && (! is_Bad(get_Block_cfgpred(block, i)))
1427 /* Fold, if no multiple distinct non-self-referencing inputs */
1429 DBG_OPT_PHI(oldn, n);
1432 } /* equivalent_node_Phi */
1435 * Several optimizations:
1436 * - fold Sync-nodes, iff they have only one predecessor except
1439 static ir_node *equivalent_node_Sync(ir_node *n) {
1440 int arity = get_Sync_n_preds(n);
1443 for (i = 0; i < arity;) {
1444 ir_node *pred = get_Sync_pred(n, i);
1447 /* Remove Bad predecessors */
1454 /* Remove duplicate predecessors */
1460 if (get_Sync_pred(n, j) == pred) {
1468 if (arity == 0) return get_irg_bad(current_ir_graph);
1469 if (arity == 1) return get_Sync_pred(n, 0);
1471 } /* equivalent_node_Sync */
1474 * Optimize Proj(Tuple).
1476 static ir_node *equivalent_node_Proj_Tuple(ir_node *proj) {
1477 ir_node *oldn = proj;
1478 ir_node *tuple = get_Proj_pred(proj);
1480 /* Remove the Tuple/Proj combination. */
1481 proj = get_Tuple_pred(tuple, get_Proj_proj(proj));
1482 DBG_OPT_TUPLE(oldn, tuple, proj);
1485 } /* equivalent_node_Proj_Tuple */
1488 * Optimize a / 1 = a.
1490 static ir_node *equivalent_node_Proj_Div(ir_node *proj) {
1491 ir_node *oldn = proj;
1492 ir_node *div = get_Proj_pred(proj);
1493 ir_node *b = get_Div_right(div);
1494 tarval *tb = value_of(b);
1496 /* Div is not commutative. */
1497 if (tarval_is_one(tb)) { /* div(x, 1) == x */
1498 switch (get_Proj_proj(proj)) {
1500 proj = get_Div_mem(div);
1501 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NEUTRAL_1);
1505 proj = get_Div_left(div);
1506 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NEUTRAL_1);
1510 /* we cannot replace the exception Proj's here, this is done in
1511 transform_node_Proj_Div() */
1516 } /* equivalent_node_Proj_Div */
1519 * Optimize a / 1.0 = a.
1521 static ir_node *equivalent_node_Proj_Quot(ir_node *proj) {
1522 ir_node *oldn = proj;
1523 ir_node *quot = get_Proj_pred(proj);
1524 ir_node *b = get_Quot_right(quot);
1525 tarval *tb = value_of(b);
1527 /* Div is not commutative. */
1528 if (tarval_is_one(tb)) { /* Quot(x, 1) == x */
1529 switch (get_Proj_proj(proj)) {
1531 proj = get_Quot_mem(quot);
1532 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NEUTRAL_1);
1536 proj = get_Quot_left(quot);
1537 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NEUTRAL_1);
1541 /* we cannot replace the exception Proj's here, this is done in
1542 transform_node_Proj_Quot() */
1547 } /* equivalent_node_Proj_Quot */
1550 * Optimize a / 1 = a.
1552 static ir_node *equivalent_node_Proj_DivMod(ir_node *proj) {
1553 ir_node *oldn = proj;
1554 ir_node *divmod = get_Proj_pred(proj);
1555 ir_node *b = get_DivMod_right(divmod);
1556 tarval *tb = value_of(b);
1558 /* Div is not commutative. */
1559 if (tarval_is_one(tb)) { /* div(x, 1) == x */
1560 switch (get_Proj_proj(proj)) {
1562 proj = get_DivMod_mem(divmod);
1563 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NEUTRAL_1);
1566 case pn_DivMod_res_div:
1567 proj = get_DivMod_left(divmod);
1568 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NEUTRAL_1);
1572 /* we cannot replace the exception Proj's here, this is done in
1573 transform_node_Proj_DivMod().
1574 Note further that the pn_DivMod_res_div case is handled in
1575 computed_value_Proj(). */
1580 } /* equivalent_node_Proj_DivMod */
1583 * Optimize CopyB(mem, x, x) into a Nop.
1585 static ir_node *equivalent_node_Proj_CopyB(ir_node *proj) {
1586 ir_node *oldn = proj;
1587 ir_node *copyb = get_Proj_pred(proj);
1588 ir_node *a = get_CopyB_dst(copyb);
1589 ir_node *b = get_CopyB_src(copyb);
1592 /* Turn CopyB into a tuple (mem, jmp, bad, bad) */
1593 switch (get_Proj_proj(proj)) {
1594 case pn_CopyB_M_regular:
1595 proj = get_CopyB_mem(copyb);
1596 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NOP);
1599 case pn_CopyB_M_except:
1600 case pn_CopyB_X_except:
1601 DBG_OPT_EXC_REM(proj);
1602 proj = get_irg_bad(current_ir_graph);
1607 } /* equivalent_node_Proj_CopyB */
1610 * Optimize Bounds(idx, idx, upper) into idx.
1612 static ir_node *equivalent_node_Proj_Bound(ir_node *proj) {
1613 ir_node *oldn = proj;
1614 ir_node *bound = get_Proj_pred(proj);
1615 ir_node *idx = get_Bound_index(bound);
1616 ir_node *pred = skip_Proj(idx);
1619 if (idx == get_Bound_lower(bound))
1621 else if (is_Bound(pred)) {
1623 * idx was Bounds checked in the same MacroBlock previously,
1624 * it is still valid if lower <= pred_lower && pred_upper <= upper.
1626 ir_node *lower = get_Bound_lower(bound);
1627 ir_node *upper = get_Bound_upper(bound);
1628 if (get_Bound_lower(pred) == lower &&
1629 get_Bound_upper(pred) == upper &&
1630 get_irn_MacroBlock(bound) == get_irn_MacroBlock(pred)) {
1632 * One could expect that we simply return the previous
1633 * Bound here. However, this would be wrong, as we could
1634 * add an exception Proj to a new location then.
1635 * So, we must turn in into a tuple.
1641 /* Turn Bound into a tuple (mem, jmp, bad, idx) */
1642 switch (get_Proj_proj(proj)) {
1644 DBG_OPT_EXC_REM(proj);
1645 proj = get_Bound_mem(bound);
1647 case pn_Bound_X_except:
1648 DBG_OPT_EXC_REM(proj);
1649 proj = get_irg_bad(current_ir_graph);
1653 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NOP);
1656 /* cannot optimize pn_Bound_X_regular, handled in transform ... */
1661 } /* equivalent_node_Proj_Bound */
1664 * Optimize an Exception Proj(Load) with a non-null address.
1666 static ir_node *equivalent_node_Proj_Load(ir_node *proj) {
1667 if (get_opt_ldst_only_null_ptr_exceptions()) {
1668 if (get_irn_mode(proj) == mode_X) {
1669 ir_node *load = get_Proj_pred(proj);
1671 /* get the Load address */
1672 const ir_node *addr = get_Load_ptr(load);
1673 const ir_node *confirm;
1675 if (value_not_null(addr, &confirm)) {
1676 if (get_Proj_proj(proj) == pn_Load_X_except) {
1677 DBG_OPT_EXC_REM(proj);
1678 return get_irg_bad(current_ir_graph);
1684 } /* equivalent_node_Proj_Load */
1687 * Optimize an Exception Proj(Store) with a non-null address.
1689 static ir_node *equivalent_node_Proj_Store(ir_node *proj) {
1690 if (get_opt_ldst_only_null_ptr_exceptions()) {
1691 if (get_irn_mode(proj) == mode_X) {
1692 ir_node *store = get_Proj_pred(proj);
1694 /* get the load/store address */
1695 const ir_node *addr = get_Store_ptr(store);
1696 const ir_node *confirm;
1698 if (value_not_null(addr, &confirm)) {
1699 if (get_Proj_proj(proj) == pn_Store_X_except) {
1700 DBG_OPT_EXC_REM(proj);
1701 return get_irg_bad(current_ir_graph);
1707 } /* equivalent_node_Proj_Store */
1710 * Does all optimizations on nodes that must be done on it's Proj's
1711 * because of creating new nodes.
1713 static ir_node *equivalent_node_Proj(ir_node *proj) {
1714 ir_node *n = get_Proj_pred(proj);
1716 if (get_irn_mode(proj) == mode_X) {
1717 if (is_Block_dead(get_nodes_block(n))) {
1718 /* Remove dead control flow -- early gigo(). */
1719 return get_irg_bad(current_ir_graph);
1722 if (n->op->ops.equivalent_node_Proj)
1723 return n->op->ops.equivalent_node_Proj(proj);
1725 } /* equivalent_node_Proj */
1730 static ir_node *equivalent_node_Id(ir_node *n) {
1737 DBG_OPT_ID(oldn, n);
1739 } /* equivalent_node_Id */
1744 static ir_node *equivalent_node_Mux(ir_node *n)
1746 ir_node *oldn = n, *sel = get_Mux_sel(n);
1748 tarval *ts = value_of(sel);
1750 /* Mux(true, f, t) == t */
1751 if (ts == tarval_b_true) {
1752 n = get_Mux_true(n);
1753 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_C);
1756 /* Mux(false, f, t) == f */
1757 if (ts == tarval_b_false) {
1758 n = get_Mux_false(n);
1759 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_C);
1762 n_t = get_Mux_true(n);
1763 n_f = get_Mux_false(n);
1765 /* Mux(v, x, T) == x */
1766 if (is_Unknown(n_f)) {
1768 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_EQ);
1771 /* Mux(v, T, x) == x */
1772 if (is_Unknown(n_t)) {
1774 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_EQ);
1778 /* Mux(v, x, x) == x */
1781 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_EQ);
1784 if (is_Proj(sel) && !mode_honor_signed_zeros(get_irn_mode(n))) {
1785 ir_node *cmp = get_Proj_pred(sel);
1786 long proj_nr = get_Proj_proj(sel);
1787 ir_node *f = get_Mux_false(n);
1788 ir_node *t = get_Mux_true(n);
1791 * Note further that these optimization work even for floating point
1792 * with NaN's because -NaN == NaN.
1793 * However, if +0 and -0 is handled differently, we cannot use the first one.
1796 ir_node *const cmp_l = get_Cmp_left(cmp);
1797 ir_node *const cmp_r = get_Cmp_right(cmp);
1801 if ((cmp_l == t && cmp_r == f) || /* Mux(t == f, t, f) -> f */
1802 (cmp_l == f && cmp_r == t)) { /* Mux(f == t, t, f) -> f */
1804 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1811 if ((cmp_l == t && cmp_r == f) || /* Mux(t != f, t, f) -> t */
1812 (cmp_l == f && cmp_r == t)) { /* Mux(f != t, t, f) -> t */
1814 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1821 * Note: normalization puts the constant on the right side,
1822 * so we check only one case.
1824 if (cmp_l == t && tarval_is_null(value_of(cmp_r))) {
1825 /* Mux(t CMP 0, X, t) */
1826 if (is_Minus(f) && get_Minus_op(f) == t) {
1827 /* Mux(t CMP 0, -t, t) */
1828 if (proj_nr == pn_Cmp_Eq) {
1829 /* Mux(t == 0, -t, t) ==> -t */
1831 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1832 } else if (proj_nr == pn_Cmp_Lg || proj_nr == pn_Cmp_Ne) {
1833 /* Mux(t != 0, -t, t) ==> t */
1835 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1842 } /* equivalent_node_Mux */
1845 * Remove Confirm nodes if setting is on.
1846 * Replace Confirms(x, '=', Constlike) by Constlike.
1848 static ir_node *equivalent_node_Confirm(ir_node *n) {
1849 ir_node *pred = get_Confirm_value(n);
1850 pn_Cmp pnc = get_Confirm_cmp(n);
1852 while (is_Confirm(pred) && pnc == get_Confirm_cmp(pred)) {
1854 * rare case: two identical Confirms one after another,
1855 * replace the second one with the first.
1858 pred = get_Confirm_value(n);
1859 pnc = get_Confirm_cmp(n);
1861 if (get_opt_remove_confirm())
1862 return get_Confirm_value(n);
1867 * equivalent_node() returns a node equivalent to input n. It skips all nodes that
1868 * perform no actual computation, as, e.g., the Id nodes. It does not create
1869 * new nodes. It is therefore safe to free n if the node returned is not n.
1870 * If a node returns a Tuple we can not just skip it. If the size of the
1871 * in array fits, we transform n into a tuple (e.g., Div).
1873 ir_node *equivalent_node(ir_node *n) {
1874 if (n->op->ops.equivalent_node)
1875 return n->op->ops.equivalent_node(n);
1877 } /* equivalent_node */
1880 * Sets the default equivalent node operation for an ir_op_ops.
1882 * @param code the opcode for the default operation
1883 * @param ops the operations initialized
1888 static ir_op_ops *firm_set_default_equivalent_node(ir_opcode code, ir_op_ops *ops)
1892 ops->equivalent_node = equivalent_node_##a; \
1894 #define CASE_PROJ(a) \
1896 ops->equivalent_node_Proj = equivalent_node_Proj_##a; \
1938 } /* firm_set_default_equivalent_node */
1941 * Returns non-zero if a node is a Phi node
1942 * with all predecessors constant.
1944 static int is_const_Phi(ir_node *n) {
1947 if (! is_Phi(n) || get_irn_arity(n) == 0)
1949 for (i = get_irn_arity(n) - 1; i >= 0; --i) {
1950 if (! is_Const(get_irn_n(n, i)))
1954 } /* is_const_Phi */
1956 typedef tarval *(*tarval_sub_type)(tarval *a, tarval *b, ir_mode *mode);
1957 typedef tarval *(*tarval_binop_type)(tarval *a, tarval *b);
1960 * Wrapper for the tarval binop evaluation, tarval_sub has one more parameter.
1962 static tarval *do_eval(tarval *(*eval)(), tarval *a, tarval *b, ir_mode *mode) {
1963 if (eval == tarval_sub) {
1964 tarval_sub_type func = (tarval_sub_type)eval;
1966 return func(a, b, mode);
1968 tarval_binop_type func = (tarval_binop_type)eval;
1975 * Apply an evaluator on a binop with a constant operators (and one Phi).
1977 * @param phi the Phi node
1978 * @param other the other operand
1979 * @param eval an evaluator function
1980 * @param mode the mode of the result, may be different from the mode of the Phi!
1981 * @param left if non-zero, other is the left operand, else the right
1983 * @return a new Phi node if the conversion was successful, NULL else
1985 static ir_node *apply_binop_on_phi(ir_node *phi, tarval *other, tarval *(*eval)(), ir_mode *mode, int left) {
1990 int i, n = get_irn_arity(phi);
1992 NEW_ARR_A(void *, res, n);
1994 for (i = 0; i < n; ++i) {
1995 pred = get_irn_n(phi, i);
1996 tv = get_Const_tarval(pred);
1997 tv = do_eval(eval, other, tv, mode);
1999 if (tv == tarval_bad) {
2000 /* folding failed, bad */
2006 for (i = 0; i < n; ++i) {
2007 pred = get_irn_n(phi, i);
2008 tv = get_Const_tarval(pred);
2009 tv = do_eval(eval, tv, other, mode);
2011 if (tv == tarval_bad) {
2012 /* folding failed, bad */
2018 irg = current_ir_graph;
2019 for (i = 0; i < n; ++i) {
2020 pred = get_irn_n(phi, i);
2021 res[i] = new_r_Const_type(irg, res[i], get_Const_type(pred));
2023 return new_r_Phi(irg, get_nodes_block(phi), n, (ir_node **)res, mode);
2024 } /* apply_binop_on_phi */
2027 * Apply an evaluator on a binop with two constant Phi.
2029 * @param a the left Phi node
2030 * @param b the right Phi node
2031 * @param eval an evaluator function
2032 * @param mode the mode of the result, may be different from the mode of the Phi!
2034 * @return a new Phi node if the conversion was successful, NULL else
2036 static ir_node *apply_binop_on_2_phis(ir_node *a, ir_node *b, tarval *(*eval)(), ir_mode *mode) {
2037 tarval *tv_l, *tv_r, *tv;
2043 if (get_nodes_block(a) != get_nodes_block(b))
2046 n = get_irn_arity(a);
2047 NEW_ARR_A(void *, res, n);
2049 for (i = 0; i < n; ++i) {
2050 pred = get_irn_n(a, i);
2051 tv_l = get_Const_tarval(pred);
2052 pred = get_irn_n(b, i);
2053 tv_r = get_Const_tarval(pred);
2054 tv = do_eval(eval, tv_l, tv_r, mode);
2056 if (tv == tarval_bad) {
2057 /* folding failed, bad */
2062 irg = current_ir_graph;
2063 for (i = 0; i < n; ++i) {
2064 pred = get_irn_n(a, i);
2065 res[i] = new_r_Const_type(irg, res[i], get_Const_type(pred));
2067 return new_r_Phi(irg, get_nodes_block(a), n, (ir_node **)res, mode);
2068 } /* apply_binop_on_2_phis */
2071 * Apply an evaluator on a unop with a constant operator (a Phi).
2073 * @param phi the Phi node
2074 * @param eval an evaluator function
2076 * @return a new Phi node if the conversion was successful, NULL else
2078 static ir_node *apply_unop_on_phi(ir_node *phi, tarval *(*eval)(tarval *)) {
2084 int i, n = get_irn_arity(phi);
2086 NEW_ARR_A(void *, res, n);
2087 for (i = 0; i < n; ++i) {
2088 pred = get_irn_n(phi, i);
2089 tv = get_Const_tarval(pred);
2092 if (tv == tarval_bad) {
2093 /* folding failed, bad */
2098 mode = get_irn_mode(phi);
2099 irg = current_ir_graph;
2100 for (i = 0; i < n; ++i) {
2101 pred = get_irn_n(phi, i);
2102 res[i] = new_r_Const_type(irg, res[i], get_Const_type(pred));
2104 return new_r_Phi(irg, get_nodes_block(phi), n, (ir_node **)res, mode);
2105 } /* apply_unop_on_phi */
2108 * Apply a conversion on a constant operator (a Phi).
2110 * @param phi the Phi node
2112 * @return a new Phi node if the conversion was successful, NULL else
2114 static ir_node *apply_conv_on_phi(ir_node *phi, ir_mode *mode) {
2119 int i, n = get_irn_arity(phi);
2121 NEW_ARR_A(void *, res, n);
2122 for (i = 0; i < n; ++i) {
2123 pred = get_irn_n(phi, i);
2124 tv = get_Const_tarval(pred);
2125 tv = tarval_convert_to(tv, mode);
2127 if (tv == tarval_bad) {
2128 /* folding failed, bad */
2133 irg = current_ir_graph;
2134 for (i = 0; i < n; ++i) {
2135 pred = get_irn_n(phi, i);
2136 res[i] = new_r_Const_type(irg, res[i], get_Const_type(pred));
2138 return new_r_Phi(irg, get_nodes_block(phi), n, (ir_node **)res, mode);
2139 } /* apply_conv_on_phi */
2142 * Transform AddP(P, ConvIs(Iu)), AddP(P, ConvIu(Is)) and
2143 * SubP(P, ConvIs(Iu)), SubP(P, ConvIu(Is)).
2144 * If possible, remove the Conv's.
2146 static ir_node *transform_node_AddSub(ir_node *n) {
2147 ir_mode *mode = get_irn_mode(n);
2149 if (mode_is_reference(mode)) {
2150 ir_node *left = get_binop_left(n);
2151 ir_node *right = get_binop_right(n);
2152 unsigned ref_bits = get_mode_size_bits(mode);
2154 if (is_Conv(left)) {
2155 ir_mode *lmode = get_irn_mode(left);
2156 unsigned bits = get_mode_size_bits(lmode);
2158 if (ref_bits == bits &&
2159 mode_is_int(lmode) &&
2160 get_mode_arithmetic(lmode) == irma_twos_complement) {
2161 ir_node *pre = get_Conv_op(left);
2162 ir_mode *pre_mode = get_irn_mode(pre);
2164 if (mode_is_int(pre_mode) &&
2165 get_mode_size_bits(pre_mode) == bits &&
2166 get_mode_arithmetic(pre_mode) == irma_twos_complement) {
2167 /* ok, this conv just changes to sign, moreover the calculation
2168 * is done with same number of bits as our address mode, so
2169 * we can ignore the conv as address calculation can be viewed
2170 * as either signed or unsigned
2172 set_binop_left(n, pre);
2177 if (is_Conv(right)) {
2178 ir_mode *rmode = get_irn_mode(right);
2179 unsigned bits = get_mode_size_bits(rmode);
2181 if (ref_bits == bits &&
2182 mode_is_int(rmode) &&
2183 get_mode_arithmetic(rmode) == irma_twos_complement) {
2184 ir_node *pre = get_Conv_op(right);
2185 ir_mode *pre_mode = get_irn_mode(pre);
2187 if (mode_is_int(pre_mode) &&
2188 get_mode_size_bits(pre_mode) == bits &&
2189 get_mode_arithmetic(pre_mode) == irma_twos_complement) {
2190 /* ok, this conv just changes to sign, moreover the calculation
2191 * is done with same number of bits as our address mode, so
2192 * we can ignore the conv as address calculation can be viewed
2193 * as either signed or unsigned
2195 set_binop_right(n, pre);
2200 /* let address arithmetic use unsigned modes */
2201 if (is_Const(right)) {
2202 ir_mode *rmode = get_irn_mode(right);
2204 if (mode_is_signed(rmode) && get_mode_arithmetic(rmode) == irma_twos_complement) {
2205 /* convert a AddP(P, *s) into AddP(P, *u) */
2206 ir_mode *nm = get_reference_mode_unsigned_eq(mode);
2208 ir_node *pre = new_r_Conv(current_ir_graph, get_nodes_block(n), right, nm);
2209 set_binop_right(n, pre);
2215 } /* transform_node_AddSub */
2217 #define HANDLE_BINOP_PHI(eval, a, b, c, mode) \
2219 if (is_Const(b) && is_const_Phi(a)) { \
2220 /* check for Op(Phi, Const) */ \
2221 c = apply_binop_on_phi(a, get_Const_tarval(b), eval, mode, 0);\
2223 else if (is_Const(a) && is_const_Phi(b)) { \
2224 /* check for Op(Const, Phi) */ \
2225 c = apply_binop_on_phi(b, get_Const_tarval(a), eval, mode, 1);\
2227 else if (is_const_Phi(a) && is_const_Phi(b)) { \
2228 /* check for Op(Phi, Phi) */ \
2229 c = apply_binop_on_2_phis(a, b, eval, mode); \
2232 DBG_OPT_ALGSIM0(oldn, c, FS_OPT_CONST_PHI); \
2236 #define HANDLE_UNOP_PHI(eval, a, c) \
2238 if (is_const_Phi(a)) { \
2239 /* check for Op(Phi) */ \
2240 c = apply_unop_on_phi(a, eval); \
2242 DBG_OPT_ALGSIM0(oldn, c, FS_OPT_CONST_PHI); \
2248 * Do the AddSub optimization, then Transform
2249 * Constant folding on Phi
2250 * Add(a,a) -> Mul(a, 2)
2251 * Add(Mul(a, x), a) -> Mul(a, x+1)
2252 * if the mode is integer or float.
2253 * Transform Add(a,-b) into Sub(a,b).
2254 * Reassociation might fold this further.
2256 static ir_node *transform_node_Add(ir_node *n) {
2258 ir_node *a, *b, *c, *oldn = n;
2260 n = transform_node_AddSub(n);
2262 a = get_Add_left(n);
2263 b = get_Add_right(n);
2265 mode = get_irn_mode(n);
2267 if (mode_is_reference(mode)) {
2268 ir_mode *lmode = get_irn_mode(a);
2270 if (is_Const(b) && is_Const_null(b) && mode_is_int(lmode)) {
2271 /* an Add(a, NULL) is a hidden Conv */
2272 dbg_info *dbg = get_irn_dbg_info(n);
2273 return new_rd_Conv(dbg, current_ir_graph, get_nodes_block(n), a, mode);
2277 HANDLE_BINOP_PHI(tarval_add, a, b, c, mode);
2279 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
2280 if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
2283 if (mode_is_num(mode)) {
2284 /* the following code leads to endless recursion when Mul are replaced by a simple instruction chain */
2285 if (!is_irg_state(current_ir_graph, IR_GRAPH_STATE_ARCH_DEP)
2286 && a == b && mode_is_int(mode)) {
2287 ir_node *block = get_nodes_block(n);
2290 get_irn_dbg_info(n),
2294 new_Const_long(mode, 2),
2296 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_A);
2301 get_irn_dbg_info(n),
2307 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_MINUS_B);
2312 get_irn_dbg_info(n),
2318 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_MINUS_B);
2321 if (get_mode_arithmetic(mode) == irma_twos_complement) {
2322 /* Here we rely on constants be on the RIGHT side */
2324 ir_node *op = get_Not_op(a);
2326 if (is_Const(b) && is_Const_one(b)) {
2328 ir_node *blk = get_nodes_block(n);
2329 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, blk, op, mode);
2330 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_PLUS_1);
2335 n = new_Const(get_mode_minus_one(mode));
2336 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_X_NOT_X);
2341 ir_node *op = get_Not_op(b);
2345 n = new_Const(get_mode_minus_one(mode));
2346 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_X_NOT_X);
2353 } /* transform_node_Add */
2356 * returns -cnst or NULL if impossible
2358 static ir_node *const_negate(ir_node *cnst) {
2359 tarval *tv = tarval_neg(get_Const_tarval(cnst));
2360 dbg_info *dbgi = get_irn_dbg_info(cnst);
2361 ir_graph *irg = get_irn_irg(cnst);
2362 if (tv == tarval_bad) return NULL;
2363 return new_rd_Const(dbgi, irg, tv);
2367 * Do the AddSub optimization, then Transform
2368 * Constant folding on Phi
2369 * Sub(0,a) -> Minus(a)
2370 * Sub(Mul(a, x), a) -> Mul(a, x-1)
2371 * Sub(Sub(x, y), b) -> Sub(x, Add(y,b))
2372 * Sub(Add(a, x), x) -> a
2373 * Sub(x, Add(x, a)) -> -a
2374 * Sub(x, Const) -> Add(x, -Const)
2376 static ir_node *transform_node_Sub(ir_node *n) {
2381 n = transform_node_AddSub(n);
2383 a = get_Sub_left(n);
2384 b = get_Sub_right(n);
2386 mode = get_irn_mode(n);
2388 if (mode_is_int(mode)) {
2389 ir_mode *lmode = get_irn_mode(a);
2391 if (is_Const(b) && is_Const_null(b) && mode_is_reference(lmode)) {
2392 /* a Sub(a, NULL) is a hidden Conv */
2393 dbg_info *dbg = get_irn_dbg_info(n);
2394 n = new_rd_Conv(dbg, current_ir_graph, get_nodes_block(n), a, mode);
2395 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_CONV);
2399 if (mode == lmode &&
2400 get_mode_arithmetic(mode) == irma_twos_complement &&
2402 get_Const_tarval(a) == get_mode_minus_one(mode)) {
2404 dbg_info *dbg = get_irn_dbg_info(n);
2405 n = new_rd_Not(dbg, current_ir_graph, get_nodes_block(n), b, mode);
2406 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_NOT);
2412 HANDLE_BINOP_PHI(tarval_sub, a, b, c, mode);
2414 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
2415 if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
2418 if (is_Const(b) && !mode_is_reference(get_irn_mode(b))) {
2419 /* a - C -> a + (-C) */
2420 ir_node *cnst = const_negate(b);
2422 ir_node *block = get_nodes_block(n);
2423 dbg_info *dbgi = get_irn_dbg_info(n);
2424 ir_graph *irg = get_irn_irg(n);
2426 n = new_rd_Add(dbgi, irg, block, a, cnst, mode);
2427 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2432 if (is_Minus(a)) { /* (-a) - b -> -(a + b) */
2433 ir_graph *irg = current_ir_graph;
2434 dbg_info *dbg = get_irn_dbg_info(n);
2435 ir_node *block = get_nodes_block(n);
2436 ir_node *left = get_Minus_op(a);
2437 ir_node *add = new_rd_Add(dbg, irg, block, left, b, mode);
2439 n = new_rd_Minus(dbg, irg, block, add, mode);
2440 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2442 } else if (is_Minus(b)) { /* a - (-b) -> a + b */
2443 ir_graph *irg = current_ir_graph;
2444 dbg_info *dbg = get_irn_dbg_info(n);
2445 ir_node *block = get_nodes_block(n);
2446 ir_node *right = get_Minus_op(b);
2448 n = new_rd_Add(dbg, irg, block, a, right, mode);
2449 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MINUS);
2451 } else if (is_Sub(b)) {
2452 /* a - (b - c) -> a + (c - b)
2453 * -> (a - b) + c iff (b - c) is a pointer */
2454 ir_graph *irg = current_ir_graph;
2455 dbg_info *s_dbg = get_irn_dbg_info(b);
2456 ir_node *s_block = get_nodes_block(b);
2457 ir_node *s_left = get_Sub_left(b);
2458 ir_node *s_right = get_Sub_right(b);
2459 ir_mode *s_mode = get_irn_mode(b);
2460 if (mode_is_reference(s_mode)) {
2461 ir_node *sub = new_rd_Sub(s_dbg, irg, s_block, a, s_left, mode);
2462 dbg_info *a_dbg = get_irn_dbg_info(n);
2463 ir_node *a_block = get_nodes_block(n);
2466 s_right = new_r_Conv(irg, a_block, s_right, mode);
2467 n = new_rd_Add(a_dbg, irg, a_block, sub, s_right, mode);
2469 ir_node *sub = new_rd_Sub(s_dbg, irg, s_block, s_right, s_left, s_mode);
2470 dbg_info *a_dbg = get_irn_dbg_info(n);
2471 ir_node *a_block = get_nodes_block(n);
2473 n = new_rd_Add(a_dbg, irg, a_block, a, sub, mode);
2475 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2477 } else if (is_Mul(b)) { /* a - (b * C) -> a + (b * -C) */
2478 ir_node *m_right = get_Mul_right(b);
2479 if (is_Const(m_right)) {
2480 ir_node *cnst2 = const_negate(m_right);
2481 if (cnst2 != NULL) {
2482 ir_graph *irg = current_ir_graph;
2483 dbg_info *m_dbg = get_irn_dbg_info(b);
2484 ir_node *m_block = get_nodes_block(b);
2485 ir_node *m_left = get_Mul_left(b);
2486 ir_mode *m_mode = get_irn_mode(b);
2487 ir_node *mul = new_rd_Mul(m_dbg, irg, m_block, m_left, cnst2, m_mode);
2488 dbg_info *a_dbg = get_irn_dbg_info(n);
2489 ir_node *a_block = get_nodes_block(n);
2491 n = new_rd_Add(a_dbg, irg, a_block, a, mul, mode);
2492 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2498 /* Beware of Sub(P, P) which cannot be optimized into a simple Minus ... */
2499 if (mode_is_num(mode) && mode == get_irn_mode(a) && is_Const(a) && is_Const_null(a)) {
2501 get_irn_dbg_info(n),
2506 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_0_A);
2510 if (mode_wrap_around(mode)) {
2511 ir_node *left = get_Add_left(a);
2512 ir_node *right = get_Add_right(a);
2514 /* FIXME: Does the Conv's work only for two complement or generally? */
2516 if (mode != get_irn_mode(right)) {
2517 /* This Sub is an effective Cast */
2518 right = new_r_Conv(get_irn_irg(n), get_nodes_block(n), right, mode);
2521 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2523 } else if (right == b) {
2524 if (mode != get_irn_mode(left)) {
2525 /* This Sub is an effective Cast */
2526 left = new_r_Conv(get_irn_irg(n), get_nodes_block(n), left, mode);
2529 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2535 if (mode_wrap_around(mode)) {
2536 ir_node *left = get_Add_left(b);
2537 ir_node *right = get_Add_right(b);
2539 /* FIXME: Does the Conv's work only for two complement or generally? */
2541 ir_mode *r_mode = get_irn_mode(right);
2543 n = new_r_Minus(get_irn_irg(n), get_nodes_block(n), right, r_mode);
2544 if (mode != r_mode) {
2545 /* This Sub is an effective Cast */
2546 n = new_r_Conv(get_irn_irg(n), get_nodes_block(n), n, mode);
2548 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2550 } else if (right == a) {
2551 ir_mode *l_mode = get_irn_mode(left);
2553 n = new_r_Minus(get_irn_irg(n), get_nodes_block(n), left, l_mode);
2554 if (mode != l_mode) {
2555 /* This Sub is an effective Cast */
2556 n = new_r_Conv(get_irn_irg(n), get_nodes_block(n), n, mode);
2558 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2563 if (mode_is_int(mode) && is_Conv(a) && is_Conv(b)) {
2564 ir_mode *mode = get_irn_mode(a);
2566 if (mode == get_irn_mode(b)) {
2568 ir_node *op_a = get_Conv_op(a);
2569 ir_node *op_b = get_Conv_op(b);
2571 /* check if it's allowed to skip the conv */
2572 ma = get_irn_mode(op_a);
2573 mb = get_irn_mode(op_b);
2575 if (mode_is_reference(ma) && mode_is_reference(mb)) {
2576 /* SubInt(ConvInt(aP), ConvInt(bP)) -> SubInt(aP,bP) */
2579 set_Sub_right(n, b);
2585 /* do NOT execute this code if reassociation is enabled, it does the inverse! */
2586 if (!is_reassoc_running() && is_Mul(a)) {
2587 ir_node *ma = get_Mul_left(a);
2588 ir_node *mb = get_Mul_right(a);
2591 ir_node *blk = get_nodes_block(n);
2593 get_irn_dbg_info(n),
2594 current_ir_graph, blk,
2597 get_irn_dbg_info(n),
2598 current_ir_graph, blk,
2600 new_Const_long(mode, 1),
2603 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MUL_A_X_A);
2605 } else if (mb == b) {
2606 ir_node *blk = get_nodes_block(n);
2608 get_irn_dbg_info(n),
2609 current_ir_graph, blk,
2612 get_irn_dbg_info(n),
2613 current_ir_graph, blk,
2615 new_Const_long(mode, 1),
2618 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MUL_A_X_A);
2622 if (is_Sub(a)) { /* (x - y) - b -> x - (y + b) */
2623 ir_node *x = get_Sub_left(a);
2624 ir_node *y = get_Sub_right(a);
2625 ir_node *blk = get_nodes_block(n);
2626 ir_mode *m_b = get_irn_mode(b);
2627 ir_mode *m_y = get_irn_mode(y);
2631 /* Determine the right mode for the Add. */
2634 else if (mode_is_reference(m_b))
2636 else if (mode_is_reference(m_y))
2640 * Both modes are different but none is reference,
2641 * happens for instance in SubP(SubP(P, Iu), Is).
2642 * We have two possibilities here: Cast or ignore.
2643 * Currently we ignore this case.
2648 add = new_r_Add(current_ir_graph, blk, y, b, add_mode);
2650 n = new_rd_Sub(get_irn_dbg_info(n), current_ir_graph, blk, x, add, mode);
2651 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_SUB_X_Y_Z);
2655 if (get_mode_arithmetic(mode) == irma_twos_complement) {
2656 if (is_Const(a) && is_Not(b)) {
2657 /* c - ~X = X + (c+1) */
2658 tarval *tv = get_Const_tarval(a);
2660 tv = tarval_add(tv, get_mode_one(mode));
2661 if (tv != tarval_bad) {
2662 ir_node *blk = get_nodes_block(n);
2663 ir_node *c = new_Const(tv);
2664 n = new_rd_Add(get_irn_dbg_info(n), current_ir_graph, blk, get_Not_op(b), c, mode);
2665 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_C_NOT_X);
2671 } /* transform_node_Sub */
2674 * Several transformation done on n*n=2n bits mul.
2675 * These transformations must be done here because new nodes may be produced.
2677 static ir_node *transform_node_Mul2n(ir_node *n, ir_mode *mode) {
2679 ir_node *a = get_Mul_left(n);
2680 ir_node *b = get_Mul_right(n);
2681 tarval *ta = value_of(a);
2682 tarval *tb = value_of(b);
2683 ir_mode *smode = get_irn_mode(a);
2685 if (ta == get_mode_one(smode)) {
2686 /* (L)1 * (L)b = (L)b */
2687 ir_node *blk = get_nodes_block(n);
2688 n = new_rd_Conv(get_irn_dbg_info(n), current_ir_graph, blk, b, mode);
2689 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
2692 else if (ta == get_mode_minus_one(smode)) {
2693 /* (L)-1 * (L)b = (L)b */
2694 ir_node *blk = get_nodes_block(n);
2695 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, blk, b, smode);
2696 n = new_rd_Conv(get_irn_dbg_info(n), current_ir_graph, blk, n, mode);
2697 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2700 if (tb == get_mode_one(smode)) {
2701 /* (L)a * (L)1 = (L)a */
2702 ir_node *blk = get_irn_n(a, -1);
2703 n = new_rd_Conv(get_irn_dbg_info(n), current_ir_graph, blk, a, mode);
2704 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
2707 else if (tb == get_mode_minus_one(smode)) {
2708 /* (L)a * (L)-1 = (L)-a */
2709 ir_node *blk = get_nodes_block(n);
2710 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, blk, a, smode);
2711 n = new_rd_Conv(get_irn_dbg_info(n), current_ir_graph, blk, n, mode);
2712 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2719 * Transform Mul(a,-1) into -a.
2720 * Do constant evaluation of Phi nodes.
2721 * Do architecture dependent optimizations on Mul nodes
2723 static ir_node *transform_node_Mul(ir_node *n) {
2724 ir_node *c, *oldn = n;
2725 ir_mode *mode = get_irn_mode(n);
2726 ir_node *a = get_Mul_left(n);
2727 ir_node *b = get_Mul_right(n);
2729 if (is_Bad(a) || is_Bad(b))
2732 if (mode != get_irn_mode(a))
2733 return transform_node_Mul2n(n, mode);
2735 HANDLE_BINOP_PHI(tarval_mul, a, b, c, mode);
2737 if (mode_is_signed(mode)) {
2740 if (value_of(a) == get_mode_minus_one(mode))
2742 else if (value_of(b) == get_mode_minus_one(mode))
2745 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, get_nodes_block(n), r, mode);
2746 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2751 if (is_Const(b)) { /* (-a) * const -> a * -const */
2752 ir_node *cnst = const_negate(b);
2754 dbg_info *dbgi = get_irn_dbg_info(n);
2755 ir_node *block = get_nodes_block(n);
2756 n = new_rd_Mul(dbgi, current_ir_graph, block, get_Minus_op(a), cnst, mode);
2757 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2760 } else if (is_Minus(b)) { /* (-a) * (-b) -> a * b */
2761 dbg_info *dbgi = get_irn_dbg_info(n);
2762 ir_node *block = get_nodes_block(n);
2763 n = new_rd_Mul(dbgi, current_ir_graph, block, get_Minus_op(a), get_Minus_op(b), mode);
2764 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_MINUS);
2766 } else if (is_Sub(b)) { /* (-a) * (b - c) -> a * (c - b) */
2767 ir_node *sub_l = get_Sub_left(b);
2768 ir_node *sub_r = get_Sub_right(b);
2769 dbg_info *dbgi = get_irn_dbg_info(n);
2770 ir_graph *irg = current_ir_graph;
2771 ir_node *block = get_nodes_block(n);
2772 ir_node *new_b = new_rd_Sub(dbgi, irg, block, sub_r, sub_l, mode);
2773 n = new_rd_Mul(dbgi, irg, block, get_Minus_op(a), new_b, mode);
2774 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS);
2777 } else if (is_Minus(b)) {
2778 if (is_Sub(a)) { /* (a - b) * (-c) -> (b - a) * c */
2779 ir_node *sub_l = get_Sub_left(a);
2780 ir_node *sub_r = get_Sub_right(a);
2781 dbg_info *dbgi = get_irn_dbg_info(n);
2782 ir_graph *irg = current_ir_graph;
2783 ir_node *block = get_nodes_block(n);
2784 ir_node *new_a = new_rd_Sub(dbgi, irg, block, sub_r, sub_l, mode);
2785 n = new_rd_Mul(dbgi, irg, block, new_a, get_Minus_op(b), mode);
2786 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS);
2790 if (get_mode_arithmetic(mode) == irma_ieee754) {
2792 tarval *tv = get_Const_tarval(a);
2793 if (tarval_ieee754_get_exponent(tv) == 1 && tarval_ieee754_zero_mantissa(tv)
2794 && !tarval_is_negative(tv)) {
2795 /* 2.0 * b = b + b */
2796 n = new_rd_Add(get_irn_dbg_info(n), current_ir_graph, get_nodes_block(n), b, b, mode);
2797 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_A_A);
2801 else if (is_Const(b)) {
2802 tarval *tv = get_Const_tarval(b);
2803 if (tarval_ieee754_get_exponent(tv) == 1 && tarval_ieee754_zero_mantissa(tv)
2804 && !tarval_is_negative(tv)) {
2805 /* a * 2.0 = a + a */
2806 n = new_rd_Add(get_irn_dbg_info(n), current_ir_graph, get_nodes_block(n), a, a, mode);
2807 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_A_A);
2812 return arch_dep_replace_mul_with_shifts(n);
2813 } /* transform_node_Mul */
2816 * Transform a Div Node.
2818 static ir_node *transform_node_Div(ir_node *n) {
2819 ir_mode *mode = get_Div_resmode(n);
2820 ir_node *a = get_Div_left(n);
2821 ir_node *b = get_Div_right(n);
2824 if (is_Const(b) && is_const_Phi(a)) {
2825 /* check for Div(Phi, Const) */
2826 value = apply_binop_on_phi(a, get_Const_tarval(b), tarval_div, mode, 0);
2828 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2832 else if (is_Const(a) && is_const_Phi(b)) {
2833 /* check for Div(Const, Phi) */
2834 value = apply_binop_on_phi(b, get_Const_tarval(a), tarval_div, mode, 1);
2836 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2840 else if (is_const_Phi(a) && is_const_Phi(b)) {
2841 /* check for Div(Phi, Phi) */
2842 value = apply_binop_on_2_phis(a, b, tarval_div, mode);
2844 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2851 const ir_node *dummy;
2852 if (a == b && value_not_zero(a, &dummy)) {
2853 /* BEWARE: we can optimize a/a to 1 only if this cannot cause a exception */
2854 value = new_Const(get_mode_one(mode));
2855 DBG_OPT_CSTEVAL(n, value);
2858 if (mode_is_signed(mode) && is_Const(b)) {
2859 tarval *tv = get_Const_tarval(b);
2861 if (tv == get_mode_minus_one(mode)) {
2863 value = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, get_nodes_block(n), a, mode);
2864 DBG_OPT_CSTEVAL(n, value);
2868 /* Try architecture dependent optimization */
2869 value = arch_dep_replace_div_by_const(n);
2876 /* Turn Div into a tuple (mem, jmp, bad, value) */
2877 mem = get_Div_mem(n);
2878 blk = get_nodes_block(n);
2880 /* skip a potential Pin */
2881 mem = skip_Pin(mem);
2882 turn_into_tuple(n, pn_Div_max);
2883 set_Tuple_pred(n, pn_Div_M, mem);
2884 set_Tuple_pred(n, pn_Div_X_regular, new_r_Jmp(current_ir_graph, blk));
2885 set_Tuple_pred(n, pn_Div_X_except, new_Bad());
2886 set_Tuple_pred(n, pn_Div_res, value);
2889 } /* transform_node_Div */
2892 * Transform a Mod node.
2894 static ir_node *transform_node_Mod(ir_node *n) {
2895 ir_mode *mode = get_Mod_resmode(n);
2896 ir_node *a = get_Mod_left(n);
2897 ir_node *b = get_Mod_right(n);
2901 if (is_Const(b) && is_const_Phi(a)) {
2902 /* check for Div(Phi, Const) */
2903 value = apply_binop_on_phi(a, get_Const_tarval(b), tarval_mod, mode, 0);
2905 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2909 else if (is_Const(a) && is_const_Phi(b)) {
2910 /* check for Div(Const, Phi) */
2911 value = apply_binop_on_phi(b, get_Const_tarval(a), tarval_mod, mode, 1);
2913 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2917 else if (is_const_Phi(a) && is_const_Phi(b)) {
2918 /* check for Div(Phi, Phi) */
2919 value = apply_binop_on_2_phis(a, b, tarval_mod, mode);
2921 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2928 if (tv != tarval_bad) {
2929 value = new_Const(tv);
2931 DBG_OPT_CSTEVAL(n, value);
2934 ir_node *a = get_Mod_left(n);
2935 ir_node *b = get_Mod_right(n);
2936 const ir_node *dummy;
2938 if (a == b && value_not_zero(a, &dummy)) {
2939 /* BEWARE: we can optimize a%a to 0 only if this cannot cause a exception */
2940 value = new_Const(get_mode_null(mode));
2941 DBG_OPT_CSTEVAL(n, value);
2944 if (mode_is_signed(mode) && is_Const(b)) {
2945 tarval *tv = get_Const_tarval(b);
2947 if (tv == get_mode_minus_one(mode)) {
2949 value = new_Const(get_mode_null(mode));
2950 DBG_OPT_CSTEVAL(n, value);
2954 /* Try architecture dependent optimization */
2955 value = arch_dep_replace_mod_by_const(n);
2963 /* Turn Mod into a tuple (mem, jmp, bad, value) */
2964 mem = get_Mod_mem(n);
2965 blk = get_nodes_block(n);
2967 /* skip a potential Pin */
2968 mem = skip_Pin(mem);
2969 turn_into_tuple(n, pn_Mod_max);
2970 set_Tuple_pred(n, pn_Mod_M, mem);
2971 set_Tuple_pred(n, pn_Mod_X_regular, new_r_Jmp(current_ir_graph, blk));
2972 set_Tuple_pred(n, pn_Mod_X_except, new_Bad());
2973 set_Tuple_pred(n, pn_Mod_res, value);
2976 } /* transform_node_Mod */
2979 * Transform a DivMod node.
2981 static ir_node *transform_node_DivMod(ir_node *n) {
2982 const ir_node *dummy;
2983 ir_node *a = get_DivMod_left(n);
2984 ir_node *b = get_DivMod_right(n);
2985 ir_mode *mode = get_DivMod_resmode(n);
2990 if (is_Const(b) && is_const_Phi(a)) {
2991 /* check for Div(Phi, Const) */
2992 va = apply_binop_on_phi(a, get_Const_tarval(b), tarval_div, mode, 0);
2993 vb = apply_binop_on_phi(a, get_Const_tarval(b), tarval_mod, mode, 0);
2995 DBG_OPT_ALGSIM0(n, va, FS_OPT_CONST_PHI);
2996 DBG_OPT_ALGSIM0(n, vb, FS_OPT_CONST_PHI);
3000 else if (is_Const(a) && is_const_Phi(b)) {
3001 /* check for Div(Const, Phi) */
3002 va = apply_binop_on_phi(b, get_Const_tarval(a), tarval_div, mode, 1);
3003 vb = apply_binop_on_phi(b, get_Const_tarval(a), tarval_mod, mode, 1);
3005 DBG_OPT_ALGSIM0(n, va, FS_OPT_CONST_PHI);
3006 DBG_OPT_ALGSIM0(n, vb, FS_OPT_CONST_PHI);
3010 else if (is_const_Phi(a) && is_const_Phi(b)) {
3011 /* check for Div(Phi, Phi) */
3012 va = apply_binop_on_2_phis(a, b, tarval_div, mode);
3013 vb = apply_binop_on_2_phis(a, b, tarval_mod, mode);
3015 DBG_OPT_ALGSIM0(n, va, FS_OPT_CONST_PHI);
3016 DBG_OPT_ALGSIM0(n, vb, FS_OPT_CONST_PHI);
3023 if (tb != tarval_bad) {
3024 if (tb == get_mode_one(get_tarval_mode(tb))) {
3026 vb = new_Const(get_mode_null(mode));
3027 DBG_OPT_CSTEVAL(n, vb);
3029 } else if (ta != tarval_bad) {
3030 tarval *resa, *resb;
3031 resa = tarval_div(ta, tb);
3032 if (resa == tarval_bad) return n; /* Causes exception!!! Model by replacing through
3033 Jmp for X result!? */
3034 resb = tarval_mod(ta, tb);
3035 if (resb == tarval_bad) return n; /* Causes exception! */
3036 va = new_Const(resa);
3037 vb = new_Const(resb);
3038 DBG_OPT_CSTEVAL(n, va);
3039 DBG_OPT_CSTEVAL(n, vb);
3041 } else if (mode_is_signed(mode) && tb == get_mode_minus_one(mode)) {
3042 va = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, get_nodes_block(n), a, mode);
3043 vb = new_Const(get_mode_null(mode));
3044 DBG_OPT_CSTEVAL(n, va);
3045 DBG_OPT_CSTEVAL(n, vb);
3047 } else { /* Try architecture dependent optimization */
3050 arch_dep_replace_divmod_by_const(&va, &vb, n);
3051 evaluated = va != NULL;
3053 } else if (a == b) {
3054 if (value_not_zero(a, &dummy)) {
3056 va = new_Const(get_mode_one(mode));
3057 vb = new_Const(get_mode_null(mode));
3058 DBG_OPT_CSTEVAL(n, va);
3059 DBG_OPT_CSTEVAL(n, vb);
3062 /* BEWARE: it is NOT possible to optimize a/a to 1, as this may cause a exception */
3065 } else if (ta == get_mode_null(mode) && value_not_zero(b, &dummy)) {
3066 /* 0 / non-Const = 0 */
3071 if (evaluated) { /* replace by tuple */
3075 mem = get_DivMod_mem(n);
3076 /* skip a potential Pin */
3077 mem = skip_Pin(mem);
3079 blk = get_nodes_block(n);
3080 turn_into_tuple(n, pn_DivMod_max);
3081 set_Tuple_pred(n, pn_DivMod_M, mem);
3082 set_Tuple_pred(n, pn_DivMod_X_regular, new_r_Jmp(current_ir_graph, blk));
3083 set_Tuple_pred(n, pn_DivMod_X_except, new_Bad()); /* no exception */
3084 set_Tuple_pred(n, pn_DivMod_res_div, va);
3085 set_Tuple_pred(n, pn_DivMod_res_mod, vb);
3089 } /* transform_node_DivMod */
3092 * Optimize x / c to x * (1/c)
3094 static ir_node *transform_node_Quot(ir_node *n) {
3095 ir_mode *mode = get_Quot_resmode(n);
3098 if (get_mode_arithmetic(mode) == irma_ieee754) {
3099 ir_node *b = get_Quot_right(n);
3100 tarval *tv = value_of(b);
3102 if (tv != tarval_bad) {
3103 int rem = tarval_fp_ops_enabled();
3106 * Floating point constant folding might be disabled here to
3108 * However, as we check for exact result, doing it is safe.
3111 tarval_enable_fp_ops(1);
3112 tv = tarval_quo(get_mode_one(mode), tv);
3113 tarval_enable_fp_ops(rem);
3115 /* Do the transformation if the result is either exact or we are not
3116 using strict rules. */
3117 if (tv != tarval_bad &&
3118 (tarval_ieee754_get_exact() || (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic) == 0)) {
3119 ir_node *blk = get_nodes_block(n);
3120 ir_node *c = new_Const(tv);
3121 ir_node *a = get_Quot_left(n);
3122 ir_node *m = new_rd_Mul(get_irn_dbg_info(n), current_ir_graph, blk, a, c, mode);
3123 ir_node *mem = get_Quot_mem(n);
3125 /* skip a potential Pin */
3126 mem = skip_Pin(mem);
3127 turn_into_tuple(n, pn_Quot_max);
3128 set_Tuple_pred(n, pn_Quot_M, mem);
3129 set_Tuple_pred(n, pn_Quot_X_regular, new_r_Jmp(current_ir_graph, blk));
3130 set_Tuple_pred(n, pn_Quot_X_except, new_r_Bad(current_ir_graph));
3131 set_Tuple_pred(n, pn_Quot_res, m);
3132 DBG_OPT_ALGSIM1(oldn, a, b, m, FS_OPT_FP_INV_MUL);
3137 } /* transform_node_Quot */
3140 * Optimize Abs(x) into x if x is Confirmed >= 0
3141 * Optimize Abs(x) into -x if x is Confirmed <= 0
3142 * Optimize Abs(-x) int Abs(x)
3144 static ir_node *transform_node_Abs(ir_node *n) {
3145 ir_node *c, *oldn = n;
3146 ir_node *a = get_Abs_op(n);
3149 HANDLE_UNOP_PHI(tarval_abs, a, c);
3151 switch (classify_value_sign(a)) {
3152 case value_classified_negative:
3153 mode = get_irn_mode(n);
3156 * We can replace the Abs by -x here.
3157 * We even could add a new Confirm here
3158 * (if not twos complement)
3160 * Note that -x would create a new node, so we could
3161 * not run it in the equivalent_node() context.
3163 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph,
3164 get_nodes_block(n), a, mode);
3166 DBG_OPT_CONFIRM(oldn, n);
3168 case value_classified_positive:
3169 /* n is positive, Abs is not needed */
3172 DBG_OPT_CONFIRM(oldn, n);
3178 /* Abs(-x) = Abs(x) */
3179 mode = get_irn_mode(n);
3180 n = new_rd_Abs(get_irn_dbg_info(n), current_ir_graph,
3181 get_nodes_block(n), get_Minus_op(a), mode);
3182 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ABS_MINUS_X);
3186 } /* transform_node_Abs */
3189 * Optimize -a CMP -b into b CMP a.
3190 * This works only for for modes where unary Minus
3192 * Note that two-complement integers can Overflow
3193 * so it will NOT work.
3195 * For == and != can be handled in Proj(Cmp)
3197 static ir_node *transform_node_Cmp(ir_node *n) {
3199 ir_node *left = get_Cmp_left(n);
3200 ir_node *right = get_Cmp_right(n);
3202 if (is_Minus(left) && is_Minus(right) &&
3203 !mode_overflow_on_unary_Minus(get_irn_mode(left))) {
3204 ir_node *const new_left = get_Minus_op(right);
3205 ir_node *const new_right = get_Minus_op(left);
3206 n = new_rd_Cmp(get_irn_dbg_info(n), current_ir_graph,
3207 get_nodes_block(n), new_left, new_right);
3208 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CMP_OP_OP);
3211 } /* transform_node_Cmp */
3215 * Transform a Cond node.
3217 * Replace the Cond by a Jmp if it branches on a constant
3220 static ir_node *transform_node_Cond(ir_node *n) {
3223 ir_node *a = get_Cond_selector(n);
3224 tarval *ta = value_of(a);
3226 /* we need block info which is not available in floating irgs */
3227 if (get_irg_pinned(current_ir_graph) == op_pin_state_floats)
3230 if ((ta != tarval_bad) &&
3231 (get_irn_mode(a) == mode_b) &&
3232 (get_opt_unreachable_code())) {
3233 /* It's a boolean Cond, branching on a boolean constant.
3234 Replace it by a tuple (Bad, Jmp) or (Jmp, Bad) */
3235 ir_node *blk = get_nodes_block(n);
3236 jmp = new_r_Jmp(current_ir_graph, blk);
3237 turn_into_tuple(n, pn_Cond_max);
3238 if (ta == tarval_b_true) {
3239 set_Tuple_pred(n, pn_Cond_false, new_Bad());
3240 set_Tuple_pred(n, pn_Cond_true, jmp);
3242 set_Tuple_pred(n, pn_Cond_false, jmp);
3243 set_Tuple_pred(n, pn_Cond_true, new_Bad());
3245 /* We might generate an endless loop, so keep it alive. */
3246 add_End_keepalive(get_irg_end(current_ir_graph), blk);
3249 } /* transform_node_Cond */
3252 * Prototype of a recursive transform function
3253 * for bitwise distributive transformations.
3255 typedef ir_node* (*recursive_transform)(ir_node *n);
3258 * makes use of distributive laws for and, or, eor
3259 * and(a OP c, b OP c) -> and(a, b) OP c
3260 * note, might return a different op than n
3262 static ir_node *transform_bitwise_distributive(ir_node *n,
3263 recursive_transform trans_func)
3266 ir_node *a = get_binop_left(n);
3267 ir_node *b = get_binop_right(n);
3268 ir_op *op = get_irn_op(a);
3269 ir_op *op_root = get_irn_op(n);
3271 if(op != get_irn_op(b))
3274 /* and(conv(a), conv(b)) -> conv(and(a,b)) */
3275 if (op == op_Conv) {
3276 ir_node *a_op = get_Conv_op(a);
3277 ir_node *b_op = get_Conv_op(b);
3278 ir_mode *a_mode = get_irn_mode(a_op);
3279 ir_mode *b_mode = get_irn_mode(b_op);
3280 if(a_mode == b_mode && (mode_is_int(a_mode) || a_mode == mode_b)) {
3281 ir_node *blk = get_nodes_block(n);
3284 set_binop_left(n, a_op);
3285 set_binop_right(n, b_op);
3286 set_irn_mode(n, a_mode);
3288 n = new_r_Conv(current_ir_graph, blk, n, get_irn_mode(oldn));
3290 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_SHIFT_AND);
3296 /* nothing to gain here */
3300 if (op == op_Shrs || op == op_Shr || op == op_Shl
3301 || op == op_And || op == op_Or || op == op_Eor) {
3302 ir_node *a_left = get_binop_left(a);
3303 ir_node *a_right = get_binop_right(a);
3304 ir_node *b_left = get_binop_left(b);
3305 ir_node *b_right = get_binop_right(b);
3307 ir_node *op1 = NULL;
3308 ir_node *op2 = NULL;
3310 if (is_op_commutative(op)) {
3311 if (a_left == b_left) {
3315 } else if(a_left == b_right) {
3319 } else if(a_right == b_left) {
3325 if(a_right == b_right) {
3332 /* (a sop c) & (b sop c) => (a & b) sop c */
3333 ir_node *blk = get_nodes_block(n);
3335 ir_node *new_n = exact_copy(n);
3336 set_binop_left(new_n, op1);
3337 set_binop_right(new_n, op2);
3338 new_n = trans_func(new_n);
3340 if(op_root == op_Eor && op == op_Or) {
3341 dbg_info *dbgi = get_irn_dbg_info(n);
3342 ir_graph *irg = current_ir_graph;
3343 ir_mode *mode = get_irn_mode(c);
3345 c = new_rd_Not(dbgi, irg, blk, c, mode);
3346 n = new_rd_And(dbgi, irg, blk, new_n, c, mode);
3349 set_nodes_block(n, blk);
3350 set_binop_left(n, new_n);
3351 set_binop_right(n, c);
3352 add_identities(current_ir_graph->value_table, n);
3355 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_SHIFT_AND);
3366 static ir_node *transform_node_And(ir_node *n) {
3367 ir_node *c, *oldn = n;
3368 ir_node *a = get_And_left(n);
3369 ir_node *b = get_And_right(n);
3372 mode = get_irn_mode(n);
3373 HANDLE_BINOP_PHI(tarval_and, a, b, c, mode);
3375 /* we can evaluate 2 Projs of the same Cmp */
3376 if (mode == mode_b && is_Proj(a) && is_Proj(b)) {
3377 ir_node *pred_a = get_Proj_pred(a);
3378 ir_node *pred_b = get_Proj_pred(b);
3379 if (pred_a == pred_b) {
3380 dbg_info *dbgi = get_irn_dbg_info(n);
3381 ir_node *block = get_nodes_block(pred_a);
3382 pn_Cmp pn_a = get_Proj_proj(a);
3383 pn_Cmp pn_b = get_Proj_proj(b);
3384 /* yes, we can simply calculate with pncs */
3385 pn_Cmp new_pnc = pn_a & pn_b;
3387 return new_rd_Proj(dbgi, current_ir_graph, block, pred_a, mode_b, new_pnc);
3392 ir_node *op = get_Not_op(b);
3394 ir_node *ba = get_And_left(op);
3395 ir_node *bb = get_And_right(op);
3397 /* it's enough to test the following cases due to normalization! */
3398 if (get_Or_left(a) == ba && get_Or_right(a) == bb) {
3399 /* (a|b) & ~(a&b) = a^b */
3400 ir_node *block = get_nodes_block(n);
3402 n = new_rd_Eor(get_irn_dbg_info(n), current_ir_graph, block, ba, bb, mode);
3403 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_TO_EOR);
3411 ir_node *op = get_Not_op(a);
3413 ir_node *aa = get_And_left(op);
3414 ir_node *ab = get_And_right(op);
3416 /* it's enough to test the following cases due to normalization! */
3417 if (get_Or_left(b) == aa && get_Or_right(b) == ab) {
3418 /* (a|b) & ~(a&b) = a^b */
3419 ir_node *block = get_nodes_block(n);
3421 n = new_rd_Eor(get_irn_dbg_info(n), current_ir_graph, block, aa, ab, mode);
3422 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_TO_EOR);
3429 ir_node *al = get_Eor_left(a);
3430 ir_node *ar = get_Eor_right(a);
3433 /* (b ^ a) & b -> ~a & b */
3434 dbg_info *dbg = get_irn_dbg_info(n);
3435 ir_node *block = get_nodes_block(n);
3437 ar = new_rd_Not(dbg, current_ir_graph, block, ar, mode);
3438 n = new_rd_And(dbg, current_ir_graph, block, ar, b, mode);
3439 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3443 /* (a ^ b) & b -> ~a & b */
3444 dbg_info *dbg = get_irn_dbg_info(n);
3445 ir_node *block = get_nodes_block(n);
3447 al = new_rd_Not(dbg, current_ir_graph, block, al, mode);
3448 n = new_rd_And(dbg, current_ir_graph, block, al, b, mode);
3449 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3454 ir_node *bl = get_Eor_left(b);
3455 ir_node *br = get_Eor_right(b);
3458 /* a & (a ^ b) -> a & ~b */
3459 dbg_info *dbg = get_irn_dbg_info(n);
3460 ir_node *block = get_nodes_block(n);
3462 br = new_rd_Not(dbg, current_ir_graph, block, br, mode);
3463 n = new_rd_And(dbg, current_ir_graph, block, br, a, mode);
3464 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3468 /* a & (b ^ a) -> a & ~b */
3469 dbg_info *dbg = get_irn_dbg_info(n);
3470 ir_node *block = get_nodes_block(n);
3472 bl = new_rd_Not(dbg, current_ir_graph, block, bl, mode);
3473 n = new_rd_And(dbg, current_ir_graph, block, bl, a, mode);
3474 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3478 if (is_Not(a) && is_Not(b)) {
3479 /* ~a & ~b = ~(a|b) */
3480 ir_node *block = get_nodes_block(n);
3481 ir_mode *mode = get_irn_mode(n);
3485 n = new_rd_Or(get_irn_dbg_info(n), current_ir_graph, block, a, b, mode);
3486 n = new_rd_Not(get_irn_dbg_info(n), current_ir_graph, block, n, mode);
3487 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_DEMORGAN);
3491 n = transform_bitwise_distributive(n, transform_node_And);
3494 } /* transform_node_And */
3499 static ir_node *transform_node_Eor(ir_node *n) {
3500 ir_node *c, *oldn = n;
3501 ir_node *a = get_Eor_left(n);
3502 ir_node *b = get_Eor_right(n);
3503 ir_mode *mode = get_irn_mode(n);
3505 HANDLE_BINOP_PHI(tarval_eor, a, b, c, mode);
3507 /* we can evaluate 2 Projs of the same Cmp */
3508 if (mode == mode_b && is_Proj(a) && is_Proj(b)) {
3509 ir_node *pred_a = get_Proj_pred(a);
3510 ir_node *pred_b = get_Proj_pred(b);
3511 if(pred_a == pred_b) {
3512 dbg_info *dbgi = get_irn_dbg_info(n);
3513 ir_node *block = get_nodes_block(pred_a);
3514 pn_Cmp pn_a = get_Proj_proj(a);
3515 pn_Cmp pn_b = get_Proj_proj(b);
3516 /* yes, we can simply calculate with pncs */
3517 pn_Cmp new_pnc = pn_a ^ pn_b;
3519 return new_rd_Proj(dbgi, current_ir_graph, block, pred_a, mode_b,
3526 n = new_rd_Const(get_irn_dbg_info(n), current_ir_graph,
3527 get_mode_null(mode));
3528 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_A_A);
3529 } else if (mode == mode_b &&
3531 is_Const(b) && is_Const_one(b) &&
3532 is_Cmp(get_Proj_pred(a))) {
3533 /* The Eor negates a Cmp. The Cmp has the negated result anyways! */
3534 n = new_r_Proj(current_ir_graph, get_nodes_block(n), get_Proj_pred(a),
3535 mode_b, get_negated_pnc(get_Proj_proj(a), mode));
3537 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT_BOOL);
3538 } else if (is_Const(b)) {
3539 if (is_Not(a)) { /* ~x ^ const -> x ^ ~const */
3540 ir_node *cnst = new_Const(tarval_not(get_Const_tarval(b)));
3541 ir_node *not_op = get_Not_op(a);
3542 dbg_info *dbg = get_irn_dbg_info(n);
3543 ir_graph *irg = current_ir_graph;
3544 ir_node *block = get_nodes_block(n);
3545 ir_mode *mode = get_irn_mode(n);
3546 n = new_rd_Eor(dbg, irg, block, not_op, cnst, mode);
3548 } else if (is_Const_all_one(b)) { /* x ^ 1...1 -> ~1 */
3549 n = new_r_Not(current_ir_graph, get_nodes_block(n), a, mode);
3550 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3553 n = transform_bitwise_distributive(n, transform_node_Eor);
3557 } /* transform_node_Eor */
3562 static ir_node *transform_node_Not(ir_node *n) {
3563 ir_node *c, *oldn = n;
3564 ir_node *a = get_Not_op(n);
3565 ir_mode *mode = get_irn_mode(n);
3567 HANDLE_UNOP_PHI(tarval_not,a,c);
3569 /* check for a boolean Not */
3570 if (mode == mode_b &&
3572 is_Cmp(get_Proj_pred(a))) {
3573 /* We negate a Cmp. The Cmp has the negated result anyways! */
3574 n = new_r_Proj(current_ir_graph, get_nodes_block(n), get_Proj_pred(a),
3575 mode_b, get_negated_pnc(get_Proj_proj(a), mode_b));
3576 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_CMP);
3580 ir_node *eor_b = get_Eor_right(a);
3581 if (is_Const(eor_b)) { /* ~(x ^ const) -> x ^ ~const */
3582 ir_node *cnst = new_Const(tarval_not(get_Const_tarval(eor_b)));
3583 ir_node *eor_a = get_Eor_left(a);
3584 dbg_info *dbg = get_irn_dbg_info(n);
3585 ir_graph *irg = current_ir_graph;
3586 ir_node *block = get_nodes_block(n);
3587 ir_mode *mode = get_irn_mode(n);
3588 n = new_rd_Eor(dbg, irg, block, eor_a, cnst, mode);
3592 if (get_mode_arithmetic(mode) == irma_twos_complement) {
3593 if (is_Minus(a)) { /* ~-x -> x + -1 */
3594 dbg_info *dbg = get_irn_dbg_info(n);
3595 ir_graph *irg = current_ir_graph;
3596 ir_node *block = get_nodes_block(n);
3597 ir_node *add_l = get_Minus_op(a);
3598 ir_node *add_r = new_rd_Const(dbg, irg, get_mode_minus_one(mode));
3599 n = new_rd_Add(dbg, irg, block, add_l, add_r, mode);
3600 } else if (is_Add(a)) {
3601 ir_node *add_r = get_Add_right(a);
3602 if (is_Const(add_r) && is_Const_all_one(add_r)) {
3603 /* ~(x + -1) = -x */
3604 ir_node *op = get_Add_left(a);
3605 ir_node *blk = get_nodes_block(n);
3606 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph, blk, op, get_irn_mode(n));
3607 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_MINUS_1);
3612 } /* transform_node_Not */
3615 * Transform a Minus.
3619 * -(a >>u (size-1)) = a >>s (size-1)
3620 * -(a >>s (size-1)) = a >>u (size-1)
3621 * -(a * const) -> a * -const
3623 static ir_node *transform_node_Minus(ir_node *n) {
3624 ir_node *c, *oldn = n;
3625 ir_node *a = get_Minus_op(n);
3628 HANDLE_UNOP_PHI(tarval_neg,a,c);
3630 mode = get_irn_mode(a);
3631 if (get_mode_arithmetic(mode) == irma_twos_complement) {
3632 /* the following rules are only to twos-complement */
3635 ir_node *op = get_Not_op(a);
3636 tarval *tv = get_mode_one(mode);
3637 ir_node *blk = get_nodes_block(n);
3638 ir_node *c = new_Const(tv);
3639 n = new_rd_Add(get_irn_dbg_info(n), current_ir_graph, blk, op, c, mode);
3640 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_MINUS_NOT);
3644 ir_node *c = get_Shr_right(a);
3647 tarval *tv = get_Const_tarval(c);
3649 if (tarval_is_long(tv) && get_tarval_long(tv) == (int) get_mode_size_bits(mode) - 1) {
3650 /* -(a >>u (size-1)) = a >>s (size-1) */
3651 ir_node *v = get_Shr_left(a);
3653 n = new_rd_Shrs(get_irn_dbg_info(n), current_ir_graph, get_nodes_block(n), v, c, mode);
3654 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_PREDICATE);
3660 ir_node *c = get_Shrs_right(a);
3663 tarval *tv = get_Const_tarval(c);
3665 if (tarval_is_long(tv) && get_tarval_long(tv) == (int) get_mode_size_bits(mode) - 1) {
3666 /* -(a >>s (size-1)) = a >>u (size-1) */
3667 ir_node *v = get_Shrs_left(a);
3669 n = new_rd_Shr(get_irn_dbg_info(n), current_ir_graph, get_nodes_block(n), v, c, mode);
3670 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_PREDICATE);
3677 /* - (a-b) = b - a */
3678 ir_node *la = get_Sub_left(a);
3679 ir_node *ra = get_Sub_right(a);
3680 ir_node *blk = get_nodes_block(n);
3682 n = new_rd_Sub(get_irn_dbg_info(n), current_ir_graph, blk, ra, la, mode);
3683 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_MINUS_SUB);
3687 if (is_Mul(a)) { /* -(a * const) -> a * -const */
3688 ir_node *mul_l = get_Mul_left(a);
3689 ir_node *mul_r = get_Mul_right(a);
3690 tarval *tv = value_of(mul_r);
3691 if (tv != tarval_bad) {
3692 tv = tarval_neg(tv);
3693 if (tv != tarval_bad) {
3694 ir_node *cnst = new_Const(tv);
3695 dbg_info *dbg = get_irn_dbg_info(a);
3696 ir_graph *irg = current_ir_graph;
3697 ir_node *block = get_nodes_block(a);
3698 n = new_rd_Mul(dbg, irg, block, mul_l, cnst, mode);
3699 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_MINUS_MUL_C);
3706 } /* transform_node_Minus */
3709 * Transform a Cast_type(Const) into a new Const_type
3711 static ir_node *transform_node_Cast(ir_node *n) {
3713 ir_node *pred = get_Cast_op(n);
3714 ir_type *tp = get_irn_type(n);
3716 if (is_Const(pred) && get_Const_type(pred) != tp) {
3717 n = new_rd_Const_type(NULL, current_ir_graph, get_Const_tarval(pred), tp);
3718 DBG_OPT_CSTEVAL(oldn, n);
3719 } else if (is_SymConst(pred) && get_SymConst_value_type(pred) != tp) {
3720 n = new_rd_SymConst_type(NULL, current_ir_graph, get_irn_n(pred, -1), get_irn_mode(pred),
3721 get_SymConst_symbol(pred), get_SymConst_kind(pred), tp);
3722 DBG_OPT_CSTEVAL(oldn, n);
3726 } /* transform_node_Cast */
3729 * Transform a Proj(Load) with a non-null address.
3731 static ir_node *transform_node_Proj_Load(ir_node *proj) {
3732 if (get_opt_ldst_only_null_ptr_exceptions()) {
3733 if (get_irn_mode(proj) == mode_X) {
3734 ir_node *load = get_Proj_pred(proj);
3736 /* get the Load address */
3737 const ir_node *addr = get_Load_ptr(load);
3738 const ir_node *confirm;
3740 if (value_not_null(addr, &confirm)) {
3741 if (confirm == NULL) {
3742 /* this node may float if it did not depend on a Confirm */
3743 set_irn_pinned(load, op_pin_state_floats);
3745 if (get_Proj_proj(proj) == pn_Load_X_except) {
3746 DBG_OPT_EXC_REM(proj);
3747 return get_irg_bad(current_ir_graph);
3749 ir_node *blk = get_nodes_block(load);
3750 return new_r_Jmp(current_ir_graph, blk);
3756 } /* transform_node_Proj_Load */
3759 * Transform a Proj(Store) with a non-null address.
3761 static ir_node *transform_node_Proj_Store(ir_node *proj) {
3762 if (get_opt_ldst_only_null_ptr_exceptions()) {
3763 if (get_irn_mode(proj) == mode_X) {
3764 ir_node *store = get_Proj_pred(proj);
3766 /* get the load/store address */
3767 const ir_node *addr = get_Store_ptr(store);
3768 const ir_node *confirm;
3770 if (value_not_null(addr, &confirm)) {
3771 if (confirm == NULL) {
3772 /* this node may float if it did not depend on a Confirm */
3773 set_irn_pinned(store, op_pin_state_floats);
3775 if (get_Proj_proj(proj) == pn_Store_X_except) {
3776 DBG_OPT_EXC_REM(proj);
3777 return get_irg_bad(current_ir_graph);
3779 ir_node *blk = get_nodes_block(store);
3780 return new_r_Jmp(current_ir_graph, blk);
3786 } /* transform_node_Proj_Store */
3789 * Transform a Proj(Div) with a non-zero value.
3790 * Removes the exceptions and routes the memory to the NoMem node.
3792 static ir_node *transform_node_Proj_Div(ir_node *proj) {
3793 ir_node *div = get_Proj_pred(proj);
3794 ir_node *b = get_Div_right(div);
3795 ir_node *res, *new_mem;
3796 const ir_node *confirm;
3799 if (value_not_zero(b, &confirm)) {
3800 /* div(x, y) && y != 0 */
3801 if (confirm == NULL) {
3802 /* we are sure we have a Const != 0 */
3803 new_mem = get_Div_mem(div);
3804 new_mem = skip_Pin(new_mem);
3805 set_Div_mem(div, new_mem);
3806 set_irn_pinned(div, op_pin_state_floats);
3809 proj_nr = get_Proj_proj(proj);
3811 case pn_Div_X_regular:
3812 return new_r_Jmp(current_ir_graph, get_irn_n(div, -1));
3814 case pn_Div_X_except:
3815 /* we found an exception handler, remove it */
3816 DBG_OPT_EXC_REM(proj);
3820 res = get_Div_mem(div);
3821 new_mem = get_irg_no_mem(current_ir_graph);
3824 /* This node can only float up to the Confirm block */
3825 new_mem = new_r_Pin(current_ir_graph, get_nodes_block(confirm), new_mem);
3827 set_irn_pinned(div, op_pin_state_floats);
3828 /* this is a Div without exception, we can remove the memory edge */
3829 set_Div_mem(div, new_mem);
3834 } /* transform_node_Proj_Div */
3837 * Transform a Proj(Mod) with a non-zero value.
3838 * Removes the exceptions and routes the memory to the NoMem node.
3840 static ir_node *transform_node_Proj_Mod(ir_node *proj) {
3841 ir_node *mod = get_Proj_pred(proj);
3842 ir_node *b = get_Mod_right(mod);
3843 ir_node *res, *new_mem;
3844 const ir_node *confirm;
3847 if (value_not_zero(b, &confirm)) {
3848 /* mod(x, y) && y != 0 */
3849 proj_nr = get_Proj_proj(proj);
3851 if (confirm == NULL) {
3852 /* we are sure we have a Const != 0 */
3853 new_mem = get_Mod_mem(mod);
3854 new_mem = skip_Pin(new_mem);
3855 set_Mod_mem(mod, new_mem);
3856 set_irn_pinned(mod, op_pin_state_floats);
3861 case pn_Mod_X_regular:
3862 return new_r_Jmp(current_ir_graph, get_irn_n(mod, -1));
3864 case pn_Mod_X_except:
3865 /* we found an exception handler, remove it */
3866 DBG_OPT_EXC_REM(proj);
3870 res = get_Mod_mem(mod);
3871 new_mem = get_irg_no_mem(current_ir_graph);
3874 /* This node can only float up to the Confirm block */
3875 new_mem = new_r_Pin(current_ir_graph, get_nodes_block(confirm), new_mem);
3877 /* this is a Mod without exception, we can remove the memory edge */
3878 set_Mod_mem(mod, new_mem);
3881 if (get_Mod_left(mod) == b) {
3882 /* a % a = 0 if a != 0 */
3883 ir_mode *mode = get_irn_mode(proj);
3884 ir_node *res = new_Const(get_mode_null(mode));
3886 DBG_OPT_CSTEVAL(mod, res);
3892 } /* transform_node_Proj_Mod */
3895 * Transform a Proj(DivMod) with a non-zero value.
3896 * Removes the exceptions and routes the memory to the NoMem node.
3898 static ir_node *transform_node_Proj_DivMod(ir_node *proj) {
3899 ir_node *divmod = get_Proj_pred(proj);
3900 ir_node *b = get_DivMod_right(divmod);
3901 ir_node *res, *new_mem;
3902 const ir_node *confirm;
3905 if (value_not_zero(b, &confirm)) {
3906 /* DivMod(x, y) && y != 0 */
3907 proj_nr = get_Proj_proj(proj);
3909 if (confirm == NULL) {
3910 /* we are sure we have a Const != 0 */
3911 new_mem = get_DivMod_mem(divmod);
3912 new_mem = skip_Pin(new_mem);
3913 set_DivMod_mem(divmod, new_mem);
3914 set_irn_pinned(divmod, op_pin_state_floats);
3919 case pn_DivMod_X_regular:
3920 return new_r_Jmp(current_ir_graph, get_irn_n(divmod, -1));
3922 case pn_DivMod_X_except:
3923 /* we found an exception handler, remove it */
3924 DBG_OPT_EXC_REM(proj);
3928 res = get_DivMod_mem(divmod);
3929 new_mem = get_irg_no_mem(current_ir_graph);
3932 /* This node can only float up to the Confirm block */
3933 new_mem = new_r_Pin(current_ir_graph, get_nodes_block(confirm), new_mem);
3935 /* this is a DivMod without exception, we can remove the memory edge */
3936 set_DivMod_mem(divmod, new_mem);
3939 case pn_DivMod_res_mod:
3940 if (get_DivMod_left(divmod) == b) {
3941 /* a % a = 0 if a != 0 */
3942 ir_mode *mode = get_irn_mode(proj);
3943 ir_node *res = new_Const(get_mode_null(mode));
3945 DBG_OPT_CSTEVAL(divmod, res);
3951 } /* transform_node_Proj_DivMod */
3954 * Optimizes jump tables (CondIs or CondIu) by removing all impossible cases.
3956 static ir_node *transform_node_Proj_Cond(ir_node *proj) {
3957 if (get_opt_unreachable_code()) {
3958 ir_node *n = get_Proj_pred(proj);
3959 ir_node *b = get_Cond_selector(n);
3961 if (mode_is_int(get_irn_mode(b))) {
3962 tarval *tb = value_of(b);
3964 if (tb != tarval_bad) {
3965 /* we have a constant switch */
3966 long num = get_Proj_proj(proj);
3968 if (num != get_Cond_default_proj(n)) { /* we cannot optimize default Proj's yet */
3969 if (get_tarval_long(tb) == num) {
3970 /* Do NOT create a jump here, or we will have 2 control flow ops
3971 * in a block. This case is optimized away in optimize_cf(). */
3974 /* this case will NEVER be taken, kill it */
3975 return get_irg_bad(current_ir_graph);
3982 } /* transform_node_Proj_Cond */
3985 * Create a 0 constant of given mode.
3987 static ir_node *create_zero_const(ir_mode *mode) {
3988 tarval *tv = get_mode_null(mode);
3989 ir_node *cnst = new_Const(tv);
3994 /* the order of the values is important! */
3995 typedef enum const_class {
4001 static const_class classify_const(const ir_node* n)
4003 if (is_Const(n)) return const_const;
4004 if (is_irn_constlike(n)) return const_like;
4009 * Determines whether r is more constlike or has a larger index (in that order)
4012 static int operands_are_normalized(const ir_node *l, const ir_node *r)
4014 const const_class l_order = classify_const(l);
4015 const const_class r_order = classify_const(r);
4017 l_order > r_order ||
4018 (l_order == r_order && get_irn_idx(l) <= get_irn_idx(r));
4022 * Normalizes and optimizes Cmp nodes.
4024 static ir_node *transform_node_Proj_Cmp(ir_node *proj) {
4025 ir_node *n = get_Proj_pred(proj);
4026 ir_node *left = get_Cmp_left(n);
4027 ir_node *right = get_Cmp_right(n);
4030 ir_mode *mode = NULL;
4031 long proj_nr = get_Proj_proj(proj);
4033 /* we can evaluate some cases directly */
4036 return new_Const(get_tarval_b_false());
4038 return new_Const(get_tarval_b_true());
4040 if (!mode_is_float(get_irn_mode(left)))
4041 return new_Const(get_tarval_b_true());
4047 /* remove Casts of both sides */
4048 left = skip_Cast(left);
4049 right = skip_Cast(right);
4051 /* Remove unnecessary conversions */
4052 /* TODO handle constants */
4053 if (is_Conv(left) && is_Conv(right)) {
4054 ir_mode *mode = get_irn_mode(left);
4055 ir_node *op_left = get_Conv_op(left);
4056 ir_node *op_right = get_Conv_op(right);
4057 ir_mode *mode_left = get_irn_mode(op_left);
4058 ir_mode *mode_right = get_irn_mode(op_right);
4060 if (smaller_mode(mode_left, mode) && smaller_mode(mode_right, mode)
4061 && mode_left != mode_b && mode_right != mode_b) {
4062 ir_graph *irg = current_ir_graph;
4063 ir_node *block = get_nodes_block(n);
4065 if (mode_left == mode_right) {
4069 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV_CONV);
4070 } else if (smaller_mode(mode_left, mode_right)) {
4071 left = new_r_Conv(irg, block, op_left, mode_right);
4074 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV);
4075 } else if (smaller_mode(mode_right, mode_left)) {
4077 right = new_r_Conv(irg, block, op_right, mode_left);
4079 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV);
4084 /* remove operation on both sides if possible */
4085 if (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) {
4087 * The following operations are NOT safe for floating point operations, for instance
4088 * 1.0 + inf == 2.0 + inf, =/=> x == y
4090 if (mode_is_int(get_irn_mode(left))) {
4091 unsigned lop = get_irn_opcode(left);
4093 if (lop == get_irn_opcode(right)) {
4094 ir_node *ll, *lr, *rl, *rr;
4096 /* same operation on both sides, try to remove */
4100 /* ~a CMP ~b => a CMP b, -a CMP -b ==> a CMP b */
4101 left = get_unop_op(left);
4102 right = get_unop_op(right);
4104 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4107 ll = get_Add_left(left);
4108 lr = get_Add_right(left);
4109 rl = get_Add_left(right);
4110 rr = get_Add_right(right);
4113 /* X + a CMP X + b ==> a CMP b */
4117 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4118 } else if (ll == rr) {
4119 /* X + a CMP b + X ==> a CMP b */
4123 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4124 } else if (lr == rl) {
4125 /* a + X CMP X + b ==> a CMP b */
4129 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4130 } else if (lr == rr) {
4131 /* a + X CMP b + X ==> a CMP b */
4135 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4139 ll = get_Sub_left(left);
4140 lr = get_Sub_right(left);
4141 rl = get_Sub_left(right);
4142 rr = get_Sub_right(right);
4145 /* X - a CMP X - b ==> a CMP b */
4149 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4150 } else if (lr == rr) {
4151 /* a - X CMP b - X ==> a CMP b */
4155 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4159 if (get_Rotl_right(left) == get_Rotl_right(right)) {
4160 /* a ROTL X CMP b ROTL X ==> a CMP b */
4161 left = get_Rotl_left(left);
4162 right = get_Rotl_left(right);
4164 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4172 /* X+A == A, A+X == A, A-X == A -> X == 0 */
4173 if (is_Add(left) || is_Sub(left)) {
4174 ir_node *ll = get_binop_left(left);
4175 ir_node *lr = get_binop_right(left);
4177 if (lr == right && is_Add(left)) {
4184 right = create_zero_const(get_irn_mode(left));
4186 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4189 if (is_Add(right) || is_Sub(right)) {
4190 ir_node *rl = get_binop_left(right);
4191 ir_node *rr = get_binop_right(right);
4193 if (rr == left && is_Add(right)) {
4200 right = create_zero_const(get_irn_mode(left));
4202 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4205 if (is_And(left) && is_Const(right)) {
4206 ir_node *ll = get_binop_left(left);
4207 ir_node *lr = get_binop_right(left);
4208 if (is_Shr(ll) && is_Const(lr)) {
4209 /* Cmp((x >>u c1) & c2, c3) = Cmp(x & (c2 << c1), c3 << c1) */
4210 ir_node *block = get_nodes_block(n);
4211 ir_mode *mode = get_irn_mode(left);
4213 ir_node *llr = get_Shr_right(ll);
4214 if (is_Const(llr)) {
4215 ir_graph *irg = current_ir_graph;
4216 dbg_info *dbg = get_irn_dbg_info(left);
4218 tarval *c1 = get_Const_tarval(llr);
4219 tarval *c2 = get_Const_tarval(lr);
4220 tarval *c3 = get_Const_tarval(right);
4221 tarval *mask = tarval_shl(c2, c1);
4222 tarval *value = tarval_shl(c3, c1);
4224 left = new_rd_And(dbg, irg, block, get_Shr_left(ll), new_Const(mask), mode);
4225 right = new_Const(value);
4230 } /* mode_is_int(...) */
4231 } /* proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg */
4233 /* replace mode_b compares with ands/ors */
4234 if (get_irn_mode(left) == mode_b) {
4235 ir_graph *irg = current_ir_graph;
4236 ir_node *block = get_nodes_block(n);
4240 case pn_Cmp_Le: bres = new_r_Or( irg, block, new_r_Not(irg, block, left, mode_b), right, mode_b); break;
4241 case pn_Cmp_Lt: bres = new_r_And(irg, block, new_r_Not(irg, block, left, mode_b), right, mode_b); break;
4242 case pn_Cmp_Ge: bres = new_r_Or( irg, block, left, new_r_Not(irg, block, right, mode_b), mode_b); break;
4243 case pn_Cmp_Gt: bres = new_r_And(irg, block, left, new_r_Not(irg, block, right, mode_b), mode_b); break;
4244 case pn_Cmp_Lg: bres = new_r_Eor(irg, block, left, right, mode_b); break;
4245 case pn_Cmp_Eq: bres = new_r_Not(irg, block, new_r_Eor(irg, block, left, right, mode_b), mode_b); break;
4246 default: bres = NULL;
4249 DBG_OPT_ALGSIM0(n, bres, FS_OPT_CMP_TO_BOOL);
4255 * First step: normalize the compare op
4256 * by placing the constant on the right side
4257 * or moving the lower address node to the left.
4259 if (!operands_are_normalized(left, right)) {
4265 proj_nr = get_inversed_pnc(proj_nr);
4270 * Second step: Try to reduce the magnitude
4271 * of a constant. This may help to generate better code
4272 * later and may help to normalize more compares.
4273 * Of course this is only possible for integer values.
4275 tv = value_of(right);
4276 if (tv != tarval_bad) {
4277 mode = get_irn_mode(right);
4279 /* TODO extend to arbitrary constants */
4280 if (is_Conv(left) && tarval_is_null(tv)) {
4281 ir_node *op = get_Conv_op(left);
4282 ir_mode *op_mode = get_irn_mode(op);
4285 * UpConv(x) REL 0 ==> x REL 0
4287 if (get_mode_size_bits(mode) > get_mode_size_bits(op_mode) &&
4288 ((proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) ||
4289 mode_is_signed(mode) || !mode_is_signed(op_mode))) {
4290 tv = get_mode_null(op_mode);
4294 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV);
4298 if (tv != tarval_bad) {
4299 /* the following optimization is possible on modes without Overflow
4300 * on Unary Minus or on == and !=:
4301 * -a CMP c ==> a swap(CMP) -c
4303 * Beware: for two-complement Overflow may occur, so only == and != can
4304 * be optimized, see this:
4305 * -MININT < 0 =/=> MININT > 0 !!!
4307 if (is_Minus(left) &&
4308 (!mode_overflow_on_unary_Minus(mode) ||
4309 (mode_is_int(mode) && (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg)))) {
4310 tv = tarval_neg(tv);
4312 if (tv != tarval_bad) {
4313 left = get_Minus_op(left);
4314 proj_nr = get_inversed_pnc(proj_nr);
4316 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4318 } else if (is_Not(left) && (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg)) {
4319 /* Not(a) ==/!= c ==> a ==/!= Not(c) */
4320 tv = tarval_not(tv);
4322 if (tv != tarval_bad) {
4323 left = get_Not_op(left);
4325 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4329 /* for integer modes, we have more */
4330 if (mode_is_int(mode)) {
4331 /* Ne includes Unordered which is not possible on integers.
4332 * However, frontends often use this wrong, so fix it here */
4333 if (proj_nr & pn_Cmp_Uo) {
4334 proj_nr &= ~pn_Cmp_Uo;
4335 set_Proj_proj(proj, proj_nr);
4338 /* c > 0 : a < c ==> a <= (c-1) a >= c ==> a > (c-1) */
4339 if ((proj_nr == pn_Cmp_Lt || proj_nr == pn_Cmp_Ge) &&
4340 tarval_cmp(tv, get_mode_null(mode)) == pn_Cmp_Gt) {
4341 tv = tarval_sub(tv, get_mode_one(mode), NULL);
4343 if (tv != tarval_bad) {
4344 proj_nr ^= pn_Cmp_Eq;
4346 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CNST_MAGN);
4349 /* c < 0 : a > c ==> a >= (c+1) a <= c ==> a < (c+1) */
4350 else if ((proj_nr == pn_Cmp_Gt || proj_nr == pn_Cmp_Le) &&
4351 tarval_cmp(tv, get_mode_null(mode)) == pn_Cmp_Lt) {
4352 tv = tarval_add(tv, get_mode_one(mode));
4354 if (tv != tarval_bad) {
4355 proj_nr ^= pn_Cmp_Eq;
4357 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CNST_MAGN);
4361 /* the following reassociations work only for == and != */
4362 if (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) {
4364 #if 0 /* Might be not that good in general */
4365 /* a-b == 0 ==> a == b, a-b != 0 ==> a != b */
4366 if (tarval_is_null(tv) && is_Sub(left)) {
4367 right = get_Sub_right(left);
4368 left = get_Sub_left(left);
4370 tv = value_of(right);
4372 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4376 if (tv != tarval_bad) {
4377 /* a-c1 == c2 ==> a == c2+c1, a-c1 != c2 ==> a != c2+c1 */
4379 ir_node *c1 = get_Sub_right(left);
4380 tarval *tv2 = value_of(c1);
4382 if (tv2 != tarval_bad) {
4383 tv2 = tarval_add(tv, value_of(c1));
4385 if (tv2 != tarval_bad) {
4386 left = get_Sub_left(left);
4389 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4393 /* a+c1 == c2 ==> a == c2-c1, a+c1 != c2 ==> a != c2-c1 */
4394 else if (is_Add(left)) {
4395 ir_node *a_l = get_Add_left(left);
4396 ir_node *a_r = get_Add_right(left);
4400 if (is_Const(a_l)) {
4402 tv2 = value_of(a_l);
4405 tv2 = value_of(a_r);
4408 if (tv2 != tarval_bad) {
4409 tv2 = tarval_sub(tv, tv2, NULL);
4411 if (tv2 != tarval_bad) {
4415 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4419 /* -a == c ==> a == -c, -a != c ==> a != -c */
4420 else if (is_Minus(left)) {
4421 tarval *tv2 = tarval_sub(get_mode_null(mode), tv, NULL);
4423 if (tv2 != tarval_bad) {
4424 left = get_Minus_op(left);
4427 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4432 /* the following reassociations work only for <= */
4433 else if (proj_nr == pn_Cmp_Le || proj_nr == pn_Cmp_Lt) {
4434 if (tv != tarval_bad) {
4435 /* c >= 0 : Abs(a) <= c ==> (unsigned)(a + c) <= 2*c */
4436 if (is_Abs(left)) { // TODO something is missing here
4442 if (proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) {
4443 switch (get_irn_opcode(left)) {
4447 c1 = get_And_right(left);
4450 * And(x, C1) == C2 ==> FALSE if C2 & C1 != C2
4451 * And(x, C1) != C2 ==> TRUE if C2 & C1 != C2
4453 tarval *mask = tarval_and(get_Const_tarval(c1), tv);
4455 /* TODO: move to constant evaluation */
4456 tv = proj_nr == pn_Cmp_Eq ? get_tarval_b_false() : get_tarval_b_true();
4458 DBG_OPT_CSTEVAL(proj, c1);
4462 if (tarval_is_single_bit(tv)) {
4464 * optimization for AND:
4466 * And(x, C) == C ==> And(x, C) != 0
4467 * And(x, C) != C ==> And(X, C) == 0
4469 * if C is a single Bit constant.
4472 /* check for Constant's match. We have check hare the tarvals,
4473 because our const might be changed */
4474 if (get_Const_tarval(c1) == tv) {
4475 /* fine: do the transformation */
4476 tv = get_mode_null(get_tarval_mode(tv));
4477 proj_nr ^= pn_Cmp_Leg;
4479 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CNST_MAGN);
4485 c1 = get_Or_right(left);
4486 if (is_Const(c1) && tarval_is_null(tv)) {
4488 * Or(x, C) == 0 && C != 0 ==> FALSE
4489 * Or(x, C) != 0 && C != 0 ==> TRUE
4491 if (! tarval_is_null(get_Const_tarval(c1))) {
4492 /* TODO: move to constant evaluation */
4493 tv = proj_nr == pn_Cmp_Eq ? get_tarval_b_false() : get_tarval_b_true();
4495 DBG_OPT_CSTEVAL(proj, c1);
4502 * optimize x << c1 == c into x & (-1 >>u c1) == c >> c1 if c & (-1 << c1) == c
4504 * optimize x << c1 != c into x & (-1 >>u c1) != c >> c1 if c & (-1 << c1) == c
4507 c1 = get_Shl_right(left);
4509 tarval *tv1 = get_Const_tarval(c1);
4510 ir_mode *mode = get_irn_mode(left);
4511 tarval *minus1 = get_mode_all_one(mode);
4512 tarval *amask = tarval_shr(minus1, tv1);
4513 tarval *cmask = tarval_shl(minus1, tv1);
4516 if (tarval_and(tv, cmask) != tv) {
4517 /* condition not met */
4518 tv = proj_nr == pn_Cmp_Eq ? get_tarval_b_false() : get_tarval_b_true();
4520 DBG_OPT_CSTEVAL(proj, c1);
4523 sl = get_Shl_left(left);
4524 blk = get_nodes_block(n);
4525 left = new_rd_And(get_irn_dbg_info(left), current_ir_graph, blk, sl, new_Const(amask), mode);
4526 tv = tarval_shr(tv, tv1);
4528 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_SHF_TO_AND);
4533 * optimize x >>u c1 == c into x & (-1 << c1) == c << c1 if c & (-1 >>u c1) == c
4535 * optimize x >>u c1 != c into x & (-1 << c1) != c << c1 if c & (-1 >>u c1) == c
4538 c1 = get_Shr_right(left);
4540 tarval *tv1 = get_Const_tarval(c1);
4541 ir_mode *mode = get_irn_mode(left);
4542 tarval *minus1 = get_mode_all_one(mode);
4543 tarval *amask = tarval_shl(minus1, tv1);
4544 tarval *cmask = tarval_shr(minus1, tv1);
4547 if (tarval_and(tv, cmask) != tv) {
4548 /* condition not met */
4549 tv = proj_nr == pn_Cmp_Eq ? get_tarval_b_false() : get_tarval_b_true();
4551 DBG_OPT_CSTEVAL(proj, c1);
4554 sl = get_Shr_left(left);
4555 blk = get_nodes_block(n);
4556 left = new_rd_And(get_irn_dbg_info(left), current_ir_graph, blk, sl, new_Const(amask), mode);
4557 tv = tarval_shl(tv, tv1);
4559 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_SHF_TO_AND);
4564 * optimize x >>s c1 == c into x & (-1 << c1) == c << c1 if (c >>s (BITS - c1)) \in {0,-1}
4566 * optimize x >>s c1 != c into x & (-1 << c1) != c << c1 if (c >>s (BITS - c1)) \in {0,-1}
4569 c1 = get_Shrs_right(left);
4571 tarval *tv1 = get_Const_tarval(c1);
4572 ir_mode *mode = get_irn_mode(left);
4573 tarval *minus1 = get_mode_all_one(mode);
4574 tarval *amask = tarval_shl(minus1, tv1);
4575 tarval *cond = new_tarval_from_long(get_mode_size_bits(mode), get_tarval_mode(tv1));
4578 cond = tarval_sub(cond, tv1, NULL);
4579 cond = tarval_shrs(tv, cond);
4581 if (!tarval_is_all_one(cond) && !tarval_is_null(cond)) {
4582 /* condition not met */
4583 tv = proj_nr == pn_Cmp_Eq ? get_tarval_b_false() : get_tarval_b_true();
4585 DBG_OPT_CSTEVAL(proj, c1);
4588 sl = get_Shrs_left(left);
4589 blk = get_nodes_block(n);
4590 left = new_rd_And(get_irn_dbg_info(left), current_ir_graph, blk, sl, new_Const(amask), mode);
4591 tv = tarval_shl(tv, tv1);
4593 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_SHF_TO_AND);
4598 } /* tarval != bad */
4601 if (changed & 2) /* need a new Const */
4602 right = new_Const(tv);
4604 if ((proj_nr == pn_Cmp_Eq || proj_nr == pn_Cmp_Lg) && is_Const(right) && is_Const_null(right) && is_Proj(left)) {
4605 ir_node *op = get_Proj_pred(left);
4607 if ((is_Mod(op) && get_Proj_proj(left) == pn_Mod_res) ||
4608 (is_DivMod(op) && get_Proj_proj(left) == pn_DivMod_res_mod)) {
4609 ir_node *c = get_binop_right(op);
4612 tarval *tv = get_Const_tarval(c);
4614 if (tarval_is_single_bit(tv)) {
4615 /* special case: (x % 2^n) CMP 0 ==> x & (2^n-1) CMP 0 */
4616 ir_node *v = get_binop_left(op);
4617 ir_node *blk = get_irn_n(op, -1);
4618 ir_mode *mode = get_irn_mode(v);
4620 tv = tarval_sub(tv, get_mode_one(mode), NULL);
4621 left = new_rd_And(get_irn_dbg_info(op), current_ir_graph, blk, v, new_Const(tv), mode);
4623 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_MOD_TO_AND);
4630 ir_node *block = get_nodes_block(n);
4632 /* create a new compare */
4633 n = new_rd_Cmp(get_irn_dbg_info(n), current_ir_graph, block, left, right);
4634 proj = new_rd_Proj(get_irn_dbg_info(proj), current_ir_graph, block, n, get_irn_mode(proj), proj_nr);
4638 } /* transform_node_Proj_Cmp */
4641 * Optimize CopyB(mem, x, x) into a Nop.
4643 static ir_node *transform_node_Proj_CopyB(ir_node *proj) {
4644 ir_node *copyb = get_Proj_pred(proj);
4645 ir_node *a = get_CopyB_dst(copyb);
4646 ir_node *b = get_CopyB_src(copyb);
4649 switch (get_Proj_proj(proj)) {
4650 case pn_CopyB_X_regular:
4651 /* Turn CopyB into a tuple (mem, jmp, bad, bad) */
4652 DBG_OPT_EXC_REM(proj);
4653 proj = new_r_Jmp(current_ir_graph, get_nodes_block(copyb));
4655 case pn_CopyB_M_except:
4656 case pn_CopyB_X_except:
4657 DBG_OPT_EXC_REM(proj);
4658 proj = get_irg_bad(current_ir_graph);
4665 } /* transform_node_Proj_CopyB */
4668 * Optimize Bounds(idx, idx, upper) into idx.
4670 static ir_node *transform_node_Proj_Bound(ir_node *proj) {
4671 ir_node *oldn = proj;
4672 ir_node *bound = get_Proj_pred(proj);
4673 ir_node *idx = get_Bound_index(bound);
4674 ir_node *pred = skip_Proj(idx);
4677 if (idx == get_Bound_lower(bound))
4679 else if (is_Bound(pred)) {
4681 * idx was Bounds checked in the same MacroBlock previously,
4682 * it is still valid if lower <= pred_lower && pred_upper <= upper.
4684 ir_node *lower = get_Bound_lower(bound);
4685 ir_node *upper = get_Bound_upper(bound);
4686 if (get_Bound_lower(pred) == lower &&
4687 get_Bound_upper(pred) == upper &&
4688 get_irn_MacroBlock(bound) == get_irn_MacroBlock(pred)) {
4690 * One could expect that we simply return the previous
4691 * Bound here. However, this would be wrong, as we could
4692 * add an exception Proj to a new location then.
4693 * So, we must turn in into a tuple.
4699 /* Turn Bound into a tuple (mem, jmp, bad, idx) */
4700 switch (get_Proj_proj(proj)) {
4702 DBG_OPT_EXC_REM(proj);
4703 proj = get_Bound_mem(bound);
4705 case pn_Bound_X_except:
4706 DBG_OPT_EXC_REM(proj);
4707 proj = get_irg_bad(current_ir_graph);
4711 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NOP);
4713 case pn_Bound_X_regular:
4714 DBG_OPT_EXC_REM(proj);
4715 proj = new_r_Jmp(current_ir_graph, get_nodes_block(bound));
4722 } /* transform_node_Proj_Bound */
4725 * Does all optimizations on nodes that must be done on it's Proj's
4726 * because of creating new nodes.
4728 static ir_node *transform_node_Proj(ir_node *proj) {
4729 ir_node *n = get_Proj_pred(proj);
4731 if (n->op->ops.transform_node_Proj)
4732 return n->op->ops.transform_node_Proj(proj);
4734 } /* transform_node_Proj */
4737 * Move Confirms down through Phi nodes.
4739 static ir_node *transform_node_Phi(ir_node *phi) {
4741 ir_mode *mode = get_irn_mode(phi);
4743 if (mode_is_reference(mode)) {
4744 n = get_irn_arity(phi);
4746 /* Beware of Phi0 */
4748 ir_node *pred = get_irn_n(phi, 0);
4749 ir_node *bound, *new_Phi, *block, **in;
4752 if (! is_Confirm(pred))
4755 bound = get_Confirm_bound(pred);
4756 pnc = get_Confirm_cmp(pred);
4758 NEW_ARR_A(ir_node *, in, n);
4759 in[0] = get_Confirm_value(pred);
4761 for (i = 1; i < n; ++i) {
4762 pred = get_irn_n(phi, i);
4764 if (! is_Confirm(pred) ||
4765 get_Confirm_bound(pred) != bound ||
4766 get_Confirm_cmp(pred) != pnc)
4768 in[i] = get_Confirm_value(pred);
4770 /* move the Confirm nodes "behind" the Phi */
4771 block = get_irn_n(phi, -1);
4772 new_Phi = new_r_Phi(current_ir_graph, block, n, in, get_irn_mode(phi));
4773 return new_r_Confirm(current_ir_graph, block, new_Phi, bound, pnc);
4777 } /* transform_node_Phi */
4780 * Returns the operands of a commutative bin-op, if one operand is
4781 * a const, it is returned as the second one.
4783 static void get_comm_Binop_Ops(ir_node *binop, ir_node **a, ir_node **c) {
4784 ir_node *op_a = get_binop_left(binop);
4785 ir_node *op_b = get_binop_right(binop);
4787 assert(is_op_commutative(get_irn_op(binop)));
4789 if (is_Const(op_a)) {
4796 } /* get_comm_Binop_Ops */
4799 * Optimize a Or(And(Or(And(v,c4),c3),c2),c1) pattern if possible.
4800 * Such pattern may arise in bitfield stores.
4802 * value c4 value c4 & c2
4803 * AND c3 AND c1 | c3
4810 * AND c1 ===> OR if (c1 | c2) == 0x111..11
4813 static ir_node *transform_node_Or_bf_store(ir_node *or) {
4816 ir_node *and_l, *c3;
4817 ir_node *value, *c4;
4818 ir_node *new_and, *new_const, *block;
4819 ir_mode *mode = get_irn_mode(or);
4821 tarval *tv1, *tv2, *tv3, *tv4, *tv, *n_tv4, *n_tv2;
4824 get_comm_Binop_Ops(or, &and, &c1);
4825 if (!is_Const(c1) || !is_And(and))
4828 get_comm_Binop_Ops(and, &or_l, &c2);
4832 tv1 = get_Const_tarval(c1);
4833 tv2 = get_Const_tarval(c2);
4835 tv = tarval_or(tv1, tv2);
4836 if (tarval_is_all_one(tv)) {
4837 /* the AND does NOT clear a bit with isn't set by the OR */
4838 set_Or_left(or, or_l);
4839 set_Or_right(or, c1);
4841 /* check for more */
4848 get_comm_Binop_Ops(or_l, &and_l, &c3);
4849 if (!is_Const(c3) || !is_And(and_l))
4852 get_comm_Binop_Ops(and_l, &value, &c4);
4856 /* ok, found the pattern, check for conditions */
4857 assert(mode == get_irn_mode(and));
4858 assert(mode == get_irn_mode(or_l));
4859 assert(mode == get_irn_mode(and_l));
4861 tv3 = get_Const_tarval(c3);
4862 tv4 = get_Const_tarval(c4);
4864 tv = tarval_or(tv4, tv2);
4865 if (!tarval_is_all_one(tv)) {
4866 /* have at least one 0 at the same bit position */
4870 n_tv4 = tarval_not(tv4);
4871 if (tv3 != tarval_and(tv3, n_tv4)) {
4872 /* bit in the or_mask is outside the and_mask */
4876 n_tv2 = tarval_not(tv2);
4877 if (tv1 != tarval_and(tv1, n_tv2)) {
4878 /* bit in the or_mask is outside the and_mask */
4882 /* ok, all conditions met */
4883 block = get_irn_n(or, -1);
4885 new_and = new_r_And(current_ir_graph, block,
4886 value, new_Const(tarval_and(tv4, tv2)), mode);
4888 new_const = new_Const(tarval_or(tv3, tv1));
4890 set_Or_left(or, new_and);
4891 set_Or_right(or, new_const);
4893 /* check for more */
4895 } /* transform_node_Or_bf_store */
4898 * Optimize an Or(shl(x, c), shr(x, bits - c)) into a Rotl
4900 static ir_node *transform_node_Or_Rotl(ir_node *or) {
4901 ir_mode *mode = get_irn_mode(or);
4902 ir_node *shl, *shr, *block;
4903 ir_node *irn, *x, *c1, *c2, *v, *sub, *n, *rotval;
4906 if (! mode_is_int(mode))
4909 shl = get_binop_left(or);
4910 shr = get_binop_right(or);
4919 } else if (!is_Shl(shl)) {
4921 } else if (!is_Shr(shr)) {
4924 x = get_Shl_left(shl);
4925 if (x != get_Shr_left(shr))
4928 c1 = get_Shl_right(shl);
4929 c2 = get_Shr_right(shr);
4930 if (is_Const(c1) && is_Const(c2)) {
4931 tv1 = get_Const_tarval(c1);
4932 if (! tarval_is_long(tv1))
4935 tv2 = get_Const_tarval(c2);
4936 if (! tarval_is_long(tv2))
4939 if (get_tarval_long(tv1) + get_tarval_long(tv2)
4940 != (int) get_mode_size_bits(mode))
4943 /* yet, condition met */
4944 block = get_nodes_block(or);
4946 n = new_r_Rotl(current_ir_graph, block, x, c1, mode);
4948 DBG_OPT_ALGSIM1(or, shl, shr, n, FS_OPT_OR_SHFT_TO_ROTL);
4955 rotval = sub; /* a Rot right is not supported, so use a rot left */
4956 } else if (is_Sub(c2)) {
4962 if (get_Sub_right(sub) != v)
4965 c1 = get_Sub_left(sub);
4969 tv1 = get_Const_tarval(c1);
4970 if (! tarval_is_long(tv1))
4973 if (get_tarval_long(tv1) != (int) get_mode_size_bits(mode))
4976 /* yet, condition met */
4977 block = get_nodes_block(or);
4979 n = new_r_Rotl(current_ir_graph, block, x, rotval, mode);
4981 DBG_OPT_ALGSIM0(or, n, FS_OPT_OR_SHFT_TO_ROTL);
4983 } /* transform_node_Or_Rotl */
4988 static ir_node *transform_node_Or(ir_node *n) {
4989 ir_node *c, *oldn = n;
4990 ir_node *a = get_Or_left(n);
4991 ir_node *b = get_Or_right(n);
4994 if (is_Not(a) && is_Not(b)) {
4995 /* ~a | ~b = ~(a&b) */
4996 ir_node *block = get_nodes_block(n);
4998 mode = get_irn_mode(n);
5001 n = new_rd_And(get_irn_dbg_info(n), current_ir_graph, block, a, b, mode);
5002 n = new_rd_Not(get_irn_dbg_info(n), current_ir_graph, block, n, mode);
5003 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_DEMORGAN);
5007 /* we can evaluate 2 Projs of the same Cmp */
5008 if (get_irn_mode(n) == mode_b && is_Proj(a) && is_Proj(b)) {
5009 ir_node *pred_a = get_Proj_pred(a);
5010 ir_node *pred_b = get_Proj_pred(b);
5011 if (pred_a == pred_b) {
5012 dbg_info *dbgi = get_irn_dbg_info(n);
5013 ir_node *block = get_nodes_block(pred_a);
5014 pn_Cmp pn_a = get_Proj_proj(a);
5015 pn_Cmp pn_b = get_Proj_proj(b);
5016 /* yes, we can simply calculate with pncs */
5017 pn_Cmp new_pnc = pn_a | pn_b;
5019 return new_rd_Proj(dbgi, current_ir_graph, block, pred_a, mode_b,
5024 mode = get_irn_mode(n);
5025 HANDLE_BINOP_PHI(tarval_or, a, b, c, mode);
5027 n = transform_node_Or_bf_store(n);
5028 n = transform_node_Or_Rotl(n);
5032 n = transform_bitwise_distributive(n, transform_node_Or);
5035 } /* transform_node_Or */
5039 static ir_node *transform_node(ir_node *n);
5042 * Optimize (a >> c1) >> c2), works for Shr, Shrs, Shl, Rotl.
5044 * Should be moved to reassociation?
5046 static ir_node *transform_node_shift(ir_node *n) {
5047 ir_node *left, *right;
5049 tarval *tv1, *tv2, *res;
5050 ir_node *in[2], *irn, *block;
5052 left = get_binop_left(n);
5054 /* different operations */
5055 if (get_irn_op(left) != get_irn_op(n))
5058 right = get_binop_right(n);
5059 tv1 = value_of(right);
5060 if (tv1 == tarval_bad)
5063 tv2 = value_of(get_binop_right(left));
5064 if (tv2 == tarval_bad)
5067 res = tarval_add(tv1, tv2);
5068 mode = get_irn_mode(n);
5070 /* beware: a simple replacement works only, if res < modulo shift */
5072 int modulo_shf = get_mode_modulo_shift(mode);
5073 if (modulo_shf > 0) {
5074 tarval *modulo = new_tarval_from_long(modulo_shf,
5075 get_tarval_mode(res));
5077 assert(modulo_shf >= (int) get_mode_size_bits(mode));
5079 /* shifting too much */
5080 if (!(tarval_cmp(res, modulo) & pn_Cmp_Lt)) {
5082 ir_graph *irg = get_irn_irg(n);
5083 ir_node *block = get_nodes_block(n);
5084 dbg_info *dbgi = get_irn_dbg_info(n);
5085 ir_mode *smode = get_irn_mode(right);
5086 ir_node *cnst = new_Const_long(smode, get_mode_size_bits(mode) - 1);
5087 return new_rd_Shrs(dbgi, irg, block, get_binop_left(left),
5091 return new_Const(get_mode_null(mode));
5095 res = tarval_mod(res, new_tarval_from_long(get_mode_size_bits(mode), get_tarval_mode(res)));
5098 /* ok, we can replace it */
5099 block = get_nodes_block(n);
5101 in[0] = get_binop_left(left);
5102 in[1] = new_Const(res);
5104 irn = new_ir_node(NULL, current_ir_graph, block, get_irn_op(n), mode, 2, in);
5106 DBG_OPT_ALGSIM0(n, irn, FS_OPT_REASSOC_SHIFT);
5108 return transform_node(irn);
5109 } /* transform_node_shift */
5112 * normalisation: (x & c1) >> c2 to (x >> c2) & (c1 >> c2)
5114 * - and, or, xor instead of &
5115 * - Shl, Shr, Shrs, rotl instead of >>
5116 * (with a special case for Or/Xor + Shrs)
5118 static ir_node *transform_node_bitop_shift(ir_node *n) {
5120 ir_node *right = get_binop_right(n);
5121 ir_mode *mode = get_irn_mode(n);
5122 ir_node *bitop_left;
5123 ir_node *bitop_right;
5135 assert(is_Shrs(n) || is_Shr(n) || is_Shl(n) || is_Rotl(n));
5137 if (!is_Const(right))
5140 left = get_binop_left(n);
5141 op_left = get_irn_op(left);
5142 if (op_left != op_And && op_left != op_Or && op_left != op_Eor)
5145 /* doing it with Shrs is not legal if the Or/Eor affects the topmost bit */
5146 if (is_Shrs(n) && (op_left == op_Or || op_left == op_Eor)) {
5147 /* TODO: test if sign bit is affectes */
5151 bitop_right = get_binop_right(left);
5152 if (!is_Const(bitop_right))
5155 bitop_left = get_binop_left(left);
5157 irg = get_irn_irg(n);
5158 block = get_nodes_block(n);
5159 dbgi = get_irn_dbg_info(n);
5160 tv1 = get_Const_tarval(bitop_right);
5161 tv2 = get_Const_tarval(right);
5163 assert(get_tarval_mode(tv1) == mode);
5166 new_shift = new_rd_Shl(dbgi, irg, block, bitop_left, right, mode);
5167 tv_shift = tarval_shl(tv1, tv2);
5168 } else if(is_Shr(n)) {
5169 new_shift = new_rd_Shr(dbgi, irg, block, bitop_left, right, mode);
5170 tv_shift = tarval_shr(tv1, tv2);
5171 } else if(is_Shrs(n)) {
5172 new_shift = new_rd_Shrs(dbgi, irg, block, bitop_left, right, mode);
5173 tv_shift = tarval_shrs(tv1, tv2);
5176 new_shift = new_rd_Rotl(dbgi, irg, block, bitop_left, right, mode);
5177 tv_shift = tarval_rotl(tv1, tv2);
5180 assert(get_tarval_mode(tv_shift) == mode);
5181 new_const = new_Const(tv_shift);
5183 if (op_left == op_And) {
5184 new_bitop = new_rd_And(dbgi, irg, block, new_shift, new_const, mode);
5185 } else if(op_left == op_Or) {
5186 new_bitop = new_rd_Or(dbgi, irg, block, new_shift, new_const, mode);
5188 assert(op_left == op_Eor);
5189 new_bitop = new_rd_Eor(dbgi, irg, block, new_shift, new_const, mode);
5197 * (x << c1) >> c2 <=> x OP (c2-c1) & ((-1 << c1) >> c2)
5199 * (x >> c1) << c2 <=> x OP (c2-c1) & ((-1 >> c1) << c2)
5200 * (also with x >>s c1 when c1>=c2)
5202 static ir_node *transform_node_shl_shr(ir_node *n) {
5204 ir_node *right = get_binop_right(n);
5220 assert(is_Shl(n) || is_Shr(n) || is_Shrs(n));
5222 if (!is_Const(right))
5225 left = get_binop_left(n);
5226 mode = get_irn_mode(n);
5227 if (is_Shl(n) && (is_Shr(left) || is_Shrs(left))) {
5228 ir_node *shr_right = get_binop_right(left);
5230 if (!is_Const(shr_right))
5233 x = get_binop_left(left);
5234 tv_shr = get_Const_tarval(shr_right);
5235 tv_shl = get_Const_tarval(right);
5237 if (is_Shrs(left)) {
5238 /* shrs variant only allowed if c1 >= c2 */
5239 if (! (tarval_cmp(tv_shl, tv_shr) & pn_Cmp_Ge))
5242 tv_mask = tarval_shrs(get_mode_all_one(mode), tv_shr);
5245 tv_mask = tarval_shr(get_mode_all_one(mode), tv_shr);
5247 tv_mask = tarval_shl(tv_mask, tv_shl);
5248 } else if(is_Shr(n) && is_Shl(left)) {
5249 ir_node *shl_right = get_Shl_right(left);
5251 if (!is_Const(shl_right))
5254 x = get_Shl_left(left);
5255 tv_shr = get_Const_tarval(right);
5256 tv_shl = get_Const_tarval(shl_right);
5258 tv_mask = tarval_shl(get_mode_all_one(mode), tv_shl);
5259 tv_mask = tarval_shr(tv_mask, tv_shr);
5264 if (get_tarval_mode(tv_shl) != get_tarval_mode(tv_shr)) {
5265 tv_shl = tarval_convert_to(tv_shl, get_tarval_mode(tv_shr));
5268 assert(tv_mask != tarval_bad);
5269 assert(get_tarval_mode(tv_mask) == mode);
5271 irg = get_irn_irg(n);
5272 block = get_nodes_block(n);
5273 dbgi = get_irn_dbg_info(n);
5275 pnc = tarval_cmp(tv_shl, tv_shr);
5276 if (pnc == pn_Cmp_Lt || pnc == pn_Cmp_Eq) {
5277 tv_shift = tarval_sub(tv_shr, tv_shl, NULL);
5278 new_const = new_Const(tv_shift);
5280 new_shift = new_rd_Shrs(dbgi, irg, block, x, new_const, mode);
5282 new_shift = new_rd_Shr(dbgi, irg, block, x, new_const, mode);
5285 assert(pnc == pn_Cmp_Gt);
5286 tv_shift = tarval_sub(tv_shl, tv_shr, NULL);
5287 new_const = new_Const(tv_shift);
5288 new_shift = new_rd_Shl(dbgi, irg, block, x, new_const, mode);
5291 new_const = new_Const(tv_mask);
5292 new_and = new_rd_And(dbgi, irg, block, new_shift, new_const, mode);
5300 static ir_node *transform_node_Shr(ir_node *n) {
5301 ir_node *c, *oldn = n;
5302 ir_node *left = get_Shr_left(n);
5303 ir_node *right = get_Shr_right(n);
5304 ir_mode *mode = get_irn_mode(n);
5306 HANDLE_BINOP_PHI(tarval_shr, left, right, c, mode);
5307 n = transform_node_shift(n);
5310 n = transform_node_shl_shr(n);
5312 n = transform_node_bitop_shift(n);
5315 } /* transform_node_Shr */
5320 static ir_node *transform_node_Shrs(ir_node *n) {
5321 ir_node *c, *oldn = n;
5322 ir_node *a = get_Shrs_left(n);
5323 ir_node *b = get_Shrs_right(n);
5324 ir_mode *mode = get_irn_mode(n);
5326 HANDLE_BINOP_PHI(tarval_shrs, a, b, c, mode);
5327 n = transform_node_shift(n);
5330 n = transform_node_bitop_shift(n);
5333 } /* transform_node_Shrs */
5338 static ir_node *transform_node_Shl(ir_node *n) {
5339 ir_node *c, *oldn = n;
5340 ir_node *a = get_Shl_left(n);
5341 ir_node *b = get_Shl_right(n);
5342 ir_mode *mode = get_irn_mode(n);
5344 HANDLE_BINOP_PHI(tarval_shl, a, b, c, mode);
5345 n = transform_node_shift(n);
5348 n = transform_node_shl_shr(n);
5350 n = transform_node_bitop_shift(n);
5353 } /* transform_node_Shl */
5358 static ir_node *transform_node_Rotl(ir_node *n) {
5359 ir_node *c, *oldn = n;
5360 ir_node *a = get_Rotl_left(n);
5361 ir_node *b = get_Rotl_right(n);
5362 ir_mode *mode = get_irn_mode(n);
5364 HANDLE_BINOP_PHI(tarval_rotl, a, b, c, mode);
5365 n = transform_node_shift(n);
5368 n = transform_node_bitop_shift(n);
5371 } /* transform_node_Rotl */
5376 static ir_node *transform_node_Conv(ir_node *n) {
5377 ir_node *c, *oldn = n;
5378 ir_mode *mode = get_irn_mode(n);
5379 ir_node *a = get_Conv_op(n);
5381 if (mode != mode_b && is_const_Phi(a)) {
5382 /* Do NOT optimize mode_b Conv's, this leads to remaining
5383 * Phib nodes later, because the conv_b_lower operation
5384 * is instantly reverted, when it tries to insert a Convb.
5386 c = apply_conv_on_phi(a, mode);
5388 DBG_OPT_ALGSIM0(oldn, c, FS_OPT_CONST_PHI);
5393 if (is_Unknown(a)) { /* Conv_A(Unknown_B) -> Unknown_A */
5394 return new_r_Unknown(current_ir_graph, mode);
5397 if (mode_is_reference(mode) &&
5398 get_mode_size_bits(mode) == get_mode_size_bits(get_irn_mode(a)) &&
5400 ir_node *l = get_Add_left(a);
5401 ir_node *r = get_Add_right(a);
5402 dbg_info *dbgi = get_irn_dbg_info(a);
5403 ir_node *block = get_nodes_block(n);
5405 ir_node *lop = get_Conv_op(l);
5406 if(get_irn_mode(lop) == mode) {
5407 /* ConvP(AddI(ConvI(P), x)) -> AddP(P, x) */
5408 n = new_rd_Add(dbgi, current_ir_graph, block, lop, r, mode);
5413 ir_node *rop = get_Conv_op(r);
5414 if(get_irn_mode(rop) == mode) {
5415 /* ConvP(AddI(x, ConvI(P))) -> AddP(x, P) */
5416 n = new_rd_Add(dbgi, current_ir_graph, block, l, rop, mode);
5423 } /* transform_node_Conv */
5426 * Remove dead blocks and nodes in dead blocks
5427 * in keep alive list. We do not generate a new End node.
5429 static ir_node *transform_node_End(ir_node *n) {
5430 int i, j, n_keepalives = get_End_n_keepalives(n);
5433 NEW_ARR_A(ir_node *, in, n_keepalives);
5435 for (i = j = 0; i < n_keepalives; ++i) {
5436 ir_node *ka = get_End_keepalive(n, i);
5438 if (! is_Block_dead(ka)) {
5442 } else if (is_irn_pinned_in_irg(ka) && is_Block_dead(get_nodes_block(ka))) {
5444 } else if (is_Bad(ka)) {
5445 /* no need to keep Bad */
5450 if (j != n_keepalives)
5451 set_End_keepalives(n, j, in);
5453 } /* transform_node_End */
5455 /** returns 1 if a == -b */
5456 static int is_negated_value(ir_node *a, ir_node *b) {
5457 if (is_Minus(a) && get_Minus_op(a) == b)
5459 if (is_Minus(b) && get_Minus_op(b) == a)
5461 if (is_Sub(a) && is_Sub(b)) {
5462 ir_node *a_left = get_Sub_left(a);
5463 ir_node *a_right = get_Sub_right(a);
5464 ir_node *b_left = get_Sub_left(b);
5465 ir_node *b_right = get_Sub_right(b);
5467 if (a_left == b_right && a_right == b_left)
5475 * Optimize a Mux into some simpler cases.
5477 static ir_node *transform_node_Mux(ir_node *n) {
5478 ir_node *oldn = n, *sel = get_Mux_sel(n);
5479 ir_mode *mode = get_irn_mode(n);
5480 ir_node *t = get_Mux_true(n);
5481 ir_node *f = get_Mux_false(n);
5482 ir_graph *irg = current_ir_graph;
5484 if (is_irg_state(irg, IR_GRAPH_STATE_KEEP_MUX))
5488 ir_node* block = get_nodes_block(n);
5490 ir_node* c1 = get_Mux_sel(t);
5491 ir_node* t1 = get_Mux_true(t);
5492 ir_node* f1 = get_Mux_false(t);
5494 /* Mux(cond0, Mux(cond1, x, y), y) -> typical if (cond0 && cond1) x else y */
5495 ir_node* and_ = new_r_And(irg, block, c0, c1, mode_b);
5496 ir_node* new_mux = new_r_Mux(irg, block, and_, f1, t1, mode);
5501 DBG_OPT_ALGSIM0(oldn, t, FS_OPT_MUX_COMBINE);
5502 } else if (f == t1) {
5503 /* Mux(cond0, Mux(cond1, x, y), x) */
5504 ir_node* not_c1 = new_r_Not(irg, block, c1, mode_b);
5505 ir_node* and_ = new_r_And(irg, block, c0, not_c1, mode_b);
5506 ir_node* new_mux = new_r_Mux(irg, block, and_, t1, f1, mode);
5511 DBG_OPT_ALGSIM0(oldn, t, FS_OPT_MUX_COMBINE);
5513 } else if (is_Mux(f)) {
5514 ir_node* block = get_nodes_block(n);
5516 ir_node* c1 = get_Mux_sel(f);
5517 ir_node* t1 = get_Mux_true(f);
5518 ir_node* f1 = get_Mux_false(f);
5520 /* Mux(cond0, x, Mux(cond1, x, y)) -> typical if (cond0 || cond1) x else y */
5521 ir_node* or_ = new_r_Or(irg, block, c0, c1, mode_b);
5522 ir_node* new_mux = new_r_Mux(irg, block, or_, f1, t1, mode);
5527 DBG_OPT_ALGSIM0(oldn, f, FS_OPT_MUX_COMBINE);
5528 } else if (t == f1) {
5529 /* Mux(cond0, x, Mux(cond1, y, x)) */
5530 ir_node* not_c1 = new_r_Not(irg, block, c1, mode_b);
5531 ir_node* or_ = new_r_Or(irg, block, c0, not_c1, mode_b);
5532 ir_node* new_mux = new_r_Mux(irg, block, or_, t1, f1, mode);
5537 DBG_OPT_ALGSIM0(oldn, f, FS_OPT_MUX_COMBINE);
5541 /* first normalization step: move a possible zero to the false case */
5543 ir_node *cmp = get_Proj_pred(sel);
5546 if (is_Const(t) && is_Const_null(t)) {
5549 /* Mux(x, 0, y) => Mux(x, y, 0) */
5550 pn_Cmp pnc = get_Proj_proj(sel);
5551 sel = new_r_Proj(irg, get_nodes_block(cmp), cmp, mode_b,
5552 get_negated_pnc(pnc, get_irn_mode(get_Cmp_left(cmp))));
5553 n = new_rd_Mux(get_irn_dbg_info(n), irg, get_nodes_block(n), sel, t, f, mode);
5561 /* note: after normalization, false can only happen on default */
5562 if (mode == mode_b) {
5563 dbg_info *dbg = get_irn_dbg_info(n);
5564 ir_node *block = get_nodes_block(n);
5565 ir_graph *irg = current_ir_graph;
5568 tarval *tv_t = get_Const_tarval(t);
5569 if (tv_t == tarval_b_true) {
5571 /* Muxb(sel, true, false) = sel */
5572 assert(get_Const_tarval(f) == tarval_b_false);
5573 DBG_OPT_ALGSIM0(oldn, sel, FS_OPT_MUX_BOOL);
5576 /* Muxb(sel, true, x) = Or(sel, x) */
5577 n = new_rd_Or(dbg, irg, block, sel, f, mode_b);
5578 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_OR_BOOL);
5582 } else if (is_Const(f)) {
5583 tarval *tv_f = get_Const_tarval(f);
5584 if (tv_f == tarval_b_true) {
5585 /* Muxb(sel, x, true) = Or(Not(sel), x) */
5586 ir_node* not_sel = new_rd_Not(dbg, irg, block, sel, mode_b);
5587 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_ORNOT_BOOL);
5588 n = new_rd_Or(dbg, irg, block, not_sel, t, mode_b);
5591 /* Muxb(sel, x, false) = And(sel, x) */
5592 assert(tv_f == tarval_b_false);
5593 n = new_rd_And(dbg, irg, block, sel, t, mode_b);
5594 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_AND_BOOL);
5600 /* more normalization: try to normalize Mux(x, C1, C2) into Mux(x, +1/-1, 0) op C2 */
5601 if (is_Const(t) && is_Const(f) && mode_is_int(mode)) {
5602 tarval *a = get_Const_tarval(t);
5603 tarval *b = get_Const_tarval(f);
5606 if (tarval_is_one(a) && tarval_is_null(b)) {
5607 ir_node *block = get_nodes_block(n);
5608 ir_node *conv = new_r_Conv(current_ir_graph, block, sel, mode);
5610 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_CONV);
5612 } else if (tarval_is_null(a) && tarval_is_one(b)) {
5613 ir_node *block = get_nodes_block(n);
5614 ir_node *not_ = new_r_Not(current_ir_graph, block, sel, mode_b);
5615 ir_node *conv = new_r_Conv(current_ir_graph, block, not_, mode);
5617 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_CONV);
5620 /* TODO: it's not really clear if that helps in general or should be moved
5621 * to backend, especially with the MUX->Conv transformation above */
5622 if (tarval_cmp(a, b) & pn_Cmp_Gt) {
5623 diff = tarval_sub(a, b, NULL);
5626 diff = tarval_sub(b, a, NULL);
5630 if (diff == get_tarval_one(mode)) {
5631 dbg_info *dbg = get_irn_dbg_info(n);
5632 ir_node *block = get_nodes_block(n);
5633 ir_node *t = new_Const(tarval_sub(a, min, NULL));
5634 ir_node *f = new_Const(tarval_sub(b, min, NULL));
5635 n = new_rd_Mux(dbg, irg, block, sel, f, t, mode);
5636 n = new_rd_Add(dbg, irg, block, n, new_Const(min), mode);
5642 ir_node *cmp = get_Proj_pred(sel);
5643 long pn = get_Proj_proj(sel);
5646 * Note: normalization puts the constant on the right side,
5647 * so we check only one case.
5649 * Note further that these optimization work even for floating point
5650 * with NaN's because -NaN == NaN.
5651 * However, if +0 and -0 is handled differently, we cannot use the Abs/-Abs
5655 ir_node *cmp_r = get_Cmp_right(cmp);
5656 if (is_Const(cmp_r) && is_Const_null(cmp_r)) {
5657 ir_node *block = get_nodes_block(n);
5658 ir_node *cmp_l = get_Cmp_left(cmp);
5660 if (!mode_honor_signed_zeros(mode) && is_negated_value(f, t)) {
5663 if ( (cmp_l == t && (pn == pn_Cmp_Ge || pn == pn_Cmp_Gt))
5664 || (cmp_l == f && (pn == pn_Cmp_Le || pn == pn_Cmp_Lt)))
5666 /* Mux(a >/>= 0, a, -a) = Mux(a </<= 0, -a, a) ==> Abs(a) */
5667 n = new_rd_Abs(get_irn_dbg_info(n), current_ir_graph, block,
5669 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_ABS);
5671 } else if ((cmp_l == t && (pn == pn_Cmp_Le || pn == pn_Cmp_Lt))
5672 || (cmp_l == f && (pn == pn_Cmp_Ge || pn == pn_Cmp_Gt)))
5674 /* Mux(a </<= 0, a, -a) = Mux(a >/>= 0, -a, a) ==> -Abs(a) */
5675 n = new_rd_Abs(get_irn_dbg_info(n), current_ir_graph, block,
5677 n = new_rd_Minus(get_irn_dbg_info(n), current_ir_graph,
5679 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_ABS);
5684 if (mode_is_int(mode)) {
5686 if ((pn == pn_Cmp_Lg || pn == pn_Cmp_Eq) && is_And(cmp_l)) {
5687 /* Mux((a & b) != 0, c, 0) */
5688 ir_node *and_r = get_And_right(cmp_l);
5691 if (and_r == t && f == cmp_r) {
5692 if (is_Const(t) && tarval_is_single_bit(get_Const_tarval(t))) {
5693 if (pn == pn_Cmp_Lg) {
5694 /* Mux((a & 2^C) != 0, 2^C, 0) */
5696 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_BITOP);
5698 /* Mux((a & 2^C) == 0, 2^C, 0) */
5699 n = new_rd_Eor(get_irn_dbg_info(n), current_ir_graph,
5700 block, cmp_l, t, mode);
5701 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_BITOP);
5706 if (is_Shl(and_r)) {
5707 ir_node *shl_l = get_Shl_left(and_r);
5708 if (is_Const(shl_l) && is_Const_one(shl_l)) {
5709 if (and_r == t && f == cmp_r) {
5710 if (pn == pn_Cmp_Lg) {
5711 /* (a & (1 << n)) != 0, (1 << n), 0) */
5713 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_BITOP);
5715 /* (a & (1 << n)) == 0, (1 << n), 0) */
5716 n = new_rd_Eor(get_irn_dbg_info(n), current_ir_graph,
5717 block, cmp_l, t, mode);
5718 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_BITOP);
5724 and_l = get_And_left(cmp_l);
5725 if (is_Shl(and_l)) {
5726 ir_node *shl_l = get_Shl_left(and_l);
5727 if (is_Const(shl_l) && is_Const_one(shl_l)) {
5728 if (and_l == t && f == cmp_r) {
5729 if (pn == pn_Cmp_Lg) {
5730 /* ((1 << n) & a) != 0, (1 << n), 0) */
5732 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_BITOP);
5734 /* ((1 << n) & a) == 0, (1 << n), 0) */
5735 n = new_rd_Eor(get_irn_dbg_info(n), current_ir_graph,
5736 block, cmp_l, t, mode);
5737 DBG_OPT_ALGSIM1(oldn, cmp, sel, n, FS_OPT_MUX_TO_BITOP);
5750 } /* transform_node_Mux */
5753 * optimize Sync nodes that have other syncs as input we simply add the inputs
5754 * of the other sync to our own inputs
5756 static ir_node *transform_node_Sync(ir_node *n) {
5757 int arity = get_Sync_n_preds(n);
5760 for (i = 0; i < arity;) {
5761 ir_node *pred = get_Sync_pred(n, i);
5765 if (!is_Sync(pred)) {
5773 pred_arity = get_Sync_n_preds(pred);
5774 for (j = 0; j < pred_arity; ++j) {
5775 ir_node *pred_pred = get_Sync_pred(pred, j);
5780 add_irn_n(n, pred_pred);
5784 if (get_Sync_pred(n, k) == pred_pred) break;
5789 /* rehash the sync node */
5790 add_identities(current_ir_graph->value_table, n);
5793 } /* transform_node_Sync */
5796 * optimize a trampoline Call into a direct Call
5798 static ir_node *transform_node_Call(ir_node *call) {
5799 ir_node *callee = get_Call_ptr(call);
5800 ir_node *adr, *mem, *res, *bl, **in;
5801 ir_type *ctp, *mtp, *tp;
5804 int i, n_res, n_param;
5807 if (! is_Proj(callee))
5809 callee = get_Proj_pred(callee);
5810 if (! is_Builtin(callee))
5812 if (get_Builtin_kind(callee) != ir_bk_inner_trampoline)
5815 mem = get_Call_mem(call);
5817 if (skip_Proj(mem) == callee) {
5818 /* memory is routed to the trampoline, skip */
5819 mem = get_Builtin_mem(callee);
5822 /* build a new call type */
5823 mtp = get_Call_type(call);
5824 id = get_type_ident(mtp);
5825 id = id_mangle(new_id_from_chars("T_", 2), id);
5826 db = get_type_dbg_info(mtp);
5828 n_res = get_method_n_ress(mtp);
5829 n_param = get_method_n_params(mtp);
5830 ctp = new_d_type_method(id, n_param + 1, n_res, db);
5832 for (i = 0; i < n_res; ++i)
5833 set_method_res_type(ctp, i, get_method_res_type(mtp, i));
5835 NEW_ARR_A(ir_node *, in, n_param + 1);
5837 /* FIXME: we don't need a new pointer type in every step */
5838 tp = get_irg_frame_type(current_ir_graph);
5839 id = id_mangle(get_type_ident(tp), new_id_from_chars("_ptr", 4));
5840 tp = new_type_pointer(id, tp, mode_P_data);
5841 set_method_param_type(ctp, 0, tp);
5843 in[0] = get_Builtin_param(callee, 2);
5844 for (i = 0; i < n_param; ++i) {
5845 set_method_param_type(ctp, i + 1, get_method_param_type(mtp, i));
5846 in[i + 1] = get_Call_param(call, i);
5848 var = get_method_variadicity(mtp);
5849 set_method_variadicity(ctp, var);
5850 if (var == variadicity_variadic) {
5851 set_method_first_variadic_param_index(ctp, get_method_first_variadic_param_index(mtp) + 1);
5853 /* When we resolve a trampoline, the function must be called by a this-call */
5854 set_method_calling_convention(ctp, get_method_calling_convention(mtp) | cc_this_call);
5855 set_method_additional_properties(ctp, get_method_additional_properties(mtp));
5857 adr = get_Builtin_param(callee, 1);
5859 db = get_irn_dbg_info(call);
5860 bl = get_nodes_block(call);
5862 res = new_rd_Call(db, current_ir_graph, bl, mem, adr, n_param + 1, in, ctp);
5863 if (get_irn_pinned(call) == op_pin_state_floats)
5864 set_irn_pinned(res, op_pin_state_floats);
5866 } /* transform_node_Call */
5869 * Tries several [inplace] [optimizing] transformations and returns an
5870 * equivalent node. The difference to equivalent_node() is that these
5871 * transformations _do_ generate new nodes, and thus the old node must
5872 * not be freed even if the equivalent node isn't the old one.
5874 static ir_node *transform_node(ir_node *n) {
5878 * Transform_node is the only "optimizing transformation" that might
5879 * return a node with a different opcode. We iterate HERE until fixpoint
5880 * to get the final result.
5884 if (n->op->ops.transform_node != NULL)
5885 n = n->op->ops.transform_node(n);
5886 } while (oldn != n);
5889 } /* transform_node */
5892 * Sets the default transform node operation for an ir_op_ops.
5894 * @param code the opcode for the default operation
5895 * @param ops the operations initialized
5900 static ir_op_ops *firm_set_default_transform_node(ir_opcode code, ir_op_ops *ops)
5904 ops->transform_node = transform_node_##a; \
5906 #define CASE_PROJ(a) \
5908 ops->transform_node_Proj = transform_node_Proj_##a; \
5910 #define CASE_PROJ_EX(a) \
5912 ops->transform_node = transform_node_##a; \
5913 ops->transform_node_Proj = transform_node_Proj_##a; \
5922 CASE_PROJ_EX(DivMod);
5957 } /* firm_set_default_transform_node */
5960 /* **************** Common Subexpression Elimination **************** */
5962 /** The size of the hash table used, should estimate the number of nodes
5964 #define N_IR_NODES 512
5966 /** Compares the attributes of two Const nodes. */
5967 static int node_cmp_attr_Const(ir_node *a, ir_node *b) {
5968 return (get_Const_tarval(a) != get_Const_tarval(b))
5969 || (get_Const_type(a) != get_Const_type(b));
5970 } /* node_cmp_attr_Const */
5972 /** Compares the attributes of two Proj nodes. */
5973 static int node_cmp_attr_Proj(ir_node *a, ir_node *b) {
5974 return get_irn_proj_attr(a) != get_irn_proj_attr(b);
5975 } /* node_cmp_attr_Proj */
5977 /** Compares the attributes of two Filter nodes. */
5978 static int node_cmp_attr_Filter(ir_node *a, ir_node *b) {
5979 return get_Filter_proj(a) != get_Filter_proj(b);
5980 } /* node_cmp_attr_Filter */
5982 /** Compares the attributes of two Alloc nodes. */
5983 static int node_cmp_attr_Alloc(ir_node *a, ir_node *b) {
5984 const alloc_attr *pa = get_irn_alloc_attr(a);
5985 const alloc_attr *pb = get_irn_alloc_attr(b);
5986 return (pa->where != pb->where) || (pa->type != pb->type);
5987 } /* node_cmp_attr_Alloc */
5989 /** Compares the attributes of two Free nodes. */
5990 static int node_cmp_attr_Free(ir_node *a, ir_node *b) {
5991 const free_attr *pa = get_irn_free_attr(a);
5992 const free_attr *pb = get_irn_free_attr(b);
5993 return (pa->where != pb->where) || (pa->type != pb->type);
5994 } /* node_cmp_attr_Free */
5996 /** Compares the attributes of two SymConst nodes. */
5997 static int node_cmp_attr_SymConst(ir_node *a, ir_node *b) {
5998 const symconst_attr *pa = get_irn_symconst_attr(a);
5999 const symconst_attr *pb = get_irn_symconst_attr(b);
6000 return (pa->kind != pb->kind)
6001 || (pa->sym.type_p != pb->sym.type_p)
6002 || (pa->tp != pb->tp);
6003 } /* node_cmp_attr_SymConst */
6005 /** Compares the attributes of two Call nodes. */
6006 static int node_cmp_attr_Call(ir_node *a, ir_node *b) {
6007 return get_irn_call_attr(a) != get_irn_call_attr(b);
6008 } /* node_cmp_attr_Call */
6010 /** Compares the attributes of two Sel nodes. */
6011 static int node_cmp_attr_Sel(ir_node *a, ir_node *b) {
6012 const ir_entity *a_ent = get_Sel_entity(a);
6013 const ir_entity *b_ent = get_Sel_entity(b);
6016 (a_ent->kind != b_ent->kind) ||
6017 (a_ent->name != b_ent->name) ||
6018 (a_ent->owner != b_ent->owner) ||
6019 (a_ent->ld_name != b_ent->ld_name) ||
6020 (a_ent->type != b_ent->type);
6022 /* Matze: inlining of functions can produce 2 entities with same type,
6024 return a_ent != b_ent;
6025 } /* node_cmp_attr_Sel */
6027 /** Compares the attributes of two Phi nodes. */
6028 static int node_cmp_attr_Phi(ir_node *a, ir_node *b) {
6029 /* we can only enter this function if both nodes have the same number of inputs,
6030 hence it is enough to check if one of them is a Phi0 */
6032 /* check the Phi0 pos attribute */
6033 return get_irn_phi_attr(a)->u.pos != get_irn_phi_attr(b)->u.pos;
6036 } /* node_cmp_attr_Phi */
6038 /** Compares the attributes of two Conv nodes. */
6039 static int node_cmp_attr_Conv(ir_node *a, ir_node *b) {
6040 return get_Conv_strict(a) != get_Conv_strict(b);
6041 } /* node_cmp_attr_Conv */
6043 /** Compares the attributes of two Cast nodes. */
6044 static int node_cmp_attr_Cast(ir_node *a, ir_node *b) {
6045 return get_Cast_type(a) != get_Cast_type(b);
6046 } /* node_cmp_attr_Cast */
6048 /** Compares the attributes of two Load nodes. */
6049 static int node_cmp_attr_Load(ir_node *a, ir_node *b) {
6050 if (get_Load_volatility(a) == volatility_is_volatile ||
6051 get_Load_volatility(b) == volatility_is_volatile)
6052 /* NEVER do CSE on volatile Loads */
6054 /* do not CSE Loads with different alignment. Be conservative. */
6055 if (get_Load_align(a) != get_Load_align(b))
6058 return get_Load_mode(a) != get_Load_mode(b);
6059 } /* node_cmp_attr_Load */
6061 /** Compares the attributes of two Store nodes. */
6062 static int node_cmp_attr_Store(ir_node *a, ir_node *b) {
6063 /* do not CSE Stores with different alignment. Be conservative. */
6064 if (get_Store_align(a) != get_Store_align(b))
6067 /* NEVER do CSE on volatile Stores */
6068 return (get_Store_volatility(a) == volatility_is_volatile ||
6069 get_Store_volatility(b) == volatility_is_volatile);
6070 } /* node_cmp_attr_Store */
6072 /** Compares two exception attributes */
6073 static int node_cmp_exception(ir_node *a, ir_node *b) {
6074 const except_attr *ea = get_irn_except_attr(a);
6075 const except_attr *eb = get_irn_except_attr(b);
6077 return ea->pin_state != eb->pin_state;
6080 #define node_cmp_attr_Bound node_cmp_exception
6082 /** Compares the attributes of two Div nodes. */
6083 static int node_cmp_attr_Div(ir_node *a, ir_node *b) {
6084 const divmod_attr *ma = get_irn_divmod_attr(a);
6085 const divmod_attr *mb = get_irn_divmod_attr(b);
6086 return ma->exc.pin_state != mb->exc.pin_state ||
6087 ma->resmode != mb->resmode ||
6088 ma->no_remainder != mb->no_remainder;
6089 } /* node_cmp_attr_Div */
6091 /** Compares the attributes of two DivMod nodes. */
6092 static int node_cmp_attr_DivMod(ir_node *a, ir_node *b) {
6093 const divmod_attr *ma = get_irn_divmod_attr(a);
6094 const divmod_attr *mb = get_irn_divmod_attr(b);
6095 return ma->exc.pin_state != mb->exc.pin_state ||
6096 ma->resmode != mb->resmode;
6097 } /* node_cmp_attr_DivMod */
6099 /** Compares the attributes of two Mod nodes. */
6100 static int node_cmp_attr_Mod(ir_node *a, ir_node *b) {
6101 const divmod_attr *ma = get_irn_divmod_attr(a);
6102 const divmod_attr *mb = get_irn_divmod_attr(b);
6103 return ma->exc.pin_state != mb->exc.pin_state ||
6104 ma->resmode != mb->resmode;
6105 } /* node_cmp_attr_Mod */
6107 /** Compares the attributes of two Quot nodes. */
6108 static int node_cmp_attr_Quot(ir_node *a, ir_node *b) {
6109 const divmod_attr *ma = get_irn_divmod_attr(a);
6110 const divmod_attr *mb = get_irn_divmod_attr(b);
6111 return ma->exc.pin_state != mb->exc.pin_state ||
6112 ma->resmode != mb->resmode;
6113 } /* node_cmp_attr_Quot */
6115 /** Compares the attributes of two Confirm nodes. */
6116 static int node_cmp_attr_Confirm(ir_node *a, ir_node *b) {
6117 /* no need to compare the bound, as this is a input */
6118 return (get_Confirm_cmp(a) != get_Confirm_cmp(b));
6119 } /* node_cmp_attr_Confirm */
6121 /** Compares the attributes of two Builtin nodes. */
6122 static int node_cmp_attr_Builtin(ir_node *a, ir_node *b) {
6123 const builtin_attr *ma = get_irn_builtin_attr(a);
6124 const builtin_attr *mb = get_irn_builtin_attr(b);
6126 /* no need to compare the type, equal kind means equal type */
6127 return ma->kind != mb->kind;
6128 } /* node_cmp_attr_Builtin */
6130 /** Compares the attributes of two ASM nodes. */
6131 static int node_cmp_attr_ASM(ir_node *a, ir_node *b) {
6133 const ir_asm_constraint *ca;
6134 const ir_asm_constraint *cb;
6137 if (get_ASM_text(a) != get_ASM_text(b))
6140 /* Should we really check the constraints here? Should be better, but is strange. */
6141 n = get_ASM_n_input_constraints(a);
6142 if (n != get_ASM_n_input_constraints(b))
6145 ca = get_ASM_input_constraints(a);
6146 cb = get_ASM_input_constraints(b);
6147 for (i = 0; i < n; ++i) {
6148 if (ca[i].pos != cb[i].pos || ca[i].constraint != cb[i].constraint)
6152 n = get_ASM_n_output_constraints(a);
6153 if (n != get_ASM_n_output_constraints(b))
6156 ca = get_ASM_output_constraints(a);
6157 cb = get_ASM_output_constraints(b);
6158 for (i = 0; i < n; ++i) {
6159 if (ca[i].pos != cb[i].pos || ca[i].constraint != cb[i].constraint)
6163 n = get_ASM_n_clobbers(a);
6164 if (n != get_ASM_n_clobbers(b))
6167 cla = get_ASM_clobbers(a);
6168 clb = get_ASM_clobbers(b);
6169 for (i = 0; i < n; ++i) {
6170 if (cla[i] != clb[i])
6174 } /* node_cmp_attr_ASM */
6176 /** Compares the inexistent attributes of two Dummy nodes. */
6177 static int node_cmp_attr_Dummy(ir_node *a, ir_node *b)
6185 * Set the default node attribute compare operation for an ir_op_ops.
6187 * @param code the opcode for the default operation
6188 * @param ops the operations initialized
6193 static ir_op_ops *firm_set_default_node_cmp_attr(ir_opcode code, ir_op_ops *ops)
6197 ops->node_cmp_attr = node_cmp_attr_##a; \
6230 } /* firm_set_default_node_cmp_attr */
6233 * Compare function for two nodes in the value table. Gets two
6234 * nodes as parameters. Returns 0 if the nodes are a Common Sub Expression.
6236 int identities_cmp(const void *elt, const void *key) {
6237 ir_node *a = (ir_node *)elt;
6238 ir_node *b = (ir_node *)key;
6241 if (a == b) return 0;
6243 if ((get_irn_op(a) != get_irn_op(b)) ||
6244 (get_irn_mode(a) != get_irn_mode(b))) return 1;
6246 /* compare if a's in and b's in are of equal length */
6247 irn_arity_a = get_irn_intra_arity(a);
6248 if (irn_arity_a != get_irn_intra_arity(b))
6251 if (get_irn_pinned(a) == op_pin_state_pinned) {
6252 /* for pinned nodes, the block inputs must be equal */
6253 if (get_irn_intra_n(a, -1) != get_irn_intra_n(b, -1))
6255 } else if (! get_opt_global_cse()) {
6256 /* for block-local CSE both nodes must be in the same MacroBlock */
6257 if (get_irn_MacroBlock(a) != get_irn_MacroBlock(b))
6261 /* compare a->in[0..ins] with b->in[0..ins] */
6262 for (i = 0; i < irn_arity_a; i++)
6263 if (get_irn_intra_n(a, i) != get_irn_intra_n(b, i))
6267 * here, we already now that the nodes are identical except their
6270 if (a->op->ops.node_cmp_attr)
6271 return a->op->ops.node_cmp_attr(a, b);
6274 } /* identities_cmp */
6277 * Calculate a hash value of a node.
6279 * @param node The IR-node
6281 unsigned ir_node_hash(const ir_node *node) {
6282 return node->op->ops.hash(node);
6283 } /* ir_node_hash */
6286 pset *new_identities(void) {
6287 return new_pset(identities_cmp, N_IR_NODES);
6288 } /* new_identities */
6290 void del_identities(pset *value_table) {
6291 del_pset(value_table);
6292 } /* del_identities */
6294 /* Normalize a node by putting constants (and operands with larger
6295 * node index) on the right (operator side). */
6296 void ir_normalize_node(ir_node *n) {
6297 if (is_op_commutative(get_irn_op(n))) {
6298 ir_node *l = get_binop_left(n);
6299 ir_node *r = get_binop_right(n);
6301 /* For commutative operators perform a OP b == b OP a but keep
6302 * constants on the RIGHT side. This helps greatly in some
6303 * optimizations. Moreover we use the idx number to make the form
6305 if (!operands_are_normalized(l, r)) {
6306 set_binop_left(n, r);
6307 set_binop_right(n, l);
6311 } /* ir_normalize_node */
6314 * Update the nodes after a match in the value table. If both nodes have
6315 * the same MacroBlock but different Blocks, we must ensure that the node
6316 * with the dominating Block (the node that is near to the MacroBlock header
6317 * is stored in the table.
6318 * Because a MacroBlock has only one "non-exception" flow, we don't need
6319 * dominance info here: We known, that one block must dominate the other and
6320 * following the only block input will allow to find it.
6322 static void update_known_irn(ir_node *known_irn, const ir_node *new_ir_node) {
6323 ir_node *known_blk, *new_block, *block, *mbh;
6325 if (get_opt_global_cse()) {
6326 /* Block inputs are meaning less */
6329 known_blk = get_irn_n(known_irn, -1);
6330 new_block = get_irn_n(new_ir_node, -1);
6331 if (known_blk == new_block) {
6332 /* already in the same block */
6336 * We expect the typical case when we built the graph. In that case, the
6337 * known_irn is already the upper one, so checking this should be faster.
6340 mbh = get_Block_MacroBlock(new_block);
6342 if (block == known_blk) {
6343 /* ok, we have found it: known_block dominates new_block as expected */
6348 * We have reached the MacroBlock header NOT founding
6349 * the known_block. new_block must dominate known_block.
6352 set_irn_n(known_irn, -1, new_block);
6355 assert(get_Block_n_cfgpreds(block) == 1);
6356 block = get_Block_cfgpred_block(block, 0);
6358 } /* update_value_table */
6361 * Return the canonical node computing the same value as n.
6362 * Looks up the node in a hash table, enters it in the table
6363 * if it isn't there yet.
6365 * @param value_table the HashSet containing all nodes in the
6367 * @param n the node to look up
6369 * @return a node that computes the same value as n or n if no such
6370 * node could be found
6372 ir_node *identify_remember(pset *value_table, ir_node *n) {
6375 if (!value_table) return n;
6377 ir_normalize_node(n);
6378 /* lookup or insert in hash table with given hash key. */
6379 o = pset_insert(value_table, n, ir_node_hash(n));
6382 update_known_irn(o, n);
6386 } /* identify_remember */
6389 * During construction we set the op_pin_state_pinned flag in the graph right when the
6390 * optimization is performed. The flag turning on procedure global cse could
6391 * be changed between two allocations. This way we are safe.
6393 * @param value_table The value table
6394 * @param n The node to lookup
6396 static inline ir_node *identify_cons(pset *value_table, ir_node *n) {
6399 n = identify_remember(value_table, n);
6400 if (n != old && get_irn_MacroBlock(old) != get_irn_MacroBlock(n))
6401 set_irg_pinned(current_ir_graph, op_pin_state_floats);
6403 } /* identify_cons */
6405 /* Add a node to the identities value table. */
6406 void add_identities(pset *value_table, ir_node *node) {
6407 if (get_opt_cse() && is_no_Block(node))
6408 identify_remember(value_table, node);
6409 } /* add_identities */
6411 /* Visit each node in the value table of a graph. */
6412 void visit_all_identities(ir_graph *irg, irg_walk_func visit, void *env) {
6414 ir_graph *rem = current_ir_graph;
6416 current_ir_graph = irg;
6417 foreach_pset(irg->value_table, node)
6419 current_ir_graph = rem;
6420 } /* visit_all_identities */
6423 * Garbage in, garbage out. If a node has a dead input, i.e., the
6424 * Bad node is input to the node, return the Bad node.
6426 static ir_node *gigo(ir_node *node) {
6428 ir_op *op = get_irn_op(node);
6430 /* remove garbage blocks by looking at control flow that leaves the block
6431 and replacing the control flow by Bad. */
6432 if (get_irn_mode(node) == mode_X) {
6433 ir_node *block = get_nodes_block(skip_Proj(node));
6435 /* Don't optimize nodes in immature blocks. */
6436 if (!get_Block_matured(block))
6438 /* Don't optimize End, may have Bads. */
6439 if (op == op_End) return node;
6441 if (is_Block(block)) {
6442 if (is_Block_dead(block)) {
6443 /* control flow from dead block is dead */
6447 for (i = get_irn_arity(block) - 1; i >= 0; --i) {
6448 if (!is_Bad(get_irn_n(block, i)))
6452 ir_graph *irg = get_irn_irg(block);
6453 /* the start block is never dead */
6454 if (block != get_irg_start_block(irg)
6455 && block != get_irg_end_block(irg)) {
6457 * Do NOT kill control flow without setting
6458 * the block to dead of bad things can happen:
6459 * We get a Block that is not reachable be irg_block_walk()
6460 * but can be found by irg_walk()!
6462 set_Block_dead(block);
6469 /* Blocks, Phis and Tuples may have dead inputs, e.g., if one of the
6470 blocks predecessors is dead. */
6471 if (op != op_Block && op != op_Phi && op != op_Tuple) {
6472 irn_arity = get_irn_arity(node);
6475 * Beware: we can only read the block of a non-floating node.
6477 if (is_irn_pinned_in_irg(node) &&
6478 is_Block_dead(get_nodes_block(skip_Proj(node))))
6481 for (i = 0; i < irn_arity; i++) {
6482 ir_node *pred = get_irn_n(node, i);
6487 /* Propagating Unknowns here seems to be a bad idea, because
6488 sometimes we need a node as a input and did not want that
6490 However, it might be useful to move this into a later phase
6491 (if you think that optimizing such code is useful). */
6492 if (is_Unknown(pred) && mode_is_data(get_irn_mode(node)))
6493 return new_Unknown(get_irn_mode(node));
6498 /* With this code we violate the agreement that local_optimize
6499 only leaves Bads in Block, Phi and Tuple nodes. */
6500 /* If Block has only Bads as predecessors it's garbage. */
6501 /* If Phi has only Bads as predecessors it's garbage. */
6502 if ((op == op_Block && get_Block_matured(node)) || op == op_Phi) {
6503 irn_arity = get_irn_arity(node);
6504 for (i = 0; i < irn_arity; i++) {
6505 if (!is_Bad(get_irn_n(node, i))) break;
6507 if (i == irn_arity) node = new_Bad();
6514 * These optimizations deallocate nodes from the obstack.
6515 * It can only be called if it is guaranteed that no other nodes
6516 * reference this one, i.e., right after construction of a node.
6518 * @param n The node to optimize
6520 * current_ir_graph must be set to the graph of the node!
6522 ir_node *optimize_node(ir_node *n) {
6525 ir_opcode iro = get_irn_opcode(n);
6527 /* Always optimize Phi nodes: part of the construction. */
6528 if ((!get_opt_optimize()) && (iro != iro_Phi)) return n;
6530 /* constant expression evaluation / constant folding */
6531 if (get_opt_constant_folding()) {
6532 /* neither constants nor Tuple values can be evaluated */
6533 if (iro != iro_Const && (get_irn_mode(n) != mode_T)) {
6534 unsigned fp_model = get_irg_fp_model(current_ir_graph);
6535 int old_fp_mode = tarval_fp_ops_enabled();
6537 tarval_enable_fp_ops(! (fp_model & fp_no_float_fold));
6539 /* try to evaluate */
6540 tv = computed_value(n);
6541 if (tv != tarval_bad) {
6543 ir_type *old_tp = get_irn_type(n);
6544 int i, arity = get_irn_arity(n);
6548 * Try to recover the type of the new expression.
6550 for (i = 0; i < arity && !old_tp; ++i)
6551 old_tp = get_irn_type(get_irn_n(n, i));
6554 * we MUST copy the node here temporary, because it's still needed
6555 * for DBG_OPT_CSTEVAL
6557 node_size = offsetof(ir_node, attr) + n->op->attr_size;
6558 oldn = alloca(node_size);
6560 memcpy(oldn, n, node_size);
6561 CLONE_ARR_A(ir_node *, oldn->in, n->in);
6563 /* ARG, copy the in array, we need it for statistics */
6564 memcpy(oldn->in, n->in, ARR_LEN(n->in) * sizeof(n->in[0]));
6566 /* note the inplace edges module */
6567 edges_node_deleted(n, current_ir_graph);
6569 /* evaluation was successful -- replace the node. */
6570 irg_kill_node(current_ir_graph, n);
6573 if (old_tp && get_type_mode(old_tp) == get_tarval_mode(tv))
6574 set_Const_type(nw, old_tp);
6575 DBG_OPT_CSTEVAL(oldn, nw);
6576 tarval_enable_fp_ops(old_fp_mode);
6579 tarval_enable_fp_ops(old_fp_mode);
6583 /* remove unnecessary nodes */
6584 if (get_opt_algebraic_simplification() ||
6585 (iro == iro_Phi) || /* always optimize these nodes. */
6587 (iro == iro_Proj) ||
6588 (iro == iro_Block) ) /* Flags tested local. */
6589 n = equivalent_node(n);
6591 /* Common Subexpression Elimination.
6593 * Checks whether n is already available.
6594 * The block input is used to distinguish different subexpressions. Right
6595 * now all nodes are op_pin_state_pinned to blocks, i.e., the CSE only finds common
6596 * subexpressions within a block.
6599 n = identify_cons(current_ir_graph->value_table, n);
6602 edges_node_deleted(oldn, current_ir_graph);
6604 /* We found an existing, better node, so we can deallocate the old node. */
6605 irg_kill_node(current_ir_graph, oldn);
6609 /* Some more constant expression evaluation that does not allow to
6611 iro = get_irn_opcode(n);
6612 if (get_opt_algebraic_simplification() ||
6613 (iro == iro_Cond) ||
6614 (iro == iro_Proj)) /* Flags tested local. */
6615 n = transform_node(n);
6617 /* Remove nodes with dead (Bad) input.
6618 Run always for transformation induced Bads. */
6621 /* Now we have a legal, useful node. Enter it in hash table for CSE */
6622 if (get_opt_cse() && (get_irn_opcode(n) != iro_Block)) {
6624 n = identify_remember(current_ir_graph->value_table, o);
6630 } /* optimize_node */
6634 * These optimizations never deallocate nodes (in place). This can cause dead
6635 * nodes lying on the obstack. Remove these by a dead node elimination,
6636 * i.e., a copying garbage collection.
6638 ir_node *optimize_in_place_2(ir_node *n) {
6641 ir_opcode iro = get_irn_opcode(n);
6643 if (!get_opt_optimize() && !is_Phi(n)) return n;
6645 /* constant expression evaluation / constant folding */
6646 if (get_opt_constant_folding()) {
6647 /* neither constants nor Tuple values can be evaluated */
6648 if (iro != iro_Const && get_irn_mode(n) != mode_T) {
6649 unsigned fp_model = get_irg_fp_model(current_ir_graph);
6650 int old_fp_mode = tarval_fp_ops_enabled();
6652 tarval_enable_fp_ops((fp_model & fp_strict_algebraic) == 0);
6653 /* try to evaluate */
6654 tv = computed_value(n);
6655 if (tv != tarval_bad) {
6656 /* evaluation was successful -- replace the node. */
6657 ir_type *old_tp = get_irn_type(n);
6658 int i, arity = get_irn_arity(n);
6661 * Try to recover the type of the new expression.
6663 for (i = 0; i < arity && !old_tp; ++i)
6664 old_tp = get_irn_type(get_irn_n(n, i));
6668 if (old_tp && get_type_mode(old_tp) == get_tarval_mode(tv))
6669 set_Const_type(n, old_tp);
6671 DBG_OPT_CSTEVAL(oldn, n);
6672 tarval_enable_fp_ops(old_fp_mode);
6675 tarval_enable_fp_ops(old_fp_mode);
6679 /* remove unnecessary nodes */
6680 if (get_opt_constant_folding() ||
6681 (iro == iro_Phi) || /* always optimize these nodes. */
6682 (iro == iro_Id) || /* ... */
6683 (iro == iro_Proj) || /* ... */
6684 (iro == iro_Block) ) /* Flags tested local. */
6685 n = equivalent_node(n);
6687 /** common subexpression elimination **/
6688 /* Checks whether n is already available. */
6689 /* The block input is used to distinguish different subexpressions. Right
6690 now all nodes are op_pin_state_pinned to blocks, i.e., the cse only finds common
6691 subexpressions within a block. */
6692 if (get_opt_cse()) {
6694 n = identify_remember(current_ir_graph->value_table, o);
6699 /* Some more constant expression evaluation. */
6700 iro = get_irn_opcode(n);
6701 if (get_opt_constant_folding() ||
6702 (iro == iro_Cond) ||
6703 (iro == iro_Proj)) /* Flags tested local. */
6704 n = transform_node(n);
6706 /* Remove nodes with dead (Bad) input.
6707 Run always for transformation induced Bads. */
6710 /* Now we can verify the node, as it has no dead inputs any more. */
6713 /* Now we have a legal, useful node. Enter it in hash table for cse.
6714 Blocks should be unique anyways. (Except the successor of start:
6715 is cse with the start block!) */
6716 if (get_opt_cse() && (get_irn_opcode(n) != iro_Block)) {
6718 n = identify_remember(current_ir_graph->value_table, o);
6724 } /* optimize_in_place_2 */
6727 * Wrapper for external use, set proper status bits after optimization.
6729 ir_node *optimize_in_place(ir_node *n) {
6730 /* Handle graph state */
6731 assert(get_irg_phase_state(current_ir_graph) != phase_building);
6733 if (get_opt_global_cse())
6734 set_irg_pinned(current_ir_graph, op_pin_state_floats);
6735 if (get_irg_outs_state(current_ir_graph) == outs_consistent)
6736 set_irg_outs_inconsistent(current_ir_graph);
6738 /* FIXME: Maybe we could also test whether optimizing the node can
6739 change the control graph. */
6740 set_irg_doms_inconsistent(current_ir_graph);
6741 return optimize_in_place_2(n);
6742 } /* optimize_in_place */
6745 * Calculate a hash value of a Const node.
6747 static unsigned hash_Const(const ir_node *node) {
6750 /* special value for const, as they only differ in their tarval. */
6751 h = HASH_PTR(node->attr.con.tv);
6757 * Calculate a hash value of a SymConst node.
6759 static unsigned hash_SymConst(const ir_node *node) {
6762 /* all others are pointers */
6763 h = HASH_PTR(node->attr.symc.sym.type_p);
6766 } /* hash_SymConst */
6769 * Set the default hash operation in an ir_op_ops.
6771 * @param code the opcode for the default operation
6772 * @param ops the operations initialized
6777 static ir_op_ops *firm_set_default_hash(ir_opcode code, ir_op_ops *ops)
6781 ops->hash = hash_##a; \
6784 /* hash function already set */
6785 if (ops->hash != NULL)
6792 /* use input/mode default hash if no function was given */
6793 ops->hash = firm_default_hash;
6801 * Sets the default operation for an ir_ops.
6803 ir_op_ops *firm_set_default_operations(ir_opcode code, ir_op_ops *ops) {
6804 ops = firm_set_default_hash(code, ops);
6805 ops = firm_set_default_computed_value(code, ops);
6806 ops = firm_set_default_equivalent_node(code, ops);
6807 ops = firm_set_default_transform_node(code, ops);
6808 ops = firm_set_default_node_cmp_attr(code, ops);
6809 ops = firm_set_default_get_type(code, ops);
6810 ops = firm_set_default_get_type_attr(code, ops);
6811 ops = firm_set_default_get_entity_attr(code, ops);
6814 } /* firm_set_default_operations */