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