1 /* Support routines for value ranges.
2 Copyright (C) 2019-2020 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3, or (at your option)
11 GCC is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
22 #include "coretypes.h"
27 #include "tree-pretty-print.h"
28 #include "fold-const.h"
30 value_range::value_range (tree min
, tree max
, value_range_kind kind
)
35 value_range::value_range (tree type
)
40 value_range::value_range (tree type
,
41 const wide_int
&wmin
, const wide_int
&wmax
,
42 enum value_range_kind kind
)
44 tree min
= wide_int_to_tree (type
, wmin
);
45 tree max
= wide_int_to_tree (type
, wmax
);
46 gcc_checking_assert (kind
== VR_RANGE
|| kind
== VR_ANTI_RANGE
);
51 value_range::set_undefined ()
53 m_kind
= VR_UNDEFINED
;
58 value_range::set_varying (tree type
)
61 if (supports_type_p (type
))
63 m_min
= vrp_val_min (type
);
64 m_max
= vrp_val_max (type
);
67 /* We can't do anything range-wise with these types. */
68 m_min
= m_max
= error_mark_node
;
71 /* Set value range to the canonical form of {VRTYPE, MIN, MAX, EQUIV}.
72 This means adjusting VRTYPE, MIN and MAX representing the case of a
73 wrapping range with MAX < MIN covering [MIN, type_max] U [type_min, MAX]
74 as anti-rage ~[MAX+1, MIN-1]. Likewise for wrapping anti-ranges.
75 In corner cases where MAX+1 or MIN-1 wraps this will fall back
77 This routine exists to ease canonicalization in the case where we
78 extract ranges from var + CST op limit. */
81 value_range::set (tree min
, tree max
, value_range_kind kind
)
83 /* Use the canonical setters for VR_UNDEFINED and VR_VARYING. */
84 if (kind
== VR_UNDEFINED
)
89 else if (kind
== VR_VARYING
)
91 gcc_assert (TREE_TYPE (min
) == TREE_TYPE (max
));
92 tree typ
= TREE_TYPE (min
);
93 if (supports_type_p (typ
))
95 gcc_assert (vrp_val_min (typ
));
96 gcc_assert (vrp_val_max (typ
));
102 /* Convert POLY_INT_CST bounds into worst-case INTEGER_CST bounds. */
103 if (POLY_INT_CST_P (min
))
105 tree type_min
= vrp_val_min (TREE_TYPE (min
));
107 = constant_lower_bound_with_limit (wi::to_poly_widest (min
),
108 wi::to_widest (type_min
));
109 min
= wide_int_to_tree (TREE_TYPE (min
), lb
);
111 if (POLY_INT_CST_P (max
))
113 tree type_max
= vrp_val_max (TREE_TYPE (max
));
115 = constant_upper_bound_with_limit (wi::to_poly_widest (max
),
116 wi::to_widest (type_max
));
117 max
= wide_int_to_tree (TREE_TYPE (max
), ub
);
120 /* Nothing to canonicalize for symbolic ranges. */
121 if (TREE_CODE (min
) != INTEGER_CST
122 || TREE_CODE (max
) != INTEGER_CST
)
130 /* Wrong order for min and max, to swap them and the VR type we need
132 if (tree_int_cst_lt (max
, min
))
136 /* For one bit precision if max < min, then the swapped
137 range covers all values, so for VR_RANGE it is varying and
138 for VR_ANTI_RANGE empty range, so drop to varying as well. */
139 if (TYPE_PRECISION (TREE_TYPE (min
)) == 1)
141 set_varying (TREE_TYPE (min
));
145 one
= build_int_cst (TREE_TYPE (min
), 1);
146 tmp
= int_const_binop (PLUS_EXPR
, max
, one
);
147 max
= int_const_binop (MINUS_EXPR
, min
, one
);
150 /* There's one corner case, if we had [C+1, C] before we now have
151 that again. But this represents an empty value range, so drop
152 to varying in this case. */
153 if (tree_int_cst_lt (max
, min
))
155 set_varying (TREE_TYPE (min
));
159 kind
= kind
== VR_RANGE
? VR_ANTI_RANGE
: VR_RANGE
;
162 tree type
= TREE_TYPE (min
);
164 /* Anti-ranges that can be represented as ranges should be so. */
165 if (kind
== VR_ANTI_RANGE
)
167 /* For -fstrict-enums we may receive out-of-range ranges so consider
168 values < -INF and values > INF as -INF/INF as well. */
169 bool is_min
= vrp_val_is_min (min
);
170 bool is_max
= vrp_val_is_max (max
);
172 if (is_min
&& is_max
)
174 /* We cannot deal with empty ranges, drop to varying.
175 ??? This could be VR_UNDEFINED instead. */
179 else if (TYPE_PRECISION (TREE_TYPE (min
)) == 1
180 && (is_min
|| is_max
))
182 /* Non-empty boolean ranges can always be represented
183 as a singleton range. */
185 min
= max
= vrp_val_max (TREE_TYPE (min
));
187 min
= max
= vrp_val_min (TREE_TYPE (min
));
192 tree one
= build_int_cst (TREE_TYPE (max
), 1);
193 min
= int_const_binop (PLUS_EXPR
, max
, one
);
194 max
= vrp_val_max (TREE_TYPE (max
));
199 tree one
= build_int_cst (TREE_TYPE (min
), 1);
200 max
= int_const_binop (MINUS_EXPR
, min
, one
);
201 min
= vrp_val_min (TREE_TYPE (min
));
206 /* Normalize [MIN, MAX] into VARYING and ~[MIN, MAX] into UNDEFINED.
208 Avoid using TYPE_{MIN,MAX}_VALUE because -fstrict-enums can
209 restrict those to a subset of what actually fits in the type.
210 Instead use the extremes of the type precision which will allow
211 compare_range_with_value() to check if a value is inside a range,
212 whereas if we used TYPE_*_VAL, said function would just punt
213 upon seeing a VARYING. */
214 unsigned prec
= TYPE_PRECISION (type
);
215 signop sign
= TYPE_SIGN (type
);
216 if (wi::eq_p (wi::to_wide (min
), wi::min_value (prec
, sign
))
217 && wi::eq_p (wi::to_wide (max
), wi::max_value (prec
, sign
)))
219 if (kind
== VR_RANGE
)
221 else if (kind
== VR_ANTI_RANGE
)
228 /* Do not drop [-INF(OVF), +INF(OVF)] to varying. (OVF) has to be sticky
229 to make sure VRP iteration terminates, otherwise we can get into
240 value_range::set (tree val
)
242 gcc_assert (TREE_CODE (val
) == SSA_NAME
|| is_gimple_min_invariant (val
));
243 if (TREE_OVERFLOW_P (val
))
244 val
= drop_tree_overflow (val
);
248 /* Set value range VR to a nonzero range of type TYPE. */
251 value_range::set_nonzero (tree type
)
253 tree zero
= build_int_cst (type
, 0);
254 set (zero
, zero
, VR_ANTI_RANGE
);
257 /* Set value range VR to a ZERO range of type TYPE. */
260 value_range::set_zero (tree type
)
262 set (build_int_cst (type
, 0));
265 /* Check the validity of the range. */
268 value_range::check ()
275 gcc_assert (m_min
&& m_max
);
276 gcc_assert (!TREE_OVERFLOW_P (m_min
) && !TREE_OVERFLOW_P (m_max
));
278 /* Creating ~[-MIN, +MAX] is stupid because that would be
280 if (INTEGRAL_TYPE_P (TREE_TYPE (m_min
)) && m_kind
== VR_ANTI_RANGE
)
281 gcc_assert (!vrp_val_is_min (m_min
) || !vrp_val_is_max (m_max
));
283 int cmp
= compare_values (m_min
, m_max
);
284 gcc_assert (cmp
== 0 || cmp
== -1 || cmp
== -2);
288 gcc_assert (!min () && !max ());
291 gcc_assert (m_min
&& m_max
);
298 /* Return the number of sub-ranges in a range. */
301 value_range::num_pairs () const
309 value_range
numeric_range (*this);
310 numeric_range
.normalize_symbolics ();
311 return numeric_range
.num_pairs ();
313 if (m_kind
== VR_ANTI_RANGE
)
315 // ~[MIN, X] has one sub-range of [X+1, MAX], and
316 // ~[X, MAX] has one sub-range of [MIN, X-1].
317 if (vrp_val_is_min (m_min
) || vrp_val_is_max (m_max
))
324 /* Return the lower bound for a sub-range. PAIR is the sub-range in
328 value_range::lower_bound (unsigned pair
) const
332 value_range
numeric_range (*this);
333 numeric_range
.normalize_symbolics ();
334 return numeric_range
.lower_bound (pair
);
337 gcc_checking_assert (!undefined_p ());
338 gcc_checking_assert (pair
+ 1 <= num_pairs ());
340 if (m_kind
== VR_ANTI_RANGE
)
343 if (pair
== 1 || vrp_val_is_min (m_min
))
344 t
= wide_int_to_tree (typ
, wi::to_wide (m_max
) + 1);
346 t
= vrp_val_min (typ
);
350 return wi::to_wide (t
);
353 /* Return the upper bound for a sub-range. PAIR is the sub-range in
357 value_range::upper_bound (unsigned pair
) const
361 value_range
numeric_range (*this);
362 numeric_range
.normalize_symbolics ();
363 return numeric_range
.upper_bound (pair
);
366 gcc_checking_assert (!undefined_p ());
367 gcc_checking_assert (pair
+ 1 <= num_pairs ());
369 if (m_kind
== VR_ANTI_RANGE
)
372 if (pair
== 1 || vrp_val_is_min (m_min
))
373 t
= vrp_val_max (typ
);
375 t
= wide_int_to_tree (typ
, wi::to_wide (m_min
) - 1);
379 return wi::to_wide (t
);
382 /* Return the highest bound in a range. */
385 value_range::upper_bound () const
387 unsigned pairs
= num_pairs ();
388 gcc_checking_assert (pairs
> 0);
389 return upper_bound (pairs
- 1);
393 value_range::equal_p (const value_range
&other
) const
395 /* Ignore types for undefined. All undefines are equal. */
397 return m_kind
== other
.m_kind
;
399 return (m_kind
== other
.m_kind
400 && vrp_operand_equal_p (m_min
, other
.m_min
)
401 && vrp_operand_equal_p (m_max
, other
.m_max
));
405 value_range::operator== (const value_range
&r
) const
410 /* If range is a singleton, place it in RESULT and return TRUE.
411 Note: A singleton can be any gimple invariant, not just constants.
412 So, [&x, &x] counts as a singleton. */
413 /* Return TRUE if this is a symbolic range. */
416 value_range::symbolic_p () const
418 return (!varying_p ()
420 && (!is_gimple_min_invariant (m_min
)
421 || !is_gimple_min_invariant (m_max
)));
424 /* NOTE: This is not the inverse of symbolic_p because the range
425 could also be varying or undefined. Ideally they should be inverse
426 of each other, with varying only applying to symbolics. Varying of
427 constants would be represented as [-MIN, +MAX]. */
430 value_range::constant_p () const
432 return (!varying_p ()
434 && TREE_CODE (m_min
) == INTEGER_CST
435 && TREE_CODE (m_max
) == INTEGER_CST
);
439 value_range::singleton_p (tree
*result
) const
441 if (m_kind
== VR_ANTI_RANGE
)
445 if (TYPE_PRECISION (type ()) == 1)
453 if (num_pairs () == 1)
455 value_range vr0
, vr1
;
456 ranges_from_anti_range (this, &vr0
, &vr1
);
457 return vr0
.singleton_p (result
);
460 if (m_kind
== VR_RANGE
461 && vrp_operand_equal_p (min (), max ())
462 && is_gimple_min_invariant (min ()))
471 /* Return 1 if VAL is inside value range.
472 0 if VAL is not inside value range.
473 -2 if we cannot tell either way.
475 Benchmark compile/20001226-1.c compilation time after changing this
479 value_range::value_inside_range (tree val
) const
489 cmp1
= operand_less_p (val
, m_min
);
493 return m_kind
!= VR_RANGE
;
495 cmp2
= operand_less_p (m_max
, val
);
499 if (m_kind
== VR_RANGE
)
505 /* Return TRUE if it is possible that range contains VAL. */
508 value_range::may_contain_p (tree val
) const
510 return value_inside_range (val
) != 0;
513 /* Return TRUE if range contains INTEGER_CST. */
516 value_range::contains_p (tree cst
) const
518 gcc_checking_assert (TREE_CODE (cst
) == INTEGER_CST
);
521 value_range
numeric_range (*this);
522 numeric_range
.normalize_symbolics ();
523 return numeric_range
.contains_p (cst
);
525 return value_inside_range (cst
) == 1;
528 /* Normalize addresses into constants. */
531 value_range::normalize_addresses ()
536 if (!POINTER_TYPE_P (type ()) || range_has_numeric_bounds_p (this))
539 if (!range_includes_zero_p (this))
541 gcc_checking_assert (TREE_CODE (m_min
) == ADDR_EXPR
542 || TREE_CODE (m_max
) == ADDR_EXPR
);
543 set_nonzero (type ());
546 set_varying (type ());
549 /* Normalize symbolics and addresses into constants. */
552 value_range::normalize_symbolics ()
554 if (varying_p () || undefined_p ())
557 tree ttype
= type ();
558 bool min_symbolic
= !is_gimple_min_invariant (min ());
559 bool max_symbolic
= !is_gimple_min_invariant (max ());
560 if (!min_symbolic
&& !max_symbolic
)
562 normalize_addresses ();
566 // [SYM, SYM] -> VARYING
567 if (min_symbolic
&& max_symbolic
)
572 if (kind () == VR_RANGE
)
574 // [SYM, NUM] -> [-MIN, NUM]
577 set (vrp_val_min (ttype
), max ());
580 // [NUM, SYM] -> [NUM, +MAX]
581 set (min (), vrp_val_max (ttype
));
584 gcc_checking_assert (kind () == VR_ANTI_RANGE
);
585 // ~[SYM, NUM] -> [NUM + 1, +MAX]
588 if (!vrp_val_is_max (max ()))
590 tree n
= wide_int_to_tree (ttype
, wi::to_wide (max ()) + 1);
591 set (n
, vrp_val_max (ttype
));
597 // ~[NUM, SYM] -> [-MIN, NUM - 1]
598 if (!vrp_val_is_min (min ()))
600 tree n
= wide_int_to_tree (ttype
, wi::to_wide (min ()) - 1);
601 set (vrp_val_min (ttype
), n
);
607 /* Intersect the two value-ranges { *VR0TYPE, *VR0MIN, *VR0MAX } and
608 { VR1TYPE, VR0MIN, VR0MAX } and store the result
609 in { *VR0TYPE, *VR0MIN, *VR0MAX }. This may not be the smallest
610 possible such range. The resulting range is not canonicalized. */
613 intersect_ranges (enum value_range_kind
*vr0type
,
614 tree
*vr0min
, tree
*vr0max
,
615 enum value_range_kind vr1type
,
616 tree vr1min
, tree vr1max
)
618 bool mineq
= vrp_operand_equal_p (*vr0min
, vr1min
);
619 bool maxeq
= vrp_operand_equal_p (*vr0max
, vr1max
);
621 /* [] is vr0, () is vr1 in the following classification comments. */
625 if (*vr0type
== vr1type
)
626 /* Nothing to do for equal ranges. */
628 else if ((*vr0type
== VR_RANGE
629 && vr1type
== VR_ANTI_RANGE
)
630 || (*vr0type
== VR_ANTI_RANGE
631 && vr1type
== VR_RANGE
))
633 /* For anti-range with range intersection the result is empty. */
634 *vr0type
= VR_UNDEFINED
;
641 else if (operand_less_p (*vr0max
, vr1min
) == 1
642 || operand_less_p (vr1max
, *vr0min
) == 1)
644 /* [ ] ( ) or ( ) [ ]
645 If the ranges have an empty intersection, the result of the
646 intersect operation is the range for intersecting an
647 anti-range with a range or empty when intersecting two ranges. */
648 if (*vr0type
== VR_RANGE
649 && vr1type
== VR_ANTI_RANGE
)
651 else if (*vr0type
== VR_ANTI_RANGE
652 && vr1type
== VR_RANGE
)
658 else if (*vr0type
== VR_RANGE
659 && vr1type
== VR_RANGE
)
661 *vr0type
= VR_UNDEFINED
;
665 else if (*vr0type
== VR_ANTI_RANGE
666 && vr1type
== VR_ANTI_RANGE
)
668 /* If the anti-ranges are adjacent to each other merge them. */
669 if (TREE_CODE (*vr0max
) == INTEGER_CST
670 && TREE_CODE (vr1min
) == INTEGER_CST
671 && operand_less_p (*vr0max
, vr1min
) == 1
672 && integer_onep (int_const_binop (MINUS_EXPR
,
675 else if (TREE_CODE (vr1max
) == INTEGER_CST
676 && TREE_CODE (*vr0min
) == INTEGER_CST
677 && operand_less_p (vr1max
, *vr0min
) == 1
678 && integer_onep (int_const_binop (MINUS_EXPR
,
681 /* Else arbitrarily take VR0. */
684 else if ((maxeq
|| operand_less_p (vr1max
, *vr0max
) == 1)
685 && (mineq
|| operand_less_p (*vr0min
, vr1min
) == 1))
687 /* [ ( ) ] or [( ) ] or [ ( )] */
688 if (*vr0type
== VR_RANGE
689 && vr1type
== VR_RANGE
)
691 /* If both are ranges the result is the inner one. */
696 else if (*vr0type
== VR_RANGE
697 && vr1type
== VR_ANTI_RANGE
)
699 /* Choose the right gap if the left one is empty. */
702 if (TREE_CODE (vr1max
) != INTEGER_CST
)
704 else if (TYPE_PRECISION (TREE_TYPE (vr1max
)) == 1
705 && !TYPE_UNSIGNED (TREE_TYPE (vr1max
)))
707 = int_const_binop (MINUS_EXPR
, vr1max
,
708 build_int_cst (TREE_TYPE (vr1max
), -1));
711 = int_const_binop (PLUS_EXPR
, vr1max
,
712 build_int_cst (TREE_TYPE (vr1max
), 1));
714 /* Choose the left gap if the right one is empty. */
717 if (TREE_CODE (vr1min
) != INTEGER_CST
)
719 else if (TYPE_PRECISION (TREE_TYPE (vr1min
)) == 1
720 && !TYPE_UNSIGNED (TREE_TYPE (vr1min
)))
722 = int_const_binop (PLUS_EXPR
, vr1min
,
723 build_int_cst (TREE_TYPE (vr1min
), -1));
726 = int_const_binop (MINUS_EXPR
, vr1min
,
727 build_int_cst (TREE_TYPE (vr1min
), 1));
729 /* Choose the anti-range if the range is effectively varying. */
730 else if (vrp_val_is_min (*vr0min
)
731 && vrp_val_is_max (*vr0max
))
737 /* Else choose the range. */
739 else if (*vr0type
== VR_ANTI_RANGE
740 && vr1type
== VR_ANTI_RANGE
)
741 /* If both are anti-ranges the result is the outer one. */
743 else if (*vr0type
== VR_ANTI_RANGE
744 && vr1type
== VR_RANGE
)
746 /* The intersection is empty. */
747 *vr0type
= VR_UNDEFINED
;
754 else if ((maxeq
|| operand_less_p (*vr0max
, vr1max
) == 1)
755 && (mineq
|| operand_less_p (vr1min
, *vr0min
) == 1))
757 /* ( [ ] ) or ([ ] ) or ( [ ]) */
758 if (*vr0type
== VR_RANGE
759 && vr1type
== VR_RANGE
)
760 /* Choose the inner range. */
762 else if (*vr0type
== VR_ANTI_RANGE
763 && vr1type
== VR_RANGE
)
765 /* Choose the right gap if the left is empty. */
769 if (TREE_CODE (*vr0max
) != INTEGER_CST
)
771 else if (TYPE_PRECISION (TREE_TYPE (*vr0max
)) == 1
772 && !TYPE_UNSIGNED (TREE_TYPE (*vr0max
)))
774 = int_const_binop (MINUS_EXPR
, *vr0max
,
775 build_int_cst (TREE_TYPE (*vr0max
), -1));
778 = int_const_binop (PLUS_EXPR
, *vr0max
,
779 build_int_cst (TREE_TYPE (*vr0max
), 1));
782 /* Choose the left gap if the right is empty. */
786 if (TREE_CODE (*vr0min
) != INTEGER_CST
)
788 else if (TYPE_PRECISION (TREE_TYPE (*vr0min
)) == 1
789 && !TYPE_UNSIGNED (TREE_TYPE (*vr0min
)))
791 = int_const_binop (PLUS_EXPR
, *vr0min
,
792 build_int_cst (TREE_TYPE (*vr0min
), -1));
795 = int_const_binop (MINUS_EXPR
, *vr0min
,
796 build_int_cst (TREE_TYPE (*vr0min
), 1));
799 /* Choose the anti-range if the range is effectively varying. */
800 else if (vrp_val_is_min (vr1min
)
801 && vrp_val_is_max (vr1max
))
803 /* Choose the anti-range if it is ~[0,0], that range is special
804 enough to special case when vr1's range is relatively wide.
805 At least for types bigger than int - this covers pointers
806 and arguments to functions like ctz. */
807 else if (*vr0min
== *vr0max
808 && integer_zerop (*vr0min
)
809 && ((TYPE_PRECISION (TREE_TYPE (*vr0min
))
810 >= TYPE_PRECISION (integer_type_node
))
811 || POINTER_TYPE_P (TREE_TYPE (*vr0min
)))
812 && TREE_CODE (vr1max
) == INTEGER_CST
813 && TREE_CODE (vr1min
) == INTEGER_CST
814 && (wi::clz (wi::to_wide (vr1max
) - wi::to_wide (vr1min
))
815 < TYPE_PRECISION (TREE_TYPE (*vr0min
)) / 2))
817 /* Else choose the range. */
825 else if (*vr0type
== VR_ANTI_RANGE
826 && vr1type
== VR_ANTI_RANGE
)
828 /* If both are anti-ranges the result is the outer one. */
833 else if (vr1type
== VR_ANTI_RANGE
834 && *vr0type
== VR_RANGE
)
836 /* The intersection is empty. */
837 *vr0type
= VR_UNDEFINED
;
844 else if ((operand_less_p (vr1min
, *vr0max
) == 1
845 || operand_equal_p (vr1min
, *vr0max
, 0))
846 && operand_less_p (*vr0min
, vr1min
) == 1)
848 /* [ ( ] ) or [ ]( ) */
849 if (*vr0type
== VR_ANTI_RANGE
850 && vr1type
== VR_ANTI_RANGE
)
852 else if (*vr0type
== VR_RANGE
853 && vr1type
== VR_RANGE
)
855 else if (*vr0type
== VR_RANGE
856 && vr1type
== VR_ANTI_RANGE
)
858 if (TREE_CODE (vr1min
) == INTEGER_CST
)
859 *vr0max
= int_const_binop (MINUS_EXPR
, vr1min
,
860 build_int_cst (TREE_TYPE (vr1min
), 1));
864 else if (*vr0type
== VR_ANTI_RANGE
865 && vr1type
== VR_RANGE
)
868 if (TREE_CODE (*vr0max
) == INTEGER_CST
)
869 *vr0min
= int_const_binop (PLUS_EXPR
, *vr0max
,
870 build_int_cst (TREE_TYPE (*vr0max
), 1));
878 else if ((operand_less_p (*vr0min
, vr1max
) == 1
879 || operand_equal_p (*vr0min
, vr1max
, 0))
880 && operand_less_p (vr1min
, *vr0min
) == 1)
882 /* ( [ ) ] or ( )[ ] */
883 if (*vr0type
== VR_ANTI_RANGE
884 && vr1type
== VR_ANTI_RANGE
)
886 else if (*vr0type
== VR_RANGE
887 && vr1type
== VR_RANGE
)
889 else if (*vr0type
== VR_RANGE
890 && vr1type
== VR_ANTI_RANGE
)
892 if (TREE_CODE (vr1max
) == INTEGER_CST
)
893 *vr0min
= int_const_binop (PLUS_EXPR
, vr1max
,
894 build_int_cst (TREE_TYPE (vr1max
), 1));
898 else if (*vr0type
== VR_ANTI_RANGE
899 && vr1type
== VR_RANGE
)
902 if (TREE_CODE (*vr0min
) == INTEGER_CST
)
903 *vr0max
= int_const_binop (MINUS_EXPR
, *vr0min
,
904 build_int_cst (TREE_TYPE (*vr0min
), 1));
913 /* If we know the intersection is empty, there's no need to
914 conservatively add anything else to the set. */
915 if (*vr0type
== VR_UNDEFINED
)
918 /* As a fallback simply use { *VRTYPE, *VR0MIN, *VR0MAX } as
919 result for the intersection. That's always a conservative
920 correct estimate unless VR1 is a constant singleton range
921 in which case we choose that. */
922 if (vr1type
== VR_RANGE
923 && is_gimple_min_invariant (vr1min
)
924 && vrp_operand_equal_p (vr1min
, vr1max
))
932 /* Helper for the intersection operation for value ranges. Given two
933 value ranges VR0 and VR1, return the intersection of the two
934 ranges. This may not be the smallest possible such range. */
937 value_range::intersect_helper (const value_range
*vr0
, const value_range
*vr1
)
939 /* If either range is VR_VARYING the other one wins. */
940 if (vr1
->varying_p ())
942 if (vr0
->varying_p ())
945 /* When either range is VR_UNDEFINED the resulting range is
946 VR_UNDEFINED, too. */
947 if (vr0
->undefined_p ())
949 if (vr1
->undefined_p ())
952 value_range_kind vr0kind
= vr0
->kind ();
953 tree vr0min
= vr0
->min ();
954 tree vr0max
= vr0
->max ();
955 intersect_ranges (&vr0kind
, &vr0min
, &vr0max
,
956 vr1
->kind (), vr1
->min (), vr1
->max ());
957 /* Make sure to canonicalize the result though as the inversion of a
958 VR_RANGE can still be a VR_RANGE. Work on a temporary so we can
959 fall back to vr0 when this turns things to varying. */
961 if (vr0kind
== VR_UNDEFINED
)
962 tem
.set_undefined ();
963 else if (vr0kind
== VR_VARYING
)
964 tem
.set_varying (vr0
->type ());
966 tem
.set (vr0min
, vr0max
, vr0kind
);
967 /* If that failed, use the saved original VR0. */
968 if (tem
.varying_p ())
974 /* Union the two value-ranges { *VR0TYPE, *VR0MIN, *VR0MAX } and
975 { VR1TYPE, VR0MIN, VR0MAX } and store the result
976 in { *VR0TYPE, *VR0MIN, *VR0MAX }. This may not be the smallest
977 possible such range. The resulting range is not canonicalized. */
980 union_ranges (enum value_range_kind
*vr0type
,
981 tree
*vr0min
, tree
*vr0max
,
982 enum value_range_kind vr1type
,
983 tree vr1min
, tree vr1max
)
985 int cmpmin
= compare_values (*vr0min
, vr1min
);
986 int cmpmax
= compare_values (*vr0max
, vr1max
);
987 bool mineq
= cmpmin
== 0;
988 bool maxeq
= cmpmax
== 0;
990 /* [] is vr0, () is vr1 in the following classification comments. */
994 if (*vr0type
== vr1type
)
995 /* Nothing to do for equal ranges. */
997 else if ((*vr0type
== VR_RANGE
998 && vr1type
== VR_ANTI_RANGE
)
999 || (*vr0type
== VR_ANTI_RANGE
1000 && vr1type
== VR_RANGE
))
1002 /* For anti-range with range union the result is varying. */
1008 else if (operand_less_p (*vr0max
, vr1min
) == 1
1009 || operand_less_p (vr1max
, *vr0min
) == 1)
1011 /* [ ] ( ) or ( ) [ ]
1012 If the ranges have an empty intersection, result of the union
1013 operation is the anti-range or if both are anti-ranges
1015 if (*vr0type
== VR_ANTI_RANGE
1016 && vr1type
== VR_ANTI_RANGE
)
1018 else if (*vr0type
== VR_ANTI_RANGE
1019 && vr1type
== VR_RANGE
)
1021 else if (*vr0type
== VR_RANGE
1022 && vr1type
== VR_ANTI_RANGE
)
1028 else if (*vr0type
== VR_RANGE
1029 && vr1type
== VR_RANGE
)
1031 /* The result is the convex hull of both ranges. */
1032 if (operand_less_p (*vr0max
, vr1min
) == 1)
1034 /* If the result can be an anti-range, create one. */
1035 if (TREE_CODE (*vr0max
) == INTEGER_CST
1036 && TREE_CODE (vr1min
) == INTEGER_CST
1037 && vrp_val_is_min (*vr0min
)
1038 && vrp_val_is_max (vr1max
))
1040 tree min
= int_const_binop (PLUS_EXPR
,
1042 build_int_cst (TREE_TYPE (*vr0max
), 1));
1043 tree max
= int_const_binop (MINUS_EXPR
,
1045 build_int_cst (TREE_TYPE (vr1min
), 1));
1046 if (!operand_less_p (max
, min
))
1048 *vr0type
= VR_ANTI_RANGE
;
1060 /* If the result can be an anti-range, create one. */
1061 if (TREE_CODE (vr1max
) == INTEGER_CST
1062 && TREE_CODE (*vr0min
) == INTEGER_CST
1063 && vrp_val_is_min (vr1min
)
1064 && vrp_val_is_max (*vr0max
))
1066 tree min
= int_const_binop (PLUS_EXPR
,
1068 build_int_cst (TREE_TYPE (vr1max
), 1));
1069 tree max
= int_const_binop (MINUS_EXPR
,
1071 build_int_cst (TREE_TYPE (*vr0min
), 1));
1072 if (!operand_less_p (max
, min
))
1074 *vr0type
= VR_ANTI_RANGE
;
1088 else if ((maxeq
|| cmpmax
== 1)
1089 && (mineq
|| cmpmin
== -1))
1091 /* [ ( ) ] or [( ) ] or [ ( )] */
1092 if (*vr0type
== VR_RANGE
1093 && vr1type
== VR_RANGE
)
1095 else if (*vr0type
== VR_ANTI_RANGE
1096 && vr1type
== VR_ANTI_RANGE
)
1102 else if (*vr0type
== VR_ANTI_RANGE
1103 && vr1type
== VR_RANGE
)
1105 /* Arbitrarily choose the right or left gap. */
1106 if (!mineq
&& TREE_CODE (vr1min
) == INTEGER_CST
)
1107 *vr0max
= int_const_binop (MINUS_EXPR
, vr1min
,
1108 build_int_cst (TREE_TYPE (vr1min
), 1));
1109 else if (!maxeq
&& TREE_CODE (vr1max
) == INTEGER_CST
)
1110 *vr0min
= int_const_binop (PLUS_EXPR
, vr1max
,
1111 build_int_cst (TREE_TYPE (vr1max
), 1));
1115 else if (*vr0type
== VR_RANGE
1116 && vr1type
== VR_ANTI_RANGE
)
1117 /* The result covers everything. */
1122 else if ((maxeq
|| cmpmax
== -1)
1123 && (mineq
|| cmpmin
== 1))
1125 /* ( [ ] ) or ([ ] ) or ( [ ]) */
1126 if (*vr0type
== VR_RANGE
1127 && vr1type
== VR_RANGE
)
1133 else if (*vr0type
== VR_ANTI_RANGE
1134 && vr1type
== VR_ANTI_RANGE
)
1136 else if (*vr0type
== VR_RANGE
1137 && vr1type
== VR_ANTI_RANGE
)
1139 *vr0type
= VR_ANTI_RANGE
;
1140 if (!mineq
&& TREE_CODE (*vr0min
) == INTEGER_CST
)
1142 *vr0max
= int_const_binop (MINUS_EXPR
, *vr0min
,
1143 build_int_cst (TREE_TYPE (*vr0min
), 1));
1146 else if (!maxeq
&& TREE_CODE (*vr0max
) == INTEGER_CST
)
1148 *vr0min
= int_const_binop (PLUS_EXPR
, *vr0max
,
1149 build_int_cst (TREE_TYPE (*vr0max
), 1));
1155 else if (*vr0type
== VR_ANTI_RANGE
1156 && vr1type
== VR_RANGE
)
1157 /* The result covers everything. */
1162 else if (cmpmin
== -1
1164 && (operand_less_p (vr1min
, *vr0max
) == 1
1165 || operand_equal_p (vr1min
, *vr0max
, 0)))
1167 /* [ ( ] ) or [ ]( ) */
1168 if (*vr0type
== VR_RANGE
1169 && vr1type
== VR_RANGE
)
1171 else if (*vr0type
== VR_ANTI_RANGE
1172 && vr1type
== VR_ANTI_RANGE
)
1174 else if (*vr0type
== VR_ANTI_RANGE
1175 && vr1type
== VR_RANGE
)
1177 if (TREE_CODE (vr1min
) == INTEGER_CST
)
1178 *vr0max
= int_const_binop (MINUS_EXPR
, vr1min
,
1179 build_int_cst (TREE_TYPE (vr1min
), 1));
1183 else if (*vr0type
== VR_RANGE
1184 && vr1type
== VR_ANTI_RANGE
)
1186 if (TREE_CODE (*vr0max
) == INTEGER_CST
)
1189 *vr0min
= int_const_binop (PLUS_EXPR
, *vr0max
,
1190 build_int_cst (TREE_TYPE (*vr0max
), 1));
1199 else if (cmpmin
== 1
1201 && (operand_less_p (*vr0min
, vr1max
) == 1
1202 || operand_equal_p (*vr0min
, vr1max
, 0)))
1204 /* ( [ ) ] or ( )[ ] */
1205 if (*vr0type
== VR_RANGE
1206 && vr1type
== VR_RANGE
)
1208 else if (*vr0type
== VR_ANTI_RANGE
1209 && vr1type
== VR_ANTI_RANGE
)
1211 else if (*vr0type
== VR_ANTI_RANGE
1212 && vr1type
== VR_RANGE
)
1214 if (TREE_CODE (vr1max
) == INTEGER_CST
)
1215 *vr0min
= int_const_binop (PLUS_EXPR
, vr1max
,
1216 build_int_cst (TREE_TYPE (vr1max
), 1));
1220 else if (*vr0type
== VR_RANGE
1221 && vr1type
== VR_ANTI_RANGE
)
1223 if (TREE_CODE (*vr0min
) == INTEGER_CST
)
1226 *vr0max
= int_const_binop (MINUS_EXPR
, *vr0min
,
1227 build_int_cst (TREE_TYPE (*vr0min
), 1));
1242 *vr0type
= VR_VARYING
;
1243 *vr0min
= NULL_TREE
;
1244 *vr0max
= NULL_TREE
;
1247 /* Helper for meet operation for value ranges. Given two value ranges VR0 and
1248 VR1, return a range that contains both VR0 and VR1. This may not be the
1249 smallest possible such range. */
1252 value_range::union_helper (const value_range
*vr0
, const value_range
*vr1
)
1254 /* VR0 has the resulting range if VR1 is undefined or VR0 is varying. */
1255 if (vr1
->undefined_p ()
1256 || vr0
->varying_p ())
1259 /* VR1 has the resulting range if VR0 is undefined or VR1 is varying. */
1260 if (vr0
->undefined_p ()
1261 || vr1
->varying_p ())
1264 value_range_kind vr0kind
= vr0
->kind ();
1265 tree vr0min
= vr0
->min ();
1266 tree vr0max
= vr0
->max ();
1267 union_ranges (&vr0kind
, &vr0min
, &vr0max
,
1268 vr1
->kind (), vr1
->min (), vr1
->max ());
1270 /* Work on a temporary so we can still use vr0 when union returns varying. */
1272 if (vr0kind
== VR_UNDEFINED
)
1273 tem
.set_undefined ();
1274 else if (vr0kind
== VR_VARYING
)
1275 tem
.set_varying (vr0
->type ());
1277 tem
.set (vr0min
, vr0max
, vr0kind
);
1279 /* Failed to find an efficient meet. Before giving up and setting
1280 the result to VARYING, see if we can at least derive a useful
1282 if (tem
.varying_p ()
1283 && range_includes_zero_p (vr0
) == 0
1284 && range_includes_zero_p (vr1
) == 0)
1286 tem
.set_nonzero (vr0
->type ());
1293 /* Meet operation for value ranges. Given two value ranges VR0 and
1294 VR1, store in VR0 a range that contains both VR0 and VR1. This
1295 may not be the smallest possible such range. */
1298 value_range::union_ (const value_range
*other
)
1300 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1302 fprintf (dump_file
, "Meeting\n ");
1303 dump_value_range (dump_file
, this);
1304 fprintf (dump_file
, "\nand\n ");
1305 dump_value_range (dump_file
, other
);
1306 fprintf (dump_file
, "\n");
1309 *this = union_helper (this, other
);
1311 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1313 fprintf (dump_file
, "to\n ");
1314 dump_value_range (dump_file
, this);
1315 fprintf (dump_file
, "\n");
1319 /* Range union, but for references. */
1322 value_range::union_ (const value_range
&r
)
1324 /* Disable details for now, because it makes the ranger dump
1325 unnecessarily verbose. */
1326 bool details
= dump_flags
& TDF_DETAILS
;
1328 dump_flags
&= ~TDF_DETAILS
;
1331 dump_flags
|= TDF_DETAILS
;
1335 value_range::intersect (const value_range
*other
)
1337 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1339 fprintf (dump_file
, "Intersecting\n ");
1340 dump_value_range (dump_file
, this);
1341 fprintf (dump_file
, "\nand\n ");
1342 dump_value_range (dump_file
, other
);
1343 fprintf (dump_file
, "\n");
1346 *this = intersect_helper (this, other
);
1348 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1350 fprintf (dump_file
, "to\n ");
1351 dump_value_range (dump_file
, this);
1352 fprintf (dump_file
, "\n");
1356 /* Range intersect, but for references. */
1359 value_range::intersect (const value_range
&r
)
1361 /* Disable details for now, because it makes the ranger dump
1362 unnecessarily verbose. */
1363 bool details
= dump_flags
& TDF_DETAILS
;
1365 dump_flags
&= ~TDF_DETAILS
;
1368 dump_flags
|= TDF_DETAILS
;
1371 /* Return the inverse of a range. */
1374 value_range::invert ()
1376 /* We can't just invert VR_RANGE and VR_ANTI_RANGE because we may
1377 create non-canonical ranges. Use the constructors instead. */
1378 if (m_kind
== VR_RANGE
)
1379 *this = value_range (m_min
, m_max
, VR_ANTI_RANGE
);
1380 else if (m_kind
== VR_ANTI_RANGE
)
1381 *this = value_range (m_min
, m_max
);
1387 value_range::dump (FILE *file
) const
1390 fprintf (file
, "UNDEFINED");
1391 else if (m_kind
== VR_RANGE
|| m_kind
== VR_ANTI_RANGE
)
1393 tree ttype
= type ();
1395 print_generic_expr (file
, ttype
);
1396 fprintf (file
, " ");
1398 fprintf (file
, "%s[", (m_kind
== VR_ANTI_RANGE
) ? "~" : "");
1400 if (INTEGRAL_TYPE_P (ttype
)
1401 && !TYPE_UNSIGNED (ttype
)
1402 && vrp_val_is_min (min ())
1403 && TYPE_PRECISION (ttype
) != 1)
1404 fprintf (file
, "-INF");
1406 print_generic_expr (file
, min ());
1408 fprintf (file
, ", ");
1410 if (supports_type_p (ttype
)
1411 && vrp_val_is_max (max ())
1412 && TYPE_PRECISION (ttype
) != 1)
1413 fprintf (file
, "+INF");
1415 print_generic_expr (file
, max ());
1417 fprintf (file
, "]");
1419 else if (varying_p ())
1421 print_generic_expr (file
, type ());
1422 fprintf (file
, " VARYING");
1429 value_range::dump () const
1435 dump_value_range (FILE *file
, const value_range
*vr
)
1438 fprintf (file
, "[]");
1444 debug (const value_range
*vr
)
1446 dump_value_range (stderr
, vr
);
1450 debug (const value_range
&vr
)
1452 dump_value_range (stderr
, &vr
);
1455 /* Create two value-ranges in *VR0 and *VR1 from the anti-range *AR
1456 so that *VR0 U *VR1 == *AR. Returns true if that is possible,
1457 false otherwise. If *AR can be represented with a single range
1458 *VR1 will be VR_UNDEFINED. */
1461 ranges_from_anti_range (const value_range
*ar
,
1462 value_range
*vr0
, value_range
*vr1
)
1464 tree type
= ar
->type ();
1466 vr0
->set_undefined ();
1467 vr1
->set_undefined ();
1469 /* As a future improvement, we could handle ~[0, A] as: [-INF, -1] U
1470 [A+1, +INF]. Not sure if this helps in practice, though. */
1472 if (ar
->kind () != VR_ANTI_RANGE
1473 || TREE_CODE (ar
->min ()) != INTEGER_CST
1474 || TREE_CODE (ar
->max ()) != INTEGER_CST
1475 || !vrp_val_min (type
)
1476 || !vrp_val_max (type
))
1479 if (tree_int_cst_lt (vrp_val_min (type
), ar
->min ()))
1480 vr0
->set (vrp_val_min (type
),
1481 wide_int_to_tree (type
, wi::to_wide (ar
->min ()) - 1));
1482 if (tree_int_cst_lt (ar
->max (), vrp_val_max (type
)))
1483 vr1
->set (wide_int_to_tree (type
, wi::to_wide (ar
->max ()) + 1),
1484 vrp_val_max (type
));
1485 if (vr0
->undefined_p ())
1488 vr1
->set_undefined ();
1491 return !vr0
->undefined_p ();
1495 range_has_numeric_bounds_p (const value_range
*vr
)
1498 && TREE_CODE (vr
->min ()) == INTEGER_CST
1499 && TREE_CODE (vr
->max ()) == INTEGER_CST
);
1502 /* Return the maximum value for TYPE. */
1505 vrp_val_max (const_tree type
)
1507 if (INTEGRAL_TYPE_P (type
))
1508 return TYPE_MAX_VALUE (type
);
1509 if (POINTER_TYPE_P (type
))
1511 wide_int max
= wi::max_value (TYPE_PRECISION (type
), TYPE_SIGN (type
));
1512 return wide_int_to_tree (const_cast<tree
> (type
), max
);
1517 /* Return the minimum value for TYPE. */
1520 vrp_val_min (const_tree type
)
1522 if (INTEGRAL_TYPE_P (type
))
1523 return TYPE_MIN_VALUE (type
);
1524 if (POINTER_TYPE_P (type
))
1525 return build_zero_cst (const_cast<tree
> (type
));
1529 /* Return whether VAL is equal to the maximum value of its type.
1530 We can't do a simple equality comparison with TYPE_MAX_VALUE because
1531 C typedefs and Ada subtypes can produce types whose TYPE_MAX_VALUE
1532 is not == to the integer constant with the same value in the type. */
1535 vrp_val_is_max (const_tree val
)
1537 tree type_max
= vrp_val_max (TREE_TYPE (val
));
1538 return (val
== type_max
1539 || (type_max
!= NULL_TREE
1540 && operand_equal_p (val
, type_max
, 0)));
1543 /* Return whether VAL is equal to the minimum value of its type. */
1546 vrp_val_is_min (const_tree val
)
1548 tree type_min
= vrp_val_min (TREE_TYPE (val
));
1549 return (val
== type_min
1550 || (type_min
!= NULL_TREE
1551 && operand_equal_p (val
, type_min
, 0)));
1554 /* Return true, if VAL1 and VAL2 are equal values for VRP purposes. */
1557 vrp_operand_equal_p (const_tree val1
, const_tree val2
)
1561 if (!val1
|| !val2
|| !operand_equal_p (val1
, val2
, 0))