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 beeing 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 can be confirmed >= 0 or <= 0,
148 * If the mode of the value did not honor signed zeros, else
149 * check for >= 0 or < 0.
151 value_classify classify_value_sign(ir_node *n)
157 if (get_irn_op(n) != op_Confirm)
158 return VALUE_UNKNOWN;
160 tv = value_of(get_Confirm_bound(n));
161 if (tv == tarval_bad)
162 return VALUE_UNKNOWN;
164 mode = get_irn_mode(n);
167 * We can handle only >=, >, <, <= cases.
168 * We could handle == too, but this will be optimized into
171 * Note that for integer modes we have a slightly better
172 * optimization possibilities, so we handle this
175 cmp = get_Confirm_cmp(n);
180 * must be x < c <= 1 to be useful if integer mode and -0 = 0
181 * x < c <= 0 to be useful else
185 * must be x <= c < 1 to be useful if integer mode and -0 = 0
186 * x <= c < 0 to be useful else
188 c = mode_is_int(mode) && mode_honor_signed_zeros(mode) ?
189 get_mode_one(mode) : get_mode_null(mode);
191 ncmp = tarval_cmp(tv, c);
192 if (ncmp == pn_Cmp_Eq)
195 if (cmp != (ncmp ^ pn_Cmp_Eq))
196 return VALUE_UNKNOWN;
199 return VALUE_NEGATIVE;
203 * must be x >= c > -1 to be useful if integer mode
204 * x >= c >= 0 to be useful else
208 * must be x > c >= -1 to be useful if integer mode
209 * x > c >= 0 to be useful else
211 if (mode_is_int(mode)) {
212 c = get_mode_minus_one(mode);
214 ncmp = tarval_cmp(tv, c);
215 if (ncmp == pn_Cmp_Eq)
218 if (cmp != (ncmp ^ pn_Cmp_Eq))
219 return VALUE_UNKNOWN;
222 c = get_mode_minus_one(mode);
224 ncmp = tarval_cmp(tv, c);
226 if (ncmp != pn_Cmp_Eq && ncmp != pn_Cmp_Gt)
227 return VALUE_UNKNOWN;
231 return VALUE_POSITIVE;
234 return VALUE_UNKNOWN;
239 * construct an interval from a value
241 * @return the filled interval or NULL if no interval
242 * can be created (happens only on floating point
244 static interval_t *get_interval_from_tv(interval_t *iv, tarval *tv)
246 ir_mode *mode = get_tarval_mode(tv);
248 if (tv == tarval_bad) {
249 if (mode_is_float(mode)) {
250 /* NaN could be included which we cannot handle */
251 iv->min = tarval_bad;
252 iv->max = tarval_bad;
253 iv->flags = MIN_EXCLUDED | MAX_EXCLUDED;
258 iv->min = get_mode_min(mode);
259 iv->max = get_mode_max(mode);
260 iv->flags = MIN_INCLUDED | MAX_INCLUDED;
265 if (mode_is_float(mode)) {
266 if (tv == get_mode_NAN(mode)) {
267 /* arg, we cannot handle NaN's. */
268 iv->min = tarval_bad;
269 iv->max = tarval_bad;
270 iv->flags = MIN_EXCLUDED | MAX_EXCLUDED;
278 iv->flags = MIN_INCLUDED | MAX_INCLUDED;
284 * construct an interval from a Confirm
286 * @param iv an empty interval, will be filled
287 * @param bound the bound value
288 * @param pnc the Confirm pnc relation
290 * @return the filled interval or NULL if no interval
291 * can be created (happens only on floating point
293 static interval_t *get_interval(interval_t *iv, ir_node *bound, pn_Cmp pnc)
295 ir_mode *mode = get_irn_mode(bound);
296 tarval *tv = value_of(bound);
298 if (tv == tarval_bad) {
299 /* There is nothing we could do here. For integer
300 * modes we could return [-oo, +oo], but there is
301 * nothing we could deduct from such an interval.
302 * So, speed things up and return unknown.
304 iv->min = tarval_bad;
305 iv->max = tarval_bad;
306 iv->flags = MIN_EXCLUDED | MAX_EXCLUDED;
310 if (mode_is_float(mode)) {
311 if (tv == get_mode_NAN(mode)) {
312 /* arg, we cannot handle NaN's. */
313 iv->min = tarval_bad;
314 iv->max = tarval_bad;
315 iv->flags = MIN_EXCLUDED | MAX_EXCLUDED;
321 /* check which side is known */
327 iv->flags = MIN_INCLUDED | MAX_INCLUDED;
332 iv->min = get_mode_min(mode);
334 iv->flags = MIN_INCLUDED | MAX_INCLUDED;
339 iv->min = get_mode_min(mode);
341 iv->flags = MIN_INCLUDED | MAX_EXCLUDED;
347 iv->max = get_mode_max(mode);
348 iv->flags = MIN_EXCLUDED | MAX_INCLUDED;
354 iv->max = get_mode_max(mode);
355 iv->flags = MIN_INCLUDED | MAX_INCLUDED;
360 * Ordered means, that at least neither
361 * our bound nor our value ara NaN's
364 iv->min = get_mode_min(mode);
365 iv->max = get_mode_max(mode);
366 iv->flags = MIN_INCLUDED | MAX_INCLUDED;
371 * We do not handle UNORDERED, as a NaN
372 * could be included in the interval.
374 iv->min = tarval_bad;
375 iv->max = tarval_bad;
376 iv->flags = MIN_EXCLUDED | MAX_EXCLUDED;
380 if (iv->min != tarval_bad && iv->max != tarval_bad)
386 * Try to evaluate l_iv pnc r_iv.
388 * @param l_iv the left interval
389 * @param r_iv the right interval
390 * @param pnc the compare relation
393 * tarval_b_true or tarval_b_false it it can be evaluated,
396 static tarval *(compare_iv)(const interval_t *l_iv, const interval_t *r_iv, pn_Cmp pnc)
400 tarval *tv_true = tarval_b_true, *tv_false = tarval_b_false;
402 /* if one interval contains NaNs, we cannot evaluate anything */
403 if (! l_iv || ! r_iv)
406 /* we can only check ordered relations */
407 if (pnc & pn_Cmp_Uo) {
410 pnc = get_negated_pnc(pnc, get_tarval_mode(l_iv->min));
416 /* if we have > or >=, we do the inverse to save some cases */
417 if (pnc == pn_Cmp_Ge || pnc == pn_Cmp_Gt) {
420 pnc = get_inversed_pnc(pnc);
426 /* now, only the following cases remains */
429 /* two intervals can be compared for equality only if they are a single value */
430 if (l_iv->min == l_iv->max && r_iv->min == r_iv->max)
431 return tarval_cmp(l_iv->min, r_iv->min) == pn_Cmp_Eq ? tv_true : tv_false;
433 /* if both interval do not intersect, it is never equal */
434 res = tarval_cmp(l_iv->max, r_iv->min);
436 /* b < c ==> [a,b] != [c,d] */
437 if (res == pn_Cmp_Lt)
440 /* b <= c ==> [a,b) != [c,d] AND [a,b] != (c,d] */
441 if ((l_iv->flags & MAX_EXCLUDED || r_iv->flags & MIN_EXCLUDED)
442 && (res == pn_Cmp_Eq))
445 res = tarval_cmp(r_iv->max, l_iv->min);
447 /* d < a ==> [c,d] != [a,b] */
448 if (res == pn_Cmp_Lt)
451 /* d <= a ==> [c,d) != [a,b] AND [c,d] != (a,b] */
452 if ((r_iv->flags & MAX_EXCLUDED || l_iv->flags & MIN_EXCLUDED)
453 && (res == pn_Cmp_Eq))
458 /* two intervals can be compared for not equality only if they are a single value */
459 if (l_iv->min == l_iv->max && r_iv->min == r_iv->max)
460 return tarval_cmp(l_iv->min, r_iv->min) != pn_Cmp_Eq ? tv_true : tv_false;
464 res = tarval_cmp(l_iv->max, r_iv->min);
466 /* [a, b] < [c, d] <==> b < c */
467 if (res == pn_Cmp_Lt)
470 /* if one border is excluded, b <= c is enough */
471 if ((l_iv->flags & MAX_EXCLUDED || r_iv->flags & MIN_EXCLUDED) &&
475 /* [a, b] >= [c, d] <==> a > d */
476 res = tarval_cmp(l_iv->min, r_iv->max);
477 if (res == pn_Cmp_Gt)
480 /* if one border is excluded, a >= d is enough */
481 if ((l_iv->flags & MIN_EXCLUDED || r_iv->flags & MAX_EXCLUDED) &&
487 /* [a, b) <= [c, d] or [a, b] <= (c, d] <==> b <= c */
488 flags = (l_iv->flags & MAX_EXCLUDED) | (r_iv->flags & MIN_EXCLUDED);
490 res = tarval_cmp(l_iv->max, r_iv->min);
492 if (res == pn_Cmp_Lt || res == pn_Cmp_Eq)
496 res = tarval_cmp(l_iv->min, r_iv->max);
498 /* [a, b] > [c, d] <==> a > d */
499 if (res == pn_Cmp_Gt)
502 /* if one border is excluded, a >= d is enough */
503 if ((l_iv->flags & MIN_EXCLUDED || r_iv->flags & MAX_EXCLUDED) &&
509 /* Hmm. if both are intervals, we can find an order */
519 * Returns non-zero, if a given relation is transitive.
521 static int is_transitive(pn_Cmp pnc) {
522 return (pn_Cmp_False < pnc && pnc < pn_Cmp_Lg);
527 * Return the value of a Cmp if one or both predecessors
530 * @param left the left operand of the Cmp
531 * @param right the right operand of the Cmp
533 tarval *computed_value_Cmp_Confirm(ir_node *cmp, ir_node *left, ir_node *right, pn_Cmp pnc)
536 pn_Cmp l_pnc, res_pnc, neg_pnc;
537 interval_t l_iv, r_iv;
541 if (get_irn_op(right) == op_Confirm) {
544 /* we want the Confirm on the left side */
549 pnc = get_inversed_pnc(pnc);
551 else if (get_irn_op(left) != op_Confirm) {
552 /* no Confirm on either one side, finish */
556 /* ok, here at least left is a Confirm, right might be */
557 l_bound = get_Confirm_bound(left);
558 l_pnc = get_Confirm_cmp(left);
560 if (get_irn_op(right) == op_Confirm) {
562 * both sides are Confirm's. Check some rare cases first.
564 ir_node *r_bound = get_Confirm_bound(right);
565 pn_Cmp r_pnc = get_Confirm_cmp(right);
568 * some check can be made WITHOUT constant bounds
570 if (r_bound == l_bound) {
571 if (is_transitive(l_pnc)) {
572 pn_Cmp r_inc_pnc = get_inversed_pnc(r_pnc);
575 * triangle inequality:
577 * a CMP B && B CMP b => a CMP b, !(a ~CMP b)
579 * We handle correctly cases with some <=/>= here
581 if ((l_pnc & ~pn_Cmp_Eq) == (r_inc_pnc & ~pn_Cmp_Eq)) {
582 res_pnc = (l_pnc & ~pn_Cmp_Eq) | (l_pnc & r_inc_pnc & pn_Cmp_Eq);
584 if ((pnc == res_pnc) || ((pnc & ~pn_Cmp_Eq) == res_pnc)) {
585 DBG_OUT_TR(l_pnc, l_bound, r_pnc, r_bound, pnc, "true");
586 DBG_EVAL_CONFIRM(cmp);
587 return tarval_b_true;
590 pn_Cmp neg_pnc = get_negated_pnc(pnc, get_irn_mode(left));
592 if ((neg_pnc == res_pnc) || ((neg_pnc & ~pn_Cmp_Eq) == res_pnc)) {
593 DBG_OUT_TR(l_pnc, l_bound, r_pnc, r_bound, pnc, "false");
594 DBG_EVAL_CONFIRM(cmp);
595 return tarval_b_false;
603 * Here, we check only the right Confirm, as the left Confirms are
604 * checked later anyway.
607 if (left == r_bound) {
609 * l == bound(r) AND pnc(r) == pnc:
611 * We know that a CMP b and check for that
613 if ((r_pnc == pnc) || (r_pnc == (pnc & ~pn_Cmp_Eq))) {
614 DBG_OUT_R(r_pnc, r_bound, left, pnc, right, "true");
615 DBG_EVAL_CONFIRM(cmp);
616 return tarval_b_true;
619 * l == bound(r) AND pnc(r) != pnc:
621 * We know that a CMP b and check for a ~CMP b
624 mode = get_irn_mode(left);
625 neg_pnc = get_negated_pnc(pnc, mode);
627 if ((r_pnc == neg_pnc) || (r_pnc == (neg_pnc & ~pn_Cmp_Eq))) {
628 DBG_OUT_R(r_pnc, r_bound, left, pnc, right, "false");
629 DBG_EVAL_CONFIRM(cmp);
630 return tarval_b_false;
635 /* now, try interval magic */
637 get_interval(&l_iv, l_bound, l_pnc),
638 get_interval(&r_iv, r_bound, r_pnc),
641 if (tv != tarval_bad) {
642 DBG_EVAL_CONFIRM(cmp);
647 /* from Here, check only left Confirm */
650 * some checks can be made WITHOUT constant bounds
652 if (right == l_bound) {
654 * r == bound(l) AND pnc(l) == pnc:
656 * We know that a CMP b and check for that
658 if ((l_pnc == pnc) || (l_pnc == (pnc & ~pn_Cmp_Eq))) {
659 DBG_OUT_L(l_pnc, l_bound, left, pnc, right, "true");
660 DBG_EVAL_CONFIRM(cmp);
661 return tarval_b_true;
664 * r == bound(l) AND pnc(l) is Not(pnc):
666 * We know that a CMP b and check for a ~CMP b
669 mode = get_irn_mode(left);
670 neg_pnc = get_negated_pnc(pnc, mode);
672 if ((l_pnc == neg_pnc) || (l_pnc == (neg_pnc & ~pn_Cmp_Eq))) {
673 DBG_OUT_L(l_pnc, l_bound, left, pnc, right, "false");
674 DBG_EVAL_CONFIRM(cmp);
675 return tarval_b_false;
680 /* now, only right == Const can help */
681 tv = value_of(right);
683 if (tv != tarval_bad) {
685 get_interval(&l_iv, l_bound, l_pnc),
686 get_interval_from_tv(&r_iv, tv),
690 if (tv != tarval_bad)
691 DBG_EVAL_CONFIRM(cmp);
698 * For debugging. Prints an interval into a string.
700 static int iv_snprintf(char *buf, size_t len, const interval_t *iv) {
701 char smin[64], smax[64];
704 tarval_snprintf(smin, sizeof(smin), iv->min);
706 if (iv->min != iv->max || (iv->flags & (MIN_EXCLUDED|MAX_EXCLUDED))) {
707 tarval_snprintf(smax, sizeof(smax), iv->max);
709 return snprintf(buf, len, "%c%s, %s%c",
710 iv->flags & MIN_EXCLUDED ? '(' : '[',
712 iv->flags & MAX_EXCLUDED ? ')' : ']'
716 return snprintf(buf, len, "%s", smin);
718 return snprintf(buf, len, "<UNKNOWN>");
722 * For debugging. Prints an interval compare
724 static void print_iv_cmp(const interval_t *l_iv, const interval_t *r_iv, pn_Cmp pnc)
726 char sl[128], sr[128];
728 iv_snprintf(sl, sizeof(sl), l_iv);
729 iv_snprintf(sr, sizeof(sr), r_iv);
731 ir_printf("%s %= %s", sl, pnc, sr);
735 * For debugging. call *compare_iv() and prints inputs and result
737 static tarval *compare_iv_dbg(const interval_t *l_iv, const interval_t *r_iv, pn_Cmp pnc)
739 tarval *tv = (compare_iv)(l_iv, r_iv, pnc);
741 if (tv == tarval_bad)
744 ir_printf("In %e:\n", get_irg_entity(current_ir_graph));
745 print_iv_cmp(l_iv, r_iv, pnc);
746 ir_printf(" = %T\n", tv);
750 #endif /* DEBUG_CONFIRM */