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 */
111 /* we can safely skip Minus 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 if (is_Cast(n)) { n = get_Cast_op(n); continue; }
197 if (is_Proj(n)) { n = get_Proj_pred(n); continue; }
202 /* global references are never NULL */
204 } else if (n == get_irg_frame(current_ir_graph)) {
205 /* local references are never NULL */
207 } else if (is_Alloc(n)) {
208 /* alloc never returns NULL (it throws an exception instead) */
211 /* check for more Confirms */
212 for (; is_Confirm(n); n = skip_Cast(get_Confirm_value(n))) {
213 if (get_Confirm_cmp(n) == pn_Cmp_Lg) {
214 ir_node *bound = get_Confirm_bound(n);
215 tarval *tv = value_of(bound);
217 if (tarval_is_null(tv)) {
225 } /* value_not_null */
228 * Check, if the value of a node can be confirmed >= 0 or <= 0,
229 * If the mode of the value did not honor signed zeros, else
230 * check for >= 0 or < 0.
232 value_classify_sign classify_value_sign(ir_node *n)
240 ir_opcode code = get_irn_opcode(n);
250 return value_classified_unknown;
255 return value_classified_unknown;
257 tv = value_of(get_Confirm_bound(n));
258 if (tv == tarval_bad)
259 return value_classified_unknown;
261 mode = get_irn_mode(n);
264 * We can handle only >=, >, <, <= cases.
265 * We could handle == too, but this will be optimized into
268 * Note that for integer modes we have a slightly better
269 * optimization possibilities, so we handle this
272 cmp = get_Confirm_cmp(n);
277 * must be x < c <= 1 to be useful if integer mode and -0 = 0
278 * x < c <= 0 to be useful else
282 * must be x <= c < 1 to be useful if integer mode and -0 = 0
283 * x <= c < 0 to be useful else
285 c = mode_is_int(mode) && mode_honor_signed_zeros(mode) ?
286 get_mode_one(mode) : get_mode_null(mode);
288 ncmp = tarval_cmp(tv, c);
289 if (ncmp == pn_Cmp_Eq)
292 if (cmp != (ncmp ^ pn_Cmp_Eq))
293 return value_classified_unknown;
296 return value_classified_negative * negate;
300 * must be x >= c > -1 to be useful if integer mode
301 * x >= c >= 0 to be useful else
305 * must be x > c >= -1 to be useful if integer mode
306 * x > c >= 0 to be useful else
308 if (mode_is_int(mode)) {
309 c = get_mode_minus_one(mode);
311 ncmp = tarval_cmp(tv, c);
312 if (ncmp == pn_Cmp_Eq)
315 if (cmp != (ncmp ^ pn_Cmp_Eq))
316 return value_classified_unknown;
318 c = get_mode_minus_one(mode);
320 ncmp = tarval_cmp(tv, c);
322 if (ncmp != pn_Cmp_Eq && ncmp != pn_Cmp_Gt)
323 return value_classified_unknown;
327 return value_classified_positive * negate;
330 return value_classified_unknown;
332 } /* classify_value_sign */
335 * construct an interval from a value
337 * @return the filled interval or NULL if no interval
338 * can be created (happens only on floating point
340 static interval_t *get_interval_from_tv(interval_t *iv, tarval *tv)
342 ir_mode *mode = get_tarval_mode(tv);
344 if (tv == tarval_bad) {
345 if (mode_is_float(mode)) {
346 /* NaN could be included which we cannot handle */
347 iv->min = tarval_bad;
348 iv->max = tarval_bad;
349 iv->flags = MIN_EXCLUDED | MAX_EXCLUDED;
353 iv->min = get_mode_min(mode);
354 iv->max = get_mode_max(mode);
355 iv->flags = MIN_INCLUDED | MAX_INCLUDED;
360 if (mode_is_float(mode)) {
361 if (tv == get_mode_NAN(mode)) {
362 /* arg, we cannot handle NaN's. */
363 iv->min = tarval_bad;
364 iv->max = tarval_bad;
365 iv->flags = MIN_EXCLUDED | MAX_EXCLUDED;
373 iv->flags = MIN_INCLUDED | MAX_INCLUDED;
376 } /* get_interval_from_tv */
379 * construct an interval from a Confirm
381 * @param iv an empty interval, will be filled
382 * @param bound the bound value
383 * @param pnc the Confirm compare relation
385 * @return the filled interval or NULL if no interval
386 * can be created (happens only on floating point
388 static interval_t *get_interval(interval_t *iv, ir_node *bound, pn_Cmp pnc)
390 ir_mode *mode = get_irn_mode(bound);
391 tarval *tv = value_of(bound);
393 if (tv == tarval_bad) {
394 /* There is nothing we could do here. For integer
395 * modes we could return [-oo, +oo], but there is
396 * nothing we could deduct from such an interval.
397 * So, speed things up and return unknown.
399 iv->min = tarval_bad;
400 iv->max = tarval_bad;
401 iv->flags = MIN_EXCLUDED | MAX_EXCLUDED;
405 if (mode_is_float(mode)) {
406 if (tv == get_mode_NAN(mode)) {
407 /* arg, we cannot handle NaN's. */
408 iv->min = tarval_bad;
409 iv->max = tarval_bad;
410 iv->flags = MIN_EXCLUDED | MAX_EXCLUDED;
416 /* check which side is known */
422 iv->flags = MIN_INCLUDED | MAX_INCLUDED;
427 iv->min = get_mode_min(mode);
429 iv->flags = MIN_INCLUDED | MAX_INCLUDED;
434 iv->min = get_mode_min(mode);
436 iv->flags = MIN_INCLUDED | MAX_EXCLUDED;
442 iv->max = get_mode_max(mode);
443 iv->flags = MIN_EXCLUDED | MAX_INCLUDED;
449 iv->max = get_mode_max(mode);
450 iv->flags = MIN_INCLUDED | MAX_INCLUDED;
455 * Ordered means, that at least neither
456 * our bound nor our value ara NaN's
459 iv->min = get_mode_min(mode);
460 iv->max = get_mode_max(mode);
461 iv->flags = MIN_INCLUDED | MAX_INCLUDED;
466 * We do not handle UNORDERED, as a NaN
467 * could be included in the interval.
469 iv->min = tarval_bad;
470 iv->max = tarval_bad;
471 iv->flags = MIN_EXCLUDED | MAX_EXCLUDED;
475 if (iv->min != tarval_bad && iv->max != tarval_bad)
481 * Try to evaluate l_iv pnc r_iv.
483 * @param l_iv the left interval
484 * @param r_iv the right interval
485 * @param pnc the compare relation
488 * tarval_b_true or tarval_b_false it it can be evaluated,
491 static tarval *(compare_iv)(const interval_t *l_iv, const interval_t *r_iv, pn_Cmp pnc)
495 tarval *tv_true = tarval_b_true, *tv_false = tarval_b_false;
497 /* if one interval contains NaNs, we cannot evaluate anything */
498 if (! l_iv || ! r_iv)
501 /* we can only check ordered relations */
502 if (pnc & pn_Cmp_Uo) {
505 pnc = get_negated_pnc(pnc, get_tarval_mode(l_iv->min));
511 /* if we have > or >=, we do the inverse to save some cases */
512 if (pnc == pn_Cmp_Ge || pnc == pn_Cmp_Gt) {
515 pnc = get_inversed_pnc(pnc);
521 /* now, only the following cases remains */
524 /* two intervals can be compared for equality only if they are a single value */
525 if (l_iv->min == l_iv->max && r_iv->min == r_iv->max)
526 return tarval_cmp(l_iv->min, r_iv->min) == pn_Cmp_Eq ? tv_true : tv_false;
528 /* if both intervals do not intersect, it is never equal */
529 res = tarval_cmp(l_iv->max, r_iv->min);
531 /* b < c ==> [a,b] != [c,d] */
532 if (res == pn_Cmp_Lt)
535 /* b <= c ==> [a,b) != [c,d] AND [a,b] != (c,d] */
536 if ((l_iv->flags & MAX_EXCLUDED || r_iv->flags & MIN_EXCLUDED)
537 && (res == pn_Cmp_Eq))
540 res = tarval_cmp(r_iv->max, l_iv->min);
542 /* d < a ==> [c,d] != [a,b] */
543 if (res == pn_Cmp_Lt)
546 /* d <= a ==> [c,d) != [a,b] AND [c,d] != (a,b] */
547 if ((r_iv->flags & MAX_EXCLUDED || l_iv->flags & MIN_EXCLUDED)
548 && (res == pn_Cmp_Eq))
553 /* two intervals can be compared for not equality only if they are a single value */
554 if (l_iv->min == l_iv->max && r_iv->min == r_iv->max)
555 return tarval_cmp(l_iv->min, r_iv->min) != pn_Cmp_Eq ? tv_true : tv_false;
559 res = tarval_cmp(l_iv->max, r_iv->min);
561 /* [a, b] < [c, d] <==> b < c */
562 if (res == pn_Cmp_Lt)
565 /* if one border is excluded, b <= c is enough */
566 if ((l_iv->flags & MAX_EXCLUDED || r_iv->flags & MIN_EXCLUDED) &&
570 /* [a, b] >= [c, d] <==> a > d */
571 res = tarval_cmp(l_iv->min, r_iv->max);
572 if (res == pn_Cmp_Gt)
575 /* if one border is excluded, a >= d is enough */
576 if ((l_iv->flags & MIN_EXCLUDED || r_iv->flags & MAX_EXCLUDED) &&
582 /* [a, b) <= [c, d] or [a, b] <= (c, d] <==> b <= c */
583 flags = (l_iv->flags & MAX_EXCLUDED) | (r_iv->flags & MIN_EXCLUDED);
585 res = tarval_cmp(l_iv->max, r_iv->min);
587 if (res == pn_Cmp_Lt || res == pn_Cmp_Eq)
591 res = tarval_cmp(l_iv->min, r_iv->max);
593 /* [a, b] > [c, d] <==> a > d */
594 if (res == pn_Cmp_Gt)
597 /* if one border is excluded, a >= d is enough */
598 if ((l_iv->flags & MIN_EXCLUDED || r_iv->flags & MAX_EXCLUDED) &&
604 /* Hmm. if both are intervals, we can find an order */
614 * Returns non-zero, if a given relation is transitive.
616 static int is_transitive(pn_Cmp pnc)
618 return (pn_Cmp_False < pnc && pnc < pn_Cmp_Lg);
619 } /* is_transitive */
622 * Return the value of a Cmp if one or both predecessors
625 * @param cmp the Cmp node
626 * @param left the left operand of the Cmp
627 * @param right the right operand of the Cmp
628 * @param pnc the compare relation
630 tarval *computed_value_Cmp_Confirm(ir_node *cmp, ir_node *left, ir_node *right, pn_Cmp pnc)
633 pn_Cmp l_pnc, res_pnc, neg_pnc;
634 interval_t l_iv, r_iv;
638 if (is_Confirm(right)) {
639 /* we want the Confirm on the left side */
644 pnc = get_inversed_pnc(pnc);
645 } else if (! is_Confirm(left)) {
646 /* nothing more found */
648 goto check_null_case;
651 /* ok, here at least left is a Confirm, right might be */
652 l_bound = get_Confirm_bound(left);
653 l_pnc = get_Confirm_cmp(left);
655 if (is_Confirm(right)) {
657 * both sides are Confirm's. Check some rare cases first.
659 ir_node *r_bound = get_Confirm_bound(right);
660 pn_Cmp r_pnc = get_Confirm_cmp(right);
663 * some check can be made WITHOUT constant bounds
665 if (r_bound == l_bound) {
666 if (is_transitive(l_pnc)) {
667 pn_Cmp r_inc_pnc = get_inversed_pnc(r_pnc);
670 * triangle inequality:
672 * a CMP B && B CMP b => a CMP b, !(a ~CMP b)
674 * We handle correctly cases with some <=/>= here
676 if ((l_pnc & ~pn_Cmp_Eq) == (r_inc_pnc & ~pn_Cmp_Eq)) {
677 res_pnc = (l_pnc & ~pn_Cmp_Eq) | (l_pnc & r_inc_pnc & pn_Cmp_Eq);
679 if ((pnc == res_pnc) || ((pnc & ~pn_Cmp_Eq) == res_pnc)) {
680 DBG_OUT_TR(l_pnc, l_bound, r_pnc, r_bound, pnc, "true");
681 DBG_EVAL_CONFIRM(cmp);
682 return tarval_b_true;
684 pn_Cmp neg_pnc = get_negated_pnc(pnc, get_irn_mode(left));
686 if ((neg_pnc == res_pnc) || ((neg_pnc & ~pn_Cmp_Eq) == res_pnc)) {
687 DBG_OUT_TR(l_pnc, l_bound, r_pnc, r_bound, pnc, "false");
688 DBG_EVAL_CONFIRM(cmp);
689 return tarval_b_false;
697 * Here, we check only the right Confirm, as the left Confirms are
698 * checked later anyway.
700 if (left == r_bound) {
702 * l == bound(r) AND pnc(r) == pnc:
704 * We know that a CMP b and check for that
706 if ((r_pnc == pnc) || (r_pnc == (pnc & ~pn_Cmp_Eq))) {
707 DBG_OUT_R(r_pnc, r_bound, left, pnc, right, "true");
708 DBG_EVAL_CONFIRM(cmp);
709 return tarval_b_true;
712 * l == bound(r) AND pnc(r) != pnc:
714 * We know that a CMP b and check for a ~CMP b
717 mode = get_irn_mode(left);
718 neg_pnc = get_negated_pnc(pnc, mode);
720 if ((r_pnc == neg_pnc) || (r_pnc == (neg_pnc & ~pn_Cmp_Eq))) {
721 DBG_OUT_R(r_pnc, r_bound, left, pnc, right, "false");
722 DBG_EVAL_CONFIRM(cmp);
723 return tarval_b_false;
728 /* now, try interval magic */
730 get_interval(&l_iv, l_bound, l_pnc),
731 get_interval(&r_iv, r_bound, r_pnc),
734 if (tv != tarval_bad) {
735 DBG_EVAL_CONFIRM(cmp);
740 /* from Here, check only left Confirm */
743 * some checks can be made WITHOUT constant bounds
745 if (right == l_bound) {
747 * r == bound(l) AND pnc(l) == pnc:
749 * We know that a CMP b and check for that
751 if ((l_pnc == pnc) || (l_pnc == (pnc & ~pn_Cmp_Eq))) {
752 DBG_OUT_L(l_pnc, l_bound, left, pnc, right, "true");
753 DBG_EVAL_CONFIRM(cmp);
754 return tarval_b_true;
757 * r == bound(l) AND pnc(l) is Not(pnc):
759 * We know that a CMP b and check for a ~CMP b
762 mode = get_irn_mode(left);
763 neg_pnc = get_negated_pnc(pnc, mode);
765 if ((l_pnc == neg_pnc) || (l_pnc == (neg_pnc & ~pn_Cmp_Eq))) {
766 DBG_OUT_L(l_pnc, l_bound, left, pnc, right, "false");
767 DBG_EVAL_CONFIRM(cmp);
768 return tarval_b_false;
773 /* now, only right == Const can help */
774 tv = value_of(right);
776 if (tv != tarval_bad) {
778 get_interval(&l_iv, l_bound, l_pnc),
779 get_interval_from_tv(&r_iv, tv),
783 /* check some other cases */
784 if ((pnc == pn_Cmp_Eq || pnc == pn_Cmp_Lg) &&
785 is_Const(right) && is_Const_null(right)) {
786 /* for == 0 or != 0 we have some special tools */
787 ir_mode *mode = get_irn_mode(left);
788 const ir_node *dummy;
789 if (mode_is_reference(mode)) {
790 if (value_not_null(left, &dummy)) {
791 tv = pnc == pn_Cmp_Eq ? tarval_b_false : tarval_b_true;
794 if (value_not_zero(left, &dummy)) {
795 tv = pnc == pn_Cmp_Eq ? tarval_b_false : tarval_b_true;
801 if (tv != tarval_bad)
802 DBG_EVAL_CONFIRM(cmp);
805 } /* computed_value_Cmp_Confirm */
809 * For debugging. Prints an interval into a string.
811 * @param buf address of a string buffer
812 * @param len length of the string buffer
813 * @param iv the interval
815 static int iv_snprintf(char *buf, size_t len, const interval_t *iv)
817 char smin[64], smax[64];
820 tarval_snprintf(smin, sizeof(smin), iv->min);
822 if (iv->min != iv->max || (iv->flags & (MIN_EXCLUDED|MAX_EXCLUDED))) {
823 tarval_snprintf(smax, sizeof(smax), iv->max);
825 return snprintf(buf, len, "%c%s, %s%c",
826 iv->flags & MIN_EXCLUDED ? '(' : '[',
828 iv->flags & MAX_EXCLUDED ? ')' : ']'
831 return snprintf(buf, len, "%s", smin);
833 return snprintf(buf, len, "<UNKNOWN>");
837 * For debugging. Prints an interval compare.
839 * @param l_iv the left interval
840 * @param r_iv the right interval
841 * @param pnc the compare relation
843 static void print_iv_cmp(const interval_t *l_iv, const interval_t *r_iv, pn_Cmp pnc)
845 char sl[128], sr[128];
847 iv_snprintf(sl, sizeof(sl), l_iv);
848 iv_snprintf(sr, sizeof(sr), r_iv);
850 ir_printf("%s %= %s", sl, pnc, sr);
854 * For debugging. call *compare_iv() and prints inputs and result.
856 * @param l_iv the left interval
857 * @param r_iv the right interval
858 * @param pnc the compare relation
860 static tarval *compare_iv_dbg(const interval_t *l_iv, const interval_t *r_iv, pn_Cmp pnc)
862 tarval *tv = (compare_iv)(l_iv, r_iv, pnc);
864 if (tv == tarval_bad)
867 ir_printf("In %e:\n", get_irg_entity(current_ir_graph));
868 print_iv_cmp(l_iv, r_iv, pnc);
869 ir_printf(" = %T\n", tv);
871 } /* compare_iv_dbg */
873 #endif /* DEBUG_CONFIRM */