2 * Copyright (C) 1995-2008 University of Karlsruhe. All right reserved.
4 * This file is part of libFirm.
6 * This file may be distributed and/or modified under the terms of the
7 * GNU General Public License version 2 as published by the Free Software
8 * Foundation and appearing in the file LICENSE.GPL included in the
9 * packaging of this file.
11 * Licensees holding valid libFirm Professional Edition licenses may use
12 * this file in accordance with the libFirm Commercial License.
13 * Agreement provided with the Software.
15 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * @brief Optimizations regarding Confirm nodes.
23 * @author Michael Beck
33 #include "iropt_dbg.h"
34 #include "opt_confirms.h"
39 MIN_INCLUDED = 0x00, /**< [min, ... */
40 MAX_INCLUDED = 0x00, /**< ..., max] */
41 MIN_EXCLUDED = 0x01, /**< (min, ... */
42 MAX_EXCLUDED = 0x02 /**< ..., max) */
46 * An interval. We could use
47 * intervals that ALWAYS include its borders, even for
48 * floating point, as the precision is limited.
49 * However, as our tarval module did not support
50 * such kind of operation, we use border flags allowing
53 typedef struct _interval_t {
54 tarval *min; /**< lowest border */
55 tarval *max; /**< highest border */
56 unsigned char flags; /**< border flags */
61 #define compare_iv(l_iv, r_iv, pnc) compare_iv_dbg(l_iv, r_iv, pnc)
64 static tarval *compare_iv_dbg(const interval_t *l_iv, const interval_t *r_iv, pn_Cmp pnc);
67 #define DBG_OUT_TR(l_pnc, l_bound, r_pnc, r_bound, pnc, v) \
68 ir_printf("In %e:\na %= %n && b %= %n ==> a %= b == %s\n", \
69 get_irg_entity(current_ir_graph), \
70 l_pnc, l_bound, r_pnc, r_bound, pnc, v);
73 #define DBG_OUT_R(r_pnc, r_bound, left, pnc, right, v) \
74 ir_printf("In %e:\na %= %n ==> %n %= %n == %s\n", \
75 get_irg_entity(current_ir_graph), \
76 r_pnc, r_bound, left, pnc, right, v);
79 #define DBG_OUT_L(l_pnc, l_bound, left, pnc, right, v) \
80 ir_printf("In %e:\na %= %n ==> %n %= %n == %s\n", \
81 get_irg_entity(current_ir_graph), \
82 l_pnc, l_bound, left, pnc, right, v);
86 #define DBG_OUT_TR(l_pnc, l_bound, r_pnc, r_bound, pnc, v)
87 #define DBG_OUT_R(r_pnc, r_bound, left, pnc, right, v)
88 #define DBG_OUT_L(l_pnc, l_bound, left, pnc, right, v)
90 #endif /* DEBUG_CONFIRM */
93 * Check, if the value of a node is != 0.
95 * This is a often needed case, so we handle here Confirm
98 int value_not_zero(const ir_node *n, ir_node_cnst_ptr *confirm)
100 #define RET_ON(x) if (x) { *confirm = n; return 1; }; break
103 ir_mode *mode = get_irn_mode(n);
108 /* there might be several Confirms one after other that form an interval */
110 if (is_Minus(n) || is_Abs(n)) {
111 /* we can safely skip Minus and Abs when checking for != 0 */
119 * Note: A Confirm is never after a Const. So,
120 * we simply can check the bound for being a Const
121 * without the fear that is might be hidden by a further Confirm.
123 tv = value_of(get_Confirm_bound(n));
124 if (tv == tarval_bad)
127 pnc = tarval_cmp(tv, get_mode_null(mode));
130 * Beware: C might by a NaN. It is not clear, what we should do
131 * than. Of course a NaN is != 0, but we might use this function
132 * to remove up Exceptions, and NaN's might generate Exception.
133 * So, we do NOT handle NaNs here for safety.
135 * Note that only the C != 0 case need additional checking.
137 switch (get_Confirm_cmp(n)) {
138 case pn_Cmp_Eq: /* n == C /\ C != 0 ==> n != 0 */
139 RET_ON(pnc != pn_Cmp_Eq && pnc != pn_Cmp_Uo);
140 case pn_Cmp_Lg: /* n != C /\ C == 0 ==> n != 0 */
141 RET_ON(pnc == pn_Cmp_Eq);
142 case pn_Cmp_Lt: /* n < C /\ C <= 0 ==> n != 0 */
143 RET_ON(pnc == pn_Cmp_Lt || pnc == pn_Cmp_Eq);
144 case pn_Cmp_Le: /* n <= C /\ C < 0 ==> n != 0 */
145 RET_ON(pnc == pn_Cmp_Lt);
146 case pn_Cmp_Ge: /* n >= C /\ C > 0 ==> n != 0 */
147 RET_ON(pnc == pn_Cmp_Gt);
148 case pn_Cmp_Gt: /* n > C /\ C >= 0 ==> n != 0 */
149 RET_ON(pnc == pn_Cmp_Gt || pnc == pn_Cmp_Eq);
153 n = get_Confirm_value(n);
157 if (tv == tarval_bad)
160 pnc = tarval_cmp(tv, get_mode_null(mode));
162 /* again, need check for NaN */
163 return (pnc != pn_Cmp_Eq) && (pnc != pn_Cmp_Uo);
166 } /* value_not_zero */
169 * Check, if the value of a node cannot represent a NULL pointer.
171 * - Casts are skipped
172 * - If sel_based_null_check_elim is enabled, all
173 * Sel nodes can be skipped.
174 * - A SymConst(entity) is NEVER a NULL pointer
175 * - Confirms are evaluated
177 int value_not_null(const ir_node *n, ir_node_cnst_ptr *confirm)
182 n = skip_Cast_const(n);
185 if (tarval_is_constant(tv) && ! tarval_is_null(tv))
188 assert(mode_is_reference(get_irn_mode(n)));
189 if (get_opt_sel_based_null_check_elim()) {
190 /* skip all Sel nodes and Cast's */
192 n = skip_Cast(get_Sel_ptr(n));
196 /* global references are never NULL */
198 } else if (n == get_irg_frame(current_ir_graph)) {
199 /* local references are never NULL */
202 /* check for more Confirms */
203 for (; is_Confirm(n); n = skip_Cast(get_Confirm_value(n))) {
204 if (get_Confirm_cmp(n) == pn_Cmp_Lg) {
205 ir_node *bound = get_Confirm_bound(n);
206 tarval *tv = value_of(bound);
208 if (tarval_is_null(tv)) {
216 } /* value_not_null */
219 * Check, if the value of a node can be confirmed >= 0 or <= 0,
220 * If the mode of the value did not honor signed zeros, else
221 * check for >= 0 or < 0.
223 value_classify_sign classify_value_sign(ir_node *n)
231 ir_opcode code = get_irn_opcode(n);
241 return value_classified_unknown;
246 return value_classified_unknown;
248 tv = value_of(get_Confirm_bound(n));
249 if (tv == tarval_bad)
250 return value_classified_unknown;
252 mode = get_irn_mode(n);
255 * We can handle only >=, >, <, <= cases.
256 * We could handle == too, but this will be optimized into
259 * Note that for integer modes we have a slightly better
260 * optimization possibilities, so we handle this
263 cmp = get_Confirm_cmp(n);
268 * must be x < c <= 1 to be useful if integer mode and -0 = 0
269 * x < c <= 0 to be useful else
273 * must be x <= c < 1 to be useful if integer mode and -0 = 0
274 * x <= c < 0 to be useful else
276 c = mode_is_int(mode) && mode_honor_signed_zeros(mode) ?
277 get_mode_one(mode) : get_mode_null(mode);
279 ncmp = tarval_cmp(tv, c);
280 if (ncmp == pn_Cmp_Eq)
283 if (cmp != (ncmp ^ pn_Cmp_Eq))
284 return value_classified_unknown;
287 return value_classified_negative * negate;
291 * must be x >= c > -1 to be useful if integer mode
292 * x >= c >= 0 to be useful else
296 * must be x > c >= -1 to be useful if integer mode
297 * x > c >= 0 to be useful else
299 if (mode_is_int(mode)) {
300 c = get_mode_minus_one(mode);
302 ncmp = tarval_cmp(tv, c);
303 if (ncmp == pn_Cmp_Eq)
306 if (cmp != (ncmp ^ pn_Cmp_Eq))
307 return value_classified_unknown;
309 c = get_mode_minus_one(mode);
311 ncmp = tarval_cmp(tv, c);
313 if (ncmp != pn_Cmp_Eq && ncmp != pn_Cmp_Gt)
314 return value_classified_unknown;
318 return value_classified_positive * negate;
321 return value_classified_unknown;
323 } /* classify_value_sign */
326 * construct an interval from a value
328 * @return the filled interval or NULL if no interval
329 * can be created (happens only on floating point
331 static interval_t *get_interval_from_tv(interval_t *iv, tarval *tv)
333 ir_mode *mode = get_tarval_mode(tv);
335 if (tv == tarval_bad) {
336 if (mode_is_float(mode)) {
337 /* NaN could be included which we cannot handle */
338 iv->min = tarval_bad;
339 iv->max = tarval_bad;
340 iv->flags = MIN_EXCLUDED | MAX_EXCLUDED;
344 iv->min = get_mode_min(mode);
345 iv->max = get_mode_max(mode);
346 iv->flags = MIN_INCLUDED | MAX_INCLUDED;
351 if (mode_is_float(mode)) {
352 if (tv == get_mode_NAN(mode)) {
353 /* arg, we cannot handle NaN's. */
354 iv->min = tarval_bad;
355 iv->max = tarval_bad;
356 iv->flags = MIN_EXCLUDED | MAX_EXCLUDED;
364 iv->flags = MIN_INCLUDED | MAX_INCLUDED;
367 } /* get_interval_from_tv */
370 * construct an interval from a Confirm
372 * @param iv an empty interval, will be filled
373 * @param bound the bound value
374 * @param pnc the Confirm compare relation
376 * @return the filled interval or NULL if no interval
377 * can be created (happens only on floating point
379 static interval_t *get_interval(interval_t *iv, ir_node *bound, pn_Cmp pnc)
381 ir_mode *mode = get_irn_mode(bound);
382 tarval *tv = value_of(bound);
384 if (tv == tarval_bad) {
385 /* There is nothing we could do here. For integer
386 * modes we could return [-oo, +oo], but there is
387 * nothing we could deduct from such an interval.
388 * So, speed things up and return unknown.
390 iv->min = tarval_bad;
391 iv->max = tarval_bad;
392 iv->flags = MIN_EXCLUDED | MAX_EXCLUDED;
396 if (mode_is_float(mode)) {
397 if (tv == get_mode_NAN(mode)) {
398 /* arg, we cannot handle NaN's. */
399 iv->min = tarval_bad;
400 iv->max = tarval_bad;
401 iv->flags = MIN_EXCLUDED | MAX_EXCLUDED;
407 /* check which side is known */
413 iv->flags = MIN_INCLUDED | MAX_INCLUDED;
418 iv->min = get_mode_min(mode);
420 iv->flags = MIN_INCLUDED | MAX_INCLUDED;
425 iv->min = get_mode_min(mode);
427 iv->flags = MIN_INCLUDED | MAX_EXCLUDED;
433 iv->max = get_mode_max(mode);
434 iv->flags = MIN_EXCLUDED | MAX_INCLUDED;
440 iv->max = get_mode_max(mode);
441 iv->flags = MIN_INCLUDED | MAX_INCLUDED;
446 * Ordered means, that at least neither
447 * our bound nor our value ara NaN's
450 iv->min = get_mode_min(mode);
451 iv->max = get_mode_max(mode);
452 iv->flags = MIN_INCLUDED | MAX_INCLUDED;
457 * We do not handle UNORDERED, as a NaN
458 * could be included in the interval.
460 iv->min = tarval_bad;
461 iv->max = tarval_bad;
462 iv->flags = MIN_EXCLUDED | MAX_EXCLUDED;
466 if (iv->min != tarval_bad && iv->max != tarval_bad)
472 * Try to evaluate l_iv pnc r_iv.
474 * @param l_iv the left interval
475 * @param r_iv the right interval
476 * @param pnc the compare relation
479 * tarval_b_true or tarval_b_false it it can be evaluated,
482 static tarval *(compare_iv)(const interval_t *l_iv, const interval_t *r_iv, pn_Cmp pnc)
486 tarval *tv_true = tarval_b_true, *tv_false = tarval_b_false;
488 /* if one interval contains NaNs, we cannot evaluate anything */
489 if (! l_iv || ! r_iv)
492 /* we can only check ordered relations */
493 if (pnc & pn_Cmp_Uo) {
496 pnc = get_negated_pnc(pnc, get_tarval_mode(l_iv->min));
502 /* if we have > or >=, we do the inverse to save some cases */
503 if (pnc == pn_Cmp_Ge || pnc == pn_Cmp_Gt) {
506 pnc = get_inversed_pnc(pnc);
512 /* now, only the following cases remains */
515 /* two intervals can be compared for equality only if they are a single value */
516 if (l_iv->min == l_iv->max && r_iv->min == r_iv->max)
517 return tarval_cmp(l_iv->min, r_iv->min) == pn_Cmp_Eq ? tv_true : tv_false;
519 /* if both intervals do not intersect, it is never equal */
520 res = tarval_cmp(l_iv->max, r_iv->min);
522 /* b < c ==> [a,b] != [c,d] */
523 if (res == pn_Cmp_Lt)
526 /* b <= c ==> [a,b) != [c,d] AND [a,b] != (c,d] */
527 if ((l_iv->flags & MAX_EXCLUDED || r_iv->flags & MIN_EXCLUDED)
528 && (res == pn_Cmp_Eq))
531 res = tarval_cmp(r_iv->max, l_iv->min);
533 /* d < a ==> [c,d] != [a,b] */
534 if (res == pn_Cmp_Lt)
537 /* d <= a ==> [c,d) != [a,b] AND [c,d] != (a,b] */
538 if ((r_iv->flags & MAX_EXCLUDED || l_iv->flags & MIN_EXCLUDED)
539 && (res == pn_Cmp_Eq))
544 /* two intervals can be compared for not equality only if they are a single value */
545 if (l_iv->min == l_iv->max && r_iv->min == r_iv->max)
546 return tarval_cmp(l_iv->min, r_iv->min) != pn_Cmp_Eq ? tv_true : tv_false;
550 res = tarval_cmp(l_iv->max, r_iv->min);
552 /* [a, b] < [c, d] <==> b < c */
553 if (res == pn_Cmp_Lt)
556 /* if one border is excluded, b <= c is enough */
557 if ((l_iv->flags & MAX_EXCLUDED || r_iv->flags & MIN_EXCLUDED) &&
561 /* [a, b] >= [c, d] <==> a > d */
562 res = tarval_cmp(l_iv->min, r_iv->max);
563 if (res == pn_Cmp_Gt)
566 /* if one border is excluded, a >= d is enough */
567 if ((l_iv->flags & MIN_EXCLUDED || r_iv->flags & MAX_EXCLUDED) &&
573 /* [a, b) <= [c, d] or [a, b] <= (c, d] <==> b <= c */
574 flags = (l_iv->flags & MAX_EXCLUDED) | (r_iv->flags & MIN_EXCLUDED);
576 res = tarval_cmp(l_iv->max, r_iv->min);
578 if (res == pn_Cmp_Lt || res == pn_Cmp_Eq)
582 res = tarval_cmp(l_iv->min, r_iv->max);
584 /* [a, b] > [c, d] <==> a > d */
585 if (res == pn_Cmp_Gt)
588 /* if one border is excluded, a >= d is enough */
589 if ((l_iv->flags & MIN_EXCLUDED || r_iv->flags & MAX_EXCLUDED) &&
595 /* Hmm. if both are intervals, we can find an order */
605 * Returns non-zero, if a given relation is transitive.
607 static int is_transitive(pn_Cmp pnc)
609 return (pn_Cmp_False < pnc && pnc < pn_Cmp_Lg);
610 } /* is_transitive */
613 * Return the value of a Cmp if one or both predecessors
616 * @param cmp the Cmp node
617 * @param left the left operand of the Cmp
618 * @param right the right operand of the Cmp
619 * @param pnc the compare relation
621 tarval *computed_value_Cmp_Confirm(ir_node *cmp, ir_node *left, ir_node *right, pn_Cmp pnc)
624 pn_Cmp l_pnc, res_pnc, neg_pnc;
625 interval_t l_iv, r_iv;
629 if (is_Confirm(right)) {
630 /* we want the Confirm on the left side */
635 pnc = get_inversed_pnc(pnc);
636 } else if (! is_Confirm(left)) {
637 /* nothing more found */
639 goto check_null_case;
642 /* ok, here at least left is a Confirm, right might be */
643 l_bound = get_Confirm_bound(left);
644 l_pnc = get_Confirm_cmp(left);
646 if (is_Confirm(right)) {
648 * both sides are Confirm's. Check some rare cases first.
650 ir_node *r_bound = get_Confirm_bound(right);
651 pn_Cmp r_pnc = get_Confirm_cmp(right);
654 * some check can be made WITHOUT constant bounds
656 if (r_bound == l_bound) {
657 if (is_transitive(l_pnc)) {
658 pn_Cmp r_inc_pnc = get_inversed_pnc(r_pnc);
661 * triangle inequality:
663 * a CMP B && B CMP b => a CMP b, !(a ~CMP b)
665 * We handle correctly cases with some <=/>= here
667 if ((l_pnc & ~pn_Cmp_Eq) == (r_inc_pnc & ~pn_Cmp_Eq)) {
668 res_pnc = (l_pnc & ~pn_Cmp_Eq) | (l_pnc & r_inc_pnc & pn_Cmp_Eq);
670 if ((pnc == res_pnc) || ((pnc & ~pn_Cmp_Eq) == res_pnc)) {
671 DBG_OUT_TR(l_pnc, l_bound, r_pnc, r_bound, pnc, "true");
672 DBG_EVAL_CONFIRM(cmp);
673 return tarval_b_true;
675 pn_Cmp neg_pnc = get_negated_pnc(pnc, get_irn_mode(left));
677 if ((neg_pnc == res_pnc) || ((neg_pnc & ~pn_Cmp_Eq) == res_pnc)) {
678 DBG_OUT_TR(l_pnc, l_bound, r_pnc, r_bound, pnc, "false");
679 DBG_EVAL_CONFIRM(cmp);
680 return tarval_b_false;
688 * Here, we check only the right Confirm, as the left Confirms are
689 * checked later anyway.
691 if (left == r_bound) {
693 * l == bound(r) AND pnc(r) == pnc:
695 * We know that a CMP b and check for that
697 if ((r_pnc == pnc) || (r_pnc == (pnc & ~pn_Cmp_Eq))) {
698 DBG_OUT_R(r_pnc, r_bound, left, pnc, right, "true");
699 DBG_EVAL_CONFIRM(cmp);
700 return tarval_b_true;
703 * l == bound(r) AND pnc(r) != pnc:
705 * We know that a CMP b and check for a ~CMP b
708 mode = get_irn_mode(left);
709 neg_pnc = get_negated_pnc(pnc, mode);
711 if ((r_pnc == neg_pnc) || (r_pnc == (neg_pnc & ~pn_Cmp_Eq))) {
712 DBG_OUT_R(r_pnc, r_bound, left, pnc, right, "false");
713 DBG_EVAL_CONFIRM(cmp);
714 return tarval_b_false;
719 /* now, try interval magic */
721 get_interval(&l_iv, l_bound, l_pnc),
722 get_interval(&r_iv, r_bound, r_pnc),
725 if (tv != tarval_bad) {
726 DBG_EVAL_CONFIRM(cmp);
731 /* from Here, check only left Confirm */
734 * some checks can be made WITHOUT constant bounds
736 if (right == l_bound) {
738 * r == bound(l) AND pnc(l) == pnc:
740 * We know that a CMP b and check for that
742 if ((l_pnc == pnc) || (l_pnc == (pnc & ~pn_Cmp_Eq))) {
743 DBG_OUT_L(l_pnc, l_bound, left, pnc, right, "true");
744 DBG_EVAL_CONFIRM(cmp);
745 return tarval_b_true;
748 * r == bound(l) AND pnc(l) is Not(pnc):
750 * We know that a CMP b and check for a ~CMP b
753 mode = get_irn_mode(left);
754 neg_pnc = get_negated_pnc(pnc, mode);
756 if ((l_pnc == neg_pnc) || (l_pnc == (neg_pnc & ~pn_Cmp_Eq))) {
757 DBG_OUT_L(l_pnc, l_bound, left, pnc, right, "false");
758 DBG_EVAL_CONFIRM(cmp);
759 return tarval_b_false;
764 /* now, only right == Const can help */
765 tv = value_of(right);
767 if (tv != tarval_bad) {
769 get_interval(&l_iv, l_bound, l_pnc),
770 get_interval_from_tv(&r_iv, tv),
774 /* check some other cases */
775 if ((pnc == pn_Cmp_Eq || pnc == pn_Cmp_Lg) &&
776 is_Const(right) && is_Const_null(right)) {
777 /* for == 0 or != 0 we have some special tools */
778 ir_mode *mode = get_irn_mode(left);
779 const ir_node *dummy;
780 if (mode_is_reference(mode)) {
781 if (value_not_null(left, &dummy)) {
782 tv = pnc == pn_Cmp_Eq ? tarval_b_false : tarval_b_true;
785 if (value_not_zero(left, &dummy)) {
786 tv = pnc == pn_Cmp_Eq ? tarval_b_false : tarval_b_true;
792 if (tv != tarval_bad)
793 DBG_EVAL_CONFIRM(cmp);
796 } /* computed_value_Cmp_Confirm */
800 * For debugging. Prints an interval into a string.
802 * @param buf address of a string buffer
803 * @param len length of the string buffer
804 * @param iv the interval
806 static int iv_snprintf(char *buf, size_t len, const interval_t *iv)
808 char smin[64], smax[64];
811 tarval_snprintf(smin, sizeof(smin), iv->min);
813 if (iv->min != iv->max || (iv->flags & (MIN_EXCLUDED|MAX_EXCLUDED))) {
814 tarval_snprintf(smax, sizeof(smax), iv->max);
816 return snprintf(buf, len, "%c%s, %s%c",
817 iv->flags & MIN_EXCLUDED ? '(' : '[',
819 iv->flags & MAX_EXCLUDED ? ')' : ']'
822 return snprintf(buf, len, "%s", smin);
824 return snprintf(buf, len, "<UNKNOWN>");
828 * For debugging. Prints an interval compare.
830 * @param l_iv the left interval
831 * @param r_iv the right interval
832 * @param pnc the compare relation
834 static void print_iv_cmp(const interval_t *l_iv, const interval_t *r_iv, pn_Cmp pnc)
836 char sl[128], sr[128];
838 iv_snprintf(sl, sizeof(sl), l_iv);
839 iv_snprintf(sr, sizeof(sr), r_iv);
841 ir_printf("%s %= %s", sl, pnc, sr);
845 * For debugging. call *compare_iv() and prints inputs and result.
847 * @param l_iv the left interval
848 * @param r_iv the right interval
849 * @param pnc the compare relation
851 static tarval *compare_iv_dbg(const interval_t *l_iv, const interval_t *r_iv, pn_Cmp pnc)
853 tarval *tv = (compare_iv)(l_iv, r_iv, pnc);
855 if (tv == tarval_bad)
858 ir_printf("In %e:\n", get_irg_entity(current_ir_graph));
859 print_iv_cmp(l_iv, r_iv, pnc);
860 ir_printf(" = %T\n", tv);
862 } /* compare_iv_dbg */
864 #endif /* DEBUG_CONFIRM */