2 * Copyright (C) 1995-2011 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
32 #include "irgraph_t.h"
33 #include "iredges_t.h"
39 #include "iroptimize.h"
41 #include "dbginfo_t.h"
42 #include "iropt_dbg.h"
47 #include "opt_polymorphy.h"
52 #include "firm_types.h"
53 #include "bitfiddle.h"
56 /* Make types visible to allow most efficient access */
60 * Returns the tarval of a Const node or tarval_bad for all other nodes.
62 static ir_tarval *default_value_of(const ir_node *n)
65 return get_Const_tarval(n); /* might return tarval_bad */
70 value_of_func value_of_ptr = default_value_of;
72 /* * Set a new value_of function. */
73 void set_value_of_func(value_of_func func)
78 value_of_ptr = default_value_of;
82 * Return the value of a Constant.
84 static ir_tarval *computed_value_Const(const ir_node *n)
86 return get_Const_tarval(n);
87 } /* computed_value_Const */
90 * Return the value of a 'sizeof', 'alignof' or 'offsetof' SymConst.
92 static ir_tarval *computed_value_SymConst(const ir_node *n)
97 switch (get_SymConst_kind(n)) {
98 case symconst_type_size:
99 type = get_SymConst_type(n);
100 if (get_type_state(type) == layout_fixed)
101 return new_tarval_from_long(get_type_size_bytes(type), get_irn_mode(n));
103 case symconst_type_align:
104 type = get_SymConst_type(n);
105 if (get_type_state(type) == layout_fixed)
106 return new_tarval_from_long(get_type_alignment_bytes(type), get_irn_mode(n));
108 case symconst_ofs_ent:
109 ent = get_SymConst_entity(n);
110 type = get_entity_owner(ent);
111 if (get_type_state(type) == layout_fixed)
112 return new_tarval_from_long(get_entity_offset(ent), get_irn_mode(n));
118 } /* computed_value_SymConst */
121 * Return the value of an Add.
123 static ir_tarval *computed_value_Add(const ir_node *n)
125 ir_node *a = get_Add_left(n);
126 ir_node *b = get_Add_right(n);
128 ir_tarval *ta = value_of(a);
129 ir_tarval *tb = value_of(b);
131 if ((ta != tarval_bad) && (tb != tarval_bad))
132 return tarval_add(ta, tb);
135 } /* computed_value_Add */
138 * Return the value of a Sub.
139 * Special case: a - a
141 static ir_tarval *computed_value_Sub(const ir_node *n)
143 ir_mode *mode = get_irn_mode(n);
144 ir_node *a = get_Sub_left(n);
145 ir_node *b = get_Sub_right(n);
150 if (! mode_is_float(mode)) {
153 return get_mode_null(mode);
159 if ((ta != tarval_bad) && (tb != tarval_bad))
160 return tarval_sub(ta, tb, mode);
163 } /* computed_value_Sub */
166 * Return the value of a Carry.
167 * Special : a op 0, 0 op b
169 static ir_tarval *computed_value_Carry(const ir_node *n)
171 ir_node *a = get_binop_left(n);
172 ir_node *b = get_binop_right(n);
173 ir_mode *m = get_irn_mode(n);
174 ir_tarval *ta = value_of(a);
175 ir_tarval *tb = value_of(b);
177 if ((ta != tarval_bad) && (tb != tarval_bad)) {
179 return tarval_carry() ? get_mode_one(m) : get_mode_null(m);
181 if (tarval_is_null(ta) || tarval_is_null(tb))
182 return get_mode_null(m);
185 } /* computed_value_Carry */
188 * Return the value of a Borrow.
191 static ir_tarval *computed_value_Borrow(const ir_node *n)
193 ir_node *a = get_binop_left(n);
194 ir_node *b = get_binop_right(n);
195 ir_mode *m = get_irn_mode(n);
196 ir_tarval *ta = value_of(a);
197 ir_tarval *tb = value_of(b);
199 if ((ta != tarval_bad) && (tb != tarval_bad)) {
200 return tarval_cmp(ta, tb) == ir_relation_less ? get_mode_one(m) : get_mode_null(m);
201 } else if (tarval_is_null(ta)) {
202 return get_mode_null(m);
205 } /* computed_value_Borrow */
208 * Return the value of an unary Minus.
210 static ir_tarval *computed_value_Minus(const ir_node *n)
212 ir_node *a = get_Minus_op(n);
213 ir_tarval *ta = value_of(a);
215 if (ta != tarval_bad)
216 return tarval_neg(ta);
219 } /* computed_value_Minus */
222 * Return the value of a Mul.
224 static ir_tarval *computed_value_Mul(const ir_node *n)
226 ir_node *a = get_Mul_left(n);
227 ir_node *b = get_Mul_right(n);
228 ir_tarval *ta = value_of(a);
229 ir_tarval *tb = value_of(b);
232 mode = get_irn_mode(n);
233 if (mode != get_irn_mode(a)) {
234 /* n * n = 2n bit multiplication */
235 ta = tarval_convert_to(ta, mode);
236 tb = tarval_convert_to(tb, mode);
239 if (ta != tarval_bad && tb != tarval_bad) {
240 return tarval_mul(ta, tb);
242 /* a * 0 != 0 if a == NaN or a == Inf */
243 if (!mode_is_float(mode)) {
244 /* a*0 = 0 or 0*b = 0 */
245 if (ta == get_mode_null(mode))
247 if (tb == get_mode_null(mode))
252 } /* computed_value_Mul */
255 * Return the value of an And.
256 * Special case: a & 0, 0 & b
258 static ir_tarval *computed_value_And(const ir_node *n)
260 ir_node *a = get_And_left(n);
261 ir_node *b = get_And_right(n);
262 ir_tarval *ta = value_of(a);
263 ir_tarval *tb = value_of(b);
265 if ((ta != tarval_bad) && (tb != tarval_bad)) {
266 return tarval_and (ta, tb);
268 if (tarval_is_null(ta)) return ta;
269 if (tarval_is_null(tb)) return tb;
272 } /* computed_value_And */
275 * Return the value of an Or.
276 * Special case: a | 1...1, 1...1 | b
278 static ir_tarval *computed_value_Or(const ir_node *n)
280 ir_node *a = get_Or_left(n);
281 ir_node *b = get_Or_right(n);
282 ir_tarval *ta = value_of(a);
283 ir_tarval *tb = value_of(b);
285 if ((ta != tarval_bad) && (tb != tarval_bad)) {
286 return tarval_or (ta, tb);
288 if (tarval_is_all_one(ta)) return ta;
289 if (tarval_is_all_one(tb)) return tb;
292 } /* computed_value_Or */
295 * Return the value of an Eor.
297 static ir_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 ir_tarval *computed_value_Not(const ir_node *n)
321 ir_node *a = get_Not_op(n);
322 ir_tarval *ta = value_of(a);
324 if (ta != tarval_bad)
325 return tarval_not(ta);
328 } /* computed_value_Not */
331 * Return the value of a Shl.
333 static ir_tarval *computed_value_Shl(const ir_node *n)
335 ir_node *a = get_Shl_left(n);
336 ir_node *b = get_Shl_right(n);
338 ir_tarval *ta = value_of(a);
339 ir_tarval *tb = value_of(b);
341 if ((ta != tarval_bad) && (tb != tarval_bad)) {
342 return tarval_shl(ta, tb);
345 } /* computed_value_Shl */
348 * Return the value of a Shr.
350 static ir_tarval *computed_value_Shr(const ir_node *n)
352 ir_node *a = get_Shr_left(n);
353 ir_node *b = get_Shr_right(n);
355 ir_tarval *ta = value_of(a);
356 ir_tarval *tb = value_of(b);
358 if ((ta != tarval_bad) && (tb != tarval_bad)) {
359 return tarval_shr(ta, tb);
362 } /* computed_value_Shr */
365 * Return the value of a Shrs.
367 static ir_tarval *computed_value_Shrs(const ir_node *n)
369 ir_node *a = get_Shrs_left(n);
370 ir_node *b = get_Shrs_right(n);
372 ir_tarval *ta = value_of(a);
373 ir_tarval *tb = value_of(b);
375 if ((ta != tarval_bad) && (tb != tarval_bad)) {
376 return tarval_shrs(ta, tb);
379 } /* computed_value_Shrs */
382 * Return the value of a Rotl.
384 static ir_tarval *computed_value_Rotl(const ir_node *n)
386 ir_node *a = get_Rotl_left(n);
387 ir_node *b = get_Rotl_right(n);
389 ir_tarval *ta = value_of(a);
390 ir_tarval *tb = value_of(b);
392 if ((ta != tarval_bad) && (tb != tarval_bad)) {
393 return tarval_rotl(ta, tb);
396 } /* computed_value_Rotl */
399 * Return the value of a Conv.
401 static ir_tarval *computed_value_Conv(const ir_node *n)
403 ir_node *a = get_Conv_op(n);
404 ir_tarval *ta = value_of(a);
406 if (ta != tarval_bad)
407 return tarval_convert_to(ta, get_irn_mode(n));
410 } /* computed_value_Conv */
413 * Calculate the value of a Mux: can be evaluated, if the
414 * sel and the right input are known.
416 static ir_tarval *computed_value_Mux(const ir_node *n)
418 ir_node *sel = get_Mux_sel(n);
419 ir_tarval *ts = value_of(sel);
421 if (ts == get_tarval_b_true()) {
422 ir_node *v = get_Mux_true(n);
425 else if (ts == get_tarval_b_false()) {
426 ir_node *v = get_Mux_false(n);
430 } /* computed_value_Mux */
433 * Calculate the value of a Confirm: can be evaluated,
434 * if it has the form Confirm(x, '=', Const).
436 static ir_tarval *computed_value_Confirm(const ir_node *n)
438 if (get_Confirm_relation(n) == ir_relation_equal) {
439 ir_tarval *tv = value_of(get_Confirm_bound(n));
440 if (tv != tarval_bad)
443 return value_of(get_Confirm_value(n));
444 } /* computed_value_Confirm */
447 * gives a (conservative) estimation of possible relation when comparing
450 ir_relation ir_get_possible_cmp_relations(const ir_node *left,
451 const ir_node *right)
453 ir_relation possible = ir_relation_true;
454 ir_tarval *tv_l = value_of(left);
455 ir_tarval *tv_r = value_of(right);
456 ir_mode *mode = get_irn_mode(left);
457 ir_tarval *min = mode == mode_b ? tarval_b_false : get_mode_min(mode);
458 ir_tarval *max = mode == mode_b ? tarval_b_true : get_mode_max(mode);
460 /* both values known - evaluate them */
461 if ((tv_l != tarval_bad) && (tv_r != tarval_bad)) {
462 possible = tarval_cmp(tv_l, tv_r);
463 /* we can return now, won't get any better */
466 /* a == a is never less or greater (but might be equal or unordered) */
468 possible &= ~ir_relation_less_greater;
469 /* unordered results only happen for float compares */
470 if (!mode_is_float(mode))
471 possible &= ~ir_relation_unordered;
472 /* values can never be less than the least representable number or
473 * greater than the greatest representable number */
475 possible &= ~ir_relation_greater;
477 possible &= ~ir_relation_less;
479 possible &= ~ir_relation_greater;
481 possible &= ~ir_relation_less;
482 /* maybe vrp can tell us more */
483 possible &= vrp_cmp(left, right);
484 /* Alloc nodes never return null (but throw an exception) */
485 if (is_Alloc(left) && tarval_is_null(tv_r))
486 possible &= ~ir_relation_equal;
492 * Return the value of a Cmp.
494 * The basic idea here is to determine which relations are possible and which
495 * one are definitely impossible.
497 static ir_tarval *computed_value_Cmp(const ir_node *cmp)
499 ir_node *left = get_Cmp_left(cmp);
500 ir_node *right = get_Cmp_right(cmp);
501 ir_relation possible = ir_get_possible_cmp_relations(left, right);
502 ir_relation relation = get_Cmp_relation(cmp);
504 /* if none of the requested relations is possible, return false */
505 if ((possible & relation) == ir_relation_false)
506 return tarval_b_false;
507 /* if possible relations are a subset of the requested ones return true */
508 if ((possible & ~relation) == ir_relation_false)
509 return tarval_b_true;
511 return computed_value_Cmp_Confirm(cmp, left, right, relation);
515 * Calculate the value of an integer Div.
516 * Special case: 0 / b
518 static ir_tarval *do_computed_value_Div(const ir_node *div)
520 const ir_node *a = get_Div_left(div);
521 const ir_node *b = get_Div_right(div);
522 const ir_mode *mode = get_Div_resmode(div);
523 ir_tarval *ta = value_of(a);
525 const ir_node *dummy;
527 /* cannot optimize 0 / b = 0 because of NaN */
528 if (!mode_is_float(mode)) {
529 if (tarval_is_null(ta) && value_not_zero(b, &dummy))
530 return ta; /* 0 / b == 0 if b != 0 */
533 if (ta != tarval_bad && tb != tarval_bad)
534 return tarval_div(ta, tb);
536 } /* do_computed_value_Div */
539 * Calculate the value of an integer Mod of two nodes.
540 * Special case: a % 1
542 static ir_tarval *do_computed_value_Mod(const ir_node *a, const ir_node *b)
544 ir_tarval *ta = value_of(a);
545 ir_tarval *tb = value_of(b);
547 /* Compute a % 1 or c1 % c2 */
548 if (tarval_is_one(tb))
549 return get_mode_null(get_irn_mode(a));
550 if (ta != tarval_bad && tb != tarval_bad)
551 return tarval_mod(ta, tb);
553 } /* do_computed_value_Mod */
556 * Return the value of a Proj(Div).
558 static ir_tarval *computed_value_Proj_Div(const ir_node *n)
560 long proj_nr = get_Proj_proj(n);
561 if (proj_nr != pn_Div_res)
564 return do_computed_value_Div(get_Proj_pred(n));
565 } /* computed_value_Proj_Div */
568 * Return the value of a Proj(Mod).
570 static ir_tarval *computed_value_Proj_Mod(const ir_node *n)
572 long proj_nr = get_Proj_proj(n);
574 if (proj_nr == pn_Mod_res) {
575 const ir_node *mod = get_Proj_pred(n);
576 return do_computed_value_Mod(get_Mod_left(mod), get_Mod_right(mod));
579 } /* computed_value_Proj_Mod */
582 * Return the value of a Proj.
584 static ir_tarval *computed_value_Proj(const ir_node *proj)
586 ir_node *n = get_Proj_pred(proj);
588 if (n->op->ops.computed_value_Proj != NULL)
589 return n->op->ops.computed_value_Proj(proj);
591 } /* computed_value_Proj */
594 * If the parameter n can be computed, return its value, else tarval_bad.
595 * Performs constant folding.
597 * @param n The node this should be evaluated
599 ir_tarval *computed_value(const ir_node *n)
601 vrp_attr *vrp = vrp_get_info(n);
602 if (vrp && vrp->valid && tarval_cmp(vrp->bits_set, vrp->bits_not_set) == ir_relation_equal) {
603 return vrp->bits_set;
605 if (n->op->ops.computed_value)
606 return n->op->ops.computed_value(n);
608 } /* computed_value */
611 * Set the default computed_value evaluator in an ir_op_ops.
613 * @param code the opcode for the default operation
614 * @param ops the operations initialized
619 static ir_op_ops *firm_set_default_computed_value(ir_opcode code, ir_op_ops *ops)
623 ops->computed_value = computed_value_##a; \
625 #define CASE_PROJ(a) \
627 ops->computed_value_Proj = computed_value_Proj_##a; \
662 } /* firm_set_default_computed_value */
665 * Returns a equivalent block for another block.
666 * If the block has only one predecessor, this is
667 * the equivalent one. If the only predecessor of a block is
668 * the block itself, this is a dead block.
670 * If both predecessors of a block are the branches of a binary
671 * Cond, the equivalent block is Cond's block.
673 * If all predecessors of a block are bad or lies in a dead
674 * block, the current block is dead as well.
676 * Note, that blocks are NEVER turned into Bad's, instead
677 * the dead_block flag is set. So, never test for is_Bad(block),
678 * always use is_dead_Block(block).
680 static ir_node *equivalent_node_Block(ir_node *n)
686 /* don't optimize dead or labeled blocks */
687 if (is_Block_dead(n) || has_Block_entity(n))
690 n_preds = get_Block_n_cfgpreds(n);
692 /* The Block constructor does not call optimize, but mature_immBlock()
693 calls the optimization. */
694 assert(get_Block_matured(n));
696 irg = get_irn_irg(n);
698 /* Straightening: a single entry Block following a single exit Block
699 can be merged, if it is not the Start block. */
700 /* !!! Beware, all Phi-nodes of n must have been optimized away.
701 This should be true, as the block is matured before optimize is called.
702 But what about Phi-cycles with the Phi0/Id that could not be resolved?
703 Remaining Phi nodes are just Ids. */
705 ir_node *pred = skip_Proj(get_Block_cfgpred(n, 0));
708 ir_node *predblock = get_nodes_block(pred);
709 if (predblock == oldn) {
710 /* Jmp jumps into the block it is in -- deal self cycle. */
711 n = set_Block_dead(n);
712 DBG_OPT_DEAD_BLOCK(oldn, n);
715 DBG_OPT_STG(oldn, n);
717 } else if (is_Cond(pred)) {
718 ir_node *predblock = get_nodes_block(pred);
719 if (predblock == oldn) {
720 /* Jmp jumps into the block it is in -- deal self cycle. */
721 n = set_Block_dead(n);
722 DBG_OPT_DEAD_BLOCK(oldn, n);
725 } else if (n_preds == 2) {
726 /* Test whether Cond jumps twice to this block
727 * The more general case which more than 2 predecessors is handles
728 * in optimize_cf(), we handle only this special case for speed here.
730 ir_node *a = get_Block_cfgpred(n, 0);
731 ir_node *b = get_Block_cfgpred(n, 1);
733 if (is_Proj(a) && is_Proj(b)) {
734 ir_node *cond = get_Proj_pred(a);
736 if (cond == get_Proj_pred(b) && is_Cond(cond) &&
737 get_irn_mode(get_Cond_selector(cond)) == mode_b) {
738 /* Also a single entry Block following a single exit Block. Phis have
739 twice the same operand and will be optimized away. */
740 n = get_nodes_block(cond);
741 DBG_OPT_IFSIM1(oldn, a, b, n);
744 } else if (get_opt_unreachable_code() &&
745 (n != get_irg_start_block(irg)) &&
746 (n != get_irg_end_block(irg))) {
749 /* If all inputs are dead, this block is dead too, except if it is
750 the start or end block. This is one step of unreachable code
752 for (i = get_Block_n_cfgpreds(n) - 1; i >= 0; --i) {
753 ir_node *pred = get_Block_cfgpred(n, i);
756 if (is_Bad(pred)) continue;
757 pred_blk = get_nodes_block(skip_Proj(pred));
759 if (is_Block_dead(pred_blk)) continue;
762 /* really found a living input */
767 n = set_Block_dead(n);
768 DBG_OPT_DEAD_BLOCK(oldn, n);
773 } /* equivalent_node_Block */
776 * Returns a equivalent node for a Jmp, a Bad :-)
777 * Of course this only happens if the Block of the Jmp is dead.
779 static ir_node *equivalent_node_Jmp(ir_node *n)
783 /* unreachable code elimination */
784 if (is_Block_dead(get_nodes_block(n))) {
785 ir_graph *irg = get_irn_irg(n);
786 n = get_irg_bad(irg);
787 DBG_OPT_DEAD_BLOCK(oldn, n);
790 } /* equivalent_node_Jmp */
792 /** Raise is handled in the same way as Jmp. */
793 #define equivalent_node_Raise equivalent_node_Jmp
796 /* We do not evaluate Cond here as we replace it by a new node, a Jmp.
797 See transform_node_Proj_Cond(). */
800 * Optimize operations that are commutative and have neutral 0,
801 * so a op 0 = 0 op a = a.
803 static ir_node *equivalent_node_neutral_zero(ir_node *n)
807 ir_node *a = get_binop_left(n);
808 ir_node *b = get_binop_right(n);
813 /* After running compute_node there is only one constant predecessor.
814 Find this predecessors value and remember the other node: */
815 if ((tv = value_of(a)) != tarval_bad) {
817 } else if ((tv = value_of(b)) != tarval_bad) {
822 /* If this predecessors constant value is zero, the operation is
823 * unnecessary. Remove it.
825 * Beware: If n is a Add, the mode of on and n might be different
826 * which happens in this rare construction: NULL + 3.
827 * Then, a Conv would be needed which we cannot include here.
829 if (tarval_is_null(tv) && get_irn_mode(on) == get_irn_mode(n)) {
832 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_0);
836 } /* equivalent_node_neutral_zero */
839 * Eor is commutative and has neutral 0.
841 static ir_node *equivalent_node_Eor(ir_node *n)
847 n = equivalent_node_neutral_zero(n);
848 if (n != oldn) return n;
851 b = get_Eor_right(n);
854 ir_node *aa = get_Eor_left(a);
855 ir_node *ab = get_Eor_right(a);
858 /* (a ^ b) ^ a -> b */
860 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_EOR_A_B_A);
862 } else if (ab == b) {
863 /* (a ^ b) ^ b -> a */
865 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_EOR_A_B_A);
870 ir_node *ba = get_Eor_left(b);
871 ir_node *bb = get_Eor_right(b);
874 /* a ^ (a ^ b) -> b */
876 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_EOR_A_B_A);
878 } else if (bb == a) {
879 /* a ^ (b ^ a) -> b */
881 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_EOR_A_B_A);
889 * Optimize a - 0 and (a - x) + x (for modes with wrap-around).
891 * The second one looks strange, but this construct
892 * is used heavily in the LCC sources :-).
894 * Beware: The Mode of an Add may be different than the mode of its
895 * predecessors, so we could not return a predecessors in all cases.
897 static ir_node *equivalent_node_Add(ir_node *n)
900 ir_node *left, *right;
901 ir_mode *mode = get_irn_mode(n);
903 n = equivalent_node_neutral_zero(n);
907 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
908 if (mode_is_float(mode)) {
909 ir_graph *irg = get_irn_irg(n);
910 if (get_irg_fp_model(irg) & fp_strict_algebraic)
914 left = get_Add_left(n);
915 right = get_Add_right(n);
918 if (get_Sub_right(left) == right) {
921 n = get_Sub_left(left);
922 if (mode == get_irn_mode(n)) {
923 DBG_OPT_ALGSIM1(oldn, left, right, n, FS_OPT_ADD_SUB);
929 if (get_Sub_right(right) == left) {
932 n = get_Sub_left(right);
933 if (mode == get_irn_mode(n)) {
934 DBG_OPT_ALGSIM1(oldn, left, right, n, FS_OPT_ADD_SUB);
940 } /* equivalent_node_Add */
943 * optimize operations that are not commutative but have neutral 0 on left,
946 static ir_node *equivalent_node_left_zero(ir_node *n)
950 ir_node *a = get_binop_left(n);
951 ir_node *b = get_binop_right(n);
952 ir_tarval *tb = value_of(b);
954 if (tarval_is_null(tb)) {
957 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_0);
960 } /* equivalent_node_left_zero */
962 #define equivalent_node_Shl equivalent_node_left_zero
963 #define equivalent_node_Shr equivalent_node_left_zero
964 #define equivalent_node_Shrs equivalent_node_left_zero
965 #define equivalent_node_Rotl equivalent_node_left_zero
968 * Optimize a - 0 and (a + x) - x (for modes with wrap-around).
970 * The second one looks strange, but this construct
971 * is used heavily in the LCC sources :-).
973 * Beware: The Mode of a Sub may be different than the mode of its
974 * predecessors, so we could not return a predecessors in all cases.
976 static ir_node *equivalent_node_Sub(ir_node *n)
980 ir_mode *mode = get_irn_mode(n);
983 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
984 if (mode_is_float(mode)) {
985 ir_graph *irg = get_irn_irg(n);
986 if (get_irg_fp_model(irg) & fp_strict_algebraic)
990 b = get_Sub_right(n);
993 /* Beware: modes might be different */
994 if (tarval_is_null(tb)) {
995 ir_node *a = get_Sub_left(n);
996 if (mode == get_irn_mode(a)) {
999 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_0);
1003 } /* equivalent_node_Sub */
1007 * Optimize an "self-inverse unary op", ie op(op(n)) = n.
1010 * -(-a) == a, but might overflow two times.
1011 * We handle it anyway here but the better way would be a
1012 * flag. This would be needed for Pascal for instance.
1014 static ir_node *equivalent_node_idempotent_unop(ir_node *n)
1017 ir_node *pred = get_unop_op(n);
1019 /* optimize symmetric unop */
1020 if (get_irn_op(pred) == get_irn_op(n)) {
1021 n = get_unop_op(pred);
1022 DBG_OPT_ALGSIM2(oldn, pred, n, FS_OPT_IDEM_UNARY);
1025 } /* equivalent_node_idempotent_unop */
1027 /** Optimize Not(Not(x)) == x. */
1028 #define equivalent_node_Not equivalent_node_idempotent_unop
1030 /** -(-x) == x ??? Is this possible or can --x raise an
1031 out of bounds exception if min =! max? */
1032 #define equivalent_node_Minus equivalent_node_idempotent_unop
1035 * Optimize a * 1 = 1 * a = a.
1037 static ir_node *equivalent_node_Mul(ir_node *n)
1040 ir_node *a = get_Mul_left(n);
1042 /* we can handle here only the n * n = n bit cases */
1043 if (get_irn_mode(n) == get_irn_mode(a)) {
1044 ir_node *b = get_Mul_right(n);
1048 * Mul is commutative and has again an other neutral element.
1049 * Constants are place right, so check this case first.
1052 if (tarval_is_one(tv)) {
1054 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
1057 if (tarval_is_one(tv)) {
1059 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
1064 } /* equivalent_node_Mul */
1067 * Use algebraic simplification a | a = a | 0 = 0 | a = a.
1069 static ir_node *equivalent_node_Or(ir_node *n)
1073 ir_node *a = get_Or_left(n);
1074 ir_node *b = get_Or_right(n);
1078 n = a; /* idempotence */
1079 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_OR);
1082 /* constants are normalized to right, check this side first */
1084 if (tarval_is_null(tv)) {
1086 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_OR);
1090 if (tarval_is_null(tv)) {
1092 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_OR);
1097 } /* equivalent_node_Or */
1100 * Optimize a & 0b1...1 = 0b1...1 & a = a & a = (a|X) & a = a.
1102 static ir_node *equivalent_node_And(ir_node *n)
1106 ir_node *a = get_And_left(n);
1107 ir_node *b = get_And_right(n);
1111 n = a; /* idempotence */
1112 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_AND);
1115 /* constants are normalized to right, check this side first */
1117 if (tarval_is_all_one(tv)) {
1119 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1122 if (tv != get_tarval_bad()) {
1123 ir_mode *mode = get_irn_mode(n);
1124 if (!mode_is_signed(mode) && is_Conv(a)) {
1125 ir_node *convop = get_Conv_op(a);
1126 ir_mode *convopmode = get_irn_mode(convop);
1127 if (!mode_is_signed(convopmode)) {
1128 if (tarval_is_all_one(tarval_convert_to(tv, convopmode))) {
1129 /* Conv(X) & all_one(mode(X)) = Conv(X) */
1131 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1138 if (tarval_is_all_one(tv)) {
1140 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1144 if (b == get_Or_left(a) || b == get_Or_right(a)) {
1147 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1152 if (a == get_Or_left(b) || a == get_Or_right(b)) {
1155 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1160 } /* equivalent_node_And */
1163 * Try to remove useless Conv's:
1165 static ir_node *equivalent_node_Conv(ir_node *n)
1168 ir_node *a = get_Conv_op(n);
1170 ir_mode *n_mode = get_irn_mode(n);
1171 ir_mode *a_mode = get_irn_mode(a);
1174 if (n_mode == a_mode) { /* No Conv necessary */
1175 if (get_Conv_strict(n)) {
1178 /* neither Minus nor Confirm change the precision,
1179 so we can "look-through" */
1182 p = get_Minus_op(p);
1183 } else if (is_Confirm(p)) {
1184 p = get_Confirm_value(p);
1190 if (is_Conv(p) && get_Conv_strict(p)) {
1191 /* we known already, that a_mode == n_mode, and neither
1192 Minus change the mode, so the second Conv
1194 assert(get_irn_mode(p) == n_mode);
1196 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CONV);
1200 ir_node *pred = get_Proj_pred(p);
1201 if (is_Load(pred)) {
1202 /* Loads always return with the exact precision of n_mode */
1203 assert(get_Load_mode(pred) == n_mode);
1205 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CONV);
1208 if (is_Proj(pred) && get_Proj_proj(pred) == pn_Start_T_args) {
1209 pred = get_Proj_pred(pred);
1210 if (is_Start(pred)) {
1211 /* Arguments always return with the exact precision,
1212 as strictConv's are place before Call -- if the
1213 caller was compiled with the same setting.
1214 Otherwise, the semantics is probably still right. */
1215 assert(get_irn_mode(p) == n_mode);
1217 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CONV);
1223 /* special case: the immediate predecessor is also a Conv */
1224 if (! get_Conv_strict(a)) {
1225 /* first one is not strict, kick it */
1227 a_mode = get_irn_mode(a);
1231 /* else both are strict conv, second is superfluous */
1233 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CONV);
1238 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CONV);
1241 } else if (is_Conv(a)) { /* Conv(Conv(b)) */
1242 ir_node *b = get_Conv_op(a);
1243 ir_mode *b_mode = get_irn_mode(b);
1245 if (get_Conv_strict(n) && get_Conv_strict(a)) {
1246 /* both are strict conv */
1247 if (smaller_mode(a_mode, n_mode)) {
1248 /* both are strict, but the first is smaller, so
1249 the second cannot remove more precision, remove the
1251 set_Conv_strict(n, 0);
1254 if (n_mode == b_mode) {
1255 if (! get_Conv_strict(n) && ! get_Conv_strict(a)) {
1256 if (n_mode == mode_b) {
1257 n = b; /* Convb(Conv*(xxxb(...))) == xxxb(...) */
1258 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
1260 } else if (get_mode_arithmetic(n_mode) == get_mode_arithmetic(a_mode)) {
1261 if (values_in_mode(b_mode, a_mode)) {
1262 n = b; /* ConvS(ConvL(xxxS(...))) == xxxS(...) */
1263 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
1268 if (mode_is_int(n_mode) && get_mode_arithmetic(a_mode) == irma_ieee754) {
1269 /* ConvI(ConvF(I)) -> I, iff float mantissa >= int mode */
1270 unsigned int_mantissa = get_mode_size_bits(n_mode) - (mode_is_signed(n_mode) ? 1 : 0);
1271 unsigned float_mantissa = tarval_ieee754_get_mantissa_size(a_mode);
1273 if (float_mantissa >= int_mantissa) {
1275 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
1280 if (smaller_mode(b_mode, a_mode)) {
1281 if (get_Conv_strict(n))
1282 set_Conv_strict(b, 1);
1283 n = b; /* ConvA(ConvB(ConvA(...))) == ConvA(...) */
1284 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
1291 } /* equivalent_node_Conv */
1294 * - fold Phi-nodes, iff they have only one predecessor except
1297 static ir_node *equivalent_node_Phi(ir_node *n)
1303 ir_node *first_val = NULL; /* to shutup gcc */
1305 if (!get_opt_optimize() &&
1306 get_irg_phase_state(get_irn_irg(n)) != phase_building)
1309 n_preds = get_Phi_n_preds(n);
1311 block = get_nodes_block(n);
1313 if (is_Block_dead(block)) {
1314 ir_graph *irg = get_irn_irg(n);
1315 return get_irg_bad(irg);
1318 if (n_preds == 0) return n; /* Phi of dead Region without predecessors. */
1320 /* Find first non-self-referencing input */
1321 for (i = 0; i < n_preds; ++i) {
1322 first_val = get_Phi_pred(n, i);
1323 if ( (first_val != n) /* not self pointer */
1325 /* BEWARE: when the if is changed to 1, Phis will ignore their Bad
1326 * predecessors. Then, Phi nodes in unreachable code might be removed,
1327 * causing nodes pointing to themselev (Adds for instance).
1328 * This is really bad and causes endless recursion on several
1329 * code pathes, so we do NOT optimize such code.
1330 * This is not that bad as it sounds, optimize_cf() removes bad control flow
1331 * (and bad Phi predecessors), so live code is optimized later.
1333 && (! is_Bad(get_Block_cfgpred(block, i)))
1335 ) { /* value not dead */
1336 break; /* then found first value. */
1341 ir_graph *irg = get_irn_irg(n);
1342 /* A totally Bad or self-referencing Phi (we didn't break the above loop) */
1343 return get_irg_bad(irg);
1346 /* search for rest of inputs, determine if any of these
1347 are non-self-referencing */
1348 while (++i < n_preds) {
1349 ir_node *scnd_val = get_Phi_pred(n, i);
1350 if ( (scnd_val != n)
1351 && (scnd_val != first_val)
1354 && (! is_Bad(get_Block_cfgpred(block, i)))
1361 if (i >= n_preds && !is_Dummy(first_val)) {
1362 /* Fold, if no multiple distinct non-self-referencing inputs */
1364 DBG_OPT_PHI(oldn, n);
1367 } /* equivalent_node_Phi */
1370 * Several optimizations:
1371 * - fold Sync-nodes, iff they have only one predecessor except
1374 static ir_node *equivalent_node_Sync(ir_node *n)
1376 int arity = get_Sync_n_preds(n);
1379 for (i = 0; i < arity;) {
1380 ir_node *pred = get_Sync_pred(n, i);
1383 /* Remove Bad predecessors */
1390 /* Remove duplicate predecessors */
1396 if (get_Sync_pred(n, j) == pred) {
1405 ir_graph *irg = get_irn_irg(n);
1406 return get_irg_bad(irg);
1408 if (arity == 1) return get_Sync_pred(n, 0);
1410 } /* equivalent_node_Sync */
1413 * Optimize Proj(Tuple).
1415 static ir_node *equivalent_node_Proj_Tuple(ir_node *proj)
1417 ir_node *oldn = proj;
1418 ir_node *tuple = get_Proj_pred(proj);
1420 /* Remove the Tuple/Proj combination. */
1421 proj = get_Tuple_pred(tuple, get_Proj_proj(proj));
1422 DBG_OPT_TUPLE(oldn, tuple, proj);
1425 } /* equivalent_node_Proj_Tuple */
1428 * Optimize a / 1 = a.
1430 static ir_node *equivalent_node_Proj_Div(ir_node *proj)
1432 ir_node *oldn = proj;
1433 ir_node *div = get_Proj_pred(proj);
1434 ir_node *b = get_Div_right(div);
1435 ir_tarval *tb = value_of(b);
1437 /* Div is not commutative. */
1438 if (tarval_is_one(tb)) { /* div(x, 1) == x */
1439 switch (get_Proj_proj(proj)) {
1441 proj = get_Div_mem(div);
1442 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NEUTRAL_1);
1446 proj = get_Div_left(div);
1447 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NEUTRAL_1);
1451 /* we cannot replace the exception Proj's here, this is done in
1452 transform_node_Proj_Div() */
1457 } /* equivalent_node_Proj_Div */
1460 * Optimize CopyB(mem, x, x) into a Nop.
1462 static ir_node *equivalent_node_Proj_CopyB(ir_node *proj)
1464 ir_node *oldn = proj;
1465 ir_node *copyb = get_Proj_pred(proj);
1466 ir_node *a = get_CopyB_dst(copyb);
1467 ir_node *b = get_CopyB_src(copyb);
1470 /* Turn CopyB into a tuple (mem, jmp, bad, bad) */
1471 switch (get_Proj_proj(proj)) {
1473 proj = get_CopyB_mem(copyb);
1474 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NOP);
1477 case pn_CopyB_X_except: {
1478 ir_graph *irg = get_irn_irg(proj);
1479 DBG_OPT_EXC_REM(proj);
1480 proj = get_irg_bad(irg);
1486 } /* equivalent_node_Proj_CopyB */
1489 * Optimize Bounds(idx, idx, upper) into idx.
1491 static ir_node *equivalent_node_Proj_Bound(ir_node *proj)
1493 ir_node *oldn = proj;
1494 ir_node *bound = get_Proj_pred(proj);
1495 ir_node *idx = get_Bound_index(bound);
1496 ir_node *pred = skip_Proj(idx);
1499 if (idx == get_Bound_lower(bound))
1501 else if (is_Bound(pred)) {
1503 * idx was Bounds checked previously, it is still valid if
1504 * lower <= pred_lower && pred_upper <= upper.
1506 ir_node *lower = get_Bound_lower(bound);
1507 ir_node *upper = get_Bound_upper(bound);
1508 if (get_Bound_lower(pred) == lower &&
1509 get_Bound_upper(pred) == upper) {
1511 * One could expect that we simply return the previous
1512 * Bound here. However, this would be wrong, as we could
1513 * add an exception Proj to a new location then.
1514 * So, we must turn in into a tuple.
1520 /* Turn Bound into a tuple (mem, jmp, bad, idx) */
1521 switch (get_Proj_proj(proj)) {
1523 DBG_OPT_EXC_REM(proj);
1524 proj = get_Bound_mem(bound);
1526 case pn_Bound_X_except: {
1527 ir_graph *irg = get_irn_irg(proj);
1528 DBG_OPT_EXC_REM(proj);
1529 proj = get_irg_bad(irg);
1534 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NOP);
1537 /* cannot optimize pn_Bound_X_regular, handled in transform ... */
1542 } /* equivalent_node_Proj_Bound */
1545 * Optimize an Exception Proj(Load) with a non-null address.
1547 static ir_node *equivalent_node_Proj_Load(ir_node *proj)
1549 if (get_opt_ldst_only_null_ptr_exceptions()) {
1550 if (get_irn_mode(proj) == mode_X) {
1551 ir_node *load = get_Proj_pred(proj);
1553 /* get the Load address */
1554 const ir_node *addr = get_Load_ptr(load);
1555 const ir_node *confirm;
1557 if (value_not_null(addr, &confirm)) {
1558 if (get_Proj_proj(proj) == pn_Load_X_except) {
1559 ir_graph *irg = get_irn_irg(proj);
1560 DBG_OPT_EXC_REM(proj);
1561 return get_irg_bad(irg);
1567 } /* equivalent_node_Proj_Load */
1570 * Optimize an Exception Proj(Store) with a non-null address.
1572 static ir_node *equivalent_node_Proj_Store(ir_node *proj)
1574 if (get_opt_ldst_only_null_ptr_exceptions()) {
1575 if (get_irn_mode(proj) == mode_X) {
1576 ir_node *store = get_Proj_pred(proj);
1578 /* get the load/store address */
1579 const ir_node *addr = get_Store_ptr(store);
1580 const ir_node *confirm;
1582 if (value_not_null(addr, &confirm)) {
1583 if (get_Proj_proj(proj) == pn_Store_X_except) {
1584 ir_graph *irg = get_irn_irg(proj);
1585 DBG_OPT_EXC_REM(proj);
1586 return get_irg_bad(irg);
1592 } /* equivalent_node_Proj_Store */
1595 * Does all optimizations on nodes that must be done on its Projs
1596 * because of creating new nodes.
1598 static ir_node *equivalent_node_Proj(ir_node *proj)
1600 ir_node *n = get_Proj_pred(proj);
1602 if (get_irn_mode(proj) == mode_X) {
1603 if (is_Block_dead(get_nodes_block(n))) {
1604 /* Remove dead control flow -- early gigo(). */
1605 ir_graph *irg = get_irn_irg(proj);
1606 return get_irg_bad(irg);
1609 if (n->op->ops.equivalent_node_Proj)
1610 return n->op->ops.equivalent_node_Proj(proj);
1612 } /* equivalent_node_Proj */
1617 static ir_node *equivalent_node_Id(ir_node *n)
1625 DBG_OPT_ID(oldn, n);
1627 } /* equivalent_node_Id */
1632 static ir_node *equivalent_node_Mux(ir_node *n)
1634 ir_node *oldn = n, *sel = get_Mux_sel(n);
1636 ir_tarval *ts = value_of(sel);
1638 /* Mux(true, f, t) == t */
1639 if (ts == tarval_b_true) {
1640 n = get_Mux_true(n);
1641 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_C);
1644 /* Mux(false, f, t) == f */
1645 if (ts == tarval_b_false) {
1646 n = get_Mux_false(n);
1647 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_C);
1650 n_t = get_Mux_true(n);
1651 n_f = get_Mux_false(n);
1653 /* Mux(v, x, T) == x */
1654 if (is_Unknown(n_f)) {
1656 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_EQ);
1659 /* Mux(v, T, x) == x */
1660 if (is_Unknown(n_t)) {
1662 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_EQ);
1666 /* Mux(v, x, x) == x */
1669 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_EQ);
1672 if (is_Cmp(sel) && !mode_honor_signed_zeros(get_irn_mode(n))) {
1673 ir_relation relation = get_Cmp_relation(sel);
1674 ir_node *f = get_Mux_false(n);
1675 ir_node *t = get_Mux_true(n);
1678 * Note further that these optimization work even for floating point
1679 * with NaN's because -NaN == NaN.
1680 * However, if +0 and -0 is handled differently, we cannot use the first one.
1682 ir_node *const cmp_l = get_Cmp_left(sel);
1683 ir_node *const cmp_r = get_Cmp_right(sel);
1686 case ir_relation_equal:
1687 if ((cmp_l == t && cmp_r == f) || /* Mux(t == f, t, f) -> f */
1688 (cmp_l == f && cmp_r == t)) { /* Mux(f == t, t, f) -> f */
1690 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1695 case ir_relation_less_greater:
1696 case ir_relation_unordered_less_greater:
1697 if ((cmp_l == t && cmp_r == f) || /* Mux(t != f, t, f) -> t */
1698 (cmp_l == f && cmp_r == t)) { /* Mux(f != t, t, f) -> t */
1700 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1709 * Note: normalization puts the constant on the right side,
1710 * so we check only one case.
1712 if (cmp_l == t && tarval_is_null(value_of(cmp_r))) {
1713 /* Mux(t CMP 0, X, t) */
1714 if (is_Minus(f) && get_Minus_op(f) == t) {
1715 /* Mux(t CMP 0, -t, t) */
1716 if (relation == ir_relation_equal) {
1717 /* Mux(t == 0, -t, t) ==> -t */
1719 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1720 } else if (relation == ir_relation_less_greater || relation == ir_relation_unordered_less_greater) {
1721 /* Mux(t != 0, -t, t) ==> t */
1723 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1733 * Remove Confirm nodes if setting is on.
1734 * Replace Confirms(x, '=', Constlike) by Constlike.
1736 static ir_node *equivalent_node_Confirm(ir_node *n)
1738 ir_node *pred = get_Confirm_value(n);
1739 ir_relation relation = get_Confirm_relation(n);
1741 while (is_Confirm(pred) && relation == get_Confirm_relation(pred)) {
1743 * rare case: two identical Confirms one after another,
1744 * replace the second one with the first.
1747 pred = get_Confirm_value(n);
1753 * equivalent_node() returns a node equivalent to input n. It skips all nodes that
1754 * perform no actual computation, as, e.g., the Id nodes. It does not create
1755 * new nodes. It is therefore safe to free n if the node returned is not n.
1756 * If a node returns a Tuple we can not just skip it. If the size of the
1757 * in array fits, we transform n into a tuple (e.g., Div).
1759 ir_node *equivalent_node(ir_node *n)
1761 if (n->op->ops.equivalent_node)
1762 return n->op->ops.equivalent_node(n);
1764 } /* equivalent_node */
1767 * Sets the default equivalent node operation for an ir_op_ops.
1769 * @param code the opcode for the default operation
1770 * @param ops the operations initialized
1775 static ir_op_ops *firm_set_default_equivalent_node(ir_opcode code, ir_op_ops *ops)
1779 ops->equivalent_node = equivalent_node_##a; \
1781 #define CASE_PROJ(a) \
1783 ops->equivalent_node_Proj = equivalent_node_Proj_##a; \
1823 } /* firm_set_default_equivalent_node */
1826 * Returns non-zero if a node is a Phi node
1827 * with all predecessors constant.
1829 static int is_const_Phi(ir_node *n)
1833 if (! is_Phi(n) || get_irn_arity(n) == 0)
1835 for (i = get_irn_arity(n) - 1; i >= 0; --i) {
1836 if (! is_Const(get_irn_n(n, i)))
1840 } /* is_const_Phi */
1842 typedef ir_tarval *(*tarval_sub_type)(ir_tarval *a, ir_tarval *b, ir_mode *mode);
1843 typedef ir_tarval *(*tarval_binop_type)(ir_tarval *a, ir_tarval *b);
1846 * in reality eval_func should be tarval (*eval_func)() but incomplete
1847 * declarations are bad style and generate noisy warnings
1849 typedef void (*eval_func)(void);
1852 * Wrapper for the tarval binop evaluation, tarval_sub has one more parameter.
1854 static ir_tarval *do_eval(eval_func eval, ir_tarval *a, ir_tarval *b, ir_mode *mode)
1856 if (eval == (eval_func) tarval_sub) {
1857 tarval_sub_type func = (tarval_sub_type)eval;
1859 return func(a, b, mode);
1861 tarval_binop_type func = (tarval_binop_type)eval;
1868 * Apply an evaluator on a binop with a constant operators (and one Phi).
1870 * @param phi the Phi node
1871 * @param other the other operand
1872 * @param eval an evaluator function
1873 * @param mode the mode of the result, may be different from the mode of the Phi!
1874 * @param left if non-zero, other is the left operand, else the right
1876 * @return a new Phi node if the conversion was successful, NULL else
1878 static ir_node *apply_binop_on_phi(ir_node *phi, ir_tarval *other, eval_func eval, ir_mode *mode, int left)
1884 int i, n = get_irn_arity(phi);
1886 NEW_ARR_A(void *, res, n);
1888 for (i = 0; i < n; ++i) {
1889 pred = get_irn_n(phi, i);
1890 tv = get_Const_tarval(pred);
1891 tv = do_eval(eval, other, tv, mode);
1893 if (tv == tarval_bad) {
1894 /* folding failed, bad */
1900 for (i = 0; i < n; ++i) {
1901 pred = get_irn_n(phi, i);
1902 tv = get_Const_tarval(pred);
1903 tv = do_eval(eval, tv, other, mode);
1905 if (tv == tarval_bad) {
1906 /* folding failed, bad */
1912 irg = get_irn_irg(phi);
1913 for (i = 0; i < n; ++i) {
1914 pred = get_irn_n(phi, i);
1915 res[i] = new_r_Const(irg, (ir_tarval*)res[i]);
1917 return new_r_Phi(get_nodes_block(phi), n, (ir_node **)res, mode);
1918 } /* apply_binop_on_phi */
1921 * Apply an evaluator on a binop with two constant Phi.
1923 * @param a the left Phi node
1924 * @param b the right Phi node
1925 * @param eval an evaluator function
1926 * @param mode the mode of the result, may be different from the mode of the Phi!
1928 * @return a new Phi node if the conversion was successful, NULL else
1930 static ir_node *apply_binop_on_2_phis(ir_node *a, ir_node *b, eval_func eval, ir_mode *mode)
1932 ir_tarval *tv_l, *tv_r, *tv;
1938 if (get_nodes_block(a) != get_nodes_block(b))
1941 n = get_irn_arity(a);
1942 NEW_ARR_A(void *, res, n);
1944 for (i = 0; i < n; ++i) {
1945 pred = get_irn_n(a, i);
1946 tv_l = get_Const_tarval(pred);
1947 pred = get_irn_n(b, i);
1948 tv_r = get_Const_tarval(pred);
1949 tv = do_eval(eval, tv_l, tv_r, mode);
1951 if (tv == tarval_bad) {
1952 /* folding failed, bad */
1957 irg = get_irn_irg(a);
1958 for (i = 0; i < n; ++i) {
1959 pred = get_irn_n(a, i);
1960 res[i] = new_r_Const(irg, (ir_tarval*)res[i]);
1962 return new_r_Phi(get_nodes_block(a), n, (ir_node **)res, mode);
1963 } /* apply_binop_on_2_phis */
1966 * Apply an evaluator on a unop with a constant operator (a Phi).
1968 * @param phi the Phi node
1969 * @param eval an evaluator function
1971 * @return a new Phi node if the conversion was successful, NULL else
1973 static ir_node *apply_unop_on_phi(ir_node *phi, ir_tarval *(*eval)(ir_tarval *))
1980 int i, n = get_irn_arity(phi);
1982 NEW_ARR_A(void *, res, n);
1983 for (i = 0; i < n; ++i) {
1984 pred = get_irn_n(phi, i);
1985 tv = get_Const_tarval(pred);
1988 if (tv == tarval_bad) {
1989 /* folding failed, bad */
1994 mode = get_irn_mode(phi);
1995 irg = get_irn_irg(phi);
1996 for (i = 0; i < n; ++i) {
1997 pred = get_irn_n(phi, i);
1998 res[i] = new_r_Const(irg, (ir_tarval*)res[i]);
2000 return new_r_Phi(get_nodes_block(phi), n, (ir_node **)res, mode);
2001 } /* apply_unop_on_phi */
2004 * Apply a conversion on a constant operator (a Phi).
2006 * @param phi the Phi node
2008 * @return a new Phi node if the conversion was successful, NULL else
2010 static ir_node *apply_conv_on_phi(ir_node *phi, ir_mode *mode)
2016 int i, n = get_irn_arity(phi);
2018 NEW_ARR_A(void *, res, n);
2019 for (i = 0; i < n; ++i) {
2020 pred = get_irn_n(phi, i);
2021 tv = get_Const_tarval(pred);
2022 tv = tarval_convert_to(tv, mode);
2024 if (tv == tarval_bad) {
2025 /* folding failed, bad */
2030 irg = get_irn_irg(phi);
2031 for (i = 0; i < n; ++i) {
2032 pred = get_irn_n(phi, i);
2033 res[i] = new_r_Const(irg, (ir_tarval*)res[i]);
2035 return new_r_Phi(get_nodes_block(phi), n, (ir_node **)res, mode);
2036 } /* apply_conv_on_phi */
2039 * Transform AddP(P, ConvIs(Iu)), AddP(P, ConvIu(Is)) and
2040 * SubP(P, ConvIs(Iu)), SubP(P, ConvIu(Is)).
2041 * If possible, remove the Conv's.
2043 static ir_node *transform_node_AddSub(ir_node *n)
2045 ir_mode *mode = get_irn_mode(n);
2047 if (mode_is_reference(mode)) {
2048 ir_node *left = get_binop_left(n);
2049 ir_node *right = get_binop_right(n);
2050 unsigned ref_bits = get_mode_size_bits(mode);
2052 if (is_Conv(left)) {
2053 ir_mode *lmode = get_irn_mode(left);
2054 unsigned bits = get_mode_size_bits(lmode);
2056 if (ref_bits == bits &&
2057 mode_is_int(lmode) &&
2058 get_mode_arithmetic(lmode) == irma_twos_complement) {
2059 ir_node *pre = get_Conv_op(left);
2060 ir_mode *pre_mode = get_irn_mode(pre);
2062 if (mode_is_int(pre_mode) &&
2063 get_mode_size_bits(pre_mode) == bits &&
2064 get_mode_arithmetic(pre_mode) == irma_twos_complement) {
2065 /* ok, this conv just changes to sign, moreover the calculation
2066 * is done with same number of bits as our address mode, so
2067 * we can ignore the conv as address calculation can be viewed
2068 * as either signed or unsigned
2070 set_binop_left(n, pre);
2075 if (is_Conv(right)) {
2076 ir_mode *rmode = get_irn_mode(right);
2077 unsigned bits = get_mode_size_bits(rmode);
2079 if (ref_bits == bits &&
2080 mode_is_int(rmode) &&
2081 get_mode_arithmetic(rmode) == irma_twos_complement) {
2082 ir_node *pre = get_Conv_op(right);
2083 ir_mode *pre_mode = get_irn_mode(pre);
2085 if (mode_is_int(pre_mode) &&
2086 get_mode_size_bits(pre_mode) == bits &&
2087 get_mode_arithmetic(pre_mode) == irma_twos_complement) {
2088 /* ok, this conv just changes to sign, moreover the calculation
2089 * is done with same number of bits as our address mode, so
2090 * we can ignore the conv as address calculation can be viewed
2091 * as either signed or unsigned
2093 set_binop_right(n, pre);
2098 /* let address arithmetic use unsigned modes */
2099 if (is_Const(right)) {
2100 ir_mode *rmode = get_irn_mode(right);
2102 if (mode_is_signed(rmode) && get_mode_arithmetic(rmode) == irma_twos_complement) {
2103 /* convert a AddP(P, *s) into AddP(P, *u) */
2104 ir_mode *nm = get_reference_mode_unsigned_eq(mode);
2106 ir_node *pre = new_r_Conv(get_nodes_block(n), right, nm);
2107 set_binop_right(n, pre);
2113 } /* transform_node_AddSub */
2115 #define HANDLE_BINOP_PHI(eval, a, b, c, mode) \
2118 if (is_Const(b) && is_const_Phi(a)) { \
2119 /* check for Op(Phi, Const) */ \
2120 c = apply_binop_on_phi(a, get_Const_tarval(b), eval, mode, 0);\
2122 else if (is_Const(a) && is_const_Phi(b)) { \
2123 /* check for Op(Const, Phi) */ \
2124 c = apply_binop_on_phi(b, get_Const_tarval(a), eval, mode, 1);\
2126 else if (is_const_Phi(a) && is_const_Phi(b)) { \
2127 /* check for Op(Phi, Phi) */ \
2128 c = apply_binop_on_2_phis(a, b, eval, mode); \
2131 DBG_OPT_ALGSIM0(oldn, c, FS_OPT_CONST_PHI); \
2136 #define HANDLE_UNOP_PHI(eval, a, c) \
2139 if (is_const_Phi(a)) { \
2140 /* check for Op(Phi) */ \
2141 c = apply_unop_on_phi(a, eval); \
2143 DBG_OPT_ALGSIM0(oldn, c, FS_OPT_CONST_PHI); \
2150 * Do the AddSub optimization, then Transform
2151 * Constant folding on Phi
2152 * Add(a,a) -> Mul(a, 2)
2153 * Add(Mul(a, x), a) -> Mul(a, x+1)
2154 * if the mode is integer or float.
2155 * Transform Add(a,-b) into Sub(a,b).
2156 * Reassociation might fold this further.
2158 static ir_node *transform_node_Add(ir_node *n)
2161 ir_node *a, *b, *c, *oldn = n;
2162 vrp_attr *a_vrp, *b_vrp;
2164 n = transform_node_AddSub(n);
2166 a = get_Add_left(n);
2167 b = get_Add_right(n);
2169 mode = get_irn_mode(n);
2171 if (mode_is_reference(mode)) {
2172 ir_mode *lmode = get_irn_mode(a);
2174 if (is_Const(b) && is_Const_null(b) && mode_is_int(lmode)) {
2175 /* an Add(a, NULL) is a hidden Conv */
2176 dbg_info *dbg = get_irn_dbg_info(n);
2177 return new_rd_Conv(dbg, get_nodes_block(n), a, mode);
2181 HANDLE_BINOP_PHI((eval_func) tarval_add, a, b, c, mode);
2183 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
2184 if (mode_is_float(mode)) {
2185 ir_graph *irg = get_irn_irg(n);
2186 if (get_irg_fp_model(irg) & fp_strict_algebraic)
2190 if (mode_is_num(mode)) {
2191 ir_graph *irg = get_irn_irg(n);
2192 /* the following code leads to endless recursion when Mul are replaced by a simple instruction chain */
2193 if (!is_irg_state(irg, IR_GRAPH_STATE_ARCH_DEP)
2194 && a == b && mode_is_int(mode)) {
2195 ir_node *block = get_nodes_block(n);
2198 get_irn_dbg_info(n),
2201 new_r_Const_long(irg, mode, 2),
2203 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_A);
2208 get_irn_dbg_info(n),
2213 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_MINUS_B);
2218 get_irn_dbg_info(n),
2223 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_MINUS_B);
2226 if (get_mode_arithmetic(mode) == irma_twos_complement) {
2227 /* Here we rely on constants be on the RIGHT side */
2229 ir_node *op = get_Not_op(a);
2231 if (is_Const(b) && is_Const_one(b)) {
2233 ir_node *blk = get_nodes_block(n);
2234 n = new_rd_Minus(get_irn_dbg_info(n), blk, op, mode);
2235 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_PLUS_1);
2240 n = new_r_Const(irg, get_mode_minus_one(mode));
2241 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_X_NOT_X);
2246 ir_node *op = get_Not_op(b);
2250 n = new_r_Const(irg, get_mode_minus_one(mode));
2251 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_X_NOT_X);
2258 a_vrp = vrp_get_info(a);
2259 b_vrp = vrp_get_info(b);
2261 if (a_vrp && b_vrp) {
2262 ir_tarval *c = tarval_and(a_vrp->bits_not_set, b_vrp->bits_not_set);
2264 if (tarval_is_null(c)) {
2265 dbg_info *dbgi = get_irn_dbg_info(n);
2266 return new_rd_Or(dbgi, get_nodes_block(n), a, b, mode);
2270 } /* transform_node_Add */
2273 * returns -cnst or NULL if impossible
2275 static ir_node *const_negate(ir_node *cnst)
2277 ir_tarval *tv = tarval_neg(get_Const_tarval(cnst));
2278 dbg_info *dbgi = get_irn_dbg_info(cnst);
2279 ir_graph *irg = get_irn_irg(cnst);
2280 if (tv == tarval_bad) return NULL;
2281 return new_rd_Const(dbgi, irg, tv);
2285 * Do the AddSub optimization, then Transform
2286 * Constant folding on Phi
2287 * Sub(0,a) -> Minus(a)
2288 * Sub(Mul(a, x), a) -> Mul(a, x-1)
2289 * Sub(Sub(x, y), b) -> Sub(x, Add(y,b))
2290 * Sub(Add(a, x), x) -> a
2291 * Sub(x, Add(x, a)) -> -a
2292 * Sub(x, Const) -> Add(x, -Const)
2294 static ir_node *transform_node_Sub(ir_node *n)
2300 n = transform_node_AddSub(n);
2302 a = get_Sub_left(n);
2303 b = get_Sub_right(n);
2305 mode = get_irn_mode(n);
2307 if (mode_is_int(mode)) {
2308 ir_mode *lmode = get_irn_mode(a);
2310 if (is_Const(b) && is_Const_null(b) && mode_is_reference(lmode)) {
2311 /* a Sub(a, NULL) is a hidden Conv */
2312 dbg_info *dbg = get_irn_dbg_info(n);
2313 n = new_rd_Conv(dbg, get_nodes_block(n), a, mode);
2314 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_CONV);
2318 if (mode == lmode &&
2319 get_mode_arithmetic(mode) == irma_twos_complement &&
2321 get_Const_tarval(a) == get_mode_minus_one(mode)) {
2323 dbg_info *dbg = get_irn_dbg_info(n);
2324 n = new_rd_Not(dbg, get_nodes_block(n), b, mode);
2325 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_NOT);
2331 HANDLE_BINOP_PHI((eval_func) tarval_sub, a, b, c, mode);
2333 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
2334 if (mode_is_float(mode)) {
2335 ir_graph *irg = get_irn_irg(n);
2336 if (get_irg_fp_model(irg) & fp_strict_algebraic)
2340 if (is_Const(b) && !mode_is_reference(get_irn_mode(b))) {
2341 /* a - C -> a + (-C) */
2342 ir_node *cnst = const_negate(b);
2344 ir_node *block = get_nodes_block(n);
2345 dbg_info *dbgi = get_irn_dbg_info(n);
2347 n = new_rd_Add(dbgi, block, a, cnst, mode);
2348 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2353 if (is_Minus(a)) { /* (-a) - b -> -(a + b) */
2354 dbg_info *dbg = get_irn_dbg_info(n);
2355 ir_node *block = get_nodes_block(n);
2356 ir_node *left = get_Minus_op(a);
2357 ir_node *add = new_rd_Add(dbg, block, left, b, mode);
2359 n = new_rd_Minus(dbg, block, add, mode);
2360 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2362 } else if (is_Minus(b)) { /* a - (-b) -> a + b */
2363 dbg_info *dbg = get_irn_dbg_info(n);
2364 ir_node *block = get_nodes_block(n);
2365 ir_node *right = get_Minus_op(b);
2367 n = new_rd_Add(dbg, block, a, right, mode);
2368 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MINUS);
2370 } else if (is_Sub(b)) {
2371 /* a - (b - c) -> a + (c - b)
2372 * -> (a - b) + c iff (b - c) is a pointer */
2373 dbg_info *s_dbg = get_irn_dbg_info(b);
2374 ir_node *s_left = get_Sub_left(b);
2375 ir_node *s_right = get_Sub_right(b);
2376 ir_mode *s_mode = get_irn_mode(b);
2377 if (mode_is_reference(s_mode)) {
2378 ir_node *lowest_block = get_nodes_block(n); /* a and b are live here */
2379 ir_node *sub = new_rd_Sub(s_dbg, lowest_block, a, s_left, mode);
2380 dbg_info *a_dbg = get_irn_dbg_info(n);
2383 s_right = new_r_Conv(lowest_block, s_right, mode);
2384 n = new_rd_Add(a_dbg, lowest_block, sub, s_right, mode);
2386 ir_node *s_block = get_nodes_block(b);
2387 ir_node *sub = new_rd_Sub(s_dbg, s_block, s_right, s_left, s_mode);
2388 dbg_info *a_dbg = get_irn_dbg_info(n);
2389 ir_node *a_block = get_nodes_block(n);
2391 n = new_rd_Add(a_dbg, a_block, a, sub, mode);
2393 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2395 } else if (is_Mul(b)) { /* a - (b * C) -> a + (b * -C) */
2396 ir_node *m_right = get_Mul_right(b);
2397 if (is_Const(m_right)) {
2398 ir_node *cnst2 = const_negate(m_right);
2399 if (cnst2 != NULL) {
2400 dbg_info *m_dbg = get_irn_dbg_info(b);
2401 ir_node *m_block = get_nodes_block(b);
2402 ir_node *m_left = get_Mul_left(b);
2403 ir_mode *m_mode = get_irn_mode(b);
2404 ir_node *mul = new_rd_Mul(m_dbg, m_block, m_left, cnst2, m_mode);
2405 dbg_info *a_dbg = get_irn_dbg_info(n);
2406 ir_node *a_block = get_nodes_block(n);
2408 n = new_rd_Add(a_dbg, a_block, a, mul, mode);
2409 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2415 /* Beware of Sub(P, P) which cannot be optimized into a simple Minus ... */
2416 if (mode_is_num(mode) && mode == get_irn_mode(a) && is_Const(a) && is_Const_null(a)) {
2418 get_irn_dbg_info(n),
2422 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_0_A);
2426 if (mode_wrap_around(mode)) {
2427 ir_node *left = get_Add_left(a);
2428 ir_node *right = get_Add_right(a);
2430 /* FIXME: Does the Conv's work only for two complement or generally? */
2432 if (mode != get_irn_mode(right)) {
2433 /* This Sub is an effective Cast */
2434 right = new_r_Conv(get_nodes_block(n), right, mode);
2437 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2439 } else if (right == b) {
2440 if (mode != get_irn_mode(left)) {
2441 /* This Sub is an effective Cast */
2442 left = new_r_Conv(get_nodes_block(n), left, mode);
2445 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2451 if (mode_wrap_around(mode)) {
2452 ir_node *left = get_Add_left(b);
2453 ir_node *right = get_Add_right(b);
2455 /* FIXME: Does the Conv's work only for two complement or generally? */
2457 ir_mode *r_mode = get_irn_mode(right);
2459 n = new_r_Minus(get_nodes_block(n), right, r_mode);
2460 if (mode != r_mode) {
2461 /* This Sub is an effective Cast */
2462 n = new_r_Conv(get_nodes_block(n), n, mode);
2464 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2466 } else if (right == a) {
2467 ir_mode *l_mode = get_irn_mode(left);
2469 n = new_r_Minus(get_nodes_block(n), left, l_mode);
2470 if (mode != l_mode) {
2471 /* This Sub is an effective Cast */
2472 n = new_r_Conv(get_nodes_block(n), n, mode);
2474 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2479 if (mode_is_int(mode) && is_Conv(a) && is_Conv(b)) {
2480 ir_mode *mode = get_irn_mode(a);
2482 if (mode == get_irn_mode(b)) {
2484 ir_node *op_a = get_Conv_op(a);
2485 ir_node *op_b = get_Conv_op(b);
2487 /* check if it's allowed to skip the conv */
2488 ma = get_irn_mode(op_a);
2489 mb = get_irn_mode(op_b);
2491 if (mode_is_reference(ma) && mode_is_reference(mb)) {
2492 /* SubInt(ConvInt(aP), ConvInt(bP)) -> SubInt(aP,bP) */
2495 set_Sub_right(n, b);
2501 /* do NOT execute this code if reassociation is enabled, it does the inverse! */
2502 if (!is_reassoc_running() && is_Mul(a)) {
2503 ir_node *ma = get_Mul_left(a);
2504 ir_node *mb = get_Mul_right(a);
2507 ir_node *blk = get_nodes_block(n);
2508 ir_graph *irg = get_irn_irg(n);
2510 get_irn_dbg_info(n),
2514 get_irn_dbg_info(n),
2517 new_r_Const(irg, get_mode_one(mode)),
2520 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MUL_A_X_A);
2522 } else if (mb == b) {
2523 ir_node *blk = get_nodes_block(n);
2524 ir_graph *irg = get_irn_irg(n);
2526 get_irn_dbg_info(n),
2530 get_irn_dbg_info(n),
2533 new_r_Const(irg, get_mode_one(mode)),
2536 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MUL_A_X_A);
2540 if (is_Sub(a)) { /* (x - y) - b -> x - (y + b) */
2541 ir_node *x = get_Sub_left(a);
2542 ir_node *y = get_Sub_right(a);
2543 ir_node *blk = get_nodes_block(n);
2544 ir_mode *m_b = get_irn_mode(b);
2545 ir_mode *m_y = get_irn_mode(y);
2549 /* Determine the right mode for the Add. */
2552 else if (mode_is_reference(m_b))
2554 else if (mode_is_reference(m_y))
2558 * Both modes are different but none is reference,
2559 * happens for instance in SubP(SubP(P, Iu), Is).
2560 * We have two possibilities here: Cast or ignore.
2561 * Currently we ignore this case.
2566 add = new_r_Add(blk, y, b, add_mode);
2568 n = new_rd_Sub(get_irn_dbg_info(n), blk, x, add, mode);
2569 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_SUB_X_Y_Z);
2573 if (get_mode_arithmetic(mode) == irma_twos_complement) {
2574 if (is_Const(a) && is_Not(b)) {
2575 /* c - ~X = X + (c+1) */
2576 ir_tarval *tv = get_Const_tarval(a);
2578 tv = tarval_add(tv, get_mode_one(mode));
2579 if (tv != tarval_bad) {
2580 ir_node *blk = get_nodes_block(n);
2581 ir_graph *irg = get_irn_irg(n);
2582 ir_node *c = new_r_Const(irg, tv);
2583 n = new_rd_Add(get_irn_dbg_info(n), blk, get_Not_op(b), c, mode);
2584 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_C_NOT_X);
2590 } /* transform_node_Sub */
2593 * Several transformation done on n*n=2n bits mul.
2594 * These transformations must be done here because new nodes may be produced.
2596 static ir_node *transform_node_Mul2n(ir_node *n, ir_mode *mode)
2599 ir_node *a = get_Mul_left(n);
2600 ir_node *b = get_Mul_right(n);
2601 ir_tarval *ta = value_of(a);
2602 ir_tarval *tb = value_of(b);
2603 ir_mode *smode = get_irn_mode(a);
2605 if (ta == get_mode_one(smode)) {
2606 /* (L)1 * (L)b = (L)b */
2607 ir_node *blk = get_nodes_block(n);
2608 n = new_rd_Conv(get_irn_dbg_info(n), blk, b, mode);
2609 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
2612 else if (ta == get_mode_minus_one(smode)) {
2613 /* (L)-1 * (L)b = (L)b */
2614 ir_node *blk = get_nodes_block(n);
2615 n = new_rd_Minus(get_irn_dbg_info(n), blk, b, smode);
2616 n = new_rd_Conv(get_irn_dbg_info(n), blk, n, mode);
2617 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2620 if (tb == get_mode_one(smode)) {
2621 /* (L)a * (L)1 = (L)a */
2622 ir_node *blk = get_irn_n(a, -1);
2623 n = new_rd_Conv(get_irn_dbg_info(n), blk, a, mode);
2624 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
2627 else if (tb == get_mode_minus_one(smode)) {
2628 /* (L)a * (L)-1 = (L)-a */
2629 ir_node *blk = get_nodes_block(n);
2630 n = new_rd_Minus(get_irn_dbg_info(n), blk, a, smode);
2631 n = new_rd_Conv(get_irn_dbg_info(n), blk, n, mode);
2632 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2639 * Transform Mul(a,-1) into -a.
2640 * Do constant evaluation of Phi nodes.
2641 * Do architecture dependent optimizations on Mul nodes
2643 static ir_node *transform_node_Mul(ir_node *n)
2645 ir_node *c, *oldn = n;
2646 ir_mode *mode = get_irn_mode(n);
2647 ir_node *a = get_Mul_left(n);
2648 ir_node *b = get_Mul_right(n);
2650 if (is_Bad(a) || is_Bad(b))
2653 if (mode != get_irn_mode(a))
2654 return transform_node_Mul2n(n, mode);
2656 HANDLE_BINOP_PHI((eval_func) tarval_mul, a, b, c, mode);
2658 if (mode_is_signed(mode)) {
2661 if (value_of(a) == get_mode_minus_one(mode))
2663 else if (value_of(b) == get_mode_minus_one(mode))
2666 n = new_rd_Minus(get_irn_dbg_info(n), get_nodes_block(n), r, mode);
2667 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2672 if (is_Const(b)) { /* (-a) * const -> a * -const */
2673 ir_node *cnst = const_negate(b);
2675 dbg_info *dbgi = get_irn_dbg_info(n);
2676 ir_node *block = get_nodes_block(n);
2677 n = new_rd_Mul(dbgi, block, get_Minus_op(a), cnst, mode);
2678 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2681 } else if (is_Minus(b)) { /* (-a) * (-b) -> a * b */
2682 dbg_info *dbgi = get_irn_dbg_info(n);
2683 ir_node *block = get_nodes_block(n);
2684 n = new_rd_Mul(dbgi, block, get_Minus_op(a), get_Minus_op(b), mode);
2685 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_MINUS);
2687 } else if (is_Sub(b)) { /* (-a) * (b - c) -> a * (c - b) */
2688 ir_node *sub_l = get_Sub_left(b);
2689 ir_node *sub_r = get_Sub_right(b);
2690 dbg_info *dbgi = get_irn_dbg_info(n);
2691 ir_node *block = get_nodes_block(n);
2692 ir_node *new_b = new_rd_Sub(dbgi, block, sub_r, sub_l, mode);
2693 n = new_rd_Mul(dbgi, block, get_Minus_op(a), new_b, mode);
2694 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS);
2697 } else if (is_Minus(b)) {
2698 if (is_Sub(a)) { /* (a - b) * (-c) -> (b - a) * c */
2699 ir_node *sub_l = get_Sub_left(a);
2700 ir_node *sub_r = get_Sub_right(a);
2701 dbg_info *dbgi = get_irn_dbg_info(n);
2702 ir_node *block = get_nodes_block(n);
2703 ir_node *new_a = new_rd_Sub(dbgi, block, sub_r, sub_l, mode);
2704 n = new_rd_Mul(dbgi, block, new_a, get_Minus_op(b), mode);
2705 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS);
2708 } else if (is_Shl(a)) {
2709 ir_node *const shl_l = get_Shl_left(a);
2710 if (is_Const(shl_l) && is_Const_one(shl_l)) {
2711 /* (1 << x) * b -> b << x */
2712 dbg_info *const dbgi = get_irn_dbg_info(n);
2713 ir_node *const block = get_nodes_block(n);
2714 ir_node *const shl_r = get_Shl_right(a);
2715 n = new_rd_Shl(dbgi, block, b, shl_r, mode);
2716 // TODO add me DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_SHIFT);
2719 } else if (is_Shl(b)) {
2720 ir_node *const shl_l = get_Shl_left(b);
2721 if (is_Const(shl_l) && is_Const_one(shl_l)) {
2722 /* a * (1 << x) -> a << x */
2723 dbg_info *const dbgi = get_irn_dbg_info(n);
2724 ir_node *const block = get_nodes_block(n);
2725 ir_node *const shl_r = get_Shl_right(b);
2726 n = new_rd_Shl(dbgi, block, a, shl_r, mode);
2727 // TODO add me DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_SHIFT);
2731 if (get_mode_arithmetic(mode) == irma_ieee754) {
2733 ir_tarval *tv = get_Const_tarval(a);
2734 if (tarval_ieee754_get_exponent(tv) == 1 && tarval_ieee754_zero_mantissa(tv)
2735 && !tarval_is_negative(tv)) {
2736 /* 2.0 * b = b + b */
2737 n = new_rd_Add(get_irn_dbg_info(n), get_nodes_block(n), b, b, mode);
2738 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_A_A);
2742 else if (is_Const(b)) {
2743 ir_tarval *tv = get_Const_tarval(b);
2744 if (tarval_ieee754_get_exponent(tv) == 1 && tarval_ieee754_zero_mantissa(tv)
2745 && !tarval_is_negative(tv)) {
2746 /* a * 2.0 = a + a */
2747 n = new_rd_Add(get_irn_dbg_info(n), get_nodes_block(n), a, a, mode);
2748 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_A_A);
2753 return arch_dep_replace_mul_with_shifts(n);
2754 } /* transform_node_Mul */
2757 * Transform a Div Node.
2759 static ir_node *transform_node_Div(ir_node *n)
2761 ir_mode *mode = get_Div_resmode(n);
2762 ir_node *a = get_Div_left(n);
2763 ir_node *b = get_Div_right(n);
2765 const ir_node *dummy;
2767 if (mode_is_int(mode)) {
2768 if (is_Const(b) && is_const_Phi(a)) {
2769 /* check for Div(Phi, Const) */
2770 value = apply_binop_on_phi(a, get_Const_tarval(b), (eval_func) tarval_div, mode, 0);
2772 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2775 } else if (is_Const(a) && is_const_Phi(b)) {
2776 /* check for Div(Const, Phi) */
2777 value = apply_binop_on_phi(b, get_Const_tarval(a), (eval_func) tarval_div, mode, 1);
2779 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2782 } else if (is_const_Phi(a) && is_const_Phi(b)) {
2783 /* check for Div(Phi, Phi) */
2784 value = apply_binop_on_2_phis(a, b, (eval_func) tarval_div, mode);
2786 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2791 if (a == b && value_not_zero(a, &dummy)) {
2792 ir_graph *irg = get_irn_irg(n);
2793 /* BEWARE: we can optimize a/a to 1 only if this cannot cause a exception */
2794 value = new_r_Const(irg, get_mode_one(mode));
2795 DBG_OPT_CSTEVAL(n, value);
2798 if (mode_is_signed(mode) && is_Const(b)) {
2799 ir_tarval *tv = get_Const_tarval(b);
2801 if (tv == get_mode_minus_one(mode)) {
2803 value = new_rd_Minus(get_irn_dbg_info(n), get_nodes_block(n), a, mode);
2804 DBG_OPT_CSTEVAL(n, value);
2808 /* Try architecture dependent optimization */
2809 value = arch_dep_replace_div_by_const(n);
2812 assert(mode_is_float(mode));
2814 /* Optimize x/c to x*(1/c) */
2815 if (get_mode_arithmetic(mode) == irma_ieee754) {
2816 ir_tarval *tv = value_of(b);
2818 if (tv != tarval_bad) {
2819 int rem = tarval_fp_ops_enabled();
2822 * Floating point constant folding might be disabled here to
2824 * However, as we check for exact result, doing it is safe.
2827 tarval_enable_fp_ops(1);
2828 tv = tarval_div(get_mode_one(mode), tv);
2829 tarval_enable_fp_ops(rem);
2831 /* Do the transformation if the result is either exact or we are
2832 not using strict rules. */
2833 if (tv != tarval_bad &&
2834 (tarval_ieee754_get_exact() || (get_irg_fp_model(get_irn_irg(n)) & fp_strict_algebraic) == 0)) {
2835 ir_node *block = get_nodes_block(n);
2836 ir_graph *irg = get_irn_irg(block);
2837 ir_node *c = new_r_Const(irg, tv);
2838 dbg_info *dbgi = get_irn_dbg_info(n);
2839 value = new_rd_Mul(dbgi, block, a, c, mode);
2852 /* Turn Div into a tuple (mem, jmp, bad, value) */
2853 mem = get_Div_mem(n);
2854 blk = get_nodes_block(n);
2855 irg = get_irn_irg(blk);
2857 /* skip a potential Pin */
2858 mem = skip_Pin(mem);
2859 turn_into_tuple(n, pn_Div_max);
2860 set_Tuple_pred(n, pn_Div_M, mem);
2861 set_Tuple_pred(n, pn_Div_X_regular, new_r_Jmp(blk));
2862 set_Tuple_pred(n, pn_Div_X_except, get_irg_bad(irg));
2863 set_Tuple_pred(n, pn_Div_res, value);
2866 } /* transform_node_Div */
2869 * Transform a Mod node.
2871 static ir_node *transform_node_Mod(ir_node *n)
2873 ir_mode *mode = get_Mod_resmode(n);
2874 ir_node *a = get_Mod_left(n);
2875 ir_node *b = get_Mod_right(n);
2880 if (is_Const(b) && is_const_Phi(a)) {
2881 /* check for Div(Phi, Const) */
2882 value = apply_binop_on_phi(a, get_Const_tarval(b), (eval_func) tarval_mod, mode, 0);
2884 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2888 else if (is_Const(a) && is_const_Phi(b)) {
2889 /* check for Div(Const, Phi) */
2890 value = apply_binop_on_phi(b, get_Const_tarval(a), (eval_func) tarval_mod, mode, 1);
2892 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2896 else if (is_const_Phi(a) && is_const_Phi(b)) {
2897 /* check for Div(Phi, Phi) */
2898 value = apply_binop_on_2_phis(a, b, (eval_func) tarval_mod, mode);
2900 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2907 irg = get_irn_irg(n);
2908 if (tv != tarval_bad) {
2909 value = new_r_Const(irg, tv);
2911 DBG_OPT_CSTEVAL(n, value);
2914 ir_node *a = get_Mod_left(n);
2915 ir_node *b = get_Mod_right(n);
2916 const ir_node *dummy;
2918 if (a == b && value_not_zero(a, &dummy)) {
2919 /* BEWARE: we can optimize a%a to 0 only if this cannot cause a exception */
2920 value = new_r_Const(irg, get_mode_null(mode));
2921 DBG_OPT_CSTEVAL(n, value);
2924 if (mode_is_signed(mode) && is_Const(b)) {
2925 ir_tarval *tv = get_Const_tarval(b);
2927 if (tv == get_mode_minus_one(mode)) {
2929 value = new_r_Const(irg, get_mode_null(mode));
2930 DBG_OPT_CSTEVAL(n, value);
2934 /* Try architecture dependent optimization */
2935 value = arch_dep_replace_mod_by_const(n);
2944 /* Turn Mod into a tuple (mem, jmp, bad, value) */
2945 mem = get_Mod_mem(n);
2946 blk = get_nodes_block(n);
2947 irg = get_irn_irg(blk);
2949 /* skip a potential Pin */
2950 mem = skip_Pin(mem);
2951 turn_into_tuple(n, pn_Mod_max);
2952 set_Tuple_pred(n, pn_Mod_M, mem);
2953 set_Tuple_pred(n, pn_Mod_X_regular, new_r_Jmp(blk));
2954 set_Tuple_pred(n, pn_Mod_X_except, get_irg_bad(irg));
2955 set_Tuple_pred(n, pn_Mod_res, value);
2958 } /* transform_node_Mod */
2961 * Transform a Cond node.
2963 * Replace the Cond by a Jmp if it branches on a constant
2966 static ir_node *transform_node_Cond(ir_node *n)
2969 ir_node *a = get_Cond_selector(n);
2970 ir_tarval *ta = value_of(a);
2971 ir_graph *irg = get_irn_irg(n);
2974 /* we need block info which is not available in floating irgs */
2975 if (get_irg_pinned(irg) == op_pin_state_floats)
2978 if ((ta != tarval_bad) &&
2979 (get_irn_mode(a) == mode_b) &&
2980 (get_opt_unreachable_code())) {
2981 /* It's a boolean Cond, branching on a boolean constant.
2982 Replace it by a tuple (Bad, Jmp) or (Jmp, Bad) */
2983 ir_node *blk = get_nodes_block(n);
2984 jmp = new_r_Jmp(blk);
2985 turn_into_tuple(n, pn_Cond_max);
2986 if (ta == tarval_b_true) {
2987 set_Tuple_pred(n, pn_Cond_false, get_irg_bad(irg));
2988 set_Tuple_pred(n, pn_Cond_true, jmp);
2990 set_Tuple_pred(n, pn_Cond_false, jmp);
2991 set_Tuple_pred(n, pn_Cond_true, get_irg_bad(irg));
2993 /* We might generate an endless loop, so keep it alive. */
2994 add_End_keepalive(get_irg_end(irg), blk);
2997 } /* transform_node_Cond */
3000 * Prototype of a recursive transform function
3001 * for bitwise distributive transformations.
3003 typedef ir_node* (*recursive_transform)(ir_node *n);
3006 * makes use of distributive laws for and, or, eor
3007 * and(a OP c, b OP c) -> and(a, b) OP c
3008 * note, might return a different op than n
3010 static ir_node *transform_bitwise_distributive(ir_node *n,
3011 recursive_transform trans_func)
3014 ir_node *a = get_binop_left(n);
3015 ir_node *b = get_binop_right(n);
3016 ir_op *op = get_irn_op(a);
3017 ir_op *op_root = get_irn_op(n);
3019 if (op != get_irn_op(b))
3022 /* and(conv(a), conv(b)) -> conv(and(a,b)) */
3023 if (op == op_Conv) {
3024 ir_node *a_op = get_Conv_op(a);
3025 ir_node *b_op = get_Conv_op(b);
3026 ir_mode *a_mode = get_irn_mode(a_op);
3027 ir_mode *b_mode = get_irn_mode(b_op);
3028 if (a_mode == b_mode && (mode_is_int(a_mode) || a_mode == mode_b)) {
3029 ir_node *blk = get_nodes_block(n);
3032 set_binop_left(n, a_op);
3033 set_binop_right(n, b_op);
3034 set_irn_mode(n, a_mode);
3036 n = new_r_Conv(blk, n, get_irn_mode(oldn));
3038 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
3044 /* nothing to gain here */
3048 if (op == op_Shrs || op == op_Shr || op == op_Shl
3049 || op == op_And || op == op_Or || op == op_Eor) {
3050 ir_node *a_left = get_binop_left(a);
3051 ir_node *a_right = get_binop_right(a);
3052 ir_node *b_left = get_binop_left(b);
3053 ir_node *b_right = get_binop_right(b);
3055 ir_node *op1 = NULL;
3056 ir_node *op2 = NULL;
3058 if (is_op_commutative(op)) {
3059 if (a_left == b_left) {
3063 } else if (a_left == b_right) {
3067 } else if (a_right == b_left) {
3073 if (a_right == b_right) {
3080 /* (a sop c) & (b sop c) => (a & b) sop c */
3081 ir_node *blk = get_nodes_block(n);
3083 ir_node *new_n = exact_copy(n);
3084 set_binop_left(new_n, op1);
3085 set_binop_right(new_n, op2);
3086 new_n = trans_func(new_n);
3088 if (op_root == op_Eor && op == op_Or) {
3089 dbg_info *dbgi = get_irn_dbg_info(n);
3090 ir_mode *mode = get_irn_mode(c);
3092 c = new_rd_Not(dbgi, blk, c, mode);
3093 n = new_rd_And(dbgi, blk, new_n, c, mode);
3096 set_nodes_block(n, blk);
3097 set_binop_left(n, new_n);
3098 set_binop_right(n, c);
3102 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_SHIFT_AND);
3110 int ir_is_equality_cmp_0(const ir_node *node)
3112 ir_relation relation = get_Cmp_relation(node);
3113 ir_node *left = get_Cmp_left(node);
3114 ir_node *right = get_Cmp_right(node);
3115 ir_mode *mode = get_irn_mode(left);
3117 /* this probably makes no sense if unordered is involved */
3118 assert(!mode_is_float(mode));
3120 if (!is_Const(right) || !is_Const_null(right))
3122 if (relation == ir_relation_equal)
3124 if (mode_is_signed(mode) && relation == ir_relation_less_greater)
3126 if (relation == ir_relation_greater)
3132 * Create a 0 constant of given mode.
3134 static ir_node *create_zero_const(ir_graph *irg, ir_mode *mode)
3136 ir_tarval *tv = get_mode_null(mode);
3137 ir_node *cnst = new_r_Const(irg, tv);
3145 static ir_node *transform_node_And(ir_node *n)
3147 ir_node *c, *oldn = n;
3148 ir_node *a = get_And_left(n);
3149 ir_node *b = get_And_right(n);
3151 vrp_attr *a_vrp, *b_vrp;
3153 if (is_Cmp(a) && is_Cmp(b)) {
3154 ir_node *a_left = get_Cmp_left(a);
3155 ir_node *a_right = get_Cmp_left(a);
3156 ir_node *b_left = get_Cmp_left(b);
3157 ir_node *b_right = get_Cmp_right(b);
3158 /* we can combine the relations of two compares with the same
3160 if (a_left == b_left && b_left == b_right) {
3161 dbg_info *dbgi = get_irn_dbg_info(n);
3162 ir_node *block = get_nodes_block(n);
3163 ir_relation a_relation = get_Cmp_relation(a);
3164 ir_relation b_relation = get_Cmp_relation(b);
3165 ir_relation new_relation = a_relation & b_relation;
3166 return new_rd_Cmp(dbgi, block, a_left, a_right, new_relation);
3168 /* Cmp(a, 0) and Cmp(b,0) can be optimized to Cmp(a|b, 0) */
3169 if (ir_is_equality_cmp_0(a) && ir_is_equality_cmp_0(b)
3170 && (get_Cmp_relation(a) & ir_relation_equal) == (get_Cmp_relation(b) & ir_relation_equal)) {
3171 dbg_info *dbgi = get_irn_dbg_info(n);
3172 ir_node *block = get_nodes_block(n);
3173 ir_relation relation = get_Cmp_relation(a);
3174 ir_mode *mode = get_irn_mode(a_left);
3175 ir_node *n_b_left = b_left;
3176 if (get_irn_mode(n_b_left) != mode) {
3177 n_b_left = new_rd_Conv(dbgi, block, b_left, mode);
3179 ir_node *or = new_rd_Or(dbgi, block, a_left, n_b_left, mode);
3180 ir_graph *irg = get_irn_irg(n);
3181 ir_node *zero = create_zero_const(irg, mode);
3182 return new_rd_Cmp(dbgi, block, or, zero, relation);
3186 mode = get_irn_mode(n);
3187 HANDLE_BINOP_PHI((eval_func) tarval_and, a, b, c, mode);
3191 ir_node *op = get_Not_op(b);
3193 ir_node *ba = get_And_left(op);
3194 ir_node *bb = get_And_right(op);
3196 /* it's enough to test the following cases due to normalization! */
3197 if (get_Or_left(a) == ba && get_Or_right(a) == bb) {
3198 /* (a|b) & ~(a&b) = a^b */
3199 ir_node *block = get_nodes_block(n);
3201 n = new_rd_Eor(get_irn_dbg_info(n), block, ba, bb, mode);
3202 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_TO_EOR);
3210 ir_node *op = get_Not_op(a);
3212 ir_node *aa = get_And_left(op);
3213 ir_node *ab = get_And_right(op);
3215 /* it's enough to test the following cases due to normalization! */
3216 if (get_Or_left(b) == aa && get_Or_right(b) == ab) {
3217 /* (a|b) & ~(a&b) = a^b */
3218 ir_node *block = get_nodes_block(n);
3220 n = new_rd_Eor(get_irn_dbg_info(n), block, aa, ab, mode);
3221 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_TO_EOR);
3228 ir_node *al = get_Eor_left(a);
3229 ir_node *ar = get_Eor_right(a);
3232 /* (b ^ a) & b -> ~a & b */
3233 dbg_info *dbg = get_irn_dbg_info(n);
3234 ir_node *block = get_nodes_block(n);
3236 ar = new_rd_Not(dbg, block, ar, mode);
3237 n = new_rd_And(dbg, block, ar, b, mode);
3238 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3242 /* (a ^ b) & b -> ~a & b */
3243 dbg_info *dbg = get_irn_dbg_info(n);
3244 ir_node *block = get_nodes_block(n);
3246 al = new_rd_Not(dbg, block, al, mode);
3247 n = new_rd_And(dbg, block, al, b, mode);
3248 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3253 ir_node *bl = get_Eor_left(b);
3254 ir_node *br = get_Eor_right(b);
3257 /* a & (a ^ b) -> a & ~b */
3258 dbg_info *dbg = get_irn_dbg_info(n);
3259 ir_node *block = get_nodes_block(n);
3261 br = new_rd_Not(dbg, block, br, mode);
3262 n = new_rd_And(dbg, block, br, a, mode);
3263 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3267 /* a & (b ^ a) -> a & ~b */
3268 dbg_info *dbg = get_irn_dbg_info(n);
3269 ir_node *block = get_nodes_block(n);
3271 bl = new_rd_Not(dbg, block, bl, mode);
3272 n = new_rd_And(dbg, block, bl, a, mode);
3273 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3277 if (is_Not(a) && is_Not(b)) {
3278 /* ~a & ~b = ~(a|b) */
3279 ir_node *block = get_nodes_block(n);
3280 ir_mode *mode = get_irn_mode(n);
3284 n = new_rd_Or(get_irn_dbg_info(n), block, a, b, mode);
3285 n = new_rd_Not(get_irn_dbg_info(n), block, n, mode);
3286 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_DEMORGAN);
3290 b_vrp = vrp_get_info(b);
3291 if (is_Const(a) && b_vrp && (tarval_cmp(tarval_or(get_Const_tarval(a),
3292 b_vrp->bits_not_set), get_Const_tarval(a)) == ir_relation_equal)) {
3298 a_vrp = vrp_get_info(a);
3299 if (is_Const(b) && a_vrp && (tarval_cmp(tarval_or(get_Const_tarval(b),
3300 a_vrp->bits_not_set), get_Const_tarval(b)) == ir_relation_equal)) {
3304 n = transform_bitwise_distributive(n, transform_node_And);
3307 } /* transform_node_And */
3309 /* the order of the values is important! */
3310 typedef enum const_class {
3316 static const_class classify_const(const ir_node* n)
3318 if (is_Const(n)) return const_const;
3319 if (is_irn_constlike(n)) return const_like;
3324 * Determines whether r is more constlike or has a larger index (in that order)
3327 static bool operands_are_normalized(const ir_node *l, const ir_node *r)
3329 const const_class l_order = classify_const(l);
3330 const const_class r_order = classify_const(r);
3332 l_order > r_order ||
3333 (l_order == r_order && get_irn_idx(l) <= get_irn_idx(r));
3339 static ir_node *transform_node_Eor(ir_node *n)
3341 ir_node *c, *oldn = n;
3342 ir_node *a = get_Eor_left(n);
3343 ir_node *b = get_Eor_right(n);
3344 ir_mode *mode = get_irn_mode(n);
3346 /* we can combine the relations of two compares with the same operands */
3347 if (is_Cmp(a) && is_Cmp(b)) {
3348 ir_node *a_left = get_Cmp_left(a);
3349 ir_node *a_right = get_Cmp_left(a);
3350 ir_node *b_left = get_Cmp_left(b);
3351 ir_node *b_right = get_Cmp_right(b);
3352 if (a_left == b_left && b_left == b_right) {
3353 dbg_info *dbgi = get_irn_dbg_info(n);
3354 ir_node *block = get_nodes_block(n);
3355 ir_relation a_relation = get_Cmp_relation(a);
3356 ir_relation b_relation = get_Cmp_relation(b);
3357 ir_relation new_relation = a_relation ^ b_relation;
3358 return new_rd_Cmp(dbgi, block, a_left, a_right, new_relation);
3362 HANDLE_BINOP_PHI((eval_func) tarval_eor, a, b, c, mode);
3364 /* normalize not nodes... ~a ^ b <=> a ^ ~b */
3365 if (is_Not(a) && operands_are_normalized(get_Not_op(a), b)) {
3366 dbg_info *dbg = get_irn_dbg_info(n);
3367 ir_node *block = get_nodes_block(n);
3368 ir_node *new_not = new_rd_Not(dbg, block, b, mode);
3369 ir_node *new_left = get_Not_op(a);
3370 n = new_rd_Eor(dbg, block, new_left, new_not, mode);
3371 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3373 } else if (is_Not(b) && !operands_are_normalized(a, get_Not_op(b))) {
3374 dbg_info *dbg = get_irn_dbg_info(n);
3375 ir_node *block = get_nodes_block(n);
3376 ir_node *new_not = new_rd_Not(dbg, block, a, mode);
3377 ir_node *new_right = get_Not_op(b);
3378 n = new_rd_Eor(dbg, block, new_not, new_right, mode);
3379 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3383 /* x ^ 1...1 -> ~1 */
3384 if (is_Const(b) && is_Const_all_one(b)) {
3385 n = new_r_Not(get_nodes_block(n), a, mode);
3386 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3390 n = transform_bitwise_distributive(n, transform_node_Eor);
3392 } /* transform_node_Eor */
3397 static ir_node *transform_node_Not(ir_node *n)
3399 ir_node *c, *oldn = n;
3400 ir_node *a = get_Not_op(n);
3401 ir_mode *mode = get_irn_mode(n);
3403 HANDLE_UNOP_PHI(tarval_not,a,c);
3405 /* check for a boolean Not */
3407 dbg_info *dbgi = get_irn_dbg_info(a);
3408 ir_node *block = get_nodes_block(a);
3409 ir_relation relation = get_Cmp_relation(a);
3410 relation = get_negated_relation(relation);
3411 n = new_rd_Cmp(dbgi, block, get_Cmp_left(a), get_Cmp_right(a), relation);
3412 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_CMP);
3416 /* normalize ~(a ^ b) => a ^ ~b */
3418 dbg_info *dbg = get_irn_dbg_info(n);
3419 ir_node *block = get_nodes_block(n);
3420 ir_node *eor_right = get_Eor_right(a);
3421 ir_node *eor_left = get_Eor_left(a);
3422 eor_right = new_rd_Not(dbg, block, eor_right, mode);
3423 n = new_rd_Eor(dbg, block, eor_left, eor_right, mode);
3427 if (get_mode_arithmetic(mode) == irma_twos_complement) {
3428 if (is_Minus(a)) { /* ~-x -> x + -1 */
3429 dbg_info *dbg = get_irn_dbg_info(n);
3430 ir_graph *irg = get_irn_irg(n);
3431 ir_node *block = get_nodes_block(n);
3432 ir_node *add_l = get_Minus_op(a);
3433 ir_node *add_r = new_rd_Const(dbg, irg, get_mode_minus_one(mode));
3434 n = new_rd_Add(dbg, block, add_l, add_r, mode);
3435 } else if (is_Add(a)) {
3436 ir_node *add_r = get_Add_right(a);
3437 if (is_Const(add_r) && is_Const_all_one(add_r)) {
3438 /* ~(x + -1) = -x */
3439 ir_node *op = get_Add_left(a);
3440 ir_node *blk = get_nodes_block(n);
3441 n = new_rd_Minus(get_irn_dbg_info(n), blk, op, get_irn_mode(n));
3442 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_MINUS_1);
3450 * Transform a Minus.
3454 * -(a >>u (size-1)) = a >>s (size-1)
3455 * -(a >>s (size-1)) = a >>u (size-1)
3456 * -(a * const) -> a * -const
3458 static ir_node *transform_node_Minus(ir_node *n)
3460 ir_node *c, *oldn = n;
3461 ir_node *a = get_Minus_op(n);
3464 HANDLE_UNOP_PHI(tarval_neg,a,c);
3466 mode = get_irn_mode(a);
3467 if (get_mode_arithmetic(mode) == irma_twos_complement) {
3468 /* the following rules are only to twos-complement */
3471 ir_node *op = get_Not_op(a);
3472 ir_tarval *tv = get_mode_one(mode);
3473 ir_node *blk = get_nodes_block(n);
3474 ir_graph *irg = get_irn_irg(blk);
3475 ir_node *c = new_r_Const(irg, tv);
3476 n = new_rd_Add(get_irn_dbg_info(n), blk, op, c, mode);
3477 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_MINUS_NOT);
3481 ir_node *c = get_Shr_right(a);
3484 ir_tarval *tv = get_Const_tarval(c);
3486 if (tarval_is_long(tv) && get_tarval_long(tv) == (int) get_mode_size_bits(mode) - 1) {
3487 /* -(a >>u (size-1)) = a >>s (size-1) */
3488 ir_node *v = get_Shr_left(a);
3490 n = new_rd_Shrs(get_irn_dbg_info(n), get_nodes_block(n), v, c, mode);
3491 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_PREDICATE);
3497 ir_node *c = get_Shrs_right(a);
3500 ir_tarval *tv = get_Const_tarval(c);
3502 if (tarval_is_long(tv) && get_tarval_long(tv) == (int) get_mode_size_bits(mode) - 1) {
3503 /* -(a >>s (size-1)) = a >>u (size-1) */
3504 ir_node *v = get_Shrs_left(a);
3506 n = new_rd_Shr(get_irn_dbg_info(n), get_nodes_block(n), v, c, mode);
3507 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_PREDICATE);
3514 /* - (a-b) = b - a */
3515 ir_node *la = get_Sub_left(a);
3516 ir_node *ra = get_Sub_right(a);
3517 ir_node *blk = get_nodes_block(n);
3519 n = new_rd_Sub(get_irn_dbg_info(n), blk, ra, la, mode);
3520 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_MINUS_SUB);
3524 if (is_Mul(a)) { /* -(a * const) -> a * -const */
3525 ir_node *mul_l = get_Mul_left(a);
3526 ir_node *mul_r = get_Mul_right(a);
3527 ir_tarval *tv = value_of(mul_r);
3528 if (tv != tarval_bad) {
3529 tv = tarval_neg(tv);
3530 if (tv != tarval_bad) {
3531 ir_graph *irg = get_irn_irg(n);
3532 ir_node *cnst = new_r_Const(irg, tv);
3533 dbg_info *dbg = get_irn_dbg_info(a);
3534 ir_node *block = get_nodes_block(a);
3535 n = new_rd_Mul(dbg, block, mul_l, cnst, mode);
3536 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_MINUS_MUL_C);
3543 } /* transform_node_Minus */
3546 * Transform a Proj(Load) with a non-null address.
3548 static ir_node *transform_node_Proj_Load(ir_node *proj)
3550 if (get_opt_ldst_only_null_ptr_exceptions()) {
3551 if (get_irn_mode(proj) == mode_X) {
3552 ir_node *load = get_Proj_pred(proj);
3554 /* get the Load address */
3555 const ir_node *addr = get_Load_ptr(load);
3556 const ir_node *confirm;
3558 if (value_not_null(addr, &confirm)) {
3559 if (confirm == NULL) {
3560 /* this node may float if it did not depend on a Confirm */
3561 set_irn_pinned(load, op_pin_state_floats);
3563 if (get_Proj_proj(proj) == pn_Load_X_except) {
3564 ir_graph *irg = get_irn_irg(proj);
3565 DBG_OPT_EXC_REM(proj);
3566 return get_irg_bad(irg);
3568 ir_node *blk = get_nodes_block(load);
3569 return new_r_Jmp(blk);
3575 } /* transform_node_Proj_Load */
3578 * Transform a Proj(Store) with a non-null address.
3580 static ir_node *transform_node_Proj_Store(ir_node *proj)
3582 if (get_opt_ldst_only_null_ptr_exceptions()) {
3583 if (get_irn_mode(proj) == mode_X) {
3584 ir_node *store = get_Proj_pred(proj);
3586 /* get the load/store address */
3587 const ir_node *addr = get_Store_ptr(store);
3588 const ir_node *confirm;
3590 if (value_not_null(addr, &confirm)) {
3591 if (confirm == NULL) {
3592 /* this node may float if it did not depend on a Confirm */
3593 set_irn_pinned(store, op_pin_state_floats);
3595 if (get_Proj_proj(proj) == pn_Store_X_except) {
3596 ir_graph *irg = get_irn_irg(proj);
3597 DBG_OPT_EXC_REM(proj);
3598 return get_irg_bad(irg);
3600 ir_node *blk = get_nodes_block(store);
3601 return new_r_Jmp(blk);
3607 } /* transform_node_Proj_Store */
3610 * Transform a Proj(Div) with a non-zero value.
3611 * Removes the exceptions and routes the memory to the NoMem node.
3613 static ir_node *transform_node_Proj_Div(ir_node *proj)
3615 ir_node *div = get_Proj_pred(proj);
3616 ir_node *b = get_Div_right(div);
3617 ir_node *res, *new_mem;
3618 const ir_node *confirm;
3621 if (value_not_zero(b, &confirm)) {
3622 /* div(x, y) && y != 0 */
3623 if (confirm == NULL) {
3624 /* we are sure we have a Const != 0 */
3625 new_mem = get_Div_mem(div);
3626 new_mem = skip_Pin(new_mem);
3627 set_Div_mem(div, new_mem);
3628 set_irn_pinned(div, op_pin_state_floats);
3631 proj_nr = get_Proj_proj(proj);
3633 case pn_Div_X_regular:
3634 return new_r_Jmp(get_nodes_block(div));
3636 case pn_Div_X_except: {
3637 ir_graph *irg = get_irn_irg(proj);
3638 /* we found an exception handler, remove it */
3639 DBG_OPT_EXC_REM(proj);
3640 return get_irg_bad(irg);
3644 ir_graph *irg = get_irn_irg(proj);
3645 res = get_Div_mem(div);
3646 new_mem = get_irg_no_mem(irg);
3649 /* This node can only float up to the Confirm block */
3650 new_mem = new_r_Pin(get_nodes_block(confirm), new_mem);
3652 set_irn_pinned(div, op_pin_state_floats);
3653 /* this is a Div without exception, we can remove the memory edge */
3654 set_Div_mem(div, new_mem);
3660 } /* transform_node_Proj_Div */
3663 * Transform a Proj(Mod) with a non-zero value.
3664 * Removes the exceptions and routes the memory to the NoMem node.
3666 static ir_node *transform_node_Proj_Mod(ir_node *proj)
3668 ir_node *mod = get_Proj_pred(proj);
3669 ir_node *b = get_Mod_right(mod);
3670 ir_node *res, *new_mem;
3671 const ir_node *confirm;
3674 if (value_not_zero(b, &confirm)) {
3675 /* mod(x, y) && y != 0 */
3676 proj_nr = get_Proj_proj(proj);
3678 if (confirm == NULL) {
3679 /* we are sure we have a Const != 0 */
3680 new_mem = get_Mod_mem(mod);
3681 new_mem = skip_Pin(new_mem);
3682 set_Mod_mem(mod, new_mem);
3683 set_irn_pinned(mod, op_pin_state_floats);
3688 case pn_Mod_X_regular:
3689 return new_r_Jmp(get_irn_n(mod, -1));
3691 case pn_Mod_X_except: {
3692 ir_graph *irg = get_irn_irg(proj);
3693 /* we found an exception handler, remove it */
3694 DBG_OPT_EXC_REM(proj);
3695 return get_irg_bad(irg);
3699 ir_graph *irg = get_irn_irg(proj);
3700 res = get_Mod_mem(mod);
3701 new_mem = get_irg_no_mem(irg);
3704 /* This node can only float up to the Confirm block */
3705 new_mem = new_r_Pin(get_nodes_block(confirm), new_mem);
3707 /* this is a Mod without exception, we can remove the memory edge */
3708 set_Mod_mem(mod, new_mem);
3712 if (get_Mod_left(mod) == b) {
3713 /* a % a = 0 if a != 0 */
3714 ir_graph *irg = get_irn_irg(proj);
3715 ir_mode *mode = get_irn_mode(proj);
3716 ir_node *res = new_r_Const(irg, get_mode_null(mode));
3718 DBG_OPT_CSTEVAL(mod, res);
3724 } /* transform_node_Proj_Mod */
3727 * Optimizes jump tables (CondIs or CondIu) by removing all impossible cases.
3729 static ir_node *transform_node_Proj_Cond(ir_node *proj)
3731 ir_node *n = get_Proj_pred(proj);
3732 ir_node *b = get_Cond_selector(n);
3734 if (!get_opt_unreachable_code())
3737 if (mode_is_int(get_irn_mode(b))) {
3738 ir_tarval *tb = value_of(b);
3740 if (tb != tarval_bad) {
3741 /* we have a constant switch */
3742 long num = get_Proj_proj(proj);
3744 if (num != get_Cond_default_proj(n)) { /* we cannot optimize default Proj's yet */
3745 if (get_tarval_long(tb) == num) {
3746 /* Do NOT create a jump here, or we will have 2 control flow ops
3747 * in a block. This case is optimized away in optimize_cf(). */
3750 ir_graph *irg = get_irn_irg(proj);
3751 /* this case will NEVER be taken, kill it */
3752 return get_irg_bad(irg);
3756 long num = get_Proj_proj(proj);
3757 vrp_attr *b_vrp = vrp_get_info(b);
3758 if (num != get_Cond_default_proj(n) && b_vrp) {
3759 /* Try handling with vrp data. We only remove dead parts. */
3760 ir_tarval *tp = new_tarval_from_long(num, get_irn_mode(b));
3762 if (b_vrp->range_type == VRP_RANGE) {
3763 ir_relation cmp_result = tarval_cmp(b_vrp->range_bottom, tp);
3764 ir_relation cmp_result2 = tarval_cmp(b_vrp->range_top, tp);
3766 if ((cmp_result & ir_relation_greater) == cmp_result && (cmp_result2
3767 & ir_relation_less) == cmp_result2) {
3768 ir_graph *irg = get_irn_irg(proj);
3769 return get_irg_bad(irg);
3771 } else if (b_vrp->range_type == VRP_ANTIRANGE) {
3772 ir_relation cmp_result = tarval_cmp(b_vrp->range_bottom, tp);
3773 ir_relation cmp_result2 = tarval_cmp(b_vrp->range_top, tp);
3775 if ((cmp_result & ir_relation_less_equal) == cmp_result && (cmp_result2
3776 & ir_relation_greater_equal) == cmp_result2) {
3777 ir_graph *irg = get_irn_irg(proj);
3778 return get_irg_bad(irg);
3783 tarval_and( b_vrp->bits_set, tp),
3785 ) == ir_relation_equal)) {
3786 ir_graph *irg = get_irn_irg(proj);
3787 return get_irg_bad(irg);
3793 tarval_not(b_vrp->bits_not_set)),
3794 tarval_not(b_vrp->bits_not_set))
3795 == ir_relation_equal)) {
3796 ir_graph *irg = get_irn_irg(proj);
3797 return get_irg_bad(irg);
3808 * return true if the operation returns a value with exactly 1 bit set
3810 static bool is_single_bit(const ir_node *node)
3812 /* a first implementation, could be extended with vrp and others... */
3814 ir_node *shl_l = get_Shl_left(node);
3815 ir_mode *mode = get_irn_mode(node);
3816 int modulo = get_mode_modulo_shift(mode);
3817 /* this works if we shift a 1 and we have modulo shift */
3818 if (is_Const(shl_l) && is_Const_one(shl_l)
3819 && 0 < modulo && modulo <= (int)get_mode_size_bits(mode)) {
3822 } else if (is_Const(node)) {
3823 ir_tarval *tv = get_Const_tarval(node);
3824 return tarval_is_single_bit(tv);
3830 * Normalizes and optimizes Cmp nodes.
3832 static ir_node *transform_node_Cmp(ir_node *n)
3834 ir_node *left = get_Cmp_left(n);
3835 ir_node *right = get_Cmp_right(n);
3836 ir_mode *mode = get_irn_mode(left);
3837 ir_tarval *tv = NULL;
3838 bool changed = false;
3839 bool changedc = false;
3840 ir_relation relation = get_Cmp_relation(n);
3841 ir_relation possible = ir_get_possible_cmp_relations(left, right);
3843 /* mask out impossible relations */
3844 ir_relation new_relation = relation & possible;
3845 if (new_relation != relation) {
3846 relation = new_relation;
3850 /* Remove unnecessary conversions */
3851 /* TODO handle conv+constant */
3852 if (is_Conv(left) && is_Conv(right)) {
3853 ir_node *op_left = get_Conv_op(left);
3854 ir_node *op_right = get_Conv_op(right);
3855 ir_mode *mode_left = get_irn_mode(op_left);
3856 ir_mode *mode_right = get_irn_mode(op_right);
3858 if (smaller_mode(mode_left, mode) && smaller_mode(mode_right, mode)
3859 && mode_left != mode_b && mode_right != mode_b) {
3860 ir_node *block = get_nodes_block(n);
3862 if (mode_left == mode_right) {
3866 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV_CONV);
3867 } else if (smaller_mode(mode_left, mode_right)) {
3868 left = new_r_Conv(block, op_left, mode_right);
3871 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV);
3872 } else if (smaller_mode(mode_right, mode_left)) {
3874 right = new_r_Conv(block, op_right, mode_left);
3876 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV);
3882 * Optimize -a CMP -b into b CMP a.
3883 * This works only for modes where unary Minus cannot Overflow.
3884 * Note that two-complement integers can Overflow so it will NOT work.
3886 if (!mode_overflow_on_unary_Minus(mode) &&
3887 is_Minus(left) && is_Minus(right)) {
3888 left = get_Minus_op(left);
3889 right = get_Minus_op(right);
3890 relation = get_inversed_relation(relation);
3892 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3895 /* remove operation on both sides if possible */
3896 if (relation == ir_relation_equal || relation == ir_relation_less_greater) {
3898 * The following operations are NOT safe for floating point operations, for instance
3899 * 1.0 + inf == 2.0 + inf, =/=> x == y
3901 if (mode_is_int(mode)) {
3902 unsigned lop = get_irn_opcode(left);
3904 if (lop == get_irn_opcode(right)) {
3905 ir_node *ll, *lr, *rl, *rr;
3907 /* same operation on both sides, try to remove */
3911 /* ~a CMP ~b => a CMP b, -a CMP -b ==> a CMP b */
3912 left = get_unop_op(left);
3913 right = get_unop_op(right);
3915 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3918 ll = get_Add_left(left);
3919 lr = get_Add_right(left);
3920 rl = get_Add_left(right);
3921 rr = get_Add_right(right);
3924 /* X + a CMP X + b ==> a CMP b */
3928 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3929 } else if (ll == rr) {
3930 /* X + a CMP b + X ==> a CMP b */
3934 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3935 } else if (lr == rl) {
3936 /* a + X CMP X + b ==> a CMP b */
3940 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3941 } else if (lr == rr) {
3942 /* a + X CMP b + X ==> a CMP b */
3946 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3950 ll = get_Sub_left(left);
3951 lr = get_Sub_right(left);
3952 rl = get_Sub_left(right);
3953 rr = get_Sub_right(right);
3956 /* X - a CMP X - b ==> a CMP b */
3960 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3961 } else if (lr == rr) {
3962 /* a - X CMP b - X ==> a CMP b */
3966 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3970 if (get_Rotl_right(left) == get_Rotl_right(right)) {
3971 /* a ROTL X CMP b ROTL X ==> a CMP b */
3972 left = get_Rotl_left(left);
3973 right = get_Rotl_left(right);
3975 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3983 /* X+A == A, A+X == A, A-X == A -> X == 0 */
3984 if (is_Add(left) || is_Sub(left)) {
3985 ir_node *ll = get_binop_left(left);
3986 ir_node *lr = get_binop_right(left);
3988 if (lr == right && is_Add(left)) {
3994 ir_graph *irg = get_irn_irg(n);
3996 right = create_zero_const(irg, mode);
3998 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4001 if (is_Add(right) || is_Sub(right)) {
4002 ir_node *rl = get_binop_left(right);
4003 ir_node *rr = get_binop_right(right);
4005 if (rr == left && is_Add(right)) {
4011 ir_graph *irg = get_irn_irg(n);
4013 right = create_zero_const(irg, mode);
4015 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
4019 if (is_And(left) && is_Const(right)) {
4020 ir_node *ll = get_binop_left(left);
4021 ir_node *lr = get_binop_right(left);
4022 if (is_Shr(ll) && is_Const(lr)) {
4023 /* Cmp((x >>u c1) & c2, c3) = Cmp(x & (c2 << c1), c3 << c1) */
4024 ir_node *block = get_nodes_block(n);
4025 ir_mode *mode = get_irn_mode(left);
4027 ir_node *llr = get_Shr_right(ll);
4028 if (is_Const(llr)) {
4029 dbg_info *dbg = get_irn_dbg_info(left);
4030 ir_graph *irg = get_irn_irg(left);
4032 ir_tarval *c1 = get_Const_tarval(llr);
4033 ir_tarval *c2 = get_Const_tarval(lr);
4034 ir_tarval *c3 = get_Const_tarval(right);
4035 ir_tarval *mask = tarval_shl(c2, c1);
4036 ir_tarval *value = tarval_shl(c3, c1);
4038 left = new_rd_And(dbg, block, get_Shr_left(ll), new_r_Const(irg, mask), mode);
4039 right = new_r_Const(irg, value);
4044 /* Cmp(Eor(x, y), 0) <=> Cmp(x, y) at least for the ==0,!=0
4046 if (is_Const(right) && is_Const_null(right) && is_Eor(left)) {
4047 right = get_Eor_right(left);
4048 left = get_Eor_left(left);
4051 } /* mode_is_int(...) */
4054 /* Cmp(And(1bit, val), 1bit) "bit-testing" can be replaced
4055 * by the simpler Cmp(And(1bit), val), 0) negated pnc */
4056 if (mode_is_int(mode) && is_And(left)
4057 && (relation == ir_relation_equal
4058 || (mode_is_signed(mode) && relation == ir_relation_less_greater)
4059 || (!mode_is_signed(mode) && (relation & ir_relation_less_equal) == ir_relation_less))) {
4060 ir_node *and0 = get_And_left(left);
4061 ir_node *and1 = get_And_right(left);
4062 if (and1 == right) {
4063 ir_node *tmp = and0;
4067 if (and0 == right && is_single_bit(and0)) {
4068 ir_graph *irg = get_irn_irg(n);
4070 relation == ir_relation_equal ? ir_relation_less_greater : ir_relation_equal;
4071 right = create_zero_const(irg, mode);
4076 /* replace mode_b compares with ands/ors */
4077 if (mode == mode_b) {
4078 ir_node *block = get_nodes_block(n);
4082 case ir_relation_less_equal:
4083 bres = new_r_Or(block, new_r_Not(block, left, mode_b), right, mode_b);
4085 case ir_relation_less:
4086 bres = new_r_And(block, new_r_Not(block, left, mode_b), right, mode_b);
4088 case ir_relation_greater_equal:
4089 bres = new_r_Or(block, left, new_r_Not(block, right, mode_b), mode_b);
4091 case ir_relation_greater:
4092 bres = new_r_And(block, left, new_r_Not(block, right, mode_b), mode_b);
4094 case ir_relation_less_greater:
4095 bres = new_r_Eor(block, left, right, mode_b);
4097 case ir_relation_equal:
4098 bres = new_r_Not(block, new_r_Eor(block, left, right, mode_b), mode_b);
4101 #ifdef DEBUG_libfirm
4102 ir_fprintf(stderr, "Optimisation warning, unexpected mode_b Cmp %+F\n", n);
4107 DBG_OPT_ALGSIM0(n, bres, FS_OPT_CMP_TO_BOOL);
4113 * First step: normalize the compare op
4114 * by placing the constant on the right side
4115 * or moving the lower address node to the left.
4117 if (!operands_are_normalized(left, right)) {
4122 relation = get_inversed_relation(relation);
4127 * Second step: Try to reduce the magnitude
4128 * of a constant. This may help to generate better code
4129 * later and may help to normalize more compares.
4130 * Of course this is only possible for integer values.
4132 tv = value_of(right);
4133 if (tv != tarval_bad) {
4134 ir_mode *mode = get_irn_mode(right);
4136 /* TODO extend to arbitrary constants */
4137 if (is_Conv(left) && tarval_is_null(tv)) {
4138 ir_node *op = get_Conv_op(left);
4139 ir_mode *op_mode = get_irn_mode(op);
4142 * UpConv(x) REL 0 ==> x REL 0
4143 * Don't do this for float values as it's unclear whether it is a
4144 * win. (on the other side it makes detection/creation of fabs hard)
4146 if (get_mode_size_bits(mode) > get_mode_size_bits(op_mode) &&
4147 ((relation == ir_relation_equal || relation == ir_relation_less_greater) ||
4148 mode_is_signed(mode) || !mode_is_signed(op_mode)) &&
4149 !mode_is_float(mode)) {
4150 tv = get_mode_null(op_mode);
4154 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV);
4158 if (tv != tarval_bad) {
4159 /* the following optimization is possible on modes without Overflow
4160 * on Unary Minus or on == and !=:
4161 * -a CMP c ==> a swap(CMP) -c
4163 * Beware: for two-complement Overflow may occur, so only == and != can
4164 * be optimized, see this:
4165 * -MININT < 0 =/=> MININT > 0 !!!
4167 if (is_Minus(left) &&
4168 (!mode_overflow_on_unary_Minus(mode) ||
4169 (mode_is_int(mode) && (relation == ir_relation_equal || relation == ir_relation_less_greater)))) {
4170 tv = tarval_neg(tv);
4172 if (tv != tarval_bad) {
4173 left = get_Minus_op(left);
4174 relation = get_inversed_relation(relation);
4176 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4178 } else if (is_Not(left) && (relation == ir_relation_equal || relation == ir_relation_less_greater)) {
4179 /* Not(a) ==/!= c ==> a ==/!= Not(c) */
4180 tv = tarval_not(tv);
4182 if (tv != tarval_bad) {
4183 left = get_Not_op(left);
4185 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4189 /* for integer modes, we have more */
4190 if (mode_is_int(mode)) {
4191 /* c > 0 : a < c ==> a <= (c-1) a >= c ==> a > (c-1) */
4192 if ((relation == ir_relation_less || relation == ir_relation_greater_equal) &&
4193 tarval_cmp(tv, get_mode_null(mode)) == ir_relation_greater) {
4194 tv = tarval_sub(tv, get_mode_one(mode), NULL);
4196 if (tv != tarval_bad) {
4197 relation ^= ir_relation_equal;
4199 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CNST_MAGN);
4202 /* c < 0 : a > c ==> a >= (c+1) a <= c ==> a < (c+1) */
4203 else if ((relation == ir_relation_greater || relation == ir_relation_less_equal) &&
4204 tarval_cmp(tv, get_mode_null(mode)) == ir_relation_less) {
4205 tv = tarval_add(tv, get_mode_one(mode));
4207 if (tv != tarval_bad) {
4208 relation ^= ir_relation_equal;
4210 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CNST_MAGN);
4214 /* the following reassociations work only for == and != */
4215 if (relation == ir_relation_equal || relation == ir_relation_less_greater) {
4217 #if 0 /* Might be not that good in general */
4218 /* a-b == 0 ==> a == b, a-b != 0 ==> a != b */
4219 if (tarval_is_null(tv) && is_Sub(left)) {
4220 right = get_Sub_right(left);
4221 left = get_Sub_left(left);
4223 tv = value_of(right);
4225 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4229 if (tv != tarval_bad) {
4230 /* a-c1 == c2 ==> a == c2+c1, a-c1 != c2 ==> a != c2+c1 */
4232 ir_node *c1 = get_Sub_right(left);
4233 ir_tarval *tv2 = value_of(c1);
4235 if (tv2 != tarval_bad) {
4236 tv2 = tarval_add(tv, value_of(c1));
4238 if (tv2 != tarval_bad) {
4239 left = get_Sub_left(left);
4242 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4246 /* a+c1 == c2 ==> a == c2-c1, a+c1 != c2 ==> a != c2-c1 */
4247 else if (is_Add(left)) {
4248 ir_node *a_l = get_Add_left(left);
4249 ir_node *a_r = get_Add_right(left);
4253 if (is_Const(a_l)) {
4255 tv2 = value_of(a_l);
4258 tv2 = value_of(a_r);
4261 if (tv2 != tarval_bad) {
4262 tv2 = tarval_sub(tv, tv2, NULL);
4264 if (tv2 != tarval_bad) {
4268 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4272 /* -a == c ==> a == -c, -a != c ==> a != -c */
4273 else if (is_Minus(left)) {
4274 ir_tarval *tv2 = tarval_sub(get_mode_null(mode), tv, NULL);
4276 if (tv2 != tarval_bad) {
4277 left = get_Minus_op(left);
4280 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4287 if (relation == ir_relation_equal || relation == ir_relation_less_greater) {
4288 switch (get_irn_opcode(left)) {
4292 c1 = get_And_right(left);
4295 * And(x, C1) == C2 ==> FALSE if C2 & C1 != C2
4296 * And(x, C1) != C2 ==> TRUE if C2 & C1 != C2
4298 ir_tarval *mask = tarval_and(get_Const_tarval(c1), tv);
4300 /* TODO: move to constant evaluation */
4301 ir_graph *irg = get_irn_irg(n);
4302 tv = relation == ir_relation_equal ? get_tarval_b_false() : get_tarval_b_true();
4303 c1 = new_r_Const(irg, tv);
4304 DBG_OPT_CSTEVAL(n, c1);
4308 if (tarval_is_single_bit(tv)) {
4310 * optimization for AND:
4312 * And(x, C) == C ==> And(x, C) != 0
4313 * And(x, C) != C ==> And(X, C) == 0
4315 * if C is a single Bit constant.
4318 /* check for Constant's match. We have check hare the tarvals,
4319 because our const might be changed */
4320 if (get_Const_tarval(c1) == tv) {
4321 /* fine: do the transformation */
4322 tv = get_mode_null(get_tarval_mode(tv));
4323 relation ^= ir_relation_less_equal_greater;
4325 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CNST_MAGN);
4331 c1 = get_Or_right(left);
4332 if (is_Const(c1) && tarval_is_null(tv)) {
4334 * Or(x, C) == 0 && C != 0 ==> FALSE
4335 * Or(x, C) != 0 && C != 0 ==> TRUE
4337 if (! tarval_is_null(get_Const_tarval(c1))) {
4338 /* TODO: move to constant evaluation */
4339 ir_graph *irg = get_irn_irg(n);
4340 tv = relation == ir_relation_equal ? get_tarval_b_false() : get_tarval_b_true();
4341 c1 = new_r_Const(irg, tv);
4342 DBG_OPT_CSTEVAL(n, c1);
4349 * optimize x << c1 == c into x & (-1 >>u c1) == c >> c1 if c & (-1 << c1) == c
4351 * optimize x << c1 != c into x & (-1 >>u c1) != c >> c1 if c & (-1 << c1) == c
4354 c1 = get_Shl_right(left);
4356 ir_graph *irg = get_irn_irg(c1);
4357 ir_tarval *tv1 = get_Const_tarval(c1);
4358 ir_mode *mode = get_irn_mode(left);
4359 ir_tarval *minus1 = get_mode_all_one(mode);
4360 ir_tarval *amask = tarval_shr(minus1, tv1);
4361 ir_tarval *cmask = tarval_shl(minus1, tv1);
4364 if (tarval_and(tv, cmask) != tv) {
4365 /* condition not met */
4366 tv = relation == ir_relation_equal ? get_tarval_b_false() : get_tarval_b_true();
4367 c1 = new_r_Const(irg, tv);
4368 DBG_OPT_CSTEVAL(n, c1);
4371 sl = get_Shl_left(left);
4372 blk = get_nodes_block(n);
4373 left = new_rd_And(get_irn_dbg_info(left), blk, sl, new_r_Const(irg, amask), mode);
4374 tv = tarval_shr(tv, tv1);
4376 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_SHF_TO_AND);
4381 * optimize x >>u c1 == c into x & (-1 << c1) == c << c1 if c & (-1 >>u c1) == c
4383 * optimize x >>u c1 != c into x & (-1 << c1) != c << c1 if c & (-1 >>u c1) == c
4386 c1 = get_Shr_right(left);
4388 ir_graph *irg = get_irn_irg(c1);
4389 ir_tarval *tv1 = get_Const_tarval(c1);
4390 ir_mode *mode = get_irn_mode(left);
4391 ir_tarval *minus1 = get_mode_all_one(mode);
4392 ir_tarval *amask = tarval_shl(minus1, tv1);
4393 ir_tarval *cmask = tarval_shr(minus1, tv1);
4396 if (tarval_and(tv, cmask) != tv) {
4397 /* condition not met */
4398 tv = relation == ir_relation_equal ? get_tarval_b_false() : get_tarval_b_true();
4399 c1 = new_r_Const(irg, tv);
4400 DBG_OPT_CSTEVAL(n, c1);
4403 sl = get_Shr_left(left);
4404 blk = get_nodes_block(n);
4405 left = new_rd_And(get_irn_dbg_info(left), blk, sl, new_r_Const(irg, amask), mode);
4406 tv = tarval_shl(tv, tv1);
4408 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_SHF_TO_AND);
4413 * optimize x >>s c1 == c into x & (-1 << c1) == c << c1 if (c >>s (BITS - c1)) \in {0,-1}
4415 * optimize x >>s c1 != c into x & (-1 << c1) != c << c1 if (c >>s (BITS - c1)) \in {0,-1}
4418 c1 = get_Shrs_right(left);
4420 ir_graph *irg = get_irn_irg(c1);
4421 ir_tarval *tv1 = get_Const_tarval(c1);
4422 ir_mode *mode = get_irn_mode(left);
4423 ir_tarval *minus1 = get_mode_all_one(mode);
4424 ir_tarval *amask = tarval_shl(minus1, tv1);
4425 ir_tarval *cond = new_tarval_from_long(get_mode_size_bits(mode), get_tarval_mode(tv1));
4428 cond = tarval_sub(cond, tv1, NULL);
4429 cond = tarval_shrs(tv, cond);
4431 if (!tarval_is_all_one(cond) && !tarval_is_null(cond)) {
4432 /* condition not met */
4433 tv = relation == ir_relation_equal ? get_tarval_b_false() : get_tarval_b_true();
4434 c1 = new_r_Const(irg, tv);
4435 DBG_OPT_CSTEVAL(n, c1);
4438 sl = get_Shrs_left(left);
4439 blk = get_nodes_block(n);
4440 left = new_rd_And(get_irn_dbg_info(left), blk, sl, new_r_Const(irg, amask), mode);
4441 tv = tarval_shl(tv, tv1);
4443 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_SHF_TO_AND);
4448 } /* tarval != bad */
4451 if (changedc) { /* need a new Const */
4452 ir_graph *irg = get_irn_irg(n);
4453 right = new_r_Const(irg, tv);
4457 if ((relation == ir_relation_equal || relation == ir_relation_less_greater) && is_Const(right) && is_Const_null(right) && is_Proj(left)) {
4458 ir_node *op = get_Proj_pred(left);
4460 if (is_Mod(op) && get_Proj_proj(left) == pn_Mod_res) {
4461 ir_node *c = get_binop_right(op);
4464 ir_tarval *tv = get_Const_tarval(c);
4466 if (tarval_is_single_bit(tv)) {
4467 /* special case: (x % 2^n) CMP 0 ==> x & (2^n-1) CMP 0 */
4468 ir_node *v = get_binop_left(op);
4469 ir_node *blk = get_irn_n(op, -1);
4470 ir_graph *irg = get_irn_irg(op);
4471 ir_mode *mode = get_irn_mode(v);
4473 tv = tarval_sub(tv, get_mode_one(mode), NULL);
4474 left = new_rd_And(get_irn_dbg_info(op), blk, v, new_r_Const(irg, tv), mode);
4476 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_MOD_TO_AND);
4483 dbg_info *dbgi = get_irn_dbg_info(n);
4484 ir_node *block = get_nodes_block(n);
4486 /* create a new compare */
4487 n = new_rd_Cmp(dbgi, block, left, right, relation);
4494 * Optimize CopyB(mem, x, x) into a Nop.
4496 static ir_node *transform_node_Proj_CopyB(ir_node *proj)
4498 ir_node *copyb = get_Proj_pred(proj);
4499 ir_node *a = get_CopyB_dst(copyb);
4500 ir_node *b = get_CopyB_src(copyb);
4503 switch (get_Proj_proj(proj)) {
4504 case pn_CopyB_X_regular:
4505 /* Turn CopyB into a tuple (mem, jmp, bad, bad) */
4506 DBG_OPT_EXC_REM(proj);
4507 proj = new_r_Jmp(get_nodes_block(copyb));
4509 case pn_CopyB_X_except:
4510 DBG_OPT_EXC_REM(proj);
4511 proj = get_irg_bad(get_irn_irg(proj));
4518 } /* transform_node_Proj_CopyB */
4521 * Optimize Bounds(idx, idx, upper) into idx.
4523 static ir_node *transform_node_Proj_Bound(ir_node *proj)
4525 ir_node *oldn = proj;
4526 ir_node *bound = get_Proj_pred(proj);
4527 ir_node *idx = get_Bound_index(bound);
4528 ir_node *pred = skip_Proj(idx);
4531 if (idx == get_Bound_lower(bound))
4533 else if (is_Bound(pred)) {
4535 * idx was Bounds checked previously, it is still valid if
4536 * lower <= pred_lower && pred_upper <= upper.
4538 ir_node *lower = get_Bound_lower(bound);
4539 ir_node *upper = get_Bound_upper(bound);
4540 if (get_Bound_lower(pred) == lower &&
4541 get_Bound_upper(pred) == upper) {
4543 * One could expect that we simply return the previous
4544 * Bound here. However, this would be wrong, as we could
4545 * add an exception Proj to a new location then.
4546 * So, we must turn in into a tuple.
4552 /* Turn Bound into a tuple (mem, jmp, bad, idx) */
4553 switch (get_Proj_proj(proj)) {
4555 DBG_OPT_EXC_REM(proj);
4556 proj = get_Bound_mem(bound);
4558 case pn_Bound_X_except:
4559 DBG_OPT_EXC_REM(proj);
4560 proj = get_irg_bad(get_irn_irg(proj));
4564 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NOP);
4566 case pn_Bound_X_regular:
4567 DBG_OPT_EXC_REM(proj);
4568 proj = new_r_Jmp(get_nodes_block(bound));
4575 } /* transform_node_Proj_Bound */
4578 * Does all optimizations on nodes that must be done on its Projs
4579 * because of creating new nodes.
4581 static ir_node *transform_node_Proj(ir_node *proj)
4583 ir_node *n = get_Proj_pred(proj);
4585 if (n->op->ops.transform_node_Proj)
4586 return n->op->ops.transform_node_Proj(proj);
4588 } /* transform_node_Proj */
4591 * Move Confirms down through Phi nodes.
4593 static ir_node *transform_node_Phi(ir_node *phi)
4596 ir_mode *mode = get_irn_mode(phi);
4598 if (mode_is_reference(mode)) {
4599 n = get_irn_arity(phi);
4601 /* Beware of Phi0 */
4603 ir_node *pred = get_irn_n(phi, 0);
4604 ir_node *bound, *new_phi, *block, **in;
4605 ir_relation relation;
4607 if (! is_Confirm(pred))
4610 bound = get_Confirm_bound(pred);
4611 relation = get_Confirm_relation(pred);
4613 NEW_ARR_A(ir_node *, in, n);
4614 in[0] = get_Confirm_value(pred);
4616 for (i = 1; i < n; ++i) {
4617 pred = get_irn_n(phi, i);
4619 if (! is_Confirm(pred) ||
4620 get_Confirm_bound(pred) != bound ||
4621 get_Confirm_relation(pred) != relation)
4623 in[i] = get_Confirm_value(pred);
4625 /* move the Confirm nodes "behind" the Phi */
4626 block = get_irn_n(phi, -1);
4627 new_phi = new_r_Phi(block, n, in, get_irn_mode(phi));
4628 return new_r_Confirm(block, new_phi, bound, relation);
4635 * Returns the operands of a commutative bin-op, if one operand is
4636 * a const, it is returned as the second one.
4638 static void get_comm_Binop_Ops(ir_node *binop, ir_node **a, ir_node **c)
4640 ir_node *op_a = get_binop_left(binop);
4641 ir_node *op_b = get_binop_right(binop);
4643 assert(is_op_commutative(get_irn_op(binop)));
4645 if (is_Const(op_a)) {
4652 } /* get_comm_Binop_Ops */
4655 * Optimize a Or(And(Or(And(v,c4),c3),c2),c1) pattern if possible.
4656 * Such pattern may arise in bitfield stores.
4658 * value c4 value c4 & c2
4659 * AND c3 AND c1 | c3
4666 * AND c1 ===> OR if (c1 | c2) == 0x111..11
4669 static ir_node *transform_node_Or_bf_store(ir_node *irn_or)
4671 ir_node *irn_and, *c1;
4673 ir_node *and_l, *c3;
4674 ir_node *value, *c4;
4675 ir_node *new_and, *new_const, *block;
4676 ir_mode *mode = get_irn_mode(irn_or);
4678 ir_tarval *tv1, *tv2, *tv3, *tv4, *tv;
4682 get_comm_Binop_Ops(irn_or, &irn_and, &c1);
4683 if (!is_Const(c1) || !is_And(irn_and))
4686 get_comm_Binop_Ops(irn_and, &or_l, &c2);
4690 tv1 = get_Const_tarval(c1);
4691 tv2 = get_Const_tarval(c2);
4693 tv = tarval_or(tv1, tv2);
4694 if (tarval_is_all_one(tv)) {
4695 /* the AND does NOT clear a bit with isn't set by the OR */
4696 set_Or_left(irn_or, or_l);
4697 set_Or_right(irn_or, c1);
4699 /* check for more */
4706 get_comm_Binop_Ops(or_l, &and_l, &c3);
4707 if (!is_Const(c3) || !is_And(and_l))
4710 get_comm_Binop_Ops(and_l, &value, &c4);
4714 /* ok, found the pattern, check for conditions */
4715 assert(mode == get_irn_mode(irn_and));
4716 assert(mode == get_irn_mode(or_l));
4717 assert(mode == get_irn_mode(and_l));
4719 tv3 = get_Const_tarval(c3);
4720 tv4 = get_Const_tarval(c4);
4722 tv = tarval_or(tv4, tv2);
4723 if (!tarval_is_all_one(tv)) {
4724 /* have at least one 0 at the same bit position */
4728 if (tv3 != tarval_andnot(tv3, tv4)) {
4729 /* bit in the or_mask is outside the and_mask */
4733 if (tv1 != tarval_andnot(tv1, tv2)) {
4734 /* bit in the or_mask is outside the and_mask */
4738 /* ok, all conditions met */
4739 block = get_irn_n(irn_or, -1);
4740 irg = get_irn_irg(block);
4742 new_and = new_r_And(block, value, new_r_Const(irg, tarval_and(tv4, tv2)), mode);
4744 new_const = new_r_Const(irg, tarval_or(tv3, tv1));
4746 set_Or_left(irn_or, new_and);
4747 set_Or_right(irn_or, new_const);
4749 /* check for more */
4751 } /* transform_node_Or_bf_store */
4754 * Optimize an Or(shl(x, c), shr(x, bits - c)) into a Rotl
4756 static ir_node *transform_node_Or_Rotl(ir_node *irn_or)
4758 ir_mode *mode = get_irn_mode(irn_or);
4759 ir_node *shl, *shr, *block;
4760 ir_node *irn, *x, *c1, *c2, *n;
4761 ir_tarval *tv1, *tv2;
4763 /* some backends can't handle rotl */
4764 if (!be_get_backend_param()->support_rotl)
4767 if (! mode_is_int(mode))
4770 shl = get_binop_left(irn_or);
4771 shr = get_binop_right(irn_or);
4780 } else if (!is_Shl(shl)) {
4782 } else if (!is_Shr(shr)) {
4785 x = get_Shl_left(shl);
4786 if (x != get_Shr_left(shr))
4789 c1 = get_Shl_right(shl);
4790 c2 = get_Shr_right(shr);
4791 if (is_Const(c1) && is_Const(c2)) {
4792 tv1 = get_Const_tarval(c1);
4793 if (! tarval_is_long(tv1))
4796 tv2 = get_Const_tarval(c2);
4797 if (! tarval_is_long(tv2))
4800 if (get_tarval_long(tv1) + get_tarval_long(tv2)
4801 != (int) get_mode_size_bits(mode))
4804 /* yet, condition met */
4805 block = get_nodes_block(irn_or);
4807 n = new_r_Rotl(block, x, c1, mode);
4809 DBG_OPT_ALGSIM1(irn_or, shl, shr, n, FS_OPT_OR_SHFT_TO_ROTL);
4813 /* Note: the obvious rot formulation (a << x) | (a >> (32-x)) gets
4814 * transformed to (a << x) | (a >> -x) by transform_node_shift_modulo() */
4815 if (!ir_is_negated_value(c1, c2)) {
4819 /* yet, condition met */
4820 block = get_nodes_block(irn_or);
4821 n = new_r_Rotl(block, x, c1, mode);
4822 DBG_OPT_ALGSIM0(irn_or, n, FS_OPT_OR_SHFT_TO_ROTL);
4824 } /* transform_node_Or_Rotl */
4829 static ir_node *transform_node_Or(ir_node *n)
4831 ir_node *c, *oldn = n;
4832 ir_node *a = get_Or_left(n);
4833 ir_node *b = get_Or_right(n);
4836 if (is_Not(a) && is_Not(b)) {
4837 /* ~a | ~b = ~(a&b) */
4838 ir_node *block = get_nodes_block(n);
4840 mode = get_irn_mode(n);
4843 n = new_rd_And(get_irn_dbg_info(n), block, a, b, mode);
4844 n = new_rd_Not(get_irn_dbg_info(n), block, n, mode);
4845 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_DEMORGAN);
4849 /* we can combine the relations of two compares with the same operands */
4850 if (is_Cmp(a) && is_Cmp(b)) {
4851 ir_node *a_left = get_Cmp_left(a);
4852 ir_node *a_right = get_Cmp_left(a);
4853 ir_node *b_left = get_Cmp_left(b);
4854 ir_node *b_right = get_Cmp_right(b);
4855 if (a_left == b_left && b_left == b_right) {
4856 dbg_info *dbgi = get_irn_dbg_info(n);
4857 ir_node *block = get_nodes_block(n);
4858 ir_relation a_relation = get_Cmp_relation(a);
4859 ir_relation b_relation = get_Cmp_relation(b);
4860 ir_relation new_relation = a_relation | b_relation;
4861 return new_rd_Cmp(dbgi, block, a_left, a_right, new_relation);
4865 mode = get_irn_mode(n);
4866 HANDLE_BINOP_PHI((eval_func) tarval_or, a, b, c, mode);
4868 n = transform_node_Or_bf_store(n);
4869 n = transform_node_Or_Rotl(n);
4873 n = transform_bitwise_distributive(n, transform_node_Or);
4876 } /* transform_node_Or */
4880 static ir_node *transform_node(ir_node *n);
4883 * Optimize (a >> c1) >> c2), works for Shr, Shrs, Shl, Rotl.
4885 * Should be moved to reassociation?
4887 static ir_node *transform_node_shift(ir_node *n)
4889 ir_node *left, *right;
4891 ir_tarval *tv1, *tv2, *res;
4892 ir_node *in[2], *irn, *block;
4895 left = get_binop_left(n);
4897 /* different operations */
4898 if (get_irn_op(left) != get_irn_op(n))
4901 right = get_binop_right(n);
4902 tv1 = value_of(right);
4903 if (tv1 == tarval_bad)
4906 tv2 = value_of(get_binop_right(left));
4907 if (tv2 == tarval_bad)
4910 res = tarval_add(tv1, tv2);
4911 mode = get_irn_mode(n);
4912 irg = get_irn_irg(n);
4914 /* beware: a simple replacement works only, if res < modulo shift */
4916 int modulo_shf = get_mode_modulo_shift(mode);
4917 if (modulo_shf > 0) {
4918 ir_tarval *modulo = new_tarval_from_long(modulo_shf,
4919 get_tarval_mode(res));
4921 assert(modulo_shf >= (int) get_mode_size_bits(mode));
4923 /* shifting too much */
4924 if (!(tarval_cmp(res, modulo) & ir_relation_less)) {
4926 ir_node *block = get_nodes_block(n);
4927 dbg_info *dbgi = get_irn_dbg_info(n);
4928 ir_mode *smode = get_irn_mode(right);
4929 ir_node *cnst = new_r_Const_long(irg, smode, get_mode_size_bits(mode) - 1);
4930 return new_rd_Shrs(dbgi, block, get_binop_left(left), cnst, mode);
4933 return new_r_Const(irg, get_mode_null(mode));
4937 res = tarval_mod(res, new_tarval_from_long(get_mode_size_bits(mode), get_tarval_mode(res)));
4940 /* ok, we can replace it */
4941 block = get_nodes_block(n);
4943 in[0] = get_binop_left(left);
4944 in[1] = new_r_Const(irg, res);
4946 irn = new_ir_node(NULL, get_Block_irg(block), block, get_irn_op(n), mode, 2, in);
4948 DBG_OPT_ALGSIM0(n, irn, FS_OPT_REASSOC_SHIFT);
4950 return transform_node(irn);
4951 } /* transform_node_shift */
4954 * normalisation: (x & c1) >> c2 to (x >> c2) & (c1 >> c2)
4956 * - and, or, xor instead of &
4957 * - Shl, Shr, Shrs, rotl instead of >>
4958 * (with a special case for Or/Xor + Shrs)
4960 static ir_node *transform_node_bitop_shift(ir_node *n)
4963 ir_node *right = get_binop_right(n);
4964 ir_mode *mode = get_irn_mode(n);
4965 ir_node *bitop_left;
4966 ir_node *bitop_right;
4976 ir_tarval *tv_shift;
4978 assert(is_Shrs(n) || is_Shr(n) || is_Shl(n) || is_Rotl(n));
4980 if (!is_Const(right))
4983 left = get_binop_left(n);
4984 op_left = get_irn_op(left);
4985 if (op_left != op_And && op_left != op_Or && op_left != op_Eor)
4988 /* doing it with Shrs is not legal if the Or/Eor affects the topmost bit */
4989 if (is_Shrs(n) && (op_left == op_Or || op_left == op_Eor)) {
4990 /* TODO: test if sign bit is affectes */
4994 bitop_right = get_binop_right(left);
4995 if (!is_Const(bitop_right))
4998 bitop_left = get_binop_left(left);
5000 block = get_nodes_block(n);
5001 dbgi = get_irn_dbg_info(n);
5002 tv1 = get_Const_tarval(bitop_right);
5003 tv2 = get_Const_tarval(right);
5005 assert(get_tarval_mode(tv1) == mode);
5008 new_shift = new_rd_Shl(dbgi, block, bitop_left, right, mode);
5009 tv_shift = tarval_shl(tv1, tv2);
5010 } else if (is_Shr(n)) {
5011 new_shift = new_rd_Shr(dbgi, block, bitop_left, right, mode);
5012 tv_shift = tarval_shr(tv1, tv2);
5013 } else if (is_Shrs(n)) {
5014 new_shift = new_rd_Shrs(dbgi, block, bitop_left, right, mode);
5015 tv_shift = tarval_shrs(tv1, tv2);
5018 new_shift = new_rd_Rotl(dbgi, block, bitop_left, right, mode);
5019 tv_shift = tarval_rotl(tv1, tv2);
5022 assert(get_tarval_mode(tv_shift) == mode);
5023 irg = get_irn_irg(n);
5024 new_const = new_r_Const(irg, tv_shift);
5026 if (op_left == op_And) {
5027 new_bitop = new_rd_And(dbgi, block, new_shift, new_const, mode);
5028 } else if (op_left == op_Or) {
5029 new_bitop = new_rd_Or(dbgi, block, new_shift, new_const, mode);
5031 assert(op_left == op_Eor);
5032 new_bitop = new_rd_Eor(dbgi, block, new_shift, new_const, mode);
5040 * (x << c1) >> c2 <=> x OP (c2-c1) & ((-1 << c1) >> c2)
5042 * (x >> c1) << c2 <=> x OP (c2-c1) & ((-1 >> c1) << c2)
5043 * (also with x >>s c1 when c1>=c2)
5045 static ir_node *transform_node_shl_shr(ir_node *n)
5048 ir_node *right = get_binop_right(n);
5058 ir_tarval *tv_shift;
5061 ir_relation relation;
5064 assert(is_Shl(n) || is_Shr(n) || is_Shrs(n));
5066 if (!is_Const(right))
5069 left = get_binop_left(n);
5070 mode = get_irn_mode(n);
5071 if (is_Shl(n) && (is_Shr(left) || is_Shrs(left))) {
5072 ir_node *shr_right = get_binop_right(left);
5074 if (!is_Const(shr_right))
5077 x = get_binop_left(left);
5078 tv_shr = get_Const_tarval(shr_right);
5079 tv_shl = get_Const_tarval(right);
5081 if (is_Shrs(left)) {
5082 /* shrs variant only allowed if c1 >= c2 */
5083 if (! (tarval_cmp(tv_shl, tv_shr) & ir_relation_greater_equal))
5086 tv_mask = tarval_shrs(get_mode_all_one(mode), tv_shr);
5089 tv_mask = tarval_shr(get_mode_all_one(mode), tv_shr);
5091 tv_mask = tarval_shl(tv_mask, tv_shl);
5092 } else if (is_Shr(n) && is_Shl(left)) {
5093 ir_node *shl_right = get_Shl_right(left);
5095 if (!is_Const(shl_right))
5098 x = get_Shl_left(left);
5099 tv_shr = get_Const_tarval(right);
5100 tv_shl = get_Const_tarval(shl_right);
5102 tv_mask = tarval_shl(get_mode_all_one(mode), tv_shl);
5103 tv_mask = tarval_shr(tv_mask, tv_shr);
5108 if (get_tarval_mode(tv_shl) != get_tarval_mode(tv_shr)) {
5109 tv_shl = tarval_convert_to(tv_shl, get_tarval_mode(tv_shr));
5112 assert(tv_mask != tarval_bad);
5113 assert(get_tarval_mode(tv_mask) == mode);
5115 block = get_nodes_block(n);
5116 irg = get_irn_irg(block);
5117 dbgi = get_irn_dbg_info(n);
5119 relation = tarval_cmp(tv_shl, tv_shr);
5120 if (relation == ir_relation_less || relation == ir_relation_equal) {
5121 tv_shift = tarval_sub(tv_shr, tv_shl, NULL);
5122 new_const = new_r_Const(irg, tv_shift);
5124 new_shift = new_rd_Shrs(dbgi, block, x, new_const, mode);
5126 new_shift = new_rd_Shr(dbgi, block, x, new_const, mode);
5129 assert(relation == ir_relation_greater);
5130 tv_shift = tarval_sub(tv_shl, tv_shr, NULL);
5131 new_const = new_r_Const(irg, tv_shift);
5132 new_shift = new_rd_Shl(dbgi, block, x, new_const, mode);
5135 new_const = new_r_Const(irg, tv_mask);
5136 new_and = new_rd_And(dbgi, block, new_shift, new_const, mode);
5141 static ir_tarval *get_modulo_tv_value(ir_tarval *tv, int modulo_val)
5143 ir_mode *mode = get_tarval_mode(tv);
5144 ir_tarval *modulo_tv = new_tarval_from_long(modulo_val, mode);
5145 return tarval_mod(tv, modulo_tv);
5148 typedef ir_node*(*new_shift_func)(dbg_info *dbgi, ir_node *block,
5149 ir_node *left, ir_node *right, ir_mode *mode);
5152 * Normalisation: if we have a shl/shr with modulo_shift behaviour
5153 * then we can use that to minimize the value of Add(x, const) or
5154 * Sub(Const, x). In particular this often avoids 1 instruction in some
5155 * backends for the Shift(x, Sub(Const, y)) case because it can be replaced
5156 * by Shift(x, Minus(y)) which doesnt't need an explicit Const constructed.
5158 static ir_node *transform_node_shift_modulo(ir_node *n,
5159 new_shift_func new_shift)
5161 ir_mode *mode = get_irn_mode(n);
5162 int modulo = get_mode_modulo_shift(mode);
5163 ir_node *newop = NULL;
5164 ir_mode *mode_right;
5171 if (get_mode_arithmetic(mode) != irma_twos_complement)
5173 if (!is_po2(modulo))
5176 irg = get_irn_irg(n);
5177 block = get_nodes_block(n);
5178 right = get_binop_right(n);
5179 mode_right = get_irn_mode(right);
5180 if (is_Const(right)) {
5181 ir_tarval *tv = get_Const_tarval(right);
5182 ir_tarval *tv_mod = get_modulo_tv_value(tv, modulo);
5187 newop = new_r_Const(irg, tv_mod);
5188 } else if (is_Add(right)) {
5189 ir_node *add_right = get_Add_right(right);
5190 if (is_Const(add_right)) {
5191 ir_tarval *tv = get_Const_tarval(add_right);
5192 ir_tarval *tv_mod = get_modulo_tv_value(tv, modulo);
5197 newconst = new_r_Const(irg, tv_mod);
5198 newop = new_r_Add(block, get_Add_left(right), newconst,
5201 } else if (is_Sub(right)) {
5202 ir_node *sub_left = get_Sub_left(right);
5203 if (is_Const(sub_left)) {
5204 ir_tarval *tv = get_Const_tarval(sub_left);
5205 ir_tarval *tv_mod = get_modulo_tv_value(tv, modulo);
5210 newconst = new_r_Const(irg, tv_mod);
5211 newop = new_r_Sub(block, newconst, get_Sub_right(right),
5218 if (newop != NULL) {
5219 dbg_info *dbgi = get_irn_dbg_info(n);
5220 ir_node *left = get_binop_left(n);
5221 return new_shift(dbgi, block, left, newop, mode);
5229 static ir_node *transform_node_Shr(ir_node *n)
5231 ir_node *c, *oldn = n;
5232 ir_node *left = get_Shr_left(n);
5233 ir_node *right = get_Shr_right(n);
5234 ir_mode *mode = get_irn_mode(n);
5236 HANDLE_BINOP_PHI((eval_func) tarval_shr, left, right, c, mode);
5237 n = transform_node_shift(n);
5240 n = transform_node_shift_modulo(n, new_rd_Shr);
5242 n = transform_node_shl_shr(n);
5244 n = transform_node_bitop_shift(n);
5247 } /* transform_node_Shr */
5252 static ir_node *transform_node_Shrs(ir_node *n)
5254 ir_node *c, *oldn = n;
5255 ir_node *a = get_Shrs_left(n);
5256 ir_node *b = get_Shrs_right(n);
5257 ir_mode *mode = get_irn_mode(n);
5259 HANDLE_BINOP_PHI((eval_func) tarval_shrs, a, b, c, mode);
5260 n = transform_node_shift(n);
5263 n = transform_node_shift_modulo(n, new_rd_Shrs);
5265 n = transform_node_bitop_shift(n);
5268 } /* transform_node_Shrs */
5273 static ir_node *transform_node_Shl(ir_node *n)
5275 ir_node *c, *oldn = n;
5276 ir_node *a = get_Shl_left(n);
5277 ir_node *b = get_Shl_right(n);
5278 ir_mode *mode = get_irn_mode(n);
5280 HANDLE_BINOP_PHI((eval_func) tarval_shl, a, b, c, mode);
5281 n = transform_node_shift(n);
5284 n = transform_node_shift_modulo(n, new_rd_Shl);
5286 n = transform_node_shl_shr(n);
5288 n = transform_node_bitop_shift(n);
5291 } /* transform_node_Shl */
5296 static ir_node *transform_node_Rotl(ir_node *n)
5298 ir_node *c, *oldn = n;
5299 ir_node *a = get_Rotl_left(n);
5300 ir_node *b = get_Rotl_right(n);
5301 ir_mode *mode = get_irn_mode(n);
5303 HANDLE_BINOP_PHI((eval_func) tarval_rotl, a, b, c, mode);
5304 n = transform_node_shift(n);
5307 n = transform_node_bitop_shift(n);
5310 } /* transform_node_Rotl */
5315 static ir_node *transform_node_Conv(ir_node *n)
5317 ir_node *c, *oldn = n;
5318 ir_mode *mode = get_irn_mode(n);
5319 ir_node *a = get_Conv_op(n);
5321 if (mode != mode_b && is_const_Phi(a)) {
5322 /* Do NOT optimize mode_b Conv's, this leads to remaining
5323 * Phib nodes later, because the conv_b_lower operation
5324 * is instantly reverted, when it tries to insert a Convb.
5326 c = apply_conv_on_phi(a, mode);
5328 DBG_OPT_ALGSIM0(oldn, c, FS_OPT_CONST_PHI);
5333 if (is_Unknown(a)) { /* Conv_A(Unknown_B) -> Unknown_A */
5334 ir_graph *irg = get_irn_irg(n);
5335 return new_r_Unknown(irg, mode);
5338 if (mode_is_reference(mode) &&
5339 get_mode_size_bits(mode) == get_mode_size_bits(get_irn_mode(a)) &&
5341 ir_node *l = get_Add_left(a);
5342 ir_node *r = get_Add_right(a);
5343 dbg_info *dbgi = get_irn_dbg_info(a);
5344 ir_node *block = get_nodes_block(n);
5346 ir_node *lop = get_Conv_op(l);
5347 if (get_irn_mode(lop) == mode) {
5348 /* ConvP(AddI(ConvI(P), x)) -> AddP(P, x) */
5349 n = new_rd_Add(dbgi, block, lop, r, mode);
5354 ir_node *rop = get_Conv_op(r);
5355 if (get_irn_mode(rop) == mode) {
5356 /* ConvP(AddI(x, ConvI(P))) -> AddP(x, P) */
5357 n = new_rd_Add(dbgi, block, l, rop, mode);
5364 } /* transform_node_Conv */
5367 * Remove dead blocks and nodes in dead blocks
5368 * in keep alive list. We do not generate a new End node.
5370 static ir_node *transform_node_End(ir_node *n)
5372 int i, j, n_keepalives = get_End_n_keepalives(n);
5375 NEW_ARR_A(ir_node *, in, n_keepalives);
5377 for (i = j = 0; i < n_keepalives; ++i) {
5378 ir_node *ka = get_End_keepalive(n, i);
5380 if (! is_Block_dead(ka)) {
5384 } else if (is_irn_pinned_in_irg(ka) && is_Block_dead(get_nodes_block(ka))) {
5386 } else if (is_Bad(ka)) {
5387 /* no need to keep Bad */
5392 if (j != n_keepalives)
5393 set_End_keepalives(n, j, in);
5395 } /* transform_node_End */
5397 int ir_is_negated_value(const ir_node *a, const ir_node *b)
5399 if (is_Minus(a) && get_Minus_op(a) == b)
5401 if (is_Minus(b) && get_Minus_op(b) == a)
5403 if (is_Sub(a) && is_Sub(b)) {
5404 ir_node *a_left = get_Sub_left(a);
5405 ir_node *a_right = get_Sub_right(a);
5406 ir_node *b_left = get_Sub_left(b);
5407 ir_node *b_right = get_Sub_right(b);
5409 if (a_left == b_right && a_right == b_left)
5417 * Optimize a Mux into some simpler cases.
5419 static ir_node *transform_node_Mux(ir_node *n)
5421 ir_node *oldn = n, *sel = get_Mux_sel(n);
5422 ir_mode *mode = get_irn_mode(n);
5423 ir_node *t = get_Mux_true(n);
5424 ir_node *f = get_Mux_false(n);
5425 ir_graph *irg = get_irn_irg(n);
5427 if (is_irg_state(irg, IR_GRAPH_STATE_KEEP_MUX))
5431 ir_node* block = get_nodes_block(n);
5433 ir_node* c1 = get_Mux_sel(t);
5434 ir_node* t1 = get_Mux_true(t);
5435 ir_node* f1 = get_Mux_false(t);
5437 /* Mux(cond0, Mux(cond1, x, y), y) -> typical if (cond0 && cond1) x else y */
5438 ir_node* and_ = new_r_And(block, c0, c1, mode_b);
5439 ir_node* new_mux = new_r_Mux(block, and_, f1, t1, mode);
5444 DBG_OPT_ALGSIM0(oldn, t, FS_OPT_MUX_COMBINE);
5445 } else if (f == t1) {
5446 /* Mux(cond0, Mux(cond1, x, y), x) */
5447 ir_node* not_c1 = new_r_Not(block, c1, mode_b);
5448 ir_node* and_ = new_r_And(block, c0, not_c1, mode_b);
5449 ir_node* new_mux = new_r_Mux(block, and_, t1, f1, mode);
5454 DBG_OPT_ALGSIM0(oldn, t, FS_OPT_MUX_COMBINE);
5456 } else if (is_Mux(f)) {
5457 ir_node* block = get_nodes_block(n);
5459 ir_node* c1 = get_Mux_sel(f);
5460 ir_node* t1 = get_Mux_true(f);
5461 ir_node* f1 = get_Mux_false(f);
5463 /* Mux(cond0, x, Mux(cond1, x, y)) -> typical if (cond0 || cond1) x else y */
5464 ir_node* or_ = new_r_Or(block, c0, c1, mode_b);
5465 ir_node* new_mux = new_r_Mux(block, or_, f1, t1, mode);
5470 DBG_OPT_ALGSIM0(oldn, f, FS_OPT_MUX_COMBINE);
5471 } else if (t == f1) {
5472 /* Mux(cond0, x, Mux(cond1, y, x)) */
5473 ir_node* not_c1 = new_r_Not(block, c1, mode_b);
5474 ir_node* or_ = new_r_Or(block, c0, not_c1, mode_b);
5475 ir_node* new_mux = new_r_Mux(block, or_, t1, f1, mode);
5480 DBG_OPT_ALGSIM0(oldn, f, FS_OPT_MUX_COMBINE);
5484 /* first normalization step: try to move a constant to the false side,
5485 * 0 preferred on false side too */
5486 if (is_Cmp(sel) && is_Const(t) &&
5487 (!is_Const(f) || (is_Const_null(t) && !is_Const_null(f)))) {
5488 dbg_info *seldbgi = get_irn_dbg_info(sel);
5489 ir_node *block = get_nodes_block(sel);
5490 ir_relation relation = get_Cmp_relation(sel);
5495 /* Mux(x, a, b) => Mux(not(x), b, a) */
5496 relation = get_negated_relation(relation);
5497 sel = new_rd_Cmp(seldbgi, block, get_Cmp_left(sel),
5498 get_Cmp_right(sel), relation);
5499 n = new_rd_Mux(get_irn_dbg_info(n), get_nodes_block(n), sel, f, t, mode);
5502 /* note: after normalization, false can only happen on default */
5503 if (mode == mode_b) {
5504 dbg_info *dbg = get_irn_dbg_info(n);
5505 ir_node *block = get_nodes_block(n);
5508 ir_tarval *tv_t = get_Const_tarval(t);
5509 if (tv_t == tarval_b_true) {
5511 /* Muxb(sel, true, false) = sel */
5512 assert(get_Const_tarval(f) == tarval_b_false);
5513 DBG_OPT_ALGSIM0(oldn, sel, FS_OPT_MUX_BOOL);
5516 /* Muxb(sel, true, x) = Or(sel, x) */
5517 n = new_rd_Or(dbg, block, sel, f, mode_b);
5518 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_OR_BOOL);
5522 } else if (is_Const(f)) {
5523 ir_tarval *tv_f = get_Const_tarval(f);
5524 if (tv_f == tarval_b_true) {
5525 /* Muxb(sel, x, true) = Or(Not(sel), x) */
5526 ir_node* not_sel = new_rd_Not(dbg, block, sel, mode_b);
5527 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_ORNOT_BOOL);
5528 n = new_rd_Or(dbg, block, not_sel, t, mode_b);
5531 /* Muxb(sel, x, false) = And(sel, x) */
5532 assert(tv_f == tarval_b_false);
5533 n = new_rd_And(dbg, block, sel, t, mode_b);
5534 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_AND_BOOL);
5540 /* more normalization: Mux(sel, 0, 1) is simply a conv from the mode_b
5541 * value to integer. */
5542 if (is_Const(t) && is_Const(f) && mode_is_int(mode)) {
5543 ir_tarval *a = get_Const_tarval(t);
5544 ir_tarval *b = get_Const_tarval(f);
5546 if (tarval_is_one(a) && tarval_is_null(b)) {
5547 ir_node *block = get_nodes_block(n);
5548 ir_node *conv = new_r_Conv(block, sel, mode);
5550 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_CONV);
5552 } else if (tarval_is_null(a) && tarval_is_one(b)) {
5553 ir_node *block = get_nodes_block(n);
5554 ir_node *not_ = new_r_Not(block, sel, mode_b);
5555 ir_node *conv = new_r_Conv(block, not_, mode);
5557 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_CONV);
5563 ir_node *cmp_r = get_Cmp_right(sel);
5564 if (is_Const(cmp_r) && is_Const_null(cmp_r)) {
5565 ir_node *block = get_nodes_block(n);
5566 ir_node *cmp_l = get_Cmp_left(sel);
5568 if (mode_is_int(mode)) {
5569 ir_relation relation = get_Cmp_relation(sel);
5571 if ((relation == ir_relation_less_greater || relation == ir_relation_equal) && is_And(cmp_l)) {
5572 /* Mux((a & b) != 0, c, 0) */
5573 ir_node *and_r = get_And_right(cmp_l);
5576 if (and_r == t && f == cmp_r) {
5577 if (is_Const(t) && tarval_is_single_bit(get_Const_tarval(t))) {
5578 if (relation == ir_relation_less_greater) {
5579 /* Mux((a & 2^C) != 0, 2^C, 0) */
5581 DBG_OPT_ALGSIM1(oldn, sel, sel, n, FS_OPT_MUX_TO_BITOP);
5583 /* Mux((a & 2^C) == 0, 2^C, 0) */
5584 n = new_rd_Eor(get_irn_dbg_info(n),
5585 block, cmp_l, t, mode);
5586 DBG_OPT_ALGSIM1(oldn, sel, sel, n, FS_OPT_MUX_TO_BITOP);
5591 if (is_Shl(and_r)) {
5592 ir_node *shl_l = get_Shl_left(and_r);
5593 if (is_Const(shl_l) && is_Const_one(shl_l)) {
5594 if (and_r == t && f == cmp_r) {
5595 if (relation == ir_relation_less_greater) {
5596 /* (a & (1 << n)) != 0, (1 << n), 0) */
5598 DBG_OPT_ALGSIM1(oldn, sel, sel, n, FS_OPT_MUX_TO_BITOP);
5600 /* (a & (1 << n)) == 0, (1 << n), 0) */
5601 n = new_rd_Eor(get_irn_dbg_info(n),
5602 block, cmp_l, t, mode);
5603 DBG_OPT_ALGSIM1(oldn, sel, sel, n, FS_OPT_MUX_TO_BITOP);
5609 and_l = get_And_left(cmp_l);
5610 if (is_Shl(and_l)) {
5611 ir_node *shl_l = get_Shl_left(and_l);
5612 if (is_Const(shl_l) && is_Const_one(shl_l)) {
5613 if (and_l == t && f == cmp_r) {
5614 if (relation == ir_relation_less_greater) {
5615 /* ((1 << n) & a) != 0, (1 << n), 0) */
5617 DBG_OPT_ALGSIM1(oldn, sel, sel, n, FS_OPT_MUX_TO_BITOP);
5619 /* ((1 << n) & a) == 0, (1 << n), 0) */
5620 n = new_rd_Eor(get_irn_dbg_info(n),
5621 block, cmp_l, t, mode);
5622 DBG_OPT_ALGSIM1(oldn, sel, sel, n, FS_OPT_MUX_TO_BITOP);
5637 * optimize Sync nodes that have other syncs as input we simply add the inputs
5638 * of the other sync to our own inputs
5640 static ir_node *transform_node_Sync(ir_node *n)
5642 int arity = get_Sync_n_preds(n);
5645 for (i = 0; i < arity;) {
5646 ir_node *pred = get_Sync_pred(n, i);
5650 if (!is_Sync(pred)) {
5658 pred_arity = get_Sync_n_preds(pred);
5659 for (j = 0; j < pred_arity; ++j) {
5660 ir_node *pred_pred = get_Sync_pred(pred, j);
5665 add_irn_n(n, pred_pred);
5669 if (get_Sync_pred(n, k) == pred_pred) break;
5674 /* rehash the sync node */
5678 } /* transform_node_Sync */
5681 * optimize a trampoline Call into a direct Call
5683 static ir_node *transform_node_Call(ir_node *call)
5685 ir_node *callee = get_Call_ptr(call);
5686 ir_node *adr, *mem, *res, *bl, **in;
5687 ir_type *ctp, *mtp, *tp;
5691 size_t i, n_res, n_param;
5694 if (! is_Proj(callee))
5696 callee = get_Proj_pred(callee);
5697 if (! is_Builtin(callee))
5699 if (get_Builtin_kind(callee) != ir_bk_inner_trampoline)
5702 mem = get_Call_mem(call);
5704 if (skip_Proj(mem) == callee) {
5705 /* memory is routed to the trampoline, skip */
5706 mem = get_Builtin_mem(callee);
5709 /* build a new call type */
5710 mtp = get_Call_type(call);
5711 tdb = get_type_dbg_info(mtp);
5713 n_res = get_method_n_ress(mtp);
5714 n_param = get_method_n_params(mtp);
5715 ctp = new_d_type_method(n_param + 1, n_res, tdb);
5717 for (i = 0; i < n_res; ++i)
5718 set_method_res_type(ctp, i, get_method_res_type(mtp, i));
5720 NEW_ARR_A(ir_node *, in, n_param + 1);
5722 /* FIXME: we don't need a new pointer type in every step */
5723 irg = get_irn_irg(call);
5724 tp = get_irg_frame_type(irg);
5725 tp = new_type_pointer(tp);
5726 set_method_param_type(ctp, 0, tp);
5728 in[0] = get_Builtin_param(callee, 2);
5729 for (i = 0; i < n_param; ++i) {
5730 set_method_param_type(ctp, i + 1, get_method_param_type(mtp, i));
5731 in[i + 1] = get_Call_param(call, i);
5733 var = get_method_variadicity(mtp);
5734 set_method_variadicity(ctp, var);
5735 if (var == variadicity_variadic) {
5736 set_method_first_variadic_param_index(ctp, get_method_first_variadic_param_index(mtp) + 1);
5738 /* When we resolve a trampoline, the function must be called by a this-call */
5739 set_method_calling_convention(ctp, get_method_calling_convention(mtp) | cc_this_call);
5740 set_method_additional_properties(ctp, get_method_additional_properties(mtp));
5742 adr = get_Builtin_param(callee, 1);
5744 db = get_irn_dbg_info(call);
5745 bl = get_nodes_block(call);
5747 res = new_rd_Call(db, bl, mem, adr, n_param + 1, in, ctp);
5748 if (get_irn_pinned(call) == op_pin_state_floats)
5749 set_irn_pinned(res, op_pin_state_floats);
5751 } /* transform_node_Call */
5754 * Tries several [inplace] [optimizing] transformations and returns an
5755 * equivalent node. The difference to equivalent_node() is that these
5756 * transformations _do_ generate new nodes, and thus the old node must
5757 * not be freed even if the equivalent node isn't the old one.
5759 static ir_node *transform_node(ir_node *n)
5764 * Transform_node is the only "optimizing transformation" that might
5765 * return a node with a different opcode. We iterate HERE until fixpoint
5766 * to get the final result.
5770 if (n->op->ops.transform_node != NULL)
5771 n = n->op->ops.transform_node(n);
5772 } while (oldn != n);
5775 } /* transform_node */
5778 * Sets the default transform node operation for an ir_op_ops.
5780 * @param code the opcode for the default operation
5781 * @param ops the operations initialized
5786 static ir_op_ops *firm_set_default_transform_node(ir_opcode code, ir_op_ops *ops)
5790 ops->transform_node = transform_node_##a; \
5792 #define CASE_PROJ(a) \
5794 ops->transform_node_Proj = transform_node_Proj_##a; \
5796 #define CASE_PROJ_EX(a) \
5798 ops->transform_node = transform_node_##a; \
5799 ops->transform_node_Proj = transform_node_Proj_##a; \
5839 } /* firm_set_default_transform_node */
5842 /* **************** Common Subexpression Elimination **************** */
5844 /** The size of the hash table used, should estimate the number of nodes
5846 #define N_IR_NODES 512
5848 /** Compares the attributes of two Const nodes. */
5849 static int node_cmp_attr_Const(const ir_node *a, const ir_node *b)
5851 return get_Const_tarval(a) != get_Const_tarval(b);
5854 /** Compares the attributes of two Proj nodes. */
5855 static int node_cmp_attr_Proj(const ir_node *a, const ir_node *b)
5857 return a->attr.proj.proj != b->attr.proj.proj;
5860 /** Compares the attributes of two Alloc nodes. */
5861 static int node_cmp_attr_Alloc(const ir_node *a, const ir_node *b)
5863 const alloc_attr *pa = &a->attr.alloc;
5864 const alloc_attr *pb = &b->attr.alloc;
5865 return (pa->where != pb->where) || (pa->type != pb->type);
5868 /** Compares the attributes of two Free nodes. */
5869 static int node_cmp_attr_Free(const ir_node *a, const ir_node *b)
5871 const free_attr *pa = &a->attr.free;
5872 const free_attr *pb = &b->attr.free;
5873 return (pa->where != pb->where) || (pa->type != pb->type);
5876 /** Compares the attributes of two SymConst nodes. */
5877 static int node_cmp_attr_SymConst(const ir_node *a, const ir_node *b)
5879 const symconst_attr *pa = &a->attr.symc;
5880 const symconst_attr *pb = &b->attr.symc;
5881 return (pa->kind != pb->kind)
5882 || (pa->sym.type_p != pb->sym.type_p);
5885 /** Compares the attributes of two Call nodes. */
5886 static int node_cmp_attr_Call(const ir_node *a, const ir_node *b)
5888 const call_attr *pa = &a->attr.call;
5889 const call_attr *pb = &b->attr.call;
5890 return (pa->type != pb->type)
5891 || (pa->tail_call != pb->tail_call);
5894 /** Compares the attributes of two Sel nodes. */
5895 static int node_cmp_attr_Sel(const ir_node *a, const ir_node *b)
5897 const ir_entity *a_ent = get_Sel_entity(a);
5898 const ir_entity *b_ent = get_Sel_entity(b);
5899 return a_ent != b_ent;
5902 /** Compares the attributes of two Phi nodes. */
5903 static int node_cmp_attr_Phi(const ir_node *a, const ir_node *b)
5905 /* we can only enter this function if both nodes have the same number of inputs,
5906 hence it is enough to check if one of them is a Phi0 */
5908 /* check the Phi0 pos attribute */
5909 return a->attr.phi.u.pos != b->attr.phi.u.pos;
5914 /** Compares the attributes of two Conv nodes. */
5915 static int node_cmp_attr_Conv(const ir_node *a, const ir_node *b)
5917 return get_Conv_strict(a) != get_Conv_strict(b);
5920 /** Compares the attributes of two Cast nodes. */
5921 static int node_cmp_attr_Cast(const ir_node *a, const ir_node *b)
5923 return get_Cast_type(a) != get_Cast_type(b);
5926 /** Compares the attributes of two Load nodes. */
5927 static int node_cmp_attr_Load(const ir_node *a, const ir_node *b)
5929 if (get_Load_volatility(a) == volatility_is_volatile ||
5930 get_Load_volatility(b) == volatility_is_volatile)
5931 /* NEVER do CSE on volatile Loads */
5933 /* do not CSE Loads with different alignment. Be conservative. */
5934 if (get_Load_align(a) != get_Load_align(b))
5937 return get_Load_mode(a) != get_Load_mode(b);
5940 /** Compares the attributes of two Store nodes. */
5941 static int node_cmp_attr_Store(const ir_node *a, const ir_node *b)
5943 /* do not CSE Stores with different alignment. Be conservative. */
5944 if (get_Store_align(a) != get_Store_align(b))
5947 /* NEVER do CSE on volatile Stores */
5948 return (get_Store_volatility(a) == volatility_is_volatile ||
5949 get_Store_volatility(b) == volatility_is_volatile);
5952 /** Compares two exception attributes */
5953 static int node_cmp_exception(const ir_node *a, const ir_node *b)
5955 const except_attr *ea = &a->attr.except;
5956 const except_attr *eb = &b->attr.except;
5958 return ea->pin_state != eb->pin_state;
5961 #define node_cmp_attr_Bound node_cmp_exception
5963 /** Compares the attributes of two Div nodes. */
5964 static int node_cmp_attr_Div(const ir_node *a, const ir_node *b)
5966 const div_attr *ma = &a->attr.div;
5967 const div_attr *mb = &b->attr.div;
5968 return ma->exc.pin_state != mb->exc.pin_state ||
5969 ma->resmode != mb->resmode ||
5970 ma->no_remainder != mb->no_remainder;
5973 /** Compares the attributes of two Mod nodes. */
5974 static int node_cmp_attr_Mod(const ir_node *a, const ir_node *b)
5976 const mod_attr *ma = &a->attr.mod;
5977 const mod_attr *mb = &b->attr.mod;
5978 return ma->exc.pin_state != mb->exc.pin_state ||
5979 ma->resmode != mb->resmode;
5982 static int node_cmp_attr_Cmp(const ir_node *a, const ir_node *b)
5984 const cmp_attr *ma = &a->attr.cmp;
5985 const cmp_attr *mb = &b->attr.cmp;
5986 return ma->relation != mb->relation;
5989 /** Compares the attributes of two Confirm nodes. */
5990 static int node_cmp_attr_Confirm(const ir_node *a, const ir_node *b)
5992 const confirm_attr *ma = &a->attr.confirm;
5993 const confirm_attr *mb = &b->attr.confirm;
5994 return ma->relation != mb->relation;
5997 /** Compares the attributes of two Builtin nodes. */
5998 static int node_cmp_attr_Builtin(const ir_node *a, const ir_node *b)
6000 /* no need to compare the type, equal kind means equal type */
6001 return get_Builtin_kind(a) != get_Builtin_kind(b);
6004 /** Compares the attributes of two ASM nodes. */
6005 static int node_cmp_attr_ASM(const ir_node *a, const ir_node *b)
6008 const ir_asm_constraint *ca;
6009 const ir_asm_constraint *cb;
6012 if (get_ASM_text(a) != get_ASM_text(b))
6015 /* Should we really check the constraints here? Should be better, but is strange. */
6016 n = get_ASM_n_input_constraints(a);
6017 if (n != get_ASM_n_input_constraints(b))
6020 ca = get_ASM_input_constraints(a);
6021 cb = get_ASM_input_constraints(b);
6022 for (i = 0; i < n; ++i) {
6023 if (ca[i].pos != cb[i].pos || ca[i].constraint != cb[i].constraint)
6027 n = get_ASM_n_output_constraints(a);
6028 if (n != get_ASM_n_output_constraints(b))
6031 ca = get_ASM_output_constraints(a);
6032 cb = get_ASM_output_constraints(b);
6033 for (i = 0; i < n; ++i) {
6034 if (ca[i].pos != cb[i].pos || ca[i].constraint != cb[i].constraint)
6038 n = get_ASM_n_clobbers(a);
6039 if (n != get_ASM_n_clobbers(b))
6042 cla = get_ASM_clobbers(a);
6043 clb = get_ASM_clobbers(b);
6044 for (i = 0; i < n; ++i) {
6045 if (cla[i] != clb[i])
6051 /** Compares the inexistent attributes of two Dummy nodes. */
6052 static int node_cmp_attr_Dummy(const ir_node *a, const ir_node *b)
6060 * Set the default node attribute compare operation for an ir_op_ops.
6062 * @param code the opcode for the default operation
6063 * @param ops the operations initialized
6068 static ir_op_ops *firm_set_default_node_cmp_attr(ir_opcode code, ir_op_ops *ops)
6072 ops->node_cmp_attr = node_cmp_attr_##a; \
6104 } /* firm_set_default_node_cmp_attr */
6107 * Compare function for two nodes in the value table. Gets two
6108 * nodes as parameters. Returns 0 if the nodes are a Common Sub Expression.
6110 int identities_cmp(const void *elt, const void *key)
6112 ir_node *a = (ir_node *)elt;
6113 ir_node *b = (ir_node *)key;
6116 if (a == b) return 0;
6118 if ((get_irn_op(a) != get_irn_op(b)) ||
6119 (get_irn_mode(a) != get_irn_mode(b))) return 1;
6121 /* compare if a's in and b's in are of equal length */
6122 irn_arity_a = get_irn_arity(a);
6123 if (irn_arity_a != get_irn_arity(b))
6126 /* blocks are never the same */
6130 if (get_irn_pinned(a) == op_pin_state_pinned) {
6131 /* for pinned nodes, the block inputs must be equal */
6132 if (get_irn_n(a, -1) != get_irn_n(b, -1))
6134 } else if (! get_opt_global_cse()) {
6135 /* for block-local CSE both nodes must be in the same Block */
6136 if (get_nodes_block(a) != get_nodes_block(b))
6140 /* compare a->in[0..ins] with b->in[0..ins] */
6141 for (i = 0; i < irn_arity_a; ++i) {
6142 ir_node *pred_a = get_irn_n(a, i);
6143 ir_node *pred_b = get_irn_n(b, i);
6144 if (pred_a != pred_b) {
6145 /* if both predecessors are CSE neutral they might be different */
6146 if (!is_irn_cse_neutral(pred_a) || !is_irn_cse_neutral(pred_b))
6152 * here, we already now that the nodes are identical except their
6155 if (a->op->ops.node_cmp_attr)
6156 return a->op->ops.node_cmp_attr(a, b);
6159 } /* identities_cmp */
6162 * Calculate a hash value of a node.
6164 * @param node The IR-node
6166 unsigned ir_node_hash(const ir_node *node)
6168 return node->op->ops.hash(node);
6169 } /* ir_node_hash */
6172 void new_identities(ir_graph *irg)
6174 if (irg->value_table != NULL)
6175 del_pset(irg->value_table);
6176 irg->value_table = new_pset(identities_cmp, N_IR_NODES);
6177 } /* new_identities */
6179 void del_identities(ir_graph *irg)
6181 if (irg->value_table != NULL)
6182 del_pset(irg->value_table);
6183 } /* del_identities */
6185 /* Normalize a node by putting constants (and operands with larger
6186 * node index) on the right (operator side). */
6187 void ir_normalize_node(ir_node *n)
6189 if (is_op_commutative(get_irn_op(n))) {
6190 ir_node *l = get_binop_left(n);
6191 ir_node *r = get_binop_right(n);
6193 /* For commutative operators perform a OP b == b OP a but keep
6194 * constants on the RIGHT side. This helps greatly in some
6195 * optimizations. Moreover we use the idx number to make the form
6197 if (!operands_are_normalized(l, r)) {
6198 set_binop_left(n, r);
6199 set_binop_right(n, l);
6203 } /* ir_normalize_node */
6206 * Return the canonical node computing the same value as n.
6207 * Looks up the node in a hash table, enters it in the table
6208 * if it isn't there yet.
6210 * @param n the node to look up
6212 * @return a node that computes the same value as n or n if no such
6213 * node could be found
6215 ir_node *identify_remember(ir_node *n)
6217 ir_graph *irg = get_irn_irg(n);
6218 pset *value_table = irg->value_table;
6221 if (value_table == NULL)
6224 ir_normalize_node(n);
6225 /* lookup or insert in hash table with given hash key. */
6226 nn = (ir_node*)pset_insert(value_table, n, ir_node_hash(n));
6229 /* n is reachable again */
6230 edges_node_revival(nn, get_irn_irg(nn));
6234 } /* identify_remember */
6237 * During construction we set the op_pin_state_pinned flag in the graph right
6238 * when the optimization is performed. The flag turning on procedure global
6239 * cse could be changed between two allocations. This way we are safe.
6241 * @param n The node to lookup
6243 static inline ir_node *identify_cons(ir_node *n)
6247 n = identify_remember(n);
6248 if (n != old && get_nodes_block(old) != get_nodes_block(n)) {
6249 ir_graph *irg = get_irn_irg(n);
6250 set_irg_pinned(irg, op_pin_state_floats);
6253 } /* identify_cons */
6255 /* Add a node to the identities value table. */
6256 void add_identities(ir_node *node)
6263 identify_remember(node);
6266 /* Visit each node in the value table of a graph. */
6267 void visit_all_identities(ir_graph *irg, irg_walk_func visit, void *env)
6270 ir_graph *rem = current_ir_graph;
6272 current_ir_graph = irg;
6273 foreach_pset(irg->value_table, ir_node*, node) {
6276 current_ir_graph = rem;
6277 } /* visit_all_identities */
6280 * Garbage in, garbage out. If a node has a dead input, i.e., the
6281 * Bad node is input to the node, return the Bad node.
6283 static ir_node *gigo(ir_node *node)
6286 ir_op *op = get_irn_op(node);
6288 /* remove garbage blocks by looking at control flow that leaves the block
6289 and replacing the control flow by Bad. */
6290 if (get_irn_mode(node) == mode_X) {
6291 ir_node *block = get_nodes_block(skip_Proj(node));
6292 ir_graph *irg = get_irn_irg(block);
6294 /* Don't optimize nodes in immature blocks. */
6295 if (!get_Block_matured(block))
6297 /* Don't optimize End, may have Bads. */
6298 if (op == op_End) return node;
6300 if (is_Block(block)) {
6301 if (is_Block_dead(block)) {
6302 /* control flow from dead block is dead */
6303 return get_irg_bad(irg);
6306 for (i = get_irn_arity(block) - 1; i >= 0; --i) {
6307 if (!is_Bad(get_irn_n(block, i)))
6311 ir_graph *irg = get_irn_irg(block);
6312 /* the start block is never dead */
6313 if (block != get_irg_start_block(irg)
6314 && block != get_irg_end_block(irg)) {
6316 * Do NOT kill control flow without setting
6317 * the block to dead of bad things can happen:
6318 * We get a Block that is not reachable be irg_block_walk()
6319 * but can be found by irg_walk()!
6321 set_Block_dead(block);
6322 return get_irg_bad(irg);
6328 /* Blocks, Phis and Tuples may have dead inputs, e.g., if one of the
6329 blocks predecessors is dead. */
6330 if (op != op_Block && op != op_Phi && op != op_Tuple && op != op_Anchor) {
6331 ir_graph *irg = get_irn_irg(node);
6332 irn_arity = get_irn_arity(node);
6335 * Beware: we can only read the block of a non-floating node.
6337 if (is_irn_pinned_in_irg(node) &&
6338 is_Block_dead(get_nodes_block(skip_Proj(node))))
6339 return get_irg_bad(irg);
6341 for (i = 0; i < irn_arity; i++) {
6342 ir_node *pred = get_irn_n(node, i);
6345 return get_irg_bad(irg);
6347 /* Propagating Unknowns here seems to be a bad idea, because
6348 sometimes we need a node as a input and did not want that
6350 However, it might be useful to move this into a later phase
6351 (if you think that optimizing such code is useful). */
6352 if (is_Unknown(pred) && mode_is_data(get_irn_mode(node)))
6353 return new_r_Unknown(irg, get_irn_mode(node));
6358 /* With this code we violate the agreement that local_optimize
6359 only leaves Bads in Block, Phi and Tuple nodes. */
6360 /* If Block has only Bads as predecessors it's garbage. */
6361 /* If Phi has only Bads as predecessors it's garbage. */
6362 if ((op == op_Block && get_Block_matured(node)) || op == op_Phi) {
6363 irn_arity = get_irn_arity(node);
6364 for (i = 0; i < irn_arity; i++) {
6365 if (!is_Bad(get_irn_n(node, i))) break;
6367 if (i == irn_arity) node = get_irg_bad(irg);
6374 * These optimizations deallocate nodes from the obstack.
6375 * It can only be called if it is guaranteed that no other nodes
6376 * reference this one, i.e., right after construction of a node.
6378 * @param n The node to optimize
6380 ir_node *optimize_node(ir_node *n)
6383 ir_graph *irg = get_irn_irg(n);
6384 unsigned iro = get_irn_opcode(n);
6387 /* Always optimize Phi nodes: part of the construction. */
6388 if ((!get_opt_optimize()) && (iro != iro_Phi)) return n;
6390 /* Remove nodes with dead (Bad) input.
6391 Run always for transformation induced Bads. */
6394 edges_node_deleted(oldn, irg);
6396 /* We found an existing, better node, so we can deallocate the old node. */
6397 irg_kill_node(irg, oldn);
6401 /* constant expression evaluation / constant folding */
6402 if (get_opt_constant_folding()) {
6403 /* neither constants nor Tuple values can be evaluated */
6404 if (iro != iro_Const && (get_irn_mode(n) != mode_T)) {
6405 /* try to evaluate */
6406 tv = computed_value(n);
6407 if (tv != tarval_bad) {
6412 * we MUST copy the node here temporarily, because it's still
6413 * needed for DBG_OPT_CSTEVAL
6415 node_size = offsetof(ir_node, attr) + n->op->attr_size;
6416 oldn = (ir_node*)alloca(node_size);
6418 memcpy(oldn, n, node_size);
6419 CLONE_ARR_A(ir_node *, oldn->in, n->in);
6421 /* ARG, copy the in array, we need it for statistics */
6422 memcpy(oldn->in, n->in, ARR_LEN(n->in) * sizeof(n->in[0]));
6424 /* note the inplace edges module */
6425 edges_node_deleted(n, irg);
6427 /* evaluation was successful -- replace the node. */
6428 irg_kill_node(irg, n);
6429 nw = new_r_Const(irg, tv);
6431 DBG_OPT_CSTEVAL(oldn, nw);
6437 /* remove unnecessary nodes */
6438 if (get_opt_algebraic_simplification() ||
6439 (iro == iro_Phi) || /* always optimize these nodes. */
6441 (iro == iro_Proj) ||
6442 (iro == iro_Block) ) /* Flags tested local. */
6443 n = equivalent_node(n);
6445 /* Common Subexpression Elimination.
6447 * Checks whether n is already available.
6448 * The block input is used to distinguish different subexpressions. Right
6449 * now all nodes are op_pin_state_pinned to blocks, i.e., the CSE only finds common
6450 * subexpressions within a block.
6453 n = identify_cons(n);
6456 edges_node_deleted(oldn, irg);
6458 /* We found an existing, better node, so we can deallocate the old node. */
6459 irg_kill_node(irg, oldn);
6463 /* Some more constant expression evaluation that does not allow to
6465 iro = get_irn_opcode(n);
6466 if (get_opt_algebraic_simplification() ||
6467 (iro == iro_Cond) ||
6468 (iro == iro_Proj)) /* Flags tested local. */
6469 n = transform_node(n);
6471 /* Now we have a legal, useful node. Enter it in hash table for CSE */
6472 if (get_opt_cse() && (get_irn_opcode(n) != iro_Block)) {
6474 n = identify_remember(o);
6480 } /* optimize_node */
6484 * These optimizations never deallocate nodes (in place). This can cause dead
6485 * nodes lying on the obstack. Remove these by a dead node elimination,
6486 * i.e., a copying garbage collection.
6488 ir_node *optimize_in_place_2(ir_node *n)
6492 unsigned iro = get_irn_opcode(n);
6494 if (!get_opt_optimize() && !is_Phi(n)) return n;
6496 if (iro == iro_Deleted)
6499 /* Remove nodes with dead (Bad) input.
6500 Run always for transformation induced Bads. */
6505 /* constant expression evaluation / constant folding */
6506 if (get_opt_constant_folding()) {
6507 /* neither constants nor Tuple values can be evaluated */
6508 if (iro != iro_Const && get_irn_mode(n) != mode_T) {
6509 /* try to evaluate */
6510 tv = computed_value(n);
6511 if (tv != tarval_bad) {
6512 /* evaluation was successful -- replace the node. */
6513 ir_graph *irg = get_irn_irg(n);
6515 n = new_r_Const(irg, tv);
6517 DBG_OPT_CSTEVAL(oldn, n);
6523 /* remove unnecessary nodes */
6524 if (get_opt_constant_folding() ||
6525 (iro == iro_Phi) || /* always optimize these nodes. */
6526 (iro == iro_Id) || /* ... */
6527 (iro == iro_Proj) || /* ... */
6528 (iro == iro_Block) ) /* Flags tested local. */
6529 n = equivalent_node(n);
6531 /** common subexpression elimination **/
6532 /* Checks whether n is already available. */
6533 /* The block input is used to distinguish different subexpressions. Right
6534 now all nodes are op_pin_state_pinned to blocks, i.e., the cse only finds common
6535 subexpressions within a block. */
6536 if (get_opt_cse()) {
6538 n = identify_remember(o);
6543 /* Some more constant expression evaluation. */
6544 iro = get_irn_opcode(n);
6545 if (get_opt_constant_folding() ||
6546 (iro == iro_Cond) ||
6547 (iro == iro_Proj)) /* Flags tested local. */
6548 n = transform_node(n);
6550 /* Now we can verify the node, as it has no dead inputs any more. */
6553 /* Now we have a legal, useful node. Enter it in hash table for cse.
6554 Blocks should be unique anyways. (Except the successor of start:
6555 is cse with the start block!) */
6556 if (get_opt_cse() && (get_irn_opcode(n) != iro_Block)) {
6558 n = identify_remember(o);
6564 } /* optimize_in_place_2 */
6567 * Wrapper for external use, set proper status bits after optimization.
6569 ir_node *optimize_in_place(ir_node *n)
6571 ir_graph *irg = get_irn_irg(n);
6572 /* Handle graph state */
6573 assert(get_irg_phase_state(irg) != phase_building);
6575 if (get_opt_global_cse())
6576 set_irg_pinned(irg, op_pin_state_floats);
6577 if (get_irg_outs_state(irg) == outs_consistent)
6578 set_irg_outs_inconsistent(irg);
6580 /* FIXME: Maybe we could also test whether optimizing the node can
6581 change the control graph. */
6582 set_irg_doms_inconsistent(irg);
6583 return optimize_in_place_2(n);
6584 } /* optimize_in_place */
6587 * Calculate a hash value of a Const node.
6589 static unsigned hash_Const(const ir_node *node)
6593 /* special value for const, as they only differ in their tarval. */
6594 h = HASH_PTR(node->attr.con.tarval);
6600 * Calculate a hash value of a SymConst node.
6602 static unsigned hash_SymConst(const ir_node *node)
6606 /* all others are pointers */
6607 h = HASH_PTR(node->attr.symc.sym.type_p);
6610 } /* hash_SymConst */
6613 * Set the default hash operation in an ir_op_ops.
6615 * @param code the opcode for the default operation
6616 * @param ops the operations initialized
6621 static ir_op_ops *firm_set_default_hash(ir_opcode code, ir_op_ops *ops)
6625 ops->hash = hash_##a; \
6628 /* hash function already set */
6629 if (ops->hash != NULL)
6636 /* use input/mode default hash if no function was given */
6637 ops->hash = firm_default_hash;
6645 * Sets the default operation for an ir_ops.
6647 ir_op_ops *firm_set_default_operations(ir_opcode code, ir_op_ops *ops)
6649 ops = firm_set_default_hash(code, ops);
6650 ops = firm_set_default_computed_value(code, ops);
6651 ops = firm_set_default_equivalent_node(code, ops);
6652 ops = firm_set_default_transform_node(code, ops);
6653 ops = firm_set_default_node_cmp_attr(code, ops);
6654 ops = firm_set_default_get_type_attr(code, ops);
6655 ops = firm_set_default_get_entity_attr(code, ops);
6658 } /* firm_set_default_operations */