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) {
99 #define RET_ON(x) if (x) { *confirm = n; return 1; }; break
102 ir_mode *mode = get_irn_mode(n);
107 /* there might be several Confirms one after other that form an interval */
109 if (is_Minus(n) || is_Abs(n)) {
110 /* we can safely skip Minus and Abs when checking for != 0 */
118 * Note: A Confirm is never after a Const. So,
119 * we simply can check the bound for being a Const
120 * without the fear that is might be hidden by a further Confirm.
122 tv = value_of(get_Confirm_bound(n));
123 if (tv == tarval_bad)
126 pnc = tarval_cmp(tv, get_mode_null(mode));
129 * Beware: C might by a NaN. It is not clear, what we should do
130 * than. Of course a NaN is != 0, but we might use this function
131 * to remove up Exceptions, and NaN's might generate Exception.
132 * So, we do NOT handle NaNs here for safety.
134 * Note that only the C != 0 case need additional checking.
136 switch (get_Confirm_cmp(n)) {
137 case pn_Cmp_Eq: /* n == C /\ C != 0 ==> n != 0 */
138 RET_ON(pnc != pn_Cmp_Eq && pnc != pn_Cmp_Uo);
139 case pn_Cmp_Lg: /* n != C /\ C == 0 ==> n != 0 */
140 RET_ON(pnc == pn_Cmp_Eq);
141 case pn_Cmp_Lt: /* n < C /\ C <= 0 ==> n != 0 */
142 RET_ON(pnc == pn_Cmp_Lt || pnc == pn_Cmp_Eq);
143 case pn_Cmp_Le: /* n <= C /\ C < 0 ==> n != 0 */
144 RET_ON(pnc == pn_Cmp_Lt);
145 case pn_Cmp_Ge: /* n >= C /\ C > 0 ==> n != 0 */
146 RET_ON(pnc == pn_Cmp_Gt);
147 case pn_Cmp_Gt: /* n > C /\ C >= 0 ==> n != 0 */
148 RET_ON(pnc == pn_Cmp_Gt || pnc == pn_Cmp_Eq);
152 n = get_Confirm_value(n);
156 if (tv == tarval_bad)
159 pnc = tarval_cmp(tv, get_mode_null(mode));
161 /* again, need check for NaN */
162 return (pnc != pn_Cmp_Eq) && (pnc != pn_Cmp_Uo);
165 } /* value_not_zero */
168 * Check, if the value of a node cannot represent a NULL pointer.
170 * - Casts are skipped
171 * - If sel_based_null_check_elim is enabled, all
172 * Sel nodes can be skipped.
173 * - A SymConst(entity) is NEVER a NULL pointer
174 * - Confirms are evaluated
176 int value_not_null(const ir_node *n, ir_node_cnst_ptr *confirm) {
180 n = skip_Cast_const(n);
183 if (tarval_is_constant(tv) && ! tarval_is_null(tv))
186 assert(mode_is_reference(get_irn_mode(n)));
187 if (get_opt_sel_based_null_check_elim()) {
188 /* skip all Sel nodes and Cast's */
190 n = skip_Cast(get_Sel_ptr(n));
194 /* global references are never NULL */
196 } else if (n == get_irg_frame(current_ir_graph)) {
197 /* local references are never NULL */
200 /* check for more Confirms */
201 for (; is_Confirm(n); n = skip_Cast(get_Confirm_value(n))) {
202 if (get_Confirm_cmp(n) == pn_Cmp_Lg) {
203 ir_node *bound = get_Confirm_bound(n);
204 tarval *tv = value_of(bound);
206 if (tarval_is_null(tv)) {
214 } /* value_not_null */
217 * Check, if the value of a node can be confirmed >= 0 or <= 0,
218 * If the mode of the value did not honor signed zeros, else
219 * check for >= 0 or < 0.
221 value_classify_sign classify_value_sign(ir_node *n) {
228 ir_opcode code = get_irn_opcode(n);
238 return value_classified_unknown;
243 return value_classified_unknown;
245 tv = value_of(get_Confirm_bound(n));
246 if (tv == tarval_bad)
247 return value_classified_unknown;
249 mode = get_irn_mode(n);
252 * We can handle only >=, >, <, <= cases.
253 * We could handle == too, but this will be optimized into
256 * Note that for integer modes we have a slightly better
257 * optimization possibilities, so we handle this
260 cmp = get_Confirm_cmp(n);
265 * must be x < c <= 1 to be useful if integer mode and -0 = 0
266 * x < c <= 0 to be useful else
270 * must be x <= c < 1 to be useful if integer mode and -0 = 0
271 * x <= c < 0 to be useful else
273 c = mode_is_int(mode) && mode_honor_signed_zeros(mode) ?
274 get_mode_one(mode) : get_mode_null(mode);
276 ncmp = tarval_cmp(tv, c);
277 if (ncmp == pn_Cmp_Eq)
280 if (cmp != (ncmp ^ pn_Cmp_Eq))
281 return value_classified_unknown;
284 return value_classified_negative * negate;
288 * must be x >= c > -1 to be useful if integer mode
289 * x >= c >= 0 to be useful else
293 * must be x > c >= -1 to be useful if integer mode
294 * x > c >= 0 to be useful else
296 if (mode_is_int(mode)) {
297 c = get_mode_minus_one(mode);
299 ncmp = tarval_cmp(tv, c);
300 if (ncmp == pn_Cmp_Eq)
303 if (cmp != (ncmp ^ pn_Cmp_Eq))
304 return value_classified_unknown;
306 c = get_mode_minus_one(mode);
308 ncmp = tarval_cmp(tv, c);
310 if (ncmp != pn_Cmp_Eq && ncmp != pn_Cmp_Gt)
311 return value_classified_unknown;
315 return value_classified_positive * negate;
318 return value_classified_unknown;
320 } /* classify_value_sign */
323 * construct an interval from a value
325 * @return the filled interval or NULL if no interval
326 * can be created (happens only on floating point
328 static interval_t *get_interval_from_tv(interval_t *iv, tarval *tv) {
329 ir_mode *mode = get_tarval_mode(tv);
331 if (tv == tarval_bad) {
332 if (mode_is_float(mode)) {
333 /* NaN could be included which we cannot handle */
334 iv->min = tarval_bad;
335 iv->max = tarval_bad;
336 iv->flags = MIN_EXCLUDED | MAX_EXCLUDED;
340 iv->min = get_mode_min(mode);
341 iv->max = get_mode_max(mode);
342 iv->flags = MIN_INCLUDED | MAX_INCLUDED;
347 if (mode_is_float(mode)) {
348 if (tv == get_mode_NAN(mode)) {
349 /* arg, we cannot handle NaN's. */
350 iv->min = tarval_bad;
351 iv->max = tarval_bad;
352 iv->flags = MIN_EXCLUDED | MAX_EXCLUDED;
360 iv->flags = MIN_INCLUDED | MAX_INCLUDED;
363 } /* get_interval_from_tv */
366 * construct an interval from a Confirm
368 * @param iv an empty interval, will be filled
369 * @param bound the bound value
370 * @param pnc the Confirm compare relation
372 * @return the filled interval or NULL if no interval
373 * can be created (happens only on floating point
375 static interval_t *get_interval(interval_t *iv, ir_node *bound, pn_Cmp pnc) {
376 ir_mode *mode = get_irn_mode(bound);
377 tarval *tv = value_of(bound);
379 if (tv == tarval_bad) {
380 /* There is nothing we could do here. For integer
381 * modes we could return [-oo, +oo], but there is
382 * nothing we could deduct from such an interval.
383 * So, speed things up and return unknown.
385 iv->min = tarval_bad;
386 iv->max = tarval_bad;
387 iv->flags = MIN_EXCLUDED | MAX_EXCLUDED;
391 if (mode_is_float(mode)) {
392 if (tv == get_mode_NAN(mode)) {
393 /* arg, we cannot handle NaN's. */
394 iv->min = tarval_bad;
395 iv->max = tarval_bad;
396 iv->flags = MIN_EXCLUDED | MAX_EXCLUDED;
402 /* check which side is known */
408 iv->flags = MIN_INCLUDED | MAX_INCLUDED;
413 iv->min = get_mode_min(mode);
415 iv->flags = MIN_INCLUDED | MAX_INCLUDED;
420 iv->min = get_mode_min(mode);
422 iv->flags = MIN_INCLUDED | MAX_EXCLUDED;
428 iv->max = get_mode_max(mode);
429 iv->flags = MIN_EXCLUDED | MAX_INCLUDED;
435 iv->max = get_mode_max(mode);
436 iv->flags = MIN_INCLUDED | MAX_INCLUDED;
441 * Ordered means, that at least neither
442 * our bound nor our value ara NaN's
445 iv->min = get_mode_min(mode);
446 iv->max = get_mode_max(mode);
447 iv->flags = MIN_INCLUDED | MAX_INCLUDED;
452 * We do not handle UNORDERED, as a NaN
453 * could be included in the interval.
455 iv->min = tarval_bad;
456 iv->max = tarval_bad;
457 iv->flags = MIN_EXCLUDED | MAX_EXCLUDED;
461 if (iv->min != tarval_bad && iv->max != tarval_bad)
467 * Try to evaluate l_iv pnc r_iv.
469 * @param l_iv the left interval
470 * @param r_iv the right interval
471 * @param pnc the compare relation
474 * tarval_b_true or tarval_b_false it it can be evaluated,
477 static tarval *(compare_iv)(const interval_t *l_iv, const interval_t *r_iv, pn_Cmp pnc) {
480 tarval *tv_true = tarval_b_true, *tv_false = tarval_b_false;
482 /* if one interval contains NaNs, we cannot evaluate anything */
483 if (! l_iv || ! r_iv)
486 /* we can only check ordered relations */
487 if (pnc & pn_Cmp_Uo) {
490 pnc = get_negated_pnc(pnc, get_tarval_mode(l_iv->min));
496 /* if we have > or >=, we do the inverse to save some cases */
497 if (pnc == pn_Cmp_Ge || pnc == pn_Cmp_Gt) {
500 pnc = get_inversed_pnc(pnc);
506 /* now, only the following cases remains */
509 /* two intervals can be compared for equality only if they are a single value */
510 if (l_iv->min == l_iv->max && r_iv->min == r_iv->max)
511 return tarval_cmp(l_iv->min, r_iv->min) == pn_Cmp_Eq ? tv_true : tv_false;
513 /* if both intervals do not intersect, it is never equal */
514 res = tarval_cmp(l_iv->max, r_iv->min);
516 /* b < c ==> [a,b] != [c,d] */
517 if (res == pn_Cmp_Lt)
520 /* b <= c ==> [a,b) != [c,d] AND [a,b] != (c,d] */
521 if ((l_iv->flags & MAX_EXCLUDED || r_iv->flags & MIN_EXCLUDED)
522 && (res == pn_Cmp_Eq))
525 res = tarval_cmp(r_iv->max, l_iv->min);
527 /* d < a ==> [c,d] != [a,b] */
528 if (res == pn_Cmp_Lt)
531 /* d <= a ==> [c,d) != [a,b] AND [c,d] != (a,b] */
532 if ((r_iv->flags & MAX_EXCLUDED || l_iv->flags & MIN_EXCLUDED)
533 && (res == pn_Cmp_Eq))
538 /* two intervals can be compared for not equality only if they are a single value */
539 if (l_iv->min == l_iv->max && r_iv->min == r_iv->max)
540 return tarval_cmp(l_iv->min, r_iv->min) != pn_Cmp_Eq ? tv_true : tv_false;
544 res = tarval_cmp(l_iv->max, r_iv->min);
546 /* [a, b] < [c, d] <==> b < c */
547 if (res == pn_Cmp_Lt)
550 /* if one border is excluded, b <= c is enough */
551 if ((l_iv->flags & MAX_EXCLUDED || r_iv->flags & MIN_EXCLUDED) &&
555 /* [a, b] >= [c, d] <==> a > d */
556 res = tarval_cmp(l_iv->min, r_iv->max);
557 if (res == pn_Cmp_Gt)
560 /* if one border is excluded, a >= d is enough */
561 if ((l_iv->flags & MIN_EXCLUDED || r_iv->flags & MAX_EXCLUDED) &&
567 /* [a, b) <= [c, d] or [a, b] <= (c, d] <==> b <= c */
568 flags = (l_iv->flags & MAX_EXCLUDED) | (r_iv->flags & MIN_EXCLUDED);
570 res = tarval_cmp(l_iv->max, r_iv->min);
572 if (res == pn_Cmp_Lt || res == pn_Cmp_Eq)
576 res = tarval_cmp(l_iv->min, r_iv->max);
578 /* [a, b] > [c, d] <==> a > d */
579 if (res == pn_Cmp_Gt)
582 /* if one border is excluded, a >= d is enough */
583 if ((l_iv->flags & MIN_EXCLUDED || r_iv->flags & MAX_EXCLUDED) &&
589 /* Hmm. if both are intervals, we can find an order */
599 * Returns non-zero, if a given relation is transitive.
601 static int is_transitive(pn_Cmp pnc) {
602 return (pn_Cmp_False < pnc && pnc < pn_Cmp_Lg);
603 } /* is_transitive */
606 * Return the value of a Cmp if one or both predecessors
609 * @param cmp the Cmp node
610 * @param left the left operand of the Cmp
611 * @param right the right operand of the Cmp
612 * @param pnc the compare relation
614 tarval *computed_value_Cmp_Confirm(ir_node *cmp, ir_node *left, ir_node *right, pn_Cmp pnc) {
616 pn_Cmp l_pnc, res_pnc, neg_pnc;
617 interval_t l_iv, r_iv;
621 if (is_Confirm(right)) {
622 /* we want the Confirm on the left side */
627 pnc = get_inversed_pnc(pnc);
628 } else if (! is_Confirm(left)) {
629 /* nothing more found */
631 goto check_null_case;
634 /* ok, here at least left is a Confirm, right might be */
635 l_bound = get_Confirm_bound(left);
636 l_pnc = get_Confirm_cmp(left);
638 if (is_Confirm(right)) {
640 * both sides are Confirm's. Check some rare cases first.
642 ir_node *r_bound = get_Confirm_bound(right);
643 pn_Cmp r_pnc = get_Confirm_cmp(right);
646 * some check can be made WITHOUT constant bounds
648 if (r_bound == l_bound) {
649 if (is_transitive(l_pnc)) {
650 pn_Cmp r_inc_pnc = get_inversed_pnc(r_pnc);
653 * triangle inequality:
655 * a CMP B && B CMP b => a CMP b, !(a ~CMP b)
657 * We handle correctly cases with some <=/>= here
659 if ((l_pnc & ~pn_Cmp_Eq) == (r_inc_pnc & ~pn_Cmp_Eq)) {
660 res_pnc = (l_pnc & ~pn_Cmp_Eq) | (l_pnc & r_inc_pnc & pn_Cmp_Eq);
662 if ((pnc == res_pnc) || ((pnc & ~pn_Cmp_Eq) == res_pnc)) {
663 DBG_OUT_TR(l_pnc, l_bound, r_pnc, r_bound, pnc, "true");
664 DBG_EVAL_CONFIRM(cmp);
665 return tarval_b_true;
667 pn_Cmp neg_pnc = get_negated_pnc(pnc, get_irn_mode(left));
669 if ((neg_pnc == res_pnc) || ((neg_pnc & ~pn_Cmp_Eq) == res_pnc)) {
670 DBG_OUT_TR(l_pnc, l_bound, r_pnc, r_bound, pnc, "false");
671 DBG_EVAL_CONFIRM(cmp);
672 return tarval_b_false;
680 * Here, we check only the right Confirm, as the left Confirms are
681 * checked later anyway.
683 if (left == r_bound) {
685 * l == bound(r) AND pnc(r) == pnc:
687 * We know that a CMP b and check for that
689 if ((r_pnc == pnc) || (r_pnc == (pnc & ~pn_Cmp_Eq))) {
690 DBG_OUT_R(r_pnc, r_bound, left, pnc, right, "true");
691 DBG_EVAL_CONFIRM(cmp);
692 return tarval_b_true;
695 * l == bound(r) AND pnc(r) != pnc:
697 * We know that a CMP b and check for a ~CMP b
700 mode = get_irn_mode(left);
701 neg_pnc = get_negated_pnc(pnc, mode);
703 if ((r_pnc == neg_pnc) || (r_pnc == (neg_pnc & ~pn_Cmp_Eq))) {
704 DBG_OUT_R(r_pnc, r_bound, left, pnc, right, "false");
705 DBG_EVAL_CONFIRM(cmp);
706 return tarval_b_false;
711 /* now, try interval magic */
713 get_interval(&l_iv, l_bound, l_pnc),
714 get_interval(&r_iv, r_bound, r_pnc),
717 if (tv != tarval_bad) {
718 DBG_EVAL_CONFIRM(cmp);
723 /* from Here, check only left Confirm */
726 * some checks can be made WITHOUT constant bounds
728 if (right == l_bound) {
730 * r == bound(l) AND pnc(l) == pnc:
732 * We know that a CMP b and check for that
734 if ((l_pnc == pnc) || (l_pnc == (pnc & ~pn_Cmp_Eq))) {
735 DBG_OUT_L(l_pnc, l_bound, left, pnc, right, "true");
736 DBG_EVAL_CONFIRM(cmp);
737 return tarval_b_true;
740 * r == bound(l) AND pnc(l) is Not(pnc):
742 * We know that a CMP b and check for a ~CMP b
745 mode = get_irn_mode(left);
746 neg_pnc = get_negated_pnc(pnc, mode);
748 if ((l_pnc == neg_pnc) || (l_pnc == (neg_pnc & ~pn_Cmp_Eq))) {
749 DBG_OUT_L(l_pnc, l_bound, left, pnc, right, "false");
750 DBG_EVAL_CONFIRM(cmp);
751 return tarval_b_false;
756 /* now, only right == Const can help */
757 tv = value_of(right);
759 if (tv != tarval_bad) {
761 get_interval(&l_iv, l_bound, l_pnc),
762 get_interval_from_tv(&r_iv, tv),
766 /* check some other cases */
767 if ((pnc == pn_Cmp_Eq || pnc == pn_Cmp_Lg) &&
768 is_Const(right) && is_Const_null(right)) {
769 /* for == 0 or != 0 we have some special tools */
770 ir_mode *mode = get_irn_mode(left);
771 const ir_node *dummy;
772 if (mode_is_reference(mode)) {
773 if (value_not_null(left, &dummy)) {
774 tv = pnc == pn_Cmp_Eq ? tarval_b_false : tarval_b_true;
777 if (value_not_zero(left, &dummy)) {
778 tv = pnc == pn_Cmp_Eq ? tarval_b_false : tarval_b_true;
784 if (tv != tarval_bad)
785 DBG_EVAL_CONFIRM(cmp);
788 } /* computed_value_Cmp_Confirm */
792 * For debugging. Prints an interval into a string.
794 * @param buf address of a string buffer
795 * @param len length of the string buffer
796 * @param iv the interval
798 static int iv_snprintf(char *buf, size_t len, const interval_t *iv) {
799 char smin[64], smax[64];
802 tarval_snprintf(smin, sizeof(smin), iv->min);
804 if (iv->min != iv->max || (iv->flags & (MIN_EXCLUDED|MAX_EXCLUDED))) {
805 tarval_snprintf(smax, sizeof(smax), iv->max);
807 return snprintf(buf, len, "%c%s, %s%c",
808 iv->flags & MIN_EXCLUDED ? '(' : '[',
810 iv->flags & MAX_EXCLUDED ? ')' : ']'
813 return snprintf(buf, len, "%s", smin);
815 return snprintf(buf, len, "<UNKNOWN>");
819 * For debugging. Prints an interval compare.
821 * @param l_iv the left interval
822 * @param r_iv the right interval
823 * @param pnc the compare relation
825 static void print_iv_cmp(const interval_t *l_iv, const interval_t *r_iv, pn_Cmp pnc) {
826 char sl[128], sr[128];
828 iv_snprintf(sl, sizeof(sl), l_iv);
829 iv_snprintf(sr, sizeof(sr), r_iv);
831 ir_printf("%s %= %s", sl, pnc, sr);
835 * For debugging. call *compare_iv() and prints inputs and result.
837 * @param l_iv the left interval
838 * @param r_iv the right interval
839 * @param pnc the compare relation
841 static tarval *compare_iv_dbg(const interval_t *l_iv, const interval_t *r_iv, pn_Cmp pnc) {
842 tarval *tv = (compare_iv)(l_iv, r_iv, pnc);
844 if (tv == tarval_bad)
847 ir_printf("In %e:\n", get_irg_entity(current_ir_graph));
848 print_iv_cmp(l_iv, r_iv, pnc);
849 ir_printf(" = %T\n", tv);
851 } /* compare_iv_dbg */
853 #endif /* DEBUG_CONFIRM */