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 * Tests wether a shift shifts more bits than available in the mode
333 static bool is_oversize_shift(const ir_node *n)
335 ir_node *count = get_binop_right(n);
336 ir_mode *mode = get_irn_mode(n);
337 ir_tarval *tv = value_of(count);
340 if (tv == tarval_bad)
342 if (!tarval_is_long(tv))
344 shiftval = get_tarval_long(tv);
345 modulo_shift = get_mode_modulo_shift(mode);
346 if (shiftval < 0 || (modulo_shift > 0 && shiftval >= modulo_shift))
349 return shiftval >= (long)get_mode_size_bits(mode);
353 * Return the value of a Shl.
355 static ir_tarval *computed_value_Shl(const ir_node *n)
357 ir_node *a = get_Shl_left(n);
358 ir_node *b = get_Shl_right(n);
360 ir_tarval *ta = value_of(a);
361 ir_tarval *tb = value_of(b);
363 if ((ta != tarval_bad) && (tb != tarval_bad)) {
364 return tarval_shl(ta, tb);
367 if (is_oversize_shift(n))
368 return get_mode_null(get_irn_mode(n));
371 } /* computed_value_Shl */
374 * Return the value of a Shr.
376 static ir_tarval *computed_value_Shr(const ir_node *n)
378 ir_node *a = get_Shr_left(n);
379 ir_node *b = get_Shr_right(n);
381 ir_tarval *ta = value_of(a);
382 ir_tarval *tb = value_of(b);
384 if ((ta != tarval_bad) && (tb != tarval_bad)) {
385 return tarval_shr(ta, tb);
387 if (is_oversize_shift(n))
388 return get_mode_null(get_irn_mode(n));
391 } /* computed_value_Shr */
394 * Return the value of a Shrs.
396 static ir_tarval *computed_value_Shrs(const ir_node *n)
398 ir_node *a = get_Shrs_left(n);
399 ir_node *b = get_Shrs_right(n);
401 ir_tarval *ta = value_of(a);
402 ir_tarval *tb = value_of(b);
404 if ((ta != tarval_bad) && (tb != tarval_bad)) {
405 return tarval_shrs(ta, tb);
408 } /* computed_value_Shrs */
411 * Return the value of a Rotl.
413 static ir_tarval *computed_value_Rotl(const ir_node *n)
415 ir_node *a = get_Rotl_left(n);
416 ir_node *b = get_Rotl_right(n);
418 ir_tarval *ta = value_of(a);
419 ir_tarval *tb = value_of(b);
421 if ((ta != tarval_bad) && (tb != tarval_bad)) {
422 return tarval_rotl(ta, tb);
425 } /* computed_value_Rotl */
427 bool ir_zero_when_converted(const ir_node *node, ir_mode *dest_mode)
429 ir_mode *mode = get_irn_mode(node);
430 if (get_mode_arithmetic(mode) != irma_twos_complement
431 || get_mode_arithmetic(dest_mode) != irma_twos_complement)
435 ir_node *count = get_Shl_right(node);
436 if (is_Const(count)) {
437 ir_tarval *tv = get_Const_tarval(count);
438 if (tarval_is_long(tv)) {
439 long shiftval = get_tarval_long(tv);
440 long destbits = get_mode_size_bits(dest_mode);
441 if (shiftval >= destbits
442 && shiftval < (long)get_mode_modulo_shift(mode))
448 ir_node *right = get_And_right(node);
449 if (is_Const(right)) {
450 ir_tarval *tv = get_Const_tarval(right);
451 ir_tarval *conved = tarval_convert_to(tv, dest_mode);
452 return tarval_is_null(conved);
459 * Return the value of a Conv.
461 static ir_tarval *computed_value_Conv(const ir_node *n)
463 ir_node *a = get_Conv_op(n);
464 ir_tarval *ta = value_of(a);
465 ir_mode *mode = get_irn_mode(n);
467 if (ta != tarval_bad)
468 return tarval_convert_to(ta, get_irn_mode(n));
470 if (ir_zero_when_converted(a, mode))
471 return get_mode_null(mode);
474 } /* computed_value_Conv */
477 * Calculate the value of a Mux: can be evaluated, if the
478 * sel and the right input are known.
480 static ir_tarval *computed_value_Mux(const ir_node *n)
482 ir_node *sel = get_Mux_sel(n);
483 ir_tarval *ts = value_of(sel);
485 if (ts == get_tarval_b_true()) {
486 ir_node *v = get_Mux_true(n);
489 else if (ts == get_tarval_b_false()) {
490 ir_node *v = get_Mux_false(n);
494 } /* computed_value_Mux */
497 * Calculate the value of a Confirm: can be evaluated,
498 * if it has the form Confirm(x, '=', Const).
500 static ir_tarval *computed_value_Confirm(const ir_node *n)
502 if (get_Confirm_relation(n) == ir_relation_equal) {
503 ir_tarval *tv = value_of(get_Confirm_bound(n));
504 if (tv != tarval_bad)
507 return value_of(get_Confirm_value(n));
508 } /* computed_value_Confirm */
511 * gives a (conservative) estimation of possible relation when comparing
514 ir_relation ir_get_possible_cmp_relations(const ir_node *left,
515 const ir_node *right)
517 ir_relation possible = ir_relation_true;
518 ir_tarval *tv_l = value_of(left);
519 ir_tarval *tv_r = value_of(right);
520 ir_mode *mode = get_irn_mode(left);
521 ir_tarval *min = mode == mode_b ? tarval_b_false : get_mode_min(mode);
522 ir_tarval *max = mode == mode_b ? tarval_b_true : get_mode_max(mode);
524 /* both values known - evaluate them */
525 if ((tv_l != tarval_bad) && (tv_r != tarval_bad)) {
526 possible = tarval_cmp(tv_l, tv_r);
527 /* we can return now, won't get any better */
530 /* a == a is never less or greater (but might be equal or unordered) */
532 possible &= ~ir_relation_less_greater;
533 /* unordered results only happen for float compares */
534 if (!mode_is_float(mode))
535 possible &= ~ir_relation_unordered;
536 /* values can never be less than the least representable number or
537 * greater than the greatest representable number */
539 possible &= ~ir_relation_greater;
541 possible &= ~ir_relation_less;
543 possible &= ~ir_relation_greater;
545 possible &= ~ir_relation_less;
546 /* maybe vrp can tell us more */
547 possible &= vrp_cmp(left, right);
548 /* Alloc nodes never return null (but throw an exception) */
549 if (is_Alloc(left) && tarval_is_null(tv_r))
550 possible &= ~ir_relation_equal;
556 * Return the value of a Cmp.
558 * The basic idea here is to determine which relations are possible and which
559 * one are definitely impossible.
561 static ir_tarval *computed_value_Cmp(const ir_node *cmp)
563 ir_node *left = get_Cmp_left(cmp);
564 ir_node *right = get_Cmp_right(cmp);
565 ir_relation possible = ir_get_possible_cmp_relations(left, right);
566 ir_relation relation = get_Cmp_relation(cmp);
568 /* if none of the requested relations is possible, return false */
569 if ((possible & relation) == ir_relation_false)
570 return tarval_b_false;
571 /* if possible relations are a subset of the requested ones return true */
572 if ((possible & ~relation) == ir_relation_false)
573 return tarval_b_true;
575 return computed_value_Cmp_Confirm(cmp, left, right, relation);
579 * Calculate the value of an integer Div.
580 * Special case: 0 / b
582 static ir_tarval *do_computed_value_Div(const ir_node *div)
584 const ir_node *a = get_Div_left(div);
585 const ir_node *b = get_Div_right(div);
586 const ir_mode *mode = get_Div_resmode(div);
587 ir_tarval *ta = value_of(a);
589 const ir_node *dummy;
591 /* cannot optimize 0 / b = 0 because of NaN */
592 if (!mode_is_float(mode)) {
593 if (tarval_is_null(ta) && value_not_zero(b, &dummy))
594 return ta; /* 0 / b == 0 if b != 0 */
597 if (ta != tarval_bad && tb != tarval_bad)
598 return tarval_div(ta, tb);
600 } /* do_computed_value_Div */
603 * Calculate the value of an integer Mod of two nodes.
604 * Special case: a % 1
606 static ir_tarval *do_computed_value_Mod(const ir_node *a, const ir_node *b)
608 ir_tarval *ta = value_of(a);
609 ir_tarval *tb = value_of(b);
611 /* Compute a % 1 or c1 % c2 */
612 if (tarval_is_one(tb))
613 return get_mode_null(get_irn_mode(a));
614 if (ta != tarval_bad && tb != tarval_bad)
615 return tarval_mod(ta, tb);
617 } /* do_computed_value_Mod */
620 * Return the value of a Proj(Div).
622 static ir_tarval *computed_value_Proj_Div(const ir_node *n)
624 long proj_nr = get_Proj_proj(n);
625 if (proj_nr != pn_Div_res)
628 return do_computed_value_Div(get_Proj_pred(n));
629 } /* computed_value_Proj_Div */
632 * Return the value of a Proj(Mod).
634 static ir_tarval *computed_value_Proj_Mod(const ir_node *n)
636 long proj_nr = get_Proj_proj(n);
638 if (proj_nr == pn_Mod_res) {
639 const ir_node *mod = get_Proj_pred(n);
640 return do_computed_value_Mod(get_Mod_left(mod), get_Mod_right(mod));
643 } /* computed_value_Proj_Mod */
646 * Return the value of a Proj.
648 static ir_tarval *computed_value_Proj(const ir_node *proj)
650 ir_node *n = get_Proj_pred(proj);
652 if (n->op->ops.computed_value_Proj != NULL)
653 return n->op->ops.computed_value_Proj(proj);
655 } /* computed_value_Proj */
658 * If the parameter n can be computed, return its value, else tarval_bad.
659 * Performs constant folding.
661 * @param n The node this should be evaluated
663 ir_tarval *computed_value(const ir_node *n)
665 vrp_attr *vrp = vrp_get_info(n);
666 if (vrp && vrp->valid && tarval_cmp(vrp->bits_set, vrp->bits_not_set) == ir_relation_equal) {
667 return vrp->bits_set;
669 if (n->op->ops.computed_value)
670 return n->op->ops.computed_value(n);
672 } /* computed_value */
675 * Set the default computed_value evaluator in an ir_op_ops.
677 * @param code the opcode for the default operation
678 * @param ops the operations initialized
683 static ir_op_ops *firm_set_default_computed_value(ir_opcode code, ir_op_ops *ops)
687 ops->computed_value = computed_value_##a; \
689 #define CASE_PROJ(a) \
691 ops->computed_value_Proj = computed_value_Proj_##a; \
726 } /* firm_set_default_computed_value */
729 * Optimize operations that are commutative and have neutral 0,
730 * so a op 0 = 0 op a = a.
732 static ir_node *equivalent_node_neutral_zero(ir_node *n)
736 ir_node *a = get_binop_left(n);
737 ir_node *b = get_binop_right(n);
742 /* After running compute_node there is only one constant predecessor.
743 Find this predecessors value and remember the other node: */
744 if ((tv = value_of(a)) != tarval_bad) {
746 } else if ((tv = value_of(b)) != tarval_bad) {
751 /* If this predecessors constant value is zero, the operation is
752 * unnecessary. Remove it.
754 * Beware: If n is a Add, the mode of on and n might be different
755 * which happens in this rare construction: NULL + 3.
756 * Then, a Conv would be needed which we cannot include here.
758 if (tarval_is_null(tv) && get_irn_mode(on) == get_irn_mode(n)) {
761 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_0);
765 } /* equivalent_node_neutral_zero */
768 * Eor is commutative and has neutral 0.
770 static ir_node *equivalent_node_Eor(ir_node *n)
776 n = equivalent_node_neutral_zero(n);
777 if (n != oldn) return n;
780 b = get_Eor_right(n);
783 ir_node *aa = get_Eor_left(a);
784 ir_node *ab = get_Eor_right(a);
787 /* (a ^ b) ^ a -> b */
789 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_EOR_A_B_A);
791 } else if (ab == b) {
792 /* (a ^ b) ^ b -> a */
794 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_EOR_A_B_A);
799 ir_node *ba = get_Eor_left(b);
800 ir_node *bb = get_Eor_right(b);
803 /* a ^ (a ^ b) -> b */
805 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_EOR_A_B_A);
807 } else if (bb == a) {
808 /* a ^ (b ^ a) -> b */
810 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_EOR_A_B_A);
818 * Optimize a - 0 and (a - x) + x (for modes with wrap-around).
820 * The second one looks strange, but this construct
821 * is used heavily in the LCC sources :-).
823 * Beware: The Mode of an Add may be different than the mode of its
824 * predecessors, so we could not return a predecessors in all cases.
826 static ir_node *equivalent_node_Add(ir_node *n)
829 ir_node *left, *right;
830 ir_mode *mode = get_irn_mode(n);
832 n = equivalent_node_neutral_zero(n);
836 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
837 if (mode_is_float(mode)) {
838 ir_graph *irg = get_irn_irg(n);
839 if (get_irg_fp_model(irg) & fp_strict_algebraic)
843 left = get_Add_left(n);
844 right = get_Add_right(n);
847 if (get_Sub_right(left) == right) {
850 n = get_Sub_left(left);
851 if (mode == get_irn_mode(n)) {
852 DBG_OPT_ALGSIM1(oldn, left, right, n, FS_OPT_ADD_SUB);
858 if (get_Sub_right(right) == left) {
861 n = get_Sub_left(right);
862 if (mode == get_irn_mode(n)) {
863 DBG_OPT_ALGSIM1(oldn, left, right, n, FS_OPT_ADD_SUB);
869 } /* equivalent_node_Add */
872 * optimize operations that are not commutative but have neutral 0 on left,
875 static ir_node *equivalent_node_left_zero(ir_node *n)
879 ir_node *a = get_binop_left(n);
880 ir_node *b = get_binop_right(n);
881 ir_tarval *tb = value_of(b);
883 if (tarval_is_null(tb)) {
886 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_0);
889 } /* equivalent_node_left_zero */
891 #define equivalent_node_Shl equivalent_node_left_zero
892 #define equivalent_node_Shr equivalent_node_left_zero
893 #define equivalent_node_Shrs equivalent_node_left_zero
894 #define equivalent_node_Rotl equivalent_node_left_zero
897 * Optimize a - 0 and (a + x) - x (for modes with wrap-around).
899 * The second one looks strange, but this construct
900 * is used heavily in the LCC sources :-).
902 * Beware: The Mode of a Sub may be different than the mode of its
903 * predecessors, so we could not return a predecessors in all cases.
905 static ir_node *equivalent_node_Sub(ir_node *n)
909 ir_mode *mode = get_irn_mode(n);
912 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
913 if (mode_is_float(mode)) {
914 ir_graph *irg = get_irn_irg(n);
915 if (get_irg_fp_model(irg) & fp_strict_algebraic)
919 b = get_Sub_right(n);
922 /* Beware: modes might be different */
923 if (tarval_is_null(tb)) {
924 ir_node *a = get_Sub_left(n);
925 if (mode == get_irn_mode(a)) {
928 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_0);
932 } /* equivalent_node_Sub */
936 * Optimize an "self-inverse unary op", ie op(op(n)) = n.
939 * -(-a) == a, but might overflow two times.
940 * We handle it anyway here but the better way would be a
941 * flag. This would be needed for Pascal for instance.
943 static ir_node *equivalent_node_idempotent_unop(ir_node *n)
946 ir_node *pred = get_unop_op(n);
948 /* optimize symmetric unop */
949 if (get_irn_op(pred) == get_irn_op(n)) {
950 n = get_unop_op(pred);
951 DBG_OPT_ALGSIM2(oldn, pred, n, FS_OPT_IDEM_UNARY);
954 } /* equivalent_node_idempotent_unop */
956 /** Optimize Not(Not(x)) == x. */
957 #define equivalent_node_Not equivalent_node_idempotent_unop
959 /** -(-x) == x ??? Is this possible or can --x raise an
960 out of bounds exception if min =! max? */
961 #define equivalent_node_Minus equivalent_node_idempotent_unop
964 * Optimize a * 1 = 1 * a = a.
966 static ir_node *equivalent_node_Mul(ir_node *n)
969 ir_node *a = get_Mul_left(n);
971 /* we can handle here only the n * n = n bit cases */
972 if (get_irn_mode(n) == get_irn_mode(a)) {
973 ir_node *b = get_Mul_right(n);
977 * Mul is commutative and has again an other neutral element.
978 * Constants are place right, so check this case first.
981 if (tarval_is_one(tv)) {
983 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
986 if (tarval_is_one(tv)) {
988 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
993 } /* equivalent_node_Mul */
996 * Use algebraic simplification a | a = a | 0 = 0 | a = a.
998 static ir_node *equivalent_node_Or(ir_node *n)
1002 ir_node *a = get_Or_left(n);
1003 ir_node *b = get_Or_right(n);
1007 n = a; /* idempotence */
1008 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_OR);
1011 /* constants are normalized to right, check this side first */
1013 if (tarval_is_null(tv)) {
1015 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_OR);
1019 if (tarval_is_null(tv)) {
1021 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_OR);
1026 } /* equivalent_node_Or */
1029 * Optimize a & 0b1...1 = 0b1...1 & a = a & a = (a|X) & a = a.
1031 static ir_node *equivalent_node_And(ir_node *n)
1035 ir_node *a = get_And_left(n);
1036 ir_node *b = get_And_right(n);
1040 n = a; /* idempotence */
1041 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_AND);
1044 /* constants are normalized to right, check this side first */
1046 if (tarval_is_all_one(tv)) {
1048 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1051 if (tv != get_tarval_bad()) {
1052 ir_mode *mode = get_irn_mode(n);
1053 if (!mode_is_signed(mode) && is_Conv(a)) {
1054 ir_node *convop = get_Conv_op(a);
1055 ir_mode *convopmode = get_irn_mode(convop);
1056 if (!mode_is_signed(convopmode)) {
1057 if (tarval_is_all_one(tarval_convert_to(tv, convopmode))) {
1058 /* Conv(X) & all_one(mode(X)) = Conv(X) */
1060 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1067 if (tarval_is_all_one(tv)) {
1069 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1073 if (b == get_Or_left(a) || b == get_Or_right(a)) {
1076 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1081 if (a == get_Or_left(b) || a == get_Or_right(b)) {
1084 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_AND);
1089 } /* equivalent_node_And */
1092 * Try to remove useless Conv's:
1094 static ir_node *equivalent_node_Conv(ir_node *n)
1097 ir_node *a = get_Conv_op(n);
1099 ir_mode *n_mode = get_irn_mode(n);
1100 ir_mode *a_mode = get_irn_mode(a);
1103 if (n_mode == a_mode) { /* No Conv necessary */
1104 if (get_Conv_strict(n)) {
1107 /* neither Minus nor Confirm change the precision,
1108 so we can "look-through" */
1111 p = get_Minus_op(p);
1112 } else if (is_Confirm(p)) {
1113 p = get_Confirm_value(p);
1119 if (is_Conv(p) && get_Conv_strict(p)) {
1120 /* we known already, that a_mode == n_mode, and neither
1121 Minus change the mode, so the second Conv
1123 assert(get_irn_mode(p) == n_mode);
1125 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CONV);
1129 ir_node *pred = get_Proj_pred(p);
1130 if (is_Load(pred)) {
1131 /* Loads always return with the exact precision of n_mode */
1132 assert(get_Load_mode(pred) == n_mode);
1134 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CONV);
1137 if (is_Proj(pred) && get_Proj_proj(pred) == pn_Start_T_args) {
1138 pred = get_Proj_pred(pred);
1139 if (is_Start(pred)) {
1140 /* Arguments always return with the exact precision,
1141 as strictConv's are place before Call -- if the
1142 caller was compiled with the same setting.
1143 Otherwise, the semantics is probably still right. */
1144 assert(get_irn_mode(p) == n_mode);
1146 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CONV);
1152 /* special case: the immediate predecessor is also a Conv */
1153 if (! get_Conv_strict(a)) {
1154 /* first one is not strict, kick it */
1156 a_mode = get_irn_mode(a);
1160 /* else both are strict conv, second is superfluous */
1162 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CONV);
1167 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_CONV);
1170 } else if (is_Conv(a)) { /* Conv(Conv(b)) */
1171 ir_node *b = get_Conv_op(a);
1172 ir_mode *b_mode = get_irn_mode(b);
1174 if (get_Conv_strict(n) && get_Conv_strict(a)) {
1175 /* both are strict conv */
1176 if (smaller_mode(a_mode, n_mode)) {
1177 /* both are strict, but the first is smaller, so
1178 the second cannot remove more precision, remove the
1180 set_Conv_strict(n, 0);
1183 if (n_mode == b_mode) {
1184 if (! get_Conv_strict(n) && ! get_Conv_strict(a)) {
1185 if (n_mode == mode_b) {
1186 n = b; /* Convb(Conv*(xxxb(...))) == xxxb(...) */
1187 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
1189 } else if (get_mode_arithmetic(n_mode) == get_mode_arithmetic(a_mode)) {
1190 if (values_in_mode(b_mode, a_mode)) {
1191 n = b; /* ConvS(ConvL(xxxS(...))) == xxxS(...) */
1192 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
1197 if (mode_is_int(n_mode) && get_mode_arithmetic(a_mode) == irma_ieee754) {
1198 /* ConvI(ConvF(I)) -> I, iff float mantissa >= int mode */
1199 unsigned int_mantissa = get_mode_size_bits(n_mode) - (mode_is_signed(n_mode) ? 1 : 0);
1200 unsigned float_mantissa = tarval_ieee754_get_mantissa_size(a_mode);
1202 if (float_mantissa >= int_mantissa) {
1204 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
1209 if (smaller_mode(b_mode, a_mode)) {
1210 if (get_Conv_strict(n))
1211 set_Conv_strict(b, 1);
1212 n = b; /* ConvA(ConvB(ConvA(...))) == ConvA(...) */
1213 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
1220 } /* equivalent_node_Conv */
1223 * - fold Phi-nodes, iff they have only one predecessor except
1226 static ir_node *equivalent_node_Phi(ir_node *n)
1232 ir_node *first_val = NULL; /* to shutup gcc */
1234 if (!get_opt_optimize() &&
1235 get_irg_phase_state(get_irn_irg(n)) != phase_building)
1238 n_preds = get_Phi_n_preds(n);
1240 block = get_nodes_block(n);
1242 /* Phi of dead Region without predecessors. */
1246 /* Find first non-self-referencing input */
1247 for (i = 0; i < n_preds; ++i) {
1248 first_val = get_Phi_pred(n, i);
1249 /* not self pointer */
1250 if (first_val != n) {
1251 /* then found first value. */
1256 /* search for rest of inputs, determine if any of these
1257 are non-self-referencing */
1258 while (++i < n_preds) {
1259 ir_node *scnd_val = get_Phi_pred(n, i);
1260 if (scnd_val != n && scnd_val != first_val) {
1265 if (i >= n_preds && !is_Dummy(first_val)) {
1266 /* Fold, if no multiple distinct non-self-referencing inputs */
1268 DBG_OPT_PHI(oldn, n);
1271 } /* equivalent_node_Phi */
1274 * Optimize Proj(Tuple).
1276 static ir_node *equivalent_node_Proj_Tuple(ir_node *proj)
1278 ir_node *oldn = proj;
1279 ir_node *tuple = get_Proj_pred(proj);
1281 /* Remove the Tuple/Proj combination. */
1282 proj = get_Tuple_pred(tuple, get_Proj_proj(proj));
1283 DBG_OPT_TUPLE(oldn, tuple, proj);
1286 } /* equivalent_node_Proj_Tuple */
1289 * Optimize a / 1 = a.
1291 static ir_node *equivalent_node_Proj_Div(ir_node *proj)
1293 ir_node *oldn = proj;
1294 ir_node *div = get_Proj_pred(proj);
1295 ir_node *b = get_Div_right(div);
1296 ir_tarval *tb = value_of(b);
1298 /* Div is not commutative. */
1299 if (tarval_is_one(tb)) { /* div(x, 1) == x */
1300 switch (get_Proj_proj(proj)) {
1302 proj = get_Div_mem(div);
1303 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NEUTRAL_1);
1307 proj = get_Div_left(div);
1308 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NEUTRAL_1);
1312 /* we cannot replace the exception Proj's here, this is done in
1313 transform_node_Proj_Div() */
1318 } /* equivalent_node_Proj_Div */
1321 * Optimize CopyB(mem, x, x) into a Nop.
1323 static ir_node *equivalent_node_Proj_CopyB(ir_node *proj)
1325 ir_node *oldn = proj;
1326 ir_node *copyb = get_Proj_pred(proj);
1327 ir_node *a = get_CopyB_dst(copyb);
1328 ir_node *b = get_CopyB_src(copyb);
1331 /* Turn CopyB into a tuple (mem, jmp, bad, bad) */
1332 switch (get_Proj_proj(proj)) {
1334 proj = get_CopyB_mem(copyb);
1335 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NOP);
1340 } /* equivalent_node_Proj_CopyB */
1343 * Optimize Bounds(idx, idx, upper) into idx.
1345 static ir_node *equivalent_node_Proj_Bound(ir_node *proj)
1347 ir_node *oldn = proj;
1348 ir_node *bound = get_Proj_pred(proj);
1349 ir_node *idx = get_Bound_index(bound);
1350 ir_node *pred = skip_Proj(idx);
1353 if (idx == get_Bound_lower(bound))
1355 else if (is_Bound(pred)) {
1357 * idx was Bounds checked previously, it is still valid if
1358 * lower <= pred_lower && pred_upper <= upper.
1360 ir_node *lower = get_Bound_lower(bound);
1361 ir_node *upper = get_Bound_upper(bound);
1362 if (get_Bound_lower(pred) == lower &&
1363 get_Bound_upper(pred) == upper) {
1365 * One could expect that we simply return the previous
1366 * Bound here. However, this would be wrong, as we could
1367 * add an exception Proj to a new location then.
1368 * So, we must turn in into a tuple.
1374 /* Turn Bound into a tuple (mem, jmp, bad, idx) */
1375 switch (get_Proj_proj(proj)) {
1377 DBG_OPT_EXC_REM(proj);
1378 proj = get_Bound_mem(bound);
1382 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NOP);
1385 /* cannot optimize pn_Bound_X_regular, handled in transform ... */
1390 } /* equivalent_node_Proj_Bound */
1393 * Does all optimizations on nodes that must be done on its Projs
1394 * because of creating new nodes.
1396 static ir_node *equivalent_node_Proj(ir_node *proj)
1398 ir_node *n = get_Proj_pred(proj);
1399 if (n->op->ops.equivalent_node_Proj)
1400 return n->op->ops.equivalent_node_Proj(proj);
1402 } /* equivalent_node_Proj */
1407 static ir_node *equivalent_node_Id(ir_node *n)
1415 DBG_OPT_ID(oldn, n);
1417 } /* equivalent_node_Id */
1422 static ir_node *equivalent_node_Mux(ir_node *n)
1424 ir_node *oldn = n, *sel = get_Mux_sel(n);
1426 ir_tarval *ts = value_of(sel);
1428 /* Mux(true, f, t) == t */
1429 if (ts == tarval_b_true) {
1430 n = get_Mux_true(n);
1431 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_C);
1434 /* Mux(false, f, t) == f */
1435 if (ts == tarval_b_false) {
1436 n = get_Mux_false(n);
1437 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_C);
1440 n_t = get_Mux_true(n);
1441 n_f = get_Mux_false(n);
1443 /* Mux(v, x, T) == x */
1444 if (is_Unknown(n_f)) {
1446 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_EQ);
1449 /* Mux(v, T, x) == x */
1450 if (is_Unknown(n_t)) {
1452 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_EQ);
1456 /* Mux(v, x, x) == x */
1459 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_EQ);
1462 if (is_Cmp(sel) && !mode_honor_signed_zeros(get_irn_mode(n))) {
1463 ir_relation relation = get_Cmp_relation(sel);
1464 ir_node *f = get_Mux_false(n);
1465 ir_node *t = get_Mux_true(n);
1468 * Note further that these optimization work even for floating point
1469 * with NaN's because -NaN == NaN.
1470 * However, if +0 and -0 is handled differently, we cannot use the first one.
1472 ir_node *const cmp_l = get_Cmp_left(sel);
1473 ir_node *const cmp_r = get_Cmp_right(sel);
1476 case ir_relation_equal:
1477 if ((cmp_l == t && cmp_r == f) || /* Mux(t == f, t, f) -> f */
1478 (cmp_l == f && cmp_r == t)) { /* Mux(f == t, t, f) -> f */
1480 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1485 case ir_relation_less_greater:
1486 case ir_relation_unordered_less_greater:
1487 if ((cmp_l == t && cmp_r == f) || /* Mux(t != f, t, f) -> t */
1488 (cmp_l == f && cmp_r == t)) { /* Mux(f != t, t, f) -> t */
1490 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1499 * Note: normalization puts the constant on the right side,
1500 * so we check only one case.
1502 if (cmp_l == t && tarval_is_null(value_of(cmp_r))) {
1503 /* Mux(t CMP 0, X, t) */
1504 if (is_Minus(f) && get_Minus_op(f) == t) {
1505 /* Mux(t CMP 0, -t, t) */
1506 if (relation == ir_relation_equal) {
1507 /* Mux(t == 0, -t, t) ==> -t */
1509 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1510 } else if (relation == ir_relation_less_greater || relation == ir_relation_unordered_less_greater) {
1511 /* Mux(t != 0, -t, t) ==> t */
1513 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1523 * Remove Confirm nodes if setting is on.
1524 * Replace Confirms(x, '=', Constlike) by Constlike.
1526 static ir_node *equivalent_node_Confirm(ir_node *n)
1528 ir_node *pred = get_Confirm_value(n);
1529 ir_relation relation = get_Confirm_relation(n);
1531 while (is_Confirm(pred) && relation == get_Confirm_relation(pred)) {
1533 * rare case: two identical Confirms one after another,
1534 * replace the second one with the first.
1537 pred = get_Confirm_value(n);
1543 * equivalent_node() returns a node equivalent to input n. It skips all nodes that
1544 * perform no actual computation, as, e.g., the Id nodes. It does not create
1545 * new nodes. It is therefore safe to free n if the node returned is not n.
1546 * If a node returns a Tuple we can not just skip it. If the size of the
1547 * in array fits, we transform n into a tuple (e.g., Div).
1549 ir_node *equivalent_node(ir_node *n)
1551 if (n->op->ops.equivalent_node)
1552 return n->op->ops.equivalent_node(n);
1554 } /* equivalent_node */
1557 * Sets the default equivalent node operation for an ir_op_ops.
1559 * @param code the opcode for the default operation
1560 * @param ops the operations initialized
1565 static ir_op_ops *firm_set_default_equivalent_node(ir_opcode code, ir_op_ops *ops)
1569 ops->equivalent_node = equivalent_node_##a; \
1571 #define CASE_PROJ(a) \
1573 ops->equivalent_node_Proj = equivalent_node_Proj_##a; \
1607 } /* firm_set_default_equivalent_node */
1610 * Returns non-zero if a node is a Phi node
1611 * with all predecessors constant.
1613 static int is_const_Phi(ir_node *n)
1617 if (! is_Phi(n) || get_irn_arity(n) == 0)
1619 for (i = get_irn_arity(n) - 1; i >= 0; --i) {
1620 if (! is_Const(get_irn_n(n, i)))
1624 } /* is_const_Phi */
1626 typedef ir_tarval *(*tarval_sub_type)(ir_tarval *a, ir_tarval *b, ir_mode *mode);
1627 typedef ir_tarval *(*tarval_binop_type)(ir_tarval *a, ir_tarval *b);
1630 * in reality eval_func should be tarval (*eval_func)() but incomplete
1631 * declarations are bad style and generate noisy warnings
1633 typedef void (*eval_func)(void);
1636 * Wrapper for the tarval binop evaluation, tarval_sub has one more parameter.
1638 static ir_tarval *do_eval(eval_func eval, ir_tarval *a, ir_tarval *b, ir_mode *mode)
1640 if (eval == (eval_func) tarval_sub) {
1641 tarval_sub_type func = (tarval_sub_type)eval;
1643 return func(a, b, mode);
1645 tarval_binop_type func = (tarval_binop_type)eval;
1652 * Apply an evaluator on a binop with a constant operators (and one Phi).
1654 * @param phi the Phi node
1655 * @param other the other operand
1656 * @param eval an evaluator function
1657 * @param mode the mode of the result, may be different from the mode of the Phi!
1658 * @param left if non-zero, other is the left operand, else the right
1660 * @return a new Phi node if the conversion was successful, NULL else
1662 static ir_node *apply_binop_on_phi(ir_node *phi, ir_tarval *other, eval_func eval, ir_mode *mode, int left)
1668 int i, n = get_irn_arity(phi);
1670 NEW_ARR_A(void *, res, n);
1672 for (i = 0; i < n; ++i) {
1673 pred = get_irn_n(phi, i);
1674 tv = get_Const_tarval(pred);
1675 tv = do_eval(eval, other, tv, mode);
1677 if (tv == tarval_bad) {
1678 /* folding failed, bad */
1684 for (i = 0; i < n; ++i) {
1685 pred = get_irn_n(phi, i);
1686 tv = get_Const_tarval(pred);
1687 tv = do_eval(eval, tv, other, mode);
1689 if (tv == tarval_bad) {
1690 /* folding failed, bad */
1696 irg = get_irn_irg(phi);
1697 for (i = 0; i < n; ++i) {
1698 pred = get_irn_n(phi, i);
1699 res[i] = new_r_Const(irg, (ir_tarval*)res[i]);
1701 return new_r_Phi(get_nodes_block(phi), n, (ir_node **)res, mode);
1702 } /* apply_binop_on_phi */
1705 * Apply an evaluator on a binop with two constant Phi.
1707 * @param a the left Phi node
1708 * @param b the right Phi node
1709 * @param eval an evaluator function
1710 * @param mode the mode of the result, may be different from the mode of the Phi!
1712 * @return a new Phi node if the conversion was successful, NULL else
1714 static ir_node *apply_binop_on_2_phis(ir_node *a, ir_node *b, eval_func eval, ir_mode *mode)
1716 ir_tarval *tv_l, *tv_r, *tv;
1722 if (get_nodes_block(a) != get_nodes_block(b))
1725 n = get_irn_arity(a);
1726 NEW_ARR_A(void *, res, n);
1728 for (i = 0; i < n; ++i) {
1729 pred = get_irn_n(a, i);
1730 tv_l = get_Const_tarval(pred);
1731 pred = get_irn_n(b, i);
1732 tv_r = get_Const_tarval(pred);
1733 tv = do_eval(eval, tv_l, tv_r, mode);
1735 if (tv == tarval_bad) {
1736 /* folding failed, bad */
1741 irg = get_irn_irg(a);
1742 for (i = 0; i < n; ++i) {
1743 pred = get_irn_n(a, i);
1744 res[i] = new_r_Const(irg, (ir_tarval*)res[i]);
1746 return new_r_Phi(get_nodes_block(a), n, (ir_node **)res, mode);
1747 } /* apply_binop_on_2_phis */
1750 * Apply an evaluator on a unop with a constant operator (a Phi).
1752 * @param phi the Phi node
1753 * @param eval an evaluator function
1755 * @return a new Phi node if the conversion was successful, NULL else
1757 static ir_node *apply_unop_on_phi(ir_node *phi, ir_tarval *(*eval)(ir_tarval *))
1764 int i, n = get_irn_arity(phi);
1766 NEW_ARR_A(void *, res, n);
1767 for (i = 0; i < n; ++i) {
1768 pred = get_irn_n(phi, i);
1769 tv = get_Const_tarval(pred);
1772 if (tv == tarval_bad) {
1773 /* folding failed, bad */
1778 mode = get_irn_mode(phi);
1779 irg = get_irn_irg(phi);
1780 for (i = 0; i < n; ++i) {
1781 pred = get_irn_n(phi, i);
1782 res[i] = new_r_Const(irg, (ir_tarval*)res[i]);
1784 return new_r_Phi(get_nodes_block(phi), n, (ir_node **)res, mode);
1785 } /* apply_unop_on_phi */
1788 * Apply a conversion on a constant operator (a Phi).
1790 * @param phi the Phi node
1792 * @return a new Phi node if the conversion was successful, NULL else
1794 static ir_node *apply_conv_on_phi(ir_node *phi, ir_mode *mode)
1800 int i, n = get_irn_arity(phi);
1802 NEW_ARR_A(void *, res, n);
1803 for (i = 0; i < n; ++i) {
1804 pred = get_irn_n(phi, i);
1805 tv = get_Const_tarval(pred);
1806 tv = tarval_convert_to(tv, mode);
1808 if (tv == tarval_bad) {
1809 /* folding failed, bad */
1814 irg = get_irn_irg(phi);
1815 for (i = 0; i < n; ++i) {
1816 pred = get_irn_n(phi, i);
1817 res[i] = new_r_Const(irg, (ir_tarval*)res[i]);
1819 return new_r_Phi(get_nodes_block(phi), n, (ir_node **)res, mode);
1820 } /* apply_conv_on_phi */
1823 * Transform AddP(P, ConvIs(Iu)), AddP(P, ConvIu(Is)) and
1824 * SubP(P, ConvIs(Iu)), SubP(P, ConvIu(Is)).
1825 * If possible, remove the Conv's.
1827 static ir_node *transform_node_AddSub(ir_node *n)
1829 ir_mode *mode = get_irn_mode(n);
1831 if (mode_is_reference(mode)) {
1832 ir_node *left = get_binop_left(n);
1833 ir_node *right = get_binop_right(n);
1834 unsigned ref_bits = get_mode_size_bits(mode);
1836 if (is_Conv(left)) {
1837 ir_mode *lmode = get_irn_mode(left);
1838 unsigned bits = get_mode_size_bits(lmode);
1840 if (ref_bits == bits &&
1841 mode_is_int(lmode) &&
1842 get_mode_arithmetic(lmode) == irma_twos_complement) {
1843 ir_node *pre = get_Conv_op(left);
1844 ir_mode *pre_mode = get_irn_mode(pre);
1846 if (mode_is_int(pre_mode) &&
1847 get_mode_size_bits(pre_mode) == bits &&
1848 get_mode_arithmetic(pre_mode) == irma_twos_complement) {
1849 /* ok, this conv just changes to sign, moreover the calculation
1850 * is done with same number of bits as our address mode, so
1851 * we can ignore the conv as address calculation can be viewed
1852 * as either signed or unsigned
1854 set_binop_left(n, pre);
1859 if (is_Conv(right)) {
1860 ir_mode *rmode = get_irn_mode(right);
1861 unsigned bits = get_mode_size_bits(rmode);
1863 if (ref_bits == bits &&
1864 mode_is_int(rmode) &&
1865 get_mode_arithmetic(rmode) == irma_twos_complement) {
1866 ir_node *pre = get_Conv_op(right);
1867 ir_mode *pre_mode = get_irn_mode(pre);
1869 if (mode_is_int(pre_mode) &&
1870 get_mode_size_bits(pre_mode) == bits &&
1871 get_mode_arithmetic(pre_mode) == irma_twos_complement) {
1872 /* ok, this conv just changes to sign, moreover the calculation
1873 * is done with same number of bits as our address mode, so
1874 * we can ignore the conv as address calculation can be viewed
1875 * as either signed or unsigned
1877 set_binop_right(n, pre);
1882 /* let address arithmetic use unsigned modes */
1883 if (is_Const(right)) {
1884 ir_mode *rmode = get_irn_mode(right);
1886 if (mode_is_signed(rmode) && get_mode_arithmetic(rmode) == irma_twos_complement) {
1887 /* convert a AddP(P, *s) into AddP(P, *u) */
1888 ir_mode *nm = get_reference_mode_unsigned_eq(mode);
1890 ir_node *pre = new_r_Conv(get_nodes_block(n), right, nm);
1891 set_binop_right(n, pre);
1897 } /* transform_node_AddSub */
1899 #define HANDLE_BINOP_PHI(eval, a, b, c, mode) \
1902 if (is_Const(b) && is_const_Phi(a)) { \
1903 /* check for Op(Phi, Const) */ \
1904 c = apply_binop_on_phi(a, get_Const_tarval(b), eval, mode, 0);\
1906 else if (is_Const(a) && is_const_Phi(b)) { \
1907 /* check for Op(Const, Phi) */ \
1908 c = apply_binop_on_phi(b, get_Const_tarval(a), eval, mode, 1);\
1910 else if (is_const_Phi(a) && is_const_Phi(b)) { \
1911 /* check for Op(Phi, Phi) */ \
1912 c = apply_binop_on_2_phis(a, b, eval, mode); \
1915 DBG_OPT_ALGSIM0(oldn, c, FS_OPT_CONST_PHI); \
1920 #define HANDLE_UNOP_PHI(eval, a, c) \
1923 if (is_const_Phi(a)) { \
1924 /* check for Op(Phi) */ \
1925 c = apply_unop_on_phi(a, eval); \
1927 DBG_OPT_ALGSIM0(oldn, c, FS_OPT_CONST_PHI); \
1934 * Do the AddSub optimization, then Transform
1935 * Constant folding on Phi
1936 * Add(a,a) -> Mul(a, 2)
1937 * Add(Mul(a, x), a) -> Mul(a, x+1)
1938 * if the mode is integer or float.
1939 * Transform Add(a,-b) into Sub(a,b).
1940 * Reassociation might fold this further.
1942 static ir_node *transform_node_Add(ir_node *n)
1945 ir_node *a, *b, *c, *oldn = n;
1946 vrp_attr *a_vrp, *b_vrp;
1948 n = transform_node_AddSub(n);
1950 a = get_Add_left(n);
1951 b = get_Add_right(n);
1953 mode = get_irn_mode(n);
1955 if (mode_is_reference(mode)) {
1956 ir_mode *lmode = get_irn_mode(a);
1958 if (is_Const(b) && is_Const_null(b) && mode_is_int(lmode)) {
1959 /* an Add(a, NULL) is a hidden Conv */
1960 dbg_info *dbg = get_irn_dbg_info(n);
1961 return new_rd_Conv(dbg, get_nodes_block(n), a, mode);
1965 HANDLE_BINOP_PHI((eval_func) tarval_add, a, b, c, mode);
1967 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
1968 if (mode_is_float(mode)) {
1969 ir_graph *irg = get_irn_irg(n);
1970 if (get_irg_fp_model(irg) & fp_strict_algebraic)
1974 if (mode_is_num(mode)) {
1975 ir_graph *irg = get_irn_irg(n);
1976 /* the following code leads to endless recursion when Mul are replaced by a simple instruction chain */
1977 if (!is_irg_state(irg, IR_GRAPH_STATE_ARCH_DEP)
1978 && a == b && mode_is_int(mode)) {
1979 ir_node *block = get_nodes_block(n);
1982 get_irn_dbg_info(n),
1985 new_r_Const_long(irg, mode, 2),
1987 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_A);
1992 get_irn_dbg_info(n),
1997 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_MINUS_B);
2002 get_irn_dbg_info(n),
2007 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_MINUS_B);
2010 if (get_mode_arithmetic(mode) == irma_twos_complement) {
2011 /* Here we rely on constants be on the RIGHT side */
2013 ir_node *op = get_Not_op(a);
2015 if (is_Const(b) && is_Const_one(b)) {
2017 ir_node *blk = get_nodes_block(n);
2018 n = new_rd_Minus(get_irn_dbg_info(n), blk, op, mode);
2019 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_PLUS_1);
2024 n = new_r_Const(irg, get_mode_minus_one(mode));
2025 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_X_NOT_X);
2030 ir_node *op = get_Not_op(b);
2034 n = new_r_Const(irg, get_mode_minus_one(mode));
2035 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_X_NOT_X);
2042 a_vrp = vrp_get_info(a);
2043 b_vrp = vrp_get_info(b);
2045 if (a_vrp && b_vrp) {
2046 ir_tarval *c = tarval_and(a_vrp->bits_not_set, b_vrp->bits_not_set);
2048 if (tarval_is_null(c)) {
2049 dbg_info *dbgi = get_irn_dbg_info(n);
2050 return new_rd_Or(dbgi, get_nodes_block(n), a, b, mode);
2054 } /* transform_node_Add */
2057 * returns -cnst or NULL if impossible
2059 static ir_node *const_negate(ir_node *cnst)
2061 ir_tarval *tv = tarval_neg(get_Const_tarval(cnst));
2062 dbg_info *dbgi = get_irn_dbg_info(cnst);
2063 ir_graph *irg = get_irn_irg(cnst);
2064 if (tv == tarval_bad) return NULL;
2065 return new_rd_Const(dbgi, irg, tv);
2069 * Do the AddSub optimization, then Transform
2070 * Constant folding on Phi
2071 * Sub(0,a) -> Minus(a)
2072 * Sub(Mul(a, x), a) -> Mul(a, x-1)
2073 * Sub(Sub(x, y), b) -> Sub(x, Add(y,b))
2074 * Sub(Add(a, x), x) -> a
2075 * Sub(x, Add(x, a)) -> -a
2076 * Sub(x, Const) -> Add(x, -Const)
2078 static ir_node *transform_node_Sub(ir_node *n)
2084 n = transform_node_AddSub(n);
2086 a = get_Sub_left(n);
2087 b = get_Sub_right(n);
2089 mode = get_irn_mode(n);
2091 if (mode_is_int(mode)) {
2092 ir_mode *lmode = get_irn_mode(a);
2094 if (is_Const(b) && is_Const_null(b) && mode_is_reference(lmode)) {
2095 /* a Sub(a, NULL) is a hidden Conv */
2096 dbg_info *dbg = get_irn_dbg_info(n);
2097 n = new_rd_Conv(dbg, get_nodes_block(n), a, mode);
2098 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_CONV);
2102 if (mode == lmode &&
2103 get_mode_arithmetic(mode) == irma_twos_complement &&
2105 get_Const_tarval(a) == get_mode_minus_one(mode)) {
2107 dbg_info *dbg = get_irn_dbg_info(n);
2108 n = new_rd_Not(dbg, get_nodes_block(n), b, mode);
2109 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_NOT);
2115 HANDLE_BINOP_PHI((eval_func) tarval_sub, a, b, c, mode);
2117 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
2118 if (mode_is_float(mode)) {
2119 ir_graph *irg = get_irn_irg(n);
2120 if (get_irg_fp_model(irg) & fp_strict_algebraic)
2124 if (is_Const(b) && !mode_is_reference(get_irn_mode(b))) {
2125 /* a - C -> a + (-C) */
2126 ir_node *cnst = const_negate(b);
2128 ir_node *block = get_nodes_block(n);
2129 dbg_info *dbgi = get_irn_dbg_info(n);
2131 n = new_rd_Add(dbgi, block, a, cnst, mode);
2132 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2137 if (is_Minus(a)) { /* (-a) - b -> -(a + b) */
2138 dbg_info *dbg = get_irn_dbg_info(n);
2139 ir_node *block = get_nodes_block(n);
2140 ir_node *left = get_Minus_op(a);
2141 ir_node *add = new_rd_Add(dbg, block, left, b, mode);
2143 n = new_rd_Minus(dbg, block, add, mode);
2144 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2146 } else if (is_Minus(b)) { /* a - (-b) -> a + b */
2147 dbg_info *dbg = get_irn_dbg_info(n);
2148 ir_node *block = get_nodes_block(n);
2149 ir_node *right = get_Minus_op(b);
2151 n = new_rd_Add(dbg, block, a, right, mode);
2152 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MINUS);
2154 } else if (is_Sub(b)) {
2155 /* a - (b - c) -> a + (c - b)
2156 * -> (a - b) + c iff (b - c) is a pointer */
2157 dbg_info *s_dbg = get_irn_dbg_info(b);
2158 ir_node *s_left = get_Sub_left(b);
2159 ir_node *s_right = get_Sub_right(b);
2160 ir_mode *s_mode = get_irn_mode(b);
2161 if (mode_is_reference(s_mode)) {
2162 ir_node *lowest_block = get_nodes_block(n); /* a and b are live here */
2163 ir_node *sub = new_rd_Sub(s_dbg, lowest_block, a, s_left, mode);
2164 dbg_info *a_dbg = get_irn_dbg_info(n);
2167 s_right = new_r_Conv(lowest_block, s_right, mode);
2168 n = new_rd_Add(a_dbg, lowest_block, sub, s_right, mode);
2170 ir_node *s_block = get_nodes_block(b);
2171 ir_node *sub = new_rd_Sub(s_dbg, s_block, s_right, s_left, s_mode);
2172 dbg_info *a_dbg = get_irn_dbg_info(n);
2173 ir_node *a_block = get_nodes_block(n);
2175 n = new_rd_Add(a_dbg, a_block, a, sub, mode);
2177 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2179 } else if (is_Mul(b)) { /* a - (b * C) -> a + (b * -C) */
2180 ir_node *m_right = get_Mul_right(b);
2181 if (is_Const(m_right)) {
2182 ir_node *cnst2 = const_negate(m_right);
2183 if (cnst2 != NULL) {
2184 dbg_info *m_dbg = get_irn_dbg_info(b);
2185 ir_node *m_block = get_nodes_block(b);
2186 ir_node *m_left = get_Mul_left(b);
2187 ir_mode *m_mode = get_irn_mode(b);
2188 ir_node *mul = new_rd_Mul(m_dbg, m_block, m_left, cnst2, m_mode);
2189 dbg_info *a_dbg = get_irn_dbg_info(n);
2190 ir_node *a_block = get_nodes_block(n);
2192 n = new_rd_Add(a_dbg, a_block, a, mul, mode);
2193 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2199 /* Beware of Sub(P, P) which cannot be optimized into a simple Minus ... */
2200 if (mode_is_num(mode) && mode == get_irn_mode(a) && is_Const(a) && is_Const_null(a)) {
2202 get_irn_dbg_info(n),
2206 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_0_A);
2210 if (mode_wrap_around(mode)) {
2211 ir_node *left = get_Add_left(a);
2212 ir_node *right = get_Add_right(a);
2214 /* FIXME: Does the Conv's work only for two complement or generally? */
2216 if (mode != get_irn_mode(right)) {
2217 /* This Sub is an effective Cast */
2218 right = new_r_Conv(get_nodes_block(n), right, mode);
2221 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2223 } else if (right == b) {
2224 if (mode != get_irn_mode(left)) {
2225 /* This Sub is an effective Cast */
2226 left = new_r_Conv(get_nodes_block(n), left, mode);
2229 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2235 if (mode_wrap_around(mode)) {
2236 ir_node *left = get_Add_left(b);
2237 ir_node *right = get_Add_right(b);
2239 /* FIXME: Does the Conv's work only for two complement or generally? */
2241 ir_mode *r_mode = get_irn_mode(right);
2243 n = new_r_Minus(get_nodes_block(n), right, r_mode);
2244 if (mode != r_mode) {
2245 /* This Sub is an effective Cast */
2246 n = new_r_Conv(get_nodes_block(n), n, mode);
2248 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2250 } else if (right == a) {
2251 ir_mode *l_mode = get_irn_mode(left);
2253 n = new_r_Minus(get_nodes_block(n), left, l_mode);
2254 if (mode != l_mode) {
2255 /* This Sub is an effective Cast */
2256 n = new_r_Conv(get_nodes_block(n), n, mode);
2258 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2263 if (mode_is_int(mode) && is_Conv(a) && is_Conv(b)) {
2264 ir_mode *mode = get_irn_mode(a);
2266 if (mode == get_irn_mode(b)) {
2268 ir_node *op_a = get_Conv_op(a);
2269 ir_node *op_b = get_Conv_op(b);
2271 /* check if it's allowed to skip the conv */
2272 ma = get_irn_mode(op_a);
2273 mb = get_irn_mode(op_b);
2275 if (mode_is_reference(ma) && mode_is_reference(mb)) {
2276 /* SubInt(ConvInt(aP), ConvInt(bP)) -> SubInt(aP,bP) */
2279 set_Sub_right(n, b);
2285 /* do NOT execute this code if reassociation is enabled, it does the inverse! */
2286 if (!is_reassoc_running() && is_Mul(a)) {
2287 ir_node *ma = get_Mul_left(a);
2288 ir_node *mb = get_Mul_right(a);
2291 ir_node *blk = get_nodes_block(n);
2292 ir_graph *irg = get_irn_irg(n);
2294 get_irn_dbg_info(n),
2298 get_irn_dbg_info(n),
2301 new_r_Const(irg, get_mode_one(mode)),
2304 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MUL_A_X_A);
2306 } else if (mb == b) {
2307 ir_node *blk = get_nodes_block(n);
2308 ir_graph *irg = get_irn_irg(n);
2310 get_irn_dbg_info(n),
2314 get_irn_dbg_info(n),
2317 new_r_Const(irg, get_mode_one(mode)),
2320 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MUL_A_X_A);
2324 if (is_Sub(a)) { /* (x - y) - b -> x - (y + b) */
2325 ir_node *x = get_Sub_left(a);
2326 ir_node *y = get_Sub_right(a);
2327 ir_node *blk = get_nodes_block(n);
2328 ir_mode *m_b = get_irn_mode(b);
2329 ir_mode *m_y = get_irn_mode(y);
2333 /* Determine the right mode for the Add. */
2336 else if (mode_is_reference(m_b))
2338 else if (mode_is_reference(m_y))
2342 * Both modes are different but none is reference,
2343 * happens for instance in SubP(SubP(P, Iu), Is).
2344 * We have two possibilities here: Cast or ignore.
2345 * Currently we ignore this case.
2350 add = new_r_Add(blk, y, b, add_mode);
2352 n = new_rd_Sub(get_irn_dbg_info(n), blk, x, add, mode);
2353 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_SUB_X_Y_Z);
2357 if (get_mode_arithmetic(mode) == irma_twos_complement) {
2358 if (is_Const(a) && is_Not(b)) {
2359 /* c - ~X = X + (c+1) */
2360 ir_tarval *tv = get_Const_tarval(a);
2362 tv = tarval_add(tv, get_mode_one(mode));
2363 if (tv != tarval_bad) {
2364 ir_node *blk = get_nodes_block(n);
2365 ir_graph *irg = get_irn_irg(n);
2366 ir_node *c = new_r_Const(irg, tv);
2367 n = new_rd_Add(get_irn_dbg_info(n), blk, get_Not_op(b), c, mode);
2368 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_C_NOT_X);
2374 } /* transform_node_Sub */
2377 * Several transformation done on n*n=2n bits mul.
2378 * These transformations must be done here because new nodes may be produced.
2380 static ir_node *transform_node_Mul2n(ir_node *n, ir_mode *mode)
2383 ir_node *a = get_Mul_left(n);
2384 ir_node *b = get_Mul_right(n);
2385 ir_tarval *ta = value_of(a);
2386 ir_tarval *tb = value_of(b);
2387 ir_mode *smode = get_irn_mode(a);
2389 if (ta == get_mode_one(smode)) {
2390 /* (L)1 * (L)b = (L)b */
2391 ir_node *blk = get_nodes_block(n);
2392 n = new_rd_Conv(get_irn_dbg_info(n), blk, b, mode);
2393 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
2396 else if (ta == get_mode_minus_one(smode)) {
2397 /* (L)-1 * (L)b = (L)b */
2398 ir_node *blk = get_nodes_block(n);
2399 n = new_rd_Minus(get_irn_dbg_info(n), blk, b, smode);
2400 n = new_rd_Conv(get_irn_dbg_info(n), blk, n, mode);
2401 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2404 if (tb == get_mode_one(smode)) {
2405 /* (L)a * (L)1 = (L)a */
2406 ir_node *blk = get_irn_n(a, -1);
2407 n = new_rd_Conv(get_irn_dbg_info(n), blk, a, mode);
2408 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
2411 else if (tb == get_mode_minus_one(smode)) {
2412 /* (L)a * (L)-1 = (L)-a */
2413 ir_node *blk = get_nodes_block(n);
2414 n = new_rd_Minus(get_irn_dbg_info(n), blk, a, smode);
2415 n = new_rd_Conv(get_irn_dbg_info(n), blk, n, mode);
2416 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2423 * Transform Mul(a,-1) into -a.
2424 * Do constant evaluation of Phi nodes.
2425 * Do architecture dependent optimizations on Mul nodes
2427 static ir_node *transform_node_Mul(ir_node *n)
2429 ir_node *c, *oldn = n;
2430 ir_mode *mode = get_irn_mode(n);
2431 ir_node *a = get_Mul_left(n);
2432 ir_node *b = get_Mul_right(n);
2434 if (is_Bad(a) || is_Bad(b))
2437 if (mode != get_irn_mode(a))
2438 return transform_node_Mul2n(n, mode);
2440 HANDLE_BINOP_PHI((eval_func) tarval_mul, a, b, c, mode);
2442 if (mode_is_signed(mode)) {
2445 if (value_of(a) == get_mode_minus_one(mode))
2447 else if (value_of(b) == get_mode_minus_one(mode))
2450 n = new_rd_Minus(get_irn_dbg_info(n), get_nodes_block(n), r, mode);
2451 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2456 if (is_Const(b)) { /* (-a) * const -> a * -const */
2457 ir_node *cnst = const_negate(b);
2459 dbg_info *dbgi = get_irn_dbg_info(n);
2460 ir_node *block = get_nodes_block(n);
2461 n = new_rd_Mul(dbgi, block, get_Minus_op(a), cnst, mode);
2462 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2465 } else if (is_Minus(b)) { /* (-a) * (-b) -> a * b */
2466 dbg_info *dbgi = get_irn_dbg_info(n);
2467 ir_node *block = get_nodes_block(n);
2468 n = new_rd_Mul(dbgi, block, get_Minus_op(a), get_Minus_op(b), mode);
2469 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_MINUS);
2471 } else if (is_Sub(b)) { /* (-a) * (b - c) -> a * (c - b) */
2472 ir_node *sub_l = get_Sub_left(b);
2473 ir_node *sub_r = get_Sub_right(b);
2474 dbg_info *dbgi = get_irn_dbg_info(n);
2475 ir_node *block = get_nodes_block(n);
2476 ir_node *new_b = new_rd_Sub(dbgi, block, sub_r, sub_l, mode);
2477 n = new_rd_Mul(dbgi, block, get_Minus_op(a), new_b, mode);
2478 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS);
2481 } else if (is_Minus(b)) {
2482 if (is_Sub(a)) { /* (a - b) * (-c) -> (b - a) * c */
2483 ir_node *sub_l = get_Sub_left(a);
2484 ir_node *sub_r = get_Sub_right(a);
2485 dbg_info *dbgi = get_irn_dbg_info(n);
2486 ir_node *block = get_nodes_block(n);
2487 ir_node *new_a = new_rd_Sub(dbgi, block, sub_r, sub_l, mode);
2488 n = new_rd_Mul(dbgi, block, new_a, get_Minus_op(b), mode);
2489 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS);
2492 } else if (is_Shl(a)) {
2493 ir_node *const shl_l = get_Shl_left(a);
2494 if (is_Const(shl_l) && is_Const_one(shl_l)) {
2495 /* (1 << x) * b -> b << x */
2496 dbg_info *const dbgi = get_irn_dbg_info(n);
2497 ir_node *const block = get_nodes_block(n);
2498 ir_node *const shl_r = get_Shl_right(a);
2499 n = new_rd_Shl(dbgi, block, b, shl_r, mode);
2500 // TODO add me DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_SHIFT);
2503 } else if (is_Shl(b)) {
2504 ir_node *const shl_l = get_Shl_left(b);
2505 if (is_Const(shl_l) && is_Const_one(shl_l)) {
2506 /* a * (1 << x) -> a << x */
2507 dbg_info *const dbgi = get_irn_dbg_info(n);
2508 ir_node *const block = get_nodes_block(n);
2509 ir_node *const shl_r = get_Shl_right(b);
2510 n = new_rd_Shl(dbgi, block, a, shl_r, mode);
2511 // TODO add me DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_SHIFT);
2515 if (get_mode_arithmetic(mode) == irma_ieee754) {
2517 ir_tarval *tv = get_Const_tarval(a);
2518 if (tarval_ieee754_get_exponent(tv) == 1 && tarval_ieee754_zero_mantissa(tv)
2519 && !tarval_is_negative(tv)) {
2520 /* 2.0 * b = b + b */
2521 n = new_rd_Add(get_irn_dbg_info(n), get_nodes_block(n), b, b, mode);
2522 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_A_A);
2526 else if (is_Const(b)) {
2527 ir_tarval *tv = get_Const_tarval(b);
2528 if (tarval_ieee754_get_exponent(tv) == 1 && tarval_ieee754_zero_mantissa(tv)
2529 && !tarval_is_negative(tv)) {
2530 /* a * 2.0 = a + a */
2531 n = new_rd_Add(get_irn_dbg_info(n), get_nodes_block(n), a, a, mode);
2532 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_A_A);
2537 return arch_dep_replace_mul_with_shifts(n);
2538 } /* transform_node_Mul */
2541 * Transform a Div Node.
2543 static ir_node *transform_node_Div(ir_node *n)
2545 ir_mode *mode = get_Div_resmode(n);
2546 ir_node *a = get_Div_left(n);
2547 ir_node *b = get_Div_right(n);
2549 const ir_node *dummy;
2551 if (mode_is_int(mode)) {
2552 if (is_Const(b) && is_const_Phi(a)) {
2553 /* check for Div(Phi, Const) */
2554 value = apply_binop_on_phi(a, get_Const_tarval(b), (eval_func) tarval_div, mode, 0);
2556 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2559 } else if (is_Const(a) && is_const_Phi(b)) {
2560 /* check for Div(Const, Phi) */
2561 value = apply_binop_on_phi(b, get_Const_tarval(a), (eval_func) tarval_div, mode, 1);
2563 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2566 } else if (is_const_Phi(a) && is_const_Phi(b)) {
2567 /* check for Div(Phi, Phi) */
2568 value = apply_binop_on_2_phis(a, b, (eval_func) tarval_div, mode);
2570 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2575 if (a == b && value_not_zero(a, &dummy)) {
2576 ir_graph *irg = get_irn_irg(n);
2577 /* BEWARE: we can optimize a/a to 1 only if this cannot cause a exception */
2578 value = new_r_Const(irg, get_mode_one(mode));
2579 DBG_OPT_CSTEVAL(n, value);
2582 if (mode_is_signed(mode) && is_Const(b)) {
2583 ir_tarval *tv = get_Const_tarval(b);
2585 if (tv == get_mode_minus_one(mode)) {
2587 value = new_rd_Minus(get_irn_dbg_info(n), get_nodes_block(n), a, mode);
2588 DBG_OPT_CSTEVAL(n, value);
2592 /* Try architecture dependent optimization */
2593 value = arch_dep_replace_div_by_const(n);
2596 assert(mode_is_float(mode));
2598 /* Optimize x/c to x*(1/c) */
2599 if (get_mode_arithmetic(mode) == irma_ieee754) {
2600 ir_tarval *tv = value_of(b);
2602 if (tv != tarval_bad) {
2603 int rem = tarval_fp_ops_enabled();
2606 * Floating point constant folding might be disabled here to
2608 * However, as we check for exact result, doing it is safe.
2611 tarval_enable_fp_ops(1);
2612 tv = tarval_div(get_mode_one(mode), tv);
2613 tarval_enable_fp_ops(rem);
2615 /* Do the transformation if the result is either exact or we are
2616 not using strict rules. */
2617 if (tv != tarval_bad &&
2618 (tarval_ieee754_get_exact() || (get_irg_fp_model(get_irn_irg(n)) & fp_strict_algebraic) == 0)) {
2619 ir_node *block = get_nodes_block(n);
2620 ir_graph *irg = get_irn_irg(block);
2621 ir_node *c = new_r_Const(irg, tv);
2622 dbg_info *dbgi = get_irn_dbg_info(n);
2623 value = new_rd_Mul(dbgi, block, a, c, mode);
2636 /* Turn Div into a tuple (mem, jmp, bad, value) */
2637 mem = get_Div_mem(n);
2638 blk = get_nodes_block(n);
2639 irg = get_irn_irg(blk);
2641 /* skip a potential Pin */
2642 mem = skip_Pin(mem);
2643 turn_into_tuple(n, pn_Div_max);
2644 set_Tuple_pred(n, pn_Div_M, mem);
2645 set_Tuple_pred(n, pn_Div_X_regular, new_r_Jmp(blk));
2646 set_Tuple_pred(n, pn_Div_X_except, new_r_Bad(irg, mode_X));
2647 set_Tuple_pred(n, pn_Div_res, value);
2650 } /* transform_node_Div */
2653 * Transform a Mod node.
2655 static ir_node *transform_node_Mod(ir_node *n)
2657 ir_mode *mode = get_Mod_resmode(n);
2658 ir_node *a = get_Mod_left(n);
2659 ir_node *b = get_Mod_right(n);
2664 if (is_Const(b) && is_const_Phi(a)) {
2665 /* check for Div(Phi, Const) */
2666 value = apply_binop_on_phi(a, get_Const_tarval(b), (eval_func) tarval_mod, mode, 0);
2668 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2672 else if (is_Const(a) && is_const_Phi(b)) {
2673 /* check for Div(Const, Phi) */
2674 value = apply_binop_on_phi(b, get_Const_tarval(a), (eval_func) tarval_mod, mode, 1);
2676 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2680 else if (is_const_Phi(a) && is_const_Phi(b)) {
2681 /* check for Div(Phi, Phi) */
2682 value = apply_binop_on_2_phis(a, b, (eval_func) tarval_mod, mode);
2684 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2691 irg = get_irn_irg(n);
2692 if (tv != tarval_bad) {
2693 value = new_r_Const(irg, tv);
2695 DBG_OPT_CSTEVAL(n, value);
2698 ir_node *a = get_Mod_left(n);
2699 ir_node *b = get_Mod_right(n);
2700 const ir_node *dummy;
2702 if (a == b && value_not_zero(a, &dummy)) {
2703 /* BEWARE: we can optimize a%a to 0 only if this cannot cause a exception */
2704 value = new_r_Const(irg, get_mode_null(mode));
2705 DBG_OPT_CSTEVAL(n, value);
2708 if (mode_is_signed(mode) && is_Const(b)) {
2709 ir_tarval *tv = get_Const_tarval(b);
2711 if (tv == get_mode_minus_one(mode)) {
2713 value = new_r_Const(irg, get_mode_null(mode));
2714 DBG_OPT_CSTEVAL(n, value);
2718 /* Try architecture dependent optimization */
2719 value = arch_dep_replace_mod_by_const(n);
2728 /* Turn Mod into a tuple (mem, jmp, bad, value) */
2729 mem = get_Mod_mem(n);
2730 blk = get_nodes_block(n);
2731 irg = get_irn_irg(blk);
2733 /* skip a potential Pin */
2734 mem = skip_Pin(mem);
2735 turn_into_tuple(n, pn_Mod_max);
2736 set_Tuple_pred(n, pn_Mod_M, mem);
2737 set_Tuple_pred(n, pn_Mod_X_regular, new_r_Jmp(blk));
2738 set_Tuple_pred(n, pn_Mod_X_except, new_r_Bad(irg, mode_X));
2739 set_Tuple_pred(n, pn_Mod_res, value);
2742 } /* transform_node_Mod */
2745 * Transform a Cond node.
2747 * Replace the Cond by a Jmp if it branches on a constant
2750 static ir_node *transform_node_Cond(ir_node *n)
2753 ir_node *a = get_Cond_selector(n);
2754 ir_tarval *ta = value_of(a);
2755 ir_graph *irg = get_irn_irg(n);
2758 /* we need block info which is not available in floating irgs */
2759 if (get_irg_pinned(irg) == op_pin_state_floats)
2762 if ((ta != tarval_bad) &&
2763 (get_irn_mode(a) == mode_b) &&
2764 (get_opt_unreachable_code())) {
2765 /* It's a boolean Cond, branching on a boolean constant.
2766 Replace it by a tuple (Bad, Jmp) or (Jmp, Bad) */
2767 ir_node *blk = get_nodes_block(n);
2768 jmp = new_r_Jmp(blk);
2769 turn_into_tuple(n, pn_Cond_max);
2770 if (ta == tarval_b_true) {
2771 set_Tuple_pred(n, pn_Cond_false, new_r_Bad(irg, mode_X));
2772 set_Tuple_pred(n, pn_Cond_true, jmp);
2774 set_Tuple_pred(n, pn_Cond_false, jmp);
2775 set_Tuple_pred(n, pn_Cond_true, new_r_Bad(irg, mode_X));
2777 /* We might generate an endless loop, so keep it alive. */
2778 add_End_keepalive(get_irg_end(irg), blk);
2781 } /* transform_node_Cond */
2784 * Prototype of a recursive transform function
2785 * for bitwise distributive transformations.
2787 typedef ir_node* (*recursive_transform)(ir_node *n);
2790 * makes use of distributive laws for and, or, eor
2791 * and(a OP c, b OP c) -> and(a, b) OP c
2792 * note, might return a different op than n
2794 static ir_node *transform_bitwise_distributive(ir_node *n,
2795 recursive_transform trans_func)
2798 ir_node *a = get_binop_left(n);
2799 ir_node *b = get_binop_right(n);
2800 ir_op *op = get_irn_op(a);
2801 ir_op *op_root = get_irn_op(n);
2803 if (op != get_irn_op(b))
2806 /* and(conv(a), conv(b)) -> conv(and(a,b)) */
2807 if (op == op_Conv) {
2808 ir_node *a_op = get_Conv_op(a);
2809 ir_node *b_op = get_Conv_op(b);
2810 ir_mode *a_mode = get_irn_mode(a_op);
2811 ir_mode *b_mode = get_irn_mode(b_op);
2812 if (a_mode == b_mode && (mode_is_int(a_mode) || a_mode == mode_b)) {
2813 ir_node *blk = get_nodes_block(n);
2816 set_binop_left(n, a_op);
2817 set_binop_right(n, b_op);
2818 set_irn_mode(n, a_mode);
2820 n = new_r_Conv(blk, n, get_irn_mode(oldn));
2822 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
2828 /* nothing to gain here */
2832 if (op == op_Shrs || op == op_Shr || op == op_Shl
2833 || op == op_And || op == op_Or || op == op_Eor) {
2834 ir_node *a_left = get_binop_left(a);
2835 ir_node *a_right = get_binop_right(a);
2836 ir_node *b_left = get_binop_left(b);
2837 ir_node *b_right = get_binop_right(b);
2839 ir_node *op1 = NULL;
2840 ir_node *op2 = NULL;
2842 if (is_op_commutative(op)) {
2843 if (a_left == b_left) {
2847 } else if (a_left == b_right) {
2851 } else if (a_right == b_left) {
2857 if (a_right == b_right) {
2864 /* (a sop c) & (b sop c) => (a & b) sop c */
2865 ir_node *blk = get_nodes_block(n);
2867 ir_node *new_n = exact_copy(n);
2868 set_binop_left(new_n, op1);
2869 set_binop_right(new_n, op2);
2870 new_n = trans_func(new_n);
2872 if (op_root == op_Eor && op == op_Or) {
2873 dbg_info *dbgi = get_irn_dbg_info(n);
2874 ir_mode *mode = get_irn_mode(c);
2876 c = new_rd_Not(dbgi, blk, c, mode);
2877 n = new_rd_And(dbgi, blk, new_n, c, mode);
2880 set_nodes_block(n, blk);
2881 set_binop_left(n, new_n);
2882 set_binop_right(n, c);
2886 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_SHIFT_AND);
2895 * Create a 0 constant of given mode.
2897 static ir_node *create_zero_const(ir_graph *irg, ir_mode *mode)
2899 ir_tarval *tv = get_mode_null(mode);
2900 ir_node *cnst = new_r_Const(irg, tv);
2908 static ir_node *transform_node_And(ir_node *n)
2910 ir_node *c, *oldn = n;
2911 ir_node *a = get_And_left(n);
2912 ir_node *b = get_And_right(n);
2914 vrp_attr *a_vrp, *b_vrp;
2916 if (is_Cmp(a) && is_Cmp(b)) {
2917 ir_node *a_left = get_Cmp_left(a);
2918 ir_node *a_right = get_Cmp_right(a);
2919 ir_node *b_left = get_Cmp_left(b);
2920 ir_node *b_right = get_Cmp_right(b);
2921 ir_relation a_relation = get_Cmp_relation(a);
2922 ir_relation b_relation = get_Cmp_relation(b);
2923 /* we can combine the relations of two compares with the same
2925 if (a_left == b_left && b_left == b_right) {
2926 dbg_info *dbgi = get_irn_dbg_info(n);
2927 ir_node *block = get_nodes_block(n);
2928 ir_relation new_relation = a_relation & b_relation;
2929 return new_rd_Cmp(dbgi, block, a_left, a_right, new_relation);
2931 /* Cmp(a==0) and Cmp(b==0) can be optimized to Cmp(a|b==0) */
2932 if (is_Const(a_right) && is_Const_null(a_right)
2933 && is_Const(b_right) && is_Const_null(b_right)
2934 && a_relation == b_relation && a_relation == ir_relation_equal
2935 && !mode_is_float(get_irn_mode(a_left))
2936 && !mode_is_float(get_irn_mode(b_left))) {
2937 dbg_info *dbgi = get_irn_dbg_info(n);
2938 ir_node *block = get_nodes_block(n);
2939 ir_mode *mode = get_irn_mode(a_left);
2940 ir_node *n_b_left = get_irn_mode(b_left) != mode ?
2941 new_rd_Conv(dbgi, block, b_left, mode) : b_left;
2942 ir_node *or = new_rd_Or(dbgi, block, a_left, n_b_left, mode);
2943 ir_graph *irg = get_irn_irg(n);
2944 ir_node *zero = create_zero_const(irg, mode);
2945 return new_rd_Cmp(dbgi, block, or, zero, ir_relation_equal);
2949 mode = get_irn_mode(n);
2950 HANDLE_BINOP_PHI((eval_func) tarval_and, a, b, c, mode);
2954 ir_node *op = get_Not_op(b);
2956 ir_node *ba = get_And_left(op);
2957 ir_node *bb = get_And_right(op);
2959 /* it's enough to test the following cases due to normalization! */
2960 if (get_Or_left(a) == ba && get_Or_right(a) == bb) {
2961 /* (a|b) & ~(a&b) = a^b */
2962 ir_node *block = get_nodes_block(n);
2964 n = new_rd_Eor(get_irn_dbg_info(n), block, ba, bb, mode);
2965 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_TO_EOR);
2973 ir_node *op = get_Not_op(a);
2975 ir_node *aa = get_And_left(op);
2976 ir_node *ab = get_And_right(op);
2978 /* it's enough to test the following cases due to normalization! */
2979 if (get_Or_left(b) == aa && get_Or_right(b) == ab) {
2980 /* (a|b) & ~(a&b) = a^b */
2981 ir_node *block = get_nodes_block(n);
2983 n = new_rd_Eor(get_irn_dbg_info(n), block, aa, ab, mode);
2984 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_TO_EOR);
2991 ir_node *al = get_Eor_left(a);
2992 ir_node *ar = get_Eor_right(a);
2995 /* (b ^ a) & b -> ~a & b */
2996 dbg_info *dbg = get_irn_dbg_info(n);
2997 ir_node *block = get_nodes_block(n);
2999 ar = new_rd_Not(dbg, block, ar, mode);
3000 n = new_rd_And(dbg, block, ar, b, mode);
3001 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3005 /* (a ^ b) & b -> ~a & b */
3006 dbg_info *dbg = get_irn_dbg_info(n);
3007 ir_node *block = get_nodes_block(n);
3009 al = new_rd_Not(dbg, block, al, mode);
3010 n = new_rd_And(dbg, block, al, b, mode);
3011 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3016 ir_node *bl = get_Eor_left(b);
3017 ir_node *br = get_Eor_right(b);
3020 /* a & (a ^ b) -> a & ~b */
3021 dbg_info *dbg = get_irn_dbg_info(n);
3022 ir_node *block = get_nodes_block(n);
3024 br = new_rd_Not(dbg, block, br, mode);
3025 n = new_rd_And(dbg, block, br, a, mode);
3026 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3030 /* a & (b ^ a) -> a & ~b */
3031 dbg_info *dbg = get_irn_dbg_info(n);
3032 ir_node *block = get_nodes_block(n);
3034 bl = new_rd_Not(dbg, block, bl, mode);
3035 n = new_rd_And(dbg, block, bl, a, mode);
3036 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3040 if (is_Not(a) && is_Not(b)) {
3041 /* ~a & ~b = ~(a|b) */
3042 ir_node *block = get_nodes_block(n);
3043 ir_mode *mode = get_irn_mode(n);
3047 n = new_rd_Or(get_irn_dbg_info(n), block, a, b, mode);
3048 n = new_rd_Not(get_irn_dbg_info(n), block, n, mode);
3049 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_DEMORGAN);
3053 b_vrp = vrp_get_info(b);
3054 if (is_Const(a) && b_vrp && (tarval_cmp(tarval_or(get_Const_tarval(a),
3055 b_vrp->bits_not_set), get_Const_tarval(a)) == ir_relation_equal)) {
3061 a_vrp = vrp_get_info(a);
3062 if (is_Const(b) && a_vrp && (tarval_cmp(tarval_or(get_Const_tarval(b),
3063 a_vrp->bits_not_set), get_Const_tarval(b)) == ir_relation_equal)) {
3067 n = transform_bitwise_distributive(n, transform_node_And);
3070 } /* transform_node_And */
3072 /* the order of the values is important! */
3073 typedef enum const_class {
3079 static const_class classify_const(const ir_node* n)
3081 if (is_Const(n)) return const_const;
3082 if (is_irn_constlike(n)) return const_like;
3087 * Determines whether r is more constlike or has a larger index (in that order)
3090 static bool operands_are_normalized(const ir_node *l, const ir_node *r)
3092 const const_class l_order = classify_const(l);
3093 const const_class r_order = classify_const(r);
3095 l_order > r_order ||
3096 (l_order == r_order && get_irn_idx(l) <= get_irn_idx(r));
3102 static ir_node *transform_node_Eor(ir_node *n)
3104 ir_node *c, *oldn = n;
3105 ir_node *a = get_Eor_left(n);
3106 ir_node *b = get_Eor_right(n);
3107 ir_mode *mode = get_irn_mode(n);
3109 /* we can combine the relations of two compares with the same operands */
3110 if (is_Cmp(a) && is_Cmp(b)) {
3111 ir_node *a_left = get_Cmp_left(a);
3112 ir_node *a_right = get_Cmp_left(a);
3113 ir_node *b_left = get_Cmp_left(b);
3114 ir_node *b_right = get_Cmp_right(b);
3115 if (a_left == b_left && b_left == b_right) {
3116 dbg_info *dbgi = get_irn_dbg_info(n);
3117 ir_node *block = get_nodes_block(n);
3118 ir_relation a_relation = get_Cmp_relation(a);
3119 ir_relation b_relation = get_Cmp_relation(b);
3120 ir_relation new_relation = a_relation ^ b_relation;
3121 return new_rd_Cmp(dbgi, block, a_left, a_right, new_relation);
3125 HANDLE_BINOP_PHI((eval_func) tarval_eor, a, b, c, mode);
3127 /* normalize not nodes... ~a ^ b <=> a ^ ~b */
3128 if (is_Not(a) && operands_are_normalized(get_Not_op(a), b)) {
3129 dbg_info *dbg = get_irn_dbg_info(n);
3130 ir_node *block = get_nodes_block(n);
3131 ir_node *new_not = new_rd_Not(dbg, block, b, mode);
3132 ir_node *new_left = get_Not_op(a);
3133 n = new_rd_Eor(dbg, block, new_left, new_not, mode);
3134 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3136 } else if (is_Not(b) && !operands_are_normalized(a, get_Not_op(b))) {
3137 dbg_info *dbg = get_irn_dbg_info(n);
3138 ir_node *block = get_nodes_block(n);
3139 ir_node *new_not = new_rd_Not(dbg, block, a, mode);
3140 ir_node *new_right = get_Not_op(b);
3141 n = new_rd_Eor(dbg, block, new_not, new_right, mode);
3142 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3146 /* x ^ 1...1 -> ~1 */
3147 if (is_Const(b) && is_Const_all_one(b)) {
3148 n = new_r_Not(get_nodes_block(n), a, mode);
3149 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3153 n = transform_bitwise_distributive(n, transform_node_Eor);
3155 } /* transform_node_Eor */
3160 static ir_node *transform_node_Not(ir_node *n)
3162 ir_node *c, *oldn = n;
3163 ir_node *a = get_Not_op(n);
3164 ir_mode *mode = get_irn_mode(n);
3166 HANDLE_UNOP_PHI(tarval_not,a,c);
3168 /* check for a boolean Not */
3170 dbg_info *dbgi = get_irn_dbg_info(a);
3171 ir_node *block = get_nodes_block(a);
3172 ir_relation relation = get_Cmp_relation(a);
3173 relation = get_negated_relation(relation);
3174 n = new_rd_Cmp(dbgi, block, get_Cmp_left(a), get_Cmp_right(a), relation);
3175 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_CMP);
3179 /* normalize ~(a ^ b) => a ^ ~b */
3181 dbg_info *dbg = get_irn_dbg_info(n);
3182 ir_node *block = get_nodes_block(n);
3183 ir_node *eor_right = get_Eor_right(a);
3184 ir_node *eor_left = get_Eor_left(a);
3185 eor_right = new_rd_Not(dbg, block, eor_right, mode);
3186 n = new_rd_Eor(dbg, block, eor_left, eor_right, mode);
3190 if (get_mode_arithmetic(mode) == irma_twos_complement) {
3191 if (is_Minus(a)) { /* ~-x -> x + -1 */
3192 dbg_info *dbg = get_irn_dbg_info(n);
3193 ir_graph *irg = get_irn_irg(n);
3194 ir_node *block = get_nodes_block(n);
3195 ir_node *add_l = get_Minus_op(a);
3196 ir_node *add_r = new_rd_Const(dbg, irg, get_mode_minus_one(mode));
3197 n = new_rd_Add(dbg, block, add_l, add_r, mode);
3198 } else if (is_Add(a)) {
3199 ir_node *add_r = get_Add_right(a);
3200 if (is_Const(add_r) && is_Const_all_one(add_r)) {
3201 /* ~(x + -1) = -x */
3202 ir_node *op = get_Add_left(a);
3203 ir_node *blk = get_nodes_block(n);
3204 n = new_rd_Minus(get_irn_dbg_info(n), blk, op, get_irn_mode(n));
3205 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_MINUS_1);
3213 * Transform a Minus.
3217 * -(a >>u (size-1)) = a >>s (size-1)
3218 * -(a >>s (size-1)) = a >>u (size-1)
3219 * -(a * const) -> a * -const
3221 static ir_node *transform_node_Minus(ir_node *n)
3223 ir_node *c, *oldn = n;
3224 ir_node *a = get_Minus_op(n);
3227 HANDLE_UNOP_PHI(tarval_neg,a,c);
3229 mode = get_irn_mode(a);
3230 if (get_mode_arithmetic(mode) == irma_twos_complement) {
3231 /* the following rules are only to twos-complement */
3234 ir_node *op = get_Not_op(a);
3235 ir_tarval *tv = get_mode_one(mode);
3236 ir_node *blk = get_nodes_block(n);
3237 ir_graph *irg = get_irn_irg(blk);
3238 ir_node *c = new_r_Const(irg, tv);
3239 n = new_rd_Add(get_irn_dbg_info(n), blk, op, c, mode);
3240 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_MINUS_NOT);
3244 ir_node *c = get_Shr_right(a);
3247 ir_tarval *tv = get_Const_tarval(c);
3249 if (tarval_is_long(tv) && get_tarval_long(tv) == (int) get_mode_size_bits(mode) - 1) {
3250 /* -(a >>u (size-1)) = a >>s (size-1) */
3251 ir_node *v = get_Shr_left(a);
3253 n = new_rd_Shrs(get_irn_dbg_info(n), get_nodes_block(n), v, c, mode);
3254 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_PREDICATE);
3260 ir_node *c = get_Shrs_right(a);
3263 ir_tarval *tv = get_Const_tarval(c);
3265 if (tarval_is_long(tv) && get_tarval_long(tv) == (int) get_mode_size_bits(mode) - 1) {
3266 /* -(a >>s (size-1)) = a >>u (size-1) */
3267 ir_node *v = get_Shrs_left(a);
3269 n = new_rd_Shr(get_irn_dbg_info(n), get_nodes_block(n), v, c, mode);
3270 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_PREDICATE);
3277 /* - (a-b) = b - a */
3278 ir_node *la = get_Sub_left(a);
3279 ir_node *ra = get_Sub_right(a);
3280 ir_node *blk = get_nodes_block(n);
3282 n = new_rd_Sub(get_irn_dbg_info(n), blk, ra, la, mode);
3283 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_MINUS_SUB);
3287 if (is_Mul(a)) { /* -(a * const) -> a * -const */
3288 ir_node *mul_l = get_Mul_left(a);
3289 ir_node *mul_r = get_Mul_right(a);
3290 ir_tarval *tv = value_of(mul_r);
3291 if (tv != tarval_bad) {
3292 tv = tarval_neg(tv);
3293 if (tv != tarval_bad) {
3294 ir_graph *irg = get_irn_irg(n);
3295 ir_node *cnst = new_r_Const(irg, tv);
3296 dbg_info *dbg = get_irn_dbg_info(a);
3297 ir_node *block = get_nodes_block(a);
3298 n = new_rd_Mul(dbg, block, mul_l, cnst, mode);
3299 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_MINUS_MUL_C);
3306 } /* transform_node_Minus */
3309 * Transform a Proj(Load) with a non-null address.
3311 static ir_node *transform_node_Proj_Load(ir_node *proj)
3313 if (get_opt_ldst_only_null_ptr_exceptions()) {
3314 if (get_irn_mode(proj) == mode_X) {
3315 ir_node *load = get_Proj_pred(proj);
3317 /* get the Load address */
3318 const ir_node *addr = get_Load_ptr(load);
3319 const ir_node *confirm;
3321 if (value_not_null(addr, &confirm)) {
3322 if (confirm == NULL) {
3323 /* this node may float if it did not depend on a Confirm */
3324 set_irn_pinned(load, op_pin_state_floats);
3326 if (get_Proj_proj(proj) == pn_Load_X_except) {
3327 ir_graph *irg = get_irn_irg(proj);
3328 DBG_OPT_EXC_REM(proj);
3329 return new_r_Bad(irg, mode_X);
3331 ir_node *blk = get_nodes_block(load);
3332 return new_r_Jmp(blk);
3338 } /* transform_node_Proj_Load */
3341 * Transform a Proj(Store) with a non-null address.
3343 static ir_node *transform_node_Proj_Store(ir_node *proj)
3345 if (get_opt_ldst_only_null_ptr_exceptions()) {
3346 if (get_irn_mode(proj) == mode_X) {
3347 ir_node *store = get_Proj_pred(proj);
3349 /* get the load/store address */
3350 const ir_node *addr = get_Store_ptr(store);
3351 const ir_node *confirm;
3353 if (value_not_null(addr, &confirm)) {
3354 if (confirm == NULL) {
3355 /* this node may float if it did not depend on a Confirm */
3356 set_irn_pinned(store, op_pin_state_floats);
3358 if (get_Proj_proj(proj) == pn_Store_X_except) {
3359 ir_graph *irg = get_irn_irg(proj);
3360 DBG_OPT_EXC_REM(proj);
3361 return new_r_Bad(irg, mode_X);
3363 ir_node *blk = get_nodes_block(store);
3364 return new_r_Jmp(blk);
3370 } /* transform_node_Proj_Store */
3373 * Transform a Proj(Div) with a non-zero value.
3374 * Removes the exceptions and routes the memory to the NoMem node.
3376 static ir_node *transform_node_Proj_Div(ir_node *proj)
3378 ir_node *div = get_Proj_pred(proj);
3379 ir_node *b = get_Div_right(div);
3380 ir_node *res, *new_mem;
3381 const ir_node *confirm;
3384 if (value_not_zero(b, &confirm)) {
3385 /* div(x, y) && y != 0 */
3386 if (confirm == NULL) {
3387 /* we are sure we have a Const != 0 */
3388 new_mem = get_Div_mem(div);
3389 new_mem = skip_Pin(new_mem);
3390 set_Div_mem(div, new_mem);
3391 set_irn_pinned(div, op_pin_state_floats);
3394 proj_nr = get_Proj_proj(proj);
3396 case pn_Div_X_regular:
3397 return new_r_Jmp(get_nodes_block(div));
3399 case pn_Div_X_except: {
3400 ir_graph *irg = get_irn_irg(proj);
3401 /* we found an exception handler, remove it */
3402 DBG_OPT_EXC_REM(proj);
3403 return new_r_Bad(irg, mode_X);
3407 ir_graph *irg = get_irn_irg(proj);
3408 res = get_Div_mem(div);
3409 new_mem = get_irg_no_mem(irg);
3412 /* This node can only float up to the Confirm block */
3413 new_mem = new_r_Pin(get_nodes_block(confirm), new_mem);
3415 set_irn_pinned(div, op_pin_state_floats);
3416 /* this is a Div without exception, we can remove the memory edge */
3417 set_Div_mem(div, new_mem);
3423 } /* transform_node_Proj_Div */
3426 * Transform a Proj(Mod) with a non-zero value.
3427 * Removes the exceptions and routes the memory to the NoMem node.
3429 static ir_node *transform_node_Proj_Mod(ir_node *proj)
3431 ir_node *mod = get_Proj_pred(proj);
3432 ir_node *b = get_Mod_right(mod);
3433 ir_node *res, *new_mem;
3434 const ir_node *confirm;
3437 if (value_not_zero(b, &confirm)) {
3438 /* mod(x, y) && y != 0 */
3439 proj_nr = get_Proj_proj(proj);
3441 if (confirm == NULL) {
3442 /* we are sure we have a Const != 0 */
3443 new_mem = get_Mod_mem(mod);
3444 new_mem = skip_Pin(new_mem);
3445 set_Mod_mem(mod, new_mem);
3446 set_irn_pinned(mod, op_pin_state_floats);
3451 case pn_Mod_X_regular:
3452 return new_r_Jmp(get_irn_n(mod, -1));
3454 case pn_Mod_X_except: {
3455 ir_graph *irg = get_irn_irg(proj);
3456 /* we found an exception handler, remove it */
3457 DBG_OPT_EXC_REM(proj);
3458 return new_r_Bad(irg, mode_X);
3462 ir_graph *irg = get_irn_irg(proj);
3463 res = get_Mod_mem(mod);
3464 new_mem = get_irg_no_mem(irg);
3467 /* This node can only float up to the Confirm block */
3468 new_mem = new_r_Pin(get_nodes_block(confirm), new_mem);
3470 /* this is a Mod without exception, we can remove the memory edge */
3471 set_Mod_mem(mod, new_mem);
3475 if (get_Mod_left(mod) == b) {
3476 /* a % a = 0 if a != 0 */
3477 ir_graph *irg = get_irn_irg(proj);
3478 ir_mode *mode = get_irn_mode(proj);
3479 ir_node *res = new_r_Const(irg, get_mode_null(mode));
3481 DBG_OPT_CSTEVAL(mod, res);
3487 } /* transform_node_Proj_Mod */
3490 * Optimizes jump tables (CondIs or CondIu) by removing all impossible cases.
3492 static ir_node *transform_node_Proj_Cond(ir_node *proj)
3494 ir_node *n = get_Proj_pred(proj);
3495 ir_node *b = get_Cond_selector(n);
3497 if (!get_opt_unreachable_code())
3500 if (mode_is_int(get_irn_mode(b))) {
3501 ir_tarval *tb = value_of(b);
3503 if (tb != tarval_bad) {
3504 /* we have a constant switch */
3505 long num = get_Proj_proj(proj);
3507 if (num != get_Cond_default_proj(n)) { /* we cannot optimize default Proj's yet */
3508 if (get_tarval_long(tb) == num) {
3509 /* Do NOT create a jump here, or we will have 2 control flow ops
3510 * in a block. This case is optimized away in optimize_cf(). */
3513 ir_graph *irg = get_irn_irg(proj);
3514 /* this case will NEVER be taken, kill it */
3515 return new_r_Bad(irg, mode_X);
3519 long num = get_Proj_proj(proj);
3520 vrp_attr *b_vrp = vrp_get_info(b);
3521 if (num != get_Cond_default_proj(n) && b_vrp) {
3522 /* Try handling with vrp data. We only remove dead parts. */
3523 ir_tarval *tp = new_tarval_from_long(num, get_irn_mode(b));
3525 if (b_vrp->range_type == VRP_RANGE) {
3526 ir_relation cmp_result = tarval_cmp(b_vrp->range_bottom, tp);
3527 ir_relation cmp_result2 = tarval_cmp(b_vrp->range_top, tp);
3529 if ((cmp_result & ir_relation_greater) == cmp_result
3530 && (cmp_result2 & ir_relation_less) == cmp_result2) {
3531 ir_graph *irg = get_irn_irg(proj);
3532 return new_r_Bad(irg, mode_X);
3534 } else if (b_vrp->range_type == VRP_ANTIRANGE) {
3535 ir_relation cmp_result = tarval_cmp(b_vrp->range_bottom, tp);
3536 ir_relation cmp_result2 = tarval_cmp(b_vrp->range_top, tp);
3538 if ((cmp_result & ir_relation_less_equal) == cmp_result
3539 && (cmp_result2 & ir_relation_greater_equal) == cmp_result2) {
3540 ir_graph *irg = get_irn_irg(proj);
3541 return new_r_Bad(irg, mode_X);
3546 tarval_and( b_vrp->bits_set, tp),
3548 ) == ir_relation_equal)) {
3549 ir_graph *irg = get_irn_irg(proj);
3550 return new_r_Bad(irg, mode_X);
3556 tarval_not(b_vrp->bits_not_set)),
3557 tarval_not(b_vrp->bits_not_set))
3558 == ir_relation_equal)) {
3559 ir_graph *irg = get_irn_irg(proj);
3560 return new_r_Bad(irg, mode_X);
3569 * return true if the operation returns a value with exactly 1 bit set
3571 static bool is_single_bit(const ir_node *node)
3573 /* a first implementation, could be extended with vrp and others... */
3575 ir_node *shl_l = get_Shl_left(node);
3576 ir_mode *mode = get_irn_mode(node);
3577 int modulo = get_mode_modulo_shift(mode);
3578 /* this works if we shift a 1 and we have modulo shift */
3579 if (is_Const(shl_l) && is_Const_one(shl_l)
3580 && 0 < modulo && modulo <= (int)get_mode_size_bits(mode)) {
3583 } else if (is_Const(node)) {
3584 ir_tarval *tv = get_Const_tarval(node);
3585 return tarval_is_single_bit(tv);
3591 * Normalizes and optimizes Cmp nodes.
3593 static ir_node *transform_node_Cmp(ir_node *n)
3595 ir_node *left = get_Cmp_left(n);
3596 ir_node *right = get_Cmp_right(n);
3597 ir_mode *mode = get_irn_mode(left);
3598 ir_tarval *tv = NULL;
3599 bool changed = false;
3600 bool changedc = false;
3601 ir_relation relation = get_Cmp_relation(n);
3602 ir_relation possible = ir_get_possible_cmp_relations(left, right);
3604 /* mask out impossible relations */
3605 ir_relation new_relation = relation & possible;
3606 if (new_relation != relation) {
3607 relation = new_relation;
3611 /* Remove unnecessary conversions */
3612 /* TODO handle conv+constant */
3613 if (is_Conv(left) && is_Conv(right)) {
3614 ir_node *op_left = get_Conv_op(left);
3615 ir_node *op_right = get_Conv_op(right);
3616 ir_mode *mode_left = get_irn_mode(op_left);
3617 ir_mode *mode_right = get_irn_mode(op_right);
3619 if (smaller_mode(mode_left, mode) && smaller_mode(mode_right, mode)
3620 && mode_left != mode_b && mode_right != mode_b) {
3621 ir_node *block = get_nodes_block(n);
3623 if (mode_left == mode_right) {
3627 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV_CONV);
3628 } else if (smaller_mode(mode_left, mode_right)) {
3629 left = new_r_Conv(block, op_left, mode_right);
3632 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV);
3633 } else if (smaller_mode(mode_right, mode_left)) {
3635 right = new_r_Conv(block, op_right, mode_left);
3637 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV);
3643 * Optimize -a CMP -b into b CMP a.
3644 * This works only for modes where unary Minus cannot Overflow.
3645 * Note that two-complement integers can Overflow so it will NOT work.
3647 if (!mode_overflow_on_unary_Minus(mode) &&
3648 is_Minus(left) && is_Minus(right)) {
3649 left = get_Minus_op(left);
3650 right = get_Minus_op(right);
3651 relation = get_inversed_relation(relation);
3653 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3656 /* remove operation on both sides if possible */
3657 if (relation == ir_relation_equal || relation == ir_relation_less_greater) {
3659 * The following operations are NOT safe for floating point operations, for instance
3660 * 1.0 + inf == 2.0 + inf, =/=> x == y
3662 if (mode_is_int(mode)) {
3663 unsigned lop = get_irn_opcode(left);
3665 if (lop == get_irn_opcode(right)) {
3666 ir_node *ll, *lr, *rl, *rr;
3668 /* same operation on both sides, try to remove */
3672 /* ~a CMP ~b => a CMP b, -a CMP -b ==> a CMP b */
3673 left = get_unop_op(left);
3674 right = get_unop_op(right);
3676 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3679 ll = get_Add_left(left);
3680 lr = get_Add_right(left);
3681 rl = get_Add_left(right);
3682 rr = get_Add_right(right);
3685 /* X + a CMP X + b ==> a CMP b */
3689 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3690 } else if (ll == rr) {
3691 /* X + a CMP b + X ==> a CMP b */
3695 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3696 } else if (lr == rl) {
3697 /* a + X CMP X + b ==> a CMP b */
3701 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3702 } else if (lr == rr) {
3703 /* a + X CMP b + X ==> a CMP b */
3707 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3711 ll = get_Sub_left(left);
3712 lr = get_Sub_right(left);
3713 rl = get_Sub_left(right);
3714 rr = get_Sub_right(right);
3717 /* X - a CMP X - b ==> a CMP b */
3721 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3722 } else if (lr == rr) {
3723 /* a - X CMP b - X ==> a CMP b */
3727 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3731 if (get_Rotl_right(left) == get_Rotl_right(right)) {
3732 /* a ROTL X CMP b ROTL X ==> a CMP b */
3733 left = get_Rotl_left(left);
3734 right = get_Rotl_left(right);
3736 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3744 /* X+A == A, A+X == A, A-X == A -> X == 0 */
3745 if (is_Add(left) || is_Sub(left)) {
3746 ir_node *ll = get_binop_left(left);
3747 ir_node *lr = get_binop_right(left);
3749 if (lr == right && is_Add(left)) {
3755 ir_graph *irg = get_irn_irg(n);
3757 right = create_zero_const(irg, mode);
3759 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3762 if (is_Add(right) || is_Sub(right)) {
3763 ir_node *rl = get_binop_left(right);
3764 ir_node *rr = get_binop_right(right);
3766 if (rr == left && is_Add(right)) {
3772 ir_graph *irg = get_irn_irg(n);
3774 right = create_zero_const(irg, mode);
3776 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3780 if (is_And(left) && is_Const(right)) {
3781 ir_node *ll = get_binop_left(left);
3782 ir_node *lr = get_binop_right(left);
3783 if (is_Shr(ll) && is_Const(lr)) {
3784 /* Cmp((x >>u c1) & c2, c3) = Cmp(x & (c2 << c1), c3 << c1) */
3785 ir_node *block = get_nodes_block(n);
3786 ir_mode *mode = get_irn_mode(left);
3788 ir_node *llr = get_Shr_right(ll);
3789 if (is_Const(llr)) {
3790 dbg_info *dbg = get_irn_dbg_info(left);
3791 ir_graph *irg = get_irn_irg(left);
3793 ir_tarval *c1 = get_Const_tarval(llr);
3794 ir_tarval *c2 = get_Const_tarval(lr);
3795 ir_tarval *c3 = get_Const_tarval(right);
3796 ir_tarval *mask = tarval_shl(c2, c1);
3797 ir_tarval *value = tarval_shl(c3, c1);
3799 left = new_rd_And(dbg, block, get_Shr_left(ll), new_r_Const(irg, mask), mode);
3800 right = new_r_Const(irg, value);
3805 /* Cmp(Eor(x, y), 0) <=> Cmp(x, y) at least for the ==0,!=0
3807 if (is_Const(right) && is_Const_null(right) && is_Eor(left)) {
3808 right = get_Eor_right(left);
3809 left = get_Eor_left(left);
3812 } /* mode_is_int(...) */
3815 /* Cmp(And(1bit, val), 1bit) "bit-testing" can be replaced
3816 * by the simpler Cmp(And(1bit), val), 0) negated pnc */
3817 if (mode_is_int(mode) && is_And(left)
3818 && (relation == ir_relation_equal
3819 || (mode_is_signed(mode) && relation == ir_relation_less_greater)
3820 || (!mode_is_signed(mode) && (relation & ir_relation_less_equal) == ir_relation_less))) {
3821 ir_node *and0 = get_And_left(left);
3822 ir_node *and1 = get_And_right(left);
3823 if (and1 == right) {
3824 ir_node *tmp = and0;
3828 if (and0 == right && is_single_bit(and0)) {
3829 ir_graph *irg = get_irn_irg(n);
3831 relation == ir_relation_equal ? ir_relation_less_greater : ir_relation_equal;
3832 right = create_zero_const(irg, mode);
3837 /* replace mode_b compares with ands/ors */
3838 if (mode == mode_b) {
3839 ir_node *block = get_nodes_block(n);
3843 case ir_relation_less_equal:
3844 bres = new_r_Or(block, new_r_Not(block, left, mode_b), right, mode_b);
3846 case ir_relation_less:
3847 bres = new_r_And(block, new_r_Not(block, left, mode_b), right, mode_b);
3849 case ir_relation_greater_equal:
3850 bres = new_r_Or(block, left, new_r_Not(block, right, mode_b), mode_b);
3852 case ir_relation_greater:
3853 bres = new_r_And(block, left, new_r_Not(block, right, mode_b), mode_b);
3855 case ir_relation_less_greater:
3856 bres = new_r_Eor(block, left, right, mode_b);
3858 case ir_relation_equal:
3859 bres = new_r_Not(block, new_r_Eor(block, left, right, mode_b), mode_b);
3862 #ifdef DEBUG_libfirm
3863 ir_fprintf(stderr, "Optimisation warning, unexpected mode_b Cmp %+F\n", n);
3868 DBG_OPT_ALGSIM0(n, bres, FS_OPT_CMP_TO_BOOL);
3874 * First step: normalize the compare op
3875 * by placing the constant on the right side
3876 * or moving the lower address node to the left.
3878 if (!operands_are_normalized(left, right)) {
3883 relation = get_inversed_relation(relation);
3888 * Second step: Try to reduce the magnitude
3889 * of a constant. This may help to generate better code
3890 * later and may help to normalize more compares.
3891 * Of course this is only possible for integer values.
3893 tv = value_of(right);
3894 if (tv != tarval_bad) {
3895 ir_mode *mode = get_irn_mode(right);
3897 /* TODO extend to arbitrary constants */
3898 if (is_Conv(left) && tarval_is_null(tv)) {
3899 ir_node *op = get_Conv_op(left);
3900 ir_mode *op_mode = get_irn_mode(op);
3903 * UpConv(x) REL 0 ==> x REL 0
3904 * Don't do this for float values as it's unclear whether it is a
3905 * win. (on the other side it makes detection/creation of fabs hard)
3907 if (get_mode_size_bits(mode) > get_mode_size_bits(op_mode) &&
3908 ((relation == ir_relation_equal || relation == ir_relation_less_greater) ||
3909 mode_is_signed(mode) || !mode_is_signed(op_mode)) &&
3910 !mode_is_float(mode)) {
3911 tv = get_mode_null(op_mode);
3915 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV);
3919 if (tv != tarval_bad) {
3920 /* the following optimization is possible on modes without Overflow
3921 * on Unary Minus or on == and !=:
3922 * -a CMP c ==> a swap(CMP) -c
3924 * Beware: for two-complement Overflow may occur, so only == and != can
3925 * be optimized, see this:
3926 * -MININT < 0 =/=> MININT > 0 !!!
3928 if (is_Minus(left) &&
3929 (!mode_overflow_on_unary_Minus(mode) ||
3930 (mode_is_int(mode) && (relation == ir_relation_equal || relation == ir_relation_less_greater)))) {
3931 tv = tarval_neg(tv);
3933 if (tv != tarval_bad) {
3934 left = get_Minus_op(left);
3935 relation = get_inversed_relation(relation);
3937 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
3939 } else if (is_Not(left) && (relation == ir_relation_equal || relation == ir_relation_less_greater)) {
3940 /* Not(a) ==/!= c ==> a ==/!= Not(c) */
3941 tv = tarval_not(tv);
3943 if (tv != tarval_bad) {
3944 left = get_Not_op(left);
3946 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
3950 /* for integer modes, we have more */
3951 if (mode_is_int(mode)) {
3952 /* c > 0 : a < c ==> a <= (c-1) a >= c ==> a > (c-1) */
3953 if ((relation == ir_relation_less || relation == ir_relation_greater_equal) &&
3954 tarval_cmp(tv, get_mode_null(mode)) == ir_relation_greater) {
3955 tv = tarval_sub(tv, get_mode_one(mode), NULL);
3957 if (tv != tarval_bad) {
3958 relation ^= ir_relation_equal;
3960 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CNST_MAGN);
3963 /* c < 0 : a > c ==> a >= (c+1) a <= c ==> a < (c+1) */
3964 else if ((relation == ir_relation_greater || relation == ir_relation_less_equal) &&
3965 tarval_cmp(tv, get_mode_null(mode)) == ir_relation_less) {
3966 tv = tarval_add(tv, get_mode_one(mode));
3968 if (tv != tarval_bad) {
3969 relation ^= ir_relation_equal;
3971 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CNST_MAGN);
3975 /* the following reassociations work only for == and != */
3976 if (relation == ir_relation_equal || relation == ir_relation_less_greater) {
3977 if (tv != tarval_bad) {
3978 /* a-c1 == c2 ==> a == c2+c1, a-c1 != c2 ==> a != c2+c1 */
3980 ir_node *c1 = get_Sub_right(left);
3981 ir_tarval *tv2 = value_of(c1);
3983 if (tv2 != tarval_bad) {
3984 tv2 = tarval_add(tv, value_of(c1));
3986 if (tv2 != tarval_bad) {
3987 left = get_Sub_left(left);
3990 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
3994 /* a+c1 == c2 ==> a == c2-c1, a+c1 != c2 ==> a != c2-c1 */
3995 else if (is_Add(left)) {
3996 ir_node *a_l = get_Add_left(left);
3997 ir_node *a_r = get_Add_right(left);
4001 if (is_Const(a_l)) {
4003 tv2 = value_of(a_l);
4006 tv2 = value_of(a_r);
4009 if (tv2 != tarval_bad) {
4010 tv2 = tarval_sub(tv, tv2, NULL);
4012 if (tv2 != tarval_bad) {
4016 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4020 /* -a == c ==> a == -c, -a != c ==> a != -c */
4021 else if (is_Minus(left)) {
4022 ir_tarval *tv2 = tarval_sub(get_mode_null(mode), tv, NULL);
4024 if (tv2 != tarval_bad) {
4025 left = get_Minus_op(left);
4028 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4035 if (relation == ir_relation_equal || relation == ir_relation_less_greater) {
4036 switch (get_irn_opcode(left)) {
4040 c1 = get_And_right(left);
4043 * And(x, C1) == C2 ==> FALSE if C2 & C1 != C2
4044 * And(x, C1) != C2 ==> TRUE if C2 & C1 != C2
4046 ir_tarval *mask = tarval_and(get_Const_tarval(c1), tv);
4048 /* TODO: move to constant evaluation */
4049 ir_graph *irg = get_irn_irg(n);
4050 tv = relation == ir_relation_equal ? get_tarval_b_false() : get_tarval_b_true();
4051 c1 = new_r_Const(irg, tv);
4052 DBG_OPT_CSTEVAL(n, c1);
4056 if (tarval_is_single_bit(tv)) {
4058 * optimization for AND:
4060 * And(x, C) == C ==> And(x, C) != 0
4061 * And(x, C) != C ==> And(X, C) == 0
4063 * if C is a single Bit constant.
4066 /* check for Constant's match. We have check hare the tarvals,
4067 because our const might be changed */
4068 if (get_Const_tarval(c1) == tv) {
4069 /* fine: do the transformation */
4070 tv = get_mode_null(get_tarval_mode(tv));
4071 relation ^= ir_relation_less_equal_greater;
4073 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CNST_MAGN);
4079 c1 = get_Or_right(left);
4080 if (is_Const(c1) && tarval_is_null(tv)) {
4082 * Or(x, C) == 0 && C != 0 ==> FALSE
4083 * Or(x, C) != 0 && C != 0 ==> TRUE
4085 if (! tarval_is_null(get_Const_tarval(c1))) {
4086 /* TODO: move to constant evaluation */
4087 ir_graph *irg = get_irn_irg(n);
4088 tv = relation == ir_relation_equal ? get_tarval_b_false() : get_tarval_b_true();
4089 c1 = new_r_Const(irg, tv);
4090 DBG_OPT_CSTEVAL(n, c1);
4097 * optimize x << c1 == c into x & (-1 >>u c1) == c >> c1 if c & (-1 << c1) == c
4099 * optimize x << c1 != c into x & (-1 >>u c1) != c >> c1 if c & (-1 << c1) == c
4102 c1 = get_Shl_right(left);
4104 ir_graph *irg = get_irn_irg(c1);
4105 ir_tarval *tv1 = get_Const_tarval(c1);
4106 ir_mode *mode = get_irn_mode(left);
4107 ir_tarval *minus1 = get_mode_all_one(mode);
4108 ir_tarval *amask = tarval_shr(minus1, tv1);
4109 ir_tarval *cmask = tarval_shl(minus1, tv1);
4112 if (tarval_and(tv, cmask) != tv) {
4113 /* condition not met */
4114 tv = relation == ir_relation_equal ? get_tarval_b_false() : get_tarval_b_true();
4115 c1 = new_r_Const(irg, tv);
4116 DBG_OPT_CSTEVAL(n, c1);
4119 sl = get_Shl_left(left);
4120 blk = get_nodes_block(n);
4121 left = new_rd_And(get_irn_dbg_info(left), blk, sl, new_r_Const(irg, amask), mode);
4122 tv = tarval_shr(tv, tv1);
4124 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_SHF_TO_AND);
4129 * optimize x >>u c1 == c into x & (-1 << c1) == c << c1 if c & (-1 >>u c1) == c
4131 * optimize x >>u c1 != c into x & (-1 << c1) != c << c1 if c & (-1 >>u c1) == c
4134 c1 = get_Shr_right(left);
4136 ir_graph *irg = get_irn_irg(c1);
4137 ir_tarval *tv1 = get_Const_tarval(c1);
4138 ir_mode *mode = get_irn_mode(left);
4139 ir_tarval *minus1 = get_mode_all_one(mode);
4140 ir_tarval *amask = tarval_shl(minus1, tv1);
4141 ir_tarval *cmask = tarval_shr(minus1, tv1);
4144 if (tarval_and(tv, cmask) != tv) {
4145 /* condition not met */
4146 tv = relation == ir_relation_equal ? get_tarval_b_false() : get_tarval_b_true();
4147 c1 = new_r_Const(irg, tv);
4148 DBG_OPT_CSTEVAL(n, c1);
4151 sl = get_Shr_left(left);
4152 blk = get_nodes_block(n);
4153 left = new_rd_And(get_irn_dbg_info(left), blk, sl, new_r_Const(irg, amask), mode);
4154 tv = tarval_shl(tv, tv1);
4156 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_SHF_TO_AND);
4161 * optimize x >>s c1 == c into x & (-1 << c1) == c << c1 if (c >>s (BITS - c1)) \in {0,-1}
4163 * optimize x >>s c1 != c into x & (-1 << c1) != c << c1 if (c >>s (BITS - c1)) \in {0,-1}
4166 c1 = get_Shrs_right(left);
4168 ir_graph *irg = get_irn_irg(c1);
4169 ir_tarval *tv1 = get_Const_tarval(c1);
4170 ir_mode *mode = get_irn_mode(left);
4171 ir_tarval *minus1 = get_mode_all_one(mode);
4172 ir_tarval *amask = tarval_shl(minus1, tv1);
4173 ir_tarval *cond = new_tarval_from_long(get_mode_size_bits(mode), get_tarval_mode(tv1));
4176 cond = tarval_sub(cond, tv1, NULL);
4177 cond = tarval_shrs(tv, cond);
4179 if (!tarval_is_all_one(cond) && !tarval_is_null(cond)) {
4180 /* condition not met */
4181 tv = relation == ir_relation_equal ? get_tarval_b_false() : get_tarval_b_true();
4182 c1 = new_r_Const(irg, tv);
4183 DBG_OPT_CSTEVAL(n, c1);
4186 sl = get_Shrs_left(left);
4187 blk = get_nodes_block(n);
4188 left = new_rd_And(get_irn_dbg_info(left), blk, sl, new_r_Const(irg, amask), mode);
4189 tv = tarval_shl(tv, tv1);
4191 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_SHF_TO_AND);
4196 } /* tarval != bad */
4199 if (changedc) { /* need a new Const */
4200 ir_graph *irg = get_irn_irg(n);
4201 right = new_r_Const(irg, tv);
4205 if ((relation == ir_relation_equal || relation == ir_relation_less_greater) && is_Const(right) && is_Const_null(right) && is_Proj(left)) {
4206 ir_node *op = get_Proj_pred(left);
4208 if (is_Mod(op) && get_Proj_proj(left) == pn_Mod_res) {
4209 ir_node *c = get_binop_right(op);
4212 ir_tarval *tv = get_Const_tarval(c);
4214 if (tarval_is_single_bit(tv)) {
4215 /* special case: (x % 2^n) CMP 0 ==> x & (2^n-1) CMP 0 */
4216 ir_node *v = get_binop_left(op);
4217 ir_node *blk = get_irn_n(op, -1);
4218 ir_graph *irg = get_irn_irg(op);
4219 ir_mode *mode = get_irn_mode(v);
4221 tv = tarval_sub(tv, get_mode_one(mode), NULL);
4222 left = new_rd_And(get_irn_dbg_info(op), blk, v, new_r_Const(irg, tv), mode);
4224 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_MOD_TO_AND);
4231 dbg_info *dbgi = get_irn_dbg_info(n);
4232 ir_node *block = get_nodes_block(n);
4234 /* create a new compare */
4235 n = new_rd_Cmp(dbgi, block, left, right, relation);
4242 * Optimize CopyB(mem, x, x) into a Nop.
4244 static ir_node *transform_node_Proj_CopyB(ir_node *proj)
4246 ir_node *copyb = get_Proj_pred(proj);
4247 ir_node *a = get_CopyB_dst(copyb);
4248 ir_node *b = get_CopyB_src(copyb);
4251 switch (get_Proj_proj(proj)) {
4252 case pn_CopyB_X_regular:
4253 /* Turn CopyB into a tuple (mem, jmp, bad, bad) */
4254 DBG_OPT_EXC_REM(proj);
4255 proj = new_r_Jmp(get_nodes_block(copyb));
4257 case pn_CopyB_X_except: {
4258 ir_graph *irg = get_irn_irg(proj);
4259 DBG_OPT_EXC_REM(proj);
4260 proj = new_r_Bad(irg, mode_X);
4268 } /* transform_node_Proj_CopyB */
4271 * Optimize Bounds(idx, idx, upper) into idx.
4273 static ir_node *transform_node_Proj_Bound(ir_node *proj)
4275 ir_node *oldn = proj;
4276 ir_node *bound = get_Proj_pred(proj);
4277 ir_node *idx = get_Bound_index(bound);
4278 ir_node *pred = skip_Proj(idx);
4281 if (idx == get_Bound_lower(bound))
4283 else if (is_Bound(pred)) {
4285 * idx was Bounds checked previously, it is still valid if
4286 * lower <= pred_lower && pred_upper <= upper.
4288 ir_node *lower = get_Bound_lower(bound);
4289 ir_node *upper = get_Bound_upper(bound);
4290 if (get_Bound_lower(pred) == lower &&
4291 get_Bound_upper(pred) == upper) {
4293 * One could expect that we simply return the previous
4294 * Bound here. However, this would be wrong, as we could
4295 * add an exception Proj to a new location then.
4296 * So, we must turn in into a tuple.
4302 /* Turn Bound into a tuple (mem, jmp, bad, idx) */
4303 switch (get_Proj_proj(proj)) {
4305 DBG_OPT_EXC_REM(proj);
4306 proj = get_Bound_mem(bound);
4308 case pn_Bound_X_except:
4309 DBG_OPT_EXC_REM(proj);
4310 proj = new_r_Bad(get_irn_irg(proj), mode_X);
4314 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NOP);
4316 case pn_Bound_X_regular:
4317 DBG_OPT_EXC_REM(proj);
4318 proj = new_r_Jmp(get_nodes_block(bound));
4325 } /* transform_node_Proj_Bound */
4328 * Does all optimizations on nodes that must be done on its Projs
4329 * because of creating new nodes.
4331 static ir_node *transform_node_Proj(ir_node *proj)
4333 ir_node *n = get_Proj_pred(proj);
4335 if (n->op->ops.transform_node_Proj)
4336 return n->op->ops.transform_node_Proj(proj);
4338 } /* transform_node_Proj */
4340 static bool is_block_unreachable(const ir_node *block)
4342 const ir_graph *irg = get_irn_irg(block);
4343 if (!is_irg_state(irg, IR_GRAPH_STATE_BAD_BLOCK))
4345 return get_Block_dom_depth(block) < 0;
4348 static ir_node *transform_node_Block(ir_node *block)
4350 ir_graph *irg = get_irn_irg(block);
4351 int arity = get_irn_arity(block);
4352 ir_node *bad = NULL;
4355 if (!is_irg_state(irg, IR_GRAPH_STATE_BAD_BLOCK))
4358 for (i = 0; i < arity; ++i) {
4359 ir_node *pred = get_Block_cfgpred(block, i);
4360 ir_node *pred_block = get_nodes_block(pred);
4361 if (!is_Bad(pred) && !is_block_unreachable(pred_block))
4364 bad = new_r_Bad(irg, mode_X);
4365 set_irn_n(block, i, bad);
4371 static ir_node *transform_node_Phi(ir_node *phi)
4373 int n = get_irn_arity(phi);
4374 ir_mode *mode = get_irn_mode(phi);
4375 ir_node *block = get_nodes_block(phi);
4376 ir_graph *irg = get_irn_irg(phi);
4377 ir_node *bad = NULL;
4380 /* Set phi-operands for bad-block inputs to bad */
4381 for (i = 0; i < n; ++i) {
4382 ir_node *pred = get_Block_cfgpred(block, i);
4383 if (is_Bad(pred) || is_block_unreachable(get_nodes_block(pred))) {
4385 bad = new_r_Bad(irg, mode);
4386 set_irn_n(phi, i, bad);
4390 /* Move Confirms down through Phi nodes. */
4391 if (mode_is_reference(mode)) {
4392 n = get_irn_arity(phi);
4394 /* Beware of Phi0 */
4396 ir_node *pred = get_irn_n(phi, 0);
4397 ir_node *bound, *new_phi, *block, **in;
4398 ir_relation relation;
4400 if (! is_Confirm(pred))
4403 bound = get_Confirm_bound(pred);
4404 relation = get_Confirm_relation(pred);
4406 NEW_ARR_A(ir_node *, in, n);
4407 in[0] = get_Confirm_value(pred);
4409 for (i = 1; i < n; ++i) {
4410 pred = get_irn_n(phi, i);
4412 if (! is_Confirm(pred) ||
4413 get_Confirm_bound(pred) != bound ||
4414 get_Confirm_relation(pred) != relation)
4416 in[i] = get_Confirm_value(pred);
4418 /* move the Confirm nodes "behind" the Phi */
4419 block = get_irn_n(phi, -1);
4420 new_phi = new_r_Phi(block, n, in, get_irn_mode(phi));
4421 return new_r_Confirm(block, new_phi, bound, relation);
4428 * Returns the operands of a commutative bin-op, if one operand is
4429 * a const, it is returned as the second one.
4431 static void get_comm_Binop_Ops(ir_node *binop, ir_node **a, ir_node **c)
4433 ir_node *op_a = get_binop_left(binop);
4434 ir_node *op_b = get_binop_right(binop);
4436 assert(is_op_commutative(get_irn_op(binop)));
4438 if (is_Const(op_a)) {
4445 } /* get_comm_Binop_Ops */
4448 * Optimize a Or(And(Or(And(v,c4),c3),c2),c1) pattern if possible.
4449 * Such pattern may arise in bitfield stores.
4451 * value c4 value c4 & c2
4452 * AND c3 AND c1 | c3
4459 * AND c1 ===> OR if (c1 | c2) == 0x111..11
4462 static ir_node *transform_node_Or_bf_store(ir_node *irn_or)
4464 ir_node *irn_and, *c1;
4466 ir_node *and_l, *c3;
4467 ir_node *value, *c4;
4468 ir_node *new_and, *new_const, *block;
4469 ir_mode *mode = get_irn_mode(irn_or);
4471 ir_tarval *tv1, *tv2, *tv3, *tv4, *tv;
4475 get_comm_Binop_Ops(irn_or, &irn_and, &c1);
4476 if (!is_Const(c1) || !is_And(irn_and))
4479 get_comm_Binop_Ops(irn_and, &or_l, &c2);
4483 tv1 = get_Const_tarval(c1);
4484 tv2 = get_Const_tarval(c2);
4486 tv = tarval_or(tv1, tv2);
4487 if (tarval_is_all_one(tv)) {
4488 /* the AND does NOT clear a bit with isn't set by the OR */
4489 set_Or_left(irn_or, or_l);
4490 set_Or_right(irn_or, c1);
4492 /* check for more */
4499 get_comm_Binop_Ops(or_l, &and_l, &c3);
4500 if (!is_Const(c3) || !is_And(and_l))
4503 get_comm_Binop_Ops(and_l, &value, &c4);
4507 /* ok, found the pattern, check for conditions */
4508 assert(mode == get_irn_mode(irn_and));
4509 assert(mode == get_irn_mode(or_l));
4510 assert(mode == get_irn_mode(and_l));
4512 tv3 = get_Const_tarval(c3);
4513 tv4 = get_Const_tarval(c4);
4515 tv = tarval_or(tv4, tv2);
4516 if (!tarval_is_all_one(tv)) {
4517 /* have at least one 0 at the same bit position */
4521 if (tv3 != tarval_andnot(tv3, tv4)) {
4522 /* bit in the or_mask is outside the and_mask */
4526 if (tv1 != tarval_andnot(tv1, tv2)) {
4527 /* bit in the or_mask is outside the and_mask */
4531 /* ok, all conditions met */
4532 block = get_irn_n(irn_or, -1);
4533 irg = get_irn_irg(block);
4535 new_and = new_r_And(block, value, new_r_Const(irg, tarval_and(tv4, tv2)), mode);
4537 new_const = new_r_Const(irg, tarval_or(tv3, tv1));
4539 set_Or_left(irn_or, new_and);
4540 set_Or_right(irn_or, new_const);
4542 /* check for more */
4544 } /* transform_node_Or_bf_store */
4547 * Optimize an Or(shl(x, c), shr(x, bits - c)) into a Rotl
4549 static ir_node *transform_node_Or_Rotl(ir_node *irn_or)
4551 ir_mode *mode = get_irn_mode(irn_or);
4552 ir_node *shl, *shr, *block;
4553 ir_node *irn, *x, *c1, *c2, *n;
4554 ir_tarval *tv1, *tv2;
4556 /* some backends can't handle rotl */
4557 if (!be_get_backend_param()->support_rotl)
4560 if (! mode_is_int(mode))
4563 shl = get_binop_left(irn_or);
4564 shr = get_binop_right(irn_or);
4573 } else if (!is_Shl(shl)) {
4575 } else if (!is_Shr(shr)) {
4578 x = get_Shl_left(shl);
4579 if (x != get_Shr_left(shr))
4582 c1 = get_Shl_right(shl);
4583 c2 = get_Shr_right(shr);
4584 if (is_Const(c1) && is_Const(c2)) {
4585 tv1 = get_Const_tarval(c1);
4586 if (! tarval_is_long(tv1))
4589 tv2 = get_Const_tarval(c2);
4590 if (! tarval_is_long(tv2))
4593 if (get_tarval_long(tv1) + get_tarval_long(tv2)
4594 != (int) get_mode_size_bits(mode))
4597 /* yet, condition met */
4598 block = get_nodes_block(irn_or);
4600 n = new_r_Rotl(block, x, c1, mode);
4602 DBG_OPT_ALGSIM1(irn_or, shl, shr, n, FS_OPT_OR_SHFT_TO_ROTL);
4606 /* Note: the obvious rot formulation (a << x) | (a >> (32-x)) gets
4607 * transformed to (a << x) | (a >> -x) by transform_node_shift_modulo() */
4608 if (!ir_is_negated_value(c1, c2)) {
4612 /* yet, condition met */
4613 block = get_nodes_block(irn_or);
4614 n = new_r_Rotl(block, x, c1, mode);
4615 DBG_OPT_ALGSIM0(irn_or, n, FS_OPT_OR_SHFT_TO_ROTL);
4617 } /* transform_node_Or_Rotl */
4619 static bool is_cmp_unequal_zero(const ir_node *node)
4621 ir_relation relation = get_Cmp_relation(node);
4622 ir_node *left = get_Cmp_left(node);
4623 ir_node *right = get_Cmp_right(node);
4624 ir_mode *mode = get_irn_mode(left);
4626 if (!is_Const(right) || !is_Const_null(right))
4628 if (mode_is_signed(mode)) {
4629 return relation == ir_relation_less_greater;
4631 return relation == ir_relation_greater;
4638 static ir_node *transform_node_Or(ir_node *n)
4640 ir_node *c, *oldn = n;
4641 ir_node *a = get_Or_left(n);
4642 ir_node *b = get_Or_right(n);
4645 if (is_Not(a) && is_Not(b)) {
4646 /* ~a | ~b = ~(a&b) */
4647 ir_node *block = get_nodes_block(n);
4649 mode = get_irn_mode(n);
4652 n = new_rd_And(get_irn_dbg_info(n), block, a, b, mode);
4653 n = new_rd_Not(get_irn_dbg_info(n), block, n, mode);
4654 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_DEMORGAN);
4658 /* we can combine the relations of two compares with the same operands */
4659 if (is_Cmp(a) && is_Cmp(b)) {
4660 ir_node *a_left = get_Cmp_left(a);
4661 ir_node *a_right = get_Cmp_left(a);
4662 ir_node *b_left = get_Cmp_left(b);
4663 ir_node *b_right = get_Cmp_right(b);
4664 if (a_left == b_left && b_left == b_right) {
4665 dbg_info *dbgi = get_irn_dbg_info(n);
4666 ir_node *block = get_nodes_block(n);
4667 ir_relation a_relation = get_Cmp_relation(a);
4668 ir_relation b_relation = get_Cmp_relation(b);
4669 ir_relation new_relation = a_relation | b_relation;
4670 return new_rd_Cmp(dbgi, block, a_left, a_right, new_relation);
4672 /* Cmp(a!=0) or Cmp(b!=0) => Cmp(a|b != 0) */
4673 if (is_cmp_unequal_zero(a) && is_cmp_unequal_zero(b)
4674 && !mode_is_float(get_irn_mode(a_left))
4675 && !mode_is_float(get_irn_mode(b_left))) {
4676 ir_graph *irg = get_irn_irg(n);
4677 dbg_info *dbgi = get_irn_dbg_info(n);
4678 ir_node *block = get_nodes_block(n);
4679 ir_mode *mode = get_irn_mode(a_left);
4680 ir_node *n_b_left = get_irn_mode(b_left) != mode ?
4681 new_rd_Conv(dbgi, block, b_left, mode) : b_left;
4682 ir_node *or = new_rd_Or(dbgi, block, a_left, n_b_left, mode);
4683 ir_node *zero = create_zero_const(irg, mode);
4684 return new_rd_Cmp(dbgi, block, or, zero, ir_relation_less_greater);
4688 mode = get_irn_mode(n);
4689 HANDLE_BINOP_PHI((eval_func) tarval_or, a, b, c, mode);
4691 n = transform_node_Or_bf_store(n);
4692 n = transform_node_Or_Rotl(n);
4696 n = transform_bitwise_distributive(n, transform_node_Or);
4699 } /* transform_node_Or */
4703 static ir_node *transform_node(ir_node *n);
4706 * Optimize (a >> c1) >> c2), works for Shr, Shrs, Shl, Rotl.
4708 * Should be moved to reassociation?
4710 static ir_node *transform_node_shift(ir_node *n)
4712 ir_node *left, *right;
4714 ir_mode *count_mode;
4715 ir_tarval *tv1, *tv2, *res;
4716 ir_node *in[2], *irn, *block;
4720 left = get_binop_left(n);
4722 /* different operations */
4723 if (get_irn_op(left) != get_irn_op(n))
4726 right = get_binop_right(n);
4727 tv1 = value_of(right);
4728 if (tv1 == tarval_bad)
4731 tv2 = value_of(get_binop_right(left));
4732 if (tv2 == tarval_bad)
4735 count_mode = get_tarval_mode(tv1);
4736 if (get_tarval_mode(tv2) != count_mode) {
4737 /* TODO: search bigger mode or something and convert... */
4741 mode = get_irn_mode(n);
4742 modulo_shf = get_mode_modulo_shift(mode);
4744 if (modulo_shf > 0) {
4745 ir_tarval *modulo_mask = new_tarval_from_long(modulo_shf-1, count_mode);
4747 /* I'm not so sure what happens in one complement... */
4748 assert(get_mode_arithmetic(count_mode) == irma_twos_complement);
4749 /* modulo shifts should always be a power of 2 (otherwise modulo_mask
4750 * above will be invalid) */
4751 assert(modulo_shf<=0 || is_po2(modulo_shf));
4753 tv1 = tarval_and(tv1, modulo_mask);
4754 tv2 = tarval_and(tv2, modulo_mask);
4756 res = tarval_add(tv1, tv2);
4757 irg = get_irn_irg(n);
4759 /* beware: a simple replacement works only, if res < modulo shift */
4761 int bits = get_mode_size_bits(mode);
4762 ir_tarval *modulo = new_tarval_from_long(bits, count_mode);
4763 res = tarval_mod(res, modulo);
4765 long bits = get_mode_size_bits(mode);
4766 ir_tarval *mode_size = new_tarval_from_long(bits, count_mode);
4768 /* shifting too much */
4769 if (!(tarval_cmp(res, mode_size) & ir_relation_less)) {
4771 ir_node *block = get_nodes_block(n);
4772 dbg_info *dbgi = get_irn_dbg_info(n);
4773 ir_mode *smode = get_irn_mode(right);
4774 ir_node *cnst = new_r_Const_long(irg, smode, get_mode_size_bits(mode) - 1);
4775 return new_rd_Shrs(dbgi, block, get_binop_left(left), cnst, mode);
4778 return new_r_Const(irg, get_mode_null(mode));
4782 /* ok, we can replace it */
4783 assert(modulo_shf >= (int) get_mode_size_bits(mode));
4784 block = get_nodes_block(n);
4786 in[0] = get_binop_left(left);
4787 in[1] = new_r_Const(irg, res);
4789 irn = new_ir_node(NULL, get_Block_irg(block), block, get_irn_op(n), mode, 2, in);
4791 DBG_OPT_ALGSIM0(n, irn, FS_OPT_REASSOC_SHIFT);
4793 return transform_node(irn);
4797 * normalisation: (x & c1) >> c2 to (x >> c2) & (c1 >> c2)
4799 * - and, or, xor instead of &
4800 * - Shl, Shr, Shrs, rotl instead of >>
4801 * (with a special case for Or/Xor + Shrs)
4803 static ir_node *transform_node_bitop_shift(ir_node *n)
4806 ir_node *right = get_binop_right(n);
4807 ir_mode *mode = get_irn_mode(n);
4808 ir_node *bitop_left;
4809 ir_node *bitop_right;
4819 ir_tarval *tv_shift;
4821 assert(is_Shrs(n) || is_Shr(n) || is_Shl(n) || is_Rotl(n));
4823 if (!is_Const(right))
4826 left = get_binop_left(n);
4827 op_left = get_irn_op(left);
4828 if (op_left != op_And && op_left != op_Or && op_left != op_Eor)
4831 /* doing it with Shrs is not legal if the Or/Eor affects the topmost bit */
4832 if (is_Shrs(n) && (op_left == op_Or || op_left == op_Eor)) {
4833 /* TODO: test if sign bit is affectes */
4837 bitop_right = get_binop_right(left);
4838 if (!is_Const(bitop_right))
4841 bitop_left = get_binop_left(left);
4843 block = get_nodes_block(n);
4844 dbgi = get_irn_dbg_info(n);
4845 tv1 = get_Const_tarval(bitop_right);
4846 tv2 = get_Const_tarval(right);
4848 assert(get_tarval_mode(tv1) == mode);
4851 new_shift = new_rd_Shl(dbgi, block, bitop_left, right, mode);
4852 tv_shift = tarval_shl(tv1, tv2);
4853 } else if (is_Shr(n)) {
4854 new_shift = new_rd_Shr(dbgi, block, bitop_left, right, mode);
4855 tv_shift = tarval_shr(tv1, tv2);
4856 } else if (is_Shrs(n)) {
4857 new_shift = new_rd_Shrs(dbgi, block, bitop_left, right, mode);
4858 tv_shift = tarval_shrs(tv1, tv2);
4861 new_shift = new_rd_Rotl(dbgi, block, bitop_left, right, mode);
4862 tv_shift = tarval_rotl(tv1, tv2);
4865 assert(get_tarval_mode(tv_shift) == mode);
4866 irg = get_irn_irg(n);
4867 new_const = new_r_Const(irg, tv_shift);
4869 if (op_left == op_And) {
4870 new_bitop = new_rd_And(dbgi, block, new_shift, new_const, mode);
4871 } else if (op_left == op_Or) {
4872 new_bitop = new_rd_Or(dbgi, block, new_shift, new_const, mode);
4874 assert(op_left == op_Eor);
4875 new_bitop = new_rd_Eor(dbgi, block, new_shift, new_const, mode);
4883 * (x << c1) >> c2 <=> x OP (c2-c1) & ((-1 << c1) >> c2)
4885 * (x >> c1) << c2 <=> x OP (c2-c1) & ((-1 >> c1) << c2)
4886 * (also with x >>s c1 when c1>=c2)
4888 static ir_node *transform_node_shl_shr(ir_node *n)
4891 ir_node *right = get_binop_right(n);
4901 ir_tarval *tv_shift;
4904 ir_relation relation;
4907 assert(is_Shl(n) || is_Shr(n) || is_Shrs(n));
4909 if (!is_Const(right))
4912 left = get_binop_left(n);
4913 mode = get_irn_mode(n);
4914 if (is_Shl(n) && (is_Shr(left) || is_Shrs(left))) {
4915 ir_node *shr_right = get_binop_right(left);
4917 if (!is_Const(shr_right))
4920 x = get_binop_left(left);
4921 tv_shr = get_Const_tarval(shr_right);
4922 tv_shl = get_Const_tarval(right);
4924 if (is_Shrs(left)) {
4925 /* shrs variant only allowed if c1 >= c2 */
4926 if (! (tarval_cmp(tv_shl, tv_shr) & ir_relation_greater_equal))
4929 tv_mask = tarval_shrs(get_mode_all_one(mode), tv_shr);
4932 tv_mask = tarval_shr(get_mode_all_one(mode), tv_shr);
4934 tv_mask = tarval_shl(tv_mask, tv_shl);
4935 } else if (is_Shr(n) && is_Shl(left)) {
4936 ir_node *shl_right = get_Shl_right(left);
4938 if (!is_Const(shl_right))
4941 x = get_Shl_left(left);
4942 tv_shr = get_Const_tarval(right);
4943 tv_shl = get_Const_tarval(shl_right);
4945 tv_mask = tarval_shl(get_mode_all_one(mode), tv_shl);
4946 tv_mask = tarval_shr(tv_mask, tv_shr);
4951 if (get_tarval_mode(tv_shl) != get_tarval_mode(tv_shr)) {
4952 tv_shl = tarval_convert_to(tv_shl, get_tarval_mode(tv_shr));
4955 assert(tv_mask != tarval_bad);
4956 assert(get_tarval_mode(tv_mask) == mode);
4958 block = get_nodes_block(n);
4959 irg = get_irn_irg(block);
4960 dbgi = get_irn_dbg_info(n);
4962 relation = tarval_cmp(tv_shl, tv_shr);
4963 if (relation == ir_relation_less || relation == ir_relation_equal) {
4964 tv_shift = tarval_sub(tv_shr, tv_shl, NULL);
4965 new_const = new_r_Const(irg, tv_shift);
4967 new_shift = new_rd_Shrs(dbgi, block, x, new_const, mode);
4969 new_shift = new_rd_Shr(dbgi, block, x, new_const, mode);
4972 assert(relation == ir_relation_greater);
4973 tv_shift = tarval_sub(tv_shl, tv_shr, NULL);
4974 new_const = new_r_Const(irg, tv_shift);
4975 new_shift = new_rd_Shl(dbgi, block, x, new_const, mode);
4978 new_const = new_r_Const(irg, tv_mask);
4979 new_and = new_rd_And(dbgi, block, new_shift, new_const, mode);
4984 static ir_tarval *get_modulo_tv_value(ir_tarval *tv, int modulo_val)
4986 ir_mode *mode = get_tarval_mode(tv);
4987 ir_tarval *modulo_tv = new_tarval_from_long(modulo_val, mode);
4988 return tarval_mod(tv, modulo_tv);
4991 typedef ir_node*(*new_shift_func)(dbg_info *dbgi, ir_node *block,
4992 ir_node *left, ir_node *right, ir_mode *mode);
4995 * Normalisation: if we have a shl/shr with modulo_shift behaviour
4996 * then we can use that to minimize the value of Add(x, const) or
4997 * Sub(Const, x). In particular this often avoids 1 instruction in some
4998 * backends for the Shift(x, Sub(Const, y)) case because it can be replaced
4999 * by Shift(x, Minus(y)) which doesnt't need an explicit Const constructed.
5001 static ir_node *transform_node_shift_modulo(ir_node *n,
5002 new_shift_func new_shift)
5004 ir_mode *mode = get_irn_mode(n);
5005 int modulo = get_mode_modulo_shift(mode);
5006 ir_node *newop = NULL;
5007 ir_mode *mode_right;
5014 if (get_mode_arithmetic(mode) != irma_twos_complement)
5016 if (!is_po2(modulo))
5019 irg = get_irn_irg(n);
5020 block = get_nodes_block(n);
5021 right = get_binop_right(n);
5022 mode_right = get_irn_mode(right);
5023 if (is_Const(right)) {
5024 ir_tarval *tv = get_Const_tarval(right);
5025 ir_tarval *tv_mod = get_modulo_tv_value(tv, modulo);
5030 newop = new_r_Const(irg, tv_mod);
5031 } else if (is_Add(right)) {
5032 ir_node *add_right = get_Add_right(right);
5033 if (is_Const(add_right)) {
5034 ir_tarval *tv = get_Const_tarval(add_right);
5035 ir_tarval *tv_mod = get_modulo_tv_value(tv, modulo);
5040 newconst = new_r_Const(irg, tv_mod);
5041 newop = new_r_Add(block, get_Add_left(right), newconst,
5044 } else if (is_Sub(right)) {
5045 ir_node *sub_left = get_Sub_left(right);
5046 if (is_Const(sub_left)) {
5047 ir_tarval *tv = get_Const_tarval(sub_left);
5048 ir_tarval *tv_mod = get_modulo_tv_value(tv, modulo);
5053 newconst = new_r_Const(irg, tv_mod);
5054 newop = new_r_Sub(block, newconst, get_Sub_right(right),
5061 if (newop != NULL) {
5062 dbg_info *dbgi = get_irn_dbg_info(n);
5063 ir_node *left = get_binop_left(n);
5064 return new_shift(dbgi, block, left, newop, mode);
5072 static ir_node *transform_node_Shr(ir_node *n)
5074 ir_node *c, *oldn = n;
5075 ir_node *left = get_Shr_left(n);
5076 ir_node *right = get_Shr_right(n);
5077 ir_mode *mode = get_irn_mode(n);
5079 HANDLE_BINOP_PHI((eval_func) tarval_shr, left, right, c, mode);
5080 n = transform_node_shift(n);
5083 n = transform_node_shift_modulo(n, new_rd_Shr);
5085 n = transform_node_shl_shr(n);
5087 n = transform_node_bitop_shift(n);
5090 } /* transform_node_Shr */
5095 static ir_node *transform_node_Shrs(ir_node *n)
5097 ir_node *c, *oldn = n;
5098 ir_node *a = get_Shrs_left(n);
5099 ir_node *b = get_Shrs_right(n);
5100 ir_mode *mode = get_irn_mode(n);
5102 if (is_oversize_shift(n)) {
5103 ir_node *block = get_nodes_block(n);
5104 dbg_info *dbgi = get_irn_dbg_info(n);
5105 ir_mode *cmode = get_irn_mode(b);
5106 long val = get_mode_size_bits(cmode)-1;
5107 ir_graph *irg = get_irn_irg(n);
5108 ir_node *cnst = new_r_Const_long(irg, cmode, val);
5109 return new_rd_Shrs(dbgi, block, a, cnst, mode);
5112 HANDLE_BINOP_PHI((eval_func) tarval_shrs, a, b, c, mode);
5113 n = transform_node_shift(n);
5116 n = transform_node_shift_modulo(n, new_rd_Shrs);
5118 n = transform_node_bitop_shift(n);
5121 } /* transform_node_Shrs */
5126 static ir_node *transform_node_Shl(ir_node *n)
5128 ir_node *c, *oldn = n;
5129 ir_node *a = get_Shl_left(n);
5130 ir_node *b = get_Shl_right(n);
5131 ir_mode *mode = get_irn_mode(n);
5133 HANDLE_BINOP_PHI((eval_func) tarval_shl, a, b, c, mode);
5134 n = transform_node_shift(n);
5137 n = transform_node_shift_modulo(n, new_rd_Shl);
5139 n = transform_node_shl_shr(n);
5141 n = transform_node_bitop_shift(n);
5144 } /* transform_node_Shl */
5149 static ir_node *transform_node_Rotl(ir_node *n)
5151 ir_node *c, *oldn = n;
5152 ir_node *a = get_Rotl_left(n);
5153 ir_node *b = get_Rotl_right(n);
5154 ir_mode *mode = get_irn_mode(n);
5156 HANDLE_BINOP_PHI((eval_func) tarval_rotl, a, b, c, mode);
5157 n = transform_node_shift(n);
5160 n = transform_node_bitop_shift(n);
5163 } /* transform_node_Rotl */
5168 static ir_node *transform_node_Conv(ir_node *n)
5170 ir_node *c, *oldn = n;
5171 ir_mode *mode = get_irn_mode(n);
5172 ir_node *a = get_Conv_op(n);
5174 if (mode != mode_b && is_const_Phi(a)) {
5175 /* Do NOT optimize mode_b Conv's, this leads to remaining
5176 * Phib nodes later, because the conv_b_lower operation
5177 * is instantly reverted, when it tries to insert a Convb.
5179 c = apply_conv_on_phi(a, mode);
5181 DBG_OPT_ALGSIM0(oldn, c, FS_OPT_CONST_PHI);
5186 if (is_Unknown(a)) { /* Conv_A(Unknown_B) -> Unknown_A */
5187 ir_graph *irg = get_irn_irg(n);
5188 return new_r_Unknown(irg, mode);
5191 if (mode_is_reference(mode) &&
5192 get_mode_size_bits(mode) == get_mode_size_bits(get_irn_mode(a)) &&
5194 ir_node *l = get_Add_left(a);
5195 ir_node *r = get_Add_right(a);
5196 dbg_info *dbgi = get_irn_dbg_info(a);
5197 ir_node *block = get_nodes_block(n);
5199 ir_node *lop = get_Conv_op(l);
5200 if (get_irn_mode(lop) == mode) {
5201 /* ConvP(AddI(ConvI(P), x)) -> AddP(P, x) */
5202 n = new_rd_Add(dbgi, block, lop, r, mode);
5207 ir_node *rop = get_Conv_op(r);
5208 if (get_irn_mode(rop) == mode) {
5209 /* ConvP(AddI(x, ConvI(P))) -> AddP(x, P) */
5210 n = new_rd_Add(dbgi, block, l, rop, mode);
5217 } /* transform_node_Conv */
5220 * Remove dead blocks and nodes in dead blocks
5221 * in keep alive list. We do not generate a new End node.
5223 static ir_node *transform_node_End(ir_node *n)
5225 int i, j, n_keepalives = get_End_n_keepalives(n);
5228 NEW_ARR_A(ir_node *, in, n_keepalives);
5230 for (i = j = 0; i < n_keepalives; ++i) {
5231 ir_node *ka = get_End_keepalive(n, i);
5233 /* no need to keep Bad */
5236 /* dont keep unreachable code */
5237 block = is_Block(ka) ? ka : get_nodes_block(ka);
5238 if (is_block_unreachable(block))
5242 if (j != n_keepalives)
5243 set_End_keepalives(n, j, in);
5245 } /* transform_node_End */
5247 int ir_is_negated_value(const ir_node *a, const ir_node *b)
5249 if (is_Minus(a) && get_Minus_op(a) == b)
5251 if (is_Minus(b) && get_Minus_op(b) == a)
5253 if (is_Sub(a) && is_Sub(b)) {
5254 ir_node *a_left = get_Sub_left(a);
5255 ir_node *a_right = get_Sub_right(a);
5256 ir_node *b_left = get_Sub_left(b);
5257 ir_node *b_right = get_Sub_right(b);
5259 if (a_left == b_right && a_right == b_left)
5267 * Optimize a Mux into some simpler cases.
5269 static ir_node *transform_node_Mux(ir_node *n)
5271 ir_node *oldn = n, *sel = get_Mux_sel(n);
5272 ir_mode *mode = get_irn_mode(n);
5273 ir_node *t = get_Mux_true(n);
5274 ir_node *f = get_Mux_false(n);
5275 ir_graph *irg = get_irn_irg(n);
5277 if (is_irg_state(irg, IR_GRAPH_STATE_KEEP_MUX))
5281 ir_node* block = get_nodes_block(n);
5283 ir_node* c1 = get_Mux_sel(t);
5284 ir_node* t1 = get_Mux_true(t);
5285 ir_node* f1 = get_Mux_false(t);
5287 /* Mux(cond0, Mux(cond1, x, y), y) -> typical if (cond0 && cond1) x else y */
5288 ir_node* and_ = new_r_And(block, c0, c1, mode_b);
5289 ir_node* new_mux = new_r_Mux(block, and_, f1, t1, mode);
5294 DBG_OPT_ALGSIM0(oldn, t, FS_OPT_MUX_COMBINE);
5295 } else if (f == t1) {
5296 /* Mux(cond0, Mux(cond1, x, y), x) */
5297 ir_node* not_c1 = new_r_Not(block, c1, mode_b);
5298 ir_node* and_ = new_r_And(block, c0, not_c1, mode_b);
5299 ir_node* new_mux = new_r_Mux(block, and_, t1, f1, mode);
5304 DBG_OPT_ALGSIM0(oldn, t, FS_OPT_MUX_COMBINE);
5306 } else if (is_Mux(f)) {
5307 ir_node* block = get_nodes_block(n);
5309 ir_node* c1 = get_Mux_sel(f);
5310 ir_node* t1 = get_Mux_true(f);
5311 ir_node* f1 = get_Mux_false(f);
5313 /* Mux(cond0, x, Mux(cond1, x, y)) -> typical if (cond0 || cond1) x else y */
5314 ir_node* or_ = new_r_Or(block, c0, c1, mode_b);
5315 ir_node* new_mux = new_r_Mux(block, or_, f1, t1, mode);
5320 DBG_OPT_ALGSIM0(oldn, f, FS_OPT_MUX_COMBINE);
5321 } else if (t == f1) {
5322 /* Mux(cond0, x, Mux(cond1, y, x)) */
5323 ir_node* not_c1 = new_r_Not(block, c1, mode_b);
5324 ir_node* or_ = new_r_Or(block, c0, not_c1, mode_b);
5325 ir_node* new_mux = new_r_Mux(block, or_, t1, f1, mode);
5330 DBG_OPT_ALGSIM0(oldn, f, FS_OPT_MUX_COMBINE);
5334 /* first normalization step: try to move a constant to the false side,
5335 * 0 preferred on false side too */
5336 if (is_Cmp(sel) && is_Const(t) &&
5337 (!is_Const(f) || (is_Const_null(t) && !is_Const_null(f)))) {
5338 dbg_info *seldbgi = get_irn_dbg_info(sel);
5339 ir_node *block = get_nodes_block(sel);
5340 ir_relation relation = get_Cmp_relation(sel);
5345 /* Mux(x, a, b) => Mux(not(x), b, a) */
5346 relation = get_negated_relation(relation);
5347 sel = new_rd_Cmp(seldbgi, block, get_Cmp_left(sel),
5348 get_Cmp_right(sel), relation);
5349 n = new_rd_Mux(get_irn_dbg_info(n), get_nodes_block(n), sel, f, t, mode);
5352 /* note: after normalization, false can only happen on default */
5353 if (mode == mode_b) {
5354 dbg_info *dbg = get_irn_dbg_info(n);
5355 ir_node *block = get_nodes_block(n);
5358 ir_tarval *tv_t = get_Const_tarval(t);
5359 if (tv_t == tarval_b_true) {
5361 /* Muxb(sel, true, false) = sel */
5362 assert(get_Const_tarval(f) == tarval_b_false);
5363 DBG_OPT_ALGSIM0(oldn, sel, FS_OPT_MUX_BOOL);
5366 /* Muxb(sel, true, x) = Or(sel, x) */
5367 n = new_rd_Or(dbg, block, sel, f, mode_b);
5368 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_OR_BOOL);
5372 } else if (is_Const(f)) {
5373 ir_tarval *tv_f = get_Const_tarval(f);
5374 if (tv_f == tarval_b_true) {
5375 /* Muxb(sel, x, true) = Or(Not(sel), x) */
5376 ir_node* not_sel = new_rd_Not(dbg, block, sel, mode_b);
5377 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_ORNOT_BOOL);
5378 n = new_rd_Or(dbg, block, not_sel, t, mode_b);
5381 /* Muxb(sel, x, false) = And(sel, x) */
5382 assert(tv_f == tarval_b_false);
5383 n = new_rd_And(dbg, block, sel, t, mode_b);
5384 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_AND_BOOL);
5390 /* more normalization: Mux(sel, 0, 1) is simply a conv from the mode_b
5391 * value to integer. */
5392 if (is_Const(t) && is_Const(f) && mode_is_int(mode)) {
5393 ir_tarval *a = get_Const_tarval(t);
5394 ir_tarval *b = get_Const_tarval(f);
5396 if (tarval_is_one(a) && tarval_is_null(b)) {
5397 ir_node *block = get_nodes_block(n);
5398 ir_node *conv = new_r_Conv(block, sel, mode);
5400 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_CONV);
5402 } else if (tarval_is_null(a) && tarval_is_one(b)) {
5403 ir_node *block = get_nodes_block(n);
5404 ir_node *not_ = new_r_Not(block, sel, mode_b);
5405 ir_node *conv = new_r_Conv(block, not_, mode);
5407 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_CONV);
5413 ir_node *cmp_r = get_Cmp_right(sel);
5414 if (is_Const(cmp_r) && is_Const_null(cmp_r)) {
5415 ir_node *block = get_nodes_block(n);
5416 ir_node *cmp_l = get_Cmp_left(sel);
5418 if (mode_is_int(mode)) {
5419 ir_relation relation = get_Cmp_relation(sel);
5421 if ((relation == ir_relation_less_greater || relation == ir_relation_equal) && is_And(cmp_l)) {
5422 /* Mux((a & b) != 0, c, 0) */
5423 ir_node *and_r = get_And_right(cmp_l);
5426 if (and_r == t && f == cmp_r) {
5427 if (is_Const(t) && tarval_is_single_bit(get_Const_tarval(t))) {
5428 if (relation == ir_relation_less_greater) {
5429 /* Mux((a & 2^C) != 0, 2^C, 0) */
5431 DBG_OPT_ALGSIM1(oldn, sel, sel, n, FS_OPT_MUX_TO_BITOP);
5433 /* Mux((a & 2^C) == 0, 2^C, 0) */
5434 n = new_rd_Eor(get_irn_dbg_info(n),
5435 block, cmp_l, t, mode);
5436 DBG_OPT_ALGSIM1(oldn, sel, sel, n, FS_OPT_MUX_TO_BITOP);
5441 if (is_Shl(and_r)) {
5442 ir_node *shl_l = get_Shl_left(and_r);
5443 if (is_Const(shl_l) && is_Const_one(shl_l)) {
5444 if (and_r == t && f == cmp_r) {
5445 if (relation == ir_relation_less_greater) {
5446 /* (a & (1 << n)) != 0, (1 << n), 0) */
5448 DBG_OPT_ALGSIM1(oldn, sel, sel, n, FS_OPT_MUX_TO_BITOP);
5450 /* (a & (1 << n)) == 0, (1 << n), 0) */
5451 n = new_rd_Eor(get_irn_dbg_info(n),
5452 block, cmp_l, t, mode);
5453 DBG_OPT_ALGSIM1(oldn, sel, sel, n, FS_OPT_MUX_TO_BITOP);
5459 and_l = get_And_left(cmp_l);
5460 if (is_Shl(and_l)) {
5461 ir_node *shl_l = get_Shl_left(and_l);
5462 if (is_Const(shl_l) && is_Const_one(shl_l)) {
5463 if (and_l == t && f == cmp_r) {
5464 if (relation == ir_relation_less_greater) {
5465 /* ((1 << n) & a) != 0, (1 << n), 0) */
5467 DBG_OPT_ALGSIM1(oldn, sel, sel, n, FS_OPT_MUX_TO_BITOP);
5469 /* ((1 << n) & a) == 0, (1 << n), 0) */
5470 n = new_rd_Eor(get_irn_dbg_info(n),
5471 block, cmp_l, t, mode);
5472 DBG_OPT_ALGSIM1(oldn, sel, sel, n, FS_OPT_MUX_TO_BITOP);
5487 * optimize Sync nodes that have other syncs as input we simply add the inputs
5488 * of the other sync to our own inputs
5490 static ir_node *transform_node_Sync(ir_node *n)
5492 int arity = get_Sync_n_preds(n);
5495 for (i = 0; i < arity;) {
5496 ir_node *pred = get_Sync_pred(n, i);
5500 /* Remove Bad predecessors */
5507 /* Remove duplicate predecessors */
5508 for (j = 0; j < i; ++j) {
5509 if (get_Sync_pred(n, j) == pred) {
5518 if (!is_Sync(pred)) {
5526 pred_arity = get_Sync_n_preds(pred);
5527 for (j = 0; j < pred_arity; ++j) {
5528 ir_node *pred_pred = get_Sync_pred(pred, j);
5533 add_irn_n(n, pred_pred);
5537 if (get_Sync_pred(n, k) == pred_pred) break;
5543 ir_graph *irg = get_irn_irg(n);
5544 return new_r_Bad(irg, mode_M);
5547 return get_Sync_pred(n, 0);
5550 /* rehash the sync node */
5555 static ir_node *transform_node_Load(ir_node *n)
5557 /* if our memory predecessor is a load from the same address, then reuse the
5558 * previous result */
5559 ir_node *mem = get_Load_mem(n);
5564 /* don't touch volatile loads */
5565 if (get_Load_volatility(n) == volatility_is_volatile)
5567 mem_pred = get_Proj_pred(mem);
5568 if (is_Load(mem_pred)) {
5569 ir_node *pred_load = mem_pred;
5571 /* conservatively compare the 2 loads. TODO: This could be less strict
5572 * with fixup code in some situations (like smaller/bigger modes) */
5573 if (get_Load_ptr(pred_load) != get_Load_ptr(n))
5575 if (get_Load_mode(pred_load) != get_Load_mode(n))
5577 /* all combinations of aligned/unaligned pred/n should be fine so we do
5578 * not compare the unaligned attribute */
5580 ir_node *block = get_nodes_block(n);
5581 ir_node *jmp = new_r_Jmp(block);
5582 ir_graph *irg = get_irn_irg(n);
5583 ir_node *bad = new_r_Bad(irg, mode_X);
5584 ir_mode *mode = get_Load_mode(n);
5585 ir_node *res = new_r_Proj(pred_load, mode, pn_Load_res);
5586 ir_node *in[pn_Load_max] = { mem, jmp, bad, res };
5587 ir_node *tuple = new_r_Tuple(block, ARRAY_SIZE(in), in);
5590 } else if (is_Store(mem_pred)) {
5591 ir_node *pred_store = mem_pred;
5592 ir_node *value = get_Store_value(pred_store);
5594 if (get_Store_ptr(pred_store) != get_Load_ptr(n))
5596 if (get_irn_mode(value) != get_Load_mode(n))
5598 /* all combinations of aligned/unaligned pred/n should be fine so we do
5599 * not compare the unaligned attribute */
5601 ir_node *block = get_nodes_block(n);
5602 ir_node *jmp = new_r_Jmp(block);
5603 ir_graph *irg = get_irn_irg(n);
5604 ir_node *bad = new_r_Bad(irg, mode_X);
5605 ir_node *res = value;
5606 ir_node *in[pn_Load_max] = { mem, jmp, bad, res };
5607 ir_node *tuple = new_r_Tuple(block, ARRAY_SIZE(in), in);
5616 * optimize a trampoline Call into a direct Call
5618 static ir_node *transform_node_Call(ir_node *call)
5620 ir_node *callee = get_Call_ptr(call);
5621 ir_node *adr, *mem, *res, *bl, **in;
5622 ir_type *ctp, *mtp, *tp;
5626 size_t i, n_res, n_param;
5629 if (! is_Proj(callee))
5631 callee = get_Proj_pred(callee);
5632 if (! is_Builtin(callee))
5634 if (get_Builtin_kind(callee) != ir_bk_inner_trampoline)
5637 mem = get_Call_mem(call);
5639 if (skip_Proj(mem) == callee) {
5640 /* memory is routed to the trampoline, skip */
5641 mem = get_Builtin_mem(callee);
5644 /* build a new call type */
5645 mtp = get_Call_type(call);
5646 tdb = get_type_dbg_info(mtp);
5648 n_res = get_method_n_ress(mtp);
5649 n_param = get_method_n_params(mtp);
5650 ctp = new_d_type_method(n_param + 1, n_res, tdb);
5652 for (i = 0; i < n_res; ++i)
5653 set_method_res_type(ctp, i, get_method_res_type(mtp, i));
5655 NEW_ARR_A(ir_node *, in, n_param + 1);
5657 /* FIXME: we don't need a new pointer type in every step */
5658 irg = get_irn_irg(call);
5659 tp = get_irg_frame_type(irg);
5660 tp = new_type_pointer(tp);
5661 set_method_param_type(ctp, 0, tp);
5663 in[0] = get_Builtin_param(callee, 2);
5664 for (i = 0; i < n_param; ++i) {
5665 set_method_param_type(ctp, i + 1, get_method_param_type(mtp, i));
5666 in[i + 1] = get_Call_param(call, i);
5668 var = get_method_variadicity(mtp);
5669 set_method_variadicity(ctp, var);
5670 /* When we resolve a trampoline, the function must be called by a this-call */
5671 set_method_calling_convention(ctp, get_method_calling_convention(mtp) | cc_this_call);
5672 set_method_additional_properties(ctp, get_method_additional_properties(mtp));
5674 adr = get_Builtin_param(callee, 1);
5676 db = get_irn_dbg_info(call);
5677 bl = get_nodes_block(call);
5679 res = new_rd_Call(db, bl, mem, adr, n_param + 1, in, ctp);
5680 if (get_irn_pinned(call) == op_pin_state_floats)
5681 set_irn_pinned(res, op_pin_state_floats);
5683 } /* transform_node_Call */
5686 * Tries several [inplace] [optimizing] transformations and returns an
5687 * equivalent node. The difference to equivalent_node() is that these
5688 * transformations _do_ generate new nodes, and thus the old node must
5689 * not be freed even if the equivalent node isn't the old one.
5691 static ir_node *transform_node(ir_node *n)
5696 * Transform_node is the only "optimizing transformation" that might
5697 * return a node with a different opcode. We iterate HERE until fixpoint
5698 * to get the final result.
5702 if (n->op->ops.transform_node != NULL)
5703 n = n->op->ops.transform_node(n);
5704 } while (oldn != n);
5707 } /* transform_node */
5710 * Sets the default transform node operation for an ir_op_ops.
5712 * @param code the opcode for the default operation
5713 * @param ops the operations initialized
5718 static ir_op_ops *firm_set_default_transform_node(ir_opcode code, ir_op_ops *ops)
5722 ops->transform_node = transform_node_##a; \
5724 #define CASE_PROJ(a) \
5726 ops->transform_node_Proj = transform_node_Proj_##a; \
5728 #define CASE_PROJ_EX(a) \
5730 ops->transform_node = transform_node_##a; \
5731 ops->transform_node_Proj = transform_node_Proj_##a; \
5772 } /* firm_set_default_transform_node */
5775 /* **************** Common Subexpression Elimination **************** */
5777 /** The size of the hash table used, should estimate the number of nodes
5779 #define N_IR_NODES 512
5781 /** Compares the attributes of two Const nodes. */
5782 static int node_cmp_attr_Const(const ir_node *a, const ir_node *b)
5784 return get_Const_tarval(a) != get_Const_tarval(b);
5787 /** Compares the attributes of two Proj nodes. */
5788 static int node_cmp_attr_Proj(const ir_node *a, const ir_node *b)
5790 return a->attr.proj.proj != b->attr.proj.proj;
5793 /** Compares the attributes of two Alloc nodes. */
5794 static int node_cmp_attr_Alloc(const ir_node *a, const ir_node *b)
5796 const alloc_attr *pa = &a->attr.alloc;
5797 const alloc_attr *pb = &b->attr.alloc;
5798 return (pa->where != pb->where) || (pa->type != pb->type);
5801 /** Compares the attributes of two Free nodes. */
5802 static int node_cmp_attr_Free(const ir_node *a, const ir_node *b)
5804 const free_attr *pa = &a->attr.free;
5805 const free_attr *pb = &b->attr.free;
5806 return (pa->where != pb->where) || (pa->type != pb->type);
5809 /** Compares the attributes of two SymConst nodes. */
5810 static int node_cmp_attr_SymConst(const ir_node *a, const ir_node *b)
5812 const symconst_attr *pa = &a->attr.symc;
5813 const symconst_attr *pb = &b->attr.symc;
5814 return (pa->kind != pb->kind)
5815 || (pa->sym.type_p != pb->sym.type_p);
5818 /** Compares the attributes of two Call nodes. */
5819 static int node_cmp_attr_Call(const ir_node *a, const ir_node *b)
5821 const call_attr *pa = &a->attr.call;
5822 const call_attr *pb = &b->attr.call;
5823 return (pa->type != pb->type)
5824 || (pa->tail_call != pb->tail_call);
5827 /** Compares the attributes of two Sel nodes. */
5828 static int node_cmp_attr_Sel(const ir_node *a, const ir_node *b)
5830 const ir_entity *a_ent = get_Sel_entity(a);
5831 const ir_entity *b_ent = get_Sel_entity(b);
5832 return a_ent != b_ent;
5835 /** Compares the attributes of two Phi nodes. */
5836 static int node_cmp_attr_Phi(const ir_node *a, const ir_node *b)
5838 /* we can only enter this function if both nodes have the same number of inputs,
5839 hence it is enough to check if one of them is a Phi0 */
5841 /* check the Phi0 pos attribute */
5842 return a->attr.phi.u.pos != b->attr.phi.u.pos;
5847 /** Compares the attributes of two Conv nodes. */
5848 static int node_cmp_attr_Conv(const ir_node *a, const ir_node *b)
5850 return get_Conv_strict(a) != get_Conv_strict(b);
5853 /** Compares the attributes of two Cast nodes. */
5854 static int node_cmp_attr_Cast(const ir_node *a, const ir_node *b)
5856 return get_Cast_type(a) != get_Cast_type(b);
5859 /** Compares the attributes of two Load nodes. */
5860 static int node_cmp_attr_Load(const ir_node *a, const ir_node *b)
5862 if (get_Load_volatility(a) == volatility_is_volatile ||
5863 get_Load_volatility(b) == volatility_is_volatile)
5864 /* NEVER do CSE on volatile Loads */
5866 /* do not CSE Loads with different alignment. Be conservative. */
5867 if (get_Load_unaligned(a) != get_Load_unaligned(b))
5870 return get_Load_mode(a) != get_Load_mode(b);
5873 /** Compares the attributes of two Store nodes. */
5874 static int node_cmp_attr_Store(const ir_node *a, const ir_node *b)
5876 /* do not CSE Stores with different alignment. Be conservative. */
5877 if (get_Store_unaligned(a) != get_Store_unaligned(b))
5880 /* NEVER do CSE on volatile Stores */
5881 return (get_Store_volatility(a) == volatility_is_volatile ||
5882 get_Store_volatility(b) == volatility_is_volatile);
5885 /** Compares two exception attributes */
5886 static int node_cmp_exception(const ir_node *a, const ir_node *b)
5888 const except_attr *ea = &a->attr.except;
5889 const except_attr *eb = &b->attr.except;
5891 return ea->pin_state != eb->pin_state;
5894 #define node_cmp_attr_Bound node_cmp_exception
5896 /** Compares the attributes of two Div nodes. */
5897 static int node_cmp_attr_Div(const ir_node *a, const ir_node *b)
5899 const div_attr *ma = &a->attr.div;
5900 const div_attr *mb = &b->attr.div;
5901 return ma->exc.pin_state != mb->exc.pin_state ||
5902 ma->resmode != mb->resmode ||
5903 ma->no_remainder != mb->no_remainder;
5906 /** Compares the attributes of two Mod nodes. */
5907 static int node_cmp_attr_Mod(const ir_node *a, const ir_node *b)
5909 const mod_attr *ma = &a->attr.mod;
5910 const mod_attr *mb = &b->attr.mod;
5911 return ma->exc.pin_state != mb->exc.pin_state ||
5912 ma->resmode != mb->resmode;
5915 static int node_cmp_attr_Cmp(const ir_node *a, const ir_node *b)
5917 const cmp_attr *ma = &a->attr.cmp;
5918 const cmp_attr *mb = &b->attr.cmp;
5919 return ma->relation != mb->relation;
5922 /** Compares the attributes of two Confirm nodes. */
5923 static int node_cmp_attr_Confirm(const ir_node *a, const ir_node *b)
5925 const confirm_attr *ma = &a->attr.confirm;
5926 const confirm_attr *mb = &b->attr.confirm;
5927 return ma->relation != mb->relation;
5930 /** Compares the attributes of two Builtin nodes. */
5931 static int node_cmp_attr_Builtin(const ir_node *a, const ir_node *b)
5933 /* no need to compare the type, equal kind means equal type */
5934 return get_Builtin_kind(a) != get_Builtin_kind(b);
5937 /** Compares the attributes of two ASM nodes. */
5938 static int node_cmp_attr_ASM(const ir_node *a, const ir_node *b)
5941 const ir_asm_constraint *ca;
5942 const ir_asm_constraint *cb;
5945 if (get_ASM_text(a) != get_ASM_text(b))
5948 /* Should we really check the constraints here? Should be better, but is strange. */
5949 n = get_ASM_n_input_constraints(a);
5950 if (n != get_ASM_n_input_constraints(b))
5953 ca = get_ASM_input_constraints(a);
5954 cb = get_ASM_input_constraints(b);
5955 for (i = 0; i < n; ++i) {
5956 if (ca[i].pos != cb[i].pos || ca[i].constraint != cb[i].constraint
5957 || ca[i].mode != cb[i].mode)
5961 n = get_ASM_n_output_constraints(a);
5962 if (n != get_ASM_n_output_constraints(b))
5965 ca = get_ASM_output_constraints(a);
5966 cb = get_ASM_output_constraints(b);
5967 for (i = 0; i < n; ++i) {
5968 if (ca[i].pos != cb[i].pos || ca[i].constraint != cb[i].constraint
5969 || ca[i].mode != cb[i].mode)
5973 n = get_ASM_n_clobbers(a);
5974 if (n != get_ASM_n_clobbers(b))
5977 cla = get_ASM_clobbers(a);
5978 clb = get_ASM_clobbers(b);
5979 for (i = 0; i < n; ++i) {
5980 if (cla[i] != clb[i])
5986 /** Compares the inexistent attributes of two Dummy nodes. */
5987 static int node_cmp_attr_Dummy(const ir_node *a, const ir_node *b)
5995 * Set the default node attribute compare operation for an ir_op_ops.
5997 * @param code the opcode for the default operation
5998 * @param ops the operations initialized
6003 static ir_op_ops *firm_set_default_node_cmp_attr(ir_opcode code, ir_op_ops *ops)
6007 ops->node_cmp_attr = node_cmp_attr_##a; \
6039 } /* firm_set_default_node_cmp_attr */
6042 * Compare function for two nodes in the value table. Gets two
6043 * nodes as parameters. Returns 0 if the nodes are a Common Sub Expression.
6045 int identities_cmp(const void *elt, const void *key)
6047 ir_node *a = (ir_node *)elt;
6048 ir_node *b = (ir_node *)key;
6051 if (a == b) return 0;
6053 if ((get_irn_op(a) != get_irn_op(b)) ||
6054 (get_irn_mode(a) != get_irn_mode(b))) return 1;
6056 /* compare if a's in and b's in are of equal length */
6057 irn_arity_a = get_irn_arity(a);
6058 if (irn_arity_a != get_irn_arity(b))
6061 /* blocks are never the same */
6065 if (get_irn_pinned(a) == op_pin_state_pinned) {
6066 /* for pinned nodes, the block inputs must be equal */
6067 if (get_irn_n(a, -1) != get_irn_n(b, -1))
6069 } else if (! get_opt_global_cse()) {
6070 /* for block-local CSE both nodes must be in the same Block */
6071 if (get_nodes_block(a) != get_nodes_block(b))
6075 /* compare a->in[0..ins] with b->in[0..ins] */
6076 for (i = 0; i < irn_arity_a; ++i) {
6077 ir_node *pred_a = get_irn_n(a, i);
6078 ir_node *pred_b = get_irn_n(b, i);
6079 if (pred_a != pred_b) {
6080 /* if both predecessors are CSE neutral they might be different */
6081 if (!is_irn_cse_neutral(pred_a) || !is_irn_cse_neutral(pred_b))
6087 * here, we already now that the nodes are identical except their
6090 if (a->op->ops.node_cmp_attr)
6091 return a->op->ops.node_cmp_attr(a, b);
6094 } /* identities_cmp */
6097 * Calculate a hash value of a node.
6099 * @param node The IR-node
6101 unsigned ir_node_hash(const ir_node *node)
6103 return node->op->ops.hash(node);
6104 } /* ir_node_hash */
6107 void new_identities(ir_graph *irg)
6109 if (irg->value_table != NULL)
6110 del_pset(irg->value_table);
6111 irg->value_table = new_pset(identities_cmp, N_IR_NODES);
6112 } /* new_identities */
6114 void del_identities(ir_graph *irg)
6116 if (irg->value_table != NULL)
6117 del_pset(irg->value_table);
6118 } /* del_identities */
6120 /* Normalize a node by putting constants (and operands with larger
6121 * node index) on the right (operator side). */
6122 void ir_normalize_node(ir_node *n)
6124 if (is_op_commutative(get_irn_op(n))) {
6125 ir_node *l = get_binop_left(n);
6126 ir_node *r = get_binop_right(n);
6128 /* For commutative operators perform a OP b == b OP a but keep
6129 * constants on the RIGHT side. This helps greatly in some
6130 * optimizations. Moreover we use the idx number to make the form
6132 if (!operands_are_normalized(l, r)) {
6133 set_binop_left(n, r);
6134 set_binop_right(n, l);
6138 } /* ir_normalize_node */
6141 * Return the canonical node computing the same value as n.
6142 * Looks up the node in a hash table, enters it in the table
6143 * if it isn't there yet.
6145 * @param n the node to look up
6147 * @return a node that computes the same value as n or n if no such
6148 * node could be found
6150 ir_node *identify_remember(ir_node *n)
6152 ir_graph *irg = get_irn_irg(n);
6153 pset *value_table = irg->value_table;
6156 if (value_table == NULL)
6159 ir_normalize_node(n);
6160 /* lookup or insert in hash table with given hash key. */
6161 nn = (ir_node*)pset_insert(value_table, n, ir_node_hash(n));
6164 /* n is reachable again */
6165 edges_node_revival(nn);
6169 } /* identify_remember */
6172 * During construction we set the op_pin_state_pinned flag in the graph right
6173 * when the optimization is performed. The flag turning on procedure global
6174 * cse could be changed between two allocations. This way we are safe.
6176 * @param n The node to lookup
6178 static inline ir_node *identify_cons(ir_node *n)
6182 n = identify_remember(n);
6183 if (n != old && get_nodes_block(old) != get_nodes_block(n)) {
6184 ir_graph *irg = get_irn_irg(n);
6185 set_irg_pinned(irg, op_pin_state_floats);
6188 } /* identify_cons */
6190 /* Add a node to the identities value table. */
6191 void add_identities(ir_node *node)
6198 identify_remember(node);
6201 /* Visit each node in the value table of a graph. */
6202 void visit_all_identities(ir_graph *irg, irg_walk_func visit, void *env)
6205 ir_graph *rem = current_ir_graph;
6207 current_ir_graph = irg;
6208 foreach_pset(irg->value_table, ir_node*, node) {
6211 current_ir_graph = rem;
6212 } /* visit_all_identities */
6215 * These optimizations deallocate nodes from the obstack.
6216 * It can only be called if it is guaranteed that no other nodes
6217 * reference this one, i.e., right after construction of a node.
6219 * @param n The node to optimize
6221 ir_node *optimize_node(ir_node *n)
6224 ir_graph *irg = get_irn_irg(n);
6225 unsigned iro = get_irn_opcode(n);
6228 /* Always optimize Phi nodes: part of the construction. */
6229 if ((!get_opt_optimize()) && (iro != iro_Phi)) return n;
6231 /* constant expression evaluation / constant folding */
6232 if (get_opt_constant_folding()) {
6233 /* neither constants nor Tuple values can be evaluated */
6234 if (iro != iro_Const && (get_irn_mode(n) != mode_T)) {
6235 /* try to evaluate */
6236 tv = computed_value(n);
6237 if (tv != tarval_bad) {
6242 * we MUST copy the node here temporarily, because it's still
6243 * needed for DBG_OPT_CSTEVAL
6245 node_size = offsetof(ir_node, attr) + n->op->attr_size;
6246 oldn = (ir_node*)alloca(node_size);
6248 memcpy(oldn, n, node_size);
6249 CLONE_ARR_A(ir_node *, oldn->in, n->in);
6251 /* ARG, copy the in array, we need it for statistics */
6252 memcpy(oldn->in, n->in, ARR_LEN(n->in) * sizeof(n->in[0]));
6254 /* note the inplace edges module */
6255 edges_node_deleted(n);
6257 /* evaluation was successful -- replace the node. */
6258 irg_kill_node(irg, n);
6259 nw = new_r_Const(irg, tv);
6261 DBG_OPT_CSTEVAL(oldn, nw);
6267 /* remove unnecessary nodes */
6268 if (get_opt_algebraic_simplification() ||
6269 (iro == iro_Phi) || /* always optimize these nodes. */
6271 (iro == iro_Proj) ||
6272 (iro == iro_Block) ) /* Flags tested local. */
6273 n = equivalent_node(n);
6275 /* Common Subexpression Elimination.
6277 * Checks whether n is already available.
6278 * The block input is used to distinguish different subexpressions. Right
6279 * now all nodes are op_pin_state_pinned to blocks, i.e., the CSE only finds common
6280 * subexpressions within a block.
6283 n = identify_cons(n);
6286 edges_node_deleted(oldn);
6288 /* We found an existing, better node, so we can deallocate the old node. */
6289 irg_kill_node(irg, oldn);
6293 /* Some more constant expression evaluation that does not allow to
6295 iro = get_irn_opcode(n);
6296 if (get_opt_algebraic_simplification() ||
6297 (iro == iro_Cond) ||
6298 (iro == iro_Proj)) /* Flags tested local. */
6299 n = transform_node(n);
6301 /* Now we have a legal, useful node. Enter it in hash table for CSE */
6302 if (get_opt_cse() && (get_irn_opcode(n) != iro_Block)) {
6304 n = identify_remember(o);
6310 } /* optimize_node */
6314 * These optimizations never deallocate nodes (in place). This can cause dead
6315 * nodes lying on the obstack. Remove these by a dead node elimination,
6316 * i.e., a copying garbage collection.
6318 ir_node *optimize_in_place_2(ir_node *n)
6322 unsigned iro = get_irn_opcode(n);
6324 if (!get_opt_optimize() && !is_Phi(n)) return n;
6326 if (iro == iro_Deleted)
6329 /* constant expression evaluation / constant folding */
6330 if (get_opt_constant_folding()) {
6331 /* neither constants nor Tuple values can be evaluated */
6332 if (iro != iro_Const && get_irn_mode(n) != mode_T) {
6333 /* try to evaluate */
6334 tv = computed_value(n);
6335 if (tv != tarval_bad) {
6336 /* evaluation was successful -- replace the node. */
6337 ir_graph *irg = get_irn_irg(n);
6339 n = new_r_Const(irg, tv);
6341 DBG_OPT_CSTEVAL(oldn, n);
6347 /* remove unnecessary nodes */
6348 if (get_opt_constant_folding() ||
6349 (iro == iro_Phi) || /* always optimize these nodes. */
6350 (iro == iro_Id) || /* ... */
6351 (iro == iro_Proj) || /* ... */
6352 (iro == iro_Block) ) /* Flags tested local. */
6353 n = equivalent_node(n);
6355 /** common subexpression elimination **/
6356 /* Checks whether n is already available. */
6357 /* The block input is used to distinguish different subexpressions. Right
6358 now all nodes are op_pin_state_pinned to blocks, i.e., the cse only finds common
6359 subexpressions within a block. */
6360 if (get_opt_cse()) {
6362 n = identify_remember(o);
6367 /* Some more constant expression evaluation. */
6368 iro = get_irn_opcode(n);
6369 if (get_opt_constant_folding() ||
6370 (iro == iro_Cond) ||
6371 (iro == iro_Proj)) /* Flags tested local. */
6372 n = transform_node(n);
6374 /* Now we can verify the node, as it has no dead inputs any more. */
6377 /* Now we have a legal, useful node. Enter it in hash table for cse.
6378 Blocks should be unique anyways. (Except the successor of start:
6379 is cse with the start block!) */
6380 if (get_opt_cse() && (get_irn_opcode(n) != iro_Block)) {
6382 n = identify_remember(o);
6388 } /* optimize_in_place_2 */
6391 * Wrapper for external use, set proper status bits after optimization.
6393 ir_node *optimize_in_place(ir_node *n)
6395 ir_graph *irg = get_irn_irg(n);
6396 /* Handle graph state */
6397 assert(get_irg_phase_state(irg) != phase_building);
6399 if (get_opt_global_cse())
6400 set_irg_pinned(irg, op_pin_state_floats);
6402 /* FIXME: Maybe we could also test whether optimizing the node can
6403 change the control graph. */
6404 set_irg_doms_inconsistent(irg);
6405 return optimize_in_place_2(n);
6406 } /* optimize_in_place */
6409 * Calculate a hash value of a Const node.
6411 static unsigned hash_Const(const ir_node *node)
6415 /* special value for const, as they only differ in their tarval. */
6416 h = HASH_PTR(node->attr.con.tarval);
6422 * Calculate a hash value of a SymConst node.
6424 static unsigned hash_SymConst(const ir_node *node)
6428 /* all others are pointers */
6429 h = HASH_PTR(node->attr.symc.sym.type_p);
6432 } /* hash_SymConst */
6435 * Set the default hash operation in an ir_op_ops.
6437 * @param code the opcode for the default operation
6438 * @param ops the operations initialized
6443 static ir_op_ops *firm_set_default_hash(unsigned code, ir_op_ops *ops)
6447 ops->hash = hash_##a; \
6450 /* hash function already set */
6451 if (ops->hash != NULL)
6458 /* use input/mode default hash if no function was given */
6459 ops->hash = firm_default_hash;
6467 * Sets the default operation for an ir_ops.
6469 ir_op_ops *firm_set_default_operations(unsigned code, ir_op_ops *ops)
6471 ops = firm_set_default_hash(code, ops);
6472 ops = firm_set_default_computed_value(code, ops);
6473 ops = firm_set_default_equivalent_node(code, ops);
6474 ops = firm_set_default_transform_node(code, ops);
6475 ops = firm_set_default_node_cmp_attr(code, ops);
6476 ops = firm_set_default_get_type_attr(code, ops);
6477 ops = firm_set_default_get_entity_attr(code, ops);
6480 } /* firm_set_default_operations */