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
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 */
111 if (is_Minus(n) || is_Abs(n)) {
112 /* we can safely skip Minus and Abs when checking for != 0 */
120 * Note: A Confirm is never after a Const. So,
121 * we simply can check the bound for being a Const
122 * without the fear that is might be hidden by a further Confirm.
124 tv = value_of(get_Confirm_bound(n));
125 if (tv == tarval_bad)
128 pnc = tarval_cmp(tv, get_mode_null(mode));
131 * Beware: C might by a NaN. It is not clear, what we should do
132 * than. Of course a NaN is != 0, but we might use this function
133 * to remove up Exceptions, and NaN's might generate Exception.
134 * So, we do NOT handle NaNs here for safety.
136 * Note that only the C != 0 case need additional checking.
138 switch (get_Confirm_cmp(n)) {
139 case pn_Cmp_Eq: /* n == C /\ C != 0 ==> n != 0 */
140 RET_ON(pnc != pn_Cmp_Eq && pnc != pn_Cmp_Uo);
141 case pn_Cmp_Lg: /* n != C /\ C == 0 ==> n != 0 */
142 RET_ON(pnc == pn_Cmp_Eq);
143 case pn_Cmp_Lt: /* n < C /\ C <= 0 ==> n != 0 */
144 RET_ON(pnc == pn_Cmp_Lt || pnc == pn_Cmp_Eq);
145 case pn_Cmp_Le: /* n <= C /\ C < 0 ==> n != 0 */
146 RET_ON(pnc == pn_Cmp_Lt);
147 case pn_Cmp_Ge: /* n >= C /\ C > 0 ==> n != 0 */
148 RET_ON(pnc == pn_Cmp_Gt);
149 case pn_Cmp_Gt: /* n > C /\ C >= 0 ==> n != 0 */
150 RET_ON(pnc == pn_Cmp_Gt || pnc == pn_Cmp_Eq);
154 n = get_Confirm_value(n);
158 if (tv == tarval_bad)
161 pnc = tarval_cmp(tv, get_mode_null(mode));
163 /* again, need check for NaN */
164 return (pnc != pn_Cmp_Eq) && (pnc != pn_Cmp_Uo);
167 } /* value_not_zero */
170 * Check, if the value of a node cannot represent a NULL pointer.
172 * - Casts are skipped
173 * - If sel_based_null_check_elim is enabled, all
174 * Sel nodes can be skipped.
175 * - A SymConst(entity) is NEVER a NULL pointer
176 * - Confirms are evaluated
178 int value_not_null(ir_node *n, ir_node **confirm) {
185 assert(mode_is_reference(get_irn_mode(n)));
186 if (get_opt_sel_based_null_check_elim()) {
187 /* skip all Sel nodes and Cast's */
188 while (op == op_Sel) {
189 n = skip_Cast(get_Sel_ptr(n));
193 if (op == op_SymConst && get_SymConst_kind(n) == symconst_addr_ent)
195 if (op == op_Const) {
196 if (!is_Const_null(n))
199 for (; is_Confirm(n); n = skip_Cast(get_Confirm_value(n))) {
200 if (get_Confirm_cmp(n) != pn_Cmp_Lg) {
201 ir_node *bound = get_Confirm_bound(n);
202 if (is_Const(bound) && is_Const_null(bound)) {
210 } /* value_not_null */
213 * Check, if the value of a node can be confirmed >= 0 or <= 0,
214 * If the mode of the value did not honor signed zeros, else
215 * check for >= 0 or < 0.
217 value_classify_sign classify_value_sign(ir_node *n) {
224 ir_opcode code = get_irn_opcode(n);
234 return value_classified_unknown;
238 if (get_irn_op(n) != op_Confirm)
239 return value_classified_unknown;
241 tv = value_of(get_Confirm_bound(n));
242 if (tv == tarval_bad)
243 return value_classified_unknown;
245 mode = get_irn_mode(n);
248 * We can handle only >=, >, <, <= cases.
249 * We could handle == too, but this will be optimized into
252 * Note that for integer modes we have a slightly better
253 * optimization possibilities, so we handle this
256 cmp = get_Confirm_cmp(n);
261 * must be x < c <= 1 to be useful if integer mode and -0 = 0
262 * x < c <= 0 to be useful else
266 * must be x <= c < 1 to be useful if integer mode and -0 = 0
267 * x <= c < 0 to be useful else
269 c = mode_is_int(mode) && mode_honor_signed_zeros(mode) ?
270 get_mode_one(mode) : get_mode_null(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;
280 return value_classified_negative * negate;
284 * must be x >= c > -1 to be useful if integer mode
285 * x >= c >= 0 to be useful else
289 * must be x > c >= -1 to be useful if integer mode
290 * x > c >= 0 to be useful else
292 if (mode_is_int(mode)) {
293 c = get_mode_minus_one(mode);
295 ncmp = tarval_cmp(tv, c);
296 if (ncmp == pn_Cmp_Eq)
299 if (cmp != (ncmp ^ pn_Cmp_Eq))
300 return value_classified_unknown;
302 c = get_mode_minus_one(mode);
304 ncmp = tarval_cmp(tv, c);
306 if (ncmp != pn_Cmp_Eq && ncmp != pn_Cmp_Gt)
307 return value_classified_unknown;
311 return value_classified_positive * negate;
314 return value_classified_unknown;
316 } /* classify_value_sign */
319 * construct an interval from a value
321 * @return the filled interval or NULL if no interval
322 * can be created (happens only on floating point
324 static interval_t *get_interval_from_tv(interval_t *iv, tarval *tv) {
325 ir_mode *mode = get_tarval_mode(tv);
327 if (tv == tarval_bad) {
328 if (mode_is_float(mode)) {
329 /* NaN could be included which we cannot handle */
330 iv->min = tarval_bad;
331 iv->max = tarval_bad;
332 iv->flags = MIN_EXCLUDED | MAX_EXCLUDED;
336 iv->min = get_mode_min(mode);
337 iv->max = get_mode_max(mode);
338 iv->flags = MIN_INCLUDED | MAX_INCLUDED;
343 if (mode_is_float(mode)) {
344 if (tv == get_mode_NAN(mode)) {
345 /* arg, we cannot handle NaN's. */
346 iv->min = tarval_bad;
347 iv->max = tarval_bad;
348 iv->flags = MIN_EXCLUDED | MAX_EXCLUDED;
356 iv->flags = MIN_INCLUDED | MAX_INCLUDED;
359 } /* get_interval_from_tv */
362 * construct an interval from a Confirm
364 * @param iv an empty interval, will be filled
365 * @param bound the bound value
366 * @param pnc the Confirm compare relation
368 * @return the filled interval or NULL if no interval
369 * can be created (happens only on floating point
371 static interval_t *get_interval(interval_t *iv, ir_node *bound, pn_Cmp pnc) {
372 ir_mode *mode = get_irn_mode(bound);
373 tarval *tv = value_of(bound);
375 if (tv == tarval_bad) {
376 /* There is nothing we could do here. For integer
377 * modes we could return [-oo, +oo], but there is
378 * nothing we could deduct from such an interval.
379 * So, speed things up and return unknown.
381 iv->min = tarval_bad;
382 iv->max = tarval_bad;
383 iv->flags = MIN_EXCLUDED | MAX_EXCLUDED;
387 if (mode_is_float(mode)) {
388 if (tv == get_mode_NAN(mode)) {
389 /* arg, we cannot handle NaN's. */
390 iv->min = tarval_bad;
391 iv->max = tarval_bad;
392 iv->flags = MIN_EXCLUDED | MAX_EXCLUDED;
398 /* check which side is known */
404 iv->flags = MIN_INCLUDED | MAX_INCLUDED;
409 iv->min = get_mode_min(mode);
411 iv->flags = MIN_INCLUDED | MAX_INCLUDED;
416 iv->min = get_mode_min(mode);
418 iv->flags = MIN_INCLUDED | MAX_EXCLUDED;
424 iv->max = get_mode_max(mode);
425 iv->flags = MIN_EXCLUDED | MAX_INCLUDED;
431 iv->max = get_mode_max(mode);
432 iv->flags = MIN_INCLUDED | MAX_INCLUDED;
437 * Ordered means, that at least neither
438 * our bound nor our value ara NaN's
441 iv->min = get_mode_min(mode);
442 iv->max = get_mode_max(mode);
443 iv->flags = MIN_INCLUDED | MAX_INCLUDED;
448 * We do not handle UNORDERED, as a NaN
449 * could be included in the interval.
451 iv->min = tarval_bad;
452 iv->max = tarval_bad;
453 iv->flags = MIN_EXCLUDED | MAX_EXCLUDED;
457 if (iv->min != tarval_bad && iv->max != tarval_bad)
463 * Try to evaluate l_iv pnc r_iv.
465 * @param l_iv the left interval
466 * @param r_iv the right interval
467 * @param pnc the compare relation
470 * tarval_b_true or tarval_b_false it it can be evaluated,
473 static tarval *(compare_iv)(const interval_t *l_iv, const interval_t *r_iv, pn_Cmp pnc) {
476 tarval *tv_true = tarval_b_true, *tv_false = tarval_b_false;
478 /* if one interval contains NaNs, we cannot evaluate anything */
479 if (! l_iv || ! r_iv)
482 /* we can only check ordered relations */
483 if (pnc & pn_Cmp_Uo) {
486 pnc = get_negated_pnc(pnc, get_tarval_mode(l_iv->min));
492 /* if we have > or >=, we do the inverse to save some cases */
493 if (pnc == pn_Cmp_Ge || pnc == pn_Cmp_Gt) {
496 pnc = get_inversed_pnc(pnc);
502 /* now, only the following cases remains */
505 /* two intervals can be compared for equality only if they are a single value */
506 if (l_iv->min == l_iv->max && r_iv->min == r_iv->max)
507 return tarval_cmp(l_iv->min, r_iv->min) == pn_Cmp_Eq ? tv_true : tv_false;
509 /* if both intervals do not intersect, it is never equal */
510 res = tarval_cmp(l_iv->max, r_iv->min);
512 /* b < c ==> [a,b] != [c,d] */
513 if (res == pn_Cmp_Lt)
516 /* b <= c ==> [a,b) != [c,d] AND [a,b] != (c,d] */
517 if ((l_iv->flags & MAX_EXCLUDED || r_iv->flags & MIN_EXCLUDED)
518 && (res == pn_Cmp_Eq))
521 res = tarval_cmp(r_iv->max, l_iv->min);
523 /* d < a ==> [c,d] != [a,b] */
524 if (res == pn_Cmp_Lt)
527 /* d <= a ==> [c,d) != [a,b] AND [c,d] != (a,b] */
528 if ((r_iv->flags & MAX_EXCLUDED || l_iv->flags & MIN_EXCLUDED)
529 && (res == pn_Cmp_Eq))
534 /* two intervals can be compared for not equality only if they are a single value */
535 if (l_iv->min == l_iv->max && r_iv->min == r_iv->max)
536 return tarval_cmp(l_iv->min, r_iv->min) != pn_Cmp_Eq ? tv_true : tv_false;
540 res = tarval_cmp(l_iv->max, r_iv->min);
542 /* [a, b] < [c, d] <==> b < c */
543 if (res == pn_Cmp_Lt)
546 /* if one border is excluded, b <= c is enough */
547 if ((l_iv->flags & MAX_EXCLUDED || r_iv->flags & MIN_EXCLUDED) &&
551 /* [a, b] >= [c, d] <==> a > d */
552 res = tarval_cmp(l_iv->min, r_iv->max);
553 if (res == pn_Cmp_Gt)
556 /* if one border is excluded, a >= d is enough */
557 if ((l_iv->flags & MIN_EXCLUDED || r_iv->flags & MAX_EXCLUDED) &&
563 /* [a, b) <= [c, d] or [a, b] <= (c, d] <==> b <= c */
564 flags = (l_iv->flags & MAX_EXCLUDED) | (r_iv->flags & MIN_EXCLUDED);
566 res = tarval_cmp(l_iv->max, r_iv->min);
568 if (res == pn_Cmp_Lt || res == pn_Cmp_Eq)
572 res = tarval_cmp(l_iv->min, r_iv->max);
574 /* [a, b] > [c, d] <==> a > d */
575 if (res == pn_Cmp_Gt)
578 /* if one border is excluded, a >= d is enough */
579 if ((l_iv->flags & MIN_EXCLUDED || r_iv->flags & MAX_EXCLUDED) &&
585 /* Hmm. if both are intervals, we can find an order */
595 * Returns non-zero, if a given relation is transitive.
597 static int is_transitive(pn_Cmp pnc) {
598 return (pn_Cmp_False < pnc && pnc < pn_Cmp_Lg);
599 } /* is_transitive */
602 * Return the value of a Cmp if one or both predecessors
605 * @param cmp the Cmp node
606 * @param left the left operand of the Cmp
607 * @param right the right operand of the Cmp
608 * @param pnc the compare relation
610 tarval *computed_value_Cmp_Confirm(ir_node *cmp, ir_node *left, ir_node *right, pn_Cmp pnc) {
612 pn_Cmp l_pnc, res_pnc, neg_pnc;
613 interval_t l_iv, r_iv;
617 if (is_Confirm(right)) {
618 /* we want the Confirm on the left side */
623 pnc = get_inversed_pnc(pnc);
624 } else if (! is_Confirm(left)) {
625 /* no Confirm on either one side, finish */
629 /* ok, here at least left is a Confirm, right might be */
630 l_bound = get_Confirm_bound(left);
631 l_pnc = get_Confirm_cmp(left);
633 if (is_Confirm(right)) {
635 * both sides are Confirm's. Check some rare cases first.
637 ir_node *r_bound = get_Confirm_bound(right);
638 pn_Cmp r_pnc = get_Confirm_cmp(right);
641 * some check can be made WITHOUT constant bounds
643 if (r_bound == l_bound) {
644 if (is_transitive(l_pnc)) {
645 pn_Cmp r_inc_pnc = get_inversed_pnc(r_pnc);
648 * triangle inequality:
650 * a CMP B && B CMP b => a CMP b, !(a ~CMP b)
652 * We handle correctly cases with some <=/>= here
654 if ((l_pnc & ~pn_Cmp_Eq) == (r_inc_pnc & ~pn_Cmp_Eq)) {
655 res_pnc = (l_pnc & ~pn_Cmp_Eq) | (l_pnc & r_inc_pnc & pn_Cmp_Eq);
657 if ((pnc == res_pnc) || ((pnc & ~pn_Cmp_Eq) == res_pnc)) {
658 DBG_OUT_TR(l_pnc, l_bound, r_pnc, r_bound, pnc, "true");
659 DBG_EVAL_CONFIRM(cmp);
660 return tarval_b_true;
662 pn_Cmp neg_pnc = get_negated_pnc(pnc, get_irn_mode(left));
664 if ((neg_pnc == res_pnc) || ((neg_pnc & ~pn_Cmp_Eq) == res_pnc)) {
665 DBG_OUT_TR(l_pnc, l_bound, r_pnc, r_bound, pnc, "false");
666 DBG_EVAL_CONFIRM(cmp);
667 return tarval_b_false;
675 * Here, we check only the right Confirm, as the left Confirms are
676 * checked later anyway.
678 if (left == r_bound) {
680 * l == bound(r) AND pnc(r) == pnc:
682 * We know that a CMP b and check for that
684 if ((r_pnc == pnc) || (r_pnc == (pnc & ~pn_Cmp_Eq))) {
685 DBG_OUT_R(r_pnc, r_bound, left, pnc, right, "true");
686 DBG_EVAL_CONFIRM(cmp);
687 return tarval_b_true;
690 * l == bound(r) AND pnc(r) != pnc:
692 * We know that a CMP b and check for a ~CMP b
695 mode = get_irn_mode(left);
696 neg_pnc = get_negated_pnc(pnc, mode);
698 if ((r_pnc == neg_pnc) || (r_pnc == (neg_pnc & ~pn_Cmp_Eq))) {
699 DBG_OUT_R(r_pnc, r_bound, left, pnc, right, "false");
700 DBG_EVAL_CONFIRM(cmp);
701 return tarval_b_false;
706 /* now, try interval magic */
708 get_interval(&l_iv, l_bound, l_pnc),
709 get_interval(&r_iv, r_bound, r_pnc),
712 if (tv != tarval_bad) {
713 DBG_EVAL_CONFIRM(cmp);
718 /* from Here, check only left Confirm */
721 * some checks can be made WITHOUT constant bounds
723 if (right == l_bound) {
725 * r == bound(l) AND pnc(l) == pnc:
727 * We know that a CMP b and check for that
729 if ((l_pnc == pnc) || (l_pnc == (pnc & ~pn_Cmp_Eq))) {
730 DBG_OUT_L(l_pnc, l_bound, left, pnc, right, "true");
731 DBG_EVAL_CONFIRM(cmp);
732 return tarval_b_true;
735 * r == bound(l) AND pnc(l) is Not(pnc):
737 * We know that a CMP b and check for a ~CMP b
740 mode = get_irn_mode(left);
741 neg_pnc = get_negated_pnc(pnc, mode);
743 if ((l_pnc == neg_pnc) || (l_pnc == (neg_pnc & ~pn_Cmp_Eq))) {
744 DBG_OUT_L(l_pnc, l_bound, left, pnc, right, "false");
745 DBG_EVAL_CONFIRM(cmp);
746 return tarval_b_false;
751 /* now, only right == Const can help */
752 tv = value_of(right);
754 if (tv != tarval_bad) {
756 get_interval(&l_iv, l_bound, l_pnc),
757 get_interval_from_tv(&r_iv, tv),
761 if (tv != tarval_bad)
762 DBG_EVAL_CONFIRM(cmp);
765 } /* computed_value_Cmp_Confirm */
769 * For debugging. Prints an interval into a string.
771 * @param buf address of a string buffer
772 * @param len length of the string buffer
773 * @param iv the interval
775 static int iv_snprintf(char *buf, size_t len, const interval_t *iv) {
776 char smin[64], smax[64];
779 tarval_snprintf(smin, sizeof(smin), iv->min);
781 if (iv->min != iv->max || (iv->flags & (MIN_EXCLUDED|MAX_EXCLUDED))) {
782 tarval_snprintf(smax, sizeof(smax), iv->max);
784 return snprintf(buf, len, "%c%s, %s%c",
785 iv->flags & MIN_EXCLUDED ? '(' : '[',
787 iv->flags & MAX_EXCLUDED ? ')' : ']'
790 return snprintf(buf, len, "%s", smin);
792 return snprintf(buf, len, "<UNKNOWN>");
796 * For debugging. Prints an interval compare.
798 * @param l_iv the left interval
799 * @param r_iv the right interval
800 * @param pnc the compare relation
802 static void print_iv_cmp(const interval_t *l_iv, const interval_t *r_iv, pn_Cmp pnc) {
803 char sl[128], sr[128];
805 iv_snprintf(sl, sizeof(sl), l_iv);
806 iv_snprintf(sr, sizeof(sr), r_iv);
808 ir_printf("%s %= %s", sl, pnc, sr);
812 * For debugging. call *compare_iv() and prints inputs and result.
814 * @param l_iv the left interval
815 * @param r_iv the right interval
816 * @param pnc the compare relation
818 static tarval *compare_iv_dbg(const interval_t *l_iv, const interval_t *r_iv, pn_Cmp pnc) {
819 tarval *tv = (compare_iv)(l_iv, r_iv, pnc);
821 if (tv == tarval_bad)
824 ir_printf("In %e:\n", get_irg_entity(current_ir_graph));
825 print_iv_cmp(l_iv, r_iv, pnc);
826 ir_printf(" = %T\n", tv);
828 } /* compare_iv_dbg */
830 #endif /* DEBUG_CONFIRM */