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)
126 relation = tarval_cmp(tv, get_mode_null(mode));
129 * Beware: C might by a NaN. It is not clear, what we should do
130 * than. Of course a NaN is != 0, but we might use this function
131 * to remove up Exceptions, and NaN's might generate Exception.
132 * So, we do NOT handle NaNs here for safety.
134 * Note that only the C != 0 case need additional checking.
136 switch (get_Confirm_relation(n)) {
137 case ir_relation_equal: /* n == C /\ C != 0 ==> n != 0 */
138 RET_ON(relation != ir_relation_equal && relation != ir_relation_unordered);
139 case ir_relation_less_greater: /* n != C /\ C == 0 ==> n != 0 */
140 RET_ON(relation == ir_relation_equal);
141 case ir_relation_less: /* n < C /\ C <= 0 ==> n != 0 */
142 RET_ON(relation == ir_relation_less || relation == ir_relation_equal);
143 case ir_relation_less_equal: /* n <= C /\ C < 0 ==> n != 0 */
144 RET_ON(relation == ir_relation_less);
145 case ir_relation_greater_equal: /* n >= C /\ C > 0 ==> n != 0 */
146 RET_ON(relation == ir_relation_greater);
147 case ir_relation_greater: /* n > C /\ C >= 0 ==> n != 0 */
148 RET_ON(relation == ir_relation_greater || relation == ir_relation_equal);
152 n = get_Confirm_value(n);
156 if (tv == tarval_bad)
159 relation = tarval_cmp(tv, get_mode_null(mode));
161 /* again, need check for NaN */
162 return (relation != ir_relation_equal) && (relation != ir_relation_unordered);
165 } /* value_not_zero */
168 * Check, if the value of a node cannot represent a NULL pointer.
170 * - Casts are skipped, Sels are skipped
171 * - A SymConst(entity) is NEVER a NULL pointer
172 * - Confirms are evaluated
174 int value_not_null(const ir_node *n, const ir_node **confirm)
179 n = skip_Cast_const(n);
182 if (tarval_is_constant(tv) && ! tarval_is_null(tv))
185 assert(mode_is_reference(get_irn_mode(n)));
186 /* skip all Sel nodes and Cast's */
188 n = skip_Cast(get_Sel_ptr(n));
191 if (is_Cast(n)) { n = get_Cast_op(n); continue; }
192 if (is_Proj(n)) { n = get_Proj_pred(n); continue; }
196 if (is_SymConst_addr_ent(n)) {
197 /* global references are never NULL */
199 } else if (n == get_irg_frame(get_irn_irg(n))) {
200 /* local references are never NULL */
202 } else if (is_Alloc(n)) {
203 /* alloc never returns NULL (it throws an exception instead) */
206 /* check for more Confirms */
207 for (; is_Confirm(n); n = skip_Cast(get_Confirm_value(n))) {
208 if (get_Confirm_relation(n) == ir_relation_less_greater) {
209 ir_node *bound = get_Confirm_bound(n);
210 ir_tarval *tv = value_of(bound);
212 if (tarval_is_null(tv)) {
220 } /* value_not_null */
224 static inline ir_value_classify_sign operator *(ir_value_classify_sign sign, int mul) {
225 return (ir_value_classify_sign) (sign*mul);
231 * Check, if the value of a node can be confirmed >= 0 or <= 0,
232 * If the mode of the value did not honor signed zeros, else
233 * check for >= 0 or < 0.
235 ir_value_classify_sign classify_value_sign(ir_node *n)
239 ir_relation cmp, ncmp;
243 unsigned code = get_irn_opcode(n);
253 return value_classified_unknown;
258 return value_classified_unknown;
260 tv = value_of(get_Confirm_bound(n));
261 if (tv == tarval_bad)
262 return value_classified_unknown;
264 mode = get_irn_mode(n);
267 * We can handle only >=, >, <, <= cases.
268 * We could handle == too, but this will be optimized into
271 * Note that for integer modes we have a slightly better
272 * optimization possibilities, so we handle this
275 cmp = get_Confirm_relation(n);
278 case ir_relation_less:
280 * must be x < c <= 1 to be useful if integer mode and -0 = 0
281 * x < c <= 0 to be useful else
283 case ir_relation_less_equal:
285 * must be x <= c < 1 to be useful if integer mode and -0 = 0
286 * x <= c < 0 to be useful else
288 c = mode_is_int(mode) && mode_honor_signed_zeros(mode) ?
289 get_mode_one(mode) : get_mode_null(mode);
291 ncmp = tarval_cmp(tv, c);
292 if (ncmp == ir_relation_equal)
293 ncmp = ir_relation_less_equal;
295 if (cmp != (ncmp ^ ir_relation_equal))
296 return value_classified_unknown;
299 return value_classified_negative * negate;
301 case ir_relation_greater_equal:
303 * must be x >= c > -1 to be useful if integer mode
304 * x >= c >= 0 to be useful else
306 case ir_relation_greater:
308 * must be x > c >= -1 to be useful if integer mode
309 * x > c >= 0 to be useful else
311 if (mode_is_int(mode)) {
312 c = get_mode_minus_one(mode);
314 ncmp = tarval_cmp(tv, c);
315 if (ncmp == ir_relation_equal)
316 ncmp = ir_relation_greater_equal;
318 if (cmp != (ncmp ^ ir_relation_equal))
319 return value_classified_unknown;
321 c = get_mode_minus_one(mode);
323 ncmp = tarval_cmp(tv, c);
325 if (ncmp != ir_relation_equal && ncmp != ir_relation_greater)
326 return value_classified_unknown;
330 return value_classified_positive * negate;
333 return value_classified_unknown;
335 } /* classify_value_sign */
338 * construct an interval from a value
340 * @return the filled interval or NULL if no interval
341 * can be created (happens only on floating point
343 static interval_t *get_interval_from_tv(interval_t *iv, ir_tarval *tv)
345 ir_mode *mode = get_tarval_mode(tv);
347 if (tv == tarval_bad) {
348 if (mode_is_float(mode)) {
349 /* NaN could be included which we cannot handle */
350 iv->min = tarval_bad;
351 iv->max = tarval_bad;
352 iv->flags = MIN_EXCLUDED | MAX_EXCLUDED;
356 iv->min = get_mode_min(mode);
357 iv->max = get_mode_max(mode);
358 iv->flags = MIN_INCLUDED | MAX_INCLUDED;
363 if (mode_is_float(mode)) {
364 if (tv == get_mode_NAN(mode)) {
365 /* arg, we cannot handle NaN's. */
366 iv->min = tarval_bad;
367 iv->max = tarval_bad;
368 iv->flags = MIN_EXCLUDED | MAX_EXCLUDED;
376 iv->flags = MIN_INCLUDED | MAX_INCLUDED;
379 } /* get_interval_from_tv */
382 * construct an interval from a Confirm
384 * @param iv an empty interval, will be filled
385 * @param bound the bound value
386 * @param relation the Confirm compare relation
388 * @return the filled interval or NULL if no interval
389 * can be created (happens only on floating point
391 static interval_t *get_interval(interval_t *iv, ir_node *bound, ir_relation relation)
393 ir_mode *mode = get_irn_mode(bound);
394 ir_tarval *tv = value_of(bound);
396 if (tv == tarval_bad) {
397 /* There is nothing we could do here. For integer
398 * modes we could return [-oo, +oo], but there is
399 * nothing we could deduct from such an interval.
400 * So, speed things up and return unknown.
402 iv->min = tarval_bad;
403 iv->max = tarval_bad;
404 iv->flags = MIN_EXCLUDED | MAX_EXCLUDED;
408 if (mode_is_float(mode)) {
409 if (tv == get_mode_NAN(mode)) {
410 /* arg, we cannot handle NaN's. */
411 iv->min = tarval_bad;
412 iv->max = tarval_bad;
413 iv->flags = MIN_EXCLUDED | MAX_EXCLUDED;
419 /* check which side is known */
421 case ir_relation_equal:
425 iv->flags = MIN_INCLUDED | MAX_INCLUDED;
428 case ir_relation_less_equal:
430 iv->min = get_mode_min(mode);
432 iv->flags = MIN_INCLUDED | MAX_INCLUDED;
435 case ir_relation_less:
437 iv->min = get_mode_min(mode);
439 iv->flags = MIN_INCLUDED | MAX_EXCLUDED;
442 case ir_relation_greater:
445 iv->max = get_mode_max(mode);
446 iv->flags = MIN_EXCLUDED | MAX_INCLUDED;
449 case ir_relation_greater_equal:
452 iv->max = get_mode_max(mode);
453 iv->flags = MIN_INCLUDED | MAX_INCLUDED;
456 case ir_relation_less_equal_greater:
458 * Ordered means, that at least neither
459 * our bound nor our value ara NaN's
462 iv->min = get_mode_min(mode);
463 iv->max = get_mode_max(mode);
464 iv->flags = MIN_INCLUDED | MAX_INCLUDED;
469 * We do not handle UNORDERED, as a NaN
470 * could be included in the interval.
472 iv->min = tarval_bad;
473 iv->max = tarval_bad;
474 iv->flags = MIN_EXCLUDED | MAX_EXCLUDED;
478 if (iv->min != tarval_bad && iv->max != tarval_bad)
484 * Try to evaluate l_iv relation r_iv.
486 * @param l_iv the left interval
487 * @param r_iv the right interval
488 * @param relation the compare relation
491 * tarval_b_true or tarval_b_false it it can be evaluated,
494 static ir_tarval *(compare_iv)(const interval_t *l_iv, const interval_t *r_iv, ir_relation relation)
498 ir_tarval *tv_true = tarval_b_true, *tv_false = tarval_b_false;
500 /* if one interval contains NaNs, we cannot evaluate anything */
501 if (! l_iv || ! r_iv)
504 /* we can only check ordered relations */
505 if (relation & ir_relation_unordered) {
508 relation = get_negated_relation(relation);
514 /* if we have > or >=, we do the inverse to save some cases */
515 if (relation == ir_relation_greater_equal || relation == ir_relation_greater) {
518 relation = get_inversed_relation(relation);
524 /* now, only the following cases remains */
526 case ir_relation_equal:
527 /* two intervals can be compared for equality only if they are a single value */
528 if (l_iv->min == l_iv->max && r_iv->min == r_iv->max)
529 return tarval_cmp(l_iv->min, r_iv->min) == ir_relation_equal ? tv_true : tv_false;
531 /* if both intervals do not intersect, it is never equal */
532 res = tarval_cmp(l_iv->max, r_iv->min);
534 /* b < c ==> [a,b] != [c,d] */
535 if (res == ir_relation_less)
538 /* b <= c ==> [a,b) != [c,d] AND [a,b] != (c,d] */
539 if ((l_iv->flags & MAX_EXCLUDED || r_iv->flags & MIN_EXCLUDED)
540 && (res == ir_relation_equal))
543 res = tarval_cmp(r_iv->max, l_iv->min);
545 /* d < a ==> [c,d] != [a,b] */
546 if (res == ir_relation_less)
549 /* d <= a ==> [c,d) != [a,b] AND [c,d] != (a,b] */
550 if ((r_iv->flags & MAX_EXCLUDED || l_iv->flags & MIN_EXCLUDED)
551 && (res == ir_relation_equal))
555 case ir_relation_less_greater:
556 /* two intervals can be compared for not equality only if they are a single value */
557 if (l_iv->min == l_iv->max && r_iv->min == r_iv->max)
558 return tarval_cmp(l_iv->min, r_iv->min) != ir_relation_equal ? tv_true : tv_false;
561 case ir_relation_less:
562 res = tarval_cmp(l_iv->max, r_iv->min);
564 /* [a, b] < [c, d] <==> b < c */
565 if (res == ir_relation_less)
568 /* if one border is excluded, b <= c is enough */
569 if ((l_iv->flags & MAX_EXCLUDED || r_iv->flags & MIN_EXCLUDED) &&
570 res == ir_relation_equal)
573 /* [a, b] >= [c, d] <==> a > d */
574 res = tarval_cmp(l_iv->min, r_iv->max);
575 if (res == ir_relation_greater)
578 /* if one border is excluded, a >= d is enough */
579 if ((l_iv->flags & MIN_EXCLUDED || r_iv->flags & MAX_EXCLUDED) &&
580 res == ir_relation_equal)
584 case ir_relation_less_equal:
585 /* [a, b) <= [c, d] or [a, b] <= (c, d] <==> b <= c */
586 flags = (l_iv->flags & MAX_EXCLUDED) | (r_iv->flags & MIN_EXCLUDED);
588 res = tarval_cmp(l_iv->max, r_iv->min);
590 if (res == ir_relation_less || res == ir_relation_equal)
594 res = tarval_cmp(l_iv->min, r_iv->max);
596 /* [a, b] > [c, d] <==> a > d */
597 if (res == ir_relation_greater)
600 /* if one border is excluded, a >= d is enough */
601 if ((l_iv->flags & MIN_EXCLUDED || r_iv->flags & MAX_EXCLUDED) &&
602 res == ir_relation_equal)
606 case ir_relation_less_equal_greater:
607 /* Hmm. if both are intervals, we can find an order */
617 * Returns non-zero, if a given relation is transitive.
619 static int is_transitive(ir_relation relation)
621 return (ir_relation_false < relation && relation < ir_relation_less_greater);
622 } /* is_transitive */
625 * Return the value of a Cmp if one or both predecessors
628 * @param cmp the Cmp node
629 * @param left the left operand of the Cmp
630 * @param right the right operand of the Cmp
631 * @param relation the compare relation
633 ir_tarval *computed_value_Cmp_Confirm(const ir_node *cmp, ir_node *left, ir_node *right, ir_relation relation)
636 ir_relation l_relation, res_relation, neg_relation;
637 interval_t l_iv, r_iv;
640 if (is_Confirm(right)) {
641 /* we want the Confirm on the left side */
646 relation = get_inversed_relation(relation);
647 } else if (! is_Confirm(left)) {
648 /* nothing more found */
650 goto check_null_case;
653 /* ok, here at least left is a Confirm, right might be */
654 l_bound = get_Confirm_bound(left);
655 l_relation = get_Confirm_relation(left);
657 if (is_Confirm(right)) {
659 * both sides are Confirm's. Check some rare cases first.
661 ir_node *r_bound = get_Confirm_bound(right);
662 ir_relation r_relation = get_Confirm_relation(right);
665 * some check can be made WITHOUT constant bounds
667 if (r_bound == l_bound) {
668 if (is_transitive(l_relation)) {
669 ir_relation r_inc_relation = get_inversed_relation(r_relation);
672 * triangle inequality:
674 * a CMP B && B CMP b => a CMP b, !(a ~CMP b)
676 * We handle correctly cases with some <=/>= here
678 if ((l_relation & ~ir_relation_equal) == (r_inc_relation & ~ir_relation_equal)) {
679 res_relation = (l_relation & ~ir_relation_equal) | (l_relation & r_inc_relation & ir_relation_equal);
681 if ((relation == res_relation) || ((relation & ~ir_relation_equal) == res_relation)) {
682 DBG_OUT_TR(l_relation, l_bound, r_relation, r_bound, relation, "true");
683 DBG_EVAL_CONFIRM(cmp);
684 return tarval_b_true;
686 ir_relation neg_relation = get_negated_relation(relation);
688 if ((neg_relation == res_relation) || ((neg_relation & ~ir_relation_equal) == res_relation)) {
689 DBG_OUT_TR(l_relation, l_bound, r_relation, r_bound, relation, "false");
690 DBG_EVAL_CONFIRM(cmp);
691 return tarval_b_false;
699 * Here, we check only the right Confirm, as the left Confirms are
700 * checked later anyway.
702 if (left == r_bound) {
704 * l == bound(r) AND relation(r) == relation:
706 * We know that a CMP b and check for that
708 if ((r_relation == relation) || (r_relation == (relation & ~ir_relation_equal))) {
709 DBG_OUT_R(r_relation, r_bound, left, relation, right, "true");
710 DBG_EVAL_CONFIRM(cmp);
711 return tarval_b_true;
714 * l == bound(r) AND relation(r) != relation:
716 * We know that a CMP b and check for a ~CMP b
719 neg_relation = get_negated_relation(relation);
721 if ((r_relation == neg_relation) || (r_relation == (neg_relation & ~ir_relation_equal))) {
722 DBG_OUT_R(r_relation, r_bound, left, relation, right, "false");
723 DBG_EVAL_CONFIRM(cmp);
724 return tarval_b_false;
729 /* now, try interval magic */
731 get_interval(&l_iv, l_bound, l_relation),
732 get_interval(&r_iv, r_bound, r_relation),
735 if (tv != tarval_bad) {
736 DBG_EVAL_CONFIRM(cmp);
741 /* from Here, check only left Confirm */
744 * some checks can be made WITHOUT constant bounds
746 if (right == l_bound) {
748 * r == bound(l) AND relation(l) == relation:
750 * We know that a CMP b and check for that
752 if ((l_relation == relation) || (l_relation == (relation & ~ir_relation_equal))) {
753 DBG_OUT_L(l_relation, l_bound, left, relation, right, "true");
754 DBG_EVAL_CONFIRM(cmp);
755 return tarval_b_true;
758 * r == bound(l) AND relation(l) is Not(relation):
760 * We know that a CMP b and check for a ~CMP b
763 neg_relation = get_negated_relation(relation);
765 if ((l_relation == neg_relation) || (l_relation == (neg_relation & ~ir_relation_equal))) {
766 DBG_OUT_L(l_relation, l_bound, left, relation, right, "false");
767 DBG_EVAL_CONFIRM(cmp);
768 return tarval_b_false;
773 /* now, only right == Const can help */
774 tv = value_of(right);
776 if (tv != tarval_bad) {
778 get_interval(&l_iv, l_bound, l_relation),
779 get_interval_from_tv(&r_iv, tv),
783 /* check some other cases */
784 if ((relation == ir_relation_equal || relation == ir_relation_less_greater) &&
785 is_Const(right) && is_Const_null(right)) {
786 /* for == 0 or != 0 we have some special tools */
787 ir_mode *mode = get_irn_mode(left);
788 const ir_node *dummy;
789 if (mode_is_reference(mode)) {
790 if (value_not_null(left, &dummy)) {
791 tv = relation == ir_relation_equal ? tarval_b_false : tarval_b_true;
794 if (value_not_zero(left, &dummy)) {
795 tv = relation == ir_relation_equal ? tarval_b_false : tarval_b_true;
801 if (tv != tarval_bad)
802 DBG_EVAL_CONFIRM(cmp);
805 } /* computed_value_Cmp_Confirm */
809 * For debugging. Prints an interval into a string.
811 * @param buf address of a string buffer
812 * @param len length of the string buffer
813 * @param iv the interval
815 static int iv_snprintf(char *buf, size_t len, const interval_t *iv)
817 char smin[64], smax[64];
820 tarval_snprintf(smin, sizeof(smin), iv->min);
822 if (iv->min != iv->max || (iv->flags & (MIN_EXCLUDED|MAX_EXCLUDED))) {
823 tarval_snprintf(smax, sizeof(smax), iv->max);
825 return snprintf(buf, len, "%c%s, %s%c",
826 iv->flags & MIN_EXCLUDED ? '(' : '[',
828 iv->flags & MAX_EXCLUDED ? ')' : ']'
831 return snprintf(buf, len, "%s", smin);
833 return snprintf(buf, len, "<UNKNOWN>");
837 * For debugging. Prints an interval compare.
839 * @param l_iv the left interval
840 * @param r_iv the right interval
841 * @param relation the compare relation
843 static void print_iv_cmp(const interval_t *l_iv, const interval_t *r_iv, ir_relation relation)
845 char sl[128], sr[128];
847 iv_snprintf(sl, sizeof(sl), l_iv);
848 iv_snprintf(sr, sizeof(sr), r_iv);
850 ir_printf("%s %= %s", sl, relation, sr);
854 * For debugging. call *compare_iv() and prints inputs and result.
856 * @param l_iv the left interval
857 * @param r_iv the right interval
858 * @param relation the compare relation
860 static tarval *compare_iv_dbg(const interval_t *l_iv, const interval_t *r_iv, ir_relation relation)
862 tarval *tv = (compare_iv)(l_iv, r_iv, relation);
864 if (tv == tarval_bad)
867 ir_printf("In %e:\n", get_irg_entity(current_ir_graph));
868 print_iv_cmp(l_iv, r_iv, relation);
869 ir_printf(" = %T\n", tv);
871 } /* compare_iv_dbg */
873 #endif /* DEBUG_CONFIRM */