1 /* Code for range operators.
2 Copyright (C) 2017-2024 Free Software Foundation, Inc.
3 Contributed by Andrew MacLeod <amacleod@redhat.com>
4 and Aldy Hernandez <aldyh@redhat.com>.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
24 #include "coretypes.h"
26 #include "insn-codes.h"
31 #include "tree-pass.h"
33 #include "optabs-tree.h"
34 #include "gimple-pretty-print.h"
35 #include "diagnostic-core.h"
37 #include "fold-const.h"
38 #include "stor-layout.h"
41 #include "gimple-iterator.h"
42 #include "gimple-fold.h"
44 #include "gimple-walk.h"
47 #include "value-relation.h"
49 #include "tree-ssa-ccp.h"
50 #include "range-op-mixed.h"
52 // Instantiate the operators which apply to multiple types here.
54 operator_equal op_equal
;
55 operator_not_equal op_not_equal
;
60 operator_identity op_ident
;
62 operator_cast op_cast
;
63 operator_plus op_plus
;
65 operator_minus op_minus
;
66 operator_negate op_negate
;
67 operator_mult op_mult
;
68 operator_addr_expr op_addr
;
69 operator_bitwise_not op_bitwise_not
;
70 operator_bitwise_xor op_bitwise_xor
;
71 operator_bitwise_and op_bitwise_and
;
72 operator_bitwise_or op_bitwise_or
;
76 // Instantaite a range operator table.
77 range_op_table operator_table
;
79 // Invoke the initialization routines for each class of range.
81 range_op_table::range_op_table ()
83 initialize_integral_ops ();
84 initialize_pointer_ops ();
85 initialize_float_ops ();
87 set (EQ_EXPR
, op_equal
);
88 set (NE_EXPR
, op_not_equal
);
93 set (SSA_NAME
, op_ident
);
94 set (PAREN_EXPR
, op_ident
);
95 set (OBJ_TYPE_REF
, op_ident
);
96 set (REAL_CST
, op_cst
);
97 set (INTEGER_CST
, op_cst
);
98 set (NOP_EXPR
, op_cast
);
99 set (CONVERT_EXPR
, op_cast
);
100 set (PLUS_EXPR
, op_plus
);
101 set (ABS_EXPR
, op_abs
);
102 set (MINUS_EXPR
, op_minus
);
103 set (NEGATE_EXPR
, op_negate
);
104 set (MULT_EXPR
, op_mult
);
106 // Occur in both integer and pointer tables, but currently share
107 // integral implementation.
108 set (ADDR_EXPR
, op_addr
);
109 set (BIT_NOT_EXPR
, op_bitwise_not
);
110 set (BIT_XOR_EXPR
, op_bitwise_xor
);
112 // These are in both integer and pointer tables, but pointer has a different
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.
118 // set (BIT_AND_EXPR, op_bitwise_and);
119 // set (BIT_IOR_EXPR, op_bitwise_or);
120 // set (MIN_EXPR, op_min);
121 // set (MAX_EXPR, op_max);
124 // Instantiate a default range operator for opcodes with no entry.
126 range_operator default_operator
;
128 // Create a default range_op_handler.
130 range_op_handler::range_op_handler ()
132 m_operator
= &default_operator
;
135 // Create a range_op_handler for CODE. Use a default operatoer if CODE
136 // does not have an entry.
138 range_op_handler::range_op_handler (unsigned code
)
140 m_operator
= operator_table
[code
];
142 m_operator
= &default_operator
;
145 // Return TRUE if this handler has a non-default operator.
147 range_op_handler::operator bool () const
149 return m_operator
!= &default_operator
;
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.
157 range_op_handler::range_op () const
159 if (m_operator
!= &default_operator
)
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.
169 dispatch_trio (unsigned lhs
, unsigned op1
, unsigned op2
)
171 return ((lhs
<< 8) + (op1
<< 4) + (op2
));
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.
178 const unsigned RO_III
= dispatch_trio (VR_IRANGE
, VR_IRANGE
, VR_IRANGE
);
179 const unsigned RO_IFI
= dispatch_trio (VR_IRANGE
, VR_FRANGE
, VR_IRANGE
);
180 const unsigned RO_IFF
= dispatch_trio (VR_IRANGE
, VR_FRANGE
, VR_FRANGE
);
181 const unsigned RO_FFF
= dispatch_trio (VR_FRANGE
, VR_FRANGE
, VR_FRANGE
);
182 const unsigned RO_FIF
= dispatch_trio (VR_FRANGE
, VR_IRANGE
, VR_FRANGE
);
183 const unsigned RO_FII
= dispatch_trio (VR_FRANGE
, VR_IRANGE
, VR_IRANGE
);
185 // Return a dispatch value for parameter types LHS, OP1 and OP2.
188 range_op_handler::dispatch_kind (const vrange
&lhs
, const vrange
&op1
,
189 const vrange
& op2
) const
191 return dispatch_trio (lhs
.m_discriminator
, op1
.m_discriminator
,
192 op2
.m_discriminator
);
195 // Dispatch a call to fold_range based on the types of R, LH and RH.
198 range_op_handler::fold_range (vrange
&r
, tree type
,
201 relation_trio rel
) const
203 gcc_checking_assert (m_operator
);
205 if (!lh
.undefined_p () && !rh
.undefined_p ())
206 gcc_assert (m_operator
->operand_check_p (type
, lh
.type (), rh
.type ()));
208 switch (dispatch_kind (r
, lh
, rh
))
211 return m_operator
->fold_range (as_a
<irange
> (r
), type
,
213 as_a
<irange
> (rh
), rel
);
215 return m_operator
->fold_range (as_a
<irange
> (r
), type
,
217 as_a
<irange
> (rh
), rel
);
219 return m_operator
->fold_range (as_a
<irange
> (r
), type
,
221 as_a
<frange
> (rh
), rel
);
223 return m_operator
->fold_range (as_a
<frange
> (r
), type
,
225 as_a
<frange
> (rh
), rel
);
227 return m_operator
->fold_range (as_a
<frange
> (r
), type
,
229 as_a
<irange
> (rh
), rel
);
235 // Dispatch a call to op1_range based on the types of R, LHS and OP2.
238 range_op_handler::op1_range (vrange
&r
, tree type
,
241 relation_trio rel
) const
243 gcc_checking_assert (m_operator
);
244 if (lhs
.undefined_p ())
247 if (!op2
.undefined_p ())
248 gcc_assert (m_operator
->operand_check_p (lhs
.type (), type
, op2
.type ()));
250 switch (dispatch_kind (r
, lhs
, op2
))
253 return m_operator
->op1_range (as_a
<irange
> (r
), type
,
255 as_a
<irange
> (op2
), rel
);
257 return m_operator
->op1_range (as_a
<frange
> (r
), type
,
259 as_a
<frange
> (op2
), rel
);
261 return m_operator
->op1_range (as_a
<frange
> (r
), type
,
263 as_a
<frange
> (op2
), rel
);
269 // Dispatch a call to op2_range based on the types of R, LHS and OP1.
272 range_op_handler::op2_range (vrange
&r
, tree type
,
275 relation_trio rel
) const
277 gcc_checking_assert (m_operator
);
278 if (lhs
.undefined_p ())
281 if (!op1
.undefined_p ())
282 gcc_assert (m_operator
->operand_check_p (lhs
.type (), op1
.type (), type
));
284 switch (dispatch_kind (r
, lhs
, op1
))
287 return m_operator
->op2_range (as_a
<irange
> (r
), type
,
289 as_a
<irange
> (op1
), rel
);
291 return m_operator
->op2_range (as_a
<frange
> (r
), type
,
293 as_a
<frange
> (op1
), rel
);
295 return m_operator
->op2_range (as_a
<frange
> (r
), type
,
297 as_a
<frange
> (op1
), rel
);
303 // Dispatch a call to lhs_op1_relation based on the types of LHS, OP1 and OP2.
306 range_op_handler::lhs_op1_relation (const vrange
&lhs
,
309 relation_kind rel
) const
311 gcc_checking_assert (m_operator
);
313 switch (dispatch_kind (lhs
, op1
, op2
))
316 return m_operator
->lhs_op1_relation (as_a
<irange
> (lhs
),
318 as_a
<irange
> (op2
), rel
);
320 return m_operator
->lhs_op1_relation (as_a
<irange
> (lhs
),
322 as_a
<frange
> (op2
), rel
);
324 return m_operator
->lhs_op1_relation (as_a
<frange
> (lhs
),
326 as_a
<frange
> (op2
), rel
);
332 // Dispatch a call to lhs_op2_relation based on the types of LHS, OP1 and OP2.
335 range_op_handler::lhs_op2_relation (const vrange
&lhs
,
338 relation_kind rel
) const
340 gcc_checking_assert (m_operator
);
341 switch (dispatch_kind (lhs
, op1
, op2
))
344 return m_operator
->lhs_op2_relation (as_a
<irange
> (lhs
),
346 as_a
<irange
> (op2
), rel
);
348 return m_operator
->lhs_op2_relation (as_a
<irange
> (lhs
),
350 as_a
<frange
> (op2
), rel
);
352 return m_operator
->lhs_op2_relation (as_a
<frange
> (lhs
),
354 as_a
<frange
> (op2
), rel
);
360 // Dispatch a call to op1_op2_relation based on the type of LHS.
363 range_op_handler::op1_op2_relation (const vrange
&lhs
,
365 const vrange
&op2
) const
367 gcc_checking_assert (m_operator
);
368 switch (dispatch_kind (lhs
, op1
, op2
))
371 return m_operator
->op1_op2_relation (as_a
<irange
> (lhs
),
373 as_a
<irange
> (op2
));
376 return m_operator
->op1_op2_relation (as_a
<irange
> (lhs
),
378 as_a
<frange
> (op2
));
381 return m_operator
->op1_op2_relation (as_a
<frange
> (lhs
),
383 as_a
<frange
> (op2
));
391 range_op_handler::overflow_free_p (const vrange
&lh
,
393 relation_trio rel
) const
395 gcc_checking_assert (m_operator
);
396 switch (dispatch_kind (lh
, lh
, rh
))
399 return m_operator
->overflow_free_p(as_a
<irange
> (lh
),
408 range_op_handler::operand_check_p (tree t1
, tree t2
, tree t3
) const
410 gcc_checking_assert (m_operator
);
411 return m_operator
->operand_check_p (t1
, t2
, t3
);
414 // Update the known bitmasks in R when applying the operation CODE to
418 update_known_bitmask (irange
&r
, tree_code code
,
419 const irange
&lh
, const irange
&rh
)
421 if (r
.undefined_p () || lh
.undefined_p () || rh
.undefined_p ()
425 widest_int widest_value
, widest_mask
;
426 tree type
= r
.type ();
427 signop sign
= TYPE_SIGN (type
);
428 int prec
= TYPE_PRECISION (type
);
429 irange_bitmask lh_bits
= lh
.get_bitmask ();
430 irange_bitmask rh_bits
= rh
.get_bitmask ();
432 switch (get_gimple_rhs_class (code
))
434 case GIMPLE_UNARY_RHS
:
435 bit_value_unop (code
, sign
, prec
, &widest_value
, &widest_mask
,
436 TYPE_SIGN (lh
.type ()),
437 TYPE_PRECISION (lh
.type ()),
438 widest_int::from (lh_bits
.value (), sign
),
439 widest_int::from (lh_bits
.mask (), sign
));
441 case GIMPLE_BINARY_RHS
:
442 bit_value_binop (code
, sign
, prec
, &widest_value
, &widest_mask
,
443 TYPE_SIGN (lh
.type ()),
444 TYPE_PRECISION (lh
.type ()),
445 widest_int::from (lh_bits
.value (), sign
),
446 widest_int::from (lh_bits
.mask (), sign
),
447 TYPE_SIGN (rh
.type ()),
448 TYPE_PRECISION (rh
.type ()),
449 widest_int::from (rh_bits
.value (), sign
),
450 widest_int::from (rh_bits
.mask (), sign
));
456 wide_int mask
= wide_int::from (widest_mask
, prec
, sign
);
457 wide_int value
= wide_int::from (widest_value
, prec
, sign
);
458 // Bitmasks must have the unknown value bits cleared.
460 irange_bitmask
bm (value
, mask
);
461 r
.update_bitmask (bm
);
464 // Return the upper limit for a type.
466 static inline wide_int
467 max_limit (const_tree type
)
469 return irange_val_max (type
);
472 // Return the lower limit for a type.
474 static inline wide_int
475 min_limit (const_tree type
)
477 return irange_val_min (type
);
480 // Return false if shifting by OP is undefined behavior. Otherwise, return
481 // true and the range it is to be shifted by. This allows trimming out of
482 // undefined ranges, leaving only valid ranges if there are any.
485 get_shift_range (irange
&r
, tree type
, const irange
&op
)
487 if (op
.undefined_p ())
490 // Build valid range and intersect it with the shift range.
491 r
= value_range (op
.type (),
492 wi::shwi (0, TYPE_PRECISION (op
.type ())),
493 wi::shwi (TYPE_PRECISION (type
) - 1, TYPE_PRECISION (op
.type ())));
496 // If there are no valid ranges in the shift range, returned false.
497 if (r
.undefined_p ())
502 // Default wide_int fold operation returns [MIN, MAX].
505 range_operator::wi_fold (irange
&r
, tree type
,
506 const wide_int
&lh_lb ATTRIBUTE_UNUSED
,
507 const wide_int
&lh_ub ATTRIBUTE_UNUSED
,
508 const wide_int
&rh_lb ATTRIBUTE_UNUSED
,
509 const wide_int
&rh_ub ATTRIBUTE_UNUSED
) const
511 gcc_checking_assert (r
.supports_type_p (type
));
512 r
.set_varying (type
);
515 // Call wi_fold when both op1 and op2 are equivalent. Further split small
516 // subranges into constants. This can provide better precision.
517 // For x + y, when x == y with a range of [0,4] instead of [0, 8] produce
518 // [0,0][2, 2][4,4][6, 6][8, 8]
519 // LIMIT is the maximum number of elements in range allowed before we
520 // do not process them individually.
523 range_operator::wi_fold_in_parts_equiv (irange
&r
, tree type
,
524 const wide_int
&lh_lb
,
525 const wide_int
&lh_ub
,
526 unsigned limit
) const
529 widest_int lh_range
= wi::sub (widest_int::from (lh_ub
, TYPE_SIGN (type
)),
530 widest_int::from (lh_lb
, TYPE_SIGN (type
)));
531 // if there are 1 to 8 values in the LH range, split them up.
533 if (lh_range
>= 0 && lh_range
< limit
)
535 for (unsigned x
= 0; x
<= lh_range
; x
++)
537 wide_int val
= lh_lb
+ x
;
538 wi_fold (tmp
, type
, val
, val
, val
, val
);
542 // Otherwise just call wi_fold.
544 wi_fold (r
, type
, lh_lb
, lh_ub
, lh_lb
, lh_ub
);
547 // Call wi_fold, except further split small subranges into constants.
548 // This can provide better precision. For something 8 >> [0,1]
549 // Instead of [8, 16], we will produce [8,8][16,16]
552 range_operator::wi_fold_in_parts (irange
&r
, tree type
,
553 const wide_int
&lh_lb
,
554 const wide_int
&lh_ub
,
555 const wide_int
&rh_lb
,
556 const wide_int
&rh_ub
) const
559 widest_int rh_range
= wi::sub (widest_int::from (rh_ub
, TYPE_SIGN (type
)),
560 widest_int::from (rh_lb
, TYPE_SIGN (type
)));
561 widest_int lh_range
= wi::sub (widest_int::from (lh_ub
, TYPE_SIGN (type
)),
562 widest_int::from (lh_lb
, TYPE_SIGN (type
)));
563 // If there are 2, 3, or 4 values in the RH range, do them separately.
564 // Call wi_fold_in_parts to check the RH side.
565 if (rh_range
> 0 && rh_range
< 4)
567 wi_fold_in_parts (r
, type
, lh_lb
, lh_ub
, rh_lb
, rh_lb
);
570 wi_fold_in_parts (tmp
, type
, lh_lb
, lh_ub
, rh_lb
+ 1, rh_lb
+ 1);
574 wi_fold_in_parts (tmp
, type
, lh_lb
, lh_ub
, rh_lb
+ 2, rh_lb
+ 2);
578 wi_fold_in_parts (tmp
, type
, lh_lb
, lh_ub
, rh_ub
, rh_ub
);
581 // Otherwise check for 2, 3, or 4 values in the LH range and split them up.
582 // The RH side has been checked, so no recursion needed.
583 else if (lh_range
> 0 && lh_range
< 4)
585 wi_fold (r
, type
, lh_lb
, lh_lb
, rh_lb
, rh_ub
);
588 wi_fold (tmp
, type
, lh_lb
+ 1, lh_lb
+ 1, rh_lb
, rh_ub
);
592 wi_fold (tmp
, type
, lh_lb
+ 2, lh_lb
+ 2, rh_lb
, rh_ub
);
596 wi_fold (tmp
, type
, lh_ub
, lh_ub
, rh_lb
, rh_ub
);
599 // Otherwise just call wi_fold.
601 wi_fold (r
, type
, lh_lb
, lh_ub
, rh_lb
, rh_ub
);
604 // The default for fold is to break all ranges into sub-ranges and
605 // invoke the wi_fold method on each sub-range pair.
608 range_operator::fold_range (irange
&r
, tree type
,
611 relation_trio trio
) const
613 gcc_checking_assert (r
.supports_type_p (type
));
614 if (empty_range_varying (r
, type
, lh
, rh
))
617 relation_kind rel
= trio
.op1_op2 ();
618 unsigned num_lh
= lh
.num_pairs ();
619 unsigned num_rh
= rh
.num_pairs ();
621 // If op1 and op2 are equivalences, then we don't need a complete cross
622 // product, just pairs of matching elements.
623 if (relation_equiv_p (rel
) && lh
== rh
)
627 for (unsigned x
= 0; x
< num_lh
; ++x
)
629 // If the number of subranges is too high, limit subrange creation.
630 unsigned limit
= (r
.num_pairs () > 32) ? 0 : 8;
631 wide_int lh_lb
= lh
.lower_bound (x
);
632 wide_int lh_ub
= lh
.upper_bound (x
);
633 wi_fold_in_parts_equiv (tmp
, type
, lh_lb
, lh_ub
, limit
);
638 op1_op2_relation_effect (r
, type
, lh
, rh
, rel
);
639 update_bitmask (r
, lh
, rh
);
643 // If both ranges are single pairs, fold directly into the result range.
644 // If the number of subranges grows too high, produce a summary result as the
645 // loop becomes exponential with little benefit. See PR 103821.
646 if ((num_lh
== 1 && num_rh
== 1) || num_lh
* num_rh
> 12)
648 wi_fold_in_parts (r
, type
, lh
.lower_bound (), lh
.upper_bound (),
649 rh
.lower_bound (), rh
.upper_bound ());
650 op1_op2_relation_effect (r
, type
, lh
, rh
, rel
);
651 update_bitmask (r
, lh
, rh
);
657 for (unsigned x
= 0; x
< num_lh
; ++x
)
658 for (unsigned y
= 0; y
< num_rh
; ++y
)
660 wide_int lh_lb
= lh
.lower_bound (x
);
661 wide_int lh_ub
= lh
.upper_bound (x
);
662 wide_int rh_lb
= rh
.lower_bound (y
);
663 wide_int rh_ub
= rh
.upper_bound (y
);
664 wi_fold_in_parts (tmp
, type
, lh_lb
, lh_ub
, rh_lb
, rh_ub
);
668 op1_op2_relation_effect (r
, type
, lh
, rh
, rel
);
669 update_bitmask (r
, lh
, rh
);
673 op1_op2_relation_effect (r
, type
, lh
, rh
, rel
);
674 update_bitmask (r
, lh
, rh
);
678 // The default for op1_range is to return false.
681 range_operator::op1_range (irange
&r ATTRIBUTE_UNUSED
,
682 tree type ATTRIBUTE_UNUSED
,
683 const irange
&lhs ATTRIBUTE_UNUSED
,
684 const irange
&op2 ATTRIBUTE_UNUSED
,
690 // The default for op2_range is to return false.
693 range_operator::op2_range (irange
&r ATTRIBUTE_UNUSED
,
694 tree type ATTRIBUTE_UNUSED
,
695 const irange
&lhs ATTRIBUTE_UNUSED
,
696 const irange
&op1 ATTRIBUTE_UNUSED
,
702 // The default relation routines return VREL_VARYING.
705 range_operator::lhs_op1_relation (const irange
&lhs ATTRIBUTE_UNUSED
,
706 const irange
&op1 ATTRIBUTE_UNUSED
,
707 const irange
&op2 ATTRIBUTE_UNUSED
,
708 relation_kind rel ATTRIBUTE_UNUSED
) const
714 range_operator::lhs_op2_relation (const irange
&lhs ATTRIBUTE_UNUSED
,
715 const irange
&op1 ATTRIBUTE_UNUSED
,
716 const irange
&op2 ATTRIBUTE_UNUSED
,
717 relation_kind rel ATTRIBUTE_UNUSED
) const
723 range_operator::op1_op2_relation (const irange
&lhs ATTRIBUTE_UNUSED
,
724 const irange
&op1 ATTRIBUTE_UNUSED
,
725 const irange
&op2 ATTRIBUTE_UNUSED
) const
730 // Default is no relation affects the LHS.
733 range_operator::op1_op2_relation_effect (irange
&lhs_range ATTRIBUTE_UNUSED
,
734 tree type ATTRIBUTE_UNUSED
,
735 const irange
&op1_range ATTRIBUTE_UNUSED
,
736 const irange
&op2_range ATTRIBUTE_UNUSED
,
737 relation_kind rel ATTRIBUTE_UNUSED
) const
743 range_operator::overflow_free_p (const irange
&, const irange
&,
749 // Apply any known bitmask updates based on this operator.
752 range_operator::update_bitmask (irange
&, const irange
&,
753 const irange
&) const
757 // Check that operand types are OK. Default to always OK.
760 range_operator::operand_check_p (tree
, tree
, tree
) const
765 // Create and return a range from a pair of wide-ints that are known
766 // to have overflowed (or underflowed).
769 value_range_from_overflowed_bounds (irange
&r
, tree type
,
770 const wide_int
&wmin
,
771 const wide_int
&wmax
)
773 const signop sgn
= TYPE_SIGN (type
);
774 const unsigned int prec
= TYPE_PRECISION (type
);
776 wide_int tmin
= wide_int::from (wmin
, prec
, sgn
);
777 wide_int tmax
= wide_int::from (wmax
, prec
, sgn
);
782 if (wi::cmp (tmin
, tmax
, sgn
) < 0)
785 if (wi::cmp (tmax
, tem
, sgn
) > 0)
788 // If the anti-range would cover nothing, drop to varying.
789 // Likewise if the anti-range bounds are outside of the types
791 if (covers
|| wi::cmp (tmin
, tmax
, sgn
) > 0)
792 r
.set_varying (type
);
794 r
.set (type
, tmin
, tmax
, VR_ANTI_RANGE
);
797 // Create and return a range from a pair of wide-ints. MIN_OVF and
798 // MAX_OVF describe any overflow that might have occurred while
799 // calculating WMIN and WMAX respectively.
802 value_range_with_overflow (irange
&r
, tree type
,
803 const wide_int
&wmin
, const wide_int
&wmax
,
804 wi::overflow_type min_ovf
= wi::OVF_NONE
,
805 wi::overflow_type max_ovf
= wi::OVF_NONE
)
807 const signop sgn
= TYPE_SIGN (type
);
808 const unsigned int prec
= TYPE_PRECISION (type
);
809 const bool overflow_wraps
= TYPE_OVERFLOW_WRAPS (type
);
811 // For one bit precision if max != min, then the range covers all
813 if (prec
== 1 && wi::ne_p (wmax
, wmin
))
815 r
.set_varying (type
);
821 // If overflow wraps, truncate the values and adjust the range,
822 // kind, and bounds appropriately.
823 if ((min_ovf
!= wi::OVF_NONE
) == (max_ovf
!= wi::OVF_NONE
))
825 wide_int tmin
= wide_int::from (wmin
, prec
, sgn
);
826 wide_int tmax
= wide_int::from (wmax
, prec
, sgn
);
827 // If the limits are swapped, we wrapped around and cover
829 if (wi::gt_p (tmin
, tmax
, sgn
))
830 r
.set_varying (type
);
832 // No overflow or both overflow or underflow. The range
833 // kind stays normal.
834 r
.set (type
, tmin
, tmax
);
838 if ((min_ovf
== wi::OVF_UNDERFLOW
&& max_ovf
== wi::OVF_NONE
)
839 || (max_ovf
== wi::OVF_OVERFLOW
&& min_ovf
== wi::OVF_NONE
))
840 value_range_from_overflowed_bounds (r
, type
, wmin
, wmax
);
842 // Other underflow and/or overflow, drop to VR_VARYING.
843 r
.set_varying (type
);
847 // If both bounds either underflowed or overflowed, then the result
849 if ((min_ovf
== wi::OVF_OVERFLOW
&& max_ovf
== wi::OVF_OVERFLOW
)
850 || (min_ovf
== wi::OVF_UNDERFLOW
&& max_ovf
== wi::OVF_UNDERFLOW
))
856 // If overflow does not wrap, saturate to [MIN, MAX].
857 wide_int new_lb
, new_ub
;
858 if (min_ovf
== wi::OVF_UNDERFLOW
)
859 new_lb
= wi::min_value (prec
, sgn
);
860 else if (min_ovf
== wi::OVF_OVERFLOW
)
861 new_lb
= wi::max_value (prec
, sgn
);
865 if (max_ovf
== wi::OVF_UNDERFLOW
)
866 new_ub
= wi::min_value (prec
, sgn
);
867 else if (max_ovf
== wi::OVF_OVERFLOW
)
868 new_ub
= wi::max_value (prec
, sgn
);
872 r
.set (type
, new_lb
, new_ub
);
876 // Create and return a range from a pair of wide-ints. Canonicalize
877 // the case where the bounds are swapped. In which case, we transform
878 // [10,5] into [MIN,5][10,MAX].
881 create_possibly_reversed_range (irange
&r
, tree type
,
882 const wide_int
&new_lb
, const wide_int
&new_ub
)
884 signop s
= TYPE_SIGN (type
);
885 // If the bounds are swapped, treat the result as if an overflow occurred.
886 if (wi::gt_p (new_lb
, new_ub
, s
))
887 value_range_from_overflowed_bounds (r
, type
, new_lb
, new_ub
);
889 // Otherwise it's just a normal range.
890 r
.set (type
, new_lb
, new_ub
);
893 // Return the summary information about boolean range LHS. If EMPTY/FULL,
894 // return the equivalent range for TYPE in R; if FALSE/TRUE, do nothing.
897 get_bool_state (vrange
&r
, const vrange
&lhs
, tree val_type
)
899 // If there is no result, then this is unexecutable.
900 if (lhs
.undefined_p ())
909 // For TRUE, we can't just test for [1,1] because Ada can have
910 // multi-bit booleans, and TRUE values can be: [1, MAX], ~[0], etc.
911 if (lhs
.contains_p (build_zero_cst (lhs
.type ())))
913 r
.set_varying (val_type
);
920 // ------------------------------------------------------------------------
923 operator_equal::update_bitmask (irange
&r
, const irange
&lh
,
924 const irange
&rh
) const
926 update_known_bitmask (r
, EQ_EXPR
, lh
, rh
);
929 // Check if the LHS range indicates a relation between OP1 and OP2.
932 operator_equal::op1_op2_relation (const irange
&lhs
, const irange
&,
933 const irange
&) const
935 if (lhs
.undefined_p ())
936 return VREL_UNDEFINED
;
938 // FALSE = op1 == op2 indicates NE_EXPR.
942 // TRUE = op1 == op2 indicates EQ_EXPR.
943 if (!contains_zero_p (lhs
))
949 operator_equal::fold_range (irange
&r
, tree type
,
952 relation_trio rel
) const
954 if (relop_early_resolve (r
, type
, op1
, op2
, rel
, VREL_EQ
))
957 // We can be sure the values are always equal or not if both ranges
958 // consist of a single value, and then compare them.
959 bool op1_const
= wi::eq_p (op1
.lower_bound (), op1
.upper_bound ());
960 bool op2_const
= wi::eq_p (op2
.lower_bound (), op2
.upper_bound ());
961 if (op1_const
&& op2_const
)
963 if (wi::eq_p (op1
.lower_bound (), op2
.upper_bound()))
964 r
= range_true (type
);
966 r
= range_false (type
);
970 // If ranges do not intersect, we know the range is not equal,
971 // otherwise we don't know anything for sure.
972 int_range_max tmp
= op1
;
974 if (tmp
.undefined_p ())
975 r
= range_false (type
);
976 // Check if a constant cannot satisfy the bitmask requirements.
977 else if (op2_const
&& !op1
.get_bitmask ().member_p (op2
.lower_bound ()))
978 r
= range_false (type
);
979 else if (op1_const
&& !op2
.get_bitmask ().member_p (op1
.lower_bound ()))
980 r
= range_false (type
);
982 r
= range_true_and_false (type
);
988 operator_equal::op1_range (irange
&r
, tree type
,
993 switch (get_bool_state (r
, lhs
, type
))
996 // If it's true, the result is the same as OP2.
1001 // If the result is false, the only time we know anything is
1002 // if OP2 is a constant.
1003 if (!op2
.undefined_p ()
1004 && wi::eq_p (op2
.lower_bound(), op2
.upper_bound()))
1010 r
.set_varying (type
);
1020 operator_equal::op2_range (irange
&r
, tree type
,
1023 relation_trio rel
) const
1025 return operator_equal::op1_range (r
, type
, lhs
, op1
, rel
.swap_op1_op2 ());
1028 // -------------------------------------------------------------------------
1031 operator_not_equal::update_bitmask (irange
&r
, const irange
&lh
,
1032 const irange
&rh
) const
1034 update_known_bitmask (r
, NE_EXPR
, lh
, rh
);
1037 // Check if the LHS range indicates a relation between OP1 and OP2.
1040 operator_not_equal::op1_op2_relation (const irange
&lhs
, const irange
&,
1041 const irange
&) const
1043 if (lhs
.undefined_p ())
1044 return VREL_UNDEFINED
;
1046 // FALSE = op1 != op2 indicates EQ_EXPR.
1050 // TRUE = op1 != op2 indicates NE_EXPR.
1051 if (!contains_zero_p (lhs
))
1053 return VREL_VARYING
;
1057 operator_not_equal::fold_range (irange
&r
, tree type
,
1060 relation_trio rel
) const
1062 if (relop_early_resolve (r
, type
, op1
, op2
, rel
, VREL_NE
))
1065 // We can be sure the values are always equal or not if both ranges
1066 // consist of a single value, and then compare them.
1067 bool op1_const
= wi::eq_p (op1
.lower_bound (), op1
.upper_bound ());
1068 bool op2_const
= wi::eq_p (op2
.lower_bound (), op2
.upper_bound ());
1069 if (op1_const
&& op2_const
)
1071 if (wi::ne_p (op1
.lower_bound (), op2
.upper_bound()))
1072 r
= range_true (type
);
1074 r
= range_false (type
);
1078 // If ranges do not intersect, we know the range is not equal,
1079 // otherwise we don't know anything for sure.
1080 int_range_max tmp
= op1
;
1081 tmp
.intersect (op2
);
1082 if (tmp
.undefined_p ())
1083 r
= range_true (type
);
1084 // Check if a constant cannot satisfy the bitmask requirements.
1085 else if (op2_const
&& !op1
.get_bitmask ().member_p (op2
.lower_bound ()))
1086 r
= range_true (type
);
1087 else if (op1_const
&& !op2
.get_bitmask ().member_p (op1
.lower_bound ()))
1088 r
= range_true (type
);
1090 r
= range_true_and_false (type
);
1096 operator_not_equal::op1_range (irange
&r
, tree type
,
1099 relation_trio
) const
1101 switch (get_bool_state (r
, lhs
, type
))
1104 // If the result is true, the only time we know anything is if
1105 // OP2 is a constant.
1106 if (!op2
.undefined_p ()
1107 && wi::eq_p (op2
.lower_bound(), op2
.upper_bound()))
1113 r
.set_varying (type
);
1117 // If it's false, the result is the same as OP2.
1129 operator_not_equal::op2_range (irange
&r
, tree type
,
1132 relation_trio rel
) const
1134 return operator_not_equal::op1_range (r
, type
, lhs
, op1
, rel
.swap_op1_op2 ());
1137 // (X < VAL) produces the range of [MIN, VAL - 1].
1140 build_lt (irange
&r
, tree type
, const wide_int
&val
)
1142 wi::overflow_type ov
;
1144 signop sgn
= TYPE_SIGN (type
);
1146 // Signed 1 bit cannot represent 1 for subtraction.
1148 lim
= wi::add (val
, -1, sgn
, &ov
);
1150 lim
= wi::sub (val
, 1, sgn
, &ov
);
1152 // If val - 1 underflows, check if X < MIN, which is an empty range.
1156 r
= int_range
<1> (type
, min_limit (type
), lim
);
1159 // (X <= VAL) produces the range of [MIN, VAL].
1162 build_le (irange
&r
, tree type
, const wide_int
&val
)
1164 r
= int_range
<1> (type
, min_limit (type
), val
);
1167 // (X > VAL) produces the range of [VAL + 1, MAX].
1170 build_gt (irange
&r
, tree type
, const wide_int
&val
)
1172 wi::overflow_type ov
;
1174 signop sgn
= TYPE_SIGN (type
);
1176 // Signed 1 bit cannot represent 1 for addition.
1178 lim
= wi::sub (val
, -1, sgn
, &ov
);
1180 lim
= wi::add (val
, 1, sgn
, &ov
);
1181 // If val + 1 overflows, check is for X > MAX, which is an empty range.
1185 r
= int_range
<1> (type
, lim
, max_limit (type
));
1188 // (X >= val) produces the range of [VAL, MAX].
1191 build_ge (irange
&r
, tree type
, const wide_int
&val
)
1193 r
= int_range
<1> (type
, val
, max_limit (type
));
1198 operator_lt::update_bitmask (irange
&r
, const irange
&lh
,
1199 const irange
&rh
) const
1201 update_known_bitmask (r
, LT_EXPR
, lh
, rh
);
1204 // Check if the LHS range indicates a relation between OP1 and OP2.
1207 operator_lt::op1_op2_relation (const irange
&lhs
, const irange
&,
1208 const irange
&) const
1210 if (lhs
.undefined_p ())
1211 return VREL_UNDEFINED
;
1213 // FALSE = op1 < op2 indicates GE_EXPR.
1217 // TRUE = op1 < op2 indicates LT_EXPR.
1218 if (!contains_zero_p (lhs
))
1220 return VREL_VARYING
;
1224 operator_lt::fold_range (irange
&r
, tree type
,
1227 relation_trio rel
) const
1229 if (relop_early_resolve (r
, type
, op1
, op2
, rel
, VREL_LT
))
1232 signop sign
= TYPE_SIGN (op1
.type ());
1233 gcc_checking_assert (sign
== TYPE_SIGN (op2
.type ()));
1235 if (wi::lt_p (op1
.upper_bound (), op2
.lower_bound (), sign
))
1236 r
= range_true (type
);
1237 else if (!wi::lt_p (op1
.lower_bound (), op2
.upper_bound (), sign
))
1238 r
= range_false (type
);
1239 // Use nonzero bits to determine if < 0 is false.
1240 else if (op2
.zero_p () && !wi::neg_p (op1
.get_nonzero_bits (), sign
))
1241 r
= range_false (type
);
1243 r
= range_true_and_false (type
);
1248 operator_lt::op1_range (irange
&r
, tree type
,
1251 relation_trio
) const
1253 if (op2
.undefined_p ())
1256 switch (get_bool_state (r
, lhs
, type
))
1259 build_lt (r
, type
, op2
.upper_bound ());
1263 build_ge (r
, type
, op2
.lower_bound ());
1273 operator_lt::op2_range (irange
&r
, tree type
,
1276 relation_trio
) const
1278 if (op1
.undefined_p ())
1281 switch (get_bool_state (r
, lhs
, type
))
1284 build_gt (r
, type
, op1
.lower_bound ());
1288 build_le (r
, type
, op1
.upper_bound ());
1299 operator_le::update_bitmask (irange
&r
, const irange
&lh
,
1300 const irange
&rh
) const
1302 update_known_bitmask (r
, LE_EXPR
, lh
, rh
);
1305 // Check if the LHS range indicates a relation between OP1 and OP2.
1308 operator_le::op1_op2_relation (const irange
&lhs
, const irange
&,
1309 const irange
&) const
1311 if (lhs
.undefined_p ())
1312 return VREL_UNDEFINED
;
1314 // FALSE = op1 <= op2 indicates GT_EXPR.
1318 // TRUE = op1 <= op2 indicates LE_EXPR.
1319 if (!contains_zero_p (lhs
))
1321 return VREL_VARYING
;
1325 operator_le::fold_range (irange
&r
, tree type
,
1328 relation_trio rel
) const
1330 if (relop_early_resolve (r
, type
, op1
, op2
, rel
, VREL_LE
))
1333 signop sign
= TYPE_SIGN (op1
.type ());
1334 gcc_checking_assert (sign
== TYPE_SIGN (op2
.type ()));
1336 if (wi::le_p (op1
.upper_bound (), op2
.lower_bound (), sign
))
1337 r
= range_true (type
);
1338 else if (!wi::le_p (op1
.lower_bound (), op2
.upper_bound (), sign
))
1339 r
= range_false (type
);
1341 r
= range_true_and_false (type
);
1346 operator_le::op1_range (irange
&r
, tree type
,
1349 relation_trio
) const
1351 if (op2
.undefined_p ())
1354 switch (get_bool_state (r
, lhs
, type
))
1357 build_le (r
, type
, op2
.upper_bound ());
1361 build_gt (r
, type
, op2
.lower_bound ());
1371 operator_le::op2_range (irange
&r
, tree type
,
1374 relation_trio
) const
1376 if (op1
.undefined_p ())
1379 switch (get_bool_state (r
, lhs
, type
))
1382 build_ge (r
, type
, op1
.lower_bound ());
1386 build_lt (r
, type
, op1
.upper_bound ());
1397 operator_gt::update_bitmask (irange
&r
, const irange
&lh
,
1398 const irange
&rh
) const
1400 update_known_bitmask (r
, GT_EXPR
, lh
, rh
);
1403 // Check if the LHS range indicates a relation between OP1 and OP2.
1406 operator_gt::op1_op2_relation (const irange
&lhs
, const irange
&,
1407 const irange
&) const
1409 if (lhs
.undefined_p ())
1410 return VREL_UNDEFINED
;
1412 // FALSE = op1 > op2 indicates LE_EXPR.
1416 // TRUE = op1 > op2 indicates GT_EXPR.
1417 if (!contains_zero_p (lhs
))
1419 return VREL_VARYING
;
1423 operator_gt::fold_range (irange
&r
, tree type
,
1424 const irange
&op1
, const irange
&op2
,
1425 relation_trio rel
) const
1427 if (relop_early_resolve (r
, type
, op1
, op2
, rel
, VREL_GT
))
1430 signop sign
= TYPE_SIGN (op1
.type ());
1431 gcc_checking_assert (sign
== TYPE_SIGN (op2
.type ()));
1433 if (wi::gt_p (op1
.lower_bound (), op2
.upper_bound (), sign
))
1434 r
= range_true (type
);
1435 else if (!wi::gt_p (op1
.upper_bound (), op2
.lower_bound (), sign
))
1436 r
= range_false (type
);
1438 r
= range_true_and_false (type
);
1443 operator_gt::op1_range (irange
&r
, tree type
,
1444 const irange
&lhs
, const irange
&op2
,
1445 relation_trio
) const
1447 if (op2
.undefined_p ())
1450 switch (get_bool_state (r
, lhs
, type
))
1453 build_gt (r
, type
, op2
.lower_bound ());
1457 build_le (r
, type
, op2
.upper_bound ());
1467 operator_gt::op2_range (irange
&r
, tree type
,
1470 relation_trio
) const
1472 if (op1
.undefined_p ())
1475 switch (get_bool_state (r
, lhs
, type
))
1478 build_lt (r
, type
, op1
.upper_bound ());
1482 build_ge (r
, type
, op1
.lower_bound ());
1493 operator_ge::update_bitmask (irange
&r
, const irange
&lh
,
1494 const irange
&rh
) const
1496 update_known_bitmask (r
, GE_EXPR
, lh
, rh
);
1499 // Check if the LHS range indicates a relation between OP1 and OP2.
1502 operator_ge::op1_op2_relation (const irange
&lhs
, const irange
&,
1503 const irange
&) const
1505 if (lhs
.undefined_p ())
1506 return VREL_UNDEFINED
;
1508 // FALSE = op1 >= op2 indicates LT_EXPR.
1512 // TRUE = op1 >= op2 indicates GE_EXPR.
1513 if (!contains_zero_p (lhs
))
1515 return VREL_VARYING
;
1519 operator_ge::fold_range (irange
&r
, tree type
,
1522 relation_trio rel
) const
1524 if (relop_early_resolve (r
, type
, op1
, op2
, rel
, VREL_GE
))
1527 signop sign
= TYPE_SIGN (op1
.type ());
1528 gcc_checking_assert (sign
== TYPE_SIGN (op2
.type ()));
1530 if (wi::ge_p (op1
.lower_bound (), op2
.upper_bound (), sign
))
1531 r
= range_true (type
);
1532 else if (!wi::ge_p (op1
.upper_bound (), op2
.lower_bound (), sign
))
1533 r
= range_false (type
);
1535 r
= range_true_and_false (type
);
1540 operator_ge::op1_range (irange
&r
, tree type
,
1543 relation_trio
) const
1545 if (op2
.undefined_p ())
1548 switch (get_bool_state (r
, lhs
, type
))
1551 build_ge (r
, type
, op2
.lower_bound ());
1555 build_lt (r
, type
, op2
.upper_bound ());
1565 operator_ge::op2_range (irange
&r
, tree type
,
1568 relation_trio
) const
1570 if (op1
.undefined_p ())
1573 switch (get_bool_state (r
, lhs
, type
))
1576 build_le (r
, type
, op1
.upper_bound ());
1580 build_gt (r
, type
, op1
.lower_bound ());
1591 operator_plus::update_bitmask (irange
&r
, const irange
&lh
,
1592 const irange
&rh
) const
1594 update_known_bitmask (r
, PLUS_EXPR
, lh
, rh
);
1597 // Check to see if the range of OP2 indicates anything about the relation
1598 // between LHS and OP1.
1601 operator_plus::lhs_op1_relation (const irange
&lhs
,
1604 relation_kind
) const
1606 if (lhs
.undefined_p () || op1
.undefined_p () || op2
.undefined_p ())
1607 return VREL_VARYING
;
1609 tree type
= lhs
.type ();
1610 unsigned prec
= TYPE_PRECISION (type
);
1611 wi::overflow_type ovf1
, ovf2
;
1612 signop sign
= TYPE_SIGN (type
);
1614 // LHS = OP1 + 0 indicates LHS == OP1.
1618 if (TYPE_OVERFLOW_WRAPS (type
))
1620 wi::add (op1
.lower_bound (), op2
.lower_bound (), sign
, &ovf1
);
1621 wi::add (op1
.upper_bound (), op2
.upper_bound (), sign
, &ovf2
);
1624 ovf1
= ovf2
= wi::OVF_NONE
;
1626 // Never wrapping additions.
1629 // Positive op2 means lhs > op1.
1630 if (wi::gt_p (op2
.lower_bound (), wi::zero (prec
), sign
))
1632 if (wi::ge_p (op2
.lower_bound (), wi::zero (prec
), sign
))
1635 // Negative op2 means lhs < op1.
1636 if (wi::lt_p (op2
.upper_bound (), wi::zero (prec
), sign
))
1638 if (wi::le_p (op2
.upper_bound (), wi::zero (prec
), sign
))
1641 // Always wrapping additions.
1642 else if (ovf1
&& ovf1
== ovf2
)
1644 // Positive op2 means lhs < op1.
1645 if (wi::gt_p (op2
.lower_bound (), wi::zero (prec
), sign
))
1647 if (wi::ge_p (op2
.lower_bound (), wi::zero (prec
), sign
))
1650 // Negative op2 means lhs > op1.
1651 if (wi::lt_p (op2
.upper_bound (), wi::zero (prec
), sign
))
1653 if (wi::le_p (op2
.upper_bound (), wi::zero (prec
), sign
))
1657 // If op2 does not contain 0, then LHS and OP1 can never be equal.
1658 if (!range_includes_zero_p (&op2
))
1661 return VREL_VARYING
;
1664 // PLUS is symmetrical, so we can simply call lhs_op1_relation with reversed
1668 operator_plus::lhs_op2_relation (const irange
&lhs
, const irange
&op1
,
1669 const irange
&op2
, relation_kind rel
) const
1671 return lhs_op1_relation (lhs
, op2
, op1
, rel
);
1675 operator_plus::wi_fold (irange
&r
, tree type
,
1676 const wide_int
&lh_lb
, const wide_int
&lh_ub
,
1677 const wide_int
&rh_lb
, const wide_int
&rh_ub
) const
1679 wi::overflow_type ov_lb
, ov_ub
;
1680 signop s
= TYPE_SIGN (type
);
1681 wide_int new_lb
= wi::add (lh_lb
, rh_lb
, s
, &ov_lb
);
1682 wide_int new_ub
= wi::add (lh_ub
, rh_ub
, s
, &ov_ub
);
1683 value_range_with_overflow (r
, type
, new_lb
, new_ub
, ov_lb
, ov_ub
);
1686 // Given addition or subtraction, determine the possible NORMAL ranges and
1687 // OVERFLOW ranges given an OFFSET range. ADD_P is true for addition.
1688 // Return the relation that exists between the LHS and OP1 in order for the
1689 // NORMAL range to apply.
1690 // a return value of VREL_VARYING means no ranges were applicable.
1692 static relation_kind
1693 plus_minus_ranges (irange
&r_ov
, irange
&r_normal
, const irange
&offset
,
1696 relation_kind kind
= VREL_VARYING
;
1697 // For now, only deal with constant adds. This could be extended to ranges
1698 // when someone is so motivated.
1699 if (!offset
.singleton_p () || offset
.zero_p ())
1702 // Always work with a positive offset. ie a+ -2 -> a-2 and a- -2 > a+2
1703 wide_int off
= offset
.lower_bound ();
1704 if (wi::neg_p (off
, SIGNED
))
1707 off
= wi::neg (off
);
1710 wi::overflow_type ov
;
1711 tree type
= offset
.type ();
1712 unsigned prec
= TYPE_PRECISION (type
);
1715 // calculate the normal range and relation for the operation.
1719 lb
= wi::zero (prec
);
1720 ub
= wi::sub (irange_val_max (type
), off
, UNSIGNED
, &ov
);
1727 ub
= irange_val_max (type
);
1730 int_range
<2> normal_range (type
, lb
, ub
);
1731 int_range
<2> ov_range (type
, lb
, ub
, VR_ANTI_RANGE
);
1734 r_normal
= normal_range
;
1738 // Once op1 has been calculated by operator_plus or operator_minus, check
1739 // to see if the relation passed causes any part of the calculation to
1740 // be not possible. ie
1741 // a_2 = b_3 + 1 with a_2 < b_3 can refine the range of b_3 to [INF, INF]
1742 // and that further refines a_2 to [0, 0].
1743 // R is the value of op1, OP2 is the offset being added/subtracted, REL is the
1744 // relation between LHS relation OP1 and ADD_P is true for PLUS, false for
1745 // MINUS. IF any adjustment can be made, R will reflect it.
1748 adjust_op1_for_overflow (irange
&r
, const irange
&op2
, relation_kind rel
,
1751 if (r
.undefined_p ())
1753 tree type
= r
.type ();
1754 // Check for unsigned overflow and calculate the overflow part.
1755 signop s
= TYPE_SIGN (type
);
1756 if (!TYPE_OVERFLOW_WRAPS (type
) || s
== SIGNED
)
1759 // Only work with <, <=, >, >= relations.
1760 if (!relation_lt_le_gt_ge_p (rel
))
1763 // Get the ranges for this offset.
1764 int_range_max normal
, overflow
;
1765 relation_kind k
= plus_minus_ranges (overflow
, normal
, op2
, add_p
);
1767 // VREL_VARYING means there are no adjustments.
1768 if (k
== VREL_VARYING
)
1771 // If the relations match use the normal range, otherwise use overflow range.
1772 if (relation_intersect (k
, rel
) == k
)
1773 r
.intersect (normal
);
1775 r
.intersect (overflow
);
1780 operator_plus::op1_range (irange
&r
, tree type
,
1783 relation_trio trio
) const
1785 if (lhs
.undefined_p ())
1787 // Start with the default operation.
1788 range_op_handler
minus (MINUS_EXPR
);
1791 bool res
= minus
.fold_range (r
, type
, lhs
, op2
);
1792 relation_kind rel
= trio
.lhs_op1 ();
1793 // Check for a relation refinement.
1795 adjust_op1_for_overflow (r
, op2
, rel
, true /* PLUS_EXPR */);
1800 operator_plus::op2_range (irange
&r
, tree type
,
1803 relation_trio rel
) const
1805 return op1_range (r
, type
, lhs
, op1
, rel
.swap_op1_op2 ());
1808 class operator_widen_plus_signed
: public range_operator
1811 virtual void wi_fold (irange
&r
, tree type
,
1812 const wide_int
&lh_lb
,
1813 const wide_int
&lh_ub
,
1814 const wide_int
&rh_lb
,
1815 const wide_int
&rh_ub
) const;
1816 } op_widen_plus_signed
;
1819 operator_widen_plus_signed::wi_fold (irange
&r
, tree type
,
1820 const wide_int
&lh_lb
,
1821 const wide_int
&lh_ub
,
1822 const wide_int
&rh_lb
,
1823 const wide_int
&rh_ub
) const
1825 wi::overflow_type ov_lb
, ov_ub
;
1826 signop s
= TYPE_SIGN (type
);
1829 = wide_int::from (lh_lb
, wi::get_precision (lh_lb
) * 2, SIGNED
);
1831 = wide_int::from (lh_ub
, wi::get_precision (lh_ub
) * 2, SIGNED
);
1832 wide_int rh_wlb
= wide_int::from (rh_lb
, wi::get_precision (rh_lb
) * 2, s
);
1833 wide_int rh_wub
= wide_int::from (rh_ub
, wi::get_precision (rh_ub
) * 2, s
);
1835 wide_int new_lb
= wi::add (lh_wlb
, rh_wlb
, s
, &ov_lb
);
1836 wide_int new_ub
= wi::add (lh_wub
, rh_wub
, s
, &ov_ub
);
1838 r
= int_range
<2> (type
, new_lb
, new_ub
);
1841 class operator_widen_plus_unsigned
: public range_operator
1844 virtual void wi_fold (irange
&r
, tree type
,
1845 const wide_int
&lh_lb
,
1846 const wide_int
&lh_ub
,
1847 const wide_int
&rh_lb
,
1848 const wide_int
&rh_ub
) const;
1849 } op_widen_plus_unsigned
;
1852 operator_widen_plus_unsigned::wi_fold (irange
&r
, tree type
,
1853 const wide_int
&lh_lb
,
1854 const wide_int
&lh_ub
,
1855 const wide_int
&rh_lb
,
1856 const wide_int
&rh_ub
) const
1858 wi::overflow_type ov_lb
, ov_ub
;
1859 signop s
= TYPE_SIGN (type
);
1862 = wide_int::from (lh_lb
, wi::get_precision (lh_lb
) * 2, UNSIGNED
);
1864 = wide_int::from (lh_ub
, wi::get_precision (lh_ub
) * 2, UNSIGNED
);
1865 wide_int rh_wlb
= wide_int::from (rh_lb
, wi::get_precision (rh_lb
) * 2, s
);
1866 wide_int rh_wub
= wide_int::from (rh_ub
, wi::get_precision (rh_ub
) * 2, s
);
1868 wide_int new_lb
= wi::add (lh_wlb
, rh_wlb
, s
, &ov_lb
);
1869 wide_int new_ub
= wi::add (lh_wub
, rh_wub
, s
, &ov_ub
);
1871 r
= int_range
<2> (type
, new_lb
, new_ub
);
1875 operator_minus::update_bitmask (irange
&r
, const irange
&lh
,
1876 const irange
&rh
) const
1878 update_known_bitmask (r
, MINUS_EXPR
, lh
, rh
);
1882 operator_minus::wi_fold (irange
&r
, tree type
,
1883 const wide_int
&lh_lb
, const wide_int
&lh_ub
,
1884 const wide_int
&rh_lb
, const wide_int
&rh_ub
) const
1886 wi::overflow_type ov_lb
, ov_ub
;
1887 signop s
= TYPE_SIGN (type
);
1888 wide_int new_lb
= wi::sub (lh_lb
, rh_ub
, s
, &ov_lb
);
1889 wide_int new_ub
= wi::sub (lh_ub
, rh_lb
, s
, &ov_ub
);
1890 value_range_with_overflow (r
, type
, new_lb
, new_ub
, ov_lb
, ov_ub
);
1894 // Return the relation between LHS and OP1 based on the relation between
1898 operator_minus::lhs_op1_relation (const irange
&, const irange
&op1
,
1899 const irange
&, relation_kind rel
) const
1901 if (!op1
.undefined_p () && TYPE_SIGN (op1
.type ()) == UNSIGNED
)
1910 return VREL_VARYING
;
1913 // Check to see if the relation REL between OP1 and OP2 has any effect on the
1914 // LHS of the expression. If so, apply it to LHS_RANGE. This is a helper
1915 // function for both MINUS_EXPR and POINTER_DIFF_EXPR.
1918 minus_op1_op2_relation_effect (irange
&lhs_range
, tree type
,
1919 const irange
&op1_range ATTRIBUTE_UNUSED
,
1920 const irange
&op2_range ATTRIBUTE_UNUSED
,
1923 if (rel
== VREL_VARYING
)
1926 int_range
<2> rel_range
;
1927 unsigned prec
= TYPE_PRECISION (type
);
1928 signop sgn
= TYPE_SIGN (type
);
1930 // == and != produce [0,0] and ~[0,0] regardless of wrapping.
1932 rel_range
= int_range
<2> (type
, wi::zero (prec
), wi::zero (prec
));
1933 else if (rel
== VREL_NE
)
1934 rel_range
= int_range
<2> (type
, wi::zero (prec
), wi::zero (prec
),
1936 else if (TYPE_OVERFLOW_WRAPS (type
))
1940 // For wrapping signed values and unsigned, if op1 > op2 or
1941 // op1 < op2, then op1 - op2 can be restricted to ~[0, 0].
1944 rel_range
= int_range
<2> (type
, wi::zero (prec
), wi::zero (prec
),
1955 // op1 > op2, op1 - op2 can be restricted to [1, +INF]
1957 rel_range
= int_range
<2> (type
, wi::one (prec
),
1958 wi::max_value (prec
, sgn
));
1960 // op1 >= op2, op1 - op2 can be restricted to [0, +INF]
1962 rel_range
= int_range
<2> (type
, wi::zero (prec
),
1963 wi::max_value (prec
, sgn
));
1965 // op1 < op2, op1 - op2 can be restricted to [-INF, -1]
1967 rel_range
= int_range
<2> (type
, wi::min_value (prec
, sgn
),
1968 wi::minus_one (prec
));
1970 // op1 <= op2, op1 - op2 can be restricted to [-INF, 0]
1972 rel_range
= int_range
<2> (type
, wi::min_value (prec
, sgn
),
1979 lhs_range
.intersect (rel_range
);
1984 operator_minus::op1_op2_relation_effect (irange
&lhs_range
, tree type
,
1985 const irange
&op1_range
,
1986 const irange
&op2_range
,
1987 relation_kind rel
) const
1989 return minus_op1_op2_relation_effect (lhs_range
, type
, op1_range
, op2_range
,
1994 operator_minus::op1_range (irange
&r
, tree type
,
1997 relation_trio trio
) const
1999 if (lhs
.undefined_p ())
2001 // Start with the default operation.
2002 range_op_handler
minus (PLUS_EXPR
);
2005 bool res
= minus
.fold_range (r
, type
, lhs
, op2
);
2006 relation_kind rel
= trio
.lhs_op1 ();
2008 adjust_op1_for_overflow (r
, op2
, rel
, false /* PLUS_EXPR */);
2014 operator_minus::op2_range (irange
&r
, tree type
,
2017 relation_trio
) const
2019 if (lhs
.undefined_p ())
2021 return fold_range (r
, type
, op1
, lhs
);
2025 operator_min::update_bitmask (irange
&r
, const irange
&lh
,
2026 const irange
&rh
) const
2028 update_known_bitmask (r
, MIN_EXPR
, lh
, rh
);
2032 operator_min::wi_fold (irange
&r
, tree type
,
2033 const wide_int
&lh_lb
, const wide_int
&lh_ub
,
2034 const wide_int
&rh_lb
, const wide_int
&rh_ub
) const
2036 signop s
= TYPE_SIGN (type
);
2037 wide_int new_lb
= wi::min (lh_lb
, rh_lb
, s
);
2038 wide_int new_ub
= wi::min (lh_ub
, rh_ub
, s
);
2039 value_range_with_overflow (r
, type
, new_lb
, new_ub
);
2044 operator_max::update_bitmask (irange
&r
, const irange
&lh
,
2045 const irange
&rh
) const
2047 update_known_bitmask (r
, MAX_EXPR
, lh
, rh
);
2051 operator_max::wi_fold (irange
&r
, tree type
,
2052 const wide_int
&lh_lb
, const wide_int
&lh_ub
,
2053 const wide_int
&rh_lb
, const wide_int
&rh_ub
) const
2055 signop s
= TYPE_SIGN (type
);
2056 wide_int new_lb
= wi::max (lh_lb
, rh_lb
, s
);
2057 wide_int new_ub
= wi::max (lh_ub
, rh_ub
, s
);
2058 value_range_with_overflow (r
, type
, new_lb
, new_ub
);
2062 // Calculate the cross product of two sets of ranges and return it.
2064 // Multiplications, divisions and shifts are a bit tricky to handle,
2065 // depending on the mix of signs we have in the two ranges, we need to
2066 // operate on different values to get the minimum and maximum values
2067 // for the new range. One approach is to figure out all the
2068 // variations of range combinations and do the operations.
2070 // However, this involves several calls to compare_values and it is
2071 // pretty convoluted. It's simpler to do the 4 operations (MIN0 OP
2072 // MIN1, MIN0 OP MAX1, MAX0 OP MIN1 and MAX0 OP MAX0 OP MAX1) and then
2073 // figure the smallest and largest values to form the new range.
2076 cross_product_operator::wi_cross_product (irange
&r
, tree type
,
2077 const wide_int
&lh_lb
,
2078 const wide_int
&lh_ub
,
2079 const wide_int
&rh_lb
,
2080 const wide_int
&rh_ub
) const
2082 wide_int cp1
, cp2
, cp3
, cp4
;
2083 // Default to varying.
2084 r
.set_varying (type
);
2086 // Compute the 4 cross operations, bailing if we get an overflow we
2088 if (wi_op_overflows (cp1
, type
, lh_lb
, rh_lb
))
2090 if (wi::eq_p (lh_lb
, lh_ub
))
2092 else if (wi_op_overflows (cp3
, type
, lh_ub
, rh_lb
))
2094 if (wi::eq_p (rh_lb
, rh_ub
))
2096 else if (wi_op_overflows (cp2
, type
, lh_lb
, rh_ub
))
2098 if (wi::eq_p (lh_lb
, lh_ub
))
2100 else if (wi_op_overflows (cp4
, type
, lh_ub
, rh_ub
))
2104 signop sign
= TYPE_SIGN (type
);
2105 if (wi::gt_p (cp1
, cp2
, sign
))
2106 std::swap (cp1
, cp2
);
2107 if (wi::gt_p (cp3
, cp4
, sign
))
2108 std::swap (cp3
, cp4
);
2110 // Choose min and max from the ordered pairs.
2111 wide_int res_lb
= wi::min (cp1
, cp3
, sign
);
2112 wide_int res_ub
= wi::max (cp2
, cp4
, sign
);
2113 value_range_with_overflow (r
, type
, res_lb
, res_ub
);
2118 operator_mult::update_bitmask (irange
&r
, const irange
&lh
,
2119 const irange
&rh
) const
2121 update_known_bitmask (r
, MULT_EXPR
, lh
, rh
);
2125 operator_mult::op1_range (irange
&r
, tree type
,
2126 const irange
&lhs
, const irange
&op2
,
2127 relation_trio
) const
2129 if (lhs
.undefined_p ())
2132 // We can't solve 0 = OP1 * N by dividing by N with a wrapping type.
2133 // For example: For 0 = OP1 * 2, OP1 could be 0, or MAXINT, whereas
2134 // for 4 = OP1 * 2, OP1 could be 2 or 130 (unsigned 8-bit)
2135 if (TYPE_OVERFLOW_WRAPS (type
))
2139 if (op2
.singleton_p (offset
) && offset
!= 0)
2140 return range_op_handler (TRUNC_DIV_EXPR
).fold_range (r
, type
, lhs
, op2
);
2145 operator_mult::op2_range (irange
&r
, tree type
,
2146 const irange
&lhs
, const irange
&op1
,
2147 relation_trio rel
) const
2149 return operator_mult::op1_range (r
, type
, lhs
, op1
, rel
.swap_op1_op2 ());
2153 operator_mult::wi_op_overflows (wide_int
&res
, tree type
,
2154 const wide_int
&w0
, const wide_int
&w1
) const
2156 wi::overflow_type overflow
= wi::OVF_NONE
;
2157 signop sign
= TYPE_SIGN (type
);
2158 res
= wi::mul (w0
, w1
, sign
, &overflow
);
2159 if (overflow
&& TYPE_OVERFLOW_UNDEFINED (type
))
2161 // For multiplication, the sign of the overflow is given
2162 // by the comparison of the signs of the operands.
2163 if (sign
== UNSIGNED
|| w0
.sign_mask () == w1
.sign_mask ())
2164 res
= wi::max_value (w0
.get_precision (), sign
);
2166 res
= wi::min_value (w0
.get_precision (), sign
);
2173 operator_mult::wi_fold (irange
&r
, tree type
,
2174 const wide_int
&lh_lb
, const wide_int
&lh_ub
,
2175 const wide_int
&rh_lb
, const wide_int
&rh_ub
) const
2177 if (TYPE_OVERFLOW_UNDEFINED (type
))
2179 wi_cross_product (r
, type
, lh_lb
, lh_ub
, rh_lb
, rh_ub
);
2183 // Multiply the ranges when overflow wraps. This is basically fancy
2184 // code so we don't drop to varying with an unsigned
2187 // This test requires 2*prec bits if both operands are signed and
2188 // 2*prec + 2 bits if either is not. Therefore, extend the values
2189 // using the sign of the result to PREC2. From here on out,
2190 // everything is just signed math no matter what the input types
2193 signop sign
= TYPE_SIGN (type
);
2194 unsigned prec
= TYPE_PRECISION (type
);
2195 widest2_int min0
= widest2_int::from (lh_lb
, sign
);
2196 widest2_int max0
= widest2_int::from (lh_ub
, sign
);
2197 widest2_int min1
= widest2_int::from (rh_lb
, sign
);
2198 widest2_int max1
= widest2_int::from (rh_ub
, sign
);
2199 widest2_int sizem1
= wi::mask
<widest2_int
> (prec
, false);
2200 widest2_int size
= sizem1
+ 1;
2202 // Canonicalize the intervals.
2203 if (sign
== UNSIGNED
)
2205 if (wi::ltu_p (size
, min0
+ max0
))
2210 if (wi::ltu_p (size
, min1
+ max1
))
2217 // Sort the 4 products so that min is in prod0 and max is in
2219 widest2_int prod0
= min0
* min1
;
2220 widest2_int prod1
= min0
* max1
;
2221 widest2_int prod2
= max0
* min1
;
2222 widest2_int prod3
= max0
* max1
;
2224 // min0min1 > max0max1
2226 std::swap (prod0
, prod3
);
2228 // min0max1 > max0min1
2230 std::swap (prod1
, prod2
);
2233 std::swap (prod0
, prod1
);
2236 std::swap (prod2
, prod3
);
2239 prod2
= prod3
- prod0
;
2240 if (wi::geu_p (prod2
, sizem1
))
2242 // Multiplying by X, where X is a power of 2 is [0,0][X,+INF].
2243 if (TYPE_UNSIGNED (type
) && rh_lb
== rh_ub
2244 && wi::exact_log2 (rh_lb
) != -1 && prec
> 1)
2246 r
.set (type
, rh_lb
, wi::max_value (prec
, sign
));
2248 zero
.set_zero (type
);
2252 // The range covers all values.
2253 r
.set_varying (type
);
2257 wide_int new_lb
= wide_int::from (prod0
, prec
, sign
);
2258 wide_int new_ub
= wide_int::from (prod3
, prec
, sign
);
2259 create_possibly_reversed_range (r
, type
, new_lb
, new_ub
);
2263 class operator_widen_mult_signed
: public range_operator
2266 virtual void wi_fold (irange
&r
, tree type
,
2267 const wide_int
&lh_lb
,
2268 const wide_int
&lh_ub
,
2269 const wide_int
&rh_lb
,
2270 const wide_int
&rh_ub
)
2272 } op_widen_mult_signed
;
2275 operator_widen_mult_signed::wi_fold (irange
&r
, tree type
,
2276 const wide_int
&lh_lb
,
2277 const wide_int
&lh_ub
,
2278 const wide_int
&rh_lb
,
2279 const wide_int
&rh_ub
) const
2281 signop s
= TYPE_SIGN (type
);
2283 wide_int lh_wlb
= wide_int::from (lh_lb
, wi::get_precision (lh_lb
) * 2, SIGNED
);
2284 wide_int lh_wub
= wide_int::from (lh_ub
, wi::get_precision (lh_ub
) * 2, SIGNED
);
2285 wide_int rh_wlb
= wide_int::from (rh_lb
, wi::get_precision (rh_lb
) * 2, s
);
2286 wide_int rh_wub
= wide_int::from (rh_ub
, wi::get_precision (rh_ub
) * 2, s
);
2288 /* We don't expect a widening multiplication to be able to overflow but range
2289 calculations for multiplications are complicated. After widening the
2290 operands lets call the base class. */
2291 return op_mult
.wi_fold (r
, type
, lh_wlb
, lh_wub
, rh_wlb
, rh_wub
);
2295 class operator_widen_mult_unsigned
: public range_operator
2298 virtual void wi_fold (irange
&r
, tree type
,
2299 const wide_int
&lh_lb
,
2300 const wide_int
&lh_ub
,
2301 const wide_int
&rh_lb
,
2302 const wide_int
&rh_ub
)
2304 } op_widen_mult_unsigned
;
2307 operator_widen_mult_unsigned::wi_fold (irange
&r
, tree type
,
2308 const wide_int
&lh_lb
,
2309 const wide_int
&lh_ub
,
2310 const wide_int
&rh_lb
,
2311 const wide_int
&rh_ub
) const
2313 signop s
= TYPE_SIGN (type
);
2315 wide_int lh_wlb
= wide_int::from (lh_lb
, wi::get_precision (lh_lb
) * 2, UNSIGNED
);
2316 wide_int lh_wub
= wide_int::from (lh_ub
, wi::get_precision (lh_ub
) * 2, UNSIGNED
);
2317 wide_int rh_wlb
= wide_int::from (rh_lb
, wi::get_precision (rh_lb
) * 2, s
);
2318 wide_int rh_wub
= wide_int::from (rh_ub
, wi::get_precision (rh_ub
) * 2, s
);
2320 /* We don't expect a widening multiplication to be able to overflow but range
2321 calculations for multiplications are complicated. After widening the
2322 operands lets call the base class. */
2323 return op_mult
.wi_fold (r
, type
, lh_wlb
, lh_wub
, rh_wlb
, rh_wub
);
2326 class operator_div
: public cross_product_operator
2329 operator_div (tree_code div_kind
) { m_code
= div_kind
; }
2330 virtual void wi_fold (irange
&r
, tree type
,
2331 const wide_int
&lh_lb
,
2332 const wide_int
&lh_ub
,
2333 const wide_int
&rh_lb
,
2334 const wide_int
&rh_ub
) const final override
;
2335 virtual bool wi_op_overflows (wide_int
&res
, tree type
,
2336 const wide_int
&, const wide_int
&)
2337 const final override
;
2338 void update_bitmask (irange
&r
, const irange
&lh
, const irange
&rh
) const
2339 { update_known_bitmask (r
, m_code
, lh
, rh
); }
2344 static operator_div
op_trunc_div (TRUNC_DIV_EXPR
);
2345 static operator_div
op_floor_div (FLOOR_DIV_EXPR
);
2346 static operator_div
op_round_div (ROUND_DIV_EXPR
);
2347 static operator_div
op_ceil_div (CEIL_DIV_EXPR
);
2350 operator_div::wi_op_overflows (wide_int
&res
, tree type
,
2351 const wide_int
&w0
, const wide_int
&w1
) const
2356 wi::overflow_type overflow
= wi::OVF_NONE
;
2357 signop sign
= TYPE_SIGN (type
);
2361 case EXACT_DIV_EXPR
:
2362 case TRUNC_DIV_EXPR
:
2363 res
= wi::div_trunc (w0
, w1
, sign
, &overflow
);
2365 case FLOOR_DIV_EXPR
:
2366 res
= wi::div_floor (w0
, w1
, sign
, &overflow
);
2368 case ROUND_DIV_EXPR
:
2369 res
= wi::div_round (w0
, w1
, sign
, &overflow
);
2372 res
= wi::div_ceil (w0
, w1
, sign
, &overflow
);
2378 if (overflow
&& TYPE_OVERFLOW_UNDEFINED (type
))
2380 // For division, the only case is -INF / -1 = +INF.
2381 res
= wi::max_value (w0
.get_precision (), sign
);
2388 operator_div::wi_fold (irange
&r
, tree type
,
2389 const wide_int
&lh_lb
, const wide_int
&lh_ub
,
2390 const wide_int
&rh_lb
, const wide_int
&rh_ub
) const
2392 const wide_int dividend_min
= lh_lb
;
2393 const wide_int dividend_max
= lh_ub
;
2394 const wide_int divisor_min
= rh_lb
;
2395 const wide_int divisor_max
= rh_ub
;
2396 signop sign
= TYPE_SIGN (type
);
2397 unsigned prec
= TYPE_PRECISION (type
);
2398 wide_int extra_min
, extra_max
;
2400 // If we know we won't divide by zero, just do the division.
2401 if (!wi_includes_zero_p (type
, divisor_min
, divisor_max
))
2403 wi_cross_product (r
, type
, dividend_min
, dividend_max
,
2404 divisor_min
, divisor_max
);
2408 // If we're definitely dividing by zero, there's nothing to do.
2409 if (wi_zero_p (type
, divisor_min
, divisor_max
))
2415 // Perform the division in 2 parts, [LB, -1] and [1, UB], which will
2416 // skip any division by zero.
2418 // First divide by the negative numbers, if any.
2419 if (wi::neg_p (divisor_min
, sign
))
2420 wi_cross_product (r
, type
, dividend_min
, dividend_max
,
2421 divisor_min
, wi::minus_one (prec
));
2425 // Then divide by the non-zero positive numbers, if any.
2426 if (wi::gt_p (divisor_max
, wi::zero (prec
), sign
))
2429 wi_cross_product (tmp
, type
, dividend_min
, dividend_max
,
2430 wi::one (prec
), divisor_max
);
2433 // We shouldn't still have undefined here.
2434 gcc_checking_assert (!r
.undefined_p ());
2438 class operator_exact_divide
: public operator_div
2440 using range_operator::op1_range
;
2442 operator_exact_divide () : operator_div (EXACT_DIV_EXPR
) { }
2443 virtual bool op1_range (irange
&r
, tree type
,
2446 relation_trio
) const;
2451 operator_exact_divide::op1_range (irange
&r
, tree type
,
2454 relation_trio
) const
2456 if (lhs
.undefined_p ())
2459 // [2, 4] = op1 / [3,3] since its exact divide, no need to worry about
2460 // remainders in the endpoints, so op1 = [2,4] * [3,3] = [6,12].
2461 // We wont bother trying to enumerate all the in between stuff :-P
2462 // TRUE accuracy is [6,6][9,9][12,12]. This is unlikely to matter most of
2463 // the time however.
2464 // If op2 is a multiple of 2, we would be able to set some non-zero bits.
2465 if (op2
.singleton_p (offset
) && offset
!= 0)
2466 return range_op_handler (MULT_EXPR
).fold_range (r
, type
, lhs
, op2
);
2471 class operator_lshift
: public cross_product_operator
2473 using range_operator::fold_range
;
2474 using range_operator::op1_range
;
2476 virtual bool op1_range (irange
&r
, tree type
, const irange
&lhs
,
2477 const irange
&op2
, relation_trio rel
= TRIO_VARYING
)
2478 const final override
;
2479 virtual bool fold_range (irange
&r
, tree type
, const irange
&op1
,
2480 const irange
&op2
, relation_trio rel
= TRIO_VARYING
)
2481 const final override
;
2483 virtual void wi_fold (irange
&r
, tree type
,
2484 const wide_int
&lh_lb
, const wide_int
&lh_ub
,
2485 const wide_int
&rh_lb
,
2486 const wide_int
&rh_ub
) const final override
;
2487 virtual bool wi_op_overflows (wide_int
&res
,
2490 const wide_int
&) const final override
;
2491 void update_bitmask (irange
&r
, const irange
&lh
,
2492 const irange
&rh
) const final override
2493 { update_known_bitmask (r
, LSHIFT_EXPR
, lh
, rh
); }
2494 // Check compatibility of LHS and op1.
2495 bool operand_check_p (tree t1
, tree t2
, tree
) const final override
2496 { return range_compatible_p (t1
, t2
); }
2499 class operator_rshift
: public cross_product_operator
2501 using range_operator::fold_range
;
2502 using range_operator::op1_range
;
2503 using range_operator::lhs_op1_relation
;
2505 virtual bool fold_range (irange
&r
, tree type
, const irange
&op1
,
2506 const irange
&op2
, relation_trio rel
= TRIO_VARYING
)
2507 const final override
;
2508 virtual void wi_fold (irange
&r
, tree type
,
2509 const wide_int
&lh_lb
,
2510 const wide_int
&lh_ub
,
2511 const wide_int
&rh_lb
,
2512 const wide_int
&rh_ub
) const final override
;
2513 virtual bool wi_op_overflows (wide_int
&res
,
2516 const wide_int
&w1
) const final override
;
2517 virtual bool op1_range (irange
&, tree type
, const irange
&lhs
,
2518 const irange
&op2
, relation_trio rel
= TRIO_VARYING
)
2519 const final override
;
2520 virtual relation_kind
lhs_op1_relation (const irange
&lhs
, const irange
&op1
,
2521 const irange
&op2
, relation_kind rel
)
2522 const final override
;
2523 void update_bitmask (irange
&r
, const irange
&lh
,
2524 const irange
&rh
) const final override
2525 { update_known_bitmask (r
, RSHIFT_EXPR
, lh
, rh
); }
2526 // Check compatibility of LHS and op1.
2527 bool operand_check_p (tree t1
, tree t2
, tree
) const final override
2528 { return range_compatible_p (t1
, t2
); }
2533 operator_rshift::lhs_op1_relation (const irange
&lhs ATTRIBUTE_UNUSED
,
2536 relation_kind
) const
2538 // If both operands range are >= 0, then the LHS <= op1.
2539 if (!op1
.undefined_p () && !op2
.undefined_p ()
2540 && wi::ge_p (op1
.lower_bound (), 0, TYPE_SIGN (op1
.type ()))
2541 && wi::ge_p (op2
.lower_bound (), 0, TYPE_SIGN (op2
.type ())))
2543 return VREL_VARYING
;
2547 operator_lshift::fold_range (irange
&r
, tree type
,
2550 relation_trio rel
) const
2552 int_range_max shift_range
;
2553 if (!get_shift_range (shift_range
, type
, op2
))
2555 if (op2
.undefined_p ())
2562 // Transform left shifts by constants into multiplies.
2563 if (shift_range
.singleton_p ())
2565 unsigned shift
= shift_range
.lower_bound ().to_uhwi ();
2566 wide_int tmp
= wi::set_bit_in_zero (shift
, TYPE_PRECISION (type
));
2567 int_range
<1> mult (type
, tmp
, tmp
);
2569 // Force wrapping multiplication.
2570 bool saved_flag_wrapv
= flag_wrapv
;
2571 bool saved_flag_wrapv_pointer
= flag_wrapv_pointer
;
2573 flag_wrapv_pointer
= 1;
2574 bool b
= op_mult
.fold_range (r
, type
, op1
, mult
);
2575 flag_wrapv
= saved_flag_wrapv
;
2576 flag_wrapv_pointer
= saved_flag_wrapv_pointer
;
2580 // Otherwise, invoke the generic fold routine.
2581 return range_operator::fold_range (r
, type
, op1
, shift_range
, rel
);
2585 operator_lshift::wi_fold (irange
&r
, tree type
,
2586 const wide_int
&lh_lb
, const wide_int
&lh_ub
,
2587 const wide_int
&rh_lb
, const wide_int
&rh_ub
) const
2589 signop sign
= TYPE_SIGN (type
);
2590 unsigned prec
= TYPE_PRECISION (type
);
2591 int overflow_pos
= sign
== SIGNED
? prec
- 1 : prec
;
2592 int bound_shift
= overflow_pos
- rh_ub
.to_shwi ();
2593 // If bound_shift == HOST_BITS_PER_WIDE_INT, the llshift can
2594 // overflow. However, for that to happen, rh.max needs to be zero,
2595 // which means rh is a singleton range of zero, which means we simply return
2596 // [lh_lb, lh_ub] as the range.
2597 if (wi::eq_p (rh_ub
, rh_lb
) && wi::eq_p (rh_ub
, 0))
2599 r
= int_range
<2> (type
, lh_lb
, lh_ub
);
2603 wide_int bound
= wi::set_bit_in_zero (bound_shift
, prec
);
2604 wide_int complement
= ~(bound
- 1);
2605 wide_int low_bound
, high_bound
;
2606 bool in_bounds
= false;
2608 if (sign
== UNSIGNED
)
2611 high_bound
= complement
;
2612 if (wi::ltu_p (lh_ub
, low_bound
))
2614 // [5, 6] << [1, 2] == [10, 24].
2615 // We're shifting out only zeroes, the value increases
2619 else if (wi::ltu_p (high_bound
, lh_lb
))
2621 // [0xffffff00, 0xffffffff] << [1, 2]
2622 // == [0xfffffc00, 0xfffffffe].
2623 // We're shifting out only ones, the value decreases
2630 // [-1, 1] << [1, 2] == [-4, 4]
2631 low_bound
= complement
;
2633 if (wi::lts_p (lh_ub
, high_bound
)
2634 && wi::lts_p (low_bound
, lh_lb
))
2636 // For non-negative numbers, we're shifting out only zeroes,
2637 // the value increases monotonically. For negative numbers,
2638 // we're shifting out only ones, the value decreases
2645 wi_cross_product (r
, type
, lh_lb
, lh_ub
, rh_lb
, rh_ub
);
2647 r
.set_varying (type
);
2651 operator_lshift::wi_op_overflows (wide_int
&res
, tree type
,
2652 const wide_int
&w0
, const wide_int
&w1
) const
2654 signop sign
= TYPE_SIGN (type
);
2657 // It's unclear from the C standard whether shifts can overflow.
2658 // The following code ignores overflow; perhaps a C standard
2659 // interpretation ruling is needed.
2660 res
= wi::rshift (w0
, -w1
, sign
);
2663 res
= wi::lshift (w0
, w1
);
2668 operator_lshift::op1_range (irange
&r
,
2672 relation_trio
) const
2674 if (lhs
.undefined_p ())
2677 if (!contains_zero_p (lhs
))
2678 r
.set_nonzero (type
);
2680 r
.set_varying (type
);
2683 if (op2
.singleton_p (shift
))
2685 if (wi::lt_p (shift
, 0, SIGNED
))
2687 if (wi::ge_p (shift
, wi::uhwi (TYPE_PRECISION (type
),
2688 TYPE_PRECISION (op2
.type ())),
2697 // Work completely in unsigned mode to start.
2699 int_range_max tmp_range
;
2700 if (TYPE_SIGN (type
) == SIGNED
)
2702 int_range_max tmp
= lhs
;
2703 utype
= unsigned_type_for (type
);
2704 range_cast (tmp
, utype
);
2705 op_rshift
.fold_range (tmp_range
, utype
, tmp
, op2
);
2708 op_rshift
.fold_range (tmp_range
, utype
, lhs
, op2
);
2710 // Start with ranges which can produce the LHS by right shifting the
2711 // result by the shift amount.
2712 // ie [0x08, 0xF0] = op1 << 2 will start with
2713 // [00001000, 11110000] = op1 << 2
2714 // [0x02, 0x4C] aka [00000010, 00111100]
2716 // Then create a range from the LB with the least significant upper bit
2717 // set, to the upper bound with all the bits set.
2718 // This would be [0x42, 0xFC] aka [01000010, 11111100].
2720 // Ideally we do this for each subrange, but just lump them all for now.
2721 unsigned low_bits
= TYPE_PRECISION (utype
) - shift
.to_uhwi ();
2722 wide_int up_mask
= wi::mask (low_bits
, true, TYPE_PRECISION (utype
));
2723 wide_int new_ub
= wi::bit_or (up_mask
, tmp_range
.upper_bound ());
2724 wide_int new_lb
= wi::set_bit (tmp_range
.lower_bound (), low_bits
);
2725 int_range
<2> fill_range (utype
, new_lb
, new_ub
);
2726 tmp_range
.union_ (fill_range
);
2729 range_cast (tmp_range
, type
);
2731 r
.intersect (tmp_range
);
2735 return !r
.varying_p ();
2739 operator_rshift::op1_range (irange
&r
,
2743 relation_trio
) const
2745 if (lhs
.undefined_p ())
2748 if (op2
.singleton_p (shift
))
2750 // Ignore nonsensical shifts.
2751 unsigned prec
= TYPE_PRECISION (type
);
2752 if (wi::ge_p (shift
,
2753 wi::uhwi (prec
, TYPE_PRECISION (op2
.type ())),
2762 // Folding the original operation may discard some impossible
2763 // ranges from the LHS.
2764 int_range_max lhs_refined
;
2765 op_rshift
.fold_range (lhs_refined
, type
, int_range
<1> (type
), op2
);
2766 lhs_refined
.intersect (lhs
);
2767 if (lhs_refined
.undefined_p ())
2772 int_range_max
shift_range (op2
.type (), shift
, shift
);
2773 int_range_max lb
, ub
;
2774 op_lshift
.fold_range (lb
, type
, lhs_refined
, shift_range
);
2776 // 0000 0111 = OP1 >> 3
2778 // OP1 is anything from 0011 1000 to 0011 1111. That is, a
2779 // range from LHS<<3 plus a mask of the 3 bits we shifted on the
2780 // right hand side (0x07).
2781 wide_int mask
= wi::bit_not (wi::lshift (wi::minus_one (prec
), shift
));
2782 int_range_max
mask_range (type
,
2783 wi::zero (TYPE_PRECISION (type
)),
2785 op_plus
.fold_range (ub
, type
, lb
, mask_range
);
2788 if (!contains_zero_p (lhs_refined
))
2790 mask_range
.invert ();
2791 r
.intersect (mask_range
);
2799 operator_rshift::wi_op_overflows (wide_int
&res
,
2802 const wide_int
&w1
) const
2804 signop sign
= TYPE_SIGN (type
);
2806 res
= wi::lshift (w0
, -w1
);
2809 // It's unclear from the C standard whether shifts can overflow.
2810 // The following code ignores overflow; perhaps a C standard
2811 // interpretation ruling is needed.
2812 res
= wi::rshift (w0
, w1
, sign
);
2818 operator_rshift::fold_range (irange
&r
, tree type
,
2821 relation_trio rel
) const
2823 int_range_max shift
;
2824 if (!get_shift_range (shift
, type
, op2
))
2826 if (op2
.undefined_p ())
2833 return range_operator::fold_range (r
, type
, op1
, shift
, rel
);
2837 operator_rshift::wi_fold (irange
&r
, tree type
,
2838 const wide_int
&lh_lb
, const wide_int
&lh_ub
,
2839 const wide_int
&rh_lb
, const wide_int
&rh_ub
) const
2841 wi_cross_product (r
, type
, lh_lb
, lh_ub
, rh_lb
, rh_ub
);
2845 // Add a partial equivalence between the LHS and op1 for casts.
2848 operator_cast::lhs_op1_relation (const irange
&lhs
,
2850 const irange
&op2 ATTRIBUTE_UNUSED
,
2851 relation_kind
) const
2853 if (lhs
.undefined_p () || op1
.undefined_p ())
2854 return VREL_VARYING
;
2855 unsigned lhs_prec
= TYPE_PRECISION (lhs
.type ());
2856 unsigned op1_prec
= TYPE_PRECISION (op1
.type ());
2857 // If the result gets sign extended into a larger type check first if this
2858 // qualifies as a partial equivalence.
2859 if (TYPE_SIGN (op1
.type ()) == SIGNED
&& lhs_prec
> op1_prec
)
2861 // If the result is sign extended, and the LHS is larger than op1,
2862 // check if op1's range can be negative as the sign extension will
2863 // cause the upper bits to be 1 instead of 0, invalidating the PE.
2864 int_range
<3> negs
= range_negatives (op1
.type ());
2865 negs
.intersect (op1
);
2866 if (!negs
.undefined_p ())
2867 return VREL_VARYING
;
2870 unsigned prec
= MIN (lhs_prec
, op1_prec
);
2871 return bits_to_pe (prec
);
2874 // Return TRUE if casting from INNER to OUTER is a truncating cast.
2877 operator_cast::truncating_cast_p (const irange
&inner
,
2878 const irange
&outer
) const
2880 return TYPE_PRECISION (outer
.type ()) < TYPE_PRECISION (inner
.type ());
2883 // Return TRUE if [MIN,MAX] is inside the domain of RANGE's type.
2886 operator_cast::inside_domain_p (const wide_int
&min
,
2887 const wide_int
&max
,
2888 const irange
&range
) const
2890 wide_int domain_min
= irange_val_min (range
.type ());
2891 wide_int domain_max
= irange_val_max (range
.type ());
2892 signop domain_sign
= TYPE_SIGN (range
.type ());
2893 return (wi::le_p (min
, domain_max
, domain_sign
)
2894 && wi::le_p (max
, domain_max
, domain_sign
)
2895 && wi::ge_p (min
, domain_min
, domain_sign
)
2896 && wi::ge_p (max
, domain_min
, domain_sign
));
2900 // Helper for fold_range which work on a pair at a time.
2903 operator_cast::fold_pair (irange
&r
, unsigned index
,
2904 const irange
&inner
,
2905 const irange
&outer
) const
2907 tree inner_type
= inner
.type ();
2908 tree outer_type
= outer
.type ();
2909 signop inner_sign
= TYPE_SIGN (inner_type
);
2910 unsigned outer_prec
= TYPE_PRECISION (outer_type
);
2912 // check to see if casting from INNER to OUTER is a conversion that
2913 // fits in the resulting OUTER type.
2914 wide_int inner_lb
= inner
.lower_bound (index
);
2915 wide_int inner_ub
= inner
.upper_bound (index
);
2916 if (truncating_cast_p (inner
, outer
))
2918 // We may be able to accommodate a truncating cast if the
2919 // resulting range can be represented in the target type...
2920 if (wi::rshift (wi::sub (inner_ub
, inner_lb
),
2921 wi::uhwi (outer_prec
, TYPE_PRECISION (inner
.type ())),
2924 r
.set_varying (outer_type
);
2928 // ...but we must still verify that the final range fits in the
2929 // domain. This catches -fstrict-enum restrictions where the domain
2930 // range is smaller than what fits in the underlying type.
2931 wide_int min
= wide_int::from (inner_lb
, outer_prec
, inner_sign
);
2932 wide_int max
= wide_int::from (inner_ub
, outer_prec
, inner_sign
);
2933 if (inside_domain_p (min
, max
, outer
))
2934 create_possibly_reversed_range (r
, outer_type
, min
, max
);
2936 r
.set_varying (outer_type
);
2941 operator_cast::fold_range (irange
&r
, tree type ATTRIBUTE_UNUSED
,
2942 const irange
&inner
,
2943 const irange
&outer
,
2944 relation_trio
) const
2946 if (empty_range_varying (r
, type
, inner
, outer
))
2949 gcc_checking_assert (outer
.varying_p ());
2950 gcc_checking_assert (inner
.num_pairs () > 0);
2952 // Avoid a temporary by folding the first pair directly into the result.
2953 fold_pair (r
, 0, inner
, outer
);
2955 // Then process any additional pairs by unioning with their results.
2956 for (unsigned x
= 1; x
< inner
.num_pairs (); ++x
)
2959 fold_pair (tmp
, x
, inner
, outer
);
2965 update_bitmask (r
, inner
, outer
);
2970 operator_cast::update_bitmask (irange
&r
, const irange
&lh
,
2971 const irange
&rh
) const
2973 update_known_bitmask (r
, CONVERT_EXPR
, lh
, rh
);
2977 operator_cast::op1_range (irange
&r
, tree type
,
2980 relation_trio
) const
2982 if (lhs
.undefined_p ())
2984 tree lhs_type
= lhs
.type ();
2985 gcc_checking_assert (types_compatible_p (op2
.type(), type
));
2987 // If we are calculating a pointer, shortcut to what we really care about.
2988 if (POINTER_TYPE_P (type
))
2990 // Conversion from other pointers or a constant (including 0/NULL)
2991 // are straightforward.
2992 if (POINTER_TYPE_P (lhs
.type ())
2993 || (lhs
.singleton_p ()
2994 && TYPE_PRECISION (lhs
.type ()) >= TYPE_PRECISION (type
)))
2997 range_cast (r
, type
);
3001 // If the LHS is not a pointer nor a singleton, then it is
3002 // either VARYING or non-zero.
3003 if (!lhs
.undefined_p () && !contains_zero_p (lhs
))
3004 r
.set_nonzero (type
);
3006 r
.set_varying (type
);
3012 if (truncating_cast_p (op2
, lhs
))
3014 if (lhs
.varying_p ())
3015 r
.set_varying (type
);
3018 // We want to insert the LHS as an unsigned value since it
3019 // would not trigger the signed bit of the larger type.
3020 int_range_max converted_lhs
= lhs
;
3021 range_cast (converted_lhs
, unsigned_type_for (lhs_type
));
3022 range_cast (converted_lhs
, type
);
3023 // Start by building the positive signed outer range for the type.
3024 wide_int lim
= wi::set_bit_in_zero (TYPE_PRECISION (lhs_type
),
3025 TYPE_PRECISION (type
));
3026 create_possibly_reversed_range (r
, type
, lim
,
3027 wi::max_value (TYPE_PRECISION (type
),
3029 // For the signed part, we need to simply union the 2 ranges now.
3030 r
.union_ (converted_lhs
);
3032 // Create maximal negative number outside of LHS bits.
3033 lim
= wi::mask (TYPE_PRECISION (lhs_type
), true,
3034 TYPE_PRECISION (type
));
3035 // Add this to the unsigned LHS range(s).
3036 int_range_max
lim_range (type
, lim
, lim
);
3037 int_range_max lhs_neg
;
3038 range_op_handler (PLUS_EXPR
).fold_range (lhs_neg
, type
,
3039 converted_lhs
, lim_range
);
3040 // lhs_neg now has all the negative versions of the LHS.
3041 // Now union in all the values from SIGNED MIN (0x80000) to
3042 // lim-1 in order to fill in all the ranges with the upper
3045 // PR 97317. If the lhs has only 1 bit less precision than the rhs,
3046 // we don't need to create a range from min to lim-1
3047 // calculate neg range traps trying to create [lim, lim - 1].
3048 wide_int min_val
= wi::min_value (TYPE_PRECISION (type
), SIGNED
);
3051 int_range_max
neg (type
,
3052 wi::min_value (TYPE_PRECISION (type
),
3055 lhs_neg
.union_ (neg
);
3057 // And finally, munge the signed and unsigned portions.
3060 // And intersect with any known value passed in the extra operand.
3066 if (TYPE_PRECISION (lhs_type
) == TYPE_PRECISION (type
))
3070 // The cast is not truncating, and the range is restricted to
3071 // the range of the RHS by this assignment.
3073 // Cast the range of the RHS to the type of the LHS.
3074 fold_range (tmp
, lhs_type
, int_range
<1> (type
), int_range
<1> (lhs_type
));
3075 // Intersect this with the LHS range will produce the range,
3076 // which will be cast to the RHS type before returning.
3077 tmp
.intersect (lhs
);
3080 // Cast the calculated range to the type of the RHS.
3081 fold_range (r
, type
, tmp
, int_range
<1> (type
));
3086 class operator_logical_and
: public range_operator
3088 using range_operator::fold_range
;
3089 using range_operator::op1_range
;
3090 using range_operator::op2_range
;
3092 virtual bool fold_range (irange
&r
, tree type
,
3095 relation_trio rel
= TRIO_VARYING
) const;
3096 virtual bool op1_range (irange
&r
, tree type
,
3099 relation_trio rel
= TRIO_VARYING
) const;
3100 virtual bool op2_range (irange
&r
, tree type
,
3103 relation_trio rel
= TRIO_VARYING
) const;
3104 // Check compatibility of all operands.
3105 bool operand_check_p (tree t1
, tree t2
, tree t3
) const final override
3106 { return range_compatible_p (t1
, t2
) && range_compatible_p (t1
, t3
); }
3110 operator_logical_and::fold_range (irange
&r
, tree type
,
3113 relation_trio
) const
3115 if (empty_range_varying (r
, type
, lh
, rh
))
3118 // Precision of LHS and both operands must match.
3119 if (TYPE_PRECISION (lh
.type ()) != TYPE_PRECISION (type
)
3120 || TYPE_PRECISION (type
) != TYPE_PRECISION (rh
.type ()))
3123 // 0 && anything is 0.
3124 if ((wi::eq_p (lh
.lower_bound (), 0) && wi::eq_p (lh
.upper_bound (), 0))
3125 || (wi::eq_p (lh
.lower_bound (), 0) && wi::eq_p (rh
.upper_bound (), 0)))
3126 r
= range_false (type
);
3127 else if (contains_zero_p (lh
) || contains_zero_p (rh
))
3128 // To reach this point, there must be a logical 1 on each side, and
3129 // the only remaining question is whether there is a zero or not.
3130 r
= range_true_and_false (type
);
3132 r
= range_true (type
);
3137 operator_logical_and::op1_range (irange
&r
, tree type
,
3139 const irange
&op2 ATTRIBUTE_UNUSED
,
3140 relation_trio
) const
3142 switch (get_bool_state (r
, lhs
, type
))
3145 // A true result means both sides of the AND must be true.
3146 r
= range_true (type
);
3149 // Any other result means only one side has to be false, the
3150 // other side can be anything. So we cannot be sure of any
3152 r
= range_true_and_false (type
);
3159 operator_logical_and::op2_range (irange
&r
, tree type
,
3162 relation_trio
) const
3164 return operator_logical_and::op1_range (r
, type
, lhs
, op1
);
3169 operator_bitwise_and::update_bitmask (irange
&r
, const irange
&lh
,
3170 const irange
&rh
) const
3172 update_known_bitmask (r
, BIT_AND_EXPR
, lh
, rh
);
3175 // Optimize BIT_AND_EXPR, BIT_IOR_EXPR and BIT_XOR_EXPR of signed types
3176 // by considering the number of leading redundant sign bit copies.
3177 // clrsb (X op Y) = min (clrsb (X), clrsb (Y)), so for example
3178 // [-1, 0] op [-1, 0] is [-1, 0] (where nonzero_bits doesn't help).
3180 wi_optimize_signed_bitwise_op (irange
&r
, tree type
,
3181 const wide_int
&lh_lb
, const wide_int
&lh_ub
,
3182 const wide_int
&rh_lb
, const wide_int
&rh_ub
)
3184 int lh_clrsb
= MIN (wi::clrsb (lh_lb
), wi::clrsb (lh_ub
));
3185 int rh_clrsb
= MIN (wi::clrsb (rh_lb
), wi::clrsb (rh_ub
));
3186 int new_clrsb
= MIN (lh_clrsb
, rh_clrsb
);
3189 int type_prec
= TYPE_PRECISION (type
);
3190 int rprec
= (type_prec
- new_clrsb
) - 1;
3191 value_range_with_overflow (r
, type
,
3192 wi::mask (rprec
, true, type_prec
),
3193 wi::mask (rprec
, false, type_prec
));
3197 // An AND of 8,16, 32 or 64 bits can produce a partial equivalence between
3201 operator_bitwise_and::lhs_op1_relation (const irange
&lhs
,
3204 relation_kind
) const
3206 if (lhs
.undefined_p () || op1
.undefined_p () || op2
.undefined_p ())
3207 return VREL_VARYING
;
3208 if (!op2
.singleton_p ())
3209 return VREL_VARYING
;
3210 // if val == 0xff or 0xFFFF OR 0Xffffffff OR 0Xffffffffffffffff, return TRUE
3211 int prec1
= TYPE_PRECISION (op1
.type ());
3212 int prec2
= TYPE_PRECISION (op2
.type ());
3214 wide_int mask
= op2
.lower_bound ();
3215 if (wi::eq_p (mask
, wi::mask (8, false, prec2
)))
3217 else if (wi::eq_p (mask
, wi::mask (16, false, prec2
)))
3219 else if (wi::eq_p (mask
, wi::mask (32, false, prec2
)))
3221 else if (wi::eq_p (mask
, wi::mask (64, false, prec2
)))
3223 return bits_to_pe (MIN (prec1
, mask_prec
));
3226 // Optimize BIT_AND_EXPR and BIT_IOR_EXPR in terms of a mask if
3227 // possible. Basically, see if we can optimize:
3231 // [LB op Z, UB op Z]
3233 // If the optimization was successful, accumulate the range in R and
3237 wi_optimize_and_or (irange
&r
,
3238 enum tree_code code
,
3240 const wide_int
&lh_lb
, const wide_int
&lh_ub
,
3241 const wide_int
&rh_lb
, const wide_int
&rh_ub
)
3243 // Calculate the singleton mask among the ranges, if any.
3244 wide_int lower_bound
, upper_bound
, mask
;
3245 if (wi::eq_p (rh_lb
, rh_ub
))
3248 lower_bound
= lh_lb
;
3249 upper_bound
= lh_ub
;
3251 else if (wi::eq_p (lh_lb
, lh_ub
))
3254 lower_bound
= rh_lb
;
3255 upper_bound
= rh_ub
;
3260 // If Z is a constant which (for op | its bitwise not) has n
3261 // consecutive least significant bits cleared followed by m 1
3262 // consecutive bits set immediately above it and either
3263 // m + n == precision, or (x >> (m + n)) == (y >> (m + n)).
3265 // The least significant n bits of all the values in the range are
3266 // cleared or set, the m bits above it are preserved and any bits
3267 // above these are required to be the same for all values in the
3271 if (code
== BIT_IOR_EXPR
)
3273 if (wi::eq_p (w
, 0))
3274 n
= w
.get_precision ();
3278 w
= ~(w
| wi::mask (n
, false, w
.get_precision ()));
3279 if (wi::eq_p (w
, 0))
3280 m
= w
.get_precision () - n
;
3282 m
= wi::ctz (w
) - n
;
3284 wide_int new_mask
= wi::mask (m
+ n
, true, w
.get_precision ());
3285 if ((new_mask
& lower_bound
) != (new_mask
& upper_bound
))
3288 wide_int res_lb
, res_ub
;
3289 if (code
== BIT_AND_EXPR
)
3291 res_lb
= wi::bit_and (lower_bound
, mask
);
3292 res_ub
= wi::bit_and (upper_bound
, mask
);
3294 else if (code
== BIT_IOR_EXPR
)
3296 res_lb
= wi::bit_or (lower_bound
, mask
);
3297 res_ub
= wi::bit_or (upper_bound
, mask
);
3301 value_range_with_overflow (r
, type
, res_lb
, res_ub
);
3303 // Furthermore, if the mask is non-zero, an IOR cannot contain zero.
3304 if (code
== BIT_IOR_EXPR
&& wi::ne_p (mask
, 0))
3307 tmp
.set_nonzero (type
);
3313 // For range [LB, UB] compute two wide_int bit masks.
3315 // In the MAYBE_NONZERO bit mask, if some bit is unset, it means that
3316 // for all numbers in the range the bit is 0, otherwise it might be 0
3319 // In the MUSTBE_NONZERO bit mask, if some bit is set, it means that
3320 // for all numbers in the range the bit is 1, otherwise it might be 0
3324 wi_set_zero_nonzero_bits (tree type
,
3325 const wide_int
&lb
, const wide_int
&ub
,
3326 wide_int
&maybe_nonzero
,
3327 wide_int
&mustbe_nonzero
)
3329 signop sign
= TYPE_SIGN (type
);
3331 if (wi::eq_p (lb
, ub
))
3332 maybe_nonzero
= mustbe_nonzero
= lb
;
3333 else if (wi::ge_p (lb
, 0, sign
) || wi::lt_p (ub
, 0, sign
))
3335 wide_int xor_mask
= lb
^ ub
;
3336 maybe_nonzero
= lb
| ub
;
3337 mustbe_nonzero
= lb
& ub
;
3340 wide_int mask
= wi::mask (wi::floor_log2 (xor_mask
), false,
3341 maybe_nonzero
.get_precision ());
3342 maybe_nonzero
= maybe_nonzero
| mask
;
3343 mustbe_nonzero
= wi::bit_and_not (mustbe_nonzero
, mask
);
3348 maybe_nonzero
= wi::minus_one (lb
.get_precision ());
3349 mustbe_nonzero
= wi::zero (lb
.get_precision ());
3354 operator_bitwise_and::wi_fold (irange
&r
, tree type
,
3355 const wide_int
&lh_lb
,
3356 const wide_int
&lh_ub
,
3357 const wide_int
&rh_lb
,
3358 const wide_int
&rh_ub
) const
3360 if (wi_optimize_and_or (r
, BIT_AND_EXPR
, type
, lh_lb
, lh_ub
, rh_lb
, rh_ub
))
3363 wide_int maybe_nonzero_lh
, mustbe_nonzero_lh
;
3364 wide_int maybe_nonzero_rh
, mustbe_nonzero_rh
;
3365 wi_set_zero_nonzero_bits (type
, lh_lb
, lh_ub
,
3366 maybe_nonzero_lh
, mustbe_nonzero_lh
);
3367 wi_set_zero_nonzero_bits (type
, rh_lb
, rh_ub
,
3368 maybe_nonzero_rh
, mustbe_nonzero_rh
);
3370 wide_int new_lb
= mustbe_nonzero_lh
& mustbe_nonzero_rh
;
3371 wide_int new_ub
= maybe_nonzero_lh
& maybe_nonzero_rh
;
3372 signop sign
= TYPE_SIGN (type
);
3373 unsigned prec
= TYPE_PRECISION (type
);
3374 // If both input ranges contain only negative values, we can
3375 // truncate the result range maximum to the minimum of the
3376 // input range maxima.
3377 if (wi::lt_p (lh_ub
, 0, sign
) && wi::lt_p (rh_ub
, 0, sign
))
3379 new_ub
= wi::min (new_ub
, lh_ub
, sign
);
3380 new_ub
= wi::min (new_ub
, rh_ub
, sign
);
3382 // If either input range contains only non-negative values
3383 // we can truncate the result range maximum to the respective
3384 // maximum of the input range.
3385 if (wi::ge_p (lh_lb
, 0, sign
))
3386 new_ub
= wi::min (new_ub
, lh_ub
, sign
);
3387 if (wi::ge_p (rh_lb
, 0, sign
))
3388 new_ub
= wi::min (new_ub
, rh_ub
, sign
);
3389 // PR68217: In case of signed & sign-bit-CST should
3390 // result in [-INF, 0] instead of [-INF, INF].
3391 if (wi::gt_p (new_lb
, new_ub
, sign
))
3393 wide_int sign_bit
= wi::set_bit_in_zero (prec
- 1, prec
);
3395 && ((wi::eq_p (lh_lb
, lh_ub
)
3396 && !wi::cmps (lh_lb
, sign_bit
))
3397 || (wi::eq_p (rh_lb
, rh_ub
)
3398 && !wi::cmps (rh_lb
, sign_bit
))))
3400 new_lb
= wi::min_value (prec
, sign
);
3401 new_ub
= wi::zero (prec
);
3404 // If the limits got swapped around, return varying.
3405 if (wi::gt_p (new_lb
, new_ub
,sign
))
3408 && wi_optimize_signed_bitwise_op (r
, type
,
3412 r
.set_varying (type
);
3415 value_range_with_overflow (r
, type
, new_lb
, new_ub
);
3419 set_nonzero_range_from_mask (irange
&r
, tree type
, const irange
&lhs
)
3421 if (lhs
.undefined_p () || contains_zero_p (lhs
))
3422 r
.set_varying (type
);
3424 r
.set_nonzero (type
);
3427 /* Find out smallest RES where RES > VAL && (RES & MASK) == RES, if any
3428 (otherwise return VAL). VAL and MASK must be zero-extended for
3429 precision PREC. If SGNBIT is non-zero, first xor VAL with SGNBIT
3430 (to transform signed values into unsigned) and at the end xor
3434 masked_increment (const wide_int
&val_in
, const wide_int
&mask
,
3435 const wide_int
&sgnbit
, unsigned int prec
)
3437 wide_int bit
= wi::one (prec
), res
;
3440 wide_int val
= val_in
^ sgnbit
;
3441 for (i
= 0; i
< prec
; i
++, bit
+= bit
)
3444 if ((res
& bit
) == 0)
3447 res
= wi::bit_and_not (val
+ bit
, res
);
3449 if (wi::gtu_p (res
, val
))
3450 return res
^ sgnbit
;
3452 return val
^ sgnbit
;
3455 // This was shamelessly stolen from register_edge_assert_for_2 and
3456 // adjusted to work with iranges.
3459 operator_bitwise_and::simple_op1_range_solver (irange
&r
, tree type
,
3461 const irange
&op2
) const
3463 if (!op2
.singleton_p ())
3465 set_nonzero_range_from_mask (r
, type
, lhs
);
3468 unsigned int nprec
= TYPE_PRECISION (type
);
3469 wide_int cst2v
= op2
.lower_bound ();
3470 bool cst2n
= wi::neg_p (cst2v
, TYPE_SIGN (type
));
3473 sgnbit
= wi::set_bit_in_zero (nprec
- 1, nprec
);
3475 sgnbit
= wi::zero (nprec
);
3477 // Solve [lhs.lower_bound (), +INF] = x & MASK.
3479 // Minimum unsigned value for >= if (VAL & CST2) == VAL is VAL and
3480 // maximum unsigned value is ~0. For signed comparison, if CST2
3481 // doesn't have the most significant bit set, handle it similarly. If
3482 // CST2 has MSB set, the minimum is the same, and maximum is ~0U/2.
3483 wide_int valv
= lhs
.lower_bound ();
3484 wide_int minv
= valv
& cst2v
, maxv
;
3485 bool we_know_nothing
= false;
3488 // If (VAL & CST2) != VAL, X & CST2 can't be equal to VAL.
3489 minv
= masked_increment (valv
, cst2v
, sgnbit
, nprec
);
3492 // If we can't determine anything on this bound, fall
3493 // through and conservatively solve for the other end point.
3494 we_know_nothing
= true;
3497 maxv
= wi::mask (nprec
- (cst2n
? 1 : 0), false, nprec
);
3498 if (we_know_nothing
)
3499 r
.set_varying (type
);
3501 create_possibly_reversed_range (r
, type
, minv
, maxv
);
3503 // Solve [-INF, lhs.upper_bound ()] = x & MASK.
3505 // Minimum unsigned value for <= is 0 and maximum unsigned value is
3506 // VAL | ~CST2 if (VAL & CST2) == VAL. Otherwise, find smallest
3508 // VAL2 > VAL && (VAL2 & CST2) == VAL2 and use (VAL2 - 1) | ~CST2
3510 // For signed comparison, if CST2 doesn't have most significant bit
3511 // set, handle it similarly. If CST2 has MSB set, the maximum is
3512 // the same and minimum is INT_MIN.
3513 valv
= lhs
.upper_bound ();
3514 minv
= valv
& cst2v
;
3519 maxv
= masked_increment (valv
, cst2v
, sgnbit
, nprec
);
3522 // If we couldn't determine anything on either bound, return
3524 if (we_know_nothing
)
3532 int_range
<2> upper_bits
;
3533 create_possibly_reversed_range (upper_bits
, type
, minv
, maxv
);
3534 r
.intersect (upper_bits
);
3538 operator_bitwise_and::op1_range (irange
&r
, tree type
,
3541 relation_trio
) const
3543 if (lhs
.undefined_p ())
3545 if (types_compatible_p (type
, boolean_type_node
))
3546 return op_logical_and
.op1_range (r
, type
, lhs
, op2
);
3549 for (unsigned i
= 0; i
< lhs
.num_pairs (); ++i
)
3551 int_range_max
chunk (lhs
.type (),
3552 lhs
.lower_bound (i
),
3553 lhs
.upper_bound (i
));
3555 simple_op1_range_solver (res
, type
, chunk
, op2
);
3558 if (r
.undefined_p ())
3559 set_nonzero_range_from_mask (r
, type
, lhs
);
3561 // For MASK == op1 & MASK, all the bits in MASK must be set in op1.
3563 if (lhs
== op2
&& lhs
.singleton_p (mask
))
3565 r
.update_bitmask (irange_bitmask (mask
, ~mask
));
3569 // For 0 = op1 & MASK, op1 is ~MASK.
3570 if (lhs
.zero_p () && op2
.singleton_p ())
3572 wide_int nz
= wi::bit_not (op2
.get_nonzero_bits ());
3573 int_range
<2> tmp (type
);
3574 tmp
.set_nonzero_bits (nz
);
3581 operator_bitwise_and::op2_range (irange
&r
, tree type
,
3584 relation_trio
) const
3586 return operator_bitwise_and::op1_range (r
, type
, lhs
, op1
);
3590 class operator_logical_or
: public range_operator
3592 using range_operator::fold_range
;
3593 using range_operator::op1_range
;
3594 using range_operator::op2_range
;
3596 virtual bool fold_range (irange
&r
, tree type
,
3599 relation_trio rel
= TRIO_VARYING
) const;
3600 virtual bool op1_range (irange
&r
, tree type
,
3603 relation_trio rel
= TRIO_VARYING
) const;
3604 virtual bool op2_range (irange
&r
, tree type
,
3607 relation_trio rel
= TRIO_VARYING
) const;
3608 // Check compatibility of all operands.
3609 bool operand_check_p (tree t1
, tree t2
, tree t3
) const final override
3610 { return range_compatible_p (t1
, t2
) && range_compatible_p (t1
, t3
); }
3614 operator_logical_or::fold_range (irange
&r
, tree type ATTRIBUTE_UNUSED
,
3617 relation_trio
) const
3619 if (empty_range_varying (r
, type
, lh
, rh
))
3628 operator_logical_or::op1_range (irange
&r
, tree type
,
3630 const irange
&op2 ATTRIBUTE_UNUSED
,
3631 relation_trio
) const
3633 switch (get_bool_state (r
, lhs
, type
))
3636 // A false result means both sides of the OR must be false.
3637 r
= range_false (type
);
3640 // Any other result means only one side has to be true, the
3641 // other side can be anything. so we can't be sure of any result
3643 r
= range_true_and_false (type
);
3650 operator_logical_or::op2_range (irange
&r
, tree type
,
3653 relation_trio
) const
3655 return operator_logical_or::op1_range (r
, type
, lhs
, op1
);
3660 operator_bitwise_or::update_bitmask (irange
&r
, const irange
&lh
,
3661 const irange
&rh
) const
3663 update_known_bitmask (r
, BIT_IOR_EXPR
, lh
, rh
);
3667 operator_bitwise_or::wi_fold (irange
&r
, tree type
,
3668 const wide_int
&lh_lb
,
3669 const wide_int
&lh_ub
,
3670 const wide_int
&rh_lb
,
3671 const wide_int
&rh_ub
) const
3673 if (wi_optimize_and_or (r
, BIT_IOR_EXPR
, type
, lh_lb
, lh_ub
, rh_lb
, rh_ub
))
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 wide_int new_lb
= mustbe_nonzero_lh
| mustbe_nonzero_rh
;
3683 wide_int new_ub
= maybe_nonzero_lh
| maybe_nonzero_rh
;
3684 signop sign
= TYPE_SIGN (type
);
3685 // If the input ranges contain only positive values we can
3686 // truncate the minimum of the result range to the maximum
3687 // of the input range minima.
3688 if (wi::ge_p (lh_lb
, 0, sign
)
3689 && wi::ge_p (rh_lb
, 0, sign
))
3691 new_lb
= wi::max (new_lb
, lh_lb
, sign
);
3692 new_lb
= wi::max (new_lb
, rh_lb
, sign
);
3694 // If either input range contains only negative values
3695 // we can truncate the minimum of the result range to the
3696 // respective minimum range.
3697 if (wi::lt_p (lh_ub
, 0, sign
))
3698 new_lb
= wi::max (new_lb
, lh_lb
, sign
);
3699 if (wi::lt_p (rh_ub
, 0, sign
))
3700 new_lb
= wi::max (new_lb
, rh_lb
, sign
);
3701 // If the limits got swapped around, return a conservative range.
3702 if (wi::gt_p (new_lb
, new_ub
, sign
))
3704 // Make sure that nonzero|X is nonzero.
3705 if (wi::gt_p (lh_lb
, 0, sign
)
3706 || wi::gt_p (rh_lb
, 0, sign
)
3707 || wi::lt_p (lh_ub
, 0, sign
)
3708 || wi::lt_p (rh_ub
, 0, sign
))
3709 r
.set_nonzero (type
);
3710 else if (sign
== SIGNED
3711 && wi_optimize_signed_bitwise_op (r
, type
,
3716 r
.set_varying (type
);
3719 value_range_with_overflow (r
, type
, new_lb
, new_ub
);
3723 operator_bitwise_or::op1_range (irange
&r
, tree type
,
3726 relation_trio
) const
3728 if (lhs
.undefined_p ())
3730 // If this is really a logical wi_fold, call that.
3731 if (types_compatible_p (type
, boolean_type_node
))
3732 return op_logical_or
.op1_range (r
, type
, lhs
, op2
);
3739 r
.set_varying (type
);
3744 operator_bitwise_or::op2_range (irange
&r
, tree type
,
3747 relation_trio
) const
3749 return operator_bitwise_or::op1_range (r
, type
, lhs
, op1
);
3753 operator_bitwise_xor::update_bitmask (irange
&r
, const irange
&lh
,
3754 const irange
&rh
) const
3756 update_known_bitmask (r
, BIT_XOR_EXPR
, lh
, rh
);
3760 operator_bitwise_xor::wi_fold (irange
&r
, tree type
,
3761 const wide_int
&lh_lb
,
3762 const wide_int
&lh_ub
,
3763 const wide_int
&rh_lb
,
3764 const wide_int
&rh_ub
) const
3766 signop sign
= TYPE_SIGN (type
);
3767 wide_int maybe_nonzero_lh
, mustbe_nonzero_lh
;
3768 wide_int maybe_nonzero_rh
, mustbe_nonzero_rh
;
3769 wi_set_zero_nonzero_bits (type
, lh_lb
, lh_ub
,
3770 maybe_nonzero_lh
, mustbe_nonzero_lh
);
3771 wi_set_zero_nonzero_bits (type
, rh_lb
, rh_ub
,
3772 maybe_nonzero_rh
, mustbe_nonzero_rh
);
3774 wide_int result_zero_bits
= ((mustbe_nonzero_lh
& mustbe_nonzero_rh
)
3775 | ~(maybe_nonzero_lh
| maybe_nonzero_rh
));
3776 wide_int result_one_bits
3777 = (wi::bit_and_not (mustbe_nonzero_lh
, maybe_nonzero_rh
)
3778 | wi::bit_and_not (mustbe_nonzero_rh
, maybe_nonzero_lh
));
3779 wide_int new_ub
= ~result_zero_bits
;
3780 wide_int new_lb
= result_one_bits
;
3782 // If the range has all positive or all negative values, the result
3783 // is better than VARYING.
3784 if (wi::lt_p (new_lb
, 0, sign
) || wi::ge_p (new_ub
, 0, sign
))
3785 value_range_with_overflow (r
, type
, new_lb
, new_ub
);
3786 else if (sign
== SIGNED
3787 && wi_optimize_signed_bitwise_op (r
, type
,
3792 r
.set_varying (type
);
3794 /* Furthermore, XOR is non-zero if its arguments can't be equal. */
3795 if (wi::lt_p (lh_ub
, rh_lb
, sign
)
3796 || wi::lt_p (rh_ub
, lh_lb
, sign
)
3797 || wi::ne_p (result_one_bits
, 0))
3800 tmp
.set_nonzero (type
);
3806 operator_bitwise_xor::op1_op2_relation_effect (irange
&lhs_range
,
3810 relation_kind rel
) const
3812 if (rel
== VREL_VARYING
)
3815 int_range
<2> rel_range
;
3820 rel_range
.set_zero (type
);
3823 rel_range
.set_nonzero (type
);
3829 lhs_range
.intersect (rel_range
);
3834 operator_bitwise_xor::op1_range (irange
&r
, tree type
,
3837 relation_trio
) const
3839 if (lhs
.undefined_p () || lhs
.varying_p ())
3844 if (types_compatible_p (type
, boolean_type_node
))
3846 switch (get_bool_state (r
, lhs
, type
))
3849 if (op2
.varying_p ())
3850 r
.set_varying (type
);
3851 else if (op2
.zero_p ())
3852 r
= range_true (type
);
3853 // See get_bool_state for the rationale
3854 else if (op2
.undefined_p () || contains_zero_p (op2
))
3855 r
= range_true_and_false (type
);
3857 r
= range_false (type
);
3867 r
.set_varying (type
);
3872 operator_bitwise_xor::op2_range (irange
&r
, tree type
,
3875 relation_trio
) const
3877 return operator_bitwise_xor::op1_range (r
, type
, lhs
, op1
);
3880 class operator_trunc_mod
: public range_operator
3882 using range_operator::op1_range
;
3883 using range_operator::op2_range
;
3885 virtual void wi_fold (irange
&r
, tree type
,
3886 const wide_int
&lh_lb
,
3887 const wide_int
&lh_ub
,
3888 const wide_int
&rh_lb
,
3889 const wide_int
&rh_ub
) const;
3890 virtual bool op1_range (irange
&r
, tree type
,
3893 relation_trio
) const;
3894 virtual bool op2_range (irange
&r
, tree type
,
3897 relation_trio
) const;
3898 void update_bitmask (irange
&r
, const irange
&lh
, const irange
&rh
) const
3899 { update_known_bitmask (r
, TRUNC_MOD_EXPR
, lh
, rh
); }
3903 operator_trunc_mod::wi_fold (irange
&r
, tree type
,
3904 const wide_int
&lh_lb
,
3905 const wide_int
&lh_ub
,
3906 const wide_int
&rh_lb
,
3907 const wide_int
&rh_ub
) const
3909 wide_int new_lb
, new_ub
, tmp
;
3910 signop sign
= TYPE_SIGN (type
);
3911 unsigned prec
= TYPE_PRECISION (type
);
3913 // Mod 0 is undefined.
3914 if (wi_zero_p (type
, rh_lb
, rh_ub
))
3920 // Check for constant and try to fold.
3921 if (lh_lb
== lh_ub
&& rh_lb
== rh_ub
)
3923 wi::overflow_type ov
= wi::OVF_NONE
;
3924 tmp
= wi::mod_trunc (lh_lb
, rh_lb
, sign
, &ov
);
3925 if (ov
== wi::OVF_NONE
)
3927 r
= int_range
<2> (type
, tmp
, tmp
);
3932 // ABS (A % B) < ABS (B) and either 0 <= A % B <= A or A <= A % B <= 0.
3937 new_ub
= wi::smax (new_ub
, tmp
);
3940 if (sign
== UNSIGNED
)
3941 new_lb
= wi::zero (prec
);
3946 if (wi::gts_p (tmp
, 0))
3947 tmp
= wi::zero (prec
);
3948 new_lb
= wi::smax (new_lb
, tmp
);
3951 if (sign
== SIGNED
&& wi::neg_p (tmp
))
3952 tmp
= wi::zero (prec
);
3953 new_ub
= wi::min (new_ub
, tmp
, sign
);
3955 value_range_with_overflow (r
, type
, new_lb
, new_ub
);
3959 operator_trunc_mod::op1_range (irange
&r
, tree type
,
3962 relation_trio
) const
3964 if (lhs
.undefined_p ())
3967 signop sign
= TYPE_SIGN (type
);
3968 unsigned prec
= TYPE_PRECISION (type
);
3969 // (a % b) >= x && x > 0 , then a >= x.
3970 if (wi::gt_p (lhs
.lower_bound (), 0, sign
))
3972 r
= value_range (type
, lhs
.lower_bound (), wi::max_value (prec
, sign
));
3975 // (a % b) <= x && x < 0 , then a <= x.
3976 if (wi::lt_p (lhs
.upper_bound (), 0, sign
))
3978 r
= value_range (type
, wi::min_value (prec
, sign
), lhs
.upper_bound ());
3985 operator_trunc_mod::op2_range (irange
&r
, tree type
,
3988 relation_trio
) const
3990 if (lhs
.undefined_p ())
3993 signop sign
= TYPE_SIGN (type
);
3994 unsigned prec
= TYPE_PRECISION (type
);
3995 // (a % b) >= x && x > 0 , then b is in ~[-x, x] for signed
3996 // or b > x for unsigned.
3997 if (wi::gt_p (lhs
.lower_bound (), 0, sign
))
4000 r
= value_range (type
, wi::neg (lhs
.lower_bound ()),
4001 lhs
.lower_bound (), VR_ANTI_RANGE
);
4002 else if (wi::lt_p (lhs
.lower_bound (), wi::max_value (prec
, sign
),
4004 r
= value_range (type
, lhs
.lower_bound () + 1,
4005 wi::max_value (prec
, sign
));
4010 // (a % b) <= x && x < 0 , then b is in ~[x, -x].
4011 if (wi::lt_p (lhs
.upper_bound (), 0, sign
))
4013 if (wi::gt_p (lhs
.upper_bound (), wi::min_value (prec
, sign
), sign
))
4014 r
= value_range (type
, lhs
.upper_bound (),
4015 wi::neg (lhs
.upper_bound ()), VR_ANTI_RANGE
);
4024 class operator_logical_not
: public range_operator
4026 using range_operator::fold_range
;
4027 using range_operator::op1_range
;
4029 virtual bool fold_range (irange
&r
, tree type
,
4032 relation_trio rel
= TRIO_VARYING
) const;
4033 virtual bool op1_range (irange
&r
, tree type
,
4036 relation_trio rel
= TRIO_VARYING
) const;
4037 // Check compatibility of LHS and op1.
4038 bool operand_check_p (tree t1
, tree t2
, tree
) const final override
4039 { return range_compatible_p (t1
, t2
); }
4042 // Folding a logical NOT, oddly enough, involves doing nothing on the
4043 // forward pass through. During the initial walk backwards, the
4044 // logical NOT reversed the desired outcome on the way back, so on the
4045 // way forward all we do is pass the range forward.
4050 // to determine the TRUE branch, walking backward
4051 // if (b_3) if ([1,1])
4052 // b_3 = !b_2 [1,1] = ![0,0]
4053 // b_2 = x_1 < 20 [0,0] = x_1 < 20, false, so x_1 == [20, 255]
4054 // which is the result we are looking for.. so.. pass it through.
4057 operator_logical_not::fold_range (irange
&r
, tree type
,
4059 const irange
&rh ATTRIBUTE_UNUSED
,
4060 relation_trio
) const
4062 if (empty_range_varying (r
, type
, lh
, rh
))
4066 if (!lh
.varying_p () && !lh
.undefined_p ())
4073 operator_logical_not::op1_range (irange
&r
,
4077 relation_trio
) const
4079 // Logical NOT is involutary...do it again.
4080 return fold_range (r
, type
, lhs
, op2
);
4084 operator_bitwise_not::fold_range (irange
&r
, tree type
,
4087 relation_trio
) const
4089 if (empty_range_varying (r
, type
, lh
, rh
))
4092 if (types_compatible_p (type
, boolean_type_node
))
4093 return op_logical_not
.fold_range (r
, type
, lh
, rh
);
4095 // ~X is simply -1 - X.
4096 int_range
<1> minusone (type
, wi::minus_one (TYPE_PRECISION (type
)),
4097 wi::minus_one (TYPE_PRECISION (type
)));
4098 return range_op_handler (MINUS_EXPR
).fold_range (r
, type
, minusone
, lh
);
4102 operator_bitwise_not::op1_range (irange
&r
, tree type
,
4105 relation_trio
) const
4107 if (lhs
.undefined_p ())
4109 if (types_compatible_p (type
, boolean_type_node
))
4110 return op_logical_not
.op1_range (r
, type
, lhs
, op2
);
4112 // ~X is -1 - X and since bitwise NOT is involutary...do it again.
4113 return fold_range (r
, type
, lhs
, op2
);
4117 operator_bitwise_not::update_bitmask (irange
&r
, const irange
&lh
,
4118 const irange
&rh
) const
4120 update_known_bitmask (r
, BIT_NOT_EXPR
, lh
, rh
);
4125 operator_cst::fold_range (irange
&r
, tree type ATTRIBUTE_UNUSED
,
4127 const irange
&rh ATTRIBUTE_UNUSED
,
4128 relation_trio
) const
4135 // Determine if there is a relationship between LHS and OP1.
4138 operator_identity::lhs_op1_relation (const irange
&lhs
,
4139 const irange
&op1 ATTRIBUTE_UNUSED
,
4140 const irange
&op2 ATTRIBUTE_UNUSED
,
4141 relation_kind
) const
4143 if (lhs
.undefined_p ())
4144 return VREL_VARYING
;
4145 // Simply a copy, so they are equivalent.
4150 operator_identity::fold_range (irange
&r
, tree type ATTRIBUTE_UNUSED
,
4152 const irange
&rh ATTRIBUTE_UNUSED
,
4153 relation_trio
) const
4160 operator_identity::op1_range (irange
&r
, tree type ATTRIBUTE_UNUSED
,
4162 const irange
&op2 ATTRIBUTE_UNUSED
,
4163 relation_trio
) const
4170 class operator_unknown
: public range_operator
4172 using range_operator::fold_range
;
4174 virtual bool fold_range (irange
&r
, tree type
,
4177 relation_trio rel
= TRIO_VARYING
) const;
4181 operator_unknown::fold_range (irange
&r
, tree type
,
4182 const irange
&lh ATTRIBUTE_UNUSED
,
4183 const irange
&rh ATTRIBUTE_UNUSED
,
4184 relation_trio
) const
4186 r
.set_varying (type
);
4192 operator_abs::wi_fold (irange
&r
, tree type
,
4193 const wide_int
&lh_lb
, const wide_int
&lh_ub
,
4194 const wide_int
&rh_lb ATTRIBUTE_UNUSED
,
4195 const wide_int
&rh_ub ATTRIBUTE_UNUSED
) const
4198 signop sign
= TYPE_SIGN (type
);
4199 unsigned prec
= TYPE_PRECISION (type
);
4201 // Pass through LH for the easy cases.
4202 if (sign
== UNSIGNED
|| wi::ge_p (lh_lb
, 0, sign
))
4204 r
= int_range
<1> (type
, lh_lb
, lh_ub
);
4208 // -TYPE_MIN_VALUE = TYPE_MIN_VALUE with flag_wrapv so we can't get
4210 wide_int min_value
= wi::min_value (prec
, sign
);
4211 wide_int max_value
= wi::max_value (prec
, sign
);
4212 if (!TYPE_OVERFLOW_UNDEFINED (type
) && wi::eq_p (lh_lb
, min_value
))
4214 r
.set_varying (type
);
4218 // ABS_EXPR may flip the range around, if the original range
4219 // included negative values.
4220 if (wi::eq_p (lh_lb
, min_value
))
4222 // ABS ([-MIN, -MIN]) isn't representable, but we have traditionally
4223 // returned [-MIN,-MIN] so this preserves that behavior. PR37078
4224 if (wi::eq_p (lh_ub
, min_value
))
4226 r
= int_range
<1> (type
, min_value
, min_value
);
4232 min
= wi::abs (lh_lb
);
4234 if (wi::eq_p (lh_ub
, min_value
))
4237 max
= wi::abs (lh_ub
);
4239 // If the range contains zero then we know that the minimum value in the
4240 // range will be zero.
4241 if (wi::le_p (lh_lb
, 0, sign
) && wi::ge_p (lh_ub
, 0, sign
))
4243 if (wi::gt_p (min
, max
, sign
))
4245 min
= wi::zero (prec
);
4249 // If the range was reversed, swap MIN and MAX.
4250 if (wi::gt_p (min
, max
, sign
))
4251 std::swap (min
, max
);
4254 // If the new range has its limits swapped around (MIN > MAX), then
4255 // the operation caused one of them to wrap around. The only thing
4256 // we know is that the result is positive.
4257 if (wi::gt_p (min
, max
, sign
))
4259 min
= wi::zero (prec
);
4262 r
= int_range
<1> (type
, min
, max
);
4266 operator_abs::op1_range (irange
&r
, tree type
,
4269 relation_trio
) const
4271 if (empty_range_varying (r
, type
, lhs
, op2
))
4273 if (TYPE_UNSIGNED (type
))
4278 // Start with the positives because negatives are an impossible result.
4279 int_range_max positives
= range_positives (type
);
4280 positives
.intersect (lhs
);
4282 // Then add the negative of each pair:
4283 // ABS(op1) = [5,20] would yield op1 => [-20,-5][5,20].
4284 for (unsigned i
= 0; i
< positives
.num_pairs (); ++i
)
4285 r
.union_ (int_range
<1> (type
,
4286 -positives
.upper_bound (i
),
4287 -positives
.lower_bound (i
)));
4288 // With flag_wrapv, -TYPE_MIN_VALUE = TYPE_MIN_VALUE which is
4289 // unrepresentable. Add -TYPE_MIN_VALUE in this case.
4290 wide_int min_value
= wi::min_value (TYPE_PRECISION (type
), TYPE_SIGN (type
));
4291 wide_int lb
= lhs
.lower_bound ();
4292 if (!TYPE_OVERFLOW_UNDEFINED (type
) && wi::eq_p (lb
, min_value
))
4293 r
.union_ (int_range
<2> (type
, lb
, lb
));
4298 operator_abs::update_bitmask (irange
&r
, const irange
&lh
,
4299 const irange
&rh
) const
4301 update_known_bitmask (r
, ABS_EXPR
, lh
, rh
);
4304 class operator_absu
: public range_operator
4307 virtual void wi_fold (irange
&r
, tree type
,
4308 const wide_int
&lh_lb
, const wide_int
&lh_ub
,
4309 const wide_int
&rh_lb
, const wide_int
&rh_ub
) const;
4310 virtual void update_bitmask (irange
&r
, const irange
&lh
,
4311 const irange
&rh
) const final override
;
4315 operator_absu::wi_fold (irange
&r
, tree type
,
4316 const wide_int
&lh_lb
, const wide_int
&lh_ub
,
4317 const wide_int
&rh_lb ATTRIBUTE_UNUSED
,
4318 const wide_int
&rh_ub ATTRIBUTE_UNUSED
) const
4320 wide_int new_lb
, new_ub
;
4322 // Pass through VR0 the easy cases.
4323 if (wi::ges_p (lh_lb
, 0))
4330 new_lb
= wi::abs (lh_lb
);
4331 new_ub
= wi::abs (lh_ub
);
4333 // If the range contains zero then we know that the minimum
4334 // value in the range will be zero.
4335 if (wi::ges_p (lh_ub
, 0))
4337 if (wi::gtu_p (new_lb
, new_ub
))
4339 new_lb
= wi::zero (TYPE_PRECISION (type
));
4342 std::swap (new_lb
, new_ub
);
4345 gcc_checking_assert (TYPE_UNSIGNED (type
));
4346 r
= int_range
<1> (type
, new_lb
, new_ub
);
4350 operator_absu::update_bitmask (irange
&r
, const irange
&lh
,
4351 const irange
&rh
) const
4353 update_known_bitmask (r
, ABSU_EXPR
, lh
, rh
);
4358 operator_negate::fold_range (irange
&r
, tree type
,
4361 relation_trio
) const
4363 if (empty_range_varying (r
, type
, lh
, rh
))
4365 // -X is simply 0 - X.
4366 return range_op_handler (MINUS_EXPR
).fold_range (r
, type
,
4367 range_zero (type
), lh
);
4371 operator_negate::op1_range (irange
&r
, tree type
,
4374 relation_trio
) const
4376 // NEGATE is involutory.
4377 return fold_range (r
, type
, lhs
, op2
);
4382 operator_addr_expr::fold_range (irange
&r
, tree type
,
4385 relation_trio
) const
4387 if (empty_range_varying (r
, type
, lh
, rh
))
4390 // Return a non-null pointer of the LHS type (passed in op2).
4392 r
= range_zero (type
);
4393 else if (lh
.undefined_p () || contains_zero_p (lh
))
4394 r
.set_varying (type
);
4396 r
.set_nonzero (type
);
4401 operator_addr_expr::op1_range (irange
&r
, tree type
,
4404 relation_trio
) const
4406 if (empty_range_varying (r
, type
, lhs
, op2
))
4409 // Return a non-null pointer of the LHS type (passed in op2), but only
4410 // if we cant overflow, eitherwise a no-zero offset could wrap to zero.
4412 if (!lhs
.undefined_p () && !contains_zero_p (lhs
) && TYPE_OVERFLOW_UNDEFINED (type
))
4413 r
.set_nonzero (type
);
4415 r
.set_varying (type
);
4419 // Initialize any integral operators to the primary table
4422 range_op_table::initialize_integral_ops ()
4424 set (TRUNC_DIV_EXPR
, op_trunc_div
);
4425 set (FLOOR_DIV_EXPR
, op_floor_div
);
4426 set (ROUND_DIV_EXPR
, op_round_div
);
4427 set (CEIL_DIV_EXPR
, op_ceil_div
);
4428 set (EXACT_DIV_EXPR
, op_exact_div
);
4429 set (LSHIFT_EXPR
, op_lshift
);
4430 set (RSHIFT_EXPR
, op_rshift
);
4431 set (TRUTH_AND_EXPR
, op_logical_and
);
4432 set (TRUTH_OR_EXPR
, op_logical_or
);
4433 set (TRUNC_MOD_EXPR
, op_trunc_mod
);
4434 set (TRUTH_NOT_EXPR
, op_logical_not
);
4435 set (IMAGPART_EXPR
, op_unknown
);
4436 set (REALPART_EXPR
, op_unknown
);
4437 set (ABSU_EXPR
, op_absu
);
4438 set (OP_WIDEN_MULT_SIGNED
, op_widen_mult_signed
);
4439 set (OP_WIDEN_MULT_UNSIGNED
, op_widen_mult_unsigned
);
4440 set (OP_WIDEN_PLUS_SIGNED
, op_widen_plus_signed
);
4441 set (OP_WIDEN_PLUS_UNSIGNED
, op_widen_plus_unsigned
);
4446 operator_plus::overflow_free_p (const irange
&lh
, const irange
&rh
,
4447 relation_trio
) const
4449 if (lh
.undefined_p () || rh
.undefined_p ())
4452 tree type
= lh
.type ();
4453 if (TYPE_OVERFLOW_UNDEFINED (type
))
4456 wi::overflow_type ovf
;
4457 signop sgn
= TYPE_SIGN (type
);
4458 wide_int wmax0
= lh
.upper_bound ();
4459 wide_int wmax1
= rh
.upper_bound ();
4460 wi::add (wmax0
, wmax1
, sgn
, &ovf
);
4461 if (ovf
!= wi::OVF_NONE
)
4464 if (TYPE_UNSIGNED (type
))
4467 wide_int wmin0
= lh
.lower_bound ();
4468 wide_int wmin1
= rh
.lower_bound ();
4469 wi::add (wmin0
, wmin1
, sgn
, &ovf
);
4470 if (ovf
!= wi::OVF_NONE
)
4477 operator_minus::overflow_free_p (const irange
&lh
, const irange
&rh
,
4478 relation_trio
) const
4480 if (lh
.undefined_p () || rh
.undefined_p ())
4483 tree type
= lh
.type ();
4484 if (TYPE_OVERFLOW_UNDEFINED (type
))
4487 wi::overflow_type ovf
;
4488 signop sgn
= TYPE_SIGN (type
);
4489 wide_int wmin0
= lh
.lower_bound ();
4490 wide_int wmax1
= rh
.upper_bound ();
4491 wi::sub (wmin0
, wmax1
, sgn
, &ovf
);
4492 if (ovf
!= wi::OVF_NONE
)
4495 if (TYPE_UNSIGNED (type
))
4498 wide_int wmax0
= lh
.upper_bound ();
4499 wide_int wmin1
= rh
.lower_bound ();
4500 wi::sub (wmax0
, wmin1
, sgn
, &ovf
);
4501 if (ovf
!= wi::OVF_NONE
)
4508 operator_mult::overflow_free_p (const irange
&lh
, const irange
&rh
,
4509 relation_trio
) const
4511 if (lh
.undefined_p () || rh
.undefined_p ())
4514 tree type
= lh
.type ();
4515 if (TYPE_OVERFLOW_UNDEFINED (type
))
4518 wi::overflow_type ovf
;
4519 signop sgn
= TYPE_SIGN (type
);
4520 wide_int wmax0
= lh
.upper_bound ();
4521 wide_int wmax1
= rh
.upper_bound ();
4522 wi::mul (wmax0
, wmax1
, sgn
, &ovf
);
4523 if (ovf
!= wi::OVF_NONE
)
4526 if (TYPE_UNSIGNED (type
))
4529 wide_int wmin0
= lh
.lower_bound ();
4530 wide_int wmin1
= rh
.lower_bound ();
4531 wi::mul (wmin0
, wmin1
, sgn
, &ovf
);
4532 if (ovf
!= wi::OVF_NONE
)
4535 wi::mul (wmin0
, wmax1
, sgn
, &ovf
);
4536 if (ovf
!= wi::OVF_NONE
)
4539 wi::mul (wmax0
, wmin1
, sgn
, &ovf
);
4540 if (ovf
!= wi::OVF_NONE
)
4547 #include "selftest.h"
4551 #define INT(x) wi::shwi ((x), TYPE_PRECISION (integer_type_node))
4552 #define UINT(x) wi::uhwi ((x), TYPE_PRECISION (unsigned_type_node))
4553 #define INT16(x) wi::shwi ((x), TYPE_PRECISION (short_integer_type_node))
4554 #define UINT16(x) wi::uhwi ((x), TYPE_PRECISION (short_unsigned_type_node))
4555 #define SCHAR(x) wi::shwi ((x), TYPE_PRECISION (signed_char_type_node))
4556 #define UCHAR(x) wi::uhwi ((x), TYPE_PRECISION (unsigned_char_type_node))
4559 range_op_cast_tests ()
4561 int_range
<2> r0
, r1
, r2
, rold
;
4562 r0
.set_varying (integer_type_node
);
4563 wide_int maxint
= r0
.upper_bound ();
4565 // If a range is in any way outside of the range for the converted
4566 // to range, default to the range for the new type.
4567 r0
.set_varying (short_integer_type_node
);
4568 wide_int minshort
= r0
.lower_bound ();
4569 wide_int maxshort
= r0
.upper_bound ();
4570 if (TYPE_PRECISION (integer_type_node
)
4571 > TYPE_PRECISION (short_integer_type_node
))
4573 r1
= int_range
<1> (integer_type_node
,
4574 wi::zero (TYPE_PRECISION (integer_type_node
)),
4576 range_cast (r1
, short_integer_type_node
);
4577 ASSERT_TRUE (r1
.lower_bound () == minshort
4578 && r1
.upper_bound() == maxshort
);
4581 // (unsigned char)[-5,-1] => [251,255].
4582 r0
= rold
= int_range
<1> (signed_char_type_node
, SCHAR (-5), SCHAR (-1));
4583 range_cast (r0
, unsigned_char_type_node
);
4584 ASSERT_TRUE (r0
== int_range
<1> (unsigned_char_type_node
,
4585 UCHAR (251), UCHAR (255)));
4586 range_cast (r0
, signed_char_type_node
);
4587 ASSERT_TRUE (r0
== rold
);
4589 // (signed char)[15, 150] => [-128,-106][15,127].
4590 r0
= rold
= int_range
<1> (unsigned_char_type_node
, UCHAR (15), UCHAR (150));
4591 range_cast (r0
, signed_char_type_node
);
4592 r1
= int_range
<1> (signed_char_type_node
, SCHAR (15), SCHAR (127));
4593 r2
= int_range
<1> (signed_char_type_node
, SCHAR (-128), SCHAR (-106));
4595 ASSERT_TRUE (r1
== r0
);
4596 range_cast (r0
, unsigned_char_type_node
);
4597 ASSERT_TRUE (r0
== rold
);
4599 // (unsigned char)[-5, 5] => [0,5][251,255].
4600 r0
= rold
= int_range
<1> (signed_char_type_node
, SCHAR (-5), SCHAR (5));
4601 range_cast (r0
, unsigned_char_type_node
);
4602 r1
= int_range
<1> (unsigned_char_type_node
, UCHAR (251), UCHAR (255));
4603 r2
= int_range
<1> (unsigned_char_type_node
, UCHAR (0), UCHAR (5));
4605 ASSERT_TRUE (r0
== r1
);
4606 range_cast (r0
, signed_char_type_node
);
4607 ASSERT_TRUE (r0
== rold
);
4609 // (unsigned char)[-5,5] => [0,5][251,255].
4610 r0
= int_range
<1> (integer_type_node
, INT (-5), INT (5));
4611 range_cast (r0
, unsigned_char_type_node
);
4612 r1
= int_range
<1> (unsigned_char_type_node
, UCHAR (0), UCHAR (5));
4613 r1
.union_ (int_range
<1> (unsigned_char_type_node
, UCHAR (251), UCHAR (255)));
4614 ASSERT_TRUE (r0
== r1
);
4616 // (unsigned char)[5U,1974U] => [0,255].
4617 r0
= int_range
<1> (unsigned_type_node
, UINT (5), UINT (1974));
4618 range_cast (r0
, unsigned_char_type_node
);
4619 ASSERT_TRUE (r0
== int_range
<1> (unsigned_char_type_node
, UCHAR (0), UCHAR (255)));
4620 range_cast (r0
, integer_type_node
);
4621 // Going to a wider range should not sign extend.
4622 ASSERT_TRUE (r0
== int_range
<1> (integer_type_node
, INT (0), INT (255)));
4624 // (unsigned char)[-350,15] => [0,255].
4625 r0
= int_range
<1> (integer_type_node
, INT (-350), INT (15));
4626 range_cast (r0
, unsigned_char_type_node
);
4627 ASSERT_TRUE (r0
== (int_range
<1>
4628 (unsigned_char_type_node
,
4629 min_limit (unsigned_char_type_node
),
4630 max_limit (unsigned_char_type_node
))));
4632 // Casting [-120,20] from signed char to unsigned short.
4633 // => [0, 20][0xff88, 0xffff].
4634 r0
= int_range
<1> (signed_char_type_node
, SCHAR (-120), SCHAR (20));
4635 range_cast (r0
, short_unsigned_type_node
);
4636 r1
= int_range
<1> (short_unsigned_type_node
, UINT16 (0), UINT16 (20));
4637 r2
= int_range
<1> (short_unsigned_type_node
,
4638 UINT16 (0xff88), UINT16 (0xffff));
4640 ASSERT_TRUE (r0
== r1
);
4641 // A truncating cast back to signed char will work because [-120, 20]
4642 // is representable in signed char.
4643 range_cast (r0
, signed_char_type_node
);
4644 ASSERT_TRUE (r0
== int_range
<1> (signed_char_type_node
,
4645 SCHAR (-120), SCHAR (20)));
4647 // unsigned char -> signed short
4648 // (signed short)[(unsigned char)25, (unsigned char)250]
4649 // => [(signed short)25, (signed short)250]
4650 r0
= rold
= int_range
<1> (unsigned_char_type_node
, UCHAR (25), UCHAR (250));
4651 range_cast (r0
, short_integer_type_node
);
4652 r1
= int_range
<1> (short_integer_type_node
, INT16 (25), INT16 (250));
4653 ASSERT_TRUE (r0
== r1
);
4654 range_cast (r0
, unsigned_char_type_node
);
4655 ASSERT_TRUE (r0
== rold
);
4657 // Test casting a wider signed [-MIN,MAX] to a narrower unsigned.
4658 r0
= int_range
<1> (long_long_integer_type_node
,
4659 min_limit (long_long_integer_type_node
),
4660 max_limit (long_long_integer_type_node
));
4661 range_cast (r0
, short_unsigned_type_node
);
4662 r1
= int_range
<1> (short_unsigned_type_node
,
4663 min_limit (short_unsigned_type_node
),
4664 max_limit (short_unsigned_type_node
));
4665 ASSERT_TRUE (r0
== r1
);
4667 // Casting NONZERO to a narrower type will wrap/overflow so
4668 // it's just the entire range for the narrower type.
4670 // "NOT 0 at signed 32-bits" ==> [-MIN_32,-1][1, +MAX_32]. This is
4671 // is outside of the range of a smaller range, return the full
4673 if (TYPE_PRECISION (integer_type_node
)
4674 > TYPE_PRECISION (short_integer_type_node
))
4676 r0
= range_nonzero (integer_type_node
);
4677 range_cast (r0
, short_integer_type_node
);
4678 r1
= int_range
<1> (short_integer_type_node
,
4679 min_limit (short_integer_type_node
),
4680 max_limit (short_integer_type_node
));
4681 ASSERT_TRUE (r0
== r1
);
4684 // Casting NONZERO from a narrower signed to a wider signed.
4686 // NONZERO signed 16-bits is [-MIN_16,-1][1, +MAX_16].
4687 // Converting this to 32-bits signed is [-MIN_16,-1][1, +MAX_16].
4688 r0
= range_nonzero (short_integer_type_node
);
4689 range_cast (r0
, integer_type_node
);
4690 r1
= int_range
<1> (integer_type_node
, INT (-32768), INT (-1));
4691 r2
= int_range
<1> (integer_type_node
, INT (1), INT (32767));
4693 ASSERT_TRUE (r0
== r1
);
4697 range_op_lshift_tests ()
4699 // Test that 0x808.... & 0x8.... still contains 0x8....
4700 // for a large set of numbers.
4703 tree big_type
= long_long_unsigned_type_node
;
4704 unsigned big_prec
= TYPE_PRECISION (big_type
);
4705 // big_num = 0x808,0000,0000,0000
4706 wide_int big_num
= wi::lshift (wi::uhwi (0x808, big_prec
),
4707 wi::uhwi (48, big_prec
));
4708 op_bitwise_and
.fold_range (res
, big_type
,
4709 int_range
<1> (big_type
),
4710 int_range
<1> (big_type
, big_num
, big_num
));
4711 // val = 0x8,0000,0000,0000
4712 wide_int val
= wi::lshift (wi::uhwi (8, big_prec
),
4713 wi::uhwi (48, big_prec
));
4714 ASSERT_TRUE (res
.contains_p (val
));
4717 if (TYPE_PRECISION (unsigned_type_node
) > 31)
4719 // unsigned VARYING = op1 << 1 should be VARYING.
4720 int_range
<2> lhs (unsigned_type_node
);
4721 int_range
<2> shift (unsigned_type_node
, INT (1), INT (1));
4723 op_lshift
.op1_range (op1
, unsigned_type_node
, lhs
, shift
);
4724 ASSERT_TRUE (op1
.varying_p ());
4726 // 0 = op1 << 1 should be [0,0], [0x8000000, 0x8000000].
4727 int_range
<2> zero (unsigned_type_node
, UINT (0), UINT (0));
4728 op_lshift
.op1_range (op1
, unsigned_type_node
, zero
, shift
);
4729 ASSERT_TRUE (op1
.num_pairs () == 2);
4730 // Remove the [0,0] range.
4731 op1
.intersect (zero
);
4732 ASSERT_TRUE (op1
.num_pairs () == 1);
4733 // op1 << 1 should be [0x8000,0x8000] << 1,
4734 // which should result in [0,0].
4735 int_range_max result
;
4736 op_lshift
.fold_range (result
, unsigned_type_node
, op1
, shift
);
4737 ASSERT_TRUE (result
== zero
);
4739 // signed VARYING = op1 << 1 should be VARYING.
4740 if (TYPE_PRECISION (integer_type_node
) > 31)
4742 // unsigned VARYING = op1 << 1 should be VARYING.
4743 int_range
<2> lhs (integer_type_node
);
4744 int_range
<2> shift (integer_type_node
, INT (1), INT (1));
4746 op_lshift
.op1_range (op1
, integer_type_node
, lhs
, shift
);
4747 ASSERT_TRUE (op1
.varying_p ());
4749 // 0 = op1 << 1 should be [0,0], [0x8000000, 0x8000000].
4750 int_range
<2> zero (integer_type_node
, INT (0), INT (0));
4751 op_lshift
.op1_range (op1
, integer_type_node
, zero
, shift
);
4752 ASSERT_TRUE (op1
.num_pairs () == 2);
4753 // Remove the [0,0] range.
4754 op1
.intersect (zero
);
4755 ASSERT_TRUE (op1
.num_pairs () == 1);
4756 // op1 << 1 should be [0x8000,0x8000] << 1,
4757 // which should result in [0,0].
4758 int_range_max result
;
4759 op_lshift
.fold_range (result
, unsigned_type_node
, op1
, shift
);
4760 ASSERT_TRUE (result
== zero
);
4765 range_op_rshift_tests ()
4767 // unsigned: [3, MAX] = OP1 >> 1
4769 int_range_max
lhs (unsigned_type_node
,
4770 UINT (3), max_limit (unsigned_type_node
));
4771 int_range_max
one (unsigned_type_node
,
4772 wi::one (TYPE_PRECISION (unsigned_type_node
)),
4773 wi::one (TYPE_PRECISION (unsigned_type_node
)));
4775 op_rshift
.op1_range (op1
, unsigned_type_node
, lhs
, one
);
4776 ASSERT_FALSE (op1
.contains_p (UINT (3)));
4779 // signed: [3, MAX] = OP1 >> 1
4781 int_range_max
lhs (integer_type_node
,
4782 INT (3), max_limit (integer_type_node
));
4783 int_range_max
one (integer_type_node
, INT (1), INT (1));
4785 op_rshift
.op1_range (op1
, integer_type_node
, lhs
, one
);
4786 ASSERT_FALSE (op1
.contains_p (INT (-2)));
4789 // This is impossible, so OP1 should be [].
4790 // signed: [MIN, MIN] = OP1 >> 1
4792 int_range_max
lhs (integer_type_node
,
4793 min_limit (integer_type_node
),
4794 min_limit (integer_type_node
));
4795 int_range_max
one (integer_type_node
, INT (1), INT (1));
4797 op_rshift
.op1_range (op1
, integer_type_node
, lhs
, one
);
4798 ASSERT_TRUE (op1
.undefined_p ());
4801 // signed: ~[-1] = OP1 >> 31
4802 if (TYPE_PRECISION (integer_type_node
) > 31)
4804 int_range_max
lhs (integer_type_node
, INT (-1), INT (-1), VR_ANTI_RANGE
);
4805 int_range_max
shift (integer_type_node
, INT (31), INT (31));
4807 op_rshift
.op1_range (op1
, integer_type_node
, lhs
, shift
);
4808 int_range_max negatives
= range_negatives (integer_type_node
);
4809 negatives
.intersect (op1
);
4810 ASSERT_TRUE (negatives
.undefined_p ());
4815 range_op_bitwise_and_tests ()
4818 wide_int min
= min_limit (integer_type_node
);
4819 wide_int max
= max_limit (integer_type_node
);
4820 wide_int tiny
= wi::add (min
, wi::one (TYPE_PRECISION (integer_type_node
)));
4821 int_range_max
i1 (integer_type_node
, tiny
, max
);
4822 int_range_max
i2 (integer_type_node
, INT (255), INT (255));
4824 // [MIN+1, MAX] = OP1 & 255: OP1 is VARYING
4825 op_bitwise_and
.op1_range (res
, integer_type_node
, i1
, i2
);
4826 ASSERT_TRUE (res
== int_range
<1> (integer_type_node
));
4828 // VARYING = OP1 & 255: OP1 is VARYING
4829 i1
= int_range
<1> (integer_type_node
);
4830 op_bitwise_and
.op1_range (res
, integer_type_node
, i1
, i2
);
4831 ASSERT_TRUE (res
== int_range
<1> (integer_type_node
));
4833 // For 0 = x & MASK, x is ~MASK.
4835 int_range
<2> zero (integer_type_node
, INT (0), INT (0));
4836 int_range
<2> mask
= int_range
<2> (integer_type_node
, INT (7), INT (7));
4837 op_bitwise_and
.op1_range (res
, integer_type_node
, zero
, mask
);
4838 wide_int inv
= wi::shwi (~7U, TYPE_PRECISION (integer_type_node
));
4839 ASSERT_TRUE (res
.get_nonzero_bits () == inv
);
4842 // (NONZERO | X) is nonzero.
4843 i1
.set_nonzero (integer_type_node
);
4844 i2
.set_varying (integer_type_node
);
4845 op_bitwise_or
.fold_range (res
, integer_type_node
, i1
, i2
);
4846 ASSERT_TRUE (res
.nonzero_p ());
4848 // (NEGATIVE | X) is nonzero.
4849 i1
= int_range
<1> (integer_type_node
, INT (-5), INT (-3));
4850 i2
.set_varying (integer_type_node
);
4851 op_bitwise_or
.fold_range (res
, integer_type_node
, i1
, i2
);
4852 ASSERT_FALSE (res
.contains_p (INT (0)));
4856 range_relational_tests ()
4858 int_range
<2> lhs (unsigned_char_type_node
);
4859 int_range
<2> op1 (unsigned_char_type_node
, UCHAR (8), UCHAR (10));
4860 int_range
<2> op2 (unsigned_char_type_node
, UCHAR (20), UCHAR (20));
4862 // Never wrapping additions mean LHS > OP1.
4863 relation_kind code
= op_plus
.lhs_op1_relation (lhs
, op1
, op2
, VREL_VARYING
);
4864 ASSERT_TRUE (code
== VREL_GT
);
4866 // Most wrapping additions mean nothing...
4867 op1
= int_range
<2> (unsigned_char_type_node
, UCHAR (8), UCHAR (10));
4868 op2
= int_range
<2> (unsigned_char_type_node
, UCHAR (0), UCHAR (255));
4869 code
= op_plus
.lhs_op1_relation (lhs
, op1
, op2
, VREL_VARYING
);
4870 ASSERT_TRUE (code
== VREL_VARYING
);
4872 // However, always wrapping additions mean LHS < OP1.
4873 op1
= int_range
<2> (unsigned_char_type_node
, UCHAR (1), UCHAR (255));
4874 op2
= int_range
<2> (unsigned_char_type_node
, UCHAR (255), UCHAR (255));
4875 code
= op_plus
.lhs_op1_relation (lhs
, op1
, op2
, VREL_VARYING
);
4876 ASSERT_TRUE (code
== VREL_LT
);
4882 range_op_rshift_tests ();
4883 range_op_lshift_tests ();
4884 range_op_bitwise_and_tests ();
4885 range_op_cast_tests ();
4886 range_relational_tests ();
4888 extern void range_op_float_tests ();
4889 range_op_float_tests ();
4892 } // namespace selftest
4894 #endif // CHECKING_P