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)
1231 ir_node *first_val = NULL; /* to shutup gcc */
1233 if (!get_opt_optimize() &&
1234 get_irg_phase_state(get_irn_irg(n)) != phase_building)
1237 n_preds = get_Phi_n_preds(n);
1239 /* Phi of dead Region without predecessors. */
1243 /* Find first non-self-referencing input */
1244 for (i = 0; i < n_preds; ++i) {
1245 first_val = get_Phi_pred(n, i);
1246 /* not self pointer */
1247 if (first_val != n) {
1248 /* then found first value. */
1253 /* search for rest of inputs, determine if any of these
1254 are non-self-referencing */
1255 while (++i < n_preds) {
1256 ir_node *scnd_val = get_Phi_pred(n, i);
1257 if (scnd_val != n && scnd_val != first_val) {
1262 if (i >= n_preds && !is_Dummy(first_val)) {
1263 /* Fold, if no multiple distinct non-self-referencing inputs */
1265 DBG_OPT_PHI(oldn, n);
1268 } /* equivalent_node_Phi */
1271 * Optimize Proj(Tuple).
1273 static ir_node *equivalent_node_Proj_Tuple(ir_node *proj)
1275 ir_node *oldn = proj;
1276 ir_node *tuple = get_Proj_pred(proj);
1278 /* Remove the Tuple/Proj combination. */
1279 proj = get_Tuple_pred(tuple, get_Proj_proj(proj));
1280 DBG_OPT_TUPLE(oldn, tuple, proj);
1283 } /* equivalent_node_Proj_Tuple */
1286 * Optimize a / 1 = a.
1288 static ir_node *equivalent_node_Proj_Div(ir_node *proj)
1290 ir_node *oldn = proj;
1291 ir_node *div = get_Proj_pred(proj);
1292 ir_node *b = get_Div_right(div);
1293 ir_tarval *tb = value_of(b);
1295 /* Div is not commutative. */
1296 if (tarval_is_one(tb)) { /* div(x, 1) == x */
1297 switch (get_Proj_proj(proj)) {
1299 proj = get_Div_mem(div);
1300 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NEUTRAL_1);
1304 proj = get_Div_left(div);
1305 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NEUTRAL_1);
1309 /* we cannot replace the exception Proj's here, this is done in
1310 transform_node_Proj_Div() */
1315 } /* equivalent_node_Proj_Div */
1318 * Optimize CopyB(mem, x, x) into a Nop.
1320 static ir_node *equivalent_node_Proj_CopyB(ir_node *proj)
1322 ir_node *oldn = proj;
1323 ir_node *copyb = get_Proj_pred(proj);
1324 ir_node *a = get_CopyB_dst(copyb);
1325 ir_node *b = get_CopyB_src(copyb);
1328 /* Turn CopyB into a tuple (mem, jmp, bad, bad) */
1329 switch (get_Proj_proj(proj)) {
1331 proj = get_CopyB_mem(copyb);
1332 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NOP);
1337 } /* equivalent_node_Proj_CopyB */
1340 * Optimize Bounds(idx, idx, upper) into idx.
1342 static ir_node *equivalent_node_Proj_Bound(ir_node *proj)
1344 ir_node *oldn = proj;
1345 ir_node *bound = get_Proj_pred(proj);
1346 ir_node *idx = get_Bound_index(bound);
1347 ir_node *pred = skip_Proj(idx);
1350 if (idx == get_Bound_lower(bound))
1352 else if (is_Bound(pred)) {
1354 * idx was Bounds checked previously, it is still valid if
1355 * lower <= pred_lower && pred_upper <= upper.
1357 ir_node *lower = get_Bound_lower(bound);
1358 ir_node *upper = get_Bound_upper(bound);
1359 if (get_Bound_lower(pred) == lower &&
1360 get_Bound_upper(pred) == upper) {
1362 * One could expect that we simply return the previous
1363 * Bound here. However, this would be wrong, as we could
1364 * add an exception Proj to a new location then.
1365 * So, we must turn in into a tuple.
1371 /* Turn Bound into a tuple (mem, jmp, bad, idx) */
1372 switch (get_Proj_proj(proj)) {
1374 DBG_OPT_EXC_REM(proj);
1375 proj = get_Bound_mem(bound);
1379 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NOP);
1382 /* cannot optimize pn_Bound_X_regular, handled in transform ... */
1387 } /* equivalent_node_Proj_Bound */
1390 * Does all optimizations on nodes that must be done on its Projs
1391 * because of creating new nodes.
1393 static ir_node *equivalent_node_Proj(ir_node *proj)
1395 ir_node *n = get_Proj_pred(proj);
1396 if (n->op->ops.equivalent_node_Proj)
1397 return n->op->ops.equivalent_node_Proj(proj);
1399 } /* equivalent_node_Proj */
1404 static ir_node *equivalent_node_Id(ir_node *n)
1412 DBG_OPT_ID(oldn, n);
1414 } /* equivalent_node_Id */
1419 static ir_node *equivalent_node_Mux(ir_node *n)
1421 ir_node *oldn = n, *sel = get_Mux_sel(n);
1423 ir_tarval *ts = value_of(sel);
1425 /* Mux(true, f, t) == t */
1426 if (ts == tarval_b_true) {
1427 n = get_Mux_true(n);
1428 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_C);
1431 /* Mux(false, f, t) == f */
1432 if (ts == tarval_b_false) {
1433 n = get_Mux_false(n);
1434 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_C);
1437 n_t = get_Mux_true(n);
1438 n_f = get_Mux_false(n);
1440 /* Mux(v, x, T) == x */
1441 if (is_Unknown(n_f)) {
1443 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_EQ);
1446 /* Mux(v, T, x) == x */
1447 if (is_Unknown(n_t)) {
1449 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_EQ);
1453 /* Mux(v, x, x) == x */
1456 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_EQ);
1459 if (is_Cmp(sel) && !mode_honor_signed_zeros(get_irn_mode(n))) {
1460 ir_relation relation = get_Cmp_relation(sel);
1461 ir_node *f = get_Mux_false(n);
1462 ir_node *t = get_Mux_true(n);
1465 * Note further that these optimization work even for floating point
1466 * with NaN's because -NaN == NaN.
1467 * However, if +0 and -0 is handled differently, we cannot use the first one.
1469 ir_node *const cmp_l = get_Cmp_left(sel);
1470 ir_node *const cmp_r = get_Cmp_right(sel);
1473 case ir_relation_equal:
1474 if ((cmp_l == t && cmp_r == f) || /* Mux(t == f, t, f) -> f */
1475 (cmp_l == f && cmp_r == t)) { /* Mux(f == t, t, f) -> f */
1477 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1482 case ir_relation_less_greater:
1483 case ir_relation_unordered_less_greater:
1484 if ((cmp_l == t && cmp_r == f) || /* Mux(t != f, t, f) -> t */
1485 (cmp_l == f && cmp_r == t)) { /* Mux(f != t, t, f) -> t */
1487 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1496 * Note: normalization puts the constant on the right side,
1497 * so we check only one case.
1499 if (cmp_l == t && tarval_is_null(value_of(cmp_r))) {
1500 /* Mux(t CMP 0, X, t) */
1501 if (is_Minus(f) && get_Minus_op(f) == t) {
1502 /* Mux(t CMP 0, -t, t) */
1503 if (relation == ir_relation_equal) {
1504 /* Mux(t == 0, -t, t) ==> -t */
1506 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1507 } else if (relation == ir_relation_less_greater || relation == ir_relation_unordered_less_greater) {
1508 /* Mux(t != 0, -t, t) ==> t */
1510 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_TRANSFORM);
1520 * Remove Confirm nodes if setting is on.
1521 * Replace Confirms(x, '=', Constlike) by Constlike.
1523 static ir_node *equivalent_node_Confirm(ir_node *n)
1525 ir_node *pred = get_Confirm_value(n);
1526 ir_relation relation = get_Confirm_relation(n);
1528 while (is_Confirm(pred) && relation == get_Confirm_relation(pred)) {
1530 * rare case: two identical Confirms one after another,
1531 * replace the second one with the first.
1534 pred = get_Confirm_value(n);
1540 * equivalent_node() returns a node equivalent to input n. It skips all nodes that
1541 * perform no actual computation, as, e.g., the Id nodes. It does not create
1542 * new nodes. It is therefore safe to free n if the node returned is not n.
1543 * If a node returns a Tuple we can not just skip it. If the size of the
1544 * in array fits, we transform n into a tuple (e.g., Div).
1546 ir_node *equivalent_node(ir_node *n)
1548 if (n->op->ops.equivalent_node)
1549 return n->op->ops.equivalent_node(n);
1551 } /* equivalent_node */
1554 * Sets the default equivalent node operation for an ir_op_ops.
1556 * @param code the opcode for the default operation
1557 * @param ops the operations initialized
1562 static ir_op_ops *firm_set_default_equivalent_node(ir_opcode code, ir_op_ops *ops)
1566 ops->equivalent_node = equivalent_node_##a; \
1568 #define CASE_PROJ(a) \
1570 ops->equivalent_node_Proj = equivalent_node_Proj_##a; \
1604 } /* firm_set_default_equivalent_node */
1607 * Returns non-zero if a node is a Phi node
1608 * with all predecessors constant.
1610 static int is_const_Phi(ir_node *n)
1614 if (! is_Phi(n) || get_irn_arity(n) == 0)
1616 for (i = get_irn_arity(n) - 1; i >= 0; --i) {
1617 if (! is_Const(get_irn_n(n, i)))
1621 } /* is_const_Phi */
1623 typedef ir_tarval *(*tarval_sub_type)(ir_tarval *a, ir_tarval *b, ir_mode *mode);
1624 typedef ir_tarval *(*tarval_binop_type)(ir_tarval *a, ir_tarval *b);
1627 * in reality eval_func should be tarval (*eval_func)() but incomplete
1628 * declarations are bad style and generate noisy warnings
1630 typedef void (*eval_func)(void);
1633 * Wrapper for the tarval binop evaluation, tarval_sub has one more parameter.
1635 static ir_tarval *do_eval(eval_func eval, ir_tarval *a, ir_tarval *b, ir_mode *mode)
1637 if (eval == (eval_func) tarval_sub) {
1638 tarval_sub_type func = (tarval_sub_type)eval;
1640 return func(a, b, mode);
1642 tarval_binop_type func = (tarval_binop_type)eval;
1649 * Apply an evaluator on a binop with a constant operators (and one Phi).
1651 * @param phi the Phi node
1652 * @param other the other operand
1653 * @param eval an evaluator function
1654 * @param mode the mode of the result, may be different from the mode of the Phi!
1655 * @param left if non-zero, other is the left operand, else the right
1657 * @return a new Phi node if the conversion was successful, NULL else
1659 static ir_node *apply_binop_on_phi(ir_node *phi, ir_tarval *other, eval_func eval, ir_mode *mode, int left)
1665 int i, n = get_irn_arity(phi);
1667 NEW_ARR_A(void *, res, n);
1669 for (i = 0; i < n; ++i) {
1670 pred = get_irn_n(phi, i);
1671 tv = get_Const_tarval(pred);
1672 tv = do_eval(eval, other, tv, mode);
1674 if (tv == tarval_bad) {
1675 /* folding failed, bad */
1681 for (i = 0; i < n; ++i) {
1682 pred = get_irn_n(phi, i);
1683 tv = get_Const_tarval(pred);
1684 tv = do_eval(eval, tv, other, mode);
1686 if (tv == tarval_bad) {
1687 /* folding failed, bad */
1693 irg = get_irn_irg(phi);
1694 for (i = 0; i < n; ++i) {
1695 pred = get_irn_n(phi, i);
1696 res[i] = new_r_Const(irg, (ir_tarval*)res[i]);
1698 return new_r_Phi(get_nodes_block(phi), n, (ir_node **)res, mode);
1699 } /* apply_binop_on_phi */
1702 * Apply an evaluator on a binop with two constant Phi.
1704 * @param a the left Phi node
1705 * @param b the right Phi node
1706 * @param eval an evaluator function
1707 * @param mode the mode of the result, may be different from the mode of the Phi!
1709 * @return a new Phi node if the conversion was successful, NULL else
1711 static ir_node *apply_binop_on_2_phis(ir_node *a, ir_node *b, eval_func eval, ir_mode *mode)
1713 ir_tarval *tv_l, *tv_r, *tv;
1719 if (get_nodes_block(a) != get_nodes_block(b))
1722 n = get_irn_arity(a);
1723 NEW_ARR_A(void *, res, n);
1725 for (i = 0; i < n; ++i) {
1726 pred = get_irn_n(a, i);
1727 tv_l = get_Const_tarval(pred);
1728 pred = get_irn_n(b, i);
1729 tv_r = get_Const_tarval(pred);
1730 tv = do_eval(eval, tv_l, tv_r, mode);
1732 if (tv == tarval_bad) {
1733 /* folding failed, bad */
1738 irg = get_irn_irg(a);
1739 for (i = 0; i < n; ++i) {
1740 pred = get_irn_n(a, i);
1741 res[i] = new_r_Const(irg, (ir_tarval*)res[i]);
1743 return new_r_Phi(get_nodes_block(a), n, (ir_node **)res, mode);
1744 } /* apply_binop_on_2_phis */
1747 * Apply an evaluator on a unop with a constant operator (a Phi).
1749 * @param phi the Phi node
1750 * @param eval an evaluator function
1752 * @return a new Phi node if the conversion was successful, NULL else
1754 static ir_node *apply_unop_on_phi(ir_node *phi, ir_tarval *(*eval)(ir_tarval *))
1761 int i, n = get_irn_arity(phi);
1763 NEW_ARR_A(void *, res, n);
1764 for (i = 0; i < n; ++i) {
1765 pred = get_irn_n(phi, i);
1766 tv = get_Const_tarval(pred);
1769 if (tv == tarval_bad) {
1770 /* folding failed, bad */
1775 mode = get_irn_mode(phi);
1776 irg = get_irn_irg(phi);
1777 for (i = 0; i < n; ++i) {
1778 pred = get_irn_n(phi, i);
1779 res[i] = new_r_Const(irg, (ir_tarval*)res[i]);
1781 return new_r_Phi(get_nodes_block(phi), n, (ir_node **)res, mode);
1782 } /* apply_unop_on_phi */
1785 * Apply a conversion on a constant operator (a Phi).
1787 * @param phi the Phi node
1789 * @return a new Phi node if the conversion was successful, NULL else
1791 static ir_node *apply_conv_on_phi(ir_node *phi, ir_mode *mode)
1797 int i, n = get_irn_arity(phi);
1799 NEW_ARR_A(void *, res, n);
1800 for (i = 0; i < n; ++i) {
1801 pred = get_irn_n(phi, i);
1802 tv = get_Const_tarval(pred);
1803 tv = tarval_convert_to(tv, mode);
1805 if (tv == tarval_bad) {
1806 /* folding failed, bad */
1811 irg = get_irn_irg(phi);
1812 for (i = 0; i < n; ++i) {
1813 pred = get_irn_n(phi, i);
1814 res[i] = new_r_Const(irg, (ir_tarval*)res[i]);
1816 return new_r_Phi(get_nodes_block(phi), n, (ir_node **)res, mode);
1817 } /* apply_conv_on_phi */
1820 * Transform AddP(P, ConvIs(Iu)), AddP(P, ConvIu(Is)) and
1821 * SubP(P, ConvIs(Iu)), SubP(P, ConvIu(Is)).
1822 * If possible, remove the Conv's.
1824 static ir_node *transform_node_AddSub(ir_node *n)
1826 ir_mode *mode = get_irn_mode(n);
1828 if (mode_is_reference(mode)) {
1829 ir_node *left = get_binop_left(n);
1830 ir_node *right = get_binop_right(n);
1831 unsigned ref_bits = get_mode_size_bits(mode);
1833 if (is_Conv(left)) {
1834 ir_mode *lmode = get_irn_mode(left);
1835 unsigned bits = get_mode_size_bits(lmode);
1837 if (ref_bits == bits &&
1838 mode_is_int(lmode) &&
1839 get_mode_arithmetic(lmode) == irma_twos_complement) {
1840 ir_node *pre = get_Conv_op(left);
1841 ir_mode *pre_mode = get_irn_mode(pre);
1843 if (mode_is_int(pre_mode) &&
1844 get_mode_size_bits(pre_mode) == bits &&
1845 get_mode_arithmetic(pre_mode) == irma_twos_complement) {
1846 /* ok, this conv just changes to sign, moreover the calculation
1847 * is done with same number of bits as our address mode, so
1848 * we can ignore the conv as address calculation can be viewed
1849 * as either signed or unsigned
1851 set_binop_left(n, pre);
1856 if (is_Conv(right)) {
1857 ir_mode *rmode = get_irn_mode(right);
1858 unsigned bits = get_mode_size_bits(rmode);
1860 if (ref_bits == bits &&
1861 mode_is_int(rmode) &&
1862 get_mode_arithmetic(rmode) == irma_twos_complement) {
1863 ir_node *pre = get_Conv_op(right);
1864 ir_mode *pre_mode = get_irn_mode(pre);
1866 if (mode_is_int(pre_mode) &&
1867 get_mode_size_bits(pre_mode) == bits &&
1868 get_mode_arithmetic(pre_mode) == irma_twos_complement) {
1869 /* ok, this conv just changes to sign, moreover the calculation
1870 * is done with same number of bits as our address mode, so
1871 * we can ignore the conv as address calculation can be viewed
1872 * as either signed or unsigned
1874 set_binop_right(n, pre);
1879 /* let address arithmetic use unsigned modes */
1880 if (is_Const(right)) {
1881 ir_mode *rmode = get_irn_mode(right);
1883 if (mode_is_signed(rmode) && get_mode_arithmetic(rmode) == irma_twos_complement) {
1884 /* convert a AddP(P, *s) into AddP(P, *u) */
1885 ir_mode *nm = get_reference_mode_unsigned_eq(mode);
1887 ir_node *pre = new_r_Conv(get_nodes_block(n), right, nm);
1888 set_binop_right(n, pre);
1894 } /* transform_node_AddSub */
1896 #define HANDLE_BINOP_PHI(eval, a, b, c, mode) \
1899 if (is_Const(b) && is_const_Phi(a)) { \
1900 /* check for Op(Phi, Const) */ \
1901 c = apply_binop_on_phi(a, get_Const_tarval(b), eval, mode, 0);\
1903 else if (is_Const(a) && is_const_Phi(b)) { \
1904 /* check for Op(Const, Phi) */ \
1905 c = apply_binop_on_phi(b, get_Const_tarval(a), eval, mode, 1);\
1907 else if (is_const_Phi(a) && is_const_Phi(b)) { \
1908 /* check for Op(Phi, Phi) */ \
1909 c = apply_binop_on_2_phis(a, b, eval, mode); \
1912 DBG_OPT_ALGSIM0(oldn, c, FS_OPT_CONST_PHI); \
1917 #define HANDLE_UNOP_PHI(eval, a, c) \
1920 if (is_const_Phi(a)) { \
1921 /* check for Op(Phi) */ \
1922 c = apply_unop_on_phi(a, eval); \
1924 DBG_OPT_ALGSIM0(oldn, c, FS_OPT_CONST_PHI); \
1931 * Do the AddSub optimization, then Transform
1932 * Constant folding on Phi
1933 * Add(a,a) -> Mul(a, 2)
1934 * Add(Mul(a, x), a) -> Mul(a, x+1)
1935 * if the mode is integer or float.
1936 * Transform Add(a,-b) into Sub(a,b).
1937 * Reassociation might fold this further.
1939 static ir_node *transform_node_Add(ir_node *n)
1942 ir_node *a, *b, *c, *oldn = n;
1943 vrp_attr *a_vrp, *b_vrp;
1945 n = transform_node_AddSub(n);
1947 a = get_Add_left(n);
1948 b = get_Add_right(n);
1950 mode = get_irn_mode(n);
1952 if (mode_is_reference(mode)) {
1953 ir_mode *lmode = get_irn_mode(a);
1955 if (is_Const(b) && is_Const_null(b) && mode_is_int(lmode)) {
1956 /* an Add(a, NULL) is a hidden Conv */
1957 dbg_info *dbg = get_irn_dbg_info(n);
1958 return new_rd_Conv(dbg, get_nodes_block(n), a, mode);
1962 HANDLE_BINOP_PHI((eval_func) tarval_add, a, b, c, mode);
1964 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
1965 if (mode_is_float(mode)) {
1966 ir_graph *irg = get_irn_irg(n);
1967 if (get_irg_fp_model(irg) & fp_strict_algebraic)
1971 if (mode_is_num(mode)) {
1972 ir_graph *irg = get_irn_irg(n);
1973 /* the following code leads to endless recursion when Mul are replaced by a simple instruction chain */
1974 if (!is_irg_state(irg, IR_GRAPH_STATE_ARCH_DEP)
1975 && a == b && mode_is_int(mode)) {
1976 ir_node *block = get_nodes_block(n);
1979 get_irn_dbg_info(n),
1982 new_r_Const_long(irg, mode, 2),
1984 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_A);
1989 get_irn_dbg_info(n),
1994 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_MINUS_B);
1999 get_irn_dbg_info(n),
2004 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_A_MINUS_B);
2007 if (get_mode_arithmetic(mode) == irma_twos_complement) {
2008 /* Here we rely on constants be on the RIGHT side */
2010 ir_node *op = get_Not_op(a);
2012 if (is_Const(b) && is_Const_one(b)) {
2014 ir_node *blk = get_nodes_block(n);
2015 n = new_rd_Minus(get_irn_dbg_info(n), blk, op, mode);
2016 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_PLUS_1);
2021 n = new_r_Const(irg, get_mode_minus_one(mode));
2022 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_X_NOT_X);
2027 ir_node *op = get_Not_op(b);
2031 n = new_r_Const(irg, get_mode_minus_one(mode));
2032 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_ADD_X_NOT_X);
2039 a_vrp = vrp_get_info(a);
2040 b_vrp = vrp_get_info(b);
2042 if (a_vrp && b_vrp) {
2043 ir_tarval *c = tarval_and(a_vrp->bits_not_set, b_vrp->bits_not_set);
2045 if (tarval_is_null(c)) {
2046 dbg_info *dbgi = get_irn_dbg_info(n);
2047 return new_rd_Or(dbgi, get_nodes_block(n), a, b, mode);
2051 } /* transform_node_Add */
2054 * returns -cnst or NULL if impossible
2056 static ir_node *const_negate(ir_node *cnst)
2058 ir_tarval *tv = tarval_neg(get_Const_tarval(cnst));
2059 dbg_info *dbgi = get_irn_dbg_info(cnst);
2060 ir_graph *irg = get_irn_irg(cnst);
2061 if (tv == tarval_bad) return NULL;
2062 return new_rd_Const(dbgi, irg, tv);
2066 * Do the AddSub optimization, then Transform
2067 * Constant folding on Phi
2068 * Sub(0,a) -> Minus(a)
2069 * Sub(Mul(a, x), a) -> Mul(a, x-1)
2070 * Sub(Sub(x, y), b) -> Sub(x, Add(y,b))
2071 * Sub(Add(a, x), x) -> a
2072 * Sub(x, Add(x, a)) -> -a
2073 * Sub(x, Const) -> Add(x, -Const)
2075 static ir_node *transform_node_Sub(ir_node *n)
2081 n = transform_node_AddSub(n);
2083 a = get_Sub_left(n);
2084 b = get_Sub_right(n);
2086 mode = get_irn_mode(n);
2088 if (mode_is_int(mode)) {
2089 ir_mode *lmode = get_irn_mode(a);
2091 if (is_Const(b) && is_Const_null(b) && mode_is_reference(lmode)) {
2092 /* a Sub(a, NULL) is a hidden Conv */
2093 dbg_info *dbg = get_irn_dbg_info(n);
2094 n = new_rd_Conv(dbg, get_nodes_block(n), a, mode);
2095 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_CONV);
2099 if (mode == lmode &&
2100 get_mode_arithmetic(mode) == irma_twos_complement &&
2102 get_Const_tarval(a) == get_mode_minus_one(mode)) {
2104 dbg_info *dbg = get_irn_dbg_info(n);
2105 n = new_rd_Not(dbg, get_nodes_block(n), b, mode);
2106 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_NOT);
2112 HANDLE_BINOP_PHI((eval_func) tarval_sub, a, b, c, mode);
2114 /* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
2115 if (mode_is_float(mode)) {
2116 ir_graph *irg = get_irn_irg(n);
2117 if (get_irg_fp_model(irg) & fp_strict_algebraic)
2121 if (is_Const(b) && !mode_is_reference(get_irn_mode(b))) {
2122 /* a - C -> a + (-C) */
2123 ir_node *cnst = const_negate(b);
2125 ir_node *block = get_nodes_block(n);
2126 dbg_info *dbgi = get_irn_dbg_info(n);
2128 n = new_rd_Add(dbgi, block, a, cnst, mode);
2129 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2134 if (is_Minus(a)) { /* (-a) - b -> -(a + b) */
2135 dbg_info *dbg = get_irn_dbg_info(n);
2136 ir_node *block = get_nodes_block(n);
2137 ir_node *left = get_Minus_op(a);
2138 ir_node *add = new_rd_Add(dbg, block, left, b, mode);
2140 n = new_rd_Minus(dbg, block, add, mode);
2141 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2143 } else if (is_Minus(b)) { /* a - (-b) -> a + b */
2144 dbg_info *dbg = get_irn_dbg_info(n);
2145 ir_node *block = get_nodes_block(n);
2146 ir_node *right = get_Minus_op(b);
2148 n = new_rd_Add(dbg, block, a, right, mode);
2149 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MINUS);
2151 } else if (is_Sub(b)) {
2152 /* a - (b - c) -> a + (c - b)
2153 * -> (a - b) + c iff (b - c) is a pointer */
2154 dbg_info *s_dbg = get_irn_dbg_info(b);
2155 ir_node *s_left = get_Sub_left(b);
2156 ir_node *s_right = get_Sub_right(b);
2157 ir_mode *s_mode = get_irn_mode(b);
2158 if (mode_is_reference(s_mode)) {
2159 ir_node *lowest_block = get_nodes_block(n); /* a and b are live here */
2160 ir_node *sub = new_rd_Sub(s_dbg, lowest_block, a, s_left, mode);
2161 dbg_info *a_dbg = get_irn_dbg_info(n);
2164 s_right = new_r_Conv(lowest_block, s_right, mode);
2165 n = new_rd_Add(a_dbg, lowest_block, sub, s_right, mode);
2167 ir_node *s_block = get_nodes_block(b);
2168 ir_node *sub = new_rd_Sub(s_dbg, s_block, s_right, s_left, s_mode);
2169 dbg_info *a_dbg = get_irn_dbg_info(n);
2170 ir_node *a_block = get_nodes_block(n);
2172 n = new_rd_Add(a_dbg, a_block, a, sub, mode);
2174 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2176 } else if (is_Mul(b)) { /* a - (b * C) -> a + (b * -C) */
2177 ir_node *m_right = get_Mul_right(b);
2178 if (is_Const(m_right)) {
2179 ir_node *cnst2 = const_negate(m_right);
2180 if (cnst2 != NULL) {
2181 dbg_info *m_dbg = get_irn_dbg_info(b);
2182 ir_node *m_block = get_nodes_block(b);
2183 ir_node *m_left = get_Mul_left(b);
2184 ir_mode *m_mode = get_irn_mode(b);
2185 ir_node *mul = new_rd_Mul(m_dbg, m_block, m_left, cnst2, m_mode);
2186 dbg_info *a_dbg = get_irn_dbg_info(n);
2187 ir_node *a_block = get_nodes_block(n);
2189 n = new_rd_Add(a_dbg, a_block, a, mul, mode);
2190 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_TO_ADD);
2196 /* Beware of Sub(P, P) which cannot be optimized into a simple Minus ... */
2197 if (mode_is_num(mode) && mode == get_irn_mode(a) && is_Const(a) && is_Const_null(a)) {
2199 get_irn_dbg_info(n),
2203 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_0_A);
2207 if (mode_wrap_around(mode)) {
2208 ir_node *left = get_Add_left(a);
2209 ir_node *right = get_Add_right(a);
2211 /* FIXME: Does the Conv's work only for two complement or generally? */
2213 if (mode != get_irn_mode(right)) {
2214 /* This Sub is an effective Cast */
2215 right = new_r_Conv(get_nodes_block(n), right, mode);
2218 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2220 } else if (right == b) {
2221 if (mode != get_irn_mode(left)) {
2222 /* This Sub is an effective Cast */
2223 left = new_r_Conv(get_nodes_block(n), left, mode);
2226 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2232 if (mode_wrap_around(mode)) {
2233 ir_node *left = get_Add_left(b);
2234 ir_node *right = get_Add_right(b);
2236 /* FIXME: Does the Conv's work only for two complement or generally? */
2238 ir_mode *r_mode = get_irn_mode(right);
2240 n = new_r_Minus(get_nodes_block(n), right, r_mode);
2241 if (mode != r_mode) {
2242 /* This Sub is an effective Cast */
2243 n = new_r_Conv(get_nodes_block(n), n, mode);
2245 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2247 } else if (right == a) {
2248 ir_mode *l_mode = get_irn_mode(left);
2250 n = new_r_Minus(get_nodes_block(n), left, l_mode);
2251 if (mode != l_mode) {
2252 /* This Sub is an effective Cast */
2253 n = new_r_Conv(get_nodes_block(n), n, mode);
2255 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
2260 if (mode_is_int(mode) && is_Conv(a) && is_Conv(b)) {
2261 ir_mode *mode = get_irn_mode(a);
2263 if (mode == get_irn_mode(b)) {
2265 ir_node *op_a = get_Conv_op(a);
2266 ir_node *op_b = get_Conv_op(b);
2268 /* check if it's allowed to skip the conv */
2269 ma = get_irn_mode(op_a);
2270 mb = get_irn_mode(op_b);
2272 if (mode_is_reference(ma) && mode_is_reference(mb)) {
2273 /* SubInt(ConvInt(aP), ConvInt(bP)) -> SubInt(aP,bP) */
2276 set_Sub_right(n, b);
2282 /* do NOT execute this code if reassociation is enabled, it does the inverse! */
2283 if (!is_reassoc_running() && is_Mul(a)) {
2284 ir_node *ma = get_Mul_left(a);
2285 ir_node *mb = get_Mul_right(a);
2288 ir_node *blk = get_nodes_block(n);
2289 ir_graph *irg = get_irn_irg(n);
2291 get_irn_dbg_info(n),
2295 get_irn_dbg_info(n),
2298 new_r_Const(irg, get_mode_one(mode)),
2301 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MUL_A_X_A);
2303 } else if (mb == b) {
2304 ir_node *blk = get_nodes_block(n);
2305 ir_graph *irg = get_irn_irg(n);
2307 get_irn_dbg_info(n),
2311 get_irn_dbg_info(n),
2314 new_r_Const(irg, get_mode_one(mode)),
2317 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_MUL_A_X_A);
2321 if (is_Sub(a)) { /* (x - y) - b -> x - (y + b) */
2322 ir_node *x = get_Sub_left(a);
2323 ir_node *y = get_Sub_right(a);
2324 ir_node *blk = get_nodes_block(n);
2325 ir_mode *m_b = get_irn_mode(b);
2326 ir_mode *m_y = get_irn_mode(y);
2330 /* Determine the right mode for the Add. */
2333 else if (mode_is_reference(m_b))
2335 else if (mode_is_reference(m_y))
2339 * Both modes are different but none is reference,
2340 * happens for instance in SubP(SubP(P, Iu), Is).
2341 * We have two possibilities here: Cast or ignore.
2342 * Currently we ignore this case.
2347 add = new_r_Add(blk, y, b, add_mode);
2349 n = new_rd_Sub(get_irn_dbg_info(n), blk, x, add, mode);
2350 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_SUB_X_Y_Z);
2354 if (get_mode_arithmetic(mode) == irma_twos_complement) {
2355 if (is_Const(a) && is_Not(b)) {
2356 /* c - ~X = X + (c+1) */
2357 ir_tarval *tv = get_Const_tarval(a);
2359 tv = tarval_add(tv, get_mode_one(mode));
2360 if (tv != tarval_bad) {
2361 ir_node *blk = get_nodes_block(n);
2362 ir_graph *irg = get_irn_irg(n);
2363 ir_node *c = new_r_Const(irg, tv);
2364 n = new_rd_Add(get_irn_dbg_info(n), blk, get_Not_op(b), c, mode);
2365 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_SUB_C_NOT_X);
2371 } /* transform_node_Sub */
2374 * Several transformation done on n*n=2n bits mul.
2375 * These transformations must be done here because new nodes may be produced.
2377 static ir_node *transform_node_Mul2n(ir_node *n, ir_mode *mode)
2380 ir_node *a = get_Mul_left(n);
2381 ir_node *b = get_Mul_right(n);
2382 ir_tarval *ta = value_of(a);
2383 ir_tarval *tb = value_of(b);
2384 ir_mode *smode = get_irn_mode(a);
2386 if (ta == get_mode_one(smode)) {
2387 /* (L)1 * (L)b = (L)b */
2388 ir_node *blk = get_nodes_block(n);
2389 n = new_rd_Conv(get_irn_dbg_info(n), blk, b, mode);
2390 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
2393 else if (ta == get_mode_minus_one(smode)) {
2394 /* (L)-1 * (L)b = (L)b */
2395 ir_node *blk = get_nodes_block(n);
2396 n = new_rd_Minus(get_irn_dbg_info(n), blk, b, smode);
2397 n = new_rd_Conv(get_irn_dbg_info(n), blk, n, mode);
2398 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2401 if (tb == get_mode_one(smode)) {
2402 /* (L)a * (L)1 = (L)a */
2403 ir_node *blk = get_irn_n(a, -1);
2404 n = new_rd_Conv(get_irn_dbg_info(n), blk, a, mode);
2405 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_1);
2408 else if (tb == get_mode_minus_one(smode)) {
2409 /* (L)a * (L)-1 = (L)-a */
2410 ir_node *blk = get_nodes_block(n);
2411 n = new_rd_Minus(get_irn_dbg_info(n), blk, a, smode);
2412 n = new_rd_Conv(get_irn_dbg_info(n), blk, n, mode);
2413 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2420 * Transform Mul(a,-1) into -a.
2421 * Do constant evaluation of Phi nodes.
2422 * Do architecture dependent optimizations on Mul nodes
2424 static ir_node *transform_node_Mul(ir_node *n)
2426 ir_node *c, *oldn = n;
2427 ir_mode *mode = get_irn_mode(n);
2428 ir_node *a = get_Mul_left(n);
2429 ir_node *b = get_Mul_right(n);
2431 if (is_Bad(a) || is_Bad(b))
2434 if (mode != get_irn_mode(a))
2435 return transform_node_Mul2n(n, mode);
2437 HANDLE_BINOP_PHI((eval_func) tarval_mul, a, b, c, mode);
2439 if (mode_is_signed(mode)) {
2442 if (value_of(a) == get_mode_minus_one(mode))
2444 else if (value_of(b) == get_mode_minus_one(mode))
2447 n = new_rd_Minus(get_irn_dbg_info(n), get_nodes_block(n), r, mode);
2448 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2453 if (is_Const(b)) { /* (-a) * const -> a * -const */
2454 ir_node *cnst = const_negate(b);
2456 dbg_info *dbgi = get_irn_dbg_info(n);
2457 ir_node *block = get_nodes_block(n);
2458 n = new_rd_Mul(dbgi, block, get_Minus_op(a), cnst, mode);
2459 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_1);
2462 } else if (is_Minus(b)) { /* (-a) * (-b) -> a * b */
2463 dbg_info *dbgi = get_irn_dbg_info(n);
2464 ir_node *block = get_nodes_block(n);
2465 n = new_rd_Mul(dbgi, block, get_Minus_op(a), get_Minus_op(b), mode);
2466 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS_MINUS);
2468 } else if (is_Sub(b)) { /* (-a) * (b - c) -> a * (c - b) */
2469 ir_node *sub_l = get_Sub_left(b);
2470 ir_node *sub_r = get_Sub_right(b);
2471 dbg_info *dbgi = get_irn_dbg_info(n);
2472 ir_node *block = get_nodes_block(n);
2473 ir_node *new_b = new_rd_Sub(dbgi, block, sub_r, sub_l, mode);
2474 n = new_rd_Mul(dbgi, block, get_Minus_op(a), new_b, mode);
2475 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS);
2478 } else if (is_Minus(b)) {
2479 if (is_Sub(a)) { /* (a - b) * (-c) -> (b - a) * c */
2480 ir_node *sub_l = get_Sub_left(a);
2481 ir_node *sub_r = get_Sub_right(a);
2482 dbg_info *dbgi = get_irn_dbg_info(n);
2483 ir_node *block = get_nodes_block(n);
2484 ir_node *new_a = new_rd_Sub(dbgi, block, sub_r, sub_l, mode);
2485 n = new_rd_Mul(dbgi, block, new_a, get_Minus_op(b), mode);
2486 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_MINUS);
2489 } else if (is_Shl(a)) {
2490 ir_node *const shl_l = get_Shl_left(a);
2491 if (is_Const(shl_l) && is_Const_one(shl_l)) {
2492 /* (1 << x) * b -> b << x */
2493 dbg_info *const dbgi = get_irn_dbg_info(n);
2494 ir_node *const block = get_nodes_block(n);
2495 ir_node *const shl_r = get_Shl_right(a);
2496 n = new_rd_Shl(dbgi, block, b, shl_r, mode);
2497 // TODO add me DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_SHIFT);
2500 } else if (is_Shl(b)) {
2501 ir_node *const shl_l = get_Shl_left(b);
2502 if (is_Const(shl_l) && is_Const_one(shl_l)) {
2503 /* a * (1 << x) -> a << x */
2504 dbg_info *const dbgi = get_irn_dbg_info(n);
2505 ir_node *const block = get_nodes_block(n);
2506 ir_node *const shl_r = get_Shl_right(b);
2507 n = new_rd_Shl(dbgi, block, a, shl_r, mode);
2508 // TODO add me DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_MUL_SHIFT);
2512 if (get_mode_arithmetic(mode) == irma_ieee754) {
2514 ir_tarval *tv = get_Const_tarval(a);
2515 if (tarval_ieee754_get_exponent(tv) == 1 && tarval_ieee754_zero_mantissa(tv)
2516 && !tarval_is_negative(tv)) {
2517 /* 2.0 * b = b + b */
2518 n = new_rd_Add(get_irn_dbg_info(n), get_nodes_block(n), b, b, mode);
2519 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_A_A);
2523 else if (is_Const(b)) {
2524 ir_tarval *tv = get_Const_tarval(b);
2525 if (tarval_ieee754_get_exponent(tv) == 1 && tarval_ieee754_zero_mantissa(tv)
2526 && !tarval_is_negative(tv)) {
2527 /* a * 2.0 = a + a */
2528 n = new_rd_Add(get_irn_dbg_info(n), get_nodes_block(n), a, a, mode);
2529 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_A_A);
2534 return arch_dep_replace_mul_with_shifts(n);
2535 } /* transform_node_Mul */
2538 * Transform a Div Node.
2540 static ir_node *transform_node_Div(ir_node *n)
2542 ir_mode *mode = get_Div_resmode(n);
2543 ir_node *a = get_Div_left(n);
2544 ir_node *b = get_Div_right(n);
2546 const ir_node *dummy;
2548 if (mode_is_int(mode)) {
2549 if (is_Const(b) && is_const_Phi(a)) {
2550 /* check for Div(Phi, Const) */
2551 value = apply_binop_on_phi(a, get_Const_tarval(b), (eval_func) tarval_div, mode, 0);
2553 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2556 } else if (is_Const(a) && is_const_Phi(b)) {
2557 /* check for Div(Const, Phi) */
2558 value = apply_binop_on_phi(b, get_Const_tarval(a), (eval_func) tarval_div, mode, 1);
2560 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2563 } else if (is_const_Phi(a) && is_const_Phi(b)) {
2564 /* check for Div(Phi, Phi) */
2565 value = apply_binop_on_2_phis(a, b, (eval_func) tarval_div, mode);
2567 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2572 if (a == b && value_not_zero(a, &dummy)) {
2573 ir_graph *irg = get_irn_irg(n);
2574 /* BEWARE: we can optimize a/a to 1 only if this cannot cause a exception */
2575 value = new_r_Const(irg, get_mode_one(mode));
2576 DBG_OPT_CSTEVAL(n, value);
2579 if (mode_is_signed(mode) && is_Const(b)) {
2580 ir_tarval *tv = get_Const_tarval(b);
2582 if (tv == get_mode_minus_one(mode)) {
2584 value = new_rd_Minus(get_irn_dbg_info(n), get_nodes_block(n), a, mode);
2585 DBG_OPT_CSTEVAL(n, value);
2589 /* Try architecture dependent optimization */
2590 value = arch_dep_replace_div_by_const(n);
2593 assert(mode_is_float(mode));
2595 /* Optimize x/c to x*(1/c) */
2596 if (get_mode_arithmetic(mode) == irma_ieee754) {
2597 ir_tarval *tv = value_of(b);
2599 if (tv != tarval_bad) {
2600 int rem = tarval_fp_ops_enabled();
2603 * Floating point constant folding might be disabled here to
2605 * However, as we check for exact result, doing it is safe.
2608 tarval_enable_fp_ops(1);
2609 tv = tarval_div(get_mode_one(mode), tv);
2610 tarval_enable_fp_ops(rem);
2612 /* Do the transformation if the result is either exact or we are
2613 not using strict rules. */
2614 if (tv != tarval_bad &&
2615 (tarval_ieee754_get_exact() || (get_irg_fp_model(get_irn_irg(n)) & fp_strict_algebraic) == 0)) {
2616 ir_node *block = get_nodes_block(n);
2617 ir_graph *irg = get_irn_irg(block);
2618 ir_node *c = new_r_Const(irg, tv);
2619 dbg_info *dbgi = get_irn_dbg_info(n);
2620 value = new_rd_Mul(dbgi, block, a, c, mode);
2633 /* Turn Div into a tuple (mem, jmp, bad, value) */
2634 mem = get_Div_mem(n);
2635 blk = get_nodes_block(n);
2636 irg = get_irn_irg(blk);
2638 /* skip a potential Pin */
2639 mem = skip_Pin(mem);
2640 turn_into_tuple(n, pn_Div_max);
2641 set_Tuple_pred(n, pn_Div_M, mem);
2642 set_Tuple_pred(n, pn_Div_X_regular, new_r_Jmp(blk));
2643 set_Tuple_pred(n, pn_Div_X_except, new_r_Bad(irg, mode_X));
2644 set_Tuple_pred(n, pn_Div_res, value);
2647 } /* transform_node_Div */
2650 * Transform a Mod node.
2652 static ir_node *transform_node_Mod(ir_node *n)
2654 ir_mode *mode = get_Mod_resmode(n);
2655 ir_node *a = get_Mod_left(n);
2656 ir_node *b = get_Mod_right(n);
2661 if (is_Const(b) && is_const_Phi(a)) {
2662 /* check for Div(Phi, Const) */
2663 value = apply_binop_on_phi(a, get_Const_tarval(b), (eval_func) tarval_mod, mode, 0);
2665 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2669 else if (is_Const(a) && is_const_Phi(b)) {
2670 /* check for Div(Const, Phi) */
2671 value = apply_binop_on_phi(b, get_Const_tarval(a), (eval_func) tarval_mod, mode, 1);
2673 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2677 else if (is_const_Phi(a) && is_const_Phi(b)) {
2678 /* check for Div(Phi, Phi) */
2679 value = apply_binop_on_2_phis(a, b, (eval_func) tarval_mod, mode);
2681 DBG_OPT_ALGSIM0(n, value, FS_OPT_CONST_PHI);
2688 irg = get_irn_irg(n);
2689 if (tv != tarval_bad) {
2690 value = new_r_Const(irg, tv);
2692 DBG_OPT_CSTEVAL(n, value);
2695 ir_node *a = get_Mod_left(n);
2696 ir_node *b = get_Mod_right(n);
2697 const ir_node *dummy;
2699 if (a == b && value_not_zero(a, &dummy)) {
2700 /* BEWARE: we can optimize a%a to 0 only if this cannot cause a exception */
2701 value = new_r_Const(irg, get_mode_null(mode));
2702 DBG_OPT_CSTEVAL(n, value);
2705 if (mode_is_signed(mode) && is_Const(b)) {
2706 ir_tarval *tv = get_Const_tarval(b);
2708 if (tv == get_mode_minus_one(mode)) {
2710 value = new_r_Const(irg, get_mode_null(mode));
2711 DBG_OPT_CSTEVAL(n, value);
2715 /* Try architecture dependent optimization */
2716 value = arch_dep_replace_mod_by_const(n);
2725 /* Turn Mod into a tuple (mem, jmp, bad, value) */
2726 mem = get_Mod_mem(n);
2727 blk = get_nodes_block(n);
2728 irg = get_irn_irg(blk);
2730 /* skip a potential Pin */
2731 mem = skip_Pin(mem);
2732 turn_into_tuple(n, pn_Mod_max);
2733 set_Tuple_pred(n, pn_Mod_M, mem);
2734 set_Tuple_pred(n, pn_Mod_X_regular, new_r_Jmp(blk));
2735 set_Tuple_pred(n, pn_Mod_X_except, new_r_Bad(irg, mode_X));
2736 set_Tuple_pred(n, pn_Mod_res, value);
2739 } /* transform_node_Mod */
2742 * Transform a Cond node.
2744 * Replace the Cond by a Jmp if it branches on a constant
2747 static ir_node *transform_node_Cond(ir_node *n)
2750 ir_node *a = get_Cond_selector(n);
2751 ir_tarval *ta = value_of(a);
2752 ir_graph *irg = get_irn_irg(n);
2755 /* we need block info which is not available in floating irgs */
2756 if (get_irg_pinned(irg) == op_pin_state_floats)
2759 if ((ta != tarval_bad) &&
2760 (get_irn_mode(a) == mode_b) &&
2761 (get_opt_unreachable_code())) {
2762 /* It's a boolean Cond, branching on a boolean constant.
2763 Replace it by a tuple (Bad, Jmp) or (Jmp, Bad) */
2764 ir_node *blk = get_nodes_block(n);
2765 jmp = new_r_Jmp(blk);
2766 turn_into_tuple(n, pn_Cond_max);
2767 if (ta == tarval_b_true) {
2768 set_Tuple_pred(n, pn_Cond_false, new_r_Bad(irg, mode_X));
2769 set_Tuple_pred(n, pn_Cond_true, jmp);
2771 set_Tuple_pred(n, pn_Cond_false, jmp);
2772 set_Tuple_pred(n, pn_Cond_true, new_r_Bad(irg, mode_X));
2774 /* We might generate an endless loop, so keep it alive. */
2775 add_End_keepalive(get_irg_end(irg), blk);
2778 } /* transform_node_Cond */
2781 * Prototype of a recursive transform function
2782 * for bitwise distributive transformations.
2784 typedef ir_node* (*recursive_transform)(ir_node *n);
2787 * makes use of distributive laws for and, or, eor
2788 * and(a OP c, b OP c) -> and(a, b) OP c
2789 * note, might return a different op than n
2791 static ir_node *transform_bitwise_distributive(ir_node *n,
2792 recursive_transform trans_func)
2795 ir_node *a = get_binop_left(n);
2796 ir_node *b = get_binop_right(n);
2797 ir_op *op = get_irn_op(a);
2798 ir_op *op_root = get_irn_op(n);
2800 if (op != get_irn_op(b))
2803 /* and(conv(a), conv(b)) -> conv(and(a,b)) */
2804 if (op == op_Conv) {
2805 ir_node *a_op = get_Conv_op(a);
2806 ir_node *b_op = get_Conv_op(b);
2807 ir_mode *a_mode = get_irn_mode(a_op);
2808 ir_mode *b_mode = get_irn_mode(b_op);
2809 if (a_mode == b_mode && (mode_is_int(a_mode) || a_mode == mode_b)) {
2810 ir_node *blk = get_nodes_block(n);
2813 set_binop_left(n, a_op);
2814 set_binop_right(n, b_op);
2815 set_irn_mode(n, a_mode);
2817 n = new_r_Conv(blk, n, get_irn_mode(oldn));
2819 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_CONV);
2825 /* nothing to gain here */
2829 if (op == op_Shrs || op == op_Shr || op == op_Shl
2830 || op == op_And || op == op_Or || op == op_Eor) {
2831 ir_node *a_left = get_binop_left(a);
2832 ir_node *a_right = get_binop_right(a);
2833 ir_node *b_left = get_binop_left(b);
2834 ir_node *b_right = get_binop_right(b);
2836 ir_node *op1 = NULL;
2837 ir_node *op2 = NULL;
2839 if (is_op_commutative(op)) {
2840 if (a_left == b_left) {
2844 } else if (a_left == b_right) {
2848 } else if (a_right == b_left) {
2854 if (a_right == b_right) {
2861 /* (a sop c) & (b sop c) => (a & b) sop c */
2862 ir_node *blk = get_nodes_block(n);
2864 ir_node *new_n = exact_copy(n);
2865 set_binop_left(new_n, op1);
2866 set_binop_right(new_n, op2);
2867 new_n = trans_func(new_n);
2869 if (op_root == op_Eor && op == op_Or) {
2870 dbg_info *dbgi = get_irn_dbg_info(n);
2871 ir_mode *mode = get_irn_mode(c);
2873 c = new_rd_Not(dbgi, blk, c, mode);
2874 n = new_rd_And(dbgi, blk, new_n, c, mode);
2877 set_nodes_block(n, blk);
2878 set_binop_left(n, new_n);
2879 set_binop_right(n, c);
2883 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_SHIFT_AND);
2892 * Create a 0 constant of given mode.
2894 static ir_node *create_zero_const(ir_graph *irg, ir_mode *mode)
2896 ir_tarval *tv = get_mode_null(mode);
2897 ir_node *cnst = new_r_Const(irg, tv);
2905 static ir_node *transform_node_And(ir_node *n)
2907 ir_node *c, *oldn = n;
2908 ir_node *a = get_And_left(n);
2909 ir_node *b = get_And_right(n);
2911 vrp_attr *a_vrp, *b_vrp;
2913 if (is_Cmp(a) && is_Cmp(b)) {
2914 ir_node *a_left = get_Cmp_left(a);
2915 ir_node *a_right = get_Cmp_right(a);
2916 ir_node *b_left = get_Cmp_left(b);
2917 ir_node *b_right = get_Cmp_right(b);
2918 ir_relation a_relation = get_Cmp_relation(a);
2919 ir_relation b_relation = get_Cmp_relation(b);
2920 /* we can combine the relations of two compares with the same
2922 if (a_left == b_left && b_left == b_right) {
2923 dbg_info *dbgi = get_irn_dbg_info(n);
2924 ir_node *block = get_nodes_block(n);
2925 ir_relation new_relation = a_relation & b_relation;
2926 return new_rd_Cmp(dbgi, block, a_left, a_right, new_relation);
2928 /* Cmp(a==0) and Cmp(b==0) can be optimized to Cmp(a|b==0) */
2929 if (is_Const(a_right) && is_Const_null(a_right)
2930 && is_Const(b_right) && is_Const_null(b_right)
2931 && a_relation == b_relation && a_relation == ir_relation_equal
2932 && !mode_is_float(get_irn_mode(a_left))
2933 && !mode_is_float(get_irn_mode(b_left))) {
2934 dbg_info *dbgi = get_irn_dbg_info(n);
2935 ir_node *block = get_nodes_block(n);
2936 ir_mode *mode = get_irn_mode(a_left);
2937 ir_node *n_b_left = get_irn_mode(b_left) != mode ?
2938 new_rd_Conv(dbgi, block, b_left, mode) : b_left;
2939 ir_node *or = new_rd_Or(dbgi, block, a_left, n_b_left, mode);
2940 ir_graph *irg = get_irn_irg(n);
2941 ir_node *zero = create_zero_const(irg, mode);
2942 return new_rd_Cmp(dbgi, block, or, zero, ir_relation_equal);
2946 mode = get_irn_mode(n);
2947 HANDLE_BINOP_PHI((eval_func) tarval_and, a, b, c, mode);
2951 ir_node *op = get_Not_op(b);
2953 ir_node *ba = get_And_left(op);
2954 ir_node *bb = get_And_right(op);
2956 /* it's enough to test the following cases due to normalization! */
2957 if (get_Or_left(a) == ba && get_Or_right(a) == bb) {
2958 /* (a|b) & ~(a&b) = a^b */
2959 ir_node *block = get_nodes_block(n);
2961 n = new_rd_Eor(get_irn_dbg_info(n), block, ba, bb, mode);
2962 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_TO_EOR);
2970 ir_node *op = get_Not_op(a);
2972 ir_node *aa = get_And_left(op);
2973 ir_node *ab = get_And_right(op);
2975 /* it's enough to test the following cases due to normalization! */
2976 if (get_Or_left(b) == aa && get_Or_right(b) == ab) {
2977 /* (a|b) & ~(a&b) = a^b */
2978 ir_node *block = get_nodes_block(n);
2980 n = new_rd_Eor(get_irn_dbg_info(n), block, aa, ab, mode);
2981 DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_TO_EOR);
2988 ir_node *al = get_Eor_left(a);
2989 ir_node *ar = get_Eor_right(a);
2992 /* (b ^ a) & b -> ~a & b */
2993 dbg_info *dbg = get_irn_dbg_info(n);
2994 ir_node *block = get_nodes_block(n);
2996 ar = new_rd_Not(dbg, block, ar, mode);
2997 n = new_rd_And(dbg, block, ar, b, mode);
2998 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3002 /* (a ^ b) & b -> ~a & b */
3003 dbg_info *dbg = get_irn_dbg_info(n);
3004 ir_node *block = get_nodes_block(n);
3006 al = new_rd_Not(dbg, block, al, mode);
3007 n = new_rd_And(dbg, block, al, b, mode);
3008 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3013 ir_node *bl = get_Eor_left(b);
3014 ir_node *br = get_Eor_right(b);
3017 /* a & (a ^ b) -> a & ~b */
3018 dbg_info *dbg = get_irn_dbg_info(n);
3019 ir_node *block = get_nodes_block(n);
3021 br = new_rd_Not(dbg, block, br, mode);
3022 n = new_rd_And(dbg, block, br, a, mode);
3023 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3027 /* a & (b ^ a) -> a & ~b */
3028 dbg_info *dbg = get_irn_dbg_info(n);
3029 ir_node *block = get_nodes_block(n);
3031 bl = new_rd_Not(dbg, block, bl, mode);
3032 n = new_rd_And(dbg, block, bl, a, mode);
3033 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3037 if (is_Not(a) && is_Not(b)) {
3038 /* ~a & ~b = ~(a|b) */
3039 ir_node *block = get_nodes_block(n);
3040 ir_mode *mode = get_irn_mode(n);
3044 n = new_rd_Or(get_irn_dbg_info(n), block, a, b, mode);
3045 n = new_rd_Not(get_irn_dbg_info(n), block, n, mode);
3046 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_DEMORGAN);
3050 b_vrp = vrp_get_info(b);
3051 if (is_Const(a) && b_vrp && (tarval_cmp(tarval_or(get_Const_tarval(a),
3052 b_vrp->bits_not_set), get_Const_tarval(a)) == ir_relation_equal)) {
3058 a_vrp = vrp_get_info(a);
3059 if (is_Const(b) && a_vrp && (tarval_cmp(tarval_or(get_Const_tarval(b),
3060 a_vrp->bits_not_set), get_Const_tarval(b)) == ir_relation_equal)) {
3064 n = transform_bitwise_distributive(n, transform_node_And);
3067 } /* transform_node_And */
3069 /* the order of the values is important! */
3070 typedef enum const_class {
3076 static const_class classify_const(const ir_node* n)
3078 if (is_Const(n)) return const_const;
3079 if (is_irn_constlike(n)) return const_like;
3084 * Determines whether r is more constlike or has a larger index (in that order)
3087 static bool operands_are_normalized(const ir_node *l, const ir_node *r)
3089 const const_class l_order = classify_const(l);
3090 const const_class r_order = classify_const(r);
3092 l_order > r_order ||
3093 (l_order == r_order && get_irn_idx(l) <= get_irn_idx(r));
3099 static ir_node *transform_node_Eor(ir_node *n)
3101 ir_node *c, *oldn = n;
3102 ir_node *a = get_Eor_left(n);
3103 ir_node *b = get_Eor_right(n);
3104 ir_mode *mode = get_irn_mode(n);
3106 /* we can combine the relations of two compares with the same operands */
3107 if (is_Cmp(a) && is_Cmp(b)) {
3108 ir_node *a_left = get_Cmp_left(a);
3109 ir_node *a_right = get_Cmp_left(a);
3110 ir_node *b_left = get_Cmp_left(b);
3111 ir_node *b_right = get_Cmp_right(b);
3112 if (a_left == b_left && b_left == b_right) {
3113 dbg_info *dbgi = get_irn_dbg_info(n);
3114 ir_node *block = get_nodes_block(n);
3115 ir_relation a_relation = get_Cmp_relation(a);
3116 ir_relation b_relation = get_Cmp_relation(b);
3117 ir_relation new_relation = a_relation ^ b_relation;
3118 return new_rd_Cmp(dbgi, block, a_left, a_right, new_relation);
3122 HANDLE_BINOP_PHI((eval_func) tarval_eor, a, b, c, mode);
3124 /* normalize not nodes... ~a ^ b <=> a ^ ~b */
3125 if (is_Not(a) && operands_are_normalized(get_Not_op(a), b)) {
3126 dbg_info *dbg = get_irn_dbg_info(n);
3127 ir_node *block = get_nodes_block(n);
3128 ir_node *new_not = new_rd_Not(dbg, block, b, mode);
3129 ir_node *new_left = get_Not_op(a);
3130 n = new_rd_Eor(dbg, block, new_left, new_not, mode);
3131 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3133 } else if (is_Not(b) && !operands_are_normalized(a, get_Not_op(b))) {
3134 dbg_info *dbg = get_irn_dbg_info(n);
3135 ir_node *block = get_nodes_block(n);
3136 ir_node *new_not = new_rd_Not(dbg, block, a, mode);
3137 ir_node *new_right = get_Not_op(b);
3138 n = new_rd_Eor(dbg, block, new_not, new_right, mode);
3139 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3143 /* x ^ 1...1 -> ~1 */
3144 if (is_Const(b) && is_Const_all_one(b)) {
3145 n = new_r_Not(get_nodes_block(n), a, mode);
3146 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_EOR_TO_NOT);
3150 n = transform_bitwise_distributive(n, transform_node_Eor);
3152 } /* transform_node_Eor */
3157 static ir_node *transform_node_Not(ir_node *n)
3159 ir_node *c, *oldn = n;
3160 ir_node *a = get_Not_op(n);
3161 ir_mode *mode = get_irn_mode(n);
3163 HANDLE_UNOP_PHI(tarval_not,a,c);
3165 /* check for a boolean Not */
3167 dbg_info *dbgi = get_irn_dbg_info(a);
3168 ir_node *block = get_nodes_block(a);
3169 ir_relation relation = get_Cmp_relation(a);
3170 relation = get_negated_relation(relation);
3171 n = new_rd_Cmp(dbgi, block, get_Cmp_left(a), get_Cmp_right(a), relation);
3172 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_CMP);
3176 /* normalize ~(a ^ b) => a ^ ~b */
3178 dbg_info *dbg = get_irn_dbg_info(n);
3179 ir_node *block = get_nodes_block(n);
3180 ir_node *eor_right = get_Eor_right(a);
3181 ir_node *eor_left = get_Eor_left(a);
3182 eor_right = new_rd_Not(dbg, block, eor_right, mode);
3183 n = new_rd_Eor(dbg, block, eor_left, eor_right, mode);
3187 if (get_mode_arithmetic(mode) == irma_twos_complement) {
3188 if (is_Minus(a)) { /* ~-x -> x + -1 */
3189 dbg_info *dbg = get_irn_dbg_info(n);
3190 ir_graph *irg = get_irn_irg(n);
3191 ir_node *block = get_nodes_block(n);
3192 ir_node *add_l = get_Minus_op(a);
3193 ir_node *add_r = new_rd_Const(dbg, irg, get_mode_minus_one(mode));
3194 n = new_rd_Add(dbg, block, add_l, add_r, mode);
3195 } else if (is_Add(a)) {
3196 ir_node *add_r = get_Add_right(a);
3197 if (is_Const(add_r) && is_Const_all_one(add_r)) {
3198 /* ~(x + -1) = -x */
3199 ir_node *op = get_Add_left(a);
3200 ir_node *blk = get_nodes_block(n);
3201 n = new_rd_Minus(get_irn_dbg_info(n), blk, op, get_irn_mode(n));
3202 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_NOT_MINUS_1);
3210 * Transform a Minus.
3214 * -(a >>u (size-1)) = a >>s (size-1)
3215 * -(a >>s (size-1)) = a >>u (size-1)
3216 * -(a * const) -> a * -const
3218 static ir_node *transform_node_Minus(ir_node *n)
3220 ir_node *c, *oldn = n;
3221 ir_node *a = get_Minus_op(n);
3224 HANDLE_UNOP_PHI(tarval_neg,a,c);
3226 mode = get_irn_mode(a);
3227 if (get_mode_arithmetic(mode) == irma_twos_complement) {
3228 /* the following rules are only to twos-complement */
3231 ir_node *op = get_Not_op(a);
3232 ir_tarval *tv = get_mode_one(mode);
3233 ir_node *blk = get_nodes_block(n);
3234 ir_graph *irg = get_irn_irg(blk);
3235 ir_node *c = new_r_Const(irg, tv);
3236 n = new_rd_Add(get_irn_dbg_info(n), blk, op, c, mode);
3237 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_MINUS_NOT);
3241 ir_node *c = get_Shr_right(a);
3244 ir_tarval *tv = get_Const_tarval(c);
3246 if (tarval_is_long(tv) && get_tarval_long(tv) == (int) get_mode_size_bits(mode) - 1) {
3247 /* -(a >>u (size-1)) = a >>s (size-1) */
3248 ir_node *v = get_Shr_left(a);
3250 n = new_rd_Shrs(get_irn_dbg_info(n), get_nodes_block(n), v, c, mode);
3251 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_PREDICATE);
3257 ir_node *c = get_Shrs_right(a);
3260 ir_tarval *tv = get_Const_tarval(c);
3262 if (tarval_is_long(tv) && get_tarval_long(tv) == (int) get_mode_size_bits(mode) - 1) {
3263 /* -(a >>s (size-1)) = a >>u (size-1) */
3264 ir_node *v = get_Shrs_left(a);
3266 n = new_rd_Shr(get_irn_dbg_info(n), get_nodes_block(n), v, c, mode);
3267 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_PREDICATE);
3274 /* - (a-b) = b - a */
3275 ir_node *la = get_Sub_left(a);
3276 ir_node *ra = get_Sub_right(a);
3277 ir_node *blk = get_nodes_block(n);
3279 n = new_rd_Sub(get_irn_dbg_info(n), blk, ra, la, mode);
3280 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_MINUS_SUB);
3284 if (is_Mul(a)) { /* -(a * const) -> a * -const */
3285 ir_node *mul_l = get_Mul_left(a);
3286 ir_node *mul_r = get_Mul_right(a);
3287 ir_tarval *tv = value_of(mul_r);
3288 if (tv != tarval_bad) {
3289 tv = tarval_neg(tv);
3290 if (tv != tarval_bad) {
3291 ir_graph *irg = get_irn_irg(n);
3292 ir_node *cnst = new_r_Const(irg, tv);
3293 dbg_info *dbg = get_irn_dbg_info(a);
3294 ir_node *block = get_nodes_block(a);
3295 n = new_rd_Mul(dbg, block, mul_l, cnst, mode);
3296 DBG_OPT_ALGSIM2(oldn, a, n, FS_OPT_MINUS_MUL_C);
3303 } /* transform_node_Minus */
3306 * Transform a Proj(Load) with a non-null address.
3308 static ir_node *transform_node_Proj_Load(ir_node *proj)
3310 if (get_opt_ldst_only_null_ptr_exceptions()) {
3311 if (get_irn_mode(proj) == mode_X) {
3312 ir_node *load = get_Proj_pred(proj);
3314 /* get the Load address */
3315 const ir_node *addr = get_Load_ptr(load);
3316 const ir_node *confirm;
3318 if (value_not_null(addr, &confirm)) {
3319 if (confirm == NULL) {
3320 /* this node may float if it did not depend on a Confirm */
3321 set_irn_pinned(load, op_pin_state_floats);
3323 if (get_Proj_proj(proj) == pn_Load_X_except) {
3324 ir_graph *irg = get_irn_irg(proj);
3325 DBG_OPT_EXC_REM(proj);
3326 return new_r_Bad(irg, mode_X);
3328 ir_node *blk = get_nodes_block(load);
3329 return new_r_Jmp(blk);
3335 } /* transform_node_Proj_Load */
3338 * Transform a Proj(Store) with a non-null address.
3340 static ir_node *transform_node_Proj_Store(ir_node *proj)
3342 if (get_opt_ldst_only_null_ptr_exceptions()) {
3343 if (get_irn_mode(proj) == mode_X) {
3344 ir_node *store = get_Proj_pred(proj);
3346 /* get the load/store address */
3347 const ir_node *addr = get_Store_ptr(store);
3348 const ir_node *confirm;
3350 if (value_not_null(addr, &confirm)) {
3351 if (confirm == NULL) {
3352 /* this node may float if it did not depend on a Confirm */
3353 set_irn_pinned(store, op_pin_state_floats);
3355 if (get_Proj_proj(proj) == pn_Store_X_except) {
3356 ir_graph *irg = get_irn_irg(proj);
3357 DBG_OPT_EXC_REM(proj);
3358 return new_r_Bad(irg, mode_X);
3360 ir_node *blk = get_nodes_block(store);
3361 return new_r_Jmp(blk);
3367 } /* transform_node_Proj_Store */
3370 * Transform a Proj(Div) with a non-zero value.
3371 * Removes the exceptions and routes the memory to the NoMem node.
3373 static ir_node *transform_node_Proj_Div(ir_node *proj)
3375 ir_node *div = get_Proj_pred(proj);
3376 ir_node *b = get_Div_right(div);
3377 ir_node *res, *new_mem;
3378 const ir_node *confirm;
3381 if (value_not_zero(b, &confirm)) {
3382 /* div(x, y) && y != 0 */
3383 if (confirm == NULL) {
3384 /* we are sure we have a Const != 0 */
3385 new_mem = get_Div_mem(div);
3386 new_mem = skip_Pin(new_mem);
3387 set_Div_mem(div, new_mem);
3388 set_irn_pinned(div, op_pin_state_floats);
3391 proj_nr = get_Proj_proj(proj);
3393 case pn_Div_X_regular:
3394 return new_r_Jmp(get_nodes_block(div));
3396 case pn_Div_X_except: {
3397 ir_graph *irg = get_irn_irg(proj);
3398 /* we found an exception handler, remove it */
3399 DBG_OPT_EXC_REM(proj);
3400 return new_r_Bad(irg, mode_X);
3404 ir_graph *irg = get_irn_irg(proj);
3405 res = get_Div_mem(div);
3406 new_mem = get_irg_no_mem(irg);
3409 /* This node can only float up to the Confirm block */
3410 new_mem = new_r_Pin(get_nodes_block(confirm), new_mem);
3412 set_irn_pinned(div, op_pin_state_floats);
3413 /* this is a Div without exception, we can remove the memory edge */
3414 set_Div_mem(div, new_mem);
3420 } /* transform_node_Proj_Div */
3423 * Transform a Proj(Mod) with a non-zero value.
3424 * Removes the exceptions and routes the memory to the NoMem node.
3426 static ir_node *transform_node_Proj_Mod(ir_node *proj)
3428 ir_node *mod = get_Proj_pred(proj);
3429 ir_node *b = get_Mod_right(mod);
3430 ir_node *res, *new_mem;
3431 const ir_node *confirm;
3434 if (value_not_zero(b, &confirm)) {
3435 /* mod(x, y) && y != 0 */
3436 proj_nr = get_Proj_proj(proj);
3438 if (confirm == NULL) {
3439 /* we are sure we have a Const != 0 */
3440 new_mem = get_Mod_mem(mod);
3441 new_mem = skip_Pin(new_mem);
3442 set_Mod_mem(mod, new_mem);
3443 set_irn_pinned(mod, op_pin_state_floats);
3448 case pn_Mod_X_regular:
3449 return new_r_Jmp(get_irn_n(mod, -1));
3451 case pn_Mod_X_except: {
3452 ir_graph *irg = get_irn_irg(proj);
3453 /* we found an exception handler, remove it */
3454 DBG_OPT_EXC_REM(proj);
3455 return new_r_Bad(irg, mode_X);
3459 ir_graph *irg = get_irn_irg(proj);
3460 res = get_Mod_mem(mod);
3461 new_mem = get_irg_no_mem(irg);
3464 /* This node can only float up to the Confirm block */
3465 new_mem = new_r_Pin(get_nodes_block(confirm), new_mem);
3467 /* this is a Mod without exception, we can remove the memory edge */
3468 set_Mod_mem(mod, new_mem);
3472 if (get_Mod_left(mod) == b) {
3473 /* a % a = 0 if a != 0 */
3474 ir_graph *irg = get_irn_irg(proj);
3475 ir_mode *mode = get_irn_mode(proj);
3476 ir_node *res = new_r_Const(irg, get_mode_null(mode));
3478 DBG_OPT_CSTEVAL(mod, res);
3484 } /* transform_node_Proj_Mod */
3487 * Optimizes jump tables (CondIs or CondIu) by removing all impossible cases.
3489 static ir_node *transform_node_Proj_Cond(ir_node *proj)
3491 ir_node *n = get_Proj_pred(proj);
3492 ir_node *b = get_Cond_selector(n);
3494 if (!get_opt_unreachable_code())
3497 if (mode_is_int(get_irn_mode(b))) {
3498 ir_tarval *tb = value_of(b);
3500 if (tb != tarval_bad) {
3501 /* we have a constant switch */
3502 long num = get_Proj_proj(proj);
3504 if (num != get_Cond_default_proj(n)) { /* we cannot optimize default Proj's yet */
3505 if (get_tarval_long(tb) == num) {
3506 /* Do NOT create a jump here, or we will have 2 control flow ops
3507 * in a block. This case is optimized away in optimize_cf(). */
3510 ir_graph *irg = get_irn_irg(proj);
3511 /* this case will NEVER be taken, kill it */
3512 return new_r_Bad(irg, mode_X);
3516 long num = get_Proj_proj(proj);
3517 vrp_attr *b_vrp = vrp_get_info(b);
3518 if (num != get_Cond_default_proj(n) && b_vrp) {
3519 /* Try handling with vrp data. We only remove dead parts. */
3520 ir_tarval *tp = new_tarval_from_long(num, get_irn_mode(b));
3522 if (b_vrp->range_type == VRP_RANGE) {
3523 ir_relation cmp_result = tarval_cmp(b_vrp->range_bottom, tp);
3524 ir_relation cmp_result2 = tarval_cmp(b_vrp->range_top, tp);
3526 if ((cmp_result & ir_relation_greater) == cmp_result
3527 && (cmp_result2 & ir_relation_less) == cmp_result2) {
3528 ir_graph *irg = get_irn_irg(proj);
3529 return new_r_Bad(irg, mode_X);
3531 } else if (b_vrp->range_type == VRP_ANTIRANGE) {
3532 ir_relation cmp_result = tarval_cmp(b_vrp->range_bottom, tp);
3533 ir_relation cmp_result2 = tarval_cmp(b_vrp->range_top, tp);
3535 if ((cmp_result & ir_relation_less_equal) == cmp_result
3536 && (cmp_result2 & ir_relation_greater_equal) == cmp_result2) {
3537 ir_graph *irg = get_irn_irg(proj);
3538 return new_r_Bad(irg, mode_X);
3543 tarval_and( b_vrp->bits_set, tp),
3545 ) == ir_relation_equal)) {
3546 ir_graph *irg = get_irn_irg(proj);
3547 return new_r_Bad(irg, mode_X);
3553 tarval_not(b_vrp->bits_not_set)),
3554 tarval_not(b_vrp->bits_not_set))
3555 == ir_relation_equal)) {
3556 ir_graph *irg = get_irn_irg(proj);
3557 return new_r_Bad(irg, mode_X);
3566 * return true if the operation returns a value with exactly 1 bit set
3568 static bool is_single_bit(const ir_node *node)
3570 /* a first implementation, could be extended with vrp and others... */
3572 ir_node *shl_l = get_Shl_left(node);
3573 ir_mode *mode = get_irn_mode(node);
3574 int modulo = get_mode_modulo_shift(mode);
3575 /* this works if we shift a 1 and we have modulo shift */
3576 if (is_Const(shl_l) && is_Const_one(shl_l)
3577 && 0 < modulo && modulo <= (int)get_mode_size_bits(mode)) {
3580 } else if (is_Const(node)) {
3581 ir_tarval *tv = get_Const_tarval(node);
3582 return tarval_is_single_bit(tv);
3588 * Normalizes and optimizes Cmp nodes.
3590 static ir_node *transform_node_Cmp(ir_node *n)
3592 ir_node *left = get_Cmp_left(n);
3593 ir_node *right = get_Cmp_right(n);
3594 ir_mode *mode = get_irn_mode(left);
3595 ir_tarval *tv = NULL;
3596 bool changed = false;
3597 bool changedc = false;
3598 ir_relation relation = get_Cmp_relation(n);
3599 ir_relation possible = ir_get_possible_cmp_relations(left, right);
3601 /* mask out impossible relations */
3602 ir_relation new_relation = relation & possible;
3603 if (new_relation != relation) {
3604 relation = new_relation;
3608 /* Remove unnecessary conversions */
3609 /* TODO handle conv+constant */
3610 if (is_Conv(left) && is_Conv(right)) {
3611 ir_node *op_left = get_Conv_op(left);
3612 ir_node *op_right = get_Conv_op(right);
3613 ir_mode *mode_left = get_irn_mode(op_left);
3614 ir_mode *mode_right = get_irn_mode(op_right);
3616 if (smaller_mode(mode_left, mode) && smaller_mode(mode_right, mode)
3617 && mode_left != mode_b && mode_right != mode_b) {
3618 ir_node *block = get_nodes_block(n);
3620 if (mode_left == mode_right) {
3624 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV_CONV);
3625 } else if (smaller_mode(mode_left, mode_right)) {
3626 left = new_r_Conv(block, op_left, mode_right);
3629 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV);
3630 } else if (smaller_mode(mode_right, mode_left)) {
3632 right = new_r_Conv(block, op_right, mode_left);
3634 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV);
3640 * Optimize -a CMP -b into b CMP a.
3641 * This works only for modes where unary Minus cannot Overflow.
3642 * Note that two-complement integers can Overflow so it will NOT work.
3644 if (!mode_overflow_on_unary_Minus(mode) &&
3645 is_Minus(left) && is_Minus(right)) {
3646 left = get_Minus_op(left);
3647 right = get_Minus_op(right);
3648 relation = get_inversed_relation(relation);
3650 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3653 /* remove operation on both sides if possible */
3654 if (relation == ir_relation_equal || relation == ir_relation_less_greater) {
3656 * The following operations are NOT safe for floating point operations, for instance
3657 * 1.0 + inf == 2.0 + inf, =/=> x == y
3659 if (mode_is_int(mode)) {
3660 unsigned lop = get_irn_opcode(left);
3662 if (lop == get_irn_opcode(right)) {
3663 ir_node *ll, *lr, *rl, *rr;
3665 /* same operation on both sides, try to remove */
3669 /* ~a CMP ~b => a CMP b, -a CMP -b ==> a CMP b */
3670 left = get_unop_op(left);
3671 right = get_unop_op(right);
3673 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3676 ll = get_Add_left(left);
3677 lr = get_Add_right(left);
3678 rl = get_Add_left(right);
3679 rr = get_Add_right(right);
3682 /* X + a CMP X + b ==> a CMP b */
3686 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3687 } else if (ll == rr) {
3688 /* X + a CMP b + X ==> a CMP b */
3692 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3693 } else if (lr == rl) {
3694 /* a + X CMP X + b ==> a CMP b */
3698 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3699 } else if (lr == rr) {
3700 /* a + X CMP b + X ==> a CMP b */
3704 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3708 ll = get_Sub_left(left);
3709 lr = get_Sub_right(left);
3710 rl = get_Sub_left(right);
3711 rr = get_Sub_right(right);
3714 /* X - a CMP X - b ==> a CMP b */
3718 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3719 } else if (lr == rr) {
3720 /* a - X CMP b - X ==> a CMP b */
3724 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3728 if (get_Rotl_right(left) == get_Rotl_right(right)) {
3729 /* a ROTL X CMP b ROTL X ==> a CMP b */
3730 left = get_Rotl_left(left);
3731 right = get_Rotl_left(right);
3733 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3741 /* X+A == A, A+X == A, A-X == A -> X == 0 */
3742 if (is_Add(left) || is_Sub(left)) {
3743 ir_node *ll = get_binop_left(left);
3744 ir_node *lr = get_binop_right(left);
3746 if (lr == right && is_Add(left)) {
3752 ir_graph *irg = get_irn_irg(n);
3754 right = create_zero_const(irg, mode);
3756 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3759 if (is_Add(right) || is_Sub(right)) {
3760 ir_node *rl = get_binop_left(right);
3761 ir_node *rr = get_binop_right(right);
3763 if (rr == left && is_Add(right)) {
3769 ir_graph *irg = get_irn_irg(n);
3771 right = create_zero_const(irg, mode);
3773 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_OP);
3777 if (is_And(left) && is_Const(right)) {
3778 ir_node *ll = get_binop_left(left);
3779 ir_node *lr = get_binop_right(left);
3780 if (is_Shr(ll) && is_Const(lr)) {
3781 /* Cmp((x >>u c1) & c2, c3) = Cmp(x & (c2 << c1), c3 << c1) */
3782 ir_node *block = get_nodes_block(n);
3783 ir_mode *mode = get_irn_mode(left);
3785 ir_node *llr = get_Shr_right(ll);
3786 if (is_Const(llr)) {
3787 dbg_info *dbg = get_irn_dbg_info(left);
3788 ir_graph *irg = get_irn_irg(left);
3790 ir_tarval *c1 = get_Const_tarval(llr);
3791 ir_tarval *c2 = get_Const_tarval(lr);
3792 ir_tarval *c3 = get_Const_tarval(right);
3793 ir_tarval *mask = tarval_shl(c2, c1);
3794 ir_tarval *value = tarval_shl(c3, c1);
3796 left = new_rd_And(dbg, block, get_Shr_left(ll), new_r_Const(irg, mask), mode);
3797 right = new_r_Const(irg, value);
3802 /* Cmp(Eor(x, y), 0) <=> Cmp(x, y) at least for the ==0,!=0
3804 if (is_Const(right) && is_Const_null(right) && is_Eor(left)) {
3805 right = get_Eor_right(left);
3806 left = get_Eor_left(left);
3809 } /* mode_is_int(...) */
3812 /* Cmp(And(1bit, val), 1bit) "bit-testing" can be replaced
3813 * by the simpler Cmp(And(1bit), val), 0) negated pnc */
3814 if (mode_is_int(mode) && is_And(left)
3815 && (relation == ir_relation_equal
3816 || (mode_is_signed(mode) && relation == ir_relation_less_greater)
3817 || (!mode_is_signed(mode) && (relation & ir_relation_less_equal) == ir_relation_less))) {
3818 ir_node *and0 = get_And_left(left);
3819 ir_node *and1 = get_And_right(left);
3820 if (and1 == right) {
3821 ir_node *tmp = and0;
3825 if (and0 == right && is_single_bit(and0)) {
3826 ir_graph *irg = get_irn_irg(n);
3828 relation == ir_relation_equal ? ir_relation_less_greater : ir_relation_equal;
3829 right = create_zero_const(irg, mode);
3834 /* replace mode_b compares with ands/ors */
3835 if (mode == mode_b) {
3836 ir_node *block = get_nodes_block(n);
3840 case ir_relation_less_equal:
3841 bres = new_r_Or(block, new_r_Not(block, left, mode_b), right, mode_b);
3843 case ir_relation_less:
3844 bres = new_r_And(block, new_r_Not(block, left, mode_b), right, mode_b);
3846 case ir_relation_greater_equal:
3847 bres = new_r_Or(block, left, new_r_Not(block, right, mode_b), mode_b);
3849 case ir_relation_greater:
3850 bres = new_r_And(block, left, new_r_Not(block, right, mode_b), mode_b);
3852 case ir_relation_less_greater:
3853 bres = new_r_Eor(block, left, right, mode_b);
3855 case ir_relation_equal:
3856 bres = new_r_Not(block, new_r_Eor(block, left, right, mode_b), mode_b);
3859 #ifdef DEBUG_libfirm
3860 ir_fprintf(stderr, "Optimisation warning, unexpected mode_b Cmp %+F\n", n);
3865 DBG_OPT_ALGSIM0(n, bres, FS_OPT_CMP_TO_BOOL);
3871 * First step: normalize the compare op
3872 * by placing the constant on the right side
3873 * or moving the lower address node to the left.
3875 if (!operands_are_normalized(left, right)) {
3880 relation = get_inversed_relation(relation);
3885 * Second step: Try to reduce the magnitude
3886 * of a constant. This may help to generate better code
3887 * later and may help to normalize more compares.
3888 * Of course this is only possible for integer values.
3890 tv = value_of(right);
3891 if (tv != tarval_bad) {
3892 ir_mode *mode = get_irn_mode(right);
3894 /* TODO extend to arbitrary constants */
3895 if (is_Conv(left) && tarval_is_null(tv)) {
3896 ir_node *op = get_Conv_op(left);
3897 ir_mode *op_mode = get_irn_mode(op);
3900 * UpConv(x) REL 0 ==> x REL 0
3901 * Don't do this for float values as it's unclear whether it is a
3902 * win. (on the other side it makes detection/creation of fabs hard)
3904 if (get_mode_size_bits(mode) > get_mode_size_bits(op_mode) &&
3905 ((relation == ir_relation_equal || relation == ir_relation_less_greater) ||
3906 mode_is_signed(mode) || !mode_is_signed(op_mode)) &&
3907 !mode_is_float(mode)) {
3908 tv = get_mode_null(op_mode);
3912 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CONV);
3916 if (tv != tarval_bad) {
3917 /* the following optimization is possible on modes without Overflow
3918 * on Unary Minus or on == and !=:
3919 * -a CMP c ==> a swap(CMP) -c
3921 * Beware: for two-complement Overflow may occur, so only == and != can
3922 * be optimized, see this:
3923 * -MININT < 0 =/=> MININT > 0 !!!
3925 if (is_Minus(left) &&
3926 (!mode_overflow_on_unary_Minus(mode) ||
3927 (mode_is_int(mode) && (relation == ir_relation_equal || relation == ir_relation_less_greater)))) {
3928 tv = tarval_neg(tv);
3930 if (tv != tarval_bad) {
3931 left = get_Minus_op(left);
3932 relation = get_inversed_relation(relation);
3934 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
3936 } else if (is_Not(left) && (relation == ir_relation_equal || relation == ir_relation_less_greater)) {
3937 /* Not(a) ==/!= c ==> a ==/!= Not(c) */
3938 tv = tarval_not(tv);
3940 if (tv != tarval_bad) {
3941 left = get_Not_op(left);
3943 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
3947 /* for integer modes, we have more */
3948 if (mode_is_int(mode)) {
3949 /* c > 0 : a < c ==> a <= (c-1) a >= c ==> a > (c-1) */
3950 if ((relation == ir_relation_less || relation == ir_relation_greater_equal) &&
3951 tarval_cmp(tv, get_mode_null(mode)) == ir_relation_greater) {
3952 tv = tarval_sub(tv, get_mode_one(mode), NULL);
3954 if (tv != tarval_bad) {
3955 relation ^= ir_relation_equal;
3957 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CNST_MAGN);
3960 /* c < 0 : a > c ==> a >= (c+1) a <= c ==> a < (c+1) */
3961 else if ((relation == ir_relation_greater || relation == ir_relation_less_equal) &&
3962 tarval_cmp(tv, get_mode_null(mode)) == ir_relation_less) {
3963 tv = tarval_add(tv, get_mode_one(mode));
3965 if (tv != tarval_bad) {
3966 relation ^= ir_relation_equal;
3968 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CNST_MAGN);
3972 /* the following reassociations work only for == and != */
3973 if (relation == ir_relation_equal || relation == ir_relation_less_greater) {
3974 if (tv != tarval_bad) {
3975 /* a-c1 == c2 ==> a == c2+c1, a-c1 != c2 ==> a != c2+c1 */
3977 ir_node *c1 = get_Sub_right(left);
3978 ir_tarval *tv2 = value_of(c1);
3980 if (tv2 != tarval_bad) {
3981 tv2 = tarval_add(tv, value_of(c1));
3983 if (tv2 != tarval_bad) {
3984 left = get_Sub_left(left);
3987 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
3991 /* a+c1 == c2 ==> a == c2-c1, a+c1 != c2 ==> a != c2-c1 */
3992 else if (is_Add(left)) {
3993 ir_node *a_l = get_Add_left(left);
3994 ir_node *a_r = get_Add_right(left);
3998 if (is_Const(a_l)) {
4000 tv2 = value_of(a_l);
4003 tv2 = value_of(a_r);
4006 if (tv2 != tarval_bad) {
4007 tv2 = tarval_sub(tv, tv2, NULL);
4009 if (tv2 != tarval_bad) {
4013 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4017 /* -a == c ==> a == -c, -a != c ==> a != -c */
4018 else if (is_Minus(left)) {
4019 ir_tarval *tv2 = tarval_sub(get_mode_null(mode), tv, NULL);
4021 if (tv2 != tarval_bad) {
4022 left = get_Minus_op(left);
4025 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_OP_C);
4032 if (relation == ir_relation_equal || relation == ir_relation_less_greater) {
4033 switch (get_irn_opcode(left)) {
4037 c1 = get_And_right(left);
4040 * And(x, C1) == C2 ==> FALSE if C2 & C1 != C2
4041 * And(x, C1) != C2 ==> TRUE if C2 & C1 != C2
4043 ir_tarval *mask = tarval_and(get_Const_tarval(c1), tv);
4045 /* TODO: move to constant evaluation */
4046 ir_graph *irg = get_irn_irg(n);
4047 tv = relation == ir_relation_equal ? get_tarval_b_false() : get_tarval_b_true();
4048 c1 = new_r_Const(irg, tv);
4049 DBG_OPT_CSTEVAL(n, c1);
4053 if (tarval_is_single_bit(tv)) {
4055 * optimization for AND:
4057 * And(x, C) == C ==> And(x, C) != 0
4058 * And(x, C) != C ==> And(X, C) == 0
4060 * if C is a single Bit constant.
4063 /* check for Constant's match. We have check hare the tarvals,
4064 because our const might be changed */
4065 if (get_Const_tarval(c1) == tv) {
4066 /* fine: do the transformation */
4067 tv = get_mode_null(get_tarval_mode(tv));
4068 relation ^= ir_relation_less_equal_greater;
4070 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_CNST_MAGN);
4076 c1 = get_Or_right(left);
4077 if (is_Const(c1) && tarval_is_null(tv)) {
4079 * Or(x, C) == 0 && C != 0 ==> FALSE
4080 * Or(x, C) != 0 && C != 0 ==> TRUE
4082 if (! tarval_is_null(get_Const_tarval(c1))) {
4083 /* TODO: move to constant evaluation */
4084 ir_graph *irg = get_irn_irg(n);
4085 tv = relation == ir_relation_equal ? get_tarval_b_false() : get_tarval_b_true();
4086 c1 = new_r_Const(irg, tv);
4087 DBG_OPT_CSTEVAL(n, c1);
4094 * optimize x << c1 == c into x & (-1 >>u c1) == c >> c1 if c & (-1 << c1) == c
4096 * optimize x << c1 != c into x & (-1 >>u c1) != c >> c1 if c & (-1 << c1) == c
4099 c1 = get_Shl_right(left);
4101 ir_graph *irg = get_irn_irg(c1);
4102 ir_tarval *tv1 = get_Const_tarval(c1);
4103 ir_mode *mode = get_irn_mode(left);
4104 ir_tarval *minus1 = get_mode_all_one(mode);
4105 ir_tarval *amask = tarval_shr(minus1, tv1);
4106 ir_tarval *cmask = tarval_shl(minus1, tv1);
4109 if (tarval_and(tv, cmask) != tv) {
4110 /* condition not met */
4111 tv = relation == ir_relation_equal ? get_tarval_b_false() : get_tarval_b_true();
4112 c1 = new_r_Const(irg, tv);
4113 DBG_OPT_CSTEVAL(n, c1);
4116 sl = get_Shl_left(left);
4117 blk = get_nodes_block(n);
4118 left = new_rd_And(get_irn_dbg_info(left), blk, sl, new_r_Const(irg, amask), mode);
4119 tv = tarval_shr(tv, tv1);
4121 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_SHF_TO_AND);
4126 * optimize x >>u c1 == c into x & (-1 << c1) == c << c1 if c & (-1 >>u c1) == c
4128 * optimize x >>u c1 != c into x & (-1 << c1) != c << c1 if c & (-1 >>u c1) == c
4131 c1 = get_Shr_right(left);
4133 ir_graph *irg = get_irn_irg(c1);
4134 ir_tarval *tv1 = get_Const_tarval(c1);
4135 ir_mode *mode = get_irn_mode(left);
4136 ir_tarval *minus1 = get_mode_all_one(mode);
4137 ir_tarval *amask = tarval_shl(minus1, tv1);
4138 ir_tarval *cmask = tarval_shr(minus1, tv1);
4141 if (tarval_and(tv, cmask) != tv) {
4142 /* condition not met */
4143 tv = relation == ir_relation_equal ? get_tarval_b_false() : get_tarval_b_true();
4144 c1 = new_r_Const(irg, tv);
4145 DBG_OPT_CSTEVAL(n, c1);
4148 sl = get_Shr_left(left);
4149 blk = get_nodes_block(n);
4150 left = new_rd_And(get_irn_dbg_info(left), blk, sl, new_r_Const(irg, amask), mode);
4151 tv = tarval_shl(tv, tv1);
4153 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_SHF_TO_AND);
4158 * optimize x >>s c1 == c into x & (-1 << c1) == c << c1 if (c >>s (BITS - c1)) \in {0,-1}
4160 * optimize x >>s c1 != c into x & (-1 << c1) != c << c1 if (c >>s (BITS - c1)) \in {0,-1}
4163 c1 = get_Shrs_right(left);
4165 ir_graph *irg = get_irn_irg(c1);
4166 ir_tarval *tv1 = get_Const_tarval(c1);
4167 ir_mode *mode = get_irn_mode(left);
4168 ir_tarval *minus1 = get_mode_all_one(mode);
4169 ir_tarval *amask = tarval_shl(minus1, tv1);
4170 ir_tarval *cond = new_tarval_from_long(get_mode_size_bits(mode), get_tarval_mode(tv1));
4173 cond = tarval_sub(cond, tv1, NULL);
4174 cond = tarval_shrs(tv, cond);
4176 if (!tarval_is_all_one(cond) && !tarval_is_null(cond)) {
4177 /* condition not met */
4178 tv = relation == ir_relation_equal ? get_tarval_b_false() : get_tarval_b_true();
4179 c1 = new_r_Const(irg, tv);
4180 DBG_OPT_CSTEVAL(n, c1);
4183 sl = get_Shrs_left(left);
4184 blk = get_nodes_block(n);
4185 left = new_rd_And(get_irn_dbg_info(left), blk, sl, new_r_Const(irg, amask), mode);
4186 tv = tarval_shl(tv, tv1);
4188 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_SHF_TO_AND);
4193 } /* tarval != bad */
4196 if (changedc) { /* need a new Const */
4197 ir_graph *irg = get_irn_irg(n);
4198 right = new_r_Const(irg, tv);
4202 if ((relation == ir_relation_equal || relation == ir_relation_less_greater) && is_Const(right) && is_Const_null(right) && is_Proj(left)) {
4203 ir_node *op = get_Proj_pred(left);
4205 if (is_Mod(op) && get_Proj_proj(left) == pn_Mod_res) {
4206 ir_node *c = get_binop_right(op);
4209 ir_tarval *tv = get_Const_tarval(c);
4211 if (tarval_is_single_bit(tv)) {
4212 /* special case: (x % 2^n) CMP 0 ==> x & (2^n-1) CMP 0 */
4213 ir_node *v = get_binop_left(op);
4214 ir_node *blk = get_irn_n(op, -1);
4215 ir_graph *irg = get_irn_irg(op);
4216 ir_mode *mode = get_irn_mode(v);
4218 tv = tarval_sub(tv, get_mode_one(mode), NULL);
4219 left = new_rd_And(get_irn_dbg_info(op), blk, v, new_r_Const(irg, tv), mode);
4221 DBG_OPT_ALGSIM0(n, n, FS_OPT_CMP_MOD_TO_AND);
4228 dbg_info *dbgi = get_irn_dbg_info(n);
4229 ir_node *block = get_nodes_block(n);
4231 /* create a new compare */
4232 n = new_rd_Cmp(dbgi, block, left, right, relation);
4239 * Optimize CopyB(mem, x, x) into a Nop.
4241 static ir_node *transform_node_Proj_CopyB(ir_node *proj)
4243 ir_node *copyb = get_Proj_pred(proj);
4244 ir_node *a = get_CopyB_dst(copyb);
4245 ir_node *b = get_CopyB_src(copyb);
4248 switch (get_Proj_proj(proj)) {
4249 case pn_CopyB_X_regular:
4250 /* Turn CopyB into a tuple (mem, jmp, bad, bad) */
4251 DBG_OPT_EXC_REM(proj);
4252 proj = new_r_Jmp(get_nodes_block(copyb));
4254 case pn_CopyB_X_except: {
4255 ir_graph *irg = get_irn_irg(proj);
4256 DBG_OPT_EXC_REM(proj);
4257 proj = new_r_Bad(irg, mode_X);
4265 } /* transform_node_Proj_CopyB */
4268 * Optimize Bounds(idx, idx, upper) into idx.
4270 static ir_node *transform_node_Proj_Bound(ir_node *proj)
4272 ir_node *oldn = proj;
4273 ir_node *bound = get_Proj_pred(proj);
4274 ir_node *idx = get_Bound_index(bound);
4275 ir_node *pred = skip_Proj(idx);
4278 if (idx == get_Bound_lower(bound))
4280 else if (is_Bound(pred)) {
4282 * idx was Bounds checked previously, it is still valid if
4283 * lower <= pred_lower && pred_upper <= upper.
4285 ir_node *lower = get_Bound_lower(bound);
4286 ir_node *upper = get_Bound_upper(bound);
4287 if (get_Bound_lower(pred) == lower &&
4288 get_Bound_upper(pred) == upper) {
4290 * One could expect that we simply return the previous
4291 * Bound here. However, this would be wrong, as we could
4292 * add an exception Proj to a new location then.
4293 * So, we must turn in into a tuple.
4299 /* Turn Bound into a tuple (mem, jmp, bad, idx) */
4300 switch (get_Proj_proj(proj)) {
4302 DBG_OPT_EXC_REM(proj);
4303 proj = get_Bound_mem(bound);
4305 case pn_Bound_X_except:
4306 DBG_OPT_EXC_REM(proj);
4307 proj = new_r_Bad(get_irn_irg(proj), mode_X);
4311 DBG_OPT_ALGSIM0(oldn, proj, FS_OPT_NOP);
4313 case pn_Bound_X_regular:
4314 DBG_OPT_EXC_REM(proj);
4315 proj = new_r_Jmp(get_nodes_block(bound));
4322 } /* transform_node_Proj_Bound */
4325 * Does all optimizations on nodes that must be done on its Projs
4326 * because of creating new nodes.
4328 static ir_node *transform_node_Proj(ir_node *proj)
4330 ir_node *n = get_Proj_pred(proj);
4332 if (n->op->ops.transform_node_Proj)
4333 return n->op->ops.transform_node_Proj(proj);
4335 } /* transform_node_Proj */
4337 static bool is_block_unreachable(const ir_node *block)
4339 const ir_graph *irg = get_irn_irg(block);
4340 if (!is_irg_state(irg, IR_GRAPH_STATE_BAD_BLOCK))
4342 return get_Block_dom_depth(block) < 0;
4345 static ir_node *transform_node_Block(ir_node *block)
4347 ir_graph *irg = get_irn_irg(block);
4348 int arity = get_irn_arity(block);
4349 ir_node *bad = NULL;
4352 if (!is_irg_state(irg, IR_GRAPH_STATE_BAD_BLOCK))
4355 for (i = 0; i < arity; ++i) {
4356 ir_node *pred = get_Block_cfgpred(block, i);
4357 ir_node *pred_block = get_nodes_block(pred);
4358 if (!is_Bad(pred) && !is_block_unreachable(pred_block))
4361 bad = new_r_Bad(irg, mode_X);
4362 set_irn_n(block, i, bad);
4368 static ir_node *transform_node_Phi(ir_node *phi)
4370 int n = get_irn_arity(phi);
4371 ir_mode *mode = get_irn_mode(phi);
4372 ir_node *block = get_nodes_block(phi);
4373 ir_graph *irg = get_irn_irg(phi);
4374 ir_node *bad = NULL;
4377 /* Set phi-operands for bad-block inputs to bad */
4378 for (i = 0; i < n; ++i) {
4379 ir_node *pred = get_Block_cfgpred(block, i);
4380 if (is_Bad(pred) || is_block_unreachable(get_nodes_block(pred))) {
4382 bad = new_r_Bad(irg, mode);
4383 set_irn_n(phi, i, bad);
4387 /* Move Confirms down through Phi nodes. */
4388 if (mode_is_reference(mode)) {
4389 n = get_irn_arity(phi);
4391 /* Beware of Phi0 */
4393 ir_node *pred = get_irn_n(phi, 0);
4394 ir_node *bound, *new_phi, *block, **in;
4395 ir_relation relation;
4397 if (! is_Confirm(pred))
4400 bound = get_Confirm_bound(pred);
4401 relation = get_Confirm_relation(pred);
4403 NEW_ARR_A(ir_node *, in, n);
4404 in[0] = get_Confirm_value(pred);
4406 for (i = 1; i < n; ++i) {
4407 pred = get_irn_n(phi, i);
4409 if (! is_Confirm(pred) ||
4410 get_Confirm_bound(pred) != bound ||
4411 get_Confirm_relation(pred) != relation)
4413 in[i] = get_Confirm_value(pred);
4415 /* move the Confirm nodes "behind" the Phi */
4416 block = get_irn_n(phi, -1);
4417 new_phi = new_r_Phi(block, n, in, get_irn_mode(phi));
4418 return new_r_Confirm(block, new_phi, bound, relation);
4425 * Returns the operands of a commutative bin-op, if one operand is
4426 * a const, it is returned as the second one.
4428 static void get_comm_Binop_Ops(ir_node *binop, ir_node **a, ir_node **c)
4430 ir_node *op_a = get_binop_left(binop);
4431 ir_node *op_b = get_binop_right(binop);
4433 assert(is_op_commutative(get_irn_op(binop)));
4435 if (is_Const(op_a)) {
4442 } /* get_comm_Binop_Ops */
4445 * Optimize a Or(And(Or(And(v,c4),c3),c2),c1) pattern if possible.
4446 * Such pattern may arise in bitfield stores.
4448 * value c4 value c4 & c2
4449 * AND c3 AND c1 | c3
4456 * AND c1 ===> OR if (c1 | c2) == 0x111..11
4459 static ir_node *transform_node_Or_bf_store(ir_node *irn_or)
4461 ir_node *irn_and, *c1;
4463 ir_node *and_l, *c3;
4464 ir_node *value, *c4;
4465 ir_node *new_and, *new_const, *block;
4466 ir_mode *mode = get_irn_mode(irn_or);
4468 ir_tarval *tv1, *tv2, *tv3, *tv4, *tv;
4472 get_comm_Binop_Ops(irn_or, &irn_and, &c1);
4473 if (!is_Const(c1) || !is_And(irn_and))
4476 get_comm_Binop_Ops(irn_and, &or_l, &c2);
4480 tv1 = get_Const_tarval(c1);
4481 tv2 = get_Const_tarval(c2);
4483 tv = tarval_or(tv1, tv2);
4484 if (tarval_is_all_one(tv)) {
4485 /* the AND does NOT clear a bit with isn't set by the OR */
4486 set_Or_left(irn_or, or_l);
4487 set_Or_right(irn_or, c1);
4489 /* check for more */
4496 get_comm_Binop_Ops(or_l, &and_l, &c3);
4497 if (!is_Const(c3) || !is_And(and_l))
4500 get_comm_Binop_Ops(and_l, &value, &c4);
4504 /* ok, found the pattern, check for conditions */
4505 assert(mode == get_irn_mode(irn_and));
4506 assert(mode == get_irn_mode(or_l));
4507 assert(mode == get_irn_mode(and_l));
4509 tv3 = get_Const_tarval(c3);
4510 tv4 = get_Const_tarval(c4);
4512 tv = tarval_or(tv4, tv2);
4513 if (!tarval_is_all_one(tv)) {
4514 /* have at least one 0 at the same bit position */
4518 if (tv3 != tarval_andnot(tv3, tv4)) {
4519 /* bit in the or_mask is outside the and_mask */
4523 if (tv1 != tarval_andnot(tv1, tv2)) {
4524 /* bit in the or_mask is outside the and_mask */
4528 /* ok, all conditions met */
4529 block = get_irn_n(irn_or, -1);
4530 irg = get_irn_irg(block);
4532 new_and = new_r_And(block, value, new_r_Const(irg, tarval_and(tv4, tv2)), mode);
4534 new_const = new_r_Const(irg, tarval_or(tv3, tv1));
4536 set_Or_left(irn_or, new_and);
4537 set_Or_right(irn_or, new_const);
4539 /* check for more */
4541 } /* transform_node_Or_bf_store */
4544 * Optimize an Or(shl(x, c), shr(x, bits - c)) into a Rotl
4546 static ir_node *transform_node_Or_Rotl(ir_node *irn_or)
4548 ir_mode *mode = get_irn_mode(irn_or);
4549 ir_node *shl, *shr, *block;
4550 ir_node *irn, *x, *c1, *c2, *n;
4551 ir_tarval *tv1, *tv2;
4553 /* some backends can't handle rotl */
4554 if (!be_get_backend_param()->support_rotl)
4557 if (! mode_is_int(mode))
4560 shl = get_binop_left(irn_or);
4561 shr = get_binop_right(irn_or);
4570 } else if (!is_Shl(shl)) {
4572 } else if (!is_Shr(shr)) {
4575 x = get_Shl_left(shl);
4576 if (x != get_Shr_left(shr))
4579 c1 = get_Shl_right(shl);
4580 c2 = get_Shr_right(shr);
4581 if (is_Const(c1) && is_Const(c2)) {
4582 tv1 = get_Const_tarval(c1);
4583 if (! tarval_is_long(tv1))
4586 tv2 = get_Const_tarval(c2);
4587 if (! tarval_is_long(tv2))
4590 if (get_tarval_long(tv1) + get_tarval_long(tv2)
4591 != (int) get_mode_size_bits(mode))
4594 /* yet, condition met */
4595 block = get_nodes_block(irn_or);
4597 n = new_r_Rotl(block, x, c1, mode);
4599 DBG_OPT_ALGSIM1(irn_or, shl, shr, n, FS_OPT_OR_SHFT_TO_ROTL);
4603 /* Note: the obvious rot formulation (a << x) | (a >> (32-x)) gets
4604 * transformed to (a << x) | (a >> -x) by transform_node_shift_modulo() */
4605 if (!ir_is_negated_value(c1, c2)) {
4609 /* yet, condition met */
4610 block = get_nodes_block(irn_or);
4611 n = new_r_Rotl(block, x, c1, mode);
4612 DBG_OPT_ALGSIM0(irn_or, n, FS_OPT_OR_SHFT_TO_ROTL);
4614 } /* transform_node_Or_Rotl */
4616 static bool is_cmp_unequal_zero(const ir_node *node)
4618 ir_relation relation = get_Cmp_relation(node);
4619 ir_node *left = get_Cmp_left(node);
4620 ir_node *right = get_Cmp_right(node);
4621 ir_mode *mode = get_irn_mode(left);
4623 if (!is_Const(right) || !is_Const_null(right))
4625 if (mode_is_signed(mode)) {
4626 return relation == ir_relation_less_greater;
4628 return relation == ir_relation_greater;
4635 static ir_node *transform_node_Or(ir_node *n)
4637 ir_node *c, *oldn = n;
4638 ir_node *a = get_Or_left(n);
4639 ir_node *b = get_Or_right(n);
4642 if (is_Not(a) && is_Not(b)) {
4643 /* ~a | ~b = ~(a&b) */
4644 ir_node *block = get_nodes_block(n);
4646 mode = get_irn_mode(n);
4649 n = new_rd_And(get_irn_dbg_info(n), block, a, b, mode);
4650 n = new_rd_Not(get_irn_dbg_info(n), block, n, mode);
4651 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_DEMORGAN);
4655 /* we can combine the relations of two compares with the same operands */
4656 if (is_Cmp(a) && is_Cmp(b)) {
4657 ir_node *a_left = get_Cmp_left(a);
4658 ir_node *a_right = get_Cmp_left(a);
4659 ir_node *b_left = get_Cmp_left(b);
4660 ir_node *b_right = get_Cmp_right(b);
4661 if (a_left == b_left && b_left == b_right) {
4662 dbg_info *dbgi = get_irn_dbg_info(n);
4663 ir_node *block = get_nodes_block(n);
4664 ir_relation a_relation = get_Cmp_relation(a);
4665 ir_relation b_relation = get_Cmp_relation(b);
4666 ir_relation new_relation = a_relation | b_relation;
4667 return new_rd_Cmp(dbgi, block, a_left, a_right, new_relation);
4669 /* Cmp(a!=0) or Cmp(b!=0) => Cmp(a|b != 0) */
4670 if (is_cmp_unequal_zero(a) && is_cmp_unequal_zero(b)
4671 && !mode_is_float(get_irn_mode(a_left))
4672 && !mode_is_float(get_irn_mode(b_left))) {
4673 ir_graph *irg = get_irn_irg(n);
4674 dbg_info *dbgi = get_irn_dbg_info(n);
4675 ir_node *block = get_nodes_block(n);
4676 ir_mode *mode = get_irn_mode(a_left);
4677 ir_node *n_b_left = get_irn_mode(b_left) != mode ?
4678 new_rd_Conv(dbgi, block, b_left, mode) : b_left;
4679 ir_node *or = new_rd_Or(dbgi, block, a_left, n_b_left, mode);
4680 ir_node *zero = create_zero_const(irg, mode);
4681 return new_rd_Cmp(dbgi, block, or, zero, ir_relation_less_greater);
4685 mode = get_irn_mode(n);
4686 HANDLE_BINOP_PHI((eval_func) tarval_or, a, b, c, mode);
4688 n = transform_node_Or_bf_store(n);
4689 n = transform_node_Or_Rotl(n);
4693 n = transform_bitwise_distributive(n, transform_node_Or);
4696 } /* transform_node_Or */
4700 static ir_node *transform_node(ir_node *n);
4703 * Optimize (a >> c1) >> c2), works for Shr, Shrs, Shl, Rotl.
4705 * Should be moved to reassociation?
4707 static ir_node *transform_node_shift(ir_node *n)
4709 ir_node *left, *right;
4711 ir_mode *count_mode;
4712 ir_tarval *tv1, *tv2, *res;
4713 ir_node *in[2], *irn, *block;
4717 left = get_binop_left(n);
4719 /* different operations */
4720 if (get_irn_op(left) != get_irn_op(n))
4723 right = get_binop_right(n);
4724 tv1 = value_of(right);
4725 if (tv1 == tarval_bad)
4728 tv2 = value_of(get_binop_right(left));
4729 if (tv2 == tarval_bad)
4732 count_mode = get_tarval_mode(tv1);
4733 if (get_tarval_mode(tv2) != count_mode) {
4734 /* TODO: search bigger mode or something and convert... */
4738 mode = get_irn_mode(n);
4739 modulo_shf = get_mode_modulo_shift(mode);
4741 if (modulo_shf > 0) {
4742 ir_tarval *modulo_mask = new_tarval_from_long(modulo_shf-1, count_mode);
4744 /* I'm not so sure what happens in one complement... */
4745 assert(get_mode_arithmetic(count_mode) == irma_twos_complement);
4746 /* modulo shifts should always be a power of 2 (otherwise modulo_mask
4747 * above will be invalid) */
4748 assert(modulo_shf<=0 || is_po2(modulo_shf));
4750 tv1 = tarval_and(tv1, modulo_mask);
4751 tv2 = tarval_and(tv2, modulo_mask);
4753 res = tarval_add(tv1, tv2);
4754 irg = get_irn_irg(n);
4756 /* beware: a simple replacement works only, if res < modulo shift */
4758 int bits = get_mode_size_bits(mode);
4759 ir_tarval *modulo = new_tarval_from_long(bits, count_mode);
4760 res = tarval_mod(res, modulo);
4762 long bits = get_mode_size_bits(mode);
4763 ir_tarval *mode_size = new_tarval_from_long(bits, count_mode);
4765 /* shifting too much */
4766 if (!(tarval_cmp(res, mode_size) & ir_relation_less)) {
4768 ir_node *block = get_nodes_block(n);
4769 dbg_info *dbgi = get_irn_dbg_info(n);
4770 ir_mode *smode = get_irn_mode(right);
4771 ir_node *cnst = new_r_Const_long(irg, smode, get_mode_size_bits(mode) - 1);
4772 return new_rd_Shrs(dbgi, block, get_binop_left(left), cnst, mode);
4775 return new_r_Const(irg, get_mode_null(mode));
4779 /* ok, we can replace it */
4780 assert(modulo_shf >= (int) get_mode_size_bits(mode));
4781 block = get_nodes_block(n);
4783 in[0] = get_binop_left(left);
4784 in[1] = new_r_Const(irg, res);
4786 irn = new_ir_node(NULL, get_Block_irg(block), block, get_irn_op(n), mode, 2, in);
4788 DBG_OPT_ALGSIM0(n, irn, FS_OPT_REASSOC_SHIFT);
4790 return transform_node(irn);
4794 * normalisation: (x & c1) >> c2 to (x >> c2) & (c1 >> c2)
4796 * - and, or, xor instead of &
4797 * - Shl, Shr, Shrs, rotl instead of >>
4798 * (with a special case for Or/Xor + Shrs)
4800 static ir_node *transform_node_bitop_shift(ir_node *n)
4803 ir_node *right = get_binop_right(n);
4804 ir_mode *mode = get_irn_mode(n);
4805 ir_node *bitop_left;
4806 ir_node *bitop_right;
4816 ir_tarval *tv_shift;
4818 assert(is_Shrs(n) || is_Shr(n) || is_Shl(n) || is_Rotl(n));
4820 if (!is_Const(right))
4823 left = get_binop_left(n);
4824 op_left = get_irn_op(left);
4825 if (op_left != op_And && op_left != op_Or && op_left != op_Eor)
4828 /* doing it with Shrs is not legal if the Or/Eor affects the topmost bit */
4829 if (is_Shrs(n) && (op_left == op_Or || op_left == op_Eor)) {
4830 /* TODO: test if sign bit is affectes */
4834 bitop_right = get_binop_right(left);
4835 if (!is_Const(bitop_right))
4838 bitop_left = get_binop_left(left);
4840 block = get_nodes_block(n);
4841 dbgi = get_irn_dbg_info(n);
4842 tv1 = get_Const_tarval(bitop_right);
4843 tv2 = get_Const_tarval(right);
4845 assert(get_tarval_mode(tv1) == mode);
4848 new_shift = new_rd_Shl(dbgi, block, bitop_left, right, mode);
4849 tv_shift = tarval_shl(tv1, tv2);
4850 } else if (is_Shr(n)) {
4851 new_shift = new_rd_Shr(dbgi, block, bitop_left, right, mode);
4852 tv_shift = tarval_shr(tv1, tv2);
4853 } else if (is_Shrs(n)) {
4854 new_shift = new_rd_Shrs(dbgi, block, bitop_left, right, mode);
4855 tv_shift = tarval_shrs(tv1, tv2);
4858 new_shift = new_rd_Rotl(dbgi, block, bitop_left, right, mode);
4859 tv_shift = tarval_rotl(tv1, tv2);
4862 assert(get_tarval_mode(tv_shift) == mode);
4863 irg = get_irn_irg(n);
4864 new_const = new_r_Const(irg, tv_shift);
4866 if (op_left == op_And) {
4867 new_bitop = new_rd_And(dbgi, block, new_shift, new_const, mode);
4868 } else if (op_left == op_Or) {
4869 new_bitop = new_rd_Or(dbgi, block, new_shift, new_const, mode);
4871 assert(op_left == op_Eor);
4872 new_bitop = new_rd_Eor(dbgi, block, new_shift, new_const, mode);
4880 * (x << c1) >> c2 <=> x OP (c2-c1) & ((-1 << c1) >> c2)
4882 * (x >> c1) << c2 <=> x OP (c2-c1) & ((-1 >> c1) << c2)
4883 * (also with x >>s c1 when c1>=c2)
4885 static ir_node *transform_node_shl_shr(ir_node *n)
4888 ir_node *right = get_binop_right(n);
4898 ir_tarval *tv_shift;
4901 ir_relation relation;
4904 assert(is_Shl(n) || is_Shr(n) || is_Shrs(n));
4906 if (!is_Const(right))
4909 left = get_binop_left(n);
4910 mode = get_irn_mode(n);
4911 if (is_Shl(n) && (is_Shr(left) || is_Shrs(left))) {
4912 ir_node *shr_right = get_binop_right(left);
4914 if (!is_Const(shr_right))
4917 x = get_binop_left(left);
4918 tv_shr = get_Const_tarval(shr_right);
4919 tv_shl = get_Const_tarval(right);
4921 if (is_Shrs(left)) {
4922 /* shrs variant only allowed if c1 >= c2 */
4923 if (! (tarval_cmp(tv_shl, tv_shr) & ir_relation_greater_equal))
4926 tv_mask = tarval_shrs(get_mode_all_one(mode), tv_shr);
4929 tv_mask = tarval_shr(get_mode_all_one(mode), tv_shr);
4931 tv_mask = tarval_shl(tv_mask, tv_shl);
4932 } else if (is_Shr(n) && is_Shl(left)) {
4933 ir_node *shl_right = get_Shl_right(left);
4935 if (!is_Const(shl_right))
4938 x = get_Shl_left(left);
4939 tv_shr = get_Const_tarval(right);
4940 tv_shl = get_Const_tarval(shl_right);
4942 tv_mask = tarval_shl(get_mode_all_one(mode), tv_shl);
4943 tv_mask = tarval_shr(tv_mask, tv_shr);
4948 if (get_tarval_mode(tv_shl) != get_tarval_mode(tv_shr)) {
4949 tv_shl = tarval_convert_to(tv_shl, get_tarval_mode(tv_shr));
4952 assert(tv_mask != tarval_bad);
4953 assert(get_tarval_mode(tv_mask) == mode);
4955 block = get_nodes_block(n);
4956 irg = get_irn_irg(block);
4957 dbgi = get_irn_dbg_info(n);
4959 relation = tarval_cmp(tv_shl, tv_shr);
4960 if (relation == ir_relation_less || relation == ir_relation_equal) {
4961 tv_shift = tarval_sub(tv_shr, tv_shl, NULL);
4962 new_const = new_r_Const(irg, tv_shift);
4964 new_shift = new_rd_Shrs(dbgi, block, x, new_const, mode);
4966 new_shift = new_rd_Shr(dbgi, block, x, new_const, mode);
4969 assert(relation == ir_relation_greater);
4970 tv_shift = tarval_sub(tv_shl, tv_shr, NULL);
4971 new_const = new_r_Const(irg, tv_shift);
4972 new_shift = new_rd_Shl(dbgi, block, x, new_const, mode);
4975 new_const = new_r_Const(irg, tv_mask);
4976 new_and = new_rd_And(dbgi, block, new_shift, new_const, mode);
4981 static ir_tarval *get_modulo_tv_value(ir_tarval *tv, int modulo_val)
4983 ir_mode *mode = get_tarval_mode(tv);
4984 ir_tarval *modulo_tv = new_tarval_from_long(modulo_val, mode);
4985 return tarval_mod(tv, modulo_tv);
4988 typedef ir_node*(*new_shift_func)(dbg_info *dbgi, ir_node *block,
4989 ir_node *left, ir_node *right, ir_mode *mode);
4992 * Normalisation: if we have a shl/shr with modulo_shift behaviour
4993 * then we can use that to minimize the value of Add(x, const) or
4994 * Sub(Const, x). In particular this often avoids 1 instruction in some
4995 * backends for the Shift(x, Sub(Const, y)) case because it can be replaced
4996 * by Shift(x, Minus(y)) which doesnt't need an explicit Const constructed.
4998 static ir_node *transform_node_shift_modulo(ir_node *n,
4999 new_shift_func new_shift)
5001 ir_mode *mode = get_irn_mode(n);
5002 int modulo = get_mode_modulo_shift(mode);
5003 ir_node *newop = NULL;
5004 ir_mode *mode_right;
5011 if (get_mode_arithmetic(mode) != irma_twos_complement)
5013 if (!is_po2(modulo))
5016 irg = get_irn_irg(n);
5017 block = get_nodes_block(n);
5018 right = get_binop_right(n);
5019 mode_right = get_irn_mode(right);
5020 if (is_Const(right)) {
5021 ir_tarval *tv = get_Const_tarval(right);
5022 ir_tarval *tv_mod = get_modulo_tv_value(tv, modulo);
5027 newop = new_r_Const(irg, tv_mod);
5028 } else if (is_Add(right)) {
5029 ir_node *add_right = get_Add_right(right);
5030 if (is_Const(add_right)) {
5031 ir_tarval *tv = get_Const_tarval(add_right);
5032 ir_tarval *tv_mod = get_modulo_tv_value(tv, modulo);
5037 newconst = new_r_Const(irg, tv_mod);
5038 newop = new_r_Add(block, get_Add_left(right), newconst,
5041 } else if (is_Sub(right)) {
5042 ir_node *sub_left = get_Sub_left(right);
5043 if (is_Const(sub_left)) {
5044 ir_tarval *tv = get_Const_tarval(sub_left);
5045 ir_tarval *tv_mod = get_modulo_tv_value(tv, modulo);
5050 newconst = new_r_Const(irg, tv_mod);
5051 newop = new_r_Sub(block, newconst, get_Sub_right(right),
5058 if (newop != NULL) {
5059 dbg_info *dbgi = get_irn_dbg_info(n);
5060 ir_node *left = get_binop_left(n);
5061 return new_shift(dbgi, block, left, newop, mode);
5069 static ir_node *transform_node_Shr(ir_node *n)
5071 ir_node *c, *oldn = n;
5072 ir_node *left = get_Shr_left(n);
5073 ir_node *right = get_Shr_right(n);
5074 ir_mode *mode = get_irn_mode(n);
5076 HANDLE_BINOP_PHI((eval_func) tarval_shr, left, right, c, mode);
5077 n = transform_node_shift(n);
5080 n = transform_node_shift_modulo(n, new_rd_Shr);
5082 n = transform_node_shl_shr(n);
5084 n = transform_node_bitop_shift(n);
5087 } /* transform_node_Shr */
5092 static ir_node *transform_node_Shrs(ir_node *n)
5094 ir_node *c, *oldn = n;
5095 ir_node *a = get_Shrs_left(n);
5096 ir_node *b = get_Shrs_right(n);
5097 ir_mode *mode = get_irn_mode(n);
5099 if (is_oversize_shift(n)) {
5100 ir_node *block = get_nodes_block(n);
5101 dbg_info *dbgi = get_irn_dbg_info(n);
5102 ir_mode *cmode = get_irn_mode(b);
5103 long val = get_mode_size_bits(cmode)-1;
5104 ir_graph *irg = get_irn_irg(n);
5105 ir_node *cnst = new_r_Const_long(irg, cmode, val);
5106 return new_rd_Shrs(dbgi, block, a, cnst, mode);
5109 HANDLE_BINOP_PHI((eval_func) tarval_shrs, a, b, c, mode);
5110 n = transform_node_shift(n);
5113 n = transform_node_shift_modulo(n, new_rd_Shrs);
5115 n = transform_node_bitop_shift(n);
5118 } /* transform_node_Shrs */
5123 static ir_node *transform_node_Shl(ir_node *n)
5125 ir_node *c, *oldn = n;
5126 ir_node *a = get_Shl_left(n);
5127 ir_node *b = get_Shl_right(n);
5128 ir_mode *mode = get_irn_mode(n);
5130 HANDLE_BINOP_PHI((eval_func) tarval_shl, a, b, c, mode);
5131 n = transform_node_shift(n);
5134 n = transform_node_shift_modulo(n, new_rd_Shl);
5136 n = transform_node_shl_shr(n);
5138 n = transform_node_bitop_shift(n);
5141 } /* transform_node_Shl */
5146 static ir_node *transform_node_Rotl(ir_node *n)
5148 ir_node *c, *oldn = n;
5149 ir_node *a = get_Rotl_left(n);
5150 ir_node *b = get_Rotl_right(n);
5151 ir_mode *mode = get_irn_mode(n);
5153 HANDLE_BINOP_PHI((eval_func) tarval_rotl, a, b, c, mode);
5154 n = transform_node_shift(n);
5157 n = transform_node_bitop_shift(n);
5160 } /* transform_node_Rotl */
5165 static ir_node *transform_node_Conv(ir_node *n)
5167 ir_node *c, *oldn = n;
5168 ir_mode *mode = get_irn_mode(n);
5169 ir_node *a = get_Conv_op(n);
5171 if (mode != mode_b && is_const_Phi(a)) {
5172 /* Do NOT optimize mode_b Conv's, this leads to remaining
5173 * Phib nodes later, because the conv_b_lower operation
5174 * is instantly reverted, when it tries to insert a Convb.
5176 c = apply_conv_on_phi(a, mode);
5178 DBG_OPT_ALGSIM0(oldn, c, FS_OPT_CONST_PHI);
5183 if (is_Unknown(a)) { /* Conv_A(Unknown_B) -> Unknown_A */
5184 ir_graph *irg = get_irn_irg(n);
5185 return new_r_Unknown(irg, mode);
5188 if (mode_is_reference(mode) &&
5189 get_mode_size_bits(mode) == get_mode_size_bits(get_irn_mode(a)) &&
5191 ir_node *l = get_Add_left(a);
5192 ir_node *r = get_Add_right(a);
5193 dbg_info *dbgi = get_irn_dbg_info(a);
5194 ir_node *block = get_nodes_block(n);
5196 ir_node *lop = get_Conv_op(l);
5197 if (get_irn_mode(lop) == mode) {
5198 /* ConvP(AddI(ConvI(P), x)) -> AddP(P, x) */
5199 n = new_rd_Add(dbgi, block, lop, r, mode);
5204 ir_node *rop = get_Conv_op(r);
5205 if (get_irn_mode(rop) == mode) {
5206 /* ConvP(AddI(x, ConvI(P))) -> AddP(x, P) */
5207 n = new_rd_Add(dbgi, block, l, rop, mode);
5214 } /* transform_node_Conv */
5217 * Remove dead blocks and nodes in dead blocks
5218 * in keep alive list. We do not generate a new End node.
5220 static ir_node *transform_node_End(ir_node *n)
5222 int i, j, n_keepalives = get_End_n_keepalives(n);
5225 NEW_ARR_A(ir_node *, in, n_keepalives);
5227 for (i = j = 0; i < n_keepalives; ++i) {
5228 ir_node *ka = get_End_keepalive(n, i);
5230 /* no need to keep Bad */
5233 /* dont keep unreachable code */
5234 block = is_Block(ka) ? ka : get_nodes_block(ka);
5235 if (is_block_unreachable(block))
5239 if (j != n_keepalives)
5240 set_End_keepalives(n, j, in);
5242 } /* transform_node_End */
5244 int ir_is_negated_value(const ir_node *a, const ir_node *b)
5246 if (is_Minus(a) && get_Minus_op(a) == b)
5248 if (is_Minus(b) && get_Minus_op(b) == a)
5250 if (is_Sub(a) && is_Sub(b)) {
5251 ir_node *a_left = get_Sub_left(a);
5252 ir_node *a_right = get_Sub_right(a);
5253 ir_node *b_left = get_Sub_left(b);
5254 ir_node *b_right = get_Sub_right(b);
5256 if (a_left == b_right && a_right == b_left)
5264 * Optimize a Mux into some simpler cases.
5266 static ir_node *transform_node_Mux(ir_node *n)
5268 ir_node *oldn = n, *sel = get_Mux_sel(n);
5269 ir_mode *mode = get_irn_mode(n);
5270 ir_node *t = get_Mux_true(n);
5271 ir_node *f = get_Mux_false(n);
5272 ir_graph *irg = get_irn_irg(n);
5274 if (is_irg_state(irg, IR_GRAPH_STATE_KEEP_MUX))
5278 ir_node* block = get_nodes_block(n);
5280 ir_node* c1 = get_Mux_sel(t);
5281 ir_node* t1 = get_Mux_true(t);
5282 ir_node* f1 = get_Mux_false(t);
5284 /* Mux(cond0, Mux(cond1, x, y), y) -> typical if (cond0 && cond1) x else y */
5285 ir_node* and_ = new_r_And(block, c0, c1, mode_b);
5286 ir_node* new_mux = new_r_Mux(block, and_, f1, t1, mode);
5291 DBG_OPT_ALGSIM0(oldn, t, FS_OPT_MUX_COMBINE);
5292 } else if (f == t1) {
5293 /* Mux(cond0, Mux(cond1, x, y), x) */
5294 ir_node* not_c1 = new_r_Not(block, c1, mode_b);
5295 ir_node* and_ = new_r_And(block, c0, not_c1, mode_b);
5296 ir_node* new_mux = new_r_Mux(block, and_, t1, f1, mode);
5301 DBG_OPT_ALGSIM0(oldn, t, FS_OPT_MUX_COMBINE);
5303 } else if (is_Mux(f)) {
5304 ir_node* block = get_nodes_block(n);
5306 ir_node* c1 = get_Mux_sel(f);
5307 ir_node* t1 = get_Mux_true(f);
5308 ir_node* f1 = get_Mux_false(f);
5310 /* Mux(cond0, x, Mux(cond1, x, y)) -> typical if (cond0 || cond1) x else y */
5311 ir_node* or_ = new_r_Or(block, c0, c1, mode_b);
5312 ir_node* new_mux = new_r_Mux(block, or_, f1, t1, mode);
5317 DBG_OPT_ALGSIM0(oldn, f, FS_OPT_MUX_COMBINE);
5318 } else if (t == f1) {
5319 /* Mux(cond0, x, Mux(cond1, y, x)) */
5320 ir_node* not_c1 = new_r_Not(block, c1, mode_b);
5321 ir_node* or_ = new_r_Or(block, c0, not_c1, mode_b);
5322 ir_node* new_mux = new_r_Mux(block, or_, t1, f1, mode);
5327 DBG_OPT_ALGSIM0(oldn, f, FS_OPT_MUX_COMBINE);
5331 /* first normalization step: try to move a constant to the false side,
5332 * 0 preferred on false side too */
5333 if (is_Cmp(sel) && is_Const(t) &&
5334 (!is_Const(f) || (is_Const_null(t) && !is_Const_null(f)))) {
5335 dbg_info *seldbgi = get_irn_dbg_info(sel);
5336 ir_node *block = get_nodes_block(sel);
5337 ir_relation relation = get_Cmp_relation(sel);
5342 /* Mux(x, a, b) => Mux(not(x), b, a) */
5343 relation = get_negated_relation(relation);
5344 sel = new_rd_Cmp(seldbgi, block, get_Cmp_left(sel),
5345 get_Cmp_right(sel), relation);
5346 n = new_rd_Mux(get_irn_dbg_info(n), get_nodes_block(n), sel, f, t, mode);
5349 /* note: after normalization, false can only happen on default */
5350 if (mode == mode_b) {
5351 dbg_info *dbg = get_irn_dbg_info(n);
5352 ir_node *block = get_nodes_block(n);
5355 ir_tarval *tv_t = get_Const_tarval(t);
5356 if (tv_t == tarval_b_true) {
5358 /* Muxb(sel, true, false) = sel */
5359 assert(get_Const_tarval(f) == tarval_b_false);
5360 DBG_OPT_ALGSIM0(oldn, sel, FS_OPT_MUX_BOOL);
5363 /* Muxb(sel, true, x) = Or(sel, x) */
5364 n = new_rd_Or(dbg, block, sel, f, mode_b);
5365 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_OR_BOOL);
5369 } else if (is_Const(f)) {
5370 ir_tarval *tv_f = get_Const_tarval(f);
5371 if (tv_f == tarval_b_true) {
5372 /* Muxb(sel, x, true) = Or(Not(sel), x) */
5373 ir_node* not_sel = new_rd_Not(dbg, block, sel, mode_b);
5374 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_ORNOT_BOOL);
5375 n = new_rd_Or(dbg, block, not_sel, t, mode_b);
5378 /* Muxb(sel, x, false) = And(sel, x) */
5379 assert(tv_f == tarval_b_false);
5380 n = new_rd_And(dbg, block, sel, t, mode_b);
5381 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_AND_BOOL);
5387 /* more normalization: Mux(sel, 0, 1) is simply a conv from the mode_b
5388 * value to integer. */
5389 if (is_Const(t) && is_Const(f) && mode_is_int(mode)) {
5390 ir_tarval *a = get_Const_tarval(t);
5391 ir_tarval *b = get_Const_tarval(f);
5393 if (tarval_is_one(a) && tarval_is_null(b)) {
5394 ir_node *block = get_nodes_block(n);
5395 ir_node *conv = new_r_Conv(block, sel, mode);
5397 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_CONV);
5399 } else if (tarval_is_null(a) && tarval_is_one(b)) {
5400 ir_node *block = get_nodes_block(n);
5401 ir_node *not_ = new_r_Not(block, sel, mode_b);
5402 ir_node *conv = new_r_Conv(block, not_, mode);
5404 DBG_OPT_ALGSIM0(oldn, n, FS_OPT_MUX_CONV);
5410 ir_node *cmp_r = get_Cmp_right(sel);
5411 if (is_Const(cmp_r) && is_Const_null(cmp_r)) {
5412 ir_node *block = get_nodes_block(n);
5413 ir_node *cmp_l = get_Cmp_left(sel);
5415 if (mode_is_int(mode)) {
5416 ir_relation relation = get_Cmp_relation(sel);
5418 if ((relation == ir_relation_less_greater || relation == ir_relation_equal) && is_And(cmp_l)) {
5419 /* Mux((a & b) != 0, c, 0) */
5420 ir_node *and_r = get_And_right(cmp_l);
5423 if (and_r == t && f == cmp_r) {
5424 if (is_Const(t) && tarval_is_single_bit(get_Const_tarval(t))) {
5425 if (relation == ir_relation_less_greater) {
5426 /* Mux((a & 2^C) != 0, 2^C, 0) */
5428 DBG_OPT_ALGSIM1(oldn, sel, sel, n, FS_OPT_MUX_TO_BITOP);
5430 /* Mux((a & 2^C) == 0, 2^C, 0) */
5431 n = new_rd_Eor(get_irn_dbg_info(n),
5432 block, cmp_l, t, mode);
5433 DBG_OPT_ALGSIM1(oldn, sel, sel, n, FS_OPT_MUX_TO_BITOP);
5438 if (is_Shl(and_r)) {
5439 ir_node *shl_l = get_Shl_left(and_r);
5440 if (is_Const(shl_l) && is_Const_one(shl_l)) {
5441 if (and_r == t && f == cmp_r) {
5442 if (relation == ir_relation_less_greater) {
5443 /* (a & (1 << n)) != 0, (1 << n), 0) */
5445 DBG_OPT_ALGSIM1(oldn, sel, sel, n, FS_OPT_MUX_TO_BITOP);
5447 /* (a & (1 << n)) == 0, (1 << n), 0) */
5448 n = new_rd_Eor(get_irn_dbg_info(n),
5449 block, cmp_l, t, mode);
5450 DBG_OPT_ALGSIM1(oldn, sel, sel, n, FS_OPT_MUX_TO_BITOP);
5456 and_l = get_And_left(cmp_l);
5457 if (is_Shl(and_l)) {
5458 ir_node *shl_l = get_Shl_left(and_l);
5459 if (is_Const(shl_l) && is_Const_one(shl_l)) {
5460 if (and_l == t && f == cmp_r) {
5461 if (relation == ir_relation_less_greater) {
5462 /* ((1 << n) & a) != 0, (1 << n), 0) */
5464 DBG_OPT_ALGSIM1(oldn, sel, sel, n, FS_OPT_MUX_TO_BITOP);
5466 /* ((1 << n) & a) == 0, (1 << n), 0) */
5467 n = new_rd_Eor(get_irn_dbg_info(n),
5468 block, cmp_l, t, mode);
5469 DBG_OPT_ALGSIM1(oldn, sel, sel, n, FS_OPT_MUX_TO_BITOP);
5484 * optimize Sync nodes that have other syncs as input we simply add the inputs
5485 * of the other sync to our own inputs
5487 static ir_node *transform_node_Sync(ir_node *n)
5489 int arity = get_Sync_n_preds(n);
5492 for (i = 0; i < arity;) {
5493 ir_node *pred = get_Sync_pred(n, i);
5497 /* Remove Bad predecessors */
5504 /* Remove duplicate predecessors */
5505 for (j = 0; j < i; ++j) {
5506 if (get_Sync_pred(n, j) == pred) {
5515 if (!is_Sync(pred)) {
5523 pred_arity = get_Sync_n_preds(pred);
5524 for (j = 0; j < pred_arity; ++j) {
5525 ir_node *pred_pred = get_Sync_pred(pred, j);
5530 add_irn_n(n, pred_pred);
5534 if (get_Sync_pred(n, k) == pred_pred) break;
5540 ir_graph *irg = get_irn_irg(n);
5541 return new_r_Bad(irg, mode_M);
5544 return get_Sync_pred(n, 0);
5547 /* rehash the sync node */
5552 static ir_node *transform_node_Load(ir_node *n)
5554 /* if our memory predecessor is a load from the same address, then reuse the
5555 * previous result */
5556 ir_node *mem = get_Load_mem(n);
5561 /* don't touch volatile loads */
5562 if (get_Load_volatility(n) == volatility_is_volatile)
5564 mem_pred = get_Proj_pred(mem);
5565 if (is_Load(mem_pred)) {
5566 ir_node *pred_load = mem_pred;
5568 /* conservatively compare the 2 loads. TODO: This could be less strict
5569 * with fixup code in some situations (like smaller/bigger modes) */
5570 if (get_Load_ptr(pred_load) != get_Load_ptr(n))
5572 if (get_Load_mode(pred_load) != get_Load_mode(n))
5574 /* all combinations of aligned/unaligned pred/n should be fine so we do
5575 * not compare the unaligned attribute */
5577 ir_node *block = get_nodes_block(n);
5578 ir_node *jmp = new_r_Jmp(block);
5579 ir_graph *irg = get_irn_irg(n);
5580 ir_node *bad = new_r_Bad(irg, mode_X);
5581 ir_mode *mode = get_Load_mode(n);
5582 ir_node *res = new_r_Proj(pred_load, mode, pn_Load_res);
5583 ir_node *in[pn_Load_max] = { mem, jmp, bad, res };
5584 ir_node *tuple = new_r_Tuple(block, ARRAY_SIZE(in), in);
5587 } else if (is_Store(mem_pred)) {
5588 ir_node *pred_store = mem_pred;
5589 ir_node *value = get_Store_value(pred_store);
5591 if (get_Store_ptr(pred_store) != get_Load_ptr(n))
5593 if (get_irn_mode(value) != get_Load_mode(n))
5595 /* all combinations of aligned/unaligned pred/n should be fine so we do
5596 * not compare the unaligned attribute */
5598 ir_node *block = get_nodes_block(n);
5599 ir_node *jmp = new_r_Jmp(block);
5600 ir_graph *irg = get_irn_irg(n);
5601 ir_node *bad = new_r_Bad(irg, mode_X);
5602 ir_node *res = value;
5603 ir_node *in[pn_Load_max] = { mem, jmp, bad, res };
5604 ir_node *tuple = new_r_Tuple(block, ARRAY_SIZE(in), in);
5613 * optimize a trampoline Call into a direct Call
5615 static ir_node *transform_node_Call(ir_node *call)
5617 ir_node *callee = get_Call_ptr(call);
5618 ir_node *adr, *mem, *res, *bl, **in;
5619 ir_type *ctp, *mtp, *tp;
5623 size_t i, n_res, n_param;
5626 if (! is_Proj(callee))
5628 callee = get_Proj_pred(callee);
5629 if (! is_Builtin(callee))
5631 if (get_Builtin_kind(callee) != ir_bk_inner_trampoline)
5634 mem = get_Call_mem(call);
5636 if (skip_Proj(mem) == callee) {
5637 /* memory is routed to the trampoline, skip */
5638 mem = get_Builtin_mem(callee);
5641 /* build a new call type */
5642 mtp = get_Call_type(call);
5643 tdb = get_type_dbg_info(mtp);
5645 n_res = get_method_n_ress(mtp);
5646 n_param = get_method_n_params(mtp);
5647 ctp = new_d_type_method(n_param + 1, n_res, tdb);
5649 for (i = 0; i < n_res; ++i)
5650 set_method_res_type(ctp, i, get_method_res_type(mtp, i));
5652 NEW_ARR_A(ir_node *, in, n_param + 1);
5654 /* FIXME: we don't need a new pointer type in every step */
5655 irg = get_irn_irg(call);
5656 tp = get_irg_frame_type(irg);
5657 tp = new_type_pointer(tp);
5658 set_method_param_type(ctp, 0, tp);
5660 in[0] = get_Builtin_param(callee, 2);
5661 for (i = 0; i < n_param; ++i) {
5662 set_method_param_type(ctp, i + 1, get_method_param_type(mtp, i));
5663 in[i + 1] = get_Call_param(call, i);
5665 var = get_method_variadicity(mtp);
5666 set_method_variadicity(ctp, var);
5667 /* When we resolve a trampoline, the function must be called by a this-call */
5668 set_method_calling_convention(ctp, get_method_calling_convention(mtp) | cc_this_call);
5669 set_method_additional_properties(ctp, get_method_additional_properties(mtp));
5671 adr = get_Builtin_param(callee, 1);
5673 db = get_irn_dbg_info(call);
5674 bl = get_nodes_block(call);
5676 res = new_rd_Call(db, bl, mem, adr, n_param + 1, in, ctp);
5677 if (get_irn_pinned(call) == op_pin_state_floats)
5678 set_irn_pinned(res, op_pin_state_floats);
5680 } /* transform_node_Call */
5683 * Tries several [inplace] [optimizing] transformations and returns an
5684 * equivalent node. The difference to equivalent_node() is that these
5685 * transformations _do_ generate new nodes, and thus the old node must
5686 * not be freed even if the equivalent node isn't the old one.
5688 static ir_node *transform_node(ir_node *n)
5693 * Transform_node is the only "optimizing transformation" that might
5694 * return a node with a different opcode. We iterate HERE until fixpoint
5695 * to get the final result.
5699 if (n->op->ops.transform_node != NULL)
5700 n = n->op->ops.transform_node(n);
5701 } while (oldn != n);
5704 } /* transform_node */
5707 * Sets the default transform node operation for an ir_op_ops.
5709 * @param code the opcode for the default operation
5710 * @param ops the operations initialized
5715 static ir_op_ops *firm_set_default_transform_node(ir_opcode code, ir_op_ops *ops)
5719 ops->transform_node = transform_node_##a; \
5721 #define CASE_PROJ(a) \
5723 ops->transform_node_Proj = transform_node_Proj_##a; \
5725 #define CASE_PROJ_EX(a) \
5727 ops->transform_node = transform_node_##a; \
5728 ops->transform_node_Proj = transform_node_Proj_##a; \
5769 } /* firm_set_default_transform_node */
5772 /* **************** Common Subexpression Elimination **************** */
5774 /** The size of the hash table used, should estimate the number of nodes
5776 #define N_IR_NODES 512
5778 /** Compares the attributes of two Const nodes. */
5779 static int node_cmp_attr_Const(const ir_node *a, const ir_node *b)
5781 return get_Const_tarval(a) != get_Const_tarval(b);
5784 /** Compares the attributes of two Proj nodes. */
5785 static int node_cmp_attr_Proj(const ir_node *a, const ir_node *b)
5787 return a->attr.proj.proj != b->attr.proj.proj;
5790 /** Compares the attributes of two Alloc nodes. */
5791 static int node_cmp_attr_Alloc(const ir_node *a, const ir_node *b)
5793 const alloc_attr *pa = &a->attr.alloc;
5794 const alloc_attr *pb = &b->attr.alloc;
5795 return (pa->where != pb->where) || (pa->type != pb->type);
5798 /** Compares the attributes of two Free nodes. */
5799 static int node_cmp_attr_Free(const ir_node *a, const ir_node *b)
5801 const free_attr *pa = &a->attr.free;
5802 const free_attr *pb = &b->attr.free;
5803 return (pa->where != pb->where) || (pa->type != pb->type);
5806 /** Compares the attributes of two SymConst nodes. */
5807 static int node_cmp_attr_SymConst(const ir_node *a, const ir_node *b)
5809 const symconst_attr *pa = &a->attr.symc;
5810 const symconst_attr *pb = &b->attr.symc;
5811 return (pa->kind != pb->kind)
5812 || (pa->sym.type_p != pb->sym.type_p);
5815 /** Compares the attributes of two Call nodes. */
5816 static int node_cmp_attr_Call(const ir_node *a, const ir_node *b)
5818 const call_attr *pa = &a->attr.call;
5819 const call_attr *pb = &b->attr.call;
5820 return (pa->type != pb->type)
5821 || (pa->tail_call != pb->tail_call);
5824 /** Compares the attributes of two Sel nodes. */
5825 static int node_cmp_attr_Sel(const ir_node *a, const ir_node *b)
5827 const ir_entity *a_ent = get_Sel_entity(a);
5828 const ir_entity *b_ent = get_Sel_entity(b);
5829 return a_ent != b_ent;
5832 /** Compares the attributes of two Phi nodes. */
5833 static int node_cmp_attr_Phi(const ir_node *a, const ir_node *b)
5835 /* we can only enter this function if both nodes have the same number of inputs,
5836 hence it is enough to check if one of them is a Phi0 */
5838 /* check the Phi0 pos attribute */
5839 return a->attr.phi.u.pos != b->attr.phi.u.pos;
5844 /** Compares the attributes of two Conv nodes. */
5845 static int node_cmp_attr_Conv(const ir_node *a, const ir_node *b)
5847 return get_Conv_strict(a) != get_Conv_strict(b);
5850 /** Compares the attributes of two Cast nodes. */
5851 static int node_cmp_attr_Cast(const ir_node *a, const ir_node *b)
5853 return get_Cast_type(a) != get_Cast_type(b);
5856 /** Compares the attributes of two Load nodes. */
5857 static int node_cmp_attr_Load(const ir_node *a, const ir_node *b)
5859 if (get_Load_volatility(a) == volatility_is_volatile ||
5860 get_Load_volatility(b) == volatility_is_volatile)
5861 /* NEVER do CSE on volatile Loads */
5863 /* do not CSE Loads with different alignment. Be conservative. */
5864 if (get_Load_unaligned(a) != get_Load_unaligned(b))
5867 return get_Load_mode(a) != get_Load_mode(b);
5870 /** Compares the attributes of two Store nodes. */
5871 static int node_cmp_attr_Store(const ir_node *a, const ir_node *b)
5873 /* do not CSE Stores with different alignment. Be conservative. */
5874 if (get_Store_unaligned(a) != get_Store_unaligned(b))
5877 /* NEVER do CSE on volatile Stores */
5878 return (get_Store_volatility(a) == volatility_is_volatile ||
5879 get_Store_volatility(b) == volatility_is_volatile);
5882 /** Compares two exception attributes */
5883 static int node_cmp_exception(const ir_node *a, const ir_node *b)
5885 const except_attr *ea = &a->attr.except;
5886 const except_attr *eb = &b->attr.except;
5888 return ea->pin_state != eb->pin_state;
5891 #define node_cmp_attr_Bound node_cmp_exception
5893 /** Compares the attributes of two Div nodes. */
5894 static int node_cmp_attr_Div(const ir_node *a, const ir_node *b)
5896 const div_attr *ma = &a->attr.div;
5897 const div_attr *mb = &b->attr.div;
5898 return ma->exc.pin_state != mb->exc.pin_state ||
5899 ma->resmode != mb->resmode ||
5900 ma->no_remainder != mb->no_remainder;
5903 /** Compares the attributes of two Mod nodes. */
5904 static int node_cmp_attr_Mod(const ir_node *a, const ir_node *b)
5906 const mod_attr *ma = &a->attr.mod;
5907 const mod_attr *mb = &b->attr.mod;
5908 return ma->exc.pin_state != mb->exc.pin_state ||
5909 ma->resmode != mb->resmode;
5912 static int node_cmp_attr_Cmp(const ir_node *a, const ir_node *b)
5914 const cmp_attr *ma = &a->attr.cmp;
5915 const cmp_attr *mb = &b->attr.cmp;
5916 return ma->relation != mb->relation;
5919 /** Compares the attributes of two Confirm nodes. */
5920 static int node_cmp_attr_Confirm(const ir_node *a, const ir_node *b)
5922 const confirm_attr *ma = &a->attr.confirm;
5923 const confirm_attr *mb = &b->attr.confirm;
5924 return ma->relation != mb->relation;
5927 /** Compares the attributes of two Builtin nodes. */
5928 static int node_cmp_attr_Builtin(const ir_node *a, const ir_node *b)
5930 /* no need to compare the type, equal kind means equal type */
5931 return get_Builtin_kind(a) != get_Builtin_kind(b);
5934 /** Compares the attributes of two ASM nodes. */
5935 static int node_cmp_attr_ASM(const ir_node *a, const ir_node *b)
5938 const ir_asm_constraint *ca;
5939 const ir_asm_constraint *cb;
5942 if (get_ASM_text(a) != get_ASM_text(b))
5945 /* Should we really check the constraints here? Should be better, but is strange. */
5946 n = get_ASM_n_input_constraints(a);
5947 if (n != get_ASM_n_input_constraints(b))
5950 ca = get_ASM_input_constraints(a);
5951 cb = get_ASM_input_constraints(b);
5952 for (i = 0; i < n; ++i) {
5953 if (ca[i].pos != cb[i].pos || ca[i].constraint != cb[i].constraint
5954 || ca[i].mode != cb[i].mode)
5958 n = get_ASM_n_output_constraints(a);
5959 if (n != get_ASM_n_output_constraints(b))
5962 ca = get_ASM_output_constraints(a);
5963 cb = get_ASM_output_constraints(b);
5964 for (i = 0; i < n; ++i) {
5965 if (ca[i].pos != cb[i].pos || ca[i].constraint != cb[i].constraint
5966 || ca[i].mode != cb[i].mode)
5970 n = get_ASM_n_clobbers(a);
5971 if (n != get_ASM_n_clobbers(b))
5974 cla = get_ASM_clobbers(a);
5975 clb = get_ASM_clobbers(b);
5976 for (i = 0; i < n; ++i) {
5977 if (cla[i] != clb[i])
5983 /** Compares the inexistent attributes of two Dummy nodes. */
5984 static int node_cmp_attr_Dummy(const ir_node *a, const ir_node *b)
5992 * Set the default node attribute compare operation for an ir_op_ops.
5994 * @param code the opcode for the default operation
5995 * @param ops the operations initialized
6000 static ir_op_ops *firm_set_default_node_cmp_attr(ir_opcode code, ir_op_ops *ops)
6004 ops->node_cmp_attr = node_cmp_attr_##a; \
6036 } /* firm_set_default_node_cmp_attr */
6039 * Compare function for two nodes in the value table. Gets two
6040 * nodes as parameters. Returns 0 if the nodes are a Common Sub Expression.
6042 int identities_cmp(const void *elt, const void *key)
6044 ir_node *a = (ir_node *)elt;
6045 ir_node *b = (ir_node *)key;
6048 if (a == b) return 0;
6050 if ((get_irn_op(a) != get_irn_op(b)) ||
6051 (get_irn_mode(a) != get_irn_mode(b))) return 1;
6053 /* compare if a's in and b's in are of equal length */
6054 irn_arity_a = get_irn_arity(a);
6055 if (irn_arity_a != get_irn_arity(b))
6058 /* blocks are never the same */
6062 if (get_irn_pinned(a) == op_pin_state_pinned) {
6063 /* for pinned nodes, the block inputs must be equal */
6064 if (get_irn_n(a, -1) != get_irn_n(b, -1))
6066 } else if (! get_opt_global_cse()) {
6067 /* for block-local CSE both nodes must be in the same Block */
6068 if (get_nodes_block(a) != get_nodes_block(b))
6072 /* compare a->in[0..ins] with b->in[0..ins] */
6073 for (i = 0; i < irn_arity_a; ++i) {
6074 ir_node *pred_a = get_irn_n(a, i);
6075 ir_node *pred_b = get_irn_n(b, i);
6076 if (pred_a != pred_b) {
6077 /* if both predecessors are CSE neutral they might be different */
6078 if (!is_irn_cse_neutral(pred_a) || !is_irn_cse_neutral(pred_b))
6084 * here, we already now that the nodes are identical except their
6087 if (a->op->ops.node_cmp_attr)
6088 return a->op->ops.node_cmp_attr(a, b);
6091 } /* identities_cmp */
6094 * Calculate a hash value of a node.
6096 * @param node The IR-node
6098 unsigned ir_node_hash(const ir_node *node)
6100 return node->op->ops.hash(node);
6101 } /* ir_node_hash */
6104 void new_identities(ir_graph *irg)
6106 if (irg->value_table != NULL)
6107 del_pset(irg->value_table);
6108 irg->value_table = new_pset(identities_cmp, N_IR_NODES);
6109 } /* new_identities */
6111 void del_identities(ir_graph *irg)
6113 if (irg->value_table != NULL)
6114 del_pset(irg->value_table);
6115 } /* del_identities */
6117 /* Normalize a node by putting constants (and operands with larger
6118 * node index) on the right (operator side). */
6119 void ir_normalize_node(ir_node *n)
6121 if (is_op_commutative(get_irn_op(n))) {
6122 ir_node *l = get_binop_left(n);
6123 ir_node *r = get_binop_right(n);
6125 /* For commutative operators perform a OP b == b OP a but keep
6126 * constants on the RIGHT side. This helps greatly in some
6127 * optimizations. Moreover we use the idx number to make the form
6129 if (!operands_are_normalized(l, r)) {
6130 set_binop_left(n, r);
6131 set_binop_right(n, l);
6135 } /* ir_normalize_node */
6138 * Return the canonical node computing the same value as n.
6139 * Looks up the node in a hash table, enters it in the table
6140 * if it isn't there yet.
6142 * @param n the node to look up
6144 * @return a node that computes the same value as n or n if no such
6145 * node could be found
6147 ir_node *identify_remember(ir_node *n)
6149 ir_graph *irg = get_irn_irg(n);
6150 pset *value_table = irg->value_table;
6153 if (value_table == NULL)
6156 ir_normalize_node(n);
6157 /* lookup or insert in hash table with given hash key. */
6158 nn = (ir_node*)pset_insert(value_table, n, ir_node_hash(n));
6161 /* n is reachable again */
6162 edges_node_revival(nn);
6166 } /* identify_remember */
6169 * During construction we set the op_pin_state_pinned flag in the graph right
6170 * when the optimization is performed. The flag turning on procedure global
6171 * cse could be changed between two allocations. This way we are safe.
6173 * @param n The node to lookup
6175 static inline ir_node *identify_cons(ir_node *n)
6179 n = identify_remember(n);
6180 if (n != old && get_nodes_block(old) != get_nodes_block(n)) {
6181 ir_graph *irg = get_irn_irg(n);
6182 set_irg_pinned(irg, op_pin_state_floats);
6185 } /* identify_cons */
6187 /* Add a node to the identities value table. */
6188 void add_identities(ir_node *node)
6195 identify_remember(node);
6198 /* Visit each node in the value table of a graph. */
6199 void visit_all_identities(ir_graph *irg, irg_walk_func visit, void *env)
6202 ir_graph *rem = current_ir_graph;
6204 current_ir_graph = irg;
6205 foreach_pset(irg->value_table, ir_node*, node) {
6208 current_ir_graph = rem;
6209 } /* visit_all_identities */
6212 * These optimizations deallocate nodes from the obstack.
6213 * It can only be called if it is guaranteed that no other nodes
6214 * reference this one, i.e., right after construction of a node.
6216 * @param n The node to optimize
6218 ir_node *optimize_node(ir_node *n)
6221 ir_graph *irg = get_irn_irg(n);
6222 unsigned iro = get_irn_opcode(n);
6225 /* Always optimize Phi nodes: part of the construction. */
6226 if ((!get_opt_optimize()) && (iro != iro_Phi)) return n;
6228 /* constant expression evaluation / constant folding */
6229 if (get_opt_constant_folding()) {
6230 /* neither constants nor Tuple values can be evaluated */
6231 if (iro != iro_Const && (get_irn_mode(n) != mode_T)) {
6232 /* try to evaluate */
6233 tv = computed_value(n);
6234 if (tv != tarval_bad) {
6239 * we MUST copy the node here temporarily, because it's still
6240 * needed for DBG_OPT_CSTEVAL
6242 node_size = offsetof(ir_node, attr) + n->op->attr_size;
6243 oldn = (ir_node*)alloca(node_size);
6245 memcpy(oldn, n, node_size);
6246 CLONE_ARR_A(ir_node *, oldn->in, n->in);
6248 /* ARG, copy the in array, we need it for statistics */
6249 memcpy(oldn->in, n->in, ARR_LEN(n->in) * sizeof(n->in[0]));
6251 /* note the inplace edges module */
6252 edges_node_deleted(n);
6254 /* evaluation was successful -- replace the node. */
6255 irg_kill_node(irg, n);
6256 nw = new_r_Const(irg, tv);
6258 DBG_OPT_CSTEVAL(oldn, nw);
6264 /* remove unnecessary nodes */
6265 if (get_opt_algebraic_simplification() ||
6266 (iro == iro_Phi) || /* always optimize these nodes. */
6268 (iro == iro_Proj) ||
6269 (iro == iro_Block) ) /* Flags tested local. */
6270 n = equivalent_node(n);
6272 /* Common Subexpression Elimination.
6274 * Checks whether n is already available.
6275 * The block input is used to distinguish different subexpressions. Right
6276 * now all nodes are op_pin_state_pinned to blocks, i.e., the CSE only finds common
6277 * subexpressions within a block.
6280 n = identify_cons(n);
6283 edges_node_deleted(oldn);
6285 /* We found an existing, better node, so we can deallocate the old node. */
6286 irg_kill_node(irg, oldn);
6290 /* Some more constant expression evaluation that does not allow to
6292 iro = get_irn_opcode(n);
6293 if (get_opt_algebraic_simplification() ||
6294 (iro == iro_Cond) ||
6295 (iro == iro_Proj)) /* Flags tested local. */
6296 n = transform_node(n);
6298 /* Now we have a legal, useful node. Enter it in hash table for CSE */
6299 if (get_opt_cse() && (get_irn_opcode(n) != iro_Block)) {
6301 n = identify_remember(o);
6307 } /* optimize_node */
6311 * These optimizations never deallocate nodes (in place). This can cause dead
6312 * nodes lying on the obstack. Remove these by a dead node elimination,
6313 * i.e., a copying garbage collection.
6315 ir_node *optimize_in_place_2(ir_node *n)
6319 unsigned iro = get_irn_opcode(n);
6321 if (!get_opt_optimize() && !is_Phi(n)) return n;
6323 if (iro == iro_Deleted)
6326 /* constant expression evaluation / constant folding */
6327 if (get_opt_constant_folding()) {
6328 /* neither constants nor Tuple values can be evaluated */
6329 if (iro != iro_Const && get_irn_mode(n) != mode_T) {
6330 /* try to evaluate */
6331 tv = computed_value(n);
6332 if (tv != tarval_bad) {
6333 /* evaluation was successful -- replace the node. */
6334 ir_graph *irg = get_irn_irg(n);
6336 n = new_r_Const(irg, tv);
6338 DBG_OPT_CSTEVAL(oldn, n);
6344 /* remove unnecessary nodes */
6345 if (get_opt_constant_folding() ||
6346 (iro == iro_Phi) || /* always optimize these nodes. */
6347 (iro == iro_Id) || /* ... */
6348 (iro == iro_Proj) || /* ... */
6349 (iro == iro_Block) ) /* Flags tested local. */
6350 n = equivalent_node(n);
6352 /** common subexpression elimination **/
6353 /* Checks whether n is already available. */
6354 /* The block input is used to distinguish different subexpressions. Right
6355 now all nodes are op_pin_state_pinned to blocks, i.e., the cse only finds common
6356 subexpressions within a block. */
6357 if (get_opt_cse()) {
6359 n = identify_remember(o);
6364 /* Some more constant expression evaluation. */
6365 iro = get_irn_opcode(n);
6366 if (get_opt_constant_folding() ||
6367 (iro == iro_Cond) ||
6368 (iro == iro_Proj)) /* Flags tested local. */
6369 n = transform_node(n);
6371 /* Now we can verify the node, as it has no dead inputs any more. */
6374 /* Now we have a legal, useful node. Enter it in hash table for cse.
6375 Blocks should be unique anyways. (Except the successor of start:
6376 is cse with the start block!) */
6377 if (get_opt_cse() && (get_irn_opcode(n) != iro_Block)) {
6379 n = identify_remember(o);
6385 } /* optimize_in_place_2 */
6388 * Wrapper for external use, set proper status bits after optimization.
6390 ir_node *optimize_in_place(ir_node *n)
6392 ir_graph *irg = get_irn_irg(n);
6393 /* Handle graph state */
6394 assert(get_irg_phase_state(irg) != phase_building);
6396 if (get_opt_global_cse())
6397 set_irg_pinned(irg, op_pin_state_floats);
6399 /* FIXME: Maybe we could also test whether optimizing the node can
6400 change the control graph. */
6401 set_irg_doms_inconsistent(irg);
6402 return optimize_in_place_2(n);
6403 } /* optimize_in_place */
6406 * Calculate a hash value of a Const node.
6408 static unsigned hash_Const(const ir_node *node)
6412 /* special value for const, as they only differ in their tarval. */
6413 h = HASH_PTR(node->attr.con.tarval);
6419 * Calculate a hash value of a SymConst node.
6421 static unsigned hash_SymConst(const ir_node *node)
6425 /* all others are pointers */
6426 h = HASH_PTR(node->attr.symc.sym.type_p);
6429 } /* hash_SymConst */
6432 * Set the default hash operation in an ir_op_ops.
6434 * @param code the opcode for the default operation
6435 * @param ops the operations initialized
6440 static ir_op_ops *firm_set_default_hash(unsigned code, ir_op_ops *ops)
6444 ops->hash = hash_##a; \
6447 /* hash function already set */
6448 if (ops->hash != NULL)
6455 /* use input/mode default hash if no function was given */
6456 ops->hash = firm_default_hash;
6464 * Sets the default operation for an ir_ops.
6466 ir_op_ops *firm_set_default_operations(unsigned code, ir_op_ops *ops)
6468 ops = firm_set_default_hash(code, ops);
6469 ops = firm_set_default_computed_value(code, ops);
6470 ops = firm_set_default_equivalent_node(code, ops);
6471 ops = firm_set_default_transform_node(code, ops);
6472 ops = firm_set_default_node_cmp_attr(code, ops);
6473 ops = firm_set_default_get_type_attr(code, ops);
6474 ops = firm_set_default_get_entity_attr(code, ops);
6477 } /* firm_set_default_operations */