* tree-ssa-dse.c (compute_trims): Avoid folding away undefined
[official-gcc.git] / gcc / wide-int-range.cc
bloba202b5fd503660590e20d4171802ae8158d36d84
1 /* Support routines for range operations on wide ints.
2 Copyright (C) 2018 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3, or (at your option)
9 any later version.
11 GCC is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 #include "config.h"
21 #include "system.h"
22 #include "coretypes.h"
23 #include "tree.h"
24 #include "fold-const.h"
25 #include "wide-int-range.h"
27 /* Wrapper around wide_int_binop that adjusts for overflow.
29 Return true if we can compute the result; i.e. if the operation
30 doesn't overflow or if the overflow is undefined. In the latter
31 case (if the operation overflows and overflow is undefined), then
32 adjust the result to be -INF or +INF depending on CODE, VAL1 and
33 VAL2. Return the value in *RES.
35 Return false for division by zero, for which the result is
36 indeterminate. */
38 static bool
39 wide_int_binop_overflow (wide_int &res,
40 enum tree_code code,
41 const wide_int &w0, const wide_int &w1,
42 signop sign, bool overflow_undefined)
44 wi::overflow_type overflow;
45 if (!wide_int_binop (res, code, w0, w1, sign, &overflow))
46 return false;
48 /* If the operation overflowed return -INF or +INF depending on the
49 operation and the combination of signs of the operands. */
50 if (overflow && overflow_undefined)
52 switch (code)
54 case MULT_EXPR:
55 /* For multiplication, the sign of the overflow is given
56 by the comparison of the signs of the operands. */
57 if (sign == UNSIGNED || w0.sign_mask () == w1.sign_mask ())
58 res = wi::max_value (w0.get_precision (), sign);
59 else
60 res = wi::min_value (w0.get_precision (), sign);
61 return true;
63 case TRUNC_DIV_EXPR:
64 case FLOOR_DIV_EXPR:
65 case CEIL_DIV_EXPR:
66 case EXACT_DIV_EXPR:
67 case ROUND_DIV_EXPR:
68 /* For division, the only case is -INF / -1 = +INF. */
69 res = wi::max_value (w0.get_precision (), sign);
70 return true;
72 default:
73 gcc_unreachable ();
76 return !overflow;
79 /* For range [LB, UB] compute two wide_int bit masks.
81 In the MAY_BE_NONZERO bit mask, if some bit is unset, it means that
82 for all numbers in the range the bit is 0, otherwise it might be 0
83 or 1.
85 In the MUST_BE_NONZERO bit mask, if some bit is set, it means that
86 for all numbers in the range the bit is 1, otherwise it might be 0
87 or 1. */
89 void
90 wide_int_range_set_zero_nonzero_bits (signop sign,
91 const wide_int &lb, const wide_int &ub,
92 wide_int &may_be_nonzero,
93 wide_int &must_be_nonzero)
95 may_be_nonzero = wi::minus_one (lb.get_precision ());
96 must_be_nonzero = wi::zero (lb.get_precision ());
98 if (wi::eq_p (lb, ub))
100 may_be_nonzero = lb;
101 must_be_nonzero = may_be_nonzero;
103 else if (wi::ge_p (lb, 0, sign) || wi::lt_p (ub, 0, sign))
105 wide_int xor_mask = lb ^ ub;
106 may_be_nonzero = lb | ub;
107 must_be_nonzero = lb & ub;
108 if (xor_mask != 0)
110 wide_int mask = wi::mask (wi::floor_log2 (xor_mask), false,
111 may_be_nonzero.get_precision ());
112 may_be_nonzero = may_be_nonzero | mask;
113 must_be_nonzero = wi::bit_and_not (must_be_nonzero, mask);
118 /* Order 2 sets of wide int ranges (w0/w1, w2/w3) and set MIN/MAX
119 accordingly. */
121 static void
122 wide_int_range_order_set (wide_int &min, wide_int &max,
123 wide_int &w0, wide_int &w1,
124 wide_int &w2, wide_int &w3,
125 signop sign)
127 /* Order pairs w0,w1 and w2,w3. */
128 if (wi::gt_p (w0, w1, sign))
129 std::swap (w0, w1);
130 if (wi::gt_p (w2, w3, sign))
131 std::swap (w2, w3);
133 /* Choose min and max from the ordered pairs. */
134 min = wi::min (w0, w2, sign);
135 max = wi::max (w1, w3, sign);
138 /* Calculate the cross product of two sets of ranges (VR0 and VR1) and
139 store the result in [RES_LB, RES_UB].
141 CODE is the operation to perform with sign SIGN.
143 OVERFLOW_UNDEFINED is set if overflow is undefined for the operation type.
145 Return TRUE if we were able to calculate the cross product. */
147 bool
148 wide_int_range_cross_product (wide_int &res_lb, wide_int &res_ub,
149 enum tree_code code, signop sign,
150 const wide_int &vr0_lb, const wide_int &vr0_ub,
151 const wide_int &vr1_lb, const wide_int &vr1_ub,
152 bool overflow_undefined)
154 wide_int cp1, cp2, cp3, cp4;
156 /* Compute the 4 cross operations, bailing if we get an overflow we
157 can't handle. */
159 if (!wide_int_binop_overflow (cp1, code, vr0_lb, vr1_lb, sign,
160 overflow_undefined))
161 return false;
163 if (wi::eq_p (vr0_lb, vr0_ub))
164 cp3 = cp1;
165 else if (!wide_int_binop_overflow (cp3, code, vr0_ub, vr1_lb, sign,
166 overflow_undefined))
167 return false;
169 if (wi::eq_p (vr1_lb, vr1_ub))
170 cp2 = cp1;
171 else if (!wide_int_binop_overflow (cp2, code, vr0_lb, vr1_ub, sign,
172 overflow_undefined))
173 return false;
175 if (wi::eq_p (vr0_lb, vr0_ub))
176 cp4 = cp2;
177 else if (!wide_int_binop_overflow (cp4, code, vr0_ub, vr1_ub, sign,
178 overflow_undefined))
179 return false;
181 wide_int_range_order_set (res_lb, res_ub, cp1, cp2, cp3, cp4, sign);
182 return true;
185 /* Multiply two ranges when TYPE_OVERFLOW_WRAPS:
187 [RES_LB, RES_UB] = [MIN0, MAX0] * [MIN1, MAX1]
189 This is basically fancy code so we don't drop to varying with an
190 unsigned [-3,-1]*[-3,-1].
192 Return TRUE if we were able to perform the operation. */
194 bool
195 wide_int_range_mult_wrapping (wide_int &res_lb,
196 wide_int &res_ub,
197 signop sign,
198 unsigned prec,
199 const wide_int &min0_,
200 const wide_int &max0_,
201 const wide_int &min1_,
202 const wide_int &max1_)
204 /* This test requires 2*prec bits if both operands are signed and
205 2*prec + 2 bits if either is not. Therefore, extend the values
206 using the sign of the result to PREC2. From here on out,
207 everthing is just signed math no matter what the input types
208 were. */
209 widest2_int min0 = widest2_int::from (min0_, sign);
210 widest2_int max0 = widest2_int::from (max0_, sign);
211 widest2_int min1 = widest2_int::from (min1_, sign);
212 widest2_int max1 = widest2_int::from (max1_, sign);
213 widest2_int sizem1 = wi::mask <widest2_int> (prec, false);
214 widest2_int size = sizem1 + 1;
216 /* Canonicalize the intervals. */
217 if (sign == UNSIGNED)
219 if (wi::ltu_p (size, min0 + max0))
221 min0 -= size;
222 max0 -= size;
225 if (wi::ltu_p (size, min1 + max1))
227 min1 -= size;
228 max1 -= size;
232 widest2_int prod0 = min0 * min1;
233 widest2_int prod1 = min0 * max1;
234 widest2_int prod2 = max0 * min1;
235 widest2_int prod3 = max0 * max1;
237 /* Sort the 4 products so that min is in prod0 and max is in
238 prod3. */
239 /* min0min1 > max0max1 */
240 if (prod0 > prod3)
241 std::swap (prod0, prod3);
243 /* min0max1 > max0min1 */
244 if (prod1 > prod2)
245 std::swap (prod1, prod2);
247 if (prod0 > prod1)
248 std::swap (prod0, prod1);
250 if (prod2 > prod3)
251 std::swap (prod2, prod3);
253 /* diff = max - min. */
254 prod2 = prod3 - prod0;
255 if (wi::geu_p (prod2, sizem1))
256 /* The range covers all values. */
257 return false;
259 res_lb = wide_int::from (prod0, prec, sign);
260 res_ub = wide_int::from (prod3, prec, sign);
261 return true;
264 /* Perform multiplicative operation CODE on two ranges:
266 [RES_LB, RES_UB] = [VR0_LB, VR0_UB] .CODE. [VR1_LB, VR1_LB]
268 Return TRUE if we were able to perform the operation.
270 NOTE: If code is MULT_EXPR and TYPE_OVERFLOW_WRAPS, the resulting
271 range must be canonicalized by the caller because its components
272 may be swapped. */
274 bool
275 wide_int_range_multiplicative_op (wide_int &res_lb, wide_int &res_ub,
276 enum tree_code code,
277 signop sign,
278 unsigned prec,
279 const wide_int &vr0_lb,
280 const wide_int &vr0_ub,
281 const wide_int &vr1_lb,
282 const wide_int &vr1_ub,
283 bool overflow_undefined,
284 bool overflow_wraps)
286 /* Multiplications, divisions and shifts are a bit tricky to handle,
287 depending on the mix of signs we have in the two ranges, we
288 need to operate on different values to get the minimum and
289 maximum values for the new range. One approach is to figure
290 out all the variations of range combinations and do the
291 operations.
293 However, this involves several calls to compare_values and it
294 is pretty convoluted. It's simpler to do the 4 operations
295 (MIN0 OP MIN1, MIN0 OP MAX1, MAX0 OP MIN1 and MAX0 OP MAX0 OP
296 MAX1) and then figure the smallest and largest values to form
297 the new range. */
298 if (code == MULT_EXPR && overflow_wraps)
299 return wide_int_range_mult_wrapping (res_lb, res_ub,
300 sign, prec,
301 vr0_lb, vr0_ub, vr1_lb, vr1_ub);
302 return wide_int_range_cross_product (res_lb, res_ub,
303 code, sign,
304 vr0_lb, vr0_ub, vr1_lb, vr1_ub,
305 overflow_undefined);
308 /* Perform a left shift operation on two ranges:
310 [RES_LB, RES_UB] = [VR0_LB, VR0_UB] << [VR1_LB, VR1_LB]
312 Return TRUE if we were able to perform the operation.
314 NOTE: The resulting range must be canonicalized by the caller
315 because its contents components may be swapped. */
317 bool
318 wide_int_range_lshift (wide_int &res_lb, wide_int &res_ub,
319 signop sign, unsigned prec,
320 const wide_int &vr0_lb, const wide_int &vr0_ub,
321 const wide_int &vr1_lb, const wide_int &vr1_ub,
322 bool overflow_undefined, bool overflow_wraps)
324 /* Transform left shifts by constants into multiplies. */
325 if (wi::eq_p (vr1_lb, vr1_ub))
327 unsigned shift = vr1_ub.to_uhwi ();
328 wide_int tmp = wi::set_bit_in_zero (shift, prec);
329 return wide_int_range_multiplicative_op (res_lb, res_ub,
330 MULT_EXPR, sign, prec,
331 vr0_lb, vr0_ub, tmp, tmp,
332 overflow_undefined,
333 /*overflow_wraps=*/true);
336 int overflow_pos = prec;
337 if (sign == SIGNED)
338 overflow_pos -= 1;
339 int bound_shift = overflow_pos - vr1_ub.to_shwi ();
340 /* If bound_shift == HOST_BITS_PER_WIDE_INT, the llshift can
341 overflow. However, for that to happen, vr1.max needs to be
342 zero, which means vr1 is a singleton range of zero, which
343 means it should be handled by the previous LSHIFT_EXPR
344 if-clause. */
345 wide_int bound = wi::set_bit_in_zero (bound_shift, prec);
346 wide_int complement = ~(bound - 1);
347 wide_int low_bound, high_bound;
348 bool in_bounds = false;
349 if (sign == UNSIGNED)
351 low_bound = bound;
352 high_bound = complement;
353 if (wi::ltu_p (vr0_ub, low_bound))
355 /* [5, 6] << [1, 2] == [10, 24]. */
356 /* We're shifting out only zeroes, the value increases
357 monotonically. */
358 in_bounds = true;
360 else if (wi::ltu_p (high_bound, vr0_lb))
362 /* [0xffffff00, 0xffffffff] << [1, 2]
363 == [0xfffffc00, 0xfffffffe]. */
364 /* We're shifting out only ones, the value decreases
365 monotonically. */
366 in_bounds = true;
369 else
371 /* [-1, 1] << [1, 2] == [-4, 4]. */
372 low_bound = complement;
373 high_bound = bound;
374 if (wi::lts_p (vr0_ub, high_bound)
375 && wi::lts_p (low_bound, vr0_lb))
377 /* For non-negative numbers, we're shifting out only
378 zeroes, the value increases monotonically.
379 For negative numbers, we're shifting out only ones, the
380 value decreases monotomically. */
381 in_bounds = true;
384 if (in_bounds)
385 return wide_int_range_multiplicative_op (res_lb, res_ub,
386 LSHIFT_EXPR, sign, prec,
387 vr0_lb, vr0_ub,
388 vr1_lb, vr1_ub,
389 overflow_undefined,
390 overflow_wraps);
391 return false;
394 /* Return TRUE if a bit operation on two ranges can be easily
395 optimized in terms of a mask.
397 Basically, for BIT_AND_EXPR or BIT_IOR_EXPR see if we can optimize:
399 [LB, UB] op Z
400 into:
401 [LB op Z, UB op Z]
403 It is up to the caller to perform the actual folding above. */
405 bool
406 wide_int_range_can_optimize_bit_op (tree_code code,
407 const wide_int &lb, const wide_int &ub,
408 const wide_int &mask)
411 if (code != BIT_AND_EXPR && code != BIT_IOR_EXPR)
412 return false;
413 /* If Z is a constant which (for op | its bitwise not) has n
414 consecutive least significant bits cleared followed by m 1
415 consecutive bits set immediately above it and either
416 m + n == precision, or (x >> (m + n)) == (y >> (m + n)).
418 The least significant n bits of all the values in the range are
419 cleared or set, the m bits above it are preserved and any bits
420 above these are required to be the same for all values in the
421 range. */
423 wide_int w = mask;
424 int m = 0, n = 0;
425 if (code == BIT_IOR_EXPR)
426 w = ~w;
427 if (wi::eq_p (w, 0))
428 n = w.get_precision ();
429 else
431 n = wi::ctz (w);
432 w = ~(w | wi::mask (n, false, w.get_precision ()));
433 if (wi::eq_p (w, 0))
434 m = w.get_precision () - n;
435 else
436 m = wi::ctz (w) - n;
438 wide_int new_mask = wi::mask (m + n, true, w.get_precision ());
439 if ((new_mask & lb) == (new_mask & ub))
440 return true;
442 return false;
445 /* Calculate the XOR of two ranges and store the result in [WMIN,WMAX].
446 The two input ranges are described by their MUST_BE_NONZERO and
447 MAY_BE_NONZERO bit masks.
449 Return TRUE if we were able to successfully calculate the new range. */
451 bool
452 wide_int_range_bit_xor (wide_int &wmin, wide_int &wmax,
453 signop sign,
454 unsigned prec,
455 const wide_int &must_be_nonzero0,
456 const wide_int &may_be_nonzero0,
457 const wide_int &must_be_nonzero1,
458 const wide_int &may_be_nonzero1)
460 wide_int result_zero_bits = ((must_be_nonzero0 & must_be_nonzero1)
461 | ~(may_be_nonzero0 | may_be_nonzero1));
462 wide_int result_one_bits
463 = (wi::bit_and_not (must_be_nonzero0, may_be_nonzero1)
464 | wi::bit_and_not (must_be_nonzero1, may_be_nonzero0));
465 wmax = ~result_zero_bits;
466 wmin = result_one_bits;
467 /* If the range has all positive or all negative values, the result
468 is better than VARYING. */
469 if (wi::lt_p (wmin, 0, sign) || wi::ge_p (wmax, 0, sign))
470 return true;
471 wmin = wi::min_value (prec, sign);
472 wmax = wi::max_value (prec, sign);
473 return false;
476 /* Calculate the IOR of two ranges and store the result in [WMIN,WMAX].
477 Return TRUE if we were able to successfully calculate the new range. */
479 bool
480 wide_int_range_bit_ior (wide_int &wmin, wide_int &wmax,
481 signop sign,
482 const wide_int &vr0_min,
483 const wide_int &vr0_max,
484 const wide_int &vr1_min,
485 const wide_int &vr1_max,
486 const wide_int &must_be_nonzero0,
487 const wide_int &may_be_nonzero0,
488 const wide_int &must_be_nonzero1,
489 const wide_int &may_be_nonzero1)
491 wmin = must_be_nonzero0 | must_be_nonzero1;
492 wmax = may_be_nonzero0 | may_be_nonzero1;
493 /* If the input ranges contain only positive values we can
494 truncate the minimum of the result range to the maximum
495 of the input range minima. */
496 if (wi::ge_p (vr0_min, 0, sign)
497 && wi::ge_p (vr1_min, 0, sign))
499 wmin = wi::max (wmin, vr0_min, sign);
500 wmin = wi::max (wmin, vr1_min, sign);
502 /* If either input range contains only negative values
503 we can truncate the minimum of the result range to the
504 respective minimum range. */
505 if (wi::lt_p (vr0_max, 0, sign))
506 wmin = wi::max (wmin, vr0_min, sign);
507 if (wi::lt_p (vr1_max, 0, sign))
508 wmin = wi::max (wmin, vr1_min, sign);
509 /* If the limits got swapped around, indicate error so we can adjust
510 the range to VARYING. */
511 if (wi::gt_p (wmin, wmax,sign))
512 return false;
513 return true;
516 /* Calculate the bitwise AND of two ranges and store the result in [WMIN,WMAX].
517 Return TRUE if we were able to successfully calculate the new range. */
519 bool
520 wide_int_range_bit_and (wide_int &wmin, wide_int &wmax,
521 signop sign,
522 unsigned prec,
523 const wide_int &vr0_min,
524 const wide_int &vr0_max,
525 const wide_int &vr1_min,
526 const wide_int &vr1_max,
527 const wide_int &must_be_nonzero0,
528 const wide_int &may_be_nonzero0,
529 const wide_int &must_be_nonzero1,
530 const wide_int &may_be_nonzero1)
532 wmin = must_be_nonzero0 & must_be_nonzero1;
533 wmax = may_be_nonzero0 & may_be_nonzero1;
534 /* If both input ranges contain only negative values we can
535 truncate the result range maximum to the minimum of the
536 input range maxima. */
537 if (wi::lt_p (vr0_max, 0, sign) && wi::lt_p (vr1_max, 0, sign))
539 wmax = wi::min (wmax, vr0_max, sign);
540 wmax = wi::min (wmax, vr1_max, sign);
542 /* If either input range contains only non-negative values
543 we can truncate the result range maximum to the respective
544 maximum of the input range. */
545 if (wi::ge_p (vr0_min, 0, sign))
546 wmax = wi::min (wmax, vr0_max, sign);
547 if (wi::ge_p (vr1_min, 0, sign))
548 wmax = wi::min (wmax, vr1_max, sign);
549 /* PR68217: In case of signed & sign-bit-CST should
550 result in [-INF, 0] instead of [-INF, INF]. */
551 if (wi::gt_p (wmin, wmax, sign))
553 wide_int sign_bit = wi::set_bit_in_zero (prec - 1, prec);
554 if (sign == SIGNED
555 && ((wi::eq_p (vr0_min, vr0_max)
556 && !wi::cmps (vr0_min, sign_bit))
557 || (wi::eq_p (vr1_min, vr1_max)
558 && !wi::cmps (vr1_min, sign_bit))))
560 wmin = wi::min_value (prec, sign);
561 wmax = wi::zero (prec);
564 /* If the limits got swapped around, indicate error so we can adjust
565 the range to VARYING. */
566 if (wi::gt_p (wmin, wmax,sign))
567 return false;
568 return true;
571 /* Calculate TRUNC_MOD_EXPR on two ranges and store the result in
572 [WMIN,WMAX]. */
574 void
575 wide_int_range_trunc_mod (wide_int &wmin, wide_int &wmax,
576 signop sign,
577 unsigned prec,
578 const wide_int &vr0_min,
579 const wide_int &vr0_max,
580 const wide_int &vr1_min,
581 const wide_int &vr1_max)
583 wide_int tmp;
585 /* ABS (A % B) < ABS (B) and either
586 0 <= A % B <= A or A <= A % B <= 0. */
587 wmax = vr1_max - 1;
588 if (sign == SIGNED)
590 tmp = -1 - vr1_min;
591 wmax = wi::smax (wmax, tmp);
594 if (sign == UNSIGNED)
595 wmin = wi::zero (prec);
596 else
598 wmin = -wmax;
599 tmp = vr0_min;
600 if (wi::gts_p (tmp, 0))
601 tmp = wi::zero (prec);
602 wmin = wi::smax (wmin, tmp);
604 tmp = vr0_max;
605 if (sign == SIGNED && wi::neg_p (tmp))
606 tmp = wi::zero (prec);
607 wmax = wi::min (wmax, tmp, sign);
610 /* Calculate ABS_EXPR on a range and store the result in [MIN, MAX]. */
612 bool
613 wide_int_range_abs (wide_int &min, wide_int &max,
614 signop sign, unsigned prec,
615 const wide_int &vr0_min, const wide_int &vr0_max,
616 bool overflow_undefined)
618 /* Pass through VR0 the easy cases. */
619 if (sign == UNSIGNED || wi::ge_p (vr0_min, 0, sign))
621 min = vr0_min;
622 max = vr0_max;
623 return true;
626 /* -TYPE_MIN_VALUE = TYPE_MIN_VALUE with flag_wrapv so we can't get a
627 useful range. */
628 wide_int min_value = wi::min_value (prec, sign);
629 wide_int max_value = wi::max_value (prec, sign);
630 if (!overflow_undefined && wi::eq_p (vr0_min, min_value))
631 return false;
633 /* ABS_EXPR may flip the range around, if the original range
634 included negative values. */
635 if (wi::eq_p (vr0_min, min_value))
636 min = max_value;
637 else
638 min = wi::abs (vr0_min);
639 if (wi::eq_p (vr0_max, min_value))
640 max = max_value;
641 else
642 max = wi::abs (vr0_max);
644 /* If the range contains zero then we know that the minimum value in the
645 range will be zero. */
646 if (wi::le_p (vr0_min, 0, sign) && wi::ge_p (vr0_max, 0, sign))
648 if (wi::gt_p (min, max, sign))
649 max = min;
650 min = wi::zero (prec);
652 else
654 /* If the range was reversed, swap MIN and MAX. */
655 if (wi::gt_p (min, max, sign))
656 std::swap (min, max);
659 /* If the new range has its limits swapped around (MIN > MAX), then
660 the operation caused one of them to wrap around, mark the new
661 range VARYING. */
662 if (wi::gt_p (min, max, sign))
663 return false;
664 return true;