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)
88 #define RET_ON(x) if (x) return 1; break
91 ir_mode *mode = get_irn_mode(n);
94 while (get_irn_op(n) == op_Confirm) {
96 * Note: A Confirm is never after a Const. So,
97 * we simply can check the bound for being a Const
98 * without the fear that is might be hidden by a further Confirm.
100 tv = value_of(get_Confirm_bound(n));
101 if (tv == tarval_bad)
104 pnc = tarval_cmp(tv, get_mode_null(mode));
107 * Beware: C might by a NaN. It is not clear, what we should do
108 * than. Of course a NaN is != 0, but we might use this function
109 * to remove up Exceptions, and NaN's might generate Exception.
110 * So, we do NOT handle NaNs here for safety.
112 * Note that only the C != 0 case need additional checking.
114 switch (get_Confirm_cmp(n)) {
115 case pn_Cmp_Eq: /* n == C /\ C != 0 ==> n != 0 */
116 RET_ON(pnc != pn_Cmp_Eq && pnc != pn_Cmp_Uo);
117 case pn_Cmp_Lg: /* n != C /\ C == 0 ==> n != 0 */
118 RET_ON(pnc == pn_Cmp_Eq);
119 case pn_Cmp_Lt: /* n < C /\ C <= 0 ==> n != 0 */
120 RET_ON(pnc == pn_Cmp_Lt || pnc == pn_Cmp_Eq);
121 case pn_Cmp_Le: /* n <= C /\ C < 0 ==> n != 0 */
122 RET_ON(pnc == pn_Cmp_Lt);
123 case pn_Cmp_Ge: /* n >= C /\ C > 0 ==> n != 0 */
124 RET_ON(pnc == pn_Cmp_Gt);
125 case pn_Cmp_Gt: /* n > C /\ C >= 0 ==> n != 0 */
126 RET_ON(pnc == pn_Cmp_Gt || pnc == pn_Cmp_Eq);
131 /* there might be several Confirms one after other that form an interval */
132 n = get_Confirm_value(n);
136 if (tv == tarval_bad)
139 pnc = tarval_cmp(tv, get_mode_null(mode));
141 /* again, need check for NaN */
142 return (pnc != pn_Cmp_Eq) && (pnc != pn_Cmp_Uo);
148 * Check, if the value of a node cannot represent a NULL pointer.
150 * - Casts are skipped
151 * - If sel_based_null_check_elim is enabled, all
152 * Sel nodes can be skipped.
153 * - A SymConst(entity) is NEVER a NULL pointer
154 * - Confirms are evaluated
156 int value_not_null(ir_node *n)
162 assert(mode_is_reference(get_irn_mode(n)));
163 if (get_opt_sel_based_null_check_elim()) {
164 /* skip all Sel nodes and Cast's */
165 while (op == op_Sel) {
166 n = skip_Cast(get_Sel_ptr(n));
170 if (op == op_SymConst && get_SymConst_kind(n) == symconst_addr_ent)
172 if (op == op_Confirm) {
173 if (get_Confirm_cmp(n) == pn_Cmp_Lg &&
174 classify_Const(get_Confirm_bound(n)) == CNST_NULL)
181 * Check, if the value of a node can be confirmed >= 0 or <= 0,
182 * If the mode of the value did not honor signed zeros, else
183 * check for >= 0 or < 0.
185 value_classify classify_value_sign(ir_node *n)
191 if (get_irn_op(n) != op_Confirm)
192 return VALUE_UNKNOWN;
194 tv = value_of(get_Confirm_bound(n));
195 if (tv == tarval_bad)
196 return VALUE_UNKNOWN;
198 mode = get_irn_mode(n);
201 * We can handle only >=, >, <, <= cases.
202 * We could handle == too, but this will be optimized into
205 * Note that for integer modes we have a slightly better
206 * optimization possibilities, so we handle this
209 cmp = get_Confirm_cmp(n);
214 * must be x < c <= 1 to be useful if integer mode and -0 = 0
215 * x < c <= 0 to be useful else
219 * must be x <= c < 1 to be useful if integer mode and -0 = 0
220 * x <= c < 0 to be useful else
222 c = mode_is_int(mode) && mode_honor_signed_zeros(mode) ?
223 get_mode_one(mode) : get_mode_null(mode);
225 ncmp = tarval_cmp(tv, c);
226 if (ncmp == pn_Cmp_Eq)
229 if (cmp != (ncmp ^ pn_Cmp_Eq))
230 return VALUE_UNKNOWN;
233 return VALUE_NEGATIVE;
237 * must be x >= c > -1 to be useful if integer mode
238 * x >= c >= 0 to be useful else
242 * must be x > c >= -1 to be useful if integer mode
243 * x > c >= 0 to be useful else
245 if (mode_is_int(mode)) {
246 c = get_mode_minus_one(mode);
248 ncmp = tarval_cmp(tv, c);
249 if (ncmp == pn_Cmp_Eq)
252 if (cmp != (ncmp ^ pn_Cmp_Eq))
253 return VALUE_UNKNOWN;
256 c = get_mode_minus_one(mode);
258 ncmp = tarval_cmp(tv, c);
260 if (ncmp != pn_Cmp_Eq && ncmp != pn_Cmp_Gt)
261 return VALUE_UNKNOWN;
265 return VALUE_POSITIVE;
268 return VALUE_UNKNOWN;
273 * construct an interval from a value
275 * @return the filled interval or NULL if no interval
276 * can be created (happens only on floating point
278 static interval_t *get_interval_from_tv(interval_t *iv, tarval *tv)
280 ir_mode *mode = get_tarval_mode(tv);
282 if (tv == tarval_bad) {
283 if (mode_is_float(mode)) {
284 /* NaN could be included which we cannot handle */
285 iv->min = tarval_bad;
286 iv->max = tarval_bad;
287 iv->flags = MIN_EXCLUDED | MAX_EXCLUDED;
292 iv->min = get_mode_min(mode);
293 iv->max = get_mode_max(mode);
294 iv->flags = MIN_INCLUDED | MAX_INCLUDED;
299 if (mode_is_float(mode)) {
300 if (tv == get_mode_NAN(mode)) {
301 /* arg, we cannot handle NaN's. */
302 iv->min = tarval_bad;
303 iv->max = tarval_bad;
304 iv->flags = MIN_EXCLUDED | MAX_EXCLUDED;
312 iv->flags = MIN_INCLUDED | MAX_INCLUDED;
318 * construct an interval from a Confirm
320 * @param iv an empty interval, will be filled
321 * @param bound the bound value
322 * @param pnc the Confirm compare relation
324 * @return the filled interval or NULL if no interval
325 * can be created (happens only on floating point
327 static interval_t *get_interval(interval_t *iv, ir_node *bound, pn_Cmp pnc)
329 ir_mode *mode = get_irn_mode(bound);
330 tarval *tv = value_of(bound);
332 if (tv == tarval_bad) {
333 /* There is nothing we could do here. For integer
334 * modes we could return [-oo, +oo], but there is
335 * nothing we could deduct from such an interval.
336 * So, speed things up and return unknown.
338 iv->min = tarval_bad;
339 iv->max = tarval_bad;
340 iv->flags = MIN_EXCLUDED | MAX_EXCLUDED;
344 if (mode_is_float(mode)) {
345 if (tv == get_mode_NAN(mode)) {
346 /* arg, we cannot handle NaN's. */
347 iv->min = tarval_bad;
348 iv->max = tarval_bad;
349 iv->flags = MIN_EXCLUDED | MAX_EXCLUDED;
355 /* check which side is known */
361 iv->flags = MIN_INCLUDED | MAX_INCLUDED;
366 iv->min = get_mode_min(mode);
368 iv->flags = MIN_INCLUDED | MAX_INCLUDED;
373 iv->min = get_mode_min(mode);
375 iv->flags = MIN_INCLUDED | MAX_EXCLUDED;
381 iv->max = get_mode_max(mode);
382 iv->flags = MIN_EXCLUDED | MAX_INCLUDED;
388 iv->max = get_mode_max(mode);
389 iv->flags = MIN_INCLUDED | MAX_INCLUDED;
394 * Ordered means, that at least neither
395 * our bound nor our value ara NaN's
398 iv->min = get_mode_min(mode);
399 iv->max = get_mode_max(mode);
400 iv->flags = MIN_INCLUDED | MAX_INCLUDED;
405 * We do not handle UNORDERED, as a NaN
406 * could be included in the interval.
408 iv->min = tarval_bad;
409 iv->max = tarval_bad;
410 iv->flags = MIN_EXCLUDED | MAX_EXCLUDED;
414 if (iv->min != tarval_bad && iv->max != tarval_bad)
420 * Try to evaluate l_iv pnc r_iv.
422 * @param l_iv the left interval
423 * @param r_iv the right interval
424 * @param pnc the compare relation
427 * tarval_b_true or tarval_b_false it it can be evaluated,
430 static tarval *(compare_iv)(const interval_t *l_iv, const interval_t *r_iv, pn_Cmp pnc)
434 tarval *tv_true = tarval_b_true, *tv_false = tarval_b_false;
436 /* if one interval contains NaNs, we cannot evaluate anything */
437 if (! l_iv || ! r_iv)
440 /* we can only check ordered relations */
441 if (pnc & pn_Cmp_Uo) {
444 pnc = get_negated_pnc(pnc, get_tarval_mode(l_iv->min));
450 /* if we have > or >=, we do the inverse to save some cases */
451 if (pnc == pn_Cmp_Ge || pnc == pn_Cmp_Gt) {
454 pnc = get_inversed_pnc(pnc);
460 /* now, only the following cases remains */
463 /* two intervals can be compared for equality only if they are a single value */
464 if (l_iv->min == l_iv->max && r_iv->min == r_iv->max)
465 return tarval_cmp(l_iv->min, r_iv->min) == pn_Cmp_Eq ? tv_true : tv_false;
467 /* if both intervals do not intersect, it is never equal */
468 res = tarval_cmp(l_iv->max, r_iv->min);
470 /* b < c ==> [a,b] != [c,d] */
471 if (res == pn_Cmp_Lt)
474 /* b <= c ==> [a,b) != [c,d] AND [a,b] != (c,d] */
475 if ((l_iv->flags & MAX_EXCLUDED || r_iv->flags & MIN_EXCLUDED)
476 && (res == pn_Cmp_Eq))
479 res = tarval_cmp(r_iv->max, l_iv->min);
481 /* d < a ==> [c,d] != [a,b] */
482 if (res == pn_Cmp_Lt)
485 /* d <= a ==> [c,d) != [a,b] AND [c,d] != (a,b] */
486 if ((r_iv->flags & MAX_EXCLUDED || l_iv->flags & MIN_EXCLUDED)
487 && (res == pn_Cmp_Eq))
492 /* two intervals can be compared for not equality only if they are a single value */
493 if (l_iv->min == l_iv->max && r_iv->min == r_iv->max)
494 return tarval_cmp(l_iv->min, r_iv->min) != pn_Cmp_Eq ? tv_true : tv_false;
498 res = tarval_cmp(l_iv->max, r_iv->min);
500 /* [a, b] < [c, d] <==> b < c */
501 if (res == pn_Cmp_Lt)
504 /* if one border is excluded, b <= c is enough */
505 if ((l_iv->flags & MAX_EXCLUDED || r_iv->flags & MIN_EXCLUDED) &&
509 /* [a, b] >= [c, d] <==> a > d */
510 res = tarval_cmp(l_iv->min, r_iv->max);
511 if (res == pn_Cmp_Gt)
514 /* if one border is excluded, a >= d is enough */
515 if ((l_iv->flags & MIN_EXCLUDED || r_iv->flags & MAX_EXCLUDED) &&
521 /* [a, b) <= [c, d] or [a, b] <= (c, d] <==> b <= c */
522 flags = (l_iv->flags & MAX_EXCLUDED) | (r_iv->flags & MIN_EXCLUDED);
524 res = tarval_cmp(l_iv->max, r_iv->min);
526 if (res == pn_Cmp_Lt || res == pn_Cmp_Eq)
530 res = tarval_cmp(l_iv->min, r_iv->max);
532 /* [a, b] > [c, d] <==> a > d */
533 if (res == pn_Cmp_Gt)
536 /* if one border is excluded, a >= d is enough */
537 if ((l_iv->flags & MIN_EXCLUDED || r_iv->flags & MAX_EXCLUDED) &&
543 /* Hmm. if both are intervals, we can find an order */
553 * Returns non-zero, if a given relation is transitive.
555 static int is_transitive(pn_Cmp pnc) {
556 return (pn_Cmp_False < pnc && pnc < pn_Cmp_Lg);
561 * Return the value of a Cmp if one or both predecessors
564 * @param cmp the Cmp node
565 * @param left the left operand of the Cmp
566 * @param right the right operand of the Cmp
567 * @param pnc the compare relation
569 tarval *computed_value_Cmp_Confirm(ir_node *cmp, ir_node *left, ir_node *right, pn_Cmp pnc)
572 pn_Cmp l_pnc, res_pnc, neg_pnc;
573 interval_t l_iv, r_iv;
577 if (get_irn_op(right) == op_Confirm) {
580 /* we want the Confirm on the left side */
585 pnc = get_inversed_pnc(pnc);
587 else if (get_irn_op(left) != op_Confirm) {
588 /* no Confirm on either one side, finish */
592 /* ok, here at least left is a Confirm, right might be */
593 l_bound = get_Confirm_bound(left);
594 l_pnc = get_Confirm_cmp(left);
596 if (get_irn_op(right) == op_Confirm) {
598 * both sides are Confirm's. Check some rare cases first.
600 ir_node *r_bound = get_Confirm_bound(right);
601 pn_Cmp r_pnc = get_Confirm_cmp(right);
604 * some check can be made WITHOUT constant bounds
606 if (r_bound == l_bound) {
607 if (is_transitive(l_pnc)) {
608 pn_Cmp r_inc_pnc = get_inversed_pnc(r_pnc);
611 * triangle inequality:
613 * a CMP B && B CMP b => a CMP b, !(a ~CMP b)
615 * We handle correctly cases with some <=/>= here
617 if ((l_pnc & ~pn_Cmp_Eq) == (r_inc_pnc & ~pn_Cmp_Eq)) {
618 res_pnc = (l_pnc & ~pn_Cmp_Eq) | (l_pnc & r_inc_pnc & pn_Cmp_Eq);
620 if ((pnc == res_pnc) || ((pnc & ~pn_Cmp_Eq) == res_pnc)) {
621 DBG_OUT_TR(l_pnc, l_bound, r_pnc, r_bound, pnc, "true");
622 DBG_EVAL_CONFIRM(cmp);
623 return tarval_b_true;
626 pn_Cmp neg_pnc = get_negated_pnc(pnc, get_irn_mode(left));
628 if ((neg_pnc == res_pnc) || ((neg_pnc & ~pn_Cmp_Eq) == res_pnc)) {
629 DBG_OUT_TR(l_pnc, l_bound, r_pnc, r_bound, pnc, "false");
630 DBG_EVAL_CONFIRM(cmp);
631 return tarval_b_false;
639 * Here, we check only the right Confirm, as the left Confirms are
640 * checked later anyway.
643 if (left == r_bound) {
645 * l == bound(r) AND pnc(r) == pnc:
647 * We know that a CMP b and check for that
649 if ((r_pnc == pnc) || (r_pnc == (pnc & ~pn_Cmp_Eq))) {
650 DBG_OUT_R(r_pnc, r_bound, left, pnc, right, "true");
651 DBG_EVAL_CONFIRM(cmp);
652 return tarval_b_true;
655 * l == bound(r) AND pnc(r) != pnc:
657 * We know that a CMP b and check for a ~CMP b
660 mode = get_irn_mode(left);
661 neg_pnc = get_negated_pnc(pnc, mode);
663 if ((r_pnc == neg_pnc) || (r_pnc == (neg_pnc & ~pn_Cmp_Eq))) {
664 DBG_OUT_R(r_pnc, r_bound, left, pnc, right, "false");
665 DBG_EVAL_CONFIRM(cmp);
666 return tarval_b_false;
671 /* now, try interval magic */
673 get_interval(&l_iv, l_bound, l_pnc),
674 get_interval(&r_iv, r_bound, r_pnc),
677 if (tv != tarval_bad) {
678 DBG_EVAL_CONFIRM(cmp);
683 /* from Here, check only left Confirm */
686 * some checks can be made WITHOUT constant bounds
688 if (right == l_bound) {
690 * r == bound(l) AND pnc(l) == pnc:
692 * We know that a CMP b and check for that
694 if ((l_pnc == pnc) || (l_pnc == (pnc & ~pn_Cmp_Eq))) {
695 DBG_OUT_L(l_pnc, l_bound, left, pnc, right, "true");
696 DBG_EVAL_CONFIRM(cmp);
697 return tarval_b_true;
700 * r == bound(l) AND pnc(l) is Not(pnc):
702 * We know that a CMP b and check for a ~CMP b
705 mode = get_irn_mode(left);
706 neg_pnc = get_negated_pnc(pnc, mode);
708 if ((l_pnc == neg_pnc) || (l_pnc == (neg_pnc & ~pn_Cmp_Eq))) {
709 DBG_OUT_L(l_pnc, l_bound, left, pnc, right, "false");
710 DBG_EVAL_CONFIRM(cmp);
711 return tarval_b_false;
716 /* now, only right == Const can help */
717 tv = value_of(right);
719 if (tv != tarval_bad) {
721 get_interval(&l_iv, l_bound, l_pnc),
722 get_interval_from_tv(&r_iv, tv),
726 if (tv != tarval_bad)
727 DBG_EVAL_CONFIRM(cmp);
734 * For debugging. Prints an interval into a string.
736 * @param buf address of a string buffer
737 * @param len length of the string buffer
738 * @param iv the interval
740 static int iv_snprintf(char *buf, size_t len, const interval_t *iv) {
741 char smin[64], smax[64];
744 tarval_snprintf(smin, sizeof(smin), iv->min);
746 if (iv->min != iv->max || (iv->flags & (MIN_EXCLUDED|MAX_EXCLUDED))) {
747 tarval_snprintf(smax, sizeof(smax), iv->max);
749 return snprintf(buf, len, "%c%s, %s%c",
750 iv->flags & MIN_EXCLUDED ? '(' : '[',
752 iv->flags & MAX_EXCLUDED ? ')' : ']'
756 return snprintf(buf, len, "%s", smin);
758 return snprintf(buf, len, "<UNKNOWN>");
762 * For debugging. Prints an interval compare.
764 * @param l_iv the left interval
765 * @param r_iv the right interval
766 * @param pnc the compare relation
768 static void print_iv_cmp(const interval_t *l_iv, const interval_t *r_iv, pn_Cmp pnc)
770 char sl[128], sr[128];
772 iv_snprintf(sl, sizeof(sl), l_iv);
773 iv_snprintf(sr, sizeof(sr), r_iv);
775 ir_printf("%s %= %s", sl, pnc, sr);
779 * For debugging. call *compare_iv() and prints inputs and result.
781 * @param l_iv the left interval
782 * @param r_iv the right interval
783 * @param pnc the compare relation
785 static tarval *compare_iv_dbg(const interval_t *l_iv, const interval_t *r_iv, pn_Cmp pnc)
787 tarval *tv = (compare_iv)(l_iv, r_iv, pnc);
789 if (tv == tarval_bad)
792 ir_printf("In %e:\n", get_irg_entity(current_ir_graph));
793 print_iv_cmp(l_iv, r_iv, pnc);
794 ir_printf(" = %T\n", tv);
798 #endif /* DEBUG_CONFIRM */