]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/range-op.cc
Fix profile update in tree_transform_and_unroll_loop
[thirdparty/gcc.git] / gcc / range-op.cc
CommitLineData
38a73435 1/* Code for range operators.
aeee4812 2 Copyright (C) 2017-2023 Free Software Foundation, Inc.
38a73435
AH
3 Contributed by Andrew MacLeod <amacleod@redhat.com>
4 and Aldy Hernandez <aldyh@redhat.com>.
5
6This file is part of GCC.
7
8GCC is free software; you can redistribute it and/or modify
9it under the terms of the GNU General Public License as published by
10the Free Software Foundation; either version 3, or (at your option)
11any later version.
12
13GCC is distributed in the hope that it will be useful,
14but WITHOUT ANY WARRANTY; without even the implied warranty of
15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16GNU General Public License for more details.
17
18You should have received a copy of the GNU General Public License
19along 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 "insn-codes.h"
27#include "rtl.h"
28#include "tree.h"
29#include "gimple.h"
30#include "cfghooks.h"
31#include "tree-pass.h"
32#include "ssa.h"
33#include "optabs-tree.h"
34#include "gimple-pretty-print.h"
35#include "diagnostic-core.h"
36#include "flags.h"
37#include "fold-const.h"
38#include "stor-layout.h"
39#include "calls.h"
40#include "cfganal.h"
ba206889 41#include "gimple-iterator.h"
38a73435
AH
42#include "gimple-fold.h"
43#include "tree-eh.h"
38a73435
AH
44#include "gimple-walk.h"
45#include "tree-cfg.h"
46#include "wide-int.h"
80dd13f5 47#include "value-relation.h"
38a73435 48#include "range-op.h"
b74dd1bb 49#include "tree-ssa-ccp.h"
07767389 50#include "range-op-mixed.h"
b74dd1bb 51
2dbf1e61
AM
52// Instantiate the operators which apply to multiple types here.
53
54operator_equal op_equal;
eb29c3e1 55operator_not_equal op_not_equal;
5b079541 56operator_lt op_lt;
d251d14c 57operator_le op_le;
f544e7e8 58operator_gt op_gt;
a0a8f1c7 59operator_ge op_ge;
b073d8af 60operator_identity op_ident;
4f0ac5a5 61operator_cst op_cst;
6a4ac393 62operator_cast op_cast;
29dbd7ef 63operator_plus op_plus;
a1aaaff3 64operator_abs op_abs;
d5818a36 65operator_minus op_minus;
56518bef 66operator_negate op_negate;
a13c4440 67operator_mult op_mult;
443485b3 68operator_addr_expr op_addr;
39636a09 69operator_bitwise_not op_bitwise_not;
af52b862 70operator_bitwise_xor op_bitwise_xor;
0965275e 71operator_bitwise_and op_bitwise_and;
b23d6b95 72operator_bitwise_or op_bitwise_or;
b08b9825 73operator_min op_min;
f0278eb0 74operator_max op_max;
2dbf1e61 75
1c0aae69
AM
76// Instantaite a range operator table.
77range_op_table operator_table;
78
07767389 79// Invoke the initialization routines for each class of range.
cd9c7f89 80
1c0aae69 81range_op_table::range_op_table ()
cd9c7f89 82{
07767389
AM
83 initialize_integral_ops ();
84 initialize_pointer_ops ();
85 initialize_float_ops ();
2dbf1e61
AM
86
87 set (EQ_EXPR, op_equal);
eb29c3e1 88 set (NE_EXPR, op_not_equal);
5b079541 89 set (LT_EXPR, op_lt);
d251d14c 90 set (LE_EXPR, op_le);
f544e7e8 91 set (GT_EXPR, op_gt);
a0a8f1c7 92 set (GE_EXPR, op_ge);
b073d8af
AM
93 set (SSA_NAME, op_ident);
94 set (PAREN_EXPR, op_ident);
95 set (OBJ_TYPE_REF, op_ident);
4f0ac5a5
AM
96 set (REAL_CST, op_cst);
97 set (INTEGER_CST, op_cst);
6a4ac393
AM
98 set (NOP_EXPR, op_cast);
99 set (CONVERT_EXPR, op_cast);
29dbd7ef 100 set (PLUS_EXPR, op_plus);
a1aaaff3 101 set (ABS_EXPR, op_abs);
d5818a36 102 set (MINUS_EXPR, op_minus);
56518bef 103 set (NEGATE_EXPR, op_negate);
a13c4440 104 set (MULT_EXPR, op_mult);
443485b3
AM
105
106 // Occur in both integer and pointer tables, but currently share
39636a09 107 // integral implementation.
443485b3 108 set (ADDR_EXPR, op_addr);
39636a09 109 set (BIT_NOT_EXPR, op_bitwise_not);
af52b862 110 set (BIT_XOR_EXPR, op_bitwise_xor);
0965275e
AM
111
112 // These are in both integer and pointer tables, but pointer has a different
8e0f292f
AM
113 // implementation.
114 // If commented out, there is a hybrid version in range-op-ptr.cc which
115 // is used until there is a pointer range class. Then we can simply
116 // uncomment the operator here and use the unified version.
117
af5e7f06
AM
118 // set (BIT_AND_EXPR, op_bitwise_and);
119 // set (BIT_IOR_EXPR, op_bitwise_or);
73cbf402 120 // set (MIN_EXPR, op_min);
110c1f8d 121 // set (MAX_EXPR, op_max);
07767389 122}
cd9c7f89 123
1b1de36a
AM
124// Instantiate a default range operator for opcodes with no entry.
125
126range_operator default_operator;
127
128// Create a default range_op_handler.
129
cd9c7f89
AM
130range_op_handler::range_op_handler ()
131{
1b1de36a 132 m_operator = &default_operator;
cd9c7f89
AM
133}
134
1b1de36a
AM
135// Create a range_op_handler for CODE. Use a default operatoer if CODE
136// does not have an entry.
07767389 137
5410b07a 138range_op_handler::range_op_handler (unsigned code)
07767389 139{
1c0aae69 140 m_operator = operator_table[code];
1b1de36a
AM
141 if (!m_operator)
142 m_operator = &default_operator;
143}
144
145// Return TRUE if this handler has a non-default operator.
146
147range_op_handler::operator bool () const
148{
149 return m_operator != &default_operator;
150}
151
152// Return a pointer to the range operator assocaited with this handler.
153// If it is a default operator, return NULL.
154// This is the equivalent of indexing the range table.
155
156range_operator *
157range_op_handler::range_op () const
158{
159 if (m_operator != &default_operator)
160 return m_operator;
161 return NULL;
07767389
AM
162}
163
cd9c7f89
AM
164// Create a dispatch pattern for value range discriminators LHS, OP1, and OP2.
165// This is used to produce a unique value for each dispatch pattern. Shift
166// values are based on the size of the m_discriminator field in value_range.h.
167
168constexpr unsigned
169dispatch_trio (unsigned lhs, unsigned op1, unsigned op2)
170{
171 return ((lhs << 8) + (op1 << 4) + (op2));
172}
173
174// These are the supported dispatch patterns. These map to the parameter list
175// of the routines in range_operator. Note the last 3 characters are
176// shorthand for the LHS, OP1, and OP2 range discriminator class.
177
178const unsigned RO_III = dispatch_trio (VR_IRANGE, VR_IRANGE, VR_IRANGE);
179const unsigned RO_IFI = dispatch_trio (VR_IRANGE, VR_FRANGE, VR_IRANGE);
180const unsigned RO_IFF = dispatch_trio (VR_IRANGE, VR_FRANGE, VR_FRANGE);
181const unsigned RO_FFF = dispatch_trio (VR_FRANGE, VR_FRANGE, VR_FRANGE);
182const unsigned RO_FIF = dispatch_trio (VR_FRANGE, VR_IRANGE, VR_FRANGE);
183const unsigned RO_FII = dispatch_trio (VR_FRANGE, VR_IRANGE, VR_IRANGE);
184
185// Return a dispatch value for parameter types LHS, OP1 and OP2.
186
187unsigned
188range_op_handler::dispatch_kind (const vrange &lhs, const vrange &op1,
189 const vrange& op2) const
190{
191 return dispatch_trio (lhs.m_discriminator, op1.m_discriminator,
192 op2.m_discriminator);
193}
194
195// Dispatch a call to fold_range based on the types of R, LH and RH.
196
197bool
198range_op_handler::fold_range (vrange &r, tree type,
199 const vrange &lh,
200 const vrange &rh,
201 relation_trio rel) const
202{
203 gcc_checking_assert (m_operator);
204 switch (dispatch_kind (r, lh, rh))
205 {
206 case RO_III:
207 return m_operator->fold_range (as_a <irange> (r), type,
208 as_a <irange> (lh),
209 as_a <irange> (rh), rel);
210 case RO_IFI:
211 return m_operator->fold_range (as_a <irange> (r), type,
212 as_a <frange> (lh),
213 as_a <irange> (rh), rel);
214 case RO_IFF:
215 return m_operator->fold_range (as_a <irange> (r), type,
216 as_a <frange> (lh),
217 as_a <frange> (rh), rel);
218 case RO_FFF:
219 return m_operator->fold_range (as_a <frange> (r), type,
220 as_a <frange> (lh),
221 as_a <frange> (rh), rel);
0ddc8c78
AM
222 case RO_FII:
223 return m_operator->fold_range (as_a <frange> (r), type,
224 as_a <irange> (lh),
225 as_a <irange> (rh), rel);
cd9c7f89
AM
226 default:
227 return false;
228 }
229}
230
231// Dispatch a call to op1_range based on the types of R, LHS and OP2.
232
233bool
234range_op_handler::op1_range (vrange &r, tree type,
235 const vrange &lhs,
236 const vrange &op2,
237 relation_trio rel) const
238{
239 gcc_checking_assert (m_operator);
240
241 if (lhs.undefined_p ())
242 return false;
243 switch (dispatch_kind (r, lhs, op2))
244 {
245 case RO_III:
246 return m_operator->op1_range (as_a <irange> (r), type,
247 as_a <irange> (lhs),
248 as_a <irange> (op2), rel);
249 case RO_FIF:
250 return m_operator->op1_range (as_a <frange> (r), type,
251 as_a <irange> (lhs),
252 as_a <frange> (op2), rel);
253 case RO_FFF:
254 return m_operator->op1_range (as_a <frange> (r), type,
255 as_a <frange> (lhs),
256 as_a <frange> (op2), rel);
257 default:
258 return false;
259 }
260}
261
262// Dispatch a call to op2_range based on the types of R, LHS and OP1.
263
264bool
265range_op_handler::op2_range (vrange &r, tree type,
266 const vrange &lhs,
267 const vrange &op1,
268 relation_trio rel) const
269{
270 gcc_checking_assert (m_operator);
271 if (lhs.undefined_p ())
272 return false;
273
274 switch (dispatch_kind (r, lhs, op1))
275 {
276 case RO_III:
277 return m_operator->op2_range (as_a <irange> (r), type,
278 as_a <irange> (lhs),
279 as_a <irange> (op1), rel);
280 case RO_FIF:
281 return m_operator->op2_range (as_a <frange> (r), type,
282 as_a <irange> (lhs),
283 as_a <frange> (op1), rel);
284 case RO_FFF:
285 return m_operator->op2_range (as_a <frange> (r), type,
286 as_a <frange> (lhs),
287 as_a <frange> (op1), rel);
288 default:
289 return false;
290 }
291}
292
293// Dispatch a call to lhs_op1_relation based on the types of LHS, OP1 and OP2.
294
295relation_kind
296range_op_handler::lhs_op1_relation (const vrange &lhs,
297 const vrange &op1,
298 const vrange &op2,
299 relation_kind rel) const
300{
301 gcc_checking_assert (m_operator);
302
303 switch (dispatch_kind (lhs, op1, op2))
304 {
305 case RO_III:
306 return m_operator->lhs_op1_relation (as_a <irange> (lhs),
307 as_a <irange> (op1),
308 as_a <irange> (op2), rel);
309 case RO_IFF:
310 return m_operator->lhs_op1_relation (as_a <irange> (lhs),
311 as_a <frange> (op1),
312 as_a <frange> (op2), rel);
313 case RO_FFF:
314 return m_operator->lhs_op1_relation (as_a <frange> (lhs),
315 as_a <frange> (op1),
316 as_a <frange> (op2), rel);
317 default:
318 return VREL_VARYING;
319 }
320}
321
322// Dispatch a call to lhs_op2_relation based on the types of LHS, OP1 and OP2.
323
324relation_kind
325range_op_handler::lhs_op2_relation (const vrange &lhs,
326 const vrange &op1,
327 const vrange &op2,
328 relation_kind rel) const
329{
330 gcc_checking_assert (m_operator);
331 switch (dispatch_kind (lhs, op1, op2))
332 {
333 case RO_III:
334 return m_operator->lhs_op2_relation (as_a <irange> (lhs),
335 as_a <irange> (op1),
336 as_a <irange> (op2), rel);
337 case RO_IFF:
338 return m_operator->lhs_op2_relation (as_a <irange> (lhs),
339 as_a <frange> (op1),
340 as_a <frange> (op2), rel);
341 case RO_FFF:
342 return m_operator->lhs_op2_relation (as_a <frange> (lhs),
343 as_a <frange> (op1),
344 as_a <frange> (op2), rel);
345 default:
346 return VREL_VARYING;
347 }
348}
349
350// Dispatch a call to op1_op2_relation based on the type of LHS.
351
352relation_kind
353range_op_handler::op1_op2_relation (const vrange &lhs) const
354{
355 gcc_checking_assert (m_operator);
356 switch (dispatch_kind (lhs, lhs, lhs))
357 {
358 case RO_III:
359 return m_operator->op1_op2_relation (as_a <irange> (lhs));
360
361 case RO_FFF:
362 return m_operator->op1_op2_relation (as_a <frange> (lhs));
363
364 default:
365 return VREL_VARYING;
366 }
367}
368
369
b74dd1bb
AH
370// Update the known bitmasks in R when applying the operation CODE to
371// LH and RH.
372
f6e160e3 373void
b74dd1bb
AH
374update_known_bitmask (irange &r, tree_code code,
375 const irange &lh, const irange &rh)
376{
602e824e
AH
377 if (r.undefined_p () || lh.undefined_p () || rh.undefined_p ()
378 || r.singleton_p ())
b74dd1bb
AH
379 return;
380
602e824e 381 widest_int widest_value, widest_mask;
b74dd1bb
AH
382 tree type = r.type ();
383 signop sign = TYPE_SIGN (type);
384 int prec = TYPE_PRECISION (type);
602e824e
AH
385 irange_bitmask lh_bits = lh.get_bitmask ();
386 irange_bitmask rh_bits = rh.get_bitmask ();
387
5cac2394
AH
388 switch (get_gimple_rhs_class (code))
389 {
390 case GIMPLE_UNARY_RHS:
391 bit_value_unop (code, sign, prec, &widest_value, &widest_mask,
392 TYPE_SIGN (lh.type ()),
393 TYPE_PRECISION (lh.type ()),
394 widest_int::from (lh_bits.value (), sign),
395 widest_int::from (lh_bits.mask (), sign));
396 break;
397 case GIMPLE_BINARY_RHS:
398 bit_value_binop (code, sign, prec, &widest_value, &widest_mask,
399 TYPE_SIGN (lh.type ()),
400 TYPE_PRECISION (lh.type ()),
401 widest_int::from (lh_bits.value (), sign),
402 widest_int::from (lh_bits.mask (), sign),
403 TYPE_SIGN (rh.type ()),
404 TYPE_PRECISION (rh.type ()),
405 widest_int::from (rh_bits.value (), sign),
406 widest_int::from (rh_bits.mask (), sign));
407 break;
408 default:
409 gcc_unreachable ();
410 }
602e824e
AH
411
412 wide_int mask = wide_int::from (widest_mask, prec, sign);
413 wide_int value = wide_int::from (widest_value, prec, sign);
414 // Bitmasks must have the unknown value bits cleared.
415 value &= ~mask;
416 irange_bitmask bm (value, mask);
417 r.update_bitmask (bm);
b74dd1bb 418}
38a73435
AH
419
420// Return the upper limit for a type.
421
422static inline wide_int
423max_limit (const_tree type)
424{
8b2181a4 425 return irange_val_max (type);
38a73435
AH
426}
427
428// Return the lower limit for a type.
429
430static inline wide_int
431min_limit (const_tree type)
432{
8b2181a4 433 return irange_val_min (type);
38a73435
AH
434}
435
d0d8b5d8
AM
436// Return false if shifting by OP is undefined behavior. Otherwise, return
437// true and the range it is to be shifted by. This allows trimming out of
438// undefined ranges, leaving only valid ranges if there are any.
38a73435
AH
439
440static inline bool
d0d8b5d8 441get_shift_range (irange &r, tree type, const irange &op)
38a73435
AH
442{
443 if (op.undefined_p ())
d0d8b5d8 444 return false;
38a73435 445
d0d8b5d8 446 // Build valid range and intersect it with the shift range.
cb779afe
AH
447 r = value_range (op.type (),
448 wi::shwi (0, TYPE_PRECISION (op.type ())),
449 wi::shwi (TYPE_PRECISION (type) - 1, TYPE_PRECISION (op.type ())));
d0d8b5d8
AM
450 r.intersect (op);
451
452 // If there are no valid ranges in the shift range, returned false.
453 if (r.undefined_p ())
454 return false;
455 return true;
38a73435
AH
456}
457
38a73435
AH
458// Default wide_int fold operation returns [MIN, MAX].
459
bb74ef9e 460void
4ba9fb0a 461range_operator::wi_fold (irange &r, tree type,
38a73435
AH
462 const wide_int &lh_lb ATTRIBUTE_UNUSED,
463 const wide_int &lh_ub ATTRIBUTE_UNUSED,
464 const wide_int &rh_lb ATTRIBUTE_UNUSED,
465 const wide_int &rh_ub ATTRIBUTE_UNUSED) const
466{
a9058b08 467 gcc_checking_assert (r.supports_type_p (type));
4ba9fb0a 468 r.set_varying (type);
38a73435
AH
469}
470
809d661a
AM
471// Call wi_fold when both op1 and op2 are equivalent. Further split small
472// subranges into constants. This can provide better precision.
473// For x + y, when x == y with a range of [0,4] instead of [0, 8] produce
474// [0,0][2, 2][4,4][6, 6][8, 8]
475// LIMIT is the maximum number of elements in range allowed before we
c46b5b0a 476// do not process them individually.
809d661a
AM
477
478void
479range_operator::wi_fold_in_parts_equiv (irange &r, tree type,
480 const wide_int &lh_lb,
481 const wide_int &lh_ub,
482 unsigned limit) const
483{
484 int_range_max tmp;
485 widest_int lh_range = wi::sub (widest_int::from (lh_ub, TYPE_SIGN (type)),
486 widest_int::from (lh_lb, TYPE_SIGN (type)));
487 // if there are 1 to 8 values in the LH range, split them up.
488 r.set_undefined ();
489 if (lh_range >= 0 && lh_range < limit)
490 {
491 for (unsigned x = 0; x <= lh_range; x++)
492 {
493 wide_int val = lh_lb + x;
494 wi_fold (tmp, type, val, val, val, val);
495 r.union_ (tmp);
496 }
497 }
498 // Otherwise just call wi_fold.
499 else
500 wi_fold (r, type, lh_lb, lh_ub, lh_lb, lh_ub);
501}
502
704e8a82
AM
503// Call wi_fold, except further split small subranges into constants.
504// This can provide better precision. For something 8 >> [0,1]
505// Instead of [8, 16], we will produce [8,8][16,16]
506
507void
508range_operator::wi_fold_in_parts (irange &r, tree type,
509 const wide_int &lh_lb,
510 const wide_int &lh_ub,
511 const wide_int &rh_lb,
512 const wide_int &rh_ub) const
513{
704e8a82 514 int_range_max tmp;
de67f943
JJ
515 widest_int rh_range = wi::sub (widest_int::from (rh_ub, TYPE_SIGN (type)),
516 widest_int::from (rh_lb, TYPE_SIGN (type)));
517 widest_int lh_range = wi::sub (widest_int::from (lh_ub, TYPE_SIGN (type)),
518 widest_int::from (lh_lb, TYPE_SIGN (type)));
704e8a82
AM
519 // If there are 2, 3, or 4 values in the RH range, do them separately.
520 // Call wi_fold_in_parts to check the RH side.
de67f943 521 if (rh_range > 0 && rh_range < 4)
704e8a82
AM
522 {
523 wi_fold_in_parts (r, type, lh_lb, lh_ub, rh_lb, rh_lb);
de67f943 524 if (rh_range > 1)
704e8a82
AM
525 {
526 wi_fold_in_parts (tmp, type, lh_lb, lh_ub, rh_lb + 1, rh_lb + 1);
527 r.union_ (tmp);
de67f943 528 if (rh_range == 3)
704e8a82
AM
529 {
530 wi_fold_in_parts (tmp, type, lh_lb, lh_ub, rh_lb + 2, rh_lb + 2);
531 r.union_ (tmp);
532 }
533 }
534 wi_fold_in_parts (tmp, type, lh_lb, lh_ub, rh_ub, rh_ub);
535 r.union_ (tmp);
536 }
c46b5b0a 537 // Otherwise check for 2, 3, or 4 values in the LH range and split them up.
704e8a82 538 // The RH side has been checked, so no recursion needed.
de67f943 539 else if (lh_range > 0 && lh_range < 4)
704e8a82
AM
540 {
541 wi_fold (r, type, lh_lb, lh_lb, rh_lb, rh_ub);
de67f943 542 if (lh_range > 1)
704e8a82
AM
543 {
544 wi_fold (tmp, type, lh_lb + 1, lh_lb + 1, rh_lb, rh_ub);
545 r.union_ (tmp);
de67f943 546 if (lh_range == 3)
704e8a82
AM
547 {
548 wi_fold (tmp, type, lh_lb + 2, lh_lb + 2, rh_lb, rh_ub);
549 r.union_ (tmp);
550 }
551 }
552 wi_fold (tmp, type, lh_ub, lh_ub, rh_lb, rh_ub);
553 r.union_ (tmp);
554 }
555 // Otherwise just call wi_fold.
556 else
557 wi_fold (r, type, lh_lb, lh_ub, rh_lb, rh_ub);
558}
559
38a73435
AH
560// The default for fold is to break all ranges into sub-ranges and
561// invoke the wi_fold method on each sub-range pair.
562
f674b4a7 563bool
4ba9fb0a
AH
564range_operator::fold_range (irange &r, tree type,
565 const irange &lh,
80dd13f5 566 const irange &rh,
b565ac19 567 relation_trio trio) const
38a73435 568{
a9058b08 569 gcc_checking_assert (r.supports_type_p (type));
4ba9fb0a 570 if (empty_range_varying (r, type, lh, rh))
f674b4a7 571 return true;
38a73435 572
b565ac19 573 relation_kind rel = trio.op1_op2 ();
4ba9fb0a
AH
574 unsigned num_lh = lh.num_pairs ();
575 unsigned num_rh = rh.num_pairs ();
576
809d661a
AM
577 // If op1 and op2 are equivalences, then we don't need a complete cross
578 // product, just pairs of matching elements.
579 if (relation_equiv_p (rel) && lh == rh)
580 {
581 int_range_max tmp;
582 r.set_undefined ();
583 for (unsigned x = 0; x < num_lh; ++x)
584 {
585 // If the number of subranges is too high, limit subrange creation.
586 unsigned limit = (r.num_pairs () > 32) ? 0 : 8;
587 wide_int lh_lb = lh.lower_bound (x);
588 wide_int lh_ub = lh.upper_bound (x);
589 wi_fold_in_parts_equiv (tmp, type, lh_lb, lh_ub, limit);
590 r.union_ (tmp);
591 if (r.varying_p ())
592 break;
593 }
594 op1_op2_relation_effect (r, type, lh, rh, rel);
cd4b7e8b 595 update_bitmask (r, lh, rh);
809d661a
AM
596 return true;
597 }
598
4ba9fb0a 599 // If both ranges are single pairs, fold directly into the result range.
71b72132
AM
600 // If the number of subranges grows too high, produce a summary result as the
601 // loop becomes exponential with little benefit. See PR 103821.
602 if ((num_lh == 1 && num_rh == 1) || num_lh * num_rh > 12)
4ba9fb0a 603 {
71b72132
AM
604 wi_fold_in_parts (r, type, lh.lower_bound (), lh.upper_bound (),
605 rh.lower_bound (), rh.upper_bound ());
80dd13f5 606 op1_op2_relation_effect (r, type, lh, rh, rel);
cd4b7e8b 607 update_bitmask (r, lh, rh);
4ba9fb0a
AH
608 return true;
609 }
610
c5a6c223 611 int_range_max tmp;
bbc85eb9 612 r.set_undefined ();
4ba9fb0a
AH
613 for (unsigned x = 0; x < num_lh; ++x)
614 for (unsigned y = 0; y < num_rh; ++y)
38a73435
AH
615 {
616 wide_int lh_lb = lh.lower_bound (x);
617 wide_int lh_ub = lh.upper_bound (x);
618 wide_int rh_lb = rh.lower_bound (y);
619 wide_int rh_ub = rh.upper_bound (y);
704e8a82 620 wi_fold_in_parts (tmp, type, lh_lb, lh_ub, rh_lb, rh_ub);
bb74ef9e 621 r.union_ (tmp);
38a73435 622 if (r.varying_p ())
80dd13f5
AM
623 {
624 op1_op2_relation_effect (r, type, lh, rh, rel);
cd4b7e8b 625 update_bitmask (r, lh, rh);
80dd13f5
AM
626 return true;
627 }
38a73435 628 }
80dd13f5 629 op1_op2_relation_effect (r, type, lh, rh, rel);
cd4b7e8b 630 update_bitmask (r, lh, rh);
f674b4a7 631 return true;
38a73435
AH
632}
633
634// The default for op1_range is to return false.
635
636bool
4ba9fb0a 637range_operator::op1_range (irange &r ATTRIBUTE_UNUSED,
38a73435 638 tree type ATTRIBUTE_UNUSED,
4ba9fb0a 639 const irange &lhs ATTRIBUTE_UNUSED,
80dd13f5 640 const irange &op2 ATTRIBUTE_UNUSED,
b565ac19 641 relation_trio) const
38a73435
AH
642{
643 return false;
644}
645
646// The default for op2_range is to return false.
647
648bool
4ba9fb0a 649range_operator::op2_range (irange &r ATTRIBUTE_UNUSED,
38a73435 650 tree type ATTRIBUTE_UNUSED,
4ba9fb0a 651 const irange &lhs ATTRIBUTE_UNUSED,
80dd13f5 652 const irange &op1 ATTRIBUTE_UNUSED,
b565ac19 653 relation_trio) const
38a73435
AH
654{
655 return false;
656}
657
ade5531c 658// The default relation routines return VREL_VARYING.
80dd13f5 659
ade5531c 660relation_kind
80dd13f5
AM
661range_operator::lhs_op1_relation (const irange &lhs ATTRIBUTE_UNUSED,
662 const irange &op1 ATTRIBUTE_UNUSED,
cf2141a0
AM
663 const irange &op2 ATTRIBUTE_UNUSED,
664 relation_kind rel ATTRIBUTE_UNUSED) const
80dd13f5 665{
ade5531c 666 return VREL_VARYING;
80dd13f5
AM
667}
668
ade5531c 669relation_kind
80dd13f5
AM
670range_operator::lhs_op2_relation (const irange &lhs ATTRIBUTE_UNUSED,
671 const irange &op1 ATTRIBUTE_UNUSED,
cf2141a0
AM
672 const irange &op2 ATTRIBUTE_UNUSED,
673 relation_kind rel ATTRIBUTE_UNUSED) const
80dd13f5 674{
ade5531c 675 return VREL_VARYING;
80dd13f5
AM
676}
677
ade5531c 678relation_kind
80dd13f5
AM
679range_operator::op1_op2_relation (const irange &lhs ATTRIBUTE_UNUSED) const
680{
ade5531c 681 return VREL_VARYING;
80dd13f5
AM
682}
683
684// Default is no relation affects the LHS.
685
686bool
687range_operator::op1_op2_relation_effect (irange &lhs_range ATTRIBUTE_UNUSED,
688 tree type ATTRIBUTE_UNUSED,
689 const irange &op1_range ATTRIBUTE_UNUSED,
690 const irange &op2_range ATTRIBUTE_UNUSED,
691 relation_kind rel ATTRIBUTE_UNUSED) const
692{
693 return false;
694}
38a73435 695
cd4b7e8b
AM
696// Apply any known bitmask updates based on this operator.
697
698void
699range_operator::update_bitmask (irange &, const irange &,
700 const irange &) const
701{
702}
703
3d203d01
AH
704// Create and return a range from a pair of wide-ints that are known
705// to have overflowed (or underflowed).
38a73435 706
bb74ef9e 707static void
4ba9fb0a 708value_range_from_overflowed_bounds (irange &r, tree type,
3d203d01
AH
709 const wide_int &wmin,
710 const wide_int &wmax)
38a73435
AH
711{
712 const signop sgn = TYPE_SIGN (type);
713 const unsigned int prec = TYPE_PRECISION (type);
714
715 wide_int tmin = wide_int::from (wmin, prec, sgn);
716 wide_int tmax = wide_int::from (wmax, prec, sgn);
717
718 bool covers = false;
719 wide_int tem = tmin;
720 tmin = tmax + 1;
721 if (wi::cmp (tmin, tmax, sgn) < 0)
722 covers = true;
723 tmax = tem - 1;
724 if (wi::cmp (tmax, tem, sgn) > 0)
725 covers = true;
726
727 // If the anti-range would cover nothing, drop to varying.
728 // Likewise if the anti-range bounds are outside of the types
729 // values.
730 if (covers || wi::cmp (tmin, tmax, sgn) > 0)
4ba9fb0a 731 r.set_varying (type);
bb74ef9e 732 else
cb779afe 733 r.set (type, tmin, tmax, VR_ANTI_RANGE);
38a73435
AH
734}
735
3d203d01
AH
736// Create and return a range from a pair of wide-ints. MIN_OVF and
737// MAX_OVF describe any overflow that might have occurred while
738// calculating WMIN and WMAX respectively.
38a73435 739
bb74ef9e 740static void
4ba9fb0a 741value_range_with_overflow (irange &r, tree type,
3d203d01
AH
742 const wide_int &wmin, const wide_int &wmax,
743 wi::overflow_type min_ovf = wi::OVF_NONE,
744 wi::overflow_type max_ovf = wi::OVF_NONE)
38a73435
AH
745{
746 const signop sgn = TYPE_SIGN (type);
747 const unsigned int prec = TYPE_PRECISION (type);
748 const bool overflow_wraps = TYPE_OVERFLOW_WRAPS (type);
749
750 // For one bit precision if max != min, then the range covers all
751 // values.
752 if (prec == 1 && wi::ne_p (wmax, wmin))
bb74ef9e 753 {
4ba9fb0a 754 r.set_varying (type);
bb74ef9e
AM
755 return;
756 }
38a73435
AH
757
758 if (overflow_wraps)
759 {
760 // If overflow wraps, truncate the values and adjust the range,
761 // kind, and bounds appropriately.
762 if ((min_ovf != wi::OVF_NONE) == (max_ovf != wi::OVF_NONE))
763 {
764 wide_int tmin = wide_int::from (wmin, prec, sgn);
765 wide_int tmax = wide_int::from (wmax, prec, sgn);
766 // If the limits are swapped, we wrapped around and cover
767 // the entire range.
768 if (wi::gt_p (tmin, tmax, sgn))
4ba9fb0a 769 r.set_varying (type);
bb74ef9e
AM
770 else
771 // No overflow or both overflow or underflow. The range
772 // kind stays normal.
cb779afe 773 r.set (type, tmin, tmax);
bb74ef9e 774 return;
38a73435
AH
775 }
776
777 if ((min_ovf == wi::OVF_UNDERFLOW && max_ovf == wi::OVF_NONE)
778 || (max_ovf == wi::OVF_OVERFLOW && min_ovf == wi::OVF_NONE))
bb74ef9e
AM
779 value_range_from_overflowed_bounds (r, type, wmin, wmax);
780 else
781 // Other underflow and/or overflow, drop to VR_VARYING.
4ba9fb0a 782 r.set_varying (type);
38a73435
AH
783 }
784 else
785 {
91ae6930
AH
786 // If both bounds either underflowed or overflowed, then the result
787 // is undefined.
788 if ((min_ovf == wi::OVF_OVERFLOW && max_ovf == wi::OVF_OVERFLOW)
789 || (min_ovf == wi::OVF_UNDERFLOW && max_ovf == wi::OVF_UNDERFLOW))
790 {
791 r.set_undefined ();
792 return;
793 }
794
38a73435
AH
795 // If overflow does not wrap, saturate to [MIN, MAX].
796 wide_int new_lb, new_ub;
797 if (min_ovf == wi::OVF_UNDERFLOW)
798 new_lb = wi::min_value (prec, sgn);
799 else if (min_ovf == wi::OVF_OVERFLOW)
800 new_lb = wi::max_value (prec, sgn);
801 else
802 new_lb = wmin;
803
804 if (max_ovf == wi::OVF_UNDERFLOW)
805 new_ub = wi::min_value (prec, sgn);
806 else if (max_ovf == wi::OVF_OVERFLOW)
807 new_ub = wi::max_value (prec, sgn);
808 else
809 new_ub = wmax;
810
cb779afe 811 r.set (type, new_lb, new_ub);
38a73435
AH
812 }
813}
814
3d203d01
AH
815// Create and return a range from a pair of wide-ints. Canonicalize
816// the case where the bounds are swapped. In which case, we transform
817// [10,5] into [MIN,5][10,MAX].
38a73435 818
bb74ef9e 819static inline void
4ba9fb0a 820create_possibly_reversed_range (irange &r, tree type,
38a73435
AH
821 const wide_int &new_lb, const wide_int &new_ub)
822{
823 signop s = TYPE_SIGN (type);
c46b5b0a 824 // If the bounds are swapped, treat the result as if an overflow occurred.
38a73435 825 if (wi::gt_p (new_lb, new_ub, s))
bb74ef9e
AM
826 value_range_from_overflowed_bounds (r, type, new_lb, new_ub);
827 else
4ba9fb0a 828 // Otherwise it's just a normal range.
cb779afe 829 r.set (type, new_lb, new_ub);
38a73435
AH
830}
831
ead233e6
EB
832// Return the summary information about boolean range LHS. If EMPTY/FULL,
833// return the equivalent range for TYPE in R; if FALSE/TRUE, do nothing.
38a73435 834
9eb38e88 835bool_range_state
cf5bea76 836get_bool_state (vrange &r, const vrange &lhs, tree val_type)
38a73435
AH
837{
838 // If there is no result, then this is unexecutable.
839 if (lhs.undefined_p ())
840 {
841 r.set_undefined ();
842 return BRS_EMPTY;
843 }
844
4ba9fb0a
AH
845 if (lhs.zero_p ())
846 return BRS_FALSE;
847
848 // For TRUE, we can't just test for [1,1] because Ada can have
849 // multi-bit booleans, and TRUE values can be: [1, MAX], ~[0], etc.
850 if (lhs.contains_p (build_zero_cst (lhs.type ())))
38a73435
AH
851 {
852 r.set_varying (val_type);
853 return BRS_FULL;
854 }
ead233e6 855
38a73435
AH
856 return BRS_TRUE;
857}
858
2dbf1e61 859// ------------------------------------------------------------------------
38a73435 860
2dbf1e61
AM
861void
862operator_equal::update_bitmask (irange &r, const irange &lh,
863 const irange &rh) const
38a73435 864{
2dbf1e61
AM
865 update_known_bitmask (r, EQ_EXPR, lh, rh);
866}
38a73435 867
80dd13f5
AM
868// Check if the LHS range indicates a relation between OP1 and OP2.
869
ade5531c 870relation_kind
2dbf1e61 871operator_equal::op1_op2_relation (const irange &lhs) const
80dd13f5
AM
872{
873 if (lhs.undefined_p ())
ade5531c 874 return VREL_UNDEFINED;
80dd13f5
AM
875
876 // FALSE = op1 == op2 indicates NE_EXPR.
877 if (lhs.zero_p ())
ade5531c 878 return VREL_NE;
80dd13f5
AM
879
880 // TRUE = op1 == op2 indicates EQ_EXPR.
cb779afe 881 if (lhs.undefined_p () || !contains_zero_p (lhs))
ade5531c
AM
882 return VREL_EQ;
883 return VREL_VARYING;
80dd13f5
AM
884}
885
f674b4a7 886bool
4ba9fb0a
AH
887operator_equal::fold_range (irange &r, tree type,
888 const irange &op1,
80dd13f5 889 const irange &op2,
b565ac19 890 relation_trio rel) const
38a73435 891{
ade5531c 892 if (relop_early_resolve (r, type, op1, op2, rel, VREL_EQ))
f674b4a7 893 return true;
38a73435
AH
894
895 // We can be sure the values are always equal or not if both ranges
896 // consist of a single value, and then compare them.
897 if (wi::eq_p (op1.lower_bound (), op1.upper_bound ())
898 && wi::eq_p (op2.lower_bound (), op2.upper_bound ()))
899 {
900 if (wi::eq_p (op1.lower_bound (), op2.upper_bound()))
901 r = range_true (type);
902 else
903 r = range_false (type);
904 }
905 else
906 {
907 // If ranges do not intersect, we know the range is not equal,
908 // otherwise we don't know anything for sure.
22984f3f
AM
909 int_range_max tmp = op1;
910 tmp.intersect (op2);
911 if (tmp.undefined_p ())
38a73435
AH
912 r = range_false (type);
913 else
914 r = range_true_and_false (type);
915 }
f674b4a7 916 return true;
38a73435
AH
917}
918
919bool
4ba9fb0a
AH
920operator_equal::op1_range (irange &r, tree type,
921 const irange &lhs,
80dd13f5 922 const irange &op2,
b565ac19 923 relation_trio) const
38a73435
AH
924{
925 switch (get_bool_state (r, lhs, type))
926 {
ad7cff63
AH
927 case BRS_TRUE:
928 // If it's true, the result is the same as OP2.
929 r = op2;
930 break;
931
38a73435
AH
932 case BRS_FALSE:
933 // If the result is false, the only time we know anything is
934 // if OP2 is a constant.
e753080a
JJ
935 if (!op2.undefined_p ()
936 && wi::eq_p (op2.lower_bound(), op2.upper_bound()))
fae08a05
AH
937 {
938 r = op2;
939 r.invert ();
940 }
38a73435
AH
941 else
942 r.set_varying (type);
943 break;
944
38a73435
AH
945 default:
946 break;
947 }
948 return true;
949}
950
951bool
4ba9fb0a
AH
952operator_equal::op2_range (irange &r, tree type,
953 const irange &lhs,
80dd13f5 954 const irange &op1,
b565ac19 955 relation_trio rel) const
38a73435 956{
b565ac19 957 return operator_equal::op1_range (r, type, lhs, op1, rel.swap_op1_op2 ());
38a73435
AH
958}
959
eb29c3e1
AM
960// -------------------------------------------------------------------------
961
962void
963operator_not_equal::update_bitmask (irange &r, const irange &lh,
964 const irange &rh) const
38a73435 965{
eb29c3e1
AM
966 update_known_bitmask (r, NE_EXPR, lh, rh);
967}
38a73435 968
80dd13f5
AM
969// Check if the LHS range indicates a relation between OP1 and OP2.
970
ade5531c 971relation_kind
eb29c3e1 972operator_not_equal::op1_op2_relation (const irange &lhs) const
80dd13f5
AM
973{
974 if (lhs.undefined_p ())
ade5531c 975 return VREL_UNDEFINED;
80dd13f5
AM
976
977 // FALSE = op1 != op2 indicates EQ_EXPR.
978 if (lhs.zero_p ())
ade5531c 979 return VREL_EQ;
80dd13f5
AM
980
981 // TRUE = op1 != op2 indicates NE_EXPR.
cb779afe 982 if (lhs.undefined_p () || !contains_zero_p (lhs))
ade5531c
AM
983 return VREL_NE;
984 return VREL_VARYING;
80dd13f5
AM
985}
986
f674b4a7 987bool
4ba9fb0a
AH
988operator_not_equal::fold_range (irange &r, tree type,
989 const irange &op1,
80dd13f5 990 const irange &op2,
b565ac19 991 relation_trio rel) const
38a73435 992{
ade5531c 993 if (relop_early_resolve (r, type, op1, op2, rel, VREL_NE))
f674b4a7 994 return true;
38a73435
AH
995
996 // We can be sure the values are always equal or not if both ranges
997 // consist of a single value, and then compare them.
998 if (wi::eq_p (op1.lower_bound (), op1.upper_bound ())
999 && wi::eq_p (op2.lower_bound (), op2.upper_bound ()))
1000 {
1001 if (wi::ne_p (op1.lower_bound (), op2.upper_bound()))
1002 r = range_true (type);
1003 else
1004 r = range_false (type);
1005 }
1006 else
1007 {
1008 // If ranges do not intersect, we know the range is not equal,
1009 // otherwise we don't know anything for sure.
22984f3f
AM
1010 int_range_max tmp = op1;
1011 tmp.intersect (op2);
1012 if (tmp.undefined_p ())
38a73435
AH
1013 r = range_true (type);
1014 else
1015 r = range_true_and_false (type);
1016 }
f674b4a7 1017 return true;
38a73435
AH
1018}
1019
1020bool
4ba9fb0a
AH
1021operator_not_equal::op1_range (irange &r, tree type,
1022 const irange &lhs,
80dd13f5 1023 const irange &op2,
b565ac19 1024 relation_trio) const
38a73435
AH
1025{
1026 switch (get_bool_state (r, lhs, type))
1027 {
1028 case BRS_TRUE:
1029 // If the result is true, the only time we know anything is if
1030 // OP2 is a constant.
e753080a
JJ
1031 if (!op2.undefined_p ()
1032 && wi::eq_p (op2.lower_bound(), op2.upper_bound()))
fae08a05
AH
1033 {
1034 r = op2;
1035 r.invert ();
1036 }
38a73435
AH
1037 else
1038 r.set_varying (type);
1039 break;
1040
1041 case BRS_FALSE:
ead233e6 1042 // If it's false, the result is the same as OP2.
38a73435
AH
1043 r = op2;
1044 break;
1045
1046 default:
1047 break;
1048 }
1049 return true;
1050}
1051
1052
1053bool
4ba9fb0a
AH
1054operator_not_equal::op2_range (irange &r, tree type,
1055 const irange &lhs,
80dd13f5 1056 const irange &op1,
b565ac19 1057 relation_trio rel) const
38a73435 1058{
b565ac19 1059 return operator_not_equal::op1_range (r, type, lhs, op1, rel.swap_op1_op2 ());
38a73435
AH
1060}
1061
1062// (X < VAL) produces the range of [MIN, VAL - 1].
1063
1064static void
4ba9fb0a 1065build_lt (irange &r, tree type, const wide_int &val)
38a73435
AH
1066{
1067 wi::overflow_type ov;
84f7bab8
AM
1068 wide_int lim;
1069 signop sgn = TYPE_SIGN (type);
1070
1071 // Signed 1 bit cannot represent 1 for subtraction.
1072 if (sgn == SIGNED)
1073 lim = wi::add (val, -1, sgn, &ov);
1074 else
1075 lim = wi::sub (val, 1, sgn, &ov);
38a73435
AH
1076
1077 // If val - 1 underflows, check if X < MIN, which is an empty range.
1078 if (ov)
1079 r.set_undefined ();
1080 else
4ba9fb0a 1081 r = int_range<1> (type, min_limit (type), lim);
38a73435
AH
1082}
1083
1084// (X <= VAL) produces the range of [MIN, VAL].
1085
1086static void
4ba9fb0a 1087build_le (irange &r, tree type, const wide_int &val)
38a73435 1088{
4ba9fb0a 1089 r = int_range<1> (type, min_limit (type), val);
38a73435
AH
1090}
1091
1092// (X > VAL) produces the range of [VAL + 1, MAX].
1093
1094static void
4ba9fb0a 1095build_gt (irange &r, tree type, const wide_int &val)
38a73435
AH
1096{
1097 wi::overflow_type ov;
84f7bab8
AM
1098 wide_int lim;
1099 signop sgn = TYPE_SIGN (type);
1100
1101 // Signed 1 bit cannot represent 1 for addition.
1102 if (sgn == SIGNED)
1103 lim = wi::sub (val, -1, sgn, &ov);
1104 else
1105 lim = wi::add (val, 1, sgn, &ov);
38a73435
AH
1106 // If val + 1 overflows, check is for X > MAX, which is an empty range.
1107 if (ov)
1108 r.set_undefined ();
1109 else
4ba9fb0a 1110 r = int_range<1> (type, lim, max_limit (type));
38a73435
AH
1111}
1112
1113// (X >= val) produces the range of [VAL, MAX].
1114
1115static void
4ba9fb0a 1116build_ge (irange &r, tree type, const wide_int &val)
38a73435 1117{
4ba9fb0a 1118 r = int_range<1> (type, val, max_limit (type));
38a73435
AH
1119}
1120
1121
5b079541
AM
1122void
1123operator_lt::update_bitmask (irange &r, const irange &lh,
1124 const irange &rh) const
38a73435 1125{
5b079541
AM
1126 update_known_bitmask (r, LT_EXPR, lh, rh);
1127}
38a73435 1128
80dd13f5
AM
1129// Check if the LHS range indicates a relation between OP1 and OP2.
1130
ade5531c 1131relation_kind
5b079541 1132operator_lt::op1_op2_relation (const irange &lhs) const
80dd13f5
AM
1133{
1134 if (lhs.undefined_p ())
ade5531c 1135 return VREL_UNDEFINED;
80dd13f5
AM
1136
1137 // FALSE = op1 < op2 indicates GE_EXPR.
1138 if (lhs.zero_p ())
ade5531c 1139 return VREL_GE;
80dd13f5
AM
1140
1141 // TRUE = op1 < op2 indicates LT_EXPR.
cb779afe 1142 if (lhs.undefined_p () || !contains_zero_p (lhs))
ade5531c
AM
1143 return VREL_LT;
1144 return VREL_VARYING;
80dd13f5
AM
1145}
1146
f674b4a7 1147bool
4ba9fb0a
AH
1148operator_lt::fold_range (irange &r, tree type,
1149 const irange &op1,
80dd13f5 1150 const irange &op2,
b565ac19 1151 relation_trio rel) const
38a73435 1152{
ade5531c 1153 if (relop_early_resolve (r, type, op1, op2, rel, VREL_LT))
f674b4a7 1154 return true;
38a73435
AH
1155
1156 signop sign = TYPE_SIGN (op1.type ());
1157 gcc_checking_assert (sign == TYPE_SIGN (op2.type ()));
1158
1159 if (wi::lt_p (op1.upper_bound (), op2.lower_bound (), sign))
1160 r = range_true (type);
1161 else if (!wi::lt_p (op1.lower_bound (), op2.upper_bound (), sign))
1162 r = range_false (type);
1184f677
AH
1163 // Use nonzero bits to determine if < 0 is false.
1164 else if (op2.zero_p () && !wi::neg_p (op1.get_nonzero_bits (), sign))
1165 r = range_false (type);
38a73435
AH
1166 else
1167 r = range_true_and_false (type);
f674b4a7 1168 return true;
38a73435
AH
1169}
1170
1171bool
4ba9fb0a
AH
1172operator_lt::op1_range (irange &r, tree type,
1173 const irange &lhs,
80dd13f5 1174 const irange &op2,
b565ac19 1175 relation_trio) const
38a73435 1176{
e753080a
JJ
1177 if (op2.undefined_p ())
1178 return false;
1179
38a73435
AH
1180 switch (get_bool_state (r, lhs, type))
1181 {
1182 case BRS_TRUE:
1183 build_lt (r, type, op2.upper_bound ());
1184 break;
1185
1186 case BRS_FALSE:
1187 build_ge (r, type, op2.lower_bound ());
1188 break;
1189
1190 default:
1191 break;
1192 }
1193 return true;
1194}
1195
1196bool
4ba9fb0a
AH
1197operator_lt::op2_range (irange &r, tree type,
1198 const irange &lhs,
80dd13f5 1199 const irange &op1,
b565ac19 1200 relation_trio) const
38a73435 1201{
e753080a
JJ
1202 if (op1.undefined_p ())
1203 return false;
1204
38a73435
AH
1205 switch (get_bool_state (r, lhs, type))
1206 {
38a73435
AH
1207 case BRS_TRUE:
1208 build_gt (r, type, op1.lower_bound ());
1209 break;
1210
ad7cff63
AH
1211 case BRS_FALSE:
1212 build_le (r, type, op1.upper_bound ());
1213 break;
1214
38a73435
AH
1215 default:
1216 break;
1217 }
1218 return true;
1219}
1220
1221
d251d14c
AM
1222void
1223operator_le::update_bitmask (irange &r, const irange &lh,
1224 const irange &rh) const
38a73435 1225{
d251d14c
AM
1226 update_known_bitmask (r, LE_EXPR, lh, rh);
1227}
38a73435 1228
80dd13f5
AM
1229// Check if the LHS range indicates a relation between OP1 and OP2.
1230
ade5531c 1231relation_kind
d251d14c 1232operator_le::op1_op2_relation (const irange &lhs) const
80dd13f5
AM
1233{
1234 if (lhs.undefined_p ())
ade5531c 1235 return VREL_UNDEFINED;
80dd13f5
AM
1236
1237 // FALSE = op1 <= op2 indicates GT_EXPR.
1238 if (lhs.zero_p ())
ade5531c 1239 return VREL_GT;
80dd13f5
AM
1240
1241 // TRUE = op1 <= op2 indicates LE_EXPR.
cb779afe 1242 if (lhs.undefined_p () || !contains_zero_p (lhs))
ade5531c
AM
1243 return VREL_LE;
1244 return VREL_VARYING;
80dd13f5
AM
1245}
1246
f674b4a7 1247bool
4ba9fb0a
AH
1248operator_le::fold_range (irange &r, tree type,
1249 const irange &op1,
80dd13f5 1250 const irange &op2,
b565ac19 1251 relation_trio rel) const
38a73435 1252{
ade5531c 1253 if (relop_early_resolve (r, type, op1, op2, rel, VREL_LE))
f674b4a7 1254 return true;
38a73435
AH
1255
1256 signop sign = TYPE_SIGN (op1.type ());
1257 gcc_checking_assert (sign == TYPE_SIGN (op2.type ()));
1258
1259 if (wi::le_p (op1.upper_bound (), op2.lower_bound (), sign))
1260 r = range_true (type);
1261 else if (!wi::le_p (op1.lower_bound (), op2.upper_bound (), sign))
1262 r = range_false (type);
1263 else
1264 r = range_true_and_false (type);
f674b4a7 1265 return true;
38a73435
AH
1266}
1267
1268bool
4ba9fb0a
AH
1269operator_le::op1_range (irange &r, tree type,
1270 const irange &lhs,
80dd13f5 1271 const irange &op2,
b565ac19 1272 relation_trio) const
38a73435 1273{
e753080a
JJ
1274 if (op2.undefined_p ())
1275 return false;
1276
38a73435
AH
1277 switch (get_bool_state (r, lhs, type))
1278 {
1279 case BRS_TRUE:
1280 build_le (r, type, op2.upper_bound ());
1281 break;
1282
1283 case BRS_FALSE:
1284 build_gt (r, type, op2.lower_bound ());
1285 break;
1286
1287 default:
1288 break;
1289 }
1290 return true;
1291}
1292
1293bool
4ba9fb0a
AH
1294operator_le::op2_range (irange &r, tree type,
1295 const irange &lhs,
80dd13f5 1296 const irange &op1,
b565ac19 1297 relation_trio) const
38a73435 1298{
e753080a
JJ
1299 if (op1.undefined_p ())
1300 return false;
1301
38a73435
AH
1302 switch (get_bool_state (r, lhs, type))
1303 {
38a73435
AH
1304 case BRS_TRUE:
1305 build_ge (r, type, op1.lower_bound ());
1306 break;
1307
ad7cff63
AH
1308 case BRS_FALSE:
1309 build_lt (r, type, op1.upper_bound ());
1310 break;
1311
38a73435
AH
1312 default:
1313 break;
1314 }
1315 return true;
1316}
1317
1318
f544e7e8
AM
1319void
1320operator_gt::update_bitmask (irange &r, const irange &lh,
1321 const irange &rh) const
38a73435 1322{
f544e7e8
AM
1323 update_known_bitmask (r, GT_EXPR, lh, rh);
1324}
38a73435 1325
80dd13f5
AM
1326// Check if the LHS range indicates a relation between OP1 and OP2.
1327
ade5531c 1328relation_kind
f544e7e8 1329operator_gt::op1_op2_relation (const irange &lhs) const
80dd13f5
AM
1330{
1331 if (lhs.undefined_p ())
ade5531c 1332 return VREL_UNDEFINED;
80dd13f5
AM
1333
1334 // FALSE = op1 > op2 indicates LE_EXPR.
1335 if (lhs.zero_p ())
ade5531c 1336 return VREL_LE;
80dd13f5
AM
1337
1338 // TRUE = op1 > op2 indicates GT_EXPR.
cb779afe 1339 if (!contains_zero_p (lhs))
ade5531c
AM
1340 return VREL_GT;
1341 return VREL_VARYING;
80dd13f5
AM
1342}
1343
f674b4a7 1344bool
4ba9fb0a 1345operator_gt::fold_range (irange &r, tree type,
80dd13f5 1346 const irange &op1, const irange &op2,
b565ac19 1347 relation_trio rel) const
38a73435 1348{
ade5531c 1349 if (relop_early_resolve (r, type, op1, op2, rel, VREL_GT))
f674b4a7 1350 return true;
38a73435
AH
1351
1352 signop sign = TYPE_SIGN (op1.type ());
1353 gcc_checking_assert (sign == TYPE_SIGN (op2.type ()));
1354
1355 if (wi::gt_p (op1.lower_bound (), op2.upper_bound (), sign))
1356 r = range_true (type);
1357 else if (!wi::gt_p (op1.upper_bound (), op2.lower_bound (), sign))
1358 r = range_false (type);
1359 else
1360 r = range_true_and_false (type);
f674b4a7 1361 return true;
38a73435
AH
1362}
1363
1364bool
4ba9fb0a 1365operator_gt::op1_range (irange &r, tree type,
80dd13f5 1366 const irange &lhs, const irange &op2,
b565ac19 1367 relation_trio) const
38a73435 1368{
e753080a
JJ
1369 if (op2.undefined_p ())
1370 return false;
1371
38a73435
AH
1372 switch (get_bool_state (r, lhs, type))
1373 {
1374 case BRS_TRUE:
1375 build_gt (r, type, op2.lower_bound ());
1376 break;
1377
1378 case BRS_FALSE:
1379 build_le (r, type, op2.upper_bound ());
1380 break;
1381
1382 default:
1383 break;
1384 }
1385 return true;
1386}
1387
1388bool
4ba9fb0a
AH
1389operator_gt::op2_range (irange &r, tree type,
1390 const irange &lhs,
80dd13f5 1391 const irange &op1,
b565ac19 1392 relation_trio) const
38a73435 1393{
e753080a
JJ
1394 if (op1.undefined_p ())
1395 return false;
1396
38a73435
AH
1397 switch (get_bool_state (r, lhs, type))
1398 {
38a73435
AH
1399 case BRS_TRUE:
1400 build_lt (r, type, op1.upper_bound ());
1401 break;
1402
ad7cff63
AH
1403 case BRS_FALSE:
1404 build_ge (r, type, op1.lower_bound ());
1405 break;
1406
38a73435
AH
1407 default:
1408 break;
1409 }
1410 return true;
1411}
1412
1413
a0a8f1c7
AM
1414void
1415operator_ge::update_bitmask (irange &r, const irange &lh,
1416 const irange &rh) const
38a73435 1417{
a0a8f1c7
AM
1418 update_known_bitmask (r, GE_EXPR, lh, rh);
1419}
38a73435 1420
80dd13f5
AM
1421// Check if the LHS range indicates a relation between OP1 and OP2.
1422
ade5531c 1423relation_kind
a0a8f1c7 1424operator_ge::op1_op2_relation (const irange &lhs) const
80dd13f5
AM
1425{
1426 if (lhs.undefined_p ())
ade5531c 1427 return VREL_UNDEFINED;
80dd13f5
AM
1428
1429 // FALSE = op1 >= op2 indicates LT_EXPR.
1430 if (lhs.zero_p ())
ade5531c 1431 return VREL_LT;
80dd13f5
AM
1432
1433 // TRUE = op1 >= op2 indicates GE_EXPR.
cb779afe 1434 if (!contains_zero_p (lhs))
ade5531c
AM
1435 return VREL_GE;
1436 return VREL_VARYING;
80dd13f5
AM
1437}
1438
f674b4a7 1439bool
4ba9fb0a
AH
1440operator_ge::fold_range (irange &r, tree type,
1441 const irange &op1,
80dd13f5 1442 const irange &op2,
b565ac19 1443 relation_trio rel) const
38a73435 1444{
ade5531c 1445 if (relop_early_resolve (r, type, op1, op2, rel, VREL_GE))
f674b4a7 1446 return true;
38a73435
AH
1447
1448 signop sign = TYPE_SIGN (op1.type ());
1449 gcc_checking_assert (sign == TYPE_SIGN (op2.type ()));
1450
1451 if (wi::ge_p (op1.lower_bound (), op2.upper_bound (), sign))
1452 r = range_true (type);
1453 else if (!wi::ge_p (op1.upper_bound (), op2.lower_bound (), sign))
1454 r = range_false (type);
1455 else
1456 r = range_true_and_false (type);
f674b4a7 1457 return true;
38a73435
AH
1458}
1459
1460bool
4ba9fb0a
AH
1461operator_ge::op1_range (irange &r, tree type,
1462 const irange &lhs,
80dd13f5 1463 const irange &op2,
b565ac19 1464 relation_trio) const
38a73435 1465{
e753080a
JJ
1466 if (op2.undefined_p ())
1467 return false;
1468
38a73435
AH
1469 switch (get_bool_state (r, lhs, type))
1470 {
1471 case BRS_TRUE:
1472 build_ge (r, type, op2.lower_bound ());
1473 break;
1474
1475 case BRS_FALSE:
1476 build_lt (r, type, op2.upper_bound ());
1477 break;
1478
1479 default:
1480 break;
1481 }
1482 return true;
1483}
1484
1485bool
4ba9fb0a
AH
1486operator_ge::op2_range (irange &r, tree type,
1487 const irange &lhs,
80dd13f5 1488 const irange &op1,
b565ac19 1489 relation_trio) const
38a73435 1490{
e753080a
JJ
1491 if (op1.undefined_p ())
1492 return false;
1493
38a73435
AH
1494 switch (get_bool_state (r, lhs, type))
1495 {
38a73435
AH
1496 case BRS_TRUE:
1497 build_le (r, type, op1.upper_bound ());
1498 break;
1499
ad7cff63
AH
1500 case BRS_FALSE:
1501 build_gt (r, type, op1.lower_bound ());
1502 break;
1503
38a73435
AH
1504 default:
1505 break;
1506 }
1507 return true;
1508}
1509
1510
29dbd7ef
AM
1511void
1512operator_plus::update_bitmask (irange &r, const irange &lh,
1513 const irange &rh) const
38a73435 1514{
29dbd7ef
AM
1515 update_known_bitmask (r, PLUS_EXPR, lh, rh);
1516}
38a73435 1517
c526de3f
AM
1518// Check to see if the range of OP2 indicates anything about the relation
1519// between LHS and OP1.
1520
ade5531c 1521relation_kind
c526de3f
AM
1522operator_plus::lhs_op1_relation (const irange &lhs,
1523 const irange &op1,
cf2141a0
AM
1524 const irange &op2,
1525 relation_kind) const
c526de3f
AM
1526{
1527 if (lhs.undefined_p () || op1.undefined_p () || op2.undefined_p ())
ade5531c 1528 return VREL_VARYING;
c526de3f
AM
1529
1530 tree type = lhs.type ();
1531 unsigned prec = TYPE_PRECISION (type);
1532 wi::overflow_type ovf1, ovf2;
1533 signop sign = TYPE_SIGN (type);
1534
1535 // LHS = OP1 + 0 indicates LHS == OP1.
1536 if (op2.zero_p ())
ade5531c 1537 return VREL_EQ;
c526de3f
AM
1538
1539 if (TYPE_OVERFLOW_WRAPS (type))
1540 {
1541 wi::add (op1.lower_bound (), op2.lower_bound (), sign, &ovf1);
1542 wi::add (op1.upper_bound (), op2.upper_bound (), sign, &ovf2);
1543 }
1544 else
1545 ovf1 = ovf2 = wi::OVF_NONE;
1546
1547 // Never wrapping additions.
1548 if (!ovf1 && !ovf2)
1549 {
1550 // Positive op2 means lhs > op1.
1551 if (wi::gt_p (op2.lower_bound (), wi::zero (prec), sign))
ade5531c 1552 return VREL_GT;
c526de3f 1553 if (wi::ge_p (op2.lower_bound (), wi::zero (prec), sign))
ade5531c 1554 return VREL_GE;
c526de3f
AM
1555
1556 // Negative op2 means lhs < op1.
1557 if (wi::lt_p (op2.upper_bound (), wi::zero (prec), sign))
ade5531c 1558 return VREL_LT;
c526de3f 1559 if (wi::le_p (op2.upper_bound (), wi::zero (prec), sign))
ade5531c 1560 return VREL_LE;
c526de3f
AM
1561 }
1562 // Always wrapping additions.
1563 else if (ovf1 && ovf1 == ovf2)
1564 {
1565 // Positive op2 means lhs < op1.
1566 if (wi::gt_p (op2.lower_bound (), wi::zero (prec), sign))
ade5531c 1567 return VREL_LT;
c526de3f 1568 if (wi::ge_p (op2.lower_bound (), wi::zero (prec), sign))
ade5531c 1569 return VREL_LE;
c526de3f
AM
1570
1571 // Negative op2 means lhs > op1.
1572 if (wi::lt_p (op2.upper_bound (), wi::zero (prec), sign))
ade5531c 1573 return VREL_GT;
c526de3f 1574 if (wi::le_p (op2.upper_bound (), wi::zero (prec), sign))
ade5531c 1575 return VREL_GE;
c526de3f
AM
1576 }
1577
1578 // If op2 does not contain 0, then LHS and OP1 can never be equal.
1579 if (!range_includes_zero_p (&op2))
ade5531c 1580 return VREL_NE;
c526de3f 1581
ade5531c 1582 return VREL_VARYING;
c526de3f
AM
1583}
1584
1585// PLUS is symmetrical, so we can simply call lhs_op1_relation with reversed
1586// operands.
1587
ade5531c 1588relation_kind
c526de3f 1589operator_plus::lhs_op2_relation (const irange &lhs, const irange &op1,
cf2141a0 1590 const irange &op2, relation_kind rel) const
c526de3f 1591{
cf2141a0 1592 return lhs_op1_relation (lhs, op2, op1, rel);
c526de3f
AM
1593}
1594
bb74ef9e 1595void
4ba9fb0a 1596operator_plus::wi_fold (irange &r, tree type,
38a73435
AH
1597 const wide_int &lh_lb, const wide_int &lh_ub,
1598 const wide_int &rh_lb, const wide_int &rh_ub) const
1599{
1600 wi::overflow_type ov_lb, ov_ub;
1601 signop s = TYPE_SIGN (type);
1602 wide_int new_lb = wi::add (lh_lb, rh_lb, s, &ov_lb);
1603 wide_int new_ub = wi::add (lh_ub, rh_ub, s, &ov_ub);
bb74ef9e 1604 value_range_with_overflow (r, type, new_lb, new_ub, ov_lb, ov_ub);
38a73435
AH
1605}
1606
7ea258a1
AM
1607// Given addition or subtraction, determine the possible NORMAL ranges and
1608// OVERFLOW ranges given an OFFSET range. ADD_P is true for addition.
1609// Return the relation that exists between the LHS and OP1 in order for the
1610// NORMAL range to apply.
1611// a return value of VREL_VARYING means no ranges were applicable.
1612
1613static relation_kind
1614plus_minus_ranges (irange &r_ov, irange &r_normal, const irange &offset,
1615 bool add_p)
1616{
1617 relation_kind kind = VREL_VARYING;
1618 // For now, only deal with constant adds. This could be extended to ranges
1619 // when someone is so motivated.
1620 if (!offset.singleton_p () || offset.zero_p ())
1621 return kind;
1622
1623 // Always work with a positive offset. ie a+ -2 -> a-2 and a- -2 > a+2
1624 wide_int off = offset.lower_bound ();
1625 if (wi::neg_p (off, SIGNED))
1626 {
1627 add_p = !add_p;
1628 off = wi::neg (off);
1629 }
1630
1631 wi::overflow_type ov;
1632 tree type = offset.type ();
1633 unsigned prec = TYPE_PRECISION (type);
1634 wide_int ub;
1635 wide_int lb;
1636 // calculate the normal range and relation for the operation.
1637 if (add_p)
1638 {
1639 // [ 0 , INF - OFF]
1640 lb = wi::zero (prec);
8b2181a4 1641 ub = wi::sub (irange_val_max (type), off, UNSIGNED, &ov);
7ea258a1
AM
1642 kind = VREL_GT;
1643 }
1644 else
1645 {
1646 // [ OFF, INF ]
1647 lb = off;
8b2181a4 1648 ub = irange_val_max (type);
7ea258a1
AM
1649 kind = VREL_LT;
1650 }
1651 int_range<2> normal_range (type, lb, ub);
1652 int_range<2> ov_range (type, lb, ub, VR_ANTI_RANGE);
1653
1654 r_ov = ov_range;
1655 r_normal = normal_range;
1656 return kind;
1657}
1658
1659// Once op1 has been calculated by operator_plus or operator_minus, check
1660// to see if the relation passed causes any part of the calculation to
1661// be not possible. ie
1662// a_2 = b_3 + 1 with a_2 < b_3 can refine the range of b_3 to [INF, INF]
1663// and that further refines a_2 to [0, 0].
1664// R is the value of op1, OP2 is the offset being added/subtracted, REL is the
c46b5b0a 1665// relation between LHS relation OP1 and ADD_P is true for PLUS, false for
7ea258a1
AM
1666// MINUS. IF any adjustment can be made, R will reflect it.
1667
1668static void
1669adjust_op1_for_overflow (irange &r, const irange &op2, relation_kind rel,
1670 bool add_p)
1671{
f41d1b39
AM
1672 if (r.undefined_p ())
1673 return;
7ea258a1
AM
1674 tree type = r.type ();
1675 // Check for unsigned overflow and calculate the overflow part.
1676 signop s = TYPE_SIGN (type);
1677 if (!TYPE_OVERFLOW_WRAPS (type) || s == SIGNED)
1678 return;
1679
1680 // Only work with <, <=, >, >= relations.
1681 if (!relation_lt_le_gt_ge_p (rel))
1682 return;
1683
1684 // Get the ranges for this offset.
1685 int_range_max normal, overflow;
1686 relation_kind k = plus_minus_ranges (overflow, normal, op2, add_p);
1687
1688 // VREL_VARYING means there are no adjustments.
1689 if (k == VREL_VARYING)
1690 return;
1691
1692 // If the relations match use the normal range, otherwise use overflow range.
1693 if (relation_intersect (k, rel) == k)
1694 r.intersect (normal);
1695 else
1696 r.intersect (overflow);
1697 return;
1698}
1699
38a73435 1700bool
4ba9fb0a
AH
1701operator_plus::op1_range (irange &r, tree type,
1702 const irange &lhs,
80dd13f5 1703 const irange &op2,
b565ac19 1704 relation_trio trio) const
38a73435 1705{
7ea258a1
AM
1706 if (lhs.undefined_p ())
1707 return false;
1708 // Start with the default operation.
2eb50117 1709 range_op_handler minus (MINUS_EXPR);
7ea258a1
AM
1710 if (!minus)
1711 return false;
1712 bool res = minus.fold_range (r, type, lhs, op2);
99fda5de 1713 relation_kind rel = trio.lhs_op1 ();
7ea258a1
AM
1714 // Check for a relation refinement.
1715 if (res)
1716 adjust_op1_for_overflow (r, op2, rel, true /* PLUS_EXPR */);
1717 return res;
38a73435
AH
1718}
1719
1720bool
4ba9fb0a
AH
1721operator_plus::op2_range (irange &r, tree type,
1722 const irange &lhs,
80dd13f5 1723 const irange &op1,
b565ac19 1724 relation_trio rel) const
38a73435 1725{
b565ac19 1726 return op1_range (r, type, lhs, op1, rel.swap_op1_op2 ());
38a73435
AH
1727}
1728
03c6ba86
TC
1729class operator_widen_plus_signed : public range_operator
1730{
1731public:
1732 virtual void wi_fold (irange &r, tree type,
1733 const wide_int &lh_lb,
1734 const wide_int &lh_ub,
1735 const wide_int &rh_lb,
1736 const wide_int &rh_ub) const;
1737} op_widen_plus_signed;
03c6ba86
TC
1738
1739void
1740operator_widen_plus_signed::wi_fold (irange &r, tree type,
1741 const wide_int &lh_lb,
1742 const wide_int &lh_ub,
1743 const wide_int &rh_lb,
1744 const wide_int &rh_ub) const
1745{
1746 wi::overflow_type ov_lb, ov_ub;
1747 signop s = TYPE_SIGN (type);
1748
1749 wide_int lh_wlb
1750 = wide_int::from (lh_lb, wi::get_precision (lh_lb) * 2, SIGNED);
1751 wide_int lh_wub
1752 = wide_int::from (lh_ub, wi::get_precision (lh_ub) * 2, SIGNED);
1753 wide_int rh_wlb = wide_int::from (rh_lb, wi::get_precision (rh_lb) * 2, s);
1754 wide_int rh_wub = wide_int::from (rh_ub, wi::get_precision (rh_ub) * 2, s);
1755
1756 wide_int new_lb = wi::add (lh_wlb, rh_wlb, s, &ov_lb);
1757 wide_int new_ub = wi::add (lh_wub, rh_wub, s, &ov_ub);
1758
1759 r = int_range<2> (type, new_lb, new_ub);
1760}
1761
1762class operator_widen_plus_unsigned : public range_operator
1763{
1764public:
1765 virtual void wi_fold (irange &r, tree type,
1766 const wide_int &lh_lb,
1767 const wide_int &lh_ub,
1768 const wide_int &rh_lb,
1769 const wide_int &rh_ub) const;
1770} op_widen_plus_unsigned;
03c6ba86
TC
1771
1772void
1773operator_widen_plus_unsigned::wi_fold (irange &r, tree type,
1774 const wide_int &lh_lb,
1775 const wide_int &lh_ub,
1776 const wide_int &rh_lb,
1777 const wide_int &rh_ub) const
1778{
1779 wi::overflow_type ov_lb, ov_ub;
1780 signop s = TYPE_SIGN (type);
1781
1782 wide_int lh_wlb
1783 = wide_int::from (lh_lb, wi::get_precision (lh_lb) * 2, UNSIGNED);
1784 wide_int lh_wub
1785 = wide_int::from (lh_ub, wi::get_precision (lh_ub) * 2, UNSIGNED);
1786 wide_int rh_wlb = wide_int::from (rh_lb, wi::get_precision (rh_lb) * 2, s);
1787 wide_int rh_wub = wide_int::from (rh_ub, wi::get_precision (rh_ub) * 2, s);
1788
1789 wide_int new_lb = wi::add (lh_wlb, rh_wlb, s, &ov_lb);
1790 wide_int new_ub = wi::add (lh_wub, rh_wub, s, &ov_ub);
1791
1792 r = int_range<2> (type, new_lb, new_ub);
1793}
38a73435 1794
d5818a36
AM
1795void
1796operator_minus::update_bitmask (irange &r, const irange &lh,
1797 const irange &rh) const
38a73435 1798{
d5818a36
AM
1799 update_known_bitmask (r, MINUS_EXPR, lh, rh);
1800}
38a73435 1801
bb74ef9e 1802void
4ba9fb0a 1803operator_minus::wi_fold (irange &r, tree type,
38a73435
AH
1804 const wide_int &lh_lb, const wide_int &lh_ub,
1805 const wide_int &rh_lb, const wide_int &rh_ub) const
1806{
1807 wi::overflow_type ov_lb, ov_ub;
1808 signop s = TYPE_SIGN (type);
1809 wide_int new_lb = wi::sub (lh_lb, rh_ub, s, &ov_lb);
1810 wide_int new_ub = wi::sub (lh_ub, rh_lb, s, &ov_ub);
bb74ef9e 1811 value_range_with_overflow (r, type, new_lb, new_ub, ov_lb, ov_ub);
38a73435
AH
1812}
1813
cf2141a0
AM
1814
1815// Return the relation between LHS and OP1 based on the relation between
1816// OP1 and OP2.
1817
ade5531c 1818relation_kind
e97e9929 1819operator_minus::lhs_op1_relation (const irange &, const irange &op1,
cf2141a0
AM
1820 const irange &, relation_kind rel) const
1821{
e97e9929 1822 if (!op1.undefined_p () && TYPE_SIGN (op1.type ()) == UNSIGNED)
cf2141a0
AM
1823 switch (rel)
1824 {
ade5531c 1825 case VREL_GT:
ade5531c
AM
1826 case VREL_GE:
1827 return VREL_LE;
cf2141a0
AM
1828 default:
1829 break;
1830 }
ade5531c 1831 return VREL_VARYING;
cf2141a0
AM
1832}
1833
ae6b830f 1834// Check to see if the relation REL between OP1 and OP2 has any effect on the
8af8abfb
AH
1835// LHS of the expression. If so, apply it to LHS_RANGE. This is a helper
1836// function for both MINUS_EXPR and POINTER_DIFF_EXPR.
ae6b830f 1837
f6e160e3 1838bool
8af8abfb
AH
1839minus_op1_op2_relation_effect (irange &lhs_range, tree type,
1840 const irange &op1_range ATTRIBUTE_UNUSED,
1841 const irange &op2_range ATTRIBUTE_UNUSED,
1842 relation_kind rel)
ae6b830f 1843{
ade5531c 1844 if (rel == VREL_VARYING)
ae6b830f
AM
1845 return false;
1846
1847 int_range<2> rel_range;
1848 unsigned prec = TYPE_PRECISION (type);
1849 signop sgn = TYPE_SIGN (type);
1850
a96d8d67 1851 // == and != produce [0,0] and ~[0,0] regardless of wrapping.
ade5531c 1852 if (rel == VREL_EQ)
a96d8d67 1853 rel_range = int_range<2> (type, wi::zero (prec), wi::zero (prec));
ade5531c 1854 else if (rel == VREL_NE)
a96d8d67
AM
1855 rel_range = int_range<2> (type, wi::zero (prec), wi::zero (prec),
1856 VR_ANTI_RANGE);
1857 else if (TYPE_OVERFLOW_WRAPS (type))
ae6b830f 1858 {
a96d8d67
AM
1859 switch (rel)
1860 {
1861 // For wrapping signed values and unsigned, if op1 > op2 or
1862 // op1 < op2, then op1 - op2 can be restricted to ~[0, 0].
ade5531c
AM
1863 case VREL_GT:
1864 case VREL_LT:
a96d8d67
AM
1865 rel_range = int_range<2> (type, wi::zero (prec), wi::zero (prec),
1866 VR_ANTI_RANGE);
1867 break;
1868 default:
1869 return false;
1870 }
1871 }
1872 else
1873 {
1874 switch (rel)
1875 {
1876 // op1 > op2, op1 - op2 can be restricted to [1, +INF]
ade5531c 1877 case VREL_GT:
a96d8d67
AM
1878 rel_range = int_range<2> (type, wi::one (prec),
1879 wi::max_value (prec, sgn));
1880 break;
1881 // op1 >= op2, op1 - op2 can be restricted to [0, +INF]
ade5531c 1882 case VREL_GE:
a96d8d67
AM
1883 rel_range = int_range<2> (type, wi::zero (prec),
1884 wi::max_value (prec, sgn));
1885 break;
1886 // op1 < op2, op1 - op2 can be restricted to [-INF, -1]
ade5531c 1887 case VREL_LT:
a96d8d67
AM
1888 rel_range = int_range<2> (type, wi::min_value (prec, sgn),
1889 wi::minus_one (prec));
1890 break;
1891 // op1 <= op2, op1 - op2 can be restricted to [-INF, 0]
ade5531c 1892 case VREL_LE:
a96d8d67
AM
1893 rel_range = int_range<2> (type, wi::min_value (prec, sgn),
1894 wi::zero (prec));
1895 break;
1896 default:
1897 return false;
1898 }
ae6b830f
AM
1899 }
1900 lhs_range.intersect (rel_range);
1901 return true;
1902}
1903
8af8abfb
AH
1904bool
1905operator_minus::op1_op2_relation_effect (irange &lhs_range, tree type,
1906 const irange &op1_range,
1907 const irange &op2_range,
1908 relation_kind rel) const
1909{
1910 return minus_op1_op2_relation_effect (lhs_range, type, op1_range, op2_range,
1911 rel);
1912}
1913
38a73435 1914bool
4ba9fb0a
AH
1915operator_minus::op1_range (irange &r, tree type,
1916 const irange &lhs,
80dd13f5 1917 const irange &op2,
b565ac19 1918 relation_trio trio) const
38a73435 1919{
7ea258a1
AM
1920 if (lhs.undefined_p ())
1921 return false;
1922 // Start with the default operation.
2eb50117 1923 range_op_handler minus (PLUS_EXPR);
7ea258a1
AM
1924 if (!minus)
1925 return false;
1926 bool res = minus.fold_range (r, type, lhs, op2);
99fda5de 1927 relation_kind rel = trio.lhs_op1 ();
7ea258a1
AM
1928 if (res)
1929 adjust_op1_for_overflow (r, op2, rel, false /* PLUS_EXPR */);
1930 return res;
1931
38a73435
AH
1932}
1933
1934bool
4ba9fb0a
AH
1935operator_minus::op2_range (irange &r, tree type,
1936 const irange &lhs,
80dd13f5 1937 const irange &op1,
b565ac19 1938 relation_trio) const
38a73435 1939{
ef9bc362
AM
1940 if (lhs.undefined_p ())
1941 return false;
f674b4a7 1942 return fold_range (r, type, op1, lhs);
38a73435
AH
1943}
1944
b08b9825
AM
1945void
1946operator_min::update_bitmask (irange &r, const irange &lh,
1947 const irange &rh) const
38a73435 1948{
b08b9825
AM
1949 update_known_bitmask (r, MIN_EXPR, lh, rh);
1950}
38a73435 1951
bb74ef9e 1952void
4ba9fb0a 1953operator_min::wi_fold (irange &r, tree type,
38a73435
AH
1954 const wide_int &lh_lb, const wide_int &lh_ub,
1955 const wide_int &rh_lb, const wide_int &rh_ub) const
1956{
1957 signop s = TYPE_SIGN (type);
1958 wide_int new_lb = wi::min (lh_lb, rh_lb, s);
1959 wide_int new_ub = wi::min (lh_ub, rh_ub, s);
bb74ef9e 1960 value_range_with_overflow (r, type, new_lb, new_ub);
38a73435
AH
1961}
1962
1963
f0278eb0
AM
1964void
1965operator_max::update_bitmask (irange &r, const irange &lh,
1966 const irange &rh) const
38a73435 1967{
f0278eb0
AM
1968 update_known_bitmask (r, MAX_EXPR, lh, rh);
1969}
38a73435 1970
bb74ef9e 1971void
4ba9fb0a 1972operator_max::wi_fold (irange &r, tree type,
38a73435
AH
1973 const wide_int &lh_lb, const wide_int &lh_ub,
1974 const wide_int &rh_lb, const wide_int &rh_ub) const
1975{
1976 signop s = TYPE_SIGN (type);
1977 wide_int new_lb = wi::max (lh_lb, rh_lb, s);
1978 wide_int new_ub = wi::max (lh_ub, rh_ub, s);
bb74ef9e 1979 value_range_with_overflow (r, type, new_lb, new_ub);
38a73435
AH
1980}
1981
1982
38a73435
AH
1983// Calculate the cross product of two sets of ranges and return it.
1984//
1985// Multiplications, divisions and shifts are a bit tricky to handle,
1986// depending on the mix of signs we have in the two ranges, we need to
1987// operate on different values to get the minimum and maximum values
1988// for the new range. One approach is to figure out all the
1989// variations of range combinations and do the operations.
1990//
1991// However, this involves several calls to compare_values and it is
1992// pretty convoluted. It's simpler to do the 4 operations (MIN0 OP
1993// MIN1, MIN0 OP MAX1, MAX0 OP MIN1 and MAX0 OP MAX0 OP MAX1) and then
1994// figure the smallest and largest values to form the new range.
1995
bb74ef9e 1996void
4ba9fb0a 1997cross_product_operator::wi_cross_product (irange &r, tree type,
38a73435
AH
1998 const wide_int &lh_lb,
1999 const wide_int &lh_ub,
2000 const wide_int &rh_lb,
2001 const wide_int &rh_ub) const
2002{
2003 wide_int cp1, cp2, cp3, cp4;
bb74ef9e 2004 // Default to varying.
4ba9fb0a 2005 r.set_varying (type);
38a73435
AH
2006
2007 // Compute the 4 cross operations, bailing if we get an overflow we
2008 // can't handle.
2009 if (wi_op_overflows (cp1, type, lh_lb, rh_lb))
bb74ef9e 2010 return;
38a73435
AH
2011 if (wi::eq_p (lh_lb, lh_ub))
2012 cp3 = cp1;
2013 else if (wi_op_overflows (cp3, type, lh_ub, rh_lb))
bb74ef9e 2014 return;
38a73435
AH
2015 if (wi::eq_p (rh_lb, rh_ub))
2016 cp2 = cp1;
2017 else if (wi_op_overflows (cp2, type, lh_lb, rh_ub))
bb74ef9e 2018 return;
38a73435
AH
2019 if (wi::eq_p (lh_lb, lh_ub))
2020 cp4 = cp2;
2021 else if (wi_op_overflows (cp4, type, lh_ub, rh_ub))
bb74ef9e 2022 return;
38a73435
AH
2023
2024 // Order pairs.
2025 signop sign = TYPE_SIGN (type);
2026 if (wi::gt_p (cp1, cp2, sign))
2027 std::swap (cp1, cp2);
2028 if (wi::gt_p (cp3, cp4, sign))
2029 std::swap (cp3, cp4);
2030
2031 // Choose min and max from the ordered pairs.
2032 wide_int res_lb = wi::min (cp1, cp3, sign);
2033 wide_int res_ub = wi::max (cp2, cp4, sign);
bb74ef9e 2034 value_range_with_overflow (r, type, res_lb, res_ub);
38a73435
AH
2035}
2036
2037
a13c4440
AM
2038void
2039operator_mult::update_bitmask (irange &r, const irange &lh,
2040 const irange &rh) const
38a73435 2041{
a13c4440
AM
2042 update_known_bitmask (r, MULT_EXPR, lh, rh);
2043}
38a73435 2044
4ba9fb0a
AH
2045bool
2046operator_mult::op1_range (irange &r, tree type,
80dd13f5 2047 const irange &lhs, const irange &op2,
b565ac19 2048 relation_trio) const
4ba9fb0a 2049{
ef9bc362
AM
2050 if (lhs.undefined_p ())
2051 return false;
4ba9fb0a
AH
2052
2053 // We can't solve 0 = OP1 * N by dividing by N with a wrapping type.
2054 // For example: For 0 = OP1 * 2, OP1 could be 0, or MAXINT, whereas
2055 // for 4 = OP1 * 2, OP1 could be 2 or 130 (unsigned 8-bit)
2056 if (TYPE_OVERFLOW_WRAPS (type))
2057 return false;
2058
cb779afe
AH
2059 wide_int offset;
2060 if (op2.singleton_p (offset) && offset != 0)
2eb50117 2061 return range_op_handler (TRUNC_DIV_EXPR).fold_range (r, type, lhs, op2);
4ba9fb0a
AH
2062 return false;
2063}
2064
2065bool
2066operator_mult::op2_range (irange &r, tree type,
80dd13f5 2067 const irange &lhs, const irange &op1,
b565ac19 2068 relation_trio rel) const
4ba9fb0a 2069{
b565ac19 2070 return operator_mult::op1_range (r, type, lhs, op1, rel.swap_op1_op2 ());
4ba9fb0a
AH
2071}
2072
38a73435 2073bool
028d81b1
AH
2074operator_mult::wi_op_overflows (wide_int &res, tree type,
2075 const wide_int &w0, const wide_int &w1) const
38a73435
AH
2076{
2077 wi::overflow_type overflow = wi::OVF_NONE;
2078 signop sign = TYPE_SIGN (type);
2079 res = wi::mul (w0, w1, sign, &overflow);
2080 if (overflow && TYPE_OVERFLOW_UNDEFINED (type))
2081 {
2082 // For multiplication, the sign of the overflow is given
2083 // by the comparison of the signs of the operands.
2084 if (sign == UNSIGNED || w0.sign_mask () == w1.sign_mask ())
2085 res = wi::max_value (w0.get_precision (), sign);
2086 else
2087 res = wi::min_value (w0.get_precision (), sign);
2088 return false;
2089 }
2090 return overflow;
2091}
2092
bb74ef9e 2093void
4ba9fb0a 2094operator_mult::wi_fold (irange &r, tree type,
38a73435
AH
2095 const wide_int &lh_lb, const wide_int &lh_ub,
2096 const wide_int &rh_lb, const wide_int &rh_ub) const
2097{
2098 if (TYPE_OVERFLOW_UNDEFINED (type))
bb74ef9e
AM
2099 {
2100 wi_cross_product (r, type, lh_lb, lh_ub, rh_lb, rh_ub);
2101 return;
2102 }
38a73435
AH
2103
2104 // Multiply the ranges when overflow wraps. This is basically fancy
2105 // code so we don't drop to varying with an unsigned
2106 // [-3,-1]*[-3,-1].
2107 //
2108 // This test requires 2*prec bits if both operands are signed and
2109 // 2*prec + 2 bits if either is not. Therefore, extend the values
2110 // using the sign of the result to PREC2. From here on out,
c46b5b0a 2111 // everything is just signed math no matter what the input types
38a73435
AH
2112 // were.
2113
2114 signop sign = TYPE_SIGN (type);
2115 unsigned prec = TYPE_PRECISION (type);
2116 widest2_int min0 = widest2_int::from (lh_lb, sign);
2117 widest2_int max0 = widest2_int::from (lh_ub, sign);
2118 widest2_int min1 = widest2_int::from (rh_lb, sign);
2119 widest2_int max1 = widest2_int::from (rh_ub, sign);
2120 widest2_int sizem1 = wi::mask <widest2_int> (prec, false);
2121 widest2_int size = sizem1 + 1;
2122
2123 // Canonicalize the intervals.
2124 if (sign == UNSIGNED)
2125 {
2126 if (wi::ltu_p (size, min0 + max0))
2127 {
2128 min0 -= size;
2129 max0 -= size;
2130 }
2131 if (wi::ltu_p (size, min1 + max1))
2132 {
2133 min1 -= size;
2134 max1 -= size;
2135 }
2136 }
2137
2138 // Sort the 4 products so that min is in prod0 and max is in
2139 // prod3.
2140 widest2_int prod0 = min0 * min1;
2141 widest2_int prod1 = min0 * max1;
2142 widest2_int prod2 = max0 * min1;
2143 widest2_int prod3 = max0 * max1;
2144
2145 // min0min1 > max0max1
2146 if (prod0 > prod3)
2147 std::swap (prod0, prod3);
2148
2149 // min0max1 > max0min1
2150 if (prod1 > prod2)
2151 std::swap (prod1, prod2);
2152
2153 if (prod0 > prod1)
2154 std::swap (prod0, prod1);
2155
2156 if (prod2 > prod3)
2157 std::swap (prod2, prod3);
2158
2159 // diff = max - min
2160 prod2 = prod3 - prod0;
2161 if (wi::geu_p (prod2, sizem1))
a239a63f
AH
2162 {
2163 // Multiplying by X, where X is a power of 2 is [0,0][X,+INF].
2164 if (TYPE_UNSIGNED (type) && rh_lb == rh_ub
2165 && wi::exact_log2 (rh_lb) != -1 && prec > 1)
2166 {
2167 r.set (type, rh_lb, wi::max_value (prec, sign));
2168 int_range<2> zero;
2169 zero.set_zero (type);
2170 r.union_ (zero);
2171 }
2172 else
2173 // The range covers all values.
2174 r.set_varying (type);
2175 }
bb74ef9e
AM
2176 else
2177 {
2178 wide_int new_lb = wide_int::from (prod0, prec, sign);
2179 wide_int new_ub = wide_int::from (prod3, prec, sign);
2180 create_possibly_reversed_range (r, type, new_lb, new_ub);
2181 }
38a73435
AH
2182}
2183
03c6ba86
TC
2184class operator_widen_mult_signed : public range_operator
2185{
2186public:
2187 virtual void wi_fold (irange &r, tree type,
2188 const wide_int &lh_lb,
2189 const wide_int &lh_ub,
2190 const wide_int &rh_lb,
2191 const wide_int &rh_ub)
2192 const;
2193} op_widen_mult_signed;
03c6ba86
TC
2194
2195void
2196operator_widen_mult_signed::wi_fold (irange &r, tree type,
2197 const wide_int &lh_lb,
2198 const wide_int &lh_ub,
2199 const wide_int &rh_lb,
2200 const wide_int &rh_ub) const
2201{
2202 signop s = TYPE_SIGN (type);
2203
2204 wide_int lh_wlb = wide_int::from (lh_lb, wi::get_precision (lh_lb) * 2, SIGNED);
2205 wide_int lh_wub = wide_int::from (lh_ub, wi::get_precision (lh_ub) * 2, SIGNED);
2206 wide_int rh_wlb = wide_int::from (rh_lb, wi::get_precision (rh_lb) * 2, s);
2207 wide_int rh_wub = wide_int::from (rh_ub, wi::get_precision (rh_ub) * 2, s);
2208
2209 /* We don't expect a widening multiplication to be able to overflow but range
2210 calculations for multiplications are complicated. After widening the
2211 operands lets call the base class. */
2212 return op_mult.wi_fold (r, type, lh_wlb, lh_wub, rh_wlb, rh_wub);
2213}
2214
2215
2216class operator_widen_mult_unsigned : public range_operator
2217{
2218public:
2219 virtual void wi_fold (irange &r, tree type,
2220 const wide_int &lh_lb,
2221 const wide_int &lh_ub,
2222 const wide_int &rh_lb,
2223 const wide_int &rh_ub)
2224 const;
2225} op_widen_mult_unsigned;
03c6ba86
TC
2226
2227void
2228operator_widen_mult_unsigned::wi_fold (irange &r, tree type,
2229 const wide_int &lh_lb,
2230 const wide_int &lh_ub,
2231 const wide_int &rh_lb,
2232 const wide_int &rh_ub) const
2233{
2234 signop s = TYPE_SIGN (type);
2235
2236 wide_int lh_wlb = wide_int::from (lh_lb, wi::get_precision (lh_lb) * 2, UNSIGNED);
2237 wide_int lh_wub = wide_int::from (lh_ub, wi::get_precision (lh_ub) * 2, UNSIGNED);
2238 wide_int rh_wlb = wide_int::from (rh_lb, wi::get_precision (rh_lb) * 2, s);
2239 wide_int rh_wub = wide_int::from (rh_ub, wi::get_precision (rh_ub) * 2, s);
2240
2241 /* We don't expect a widening multiplication to be able to overflow but range
2242 calculations for multiplications are complicated. After widening the
2243 operands lets call the base class. */
2244 return op_mult.wi_fold (r, type, lh_wlb, lh_wub, rh_wlb, rh_wub);
2245}
38a73435
AH
2246
2247class operator_div : public cross_product_operator
2248{
2249public:
cd4b7e8b 2250 operator_div (tree_code div_kind) { m_code = div_kind; }
4ba9fb0a 2251 virtual void wi_fold (irange &r, tree type,
bb74ef9e
AM
2252 const wide_int &lh_lb,
2253 const wide_int &lh_ub,
2254 const wide_int &rh_lb,
33dc1bac 2255 const wide_int &rh_ub) const final override;
028d81b1 2256 virtual bool wi_op_overflows (wide_int &res, tree type,
33dc1bac
ML
2257 const wide_int &, const wide_int &)
2258 const final override;
cd4b7e8b
AM
2259 void update_bitmask (irange &r, const irange &lh, const irange &rh) const
2260 { update_known_bitmask (r, m_code, lh, rh); }
2261protected:
2262 tree_code m_code;
38a73435
AH
2263};
2264
cd4b7e8b
AM
2265static operator_div op_trunc_div (TRUNC_DIV_EXPR);
2266static operator_div op_floor_div (FLOOR_DIV_EXPR);
2267static operator_div op_round_div (ROUND_DIV_EXPR);
2268static operator_div op_ceil_div (CEIL_DIV_EXPR);
2269
38a73435 2270bool
028d81b1
AH
2271operator_div::wi_op_overflows (wide_int &res, tree type,
2272 const wide_int &w0, const wide_int &w1) const
38a73435
AH
2273{
2274 if (w1 == 0)
2275 return true;
2276
2277 wi::overflow_type overflow = wi::OVF_NONE;
2278 signop sign = TYPE_SIGN (type);
2279
b3e8dc87 2280 switch (m_code)
38a73435
AH
2281 {
2282 case EXACT_DIV_EXPR:
38a73435
AH
2283 case TRUNC_DIV_EXPR:
2284 res = wi::div_trunc (w0, w1, sign, &overflow);
2285 break;
2286 case FLOOR_DIV_EXPR:
2287 res = wi::div_floor (w0, w1, sign, &overflow);
2288 break;
2289 case ROUND_DIV_EXPR:
2290 res = wi::div_round (w0, w1, sign, &overflow);
2291 break;
2292 case CEIL_DIV_EXPR:
2293 res = wi::div_ceil (w0, w1, sign, &overflow);
2294 break;
2295 default:
2296 gcc_unreachable ();
2297 }
2298
2299 if (overflow && TYPE_OVERFLOW_UNDEFINED (type))
2300 {
2301 // For division, the only case is -INF / -1 = +INF.
2302 res = wi::max_value (w0.get_precision (), sign);
2303 return false;
2304 }
2305 return overflow;
2306}
2307
bb74ef9e 2308void
4ba9fb0a 2309operator_div::wi_fold (irange &r, tree type,
38a73435
AH
2310 const wide_int &lh_lb, const wide_int &lh_ub,
2311 const wide_int &rh_lb, const wide_int &rh_ub) const
2312{
38a73435
AH
2313 const wide_int dividend_min = lh_lb;
2314 const wide_int dividend_max = lh_ub;
2315 const wide_int divisor_min = rh_lb;
2316 const wide_int divisor_max = rh_ub;
2317 signop sign = TYPE_SIGN (type);
2318 unsigned prec = TYPE_PRECISION (type);
2319 wide_int extra_min, extra_max;
2320
2321 // If we know we won't divide by zero, just do the division.
2322 if (!wi_includes_zero_p (type, divisor_min, divisor_max))
bb74ef9e
AM
2323 {
2324 wi_cross_product (r, type, dividend_min, dividend_max,
2325 divisor_min, divisor_max);
2326 return;
2327 }
38a73435 2328
38a73435
AH
2329 // If we're definitely dividing by zero, there's nothing to do.
2330 if (wi_zero_p (type, divisor_min, divisor_max))
bb74ef9e 2331 {
ebbcdd7f 2332 r.set_undefined ();
bb74ef9e
AM
2333 return;
2334 }
38a73435
AH
2335
2336 // Perform the division in 2 parts, [LB, -1] and [1, UB], which will
2337 // skip any division by zero.
2338
2339 // First divide by the negative numbers, if any.
38a73435 2340 if (wi::neg_p (divisor_min, sign))
bb74ef9e
AM
2341 wi_cross_product (r, type, dividend_min, dividend_max,
2342 divisor_min, wi::minus_one (prec));
2343 else
4ba9fb0a 2344 r.set_undefined ();
bb74ef9e 2345
38a73435
AH
2346 // Then divide by the non-zero positive numbers, if any.
2347 if (wi::gt_p (divisor_max, wi::zero (prec), sign))
2348 {
c5a6c223 2349 int_range_max tmp;
bb74ef9e
AM
2350 wi_cross_product (tmp, type, dividend_min, dividend_max,
2351 wi::one (prec), divisor_max);
38a73435
AH
2352 r.union_ (tmp);
2353 }
bb74ef9e
AM
2354 // We shouldn't still have undefined here.
2355 gcc_checking_assert (!r.undefined_p ());
38a73435
AH
2356}
2357
38a73435
AH
2358
2359class operator_exact_divide : public operator_div
2360{
cf5bea76 2361 using range_operator::op1_range;
38a73435 2362public:
cd4b7e8b 2363 operator_exact_divide () : operator_div (EXACT_DIV_EXPR) { }
4ba9fb0a
AH
2364 virtual bool op1_range (irange &r, tree type,
2365 const irange &lhs,
80dd13f5 2366 const irange &op2,
b565ac19 2367 relation_trio) const;
38a73435
AH
2368
2369} op_exact_div;
2370
2371bool
4ba9fb0a
AH
2372operator_exact_divide::op1_range (irange &r, tree type,
2373 const irange &lhs,
80dd13f5 2374 const irange &op2,
b565ac19 2375 relation_trio) const
38a73435 2376{
ef9bc362
AM
2377 if (lhs.undefined_p ())
2378 return false;
cb779afe 2379 wide_int offset;
38a73435
AH
2380 // [2, 4] = op1 / [3,3] since its exact divide, no need to worry about
2381 // remainders in the endpoints, so op1 = [2,4] * [3,3] = [6,12].
2382 // We wont bother trying to enumerate all the in between stuff :-P
c46b5b0a 2383 // TRUE accuracy is [6,6][9,9][12,12]. This is unlikely to matter most of
38a73435
AH
2384 // the time however.
2385 // If op2 is a multiple of 2, we would be able to set some non-zero bits.
cb779afe 2386 if (op2.singleton_p (offset) && offset != 0)
2eb50117 2387 return range_op_handler (MULT_EXPR).fold_range (r, type, lhs, op2);
38a73435
AH
2388 return false;
2389}
2390
2391
2392class operator_lshift : public cross_product_operator
2393{
cf5bea76
AH
2394 using range_operator::fold_range;
2395 using range_operator::op1_range;
38a73435 2396public:
4ba9fb0a
AH
2397 virtual bool op1_range (irange &r, tree type,
2398 const irange &lhs,
80dd13f5 2399 const irange &op2,
b565ac19 2400 relation_trio rel = TRIO_VARYING) const;
4ba9fb0a
AH
2401 virtual bool fold_range (irange &r, tree type,
2402 const irange &op1,
80dd13f5 2403 const irange &op2,
b565ac19 2404 relation_trio rel = TRIO_VARYING) const;
4ba9fb0a
AH
2405
2406 virtual void wi_fold (irange &r, tree type,
bb74ef9e
AM
2407 const wide_int &lh_lb, const wide_int &lh_ub,
2408 const wide_int &rh_lb, const wide_int &rh_ub) const;
38a73435
AH
2409 virtual bool wi_op_overflows (wide_int &res,
2410 tree type,
2411 const wide_int &,
2412 const wide_int &) const;
0ddc8c78
AM
2413 void update_bitmask (irange &r, const irange &lh,
2414 const irange &rh) const final override
cd4b7e8b 2415 { update_known_bitmask (r, LSHIFT_EXPR, lh, rh); }
38a73435
AH
2416} op_lshift;
2417
bd431d26
AH
2418class operator_rshift : public cross_product_operator
2419{
cf5bea76
AH
2420 using range_operator::fold_range;
2421 using range_operator::op1_range;
2422 using range_operator::lhs_op1_relation;
bd431d26
AH
2423public:
2424 virtual bool fold_range (irange &r, tree type,
2425 const irange &op1,
80dd13f5 2426 const irange &op2,
b565ac19 2427 relation_trio rel = TRIO_VARYING) const;
bd431d26
AH
2428 virtual void wi_fold (irange &r, tree type,
2429 const wide_int &lh_lb,
2430 const wide_int &lh_ub,
2431 const wide_int &rh_lb,
2432 const wide_int &rh_ub) const;
2433 virtual bool wi_op_overflows (wide_int &res,
2434 tree type,
2435 const wide_int &w0,
2436 const wide_int &w1) const;
2437 virtual bool op1_range (irange &, tree type,
2438 const irange &lhs,
80dd13f5 2439 const irange &op2,
b565ac19 2440 relation_trio rel = TRIO_VARYING) const;
ade5531c 2441 virtual relation_kind lhs_op1_relation (const irange &lhs,
27e42601 2442 const irange &op1,
cf2141a0
AM
2443 const irange &op2,
2444 relation_kind rel) const;
0ddc8c78
AM
2445 void update_bitmask (irange &r, const irange &lh,
2446 const irange &rh) const final override
cd4b7e8b 2447 { update_known_bitmask (r, RSHIFT_EXPR, lh, rh); }
bd431d26
AH
2448} op_rshift;
2449
2450
ade5531c 2451relation_kind
27e42601
AM
2452operator_rshift::lhs_op1_relation (const irange &lhs ATTRIBUTE_UNUSED,
2453 const irange &op1,
cf2141a0
AM
2454 const irange &op2,
2455 relation_kind) const
27e42601
AM
2456{
2457 // If both operands range are >= 0, then the LHS <= op1.
2458 if (!op1.undefined_p () && !op2.undefined_p ()
2459 && wi::ge_p (op1.lower_bound (), 0, TYPE_SIGN (op1.type ()))
2460 && wi::ge_p (op2.lower_bound (), 0, TYPE_SIGN (op2.type ())))
ade5531c
AM
2461 return VREL_LE;
2462 return VREL_VARYING;
27e42601
AM
2463}
2464
f674b4a7 2465bool
4ba9fb0a
AH
2466operator_lshift::fold_range (irange &r, tree type,
2467 const irange &op1,
80dd13f5 2468 const irange &op2,
b565ac19 2469 relation_trio rel) const
38a73435 2470{
d0d8b5d8
AM
2471 int_range_max shift_range;
2472 if (!get_shift_range (shift_range, type, op2))
2473 {
2474 if (op2.undefined_p ())
2475 r.set_undefined ();
2476 else
f884c133 2477 r.set_zero (type);
d0d8b5d8
AM
2478 return true;
2479 }
38a73435
AH
2480
2481 // Transform left shifts by constants into multiplies.
d0d8b5d8 2482 if (shift_range.singleton_p ())
38a73435 2483 {
d0d8b5d8 2484 unsigned shift = shift_range.lower_bound ().to_uhwi ();
38a73435 2485 wide_int tmp = wi::set_bit_in_zero (shift, TYPE_PRECISION (type));
4ba9fb0a 2486 int_range<1> mult (type, tmp, tmp);
38a73435
AH
2487
2488 // Force wrapping multiplication.
2489 bool saved_flag_wrapv = flag_wrapv;
2490 bool saved_flag_wrapv_pointer = flag_wrapv_pointer;
2491 flag_wrapv = 1;
2492 flag_wrapv_pointer = 1;
4ba9fb0a 2493 bool b = op_mult.fold_range (r, type, op1, mult);
38a73435
AH
2494 flag_wrapv = saved_flag_wrapv;
2495 flag_wrapv_pointer = saved_flag_wrapv_pointer;
f674b4a7 2496 return b;
38a73435 2497 }
f674b4a7
AM
2498 else
2499 // Otherwise, invoke the generic fold routine.
3cb72ac1 2500 return range_operator::fold_range (r, type, op1, shift_range, rel);
38a73435
AH
2501}
2502
bb74ef9e 2503void
4ba9fb0a 2504operator_lshift::wi_fold (irange &r, tree type,
38a73435
AH
2505 const wide_int &lh_lb, const wide_int &lh_ub,
2506 const wide_int &rh_lb, const wide_int &rh_ub) const
2507{
2508 signop sign = TYPE_SIGN (type);
2509 unsigned prec = TYPE_PRECISION (type);
2510 int overflow_pos = sign == SIGNED ? prec - 1 : prec;
2511 int bound_shift = overflow_pos - rh_ub.to_shwi ();
2512 // If bound_shift == HOST_BITS_PER_WIDE_INT, the llshift can
2513 // overflow. However, for that to happen, rh.max needs to be zero,
704e8a82
AM
2514 // which means rh is a singleton range of zero, which means we simply return
2515 // [lh_lb, lh_ub] as the range.
2516 if (wi::eq_p (rh_ub, rh_lb) && wi::eq_p (rh_ub, 0))
2517 {
2518 r = int_range<2> (type, lh_lb, lh_ub);
2519 return;
2520 }
2521
38a73435
AH
2522 wide_int bound = wi::set_bit_in_zero (bound_shift, prec);
2523 wide_int complement = ~(bound - 1);
2524 wide_int low_bound, high_bound;
2525 bool in_bounds = false;
2526
2527 if (sign == UNSIGNED)
2528 {
2529 low_bound = bound;
2530 high_bound = complement;
2531 if (wi::ltu_p (lh_ub, low_bound))
2532 {
2533 // [5, 6] << [1, 2] == [10, 24].
2534 // We're shifting out only zeroes, the value increases
2535 // monotonically.
2536 in_bounds = true;
2537 }
2538 else if (wi::ltu_p (high_bound, lh_lb))
2539 {
2540 // [0xffffff00, 0xffffffff] << [1, 2]
2541 // == [0xfffffc00, 0xfffffffe].
2542 // We're shifting out only ones, the value decreases
2543 // monotonically.
2544 in_bounds = true;
2545 }
2546 }
2547 else
2548 {
2549 // [-1, 1] << [1, 2] == [-4, 4]
2550 low_bound = complement;
2551 high_bound = bound;
2552 if (wi::lts_p (lh_ub, high_bound)
2553 && wi::lts_p (low_bound, lh_lb))
2554 {
2555 // For non-negative numbers, we're shifting out only zeroes,
2556 // the value increases monotonically. For negative numbers,
2557 // we're shifting out only ones, the value decreases
2558 // monotonically.
2559 in_bounds = true;
2560 }
2561 }
2562
2563 if (in_bounds)
bb74ef9e
AM
2564 wi_cross_product (r, type, lh_lb, lh_ub, rh_lb, rh_ub);
2565 else
4ba9fb0a 2566 r.set_varying (type);
38a73435
AH
2567}
2568
2569bool
028d81b1
AH
2570operator_lshift::wi_op_overflows (wide_int &res, tree type,
2571 const wide_int &w0, const wide_int &w1) const
38a73435
AH
2572{
2573 signop sign = TYPE_SIGN (type);
2574 if (wi::neg_p (w1))
2575 {
2576 // It's unclear from the C standard whether shifts can overflow.
2577 // The following code ignores overflow; perhaps a C standard
2578 // interpretation ruling is needed.
2579 res = wi::rshift (w0, -w1, sign);
2580 }
2581 else
2582 res = wi::lshift (w0, w1);
2583 return false;
2584}
2585
4ba9fb0a
AH
2586bool
2587operator_lshift::op1_range (irange &r,
2588 tree type,
2589 const irange &lhs,
80dd13f5 2590 const irange &op2,
b565ac19 2591 relation_trio) const
4ba9fb0a 2592{
ef9bc362
AM
2593 if (lhs.undefined_p ())
2594 return false;
5f9ccf17 2595
cb779afe 2596 if (!contains_zero_p (lhs))
5f9ccf17
AH
2597 r.set_nonzero (type);
2598 else
2599 r.set_varying (type);
2600
cb779afe
AH
2601 wide_int shift;
2602 if (op2.singleton_p (shift))
4ba9fb0a 2603 {
4a135bd9
AM
2604 if (wi::lt_p (shift, 0, SIGNED))
2605 return false;
2d2f4ffc
AH
2606 if (wi::ge_p (shift, wi::uhwi (TYPE_PRECISION (type),
2607 TYPE_PRECISION (op2.type ())),
2608 UNSIGNED))
2609 return false;
5b80069c
AH
2610 if (shift == 0)
2611 {
5f9ccf17 2612 r.intersect (lhs);
5b80069c
AH
2613 return true;
2614 }
bd431d26
AH
2615
2616 // Work completely in unsigned mode to start.
2617 tree utype = type;
5f9ccf17 2618 int_range_max tmp_range;
bd431d26 2619 if (TYPE_SIGN (type) == SIGNED)
4ba9fb0a 2620 {
bd431d26
AH
2621 int_range_max tmp = lhs;
2622 utype = unsigned_type_for (type);
2623 range_cast (tmp, utype);
5f9ccf17 2624 op_rshift.fold_range (tmp_range, utype, tmp, op2);
4ba9fb0a 2625 }
bd431d26 2626 else
5f9ccf17
AH
2627 op_rshift.fold_range (tmp_range, utype, lhs, op2);
2628
bd431d26
AH
2629 // Start with ranges which can produce the LHS by right shifting the
2630 // result by the shift amount.
2631 // ie [0x08, 0xF0] = op1 << 2 will start with
2632 // [00001000, 11110000] = op1 << 2
2633 // [0x02, 0x4C] aka [00000010, 00111100]
2634
2635 // Then create a range from the LB with the least significant upper bit
2636 // set, to the upper bound with all the bits set.
2637 // This would be [0x42, 0xFC] aka [01000010, 11111100].
2638
2639 // Ideally we do this for each subrange, but just lump them all for now.
cb779afe 2640 unsigned low_bits = TYPE_PRECISION (utype) - shift.to_uhwi ();
bd431d26 2641 wide_int up_mask = wi::mask (low_bits, true, TYPE_PRECISION (utype));
6c0dd029
AH
2642 wide_int new_ub = wi::bit_or (up_mask, tmp_range.upper_bound ());
2643 wide_int new_lb = wi::set_bit (tmp_range.lower_bound (), low_bits);
bd431d26 2644 int_range<2> fill_range (utype, new_lb, new_ub);
6c0dd029 2645 tmp_range.union_ (fill_range);
bd431d26
AH
2646
2647 if (utype != type)
6c0dd029
AH
2648 range_cast (tmp_range, type);
2649
2650 r.intersect (tmp_range);
4ba9fb0a
AH
2651 return true;
2652 }
5f9ccf17
AH
2653
2654 return !r.varying_p ();
4ba9fb0a
AH
2655}
2656
4ba9fb0a
AH
2657bool
2658operator_rshift::op1_range (irange &r,
2659 tree type,
2660 const irange &lhs,
80dd13f5 2661 const irange &op2,
b565ac19 2662 relation_trio) const
4ba9fb0a 2663{
ef9bc362
AM
2664 if (lhs.undefined_p ())
2665 return false;
cb779afe
AH
2666 wide_int shift;
2667 if (op2.singleton_p (shift))
4ba9fb0a 2668 {
e1b4fbfe
AH
2669 // Ignore nonsensical shifts.
2670 unsigned prec = TYPE_PRECISION (type);
cb779afe
AH
2671 if (wi::ge_p (shift,
2672 wi::uhwi (prec, TYPE_PRECISION (op2.type ())),
e1b4fbfe
AH
2673 UNSIGNED))
2674 return false;
cb779afe 2675 if (shift == 0)
f0c0f124
AH
2676 {
2677 r = lhs;
2678 return true;
2679 }
e1b4fbfe 2680
4ba9fb0a
AH
2681 // Folding the original operation may discard some impossible
2682 // ranges from the LHS.
c5a6c223 2683 int_range_max lhs_refined;
4ba9fb0a
AH
2684 op_rshift.fold_range (lhs_refined, type, int_range<1> (type), op2);
2685 lhs_refined.intersect (lhs);
2686 if (lhs_refined.undefined_p ())
2687 {
2688 r.set_undefined ();
2689 return true;
2690 }
cb779afe 2691 int_range_max shift_range (op2.type (), shift, shift);
c5a6c223 2692 int_range_max lb, ub;
4ba9fb0a
AH
2693 op_lshift.fold_range (lb, type, lhs_refined, shift_range);
2694 // LHS
2695 // 0000 0111 = OP1 >> 3
2696 //
2697 // OP1 is anything from 0011 1000 to 0011 1111. That is, a
2698 // range from LHS<<3 plus a mask of the 3 bits we shifted on the
2699 // right hand side (0x07).
8b2181a4 2700 wide_int mask = wi::bit_not (wi::lshift (wi::minus_one (prec), shift));
cb779afe
AH
2701 int_range_max mask_range (type,
2702 wi::zero (TYPE_PRECISION (type)),
8b2181a4 2703 mask);
4ba9fb0a
AH
2704 op_plus.fold_range (ub, type, lb, mask_range);
2705 r = lb;
2706 r.union_ (ub);
cb779afe 2707 if (!contains_zero_p (lhs_refined))
4ba9fb0a
AH
2708 {
2709 mask_range.invert ();
2710 r.intersect (mask_range);
2711 }
2712 return true;
2713 }
2714 return false;
2715}
2716
38a73435
AH
2717bool
2718operator_rshift::wi_op_overflows (wide_int &res,
2719 tree type,
2720 const wide_int &w0,
2721 const wide_int &w1) const
2722{
2723 signop sign = TYPE_SIGN (type);
2724 if (wi::neg_p (w1))
2725 res = wi::lshift (w0, -w1);
2726 else
2727 {
2728 // It's unclear from the C standard whether shifts can overflow.
2729 // The following code ignores overflow; perhaps a C standard
2730 // interpretation ruling is needed.
2731 res = wi::rshift (w0, w1, sign);
2732 }
2733 return false;
2734}
2735
f674b4a7 2736bool
4ba9fb0a
AH
2737operator_rshift::fold_range (irange &r, tree type,
2738 const irange &op1,
80dd13f5 2739 const irange &op2,
b565ac19 2740 relation_trio rel) const
38a73435 2741{
d0d8b5d8
AM
2742 int_range_max shift;
2743 if (!get_shift_range (shift, type, op2))
2744 {
2745 if (op2.undefined_p ())
2746 r.set_undefined ();
2747 else
f884c133 2748 r.set_zero (type);
d0d8b5d8
AM
2749 return true;
2750 }
38a73435 2751
3cb72ac1 2752 return range_operator::fold_range (r, type, op1, shift, rel);
38a73435
AH
2753}
2754
bb74ef9e 2755void
4ba9fb0a 2756operator_rshift::wi_fold (irange &r, tree type,
38a73435
AH
2757 const wide_int &lh_lb, const wide_int &lh_ub,
2758 const wide_int &rh_lb, const wide_int &rh_ub) const
2759{
bb74ef9e 2760 wi_cross_product (r, type, lh_lb, lh_ub, rh_lb, rh_ub);
38a73435
AH
2761}
2762
2763
d75be7e4
AM
2764// Add a partial equivalence between the LHS and op1 for casts.
2765
2766relation_kind
2767operator_cast::lhs_op1_relation (const irange &lhs,
2768 const irange &op1,
2769 const irange &op2 ATTRIBUTE_UNUSED,
2770 relation_kind) const
2771{
2772 if (lhs.undefined_p () || op1.undefined_p ())
2773 return VREL_VARYING;
2774 unsigned lhs_prec = TYPE_PRECISION (lhs.type ());
2775 unsigned op1_prec = TYPE_PRECISION (op1.type ());
2776 // If the result gets sign extended into a larger type check first if this
2777 // qualifies as a partial equivalence.
2778 if (TYPE_SIGN (op1.type ()) == SIGNED && lhs_prec > op1_prec)
2779 {
2780 // If the result is sign extended, and the LHS is larger than op1,
c46b5b0a 2781 // check if op1's range can be negative as the sign extension will
d75be7e4
AM
2782 // cause the upper bits to be 1 instead of 0, invalidating the PE.
2783 int_range<3> negs = range_negatives (op1.type ());
2784 negs.intersect (op1);
2785 if (!negs.undefined_p ())
2786 return VREL_VARYING;
2787 }
2788
2789 unsigned prec = MIN (lhs_prec, op1_prec);
2790 return bits_to_pe (prec);
2791}
2792
4ba9fb0a
AH
2793// Return TRUE if casting from INNER to OUTER is a truncating cast.
2794
2795inline bool
2796operator_cast::truncating_cast_p (const irange &inner,
2797 const irange &outer) const
2798{
2799 return TYPE_PRECISION (outer.type ()) < TYPE_PRECISION (inner.type ());
2800}
2801
2802// Return TRUE if [MIN,MAX] is inside the domain of RANGE's type.
2803
f674b4a7 2804bool
4ba9fb0a
AH
2805operator_cast::inside_domain_p (const wide_int &min,
2806 const wide_int &max,
2807 const irange &range) const
38a73435 2808{
8b2181a4
AH
2809 wide_int domain_min = irange_val_min (range.type ());
2810 wide_int domain_max = irange_val_max (range.type ());
4ba9fb0a
AH
2811 signop domain_sign = TYPE_SIGN (range.type ());
2812 return (wi::le_p (min, domain_max, domain_sign)
2813 && wi::le_p (max, domain_max, domain_sign)
2814 && wi::ge_p (min, domain_min, domain_sign)
2815 && wi::ge_p (max, domain_min, domain_sign));
2816}
2817
2818
2819// Helper for fold_range which work on a pair at a time.
2820
2821void
2822operator_cast::fold_pair (irange &r, unsigned index,
2823 const irange &inner,
2824 const irange &outer) const
2825{
2826 tree inner_type = inner.type ();
2827 tree outer_type = outer.type ();
2828 signop inner_sign = TYPE_SIGN (inner_type);
2829 unsigned outer_prec = TYPE_PRECISION (outer_type);
2830
2831 // check to see if casting from INNER to OUTER is a conversion that
2832 // fits in the resulting OUTER type.
2833 wide_int inner_lb = inner.lower_bound (index);
2834 wide_int inner_ub = inner.upper_bound (index);
2835 if (truncating_cast_p (inner, outer))
2836 {
c46b5b0a 2837 // We may be able to accommodate a truncating cast if the
4ba9fb0a
AH
2838 // resulting range can be represented in the target type...
2839 if (wi::rshift (wi::sub (inner_ub, inner_lb),
2840 wi::uhwi (outer_prec, TYPE_PRECISION (inner.type ())),
2841 inner_sign) != 0)
38a73435 2842 {
4ba9fb0a
AH
2843 r.set_varying (outer_type);
2844 return;
38a73435 2845 }
4ba9fb0a
AH
2846 }
2847 // ...but we must still verify that the final range fits in the
2848 // domain. This catches -fstrict-enum restrictions where the domain
2849 // range is smaller than what fits in the underlying type.
2850 wide_int min = wide_int::from (inner_lb, outer_prec, inner_sign);
2851 wide_int max = wide_int::from (inner_ub, outer_prec, inner_sign);
2852 if (inside_domain_p (min, max, outer))
2853 create_possibly_reversed_range (r, outer_type, min, max);
2854 else
2855 r.set_varying (outer_type);
2856}
2857
2858
2859bool
2860operator_cast::fold_range (irange &r, tree type ATTRIBUTE_UNUSED,
2861 const irange &inner,
80dd13f5 2862 const irange &outer,
b565ac19 2863 relation_trio) const
4ba9fb0a
AH
2864{
2865 if (empty_range_varying (r, type, inner, outer))
2866 return true;
2867
2868 gcc_checking_assert (outer.varying_p ());
2869 gcc_checking_assert (inner.num_pairs () > 0);
2870
2871 // Avoid a temporary by folding the first pair directly into the result.
2872 fold_pair (r, 0, inner, outer);
2873
c46b5b0a 2874 // Then process any additional pairs by unioning with their results.
4ba9fb0a
AH
2875 for (unsigned x = 1; x < inner.num_pairs (); ++x)
2876 {
c5a6c223 2877 int_range_max tmp;
4ba9fb0a
AH
2878 fold_pair (tmp, x, inner, outer);
2879 r.union_ (tmp);
2880 if (r.varying_p ())
2881 return true;
38a73435 2882 }
ae56d600 2883
8605bd93 2884 update_bitmask (r, inner, outer);
f674b4a7 2885 return true;
38a73435
AH
2886}
2887
8605bd93
AH
2888void
2889operator_cast::update_bitmask (irange &r, const irange &lh,
2890 const irange &rh) const
2891{
2892 update_known_bitmask (r, CONVERT_EXPR, lh, rh);
2893}
2894
38a73435 2895bool
4ba9fb0a
AH
2896operator_cast::op1_range (irange &r, tree type,
2897 const irange &lhs,
80dd13f5 2898 const irange &op2,
b565ac19 2899 relation_trio) const
38a73435 2900{
ef9bc362
AM
2901 if (lhs.undefined_p ())
2902 return false;
38a73435
AH
2903 tree lhs_type = lhs.type ();
2904 gcc_checking_assert (types_compatible_p (op2.type(), type));
2905
c25b5046
AM
2906 // If we are calculating a pointer, shortcut to what we really care about.
2907 if (POINTER_TYPE_P (type))
2908 {
2909 // Conversion from other pointers or a constant (including 0/NULL)
2910 // are straightforward.
2911 if (POINTER_TYPE_P (lhs.type ())
2912 || (lhs.singleton_p ()
2913 && TYPE_PRECISION (lhs.type ()) >= TYPE_PRECISION (type)))
2914 {
2915 r = lhs;
2916 range_cast (r, type);
2917 }
2918 else
2919 {
2920 // If the LHS is not a pointer nor a singleton, then it is
2921 // either VARYING or non-zero.
cb779afe 2922 if (!contains_zero_p (lhs))
c25b5046
AM
2923 r.set_nonzero (type);
2924 else
2925 r.set_varying (type);
2926 }
2927 r.intersect (op2);
2928 return true;
2929 }
2930
4ba9fb0a
AH
2931 if (truncating_cast_p (op2, lhs))
2932 {
2933 if (lhs.varying_p ())
2934 r.set_varying (type);
2935 else
38a73435 2936 {
4ba9fb0a
AH
2937 // We want to insert the LHS as an unsigned value since it
2938 // would not trigger the signed bit of the larger type.
c5a6c223 2939 int_range_max converted_lhs = lhs;
4ba9fb0a
AH
2940 range_cast (converted_lhs, unsigned_type_for (lhs_type));
2941 range_cast (converted_lhs, type);
2942 // Start by building the positive signed outer range for the type.
2943 wide_int lim = wi::set_bit_in_zero (TYPE_PRECISION (lhs_type),
2944 TYPE_PRECISION (type));
04e5ddf8
AH
2945 create_possibly_reversed_range (r, type, lim,
2946 wi::max_value (TYPE_PRECISION (type),
2947 SIGNED));
4ba9fb0a
AH
2948 // For the signed part, we need to simply union the 2 ranges now.
2949 r.union_ (converted_lhs);
2950
2951 // Create maximal negative number outside of LHS bits.
2952 lim = wi::mask (TYPE_PRECISION (lhs_type), true,
2953 TYPE_PRECISION (type));
2954 // Add this to the unsigned LHS range(s).
c5a6c223
AH
2955 int_range_max lim_range (type, lim, lim);
2956 int_range_max lhs_neg;
2eb50117
AM
2957 range_op_handler (PLUS_EXPR).fold_range (lhs_neg, type,
2958 converted_lhs, lim_range);
1cde5d85
AM
2959 // lhs_neg now has all the negative versions of the LHS.
2960 // Now union in all the values from SIGNED MIN (0x80000) to
2961 // lim-1 in order to fill in all the ranges with the upper
2962 // bits set.
2963
2964 // PR 97317. If the lhs has only 1 bit less precision than the rhs,
2965 // we don't need to create a range from min to lim-1
2966 // calculate neg range traps trying to create [lim, lim - 1].
2967 wide_int min_val = wi::min_value (TYPE_PRECISION (type), SIGNED);
2968 if (lim != min_val)
2969 {
2970 int_range_max neg (type,
2971 wi::min_value (TYPE_PRECISION (type),
2972 SIGNED),
2973 lim - 1);
2974 lhs_neg.union_ (neg);
2975 }
4ba9fb0a 2976 // And finally, munge the signed and unsigned portions.
1cde5d85 2977 r.union_ (lhs_neg);
38a73435 2978 }
4ba9fb0a
AH
2979 // And intersect with any known value passed in the extra operand.
2980 r.intersect (op2);
38a73435
AH
2981 return true;
2982 }
2983
c5a6c223 2984 int_range_max tmp;
4ba9fb0a
AH
2985 if (TYPE_PRECISION (lhs_type) == TYPE_PRECISION (type))
2986 tmp = lhs;
2987 else
38a73435 2988 {
4ba9fb0a
AH
2989 // The cast is not truncating, and the range is restricted to
2990 // the range of the RHS by this assignment.
2991 //
38a73435 2992 // Cast the range of the RHS to the type of the LHS.
4ba9fb0a
AH
2993 fold_range (tmp, lhs_type, int_range<1> (type), int_range<1> (lhs_type));
2994 // Intersect this with the LHS range will produce the range,
2995 // which will be cast to the RHS type before returning.
bb74ef9e 2996 tmp.intersect (lhs);
38a73435 2997 }
38a73435
AH
2998
2999 // Cast the calculated range to the type of the RHS.
4ba9fb0a 3000 fold_range (r, type, tmp, int_range<1> (type));
38a73435
AH
3001 return true;
3002}
3003
3004
3005class operator_logical_and : public range_operator
3006{
cf5bea76
AH
3007 using range_operator::fold_range;
3008 using range_operator::op1_range;
3009 using range_operator::op2_range;
38a73435 3010public:
4ba9fb0a
AH
3011 virtual bool fold_range (irange &r, tree type,
3012 const irange &lh,
80dd13f5 3013 const irange &rh,
b565ac19 3014 relation_trio rel = TRIO_VARYING) const;
4ba9fb0a
AH
3015 virtual bool op1_range (irange &r, tree type,
3016 const irange &lhs,
80dd13f5 3017 const irange &op2,
b565ac19 3018 relation_trio rel = TRIO_VARYING) const;
4ba9fb0a
AH
3019 virtual bool op2_range (irange &r, tree type,
3020 const irange &lhs,
80dd13f5 3021 const irange &op1,
b565ac19 3022 relation_trio rel = TRIO_VARYING) const;
38a73435
AH
3023} op_logical_and;
3024
3025
f674b4a7 3026bool
4ba9fb0a
AH
3027operator_logical_and::fold_range (irange &r, tree type,
3028 const irange &lh,
80dd13f5 3029 const irange &rh,
b565ac19 3030 relation_trio) const
38a73435 3031{
4ba9fb0a 3032 if (empty_range_varying (r, type, lh, rh))
f674b4a7 3033 return true;
38a73435
AH
3034
3035 // 0 && anything is 0.
3036 if ((wi::eq_p (lh.lower_bound (), 0) && wi::eq_p (lh.upper_bound (), 0))
3037 || (wi::eq_p (lh.lower_bound (), 0) && wi::eq_p (rh.upper_bound (), 0)))
bb74ef9e 3038 r = range_false (type);
cb779afe 3039 else if (contains_zero_p (lh) || contains_zero_p (rh))
bb74ef9e
AM
3040 // To reach this point, there must be a logical 1 on each side, and
3041 // the only remaining question is whether there is a zero or not.
3042 r = range_true_and_false (type);
3043 else
3044 r = range_true (type);
f674b4a7 3045 return true;
38a73435
AH
3046}
3047
3048bool
4ba9fb0a
AH
3049operator_logical_and::op1_range (irange &r, tree type,
3050 const irange &lhs,
80dd13f5 3051 const irange &op2 ATTRIBUTE_UNUSED,
b565ac19 3052 relation_trio) const
38a73435
AH
3053{
3054 switch (get_bool_state (r, lhs, type))
3055 {
3056 case BRS_TRUE:
3057 // A true result means both sides of the AND must be true.
3058 r = range_true (type);
3059 break;
3060 default:
3061 // Any other result means only one side has to be false, the
b4244671 3062 // other side can be anything. So we cannot be sure of any
38a73435
AH
3063 // result here.
3064 r = range_true_and_false (type);
3065 break;
3066 }
3067 return true;
3068}
3069
3070bool
4ba9fb0a
AH
3071operator_logical_and::op2_range (irange &r, tree type,
3072 const irange &lhs,
80dd13f5 3073 const irange &op1,
b565ac19 3074 relation_trio) const
38a73435
AH
3075{
3076 return operator_logical_and::op1_range (r, type, lhs, op1);
3077}
3078
3079
0965275e
AM
3080void
3081operator_bitwise_and::update_bitmask (irange &r, const irange &lh,
3082 const irange &rh) const
38a73435 3083{
0965275e
AM
3084 update_known_bitmask (r, BIT_AND_EXPR, lh, rh);
3085}
4ba9fb0a 3086
b3e98eb3
RS
3087// Optimize BIT_AND_EXPR, BIT_IOR_EXPR and BIT_XOR_EXPR of signed types
3088// by considering the number of leading redundant sign bit copies.
3089// clrsb (X op Y) = min (clrsb (X), clrsb (Y)), so for example
3090// [-1, 0] op [-1, 0] is [-1, 0] (where nonzero_bits doesn't help).
3091static bool
3092wi_optimize_signed_bitwise_op (irange &r, tree type,
3093 const wide_int &lh_lb, const wide_int &lh_ub,
3094 const wide_int &rh_lb, const wide_int &rh_ub)
3095{
3096 int lh_clrsb = MIN (wi::clrsb (lh_lb), wi::clrsb (lh_ub));
3097 int rh_clrsb = MIN (wi::clrsb (rh_lb), wi::clrsb (rh_ub));
3098 int new_clrsb = MIN (lh_clrsb, rh_clrsb);
3099 if (new_clrsb == 0)
3100 return false;
3101 int type_prec = TYPE_PRECISION (type);
3102 int rprec = (type_prec - new_clrsb) - 1;
3103 value_range_with_overflow (r, type,
3104 wi::mask (rprec, true, type_prec),
3105 wi::mask (rprec, false, type_prec));
3106 return true;
3107}
3108
d75be7e4
AM
3109// An AND of 8,16, 32 or 64 bits can produce a partial equivalence between
3110// the LHS and op1.
3111
3112relation_kind
3113operator_bitwise_and::lhs_op1_relation (const irange &lhs,
3114 const irange &op1,
3115 const irange &op2,
3116 relation_kind) const
3117{
3118 if (lhs.undefined_p () || op1.undefined_p () || op2.undefined_p ())
3119 return VREL_VARYING;
3120 if (!op2.singleton_p ())
3121 return VREL_VARYING;
3122 // if val == 0xff or 0xFFFF OR 0Xffffffff OR 0Xffffffffffffffff, return TRUE
3123 int prec1 = TYPE_PRECISION (op1.type ());
3124 int prec2 = TYPE_PRECISION (op2.type ());
3125 int mask_prec = 0;
3126 wide_int mask = op2.lower_bound ();
3127 if (wi::eq_p (mask, wi::mask (8, false, prec2)))
3128 mask_prec = 8;
3129 else if (wi::eq_p (mask, wi::mask (16, false, prec2)))
3130 mask_prec = 16;
3131 else if (wi::eq_p (mask, wi::mask (32, false, prec2)))
3132 mask_prec = 32;
3133 else if (wi::eq_p (mask, wi::mask (64, false, prec2)))
3134 mask_prec = 64;
3135 return bits_to_pe (MIN (prec1, mask_prec));
3136}
b3e98eb3 3137
38a73435
AH
3138// Optimize BIT_AND_EXPR and BIT_IOR_EXPR in terms of a mask if
3139// possible. Basically, see if we can optimize:
3140//
3141// [LB, UB] op Z
3142// into:
3143// [LB op Z, UB op Z]
3144//
3145// If the optimization was successful, accumulate the range in R and
3146// return TRUE.
3147
3148static bool
4ba9fb0a 3149wi_optimize_and_or (irange &r,
38a73435
AH
3150 enum tree_code code,
3151 tree type,
3152 const wide_int &lh_lb, const wide_int &lh_ub,
3153 const wide_int &rh_lb, const wide_int &rh_ub)
3154{
3155 // Calculate the singleton mask among the ranges, if any.
3156 wide_int lower_bound, upper_bound, mask;
3157 if (wi::eq_p (rh_lb, rh_ub))
3158 {
3159 mask = rh_lb;
3160 lower_bound = lh_lb;
3161 upper_bound = lh_ub;
3162 }
3163 else if (wi::eq_p (lh_lb, lh_ub))
3164 {
3165 mask = lh_lb;
3166 lower_bound = rh_lb;
3167 upper_bound = rh_ub;
3168 }
3169 else
3170 return false;
3171
3172 // If Z is a constant which (for op | its bitwise not) has n
3173 // consecutive least significant bits cleared followed by m 1
3174 // consecutive bits set immediately above it and either
3175 // m + n == precision, or (x >> (m + n)) == (y >> (m + n)).
3176 //
3177 // The least significant n bits of all the values in the range are
3178 // cleared or set, the m bits above it are preserved and any bits
3179 // above these are required to be the same for all values in the
3180 // range.
3181 wide_int w = mask;
3182 int m = 0, n = 0;
3183 if (code == BIT_IOR_EXPR)
3184 w = ~w;
3185 if (wi::eq_p (w, 0))
3186 n = w.get_precision ();
3187 else
3188 {
3189 n = wi::ctz (w);
3190 w = ~(w | wi::mask (n, false, w.get_precision ()));
3191 if (wi::eq_p (w, 0))
3192 m = w.get_precision () - n;
3193 else
3194 m = wi::ctz (w) - n;
3195 }
3196 wide_int new_mask = wi::mask (m + n, true, w.get_precision ());
3197 if ((new_mask & lower_bound) != (new_mask & upper_bound))
3198 return false;
3199
3200 wide_int res_lb, res_ub;
3201 if (code == BIT_AND_EXPR)
3202 {
3203 res_lb = wi::bit_and (lower_bound, mask);
3204 res_ub = wi::bit_and (upper_bound, mask);
3205 }
3206 else if (code == BIT_IOR_EXPR)
3207 {
3208 res_lb = wi::bit_or (lower_bound, mask);
3209 res_ub = wi::bit_or (upper_bound, mask);
3210 }
3211 else
3212 gcc_unreachable ();
bb74ef9e 3213 value_range_with_overflow (r, type, res_lb, res_ub);
a5f9c27b
AM
3214
3215 // Furthermore, if the mask is non-zero, an IOR cannot contain zero.
3216 if (code == BIT_IOR_EXPR && wi::ne_p (mask, 0))
3217 {
3218 int_range<2> tmp;
3219 tmp.set_nonzero (type);
3220 r.intersect (tmp);
3221 }
38a73435
AH
3222 return true;
3223}
3224
3225// For range [LB, UB] compute two wide_int bit masks.
3226//
3227// In the MAYBE_NONZERO bit mask, if some bit is unset, it means that
3228// for all numbers in the range the bit is 0, otherwise it might be 0
3229// or 1.
3230//
3231// In the MUSTBE_NONZERO bit mask, if some bit is set, it means that
3232// for all numbers in the range the bit is 1, otherwise it might be 0
3233// or 1.
3234
8f119c55 3235void
38a73435
AH
3236wi_set_zero_nonzero_bits (tree type,
3237 const wide_int &lb, const wide_int &ub,
3238 wide_int &maybe_nonzero,
3239 wide_int &mustbe_nonzero)
3240{
3241 signop sign = TYPE_SIGN (type);
3242
3243 if (wi::eq_p (lb, ub))
3244 maybe_nonzero = mustbe_nonzero = lb;
3245 else if (wi::ge_p (lb, 0, sign) || wi::lt_p (ub, 0, sign))
3246 {
3247 wide_int xor_mask = lb ^ ub;
3248 maybe_nonzero = lb | ub;
3249 mustbe_nonzero = lb & ub;
3250 if (xor_mask != 0)
3251 {
3252 wide_int mask = wi::mask (wi::floor_log2 (xor_mask), false,
3253 maybe_nonzero.get_precision ());
3254 maybe_nonzero = maybe_nonzero | mask;
3255 mustbe_nonzero = wi::bit_and_not (mustbe_nonzero, mask);
3256 }
3257 }
3258 else
3259 {
3260 maybe_nonzero = wi::minus_one (lb.get_precision ());
3261 mustbe_nonzero = wi::zero (lb.get_precision ());
3262 }
3263}
3264
bb74ef9e 3265void
4ba9fb0a 3266operator_bitwise_and::wi_fold (irange &r, tree type,
38a73435
AH
3267 const wide_int &lh_lb,
3268 const wide_int &lh_ub,
3269 const wide_int &rh_lb,
3270 const wide_int &rh_ub) const
3271{
38a73435 3272 if (wi_optimize_and_or (r, BIT_AND_EXPR, type, lh_lb, lh_ub, rh_lb, rh_ub))
bb74ef9e 3273 return;
38a73435
AH
3274
3275 wide_int maybe_nonzero_lh, mustbe_nonzero_lh;
3276 wide_int maybe_nonzero_rh, mustbe_nonzero_rh;
3277 wi_set_zero_nonzero_bits (type, lh_lb, lh_ub,
3278 maybe_nonzero_lh, mustbe_nonzero_lh);
3279 wi_set_zero_nonzero_bits (type, rh_lb, rh_ub,
3280 maybe_nonzero_rh, mustbe_nonzero_rh);
3281
3282 wide_int new_lb = mustbe_nonzero_lh & mustbe_nonzero_rh;
3283 wide_int new_ub = maybe_nonzero_lh & maybe_nonzero_rh;
3284 signop sign = TYPE_SIGN (type);
3285 unsigned prec = TYPE_PRECISION (type);
3286 // If both input ranges contain only negative values, we can
3287 // truncate the result range maximum to the minimum of the
3288 // input range maxima.
3289 if (wi::lt_p (lh_ub, 0, sign) && wi::lt_p (rh_ub, 0, sign))
3290 {
3291 new_ub = wi::min (new_ub, lh_ub, sign);
3292 new_ub = wi::min (new_ub, rh_ub, sign);
3293 }
3294 // If either input range contains only non-negative values
3295 // we can truncate the result range maximum to the respective
3296 // maximum of the input range.
3297 if (wi::ge_p (lh_lb, 0, sign))
3298 new_ub = wi::min (new_ub, lh_ub, sign);
3299 if (wi::ge_p (rh_lb, 0, sign))
3300 new_ub = wi::min (new_ub, rh_ub, sign);
3301 // PR68217: In case of signed & sign-bit-CST should
3302 // result in [-INF, 0] instead of [-INF, INF].
3303 if (wi::gt_p (new_lb, new_ub, sign))
3304 {
3305 wide_int sign_bit = wi::set_bit_in_zero (prec - 1, prec);
3306 if (sign == SIGNED
3307 && ((wi::eq_p (lh_lb, lh_ub)
3308 && !wi::cmps (lh_lb, sign_bit))
3309 || (wi::eq_p (rh_lb, rh_ub)
3310 && !wi::cmps (rh_lb, sign_bit))))
3311 {
3312 new_lb = wi::min_value (prec, sign);
3313 new_ub = wi::zero (prec);
3314 }
3315 }
3316 // If the limits got swapped around, return varying.
3317 if (wi::gt_p (new_lb, new_ub,sign))
b3e98eb3
RS
3318 {
3319 if (sign == SIGNED
3320 && wi_optimize_signed_bitwise_op (r, type,
3321 lh_lb, lh_ub,
3322 rh_lb, rh_ub))
3323 return;
3324 r.set_varying (type);
3325 }
bb74ef9e
AM
3326 else
3327 value_range_with_overflow (r, type, new_lb, new_ub);
38a73435
AH
3328}
3329
4ba9fb0a
AH
3330static void
3331set_nonzero_range_from_mask (irange &r, tree type, const irange &lhs)
3332{
cb779afe 3333 if (!contains_zero_p (lhs))
4ba9fb0a
AH
3334 r = range_nonzero (type);
3335 else
3336 r.set_varying (type);
3337}
3338
ca0be1bb
AH
3339/* Find out smallest RES where RES > VAL && (RES & MASK) == RES, if any
3340 (otherwise return VAL). VAL and MASK must be zero-extended for
3341 precision PREC. If SGNBIT is non-zero, first xor VAL with SGNBIT
3342 (to transform signed values into unsigned) and at the end xor
3343 SGNBIT back. */
3344
3345wide_int
3346masked_increment (const wide_int &val_in, const wide_int &mask,
3347 const wide_int &sgnbit, unsigned int prec)
3348{
3349 wide_int bit = wi::one (prec), res;
3350 unsigned int i;
3351
3352 wide_int val = val_in ^ sgnbit;
3353 for (i = 0; i < prec; i++, bit += bit)
3354 {
3355 res = mask;
3356 if ((res & bit) == 0)
3357 continue;
3358 res = bit - 1;
3359 res = wi::bit_and_not (val + bit, res);
3360 res &= mask;
3361 if (wi::gtu_p (res, val))
3362 return res ^ sgnbit;
3363 }
3364 return val ^ sgnbit;
3365}
3366
4ba9fb0a
AH
3367// This was shamelessly stolen from register_edge_assert_for_2 and
3368// adjusted to work with iranges.
3369
3370void
3371operator_bitwise_and::simple_op1_range_solver (irange &r, tree type,
3372 const irange &lhs,
3373 const irange &op2) const
3374{
3375 if (!op2.singleton_p ())
3376 {
3377 set_nonzero_range_from_mask (r, type, lhs);
3378 return;
3379 }
3380 unsigned int nprec = TYPE_PRECISION (type);
3381 wide_int cst2v = op2.lower_bound ();
3382 bool cst2n = wi::neg_p (cst2v, TYPE_SIGN (type));
3383 wide_int sgnbit;
3384 if (cst2n)
3385 sgnbit = wi::set_bit_in_zero (nprec - 1, nprec);
3386 else
3387 sgnbit = wi::zero (nprec);
3388
3389 // Solve [lhs.lower_bound (), +INF] = x & MASK.
3390 //
3391 // Minimum unsigned value for >= if (VAL & CST2) == VAL is VAL and
3392 // maximum unsigned value is ~0. For signed comparison, if CST2
3393 // doesn't have the most significant bit set, handle it similarly. If
3394 // CST2 has MSB set, the minimum is the same, and maximum is ~0U/2.
3395 wide_int valv = lhs.lower_bound ();
3396 wide_int minv = valv & cst2v, maxv;
3397 bool we_know_nothing = false;
3398 if (minv != valv)
3399 {
3400 // If (VAL & CST2) != VAL, X & CST2 can't be equal to VAL.
3401 minv = masked_increment (valv, cst2v, sgnbit, nprec);
3402 if (minv == valv)
3403 {
3404 // If we can't determine anything on this bound, fall
3405 // through and conservatively solve for the other end point.
3406 we_know_nothing = true;
3407 }
3408 }
3409 maxv = wi::mask (nprec - (cst2n ? 1 : 0), false, nprec);
3410 if (we_know_nothing)
3411 r.set_varying (type);
3412 else
04e5ddf8 3413 create_possibly_reversed_range (r, type, minv, maxv);
4ba9fb0a
AH
3414
3415 // Solve [-INF, lhs.upper_bound ()] = x & MASK.
3416 //
3417 // Minimum unsigned value for <= is 0 and maximum unsigned value is
3418 // VAL | ~CST2 if (VAL & CST2) == VAL. Otherwise, find smallest
3419 // VAL2 where
3420 // VAL2 > VAL && (VAL2 & CST2) == VAL2 and use (VAL2 - 1) | ~CST2
3421 // as maximum.
3422 // For signed comparison, if CST2 doesn't have most significant bit
3423 // set, handle it similarly. If CST2 has MSB set, the maximum is
3424 // the same and minimum is INT_MIN.
3425 valv = lhs.upper_bound ();
3426 minv = valv & cst2v;
3427 if (minv == valv)
3428 maxv = valv;
3429 else
3430 {
3431 maxv = masked_increment (valv, cst2v, sgnbit, nprec);
3432 if (maxv == valv)
3433 {
3434 // If we couldn't determine anything on either bound, return
3435 // undefined.
3436 if (we_know_nothing)
3437 r.set_undefined ();
3438 return;
3439 }
3440 maxv -= 1;
3441 }
3442 maxv |= ~cst2v;
3443 minv = sgnbit;
04e5ddf8
AH
3444 int_range<2> upper_bits;
3445 create_possibly_reversed_range (upper_bits, type, minv, maxv);
4ba9fb0a
AH
3446 r.intersect (upper_bits);
3447}
3448
38a73435 3449bool
4ba9fb0a
AH
3450operator_bitwise_and::op1_range (irange &r, tree type,
3451 const irange &lhs,
80dd13f5 3452 const irange &op2,
b565ac19 3453 relation_trio) const
38a73435 3454{
ef9bc362
AM
3455 if (lhs.undefined_p ())
3456 return false;
38a73435
AH
3457 if (types_compatible_p (type, boolean_type_node))
3458 return op_logical_and.op1_range (r, type, lhs, op2);
3459
4ba9fb0a
AH
3460 r.set_undefined ();
3461 for (unsigned i = 0; i < lhs.num_pairs (); ++i)
3462 {
c5a6c223 3463 int_range_max chunk (lhs.type (),
4ba9fb0a
AH
3464 lhs.lower_bound (i),
3465 lhs.upper_bound (i));
c5a6c223 3466 int_range_max res;
4ba9fb0a
AH
3467 simple_op1_range_solver (res, type, chunk, op2);
3468 r.union_ (res);
3469 }
3470 if (r.undefined_p ())
3471 set_nonzero_range_from_mask (r, type, lhs);
5e77d408 3472
7a5e4765
AH
3473 // For MASK == op1 & MASK, all the bits in MASK must be set in op1.
3474 wide_int mask;
3475 if (lhs == op2 && lhs.singleton_p (mask))
3476 {
3477 r.update_bitmask (irange_bitmask (mask, ~mask));
3478 return true;
3479 }
3480
5e77d408
AH
3481 // For 0 = op1 & MASK, op1 is ~MASK.
3482 if (lhs.zero_p () && op2.singleton_p ())
3483 {
3484 wide_int nz = wi::bit_not (op2.get_nonzero_bits ());
3485 int_range<2> tmp (type);
3486 tmp.set_nonzero_bits (nz);
3487 r.intersect (tmp);
3488 }
38a73435
AH
3489 return true;
3490}
3491
3492bool
4ba9fb0a
AH
3493operator_bitwise_and::op2_range (irange &r, tree type,
3494 const irange &lhs,
80dd13f5 3495 const irange &op1,
b565ac19 3496 relation_trio) const
38a73435
AH
3497{
3498 return operator_bitwise_and::op1_range (r, type, lhs, op1);
3499}
3500
3501
3502class operator_logical_or : public range_operator
3503{
cf5bea76
AH
3504 using range_operator::fold_range;
3505 using range_operator::op1_range;
3506 using range_operator::op2_range;
38a73435 3507public:
4ba9fb0a
AH
3508 virtual bool fold_range (irange &r, tree type,
3509 const irange &lh,
80dd13f5 3510 const irange &rh,
b565ac19 3511 relation_trio rel = TRIO_VARYING) const;
4ba9fb0a
AH
3512 virtual bool op1_range (irange &r, tree type,
3513 const irange &lhs,
80dd13f5 3514 const irange &op2,
b565ac19 3515 relation_trio rel = TRIO_VARYING) const;
4ba9fb0a
AH
3516 virtual bool op2_range (irange &r, tree type,
3517 const irange &lhs,
80dd13f5 3518 const irange &op1,
b565ac19 3519 relation_trio rel = TRIO_VARYING) const;
38a73435
AH
3520} op_logical_or;
3521
f674b4a7 3522bool
4ba9fb0a
AH
3523operator_logical_or::fold_range (irange &r, tree type ATTRIBUTE_UNUSED,
3524 const irange &lh,
80dd13f5 3525 const irange &rh,
b565ac19 3526 relation_trio) const
38a73435 3527{
4ba9fb0a 3528 if (empty_range_varying (r, type, lh, rh))
f674b4a7 3529 return true;
38a73435 3530
fae08a05
AH
3531 r = lh;
3532 r.union_ (rh);
f674b4a7 3533 return true;
38a73435
AH
3534}
3535
3536bool
4ba9fb0a
AH
3537operator_logical_or::op1_range (irange &r, tree type,
3538 const irange &lhs,
80dd13f5 3539 const irange &op2 ATTRIBUTE_UNUSED,
b565ac19 3540 relation_trio) const
38a73435
AH
3541{
3542 switch (get_bool_state (r, lhs, type))
3543 {
3544 case BRS_FALSE:
3545 // A false result means both sides of the OR must be false.
3546 r = range_false (type);
3547 break;
3548 default:
3549 // Any other result means only one side has to be true, the
3550 // other side can be anything. so we can't be sure of any result
3551 // here.
3552 r = range_true_and_false (type);
3553 break;
3554 }
3555 return true;
3556}
3557
3558bool
4ba9fb0a
AH
3559operator_logical_or::op2_range (irange &r, tree type,
3560 const irange &lhs,
80dd13f5 3561 const irange &op1,
b565ac19 3562 relation_trio) const
38a73435
AH
3563{
3564 return operator_logical_or::op1_range (r, type, lhs, op1);
3565}
3566
3567
b23d6b95
AM
3568void
3569operator_bitwise_or::update_bitmask (irange &r, const irange &lh,
3570 const irange &rh) const
38a73435 3571{
b23d6b95
AM
3572 update_known_bitmask (r, BIT_IOR_EXPR, lh, rh);
3573}
38a73435 3574
bb74ef9e 3575void
4ba9fb0a 3576operator_bitwise_or::wi_fold (irange &r, tree type,
38a73435
AH
3577 const wide_int &lh_lb,
3578 const wide_int &lh_ub,
3579 const wide_int &rh_lb,
3580 const wide_int &rh_ub) const
3581{
38a73435 3582 if (wi_optimize_and_or (r, BIT_IOR_EXPR, type, lh_lb, lh_ub, rh_lb, rh_ub))
bb74ef9e 3583 return;
38a73435
AH
3584
3585 wide_int maybe_nonzero_lh, mustbe_nonzero_lh;
3586 wide_int maybe_nonzero_rh, mustbe_nonzero_rh;
3587 wi_set_zero_nonzero_bits (type, lh_lb, lh_ub,
3588 maybe_nonzero_lh, mustbe_nonzero_lh);
3589 wi_set_zero_nonzero_bits (type, rh_lb, rh_ub,
3590 maybe_nonzero_rh, mustbe_nonzero_rh);
3591 wide_int new_lb = mustbe_nonzero_lh | mustbe_nonzero_rh;
3592 wide_int new_ub = maybe_nonzero_lh | maybe_nonzero_rh;
3593 signop sign = TYPE_SIGN (type);
3594 // If the input ranges contain only positive values we can
3595 // truncate the minimum of the result range to the maximum
3596 // of the input range minima.
3597 if (wi::ge_p (lh_lb, 0, sign)
3598 && wi::ge_p (rh_lb, 0, sign))
3599 {
3600 new_lb = wi::max (new_lb, lh_lb, sign);
3601 new_lb = wi::max (new_lb, rh_lb, sign);
3602 }
3603 // If either input range contains only negative values
3604 // we can truncate the minimum of the result range to the
3605 // respective minimum range.
3606 if (wi::lt_p (lh_ub, 0, sign))
3607 new_lb = wi::max (new_lb, lh_lb, sign);
3608 if (wi::lt_p (rh_ub, 0, sign))
3609 new_lb = wi::max (new_lb, rh_lb, sign);
46027143
AH
3610 // If the limits got swapped around, return a conservative range.
3611 if (wi::gt_p (new_lb, new_ub, sign))
3612 {
3613 // Make sure that nonzero|X is nonzero.
3614 if (wi::gt_p (lh_lb, 0, sign)
3615 || wi::gt_p (rh_lb, 0, sign)
3616 || wi::lt_p (lh_ub, 0, sign)
3617 || wi::lt_p (rh_ub, 0, sign))
3618 r.set_nonzero (type);
b3e98eb3
RS
3619 else if (sign == SIGNED
3620 && wi_optimize_signed_bitwise_op (r, type,
3621 lh_lb, lh_ub,
3622 rh_lb, rh_ub))
3623 return;
46027143
AH
3624 else
3625 r.set_varying (type);
3626 return;
3627 }
3628 value_range_with_overflow (r, type, new_lb, new_ub);
38a73435
AH
3629}
3630
3631bool
4ba9fb0a
AH
3632operator_bitwise_or::op1_range (irange &r, tree type,
3633 const irange &lhs,
80dd13f5 3634 const irange &op2,
b565ac19 3635 relation_trio) const
38a73435 3636{
ef9bc362
AM
3637 if (lhs.undefined_p ())
3638 return false;
38a73435
AH
3639 // If this is really a logical wi_fold, call that.
3640 if (types_compatible_p (type, boolean_type_node))
3641 return op_logical_or.op1_range (r, type, lhs, op2);
3642
4ba9fb0a
AH
3643 if (lhs.zero_p ())
3644 {
cb779afe 3645 r.set_zero (type);
4ba9fb0a
AH
3646 return true;
3647 }
38a73435
AH
3648 r.set_varying (type);
3649 return true;
3650}
3651
3652bool
4ba9fb0a
AH
3653operator_bitwise_or::op2_range (irange &r, tree type,
3654 const irange &lhs,
80dd13f5 3655 const irange &op1,
b565ac19 3656 relation_trio) const
38a73435
AH
3657{
3658 return operator_bitwise_or::op1_range (r, type, lhs, op1);
3659}
3660
af52b862
AM
3661void
3662operator_bitwise_xor::update_bitmask (irange &r, const irange &lh,
3663 const irange &rh) const
38a73435 3664{
af52b862
AM
3665 update_known_bitmask (r, BIT_XOR_EXPR, lh, rh);
3666}
38a73435 3667
bb74ef9e 3668void
4ba9fb0a 3669operator_bitwise_xor::wi_fold (irange &r, tree type,
38a73435
AH
3670 const wide_int &lh_lb,
3671 const wide_int &lh_ub,
3672 const wide_int &rh_lb,
3673 const wide_int &rh_ub) const
3674{
3675 signop sign = TYPE_SIGN (type);
3676 wide_int maybe_nonzero_lh, mustbe_nonzero_lh;
3677 wide_int maybe_nonzero_rh, mustbe_nonzero_rh;
3678 wi_set_zero_nonzero_bits (type, lh_lb, lh_ub,
3679 maybe_nonzero_lh, mustbe_nonzero_lh);
3680 wi_set_zero_nonzero_bits (type, rh_lb, rh_ub,
3681 maybe_nonzero_rh, mustbe_nonzero_rh);
3682
3683 wide_int result_zero_bits = ((mustbe_nonzero_lh & mustbe_nonzero_rh)
3684 | ~(maybe_nonzero_lh | maybe_nonzero_rh));
3685 wide_int result_one_bits
3686 = (wi::bit_and_not (mustbe_nonzero_lh, maybe_nonzero_rh)
3687 | wi::bit_and_not (mustbe_nonzero_rh, maybe_nonzero_lh));
3688 wide_int new_ub = ~result_zero_bits;
3689 wide_int new_lb = result_one_bits;
3690
3691 // If the range has all positive or all negative values, the result
3692 // is better than VARYING.
3693 if (wi::lt_p (new_lb, 0, sign) || wi::ge_p (new_ub, 0, sign))
bb74ef9e 3694 value_range_with_overflow (r, type, new_lb, new_ub);
b3e98eb3
RS
3695 else if (sign == SIGNED
3696 && wi_optimize_signed_bitwise_op (r, type,
3697 lh_lb, lh_ub,
3698 rh_lb, rh_ub))
3699 ; /* Do nothing. */
bb74ef9e 3700 else
4ba9fb0a 3701 r.set_varying (type);
b3e98eb3
RS
3702
3703 /* Furthermore, XOR is non-zero if its arguments can't be equal. */
3704 if (wi::lt_p (lh_ub, rh_lb, sign)
3705 || wi::lt_p (rh_ub, lh_lb, sign)
3706 || wi::ne_p (result_one_bits, 0))
3707 {
3708 int_range<2> tmp;
3709 tmp.set_nonzero (type);
3710 r.intersect (tmp);
3711 }
4ba9fb0a
AH
3712}
3713
f384e2f5
AH
3714bool
3715operator_bitwise_xor::op1_op2_relation_effect (irange &lhs_range,
3716 tree type,
3717 const irange &,
3718 const irange &,
3719 relation_kind rel) const
3720{
ade5531c 3721 if (rel == VREL_VARYING)
f384e2f5
AH
3722 return false;
3723
3724 int_range<2> rel_range;
3725
3726 switch (rel)
3727 {
ade5531c 3728 case VREL_EQ:
f384e2f5
AH
3729 rel_range.set_zero (type);
3730 break;
ade5531c 3731 case VREL_NE:
f384e2f5
AH
3732 rel_range.set_nonzero (type);
3733 break;
3734 default:
3735 return false;
3736 }
3737
3738 lhs_range.intersect (rel_range);
3739 return true;
3740}
3741
4ba9fb0a
AH
3742bool
3743operator_bitwise_xor::op1_range (irange &r, tree type,
3744 const irange &lhs,
80dd13f5 3745 const irange &op2,
b565ac19 3746 relation_trio) const
4ba9fb0a
AH
3747{
3748 if (lhs.undefined_p () || lhs.varying_p ())
3749 {
3750 r = lhs;
3751 return true;
3752 }
3753 if (types_compatible_p (type, boolean_type_node))
3754 {
3755 switch (get_bool_state (r, lhs, type))
3756 {
3757 case BRS_TRUE:
3758 if (op2.varying_p ())
3759 r.set_varying (type);
3760 else if (op2.zero_p ())
3761 r = range_true (type);
a8404c07 3762 // See get_bool_state for the rationale
cb779afe 3763 else if (contains_zero_p (op2))
a8404c07 3764 r = range_true_and_false (type);
4ba9fb0a
AH
3765 else
3766 r = range_false (type);
3767 break;
3768 case BRS_FALSE:
3769 r = op2;
3770 break;
3771 default:
ead233e6 3772 break;
4ba9fb0a
AH
3773 }
3774 return true;
3775 }
3776 r.set_varying (type);
3777 return true;
38a73435
AH
3778}
3779
4ba9fb0a
AH
3780bool
3781operator_bitwise_xor::op2_range (irange &r, tree type,
3782 const irange &lhs,
80dd13f5 3783 const irange &op1,
b565ac19 3784 relation_trio) const
4ba9fb0a
AH
3785{
3786 return operator_bitwise_xor::op1_range (r, type, lhs, op1);
3787}
38a73435
AH
3788
3789class operator_trunc_mod : public range_operator
3790{
cf5bea76
AH
3791 using range_operator::op1_range;
3792 using range_operator::op2_range;
38a73435 3793public:
4ba9fb0a 3794 virtual void wi_fold (irange &r, tree type,
bb74ef9e
AM
3795 const wide_int &lh_lb,
3796 const wide_int &lh_ub,
3797 const wide_int &rh_lb,
3798 const wide_int &rh_ub) const;
1e27e7a5
AM
3799 virtual bool op1_range (irange &r, tree type,
3800 const irange &lhs,
80dd13f5 3801 const irange &op2,
b565ac19 3802 relation_trio) const;
d3f29334
JJ
3803 virtual bool op2_range (irange &r, tree type,
3804 const irange &lhs,
80dd13f5 3805 const irange &op1,
b565ac19 3806 relation_trio) const;
cd4b7e8b
AM
3807 void update_bitmask (irange &r, const irange &lh, const irange &rh) const
3808 { update_known_bitmask (r, TRUNC_MOD_EXPR, lh, rh); }
38a73435
AH
3809} op_trunc_mod;
3810
bb74ef9e 3811void
4ba9fb0a 3812operator_trunc_mod::wi_fold (irange &r, tree type,
38a73435
AH
3813 const wide_int &lh_lb,
3814 const wide_int &lh_ub,
3815 const wide_int &rh_lb,
3816 const wide_int &rh_ub) const
3817{
3818 wide_int new_lb, new_ub, tmp;
3819 signop sign = TYPE_SIGN (type);
3820 unsigned prec = TYPE_PRECISION (type);
3821
82118acc 3822 // Mod 0 is undefined.
38a73435 3823 if (wi_zero_p (type, rh_lb, rh_ub))
bb74ef9e 3824 {
156054e8 3825 r.set_undefined ();
bb74ef9e
AM
3826 return;
3827 }
38a73435 3828
145bc41d
AM
3829 // Check for constant and try to fold.
3830 if (lh_lb == lh_ub && rh_lb == rh_ub)
3831 {
3832 wi::overflow_type ov = wi::OVF_NONE;
3833 tmp = wi::mod_trunc (lh_lb, rh_lb, sign, &ov);
3834 if (ov == wi::OVF_NONE)
3835 {
3836 r = int_range<2> (type, tmp, tmp);
3837 return;
3838 }
3839 }
3840
38a73435
AH
3841 // ABS (A % B) < ABS (B) and either 0 <= A % B <= A or A <= A % B <= 0.
3842 new_ub = rh_ub - 1;
3843 if (sign == SIGNED)
3844 {
3845 tmp = -1 - rh_lb;
3846 new_ub = wi::smax (new_ub, tmp);
3847 }
3848
3849 if (sign == UNSIGNED)
3850 new_lb = wi::zero (prec);
3851 else
3852 {
3853 new_lb = -new_ub;
3854 tmp = lh_lb;
3855 if (wi::gts_p (tmp, 0))
3856 tmp = wi::zero (prec);
3857 new_lb = wi::smax (new_lb, tmp);
3858 }
3859 tmp = lh_ub;
3860 if (sign == SIGNED && wi::neg_p (tmp))
3861 tmp = wi::zero (prec);
3862 new_ub = wi::min (new_ub, tmp, sign);
3863
bb74ef9e 3864 value_range_with_overflow (r, type, new_lb, new_ub);
38a73435
AH
3865}
3866
1e27e7a5
AM
3867bool
3868operator_trunc_mod::op1_range (irange &r, tree type,
3869 const irange &lhs,
80dd13f5 3870 const irange &,
b565ac19 3871 relation_trio) const
1e27e7a5 3872{
ef9bc362
AM
3873 if (lhs.undefined_p ())
3874 return false;
d3f29334
JJ
3875 // PR 91029.
3876 signop sign = TYPE_SIGN (type);
3877 unsigned prec = TYPE_PRECISION (type);
3878 // (a % b) >= x && x > 0 , then a >= x.
3879 if (wi::gt_p (lhs.lower_bound (), 0, sign))
1e27e7a5 3880 {
d3f29334
JJ
3881 r = value_range (type, lhs.lower_bound (), wi::max_value (prec, sign));
3882 return true;
3883 }
3884 // (a % b) <= x && x < 0 , then a <= x.
3885 if (wi::lt_p (lhs.upper_bound (), 0, sign))
3886 {
3887 r = value_range (type, wi::min_value (prec, sign), lhs.upper_bound ());
3888 return true;
3889 }
3890 return false;
3891}
3892
3893bool
3894operator_trunc_mod::op2_range (irange &r, tree type,
3895 const irange &lhs,
80dd13f5 3896 const irange &,
b565ac19 3897 relation_trio) const
d3f29334 3898{
ef9bc362
AM
3899 if (lhs.undefined_p ())
3900 return false;
d3f29334
JJ
3901 // PR 91029.
3902 signop sign = TYPE_SIGN (type);
3903 unsigned prec = TYPE_PRECISION (type);
3904 // (a % b) >= x && x > 0 , then b is in ~[-x, x] for signed
3905 // or b > x for unsigned.
3906 if (wi::gt_p (lhs.lower_bound (), 0, sign))
3907 {
3908 if (sign == SIGNED)
3909 r = value_range (type, wi::neg (lhs.lower_bound ()),
3910 lhs.lower_bound (), VR_ANTI_RANGE);
3911 else if (wi::lt_p (lhs.lower_bound (), wi::max_value (prec, sign),
3912 sign))
3913 r = value_range (type, lhs.lower_bound () + 1,
3914 wi::max_value (prec, sign));
3915 else
3916 return false;
3917 return true;
3918 }
3919 // (a % b) <= x && x < 0 , then b is in ~[x, -x].
3920 if (wi::lt_p (lhs.upper_bound (), 0, sign))
3921 {
3922 if (wi::gt_p (lhs.upper_bound (), wi::min_value (prec, sign), sign))
3923 r = value_range (type, lhs.upper_bound (),
3924 wi::neg (lhs.upper_bound ()), VR_ANTI_RANGE);
3925 else
3926 return false;
3927 return true;
1e27e7a5
AM
3928 }
3929 return false;
3930}
3931
38a73435
AH
3932
3933class operator_logical_not : public range_operator
3934{
cf5bea76
AH
3935 using range_operator::fold_range;
3936 using range_operator::op1_range;
38a73435 3937public:
4ba9fb0a
AH
3938 virtual bool fold_range (irange &r, tree type,
3939 const irange &lh,
80dd13f5 3940 const irange &rh,
b565ac19 3941 relation_trio rel = TRIO_VARYING) const;
4ba9fb0a
AH
3942 virtual bool op1_range (irange &r, tree type,
3943 const irange &lhs,
80dd13f5 3944 const irange &op2,
b565ac19 3945 relation_trio rel = TRIO_VARYING) const;
38a73435
AH
3946} op_logical_not;
3947
3948// Folding a logical NOT, oddly enough, involves doing nothing on the
3949// forward pass through. During the initial walk backwards, the
3950// logical NOT reversed the desired outcome on the way back, so on the
3951// way forward all we do is pass the range forward.
3952//
3953// b_2 = x_1 < 20
3954// b_3 = !b_2
3955// if (b_3)
3956// to determine the TRUE branch, walking backward
3957// if (b_3) if ([1,1])
3958// b_3 = !b_2 [1,1] = ![0,0]
3959// b_2 = x_1 < 20 [0,0] = x_1 < 20, false, so x_1 == [20, 255]
3960// which is the result we are looking for.. so.. pass it through.
3961
f674b4a7 3962bool
4ba9fb0a
AH
3963operator_logical_not::fold_range (irange &r, tree type,
3964 const irange &lh,
80dd13f5 3965 const irange &rh ATTRIBUTE_UNUSED,
b565ac19 3966 relation_trio) const
38a73435 3967{
4ba9fb0a 3968 if (empty_range_varying (r, type, lh, rh))
f674b4a7 3969 return true;
38a73435 3970
61dd8dab
EB
3971 r = lh;
3972 if (!lh.varying_p () && !lh.undefined_p ())
3973 r.invert ();
3974
f674b4a7 3975 return true;
38a73435
AH
3976}
3977
3978bool
4ba9fb0a 3979operator_logical_not::op1_range (irange &r,
61dd8dab 3980 tree type,
4ba9fb0a 3981 const irange &lhs,
80dd13f5 3982 const irange &op2,
b565ac19 3983 relation_trio) const
38a73435 3984{
61dd8dab
EB
3985 // Logical NOT is involutary...do it again.
3986 return fold_range (r, type, lhs, op2);
38a73435
AH
3987}
3988
3989
f674b4a7 3990bool
4ba9fb0a
AH
3991operator_bitwise_not::fold_range (irange &r, tree type,
3992 const irange &lh,
80dd13f5 3993 const irange &rh,
b565ac19 3994 relation_trio) const
38a73435 3995{
4ba9fb0a 3996 if (empty_range_varying (r, type, lh, rh))
f674b4a7 3997 return true;
38a73435 3998
61dd8dab
EB
3999 if (types_compatible_p (type, boolean_type_node))
4000 return op_logical_not.fold_range (r, type, lh, rh);
4001
38a73435 4002 // ~X is simply -1 - X.
4ba9fb0a
AH
4003 int_range<1> minusone (type, wi::minus_one (TYPE_PRECISION (type)),
4004 wi::minus_one (TYPE_PRECISION (type)));
2eb50117 4005 return range_op_handler (MINUS_EXPR).fold_range (r, type, minusone, lh);
38a73435
AH
4006}
4007
4008bool
4ba9fb0a
AH
4009operator_bitwise_not::op1_range (irange &r, tree type,
4010 const irange &lhs,
80dd13f5 4011 const irange &op2,
b565ac19 4012 relation_trio) const
38a73435 4013{
ef9bc362
AM
4014 if (lhs.undefined_p ())
4015 return false;
61dd8dab
EB
4016 if (types_compatible_p (type, boolean_type_node))
4017 return op_logical_not.op1_range (r, type, lhs, op2);
4018
38a73435 4019 // ~X is -1 - X and since bitwise NOT is involutary...do it again.
f674b4a7 4020 return fold_range (r, type, lhs, op2);
38a73435
AH
4021}
4022
4a188dee
AH
4023void
4024operator_bitwise_not::update_bitmask (irange &r, const irange &lh,
4025 const irange &rh) const
4026{
4027 update_known_bitmask (r, BIT_NOT_EXPR, lh, rh);
4028}
4029
38a73435 4030
f674b4a7 4031bool
4ba9fb0a
AH
4032operator_cst::fold_range (irange &r, tree type ATTRIBUTE_UNUSED,
4033 const irange &lh,
80dd13f5 4034 const irange &rh ATTRIBUTE_UNUSED,
b565ac19 4035 relation_trio) const
38a73435 4036{
bb74ef9e 4037 r = lh;
f674b4a7 4038 return true;
38a73435
AH
4039}
4040
4041
0f7ccc06
AM
4042// Determine if there is a relationship between LHS and OP1.
4043
ade5531c 4044relation_kind
0f7ccc06
AM
4045operator_identity::lhs_op1_relation (const irange &lhs,
4046 const irange &op1 ATTRIBUTE_UNUSED,
cf2141a0
AM
4047 const irange &op2 ATTRIBUTE_UNUSED,
4048 relation_kind) const
0f7ccc06
AM
4049{
4050 if (lhs.undefined_p ())
ade5531c 4051 return VREL_VARYING;
0f7ccc06 4052 // Simply a copy, so they are equivalent.
ade5531c 4053 return VREL_EQ;
0f7ccc06
AM
4054}
4055
f674b4a7 4056bool
4ba9fb0a
AH
4057operator_identity::fold_range (irange &r, tree type ATTRIBUTE_UNUSED,
4058 const irange &lh,
80dd13f5 4059 const irange &rh ATTRIBUTE_UNUSED,
b565ac19 4060 relation_trio) const
38a73435 4061{
bb74ef9e 4062 r = lh;
f674b4a7 4063 return true;
38a73435
AH
4064}
4065
4066bool
4ba9fb0a
AH
4067operator_identity::op1_range (irange &r, tree type ATTRIBUTE_UNUSED,
4068 const irange &lhs,
80dd13f5 4069 const irange &op2 ATTRIBUTE_UNUSED,
b565ac19 4070 relation_trio) const
38a73435
AH
4071{
4072 r = lhs;
4073 return true;
4074}
4075
4076
4ba9fb0a
AH
4077class operator_unknown : public range_operator
4078{
cf5bea76 4079 using range_operator::fold_range;
4ba9fb0a
AH
4080public:
4081 virtual bool fold_range (irange &r, tree type,
4082 const irange &op1,
80dd13f5 4083 const irange &op2,
b565ac19 4084 relation_trio rel = TRIO_VARYING) const;
cd4b7e8b 4085} op_unknown;
4ba9fb0a
AH
4086
4087bool
4088operator_unknown::fold_range (irange &r, tree type,
4089 const irange &lh ATTRIBUTE_UNUSED,
80dd13f5 4090 const irange &rh ATTRIBUTE_UNUSED,
b565ac19 4091 relation_trio) const
4ba9fb0a
AH
4092{
4093 r.set_varying (type);
4094 return true;
4095}
4096
4097
bb74ef9e 4098void
4ba9fb0a 4099operator_abs::wi_fold (irange &r, tree type,
38a73435
AH
4100 const wide_int &lh_lb, const wide_int &lh_ub,
4101 const wide_int &rh_lb ATTRIBUTE_UNUSED,
4102 const wide_int &rh_ub ATTRIBUTE_UNUSED) const
4103{
4104 wide_int min, max;
4105 signop sign = TYPE_SIGN (type);
4106 unsigned prec = TYPE_PRECISION (type);
4107
4108 // Pass through LH for the easy cases.
4109 if (sign == UNSIGNED || wi::ge_p (lh_lb, 0, sign))
bb74ef9e 4110 {
4ba9fb0a 4111 r = int_range<1> (type, lh_lb, lh_ub);
bb74ef9e
AM
4112 return;
4113 }
38a73435
AH
4114
4115 // -TYPE_MIN_VALUE = TYPE_MIN_VALUE with flag_wrapv so we can't get
4116 // a useful range.
4117 wide_int min_value = wi::min_value (prec, sign);
4118 wide_int max_value = wi::max_value (prec, sign);
4119 if (!TYPE_OVERFLOW_UNDEFINED (type) && wi::eq_p (lh_lb, min_value))
bb74ef9e 4120 {
4ba9fb0a 4121 r.set_varying (type);
bb74ef9e
AM
4122 return;
4123 }
38a73435
AH
4124
4125 // ABS_EXPR may flip the range around, if the original range
4126 // included negative values.
4127 if (wi::eq_p (lh_lb, min_value))
bd431d26
AH
4128 {
4129 // ABS ([-MIN, -MIN]) isn't representable, but we have traditionally
c46b5b0a 4130 // returned [-MIN,-MIN] so this preserves that behavior. PR37078
bd431d26
AH
4131 if (wi::eq_p (lh_ub, min_value))
4132 {
4133 r = int_range<1> (type, min_value, min_value);
4134 return;
4135 }
4136 min = max_value;
4137 }
38a73435
AH
4138 else
4139 min = wi::abs (lh_lb);
bd431d26 4140
38a73435
AH
4141 if (wi::eq_p (lh_ub, min_value))
4142 max = max_value;
4143 else
4144 max = wi::abs (lh_ub);
4145
4146 // If the range contains zero then we know that the minimum value in the
4147 // range will be zero.
4148 if (wi::le_p (lh_lb, 0, sign) && wi::ge_p (lh_ub, 0, sign))
4149 {
4150 if (wi::gt_p (min, max, sign))
4151 max = min;
4152 min = wi::zero (prec);
4153 }
4154 else
4155 {
4156 // If the range was reversed, swap MIN and MAX.
4157 if (wi::gt_p (min, max, sign))
4158 std::swap (min, max);
4159 }
4160
4161 // If the new range has its limits swapped around (MIN > MAX), then
4162 // the operation caused one of them to wrap around. The only thing
4163 // we know is that the result is positive.
4164 if (wi::gt_p (min, max, sign))
4165 {
4166 min = wi::zero (prec);
4167 max = max_value;
4168 }
4ba9fb0a 4169 r = int_range<1> (type, min, max);
38a73435
AH
4170}
4171
4172bool
4ba9fb0a
AH
4173operator_abs::op1_range (irange &r, tree type,
4174 const irange &lhs,
80dd13f5 4175 const irange &op2,
b565ac19 4176 relation_trio) const
38a73435 4177{
4ba9fb0a 4178 if (empty_range_varying (r, type, lhs, op2))
38a73435
AH
4179 return true;
4180 if (TYPE_UNSIGNED (type))
4181 {
4182 r = lhs;
4183 return true;
4184 }
4185 // Start with the positives because negatives are an impossible result.
c5a6c223 4186 int_range_max positives = range_positives (type);
38a73435
AH
4187 positives.intersect (lhs);
4188 r = positives;
4189 // Then add the negative of each pair:
4190 // ABS(op1) = [5,20] would yield op1 => [-20,-5][5,20].
4191 for (unsigned i = 0; i < positives.num_pairs (); ++i)
4ba9fb0a
AH
4192 r.union_ (int_range<1> (type,
4193 -positives.upper_bound (i),
4194 -positives.lower_bound (i)));
891bdbf2
AM
4195 // With flag_wrapv, -TYPE_MIN_VALUE = TYPE_MIN_VALUE which is
4196 // unrepresentable. Add -TYPE_MIN_VALUE in this case.
4197 wide_int min_value = wi::min_value (TYPE_PRECISION (type), TYPE_SIGN (type));
4198 wide_int lb = lhs.lower_bound ();
4199 if (!TYPE_OVERFLOW_UNDEFINED (type) && wi::eq_p (lb, min_value))
4200 r.union_ (int_range<2> (type, lb, lb));
38a73435
AH
4201 return true;
4202}
4203
5346a2fc
AH
4204void
4205operator_abs::update_bitmask (irange &r, const irange &lh,
4206 const irange &rh) const
4207{
4208 update_known_bitmask (r, ABS_EXPR, lh, rh);
4209}
38a73435
AH
4210
4211class operator_absu : public range_operator
4212{
4213 public:
4ba9fb0a 4214 virtual void wi_fold (irange &r, tree type,
bb74ef9e
AM
4215 const wide_int &lh_lb, const wide_int &lh_ub,
4216 const wide_int &rh_lb, const wide_int &rh_ub) const;
39f117d6
AH
4217 virtual void update_bitmask (irange &r, const irange &lh,
4218 const irange &rh) const final override;
38a73435
AH
4219} op_absu;
4220
bb74ef9e 4221void
4ba9fb0a 4222operator_absu::wi_fold (irange &r, tree type,
38a73435
AH
4223 const wide_int &lh_lb, const wide_int &lh_ub,
4224 const wide_int &rh_lb ATTRIBUTE_UNUSED,
4225 const wide_int &rh_ub ATTRIBUTE_UNUSED) const
4226{
4227 wide_int new_lb, new_ub;
4228
4229 // Pass through VR0 the easy cases.
4230 if (wi::ges_p (lh_lb, 0))
4231 {
4232 new_lb = lh_lb;
4233 new_ub = lh_ub;
4234 }
4235 else
4236 {
4237 new_lb = wi::abs (lh_lb);
4238 new_ub = wi::abs (lh_ub);
4239
4240 // If the range contains zero then we know that the minimum
4241 // value in the range will be zero.
4242 if (wi::ges_p (lh_ub, 0))
4243 {
4244 if (wi::gtu_p (new_lb, new_ub))
4245 new_ub = new_lb;
4246 new_lb = wi::zero (TYPE_PRECISION (type));
4247 }
4248 else
4249 std::swap (new_lb, new_ub);
4250 }
4251
4252 gcc_checking_assert (TYPE_UNSIGNED (type));
4ba9fb0a 4253 r = int_range<1> (type, new_lb, new_ub);
38a73435
AH
4254}
4255
39f117d6
AH
4256void
4257operator_absu::update_bitmask (irange &r, const irange &lh,
4258 const irange &rh) const
4259{
4260 update_known_bitmask (r, ABSU_EXPR, lh, rh);
4261}
4262
38a73435 4263
f674b4a7 4264bool
4ba9fb0a
AH
4265operator_negate::fold_range (irange &r, tree type,
4266 const irange &lh,
80dd13f5 4267 const irange &rh,
b565ac19 4268 relation_trio) const
38a73435 4269{
4ba9fb0a 4270 if (empty_range_varying (r, type, lh, rh))
f674b4a7 4271 return true;
38a73435 4272 // -X is simply 0 - X.
2eb50117
AM
4273 return range_op_handler (MINUS_EXPR).fold_range (r, type,
4274 range_zero (type), lh);
38a73435
AH
4275}
4276
4277bool
4ba9fb0a
AH
4278operator_negate::op1_range (irange &r, tree type,
4279 const irange &lhs,
80dd13f5 4280 const irange &op2,
b565ac19 4281 relation_trio) const
38a73435
AH
4282{
4283 // NEGATE is involutory.
f674b4a7 4284 return fold_range (r, type, lhs, op2);
38a73435
AH
4285}
4286
4287
f674b4a7 4288bool
4ba9fb0a
AH
4289operator_addr_expr::fold_range (irange &r, tree type,
4290 const irange &lh,
80dd13f5 4291 const irange &rh,
b565ac19 4292 relation_trio) const
38a73435 4293{
4ba9fb0a 4294 if (empty_range_varying (r, type, lh, rh))
f674b4a7 4295 return true;
38a73435
AH
4296
4297 // Return a non-null pointer of the LHS type (passed in op2).
4298 if (lh.zero_p ())
bb74ef9e 4299 r = range_zero (type);
cb779afe 4300 else if (!contains_zero_p (lh))
bb74ef9e
AM
4301 r = range_nonzero (type);
4302 else
4ba9fb0a 4303 r.set_varying (type);
f674b4a7 4304 return true;
38a73435
AH
4305}
4306
4307bool
4ba9fb0a
AH
4308operator_addr_expr::op1_range (irange &r, tree type,
4309 const irange &lhs,
80dd13f5 4310 const irange &op2,
b565ac19 4311 relation_trio) const
38a73435 4312{
f674b4a7 4313 return operator_addr_expr::fold_range (r, type, lhs, op2);
38a73435 4314}
38a73435 4315\f
07767389
AM
4316// Initialize any integral operators to the primary table
4317
4318void
4319range_op_table::initialize_integral_ops ()
4320{
38a73435
AH
4321 set (TRUNC_DIV_EXPR, op_trunc_div);
4322 set (FLOOR_DIV_EXPR, op_floor_div);
4323 set (ROUND_DIV_EXPR, op_round_div);
4324 set (CEIL_DIV_EXPR, op_ceil_div);
4325 set (EXACT_DIV_EXPR, op_exact_div);
4326 set (LSHIFT_EXPR, op_lshift);
4327 set (RSHIFT_EXPR, op_rshift);
38a73435 4328 set (TRUTH_AND_EXPR, op_logical_and);
38a73435 4329 set (TRUTH_OR_EXPR, op_logical_or);
38a73435
AH
4330 set (TRUNC_MOD_EXPR, op_trunc_mod);
4331 set (TRUTH_NOT_EXPR, op_logical_not);
cd4b7e8b
AM
4332 set (IMAGPART_EXPR, op_unknown);
4333 set (REALPART_EXPR, op_unknown);
38a73435 4334 set (ABSU_EXPR, op_absu);
5410b07a
AM
4335 set (OP_WIDEN_MULT_SIGNED, op_widen_mult_signed);
4336 set (OP_WIDEN_MULT_UNSIGNED, op_widen_mult_unsigned);
4337 set (OP_WIDEN_PLUS_SIGNED, op_widen_plus_signed);
4338 set (OP_WIDEN_PLUS_UNSIGNED, op_widen_plus_unsigned);
4339
38a73435
AH
4340}
4341
38a73435
AH
4342#if CHECKING_P
4343#include "selftest.h"
38a73435 4344
f1471317
AH
4345namespace selftest
4346{
cb779afe
AH
4347#define INT(x) wi::shwi ((x), TYPE_PRECISION (integer_type_node))
4348#define UINT(x) wi::uhwi ((x), TYPE_PRECISION (unsigned_type_node))
4349#define INT16(x) wi::shwi ((x), TYPE_PRECISION (short_integer_type_node))
4350#define UINT16(x) wi::uhwi ((x), TYPE_PRECISION (short_unsigned_type_node))
4351#define SCHAR(x) wi::shwi ((x), TYPE_PRECISION (signed_char_type_node))
4352#define UCHAR(x) wi::uhwi ((x), TYPE_PRECISION (unsigned_char_type_node))
4ba9fb0a
AH
4353
4354static void
b5cff0db 4355range_op_cast_tests ()
38a73435 4356{
0ef3756a 4357 int_range<2> r0, r1, r2, rold;
38a73435 4358 r0.set_varying (integer_type_node);
cb779afe 4359 wide_int maxint = r0.upper_bound ();
38a73435 4360
b5cff0db
AH
4361 // If a range is in any way outside of the range for the converted
4362 // to range, default to the range for the new type.
38a73435 4363 r0.set_varying (short_integer_type_node);
cb779afe
AH
4364 wide_int minshort = r0.lower_bound ();
4365 wide_int maxshort = r0.upper_bound ();
4366 if (TYPE_PRECISION (integer_type_node)
82de69ff
JL
4367 > TYPE_PRECISION (short_integer_type_node))
4368 {
cb779afe
AH
4369 r1 = int_range<1> (integer_type_node,
4370 wi::zero (TYPE_PRECISION (integer_type_node)),
4371 maxint);
82de69ff 4372 range_cast (r1, short_integer_type_node);
cb779afe
AH
4373 ASSERT_TRUE (r1.lower_bound () == minshort
4374 && r1.upper_bound() == maxshort);
82de69ff 4375 }
38a73435
AH
4376
4377 // (unsigned char)[-5,-1] => [251,255].
cb779afe 4378 r0 = rold = int_range<1> (signed_char_type_node, SCHAR (-5), SCHAR (-1));
38a73435 4379 range_cast (r0, unsigned_char_type_node);
cb779afe
AH
4380 ASSERT_TRUE (r0 == int_range<1> (unsigned_char_type_node,
4381 UCHAR (251), UCHAR (255)));
38a73435
AH
4382 range_cast (r0, signed_char_type_node);
4383 ASSERT_TRUE (r0 == rold);
4384
4385 // (signed char)[15, 150] => [-128,-106][15,127].
cb779afe 4386 r0 = rold = int_range<1> (unsigned_char_type_node, UCHAR (15), UCHAR (150));
38a73435 4387 range_cast (r0, signed_char_type_node);
cb779afe
AH
4388 r1 = int_range<1> (signed_char_type_node, SCHAR (15), SCHAR (127));
4389 r2 = int_range<1> (signed_char_type_node, SCHAR (-128), SCHAR (-106));
38a73435
AH
4390 r1.union_ (r2);
4391 ASSERT_TRUE (r1 == r0);
4392 range_cast (r0, unsigned_char_type_node);
4393 ASSERT_TRUE (r0 == rold);
4394
4395 // (unsigned char)[-5, 5] => [0,5][251,255].
cb779afe 4396 r0 = rold = int_range<1> (signed_char_type_node, SCHAR (-5), SCHAR (5));
38a73435 4397 range_cast (r0, unsigned_char_type_node);
cb779afe
AH
4398 r1 = int_range<1> (unsigned_char_type_node, UCHAR (251), UCHAR (255));
4399 r2 = int_range<1> (unsigned_char_type_node, UCHAR (0), UCHAR (5));
38a73435
AH
4400 r1.union_ (r2);
4401 ASSERT_TRUE (r0 == r1);
4402 range_cast (r0, signed_char_type_node);
4403 ASSERT_TRUE (r0 == rold);
4404
4405 // (unsigned char)[-5,5] => [0,5][251,255].
cb779afe 4406 r0 = int_range<1> (integer_type_node, INT (-5), INT (5));
38a73435 4407 range_cast (r0, unsigned_char_type_node);
cb779afe
AH
4408 r1 = int_range<1> (unsigned_char_type_node, UCHAR (0), UCHAR (5));
4409 r1.union_ (int_range<1> (unsigned_char_type_node, UCHAR (251), UCHAR (255)));
38a73435
AH
4410 ASSERT_TRUE (r0 == r1);
4411
4412 // (unsigned char)[5U,1974U] => [0,255].
cb779afe 4413 r0 = int_range<1> (unsigned_type_node, UINT (5), UINT (1974));
38a73435 4414 range_cast (r0, unsigned_char_type_node);
cb779afe 4415 ASSERT_TRUE (r0 == int_range<1> (unsigned_char_type_node, UCHAR (0), UCHAR (255)));
38a73435
AH
4416 range_cast (r0, integer_type_node);
4417 // Going to a wider range should not sign extend.
cb779afe 4418 ASSERT_TRUE (r0 == int_range<1> (integer_type_node, INT (0), INT (255)));
38a73435
AH
4419
4420 // (unsigned char)[-350,15] => [0,255].
cb779afe 4421 r0 = int_range<1> (integer_type_node, INT (-350), INT (15));
38a73435 4422 range_cast (r0, unsigned_char_type_node);
4ba9fb0a 4423 ASSERT_TRUE (r0 == (int_range<1>
cb779afe
AH
4424 (unsigned_char_type_node,
4425 min_limit (unsigned_char_type_node),
4426 max_limit (unsigned_char_type_node))));
38a73435
AH
4427
4428 // Casting [-120,20] from signed char to unsigned short.
4429 // => [0, 20][0xff88, 0xffff].
cb779afe 4430 r0 = int_range<1> (signed_char_type_node, SCHAR (-120), SCHAR (20));
38a73435 4431 range_cast (r0, short_unsigned_type_node);
cb779afe
AH
4432 r1 = int_range<1> (short_unsigned_type_node, UINT16 (0), UINT16 (20));
4433 r2 = int_range<1> (short_unsigned_type_node,
4434 UINT16 (0xff88), UINT16 (0xffff));
38a73435
AH
4435 r1.union_ (r2);
4436 ASSERT_TRUE (r0 == r1);
4437 // A truncating cast back to signed char will work because [-120, 20]
4438 // is representable in signed char.
4439 range_cast (r0, signed_char_type_node);
cb779afe
AH
4440 ASSERT_TRUE (r0 == int_range<1> (signed_char_type_node,
4441 SCHAR (-120), SCHAR (20)));
38a73435
AH
4442
4443 // unsigned char -> signed short
4444 // (signed short)[(unsigned char)25, (unsigned char)250]
4445 // => [(signed short)25, (signed short)250]
cb779afe 4446 r0 = rold = int_range<1> (unsigned_char_type_node, UCHAR (25), UCHAR (250));
38a73435 4447 range_cast (r0, short_integer_type_node);
cb779afe 4448 r1 = int_range<1> (short_integer_type_node, INT16 (25), INT16 (250));
38a73435
AH
4449 ASSERT_TRUE (r0 == r1);
4450 range_cast (r0, unsigned_char_type_node);
4451 ASSERT_TRUE (r0 == rold);
4452
c46b5b0a 4453 // Test casting a wider signed [-MIN,MAX] to a narrower unsigned.
cb779afe
AH
4454 r0 = int_range<1> (long_long_integer_type_node,
4455 min_limit (long_long_integer_type_node),
4456 max_limit (long_long_integer_type_node));
38a73435 4457 range_cast (r0, short_unsigned_type_node);
cb779afe
AH
4458 r1 = int_range<1> (short_unsigned_type_node,
4459 min_limit (short_unsigned_type_node),
4460 max_limit (short_unsigned_type_node));
38a73435
AH
4461 ASSERT_TRUE (r0 == r1);
4462
38a73435
AH
4463 // Casting NONZERO to a narrower type will wrap/overflow so
4464 // it's just the entire range for the narrower type.
4465 //
4466 // "NOT 0 at signed 32-bits" ==> [-MIN_32,-1][1, +MAX_32]. This is
4467 // is outside of the range of a smaller range, return the full
4468 // smaller range.
82de69ff
JL
4469 if (TYPE_PRECISION (integer_type_node)
4470 > TYPE_PRECISION (short_integer_type_node))
4471 {
4472 r0 = range_nonzero (integer_type_node);
4473 range_cast (r0, short_integer_type_node);
cb779afe
AH
4474 r1 = int_range<1> (short_integer_type_node,
4475 min_limit (short_integer_type_node),
4476 max_limit (short_integer_type_node));
82de69ff
JL
4477 ASSERT_TRUE (r0 == r1);
4478 }
38a73435
AH
4479
4480 // Casting NONZERO from a narrower signed to a wider signed.
4481 //
4482 // NONZERO signed 16-bits is [-MIN_16,-1][1, +MAX_16].
4483 // Converting this to 32-bits signed is [-MIN_16,-1][1, +MAX_16].
4484 r0 = range_nonzero (short_integer_type_node);
4485 range_cast (r0, integer_type_node);
cb779afe
AH
4486 r1 = int_range<1> (integer_type_node, INT (-32768), INT (-1));
4487 r2 = int_range<1> (integer_type_node, INT (1), INT (32767));
38a73435
AH
4488 r1.union_ (r2);
4489 ASSERT_TRUE (r0 == r1);
b5cff0db 4490}
38a73435 4491
b5cff0db
AH
4492static void
4493range_op_lshift_tests ()
4494{
4495 // Test that 0x808.... & 0x8.... still contains 0x8....
4496 // for a large set of numbers.
4497 {
4498 int_range_max res;
4499 tree big_type = long_long_unsigned_type_node;
cb779afe 4500 unsigned big_prec = TYPE_PRECISION (big_type);
b5cff0db 4501 // big_num = 0x808,0000,0000,0000
cb779afe
AH
4502 wide_int big_num = wi::lshift (wi::uhwi (0x808, big_prec),
4503 wi::uhwi (48, big_prec));
b5cff0db
AH
4504 op_bitwise_and.fold_range (res, big_type,
4505 int_range <1> (big_type),
cb779afe 4506 int_range <1> (big_type, big_num, big_num));
b5cff0db 4507 // val = 0x8,0000,0000,0000
cb779afe
AH
4508 wide_int val = wi::lshift (wi::uhwi (8, big_prec),
4509 wi::uhwi (48, big_prec));
b5cff0db
AH
4510 ASSERT_TRUE (res.contains_p (val));
4511 }
38a73435 4512
b5cff0db
AH
4513 if (TYPE_PRECISION (unsigned_type_node) > 31)
4514 {
4515 // unsigned VARYING = op1 << 1 should be VARYING.
4516 int_range<2> lhs (unsigned_type_node);
cb779afe 4517 int_range<2> shift (unsigned_type_node, INT (1), INT (1));
b5cff0db
AH
4518 int_range_max op1;
4519 op_lshift.op1_range (op1, unsigned_type_node, lhs, shift);
4520 ASSERT_TRUE (op1.varying_p ());
4521
4522 // 0 = op1 << 1 should be [0,0], [0x8000000, 0x8000000].
cb779afe 4523 int_range<2> zero (unsigned_type_node, UINT (0), UINT (0));
b5cff0db
AH
4524 op_lshift.op1_range (op1, unsigned_type_node, zero, shift);
4525 ASSERT_TRUE (op1.num_pairs () == 2);
4526 // Remove the [0,0] range.
4527 op1.intersect (zero);
4528 ASSERT_TRUE (op1.num_pairs () == 1);
4529 // op1 << 1 should be [0x8000,0x8000] << 1,
4530 // which should result in [0,0].
4531 int_range_max result;
4532 op_lshift.fold_range (result, unsigned_type_node, op1, shift);
4533 ASSERT_TRUE (result == zero);
4534 }
4535 // signed VARYING = op1 << 1 should be VARYING.
4536 if (TYPE_PRECISION (integer_type_node) > 31)
4537 {
c46b5b0a 4538 // unsigned VARYING = op1 << 1 should be VARYING.
b5cff0db 4539 int_range<2> lhs (integer_type_node);
cb779afe 4540 int_range<2> shift (integer_type_node, INT (1), INT (1));
b5cff0db
AH
4541 int_range_max op1;
4542 op_lshift.op1_range (op1, integer_type_node, lhs, shift);
4543 ASSERT_TRUE (op1.varying_p ());
4544
4545 // 0 = op1 << 1 should be [0,0], [0x8000000, 0x8000000].
cb779afe 4546 int_range<2> zero (integer_type_node, INT (0), INT (0));
b5cff0db
AH
4547 op_lshift.op1_range (op1, integer_type_node, zero, shift);
4548 ASSERT_TRUE (op1.num_pairs () == 2);
4549 // Remove the [0,0] range.
4550 op1.intersect (zero);
4551 ASSERT_TRUE (op1.num_pairs () == 1);
c46b5b0a 4552 // op1 << 1 should be [0x8000,0x8000] << 1,
b5cff0db
AH
4553 // which should result in [0,0].
4554 int_range_max result;
4555 op_lshift.fold_range (result, unsigned_type_node, op1, shift);
4556 ASSERT_TRUE (result == zero);
4557 }
4558}
4559
4560static void
4561range_op_rshift_tests ()
4562{
4563 // unsigned: [3, MAX] = OP1 >> 1
4564 {
cb779afe
AH
4565 int_range_max lhs (unsigned_type_node,
4566 UINT (3), max_limit (unsigned_type_node));
4567 int_range_max one (unsigned_type_node,
4568 wi::one (TYPE_PRECISION (unsigned_type_node)),
4569 wi::one (TYPE_PRECISION (unsigned_type_node)));
b5cff0db
AH
4570 int_range_max op1;
4571 op_rshift.op1_range (op1, unsigned_type_node, lhs, one);
4572 ASSERT_FALSE (op1.contains_p (UINT (3)));
4573 }
4574
4575 // signed: [3, MAX] = OP1 >> 1
4576 {
cb779afe
AH
4577 int_range_max lhs (integer_type_node,
4578 INT (3), max_limit (integer_type_node));
4579 int_range_max one (integer_type_node, INT (1), INT (1));
b5cff0db
AH
4580 int_range_max op1;
4581 op_rshift.op1_range (op1, integer_type_node, lhs, one);
4582 ASSERT_FALSE (op1.contains_p (INT (-2)));
4583 }
4584
4585 // This is impossible, so OP1 should be [].
4586 // signed: [MIN, MIN] = OP1 >> 1
4587 {
cb779afe
AH
4588 int_range_max lhs (integer_type_node,
4589 min_limit (integer_type_node),
4590 min_limit (integer_type_node));
4591 int_range_max one (integer_type_node, INT (1), INT (1));
b5cff0db
AH
4592 int_range_max op1;
4593 op_rshift.op1_range (op1, integer_type_node, lhs, one);
4594 ASSERT_TRUE (op1.undefined_p ());
4595 }
4596
4597 // signed: ~[-1] = OP1 >> 31
4598 if (TYPE_PRECISION (integer_type_node) > 31)
4599 {
cb779afe
AH
4600 int_range_max lhs (integer_type_node, INT (-1), INT (-1), VR_ANTI_RANGE);
4601 int_range_max shift (integer_type_node, INT (31), INT (31));
b5cff0db
AH
4602 int_range_max op1;
4603 op_rshift.op1_range (op1, integer_type_node, lhs, shift);
4604 int_range_max negatives = range_negatives (integer_type_node);
4605 negatives.intersect (op1);
4606 ASSERT_TRUE (negatives.undefined_p ());
4607 }
4608}
4609
4610static void
4611range_op_bitwise_and_tests ()
4612{
4613 int_range_max res;
cb779afe
AH
4614 wide_int min = min_limit (integer_type_node);
4615 wide_int max = max_limit (integer_type_node);
4616 wide_int tiny = wi::add (min, wi::one (TYPE_PRECISION (integer_type_node)));
4617 int_range_max i1 (integer_type_node, tiny, max);
4618 int_range_max i2 (integer_type_node, INT (255), INT (255));
b5cff0db
AH
4619
4620 // [MIN+1, MAX] = OP1 & 255: OP1 is VARYING
4621 op_bitwise_and.op1_range (res, integer_type_node, i1, i2);
4622 ASSERT_TRUE (res == int_range<1> (integer_type_node));
4623
4624 // VARYING = OP1 & 255: OP1 is VARYING
4625 i1 = int_range<1> (integer_type_node);
4626 op_bitwise_and.op1_range (res, integer_type_node, i1, i2);
4627 ASSERT_TRUE (res == int_range<1> (integer_type_node));
46027143 4628
5e77d408
AH
4629 // For 0 = x & MASK, x is ~MASK.
4630 {
cb779afe
AH
4631 int_range<2> zero (integer_type_node, INT (0), INT (0));
4632 int_range<2> mask = int_range<2> (integer_type_node, INT (7), INT (7));
5e77d408
AH
4633 op_bitwise_and.op1_range (res, integer_type_node, zero, mask);
4634 wide_int inv = wi::shwi (~7U, TYPE_PRECISION (integer_type_node));
4635 ASSERT_TRUE (res.get_nonzero_bits () == inv);
4636 }
4637
46027143
AH
4638 // (NONZERO | X) is nonzero.
4639 i1.set_nonzero (integer_type_node);
4640 i2.set_varying (integer_type_node);
4641 op_bitwise_or.fold_range (res, integer_type_node, i1, i2);
4642 ASSERT_TRUE (res.nonzero_p ());
4643
4644 // (NEGATIVE | X) is nonzero.
cb779afe 4645 i1 = int_range<1> (integer_type_node, INT (-5), INT (-3));
46027143
AH
4646 i2.set_varying (integer_type_node);
4647 op_bitwise_or.fold_range (res, integer_type_node, i1, i2);
4648 ASSERT_FALSE (res.contains_p (INT (0)));
b5cff0db
AH
4649}
4650
ca1f9f22
AM
4651static void
4652range_relational_tests ()
4653{
4654 int_range<2> lhs (unsigned_char_type_node);
cb779afe
AH
4655 int_range<2> op1 (unsigned_char_type_node, UCHAR (8), UCHAR (10));
4656 int_range<2> op2 (unsigned_char_type_node, UCHAR (20), UCHAR (20));
ca1f9f22
AM
4657
4658 // Never wrapping additions mean LHS > OP1.
ade5531c
AM
4659 relation_kind code = op_plus.lhs_op1_relation (lhs, op1, op2, VREL_VARYING);
4660 ASSERT_TRUE (code == VREL_GT);
ca1f9f22
AM
4661
4662 // Most wrapping additions mean nothing...
cb779afe
AH
4663 op1 = int_range<2> (unsigned_char_type_node, UCHAR (8), UCHAR (10));
4664 op2 = int_range<2> (unsigned_char_type_node, UCHAR (0), UCHAR (255));
ade5531c
AM
4665 code = op_plus.lhs_op1_relation (lhs, op1, op2, VREL_VARYING);
4666 ASSERT_TRUE (code == VREL_VARYING);
ca1f9f22
AM
4667
4668 // However, always wrapping additions mean LHS < OP1.
cb779afe
AH
4669 op1 = int_range<2> (unsigned_char_type_node, UCHAR (1), UCHAR (255));
4670 op2 = int_range<2> (unsigned_char_type_node, UCHAR (255), UCHAR (255));
ade5531c
AM
4671 code = op_plus.lhs_op1_relation (lhs, op1, op2, VREL_VARYING);
4672 ASSERT_TRUE (code == VREL_LT);
ca1f9f22
AM
4673}
4674
b5cff0db
AH
4675void
4676range_op_tests ()
4677{
4678 range_op_rshift_tests ();
4679 range_op_lshift_tests ();
4680 range_op_bitwise_and_tests ();
4681 range_op_cast_tests ();
ca1f9f22 4682 range_relational_tests ();
1c0670c6
AH
4683
4684 extern void range_op_float_tests ();
4685 range_op_float_tests ();
38a73435 4686}
f1471317
AH
4687
4688} // namespace selftest
4689
38a73435 4690#endif // CHECKING_P