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 * File name: ir/opt/opt_confirms.c
23 * Purpose: Optimizations regarding Confirm nodes
24 * Author: Michael Beck
28 * Copyright: (c) 1998-2005 Universität Karlsruhe
39 #include "iropt_dbg.h"
40 #include "opt_confirms.h"
45 MIN_INCLUDED = 0x00, /**< [min, ... */
46 MAX_INCLUDED = 0x00, /**< ..., max] */
47 MIN_EXCLUDED = 0x01, /**< (min, ... */
48 MAX_EXCLUDED = 0x02 /**< ..., max) */
52 * An interval. We could use
53 * intervals that ALWAYS include its borders, even for
54 * floating point, as the precision is limited.
55 * However, as our tarval module did not support
56 * such kind of operation, we use border flags allowing
59 typedef struct _interval_t {
60 tarval *min; /**< lowest border */
61 tarval *max; /**< highest border */
62 unsigned char flags; /**< border flags */
67 #define compare_iv(l_iv, r_iv, pnc) compare_iv_dbg(l_iv, r_iv, pnc)
70 static tarval *compare_iv_dbg(const interval_t *l_iv, const interval_t *r_iv, pn_Cmp pnc);
73 #define DBG_OUT_TR(l_pnc, l_bound, r_pnc, r_bound, pnc, v) \
74 ir_printf("In %e:\na %= %n && b %= %n ==> a %= b == %s\n", \
75 get_irg_entity(current_ir_graph), \
76 l_pnc, l_bound, r_pnc, r_bound, pnc, v);
79 #define DBG_OUT_R(r_pnc, r_bound, left, pnc, right, v) \
80 ir_printf("In %e:\na %= %n ==> %n %= %n == %s\n", \
81 get_irg_entity(current_ir_graph), \
82 r_pnc, r_bound, left, pnc, right, v);
85 #define DBG_OUT_L(l_pnc, l_bound, left, pnc, right, v) \
86 ir_printf("In %e:\na %= %n ==> %n %= %n == %s\n", \
87 get_irg_entity(current_ir_graph), \
88 l_pnc, l_bound, left, pnc, right, v);
92 #define DBG_OUT_TR(l_pnc, l_bound, r_pnc, r_bound, pnc, v)
93 #define DBG_OUT_R(r_pnc, r_bound, left, pnc, right, v)
94 #define DBG_OUT_L(l_pnc, l_bound, left, pnc, right, v)
96 #endif /* DEBUG_CONFIRM */
99 * Check, if the value of a node is != 0.
101 * This is a often needed case, so we handle here Confirm
104 int value_not_zero(ir_node *n, ir_node **confirm)
106 #define RET_ON(x) if (x) { *confirm = n; return 1; }; break
109 ir_mode *mode = get_irn_mode(n);
113 while (get_irn_op(n) == op_Confirm) {
115 * Note: A Confirm is never after a Const. So,
116 * we simply can check the bound for being a Const
117 * without the fear that is might be hidden by a further Confirm.
119 tv = value_of(get_Confirm_bound(n));
120 if (tv == tarval_bad)
123 pnc = tarval_cmp(tv, get_mode_null(mode));
126 * Beware: C might by a NaN. It is not clear, what we should do
127 * than. Of course a NaN is != 0, but we might use this function
128 * to remove up Exceptions, and NaN's might generate Exception.
129 * So, we do NOT handle NaNs here for safety.
131 * Note that only the C != 0 case need additional checking.
133 switch (get_Confirm_cmp(n)) {
134 case pn_Cmp_Eq: /* n == C /\ C != 0 ==> n != 0 */
135 RET_ON(pnc != pn_Cmp_Eq && pnc != pn_Cmp_Uo);
136 case pn_Cmp_Lg: /* n != C /\ C == 0 ==> n != 0 */
137 RET_ON(pnc == pn_Cmp_Eq);
138 case pn_Cmp_Lt: /* n < C /\ C <= 0 ==> n != 0 */
139 RET_ON(pnc == pn_Cmp_Lt || pnc == pn_Cmp_Eq);
140 case pn_Cmp_Le: /* n <= C /\ C < 0 ==> n != 0 */
141 RET_ON(pnc == pn_Cmp_Lt);
142 case pn_Cmp_Ge: /* n >= C /\ C > 0 ==> n != 0 */
143 RET_ON(pnc == pn_Cmp_Gt);
144 case pn_Cmp_Gt: /* n > C /\ C >= 0 ==> n != 0 */
145 RET_ON(pnc == pn_Cmp_Gt || pnc == pn_Cmp_Eq);
150 /* there might be several Confirms one after other that form an interval */
151 n = get_Confirm_value(n);
155 if (tv == tarval_bad)
158 pnc = tarval_cmp(tv, get_mode_null(mode));
160 /* again, need check for NaN */
161 return (pnc != pn_Cmp_Eq) && (pnc != pn_Cmp_Uo);
164 } /* value_not_zero */
167 * Check, if the value of a node cannot represent a NULL pointer.
169 * - Casts are skipped
170 * - If sel_based_null_check_elim is enabled, all
171 * Sel nodes can be skipped.
172 * - A SymConst(entity) is NEVER a NULL pointer
173 * - Confirms are evaluated
175 int value_not_null(ir_node *n, ir_node **confirm)
182 assert(mode_is_reference(get_irn_mode(n)));
183 if (get_opt_sel_based_null_check_elim()) {
184 /* skip all Sel nodes and Cast's */
185 while (op == op_Sel) {
186 n = skip_Cast(get_Sel_ptr(n));
190 if (op == op_SymConst && get_SymConst_kind(n) == symconst_addr_ent)
192 if (op == op_Const) {
193 tarval *tv = get_Const_tarval(n);
195 if (tv != tarval_bad && classify_tarval(tv) != TV_CLASSIFY_NULL)
198 else if (op == op_Confirm) {
199 if (get_Confirm_cmp(n) == pn_Cmp_Lg &&
200 classify_Const(get_Confirm_bound(n)) == CNST_NULL) {
206 } /* value_not_null */
209 * Check, if the value of a node can be confirmed >= 0 or <= 0,
210 * If the mode of the value did not honor signed zeros, else
211 * check for >= 0 or < 0.
213 value_classify_sign classify_value_sign(ir_node *n)
219 if (get_irn_op(n) != op_Confirm)
220 return value_classified_unknown;
222 tv = value_of(get_Confirm_bound(n));
223 if (tv == tarval_bad)
224 return value_classified_unknown;
226 mode = get_irn_mode(n);
229 * We can handle only >=, >, <, <= cases.
230 * We could handle == too, but this will be optimized into
233 * Note that for integer modes we have a slightly better
234 * optimization possibilities, so we handle this
237 cmp = get_Confirm_cmp(n);
242 * must be x < c <= 1 to be useful if integer mode and -0 = 0
243 * x < c <= 0 to be useful else
247 * must be x <= c < 1 to be useful if integer mode and -0 = 0
248 * x <= c < 0 to be useful else
250 c = mode_is_int(mode) && mode_honor_signed_zeros(mode) ?
251 get_mode_one(mode) : get_mode_null(mode);
253 ncmp = tarval_cmp(tv, c);
254 if (ncmp == pn_Cmp_Eq)
257 if (cmp != (ncmp ^ pn_Cmp_Eq))
258 return value_classified_unknown;
261 return value_classified_negative;
265 * must be x >= c > -1 to be useful if integer mode
266 * x >= c >= 0 to be useful else
270 * must be x > c >= -1 to be useful if integer mode
271 * x > c >= 0 to be useful else
273 if (mode_is_int(mode)) {
274 c = get_mode_minus_one(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 c = get_mode_minus_one(mode);
286 ncmp = tarval_cmp(tv, c);
288 if (ncmp != pn_Cmp_Eq && ncmp != pn_Cmp_Gt)
289 return value_classified_unknown;
293 return value_classified_positive;
296 return value_classified_unknown;
298 } /* classify_value_sign */
301 * construct an interval from a value
303 * @return the filled interval or NULL if no interval
304 * can be created (happens only on floating point
306 static interval_t *get_interval_from_tv(interval_t *iv, tarval *tv)
308 ir_mode *mode = get_tarval_mode(tv);
310 if (tv == tarval_bad) {
311 if (mode_is_float(mode)) {
312 /* NaN could be included which we cannot handle */
313 iv->min = tarval_bad;
314 iv->max = tarval_bad;
315 iv->flags = MIN_EXCLUDED | MAX_EXCLUDED;
320 iv->min = get_mode_min(mode);
321 iv->max = get_mode_max(mode);
322 iv->flags = MIN_INCLUDED | MAX_INCLUDED;
327 if (mode_is_float(mode)) {
328 if (tv == get_mode_NAN(mode)) {
329 /* arg, we cannot handle NaN's. */
330 iv->min = tarval_bad;
331 iv->max = tarval_bad;
332 iv->flags = MIN_EXCLUDED | MAX_EXCLUDED;
340 iv->flags = MIN_INCLUDED | MAX_INCLUDED;
343 } /* get_interval_from_tv */
346 * construct an interval from a Confirm
348 * @param iv an empty interval, will be filled
349 * @param bound the bound value
350 * @param pnc the Confirm compare relation
352 * @return the filled interval or NULL if no interval
353 * can be created (happens only on floating point
355 static interval_t *get_interval(interval_t *iv, ir_node *bound, pn_Cmp pnc)
357 ir_mode *mode = get_irn_mode(bound);
358 tarval *tv = value_of(bound);
360 if (tv == tarval_bad) {
361 /* There is nothing we could do here. For integer
362 * modes we could return [-oo, +oo], but there is
363 * nothing we could deduct from such an interval.
364 * So, speed things up and return unknown.
366 iv->min = tarval_bad;
367 iv->max = tarval_bad;
368 iv->flags = MIN_EXCLUDED | MAX_EXCLUDED;
372 if (mode_is_float(mode)) {
373 if (tv == get_mode_NAN(mode)) {
374 /* arg, we cannot handle NaN's. */
375 iv->min = tarval_bad;
376 iv->max = tarval_bad;
377 iv->flags = MIN_EXCLUDED | MAX_EXCLUDED;
383 /* check which side is known */
389 iv->flags = MIN_INCLUDED | MAX_INCLUDED;
394 iv->min = get_mode_min(mode);
396 iv->flags = MIN_INCLUDED | MAX_INCLUDED;
401 iv->min = get_mode_min(mode);
403 iv->flags = MIN_INCLUDED | MAX_EXCLUDED;
409 iv->max = get_mode_max(mode);
410 iv->flags = MIN_EXCLUDED | MAX_INCLUDED;
416 iv->max = get_mode_max(mode);
417 iv->flags = MIN_INCLUDED | MAX_INCLUDED;
422 * Ordered means, that at least neither
423 * our bound nor our value ara NaN's
426 iv->min = get_mode_min(mode);
427 iv->max = get_mode_max(mode);
428 iv->flags = MIN_INCLUDED | MAX_INCLUDED;
433 * We do not handle UNORDERED, as a NaN
434 * could be included in the interval.
436 iv->min = tarval_bad;
437 iv->max = tarval_bad;
438 iv->flags = MIN_EXCLUDED | MAX_EXCLUDED;
442 if (iv->min != tarval_bad && iv->max != tarval_bad)
448 * Try to evaluate l_iv pnc r_iv.
450 * @param l_iv the left interval
451 * @param r_iv the right interval
452 * @param pnc the compare relation
455 * tarval_b_true or tarval_b_false it it can be evaluated,
458 static tarval *(compare_iv)(const interval_t *l_iv, const interval_t *r_iv, pn_Cmp pnc)
462 tarval *tv_true = tarval_b_true, *tv_false = tarval_b_false;
464 /* if one interval contains NaNs, we cannot evaluate anything */
465 if (! l_iv || ! r_iv)
468 /* we can only check ordered relations */
469 if (pnc & pn_Cmp_Uo) {
472 pnc = get_negated_pnc(pnc, get_tarval_mode(l_iv->min));
478 /* if we have > or >=, we do the inverse to save some cases */
479 if (pnc == pn_Cmp_Ge || pnc == pn_Cmp_Gt) {
482 pnc = get_inversed_pnc(pnc);
488 /* now, only the following cases remains */
491 /* two intervals can be compared for equality only if they are a single value */
492 if (l_iv->min == l_iv->max && r_iv->min == r_iv->max)
493 return tarval_cmp(l_iv->min, r_iv->min) == pn_Cmp_Eq ? tv_true : tv_false;
495 /* if both intervals do not intersect, it is never equal */
496 res = tarval_cmp(l_iv->max, r_iv->min);
498 /* b < c ==> [a,b] != [c,d] */
499 if (res == pn_Cmp_Lt)
502 /* b <= c ==> [a,b) != [c,d] AND [a,b] != (c,d] */
503 if ((l_iv->flags & MAX_EXCLUDED || r_iv->flags & MIN_EXCLUDED)
504 && (res == pn_Cmp_Eq))
507 res = tarval_cmp(r_iv->max, l_iv->min);
509 /* d < a ==> [c,d] != [a,b] */
510 if (res == pn_Cmp_Lt)
513 /* d <= a ==> [c,d) != [a,b] AND [c,d] != (a,b] */
514 if ((r_iv->flags & MAX_EXCLUDED || l_iv->flags & MIN_EXCLUDED)
515 && (res == pn_Cmp_Eq))
520 /* two intervals can be compared for not equality only if they are a single value */
521 if (l_iv->min == l_iv->max && r_iv->min == r_iv->max)
522 return tarval_cmp(l_iv->min, r_iv->min) != pn_Cmp_Eq ? tv_true : tv_false;
526 res = tarval_cmp(l_iv->max, r_iv->min);
528 /* [a, b] < [c, d] <==> b < c */
529 if (res == pn_Cmp_Lt)
532 /* if one border is excluded, b <= c is enough */
533 if ((l_iv->flags & MAX_EXCLUDED || r_iv->flags & MIN_EXCLUDED) &&
537 /* [a, b] >= [c, d] <==> a > d */
538 res = tarval_cmp(l_iv->min, r_iv->max);
539 if (res == pn_Cmp_Gt)
542 /* if one border is excluded, a >= d is enough */
543 if ((l_iv->flags & MIN_EXCLUDED || r_iv->flags & MAX_EXCLUDED) &&
549 /* [a, b) <= [c, d] or [a, b] <= (c, d] <==> b <= c */
550 flags = (l_iv->flags & MAX_EXCLUDED) | (r_iv->flags & MIN_EXCLUDED);
552 res = tarval_cmp(l_iv->max, r_iv->min);
554 if (res == pn_Cmp_Lt || res == pn_Cmp_Eq)
558 res = tarval_cmp(l_iv->min, r_iv->max);
560 /* [a, b] > [c, d] <==> a > d */
561 if (res == pn_Cmp_Gt)
564 /* if one border is excluded, a >= d is enough */
565 if ((l_iv->flags & MIN_EXCLUDED || r_iv->flags & MAX_EXCLUDED) &&
571 /* Hmm. if both are intervals, we can find an order */
581 * Returns non-zero, if a given relation is transitive.
583 static int is_transitive(pn_Cmp pnc) {
584 return (pn_Cmp_False < pnc && pnc < pn_Cmp_Lg);
585 } /* is_transitive */
589 * Return the value of a Cmp if one or both predecessors
592 * @param cmp the Cmp node
593 * @param left the left operand of the Cmp
594 * @param right the right operand of the Cmp
595 * @param pnc the compare relation
597 tarval *computed_value_Cmp_Confirm(ir_node *cmp, ir_node *left, ir_node *right, pn_Cmp pnc)
600 pn_Cmp l_pnc, res_pnc, neg_pnc;
601 interval_t l_iv, r_iv;
605 if (get_irn_op(right) == op_Confirm) {
608 /* we want the Confirm on the left side */
613 pnc = get_inversed_pnc(pnc);
615 else if (get_irn_op(left) != op_Confirm) {
616 /* no Confirm on either one side, finish */
620 /* ok, here at least left is a Confirm, right might be */
621 l_bound = get_Confirm_bound(left);
622 l_pnc = get_Confirm_cmp(left);
624 if (get_irn_op(right) == op_Confirm) {
626 * both sides are Confirm's. Check some rare cases first.
628 ir_node *r_bound = get_Confirm_bound(right);
629 pn_Cmp r_pnc = get_Confirm_cmp(right);
632 * some check can be made WITHOUT constant bounds
634 if (r_bound == l_bound) {
635 if (is_transitive(l_pnc)) {
636 pn_Cmp r_inc_pnc = get_inversed_pnc(r_pnc);
639 * triangle inequality:
641 * a CMP B && B CMP b => a CMP b, !(a ~CMP b)
643 * We handle correctly cases with some <=/>= here
645 if ((l_pnc & ~pn_Cmp_Eq) == (r_inc_pnc & ~pn_Cmp_Eq)) {
646 res_pnc = (l_pnc & ~pn_Cmp_Eq) | (l_pnc & r_inc_pnc & pn_Cmp_Eq);
648 if ((pnc == res_pnc) || ((pnc & ~pn_Cmp_Eq) == res_pnc)) {
649 DBG_OUT_TR(l_pnc, l_bound, r_pnc, r_bound, pnc, "true");
650 DBG_EVAL_CONFIRM(cmp);
651 return tarval_b_true;
654 pn_Cmp neg_pnc = get_negated_pnc(pnc, get_irn_mode(left));
656 if ((neg_pnc == res_pnc) || ((neg_pnc & ~pn_Cmp_Eq) == res_pnc)) {
657 DBG_OUT_TR(l_pnc, l_bound, r_pnc, r_bound, pnc, "false");
658 DBG_EVAL_CONFIRM(cmp);
659 return tarval_b_false;
667 * Here, we check only the right Confirm, as the left Confirms are
668 * checked later anyway.
671 if (left == r_bound) {
673 * l == bound(r) AND pnc(r) == pnc:
675 * We know that a CMP b and check for that
677 if ((r_pnc == pnc) || (r_pnc == (pnc & ~pn_Cmp_Eq))) {
678 DBG_OUT_R(r_pnc, r_bound, left, pnc, right, "true");
679 DBG_EVAL_CONFIRM(cmp);
680 return tarval_b_true;
683 * l == bound(r) AND pnc(r) != pnc:
685 * We know that a CMP b and check for a ~CMP b
688 mode = get_irn_mode(left);
689 neg_pnc = get_negated_pnc(pnc, mode);
691 if ((r_pnc == neg_pnc) || (r_pnc == (neg_pnc & ~pn_Cmp_Eq))) {
692 DBG_OUT_R(r_pnc, r_bound, left, pnc, right, "false");
693 DBG_EVAL_CONFIRM(cmp);
694 return tarval_b_false;
699 /* now, try interval magic */
701 get_interval(&l_iv, l_bound, l_pnc),
702 get_interval(&r_iv, r_bound, r_pnc),
705 if (tv != tarval_bad) {
706 DBG_EVAL_CONFIRM(cmp);
711 /* from Here, check only left Confirm */
714 * some checks can be made WITHOUT constant bounds
716 if (right == l_bound) {
718 * r == bound(l) AND pnc(l) == pnc:
720 * We know that a CMP b and check for that
722 if ((l_pnc == pnc) || (l_pnc == (pnc & ~pn_Cmp_Eq))) {
723 DBG_OUT_L(l_pnc, l_bound, left, pnc, right, "true");
724 DBG_EVAL_CONFIRM(cmp);
725 return tarval_b_true;
728 * r == bound(l) AND pnc(l) is Not(pnc):
730 * We know that a CMP b and check for a ~CMP b
733 mode = get_irn_mode(left);
734 neg_pnc = get_negated_pnc(pnc, mode);
736 if ((l_pnc == neg_pnc) || (l_pnc == (neg_pnc & ~pn_Cmp_Eq))) {
737 DBG_OUT_L(l_pnc, l_bound, left, pnc, right, "false");
738 DBG_EVAL_CONFIRM(cmp);
739 return tarval_b_false;
744 /* now, only right == Const can help */
745 tv = value_of(right);
747 if (tv != tarval_bad) {
749 get_interval(&l_iv, l_bound, l_pnc),
750 get_interval_from_tv(&r_iv, tv),
754 if (tv != tarval_bad)
755 DBG_EVAL_CONFIRM(cmp);
758 } /* computed_value_Cmp_Confirm */
762 * For debugging. Prints an interval into a string.
764 * @param buf address of a string buffer
765 * @param len length of the string buffer
766 * @param iv the interval
768 static int iv_snprintf(char *buf, size_t len, const interval_t *iv) {
769 char smin[64], smax[64];
772 tarval_snprintf(smin, sizeof(smin), iv->min);
774 if (iv->min != iv->max || (iv->flags & (MIN_EXCLUDED|MAX_EXCLUDED))) {
775 tarval_snprintf(smax, sizeof(smax), iv->max);
777 return snprintf(buf, len, "%c%s, %s%c",
778 iv->flags & MIN_EXCLUDED ? '(' : '[',
780 iv->flags & MAX_EXCLUDED ? ')' : ']'
784 return snprintf(buf, len, "%s", smin);
786 return snprintf(buf, len, "<UNKNOWN>");
790 * For debugging. Prints an interval compare.
792 * @param l_iv the left interval
793 * @param r_iv the right interval
794 * @param pnc the compare relation
796 static void print_iv_cmp(const interval_t *l_iv, const interval_t *r_iv, pn_Cmp pnc)
798 char sl[128], sr[128];
800 iv_snprintf(sl, sizeof(sl), l_iv);
801 iv_snprintf(sr, sizeof(sr), r_iv);
803 ir_printf("%s %= %s", sl, pnc, sr);
807 * For debugging. call *compare_iv() and prints inputs and result.
809 * @param l_iv the left interval
810 * @param r_iv the right interval
811 * @param pnc the compare relation
813 static tarval *compare_iv_dbg(const interval_t *l_iv, const interval_t *r_iv, pn_Cmp pnc)
815 tarval *tv = (compare_iv)(l_iv, r_iv, pnc);
817 if (tv == tarval_bad)
820 ir_printf("In %e:\n", get_irg_entity(current_ir_graph));
821 print_iv_cmp(l_iv, r_iv, pnc);
822 ir_printf(" = %T\n", tv);
824 } /* compare_iv_dbg */
826 #endif /* DEBUG_CONFIRM */