]>
Commit | Line | Data |
---|---|---|
cca78449 | 1 | /* Support routines for value ranges. |
99dee823 | 2 | Copyright (C) 2019-2021 Free Software Foundation, Inc. |
4ba9fb0a AH |
3 | Major hacks by Aldy Hernandez <aldyh@redhat.com> and |
4 | Andrew MacLeod <amacleod@redhat.com>. | |
cca78449 AH |
5 | |
6 | This file is part of GCC. | |
7 | ||
8 | GCC is free software; you can redistribute it and/or modify | |
9 | it under the terms of the GNU General Public License as published by | |
10 | the Free Software Foundation; either version 3, or (at your option) | |
11 | any later version. | |
12 | ||
13 | GCC is distributed in the hope that it will be useful, | |
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | GNU General Public License for more details. | |
17 | ||
18 | You should have received a copy of the GNU General Public License | |
19 | along with GCC; see the file COPYING3. If not see | |
20 | <http://www.gnu.org/licenses/>. */ | |
21 | ||
22 | #include "config.h" | |
23 | #include "system.h" | |
24 | #include "coretypes.h" | |
25 | #include "backend.h" | |
26 | #include "tree.h" | |
27 | #include "gimple.h" | |
28 | #include "ssa.h" | |
29 | #include "tree-pretty-print.h" | |
30 | #include "fold-const.h" | |
ca8cc827 | 31 | #include "gimple-range.h" |
cca78449 | 32 | |
4ba9fb0a AH |
33 | // Here we copy between any two irange's. The ranges can be legacy or |
34 | // multi-ranges, and copying between any combination works correctly. | |
35 | ||
36 | irange & | |
37 | irange::operator= (const irange &src) | |
38 | { | |
5bcd7de6 | 39 | if (legacy_mode_p ()) |
4ba9fb0a | 40 | { |
5bcd7de6 | 41 | copy_to_legacy (src); |
4ba9fb0a AH |
42 | return *this; |
43 | } | |
5bcd7de6 | 44 | if (src.legacy_mode_p ()) |
4ba9fb0a | 45 | { |
5bcd7de6 | 46 | copy_legacy_to_multi_range (src); |
4ba9fb0a AH |
47 | return *this; |
48 | } | |
49 | ||
50 | unsigned x; | |
51 | unsigned lim = src.m_num_ranges; | |
52 | if (lim > m_max_ranges) | |
53 | lim = m_max_ranges; | |
54 | ||
55 | for (x = 0; x < lim * 2; ++x) | |
56 | m_base[x] = src.m_base[x]; | |
57 | ||
58 | // If the range didn't fit, the last range should cover the rest. | |
59 | if (lim != src.m_num_ranges) | |
60 | m_base[x - 1] = src.m_base[src.m_num_ranges * 2 - 1]; | |
61 | ||
62 | m_num_ranges = lim; | |
dc80d5e8 | 63 | m_kind = src.m_kind; |
4ba9fb0a AH |
64 | return *this; |
65 | } | |
66 | ||
67 | // Return TRUE if range is a multi-range that can be represented as a | |
68 | // VR_ANTI_RANGE. | |
69 | ||
70 | bool | |
71 | irange::maybe_anti_range () const | |
cca78449 | 72 | { |
4ba9fb0a AH |
73 | tree ttype = type (); |
74 | unsigned int precision = TYPE_PRECISION (ttype); | |
75 | signop sign = TYPE_SIGN (ttype); | |
76 | return (num_pairs () > 1 | |
77 | && precision > 1 | |
78 | && lower_bound () == wi::min_value (precision, sign) | |
79 | && upper_bound () == wi::max_value (precision, sign)); | |
cca78449 AH |
80 | } |
81 | ||
4ba9fb0a | 82 | void |
5bcd7de6 | 83 | irange::copy_legacy_to_multi_range (const irange &src) |
cca78449 | 84 | { |
5bcd7de6 AH |
85 | gcc_checking_assert (src.legacy_mode_p ()); |
86 | gcc_checking_assert (!legacy_mode_p ()); | |
4ba9fb0a AH |
87 | if (src.undefined_p ()) |
88 | set_undefined (); | |
89 | else if (src.varying_p ()) | |
90 | set_varying (src.type ()); | |
4ba9fb0a | 91 | else |
80cbca32 | 92 | { |
5bcd7de6 AH |
93 | if (range_has_numeric_bounds_p (&src)) |
94 | set (src.min (), src.max (), src.kind ()); | |
95 | else | |
80cbca32 AH |
96 | { |
97 | value_range cst (src); | |
98 | cst.normalize_symbolics (); | |
5bcd7de6 | 99 | gcc_checking_assert (cst.varying_p () || cst.kind () == VR_RANGE); |
80cbca32 | 100 | set (cst.min (), cst.max ()); |
80cbca32 | 101 | } |
80cbca32 | 102 | } |
cca78449 AH |
103 | } |
104 | ||
5bcd7de6 AH |
105 | // Copy any type of irange into a legacy. |
106 | ||
107 | void | |
108 | irange::copy_to_legacy (const irange &src) | |
109 | { | |
110 | gcc_checking_assert (legacy_mode_p ()); | |
dc80d5e8 AH |
111 | // Handle legacy to legacy and other things that are easy to copy. |
112 | if (src.legacy_mode_p () || src.varying_p () || src.undefined_p ()) | |
5bcd7de6 AH |
113 | { |
114 | m_num_ranges = src.m_num_ranges; | |
115 | m_base[0] = src.m_base[0]; | |
116 | m_base[1] = src.m_base[1]; | |
117 | m_kind = src.m_kind; | |
118 | return; | |
119 | } | |
120 | // Copy multi-range to legacy. | |
dc80d5e8 | 121 | if (src.maybe_anti_range ()) |
5bcd7de6 AH |
122 | { |
123 | int_range<3> r (src); | |
124 | r.invert (); | |
125 | // Use tree variants to save on tree -> wi -> tree conversions. | |
126 | set (r.tree_lower_bound (0), r.tree_upper_bound (0), VR_ANTI_RANGE); | |
127 | } | |
128 | else | |
129 | set (src.tree_lower_bound (), src.tree_upper_bound ()); | |
130 | } | |
131 | ||
4e85ad79 | 132 | // Swap MIN/MAX if they are out of order and adjust KIND appropriately. |
4ba9fb0a | 133 | |
4e85ad79 AH |
134 | static void |
135 | swap_out_of_order_endpoints (tree &min, tree &max, value_range_kind &kind) | |
cca78449 | 136 | { |
4e85ad79 AH |
137 | gcc_checking_assert (kind != VR_UNDEFINED); |
138 | if (kind == VR_VARYING) | |
139 | return; | |
4ba9fb0a AH |
140 | /* Wrong order for min and max, to swap them and the VR type we need |
141 | to adjust them. */ | |
142 | if (tree_int_cst_lt (max, min)) | |
143 | { | |
144 | tree one, tmp; | |
145 | ||
146 | /* For one bit precision if max < min, then the swapped | |
147 | range covers all values, so for VR_RANGE it is varying and | |
148 | for VR_ANTI_RANGE empty range, so drop to varying as well. */ | |
149 | if (TYPE_PRECISION (TREE_TYPE (min)) == 1) | |
150 | { | |
4e85ad79 AH |
151 | kind = VR_VARYING; |
152 | return; | |
4ba9fb0a AH |
153 | } |
154 | ||
155 | one = build_int_cst (TREE_TYPE (min), 1); | |
156 | tmp = int_const_binop (PLUS_EXPR, max, one); | |
157 | max = int_const_binop (MINUS_EXPR, min, one); | |
158 | min = tmp; | |
159 | ||
160 | /* There's one corner case, if we had [C+1, C] before we now have | |
161 | that again. But this represents an empty value range, so drop | |
162 | to varying in this case. */ | |
163 | if (tree_int_cst_lt (max, min)) | |
164 | { | |
4e85ad79 AH |
165 | kind = VR_VARYING; |
166 | return; | |
4ba9fb0a AH |
167 | } |
168 | kind = kind == VR_RANGE ? VR_ANTI_RANGE : VR_RANGE; | |
169 | } | |
cca78449 AH |
170 | } |
171 | ||
172 | void | |
4ba9fb0a | 173 | irange::irange_set (tree min, tree max) |
cca78449 | 174 | { |
4ba9fb0a AH |
175 | gcc_checking_assert (!POLY_INT_CST_P (min)); |
176 | gcc_checking_assert (!POLY_INT_CST_P (max)); | |
177 | ||
178 | m_base[0] = min; | |
179 | m_base[1] = max; | |
180 | m_num_ranges = 1; | |
dc80d5e8 AH |
181 | m_kind = VR_RANGE; |
182 | normalize_kind (); | |
183 | ||
4ba9fb0a AH |
184 | if (flag_checking) |
185 | verify_range (); | |
cca78449 AH |
186 | } |
187 | ||
88081d38 AH |
188 | void |
189 | irange::irange_set_1bit_anti_range (tree min, tree max) | |
190 | { | |
191 | tree type = TREE_TYPE (min); | |
192 | gcc_checking_assert (TYPE_PRECISION (type) == 1); | |
193 | ||
194 | if (operand_equal_p (min, max)) | |
195 | { | |
196 | // Since these are 1-bit quantities, they can only be [MIN,MIN] | |
197 | // or [MAX,MAX]. | |
198 | if (vrp_val_is_min (min)) | |
199 | min = max = vrp_val_max (type); | |
200 | else | |
201 | min = max = vrp_val_min (type); | |
202 | set (min, max); | |
203 | } | |
204 | else | |
205 | { | |
206 | // The only alternative is [MIN,MAX], which is the empty range. | |
e6455a09 AH |
207 | gcc_checking_assert (vrp_val_is_min (min)); |
208 | gcc_checking_assert (vrp_val_is_max (max)); | |
88081d38 AH |
209 | set_undefined (); |
210 | } | |
211 | if (flag_checking) | |
212 | verify_range (); | |
213 | } | |
214 | ||
cca78449 | 215 | void |
4ba9fb0a | 216 | irange::irange_set_anti_range (tree min, tree max) |
cca78449 | 217 | { |
4ba9fb0a AH |
218 | gcc_checking_assert (!POLY_INT_CST_P (min)); |
219 | gcc_checking_assert (!POLY_INT_CST_P (max)); | |
220 | ||
88081d38 AH |
221 | if (TYPE_PRECISION (TREE_TYPE (min)) == 1) |
222 | { | |
223 | irange_set_1bit_anti_range (min, max); | |
224 | return; | |
225 | } | |
226 | ||
4ba9fb0a AH |
227 | // set an anti-range |
228 | tree type = TREE_TYPE (min); | |
229 | signop sign = TYPE_SIGN (type); | |
230 | int_range<2> type_range (type); | |
231 | // Calculate INVERSE([I,J]) as [-MIN, I-1][J+1, +MAX]. | |
232 | m_num_ranges = 0; | |
233 | wi::overflow_type ovf; | |
234 | ||
235 | wide_int w_min = wi::to_wide (min); | |
236 | if (wi::ne_p (w_min, type_range.lower_bound ())) | |
cca78449 | 237 | { |
4ba9fb0a AH |
238 | wide_int lim1 = wi::sub (w_min, 1, sign, &ovf); |
239 | gcc_checking_assert (ovf != wi::OVF_OVERFLOW); | |
240 | m_base[0] = type_range.tree_lower_bound (0); | |
241 | m_base[1] = wide_int_to_tree (type, lim1); | |
242 | m_num_ranges = 1; | |
cca78449 | 243 | } |
4ba9fb0a AH |
244 | wide_int w_max = wi::to_wide (max); |
245 | if (wi::ne_p (w_max, type_range.upper_bound ())) | |
246 | { | |
247 | wide_int lim2 = wi::add (w_max, 1, sign, &ovf); | |
248 | gcc_checking_assert (ovf != wi::OVF_OVERFLOW); | |
249 | m_base[m_num_ranges * 2] = wide_int_to_tree (type, lim2); | |
250 | m_base[m_num_ranges * 2 + 1] = type_range.tree_upper_bound (0); | |
251 | ++m_num_ranges; | |
252 | } | |
dc80d5e8 AH |
253 | |
254 | m_kind = VR_RANGE; | |
255 | normalize_kind (); | |
256 | ||
4ba9fb0a AH |
257 | if (flag_checking) |
258 | verify_range (); | |
cca78449 AH |
259 | } |
260 | ||
261 | /* Set value range to the canonical form of {VRTYPE, MIN, MAX, EQUIV}. | |
262 | This means adjusting VRTYPE, MIN and MAX representing the case of a | |
263 | wrapping range with MAX < MIN covering [MIN, type_max] U [type_min, MAX] | |
264 | as anti-rage ~[MAX+1, MIN-1]. Likewise for wrapping anti-ranges. | |
265 | In corner cases where MAX+1 or MIN-1 wraps this will fall back | |
266 | to varying. | |
267 | This routine exists to ease canonicalization in the case where we | |
268 | extract ranges from var + CST op limit. */ | |
269 | ||
270 | void | |
4ba9fb0a | 271 | irange::set (tree min, tree max, value_range_kind kind) |
cca78449 | 272 | { |
4ba9fb0a AH |
273 | if (!legacy_mode_p ()) |
274 | { | |
275 | if (kind == VR_RANGE) | |
276 | irange_set (min, max); | |
277 | else | |
278 | { | |
279 | gcc_checking_assert (kind == VR_ANTI_RANGE); | |
280 | irange_set_anti_range (min, max); | |
281 | } | |
282 | return; | |
283 | } | |
cca78449 AH |
284 | if (kind == VR_UNDEFINED) |
285 | { | |
286 | set_undefined (); | |
287 | return; | |
288 | } | |
54ef7701 | 289 | |
c76c23a0 AH |
290 | if (kind == VR_VARYING |
291 | || POLY_INT_CST_P (min) | |
292 | || POLY_INT_CST_P (max)) | |
5e41e7f0 AH |
293 | { |
294 | set_varying (TREE_TYPE (min)); | |
295 | return; | |
296 | } | |
54ef7701 | 297 | |
4ba9fb0a | 298 | // Nothing to canonicalize for symbolic ranges. |
cca78449 AH |
299 | if (TREE_CODE (min) != INTEGER_CST |
300 | || TREE_CODE (max) != INTEGER_CST) | |
301 | { | |
302 | m_kind = kind; | |
4ba9fb0a AH |
303 | m_base[0] = min; |
304 | m_base[1] = max; | |
305 | m_num_ranges = 1; | |
cca78449 AH |
306 | return; |
307 | } | |
4e85ad79 AH |
308 | |
309 | swap_out_of_order_endpoints (min, max, kind); | |
310 | if (kind == VR_VARYING) | |
311 | { | |
312 | set_varying (TREE_TYPE (min)); | |
313 | return; | |
314 | } | |
cca78449 | 315 | |
4ba9fb0a | 316 | // Anti-ranges that can be represented as ranges should be so. |
cca78449 AH |
317 | if (kind == VR_ANTI_RANGE) |
318 | { | |
cca78449 AH |
319 | bool is_min = vrp_val_is_min (min); |
320 | bool is_max = vrp_val_is_max (max); | |
321 | ||
322 | if (is_min && is_max) | |
323 | { | |
e6455a09 AH |
324 | // Fall through. This will either be normalized as |
325 | // VR_UNDEFINED if the anti-range spans the entire | |
326 | // precision, or it will remain an VR_ANTI_RANGE in the case | |
327 | // of an -fstrict-enum where [MIN,MAX] is less than the span | |
328 | // of underlying precision. | |
cca78449 | 329 | } |
e6455a09 | 330 | else if (TYPE_PRECISION (TREE_TYPE (min)) == 1) |
cca78449 | 331 | { |
e6455a09 AH |
332 | irange_set_1bit_anti_range (min, max); |
333 | return; | |
cca78449 AH |
334 | } |
335 | else if (is_min) | |
336 | { | |
337 | tree one = build_int_cst (TREE_TYPE (max), 1); | |
338 | min = int_const_binop (PLUS_EXPR, max, one); | |
339 | max = vrp_val_max (TREE_TYPE (max)); | |
340 | kind = VR_RANGE; | |
341 | } | |
342 | else if (is_max) | |
343 | { | |
344 | tree one = build_int_cst (TREE_TYPE (min), 1); | |
345 | max = int_const_binop (MINUS_EXPR, min, one); | |
346 | min = vrp_val_min (TREE_TYPE (min)); | |
347 | kind = VR_RANGE; | |
348 | } | |
349 | } | |
cca78449 | 350 | |
4e85ad79 AH |
351 | m_kind = kind; |
352 | m_base[0] = min; | |
353 | m_base[1] = max; | |
354 | m_num_ranges = 1; | |
dc80d5e8 | 355 | normalize_kind (); |
cca78449 | 356 | if (flag_checking) |
4ba9fb0a | 357 | verify_range (); |
cca78449 AH |
358 | } |
359 | ||
4e85ad79 | 360 | // Check the validity of the range. |
cca78449 AH |
361 | |
362 | void | |
4ba9fb0a | 363 | irange::verify_range () |
cca78449 | 364 | { |
dc80d5e8 AH |
365 | if (m_kind == VR_UNDEFINED) |
366 | { | |
db3581c4 | 367 | gcc_checking_assert (m_num_ranges == 0); |
dc80d5e8 AH |
368 | return; |
369 | } | |
dc80d5e8 AH |
370 | if (m_kind == VR_VARYING) |
371 | { | |
372 | gcc_checking_assert (m_num_ranges == 1); | |
373 | gcc_checking_assert (varying_compatible_p ()); | |
374 | return; | |
375 | } | |
4ba9fb0a | 376 | if (!legacy_mode_p ()) |
cca78449 | 377 | { |
db3581c4 | 378 | gcc_checking_assert (m_num_ranges != 0); |
dc80d5e8 | 379 | gcc_checking_assert (!varying_compatible_p ()); |
4ba9fb0a AH |
380 | for (unsigned i = 0; i < m_num_ranges; ++i) |
381 | { | |
382 | tree lb = tree_lower_bound (i); | |
383 | tree ub = tree_upper_bound (i); | |
384 | int c = compare_values (lb, ub); | |
db3581c4 | 385 | gcc_checking_assert (c == 0 || c == -1); |
4ba9fb0a AH |
386 | } |
387 | return; | |
388 | } | |
dc80d5e8 | 389 | if (m_kind == VR_RANGE || m_kind == VR_ANTI_RANGE) |
4ba9fb0a | 390 | { |
db3581c4 | 391 | gcc_checking_assert (m_num_ranges == 1); |
dc80d5e8 | 392 | int cmp = compare_values (tree_lower_bound (0), tree_upper_bound (0)); |
db3581c4 | 393 | gcc_checking_assert (cmp == 0 || cmp == -1 || cmp == -2); |
cca78449 | 394 | } |
cca78449 AH |
395 | } |
396 | ||
4ba9fb0a AH |
397 | // Return the lower bound for a sub-range. PAIR is the sub-range in |
398 | // question. | |
cca78449 AH |
399 | |
400 | wide_int | |
4ba9fb0a | 401 | irange::legacy_lower_bound (unsigned pair) const |
cca78449 | 402 | { |
4ba9fb0a | 403 | gcc_checking_assert (legacy_mode_p ()); |
cca78449 | 404 | if (symbolic_p ()) |
6ee86466 AH |
405 | { |
406 | value_range numeric_range (*this); | |
407 | numeric_range.normalize_symbolics (); | |
4ba9fb0a | 408 | return numeric_range.legacy_lower_bound (pair); |
6ee86466 | 409 | } |
77803216 | 410 | gcc_checking_assert (m_num_ranges > 0); |
cca78449 | 411 | gcc_checking_assert (pair + 1 <= num_pairs ()); |
cca78449 AH |
412 | if (m_kind == VR_ANTI_RANGE) |
413 | { | |
4ba9fb0a AH |
414 | tree typ = type (), t; |
415 | if (pair == 1 || vrp_val_is_min (min ())) | |
416 | t = wide_int_to_tree (typ, wi::to_wide (max ()) + 1); | |
cca78449 AH |
417 | else |
418 | t = vrp_val_min (typ); | |
4ba9fb0a | 419 | return wi::to_wide (t); |
cca78449 | 420 | } |
4ba9fb0a | 421 | return wi::to_wide (tree_lower_bound (pair)); |
cca78449 AH |
422 | } |
423 | ||
4ba9fb0a AH |
424 | // Return the upper bound for a sub-range. PAIR is the sub-range in |
425 | // question. | |
cca78449 AH |
426 | |
427 | wide_int | |
4ba9fb0a | 428 | irange::legacy_upper_bound (unsigned pair) const |
cca78449 | 429 | { |
4ba9fb0a | 430 | gcc_checking_assert (legacy_mode_p ()); |
cca78449 | 431 | if (symbolic_p ()) |
6ee86466 AH |
432 | { |
433 | value_range numeric_range (*this); | |
434 | numeric_range.normalize_symbolics (); | |
4ba9fb0a | 435 | return numeric_range.legacy_upper_bound (pair); |
6ee86466 | 436 | } |
77803216 | 437 | gcc_checking_assert (m_num_ranges > 0); |
cca78449 | 438 | gcc_checking_assert (pair + 1 <= num_pairs ()); |
cca78449 AH |
439 | if (m_kind == VR_ANTI_RANGE) |
440 | { | |
4ba9fb0a AH |
441 | tree typ = type (), t; |
442 | if (pair == 1 || vrp_val_is_min (min ())) | |
cca78449 AH |
443 | t = vrp_val_max (typ); |
444 | else | |
4ba9fb0a AH |
445 | t = wide_int_to_tree (typ, wi::to_wide (min ()) - 1); |
446 | return wi::to_wide (t); | |
cca78449 | 447 | } |
4ba9fb0a | 448 | return wi::to_wide (tree_upper_bound (pair)); |
cca78449 AH |
449 | } |
450 | ||
451 | bool | |
4ba9fb0a | 452 | irange::legacy_equal_p (const irange &other) const |
cca78449 | 453 | { |
4ba9fb0a | 454 | gcc_checking_assert (legacy_mode_p () && other.legacy_mode_p ()); |
cca78449 | 455 | |
4ba9fb0a AH |
456 | if (m_kind != other.m_kind) |
457 | return false; | |
ca8cc827 | 458 | if (m_kind == VR_UNDEFINED) |
4ba9fb0a | 459 | return true; |
ca8cc827 AH |
460 | if (m_kind == VR_VARYING) |
461 | return range_compatible_p (type (), other.type ()); | |
4ba9fb0a AH |
462 | return (vrp_operand_equal_p (tree_lower_bound (0), |
463 | other.tree_lower_bound (0)) | |
464 | && vrp_operand_equal_p (tree_upper_bound (0), | |
465 | other.tree_upper_bound (0))); | |
cca78449 AH |
466 | } |
467 | ||
468 | bool | |
4ba9fb0a | 469 | irange::equal_p (const irange &other) const |
cca78449 | 470 | { |
4ba9fb0a AH |
471 | if (legacy_mode_p ()) |
472 | { | |
473 | if (other.legacy_mode_p ()) | |
474 | return legacy_equal_p (other); | |
475 | value_range tmp (other); | |
476 | return legacy_equal_p (tmp); | |
477 | } | |
478 | if (other.legacy_mode_p ()) | |
479 | { | |
480 | value_range tmp2 (*this); | |
481 | return tmp2.legacy_equal_p (other); | |
482 | } | |
483 | ||
484 | if (m_num_ranges != other.m_num_ranges) | |
485 | return false; | |
486 | ||
487 | for (unsigned i = 0; i < m_num_ranges; ++i) | |
488 | { | |
489 | tree lb = tree_lower_bound (i); | |
490 | tree ub = tree_upper_bound (i); | |
491 | tree lb_other = other.tree_lower_bound (i); | |
492 | tree ub_other = other.tree_upper_bound (i); | |
493 | if (!operand_equal_p (lb, lb_other, 0) | |
494 | || !operand_equal_p (ub, ub_other, 0)) | |
495 | return false; | |
496 | } | |
497 | return true; | |
cca78449 AH |
498 | } |
499 | ||
cca78449 AH |
500 | /* Return TRUE if this is a symbolic range. */ |
501 | ||
502 | bool | |
4ba9fb0a | 503 | irange::symbolic_p () const |
cca78449 | 504 | { |
694c956b | 505 | return (m_num_ranges > 0 |
4ba9fb0a AH |
506 | && (!is_gimple_min_invariant (min ()) |
507 | || !is_gimple_min_invariant (max ()))); | |
cca78449 AH |
508 | } |
509 | ||
694c956b | 510 | /* Return TRUE if this is a constant range. */ |
cca78449 AH |
511 | |
512 | bool | |
4ba9fb0a | 513 | irange::constant_p () const |
cca78449 | 514 | { |
694c956b | 515 | return (m_num_ranges > 0 |
4ba9fb0a AH |
516 | && TREE_CODE (min ()) == INTEGER_CST |
517 | && TREE_CODE (max ()) == INTEGER_CST); | |
cca78449 AH |
518 | } |
519 | ||
4ba9fb0a AH |
520 | /* If range is a singleton, place it in RESULT and return TRUE. |
521 | Note: A singleton can be any gimple invariant, not just constants. | |
522 | So, [&x, &x] counts as a singleton. */ | |
523 | ||
cca78449 | 524 | bool |
4ba9fb0a | 525 | irange::singleton_p (tree *result) const |
cca78449 | 526 | { |
4ba9fb0a AH |
527 | if (!legacy_mode_p ()) |
528 | { | |
529 | if (num_pairs () == 1 && (wi::to_wide (tree_lower_bound ()) | |
530 | == wi::to_wide (tree_upper_bound ()))) | |
531 | { | |
532 | if (result) | |
533 | *result = tree_lower_bound (); | |
534 | return true; | |
535 | } | |
536 | return false; | |
537 | } | |
cca78449 AH |
538 | if (m_kind == VR_ANTI_RANGE) |
539 | { | |
540 | if (nonzero_p ()) | |
541 | { | |
542 | if (TYPE_PRECISION (type ()) == 1) | |
543 | { | |
544 | if (result) | |
4ba9fb0a | 545 | *result = max (); |
cca78449 AH |
546 | return true; |
547 | } | |
548 | return false; | |
549 | } | |
550 | if (num_pairs () == 1) | |
551 | { | |
552 | value_range vr0, vr1; | |
4ba9fb0a | 553 | ranges_from_anti_range ((const value_range *) this, &vr0, &vr1); |
cca78449 AH |
554 | return vr0.singleton_p (result); |
555 | } | |
556 | } | |
4ba9fb0a | 557 | // Catches non-numeric extremes as well. |
cca78449 AH |
558 | if (m_kind == VR_RANGE |
559 | && vrp_operand_equal_p (min (), max ()) | |
560 | && is_gimple_min_invariant (min ())) | |
561 | { | |
562 | if (result) | |
563 | *result = min (); | |
564 | return true; | |
565 | } | |
566 | return false; | |
567 | } | |
568 | ||
569 | /* Return 1 if VAL is inside value range. | |
4ba9fb0a | 570 | 0 if VAL is not inside value range. |
cca78449 AH |
571 | -2 if we cannot tell either way. |
572 | ||
573 | Benchmark compile/20001226-1.c compilation time after changing this | |
574 | function. */ | |
575 | ||
576 | int | |
4ba9fb0a | 577 | irange::value_inside_range (tree val) const |
cca78449 | 578 | { |
cca78449 AH |
579 | if (varying_p ()) |
580 | return 1; | |
581 | ||
582 | if (undefined_p ()) | |
583 | return 0; | |
584 | ||
4ba9fb0a AH |
585 | if (!legacy_mode_p () && TREE_CODE (val) == INTEGER_CST) |
586 | return contains_p (val); | |
587 | ||
588 | int cmp1 = operand_less_p (val, min ()); | |
cca78449 AH |
589 | if (cmp1 == -2) |
590 | return -2; | |
591 | if (cmp1 == 1) | |
592 | return m_kind != VR_RANGE; | |
593 | ||
4ba9fb0a | 594 | int cmp2 = operand_less_p (max (), val); |
cca78449 AH |
595 | if (cmp2 == -2) |
596 | return -2; | |
597 | ||
598 | if (m_kind == VR_RANGE) | |
599 | return !cmp2; | |
600 | else | |
601 | return !!cmp2; | |
602 | } | |
603 | ||
604 | /* Return TRUE if it is possible that range contains VAL. */ | |
605 | ||
606 | bool | |
4ba9fb0a | 607 | irange::may_contain_p (tree val) const |
cca78449 AH |
608 | { |
609 | return value_inside_range (val) != 0; | |
610 | } | |
611 | ||
612 | /* Return TRUE if range contains INTEGER_CST. */ | |
4ba9fb0a AH |
613 | /* Return 1 if VAL is inside value range. |
614 | 0 if VAL is not inside value range. | |
615 | ||
616 | Benchmark compile/20001226-1.c compilation time after changing this | |
617 | function. */ | |
618 | ||
cca78449 AH |
619 | |
620 | bool | |
4ba9fb0a | 621 | irange::contains_p (tree cst) const |
cca78449 | 622 | { |
4ba9fb0a AH |
623 | if (undefined_p ()) |
624 | return false; | |
625 | ||
626 | if (legacy_mode_p ()) | |
627 | { | |
628 | gcc_checking_assert (TREE_CODE (cst) == INTEGER_CST); | |
629 | if (symbolic_p ()) | |
630 | { | |
631 | value_range numeric_range (*this); | |
632 | numeric_range.normalize_symbolics (); | |
633 | return numeric_range.contains_p (cst); | |
634 | } | |
635 | return value_inside_range (cst) == 1; | |
636 | } | |
637 | ||
cca78449 | 638 | gcc_checking_assert (TREE_CODE (cst) == INTEGER_CST); |
4ba9fb0a AH |
639 | signop sign = TYPE_SIGN (TREE_TYPE (cst)); |
640 | wide_int v = wi::to_wide (cst); | |
641 | for (unsigned r = 0; r < m_num_ranges; ++r) | |
6ee86466 | 642 | { |
4ba9fb0a AH |
643 | if (wi::lt_p (v, lower_bound (r), sign)) |
644 | return false; | |
645 | if (wi::le_p (v, upper_bound (r), sign)) | |
646 | return true; | |
6ee86466 | 647 | } |
4ba9fb0a AH |
648 | |
649 | return false; | |
cca78449 AH |
650 | } |
651 | ||
4ba9fb0a | 652 | |
cca78449 AH |
653 | /* Normalize addresses into constants. */ |
654 | ||
6ee86466 | 655 | void |
4ba9fb0a | 656 | irange::normalize_addresses () |
cca78449 AH |
657 | { |
658 | if (undefined_p ()) | |
6ee86466 | 659 | return; |
cca78449 AH |
660 | |
661 | if (!POINTER_TYPE_P (type ()) || range_has_numeric_bounds_p (this)) | |
6ee86466 | 662 | return; |
cca78449 AH |
663 | |
664 | if (!range_includes_zero_p (this)) | |
665 | { | |
4ba9fb0a AH |
666 | gcc_checking_assert (TREE_CODE (min ()) == ADDR_EXPR |
667 | || TREE_CODE (max ()) == ADDR_EXPR); | |
6ee86466 AH |
668 | set_nonzero (type ()); |
669 | return; | |
cca78449 | 670 | } |
6ee86466 | 671 | set_varying (type ()); |
cca78449 AH |
672 | } |
673 | ||
674 | /* Normalize symbolics and addresses into constants. */ | |
675 | ||
6ee86466 | 676 | void |
4ba9fb0a | 677 | irange::normalize_symbolics () |
cca78449 AH |
678 | { |
679 | if (varying_p () || undefined_p ()) | |
6ee86466 AH |
680 | return; |
681 | ||
cca78449 AH |
682 | tree ttype = type (); |
683 | bool min_symbolic = !is_gimple_min_invariant (min ()); | |
684 | bool max_symbolic = !is_gimple_min_invariant (max ()); | |
685 | if (!min_symbolic && !max_symbolic) | |
6ee86466 AH |
686 | { |
687 | normalize_addresses (); | |
688 | return; | |
689 | } | |
cca78449 AH |
690 | |
691 | // [SYM, SYM] -> VARYING | |
692 | if (min_symbolic && max_symbolic) | |
693 | { | |
6ee86466 AH |
694 | set_varying (ttype); |
695 | return; | |
cca78449 AH |
696 | } |
697 | if (kind () == VR_RANGE) | |
698 | { | |
699 | // [SYM, NUM] -> [-MIN, NUM] | |
700 | if (min_symbolic) | |
6ee86466 AH |
701 | { |
702 | set (vrp_val_min (ttype), max ()); | |
703 | return; | |
704 | } | |
cca78449 | 705 | // [NUM, SYM] -> [NUM, +MAX] |
6ee86466 AH |
706 | set (min (), vrp_val_max (ttype)); |
707 | return; | |
cca78449 AH |
708 | } |
709 | gcc_checking_assert (kind () == VR_ANTI_RANGE); | |
710 | // ~[SYM, NUM] -> [NUM + 1, +MAX] | |
711 | if (min_symbolic) | |
712 | { | |
713 | if (!vrp_val_is_max (max ())) | |
714 | { | |
715 | tree n = wide_int_to_tree (ttype, wi::to_wide (max ()) + 1); | |
6ee86466 AH |
716 | set (n, vrp_val_max (ttype)); |
717 | return; | |
cca78449 | 718 | } |
6ee86466 AH |
719 | set_varying (ttype); |
720 | return; | |
cca78449 AH |
721 | } |
722 | // ~[NUM, SYM] -> [-MIN, NUM - 1] | |
723 | if (!vrp_val_is_min (min ())) | |
724 | { | |
725 | tree n = wide_int_to_tree (ttype, wi::to_wide (min ()) - 1); | |
6ee86466 AH |
726 | set (vrp_val_min (ttype), n); |
727 | return; | |
cca78449 | 728 | } |
6ee86466 | 729 | set_varying (ttype); |
cca78449 AH |
730 | } |
731 | ||
732 | /* Intersect the two value-ranges { *VR0TYPE, *VR0MIN, *VR0MAX } and | |
733 | { VR1TYPE, VR0MIN, VR0MAX } and store the result | |
734 | in { *VR0TYPE, *VR0MIN, *VR0MAX }. This may not be the smallest | |
735 | possible such range. The resulting range is not canonicalized. */ | |
736 | ||
737 | static void | |
738 | intersect_ranges (enum value_range_kind *vr0type, | |
739 | tree *vr0min, tree *vr0max, | |
740 | enum value_range_kind vr1type, | |
741 | tree vr1min, tree vr1max) | |
742 | { | |
743 | bool mineq = vrp_operand_equal_p (*vr0min, vr1min); | |
744 | bool maxeq = vrp_operand_equal_p (*vr0max, vr1max); | |
745 | ||
746 | /* [] is vr0, () is vr1 in the following classification comments. */ | |
747 | if (mineq && maxeq) | |
748 | { | |
749 | /* [( )] */ | |
750 | if (*vr0type == vr1type) | |
751 | /* Nothing to do for equal ranges. */ | |
752 | ; | |
753 | else if ((*vr0type == VR_RANGE | |
754 | && vr1type == VR_ANTI_RANGE) | |
755 | || (*vr0type == VR_ANTI_RANGE | |
756 | && vr1type == VR_RANGE)) | |
757 | { | |
758 | /* For anti-range with range intersection the result is empty. */ | |
759 | *vr0type = VR_UNDEFINED; | |
760 | *vr0min = NULL_TREE; | |
761 | *vr0max = NULL_TREE; | |
762 | } | |
763 | else | |
764 | gcc_unreachable (); | |
765 | } | |
766 | else if (operand_less_p (*vr0max, vr1min) == 1 | |
767 | || operand_less_p (vr1max, *vr0min) == 1) | |
768 | { | |
769 | /* [ ] ( ) or ( ) [ ] | |
770 | If the ranges have an empty intersection, the result of the | |
771 | intersect operation is the range for intersecting an | |
772 | anti-range with a range or empty when intersecting two ranges. */ | |
773 | if (*vr0type == VR_RANGE | |
774 | && vr1type == VR_ANTI_RANGE) | |
775 | ; | |
776 | else if (*vr0type == VR_ANTI_RANGE | |
777 | && vr1type == VR_RANGE) | |
778 | { | |
779 | *vr0type = vr1type; | |
780 | *vr0min = vr1min; | |
781 | *vr0max = vr1max; | |
782 | } | |
783 | else if (*vr0type == VR_RANGE | |
784 | && vr1type == VR_RANGE) | |
785 | { | |
786 | *vr0type = VR_UNDEFINED; | |
787 | *vr0min = NULL_TREE; | |
788 | *vr0max = NULL_TREE; | |
789 | } | |
790 | else if (*vr0type == VR_ANTI_RANGE | |
791 | && vr1type == VR_ANTI_RANGE) | |
792 | { | |
793 | /* If the anti-ranges are adjacent to each other merge them. */ | |
794 | if (TREE_CODE (*vr0max) == INTEGER_CST | |
795 | && TREE_CODE (vr1min) == INTEGER_CST | |
796 | && operand_less_p (*vr0max, vr1min) == 1 | |
797 | && integer_onep (int_const_binop (MINUS_EXPR, | |
798 | vr1min, *vr0max))) | |
799 | *vr0max = vr1max; | |
800 | else if (TREE_CODE (vr1max) == INTEGER_CST | |
801 | && TREE_CODE (*vr0min) == INTEGER_CST | |
802 | && operand_less_p (vr1max, *vr0min) == 1 | |
803 | && integer_onep (int_const_binop (MINUS_EXPR, | |
804 | *vr0min, vr1max))) | |
805 | *vr0min = vr1min; | |
806 | /* Else arbitrarily take VR0. */ | |
807 | } | |
808 | } | |
809 | else if ((maxeq || operand_less_p (vr1max, *vr0max) == 1) | |
810 | && (mineq || operand_less_p (*vr0min, vr1min) == 1)) | |
811 | { | |
812 | /* [ ( ) ] or [( ) ] or [ ( )] */ | |
813 | if (*vr0type == VR_RANGE | |
814 | && vr1type == VR_RANGE) | |
815 | { | |
816 | /* If both are ranges the result is the inner one. */ | |
817 | *vr0type = vr1type; | |
818 | *vr0min = vr1min; | |
819 | *vr0max = vr1max; | |
820 | } | |
821 | else if (*vr0type == VR_RANGE | |
822 | && vr1type == VR_ANTI_RANGE) | |
823 | { | |
824 | /* Choose the right gap if the left one is empty. */ | |
825 | if (mineq) | |
826 | { | |
827 | if (TREE_CODE (vr1max) != INTEGER_CST) | |
828 | *vr0min = vr1max; | |
829 | else if (TYPE_PRECISION (TREE_TYPE (vr1max)) == 1 | |
830 | && !TYPE_UNSIGNED (TREE_TYPE (vr1max))) | |
831 | *vr0min | |
832 | = int_const_binop (MINUS_EXPR, vr1max, | |
833 | build_int_cst (TREE_TYPE (vr1max), -1)); | |
834 | else | |
835 | *vr0min | |
836 | = int_const_binop (PLUS_EXPR, vr1max, | |
837 | build_int_cst (TREE_TYPE (vr1max), 1)); | |
838 | } | |
839 | /* Choose the left gap if the right one is empty. */ | |
840 | else if (maxeq) | |
841 | { | |
842 | if (TREE_CODE (vr1min) != INTEGER_CST) | |
843 | *vr0max = vr1min; | |
844 | else if (TYPE_PRECISION (TREE_TYPE (vr1min)) == 1 | |
845 | && !TYPE_UNSIGNED (TREE_TYPE (vr1min))) | |
846 | *vr0max | |
847 | = int_const_binop (PLUS_EXPR, vr1min, | |
848 | build_int_cst (TREE_TYPE (vr1min), -1)); | |
849 | else | |
850 | *vr0max | |
851 | = int_const_binop (MINUS_EXPR, vr1min, | |
852 | build_int_cst (TREE_TYPE (vr1min), 1)); | |
853 | } | |
854 | /* Choose the anti-range if the range is effectively varying. */ | |
855 | else if (vrp_val_is_min (*vr0min) | |
856 | && vrp_val_is_max (*vr0max)) | |
857 | { | |
858 | *vr0type = vr1type; | |
859 | *vr0min = vr1min; | |
860 | *vr0max = vr1max; | |
861 | } | |
862 | /* Else choose the range. */ | |
863 | } | |
864 | else if (*vr0type == VR_ANTI_RANGE | |
865 | && vr1type == VR_ANTI_RANGE) | |
866 | /* If both are anti-ranges the result is the outer one. */ | |
867 | ; | |
868 | else if (*vr0type == VR_ANTI_RANGE | |
869 | && vr1type == VR_RANGE) | |
870 | { | |
871 | /* The intersection is empty. */ | |
872 | *vr0type = VR_UNDEFINED; | |
873 | *vr0min = NULL_TREE; | |
874 | *vr0max = NULL_TREE; | |
875 | } | |
876 | else | |
877 | gcc_unreachable (); | |
878 | } | |
879 | else if ((maxeq || operand_less_p (*vr0max, vr1max) == 1) | |
880 | && (mineq || operand_less_p (vr1min, *vr0min) == 1)) | |
881 | { | |
882 | /* ( [ ] ) or ([ ] ) or ( [ ]) */ | |
883 | if (*vr0type == VR_RANGE | |
884 | && vr1type == VR_RANGE) | |
885 | /* Choose the inner range. */ | |
886 | ; | |
887 | else if (*vr0type == VR_ANTI_RANGE | |
888 | && vr1type == VR_RANGE) | |
889 | { | |
890 | /* Choose the right gap if the left is empty. */ | |
891 | if (mineq) | |
892 | { | |
893 | *vr0type = VR_RANGE; | |
894 | if (TREE_CODE (*vr0max) != INTEGER_CST) | |
895 | *vr0min = *vr0max; | |
896 | else if (TYPE_PRECISION (TREE_TYPE (*vr0max)) == 1 | |
897 | && !TYPE_UNSIGNED (TREE_TYPE (*vr0max))) | |
898 | *vr0min | |
899 | = int_const_binop (MINUS_EXPR, *vr0max, | |
900 | build_int_cst (TREE_TYPE (*vr0max), -1)); | |
901 | else | |
902 | *vr0min | |
903 | = int_const_binop (PLUS_EXPR, *vr0max, | |
904 | build_int_cst (TREE_TYPE (*vr0max), 1)); | |
905 | *vr0max = vr1max; | |
906 | } | |
907 | /* Choose the left gap if the right is empty. */ | |
908 | else if (maxeq) | |
909 | { | |
910 | *vr0type = VR_RANGE; | |
911 | if (TREE_CODE (*vr0min) != INTEGER_CST) | |
912 | *vr0max = *vr0min; | |
913 | else if (TYPE_PRECISION (TREE_TYPE (*vr0min)) == 1 | |
914 | && !TYPE_UNSIGNED (TREE_TYPE (*vr0min))) | |
915 | *vr0max | |
916 | = int_const_binop (PLUS_EXPR, *vr0min, | |
917 | build_int_cst (TREE_TYPE (*vr0min), -1)); | |
918 | else | |
919 | *vr0max | |
920 | = int_const_binop (MINUS_EXPR, *vr0min, | |
921 | build_int_cst (TREE_TYPE (*vr0min), 1)); | |
922 | *vr0min = vr1min; | |
923 | } | |
924 | /* Choose the anti-range if the range is effectively varying. */ | |
925 | else if (vrp_val_is_min (vr1min) | |
926 | && vrp_val_is_max (vr1max)) | |
927 | ; | |
928 | /* Choose the anti-range if it is ~[0,0], that range is special | |
929 | enough to special case when vr1's range is relatively wide. | |
930 | At least for types bigger than int - this covers pointers | |
931 | and arguments to functions like ctz. */ | |
932 | else if (*vr0min == *vr0max | |
933 | && integer_zerop (*vr0min) | |
934 | && ((TYPE_PRECISION (TREE_TYPE (*vr0min)) | |
935 | >= TYPE_PRECISION (integer_type_node)) | |
936 | || POINTER_TYPE_P (TREE_TYPE (*vr0min))) | |
937 | && TREE_CODE (vr1max) == INTEGER_CST | |
938 | && TREE_CODE (vr1min) == INTEGER_CST | |
939 | && (wi::clz (wi::to_wide (vr1max) - wi::to_wide (vr1min)) | |
940 | < TYPE_PRECISION (TREE_TYPE (*vr0min)) / 2)) | |
941 | ; | |
942 | /* Else choose the range. */ | |
943 | else | |
944 | { | |
945 | *vr0type = vr1type; | |
946 | *vr0min = vr1min; | |
947 | *vr0max = vr1max; | |
948 | } | |
949 | } | |
950 | else if (*vr0type == VR_ANTI_RANGE | |
951 | && vr1type == VR_ANTI_RANGE) | |
952 | { | |
953 | /* If both are anti-ranges the result is the outer one. */ | |
954 | *vr0type = vr1type; | |
955 | *vr0min = vr1min; | |
956 | *vr0max = vr1max; | |
957 | } | |
958 | else if (vr1type == VR_ANTI_RANGE | |
959 | && *vr0type == VR_RANGE) | |
960 | { | |
961 | /* The intersection is empty. */ | |
962 | *vr0type = VR_UNDEFINED; | |
963 | *vr0min = NULL_TREE; | |
964 | *vr0max = NULL_TREE; | |
965 | } | |
966 | else | |
967 | gcc_unreachable (); | |
968 | } | |
969 | else if ((operand_less_p (vr1min, *vr0max) == 1 | |
970 | || operand_equal_p (vr1min, *vr0max, 0)) | |
a05cc70a RB |
971 | && operand_less_p (*vr0min, vr1min) == 1 |
972 | && operand_less_p (*vr0max, vr1max) == 1) | |
cca78449 AH |
973 | { |
974 | /* [ ( ] ) or [ ]( ) */ | |
975 | if (*vr0type == VR_ANTI_RANGE | |
976 | && vr1type == VR_ANTI_RANGE) | |
977 | *vr0max = vr1max; | |
978 | else if (*vr0type == VR_RANGE | |
979 | && vr1type == VR_RANGE) | |
980 | *vr0min = vr1min; | |
981 | else if (*vr0type == VR_RANGE | |
982 | && vr1type == VR_ANTI_RANGE) | |
983 | { | |
984 | if (TREE_CODE (vr1min) == INTEGER_CST) | |
985 | *vr0max = int_const_binop (MINUS_EXPR, vr1min, | |
986 | build_int_cst (TREE_TYPE (vr1min), 1)); | |
987 | else | |
988 | *vr0max = vr1min; | |
989 | } | |
990 | else if (*vr0type == VR_ANTI_RANGE | |
991 | && vr1type == VR_RANGE) | |
992 | { | |
993 | *vr0type = VR_RANGE; | |
994 | if (TREE_CODE (*vr0max) == INTEGER_CST) | |
995 | *vr0min = int_const_binop (PLUS_EXPR, *vr0max, | |
996 | build_int_cst (TREE_TYPE (*vr0max), 1)); | |
997 | else | |
998 | *vr0min = *vr0max; | |
999 | *vr0max = vr1max; | |
1000 | } | |
1001 | else | |
1002 | gcc_unreachable (); | |
1003 | } | |
1004 | else if ((operand_less_p (*vr0min, vr1max) == 1 | |
1005 | || operand_equal_p (*vr0min, vr1max, 0)) | |
a05cc70a RB |
1006 | && operand_less_p (vr1min, *vr0min) == 1 |
1007 | && operand_less_p (vr1max, *vr0max) == 1) | |
cca78449 AH |
1008 | { |
1009 | /* ( [ ) ] or ( )[ ] */ | |
1010 | if (*vr0type == VR_ANTI_RANGE | |
1011 | && vr1type == VR_ANTI_RANGE) | |
1012 | *vr0min = vr1min; | |
1013 | else if (*vr0type == VR_RANGE | |
1014 | && vr1type == VR_RANGE) | |
1015 | *vr0max = vr1max; | |
1016 | else if (*vr0type == VR_RANGE | |
1017 | && vr1type == VR_ANTI_RANGE) | |
1018 | { | |
1019 | if (TREE_CODE (vr1max) == INTEGER_CST) | |
1020 | *vr0min = int_const_binop (PLUS_EXPR, vr1max, | |
1021 | build_int_cst (TREE_TYPE (vr1max), 1)); | |
1022 | else | |
1023 | *vr0min = vr1max; | |
1024 | } | |
1025 | else if (*vr0type == VR_ANTI_RANGE | |
1026 | && vr1type == VR_RANGE) | |
1027 | { | |
1028 | *vr0type = VR_RANGE; | |
1029 | if (TREE_CODE (*vr0min) == INTEGER_CST) | |
1030 | *vr0max = int_const_binop (MINUS_EXPR, *vr0min, | |
1031 | build_int_cst (TREE_TYPE (*vr0min), 1)); | |
1032 | else | |
1033 | *vr0max = *vr0min; | |
1034 | *vr0min = vr1min; | |
1035 | } | |
1036 | else | |
1037 | gcc_unreachable (); | |
1038 | } | |
1039 | ||
1040 | /* If we know the intersection is empty, there's no need to | |
1041 | conservatively add anything else to the set. */ | |
1042 | if (*vr0type == VR_UNDEFINED) | |
1043 | return; | |
1044 | ||
1045 | /* As a fallback simply use { *VRTYPE, *VR0MIN, *VR0MAX } as | |
1046 | result for the intersection. That's always a conservative | |
1047 | correct estimate unless VR1 is a constant singleton range | |
1048 | in which case we choose that. */ | |
1049 | if (vr1type == VR_RANGE | |
1050 | && is_gimple_min_invariant (vr1min) | |
1051 | && vrp_operand_equal_p (vr1min, vr1max)) | |
1052 | { | |
1053 | *vr0type = vr1type; | |
1054 | *vr0min = vr1min; | |
1055 | *vr0max = vr1max; | |
1056 | } | |
1057 | } | |
1058 | ||
1059 | /* Helper for the intersection operation for value ranges. Given two | |
4ba9fb0a AH |
1060 | ranges VR0 and VR1, set VR0 to the intersection of both ranges. |
1061 | This may not be the smallest possible such range. */ | |
cca78449 | 1062 | |
4ba9fb0a AH |
1063 | void |
1064 | irange::legacy_intersect (irange *vr0, const irange *vr1) | |
cca78449 | 1065 | { |
ea6da7f5 AH |
1066 | gcc_checking_assert (vr0->legacy_mode_p ()); |
1067 | gcc_checking_assert (vr1->legacy_mode_p ()); | |
cca78449 AH |
1068 | /* If either range is VR_VARYING the other one wins. */ |
1069 | if (vr1->varying_p ()) | |
4ba9fb0a | 1070 | return; |
cca78449 | 1071 | if (vr0->varying_p ()) |
4ba9fb0a | 1072 | { |
ea6da7f5 | 1073 | vr0->set (vr1->min (), vr1->max (), vr1->kind ()); |
4ba9fb0a AH |
1074 | return; |
1075 | } | |
cca78449 AH |
1076 | |
1077 | /* When either range is VR_UNDEFINED the resulting range is | |
1078 | VR_UNDEFINED, too. */ | |
1079 | if (vr0->undefined_p ()) | |
4ba9fb0a | 1080 | return; |
cca78449 | 1081 | if (vr1->undefined_p ()) |
4ba9fb0a AH |
1082 | { |
1083 | vr0->set_undefined (); | |
1084 | return; | |
1085 | } | |
cca78449 AH |
1086 | |
1087 | value_range_kind vr0kind = vr0->kind (); | |
1088 | tree vr0min = vr0->min (); | |
1089 | tree vr0max = vr0->max (); | |
ea6da7f5 AH |
1090 | |
1091 | intersect_ranges (&vr0kind, &vr0min, &vr0max, | |
1092 | vr1->kind (), vr1->min (), vr1->max ()); | |
4ba9fb0a | 1093 | |
cca78449 | 1094 | /* Make sure to canonicalize the result though as the inversion of a |
4ba9fb0a | 1095 | VR_RANGE can still be a VR_RANGE. */ |
cca78449 | 1096 | if (vr0kind == VR_UNDEFINED) |
4ba9fb0a | 1097 | vr0->set_undefined (); |
cca78449 | 1098 | else if (vr0kind == VR_VARYING) |
4ba9fb0a AH |
1099 | { |
1100 | /* If we failed, use the original VR0. */ | |
1101 | return; | |
1102 | } | |
cca78449 | 1103 | else |
4ba9fb0a | 1104 | vr0->set (vr0min, vr0max, vr0kind); |
cca78449 AH |
1105 | } |
1106 | ||
1107 | /* Union the two value-ranges { *VR0TYPE, *VR0MIN, *VR0MAX } and | |
1108 | { VR1TYPE, VR0MIN, VR0MAX } and store the result | |
1109 | in { *VR0TYPE, *VR0MIN, *VR0MAX }. This may not be the smallest | |
1110 | possible such range. The resulting range is not canonicalized. */ | |
1111 | ||
1112 | static void | |
1113 | union_ranges (enum value_range_kind *vr0type, | |
1114 | tree *vr0min, tree *vr0max, | |
1115 | enum value_range_kind vr1type, | |
1116 | tree vr1min, tree vr1max) | |
1117 | { | |
1118 | int cmpmin = compare_values (*vr0min, vr1min); | |
1119 | int cmpmax = compare_values (*vr0max, vr1max); | |
1120 | bool mineq = cmpmin == 0; | |
1121 | bool maxeq = cmpmax == 0; | |
1122 | ||
1123 | /* [] is vr0, () is vr1 in the following classification comments. */ | |
1124 | if (mineq && maxeq) | |
1125 | { | |
1126 | /* [( )] */ | |
1127 | if (*vr0type == vr1type) | |
1128 | /* Nothing to do for equal ranges. */ | |
1129 | ; | |
1130 | else if ((*vr0type == VR_RANGE | |
1131 | && vr1type == VR_ANTI_RANGE) | |
1132 | || (*vr0type == VR_ANTI_RANGE | |
1133 | && vr1type == VR_RANGE)) | |
1134 | { | |
1135 | /* For anti-range with range union the result is varying. */ | |
1136 | goto give_up; | |
1137 | } | |
1138 | else | |
1139 | gcc_unreachable (); | |
1140 | } | |
1141 | else if (operand_less_p (*vr0max, vr1min) == 1 | |
1142 | || operand_less_p (vr1max, *vr0min) == 1) | |
1143 | { | |
1144 | /* [ ] ( ) or ( ) [ ] | |
1145 | If the ranges have an empty intersection, result of the union | |
1146 | operation is the anti-range or if both are anti-ranges | |
1147 | it covers all. */ | |
1148 | if (*vr0type == VR_ANTI_RANGE | |
1149 | && vr1type == VR_ANTI_RANGE) | |
1150 | goto give_up; | |
1151 | else if (*vr0type == VR_ANTI_RANGE | |
1152 | && vr1type == VR_RANGE) | |
1153 | ; | |
1154 | else if (*vr0type == VR_RANGE | |
1155 | && vr1type == VR_ANTI_RANGE) | |
1156 | { | |
1157 | *vr0type = vr1type; | |
1158 | *vr0min = vr1min; | |
1159 | *vr0max = vr1max; | |
1160 | } | |
1161 | else if (*vr0type == VR_RANGE | |
1162 | && vr1type == VR_RANGE) | |
1163 | { | |
1164 | /* The result is the convex hull of both ranges. */ | |
1165 | if (operand_less_p (*vr0max, vr1min) == 1) | |
1166 | { | |
1167 | /* If the result can be an anti-range, create one. */ | |
1168 | if (TREE_CODE (*vr0max) == INTEGER_CST | |
1169 | && TREE_CODE (vr1min) == INTEGER_CST | |
1170 | && vrp_val_is_min (*vr0min) | |
1171 | && vrp_val_is_max (vr1max)) | |
1172 | { | |
1173 | tree min = int_const_binop (PLUS_EXPR, | |
1174 | *vr0max, | |
1175 | build_int_cst (TREE_TYPE (*vr0max), 1)); | |
1176 | tree max = int_const_binop (MINUS_EXPR, | |
1177 | vr1min, | |
1178 | build_int_cst (TREE_TYPE (vr1min), 1)); | |
1179 | if (!operand_less_p (max, min)) | |
1180 | { | |
1181 | *vr0type = VR_ANTI_RANGE; | |
1182 | *vr0min = min; | |
1183 | *vr0max = max; | |
1184 | } | |
1185 | else | |
1186 | *vr0max = vr1max; | |
1187 | } | |
1188 | else | |
1189 | *vr0max = vr1max; | |
1190 | } | |
1191 | else | |
1192 | { | |
1193 | /* If the result can be an anti-range, create one. */ | |
1194 | if (TREE_CODE (vr1max) == INTEGER_CST | |
1195 | && TREE_CODE (*vr0min) == INTEGER_CST | |
1196 | && vrp_val_is_min (vr1min) | |
1197 | && vrp_val_is_max (*vr0max)) | |
1198 | { | |
1199 | tree min = int_const_binop (PLUS_EXPR, | |
1200 | vr1max, | |
1201 | build_int_cst (TREE_TYPE (vr1max), 1)); | |
1202 | tree max = int_const_binop (MINUS_EXPR, | |
1203 | *vr0min, | |
1204 | build_int_cst (TREE_TYPE (*vr0min), 1)); | |
1205 | if (!operand_less_p (max, min)) | |
1206 | { | |
1207 | *vr0type = VR_ANTI_RANGE; | |
1208 | *vr0min = min; | |
1209 | *vr0max = max; | |
1210 | } | |
1211 | else | |
1212 | *vr0min = vr1min; | |
1213 | } | |
1214 | else | |
1215 | *vr0min = vr1min; | |
1216 | } | |
1217 | } | |
1218 | else | |
1219 | gcc_unreachable (); | |
1220 | } | |
1221 | else if ((maxeq || cmpmax == 1) | |
1222 | && (mineq || cmpmin == -1)) | |
1223 | { | |
1224 | /* [ ( ) ] or [( ) ] or [ ( )] */ | |
1225 | if (*vr0type == VR_RANGE | |
1226 | && vr1type == VR_RANGE) | |
1227 | ; | |
1228 | else if (*vr0type == VR_ANTI_RANGE | |
1229 | && vr1type == VR_ANTI_RANGE) | |
1230 | { | |
1231 | *vr0type = vr1type; | |
1232 | *vr0min = vr1min; | |
1233 | *vr0max = vr1max; | |
1234 | } | |
1235 | else if (*vr0type == VR_ANTI_RANGE | |
1236 | && vr1type == VR_RANGE) | |
1237 | { | |
1238 | /* Arbitrarily choose the right or left gap. */ | |
1239 | if (!mineq && TREE_CODE (vr1min) == INTEGER_CST) | |
1240 | *vr0max = int_const_binop (MINUS_EXPR, vr1min, | |
1241 | build_int_cst (TREE_TYPE (vr1min), 1)); | |
1242 | else if (!maxeq && TREE_CODE (vr1max) == INTEGER_CST) | |
1243 | *vr0min = int_const_binop (PLUS_EXPR, vr1max, | |
1244 | build_int_cst (TREE_TYPE (vr1max), 1)); | |
1245 | else | |
1246 | goto give_up; | |
1247 | } | |
1248 | else if (*vr0type == VR_RANGE | |
1249 | && vr1type == VR_ANTI_RANGE) | |
1250 | /* The result covers everything. */ | |
1251 | goto give_up; | |
1252 | else | |
1253 | gcc_unreachable (); | |
1254 | } | |
1255 | else if ((maxeq || cmpmax == -1) | |
1256 | && (mineq || cmpmin == 1)) | |
1257 | { | |
1258 | /* ( [ ] ) or ([ ] ) or ( [ ]) */ | |
1259 | if (*vr0type == VR_RANGE | |
1260 | && vr1type == VR_RANGE) | |
1261 | { | |
1262 | *vr0type = vr1type; | |
1263 | *vr0min = vr1min; | |
1264 | *vr0max = vr1max; | |
1265 | } | |
1266 | else if (*vr0type == VR_ANTI_RANGE | |
1267 | && vr1type == VR_ANTI_RANGE) | |
1268 | ; | |
1269 | else if (*vr0type == VR_RANGE | |
1270 | && vr1type == VR_ANTI_RANGE) | |
1271 | { | |
1272 | *vr0type = VR_ANTI_RANGE; | |
1273 | if (!mineq && TREE_CODE (*vr0min) == INTEGER_CST) | |
1274 | { | |
1275 | *vr0max = int_const_binop (MINUS_EXPR, *vr0min, | |
1276 | build_int_cst (TREE_TYPE (*vr0min), 1)); | |
1277 | *vr0min = vr1min; | |
1278 | } | |
1279 | else if (!maxeq && TREE_CODE (*vr0max) == INTEGER_CST) | |
1280 | { | |
1281 | *vr0min = int_const_binop (PLUS_EXPR, *vr0max, | |
1282 | build_int_cst (TREE_TYPE (*vr0max), 1)); | |
1283 | *vr0max = vr1max; | |
1284 | } | |
1285 | else | |
1286 | goto give_up; | |
1287 | } | |
1288 | else if (*vr0type == VR_ANTI_RANGE | |
1289 | && vr1type == VR_RANGE) | |
1290 | /* The result covers everything. */ | |
1291 | goto give_up; | |
1292 | else | |
1293 | gcc_unreachable (); | |
1294 | } | |
1295 | else if (cmpmin == -1 | |
1296 | && cmpmax == -1 | |
1297 | && (operand_less_p (vr1min, *vr0max) == 1 | |
1298 | || operand_equal_p (vr1min, *vr0max, 0))) | |
1299 | { | |
1300 | /* [ ( ] ) or [ ]( ) */ | |
1301 | if (*vr0type == VR_RANGE | |
1302 | && vr1type == VR_RANGE) | |
1303 | *vr0max = vr1max; | |
1304 | else if (*vr0type == VR_ANTI_RANGE | |
1305 | && vr1type == VR_ANTI_RANGE) | |
1306 | *vr0min = vr1min; | |
1307 | else if (*vr0type == VR_ANTI_RANGE | |
1308 | && vr1type == VR_RANGE) | |
1309 | { | |
1310 | if (TREE_CODE (vr1min) == INTEGER_CST) | |
1311 | *vr0max = int_const_binop (MINUS_EXPR, vr1min, | |
1312 | build_int_cst (TREE_TYPE (vr1min), 1)); | |
1313 | else | |
1314 | goto give_up; | |
1315 | } | |
1316 | else if (*vr0type == VR_RANGE | |
1317 | && vr1type == VR_ANTI_RANGE) | |
1318 | { | |
1319 | if (TREE_CODE (*vr0max) == INTEGER_CST) | |
1320 | { | |
1321 | *vr0type = vr1type; | |
1322 | *vr0min = int_const_binop (PLUS_EXPR, *vr0max, | |
1323 | build_int_cst (TREE_TYPE (*vr0max), 1)); | |
1324 | *vr0max = vr1max; | |
1325 | } | |
1326 | else | |
1327 | goto give_up; | |
1328 | } | |
1329 | else | |
1330 | gcc_unreachable (); | |
1331 | } | |
1332 | else if (cmpmin == 1 | |
1333 | && cmpmax == 1 | |
1334 | && (operand_less_p (*vr0min, vr1max) == 1 | |
1335 | || operand_equal_p (*vr0min, vr1max, 0))) | |
1336 | { | |
1337 | /* ( [ ) ] or ( )[ ] */ | |
1338 | if (*vr0type == VR_RANGE | |
1339 | && vr1type == VR_RANGE) | |
1340 | *vr0min = vr1min; | |
1341 | else if (*vr0type == VR_ANTI_RANGE | |
1342 | && vr1type == VR_ANTI_RANGE) | |
1343 | *vr0max = vr1max; | |
1344 | else if (*vr0type == VR_ANTI_RANGE | |
1345 | && vr1type == VR_RANGE) | |
1346 | { | |
1347 | if (TREE_CODE (vr1max) == INTEGER_CST) | |
1348 | *vr0min = int_const_binop (PLUS_EXPR, vr1max, | |
1349 | build_int_cst (TREE_TYPE (vr1max), 1)); | |
1350 | else | |
1351 | goto give_up; | |
1352 | } | |
1353 | else if (*vr0type == VR_RANGE | |
1354 | && vr1type == VR_ANTI_RANGE) | |
1355 | { | |
1356 | if (TREE_CODE (*vr0min) == INTEGER_CST) | |
1357 | { | |
1358 | *vr0type = vr1type; | |
1359 | *vr0max = int_const_binop (MINUS_EXPR, *vr0min, | |
1360 | build_int_cst (TREE_TYPE (*vr0min), 1)); | |
1361 | *vr0min = vr1min; | |
1362 | } | |
1363 | else | |
1364 | goto give_up; | |
1365 | } | |
1366 | else | |
1367 | gcc_unreachable (); | |
1368 | } | |
1369 | else | |
1370 | goto give_up; | |
1371 | ||
1372 | return; | |
1373 | ||
1374 | give_up: | |
1375 | *vr0type = VR_VARYING; | |
1376 | *vr0min = NULL_TREE; | |
1377 | *vr0max = NULL_TREE; | |
1378 | } | |
1379 | ||
4ba9fb0a AH |
1380 | /* Helper for meet operation for value ranges. Given two ranges VR0 |
1381 | and VR1, set VR0 to the union of both ranges. This may not be the | |
cca78449 AH |
1382 | smallest possible such range. */ |
1383 | ||
4ba9fb0a AH |
1384 | void |
1385 | irange::legacy_union (irange *vr0, const irange *vr1) | |
cca78449 | 1386 | { |
ea6da7f5 AH |
1387 | gcc_checking_assert (vr0->legacy_mode_p ()); |
1388 | gcc_checking_assert (vr1->legacy_mode_p ()); | |
1389 | ||
cca78449 AH |
1390 | /* VR0 has the resulting range if VR1 is undefined or VR0 is varying. */ |
1391 | if (vr1->undefined_p () | |
1392 | || vr0->varying_p ()) | |
4ba9fb0a | 1393 | return; |
cca78449 AH |
1394 | |
1395 | /* VR1 has the resulting range if VR0 is undefined or VR1 is varying. */ | |
4ba9fb0a AH |
1396 | if (vr0->undefined_p ()) |
1397 | { | |
ea6da7f5 | 1398 | vr0->set (vr1->min (), vr1->max (), vr1->kind ()); |
4ba9fb0a AH |
1399 | return; |
1400 | } | |
ea6da7f5 | 1401 | |
4ba9fb0a AH |
1402 | if (vr1->varying_p ()) |
1403 | { | |
1404 | vr0->set_varying (vr1->type ()); | |
1405 | return; | |
1406 | } | |
cca78449 AH |
1407 | |
1408 | value_range_kind vr0kind = vr0->kind (); | |
1409 | tree vr0min = vr0->min (); | |
1410 | tree vr0max = vr0->max (); | |
ea6da7f5 AH |
1411 | |
1412 | union_ranges (&vr0kind, &vr0min, &vr0max, | |
1413 | vr1->kind (), vr1->min (), vr1->max ()); | |
cca78449 | 1414 | |
cca78449 | 1415 | if (vr0kind == VR_UNDEFINED) |
4ba9fb0a | 1416 | vr0->set_undefined (); |
cca78449 | 1417 | else if (vr0kind == VR_VARYING) |
cca78449 | 1418 | { |
4ba9fb0a AH |
1419 | /* Failed to find an efficient meet. Before giving up and |
1420 | setting the result to VARYING, see if we can at least derive | |
1421 | a non-zero range. */ | |
1422 | if (range_includes_zero_p (vr0) == 0 | |
1423 | && range_includes_zero_p (vr1) == 0) | |
1424 | vr0->set_nonzero (vr0->type ()); | |
1425 | else | |
1426 | vr0->set_varying (vr0->type ()); | |
cca78449 | 1427 | } |
4ba9fb0a AH |
1428 | else |
1429 | vr0->set (vr0min, vr0max, vr0kind); | |
cca78449 AH |
1430 | } |
1431 | ||
1432 | /* Meet operation for value ranges. Given two value ranges VR0 and | |
1433 | VR1, store in VR0 a range that contains both VR0 and VR1. This | |
1434 | may not be the smallest possible such range. */ | |
1435 | ||
1436 | void | |
4ba9fb0a | 1437 | irange::union_ (const irange *other) |
cca78449 | 1438 | { |
4ba9fb0a | 1439 | if (legacy_mode_p ()) |
cca78449 | 1440 | { |
ea6da7f5 AH |
1441 | if (!other->legacy_mode_p ()) |
1442 | { | |
1443 | int_range<1> tmp = *other; | |
1444 | legacy_union (this, &tmp); | |
1445 | return; | |
1446 | } | |
4ba9fb0a AH |
1447 | if (dump_file && (dump_flags & TDF_DETAILS)) |
1448 | { | |
1449 | fprintf (dump_file, "Meeting\n "); | |
1450 | dump_value_range (dump_file, this); | |
1451 | fprintf (dump_file, "\nand\n "); | |
1452 | dump_value_range (dump_file, other); | |
1453 | fprintf (dump_file, "\n"); | |
1454 | } | |
cca78449 | 1455 | |
4ba9fb0a | 1456 | legacy_union (this, other); |
cca78449 | 1457 | |
4ba9fb0a AH |
1458 | if (dump_file && (dump_flags & TDF_DETAILS)) |
1459 | { | |
1460 | fprintf (dump_file, "to\n "); | |
1461 | dump_value_range (dump_file, this); | |
1462 | fprintf (dump_file, "\n"); | |
1463 | } | |
1464 | return; | |
1465 | } | |
1466 | ||
1467 | if (other->legacy_mode_p ()) | |
cca78449 | 1468 | { |
ea6da7f5 | 1469 | int_range<2> wider = *other; |
4ba9fb0a | 1470 | irange_union (wider); |
cca78449 | 1471 | } |
4ba9fb0a AH |
1472 | else |
1473 | irange_union (*other); | |
cca78449 AH |
1474 | } |
1475 | ||
cca78449 | 1476 | void |
4ba9fb0a | 1477 | irange::intersect (const irange *other) |
cca78449 | 1478 | { |
4ba9fb0a AH |
1479 | if (legacy_mode_p ()) |
1480 | { | |
ea6da7f5 AH |
1481 | if (!other->legacy_mode_p ()) |
1482 | { | |
1483 | int_range<1> tmp = *other; | |
1484 | legacy_intersect (this, &tmp); | |
1485 | return; | |
1486 | } | |
4ba9fb0a AH |
1487 | if (dump_file && (dump_flags & TDF_DETAILS)) |
1488 | { | |
1489 | fprintf (dump_file, "Intersecting\n "); | |
1490 | dump_value_range (dump_file, this); | |
1491 | fprintf (dump_file, "\nand\n "); | |
1492 | dump_value_range (dump_file, other); | |
1493 | fprintf (dump_file, "\n"); | |
1494 | } | |
1495 | ||
1496 | legacy_intersect (this, other); | |
1497 | ||
1498 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
1499 | { | |
1500 | fprintf (dump_file, "to\n "); | |
1501 | dump_value_range (dump_file, this); | |
1502 | fprintf (dump_file, "\n"); | |
1503 | } | |
1504 | return; | |
1505 | } | |
1506 | ||
1507 | if (other->legacy_mode_p ()) | |
1508 | { | |
1509 | int_range<2> wider; | |
1510 | wider = *other; | |
1511 | irange_intersect (wider); | |
1512 | } | |
1513 | else | |
1514 | irange_intersect (*other); | |
cca78449 AH |
1515 | } |
1516 | ||
4ba9fb0a AH |
1517 | // union_ for multi-ranges. |
1518 | ||
cca78449 | 1519 | void |
4ba9fb0a | 1520 | irange::irange_union (const irange &r) |
cca78449 | 1521 | { |
4ba9fb0a AH |
1522 | gcc_checking_assert (!legacy_mode_p () && !r.legacy_mode_p ()); |
1523 | ||
1524 | if (r.undefined_p () || varying_p ()) | |
1525 | return; | |
1526 | ||
1527 | if (undefined_p () || r.varying_p ()) | |
cca78449 | 1528 | { |
4ba9fb0a AH |
1529 | operator= (r); |
1530 | return; | |
cca78449 AH |
1531 | } |
1532 | ||
4ba9fb0a AH |
1533 | // Do not worry about merging and such by reserving twice as many |
1534 | // pairs as needed, and then simply sort the 2 ranges into this | |
1535 | // intermediate form. | |
1536 | // | |
1537 | // The intermediate result will have the property that the beginning | |
1538 | // of each range is <= the beginning of the next range. There may | |
1539 | // be overlapping ranges at this point. I.e. this would be valid | |
1540 | // [-20, 10], [-10, 0], [0, 20], [40, 90] as it satisfies this | |
1541 | // contraint : -20 < -10 < 0 < 40. When the range is rebuilt into r, | |
1542 | // the merge is performed. | |
1543 | // | |
1544 | // [Xi,Yi]..[Xn,Yn] U [Xj,Yj]..[Xm,Ym] --> [Xk,Yk]..[Xp,Yp] | |
1545 | tree ttype = r.type (); | |
1546 | signop sign = TYPE_SIGN (ttype); | |
1547 | ||
1548 | auto_vec<tree, 20> res; | |
1549 | wide_int u1 ; | |
1550 | wi::overflow_type ovf; | |
1551 | unsigned i = 0, j = 0, k = 0; | |
1552 | ||
1553 | while (i < m_num_ranges * 2 && j < r.m_num_ranges * 2) | |
1554 | { | |
1555 | // lower of Xi and Xj is the lowest point. | |
1556 | if (wi::le_p (wi::to_wide (m_base[i]), wi::to_wide (r.m_base[j]), sign)) | |
1557 | { | |
1558 | res.safe_push (m_base[i]); | |
1559 | res.safe_push (m_base[i + 1]); | |
1560 | k += 2; | |
1561 | i += 2; | |
1562 | } | |
1563 | else | |
1564 | { | |
1565 | res.safe_push (r.m_base[j]); | |
1566 | res.safe_push (r.m_base[j + 1]); | |
1567 | k += 2; | |
1568 | j += 2; | |
1569 | } | |
1570 | } | |
1571 | for ( ; i < m_num_ranges * 2; i += 2) | |
1572 | { | |
1573 | res.safe_push (m_base[i]); | |
1574 | res.safe_push (m_base[i + 1]); | |
1575 | k += 2; | |
1576 | } | |
1577 | for ( ; j < r.m_num_ranges * 2; j += 2) | |
1578 | { | |
1579 | res.safe_push (r.m_base[j]); | |
1580 | res.safe_push (r.m_base[j + 1]); | |
1581 | k += 2; | |
1582 | } | |
cca78449 | 1583 | |
4ba9fb0a AH |
1584 | // Now normalize the vector removing any overlaps. |
1585 | i = 2; | |
1586 | int prec = TYPE_PRECISION (ttype); | |
1587 | wide_int max_val = wi::max_value (prec, sign); | |
1588 | for (j = 2; j < k ; j += 2) | |
cca78449 | 1589 | { |
4ba9fb0a AH |
1590 | wide_int val_im1 = wi::to_wide (res[i - 1]); |
1591 | if (val_im1 == max_val) | |
1592 | break; | |
1593 | u1 = wi::add (val_im1, 1, sign, &ovf); | |
1594 | ||
1595 | // Overflow indicates we are at MAX already. | |
1596 | // A wide int bug requires the previous max_val check | |
1597 | // trigger: gcc.c-torture/compile/pr80443.c with -O3 | |
1598 | if (ovf == wi::OVF_OVERFLOW) | |
1599 | break; | |
1600 | ||
1601 | wide_int val_j = wi::to_wide (res[j]); | |
1602 | wide_int val_jp1 = wi::to_wide (res[j+1]); | |
1603 | // Current upper+1 is >= lower bound next pair, then we merge ranges. | |
1604 | if (wi::ge_p (u1, val_j, sign)) | |
1605 | { | |
1606 | // New upper bounds is greater of current or the next one. | |
1607 | if (wi::gt_p (val_jp1, val_im1, sign)) | |
1608 | res [i - 1] = res[j + 1]; | |
1609 | } | |
1610 | else | |
1611 | { | |
1612 | // This is a new distinct range, but no point in copying it | |
1613 | // if it is already in the right place. | |
1614 | if (i != j) | |
1615 | { | |
1616 | res[i++] = res[j]; | |
1617 | res[i++] = res[j + 1]; | |
1618 | } | |
1619 | else | |
1620 | i += 2; | |
1621 | } | |
cca78449 | 1622 | } |
4ba9fb0a AH |
1623 | |
1624 | // At this point, the vector should have i ranges, none overlapping. | |
1625 | // Now it simply needs to be copied, and if there are too many | |
1626 | // ranges, merge some. We wont do any analysis as to what the | |
1627 | // "best" merges are, simply combine the final ranges into one. | |
1628 | if (i > m_max_ranges * 2) | |
1629 | { | |
1630 | res[m_max_ranges * 2 - 1] = res[i - 1]; | |
1631 | i = m_max_ranges * 2; | |
1632 | } | |
1633 | ||
1634 | for (j = 0; j < i ; j++) | |
1635 | m_base[j] = res [j]; | |
1636 | m_num_ranges = i / 2; | |
1637 | ||
dc80d5e8 AH |
1638 | m_kind = VR_RANGE; |
1639 | normalize_kind (); | |
1640 | ||
4ba9fb0a AH |
1641 | if (flag_checking) |
1642 | verify_range (); | |
cca78449 AH |
1643 | } |
1644 | ||
4ba9fb0a | 1645 | // intersect for multi-ranges. |
cca78449 AH |
1646 | |
1647 | void | |
4ba9fb0a AH |
1648 | irange::irange_intersect (const irange &r) |
1649 | { | |
1650 | gcc_checking_assert (!legacy_mode_p () && !r.legacy_mode_p ()); | |
1651 | ||
1652 | if (undefined_p () || r.varying_p ()) | |
1653 | return; | |
1654 | if (r.undefined_p ()) | |
1655 | { | |
1656 | set_undefined (); | |
1657 | return; | |
1658 | } | |
1659 | if (varying_p ()) | |
1660 | { | |
1661 | operator= (r); | |
1662 | return; | |
1663 | } | |
1664 | ||
1665 | signop sign = TYPE_SIGN (TREE_TYPE(m_base[0])); | |
1666 | unsigned bld_pair = 0; | |
1667 | unsigned bld_lim = m_max_ranges; | |
c5a6c223 | 1668 | int_range_max r2 (*this); |
4ba9fb0a AH |
1669 | unsigned r2_lim = r2.num_pairs (); |
1670 | unsigned i2 = 0; | |
1671 | for (unsigned i = 0; i < r.num_pairs (); ) | |
1672 | { | |
1673 | // If r1's upper is < r2's lower, we can skip r1's pair. | |
1674 | tree ru = r.m_base[i * 2 + 1]; | |
1675 | tree r2l = r2.m_base[i2 * 2]; | |
1676 | if (wi::lt_p (wi::to_wide (ru), wi::to_wide (r2l), sign)) | |
1677 | { | |
1678 | i++; | |
1679 | continue; | |
1680 | } | |
1681 | // Likewise, skip r2's pair if its excluded. | |
1682 | tree r2u = r2.m_base[i2 * 2 + 1]; | |
1683 | tree rl = r.m_base[i * 2]; | |
1684 | if (wi::lt_p (wi::to_wide (r2u), wi::to_wide (rl), sign)) | |
1685 | { | |
1686 | i2++; | |
1687 | if (i2 < r2_lim) | |
1688 | continue; | |
1689 | // No more r2, break. | |
1690 | break; | |
1691 | } | |
1692 | ||
1693 | // Must be some overlap. Find the highest of the lower bounds, | |
1694 | // and set it, unless the build limits lower bounds is already | |
1695 | // set. | |
1696 | if (bld_pair < bld_lim) | |
1697 | { | |
1698 | if (wi::ge_p (wi::to_wide (rl), wi::to_wide (r2l), sign)) | |
1699 | m_base[bld_pair * 2] = rl; | |
1700 | else | |
1701 | m_base[bld_pair * 2] = r2l; | |
1702 | } | |
1703 | else | |
1704 | // Decrease and set a new upper. | |
1705 | bld_pair--; | |
1706 | ||
1707 | // ...and choose the lower of the upper bounds. | |
1708 | if (wi::le_p (wi::to_wide (ru), wi::to_wide (r2u), sign)) | |
1709 | { | |
1710 | m_base[bld_pair * 2 + 1] = ru; | |
1711 | bld_pair++; | |
1712 | // Move past the r1 pair and keep trying. | |
1713 | i++; | |
1714 | continue; | |
1715 | } | |
1716 | else | |
1717 | { | |
1718 | m_base[bld_pair * 2 + 1] = r2u; | |
1719 | bld_pair++; | |
1720 | i2++; | |
1721 | if (i2 < r2_lim) | |
1722 | continue; | |
1723 | // No more r2, break. | |
1724 | break; | |
1725 | } | |
1726 | // r2 has the higher lower bound. | |
1727 | } | |
1728 | ||
1729 | // At the exit of this loop, it is one of 2 things: | |
1730 | // ran out of r1, or r2, but either means we are done. | |
1731 | m_num_ranges = bld_pair; | |
dc80d5e8 AH |
1732 | |
1733 | m_kind = VR_RANGE; | |
1734 | normalize_kind (); | |
1735 | ||
4ba9fb0a AH |
1736 | if (flag_checking) |
1737 | verify_range (); | |
1738 | } | |
1739 | ||
2118438f AH |
1740 | // Signed 1-bits are strange. You can't subtract 1, because you can't |
1741 | // represent the number 1. This works around that for the invert routine. | |
1742 | ||
4ba9fb0a AH |
1743 | static wide_int inline |
1744 | subtract_one (const wide_int &x, tree type, wi::overflow_type &overflow) | |
cca78449 | 1745 | { |
4ba9fb0a | 1746 | if (TYPE_SIGN (type) == SIGNED) |
2118438f | 1747 | return wi::add (x, -1, SIGNED, &overflow); |
4ba9fb0a AH |
1748 | else |
1749 | return wi::sub (x, 1, UNSIGNED, &overflow); | |
cca78449 AH |
1750 | } |
1751 | ||
2118438f AH |
1752 | // The analogous function for adding 1. |
1753 | ||
1754 | static wide_int inline | |
1755 | add_one (const wide_int &x, tree type, wi::overflow_type &overflow) | |
1756 | { | |
1757 | if (TYPE_SIGN (type) == SIGNED) | |
1758 | return wi::sub (x, -1, SIGNED, &overflow); | |
1759 | else | |
1760 | return wi::add (x, 1, UNSIGNED, &overflow); | |
1761 | } | |
1762 | ||
1763 | // Return the inverse of a range. | |
cca78449 AH |
1764 | |
1765 | void | |
4ba9fb0a | 1766 | irange::invert () |
cca78449 | 1767 | { |
4ba9fb0a AH |
1768 | if (legacy_mode_p ()) |
1769 | { | |
1770 | // We can't just invert VR_RANGE and VR_ANTI_RANGE because we may | |
1771 | // create non-canonical ranges. Use the constructors instead. | |
1772 | if (m_kind == VR_RANGE) | |
1773 | *this = value_range (min (), max (), VR_ANTI_RANGE); | |
1774 | else if (m_kind == VR_ANTI_RANGE) | |
1775 | *this = value_range (min (), max ()); | |
1776 | else | |
1777 | gcc_unreachable (); | |
1778 | return; | |
1779 | } | |
1780 | ||
db3581c4 | 1781 | gcc_checking_assert (!undefined_p () && !varying_p ()); |
4ba9fb0a AH |
1782 | |
1783 | // We always need one more set of bounds to represent an inverse, so | |
1784 | // if we're at the limit, we can't properly represent things. | |
1785 | // | |
1786 | // For instance, to represent the inverse of a 2 sub-range set | |
1787 | // [5, 10][20, 30], we would need a 3 sub-range set | |
1788 | // [-MIN, 4][11, 19][31, MAX]. | |
1789 | // | |
1790 | // In this case, return the most conservative thing. | |
1791 | // | |
1792 | // However, if any of the extremes of the range are -MIN/+MAX, we | |
1793 | // know we will not need an extra bound. For example: | |
1794 | // | |
1795 | // INVERT([-MIN,20][30,40]) => [21,29][41,+MAX] | |
1796 | // INVERT([-MIN,20][30,MAX]) => [21,29] | |
1797 | tree ttype = type (); | |
1798 | unsigned prec = TYPE_PRECISION (ttype); | |
1799 | signop sign = TYPE_SIGN (ttype); | |
1800 | wide_int type_min = wi::min_value (prec, sign); | |
1801 | wide_int type_max = wi::max_value (prec, sign); | |
1802 | if (m_num_ranges == m_max_ranges | |
1803 | && lower_bound () != type_min | |
1804 | && upper_bound () != type_max) | |
1805 | { | |
1806 | m_base[1] = wide_int_to_tree (ttype, type_max); | |
1807 | m_num_ranges = 1; | |
1808 | return; | |
1809 | } | |
1810 | // The algorithm is as follows. To calculate INVERT ([a,b][c,d]), we | |
1811 | // generate [-MIN, a-1][b+1, c-1][d+1, MAX]. | |
1812 | // | |
1813 | // If there is an over/underflow in the calculation for any | |
1814 | // sub-range, we eliminate that subrange. This allows us to easily | |
1815 | // calculate INVERT([-MIN, 5]) with: [-MIN, -MIN-1][6, MAX]. And since | |
1816 | // we eliminate the underflow, only [6, MAX] remains. | |
1817 | unsigned i = 0; | |
1818 | wi::overflow_type ovf; | |
1819 | // Construct leftmost range. | |
c5a6c223 | 1820 | int_range_max orig_range (*this); |
4ba9fb0a AH |
1821 | unsigned nitems = 0; |
1822 | wide_int tmp; | |
1823 | // If this is going to underflow on the MINUS 1, don't even bother | |
1824 | // checking. This also handles subtracting one from an unsigned 0, | |
1825 | // which doesn't set the underflow bit. | |
1826 | if (type_min != orig_range.lower_bound ()) | |
1827 | { | |
1828 | m_base[nitems++] = wide_int_to_tree (ttype, type_min); | |
1829 | tmp = subtract_one (orig_range.lower_bound (), ttype, ovf); | |
1830 | m_base[nitems++] = wide_int_to_tree (ttype, tmp); | |
1831 | if (ovf) | |
1832 | nitems = 0; | |
1833 | } | |
1834 | i++; | |
1835 | // Construct middle ranges if applicable. | |
1836 | if (orig_range.num_pairs () > 1) | |
1837 | { | |
1838 | unsigned j = i; | |
1839 | for (; j < (orig_range.num_pairs () * 2) - 1; j += 2) | |
1840 | { | |
1841 | // The middle ranges cannot have MAX/MIN, so there's no need | |
1842 | // to check for unsigned overflow on the +1 and -1 here. | |
1843 | tmp = wi::add (wi::to_wide (orig_range.m_base[j]), 1, sign, &ovf); | |
1844 | m_base[nitems++] = wide_int_to_tree (ttype, tmp); | |
1845 | tmp = subtract_one (wi::to_wide (orig_range.m_base[j + 1]), | |
1846 | ttype, ovf); | |
1847 | m_base[nitems++] = wide_int_to_tree (ttype, tmp); | |
1848 | if (ovf) | |
1849 | nitems -= 2; | |
1850 | } | |
1851 | i = j; | |
1852 | } | |
1853 | // Construct rightmost range. | |
1854 | // | |
1855 | // However, if this will overflow on the PLUS 1, don't even bother. | |
1856 | // This also handles adding one to an unsigned MAX, which doesn't | |
1857 | // set the overflow bit. | |
1858 | if (type_max != wi::to_wide (orig_range.m_base[i])) | |
1859 | { | |
2118438f | 1860 | tmp = add_one (wi::to_wide (orig_range.m_base[i]), ttype, ovf); |
4ba9fb0a AH |
1861 | m_base[nitems++] = wide_int_to_tree (ttype, tmp); |
1862 | m_base[nitems++] = wide_int_to_tree (ttype, type_max); | |
1863 | if (ovf) | |
1864 | nitems -= 2; | |
1865 | } | |
1866 | m_num_ranges = nitems / 2; | |
1867 | ||
dc80d5e8 AH |
1868 | // We disallow undefined or varying coming in, so the result can |
1869 | // only be a VR_RANGE. | |
1870 | gcc_checking_assert (m_kind == VR_RANGE); | |
1871 | ||
4ba9fb0a AH |
1872 | if (flag_checking) |
1873 | verify_range (); | |
1874 | } | |
1875 | ||
1876 | static void | |
1877 | dump_bound_with_infinite_markers (FILE *file, tree bound) | |
1878 | { | |
1879 | tree type = TREE_TYPE (bound); | |
3d3470e2 AH |
1880 | wide_int type_min = wi::min_value (TYPE_PRECISION (type), TYPE_SIGN (type)); |
1881 | wide_int type_max = wi::max_value (TYPE_PRECISION (type), TYPE_SIGN (type)); | |
1882 | ||
4ba9fb0a AH |
1883 | if (INTEGRAL_TYPE_P (type) |
1884 | && !TYPE_UNSIGNED (type) | |
3d3470e2 AH |
1885 | && TREE_CODE (bound) == INTEGER_CST |
1886 | && wi::to_wide (bound) == type_min | |
4ba9fb0a AH |
1887 | && TYPE_PRECISION (type) != 1) |
1888 | fprintf (file, "-INF"); | |
3d3470e2 AH |
1889 | else if (TREE_CODE (bound) == INTEGER_CST |
1890 | && wi::to_wide (bound) == type_max | |
4ba9fb0a AH |
1891 | && TYPE_PRECISION (type) != 1) |
1892 | fprintf (file, "+INF"); | |
cca78449 | 1893 | else |
4ba9fb0a | 1894 | print_generic_expr (file, bound); |
cca78449 AH |
1895 | } |
1896 | ||
1897 | void | |
4ba9fb0a | 1898 | irange::dump (FILE *file) const |
cca78449 AH |
1899 | { |
1900 | if (undefined_p ()) | |
cca78449 | 1901 | { |
4ba9fb0a AH |
1902 | fprintf (file, "UNDEFINED"); |
1903 | return; | |
1904 | } | |
1905 | print_generic_expr (file, type ()); | |
1906 | fprintf (file, " "); | |
1907 | if (varying_p ()) | |
1908 | { | |
1909 | fprintf (file, "VARYING"); | |
1910 | return; | |
1911 | } | |
1912 | if (legacy_mode_p ()) | |
1913 | { | |
cca78449 | 1914 | fprintf (file, "%s[", (m_kind == VR_ANTI_RANGE) ? "~" : ""); |
4ba9fb0a | 1915 | dump_bound_with_infinite_markers (file, min ()); |
cca78449 | 1916 | fprintf (file, ", "); |
4ba9fb0a | 1917 | dump_bound_with_infinite_markers (file, max ()); |
cca78449 | 1918 | fprintf (file, "]"); |
4ba9fb0a | 1919 | return; |
cca78449 | 1920 | } |
4ba9fb0a | 1921 | for (unsigned i = 0; i < m_num_ranges; ++i) |
cca78449 | 1922 | { |
4ba9fb0a AH |
1923 | tree lb = m_base[i * 2]; |
1924 | tree ub = m_base[i * 2 + 1]; | |
1925 | fprintf (file, "["); | |
1926 | dump_bound_with_infinite_markers (file, lb); | |
1927 | fprintf (file, ", "); | |
1928 | dump_bound_with_infinite_markers (file, ub); | |
1929 | fprintf (file, "]"); | |
cca78449 | 1930 | } |
cca78449 AH |
1931 | } |
1932 | ||
1933 | void | |
4ba9fb0a | 1934 | dump_value_range (FILE *file, const irange *vr) |
cca78449 | 1935 | { |
4ba9fb0a | 1936 | vr->dump (file); |
cca78449 AH |
1937 | } |
1938 | ||
4ba9fb0a AH |
1939 | DEBUG_FUNCTION void |
1940 | debug (const irange *vr) | |
cca78449 | 1941 | { |
4ba9fb0a AH |
1942 | dump_value_range (stderr, vr); |
1943 | fprintf (stderr, "\n"); | |
1944 | } | |
1945 | ||
1946 | DEBUG_FUNCTION void | |
1947 | debug (const irange &vr) | |
1948 | { | |
1949 | debug (&vr); | |
cca78449 AH |
1950 | } |
1951 | ||
1952 | DEBUG_FUNCTION void | |
1953 | debug (const value_range *vr) | |
1954 | { | |
1955 | dump_value_range (stderr, vr); | |
4ba9fb0a | 1956 | fprintf (stderr, "\n"); |
cca78449 AH |
1957 | } |
1958 | ||
1959 | DEBUG_FUNCTION void | |
1960 | debug (const value_range &vr) | |
1961 | { | |
1962 | dump_value_range (stderr, &vr); | |
4ba9fb0a | 1963 | fprintf (stderr, "\n"); |
cca78449 AH |
1964 | } |
1965 | ||
1966 | /* Create two value-ranges in *VR0 and *VR1 from the anti-range *AR | |
1967 | so that *VR0 U *VR1 == *AR. Returns true if that is possible, | |
1968 | false otherwise. If *AR can be represented with a single range | |
1969 | *VR1 will be VR_UNDEFINED. */ | |
1970 | ||
1971 | bool | |
1972 | ranges_from_anti_range (const value_range *ar, | |
1973 | value_range *vr0, value_range *vr1) | |
1974 | { | |
1975 | tree type = ar->type (); | |
1976 | ||
1977 | vr0->set_undefined (); | |
1978 | vr1->set_undefined (); | |
1979 | ||
1980 | /* As a future improvement, we could handle ~[0, A] as: [-INF, -1] U | |
1981 | [A+1, +INF]. Not sure if this helps in practice, though. */ | |
1982 | ||
1983 | if (ar->kind () != VR_ANTI_RANGE | |
1984 | || TREE_CODE (ar->min ()) != INTEGER_CST | |
1985 | || TREE_CODE (ar->max ()) != INTEGER_CST | |
1986 | || !vrp_val_min (type) | |
1987 | || !vrp_val_max (type)) | |
1988 | return false; | |
1989 | ||
1990 | if (tree_int_cst_lt (vrp_val_min (type), ar->min ())) | |
1991 | vr0->set (vrp_val_min (type), | |
1992 | wide_int_to_tree (type, wi::to_wide (ar->min ()) - 1)); | |
1993 | if (tree_int_cst_lt (ar->max (), vrp_val_max (type))) | |
1994 | vr1->set (wide_int_to_tree (type, wi::to_wide (ar->max ()) + 1), | |
1995 | vrp_val_max (type)); | |
1996 | if (vr0->undefined_p ()) | |
1997 | { | |
1998 | *vr0 = *vr1; | |
1999 | vr1->set_undefined (); | |
2000 | } | |
2001 | ||
2002 | return !vr0->undefined_p (); | |
2003 | } | |
2004 | ||
2005 | bool | |
4ba9fb0a | 2006 | range_has_numeric_bounds_p (const irange *vr) |
cca78449 | 2007 | { |
4ba9fb0a | 2008 | return (!vr->undefined_p () |
cca78449 AH |
2009 | && TREE_CODE (vr->min ()) == INTEGER_CST |
2010 | && TREE_CODE (vr->max ()) == INTEGER_CST); | |
2011 | } | |
2012 | ||
cca78449 AH |
2013 | /* Return whether VAL is equal to the maximum value of its type. |
2014 | We can't do a simple equality comparison with TYPE_MAX_VALUE because | |
2015 | C typedefs and Ada subtypes can produce types whose TYPE_MAX_VALUE | |
2016 | is not == to the integer constant with the same value in the type. */ | |
2017 | ||
2018 | bool | |
2019 | vrp_val_is_max (const_tree val) | |
2020 | { | |
2021 | tree type_max = vrp_val_max (TREE_TYPE (val)); | |
2022 | return (val == type_max | |
2023 | || (type_max != NULL_TREE | |
2024 | && operand_equal_p (val, type_max, 0))); | |
2025 | } | |
2026 | ||
2027 | /* Return whether VAL is equal to the minimum value of its type. */ | |
2028 | ||
2029 | bool | |
2030 | vrp_val_is_min (const_tree val) | |
2031 | { | |
2032 | tree type_min = vrp_val_min (TREE_TYPE (val)); | |
2033 | return (val == type_min | |
2034 | || (type_min != NULL_TREE | |
2035 | && operand_equal_p (val, type_min, 0))); | |
2036 | } | |
2037 | ||
2038 | /* Return true, if VAL1 and VAL2 are equal values for VRP purposes. */ | |
2039 | ||
2040 | bool | |
2041 | vrp_operand_equal_p (const_tree val1, const_tree val2) | |
2042 | { | |
2043 | if (val1 == val2) | |
2044 | return true; | |
2045 | if (!val1 || !val2 || !operand_equal_p (val1, val2, 0)) | |
2046 | return false; | |
2047 | return true; | |
2048 | } | |
4ba9fb0a | 2049 | |
3c658587 AH |
2050 | // ?? These stubs are for ipa-prop.c which use a value_range in a |
2051 | // hash_traits. hash-traits.h defines an extern of gt_ggc_mx (T &) | |
2052 | // instead of picking up the gt_ggc_mx (T *) version. | |
2053 | void | |
2054 | gt_pch_nx (int_range<1> *&x) | |
2055 | { | |
2056 | return gt_pch_nx ((irange *) x); | |
2057 | } | |
2058 | ||
2059 | void | |
2060 | gt_ggc_mx (int_range<1> *&x) | |
2061 | { | |
2062 | return gt_ggc_mx ((irange *) x); | |
2063 | } | |
4ba9fb0a AH |
2064 | |
2065 | #define DEFINE_INT_RANGE_INSTANCE(N) \ | |
2066 | template int_range<N>::int_range(tree, tree, value_range_kind); \ | |
2067 | template int_range<N>::int_range(tree_node *, \ | |
2068 | const wide_int &, \ | |
2069 | const wide_int &, \ | |
2070 | value_range_kind); \ | |
2071 | template int_range<N>::int_range(tree); \ | |
2072 | template int_range<N>::int_range(const irange &); \ | |
2073 | template int_range<N>::int_range(const int_range &); \ | |
2074 | template int_range<N>& int_range<N>::operator= (const int_range &); | |
2075 | ||
2076 | DEFINE_INT_RANGE_INSTANCE(1) | |
2077 | DEFINE_INT_RANGE_INSTANCE(2) | |
2078 | DEFINE_INT_RANGE_INSTANCE(3) | |
2079 | DEFINE_INT_RANGE_INSTANCE(255) | |
b5cff0db AH |
2080 | |
2081 | #if CHECKING_P | |
2082 | #include "selftest.h" | |
2083 | ||
2084 | namespace selftest | |
2085 | { | |
2086 | #define INT(N) build_int_cst (integer_type_node, (N)) | |
2087 | #define UINT(N) build_int_cstu (unsigned_type_node, (N)) | |
2088 | #define UINT128(N) build_int_cstu (u128_type, (N)) | |
2089 | #define UCHAR(N) build_int_cstu (unsigned_char_type_node, (N)) | |
2090 | #define SCHAR(N) build_int_cst (signed_char_type_node, (N)) | |
2091 | ||
2092 | static int_range<3> | |
2093 | build_range3 (int a, int b, int c, int d, int e, int f) | |
2094 | { | |
2095 | int_range<3> i1 (INT (a), INT (b)); | |
2096 | int_range<3> i2 (INT (c), INT (d)); | |
2097 | int_range<3> i3 (INT (e), INT (f)); | |
2098 | i1.union_ (i2); | |
2099 | i1.union_ (i3); | |
2100 | return i1; | |
2101 | } | |
2102 | ||
2103 | static void | |
2104 | range_tests_irange3 () | |
2105 | { | |
2106 | typedef int_range<3> int_range3; | |
2107 | int_range3 r0, r1, r2; | |
2108 | int_range3 i1, i2, i3; | |
2109 | ||
2110 | // ([10,20] U [5,8]) U [1,3] ==> [1,3][5,8][10,20]. | |
2111 | r0 = int_range3 (INT (10), INT (20)); | |
2112 | r1 = int_range3 (INT (5), INT (8)); | |
2113 | r0.union_ (r1); | |
2114 | r1 = int_range3 (INT (1), INT (3)); | |
2115 | r0.union_ (r1); | |
2116 | ASSERT_TRUE (r0 == build_range3 (1, 3, 5, 8, 10, 20)); | |
2117 | ||
2118 | // [1,3][5,8][10,20] U [-5,0] => [-5,3][5,8][10,20]. | |
2119 | r1 = int_range3 (INT (-5), INT (0)); | |
2120 | r0.union_ (r1); | |
2121 | ASSERT_TRUE (r0 == build_range3 (-5, 3, 5, 8, 10, 20)); | |
2122 | ||
2123 | // [10,20][30,40] U [50,60] ==> [10,20][30,40][50,60]. | |
2124 | r1 = int_range3 (INT (50), INT (60)); | |
2125 | r0 = int_range3 (INT (10), INT (20)); | |
2126 | r0.union_ (int_range3 (INT (30), INT (40))); | |
2127 | r0.union_ (r1); | |
2128 | ASSERT_TRUE (r0 == build_range3 (10, 20, 30, 40, 50, 60)); | |
2129 | // [10,20][30,40][50,60] U [70, 80] ==> [10,20][30,40][50,60][70,80]. | |
2130 | r1 = int_range3 (INT (70), INT (80)); | |
2131 | r0.union_ (r1); | |
2132 | ||
2133 | r2 = build_range3 (10, 20, 30, 40, 50, 60); | |
2134 | r2.union_ (int_range3 (INT (70), INT (80))); | |
2135 | ASSERT_TRUE (r0 == r2); | |
2136 | ||
2137 | // [10,20][30,40][50,60] U [6,35] => [6,40][50,60]. | |
2138 | r0 = build_range3 (10, 20, 30, 40, 50, 60); | |
2139 | r1 = int_range3 (INT (6), INT (35)); | |
2140 | r0.union_ (r1); | |
2141 | r1 = int_range3 (INT (6), INT (40)); | |
2142 | r1.union_ (int_range3 (INT (50), INT (60))); | |
2143 | ASSERT_TRUE (r0 == r1); | |
2144 | ||
2145 | // [10,20][30,40][50,60] U [6,60] => [6,60]. | |
2146 | r0 = build_range3 (10, 20, 30, 40, 50, 60); | |
2147 | r1 = int_range3 (INT (6), INT (60)); | |
2148 | r0.union_ (r1); | |
2149 | ASSERT_TRUE (r0 == int_range3 (INT (6), INT (60))); | |
2150 | ||
2151 | // [10,20][30,40][50,60] U [6,70] => [6,70]. | |
2152 | r0 = build_range3 (10, 20, 30, 40, 50, 60); | |
2153 | r1 = int_range3 (INT (6), INT (70)); | |
2154 | r0.union_ (r1); | |
2155 | ASSERT_TRUE (r0 == int_range3 (INT (6), INT (70))); | |
2156 | ||
2157 | // [10,20][30,40][50,60] U [35,70] => [10,20][30,70]. | |
2158 | r0 = build_range3 (10, 20, 30, 40, 50, 60); | |
2159 | r1 = int_range3 (INT (35), INT (70)); | |
2160 | r0.union_ (r1); | |
2161 | r1 = int_range3 (INT (10), INT (20)); | |
2162 | r1.union_ (int_range3 (INT (30), INT (70))); | |
2163 | ASSERT_TRUE (r0 == r1); | |
2164 | ||
2165 | // [10,20][30,40][50,60] U [15,35] => [10,40][50,60]. | |
2166 | r0 = build_range3 (10, 20, 30, 40, 50, 60); | |
2167 | r1 = int_range3 (INT (15), INT (35)); | |
2168 | r0.union_ (r1); | |
2169 | r1 = int_range3 (INT (10), INT (40)); | |
2170 | r1.union_ (int_range3 (INT (50), INT (60))); | |
2171 | ASSERT_TRUE (r0 == r1); | |
2172 | ||
2173 | // [10,20][30,40][50,60] U [35,35] => [10,20][30,40][50,60]. | |
2174 | r0 = build_range3 (10, 20, 30, 40, 50, 60); | |
2175 | r1 = int_range3 (INT (35), INT (35)); | |
2176 | r0.union_ (r1); | |
2177 | ASSERT_TRUE (r0 == build_range3 (10, 20, 30, 40, 50, 60)); | |
2178 | } | |
2179 | ||
2180 | static void | |
2181 | range_tests_int_range_max () | |
2182 | { | |
2183 | int_range_max big; | |
2184 | unsigned int nrange; | |
2185 | ||
2186 | // Build a huge multi-range range. | |
2187 | for (nrange = 0; nrange < 50; ++nrange) | |
2188 | { | |
2189 | int_range<1> tmp (INT (nrange*10), INT (nrange*10 + 5)); | |
2190 | big.union_ (tmp); | |
2191 | } | |
2192 | ASSERT_TRUE (big.num_pairs () == nrange); | |
2193 | ||
2194 | // Verify that we can copy it without loosing precision. | |
2195 | int_range_max copy (big); | |
2196 | ASSERT_TRUE (copy.num_pairs () == nrange); | |
2197 | ||
2198 | // Inverting it should produce one more sub-range. | |
2199 | big.invert (); | |
2200 | ASSERT_TRUE (big.num_pairs () == nrange + 1); | |
2201 | ||
2202 | int_range<1> tmp (INT (5), INT (37)); | |
2203 | big.intersect (tmp); | |
2204 | ASSERT_TRUE (big.num_pairs () == 4); | |
2205 | ||
2206 | // Test that [10,10][20,20] does NOT contain 15. | |
2207 | { | |
2208 | int_range_max i1 (build_int_cst (integer_type_node, 10), | |
2209 | build_int_cst (integer_type_node, 10)); | |
2210 | int_range_max i2 (build_int_cst (integer_type_node, 20), | |
2211 | build_int_cst (integer_type_node, 20)); | |
2212 | i1.union_ (i2); | |
2213 | ASSERT_FALSE (i1.contains_p (build_int_cst (integer_type_node, 15))); | |
2214 | } | |
2215 | } | |
2216 | ||
2217 | static void | |
2218 | range_tests_legacy () | |
2219 | { | |
2220 | // Test truncating copy to int_range<1>. | |
2221 | int_range<3> big = build_range3 (10, 20, 30, 40, 50, 60); | |
2222 | int_range<1> small = big; | |
2223 | ASSERT_TRUE (small == int_range<1> (INT (10), INT (60))); | |
2224 | ||
2225 | // Test truncating copy to int_range<2>. | |
2226 | int_range<2> medium = big; | |
2227 | ASSERT_TRUE (!medium.undefined_p ()); | |
2228 | ||
2229 | // Test that a truncating copy of [MIN,20][22,40][80,MAX] | |
2230 | // ends up as a conservative anti-range of ~[21,21]. | |
2231 | big = int_range<3> (vrp_val_min (integer_type_node), INT (20)); | |
2232 | big.union_ (int_range<1> (INT (22), INT (40))); | |
2233 | big.union_ (int_range<1> (INT (80), vrp_val_max (integer_type_node))); | |
2234 | small = big; | |
2235 | ASSERT_TRUE (small == int_range<1> (INT (21), INT (21), VR_ANTI_RANGE)); | |
2236 | ||
2237 | // Copying a legacy symbolic to an int_range should normalize the | |
2238 | // symbolic at copy time. | |
2239 | { | |
2240 | tree ssa = make_ssa_name (integer_type_node); | |
2241 | value_range legacy_range (ssa, INT (25)); | |
2242 | int_range<2> copy = legacy_range; | |
2243 | ASSERT_TRUE (copy == int_range<2> (vrp_val_min (integer_type_node), | |
2244 | INT (25))); | |
2245 | ||
2246 | // Test that copying ~[abc_23, abc_23] to a multi-range yields varying. | |
2247 | legacy_range = value_range (ssa, ssa, VR_ANTI_RANGE); | |
2248 | copy = legacy_range; | |
2249 | ASSERT_TRUE (copy.varying_p ()); | |
2250 | } | |
ca8cc827 AH |
2251 | |
2252 | // VARYING of different sizes should not be equal. | |
41ddc5b0 AH |
2253 | tree big_type = build_nonstandard_integer_type (32, 1); |
2254 | tree small_type = build_nonstandard_integer_type (16, 1); | |
2255 | int_range_max r0 (big_type); | |
2256 | int_range_max r1 (small_type); | |
ca8cc827 | 2257 | ASSERT_TRUE (r0 != r1); |
41ddc5b0 AH |
2258 | value_range vr0 (big_type); |
2259 | int_range_max vr1 (small_type); | |
ca8cc827 | 2260 | ASSERT_TRUE (vr0 != vr1); |
b5cff0db AH |
2261 | } |
2262 | ||
3d3470e2 AH |
2263 | // Simulate -fstrict-enums where the domain of a type is less than the |
2264 | // underlying type. | |
2265 | ||
2266 | static void | |
2267 | range_tests_strict_enum () | |
2268 | { | |
2269 | // The enum can only hold [0, 3]. | |
2270 | tree rtype = copy_node (unsigned_type_node); | |
2271 | TYPE_MIN_VALUE (rtype) = build_int_cstu (rtype, 0); | |
2272 | TYPE_MAX_VALUE (rtype) = build_int_cstu (rtype, 3); | |
2273 | ||
2274 | // Test that even though vr1 covers the strict enum domain ([0, 3]), | |
2275 | // it does not cover the domain of the underlying type. | |
2276 | int_range<1> vr1 (build_int_cstu (rtype, 0), build_int_cstu (rtype, 1)); | |
2277 | int_range<1> vr2 (build_int_cstu (rtype, 2), build_int_cstu (rtype, 3)); | |
2278 | vr1.union_ (vr2); | |
2279 | ASSERT_TRUE (vr1 == int_range<1> (build_int_cstu (rtype, 0), | |
2280 | build_int_cstu (rtype, 3))); | |
2281 | ASSERT_FALSE (vr1.varying_p ()); | |
2282 | ||
2283 | // Test that copying to a multi-range does not change things. | |
2284 | int_range<2> ir1 (vr1); | |
2285 | ASSERT_TRUE (ir1 == vr1); | |
2286 | ASSERT_FALSE (ir1.varying_p ()); | |
2287 | ||
2288 | // The same test as above, but using TYPE_{MIN,MAX}_VALUE instead of [0,3]. | |
2289 | vr1 = int_range<1> (TYPE_MIN_VALUE (rtype), TYPE_MAX_VALUE (rtype)); | |
2290 | ir1 = vr1; | |
2291 | ASSERT_TRUE (ir1 == vr1); | |
2292 | ASSERT_FALSE (ir1.varying_p ()); | |
2293 | } | |
2294 | ||
b5cff0db AH |
2295 | static void |
2296 | range_tests_misc () | |
2297 | { | |
2298 | tree u128_type = build_nonstandard_integer_type (128, /*unsigned=*/1); | |
2299 | int_range<1> i1, i2, i3; | |
2300 | int_range<1> r0, r1, rold; | |
2301 | ||
2302 | // Test 1-bit signed integer union. | |
2303 | // [-1,-1] U [0,0] = VARYING. | |
2304 | tree one_bit_type = build_nonstandard_integer_type (1, 0); | |
2305 | tree one_bit_min = vrp_val_min (one_bit_type); | |
2306 | tree one_bit_max = vrp_val_max (one_bit_type); | |
2307 | { | |
2308 | int_range<2> min (one_bit_min, one_bit_min); | |
2309 | int_range<2> max (one_bit_max, one_bit_max); | |
2310 | max.union_ (min); | |
2311 | ASSERT_TRUE (max.varying_p ()); | |
2312 | } | |
2313 | ||
2314 | // Test inversion of 1-bit signed integers. | |
2315 | { | |
2316 | int_range<2> min (one_bit_min, one_bit_min); | |
2317 | int_range<2> max (one_bit_max, one_bit_max); | |
2318 | int_range<2> t; | |
2319 | t = min; | |
2320 | t.invert (); | |
2321 | ASSERT_TRUE (t == max); | |
2322 | t = max; | |
2323 | t.invert (); | |
2324 | ASSERT_TRUE (t == min); | |
2325 | } | |
2326 | ||
2327 | // Test that NOT(255) is [0..254] in 8-bit land. | |
2328 | int_range<1> not_255 (UCHAR (255), UCHAR (255), VR_ANTI_RANGE); | |
2329 | ASSERT_TRUE (not_255 == int_range<1> (UCHAR (0), UCHAR (254))); | |
2330 | ||
2331 | // Test that NOT(0) is [1..255] in 8-bit land. | |
2332 | int_range<1> not_zero = range_nonzero (unsigned_char_type_node); | |
2333 | ASSERT_TRUE (not_zero == int_range<1> (UCHAR (1), UCHAR (255))); | |
2334 | ||
2335 | // Check that [0,127][0x..ffffff80,0x..ffffff] | |
2336 | // => ~[128, 0x..ffffff7f]. | |
2337 | r0 = int_range<1> (UINT128 (0), UINT128 (127)); | |
2338 | tree high = build_minus_one_cst (u128_type); | |
2339 | // low = -1 - 127 => 0x..ffffff80. | |
2340 | tree low = fold_build2 (MINUS_EXPR, u128_type, high, UINT128(127)); | |
2341 | r1 = int_range<1> (low, high); // [0x..ffffff80, 0x..ffffffff] | |
2342 | // r0 = [0,127][0x..ffffff80,0x..fffffff]. | |
2343 | r0.union_ (r1); | |
2344 | // r1 = [128, 0x..ffffff7f]. | |
2345 | r1 = int_range<1> (UINT128(128), | |
2346 | fold_build2 (MINUS_EXPR, u128_type, | |
2347 | build_minus_one_cst (u128_type), | |
2348 | UINT128(128))); | |
2349 | r0.invert (); | |
2350 | ASSERT_TRUE (r0 == r1); | |
2351 | ||
2352 | r0.set_varying (integer_type_node); | |
2353 | tree minint = wide_int_to_tree (integer_type_node, r0.lower_bound ()); | |
2354 | tree maxint = wide_int_to_tree (integer_type_node, r0.upper_bound ()); | |
2355 | ||
2356 | r0.set_varying (short_integer_type_node); | |
2357 | ||
2358 | r0.set_varying (unsigned_type_node); | |
2359 | tree maxuint = wide_int_to_tree (unsigned_type_node, r0.upper_bound ()); | |
2360 | ||
2361 | // Check that ~[0,5] => [6,MAX] for unsigned int. | |
2362 | r0 = int_range<1> (UINT (0), UINT (5)); | |
2363 | r0.invert (); | |
2364 | ASSERT_TRUE (r0 == int_range<1> (UINT(6), maxuint)); | |
2365 | ||
2366 | // Check that ~[10,MAX] => [0,9] for unsigned int. | |
2367 | r0 = int_range<1> (UINT(10), maxuint); | |
2368 | r0.invert (); | |
2369 | ASSERT_TRUE (r0 == int_range<1> (UINT (0), UINT (9))); | |
2370 | ||
2371 | // Check that ~[0,5] => [6,MAX] for unsigned 128-bit numbers. | |
2372 | r0 = int_range<1> (UINT128 (0), UINT128 (5), VR_ANTI_RANGE); | |
2373 | r1 = int_range<1> (UINT128(6), build_minus_one_cst (u128_type)); | |
2374 | ASSERT_TRUE (r0 == r1); | |
2375 | ||
2376 | // Check that [~5] is really [-MIN,4][6,MAX]. | |
2377 | r0 = int_range<1> (INT (5), INT (5), VR_ANTI_RANGE); | |
2378 | r1 = int_range<1> (minint, INT (4)); | |
2379 | r1.union_ (int_range<1> (INT (6), maxint)); | |
2380 | ASSERT_FALSE (r1.undefined_p ()); | |
2381 | ASSERT_TRUE (r0 == r1); | |
2382 | ||
2383 | r1 = int_range<1> (INT (5), INT (5)); | |
2384 | int_range<1> r2 (r1); | |
2385 | ASSERT_TRUE (r1 == r2); | |
2386 | ||
2387 | r1 = int_range<1> (INT (5), INT (10)); | |
2388 | ||
2389 | r1 = int_range<1> (integer_type_node, | |
2390 | wi::to_wide (INT (5)), wi::to_wide (INT (10))); | |
2391 | ASSERT_TRUE (r1.contains_p (INT (7))); | |
2392 | ||
2393 | r1 = int_range<1> (SCHAR (0), SCHAR (20)); | |
2394 | ASSERT_TRUE (r1.contains_p (SCHAR(15))); | |
2395 | ASSERT_FALSE (r1.contains_p (SCHAR(300))); | |
2396 | ||
2397 | // NOT([10,20]) ==> [-MIN,9][21,MAX]. | |
2398 | r0 = r1 = int_range<1> (INT (10), INT (20)); | |
2399 | r2 = int_range<1> (minint, INT(9)); | |
2400 | r2.union_ (int_range<1> (INT(21), maxint)); | |
2401 | ASSERT_FALSE (r2.undefined_p ()); | |
2402 | r1.invert (); | |
2403 | ASSERT_TRUE (r1 == r2); | |
2404 | // Test that NOT(NOT(x)) == x. | |
2405 | r2.invert (); | |
2406 | ASSERT_TRUE (r0 == r2); | |
2407 | ||
2408 | // Test that booleans and their inverse work as expected. | |
2409 | r0 = range_zero (boolean_type_node); | |
2410 | ASSERT_TRUE (r0 == int_range<1> (build_zero_cst (boolean_type_node), | |
2411 | build_zero_cst (boolean_type_node))); | |
2412 | r0.invert (); | |
2413 | ASSERT_TRUE (r0 == int_range<1> (build_one_cst (boolean_type_node), | |
2414 | build_one_cst (boolean_type_node))); | |
2415 | ||
2416 | // Make sure NULL and non-NULL of pointer types work, and that | |
2417 | // inverses of them are consistent. | |
2418 | tree voidp = build_pointer_type (void_type_node); | |
2419 | r0 = range_zero (voidp); | |
2420 | r1 = r0; | |
2421 | r0.invert (); | |
2422 | r0.invert (); | |
2423 | ASSERT_TRUE (r0 == r1); | |
2424 | ||
2425 | // [10,20] U [15, 30] => [10, 30]. | |
2426 | r0 = int_range<1> (INT (10), INT (20)); | |
2427 | r1 = int_range<1> (INT (15), INT (30)); | |
2428 | r0.union_ (r1); | |
2429 | ASSERT_TRUE (r0 == int_range<1> (INT (10), INT (30))); | |
2430 | ||
2431 | // [15,40] U [] => [15,40]. | |
2432 | r0 = int_range<1> (INT (15), INT (40)); | |
2433 | r1.set_undefined (); | |
2434 | r0.union_ (r1); | |
2435 | ASSERT_TRUE (r0 == int_range<1> (INT (15), INT (40))); | |
2436 | ||
2437 | // [10,20] U [10,10] => [10,20]. | |
2438 | r0 = int_range<1> (INT (10), INT (20)); | |
2439 | r1 = int_range<1> (INT (10), INT (10)); | |
2440 | r0.union_ (r1); | |
2441 | ASSERT_TRUE (r0 == int_range<1> (INT (10), INT (20))); | |
2442 | ||
2443 | // [10,20] U [9,9] => [9,20]. | |
2444 | r0 = int_range<1> (INT (10), INT (20)); | |
2445 | r1 = int_range<1> (INT (9), INT (9)); | |
2446 | r0.union_ (r1); | |
2447 | ASSERT_TRUE (r0 == int_range<1> (INT (9), INT (20))); | |
2448 | ||
2449 | // [10,20] ^ [15,30] => [15,20]. | |
2450 | r0 = int_range<1> (INT (10), INT (20)); | |
2451 | r1 = int_range<1> (INT (15), INT (30)); | |
2452 | r0.intersect (r1); | |
2453 | ASSERT_TRUE (r0 == int_range<1> (INT (15), INT (20))); | |
2454 | ||
2455 | // Test the internal sanity of wide_int's wrt HWIs. | |
2456 | ASSERT_TRUE (wi::max_value (TYPE_PRECISION (boolean_type_node), | |
2457 | TYPE_SIGN (boolean_type_node)) | |
2458 | == wi::uhwi (1, TYPE_PRECISION (boolean_type_node))); | |
2459 | ||
2460 | // Test zero_p(). | |
2461 | r0 = int_range<1> (INT (0), INT (0)); | |
2462 | ASSERT_TRUE (r0.zero_p ()); | |
2463 | ||
2464 | // Test nonzero_p(). | |
2465 | r0 = int_range<1> (INT (0), INT (0)); | |
2466 | r0.invert (); | |
2467 | ASSERT_TRUE (r0.nonzero_p ()); | |
2468 | ||
2469 | // test legacy interaction | |
2470 | // r0 = ~[1,1] | |
2471 | r0 = int_range<1> (UINT (1), UINT (1), VR_ANTI_RANGE); | |
2472 | // r1 = ~[3,3] | |
2473 | r1 = int_range<1> (UINT (3), UINT (3), VR_ANTI_RANGE); | |
2474 | ||
2475 | // vv = [0,0][2,2][4, MAX] | |
2476 | int_range<3> vv = r0; | |
2477 | vv.intersect (r1); | |
2478 | ||
2479 | ASSERT_TRUE (vv.contains_p (UINT (2))); | |
2480 | ASSERT_TRUE (vv.num_pairs () == 3); | |
2481 | ||
2482 | // create r0 as legacy [1,1] | |
2483 | r0 = int_range<1> (UINT (1), UINT (1)); | |
2484 | // And union it with [0,0][2,2][4,MAX] multi range | |
2485 | r0.union_ (vv); | |
2486 | // The result should be [0,2][4,MAX], or ~[3,3] but it must contain 2 | |
2487 | ASSERT_TRUE (r0.contains_p (UINT (2))); | |
2488 | } | |
2489 | ||
2490 | void | |
2491 | range_tests () | |
2492 | { | |
2493 | range_tests_legacy (); | |
2494 | range_tests_irange3 (); | |
2495 | range_tests_int_range_max (); | |
3d3470e2 | 2496 | range_tests_strict_enum (); |
b5cff0db AH |
2497 | range_tests_misc (); |
2498 | } | |
2499 | ||
2500 | } // namespace selftest | |
2501 | ||
2502 | #endif // CHECKING_P |