2 * This file is part of libFirm.
3 * Copyright (C) 2012 University of Karlsruhe.
8 * @brief Optimizations regarding Confirm nodes.
18 #include "iropt_dbg.h"
19 #include "iroptimize.h"
24 MIN_INCLUDED = 0x00, /**< [min, ... */
25 MAX_INCLUDED = 0x00, /**< ..., max] */
26 MIN_EXCLUDED = 0x01, /**< (min, ... */
27 MAX_EXCLUDED = 0x02 /**< ..., max) */
31 * An interval. We could use
32 * intervals that ALWAYS include its borders, even for
33 * floating point, as the precision is limited.
34 * However, as our tarval module did not support
35 * such kind of operation, we use border flags allowing
38 typedef struct interval_t {
39 ir_tarval *min; /**< lowest border */
40 ir_tarval *max; /**< highest border */
41 unsigned char flags; /**< border flags */
46 #define compare_iv(l_iv, r_iv, relation) compare_iv_dbg(l_iv, r_iv, relation)
49 static tarval *compare_iv_dbg(const interval_t *l_iv, const interval_t *r_iv, ir_relation relation);
52 #define DBG_OUT_TR(l_relation, l_bound, r_relation, r_bound, relation, v) \
53 ir_printf("In %e:\na %= %n && b %= %n ==> a %= b == %s\n", \
54 get_irg_entity(current_ir_graph), \
55 l_relation, l_bound, r_relation, r_bound, relation, v)
58 #define DBG_OUT_R(r_relation, r_bound, left, relation, right, v) \
59 ir_printf("In %e:\na %= %n ==> %n %= %n == %s\n", \
60 get_irg_entity(current_ir_graph), \
61 r_relation, r_bound, left, relation, right, v)
64 #define DBG_OUT_L(l_relation, l_bound, left, relation, right, v) \
65 ir_printf("In %e:\na %= %n ==> %n %= %n == %s\n", \
66 get_irg_entity(current_ir_graph), \
67 l_relation, l_bound, left, relation, right, v)
71 #define DBG_OUT_TR(l_relation, l_bound, r_relation, r_bound, relation, v) (void)0
72 #define DBG_OUT_R(r_relation, r_bound, left, relation, right, v) (void)0
73 #define DBG_OUT_L(l_relation, l_bound, left, relation, right, v) (void)0
75 #endif /* DEBUG_CONFIRM */
78 * Check, if the value of a node is != 0.
80 * This is a often needed case, so we handle here Confirm
83 int value_not_zero(const ir_node *n, const ir_node **confirm)
85 #define RET_ON(x) if (x) { *confirm = n; return 1; } break
88 ir_mode *mode = get_irn_mode(n);
93 /* there might be several Confirms one after other that form an interval */
96 /* we can safely skip Minus when checking for != 0 */
104 * Note: A Confirm is never after a Const. So,
105 * we simply can check the bound for being a Const
106 * without the fear that is might be hidden by a further Confirm.
108 tv = value_of(get_Confirm_bound(n));
109 if (tv == tarval_bad) {
110 n = get_Confirm_value(n);
114 relation = tarval_cmp(tv, get_mode_null(mode));
117 * Beware: C might by a NaN. It is not clear, what we should do
118 * than. Of course a NaN is != 0, but we might use this function
119 * to remove up Exceptions, and NaN's might generate Exception.
120 * So, we do NOT handle NaNs here for safety.
122 * Note that only the C != 0 case need additional checking.
124 switch (get_Confirm_relation(n)) {
125 case ir_relation_equal: /* n == C /\ C != 0 ==> n != 0 */
126 RET_ON(relation != ir_relation_equal && relation != ir_relation_unordered);
127 case ir_relation_less_greater: /* n != C /\ C == 0 ==> n != 0 */
128 RET_ON(relation == ir_relation_equal);
129 case ir_relation_less: /* n < C /\ C <= 0 ==> n != 0 */
130 RET_ON(relation == ir_relation_less || relation == ir_relation_equal);
131 case ir_relation_less_equal: /* n <= C /\ C < 0 ==> n != 0 */
132 RET_ON(relation == ir_relation_less);
133 case ir_relation_greater_equal: /* n >= C /\ C > 0 ==> n != 0 */
134 RET_ON(relation == ir_relation_greater);
135 case ir_relation_greater: /* n > C /\ C >= 0 ==> n != 0 */
136 RET_ON(relation == ir_relation_greater || relation == ir_relation_equal);
140 n = get_Confirm_value(n);
142 /* global entities are never NULL */
143 if (is_SymConst_addr_ent(n))
147 if (tv == tarval_bad)
150 relation = tarval_cmp(tv, get_mode_null(mode));
152 /* again, need check for NaN */
153 return (relation != ir_relation_equal) && (relation != ir_relation_unordered);
159 * Check, if the value of a node cannot represent a NULL pointer.
162 * - A SymConst(entity) is NEVER a NULL pointer
163 * - Confirms are evaluated
165 int value_not_null(const ir_node *n, const ir_node **confirm)
172 if (tarval_is_constant(tv) && ! tarval_is_null(tv))
175 assert(mode_is_reference(get_irn_mode(n)));
176 /* skip all Sel nodes */
181 if (is_Proj(n)) { n = get_Proj_pred(n); continue; }
185 if (is_SymConst_addr_ent(n)) {
186 /* global references are never NULL */
188 } else if (n == get_irg_frame(get_irn_irg(n))) {
189 /* local references are never NULL */
191 } else if (is_Alloc(n)) {
192 /* alloc never returns NULL (it throws an exception instead) */
195 /* check for more Confirms */
196 for (; is_Confirm(n); n = get_Confirm_value(n)) {
197 if (get_Confirm_relation(n) == ir_relation_less_greater) {
198 ir_node *bound = get_Confirm_bound(n);
199 ir_tarval *tv = value_of(bound);
201 if (tarval_is_null(tv)) {
213 static inline ir_value_classify_sign operator *(ir_value_classify_sign sign, int mul) {
214 return (ir_value_classify_sign) (sign*mul);
220 * Check, if the value of a node can be confirmed >= 0 or <= 0,
221 * If the mode of the value did not honor signed zeros, else
222 * check for >= 0 or < 0.
224 ir_value_classify_sign classify_value_sign(ir_node *n)
228 ir_relation cmp, ncmp;
232 unsigned code = get_irn_opcode(n);
242 return value_classified_unknown;
247 return value_classified_unknown;
249 tv = value_of(get_Confirm_bound(n));
250 if (tv == tarval_bad)
251 return value_classified_unknown;
253 mode = get_irn_mode(n);
256 * We can handle only >=, >, <, <= cases.
257 * We could handle == too, but this will be optimized into
260 * Note that for integer modes we have a slightly better
261 * optimization possibilities, so we handle this
264 cmp = get_Confirm_relation(n);
267 case ir_relation_less:
269 * must be x < c <= 1 to be useful if integer mode and -0 = 0
270 * x < c <= 0 to be useful else
272 case ir_relation_less_equal:
274 * must be x <= c < 1 to be useful if integer mode and -0 = 0
275 * x <= c < 0 to be useful else
277 c = mode_is_int(mode) && mode_honor_signed_zeros(mode) ?
278 get_mode_one(mode) : get_mode_null(mode);
280 ncmp = tarval_cmp(tv, c);
281 if (ncmp == ir_relation_equal)
282 ncmp = ir_relation_less_equal;
284 if (cmp != (ncmp ^ ir_relation_equal))
285 return value_classified_unknown;
288 return value_classified_negative * negate;
290 case ir_relation_greater_equal:
292 * must be x >= c > -1 to be useful if integer mode
293 * x >= c >= 0 to be useful else
295 case ir_relation_greater:
297 * must be x > c >= -1 to be useful if integer mode
298 * x > c >= 0 to be useful else
300 if (mode_is_int(mode)) {
301 c = get_mode_minus_one(mode);
303 ncmp = tarval_cmp(tv, c);
304 if (ncmp == ir_relation_equal)
305 ncmp = ir_relation_greater_equal;
307 if (cmp != (ncmp ^ ir_relation_equal))
308 return value_classified_unknown;
310 c = get_mode_minus_one(mode);
312 ncmp = tarval_cmp(tv, c);
314 if (ncmp != ir_relation_equal && ncmp != ir_relation_greater)
315 return value_classified_unknown;
319 return value_classified_positive * negate;
322 return value_classified_unknown;
327 * construct an interval from a value
329 * @return the filled interval or NULL if no interval
330 * can be created (happens only on floating point
332 static interval_t *get_interval_from_tv(interval_t *iv, ir_tarval *tv)
334 ir_mode *mode = get_tarval_mode(tv);
336 if (tv == tarval_bad) {
337 if (mode_is_float(mode)) {
338 /* NaN could be included which we cannot handle */
339 iv->min = tarval_bad;
340 iv->max = tarval_bad;
341 iv->flags = MIN_EXCLUDED | MAX_EXCLUDED;
345 iv->min = get_mode_min(mode);
346 iv->max = get_mode_max(mode);
347 iv->flags = MIN_INCLUDED | MAX_INCLUDED;
352 if (mode_is_float(mode)) {
353 if (tv == get_mode_NAN(mode)) {
354 /* arg, we cannot handle NaN's. */
355 iv->min = tarval_bad;
356 iv->max = tarval_bad;
357 iv->flags = MIN_EXCLUDED | MAX_EXCLUDED;
365 iv->flags = MIN_INCLUDED | MAX_INCLUDED;
371 * construct an interval from a Confirm
373 * @param iv an empty interval, will be filled
374 * @param bound the bound value
375 * @param relation the Confirm compare relation
377 * @return the filled interval or NULL if no interval
378 * can be created (happens only on floating point
380 static interval_t *get_interval(interval_t *iv, ir_node *bound, ir_relation relation)
382 ir_mode *mode = get_irn_mode(bound);
383 ir_tarval *tv = value_of(bound);
385 if (tv == tarval_bad) {
386 /* There is nothing we could do here. For integer
387 * modes we could return [-oo, +oo], but there is
388 * nothing we could deduct from such an interval.
389 * So, speed things up and return unknown.
391 iv->min = tarval_bad;
392 iv->max = tarval_bad;
393 iv->flags = MIN_EXCLUDED | MAX_EXCLUDED;
397 if (mode_is_float(mode)) {
398 if (tv == get_mode_NAN(mode)) {
399 /* arg, we cannot handle NaN's. */
400 iv->min = tarval_bad;
401 iv->max = tarval_bad;
402 iv->flags = MIN_EXCLUDED | MAX_EXCLUDED;
408 /* check which side is known */
410 case ir_relation_equal:
414 iv->flags = MIN_INCLUDED | MAX_INCLUDED;
417 case ir_relation_less_equal:
419 iv->min = get_mode_min(mode);
421 iv->flags = MIN_INCLUDED | MAX_INCLUDED;
424 case ir_relation_less:
426 iv->min = get_mode_min(mode);
428 iv->flags = MIN_INCLUDED | MAX_EXCLUDED;
431 case ir_relation_greater:
434 iv->max = get_mode_max(mode);
435 iv->flags = MIN_EXCLUDED | MAX_INCLUDED;
438 case ir_relation_greater_equal:
441 iv->max = get_mode_max(mode);
442 iv->flags = MIN_INCLUDED | MAX_INCLUDED;
445 case ir_relation_less_equal_greater:
447 * Ordered means, that at least neither
448 * our bound nor our value ara NaN's
451 iv->min = get_mode_min(mode);
452 iv->max = get_mode_max(mode);
453 iv->flags = MIN_INCLUDED | MAX_INCLUDED;
458 * We do not handle UNORDERED, as a NaN
459 * could be included in the interval.
461 iv->min = tarval_bad;
462 iv->max = tarval_bad;
463 iv->flags = MIN_EXCLUDED | MAX_EXCLUDED;
467 if (iv->min != tarval_bad && iv->max != tarval_bad)
473 * Try to evaluate l_iv relation r_iv.
475 * @param l_iv the left interval
476 * @param r_iv the right interval
477 * @param relation the compare relation
480 * tarval_b_true or tarval_b_false it it can be evaluated,
483 static ir_tarval *(compare_iv)(const interval_t *l_iv, const interval_t *r_iv, ir_relation relation)
487 ir_tarval *tv_true = tarval_b_true, *tv_false = tarval_b_false;
489 /* if one interval contains NaNs, we cannot evaluate anything */
490 if (! l_iv || ! r_iv)
493 /* we can only check ordered relations */
494 if (relation & ir_relation_unordered) {
497 relation = get_negated_relation(relation);
503 /* if we have > or >=, we do the inverse to save some cases */
504 if (relation == ir_relation_greater_equal || relation == ir_relation_greater) {
507 relation = get_inversed_relation(relation);
513 /* now, only the following cases remains */
515 case ir_relation_equal:
516 /* two intervals can be compared for equality only if they are a single value */
517 if (l_iv->min == l_iv->max && r_iv->min == r_iv->max)
518 return tarval_cmp(l_iv->min, r_iv->min) == ir_relation_equal ? tv_true : tv_false;
520 /* if both intervals do not intersect, it is never equal */
521 res = tarval_cmp(l_iv->max, r_iv->min);
523 /* b < c ==> [a,b] != [c,d] */
524 if (res == ir_relation_less)
527 /* b <= c ==> [a,b) != [c,d] AND [a,b] != (c,d] */
528 if ((l_iv->flags & MAX_EXCLUDED || r_iv->flags & MIN_EXCLUDED)
529 && (res == ir_relation_equal))
532 res = tarval_cmp(r_iv->max, l_iv->min);
534 /* d < a ==> [c,d] != [a,b] */
535 if (res == ir_relation_less)
538 /* d <= a ==> [c,d) != [a,b] AND [c,d] != (a,b] */
539 if ((r_iv->flags & MAX_EXCLUDED || l_iv->flags & MIN_EXCLUDED)
540 && (res == ir_relation_equal))
544 case ir_relation_less_greater:
545 /* two intervals can be compared for not equality only if they are a single value */
546 if (l_iv->min == l_iv->max && r_iv->min == r_iv->max)
547 return tarval_cmp(l_iv->min, r_iv->min) != ir_relation_equal ? tv_true : tv_false;
550 case ir_relation_less:
551 res = tarval_cmp(l_iv->max, r_iv->min);
553 /* [a, b] < [c, d] <==> b < c */
554 if (res == ir_relation_less)
557 /* if one border is excluded, b <= c is enough */
558 if ((l_iv->flags & MAX_EXCLUDED || r_iv->flags & MIN_EXCLUDED) &&
559 res == ir_relation_equal)
562 /* [a, b] >= [c, d] <==> a > d */
563 res = tarval_cmp(l_iv->min, r_iv->max);
564 if (res == ir_relation_greater)
567 /* if one border is excluded, a >= d is enough */
568 if ((l_iv->flags & MIN_EXCLUDED || r_iv->flags & MAX_EXCLUDED) &&
569 res == ir_relation_equal)
573 case ir_relation_less_equal:
574 /* [a, b) <= [c, d] or [a, b] <= (c, d] <==> b <= c */
575 flags = (l_iv->flags & MAX_EXCLUDED) | (r_iv->flags & MIN_EXCLUDED);
577 res = tarval_cmp(l_iv->max, r_iv->min);
579 if (res == ir_relation_less || res == ir_relation_equal)
583 res = tarval_cmp(l_iv->min, r_iv->max);
585 /* [a, b] > [c, d] <==> a > d */
586 if (res == ir_relation_greater)
589 /* if one border is excluded, a >= d is enough */
590 if ((l_iv->flags & MIN_EXCLUDED || r_iv->flags & MAX_EXCLUDED) &&
591 res == ir_relation_equal)
595 case ir_relation_less_equal_greater:
596 /* Hmm. if both are intervals, we can find an order */
606 * Returns non-zero, if a given relation is transitive.
608 static int is_transitive(ir_relation relation)
610 return (ir_relation_false < relation && relation < ir_relation_less_greater);
614 * Return the value of a Cmp if one or both predecessors
617 * @param cmp the Cmp node
618 * @param left the left operand of the Cmp
619 * @param right the right operand of the Cmp
620 * @param relation the compare relation
622 ir_tarval *computed_value_Cmp_Confirm(const ir_node *cmp, ir_node *left, ir_node *right, ir_relation relation)
625 ir_relation l_relation, res_relation, neg_relation;
626 interval_t l_iv, r_iv;
629 if (is_Confirm(right)) {
630 /* we want the Confirm on the left side */
635 relation = get_inversed_relation(relation);
636 } else if (! is_Confirm(left)) {
637 /* nothing more found */
639 goto check_null_case;
642 /* ok, here at least left is a Confirm, right might be */
643 l_bound = get_Confirm_bound(left);
644 l_relation = get_Confirm_relation(left);
646 if (is_Confirm(right)) {
648 * both sides are Confirm's. Check some rare cases first.
650 ir_node *r_bound = get_Confirm_bound(right);
651 ir_relation r_relation = get_Confirm_relation(right);
654 * some check can be made WITHOUT constant bounds
656 if (r_bound == l_bound) {
657 if (is_transitive(l_relation)) {
658 ir_relation r_inc_relation = get_inversed_relation(r_relation);
661 * triangle inequality:
663 * a CMP B && B CMP b => a CMP b, !(a ~CMP b)
665 * We handle correctly cases with some <=/>= here
667 if ((l_relation & ~ir_relation_equal) == (r_inc_relation & ~ir_relation_equal)) {
668 res_relation = (l_relation & ~ir_relation_equal) | (l_relation & r_inc_relation & ir_relation_equal);
670 if ((relation == res_relation) || ((relation & ~ir_relation_equal) == res_relation)) {
671 DBG_OUT_TR(l_relation, l_bound, r_relation, r_bound, relation, "true");
672 DBG_EVAL_CONFIRM(cmp);
673 return tarval_b_true;
675 ir_relation neg_relation = get_negated_relation(relation);
677 if ((neg_relation == res_relation) || ((neg_relation & ~ir_relation_equal) == res_relation)) {
678 DBG_OUT_TR(l_relation, l_bound, r_relation, r_bound, relation, "false");
679 DBG_EVAL_CONFIRM(cmp);
680 return tarval_b_false;
688 * Here, we check only the right Confirm, as the left Confirms are
689 * checked later anyway.
691 if (left == r_bound) {
693 * l == bound(r) AND relation(r) == relation:
695 * We know that a CMP b and check for that
697 if ((r_relation == relation) || (r_relation == (relation & ~ir_relation_equal))) {
698 DBG_OUT_R(r_relation, r_bound, left, relation, right, "true");
699 DBG_EVAL_CONFIRM(cmp);
700 return tarval_b_true;
703 * l == bound(r) AND relation(r) != relation:
705 * We know that a CMP b and check for a ~CMP b
708 neg_relation = get_negated_relation(relation);
710 if ((r_relation == neg_relation) || (r_relation == (neg_relation & ~ir_relation_equal))) {
711 DBG_OUT_R(r_relation, r_bound, left, relation, right, "false");
712 DBG_EVAL_CONFIRM(cmp);
713 return tarval_b_false;
718 /* now, try interval magic */
720 get_interval(&l_iv, l_bound, l_relation),
721 get_interval(&r_iv, r_bound, r_relation),
724 if (tv != tarval_bad) {
725 DBG_EVAL_CONFIRM(cmp);
730 /* from Here, check only left Confirm */
733 * some checks can be made WITHOUT constant bounds
735 if (right == l_bound) {
737 * r == bound(l) AND relation(l) == relation:
739 * We know that a CMP b and check for that
741 if ((l_relation == relation) || (l_relation == (relation & ~ir_relation_equal))) {
742 DBG_OUT_L(l_relation, l_bound, left, relation, right, "true");
743 DBG_EVAL_CONFIRM(cmp);
744 return tarval_b_true;
747 * r == bound(l) AND relation(l) is Not(relation):
749 * We know that a CMP b and check for a ~CMP b
752 neg_relation = get_negated_relation(relation);
754 if ((l_relation == neg_relation) || (l_relation == (neg_relation & ~ir_relation_equal))) {
755 DBG_OUT_L(l_relation, l_bound, left, relation, right, "false");
756 DBG_EVAL_CONFIRM(cmp);
757 return tarval_b_false;
762 /* now, only right == Const can help */
763 tv = value_of(right);
765 if (tv != tarval_bad) {
767 get_interval(&l_iv, l_bound, l_relation),
768 get_interval_from_tv(&r_iv, tv),
772 /* check some other cases */
773 if ((relation == ir_relation_equal || relation == ir_relation_less_greater) &&
774 is_Const(right) && is_Const_null(right)) {
775 /* for == 0 or != 0 we have some special tools */
776 ir_mode *mode = get_irn_mode(left);
777 const ir_node *dummy;
778 if (mode_is_reference(mode)) {
779 if (value_not_null(left, &dummy)) {
780 tv = relation == ir_relation_equal ? tarval_b_false : tarval_b_true;
783 if (value_not_zero(left, &dummy)) {
784 tv = relation == ir_relation_equal ? tarval_b_false : tarval_b_true;
790 if (tv != tarval_bad)
791 DBG_EVAL_CONFIRM(cmp);
798 * For debugging. Prints an interval into a string.
800 * @param buf address of a string buffer
801 * @param len length of the string buffer
802 * @param iv the interval
804 static int iv_snprintf(char *buf, size_t len, const interval_t *iv)
806 char smin[64], smax[64];
809 tarval_snprintf(smin, sizeof(smin), iv->min);
811 if (iv->min != iv->max || (iv->flags & (MIN_EXCLUDED|MAX_EXCLUDED))) {
812 tarval_snprintf(smax, sizeof(smax), iv->max);
814 return snprintf(buf, len, "%c%s, %s%c",
815 iv->flags & MIN_EXCLUDED ? '(' : '[',
817 iv->flags & MAX_EXCLUDED ? ')' : ']'
820 return snprintf(buf, len, "%s", smin);
822 return snprintf(buf, len, "<UNKNOWN>");
826 * For debugging. Prints an interval compare.
828 * @param l_iv the left interval
829 * @param r_iv the right interval
830 * @param relation the compare relation
832 static void print_iv_cmp(const interval_t *l_iv, const interval_t *r_iv, ir_relation relation)
834 char sl[128], sr[128];
836 iv_snprintf(sl, sizeof(sl), l_iv);
837 iv_snprintf(sr, sizeof(sr), r_iv);
839 ir_printf("%s %= %s", sl, relation, sr);
843 * For debugging. call *compare_iv() and prints inputs and result.
845 * @param l_iv the left interval
846 * @param r_iv the right interval
847 * @param relation the compare relation
849 static tarval *compare_iv_dbg(const interval_t *l_iv, const interval_t *r_iv, ir_relation relation)
851 tarval *tv = (compare_iv)(l_iv, r_iv, relation);
853 if (tv == tarval_bad)
856 ir_printf("In %e:\n", get_irg_entity(current_ir_graph));
857 print_iv_cmp(l_iv, r_iv, relation);
858 ir_printf(" = %T\n", tv);
862 #endif /* DEBUG_CONFIRM */