2 * Copyright (C) 1995-2007 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
35 #include "iropt_dbg.h"
36 #include "opt_confirms.h"
41 MIN_INCLUDED = 0x00, /**< [min, ... */
42 MAX_INCLUDED = 0x00, /**< ..., max] */
43 MIN_EXCLUDED = 0x01, /**< (min, ... */
44 MAX_EXCLUDED = 0x02 /**< ..., max) */
48 * An interval. We could use
49 * intervals that ALWAYS include its borders, even for
50 * floating point, as the precision is limited.
51 * However, as our tarval module did not support
52 * such kind of operation, we use border flags allowing
55 typedef struct _interval_t {
56 tarval *min; /**< lowest border */
57 tarval *max; /**< highest border */
58 unsigned char flags; /**< border flags */
63 #define compare_iv(l_iv, r_iv, pnc) compare_iv_dbg(l_iv, r_iv, pnc)
66 static tarval *compare_iv_dbg(const interval_t *l_iv, const interval_t *r_iv, pn_Cmp pnc);
69 #define DBG_OUT_TR(l_pnc, l_bound, r_pnc, r_bound, pnc, v) \
70 ir_printf("In %e:\na %= %n && b %= %n ==> a %= b == %s\n", \
71 get_irg_entity(current_ir_graph), \
72 l_pnc, l_bound, r_pnc, r_bound, pnc, v);
75 #define DBG_OUT_R(r_pnc, r_bound, left, pnc, right, v) \
76 ir_printf("In %e:\na %= %n ==> %n %= %n == %s\n", \
77 get_irg_entity(current_ir_graph), \
78 r_pnc, r_bound, left, pnc, right, v);
81 #define DBG_OUT_L(l_pnc, l_bound, left, pnc, right, v) \
82 ir_printf("In %e:\na %= %n ==> %n %= %n == %s\n", \
83 get_irg_entity(current_ir_graph), \
84 l_pnc, l_bound, left, pnc, right, v);
88 #define DBG_OUT_TR(l_pnc, l_bound, r_pnc, r_bound, pnc, v)
89 #define DBG_OUT_R(r_pnc, r_bound, left, pnc, right, v)
90 #define DBG_OUT_L(l_pnc, l_bound, left, pnc, right, v)
92 #endif /* DEBUG_CONFIRM */
95 * Check, if the value of a node is != 0.
97 * This is a often needed case, so we handle here Confirm
100 int value_not_zero(ir_node *n, ir_node **confirm) {
101 #define RET_ON(x) if (x) { *confirm = n; return 1; }; break
104 ir_mode *mode = get_irn_mode(n);
109 /* there might be several Confirms one after other that form an interval */
110 for (; is_Confirm(n); n = get_Confirm_value(n)) {
112 * Note: A Confirm is never after a Const. So,
113 * we simply can check the bound for being a Const
114 * without the fear that is might be hidden by a further Confirm.
116 tv = value_of(get_Confirm_bound(n));
117 if (tv == tarval_bad)
120 pnc = tarval_cmp(tv, get_mode_null(mode));
123 * Beware: C might by a NaN. It is not clear, what we should do
124 * than. Of course a NaN is != 0, but we might use this function
125 * to remove up Exceptions, and NaN's might generate Exception.
126 * So, we do NOT handle NaNs here for safety.
128 * Note that only the C != 0 case need additional checking.
130 switch (get_Confirm_cmp(n)) {
131 case pn_Cmp_Eq: /* n == C /\ C != 0 ==> n != 0 */
132 RET_ON(pnc != pn_Cmp_Eq && pnc != pn_Cmp_Uo);
133 case pn_Cmp_Lg: /* n != C /\ C == 0 ==> n != 0 */
134 RET_ON(pnc == pn_Cmp_Eq);
135 case pn_Cmp_Lt: /* n < C /\ C <= 0 ==> n != 0 */
136 RET_ON(pnc == pn_Cmp_Lt || pnc == pn_Cmp_Eq);
137 case pn_Cmp_Le: /* n <= C /\ C < 0 ==> n != 0 */
138 RET_ON(pnc == pn_Cmp_Lt);
139 case pn_Cmp_Ge: /* n >= C /\ C > 0 ==> n != 0 */
140 RET_ON(pnc == pn_Cmp_Gt);
141 case pn_Cmp_Gt: /* n > C /\ C >= 0 ==> n != 0 */
142 RET_ON(pnc == pn_Cmp_Gt || pnc == pn_Cmp_Eq);
149 if (tv == tarval_bad)
152 pnc = tarval_cmp(tv, get_mode_null(mode));
154 /* again, need check for NaN */
155 return (pnc != pn_Cmp_Eq) && (pnc != pn_Cmp_Uo);
158 } /* value_not_zero */
161 * Check, if the value of a node cannot represent a NULL pointer.
163 * - Casts are skipped
164 * - If sel_based_null_check_elim is enabled, all
165 * Sel nodes can be skipped.
166 * - A SymConst(entity) is NEVER a NULL pointer
167 * - Confirms are evaluated
169 int value_not_null(ir_node *n, ir_node **confirm) {
176 assert(mode_is_reference(get_irn_mode(n)));
177 if (get_opt_sel_based_null_check_elim()) {
178 /* skip all Sel nodes and Cast's */
179 while (op == op_Sel) {
180 n = skip_Cast(get_Sel_ptr(n));
184 if (op == op_SymConst && get_SymConst_kind(n) == symconst_addr_ent)
186 if (op == op_Const) {
187 tarval *tv = get_Const_tarval(n);
189 if (tv != tarval_bad && !tarval_is_null(tv))
192 for (; is_Confirm(n); n = skip_Cast(get_Confirm_value(n))) {
193 if (get_Confirm_cmp(n) != pn_Cmp_Lg) {
194 ir_node *bound = get_Confirm_bound(n);
195 if (is_Const(bound) && is_Const_null(bound)) {
203 } /* value_not_null */
206 * Check, if the value of a node can be confirmed >= 0 or <= 0,
207 * If the mode of the value did not honor signed zeros, else
208 * check for >= 0 or < 0.
210 value_classify_sign classify_value_sign(ir_node *n) {
215 if (get_irn_op(n) != op_Confirm)
216 return value_classified_unknown;
218 tv = value_of(get_Confirm_bound(n));
219 if (tv == tarval_bad)
220 return value_classified_unknown;
222 mode = get_irn_mode(n);
225 * We can handle only >=, >, <, <= cases.
226 * We could handle == too, but this will be optimized into
229 * Note that for integer modes we have a slightly better
230 * optimization possibilities, so we handle this
233 cmp = get_Confirm_cmp(n);
238 * must be x < c <= 1 to be useful if integer mode and -0 = 0
239 * x < c <= 0 to be useful else
243 * must be x <= c < 1 to be useful if integer mode and -0 = 0
244 * x <= c < 0 to be useful else
246 c = mode_is_int(mode) && mode_honor_signed_zeros(mode) ?
247 get_mode_one(mode) : get_mode_null(mode);
249 ncmp = tarval_cmp(tv, c);
250 if (ncmp == pn_Cmp_Eq)
253 if (cmp != (ncmp ^ pn_Cmp_Eq))
254 return value_classified_unknown;
257 return value_classified_negative;
261 * must be x >= c > -1 to be useful if integer mode
262 * x >= c >= 0 to be useful else
266 * must be x > c >= -1 to be useful if integer mode
267 * x > c >= 0 to be useful else
269 if (mode_is_int(mode)) {
270 c = get_mode_minus_one(mode);
272 ncmp = tarval_cmp(tv, c);
273 if (ncmp == pn_Cmp_Eq)
276 if (cmp != (ncmp ^ pn_Cmp_Eq))
277 return value_classified_unknown;
279 c = get_mode_minus_one(mode);
281 ncmp = tarval_cmp(tv, c);
283 if (ncmp != pn_Cmp_Eq && ncmp != pn_Cmp_Gt)
284 return value_classified_unknown;
288 return value_classified_positive;
291 return value_classified_unknown;
293 } /* classify_value_sign */
296 * construct an interval from a value
298 * @return the filled interval or NULL if no interval
299 * can be created (happens only on floating point
301 static interval_t *get_interval_from_tv(interval_t *iv, tarval *tv) {
302 ir_mode *mode = get_tarval_mode(tv);
304 if (tv == tarval_bad) {
305 if (mode_is_float(mode)) {
306 /* NaN could be included which we cannot handle */
307 iv->min = tarval_bad;
308 iv->max = tarval_bad;
309 iv->flags = MIN_EXCLUDED | MAX_EXCLUDED;
313 iv->min = get_mode_min(mode);
314 iv->max = get_mode_max(mode);
315 iv->flags = MIN_INCLUDED | MAX_INCLUDED;
320 if (mode_is_float(mode)) {
321 if (tv == get_mode_NAN(mode)) {
322 /* arg, we cannot handle NaN's. */
323 iv->min = tarval_bad;
324 iv->max = tarval_bad;
325 iv->flags = MIN_EXCLUDED | MAX_EXCLUDED;
333 iv->flags = MIN_INCLUDED | MAX_INCLUDED;
336 } /* get_interval_from_tv */
339 * construct an interval from a Confirm
341 * @param iv an empty interval, will be filled
342 * @param bound the bound value
343 * @param pnc the Confirm compare relation
345 * @return the filled interval or NULL if no interval
346 * can be created (happens only on floating point
348 static interval_t *get_interval(interval_t *iv, ir_node *bound, pn_Cmp pnc) {
349 ir_mode *mode = get_irn_mode(bound);
350 tarval *tv = value_of(bound);
352 if (tv == tarval_bad) {
353 /* There is nothing we could do here. For integer
354 * modes we could return [-oo, +oo], but there is
355 * nothing we could deduct from such an interval.
356 * So, speed things up and return unknown.
358 iv->min = tarval_bad;
359 iv->max = tarval_bad;
360 iv->flags = MIN_EXCLUDED | MAX_EXCLUDED;
364 if (mode_is_float(mode)) {
365 if (tv == get_mode_NAN(mode)) {
366 /* arg, we cannot handle NaN's. */
367 iv->min = tarval_bad;
368 iv->max = tarval_bad;
369 iv->flags = MIN_EXCLUDED | MAX_EXCLUDED;
375 /* check which side is known */
381 iv->flags = MIN_INCLUDED | MAX_INCLUDED;
386 iv->min = get_mode_min(mode);
388 iv->flags = MIN_INCLUDED | MAX_INCLUDED;
393 iv->min = get_mode_min(mode);
395 iv->flags = MIN_INCLUDED | MAX_EXCLUDED;
401 iv->max = get_mode_max(mode);
402 iv->flags = MIN_EXCLUDED | MAX_INCLUDED;
408 iv->max = get_mode_max(mode);
409 iv->flags = MIN_INCLUDED | MAX_INCLUDED;
414 * Ordered means, that at least neither
415 * our bound nor our value ara NaN's
418 iv->min = get_mode_min(mode);
419 iv->max = get_mode_max(mode);
420 iv->flags = MIN_INCLUDED | MAX_INCLUDED;
425 * We do not handle UNORDERED, as a NaN
426 * could be included in the interval.
428 iv->min = tarval_bad;
429 iv->max = tarval_bad;
430 iv->flags = MIN_EXCLUDED | MAX_EXCLUDED;
434 if (iv->min != tarval_bad && iv->max != tarval_bad)
440 * Try to evaluate l_iv pnc r_iv.
442 * @param l_iv the left interval
443 * @param r_iv the right interval
444 * @param pnc the compare relation
447 * tarval_b_true or tarval_b_false it it can be evaluated,
450 static tarval *(compare_iv)(const interval_t *l_iv, const interval_t *r_iv, pn_Cmp pnc) {
453 tarval *tv_true = tarval_b_true, *tv_false = tarval_b_false;
455 /* if one interval contains NaNs, we cannot evaluate anything */
456 if (! l_iv || ! r_iv)
459 /* we can only check ordered relations */
460 if (pnc & pn_Cmp_Uo) {
463 pnc = get_negated_pnc(pnc, get_tarval_mode(l_iv->min));
469 /* if we have > or >=, we do the inverse to save some cases */
470 if (pnc == pn_Cmp_Ge || pnc == pn_Cmp_Gt) {
473 pnc = get_inversed_pnc(pnc);
479 /* now, only the following cases remains */
482 /* two intervals can be compared for equality only if they are a single value */
483 if (l_iv->min == l_iv->max && r_iv->min == r_iv->max)
484 return tarval_cmp(l_iv->min, r_iv->min) == pn_Cmp_Eq ? tv_true : tv_false;
486 /* if both intervals do not intersect, it is never equal */
487 res = tarval_cmp(l_iv->max, r_iv->min);
489 /* b < c ==> [a,b] != [c,d] */
490 if (res == pn_Cmp_Lt)
493 /* b <= c ==> [a,b) != [c,d] AND [a,b] != (c,d] */
494 if ((l_iv->flags & MAX_EXCLUDED || r_iv->flags & MIN_EXCLUDED)
495 && (res == pn_Cmp_Eq))
498 res = tarval_cmp(r_iv->max, l_iv->min);
500 /* d < a ==> [c,d] != [a,b] */
501 if (res == pn_Cmp_Lt)
504 /* d <= a ==> [c,d) != [a,b] AND [c,d] != (a,b] */
505 if ((r_iv->flags & MAX_EXCLUDED || l_iv->flags & MIN_EXCLUDED)
506 && (res == pn_Cmp_Eq))
511 /* two intervals can be compared for not equality only if they are a single value */
512 if (l_iv->min == l_iv->max && r_iv->min == r_iv->max)
513 return tarval_cmp(l_iv->min, r_iv->min) != pn_Cmp_Eq ? tv_true : tv_false;
517 res = tarval_cmp(l_iv->max, r_iv->min);
519 /* [a, b] < [c, d] <==> b < c */
520 if (res == pn_Cmp_Lt)
523 /* if one border is excluded, b <= c is enough */
524 if ((l_iv->flags & MAX_EXCLUDED || r_iv->flags & MIN_EXCLUDED) &&
528 /* [a, b] >= [c, d] <==> a > d */
529 res = tarval_cmp(l_iv->min, r_iv->max);
530 if (res == pn_Cmp_Gt)
533 /* if one border is excluded, a >= d is enough */
534 if ((l_iv->flags & MIN_EXCLUDED || r_iv->flags & MAX_EXCLUDED) &&
540 /* [a, b) <= [c, d] or [a, b] <= (c, d] <==> b <= c */
541 flags = (l_iv->flags & MAX_EXCLUDED) | (r_iv->flags & MIN_EXCLUDED);
543 res = tarval_cmp(l_iv->max, r_iv->min);
545 if (res == pn_Cmp_Lt || res == pn_Cmp_Eq)
549 res = tarval_cmp(l_iv->min, r_iv->max);
551 /* [a, b] > [c, d] <==> a > d */
552 if (res == pn_Cmp_Gt)
555 /* if one border is excluded, a >= d is enough */
556 if ((l_iv->flags & MIN_EXCLUDED || r_iv->flags & MAX_EXCLUDED) &&
562 /* Hmm. if both are intervals, we can find an order */
572 * Returns non-zero, if a given relation is transitive.
574 static int is_transitive(pn_Cmp pnc) {
575 return (pn_Cmp_False < pnc && pnc < pn_Cmp_Lg);
576 } /* is_transitive */
579 * Return the value of a Cmp if one or both predecessors
582 * @param cmp the Cmp node
583 * @param left the left operand of the Cmp
584 * @param right the right operand of the Cmp
585 * @param pnc the compare relation
587 tarval *computed_value_Cmp_Confirm(ir_node *cmp, ir_node *left, ir_node *right, pn_Cmp pnc) {
589 pn_Cmp l_pnc, res_pnc, neg_pnc;
590 interval_t l_iv, r_iv;
594 if (is_Confirm(right)) {
595 /* we want the Confirm on the left side */
600 pnc = get_inversed_pnc(pnc);
601 } else if (! is_Confirm(left)) {
602 /* no Confirm on either one side, finish */
606 /* ok, here at least left is a Confirm, right might be */
607 l_bound = get_Confirm_bound(left);
608 l_pnc = get_Confirm_cmp(left);
610 if (is_Confirm(right)) {
612 * both sides are Confirm's. Check some rare cases first.
614 ir_node *r_bound = get_Confirm_bound(right);
615 pn_Cmp r_pnc = get_Confirm_cmp(right);
618 * some check can be made WITHOUT constant bounds
620 if (r_bound == l_bound) {
621 if (is_transitive(l_pnc)) {
622 pn_Cmp r_inc_pnc = get_inversed_pnc(r_pnc);
625 * triangle inequality:
627 * a CMP B && B CMP b => a CMP b, !(a ~CMP b)
629 * We handle correctly cases with some <=/>= here
631 if ((l_pnc & ~pn_Cmp_Eq) == (r_inc_pnc & ~pn_Cmp_Eq)) {
632 res_pnc = (l_pnc & ~pn_Cmp_Eq) | (l_pnc & r_inc_pnc & pn_Cmp_Eq);
634 if ((pnc == res_pnc) || ((pnc & ~pn_Cmp_Eq) == res_pnc)) {
635 DBG_OUT_TR(l_pnc, l_bound, r_pnc, r_bound, pnc, "true");
636 DBG_EVAL_CONFIRM(cmp);
637 return tarval_b_true;
639 pn_Cmp neg_pnc = get_negated_pnc(pnc, get_irn_mode(left));
641 if ((neg_pnc == res_pnc) || ((neg_pnc & ~pn_Cmp_Eq) == res_pnc)) {
642 DBG_OUT_TR(l_pnc, l_bound, r_pnc, r_bound, pnc, "false");
643 DBG_EVAL_CONFIRM(cmp);
644 return tarval_b_false;
652 * Here, we check only the right Confirm, as the left Confirms are
653 * checked later anyway.
655 if (left == r_bound) {
657 * l == bound(r) AND pnc(r) == pnc:
659 * We know that a CMP b and check for that
661 if ((r_pnc == pnc) || (r_pnc == (pnc & ~pn_Cmp_Eq))) {
662 DBG_OUT_R(r_pnc, r_bound, left, pnc, right, "true");
663 DBG_EVAL_CONFIRM(cmp);
664 return tarval_b_true;
667 * l == bound(r) AND pnc(r) != pnc:
669 * We know that a CMP b and check for a ~CMP b
672 mode = get_irn_mode(left);
673 neg_pnc = get_negated_pnc(pnc, mode);
675 if ((r_pnc == neg_pnc) || (r_pnc == (neg_pnc & ~pn_Cmp_Eq))) {
676 DBG_OUT_R(r_pnc, r_bound, left, pnc, right, "false");
677 DBG_EVAL_CONFIRM(cmp);
678 return tarval_b_false;
683 /* now, try interval magic */
685 get_interval(&l_iv, l_bound, l_pnc),
686 get_interval(&r_iv, r_bound, r_pnc),
689 if (tv != tarval_bad) {
690 DBG_EVAL_CONFIRM(cmp);
695 /* from Here, check only left Confirm */
698 * some checks can be made WITHOUT constant bounds
700 if (right == l_bound) {
702 * r == bound(l) AND pnc(l) == pnc:
704 * We know that a CMP b and check for that
706 if ((l_pnc == pnc) || (l_pnc == (pnc & ~pn_Cmp_Eq))) {
707 DBG_OUT_L(l_pnc, l_bound, left, pnc, right, "true");
708 DBG_EVAL_CONFIRM(cmp);
709 return tarval_b_true;
712 * r == bound(l) AND pnc(l) is Not(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 ((l_pnc == neg_pnc) || (l_pnc == (neg_pnc & ~pn_Cmp_Eq))) {
721 DBG_OUT_L(l_pnc, l_bound, left, pnc, right, "false");
722 DBG_EVAL_CONFIRM(cmp);
723 return tarval_b_false;
728 /* now, only right == Const can help */
729 tv = value_of(right);
731 if (tv != tarval_bad) {
733 get_interval(&l_iv, l_bound, l_pnc),
734 get_interval_from_tv(&r_iv, tv),
738 if (tv != tarval_bad)
739 DBG_EVAL_CONFIRM(cmp);
742 } /* computed_value_Cmp_Confirm */
746 * For debugging. Prints an interval into a string.
748 * @param buf address of a string buffer
749 * @param len length of the string buffer
750 * @param iv the interval
752 static int iv_snprintf(char *buf, size_t len, const interval_t *iv) {
753 char smin[64], smax[64];
756 tarval_snprintf(smin, sizeof(smin), iv->min);
758 if (iv->min != iv->max || (iv->flags & (MIN_EXCLUDED|MAX_EXCLUDED))) {
759 tarval_snprintf(smax, sizeof(smax), iv->max);
761 return snprintf(buf, len, "%c%s, %s%c",
762 iv->flags & MIN_EXCLUDED ? '(' : '[',
764 iv->flags & MAX_EXCLUDED ? ')' : ']'
767 return snprintf(buf, len, "%s", smin);
769 return snprintf(buf, len, "<UNKNOWN>");
773 * For debugging. Prints an interval compare.
775 * @param l_iv the left interval
776 * @param r_iv the right interval
777 * @param pnc the compare relation
779 static void print_iv_cmp(const interval_t *l_iv, const interval_t *r_iv, pn_Cmp pnc) {
780 char sl[128], sr[128];
782 iv_snprintf(sl, sizeof(sl), l_iv);
783 iv_snprintf(sr, sizeof(sr), r_iv);
785 ir_printf("%s %= %s", sl, pnc, sr);
789 * For debugging. call *compare_iv() and prints inputs and result.
791 * @param l_iv the left interval
792 * @param r_iv the right interval
793 * @param pnc the compare relation
795 static tarval *compare_iv_dbg(const interval_t *l_iv, const interval_t *r_iv, pn_Cmp pnc) {
796 tarval *tv = (compare_iv)(l_iv, r_iv, pnc);
798 if (tv == tarval_bad)
801 ir_printf("In %e:\n", get_irg_entity(current_ir_graph));
802 print_iv_cmp(l_iv, r_iv, pnc);
803 ir_printf(" = %T\n", tv);
805 } /* compare_iv_dbg */
807 #endif /* DEBUG_CONFIRM */