comparison gcc/tree-vrp.c @ 145:1830386684a0

gcc-9.2.0
author anatofuz
date Thu, 13 Feb 2020 11:34:05 +0900
parents 84e7813d76e9
children
comparison
equal deleted inserted replaced
131:84e7813d76e9 145:1830386684a0
1 /* Support routines for Value Range Propagation (VRP). 1 /* Support routines for Value Range Propagation (VRP).
2 Copyright (C) 2005-2018 Free Software Foundation, Inc. 2 Copyright (C) 2005-2020 Free Software Foundation, Inc.
3 Contributed by Diego Novillo <dnovillo@redhat.com>. 3 Contributed by Diego Novillo <dnovillo@redhat.com>.
4 4
5 This file is part of GCC. 5 This file is part of GCC.
6 6
7 GCC is free software; you can redistribute it and/or modify 7 GCC is free software; you can redistribute it and/or modify
57 #include "tree-ssa-scopedtables.h" 57 #include "tree-ssa-scopedtables.h"
58 #include "tree-ssa-threadedge.h" 58 #include "tree-ssa-threadedge.h"
59 #include "omp-general.h" 59 #include "omp-general.h"
60 #include "target.h" 60 #include "target.h"
61 #include "case-cfn-macros.h" 61 #include "case-cfn-macros.h"
62 #include "params.h"
63 #include "alloc-pool.h" 62 #include "alloc-pool.h"
64 #include "domwalk.h" 63 #include "domwalk.h"
65 #include "tree-cfgcleanup.h" 64 #include "tree-cfgcleanup.h"
66 #include "stringpool.h" 65 #include "stringpool.h"
67 #include "attribs.h" 66 #include "attribs.h"
68 #include "vr-values.h" 67 #include "vr-values.h"
69 #include "builtins.h" 68 #include "builtins.h"
70 #include "wide-int-range.h" 69 #include "range-op.h"
71 70
72 /* Set of SSA names found live during the RPO traversal of the function 71 /* Set of SSA names found live during the RPO traversal of the function
73 for still active basic-blocks. */ 72 for still active basic-blocks. */
74 static sbitmap *live; 73 static sbitmap *live;
75 74
76 /* Initialize value_range. */
77
78 void 75 void
79 value_range::set (enum value_range_kind kind, tree min, tree max, 76 value_range_equiv::set_equiv (bitmap equiv)
80 bitmap equiv) 77 {
81 { 78 if (undefined_p () || varying_p ())
82 m_kind = kind; 79 equiv = NULL;
83 m_min = min;
84 m_max = max;
85
86 /* Since updating the equivalence set involves deep copying the 80 /* Since updating the equivalence set involves deep copying the
87 bitmaps, only do it if absolutely necessary. 81 bitmaps, only do it if absolutely necessary.
88 82
89 All equivalence bitmaps are allocated from the same obstack. So 83 All equivalence bitmaps are allocated from the same obstack. So
90 we can use the obstack associated with EQUIV to allocate vr->equiv. */ 84 we can use the obstack associated with EQUIV to allocate vr->equiv. */
97 if (equiv && !bitmap_empty_p (equiv)) 91 if (equiv && !bitmap_empty_p (equiv))
98 bitmap_copy (m_equiv, equiv); 92 bitmap_copy (m_equiv, equiv);
99 else 93 else
100 bitmap_clear (m_equiv); 94 bitmap_clear (m_equiv);
101 } 95 }
96 }
97
98 /* Initialize value_range. */
99
100 void
101 value_range_equiv::set (tree min, tree max, bitmap equiv,
102 value_range_kind kind)
103 {
104 value_range::set (min, max, kind);
105 set_equiv (equiv);
102 if (flag_checking) 106 if (flag_checking)
103 check (); 107 check ();
104 } 108 }
105 109
106 value_range::value_range (value_range_kind kind, tree min, tree max, 110 value_range_equiv::value_range_equiv (tree min, tree max, bitmap equiv,
107 bitmap equiv) 111 value_range_kind kind)
108 { 112 {
109 m_equiv = NULL; 113 m_equiv = NULL;
110 set (kind, min, max, equiv); 114 set (min, max, equiv, kind);
111 } 115 }
112 116
113 /* Like above, but keep the equivalences intact. */ 117 value_range_equiv::value_range_equiv (const value_range &other)
118 {
119 m_equiv = NULL;
120 set (other.min(), other.max (), NULL, other.kind ());
121 }
122
123 /* Like set, but keep the equivalences in place. */
114 124
115 void 125 void
116 value_range::update (value_range_kind kind, tree min, tree max) 126 value_range_equiv::update (tree min, tree max, value_range_kind kind)
117 { 127 {
118 set (kind, min, max, m_equiv); 128 set (min, max,
129 (kind != VR_UNDEFINED && kind != VR_VARYING) ? m_equiv : NULL, kind);
119 } 130 }
120 131
121 /* Copy value_range in FROM into THIS while avoiding bitmap sharing. 132 /* Copy value_range in FROM into THIS while avoiding bitmap sharing.
122 133
123 Note: The code that avoids the bitmap sharing looks at the existing 134 Note: The code that avoids the bitmap sharing looks at the existing
124 this->m_equiv, so this function cannot be used to initalize an 135 this->m_equiv, so this function cannot be used to initalize an
125 object. Use the constructors for initialization. */ 136 object. Use the constructors for initialization. */
126 137
127 void 138 void
128 value_range::deep_copy (const value_range *from) 139 value_range_equiv::deep_copy (const value_range_equiv *from)
129 { 140 {
130 set (from->m_kind, from->min (), from->max (), from->m_equiv); 141 set (from->min (), from->max (), from->m_equiv, from->m_kind);
131 } 142 }
132
133 /* Check the validity of the range. */
134 143
135 void 144 void
136 value_range::check () 145 value_range_equiv::move (value_range_equiv *from)
137 { 146 {
147 set (from->min (), from->max (), NULL, from->m_kind);
148 m_equiv = from->m_equiv;
149 from->m_equiv = NULL;
150 }
151
152 void
153 value_range_equiv::check ()
154 {
155 value_range::check ();
138 switch (m_kind) 156 switch (m_kind)
139 { 157 {
140 case VR_RANGE:
141 case VR_ANTI_RANGE:
142 {
143 int cmp;
144
145 gcc_assert (m_min && m_max);
146
147 gcc_assert (!TREE_OVERFLOW_P (m_min) && !TREE_OVERFLOW_P (m_max));
148
149 /* Creating ~[-MIN, +MAX] is stupid because that would be
150 the empty set. */
151 if (INTEGRAL_TYPE_P (TREE_TYPE (m_min)) && m_kind == VR_ANTI_RANGE)
152 gcc_assert (!vrp_val_is_min (m_min) || !vrp_val_is_max (m_max));
153
154 cmp = compare_values (m_min, m_max);
155 gcc_assert (cmp == 0 || cmp == -1 || cmp == -2);
156 break;
157 }
158 case VR_UNDEFINED: 158 case VR_UNDEFINED:
159 case VR_VARYING: 159 case VR_VARYING:
160 gcc_assert (!m_min && !m_max);
161 gcc_assert (!m_equiv || bitmap_empty_p (m_equiv)); 160 gcc_assert (!m_equiv || bitmap_empty_p (m_equiv));
162 break; 161 default:;
163 default: 162 }
164 gcc_unreachable (); 163 }
165 } 164
165 /* Return true if the bitmaps B1 and B2 are equal. */
166
167 static bool
168 vrp_bitmap_equal_p (const_bitmap b1, const_bitmap b2)
169 {
170 return (b1 == b2
171 || ((!b1 || bitmap_empty_p (b1))
172 && (!b2 || bitmap_empty_p (b2)))
173 || (b1 && b2
174 && bitmap_equal_p (b1, b2)));
166 } 175 }
167 176
168 /* Returns TRUE if THIS == OTHER. Ignores the equivalence bitmap if 177 /* Returns TRUE if THIS == OTHER. Ignores the equivalence bitmap if
169 IGNORE_EQUIVS is TRUE. */ 178 IGNORE_EQUIVS is TRUE. */
170 179
171 bool 180 bool
172 value_range::equal_p (const value_range &other, bool ignore_equivs) const 181 value_range_equiv::equal_p (const value_range_equiv &other,
173 { 182 bool ignore_equivs) const
174 return (m_kind == other.m_kind 183 {
175 && vrp_operand_equal_p (m_min, other.m_min) 184 return (value_range::equal_p (other)
176 && vrp_operand_equal_p (m_max, other.m_max)
177 && (ignore_equivs 185 && (ignore_equivs
178 || vrp_bitmap_equal_p (m_equiv, other.m_equiv))); 186 || vrp_bitmap_equal_p (m_equiv, other.m_equiv)));
179 } 187 }
180 188
181 /* Return equality while ignoring equivalence bitmap. */
182
183 bool
184 value_range::ignore_equivs_equal_p (const value_range &other) const
185 {
186 return equal_p (other, /*ignore_equivs=*/true);
187 }
188
189 bool
190 value_range::operator== (const value_range &other) const
191 {
192 return equal_p (other, /*ignore_equivs=*/false);
193 }
194
195 bool
196 value_range::operator!= (const value_range &other) const
197 {
198 return !(*this == other);
199 }
200
201 /* Return TRUE if this is a symbolic range. */
202
203 bool
204 value_range::symbolic_p () const
205 {
206 return (!varying_p ()
207 && !undefined_p ()
208 && (!is_gimple_min_invariant (m_min)
209 || !is_gimple_min_invariant (m_max)));
210 }
211
212 /* NOTE: This is not the inverse of symbolic_p because the range
213 could also be varying or undefined. Ideally they should be inverse
214 of each other, with varying only applying to symbolics. Varying of
215 constants would be represented as [-MIN, +MAX]. */
216
217 bool
218 value_range::constant_p () const
219 {
220 return (!varying_p ()
221 && !undefined_p ()
222 && TREE_CODE (m_min) == INTEGER_CST
223 && TREE_CODE (m_max) == INTEGER_CST);
224 }
225
226 void 189 void
227 value_range::set_undefined () 190 value_range_equiv::set_undefined ()
228 { 191 {
192 set (NULL, NULL, NULL, VR_UNDEFINED);
193 }
194
195 void
196 value_range_equiv::set_varying (tree type)
197 {
198 value_range::set_varying (type);
229 equiv_clear (); 199 equiv_clear ();
230 *this = value_range (VR_UNDEFINED, NULL, NULL, NULL);
231 } 200 }
232 201
233 void 202 void
234 value_range::set_varying () 203 value_range_equiv::equiv_clear ()
235 {
236 equiv_clear ();
237 *this = value_range (VR_VARYING, NULL, NULL, NULL);
238 }
239
240 /* Return TRUE if it is possible that range contains VAL. */
241
242 bool
243 value_range::may_contain_p (tree val) const
244 {
245 if (varying_p ())
246 return true;
247
248 if (undefined_p ())
249 return true;
250
251 if (m_kind == VR_ANTI_RANGE)
252 {
253 int res = value_inside_range (val, m_min, m_max);
254 return res == 0 || res == -2;
255 }
256 return value_inside_range (val, m_min, m_max) != 0;
257 }
258
259 void
260 value_range::equiv_clear ()
261 { 204 {
262 if (m_equiv) 205 if (m_equiv)
263 bitmap_clear (m_equiv); 206 bitmap_clear (m_equiv);
264 } 207 }
265 208
269 212
270 This is the central point where equivalence processing can be 213 This is the central point where equivalence processing can be
271 turned on/off. */ 214 turned on/off. */
272 215
273 void 216 void
274 value_range::equiv_add (const_tree var, 217 value_range_equiv::equiv_add (const_tree var,
275 const value_range *var_vr, 218 const value_range_equiv *var_vr,
276 bitmap_obstack *obstack) 219 bitmap_obstack *obstack)
277 { 220 {
278 if (!m_equiv) 221 if (!m_equiv)
279 m_equiv = BITMAP_ALLOC (obstack); 222 m_equiv = BITMAP_ALLOC (obstack);
280 unsigned ver = SSA_NAME_VERSION (var); 223 unsigned ver = SSA_NAME_VERSION (var);
281 bitmap_set_bit (m_equiv, ver); 224 bitmap_set_bit (m_equiv, ver);
282 if (var_vr && var_vr->m_equiv) 225 if (var_vr && var_vr->m_equiv)
283 bitmap_ior_into (m_equiv, var_vr->m_equiv); 226 bitmap_ior_into (m_equiv, var_vr->m_equiv);
284 } 227 }
285 228
286 /* If range is a singleton, place it in RESULT and return TRUE.
287 Note: A singleton can be any gimple invariant, not just constants.
288 So, [&x, &x] counts as a singleton. */
289
290 bool
291 value_range::singleton_p (tree *result) const
292 {
293 if (m_kind == VR_RANGE
294 && vrp_operand_equal_p (m_min, m_max)
295 && is_gimple_min_invariant (m_min))
296 {
297 if (result)
298 *result = m_min;
299 return true;
300 }
301 return false;
302 }
303
304 tree
305 value_range::type () const
306 {
307 /* Types are only valid for VR_RANGE and VR_ANTI_RANGE, which are
308 known to have non-zero min/max. */
309 gcc_assert (m_min);
310 return TREE_TYPE (m_min);
311 }
312
313 /* Dump value range to FILE. */
314
315 void 229 void
316 value_range::dump (FILE *file) const 230 value_range_equiv::dump (FILE *file) const
317 { 231 {
318 if (undefined_p ()) 232 value_range::dump (file);
319 fprintf (file, "UNDEFINED"); 233 if ((m_kind == VR_RANGE || m_kind == VR_ANTI_RANGE)
320 else if (m_kind == VR_RANGE || m_kind == VR_ANTI_RANGE) 234 && m_equiv)
321 { 235 {
322 tree type = TREE_TYPE (min ()); 236 bitmap_iterator bi;
323 237 unsigned i, c = 0;
324 fprintf (file, "%s[", (m_kind == VR_ANTI_RANGE) ? "~" : ""); 238
325 239 fprintf (file, " EQUIVALENCES: { ");
326 if (INTEGRAL_TYPE_P (type) 240
327 && !TYPE_UNSIGNED (type) 241 EXECUTE_IF_SET_IN_BITMAP (m_equiv, 0, i, bi)
328 && vrp_val_is_min (min ())) 242 {
329 fprintf (file, "-INF"); 243 print_generic_expr (file, ssa_name (i));
330 else 244 fprintf (file, " ");
331 print_generic_expr (file, min ()); 245 c++;
332 246 }
333 fprintf (file, ", "); 247
334 248 fprintf (file, "} (%u elements)", c);
335 if (INTEGRAL_TYPE_P (type) 249 }
336 && vrp_val_is_max (max ())) 250 }
337 fprintf (file, "+INF"); 251
338 else 252 void
339 print_generic_expr (file, max ()); 253 value_range_equiv::dump () const
340 254 {
341 fprintf (file, "]"); 255 dump (stderr);
342 256 }
343 if (m_equiv) 257
344 { 258 void
345 bitmap_iterator bi; 259 dump_value_range (FILE *file, const value_range_equiv *vr)
346 unsigned i, c = 0; 260 {
347 261 if (!vr)
348 fprintf (file, " EQUIVALENCES: { "); 262 fprintf (file, "[]");
349
350 EXECUTE_IF_SET_IN_BITMAP (m_equiv, 0, i, bi)
351 {
352 print_generic_expr (file, ssa_name (i));
353 fprintf (file, " ");
354 c++;
355 }
356
357 fprintf (file, "} (%u elements)", c);
358 }
359 }
360 else if (varying_p ())
361 fprintf (file, "VARYING");
362 else 263 else
363 fprintf (file, "INVALID RANGE"); 264 vr->dump (file);
364 } 265 }
365 266
366 void 267 DEBUG_FUNCTION void
367 value_range::dump () const 268 debug (const value_range_equiv *vr)
368 { 269 {
369 dump_value_range (stderr, this); 270 dump_value_range (stderr, vr);
370 fprintf (stderr, "\n"); 271 }
272
273 DEBUG_FUNCTION void
274 debug (const value_range_equiv &vr)
275 {
276 dump_value_range (stderr, &vr);
371 } 277 }
372 278
373 /* Return true if the SSA name NAME is live on the edge E. */ 279 /* Return true if the SSA name NAME is live on the edge E. */
374 280
375 static bool 281 static bool
416 /* Array of locations lists where to insert assertions. ASSERTS_FOR[I] 322 /* Array of locations lists where to insert assertions. ASSERTS_FOR[I]
417 holds a list of ASSERT_LOCUS_T nodes that describe where 323 holds a list of ASSERT_LOCUS_T nodes that describe where
418 ASSERT_EXPRs for SSA name N_I should be inserted. */ 324 ASSERT_EXPRs for SSA name N_I should be inserted. */
419 static assert_locus **asserts_for; 325 static assert_locus **asserts_for;
420 326
421 /* Return the maximum value for TYPE. */
422
423 tree
424 vrp_val_max (const_tree type)
425 {
426 if (!INTEGRAL_TYPE_P (type))
427 return NULL_TREE;
428
429 return TYPE_MAX_VALUE (type);
430 }
431
432 /* Return the minimum value for TYPE. */
433
434 tree
435 vrp_val_min (const_tree type)
436 {
437 if (!INTEGRAL_TYPE_P (type))
438 return NULL_TREE;
439
440 return TYPE_MIN_VALUE (type);
441 }
442
443 /* Return whether VAL is equal to the maximum value of its type.
444 We can't do a simple equality comparison with TYPE_MAX_VALUE because
445 C typedefs and Ada subtypes can produce types whose TYPE_MAX_VALUE
446 is not == to the integer constant with the same value in the type. */
447
448 bool
449 vrp_val_is_max (const_tree val)
450 {
451 tree type_max = vrp_val_max (TREE_TYPE (val));
452 return (val == type_max
453 || (type_max != NULL_TREE
454 && operand_equal_p (val, type_max, 0)));
455 }
456
457 /* Return whether VAL is equal to the minimum value of its type. */
458
459 bool
460 vrp_val_is_min (const_tree val)
461 {
462 tree type_min = vrp_val_min (TREE_TYPE (val));
463 return (val == type_min
464 || (type_min != NULL_TREE
465 && operand_equal_p (val, type_min, 0)));
466 }
467
468 /* VR_TYPE describes a range with mininum value *MIN and maximum 327 /* VR_TYPE describes a range with mininum value *MIN and maximum
469 value *MAX. Restrict the range to the set of values that have 328 value *MAX. Restrict the range to the set of values that have
470 no bits set outside NONZERO_BITS. Update *MIN and *MAX and 329 no bits set outside NONZERO_BITS. Update *MIN and *MAX and
471 return the new range type. 330 return the new range type.
472 331
535 gcc_checking_assert (wi::le_p (*min, *max, sgn)); 394 gcc_checking_assert (wi::le_p (*min, *max, sgn));
536 } 395 }
537 return vr_type; 396 return vr_type;
538 } 397 }
539 398
540 /* Set value range VR to VR_UNDEFINED. */
541
542 static inline void
543 set_value_range_to_undefined (value_range *vr)
544 {
545 vr->set_undefined ();
546 }
547
548 /* Set value range VR to VR_VARYING. */
549
550 void 399 void
551 set_value_range_to_varying (value_range *vr) 400 value_range_equiv::set (tree val)
552 { 401 {
553 vr->set_varying (); 402 gcc_assert (TREE_CODE (val) == SSA_NAME || is_gimple_min_invariant (val));
554 }
555
556 /* Set value range VR to {T, MIN, MAX, EQUIV}. */
557
558 void
559 set_value_range (value_range *vr, enum value_range_kind kind,
560 tree min, tree max, bitmap equiv)
561 {
562 *vr = value_range (kind, min, max, equiv);
563 }
564
565
566 /* Set value range to the canonical form of {VRTYPE, MIN, MAX, EQUIV}.
567 This means adjusting VRTYPE, MIN and MAX representing the case of a
568 wrapping range with MAX < MIN covering [MIN, type_max] U [type_min, MAX]
569 as anti-rage ~[MAX+1, MIN-1]. Likewise for wrapping anti-ranges.
570 In corner cases where MAX+1 or MIN-1 wraps this will fall back
571 to varying.
572 This routine exists to ease canonicalization in the case where we
573 extract ranges from var + CST op limit. */
574
575 void
576 value_range::set_and_canonicalize (enum value_range_kind kind,
577 tree min, tree max, bitmap equiv)
578 {
579 /* Use the canonical setters for VR_UNDEFINED and VR_VARYING. */
580 if (kind == VR_UNDEFINED)
581 {
582 set_undefined ();
583 return;
584 }
585 else if (kind == VR_VARYING)
586 {
587 set_varying ();
588 return;
589 }
590
591 /* Nothing to canonicalize for symbolic ranges. */
592 if (TREE_CODE (min) != INTEGER_CST
593 || TREE_CODE (max) != INTEGER_CST)
594 {
595 set_value_range (this, kind, min, max, equiv);
596 return;
597 }
598
599 /* Wrong order for min and max, to swap them and the VR type we need
600 to adjust them. */
601 if (tree_int_cst_lt (max, min))
602 {
603 tree one, tmp;
604
605 /* For one bit precision if max < min, then the swapped
606 range covers all values, so for VR_RANGE it is varying and
607 for VR_ANTI_RANGE empty range, so drop to varying as well. */
608 if (TYPE_PRECISION (TREE_TYPE (min)) == 1)
609 {
610 set_varying ();
611 return;
612 }
613
614 one = build_int_cst (TREE_TYPE (min), 1);
615 tmp = int_const_binop (PLUS_EXPR, max, one);
616 max = int_const_binop (MINUS_EXPR, min, one);
617 min = tmp;
618
619 /* There's one corner case, if we had [C+1, C] before we now have
620 that again. But this represents an empty value range, so drop
621 to varying in this case. */
622 if (tree_int_cst_lt (max, min))
623 {
624 set_varying ();
625 return;
626 }
627
628 kind = kind == VR_RANGE ? VR_ANTI_RANGE : VR_RANGE;
629 }
630
631 /* Anti-ranges that can be represented as ranges should be so. */
632 if (kind == VR_ANTI_RANGE)
633 {
634 /* For -fstrict-enums we may receive out-of-range ranges so consider
635 values < -INF and values > INF as -INF/INF as well. */
636 tree type = TREE_TYPE (min);
637 bool is_min = (INTEGRAL_TYPE_P (type)
638 && tree_int_cst_compare (min, TYPE_MIN_VALUE (type)) <= 0);
639 bool is_max = (INTEGRAL_TYPE_P (type)
640 && tree_int_cst_compare (max, TYPE_MAX_VALUE (type)) >= 0);
641
642 if (is_min && is_max)
643 {
644 /* We cannot deal with empty ranges, drop to varying.
645 ??? This could be VR_UNDEFINED instead. */
646 set_varying ();
647 return;
648 }
649 else if (TYPE_PRECISION (TREE_TYPE (min)) == 1
650 && (is_min || is_max))
651 {
652 /* Non-empty boolean ranges can always be represented
653 as a singleton range. */
654 if (is_min)
655 min = max = vrp_val_max (TREE_TYPE (min));
656 else
657 min = max = vrp_val_min (TREE_TYPE (min));
658 kind = VR_RANGE;
659 }
660 else if (is_min
661 /* As a special exception preserve non-null ranges. */
662 && !(TYPE_UNSIGNED (TREE_TYPE (min))
663 && integer_zerop (max)))
664 {
665 tree one = build_int_cst (TREE_TYPE (max), 1);
666 min = int_const_binop (PLUS_EXPR, max, one);
667 max = vrp_val_max (TREE_TYPE (max));
668 kind = VR_RANGE;
669 }
670 else if (is_max)
671 {
672 tree one = build_int_cst (TREE_TYPE (min), 1);
673 max = int_const_binop (MINUS_EXPR, min, one);
674 min = vrp_val_min (TREE_TYPE (min));
675 kind = VR_RANGE;
676 }
677 }
678
679 /* Do not drop [-INF(OVF), +INF(OVF)] to varying. (OVF) has to be sticky
680 to make sure VRP iteration terminates, otherwise we can get into
681 oscillations. */
682
683 set_value_range (this, kind, min, max, equiv);
684 }
685
686 /* Set value range VR to a single value. This function is only called
687 with values we get from statements, and exists to clear the
688 TREE_OVERFLOW flag. */
689
690 void
691 set_value_range_to_value (value_range *vr, tree val, bitmap equiv)
692 {
693 gcc_assert (is_gimple_min_invariant (val));
694 if (TREE_OVERFLOW_P (val)) 403 if (TREE_OVERFLOW_P (val))
695 val = drop_tree_overflow (val); 404 val = drop_tree_overflow (val);
696 set_value_range (vr, VR_RANGE, val, val, equiv); 405 set (val, val);
697 }
698
699 /* Set value range VR to a non-NULL range of type TYPE. */
700
701 void
702 set_value_range_to_nonnull (value_range *vr, tree type)
703 {
704 tree zero = build_int_cst (type, 0);
705 vr->update (VR_ANTI_RANGE, zero, zero);
706 }
707
708
709 /* Set value range VR to a NULL range of type TYPE. */
710
711 void
712 set_value_range_to_null (value_range *vr, tree type)
713 {
714 set_value_range_to_value (vr, build_int_cst (type, 0), vr->equiv ());
715 }
716
717 /* Return true, if VAL1 and VAL2 are equal values for VRP purposes. */
718
719 bool
720 vrp_operand_equal_p (const_tree val1, const_tree val2)
721 {
722 if (val1 == val2)
723 return true;
724 if (!val1 || !val2 || !operand_equal_p (val1, val2, 0))
725 return false;
726 return true;
727 }
728
729 /* Return true, if the bitmaps B1 and B2 are equal. */
730
731 bool
732 vrp_bitmap_equal_p (const_bitmap b1, const_bitmap b2)
733 {
734 return (b1 == b2
735 || ((!b1 || bitmap_empty_p (b1))
736 && (!b2 || bitmap_empty_p (b2)))
737 || (b1 && b2
738 && bitmap_equal_p (b1, b2)));
739 }
740
741 /* Return true if VR is [0, 0]. */
742
743 static inline bool
744 range_is_null (const value_range *vr)
745 {
746 return vr->null_p ();
747 }
748
749 static inline bool
750 range_is_nonnull (const value_range *vr)
751 {
752 return (vr->kind () == VR_ANTI_RANGE
753 && vr->min () == vr->max ()
754 && integer_zerop (vr->min ()));
755 } 406 }
756 407
757 /* Return true if max and min of VR are INTEGER_CST. It's not necessary 408 /* Return true if max and min of VR are INTEGER_CST. It's not necessary
758 a singleton. */ 409 a singleton. */
759 410
760 bool 411 bool
761 range_int_cst_p (const value_range *vr) 412 range_int_cst_p (const value_range *vr)
762 { 413 {
763 return (vr->kind () == VR_RANGE 414 return (vr->kind () == VR_RANGE && range_has_numeric_bounds_p (vr));
764 && TREE_CODE (vr->min ()) == INTEGER_CST
765 && TREE_CODE (vr->max ()) == INTEGER_CST);
766 }
767
768 /* Return true if VR is a INTEGER_CST singleton. */
769
770 bool
771 range_int_cst_singleton_p (const value_range *vr)
772 {
773 return (range_int_cst_p (vr)
774 && tree_int_cst_equal (vr->min (), vr->max ()));
775 } 415 }
776 416
777 /* Return the single symbol (an SSA_NAME) contained in T if any, or NULL_TREE 417 /* Return the single symbol (an SSA_NAME) contained in T if any, or NULL_TREE
778 otherwise. We only handle additive operations and set NEG to true if the 418 otherwise. We only handle additive operations and set NEG to true if the
779 symbol is negated and INV to the invariant part, if any. */ 419 symbol is negated and INV to the invariant part, if any. */
855 operand_less_p (tree val, tree val2) 495 operand_less_p (tree val, tree val2)
856 { 496 {
857 /* LT is folded faster than GE and others. Inline the common case. */ 497 /* LT is folded faster than GE and others. Inline the common case. */
858 if (TREE_CODE (val) == INTEGER_CST && TREE_CODE (val2) == INTEGER_CST) 498 if (TREE_CODE (val) == INTEGER_CST && TREE_CODE (val2) == INTEGER_CST)
859 return tree_int_cst_lt (val, val2); 499 return tree_int_cst_lt (val, val2);
500 else if (TREE_CODE (val) == SSA_NAME && TREE_CODE (val2) == SSA_NAME)
501 return val == val2 ? 0 : -2;
860 else 502 else
861 { 503 {
862 tree tcmp; 504 int cmp = compare_values (val, val2);
863 505 if (cmp == -1)
864 fold_defer_overflow_warnings (); 506 return 1;
865 507 else if (cmp == 0 || cmp == 1)
866 tcmp = fold_binary_to_constant (LT_EXPR, boolean_type_node, val, val2); 508 return 0;
867 509 else
868 fold_undefer_and_ignore_overflow_warnings ();
869
870 if (!tcmp
871 || TREE_CODE (tcmp) != INTEGER_CST)
872 return -2; 510 return -2;
873
874 if (!integer_zerop (tcmp))
875 return 1;
876 } 511 }
877 512
878 return 0; 513 return 0;
879 } 514 }
880 515
904 gcc_assert (POINTER_TYPE_P (TREE_TYPE (val1)) 539 gcc_assert (POINTER_TYPE_P (TREE_TYPE (val1))
905 == POINTER_TYPE_P (TREE_TYPE (val2))); 540 == POINTER_TYPE_P (TREE_TYPE (val2)));
906 541
907 /* Convert the two values into the same type. This is needed because 542 /* Convert the two values into the same type. This is needed because
908 sizetype causes sign extension even for unsigned types. */ 543 sizetype causes sign extension even for unsigned types. */
909 val2 = fold_convert (TREE_TYPE (val1), val2); 544 if (!useless_type_conversion_p (TREE_TYPE (val1), TREE_TYPE (val2)))
910 STRIP_USELESS_TYPE_CONVERSION (val2); 545 val2 = fold_convert (TREE_TYPE (val1), val2);
911 546
912 const bool overflow_undefined 547 const bool overflow_undefined
913 = INTEGRAL_TYPE_P (TREE_TYPE (val1)) 548 = INTEGRAL_TYPE_P (TREE_TYPE (val1))
914 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (val1)); 549 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (val1));
915 tree inv1, inv2; 550 tree inv1, inv2;
1013 648
1014 return -2; 649 return -2;
1015 } 650 }
1016 else 651 else
1017 { 652 {
1018 tree t; 653 if (TREE_CODE (val1) == INTEGER_CST && TREE_CODE (val2) == INTEGER_CST)
654 {
655 /* We cannot compare overflowed values. */
656 if (TREE_OVERFLOW (val1) || TREE_OVERFLOW (val2))
657 return -2;
658
659 return tree_int_cst_compare (val1, val2);
660 }
1019 661
1020 /* First see if VAL1 and VAL2 are not the same. */ 662 /* First see if VAL1 and VAL2 are not the same. */
1021 if (val1 == val2 || operand_equal_p (val1, val2, 0)) 663 if (operand_equal_p (val1, val2, 0))
1022 return 0; 664 return 0;
1023 665
666 fold_defer_overflow_warnings ();
667
1024 /* If VAL1 is a lower address than VAL2, return -1. */ 668 /* If VAL1 is a lower address than VAL2, return -1. */
1025 if (operand_less_p (val1, val2) == 1) 669 tree t = fold_binary_to_constant (LT_EXPR, boolean_type_node, val1, val2);
1026 return -1; 670 if (t && integer_onep (t))
671 {
672 fold_undefer_and_ignore_overflow_warnings ();
673 return -1;
674 }
1027 675
1028 /* If VAL1 is a higher address than VAL2, return +1. */ 676 /* If VAL1 is a higher address than VAL2, return +1. */
1029 if (operand_less_p (val2, val1) == 1) 677 t = fold_binary_to_constant (LT_EXPR, boolean_type_node, val2, val1);
1030 return 1; 678 if (t && integer_onep (t))
1031 679 {
1032 /* If VAL1 is different than VAL2, return +2. 680 fold_undefer_and_ignore_overflow_warnings ();
1033 For integer constants we either have already returned -1 or 1 681 return 1;
1034 or they are equivalent. We still might succeed in proving 682 }
1035 something about non-trivial operands. */ 683
1036 if (TREE_CODE (val1) != INTEGER_CST 684 /* If VAL1 is different than VAL2, return +2. */
1037 || TREE_CODE (val2) != INTEGER_CST) 685 t = fold_binary_to_constant (NE_EXPR, boolean_type_node, val1, val2);
1038 { 686 fold_undefer_and_ignore_overflow_warnings ();
1039 t = fold_binary_to_constant (NE_EXPR, boolean_type_node, val1, val2); 687 if (t && integer_onep (t))
1040 if (t && integer_onep (t)) 688 return 2;
1041 return 2;
1042 }
1043 689
1044 return -2; 690 return -2;
1045 } 691 }
1046 } 692 }
1047 693
1050 int 696 int
1051 compare_values (tree val1, tree val2) 697 compare_values (tree val1, tree val2)
1052 { 698 {
1053 bool sop; 699 bool sop;
1054 return compare_values_warnv (val1, val2, &sop); 700 return compare_values_warnv (val1, val2, &sop);
1055 }
1056
1057
1058 /* Return 1 if VAL is inside value range MIN <= VAL <= MAX,
1059 0 if VAL is not inside [MIN, MAX],
1060 -2 if we cannot tell either way.
1061
1062 Benchmark compile/20001226-1.c compilation time after changing this
1063 function. */
1064
1065 int
1066 value_inside_range (tree val, tree min, tree max)
1067 {
1068 int cmp1, cmp2;
1069
1070 cmp1 = operand_less_p (val, min);
1071 if (cmp1 == -2)
1072 return -2;
1073 if (cmp1 == 1)
1074 return 0;
1075
1076 cmp2 = operand_less_p (max, val);
1077 if (cmp2 == -2)
1078 return -2;
1079
1080 return !cmp2;
1081 }
1082
1083
1084 /* Return TRUE if *VR includes the value zero. */
1085
1086 bool
1087 range_includes_zero_p (const value_range *vr)
1088 {
1089 if (vr->varying_p () || vr->undefined_p ())
1090 return true;
1091 tree zero = build_int_cst (vr->type (), 0);
1092 return vr->may_contain_p (zero);
1093 }
1094
1095 /* If *VR has a value range that is a single constant value return that,
1096 otherwise return NULL_TREE.
1097
1098 ?? This actually returns TRUE for [&x, &x], so perhaps "constant"
1099 is not the best name. */
1100
1101 tree
1102 value_range_constant_singleton (const value_range *vr)
1103 {
1104 tree result = NULL;
1105 if (vr->singleton_p (&result))
1106 return result;
1107 return NULL;
1108 }
1109
1110 /* Value range wrapper for wide_int_range_set_zero_nonzero_bits.
1111
1112 Compute MAY_BE_NONZERO and MUST_BE_NONZERO bit masks for range in VR.
1113
1114 Return TRUE if VR was a constant range and we were able to compute
1115 the bit masks. */
1116
1117 bool
1118 vrp_set_zero_nonzero_bits (const tree expr_type,
1119 const value_range *vr,
1120 wide_int *may_be_nonzero,
1121 wide_int *must_be_nonzero)
1122 {
1123 if (!range_int_cst_p (vr))
1124 {
1125 *may_be_nonzero = wi::minus_one (TYPE_PRECISION (expr_type));
1126 *must_be_nonzero = wi::zero (TYPE_PRECISION (expr_type));
1127 return false;
1128 }
1129 wide_int_range_set_zero_nonzero_bits (TYPE_SIGN (expr_type),
1130 wi::to_wide (vr->min ()),
1131 wi::to_wide (vr->max ()),
1132 *may_be_nonzero, *must_be_nonzero);
1133 return true;
1134 }
1135
1136 /* Create two value-ranges in *VR0 and *VR1 from the anti-range *AR
1137 so that *VR0 U *VR1 == *AR. Returns true if that is possible,
1138 false otherwise. If *AR can be represented with a single range
1139 *VR1 will be VR_UNDEFINED. */
1140
1141 static bool
1142 ranges_from_anti_range (const value_range *ar,
1143 value_range *vr0, value_range *vr1)
1144 {
1145 tree type = ar->type ();
1146
1147 vr0->set_undefined ();
1148 vr1->set_undefined ();
1149
1150 /* As a future improvement, we could handle ~[0, A] as: [-INF, -1] U
1151 [A+1, +INF]. Not sure if this helps in practice, though. */
1152
1153 if (ar->kind () != VR_ANTI_RANGE
1154 || TREE_CODE (ar->min ()) != INTEGER_CST
1155 || TREE_CODE (ar->max ()) != INTEGER_CST
1156 || !vrp_val_min (type)
1157 || !vrp_val_max (type))
1158 return false;
1159
1160 if (!vrp_val_is_min (ar->min ()))
1161 *vr0 = value_range (VR_RANGE,
1162 vrp_val_min (type),
1163 wide_int_to_tree (type, wi::to_wide (ar->min ()) - 1));
1164 if (!vrp_val_is_max (ar->max ()))
1165 *vr1 = value_range (VR_RANGE,
1166 wide_int_to_tree (type, wi::to_wide (ar->max ()) + 1),
1167 vrp_val_max (type));
1168 if (vr0->undefined_p ())
1169 {
1170 *vr0 = *vr1;
1171 vr1->set_undefined ();
1172 }
1173
1174 return !vr0->undefined_p ();
1175 }
1176
1177 /* Extract the components of a value range into a pair of wide ints in
1178 [WMIN, WMAX].
1179
1180 If the value range is anything but a VR_*RANGE of constants, the
1181 resulting wide ints are set to [-MIN, +MAX] for the type. */
1182
1183 static void inline
1184 extract_range_into_wide_ints (const value_range *vr,
1185 signop sign, unsigned prec,
1186 wide_int &wmin, wide_int &wmax)
1187 {
1188 gcc_assert (vr->kind () != VR_ANTI_RANGE || vr->symbolic_p ());
1189 if (range_int_cst_p (vr))
1190 {
1191 wmin = wi::to_wide (vr->min ());
1192 wmax = wi::to_wide (vr->max ());
1193 }
1194 else
1195 {
1196 wmin = wi::min_value (prec, sign);
1197 wmax = wi::max_value (prec, sign);
1198 }
1199 }
1200
1201 /* Value range wrapper for wide_int_range_multiplicative_op:
1202
1203 *VR = *VR0 .CODE. *VR1. */
1204
1205 static void
1206 extract_range_from_multiplicative_op (value_range *vr,
1207 enum tree_code code,
1208 const value_range *vr0,
1209 const value_range *vr1)
1210 {
1211 gcc_assert (code == MULT_EXPR
1212 || code == TRUNC_DIV_EXPR
1213 || code == FLOOR_DIV_EXPR
1214 || code == CEIL_DIV_EXPR
1215 || code == EXACT_DIV_EXPR
1216 || code == ROUND_DIV_EXPR
1217 || code == RSHIFT_EXPR
1218 || code == LSHIFT_EXPR);
1219 gcc_assert (vr0->kind () == VR_RANGE
1220 && vr0->kind () == vr1->kind ());
1221
1222 tree type = vr0->type ();
1223 wide_int res_lb, res_ub;
1224 wide_int vr0_lb = wi::to_wide (vr0->min ());
1225 wide_int vr0_ub = wi::to_wide (vr0->max ());
1226 wide_int vr1_lb = wi::to_wide (vr1->min ());
1227 wide_int vr1_ub = wi::to_wide (vr1->max ());
1228 bool overflow_undefined = TYPE_OVERFLOW_UNDEFINED (type);
1229 unsigned prec = TYPE_PRECISION (type);
1230
1231 if (wide_int_range_multiplicative_op (res_lb, res_ub,
1232 code, TYPE_SIGN (type), prec,
1233 vr0_lb, vr0_ub, vr1_lb, vr1_ub,
1234 overflow_undefined))
1235 vr->set_and_canonicalize (VR_RANGE,
1236 wide_int_to_tree (type, res_lb),
1237 wide_int_to_tree (type, res_ub), NULL);
1238 else
1239 set_value_range_to_varying (vr);
1240 } 701 }
1241 702
1242 /* If BOUND will include a symbolic bound, adjust it accordingly, 703 /* If BOUND will include a symbolic bound, adjust it accordingly,
1243 otherwise leave it as is. 704 otherwise leave it as is.
1244 705
1352 wide_int tmin = wide_int::from (wmin, prec, sgn); 813 wide_int tmin = wide_int::from (wmin, prec, sgn);
1353 wide_int tmax = wide_int::from (wmax, prec, sgn); 814 wide_int tmax = wide_int::from (wmax, prec, sgn);
1354 if ((min_ovf != wi::OVF_NONE) == (max_ovf != wi::OVF_NONE)) 815 if ((min_ovf != wi::OVF_NONE) == (max_ovf != wi::OVF_NONE))
1355 { 816 {
1356 /* If the limits are swapped, we wrapped around and cover 817 /* If the limits are swapped, we wrapped around and cover
1357 the entire range. We have a similar check at the end of 818 the entire range. */
1358 extract_range_from_binary_expr_1. */
1359 if (wi::gt_p (tmin, tmax, sgn)) 819 if (wi::gt_p (tmin, tmax, sgn))
1360 kind = VR_VARYING; 820 kind = VR_VARYING;
1361 else 821 else
1362 { 822 {
1363 kind = VR_RANGE; 823 kind = VR_RANGE;
1422 else 882 else
1423 max = wide_int_to_tree (type, wmax); 883 max = wide_int_to_tree (type, wmax);
1424 } 884 }
1425 } 885 }
1426 886
1427 /* Extract range information from a binary operation CODE based on 887 /* Fold two value range's of a POINTER_PLUS_EXPR into VR. */
1428 the ranges of each of its operands *VR0 and *VR1 with resulting 888
1429 type EXPR_TYPE. The resulting range is stored in *VR. */ 889 static void
1430 890 extract_range_from_pointer_plus_expr (value_range *vr,
1431 void 891 enum tree_code code,
1432 extract_range_from_binary_expr_1 (value_range *vr, 892 tree expr_type,
1433 enum tree_code code, tree expr_type, 893 const value_range *vr0,
1434 const value_range *vr0_, 894 const value_range *vr1)
1435 const value_range *vr1_) 895 {
1436 { 896 gcc_checking_assert (POINTER_TYPE_P (expr_type)
1437 signop sign = TYPE_SIGN (expr_type); 897 && code == POINTER_PLUS_EXPR);
1438 unsigned int prec = TYPE_PRECISION (expr_type); 898 /* For pointer types, we are really only interested in asserting
899 whether the expression evaluates to non-NULL.
900 With -fno-delete-null-pointer-checks we need to be more
901 conservative. As some object might reside at address 0,
902 then some offset could be added to it and the same offset
903 subtracted again and the result would be NULL.
904 E.g.
905 static int a[12]; where &a[0] is NULL and
906 ptr = &a[6];
907 ptr -= 6;
908 ptr will be NULL here, even when there is POINTER_PLUS_EXPR
909 where the first range doesn't include zero and the second one
910 doesn't either. As the second operand is sizetype (unsigned),
911 consider all ranges where the MSB could be set as possible
912 subtractions where the result might be NULL. */
913 if ((!range_includes_zero_p (vr0)
914 || !range_includes_zero_p (vr1))
915 && !TYPE_OVERFLOW_WRAPS (expr_type)
916 && (flag_delete_null_pointer_checks
917 || (range_int_cst_p (vr1)
918 && !tree_int_cst_sign_bit (vr1->max ()))))
919 vr->set_nonzero (expr_type);
920 else if (vr0->zero_p () && vr1->zero_p ())
921 vr->set_zero (expr_type);
922 else
923 vr->set_varying (expr_type);
924 }
925
926 /* Extract range information from a PLUS/MINUS_EXPR and store the
927 result in *VR. */
928
929 static void
930 extract_range_from_plus_minus_expr (value_range *vr,
931 enum tree_code code,
932 tree expr_type,
933 const value_range *vr0_,
934 const value_range *vr1_)
935 {
936 gcc_checking_assert (code == PLUS_EXPR || code == MINUS_EXPR);
937
1439 value_range vr0 = *vr0_, vr1 = *vr1_; 938 value_range vr0 = *vr0_, vr1 = *vr1_;
1440 value_range vrtem0, vrtem1; 939 value_range vrtem0, vrtem1;
1441 enum value_range_kind type;
1442 tree min = NULL_TREE, max = NULL_TREE;
1443 int cmp;
1444
1445 if (!INTEGRAL_TYPE_P (expr_type)
1446 && !POINTER_TYPE_P (expr_type))
1447 {
1448 set_value_range_to_varying (vr);
1449 return;
1450 }
1451
1452 /* Not all binary expressions can be applied to ranges in a
1453 meaningful way. Handle only arithmetic operations. */
1454 if (code != PLUS_EXPR
1455 && code != MINUS_EXPR
1456 && code != POINTER_PLUS_EXPR
1457 && code != MULT_EXPR
1458 && code != TRUNC_DIV_EXPR
1459 && code != FLOOR_DIV_EXPR
1460 && code != CEIL_DIV_EXPR
1461 && code != EXACT_DIV_EXPR
1462 && code != ROUND_DIV_EXPR
1463 && code != TRUNC_MOD_EXPR
1464 && code != RSHIFT_EXPR
1465 && code != LSHIFT_EXPR
1466 && code != MIN_EXPR
1467 && code != MAX_EXPR
1468 && code != BIT_AND_EXPR
1469 && code != BIT_IOR_EXPR
1470 && code != BIT_XOR_EXPR)
1471 {
1472 set_value_range_to_varying (vr);
1473 return;
1474 }
1475
1476 /* If both ranges are UNDEFINED, so is the result. */
1477 if (vr0.undefined_p () && vr1.undefined_p ())
1478 {
1479 set_value_range_to_undefined (vr);
1480 return;
1481 }
1482 /* If one of the ranges is UNDEFINED drop it to VARYING for the following
1483 code. At some point we may want to special-case operations that
1484 have UNDEFINED result for all or some value-ranges of the not UNDEFINED
1485 operand. */
1486 else if (vr0.undefined_p ())
1487 set_value_range_to_varying (&vr0);
1488 else if (vr1.undefined_p ())
1489 set_value_range_to_varying (&vr1);
1490
1491 /* We get imprecise results from ranges_from_anti_range when
1492 code is EXACT_DIV_EXPR. We could mask out bits in the resulting
1493 range, but then we also need to hack up vrp_union. It's just
1494 easier to special case when vr0 is ~[0,0] for EXACT_DIV_EXPR. */
1495 if (code == EXACT_DIV_EXPR && range_is_nonnull (&vr0))
1496 {
1497 set_value_range_to_nonnull (vr, expr_type);
1498 return;
1499 }
1500 940
1501 /* Now canonicalize anti-ranges to ranges when they are not symbolic 941 /* Now canonicalize anti-ranges to ranges when they are not symbolic
1502 and express ~[] op X as ([]' op X) U ([]'' op X). */ 942 and express ~[] op X as ([]' op X) U ([]'' op X). */
1503 if (vr0.kind () == VR_ANTI_RANGE 943 if (vr0.kind () == VR_ANTI_RANGE
1504 && ranges_from_anti_range (&vr0, &vrtem0, &vrtem1)) 944 && ranges_from_anti_range (&vr0, &vrtem0, &vrtem1))
1505 { 945 {
1506 extract_range_from_binary_expr_1 (vr, code, expr_type, &vrtem0, vr1_); 946 extract_range_from_plus_minus_expr (vr, code, expr_type, &vrtem0, vr1_);
1507 if (!vrtem1.undefined_p ()) 947 if (!vrtem1.undefined_p ())
1508 { 948 {
1509 value_range vrres; 949 value_range vrres;
1510 extract_range_from_binary_expr_1 (&vrres, code, expr_type, &vrtem1, vr1_); 950 extract_range_from_plus_minus_expr (&vrres, code, expr_type,
951 &vrtem1, vr1_);
1511 vr->union_ (&vrres); 952 vr->union_ (&vrres);
1512 } 953 }
1513 return; 954 return;
1514 } 955 }
1515 /* Likewise for X op ~[]. */ 956 /* Likewise for X op ~[]. */
1516 if (vr1.kind () == VR_ANTI_RANGE 957 if (vr1.kind () == VR_ANTI_RANGE
1517 && ranges_from_anti_range (&vr1, &vrtem0, &vrtem1)) 958 && ranges_from_anti_range (&vr1, &vrtem0, &vrtem1))
1518 { 959 {
1519 extract_range_from_binary_expr_1 (vr, code, expr_type, vr0_, &vrtem0); 960 extract_range_from_plus_minus_expr (vr, code, expr_type, vr0_, &vrtem0);
1520 if (!vrtem1.undefined_p ()) 961 if (!vrtem1.undefined_p ())
1521 { 962 {
1522 value_range vrres; 963 value_range vrres;
1523 extract_range_from_binary_expr_1 (&vrres, code, expr_type, 964 extract_range_from_plus_minus_expr (&vrres, code, expr_type,
1524 vr0_, &vrtem1); 965 vr0_, &vrtem1);
1525 vr->union_ (&vrres); 966 vr->union_ (&vrres);
1526 } 967 }
1527 return; 968 return;
1528 } 969 }
1529 970
1530 /* The type of the resulting value range defaults to VR0.TYPE. */ 971 value_range_kind kind;
1531 type = vr0.kind (); 972 value_range_kind vr0_kind = vr0.kind (), vr1_kind = vr1.kind ();
1532 973 tree vr0_min = vr0.min (), vr0_max = vr0.max ();
1533 /* Refuse to operate on VARYING ranges, ranges of different kinds 974 tree vr1_min = vr1.min (), vr1_max = vr1.max ();
1534 and symbolic ranges. As an exception, we allow BIT_{AND,IOR} 975 tree min = NULL_TREE, max = NULL_TREE;
1535 because we may be able to derive a useful range even if one of 976
1536 the operands is VR_VARYING or symbolic range. Similarly for 977 /* This will normalize things such that calculating
1537 divisions, MIN/MAX and PLUS/MINUS. 978 [0,0] - VR_VARYING is not dropped to varying, but is
1538 979 calculated as [MIN+1, MAX]. */
1539 TODO, we may be able to derive anti-ranges in some cases. */ 980 if (vr0.varying_p ())
1540 if (code != BIT_AND_EXPR 981 {
1541 && code != BIT_IOR_EXPR 982 vr0_kind = VR_RANGE;
1542 && code != TRUNC_DIV_EXPR 983 vr0_min = vrp_val_min (expr_type);
1543 && code != FLOOR_DIV_EXPR 984 vr0_max = vrp_val_max (expr_type);
1544 && code != CEIL_DIV_EXPR 985 }
1545 && code != EXACT_DIV_EXPR 986 if (vr1.varying_p ())
1546 && code != ROUND_DIV_EXPR 987 {
1547 && code != TRUNC_MOD_EXPR 988 vr1_kind = VR_RANGE;
1548 && code != MIN_EXPR 989 vr1_min = vrp_val_min (expr_type);
1549 && code != MAX_EXPR 990 vr1_max = vrp_val_max (expr_type);
1550 && code != PLUS_EXPR 991 }
1551 && code != MINUS_EXPR 992
1552 && code != RSHIFT_EXPR 993 const bool minus_p = (code == MINUS_EXPR);
1553 && code != POINTER_PLUS_EXPR 994 tree min_op0 = vr0_min;
1554 && (vr0.varying_p () 995 tree min_op1 = minus_p ? vr1_max : vr1_min;
1555 || vr1.varying_p () 996 tree max_op0 = vr0_max;
1556 || vr0.kind () != vr1.kind () 997 tree max_op1 = minus_p ? vr1_min : vr1_max;
1557 || vr0.symbolic_p () 998 tree sym_min_op0 = NULL_TREE;
1558 || vr1.symbolic_p ())) 999 tree sym_min_op1 = NULL_TREE;
1559 { 1000 tree sym_max_op0 = NULL_TREE;
1560 set_value_range_to_varying (vr); 1001 tree sym_max_op1 = NULL_TREE;
1002 bool neg_min_op0, neg_min_op1, neg_max_op0, neg_max_op1;
1003
1004 neg_min_op0 = neg_min_op1 = neg_max_op0 = neg_max_op1 = false;
1005
1006 /* If we have a PLUS or MINUS with two VR_RANGEs, either constant or
1007 single-symbolic ranges, try to compute the precise resulting range,
1008 but only if we know that this resulting range will also be constant
1009 or single-symbolic. */
1010 if (vr0_kind == VR_RANGE && vr1_kind == VR_RANGE
1011 && (TREE_CODE (min_op0) == INTEGER_CST
1012 || (sym_min_op0
1013 = get_single_symbol (min_op0, &neg_min_op0, &min_op0)))
1014 && (TREE_CODE (min_op1) == INTEGER_CST
1015 || (sym_min_op1
1016 = get_single_symbol (min_op1, &neg_min_op1, &min_op1)))
1017 && (!(sym_min_op0 && sym_min_op1)
1018 || (sym_min_op0 == sym_min_op1
1019 && neg_min_op0 == (minus_p ? neg_min_op1 : !neg_min_op1)))
1020 && (TREE_CODE (max_op0) == INTEGER_CST
1021 || (sym_max_op0
1022 = get_single_symbol (max_op0, &neg_max_op0, &max_op0)))
1023 && (TREE_CODE (max_op1) == INTEGER_CST
1024 || (sym_max_op1
1025 = get_single_symbol (max_op1, &neg_max_op1, &max_op1)))
1026 && (!(sym_max_op0 && sym_max_op1)
1027 || (sym_max_op0 == sym_max_op1
1028 && neg_max_op0 == (minus_p ? neg_max_op1 : !neg_max_op1))))
1029 {
1030 wide_int wmin, wmax;
1031 wi::overflow_type min_ovf = wi::OVF_NONE;
1032 wi::overflow_type max_ovf = wi::OVF_NONE;
1033
1034 /* Build the bounds. */
1035 combine_bound (code, wmin, min_ovf, expr_type, min_op0, min_op1);
1036 combine_bound (code, wmax, max_ovf, expr_type, max_op0, max_op1);
1037
1038 /* If the resulting range will be symbolic, we need to eliminate any
1039 explicit or implicit overflow introduced in the above computation
1040 because compare_values could make an incorrect use of it. That's
1041 why we require one of the ranges to be a singleton. */
1042 if ((sym_min_op0 != sym_min_op1 || sym_max_op0 != sym_max_op1)
1043 && ((bool)min_ovf || (bool)max_ovf
1044 || (min_op0 != max_op0 && min_op1 != max_op1)))
1045 {
1046 vr->set_varying (expr_type);
1047 return;
1048 }
1049
1050 /* Adjust the range for possible overflow. */
1051 set_value_range_with_overflow (kind, min, max, expr_type,
1052 wmin, wmax, min_ovf, max_ovf);
1053 if (kind == VR_VARYING)
1054 {
1055 vr->set_varying (expr_type);
1056 return;
1057 }
1058
1059 /* Build the symbolic bounds if needed. */
1060 adjust_symbolic_bound (min, code, expr_type,
1061 sym_min_op0, sym_min_op1,
1062 neg_min_op0, neg_min_op1);
1063 adjust_symbolic_bound (max, code, expr_type,
1064 sym_max_op0, sym_max_op1,
1065 neg_max_op0, neg_max_op1);
1066 }
1067 else
1068 {
1069 /* For other cases, for example if we have a PLUS_EXPR with two
1070 VR_ANTI_RANGEs, drop to VR_VARYING. It would take more effort
1071 to compute a precise range for such a case.
1072 ??? General even mixed range kind operations can be expressed
1073 by for example transforming ~[3, 5] + [1, 2] to range-only
1074 operations and a union primitive:
1075 [-INF, 2] + [1, 2] U [5, +INF] + [1, 2]
1076 [-INF+1, 4] U [6, +INF(OVF)]
1077 though usually the union is not exactly representable with
1078 a single range or anti-range as the above is
1079 [-INF+1, +INF(OVF)] intersected with ~[5, 5]
1080 but one could use a scheme similar to equivalences for this. */
1081 vr->set_varying (expr_type);
1561 return; 1082 return;
1562 } 1083 }
1563
1564 /* Now evaluate the expression to determine the new range. */
1565 if (POINTER_TYPE_P (expr_type))
1566 {
1567 if (code == MIN_EXPR || code == MAX_EXPR)
1568 {
1569 /* For MIN/MAX expressions with pointers, we only care about
1570 nullness, if both are non null, then the result is nonnull.
1571 If both are null, then the result is null. Otherwise they
1572 are varying. */
1573 if (!range_includes_zero_p (&vr0) && !range_includes_zero_p (&vr1))
1574 set_value_range_to_nonnull (vr, expr_type);
1575 else if (range_is_null (&vr0) && range_is_null (&vr1))
1576 set_value_range_to_null (vr, expr_type);
1577 else
1578 set_value_range_to_varying (vr);
1579 }
1580 else if (code == POINTER_PLUS_EXPR)
1581 {
1582 /* For pointer types, we are really only interested in asserting
1583 whether the expression evaluates to non-NULL. */
1584 if (!range_includes_zero_p (&vr0)
1585 || !range_includes_zero_p (&vr1))
1586 set_value_range_to_nonnull (vr, expr_type);
1587 else if (range_is_null (&vr0) && range_is_null (&vr1))
1588 set_value_range_to_null (vr, expr_type);
1589 else
1590 set_value_range_to_varying (vr);
1591 }
1592 else if (code == BIT_AND_EXPR)
1593 {
1594 /* For pointer types, we are really only interested in asserting
1595 whether the expression evaluates to non-NULL. */
1596 if (!range_includes_zero_p (&vr0) && !range_includes_zero_p (&vr1))
1597 set_value_range_to_nonnull (vr, expr_type);
1598 else if (range_is_null (&vr0) || range_is_null (&vr1))
1599 set_value_range_to_null (vr, expr_type);
1600 else
1601 set_value_range_to_varying (vr);
1602 }
1603 else
1604 set_value_range_to_varying (vr);
1605
1606 return;
1607 }
1608
1609 /* For integer ranges, apply the operation to each end of the
1610 range and see what we end up with. */
1611 if (code == PLUS_EXPR || code == MINUS_EXPR)
1612 {
1613 /* This will normalize things such that calculating
1614 [0,0] - VR_VARYING is not dropped to varying, but is
1615 calculated as [MIN+1, MAX]. */
1616 if (vr0.varying_p ())
1617 vr0.update (VR_RANGE,
1618 vrp_val_min (expr_type),
1619 vrp_val_max (expr_type));
1620 if (vr1.varying_p ())
1621 vr1.update (VR_RANGE,
1622 vrp_val_min (expr_type),
1623 vrp_val_max (expr_type));
1624
1625 const bool minus_p = (code == MINUS_EXPR);
1626 tree min_op0 = vr0.min ();
1627 tree min_op1 = minus_p ? vr1.max () : vr1.min ();
1628 tree max_op0 = vr0.max ();
1629 tree max_op1 = minus_p ? vr1.min () : vr1.max ();
1630 tree sym_min_op0 = NULL_TREE;
1631 tree sym_min_op1 = NULL_TREE;
1632 tree sym_max_op0 = NULL_TREE;
1633 tree sym_max_op1 = NULL_TREE;
1634 bool neg_min_op0, neg_min_op1, neg_max_op0, neg_max_op1;
1635
1636 neg_min_op0 = neg_min_op1 = neg_max_op0 = neg_max_op1 = false;
1637
1638 /* If we have a PLUS or MINUS with two VR_RANGEs, either constant or
1639 single-symbolic ranges, try to compute the precise resulting range,
1640 but only if we know that this resulting range will also be constant
1641 or single-symbolic. */
1642 if (vr0.kind () == VR_RANGE && vr1.kind () == VR_RANGE
1643 && (TREE_CODE (min_op0) == INTEGER_CST
1644 || (sym_min_op0
1645 = get_single_symbol (min_op0, &neg_min_op0, &min_op0)))
1646 && (TREE_CODE (min_op1) == INTEGER_CST
1647 || (sym_min_op1
1648 = get_single_symbol (min_op1, &neg_min_op1, &min_op1)))
1649 && (!(sym_min_op0 && sym_min_op1)
1650 || (sym_min_op0 == sym_min_op1
1651 && neg_min_op0 == (minus_p ? neg_min_op1 : !neg_min_op1)))
1652 && (TREE_CODE (max_op0) == INTEGER_CST
1653 || (sym_max_op0
1654 = get_single_symbol (max_op0, &neg_max_op0, &max_op0)))
1655 && (TREE_CODE (max_op1) == INTEGER_CST
1656 || (sym_max_op1
1657 = get_single_symbol (max_op1, &neg_max_op1, &max_op1)))
1658 && (!(sym_max_op0 && sym_max_op1)
1659 || (sym_max_op0 == sym_max_op1
1660 && neg_max_op0 == (minus_p ? neg_max_op1 : !neg_max_op1))))
1661 {
1662 wide_int wmin, wmax;
1663 wi::overflow_type min_ovf = wi::OVF_NONE;
1664 wi::overflow_type max_ovf = wi::OVF_NONE;
1665
1666 /* Build the bounds. */
1667 combine_bound (code, wmin, min_ovf, expr_type, min_op0, min_op1);
1668 combine_bound (code, wmax, max_ovf, expr_type, max_op0, max_op1);
1669
1670 /* If we have overflow for the constant part and the resulting
1671 range will be symbolic, drop to VR_VARYING. */
1672 if (((bool)min_ovf && sym_min_op0 != sym_min_op1)
1673 || ((bool)max_ovf && sym_max_op0 != sym_max_op1))
1674 {
1675 set_value_range_to_varying (vr);
1676 return;
1677 }
1678
1679 /* Adjust the range for possible overflow. */
1680 min = NULL_TREE;
1681 max = NULL_TREE;
1682 set_value_range_with_overflow (type, min, max, expr_type,
1683 wmin, wmax, min_ovf, max_ovf);
1684 if (type == VR_VARYING)
1685 {
1686 set_value_range_to_varying (vr);
1687 return;
1688 }
1689
1690 /* Build the symbolic bounds if needed. */
1691 adjust_symbolic_bound (min, code, expr_type,
1692 sym_min_op0, sym_min_op1,
1693 neg_min_op0, neg_min_op1);
1694 adjust_symbolic_bound (max, code, expr_type,
1695 sym_max_op0, sym_max_op1,
1696 neg_max_op0, neg_max_op1);
1697 }
1698 else
1699 {
1700 /* For other cases, for example if we have a PLUS_EXPR with two
1701 VR_ANTI_RANGEs, drop to VR_VARYING. It would take more effort
1702 to compute a precise range for such a case.
1703 ??? General even mixed range kind operations can be expressed
1704 by for example transforming ~[3, 5] + [1, 2] to range-only
1705 operations and a union primitive:
1706 [-INF, 2] + [1, 2] U [5, +INF] + [1, 2]
1707 [-INF+1, 4] U [6, +INF(OVF)]
1708 though usually the union is not exactly representable with
1709 a single range or anti-range as the above is
1710 [-INF+1, +INF(OVF)] intersected with ~[5, 5]
1711 but one could use a scheme similar to equivalences for this. */
1712 set_value_range_to_varying (vr);
1713 return;
1714 }
1715 }
1716 else if (code == MIN_EXPR
1717 || code == MAX_EXPR)
1718 {
1719 wide_int wmin, wmax;
1720 wide_int vr0_min, vr0_max;
1721 wide_int vr1_min, vr1_max;
1722 extract_range_into_wide_ints (&vr0, sign, prec, vr0_min, vr0_max);
1723 extract_range_into_wide_ints (&vr1, sign, prec, vr1_min, vr1_max);
1724 if (wide_int_range_min_max (wmin, wmax, code, sign, prec,
1725 vr0_min, vr0_max, vr1_min, vr1_max))
1726 vr->update (VR_RANGE, wide_int_to_tree (expr_type, wmin),
1727 wide_int_to_tree (expr_type, wmax));
1728 else
1729 set_value_range_to_varying (vr);
1730 return;
1731 }
1732 else if (code == MULT_EXPR)
1733 {
1734 if (!range_int_cst_p (&vr0)
1735 || !range_int_cst_p (&vr1))
1736 {
1737 set_value_range_to_varying (vr);
1738 return;
1739 }
1740 extract_range_from_multiplicative_op (vr, code, &vr0, &vr1);
1741 return;
1742 }
1743 else if (code == RSHIFT_EXPR
1744 || code == LSHIFT_EXPR)
1745 {
1746 if (range_int_cst_p (&vr1)
1747 && !wide_int_range_shift_undefined_p
1748 (TYPE_SIGN (TREE_TYPE (vr1.min ())),
1749 prec,
1750 wi::to_wide (vr1.min ()),
1751 wi::to_wide (vr1.max ())))
1752 {
1753 if (code == RSHIFT_EXPR)
1754 {
1755 /* Even if vr0 is VARYING or otherwise not usable, we can derive
1756 useful ranges just from the shift count. E.g.
1757 x >> 63 for signed 64-bit x is always [-1, 0]. */
1758 if (vr0.kind () != VR_RANGE || vr0.symbolic_p ())
1759 vr0.update (VR_RANGE,
1760 vrp_val_min (expr_type),
1761 vrp_val_max (expr_type));
1762 extract_range_from_multiplicative_op (vr, code, &vr0, &vr1);
1763 return;
1764 }
1765 else if (code == LSHIFT_EXPR
1766 && range_int_cst_p (&vr0))
1767 {
1768 wide_int res_lb, res_ub;
1769 if (wide_int_range_lshift (res_lb, res_ub, sign, prec,
1770 wi::to_wide (vr0.min ()),
1771 wi::to_wide (vr0.max ()),
1772 wi::to_wide (vr1.min ()),
1773 wi::to_wide (vr1.max ()),
1774 TYPE_OVERFLOW_UNDEFINED (expr_type)))
1775 {
1776 min = wide_int_to_tree (expr_type, res_lb);
1777 max = wide_int_to_tree (expr_type, res_ub);
1778 vr->set_and_canonicalize (VR_RANGE, min, max, NULL);
1779 return;
1780 }
1781 }
1782 }
1783 set_value_range_to_varying (vr);
1784 return;
1785 }
1786 else if (code == TRUNC_DIV_EXPR
1787 || code == FLOOR_DIV_EXPR
1788 || code == CEIL_DIV_EXPR
1789 || code == EXACT_DIV_EXPR
1790 || code == ROUND_DIV_EXPR)
1791 {
1792 wide_int dividend_min, dividend_max, divisor_min, divisor_max;
1793 wide_int wmin, wmax, extra_min, extra_max;
1794 bool extra_range_p;
1795
1796 /* Special case explicit division by zero as undefined. */
1797 if (range_is_null (&vr1))
1798 {
1799 set_value_range_to_undefined (vr);
1800 return;
1801 }
1802
1803 /* First, normalize ranges into constants we can handle. Note
1804 that VR_ANTI_RANGE's of constants were already normalized
1805 before arriving here.
1806
1807 NOTE: As a future improvement, we may be able to do better
1808 with mixed symbolic (anti-)ranges like [0, A]. See note in
1809 ranges_from_anti_range. */
1810 extract_range_into_wide_ints (&vr0, sign, prec,
1811 dividend_min, dividend_max);
1812 extract_range_into_wide_ints (&vr1, sign, prec,
1813 divisor_min, divisor_max);
1814 if (!wide_int_range_div (wmin, wmax, code, sign, prec,
1815 dividend_min, dividend_max,
1816 divisor_min, divisor_max,
1817 TYPE_OVERFLOW_UNDEFINED (expr_type),
1818 extra_range_p, extra_min, extra_max))
1819 {
1820 set_value_range_to_varying (vr);
1821 return;
1822 }
1823 set_value_range (vr, VR_RANGE,
1824 wide_int_to_tree (expr_type, wmin),
1825 wide_int_to_tree (expr_type, wmax), NULL);
1826 if (extra_range_p)
1827 {
1828 value_range extra_range;
1829 set_value_range (&extra_range, VR_RANGE,
1830 wide_int_to_tree (expr_type, extra_min),
1831 wide_int_to_tree (expr_type, extra_max), NULL);
1832 vr->union_ (&extra_range);
1833 }
1834 return;
1835 }
1836 else if (code == TRUNC_MOD_EXPR)
1837 {
1838 if (range_is_null (&vr1))
1839 {
1840 set_value_range_to_undefined (vr);
1841 return;
1842 }
1843 wide_int wmin, wmax, tmp;
1844 wide_int vr0_min, vr0_max, vr1_min, vr1_max;
1845 extract_range_into_wide_ints (&vr0, sign, prec, vr0_min, vr0_max);
1846 extract_range_into_wide_ints (&vr1, sign, prec, vr1_min, vr1_max);
1847 wide_int_range_trunc_mod (wmin, wmax, sign, prec,
1848 vr0_min, vr0_max, vr1_min, vr1_max);
1849 min = wide_int_to_tree (expr_type, wmin);
1850 max = wide_int_to_tree (expr_type, wmax);
1851 set_value_range (vr, VR_RANGE, min, max, NULL);
1852 return;
1853 }
1854 else if (code == BIT_AND_EXPR || code == BIT_IOR_EXPR || code == BIT_XOR_EXPR)
1855 {
1856 wide_int may_be_nonzero0, may_be_nonzero1;
1857 wide_int must_be_nonzero0, must_be_nonzero1;
1858 wide_int wmin, wmax;
1859 wide_int vr0_min, vr0_max, vr1_min, vr1_max;
1860 vrp_set_zero_nonzero_bits (expr_type, &vr0,
1861 &may_be_nonzero0, &must_be_nonzero0);
1862 vrp_set_zero_nonzero_bits (expr_type, &vr1,
1863 &may_be_nonzero1, &must_be_nonzero1);
1864 extract_range_into_wide_ints (&vr0, sign, prec, vr0_min, vr0_max);
1865 extract_range_into_wide_ints (&vr1, sign, prec, vr1_min, vr1_max);
1866 if (code == BIT_AND_EXPR)
1867 {
1868 if (wide_int_range_bit_and (wmin, wmax, sign, prec,
1869 vr0_min, vr0_max,
1870 vr1_min, vr1_max,
1871 must_be_nonzero0,
1872 may_be_nonzero0,
1873 must_be_nonzero1,
1874 may_be_nonzero1))
1875 {
1876 min = wide_int_to_tree (expr_type, wmin);
1877 max = wide_int_to_tree (expr_type, wmax);
1878 set_value_range (vr, VR_RANGE, min, max, NULL);
1879 }
1880 else
1881 set_value_range_to_varying (vr);
1882 return;
1883 }
1884 else if (code == BIT_IOR_EXPR)
1885 {
1886 if (wide_int_range_bit_ior (wmin, wmax, sign,
1887 vr0_min, vr0_max,
1888 vr1_min, vr1_max,
1889 must_be_nonzero0,
1890 may_be_nonzero0,
1891 must_be_nonzero1,
1892 may_be_nonzero1))
1893 {
1894 min = wide_int_to_tree (expr_type, wmin);
1895 max = wide_int_to_tree (expr_type, wmax);
1896 set_value_range (vr, VR_RANGE, min, max, NULL);
1897 }
1898 else
1899 set_value_range_to_varying (vr);
1900 return;
1901 }
1902 else if (code == BIT_XOR_EXPR)
1903 {
1904 if (wide_int_range_bit_xor (wmin, wmax, sign, prec,
1905 must_be_nonzero0,
1906 may_be_nonzero0,
1907 must_be_nonzero1,
1908 may_be_nonzero1))
1909 {
1910 min = wide_int_to_tree (expr_type, wmin);
1911 max = wide_int_to_tree (expr_type, wmax);
1912 set_value_range (vr, VR_RANGE, min, max, NULL);
1913 }
1914 else
1915 set_value_range_to_varying (vr);
1916 return;
1917 }
1918 }
1919 else
1920 gcc_unreachable ();
1921 1084
1922 /* If either MIN or MAX overflowed, then set the resulting range to 1085 /* If either MIN or MAX overflowed, then set the resulting range to
1923 VARYING. */ 1086 VARYING. */
1924 if (min == NULL_TREE 1087 if (min == NULL_TREE
1925 || TREE_OVERFLOW_P (min) 1088 || TREE_OVERFLOW_P (min)
1926 || max == NULL_TREE 1089 || max == NULL_TREE
1927 || TREE_OVERFLOW_P (max)) 1090 || TREE_OVERFLOW_P (max))
1928 { 1091 {
1929 set_value_range_to_varying (vr); 1092 vr->set_varying (expr_type);
1930 return; 1093 return;
1931 } 1094 }
1932 1095
1933 /* We punt for [-INF, +INF]. 1096 int cmp = compare_values (min, max);
1934 We learn nothing when we have INF on both sides.
1935 Note that we do accept [-INF, -INF] and [+INF, +INF]. */
1936 if (vrp_val_is_min (min) && vrp_val_is_max (max))
1937 {
1938 set_value_range_to_varying (vr);
1939 return;
1940 }
1941
1942 cmp = compare_values (min, max);
1943 if (cmp == -2 || cmp == 1) 1097 if (cmp == -2 || cmp == 1)
1944 { 1098 {
1945 /* If the new range has its limits swapped around (MIN > MAX), 1099 /* If the new range has its limits swapped around (MIN > MAX),
1946 then the operation caused one of them to wrap around, mark 1100 then the operation caused one of them to wrap around, mark
1947 the new range VARYING. */ 1101 the new range VARYING. */
1948 set_value_range_to_varying (vr); 1102 vr->set_varying (expr_type);
1949 } 1103 }
1950 else 1104 else
1951 set_value_range (vr, type, min, max, NULL); 1105 vr->set (min, max, kind);
1952 } 1106 }
1953 1107
1954 /* Extract range information from a unary operation CODE based on 1108 /* Return the range-ops handler for CODE and EXPR_TYPE. If no
1955 the range of its operand *VR0 with type OP0_TYPE with resulting type TYPE. 1109 suitable operator is found, return NULL and set VR to VARYING. */
1956 The resulting range is stored in *VR. */ 1110
1111 static const range_operator *
1112 get_range_op_handler (value_range *vr,
1113 enum tree_code code,
1114 tree expr_type)
1115 {
1116 const range_operator *op = range_op_handler (code, expr_type);
1117 if (!op)
1118 vr->set_varying (expr_type);
1119 return op;
1120 }
1121
1122 /* If the types passed are supported, return TRUE, otherwise set VR to
1123 VARYING and return FALSE. */
1124
1125 static bool
1126 supported_types_p (value_range *vr,
1127 tree type0,
1128 tree type1 = NULL)
1129 {
1130 if (!value_range::supports_type_p (type0)
1131 || (type1 && !value_range::supports_type_p (type1)))
1132 {
1133 vr->set_varying (type0);
1134 return false;
1135 }
1136 return true;
1137 }
1138
1139 /* If any of the ranges passed are defined, return TRUE, otherwise set
1140 VR to UNDEFINED and return FALSE. */
1141
1142 static bool
1143 defined_ranges_p (value_range *vr,
1144 const value_range *vr0, const value_range *vr1 = NULL)
1145 {
1146 if (vr0->undefined_p () && (!vr1 || vr1->undefined_p ()))
1147 {
1148 vr->set_undefined ();
1149 return false;
1150 }
1151 return true;
1152 }
1153
1154 static value_range
1155 drop_undefines_to_varying (const value_range *vr, tree expr_type)
1156 {
1157 if (vr->undefined_p ())
1158 return value_range (expr_type);
1159 else
1160 return *vr;
1161 }
1162
1163 /* If any operand is symbolic, perform a binary operation on them and
1164 return TRUE, otherwise return FALSE. */
1165
1166 static bool
1167 range_fold_binary_symbolics_p (value_range *vr,
1168 tree_code code,
1169 tree expr_type,
1170 const value_range *vr0, const value_range *vr1)
1171 {
1172 if (vr0->symbolic_p () || vr1->symbolic_p ())
1173 {
1174 if ((code == PLUS_EXPR || code == MINUS_EXPR))
1175 {
1176 extract_range_from_plus_minus_expr (vr, code, expr_type, vr0, vr1);
1177 return true;
1178 }
1179 if (POINTER_TYPE_P (expr_type) && code == POINTER_PLUS_EXPR)
1180 {
1181 extract_range_from_pointer_plus_expr (vr, code, expr_type, vr0, vr1);
1182 return true;
1183 }
1184 const range_operator *op = get_range_op_handler (vr, code, expr_type);
1185 value_range vr0_cst (*vr0), vr1_cst (*vr1);
1186 vr0_cst.normalize_symbolics ();
1187 vr1_cst.normalize_symbolics ();
1188 return op->fold_range (*vr, expr_type, vr0_cst, vr1_cst);
1189 }
1190 return false;
1191 }
1192
1193 /* If operand is symbolic, perform a unary operation on it and return
1194 TRUE, otherwise return FALSE. */
1195
1196 static bool
1197 range_fold_unary_symbolics_p (value_range *vr,
1198 tree_code code,
1199 tree expr_type,
1200 const value_range *vr0)
1201 {
1202 if (vr0->symbolic_p ())
1203 {
1204 if (code == NEGATE_EXPR)
1205 {
1206 /* -X is simply 0 - X. */
1207 value_range zero;
1208 zero.set_zero (vr0->type ());
1209 range_fold_binary_expr (vr, MINUS_EXPR, expr_type, &zero, vr0);
1210 return true;
1211 }
1212 if (code == BIT_NOT_EXPR)
1213 {
1214 /* ~X is simply -1 - X. */
1215 value_range minusone;
1216 minusone.set (build_int_cst (vr0->type (), -1));
1217 range_fold_binary_expr (vr, MINUS_EXPR, expr_type, &minusone, vr0);
1218 return true;
1219 }
1220 const range_operator *op = get_range_op_handler (vr, code, expr_type);
1221 value_range vr0_cst (*vr0);
1222 vr0_cst.normalize_symbolics ();
1223 return op->fold_range (*vr, expr_type, vr0_cst, value_range (expr_type));
1224 }
1225 return false;
1226 }
1227
1228 /* Perform a binary operation on a pair of ranges. */
1957 1229
1958 void 1230 void
1959 extract_range_from_unary_expr (value_range *vr, 1231 range_fold_binary_expr (value_range *vr,
1960 enum tree_code code, tree type, 1232 enum tree_code code,
1961 const value_range *vr0_, tree op0_type) 1233 tree expr_type,
1962 { 1234 const value_range *vr0_,
1963 signop sign = TYPE_SIGN (type); 1235 const value_range *vr1_)
1964 unsigned int prec = TYPE_PRECISION (type); 1236 {
1965 value_range vr0 = *vr0_; 1237 if (!supported_types_p (vr, expr_type)
1966 value_range vrtem0, vrtem1; 1238 || !defined_ranges_p (vr, vr0_, vr1_))
1967 1239 return;
1968 /* VRP only operates on integral and pointer types. */ 1240 const range_operator *op = get_range_op_handler (vr, code, expr_type);
1969 if (!(INTEGRAL_TYPE_P (op0_type) 1241 if (!op)
1970 || POINTER_TYPE_P (op0_type)) 1242 return;
1971 || !(INTEGRAL_TYPE_P (type) 1243
1972 || POINTER_TYPE_P (type))) 1244 value_range vr0 = drop_undefines_to_varying (vr0_, expr_type);
1973 { 1245 value_range vr1 = drop_undefines_to_varying (vr1_, expr_type);
1974 set_value_range_to_varying (vr); 1246 if (range_fold_binary_symbolics_p (vr, code, expr_type, &vr0, &vr1))
1975 return; 1247 return;
1976 } 1248
1977 1249 vr0.normalize_addresses ();
1978 /* If VR0 is UNDEFINED, so is the result. */ 1250 vr1.normalize_addresses ();
1979 if (vr0.undefined_p ()) 1251 op->fold_range (*vr, expr_type, vr0, vr1);
1980 { 1252 }
1981 set_value_range_to_undefined (vr); 1253
1982 return; 1254 /* Perform a unary operation on a range. */
1983 }
1984
1985 /* Handle operations that we express in terms of others. */
1986 if (code == PAREN_EXPR || code == OBJ_TYPE_REF)
1987 {
1988 /* PAREN_EXPR and OBJ_TYPE_REF are simple copies. */
1989 vr->deep_copy (&vr0);
1990 return;
1991 }
1992 else if (code == NEGATE_EXPR)
1993 {
1994 /* -X is simply 0 - X, so re-use existing code that also handles
1995 anti-ranges fine. */
1996 value_range zero;
1997 set_value_range_to_value (&zero, build_int_cst (type, 0), NULL);
1998 extract_range_from_binary_expr_1 (vr, MINUS_EXPR, type, &zero, &vr0);
1999 return;
2000 }
2001 else if (code == BIT_NOT_EXPR)
2002 {
2003 /* ~X is simply -1 - X, so re-use existing code that also handles
2004 anti-ranges fine. */
2005 value_range minusone;
2006 set_value_range_to_value (&minusone, build_int_cst (type, -1), NULL);
2007 extract_range_from_binary_expr_1 (vr, MINUS_EXPR,
2008 type, &minusone, &vr0);
2009 return;
2010 }
2011
2012 /* Now canonicalize anti-ranges to ranges when they are not symbolic
2013 and express op ~[] as (op []') U (op []''). */
2014 if (vr0.kind () == VR_ANTI_RANGE
2015 && ranges_from_anti_range (&vr0, &vrtem0, &vrtem1))
2016 {
2017 extract_range_from_unary_expr (vr, code, type, &vrtem0, op0_type);
2018 if (!vrtem1.undefined_p ())
2019 {
2020 value_range vrres;
2021 extract_range_from_unary_expr (&vrres, code, type,
2022 &vrtem1, op0_type);
2023 vr->union_ (&vrres);
2024 }
2025 return;
2026 }
2027
2028 if (CONVERT_EXPR_CODE_P (code))
2029 {
2030 tree inner_type = op0_type;
2031 tree outer_type = type;
2032
2033 /* If the expression involves a pointer, we are only interested in
2034 determining if it evaluates to NULL [0, 0] or non-NULL (~[0, 0]).
2035
2036 This may lose precision when converting (char *)~[0,2] to
2037 int, because we'll forget that the pointer can also not be 1
2038 or 2. In practice we don't care, as this is some idiot
2039 storing a magic constant to a pointer. */
2040 if (POINTER_TYPE_P (type) || POINTER_TYPE_P (op0_type))
2041 {
2042 if (!range_includes_zero_p (&vr0))
2043 set_value_range_to_nonnull (vr, type);
2044 else if (range_is_null (&vr0))
2045 set_value_range_to_null (vr, type);
2046 else
2047 set_value_range_to_varying (vr);
2048 return;
2049 }
2050
2051 /* The POINTER_TYPE_P code above will have dealt with all
2052 pointer anti-ranges. Any remaining anti-ranges at this point
2053 will be integer conversions from SSA names that will be
2054 normalized into VARYING. For instance: ~[x_55, x_55]. */
2055 gcc_assert (vr0.kind () != VR_ANTI_RANGE
2056 || TREE_CODE (vr0.min ()) != INTEGER_CST);
2057
2058 /* NOTES: Previously we were returning VARYING for all symbolics, but
2059 we can do better by treating them as [-MIN, +MAX]. For
2060 example, converting [SYM, SYM] from INT to LONG UNSIGNED,
2061 we can return: ~[0x8000000, 0xffffffff7fffffff].
2062
2063 We were also failing to convert ~[0,0] from char* to unsigned,
2064 instead choosing to return VR_VARYING. Now we return ~[0,0]. */
2065 wide_int vr0_min, vr0_max, wmin, wmax;
2066 signop inner_sign = TYPE_SIGN (inner_type);
2067 signop outer_sign = TYPE_SIGN (outer_type);
2068 unsigned inner_prec = TYPE_PRECISION (inner_type);
2069 unsigned outer_prec = TYPE_PRECISION (outer_type);
2070 extract_range_into_wide_ints (&vr0, inner_sign, inner_prec,
2071 vr0_min, vr0_max);
2072 if (wide_int_range_convert (wmin, wmax,
2073 inner_sign, inner_prec,
2074 outer_sign, outer_prec,
2075 vr0_min, vr0_max))
2076 {
2077 tree min = wide_int_to_tree (outer_type, wmin);
2078 tree max = wide_int_to_tree (outer_type, wmax);
2079 vr->set_and_canonicalize (VR_RANGE, min, max, NULL);
2080 }
2081 else
2082 set_value_range_to_varying (vr);
2083 return;
2084 }
2085 else if (code == ABS_EXPR)
2086 {
2087 wide_int wmin, wmax;
2088 wide_int vr0_min, vr0_max;
2089 extract_range_into_wide_ints (&vr0, sign, prec, vr0_min, vr0_max);
2090 if (wide_int_range_abs (wmin, wmax, sign, prec, vr0_min, vr0_max,
2091 TYPE_OVERFLOW_UNDEFINED (type)))
2092 set_value_range (vr, VR_RANGE,
2093 wide_int_to_tree (type, wmin),
2094 wide_int_to_tree (type, wmax), NULL);
2095 else
2096 set_value_range_to_varying (vr);
2097 return;
2098 }
2099
2100 /* For unhandled operations fall back to varying. */
2101 set_value_range_to_varying (vr);
2102 return;
2103 }
2104
2105 /* Debugging dumps. */
2106
2107 void dump_value_range (FILE *, const value_range *);
2108 void debug_value_range (const value_range *);
2109 void dump_all_value_ranges (FILE *);
2110 void dump_vr_equiv (FILE *, bitmap);
2111 void debug_vr_equiv (bitmap);
2112 1255
2113 void 1256 void
2114 dump_value_range (FILE *file, const value_range *vr) 1257 range_fold_unary_expr (value_range *vr,
2115 { 1258 enum tree_code code, tree expr_type,
2116 if (!vr) 1259 const value_range *vr0,
2117 fprintf (file, "[]"); 1260 tree vr0_type)
2118 else 1261 {
2119 vr->dump (file); 1262 if (!supported_types_p (vr, expr_type, vr0_type)
2120 } 1263 || !defined_ranges_p (vr, vr0))
2121 1264 return;
2122 /* Dump value range VR to stderr. */ 1265 const range_operator *op = get_range_op_handler (vr, code, expr_type);
2123 1266 if (!op)
2124 DEBUG_FUNCTION void 1267 return;
2125 debug_value_range (const value_range *vr) 1268
2126 { 1269 if (range_fold_unary_symbolics_p (vr, code, expr_type, vr0))
2127 vr->dump (); 1270 return;
2128 } 1271
2129 1272 value_range vr0_cst (*vr0);
1273 vr0_cst.normalize_addresses ();
1274 op->fold_range (*vr, expr_type, vr0_cst, value_range (expr_type));
1275 }
2130 1276
2131 /* Given a COND_EXPR COND of the form 'V OP W', and an SSA name V, 1277 /* Given a COND_EXPR COND of the form 'V OP W', and an SSA name V,
2132 create a new SSA name N and return the assertion assignment 1278 create a new SSA name N and return the assertion assignment
2133 'N = ASSERT_EXPR <V, V OP W>'. */ 1279 'N = ASSERT_EXPR <V, V OP W>'. */
2134 1280
2281 1427
2282 DEBUG_FUNCTION void 1428 DEBUG_FUNCTION void
2283 debug_all_asserts (void) 1429 debug_all_asserts (void)
2284 { 1430 {
2285 dump_all_asserts (stderr); 1431 dump_all_asserts (stderr);
1432 }
1433
1434 /* Dump assert_info structure. */
1435
1436 void
1437 dump_assert_info (FILE *file, const assert_info &assert)
1438 {
1439 fprintf (file, "Assert for: ");
1440 print_generic_expr (file, assert.name);
1441 fprintf (file, "\n\tPREDICATE: expr=[");
1442 print_generic_expr (file, assert.expr);
1443 fprintf (file, "] %s ", get_tree_code_name (assert.comp_code));
1444 fprintf (file, "val=[");
1445 print_generic_expr (file, assert.val);
1446 fprintf (file, "]\n\n");
1447 }
1448
1449 DEBUG_FUNCTION void
1450 debug (const assert_info &assert)
1451 {
1452 dump_assert_info (stderr, assert);
1453 }
1454
1455 /* Dump a vector of assert_info's. */
1456
1457 void
1458 dump_asserts_info (FILE *file, const vec<assert_info> &asserts)
1459 {
1460 for (unsigned i = 0; i < asserts.length (); ++i)
1461 {
1462 dump_assert_info (file, asserts[i]);
1463 fprintf (file, "\n");
1464 }
1465 }
1466
1467 DEBUG_FUNCTION void
1468 debug (const vec<assert_info> &asserts)
1469 {
1470 dump_asserts_info (stderr, asserts);
2286 } 1471 }
2287 1472
2288 /* Push the assert info for NAME, EXPR, COMP_CODE and VAL to ASSERTS. */ 1473 /* Push the assert info for NAME, EXPR, COMP_CODE and VAL to ASSERTS. */
2289 1474
2290 static void 1475 static void
2817 || comp_code == GT_EXPR || comp_code == GE_EXPR) 2002 || comp_code == GT_EXPR || comp_code == GE_EXPR)
2818 && gimple_assign_cast_p (def_stmt)) 2003 && gimple_assign_cast_p (def_stmt))
2819 { 2004 {
2820 name2 = gimple_assign_rhs1 (def_stmt); 2005 name2 = gimple_assign_rhs1 (def_stmt);
2821 if (CONVERT_EXPR_CODE_P (rhs_code) 2006 if (CONVERT_EXPR_CODE_P (rhs_code)
2007 && TREE_CODE (name2) == SSA_NAME
2822 && INTEGRAL_TYPE_P (TREE_TYPE (name2)) 2008 && INTEGRAL_TYPE_P (TREE_TYPE (name2))
2823 && TYPE_UNSIGNED (TREE_TYPE (name2)) 2009 && TYPE_UNSIGNED (TREE_TYPE (name2))
2824 && prec == TYPE_PRECISION (TREE_TYPE (name2)) 2010 && prec == TYPE_PRECISION (TREE_TYPE (name2))
2825 && (comp_code == LE_EXPR || comp_code == GT_EXPR 2011 && (comp_code == LE_EXPR || comp_code == GT_EXPR
2826 || !tree_int_cst_equal (val, 2012 || !tree_int_cst_equal (val,
2906 2092
2907 if (new_val) 2093 if (new_val)
2908 add_assert_info (asserts, name2, tmp, new_comp_code, new_val); 2094 add_assert_info (asserts, name2, tmp, new_comp_code, new_val);
2909 } 2095 }
2910 2096
2097 /* If we have a conversion that doesn't change the value of the source
2098 simply register the same assert for it. */
2099 if (CONVERT_EXPR_CODE_P (rhs_code))
2100 {
2101 wide_int rmin, rmax;
2102 tree rhs1 = gimple_assign_rhs1 (def_stmt);
2103 if (INTEGRAL_TYPE_P (TREE_TYPE (rhs1))
2104 && TREE_CODE (rhs1) == SSA_NAME
2105 /* Make sure the relation preserves the upper/lower boundary of
2106 the range conservatively. */
2107 && (comp_code == NE_EXPR
2108 || comp_code == EQ_EXPR
2109 || (TYPE_SIGN (TREE_TYPE (name))
2110 == TYPE_SIGN (TREE_TYPE (rhs1)))
2111 || ((comp_code == LE_EXPR
2112 || comp_code == LT_EXPR)
2113 && !TYPE_UNSIGNED (TREE_TYPE (rhs1)))
2114 || ((comp_code == GE_EXPR
2115 || comp_code == GT_EXPR)
2116 && TYPE_UNSIGNED (TREE_TYPE (rhs1))))
2117 /* And the conversion does not alter the value we compare
2118 against and all values in rhs1 can be represented in
2119 the converted to type. */
2120 && int_fits_type_p (val, TREE_TYPE (rhs1))
2121 && ((TYPE_PRECISION (TREE_TYPE (name))
2122 > TYPE_PRECISION (TREE_TYPE (rhs1)))
2123 || (get_range_info (rhs1, &rmin, &rmax) == VR_RANGE
2124 && wi::fits_to_tree_p (rmin, TREE_TYPE (name))
2125 && wi::fits_to_tree_p (rmax, TREE_TYPE (name)))))
2126 add_assert_info (asserts, rhs1, rhs1,
2127 comp_code, fold_convert (TREE_TYPE (rhs1), val));
2128 }
2129
2911 /* Add asserts for NAME cmp CST and NAME being defined as 2130 /* Add asserts for NAME cmp CST and NAME being defined as
2912 NAME = NAME2 & CST2. 2131 NAME = NAME2 & CST2.
2913 2132
2914 Extract CST2 from the and. 2133 Extract CST2 from the and.
2915 2134
2945 gimple *def_stmt2 = SSA_NAME_DEF_STMT (name2); 2164 gimple *def_stmt2 = SSA_NAME_DEF_STMT (name2);
2946 if (gimple_assign_cast_p (def_stmt2)) 2165 if (gimple_assign_cast_p (def_stmt2))
2947 { 2166 {
2948 names[1] = gimple_assign_rhs1 (def_stmt2); 2167 names[1] = gimple_assign_rhs1 (def_stmt2);
2949 if (!CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt2)) 2168 if (!CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt2))
2169 || TREE_CODE (names[1]) != SSA_NAME
2950 || !INTEGRAL_TYPE_P (TREE_TYPE (names[1])) 2170 || !INTEGRAL_TYPE_P (TREE_TYPE (names[1]))
2951 || (TYPE_PRECISION (TREE_TYPE (name2)) 2171 || (TYPE_PRECISION (TREE_TYPE (name2))
2952 != TYPE_PRECISION (TREE_TYPE (names[1])))) 2172 != TYPE_PRECISION (TREE_TYPE (names[1]))))
2953 names[1] = NULL_TREE; 2173 names[1] = NULL_TREE;
2954 } 2174 }
3573 if (!live_on_edge (default_edge, op)) 2793 if (!live_on_edge (default_edge, op))
3574 return; 2794 return;
3575 2795
3576 /* Now register along the default label assertions that correspond to the 2796 /* Now register along the default label assertions that correspond to the
3577 anti-range of each label. */ 2797 anti-range of each label. */
3578 int insertion_limit = PARAM_VALUE (PARAM_MAX_VRP_SWITCH_ASSERTIONS); 2798 int insertion_limit = param_max_vrp_switch_assertions;
3579 if (insertion_limit == 0) 2799 if (insertion_limit == 0)
3580 return; 2800 return;
3581 2801
3582 /* We can't do this if the default case shares a label with another case. */ 2802 /* We can't do this if the default case shares a label with another case. */
3583 tree default_cl = gimple_switch_default_label (last); 2803 tree default_cl = gimple_switch_default_label (last);
4222 enum ssa_prop_result visit_phi (gphi *) FINAL OVERRIDE; 3442 enum ssa_prop_result visit_phi (gphi *) FINAL OVERRIDE;
4223 3443
4224 void vrp_initialize (void); 3444 void vrp_initialize (void);
4225 void vrp_finalize (bool); 3445 void vrp_finalize (bool);
4226 void check_all_array_refs (void); 3446 void check_all_array_refs (void);
4227 void check_array_ref (location_t, tree, bool); 3447 bool check_array_ref (location_t, tree, bool);
4228 void check_mem_ref (location_t, tree, bool); 3448 bool check_mem_ref (location_t, tree, bool);
4229 void search_for_addr_array (tree, location_t); 3449 void search_for_addr_array (tree, location_t);
4230 3450
4231 class vr_values vr_values; 3451 class vr_values vr_values;
4232 /* Temporary delegator to minimize code churn. */ 3452 /* Temporary delegator to minimize code churn. */
4233 value_range *get_value_range (const_tree op) 3453 const value_range_equiv *get_value_range (const_tree op)
4234 { return vr_values.get_value_range (op); } 3454 { return vr_values.get_value_range (op); }
3455 void set_def_to_varying (const_tree def)
3456 { vr_values.set_def_to_varying (def); }
4235 void set_defs_to_varying (gimple *stmt) 3457 void set_defs_to_varying (gimple *stmt)
4236 { return vr_values.set_defs_to_varying (stmt); } 3458 { vr_values.set_defs_to_varying (stmt); }
4237 void extract_range_from_stmt (gimple *stmt, edge *taken_edge_p, 3459 void extract_range_from_stmt (gimple *stmt, edge *taken_edge_p,
4238 tree *output_p, value_range *vr) 3460 tree *output_p, value_range_equiv *vr)
4239 { vr_values.extract_range_from_stmt (stmt, taken_edge_p, output_p, vr); } 3461 { vr_values.extract_range_from_stmt (stmt, taken_edge_p, output_p, vr); }
4240 bool update_value_range (const_tree op, value_range *vr) 3462 bool update_value_range (const_tree op, value_range_equiv *vr)
4241 { return vr_values.update_value_range (op, vr); } 3463 { return vr_values.update_value_range (op, vr); }
4242 void extract_range_basic (value_range *vr, gimple *stmt) 3464 void extract_range_basic (value_range_equiv *vr, gimple *stmt)
4243 { vr_values.extract_range_basic (vr, stmt); } 3465 { vr_values.extract_range_basic (vr, stmt); }
4244 void extract_range_from_phi_node (gphi *phi, value_range *vr) 3466 void extract_range_from_phi_node (gphi *phi, value_range_equiv *vr)
4245 { vr_values.extract_range_from_phi_node (phi, vr); } 3467 { vr_values.extract_range_from_phi_node (phi, vr); }
4246 }; 3468 };
4247 /* Checks one ARRAY_REF in REF, located at LOCUS. Ignores flexible arrays 3469 /* Checks one ARRAY_REF in REF, located at LOCUS. Ignores flexible arrays
4248 and "struct" hacks. If VRP can determine that the 3470 and "struct" hacks. If VRP can determine that the
4249 array subscript is a constant, check if it is outside valid 3471 array subscript is a constant, check if it is outside valid
4250 range. If the array subscript is a RANGE, warn if it is 3472 range. If the array subscript is a RANGE, warn if it is
4251 non-overlapping with valid range. 3473 non-overlapping with valid range.
4252 IGNORE_OFF_BY_ONE is true if the ARRAY_REF is inside a ADDR_EXPR. */ 3474 IGNORE_OFF_BY_ONE is true if the ARRAY_REF is inside a ADDR_EXPR.
4253 3475 Returns true if a warning has been issued. */
4254 void 3476
3477 bool
4255 vrp_prop::check_array_ref (location_t location, tree ref, 3478 vrp_prop::check_array_ref (location_t location, tree ref,
4256 bool ignore_off_by_one) 3479 bool ignore_off_by_one)
4257 { 3480 {
4258 const value_range *vr = NULL;
4259 tree low_sub, up_sub;
4260 tree low_bound, up_bound, up_bound_p1;
4261
4262 if (TREE_NO_WARNING (ref)) 3481 if (TREE_NO_WARNING (ref))
4263 return; 3482 return false;
4264 3483
4265 low_sub = up_sub = TREE_OPERAND (ref, 1); 3484 tree low_sub = TREE_OPERAND (ref, 1);
4266 up_bound = array_ref_up_bound (ref); 3485 tree up_sub = low_sub;
3486 tree up_bound = array_ref_up_bound (ref);
3487
3488 /* Referenced decl if one can be determined. */
3489 tree decl = NULL_TREE;
3490
3491 /* Set for accesses to interior zero-length arrays. */
3492 bool interior_zero_len = false;
3493
3494 tree up_bound_p1;
4267 3495
4268 if (!up_bound 3496 if (!up_bound
4269 || TREE_CODE (up_bound) != INTEGER_CST 3497 || TREE_CODE (up_bound) != INTEGER_CST
4270 || (warn_array_bounds < 2 3498 || (warn_array_bounds < 2
4271 && array_at_struct_end_p (ref))) 3499 && array_at_struct_end_p (ref)))
4283 up_bound = NULL_TREE; 3511 up_bound = NULL_TREE;
4284 up_bound_p1 = NULL_TREE; 3512 up_bound_p1 = NULL_TREE;
4285 } 3513 }
4286 else 3514 else
4287 { 3515 {
4288 tree maxbound = TYPE_MAX_VALUE (ptrdiff_type_node); 3516 tree ptrdiff_max = TYPE_MAX_VALUE (ptrdiff_type_node);
3517 tree maxbound = ptrdiff_max;
4289 tree arg = TREE_OPERAND (ref, 0); 3518 tree arg = TREE_OPERAND (ref, 0);
4290 poly_int64 off; 3519
4291 3520 const bool compref = TREE_CODE (arg) == COMPONENT_REF;
4292 if (get_addr_base_and_unit_offset (arg, &off) && known_gt (off, 0)) 3521 if (compref)
4293 maxbound = wide_int_to_tree (sizetype, 3522 {
4294 wi::sub (wi::to_wide (maxbound), 3523 /* Try to determine the size of the trailing array from
4295 off)); 3524 its initializer (if it has one). */
3525 if (tree refsize = component_ref_size (arg, &interior_zero_len))
3526 if (TREE_CODE (refsize) == INTEGER_CST)
3527 maxbound = refsize;
3528 }
3529
3530 if (maxbound == ptrdiff_max)
3531 {
3532 /* Try to determine the size of the base object. Avoid
3533 COMPONENT_REF already tried above. Using its DECL_SIZE
3534 size wouldn't necessarily be correct if the reference is
3535 to its flexible array member initialized in a different
3536 translation unit. */
3537 poly_int64 off;
3538 if (tree base = get_addr_base_and_unit_offset (arg, &off))
3539 {
3540 if (!compref && DECL_P (base))
3541 if (tree basesize = DECL_SIZE_UNIT (base))
3542 if (TREE_CODE (basesize) == INTEGER_CST)
3543 {
3544 maxbound = basesize;
3545 decl = base;
3546 }
3547
3548 if (known_gt (off, 0))
3549 maxbound = wide_int_to_tree (sizetype,
3550 wi::sub (wi::to_wide (maxbound),
3551 off));
3552 }
3553 }
4296 else 3554 else
4297 maxbound = fold_convert (sizetype, maxbound); 3555 maxbound = fold_convert (sizetype, maxbound);
4298 3556
4299 up_bound_p1 = int_const_binop (TRUNC_DIV_EXPR, maxbound, eltsize); 3557 up_bound_p1 = int_const_binop (TRUNC_DIV_EXPR, maxbound, eltsize);
4300 3558
4301 up_bound = int_const_binop (MINUS_EXPR, up_bound_p1, 3559 if (up_bound_p1 != NULL_TREE)
4302 build_int_cst (ptrdiff_type_node, 1)); 3560 up_bound = int_const_binop (MINUS_EXPR, up_bound_p1,
3561 build_int_cst (ptrdiff_type_node, 1));
3562 else
3563 up_bound = NULL_TREE;
4303 } 3564 }
4304 } 3565 }
4305 else 3566 else
4306 up_bound_p1 = int_const_binop (PLUS_EXPR, up_bound, 3567 up_bound_p1 = int_const_binop (PLUS_EXPR, up_bound,
4307 build_int_cst (TREE_TYPE (up_bound), 1)); 3568 build_int_cst (TREE_TYPE (up_bound), 1));
4308 3569
4309 low_bound = array_ref_low_bound (ref); 3570 tree low_bound = array_ref_low_bound (ref);
4310 3571
4311 tree artype = TREE_TYPE (TREE_OPERAND (ref, 0)); 3572 tree artype = TREE_TYPE (TREE_OPERAND (ref, 0));
4312 3573
4313 bool warned = false; 3574 bool warned = false;
4314 3575
4315 /* Empty array. */ 3576 /* Empty array. */
4316 if (up_bound && tree_int_cst_equal (low_bound, up_bound_p1)) 3577 if (up_bound && tree_int_cst_equal (low_bound, up_bound_p1))
4317 warned = warning_at (location, OPT_Warray_bounds, 3578 warned = warning_at (location, OPT_Warray_bounds,
4318 "array subscript %E is above array bounds of %qT", 3579 "array subscript %E is outside array bounds of %qT",
4319 low_bound, artype); 3580 low_sub, artype);
4320 3581
3582 const value_range_equiv *vr = NULL;
4321 if (TREE_CODE (low_sub) == SSA_NAME) 3583 if (TREE_CODE (low_sub) == SSA_NAME)
4322 { 3584 {
4323 vr = get_value_range (low_sub); 3585 vr = get_value_range (low_sub);
4324 if (!vr->undefined_p () && !vr->varying_p ()) 3586 if (!vr->undefined_p () && !vr->varying_p ())
4325 { 3587 {
4326 low_sub = vr->kind () == VR_RANGE ? vr->max () : vr->min (); 3588 low_sub = vr->kind () == VR_RANGE ? vr->max () : vr->min ();
4327 up_sub = vr->kind () == VR_RANGE ? vr->min () : vr->max (); 3589 up_sub = vr->kind () == VR_RANGE ? vr->min () : vr->max ();
4328 } 3590 }
4329 } 3591 }
4330 3592
4331 if (vr && vr->kind () == VR_ANTI_RANGE) 3593 if (warned)
3594 ; /* Do nothing. */
3595 else if (vr && vr->kind () == VR_ANTI_RANGE)
4332 { 3596 {
4333 if (up_bound 3597 if (up_bound
4334 && TREE_CODE (up_sub) == INTEGER_CST 3598 && TREE_CODE (up_sub) == INTEGER_CST
4335 && (ignore_off_by_one 3599 && (ignore_off_by_one
4336 ? tree_int_cst_lt (up_bound, up_sub) 3600 ? tree_int_cst_lt (up_bound, up_sub)
4345 else if (up_bound 3609 else if (up_bound
4346 && TREE_CODE (up_sub) == INTEGER_CST 3610 && TREE_CODE (up_sub) == INTEGER_CST
4347 && (ignore_off_by_one 3611 && (ignore_off_by_one
4348 ? !tree_int_cst_le (up_sub, up_bound_p1) 3612 ? !tree_int_cst_le (up_sub, up_bound_p1)
4349 : !tree_int_cst_le (up_sub, up_bound))) 3613 : !tree_int_cst_le (up_sub, up_bound)))
3614 warned = warning_at (location, OPT_Warray_bounds,
3615 "array subscript %E is above array bounds of %qT",
3616 up_sub, artype);
3617 else if (TREE_CODE (low_sub) == INTEGER_CST
3618 && tree_int_cst_lt (low_sub, low_bound))
3619 warned = warning_at (location, OPT_Warray_bounds,
3620 "array subscript %E is below array bounds of %qT",
3621 low_sub, artype);
3622
3623 if (!warned && interior_zero_len)
3624 warned = warning_at (location, OPT_Wzero_length_bounds,
3625 (TREE_CODE (low_sub) == INTEGER_CST
3626 ? G_("array subscript %E is outside the bounds "
3627 "of an interior zero-length array %qT")
3628 : G_("array subscript %qE is outside the bounds "
3629 "of an interior zero-length array %qT")),
3630 low_sub, artype);
3631
3632 if (warned)
4350 { 3633 {
4351 if (dump_file && (dump_flags & TDF_DETAILS)) 3634 if (dump_file && (dump_flags & TDF_DETAILS))
4352 { 3635 {
4353 fprintf (dump_file, "Array bound warning for "); 3636 fprintf (dump_file, "Array bound warning for ");
4354 dump_generic_expr (MSG_NOTE, TDF_SLIM, ref); 3637 dump_generic_expr (MSG_NOTE, TDF_SLIM, ref);
4355 fprintf (dump_file, "\n"); 3638 fprintf (dump_file, "\n");
4356 } 3639 }
4357 warned = warning_at (location, OPT_Warray_bounds, 3640
4358 "array subscript %E is above array bounds of %qT", 3641 ref = decl ? decl : TREE_OPERAND (ref, 0);
4359 up_sub, artype); 3642
4360 } 3643 tree rec = NULL_TREE;
4361 else if (TREE_CODE (low_sub) == INTEGER_CST 3644 if (TREE_CODE (ref) == COMPONENT_REF)
4362 && tree_int_cst_lt (low_sub, low_bound)) 3645 {
4363 { 3646 /* For a reference to a member of a struct object also mention
4364 if (dump_file && (dump_flags & TDF_DETAILS)) 3647 the object if it's known. It may be defined in a different
4365 { 3648 function than the out-of-bounds access. */
4366 fprintf (dump_file, "Array bound warning for "); 3649 rec = TREE_OPERAND (ref, 0);
4367 dump_generic_expr (MSG_NOTE, TDF_SLIM, ref); 3650 if (!VAR_P (rec))
4368 fprintf (dump_file, "\n"); 3651 rec = NULL_TREE;
4369 } 3652 ref = TREE_OPERAND (ref, 1);
4370 warned = warning_at (location, OPT_Warray_bounds, 3653 }
4371 "array subscript %E is below array bounds of %qT",
4372 low_sub, artype);
4373 }
4374
4375 if (warned)
4376 {
4377 ref = TREE_OPERAND (ref, 0);
4378 3654
4379 if (DECL_P (ref)) 3655 if (DECL_P (ref))
4380 inform (DECL_SOURCE_LOCATION (ref), "while referencing %qD", ref); 3656 inform (DECL_SOURCE_LOCATION (ref), "while referencing %qD", ref);
3657 if (rec && DECL_P (rec))
3658 inform (DECL_SOURCE_LOCATION (rec), "defined here %qD", rec);
4381 3659
4382 TREE_NO_WARNING (ref) = 1; 3660 TREE_NO_WARNING (ref) = 1;
4383 } 3661 }
3662
3663 return warned;
4384 } 3664 }
4385 3665
4386 /* Checks one MEM_REF in REF, located at LOCATION, for out-of-bounds 3666 /* Checks one MEM_REF in REF, located at LOCATION, for out-of-bounds
4387 references to string constants. If VRP can determine that the array 3667 references to string constants. If VRP can determine that the array
4388 subscript is a constant, check if it is outside valid range. 3668 subscript is a constant, check if it is outside valid range.
4389 If the array subscript is a RANGE, warn if it is non-overlapping 3669 If the array subscript is a RANGE, warn if it is non-overlapping
4390 with valid range. 3670 with valid range.
4391 IGNORE_OFF_BY_ONE is true if the MEM_REF is inside an ADDR_EXPR 3671 IGNORE_OFF_BY_ONE is true if the MEM_REF is inside an ADDR_EXPR
4392 (used to allow one-past-the-end indices for code that takes 3672 (used to allow one-past-the-end indices for code that takes
4393 the address of the just-past-the-end element of an array). */ 3673 the address of the just-past-the-end element of an array).
4394 3674 Returns true if a warning has been issued. */
4395 void 3675
3676 bool
4396 vrp_prop::check_mem_ref (location_t location, tree ref, 3677 vrp_prop::check_mem_ref (location_t location, tree ref,
4397 bool ignore_off_by_one) 3678 bool ignore_off_by_one)
4398 { 3679 {
4399 if (TREE_NO_WARNING (ref)) 3680 if (TREE_NO_WARNING (ref))
4400 return; 3681 return false;
4401 3682
4402 tree arg = TREE_OPERAND (ref, 0); 3683 tree arg = TREE_OPERAND (ref, 0);
4403 /* The constant and variable offset of the reference. */ 3684 /* The constant and variable offset of the reference. */
4404 tree cstoff = TREE_OPERAND (ref, 1); 3685 tree cstoff = TREE_OPERAND (ref, 1);
4405 tree varoff = NULL_TREE; 3686 tree varoff = NULL_TREE;
4422 offset_int extrema[2] = { 0, wi::abs (ioff) }; 3703 offset_int extrema[2] = { 0, wi::abs (ioff) };
4423 3704
4424 /* The range of the byte offset into the reference. */ 3705 /* The range of the byte offset into the reference. */
4425 offset_int offrange[2] = { 0, 0 }; 3706 offset_int offrange[2] = { 0, 0 };
4426 3707
4427 const value_range *vr = NULL; 3708 const value_range_equiv *vr = NULL;
4428 3709
4429 /* Determine the offsets and increment OFFRANGE for the bounds of each. 3710 /* Determine the offsets and increment OFFRANGE for the bounds of each.
4430 The loop computes the the range of the final offset for expressions 3711 The loop computes the range of the final offset for expressions such
4431 such as (A + i0 + ... + iN)[CSTOFF] where i0 through iN are SSA_NAMEs 3712 as (A + i0 + ... + iN)[CSTOFF] where i0 through iN are SSA_NAMEs in
4432 in some range. */ 3713 some range. */
4433 while (TREE_CODE (arg) == SSA_NAME) 3714 const unsigned limit = param_ssa_name_def_chain_limit;
3715 for (unsigned n = 0; TREE_CODE (arg) == SSA_NAME && n < limit; ++n)
4434 { 3716 {
4435 gimple *def = SSA_NAME_DEF_STMT (arg); 3717 gimple *def = SSA_NAME_DEF_STMT (arg);
4436 if (!is_gimple_assign (def)) 3718 if (!is_gimple_assign (def))
4437 break; 3719 break;
4438 3720
4446 { 3728 {
4447 arg = TREE_OPERAND (gimple_assign_rhs1 (def), 0); 3729 arg = TREE_OPERAND (gimple_assign_rhs1 (def), 0);
4448 continue; 3730 continue;
4449 } 3731 }
4450 else 3732 else
4451 return; 3733 return false;
4452 3734
4453 /* VAROFF should always be a SSA_NAME here (and not even 3735 /* VAROFF should always be a SSA_NAME here (and not even
4454 INTEGER_CST) but there's no point in taking chances. */ 3736 INTEGER_CST) but there's no point in taking chances. */
4455 if (TREE_CODE (varoff) != SSA_NAME) 3737 if (TREE_CODE (varoff) != SSA_NAME)
4456 break; 3738 break;
4462 if (!vr->constant_p ()) 3744 if (!vr->constant_p ())
4463 break; 3745 break;
4464 3746
4465 if (vr->kind () == VR_RANGE) 3747 if (vr->kind () == VR_RANGE)
4466 { 3748 {
4467 if (tree_int_cst_lt (vr->min (), vr->max ())) 3749 offset_int min
3750 = wi::to_offset (fold_convert (ptrdiff_type_node, vr->min ()));
3751 offset_int max
3752 = wi::to_offset (fold_convert (ptrdiff_type_node, vr->max ()));
3753 if (min < max)
4468 { 3754 {
4469 offset_int min 3755 offrange[0] += min;
4470 = wi::to_offset (fold_convert (ptrdiff_type_node, vr->min ())); 3756 offrange[1] += max;
4471 offset_int max
4472 = wi::to_offset (fold_convert (ptrdiff_type_node, vr->max ()));
4473 if (min < max)
4474 {
4475 offrange[0] += min;
4476 offrange[1] += max;
4477 }
4478 else
4479 {
4480 offrange[0] += max;
4481 offrange[1] += min;
4482 }
4483 } 3757 }
4484 else 3758 else
4485 { 3759 {
4486 /* Conservatively add [-MAXOBJSIZE -1, MAXOBJSIZE] 3760 /* When MIN >= MAX, the offset is effectively in a union
3761 of two ranges: [-MAXOBJSIZE -1, MAX] and [MIN, MAXOBJSIZE].
3762 Since there is no way to represent such a range across
3763 additions, conservatively add [-MAXOBJSIZE -1, MAXOBJSIZE]
4487 to OFFRANGE. */ 3764 to OFFRANGE. */
4488 offrange[0] += arrbounds[0]; 3765 offrange[0] += arrbounds[0];
4489 offrange[1] += arrbounds[1]; 3766 offrange[1] += arrbounds[1];
4490 } 3767 }
4491 } 3768 }
4512 3789
4513 if (TREE_CODE (arg) == ADDR_EXPR) 3790 if (TREE_CODE (arg) == ADDR_EXPR)
4514 { 3791 {
4515 arg = TREE_OPERAND (arg, 0); 3792 arg = TREE_OPERAND (arg, 0);
4516 if (TREE_CODE (arg) != STRING_CST 3793 if (TREE_CODE (arg) != STRING_CST
3794 && TREE_CODE (arg) != PARM_DECL
4517 && TREE_CODE (arg) != VAR_DECL) 3795 && TREE_CODE (arg) != VAR_DECL)
4518 return; 3796 return false;
4519 } 3797 }
4520 else 3798 else
4521 return; 3799 return false;
4522 3800
4523 /* The type of the object being referred to. It can be an array, 3801 /* The type of the object being referred to. It can be an array,
4524 string literal, or a non-array type when the MEM_REF represents 3802 string literal, or a non-array type when the MEM_REF represents
4525 a reference/subscript via a pointer to an object that is not 3803 a reference/subscript via a pointer to an object that is not
4526 an element of an array. References to members of structs and 3804 an element of an array. Incomplete types are excluded as well
4527 unions are excluded because MEM_REF doesn't make it possible 3805 because their size is not known. */
4528 to identify the member where the reference originated.
4529 Incomplete types are excluded as well because their size is
4530 not known. */
4531 tree reftype = TREE_TYPE (arg); 3806 tree reftype = TREE_TYPE (arg);
4532 if (POINTER_TYPE_P (reftype) 3807 if (POINTER_TYPE_P (reftype)
4533 || !COMPLETE_TYPE_P (reftype) 3808 || !COMPLETE_TYPE_P (reftype)
4534 || TREE_CODE (TYPE_SIZE_UNIT (reftype)) != INTEGER_CST 3809 || TREE_CODE (TYPE_SIZE_UNIT (reftype)) != INTEGER_CST)
4535 || RECORD_OR_UNION_TYPE_P (reftype)) 3810 return false;
4536 return; 3811
3812 /* Except in declared objects, references to trailing array members
3813 of structs and union objects are excluded because MEM_REF doesn't
3814 make it possible to identify the member where the reference
3815 originated. */
3816 if (RECORD_OR_UNION_TYPE_P (reftype)
3817 && (!VAR_P (arg)
3818 || (DECL_EXTERNAL (arg) && array_at_struct_end_p (ref))))
3819 return false;
3820
3821 arrbounds[0] = 0;
4537 3822
4538 offset_int eltsize; 3823 offset_int eltsize;
4539 if (TREE_CODE (reftype) == ARRAY_TYPE) 3824 if (TREE_CODE (reftype) == ARRAY_TYPE)
4540 { 3825 {
4541 eltsize = wi::to_offset (TYPE_SIZE_UNIT (TREE_TYPE (reftype))); 3826 eltsize = wi::to_offset (TYPE_SIZE_UNIT (TREE_TYPE (reftype)));
4542
4543 if (tree dom = TYPE_DOMAIN (reftype)) 3827 if (tree dom = TYPE_DOMAIN (reftype))
4544 { 3828 {
4545 tree bnds[] = { TYPE_MIN_VALUE (dom), TYPE_MAX_VALUE (dom) }; 3829 tree bnds[] = { TYPE_MIN_VALUE (dom), TYPE_MAX_VALUE (dom) };
4546 if (array_at_struct_end_p (arg) 3830 if (TREE_CODE (arg) == COMPONENT_REF)
4547 || !bnds[0] || !bnds[1])
4548 { 3831 {
4549 arrbounds[0] = 0; 3832 offset_int size = maxobjsize;
4550 arrbounds[1] = wi::lrshift (maxobjsize, wi::floor_log2 (eltsize)); 3833 if (tree fldsize = component_ref_size (arg))
3834 size = wi::to_offset (fldsize);
3835 arrbounds[1] = wi::lrshift (size, wi::floor_log2 (eltsize));
4551 } 3836 }
3837 else if (array_at_struct_end_p (arg) || !bnds[0] || !bnds[1])
3838 arrbounds[1] = wi::lrshift (maxobjsize, wi::floor_log2 (eltsize));
4552 else 3839 else
4553 { 3840 arrbounds[1] = (wi::to_offset (bnds[1]) - wi::to_offset (bnds[0])
4554 arrbounds[0] = wi::to_offset (bnds[0]) * eltsize; 3841 + 1) * eltsize;
4555 arrbounds[1] = (wi::to_offset (bnds[1]) + 1) * eltsize;
4556 }
4557 } 3842 }
4558 else 3843 else
4559 { 3844 arrbounds[1] = wi::lrshift (maxobjsize, wi::floor_log2 (eltsize));
4560 arrbounds[0] = 0;
4561 arrbounds[1] = wi::lrshift (maxobjsize, wi::floor_log2 (eltsize));
4562 }
4563 3845
4564 if (TREE_CODE (ref) == MEM_REF) 3846 if (TREE_CODE (ref) == MEM_REF)
4565 { 3847 {
4566 /* For MEM_REF determine a tighter bound of the non-array 3848 /* For MEM_REF determine a tighter bound of the non-array
4567 element type. */ 3849 element type. */
4572 } 3854 }
4573 } 3855 }
4574 else 3856 else
4575 { 3857 {
4576 eltsize = 1; 3858 eltsize = 1;
4577 arrbounds[0] = 0; 3859 tree size = TYPE_SIZE_UNIT (reftype);
4578 arrbounds[1] = wi::to_offset (TYPE_SIZE_UNIT (reftype)); 3860 if (VAR_P (arg))
3861 if (tree initsize = DECL_SIZE_UNIT (arg))
3862 if (tree_int_cst_lt (size, initsize))
3863 size = initsize;
3864
3865 arrbounds[1] = wi::to_offset (size);
4579 } 3866 }
4580 3867
4581 offrange[0] += ioff; 3868 offrange[0] += ioff;
4582 offrange[1] += ioff; 3869 offrange[1] += ioff;
4583 3870
4586 of an array) but always use the stricter bound in diagnostics. */ 3873 of an array) but always use the stricter bound in diagnostics. */
4587 offset_int ubound = arrbounds[1]; 3874 offset_int ubound = arrbounds[1];
4588 if (ignore_off_by_one) 3875 if (ignore_off_by_one)
4589 ubound += 1; 3876 ubound += 1;
4590 3877
4591 if (offrange[0] >= ubound || offrange[1] < arrbounds[0]) 3878 if (arrbounds[0] == arrbounds[1]
3879 || offrange[0] >= ubound
3880 || offrange[1] < arrbounds[0])
4592 { 3881 {
4593 /* Treat a reference to a non-array object as one to an array 3882 /* Treat a reference to a non-array object as one to an array
4594 of a single element. */ 3883 of a single element. */
4595 if (TREE_CODE (reftype) != ARRAY_TYPE) 3884 if (TREE_CODE (reftype) != ARRAY_TYPE)
4596 reftype = build_array_type_nelts (reftype, 1); 3885 reftype = build_array_type_nelts (reftype, 1);
4597 3886
4598 if (TREE_CODE (ref) == MEM_REF) 3887 if (TREE_CODE (ref) == MEM_REF)
4599 { 3888 {
4600 /* Extract the element type out of MEM_REF and use its size 3889 /* Extract the element type out of MEM_REF and use its size
4601 to compute the index to print in the diagnostic; arrays 3890 to compute the index to print in the diagnostic; arrays
4602 in MEM_REF don't mean anything. */ 3891 in MEM_REF don't mean anything. A type with no size like
3892 void is as good as having a size of 1. */
4603 tree type = TREE_TYPE (ref); 3893 tree type = TREE_TYPE (ref);
4604 while (TREE_CODE (type) == ARRAY_TYPE) 3894 while (TREE_CODE (type) == ARRAY_TYPE)
4605 type = TREE_TYPE (type); 3895 type = TREE_TYPE (type);
4606 tree size = TYPE_SIZE_UNIT (type); 3896 if (tree size = TYPE_SIZE_UNIT (type))
4607 offrange[0] = offrange[0] / wi::to_offset (size); 3897 {
4608 offrange[1] = offrange[1] / wi::to_offset (size); 3898 offrange[0] = offrange[0] / wi::to_offset (size);
3899 offrange[1] = offrange[1] / wi::to_offset (size);
3900 }
4609 } 3901 }
4610 else 3902 else
4611 { 3903 {
4612 /* For anything other than MEM_REF, compute the index to 3904 /* For anything other than MEM_REF, compute the index to
4613 print in the diagnostic as the offset over element size. */ 3905 print in the diagnostic as the offset over element size. */
4628 offrange[0].to_shwi (), 3920 offrange[0].to_shwi (),
4629 offrange[1].to_shwi (), reftype); 3921 offrange[1].to_shwi (), reftype);
4630 if (warned && DECL_P (arg)) 3922 if (warned && DECL_P (arg))
4631 inform (DECL_SOURCE_LOCATION (arg), "while referencing %qD", arg); 3923 inform (DECL_SOURCE_LOCATION (arg), "while referencing %qD", arg);
4632 3924
4633 TREE_NO_WARNING (ref) = 1; 3925 if (warned)
4634 return; 3926 TREE_NO_WARNING (ref) = 1;
3927 return warned;
4635 } 3928 }
4636 3929
4637 if (warn_array_bounds < 2) 3930 if (warn_array_bounds < 2)
4638 return; 3931 return false;
4639 3932
4640 /* At level 2 check also intermediate offsets. */ 3933 /* At level 2 check also intermediate offsets. */
4641 int i = 0; 3934 int i = 0;
4642 if (extrema[i] < -arrbounds[1] || extrema[i = 1] > ubound) 3935 if (extrema[i] < -arrbounds[1] || extrema[i = 1] > ubound)
4643 { 3936 {
4644 HOST_WIDE_INT tmpidx = extrema[i].to_shwi () / eltsize.to_shwi (); 3937 HOST_WIDE_INT tmpidx = extrema[i].to_shwi () / eltsize.to_shwi ();
4645 3938
4646 warning_at (location, OPT_Warray_bounds, 3939 if (warning_at (location, OPT_Warray_bounds,
4647 "intermediate array offset %wi is outside array bounds " 3940 "intermediate array offset %wi is outside array bounds "
4648 "of %qT", 3941 "of %qT", tmpidx, reftype))
4649 tmpidx, reftype); 3942 {
4650 TREE_NO_WARNING (ref) = 1; 3943 TREE_NO_WARNING (ref) = 1;
4651 } 3944 return true;
3945 }
3946 }
3947
3948 return false;
4652 } 3949 }
4653 3950
4654 /* Searches if the expr T, located at LOCATION computes 3951 /* Searches if the expr T, located at LOCATION computes
4655 address of an ARRAY_REF, and call check_array_ref on it. */ 3952 address of an ARRAY_REF, and call check_array_ref on it. */
4656 3953
4658 vrp_prop::search_for_addr_array (tree t, location_t location) 3955 vrp_prop::search_for_addr_array (tree t, location_t location)
4659 { 3956 {
4660 /* Check each ARRAY_REF and MEM_REF in the reference chain. */ 3957 /* Check each ARRAY_REF and MEM_REF in the reference chain. */
4661 do 3958 do
4662 { 3959 {
3960 bool warned = false;
4663 if (TREE_CODE (t) == ARRAY_REF) 3961 if (TREE_CODE (t) == ARRAY_REF)
4664 check_array_ref (location, t, true /*ignore_off_by_one*/); 3962 warned = check_array_ref (location, t, true /*ignore_off_by_one*/);
4665 else if (TREE_CODE (t) == MEM_REF) 3963 else if (TREE_CODE (t) == MEM_REF)
4666 check_mem_ref (location, t, true /*ignore_off_by_one*/); 3964 warned = check_mem_ref (location, t, true /*ignore_off_by_one*/);
3965
3966 if (warned)
3967 TREE_NO_WARNING (t) = true;
4667 3968
4668 t = TREE_OPERAND (t, 0); 3969 t = TREE_OPERAND (t, 0);
4669 } 3970 }
4670 while (handled_component_p (t) || TREE_CODE (t) == MEM_REF); 3971 while (handled_component_p (t) || TREE_CODE (t) == MEM_REF);
4671 3972
4753 else 4054 else
4754 location = gimple_location (wi->stmt); 4055 location = gimple_location (wi->stmt);
4755 4056
4756 *walk_subtree = TRUE; 4057 *walk_subtree = TRUE;
4757 4058
4059 bool warned = false;
4758 vrp_prop *vrp_prop = (class vrp_prop *)wi->info; 4060 vrp_prop *vrp_prop = (class vrp_prop *)wi->info;
4759 if (TREE_CODE (t) == ARRAY_REF) 4061 if (TREE_CODE (t) == ARRAY_REF)
4760 vrp_prop->check_array_ref (location, t, false /*ignore_off_by_one*/); 4062 warned = vrp_prop->check_array_ref (location, t, false/*ignore_off_by_one*/);
4761 else if (TREE_CODE (t) == MEM_REF) 4063 else if (TREE_CODE (t) == MEM_REF)
4762 vrp_prop->check_mem_ref (location, t, false /*ignore_off_by_one*/); 4064 warned = vrp_prop->check_mem_ref (location, t, false /*ignore_off_by_one*/);
4763 else if (TREE_CODE (t) == ADDR_EXPR) 4065 else if (TREE_CODE (t) == ADDR_EXPR)
4764 { 4066 {
4765 vrp_prop->search_for_addr_array (t, location); 4067 vrp_prop->search_for_addr_array (t, location);
4766 *walk_subtree = FALSE; 4068 *walk_subtree = FALSE;
4767 } 4069 }
4070 /* Propagate the no-warning bit to the outer expression. */
4071 if (warned)
4072 TREE_NO_WARNING (t) = true;
4768 4073
4769 return NULL_TREE; 4074 return NULL_TREE;
4770 } 4075 }
4771 4076
4772 /* A dom_walker subclass for use by vrp_prop::check_all_array_refs, 4077 /* A dom_walker subclass for use by vrp_prop::check_all_array_refs,
5085 { 4390 {
5086 gphi *phi = si.phi (); 4391 gphi *phi = si.phi ();
5087 if (!stmt_interesting_for_vrp (phi)) 4392 if (!stmt_interesting_for_vrp (phi))
5088 { 4393 {
5089 tree lhs = PHI_RESULT (phi); 4394 tree lhs = PHI_RESULT (phi);
5090 set_value_range_to_varying (get_value_range (lhs)); 4395 set_def_to_varying (lhs);
5091 prop_set_simulate_again (phi, false); 4396 prop_set_simulate_again (phi, false);
5092 } 4397 }
5093 else 4398 else
5094 prop_set_simulate_again (phi, true); 4399 prop_set_simulate_again (phi, true);
5095 } 4400 }
5240 4545
5241 enum ssa_prop_result 4546 enum ssa_prop_result
5242 vrp_prop::visit_stmt (gimple *stmt, edge *taken_edge_p, tree *output_p) 4547 vrp_prop::visit_stmt (gimple *stmt, edge *taken_edge_p, tree *output_p)
5243 { 4548 {
5244 tree lhs = gimple_get_lhs (stmt); 4549 tree lhs = gimple_get_lhs (stmt);
5245 value_range vr; 4550 value_range_equiv vr;
5246 extract_range_from_stmt (stmt, taken_edge_p, output_p, &vr); 4551 extract_range_from_stmt (stmt, taken_edge_p, output_p, &vr);
5247 4552
5248 if (*output_p) 4553 if (*output_p)
5249 { 4554 {
5250 if (update_value_range (*output_p, &vr)) 4555 if (update_value_range (*output_p, &vr))
5280 { 4585 {
5281 imm_use_iterator iter; 4586 imm_use_iterator iter;
5282 use_operand_p use_p; 4587 use_operand_p use_p;
5283 enum ssa_prop_result res = SSA_PROP_VARYING; 4588 enum ssa_prop_result res = SSA_PROP_VARYING;
5284 4589
5285 set_value_range_to_varying (get_value_range (lhs)); 4590 set_def_to_varying (lhs);
5286 4591
5287 FOR_EACH_IMM_USE_FAST (use_p, iter, lhs) 4592 FOR_EACH_IMM_USE_FAST (use_p, iter, lhs)
5288 { 4593 {
5289 gimple *use_stmt = USE_STMT (use_p); 4594 gimple *use_stmt = USE_STMT (use_p);
5290 if (!is_gimple_assign (use_stmt)) 4595 if (!is_gimple_assign (use_stmt))
5309 or IMAGPART_EXPR immediate uses, but none of them have 4614 or IMAGPART_EXPR immediate uses, but none of them have
5310 a change in their value ranges, return 4615 a change in their value ranges, return
5311 SSA_PROP_NOT_INTERESTING. If there are no 4616 SSA_PROP_NOT_INTERESTING. If there are no
5312 {REAL,IMAG}PART_EXPR uses at all, 4617 {REAL,IMAG}PART_EXPR uses at all,
5313 return SSA_PROP_VARYING. */ 4618 return SSA_PROP_VARYING. */
5314 value_range new_vr; 4619 value_range_equiv new_vr;
5315 extract_range_basic (&new_vr, use_stmt); 4620 extract_range_basic (&new_vr, use_stmt);
5316 const value_range *old_vr = get_value_range (use_lhs); 4621 const value_range_equiv *old_vr = get_value_range (use_lhs);
5317 if (*old_vr != new_vr) 4622 if (!old_vr->equal_p (new_vr, /*ignore_equivs=*/false))
5318 res = SSA_PROP_INTERESTING; 4623 res = SSA_PROP_INTERESTING;
5319 else 4624 else
5320 res = SSA_PROP_NOT_INTERESTING; 4625 res = SSA_PROP_NOT_INTERESTING;
5321 new_vr.equiv_clear (); 4626 new_vr.equiv_clear ();
5322 if (res == SSA_PROP_INTERESTING) 4627 if (res == SSA_PROP_INTERESTING)
5338 set_defs_to_varying (stmt); 4643 set_defs_to_varying (stmt);
5339 4644
5340 return (*taken_edge_p) ? SSA_PROP_INTERESTING : SSA_PROP_VARYING; 4645 return (*taken_edge_p) ? SSA_PROP_INTERESTING : SSA_PROP_VARYING;
5341 } 4646 }
5342 4647
5343 /* Union the two value-ranges { *VR0TYPE, *VR0MIN, *VR0MAX } and
5344 { VR1TYPE, VR0MIN, VR0MAX } and store the result
5345 in { *VR0TYPE, *VR0MIN, *VR0MAX }. This may not be the smallest
5346 possible such range. The resulting range is not canonicalized. */
5347
5348 static void
5349 union_ranges (enum value_range_kind *vr0type,
5350 tree *vr0min, tree *vr0max,
5351 enum value_range_kind vr1type,
5352 tree vr1min, tree vr1max)
5353 {
5354 bool mineq = vrp_operand_equal_p (*vr0min, vr1min);
5355 bool maxeq = vrp_operand_equal_p (*vr0max, vr1max);
5356
5357 /* [] is vr0, () is vr1 in the following classification comments. */
5358 if (mineq && maxeq)
5359 {
5360 /* [( )] */
5361 if (*vr0type == vr1type)
5362 /* Nothing to do for equal ranges. */
5363 ;
5364 else if ((*vr0type == VR_RANGE
5365 && vr1type == VR_ANTI_RANGE)
5366 || (*vr0type == VR_ANTI_RANGE
5367 && vr1type == VR_RANGE))
5368 {
5369 /* For anti-range with range union the result is varying. */
5370 goto give_up;
5371 }
5372 else
5373 gcc_unreachable ();
5374 }
5375 else if (operand_less_p (*vr0max, vr1min) == 1
5376 || operand_less_p (vr1max, *vr0min) == 1)
5377 {
5378 /* [ ] ( ) or ( ) [ ]
5379 If the ranges have an empty intersection, result of the union
5380 operation is the anti-range or if both are anti-ranges
5381 it covers all. */
5382 if (*vr0type == VR_ANTI_RANGE
5383 && vr1type == VR_ANTI_RANGE)
5384 goto give_up;
5385 else if (*vr0type == VR_ANTI_RANGE
5386 && vr1type == VR_RANGE)
5387 ;
5388 else if (*vr0type == VR_RANGE
5389 && vr1type == VR_ANTI_RANGE)
5390 {
5391 *vr0type = vr1type;
5392 *vr0min = vr1min;
5393 *vr0max = vr1max;
5394 }
5395 else if (*vr0type == VR_RANGE
5396 && vr1type == VR_RANGE)
5397 {
5398 /* The result is the convex hull of both ranges. */
5399 if (operand_less_p (*vr0max, vr1min) == 1)
5400 {
5401 /* If the result can be an anti-range, create one. */
5402 if (TREE_CODE (*vr0max) == INTEGER_CST
5403 && TREE_CODE (vr1min) == INTEGER_CST
5404 && vrp_val_is_min (*vr0min)
5405 && vrp_val_is_max (vr1max))
5406 {
5407 tree min = int_const_binop (PLUS_EXPR,
5408 *vr0max,
5409 build_int_cst (TREE_TYPE (*vr0max), 1));
5410 tree max = int_const_binop (MINUS_EXPR,
5411 vr1min,
5412 build_int_cst (TREE_TYPE (vr1min), 1));
5413 if (!operand_less_p (max, min))
5414 {
5415 *vr0type = VR_ANTI_RANGE;
5416 *vr0min = min;
5417 *vr0max = max;
5418 }
5419 else
5420 *vr0max = vr1max;
5421 }
5422 else
5423 *vr0max = vr1max;
5424 }
5425 else
5426 {
5427 /* If the result can be an anti-range, create one. */
5428 if (TREE_CODE (vr1max) == INTEGER_CST
5429 && TREE_CODE (*vr0min) == INTEGER_CST
5430 && vrp_val_is_min (vr1min)
5431 && vrp_val_is_max (*vr0max))
5432 {
5433 tree min = int_const_binop (PLUS_EXPR,
5434 vr1max,
5435 build_int_cst (TREE_TYPE (vr1max), 1));
5436 tree max = int_const_binop (MINUS_EXPR,
5437 *vr0min,
5438 build_int_cst (TREE_TYPE (*vr0min), 1));
5439 if (!operand_less_p (max, min))
5440 {
5441 *vr0type = VR_ANTI_RANGE;
5442 *vr0min = min;
5443 *vr0max = max;
5444 }
5445 else
5446 *vr0min = vr1min;
5447 }
5448 else
5449 *vr0min = vr1min;
5450 }
5451 }
5452 else
5453 gcc_unreachable ();
5454 }
5455 else if ((maxeq || operand_less_p (vr1max, *vr0max) == 1)
5456 && (mineq || operand_less_p (*vr0min, vr1min) == 1))
5457 {
5458 /* [ ( ) ] or [( ) ] or [ ( )] */
5459 if (*vr0type == VR_RANGE
5460 && vr1type == VR_RANGE)
5461 ;
5462 else if (*vr0type == VR_ANTI_RANGE
5463 && vr1type == VR_ANTI_RANGE)
5464 {
5465 *vr0type = vr1type;
5466 *vr0min = vr1min;
5467 *vr0max = vr1max;
5468 }
5469 else if (*vr0type == VR_ANTI_RANGE
5470 && vr1type == VR_RANGE)
5471 {
5472 /* Arbitrarily choose the right or left gap. */
5473 if (!mineq && TREE_CODE (vr1min) == INTEGER_CST)
5474 *vr0max = int_const_binop (MINUS_EXPR, vr1min,
5475 build_int_cst (TREE_TYPE (vr1min), 1));
5476 else if (!maxeq && TREE_CODE (vr1max) == INTEGER_CST)
5477 *vr0min = int_const_binop (PLUS_EXPR, vr1max,
5478 build_int_cst (TREE_TYPE (vr1max), 1));
5479 else
5480 goto give_up;
5481 }
5482 else if (*vr0type == VR_RANGE
5483 && vr1type == VR_ANTI_RANGE)
5484 /* The result covers everything. */
5485 goto give_up;
5486 else
5487 gcc_unreachable ();
5488 }
5489 else if ((maxeq || operand_less_p (*vr0max, vr1max) == 1)
5490 && (mineq || operand_less_p (vr1min, *vr0min) == 1))
5491 {
5492 /* ( [ ] ) or ([ ] ) or ( [ ]) */
5493 if (*vr0type == VR_RANGE
5494 && vr1type == VR_RANGE)
5495 {
5496 *vr0type = vr1type;
5497 *vr0min = vr1min;
5498 *vr0max = vr1max;
5499 }
5500 else if (*vr0type == VR_ANTI_RANGE
5501 && vr1type == VR_ANTI_RANGE)
5502 ;
5503 else if (*vr0type == VR_RANGE
5504 && vr1type == VR_ANTI_RANGE)
5505 {
5506 *vr0type = VR_ANTI_RANGE;
5507 if (!mineq && TREE_CODE (*vr0min) == INTEGER_CST)
5508 {
5509 *vr0max = int_const_binop (MINUS_EXPR, *vr0min,
5510 build_int_cst (TREE_TYPE (*vr0min), 1));
5511 *vr0min = vr1min;
5512 }
5513 else if (!maxeq && TREE_CODE (*vr0max) == INTEGER_CST)
5514 {
5515 *vr0min = int_const_binop (PLUS_EXPR, *vr0max,
5516 build_int_cst (TREE_TYPE (*vr0max), 1));
5517 *vr0max = vr1max;
5518 }
5519 else
5520 goto give_up;
5521 }
5522 else if (*vr0type == VR_ANTI_RANGE
5523 && vr1type == VR_RANGE)
5524 /* The result covers everything. */
5525 goto give_up;
5526 else
5527 gcc_unreachable ();
5528 }
5529 else if ((operand_less_p (vr1min, *vr0max) == 1
5530 || operand_equal_p (vr1min, *vr0max, 0))
5531 && operand_less_p (*vr0min, vr1min) == 1
5532 && operand_less_p (*vr0max, vr1max) == 1)
5533 {
5534 /* [ ( ] ) or [ ]( ) */
5535 if (*vr0type == VR_RANGE
5536 && vr1type == VR_RANGE)
5537 *vr0max = vr1max;
5538 else if (*vr0type == VR_ANTI_RANGE
5539 && vr1type == VR_ANTI_RANGE)
5540 *vr0min = vr1min;
5541 else if (*vr0type == VR_ANTI_RANGE
5542 && vr1type == VR_RANGE)
5543 {
5544 if (TREE_CODE (vr1min) == INTEGER_CST)
5545 *vr0max = int_const_binop (MINUS_EXPR, vr1min,
5546 build_int_cst (TREE_TYPE (vr1min), 1));
5547 else
5548 goto give_up;
5549 }
5550 else if (*vr0type == VR_RANGE
5551 && vr1type == VR_ANTI_RANGE)
5552 {
5553 if (TREE_CODE (*vr0max) == INTEGER_CST)
5554 {
5555 *vr0type = vr1type;
5556 *vr0min = int_const_binop (PLUS_EXPR, *vr0max,
5557 build_int_cst (TREE_TYPE (*vr0max), 1));
5558 *vr0max = vr1max;
5559 }
5560 else
5561 goto give_up;
5562 }
5563 else
5564 gcc_unreachable ();
5565 }
5566 else if ((operand_less_p (*vr0min, vr1max) == 1
5567 || operand_equal_p (*vr0min, vr1max, 0))
5568 && operand_less_p (vr1min, *vr0min) == 1
5569 && operand_less_p (vr1max, *vr0max) == 1)
5570 {
5571 /* ( [ ) ] or ( )[ ] */
5572 if (*vr0type == VR_RANGE
5573 && vr1type == VR_RANGE)
5574 *vr0min = vr1min;
5575 else if (*vr0type == VR_ANTI_RANGE
5576 && vr1type == VR_ANTI_RANGE)
5577 *vr0max = vr1max;
5578 else if (*vr0type == VR_ANTI_RANGE
5579 && vr1type == VR_RANGE)
5580 {
5581 if (TREE_CODE (vr1max) == INTEGER_CST)
5582 *vr0min = int_const_binop (PLUS_EXPR, vr1max,
5583 build_int_cst (TREE_TYPE (vr1max), 1));
5584 else
5585 goto give_up;
5586 }
5587 else if (*vr0type == VR_RANGE
5588 && vr1type == VR_ANTI_RANGE)
5589 {
5590 if (TREE_CODE (*vr0min) == INTEGER_CST)
5591 {
5592 *vr0type = vr1type;
5593 *vr0max = int_const_binop (MINUS_EXPR, *vr0min,
5594 build_int_cst (TREE_TYPE (*vr0min), 1));
5595 *vr0min = vr1min;
5596 }
5597 else
5598 goto give_up;
5599 }
5600 else
5601 gcc_unreachable ();
5602 }
5603 else
5604 goto give_up;
5605
5606 return;
5607
5608 give_up:
5609 *vr0type = VR_VARYING;
5610 *vr0min = NULL_TREE;
5611 *vr0max = NULL_TREE;
5612 }
5613
5614 /* Intersect the two value-ranges { *VR0TYPE, *VR0MIN, *VR0MAX } and
5615 { VR1TYPE, VR0MIN, VR0MAX } and store the result
5616 in { *VR0TYPE, *VR0MIN, *VR0MAX }. This may not be the smallest
5617 possible such range. The resulting range is not canonicalized. */
5618
5619 static void
5620 intersect_ranges (enum value_range_kind *vr0type,
5621 tree *vr0min, tree *vr0max,
5622 enum value_range_kind vr1type,
5623 tree vr1min, tree vr1max)
5624 {
5625 bool mineq = vrp_operand_equal_p (*vr0min, vr1min);
5626 bool maxeq = vrp_operand_equal_p (*vr0max, vr1max);
5627
5628 /* [] is vr0, () is vr1 in the following classification comments. */
5629 if (mineq && maxeq)
5630 {
5631 /* [( )] */
5632 if (*vr0type == vr1type)
5633 /* Nothing to do for equal ranges. */
5634 ;
5635 else if ((*vr0type == VR_RANGE
5636 && vr1type == VR_ANTI_RANGE)
5637 || (*vr0type == VR_ANTI_RANGE
5638 && vr1type == VR_RANGE))
5639 {
5640 /* For anti-range with range intersection the result is empty. */
5641 *vr0type = VR_UNDEFINED;
5642 *vr0min = NULL_TREE;
5643 *vr0max = NULL_TREE;
5644 }
5645 else
5646 gcc_unreachable ();
5647 }
5648 else if (operand_less_p (*vr0max, vr1min) == 1
5649 || operand_less_p (vr1max, *vr0min) == 1)
5650 {
5651 /* [ ] ( ) or ( ) [ ]
5652 If the ranges have an empty intersection, the result of the
5653 intersect operation is the range for intersecting an
5654 anti-range with a range or empty when intersecting two ranges. */
5655 if (*vr0type == VR_RANGE
5656 && vr1type == VR_ANTI_RANGE)
5657 ;
5658 else if (*vr0type == VR_ANTI_RANGE
5659 && vr1type == VR_RANGE)
5660 {
5661 *vr0type = vr1type;
5662 *vr0min = vr1min;
5663 *vr0max = vr1max;
5664 }
5665 else if (*vr0type == VR_RANGE
5666 && vr1type == VR_RANGE)
5667 {
5668 *vr0type = VR_UNDEFINED;
5669 *vr0min = NULL_TREE;
5670 *vr0max = NULL_TREE;
5671 }
5672 else if (*vr0type == VR_ANTI_RANGE
5673 && vr1type == VR_ANTI_RANGE)
5674 {
5675 /* If the anti-ranges are adjacent to each other merge them. */
5676 if (TREE_CODE (*vr0max) == INTEGER_CST
5677 && TREE_CODE (vr1min) == INTEGER_CST
5678 && operand_less_p (*vr0max, vr1min) == 1
5679 && integer_onep (int_const_binop (MINUS_EXPR,
5680 vr1min, *vr0max)))
5681 *vr0max = vr1max;
5682 else if (TREE_CODE (vr1max) == INTEGER_CST
5683 && TREE_CODE (*vr0min) == INTEGER_CST
5684 && operand_less_p (vr1max, *vr0min) == 1
5685 && integer_onep (int_const_binop (MINUS_EXPR,
5686 *vr0min, vr1max)))
5687 *vr0min = vr1min;
5688 /* Else arbitrarily take VR0. */
5689 }
5690 }
5691 else if ((maxeq || operand_less_p (vr1max, *vr0max) == 1)
5692 && (mineq || operand_less_p (*vr0min, vr1min) == 1))
5693 {
5694 /* [ ( ) ] or [( ) ] or [ ( )] */
5695 if (*vr0type == VR_RANGE
5696 && vr1type == VR_RANGE)
5697 {
5698 /* If both are ranges the result is the inner one. */
5699 *vr0type = vr1type;
5700 *vr0min = vr1min;
5701 *vr0max = vr1max;
5702 }
5703 else if (*vr0type == VR_RANGE
5704 && vr1type == VR_ANTI_RANGE)
5705 {
5706 /* Choose the right gap if the left one is empty. */
5707 if (mineq)
5708 {
5709 if (TREE_CODE (vr1max) != INTEGER_CST)
5710 *vr0min = vr1max;
5711 else if (TYPE_PRECISION (TREE_TYPE (vr1max)) == 1
5712 && !TYPE_UNSIGNED (TREE_TYPE (vr1max)))
5713 *vr0min
5714 = int_const_binop (MINUS_EXPR, vr1max,
5715 build_int_cst (TREE_TYPE (vr1max), -1));
5716 else
5717 *vr0min
5718 = int_const_binop (PLUS_EXPR, vr1max,
5719 build_int_cst (TREE_TYPE (vr1max), 1));
5720 }
5721 /* Choose the left gap if the right one is empty. */
5722 else if (maxeq)
5723 {
5724 if (TREE_CODE (vr1min) != INTEGER_CST)
5725 *vr0max = vr1min;
5726 else if (TYPE_PRECISION (TREE_TYPE (vr1min)) == 1
5727 && !TYPE_UNSIGNED (TREE_TYPE (vr1min)))
5728 *vr0max
5729 = int_const_binop (PLUS_EXPR, vr1min,
5730 build_int_cst (TREE_TYPE (vr1min), -1));
5731 else
5732 *vr0max
5733 = int_const_binop (MINUS_EXPR, vr1min,
5734 build_int_cst (TREE_TYPE (vr1min), 1));
5735 }
5736 /* Choose the anti-range if the range is effectively varying. */
5737 else if (vrp_val_is_min (*vr0min)
5738 && vrp_val_is_max (*vr0max))
5739 {
5740 *vr0type = vr1type;
5741 *vr0min = vr1min;
5742 *vr0max = vr1max;
5743 }
5744 /* Else choose the range. */
5745 }
5746 else if (*vr0type == VR_ANTI_RANGE
5747 && vr1type == VR_ANTI_RANGE)
5748 /* If both are anti-ranges the result is the outer one. */
5749 ;
5750 else if (*vr0type == VR_ANTI_RANGE
5751 && vr1type == VR_RANGE)
5752 {
5753 /* The intersection is empty. */
5754 *vr0type = VR_UNDEFINED;
5755 *vr0min = NULL_TREE;
5756 *vr0max = NULL_TREE;
5757 }
5758 else
5759 gcc_unreachable ();
5760 }
5761 else if ((maxeq || operand_less_p (*vr0max, vr1max) == 1)
5762 && (mineq || operand_less_p (vr1min, *vr0min) == 1))
5763 {
5764 /* ( [ ] ) or ([ ] ) or ( [ ]) */
5765 if (*vr0type == VR_RANGE
5766 && vr1type == VR_RANGE)
5767 /* Choose the inner range. */
5768 ;
5769 else if (*vr0type == VR_ANTI_RANGE
5770 && vr1type == VR_RANGE)
5771 {
5772 /* Choose the right gap if the left is empty. */
5773 if (mineq)
5774 {
5775 *vr0type = VR_RANGE;
5776 if (TREE_CODE (*vr0max) != INTEGER_CST)
5777 *vr0min = *vr0max;
5778 else if (TYPE_PRECISION (TREE_TYPE (*vr0max)) == 1
5779 && !TYPE_UNSIGNED (TREE_TYPE (*vr0max)))
5780 *vr0min
5781 = int_const_binop (MINUS_EXPR, *vr0max,
5782 build_int_cst (TREE_TYPE (*vr0max), -1));
5783 else
5784 *vr0min
5785 = int_const_binop (PLUS_EXPR, *vr0max,
5786 build_int_cst (TREE_TYPE (*vr0max), 1));
5787 *vr0max = vr1max;
5788 }
5789 /* Choose the left gap if the right is empty. */
5790 else if (maxeq)
5791 {
5792 *vr0type = VR_RANGE;
5793 if (TREE_CODE (*vr0min) != INTEGER_CST)
5794 *vr0max = *vr0min;
5795 else if (TYPE_PRECISION (TREE_TYPE (*vr0min)) == 1
5796 && !TYPE_UNSIGNED (TREE_TYPE (*vr0min)))
5797 *vr0max
5798 = int_const_binop (PLUS_EXPR, *vr0min,
5799 build_int_cst (TREE_TYPE (*vr0min), -1));
5800 else
5801 *vr0max
5802 = int_const_binop (MINUS_EXPR, *vr0min,
5803 build_int_cst (TREE_TYPE (*vr0min), 1));
5804 *vr0min = vr1min;
5805 }
5806 /* Choose the anti-range if the range is effectively varying. */
5807 else if (vrp_val_is_min (vr1min)
5808 && vrp_val_is_max (vr1max))
5809 ;
5810 /* Choose the anti-range if it is ~[0,0], that range is special
5811 enough to special case when vr1's range is relatively wide.
5812 At least for types bigger than int - this covers pointers
5813 and arguments to functions like ctz. */
5814 else if (*vr0min == *vr0max
5815 && integer_zerop (*vr0min)
5816 && ((TYPE_PRECISION (TREE_TYPE (*vr0min))
5817 >= TYPE_PRECISION (integer_type_node))
5818 || POINTER_TYPE_P (TREE_TYPE (*vr0min)))
5819 && TREE_CODE (vr1max) == INTEGER_CST
5820 && TREE_CODE (vr1min) == INTEGER_CST
5821 && (wi::clz (wi::to_wide (vr1max) - wi::to_wide (vr1min))
5822 < TYPE_PRECISION (TREE_TYPE (*vr0min)) / 2))
5823 ;
5824 /* Else choose the range. */
5825 else
5826 {
5827 *vr0type = vr1type;
5828 *vr0min = vr1min;
5829 *vr0max = vr1max;
5830 }
5831 }
5832 else if (*vr0type == VR_ANTI_RANGE
5833 && vr1type == VR_ANTI_RANGE)
5834 {
5835 /* If both are anti-ranges the result is the outer one. */
5836 *vr0type = vr1type;
5837 *vr0min = vr1min;
5838 *vr0max = vr1max;
5839 }
5840 else if (vr1type == VR_ANTI_RANGE
5841 && *vr0type == VR_RANGE)
5842 {
5843 /* The intersection is empty. */
5844 *vr0type = VR_UNDEFINED;
5845 *vr0min = NULL_TREE;
5846 *vr0max = NULL_TREE;
5847 }
5848 else
5849 gcc_unreachable ();
5850 }
5851 else if ((operand_less_p (vr1min, *vr0max) == 1
5852 || operand_equal_p (vr1min, *vr0max, 0))
5853 && operand_less_p (*vr0min, vr1min) == 1)
5854 {
5855 /* [ ( ] ) or [ ]( ) */
5856 if (*vr0type == VR_ANTI_RANGE
5857 && vr1type == VR_ANTI_RANGE)
5858 *vr0max = vr1max;
5859 else if (*vr0type == VR_RANGE
5860 && vr1type == VR_RANGE)
5861 *vr0min = vr1min;
5862 else if (*vr0type == VR_RANGE
5863 && vr1type == VR_ANTI_RANGE)
5864 {
5865 if (TREE_CODE (vr1min) == INTEGER_CST)
5866 *vr0max = int_const_binop (MINUS_EXPR, vr1min,
5867 build_int_cst (TREE_TYPE (vr1min), 1));
5868 else
5869 *vr0max = vr1min;
5870 }
5871 else if (*vr0type == VR_ANTI_RANGE
5872 && vr1type == VR_RANGE)
5873 {
5874 *vr0type = VR_RANGE;
5875 if (TREE_CODE (*vr0max) == INTEGER_CST)
5876 *vr0min = int_const_binop (PLUS_EXPR, *vr0max,
5877 build_int_cst (TREE_TYPE (*vr0max), 1));
5878 else
5879 *vr0min = *vr0max;
5880 *vr0max = vr1max;
5881 }
5882 else
5883 gcc_unreachable ();
5884 }
5885 else if ((operand_less_p (*vr0min, vr1max) == 1
5886 || operand_equal_p (*vr0min, vr1max, 0))
5887 && operand_less_p (vr1min, *vr0min) == 1)
5888 {
5889 /* ( [ ) ] or ( )[ ] */
5890 if (*vr0type == VR_ANTI_RANGE
5891 && vr1type == VR_ANTI_RANGE)
5892 *vr0min = vr1min;
5893 else if (*vr0type == VR_RANGE
5894 && vr1type == VR_RANGE)
5895 *vr0max = vr1max;
5896 else if (*vr0type == VR_RANGE
5897 && vr1type == VR_ANTI_RANGE)
5898 {
5899 if (TREE_CODE (vr1max) == INTEGER_CST)
5900 *vr0min = int_const_binop (PLUS_EXPR, vr1max,
5901 build_int_cst (TREE_TYPE (vr1max), 1));
5902 else
5903 *vr0min = vr1max;
5904 }
5905 else if (*vr0type == VR_ANTI_RANGE
5906 && vr1type == VR_RANGE)
5907 {
5908 *vr0type = VR_RANGE;
5909 if (TREE_CODE (*vr0min) == INTEGER_CST)
5910 *vr0max = int_const_binop (MINUS_EXPR, *vr0min,
5911 build_int_cst (TREE_TYPE (*vr0min), 1));
5912 else
5913 *vr0max = *vr0min;
5914 *vr0min = vr1min;
5915 }
5916 else
5917 gcc_unreachable ();
5918 }
5919
5920 /* As a fallback simply use { *VRTYPE, *VR0MIN, *VR0MAX } as
5921 result for the intersection. That's always a conservative
5922 correct estimate unless VR1 is a constant singleton range
5923 in which case we choose that. */
5924 if (vr1type == VR_RANGE
5925 && is_gimple_min_invariant (vr1min)
5926 && vrp_operand_equal_p (vr1min, vr1max))
5927 {
5928 *vr0type = vr1type;
5929 *vr0min = vr1min;
5930 *vr0max = vr1max;
5931 }
5932 }
5933
5934
5935 /* Intersect the two value-ranges *VR0 and *VR1 and store the result
5936 in *VR0. This may not be the smallest possible such range. */
5937
5938 void 4648 void
5939 value_range::intersect_helper (value_range *vr0, const value_range *vr1) 4649 value_range_equiv::intersect (const value_range_equiv *other)
5940 {
5941 /* If either range is VR_VARYING the other one wins. */
5942 if (vr1->varying_p ())
5943 return;
5944 if (vr0->varying_p ())
5945 {
5946 vr0->deep_copy (vr1);
5947 return;
5948 }
5949
5950 /* When either range is VR_UNDEFINED the resulting range is
5951 VR_UNDEFINED, too. */
5952 if (vr0->undefined_p ())
5953 return;
5954 if (vr1->undefined_p ())
5955 {
5956 set_value_range_to_undefined (vr0);
5957 return;
5958 }
5959
5960 /* Save the original vr0 so we can return it as conservative intersection
5961 result when our worker turns things to varying. */
5962 value_range saved (*vr0);
5963
5964 value_range_kind vr0type = vr0->kind ();
5965 tree vr0min = vr0->min ();
5966 tree vr0max = vr0->max ();
5967 intersect_ranges (&vr0type, &vr0min, &vr0max,
5968 vr1->kind (), vr1->min (), vr1->max ());
5969 /* Make sure to canonicalize the result though as the inversion of a
5970 VR_RANGE can still be a VR_RANGE. */
5971 vr0->set_and_canonicalize (vr0type, vr0min, vr0max, vr0->m_equiv);
5972 /* If that failed, use the saved original VR0. */
5973 if (vr0->varying_p ())
5974 {
5975 *vr0 = saved;
5976 return;
5977 }
5978 /* If the result is VR_UNDEFINED there is no need to mess with
5979 the equivalencies. */
5980 if (vr0->undefined_p ())
5981 return;
5982
5983 /* The resulting set of equivalences for range intersection is the union of
5984 the two sets. */
5985 if (vr0->m_equiv && vr1->m_equiv && vr0->m_equiv != vr1->m_equiv)
5986 bitmap_ior_into (vr0->m_equiv, vr1->m_equiv);
5987 else if (vr1->m_equiv && !vr0->m_equiv)
5988 {
5989 /* All equivalence bitmaps are allocated from the same obstack. So
5990 we can use the obstack associated with VR to allocate vr0->equiv. */
5991 vr0->m_equiv = BITMAP_ALLOC (vr1->m_equiv->obstack);
5992 bitmap_copy (m_equiv, vr1->m_equiv);
5993 }
5994 }
5995
5996 void
5997 value_range::intersect (const value_range *other)
5998 { 4650 {
5999 if (dump_file && (dump_flags & TDF_DETAILS)) 4651 if (dump_file && (dump_flags & TDF_DETAILS))
6000 { 4652 {
6001 fprintf (dump_file, "Intersecting\n "); 4653 fprintf (dump_file, "Intersecting\n ");
6002 dump_value_range (dump_file, this); 4654 dump_value_range (dump_file, this);
6003 fprintf (dump_file, "\nand\n "); 4655 fprintf (dump_file, "\nand\n ");
6004 dump_value_range (dump_file, other); 4656 dump_value_range (dump_file, other);
6005 fprintf (dump_file, "\n"); 4657 fprintf (dump_file, "\n");
6006 } 4658 }
6007 intersect_helper (this, other); 4659
4660 /* If THIS is varying we want to pick up equivalences from OTHER.
4661 Just special-case this here rather than trying to fixup after the
4662 fact. */
4663 if (this->varying_p ())
4664 this->deep_copy (other);
4665 else
4666 {
4667 value_range tem = intersect_helper (this, other);
4668 this->update (tem.min (), tem.max (), tem.kind ());
4669
4670 /* If the result is VR_UNDEFINED there is no need to mess with
4671 equivalencies. */
4672 if (!undefined_p ())
4673 {
4674 /* The resulting set of equivalences for range intersection
4675 is the union of the two sets. */
4676 if (m_equiv && other->m_equiv && m_equiv != other->m_equiv)
4677 bitmap_ior_into (m_equiv, other->m_equiv);
4678 else if (other->m_equiv && !m_equiv)
4679 {
4680 /* All equivalence bitmaps are allocated from the same
4681 obstack. So we can use the obstack associated with
4682 VR to allocate this->m_equiv. */
4683 m_equiv = BITMAP_ALLOC (other->m_equiv->obstack);
4684 bitmap_copy (m_equiv, other->m_equiv);
4685 }
4686 }
4687 }
4688
6008 if (dump_file && (dump_flags & TDF_DETAILS)) 4689 if (dump_file && (dump_flags & TDF_DETAILS))
6009 { 4690 {
6010 fprintf (dump_file, "to\n "); 4691 fprintf (dump_file, "to\n ");
6011 dump_value_range (dump_file, this); 4692 dump_value_range (dump_file, this);
6012 fprintf (dump_file, "\n"); 4693 fprintf (dump_file, "\n");
6013 } 4694 }
6014 } 4695 }
6015 4696
6016 /* Meet operation for value ranges. Given two value ranges VR0 and
6017 VR1, store in VR0 a range that contains both VR0 and VR1. This
6018 may not be the smallest possible such range. */
6019
6020 void 4697 void
6021 value_range::union_helper (value_range *vr0, const value_range *vr1) 4698 value_range_equiv::union_ (const value_range_equiv *other)
6022 {
6023 if (vr1->undefined_p ())
6024 {
6025 /* VR0 already has the resulting range. */
6026 return;
6027 }
6028
6029 if (vr0->undefined_p ())
6030 {
6031 vr0->deep_copy (vr1);
6032 return;
6033 }
6034
6035 if (vr0->varying_p ())
6036 {
6037 /* Nothing to do. VR0 already has the resulting range. */
6038 return;
6039 }
6040
6041 if (vr1->varying_p ())
6042 {
6043 set_value_range_to_varying (vr0);
6044 return;
6045 }
6046
6047 value_range saved (*vr0);
6048 value_range_kind vr0type = vr0->kind ();
6049 tree vr0min = vr0->min ();
6050 tree vr0max = vr0->max ();
6051 union_ranges (&vr0type, &vr0min, &vr0max,
6052 vr1->kind (), vr1->min (), vr1->max ());
6053 *vr0 = value_range (vr0type, vr0min, vr0max);
6054 if (vr0->varying_p ())
6055 {
6056 /* Failed to find an efficient meet. Before giving up and setting
6057 the result to VARYING, see if we can at least derive a useful
6058 anti-range. */
6059 if (range_includes_zero_p (&saved) == 0
6060 && range_includes_zero_p (vr1) == 0)
6061 {
6062 set_value_range_to_nonnull (vr0, saved.type ());
6063
6064 /* Since this meet operation did not result from the meeting of
6065 two equivalent names, VR0 cannot have any equivalences. */
6066 if (vr0->m_equiv)
6067 bitmap_clear (vr0->m_equiv);
6068 return;
6069 }
6070
6071 set_value_range_to_varying (vr0);
6072 return;
6073 }
6074 vr0->set_and_canonicalize (vr0->kind (), vr0->min (), vr0->max (),
6075 vr0->equiv ());
6076 if (vr0->varying_p ())
6077 return;
6078
6079 /* The resulting set of equivalences is always the intersection of
6080 the two sets. */
6081 if (vr0->m_equiv && vr1->m_equiv && vr0->m_equiv != vr1->m_equiv)
6082 bitmap_and_into (vr0->m_equiv, vr1->m_equiv);
6083 else if (vr0->m_equiv && !vr1->m_equiv)
6084 bitmap_clear (vr0->m_equiv);
6085 }
6086
6087 void
6088 value_range::union_ (const value_range *other)
6089 { 4699 {
6090 if (dump_file && (dump_flags & TDF_DETAILS)) 4700 if (dump_file && (dump_flags & TDF_DETAILS))
6091 { 4701 {
6092 fprintf (dump_file, "Meeting\n "); 4702 fprintf (dump_file, "Meeting\n ");
6093 dump_value_range (dump_file, this); 4703 dump_value_range (dump_file, this);
6094 fprintf (dump_file, "\nand\n "); 4704 fprintf (dump_file, "\nand\n ");
6095 dump_value_range (dump_file, other); 4705 dump_value_range (dump_file, other);
6096 fprintf (dump_file, "\n"); 4706 fprintf (dump_file, "\n");
6097 } 4707 }
6098 union_helper (this, other); 4708
4709 /* If THIS is undefined we want to pick up equivalences from OTHER.
4710 Just special-case this here rather than trying to fixup after the fact. */
4711 if (this->undefined_p ())
4712 this->deep_copy (other);
4713 else
4714 {
4715 value_range tem = union_helper (this, other);
4716 this->update (tem.min (), tem.max (), tem.kind ());
4717
4718 /* The resulting set of equivalences is always the intersection of
4719 the two sets. */
4720 if (this->m_equiv && other->m_equiv && this->m_equiv != other->m_equiv)
4721 bitmap_and_into (this->m_equiv, other->m_equiv);
4722 else if (this->m_equiv && !other->m_equiv)
4723 bitmap_clear (this->m_equiv);
4724 }
4725
6099 if (dump_file && (dump_flags & TDF_DETAILS)) 4726 if (dump_file && (dump_flags & TDF_DETAILS))
6100 { 4727 {
6101 fprintf (dump_file, "to\n "); 4728 fprintf (dump_file, "to\n ");
6102 dump_value_range (dump_file, this); 4729 dump_value_range (dump_file, this);
6103 fprintf (dump_file, "\n"); 4730 fprintf (dump_file, "\n");
6110 4737
6111 enum ssa_prop_result 4738 enum ssa_prop_result
6112 vrp_prop::visit_phi (gphi *phi) 4739 vrp_prop::visit_phi (gphi *phi)
6113 { 4740 {
6114 tree lhs = PHI_RESULT (phi); 4741 tree lhs = PHI_RESULT (phi);
6115 value_range vr_result; 4742 value_range_equiv vr_result;
6116 extract_range_from_phi_node (phi, &vr_result); 4743 extract_range_from_phi_node (phi, &vr_result);
6117 if (update_value_range (lhs, &vr_result)) 4744 if (update_value_range (lhs, &vr_result))
6118 { 4745 {
6119 if (dump_file && (dump_flags & TDF_DETAILS)) 4746 if (dump_file && (dump_flags & TDF_DETAILS))
6120 { 4747 {
6136 } 4763 }
6137 4764
6138 class vrp_folder : public substitute_and_fold_engine 4765 class vrp_folder : public substitute_and_fold_engine
6139 { 4766 {
6140 public: 4767 public:
4768 vrp_folder () : substitute_and_fold_engine (/* Fold all stmts. */ true) { }
6141 tree get_value (tree) FINAL OVERRIDE; 4769 tree get_value (tree) FINAL OVERRIDE;
6142 bool fold_stmt (gimple_stmt_iterator *) FINAL OVERRIDE; 4770 bool fold_stmt (gimple_stmt_iterator *) FINAL OVERRIDE;
6143 bool fold_predicate_in (gimple_stmt_iterator *); 4771 bool fold_predicate_in (gimple_stmt_iterator *);
6144 4772
6145 class vr_values *vr_values; 4773 class vr_values *vr_values;
6306 if (TREE_CODE (op) != SSA_NAME) 4934 if (TREE_CODE (op) != SSA_NAME)
6307 return NULL_TREE; 4935 return NULL_TREE;
6308 4936
6309 op = lhs_of_dominating_assert (op, bb, stmt); 4937 op = lhs_of_dominating_assert (op, bb, stmt);
6310 4938
6311 const value_range *vr = vr_values->get_value_range (op); 4939 const value_range_equiv *vr = vr_values->get_value_range (op);
6312 if (vr->undefined_p () 4940 if (vr->undefined_p ()
6313 || vr->varying_p () 4941 || vr->varying_p ()
6314 || vr->symbolic_p ()) 4942 || vr->symbolic_p ())
6315 return NULL_TREE; 4943 return NULL_TREE;
6316 4944
6372 || POINTER_TYPE_P (TREE_TYPE (lhs))) 5000 || POINTER_TYPE_P (TREE_TYPE (lhs)))
6373 && stmt_interesting_for_vrp (stmt)) 5001 && stmt_interesting_for_vrp (stmt))
6374 { 5002 {
6375 edge dummy_e; 5003 edge dummy_e;
6376 tree dummy_tree; 5004 tree dummy_tree;
6377 value_range new_vr; 5005 value_range_equiv new_vr;
6378 vr_values->extract_range_from_stmt (stmt, &dummy_e, 5006 vr_values->extract_range_from_stmt (stmt, &dummy_e,
6379 &dummy_tree, &new_vr); 5007 &dummy_tree, &new_vr);
6380 tree singleton; 5008 tree singleton;
6381 if (new_vr.singleton_p (&singleton)) 5009 if (new_vr.singleton_p (&singleton))
6382 return singleton; 5010 return singleton;
6548 { 5176 {
6549 tree name = ssa_name (i); 5177 tree name = ssa_name (i);
6550 if (!name) 5178 if (!name)
6551 continue; 5179 continue;
6552 5180
6553 const value_range *vr = get_value_range (name); 5181 const value_range_equiv *vr = get_value_range (name);
6554 if (!name || !vr->constant_p ()) 5182 if (!name || !vr->constant_p ())
6555 continue; 5183 continue;
6556 5184
6557 if (POINTER_TYPE_P (TREE_TYPE (name)) 5185 if (POINTER_TYPE_P (TREE_TYPE (name))
6558 && range_includes_zero_p (vr) == 0) 5186 && range_includes_zero_p (vr) == 0)
6559 set_ptr_nonnull (name); 5187 set_ptr_nonnull (name);
6560 else if (!POINTER_TYPE_P (TREE_TYPE (name))) 5188 else if (!POINTER_TYPE_P (TREE_TYPE (name)))
6561 set_range_info (name, vr->kind (), 5189 set_range_info (name, *vr);
6562 wi::to_wide (vr->min ()),
6563 wi::to_wide (vr->max ()));
6564 } 5190 }
6565 5191
6566 /* If we're checking array refs, we want to merge information on 5192 /* If we're checking array refs, we want to merge information on
6567 the executability of each edge between vrp_folder and the 5193 the executability of each edge between vrp_folder and the
6568 check_array_bounds_dom_walker: each can clear the 5194 check_array_bounds_dom_walker: each can clear the
6753 if (BINARY_CLASS_P (expr)) 5379 if (BINARY_CLASS_P (expr))
6754 { 5380 {
6755 value_range vr0, vr1; 5381 value_range vr0, vr1;
6756 determine_value_range_1 (&vr0, TREE_OPERAND (expr, 0)); 5382 determine_value_range_1 (&vr0, TREE_OPERAND (expr, 0));
6757 determine_value_range_1 (&vr1, TREE_OPERAND (expr, 1)); 5383 determine_value_range_1 (&vr1, TREE_OPERAND (expr, 1));
6758 extract_range_from_binary_expr_1 (vr, TREE_CODE (expr), TREE_TYPE (expr), 5384 range_fold_binary_expr (vr, TREE_CODE (expr), TREE_TYPE (expr),
6759 &vr0, &vr1); 5385 &vr0, &vr1);
6760 } 5386 }
6761 else if (UNARY_CLASS_P (expr)) 5387 else if (UNARY_CLASS_P (expr))
6762 { 5388 {
6763 value_range vr0; 5389 value_range vr0;
6764 determine_value_range_1 (&vr0, TREE_OPERAND (expr, 0)); 5390 determine_value_range_1 (&vr0, TREE_OPERAND (expr, 0));
6765 extract_range_from_unary_expr (vr, TREE_CODE (expr), TREE_TYPE (expr), 5391 range_fold_unary_expr (vr, TREE_CODE (expr), TREE_TYPE (expr),
6766 &vr0, TREE_TYPE (TREE_OPERAND (expr, 0))); 5392 &vr0, TREE_TYPE (TREE_OPERAND (expr, 0)));
6767 } 5393 }
6768 else if (TREE_CODE (expr) == INTEGER_CST) 5394 else if (TREE_CODE (expr) == INTEGER_CST)
6769 set_value_range_to_value (vr, expr, NULL); 5395 vr->set (expr);
6770 else 5396 else
6771 { 5397 {
6772 value_range_kind kind; 5398 value_range_kind kind;
6773 wide_int min, max; 5399 wide_int min, max;
6774 /* For SSA names try to extract range info computed by VRP. Otherwise 5400 /* For SSA names try to extract range info computed by VRP. Otherwise
6775 fall back to varying. */ 5401 fall back to varying. */
6776 if (TREE_CODE (expr) == SSA_NAME 5402 if (TREE_CODE (expr) == SSA_NAME
6777 && INTEGRAL_TYPE_P (TREE_TYPE (expr)) 5403 && INTEGRAL_TYPE_P (TREE_TYPE (expr))
6778 && (kind = get_range_info (expr, &min, &max)) != VR_VARYING) 5404 && (kind = get_range_info (expr, &min, &max)) != VR_VARYING)
6779 set_value_range (vr, kind, wide_int_to_tree (TREE_TYPE (expr), min), 5405 vr->set (wide_int_to_tree (TREE_TYPE (expr), min),
6780 wide_int_to_tree (TREE_TYPE (expr), max), NULL); 5406 wide_int_to_tree (TREE_TYPE (expr), max),
5407 kind);
6781 else 5408 else
6782 set_value_range_to_varying (vr); 5409 vr->set_varying (TREE_TYPE (expr));
6783 } 5410 }
6784 } 5411 }
6785 5412
6786 /* Compute a value-range for EXPR and set it in *MIN and *MAX. Return 5413 /* Compute a value-range for EXPR and set it in *MIN and *MAX. Return
6787 the determined range type. */ 5414 the determined range type. */