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"
26 MIN_INCLUDED = 0x00, /**< [min, ... */
27 MAX_INCLUDED = 0x00, /**< ..., max] */
28 MIN_EXCLUDED = 0x01, /**< (min, ... */
29 MAX_EXCLUDED = 0x02 /**< ..., max) */
33 * An interval. We could use
34 * intervals that ALWAYS include its borders, even for
35 * floating point, as the precision is limited.
36 * However, as our tarval module did not support
37 * such kind of operation, we use border flags allowing
40 typedef struct _interval_t {
41 tarval *min; /**< lowest border */
42 tarval *max; /**< highest border */
43 unsigned char flags; /**< border flags */
48 #define compare_iv(l_iv, r_iv, pnc) compare_iv_dbg(l_iv, r_iv, pnc)
51 static tarval *compare_iv_dbg(const interval_t *l_iv, const interval_t *r_iv, pn_Cmp pnc);
54 #define DBG_OUT_TR(l_pnc, l_bound, r_pnc, r_bound, pnc, v) \
55 ir_printf("In %e:\na %= %n && b %= %n ==> a %= b == %s\n", \
56 get_irg_entity(current_ir_graph), \
57 l_pnc, l_bound, r_pnc, r_bound, pnc, v);
60 #define DBG_OUT_R(r_pnc, r_bound, left, pnc, right, v) \
61 ir_printf("In %e:\na %= %n ==> %n %= %n == %s\n", \
62 get_irg_entity(current_ir_graph), \
63 r_pnc, r_bound, left, pnc, right, v);
66 #define DBG_OUT_L(l_pnc, l_bound, left, pnc, right, v) \
67 ir_printf("In %e:\na %= %n ==> %n %= %n == %s\n", \
68 get_irg_entity(current_ir_graph), \
69 l_pnc, l_bound, left, pnc, right, v);
73 #define DBG_OUT_TR(l_pnc, l_bound, r_pnc, r_bound, pnc, v)
74 #define DBG_OUT_R(r_pnc, r_bound, left, pnc, right, v)
75 #define DBG_OUT_L(l_pnc, l_bound, left, pnc, right, v)
77 #endif /* DEBUG_CONFIRM */
80 * Check, if the value of a node is != 0.
82 * This is a often needed case, so we handle here Confirm
85 int value_not_zero(ir_node *n)
87 #define RET_ON(x) if (x) return 1; break
90 ir_mode *mode = get_irn_mode(n);
93 while (get_irn_op(n) == op_Confirm) {
95 * Note: A Confirm is never after a Const. So,
96 * we simply can check the bound for being a Const
97 * without the fear that is might be hidden by a further Confirm.
99 tv = value_of(get_Confirm_bound(n));
100 if (tv == tarval_bad)
103 pnc = tarval_cmp(tv, get_mode_null(mode));
106 * Beware: C might by a NaN. It is not clear, what we should do
107 * than. Of course a NaN is != 0, but we might use this function
108 * to remove up Exceptions, and NaN's might generate Exception.
109 * So, we do NOT handle NaNs here for safety.
111 * Note that only the C != 0 case need additional checking.
113 switch (get_Confirm_cmp(n)) {
114 case pn_Cmp_Eq: /* n == C /\ C != 0 ==> n != 0 */
115 RET_ON(pnc != pn_Cmp_Eq && pnc != pn_Cmp_Uo);
116 case pn_Cmp_Lg: /* n != C /\ C == 0 ==> n != 0 */
117 RET_ON(pnc == pn_Cmp_Eq);
118 case pn_Cmp_Lt: /* n < C /\ C <= 0 ==> n != 0 */
119 RET_ON(pnc == pn_Cmp_Lt || pnc == pn_Cmp_Eq);
120 case pn_Cmp_Le: /* n <= C /\ C < 0 ==> n != 0 */
121 RET_ON(pnc == pn_Cmp_Lt);
122 case pn_Cmp_Ge: /* n >= C /\ C > 0 ==> n != 0 */
123 RET_ON(pnc == pn_Cmp_Gt);
124 case pn_Cmp_Gt: /* n > C /\ C >= 0 ==> n != 0 */
125 RET_ON(pnc == pn_Cmp_Gt || pnc == pn_Cmp_Eq);
130 /* there might be several Confirms one after other that form an interval */
131 n = get_Confirm_value(n);
135 if (tv == tarval_bad)
138 pnc = tarval_cmp(tv, get_mode_null(mode));
140 /* again, need check for NaN */
141 return (pnc != pn_Cmp_Eq) && (pnc != pn_Cmp_Uo);
147 * Check, if the value of a node is != NULL.
149 * This is a often needed case, so we handle here Confirm
152 int value_not_null(ir_node *n)
155 ir_mode *mode = get_irn_mode(n);
157 assert(mode_is_reference(mode));
159 while (get_irn_op(n) == op_Confirm) {
161 * Note: A Confirm is never after a Const. So,
162 * we simply can check the bound for being a Const
163 * without the fear that is might be hidden by a further Confirm.
165 tv = value_of(get_Confirm_bound(n));
166 if (tv == get_mode_null(mode) &&
167 get_Confirm_cmp(n) == pn_Cmp_Lg) {
168 /* n != C /\ C == 0 ==> n != 0 */
172 /* there might be several Confirms one after other that form an interval */
173 n = get_Confirm_value(n);
179 * Check, if the value of a node can be confirmed >= 0 or <= 0,
180 * If the mode of the value did not honor signed zeros, else
181 * check for >= 0 or < 0.
183 value_classify classify_value_sign(ir_node *n)
189 if (get_irn_op(n) != op_Confirm)
190 return VALUE_UNKNOWN;
192 tv = value_of(get_Confirm_bound(n));
193 if (tv == tarval_bad)
194 return VALUE_UNKNOWN;
196 mode = get_irn_mode(n);
199 * We can handle only >=, >, <, <= cases.
200 * We could handle == too, but this will be optimized into
203 * Note that for integer modes we have a slightly better
204 * optimization possibilities, so we handle this
207 cmp = get_Confirm_cmp(n);
212 * must be x < c <= 1 to be useful if integer mode and -0 = 0
213 * x < c <= 0 to be useful else
217 * must be x <= c < 1 to be useful if integer mode and -0 = 0
218 * x <= c < 0 to be useful else
220 c = mode_is_int(mode) && mode_honor_signed_zeros(mode) ?
221 get_mode_one(mode) : get_mode_null(mode);
223 ncmp = tarval_cmp(tv, c);
224 if (ncmp == pn_Cmp_Eq)
227 if (cmp != (ncmp ^ pn_Cmp_Eq))
228 return VALUE_UNKNOWN;
231 return VALUE_NEGATIVE;
235 * must be x >= c > -1 to be useful if integer mode
236 * x >= c >= 0 to be useful else
240 * must be x > c >= -1 to be useful if integer mode
241 * x > c >= 0 to be useful else
243 if (mode_is_int(mode)) {
244 c = get_mode_minus_one(mode);
246 ncmp = tarval_cmp(tv, c);
247 if (ncmp == pn_Cmp_Eq)
250 if (cmp != (ncmp ^ pn_Cmp_Eq))
251 return VALUE_UNKNOWN;
254 c = get_mode_minus_one(mode);
256 ncmp = tarval_cmp(tv, c);
258 if (ncmp != pn_Cmp_Eq && ncmp != pn_Cmp_Gt)
259 return VALUE_UNKNOWN;
263 return VALUE_POSITIVE;
266 return VALUE_UNKNOWN;
271 * construct an interval from a value
273 * @return the filled interval or NULL if no interval
274 * can be created (happens only on floating point
276 static interval_t *get_interval_from_tv(interval_t *iv, tarval *tv)
278 ir_mode *mode = get_tarval_mode(tv);
280 if (tv == tarval_bad) {
281 if (mode_is_float(mode)) {
282 /* NaN could be included which we cannot handle */
283 iv->min = tarval_bad;
284 iv->max = tarval_bad;
285 iv->flags = MIN_EXCLUDED | MAX_EXCLUDED;
290 iv->min = get_mode_min(mode);
291 iv->max = get_mode_max(mode);
292 iv->flags = MIN_INCLUDED | MAX_INCLUDED;
297 if (mode_is_float(mode)) {
298 if (tv == get_mode_NAN(mode)) {
299 /* arg, we cannot handle NaN's. */
300 iv->min = tarval_bad;
301 iv->max = tarval_bad;
302 iv->flags = MIN_EXCLUDED | MAX_EXCLUDED;
310 iv->flags = MIN_INCLUDED | MAX_INCLUDED;
316 * construct an interval from a Confirm
318 * @param iv an empty interval, will be filled
319 * @param bound the bound value
320 * @param pnc the Confirm compare relation
322 * @return the filled interval or NULL if no interval
323 * can be created (happens only on floating point
325 static interval_t *get_interval(interval_t *iv, ir_node *bound, pn_Cmp pnc)
327 ir_mode *mode = get_irn_mode(bound);
328 tarval *tv = value_of(bound);
330 if (tv == tarval_bad) {
331 /* There is nothing we could do here. For integer
332 * modes we could return [-oo, +oo], but there is
333 * nothing we could deduct from such an interval.
334 * So, speed things up and return unknown.
336 iv->min = tarval_bad;
337 iv->max = tarval_bad;
338 iv->flags = MIN_EXCLUDED | MAX_EXCLUDED;
342 if (mode_is_float(mode)) {
343 if (tv == get_mode_NAN(mode)) {
344 /* arg, we cannot handle NaN's. */
345 iv->min = tarval_bad;
346 iv->max = tarval_bad;
347 iv->flags = MIN_EXCLUDED | MAX_EXCLUDED;
353 /* check which side is known */
359 iv->flags = MIN_INCLUDED | MAX_INCLUDED;
364 iv->min = get_mode_min(mode);
366 iv->flags = MIN_INCLUDED | MAX_INCLUDED;
371 iv->min = get_mode_min(mode);
373 iv->flags = MIN_INCLUDED | MAX_EXCLUDED;
379 iv->max = get_mode_max(mode);
380 iv->flags = MIN_EXCLUDED | MAX_INCLUDED;
386 iv->max = get_mode_max(mode);
387 iv->flags = MIN_INCLUDED | MAX_INCLUDED;
392 * Ordered means, that at least neither
393 * our bound nor our value ara NaN's
396 iv->min = get_mode_min(mode);
397 iv->max = get_mode_max(mode);
398 iv->flags = MIN_INCLUDED | MAX_INCLUDED;
403 * We do not handle UNORDERED, as a NaN
404 * could be included in the interval.
406 iv->min = tarval_bad;
407 iv->max = tarval_bad;
408 iv->flags = MIN_EXCLUDED | MAX_EXCLUDED;
412 if (iv->min != tarval_bad && iv->max != tarval_bad)
418 * Try to evaluate l_iv pnc r_iv.
420 * @param l_iv the left interval
421 * @param r_iv the right interval
422 * @param pnc the compare relation
425 * tarval_b_true or tarval_b_false it it can be evaluated,
428 static tarval *(compare_iv)(const interval_t *l_iv, const interval_t *r_iv, pn_Cmp pnc)
432 tarval *tv_true = tarval_b_true, *tv_false = tarval_b_false;
434 /* if one interval contains NaNs, we cannot evaluate anything */
435 if (! l_iv || ! r_iv)
438 /* we can only check ordered relations */
439 if (pnc & pn_Cmp_Uo) {
442 pnc = get_negated_pnc(pnc, get_tarval_mode(l_iv->min));
448 /* if we have > or >=, we do the inverse to save some cases */
449 if (pnc == pn_Cmp_Ge || pnc == pn_Cmp_Gt) {
452 pnc = get_inversed_pnc(pnc);
458 /* now, only the following cases remains */
461 /* two intervals can be compared for equality only if they are a single value */
462 if (l_iv->min == l_iv->max && r_iv->min == r_iv->max)
463 return tarval_cmp(l_iv->min, r_iv->min) == pn_Cmp_Eq ? tv_true : tv_false;
465 /* if both intervals do not intersect, it is never equal */
466 res = tarval_cmp(l_iv->max, r_iv->min);
468 /* b < c ==> [a,b] != [c,d] */
469 if (res == pn_Cmp_Lt)
472 /* b <= c ==> [a,b) != [c,d] AND [a,b] != (c,d] */
473 if ((l_iv->flags & MAX_EXCLUDED || r_iv->flags & MIN_EXCLUDED)
474 && (res == pn_Cmp_Eq))
477 res = tarval_cmp(r_iv->max, l_iv->min);
479 /* d < a ==> [c,d] != [a,b] */
480 if (res == pn_Cmp_Lt)
483 /* d <= a ==> [c,d) != [a,b] AND [c,d] != (a,b] */
484 if ((r_iv->flags & MAX_EXCLUDED || l_iv->flags & MIN_EXCLUDED)
485 && (res == pn_Cmp_Eq))
490 /* two intervals can be compared for not equality only if they are a single value */
491 if (l_iv->min == l_iv->max && r_iv->min == r_iv->max)
492 return tarval_cmp(l_iv->min, r_iv->min) != pn_Cmp_Eq ? tv_true : tv_false;
496 res = tarval_cmp(l_iv->max, r_iv->min);
498 /* [a, b] < [c, d] <==> b < c */
499 if (res == pn_Cmp_Lt)
502 /* if one border is excluded, b <= c is enough */
503 if ((l_iv->flags & MAX_EXCLUDED || r_iv->flags & MIN_EXCLUDED) &&
507 /* [a, b] >= [c, d] <==> a > d */
508 res = tarval_cmp(l_iv->min, r_iv->max);
509 if (res == pn_Cmp_Gt)
512 /* if one border is excluded, a >= d is enough */
513 if ((l_iv->flags & MIN_EXCLUDED || r_iv->flags & MAX_EXCLUDED) &&
519 /* [a, b) <= [c, d] or [a, b] <= (c, d] <==> b <= c */
520 flags = (l_iv->flags & MAX_EXCLUDED) | (r_iv->flags & MIN_EXCLUDED);
522 res = tarval_cmp(l_iv->max, r_iv->min);
524 if (res == pn_Cmp_Lt || res == pn_Cmp_Eq)
528 res = tarval_cmp(l_iv->min, r_iv->max);
530 /* [a, b] > [c, d] <==> a > d */
531 if (res == pn_Cmp_Gt)
534 /* if one border is excluded, a >= d is enough */
535 if ((l_iv->flags & MIN_EXCLUDED || r_iv->flags & MAX_EXCLUDED) &&
541 /* Hmm. if both are intervals, we can find an order */
551 * Returns non-zero, if a given relation is transitive.
553 static int is_transitive(pn_Cmp pnc) {
554 return (pn_Cmp_False < pnc && pnc < pn_Cmp_Lg);
559 * Return the value of a Cmp if one or both predecessors
562 * @param cmp the Cmp node
563 * @param left the left operand of the Cmp
564 * @param right the right operand of the Cmp
565 * @param pnc the compare relation
567 tarval *computed_value_Cmp_Confirm(ir_node *cmp, ir_node *left, ir_node *right, pn_Cmp pnc)
570 pn_Cmp l_pnc, res_pnc, neg_pnc;
571 interval_t l_iv, r_iv;
575 if (get_irn_op(right) == op_Confirm) {
578 /* we want the Confirm on the left side */
583 pnc = get_inversed_pnc(pnc);
585 else if (get_irn_op(left) != op_Confirm) {
586 /* no Confirm on either one side, finish */
590 /* ok, here at least left is a Confirm, right might be */
591 l_bound = get_Confirm_bound(left);
592 l_pnc = get_Confirm_cmp(left);
594 if (get_irn_op(right) == op_Confirm) {
596 * both sides are Confirm's. Check some rare cases first.
598 ir_node *r_bound = get_Confirm_bound(right);
599 pn_Cmp r_pnc = get_Confirm_cmp(right);
602 * some check can be made WITHOUT constant bounds
604 if (r_bound == l_bound) {
605 if (is_transitive(l_pnc)) {
606 pn_Cmp r_inc_pnc = get_inversed_pnc(r_pnc);
609 * triangle inequality:
611 * a CMP B && B CMP b => a CMP b, !(a ~CMP b)
613 * We handle correctly cases with some <=/>= here
615 if ((l_pnc & ~pn_Cmp_Eq) == (r_inc_pnc & ~pn_Cmp_Eq)) {
616 res_pnc = (l_pnc & ~pn_Cmp_Eq) | (l_pnc & r_inc_pnc & pn_Cmp_Eq);
618 if ((pnc == res_pnc) || ((pnc & ~pn_Cmp_Eq) == res_pnc)) {
619 DBG_OUT_TR(l_pnc, l_bound, r_pnc, r_bound, pnc, "true");
620 DBG_EVAL_CONFIRM(cmp);
621 return tarval_b_true;
624 pn_Cmp neg_pnc = get_negated_pnc(pnc, get_irn_mode(left));
626 if ((neg_pnc == res_pnc) || ((neg_pnc & ~pn_Cmp_Eq) == res_pnc)) {
627 DBG_OUT_TR(l_pnc, l_bound, r_pnc, r_bound, pnc, "false");
628 DBG_EVAL_CONFIRM(cmp);
629 return tarval_b_false;
637 * Here, we check only the right Confirm, as the left Confirms are
638 * checked later anyway.
641 if (left == r_bound) {
643 * l == bound(r) AND pnc(r) == pnc:
645 * We know that a CMP b and check for that
647 if ((r_pnc == pnc) || (r_pnc == (pnc & ~pn_Cmp_Eq))) {
648 DBG_OUT_R(r_pnc, r_bound, left, pnc, right, "true");
649 DBG_EVAL_CONFIRM(cmp);
650 return tarval_b_true;
653 * l == bound(r) AND pnc(r) != pnc:
655 * We know that a CMP b and check for a ~CMP b
658 mode = get_irn_mode(left);
659 neg_pnc = get_negated_pnc(pnc, mode);
661 if ((r_pnc == neg_pnc) || (r_pnc == (neg_pnc & ~pn_Cmp_Eq))) {
662 DBG_OUT_R(r_pnc, r_bound, left, pnc, right, "false");
663 DBG_EVAL_CONFIRM(cmp);
664 return tarval_b_false;
669 /* now, try interval magic */
671 get_interval(&l_iv, l_bound, l_pnc),
672 get_interval(&r_iv, r_bound, r_pnc),
675 if (tv != tarval_bad) {
676 DBG_EVAL_CONFIRM(cmp);
681 /* from Here, check only left Confirm */
684 * some checks can be made WITHOUT constant bounds
686 if (right == l_bound) {
688 * r == bound(l) AND pnc(l) == pnc:
690 * We know that a CMP b and check for that
692 if ((l_pnc == pnc) || (l_pnc == (pnc & ~pn_Cmp_Eq))) {
693 DBG_OUT_L(l_pnc, l_bound, left, pnc, right, "true");
694 DBG_EVAL_CONFIRM(cmp);
695 return tarval_b_true;
698 * r == bound(l) AND pnc(l) is Not(pnc):
700 * We know that a CMP b and check for a ~CMP b
703 mode = get_irn_mode(left);
704 neg_pnc = get_negated_pnc(pnc, mode);
706 if ((l_pnc == neg_pnc) || (l_pnc == (neg_pnc & ~pn_Cmp_Eq))) {
707 DBG_OUT_L(l_pnc, l_bound, left, pnc, right, "false");
708 DBG_EVAL_CONFIRM(cmp);
709 return tarval_b_false;
714 /* now, only right == Const can help */
715 tv = value_of(right);
717 if (tv != tarval_bad) {
719 get_interval(&l_iv, l_bound, l_pnc),
720 get_interval_from_tv(&r_iv, tv),
724 if (tv != tarval_bad)
725 DBG_EVAL_CONFIRM(cmp);
732 * For debugging. Prints an interval into a string.
734 * @param buf address of a string buffer
735 * @param len length of the string buffer
736 * @param iv the interval
738 static int iv_snprintf(char *buf, size_t len, const interval_t *iv) {
739 char smin[64], smax[64];
742 tarval_snprintf(smin, sizeof(smin), iv->min);
744 if (iv->min != iv->max || (iv->flags & (MIN_EXCLUDED|MAX_EXCLUDED))) {
745 tarval_snprintf(smax, sizeof(smax), iv->max);
747 return snprintf(buf, len, "%c%s, %s%c",
748 iv->flags & MIN_EXCLUDED ? '(' : '[',
750 iv->flags & MAX_EXCLUDED ? ')' : ']'
754 return snprintf(buf, len, "%s", smin);
756 return snprintf(buf, len, "<UNKNOWN>");
760 * For debugging. Prints an interval compare.
762 * @param l_iv the left interval
763 * @param r_iv the right interval
764 * @param pnc the compare relation
766 static void print_iv_cmp(const interval_t *l_iv, const interval_t *r_iv, pn_Cmp pnc)
768 char sl[128], sr[128];
770 iv_snprintf(sl, sizeof(sl), l_iv);
771 iv_snprintf(sr, sizeof(sr), r_iv);
773 ir_printf("%s %= %s", sl, pnc, sr);
777 * For debugging. call *compare_iv() and prints inputs and result.
779 * @param l_iv the left interval
780 * @param r_iv the right interval
781 * @param pnc the compare relation
783 static tarval *compare_iv_dbg(const interval_t *l_iv, const interval_t *r_iv, pn_Cmp pnc)
785 tarval *tv = (compare_iv)(l_iv, r_iv, pnc);
787 if (tv == tarval_bad)
790 ir_printf("In %e:\n", get_irg_entity(current_ir_graph));
791 print_iv_cmp(l_iv, r_iv, pnc);
792 ir_printf(" = %T\n", tv);
796 #endif /* DEBUG_CONFIRM */