Line data Source code
1 : /* Code for range operators.
2 : Copyright (C) 2017-2026 Free Software Foundation, Inc.
3 : Contributed by Andrew MacLeod <amacleod@redhat.com>
4 : and Aldy Hernandez <aldyh@redhat.com>.
5 :
6 : This file is part of GCC.
7 :
8 : GCC is free software; you can redistribute it and/or modify
9 : it under the terms of the GNU General Public License as published by
10 : the Free Software Foundation; either version 3, or (at your option)
11 : any later version.
12 :
13 : GCC is distributed in the hope that it will be useful,
14 : but WITHOUT ANY WARRANTY; without even the implied warranty of
15 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 : GNU General Public License for more details.
17 :
18 : You should have received a copy of the GNU General Public License
19 : along with GCC; see the file COPYING3. If not see
20 : <http://www.gnu.org/licenses/>. */
21 :
22 : #include "config.h"
23 : #include "system.h"
24 : #include "coretypes.h"
25 : #include "backend.h"
26 : #include "insn-codes.h"
27 : #include "rtl.h"
28 : #include "tree.h"
29 : #include "gimple.h"
30 : #include "cfghooks.h"
31 : #include "tree-pass.h"
32 : #include "ssa.h"
33 : #include "optabs-tree.h"
34 : #include "gimple-pretty-print.h"
35 : #include "diagnostic-core.h"
36 : #include "flags.h"
37 : #include "fold-const.h"
38 : #include "stor-layout.h"
39 : #include "calls.h"
40 : #include "cfganal.h"
41 : #include "gimple-iterator.h"
42 : #include "gimple-fold.h"
43 : #include "tree-eh.h"
44 : #include "gimple-walk.h"
45 : #include "tree-cfg.h"
46 : #include "wide-int.h"
47 : #include "value-relation.h"
48 : #include "range-op.h"
49 : #include "tree-ssa-ccp.h"
50 : #include "range-op-mixed.h"
51 :
52 : // Instantiate the operators which apply to multiple types here.
53 :
54 : static const operator_equal op_equal;
55 : static const operator_not_equal op_not_equal;
56 : static const operator_lt op_lt;
57 : static const operator_le op_le;
58 : static const operator_gt op_gt;
59 : static const operator_ge op_ge;
60 : static const operator_identity op_ident;
61 : static const operator_cst op_cst;
62 : static const operator_cast op_cast;
63 : static const operator_view op_view;
64 : static const operator_plus op_plus;
65 : static const operator_abs op_abs;
66 : static const operator_minus op_minus;
67 : static const operator_negate op_negate;
68 : static const operator_mult op_mult;
69 : static const operator_addr_expr op_addr;
70 : static const operator_bitwise_not op_bitwise_not;
71 : static const operator_bitwise_xor op_bitwise_xor;
72 : static const operator_bitwise_and op_bitwise_and;
73 : static const operator_bitwise_or op_bitwise_or;
74 : static const operator_min op_min;
75 : static const operator_max op_max;
76 :
77 : // Instantiate a range operator table.
78 : static range_op_table operator_table;
79 :
80 : // Invoke the initialization routines for each class of range.
81 :
82 301808 : range_op_table::range_op_table ()
83 : {
84 301808 : initialize_integral_ops ();
85 301808 : initialize_pointer_ops ();
86 301808 : initialize_float_ops ();
87 :
88 301808 : set (EQ_EXPR, op_equal);
89 301808 : set (NE_EXPR, op_not_equal);
90 301808 : set (LT_EXPR, op_lt);
91 301808 : set (LE_EXPR, op_le);
92 301808 : set (GT_EXPR, op_gt);
93 301808 : set (GE_EXPR, op_ge);
94 301808 : set (SSA_NAME, op_ident);
95 301808 : set (PAREN_EXPR, op_ident);
96 301808 : set (OBJ_TYPE_REF, op_ident);
97 301808 : set (REAL_CST, op_cst);
98 301808 : set (INTEGER_CST, op_cst);
99 301808 : set (NOP_EXPR, op_cast);
100 301808 : set (CONVERT_EXPR, op_cast);
101 301808 : set (VIEW_CONVERT_EXPR, op_view);
102 301808 : set (FLOAT_EXPR, op_cast);
103 301808 : set (FIX_TRUNC_EXPR, op_cast);
104 301808 : set (PLUS_EXPR, op_plus);
105 301808 : set (ABS_EXPR, op_abs);
106 301808 : set (MINUS_EXPR, op_minus);
107 301808 : set (NEGATE_EXPR, op_negate);
108 301808 : set (MULT_EXPR, op_mult);
109 301808 : set (ADDR_EXPR, op_addr);
110 301808 : set (BIT_NOT_EXPR, op_bitwise_not);
111 301808 : set (BIT_XOR_EXPR, op_bitwise_xor);
112 301808 : set (BIT_AND_EXPR, op_bitwise_and);
113 301808 : set (BIT_IOR_EXPR, op_bitwise_or);
114 301808 : set (MIN_EXPR, op_min);
115 301808 : set (MAX_EXPR, op_max);
116 301808 : }
117 :
118 : // Instantiate a default range operator for opcodes with no entry.
119 :
120 : range_operator default_operator;
121 :
122 : // Create a default range_op_handler.
123 :
124 2114296525 : range_op_handler::range_op_handler ()
125 : {
126 2114296525 : m_operator = &default_operator;
127 2114296525 : }
128 :
129 : // Create a range_op_handler for CODE. Use a default operator if CODE
130 : // does not have an entry.
131 :
132 5143470331 : range_op_handler::range_op_handler (unsigned code)
133 : {
134 5143470331 : m_operator = operator_table[code];
135 5143470331 : if (!m_operator)
136 971638832 : m_operator = &default_operator;
137 5143470331 : }
138 :
139 : // Return TRUE if this handler has a non-default operator.
140 :
141 7403948508 : range_op_handler::operator bool () const
142 : {
143 7403948508 : return m_operator != &default_operator;
144 : }
145 :
146 : // Return a pointer to the range operator associated with this handler.
147 : // If it is a default operator, return nullptr.
148 : // This is the equivalent of indexing the range table.
149 :
150 : const range_operator *
151 1250032253 : range_op_handler::range_op () const
152 : {
153 1250032253 : if (m_operator != &default_operator)
154 1250032253 : return m_operator;
155 : return nullptr;
156 : }
157 :
158 : // Create a dispatch pattern for value range discriminators LHS, OP1, and OP2.
159 : // This is used to produce a unique value for each dispatch pattern. Shift
160 : // values are based on the size of the m_discriminator field in value_range.h.
161 :
162 : constexpr unsigned
163 758824018 : dispatch_trio (unsigned lhs, unsigned op1, unsigned op2)
164 : {
165 758824018 : return ((lhs << 8) + (op1 << 4) + (op2));
166 : }
167 :
168 : // These are the supported dispatch patterns. These map to the parameter list
169 : // of the routines in range_operator. Note the last 3 characters are
170 : // shorthand for the LHS, OP1, and OP2 range discriminator class.
171 : // Reminder, single operand instructions use the LHS type for op2, even if
172 : // unused. So FLOAT = INT would be RO_FIF.
173 :
174 : static const unsigned RO_III = dispatch_trio (VR_IRANGE, VR_IRANGE, VR_IRANGE);
175 : static const unsigned RO_IFI = dispatch_trio (VR_IRANGE, VR_FRANGE, VR_IRANGE);
176 : static const unsigned RO_IFF = dispatch_trio (VR_IRANGE, VR_FRANGE, VR_FRANGE);
177 : static const unsigned RO_FFF = dispatch_trio (VR_FRANGE, VR_FRANGE, VR_FRANGE);
178 : static const unsigned RO_FIF = dispatch_trio (VR_FRANGE, VR_IRANGE, VR_FRANGE);
179 : static const unsigned RO_FII = dispatch_trio (VR_FRANGE, VR_IRANGE, VR_IRANGE);
180 : static const unsigned RO_PPP = dispatch_trio (VR_PRANGE, VR_PRANGE, VR_PRANGE);
181 : static const unsigned RO_PPI = dispatch_trio (VR_PRANGE, VR_PRANGE, VR_IRANGE);
182 : static const unsigned RO_IPP = dispatch_trio (VR_IRANGE, VR_PRANGE, VR_PRANGE);
183 : static const unsigned RO_IPI = dispatch_trio (VR_IRANGE, VR_PRANGE, VR_IRANGE);
184 : static const unsigned RO_PIP = dispatch_trio (VR_PRANGE, VR_IRANGE, VR_PRANGE);
185 : static const unsigned RO_PII = dispatch_trio (VR_PRANGE, VR_IRANGE, VR_IRANGE);
186 :
187 : // Return a dispatch value for parameter types LHS, OP1 and OP2.
188 :
189 : unsigned
190 758824018 : range_op_handler::dispatch_kind (const vrange &lhs, const vrange &op1,
191 : const vrange& op2) const
192 : {
193 758824018 : return dispatch_trio (lhs.m_discriminator, op1.m_discriminator,
194 758824018 : op2.m_discriminator);
195 : }
196 :
197 : void
198 0 : range_op_handler::discriminator_fail (const vrange &r1,
199 : const vrange &r2,
200 : const vrange &r3) const
201 : {
202 0 : const char name[] = "IPF";
203 0 : gcc_checking_assert (r1.m_discriminator < sizeof (name) - 1);
204 0 : gcc_checking_assert (r2.m_discriminator < sizeof (name) - 1);
205 0 : gcc_checking_assert (r3.m_discriminator < sizeof (name) - 1);
206 0 : fprintf (stderr,
207 : "Unsupported operand combination in dispatch: RO_%c%c%c\n",
208 0 : name[r1.m_discriminator],
209 0 : name[r2.m_discriminator],
210 0 : name[r3.m_discriminator]);
211 0 : gcc_unreachable ();
212 : }
213 :
214 : static inline bool
215 : has_pointer_operand_p (const vrange &r1, const vrange &r2, const vrange &r3)
216 : {
217 : return is_a <prange> (r1) || is_a <prange> (r2) || is_a <prange> (r3);
218 : }
219 :
220 : // Dispatch a call to fold_range based on the types of R, LH and RH.
221 :
222 : bool
223 314589678 : range_op_handler::fold_range (vrange &r, tree type,
224 : const vrange &lh,
225 : const vrange &rh,
226 : relation_trio rel) const
227 : {
228 314589678 : gcc_checking_assert (m_operator);
229 : #if CHECKING_P
230 314589678 : if (!lh.undefined_p () && !rh.undefined_p ())
231 308630114 : gcc_assert (m_operator->operand_check_p (type, lh.type (), rh.type ()));
232 : #endif
233 314589678 : switch (dispatch_kind (r, lh, rh))
234 : {
235 243602973 : case RO_III:
236 243602973 : return m_operator->fold_range (as_a <irange> (r), type,
237 : as_a <irange> (lh),
238 243602973 : as_a <irange> (rh), rel);
239 374331 : case RO_IFI:
240 374331 : return m_operator->fold_range (as_a <irange> (r), type,
241 : as_a <frange> (lh),
242 374331 : as_a <irange> (rh), rel);
243 2669462 : case RO_IFF:
244 2669462 : return m_operator->fold_range (as_a <irange> (r), type,
245 : as_a <frange> (lh),
246 2669462 : as_a <frange> (rh), rel);
247 6893210 : case RO_FFF:
248 6893210 : return m_operator->fold_range (as_a <frange> (r), type,
249 : as_a <frange> (lh),
250 6893210 : as_a <frange> (rh), rel);
251 0 : case RO_FII:
252 0 : return m_operator->fold_range (as_a <frange> (r), type,
253 : as_a <irange> (lh),
254 0 : as_a <irange> (rh), rel);
255 856519 : case RO_FIF:
256 856519 : return m_operator->fold_range (as_a <frange> (r), type,
257 : as_a <irange> (lh),
258 856519 : as_a <frange> (rh), rel);
259 18797856 : case RO_PPP:
260 18797856 : return m_operator->fold_range (as_a <prange> (r), type,
261 : as_a <prange> (lh),
262 18797856 : as_a <prange> (rh), rel);
263 9047341 : case RO_PPI:
264 9047341 : return m_operator->fold_range (as_a <prange> (r), type,
265 : as_a <prange> (lh),
266 9047341 : as_a <irange> (rh), rel);
267 18780104 : case RO_IPP:
268 18780104 : return m_operator->fold_range (as_a <irange> (r), type,
269 : as_a <prange> (lh),
270 18780104 : as_a <prange> (rh), rel);
271 1916600 : case RO_PIP:
272 1916600 : return m_operator->fold_range (as_a <prange> (r), type,
273 : as_a <irange> (lh),
274 1916600 : as_a <prange> (rh), rel);
275 11651252 : case RO_IPI:
276 11651252 : return m_operator->fold_range (as_a <irange> (r), type,
277 : as_a <prange> (lh),
278 11651252 : as_a <irange> (rh), rel);
279 : default:
280 : return false;
281 : }
282 : }
283 :
284 : // Dispatch a call to op1_range based on the types of R, LHS and OP2.
285 :
286 : bool
287 122060027 : range_op_handler::op1_range (vrange &r, tree type,
288 : const vrange &lhs,
289 : const vrange &op2,
290 : relation_trio rel) const
291 : {
292 122060027 : gcc_checking_assert (m_operator);
293 122060027 : if (lhs.undefined_p ())
294 : return false;
295 : #if CHECKING_P
296 122059193 : if (!op2.undefined_p ())
297 122059032 : gcc_assert (m_operator->operand_check_p (lhs.type (), type, op2.type ()));
298 : #endif
299 122059193 : switch (dispatch_kind (r, lhs, op2))
300 : {
301 110035080 : case RO_III:
302 110035080 : return m_operator->op1_range (as_a <irange> (r), type,
303 : as_a <irange> (lhs),
304 110035080 : as_a <irange> (op2), rel);
305 20023 : case RO_IFI:
306 20023 : return m_operator->op1_range (as_a <irange> (r), type,
307 : as_a <frange> (lhs),
308 20023 : as_a <irange> (op2), rel);
309 70126 : case RO_PPP:
310 70126 : return m_operator->op1_range (as_a <prange> (r), type,
311 : as_a <prange> (lhs),
312 70126 : as_a <prange> (op2), rel);
313 9593325 : case RO_PIP:
314 9593325 : return m_operator->op1_range (as_a <prange> (r), type,
315 : as_a <irange> (lhs),
316 9593325 : as_a <prange> (op2), rel);
317 123616 : case RO_PPI:
318 123616 : return m_operator->op1_range (as_a <prange> (r), type,
319 : as_a <prange> (lhs),
320 123616 : as_a <irange> (op2), rel);
321 147285 : case RO_IPI:
322 147285 : return m_operator->op1_range (as_a <irange> (r), type,
323 : as_a <prange> (lhs),
324 147285 : as_a <irange> (op2), rel);
325 1507171 : case RO_FIF:
326 1507171 : return m_operator->op1_range (as_a <frange> (r), type,
327 : as_a <irange> (lhs),
328 1507171 : as_a <frange> (op2), rel);
329 562567 : case RO_FFF:
330 562567 : return m_operator->op1_range (as_a <frange> (r), type,
331 : as_a <frange> (lhs),
332 562567 : as_a <frange> (op2), rel);
333 : default:
334 : return false;
335 : }
336 : }
337 :
338 : // Dispatch a call to op2_range based on the types of R, LHS and OP1.
339 :
340 : bool
341 39061287 : range_op_handler::op2_range (vrange &r, tree type,
342 : const vrange &lhs,
343 : const vrange &op1,
344 : relation_trio rel) const
345 : {
346 39061287 : gcc_checking_assert (m_operator);
347 39061287 : if (lhs.undefined_p ())
348 : return false;
349 : #if CHECKING_P
350 39061113 : if (!op1.undefined_p ())
351 39060992 : gcc_assert (m_operator->operand_check_p (lhs.type (), op1.type (), type));
352 : #endif
353 39061113 : switch (dispatch_kind (r, lhs, op1))
354 : {
355 32404547 : case RO_III:
356 32404547 : return m_operator->op2_range (as_a <irange> (r), type,
357 : as_a <irange> (lhs),
358 32404547 : as_a <irange> (op1), rel);
359 5801459 : case RO_PIP:
360 5801459 : return m_operator->op2_range (as_a <prange> (r), type,
361 : as_a <irange> (lhs),
362 5801459 : as_a <prange> (op1), rel);
363 132357 : case RO_IPP:
364 132357 : return m_operator->op2_range (as_a <irange> (r), type,
365 : as_a <prange> (lhs),
366 132357 : as_a <prange> (op1), rel);
367 532887 : case RO_FIF:
368 532887 : return m_operator->op2_range (as_a <frange> (r), type,
369 : as_a <irange> (lhs),
370 532887 : as_a <frange> (op1), rel);
371 189571 : case RO_FFF:
372 189571 : return m_operator->op2_range (as_a <frange> (r), type,
373 : as_a <frange> (lhs),
374 189571 : as_a <frange> (op1), rel);
375 : default:
376 : return false;
377 : }
378 : }
379 :
380 : // Dispatch a call to lhs_op1_relation based on the types of LHS, OP1 and OP2.
381 :
382 : relation_kind
383 129935122 : range_op_handler::lhs_op1_relation (const vrange &lhs,
384 : const vrange &op1,
385 : const vrange &op2,
386 : relation_kind rel) const
387 : {
388 129935122 : gcc_checking_assert (m_operator);
389 129935122 : switch (dispatch_kind (lhs, op1, op2))
390 : {
391 104706355 : case RO_III:
392 104706355 : return m_operator->lhs_op1_relation (as_a <irange> (lhs),
393 : as_a <irange> (op1),
394 104706355 : as_a <irange> (op2), rel);
395 2218841 : case RO_PPP:
396 2218841 : return m_operator->lhs_op1_relation (as_a <prange> (lhs),
397 : as_a <prange> (op1),
398 2218841 : as_a <prange> (op2), rel);
399 6450692 : case RO_IPP:
400 6450692 : return m_operator->lhs_op1_relation (as_a <irange> (lhs),
401 : as_a <prange> (op1),
402 6450692 : as_a <prange> (op2), rel);
403 1441618 : case RO_PII:
404 1441618 : return m_operator->lhs_op1_relation (as_a <prange> (lhs),
405 : as_a <irange> (op1),
406 1441618 : as_a <irange> (op2), rel);
407 7836054 : case RO_PPI:
408 7836054 : return m_operator->lhs_op1_relation (as_a <prange> (lhs),
409 : as_a <prange> (op1),
410 7836054 : as_a <irange> (op2), rel);
411 1053980 : case RO_IFF:
412 1053980 : return m_operator->lhs_op1_relation (as_a <irange> (lhs),
413 : as_a <frange> (op1),
414 1053980 : as_a <frange> (op2), rel);
415 5371614 : case RO_FFF:
416 5371614 : return m_operator->lhs_op1_relation (as_a <frange> (lhs),
417 : as_a <frange> (op1),
418 5371614 : as_a <frange> (op2), rel);
419 : default:
420 : return VREL_VARYING;
421 : }
422 : }
423 :
424 : // Dispatch a call to lhs_op2_relation based on the types of LHS, OP1 and OP2.
425 :
426 : relation_kind
427 38262283 : range_op_handler::lhs_op2_relation (const vrange &lhs,
428 : const vrange &op1,
429 : const vrange &op2,
430 : relation_kind rel) const
431 : {
432 38262283 : gcc_checking_assert (m_operator);
433 38262283 : switch (dispatch_kind (lhs, op1, op2))
434 : {
435 27114828 : case RO_III:
436 27114828 : return m_operator->lhs_op2_relation (as_a <irange> (lhs),
437 : as_a <irange> (op1),
438 27114828 : as_a <irange> (op2), rel);
439 164673 : case RO_IFF:
440 164673 : return m_operator->lhs_op2_relation (as_a <irange> (lhs),
441 : as_a <frange> (op1),
442 164673 : as_a <frange> (op2), rel);
443 3237041 : case RO_FFF:
444 3237041 : return m_operator->lhs_op2_relation (as_a <frange> (lhs),
445 : as_a <frange> (op1),
446 3237041 : as_a <frange> (op2), rel);
447 : default:
448 : return VREL_VARYING;
449 : }
450 : }
451 :
452 : // Dispatch a call to op1_op2_relation based on the type of LHS.
453 :
454 : relation_kind
455 114801345 : range_op_handler::op1_op2_relation (const vrange &lhs,
456 : const vrange &op1,
457 : const vrange &op2) const
458 : {
459 114801345 : gcc_checking_assert (m_operator);
460 :
461 114801345 : switch (dispatch_kind (lhs, op1, op2))
462 : {
463 93172151 : case RO_III:
464 93172151 : return m_operator->op1_op2_relation (as_a <irange> (lhs),
465 : as_a <irange> (op1),
466 93172151 : as_a <irange> (op2));
467 :
468 19201686 : case RO_IPP:
469 19201686 : return m_operator->op1_op2_relation (as_a <irange> (lhs),
470 : as_a <prange> (op1),
471 19201686 : as_a <prange> (op2));
472 :
473 1903352 : case RO_IFF:
474 1903352 : return m_operator->op1_op2_relation (as_a <irange> (lhs),
475 : as_a <frange> (op1),
476 1903352 : as_a <frange> (op2));
477 :
478 347663 : case RO_FFF:
479 347663 : return m_operator->op1_op2_relation (as_a <frange> (lhs),
480 : as_a <frange> (op1),
481 347663 : as_a <frange> (op2));
482 :
483 : default:
484 : return VREL_VARYING;
485 : }
486 : }
487 :
488 : bool
489 115284 : range_op_handler::overflow_free_p (const vrange &lh,
490 : const vrange &rh,
491 : relation_trio rel) const
492 : {
493 115284 : gcc_checking_assert (m_operator);
494 115284 : switch (dispatch_kind (lh, lh, rh))
495 : {
496 115284 : case RO_III:
497 115284 : return m_operator->overflow_free_p(as_a <irange> (lh),
498 : as_a <irange> (rh),
499 115284 : rel);
500 : default:
501 : return false;
502 : }
503 : }
504 :
505 : bool
506 9559152 : range_op_handler::operand_check_p (tree t1, tree t2, tree t3) const
507 : {
508 9559152 : gcc_checking_assert (m_operator);
509 9559152 : return m_operator->operand_check_p (t1, t2, t3);
510 : }
511 :
512 : // Update the known bitmasks in R when applying the operation CODE to
513 : // LH and RH.
514 :
515 : void
516 189966582 : update_known_bitmask (vrange &r, tree_code code,
517 : const vrange &lh, const vrange &rh)
518 : {
519 189966582 : if (r.undefined_p () || lh.undefined_p () || rh.undefined_p ()
520 379927067 : || r.singleton_p ())
521 12030196 : return;
522 :
523 177936386 : widest_int widest_value, widest_mask;
524 177936386 : tree type = r.type ();
525 177936386 : signop sign = TYPE_SIGN (type);
526 177936386 : int prec = TYPE_PRECISION (type);
527 177936386 : irange_bitmask lh_bits = lh.get_bitmask ();
528 177936386 : irange_bitmask rh_bits = rh.get_bitmask ();
529 :
530 177936386 : switch (get_gimple_rhs_class (code))
531 : {
532 58882438 : case GIMPLE_UNARY_RHS:
533 58882438 : bit_value_unop (code, sign, prec, &widest_value, &widest_mask,
534 58882438 : TYPE_SIGN (lh.type ()),
535 58882438 : TYPE_PRECISION (lh.type ()),
536 117765396 : widest_int::from (lh_bits.value (),
537 58882438 : TYPE_SIGN (lh.type ())),
538 117764876 : widest_int::from (lh_bits.mask (),
539 58882438 : TYPE_SIGN (lh.type ())));
540 58882438 : break;
541 119053948 : case GIMPLE_BINARY_RHS:
542 238107896 : bit_value_binop (code, sign, prec, &widest_value, &widest_mask,
543 119053948 : TYPE_SIGN (lh.type ()),
544 119053948 : TYPE_PRECISION (lh.type ()),
545 238109459 : widest_int::from (lh_bits.value (), sign),
546 238109459 : widest_int::from (lh_bits.mask (), sign),
547 119053948 : TYPE_SIGN (rh.type ()),
548 119053948 : TYPE_PRECISION (rh.type ()),
549 238109425 : widest_int::from (rh_bits.value (), sign),
550 238107896 : widest_int::from (rh_bits.mask (), sign));
551 119053948 : break;
552 0 : default:
553 0 : gcc_unreachable ();
554 : }
555 :
556 177936386 : wide_int mask = wide_int::from (widest_mask, prec, sign);
557 355872772 : wide_int value = wide_int::from (widest_value, prec, sign);
558 : // Bitmasks must have the unknown value bits cleared.
559 177936386 : value &= ~mask;
560 355872772 : irange_bitmask bm (value, mask);
561 177936386 : r.update_bitmask (bm);
562 177937517 : }
563 :
564 : // Return the upper limit for a type.
565 :
566 : static inline wide_int
567 26689086 : max_limit (const_tree type)
568 : {
569 26689086 : return irange_val_max (type);
570 : }
571 :
572 : // Return the lower limit for a type.
573 :
574 : static inline wide_int
575 27119089 : min_limit (const_tree type)
576 : {
577 27119089 : return irange_val_min (type);
578 : }
579 :
580 : // Return false if shifting by OP is undefined behavior. Otherwise, return
581 : // true and the range it is to be shifted by. This allows trimming out of
582 : // undefined ranges, leaving only valid ranges if there are any.
583 :
584 : static inline bool
585 4703725 : get_shift_range (irange &r, tree type, const irange &op)
586 : {
587 4703725 : if (op.undefined_p ())
588 : return false;
589 :
590 : // Build valid range and intersect it with the shift range.
591 4702598 : r.set (op.type (),
592 9405196 : wi::shwi (0, TYPE_PRECISION (op.type ())),
593 4702598 : wi::shwi (TYPE_PRECISION (type) - 1, TYPE_PRECISION (op.type ())));
594 4702598 : r.intersect (op);
595 :
596 : // If there are no valid ranges in the shift range, returned false.
597 4702598 : if (r.undefined_p ())
598 624 : return false;
599 : return true;
600 : }
601 :
602 : // Default wide_int fold operation returns [MIN, MAX].
603 :
604 : void
605 346 : range_operator::wi_fold (irange &r, tree type,
606 : const wide_int &lh_lb ATTRIBUTE_UNUSED,
607 : const wide_int &lh_ub ATTRIBUTE_UNUSED,
608 : const wide_int &rh_lb ATTRIBUTE_UNUSED,
609 : const wide_int &rh_ub ATTRIBUTE_UNUSED) const
610 : {
611 346 : gcc_checking_assert (r.supports_type_p (type));
612 346 : r.set_varying (type);
613 346 : }
614 :
615 : // Call wi_fold when both op1 and op2 are equivalent. Further split small
616 : // subranges into constants. This can provide better precision.
617 : // For x + y, when x == y with a range of [0,4] instead of [0, 8] produce
618 : // [0,0][2, 2][4,4][6, 6][8, 8]
619 : // LIMIT is the maximum number of elements in range allowed before we
620 : // do not process them individually.
621 :
622 : void
623 55608 : range_operator::wi_fold_in_parts_equiv (irange &r, tree type,
624 : const wide_int &lh_lb,
625 : const wide_int &lh_ub,
626 : unsigned limit) const
627 : {
628 55608 : int_range_max tmp;
629 111216 : widest_int lh_range = wi::sub (widest_int::from (lh_ub, TYPE_SIGN (type)),
630 111216 : widest_int::from (lh_lb, TYPE_SIGN (type)));
631 : // if there are 1 to 8 values in the LH range, split them up.
632 55608 : r.set_undefined ();
633 111216 : if (lh_range >= 0 && lh_range < limit)
634 : {
635 16858 : for (unsigned x = 0; x <= lh_range; x++)
636 : {
637 11931 : wide_int val = lh_lb + x;
638 11931 : wi_fold (tmp, type, val, val, val, val);
639 11931 : r.union_ (tmp);
640 11931 : }
641 : }
642 : // Otherwise just call wi_fold.
643 : else
644 50681 : wi_fold (r, type, lh_lb, lh_ub, lh_lb, lh_ub);
645 55608 : }
646 :
647 : // Call wi_fold, except further split small subranges into constants.
648 : // This can provide better precision. For something 8 >> [0,1]
649 : // Instead of [8, 16], we will produce [8,8][16,16]
650 :
651 : void
652 162182557 : range_operator::wi_fold_in_parts (irange &r, tree type,
653 : const wide_int &lh_lb,
654 : const wide_int &lh_ub,
655 : const wide_int &rh_lb,
656 : const wide_int &rh_ub) const
657 : {
658 162182557 : int_range_max tmp;
659 324365114 : widest_int rh_range = wi::sub (widest_int::from (rh_ub, TYPE_SIGN (type)),
660 324365114 : widest_int::from (rh_lb, TYPE_SIGN (type)));
661 324365114 : widest_int lh_range = wi::sub (widest_int::from (lh_ub, TYPE_SIGN (type)),
662 324365114 : widest_int::from (lh_lb, TYPE_SIGN (type)));
663 : // If there are 2, 3, or 4 values in the RH range, do them separately.
664 : // Call wi_fold_in_parts to check the RH side.
665 162182557 : if (rh_range > 0 && rh_range < 4)
666 : {
667 5515453 : wi_fold_in_parts (r, type, lh_lb, lh_ub, rh_lb, rh_lb);
668 5515453 : if (rh_range > 1)
669 : {
670 732562 : wi_fold_in_parts (tmp, type, lh_lb, lh_ub, rh_lb + 1, rh_lb + 1);
671 732562 : r.union_ (tmp);
672 732562 : if (rh_range == 3)
673 : {
674 469156 : wi_fold_in_parts (tmp, type, lh_lb, lh_ub, rh_lb + 2, rh_lb + 2);
675 469156 : r.union_ (tmp);
676 : }
677 : }
678 5515453 : wi_fold_in_parts (tmp, type, lh_lb, lh_ub, rh_ub, rh_ub);
679 5515453 : r.union_ (tmp);
680 : }
681 : // Otherwise check for 2, 3, or 4 values in the LH range and split them up.
682 : // The RH side has been checked, so no recursion needed.
683 156667104 : else if (lh_range > 0 && lh_range < 4)
684 : {
685 10525406 : wi_fold (r, type, lh_lb, lh_lb, rh_lb, rh_ub);
686 10525406 : if (lh_range > 1)
687 : {
688 1937543 : wi_fold (tmp, type, lh_lb + 1, lh_lb + 1, rh_lb, rh_ub);
689 1937531 : r.union_ (tmp);
690 1937531 : if (lh_range == 3)
691 : {
692 852654 : wi_fold (tmp, type, lh_lb + 2, lh_lb + 2, rh_lb, rh_ub);
693 852648 : r.union_ (tmp);
694 : }
695 : }
696 10525406 : wi_fold (tmp, type, lh_ub, lh_ub, rh_lb, rh_ub);
697 10525406 : r.union_ (tmp);
698 : }
699 : // Otherwise just call wi_fold.
700 : else
701 146141698 : wi_fold (r, type, lh_lb, lh_ub, rh_lb, rh_ub);
702 162183200 : }
703 :
704 : // The default for fold is to break all ranges into sub-ranges and
705 : // invoke the wi_fold method on each sub-range pair.
706 :
707 : bool
708 114120302 : range_operator::fold_range (irange &r, tree type,
709 : const irange &lh,
710 : const irange &rh,
711 : relation_trio trio) const
712 : {
713 114120302 : gcc_checking_assert (r.supports_type_p (type));
714 114120302 : if (empty_range_varying (r, type, lh, rh))
715 57001 : return true;
716 :
717 114063301 : relation_kind rel = trio.op1_op2 ();
718 114063301 : unsigned num_lh = lh.num_pairs ();
719 114063301 : unsigned num_rh = rh.num_pairs ();
720 :
721 : // If op1 and op2 are equivalences, then we don't need a complete cross
722 : // product, just pairs of matching elements.
723 114063799 : if (relation_equiv_p (rel) && lh == rh)
724 : {
725 53140 : int_range_max tmp;
726 53140 : r.set_undefined ();
727 147307 : for (unsigned x = 0; x < num_lh; ++x)
728 : {
729 : // If the number of subranges is too high, limit subrange creation.
730 55608 : unsigned limit = (r.num_pairs () > 32) ? 0 : 8;
731 55608 : wide_int lh_lb = lh.lower_bound (x);
732 55608 : wide_int lh_ub = lh.upper_bound (x);
733 55608 : wi_fold_in_parts_equiv (tmp, type, lh_lb, lh_ub, limit);
734 55608 : r.union_ (tmp);
735 55608 : if (r.varying_p ())
736 : break;
737 55608 : }
738 53140 : op1_op2_relation_effect (r, type, lh, rh, rel);
739 53140 : update_bitmask (r, lh, rh);
740 53140 : return true;
741 53140 : }
742 :
743 : // If both ranges are single pairs, fold directly into the result range.
744 114010161 : if (num_lh == 1 && num_rh == 1)
745 : {
746 94252205 : wi_fold_in_parts (r, type, lh.lower_bound (), lh.upper_bound (),
747 188500566 : rh.lower_bound (), rh.upper_bound ());
748 94250283 : op1_op2_relation_effect (r, type, lh, rh, rel);
749 94250283 : update_bitmask (r, lh, rh);
750 94250283 : return true;
751 : }
752 :
753 : // If the number of subranges grows too high, produce a summary result as the
754 : // loop becomes exponential with little benefit. See PR 103821.
755 : // Rather than using [MIN, MAX] for both pairs, drop each to a maximum of
756 : // 3 subranges and try that. See PR 128983.
757 19759878 : if (num_lh * num_rh > 12)
758 : {
759 446903 : int_range<3> new_lh = lh;
760 446903 : int_range<3> new_rh = rh;
761 446903 : return range_operator::fold_range (r, type, new_lh, new_rh, trio);
762 446903 : }
763 :
764 19312975 : int_range_max tmp;
765 19312975 : r.set_undefined ();
766 78468430 : for (unsigned x = 0; x < num_lh; ++x)
767 95542130 : for (unsigned y = 0; y < num_rh; ++y)
768 : {
769 55699650 : wide_int lh_lb = lh.lower_bound (x);
770 55699650 : wide_int lh_ub = lh.upper_bound (x);
771 55699650 : wide_int rh_lb = rh.lower_bound (y);
772 55699650 : wide_int rh_ub = rh.upper_bound (y);
773 55699650 : wi_fold_in_parts (tmp, type, lh_lb, lh_ub, rh_lb, rh_ub);
774 55699650 : r.union_ (tmp);
775 55699650 : if (r.varying_p ())
776 : {
777 5074189 : op1_op2_relation_effect (r, type, lh, rh, rel);
778 5074189 : update_bitmask (r, lh, rh);
779 5074189 : return true;
780 : }
781 55704372 : }
782 14238786 : op1_op2_relation_effect (r, type, lh, rh, rel);
783 14238786 : update_bitmask (r, lh, rh);
784 14238786 : return true;
785 19312975 : }
786 :
787 :
788 : bool
789 75679 : range_operator::fold_range (frange &, tree, const irange &,
790 : const frange &, relation_trio) const
791 : {
792 75679 : return false;
793 : }
794 :
795 : bool
796 2387 : range_operator::op1_range (irange &, tree, const frange &,
797 : const irange &, relation_trio) const
798 : {
799 2387 : return false;
800 : }
801 :
802 :
803 :
804 : // The default for op1_range is to return false.
805 :
806 : bool
807 885379 : range_operator::op1_range (irange &r ATTRIBUTE_UNUSED,
808 : tree type ATTRIBUTE_UNUSED,
809 : const irange &lhs ATTRIBUTE_UNUSED,
810 : const irange &op2 ATTRIBUTE_UNUSED,
811 : relation_trio) const
812 : {
813 885379 : return false;
814 : }
815 :
816 : // The default for op2_range is to return false.
817 :
818 : bool
819 724152 : range_operator::op2_range (irange &r ATTRIBUTE_UNUSED,
820 : tree type ATTRIBUTE_UNUSED,
821 : const irange &lhs ATTRIBUTE_UNUSED,
822 : const irange &op1 ATTRIBUTE_UNUSED,
823 : relation_trio) const
824 : {
825 724152 : return false;
826 : }
827 :
828 : // The default relation routines return VREL_VARYING.
829 :
830 : relation_kind
831 20934082 : range_operator::lhs_op1_relation (const irange &lhs ATTRIBUTE_UNUSED,
832 : const irange &op1 ATTRIBUTE_UNUSED,
833 : const irange &op2 ATTRIBUTE_UNUSED,
834 : relation_kind rel ATTRIBUTE_UNUSED) const
835 : {
836 20934082 : return VREL_VARYING;
837 : }
838 :
839 : relation_kind
840 17912905 : range_operator::lhs_op2_relation (const irange &lhs ATTRIBUTE_UNUSED,
841 : const irange &op1 ATTRIBUTE_UNUSED,
842 : const irange &op2 ATTRIBUTE_UNUSED,
843 : relation_kind rel ATTRIBUTE_UNUSED) const
844 : {
845 17912905 : return VREL_VARYING;
846 : }
847 :
848 : relation_kind
849 25320481 : range_operator::op1_op2_relation (const irange &lhs ATTRIBUTE_UNUSED,
850 : const irange &op1 ATTRIBUTE_UNUSED,
851 : const irange &op2 ATTRIBUTE_UNUSED) const
852 : {
853 25320481 : return VREL_VARYING;
854 : }
855 :
856 : // Default is no relation affects the LHS.
857 :
858 : bool
859 67475647 : range_operator::op1_op2_relation_effect (irange &lhs_range ATTRIBUTE_UNUSED,
860 : tree type ATTRIBUTE_UNUSED,
861 : const irange &op1_range
862 : ATTRIBUTE_UNUSED,
863 : const irange &op2_range
864 : ATTRIBUTE_UNUSED,
865 : relation_kind rel
866 : ATTRIBUTE_UNUSED) const
867 : {
868 67475647 : return false;
869 : }
870 :
871 : bool
872 0 : range_operator::overflow_free_p (const irange &, const irange &,
873 : relation_trio) const
874 : {
875 0 : return false;
876 : }
877 :
878 : // Apply any known bitmask updates based on this operator.
879 :
880 : void
881 8391 : range_operator::update_bitmask (irange &, const irange &,
882 : const irange &) const
883 : {
884 8391 : }
885 :
886 : // Check that operand types are OK. Default to always OK.
887 :
888 : bool
889 132064927 : range_operator::operand_check_p (tree, tree, tree) const
890 : {
891 132064927 : return true;
892 : }
893 :
894 : // Create and return a range from a pair of wide-ints that are known
895 : // to have overflowed (or underflowed).
896 :
897 : static void
898 48396064 : value_range_from_overflowed_bounds (irange &r, tree type,
899 : const wide_int &wmin,
900 : const wide_int &wmax)
901 : {
902 48396064 : const signop sgn = TYPE_SIGN (type);
903 48396064 : const unsigned int prec = TYPE_PRECISION (type);
904 :
905 48396064 : wide_int tmin = wide_int::from (wmin, prec, sgn);
906 48396064 : wide_int tmax = wide_int::from (wmax, prec, sgn);
907 :
908 48396064 : bool covers = false;
909 48396064 : wide_int tem = tmin;
910 48396064 : tmin = tmax + 1;
911 48396064 : if (wi::cmp (tmin, tmax, sgn) < 0)
912 4148123 : covers = true;
913 48396064 : tmax = tem - 1;
914 48396064 : if (wi::cmp (tmax, tem, sgn) > 0)
915 : covers = true;
916 :
917 : // If the anti-range would cover nothing, drop to varying.
918 : // Likewise if the anti-range bounds are outside of the types
919 : // values.
920 44710473 : if (covers || wi::cmp (tmin, tmax, sgn) > 0)
921 32410877 : r.set_varying (type);
922 : else
923 15985187 : r.set (type, tmin, tmax, VR_ANTI_RANGE);
924 48396842 : }
925 :
926 : // Create and return a range from a pair of wide-ints. MIN_OVF and
927 : // MAX_OVF describe any overflow that might have occurred while
928 : // calculating WMIN and WMAX respectively.
929 :
930 : static void
931 158327950 : value_range_with_overflow (irange &r, tree type,
932 : const wide_int &wmin, const wide_int &wmax,
933 : wi::overflow_type min_ovf = wi::OVF_NONE,
934 : wi::overflow_type max_ovf = wi::OVF_NONE)
935 : {
936 158327950 : const signop sgn = TYPE_SIGN (type);
937 158327950 : const unsigned int prec = TYPE_PRECISION (type);
938 248956623 : const bool overflow_wraps = TYPE_OVERFLOW_WRAPS (type);
939 :
940 : // For one bit precision if max != min, then the range covers all
941 : // values.
942 173301918 : if (prec == 1 && wi::ne_p (wmax, wmin))
943 : {
944 0 : r.set_varying (type);
945 0 : return;
946 : }
947 :
948 158327950 : if (overflow_wraps)
949 : {
950 : // If overflow wraps, truncate the values and adjust the range,
951 : // kind, and bounds appropriately.
952 90628673 : if ((min_ovf != wi::OVF_NONE) == (max_ovf != wi::OVF_NONE))
953 : {
954 60121422 : wide_int tmin = wide_int::from (wmin, prec, sgn);
955 60121422 : wide_int tmax = wide_int::from (wmax, prec, sgn);
956 : // If the limits are swapped, we wrapped around and cover
957 : // the entire range.
958 60121422 : if (wi::gt_p (tmin, tmax, sgn))
959 589194 : r.set_varying (type);
960 : else
961 : // No overflow or both overflow or underflow. The range
962 : // kind stays normal.
963 59532228 : r.set (type, tmin, tmax);
964 60121422 : return;
965 60121647 : }
966 :
967 30507251 : if ((min_ovf == wi::OVF_UNDERFLOW && max_ovf == wi::OVF_NONE)
968 19447564 : || (max_ovf == wi::OVF_OVERFLOW && min_ovf == wi::OVF_NONE))
969 30507251 : value_range_from_overflowed_bounds (r, type, wmin, wmax);
970 : else
971 : // Other underflow and/or overflow, drop to VR_VARYING.
972 0 : r.set_varying (type);
973 : }
974 : else
975 : {
976 : // If both bounds either underflowed or overflowed, then the result
977 : // is undefined.
978 67699277 : if ((min_ovf == wi::OVF_OVERFLOW && max_ovf == wi::OVF_OVERFLOW)
979 67696445 : || (min_ovf == wi::OVF_UNDERFLOW && max_ovf == wi::OVF_UNDERFLOW))
980 : {
981 11969 : r.set_undefined ();
982 11969 : return;
983 : }
984 :
985 : // If overflow does not wrap, saturate to [MIN, MAX].
986 67687308 : wide_int new_lb, new_ub;
987 67687308 : if (min_ovf == wi::OVF_UNDERFLOW)
988 9582334 : new_lb = wi::min_value (prec, sgn);
989 58105325 : else if (min_ovf == wi::OVF_OVERFLOW)
990 0 : new_lb = wi::max_value (prec, sgn);
991 : else
992 58105325 : new_lb = wmin;
993 :
994 67687308 : if (max_ovf == wi::OVF_UNDERFLOW)
995 0 : new_ub = wi::min_value (prec, sgn);
996 67687308 : else if (max_ovf == wi::OVF_OVERFLOW)
997 14853013 : new_ub = wi::max_value (prec, sgn);
998 : else
999 52834694 : new_ub = wmax;
1000 :
1001 67687308 : r.set (type, new_lb, new_ub);
1002 67689223 : }
1003 : }
1004 :
1005 : // Create and return a range from a pair of wide-ints. Canonicalize
1006 : // the case where the bounds are swapped. In which case, we transform
1007 : // [10,5] into [MIN,5][10,MAX].
1008 :
1009 : static inline void
1010 91279355 : create_possibly_reversed_range (irange &r, tree type,
1011 : const wide_int &new_lb, const wide_int &new_ub)
1012 : {
1013 91279355 : signop s = TYPE_SIGN (type);
1014 : // If the bounds are swapped, treat the result as if an overflow occurred.
1015 91279355 : if (wi::gt_p (new_lb, new_ub, s))
1016 17888813 : value_range_from_overflowed_bounds (r, type, new_lb, new_ub);
1017 : else
1018 : // Otherwise it's just a normal range.
1019 73390542 : r.set (type, new_lb, new_ub);
1020 91279355 : }
1021 :
1022 : // Return the summary information about boolean range LHS. If EMPTY/FULL,
1023 : // return the equivalent range for TYPE in R; if FALSE/TRUE, do nothing.
1024 :
1025 : bool_range_state
1026 118016547 : get_bool_state (vrange &r, const vrange &lhs, tree val_type)
1027 : {
1028 : // If there is no result, then this is unexecutable.
1029 118016547 : if (lhs.undefined_p ())
1030 : {
1031 0 : r.set_undefined ();
1032 0 : return BRS_EMPTY;
1033 : }
1034 :
1035 118016547 : if (lhs.zero_p ())
1036 : return BRS_FALSE;
1037 :
1038 : // For TRUE, we can't just test for [1,1] because Ada can have
1039 : // multi-bit booleans, and TRUE values can be: [1, MAX], ~[0], etc.
1040 59805645 : if (lhs.contains_p (build_zero_cst (lhs.type ())))
1041 : {
1042 1621 : r.set_varying (val_type);
1043 1621 : return BRS_FULL;
1044 : }
1045 :
1046 : return BRS_TRUE;
1047 : }
1048 :
1049 : // ------------------------------------------------------------------------
1050 :
1051 : void
1052 0 : operator_equal::update_bitmask (irange &r, const irange &lh,
1053 : const irange &rh) const
1054 : {
1055 0 : update_known_bitmask (r, EQ_EXPR, lh, rh);
1056 0 : }
1057 :
1058 : // Check if the LHS range indicates a relation between OP1 and OP2.
1059 :
1060 : relation_kind
1061 7017644 : operator_equal::op1_op2_relation (const irange &lhs, const irange &,
1062 : const irange &) const
1063 : {
1064 7017644 : if (lhs.undefined_p ())
1065 : return VREL_UNDEFINED;
1066 :
1067 : // FALSE = op1 == op2 indicates NE_EXPR.
1068 7017644 : if (lhs.zero_p ())
1069 : return VREL_NE;
1070 :
1071 : // TRUE = op1 == op2 indicates EQ_EXPR.
1072 3950566 : if (!lhs.contains_zero_p ())
1073 3946882 : return VREL_EQ;
1074 : return VREL_VARYING;
1075 : }
1076 :
1077 : bool
1078 18379931 : operator_equal::fold_range (irange &r, tree type,
1079 : const irange &op1,
1080 : const irange &op2,
1081 : relation_trio rel) const
1082 : {
1083 18379931 : if (relop_early_resolve (r, type, op1, op2, rel, VREL_EQ))
1084 : return true;
1085 :
1086 : // We can be sure the values are always equal or not if both ranges
1087 : // consist of a single value, and then compare them.
1088 18336513 : bool op1_const = wi::eq_p (op1.lower_bound (), op1.upper_bound ());
1089 18336513 : bool op2_const = wi::eq_p (op2.lower_bound (), op2.upper_bound ());
1090 18336499 : if (op1_const && op2_const)
1091 : {
1092 460102 : if (wi::eq_p (op1.lower_bound (), op2.upper_bound()))
1093 187142 : r = range_true (type);
1094 : else
1095 272960 : r = range_false (type);
1096 : }
1097 : else
1098 : {
1099 : // If ranges do not intersect, we know the range is not equal,
1100 : // otherwise we don't know anything for sure.
1101 17876397 : int_range_max tmp = op1;
1102 17876397 : tmp.intersect (op2);
1103 17876397 : if (tmp.undefined_p ())
1104 260776 : r = range_false (type);
1105 : // Check if a constant cannot satisfy the bitmask requirements.
1106 32683578 : else if (op2_const && !op1.get_bitmask ().member_p (op2.lower_bound ()))
1107 0 : r = range_false (type);
1108 17708497 : else if (op1_const && !op2.get_bitmask ().member_p (op1.lower_bound ()))
1109 0 : r = range_false (type);
1110 : else
1111 17615621 : r = range_true_and_false (type);
1112 17876397 : }
1113 : return true;
1114 : }
1115 :
1116 : bool
1117 15299588 : operator_equal::op1_range (irange &r, tree type,
1118 : const irange &lhs,
1119 : const irange &op2,
1120 : relation_trio) const
1121 : {
1122 15299588 : switch (get_bool_state (r, lhs, type))
1123 : {
1124 5745722 : case BRS_TRUE:
1125 : // If it's true, the result is the same as OP2.
1126 5745722 : r = op2;
1127 5745722 : break;
1128 :
1129 9553783 : case BRS_FALSE:
1130 : // If the result is false, the only time we know anything is
1131 : // if OP2 is a constant.
1132 9553783 : if (!op2.undefined_p ()
1133 28661349 : && wi::eq_p (op2.lower_bound(), op2.upper_bound()))
1134 : {
1135 7734686 : r = op2;
1136 7734686 : if (!r.invert ())
1137 : return false;
1138 : }
1139 : else
1140 1819097 : r.set_varying (type);
1141 : break;
1142 :
1143 : default:
1144 : break;
1145 : }
1146 : return true;
1147 : }
1148 :
1149 : bool
1150 1918108 : operator_equal::op2_range (irange &r, tree type,
1151 : const irange &lhs,
1152 : const irange &op1,
1153 : relation_trio rel) const
1154 : {
1155 1918108 : return operator_equal::op1_range (r, type, lhs, op1, rel.swap_op1_op2 ());
1156 : }
1157 :
1158 : // -------------------------------------------------------------------------
1159 :
1160 : void
1161 0 : operator_not_equal::update_bitmask (irange &r, const irange &lh,
1162 : const irange &rh) const
1163 : {
1164 0 : update_known_bitmask (r, NE_EXPR, lh, rh);
1165 0 : }
1166 :
1167 : // Check if the LHS range indicates a relation between OP1 and OP2.
1168 :
1169 : relation_kind
1170 12300803 : operator_not_equal::op1_op2_relation (const irange &lhs, const irange &,
1171 : const irange &) const
1172 : {
1173 12300803 : if (lhs.undefined_p ())
1174 : return VREL_UNDEFINED;
1175 :
1176 : // FALSE = op1 != op2 indicates EQ_EXPR.
1177 12300803 : if (lhs.zero_p ())
1178 : return VREL_EQ;
1179 :
1180 : // TRUE = op1 != op2 indicates NE_EXPR.
1181 5725177 : if (!lhs.contains_zero_p ())
1182 5724956 : return VREL_NE;
1183 : return VREL_VARYING;
1184 : }
1185 :
1186 : bool
1187 28539114 : operator_not_equal::fold_range (irange &r, tree type,
1188 : const irange &op1,
1189 : const irange &op2,
1190 : relation_trio rel) const
1191 : {
1192 28539114 : if (relop_early_resolve (r, type, op1, op2, rel, VREL_NE))
1193 : return true;
1194 :
1195 : // We can be sure the values are always equal or not if both ranges
1196 : // consist of a single value, and then compare them.
1197 28516227 : bool op1_const = wi::eq_p (op1.lower_bound (), op1.upper_bound ());
1198 28516227 : bool op2_const = wi::eq_p (op2.lower_bound (), op2.upper_bound ());
1199 28515667 : if (op1_const && op2_const)
1200 : {
1201 2051634 : if (wi::ne_p (op1.lower_bound (), op2.upper_bound()))
1202 1385019 : r = range_true (type);
1203 : else
1204 666611 : r = range_false (type);
1205 : }
1206 : else
1207 : {
1208 : // If ranges do not intersect, we know the range is not equal,
1209 : // otherwise we don't know anything for sure.
1210 26464037 : int_range_max tmp = op1;
1211 26464037 : tmp.intersect (op2);
1212 26464037 : if (tmp.undefined_p ())
1213 320906 : r = range_true (type);
1214 : // Check if a constant cannot satisfy the bitmask requirements.
1215 47395475 : else if (op2_const && !op1.get_bitmask ().member_p (op2.lower_bound ()))
1216 0 : r = range_true (type);
1217 26295378 : else if (op1_const && !op2.get_bitmask ().member_p (op1.lower_bound ()))
1218 0 : r = range_true (type);
1219 : else
1220 26143131 : r = range_true_and_false (type);
1221 26464037 : }
1222 : return true;
1223 : }
1224 :
1225 : bool
1226 30706316 : operator_not_equal::op1_range (irange &r, tree type,
1227 : const irange &lhs,
1228 : const irange &op2,
1229 : relation_trio) const
1230 : {
1231 30706316 : switch (get_bool_state (r, lhs, type))
1232 : {
1233 17979534 : case BRS_TRUE:
1234 : // If the result is true, the only time we know anything is if
1235 : // OP2 is a constant.
1236 17979534 : if (!op2.undefined_p ()
1237 53938602 : && wi::eq_p (op2.lower_bound(), op2.upper_bound()))
1238 : {
1239 14973195 : r = op2;
1240 14973195 : if (!r.invert ())
1241 : return false;
1242 : }
1243 : else
1244 3006339 : r.set_varying (type);
1245 : break;
1246 :
1247 12725852 : case BRS_FALSE:
1248 : // If it's false, the result is the same as OP2.
1249 12725852 : r = op2;
1250 12725852 : break;
1251 :
1252 : default:
1253 : break;
1254 : }
1255 : return true;
1256 : }
1257 :
1258 :
1259 : bool
1260 3571237 : operator_not_equal::op2_range (irange &r, tree type,
1261 : const irange &lhs,
1262 : const irange &op1,
1263 : relation_trio rel) const
1264 : {
1265 3571237 : return operator_not_equal::op1_range (r, type, lhs, op1, rel.swap_op1_op2 ());
1266 : }
1267 :
1268 : // (X < VAL) produces the range of [MIN, VAL - 1].
1269 :
1270 : static void
1271 8984209 : build_lt (irange &r, tree type, const wide_int &val)
1272 : {
1273 8984209 : wi::overflow_type ov;
1274 8984209 : wide_int lim;
1275 8984209 : signop sgn = TYPE_SIGN (type);
1276 :
1277 : // Signed 1 bit cannot represent 1 for subtraction.
1278 8984209 : if (sgn == SIGNED)
1279 5979776 : lim = wi::add (val, -1, sgn, &ov);
1280 : else
1281 3004515 : lim = wi::sub (val, 1, sgn, &ov);
1282 :
1283 : // If val - 1 underflows, check if X < MIN, which is an empty range.
1284 8984209 : if (ov)
1285 235 : r.set_undefined ();
1286 : else
1287 8984056 : r = int_range<1> (type, min_limit (type), lim);
1288 8984209 : }
1289 :
1290 : // (X <= VAL) produces the range of [MIN, VAL].
1291 :
1292 : static void
1293 18135087 : build_le (irange &r, tree type, const wide_int &val)
1294 : {
1295 18135087 : r = int_range<1> (type, min_limit (type), val);
1296 18135087 : }
1297 :
1298 : // (X > VAL) produces the range of [VAL + 1, MAX].
1299 :
1300 : static void
1301 16153437 : build_gt (irange &r, tree type, const wide_int &val)
1302 : {
1303 16153437 : wi::overflow_type ov;
1304 16153437 : wide_int lim;
1305 16153437 : signop sgn = TYPE_SIGN (type);
1306 :
1307 : // Signed 1 bit cannot represent 1 for addition.
1308 16153437 : if (sgn == SIGNED)
1309 8447103 : lim = wi::sub (val, -1, sgn, &ov);
1310 : else
1311 7706536 : lim = wi::add (val, 1, sgn, &ov);
1312 : // If val + 1 overflows, check is for X > MAX, which is an empty range.
1313 16153437 : if (ov)
1314 4 : r.set_undefined ();
1315 : else
1316 16153635 : r = int_range<1> (type, lim, max_limit (type));
1317 16153437 : }
1318 :
1319 : // (X >= val) produces the range of [VAL, MAX].
1320 :
1321 : static void
1322 10535625 : build_ge (irange &r, tree type, const wide_int &val)
1323 : {
1324 10535625 : r = int_range<1> (type, val, max_limit (type));
1325 10535625 : }
1326 :
1327 :
1328 : void
1329 0 : operator_lt::update_bitmask (irange &r, const irange &lh,
1330 : const irange &rh) const
1331 : {
1332 0 : update_known_bitmask (r, LT_EXPR, lh, rh);
1333 0 : }
1334 :
1335 : // Check if the LHS range indicates a relation between OP1 and OP2.
1336 :
1337 : relation_kind
1338 15496268 : operator_lt::op1_op2_relation (const irange &lhs, const irange &,
1339 : const irange &) const
1340 : {
1341 15496268 : if (lhs.undefined_p ())
1342 : return VREL_UNDEFINED;
1343 :
1344 : // FALSE = op1 < op2 indicates GE_EXPR.
1345 15496268 : if (lhs.zero_p ())
1346 : return VREL_GE;
1347 :
1348 : // TRUE = op1 < op2 indicates LT_EXPR.
1349 8089889 : if (!lhs.contains_zero_p ())
1350 8089190 : return VREL_LT;
1351 : return VREL_VARYING;
1352 : }
1353 :
1354 : bool
1355 6997119 : operator_lt::fold_range (irange &r, tree type,
1356 : const irange &op1,
1357 : const irange &op2,
1358 : relation_trio rel) const
1359 : {
1360 6997119 : if (relop_early_resolve (r, type, op1, op2, rel, VREL_LT))
1361 : return true;
1362 :
1363 6955042 : signop sign = TYPE_SIGN (op1.type ());
1364 6955042 : gcc_checking_assert (sign == TYPE_SIGN (op2.type ()));
1365 :
1366 6955078 : if (wi::lt_p (op1.upper_bound (), op2.lower_bound (), sign))
1367 51238 : r = range_true (type);
1368 6903840 : else if (!wi::lt_p (op1.lower_bound (), op2.upper_bound (), sign))
1369 82569 : r = range_false (type);
1370 : // Use nonzero bits to determine if < 0 is false.
1371 8921265 : else if (op2.zero_p () && !wi::neg_p (op1.get_nonzero_bits (), sign))
1372 0 : r = range_false (type);
1373 : else
1374 6821235 : r = range_true_and_false (type);
1375 : return true;
1376 : }
1377 :
1378 : bool
1379 8287886 : operator_lt::op1_range (irange &r, tree type,
1380 : const irange &lhs,
1381 : const irange &op2,
1382 : relation_trio) const
1383 : {
1384 8287886 : if (op2.undefined_p ())
1385 : return false;
1386 :
1387 8287886 : switch (get_bool_state (r, lhs, type))
1388 : {
1389 3542562 : case BRS_TRUE:
1390 3542562 : build_lt (r, type, op2.upper_bound ());
1391 3542562 : break;
1392 :
1393 4745324 : case BRS_FALSE:
1394 4745324 : build_ge (r, type, op2.lower_bound ());
1395 4745324 : break;
1396 :
1397 : default:
1398 : break;
1399 : }
1400 : return true;
1401 : }
1402 :
1403 : bool
1404 5582750 : operator_lt::op2_range (irange &r, tree type,
1405 : const irange &lhs,
1406 : const irange &op1,
1407 : relation_trio) const
1408 : {
1409 5582750 : if (op1.undefined_p ())
1410 : return false;
1411 :
1412 5582750 : switch (get_bool_state (r, lhs, type))
1413 : {
1414 2730114 : case BRS_TRUE:
1415 2730114 : build_gt (r, type, op1.lower_bound ());
1416 2730114 : break;
1417 :
1418 2852630 : case BRS_FALSE:
1419 2852630 : build_le (r, type, op1.upper_bound ());
1420 2852630 : break;
1421 :
1422 : default:
1423 : break;
1424 : }
1425 : return true;
1426 : }
1427 :
1428 :
1429 : void
1430 0 : operator_le::update_bitmask (irange &r, const irange &lh,
1431 : const irange &rh) const
1432 : {
1433 0 : update_known_bitmask (r, LE_EXPR, lh, rh);
1434 0 : }
1435 :
1436 : // Check if the LHS range indicates a relation between OP1 and OP2.
1437 :
1438 : relation_kind
1439 6187700 : operator_le::op1_op2_relation (const irange &lhs, const irange &,
1440 : const irange &) const
1441 : {
1442 6187700 : if (lhs.undefined_p ())
1443 : return VREL_UNDEFINED;
1444 :
1445 : // FALSE = op1 <= op2 indicates GT_EXPR.
1446 6187700 : if (lhs.zero_p ())
1447 : return VREL_GT;
1448 :
1449 : // TRUE = op1 <= op2 indicates LE_EXPR.
1450 3565517 : if (!lhs.contains_zero_p ())
1451 3564284 : return VREL_LE;
1452 : return VREL_VARYING;
1453 : }
1454 :
1455 : bool
1456 5722363 : operator_le::fold_range (irange &r, tree type,
1457 : const irange &op1,
1458 : const irange &op2,
1459 : relation_trio rel) const
1460 : {
1461 5722363 : if (relop_early_resolve (r, type, op1, op2, rel, VREL_LE))
1462 : return true;
1463 :
1464 5707452 : signop sign = TYPE_SIGN (op1.type ());
1465 5707452 : gcc_checking_assert (sign == TYPE_SIGN (op2.type ()));
1466 :
1467 5707456 : if (wi::le_p (op1.upper_bound (), op2.lower_bound (), sign))
1468 295682 : r = range_true (type);
1469 5411774 : else if (!wi::le_p (op1.lower_bound (), op2.upper_bound (), sign))
1470 56175 : r = range_false (type);
1471 : else
1472 5355595 : r = range_true_and_false (type);
1473 : return true;
1474 : }
1475 :
1476 : bool
1477 9479609 : operator_le::op1_range (irange &r, tree type,
1478 : const irange &lhs,
1479 : const irange &op2,
1480 : relation_trio) const
1481 : {
1482 9479609 : if (op2.undefined_p ())
1483 : return false;
1484 :
1485 9479609 : switch (get_bool_state (r, lhs, type))
1486 : {
1487 4667208 : case BRS_TRUE:
1488 4667208 : build_le (r, type, op2.upper_bound ());
1489 4667208 : break;
1490 :
1491 4812401 : case BRS_FALSE:
1492 4812401 : build_gt (r, type, op2.lower_bound ());
1493 4812401 : break;
1494 :
1495 : default:
1496 : break;
1497 : }
1498 : return true;
1499 : }
1500 :
1501 : bool
1502 1681710 : operator_le::op2_range (irange &r, tree type,
1503 : const irange &lhs,
1504 : const irange &op1,
1505 : relation_trio) const
1506 : {
1507 1681710 : if (op1.undefined_p ())
1508 : return false;
1509 :
1510 1681710 : switch (get_bool_state (r, lhs, type))
1511 : {
1512 849315 : case BRS_TRUE:
1513 849315 : build_ge (r, type, op1.lower_bound ());
1514 849315 : break;
1515 :
1516 832393 : case BRS_FALSE:
1517 832393 : build_lt (r, type, op1.upper_bound ());
1518 832393 : break;
1519 :
1520 : default:
1521 : break;
1522 : }
1523 : return true;
1524 : }
1525 :
1526 :
1527 : void
1528 0 : operator_gt::update_bitmask (irange &r, const irange &lh,
1529 : const irange &rh) const
1530 : {
1531 0 : update_known_bitmask (r, GT_EXPR, lh, rh);
1532 0 : }
1533 :
1534 : // Check if the LHS range indicates a relation between OP1 and OP2.
1535 :
1536 : relation_kind
1537 15412292 : operator_gt::op1_op2_relation (const irange &lhs, const irange &,
1538 : const irange &) const
1539 : {
1540 15412292 : if (lhs.undefined_p ())
1541 : return VREL_UNDEFINED;
1542 :
1543 : // FALSE = op1 > op2 indicates LE_EXPR.
1544 15412292 : if (lhs.zero_p ())
1545 : return VREL_LE;
1546 :
1547 : // TRUE = op1 > op2 indicates GT_EXPR.
1548 8446546 : if (!lhs.contains_zero_p ())
1549 8444529 : return VREL_GT;
1550 : return VREL_VARYING;
1551 : }
1552 :
1553 : bool
1554 12196673 : operator_gt::fold_range (irange &r, tree type,
1555 : const irange &op1, const irange &op2,
1556 : relation_trio rel) const
1557 : {
1558 12196673 : if (relop_early_resolve (r, type, op1, op2, rel, VREL_GT))
1559 : return true;
1560 :
1561 12131731 : signop sign = TYPE_SIGN (op1.type ());
1562 12131731 : gcc_checking_assert (sign == TYPE_SIGN (op2.type ()));
1563 :
1564 12131757 : if (wi::gt_p (op1.lower_bound (), op2.upper_bound (), sign))
1565 143923 : r = range_true (type);
1566 11987834 : else if (!wi::gt_p (op1.upper_bound (), op2.lower_bound (), sign))
1567 491913 : r = range_false (type);
1568 : else
1569 11495895 : r = range_true_and_false (type);
1570 : return true;
1571 : }
1572 :
1573 : bool
1574 16890659 : operator_gt::op1_range (irange &r, tree type,
1575 : const irange &lhs, const irange &op2,
1576 : relation_trio) const
1577 : {
1578 16890659 : if (op2.undefined_p ())
1579 : return false;
1580 :
1581 16890659 : switch (get_bool_state (r, lhs, type))
1582 : {
1583 7377218 : case BRS_TRUE:
1584 7377218 : build_gt (r, type, op2.lower_bound ());
1585 7377218 : break;
1586 :
1587 9513200 : case BRS_FALSE:
1588 9513200 : build_le (r, type, op2.upper_bound ());
1589 9513200 : break;
1590 :
1591 : default:
1592 : break;
1593 : }
1594 : return true;
1595 : }
1596 :
1597 : bool
1598 4769303 : operator_gt::op2_range (irange &r, tree type,
1599 : const irange &lhs,
1600 : const irange &op1,
1601 : relation_trio) const
1602 : {
1603 4769303 : if (op1.undefined_p ())
1604 : return false;
1605 :
1606 4769303 : switch (get_bool_state (r, lhs, type))
1607 : {
1608 2851982 : case BRS_TRUE:
1609 2851982 : build_lt (r, type, op1.upper_bound ());
1610 2851982 : break;
1611 :
1612 1917318 : case BRS_FALSE:
1613 1917318 : build_ge (r, type, op1.lower_bound ());
1614 1917318 : break;
1615 :
1616 : default:
1617 : break;
1618 : }
1619 : return true;
1620 : }
1621 :
1622 :
1623 : void
1624 0 : operator_ge::update_bitmask (irange &r, const irange &lh,
1625 : const irange &rh) const
1626 : {
1627 0 : update_known_bitmask (r, GE_EXPR, lh, rh);
1628 0 : }
1629 :
1630 : // Check if the LHS range indicates a relation between OP1 and OP2.
1631 :
1632 : relation_kind
1633 5797706 : operator_ge::op1_op2_relation (const irange &lhs, const irange &,
1634 : const irange &) const
1635 : {
1636 5797706 : if (lhs.undefined_p ())
1637 : return VREL_UNDEFINED;
1638 :
1639 : // FALSE = op1 >= op2 indicates LT_EXPR.
1640 5797706 : if (lhs.zero_p ())
1641 : return VREL_LT;
1642 :
1643 : // TRUE = op1 >= op2 indicates GE_EXPR.
1644 2831578 : if (!lhs.contains_zero_p ())
1645 2830313 : return VREL_GE;
1646 : return VREL_VARYING;
1647 : }
1648 :
1649 : bool
1650 2873471 : operator_ge::fold_range (irange &r, tree type,
1651 : const irange &op1,
1652 : const irange &op2,
1653 : relation_trio rel) const
1654 : {
1655 2873471 : if (relop_early_resolve (r, type, op1, op2, rel, VREL_GE))
1656 : return true;
1657 :
1658 2860260 : signop sign = TYPE_SIGN (op1.type ());
1659 2860260 : gcc_checking_assert (sign == TYPE_SIGN (op2.type ()));
1660 :
1661 2860292 : if (wi::ge_p (op1.lower_bound (), op2.upper_bound (), sign))
1662 238680 : r = range_true (type);
1663 2621612 : else if (!wi::ge_p (op1.upper_bound (), op2.lower_bound (), sign))
1664 7363 : r = range_false (type);
1665 : else
1666 2614217 : r = range_true_and_false (type);
1667 : return true;
1668 : }
1669 :
1670 : bool
1671 4781185 : operator_ge::op1_range (irange &r, tree type,
1672 : const irange &lhs,
1673 : const irange &op2,
1674 : relation_trio) const
1675 : {
1676 4781185 : if (op2.undefined_p ())
1677 : return false;
1678 :
1679 4781185 : switch (get_bool_state (r, lhs, type))
1680 : {
1681 3023668 : case BRS_TRUE:
1682 3023668 : build_ge (r, type, op2.lower_bound ());
1683 3023668 : break;
1684 :
1685 1757272 : case BRS_FALSE:
1686 1757272 : build_lt (r, type, op2.upper_bound ());
1687 1757272 : break;
1688 :
1689 : default:
1690 : break;
1691 : }
1692 : return true;
1693 : }
1694 :
1695 : bool
1696 2335783 : operator_ge::op2_range (irange &r, tree type,
1697 : const irange &lhs,
1698 : const irange &op1,
1699 : relation_trio) const
1700 : {
1701 2335783 : if (op1.undefined_p ())
1702 : return false;
1703 :
1704 2335783 : switch (get_bool_state (r, lhs, type))
1705 : {
1706 1102049 : case BRS_TRUE:
1707 1102049 : build_le (r, type, op1.upper_bound ());
1708 1102049 : break;
1709 :
1710 1233704 : case BRS_FALSE:
1711 1233704 : build_gt (r, type, op1.lower_bound ());
1712 1233704 : break;
1713 :
1714 : default:
1715 : break;
1716 : }
1717 : return true;
1718 : }
1719 :
1720 :
1721 : void
1722 54451982 : operator_plus::update_bitmask (irange &r, const irange &lh,
1723 : const irange &rh) const
1724 : {
1725 54451982 : update_known_bitmask (r, PLUS_EXPR, lh, rh);
1726 54451982 : }
1727 :
1728 : // For unsigned wrapping addition lhs = op1 + op2, if the relation
1729 : // lhs >= op1 (or lhs > op1) holds and op2 has a positive lower bound,
1730 : // the addition could not have wrapped and therefore lhs >= op2_lb >= 1.
1731 : // Tighten the computed range accordingly. The symmetric case (lhs >= op2
1732 : // with op1_lb > 0) is handled for completeness.
1733 : //
1734 : // Note: this override is only applicable during GORI recomputations and
1735 : // refinements, where the LHS is an already-known value being intersected
1736 : // against. Under normal fold_range use the LHS is being freshly computed,
1737 : // and an LHS/operand relation would be circular.
1738 :
1739 : bool
1740 54485220 : operator_plus::fold_range (irange &r, tree type,
1741 : const irange &lh, const irange &rh,
1742 : relation_trio rel) const
1743 : {
1744 54485220 : if (!range_operator::fold_range (r, type, lh, rh, rel))
1745 : return false;
1746 :
1747 54485220 : if (r.undefined_p ()
1748 54484722 : || !TYPE_UNSIGNED (type)
1749 27048733 : || !TYPE_OVERFLOW_WRAPS (type)
1750 81533953 : || (rel.lhs_op1 () == VREL_VARYING && rel.lhs_op2 () == VREL_VARYING))
1751 : return true;
1752 :
1753 1919482 : const unsigned prec = TYPE_PRECISION (type);
1754 1919482 : const wide_int zero = wi::zero (prec);
1755 1919482 : const wide_int max = irange_val_max (type);
1756 :
1757 : // lhs >= op1 with op2 > 0 => no unsigned wrap => lhs >= op2_lb.
1758 1919482 : relation_kind lhs_op1 = rel.lhs_op1 ();
1759 3838964 : if ((lhs_op1 == VREL_GE || lhs_op1 == VREL_GT)
1760 477378 : && !rh.undefined_p ()
1761 2396860 : && wi::gt_p (rh.lower_bound (), zero, UNSIGNED))
1762 : {
1763 34254 : int_range<1> lb (type, rh.lower_bound (), max);
1764 34254 : r.intersect (lb);
1765 34254 : }
1766 :
1767 : // lhs >= op2 with op1 > 0 => no unsigned wrap => lhs >= op1_lb.
1768 1919482 : relation_kind lhs_op2 = rel.lhs_op2 ();
1769 3838964 : if ((lhs_op2 == VREL_GE || lhs_op2 == VREL_GT)
1770 781421 : && !lh.undefined_p ()
1771 2700952 : && wi::gt_p (lh.lower_bound (), zero, UNSIGNED))
1772 : {
1773 87875 : int_range<1> lb (type, lh.lower_bound (), max);
1774 87875 : r.intersect (lb);
1775 87875 : }
1776 :
1777 1919482 : return true;
1778 1919531 : }
1779 :
1780 : // Check to see if the range of OP2 indicates anything about the relation
1781 : // between LHS and OP1.
1782 :
1783 : relation_kind
1784 47838028 : operator_plus::lhs_op1_relation (const irange &lhs,
1785 : const irange &op1,
1786 : const irange &op2,
1787 : relation_kind) const
1788 : {
1789 47838028 : if (lhs.undefined_p () || op1.undefined_p () || op2.undefined_p ())
1790 : return VREL_VARYING;
1791 :
1792 47802615 : tree type = lhs.type ();
1793 47802615 : unsigned prec = TYPE_PRECISION (type);
1794 47802615 : wi::overflow_type ovf1, ovf2;
1795 47802615 : signop sign = TYPE_SIGN (type);
1796 :
1797 : // LHS = OP1 + 0 indicates LHS == OP1.
1798 47802615 : if (op2.zero_p ())
1799 : return VREL_EQ;
1800 :
1801 47567855 : if (TYPE_OVERFLOW_WRAPS (type))
1802 : {
1803 27100188 : wi::add (op1.lower_bound (), op2.lower_bound (), sign, &ovf1);
1804 27100422 : wi::add (op1.upper_bound (), op2.upper_bound (), sign, &ovf2);
1805 : }
1806 : else
1807 20468135 : ovf1 = ovf2 = wi::OVF_NONE;
1808 :
1809 : // Never wrapping additions.
1810 47567855 : if (!ovf1 && !ovf2)
1811 : {
1812 : // Positive op2 means lhs > op1.
1813 28021586 : if (wi::gt_p (op2.lower_bound (), wi::zero (prec), sign))
1814 : return VREL_GT;
1815 10626341 : if (wi::ge_p (op2.lower_bound (), wi::zero (prec), sign))
1816 : return VREL_GE;
1817 :
1818 : // Negative op2 means lhs < op1.
1819 8983450 : if (wi::lt_p (op2.upper_bound (), wi::zero (prec), sign))
1820 : return VREL_LT;
1821 5544640 : if (wi::le_p (op2.upper_bound (), wi::zero (prec), sign))
1822 : return VREL_LE;
1823 : }
1824 : // Always wrapping additions.
1825 19546660 : else if (ovf1 && ovf1 == ovf2)
1826 : {
1827 : // Positive op2 means lhs < op1.
1828 672049 : if (wi::gt_p (op2.lower_bound (), wi::zero (prec), sign))
1829 : return VREL_LT;
1830 20 : if (wi::ge_p (op2.lower_bound (), wi::zero (prec), sign))
1831 : return VREL_LE;
1832 :
1833 : // Negative op2 means lhs > op1.
1834 20 : if (wi::lt_p (op2.upper_bound (), wi::zero (prec), sign))
1835 : return VREL_GT;
1836 0 : if (wi::le_p (op2.upper_bound (), wi::zero (prec), sign))
1837 : return VREL_GE;
1838 : }
1839 :
1840 : // If op2 does not contain 0, then LHS and OP1 can never be equal.
1841 24406390 : if (!range_includes_zero_p (op2))
1842 12051900 : return VREL_NE;
1843 :
1844 : return VREL_VARYING;
1845 : }
1846 :
1847 : // PLUS is symmetrical, so we can simply call lhs_op1_relation with reversed
1848 : // operands.
1849 :
1850 : relation_kind
1851 8484298 : operator_plus::lhs_op2_relation (const irange &lhs, const irange &op1,
1852 : const irange &op2, relation_kind rel) const
1853 : {
1854 8484298 : return lhs_op1_relation (lhs, op2, op1, rel);
1855 : }
1856 :
1857 : void
1858 81142511 : operator_plus::wi_fold (irange &r, tree type,
1859 : const wide_int &lh_lb, const wide_int &lh_ub,
1860 : const wide_int &rh_lb, const wide_int &rh_ub) const
1861 : {
1862 81142511 : wi::overflow_type ov_lb, ov_ub;
1863 81142511 : signop s = TYPE_SIGN (type);
1864 81142511 : wide_int new_lb = wi::add (lh_lb, rh_lb, s, &ov_lb);
1865 81142511 : wide_int new_ub = wi::add (lh_ub, rh_ub, s, &ov_ub);
1866 81142511 : value_range_with_overflow (r, type, new_lb, new_ub, ov_lb, ov_ub);
1867 81142511 : }
1868 :
1869 : // Given addition or subtraction, determine the possible NORMAL ranges and
1870 : // OVERFLOW ranges given an OFFSET range. ADD_P is true for addition.
1871 : // Return the relation that exists between the LHS and OP1 in order for the
1872 : // NORMAL range to apply.
1873 : // a return value of VREL_VARYING means no ranges were applicable.
1874 :
1875 : static relation_kind
1876 2119186 : plus_minus_ranges (irange &r_ov, irange &r_normal, const irange &offset,
1877 : bool add_p)
1878 : {
1879 2119186 : relation_kind kind = VREL_VARYING;
1880 : // For now, only deal with constant adds. This could be extended to ranges
1881 : // when someone is so motivated.
1882 2119186 : if (!offset.singleton_p () || offset.zero_p ())
1883 : return kind;
1884 :
1885 : // Always work with a positive offset. ie a+ -2 -> a-2 and a- -2 > a+2
1886 18421 : wide_int off = offset.lower_bound ();
1887 18421 : if (wi::neg_p (off, SIGNED))
1888 : {
1889 1426 : add_p = !add_p;
1890 1426 : off = wi::neg (off);
1891 : }
1892 :
1893 18421 : wi::overflow_type ov;
1894 18421 : tree type = offset.type ();
1895 18421 : unsigned prec = TYPE_PRECISION (type);
1896 18421 : wide_int ub;
1897 18421 : wide_int lb;
1898 : // calculate the normal range and relation for the operation.
1899 18421 : if (add_p)
1900 : {
1901 : // [ 0 , INF - OFF]
1902 17085 : lb = wi::zero (prec);
1903 17085 : ub = wi::sub (irange_val_max (type), off, UNSIGNED, &ov);
1904 17085 : kind = VREL_GT;
1905 : }
1906 : else
1907 : {
1908 : // [ OFF, INF ]
1909 1336 : lb = off;
1910 1336 : ub = irange_val_max (type);
1911 1336 : kind = VREL_LT;
1912 : }
1913 18421 : int_range<2> normal_range (type, lb, ub);
1914 18421 : int_range<2> ov_range (type, lb, ub, VR_ANTI_RANGE);
1915 :
1916 18421 : r_ov = ov_range;
1917 18421 : r_normal = normal_range;
1918 18421 : return kind;
1919 18421 : }
1920 :
1921 : // Once op1 has been calculated by operator_plus or operator_minus, check
1922 : // to see if the relation passed causes any part of the calculation to
1923 : // be not possible. ie
1924 : // a_2 = b_3 + 1 with a_2 < b_3 can refine the range of b_3 to [INF, INF]
1925 : // and that further refines a_2 to [0, 0].
1926 : // R is the value of op1, OP2 is the offset being added/subtracted, REL is the
1927 : // relation between LHS relation OP1 and ADD_P is true for PLUS, false for
1928 : // MINUS. IF any adjustment can be made, R will reflect it.
1929 :
1930 : static void
1931 15726521 : adjust_op1_for_overflow (irange &r, const irange &op2, relation_kind rel,
1932 : bool add_p)
1933 : {
1934 15726521 : if (r.undefined_p ())
1935 : return;
1936 15726515 : tree type = r.type ();
1937 : // Check for unsigned overflow and calculate the overflow part.
1938 15726515 : signop s = TYPE_SIGN (type);
1939 15726515 : if (!TYPE_OVERFLOW_WRAPS (type) || s == SIGNED)
1940 : return;
1941 :
1942 : // Only work with <, <=, >, >= relations.
1943 8656778 : if (!relation_lt_le_gt_ge_p (rel))
1944 : return;
1945 :
1946 : // Get the ranges for this offset.
1947 2119186 : int_range_max normal, overflow;
1948 2119186 : relation_kind k = plus_minus_ranges (overflow, normal, op2, add_p);
1949 :
1950 : // VREL_VARYING means there are no adjustments.
1951 2119186 : if (k == VREL_VARYING)
1952 : return;
1953 :
1954 : // If the relations match use the normal range, otherwise use overflow range.
1955 18421 : if (relation_intersect (k, rel) == k)
1956 13276 : r.intersect (normal);
1957 : else
1958 5145 : r.intersect (overflow);
1959 : return;
1960 2119186 : }
1961 :
1962 : bool
1963 13475432 : operator_plus::op1_range (irange &r, tree type,
1964 : const irange &lhs,
1965 : const irange &op2,
1966 : relation_trio trio) const
1967 : {
1968 13475432 : if (lhs.undefined_p ())
1969 : return false;
1970 : // Start with the default operation.
1971 13475432 : range_op_handler minus (MINUS_EXPR);
1972 13475432 : if (!minus)
1973 : return false;
1974 13475432 : bool res = minus.fold_range (r, type, lhs, op2);
1975 13475432 : relation_kind rel = trio.lhs_op1 ();
1976 : // Check for a relation refinement.
1977 13475432 : if (res)
1978 13475432 : adjust_op1_for_overflow (r, op2, rel, true /* PLUS_EXPR */);
1979 : return res;
1980 : }
1981 :
1982 : bool
1983 5223102 : operator_plus::op2_range (irange &r, tree type,
1984 : const irange &lhs,
1985 : const irange &op1,
1986 : relation_trio rel) const
1987 : {
1988 5223102 : return op1_range (r, type, lhs, op1, rel.swap_op1_op2 ());
1989 : }
1990 :
1991 : class operator_widen_plus_signed : public range_operator
1992 : {
1993 : public:
1994 : virtual void wi_fold (irange &r, tree type,
1995 : const wide_int &lh_lb,
1996 : const wide_int &lh_ub,
1997 : const wide_int &rh_lb,
1998 : const wide_int &rh_ub) const;
1999 : } op_widen_plus_signed;
2000 :
2001 : void
2002 0 : operator_widen_plus_signed::wi_fold (irange &r, tree type,
2003 : const wide_int &lh_lb,
2004 : const wide_int &lh_ub,
2005 : const wide_int &rh_lb,
2006 : const wide_int &rh_ub) const
2007 : {
2008 0 : wi::overflow_type ov_lb, ov_ub;
2009 0 : signop s = TYPE_SIGN (type);
2010 :
2011 0 : wide_int lh_wlb
2012 0 : = wide_int::from (lh_lb, wi::get_precision (lh_lb) * 2, SIGNED);
2013 0 : wide_int lh_wub
2014 0 : = wide_int::from (lh_ub, wi::get_precision (lh_ub) * 2, SIGNED);
2015 0 : wide_int rh_wlb = wide_int::from (rh_lb, wi::get_precision (rh_lb) * 2, s);
2016 0 : wide_int rh_wub = wide_int::from (rh_ub, wi::get_precision (rh_ub) * 2, s);
2017 :
2018 0 : wide_int new_lb = wi::add (lh_wlb, rh_wlb, s, &ov_lb);
2019 0 : wide_int new_ub = wi::add (lh_wub, rh_wub, s, &ov_ub);
2020 :
2021 0 : r = int_range<2> (type, new_lb, new_ub);
2022 0 : }
2023 :
2024 : class operator_widen_plus_unsigned : public range_operator
2025 : {
2026 : public:
2027 : virtual void wi_fold (irange &r, tree type,
2028 : const wide_int &lh_lb,
2029 : const wide_int &lh_ub,
2030 : const wide_int &rh_lb,
2031 : const wide_int &rh_ub) const;
2032 : } op_widen_plus_unsigned;
2033 :
2034 : void
2035 0 : operator_widen_plus_unsigned::wi_fold (irange &r, tree type,
2036 : const wide_int &lh_lb,
2037 : const wide_int &lh_ub,
2038 : const wide_int &rh_lb,
2039 : const wide_int &rh_ub) const
2040 : {
2041 0 : wi::overflow_type ov_lb, ov_ub;
2042 0 : signop s = TYPE_SIGN (type);
2043 :
2044 0 : wide_int lh_wlb
2045 0 : = wide_int::from (lh_lb, wi::get_precision (lh_lb) * 2, UNSIGNED);
2046 0 : wide_int lh_wub
2047 0 : = wide_int::from (lh_ub, wi::get_precision (lh_ub) * 2, UNSIGNED);
2048 0 : wide_int rh_wlb = wide_int::from (rh_lb, wi::get_precision (rh_lb) * 2, s);
2049 0 : wide_int rh_wub = wide_int::from (rh_ub, wi::get_precision (rh_ub) * 2, s);
2050 :
2051 0 : wide_int new_lb = wi::add (lh_wlb, rh_wlb, s, &ov_lb);
2052 0 : wide_int new_ub = wi::add (lh_wub, rh_wub, s, &ov_ub);
2053 :
2054 0 : r = int_range<2> (type, new_lb, new_ub);
2055 0 : }
2056 :
2057 : void
2058 24189928 : operator_minus::update_bitmask (irange &r, const irange &lh,
2059 : const irange &rh) const
2060 : {
2061 24189928 : update_known_bitmask (r, MINUS_EXPR, lh, rh);
2062 24189928 : }
2063 :
2064 : void
2065 33677395 : operator_minus::wi_fold (irange &r, tree type,
2066 : const wide_int &lh_lb, const wide_int &lh_ub,
2067 : const wide_int &rh_lb, const wide_int &rh_ub) const
2068 : {
2069 33677395 : wi::overflow_type ov_lb, ov_ub;
2070 33677395 : signop s = TYPE_SIGN (type);
2071 33677395 : wide_int new_lb = wi::sub (lh_lb, rh_ub, s, &ov_lb);
2072 33677395 : wide_int new_ub = wi::sub (lh_ub, rh_lb, s, &ov_ub);
2073 33677395 : value_range_with_overflow (r, type, new_lb, new_ub, ov_lb, ov_ub);
2074 33677395 : }
2075 :
2076 :
2077 : // Return the relation between LHS and OP1 based on the relation between
2078 : // OP1 and OP2.
2079 :
2080 : relation_kind
2081 5225343 : operator_minus::lhs_op1_relation (const irange &, const irange &op1,
2082 : const irange &, relation_kind rel) const
2083 : {
2084 5225343 : if (!op1.undefined_p () && TYPE_SIGN (op1.type ()) == UNSIGNED)
2085 2609323 : switch (rel)
2086 : {
2087 69850 : case VREL_GT:
2088 69850 : case VREL_GE:
2089 69850 : return VREL_LE;
2090 : default:
2091 : break;
2092 : }
2093 : return VREL_VARYING;
2094 : }
2095 :
2096 : // Check to see if the relation REL between OP1 and OP2 has any effect on the
2097 : // LHS of the expression. If so, apply it to LHS_RANGE. This is a helper
2098 : // function for both MINUS_EXPR and POINTER_DIFF_EXPR.
2099 :
2100 : bool
2101 27031236 : minus_op1_op2_relation_effect (irange &lhs_range, tree type,
2102 : const irange &op1_range,
2103 : const irange &op2_range ATTRIBUTE_UNUSED,
2104 : relation_kind rel)
2105 : {
2106 27031236 : if (rel == VREL_VARYING)
2107 : return false;
2108 :
2109 345096 : int_range<2> rel_range;
2110 345096 : unsigned prec = TYPE_PRECISION (type);
2111 345096 : signop sgn = TYPE_SIGN (type);
2112 :
2113 : // == and != produce [0,0] and ~[0,0] regardless of wrapping.
2114 345096 : if (rel == VREL_EQ)
2115 10242 : rel_range = int_range<2> (type, wi::zero (prec), wi::zero (prec));
2116 334854 : else if (rel == VREL_NE)
2117 136176 : rel_range = int_range<2> (type, wi::zero (prec), wi::zero (prec),
2118 68088 : VR_ANTI_RANGE);
2119 266766 : else if (TYPE_UNSIGNED (type))
2120 : {
2121 71113 : wide_int ub1 = op1_range.upper_bound ();
2122 71113 : wide_int ub2 = op2_range.upper_bound ();
2123 71113 : switch (rel)
2124 : {
2125 : // op1 > op2, so the subtraction cannot wrap : [1, UB (op1)].
2126 36614 : case VREL_GT:
2127 : // [0, 0] > op2 is undefined, so just return.
2128 36614 : if (ub1 == 0)
2129 : return false;
2130 36614 : rel_range = int_range<2> (type, wi::one (prec), ub1);
2131 36614 : break;
2132 : // op1 >= op2 >= 0, so the subtraction cannot wrap : [0, UB (op1)].
2133 33200 : case VREL_GE:
2134 33200 : rel_range = int_range<2> (type, wi::zero (prec), ub1);
2135 33200 : break;
2136 : // op1 < op2, so the subtraction always wraps : [-UB (op2), -1].
2137 988 : case VREL_LT:
2138 : // op1 < [0, 0] is undefined, so just return.
2139 988 : if (ub2 == 0)
2140 : return false;
2141 1976 : rel_range = int_range<2> (type, wi::neg (ub2),
2142 988 : wi::minus_one (prec));
2143 988 : break;
2144 : // As VREL_LT, except that op1 == op2 also produces zero.
2145 122 : case VREL_LE:
2146 : // If UB (op2) is MAX_INT the result won't change, plus
2147 : // we dont want to construct an illegal [1, 0] range next.
2148 122 : if (ub2 == wi::max_value (prec, UNSIGNED))
2149 : return false;
2150 123 : rel_range = int_range<2> (type, wi::one (prec),
2151 41 : wi::sub (wi::minus_one (prec), ub2),
2152 41 : VR_ANTI_RANGE);
2153 41 : break;
2154 : default:
2155 : return false;
2156 : }
2157 71113 : }
2158 195653 : else if (TYPE_OVERFLOW_WRAPS (type))
2159 : {
2160 75832 : switch (rel)
2161 : {
2162 : // For wrapping signed values , if op1 > op2 or op1 < op2,
2163 : // then op1 - op2 can be restricted to ~[0, 0].
2164 18383 : case VREL_GT:
2165 18383 : case VREL_LT:
2166 36766 : rel_range = int_range<2> (type, wi::zero (prec), wi::zero (prec),
2167 18383 : VR_ANTI_RANGE);
2168 18383 : break;
2169 : default:
2170 : return false;
2171 : }
2172 : }
2173 : else
2174 : {
2175 119821 : switch (rel)
2176 : {
2177 : // op1 > op2, op1 - op2 can be restricted to [1, +INF]
2178 27748 : case VREL_GT:
2179 55496 : rel_range = int_range<2> (type, wi::one (prec),
2180 55496 : wi::max_value (prec, sgn));
2181 27748 : break;
2182 : // op1 >= op2, op1 - op2 can be restricted to [0, +INF]
2183 91060 : case VREL_GE:
2184 182120 : rel_range = int_range<2> (type, wi::zero (prec),
2185 182120 : wi::max_value (prec, sgn));
2186 91060 : break;
2187 : // op1 < op2, op1 - op2 can be restricted to [-INF, -1]
2188 588 : case VREL_LT:
2189 1176 : rel_range = int_range<2> (type, wi::min_value (prec, sgn),
2190 588 : wi::minus_one (prec));
2191 588 : break;
2192 : // op1 <= op2, op1 - op2 can be restricted to [-INF, 0]
2193 370 : case VREL_LE:
2194 740 : rel_range = int_range<2> (type, wi::min_value (prec, sgn),
2195 370 : wi::zero (prec));
2196 370 : break;
2197 : default:
2198 : return false;
2199 : }
2200 : }
2201 287322 : lhs_range.intersect (rel_range);
2202 287322 : return true;
2203 345096 : }
2204 :
2205 : // Check if the LHS range indicates a relation between OP1 and OP2.
2206 : // OP1 - OP2 == 0 implies OP1 == OP2 even with wrapping arithmetic.
2207 : // For signed types, when both OP1 and OP2 are both positive, infer
2208 : // a relationship if there were it is overflow free.
2209 :
2210 : relation_kind
2211 5639257 : operator_minus::op1_op2_relation (const irange &lhs, const irange &rhs1,
2212 : const irange &rhs2) const
2213 : {
2214 5639257 : if (lhs.undefined_p ())
2215 : return VREL_UNDEFINED;
2216 :
2217 5639257 : if (lhs.zero_p ())
2218 : return VREL_EQ;
2219 :
2220 5444427 : bool contains_z = lhs.contains_zero_p ();
2221 : // When a - b does not overflow and signed,
2222 : // we can figure out the relationship between
2223 : // a and b if the result is non-negative or non-positive.
2224 5444427 : if (!TYPE_UNSIGNED (lhs.type ())
2225 5444427 : && overflow_free_p (rhs1, rhs2))
2226 : {
2227 2219712 : if (lhs.nonnegative_p ())
2228 1137595 : return contains_z ? VREL_GE : VREL_GT;
2229 1082117 : if (lhs.nonpositive_p ())
2230 387094 : return contains_z ? VREL_LE : VREL_LT;
2231 : }
2232 :
2233 3919738 : if (!contains_z)
2234 1919963 : return VREL_NE;
2235 :
2236 : return VREL_VARYING;
2237 : }
2238 :
2239 : bool
2240 24189928 : operator_minus::op1_op2_relation_effect (irange &lhs_range, tree type,
2241 : const irange &op1_range,
2242 : const irange &op2_range,
2243 : relation_kind rel) const
2244 : {
2245 24189928 : return minus_op1_op2_relation_effect (lhs_range, type, op1_range, op2_range,
2246 24189928 : rel);
2247 : }
2248 :
2249 : bool
2250 2251089 : operator_minus::op1_range (irange &r, tree type,
2251 : const irange &lhs,
2252 : const irange &op2,
2253 : relation_trio trio) const
2254 : {
2255 2251089 : if (lhs.undefined_p ())
2256 : return false;
2257 : // Start with the default operation.
2258 2251089 : range_op_handler minus (PLUS_EXPR);
2259 2251089 : if (!minus)
2260 : return false;
2261 2251089 : bool res = minus.fold_range (r, type, lhs, op2);
2262 2251089 : relation_kind rel = trio.lhs_op1 ();
2263 2251089 : if (res)
2264 2251089 : adjust_op1_for_overflow (r, op2, rel, false /* PLUS_EXPR */);
2265 : return res;
2266 :
2267 : }
2268 :
2269 : bool
2270 2490619 : operator_minus::op2_range (irange &r, tree type,
2271 : const irange &lhs,
2272 : const irange &op1,
2273 : relation_trio) const
2274 : {
2275 2490619 : if (lhs.undefined_p ())
2276 : return false;
2277 2490619 : return fold_range (r, type, op1, lhs);
2278 : }
2279 :
2280 : void
2281 978161 : operator_min::update_bitmask (irange &r, const irange &lh,
2282 : const irange &rh) const
2283 : {
2284 978161 : update_known_bitmask (r, MIN_EXPR, lh, rh);
2285 978161 : }
2286 :
2287 : void
2288 1588593 : operator_min::wi_fold (irange &r, tree type,
2289 : const wide_int &lh_lb, const wide_int &lh_ub,
2290 : const wide_int &rh_lb, const wide_int &rh_ub) const
2291 : {
2292 1588593 : signop s = TYPE_SIGN (type);
2293 1588593 : wide_int new_lb = wi::min (lh_lb, rh_lb, s);
2294 1588593 : wide_int new_ub = wi::min (lh_ub, rh_ub, s);
2295 1588593 : value_range_with_overflow (r, type, new_lb, new_ub);
2296 1588593 : }
2297 :
2298 : // When op1 <= op2, MIN (op1, op2) is op1.
2299 :
2300 : relation_kind
2301 978219 : operator_min::lhs_op1_relation (const irange &lhs, const irange &op1,
2302 : const irange &op2, relation_kind rel) const
2303 : {
2304 978219 : if (lhs.undefined_p () || op1.undefined_p () || op2.undefined_p ())
2305 : return VREL_VARYING;
2306 1955388 : if (rel == VREL_LE || rel == VREL_LT || rel == VREL_EQ
2307 3896214 : || wi::le_p (op1.upper_bound (), op2.lower_bound (),
2308 972840 : TYPE_SIGN (op1.type ())))
2309 13505 : return VREL_EQ;
2310 : return VREL_VARYING;
2311 : }
2312 :
2313 : // Same as above, but for op2.
2314 :
2315 : relation_kind
2316 664804 : operator_min::lhs_op2_relation (const irange &lhs, const irange &op1,
2317 : const irange &op2, relation_kind rel) const
2318 : {
2319 664804 : if (lhs.undefined_p () || op1.undefined_p () || op2.undefined_p ())
2320 : return VREL_VARYING;
2321 1328760 : if (rel == VREL_GE || rel == VREL_GT || rel == VREL_EQ
2322 2651361 : || wi::le_p (op2.upper_bound (), op1.lower_bound (),
2323 662327 : TYPE_SIGN (op1.type ())))
2324 4168 : return VREL_EQ;
2325 : return VREL_VARYING;
2326 : }
2327 :
2328 :
2329 : void
2330 770696 : operator_max::update_bitmask (irange &r, const irange &lh,
2331 : const irange &rh) const
2332 : {
2333 770696 : update_known_bitmask (r, MAX_EXPR, lh, rh);
2334 770696 : }
2335 :
2336 : void
2337 958240 : operator_max::wi_fold (irange &r, tree type,
2338 : const wide_int &lh_lb, const wide_int &lh_ub,
2339 : const wide_int &rh_lb, const wide_int &rh_ub) const
2340 : {
2341 958240 : signop s = TYPE_SIGN (type);
2342 958240 : wide_int new_lb = wi::max (lh_lb, rh_lb, s);
2343 958240 : wide_int new_ub = wi::max (lh_ub, rh_ub, s);
2344 958240 : value_range_with_overflow (r, type, new_lb, new_ub);
2345 958240 : }
2346 :
2347 : // When op1 >= op2, MAX (op1, op2) is op1.
2348 :
2349 : relation_kind
2350 770918 : operator_max::lhs_op1_relation (const irange &lhs, const irange &op1,
2351 : const irange &op2, relation_kind rel) const
2352 : {
2353 770918 : if (lhs.undefined_p () || op1.undefined_p () || op2.undefined_p ())
2354 : return VREL_VARYING;
2355 1540938 : if (rel == VREL_GE || rel == VREL_GT || rel == VREL_EQ
2356 3081399 : || wi::ge_p (op1.lower_bound (), op2.upper_bound (),
2357 770310 : TYPE_SIGN (op1.type ())))
2358 56823 : return VREL_EQ;
2359 : return VREL_VARYING;
2360 : }
2361 :
2362 : // Same as above, but for op2.
2363 :
2364 : relation_kind
2365 52821 : operator_max::lhs_op2_relation (const irange &lhs, const irange &op1,
2366 : const irange &op2, relation_kind rel) const
2367 : {
2368 52821 : if (lhs.undefined_p () || op1.undefined_p () || op2.undefined_p ())
2369 : return VREL_VARYING;
2370 105628 : if (rel == VREL_LE || rel == VREL_LT || rel == VREL_EQ
2371 210707 : || wi::ge_p (op2.lower_bound (), op1.upper_bound (),
2372 52631 : TYPE_SIGN (op1.type ())))
2373 275 : return VREL_EQ;
2374 : return VREL_VARYING;
2375 : }
2376 :
2377 :
2378 : // Calculate the cross product of two sets of ranges and return it.
2379 : //
2380 : // Multiplications, divisions and shifts are a bit tricky to handle,
2381 : // depending on the mix of signs we have in the two ranges, we need to
2382 : // operate on different values to get the minimum and maximum values
2383 : // for the new range. One approach is to figure out all the
2384 : // variations of range combinations and do the operations.
2385 : //
2386 : // However, this involves several calls to compare_values and it is
2387 : // pretty convoluted. It's simpler to do the 4 operations (MIN0 OP
2388 : // MIN1, MIN0 OP MAX1, MAX0 OP MIN1 and MAX0 OP MAX0 OP MAX1) and then
2389 : // figure the smallest and largest values to form the new range.
2390 :
2391 : void
2392 16054016 : cross_product_operator::wi_cross_product (irange &r, tree type,
2393 : const wide_int &lh_lb,
2394 : const wide_int &lh_ub,
2395 : const wide_int &rh_lb,
2396 : const wide_int &rh_ub) const
2397 : {
2398 16054016 : wide_int cp1, cp2, cp3, cp4;
2399 : // Default to varying.
2400 16054016 : r.set_varying (type);
2401 :
2402 : // Compute the 4 cross operations, bailing if we get an overflow we
2403 : // can't handle.
2404 16054016 : if (wi_op_overflows (cp1, type, lh_lb, rh_lb))
2405 : return;
2406 16053970 : if (wi::eq_p (lh_lb, lh_ub))
2407 4827493 : cp3 = cp1;
2408 11226477 : else if (wi_op_overflows (cp3, type, lh_ub, rh_lb))
2409 : return;
2410 16053970 : if (wi::eq_p (rh_lb, rh_ub))
2411 12333467 : cp2 = cp1;
2412 3720503 : else if (wi_op_overflows (cp2, type, lh_lb, rh_ub))
2413 : return;
2414 16051907 : if (wi::eq_p (lh_lb, lh_ub))
2415 4827475 : cp4 = cp2;
2416 11224432 : else if (wi_op_overflows (cp4, type, lh_ub, rh_ub))
2417 : return;
2418 :
2419 : // Order pairs.
2420 16051907 : signop sign = TYPE_SIGN (type);
2421 16051907 : if (wi::gt_p (cp1, cp2, sign))
2422 1705859 : std::swap (cp1, cp2);
2423 16051907 : if (wi::gt_p (cp3, cp4, sign))
2424 1703160 : std::swap (cp3, cp4);
2425 :
2426 : // Choose min and max from the ordered pairs.
2427 16051907 : wide_int res_lb = wi::min (cp1, cp3, sign);
2428 16051907 : wide_int res_ub = wi::max (cp2, cp4, sign);
2429 16051907 : value_range_with_overflow (r, type, res_lb, res_ub);
2430 16061281 : }
2431 :
2432 :
2433 : void
2434 15030613 : operator_mult::update_bitmask (irange &r, const irange &lh,
2435 : const irange &rh) const
2436 : {
2437 15030613 : update_known_bitmask (r, MULT_EXPR, lh, rh);
2438 15030613 : }
2439 :
2440 : bool
2441 1263443 : operator_mult::op1_range (irange &r, tree type,
2442 : const irange &lhs, const irange &op2,
2443 : relation_trio) const
2444 : {
2445 1263443 : if (lhs.undefined_p ())
2446 : return false;
2447 :
2448 : // We can't solve 0 = OP1 * N by dividing by N with a wrapping type.
2449 : // For example: For 0 = OP1 * 2, OP1 could be 0, or MAXINT, whereas
2450 : // for 4 = OP1 * 2, OP1 could be 2 or 130 (unsigned 8-bit)
2451 1263443 : if (TYPE_OVERFLOW_WRAPS (type))
2452 : return false;
2453 :
2454 543199 : wide_int offset;
2455 543199 : if (op2.singleton_p (offset) && offset != 0)
2456 312797 : return range_op_handler (TRUNC_DIV_EXPR).fold_range (r, type, lhs, op2);
2457 :
2458 : // ~[0, 0] = op1 * op2 defines op1 and op2 as non-zero.
2459 230402 : if (!lhs.contains_p (wi::zero (TYPE_PRECISION (lhs.type ()))))
2460 : {
2461 108055 : r.set_nonzero (type);
2462 108055 : return true;
2463 : }
2464 : return false;
2465 543199 : }
2466 :
2467 : bool
2468 251515 : operator_mult::op2_range (irange &r, tree type,
2469 : const irange &lhs, const irange &op1,
2470 : relation_trio rel) const
2471 : {
2472 251515 : return operator_mult::op1_range (r, type, lhs, op1, rel.swap_op1_op2 ());
2473 : }
2474 :
2475 : bool
2476 17483130 : operator_mult::wi_op_overflows (wide_int &res, tree type,
2477 : const wide_int &w0, const wide_int &w1) const
2478 : {
2479 17483130 : wi::overflow_type overflow = wi::OVF_NONE;
2480 17483130 : signop sign = TYPE_SIGN (type);
2481 17483130 : res = wi::mul (w0, w1, sign, &overflow);
2482 17483130 : if (overflow && TYPE_OVERFLOW_UNDEFINED (type))
2483 : {
2484 : // For multiplication, the sign of the overflow is given
2485 : // by the comparison of the signs of the operands.
2486 7726495 : if (sign == UNSIGNED || w0.sign_mask () == w1.sign_mask ())
2487 4287993 : res = wi::max_value (w0.get_precision (), sign);
2488 : else
2489 3438732 : res = wi::min_value (w0.get_precision (), sign);
2490 : return false;
2491 : }
2492 9756635 : return overflow;
2493 : }
2494 :
2495 : void
2496 19422520 : operator_mult::wi_fold (irange &r, tree type,
2497 : const wide_int &lh_lb, const wide_int &lh_ub,
2498 : const wide_int &rh_lb, const wide_int &rh_ub) const
2499 : {
2500 19422520 : if (TYPE_OVERFLOW_UNDEFINED (type))
2501 : {
2502 7224279 : wi_cross_product (r, type, lh_lb, lh_ub, rh_lb, rh_ub);
2503 7224279 : return;
2504 : }
2505 :
2506 : // Multiply the ranges when overflow wraps. This is basically fancy
2507 : // code so we don't drop to varying with an unsigned
2508 : // [-3,-1]*[-3,-1].
2509 : //
2510 : // This test requires 2*prec bits if both operands are signed and
2511 : // 2*prec + 2 bits if either is not. Therefore, extend the values
2512 : // using the sign of the result to PREC2. From here on out,
2513 : // everything is just signed math no matter what the input types
2514 : // were.
2515 :
2516 12198241 : signop sign = TYPE_SIGN (type);
2517 12198241 : unsigned prec = TYPE_PRECISION (type);
2518 12198241 : widest2_int min0 = widest2_int::from (lh_lb, sign);
2519 12198241 : widest2_int max0 = widest2_int::from (lh_ub, sign);
2520 12198241 : widest2_int min1 = widest2_int::from (rh_lb, sign);
2521 12198241 : widest2_int max1 = widest2_int::from (rh_ub, sign);
2522 12198241 : widest2_int sizem1 = wi::mask <widest2_int> (prec, false);
2523 12198241 : widest2_int size = sizem1 + 1;
2524 :
2525 : // Canonicalize the intervals.
2526 12198241 : if (sign == UNSIGNED)
2527 : {
2528 11454968 : if (wi::ltu_p (size, min0 + max0))
2529 : {
2530 1544022 : min0 -= size;
2531 1544022 : max0 -= size;
2532 : }
2533 11454931 : if (wi::ltu_p (size, min1 + max1))
2534 : {
2535 174903 : min1 -= size;
2536 174903 : max1 -= size;
2537 : }
2538 : }
2539 :
2540 : // Sort the 4 products so that min is in prod0 and max is in
2541 : // prod3.
2542 12198241 : widest2_int prod0 = min0 * min1;
2543 12198241 : widest2_int prod1 = min0 * max1;
2544 12198241 : widest2_int prod2 = max0 * min1;
2545 12198241 : widest2_int prod3 = max0 * max1;
2546 :
2547 : // min0min1 > max0max1
2548 12198241 : if (prod0 > prod3)
2549 199527 : std::swap (prod0, prod3);
2550 :
2551 : // min0max1 > max0min1
2552 12198241 : if (prod1 > prod2)
2553 342003 : std::swap (prod1, prod2);
2554 :
2555 12198241 : if (prod0 > prod1)
2556 77992 : std::swap (prod0, prod1);
2557 :
2558 12198241 : if (prod2 > prod3)
2559 4922 : std::swap (prod2, prod3);
2560 :
2561 : // diff = max - min
2562 12198241 : prod2 = prod3 - prod0;
2563 12198241 : if (wi::geu_p (prod2, sizem1))
2564 : {
2565 : // Multiplying by X, where X is a power of 2 is [0,0][X,+INF].
2566 8210631 : if (TYPE_UNSIGNED (type) && rh_lb == rh_ub
2567 7514245 : && wi::exact_log2 (rh_lb) != -1 && prec > 1)
2568 : {
2569 2801835 : r.set (type, rh_lb, wi::max_value (prec, sign));
2570 2801835 : int_range<2> zero;
2571 2801835 : zero.set_zero (type);
2572 2801835 : r.union_ (zero);
2573 2801835 : }
2574 : else
2575 : // The range covers all values.
2576 1375933 : r.set_varying (type);
2577 : }
2578 : else
2579 : {
2580 8020473 : wide_int new_lb = wide_int::from (prod0, prec, sign);
2581 8020473 : wide_int new_ub = wide_int::from (prod3, prec, sign);
2582 8020473 : create_possibly_reversed_range (r, type, new_lb, new_ub);
2583 8020542 : }
2584 12199520 : }
2585 :
2586 : bool
2587 15030613 : operator_mult::op1_op2_relation_effect (irange &lhs_range, tree type,
2588 : const irange &,
2589 : const irange &,
2590 : relation_kind rel) const
2591 : {
2592 : // a*a is nonnegative without overflow.
2593 : // tree_binary_nonnegative_p handles this in a similar way.
2594 15030613 : if (rel == VREL_EQ
2595 15030613 : && TYPE_OVERFLOW_UNDEFINED (type))
2596 : {
2597 35555 : int_range<2> nonnegative;
2598 35555 : nonnegative.set_nonnegative (type);
2599 35555 : lhs_range.intersect (nonnegative);
2600 35555 : return true;
2601 35555 : }
2602 : return false;
2603 : }
2604 :
2605 : class operator_widen_mult_signed : public range_operator
2606 : {
2607 : public:
2608 : virtual void wi_fold (irange &r, tree type,
2609 : const wide_int &lh_lb,
2610 : const wide_int &lh_ub,
2611 : const wide_int &rh_lb,
2612 : const wide_int &rh_ub)
2613 : const;
2614 : } op_widen_mult_signed;
2615 :
2616 : void
2617 1331 : operator_widen_mult_signed::wi_fold (irange &r, tree type,
2618 : const wide_int &lh_lb,
2619 : const wide_int &lh_ub,
2620 : const wide_int &rh_lb,
2621 : const wide_int &rh_ub) const
2622 : {
2623 1331 : wide_int lh_wlb = wide_int::from (lh_lb, TYPE_PRECISION (type), SIGNED);
2624 1331 : wide_int lh_wub = wide_int::from (lh_ub, TYPE_PRECISION (type), SIGNED);
2625 1331 : wide_int rh_wlb = wide_int::from (rh_lb, TYPE_PRECISION (type), SIGNED);
2626 1331 : wide_int rh_wub = wide_int::from (rh_ub, TYPE_PRECISION (type), SIGNED);
2627 :
2628 : /* We don't expect a widening multiplication to be able to overflow but range
2629 : calculations for multiplications are complicated. After widening the
2630 : operands lets call the base class. */
2631 1331 : return op_mult.wi_fold (r, type, lh_wlb, lh_wub, rh_wlb, rh_wub);
2632 1331 : }
2633 :
2634 : class operator_widen_mult_unsigned : public range_operator
2635 : {
2636 : public:
2637 : virtual void wi_fold (irange &r, tree type,
2638 : const wide_int &lh_lb,
2639 : const wide_int &lh_ub,
2640 : const wide_int &rh_lb,
2641 : const wide_int &rh_ub)
2642 : const;
2643 : } op_widen_mult_unsigned;
2644 :
2645 : void
2646 7663 : operator_widen_mult_unsigned::wi_fold (irange &r, tree type,
2647 : const wide_int &lh_lb,
2648 : const wide_int &lh_ub,
2649 : const wide_int &rh_lb,
2650 : const wide_int &rh_ub) const
2651 : {
2652 7663 : wide_int lh_wlb = wide_int::from (lh_lb, TYPE_PRECISION (type), UNSIGNED);
2653 7663 : wide_int lh_wub = wide_int::from (lh_ub, TYPE_PRECISION (type), UNSIGNED);
2654 7663 : wide_int rh_wlb = wide_int::from (rh_lb, TYPE_PRECISION (type), UNSIGNED);
2655 7663 : wide_int rh_wub = wide_int::from (rh_ub, TYPE_PRECISION (type), UNSIGNED);
2656 :
2657 : /* We don't expect a widening multiplication to be able to overflow but range
2658 : calculations for multiplications are complicated. After widening the
2659 : operands lets call the base class. */
2660 7663 : return op_mult.wi_fold (r, type, lh_wlb, lh_wub, rh_wlb, rh_wub);
2661 7663 : }
2662 :
2663 : class operator_widen_mult_signed_unsigned : public range_operator
2664 : {
2665 : public:
2666 : virtual void wi_fold (irange &r, tree type,
2667 : const wide_int &lh_lb,
2668 : const wide_int &lh_ub,
2669 : const wide_int &rh_lb,
2670 : const wide_int &rh_ub)
2671 : const;
2672 : } op_widen_mult_signed_unsigned;
2673 :
2674 : void
2675 0 : operator_widen_mult_signed_unsigned::wi_fold (irange &r, tree type,
2676 : const wide_int &lh_lb,
2677 : const wide_int &lh_ub,
2678 : const wide_int &rh_lb,
2679 : const wide_int &rh_ub) const
2680 : {
2681 0 : wide_int lh_wlb = wide_int::from (lh_lb, TYPE_PRECISION (type), SIGNED);
2682 0 : wide_int lh_wub = wide_int::from (lh_ub, TYPE_PRECISION (type), SIGNED);
2683 0 : wide_int rh_wlb = wide_int::from (rh_lb, TYPE_PRECISION (type), UNSIGNED);
2684 0 : wide_int rh_wub = wide_int::from (rh_ub, TYPE_PRECISION (type), UNSIGNED);
2685 :
2686 : /* We don't expect a widening multiplication to be able to overflow but range
2687 : calculations for multiplications are complicated. After widening the
2688 : operands lets call the base class. */
2689 0 : return op_mult.wi_fold (r, type, lh_wlb, lh_wub, rh_wlb, rh_wub);
2690 0 : }
2691 :
2692 : class operator_div : public cross_product_operator
2693 : {
2694 : using range_operator::update_bitmask;
2695 : using range_operator::op2_range;
2696 : using range_operator::op1_op2_relation_effect;
2697 : public:
2698 : operator_div (tree_code div_kind) { m_code = div_kind; }
2699 : bool op2_range (irange &r, tree type, const irange &lhs, const irange &,
2700 : relation_trio) const final override;
2701 : virtual void wi_fold (irange &r, tree type,
2702 : const wide_int &lh_lb,
2703 : const wide_int &lh_ub,
2704 : const wide_int &rh_lb,
2705 : const wide_int &rh_ub) const final override;
2706 : virtual bool wi_op_overflows (wide_int &res, tree type,
2707 : const wide_int &, const wide_int &)
2708 : const final override;
2709 : bool op1_op2_relation_effect (irange &lhs_range,
2710 : tree type,
2711 : const irange &op1_range,
2712 : const irange &op2_range,
2713 : relation_kind rel) const final override;
2714 3028096 : void update_bitmask (irange &r, const irange &lh, const irange &rh)
2715 : const final override
2716 3028096 : { update_known_bitmask (r, m_code, lh, rh); }
2717 : protected:
2718 : tree_code m_code;
2719 : };
2720 :
2721 : static const operator_div op_trunc_div (TRUNC_DIV_EXPR);
2722 : static const operator_div op_floor_div (FLOOR_DIV_EXPR);
2723 : static const operator_div op_round_div (ROUND_DIV_EXPR);
2724 : static const operator_div op_ceil_div (CEIL_DIV_EXPR);
2725 :
2726 : // Set OP2 to non-zero if the LHS isn't UNDEFINED.
2727 : bool
2728 45157 : operator_div::op2_range (irange &r, tree type, const irange &lhs,
2729 : const irange &, relation_trio) const
2730 : {
2731 45157 : if (!lhs.undefined_p ())
2732 : {
2733 45157 : r.set_nonzero (type);
2734 45157 : return true;
2735 : }
2736 : return false;
2737 : }
2738 :
2739 : bool
2740 13543928 : operator_div::wi_op_overflows (wide_int &res, tree type,
2741 : const wide_int &w0, const wide_int &w1) const
2742 : {
2743 13543928 : if (w1 == 0)
2744 : return true;
2745 :
2746 13543928 : wi::overflow_type overflow = wi::OVF_NONE;
2747 13543928 : signop sign = TYPE_SIGN (type);
2748 :
2749 13543928 : switch (m_code)
2750 : {
2751 13412961 : case EXACT_DIV_EXPR:
2752 13412961 : case TRUNC_DIV_EXPR:
2753 13412961 : res = wi::div_trunc (w0, w1, sign, &overflow);
2754 13412961 : break;
2755 119436 : case FLOOR_DIV_EXPR:
2756 119436 : res = wi::div_floor (w0, w1, sign, &overflow);
2757 119436 : break;
2758 288 : case ROUND_DIV_EXPR:
2759 288 : res = wi::div_round (w0, w1, sign, &overflow);
2760 288 : break;
2761 11243 : case CEIL_DIV_EXPR:
2762 11243 : res = wi::div_ceil (w0, w1, sign, &overflow);
2763 11243 : break;
2764 0 : default:
2765 0 : gcc_unreachable ();
2766 : }
2767 :
2768 13543928 : if (overflow && TYPE_OVERFLOW_UNDEFINED (type))
2769 : {
2770 : // For division, the only case is -INF / -1 = +INF.
2771 216147 : res = wi::max_value (w0.get_precision (), sign);
2772 216147 : return false;
2773 : }
2774 13327781 : return overflow;
2775 : }
2776 :
2777 : void
2778 4217572 : operator_div::wi_fold (irange &r, tree type,
2779 : const wide_int ÷nd_min,
2780 : const wide_int ÷nd_max,
2781 : const wide_int &divisor_min,
2782 : const wide_int &divisor_max) const
2783 : {
2784 4217572 : signop sign = TYPE_SIGN (type);
2785 4217572 : unsigned prec = TYPE_PRECISION (type);
2786 4217572 : wide_int extra_min, extra_max;
2787 :
2788 : // If we know we won't divide by zero, just do the division.
2789 4217572 : if (!wi_includes_zero_p (type, divisor_min, divisor_max))
2790 : {
2791 3395277 : wi_cross_product (r, type, dividend_min, dividend_max,
2792 : divisor_min, divisor_max);
2793 3395277 : return;
2794 : }
2795 :
2796 : // If we're definitely dividing by zero, there's nothing to do.
2797 822295 : if (wi_zero_p (type, divisor_min, divisor_max))
2798 : {
2799 18670 : r.set_undefined ();
2800 18670 : return;
2801 : }
2802 :
2803 : // Perform the division in 2 parts, [LB, -1] and [1, UB], which will
2804 : // skip any division by zero.
2805 :
2806 : // First divide by the negative numbers, if any.
2807 803625 : if (wi::neg_p (divisor_min, sign))
2808 1018594 : wi_cross_product (r, type, dividend_min, dividend_max,
2809 1018594 : divisor_min, wi::minus_one (prec));
2810 : else
2811 294328 : r.set_undefined ();
2812 :
2813 : // Then divide by the non-zero positive numbers, if any.
2814 803625 : if (wi::gt_p (divisor_max, wi::zero (prec), sign))
2815 : {
2816 803146 : int_range_max tmp;
2817 1606292 : wi_cross_product (tmp, type, dividend_min, dividend_max,
2818 803146 : wi::one (prec), divisor_max);
2819 803146 : r.union_ (tmp);
2820 803146 : }
2821 : // We shouldn't still have undefined here.
2822 803625 : gcc_checking_assert (!r.undefined_p ());
2823 : }
2824 :
2825 : bool
2826 3028096 : operator_div::op1_op2_relation_effect (irange &lhs_range,
2827 : tree type,
2828 : const irange &op1_range,
2829 : const irange &op2_range,
2830 : relation_kind rel) const
2831 : {
2832 3028096 : if (rel == VREL_VARYING)
2833 : return false;
2834 :
2835 3232 : int_range<2> rel_range;
2836 :
2837 3232 : switch (rel)
2838 : {
2839 : /* op1/op2 = 0 if op1 < op2 and both op1 and op2
2840 : are known positives. */
2841 125 : case VREL_LT:
2842 : // Exact, and truncate division will produce 0 for this case.
2843 : // Floor division will be treated similar to truncate div as rounding towards
2844 : // to 0 is the same as rounding towards -inf for positive values.
2845 125 : if (m_code != EXACT_DIV_EXPR
2846 125 : && m_code != TRUNC_DIV_EXPR
2847 4 : && m_code != FLOOR_DIV_EXPR)
2848 : return false;
2849 121 : if (!op1_range.nonnegative_p ()
2850 121 : || !op2_range.nonnegative_p ())
2851 : return false;
2852 84 : rel_range.set_zero (type);
2853 84 : break;
2854 3 : case VREL_EQ:
2855 3 : {
2856 3 : wide_int one = wi::one (TYPE_PRECISION (type));
2857 3 : rel_range.set (type, one, one);
2858 3 : break;
2859 3 : }
2860 : default:
2861 : return false;
2862 : }
2863 :
2864 87 : lhs_range.intersect (rel_range);
2865 87 : return true;
2866 3232 : }
2867 :
2868 : class operator_exact_divide : public operator_div
2869 : {
2870 : using range_operator::op1_range;
2871 : public:
2872 : operator_exact_divide () : operator_div (EXACT_DIV_EXPR) { }
2873 : virtual bool op1_range (irange &r, tree type,
2874 : const irange &lhs,
2875 : const irange &op2,
2876 : relation_trio) const;
2877 :
2878 : } op_exact_div;
2879 :
2880 : bool
2881 933507 : operator_exact_divide::op1_range (irange &r, tree type,
2882 : const irange &lhs,
2883 : const irange &op2,
2884 : relation_trio) const
2885 : {
2886 933507 : if (lhs.undefined_p ())
2887 : return false;
2888 933507 : wide_int offset;
2889 : // [2, 4] = op1 / [3,3] since its exact divide, no need to worry about
2890 : // remainders in the endpoints, so op1 = [2,4] * [3,3] = [6,12].
2891 : // We wont bother trying to enumerate all the in between stuff :-P
2892 : // TRUE accuracy is [6,6][9,9][12,12]. This is unlikely to matter most of
2893 : // the time however.
2894 : // If op2 is a multiple of 2, we would be able to set some non-zero bits.
2895 933507 : if (op2.singleton_p (offset) && offset != 0)
2896 933507 : return range_op_handler (MULT_EXPR).fold_range (r, type, lhs, op2);
2897 : return false;
2898 933507 : }
2899 :
2900 :
2901 : class operator_lshift : public cross_product_operator
2902 : {
2903 : using range_operator::fold_range;
2904 : using range_operator::op1_range;
2905 : using range_operator::update_bitmask;
2906 : public:
2907 : virtual bool op1_range (irange &r, tree type, const irange &lhs,
2908 : const irange &op2, relation_trio rel = TRIO_VARYING)
2909 : const final override;
2910 : virtual bool fold_range (irange &r, tree type, const irange &op1,
2911 : const irange &op2, relation_trio rel = TRIO_VARYING)
2912 : const final override;
2913 :
2914 : virtual void wi_fold (irange &r, tree type,
2915 : const wide_int &lh_lb, const wide_int &lh_ub,
2916 : const wide_int &rh_lb,
2917 : const wide_int &rh_ub) const final override;
2918 : virtual bool wi_op_overflows (wide_int &res,
2919 : tree type,
2920 : const wide_int &,
2921 : const wide_int &) const final override;
2922 461876 : void update_bitmask (irange &r, const irange &lh,
2923 : const irange &rh) const final override
2924 461876 : { update_known_bitmask (r, LSHIFT_EXPR, lh, rh); }
2925 : // Check compatibility of LHS and op1.
2926 1070474 : bool operand_check_p (tree t1, tree t2, tree) const final override
2927 1070474 : { return range_compatible_p (t1, t2); }
2928 : } op_lshift;
2929 :
2930 : class operator_rshift : public cross_product_operator
2931 : {
2932 : using range_operator::fold_range;
2933 : using range_operator::op1_range;
2934 : using range_operator::lhs_op1_relation;
2935 : using range_operator::update_bitmask;
2936 : public:
2937 : virtual bool fold_range (irange &r, tree type, const irange &op1,
2938 : const irange &op2, relation_trio rel = TRIO_VARYING)
2939 : const final override;
2940 : virtual void wi_fold (irange &r, tree type,
2941 : const wide_int &lh_lb,
2942 : const wide_int &lh_ub,
2943 : const wide_int &rh_lb,
2944 : const wide_int &rh_ub) const final override;
2945 : virtual bool wi_op_overflows (wide_int &res,
2946 : tree type,
2947 : const wide_int &w0,
2948 : const wide_int &w1) const final override;
2949 : virtual bool op1_range (irange &, tree type, const irange &lhs,
2950 : const irange &op2, relation_trio rel = TRIO_VARYING)
2951 : const final override;
2952 : virtual relation_kind lhs_op1_relation (const irange &lhs, const irange &op1,
2953 : const irange &op2, relation_kind rel)
2954 : const final override;
2955 3178212 : void update_bitmask (irange &r, const irange &lh,
2956 : const irange &rh) const final override
2957 3178212 : { update_known_bitmask (r, RSHIFT_EXPR, lh, rh); }
2958 : // Check compatibility of LHS and op1.
2959 3333281 : bool operand_check_p (tree t1, tree t2, tree) const final override
2960 3333281 : { return range_compatible_p (t1, t2); }
2961 : } op_rshift;
2962 :
2963 :
2964 : relation_kind
2965 2397179 : operator_rshift::lhs_op1_relation (const irange &lhs ATTRIBUTE_UNUSED,
2966 : const irange &op1,
2967 : const irange &op2,
2968 : relation_kind) const
2969 : {
2970 : // If both operands range are >= 0, then the LHS <= op1.
2971 2397179 : if (!op1.undefined_p () && !op2.undefined_p ()
2972 4793202 : && wi::ge_p (op1.lower_bound (), 0, TYPE_SIGN (op1.type ()))
2973 6931742 : && wi::ge_p (op2.lower_bound (), 0, TYPE_SIGN (op2.type ())))
2974 2110817 : return VREL_LE;
2975 : return VREL_VARYING;
2976 : }
2977 :
2978 : bool
2979 1524025 : operator_lshift::fold_range (irange &r, tree type,
2980 : const irange &op1,
2981 : const irange &op2,
2982 : relation_trio rel) const
2983 : {
2984 1524025 : int_range_max shift_range;
2985 1524025 : if (!get_shift_range (shift_range, type, op2))
2986 : {
2987 1237 : if (op2.undefined_p ())
2988 916 : r.set_undefined ();
2989 : else
2990 321 : r.set_zero (type);
2991 : return true;
2992 : }
2993 :
2994 : // Transform left shifts by constants into multiplies.
2995 1522788 : if (shift_range.singleton_p ())
2996 : {
2997 1060874 : unsigned shift = shift_range.lower_bound ().to_uhwi ();
2998 1060874 : wide_int tmp = wi::set_bit_in_zero (shift, TYPE_PRECISION (type));
2999 1060874 : int_range<1> mult (type, tmp, tmp);
3000 :
3001 : // Force wrapping multiplication.
3002 1060874 : bool saved_flag_wrapv = flag_wrapv;
3003 1060874 : bool saved_flag_wrapv_pointer = flag_wrapv_pointer;
3004 1060874 : flag_wrapv = 1;
3005 1060874 : flag_wrapv_pointer = 1;
3006 1060874 : bool b = op_mult.fold_range (r, type, op1, mult);
3007 1060874 : flag_wrapv = saved_flag_wrapv;
3008 1060874 : flag_wrapv_pointer = saved_flag_wrapv_pointer;
3009 1060874 : return b;
3010 1060883 : }
3011 : else
3012 : // Otherwise, invoke the generic fold routine.
3013 461914 : return range_operator::fold_range (r, type, op1, shift_range, rel);
3014 1524025 : }
3015 :
3016 : void
3017 544019 : operator_lshift::wi_fold (irange &r, tree type,
3018 : const wide_int &lh_lb, const wide_int &lh_ub,
3019 : const wide_int &rh_lb, const wide_int &rh_ub) const
3020 : {
3021 544019 : signop sign = TYPE_SIGN (type);
3022 544019 : unsigned prec = TYPE_PRECISION (type);
3023 544019 : int overflow_pos = sign == SIGNED ? prec - 1 : prec;
3024 544019 : int bound_shift = overflow_pos - rh_ub.to_shwi ();
3025 : // If bound_shift == HOST_BITS_PER_WIDE_INT, the llshift can
3026 : // overflow. However, for that to happen, rh.max needs to be zero,
3027 : // which means rh is a singleton range of zero, which means we simply return
3028 : // [lh_lb, lh_ub] as the range.
3029 544019 : if (wi::eq_p (rh_ub, rh_lb) && wi::eq_p (rh_ub, 0))
3030 : {
3031 21655 : r = int_range<2> (type, lh_lb, lh_ub);
3032 21655 : return;
3033 : }
3034 :
3035 522364 : wide_int bound = wi::set_bit_in_zero (bound_shift, prec);
3036 522364 : wide_int complement = ~(bound - 1);
3037 522364 : wide_int low_bound, high_bound;
3038 522364 : bool in_bounds = false;
3039 :
3040 522364 : if (sign == UNSIGNED)
3041 : {
3042 242594 : low_bound = bound;
3043 242594 : high_bound = complement;
3044 242594 : if (wi::ltu_p (lh_ub, low_bound))
3045 : {
3046 : // [5, 6] << [1, 2] == [10, 24].
3047 : // We're shifting out only zeroes, the value increases
3048 : // monotonically.
3049 : in_bounds = true;
3050 : }
3051 84737 : else if (wi::ltu_p (high_bound, lh_lb))
3052 : {
3053 : // [0xffffff00, 0xffffffff] << [1, 2]
3054 : // == [0xfffffc00, 0xfffffffe].
3055 : // We're shifting out only ones, the value decreases
3056 : // monotonically.
3057 : in_bounds = true;
3058 : }
3059 : }
3060 : else
3061 : {
3062 : // [-1, 1] << [1, 2] == [-4, 4]
3063 279770 : low_bound = complement;
3064 279770 : high_bound = bound;
3065 279770 : if (wi::lts_p (lh_ub, high_bound)
3066 279770 : && wi::lts_p (low_bound, lh_lb))
3067 : {
3068 : // For non-negative numbers, we're shifting out only zeroes,
3069 : // the value increases monotonically. For negative numbers,
3070 : // we're shifting out only ones, the value decreases
3071 : // monotonically.
3072 : in_bounds = true;
3073 : }
3074 : }
3075 :
3076 : if (in_bounds)
3077 271314 : wi_cross_product (r, type, lh_lb, lh_ub, rh_lb, rh_ub);
3078 : else
3079 251050 : r.set_varying (type);
3080 522385 : }
3081 :
3082 : bool
3083 529365 : operator_lshift::wi_op_overflows (wide_int &res, tree type,
3084 : const wide_int &w0, const wide_int &w1) const
3085 : {
3086 529365 : signop sign = TYPE_SIGN (type);
3087 529365 : if (wi::neg_p (w1))
3088 : {
3089 : // It's unclear from the C standard whether shifts can overflow.
3090 : // The following code ignores overflow; perhaps a C standard
3091 : // interpretation ruling is needed.
3092 0 : res = wi::rshift (w0, -w1, sign);
3093 : }
3094 : else
3095 529365 : res = wi::lshift (w0, w1);
3096 529365 : return false;
3097 : }
3098 :
3099 : bool
3100 73106 : operator_lshift::op1_range (irange &r,
3101 : tree type,
3102 : const irange &lhs,
3103 : const irange &op2,
3104 : relation_trio) const
3105 : {
3106 73106 : if (lhs.undefined_p ())
3107 : return false;
3108 :
3109 73106 : if (!lhs.contains_zero_p ())
3110 24996 : r.set_nonzero (type);
3111 : else
3112 48110 : r.set_varying (type);
3113 :
3114 73106 : wide_int shift;
3115 73106 : if (op2.singleton_p (shift))
3116 : {
3117 62935 : if (wi::lt_p (shift, 0, SIGNED))
3118 : return false;
3119 62935 : if (wi::ge_p (shift, wi::uhwi (TYPE_PRECISION (type),
3120 62935 : TYPE_PRECISION (op2.type ())),
3121 : UNSIGNED))
3122 : return false;
3123 62935 : if (shift == 0)
3124 : {
3125 244 : r.intersect (lhs);
3126 244 : return true;
3127 : }
3128 :
3129 : // Work completely in unsigned mode to start.
3130 62691 : tree utype = type;
3131 62691 : int_range_max tmp_range;
3132 62691 : if (TYPE_SIGN (type) == SIGNED)
3133 : {
3134 6931 : int_range_max tmp = lhs;
3135 6931 : utype = unsigned_type_for (type);
3136 6931 : range_cast (tmp, utype);
3137 6931 : op_rshift.fold_range (tmp_range, utype, tmp, op2);
3138 6931 : }
3139 : else
3140 55760 : op_rshift.fold_range (tmp_range, utype, lhs, op2);
3141 :
3142 : // If no valid range is found, abort the calculation and return falae.
3143 62691 : if (tmp_range.undefined_p ())
3144 : return false;
3145 :
3146 : // Start with ranges which can produce the LHS by right shifting the
3147 : // result by the shift amount.
3148 : // ie [0x08, 0xF0] = op1 << 2 will start with
3149 : // [00001000, 11110000] = op1 << 2
3150 : // [0x02, 0x4C] aka [00000010, 00111100]
3151 :
3152 : // Then create a range from the LB with the least significant upper bit
3153 : // set, to the upper bound with all the bits set.
3154 : // This would be [0x42, 0xFC] aka [01000010, 11111100].
3155 :
3156 : // Ideally we do this for each subrange, but just lump them all for now.
3157 62691 : unsigned low_bits = TYPE_PRECISION (utype) - shift.to_uhwi ();
3158 62691 : wide_int up_mask = wi::mask (low_bits, true, TYPE_PRECISION (utype));
3159 62691 : wide_int new_ub = wi::bit_or (up_mask, tmp_range.upper_bound ());
3160 62691 : wide_int new_lb = wi::set_bit (tmp_range.lower_bound (), low_bits);
3161 62691 : int_range<2> fill_range (utype, new_lb, new_ub);
3162 62691 : tmp_range.union_ (fill_range);
3163 :
3164 62691 : if (utype != type)
3165 6931 : range_cast (tmp_range, type);
3166 :
3167 62691 : r.intersect (tmp_range);
3168 62691 : return true;
3169 62691 : }
3170 :
3171 10171 : return !r.varying_p ();
3172 73106 : }
3173 :
3174 : bool
3175 733474 : operator_rshift::op1_range (irange &r,
3176 : tree type,
3177 : const irange &lhs,
3178 : const irange &op2,
3179 : relation_trio) const
3180 : {
3181 733474 : if (lhs.undefined_p ())
3182 : return false;
3183 733474 : wide_int shift;
3184 733474 : if (op2.singleton_p (shift))
3185 : {
3186 : // Ignore nonsensical shifts.
3187 668083 : unsigned prec = TYPE_PRECISION (type);
3188 1336166 : if (wi::ge_p (shift,
3189 668083 : wi::uhwi (prec, TYPE_PRECISION (op2.type ())),
3190 : UNSIGNED))
3191 : return false;
3192 668083 : if (shift == 0)
3193 : {
3194 5022 : r = lhs;
3195 5022 : return true;
3196 : }
3197 :
3198 : // Folding the original operation may discard some impossible
3199 : // ranges from the LHS.
3200 663061 : int_range_max lhs_refined;
3201 663061 : op_rshift.fold_range (lhs_refined, type, int_range<1> (type), op2);
3202 663061 : lhs_refined.intersect (lhs);
3203 663061 : if (lhs_refined.undefined_p ())
3204 : {
3205 4 : r.set_undefined ();
3206 4 : return true;
3207 : }
3208 663057 : int_range_max shift_range (op2.type (), shift, shift);
3209 663057 : int_range_max lb, ub;
3210 663057 : op_lshift.fold_range (lb, type, lhs_refined, shift_range);
3211 : // LHS
3212 : // 0000 0111 = OP1 >> 3
3213 : //
3214 : // OP1 is anything from 0011 1000 to 0011 1111. That is, a
3215 : // range from LHS<<3 plus a mask of the 3 bits we shifted on the
3216 : // right hand side (0x07).
3217 663057 : wide_int mask = wi::mask (shift.to_uhwi (), false, prec);
3218 663057 : int_range_max mask_range (type,
3219 663057 : wi::zero (TYPE_PRECISION (type)),
3220 663057 : mask);
3221 663057 : op_plus.fold_range (ub, type, lb, mask_range);
3222 663057 : r = lb;
3223 663057 : r.union_ (ub);
3224 663057 : if (!lhs_refined.contains_zero_p ())
3225 : {
3226 512899 : if (!mask_range.invert ())
3227 : return false;
3228 512899 : r.intersect (mask_range);
3229 : }
3230 : return true;
3231 663061 : }
3232 : return false;
3233 733474 : }
3234 :
3235 : bool
3236 10669005 : operator_rshift::wi_op_overflows (wide_int &res,
3237 : tree type,
3238 : const wide_int &w0,
3239 : const wide_int &w1) const
3240 : {
3241 10669005 : signop sign = TYPE_SIGN (type);
3242 10669005 : if (wi::neg_p (w1))
3243 0 : res = wi::lshift (w0, -w1);
3244 : else
3245 : {
3246 : // It's unclear from the C standard whether shifts can overflow.
3247 : // The following code ignores overflow; perhaps a C standard
3248 : // interpretation ruling is needed.
3249 10669096 : res = wi::rshift (w0, w1, sign);
3250 : }
3251 10669005 : return false;
3252 : }
3253 :
3254 : bool
3255 3179700 : operator_rshift::fold_range (irange &r, tree type,
3256 : const irange &op1,
3257 : const irange &op2,
3258 : relation_trio rel) const
3259 : {
3260 3179700 : int_range_max shift;
3261 3179700 : if (!get_shift_range (shift, type, op2))
3262 : {
3263 514 : if (op2.undefined_p ())
3264 211 : r.set_undefined ();
3265 : else
3266 303 : r.set_zero (type);
3267 : return true;
3268 : }
3269 :
3270 3179186 : return range_operator::fold_range (r, type, op1, shift, rel);
3271 3179700 : }
3272 :
3273 : void
3274 3850703 : operator_rshift::wi_fold (irange &r, tree type,
3275 : const wide_int &lh_lb, const wide_int &lh_ub,
3276 : const wide_int &rh_lb, const wide_int &rh_ub) const
3277 : {
3278 3850703 : wi_cross_product (r, type, lh_lb, lh_ub, rh_lb, rh_ub);
3279 3850703 : }
3280 :
3281 :
3282 : // Add a partial equivalence between the LHS and op1 for casts.
3283 :
3284 : relation_kind
3285 24420923 : operator_cast::lhs_op1_relation (const irange &lhs,
3286 : const irange &op1,
3287 : const irange &op2 ATTRIBUTE_UNUSED,
3288 : relation_kind) const
3289 : {
3290 24420923 : if (lhs.undefined_p () || op1.undefined_p ())
3291 : return VREL_VARYING;
3292 24397122 : unsigned lhs_prec = TYPE_PRECISION (lhs.type ());
3293 24397122 : unsigned op1_prec = TYPE_PRECISION (op1.type ());
3294 : // If the result gets sign extended into a larger type check first if this
3295 : // qualifies as a partial equivalence.
3296 24397122 : if (TYPE_SIGN (op1.type ()) == SIGNED && lhs_prec > op1_prec)
3297 : {
3298 : // If the result is sign extended, and the LHS is larger than op1,
3299 : // check if op1's range can be negative as the sign extension will
3300 : // cause the upper bits to be 1 instead of 0, invalidating the PE.
3301 3905568 : int_range<3> negs = range_negatives (op1.type ());
3302 3905568 : negs.intersect (op1);
3303 3905568 : if (!negs.undefined_p ())
3304 2726189 : return VREL_VARYING;
3305 3905568 : }
3306 :
3307 21670933 : unsigned prec = MIN (lhs_prec, op1_prec);
3308 21670933 : return bits_to_pe (prec);
3309 : }
3310 :
3311 : // Return TRUE if casting from INNER to OUTER is a truncating cast.
3312 :
3313 : inline bool
3314 89190705 : operator_cast::truncating_cast_p (const irange &inner,
3315 : const irange &outer) const
3316 : {
3317 89190705 : return TYPE_PRECISION (outer.type ()) < TYPE_PRECISION (inner.type ());
3318 : }
3319 :
3320 : // Return TRUE if [MIN,MAX] is inside the domain of RANGE's type.
3321 :
3322 : bool
3323 76594222 : operator_cast::inside_domain_p (const wide_int &min,
3324 : const wide_int &max,
3325 : const irange &range) const
3326 : {
3327 76594222 : wide_int domain_min = irange_val_min (range.type ());
3328 76594222 : wide_int domain_max = irange_val_max (range.type ());
3329 76594222 : signop domain_sign = TYPE_SIGN (range.type ());
3330 76594222 : return (wi::le_p (min, domain_max, domain_sign)
3331 76594222 : && wi::le_p (max, domain_max, domain_sign)
3332 76594222 : && wi::ge_p (min, domain_min, domain_sign)
3333 153188444 : && wi::ge_p (max, domain_min, domain_sign));
3334 76594222 : }
3335 :
3336 :
3337 : // Helper for fold_range which work on a pair at a time.
3338 :
3339 : void
3340 79903229 : operator_cast::fold_pair (irange &r, unsigned index,
3341 : const irange &inner,
3342 : const irange &outer) const
3343 : {
3344 79903229 : tree inner_type = inner.type ();
3345 79903229 : tree outer_type = outer.type ();
3346 79903229 : signop inner_sign = TYPE_SIGN (inner_type);
3347 79903229 : unsigned outer_prec = TYPE_PRECISION (outer_type);
3348 :
3349 : // check to see if casting from INNER to OUTER is a conversion that
3350 : // fits in the resulting OUTER type.
3351 79903229 : wide_int inner_lb = inner.lower_bound (index);
3352 79903229 : wide_int inner_ub = inner.upper_bound (index);
3353 79903229 : if (truncating_cast_p (inner, outer))
3354 : {
3355 : // We may be able to accommodate a truncating cast if the
3356 : // resulting range can be represented in the target type...
3357 15102532 : if (wi::rshift (wi::sub (inner_ub, inner_lb),
3358 7551266 : wi::uhwi (outer_prec, TYPE_PRECISION (inner.type ())),
3359 22653798 : inner_sign) != 0)
3360 : {
3361 3309007 : r.set_varying (outer_type);
3362 3309007 : return;
3363 : }
3364 : }
3365 : // ...but we must still verify that the final range fits in the
3366 : // domain. This catches -fstrict-enum restrictions where the domain
3367 : // range is smaller than what fits in the underlying type.
3368 76594222 : wide_int min = wide_int::from (inner_lb, outer_prec, inner_sign);
3369 76594222 : wide_int max = wide_int::from (inner_ub, outer_prec, inner_sign);
3370 76594222 : if (inside_domain_p (min, max, outer))
3371 76594222 : create_possibly_reversed_range (r, outer_type, min, max);
3372 : else
3373 0 : r.set_varying (outer_type);
3374 79906546 : }
3375 :
3376 :
3377 : bool
3378 64509948 : operator_cast::fold_range (irange &r, tree type ATTRIBUTE_UNUSED,
3379 : const irange &inner,
3380 : const irange &outer,
3381 : relation_trio) const
3382 : {
3383 64509948 : if (empty_range_varying (r, type, inner, outer))
3384 35831 : return true;
3385 :
3386 64474117 : gcc_checking_assert (outer.varying_p ());
3387 64474117 : gcc_checking_assert (inner.num_pairs () > 0);
3388 :
3389 : // Avoid a temporary by folding the first pair directly into the result.
3390 64474117 : fold_pair (r, 0, inner, outer);
3391 :
3392 : // Then process any additional pairs by unioning with their results.
3393 143702923 : for (unsigned x = 1; x < inner.num_pairs (); ++x)
3394 : {
3395 15429112 : int_range_max tmp;
3396 15429112 : fold_pair (tmp, x, inner, outer);
3397 15429112 : r.union_ (tmp);
3398 : // If we hit varying, go update the bitmask.
3399 15429112 : if (r.varying_p ())
3400 : break;
3401 15429112 : }
3402 :
3403 64474117 : update_bitmask (r, inner, outer);
3404 64474117 : return true;
3405 : }
3406 :
3407 : void
3408 64474117 : operator_cast::update_bitmask (irange &r, const irange &lh,
3409 : const irange &rh) const
3410 : {
3411 64474117 : update_known_bitmask (r, CONVERT_EXPR, lh, rh);
3412 64474117 : }
3413 :
3414 : bool
3415 9287476 : operator_cast::op1_range (irange &r, tree type,
3416 : const irange &lhs,
3417 : const irange &op2,
3418 : relation_trio) const
3419 : {
3420 9287476 : if (lhs.undefined_p ())
3421 : return false;
3422 9287476 : tree lhs_type = lhs.type ();
3423 9287476 : gcc_checking_assert (types_compatible_p (op2.type(), type));
3424 :
3425 : // If we are calculating a pointer, shortcut to what we really care about.
3426 9287476 : if (POINTER_TYPE_P (type))
3427 : {
3428 : // Conversion from other pointers or a constant (including 0/NULL)
3429 : // are straightforward.
3430 0 : if (POINTER_TYPE_P (lhs.type ())
3431 0 : || (lhs.singleton_p ()
3432 0 : && TYPE_PRECISION (lhs.type ()) >= TYPE_PRECISION (type)))
3433 : {
3434 0 : r = lhs;
3435 0 : range_cast (r, type);
3436 : }
3437 : else
3438 : {
3439 : // If the LHS is not a pointer nor a singleton, then it is
3440 : // either VARYING or non-zero.
3441 0 : if (!lhs.undefined_p () && !lhs.contains_zero_p ())
3442 0 : r.set_nonzero (type);
3443 : else
3444 0 : r.set_varying (type);
3445 : }
3446 0 : r.intersect (op2);
3447 0 : return true;
3448 : }
3449 :
3450 9287476 : if (truncating_cast_p (op2, lhs))
3451 : {
3452 1087627 : if (lhs.varying_p ())
3453 155 : r.set_varying (type);
3454 : else
3455 : {
3456 : // We want to insert the LHS as an unsigned value since it
3457 : // would not trigger the signed bit of the larger type.
3458 1087472 : int_range_max converted_lhs = lhs;
3459 1087472 : range_cast (converted_lhs, unsigned_type_for (lhs_type));
3460 1087472 : range_cast (converted_lhs, type);
3461 : // Start by building the positive signed outer range for the type.
3462 1087472 : wide_int lim = wi::set_bit_in_zero (TYPE_PRECISION (lhs_type),
3463 2174944 : TYPE_PRECISION (type));
3464 1087472 : create_possibly_reversed_range (r, type, lim,
3465 1087472 : wi::max_value (TYPE_PRECISION (type),
3466 : SIGNED));
3467 : // For the signed part, we need to simply union the 2 ranges now.
3468 1087472 : r.union_ (converted_lhs);
3469 :
3470 : // Create maximal negative number outside of LHS bits.
3471 1087472 : lim = wi::mask (TYPE_PRECISION (lhs_type), true,
3472 2174944 : TYPE_PRECISION (type));
3473 : // Add this to the unsigned LHS range(s).
3474 1087472 : int_range_max lim_range (type, lim, lim);
3475 1087472 : int_range_max lhs_neg;
3476 1087472 : range_op_handler (PLUS_EXPR).fold_range (lhs_neg, type,
3477 : converted_lhs, lim_range);
3478 : // lhs_neg now has all the negative versions of the LHS.
3479 : // Now union in all the values from SIGNED MIN (0x80000) to
3480 : // lim-1 in order to fill in all the ranges with the upper
3481 : // bits set.
3482 :
3483 : // PR 97317. If the lhs has only 1 bit less precision than the rhs,
3484 : // we don't need to create a range from min to lim-1
3485 : // calculate neg range traps trying to create [lim, lim - 1].
3486 1087472 : wide_int min_val = wi::min_value (TYPE_PRECISION (type), SIGNED);
3487 1087472 : if (lim != min_val)
3488 : {
3489 1085485 : int_range_max neg (type,
3490 2170970 : wi::min_value (TYPE_PRECISION (type),
3491 : SIGNED),
3492 2170970 : lim - 1);
3493 1085485 : lhs_neg.union_ (neg);
3494 1085485 : }
3495 : // And finally, munge the signed and unsigned portions.
3496 1087472 : r.union_ (lhs_neg);
3497 1087481 : }
3498 : // And intersect with any known value passed in the extra operand.
3499 1087627 : r.intersect (op2);
3500 1087627 : if (r.undefined_p ())
3501 : return true;
3502 :
3503 : // Now create a bitmask indicating that the lower bit must match the
3504 : // bits in the LHS. Zero-extend LHS bitmask to precision of op1.
3505 1087512 : irange_bitmask bm = lhs.get_bitmask ();
3506 2175024 : wide_int mask = wide_int::from (bm.mask (), TYPE_PRECISION (type),
3507 2175024 : UNSIGNED);
3508 2175024 : wide_int value = wide_int::from (bm.value (), TYPE_PRECISION (type),
3509 2175024 : UNSIGNED);
3510 :
3511 : // Set then additional unknown bits in mask.
3512 1087512 : wide_int lim = wi::mask (TYPE_PRECISION (lhs_type), true,
3513 2175024 : TYPE_PRECISION (type));
3514 1087512 : mask = mask | lim;
3515 :
3516 : // Now set the new bitmask for the range.
3517 1087512 : irange_bitmask new_bm (value, mask);
3518 1087512 : r.update_bitmask (new_bm);
3519 1087512 : return true;
3520 1087539 : }
3521 :
3522 8199849 : int_range_max tmp;
3523 8199849 : if (TYPE_PRECISION (lhs_type) == TYPE_PRECISION (type))
3524 6457116 : tmp = lhs;
3525 : else
3526 : {
3527 : // The cast is not truncating, and the range is restricted to
3528 : // the range of the RHS by this assignment.
3529 : //
3530 : // Cast the range of the RHS to the type of the LHS.
3531 1742733 : fold_range (tmp, lhs_type, int_range<1> (type), int_range<1> (lhs_type));
3532 : // Intersect this with the LHS range will produce the range,
3533 : // which will be cast to the RHS type before returning.
3534 1742733 : tmp.intersect (lhs);
3535 : }
3536 :
3537 : // Cast the calculated range to the type of the RHS.
3538 8199849 : fold_range (r, type, tmp, int_range<1> (type));
3539 8199849 : return true;
3540 8199849 : }
3541 :
3542 : // VIEW_CONVERT_EXPR works like a cast between integral values.
3543 : // If the number of bits are not the same, behaviour is undefined,
3544 : // so cast behaviour still works.
3545 :
3546 : bool
3547 273105 : operator_view::fold_range (irange &r, tree type,
3548 : const irange &op1, const irange &op2,
3549 : relation_trio rel) const
3550 : {
3551 273105 : return m_cast.fold_range (r, type, op1, op2, rel);
3552 : }
3553 :
3554 : bool
3555 0 : operator_view::fold_range (prange &r, tree type,
3556 : const prange &op1, const prange &op2,
3557 : relation_trio rel) const
3558 : {
3559 0 : return m_cast.fold_range (r, type, op1, op2, rel);
3560 : }
3561 : bool
3562 257002 : operator_view::fold_range (irange &r, tree type,
3563 : const prange &op1, const irange &op2,
3564 : relation_trio rel) const
3565 : {
3566 257002 : return m_cast.fold_range (r, type, op1, op2, rel);
3567 : }
3568 :
3569 : bool
3570 0 : operator_view::fold_range (prange &r, tree type,
3571 : const irange &op1, const prange &op2,
3572 : relation_trio rel) const
3573 : {
3574 0 : return m_cast.fold_range (r, type, op1, op2, rel);
3575 : }
3576 :
3577 : bool
3578 8565 : operator_view::op1_range (irange &r, tree type,
3579 : const irange &lhs, const irange &op2,
3580 : relation_trio rel) const
3581 : {
3582 8565 : return m_cast.op1_range (r, type, lhs, op2, rel);
3583 : }
3584 :
3585 : bool
3586 0 : operator_view::op1_range (prange &r, tree type,
3587 : const prange &lhs, const prange &op2,
3588 : relation_trio rel) const
3589 : {
3590 0 : return m_cast.op1_range (r, type, lhs, op2, rel);
3591 : }
3592 :
3593 : bool
3594 0 : operator_view::op1_range (irange &r, tree type,
3595 : const prange &lhs, const irange &op2,
3596 : relation_trio rel) const
3597 : {
3598 0 : return m_cast.op1_range (r, type, lhs, op2, rel);
3599 : }
3600 :
3601 : bool
3602 0 : operator_view::op1_range (prange &r, tree type,
3603 : const irange &lhs, const prange &op2,
3604 : relation_trio rel) const
3605 : {
3606 0 : return m_cast.op1_range (r, type, lhs, op2, rel);
3607 : }
3608 :
3609 : void
3610 0 : operator_view::update_bitmask (irange &r, const irange &lh,
3611 : const irange &rh) const
3612 : {
3613 0 : m_cast.update_bitmask (r, lh, rh);
3614 0 : }
3615 :
3616 :
3617 : class operator_logical_and : public range_operator
3618 : {
3619 : using range_operator::fold_range;
3620 : using range_operator::op1_range;
3621 : using range_operator::op2_range;
3622 : public:
3623 : bool fold_range (irange &r, tree type,
3624 : const irange &lh,
3625 : const irange &rh,
3626 : relation_trio rel = TRIO_VARYING) const final override;
3627 : bool op1_range (irange &r, tree type,
3628 : const irange &lhs,
3629 : const irange &op2,
3630 : relation_trio rel = TRIO_VARYING) const final override;
3631 : bool op2_range (irange &r, tree type,
3632 : const irange &lhs,
3633 : const irange &op1,
3634 : relation_trio rel = TRIO_VARYING) const final override;
3635 : // Check compatibility of all operands.
3636 0 : bool operand_check_p (tree t1, tree t2, tree t3) const final override
3637 0 : { return range_compatible_p (t1, t2) && range_compatible_p (t1, t3); }
3638 : } op_logical_and;
3639 :
3640 : bool
3641 0 : operator_logical_and::fold_range (irange &r, tree type,
3642 : const irange &lh,
3643 : const irange &rh,
3644 : relation_trio) const
3645 : {
3646 0 : if (empty_range_varying (r, type, lh, rh))
3647 0 : return true;
3648 :
3649 : // Precision of LHS and both operands must match.
3650 0 : if (TYPE_PRECISION (lh.type ()) != TYPE_PRECISION (type)
3651 0 : || TYPE_PRECISION (type) != TYPE_PRECISION (rh.type ()))
3652 : return false;
3653 :
3654 : // 0 && anything is 0.
3655 0 : if ((wi::eq_p (lh.lower_bound (), 0) && wi::eq_p (lh.upper_bound (), 0))
3656 0 : || (wi::eq_p (lh.lower_bound (), 0) && wi::eq_p (rh.upper_bound (), 0)))
3657 0 : r = range_false (type);
3658 0 : else if (lh.contains_zero_p () || rh.contains_zero_p ())
3659 : // To reach this point, there must be a logical 1 on each side, and
3660 : // the only remaining question is whether there is a zero or not.
3661 0 : r = range_true_and_false (type);
3662 : else
3663 0 : r = range_true (type);
3664 : return true;
3665 : }
3666 :
3667 : bool
3668 3349968 : operator_logical_and::op1_range (irange &r, tree type,
3669 : const irange &lhs,
3670 : const irange &op2,
3671 : relation_trio) const
3672 : {
3673 3349968 : switch (get_bool_state (r, lhs, type))
3674 : {
3675 1828047 : case BRS_TRUE:
3676 : // A TRUE result means both sides of the AND must be true.
3677 1828047 : r = range_true (type);
3678 1828047 : return true;
3679 :
3680 1521874 : case BRS_FALSE:
3681 : // A FALSE result when op2 is TRUE, must have op1 FALSE.
3682 1521874 : if (!op2.contains_p (wi::zero (TYPE_PRECISION (op2.type ()))))
3683 : {
3684 38724 : r = range_false (type);
3685 38724 : return true;
3686 : }
3687 : break;
3688 :
3689 : default:
3690 : break;
3691 : }
3692 :
3693 : // Any other result means we cannot be sure of any result.
3694 1483197 : r = range_true_and_false (type);
3695 1483197 : return true;
3696 : }
3697 :
3698 : bool
3699 0 : operator_logical_and::op2_range (irange &r, tree type,
3700 : const irange &lhs,
3701 : const irange &op1,
3702 : relation_trio) const
3703 : {
3704 0 : return operator_logical_and::op1_range (r, type, lhs, op1);
3705 : }
3706 :
3707 :
3708 : void
3709 7541369 : operator_bitwise_and::update_bitmask (irange &r, const irange &lh,
3710 : const irange &rh) const
3711 : {
3712 7541369 : update_known_bitmask (r, BIT_AND_EXPR, lh, rh);
3713 7541369 : }
3714 :
3715 : // Optimize BIT_AND_EXPR, BIT_IOR_EXPR and BIT_XOR_EXPR of signed types
3716 : // by considering the number of leading redundant sign bit copies.
3717 : // clrsb (X op Y) = min (clrsb (X), clrsb (Y)), so for example
3718 : // [-1, 0] op [-1, 0] is [-1, 0] (where nonzero_bits doesn't help).
3719 : static bool
3720 193474 : wi_optimize_signed_bitwise_op (irange &r, tree type,
3721 : const wide_int &lh_lb, const wide_int &lh_ub,
3722 : const wide_int &rh_lb, const wide_int &rh_ub)
3723 : {
3724 386948 : int lh_clrsb = MIN (wi::clrsb (lh_lb), wi::clrsb (lh_ub));
3725 386948 : int rh_clrsb = MIN (wi::clrsb (rh_lb), wi::clrsb (rh_ub));
3726 193474 : int new_clrsb = MIN (lh_clrsb, rh_clrsb);
3727 193474 : if (new_clrsb == 0)
3728 : return false;
3729 11511 : int type_prec = TYPE_PRECISION (type);
3730 11511 : int rprec = (type_prec - new_clrsb) - 1;
3731 11511 : value_range_with_overflow (r, type,
3732 23022 : wi::mask (rprec, true, type_prec),
3733 11511 : wi::mask (rprec, false, type_prec));
3734 11511 : return true;
3735 : }
3736 :
3737 : // An AND of 8,16, 32 or 64 bits can produce a partial equivalence between
3738 : // the LHS and op1.
3739 :
3740 : relation_kind
3741 6774350 : operator_bitwise_and::lhs_op1_relation (const irange &lhs,
3742 : const irange &op1,
3743 : const irange &op2,
3744 : relation_kind rel) const
3745 : {
3746 6774350 : if (lhs.undefined_p () || op1.undefined_p () || op2.undefined_p ())
3747 : return VREL_VARYING;
3748 : // When x == y, x & y is x.
3749 6768542 : if (rel == VREL_EQ)
3750 : return VREL_EQ;
3751 6768539 : if (!op2.singleton_p ())
3752 : return VREL_VARYING;
3753 : // if val == 0xff or 0xFFFF OR 0Xffffffff OR 0Xffffffffffffffff, return TRUE
3754 3701282 : int prec1 = TYPE_PRECISION (op1.type ());
3755 3701282 : int prec2 = TYPE_PRECISION (op2.type ());
3756 3701282 : int mask_prec = 0;
3757 3701282 : wide_int mask = op2.lower_bound ();
3758 3701282 : if (wi::eq_p (mask, wi::mask (8, false, prec2)))
3759 : mask_prec = 8;
3760 3610676 : else if (wi::eq_p (mask, wi::mask (16, false, prec2)))
3761 : mask_prec = 16;
3762 3595292 : else if (wi::eq_p (mask, wi::mask (32, false, prec2)))
3763 : mask_prec = 32;
3764 3430126 : else if (wi::eq_p (mask, wi::mask (64, false, prec2)))
3765 : mask_prec = 64;
3766 3701282 : return bits_to_pe (MIN (prec1, mask_prec));
3767 3701282 : }
3768 :
3769 : // Optimize BIT_AND_EXPR and BIT_IOR_EXPR in terms of a mask if
3770 : // possible. Basically, see if we can optimize:
3771 : //
3772 : // [LB, UB] op Z
3773 : // into:
3774 : // [LB op Z, UB op Z]
3775 : //
3776 : // If the optimization was successful, accumulate the range in R and
3777 : // return TRUE.
3778 :
3779 : static bool
3780 23397449 : wi_optimize_and_or (irange &r,
3781 : enum tree_code code,
3782 : tree type,
3783 : const wide_int &lh_lb, const wide_int &lh_ub,
3784 : const wide_int &rh_lb, const wide_int &rh_ub)
3785 : {
3786 : // Calculate the singleton mask among the ranges, if any.
3787 23397449 : wide_int lower_bound, upper_bound, mask;
3788 23397449 : if (wi::eq_p (rh_lb, rh_ub))
3789 : {
3790 21647393 : mask = rh_lb;
3791 21647393 : lower_bound = lh_lb;
3792 21647393 : upper_bound = lh_ub;
3793 : }
3794 1750056 : else if (wi::eq_p (lh_lb, lh_ub))
3795 : {
3796 348733 : mask = lh_lb;
3797 348733 : lower_bound = rh_lb;
3798 348733 : upper_bound = rh_ub;
3799 : }
3800 : else
3801 : return false;
3802 :
3803 : // If Z is a constant which (for op | its bitwise not) has n
3804 : // consecutive least significant bits cleared followed by m 1
3805 : // consecutive bits set immediately above it and either
3806 : // m + n == precision, or (x >> (m + n)) == (y >> (m + n)).
3807 : //
3808 : // The least significant n bits of all the values in the range are
3809 : // cleared or set, the m bits above it are preserved and any bits
3810 : // above these are required to be the same for all values in the
3811 : // range.
3812 21996126 : wide_int w = mask;
3813 21996126 : int m = 0, n = 0;
3814 21996126 : if (code == BIT_IOR_EXPR)
3815 6418165 : w = ~w;
3816 21996126 : if (wi::eq_p (w, 0))
3817 7557256 : n = w.get_precision ();
3818 : else
3819 : {
3820 14438870 : n = wi::ctz (w);
3821 14438876 : w = ~(w | wi::mask (n, false, w.get_precision ()));
3822 14438870 : if (wi::eq_p (w, 0))
3823 8803176 : m = w.get_precision () - n;
3824 : else
3825 5635694 : m = wi::ctz (w) - n;
3826 : }
3827 21996126 : wide_int new_mask = wi::mask (m + n, true, w.get_precision ());
3828 21996132 : if ((new_mask & lower_bound) != (new_mask & upper_bound))
3829 : return false;
3830 :
3831 17197141 : wide_int res_lb, res_ub;
3832 17197141 : if (code == BIT_AND_EXPR)
3833 : {
3834 11009790 : res_lb = wi::bit_and (lower_bound, mask);
3835 11009790 : res_ub = wi::bit_and (upper_bound, mask);
3836 : }
3837 6187351 : else if (code == BIT_IOR_EXPR)
3838 : {
3839 6187351 : res_lb = wi::bit_or (lower_bound, mask);
3840 6187351 : res_ub = wi::bit_or (upper_bound, mask);
3841 : }
3842 : else
3843 0 : gcc_unreachable ();
3844 17197141 : value_range_with_overflow (r, type, res_lb, res_ub);
3845 :
3846 : // Furthermore, if the mask is non-zero, an IOR cannot contain zero.
3847 17197141 : if (code == BIT_IOR_EXPR && wi::ne_p (mask, 0))
3848 : {
3849 3136284 : int_range<2> tmp;
3850 3136284 : tmp.set_nonzero (type);
3851 3136284 : r.intersect (tmp);
3852 3136284 : }
3853 17197141 : return true;
3854 62590722 : }
3855 :
3856 : // For range [LB, UB] compute two wide_int bit masks.
3857 : //
3858 : // In the MAYBE_NONZERO bit mask, if some bit is unset, it means that
3859 : // for all numbers in the range the bit is 0, otherwise it might be 0
3860 : // or 1.
3861 : //
3862 : // In the MUSTBE_NONZERO bit mask, if some bit is set, it means that
3863 : // for all numbers in the range the bit is 1, otherwise it might be 0
3864 : // or 1.
3865 :
3866 : void
3867 13472801 : wi_set_zero_nonzero_bits (tree type,
3868 : const wide_int &lb, const wide_int &ub,
3869 : wide_int &maybe_nonzero,
3870 : wide_int &mustbe_nonzero)
3871 : {
3872 13472801 : signop sign = TYPE_SIGN (type);
3873 :
3874 13472801 : if (wi::eq_p (lb, ub))
3875 5292991 : maybe_nonzero = mustbe_nonzero = lb;
3876 8179810 : else if (wi::ge_p (lb, 0, sign) || wi::lt_p (ub, 0, sign))
3877 : {
3878 7727480 : wide_int xor_mask = lb ^ ub;
3879 7727480 : maybe_nonzero = lb | ub;
3880 7727480 : mustbe_nonzero = lb & ub;
3881 7727480 : if (xor_mask != 0)
3882 : {
3883 7727480 : wide_int mask = wi::mask (wi::floor_log2 (xor_mask), false,
3884 15454960 : maybe_nonzero.get_precision ());
3885 7727480 : maybe_nonzero = maybe_nonzero | mask;
3886 7727493 : mustbe_nonzero = wi::bit_and_not (mustbe_nonzero, mask);
3887 7727480 : }
3888 7727480 : }
3889 : else
3890 : {
3891 452330 : maybe_nonzero = wi::minus_one (lb.get_precision ());
3892 452330 : mustbe_nonzero = wi::zero (lb.get_precision ());
3893 : }
3894 13472801 : }
3895 :
3896 : void
3897 16936299 : operator_bitwise_and::wi_fold (irange &r, tree type,
3898 : const wide_int &lh_lb,
3899 : const wide_int &lh_ub,
3900 : const wide_int &rh_lb,
3901 : const wide_int &rh_ub) const
3902 : {
3903 : // The AND algorithm does not handle complex signed operations well.
3904 : // If a signed range crosses the boundary between signed and unsigned
3905 : // process it as 2 ranges and union the results.
3906 16936299 : if (TYPE_SIGN (type) == SIGNED
3907 16936299 : && wi::neg_p (lh_lb, SIGNED) != wi::neg_p (lh_ub, SIGNED))
3908 : {
3909 639996 : int prec = TYPE_PRECISION (type);
3910 639996 : int_range_max tmp;
3911 : // Process [lh_lb, -1]
3912 639996 : wi_fold (tmp, type, lh_lb, wi::minus_one (prec), rh_lb, rh_ub);
3913 : // Now Process [0, rh_ub]
3914 639996 : wi_fold (r, type, wi::zero (prec), lh_ub, rh_lb, rh_ub);
3915 639996 : r.union_ (tmp);
3916 639996 : return;
3917 639996 : }
3918 :
3919 16296303 : if (wi_optimize_and_or (r, BIT_AND_EXPR, type, lh_lb, lh_ub, rh_lb, rh_ub))
3920 : return;
3921 :
3922 5286513 : wide_int maybe_nonzero_lh, mustbe_nonzero_lh;
3923 5286513 : wide_int maybe_nonzero_rh, mustbe_nonzero_rh;
3924 5286513 : wi_set_zero_nonzero_bits (type, lh_lb, lh_ub,
3925 : maybe_nonzero_lh, mustbe_nonzero_lh);
3926 5286513 : wi_set_zero_nonzero_bits (type, rh_lb, rh_ub,
3927 : maybe_nonzero_rh, mustbe_nonzero_rh);
3928 :
3929 5286513 : wide_int new_lb = mustbe_nonzero_lh & mustbe_nonzero_rh;
3930 5286513 : wide_int new_ub = maybe_nonzero_lh & maybe_nonzero_rh;
3931 5286513 : signop sign = TYPE_SIGN (type);
3932 5286513 : unsigned prec = TYPE_PRECISION (type);
3933 : // If both input ranges contain only negative values, we can
3934 : // truncate the result range maximum to the minimum of the
3935 : // input range maxima.
3936 5286513 : if (wi::lt_p (lh_ub, 0, sign) && wi::lt_p (rh_ub, 0, sign))
3937 : {
3938 44798 : new_ub = wi::min (new_ub, lh_ub, sign);
3939 44798 : new_ub = wi::min (new_ub, rh_ub, sign);
3940 : }
3941 : // If either input range contains only non-negative values
3942 : // we can truncate the result range maximum to the respective
3943 : // maximum of the input range.
3944 5286513 : if (wi::ge_p (lh_lb, 0, sign))
3945 4516483 : new_ub = wi::min (new_ub, lh_ub, sign);
3946 5286513 : if (wi::ge_p (rh_lb, 0, sign))
3947 5068613 : new_ub = wi::min (new_ub, rh_ub, sign);
3948 : // PR68217: In case of signed & sign-bit-CST should
3949 : // result in [-INF, 0] instead of [-INF, INF].
3950 5286513 : if (wi::gt_p (new_lb, new_ub, sign))
3951 : {
3952 54974 : wide_int sign_bit = wi::set_bit_in_zero (prec - 1, prec);
3953 54974 : if (sign == SIGNED
3954 54974 : && ((wi::eq_p (lh_lb, lh_ub)
3955 78 : && !wi::cmps (lh_lb, sign_bit))
3956 54974 : || (wi::eq_p (rh_lb, rh_ub)
3957 0 : && !wi::cmps (rh_lb, sign_bit))))
3958 : {
3959 0 : new_lb = wi::min_value (prec, sign);
3960 0 : new_ub = wi::zero (prec);
3961 : }
3962 54974 : }
3963 : // If the limits got swapped around, return varying.
3964 5286513 : if (wi::gt_p (new_lb, new_ub,sign))
3965 : {
3966 54974 : if (sign == SIGNED
3967 54974 : && wi_optimize_signed_bitwise_op (r, type,
3968 : lh_lb, lh_ub,
3969 : rh_lb, rh_ub))
3970 4069 : return;
3971 50905 : r.set_varying (type);
3972 : }
3973 : else
3974 5231539 : value_range_with_overflow (r, type, new_lb, new_ub);
3975 5286528 : }
3976 :
3977 : static void
3978 1059326 : set_nonzero_range_from_mask (irange &r, tree type, const irange &lhs)
3979 : {
3980 1059326 : if (lhs.undefined_p () || lhs.contains_zero_p ())
3981 466240 : r.set_varying (type);
3982 : else
3983 593086 : r.set_nonzero (type);
3984 1059326 : }
3985 :
3986 : /* Find out smallest RES where RES > VAL && (RES & MASK) == RES, if any
3987 : (otherwise return VAL). VAL and MASK must be zero-extended for
3988 : precision PREC. If SGNBIT is non-zero, first xor VAL with SGNBIT
3989 : (to transform signed values into unsigned) and at the end xor
3990 : SGNBIT back. */
3991 :
3992 : wide_int
3993 34652 : masked_increment (const wide_int &val_in, const wide_int &mask,
3994 : const wide_int &sgnbit, unsigned int prec)
3995 : {
3996 34652 : wide_int bit = wi::one (prec), res;
3997 34652 : unsigned int i;
3998 :
3999 34652 : wide_int val = val_in ^ sgnbit;
4000 566024 : for (i = 0; i < prec; i++, bit += bit)
4001 : {
4002 522054 : res = mask;
4003 522054 : if ((res & bit) == 0)
4004 451798 : continue;
4005 70256 : res = bit - 1;
4006 70256 : res = wi::bit_and_not (val + bit, res);
4007 70256 : res &= mask;
4008 70256 : if (wi::gtu_p (res, val))
4009 25334 : return res ^ sgnbit;
4010 : }
4011 9318 : return val ^ sgnbit;
4012 34652 : }
4013 :
4014 : // This was shamelessly stolen from register_edge_assert_for_2 and
4015 : // adjusted to work with iranges.
4016 :
4017 : void
4018 3852548 : operator_bitwise_and::simple_op1_range_solver (irange &r, tree type,
4019 : const irange &lhs,
4020 : const irange &op2) const
4021 : {
4022 3852548 : if (!op2.singleton_p ())
4023 : {
4024 1059295 : set_nonzero_range_from_mask (r, type, lhs);
4025 2124321 : return;
4026 : }
4027 2793253 : unsigned int nprec = TYPE_PRECISION (type);
4028 2793253 : wide_int cst2v = op2.lower_bound ();
4029 2793253 : bool cst2n = wi::neg_p (cst2v, TYPE_SIGN (type));
4030 2793253 : wide_int sgnbit;
4031 2793253 : if (cst2n)
4032 613283 : sgnbit = wi::set_bit_in_zero (nprec - 1, nprec);
4033 : else
4034 2179970 : sgnbit = wi::zero (nprec);
4035 :
4036 : // Solve [lhs.lower_bound (), +INF] = x & MASK.
4037 : //
4038 : // Minimum unsigned value for >= if (VAL & CST2) == VAL is VAL and
4039 : // maximum unsigned value is ~0. For signed comparison, if CST2
4040 : // doesn't have the most significant bit set, handle it similarly. If
4041 : // CST2 has MSB set, the minimum is the same, and maximum is ~0U/2.
4042 2793253 : wide_int valv = lhs.lower_bound ();
4043 2793253 : wide_int minv = valv & cst2v, maxv;
4044 2793253 : bool we_know_nothing = false;
4045 2793253 : if (minv != valv)
4046 : {
4047 : // If (VAL & CST2) != VAL, X & CST2 can't be equal to VAL.
4048 9214 : minv = masked_increment (valv, cst2v, sgnbit, nprec);
4049 9214 : if (minv == valv)
4050 : {
4051 : // If we can't determine anything on this bound, fall
4052 : // through and conservatively solve for the other end point.
4053 2793253 : we_know_nothing = true;
4054 : }
4055 : }
4056 4973223 : maxv = wi::mask (nprec - (cst2n ? 1 : 0), false, nprec);
4057 2793253 : if (we_know_nothing)
4058 3587 : r.set_varying (type);
4059 : else
4060 2789666 : create_possibly_reversed_range (r, type, minv, maxv);
4061 :
4062 : // Solve [-INF, lhs.upper_bound ()] = x & MASK.
4063 : //
4064 : // Minimum unsigned value for <= is 0 and maximum unsigned value is
4065 : // VAL | ~CST2 if (VAL & CST2) == VAL. Otherwise, find smallest
4066 : // VAL2 where
4067 : // VAL2 > VAL && (VAL2 & CST2) == VAL2 and use (VAL2 - 1) | ~CST2
4068 : // as maximum.
4069 : // For signed comparison, if CST2 doesn't have most significant bit
4070 : // set, handle it similarly. If CST2 has MSB set, the maximum is
4071 : // the same and minimum is INT_MIN.
4072 2793253 : valv = lhs.upper_bound ();
4073 2793253 : minv = valv & cst2v;
4074 2793253 : if (minv == valv)
4075 2767815 : maxv = valv;
4076 : else
4077 : {
4078 25438 : maxv = masked_increment (valv, cst2v, sgnbit, nprec);
4079 25438 : if (maxv == valv)
4080 : {
4081 : // If we couldn't determine anything on either bound, return
4082 : // undefined.
4083 5731 : if (we_know_nothing)
4084 2645 : r.set_undefined ();
4085 5731 : return;
4086 : }
4087 19707 : maxv -= 1;
4088 : }
4089 2787522 : maxv |= ~cst2v;
4090 2787522 : minv = sgnbit;
4091 2787522 : int_range<2> upper_bits;
4092 2787522 : create_possibly_reversed_range (upper_bits, type, minv, maxv);
4093 2787522 : r.intersect (upper_bits);
4094 2793253 : }
4095 :
4096 : bool
4097 6462161 : operator_bitwise_and::op1_range (irange &r, tree type,
4098 : const irange &lhs,
4099 : const irange &op2,
4100 : relation_trio) const
4101 : {
4102 6462161 : if (lhs.undefined_p ())
4103 : return false;
4104 6462161 : if (types_compatible_p (type, boolean_type_node))
4105 3349968 : return op_logical_and.op1_range (r, type, lhs, op2);
4106 :
4107 3112193 : r.set_undefined ();
4108 10076934 : for (unsigned i = 0; i < lhs.num_pairs (); ++i)
4109 : {
4110 7705096 : int_range_max chunk (lhs.type (),
4111 7705096 : lhs.lower_bound (i),
4112 7705096 : lhs.upper_bound (i));
4113 3852548 : int_range_max res;
4114 3852548 : simple_op1_range_solver (res, type, chunk, op2);
4115 3852548 : r.union_ (res);
4116 3852548 : }
4117 3112193 : if (r.undefined_p ())
4118 31 : set_nonzero_range_from_mask (r, type, lhs);
4119 :
4120 : // For MASK == op1 & MASK, all the bits in MASK must be set in op1.
4121 3112193 : wide_int mask;
4122 3112193 : if (lhs == op2 && lhs.singleton_p (mask))
4123 : {
4124 453099 : r.update_bitmask (irange_bitmask (mask, ~mask));
4125 453099 : return true;
4126 : }
4127 :
4128 2659094 : if (!op2.singleton_p (mask))
4129 : return true;
4130 :
4131 : // For 0 = op1 & MASK, op1 is ~MASK.
4132 1716327 : if (lhs.zero_p ())
4133 : {
4134 783398 : wide_int nz = wi::bit_not (op2.get_nonzero_bits ());
4135 783398 : int_range<2> tmp (type);
4136 783398 : tmp.set_nonzero_bits (nz);
4137 783398 : r.intersect (tmp);
4138 783398 : }
4139 :
4140 1716327 : irange_bitmask lhs_bm = lhs.get_bitmask ();
4141 : // given [5,7] mask 0x3 value 0x4 = N & [7, 7] mask 0x0 value 0x7
4142 : // Nothing is known about the bits not specified in the mask value (op2),
4143 : // Start with the mask, 1's will occur where values were masked.
4144 1716327 : wide_int op1_mask = ~mask;
4145 : // Any bits that are unknown on the LHS are also unknown in op1,
4146 : // so union the current mask with the LHS mask.
4147 1716327 : op1_mask |= lhs_bm.mask ();
4148 : // The resulting zeros correspond to known bits in the LHS mask, and
4149 : // the LHS value should tell us what they are. Mask off any
4150 : // extraneous values that are not covered by the mask.
4151 1716327 : wide_int op1_value = lhs_bm.value () & ~op1_mask;
4152 1716327 : irange_bitmask op1_bm (op1_value, op1_mask);
4153 : // Intersect this mask with anything already known about the value.
4154 : // A return valueof false indicated the bitmask is an UNDEFINED range.
4155 1716327 : if (op1_bm.intersect (r.get_bitmask ()))
4156 1716327 : r.update_bitmask (op1_bm);
4157 : else
4158 0 : r.set_undefined ();
4159 1716327 : return true;
4160 4828520 : }
4161 :
4162 : bool
4163 2390796 : operator_bitwise_and::op2_range (irange &r, tree type,
4164 : const irange &lhs,
4165 : const irange &op1,
4166 : relation_trio) const
4167 : {
4168 2390796 : return operator_bitwise_and::op1_range (r, type, lhs, op1);
4169 : }
4170 :
4171 :
4172 : class operator_logical_or : public range_operator
4173 : {
4174 : using range_operator::fold_range;
4175 : using range_operator::op1_range;
4176 : using range_operator::op2_range;
4177 : public:
4178 : bool fold_range (irange &r, tree type,
4179 : const irange &lh,
4180 : const irange &rh,
4181 : relation_trio rel = TRIO_VARYING) const final override;
4182 : bool op1_range (irange &r, tree type,
4183 : const irange &lhs,
4184 : const irange &op2,
4185 : relation_trio rel = TRIO_VARYING) const final override;
4186 : bool op2_range (irange &r, tree type,
4187 : const irange &lhs,
4188 : const irange &op1,
4189 : relation_trio rel = TRIO_VARYING) const final override;
4190 : // Check compatibility of all operands.
4191 0 : bool operand_check_p (tree t1, tree t2, tree t3) const final override
4192 0 : { return range_compatible_p (t1, t2) && range_compatible_p (t1, t3); }
4193 : } op_logical_or;
4194 :
4195 : bool
4196 0 : operator_logical_or::fold_range (irange &r, tree type ATTRIBUTE_UNUSED,
4197 : const irange &lh,
4198 : const irange &rh,
4199 : relation_trio) const
4200 : {
4201 0 : if (empty_range_varying (r, type, lh, rh))
4202 0 : return true;
4203 :
4204 0 : r = lh;
4205 0 : r.union_ (rh);
4206 0 : return true;
4207 : }
4208 :
4209 : bool
4210 1520634 : operator_logical_or::op1_range (irange &r, tree type,
4211 : const irange &lhs,
4212 : const irange &op2,
4213 : relation_trio) const
4214 : {
4215 1520634 : switch (get_bool_state (r, lhs, type))
4216 : {
4217 931991 : case BRS_FALSE:
4218 : // A false result means both sides of the OR must be false.
4219 931991 : r = range_false (type);
4220 931991 : return true;
4221 :
4222 588609 : case BRS_TRUE:
4223 : // A TRUE result when op2 is FALSE must have op1 TRUE.
4224 588609 : if (op2.zero_p ())
4225 : {
4226 4693 : r = range_true (type);
4227 4693 : return true;
4228 : }
4229 : break;
4230 :
4231 : default:
4232 : break;
4233 : }
4234 :
4235 : // Any other result means we cannot be sure of any result.
4236 583950 : r = range_true_and_false (type);
4237 583950 : return true;
4238 : }
4239 :
4240 : bool
4241 0 : operator_logical_or::op2_range (irange &r, tree type,
4242 : const irange &lhs,
4243 : const irange &op1,
4244 : relation_trio) const
4245 : {
4246 0 : return operator_logical_or::op1_range (r, type, lhs, op1);
4247 : }
4248 :
4249 :
4250 : void
4251 2481343 : operator_bitwise_or::update_bitmask (irange &r, const irange &lh,
4252 : const irange &rh) const
4253 : {
4254 2481343 : update_known_bitmask (r, BIT_IOR_EXPR, lh, rh);
4255 2481343 : }
4256 :
4257 : void
4258 7101146 : operator_bitwise_or::wi_fold (irange &r, tree type,
4259 : const wide_int &lh_lb,
4260 : const wide_int &lh_ub,
4261 : const wide_int &rh_lb,
4262 : const wide_int &rh_ub) const
4263 : {
4264 7101146 : if (wi_optimize_and_or (r, BIT_IOR_EXPR, type, lh_lb, lh_ub, rh_lb, rh_ub))
4265 6384261 : return;
4266 :
4267 913795 : wide_int maybe_nonzero_lh, mustbe_nonzero_lh;
4268 913795 : wide_int maybe_nonzero_rh, mustbe_nonzero_rh;
4269 913795 : wi_set_zero_nonzero_bits (type, lh_lb, lh_ub,
4270 : maybe_nonzero_lh, mustbe_nonzero_lh);
4271 913795 : wi_set_zero_nonzero_bits (type, rh_lb, rh_ub,
4272 : maybe_nonzero_rh, mustbe_nonzero_rh);
4273 913795 : wide_int new_lb = mustbe_nonzero_lh | mustbe_nonzero_rh;
4274 913795 : wide_int new_ub = maybe_nonzero_lh | maybe_nonzero_rh;
4275 913795 : signop sign = TYPE_SIGN (type);
4276 : // If the input ranges contain only positive values we can
4277 : // truncate the minimum of the result range to the maximum
4278 : // of the input range minima.
4279 913795 : if (wi::ge_p (lh_lb, 0, sign)
4280 913795 : && wi::ge_p (rh_lb, 0, sign))
4281 : {
4282 687460 : new_lb = wi::max (new_lb, lh_lb, sign);
4283 687461 : new_lb = wi::max (new_lb, rh_lb, sign);
4284 : }
4285 : // If either input range contains only negative values
4286 : // we can truncate the minimum of the result range to the
4287 : // respective minimum range.
4288 913795 : if (wi::lt_p (lh_ub, 0, sign))
4289 20222 : new_lb = wi::max (new_lb, lh_lb, sign);
4290 913795 : if (wi::lt_p (rh_ub, 0, sign))
4291 10163 : new_lb = wi::max (new_lb, rh_lb, sign);
4292 : // If the limits got swapped around, return a conservative range.
4293 913795 : if (wi::gt_p (new_lb, new_ub, sign))
4294 : {
4295 : // Make sure that nonzero|X is nonzero.
4296 196910 : if (wi::gt_p (lh_lb, 0, sign)
4297 191957 : || wi::gt_p (rh_lb, 0, sign)
4298 132693 : || wi::lt_p (lh_ub, 0, sign)
4299 388867 : || wi::lt_p (rh_ub, 0, sign))
4300 64217 : r.set_nonzero (type);
4301 132693 : else if (sign == SIGNED
4302 132693 : && wi_optimize_signed_bitwise_op (r, type,
4303 : lh_lb, lh_ub,
4304 : rh_lb, rh_ub))
4305 : return;
4306 : else
4307 130051 : r.set_varying (type);
4308 : return;
4309 : }
4310 716885 : value_range_with_overflow (r, type, new_lb, new_ub);
4311 913830 : }
4312 :
4313 : // When x == y, x | y is x.
4314 :
4315 : relation_kind
4316 2379449 : operator_bitwise_or::lhs_op1_relation (const irange &lhs,
4317 : const irange &op1,
4318 : const irange &op2,
4319 : relation_kind rel) const
4320 : {
4321 2379449 : if (lhs.undefined_p () || op1.undefined_p () || op2.undefined_p ())
4322 : return VREL_VARYING;
4323 2378233 : if (rel == VREL_EQ)
4324 24 : return VREL_EQ;
4325 : return VREL_VARYING;
4326 : }
4327 :
4328 : bool
4329 2481343 : operator_bitwise_or::op1_op2_relation_effect (irange &lhs_range,
4330 : tree type,
4331 : const irange &,
4332 : const irange &,
4333 : relation_kind rel) const
4334 : {
4335 2481343 : if (rel == VREL_VARYING)
4336 : return false;
4337 :
4338 6142 : int_range<2> rel_range;
4339 :
4340 6142 : switch (rel)
4341 : {
4342 853 : case VREL_NE:
4343 853 : rel_range.set_nonzero (type);
4344 853 : break;
4345 : default:
4346 : return false;
4347 : }
4348 :
4349 853 : lhs_range.intersect (rel_range);
4350 853 : return true;
4351 6142 : }
4352 :
4353 : bool
4354 2333492 : operator_bitwise_or::op1_range (irange &r, tree type,
4355 : const irange &lhs,
4356 : const irange &op2,
4357 : relation_trio) const
4358 : {
4359 2333492 : if (lhs.undefined_p ())
4360 : return false;
4361 : // If this is really a logical wi_fold, call that.
4362 2333492 : if (types_compatible_p (type, boolean_type_node))
4363 1520634 : return op_logical_or.op1_range (r, type, lhs, op2);
4364 :
4365 812858 : if (lhs.zero_p ())
4366 : {
4367 378333 : r.set_zero (type);
4368 378333 : return true;
4369 : }
4370 :
4371 : // if (A < 0 && B < 0)
4372 : // Sometimes gets translated to
4373 : // _1 = A | B
4374 : // if (_1 < 0))
4375 : // It is useful for ranger to recognize a positive LHS means the RHS
4376 : // operands are also positive when dealing with the ELSE range..
4377 592670 : if (TYPE_SIGN (type) == SIGNED && wi::ge_p (lhs.lower_bound (), 0, SIGNED))
4378 : {
4379 66543 : unsigned prec = TYPE_PRECISION (type);
4380 66543 : r.set (type, wi::zero (prec), wi::max_value (prec, SIGNED));
4381 66543 : return true;
4382 : }
4383 367982 : r.set_varying (type);
4384 367982 : return true;
4385 : }
4386 :
4387 : bool
4388 1145794 : operator_bitwise_or::op2_range (irange &r, tree type,
4389 : const irange &lhs,
4390 : const irange &op1,
4391 : relation_trio) const
4392 : {
4393 1145794 : return operator_bitwise_or::op1_range (r, type, lhs, op1);
4394 : }
4395 :
4396 : void
4397 99737 : operator_bitwise_xor::update_bitmask (irange &r, const irange &lh,
4398 : const irange &rh) const
4399 : {
4400 99737 : update_known_bitmask (r, BIT_XOR_EXPR, lh, rh);
4401 99737 : }
4402 :
4403 : bool
4404 340459 : operator_bitwise_xor::fold_range (irange &r, tree type,
4405 : const irange &lh, const irange &rh,
4406 : relation_trio rel) const
4407 : {
4408 : // Handle X ^ UNDEFINED = UNDEFINED.
4409 340459 : if (lh.undefined_p () || rh.undefined_p ())
4410 : {
4411 410 : r.set_undefined ();
4412 410 : return true;
4413 : }
4414 :
4415 : // Next, handle X ^ X == [0, 0].
4416 340049 : if (rel.op1_op2 () == VREL_EQ)
4417 : {
4418 131 : r.set_zero (type);
4419 131 : return true;
4420 : }
4421 :
4422 : // If either operand is VARYING, the result is VARYING.
4423 339918 : if (lh.varying_p () || rh.varying_p ())
4424 : {
4425 : // If the operands are not equal, zero is not possible.
4426 237324 : if (rel.op1_op2 () != VREL_NE)
4427 236209 : r.set_varying (type);
4428 : else
4429 1115 : r.set_nonzero (type);
4430 : return true;
4431 : }
4432 :
4433 : // Now deal with X ^ 0 == X.
4434 102594 : if (lh.zero_p ())
4435 : {
4436 2340 : r = rh;
4437 2340 : return true;
4438 : }
4439 100254 : if (rh.zero_p ())
4440 : {
4441 517 : r = lh;
4442 517 : return true;
4443 : }
4444 :
4445 : // Start with the legacy range. This can sometimes pick up values
4446 : // when there are a lot of subranges and fold_range aggregates them.
4447 99737 : bool res = range_operator::fold_range (r, type, lh, rh, rel);
4448 :
4449 : // Calculate the XOR identity : x ^ y = (x | y) & ~(x & y)
4450 : // AND and OR are already much better optimized.
4451 99737 : int_range_max tmp1, tmp2, tmp3, new_result;
4452 99737 : int_range<2> varying;
4453 99737 : varying.set_varying (type);
4454 :
4455 99737 : if (m_or.fold_range (tmp1, type, lh, rh, rel)
4456 99737 : && m_and.fold_range (tmp2, type, lh, rh, rel)
4457 99737 : && m_not.fold_range (tmp3, type, tmp2, varying, rel)
4458 199474 : && m_and.fold_range (new_result, type, tmp1, tmp3, rel))
4459 : {
4460 : // If the operands are not equal, or the LH does not contain any
4461 : // element of the RH, zero is not possible.
4462 99737 : tmp1 = lh;
4463 99737 : if (rel.op1_op2 () == VREL_NE
4464 99737 : || (tmp1.intersect (rh) && tmp1.undefined_p ()))
4465 : {
4466 38144 : tmp1.set_nonzero (type);
4467 38144 : new_result.intersect (tmp1);
4468 : }
4469 :
4470 : // Combine with the legacy range if there was one.
4471 99737 : if (res)
4472 99737 : r.intersect (new_result);
4473 : else
4474 0 : r = new_result;
4475 : return true;
4476 : }
4477 : return res;
4478 99737 : }
4479 :
4480 : void
4481 192884 : operator_bitwise_xor::wi_fold (irange &r, tree type,
4482 : const wide_int &lh_lb,
4483 : const wide_int &lh_ub,
4484 : const wide_int &rh_lb,
4485 : const wide_int &rh_ub) const
4486 : {
4487 192884 : signop sign = TYPE_SIGN (type);
4488 192884 : wide_int maybe_nonzero_lh, mustbe_nonzero_lh;
4489 192884 : wide_int maybe_nonzero_rh, mustbe_nonzero_rh;
4490 192884 : wi_set_zero_nonzero_bits (type, lh_lb, lh_ub,
4491 : maybe_nonzero_lh, mustbe_nonzero_lh);
4492 192884 : wi_set_zero_nonzero_bits (type, rh_lb, rh_ub,
4493 : maybe_nonzero_rh, mustbe_nonzero_rh);
4494 :
4495 578652 : wide_int result_zero_bits = ((mustbe_nonzero_lh & mustbe_nonzero_rh)
4496 578652 : | ~(maybe_nonzero_lh | maybe_nonzero_rh));
4497 192884 : wide_int result_one_bits
4498 385768 : = (wi::bit_and_not (mustbe_nonzero_lh, maybe_nonzero_rh)
4499 385768 : | wi::bit_and_not (mustbe_nonzero_rh, maybe_nonzero_lh));
4500 192884 : wide_int new_ub = ~result_zero_bits;
4501 192884 : wide_int new_lb = result_one_bits;
4502 :
4503 : // If the range has all positive or all negative values, the result
4504 : // is better than VARYING.
4505 192884 : if (wi::lt_p (new_lb, 0, sign) || wi::ge_p (new_ub, 0, sign))
4506 187077 : value_range_with_overflow (r, type, new_lb, new_ub);
4507 5807 : else if (sign == SIGNED
4508 5807 : && wi_optimize_signed_bitwise_op (r, type,
4509 : lh_lb, lh_ub,
4510 : rh_lb, rh_ub))
4511 : ; /* Do nothing. */
4512 : else
4513 1007 : r.set_varying (type);
4514 :
4515 : /* Furthermore, XOR is non-zero if its arguments can't be equal. */
4516 192884 : if (wi::lt_p (lh_ub, rh_lb, sign)
4517 126772 : || wi::lt_p (rh_ub, lh_lb, sign)
4518 319656 : || wi::ne_p (result_one_bits, 0))
4519 : {
4520 88531 : int_range<2> tmp;
4521 88531 : tmp.set_nonzero (type);
4522 88531 : r.intersect (tmp);
4523 88531 : }
4524 192884 : }
4525 :
4526 : bool
4527 99737 : operator_bitwise_xor::op1_op2_relation_effect (irange &lhs_range,
4528 : tree type,
4529 : const irange &,
4530 : const irange &,
4531 : relation_kind rel) const
4532 : {
4533 99737 : if (rel == VREL_VARYING)
4534 : return false;
4535 :
4536 4780 : int_range<2> rel_range;
4537 :
4538 4780 : switch (rel)
4539 : {
4540 0 : case VREL_EQ:
4541 0 : rel_range.set_zero (type);
4542 0 : break;
4543 600 : case VREL_NE:
4544 600 : rel_range.set_nonzero (type);
4545 600 : break;
4546 : default:
4547 : return false;
4548 : }
4549 :
4550 600 : lhs_range.intersect (rel_range);
4551 600 : return true;
4552 4780 : }
4553 :
4554 : bool
4555 186360 : operator_bitwise_xor::op1_range (irange &r, tree type,
4556 : const irange &lhs,
4557 : const irange &op2,
4558 : relation_trio) const
4559 : {
4560 186360 : if (lhs.undefined_p () || lhs.varying_p ())
4561 : {
4562 112 : r = lhs;
4563 112 : return true;
4564 : }
4565 186248 : if (types_compatible_p (type, boolean_type_node))
4566 : {
4567 2998 : switch (get_bool_state (r, lhs, type))
4568 : {
4569 1245 : case BRS_TRUE:
4570 1245 : if (op2.varying_p ())
4571 1197 : r.set_varying (type);
4572 48 : else if (op2.zero_p ())
4573 36 : r = range_true (type);
4574 : // See get_bool_state for the rationale
4575 12 : else if (op2.undefined_p () || op2.contains_zero_p ())
4576 0 : r = range_true_and_false (type);
4577 : else
4578 12 : r = range_false (type);
4579 : break;
4580 1753 : case BRS_FALSE:
4581 1753 : r = op2;
4582 1753 : break;
4583 : default:
4584 : break;
4585 : }
4586 : return true;
4587 : }
4588 183250 : r.set_varying (type);
4589 183250 : return true;
4590 : }
4591 :
4592 : bool
4593 87731 : operator_bitwise_xor::op2_range (irange &r, tree type,
4594 : const irange &lhs,
4595 : const irange &op1,
4596 : relation_trio) const
4597 : {
4598 87731 : return operator_bitwise_xor::op1_range (r, type, lhs, op1);
4599 : }
4600 :
4601 : class operator_trunc_mod : public range_operator
4602 : {
4603 : using range_operator::op1_range;
4604 : using range_operator::op2_range;
4605 : using range_operator::update_bitmask;
4606 : using range_operator::op1_op2_relation_effect;
4607 : public:
4608 : virtual void wi_fold (irange &r, tree type,
4609 : const wide_int &lh_lb,
4610 : const wide_int &lh_ub,
4611 : const wide_int &rh_lb,
4612 : const wide_int &rh_ub) const override;
4613 : virtual bool op1_range (irange &r, tree type,
4614 : const irange &lhs,
4615 : const irange &op2,
4616 : relation_trio) const override;
4617 : virtual bool op2_range (irange &r, tree type,
4618 : const irange &lhs,
4619 : const irange &op1,
4620 : relation_trio) const override;
4621 1311034 : void update_bitmask (irange &r, const irange &lh,
4622 : const irange &rh) const override
4623 1311034 : { update_known_bitmask (r, TRUNC_MOD_EXPR, lh, rh); }
4624 : bool op1_op2_relation_effect (irange &lhs_range,
4625 : tree type,
4626 : const irange &op1_range,
4627 : const irange &op2_range,
4628 : relation_kind rel) const final override;
4629 : protected:
4630 : bool same_quotient (wide_int &lb, wide_int &ub, const wide_int &x_lb,
4631 : const wide_int &x_ub, const wide_int &y_lb,
4632 : const wide_int &y_ub, signop sign) const;
4633 : } op_trunc_mod;
4634 :
4635 :
4636 : bool
4637 1565151 : operator_trunc_mod::same_quotient (wide_int &lb, wide_int &ub,
4638 : const wide_int &x_lb, const wide_int &x_ub,
4639 : const wide_int &y_lb, const wide_int &y_ub,
4640 : signop sign) const
4641 : {
4642 : // Y must not contain zero.
4643 1565151 : if (wi::ge_p (y_ub, 0, sign) && wi::le_p (y_lb, 0, sign))
4644 1059146 : return false;
4645 :
4646 : // If all endpoints do not have the same quotient, nothing further.
4647 506005 : wide_int q = wi::div_trunc (x_lb, y_lb, sign);
4648 506053 : if (q != wi::div_trunc (x_lb, y_ub, sign))
4649 : return false;
4650 463093 : if (q != wi::div_trunc (x_ub, y_lb, sign))
4651 : return false;
4652 48314 : if (q != wi::div_trunc (x_ub, y_ub, sign))
4653 : return false;
4654 :
4655 : // Compute the range extremes.
4656 47931 : wide_int v1 = x_lb - q * y_lb;
4657 47931 : wide_int v2 = x_lb - q * y_ub;
4658 47931 : wide_int v3 = x_ub - q * y_lb;
4659 47931 : wide_int v4 = x_ub - q * y_ub;
4660 :
4661 : // And choose the minimum and maximum
4662 47931 : lb = wi::min (v1, v2, sign);
4663 47931 : lb = wi::min (lb, v3, sign);
4664 47931 : lb = wi::min (lb, v4, sign);
4665 :
4666 47931 : ub = wi::max (v1, v2, sign);
4667 47931 : ub = wi::max (ub, v3, sign);
4668 47931 : ub = wi::max (ub, v4, sign);
4669 :
4670 47931 : return true;
4671 553936 : }
4672 :
4673 : void
4674 1602065 : operator_trunc_mod::wi_fold (irange &r, tree type,
4675 : const wide_int &lh_lb,
4676 : const wide_int &lh_ub,
4677 : const wide_int &rh_lb,
4678 : const wide_int &rh_ub) const
4679 : {
4680 1602065 : wide_int new_lb, new_ub, tmp;
4681 1602065 : signop sign = TYPE_SIGN (type);
4682 1602065 : unsigned prec = TYPE_PRECISION (type);
4683 :
4684 : // Mod 0 is undefined.
4685 1602065 : if (wi_zero_p (type, rh_lb, rh_ub))
4686 : {
4687 11647 : r.set_undefined ();
4688 11647 : return;
4689 : }
4690 :
4691 : // Check for constant and try to fold.
4692 1858051 : if (lh_lb == lh_ub && rh_lb == rh_ub)
4693 : {
4694 25293 : wi::overflow_type ov = wi::OVF_NONE;
4695 25293 : tmp = wi::mod_trunc (lh_lb, rh_lb, sign, &ov);
4696 25293 : if (ov == wi::OVF_NONE)
4697 : {
4698 25267 : r = int_range<2> (type, tmp, tmp);
4699 25267 : return;
4700 : }
4701 : }
4702 :
4703 : // ABS (A % B) < ABS (B) and either 0 <= A % B <= A or A <= A % B <= 0.
4704 1565151 : new_ub = rh_ub - 1;
4705 1565151 : if (sign == SIGNED)
4706 : {
4707 681418 : tmp = -1 - rh_lb;
4708 681418 : new_ub = wi::smax (new_ub, tmp);
4709 : }
4710 :
4711 681418 : if (sign == UNSIGNED)
4712 883733 : new_lb = wi::zero (prec);
4713 : else
4714 : {
4715 681418 : new_lb = -new_ub;
4716 681418 : tmp = lh_lb;
4717 681418 : if (wi::gts_p (tmp, 0))
4718 167641 : tmp = wi::zero (prec);
4719 681467 : new_lb = wi::smax (new_lb, tmp);
4720 : }
4721 1565151 : tmp = lh_ub;
4722 1565151 : if (sign == SIGNED && wi::neg_p (tmp))
4723 35155 : tmp = wi::zero (prec);
4724 1565151 : new_ub = wi::min (new_ub, tmp, sign);
4725 :
4726 1565151 : value_range_with_overflow (r, type, new_lb, new_ub);
4727 :
4728 : // If all X/Y combinations have the same quotient, refine using
4729 : // X % Y == X - Q * Y.
4730 1565151 : if (same_quotient (new_lb, new_ub, lh_lb, lh_ub, rh_lb, rh_ub, sign))
4731 : {
4732 47931 : int_range<2> refined (type, new_lb, new_ub);
4733 47931 : r.intersect (refined);
4734 47931 : }
4735 1602395 : }
4736 :
4737 : bool
4738 418120 : operator_trunc_mod::op1_range (irange &r, tree type,
4739 : const irange &lhs,
4740 : const irange &,
4741 : relation_trio) const
4742 : {
4743 418120 : if (lhs.undefined_p ())
4744 : return false;
4745 : // PR 91029.
4746 418120 : signop sign = TYPE_SIGN (type);
4747 418120 : unsigned prec = TYPE_PRECISION (type);
4748 : // (a % b) >= x && x > 0 , then a >= x.
4749 418176 : if (wi::gt_p (lhs.lower_bound (), 0, sign))
4750 : {
4751 287683 : r.set (type, lhs.lower_bound (), wi::max_value (prec, sign));
4752 287629 : return true;
4753 : }
4754 : // (a % b) <= x && x < 0 , then a <= x.
4755 130493 : if (wi::lt_p (lhs.upper_bound (), 0, sign))
4756 : {
4757 15660 : r.set (type, wi::min_value (prec, sign), lhs.upper_bound ());
4758 15660 : return true;
4759 : }
4760 : return false;
4761 : }
4762 :
4763 : bool
4764 186790 : operator_trunc_mod::op2_range (irange &r, tree type,
4765 : const irange &lhs,
4766 : const irange &,
4767 : relation_trio) const
4768 : {
4769 186790 : if (lhs.undefined_p ())
4770 : return false;
4771 : // PR 91029.
4772 186790 : signop sign = TYPE_SIGN (type);
4773 186790 : unsigned prec = TYPE_PRECISION (type);
4774 : // (a % b) >= x && x > 0 , then b is in ~[-x, x] for signed
4775 : // or b > x for unsigned.
4776 186801 : if (wi::gt_p (lhs.lower_bound (), 0, sign))
4777 : {
4778 107965 : if (sign == SIGNED)
4779 4150 : r.set (type, wi::neg (lhs.lower_bound ()),
4780 8300 : lhs.lower_bound (), VR_ANTI_RANGE);
4781 103833 : else if (wi::lt_p (lhs.lower_bound (), wi::max_value (prec, sign),
4782 : sign))
4783 103842 : r.set (type, lhs.lower_bound () + 1, wi::max_value (prec, sign));
4784 : else
4785 : return false;
4786 : return true;
4787 : }
4788 : // (a % b) <= x && x < 0 , then b is in ~[x, -x].
4789 78827 : if (wi::lt_p (lhs.upper_bound (), 0, sign))
4790 : {
4791 9049 : if (wi::gt_p (lhs.upper_bound (), wi::min_value (prec, sign), sign))
4792 9049 : r.set (type, lhs.upper_bound (),
4793 18098 : wi::neg (lhs.upper_bound ()), VR_ANTI_RANGE);
4794 : else
4795 : return false;
4796 9049 : return true;
4797 : }
4798 : return false;
4799 : }
4800 :
4801 : bool
4802 1311034 : operator_trunc_mod::op1_op2_relation_effect (irange &lhs_range,
4803 : tree type,
4804 : const irange &,
4805 : const irange &,
4806 : relation_kind rel) const
4807 : {
4808 1311034 : if (rel == VREL_VARYING)
4809 : return false;
4810 :
4811 8296 : int_range<2> rel_range;
4812 :
4813 8296 : switch (rel)
4814 : {
4815 19 : case VREL_EQ:
4816 19 : rel_range.set_zero (type);
4817 19 : break;
4818 : default:
4819 : return false;
4820 : }
4821 :
4822 19 : lhs_range.intersect (rel_range);
4823 19 : return true;
4824 8296 : }
4825 :
4826 :
4827 : class operator_logical_not : public range_operator
4828 : {
4829 : using range_operator::fold_range;
4830 : using range_operator::op1_range;
4831 : public:
4832 : bool fold_range (irange &r, tree type,
4833 : const irange &lh,
4834 : const irange &rh,
4835 : relation_trio rel = TRIO_VARYING) const final override;
4836 : bool op1_range (irange &r, tree type,
4837 : const irange &lhs,
4838 : const irange &op2,
4839 : relation_trio rel = TRIO_VARYING) const final override;
4840 : // Check compatibility of LHS and op1.
4841 0 : bool operand_check_p (tree t1, tree t2, tree) const final override
4842 0 : { return range_compatible_p (t1, t2); }
4843 : } op_logical_not;
4844 :
4845 : // Folding a logical NOT, oddly enough, involves doing nothing on the
4846 : // forward pass through. During the initial walk backwards, the
4847 : // logical NOT reversed the desired outcome on the way back, so on the
4848 : // way forward all we do is pass the range forward.
4849 : //
4850 : // b_2 = x_1 < 20
4851 : // b_3 = !b_2
4852 : // if (b_3)
4853 : // to determine the TRUE branch, walking backward
4854 : // if (b_3) if ([1,1])
4855 : // b_3 = !b_2 [1,1] = ![0,0]
4856 : // b_2 = x_1 < 20 [0,0] = x_1 < 20, false, so x_1 == [20, 255]
4857 : // which is the result we are looking for.. so.. pass it through.
4858 :
4859 : bool
4860 257455 : operator_logical_not::fold_range (irange &r, tree type,
4861 : const irange &lh,
4862 : const irange &rh ATTRIBUTE_UNUSED,
4863 : relation_trio) const
4864 : {
4865 257455 : if (empty_range_varying (r, type, lh, rh))
4866 0 : return true;
4867 :
4868 257455 : r = lh;
4869 257455 : if (!lh.varying_p () && !lh.undefined_p ())
4870 : {
4871 66576 : if (!r.invert ())
4872 : return false;
4873 : }
4874 : return true;
4875 : }
4876 :
4877 : bool
4878 41661 : operator_logical_not::op1_range (irange &r,
4879 : tree type,
4880 : const irange &lhs,
4881 : const irange &op2,
4882 : relation_trio) const
4883 : {
4884 : // Logical NOT is involutary...do it again.
4885 41661 : return fold_range (r, type, lhs, op2);
4886 : }
4887 :
4888 : bool
4889 616750 : operator_bitwise_not::fold_range (irange &r, tree type,
4890 : const irange &lh,
4891 : const irange &rh,
4892 : relation_trio) const
4893 : {
4894 616750 : if (empty_range_varying (r, type, lh, rh))
4895 112 : return true;
4896 :
4897 616638 : if (types_compatible_p (type, boolean_type_node))
4898 215794 : return op_logical_not.fold_range (r, type, lh, rh);
4899 :
4900 : // ~X is simply -1 - X.
4901 801688 : int_range<1> minusone (type, wi::minus_one (TYPE_PRECISION (type)),
4902 801688 : wi::minus_one (TYPE_PRECISION (type)));
4903 400844 : return range_op_handler (MINUS_EXPR).fold_range (r, type, minusone, lh);
4904 400844 : }
4905 :
4906 : bool
4907 54272 : operator_bitwise_not::op1_range (irange &r, tree type,
4908 : const irange &lhs,
4909 : const irange &op2,
4910 : relation_trio) const
4911 : {
4912 54272 : if (lhs.undefined_p ())
4913 : return false;
4914 54272 : if (types_compatible_p (type, boolean_type_node))
4915 41661 : return op_logical_not.op1_range (r, type, lhs, op2);
4916 :
4917 : // ~X is -1 - X and since bitwise NOT is involutary...do it again.
4918 12611 : return fold_range (r, type, lhs, op2);
4919 : }
4920 :
4921 : void
4922 0 : operator_bitwise_not::update_bitmask (irange &r, const irange &lh,
4923 : const irange &rh) const
4924 : {
4925 0 : update_known_bitmask (r, BIT_NOT_EXPR, lh, rh);
4926 0 : }
4927 :
4928 :
4929 : bool
4930 704536 : operator_cst::fold_range (irange &r, tree type ATTRIBUTE_UNUSED,
4931 : const irange &lh,
4932 : const irange &rh ATTRIBUTE_UNUSED,
4933 : relation_trio) const
4934 : {
4935 704536 : r = lh;
4936 704536 : return true;
4937 : }
4938 :
4939 :
4940 : // Determine if there is a relationship between LHS and OP1.
4941 :
4942 : relation_kind
4943 1472174 : operator_identity::lhs_op1_relation (const irange &lhs,
4944 : const irange &op1 ATTRIBUTE_UNUSED,
4945 : const irange &op2 ATTRIBUTE_UNUSED,
4946 : relation_kind) const
4947 : {
4948 1472174 : if (lhs.undefined_p ())
4949 9876 : return VREL_VARYING;
4950 : // Simply a copy, so they are equivalent.
4951 : return VREL_EQ;
4952 : }
4953 :
4954 : bool
4955 1472174 : operator_identity::fold_range (irange &r, tree type ATTRIBUTE_UNUSED,
4956 : const irange &lh,
4957 : const irange &rh ATTRIBUTE_UNUSED,
4958 : relation_trio) const
4959 : {
4960 1472174 : r = lh;
4961 1472174 : return true;
4962 : }
4963 :
4964 : bool
4965 317617 : operator_identity::op1_range (irange &r, tree type ATTRIBUTE_UNUSED,
4966 : const irange &lhs,
4967 : const irange &op2 ATTRIBUTE_UNUSED,
4968 : relation_trio) const
4969 : {
4970 317617 : r = lhs;
4971 317617 : return true;
4972 : }
4973 :
4974 :
4975 : class operator_unknown : public range_operator
4976 : {
4977 : using range_operator::fold_range;
4978 : public:
4979 : virtual bool fold_range (irange &r, tree type,
4980 : const irange &op1,
4981 : const irange &op2,
4982 : relation_trio rel = TRIO_VARYING) const;
4983 : } op_unknown;
4984 :
4985 : bool
4986 1490356 : operator_unknown::fold_range (irange &r, tree type,
4987 : const irange &lh ATTRIBUTE_UNUSED,
4988 : const irange &rh ATTRIBUTE_UNUSED,
4989 : relation_trio) const
4990 : {
4991 1490356 : r.set_varying (type);
4992 1490356 : return true;
4993 : }
4994 :
4995 :
4996 : void
4997 83305 : operator_abs::wi_fold (irange &r, tree type,
4998 : const wide_int &lh_lb, const wide_int &lh_ub,
4999 : const wide_int &rh_lb ATTRIBUTE_UNUSED,
5000 : const wide_int &rh_ub ATTRIBUTE_UNUSED) const
5001 : {
5002 83305 : wide_int min, max;
5003 83305 : signop sign = TYPE_SIGN (type);
5004 83305 : unsigned prec = TYPE_PRECISION (type);
5005 :
5006 : // Pass through LH for the easy cases.
5007 83305 : if (sign == UNSIGNED || wi::ge_p (lh_lb, 0, sign))
5008 : {
5009 6175 : r = int_range<1> (type, lh_lb, lh_ub);
5010 6175 : return;
5011 : }
5012 :
5013 : // -TYPE_MIN_VALUE = TYPE_MIN_VALUE with flag_wrapv so we can't get
5014 : // a useful range.
5015 77130 : wide_int min_value = wi::min_value (prec, sign);
5016 77130 : wide_int max_value = wi::max_value (prec, sign);
5017 77130 : if (!TYPE_OVERFLOW_UNDEFINED (type) && wi::eq_p (lh_lb, min_value))
5018 : {
5019 549 : r.set_varying (type);
5020 549 : return;
5021 : }
5022 :
5023 : // ABS_EXPR may flip the range around, if the original range
5024 : // included negative values.
5025 76581 : if (wi::eq_p (lh_lb, min_value))
5026 : {
5027 : // ABS ([-MIN, -MIN]) isn't representable, but we have traditionally
5028 : // returned [-MIN,-MIN] so this preserves that behavior. PR37078
5029 28227 : if (wi::eq_p (lh_ub, min_value))
5030 : {
5031 60 : r = int_range<1> (type, min_value, min_value);
5032 60 : return;
5033 : }
5034 28167 : min = max_value;
5035 : }
5036 : else
5037 48354 : min = wi::abs (lh_lb);
5038 :
5039 76521 : if (wi::eq_p (lh_ub, min_value))
5040 0 : max = max_value;
5041 : else
5042 76521 : max = wi::abs (lh_ub);
5043 :
5044 : // If the range contains zero then we know that the minimum value in the
5045 : // range will be zero.
5046 76521 : if (wi::le_p (lh_lb, 0, sign) && wi::ge_p (lh_ub, 0, sign))
5047 : {
5048 69805 : if (wi::gt_p (min, max, sign))
5049 40332 : max = min;
5050 69805 : min = wi::zero (prec);
5051 : }
5052 : else
5053 : {
5054 : // If the range was reversed, swap MIN and MAX.
5055 6716 : if (wi::gt_p (min, max, sign))
5056 6554 : std::swap (min, max);
5057 : }
5058 :
5059 : // If the new range has its limits swapped around (MIN > MAX), then
5060 : // the operation caused one of them to wrap around. The only thing
5061 : // we know is that the result is positive.
5062 76521 : if (wi::gt_p (min, max, sign))
5063 : {
5064 0 : min = wi::zero (prec);
5065 0 : max = max_value;
5066 : }
5067 76521 : r = int_range<1> (type, min, max);
5068 83914 : }
5069 :
5070 : bool
5071 77492 : operator_abs::op1_range (irange &r, tree type,
5072 : const irange &lhs,
5073 : const irange &op2,
5074 : relation_trio) const
5075 : {
5076 77492 : if (empty_range_varying (r, type, lhs, op2))
5077 0 : return true;
5078 77492 : if (TYPE_UNSIGNED (type))
5079 : {
5080 0 : r = lhs;
5081 0 : return true;
5082 : }
5083 : // Start with the positives because negatives are an impossible result.
5084 77492 : int_range_max positives = range_positives (type);
5085 77492 : positives.intersect (lhs);
5086 77492 : r = positives;
5087 : // Then add the negative of each pair:
5088 : // ABS(op1) = [5,20] would yield op1 => [-20,-5][5,20].
5089 233311 : for (unsigned i = 0; i < positives.num_pairs (); ++i)
5090 78327 : r.union_ (int_range<1> (type,
5091 156654 : -positives.upper_bound (i),
5092 234981 : -positives.lower_bound (i)));
5093 : // With flag_wrapv, -TYPE_MIN_VALUE = TYPE_MIN_VALUE which is
5094 : // unrepresentable. Add -TYPE_MIN_VALUE in this case.
5095 77492 : wide_int min_value = wi::min_value (TYPE_PRECISION (type), TYPE_SIGN (type));
5096 77492 : wide_int lb = lhs.lower_bound ();
5097 77492 : if (!TYPE_OVERFLOW_UNDEFINED (type) && wi::eq_p (lb, min_value))
5098 15 : r.union_ (int_range<2> (type, lb, lb));
5099 77492 : return true;
5100 77492 : }
5101 :
5102 : void
5103 77619 : operator_abs::update_bitmask (irange &r, const irange &lh,
5104 : const irange &rh) const
5105 : {
5106 77619 : update_known_bitmask (r, ABS_EXPR, lh, rh);
5107 77619 : }
5108 :
5109 : class operator_absu : public range_operator
5110 : {
5111 : using range_operator::update_bitmask;
5112 : public:
5113 : virtual void wi_fold (irange &r, tree type,
5114 : const wide_int &lh_lb, const wide_int &lh_ub,
5115 : const wide_int &rh_lb, const wide_int &rh_ub)
5116 : const final override;
5117 : virtual void update_bitmask (irange &r, const irange &lh,
5118 : const irange &rh) const final override;
5119 : } op_absu;
5120 :
5121 : void
5122 7695 : operator_absu::wi_fold (irange &r, tree type,
5123 : const wide_int &lh_lb, const wide_int &lh_ub,
5124 : const wide_int &rh_lb ATTRIBUTE_UNUSED,
5125 : const wide_int &rh_ub ATTRIBUTE_UNUSED) const
5126 : {
5127 7695 : wide_int new_lb, new_ub;
5128 :
5129 : // Pass through VR0 the easy cases.
5130 7695 : if (wi::ges_p (lh_lb, 0))
5131 : {
5132 390 : new_lb = lh_lb;
5133 390 : new_ub = lh_ub;
5134 : }
5135 : else
5136 : {
5137 7305 : new_lb = wi::abs (lh_lb);
5138 7305 : new_ub = wi::abs (lh_ub);
5139 :
5140 : // If the range contains zero then we know that the minimum
5141 : // value in the range will be zero.
5142 7305 : if (wi::ges_p (lh_ub, 0))
5143 : {
5144 6902 : if (wi::gtu_p (new_lb, new_ub))
5145 5812 : new_ub = new_lb;
5146 6902 : new_lb = wi::zero (TYPE_PRECISION (type));
5147 : }
5148 : else
5149 403 : std::swap (new_lb, new_ub);
5150 : }
5151 :
5152 7695 : gcc_checking_assert (TYPE_UNSIGNED (type));
5153 7695 : r = int_range<1> (type, new_lb, new_ub);
5154 7695 : }
5155 :
5156 : void
5157 7341 : operator_absu::update_bitmask (irange &r, const irange &lh,
5158 : const irange &rh) const
5159 : {
5160 7341 : update_known_bitmask (r, ABSU_EXPR, lh, rh);
5161 7341 : }
5162 :
5163 :
5164 : bool
5165 590495 : operator_negate::fold_range (irange &r, tree type,
5166 : const irange &lh,
5167 : const irange &rh,
5168 : relation_trio) const
5169 : {
5170 590495 : if (empty_range_varying (r, type, lh, rh))
5171 961 : return true;
5172 :
5173 : // -X is simply 0 - X.
5174 589534 : int_range<1> zero;
5175 589534 : zero.set_zero (type);
5176 589534 : return range_op_handler (MINUS_EXPR).fold_range (r, type, zero, lh);
5177 589534 : }
5178 :
5179 : bool
5180 44625 : operator_negate::op1_range (irange &r, tree type,
5181 : const irange &lhs,
5182 : const irange &op2,
5183 : relation_trio) const
5184 : {
5185 : // NEGATE is involutory.
5186 44625 : return fold_range (r, type, lhs, op2);
5187 : }
5188 :
5189 :
5190 : bool
5191 0 : operator_addr_expr::fold_range (irange &r, tree type,
5192 : const irange &lh,
5193 : const irange &rh,
5194 : relation_trio) const
5195 : {
5196 0 : if (empty_range_varying (r, type, lh, rh))
5197 0 : return true;
5198 :
5199 : // Return a non-null pointer of the LHS type (passed in op2).
5200 0 : if (lh.zero_p ())
5201 0 : r.set_zero (type);
5202 0 : else if (lh.undefined_p () || lh.contains_zero_p ())
5203 0 : r.set_varying (type);
5204 : else
5205 0 : r.set_nonzero (type);
5206 : return true;
5207 : }
5208 :
5209 : bool
5210 0 : operator_addr_expr::op1_range (irange &r, tree type,
5211 : const irange &lhs,
5212 : const irange &op2,
5213 : relation_trio) const
5214 : {
5215 0 : if (empty_range_varying (r, type, lhs, op2))
5216 0 : return true;
5217 :
5218 : // Return a non-null pointer of the LHS type (passed in op2), but only
5219 : // if we cant overflow, eitherwise a no-zero offset could wrap to zero.
5220 : // See PR 111009.
5221 0 : if (!lhs.undefined_p () && !lhs.contains_zero_p () && TYPE_OVERFLOW_UNDEFINED (type))
5222 0 : r.set_nonzero (type);
5223 : else
5224 0 : r.set_varying (type);
5225 : return true;
5226 : }
5227 :
5228 : // Initialize any integral operators to the primary table
5229 :
5230 : void
5231 301808 : range_op_table::initialize_integral_ops ()
5232 : {
5233 301808 : set (TRUNC_DIV_EXPR, op_trunc_div);
5234 301808 : set (FLOOR_DIV_EXPR, op_floor_div);
5235 301808 : set (ROUND_DIV_EXPR, op_round_div);
5236 301808 : set (CEIL_DIV_EXPR, op_ceil_div);
5237 301808 : set (EXACT_DIV_EXPR, op_exact_div);
5238 301808 : set (LSHIFT_EXPR, op_lshift);
5239 301808 : set (RSHIFT_EXPR, op_rshift);
5240 301808 : set (TRUTH_AND_EXPR, op_logical_and);
5241 301808 : set (TRUTH_OR_EXPR, op_logical_or);
5242 301808 : set (TRUNC_MOD_EXPR, op_trunc_mod);
5243 301808 : set (TRUTH_NOT_EXPR, op_logical_not);
5244 301808 : set (IMAGPART_EXPR, op_unknown);
5245 301808 : set (REALPART_EXPR, op_unknown);
5246 301808 : set (ABSU_EXPR, op_absu);
5247 301808 : set (OP_WIDEN_MULT_SIGNED, op_widen_mult_signed);
5248 301808 : set (OP_WIDEN_MULT_UNSIGNED, op_widen_mult_unsigned);
5249 301808 : set (OP_WIDEN_MULT_SIGNED_UNSIGNED, op_widen_mult_signed_unsigned);
5250 301808 : set (OP_WIDEN_PLUS_SIGNED, op_widen_plus_signed);
5251 301808 : set (OP_WIDEN_PLUS_UNSIGNED, op_widen_plus_unsigned);
5252 :
5253 301808 : }
5254 :
5255 : bool
5256 68219 : operator_plus::overflow_free_p (const irange &lh, const irange &rh,
5257 : relation_trio) const
5258 : {
5259 68219 : if (lh.undefined_p () || rh.undefined_p ())
5260 : return false;
5261 :
5262 68219 : tree type = lh.type ();
5263 68219 : if (TYPE_OVERFLOW_UNDEFINED (type))
5264 : return true;
5265 :
5266 36607 : wi::overflow_type ovf;
5267 36607 : signop sgn = TYPE_SIGN (type);
5268 36607 : wide_int wmax0 = lh.upper_bound ();
5269 36607 : wide_int wmax1 = rh.upper_bound ();
5270 36607 : wi::add (wmax0, wmax1, sgn, &ovf);
5271 36607 : if (ovf != wi::OVF_NONE)
5272 : return false;
5273 :
5274 25062 : if (TYPE_UNSIGNED (type))
5275 : return true;
5276 :
5277 346 : wide_int wmin0 = lh.lower_bound ();
5278 346 : wide_int wmin1 = rh.lower_bound ();
5279 346 : wi::add (wmin0, wmin1, sgn, &ovf);
5280 346 : if (ovf != wi::OVF_NONE)
5281 237 : return false;
5282 :
5283 : return true;
5284 36953 : }
5285 :
5286 : bool
5287 2675162 : operator_minus::overflow_free_p (const irange &lh, const irange &rh,
5288 : relation_trio) const
5289 : {
5290 2675162 : if (lh.undefined_p () || rh.undefined_p ())
5291 : return false;
5292 :
5293 2675162 : tree type = lh.type ();
5294 2675162 : if (TYPE_OVERFLOW_UNDEFINED (type))
5295 : return true;
5296 :
5297 471310 : wi::overflow_type ovf;
5298 471310 : signop sgn = TYPE_SIGN (type);
5299 471310 : wide_int wmin0 = lh.lower_bound ();
5300 471310 : wide_int wmax1 = rh.upper_bound ();
5301 471310 : wi::sub (wmin0, wmax1, sgn, &ovf);
5302 471310 : if (ovf != wi::OVF_NONE)
5303 : return false;
5304 :
5305 48925 : if (TYPE_UNSIGNED (type))
5306 : return true;
5307 :
5308 28623 : wide_int wmax0 = lh.upper_bound ();
5309 28623 : wide_int wmin1 = rh.lower_bound ();
5310 28623 : wi::sub (wmax0, wmin1, sgn, &ovf);
5311 28623 : if (ovf != wi::OVF_NONE)
5312 12326 : return false;
5313 :
5314 : return true;
5315 499933 : }
5316 :
5317 : bool
5318 7576 : operator_mult::overflow_free_p (const irange &lh, const irange &rh,
5319 : relation_trio) const
5320 : {
5321 7576 : if (lh.undefined_p () || rh.undefined_p ())
5322 : return false;
5323 :
5324 7576 : tree type = lh.type ();
5325 7576 : if (TYPE_OVERFLOW_UNDEFINED (type))
5326 : return true;
5327 :
5328 6499 : wi::overflow_type ovf;
5329 6499 : signop sgn = TYPE_SIGN (type);
5330 6499 : wide_int wmax0 = lh.upper_bound ();
5331 6499 : wide_int wmax1 = rh.upper_bound ();
5332 6499 : wi::mul (wmax0, wmax1, sgn, &ovf);
5333 6499 : if (ovf != wi::OVF_NONE)
5334 : return false;
5335 :
5336 260 : if (TYPE_UNSIGNED (type))
5337 : return true;
5338 :
5339 53 : wide_int wmin0 = lh.lower_bound ();
5340 53 : wide_int wmin1 = rh.lower_bound ();
5341 53 : wi::mul (wmin0, wmin1, sgn, &ovf);
5342 53 : if (ovf != wi::OVF_NONE)
5343 : return false;
5344 :
5345 23 : wi::mul (wmin0, wmax1, sgn, &ovf);
5346 23 : if (ovf != wi::OVF_NONE)
5347 : return false;
5348 :
5349 23 : wi::mul (wmax0, wmin1, sgn, &ovf);
5350 23 : if (ovf != wi::OVF_NONE)
5351 0 : return false;
5352 :
5353 : return true;
5354 6552 : }
5355 :
5356 : #if CHECKING_P
5357 : #include "selftest.h"
5358 :
5359 : namespace selftest
5360 : {
5361 : #define INT(x) wi::shwi ((x), TYPE_PRECISION (integer_type_node))
5362 : #define UINT(x) wi::uhwi ((x), TYPE_PRECISION (unsigned_type_node))
5363 : #define INT16(x) wi::shwi ((x), TYPE_PRECISION (short_integer_type_node))
5364 : #define UINT16(x) wi::uhwi ((x), TYPE_PRECISION (short_unsigned_type_node))
5365 : #define SCHAR(x) wi::shwi ((x), TYPE_PRECISION (signed_char_type_node))
5366 : #define UCHAR(x) wi::uhwi ((x), TYPE_PRECISION (unsigned_char_type_node))
5367 :
5368 : static void
5369 4 : range_op_cast_tests ()
5370 : {
5371 4 : int_range<2> r0, r1, r2, rold;
5372 4 : r0.set_varying (integer_type_node);
5373 4 : wide_int maxint = r0.upper_bound ();
5374 :
5375 : // If a range is in any way outside of the range for the converted
5376 : // to range, default to the range for the new type.
5377 4 : r0.set_varying (short_integer_type_node);
5378 4 : wide_int minshort = r0.lower_bound ();
5379 4 : wide_int maxshort = r0.upper_bound ();
5380 4 : if (TYPE_PRECISION (integer_type_node)
5381 4 : > TYPE_PRECISION (short_integer_type_node))
5382 : {
5383 8 : r1 = int_range<1> (integer_type_node,
5384 4 : wi::zero (TYPE_PRECISION (integer_type_node)),
5385 8 : maxint);
5386 4 : range_cast (r1, short_integer_type_node);
5387 4 : ASSERT_TRUE (r1.lower_bound () == minshort
5388 : && r1.upper_bound() == maxshort);
5389 : }
5390 :
5391 : // (unsigned char)[-5,-1] => [251,255].
5392 4 : r0 = rold = int_range<1> (signed_char_type_node, SCHAR (-5), SCHAR (-1));
5393 4 : range_cast (r0, unsigned_char_type_node);
5394 4 : ASSERT_TRUE (r0 == int_range<1> (unsigned_char_type_node,
5395 : UCHAR (251), UCHAR (255)));
5396 4 : range_cast (r0, signed_char_type_node);
5397 4 : ASSERT_TRUE (r0 == rold);
5398 :
5399 : // (signed char)[15, 150] => [-128,-106][15,127].
5400 4 : r0 = rold = int_range<1> (unsigned_char_type_node, UCHAR (15), UCHAR (150));
5401 4 : range_cast (r0, signed_char_type_node);
5402 4 : r1 = int_range<1> (signed_char_type_node, SCHAR (15), SCHAR (127));
5403 4 : r2 = int_range<1> (signed_char_type_node, SCHAR (-128), SCHAR (-106));
5404 4 : r1.union_ (r2);
5405 4 : ASSERT_TRUE (r1 == r0);
5406 4 : range_cast (r0, unsigned_char_type_node);
5407 4 : ASSERT_TRUE (r0 == rold);
5408 :
5409 : // (unsigned char)[-5, 5] => [0,5][251,255].
5410 4 : r0 = rold = int_range<1> (signed_char_type_node, SCHAR (-5), SCHAR (5));
5411 4 : range_cast (r0, unsigned_char_type_node);
5412 4 : r1 = int_range<1> (unsigned_char_type_node, UCHAR (251), UCHAR (255));
5413 4 : r2 = int_range<1> (unsigned_char_type_node, UCHAR (0), UCHAR (5));
5414 4 : r1.union_ (r2);
5415 4 : ASSERT_TRUE (r0 == r1);
5416 4 : range_cast (r0, signed_char_type_node);
5417 4 : ASSERT_TRUE (r0 == rold);
5418 :
5419 : // (unsigned char)[-5,5] => [0,5][251,255].
5420 4 : r0 = int_range<1> (integer_type_node, INT (-5), INT (5));
5421 4 : range_cast (r0, unsigned_char_type_node);
5422 4 : r1 = int_range<1> (unsigned_char_type_node, UCHAR (0), UCHAR (5));
5423 4 : r1.union_ (int_range<1> (unsigned_char_type_node, UCHAR (251), UCHAR (255)));
5424 4 : ASSERT_TRUE (r0 == r1);
5425 :
5426 : // (unsigned char)[5U,1974U] => [0,255].
5427 4 : r0 = int_range<1> (unsigned_type_node, UINT (5), UINT (1974));
5428 4 : range_cast (r0, unsigned_char_type_node);
5429 4 : ASSERT_TRUE (r0 == int_range<1> (unsigned_char_type_node, UCHAR (0), UCHAR (255)));
5430 4 : range_cast (r0, integer_type_node);
5431 : // Going to a wider range should not sign extend.
5432 4 : ASSERT_TRUE (r0 == int_range<1> (integer_type_node, INT (0), INT (255)));
5433 :
5434 : // (unsigned char)[-350,15] => [0,255].
5435 4 : r0 = int_range<1> (integer_type_node, INT (-350), INT (15));
5436 4 : range_cast (r0, unsigned_char_type_node);
5437 4 : ASSERT_TRUE (r0 == (int_range<1>
5438 : (unsigned_char_type_node,
5439 : min_limit (unsigned_char_type_node),
5440 : max_limit (unsigned_char_type_node))));
5441 :
5442 : // Casting [-120,20] from signed char to unsigned short.
5443 : // => [0, 20][0xff88, 0xffff].
5444 4 : r0 = int_range<1> (signed_char_type_node, SCHAR (-120), SCHAR (20));
5445 4 : range_cast (r0, short_unsigned_type_node);
5446 4 : r1 = int_range<1> (short_unsigned_type_node, UINT16 (0), UINT16 (20));
5447 8 : r2 = int_range<1> (short_unsigned_type_node,
5448 12 : UINT16 (0xff88), UINT16 (0xffff));
5449 4 : r1.union_ (r2);
5450 4 : ASSERT_TRUE (r0 == r1);
5451 : // A truncating cast back to signed char will work because [-120, 20]
5452 : // is representable in signed char.
5453 4 : range_cast (r0, signed_char_type_node);
5454 4 : ASSERT_TRUE (r0 == int_range<1> (signed_char_type_node,
5455 : SCHAR (-120), SCHAR (20)));
5456 :
5457 : // unsigned char -> signed short
5458 : // (signed short)[(unsigned char)25, (unsigned char)250]
5459 : // => [(signed short)25, (signed short)250]
5460 4 : r0 = rold = int_range<1> (unsigned_char_type_node, UCHAR (25), UCHAR (250));
5461 4 : range_cast (r0, short_integer_type_node);
5462 4 : r1 = int_range<1> (short_integer_type_node, INT16 (25), INT16 (250));
5463 4 : ASSERT_TRUE (r0 == r1);
5464 4 : range_cast (r0, unsigned_char_type_node);
5465 4 : ASSERT_TRUE (r0 == rold);
5466 :
5467 : // Test casting a wider signed [-MIN,MAX] to a narrower unsigned.
5468 8 : r0 = int_range<1> (long_long_integer_type_node,
5469 4 : min_limit (long_long_integer_type_node),
5470 8 : max_limit (long_long_integer_type_node));
5471 4 : range_cast (r0, short_unsigned_type_node);
5472 8 : r1 = int_range<1> (short_unsigned_type_node,
5473 4 : min_limit (short_unsigned_type_node),
5474 8 : max_limit (short_unsigned_type_node));
5475 4 : ASSERT_TRUE (r0 == r1);
5476 :
5477 : // Casting NONZERO to a narrower type will wrap/overflow so
5478 : // it's just the entire range for the narrower type.
5479 : //
5480 : // "NOT 0 at signed 32-bits" ==> [-MIN_32,-1][1, +MAX_32]. This is
5481 : // is outside of the range of a smaller range, return the full
5482 : // smaller range.
5483 4 : if (TYPE_PRECISION (integer_type_node)
5484 4 : > TYPE_PRECISION (short_integer_type_node))
5485 : {
5486 4 : r0.set_nonzero (integer_type_node);
5487 4 : range_cast (r0, short_integer_type_node);
5488 8 : r1 = int_range<1> (short_integer_type_node,
5489 4 : min_limit (short_integer_type_node),
5490 8 : max_limit (short_integer_type_node));
5491 4 : ASSERT_TRUE (r0 == r1);
5492 : }
5493 :
5494 : // Casting NONZERO from a narrower signed to a wider signed.
5495 : //
5496 : // NONZERO signed 16-bits is [-MIN_16,-1][1, +MAX_16].
5497 : // Converting this to 32-bits signed is [-MIN_16,-1][1, +MAX_16].
5498 4 : r0.set_nonzero (short_integer_type_node);
5499 4 : range_cast (r0, integer_type_node);
5500 4 : r1 = int_range<1> (integer_type_node, INT (-32768), INT (-1));
5501 4 : r2 = int_range<1> (integer_type_node, INT (1), INT (32767));
5502 4 : r1.union_ (r2);
5503 4 : ASSERT_TRUE (r0 == r1);
5504 4 : }
5505 :
5506 : static void
5507 4 : range_op_lshift_tests ()
5508 : {
5509 : // Test that 0x808.... & 0x8.... still contains 0x8....
5510 : // for a large set of numbers.
5511 4 : {
5512 4 : int_range_max res;
5513 4 : tree big_type = long_long_unsigned_type_node;
5514 4 : unsigned big_prec = TYPE_PRECISION (big_type);
5515 : // big_num = 0x808,0000,0000,0000
5516 4 : wide_int big_num = wi::lshift (wi::uhwi (0x808, big_prec),
5517 8 : wi::uhwi (48, big_prec));
5518 8 : op_bitwise_and.fold_range (res, big_type,
5519 8 : int_range <1> (big_type),
5520 8 : int_range <1> (big_type, big_num, big_num));
5521 : // val = 0x8,0000,0000,0000
5522 4 : wide_int val = wi::lshift (wi::uhwi (8, big_prec),
5523 8 : wi::uhwi (48, big_prec));
5524 4 : ASSERT_TRUE (res.contains_p (val));
5525 4 : }
5526 :
5527 4 : if (TYPE_PRECISION (unsigned_type_node) > 31)
5528 : {
5529 : // unsigned VARYING = op1 << 1 should be VARYING.
5530 4 : int_range<2> lhs (unsigned_type_node);
5531 4 : int_range<2> shift (unsigned_type_node, INT (1), INT (1));
5532 4 : int_range_max op1;
5533 4 : op_lshift.op1_range (op1, unsigned_type_node, lhs, shift);
5534 4 : ASSERT_TRUE (op1.varying_p ());
5535 :
5536 : // 0 = op1 << 1 should be [0,0], [0x8000000, 0x8000000].
5537 4 : int_range<2> zero (unsigned_type_node, UINT (0), UINT (0));
5538 4 : op_lshift.op1_range (op1, unsigned_type_node, zero, shift);
5539 4 : ASSERT_TRUE (op1.num_pairs () == 2);
5540 : // Remove the [0,0] range.
5541 4 : op1.intersect (zero);
5542 4 : ASSERT_TRUE (op1.num_pairs () == 1);
5543 : // op1 << 1 should be [0x8000,0x8000] << 1,
5544 : // which should result in [0,0].
5545 4 : int_range_max result;
5546 4 : op_lshift.fold_range (result, unsigned_type_node, op1, shift);
5547 4 : ASSERT_TRUE (result == zero);
5548 4 : }
5549 : // signed VARYING = op1 << 1 should be VARYING.
5550 4 : if (TYPE_PRECISION (integer_type_node) > 31)
5551 : {
5552 : // unsigned VARYING = op1 << 1 should be VARYING.
5553 4 : int_range<2> lhs (integer_type_node);
5554 4 : int_range<2> shift (integer_type_node, INT (1), INT (1));
5555 4 : int_range_max op1;
5556 4 : op_lshift.op1_range (op1, integer_type_node, lhs, shift);
5557 4 : ASSERT_TRUE (op1.varying_p ());
5558 :
5559 : // 0 = op1 << 1 should be [0,0], [0x8000000, 0x8000000].
5560 4 : int_range<2> zero (integer_type_node, INT (0), INT (0));
5561 4 : op_lshift.op1_range (op1, integer_type_node, zero, shift);
5562 4 : ASSERT_TRUE (op1.num_pairs () == 2);
5563 : // Remove the [0,0] range.
5564 4 : op1.intersect (zero);
5565 4 : ASSERT_TRUE (op1.num_pairs () == 1);
5566 : // op1 << 1 should be [0x8000,0x8000] << 1,
5567 : // which should result in [0,0].
5568 4 : int_range_max result;
5569 4 : op_lshift.fold_range (result, unsigned_type_node, op1, shift);
5570 4 : ASSERT_TRUE (result == zero);
5571 4 : }
5572 4 : }
5573 :
5574 : static void
5575 4 : range_op_rshift_tests ()
5576 : {
5577 : // unsigned: [3, MAX] = OP1 >> 1
5578 4 : {
5579 4 : int_range_max lhs (unsigned_type_node,
5580 4 : UINT (3), max_limit (unsigned_type_node));
5581 4 : int_range_max one (unsigned_type_node,
5582 8 : wi::one (TYPE_PRECISION (unsigned_type_node)),
5583 8 : wi::one (TYPE_PRECISION (unsigned_type_node)));
5584 4 : int_range_max op1;
5585 4 : op_rshift.op1_range (op1, unsigned_type_node, lhs, one);
5586 4 : ASSERT_FALSE (op1.contains_p (UINT (3)));
5587 4 : }
5588 :
5589 : // signed: [3, MAX] = OP1 >> 1
5590 4 : {
5591 4 : int_range_max lhs (integer_type_node,
5592 4 : INT (3), max_limit (integer_type_node));
5593 4 : int_range_max one (integer_type_node, INT (1), INT (1));
5594 4 : int_range_max op1;
5595 4 : op_rshift.op1_range (op1, integer_type_node, lhs, one);
5596 4 : ASSERT_FALSE (op1.contains_p (INT (-2)));
5597 4 : }
5598 :
5599 : // This is impossible, so OP1 should be [].
5600 : // signed: [MIN, MIN] = OP1 >> 1
5601 4 : {
5602 4 : int_range_max lhs (integer_type_node,
5603 4 : min_limit (integer_type_node),
5604 4 : min_limit (integer_type_node));
5605 4 : int_range_max one (integer_type_node, INT (1), INT (1));
5606 4 : int_range_max op1;
5607 4 : op_rshift.op1_range (op1, integer_type_node, lhs, one);
5608 4 : ASSERT_TRUE (op1.undefined_p ());
5609 4 : }
5610 :
5611 : // signed: ~[-1] = OP1 >> 31
5612 4 : if (TYPE_PRECISION (integer_type_node) > 31)
5613 : {
5614 4 : int_range_max lhs (integer_type_node, INT (-1), INT (-1), VR_ANTI_RANGE);
5615 4 : int_range_max shift (integer_type_node, INT (31), INT (31));
5616 4 : int_range_max op1;
5617 4 : op_rshift.op1_range (op1, integer_type_node, lhs, shift);
5618 4 : int_range_max negatives = range_negatives (integer_type_node);
5619 4 : negatives.intersect (op1);
5620 4 : ASSERT_TRUE (negatives.undefined_p ());
5621 4 : }
5622 4 : }
5623 :
5624 : static void
5625 4 : range_op_bitwise_and_tests ()
5626 : {
5627 4 : int_range_max res;
5628 4 : wide_int min = min_limit (integer_type_node);
5629 4 : wide_int max = max_limit (integer_type_node);
5630 4 : wide_int tiny = wi::add (min, wi::one (TYPE_PRECISION (integer_type_node)));
5631 4 : int_range_max i1 (integer_type_node, tiny, max);
5632 4 : int_range_max i2 (integer_type_node, INT (255), INT (255));
5633 :
5634 : // [MIN+1, MAX] = OP1 & 255: OP1 is VARYING
5635 4 : op_bitwise_and.op1_range (res, integer_type_node, i1, i2);
5636 4 : ASSERT_TRUE (res == int_range<1> (integer_type_node));
5637 :
5638 : // VARYING = OP1 & 255: OP1 is VARYING
5639 4 : i1 = int_range<1> (integer_type_node);
5640 4 : op_bitwise_and.op1_range (res, integer_type_node, i1, i2);
5641 4 : ASSERT_TRUE (res == int_range<1> (integer_type_node));
5642 :
5643 : // For 0 = x & MASK, x is ~MASK.
5644 4 : {
5645 4 : int_range<2> zero (integer_type_node, INT (0), INT (0));
5646 4 : int_range<2> mask = int_range<2> (integer_type_node, INT (7), INT (7));
5647 4 : op_bitwise_and.op1_range (res, integer_type_node, zero, mask);
5648 4 : wide_int inv = wi::shwi (~7U, TYPE_PRECISION (integer_type_node));
5649 4 : ASSERT_TRUE (res.get_nonzero_bits () == inv);
5650 4 : }
5651 :
5652 : // (NONZERO | X) is nonzero.
5653 4 : i1.set_nonzero (integer_type_node);
5654 4 : i2.set_varying (integer_type_node);
5655 4 : op_bitwise_or.fold_range (res, integer_type_node, i1, i2);
5656 4 : ASSERT_FALSE (res.contains_zero_p ());
5657 :
5658 : // (NEGATIVE | X) is nonzero.
5659 4 : i1 = int_range<1> (integer_type_node, INT (-5), INT (-3));
5660 4 : i2.set_varying (integer_type_node);
5661 4 : op_bitwise_or.fold_range (res, integer_type_node, i1, i2);
5662 4 : ASSERT_FALSE (res.contains_p (INT (0)));
5663 4 : }
5664 :
5665 : static void
5666 4 : range_relational_tests ()
5667 : {
5668 4 : int_range<2> lhs (unsigned_char_type_node);
5669 4 : int_range<2> op1 (unsigned_char_type_node, UCHAR (8), UCHAR (10));
5670 4 : int_range<2> op2 (unsigned_char_type_node, UCHAR (20), UCHAR (20));
5671 :
5672 : // Never wrapping additions mean LHS > OP1.
5673 4 : relation_kind code = op_plus.lhs_op1_relation (lhs, op1, op2, VREL_VARYING);
5674 4 : ASSERT_TRUE (code == VREL_GT);
5675 :
5676 : // Most wrapping additions mean nothing...
5677 4 : op1 = int_range<2> (unsigned_char_type_node, UCHAR (8), UCHAR (10));
5678 4 : op2 = int_range<2> (unsigned_char_type_node, UCHAR (0), UCHAR (255));
5679 4 : code = op_plus.lhs_op1_relation (lhs, op1, op2, VREL_VARYING);
5680 4 : ASSERT_TRUE (code == VREL_VARYING);
5681 :
5682 : // However, always wrapping additions mean LHS < OP1.
5683 4 : op1 = int_range<2> (unsigned_char_type_node, UCHAR (1), UCHAR (255));
5684 4 : op2 = int_range<2> (unsigned_char_type_node, UCHAR (255), UCHAR (255));
5685 4 : code = op_plus.lhs_op1_relation (lhs, op1, op2, VREL_VARYING);
5686 4 : ASSERT_TRUE (code == VREL_LT);
5687 4 : }
5688 :
5689 : void
5690 4 : range_op_tests ()
5691 : {
5692 4 : range_op_rshift_tests ();
5693 4 : range_op_lshift_tests ();
5694 4 : range_op_bitwise_and_tests ();
5695 4 : range_op_cast_tests ();
5696 4 : range_relational_tests ();
5697 :
5698 4 : extern void range_op_float_tests ();
5699 4 : range_op_float_tests ();
5700 4 : }
5701 :
5702 : } // namespace selftest
5703 :
5704 : #endif // CHECKING_P
|