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 (),
439 TYPE_SIGN (lh
.type ())),
440 widest_int::from (lh_bits
.mask (),
441 TYPE_SIGN (lh
.type ())));
443 case GIMPLE_BINARY_RHS
:
444 bit_value_binop (code
, sign
, prec
, &widest_value
, &widest_mask
,
445 TYPE_SIGN (lh
.type ()),
446 TYPE_PRECISION (lh
.type ()),
447 widest_int::from (lh_bits
.value (), sign
),
448 widest_int::from (lh_bits
.mask (), sign
),
449 TYPE_SIGN (rh
.type ()),
450 TYPE_PRECISION (rh
.type ()),
451 widest_int::from (rh_bits
.value (), sign
),
452 widest_int::from (rh_bits
.mask (), sign
));
458 wide_int mask
= wide_int::from (widest_mask
, prec
, sign
);
459 wide_int value
= wide_int::from (widest_value
, prec
, sign
);
460 // Bitmasks must have the unknown value bits cleared.
462 irange_bitmask
bm (value
, mask
);
463 r
.update_bitmask (bm
);
466 // Return the upper limit for a type.
468 static inline wide_int
469 max_limit (const_tree type
)
471 return irange_val_max (type
);
474 // Return the lower limit for a type.
476 static inline wide_int
477 min_limit (const_tree type
)
479 return irange_val_min (type
);
482 // Return false if shifting by OP is undefined behavior. Otherwise, return
483 // true and the range it is to be shifted by. This allows trimming out of
484 // undefined ranges, leaving only valid ranges if there are any.
487 get_shift_range (irange
&r
, tree type
, const irange
&op
)
489 if (op
.undefined_p ())
492 // Build valid range and intersect it with the shift range.
493 r
= value_range (op
.type (),
494 wi::shwi (0, TYPE_PRECISION (op
.type ())),
495 wi::shwi (TYPE_PRECISION (type
) - 1, TYPE_PRECISION (op
.type ())));
498 // If there are no valid ranges in the shift range, returned false.
499 if (r
.undefined_p ())
504 // Default wide_int fold operation returns [MIN, MAX].
507 range_operator::wi_fold (irange
&r
, tree type
,
508 const wide_int
&lh_lb ATTRIBUTE_UNUSED
,
509 const wide_int
&lh_ub ATTRIBUTE_UNUSED
,
510 const wide_int
&rh_lb ATTRIBUTE_UNUSED
,
511 const wide_int
&rh_ub ATTRIBUTE_UNUSED
) const
513 gcc_checking_assert (r
.supports_type_p (type
));
514 r
.set_varying (type
);
517 // Call wi_fold when both op1 and op2 are equivalent. Further split small
518 // subranges into constants. This can provide better precision.
519 // For x + y, when x == y with a range of [0,4] instead of [0, 8] produce
520 // [0,0][2, 2][4,4][6, 6][8, 8]
521 // LIMIT is the maximum number of elements in range allowed before we
522 // do not process them individually.
525 range_operator::wi_fold_in_parts_equiv (irange
&r
, tree type
,
526 const wide_int
&lh_lb
,
527 const wide_int
&lh_ub
,
528 unsigned limit
) const
531 widest_int lh_range
= wi::sub (widest_int::from (lh_ub
, TYPE_SIGN (type
)),
532 widest_int::from (lh_lb
, TYPE_SIGN (type
)));
533 // if there are 1 to 8 values in the LH range, split them up.
535 if (lh_range
>= 0 && lh_range
< limit
)
537 for (unsigned x
= 0; x
<= lh_range
; x
++)
539 wide_int val
= lh_lb
+ x
;
540 wi_fold (tmp
, type
, val
, val
, val
, val
);
544 // Otherwise just call wi_fold.
546 wi_fold (r
, type
, lh_lb
, lh_ub
, lh_lb
, lh_ub
);
549 // Call wi_fold, except further split small subranges into constants.
550 // This can provide better precision. For something 8 >> [0,1]
551 // Instead of [8, 16], we will produce [8,8][16,16]
554 range_operator::wi_fold_in_parts (irange
&r
, tree type
,
555 const wide_int
&lh_lb
,
556 const wide_int
&lh_ub
,
557 const wide_int
&rh_lb
,
558 const wide_int
&rh_ub
) const
561 widest_int rh_range
= wi::sub (widest_int::from (rh_ub
, TYPE_SIGN (type
)),
562 widest_int::from (rh_lb
, TYPE_SIGN (type
)));
563 widest_int lh_range
= wi::sub (widest_int::from (lh_ub
, TYPE_SIGN (type
)),
564 widest_int::from (lh_lb
, TYPE_SIGN (type
)));
565 // If there are 2, 3, or 4 values in the RH range, do them separately.
566 // Call wi_fold_in_parts to check the RH side.
567 if (rh_range
> 0 && rh_range
< 4)
569 wi_fold_in_parts (r
, type
, lh_lb
, lh_ub
, rh_lb
, rh_lb
);
572 wi_fold_in_parts (tmp
, type
, lh_lb
, lh_ub
, rh_lb
+ 1, rh_lb
+ 1);
576 wi_fold_in_parts (tmp
, type
, lh_lb
, lh_ub
, rh_lb
+ 2, rh_lb
+ 2);
580 wi_fold_in_parts (tmp
, type
, lh_lb
, lh_ub
, rh_ub
, rh_ub
);
583 // Otherwise check for 2, 3, or 4 values in the LH range and split them up.
584 // The RH side has been checked, so no recursion needed.
585 else if (lh_range
> 0 && lh_range
< 4)
587 wi_fold (r
, type
, lh_lb
, lh_lb
, rh_lb
, rh_ub
);
590 wi_fold (tmp
, type
, lh_lb
+ 1, lh_lb
+ 1, rh_lb
, rh_ub
);
594 wi_fold (tmp
, type
, lh_lb
+ 2, lh_lb
+ 2, rh_lb
, rh_ub
);
598 wi_fold (tmp
, type
, lh_ub
, lh_ub
, rh_lb
, rh_ub
);
601 // Otherwise just call wi_fold.
603 wi_fold (r
, type
, lh_lb
, lh_ub
, rh_lb
, rh_ub
);
606 // The default for fold is to break all ranges into sub-ranges and
607 // invoke the wi_fold method on each sub-range pair.
610 range_operator::fold_range (irange
&r
, tree type
,
613 relation_trio trio
) const
615 gcc_checking_assert (r
.supports_type_p (type
));
616 if (empty_range_varying (r
, type
, lh
, rh
))
619 relation_kind rel
= trio
.op1_op2 ();
620 unsigned num_lh
= lh
.num_pairs ();
621 unsigned num_rh
= rh
.num_pairs ();
623 // If op1 and op2 are equivalences, then we don't need a complete cross
624 // product, just pairs of matching elements.
625 if (relation_equiv_p (rel
) && lh
== rh
)
629 for (unsigned x
= 0; x
< num_lh
; ++x
)
631 // If the number of subranges is too high, limit subrange creation.
632 unsigned limit
= (r
.num_pairs () > 32) ? 0 : 8;
633 wide_int lh_lb
= lh
.lower_bound (x
);
634 wide_int lh_ub
= lh
.upper_bound (x
);
635 wi_fold_in_parts_equiv (tmp
, type
, lh_lb
, lh_ub
, limit
);
640 op1_op2_relation_effect (r
, type
, lh
, rh
, rel
);
641 update_bitmask (r
, lh
, rh
);
645 // If both ranges are single pairs, fold directly into the result range.
646 // If the number of subranges grows too high, produce a summary result as the
647 // loop becomes exponential with little benefit. See PR 103821.
648 if ((num_lh
== 1 && num_rh
== 1) || num_lh
* num_rh
> 12)
650 wi_fold_in_parts (r
, type
, lh
.lower_bound (), lh
.upper_bound (),
651 rh
.lower_bound (), rh
.upper_bound ());
652 op1_op2_relation_effect (r
, type
, lh
, rh
, rel
);
653 update_bitmask (r
, lh
, rh
);
659 for (unsigned x
= 0; x
< num_lh
; ++x
)
660 for (unsigned y
= 0; y
< num_rh
; ++y
)
662 wide_int lh_lb
= lh
.lower_bound (x
);
663 wide_int lh_ub
= lh
.upper_bound (x
);
664 wide_int rh_lb
= rh
.lower_bound (y
);
665 wide_int rh_ub
= rh
.upper_bound (y
);
666 wi_fold_in_parts (tmp
, type
, lh_lb
, lh_ub
, rh_lb
, rh_ub
);
670 op1_op2_relation_effect (r
, type
, lh
, rh
, rel
);
671 update_bitmask (r
, lh
, rh
);
675 op1_op2_relation_effect (r
, type
, lh
, rh
, rel
);
676 update_bitmask (r
, lh
, rh
);
680 // The default for op1_range is to return false.
683 range_operator::op1_range (irange
&r ATTRIBUTE_UNUSED
,
684 tree type ATTRIBUTE_UNUSED
,
685 const irange
&lhs ATTRIBUTE_UNUSED
,
686 const irange
&op2 ATTRIBUTE_UNUSED
,
692 // The default for op2_range is to return false.
695 range_operator::op2_range (irange
&r ATTRIBUTE_UNUSED
,
696 tree type ATTRIBUTE_UNUSED
,
697 const irange
&lhs ATTRIBUTE_UNUSED
,
698 const irange
&op1 ATTRIBUTE_UNUSED
,
704 // The default relation routines return VREL_VARYING.
707 range_operator::lhs_op1_relation (const irange
&lhs ATTRIBUTE_UNUSED
,
708 const irange
&op1 ATTRIBUTE_UNUSED
,
709 const irange
&op2 ATTRIBUTE_UNUSED
,
710 relation_kind rel ATTRIBUTE_UNUSED
) const
716 range_operator::lhs_op2_relation (const irange
&lhs ATTRIBUTE_UNUSED
,
717 const irange
&op1 ATTRIBUTE_UNUSED
,
718 const irange
&op2 ATTRIBUTE_UNUSED
,
719 relation_kind rel ATTRIBUTE_UNUSED
) const
725 range_operator::op1_op2_relation (const irange
&lhs ATTRIBUTE_UNUSED
,
726 const irange
&op1 ATTRIBUTE_UNUSED
,
727 const irange
&op2 ATTRIBUTE_UNUSED
) const
732 // Default is no relation affects the LHS.
735 range_operator::op1_op2_relation_effect (irange
&lhs_range ATTRIBUTE_UNUSED
,
736 tree type ATTRIBUTE_UNUSED
,
737 const irange
&op1_range ATTRIBUTE_UNUSED
,
738 const irange
&op2_range ATTRIBUTE_UNUSED
,
739 relation_kind rel ATTRIBUTE_UNUSED
) const
745 range_operator::overflow_free_p (const irange
&, const irange
&,
751 // Apply any known bitmask updates based on this operator.
754 range_operator::update_bitmask (irange
&, const irange
&,
755 const irange
&) const
759 // Check that operand types are OK. Default to always OK.
762 range_operator::operand_check_p (tree
, tree
, tree
) const
767 // Create and return a range from a pair of wide-ints that are known
768 // to have overflowed (or underflowed).
771 value_range_from_overflowed_bounds (irange
&r
, tree type
,
772 const wide_int
&wmin
,
773 const wide_int
&wmax
)
775 const signop sgn
= TYPE_SIGN (type
);
776 const unsigned int prec
= TYPE_PRECISION (type
);
778 wide_int tmin
= wide_int::from (wmin
, prec
, sgn
);
779 wide_int tmax
= wide_int::from (wmax
, prec
, sgn
);
784 if (wi::cmp (tmin
, tmax
, sgn
) < 0)
787 if (wi::cmp (tmax
, tem
, sgn
) > 0)
790 // If the anti-range would cover nothing, drop to varying.
791 // Likewise if the anti-range bounds are outside of the types
793 if (covers
|| wi::cmp (tmin
, tmax
, sgn
) > 0)
794 r
.set_varying (type
);
796 r
.set (type
, tmin
, tmax
, VR_ANTI_RANGE
);
799 // Create and return a range from a pair of wide-ints. MIN_OVF and
800 // MAX_OVF describe any overflow that might have occurred while
801 // calculating WMIN and WMAX respectively.
804 value_range_with_overflow (irange
&r
, tree type
,
805 const wide_int
&wmin
, const wide_int
&wmax
,
806 wi::overflow_type min_ovf
= wi::OVF_NONE
,
807 wi::overflow_type max_ovf
= wi::OVF_NONE
)
809 const signop sgn
= TYPE_SIGN (type
);
810 const unsigned int prec
= TYPE_PRECISION (type
);
811 const bool overflow_wraps
= TYPE_OVERFLOW_WRAPS (type
);
813 // For one bit precision if max != min, then the range covers all
815 if (prec
== 1 && wi::ne_p (wmax
, wmin
))
817 r
.set_varying (type
);
823 // If overflow wraps, truncate the values and adjust the range,
824 // kind, and bounds appropriately.
825 if ((min_ovf
!= wi::OVF_NONE
) == (max_ovf
!= wi::OVF_NONE
))
827 wide_int tmin
= wide_int::from (wmin
, prec
, sgn
);
828 wide_int tmax
= wide_int::from (wmax
, prec
, sgn
);
829 // If the limits are swapped, we wrapped around and cover
831 if (wi::gt_p (tmin
, tmax
, sgn
))
832 r
.set_varying (type
);
834 // No overflow or both overflow or underflow. The range
835 // kind stays normal.
836 r
.set (type
, tmin
, tmax
);
840 if ((min_ovf
== wi::OVF_UNDERFLOW
&& max_ovf
== wi::OVF_NONE
)
841 || (max_ovf
== wi::OVF_OVERFLOW
&& min_ovf
== wi::OVF_NONE
))
842 value_range_from_overflowed_bounds (r
, type
, wmin
, wmax
);
844 // Other underflow and/or overflow, drop to VR_VARYING.
845 r
.set_varying (type
);
849 // If both bounds either underflowed or overflowed, then the result
851 if ((min_ovf
== wi::OVF_OVERFLOW
&& max_ovf
== wi::OVF_OVERFLOW
)
852 || (min_ovf
== wi::OVF_UNDERFLOW
&& max_ovf
== wi::OVF_UNDERFLOW
))
858 // If overflow does not wrap, saturate to [MIN, MAX].
859 wide_int new_lb
, new_ub
;
860 if (min_ovf
== wi::OVF_UNDERFLOW
)
861 new_lb
= wi::min_value (prec
, sgn
);
862 else if (min_ovf
== wi::OVF_OVERFLOW
)
863 new_lb
= wi::max_value (prec
, sgn
);
867 if (max_ovf
== wi::OVF_UNDERFLOW
)
868 new_ub
= wi::min_value (prec
, sgn
);
869 else if (max_ovf
== wi::OVF_OVERFLOW
)
870 new_ub
= wi::max_value (prec
, sgn
);
874 r
.set (type
, new_lb
, new_ub
);
878 // Create and return a range from a pair of wide-ints. Canonicalize
879 // the case where the bounds are swapped. In which case, we transform
880 // [10,5] into [MIN,5][10,MAX].
883 create_possibly_reversed_range (irange
&r
, tree type
,
884 const wide_int
&new_lb
, const wide_int
&new_ub
)
886 signop s
= TYPE_SIGN (type
);
887 // If the bounds are swapped, treat the result as if an overflow occurred.
888 if (wi::gt_p (new_lb
, new_ub
, s
))
889 value_range_from_overflowed_bounds (r
, type
, new_lb
, new_ub
);
891 // Otherwise it's just a normal range.
892 r
.set (type
, new_lb
, new_ub
);
895 // Return the summary information about boolean range LHS. If EMPTY/FULL,
896 // return the equivalent range for TYPE in R; if FALSE/TRUE, do nothing.
899 get_bool_state (vrange
&r
, const vrange
&lhs
, tree val_type
)
901 // If there is no result, then this is unexecutable.
902 if (lhs
.undefined_p ())
911 // For TRUE, we can't just test for [1,1] because Ada can have
912 // multi-bit booleans, and TRUE values can be: [1, MAX], ~[0], etc.
913 if (lhs
.contains_p (build_zero_cst (lhs
.type ())))
915 r
.set_varying (val_type
);
922 // ------------------------------------------------------------------------
925 operator_equal::update_bitmask (irange
&r
, const irange
&lh
,
926 const irange
&rh
) const
928 update_known_bitmask (r
, EQ_EXPR
, lh
, rh
);
931 // Check if the LHS range indicates a relation between OP1 and OP2.
934 operator_equal::op1_op2_relation (const irange
&lhs
, const irange
&,
935 const irange
&) const
937 if (lhs
.undefined_p ())
938 return VREL_UNDEFINED
;
940 // FALSE = op1 == op2 indicates NE_EXPR.
944 // TRUE = op1 == op2 indicates EQ_EXPR.
945 if (!contains_zero_p (lhs
))
951 operator_equal::fold_range (irange
&r
, tree type
,
954 relation_trio rel
) const
956 if (relop_early_resolve (r
, type
, op1
, op2
, rel
, VREL_EQ
))
959 // We can be sure the values are always equal or not if both ranges
960 // consist of a single value, and then compare them.
961 bool op1_const
= wi::eq_p (op1
.lower_bound (), op1
.upper_bound ());
962 bool op2_const
= wi::eq_p (op2
.lower_bound (), op2
.upper_bound ());
963 if (op1_const
&& op2_const
)
965 if (wi::eq_p (op1
.lower_bound (), op2
.upper_bound()))
966 r
= range_true (type
);
968 r
= range_false (type
);
972 // If ranges do not intersect, we know the range is not equal,
973 // otherwise we don't know anything for sure.
974 int_range_max tmp
= op1
;
976 if (tmp
.undefined_p ())
977 r
= range_false (type
);
978 // Check if a constant cannot satisfy the bitmask requirements.
979 else if (op2_const
&& !op1
.get_bitmask ().member_p (op2
.lower_bound ()))
980 r
= range_false (type
);
981 else if (op1_const
&& !op2
.get_bitmask ().member_p (op1
.lower_bound ()))
982 r
= range_false (type
);
984 r
= range_true_and_false (type
);
990 operator_equal::op1_range (irange
&r
, tree type
,
995 switch (get_bool_state (r
, lhs
, type
))
998 // If it's true, the result is the same as OP2.
1003 // If the result is false, the only time we know anything is
1004 // if OP2 is a constant.
1005 if (!op2
.undefined_p ()
1006 && wi::eq_p (op2
.lower_bound(), op2
.upper_bound()))
1012 r
.set_varying (type
);
1022 operator_equal::op2_range (irange
&r
, tree type
,
1025 relation_trio rel
) const
1027 return operator_equal::op1_range (r
, type
, lhs
, op1
, rel
.swap_op1_op2 ());
1030 // -------------------------------------------------------------------------
1033 operator_not_equal::update_bitmask (irange
&r
, const irange
&lh
,
1034 const irange
&rh
) const
1036 update_known_bitmask (r
, NE_EXPR
, lh
, rh
);
1039 // Check if the LHS range indicates a relation between OP1 and OP2.
1042 operator_not_equal::op1_op2_relation (const irange
&lhs
, const irange
&,
1043 const irange
&) const
1045 if (lhs
.undefined_p ())
1046 return VREL_UNDEFINED
;
1048 // FALSE = op1 != op2 indicates EQ_EXPR.
1052 // TRUE = op1 != op2 indicates NE_EXPR.
1053 if (!contains_zero_p (lhs
))
1055 return VREL_VARYING
;
1059 operator_not_equal::fold_range (irange
&r
, tree type
,
1062 relation_trio rel
) const
1064 if (relop_early_resolve (r
, type
, op1
, op2
, rel
, VREL_NE
))
1067 // We can be sure the values are always equal or not if both ranges
1068 // consist of a single value, and then compare them.
1069 bool op1_const
= wi::eq_p (op1
.lower_bound (), op1
.upper_bound ());
1070 bool op2_const
= wi::eq_p (op2
.lower_bound (), op2
.upper_bound ());
1071 if (op1_const
&& op2_const
)
1073 if (wi::ne_p (op1
.lower_bound (), op2
.upper_bound()))
1074 r
= range_true (type
);
1076 r
= range_false (type
);
1080 // If ranges do not intersect, we know the range is not equal,
1081 // otherwise we don't know anything for sure.
1082 int_range_max tmp
= op1
;
1083 tmp
.intersect (op2
);
1084 if (tmp
.undefined_p ())
1085 r
= range_true (type
);
1086 // Check if a constant cannot satisfy the bitmask requirements.
1087 else if (op2_const
&& !op1
.get_bitmask ().member_p (op2
.lower_bound ()))
1088 r
= range_true (type
);
1089 else if (op1_const
&& !op2
.get_bitmask ().member_p (op1
.lower_bound ()))
1090 r
= range_true (type
);
1092 r
= range_true_and_false (type
);
1098 operator_not_equal::op1_range (irange
&r
, tree type
,
1101 relation_trio
) const
1103 switch (get_bool_state (r
, lhs
, type
))
1106 // If the result is true, the only time we know anything is if
1107 // OP2 is a constant.
1108 if (!op2
.undefined_p ()
1109 && wi::eq_p (op2
.lower_bound(), op2
.upper_bound()))
1115 r
.set_varying (type
);
1119 // If it's false, the result is the same as OP2.
1131 operator_not_equal::op2_range (irange
&r
, tree type
,
1134 relation_trio rel
) const
1136 return operator_not_equal::op1_range (r
, type
, lhs
, op1
, rel
.swap_op1_op2 ());
1139 // (X < VAL) produces the range of [MIN, VAL - 1].
1142 build_lt (irange
&r
, tree type
, const wide_int
&val
)
1144 wi::overflow_type ov
;
1146 signop sgn
= TYPE_SIGN (type
);
1148 // Signed 1 bit cannot represent 1 for subtraction.
1150 lim
= wi::add (val
, -1, sgn
, &ov
);
1152 lim
= wi::sub (val
, 1, sgn
, &ov
);
1154 // If val - 1 underflows, check if X < MIN, which is an empty range.
1158 r
= int_range
<1> (type
, min_limit (type
), lim
);
1161 // (X <= VAL) produces the range of [MIN, VAL].
1164 build_le (irange
&r
, tree type
, const wide_int
&val
)
1166 r
= int_range
<1> (type
, min_limit (type
), val
);
1169 // (X > VAL) produces the range of [VAL + 1, MAX].
1172 build_gt (irange
&r
, tree type
, const wide_int
&val
)
1174 wi::overflow_type ov
;
1176 signop sgn
= TYPE_SIGN (type
);
1178 // Signed 1 bit cannot represent 1 for addition.
1180 lim
= wi::sub (val
, -1, sgn
, &ov
);
1182 lim
= wi::add (val
, 1, sgn
, &ov
);
1183 // If val + 1 overflows, check is for X > MAX, which is an empty range.
1187 r
= int_range
<1> (type
, lim
, max_limit (type
));
1190 // (X >= val) produces the range of [VAL, MAX].
1193 build_ge (irange
&r
, tree type
, const wide_int
&val
)
1195 r
= int_range
<1> (type
, val
, max_limit (type
));
1200 operator_lt::update_bitmask (irange
&r
, const irange
&lh
,
1201 const irange
&rh
) const
1203 update_known_bitmask (r
, LT_EXPR
, lh
, rh
);
1206 // Check if the LHS range indicates a relation between OP1 and OP2.
1209 operator_lt::op1_op2_relation (const irange
&lhs
, const irange
&,
1210 const irange
&) const
1212 if (lhs
.undefined_p ())
1213 return VREL_UNDEFINED
;
1215 // FALSE = op1 < op2 indicates GE_EXPR.
1219 // TRUE = op1 < op2 indicates LT_EXPR.
1220 if (!contains_zero_p (lhs
))
1222 return VREL_VARYING
;
1226 operator_lt::fold_range (irange
&r
, tree type
,
1229 relation_trio rel
) const
1231 if (relop_early_resolve (r
, type
, op1
, op2
, rel
, VREL_LT
))
1234 signop sign
= TYPE_SIGN (op1
.type ());
1235 gcc_checking_assert (sign
== TYPE_SIGN (op2
.type ()));
1237 if (wi::lt_p (op1
.upper_bound (), op2
.lower_bound (), sign
))
1238 r
= range_true (type
);
1239 else if (!wi::lt_p (op1
.lower_bound (), op2
.upper_bound (), sign
))
1240 r
= range_false (type
);
1241 // Use nonzero bits to determine if < 0 is false.
1242 else if (op2
.zero_p () && !wi::neg_p (op1
.get_nonzero_bits (), sign
))
1243 r
= range_false (type
);
1245 r
= range_true_and_false (type
);
1250 operator_lt::op1_range (irange
&r
, tree type
,
1253 relation_trio
) const
1255 if (op2
.undefined_p ())
1258 switch (get_bool_state (r
, lhs
, type
))
1261 build_lt (r
, type
, op2
.upper_bound ());
1265 build_ge (r
, type
, op2
.lower_bound ());
1275 operator_lt::op2_range (irange
&r
, tree type
,
1278 relation_trio
) const
1280 if (op1
.undefined_p ())
1283 switch (get_bool_state (r
, lhs
, type
))
1286 build_gt (r
, type
, op1
.lower_bound ());
1290 build_le (r
, type
, op1
.upper_bound ());
1301 operator_le::update_bitmask (irange
&r
, const irange
&lh
,
1302 const irange
&rh
) const
1304 update_known_bitmask (r
, LE_EXPR
, lh
, rh
);
1307 // Check if the LHS range indicates a relation between OP1 and OP2.
1310 operator_le::op1_op2_relation (const irange
&lhs
, const irange
&,
1311 const irange
&) const
1313 if (lhs
.undefined_p ())
1314 return VREL_UNDEFINED
;
1316 // FALSE = op1 <= op2 indicates GT_EXPR.
1320 // TRUE = op1 <= op2 indicates LE_EXPR.
1321 if (!contains_zero_p (lhs
))
1323 return VREL_VARYING
;
1327 operator_le::fold_range (irange
&r
, tree type
,
1330 relation_trio rel
) const
1332 if (relop_early_resolve (r
, type
, op1
, op2
, rel
, VREL_LE
))
1335 signop sign
= TYPE_SIGN (op1
.type ());
1336 gcc_checking_assert (sign
== TYPE_SIGN (op2
.type ()));
1338 if (wi::le_p (op1
.upper_bound (), op2
.lower_bound (), sign
))
1339 r
= range_true (type
);
1340 else if (!wi::le_p (op1
.lower_bound (), op2
.upper_bound (), sign
))
1341 r
= range_false (type
);
1343 r
= range_true_and_false (type
);
1348 operator_le::op1_range (irange
&r
, tree type
,
1351 relation_trio
) const
1353 if (op2
.undefined_p ())
1356 switch (get_bool_state (r
, lhs
, type
))
1359 build_le (r
, type
, op2
.upper_bound ());
1363 build_gt (r
, type
, op2
.lower_bound ());
1373 operator_le::op2_range (irange
&r
, tree type
,
1376 relation_trio
) const
1378 if (op1
.undefined_p ())
1381 switch (get_bool_state (r
, lhs
, type
))
1384 build_ge (r
, type
, op1
.lower_bound ());
1388 build_lt (r
, type
, op1
.upper_bound ());
1399 operator_gt::update_bitmask (irange
&r
, const irange
&lh
,
1400 const irange
&rh
) const
1402 update_known_bitmask (r
, GT_EXPR
, lh
, rh
);
1405 // Check if the LHS range indicates a relation between OP1 and OP2.
1408 operator_gt::op1_op2_relation (const irange
&lhs
, const irange
&,
1409 const irange
&) const
1411 if (lhs
.undefined_p ())
1412 return VREL_UNDEFINED
;
1414 // FALSE = op1 > op2 indicates LE_EXPR.
1418 // TRUE = op1 > op2 indicates GT_EXPR.
1419 if (!contains_zero_p (lhs
))
1421 return VREL_VARYING
;
1425 operator_gt::fold_range (irange
&r
, tree type
,
1426 const irange
&op1
, const irange
&op2
,
1427 relation_trio rel
) const
1429 if (relop_early_resolve (r
, type
, op1
, op2
, rel
, VREL_GT
))
1432 signop sign
= TYPE_SIGN (op1
.type ());
1433 gcc_checking_assert (sign
== TYPE_SIGN (op2
.type ()));
1435 if (wi::gt_p (op1
.lower_bound (), op2
.upper_bound (), sign
))
1436 r
= range_true (type
);
1437 else if (!wi::gt_p (op1
.upper_bound (), op2
.lower_bound (), sign
))
1438 r
= range_false (type
);
1440 r
= range_true_and_false (type
);
1445 operator_gt::op1_range (irange
&r
, tree type
,
1446 const irange
&lhs
, const irange
&op2
,
1447 relation_trio
) const
1449 if (op2
.undefined_p ())
1452 switch (get_bool_state (r
, lhs
, type
))
1455 build_gt (r
, type
, op2
.lower_bound ());
1459 build_le (r
, type
, op2
.upper_bound ());
1469 operator_gt::op2_range (irange
&r
, tree type
,
1472 relation_trio
) const
1474 if (op1
.undefined_p ())
1477 switch (get_bool_state (r
, lhs
, type
))
1480 build_lt (r
, type
, op1
.upper_bound ());
1484 build_ge (r
, type
, op1
.lower_bound ());
1495 operator_ge::update_bitmask (irange
&r
, const irange
&lh
,
1496 const irange
&rh
) const
1498 update_known_bitmask (r
, GE_EXPR
, lh
, rh
);
1501 // Check if the LHS range indicates a relation between OP1 and OP2.
1504 operator_ge::op1_op2_relation (const irange
&lhs
, const irange
&,
1505 const irange
&) const
1507 if (lhs
.undefined_p ())
1508 return VREL_UNDEFINED
;
1510 // FALSE = op1 >= op2 indicates LT_EXPR.
1514 // TRUE = op1 >= op2 indicates GE_EXPR.
1515 if (!contains_zero_p (lhs
))
1517 return VREL_VARYING
;
1521 operator_ge::fold_range (irange
&r
, tree type
,
1524 relation_trio rel
) const
1526 if (relop_early_resolve (r
, type
, op1
, op2
, rel
, VREL_GE
))
1529 signop sign
= TYPE_SIGN (op1
.type ());
1530 gcc_checking_assert (sign
== TYPE_SIGN (op2
.type ()));
1532 if (wi::ge_p (op1
.lower_bound (), op2
.upper_bound (), sign
))
1533 r
= range_true (type
);
1534 else if (!wi::ge_p (op1
.upper_bound (), op2
.lower_bound (), sign
))
1535 r
= range_false (type
);
1537 r
= range_true_and_false (type
);
1542 operator_ge::op1_range (irange
&r
, tree type
,
1545 relation_trio
) const
1547 if (op2
.undefined_p ())
1550 switch (get_bool_state (r
, lhs
, type
))
1553 build_ge (r
, type
, op2
.lower_bound ());
1557 build_lt (r
, type
, op2
.upper_bound ());
1567 operator_ge::op2_range (irange
&r
, tree type
,
1570 relation_trio
) const
1572 if (op1
.undefined_p ())
1575 switch (get_bool_state (r
, lhs
, type
))
1578 build_le (r
, type
, op1
.upper_bound ());
1582 build_gt (r
, type
, op1
.lower_bound ());
1593 operator_plus::update_bitmask (irange
&r
, const irange
&lh
,
1594 const irange
&rh
) const
1596 update_known_bitmask (r
, PLUS_EXPR
, lh
, rh
);
1599 // Check to see if the range of OP2 indicates anything about the relation
1600 // between LHS and OP1.
1603 operator_plus::lhs_op1_relation (const irange
&lhs
,
1606 relation_kind
) const
1608 if (lhs
.undefined_p () || op1
.undefined_p () || op2
.undefined_p ())
1609 return VREL_VARYING
;
1611 tree type
= lhs
.type ();
1612 unsigned prec
= TYPE_PRECISION (type
);
1613 wi::overflow_type ovf1
, ovf2
;
1614 signop sign
= TYPE_SIGN (type
);
1616 // LHS = OP1 + 0 indicates LHS == OP1.
1620 if (TYPE_OVERFLOW_WRAPS (type
))
1622 wi::add (op1
.lower_bound (), op2
.lower_bound (), sign
, &ovf1
);
1623 wi::add (op1
.upper_bound (), op2
.upper_bound (), sign
, &ovf2
);
1626 ovf1
= ovf2
= wi::OVF_NONE
;
1628 // Never wrapping additions.
1631 // Positive op2 means lhs > op1.
1632 if (wi::gt_p (op2
.lower_bound (), wi::zero (prec
), sign
))
1634 if (wi::ge_p (op2
.lower_bound (), wi::zero (prec
), sign
))
1637 // Negative op2 means lhs < op1.
1638 if (wi::lt_p (op2
.upper_bound (), wi::zero (prec
), sign
))
1640 if (wi::le_p (op2
.upper_bound (), wi::zero (prec
), sign
))
1643 // Always wrapping additions.
1644 else if (ovf1
&& ovf1
== ovf2
)
1646 // Positive op2 means lhs < op1.
1647 if (wi::gt_p (op2
.lower_bound (), wi::zero (prec
), sign
))
1649 if (wi::ge_p (op2
.lower_bound (), wi::zero (prec
), sign
))
1652 // Negative op2 means lhs > op1.
1653 if (wi::lt_p (op2
.upper_bound (), wi::zero (prec
), sign
))
1655 if (wi::le_p (op2
.upper_bound (), wi::zero (prec
), sign
))
1659 // If op2 does not contain 0, then LHS and OP1 can never be equal.
1660 if (!range_includes_zero_p (&op2
))
1663 return VREL_VARYING
;
1666 // PLUS is symmetrical, so we can simply call lhs_op1_relation with reversed
1670 operator_plus::lhs_op2_relation (const irange
&lhs
, const irange
&op1
,
1671 const irange
&op2
, relation_kind rel
) const
1673 return lhs_op1_relation (lhs
, op2
, op1
, rel
);
1677 operator_plus::wi_fold (irange
&r
, tree type
,
1678 const wide_int
&lh_lb
, const wide_int
&lh_ub
,
1679 const wide_int
&rh_lb
, const wide_int
&rh_ub
) const
1681 wi::overflow_type ov_lb
, ov_ub
;
1682 signop s
= TYPE_SIGN (type
);
1683 wide_int new_lb
= wi::add (lh_lb
, rh_lb
, s
, &ov_lb
);
1684 wide_int new_ub
= wi::add (lh_ub
, rh_ub
, s
, &ov_ub
);
1685 value_range_with_overflow (r
, type
, new_lb
, new_ub
, ov_lb
, ov_ub
);
1688 // Given addition or subtraction, determine the possible NORMAL ranges and
1689 // OVERFLOW ranges given an OFFSET range. ADD_P is true for addition.
1690 // Return the relation that exists between the LHS and OP1 in order for the
1691 // NORMAL range to apply.
1692 // a return value of VREL_VARYING means no ranges were applicable.
1694 static relation_kind
1695 plus_minus_ranges (irange
&r_ov
, irange
&r_normal
, const irange
&offset
,
1698 relation_kind kind
= VREL_VARYING
;
1699 // For now, only deal with constant adds. This could be extended to ranges
1700 // when someone is so motivated.
1701 if (!offset
.singleton_p () || offset
.zero_p ())
1704 // Always work with a positive offset. ie a+ -2 -> a-2 and a- -2 > a+2
1705 wide_int off
= offset
.lower_bound ();
1706 if (wi::neg_p (off
, SIGNED
))
1709 off
= wi::neg (off
);
1712 wi::overflow_type ov
;
1713 tree type
= offset
.type ();
1714 unsigned prec
= TYPE_PRECISION (type
);
1717 // calculate the normal range and relation for the operation.
1721 lb
= wi::zero (prec
);
1722 ub
= wi::sub (irange_val_max (type
), off
, UNSIGNED
, &ov
);
1729 ub
= irange_val_max (type
);
1732 int_range
<2> normal_range (type
, lb
, ub
);
1733 int_range
<2> ov_range (type
, lb
, ub
, VR_ANTI_RANGE
);
1736 r_normal
= normal_range
;
1740 // Once op1 has been calculated by operator_plus or operator_minus, check
1741 // to see if the relation passed causes any part of the calculation to
1742 // be not possible. ie
1743 // a_2 = b_3 + 1 with a_2 < b_3 can refine the range of b_3 to [INF, INF]
1744 // and that further refines a_2 to [0, 0].
1745 // R is the value of op1, OP2 is the offset being added/subtracted, REL is the
1746 // relation between LHS relation OP1 and ADD_P is true for PLUS, false for
1747 // MINUS. IF any adjustment can be made, R will reflect it.
1750 adjust_op1_for_overflow (irange
&r
, const irange
&op2
, relation_kind rel
,
1753 if (r
.undefined_p ())
1755 tree type
= r
.type ();
1756 // Check for unsigned overflow and calculate the overflow part.
1757 signop s
= TYPE_SIGN (type
);
1758 if (!TYPE_OVERFLOW_WRAPS (type
) || s
== SIGNED
)
1761 // Only work with <, <=, >, >= relations.
1762 if (!relation_lt_le_gt_ge_p (rel
))
1765 // Get the ranges for this offset.
1766 int_range_max normal
, overflow
;
1767 relation_kind k
= plus_minus_ranges (overflow
, normal
, op2
, add_p
);
1769 // VREL_VARYING means there are no adjustments.
1770 if (k
== VREL_VARYING
)
1773 // If the relations match use the normal range, otherwise use overflow range.
1774 if (relation_intersect (k
, rel
) == k
)
1775 r
.intersect (normal
);
1777 r
.intersect (overflow
);
1782 operator_plus::op1_range (irange
&r
, tree type
,
1785 relation_trio trio
) const
1787 if (lhs
.undefined_p ())
1789 // Start with the default operation.
1790 range_op_handler
minus (MINUS_EXPR
);
1793 bool res
= minus
.fold_range (r
, type
, lhs
, op2
);
1794 relation_kind rel
= trio
.lhs_op1 ();
1795 // Check for a relation refinement.
1797 adjust_op1_for_overflow (r
, op2
, rel
, true /* PLUS_EXPR */);
1802 operator_plus::op2_range (irange
&r
, tree type
,
1805 relation_trio rel
) const
1807 return op1_range (r
, type
, lhs
, op1
, rel
.swap_op1_op2 ());
1810 class operator_widen_plus_signed
: public range_operator
1813 virtual void wi_fold (irange
&r
, tree type
,
1814 const wide_int
&lh_lb
,
1815 const wide_int
&lh_ub
,
1816 const wide_int
&rh_lb
,
1817 const wide_int
&rh_ub
) const;
1818 } op_widen_plus_signed
;
1821 operator_widen_plus_signed::wi_fold (irange
&r
, tree type
,
1822 const wide_int
&lh_lb
,
1823 const wide_int
&lh_ub
,
1824 const wide_int
&rh_lb
,
1825 const wide_int
&rh_ub
) const
1827 wi::overflow_type ov_lb
, ov_ub
;
1828 signop s
= TYPE_SIGN (type
);
1831 = wide_int::from (lh_lb
, wi::get_precision (lh_lb
) * 2, SIGNED
);
1833 = wide_int::from (lh_ub
, wi::get_precision (lh_ub
) * 2, SIGNED
);
1834 wide_int rh_wlb
= wide_int::from (rh_lb
, wi::get_precision (rh_lb
) * 2, s
);
1835 wide_int rh_wub
= wide_int::from (rh_ub
, wi::get_precision (rh_ub
) * 2, s
);
1837 wide_int new_lb
= wi::add (lh_wlb
, rh_wlb
, s
, &ov_lb
);
1838 wide_int new_ub
= wi::add (lh_wub
, rh_wub
, s
, &ov_ub
);
1840 r
= int_range
<2> (type
, new_lb
, new_ub
);
1843 class operator_widen_plus_unsigned
: public range_operator
1846 virtual void wi_fold (irange
&r
, tree type
,
1847 const wide_int
&lh_lb
,
1848 const wide_int
&lh_ub
,
1849 const wide_int
&rh_lb
,
1850 const wide_int
&rh_ub
) const;
1851 } op_widen_plus_unsigned
;
1854 operator_widen_plus_unsigned::wi_fold (irange
&r
, tree type
,
1855 const wide_int
&lh_lb
,
1856 const wide_int
&lh_ub
,
1857 const wide_int
&rh_lb
,
1858 const wide_int
&rh_ub
) const
1860 wi::overflow_type ov_lb
, ov_ub
;
1861 signop s
= TYPE_SIGN (type
);
1864 = wide_int::from (lh_lb
, wi::get_precision (lh_lb
) * 2, UNSIGNED
);
1866 = wide_int::from (lh_ub
, wi::get_precision (lh_ub
) * 2, UNSIGNED
);
1867 wide_int rh_wlb
= wide_int::from (rh_lb
, wi::get_precision (rh_lb
) * 2, s
);
1868 wide_int rh_wub
= wide_int::from (rh_ub
, wi::get_precision (rh_ub
) * 2, s
);
1870 wide_int new_lb
= wi::add (lh_wlb
, rh_wlb
, s
, &ov_lb
);
1871 wide_int new_ub
= wi::add (lh_wub
, rh_wub
, s
, &ov_ub
);
1873 r
= int_range
<2> (type
, new_lb
, new_ub
);
1877 operator_minus::update_bitmask (irange
&r
, const irange
&lh
,
1878 const irange
&rh
) const
1880 update_known_bitmask (r
, MINUS_EXPR
, lh
, rh
);
1884 operator_minus::wi_fold (irange
&r
, tree type
,
1885 const wide_int
&lh_lb
, const wide_int
&lh_ub
,
1886 const wide_int
&rh_lb
, const wide_int
&rh_ub
) const
1888 wi::overflow_type ov_lb
, ov_ub
;
1889 signop s
= TYPE_SIGN (type
);
1890 wide_int new_lb
= wi::sub (lh_lb
, rh_ub
, s
, &ov_lb
);
1891 wide_int new_ub
= wi::sub (lh_ub
, rh_lb
, s
, &ov_ub
);
1892 value_range_with_overflow (r
, type
, new_lb
, new_ub
, ov_lb
, ov_ub
);
1896 // Return the relation between LHS and OP1 based on the relation between
1900 operator_minus::lhs_op1_relation (const irange
&, const irange
&op1
,
1901 const irange
&, relation_kind rel
) const
1903 if (!op1
.undefined_p () && TYPE_SIGN (op1
.type ()) == UNSIGNED
)
1912 return VREL_VARYING
;
1915 // Check to see if the relation REL between OP1 and OP2 has any effect on the
1916 // LHS of the expression. If so, apply it to LHS_RANGE. This is a helper
1917 // function for both MINUS_EXPR and POINTER_DIFF_EXPR.
1920 minus_op1_op2_relation_effect (irange
&lhs_range
, tree type
,
1921 const irange
&op1_range ATTRIBUTE_UNUSED
,
1922 const irange
&op2_range ATTRIBUTE_UNUSED
,
1925 if (rel
== VREL_VARYING
)
1928 int_range
<2> rel_range
;
1929 unsigned prec
= TYPE_PRECISION (type
);
1930 signop sgn
= TYPE_SIGN (type
);
1932 // == and != produce [0,0] and ~[0,0] regardless of wrapping.
1934 rel_range
= int_range
<2> (type
, wi::zero (prec
), wi::zero (prec
));
1935 else if (rel
== VREL_NE
)
1936 rel_range
= int_range
<2> (type
, wi::zero (prec
), wi::zero (prec
),
1938 else if (TYPE_OVERFLOW_WRAPS (type
))
1942 // For wrapping signed values and unsigned, if op1 > op2 or
1943 // op1 < op2, then op1 - op2 can be restricted to ~[0, 0].
1946 rel_range
= int_range
<2> (type
, wi::zero (prec
), wi::zero (prec
),
1957 // op1 > op2, op1 - op2 can be restricted to [1, +INF]
1959 rel_range
= int_range
<2> (type
, wi::one (prec
),
1960 wi::max_value (prec
, sgn
));
1962 // op1 >= op2, op1 - op2 can be restricted to [0, +INF]
1964 rel_range
= int_range
<2> (type
, wi::zero (prec
),
1965 wi::max_value (prec
, sgn
));
1967 // op1 < op2, op1 - op2 can be restricted to [-INF, -1]
1969 rel_range
= int_range
<2> (type
, wi::min_value (prec
, sgn
),
1970 wi::minus_one (prec
));
1972 // op1 <= op2, op1 - op2 can be restricted to [-INF, 0]
1974 rel_range
= int_range
<2> (type
, wi::min_value (prec
, sgn
),
1981 lhs_range
.intersect (rel_range
);
1986 operator_minus::op1_op2_relation_effect (irange
&lhs_range
, tree type
,
1987 const irange
&op1_range
,
1988 const irange
&op2_range
,
1989 relation_kind rel
) const
1991 return minus_op1_op2_relation_effect (lhs_range
, type
, op1_range
, op2_range
,
1996 operator_minus::op1_range (irange
&r
, tree type
,
1999 relation_trio trio
) const
2001 if (lhs
.undefined_p ())
2003 // Start with the default operation.
2004 range_op_handler
minus (PLUS_EXPR
);
2007 bool res
= minus
.fold_range (r
, type
, lhs
, op2
);
2008 relation_kind rel
= trio
.lhs_op1 ();
2010 adjust_op1_for_overflow (r
, op2
, rel
, false /* PLUS_EXPR */);
2016 operator_minus::op2_range (irange
&r
, tree type
,
2019 relation_trio
) const
2021 if (lhs
.undefined_p ())
2023 return fold_range (r
, type
, op1
, lhs
);
2027 operator_min::update_bitmask (irange
&r
, const irange
&lh
,
2028 const irange
&rh
) const
2030 update_known_bitmask (r
, MIN_EXPR
, lh
, rh
);
2034 operator_min::wi_fold (irange
&r
, tree type
,
2035 const wide_int
&lh_lb
, const wide_int
&lh_ub
,
2036 const wide_int
&rh_lb
, const wide_int
&rh_ub
) const
2038 signop s
= TYPE_SIGN (type
);
2039 wide_int new_lb
= wi::min (lh_lb
, rh_lb
, s
);
2040 wide_int new_ub
= wi::min (lh_ub
, rh_ub
, s
);
2041 value_range_with_overflow (r
, type
, new_lb
, new_ub
);
2046 operator_max::update_bitmask (irange
&r
, const irange
&lh
,
2047 const irange
&rh
) const
2049 update_known_bitmask (r
, MAX_EXPR
, lh
, rh
);
2053 operator_max::wi_fold (irange
&r
, tree type
,
2054 const wide_int
&lh_lb
, const wide_int
&lh_ub
,
2055 const wide_int
&rh_lb
, const wide_int
&rh_ub
) const
2057 signop s
= TYPE_SIGN (type
);
2058 wide_int new_lb
= wi::max (lh_lb
, rh_lb
, s
);
2059 wide_int new_ub
= wi::max (lh_ub
, rh_ub
, s
);
2060 value_range_with_overflow (r
, type
, new_lb
, new_ub
);
2064 // Calculate the cross product of two sets of ranges and return it.
2066 // Multiplications, divisions and shifts are a bit tricky to handle,
2067 // depending on the mix of signs we have in the two ranges, we need to
2068 // operate on different values to get the minimum and maximum values
2069 // for the new range. One approach is to figure out all the
2070 // variations of range combinations and do the operations.
2072 // However, this involves several calls to compare_values and it is
2073 // pretty convoluted. It's simpler to do the 4 operations (MIN0 OP
2074 // MIN1, MIN0 OP MAX1, MAX0 OP MIN1 and MAX0 OP MAX0 OP MAX1) and then
2075 // figure the smallest and largest values to form the new range.
2078 cross_product_operator::wi_cross_product (irange
&r
, tree type
,
2079 const wide_int
&lh_lb
,
2080 const wide_int
&lh_ub
,
2081 const wide_int
&rh_lb
,
2082 const wide_int
&rh_ub
) const
2084 wide_int cp1
, cp2
, cp3
, cp4
;
2085 // Default to varying.
2086 r
.set_varying (type
);
2088 // Compute the 4 cross operations, bailing if we get an overflow we
2090 if (wi_op_overflows (cp1
, type
, lh_lb
, rh_lb
))
2092 if (wi::eq_p (lh_lb
, lh_ub
))
2094 else if (wi_op_overflows (cp3
, type
, lh_ub
, rh_lb
))
2096 if (wi::eq_p (rh_lb
, rh_ub
))
2098 else if (wi_op_overflows (cp2
, type
, lh_lb
, rh_ub
))
2100 if (wi::eq_p (lh_lb
, lh_ub
))
2102 else if (wi_op_overflows (cp4
, type
, lh_ub
, rh_ub
))
2106 signop sign
= TYPE_SIGN (type
);
2107 if (wi::gt_p (cp1
, cp2
, sign
))
2108 std::swap (cp1
, cp2
);
2109 if (wi::gt_p (cp3
, cp4
, sign
))
2110 std::swap (cp3
, cp4
);
2112 // Choose min and max from the ordered pairs.
2113 wide_int res_lb
= wi::min (cp1
, cp3
, sign
);
2114 wide_int res_ub
= wi::max (cp2
, cp4
, sign
);
2115 value_range_with_overflow (r
, type
, res_lb
, res_ub
);
2120 operator_mult::update_bitmask (irange
&r
, const irange
&lh
,
2121 const irange
&rh
) const
2123 update_known_bitmask (r
, MULT_EXPR
, lh
, rh
);
2127 operator_mult::op1_range (irange
&r
, tree type
,
2128 const irange
&lhs
, const irange
&op2
,
2129 relation_trio
) const
2131 if (lhs
.undefined_p ())
2134 // We can't solve 0 = OP1 * N by dividing by N with a wrapping type.
2135 // For example: For 0 = OP1 * 2, OP1 could be 0, or MAXINT, whereas
2136 // for 4 = OP1 * 2, OP1 could be 2 or 130 (unsigned 8-bit)
2137 if (TYPE_OVERFLOW_WRAPS (type
))
2141 if (op2
.singleton_p (offset
) && offset
!= 0)
2142 return range_op_handler (TRUNC_DIV_EXPR
).fold_range (r
, type
, lhs
, op2
);
2147 operator_mult::op2_range (irange
&r
, tree type
,
2148 const irange
&lhs
, const irange
&op1
,
2149 relation_trio rel
) const
2151 return operator_mult::op1_range (r
, type
, lhs
, op1
, rel
.swap_op1_op2 ());
2155 operator_mult::wi_op_overflows (wide_int
&res
, tree type
,
2156 const wide_int
&w0
, const wide_int
&w1
) const
2158 wi::overflow_type overflow
= wi::OVF_NONE
;
2159 signop sign
= TYPE_SIGN (type
);
2160 res
= wi::mul (w0
, w1
, sign
, &overflow
);
2161 if (overflow
&& TYPE_OVERFLOW_UNDEFINED (type
))
2163 // For multiplication, the sign of the overflow is given
2164 // by the comparison of the signs of the operands.
2165 if (sign
== UNSIGNED
|| w0
.sign_mask () == w1
.sign_mask ())
2166 res
= wi::max_value (w0
.get_precision (), sign
);
2168 res
= wi::min_value (w0
.get_precision (), sign
);
2175 operator_mult::wi_fold (irange
&r
, tree type
,
2176 const wide_int
&lh_lb
, const wide_int
&lh_ub
,
2177 const wide_int
&rh_lb
, const wide_int
&rh_ub
) const
2179 if (TYPE_OVERFLOW_UNDEFINED (type
))
2181 wi_cross_product (r
, type
, lh_lb
, lh_ub
, rh_lb
, rh_ub
);
2185 // Multiply the ranges when overflow wraps. This is basically fancy
2186 // code so we don't drop to varying with an unsigned
2189 // This test requires 2*prec bits if both operands are signed and
2190 // 2*prec + 2 bits if either is not. Therefore, extend the values
2191 // using the sign of the result to PREC2. From here on out,
2192 // everything is just signed math no matter what the input types
2195 signop sign
= TYPE_SIGN (type
);
2196 unsigned prec
= TYPE_PRECISION (type
);
2197 widest2_int min0
= widest2_int::from (lh_lb
, sign
);
2198 widest2_int max0
= widest2_int::from (lh_ub
, sign
);
2199 widest2_int min1
= widest2_int::from (rh_lb
, sign
);
2200 widest2_int max1
= widest2_int::from (rh_ub
, sign
);
2201 widest2_int sizem1
= wi::mask
<widest2_int
> (prec
, false);
2202 widest2_int size
= sizem1
+ 1;
2204 // Canonicalize the intervals.
2205 if (sign
== UNSIGNED
)
2207 if (wi::ltu_p (size
, min0
+ max0
))
2212 if (wi::ltu_p (size
, min1
+ max1
))
2219 // Sort the 4 products so that min is in prod0 and max is in
2221 widest2_int prod0
= min0
* min1
;
2222 widest2_int prod1
= min0
* max1
;
2223 widest2_int prod2
= max0
* min1
;
2224 widest2_int prod3
= max0
* max1
;
2226 // min0min1 > max0max1
2228 std::swap (prod0
, prod3
);
2230 // min0max1 > max0min1
2232 std::swap (prod1
, prod2
);
2235 std::swap (prod0
, prod1
);
2238 std::swap (prod2
, prod3
);
2241 prod2
= prod3
- prod0
;
2242 if (wi::geu_p (prod2
, sizem1
))
2244 // Multiplying by X, where X is a power of 2 is [0,0][X,+INF].
2245 if (TYPE_UNSIGNED (type
) && rh_lb
== rh_ub
2246 && wi::exact_log2 (rh_lb
) != -1 && prec
> 1)
2248 r
.set (type
, rh_lb
, wi::max_value (prec
, sign
));
2250 zero
.set_zero (type
);
2254 // The range covers all values.
2255 r
.set_varying (type
);
2259 wide_int new_lb
= wide_int::from (prod0
, prec
, sign
);
2260 wide_int new_ub
= wide_int::from (prod3
, prec
, sign
);
2261 create_possibly_reversed_range (r
, type
, new_lb
, new_ub
);
2265 class operator_widen_mult_signed
: public range_operator
2268 virtual void wi_fold (irange
&r
, tree type
,
2269 const wide_int
&lh_lb
,
2270 const wide_int
&lh_ub
,
2271 const wide_int
&rh_lb
,
2272 const wide_int
&rh_ub
)
2274 } op_widen_mult_signed
;
2277 operator_widen_mult_signed::wi_fold (irange
&r
, tree type
,
2278 const wide_int
&lh_lb
,
2279 const wide_int
&lh_ub
,
2280 const wide_int
&rh_lb
,
2281 const wide_int
&rh_ub
) const
2283 signop s
= TYPE_SIGN (type
);
2285 wide_int lh_wlb
= wide_int::from (lh_lb
, wi::get_precision (lh_lb
) * 2, SIGNED
);
2286 wide_int lh_wub
= wide_int::from (lh_ub
, wi::get_precision (lh_ub
) * 2, SIGNED
);
2287 wide_int rh_wlb
= wide_int::from (rh_lb
, wi::get_precision (rh_lb
) * 2, s
);
2288 wide_int rh_wub
= wide_int::from (rh_ub
, wi::get_precision (rh_ub
) * 2, s
);
2290 /* We don't expect a widening multiplication to be able to overflow but range
2291 calculations for multiplications are complicated. After widening the
2292 operands lets call the base class. */
2293 return op_mult
.wi_fold (r
, type
, lh_wlb
, lh_wub
, rh_wlb
, rh_wub
);
2297 class operator_widen_mult_unsigned
: public range_operator
2300 virtual void wi_fold (irange
&r
, tree type
,
2301 const wide_int
&lh_lb
,
2302 const wide_int
&lh_ub
,
2303 const wide_int
&rh_lb
,
2304 const wide_int
&rh_ub
)
2306 } op_widen_mult_unsigned
;
2309 operator_widen_mult_unsigned::wi_fold (irange
&r
, tree type
,
2310 const wide_int
&lh_lb
,
2311 const wide_int
&lh_ub
,
2312 const wide_int
&rh_lb
,
2313 const wide_int
&rh_ub
) const
2315 signop s
= TYPE_SIGN (type
);
2317 wide_int lh_wlb
= wide_int::from (lh_lb
, wi::get_precision (lh_lb
) * 2, UNSIGNED
);
2318 wide_int lh_wub
= wide_int::from (lh_ub
, wi::get_precision (lh_ub
) * 2, UNSIGNED
);
2319 wide_int rh_wlb
= wide_int::from (rh_lb
, wi::get_precision (rh_lb
) * 2, s
);
2320 wide_int rh_wub
= wide_int::from (rh_ub
, wi::get_precision (rh_ub
) * 2, s
);
2322 /* We don't expect a widening multiplication to be able to overflow but range
2323 calculations for multiplications are complicated. After widening the
2324 operands lets call the base class. */
2325 return op_mult
.wi_fold (r
, type
, lh_wlb
, lh_wub
, rh_wlb
, rh_wub
);
2328 class operator_div
: public cross_product_operator
2331 operator_div (tree_code div_kind
) { m_code
= div_kind
; }
2332 virtual void wi_fold (irange
&r
, tree type
,
2333 const wide_int
&lh_lb
,
2334 const wide_int
&lh_ub
,
2335 const wide_int
&rh_lb
,
2336 const wide_int
&rh_ub
) const final override
;
2337 virtual bool wi_op_overflows (wide_int
&res
, tree type
,
2338 const wide_int
&, const wide_int
&)
2339 const final override
;
2340 void update_bitmask (irange
&r
, const irange
&lh
, const irange
&rh
) const
2341 { update_known_bitmask (r
, m_code
, lh
, rh
); }
2346 static operator_div
op_trunc_div (TRUNC_DIV_EXPR
);
2347 static operator_div
op_floor_div (FLOOR_DIV_EXPR
);
2348 static operator_div
op_round_div (ROUND_DIV_EXPR
);
2349 static operator_div
op_ceil_div (CEIL_DIV_EXPR
);
2352 operator_div::wi_op_overflows (wide_int
&res
, tree type
,
2353 const wide_int
&w0
, const wide_int
&w1
) const
2358 wi::overflow_type overflow
= wi::OVF_NONE
;
2359 signop sign
= TYPE_SIGN (type
);
2363 case EXACT_DIV_EXPR
:
2364 case TRUNC_DIV_EXPR
:
2365 res
= wi::div_trunc (w0
, w1
, sign
, &overflow
);
2367 case FLOOR_DIV_EXPR
:
2368 res
= wi::div_floor (w0
, w1
, sign
, &overflow
);
2370 case ROUND_DIV_EXPR
:
2371 res
= wi::div_round (w0
, w1
, sign
, &overflow
);
2374 res
= wi::div_ceil (w0
, w1
, sign
, &overflow
);
2380 if (overflow
&& TYPE_OVERFLOW_UNDEFINED (type
))
2382 // For division, the only case is -INF / -1 = +INF.
2383 res
= wi::max_value (w0
.get_precision (), sign
);
2390 operator_div::wi_fold (irange
&r
, tree type
,
2391 const wide_int
&lh_lb
, const wide_int
&lh_ub
,
2392 const wide_int
&rh_lb
, const wide_int
&rh_ub
) const
2394 const wide_int dividend_min
= lh_lb
;
2395 const wide_int dividend_max
= lh_ub
;
2396 const wide_int divisor_min
= rh_lb
;
2397 const wide_int divisor_max
= rh_ub
;
2398 signop sign
= TYPE_SIGN (type
);
2399 unsigned prec
= TYPE_PRECISION (type
);
2400 wide_int extra_min
, extra_max
;
2402 // If we know we won't divide by zero, just do the division.
2403 if (!wi_includes_zero_p (type
, divisor_min
, divisor_max
))
2405 wi_cross_product (r
, type
, dividend_min
, dividend_max
,
2406 divisor_min
, divisor_max
);
2410 // If we're definitely dividing by zero, there's nothing to do.
2411 if (wi_zero_p (type
, divisor_min
, divisor_max
))
2417 // Perform the division in 2 parts, [LB, -1] and [1, UB], which will
2418 // skip any division by zero.
2420 // First divide by the negative numbers, if any.
2421 if (wi::neg_p (divisor_min
, sign
))
2422 wi_cross_product (r
, type
, dividend_min
, dividend_max
,
2423 divisor_min
, wi::minus_one (prec
));
2427 // Then divide by the non-zero positive numbers, if any.
2428 if (wi::gt_p (divisor_max
, wi::zero (prec
), sign
))
2431 wi_cross_product (tmp
, type
, dividend_min
, dividend_max
,
2432 wi::one (prec
), divisor_max
);
2435 // We shouldn't still have undefined here.
2436 gcc_checking_assert (!r
.undefined_p ());
2440 class operator_exact_divide
: public operator_div
2442 using range_operator::op1_range
;
2444 operator_exact_divide () : operator_div (EXACT_DIV_EXPR
) { }
2445 virtual bool op1_range (irange
&r
, tree type
,
2448 relation_trio
) const;
2453 operator_exact_divide::op1_range (irange
&r
, tree type
,
2456 relation_trio
) const
2458 if (lhs
.undefined_p ())
2461 // [2, 4] = op1 / [3,3] since its exact divide, no need to worry about
2462 // remainders in the endpoints, so op1 = [2,4] * [3,3] = [6,12].
2463 // We wont bother trying to enumerate all the in between stuff :-P
2464 // TRUE accuracy is [6,6][9,9][12,12]. This is unlikely to matter most of
2465 // the time however.
2466 // If op2 is a multiple of 2, we would be able to set some non-zero bits.
2467 if (op2
.singleton_p (offset
) && offset
!= 0)
2468 return range_op_handler (MULT_EXPR
).fold_range (r
, type
, lhs
, op2
);
2473 class operator_lshift
: public cross_product_operator
2475 using range_operator::fold_range
;
2476 using range_operator::op1_range
;
2478 virtual bool op1_range (irange
&r
, tree type
, const irange
&lhs
,
2479 const irange
&op2
, relation_trio rel
= TRIO_VARYING
)
2480 const final override
;
2481 virtual bool fold_range (irange
&r
, tree type
, const irange
&op1
,
2482 const irange
&op2
, relation_trio rel
= TRIO_VARYING
)
2483 const final override
;
2485 virtual void wi_fold (irange
&r
, tree type
,
2486 const wide_int
&lh_lb
, const wide_int
&lh_ub
,
2487 const wide_int
&rh_lb
,
2488 const wide_int
&rh_ub
) const final override
;
2489 virtual bool wi_op_overflows (wide_int
&res
,
2492 const wide_int
&) const final override
;
2493 void update_bitmask (irange
&r
, const irange
&lh
,
2494 const irange
&rh
) const final override
2495 { update_known_bitmask (r
, LSHIFT_EXPR
, lh
, rh
); }
2496 // Check compatibility of LHS and op1.
2497 bool operand_check_p (tree t1
, tree t2
, tree
) const final override
2498 { return range_compatible_p (t1
, t2
); }
2501 class operator_rshift
: public cross_product_operator
2503 using range_operator::fold_range
;
2504 using range_operator::op1_range
;
2505 using range_operator::lhs_op1_relation
;
2507 virtual bool fold_range (irange
&r
, tree type
, const irange
&op1
,
2508 const irange
&op2
, relation_trio rel
= TRIO_VARYING
)
2509 const final override
;
2510 virtual void wi_fold (irange
&r
, tree type
,
2511 const wide_int
&lh_lb
,
2512 const wide_int
&lh_ub
,
2513 const wide_int
&rh_lb
,
2514 const wide_int
&rh_ub
) const final override
;
2515 virtual bool wi_op_overflows (wide_int
&res
,
2518 const wide_int
&w1
) const final override
;
2519 virtual bool op1_range (irange
&, tree type
, const irange
&lhs
,
2520 const irange
&op2
, relation_trio rel
= TRIO_VARYING
)
2521 const final override
;
2522 virtual relation_kind
lhs_op1_relation (const irange
&lhs
, const irange
&op1
,
2523 const irange
&op2
, relation_kind rel
)
2524 const final override
;
2525 void update_bitmask (irange
&r
, const irange
&lh
,
2526 const irange
&rh
) const final override
2527 { update_known_bitmask (r
, RSHIFT_EXPR
, lh
, rh
); }
2528 // Check compatibility of LHS and op1.
2529 bool operand_check_p (tree t1
, tree t2
, tree
) const final override
2530 { return range_compatible_p (t1
, t2
); }
2535 operator_rshift::lhs_op1_relation (const irange
&lhs ATTRIBUTE_UNUSED
,
2538 relation_kind
) const
2540 // If both operands range are >= 0, then the LHS <= op1.
2541 if (!op1
.undefined_p () && !op2
.undefined_p ()
2542 && wi::ge_p (op1
.lower_bound (), 0, TYPE_SIGN (op1
.type ()))
2543 && wi::ge_p (op2
.lower_bound (), 0, TYPE_SIGN (op2
.type ())))
2545 return VREL_VARYING
;
2549 operator_lshift::fold_range (irange
&r
, tree type
,
2552 relation_trio rel
) const
2554 int_range_max shift_range
;
2555 if (!get_shift_range (shift_range
, type
, op2
))
2557 if (op2
.undefined_p ())
2564 // Transform left shifts by constants into multiplies.
2565 if (shift_range
.singleton_p ())
2567 unsigned shift
= shift_range
.lower_bound ().to_uhwi ();
2568 wide_int tmp
= wi::set_bit_in_zero (shift
, TYPE_PRECISION (type
));
2569 int_range
<1> mult (type
, tmp
, tmp
);
2571 // Force wrapping multiplication.
2572 bool saved_flag_wrapv
= flag_wrapv
;
2573 bool saved_flag_wrapv_pointer
= flag_wrapv_pointer
;
2575 flag_wrapv_pointer
= 1;
2576 bool b
= op_mult
.fold_range (r
, type
, op1
, mult
);
2577 flag_wrapv
= saved_flag_wrapv
;
2578 flag_wrapv_pointer
= saved_flag_wrapv_pointer
;
2582 // Otherwise, invoke the generic fold routine.
2583 return range_operator::fold_range (r
, type
, op1
, shift_range
, rel
);
2587 operator_lshift::wi_fold (irange
&r
, tree type
,
2588 const wide_int
&lh_lb
, const wide_int
&lh_ub
,
2589 const wide_int
&rh_lb
, const wide_int
&rh_ub
) const
2591 signop sign
= TYPE_SIGN (type
);
2592 unsigned prec
= TYPE_PRECISION (type
);
2593 int overflow_pos
= sign
== SIGNED
? prec
- 1 : prec
;
2594 int bound_shift
= overflow_pos
- rh_ub
.to_shwi ();
2595 // If bound_shift == HOST_BITS_PER_WIDE_INT, the llshift can
2596 // overflow. However, for that to happen, rh.max needs to be zero,
2597 // which means rh is a singleton range of zero, which means we simply return
2598 // [lh_lb, lh_ub] as the range.
2599 if (wi::eq_p (rh_ub
, rh_lb
) && wi::eq_p (rh_ub
, 0))
2601 r
= int_range
<2> (type
, lh_lb
, lh_ub
);
2605 wide_int bound
= wi::set_bit_in_zero (bound_shift
, prec
);
2606 wide_int complement
= ~(bound
- 1);
2607 wide_int low_bound
, high_bound
;
2608 bool in_bounds
= false;
2610 if (sign
== UNSIGNED
)
2613 high_bound
= complement
;
2614 if (wi::ltu_p (lh_ub
, low_bound
))
2616 // [5, 6] << [1, 2] == [10, 24].
2617 // We're shifting out only zeroes, the value increases
2621 else if (wi::ltu_p (high_bound
, lh_lb
))
2623 // [0xffffff00, 0xffffffff] << [1, 2]
2624 // == [0xfffffc00, 0xfffffffe].
2625 // We're shifting out only ones, the value decreases
2632 // [-1, 1] << [1, 2] == [-4, 4]
2633 low_bound
= complement
;
2635 if (wi::lts_p (lh_ub
, high_bound
)
2636 && wi::lts_p (low_bound
, lh_lb
))
2638 // For non-negative numbers, we're shifting out only zeroes,
2639 // the value increases monotonically. For negative numbers,
2640 // we're shifting out only ones, the value decreases
2647 wi_cross_product (r
, type
, lh_lb
, lh_ub
, rh_lb
, rh_ub
);
2649 r
.set_varying (type
);
2653 operator_lshift::wi_op_overflows (wide_int
&res
, tree type
,
2654 const wide_int
&w0
, const wide_int
&w1
) const
2656 signop sign
= TYPE_SIGN (type
);
2659 // It's unclear from the C standard whether shifts can overflow.
2660 // The following code ignores overflow; perhaps a C standard
2661 // interpretation ruling is needed.
2662 res
= wi::rshift (w0
, -w1
, sign
);
2665 res
= wi::lshift (w0
, w1
);
2670 operator_lshift::op1_range (irange
&r
,
2674 relation_trio
) const
2676 if (lhs
.undefined_p ())
2679 if (!contains_zero_p (lhs
))
2680 r
.set_nonzero (type
);
2682 r
.set_varying (type
);
2685 if (op2
.singleton_p (shift
))
2687 if (wi::lt_p (shift
, 0, SIGNED
))
2689 if (wi::ge_p (shift
, wi::uhwi (TYPE_PRECISION (type
),
2690 TYPE_PRECISION (op2
.type ())),
2699 // Work completely in unsigned mode to start.
2701 int_range_max tmp_range
;
2702 if (TYPE_SIGN (type
) == SIGNED
)
2704 int_range_max tmp
= lhs
;
2705 utype
= unsigned_type_for (type
);
2706 range_cast (tmp
, utype
);
2707 op_rshift
.fold_range (tmp_range
, utype
, tmp
, op2
);
2710 op_rshift
.fold_range (tmp_range
, utype
, lhs
, op2
);
2712 // Start with ranges which can produce the LHS by right shifting the
2713 // result by the shift amount.
2714 // ie [0x08, 0xF0] = op1 << 2 will start with
2715 // [00001000, 11110000] = op1 << 2
2716 // [0x02, 0x4C] aka [00000010, 00111100]
2718 // Then create a range from the LB with the least significant upper bit
2719 // set, to the upper bound with all the bits set.
2720 // This would be [0x42, 0xFC] aka [01000010, 11111100].
2722 // Ideally we do this for each subrange, but just lump them all for now.
2723 unsigned low_bits
= TYPE_PRECISION (utype
) - shift
.to_uhwi ();
2724 wide_int up_mask
= wi::mask (low_bits
, true, TYPE_PRECISION (utype
));
2725 wide_int new_ub
= wi::bit_or (up_mask
, tmp_range
.upper_bound ());
2726 wide_int new_lb
= wi::set_bit (tmp_range
.lower_bound (), low_bits
);
2727 int_range
<2> fill_range (utype
, new_lb
, new_ub
);
2728 tmp_range
.union_ (fill_range
);
2731 range_cast (tmp_range
, type
);
2733 r
.intersect (tmp_range
);
2737 return !r
.varying_p ();
2741 operator_rshift::op1_range (irange
&r
,
2745 relation_trio
) const
2747 if (lhs
.undefined_p ())
2750 if (op2
.singleton_p (shift
))
2752 // Ignore nonsensical shifts.
2753 unsigned prec
= TYPE_PRECISION (type
);
2754 if (wi::ge_p (shift
,
2755 wi::uhwi (prec
, TYPE_PRECISION (op2
.type ())),
2764 // Folding the original operation may discard some impossible
2765 // ranges from the LHS.
2766 int_range_max lhs_refined
;
2767 op_rshift
.fold_range (lhs_refined
, type
, int_range
<1> (type
), op2
);
2768 lhs_refined
.intersect (lhs
);
2769 if (lhs_refined
.undefined_p ())
2774 int_range_max
shift_range (op2
.type (), shift
, shift
);
2775 int_range_max lb
, ub
;
2776 op_lshift
.fold_range (lb
, type
, lhs_refined
, shift_range
);
2778 // 0000 0111 = OP1 >> 3
2780 // OP1 is anything from 0011 1000 to 0011 1111. That is, a
2781 // range from LHS<<3 plus a mask of the 3 bits we shifted on the
2782 // right hand side (0x07).
2783 wide_int mask
= wi::bit_not (wi::lshift (wi::minus_one (prec
), shift
));
2784 int_range_max
mask_range (type
,
2785 wi::zero (TYPE_PRECISION (type
)),
2787 op_plus
.fold_range (ub
, type
, lb
, mask_range
);
2790 if (!contains_zero_p (lhs_refined
))
2792 mask_range
.invert ();
2793 r
.intersect (mask_range
);
2801 operator_rshift::wi_op_overflows (wide_int
&res
,
2804 const wide_int
&w1
) const
2806 signop sign
= TYPE_SIGN (type
);
2808 res
= wi::lshift (w0
, -w1
);
2811 // It's unclear from the C standard whether shifts can overflow.
2812 // The following code ignores overflow; perhaps a C standard
2813 // interpretation ruling is needed.
2814 res
= wi::rshift (w0
, w1
, sign
);
2820 operator_rshift::fold_range (irange
&r
, tree type
,
2823 relation_trio rel
) const
2825 int_range_max shift
;
2826 if (!get_shift_range (shift
, type
, op2
))
2828 if (op2
.undefined_p ())
2835 return range_operator::fold_range (r
, type
, op1
, shift
, rel
);
2839 operator_rshift::wi_fold (irange
&r
, tree type
,
2840 const wide_int
&lh_lb
, const wide_int
&lh_ub
,
2841 const wide_int
&rh_lb
, const wide_int
&rh_ub
) const
2843 wi_cross_product (r
, type
, lh_lb
, lh_ub
, rh_lb
, rh_ub
);
2847 // Add a partial equivalence between the LHS and op1 for casts.
2850 operator_cast::lhs_op1_relation (const irange
&lhs
,
2852 const irange
&op2 ATTRIBUTE_UNUSED
,
2853 relation_kind
) const
2855 if (lhs
.undefined_p () || op1
.undefined_p ())
2856 return VREL_VARYING
;
2857 unsigned lhs_prec
= TYPE_PRECISION (lhs
.type ());
2858 unsigned op1_prec
= TYPE_PRECISION (op1
.type ());
2859 // If the result gets sign extended into a larger type check first if this
2860 // qualifies as a partial equivalence.
2861 if (TYPE_SIGN (op1
.type ()) == SIGNED
&& lhs_prec
> op1_prec
)
2863 // If the result is sign extended, and the LHS is larger than op1,
2864 // check if op1's range can be negative as the sign extension will
2865 // cause the upper bits to be 1 instead of 0, invalidating the PE.
2866 int_range
<3> negs
= range_negatives (op1
.type ());
2867 negs
.intersect (op1
);
2868 if (!negs
.undefined_p ())
2869 return VREL_VARYING
;
2872 unsigned prec
= MIN (lhs_prec
, op1_prec
);
2873 return bits_to_pe (prec
);
2876 // Return TRUE if casting from INNER to OUTER is a truncating cast.
2879 operator_cast::truncating_cast_p (const irange
&inner
,
2880 const irange
&outer
) const
2882 return TYPE_PRECISION (outer
.type ()) < TYPE_PRECISION (inner
.type ());
2885 // Return TRUE if [MIN,MAX] is inside the domain of RANGE's type.
2888 operator_cast::inside_domain_p (const wide_int
&min
,
2889 const wide_int
&max
,
2890 const irange
&range
) const
2892 wide_int domain_min
= irange_val_min (range
.type ());
2893 wide_int domain_max
= irange_val_max (range
.type ());
2894 signop domain_sign
= TYPE_SIGN (range
.type ());
2895 return (wi::le_p (min
, domain_max
, domain_sign
)
2896 && wi::le_p (max
, domain_max
, domain_sign
)
2897 && wi::ge_p (min
, domain_min
, domain_sign
)
2898 && wi::ge_p (max
, domain_min
, domain_sign
));
2902 // Helper for fold_range which work on a pair at a time.
2905 operator_cast::fold_pair (irange
&r
, unsigned index
,
2906 const irange
&inner
,
2907 const irange
&outer
) const
2909 tree inner_type
= inner
.type ();
2910 tree outer_type
= outer
.type ();
2911 signop inner_sign
= TYPE_SIGN (inner_type
);
2912 unsigned outer_prec
= TYPE_PRECISION (outer_type
);
2914 // check to see if casting from INNER to OUTER is a conversion that
2915 // fits in the resulting OUTER type.
2916 wide_int inner_lb
= inner
.lower_bound (index
);
2917 wide_int inner_ub
= inner
.upper_bound (index
);
2918 if (truncating_cast_p (inner
, outer
))
2920 // We may be able to accommodate a truncating cast if the
2921 // resulting range can be represented in the target type...
2922 if (wi::rshift (wi::sub (inner_ub
, inner_lb
),
2923 wi::uhwi (outer_prec
, TYPE_PRECISION (inner
.type ())),
2926 r
.set_varying (outer_type
);
2930 // ...but we must still verify that the final range fits in the
2931 // domain. This catches -fstrict-enum restrictions where the domain
2932 // range is smaller than what fits in the underlying type.
2933 wide_int min
= wide_int::from (inner_lb
, outer_prec
, inner_sign
);
2934 wide_int max
= wide_int::from (inner_ub
, outer_prec
, inner_sign
);
2935 if (inside_domain_p (min
, max
, outer
))
2936 create_possibly_reversed_range (r
, outer_type
, min
, max
);
2938 r
.set_varying (outer_type
);
2943 operator_cast::fold_range (irange
&r
, tree type ATTRIBUTE_UNUSED
,
2944 const irange
&inner
,
2945 const irange
&outer
,
2946 relation_trio
) const
2948 if (empty_range_varying (r
, type
, inner
, outer
))
2951 gcc_checking_assert (outer
.varying_p ());
2952 gcc_checking_assert (inner
.num_pairs () > 0);
2954 // Avoid a temporary by folding the first pair directly into the result.
2955 fold_pair (r
, 0, inner
, outer
);
2957 // Then process any additional pairs by unioning with their results.
2958 for (unsigned x
= 1; x
< inner
.num_pairs (); ++x
)
2961 fold_pair (tmp
, x
, inner
, outer
);
2967 update_bitmask (r
, inner
, outer
);
2972 operator_cast::update_bitmask (irange
&r
, const irange
&lh
,
2973 const irange
&rh
) const
2975 update_known_bitmask (r
, CONVERT_EXPR
, lh
, rh
);
2979 operator_cast::op1_range (irange
&r
, tree type
,
2982 relation_trio
) const
2984 if (lhs
.undefined_p ())
2986 tree lhs_type
= lhs
.type ();
2987 gcc_checking_assert (types_compatible_p (op2
.type(), type
));
2989 // If we are calculating a pointer, shortcut to what we really care about.
2990 if (POINTER_TYPE_P (type
))
2992 // Conversion from other pointers or a constant (including 0/NULL)
2993 // are straightforward.
2994 if (POINTER_TYPE_P (lhs
.type ())
2995 || (lhs
.singleton_p ()
2996 && TYPE_PRECISION (lhs
.type ()) >= TYPE_PRECISION (type
)))
2999 range_cast (r
, type
);
3003 // If the LHS is not a pointer nor a singleton, then it is
3004 // either VARYING or non-zero.
3005 if (!lhs
.undefined_p () && !contains_zero_p (lhs
))
3006 r
.set_nonzero (type
);
3008 r
.set_varying (type
);
3014 if (truncating_cast_p (op2
, lhs
))
3016 if (lhs
.varying_p ())
3017 r
.set_varying (type
);
3020 // We want to insert the LHS as an unsigned value since it
3021 // would not trigger the signed bit of the larger type.
3022 int_range_max converted_lhs
= lhs
;
3023 range_cast (converted_lhs
, unsigned_type_for (lhs_type
));
3024 range_cast (converted_lhs
, type
);
3025 // Start by building the positive signed outer range for the type.
3026 wide_int lim
= wi::set_bit_in_zero (TYPE_PRECISION (lhs_type
),
3027 TYPE_PRECISION (type
));
3028 create_possibly_reversed_range (r
, type
, lim
,
3029 wi::max_value (TYPE_PRECISION (type
),
3031 // For the signed part, we need to simply union the 2 ranges now.
3032 r
.union_ (converted_lhs
);
3034 // Create maximal negative number outside of LHS bits.
3035 lim
= wi::mask (TYPE_PRECISION (lhs_type
), true,
3036 TYPE_PRECISION (type
));
3037 // Add this to the unsigned LHS range(s).
3038 int_range_max
lim_range (type
, lim
, lim
);
3039 int_range_max lhs_neg
;
3040 range_op_handler (PLUS_EXPR
).fold_range (lhs_neg
, type
,
3041 converted_lhs
, lim_range
);
3042 // lhs_neg now has all the negative versions of the LHS.
3043 // Now union in all the values from SIGNED MIN (0x80000) to
3044 // lim-1 in order to fill in all the ranges with the upper
3047 // PR 97317. If the lhs has only 1 bit less precision than the rhs,
3048 // we don't need to create a range from min to lim-1
3049 // calculate neg range traps trying to create [lim, lim - 1].
3050 wide_int min_val
= wi::min_value (TYPE_PRECISION (type
), SIGNED
);
3053 int_range_max
neg (type
,
3054 wi::min_value (TYPE_PRECISION (type
),
3057 lhs_neg
.union_ (neg
);
3059 // And finally, munge the signed and unsigned portions.
3062 // And intersect with any known value passed in the extra operand.
3068 if (TYPE_PRECISION (lhs_type
) == TYPE_PRECISION (type
))
3072 // The cast is not truncating, and the range is restricted to
3073 // the range of the RHS by this assignment.
3075 // Cast the range of the RHS to the type of the LHS.
3076 fold_range (tmp
, lhs_type
, int_range
<1> (type
), int_range
<1> (lhs_type
));
3077 // Intersect this with the LHS range will produce the range,
3078 // which will be cast to the RHS type before returning.
3079 tmp
.intersect (lhs
);
3082 // Cast the calculated range to the type of the RHS.
3083 fold_range (r
, type
, tmp
, int_range
<1> (type
));
3088 class operator_logical_and
: public range_operator
3090 using range_operator::fold_range
;
3091 using range_operator::op1_range
;
3092 using range_operator::op2_range
;
3094 virtual bool fold_range (irange
&r
, tree type
,
3097 relation_trio rel
= TRIO_VARYING
) const;
3098 virtual bool op1_range (irange
&r
, tree type
,
3101 relation_trio rel
= TRIO_VARYING
) const;
3102 virtual bool op2_range (irange
&r
, tree type
,
3105 relation_trio rel
= TRIO_VARYING
) const;
3106 // Check compatibility of all operands.
3107 bool operand_check_p (tree t1
, tree t2
, tree t3
) const final override
3108 { return range_compatible_p (t1
, t2
) && range_compatible_p (t1
, t3
); }
3112 operator_logical_and::fold_range (irange
&r
, tree type
,
3115 relation_trio
) const
3117 if (empty_range_varying (r
, type
, lh
, rh
))
3120 // Precision of LHS and both operands must match.
3121 if (TYPE_PRECISION (lh
.type ()) != TYPE_PRECISION (type
)
3122 || TYPE_PRECISION (type
) != TYPE_PRECISION (rh
.type ()))
3125 // 0 && anything is 0.
3126 if ((wi::eq_p (lh
.lower_bound (), 0) && wi::eq_p (lh
.upper_bound (), 0))
3127 || (wi::eq_p (lh
.lower_bound (), 0) && wi::eq_p (rh
.upper_bound (), 0)))
3128 r
= range_false (type
);
3129 else if (contains_zero_p (lh
) || contains_zero_p (rh
))
3130 // To reach this point, there must be a logical 1 on each side, and
3131 // the only remaining question is whether there is a zero or not.
3132 r
= range_true_and_false (type
);
3134 r
= range_true (type
);
3139 operator_logical_and::op1_range (irange
&r
, tree type
,
3141 const irange
&op2 ATTRIBUTE_UNUSED
,
3142 relation_trio
) const
3144 switch (get_bool_state (r
, lhs
, type
))
3147 // A true result means both sides of the AND must be true.
3148 r
= range_true (type
);
3151 // Any other result means only one side has to be false, the
3152 // other side can be anything. So we cannot be sure of any
3154 r
= range_true_and_false (type
);
3161 operator_logical_and::op2_range (irange
&r
, tree type
,
3164 relation_trio
) const
3166 return operator_logical_and::op1_range (r
, type
, lhs
, op1
);
3171 operator_bitwise_and::update_bitmask (irange
&r
, const irange
&lh
,
3172 const irange
&rh
) const
3174 update_known_bitmask (r
, BIT_AND_EXPR
, lh
, rh
);
3177 // Optimize BIT_AND_EXPR, BIT_IOR_EXPR and BIT_XOR_EXPR of signed types
3178 // by considering the number of leading redundant sign bit copies.
3179 // clrsb (X op Y) = min (clrsb (X), clrsb (Y)), so for example
3180 // [-1, 0] op [-1, 0] is [-1, 0] (where nonzero_bits doesn't help).
3182 wi_optimize_signed_bitwise_op (irange
&r
, tree type
,
3183 const wide_int
&lh_lb
, const wide_int
&lh_ub
,
3184 const wide_int
&rh_lb
, const wide_int
&rh_ub
)
3186 int lh_clrsb
= MIN (wi::clrsb (lh_lb
), wi::clrsb (lh_ub
));
3187 int rh_clrsb
= MIN (wi::clrsb (rh_lb
), wi::clrsb (rh_ub
));
3188 int new_clrsb
= MIN (lh_clrsb
, rh_clrsb
);
3191 int type_prec
= TYPE_PRECISION (type
);
3192 int rprec
= (type_prec
- new_clrsb
) - 1;
3193 value_range_with_overflow (r
, type
,
3194 wi::mask (rprec
, true, type_prec
),
3195 wi::mask (rprec
, false, type_prec
));
3199 // An AND of 8,16, 32 or 64 bits can produce a partial equivalence between
3203 operator_bitwise_and::lhs_op1_relation (const irange
&lhs
,
3206 relation_kind
) const
3208 if (lhs
.undefined_p () || op1
.undefined_p () || op2
.undefined_p ())
3209 return VREL_VARYING
;
3210 if (!op2
.singleton_p ())
3211 return VREL_VARYING
;
3212 // if val == 0xff or 0xFFFF OR 0Xffffffff OR 0Xffffffffffffffff, return TRUE
3213 int prec1
= TYPE_PRECISION (op1
.type ());
3214 int prec2
= TYPE_PRECISION (op2
.type ());
3216 wide_int mask
= op2
.lower_bound ();
3217 if (wi::eq_p (mask
, wi::mask (8, false, prec2
)))
3219 else if (wi::eq_p (mask
, wi::mask (16, false, prec2
)))
3221 else if (wi::eq_p (mask
, wi::mask (32, false, prec2
)))
3223 else if (wi::eq_p (mask
, wi::mask (64, false, prec2
)))
3225 return bits_to_pe (MIN (prec1
, mask_prec
));
3228 // Optimize BIT_AND_EXPR and BIT_IOR_EXPR in terms of a mask if
3229 // possible. Basically, see if we can optimize:
3233 // [LB op Z, UB op Z]
3235 // If the optimization was successful, accumulate the range in R and
3239 wi_optimize_and_or (irange
&r
,
3240 enum tree_code code
,
3242 const wide_int
&lh_lb
, const wide_int
&lh_ub
,
3243 const wide_int
&rh_lb
, const wide_int
&rh_ub
)
3245 // Calculate the singleton mask among the ranges, if any.
3246 wide_int lower_bound
, upper_bound
, mask
;
3247 if (wi::eq_p (rh_lb
, rh_ub
))
3250 lower_bound
= lh_lb
;
3251 upper_bound
= lh_ub
;
3253 else if (wi::eq_p (lh_lb
, lh_ub
))
3256 lower_bound
= rh_lb
;
3257 upper_bound
= rh_ub
;
3262 // If Z is a constant which (for op | its bitwise not) has n
3263 // consecutive least significant bits cleared followed by m 1
3264 // consecutive bits set immediately above it and either
3265 // m + n == precision, or (x >> (m + n)) == (y >> (m + n)).
3267 // The least significant n bits of all the values in the range are
3268 // cleared or set, the m bits above it are preserved and any bits
3269 // above these are required to be the same for all values in the
3273 if (code
== BIT_IOR_EXPR
)
3275 if (wi::eq_p (w
, 0))
3276 n
= w
.get_precision ();
3280 w
= ~(w
| wi::mask (n
, false, w
.get_precision ()));
3281 if (wi::eq_p (w
, 0))
3282 m
= w
.get_precision () - n
;
3284 m
= wi::ctz (w
) - n
;
3286 wide_int new_mask
= wi::mask (m
+ n
, true, w
.get_precision ());
3287 if ((new_mask
& lower_bound
) != (new_mask
& upper_bound
))
3290 wide_int res_lb
, res_ub
;
3291 if (code
== BIT_AND_EXPR
)
3293 res_lb
= wi::bit_and (lower_bound
, mask
);
3294 res_ub
= wi::bit_and (upper_bound
, mask
);
3296 else if (code
== BIT_IOR_EXPR
)
3298 res_lb
= wi::bit_or (lower_bound
, mask
);
3299 res_ub
= wi::bit_or (upper_bound
, mask
);
3303 value_range_with_overflow (r
, type
, res_lb
, res_ub
);
3305 // Furthermore, if the mask is non-zero, an IOR cannot contain zero.
3306 if (code
== BIT_IOR_EXPR
&& wi::ne_p (mask
, 0))
3309 tmp
.set_nonzero (type
);
3315 // For range [LB, UB] compute two wide_int bit masks.
3317 // In the MAYBE_NONZERO bit mask, if some bit is unset, it means that
3318 // for all numbers in the range the bit is 0, otherwise it might be 0
3321 // In the MUSTBE_NONZERO bit mask, if some bit is set, it means that
3322 // for all numbers in the range the bit is 1, otherwise it might be 0
3326 wi_set_zero_nonzero_bits (tree type
,
3327 const wide_int
&lb
, const wide_int
&ub
,
3328 wide_int
&maybe_nonzero
,
3329 wide_int
&mustbe_nonzero
)
3331 signop sign
= TYPE_SIGN (type
);
3333 if (wi::eq_p (lb
, ub
))
3334 maybe_nonzero
= mustbe_nonzero
= lb
;
3335 else if (wi::ge_p (lb
, 0, sign
) || wi::lt_p (ub
, 0, sign
))
3337 wide_int xor_mask
= lb
^ ub
;
3338 maybe_nonzero
= lb
| ub
;
3339 mustbe_nonzero
= lb
& ub
;
3342 wide_int mask
= wi::mask (wi::floor_log2 (xor_mask
), false,
3343 maybe_nonzero
.get_precision ());
3344 maybe_nonzero
= maybe_nonzero
| mask
;
3345 mustbe_nonzero
= wi::bit_and_not (mustbe_nonzero
, mask
);
3350 maybe_nonzero
= wi::minus_one (lb
.get_precision ());
3351 mustbe_nonzero
= wi::zero (lb
.get_precision ());
3356 operator_bitwise_and::wi_fold (irange
&r
, tree type
,
3357 const wide_int
&lh_lb
,
3358 const wide_int
&lh_ub
,
3359 const wide_int
&rh_lb
,
3360 const wide_int
&rh_ub
) const
3362 if (wi_optimize_and_or (r
, BIT_AND_EXPR
, type
, lh_lb
, lh_ub
, rh_lb
, rh_ub
))
3365 wide_int maybe_nonzero_lh
, mustbe_nonzero_lh
;
3366 wide_int maybe_nonzero_rh
, mustbe_nonzero_rh
;
3367 wi_set_zero_nonzero_bits (type
, lh_lb
, lh_ub
,
3368 maybe_nonzero_lh
, mustbe_nonzero_lh
);
3369 wi_set_zero_nonzero_bits (type
, rh_lb
, rh_ub
,
3370 maybe_nonzero_rh
, mustbe_nonzero_rh
);
3372 wide_int new_lb
= mustbe_nonzero_lh
& mustbe_nonzero_rh
;
3373 wide_int new_ub
= maybe_nonzero_lh
& maybe_nonzero_rh
;
3374 signop sign
= TYPE_SIGN (type
);
3375 unsigned prec
= TYPE_PRECISION (type
);
3376 // If both input ranges contain only negative values, we can
3377 // truncate the result range maximum to the minimum of the
3378 // input range maxima.
3379 if (wi::lt_p (lh_ub
, 0, sign
) && wi::lt_p (rh_ub
, 0, sign
))
3381 new_ub
= wi::min (new_ub
, lh_ub
, sign
);
3382 new_ub
= wi::min (new_ub
, rh_ub
, sign
);
3384 // If either input range contains only non-negative values
3385 // we can truncate the result range maximum to the respective
3386 // maximum of the input range.
3387 if (wi::ge_p (lh_lb
, 0, sign
))
3388 new_ub
= wi::min (new_ub
, lh_ub
, sign
);
3389 if (wi::ge_p (rh_lb
, 0, sign
))
3390 new_ub
= wi::min (new_ub
, rh_ub
, sign
);
3391 // PR68217: In case of signed & sign-bit-CST should
3392 // result in [-INF, 0] instead of [-INF, INF].
3393 if (wi::gt_p (new_lb
, new_ub
, sign
))
3395 wide_int sign_bit
= wi::set_bit_in_zero (prec
- 1, prec
);
3397 && ((wi::eq_p (lh_lb
, lh_ub
)
3398 && !wi::cmps (lh_lb
, sign_bit
))
3399 || (wi::eq_p (rh_lb
, rh_ub
)
3400 && !wi::cmps (rh_lb
, sign_bit
))))
3402 new_lb
= wi::min_value (prec
, sign
);
3403 new_ub
= wi::zero (prec
);
3406 // If the limits got swapped around, return varying.
3407 if (wi::gt_p (new_lb
, new_ub
,sign
))
3410 && wi_optimize_signed_bitwise_op (r
, type
,
3414 r
.set_varying (type
);
3417 value_range_with_overflow (r
, type
, new_lb
, new_ub
);
3421 set_nonzero_range_from_mask (irange
&r
, tree type
, const irange
&lhs
)
3423 if (lhs
.undefined_p () || contains_zero_p (lhs
))
3424 r
.set_varying (type
);
3426 r
.set_nonzero (type
);
3429 /* Find out smallest RES where RES > VAL && (RES & MASK) == RES, if any
3430 (otherwise return VAL). VAL and MASK must be zero-extended for
3431 precision PREC. If SGNBIT is non-zero, first xor VAL with SGNBIT
3432 (to transform signed values into unsigned) and at the end xor
3436 masked_increment (const wide_int
&val_in
, const wide_int
&mask
,
3437 const wide_int
&sgnbit
, unsigned int prec
)
3439 wide_int bit
= wi::one (prec
), res
;
3442 wide_int val
= val_in
^ sgnbit
;
3443 for (i
= 0; i
< prec
; i
++, bit
+= bit
)
3446 if ((res
& bit
) == 0)
3449 res
= wi::bit_and_not (val
+ bit
, res
);
3451 if (wi::gtu_p (res
, val
))
3452 return res
^ sgnbit
;
3454 return val
^ sgnbit
;
3457 // This was shamelessly stolen from register_edge_assert_for_2 and
3458 // adjusted to work with iranges.
3461 operator_bitwise_and::simple_op1_range_solver (irange
&r
, tree type
,
3463 const irange
&op2
) const
3465 if (!op2
.singleton_p ())
3467 set_nonzero_range_from_mask (r
, type
, lhs
);
3470 unsigned int nprec
= TYPE_PRECISION (type
);
3471 wide_int cst2v
= op2
.lower_bound ();
3472 bool cst2n
= wi::neg_p (cst2v
, TYPE_SIGN (type
));
3475 sgnbit
= wi::set_bit_in_zero (nprec
- 1, nprec
);
3477 sgnbit
= wi::zero (nprec
);
3479 // Solve [lhs.lower_bound (), +INF] = x & MASK.
3481 // Minimum unsigned value for >= if (VAL & CST2) == VAL is VAL and
3482 // maximum unsigned value is ~0. For signed comparison, if CST2
3483 // doesn't have the most significant bit set, handle it similarly. If
3484 // CST2 has MSB set, the minimum is the same, and maximum is ~0U/2.
3485 wide_int valv
= lhs
.lower_bound ();
3486 wide_int minv
= valv
& cst2v
, maxv
;
3487 bool we_know_nothing
= false;
3490 // If (VAL & CST2) != VAL, X & CST2 can't be equal to VAL.
3491 minv
= masked_increment (valv
, cst2v
, sgnbit
, nprec
);
3494 // If we can't determine anything on this bound, fall
3495 // through and conservatively solve for the other end point.
3496 we_know_nothing
= true;
3499 maxv
= wi::mask (nprec
- (cst2n
? 1 : 0), false, nprec
);
3500 if (we_know_nothing
)
3501 r
.set_varying (type
);
3503 create_possibly_reversed_range (r
, type
, minv
, maxv
);
3505 // Solve [-INF, lhs.upper_bound ()] = x & MASK.
3507 // Minimum unsigned value for <= is 0 and maximum unsigned value is
3508 // VAL | ~CST2 if (VAL & CST2) == VAL. Otherwise, find smallest
3510 // VAL2 > VAL && (VAL2 & CST2) == VAL2 and use (VAL2 - 1) | ~CST2
3512 // For signed comparison, if CST2 doesn't have most significant bit
3513 // set, handle it similarly. If CST2 has MSB set, the maximum is
3514 // the same and minimum is INT_MIN.
3515 valv
= lhs
.upper_bound ();
3516 minv
= valv
& cst2v
;
3521 maxv
= masked_increment (valv
, cst2v
, sgnbit
, nprec
);
3524 // If we couldn't determine anything on either bound, return
3526 if (we_know_nothing
)
3534 int_range
<2> upper_bits
;
3535 create_possibly_reversed_range (upper_bits
, type
, minv
, maxv
);
3536 r
.intersect (upper_bits
);
3540 operator_bitwise_and::op1_range (irange
&r
, tree type
,
3543 relation_trio
) const
3545 if (lhs
.undefined_p ())
3547 if (types_compatible_p (type
, boolean_type_node
))
3548 return op_logical_and
.op1_range (r
, type
, lhs
, op2
);
3551 for (unsigned i
= 0; i
< lhs
.num_pairs (); ++i
)
3553 int_range_max
chunk (lhs
.type (),
3554 lhs
.lower_bound (i
),
3555 lhs
.upper_bound (i
));
3557 simple_op1_range_solver (res
, type
, chunk
, op2
);
3560 if (r
.undefined_p ())
3561 set_nonzero_range_from_mask (r
, type
, lhs
);
3563 // For MASK == op1 & MASK, all the bits in MASK must be set in op1.
3565 if (lhs
== op2
&& lhs
.singleton_p (mask
))
3567 r
.update_bitmask (irange_bitmask (mask
, ~mask
));
3571 // For 0 = op1 & MASK, op1 is ~MASK.
3572 if (lhs
.zero_p () && op2
.singleton_p ())
3574 wide_int nz
= wi::bit_not (op2
.get_nonzero_bits ());
3575 int_range
<2> tmp (type
);
3576 tmp
.set_nonzero_bits (nz
);
3583 operator_bitwise_and::op2_range (irange
&r
, tree type
,
3586 relation_trio
) const
3588 return operator_bitwise_and::op1_range (r
, type
, lhs
, op1
);
3592 class operator_logical_or
: public range_operator
3594 using range_operator::fold_range
;
3595 using range_operator::op1_range
;
3596 using range_operator::op2_range
;
3598 virtual bool fold_range (irange
&r
, tree type
,
3601 relation_trio rel
= TRIO_VARYING
) const;
3602 virtual bool op1_range (irange
&r
, tree type
,
3605 relation_trio rel
= TRIO_VARYING
) const;
3606 virtual bool op2_range (irange
&r
, tree type
,
3609 relation_trio rel
= TRIO_VARYING
) const;
3610 // Check compatibility of all operands.
3611 bool operand_check_p (tree t1
, tree t2
, tree t3
) const final override
3612 { return range_compatible_p (t1
, t2
) && range_compatible_p (t1
, t3
); }
3616 operator_logical_or::fold_range (irange
&r
, tree type ATTRIBUTE_UNUSED
,
3619 relation_trio
) const
3621 if (empty_range_varying (r
, type
, lh
, rh
))
3630 operator_logical_or::op1_range (irange
&r
, tree type
,
3632 const irange
&op2 ATTRIBUTE_UNUSED
,
3633 relation_trio
) const
3635 switch (get_bool_state (r
, lhs
, type
))
3638 // A false result means both sides of the OR must be false.
3639 r
= range_false (type
);
3642 // Any other result means only one side has to be true, the
3643 // other side can be anything. so we can't be sure of any result
3645 r
= range_true_and_false (type
);
3652 operator_logical_or::op2_range (irange
&r
, tree type
,
3655 relation_trio
) const
3657 return operator_logical_or::op1_range (r
, type
, lhs
, op1
);
3662 operator_bitwise_or::update_bitmask (irange
&r
, const irange
&lh
,
3663 const irange
&rh
) const
3665 update_known_bitmask (r
, BIT_IOR_EXPR
, lh
, rh
);
3669 operator_bitwise_or::wi_fold (irange
&r
, tree type
,
3670 const wide_int
&lh_lb
,
3671 const wide_int
&lh_ub
,
3672 const wide_int
&rh_lb
,
3673 const wide_int
&rh_ub
) const
3675 if (wi_optimize_and_or (r
, BIT_IOR_EXPR
, type
, lh_lb
, lh_ub
, rh_lb
, rh_ub
))
3678 wide_int maybe_nonzero_lh
, mustbe_nonzero_lh
;
3679 wide_int maybe_nonzero_rh
, mustbe_nonzero_rh
;
3680 wi_set_zero_nonzero_bits (type
, lh_lb
, lh_ub
,
3681 maybe_nonzero_lh
, mustbe_nonzero_lh
);
3682 wi_set_zero_nonzero_bits (type
, rh_lb
, rh_ub
,
3683 maybe_nonzero_rh
, mustbe_nonzero_rh
);
3684 wide_int new_lb
= mustbe_nonzero_lh
| mustbe_nonzero_rh
;
3685 wide_int new_ub
= maybe_nonzero_lh
| maybe_nonzero_rh
;
3686 signop sign
= TYPE_SIGN (type
);
3687 // If the input ranges contain only positive values we can
3688 // truncate the minimum of the result range to the maximum
3689 // of the input range minima.
3690 if (wi::ge_p (lh_lb
, 0, sign
)
3691 && wi::ge_p (rh_lb
, 0, sign
))
3693 new_lb
= wi::max (new_lb
, lh_lb
, sign
);
3694 new_lb
= wi::max (new_lb
, rh_lb
, sign
);
3696 // If either input range contains only negative values
3697 // we can truncate the minimum of the result range to the
3698 // respective minimum range.
3699 if (wi::lt_p (lh_ub
, 0, sign
))
3700 new_lb
= wi::max (new_lb
, lh_lb
, sign
);
3701 if (wi::lt_p (rh_ub
, 0, sign
))
3702 new_lb
= wi::max (new_lb
, rh_lb
, sign
);
3703 // If the limits got swapped around, return a conservative range.
3704 if (wi::gt_p (new_lb
, new_ub
, sign
))
3706 // Make sure that nonzero|X is nonzero.
3707 if (wi::gt_p (lh_lb
, 0, sign
)
3708 || wi::gt_p (rh_lb
, 0, sign
)
3709 || wi::lt_p (lh_ub
, 0, sign
)
3710 || wi::lt_p (rh_ub
, 0, sign
))
3711 r
.set_nonzero (type
);
3712 else if (sign
== SIGNED
3713 && wi_optimize_signed_bitwise_op (r
, type
,
3718 r
.set_varying (type
);
3721 value_range_with_overflow (r
, type
, new_lb
, new_ub
);
3725 operator_bitwise_or::op1_range (irange
&r
, tree type
,
3728 relation_trio
) const
3730 if (lhs
.undefined_p ())
3732 // If this is really a logical wi_fold, call that.
3733 if (types_compatible_p (type
, boolean_type_node
))
3734 return op_logical_or
.op1_range (r
, type
, lhs
, op2
);
3741 r
.set_varying (type
);
3746 operator_bitwise_or::op2_range (irange
&r
, tree type
,
3749 relation_trio
) const
3751 return operator_bitwise_or::op1_range (r
, type
, lhs
, op1
);
3755 operator_bitwise_xor::update_bitmask (irange
&r
, const irange
&lh
,
3756 const irange
&rh
) const
3758 update_known_bitmask (r
, BIT_XOR_EXPR
, lh
, rh
);
3762 operator_bitwise_xor::wi_fold (irange
&r
, tree type
,
3763 const wide_int
&lh_lb
,
3764 const wide_int
&lh_ub
,
3765 const wide_int
&rh_lb
,
3766 const wide_int
&rh_ub
) const
3768 signop sign
= TYPE_SIGN (type
);
3769 wide_int maybe_nonzero_lh
, mustbe_nonzero_lh
;
3770 wide_int maybe_nonzero_rh
, mustbe_nonzero_rh
;
3771 wi_set_zero_nonzero_bits (type
, lh_lb
, lh_ub
,
3772 maybe_nonzero_lh
, mustbe_nonzero_lh
);
3773 wi_set_zero_nonzero_bits (type
, rh_lb
, rh_ub
,
3774 maybe_nonzero_rh
, mustbe_nonzero_rh
);
3776 wide_int result_zero_bits
= ((mustbe_nonzero_lh
& mustbe_nonzero_rh
)
3777 | ~(maybe_nonzero_lh
| maybe_nonzero_rh
));
3778 wide_int result_one_bits
3779 = (wi::bit_and_not (mustbe_nonzero_lh
, maybe_nonzero_rh
)
3780 | wi::bit_and_not (mustbe_nonzero_rh
, maybe_nonzero_lh
));
3781 wide_int new_ub
= ~result_zero_bits
;
3782 wide_int new_lb
= result_one_bits
;
3784 // If the range has all positive or all negative values, the result
3785 // is better than VARYING.
3786 if (wi::lt_p (new_lb
, 0, sign
) || wi::ge_p (new_ub
, 0, sign
))
3787 value_range_with_overflow (r
, type
, new_lb
, new_ub
);
3788 else if (sign
== SIGNED
3789 && wi_optimize_signed_bitwise_op (r
, type
,
3794 r
.set_varying (type
);
3796 /* Furthermore, XOR is non-zero if its arguments can't be equal. */
3797 if (wi::lt_p (lh_ub
, rh_lb
, sign
)
3798 || wi::lt_p (rh_ub
, lh_lb
, sign
)
3799 || wi::ne_p (result_one_bits
, 0))
3802 tmp
.set_nonzero (type
);
3808 operator_bitwise_xor::op1_op2_relation_effect (irange
&lhs_range
,
3812 relation_kind rel
) const
3814 if (rel
== VREL_VARYING
)
3817 int_range
<2> rel_range
;
3822 rel_range
.set_zero (type
);
3825 rel_range
.set_nonzero (type
);
3831 lhs_range
.intersect (rel_range
);
3836 operator_bitwise_xor::op1_range (irange
&r
, tree type
,
3839 relation_trio
) const
3841 if (lhs
.undefined_p () || lhs
.varying_p ())
3846 if (types_compatible_p (type
, boolean_type_node
))
3848 switch (get_bool_state (r
, lhs
, type
))
3851 if (op2
.varying_p ())
3852 r
.set_varying (type
);
3853 else if (op2
.zero_p ())
3854 r
= range_true (type
);
3855 // See get_bool_state for the rationale
3856 else if (op2
.undefined_p () || contains_zero_p (op2
))
3857 r
= range_true_and_false (type
);
3859 r
= range_false (type
);
3869 r
.set_varying (type
);
3874 operator_bitwise_xor::op2_range (irange
&r
, tree type
,
3877 relation_trio
) const
3879 return operator_bitwise_xor::op1_range (r
, type
, lhs
, op1
);
3882 class operator_trunc_mod
: public range_operator
3884 using range_operator::op1_range
;
3885 using range_operator::op2_range
;
3887 virtual void wi_fold (irange
&r
, tree type
,
3888 const wide_int
&lh_lb
,
3889 const wide_int
&lh_ub
,
3890 const wide_int
&rh_lb
,
3891 const wide_int
&rh_ub
) const;
3892 virtual bool op1_range (irange
&r
, tree type
,
3895 relation_trio
) const;
3896 virtual bool op2_range (irange
&r
, tree type
,
3899 relation_trio
) const;
3900 void update_bitmask (irange
&r
, const irange
&lh
, const irange
&rh
) const
3901 { update_known_bitmask (r
, TRUNC_MOD_EXPR
, lh
, rh
); }
3905 operator_trunc_mod::wi_fold (irange
&r
, tree type
,
3906 const wide_int
&lh_lb
,
3907 const wide_int
&lh_ub
,
3908 const wide_int
&rh_lb
,
3909 const wide_int
&rh_ub
) const
3911 wide_int new_lb
, new_ub
, tmp
;
3912 signop sign
= TYPE_SIGN (type
);
3913 unsigned prec
= TYPE_PRECISION (type
);
3915 // Mod 0 is undefined.
3916 if (wi_zero_p (type
, rh_lb
, rh_ub
))
3922 // Check for constant and try to fold.
3923 if (lh_lb
== lh_ub
&& rh_lb
== rh_ub
)
3925 wi::overflow_type ov
= wi::OVF_NONE
;
3926 tmp
= wi::mod_trunc (lh_lb
, rh_lb
, sign
, &ov
);
3927 if (ov
== wi::OVF_NONE
)
3929 r
= int_range
<2> (type
, tmp
, tmp
);
3934 // ABS (A % B) < ABS (B) and either 0 <= A % B <= A or A <= A % B <= 0.
3939 new_ub
= wi::smax (new_ub
, tmp
);
3942 if (sign
== UNSIGNED
)
3943 new_lb
= wi::zero (prec
);
3948 if (wi::gts_p (tmp
, 0))
3949 tmp
= wi::zero (prec
);
3950 new_lb
= wi::smax (new_lb
, tmp
);
3953 if (sign
== SIGNED
&& wi::neg_p (tmp
))
3954 tmp
= wi::zero (prec
);
3955 new_ub
= wi::min (new_ub
, tmp
, sign
);
3957 value_range_with_overflow (r
, type
, new_lb
, new_ub
);
3961 operator_trunc_mod::op1_range (irange
&r
, tree type
,
3964 relation_trio
) const
3966 if (lhs
.undefined_p ())
3969 signop sign
= TYPE_SIGN (type
);
3970 unsigned prec
= TYPE_PRECISION (type
);
3971 // (a % b) >= x && x > 0 , then a >= x.
3972 if (wi::gt_p (lhs
.lower_bound (), 0, sign
))
3974 r
= value_range (type
, lhs
.lower_bound (), wi::max_value (prec
, sign
));
3977 // (a % b) <= x && x < 0 , then a <= x.
3978 if (wi::lt_p (lhs
.upper_bound (), 0, sign
))
3980 r
= value_range (type
, wi::min_value (prec
, sign
), lhs
.upper_bound ());
3987 operator_trunc_mod::op2_range (irange
&r
, tree type
,
3990 relation_trio
) const
3992 if (lhs
.undefined_p ())
3995 signop sign
= TYPE_SIGN (type
);
3996 unsigned prec
= TYPE_PRECISION (type
);
3997 // (a % b) >= x && x > 0 , then b is in ~[-x, x] for signed
3998 // or b > x for unsigned.
3999 if (wi::gt_p (lhs
.lower_bound (), 0, sign
))
4002 r
= value_range (type
, wi::neg (lhs
.lower_bound ()),
4003 lhs
.lower_bound (), VR_ANTI_RANGE
);
4004 else if (wi::lt_p (lhs
.lower_bound (), wi::max_value (prec
, sign
),
4006 r
= value_range (type
, lhs
.lower_bound () + 1,
4007 wi::max_value (prec
, sign
));
4012 // (a % b) <= x && x < 0 , then b is in ~[x, -x].
4013 if (wi::lt_p (lhs
.upper_bound (), 0, sign
))
4015 if (wi::gt_p (lhs
.upper_bound (), wi::min_value (prec
, sign
), sign
))
4016 r
= value_range (type
, lhs
.upper_bound (),
4017 wi::neg (lhs
.upper_bound ()), VR_ANTI_RANGE
);
4026 class operator_logical_not
: public range_operator
4028 using range_operator::fold_range
;
4029 using range_operator::op1_range
;
4031 virtual bool fold_range (irange
&r
, tree type
,
4034 relation_trio rel
= TRIO_VARYING
) const;
4035 virtual bool op1_range (irange
&r
, tree type
,
4038 relation_trio rel
= TRIO_VARYING
) const;
4039 // Check compatibility of LHS and op1.
4040 bool operand_check_p (tree t1
, tree t2
, tree
) const final override
4041 { return range_compatible_p (t1
, t2
); }
4044 // Folding a logical NOT, oddly enough, involves doing nothing on the
4045 // forward pass through. During the initial walk backwards, the
4046 // logical NOT reversed the desired outcome on the way back, so on the
4047 // way forward all we do is pass the range forward.
4052 // to determine the TRUE branch, walking backward
4053 // if (b_3) if ([1,1])
4054 // b_3 = !b_2 [1,1] = ![0,0]
4055 // b_2 = x_1 < 20 [0,0] = x_1 < 20, false, so x_1 == [20, 255]
4056 // which is the result we are looking for.. so.. pass it through.
4059 operator_logical_not::fold_range (irange
&r
, tree type
,
4061 const irange
&rh ATTRIBUTE_UNUSED
,
4062 relation_trio
) const
4064 if (empty_range_varying (r
, type
, lh
, rh
))
4068 if (!lh
.varying_p () && !lh
.undefined_p ())
4075 operator_logical_not::op1_range (irange
&r
,
4079 relation_trio
) const
4081 // Logical NOT is involutary...do it again.
4082 return fold_range (r
, type
, lhs
, op2
);
4086 operator_bitwise_not::fold_range (irange
&r
, tree type
,
4089 relation_trio
) const
4091 if (empty_range_varying (r
, type
, lh
, rh
))
4094 if (types_compatible_p (type
, boolean_type_node
))
4095 return op_logical_not
.fold_range (r
, type
, lh
, rh
);
4097 // ~X is simply -1 - X.
4098 int_range
<1> minusone (type
, wi::minus_one (TYPE_PRECISION (type
)),
4099 wi::minus_one (TYPE_PRECISION (type
)));
4100 return range_op_handler (MINUS_EXPR
).fold_range (r
, type
, minusone
, lh
);
4104 operator_bitwise_not::op1_range (irange
&r
, tree type
,
4107 relation_trio
) const
4109 if (lhs
.undefined_p ())
4111 if (types_compatible_p (type
, boolean_type_node
))
4112 return op_logical_not
.op1_range (r
, type
, lhs
, op2
);
4114 // ~X is -1 - X and since bitwise NOT is involutary...do it again.
4115 return fold_range (r
, type
, lhs
, op2
);
4119 operator_bitwise_not::update_bitmask (irange
&r
, const irange
&lh
,
4120 const irange
&rh
) const
4122 update_known_bitmask (r
, BIT_NOT_EXPR
, lh
, rh
);
4127 operator_cst::fold_range (irange
&r
, tree type ATTRIBUTE_UNUSED
,
4129 const irange
&rh ATTRIBUTE_UNUSED
,
4130 relation_trio
) const
4137 // Determine if there is a relationship between LHS and OP1.
4140 operator_identity::lhs_op1_relation (const irange
&lhs
,
4141 const irange
&op1 ATTRIBUTE_UNUSED
,
4142 const irange
&op2 ATTRIBUTE_UNUSED
,
4143 relation_kind
) const
4145 if (lhs
.undefined_p ())
4146 return VREL_VARYING
;
4147 // Simply a copy, so they are equivalent.
4152 operator_identity::fold_range (irange
&r
, tree type ATTRIBUTE_UNUSED
,
4154 const irange
&rh ATTRIBUTE_UNUSED
,
4155 relation_trio
) const
4162 operator_identity::op1_range (irange
&r
, tree type ATTRIBUTE_UNUSED
,
4164 const irange
&op2 ATTRIBUTE_UNUSED
,
4165 relation_trio
) const
4172 class operator_unknown
: public range_operator
4174 using range_operator::fold_range
;
4176 virtual bool fold_range (irange
&r
, tree type
,
4179 relation_trio rel
= TRIO_VARYING
) const;
4183 operator_unknown::fold_range (irange
&r
, tree type
,
4184 const irange
&lh ATTRIBUTE_UNUSED
,
4185 const irange
&rh ATTRIBUTE_UNUSED
,
4186 relation_trio
) const
4188 r
.set_varying (type
);
4194 operator_abs::wi_fold (irange
&r
, tree type
,
4195 const wide_int
&lh_lb
, const wide_int
&lh_ub
,
4196 const wide_int
&rh_lb ATTRIBUTE_UNUSED
,
4197 const wide_int
&rh_ub ATTRIBUTE_UNUSED
) const
4200 signop sign
= TYPE_SIGN (type
);
4201 unsigned prec
= TYPE_PRECISION (type
);
4203 // Pass through LH for the easy cases.
4204 if (sign
== UNSIGNED
|| wi::ge_p (lh_lb
, 0, sign
))
4206 r
= int_range
<1> (type
, lh_lb
, lh_ub
);
4210 // -TYPE_MIN_VALUE = TYPE_MIN_VALUE with flag_wrapv so we can't get
4212 wide_int min_value
= wi::min_value (prec
, sign
);
4213 wide_int max_value
= wi::max_value (prec
, sign
);
4214 if (!TYPE_OVERFLOW_UNDEFINED (type
) && wi::eq_p (lh_lb
, min_value
))
4216 r
.set_varying (type
);
4220 // ABS_EXPR may flip the range around, if the original range
4221 // included negative values.
4222 if (wi::eq_p (lh_lb
, min_value
))
4224 // ABS ([-MIN, -MIN]) isn't representable, but we have traditionally
4225 // returned [-MIN,-MIN] so this preserves that behavior. PR37078
4226 if (wi::eq_p (lh_ub
, min_value
))
4228 r
= int_range
<1> (type
, min_value
, min_value
);
4234 min
= wi::abs (lh_lb
);
4236 if (wi::eq_p (lh_ub
, min_value
))
4239 max
= wi::abs (lh_ub
);
4241 // If the range contains zero then we know that the minimum value in the
4242 // range will be zero.
4243 if (wi::le_p (lh_lb
, 0, sign
) && wi::ge_p (lh_ub
, 0, sign
))
4245 if (wi::gt_p (min
, max
, sign
))
4247 min
= wi::zero (prec
);
4251 // If the range was reversed, swap MIN and MAX.
4252 if (wi::gt_p (min
, max
, sign
))
4253 std::swap (min
, max
);
4256 // If the new range has its limits swapped around (MIN > MAX), then
4257 // the operation caused one of them to wrap around. The only thing
4258 // we know is that the result is positive.
4259 if (wi::gt_p (min
, max
, sign
))
4261 min
= wi::zero (prec
);
4264 r
= int_range
<1> (type
, min
, max
);
4268 operator_abs::op1_range (irange
&r
, tree type
,
4271 relation_trio
) const
4273 if (empty_range_varying (r
, type
, lhs
, op2
))
4275 if (TYPE_UNSIGNED (type
))
4280 // Start with the positives because negatives are an impossible result.
4281 int_range_max positives
= range_positives (type
);
4282 positives
.intersect (lhs
);
4284 // Then add the negative of each pair:
4285 // ABS(op1) = [5,20] would yield op1 => [-20,-5][5,20].
4286 for (unsigned i
= 0; i
< positives
.num_pairs (); ++i
)
4287 r
.union_ (int_range
<1> (type
,
4288 -positives
.upper_bound (i
),
4289 -positives
.lower_bound (i
)));
4290 // With flag_wrapv, -TYPE_MIN_VALUE = TYPE_MIN_VALUE which is
4291 // unrepresentable. Add -TYPE_MIN_VALUE in this case.
4292 wide_int min_value
= wi::min_value (TYPE_PRECISION (type
), TYPE_SIGN (type
));
4293 wide_int lb
= lhs
.lower_bound ();
4294 if (!TYPE_OVERFLOW_UNDEFINED (type
) && wi::eq_p (lb
, min_value
))
4295 r
.union_ (int_range
<2> (type
, lb
, lb
));
4300 operator_abs::update_bitmask (irange
&r
, const irange
&lh
,
4301 const irange
&rh
) const
4303 update_known_bitmask (r
, ABS_EXPR
, lh
, rh
);
4306 class operator_absu
: public range_operator
4309 virtual void wi_fold (irange
&r
, tree type
,
4310 const wide_int
&lh_lb
, const wide_int
&lh_ub
,
4311 const wide_int
&rh_lb
, const wide_int
&rh_ub
) const;
4312 virtual void update_bitmask (irange
&r
, const irange
&lh
,
4313 const irange
&rh
) const final override
;
4317 operator_absu::wi_fold (irange
&r
, tree type
,
4318 const wide_int
&lh_lb
, const wide_int
&lh_ub
,
4319 const wide_int
&rh_lb ATTRIBUTE_UNUSED
,
4320 const wide_int
&rh_ub ATTRIBUTE_UNUSED
) const
4322 wide_int new_lb
, new_ub
;
4324 // Pass through VR0 the easy cases.
4325 if (wi::ges_p (lh_lb
, 0))
4332 new_lb
= wi::abs (lh_lb
);
4333 new_ub
= wi::abs (lh_ub
);
4335 // If the range contains zero then we know that the minimum
4336 // value in the range will be zero.
4337 if (wi::ges_p (lh_ub
, 0))
4339 if (wi::gtu_p (new_lb
, new_ub
))
4341 new_lb
= wi::zero (TYPE_PRECISION (type
));
4344 std::swap (new_lb
, new_ub
);
4347 gcc_checking_assert (TYPE_UNSIGNED (type
));
4348 r
= int_range
<1> (type
, new_lb
, new_ub
);
4352 operator_absu::update_bitmask (irange
&r
, const irange
&lh
,
4353 const irange
&rh
) const
4355 update_known_bitmask (r
, ABSU_EXPR
, lh
, rh
);
4360 operator_negate::fold_range (irange
&r
, tree type
,
4363 relation_trio
) const
4365 if (empty_range_varying (r
, type
, lh
, rh
))
4367 // -X is simply 0 - X.
4368 return range_op_handler (MINUS_EXPR
).fold_range (r
, type
,
4369 range_zero (type
), lh
);
4373 operator_negate::op1_range (irange
&r
, tree type
,
4376 relation_trio
) const
4378 // NEGATE is involutory.
4379 return fold_range (r
, type
, lhs
, op2
);
4384 operator_addr_expr::fold_range (irange
&r
, tree type
,
4387 relation_trio
) const
4389 if (empty_range_varying (r
, type
, lh
, rh
))
4392 // Return a non-null pointer of the LHS type (passed in op2).
4394 r
= range_zero (type
);
4395 else if (lh
.undefined_p () || contains_zero_p (lh
))
4396 r
.set_varying (type
);
4398 r
.set_nonzero (type
);
4403 operator_addr_expr::op1_range (irange
&r
, tree type
,
4406 relation_trio
) const
4408 if (empty_range_varying (r
, type
, lhs
, op2
))
4411 // Return a non-null pointer of the LHS type (passed in op2), but only
4412 // if we cant overflow, eitherwise a no-zero offset could wrap to zero.
4414 if (!lhs
.undefined_p () && !contains_zero_p (lhs
) && TYPE_OVERFLOW_UNDEFINED (type
))
4415 r
.set_nonzero (type
);
4417 r
.set_varying (type
);
4421 // Initialize any integral operators to the primary table
4424 range_op_table::initialize_integral_ops ()
4426 set (TRUNC_DIV_EXPR
, op_trunc_div
);
4427 set (FLOOR_DIV_EXPR
, op_floor_div
);
4428 set (ROUND_DIV_EXPR
, op_round_div
);
4429 set (CEIL_DIV_EXPR
, op_ceil_div
);
4430 set (EXACT_DIV_EXPR
, op_exact_div
);
4431 set (LSHIFT_EXPR
, op_lshift
);
4432 set (RSHIFT_EXPR
, op_rshift
);
4433 set (TRUTH_AND_EXPR
, op_logical_and
);
4434 set (TRUTH_OR_EXPR
, op_logical_or
);
4435 set (TRUNC_MOD_EXPR
, op_trunc_mod
);
4436 set (TRUTH_NOT_EXPR
, op_logical_not
);
4437 set (IMAGPART_EXPR
, op_unknown
);
4438 set (REALPART_EXPR
, op_unknown
);
4439 set (ABSU_EXPR
, op_absu
);
4440 set (OP_WIDEN_MULT_SIGNED
, op_widen_mult_signed
);
4441 set (OP_WIDEN_MULT_UNSIGNED
, op_widen_mult_unsigned
);
4442 set (OP_WIDEN_PLUS_SIGNED
, op_widen_plus_signed
);
4443 set (OP_WIDEN_PLUS_UNSIGNED
, op_widen_plus_unsigned
);
4448 operator_plus::overflow_free_p (const irange
&lh
, const irange
&rh
,
4449 relation_trio
) const
4451 if (lh
.undefined_p () || rh
.undefined_p ())
4454 tree type
= lh
.type ();
4455 if (TYPE_OVERFLOW_UNDEFINED (type
))
4458 wi::overflow_type ovf
;
4459 signop sgn
= TYPE_SIGN (type
);
4460 wide_int wmax0
= lh
.upper_bound ();
4461 wide_int wmax1
= rh
.upper_bound ();
4462 wi::add (wmax0
, wmax1
, sgn
, &ovf
);
4463 if (ovf
!= wi::OVF_NONE
)
4466 if (TYPE_UNSIGNED (type
))
4469 wide_int wmin0
= lh
.lower_bound ();
4470 wide_int wmin1
= rh
.lower_bound ();
4471 wi::add (wmin0
, wmin1
, sgn
, &ovf
);
4472 if (ovf
!= wi::OVF_NONE
)
4479 operator_minus::overflow_free_p (const irange
&lh
, const irange
&rh
,
4480 relation_trio
) const
4482 if (lh
.undefined_p () || rh
.undefined_p ())
4485 tree type
= lh
.type ();
4486 if (TYPE_OVERFLOW_UNDEFINED (type
))
4489 wi::overflow_type ovf
;
4490 signop sgn
= TYPE_SIGN (type
);
4491 wide_int wmin0
= lh
.lower_bound ();
4492 wide_int wmax1
= rh
.upper_bound ();
4493 wi::sub (wmin0
, wmax1
, sgn
, &ovf
);
4494 if (ovf
!= wi::OVF_NONE
)
4497 if (TYPE_UNSIGNED (type
))
4500 wide_int wmax0
= lh
.upper_bound ();
4501 wide_int wmin1
= rh
.lower_bound ();
4502 wi::sub (wmax0
, wmin1
, sgn
, &ovf
);
4503 if (ovf
!= wi::OVF_NONE
)
4510 operator_mult::overflow_free_p (const irange
&lh
, const irange
&rh
,
4511 relation_trio
) const
4513 if (lh
.undefined_p () || rh
.undefined_p ())
4516 tree type
= lh
.type ();
4517 if (TYPE_OVERFLOW_UNDEFINED (type
))
4520 wi::overflow_type ovf
;
4521 signop sgn
= TYPE_SIGN (type
);
4522 wide_int wmax0
= lh
.upper_bound ();
4523 wide_int wmax1
= rh
.upper_bound ();
4524 wi::mul (wmax0
, wmax1
, sgn
, &ovf
);
4525 if (ovf
!= wi::OVF_NONE
)
4528 if (TYPE_UNSIGNED (type
))
4531 wide_int wmin0
= lh
.lower_bound ();
4532 wide_int wmin1
= rh
.lower_bound ();
4533 wi::mul (wmin0
, wmin1
, sgn
, &ovf
);
4534 if (ovf
!= wi::OVF_NONE
)
4537 wi::mul (wmin0
, wmax1
, sgn
, &ovf
);
4538 if (ovf
!= wi::OVF_NONE
)
4541 wi::mul (wmax0
, wmin1
, sgn
, &ovf
);
4542 if (ovf
!= wi::OVF_NONE
)
4549 #include "selftest.h"
4553 #define INT(x) wi::shwi ((x), TYPE_PRECISION (integer_type_node))
4554 #define UINT(x) wi::uhwi ((x), TYPE_PRECISION (unsigned_type_node))
4555 #define INT16(x) wi::shwi ((x), TYPE_PRECISION (short_integer_type_node))
4556 #define UINT16(x) wi::uhwi ((x), TYPE_PRECISION (short_unsigned_type_node))
4557 #define SCHAR(x) wi::shwi ((x), TYPE_PRECISION (signed_char_type_node))
4558 #define UCHAR(x) wi::uhwi ((x), TYPE_PRECISION (unsigned_char_type_node))
4561 range_op_cast_tests ()
4563 int_range
<2> r0
, r1
, r2
, rold
;
4564 r0
.set_varying (integer_type_node
);
4565 wide_int maxint
= r0
.upper_bound ();
4567 // If a range is in any way outside of the range for the converted
4568 // to range, default to the range for the new type.
4569 r0
.set_varying (short_integer_type_node
);
4570 wide_int minshort
= r0
.lower_bound ();
4571 wide_int maxshort
= r0
.upper_bound ();
4572 if (TYPE_PRECISION (integer_type_node
)
4573 > TYPE_PRECISION (short_integer_type_node
))
4575 r1
= int_range
<1> (integer_type_node
,
4576 wi::zero (TYPE_PRECISION (integer_type_node
)),
4578 range_cast (r1
, short_integer_type_node
);
4579 ASSERT_TRUE (r1
.lower_bound () == minshort
4580 && r1
.upper_bound() == maxshort
);
4583 // (unsigned char)[-5,-1] => [251,255].
4584 r0
= rold
= int_range
<1> (signed_char_type_node
, SCHAR (-5), SCHAR (-1));
4585 range_cast (r0
, unsigned_char_type_node
);
4586 ASSERT_TRUE (r0
== int_range
<1> (unsigned_char_type_node
,
4587 UCHAR (251), UCHAR (255)));
4588 range_cast (r0
, signed_char_type_node
);
4589 ASSERT_TRUE (r0
== rold
);
4591 // (signed char)[15, 150] => [-128,-106][15,127].
4592 r0
= rold
= int_range
<1> (unsigned_char_type_node
, UCHAR (15), UCHAR (150));
4593 range_cast (r0
, signed_char_type_node
);
4594 r1
= int_range
<1> (signed_char_type_node
, SCHAR (15), SCHAR (127));
4595 r2
= int_range
<1> (signed_char_type_node
, SCHAR (-128), SCHAR (-106));
4597 ASSERT_TRUE (r1
== r0
);
4598 range_cast (r0
, unsigned_char_type_node
);
4599 ASSERT_TRUE (r0
== rold
);
4601 // (unsigned char)[-5, 5] => [0,5][251,255].
4602 r0
= rold
= int_range
<1> (signed_char_type_node
, SCHAR (-5), SCHAR (5));
4603 range_cast (r0
, unsigned_char_type_node
);
4604 r1
= int_range
<1> (unsigned_char_type_node
, UCHAR (251), UCHAR (255));
4605 r2
= int_range
<1> (unsigned_char_type_node
, UCHAR (0), UCHAR (5));
4607 ASSERT_TRUE (r0
== r1
);
4608 range_cast (r0
, signed_char_type_node
);
4609 ASSERT_TRUE (r0
== rold
);
4611 // (unsigned char)[-5,5] => [0,5][251,255].
4612 r0
= int_range
<1> (integer_type_node
, INT (-5), INT (5));
4613 range_cast (r0
, unsigned_char_type_node
);
4614 r1
= int_range
<1> (unsigned_char_type_node
, UCHAR (0), UCHAR (5));
4615 r1
.union_ (int_range
<1> (unsigned_char_type_node
, UCHAR (251), UCHAR (255)));
4616 ASSERT_TRUE (r0
== r1
);
4618 // (unsigned char)[5U,1974U] => [0,255].
4619 r0
= int_range
<1> (unsigned_type_node
, UINT (5), UINT (1974));
4620 range_cast (r0
, unsigned_char_type_node
);
4621 ASSERT_TRUE (r0
== int_range
<1> (unsigned_char_type_node
, UCHAR (0), UCHAR (255)));
4622 range_cast (r0
, integer_type_node
);
4623 // Going to a wider range should not sign extend.
4624 ASSERT_TRUE (r0
== int_range
<1> (integer_type_node
, INT (0), INT (255)));
4626 // (unsigned char)[-350,15] => [0,255].
4627 r0
= int_range
<1> (integer_type_node
, INT (-350), INT (15));
4628 range_cast (r0
, unsigned_char_type_node
);
4629 ASSERT_TRUE (r0
== (int_range
<1>
4630 (unsigned_char_type_node
,
4631 min_limit (unsigned_char_type_node
),
4632 max_limit (unsigned_char_type_node
))));
4634 // Casting [-120,20] from signed char to unsigned short.
4635 // => [0, 20][0xff88, 0xffff].
4636 r0
= int_range
<1> (signed_char_type_node
, SCHAR (-120), SCHAR (20));
4637 range_cast (r0
, short_unsigned_type_node
);
4638 r1
= int_range
<1> (short_unsigned_type_node
, UINT16 (0), UINT16 (20));
4639 r2
= int_range
<1> (short_unsigned_type_node
,
4640 UINT16 (0xff88), UINT16 (0xffff));
4642 ASSERT_TRUE (r0
== r1
);
4643 // A truncating cast back to signed char will work because [-120, 20]
4644 // is representable in signed char.
4645 range_cast (r0
, signed_char_type_node
);
4646 ASSERT_TRUE (r0
== int_range
<1> (signed_char_type_node
,
4647 SCHAR (-120), SCHAR (20)));
4649 // unsigned char -> signed short
4650 // (signed short)[(unsigned char)25, (unsigned char)250]
4651 // => [(signed short)25, (signed short)250]
4652 r0
= rold
= int_range
<1> (unsigned_char_type_node
, UCHAR (25), UCHAR (250));
4653 range_cast (r0
, short_integer_type_node
);
4654 r1
= int_range
<1> (short_integer_type_node
, INT16 (25), INT16 (250));
4655 ASSERT_TRUE (r0
== r1
);
4656 range_cast (r0
, unsigned_char_type_node
);
4657 ASSERT_TRUE (r0
== rold
);
4659 // Test casting a wider signed [-MIN,MAX] to a narrower unsigned.
4660 r0
= int_range
<1> (long_long_integer_type_node
,
4661 min_limit (long_long_integer_type_node
),
4662 max_limit (long_long_integer_type_node
));
4663 range_cast (r0
, short_unsigned_type_node
);
4664 r1
= int_range
<1> (short_unsigned_type_node
,
4665 min_limit (short_unsigned_type_node
),
4666 max_limit (short_unsigned_type_node
));
4667 ASSERT_TRUE (r0
== r1
);
4669 // Casting NONZERO to a narrower type will wrap/overflow so
4670 // it's just the entire range for the narrower type.
4672 // "NOT 0 at signed 32-bits" ==> [-MIN_32,-1][1, +MAX_32]. This is
4673 // is outside of the range of a smaller range, return the full
4675 if (TYPE_PRECISION (integer_type_node
)
4676 > TYPE_PRECISION (short_integer_type_node
))
4678 r0
= range_nonzero (integer_type_node
);
4679 range_cast (r0
, short_integer_type_node
);
4680 r1
= int_range
<1> (short_integer_type_node
,
4681 min_limit (short_integer_type_node
),
4682 max_limit (short_integer_type_node
));
4683 ASSERT_TRUE (r0
== r1
);
4686 // Casting NONZERO from a narrower signed to a wider signed.
4688 // NONZERO signed 16-bits is [-MIN_16,-1][1, +MAX_16].
4689 // Converting this to 32-bits signed is [-MIN_16,-1][1, +MAX_16].
4690 r0
= range_nonzero (short_integer_type_node
);
4691 range_cast (r0
, integer_type_node
);
4692 r1
= int_range
<1> (integer_type_node
, INT (-32768), INT (-1));
4693 r2
= int_range
<1> (integer_type_node
, INT (1), INT (32767));
4695 ASSERT_TRUE (r0
== r1
);
4699 range_op_lshift_tests ()
4701 // Test that 0x808.... & 0x8.... still contains 0x8....
4702 // for a large set of numbers.
4705 tree big_type
= long_long_unsigned_type_node
;
4706 unsigned big_prec
= TYPE_PRECISION (big_type
);
4707 // big_num = 0x808,0000,0000,0000
4708 wide_int big_num
= wi::lshift (wi::uhwi (0x808, big_prec
),
4709 wi::uhwi (48, big_prec
));
4710 op_bitwise_and
.fold_range (res
, big_type
,
4711 int_range
<1> (big_type
),
4712 int_range
<1> (big_type
, big_num
, big_num
));
4713 // val = 0x8,0000,0000,0000
4714 wide_int val
= wi::lshift (wi::uhwi (8, big_prec
),
4715 wi::uhwi (48, big_prec
));
4716 ASSERT_TRUE (res
.contains_p (val
));
4719 if (TYPE_PRECISION (unsigned_type_node
) > 31)
4721 // unsigned VARYING = op1 << 1 should be VARYING.
4722 int_range
<2> lhs (unsigned_type_node
);
4723 int_range
<2> shift (unsigned_type_node
, INT (1), INT (1));
4725 op_lshift
.op1_range (op1
, unsigned_type_node
, lhs
, shift
);
4726 ASSERT_TRUE (op1
.varying_p ());
4728 // 0 = op1 << 1 should be [0,0], [0x8000000, 0x8000000].
4729 int_range
<2> zero (unsigned_type_node
, UINT (0), UINT (0));
4730 op_lshift
.op1_range (op1
, unsigned_type_node
, zero
, shift
);
4731 ASSERT_TRUE (op1
.num_pairs () == 2);
4732 // Remove the [0,0] range.
4733 op1
.intersect (zero
);
4734 ASSERT_TRUE (op1
.num_pairs () == 1);
4735 // op1 << 1 should be [0x8000,0x8000] << 1,
4736 // which should result in [0,0].
4737 int_range_max result
;
4738 op_lshift
.fold_range (result
, unsigned_type_node
, op1
, shift
);
4739 ASSERT_TRUE (result
== zero
);
4741 // signed VARYING = op1 << 1 should be VARYING.
4742 if (TYPE_PRECISION (integer_type_node
) > 31)
4744 // unsigned VARYING = op1 << 1 should be VARYING.
4745 int_range
<2> lhs (integer_type_node
);
4746 int_range
<2> shift (integer_type_node
, INT (1), INT (1));
4748 op_lshift
.op1_range (op1
, integer_type_node
, lhs
, shift
);
4749 ASSERT_TRUE (op1
.varying_p ());
4751 // 0 = op1 << 1 should be [0,0], [0x8000000, 0x8000000].
4752 int_range
<2> zero (integer_type_node
, INT (0), INT (0));
4753 op_lshift
.op1_range (op1
, integer_type_node
, zero
, shift
);
4754 ASSERT_TRUE (op1
.num_pairs () == 2);
4755 // Remove the [0,0] range.
4756 op1
.intersect (zero
);
4757 ASSERT_TRUE (op1
.num_pairs () == 1);
4758 // op1 << 1 should be [0x8000,0x8000] << 1,
4759 // which should result in [0,0].
4760 int_range_max result
;
4761 op_lshift
.fold_range (result
, unsigned_type_node
, op1
, shift
);
4762 ASSERT_TRUE (result
== zero
);
4767 range_op_rshift_tests ()
4769 // unsigned: [3, MAX] = OP1 >> 1
4771 int_range_max
lhs (unsigned_type_node
,
4772 UINT (3), max_limit (unsigned_type_node
));
4773 int_range_max
one (unsigned_type_node
,
4774 wi::one (TYPE_PRECISION (unsigned_type_node
)),
4775 wi::one (TYPE_PRECISION (unsigned_type_node
)));
4777 op_rshift
.op1_range (op1
, unsigned_type_node
, lhs
, one
);
4778 ASSERT_FALSE (op1
.contains_p (UINT (3)));
4781 // signed: [3, MAX] = OP1 >> 1
4783 int_range_max
lhs (integer_type_node
,
4784 INT (3), max_limit (integer_type_node
));
4785 int_range_max
one (integer_type_node
, INT (1), INT (1));
4787 op_rshift
.op1_range (op1
, integer_type_node
, lhs
, one
);
4788 ASSERT_FALSE (op1
.contains_p (INT (-2)));
4791 // This is impossible, so OP1 should be [].
4792 // signed: [MIN, MIN] = OP1 >> 1
4794 int_range_max
lhs (integer_type_node
,
4795 min_limit (integer_type_node
),
4796 min_limit (integer_type_node
));
4797 int_range_max
one (integer_type_node
, INT (1), INT (1));
4799 op_rshift
.op1_range (op1
, integer_type_node
, lhs
, one
);
4800 ASSERT_TRUE (op1
.undefined_p ());
4803 // signed: ~[-1] = OP1 >> 31
4804 if (TYPE_PRECISION (integer_type_node
) > 31)
4806 int_range_max
lhs (integer_type_node
, INT (-1), INT (-1), VR_ANTI_RANGE
);
4807 int_range_max
shift (integer_type_node
, INT (31), INT (31));
4809 op_rshift
.op1_range (op1
, integer_type_node
, lhs
, shift
);
4810 int_range_max negatives
= range_negatives (integer_type_node
);
4811 negatives
.intersect (op1
);
4812 ASSERT_TRUE (negatives
.undefined_p ());
4817 range_op_bitwise_and_tests ()
4820 wide_int min
= min_limit (integer_type_node
);
4821 wide_int max
= max_limit (integer_type_node
);
4822 wide_int tiny
= wi::add (min
, wi::one (TYPE_PRECISION (integer_type_node
)));
4823 int_range_max
i1 (integer_type_node
, tiny
, max
);
4824 int_range_max
i2 (integer_type_node
, INT (255), INT (255));
4826 // [MIN+1, MAX] = OP1 & 255: OP1 is VARYING
4827 op_bitwise_and
.op1_range (res
, integer_type_node
, i1
, i2
);
4828 ASSERT_TRUE (res
== int_range
<1> (integer_type_node
));
4830 // VARYING = OP1 & 255: OP1 is VARYING
4831 i1
= int_range
<1> (integer_type_node
);
4832 op_bitwise_and
.op1_range (res
, integer_type_node
, i1
, i2
);
4833 ASSERT_TRUE (res
== int_range
<1> (integer_type_node
));
4835 // For 0 = x & MASK, x is ~MASK.
4837 int_range
<2> zero (integer_type_node
, INT (0), INT (0));
4838 int_range
<2> mask
= int_range
<2> (integer_type_node
, INT (7), INT (7));
4839 op_bitwise_and
.op1_range (res
, integer_type_node
, zero
, mask
);
4840 wide_int inv
= wi::shwi (~7U, TYPE_PRECISION (integer_type_node
));
4841 ASSERT_TRUE (res
.get_nonzero_bits () == inv
);
4844 // (NONZERO | X) is nonzero.
4845 i1
.set_nonzero (integer_type_node
);
4846 i2
.set_varying (integer_type_node
);
4847 op_bitwise_or
.fold_range (res
, integer_type_node
, i1
, i2
);
4848 ASSERT_TRUE (res
.nonzero_p ());
4850 // (NEGATIVE | X) is nonzero.
4851 i1
= int_range
<1> (integer_type_node
, INT (-5), INT (-3));
4852 i2
.set_varying (integer_type_node
);
4853 op_bitwise_or
.fold_range (res
, integer_type_node
, i1
, i2
);
4854 ASSERT_FALSE (res
.contains_p (INT (0)));
4858 range_relational_tests ()
4860 int_range
<2> lhs (unsigned_char_type_node
);
4861 int_range
<2> op1 (unsigned_char_type_node
, UCHAR (8), UCHAR (10));
4862 int_range
<2> op2 (unsigned_char_type_node
, UCHAR (20), UCHAR (20));
4864 // Never wrapping additions mean LHS > OP1.
4865 relation_kind code
= op_plus
.lhs_op1_relation (lhs
, op1
, op2
, VREL_VARYING
);
4866 ASSERT_TRUE (code
== VREL_GT
);
4868 // Most wrapping additions mean nothing...
4869 op1
= int_range
<2> (unsigned_char_type_node
, UCHAR (8), UCHAR (10));
4870 op2
= int_range
<2> (unsigned_char_type_node
, UCHAR (0), UCHAR (255));
4871 code
= op_plus
.lhs_op1_relation (lhs
, op1
, op2
, VREL_VARYING
);
4872 ASSERT_TRUE (code
== VREL_VARYING
);
4874 // However, always wrapping additions mean LHS < OP1.
4875 op1
= int_range
<2> (unsigned_char_type_node
, UCHAR (1), UCHAR (255));
4876 op2
= int_range
<2> (unsigned_char_type_node
, UCHAR (255), UCHAR (255));
4877 code
= op_plus
.lhs_op1_relation (lhs
, op1
, op2
, VREL_VARYING
);
4878 ASSERT_TRUE (code
== VREL_LT
);
4884 range_op_rshift_tests ();
4885 range_op_lshift_tests ();
4886 range_op_bitwise_and_tests ();
4887 range_op_cast_tests ();
4888 range_relational_tests ();
4890 extern void range_op_float_tests ();
4891 range_op_float_tests ();
4894 } // namespace selftest
4896 #endif // CHECKING_P