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 *blk, 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);
107 if (get_irg_pinned(get_irn_irg(blk)) != op_pin_state_pinned)
112 /* there might be several Confirms one after other that form an interval */
113 for (; is_Confirm(n); n = get_Confirm_value(n)) {
114 unsigned long region = get_Confirm_region(n);
116 /* check if it's legal to use this confirm. */
118 ir_node *curr_blk = get_irn_n(n, -1);
120 /* this confirm is bound to a region. */
124 if (get_Block_exc_region(curr_blk) != region)
126 /* all went fine, the current Confirm belongs to the same region */
130 * Note: A Confirm is never after a Const. So,
131 * we simply can check the bound for being a Const
132 * without the fear that is might be hidden by a further Confirm.
134 tv = value_of(get_Confirm_bound(n));
135 if (tv == tarval_bad)
138 pnc = tarval_cmp(tv, get_mode_null(mode));
141 * Beware: C might by a NaN. It is not clear, what we should do
142 * than. Of course a NaN is != 0, but we might use this function
143 * to remove up Exceptions, and NaN's might generate Exception.
144 * So, we do NOT handle NaNs here for safety.
146 * Note that only the C != 0 case need additional checking.
148 switch (get_Confirm_cmp(n)) {
149 case pn_Cmp_Eq: /* n == C /\ C != 0 ==> n != 0 */
150 RET_ON(pnc != pn_Cmp_Eq && pnc != pn_Cmp_Uo);
151 case pn_Cmp_Lg: /* n != C /\ C == 0 ==> n != 0 */
152 RET_ON(pnc == pn_Cmp_Eq);
153 case pn_Cmp_Lt: /* n < C /\ C <= 0 ==> n != 0 */
154 RET_ON(pnc == pn_Cmp_Lt || pnc == pn_Cmp_Eq);
155 case pn_Cmp_Le: /* n <= C /\ C < 0 ==> n != 0 */
156 RET_ON(pnc == pn_Cmp_Lt);
157 case pn_Cmp_Ge: /* n >= C /\ C > 0 ==> n != 0 */
158 RET_ON(pnc == pn_Cmp_Gt);
159 case pn_Cmp_Gt: /* n > C /\ C >= 0 ==> n != 0 */
160 RET_ON(pnc == pn_Cmp_Gt || pnc == pn_Cmp_Eq);
167 if (tv == tarval_bad)
170 pnc = tarval_cmp(tv, get_mode_null(mode));
172 /* again, need check for NaN */
173 return (pnc != pn_Cmp_Eq) && (pnc != pn_Cmp_Uo);
176 } /* value_not_zero */
179 * Check, if the value of a node cannot represent a NULL pointer.
181 * - Casts are skipped
182 * - If sel_based_null_check_elim is enabled, all
183 * Sel nodes can be skipped.
184 * - A SymConst(entity) is NEVER a NULL pointer
185 * - Confirms are evaluated
187 int value_not_null(ir_node *blk, ir_node *n, ir_node **confirm) {
190 if (get_irg_pinned(get_irn_irg(blk)) != op_pin_state_pinned)
196 assert(mode_is_reference(get_irn_mode(n)));
197 if (get_opt_sel_based_null_check_elim()) {
198 /* skip all Sel nodes and Cast's */
199 while (op == op_Sel) {
200 n = skip_Cast(get_Sel_ptr(n));
204 if (op == op_SymConst && get_SymConst_kind(n) == symconst_addr_ent)
206 if (op == op_Const) {
207 tarval *tv = get_Const_tarval(n);
209 if (tv != tarval_bad && classify_tarval(tv) != TV_CLASSIFY_NULL)
212 for (; is_Confirm(n); n = skip_Cast(get_Confirm_value(n))) {
213 unsigned long region = get_Confirm_region(n);
215 /* check if it's legal to use this confirm. */
217 ir_node *curr_blk = get_irn_n(n, -1);
219 /* this confirm is bound to a region. */
223 if (get_Block_exc_region(curr_blk) != region)
225 /* all went fine, the current Confirm belongs to the same region */
228 if (get_Confirm_cmp(n) == pn_Cmp_Lg &&
229 classify_Const(get_Confirm_bound(n)) == CNST_NULL) {
236 } /* value_not_null */
239 * Check, if the value of a node can be confirmed >= 0 or <= 0,
240 * If the mode of the value did not honor signed zeros, else
241 * check for >= 0 or < 0.
243 value_classify_sign classify_value_sign(ir_node *n) {
248 if (get_irn_op(n) != op_Confirm)
249 return value_classified_unknown;
251 tv = value_of(get_Confirm_bound(n));
252 if (tv == tarval_bad)
253 return value_classified_unknown;
255 mode = get_irn_mode(n);
258 * We can handle only >=, >, <, <= cases.
259 * We could handle == too, but this will be optimized into
262 * Note that for integer modes we have a slightly better
263 * optimization possibilities, so we handle this
266 cmp = get_Confirm_cmp(n);
271 * must be x < c <= 1 to be useful if integer mode and -0 = 0
272 * x < c <= 0 to be useful else
276 * must be x <= c < 1 to be useful if integer mode and -0 = 0
277 * x <= c < 0 to be useful else
279 c = mode_is_int(mode) && mode_honor_signed_zeros(mode) ?
280 get_mode_one(mode) : get_mode_null(mode);
282 ncmp = tarval_cmp(tv, c);
283 if (ncmp == pn_Cmp_Eq)
286 if (cmp != (ncmp ^ pn_Cmp_Eq))
287 return value_classified_unknown;
290 return value_classified_negative;
294 * must be x >= c > -1 to be useful if integer mode
295 * x >= c >= 0 to be useful else
299 * must be x > c >= -1 to be useful if integer mode
300 * x > c >= 0 to be useful else
302 if (mode_is_int(mode)) {
303 c = get_mode_minus_one(mode);
305 ncmp = tarval_cmp(tv, c);
306 if (ncmp == pn_Cmp_Eq)
309 if (cmp != (ncmp ^ pn_Cmp_Eq))
310 return value_classified_unknown;
312 c = get_mode_minus_one(mode);
314 ncmp = tarval_cmp(tv, c);
316 if (ncmp != pn_Cmp_Eq && ncmp != pn_Cmp_Gt)
317 return value_classified_unknown;
321 return value_classified_positive;
324 return value_classified_unknown;
326 } /* classify_value_sign */
329 * construct an interval from a value
331 * @return the filled interval or NULL if no interval
332 * can be created (happens only on floating point
334 static interval_t *get_interval_from_tv(interval_t *iv, tarval *tv) {
335 ir_mode *mode = get_tarval_mode(tv);
337 if (tv == tarval_bad) {
338 if (mode_is_float(mode)) {
339 /* NaN could be included which we cannot handle */
340 iv->min = tarval_bad;
341 iv->max = tarval_bad;
342 iv->flags = MIN_EXCLUDED | MAX_EXCLUDED;
346 iv->min = get_mode_min(mode);
347 iv->max = get_mode_max(mode);
348 iv->flags = MIN_INCLUDED | MAX_INCLUDED;
353 if (mode_is_float(mode)) {
354 if (tv == get_mode_NAN(mode)) {
355 /* arg, we cannot handle NaN's. */
356 iv->min = tarval_bad;
357 iv->max = tarval_bad;
358 iv->flags = MIN_EXCLUDED | MAX_EXCLUDED;
366 iv->flags = MIN_INCLUDED | MAX_INCLUDED;
369 } /* get_interval_from_tv */
372 * construct an interval from a Confirm
374 * @param iv an empty interval, will be filled
375 * @param bound the bound value
376 * @param pnc the Confirm compare relation
378 * @return the filled interval or NULL if no interval
379 * can be created (happens only on floating point
381 static interval_t *get_interval(interval_t *iv, ir_node *bound, pn_Cmp pnc) {
382 ir_mode *mode = get_irn_mode(bound);
383 tarval *tv = value_of(bound);
385 if (tv == tarval_bad) {
386 /* There is nothing we could do here. For integer
387 * modes we could return [-oo, +oo], but there is
388 * nothing we could deduct from such an interval.
389 * So, speed things up and return unknown.
391 iv->min = tarval_bad;
392 iv->max = tarval_bad;
393 iv->flags = MIN_EXCLUDED | MAX_EXCLUDED;
397 if (mode_is_float(mode)) {
398 if (tv == get_mode_NAN(mode)) {
399 /* arg, we cannot handle NaN's. */
400 iv->min = tarval_bad;
401 iv->max = tarval_bad;
402 iv->flags = MIN_EXCLUDED | MAX_EXCLUDED;
408 /* check which side is known */
414 iv->flags = MIN_INCLUDED | MAX_INCLUDED;
419 iv->min = get_mode_min(mode);
421 iv->flags = MIN_INCLUDED | MAX_INCLUDED;
426 iv->min = get_mode_min(mode);
428 iv->flags = MIN_INCLUDED | MAX_EXCLUDED;
434 iv->max = get_mode_max(mode);
435 iv->flags = MIN_EXCLUDED | MAX_INCLUDED;
441 iv->max = get_mode_max(mode);
442 iv->flags = MIN_INCLUDED | MAX_INCLUDED;
447 * Ordered means, that at least neither
448 * our bound nor our value ara NaN's
451 iv->min = get_mode_min(mode);
452 iv->max = get_mode_max(mode);
453 iv->flags = MIN_INCLUDED | MAX_INCLUDED;
458 * We do not handle UNORDERED, as a NaN
459 * could be included in the interval.
461 iv->min = tarval_bad;
462 iv->max = tarval_bad;
463 iv->flags = MIN_EXCLUDED | MAX_EXCLUDED;
467 if (iv->min != tarval_bad && iv->max != tarval_bad)
473 * Try to evaluate l_iv pnc r_iv.
475 * @param l_iv the left interval
476 * @param r_iv the right interval
477 * @param pnc the compare relation
480 * tarval_b_true or tarval_b_false it it can be evaluated,
483 static tarval *(compare_iv)(const interval_t *l_iv, const interval_t *r_iv, pn_Cmp pnc) {
486 tarval *tv_true = tarval_b_true, *tv_false = tarval_b_false;
488 /* if one interval contains NaNs, we cannot evaluate anything */
489 if (! l_iv || ! r_iv)
492 /* we can only check ordered relations */
493 if (pnc & pn_Cmp_Uo) {
496 pnc = get_negated_pnc(pnc, get_tarval_mode(l_iv->min));
502 /* if we have > or >=, we do the inverse to save some cases */
503 if (pnc == pn_Cmp_Ge || pnc == pn_Cmp_Gt) {
506 pnc = get_inversed_pnc(pnc);
512 /* now, only the following cases remains */
515 /* two intervals can be compared for equality only if they are a single value */
516 if (l_iv->min == l_iv->max && r_iv->min == r_iv->max)
517 return tarval_cmp(l_iv->min, r_iv->min) == pn_Cmp_Eq ? tv_true : tv_false;
519 /* if both intervals do not intersect, it is never equal */
520 res = tarval_cmp(l_iv->max, r_iv->min);
522 /* b < c ==> [a,b] != [c,d] */
523 if (res == pn_Cmp_Lt)
526 /* b <= c ==> [a,b) != [c,d] AND [a,b] != (c,d] */
527 if ((l_iv->flags & MAX_EXCLUDED || r_iv->flags & MIN_EXCLUDED)
528 && (res == pn_Cmp_Eq))
531 res = tarval_cmp(r_iv->max, l_iv->min);
533 /* d < a ==> [c,d] != [a,b] */
534 if (res == pn_Cmp_Lt)
537 /* d <= a ==> [c,d) != [a,b] AND [c,d] != (a,b] */
538 if ((r_iv->flags & MAX_EXCLUDED || l_iv->flags & MIN_EXCLUDED)
539 && (res == pn_Cmp_Eq))
544 /* two intervals can be compared for not equality only if they are a single value */
545 if (l_iv->min == l_iv->max && r_iv->min == r_iv->max)
546 return tarval_cmp(l_iv->min, r_iv->min) != pn_Cmp_Eq ? tv_true : tv_false;
550 res = tarval_cmp(l_iv->max, r_iv->min);
552 /* [a, b] < [c, d] <==> b < c */
553 if (res == pn_Cmp_Lt)
556 /* if one border is excluded, b <= c is enough */
557 if ((l_iv->flags & MAX_EXCLUDED || r_iv->flags & MIN_EXCLUDED) &&
561 /* [a, b] >= [c, d] <==> a > d */
562 res = tarval_cmp(l_iv->min, r_iv->max);
563 if (res == pn_Cmp_Gt)
566 /* if one border is excluded, a >= d is enough */
567 if ((l_iv->flags & MIN_EXCLUDED || r_iv->flags & MAX_EXCLUDED) &&
573 /* [a, b) <= [c, d] or [a, b] <= (c, d] <==> b <= c */
574 flags = (l_iv->flags & MAX_EXCLUDED) | (r_iv->flags & MIN_EXCLUDED);
576 res = tarval_cmp(l_iv->max, r_iv->min);
578 if (res == pn_Cmp_Lt || res == pn_Cmp_Eq)
582 res = tarval_cmp(l_iv->min, r_iv->max);
584 /* [a, b] > [c, d] <==> a > d */
585 if (res == pn_Cmp_Gt)
588 /* if one border is excluded, a >= d is enough */
589 if ((l_iv->flags & MIN_EXCLUDED || r_iv->flags & MAX_EXCLUDED) &&
595 /* Hmm. if both are intervals, we can find an order */
605 * Returns non-zero, if a given relation is transitive.
607 static int is_transitive(pn_Cmp pnc) {
608 return (pn_Cmp_False < pnc && pnc < pn_Cmp_Lg);
609 } /* is_transitive */
612 * returns a confirm node starting from n that is allowed to be used in the exception
615 * @param region the exception region
618 static ir_node *get_allowed_confirm(unsigned long region, ir_node *n) {
619 for (; is_Confirm(n); n = get_Confirm_value(n)) {
620 unsigned long reg = get_Confirm_region(n);
622 if (reg == 0 || reg == region) {
623 /* found an allowed Confirm */
629 } /* get_allowed_confirm */
632 * Return the value of a Cmp if one or both predecessors
635 * @param cmp the Cmp node
636 * @param left the left operand of the Cmp
637 * @param right the right operand of the Cmp
638 * @param pnc the compare relation
640 tarval *computed_value_Cmp_Confirm(ir_node *cmp, ir_node *left, ir_node *right, pn_Cmp pnc) {
641 ir_node *l_bound, *confirm;
642 pn_Cmp l_pnc, res_pnc, neg_pnc;
643 interval_t l_iv, r_iv;
646 ir_exc_region_t region;
648 /* Cmp is pinned, so this always gives the right exception region. */
649 region = get_Block_exc_region(get_nodes_block(cmp));
651 if ((confirm = get_allowed_confirm(region, right)) != NULL) {
652 /* we want the Confirm on the left side */
656 pnc = get_inversed_pnc(pnc);
657 } else if ((confirm = get_allowed_confirm(region, left)) != NULL) {
660 /* no Confirm on either one side, finish */
664 /* ok, here at least left is a Confirm, right might be */
665 l_bound = get_Confirm_bound(left);
666 l_pnc = get_Confirm_cmp(left);
668 if (is_Confirm(right)) {
670 * both sides are Confirm's. Check some rare cases first.
672 ir_node *r_bound = get_Confirm_bound(right);
673 pn_Cmp r_pnc = get_Confirm_cmp(right);
676 * some check can be made WITHOUT constant bounds
678 if (r_bound == l_bound) {
679 if (is_transitive(l_pnc)) {
680 pn_Cmp r_inc_pnc = get_inversed_pnc(r_pnc);
683 * triangle inequality:
685 * a CMP B && B CMP b => a CMP b, !(a ~CMP b)
687 * We handle correctly cases with some <=/>= here
689 if ((l_pnc & ~pn_Cmp_Eq) == (r_inc_pnc & ~pn_Cmp_Eq)) {
690 res_pnc = (l_pnc & ~pn_Cmp_Eq) | (l_pnc & r_inc_pnc & pn_Cmp_Eq);
692 if ((pnc == res_pnc) || ((pnc & ~pn_Cmp_Eq) == res_pnc)) {
693 DBG_OUT_TR(l_pnc, l_bound, r_pnc, r_bound, pnc, "true");
694 DBG_EVAL_CONFIRM(cmp);
695 return tarval_b_true;
697 pn_Cmp neg_pnc = get_negated_pnc(pnc, get_irn_mode(left));
699 if ((neg_pnc == res_pnc) || ((neg_pnc & ~pn_Cmp_Eq) == res_pnc)) {
700 DBG_OUT_TR(l_pnc, l_bound, r_pnc, r_bound, pnc, "false");
701 DBG_EVAL_CONFIRM(cmp);
702 return tarval_b_false;
710 * Here, we check only the right Confirm, as the left Confirms are
711 * checked later anyway.
713 if (left == r_bound) {
715 * l == bound(r) AND pnc(r) == pnc:
717 * We know that a CMP b and check for that
719 if ((r_pnc == pnc) || (r_pnc == (pnc & ~pn_Cmp_Eq))) {
720 DBG_OUT_R(r_pnc, r_bound, left, pnc, right, "true");
721 DBG_EVAL_CONFIRM(cmp);
722 return tarval_b_true;
725 * l == bound(r) AND pnc(r) != pnc:
727 * We know that a CMP b and check for a ~CMP b
730 mode = get_irn_mode(left);
731 neg_pnc = get_negated_pnc(pnc, mode);
733 if ((r_pnc == neg_pnc) || (r_pnc == (neg_pnc & ~pn_Cmp_Eq))) {
734 DBG_OUT_R(r_pnc, r_bound, left, pnc, right, "false");
735 DBG_EVAL_CONFIRM(cmp);
736 return tarval_b_false;
741 /* now, try interval magic */
743 get_interval(&l_iv, l_bound, l_pnc),
744 get_interval(&r_iv, r_bound, r_pnc),
747 if (tv != tarval_bad) {
748 DBG_EVAL_CONFIRM(cmp);
753 /* from Here, check only left Confirm */
756 * some checks can be made WITHOUT constant bounds
758 if (right == l_bound) {
760 * r == bound(l) AND pnc(l) == pnc:
762 * We know that a CMP b and check for that
764 if ((l_pnc == pnc) || (l_pnc == (pnc & ~pn_Cmp_Eq))) {
765 DBG_OUT_L(l_pnc, l_bound, left, pnc, right, "true");
766 DBG_EVAL_CONFIRM(cmp);
767 return tarval_b_true;
770 * r == bound(l) AND pnc(l) is Not(pnc):
772 * We know that a CMP b and check for a ~CMP b
775 mode = get_irn_mode(left);
776 neg_pnc = get_negated_pnc(pnc, mode);
778 if ((l_pnc == neg_pnc) || (l_pnc == (neg_pnc & ~pn_Cmp_Eq))) {
779 DBG_OUT_L(l_pnc, l_bound, left, pnc, right, "false");
780 DBG_EVAL_CONFIRM(cmp);
781 return tarval_b_false;
786 /* now, only right == Const can help */
787 tv = value_of(right);
789 if (tv != tarval_bad) {
791 get_interval(&l_iv, l_bound, l_pnc),
792 get_interval_from_tv(&r_iv, tv),
796 if (tv != tarval_bad)
797 DBG_EVAL_CONFIRM(cmp);
800 } /* computed_value_Cmp_Confirm */
804 * For debugging. Prints an interval into a string.
806 * @param buf address of a string buffer
807 * @param len length of the string buffer
808 * @param iv the interval
810 static int iv_snprintf(char *buf, size_t len, const interval_t *iv) {
811 char smin[64], smax[64];
814 tarval_snprintf(smin, sizeof(smin), iv->min);
816 if (iv->min != iv->max || (iv->flags & (MIN_EXCLUDED|MAX_EXCLUDED))) {
817 tarval_snprintf(smax, sizeof(smax), iv->max);
819 return snprintf(buf, len, "%c%s, %s%c",
820 iv->flags & MIN_EXCLUDED ? '(' : '[',
822 iv->flags & MAX_EXCLUDED ? ')' : ']'
825 return snprintf(buf, len, "%s", smin);
827 return snprintf(buf, len, "<UNKNOWN>");
831 * For debugging. Prints an interval compare.
833 * @param l_iv the left interval
834 * @param r_iv the right interval
835 * @param pnc the compare relation
837 static void print_iv_cmp(const interval_t *l_iv, const interval_t *r_iv, pn_Cmp pnc) {
838 char sl[128], sr[128];
840 iv_snprintf(sl, sizeof(sl), l_iv);
841 iv_snprintf(sr, sizeof(sr), r_iv);
843 ir_printf("%s %= %s", sl, pnc, sr);
847 * For debugging. call *compare_iv() and prints inputs and result.
849 * @param l_iv the left interval
850 * @param r_iv the right interval
851 * @param pnc the compare relation
853 static tarval *compare_iv_dbg(const interval_t *l_iv, const interval_t *r_iv, pn_Cmp pnc) {
854 tarval *tv = (compare_iv)(l_iv, r_iv, pnc);
856 if (tv == tarval_bad)
859 ir_printf("In %e:\n", get_irg_entity(current_ir_graph));
860 print_iv_cmp(l_iv, r_iv, pnc);
861 ir_printf(" = %T\n", tv);
863 } /* compare_iv_dbg */
865 #endif /* DEBUG_CONFIRM */