3 * File name: ir/opt/opt_confirms.c
4 * Purpose: Optimizations regarding Confirm nodes
9 * Copyright: (c) 1998-2005 Universität Karlsruhe
10 * Licence: This file protected by GPL - GNU GENERAL PUBLIC LICENSE.
21 #include "iropt_dbg.h"
22 #include "opt_confirms.h"
27 MIN_INCLUDED = 0x00, /**< [min, ... */
28 MAX_INCLUDED = 0x00, /**< ..., max] */
29 MIN_EXCLUDED = 0x01, /**< (min, ... */
30 MAX_EXCLUDED = 0x02 /**< ..., max) */
34 * An interval. We could use
35 * intervals that ALWAYS include its borders, even for
36 * floating point, as the precision is limited.
37 * However, as our tarval module did not support
38 * such kind of operation, we use border flags allowing
41 typedef struct _interval_t {
42 tarval *min; /**< lowest border */
43 tarval *max; /**< highest border */
44 unsigned char flags; /**< border flags */
49 #define compare_iv(l_iv, r_iv, pnc) compare_iv_dbg(l_iv, r_iv, pnc)
52 static tarval *compare_iv_dbg(const interval_t *l_iv, const interval_t *r_iv, pn_Cmp pnc);
55 #define DBG_OUT_TR(l_pnc, l_bound, r_pnc, r_bound, pnc, v) \
56 ir_printf("In %e:\na %= %n && b %= %n ==> a %= b == %s\n", \
57 get_irg_entity(current_ir_graph), \
58 l_pnc, l_bound, r_pnc, r_bound, pnc, v);
61 #define DBG_OUT_R(r_pnc, r_bound, left, pnc, right, v) \
62 ir_printf("In %e:\na %= %n ==> %n %= %n == %s\n", \
63 get_irg_entity(current_ir_graph), \
64 r_pnc, r_bound, left, pnc, right, v);
67 #define DBG_OUT_L(l_pnc, l_bound, left, pnc, right, v) \
68 ir_printf("In %e:\na %= %n ==> %n %= %n == %s\n", \
69 get_irg_entity(current_ir_graph), \
70 l_pnc, l_bound, left, pnc, right, v);
74 #define DBG_OUT_TR(l_pnc, l_bound, r_pnc, r_bound, pnc, v)
75 #define DBG_OUT_R(r_pnc, r_bound, left, pnc, right, v)
76 #define DBG_OUT_L(l_pnc, l_bound, left, pnc, right, v)
78 #endif /* DEBUG_CONFIRM */
81 * Check, if the value of a node is != 0.
83 * This is a often needed case, so we handle here Confirm
86 int value_not_zero(ir_node *n, ir_node **confirm)
88 #define RET_ON(x) if (x) { *confirm = n; return 1; }; break
91 ir_mode *mode = get_irn_mode(n);
95 while (get_irn_op(n) == op_Confirm) {
97 * Note: A Confirm is never after a Const. So,
98 * we simply can check the bound for being a Const
99 * without the fear that is might be hidden by a further Confirm.
101 tv = value_of(get_Confirm_bound(n));
102 if (tv == tarval_bad)
105 pnc = tarval_cmp(tv, get_mode_null(mode));
108 * Beware: C might by a NaN. It is not clear, what we should do
109 * than. Of course a NaN is != 0, but we might use this function
110 * to remove up Exceptions, and NaN's might generate Exception.
111 * So, we do NOT handle NaNs here for safety.
113 * Note that only the C != 0 case need additional checking.
115 switch (get_Confirm_cmp(n)) {
116 case pn_Cmp_Eq: /* n == C /\ C != 0 ==> n != 0 */
117 RET_ON(pnc != pn_Cmp_Eq && pnc != pn_Cmp_Uo);
118 case pn_Cmp_Lg: /* n != C /\ C == 0 ==> n != 0 */
119 RET_ON(pnc == pn_Cmp_Eq);
120 case pn_Cmp_Lt: /* n < C /\ C <= 0 ==> n != 0 */
121 RET_ON(pnc == pn_Cmp_Lt || pnc == pn_Cmp_Eq);
122 case pn_Cmp_Le: /* n <= C /\ C < 0 ==> n != 0 */
123 RET_ON(pnc == pn_Cmp_Lt);
124 case pn_Cmp_Ge: /* n >= C /\ C > 0 ==> n != 0 */
125 RET_ON(pnc == pn_Cmp_Gt);
126 case pn_Cmp_Gt: /* n > C /\ C >= 0 ==> n != 0 */
127 RET_ON(pnc == pn_Cmp_Gt || pnc == pn_Cmp_Eq);
132 /* there might be several Confirms one after other that form an interval */
133 n = get_Confirm_value(n);
137 if (tv == tarval_bad)
140 pnc = tarval_cmp(tv, get_mode_null(mode));
142 /* again, need check for NaN */
143 return (pnc != pn_Cmp_Eq) && (pnc != pn_Cmp_Uo);
146 } /* value_not_zero */
149 * Check, if the value of a node cannot represent a NULL pointer.
151 * - Casts are skipped
152 * - If sel_based_null_check_elim is enabled, all
153 * Sel nodes can be skipped.
154 * - A SymConst(entity) is NEVER a NULL pointer
155 * - Confirms are evaluated
157 int value_not_null(ir_node *n, ir_node **confirm)
164 assert(mode_is_reference(get_irn_mode(n)));
165 if (get_opt_sel_based_null_check_elim()) {
166 /* skip all Sel nodes and Cast's */
167 while (op == op_Sel) {
168 n = skip_Cast(get_Sel_ptr(n));
172 if (op == op_SymConst && get_SymConst_kind(n) == symconst_addr_ent)
174 if (op == op_Const) {
175 tarval *tv = get_Const_tarval(n);
177 if (tv != tarval_bad && classify_tarval(tv) != TV_CLASSIFY_NULL)
180 else if (op == op_Confirm) {
181 if (get_Confirm_cmp(n) == pn_Cmp_Lg &&
182 classify_Const(get_Confirm_bound(n)) == CNST_NULL) {
188 } /* value_not_null */
191 * Check, if the value of a node can be confirmed >= 0 or <= 0,
192 * If the mode of the value did not honor signed zeros, else
193 * check for >= 0 or < 0.
195 value_classify_sign classify_value_sign(ir_node *n)
201 if (get_irn_op(n) != op_Confirm)
202 return value_classified_unknown;
204 tv = value_of(get_Confirm_bound(n));
205 if (tv == tarval_bad)
206 return value_classified_unknown;
208 mode = get_irn_mode(n);
211 * We can handle only >=, >, <, <= cases.
212 * We could handle == too, but this will be optimized into
215 * Note that for integer modes we have a slightly better
216 * optimization possibilities, so we handle this
219 cmp = get_Confirm_cmp(n);
224 * must be x < c <= 1 to be useful if integer mode and -0 = 0
225 * x < c <= 0 to be useful else
229 * must be x <= c < 1 to be useful if integer mode and -0 = 0
230 * x <= c < 0 to be useful else
232 c = mode_is_int(mode) && mode_honor_signed_zeros(mode) ?
233 get_mode_one(mode) : get_mode_null(mode);
235 ncmp = tarval_cmp(tv, c);
236 if (ncmp == pn_Cmp_Eq)
239 if (cmp != (ncmp ^ pn_Cmp_Eq))
240 return value_classified_unknown;
243 return value_classified_negative;
247 * must be x >= c > -1 to be useful if integer mode
248 * x >= c >= 0 to be useful else
252 * must be x > c >= -1 to be useful if integer mode
253 * x > c >= 0 to be useful else
255 if (mode_is_int(mode)) {
256 c = get_mode_minus_one(mode);
258 ncmp = tarval_cmp(tv, c);
259 if (ncmp == pn_Cmp_Eq)
262 if (cmp != (ncmp ^ pn_Cmp_Eq))
263 return value_classified_unknown;
266 c = get_mode_minus_one(mode);
268 ncmp = tarval_cmp(tv, c);
270 if (ncmp != pn_Cmp_Eq && ncmp != pn_Cmp_Gt)
271 return value_classified_unknown;
275 return value_classified_positive;
278 return value_classified_unknown;
280 } /* classify_value_sign */
283 * construct an interval from a value
285 * @return the filled interval or NULL if no interval
286 * can be created (happens only on floating point
288 static interval_t *get_interval_from_tv(interval_t *iv, tarval *tv)
290 ir_mode *mode = get_tarval_mode(tv);
292 if (tv == tarval_bad) {
293 if (mode_is_float(mode)) {
294 /* NaN could be included which we cannot handle */
295 iv->min = tarval_bad;
296 iv->max = tarval_bad;
297 iv->flags = MIN_EXCLUDED | MAX_EXCLUDED;
302 iv->min = get_mode_min(mode);
303 iv->max = get_mode_max(mode);
304 iv->flags = MIN_INCLUDED | MAX_INCLUDED;
309 if (mode_is_float(mode)) {
310 if (tv == get_mode_NAN(mode)) {
311 /* arg, we cannot handle NaN's. */
312 iv->min = tarval_bad;
313 iv->max = tarval_bad;
314 iv->flags = MIN_EXCLUDED | MAX_EXCLUDED;
322 iv->flags = MIN_INCLUDED | MAX_INCLUDED;
325 } /* get_interval_from_tv */
328 * construct an interval from a Confirm
330 * @param iv an empty interval, will be filled
331 * @param bound the bound value
332 * @param pnc the Confirm compare relation
334 * @return the filled interval or NULL if no interval
335 * can be created (happens only on floating point
337 static interval_t *get_interval(interval_t *iv, ir_node *bound, pn_Cmp pnc)
339 ir_mode *mode = get_irn_mode(bound);
340 tarval *tv = value_of(bound);
342 if (tv == tarval_bad) {
343 /* There is nothing we could do here. For integer
344 * modes we could return [-oo, +oo], but there is
345 * nothing we could deduct from such an interval.
346 * So, speed things up and return unknown.
348 iv->min = tarval_bad;
349 iv->max = tarval_bad;
350 iv->flags = MIN_EXCLUDED | MAX_EXCLUDED;
354 if (mode_is_float(mode)) {
355 if (tv == get_mode_NAN(mode)) {
356 /* arg, we cannot handle NaN's. */
357 iv->min = tarval_bad;
358 iv->max = tarval_bad;
359 iv->flags = MIN_EXCLUDED | MAX_EXCLUDED;
365 /* check which side is known */
371 iv->flags = MIN_INCLUDED | MAX_INCLUDED;
376 iv->min = get_mode_min(mode);
378 iv->flags = MIN_INCLUDED | MAX_INCLUDED;
383 iv->min = get_mode_min(mode);
385 iv->flags = MIN_INCLUDED | MAX_EXCLUDED;
391 iv->max = get_mode_max(mode);
392 iv->flags = MIN_EXCLUDED | MAX_INCLUDED;
398 iv->max = get_mode_max(mode);
399 iv->flags = MIN_INCLUDED | MAX_INCLUDED;
404 * Ordered means, that at least neither
405 * our bound nor our value ara NaN's
408 iv->min = get_mode_min(mode);
409 iv->max = get_mode_max(mode);
410 iv->flags = MIN_INCLUDED | MAX_INCLUDED;
415 * We do not handle UNORDERED, as a NaN
416 * could be included in the interval.
418 iv->min = tarval_bad;
419 iv->max = tarval_bad;
420 iv->flags = MIN_EXCLUDED | MAX_EXCLUDED;
424 if (iv->min != tarval_bad && iv->max != tarval_bad)
430 * Try to evaluate l_iv pnc r_iv.
432 * @param l_iv the left interval
433 * @param r_iv the right interval
434 * @param pnc the compare relation
437 * tarval_b_true or tarval_b_false it it can be evaluated,
440 static tarval *(compare_iv)(const interval_t *l_iv, const interval_t *r_iv, pn_Cmp pnc)
444 tarval *tv_true = tarval_b_true, *tv_false = tarval_b_false;
446 /* if one interval contains NaNs, we cannot evaluate anything */
447 if (! l_iv || ! r_iv)
450 /* we can only check ordered relations */
451 if (pnc & pn_Cmp_Uo) {
454 pnc = get_negated_pnc(pnc, get_tarval_mode(l_iv->min));
460 /* if we have > or >=, we do the inverse to save some cases */
461 if (pnc == pn_Cmp_Ge || pnc == pn_Cmp_Gt) {
464 pnc = get_inversed_pnc(pnc);
470 /* now, only the following cases remains */
473 /* two intervals can be compared for equality only if they are a single value */
474 if (l_iv->min == l_iv->max && r_iv->min == r_iv->max)
475 return tarval_cmp(l_iv->min, r_iv->min) == pn_Cmp_Eq ? tv_true : tv_false;
477 /* if both intervals do not intersect, it is never equal */
478 res = tarval_cmp(l_iv->max, r_iv->min);
480 /* b < c ==> [a,b] != [c,d] */
481 if (res == pn_Cmp_Lt)
484 /* b <= c ==> [a,b) != [c,d] AND [a,b] != (c,d] */
485 if ((l_iv->flags & MAX_EXCLUDED || r_iv->flags & MIN_EXCLUDED)
486 && (res == pn_Cmp_Eq))
489 res = tarval_cmp(r_iv->max, l_iv->min);
491 /* d < a ==> [c,d] != [a,b] */
492 if (res == pn_Cmp_Lt)
495 /* d <= a ==> [c,d) != [a,b] AND [c,d] != (a,b] */
496 if ((r_iv->flags & MAX_EXCLUDED || l_iv->flags & MIN_EXCLUDED)
497 && (res == pn_Cmp_Eq))
502 /* two intervals can be compared for not equality only if they are a single value */
503 if (l_iv->min == l_iv->max && r_iv->min == r_iv->max)
504 return tarval_cmp(l_iv->min, r_iv->min) != pn_Cmp_Eq ? tv_true : tv_false;
508 res = tarval_cmp(l_iv->max, r_iv->min);
510 /* [a, b] < [c, d] <==> b < c */
511 if (res == pn_Cmp_Lt)
514 /* if one border is excluded, b <= c is enough */
515 if ((l_iv->flags & MAX_EXCLUDED || r_iv->flags & MIN_EXCLUDED) &&
519 /* [a, b] >= [c, d] <==> a > d */
520 res = tarval_cmp(l_iv->min, r_iv->max);
521 if (res == pn_Cmp_Gt)
524 /* if one border is excluded, a >= d is enough */
525 if ((l_iv->flags & MIN_EXCLUDED || r_iv->flags & MAX_EXCLUDED) &&
531 /* [a, b) <= [c, d] or [a, b] <= (c, d] <==> b <= c */
532 flags = (l_iv->flags & MAX_EXCLUDED) | (r_iv->flags & MIN_EXCLUDED);
534 res = tarval_cmp(l_iv->max, r_iv->min);
536 if (res == pn_Cmp_Lt || res == pn_Cmp_Eq)
540 res = tarval_cmp(l_iv->min, r_iv->max);
542 /* [a, b] > [c, d] <==> a > d */
543 if (res == pn_Cmp_Gt)
546 /* if one border is excluded, a >= d is enough */
547 if ((l_iv->flags & MIN_EXCLUDED || r_iv->flags & MAX_EXCLUDED) &&
553 /* Hmm. if both are intervals, we can find an order */
563 * Returns non-zero, if a given relation is transitive.
565 static int is_transitive(pn_Cmp pnc) {
566 return (pn_Cmp_False < pnc && pnc < pn_Cmp_Lg);
567 } /* is_transitive */
571 * Return the value of a Cmp if one or both predecessors
574 * @param cmp the Cmp node
575 * @param left the left operand of the Cmp
576 * @param right the right operand of the Cmp
577 * @param pnc the compare relation
579 tarval *computed_value_Cmp_Confirm(ir_node *cmp, ir_node *left, ir_node *right, pn_Cmp pnc)
582 pn_Cmp l_pnc, res_pnc, neg_pnc;
583 interval_t l_iv, r_iv;
587 if (get_irn_op(right) == op_Confirm) {
590 /* we want the Confirm on the left side */
595 pnc = get_inversed_pnc(pnc);
597 else if (get_irn_op(left) != op_Confirm) {
598 /* no Confirm on either one side, finish */
602 /* ok, here at least left is a Confirm, right might be */
603 l_bound = get_Confirm_bound(left);
604 l_pnc = get_Confirm_cmp(left);
606 if (get_irn_op(right) == op_Confirm) {
608 * both sides are Confirm's. Check some rare cases first.
610 ir_node *r_bound = get_Confirm_bound(right);
611 pn_Cmp r_pnc = get_Confirm_cmp(right);
614 * some check can be made WITHOUT constant bounds
616 if (r_bound == l_bound) {
617 if (is_transitive(l_pnc)) {
618 pn_Cmp r_inc_pnc = get_inversed_pnc(r_pnc);
621 * triangle inequality:
623 * a CMP B && B CMP b => a CMP b, !(a ~CMP b)
625 * We handle correctly cases with some <=/>= here
627 if ((l_pnc & ~pn_Cmp_Eq) == (r_inc_pnc & ~pn_Cmp_Eq)) {
628 res_pnc = (l_pnc & ~pn_Cmp_Eq) | (l_pnc & r_inc_pnc & pn_Cmp_Eq);
630 if ((pnc == res_pnc) || ((pnc & ~pn_Cmp_Eq) == res_pnc)) {
631 DBG_OUT_TR(l_pnc, l_bound, r_pnc, r_bound, pnc, "true");
632 DBG_EVAL_CONFIRM(cmp);
633 return tarval_b_true;
636 pn_Cmp neg_pnc = get_negated_pnc(pnc, get_irn_mode(left));
638 if ((neg_pnc == res_pnc) || ((neg_pnc & ~pn_Cmp_Eq) == res_pnc)) {
639 DBG_OUT_TR(l_pnc, l_bound, r_pnc, r_bound, pnc, "false");
640 DBG_EVAL_CONFIRM(cmp);
641 return tarval_b_false;
649 * Here, we check only the right Confirm, as the left Confirms are
650 * checked later anyway.
653 if (left == r_bound) {
655 * l == bound(r) AND pnc(r) == pnc:
657 * We know that a CMP b and check for that
659 if ((r_pnc == pnc) || (r_pnc == (pnc & ~pn_Cmp_Eq))) {
660 DBG_OUT_R(r_pnc, r_bound, left, pnc, right, "true");
661 DBG_EVAL_CONFIRM(cmp);
662 return tarval_b_true;
665 * l == bound(r) AND pnc(r) != pnc:
667 * We know that a CMP b and check for a ~CMP b
670 mode = get_irn_mode(left);
671 neg_pnc = get_negated_pnc(pnc, mode);
673 if ((r_pnc == neg_pnc) || (r_pnc == (neg_pnc & ~pn_Cmp_Eq))) {
674 DBG_OUT_R(r_pnc, r_bound, left, pnc, right, "false");
675 DBG_EVAL_CONFIRM(cmp);
676 return tarval_b_false;
681 /* now, try interval magic */
683 get_interval(&l_iv, l_bound, l_pnc),
684 get_interval(&r_iv, r_bound, r_pnc),
687 if (tv != tarval_bad) {
688 DBG_EVAL_CONFIRM(cmp);
693 /* from Here, check only left Confirm */
696 * some checks can be made WITHOUT constant bounds
698 if (right == l_bound) {
700 * r == bound(l) AND pnc(l) == pnc:
702 * We know that a CMP b and check for that
704 if ((l_pnc == pnc) || (l_pnc == (pnc & ~pn_Cmp_Eq))) {
705 DBG_OUT_L(l_pnc, l_bound, left, pnc, right, "true");
706 DBG_EVAL_CONFIRM(cmp);
707 return tarval_b_true;
710 * r == bound(l) AND pnc(l) is Not(pnc):
712 * We know that a CMP b and check for a ~CMP b
715 mode = get_irn_mode(left);
716 neg_pnc = get_negated_pnc(pnc, mode);
718 if ((l_pnc == neg_pnc) || (l_pnc == (neg_pnc & ~pn_Cmp_Eq))) {
719 DBG_OUT_L(l_pnc, l_bound, left, pnc, right, "false");
720 DBG_EVAL_CONFIRM(cmp);
721 return tarval_b_false;
726 /* now, only right == Const can help */
727 tv = value_of(right);
729 if (tv != tarval_bad) {
731 get_interval(&l_iv, l_bound, l_pnc),
732 get_interval_from_tv(&r_iv, tv),
736 if (tv != tarval_bad)
737 DBG_EVAL_CONFIRM(cmp);
740 } /* computed_value_Cmp_Confirm */
744 * For debugging. Prints an interval into a string.
746 * @param buf address of a string buffer
747 * @param len length of the string buffer
748 * @param iv the interval
750 static int iv_snprintf(char *buf, size_t len, const interval_t *iv) {
751 char smin[64], smax[64];
754 tarval_snprintf(smin, sizeof(smin), iv->min);
756 if (iv->min != iv->max || (iv->flags & (MIN_EXCLUDED|MAX_EXCLUDED))) {
757 tarval_snprintf(smax, sizeof(smax), iv->max);
759 return snprintf(buf, len, "%c%s, %s%c",
760 iv->flags & MIN_EXCLUDED ? '(' : '[',
762 iv->flags & MAX_EXCLUDED ? ')' : ']'
766 return snprintf(buf, len, "%s", smin);
768 return snprintf(buf, len, "<UNKNOWN>");
772 * For debugging. Prints an interval compare.
774 * @param l_iv the left interval
775 * @param r_iv the right interval
776 * @param pnc the compare relation
778 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)
797 tarval *tv = (compare_iv)(l_iv, r_iv, pnc);
799 if (tv == tarval_bad)
802 ir_printf("In %e:\n", get_irg_entity(current_ir_graph));
803 print_iv_cmp(l_iv, r_iv, pnc);
804 ir_printf(" = %T\n", tv);
806 } /* compare_iv_dbg */
808 #endif /* DEBUG_CONFIRM */