2 * Copyright (C) 1995-2008 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
32 #include "iropt_dbg.h"
33 #include "iroptimize.h"
38 MIN_INCLUDED = 0x00, /**< [min, ... */
39 MAX_INCLUDED = 0x00, /**< ..., max] */
40 MIN_EXCLUDED = 0x01, /**< (min, ... */
41 MAX_EXCLUDED = 0x02 /**< ..., max) */
45 * An interval. We could use
46 * intervals that ALWAYS include its borders, even for
47 * floating point, as the precision is limited.
48 * However, as our tarval module did not support
49 * such kind of operation, we use border flags allowing
52 typedef struct interval_t {
53 ir_tarval *min; /**< lowest border */
54 ir_tarval *max; /**< highest border */
55 unsigned char flags; /**< border flags */
60 #define compare_iv(l_iv, r_iv, relation) compare_iv_dbg(l_iv, r_iv, relation)
63 static tarval *compare_iv_dbg(const interval_t *l_iv, const interval_t *r_iv, ir_relation relation);
66 #define DBG_OUT_TR(l_relation, l_bound, r_relation, r_bound, relation, v) \
67 ir_printf("In %e:\na %= %n && b %= %n ==> a %= b == %s\n", \
68 get_irg_entity(current_ir_graph), \
69 l_relation, l_bound, r_relation, r_bound, relation, v)
72 #define DBG_OUT_R(r_relation, r_bound, left, relation, right, v) \
73 ir_printf("In %e:\na %= %n ==> %n %= %n == %s\n", \
74 get_irg_entity(current_ir_graph), \
75 r_relation, r_bound, left, relation, right, v)
78 #define DBG_OUT_L(l_relation, l_bound, left, relation, right, v) \
79 ir_printf("In %e:\na %= %n ==> %n %= %n == %s\n", \
80 get_irg_entity(current_ir_graph), \
81 l_relation, l_bound, left, relation, right, v)
85 #define DBG_OUT_TR(l_relation, l_bound, r_relation, r_bound, relation, v) (void)0
86 #define DBG_OUT_R(r_relation, r_bound, left, relation, right, v) (void)0
87 #define DBG_OUT_L(l_relation, l_bound, left, relation, right, v) (void)0
89 #endif /* DEBUG_CONFIRM */
92 * Check, if the value of a node is != 0.
94 * This is a often needed case, so we handle here Confirm
97 int value_not_zero(const ir_node *n, const ir_node **confirm)
99 #define RET_ON(x) if (x) { *confirm = n; return 1; } break
102 ir_mode *mode = get_irn_mode(n);
103 ir_relation relation;
107 /* there might be several Confirms one after other that form an interval */
110 /* we can safely skip Minus when checking for != 0 */
118 * Note: A Confirm is never after a Const. So,
119 * we simply can check the bound for being a Const
120 * without the fear that is might be hidden by a further Confirm.
122 tv = value_of(get_Confirm_bound(n));
123 if (tv == tarval_bad) {
124 n = get_Confirm_value(n);
128 relation = tarval_cmp(tv, get_mode_null(mode));
131 * Beware: C might by a NaN. It is not clear, what we should do
132 * than. Of course a NaN is != 0, but we might use this function
133 * to remove up Exceptions, and NaN's might generate Exception.
134 * So, we do NOT handle NaNs here for safety.
136 * Note that only the C != 0 case need additional checking.
138 switch (get_Confirm_relation(n)) {
139 case ir_relation_equal: /* n == C /\ C != 0 ==> n != 0 */
140 RET_ON(relation != ir_relation_equal && relation != ir_relation_unordered);
141 case ir_relation_less_greater: /* n != C /\ C == 0 ==> n != 0 */
142 RET_ON(relation == ir_relation_equal);
143 case ir_relation_less: /* n < C /\ C <= 0 ==> n != 0 */
144 RET_ON(relation == ir_relation_less || relation == ir_relation_equal);
145 case ir_relation_less_equal: /* n <= C /\ C < 0 ==> n != 0 */
146 RET_ON(relation == ir_relation_less);
147 case ir_relation_greater_equal: /* n >= C /\ C > 0 ==> n != 0 */
148 RET_ON(relation == ir_relation_greater);
149 case ir_relation_greater: /* n > C /\ C >= 0 ==> n != 0 */
150 RET_ON(relation == ir_relation_greater || relation == ir_relation_equal);
154 n = get_Confirm_value(n);
156 /* global entities are never NULL */
157 if (is_SymConst_addr_ent(n))
161 if (tv == tarval_bad)
164 relation = tarval_cmp(tv, get_mode_null(mode));
166 /* again, need check for NaN */
167 return (relation != ir_relation_equal) && (relation != ir_relation_unordered);
173 * Check, if the value of a node cannot represent a NULL pointer.
176 * - A SymConst(entity) is NEVER a NULL pointer
177 * - Confirms are evaluated
179 int value_not_null(const ir_node *n, const ir_node **confirm)
186 if (tarval_is_constant(tv) && ! tarval_is_null(tv))
189 assert(mode_is_reference(get_irn_mode(n)));
190 /* skip all Sel nodes */
195 if (is_Proj(n)) { n = get_Proj_pred(n); continue; }
199 if (is_SymConst_addr_ent(n)) {
200 /* global references are never NULL */
202 } else if (n == get_irg_frame(get_irn_irg(n))) {
203 /* local references are never NULL */
205 } else if (is_Alloc(n)) {
206 /* alloc never returns NULL (it throws an exception instead) */
209 /* check for more Confirms */
210 for (; is_Confirm(n); n = get_Confirm_value(n)) {
211 if (get_Confirm_relation(n) == ir_relation_less_greater) {
212 ir_node *bound = get_Confirm_bound(n);
213 ir_tarval *tv = value_of(bound);
215 if (tarval_is_null(tv)) {
227 static inline ir_value_classify_sign operator *(ir_value_classify_sign sign, int mul) {
228 return (ir_value_classify_sign) (sign*mul);
234 * Check, if the value of a node can be confirmed >= 0 or <= 0,
235 * If the mode of the value did not honor signed zeros, else
236 * check for >= 0 or < 0.
238 ir_value_classify_sign classify_value_sign(ir_node *n)
242 ir_relation cmp, ncmp;
246 unsigned code = get_irn_opcode(n);
256 return value_classified_unknown;
261 return value_classified_unknown;
263 tv = value_of(get_Confirm_bound(n));
264 if (tv == tarval_bad)
265 return value_classified_unknown;
267 mode = get_irn_mode(n);
270 * We can handle only >=, >, <, <= cases.
271 * We could handle == too, but this will be optimized into
274 * Note that for integer modes we have a slightly better
275 * optimization possibilities, so we handle this
278 cmp = get_Confirm_relation(n);
281 case ir_relation_less:
283 * must be x < c <= 1 to be useful if integer mode and -0 = 0
284 * x < c <= 0 to be useful else
286 case ir_relation_less_equal:
288 * must be x <= c < 1 to be useful if integer mode and -0 = 0
289 * x <= c < 0 to be useful else
291 c = mode_is_int(mode) && mode_honor_signed_zeros(mode) ?
292 get_mode_one(mode) : get_mode_null(mode);
294 ncmp = tarval_cmp(tv, c);
295 if (ncmp == ir_relation_equal)
296 ncmp = ir_relation_less_equal;
298 if (cmp != (ncmp ^ ir_relation_equal))
299 return value_classified_unknown;
302 return value_classified_negative * negate;
304 case ir_relation_greater_equal:
306 * must be x >= c > -1 to be useful if integer mode
307 * x >= c >= 0 to be useful else
309 case ir_relation_greater:
311 * must be x > c >= -1 to be useful if integer mode
312 * x > c >= 0 to be useful else
314 if (mode_is_int(mode)) {
315 c = get_mode_minus_one(mode);
317 ncmp = tarval_cmp(tv, c);
318 if (ncmp == ir_relation_equal)
319 ncmp = ir_relation_greater_equal;
321 if (cmp != (ncmp ^ ir_relation_equal))
322 return value_classified_unknown;
324 c = get_mode_minus_one(mode);
326 ncmp = tarval_cmp(tv, c);
328 if (ncmp != ir_relation_equal && ncmp != ir_relation_greater)
329 return value_classified_unknown;
333 return value_classified_positive * negate;
336 return value_classified_unknown;
341 * construct an interval from a value
343 * @return the filled interval or NULL if no interval
344 * can be created (happens only on floating point
346 static interval_t *get_interval_from_tv(interval_t *iv, ir_tarval *tv)
348 ir_mode *mode = get_tarval_mode(tv);
350 if (tv == tarval_bad) {
351 if (mode_is_float(mode)) {
352 /* NaN could be included which we cannot handle */
353 iv->min = tarval_bad;
354 iv->max = tarval_bad;
355 iv->flags = MIN_EXCLUDED | MAX_EXCLUDED;
359 iv->min = get_mode_min(mode);
360 iv->max = get_mode_max(mode);
361 iv->flags = MIN_INCLUDED | MAX_INCLUDED;
366 if (mode_is_float(mode)) {
367 if (tv == get_mode_NAN(mode)) {
368 /* arg, we cannot handle NaN's. */
369 iv->min = tarval_bad;
370 iv->max = tarval_bad;
371 iv->flags = MIN_EXCLUDED | MAX_EXCLUDED;
379 iv->flags = MIN_INCLUDED | MAX_INCLUDED;
385 * construct an interval from a Confirm
387 * @param iv an empty interval, will be filled
388 * @param bound the bound value
389 * @param relation the Confirm compare relation
391 * @return the filled interval or NULL if no interval
392 * can be created (happens only on floating point
394 static interval_t *get_interval(interval_t *iv, ir_node *bound, ir_relation relation)
396 ir_mode *mode = get_irn_mode(bound);
397 ir_tarval *tv = value_of(bound);
399 if (tv == tarval_bad) {
400 /* There is nothing we could do here. For integer
401 * modes we could return [-oo, +oo], but there is
402 * nothing we could deduct from such an interval.
403 * So, speed things up and return unknown.
405 iv->min = tarval_bad;
406 iv->max = tarval_bad;
407 iv->flags = MIN_EXCLUDED | MAX_EXCLUDED;
411 if (mode_is_float(mode)) {
412 if (tv == get_mode_NAN(mode)) {
413 /* arg, we cannot handle NaN's. */
414 iv->min = tarval_bad;
415 iv->max = tarval_bad;
416 iv->flags = MIN_EXCLUDED | MAX_EXCLUDED;
422 /* check which side is known */
424 case ir_relation_equal:
428 iv->flags = MIN_INCLUDED | MAX_INCLUDED;
431 case ir_relation_less_equal:
433 iv->min = get_mode_min(mode);
435 iv->flags = MIN_INCLUDED | MAX_INCLUDED;
438 case ir_relation_less:
440 iv->min = get_mode_min(mode);
442 iv->flags = MIN_INCLUDED | MAX_EXCLUDED;
445 case ir_relation_greater:
448 iv->max = get_mode_max(mode);
449 iv->flags = MIN_EXCLUDED | MAX_INCLUDED;
452 case ir_relation_greater_equal:
455 iv->max = get_mode_max(mode);
456 iv->flags = MIN_INCLUDED | MAX_INCLUDED;
459 case ir_relation_less_equal_greater:
461 * Ordered means, that at least neither
462 * our bound nor our value ara NaN's
465 iv->min = get_mode_min(mode);
466 iv->max = get_mode_max(mode);
467 iv->flags = MIN_INCLUDED | MAX_INCLUDED;
472 * We do not handle UNORDERED, as a NaN
473 * could be included in the interval.
475 iv->min = tarval_bad;
476 iv->max = tarval_bad;
477 iv->flags = MIN_EXCLUDED | MAX_EXCLUDED;
481 if (iv->min != tarval_bad && iv->max != tarval_bad)
487 * Try to evaluate l_iv relation r_iv.
489 * @param l_iv the left interval
490 * @param r_iv the right interval
491 * @param relation the compare relation
494 * tarval_b_true or tarval_b_false it it can be evaluated,
497 static ir_tarval *(compare_iv)(const interval_t *l_iv, const interval_t *r_iv, ir_relation relation)
501 ir_tarval *tv_true = tarval_b_true, *tv_false = tarval_b_false;
503 /* if one interval contains NaNs, we cannot evaluate anything */
504 if (! l_iv || ! r_iv)
507 /* we can only check ordered relations */
508 if (relation & ir_relation_unordered) {
511 relation = get_negated_relation(relation);
517 /* if we have > or >=, we do the inverse to save some cases */
518 if (relation == ir_relation_greater_equal || relation == ir_relation_greater) {
521 relation = get_inversed_relation(relation);
527 /* now, only the following cases remains */
529 case ir_relation_equal:
530 /* two intervals can be compared for equality only if they are a single value */
531 if (l_iv->min == l_iv->max && r_iv->min == r_iv->max)
532 return tarval_cmp(l_iv->min, r_iv->min) == ir_relation_equal ? tv_true : tv_false;
534 /* if both intervals do not intersect, it is never equal */
535 res = tarval_cmp(l_iv->max, r_iv->min);
537 /* b < c ==> [a,b] != [c,d] */
538 if (res == ir_relation_less)
541 /* b <= c ==> [a,b) != [c,d] AND [a,b] != (c,d] */
542 if ((l_iv->flags & MAX_EXCLUDED || r_iv->flags & MIN_EXCLUDED)
543 && (res == ir_relation_equal))
546 res = tarval_cmp(r_iv->max, l_iv->min);
548 /* d < a ==> [c,d] != [a,b] */
549 if (res == ir_relation_less)
552 /* d <= a ==> [c,d) != [a,b] AND [c,d] != (a,b] */
553 if ((r_iv->flags & MAX_EXCLUDED || l_iv->flags & MIN_EXCLUDED)
554 && (res == ir_relation_equal))
558 case ir_relation_less_greater:
559 /* two intervals can be compared for not equality only if they are a single value */
560 if (l_iv->min == l_iv->max && r_iv->min == r_iv->max)
561 return tarval_cmp(l_iv->min, r_iv->min) != ir_relation_equal ? tv_true : tv_false;
564 case ir_relation_less:
565 res = tarval_cmp(l_iv->max, r_iv->min);
567 /* [a, b] < [c, d] <==> b < c */
568 if (res == ir_relation_less)
571 /* if one border is excluded, b <= c is enough */
572 if ((l_iv->flags & MAX_EXCLUDED || r_iv->flags & MIN_EXCLUDED) &&
573 res == ir_relation_equal)
576 /* [a, b] >= [c, d] <==> a > d */
577 res = tarval_cmp(l_iv->min, r_iv->max);
578 if (res == ir_relation_greater)
581 /* if one border is excluded, a >= d is enough */
582 if ((l_iv->flags & MIN_EXCLUDED || r_iv->flags & MAX_EXCLUDED) &&
583 res == ir_relation_equal)
587 case ir_relation_less_equal:
588 /* [a, b) <= [c, d] or [a, b] <= (c, d] <==> b <= c */
589 flags = (l_iv->flags & MAX_EXCLUDED) | (r_iv->flags & MIN_EXCLUDED);
591 res = tarval_cmp(l_iv->max, r_iv->min);
593 if (res == ir_relation_less || res == ir_relation_equal)
597 res = tarval_cmp(l_iv->min, r_iv->max);
599 /* [a, b] > [c, d] <==> a > d */
600 if (res == ir_relation_greater)
603 /* if one border is excluded, a >= d is enough */
604 if ((l_iv->flags & MIN_EXCLUDED || r_iv->flags & MAX_EXCLUDED) &&
605 res == ir_relation_equal)
609 case ir_relation_less_equal_greater:
610 /* Hmm. if both are intervals, we can find an order */
620 * Returns non-zero, if a given relation is transitive.
622 static int is_transitive(ir_relation relation)
624 return (ir_relation_false < relation && relation < ir_relation_less_greater);
628 * Return the value of a Cmp if one or both predecessors
631 * @param cmp the Cmp node
632 * @param left the left operand of the Cmp
633 * @param right the right operand of the Cmp
634 * @param relation the compare relation
636 ir_tarval *computed_value_Cmp_Confirm(const ir_node *cmp, ir_node *left, ir_node *right, ir_relation relation)
639 ir_relation l_relation, res_relation, neg_relation;
640 interval_t l_iv, r_iv;
643 if (is_Confirm(right)) {
644 /* we want the Confirm on the left side */
649 relation = get_inversed_relation(relation);
650 } else if (! is_Confirm(left)) {
651 /* nothing more found */
653 goto check_null_case;
656 /* ok, here at least left is a Confirm, right might be */
657 l_bound = get_Confirm_bound(left);
658 l_relation = get_Confirm_relation(left);
660 if (is_Confirm(right)) {
662 * both sides are Confirm's. Check some rare cases first.
664 ir_node *r_bound = get_Confirm_bound(right);
665 ir_relation r_relation = get_Confirm_relation(right);
668 * some check can be made WITHOUT constant bounds
670 if (r_bound == l_bound) {
671 if (is_transitive(l_relation)) {
672 ir_relation r_inc_relation = get_inversed_relation(r_relation);
675 * triangle inequality:
677 * a CMP B && B CMP b => a CMP b, !(a ~CMP b)
679 * We handle correctly cases with some <=/>= here
681 if ((l_relation & ~ir_relation_equal) == (r_inc_relation & ~ir_relation_equal)) {
682 res_relation = (l_relation & ~ir_relation_equal) | (l_relation & r_inc_relation & ir_relation_equal);
684 if ((relation == res_relation) || ((relation & ~ir_relation_equal) == res_relation)) {
685 DBG_OUT_TR(l_relation, l_bound, r_relation, r_bound, relation, "true");
686 DBG_EVAL_CONFIRM(cmp);
687 return tarval_b_true;
689 ir_relation neg_relation = get_negated_relation(relation);
691 if ((neg_relation == res_relation) || ((neg_relation & ~ir_relation_equal) == res_relation)) {
692 DBG_OUT_TR(l_relation, l_bound, r_relation, r_bound, relation, "false");
693 DBG_EVAL_CONFIRM(cmp);
694 return tarval_b_false;
702 * Here, we check only the right Confirm, as the left Confirms are
703 * checked later anyway.
705 if (left == r_bound) {
707 * l == bound(r) AND relation(r) == relation:
709 * We know that a CMP b and check for that
711 if ((r_relation == relation) || (r_relation == (relation & ~ir_relation_equal))) {
712 DBG_OUT_R(r_relation, r_bound, left, relation, right, "true");
713 DBG_EVAL_CONFIRM(cmp);
714 return tarval_b_true;
717 * l == bound(r) AND relation(r) != relation:
719 * We know that a CMP b and check for a ~CMP b
722 neg_relation = get_negated_relation(relation);
724 if ((r_relation == neg_relation) || (r_relation == (neg_relation & ~ir_relation_equal))) {
725 DBG_OUT_R(r_relation, r_bound, left, relation, right, "false");
726 DBG_EVAL_CONFIRM(cmp);
727 return tarval_b_false;
732 /* now, try interval magic */
734 get_interval(&l_iv, l_bound, l_relation),
735 get_interval(&r_iv, r_bound, r_relation),
738 if (tv != tarval_bad) {
739 DBG_EVAL_CONFIRM(cmp);
744 /* from Here, check only left Confirm */
747 * some checks can be made WITHOUT constant bounds
749 if (right == l_bound) {
751 * r == bound(l) AND relation(l) == relation:
753 * We know that a CMP b and check for that
755 if ((l_relation == relation) || (l_relation == (relation & ~ir_relation_equal))) {
756 DBG_OUT_L(l_relation, l_bound, left, relation, right, "true");
757 DBG_EVAL_CONFIRM(cmp);
758 return tarval_b_true;
761 * r == bound(l) AND relation(l) is Not(relation):
763 * We know that a CMP b and check for a ~CMP b
766 neg_relation = get_negated_relation(relation);
768 if ((l_relation == neg_relation) || (l_relation == (neg_relation & ~ir_relation_equal))) {
769 DBG_OUT_L(l_relation, l_bound, left, relation, right, "false");
770 DBG_EVAL_CONFIRM(cmp);
771 return tarval_b_false;
776 /* now, only right == Const can help */
777 tv = value_of(right);
779 if (tv != tarval_bad) {
781 get_interval(&l_iv, l_bound, l_relation),
782 get_interval_from_tv(&r_iv, tv),
786 /* check some other cases */
787 if ((relation == ir_relation_equal || relation == ir_relation_less_greater) &&
788 is_Const(right) && is_Const_null(right)) {
789 /* for == 0 or != 0 we have some special tools */
790 ir_mode *mode = get_irn_mode(left);
791 const ir_node *dummy;
792 if (mode_is_reference(mode)) {
793 if (value_not_null(left, &dummy)) {
794 tv = relation == ir_relation_equal ? tarval_b_false : tarval_b_true;
797 if (value_not_zero(left, &dummy)) {
798 tv = relation == ir_relation_equal ? tarval_b_false : tarval_b_true;
804 if (tv != tarval_bad)
805 DBG_EVAL_CONFIRM(cmp);
812 * For debugging. Prints an interval into a string.
814 * @param buf address of a string buffer
815 * @param len length of the string buffer
816 * @param iv the interval
818 static int iv_snprintf(char *buf, size_t len, const interval_t *iv)
820 char smin[64], smax[64];
823 tarval_snprintf(smin, sizeof(smin), iv->min);
825 if (iv->min != iv->max || (iv->flags & (MIN_EXCLUDED|MAX_EXCLUDED))) {
826 tarval_snprintf(smax, sizeof(smax), iv->max);
828 return snprintf(buf, len, "%c%s, %s%c",
829 iv->flags & MIN_EXCLUDED ? '(' : '[',
831 iv->flags & MAX_EXCLUDED ? ')' : ']'
834 return snprintf(buf, len, "%s", smin);
836 return snprintf(buf, len, "<UNKNOWN>");
840 * For debugging. Prints an interval compare.
842 * @param l_iv the left interval
843 * @param r_iv the right interval
844 * @param relation the compare relation
846 static void print_iv_cmp(const interval_t *l_iv, const interval_t *r_iv, ir_relation relation)
848 char sl[128], sr[128];
850 iv_snprintf(sl, sizeof(sl), l_iv);
851 iv_snprintf(sr, sizeof(sr), r_iv);
853 ir_printf("%s %= %s", sl, relation, sr);
857 * For debugging. call *compare_iv() and prints inputs and result.
859 * @param l_iv the left interval
860 * @param r_iv the right interval
861 * @param relation the compare relation
863 static tarval *compare_iv_dbg(const interval_t *l_iv, const interval_t *r_iv, ir_relation relation)
865 tarval *tv = (compare_iv)(l_iv, r_iv, relation);
867 if (tv == tarval_bad)
870 ir_printf("In %e:\n", get_irg_entity(current_ir_graph));
871 print_iv_cmp(l_iv, r_iv, relation);
872 ir_printf(" = %T\n", tv);
876 #endif /* DEBUG_CONFIRM */