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
33 #include "iropt_dbg.h"
34 #include "iroptimize.h"
39 MIN_INCLUDED = 0x00, /**< [min, ... */
40 MAX_INCLUDED = 0x00, /**< ..., max] */
41 MIN_EXCLUDED = 0x01, /**< (min, ... */
42 MAX_EXCLUDED = 0x02 /**< ..., max) */
46 * An interval. We could use
47 * intervals that ALWAYS include its borders, even for
48 * floating point, as the precision is limited.
49 * However, as our tarval module did not support
50 * such kind of operation, we use border flags allowing
53 typedef struct interval_t {
54 ir_tarval *min; /**< lowest border */
55 ir_tarval *max; /**< highest border */
56 unsigned char flags; /**< border flags */
61 #define compare_iv(l_iv, r_iv, relation) compare_iv_dbg(l_iv, r_iv, relation)
64 static tarval *compare_iv_dbg(const interval_t *l_iv, const interval_t *r_iv, ir_relation relation);
67 #define DBG_OUT_TR(l_relation, l_bound, r_relation, r_bound, relation, v) \
68 ir_printf("In %e:\na %= %n && b %= %n ==> a %= b == %s\n", \
69 get_irg_entity(current_ir_graph), \
70 l_relation, l_bound, r_relation, r_bound, relation, v);
73 #define DBG_OUT_R(r_relation, r_bound, left, relation, right, v) \
74 ir_printf("In %e:\na %= %n ==> %n %= %n == %s\n", \
75 get_irg_entity(current_ir_graph), \
76 r_relation, r_bound, left, relation, right, v);
79 #define DBG_OUT_L(l_relation, l_bound, left, relation, right, v) \
80 ir_printf("In %e:\na %= %n ==> %n %= %n == %s\n", \
81 get_irg_entity(current_ir_graph), \
82 l_relation, l_bound, left, relation, right, v);
86 #define DBG_OUT_TR(l_relation, l_bound, r_relation, r_bound, relation, v)
87 #define DBG_OUT_R(r_relation, r_bound, left, relation, right, v)
88 #define DBG_OUT_L(l_relation, l_bound, left, relation, right, v)
90 #endif /* DEBUG_CONFIRM */
93 * Check, if the value of a node is != 0.
95 * This is a often needed case, so we handle here Confirm
98 FIRM_API int value_not_zero(const ir_node *n, ir_node_cnst_ptr *confirm)
100 #define RET_ON(x) if (x) { *confirm = n; return 1; }; break
103 ir_mode *mode = get_irn_mode(n);
104 ir_relation relation;
108 /* there might be several Confirms one after other that form an interval */
111 /* we can safely skip Minus when checking for != 0 */
119 * Note: A Confirm is never after a Const. So,
120 * we simply can check the bound for being a Const
121 * without the fear that is might be hidden by a further Confirm.
123 tv = value_of(get_Confirm_bound(n));
124 if (tv == tarval_bad)
127 relation = tarval_cmp(tv, get_mode_null(mode));
130 * Beware: C might by a NaN. It is not clear, what we should do
131 * than. Of course a NaN is != 0, but we might use this function
132 * to remove up Exceptions, and NaN's might generate Exception.
133 * So, we do NOT handle NaNs here for safety.
135 * Note that only the C != 0 case need additional checking.
137 switch (get_Confirm_relation(n)) {
138 case ir_relation_equal: /* n == C /\ C != 0 ==> n != 0 */
139 RET_ON(relation != ir_relation_equal && relation != ir_relation_unordered);
140 case ir_relation_less_greater: /* n != C /\ C == 0 ==> n != 0 */
141 RET_ON(relation == ir_relation_equal);
142 case ir_relation_less: /* n < C /\ C <= 0 ==> n != 0 */
143 RET_ON(relation == ir_relation_less || relation == ir_relation_equal);
144 case ir_relation_less_equal: /* n <= C /\ C < 0 ==> n != 0 */
145 RET_ON(relation == ir_relation_less);
146 case ir_relation_greater_equal: /* n >= C /\ C > 0 ==> n != 0 */
147 RET_ON(relation == ir_relation_greater);
148 case ir_relation_greater: /* n > C /\ C >= 0 ==> n != 0 */
149 RET_ON(relation == ir_relation_greater || relation == ir_relation_equal);
153 n = get_Confirm_value(n);
157 if (tv == tarval_bad)
160 relation = tarval_cmp(tv, get_mode_null(mode));
162 /* again, need check for NaN */
163 return (relation != ir_relation_equal) && (relation != ir_relation_unordered);
166 } /* value_not_zero */
169 * Check, if the value of a node cannot represent a NULL pointer.
171 * - Casts are skipped
172 * - If sel_based_null_check_elim is enabled, all
173 * Sel nodes can be skipped.
174 * - A SymConst(entity) is NEVER a NULL pointer
175 * - Confirms are evaluated
177 FIRM_API int value_not_null(const ir_node *n, ir_node_cnst_ptr *confirm)
182 n = skip_Cast_const(n);
185 if (tarval_is_constant(tv) && ! tarval_is_null(tv))
188 assert(mode_is_reference(get_irn_mode(n)));
189 if (get_opt_sel_based_null_check_elim()) {
190 /* skip all Sel nodes and Cast's */
192 n = skip_Cast(get_Sel_ptr(n));
196 if (is_Cast(n)) { n = get_Cast_op(n); continue; }
197 if (is_Proj(n)) { n = get_Proj_pred(n); continue; }
202 /* global references are never NULL */
204 } else if (n == get_irg_frame(get_irn_irg(n))) {
205 /* local references are never NULL */
207 } else if (is_Alloc(n)) {
208 /* alloc never returns NULL (it throws an exception instead) */
211 /* check for more Confirms */
212 for (; is_Confirm(n); n = skip_Cast(get_Confirm_value(n))) {
213 if (get_Confirm_relation(n) == ir_relation_less_greater) {
214 ir_node *bound = get_Confirm_bound(n);
215 ir_tarval *tv = value_of(bound);
217 if (tarval_is_null(tv)) {
225 } /* value_not_null */
229 static inline ir_value_classify_sign operator *(ir_value_classify_sign sign, int mul) {
230 return (ir_value_classify_sign) (sign*mul);
236 * Check, if the value of a node can be confirmed >= 0 or <= 0,
237 * If the mode of the value did not honor signed zeros, else
238 * check for >= 0 or < 0.
240 FIRM_API ir_value_classify_sign classify_value_sign(ir_node *n)
244 ir_relation cmp, ncmp;
248 unsigned code = get_irn_opcode(n);
258 return value_classified_unknown;
263 return value_classified_unknown;
265 tv = value_of(get_Confirm_bound(n));
266 if (tv == tarval_bad)
267 return value_classified_unknown;
269 mode = get_irn_mode(n);
272 * We can handle only >=, >, <, <= cases.
273 * We could handle == too, but this will be optimized into
276 * Note that for integer modes we have a slightly better
277 * optimization possibilities, so we handle this
280 cmp = get_Confirm_relation(n);
283 case ir_relation_less:
285 * must be x < c <= 1 to be useful if integer mode and -0 = 0
286 * x < c <= 0 to be useful else
288 case ir_relation_less_equal:
290 * must be x <= c < 1 to be useful if integer mode and -0 = 0
291 * x <= c < 0 to be useful else
293 c = mode_is_int(mode) && mode_honor_signed_zeros(mode) ?
294 get_mode_one(mode) : get_mode_null(mode);
296 ncmp = tarval_cmp(tv, c);
297 if (ncmp == ir_relation_equal)
298 ncmp = ir_relation_less_equal;
300 if (cmp != (ncmp ^ ir_relation_equal))
301 return value_classified_unknown;
304 return value_classified_negative * negate;
306 case ir_relation_greater_equal:
308 * must be x >= c > -1 to be useful if integer mode
309 * x >= c >= 0 to be useful else
311 case ir_relation_greater:
313 * must be x > c >= -1 to be useful if integer mode
314 * x > c >= 0 to be useful else
316 if (mode_is_int(mode)) {
317 c = get_mode_minus_one(mode);
319 ncmp = tarval_cmp(tv, c);
320 if (ncmp == ir_relation_equal)
321 ncmp = ir_relation_greater_equal;
323 if (cmp != (ncmp ^ ir_relation_equal))
324 return value_classified_unknown;
326 c = get_mode_minus_one(mode);
328 ncmp = tarval_cmp(tv, c);
330 if (ncmp != ir_relation_equal && ncmp != ir_relation_greater)
331 return value_classified_unknown;
335 return value_classified_positive * negate;
338 return value_classified_unknown;
340 } /* classify_value_sign */
343 * construct an interval from a value
345 * @return the filled interval or NULL if no interval
346 * can be created (happens only on floating point
348 static interval_t *get_interval_from_tv(interval_t *iv, ir_tarval *tv)
350 ir_mode *mode = get_tarval_mode(tv);
352 if (tv == tarval_bad) {
353 if (mode_is_float(mode)) {
354 /* NaN could be included which we cannot handle */
355 iv->min = tarval_bad;
356 iv->max = tarval_bad;
357 iv->flags = MIN_EXCLUDED | MAX_EXCLUDED;
361 iv->min = get_mode_min(mode);
362 iv->max = get_mode_max(mode);
363 iv->flags = MIN_INCLUDED | MAX_INCLUDED;
368 if (mode_is_float(mode)) {
369 if (tv == get_mode_NAN(mode)) {
370 /* arg, we cannot handle NaN's. */
371 iv->min = tarval_bad;
372 iv->max = tarval_bad;
373 iv->flags = MIN_EXCLUDED | MAX_EXCLUDED;
381 iv->flags = MIN_INCLUDED | MAX_INCLUDED;
384 } /* get_interval_from_tv */
387 * construct an interval from a Confirm
389 * @param iv an empty interval, will be filled
390 * @param bound the bound value
391 * @param relation the Confirm compare relation
393 * @return the filled interval or NULL if no interval
394 * can be created (happens only on floating point
396 static interval_t *get_interval(interval_t *iv, ir_node *bound, ir_relation relation)
398 ir_mode *mode = get_irn_mode(bound);
399 ir_tarval *tv = value_of(bound);
401 if (tv == tarval_bad) {
402 /* There is nothing we could do here. For integer
403 * modes we could return [-oo, +oo], but there is
404 * nothing we could deduct from such an interval.
405 * So, speed things up and return unknown.
407 iv->min = tarval_bad;
408 iv->max = tarval_bad;
409 iv->flags = MIN_EXCLUDED | MAX_EXCLUDED;
413 if (mode_is_float(mode)) {
414 if (tv == get_mode_NAN(mode)) {
415 /* arg, we cannot handle NaN's. */
416 iv->min = tarval_bad;
417 iv->max = tarval_bad;
418 iv->flags = MIN_EXCLUDED | MAX_EXCLUDED;
424 /* check which side is known */
426 case ir_relation_equal:
430 iv->flags = MIN_INCLUDED | MAX_INCLUDED;
433 case ir_relation_less_equal:
435 iv->min = get_mode_min(mode);
437 iv->flags = MIN_INCLUDED | MAX_INCLUDED;
440 case ir_relation_less:
442 iv->min = get_mode_min(mode);
444 iv->flags = MIN_INCLUDED | MAX_EXCLUDED;
447 case ir_relation_greater:
450 iv->max = get_mode_max(mode);
451 iv->flags = MIN_EXCLUDED | MAX_INCLUDED;
454 case ir_relation_greater_equal:
457 iv->max = get_mode_max(mode);
458 iv->flags = MIN_INCLUDED | MAX_INCLUDED;
461 case ir_relation_less_equal_greater:
463 * Ordered means, that at least neither
464 * our bound nor our value ara NaN's
467 iv->min = get_mode_min(mode);
468 iv->max = get_mode_max(mode);
469 iv->flags = MIN_INCLUDED | MAX_INCLUDED;
474 * We do not handle UNORDERED, as a NaN
475 * could be included in the interval.
477 iv->min = tarval_bad;
478 iv->max = tarval_bad;
479 iv->flags = MIN_EXCLUDED | MAX_EXCLUDED;
483 if (iv->min != tarval_bad && iv->max != tarval_bad)
489 * Try to evaluate l_iv relation r_iv.
491 * @param l_iv the left interval
492 * @param r_iv the right interval
493 * @param relation the compare relation
496 * tarval_b_true or tarval_b_false it it can be evaluated,
499 static ir_tarval *(compare_iv)(const interval_t *l_iv, const interval_t *r_iv, ir_relation relation)
503 ir_tarval *tv_true = tarval_b_true, *tv_false = tarval_b_false;
505 /* if one interval contains NaNs, we cannot evaluate anything */
506 if (! l_iv || ! r_iv)
509 /* we can only check ordered relations */
510 if (relation & ir_relation_unordered) {
513 relation = get_negated_relation(relation);
519 /* if we have > or >=, we do the inverse to save some cases */
520 if (relation == ir_relation_greater_equal || relation == ir_relation_greater) {
523 relation = get_inversed_relation(relation);
529 /* now, only the following cases remains */
531 case ir_relation_equal:
532 /* two intervals can be compared for equality only if they are a single value */
533 if (l_iv->min == l_iv->max && r_iv->min == r_iv->max)
534 return tarval_cmp(l_iv->min, r_iv->min) == ir_relation_equal ? tv_true : tv_false;
536 /* if both intervals do not intersect, it is never equal */
537 res = tarval_cmp(l_iv->max, r_iv->min);
539 /* b < c ==> [a,b] != [c,d] */
540 if (res == ir_relation_less)
543 /* b <= c ==> [a,b) != [c,d] AND [a,b] != (c,d] */
544 if ((l_iv->flags & MAX_EXCLUDED || r_iv->flags & MIN_EXCLUDED)
545 && (res == ir_relation_equal))
548 res = tarval_cmp(r_iv->max, l_iv->min);
550 /* d < a ==> [c,d] != [a,b] */
551 if (res == ir_relation_less)
554 /* d <= a ==> [c,d) != [a,b] AND [c,d] != (a,b] */
555 if ((r_iv->flags & MAX_EXCLUDED || l_iv->flags & MIN_EXCLUDED)
556 && (res == ir_relation_equal))
560 case ir_relation_less_greater:
561 /* two intervals can be compared for not equality only if they are a single value */
562 if (l_iv->min == l_iv->max && r_iv->min == r_iv->max)
563 return tarval_cmp(l_iv->min, r_iv->min) != ir_relation_equal ? tv_true : tv_false;
566 case ir_relation_less:
567 res = tarval_cmp(l_iv->max, r_iv->min);
569 /* [a, b] < [c, d] <==> b < c */
570 if (res == ir_relation_less)
573 /* if one border is excluded, b <= c is enough */
574 if ((l_iv->flags & MAX_EXCLUDED || r_iv->flags & MIN_EXCLUDED) &&
575 res == ir_relation_equal)
578 /* [a, b] >= [c, d] <==> a > d */
579 res = tarval_cmp(l_iv->min, r_iv->max);
580 if (res == ir_relation_greater)
583 /* if one border is excluded, a >= d is enough */
584 if ((l_iv->flags & MIN_EXCLUDED || r_iv->flags & MAX_EXCLUDED) &&
585 res == ir_relation_equal)
589 case ir_relation_less_equal:
590 /* [a, b) <= [c, d] or [a, b] <= (c, d] <==> b <= c */
591 flags = (l_iv->flags & MAX_EXCLUDED) | (r_iv->flags & MIN_EXCLUDED);
593 res = tarval_cmp(l_iv->max, r_iv->min);
595 if (res == ir_relation_less || res == ir_relation_equal)
599 res = tarval_cmp(l_iv->min, r_iv->max);
601 /* [a, b] > [c, d] <==> a > d */
602 if (res == ir_relation_greater)
605 /* if one border is excluded, a >= d is enough */
606 if ((l_iv->flags & MIN_EXCLUDED || r_iv->flags & MAX_EXCLUDED) &&
607 res == ir_relation_equal)
611 case ir_relation_less_equal_greater:
612 /* Hmm. if both are intervals, we can find an order */
622 * Returns non-zero, if a given relation is transitive.
624 static int is_transitive(ir_relation relation)
626 return (ir_relation_false < relation && relation < ir_relation_less_greater);
627 } /* is_transitive */
630 * Return the value of a Cmp if one or both predecessors
633 * @param cmp the Cmp node
634 * @param left the left operand of the Cmp
635 * @param right the right operand of the Cmp
636 * @param relation the compare relation
638 FIRM_API ir_tarval *computed_value_Cmp_Confirm(const ir_node *cmp, ir_node *left, ir_node *right, ir_relation relation)
641 ir_relation l_relation, res_relation, neg_relation;
642 interval_t l_iv, r_iv;
645 if (is_Confirm(right)) {
646 /* we want the Confirm on the left side */
651 relation = get_inversed_relation(relation);
652 } else if (! is_Confirm(left)) {
653 /* nothing more found */
655 goto check_null_case;
658 /* ok, here at least left is a Confirm, right might be */
659 l_bound = get_Confirm_bound(left);
660 l_relation = get_Confirm_relation(left);
662 if (is_Confirm(right)) {
664 * both sides are Confirm's. Check some rare cases first.
666 ir_node *r_bound = get_Confirm_bound(right);
667 ir_relation r_relation = get_Confirm_relation(right);
670 * some check can be made WITHOUT constant bounds
672 if (r_bound == l_bound) {
673 if (is_transitive(l_relation)) {
674 ir_relation r_inc_relation = get_inversed_relation(r_relation);
677 * triangle inequality:
679 * a CMP B && B CMP b => a CMP b, !(a ~CMP b)
681 * We handle correctly cases with some <=/>= here
683 if ((l_relation & ~ir_relation_equal) == (r_inc_relation & ~ir_relation_equal)) {
684 res_relation = (l_relation & ~ir_relation_equal) | (l_relation & r_inc_relation & ir_relation_equal);
686 if ((relation == res_relation) || ((relation & ~ir_relation_equal) == res_relation)) {
687 DBG_OUT_TR(l_relation, l_bound, r_relation, r_bound, relation, "true");
688 DBG_EVAL_CONFIRM(cmp);
689 return tarval_b_true;
691 ir_relation neg_relation = get_negated_relation(relation);
693 if ((neg_relation == res_relation) || ((neg_relation & ~ir_relation_equal) == res_relation)) {
694 DBG_OUT_TR(l_relation, l_bound, r_relation, r_bound, relation, "false");
695 DBG_EVAL_CONFIRM(cmp);
696 return tarval_b_false;
704 * Here, we check only the right Confirm, as the left Confirms are
705 * checked later anyway.
707 if (left == r_bound) {
709 * l == bound(r) AND relation(r) == relation:
711 * We know that a CMP b and check for that
713 if ((r_relation == relation) || (r_relation == (relation & ~ir_relation_equal))) {
714 DBG_OUT_R(r_relation, r_bound, left, relation, right, "true");
715 DBG_EVAL_CONFIRM(cmp);
716 return tarval_b_true;
719 * l == bound(r) AND relation(r) != relation:
721 * We know that a CMP b and check for a ~CMP b
724 neg_relation = get_negated_relation(relation);
726 if ((r_relation == neg_relation) || (r_relation == (neg_relation & ~ir_relation_equal))) {
727 DBG_OUT_R(r_relation, r_bound, left, relation, right, "false");
728 DBG_EVAL_CONFIRM(cmp);
729 return tarval_b_false;
734 /* now, try interval magic */
736 get_interval(&l_iv, l_bound, l_relation),
737 get_interval(&r_iv, r_bound, r_relation),
740 if (tv != tarval_bad) {
741 DBG_EVAL_CONFIRM(cmp);
746 /* from Here, check only left Confirm */
749 * some checks can be made WITHOUT constant bounds
751 if (right == l_bound) {
753 * r == bound(l) AND relation(l) == relation:
755 * We know that a CMP b and check for that
757 if ((l_relation == relation) || (l_relation == (relation & ~ir_relation_equal))) {
758 DBG_OUT_L(l_relation, l_bound, left, relation, right, "true");
759 DBG_EVAL_CONFIRM(cmp);
760 return tarval_b_true;
763 * r == bound(l) AND relation(l) is Not(relation):
765 * We know that a CMP b and check for a ~CMP b
768 neg_relation = get_negated_relation(relation);
770 if ((l_relation == neg_relation) || (l_relation == (neg_relation & ~ir_relation_equal))) {
771 DBG_OUT_L(l_relation, l_bound, left, relation, right, "false");
772 DBG_EVAL_CONFIRM(cmp);
773 return tarval_b_false;
778 /* now, only right == Const can help */
779 tv = value_of(right);
781 if (tv != tarval_bad) {
783 get_interval(&l_iv, l_bound, l_relation),
784 get_interval_from_tv(&r_iv, tv),
788 /* check some other cases */
789 if ((relation == ir_relation_equal || relation == ir_relation_less_greater) &&
790 is_Const(right) && is_Const_null(right)) {
791 /* for == 0 or != 0 we have some special tools */
792 ir_mode *mode = get_irn_mode(left);
793 const ir_node *dummy;
794 if (mode_is_reference(mode)) {
795 if (value_not_null(left, &dummy)) {
796 tv = relation == ir_relation_equal ? tarval_b_false : tarval_b_true;
799 if (value_not_zero(left, &dummy)) {
800 tv = relation == ir_relation_equal ? tarval_b_false : tarval_b_true;
806 if (tv != tarval_bad)
807 DBG_EVAL_CONFIRM(cmp);
810 } /* computed_value_Cmp_Confirm */
814 * For debugging. Prints an interval into a string.
816 * @param buf address of a string buffer
817 * @param len length of the string buffer
818 * @param iv the interval
820 static int iv_snprintf(char *buf, size_t len, const interval_t *iv)
822 char smin[64], smax[64];
825 tarval_snprintf(smin, sizeof(smin), iv->min);
827 if (iv->min != iv->max || (iv->flags & (MIN_EXCLUDED|MAX_EXCLUDED))) {
828 tarval_snprintf(smax, sizeof(smax), iv->max);
830 return snprintf(buf, len, "%c%s, %s%c",
831 iv->flags & MIN_EXCLUDED ? '(' : '[',
833 iv->flags & MAX_EXCLUDED ? ')' : ']'
836 return snprintf(buf, len, "%s", smin);
838 return snprintf(buf, len, "<UNKNOWN>");
842 * For debugging. Prints an interval compare.
844 * @param l_iv the left interval
845 * @param r_iv the right interval
846 * @param relation the compare relation
848 static void print_iv_cmp(const interval_t *l_iv, const interval_t *r_iv, ir_relation relation)
850 char sl[128], sr[128];
852 iv_snprintf(sl, sizeof(sl), l_iv);
853 iv_snprintf(sr, sizeof(sr), r_iv);
855 ir_printf("%s %= %s", sl, relation, sr);
859 * For debugging. call *compare_iv() and prints inputs and result.
861 * @param l_iv the left interval
862 * @param r_iv the right interval
863 * @param relation the compare relation
865 static tarval *compare_iv_dbg(const interval_t *l_iv, const interval_t *r_iv, ir_relation relation)
867 tarval *tv = (compare_iv)(l_iv, r_iv, relation);
869 if (tv == tarval_bad)
872 ir_printf("In %e:\n", get_irg_entity(current_ir_graph));
873 print_iv_cmp(l_iv, r_iv, relation);
874 ir_printf(" = %T\n", tv);
876 } /* compare_iv_dbg */
878 #endif /* DEBUG_CONFIRM */