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