Branch data Line data Source code
1 : : /* Code for range operators.
2 : : Copyright (C) 2017-2026 Free Software Foundation, Inc.
3 : : Contributed by Andrew MacLeod <amacleod@redhat.com>
4 : : and Aldy Hernandez <aldyh@redhat.com>.
5 : :
6 : : This file is part of GCC.
7 : :
8 : : GCC is free software; you can redistribute it and/or modify
9 : : it under the terms of the GNU General Public License as published by
10 : : the Free Software Foundation; either version 3, or (at your option)
11 : : any later version.
12 : :
13 : : GCC is distributed in the hope that it will be useful,
14 : : but WITHOUT ANY WARRANTY; without even the implied warranty of
15 : : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 : : GNU General Public License for more details.
17 : :
18 : : You should have received a copy of the GNU General Public License
19 : : along with GCC; see the file COPYING3. If not see
20 : : <http://www.gnu.org/licenses/>. */
21 : :
22 : : #include "config.h"
23 : : #include "system.h"
24 : : #include "coretypes.h"
25 : : #include "backend.h"
26 : : #include "insn-codes.h"
27 : : #include "rtl.h"
28 : : #include "tree.h"
29 : : #include "gimple.h"
30 : : #include "cfghooks.h"
31 : : #include "tree-pass.h"
32 : : #include "ssa.h"
33 : : #include "optabs-tree.h"
34 : : #include "gimple-pretty-print.h"
35 : : #include "diagnostic-core.h"
36 : : #include "flags.h"
37 : : #include "fold-const.h"
38 : : #include "stor-layout.h"
39 : : #include "calls.h"
40 : : #include "cfganal.h"
41 : : #include "gimple-iterator.h"
42 : : #include "gimple-fold.h"
43 : : #include "tree-eh.h"
44 : : #include "gimple-walk.h"
45 : : #include "tree-cfg.h"
46 : : #include "wide-int.h"
47 : : #include "value-relation.h"
48 : : #include "range-op.h"
49 : : #include "tree-ssa-ccp.h"
50 : : #include "range-op-mixed.h"
51 : :
52 : : // Instantiate the operators which apply to multiple types here.
53 : :
54 : : 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_view op_view;
64 : : operator_plus op_plus;
65 : : operator_abs op_abs;
66 : : operator_minus op_minus;
67 : : operator_negate op_negate;
68 : : operator_mult op_mult;
69 : : operator_addr_expr op_addr;
70 : : operator_bitwise_not op_bitwise_not;
71 : : operator_bitwise_xor op_bitwise_xor;
72 : : operator_bitwise_and op_bitwise_and;
73 : : operator_bitwise_or op_bitwise_or;
74 : : operator_min op_min;
75 : : operator_max op_max;
76 : :
77 : : // Instantaite a range operator table.
78 : : range_op_table operator_table;
79 : :
80 : : // Invoke the initialization routines for each class of range.
81 : :
82 : 285306 : range_op_table::range_op_table ()
83 : : {
84 : 285306 : initialize_integral_ops ();
85 : 285306 : initialize_pointer_ops ();
86 : 285306 : initialize_float_ops ();
87 : :
88 : 285306 : set (EQ_EXPR, op_equal);
89 : 285306 : set (NE_EXPR, op_not_equal);
90 : 285306 : set (LT_EXPR, op_lt);
91 : 285306 : set (LE_EXPR, op_le);
92 : 285306 : set (GT_EXPR, op_gt);
93 : 285306 : set (GE_EXPR, op_ge);
94 : 285306 : set (SSA_NAME, op_ident);
95 : 285306 : set (PAREN_EXPR, op_ident);
96 : 285306 : set (OBJ_TYPE_REF, op_ident);
97 : 285306 : set (REAL_CST, op_cst);
98 : 285306 : set (INTEGER_CST, op_cst);
99 : 285306 : set (NOP_EXPR, op_cast);
100 : 285306 : set (CONVERT_EXPR, op_cast);
101 : 285306 : set (VIEW_CONVERT_EXPR, op_view);
102 : 285306 : set (FLOAT_EXPR, op_cast);
103 : 285306 : set (FIX_TRUNC_EXPR, op_cast);
104 : 285306 : set (PLUS_EXPR, op_plus);
105 : 285306 : set (ABS_EXPR, op_abs);
106 : 285306 : set (MINUS_EXPR, op_minus);
107 : 285306 : set (NEGATE_EXPR, op_negate);
108 : 285306 : set (MULT_EXPR, op_mult);
109 : 285306 : set (ADDR_EXPR, op_addr);
110 : 285306 : set (BIT_NOT_EXPR, op_bitwise_not);
111 : 285306 : set (BIT_XOR_EXPR, op_bitwise_xor);
112 : 285306 : set (BIT_AND_EXPR, op_bitwise_and);
113 : 285306 : set (BIT_IOR_EXPR, op_bitwise_or);
114 : 285306 : set (MIN_EXPR, op_min);
115 : 285306 : set (MAX_EXPR, op_max);
116 : 285306 : }
117 : :
118 : : // Instantiate a default range operator for opcodes with no entry.
119 : :
120 : : range_operator default_operator;
121 : :
122 : : // Create a default range_op_handler.
123 : :
124 : 1688425643 : range_op_handler::range_op_handler ()
125 : : {
126 : 1688425643 : m_operator = &default_operator;
127 : 1688425643 : }
128 : :
129 : : // Create a range_op_handler for CODE. Use a default operatoer if CODE
130 : : // does not have an entry.
131 : :
132 : 4088037885 : range_op_handler::range_op_handler (unsigned code)
133 : : {
134 : 4088037885 : m_operator = operator_table[code];
135 : 4088037885 : if (!m_operator)
136 : 808342634 : m_operator = &default_operator;
137 : 4088037885 : }
138 : :
139 : : // Return TRUE if this handler has a non-default operator.
140 : :
141 : 5917355681 : range_op_handler::operator bool () const
142 : : {
143 : 5917355681 : return m_operator != &default_operator;
144 : : }
145 : :
146 : : // Return a pointer to the range operator assocaited with this handler.
147 : : // If it is a default operator, return NULL.
148 : : // This is the equivalent of indexing the range table.
149 : :
150 : : range_operator *
151 : 988968666 : range_op_handler::range_op () const
152 : : {
153 : 988968666 : if (m_operator != &default_operator)
154 : 988968666 : return m_operator;
155 : : return NULL;
156 : : }
157 : :
158 : : // Create a dispatch pattern for value range discriminators LHS, OP1, and OP2.
159 : : // This is used to produce a unique value for each dispatch pattern. Shift
160 : : // values are based on the size of the m_discriminator field in value_range.h.
161 : :
162 : : constexpr unsigned
163 : 664849493 : dispatch_trio (unsigned lhs, unsigned op1, unsigned op2)
164 : : {
165 : 664849493 : return ((lhs << 8) + (op1 << 4) + (op2));
166 : : }
167 : :
168 : : // These are the supported dispatch patterns. These map to the parameter list
169 : : // of the routines in range_operator. Note the last 3 characters are
170 : : // shorthand for the LHS, OP1, and OP2 range discriminator class.
171 : : // Reminder, single operand instructions use the LHS type for op2, even if
172 : : // unused. So FLOAT = INT would be RO_FIF.
173 : :
174 : : const unsigned RO_III = dispatch_trio (VR_IRANGE, VR_IRANGE, VR_IRANGE);
175 : : const unsigned RO_IFI = dispatch_trio (VR_IRANGE, VR_FRANGE, VR_IRANGE);
176 : : const unsigned RO_IFF = dispatch_trio (VR_IRANGE, VR_FRANGE, VR_FRANGE);
177 : : const unsigned RO_FFF = dispatch_trio (VR_FRANGE, VR_FRANGE, VR_FRANGE);
178 : : const unsigned RO_FIF = dispatch_trio (VR_FRANGE, VR_IRANGE, VR_FRANGE);
179 : : const unsigned RO_FII = dispatch_trio (VR_FRANGE, VR_IRANGE, VR_IRANGE);
180 : : const unsigned RO_PPP = dispatch_trio (VR_PRANGE, VR_PRANGE, VR_PRANGE);
181 : : const unsigned RO_PPI = dispatch_trio (VR_PRANGE, VR_PRANGE, VR_IRANGE);
182 : : const unsigned RO_IPP = dispatch_trio (VR_IRANGE, VR_PRANGE, VR_PRANGE);
183 : : const unsigned RO_IPI = dispatch_trio (VR_IRANGE, VR_PRANGE, VR_IRANGE);
184 : : const unsigned RO_PIP = dispatch_trio (VR_PRANGE, VR_IRANGE, VR_PRANGE);
185 : : const unsigned RO_PII = dispatch_trio (VR_PRANGE, VR_IRANGE, VR_IRANGE);
186 : :
187 : : // Return a dispatch value for parameter types LHS, OP1 and OP2.
188 : :
189 : : unsigned
190 : 664849493 : range_op_handler::dispatch_kind (const vrange &lhs, const vrange &op1,
191 : : const vrange& op2) const
192 : : {
193 : 664849493 : return dispatch_trio (lhs.m_discriminator, op1.m_discriminator,
194 : 664849493 : op2.m_discriminator);
195 : : }
196 : :
197 : : void
198 : 0 : range_op_handler::discriminator_fail (const vrange &r1,
199 : : const vrange &r2,
200 : : const vrange &r3) const
201 : : {
202 : 0 : const char name[] = "IPF";
203 : 0 : gcc_checking_assert (r1.m_discriminator < sizeof (name) - 1);
204 : 0 : gcc_checking_assert (r2.m_discriminator < sizeof (name) - 1);
205 : 0 : gcc_checking_assert (r3.m_discriminator < sizeof (name) - 1);
206 : 0 : fprintf (stderr,
207 : : "Unsupported operand combination in dispatch: RO_%c%c%c\n",
208 : 0 : name[r1.m_discriminator],
209 : 0 : name[r2.m_discriminator],
210 : 0 : name[r3.m_discriminator]);
211 : 0 : gcc_unreachable ();
212 : : }
213 : :
214 : : static inline bool
215 : : has_pointer_operand_p (const vrange &r1, const vrange &r2, const vrange &r3)
216 : : {
217 : : return is_a <prange> (r1) || is_a <prange> (r2) || is_a <prange> (r3);
218 : : }
219 : :
220 : : // Dispatch a call to fold_range based on the types of R, LH and RH.
221 : :
222 : : bool
223 : 295607177 : range_op_handler::fold_range (vrange &r, tree type,
224 : : const vrange &lh,
225 : : const vrange &rh,
226 : : relation_trio rel) const
227 : : {
228 : 295607177 : gcc_checking_assert (m_operator);
229 : : #if CHECKING_P
230 : 295607177 : if (!lh.undefined_p () && !rh.undefined_p ())
231 : 290035218 : gcc_assert (m_operator->operand_check_p (type, lh.type (), rh.type ()));
232 : : #endif
233 : 295607177 : switch (dispatch_kind (r, lh, rh))
234 : : {
235 : 224500096 : case RO_III:
236 : 224500096 : return m_operator->fold_range (as_a <irange> (r), type,
237 : : as_a <irange> (lh),
238 : 224500096 : as_a <irange> (rh), rel);
239 : 354671 : case RO_IFI:
240 : 354671 : return m_operator->fold_range (as_a <irange> (r), type,
241 : : as_a <frange> (lh),
242 : 354671 : as_a <irange> (rh), rel);
243 : 2629702 : case RO_IFF:
244 : 2629702 : return m_operator->fold_range (as_a <irange> (r), type,
245 : : as_a <frange> (lh),
246 : 2629702 : as_a <frange> (rh), rel);
247 : 7567464 : case RO_FFF:
248 : 7567464 : return m_operator->fold_range (as_a <frange> (r), type,
249 : : as_a <frange> (lh),
250 : 7567464 : as_a <frange> (rh), rel);
251 : 0 : case RO_FII:
252 : 0 : return m_operator->fold_range (as_a <frange> (r), type,
253 : : as_a <irange> (lh),
254 : 0 : as_a <irange> (rh), rel);
255 : 912874 : case RO_FIF:
256 : 912874 : return m_operator->fold_range (as_a <frange> (r), type,
257 : : as_a <irange> (lh),
258 : 912874 : as_a <frange> (rh), rel);
259 : 18380898 : case RO_PPP:
260 : 18380898 : return m_operator->fold_range (as_a <prange> (r), type,
261 : : as_a <prange> (lh),
262 : 18380898 : as_a <prange> (rh), rel);
263 : 9693038 : case RO_PPI:
264 : 9693038 : return m_operator->fold_range (as_a <prange> (r), type,
265 : : as_a <prange> (lh),
266 : 9693038 : as_a <irange> (rh), rel);
267 : 18656932 : case RO_IPP:
268 : 18656932 : return m_operator->fold_range (as_a <irange> (r), type,
269 : : as_a <prange> (lh),
270 : 18656932 : as_a <prange> (rh), rel);
271 : 1959414 : case RO_PIP:
272 : 1959414 : return m_operator->fold_range (as_a <prange> (r), type,
273 : : as_a <irange> (lh),
274 : 1959414 : as_a <prange> (rh), rel);
275 : 10952056 : case RO_IPI:
276 : 10952056 : return m_operator->fold_range (as_a <irange> (r), type,
277 : : as_a <prange> (lh),
278 : 10952056 : as_a <irange> (rh), rel);
279 : : default:
280 : : return false;
281 : : }
282 : : }
283 : :
284 : : // Dispatch a call to op1_range based on the types of R, LHS and OP2.
285 : :
286 : : bool
287 : 93814604 : range_op_handler::op1_range (vrange &r, tree type,
288 : : const vrange &lhs,
289 : : const vrange &op2,
290 : : relation_trio rel) const
291 : : {
292 : 93814604 : gcc_checking_assert (m_operator);
293 : 93814604 : if (lhs.undefined_p ())
294 : : return false;
295 : : #if CHECKING_P
296 : 93813371 : if (!op2.undefined_p ())
297 : 93813255 : gcc_assert (m_operator->operand_check_p (lhs.type (), type, op2.type ()));
298 : : #endif
299 : 93813371 : switch (dispatch_kind (r, lhs, op2))
300 : : {
301 : 80791888 : case RO_III:
302 : 80791888 : return m_operator->op1_range (as_a <irange> (r), type,
303 : : as_a <irange> (lhs),
304 : 80791888 : as_a <irange> (op2), rel);
305 : 245316 : case RO_IFI:
306 : 245316 : return m_operator->op1_range (as_a <irange> (r), type,
307 : : as_a <frange> (lhs),
308 : 245316 : as_a <irange> (op2), rel);
309 : 869539 : case RO_PPP:
310 : 869539 : return m_operator->op1_range (as_a <prange> (r), type,
311 : : as_a <prange> (lhs),
312 : 869539 : as_a <prange> (op2), rel);
313 : 8767342 : case RO_PIP:
314 : 8767342 : return m_operator->op1_range (as_a <prange> (r), type,
315 : : as_a <irange> (lhs),
316 : 8767342 : as_a <prange> (op2), rel);
317 : 488216 : case RO_PPI:
318 : 488216 : return m_operator->op1_range (as_a <prange> (r), type,
319 : : as_a <prange> (lhs),
320 : 488216 : as_a <irange> (op2), rel);
321 : 251592 : case RO_IPI:
322 : 251592 : return m_operator->op1_range (as_a <irange> (r), type,
323 : : as_a <prange> (lhs),
324 : 251592 : as_a <irange> (op2), rel);
325 : 1486937 : case RO_FIF:
326 : 1486937 : return m_operator->op1_range (as_a <frange> (r), type,
327 : : as_a <irange> (lhs),
328 : 1486937 : as_a <frange> (op2), rel);
329 : 912541 : case RO_FFF:
330 : 912541 : return m_operator->op1_range (as_a <frange> (r), type,
331 : : as_a <frange> (lhs),
332 : 912541 : as_a <frange> (op2), rel);
333 : : default:
334 : : return false;
335 : : }
336 : : }
337 : :
338 : : // Dispatch a call to op2_range based on the types of R, LHS and OP1.
339 : :
340 : : bool
341 : 27245599 : range_op_handler::op2_range (vrange &r, tree type,
342 : : const vrange &lhs,
343 : : const vrange &op1,
344 : : relation_trio rel) const
345 : : {
346 : 27245599 : gcc_checking_assert (m_operator);
347 : 27245599 : if (lhs.undefined_p ())
348 : : return false;
349 : : #if CHECKING_P
350 : 27245579 : if (!op1.undefined_p ())
351 : 27245461 : gcc_assert (m_operator->operand_check_p (lhs.type (), op1.type (), type));
352 : : #endif
353 : 27245579 : switch (dispatch_kind (r, lhs, op1))
354 : : {
355 : 20965833 : case RO_III:
356 : 20965833 : return m_operator->op2_range (as_a <irange> (r), type,
357 : : as_a <irange> (lhs),
358 : 20965833 : as_a <irange> (op1), rel);
359 : 5129814 : case RO_PIP:
360 : 5129814 : return m_operator->op2_range (as_a <prange> (r), type,
361 : : as_a <irange> (lhs),
362 : 5129814 : as_a <prange> (op1), rel);
363 : 215550 : case RO_IPP:
364 : 215550 : return m_operator->op2_range (as_a <irange> (r), type,
365 : : as_a <prange> (lhs),
366 : 215550 : as_a <prange> (op1), rel);
367 : 560860 : case RO_FIF:
368 : 560860 : return m_operator->op2_range (as_a <frange> (r), type,
369 : : as_a <irange> (lhs),
370 : 560860 : as_a <frange> (op1), rel);
371 : 373378 : case RO_FFF:
372 : 373378 : return m_operator->op2_range (as_a <frange> (r), type,
373 : : as_a <frange> (lhs),
374 : 373378 : as_a <frange> (op1), rel);
375 : : default:
376 : : return false;
377 : : }
378 : : }
379 : :
380 : : // Dispatch a call to lhs_op1_relation based on the types of LHS, OP1 and OP2.
381 : :
382 : : relation_kind
383 : 126993381 : range_op_handler::lhs_op1_relation (const vrange &lhs,
384 : : const vrange &op1,
385 : : const vrange &op2,
386 : : relation_kind rel) const
387 : : {
388 : 126993381 : gcc_checking_assert (m_operator);
389 : 126993381 : switch (dispatch_kind (lhs, op1, op2))
390 : : {
391 : 101609085 : case RO_III:
392 : 101609085 : return m_operator->lhs_op1_relation (as_a <irange> (lhs),
393 : : as_a <irange> (op1),
394 : 101609085 : as_a <irange> (op2), rel);
395 : 1466032 : case RO_PPP:
396 : 1466032 : return m_operator->lhs_op1_relation (as_a <prange> (lhs),
397 : : as_a <prange> (op1),
398 : 1466032 : as_a <prange> (op2), rel);
399 : 6285421 : case RO_IPP:
400 : 6285421 : return m_operator->lhs_op1_relation (as_a <irange> (lhs),
401 : : as_a <prange> (op1),
402 : 6285421 : as_a <prange> (op2), rel);
403 : 1466339 : case RO_PII:
404 : 1466339 : return m_operator->lhs_op1_relation (as_a <prange> (lhs),
405 : : as_a <irange> (op1),
406 : 1466339 : as_a <irange> (op2), rel);
407 : 8435580 : case RO_PPI:
408 : 8435580 : return m_operator->lhs_op1_relation (as_a <prange> (lhs),
409 : : as_a <prange> (op1),
410 : 8435580 : as_a <irange> (op2), rel);
411 : 1031873 : case RO_IFF:
412 : 1031873 : return m_operator->lhs_op1_relation (as_a <irange> (lhs),
413 : : as_a <frange> (op1),
414 : 1031873 : as_a <frange> (op2), rel);
415 : 5786718 : case RO_FFF:
416 : 5786718 : return m_operator->lhs_op1_relation (as_a <frange> (lhs),
417 : : as_a <frange> (op1),
418 : 5786718 : as_a <frange> (op2), rel);
419 : : default:
420 : : return VREL_VARYING;
421 : : }
422 : : }
423 : :
424 : : // Dispatch a call to lhs_op2_relation based on the types of LHS, OP1 and OP2.
425 : :
426 : : relation_kind
427 : 37407967 : range_op_handler::lhs_op2_relation (const vrange &lhs,
428 : : const vrange &op1,
429 : : const vrange &op2,
430 : : relation_kind rel) const
431 : : {
432 : 37407967 : gcc_checking_assert (m_operator);
433 : 37407967 : switch (dispatch_kind (lhs, op1, op2))
434 : : {
435 : 25657552 : case RO_III:
436 : 25657552 : return m_operator->lhs_op2_relation (as_a <irange> (lhs),
437 : : as_a <irange> (op1),
438 : 25657552 : as_a <irange> (op2), rel);
439 : 169781 : case RO_IFF:
440 : 169781 : return m_operator->lhs_op2_relation (as_a <irange> (lhs),
441 : : as_a <frange> (op1),
442 : 169781 : as_a <frange> (op2), rel);
443 : 3633877 : case RO_FFF:
444 : 3633877 : return m_operator->lhs_op2_relation (as_a <frange> (lhs),
445 : : as_a <frange> (op1),
446 : 3633877 : as_a <frange> (op2), rel);
447 : : default:
448 : : return VREL_VARYING;
449 : : }
450 : : }
451 : :
452 : : // Dispatch a call to op1_op2_relation based on the type of LHS.
453 : :
454 : : relation_kind
455 : 83737637 : range_op_handler::op1_op2_relation (const vrange &lhs,
456 : : const vrange &op1,
457 : : const vrange &op2) const
458 : : {
459 : 83737637 : gcc_checking_assert (m_operator);
460 : :
461 : 83737637 : switch (dispatch_kind (lhs, op1, op2))
462 : : {
463 : 64711254 : case RO_III:
464 : 64711254 : return m_operator->op1_op2_relation (as_a <irange> (lhs),
465 : : as_a <irange> (op1),
466 : 64711254 : as_a <irange> (op2));
467 : :
468 : 16156024 : case RO_IPP:
469 : 16156024 : return m_operator->op1_op2_relation (as_a <irange> (lhs),
470 : : as_a <prange> (op1),
471 : 16156024 : as_a <prange> (op2));
472 : :
473 : 1884914 : case RO_IFF:
474 : 1884914 : return m_operator->op1_op2_relation (as_a <irange> (lhs),
475 : : as_a <frange> (op1),
476 : 1884914 : as_a <frange> (op2));
477 : :
478 : 657112 : case RO_FFF:
479 : 657112 : return m_operator->op1_op2_relation (as_a <frange> (lhs),
480 : : as_a <frange> (op1),
481 : 657112 : as_a <frange> (op2));
482 : :
483 : : default:
484 : : return VREL_VARYING;
485 : : }
486 : : }
487 : :
488 : : bool
489 : 44381 : range_op_handler::overflow_free_p (const vrange &lh,
490 : : const vrange &rh,
491 : : relation_trio rel) const
492 : : {
493 : 44381 : gcc_checking_assert (m_operator);
494 : 44381 : switch (dispatch_kind (lh, lh, rh))
495 : : {
496 : 44381 : case RO_III:
497 : 44381 : return m_operator->overflow_free_p(as_a <irange> (lh),
498 : : as_a <irange> (rh),
499 : 44381 : rel);
500 : : default:
501 : : return false;
502 : : }
503 : : }
504 : :
505 : : bool
506 : 9327069 : range_op_handler::operand_check_p (tree t1, tree t2, tree t3) const
507 : : {
508 : 9327069 : gcc_checking_assert (m_operator);
509 : 9327069 : return m_operator->operand_check_p (t1, t2, t3);
510 : : }
511 : :
512 : : // Update the known bitmasks in R when applying the operation CODE to
513 : : // LH and RH.
514 : :
515 : : void
516 : 174361797 : update_known_bitmask (vrange &r, tree_code code,
517 : : const vrange &lh, const vrange &rh)
518 : : {
519 : 174361797 : if (r.undefined_p () || lh.undefined_p () || rh.undefined_p ()
520 : 348718891 : || r.singleton_p ())
521 : 9352790 : return;
522 : :
523 : 165009007 : widest_int widest_value, widest_mask;
524 : 165009007 : tree type = r.type ();
525 : 165009007 : signop sign = TYPE_SIGN (type);
526 : 165009007 : int prec = TYPE_PRECISION (type);
527 : 165009007 : irange_bitmask lh_bits = lh.get_bitmask ();
528 : 165009007 : irange_bitmask rh_bits = rh.get_bitmask ();
529 : :
530 : 165009007 : switch (get_gimple_rhs_class (code))
531 : : {
532 : 54657420 : case GIMPLE_UNARY_RHS:
533 : 54657420 : bit_value_unop (code, sign, prec, &widest_value, &widest_mask,
534 : 54657420 : TYPE_SIGN (lh.type ()),
535 : 54657420 : TYPE_PRECISION (lh.type ()),
536 : 109315300 : widest_int::from (lh_bits.value (),
537 : 54657420 : TYPE_SIGN (lh.type ())),
538 : 109314840 : widest_int::from (lh_bits.mask (),
539 : 54657420 : TYPE_SIGN (lh.type ())));
540 : 54657420 : break;
541 : 110351587 : case GIMPLE_BINARY_RHS:
542 : 220703174 : bit_value_binop (code, sign, prec, &widest_value, &widest_mask,
543 : 110351587 : TYPE_SIGN (lh.type ()),
544 : 110351587 : TYPE_PRECISION (lh.type ()),
545 : 220704061 : widest_int::from (lh_bits.value (), sign),
546 : 220704061 : widest_int::from (lh_bits.mask (), sign),
547 : 110351587 : TYPE_SIGN (rh.type ()),
548 : 110351587 : TYPE_PRECISION (rh.type ()),
549 : 220704031 : widest_int::from (rh_bits.value (), sign),
550 : 220703174 : widest_int::from (rh_bits.mask (), sign));
551 : 110351587 : break;
552 : 0 : default:
553 : 0 : gcc_unreachable ();
554 : : }
555 : :
556 : 165009007 : wide_int mask = wide_int::from (widest_mask, prec, sign);
557 : 330018014 : wide_int value = wide_int::from (widest_value, prec, sign);
558 : : // Bitmasks must have the unknown value bits cleared.
559 : 165009007 : value &= ~mask;
560 : 330018014 : irange_bitmask bm (value, mask);
561 : 165009007 : r.update_bitmask (bm);
562 : 165009944 : }
563 : :
564 : : // Return the upper limit for a type.
565 : :
566 : : static inline wide_int
567 : 17357158 : max_limit (const_tree type)
568 : : {
569 : 17357158 : return irange_val_max (type);
570 : : }
571 : :
572 : : // Return the lower limit for a type.
573 : :
574 : : static inline wide_int
575 : 20291541 : min_limit (const_tree type)
576 : : {
577 : 20291541 : return irange_val_min (type);
578 : : }
579 : :
580 : : // Return false if shifting by OP is undefined behavior. Otherwise, return
581 : : // true and the range it is to be shifted by. This allows trimming out of
582 : : // undefined ranges, leaving only valid ranges if there are any.
583 : :
584 : : static inline bool
585 : 4889886 : get_shift_range (irange &r, tree type, const irange &op)
586 : : {
587 : 4889886 : if (op.undefined_p ())
588 : : return false;
589 : :
590 : : // Build valid range and intersect it with the shift range.
591 : 4889405 : r.set (op.type (),
592 : 9778810 : wi::shwi (0, TYPE_PRECISION (op.type ())),
593 : 4889405 : wi::shwi (TYPE_PRECISION (type) - 1, TYPE_PRECISION (op.type ())));
594 : 4889405 : r.intersect (op);
595 : :
596 : : // If there are no valid ranges in the shift range, returned false.
597 : 4889405 : if (r.undefined_p ())
598 : : return false;
599 : : return true;
600 : : }
601 : :
602 : : // Default wide_int fold operation returns [MIN, MAX].
603 : :
604 : : void
605 : 0 : range_operator::wi_fold (irange &r, tree type,
606 : : const wide_int &lh_lb ATTRIBUTE_UNUSED,
607 : : const wide_int &lh_ub ATTRIBUTE_UNUSED,
608 : : const wide_int &rh_lb ATTRIBUTE_UNUSED,
609 : : const wide_int &rh_ub ATTRIBUTE_UNUSED) const
610 : : {
611 : 0 : gcc_checking_assert (r.supports_type_p (type));
612 : 0 : r.set_varying (type);
613 : 0 : }
614 : :
615 : : // Call wi_fold when both op1 and op2 are equivalent. Further split small
616 : : // subranges into constants. This can provide better precision.
617 : : // For x + y, when x == y with a range of [0,4] instead of [0, 8] produce
618 : : // [0,0][2, 2][4,4][6, 6][8, 8]
619 : : // LIMIT is the maximum number of elements in range allowed before we
620 : : // do not process them individually.
621 : :
622 : : void
623 : 53557 : range_operator::wi_fold_in_parts_equiv (irange &r, tree type,
624 : : const wide_int &lh_lb,
625 : : const wide_int &lh_ub,
626 : : unsigned limit) const
627 : : {
628 : 53557 : int_range_max tmp;
629 : 107114 : widest_int lh_range = wi::sub (widest_int::from (lh_ub, TYPE_SIGN (type)),
630 : 107114 : widest_int::from (lh_lb, TYPE_SIGN (type)));
631 : : // if there are 1 to 8 values in the LH range, split them up.
632 : 53557 : r.set_undefined ();
633 : 107114 : if (lh_range >= 0 && lh_range < limit)
634 : : {
635 : 15755 : for (unsigned x = 0; x <= lh_range; x++)
636 : : {
637 : 10763 : wide_int val = lh_lb + x;
638 : 10763 : wi_fold (tmp, type, val, val, val, val);
639 : 10763 : r.union_ (tmp);
640 : 10763 : }
641 : : }
642 : : // Otherwise just call wi_fold.
643 : : else
644 : 48565 : wi_fold (r, type, lh_lb, lh_ub, lh_lb, lh_ub);
645 : 53557 : }
646 : :
647 : : // Call wi_fold, except further split small subranges into constants.
648 : : // This can provide better precision. For something 8 >> [0,1]
649 : : // Instead of [8, 16], we will produce [8,8][16,16]
650 : :
651 : : void
652 : 147361388 : range_operator::wi_fold_in_parts (irange &r, tree type,
653 : : const wide_int &lh_lb,
654 : : const wide_int &lh_ub,
655 : : const wide_int &rh_lb,
656 : : const wide_int &rh_ub) const
657 : : {
658 : 147361388 : int_range_max tmp;
659 : 294722776 : widest_int rh_range = wi::sub (widest_int::from (rh_ub, TYPE_SIGN (type)),
660 : 294722776 : widest_int::from (rh_lb, TYPE_SIGN (type)));
661 : 294722776 : widest_int lh_range = wi::sub (widest_int::from (lh_ub, TYPE_SIGN (type)),
662 : 294722776 : widest_int::from (lh_lb, TYPE_SIGN (type)));
663 : : // If there are 2, 3, or 4 values in the RH range, do them separately.
664 : : // Call wi_fold_in_parts to check the RH side.
665 : 147361388 : if (rh_range > 0 && rh_range < 4)
666 : : {
667 : 5245339 : wi_fold_in_parts (r, type, lh_lb, lh_ub, rh_lb, rh_lb);
668 : 5245339 : if (rh_range > 1)
669 : : {
670 : 563765 : wi_fold_in_parts (tmp, type, lh_lb, lh_ub, rh_lb + 1, rh_lb + 1);
671 : 563765 : r.union_ (tmp);
672 : 563765 : if (rh_range == 3)
673 : : {
674 : 343652 : wi_fold_in_parts (tmp, type, lh_lb, lh_ub, rh_lb + 2, rh_lb + 2);
675 : 343652 : r.union_ (tmp);
676 : : }
677 : : }
678 : 5245339 : wi_fold_in_parts (tmp, type, lh_lb, lh_ub, rh_ub, rh_ub);
679 : 5245339 : r.union_ (tmp);
680 : : }
681 : : // Otherwise check for 2, 3, or 4 values in the LH range and split them up.
682 : : // The RH side has been checked, so no recursion needed.
683 : 142116049 : else if (lh_range > 0 && lh_range < 4)
684 : : {
685 : 10366338 : wi_fold (r, type, lh_lb, lh_lb, rh_lb, rh_ub);
686 : 10366338 : if (lh_range > 1)
687 : : {
688 : 1802879 : wi_fold (tmp, type, lh_lb + 1, lh_lb + 1, rh_lb, rh_ub);
689 : 1802871 : r.union_ (tmp);
690 : 1802871 : if (lh_range == 3)
691 : : {
692 : 767034 : wi_fold (tmp, type, lh_lb + 2, lh_lb + 2, rh_lb, rh_ub);
693 : 767030 : r.union_ (tmp);
694 : : }
695 : : }
696 : 10366338 : wi_fold (tmp, type, lh_ub, lh_ub, rh_lb, rh_ub);
697 : 10366338 : r.union_ (tmp);
698 : : }
699 : : // Otherwise just call wi_fold.
700 : : else
701 : 131749711 : wi_fold (r, type, lh_lb, lh_ub, rh_lb, rh_ub);
702 : 147361736 : }
703 : :
704 : : // The default for fold is to break all ranges into sub-ranges and
705 : : // invoke the wi_fold method on each sub-range pair.
706 : :
707 : : bool
708 : 103420078 : range_operator::fold_range (irange &r, tree type,
709 : : const irange &lh,
710 : : const irange &rh,
711 : : relation_trio trio) const
712 : : {
713 : 103420078 : gcc_checking_assert (r.supports_type_p (type));
714 : 103420078 : if (empty_range_varying (r, type, lh, rh))
715 : 46196 : return true;
716 : :
717 : 103373882 : relation_kind rel = trio.op1_op2 ();
718 : 103373882 : unsigned num_lh = lh.num_pairs ();
719 : 103373882 : unsigned num_rh = rh.num_pairs ();
720 : :
721 : : // If op1 and op2 are equivalences, then we don't need a complete cross
722 : : // product, just pairs of matching elements.
723 : 103374252 : if (relation_equiv_p (rel) && lh == rh)
724 : : {
725 : 50506 : int_range_max tmp;
726 : 50506 : r.set_undefined ();
727 : 89398 : for (unsigned x = 0; x < num_lh; ++x)
728 : : {
729 : : // If the number of subranges is too high, limit subrange creation.
730 : 53557 : unsigned limit = (r.num_pairs () > 32) ? 0 : 8;
731 : 53557 : wide_int lh_lb = lh.lower_bound (x);
732 : 53557 : wide_int lh_ub = lh.upper_bound (x);
733 : 53557 : wi_fold_in_parts_equiv (tmp, type, lh_lb, lh_ub, limit);
734 : 53557 : r.union_ (tmp);
735 : 53557 : if (r.varying_p ())
736 : : break;
737 : 53557 : }
738 : 50506 : op1_op2_relation_effect (r, type, lh, rh, rel);
739 : 50506 : update_bitmask (r, lh, rh);
740 : 50506 : return true;
741 : 50506 : }
742 : :
743 : : // If both ranges are single pairs, fold directly into the result range.
744 : : // If the number of subranges grows too high, produce a summary result as the
745 : : // loop becomes exponential with little benefit. See PR 103821.
746 : 103323376 : if ((num_lh == 1 && num_rh == 1) || num_lh * num_rh > 12)
747 : : {
748 : 85604798 : wi_fold_in_parts (r, type, lh.lower_bound (), lh.upper_bound (),
749 : 171207320 : rh.lower_bound (), rh.upper_bound ());
750 : 85603660 : op1_op2_relation_effect (r, type, lh, rh, rel);
751 : 85603660 : update_bitmask (r, lh, rh);
752 : 85603660 : return true;
753 : : }
754 : :
755 : 17719716 : int_range_max tmp;
756 : 17719716 : r.set_undefined ();
757 : 55582894 : for (unsigned x = 0; x < num_lh; ++x)
758 : 88222811 : for (unsigned y = 0; y < num_rh; ++y)
759 : : {
760 : 50359633 : wide_int lh_lb = lh.lower_bound (x);
761 : 50359633 : wide_int lh_ub = lh.upper_bound (x);
762 : 50359633 : wide_int rh_lb = rh.lower_bound (y);
763 : 50359633 : wide_int rh_ub = rh.upper_bound (y);
764 : 50359633 : wi_fold_in_parts (tmp, type, lh_lb, lh_ub, rh_lb, rh_ub);
765 : 50359633 : r.union_ (tmp);
766 : 50359633 : if (r.varying_p ())
767 : : {
768 : 4021166 : op1_op2_relation_effect (r, type, lh, rh, rel);
769 : 4021166 : update_bitmask (r, lh, rh);
770 : 4021166 : return true;
771 : : }
772 : 50360722 : }
773 : 13698550 : op1_op2_relation_effect (r, type, lh, rh, rel);
774 : 13698550 : update_bitmask (r, lh, rh);
775 : 13698550 : return true;
776 : 17719716 : }
777 : :
778 : :
779 : : bool
780 : 71419 : range_operator::fold_range (frange &, tree, const irange &,
781 : : const frange &, relation_trio) const
782 : : {
783 : 71419 : return false;
784 : : }
785 : :
786 : : bool
787 : 1553 : range_operator::op1_range (irange &, tree, const frange &,
788 : : const irange &, relation_trio) const
789 : : {
790 : 1553 : return false;
791 : : }
792 : :
793 : :
794 : :
795 : : // The default for op1_range is to return false.
796 : :
797 : : bool
798 : 710195 : range_operator::op1_range (irange &r ATTRIBUTE_UNUSED,
799 : : tree type ATTRIBUTE_UNUSED,
800 : : const irange &lhs ATTRIBUTE_UNUSED,
801 : : const irange &op2 ATTRIBUTE_UNUSED,
802 : : relation_trio) const
803 : : {
804 : 710195 : return false;
805 : : }
806 : :
807 : : // The default for op2_range is to return false.
808 : :
809 : : bool
810 : 569960 : range_operator::op2_range (irange &r ATTRIBUTE_UNUSED,
811 : : tree type ATTRIBUTE_UNUSED,
812 : : const irange &lhs ATTRIBUTE_UNUSED,
813 : : const irange &op1 ATTRIBUTE_UNUSED,
814 : : relation_trio) const
815 : : {
816 : 569960 : return false;
817 : : }
818 : :
819 : : // The default relation routines return VREL_VARYING.
820 : :
821 : : relation_kind
822 : 23663871 : range_operator::lhs_op1_relation (const irange &lhs ATTRIBUTE_UNUSED,
823 : : const irange &op1 ATTRIBUTE_UNUSED,
824 : : const irange &op2 ATTRIBUTE_UNUSED,
825 : : relation_kind rel ATTRIBUTE_UNUSED) const
826 : : {
827 : 23663871 : return VREL_VARYING;
828 : : }
829 : :
830 : : relation_kind
831 : 17463804 : range_operator::lhs_op2_relation (const irange &lhs ATTRIBUTE_UNUSED,
832 : : const irange &op1 ATTRIBUTE_UNUSED,
833 : : const irange &op2 ATTRIBUTE_UNUSED,
834 : : relation_kind rel ATTRIBUTE_UNUSED) const
835 : : {
836 : 17463804 : return VREL_VARYING;
837 : : }
838 : :
839 : : relation_kind
840 : 14948544 : range_operator::op1_op2_relation (const irange &lhs ATTRIBUTE_UNUSED,
841 : : const irange &op1 ATTRIBUTE_UNUSED,
842 : : const irange &op2 ATTRIBUTE_UNUSED) const
843 : : {
844 : 14948544 : return VREL_VARYING;
845 : : }
846 : :
847 : : // Default is no relation affects the LHS.
848 : :
849 : : bool
850 : 84657358 : range_operator::op1_op2_relation_effect (irange &lhs_range ATTRIBUTE_UNUSED,
851 : : tree type ATTRIBUTE_UNUSED,
852 : : const irange &op1_range
853 : : ATTRIBUTE_UNUSED,
854 : : const irange &op2_range
855 : : ATTRIBUTE_UNUSED,
856 : : relation_kind rel
857 : : ATTRIBUTE_UNUSED) const
858 : : {
859 : 84657358 : return false;
860 : : }
861 : :
862 : : bool
863 : 0 : range_operator::overflow_free_p (const irange &, const irange &,
864 : : relation_trio) const
865 : : {
866 : 0 : return false;
867 : : }
868 : :
869 : : // Apply any known bitmask updates based on this operator.
870 : :
871 : : void
872 : 6281 : range_operator::update_bitmask (irange &, const irange &,
873 : : const irange &) const
874 : : {
875 : 6281 : }
876 : :
877 : : // Check that operand types are OK. Default to always OK.
878 : :
879 : : bool
880 : 123793429 : range_operator::operand_check_p (tree, tree, tree) const
881 : : {
882 : 123793429 : return true;
883 : : }
884 : :
885 : : // Create and return a range from a pair of wide-ints that are known
886 : : // to have overflowed (or underflowed).
887 : :
888 : : static void
889 : 42489135 : value_range_from_overflowed_bounds (irange &r, tree type,
890 : : const wide_int &wmin,
891 : : const wide_int &wmax)
892 : : {
893 : 42489135 : const signop sgn = TYPE_SIGN (type);
894 : 42489135 : const unsigned int prec = TYPE_PRECISION (type);
895 : :
896 : 42489135 : wide_int tmin = wide_int::from (wmin, prec, sgn);
897 : 42489135 : wide_int tmax = wide_int::from (wmax, prec, sgn);
898 : :
899 : 42489135 : bool covers = false;
900 : 42489135 : wide_int tem = tmin;
901 : 42489135 : tmin = tmax + 1;
902 : 42489135 : if (wi::cmp (tmin, tmax, sgn) < 0)
903 : 2790700 : covers = true;
904 : 42489135 : tmax = tem - 1;
905 : 42489135 : if (wi::cmp (tmax, tem, sgn) > 0)
906 : : covers = true;
907 : :
908 : : // If the anti-range would cover nothing, drop to varying.
909 : : // Likewise if the anti-range bounds are outside of the types
910 : : // values.
911 : 39759259 : if (covers || wi::cmp (tmin, tmax, sgn) > 0)
912 : 28909689 : r.set_varying (type);
913 : : else
914 : 13579446 : r.set (type, tmin, tmax, VR_ANTI_RANGE);
915 : 42489729 : }
916 : :
917 : : // Create and return a range from a pair of wide-ints. MIN_OVF and
918 : : // MAX_OVF describe any overflow that might have occurred while
919 : : // calculating WMIN and WMAX respectively.
920 : :
921 : : static void
922 : 143759805 : value_range_with_overflow (irange &r, tree type,
923 : : const wide_int &wmin, const wide_int &wmax,
924 : : wi::overflow_type min_ovf = wi::OVF_NONE,
925 : : wi::overflow_type max_ovf = wi::OVF_NONE)
926 : : {
927 : 143759805 : const signop sgn = TYPE_SIGN (type);
928 : 143759805 : const unsigned int prec = TYPE_PRECISION (type);
929 : 228169054 : const bool overflow_wraps = TYPE_OVERFLOW_WRAPS (type);
930 : :
931 : : // For one bit precision if max != min, then the range covers all
932 : : // values.
933 : 158807079 : if (prec == 1 && wi::ne_p (wmax, wmin))
934 : : {
935 : 0 : r.set_varying (type);
936 : 0 : return;
937 : : }
938 : :
939 : 143759805 : if (overflow_wraps)
940 : : {
941 : : // If overflow wraps, truncate the values and adjust the range,
942 : : // kind, and bounds appropriately.
943 : 84409249 : if ((min_ovf != wi::OVF_NONE) == (max_ovf != wi::OVF_NONE))
944 : : {
945 : 58911420 : wide_int tmin = wide_int::from (wmin, prec, sgn);
946 : 58911420 : wide_int tmax = wide_int::from (wmax, prec, sgn);
947 : : // If the limits are swapped, we wrapped around and cover
948 : : // the entire range.
949 : 58911420 : if (wi::gt_p (tmin, tmax, sgn))
950 : 539364 : r.set_varying (type);
951 : : else
952 : : // No overflow or both overflow or underflow. The range
953 : : // kind stays normal.
954 : 58372056 : r.set (type, tmin, tmax);
955 : 58911420 : return;
956 : 58911558 : }
957 : :
958 : 25497829 : if ((min_ovf == wi::OVF_UNDERFLOW && max_ovf == wi::OVF_NONE)
959 : 17855514 : || (max_ovf == wi::OVF_OVERFLOW && min_ovf == wi::OVF_NONE))
960 : 25497829 : value_range_from_overflowed_bounds (r, type, wmin, wmax);
961 : : else
962 : : // Other underflow and/or overflow, drop to VR_VARYING.
963 : 0 : r.set_varying (type);
964 : : }
965 : : else
966 : : {
967 : : // If both bounds either underflowed or overflowed, then the result
968 : : // is undefined.
969 : 59350556 : if ((min_ovf == wi::OVF_OVERFLOW && max_ovf == wi::OVF_OVERFLOW)
970 : 59348206 : || (min_ovf == wi::OVF_UNDERFLOW && max_ovf == wi::OVF_UNDERFLOW))
971 : : {
972 : 3879 : r.set_undefined ();
973 : 3879 : return;
974 : : }
975 : :
976 : : // If overflow does not wrap, saturate to [MIN, MAX].
977 : 59346677 : wide_int new_lb, new_ub;
978 : 59346677 : if (min_ovf == wi::OVF_UNDERFLOW)
979 : 7203393 : new_lb = wi::min_value (prec, sgn);
980 : 52143494 : else if (min_ovf == wi::OVF_OVERFLOW)
981 : 0 : new_lb = wi::max_value (prec, sgn);
982 : : else
983 : 52143494 : new_lb = wmin;
984 : :
985 : 59346677 : if (max_ovf == wi::OVF_UNDERFLOW)
986 : 0 : new_ub = wi::min_value (prec, sgn);
987 : 59346677 : else if (max_ovf == wi::OVF_OVERFLOW)
988 : 12115824 : new_ub = wi::max_value (prec, sgn);
989 : : else
990 : 47231107 : new_ub = wmax;
991 : :
992 : 59346677 : r.set (type, new_lb, new_ub);
993 : 59347248 : }
994 : : }
995 : :
996 : : // Create and return a range from a pair of wide-ints. Canonicalize
997 : : // the case where the bounds are swapped. In which case, we transform
998 : : // [10,5] into [MIN,5][10,MAX].
999 : :
1000 : : static inline void
1001 : 84482245 : create_possibly_reversed_range (irange &r, tree type,
1002 : : const wide_int &new_lb, const wide_int &new_ub)
1003 : : {
1004 : 84482245 : signop s = TYPE_SIGN (type);
1005 : : // If the bounds are swapped, treat the result as if an overflow occurred.
1006 : 84482245 : if (wi::gt_p (new_lb, new_ub, s))
1007 : 16991306 : value_range_from_overflowed_bounds (r, type, new_lb, new_ub);
1008 : : else
1009 : : // Otherwise it's just a normal range.
1010 : 67490939 : r.set (type, new_lb, new_ub);
1011 : 84482245 : }
1012 : :
1013 : : // Return the summary information about boolean range LHS. If EMPTY/FULL,
1014 : : // return the equivalent range for TYPE in R; if FALSE/TRUE, do nothing.
1015 : :
1016 : : bool_range_state
1017 : 86242192 : get_bool_state (vrange &r, const vrange &lhs, tree val_type)
1018 : : {
1019 : : // If there is no result, then this is unexecutable.
1020 : 86242192 : if (lhs.undefined_p ())
1021 : : {
1022 : 0 : r.set_undefined ();
1023 : 0 : return BRS_EMPTY;
1024 : : }
1025 : :
1026 : 86242192 : if (lhs.zero_p ())
1027 : : return BRS_FALSE;
1028 : :
1029 : : // For TRUE, we can't just test for [1,1] because Ada can have
1030 : : // multi-bit booleans, and TRUE values can be: [1, MAX], ~[0], etc.
1031 : 44106350 : if (lhs.contains_p (build_zero_cst (lhs.type ())))
1032 : : {
1033 : 167995 : r.set_varying (val_type);
1034 : 167995 : return BRS_FULL;
1035 : : }
1036 : :
1037 : : return BRS_TRUE;
1038 : : }
1039 : :
1040 : : // ------------------------------------------------------------------------
1041 : :
1042 : : void
1043 : 0 : operator_equal::update_bitmask (irange &r, const irange &lh,
1044 : : const irange &rh) const
1045 : : {
1046 : 0 : update_known_bitmask (r, EQ_EXPR, lh, rh);
1047 : 0 : }
1048 : :
1049 : : // Check if the LHS range indicates a relation between OP1 and OP2.
1050 : :
1051 : : relation_kind
1052 : 5832838 : operator_equal::op1_op2_relation (const irange &lhs, const irange &,
1053 : : const irange &) const
1054 : : {
1055 : 5832838 : if (lhs.undefined_p ())
1056 : : return VREL_UNDEFINED;
1057 : :
1058 : : // FALSE = op1 == op2 indicates NE_EXPR.
1059 : 5832838 : if (lhs.zero_p ())
1060 : : return VREL_NE;
1061 : :
1062 : : // TRUE = op1 == op2 indicates EQ_EXPR.
1063 : 3046907 : if (!contains_zero_p (lhs))
1064 : 3020818 : return VREL_EQ;
1065 : : return VREL_VARYING;
1066 : : }
1067 : :
1068 : : bool
1069 : 18052174 : operator_equal::fold_range (irange &r, tree type,
1070 : : const irange &op1,
1071 : : const irange &op2,
1072 : : relation_trio rel) const
1073 : : {
1074 : 18052174 : if (relop_early_resolve (r, type, op1, op2, rel, VREL_EQ))
1075 : : return true;
1076 : :
1077 : : // We can be sure the values are always equal or not if both ranges
1078 : : // consist of a single value, and then compare them.
1079 : 18022158 : bool op1_const = wi::eq_p (op1.lower_bound (), op1.upper_bound ());
1080 : 18022158 : bool op2_const = wi::eq_p (op2.lower_bound (), op2.upper_bound ());
1081 : 18022148 : if (op1_const && op2_const)
1082 : : {
1083 : 289977 : if (wi::eq_p (op1.lower_bound (), op2.upper_bound()))
1084 : 151819 : r = range_true (type);
1085 : : else
1086 : 138158 : r = range_false (type);
1087 : : }
1088 : : else
1089 : : {
1090 : : // If ranges do not intersect, we know the range is not equal,
1091 : : // otherwise we don't know anything for sure.
1092 : 17732171 : int_range_max tmp = op1;
1093 : 17732171 : tmp.intersect (op2);
1094 : 17732171 : if (tmp.undefined_p ())
1095 : 254632 : r = range_false (type);
1096 : : // Check if a constant cannot satisfy the bitmask requirements.
1097 : 32612173 : else if (op2_const && !op1.get_bitmask ().member_p (op2.lower_bound ()))
1098 : 0 : r = range_false (type);
1099 : 17535637 : else if (op1_const && !op2.get_bitmask ().member_p (op1.lower_bound ()))
1100 : 0 : r = range_false (type);
1101 : : else
1102 : 17477539 : r = range_true_and_false (type);
1103 : 17732171 : }
1104 : : return true;
1105 : : }
1106 : :
1107 : : bool
1108 : 11568937 : operator_equal::op1_range (irange &r, tree type,
1109 : : const irange &lhs,
1110 : : const irange &op2,
1111 : : relation_trio) const
1112 : : {
1113 : 11568937 : switch (get_bool_state (r, lhs, type))
1114 : : {
1115 : 3464225 : case BRS_TRUE:
1116 : : // If it's true, the result is the same as OP2.
1117 : 3464225 : r = op2;
1118 : 3464225 : break;
1119 : :
1120 : 8073167 : case BRS_FALSE:
1121 : : // If the result is false, the only time we know anything is
1122 : : // if OP2 is a constant.
1123 : 8073167 : if (!op2.undefined_p ()
1124 : 24219501 : && wi::eq_p (op2.lower_bound(), op2.upper_bound()))
1125 : : {
1126 : 6384915 : r = op2;
1127 : 6384915 : r.invert ();
1128 : : }
1129 : : else
1130 : 1688252 : r.set_varying (type);
1131 : : break;
1132 : :
1133 : : default:
1134 : : break;
1135 : : }
1136 : 11568937 : return true;
1137 : : }
1138 : :
1139 : : bool
1140 : 1532265 : operator_equal::op2_range (irange &r, tree type,
1141 : : const irange &lhs,
1142 : : const irange &op1,
1143 : : relation_trio rel) const
1144 : : {
1145 : 1532265 : return operator_equal::op1_range (r, type, lhs, op1, rel.swap_op1_op2 ());
1146 : : }
1147 : :
1148 : : // -------------------------------------------------------------------------
1149 : :
1150 : : void
1151 : 0 : operator_not_equal::update_bitmask (irange &r, const irange &lh,
1152 : : const irange &rh) const
1153 : : {
1154 : 0 : update_known_bitmask (r, NE_EXPR, lh, rh);
1155 : 0 : }
1156 : :
1157 : : // Check if the LHS range indicates a relation between OP1 and OP2.
1158 : :
1159 : : relation_kind
1160 : 10933437 : operator_not_equal::op1_op2_relation (const irange &lhs, const irange &,
1161 : : const irange &) const
1162 : : {
1163 : 10933437 : if (lhs.undefined_p ())
1164 : : return VREL_UNDEFINED;
1165 : :
1166 : : // FALSE = op1 != op2 indicates EQ_EXPR.
1167 : 10933437 : if (lhs.zero_p ())
1168 : : return VREL_EQ;
1169 : :
1170 : : // TRUE = op1 != op2 indicates NE_EXPR.
1171 : 5534986 : if (!contains_zero_p (lhs))
1172 : 5495525 : return VREL_NE;
1173 : : return VREL_VARYING;
1174 : : }
1175 : :
1176 : : bool
1177 : 27177890 : operator_not_equal::fold_range (irange &r, tree type,
1178 : : const irange &op1,
1179 : : const irange &op2,
1180 : : relation_trio rel) const
1181 : : {
1182 : 27177890 : if (relop_early_resolve (r, type, op1, op2, rel, VREL_NE))
1183 : : return true;
1184 : :
1185 : : // We can be sure the values are always equal or not if both ranges
1186 : : // consist of a single value, and then compare them.
1187 : 27157046 : bool op1_const = wi::eq_p (op1.lower_bound (), op1.upper_bound ());
1188 : 27157046 : bool op2_const = wi::eq_p (op2.lower_bound (), op2.upper_bound ());
1189 : 27156713 : if (op1_const && op2_const)
1190 : : {
1191 : 1111991 : if (wi::ne_p (op1.lower_bound (), op2.upper_bound()))
1192 : 628575 : r = range_true (type);
1193 : : else
1194 : 483414 : r = range_false (type);
1195 : : }
1196 : : else
1197 : : {
1198 : : // If ranges do not intersect, we know the range is not equal,
1199 : : // otherwise we don't know anything for sure.
1200 : 26044724 : int_range_max tmp = op1;
1201 : 26044724 : tmp.intersect (op2);
1202 : 26044724 : if (tmp.undefined_p ())
1203 : 244943 : r = range_true (type);
1204 : : // Check if a constant cannot satisfy the bitmask requirements.
1205 : 47041056 : else if (op2_const && !op1.get_bitmask ().member_p (op2.lower_bound ()))
1206 : 0 : r = range_true (type);
1207 : 25836163 : else if (op1_const && !op2.get_bitmask ().member_p (op1.lower_bound ()))
1208 : 0 : r = range_true (type);
1209 : : else
1210 : 25799781 : r = range_true_and_false (type);
1211 : 26044724 : }
1212 : : return true;
1213 : : }
1214 : :
1215 : : bool
1216 : 22256990 : operator_not_equal::op1_range (irange &r, tree type,
1217 : : const irange &lhs,
1218 : : const irange &op2,
1219 : : relation_trio) const
1220 : : {
1221 : 22256990 : switch (get_bool_state (r, lhs, type))
1222 : : {
1223 : 14098173 : case BRS_TRUE:
1224 : : // If the result is true, the only time we know anything is if
1225 : : // OP2 is a constant.
1226 : 14098173 : if (!op2.undefined_p ()
1227 : 42294519 : && wi::eq_p (op2.lower_bound(), op2.upper_bound()))
1228 : : {
1229 : 11060088 : r = op2;
1230 : 11060088 : r.invert ();
1231 : : }
1232 : : else
1233 : 3038085 : r.set_varying (type);
1234 : : break;
1235 : :
1236 : 8133050 : case BRS_FALSE:
1237 : : // If it's false, the result is the same as OP2.
1238 : 8133050 : r = op2;
1239 : 8133050 : break;
1240 : :
1241 : : default:
1242 : : break;
1243 : : }
1244 : 22256990 : return true;
1245 : : }
1246 : :
1247 : :
1248 : : bool
1249 : 2782831 : operator_not_equal::op2_range (irange &r, tree type,
1250 : : const irange &lhs,
1251 : : const irange &op1,
1252 : : relation_trio rel) const
1253 : : {
1254 : 2782831 : return operator_not_equal::op1_range (r, type, lhs, op1, rel.swap_op1_op2 ());
1255 : : }
1256 : :
1257 : : // (X < VAL) produces the range of [MIN, VAL - 1].
1258 : :
1259 : : static void
1260 : 6630134 : build_lt (irange &r, tree type, const wide_int &val)
1261 : : {
1262 : 6630134 : wi::overflow_type ov;
1263 : 6630134 : wide_int lim;
1264 : 6630134 : signop sgn = TYPE_SIGN (type);
1265 : :
1266 : : // Signed 1 bit cannot represent 1 for subtraction.
1267 : 6630134 : if (sgn == SIGNED)
1268 : 4438638 : lim = wi::add (val, -1, sgn, &ov);
1269 : : else
1270 : 2191554 : lim = wi::sub (val, 1, sgn, &ov);
1271 : :
1272 : : // If val - 1 underflows, check if X < MIN, which is an empty range.
1273 : 6630134 : if (ov)
1274 : 426 : r.set_undefined ();
1275 : : else
1276 : 6629766 : r = int_range<1> (type, min_limit (type), lim);
1277 : 6630134 : }
1278 : :
1279 : : // (X <= VAL) produces the range of [MIN, VAL].
1280 : :
1281 : : static void
1282 : 13661805 : build_le (irange &r, tree type, const wide_int &val)
1283 : : {
1284 : 13661805 : r = int_range<1> (type, min_limit (type), val);
1285 : 13661805 : }
1286 : :
1287 : : // (X > VAL) produces the range of [VAL + 1, MAX].
1288 : :
1289 : : static void
1290 : 10246918 : build_gt (irange &r, tree type, const wide_int &val)
1291 : : {
1292 : 10246918 : wi::overflow_type ov;
1293 : 10246918 : wide_int lim;
1294 : 10246918 : signop sgn = TYPE_SIGN (type);
1295 : :
1296 : : // Signed 1 bit cannot represent 1 for addition.
1297 : 10246918 : if (sgn == SIGNED)
1298 : 5278139 : lim = wi::sub (val, -1, sgn, &ov);
1299 : : else
1300 : 4968865 : lim = wi::add (val, 1, sgn, &ov);
1301 : : // If val + 1 overflows, check is for X > MAX, which is an empty range.
1302 : 10246918 : if (ov)
1303 : 0 : r.set_undefined ();
1304 : : else
1305 : 10247004 : r = int_range<1> (type, lim, max_limit (type));
1306 : 10246918 : }
1307 : :
1308 : : // (X >= val) produces the range of [VAL, MAX].
1309 : :
1310 : : static void
1311 : 7110212 : build_ge (irange &r, tree type, const wide_int &val)
1312 : : {
1313 : 7110212 : r = int_range<1> (type, val, max_limit (type));
1314 : 7110212 : }
1315 : :
1316 : :
1317 : : void
1318 : 0 : operator_lt::update_bitmask (irange &r, const irange &lh,
1319 : : const irange &rh) const
1320 : : {
1321 : 0 : update_known_bitmask (r, LT_EXPR, lh, rh);
1322 : 0 : }
1323 : :
1324 : : // Check if the LHS range indicates a relation between OP1 and OP2.
1325 : :
1326 : : relation_kind
1327 : 12374007 : operator_lt::op1_op2_relation (const irange &lhs, const irange &,
1328 : : const irange &) const
1329 : : {
1330 : 12374007 : if (lhs.undefined_p ())
1331 : : return VREL_UNDEFINED;
1332 : :
1333 : : // FALSE = op1 < op2 indicates GE_EXPR.
1334 : 12374007 : if (lhs.zero_p ())
1335 : : return VREL_GE;
1336 : :
1337 : : // TRUE = op1 < op2 indicates LT_EXPR.
1338 : 6346272 : if (!contains_zero_p (lhs))
1339 : 6338670 : return VREL_LT;
1340 : : return VREL_VARYING;
1341 : : }
1342 : :
1343 : : bool
1344 : 6514054 : operator_lt::fold_range (irange &r, tree type,
1345 : : const irange &op1,
1346 : : const irange &op2,
1347 : : relation_trio rel) const
1348 : : {
1349 : 6514054 : if (relop_early_resolve (r, type, op1, op2, rel, VREL_LT))
1350 : : return true;
1351 : :
1352 : 6491415 : signop sign = TYPE_SIGN (op1.type ());
1353 : 6491415 : gcc_checking_assert (sign == TYPE_SIGN (op2.type ()));
1354 : :
1355 : 6491451 : if (wi::lt_p (op1.upper_bound (), op2.lower_bound (), sign))
1356 : 22862 : r = range_true (type);
1357 : 6468589 : else if (!wi::lt_p (op1.lower_bound (), op2.upper_bound (), sign))
1358 : 81605 : r = range_false (type);
1359 : : // Use nonzero bits to determine if < 0 is false.
1360 : 8388290 : else if (op2.zero_p () && !wi::neg_p (op1.get_nonzero_bits (), sign))
1361 : 0 : r = range_false (type);
1362 : : else
1363 : 6386948 : r = range_true_and_false (type);
1364 : : return true;
1365 : : }
1366 : :
1367 : : bool
1368 : 5584341 : operator_lt::op1_range (irange &r, tree type,
1369 : : const irange &lhs,
1370 : : const irange &op2,
1371 : : relation_trio) const
1372 : : {
1373 : 5584341 : if (op2.undefined_p ())
1374 : : return false;
1375 : :
1376 : 5584341 : switch (get_bool_state (r, lhs, type))
1377 : : {
1378 : 2454622 : case BRS_TRUE:
1379 : 2454622 : build_lt (r, type, op2.upper_bound ());
1380 : 2454622 : break;
1381 : :
1382 : 3124939 : case BRS_FALSE:
1383 : 3124939 : build_ge (r, type, op2.lower_bound ());
1384 : 3124939 : break;
1385 : :
1386 : : default:
1387 : : break;
1388 : : }
1389 : : return true;
1390 : : }
1391 : :
1392 : : bool
1393 : 4123444 : operator_lt::op2_range (irange &r, tree type,
1394 : : const irange &lhs,
1395 : : const irange &op1,
1396 : : relation_trio) const
1397 : : {
1398 : 4123444 : if (op1.undefined_p ())
1399 : : return false;
1400 : :
1401 : 4123442 : switch (get_bool_state (r, lhs, type))
1402 : : {
1403 : 1874228 : case BRS_TRUE:
1404 : 1874228 : build_gt (r, type, op1.lower_bound ());
1405 : 1874228 : break;
1406 : :
1407 : 2245702 : case BRS_FALSE:
1408 : 2245702 : build_le (r, type, op1.upper_bound ());
1409 : 2245702 : break;
1410 : :
1411 : : default:
1412 : : break;
1413 : : }
1414 : : return true;
1415 : : }
1416 : :
1417 : :
1418 : : void
1419 : 0 : operator_le::update_bitmask (irange &r, const irange &lh,
1420 : : const irange &rh) const
1421 : : {
1422 : 0 : update_known_bitmask (r, LE_EXPR, lh, rh);
1423 : 0 : }
1424 : :
1425 : : // Check if the LHS range indicates a relation between OP1 and OP2.
1426 : :
1427 : : relation_kind
1428 : 4417595 : operator_le::op1_op2_relation (const irange &lhs, const irange &,
1429 : : const irange &) const
1430 : : {
1431 : 4417595 : if (lhs.undefined_p ())
1432 : : return VREL_UNDEFINED;
1433 : :
1434 : : // FALSE = op1 <= op2 indicates GT_EXPR.
1435 : 4417595 : if (lhs.zero_p ())
1436 : : return VREL_GT;
1437 : :
1438 : : // TRUE = op1 <= op2 indicates LE_EXPR.
1439 : 2547392 : if (!contains_zero_p (lhs))
1440 : 2538060 : return VREL_LE;
1441 : : return VREL_VARYING;
1442 : : }
1443 : :
1444 : : bool
1445 : 5177373 : operator_le::fold_range (irange &r, tree type,
1446 : : const irange &op1,
1447 : : const irange &op2,
1448 : : relation_trio rel) const
1449 : : {
1450 : 5177373 : if (relop_early_resolve (r, type, op1, op2, rel, VREL_LE))
1451 : : return true;
1452 : :
1453 : 5164320 : signop sign = TYPE_SIGN (op1.type ());
1454 : 5164320 : gcc_checking_assert (sign == TYPE_SIGN (op2.type ()));
1455 : :
1456 : 5164324 : if (wi::le_p (op1.upper_bound (), op2.lower_bound (), sign))
1457 : 113870 : r = range_true (type);
1458 : 5050454 : else if (!wi::le_p (op1.lower_bound (), op2.upper_bound (), sign))
1459 : 80713 : r = range_false (type);
1460 : : else
1461 : 4969737 : r = range_true_and_false (type);
1462 : : return true;
1463 : : }
1464 : :
1465 : : bool
1466 : 6058795 : operator_le::op1_range (irange &r, tree type,
1467 : : const irange &lhs,
1468 : : const irange &op2,
1469 : : relation_trio) const
1470 : : {
1471 : 6058795 : if (op2.undefined_p ())
1472 : : return false;
1473 : :
1474 : 6058795 : switch (get_bool_state (r, lhs, type))
1475 : : {
1476 : 3240478 : case BRS_TRUE:
1477 : 3240478 : build_le (r, type, op2.upper_bound ());
1478 : 3240478 : break;
1479 : :
1480 : 2805369 : case BRS_FALSE:
1481 : 2805369 : build_gt (r, type, op2.lower_bound ());
1482 : 2805369 : break;
1483 : :
1484 : : default:
1485 : : break;
1486 : : }
1487 : : return true;
1488 : : }
1489 : :
1490 : : bool
1491 : 1120289 : operator_le::op2_range (irange &r, tree type,
1492 : : const irange &lhs,
1493 : : const irange &op1,
1494 : : relation_trio) const
1495 : : {
1496 : 1120289 : if (op1.undefined_p ())
1497 : : return false;
1498 : :
1499 : 1120289 : switch (get_bool_state (r, lhs, type))
1500 : : {
1501 : 501742 : case BRS_TRUE:
1502 : 501742 : build_ge (r, type, op1.lower_bound ());
1503 : 501742 : break;
1504 : :
1505 : 614695 : case BRS_FALSE:
1506 : 614695 : build_lt (r, type, op1.upper_bound ());
1507 : 614695 : break;
1508 : :
1509 : : default:
1510 : : break;
1511 : : }
1512 : : return true;
1513 : : }
1514 : :
1515 : :
1516 : : void
1517 : 0 : operator_gt::update_bitmask (irange &r, const irange &lh,
1518 : : const irange &rh) const
1519 : : {
1520 : 0 : update_known_bitmask (r, GT_EXPR, lh, rh);
1521 : 0 : }
1522 : :
1523 : : // Check if the LHS range indicates a relation between OP1 and OP2.
1524 : :
1525 : : relation_kind
1526 : 12099341 : operator_gt::op1_op2_relation (const irange &lhs, const irange &,
1527 : : const irange &) const
1528 : : {
1529 : 12099341 : if (lhs.undefined_p ())
1530 : : return VREL_UNDEFINED;
1531 : :
1532 : : // FALSE = op1 > op2 indicates LE_EXPR.
1533 : 12099341 : if (lhs.zero_p ())
1534 : : return VREL_LE;
1535 : :
1536 : : // TRUE = op1 > op2 indicates GT_EXPR.
1537 : 6621543 : if (!contains_zero_p (lhs))
1538 : 6608040 : return VREL_GT;
1539 : : return VREL_VARYING;
1540 : : }
1541 : :
1542 : : bool
1543 : 11710095 : operator_gt::fold_range (irange &r, tree type,
1544 : : const irange &op1, const irange &op2,
1545 : : relation_trio rel) const
1546 : : {
1547 : 11710095 : if (relop_early_resolve (r, type, op1, op2, rel, VREL_GT))
1548 : : return true;
1549 : :
1550 : 11650380 : signop sign = TYPE_SIGN (op1.type ());
1551 : 11650380 : gcc_checking_assert (sign == TYPE_SIGN (op2.type ()));
1552 : :
1553 : 11650406 : if (wi::gt_p (op1.lower_bound (), op2.upper_bound (), sign))
1554 : 97577 : r = range_true (type);
1555 : 11552829 : else if (!wi::gt_p (op1.upper_bound (), op2.lower_bound (), sign))
1556 : 388976 : r = range_false (type);
1557 : : else
1558 : 11163827 : r = range_true_and_false (type);
1559 : : return true;
1560 : : }
1561 : :
1562 : : bool
1563 : 12352164 : operator_gt::op1_range (irange &r, tree type,
1564 : : const irange &lhs, const irange &op2,
1565 : : relation_trio) const
1566 : : {
1567 : 12352164 : if (op2.undefined_p ())
1568 : : return false;
1569 : :
1570 : 12352164 : switch (get_bool_state (r, lhs, type))
1571 : : {
1572 : 5003178 : case BRS_TRUE:
1573 : 5003178 : build_gt (r, type, op2.lower_bound ());
1574 : 5003178 : break;
1575 : :
1576 : 7339683 : case BRS_FALSE:
1577 : 7339683 : build_le (r, type, op2.upper_bound ());
1578 : 7339683 : break;
1579 : :
1580 : : default:
1581 : : break;
1582 : : }
1583 : : return true;
1584 : : }
1585 : :
1586 : : bool
1587 : 3852349 : operator_gt::op2_range (irange &r, tree type,
1588 : : const irange &lhs,
1589 : : const irange &op1,
1590 : : relation_trio) const
1591 : : {
1592 : 3852349 : if (op1.undefined_p ())
1593 : : return false;
1594 : :
1595 : 3852349 : switch (get_bool_state (r, lhs, type))
1596 : : {
1597 : 2370122 : case BRS_TRUE:
1598 : 2370122 : build_lt (r, type, op1.upper_bound ());
1599 : 2370122 : break;
1600 : :
1601 : 1473633 : case BRS_FALSE:
1602 : 1473633 : build_ge (r, type, op1.lower_bound ());
1603 : 1473633 : break;
1604 : :
1605 : : default:
1606 : : break;
1607 : : }
1608 : : return true;
1609 : : }
1610 : :
1611 : :
1612 : : void
1613 : 0 : operator_ge::update_bitmask (irange &r, const irange &lh,
1614 : : const irange &rh) const
1615 : : {
1616 : 0 : update_known_bitmask (r, GE_EXPR, lh, rh);
1617 : 0 : }
1618 : :
1619 : : // Check if the LHS range indicates a relation between OP1 and OP2.
1620 : :
1621 : : relation_kind
1622 : 4105492 : operator_ge::op1_op2_relation (const irange &lhs, const irange &,
1623 : : const irange &) const
1624 : : {
1625 : 4105492 : if (lhs.undefined_p ())
1626 : : return VREL_UNDEFINED;
1627 : :
1628 : : // FALSE = op1 >= op2 indicates LT_EXPR.
1629 : 4105492 : if (lhs.zero_p ())
1630 : : return VREL_LT;
1631 : :
1632 : : // TRUE = op1 >= op2 indicates GE_EXPR.
1633 : 2209792 : if (!contains_zero_p (lhs))
1634 : 2204184 : return VREL_GE;
1635 : : return VREL_VARYING;
1636 : : }
1637 : :
1638 : : bool
1639 : 2686262 : operator_ge::fold_range (irange &r, tree type,
1640 : : const irange &op1,
1641 : : const irange &op2,
1642 : : relation_trio rel) const
1643 : : {
1644 : 2686262 : if (relop_early_resolve (r, type, op1, op2, rel, VREL_GE))
1645 : : return true;
1646 : :
1647 : 2674381 : signop sign = TYPE_SIGN (op1.type ());
1648 : 2674381 : gcc_checking_assert (sign == TYPE_SIGN (op2.type ()));
1649 : :
1650 : 2674413 : if (wi::ge_p (op1.lower_bound (), op2.upper_bound (), sign))
1651 : 171901 : r = range_true (type);
1652 : 2502512 : else if (!wi::ge_p (op1.upper_bound (), op2.lower_bound (), sign))
1653 : 6259 : r = range_false (type);
1654 : : else
1655 : 2496221 : r = range_true_and_false (type);
1656 : : return true;
1657 : : }
1658 : :
1659 : : bool
1660 : 3206913 : operator_ge::op1_range (irange &r, tree type,
1661 : : const irange &lhs,
1662 : : const irange &op2,
1663 : : relation_trio) const
1664 : : {
1665 : 3206913 : if (op2.undefined_p ())
1666 : : return false;
1667 : :
1668 : 3206913 : switch (get_bool_state (r, lhs, type))
1669 : : {
1670 : 2009898 : case BRS_TRUE:
1671 : 2009898 : build_ge (r, type, op2.lower_bound ());
1672 : 2009898 : break;
1673 : :
1674 : 1190695 : case BRS_FALSE:
1675 : 1190695 : build_lt (r, type, op2.upper_bound ());
1676 : 1190695 : break;
1677 : :
1678 : : default:
1679 : : break;
1680 : : }
1681 : : return true;
1682 : : }
1683 : :
1684 : : bool
1685 : 1402879 : operator_ge::op2_range (irange &r, tree type,
1686 : : const irange &lhs,
1687 : : const irange &op1,
1688 : : relation_trio) const
1689 : : {
1690 : 1402879 : if (op1.undefined_p ())
1691 : : return false;
1692 : :
1693 : 1402879 : switch (get_bool_state (r, lhs, type))
1694 : : {
1695 : 835942 : case BRS_TRUE:
1696 : 835942 : build_le (r, type, op1.upper_bound ());
1697 : 835942 : break;
1698 : :
1699 : 564143 : case BRS_FALSE:
1700 : 564143 : build_gt (r, type, op1.lower_bound ());
1701 : 564143 : break;
1702 : :
1703 : : default:
1704 : : break;
1705 : : }
1706 : : return true;
1707 : : }
1708 : :
1709 : :
1710 : : void
1711 : 51485981 : operator_plus::update_bitmask (irange &r, const irange &lh,
1712 : : const irange &rh) const
1713 : : {
1714 : 51485981 : update_known_bitmask (r, PLUS_EXPR, lh, rh);
1715 : 51485981 : }
1716 : :
1717 : : // Check to see if the range of OP2 indicates anything about the relation
1718 : : // between LHS and OP1.
1719 : :
1720 : : relation_kind
1721 : 47782205 : operator_plus::lhs_op1_relation (const irange &lhs,
1722 : : const irange &op1,
1723 : : const irange &op2,
1724 : : relation_kind) const
1725 : : {
1726 : 47782205 : if (lhs.undefined_p () || op1.undefined_p () || op2.undefined_p ())
1727 : : return VREL_VARYING;
1728 : :
1729 : 47754408 : tree type = lhs.type ();
1730 : 47754408 : unsigned prec = TYPE_PRECISION (type);
1731 : 47754408 : wi::overflow_type ovf1, ovf2;
1732 : 47754408 : signop sign = TYPE_SIGN (type);
1733 : :
1734 : : // LHS = OP1 + 0 indicates LHS == OP1.
1735 : 47754408 : if (op2.zero_p ())
1736 : : return VREL_EQ;
1737 : :
1738 : 47584744 : if (TYPE_OVERFLOW_WRAPS (type))
1739 : : {
1740 : 27464945 : wi::add (op1.lower_bound (), op2.lower_bound (), sign, &ovf1);
1741 : 27465164 : wi::add (op1.upper_bound (), op2.upper_bound (), sign, &ovf2);
1742 : : }
1743 : : else
1744 : 20120237 : ovf1 = ovf2 = wi::OVF_NONE;
1745 : :
1746 : : // Never wrapping additions.
1747 : 47584744 : if (!ovf1 && !ovf2)
1748 : : {
1749 : : // Positive op2 means lhs > op1.
1750 : 27741438 : if (wi::gt_p (op2.lower_bound (), wi::zero (prec), sign))
1751 : : return VREL_GT;
1752 : 10337811 : if (wi::ge_p (op2.lower_bound (), wi::zero (prec), sign))
1753 : : return VREL_GE;
1754 : :
1755 : : // Negative op2 means lhs < op1.
1756 : 8462994 : if (wi::lt_p (op2.upper_bound (), wi::zero (prec), sign))
1757 : : return VREL_LT;
1758 : 5193398 : if (wi::le_p (op2.upper_bound (), wi::zero (prec), sign))
1759 : : return VREL_LE;
1760 : : }
1761 : : // Always wrapping additions.
1762 : 19843684 : else if (ovf1 && ovf1 == ovf2)
1763 : : {
1764 : : // Positive op2 means lhs < op1.
1765 : 798078 : if (wi::gt_p (op2.lower_bound (), wi::zero (prec), sign))
1766 : : return VREL_LT;
1767 : 20 : if (wi::ge_p (op2.lower_bound (), wi::zero (prec), sign))
1768 : : return VREL_LE;
1769 : :
1770 : : // Negative op2 means lhs > op1.
1771 : 20 : if (wi::lt_p (op2.upper_bound (), wi::zero (prec), sign))
1772 : : return VREL_GT;
1773 : 0 : if (wi::le_p (op2.upper_bound (), wi::zero (prec), sign))
1774 : : return VREL_GE;
1775 : : }
1776 : :
1777 : : // If op2 does not contain 0, then LHS and OP1 can never be equal.
1778 : 24226726 : if (!range_includes_zero_p (op2))
1779 : : return VREL_NE;
1780 : :
1781 : : return VREL_VARYING;
1782 : : }
1783 : :
1784 : : // PLUS is symmetrical, so we can simply call lhs_op1_relation with reversed
1785 : : // operands.
1786 : :
1787 : : relation_kind
1788 : 8193748 : operator_plus::lhs_op2_relation (const irange &lhs, const irange &op1,
1789 : : const irange &op2, relation_kind rel) const
1790 : : {
1791 : 8193748 : return lhs_op1_relation (lhs, op2, op1, rel);
1792 : : }
1793 : :
1794 : : void
1795 : 76204593 : operator_plus::wi_fold (irange &r, tree type,
1796 : : const wide_int &lh_lb, const wide_int &lh_ub,
1797 : : const wide_int &rh_lb, const wide_int &rh_ub) const
1798 : : {
1799 : 76204593 : wi::overflow_type ov_lb, ov_ub;
1800 : 76204593 : signop s = TYPE_SIGN (type);
1801 : 76204593 : wide_int new_lb = wi::add (lh_lb, rh_lb, s, &ov_lb);
1802 : 76204593 : wide_int new_ub = wi::add (lh_ub, rh_ub, s, &ov_ub);
1803 : 76204593 : value_range_with_overflow (r, type, new_lb, new_ub, ov_lb, ov_ub);
1804 : 76204593 : }
1805 : :
1806 : : // Given addition or subtraction, determine the possible NORMAL ranges and
1807 : : // OVERFLOW ranges given an OFFSET range. ADD_P is true for addition.
1808 : : // Return the relation that exists between the LHS and OP1 in order for the
1809 : : // NORMAL range to apply.
1810 : : // a return value of VREL_VARYING means no ranges were applicable.
1811 : :
1812 : : static relation_kind
1813 : 768465 : plus_minus_ranges (irange &r_ov, irange &r_normal, const irange &offset,
1814 : : bool add_p)
1815 : : {
1816 : 768465 : relation_kind kind = VREL_VARYING;
1817 : : // For now, only deal with constant adds. This could be extended to ranges
1818 : : // when someone is so motivated.
1819 : 768465 : if (!offset.singleton_p () || offset.zero_p ())
1820 : 751549 : return kind;
1821 : :
1822 : : // Always work with a positive offset. ie a+ -2 -> a-2 and a- -2 > a+2
1823 : 16916 : wide_int off = offset.lower_bound ();
1824 : 16916 : if (wi::neg_p (off, SIGNED))
1825 : : {
1826 : 1190 : add_p = !add_p;
1827 : 1190 : off = wi::neg (off);
1828 : : }
1829 : :
1830 : 16916 : wi::overflow_type ov;
1831 : 16916 : tree type = offset.type ();
1832 : 16916 : unsigned prec = TYPE_PRECISION (type);
1833 : 16916 : wide_int ub;
1834 : 16916 : wide_int lb;
1835 : : // calculate the normal range and relation for the operation.
1836 : 16916 : if (add_p)
1837 : : {
1838 : : // [ 0 , INF - OFF]
1839 : 15726 : lb = wi::zero (prec);
1840 : 15726 : ub = wi::sub (irange_val_max (type), off, UNSIGNED, &ov);
1841 : 15726 : kind = VREL_GT;
1842 : : }
1843 : : else
1844 : : {
1845 : : // [ OFF, INF ]
1846 : 1190 : lb = off;
1847 : 1190 : ub = irange_val_max (type);
1848 : 1190 : kind = VREL_LT;
1849 : : }
1850 : 16916 : int_range<2> normal_range (type, lb, ub);
1851 : 16916 : int_range<2> ov_range (type, lb, ub, VR_ANTI_RANGE);
1852 : :
1853 : 16916 : r_ov = ov_range;
1854 : 16916 : r_normal = normal_range;
1855 : 16916 : return kind;
1856 : 16916 : }
1857 : :
1858 : : // Once op1 has been calculated by operator_plus or operator_minus, check
1859 : : // to see if the relation passed causes any part of the calculation to
1860 : : // be not possible. ie
1861 : : // a_2 = b_3 + 1 with a_2 < b_3 can refine the range of b_3 to [INF, INF]
1862 : : // and that further refines a_2 to [0, 0].
1863 : : // R is the value of op1, OP2 is the offset being added/subtracted, REL is the
1864 : : // relation between LHS relation OP1 and ADD_P is true for PLUS, false for
1865 : : // MINUS. IF any adjustment can be made, R will reflect it.
1866 : :
1867 : : static void
1868 : 10872300 : adjust_op1_for_overflow (irange &r, const irange &op2, relation_kind rel,
1869 : : bool add_p)
1870 : : {
1871 : 10872300 : if (r.undefined_p ())
1872 : : return;
1873 : 10872298 : tree type = r.type ();
1874 : : // Check for unsigned overflow and calculate the overflow part.
1875 : 10872298 : signop s = TYPE_SIGN (type);
1876 : 10872298 : if (!TYPE_OVERFLOW_WRAPS (type) || s == SIGNED)
1877 : : return;
1878 : :
1879 : : // Only work with <, <=, >, >= relations.
1880 : 5701241 : if (!relation_lt_le_gt_ge_p (rel))
1881 : : return;
1882 : :
1883 : : // Get the ranges for this offset.
1884 : 768465 : int_range_max normal, overflow;
1885 : 768465 : relation_kind k = plus_minus_ranges (overflow, normal, op2, add_p);
1886 : :
1887 : : // VREL_VARYING means there are no adjustments.
1888 : 768465 : if (k == VREL_VARYING)
1889 : : return;
1890 : :
1891 : : // If the relations match use the normal range, otherwise use overflow range.
1892 : 16916 : if (relation_intersect (k, rel) == k)
1893 : 12065 : r.intersect (normal);
1894 : : else
1895 : 4851 : r.intersect (overflow);
1896 : : return;
1897 : 768465 : }
1898 : :
1899 : : bool
1900 : 9761010 : operator_plus::op1_range (irange &r, tree type,
1901 : : const irange &lhs,
1902 : : const irange &op2,
1903 : : relation_trio trio) const
1904 : : {
1905 : 9761010 : if (lhs.undefined_p ())
1906 : : return false;
1907 : : // Start with the default operation.
1908 : 9761010 : range_op_handler minus (MINUS_EXPR);
1909 : 9761010 : if (!minus)
1910 : : return false;
1911 : 9761010 : bool res = minus.fold_range (r, type, lhs, op2);
1912 : 9761010 : relation_kind rel = trio.lhs_op1 ();
1913 : : // Check for a relation refinement.
1914 : 9761010 : if (res)
1915 : 9761010 : adjust_op1_for_overflow (r, op2, rel, true /* PLUS_EXPR */);
1916 : : return res;
1917 : : }
1918 : :
1919 : : bool
1920 : 2079826 : operator_plus::op2_range (irange &r, tree type,
1921 : : const irange &lhs,
1922 : : const irange &op1,
1923 : : relation_trio rel) const
1924 : : {
1925 : 2079826 : return op1_range (r, type, lhs, op1, rel.swap_op1_op2 ());
1926 : : }
1927 : :
1928 : : class operator_widen_plus_signed : public range_operator
1929 : : {
1930 : : public:
1931 : : virtual void wi_fold (irange &r, tree type,
1932 : : const wide_int &lh_lb,
1933 : : const wide_int &lh_ub,
1934 : : const wide_int &rh_lb,
1935 : : const wide_int &rh_ub) const;
1936 : : } op_widen_plus_signed;
1937 : :
1938 : : void
1939 : 0 : operator_widen_plus_signed::wi_fold (irange &r, tree type,
1940 : : const wide_int &lh_lb,
1941 : : const wide_int &lh_ub,
1942 : : const wide_int &rh_lb,
1943 : : const wide_int &rh_ub) const
1944 : : {
1945 : 0 : wi::overflow_type ov_lb, ov_ub;
1946 : 0 : signop s = TYPE_SIGN (type);
1947 : :
1948 : 0 : wide_int lh_wlb
1949 : 0 : = wide_int::from (lh_lb, wi::get_precision (lh_lb) * 2, SIGNED);
1950 : 0 : wide_int lh_wub
1951 : 0 : = wide_int::from (lh_ub, wi::get_precision (lh_ub) * 2, SIGNED);
1952 : 0 : wide_int rh_wlb = wide_int::from (rh_lb, wi::get_precision (rh_lb) * 2, s);
1953 : 0 : wide_int rh_wub = wide_int::from (rh_ub, wi::get_precision (rh_ub) * 2, s);
1954 : :
1955 : 0 : wide_int new_lb = wi::add (lh_wlb, rh_wlb, s, &ov_lb);
1956 : 0 : wide_int new_ub = wi::add (lh_wub, rh_wub, s, &ov_ub);
1957 : :
1958 : 0 : r = int_range<2> (type, new_lb, new_ub);
1959 : 0 : }
1960 : :
1961 : : class operator_widen_plus_unsigned : public range_operator
1962 : : {
1963 : : public:
1964 : : virtual void wi_fold (irange &r, tree type,
1965 : : const wide_int &lh_lb,
1966 : : const wide_int &lh_ub,
1967 : : const wide_int &rh_lb,
1968 : : const wide_int &rh_ub) const;
1969 : : } op_widen_plus_unsigned;
1970 : :
1971 : : void
1972 : 0 : operator_widen_plus_unsigned::wi_fold (irange &r, tree type,
1973 : : const wide_int &lh_lb,
1974 : : const wide_int &lh_ub,
1975 : : const wide_int &rh_lb,
1976 : : const wide_int &rh_ub) const
1977 : : {
1978 : 0 : wi::overflow_type ov_lb, ov_ub;
1979 : 0 : signop s = TYPE_SIGN (type);
1980 : :
1981 : 0 : wide_int lh_wlb
1982 : 0 : = wide_int::from (lh_lb, wi::get_precision (lh_lb) * 2, UNSIGNED);
1983 : 0 : wide_int lh_wub
1984 : 0 : = wide_int::from (lh_ub, wi::get_precision (lh_ub) * 2, UNSIGNED);
1985 : 0 : wide_int rh_wlb = wide_int::from (rh_lb, wi::get_precision (rh_lb) * 2, s);
1986 : 0 : wide_int rh_wub = wide_int::from (rh_ub, wi::get_precision (rh_ub) * 2, s);
1987 : :
1988 : 0 : wide_int new_lb = wi::add (lh_wlb, rh_wlb, s, &ov_lb);
1989 : 0 : wide_int new_ub = wi::add (lh_wub, rh_wub, s, &ov_ub);
1990 : :
1991 : 0 : r = int_range<2> (type, new_lb, new_ub);
1992 : 0 : }
1993 : :
1994 : : void
1995 : 18633679 : operator_minus::update_bitmask (irange &r, const irange &lh,
1996 : : const irange &rh) const
1997 : : {
1998 : 18633679 : update_known_bitmask (r, MINUS_EXPR, lh, rh);
1999 : 18633679 : }
2000 : :
2001 : : void
2002 : 26426562 : operator_minus::wi_fold (irange &r, tree type,
2003 : : const wide_int &lh_lb, const wide_int &lh_ub,
2004 : : const wide_int &rh_lb, const wide_int &rh_ub) const
2005 : : {
2006 : 26426562 : wi::overflow_type ov_lb, ov_ub;
2007 : 26426562 : signop s = TYPE_SIGN (type);
2008 : 26426562 : wide_int new_lb = wi::sub (lh_lb, rh_ub, s, &ov_lb);
2009 : 26426562 : wide_int new_ub = wi::sub (lh_ub, rh_lb, s, &ov_ub);
2010 : 26426562 : value_range_with_overflow (r, type, new_lb, new_ub, ov_lb, ov_ub);
2011 : 26426562 : }
2012 : :
2013 : :
2014 : : // Return the relation between LHS and OP1 based on the relation between
2015 : : // OP1 and OP2.
2016 : :
2017 : : relation_kind
2018 : 4788735 : operator_minus::lhs_op1_relation (const irange &, const irange &op1,
2019 : : const irange &, relation_kind rel) const
2020 : : {
2021 : 4788735 : if (!op1.undefined_p () && TYPE_SIGN (op1.type ()) == UNSIGNED)
2022 : 2430542 : switch (rel)
2023 : : {
2024 : : case VREL_GT:
2025 : : case VREL_GE:
2026 : : return VREL_LE;
2027 : : default:
2028 : : break;
2029 : : }
2030 : : return VREL_VARYING;
2031 : : }
2032 : :
2033 : : // Check to see if the relation REL between OP1 and OP2 has any effect on the
2034 : : // LHS of the expression. If so, apply it to LHS_RANGE. This is a helper
2035 : : // function for both MINUS_EXPR and POINTER_DIFF_EXPR.
2036 : :
2037 : : bool
2038 : 21315090 : minus_op1_op2_relation_effect (irange &lhs_range, tree type,
2039 : : const irange &op1_range ATTRIBUTE_UNUSED,
2040 : : const irange &op2_range ATTRIBUTE_UNUSED,
2041 : : relation_kind rel)
2042 : : {
2043 : 21315090 : if (rel == VREL_VARYING)
2044 : : return false;
2045 : :
2046 : 251265 : int_range<2> rel_range;
2047 : 251265 : unsigned prec = TYPE_PRECISION (type);
2048 : 251265 : signop sgn = TYPE_SIGN (type);
2049 : :
2050 : : // == and != produce [0,0] and ~[0,0] regardless of wrapping.
2051 : 251265 : if (rel == VREL_EQ)
2052 : 7823 : rel_range = int_range<2> (type, wi::zero (prec), wi::zero (prec));
2053 : 243442 : else if (rel == VREL_NE)
2054 : 99290 : rel_range = int_range<2> (type, wi::zero (prec), wi::zero (prec),
2055 : 49645 : VR_ANTI_RANGE);
2056 : 193797 : else if (TYPE_OVERFLOW_WRAPS (type))
2057 : : {
2058 : 125556 : switch (rel)
2059 : : {
2060 : : // For wrapping signed values and unsigned, if op1 > op2 or
2061 : : // op1 < op2, then op1 - op2 can be restricted to ~[0, 0].
2062 : 46359 : case VREL_GT:
2063 : 46359 : case VREL_LT:
2064 : 92718 : rel_range = int_range<2> (type, wi::zero (prec), wi::zero (prec),
2065 : 46359 : VR_ANTI_RANGE);
2066 : 46359 : break;
2067 : : default:
2068 : : return false;
2069 : : }
2070 : : }
2071 : : else
2072 : : {
2073 : 68241 : switch (rel)
2074 : : {
2075 : : // op1 > op2, op1 - op2 can be restricted to [1, +INF]
2076 : 21698 : case VREL_GT:
2077 : 43396 : rel_range = int_range<2> (type, wi::one (prec),
2078 : 43396 : wi::max_value (prec, sgn));
2079 : 21698 : break;
2080 : : // op1 >= op2, op1 - op2 can be restricted to [0, +INF]
2081 : 45810 : case VREL_GE:
2082 : 91620 : rel_range = int_range<2> (type, wi::zero (prec),
2083 : 91620 : wi::max_value (prec, sgn));
2084 : 45810 : break;
2085 : : // op1 < op2, op1 - op2 can be restricted to [-INF, -1]
2086 : 287 : case VREL_LT:
2087 : 574 : rel_range = int_range<2> (type, wi::min_value (prec, sgn),
2088 : 287 : wi::minus_one (prec));
2089 : 287 : break;
2090 : : // op1 <= op2, op1 - op2 can be restricted to [-INF, 0]
2091 : 280 : case VREL_LE:
2092 : 560 : rel_range = int_range<2> (type, wi::min_value (prec, sgn),
2093 : 280 : wi::zero (prec));
2094 : 280 : break;
2095 : : default:
2096 : : return false;
2097 : : }
2098 : : }
2099 : 171902 : lhs_range.intersect (rel_range);
2100 : 171902 : return true;
2101 : 251265 : }
2102 : :
2103 : : bool
2104 : 18633679 : operator_minus::op1_op2_relation_effect (irange &lhs_range, tree type,
2105 : : const irange &op1_range,
2106 : : const irange &op2_range,
2107 : : relation_kind rel) const
2108 : : {
2109 : 18633679 : return minus_op1_op2_relation_effect (lhs_range, type, op1_range, op2_range,
2110 : 18633679 : rel);
2111 : : }
2112 : :
2113 : : bool
2114 : 1111290 : operator_minus::op1_range (irange &r, tree type,
2115 : : const irange &lhs,
2116 : : const irange &op2,
2117 : : relation_trio trio) const
2118 : : {
2119 : 1111290 : if (lhs.undefined_p ())
2120 : : return false;
2121 : : // Start with the default operation.
2122 : 1111290 : range_op_handler minus (PLUS_EXPR);
2123 : 1111290 : if (!minus)
2124 : : return false;
2125 : 1111290 : bool res = minus.fold_range (r, type, lhs, op2);
2126 : 1111290 : relation_kind rel = trio.lhs_op1 ();
2127 : 1111290 : if (res)
2128 : 1111290 : adjust_op1_for_overflow (r, op2, rel, false /* PLUS_EXPR */);
2129 : : return res;
2130 : :
2131 : : }
2132 : :
2133 : : bool
2134 : 1741454 : operator_minus::op2_range (irange &r, tree type,
2135 : : const irange &lhs,
2136 : : const irange &op1,
2137 : : relation_trio) const
2138 : : {
2139 : 1741454 : if (lhs.undefined_p ())
2140 : : return false;
2141 : 1741454 : return fold_range (r, type, op1, lhs);
2142 : : }
2143 : :
2144 : : void
2145 : 912505 : operator_min::update_bitmask (irange &r, const irange &lh,
2146 : : const irange &rh) const
2147 : : {
2148 : 912505 : update_known_bitmask (r, MIN_EXPR, lh, rh);
2149 : 912505 : }
2150 : :
2151 : : void
2152 : 1423993 : operator_min::wi_fold (irange &r, tree type,
2153 : : const wide_int &lh_lb, const wide_int &lh_ub,
2154 : : const wide_int &rh_lb, const wide_int &rh_ub) const
2155 : : {
2156 : 1423993 : signop s = TYPE_SIGN (type);
2157 : 1423993 : wide_int new_lb = wi::min (lh_lb, rh_lb, s);
2158 : 1423993 : wide_int new_ub = wi::min (lh_ub, rh_ub, s);
2159 : 1423993 : value_range_with_overflow (r, type, new_lb, new_ub);
2160 : 1423993 : }
2161 : :
2162 : :
2163 : : void
2164 : 698940 : operator_max::update_bitmask (irange &r, const irange &lh,
2165 : : const irange &rh) const
2166 : : {
2167 : 698940 : update_known_bitmask (r, MAX_EXPR, lh, rh);
2168 : 698940 : }
2169 : :
2170 : : void
2171 : 829480 : operator_max::wi_fold (irange &r, tree type,
2172 : : const wide_int &lh_lb, const wide_int &lh_ub,
2173 : : const wide_int &rh_lb, const wide_int &rh_ub) const
2174 : : {
2175 : 829480 : signop s = TYPE_SIGN (type);
2176 : 829480 : wide_int new_lb = wi::max (lh_lb, rh_lb, s);
2177 : 829480 : wide_int new_ub = wi::max (lh_ub, rh_ub, s);
2178 : 829480 : value_range_with_overflow (r, type, new_lb, new_ub);
2179 : 829480 : }
2180 : :
2181 : :
2182 : : // Calculate the cross product of two sets of ranges and return it.
2183 : : //
2184 : : // Multiplications, divisions and shifts are a bit tricky to handle,
2185 : : // depending on the mix of signs we have in the two ranges, we need to
2186 : : // operate on different values to get the minimum and maximum values
2187 : : // for the new range. One approach is to figure out all the
2188 : : // variations of range combinations and do the operations.
2189 : : //
2190 : : // However, this involves several calls to compare_values and it is
2191 : : // pretty convoluted. It's simpler to do the 4 operations (MIN0 OP
2192 : : // MIN1, MIN0 OP MAX1, MAX0 OP MIN1 and MAX0 OP MAX0 OP MAX1) and then
2193 : : // figure the smallest and largest values to form the new range.
2194 : :
2195 : : void
2196 : 14350553 : cross_product_operator::wi_cross_product (irange &r, tree type,
2197 : : const wide_int &lh_lb,
2198 : : const wide_int &lh_ub,
2199 : : const wide_int &rh_lb,
2200 : : const wide_int &rh_ub) const
2201 : : {
2202 : 14350553 : wide_int cp1, cp2, cp3, cp4;
2203 : : // Default to varying.
2204 : 14350553 : r.set_varying (type);
2205 : :
2206 : : // Compute the 4 cross operations, bailing if we get an overflow we
2207 : : // can't handle.
2208 : 14350553 : if (wi_op_overflows (cp1, type, lh_lb, rh_lb))
2209 : : return;
2210 : 14350549 : if (wi::eq_p (lh_lb, lh_ub))
2211 : 4324144 : cp3 = cp1;
2212 : 10026405 : else if (wi_op_overflows (cp3, type, lh_ub, rh_lb))
2213 : : return;
2214 : 14350549 : if (wi::eq_p (rh_lb, rh_ub))
2215 : 11082077 : cp2 = cp1;
2216 : 3268472 : else if (wi_op_overflows (cp2, type, lh_lb, rh_ub))
2217 : : return;
2218 : 14348279 : if (wi::eq_p (lh_lb, lh_ub))
2219 : 4324124 : cp4 = cp2;
2220 : 10024155 : else if (wi_op_overflows (cp4, type, lh_ub, rh_ub))
2221 : : return;
2222 : :
2223 : : // Order pairs.
2224 : 14348279 : signop sign = TYPE_SIGN (type);
2225 : 14348279 : if (wi::gt_p (cp1, cp2, sign))
2226 : 1498545 : std::swap (cp1, cp2);
2227 : 14348279 : if (wi::gt_p (cp3, cp4, sign))
2228 : 1471076 : std::swap (cp3, cp4);
2229 : :
2230 : : // Choose min and max from the ordered pairs.
2231 : 14348279 : wide_int res_lb = wi::min (cp1, cp3, sign);
2232 : 14348279 : wide_int res_ub = wi::max (cp2, cp4, sign);
2233 : 14348279 : value_range_with_overflow (r, type, res_lb, res_ub);
2234 : 14352103 : }
2235 : :
2236 : :
2237 : : void
2238 : 13885939 : operator_mult::update_bitmask (irange &r, const irange &lh,
2239 : : const irange &rh) const
2240 : : {
2241 : 13885939 : update_known_bitmask (r, MULT_EXPR, lh, rh);
2242 : 13885939 : }
2243 : :
2244 : : bool
2245 : 1266480 : operator_mult::op1_range (irange &r, tree type,
2246 : : const irange &lhs, const irange &op2,
2247 : : relation_trio) const
2248 : : {
2249 : 1266480 : if (lhs.undefined_p ())
2250 : : return false;
2251 : :
2252 : : // We can't solve 0 = OP1 * N by dividing by N with a wrapping type.
2253 : : // For example: For 0 = OP1 * 2, OP1 could be 0, or MAXINT, whereas
2254 : : // for 4 = OP1 * 2, OP1 could be 2 or 130 (unsigned 8-bit)
2255 : 1266480 : if (TYPE_OVERFLOW_WRAPS (type))
2256 : : return false;
2257 : :
2258 : 337045 : wide_int offset;
2259 : 337045 : if (op2.singleton_p (offset) && offset != 0)
2260 : 230119 : return range_op_handler (TRUNC_DIV_EXPR).fold_range (r, type, lhs, op2);
2261 : :
2262 : : // ~[0, 0] = op1 * op2 defines op1 and op2 as non-zero.
2263 : 106926 : if (!lhs.contains_p (wi::zero (TYPE_PRECISION (lhs.type ()))))
2264 : : {
2265 : 21277 : r.set_nonzero (type);
2266 : 21277 : return true;
2267 : : }
2268 : : return false;
2269 : 337045 : }
2270 : :
2271 : : bool
2272 : 129897 : operator_mult::op2_range (irange &r, tree type,
2273 : : const irange &lhs, const irange &op1,
2274 : : relation_trio rel) const
2275 : : {
2276 : 129897 : return operator_mult::op1_range (r, type, lhs, op1, rel.swap_op1_op2 ());
2277 : : }
2278 : :
2279 : : bool
2280 : 13913629 : operator_mult::wi_op_overflows (wide_int &res, tree type,
2281 : : const wide_int &w0, const wide_int &w1) const
2282 : : {
2283 : 13913629 : wi::overflow_type overflow = wi::OVF_NONE;
2284 : 13913629 : signop sign = TYPE_SIGN (type);
2285 : 13913629 : res = wi::mul (w0, w1, sign, &overflow);
2286 : 13913629 : if (overflow && TYPE_OVERFLOW_UNDEFINED (type))
2287 : : {
2288 : : // For multiplication, the sign of the overflow is given
2289 : : // by the comparison of the signs of the operands.
2290 : 6637727 : if (sign == UNSIGNED || w0.sign_mask () == w1.sign_mask ())
2291 : 3658316 : res = wi::max_value (w0.get_precision (), sign);
2292 : : else
2293 : 2979591 : res = wi::min_value (w0.get_precision (), sign);
2294 : 6637727 : return false;
2295 : : }
2296 : 7275902 : return overflow;
2297 : : }
2298 : :
2299 : : void
2300 : 17589646 : operator_mult::wi_fold (irange &r, tree type,
2301 : : const wide_int &lh_lb, const wide_int &lh_ub,
2302 : : const wide_int &rh_lb, const wide_int &rh_ub) const
2303 : : {
2304 : 17589646 : if (TYPE_OVERFLOW_UNDEFINED (type))
2305 : : {
2306 : 5822876 : wi_cross_product (r, type, lh_lb, lh_ub, rh_lb, rh_ub);
2307 : 5822876 : return;
2308 : : }
2309 : :
2310 : : // Multiply the ranges when overflow wraps. This is basically fancy
2311 : : // code so we don't drop to varying with an unsigned
2312 : : // [-3,-1]*[-3,-1].
2313 : : //
2314 : : // This test requires 2*prec bits if both operands are signed and
2315 : : // 2*prec + 2 bits if either is not. Therefore, extend the values
2316 : : // using the sign of the result to PREC2. From here on out,
2317 : : // everything is just signed math no matter what the input types
2318 : : // were.
2319 : :
2320 : 11766770 : signop sign = TYPE_SIGN (type);
2321 : 11766770 : unsigned prec = TYPE_PRECISION (type);
2322 : 11766770 : widest2_int min0 = widest2_int::from (lh_lb, sign);
2323 : 11766770 : widest2_int max0 = widest2_int::from (lh_ub, sign);
2324 : 11766770 : widest2_int min1 = widest2_int::from (rh_lb, sign);
2325 : 11766770 : widest2_int max1 = widest2_int::from (rh_ub, sign);
2326 : 11766770 : widest2_int sizem1 = wi::mask <widest2_int> (prec, false);
2327 : 11766770 : widest2_int size = sizem1 + 1;
2328 : :
2329 : : // Canonicalize the intervals.
2330 : 11766770 : if (sign == UNSIGNED)
2331 : : {
2332 : 11066153 : if (wi::ltu_p (size, min0 + max0))
2333 : : {
2334 : 1559048 : min0 -= size;
2335 : 1559048 : max0 -= size;
2336 : : }
2337 : 11066121 : if (wi::ltu_p (size, min1 + max1))
2338 : : {
2339 : 156434 : min1 -= size;
2340 : 156434 : max1 -= size;
2341 : : }
2342 : : }
2343 : :
2344 : : // Sort the 4 products so that min is in prod0 and max is in
2345 : : // prod3.
2346 : 11766770 : widest2_int prod0 = min0 * min1;
2347 : 11766770 : widest2_int prod1 = min0 * max1;
2348 : 11766770 : widest2_int prod2 = max0 * min1;
2349 : 11766770 : widest2_int prod3 = max0 * max1;
2350 : :
2351 : : // min0min1 > max0max1
2352 : 11766770 : if (prod0 > prod3)
2353 : 188040 : std::swap (prod0, prod3);
2354 : :
2355 : : // min0max1 > max0min1
2356 : 11766770 : if (prod1 > prod2)
2357 : 299241 : std::swap (prod1, prod2);
2358 : :
2359 : 11766770 : if (prod0 > prod1)
2360 : 79772 : std::swap (prod0, prod1);
2361 : :
2362 : 11766770 : if (prod2 > prod3)
2363 : 3936 : std::swap (prod2, prod3);
2364 : :
2365 : : // diff = max - min
2366 : 11766770 : prod2 = prod3 - prod0;
2367 : 11766770 : if (wi::geu_p (prod2, sizem1))
2368 : : {
2369 : : // Multiplying by X, where X is a power of 2 is [0,0][X,+INF].
2370 : 7742576 : if (TYPE_UNSIGNED (type) && rh_lb == rh_ub
2371 : 7147904 : && wi::exact_log2 (rh_lb) != -1 && prec > 1)
2372 : : {
2373 : 2676409 : r.set (type, rh_lb, wi::max_value (prec, sign));
2374 : 2676409 : int_range<2> zero;
2375 : 2676409 : zero.set_zero (type);
2376 : 2676409 : r.union_ (zero);
2377 : 2676409 : }
2378 : : else
2379 : : // The range covers all values.
2380 : 1264955 : r.set_varying (type);
2381 : : }
2382 : : else
2383 : : {
2384 : 7825406 : wide_int new_lb = wide_int::from (prod0, prec, sign);
2385 : 7825406 : wide_int new_ub = wide_int::from (prod3, prec, sign);
2386 : 7825406 : create_possibly_reversed_range (r, type, new_lb, new_ub);
2387 : 7825449 : }
2388 : 11767370 : }
2389 : :
2390 : : class operator_widen_mult_signed : public range_operator
2391 : : {
2392 : : public:
2393 : : virtual void wi_fold (irange &r, tree type,
2394 : : const wide_int &lh_lb,
2395 : : const wide_int &lh_ub,
2396 : : const wide_int &rh_lb,
2397 : : const wide_int &rh_ub)
2398 : : const;
2399 : : } op_widen_mult_signed;
2400 : :
2401 : : void
2402 : 1347 : operator_widen_mult_signed::wi_fold (irange &r, tree type,
2403 : : const wide_int &lh_lb,
2404 : : const wide_int &lh_ub,
2405 : : const wide_int &rh_lb,
2406 : : const wide_int &rh_ub) const
2407 : : {
2408 : 1347 : wide_int lh_wlb = wide_int::from (lh_lb, TYPE_PRECISION (type), SIGNED);
2409 : 1347 : wide_int lh_wub = wide_int::from (lh_ub, TYPE_PRECISION (type), SIGNED);
2410 : 1347 : wide_int rh_wlb = wide_int::from (rh_lb, TYPE_PRECISION (type), SIGNED);
2411 : 1347 : wide_int rh_wub = wide_int::from (rh_ub, TYPE_PRECISION (type), SIGNED);
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 : 1347 : return op_mult.wi_fold (r, type, lh_wlb, lh_wub, rh_wlb, rh_wub);
2417 : 1347 : }
2418 : :
2419 : : class operator_widen_mult_unsigned : public range_operator
2420 : : {
2421 : : public:
2422 : : virtual void wi_fold (irange &r, tree type,
2423 : : const wide_int &lh_lb,
2424 : : const wide_int &lh_ub,
2425 : : const wide_int &rh_lb,
2426 : : const wide_int &rh_ub)
2427 : : const;
2428 : : } op_widen_mult_unsigned;
2429 : :
2430 : : void
2431 : 5540 : operator_widen_mult_unsigned::wi_fold (irange &r, tree type,
2432 : : const wide_int &lh_lb,
2433 : : const wide_int &lh_ub,
2434 : : const wide_int &rh_lb,
2435 : : const wide_int &rh_ub) const
2436 : : {
2437 : 5540 : wide_int lh_wlb = wide_int::from (lh_lb, TYPE_PRECISION (type), UNSIGNED);
2438 : 5540 : wide_int lh_wub = wide_int::from (lh_ub, TYPE_PRECISION (type), UNSIGNED);
2439 : 5540 : wide_int rh_wlb = wide_int::from (rh_lb, TYPE_PRECISION (type), UNSIGNED);
2440 : 5540 : wide_int rh_wub = wide_int::from (rh_ub, TYPE_PRECISION (type), UNSIGNED);
2441 : :
2442 : : /* We don't expect a widening multiplication to be able to overflow but range
2443 : : calculations for multiplications are complicated. After widening the
2444 : : operands lets call the base class. */
2445 : 5540 : return op_mult.wi_fold (r, type, lh_wlb, lh_wub, rh_wlb, rh_wub);
2446 : 5540 : }
2447 : :
2448 : : class operator_widen_mult_signed_unsigned : public range_operator
2449 : : {
2450 : : public:
2451 : : virtual void wi_fold (irange &r, tree type,
2452 : : const wide_int &lh_lb,
2453 : : const wide_int &lh_ub,
2454 : : const wide_int &rh_lb,
2455 : : const wide_int &rh_ub)
2456 : : const;
2457 : : } op_widen_mult_signed_unsigned;
2458 : :
2459 : : void
2460 : 0 : operator_widen_mult_signed_unsigned::wi_fold (irange &r, tree type,
2461 : : const wide_int &lh_lb,
2462 : : const wide_int &lh_ub,
2463 : : const wide_int &rh_lb,
2464 : : const wide_int &rh_ub) const
2465 : : {
2466 : 0 : wide_int lh_wlb = wide_int::from (lh_lb, TYPE_PRECISION (type), SIGNED);
2467 : 0 : wide_int lh_wub = wide_int::from (lh_ub, TYPE_PRECISION (type), SIGNED);
2468 : 0 : wide_int rh_wlb = wide_int::from (rh_lb, TYPE_PRECISION (type), UNSIGNED);
2469 : 0 : wide_int rh_wub = wide_int::from (rh_ub, TYPE_PRECISION (type), UNSIGNED);
2470 : :
2471 : : /* We don't expect a widening multiplication to be able to overflow but range
2472 : : calculations for multiplications are complicated. After widening the
2473 : : operands lets call the base class. */
2474 : 0 : return op_mult.wi_fold (r, type, lh_wlb, lh_wub, rh_wlb, rh_wub);
2475 : 0 : }
2476 : :
2477 : : class operator_div : public cross_product_operator
2478 : : {
2479 : : using range_operator::update_bitmask;
2480 : : using range_operator::op2_range;
2481 : : public:
2482 : : operator_div (tree_code div_kind) { m_code = div_kind; }
2483 : : bool op2_range (irange &r, tree type, const irange &lhs, const irange &,
2484 : : relation_trio) const final override;
2485 : : virtual void wi_fold (irange &r, tree type,
2486 : : const wide_int &lh_lb,
2487 : : const wide_int &lh_ub,
2488 : : const wide_int &rh_lb,
2489 : : const wide_int &rh_ub) const final override;
2490 : : virtual bool wi_op_overflows (wide_int &res, tree type,
2491 : : const wide_int &, const wide_int &)
2492 : : const final override;
2493 : 2845836 : void update_bitmask (irange &r, const irange &lh, const irange &rh)
2494 : : const final override
2495 : 2845836 : { update_known_bitmask (r, m_code, lh, rh); }
2496 : : protected:
2497 : : tree_code m_code;
2498 : : };
2499 : :
2500 : : static operator_div op_trunc_div (TRUNC_DIV_EXPR);
2501 : : static operator_div op_floor_div (FLOOR_DIV_EXPR);
2502 : : static operator_div op_round_div (ROUND_DIV_EXPR);
2503 : : static operator_div op_ceil_div (CEIL_DIV_EXPR);
2504 : :
2505 : : // Set OP2 to non-zero if the LHS isn't UNDEFINED.
2506 : : bool
2507 : 36247 : operator_div::op2_range (irange &r, tree type, const irange &lhs,
2508 : : const irange &, relation_trio) const
2509 : : {
2510 : 36247 : if (!lhs.undefined_p ())
2511 : : {
2512 : 36247 : r.set_nonzero (type);
2513 : 36247 : return true;
2514 : : }
2515 : : return false;
2516 : : }
2517 : :
2518 : : bool
2519 : 12281956 : operator_div::wi_op_overflows (wide_int &res, tree type,
2520 : : const wide_int &w0, const wide_int &w1) const
2521 : : {
2522 : 12281956 : if (w1 == 0)
2523 : : return true;
2524 : :
2525 : 12281956 : wi::overflow_type overflow = wi::OVF_NONE;
2526 : 12281956 : signop sign = TYPE_SIGN (type);
2527 : :
2528 : 12281956 : switch (m_code)
2529 : : {
2530 : 12176990 : case EXACT_DIV_EXPR:
2531 : 12176990 : case TRUNC_DIV_EXPR:
2532 : 12176990 : res = wi::div_trunc (w0, w1, sign, &overflow);
2533 : 12176990 : break;
2534 : 93231 : case FLOOR_DIV_EXPR:
2535 : 93231 : res = wi::div_floor (w0, w1, sign, &overflow);
2536 : 93231 : break;
2537 : 288 : case ROUND_DIV_EXPR:
2538 : 288 : res = wi::div_round (w0, w1, sign, &overflow);
2539 : 288 : break;
2540 : 11447 : case CEIL_DIV_EXPR:
2541 : 11447 : res = wi::div_ceil (w0, w1, sign, &overflow);
2542 : 11447 : break;
2543 : 0 : default:
2544 : 0 : gcc_unreachable ();
2545 : : }
2546 : :
2547 : 12281956 : if (overflow && TYPE_OVERFLOW_UNDEFINED (type))
2548 : : {
2549 : : // For division, the only case is -INF / -1 = +INF.
2550 : 209313 : res = wi::max_value (w0.get_precision (), sign);
2551 : 209313 : return false;
2552 : : }
2553 : 12072643 : return overflow;
2554 : : }
2555 : :
2556 : : void
2557 : 3973390 : operator_div::wi_fold (irange &r, tree type,
2558 : : const wide_int &lh_lb, const wide_int &lh_ub,
2559 : : const wide_int &rh_lb, const wide_int &rh_ub) const
2560 : : {
2561 : 3973390 : const wide_int dividend_min = lh_lb;
2562 : 3973390 : const wide_int dividend_max = lh_ub;
2563 : 3973390 : const wide_int divisor_min = rh_lb;
2564 : 3973390 : const wide_int divisor_max = rh_ub;
2565 : 3973390 : signop sign = TYPE_SIGN (type);
2566 : 3973390 : unsigned prec = TYPE_PRECISION (type);
2567 : 3973390 : wide_int extra_min, extra_max;
2568 : :
2569 : : // If we know we won't divide by zero, just do the division.
2570 : 3973390 : if (!wi_includes_zero_p (type, divisor_min, divisor_max))
2571 : : {
2572 : 3286090 : wi_cross_product (r, type, dividend_min, dividend_max,
2573 : : divisor_min, divisor_max);
2574 : 3286090 : return;
2575 : : }
2576 : :
2577 : : // If we're definitely dividing by zero, there's nothing to do.
2578 : 687300 : if (wi_zero_p (type, divisor_min, divisor_max))
2579 : : {
2580 : 19090 : r.set_undefined ();
2581 : 19090 : return;
2582 : : }
2583 : :
2584 : : // Perform the division in 2 parts, [LB, -1] and [1, UB], which will
2585 : : // skip any division by zero.
2586 : :
2587 : : // First divide by the negative numbers, if any.
2588 : 668210 : if (wi::neg_p (divisor_min, sign))
2589 : 863638 : wi_cross_product (r, type, dividend_min, dividend_max,
2590 : 863638 : divisor_min, wi::minus_one (prec));
2591 : : else
2592 : 236391 : r.set_undefined ();
2593 : :
2594 : : // Then divide by the non-zero positive numbers, if any.
2595 : 668210 : if (wi::gt_p (divisor_max, wi::zero (prec), sign))
2596 : : {
2597 : 667454 : int_range_max tmp;
2598 : 1334908 : wi_cross_product (tmp, type, dividend_min, dividend_max,
2599 : 667454 : wi::one (prec), divisor_max);
2600 : 667454 : r.union_ (tmp);
2601 : 667454 : }
2602 : : // We shouldn't still have undefined here.
2603 : 668210 : gcc_checking_assert (!r.undefined_p ());
2604 : 3974042 : }
2605 : :
2606 : :
2607 : : class operator_exact_divide : public operator_div
2608 : : {
2609 : : using range_operator::op1_range;
2610 : : public:
2611 : : operator_exact_divide () : operator_div (EXACT_DIV_EXPR) { }
2612 : : virtual bool op1_range (irange &r, tree type,
2613 : : const irange &lhs,
2614 : : const irange &op2,
2615 : : relation_trio) const;
2616 : :
2617 : : } op_exact_div;
2618 : :
2619 : : bool
2620 : 515136 : operator_exact_divide::op1_range (irange &r, tree type,
2621 : : const irange &lhs,
2622 : : const irange &op2,
2623 : : relation_trio) const
2624 : : {
2625 : 515136 : if (lhs.undefined_p ())
2626 : : return false;
2627 : 515136 : wide_int offset;
2628 : : // [2, 4] = op1 / [3,3] since its exact divide, no need to worry about
2629 : : // remainders in the endpoints, so op1 = [2,4] * [3,3] = [6,12].
2630 : : // We wont bother trying to enumerate all the in between stuff :-P
2631 : : // TRUE accuracy is [6,6][9,9][12,12]. This is unlikely to matter most of
2632 : : // the time however.
2633 : : // If op2 is a multiple of 2, we would be able to set some non-zero bits.
2634 : 515136 : if (op2.singleton_p (offset) && offset != 0)
2635 : 515136 : return range_op_handler (MULT_EXPR).fold_range (r, type, lhs, op2);
2636 : : return false;
2637 : 515136 : }
2638 : :
2639 : :
2640 : : class operator_lshift : public cross_product_operator
2641 : : {
2642 : : using range_operator::fold_range;
2643 : : using range_operator::op1_range;
2644 : : using range_operator::update_bitmask;
2645 : : public:
2646 : : virtual bool op1_range (irange &r, tree type, const irange &lhs,
2647 : : const irange &op2, relation_trio rel = TRIO_VARYING)
2648 : : const final override;
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 : :
2653 : : virtual void wi_fold (irange &r, tree type,
2654 : : const wide_int &lh_lb, 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 &,
2660 : : const wide_int &) const final override;
2661 : 461588 : void update_bitmask (irange &r, const irange &lh,
2662 : : const irange &rh) const final override
2663 : 461588 : { update_known_bitmask (r, LSHIFT_EXPR, lh, rh); }
2664 : : // Check compatibility of LHS and op1.
2665 : 1074353 : bool operand_check_p (tree t1, tree t2, tree) const final override
2666 : 1074353 : { return range_compatible_p (t1, t2); }
2667 : : } op_lshift;
2668 : :
2669 : : class operator_rshift : public cross_product_operator
2670 : : {
2671 : : using range_operator::fold_range;
2672 : : using range_operator::op1_range;
2673 : : using range_operator::lhs_op1_relation;
2674 : : using range_operator::update_bitmask;
2675 : : public:
2676 : : virtual bool fold_range (irange &r, tree type, const irange &op1,
2677 : : const irange &op2, relation_trio rel = TRIO_VARYING)
2678 : : const final override;
2679 : : virtual void wi_fold (irange &r, tree type,
2680 : : const wide_int &lh_lb,
2681 : : const wide_int &lh_ub,
2682 : : const wide_int &rh_lb,
2683 : : const wide_int &rh_ub) const final override;
2684 : : virtual bool wi_op_overflows (wide_int &res,
2685 : : tree type,
2686 : : const wide_int &w0,
2687 : : const wide_int &w1) const final override;
2688 : : virtual bool op1_range (irange &, tree type, const irange &lhs,
2689 : : const irange &op2, relation_trio rel = TRIO_VARYING)
2690 : : const final override;
2691 : : virtual relation_kind lhs_op1_relation (const irange &lhs, const irange &op1,
2692 : : const irange &op2, relation_kind rel)
2693 : : const final override;
2694 : 3276147 : void update_bitmask (irange &r, const irange &lh,
2695 : : const irange &rh) const final override
2696 : 3276147 : { update_known_bitmask (r, RSHIFT_EXPR, lh, rh); }
2697 : : // Check compatibility of LHS and op1.
2698 : 3370614 : bool operand_check_p (tree t1, tree t2, tree) const final override
2699 : 3370614 : { return range_compatible_p (t1, t2); }
2700 : : } op_rshift;
2701 : :
2702 : :
2703 : : relation_kind
2704 : 2380631 : operator_rshift::lhs_op1_relation (const irange &lhs ATTRIBUTE_UNUSED,
2705 : : const irange &op1,
2706 : : const irange &op2,
2707 : : relation_kind) const
2708 : : {
2709 : : // If both operands range are >= 0, then the LHS <= op1.
2710 : 2380631 : if (!op1.undefined_p () && !op2.undefined_p ()
2711 : 4760496 : && wi::ge_p (op1.lower_bound (), 0, TYPE_SIGN (op1.type ()))
2712 : 6939340 : && wi::ge_p (op2.lower_bound (), 0, TYPE_SIGN (op2.type ())))
2713 : 2151943 : return VREL_LE;
2714 : : return VREL_VARYING;
2715 : : }
2716 : :
2717 : : bool
2718 : 1612525 : operator_lshift::fold_range (irange &r, tree type,
2719 : : const irange &op1,
2720 : : const irange &op2,
2721 : : relation_trio rel) const
2722 : : {
2723 : 1612525 : int_range_max shift_range;
2724 : 1612525 : if (!get_shift_range (shift_range, type, op2))
2725 : : {
2726 : 575 : if (op2.undefined_p ())
2727 : 230 : r.set_undefined ();
2728 : : else
2729 : 345 : r.set_zero (type);
2730 : 575 : return true;
2731 : : }
2732 : :
2733 : : // Transform left shifts by constants into multiplies.
2734 : 1611950 : if (shift_range.singleton_p ())
2735 : : {
2736 : 1150335 : unsigned shift = shift_range.lower_bound ().to_uhwi ();
2737 : 1150335 : wide_int tmp = wi::set_bit_in_zero (shift, TYPE_PRECISION (type));
2738 : 1150335 : int_range<1> mult (type, tmp, tmp);
2739 : :
2740 : : // Force wrapping multiplication.
2741 : 1150335 : bool saved_flag_wrapv = flag_wrapv;
2742 : 1150335 : bool saved_flag_wrapv_pointer = flag_wrapv_pointer;
2743 : 1150335 : flag_wrapv = 1;
2744 : 1150335 : flag_wrapv_pointer = 1;
2745 : 1150335 : bool b = op_mult.fold_range (r, type, op1, mult);
2746 : 1150335 : flag_wrapv = saved_flag_wrapv;
2747 : 1150335 : flag_wrapv_pointer = saved_flag_wrapv_pointer;
2748 : 1150335 : return b;
2749 : 1150344 : }
2750 : : else
2751 : : // Otherwise, invoke the generic fold routine.
2752 : 461615 : return range_operator::fold_range (r, type, op1, shift_range, rel);
2753 : 1612525 : }
2754 : :
2755 : : void
2756 : 552918 : operator_lshift::wi_fold (irange &r, tree type,
2757 : : const wide_int &lh_lb, const wide_int &lh_ub,
2758 : : const wide_int &rh_lb, const wide_int &rh_ub) const
2759 : : {
2760 : 552918 : signop sign = TYPE_SIGN (type);
2761 : 552918 : unsigned prec = TYPE_PRECISION (type);
2762 : 552918 : int overflow_pos = sign == SIGNED ? prec - 1 : prec;
2763 : 552918 : int bound_shift = overflow_pos - rh_ub.to_shwi ();
2764 : : // If bound_shift == HOST_BITS_PER_WIDE_INT, the llshift can
2765 : : // overflow. However, for that to happen, rh.max needs to be zero,
2766 : : // which means rh is a singleton range of zero, which means we simply return
2767 : : // [lh_lb, lh_ub] as the range.
2768 : 552918 : if (wi::eq_p (rh_ub, rh_lb) && wi::eq_p (rh_ub, 0))
2769 : : {
2770 : 24703 : r = int_range<2> (type, lh_lb, lh_ub);
2771 : 24703 : return;
2772 : : }
2773 : :
2774 : 528215 : wide_int bound = wi::set_bit_in_zero (bound_shift, prec);
2775 : 528215 : wide_int complement = ~(bound - 1);
2776 : 528215 : wide_int low_bound, high_bound;
2777 : 528215 : bool in_bounds = false;
2778 : :
2779 : 528215 : if (sign == UNSIGNED)
2780 : : {
2781 : 267381 : low_bound = bound;
2782 : 267381 : high_bound = complement;
2783 : 267381 : if (wi::ltu_p (lh_ub, low_bound))
2784 : : {
2785 : : // [5, 6] << [1, 2] == [10, 24].
2786 : : // We're shifting out only zeroes, the value increases
2787 : : // monotonically.
2788 : : in_bounds = true;
2789 : : }
2790 : 84766 : else if (wi::ltu_p (high_bound, lh_lb))
2791 : : {
2792 : : // [0xffffff00, 0xffffffff] << [1, 2]
2793 : : // == [0xfffffc00, 0xfffffffe].
2794 : : // We're shifting out only ones, the value decreases
2795 : : // monotonically.
2796 : : in_bounds = true;
2797 : : }
2798 : : }
2799 : : else
2800 : : {
2801 : : // [-1, 1] << [1, 2] == [-4, 4]
2802 : 260834 : low_bound = complement;
2803 : 260834 : high_bound = bound;
2804 : 260834 : if (wi::lts_p (lh_ub, high_bound)
2805 : 260834 : && wi::lts_p (low_bound, lh_lb))
2806 : : {
2807 : : // For non-negative numbers, we're shifting out only zeroes,
2808 : : // the value increases monotonically. For negative numbers,
2809 : : // we're shifting out only ones, the value decreases
2810 : : // monotonically.
2811 : : in_bounds = true;
2812 : : }
2813 : : }
2814 : :
2815 : : if (in_bounds)
2816 : 308804 : wi_cross_product (r, type, lh_lb, lh_ub, rh_lb, rh_ub);
2817 : : else
2818 : 219411 : r.set_varying (type);
2819 : 528224 : }
2820 : :
2821 : : bool
2822 : 592305 : operator_lshift::wi_op_overflows (wide_int &res, tree type,
2823 : : const wide_int &w0, const wide_int &w1) const
2824 : : {
2825 : 592305 : signop sign = TYPE_SIGN (type);
2826 : 592305 : if (wi::neg_p (w1))
2827 : : {
2828 : : // It's unclear from the C standard whether shifts can overflow.
2829 : : // The following code ignores overflow; perhaps a C standard
2830 : : // interpretation ruling is needed.
2831 : 0 : res = wi::rshift (w0, -w1, sign);
2832 : : }
2833 : : else
2834 : 592305 : res = wi::lshift (w0, w1);
2835 : 592305 : return false;
2836 : : }
2837 : :
2838 : : bool
2839 : 49262 : operator_lshift::op1_range (irange &r,
2840 : : tree type,
2841 : : const irange &lhs,
2842 : : const irange &op2,
2843 : : relation_trio) const
2844 : : {
2845 : 49262 : if (lhs.undefined_p ())
2846 : : return false;
2847 : :
2848 : 49262 : if (!contains_zero_p (lhs))
2849 : 21305 : r.set_nonzero (type);
2850 : : else
2851 : 27957 : r.set_varying (type);
2852 : :
2853 : 49262 : wide_int shift;
2854 : 49262 : if (op2.singleton_p (shift))
2855 : : {
2856 : 43697 : if (wi::lt_p (shift, 0, SIGNED))
2857 : : return false;
2858 : 43697 : if (wi::ge_p (shift, wi::uhwi (TYPE_PRECISION (type),
2859 : 43697 : TYPE_PRECISION (op2.type ())),
2860 : : UNSIGNED))
2861 : : return false;
2862 : 43697 : if (shift == 0)
2863 : : {
2864 : 6 : r.intersect (lhs);
2865 : 6 : return true;
2866 : : }
2867 : :
2868 : : // Work completely in unsigned mode to start.
2869 : 43691 : tree utype = type;
2870 : 43691 : int_range_max tmp_range;
2871 : 43691 : if (TYPE_SIGN (type) == SIGNED)
2872 : : {
2873 : 7224 : int_range_max tmp = lhs;
2874 : 7224 : utype = unsigned_type_for (type);
2875 : 7224 : range_cast (tmp, utype);
2876 : 7224 : op_rshift.fold_range (tmp_range, utype, tmp, op2);
2877 : 7224 : }
2878 : : else
2879 : 36467 : op_rshift.fold_range (tmp_range, utype, lhs, op2);
2880 : :
2881 : : // Start with ranges which can produce the LHS by right shifting the
2882 : : // result by the shift amount.
2883 : : // ie [0x08, 0xF0] = op1 << 2 will start with
2884 : : // [00001000, 11110000] = op1 << 2
2885 : : // [0x02, 0x4C] aka [00000010, 00111100]
2886 : :
2887 : : // Then create a range from the LB with the least significant upper bit
2888 : : // set, to the upper bound with all the bits set.
2889 : : // This would be [0x42, 0xFC] aka [01000010, 11111100].
2890 : :
2891 : : // Ideally we do this for each subrange, but just lump them all for now.
2892 : 43691 : unsigned low_bits = TYPE_PRECISION (utype) - shift.to_uhwi ();
2893 : 43691 : wide_int up_mask = wi::mask (low_bits, true, TYPE_PRECISION (utype));
2894 : 43691 : wide_int new_ub = wi::bit_or (up_mask, tmp_range.upper_bound ());
2895 : 43691 : wide_int new_lb = wi::set_bit (tmp_range.lower_bound (), low_bits);
2896 : 43691 : int_range<2> fill_range (utype, new_lb, new_ub);
2897 : 43691 : tmp_range.union_ (fill_range);
2898 : :
2899 : 43691 : if (utype != type)
2900 : 7224 : range_cast (tmp_range, type);
2901 : :
2902 : 43691 : r.intersect (tmp_range);
2903 : 43691 : return true;
2904 : 43691 : }
2905 : :
2906 : 5565 : return !r.varying_p ();
2907 : 49262 : }
2908 : :
2909 : : bool
2910 : 809011 : operator_rshift::op1_range (irange &r,
2911 : : tree type,
2912 : : const irange &lhs,
2913 : : const irange &op2,
2914 : : relation_trio) const
2915 : : {
2916 : 809011 : if (lhs.undefined_p ())
2917 : : return false;
2918 : 809011 : wide_int shift;
2919 : 809011 : if (op2.singleton_p (shift))
2920 : : {
2921 : : // Ignore nonsensical shifts.
2922 : 783888 : unsigned prec = TYPE_PRECISION (type);
2923 : 1567776 : if (wi::ge_p (shift,
2924 : 783888 : wi::uhwi (prec, TYPE_PRECISION (op2.type ())),
2925 : : UNSIGNED))
2926 : : return false;
2927 : 783888 : if (shift == 0)
2928 : : {
2929 : 75 : r = lhs;
2930 : 75 : return true;
2931 : : }
2932 : :
2933 : : // Folding the original operation may discard some impossible
2934 : : // ranges from the LHS.
2935 : 783813 : int_range_max lhs_refined;
2936 : 783813 : op_rshift.fold_range (lhs_refined, type, int_range<1> (type), op2);
2937 : 783813 : lhs_refined.intersect (lhs);
2938 : 783813 : if (lhs_refined.undefined_p ())
2939 : : {
2940 : 4 : r.set_undefined ();
2941 : 4 : return true;
2942 : : }
2943 : 783809 : int_range_max shift_range (op2.type (), shift, shift);
2944 : 783809 : int_range_max lb, ub;
2945 : 783809 : op_lshift.fold_range (lb, type, lhs_refined, shift_range);
2946 : : // LHS
2947 : : // 0000 0111 = OP1 >> 3
2948 : : //
2949 : : // OP1 is anything from 0011 1000 to 0011 1111. That is, a
2950 : : // range from LHS<<3 plus a mask of the 3 bits we shifted on the
2951 : : // right hand side (0x07).
2952 : 783809 : wide_int mask = wi::mask (shift.to_uhwi (), false, prec);
2953 : 783809 : int_range_max mask_range (type,
2954 : 783809 : wi::zero (TYPE_PRECISION (type)),
2955 : 783809 : mask);
2956 : 783809 : op_plus.fold_range (ub, type, lb, mask_range);
2957 : 783809 : r = lb;
2958 : 783809 : r.union_ (ub);
2959 : 783809 : if (!contains_zero_p (lhs_refined))
2960 : : {
2961 : 451123 : mask_range.invert ();
2962 : 451123 : r.intersect (mask_range);
2963 : : }
2964 : 783809 : return true;
2965 : 783813 : }
2966 : : return false;
2967 : 809011 : }
2968 : :
2969 : : bool
2970 : 10881695 : operator_rshift::wi_op_overflows (wide_int &res,
2971 : : tree type,
2972 : : const wide_int &w0,
2973 : : const wide_int &w1) const
2974 : : {
2975 : 10881695 : signop sign = TYPE_SIGN (type);
2976 : 10881695 : if (wi::neg_p (w1))
2977 : 0 : res = wi::lshift (w0, -w1);
2978 : : else
2979 : : {
2980 : : // It's unclear from the C standard whether shifts can overflow.
2981 : : // The following code ignores overflow; perhaps a C standard
2982 : : // interpretation ruling is needed.
2983 : 10881786 : res = wi::rshift (w0, w1, sign);
2984 : : }
2985 : 10881695 : return false;
2986 : : }
2987 : :
2988 : : bool
2989 : 3277361 : operator_rshift::fold_range (irange &r, tree type,
2990 : : const irange &op1,
2991 : : const irange &op2,
2992 : : relation_trio rel) const
2993 : : {
2994 : 3277361 : int_range_max shift;
2995 : 3277361 : if (!get_shift_range (shift, type, op2))
2996 : : {
2997 : 664 : if (op2.undefined_p ())
2998 : 251 : r.set_undefined ();
2999 : : else
3000 : 413 : r.set_zero (type);
3001 : 664 : return true;
3002 : : }
3003 : :
3004 : 3276697 : return range_operator::fold_range (r, type, op1, shift, rel);
3005 : 3277361 : }
3006 : :
3007 : : void
3008 : 3833510 : operator_rshift::wi_fold (irange &r, tree type,
3009 : : const wide_int &lh_lb, const wide_int &lh_ub,
3010 : : const wide_int &rh_lb, const wide_int &rh_ub) const
3011 : : {
3012 : 3833510 : wi_cross_product (r, type, lh_lb, lh_ub, rh_lb, rh_ub);
3013 : 3833510 : }
3014 : :
3015 : :
3016 : : // Add a partial equivalence between the LHS and op1 for casts.
3017 : :
3018 : : relation_kind
3019 : 23426767 : operator_cast::lhs_op1_relation (const irange &lhs,
3020 : : const irange &op1,
3021 : : const irange &op2 ATTRIBUTE_UNUSED,
3022 : : relation_kind) const
3023 : : {
3024 : 23426767 : if (lhs.undefined_p () || op1.undefined_p ())
3025 : : return VREL_VARYING;
3026 : 23404843 : unsigned lhs_prec = TYPE_PRECISION (lhs.type ());
3027 : 23404843 : unsigned op1_prec = TYPE_PRECISION (op1.type ());
3028 : : // If the result gets sign extended into a larger type check first if this
3029 : : // qualifies as a partial equivalence.
3030 : 23404843 : if (TYPE_SIGN (op1.type ()) == SIGNED && lhs_prec > op1_prec)
3031 : : {
3032 : : // If the result is sign extended, and the LHS is larger than op1,
3033 : : // check if op1's range can be negative as the sign extension will
3034 : : // cause the upper bits to be 1 instead of 0, invalidating the PE.
3035 : 3640356 : int_range<3> negs = range_negatives (op1.type ());
3036 : 3640356 : negs.intersect (op1);
3037 : 3640356 : if (!negs.undefined_p ())
3038 : 2564528 : return VREL_VARYING;
3039 : 3640356 : }
3040 : :
3041 : 20840315 : unsigned prec = MIN (lhs_prec, op1_prec);
3042 : 20840315 : return bits_to_pe (prec);
3043 : : }
3044 : :
3045 : : // Return TRUE if casting from INNER to OUTER is a truncating cast.
3046 : :
3047 : : inline bool
3048 : 80892527 : operator_cast::truncating_cast_p (const irange &inner,
3049 : : const irange &outer) const
3050 : : {
3051 : 80892527 : return TYPE_PRECISION (outer.type ()) < TYPE_PRECISION (inner.type ());
3052 : : }
3053 : :
3054 : : // Return TRUE if [MIN,MAX] is inside the domain of RANGE's type.
3055 : :
3056 : : bool
3057 : 69943604 : operator_cast::inside_domain_p (const wide_int &min,
3058 : : const wide_int &max,
3059 : : const irange &range) const
3060 : : {
3061 : 69943604 : wide_int domain_min = irange_val_min (range.type ());
3062 : 69943604 : wide_int domain_max = irange_val_max (range.type ());
3063 : 69943604 : signop domain_sign = TYPE_SIGN (range.type ());
3064 : 69943604 : return (wi::le_p (min, domain_max, domain_sign)
3065 : 69943604 : && wi::le_p (max, domain_max, domain_sign)
3066 : 69943604 : && wi::ge_p (min, domain_min, domain_sign)
3067 : 139887208 : && wi::ge_p (max, domain_min, domain_sign));
3068 : 69943604 : }
3069 : :
3070 : :
3071 : : // Helper for fold_range which work on a pair at a time.
3072 : :
3073 : : void
3074 : 72978052 : operator_cast::fold_pair (irange &r, unsigned index,
3075 : : const irange &inner,
3076 : : const irange &outer) const
3077 : : {
3078 : 72978052 : tree inner_type = inner.type ();
3079 : 72978052 : tree outer_type = outer.type ();
3080 : 72978052 : signop inner_sign = TYPE_SIGN (inner_type);
3081 : 72978052 : unsigned outer_prec = TYPE_PRECISION (outer_type);
3082 : :
3083 : : // check to see if casting from INNER to OUTER is a conversion that
3084 : : // fits in the resulting OUTER type.
3085 : 72978052 : wide_int inner_lb = inner.lower_bound (index);
3086 : 72978052 : wide_int inner_ub = inner.upper_bound (index);
3087 : 72978052 : if (truncating_cast_p (inner, outer))
3088 : : {
3089 : : // We may be able to accommodate a truncating cast if the
3090 : : // resulting range can be represented in the target type...
3091 : 14015612 : if (wi::rshift (wi::sub (inner_ub, inner_lb),
3092 : 7007806 : wi::uhwi (outer_prec, TYPE_PRECISION (inner.type ())),
3093 : 21023418 : inner_sign) != 0)
3094 : : {
3095 : 3034448 : r.set_varying (outer_type);
3096 : 3034448 : return;
3097 : : }
3098 : : }
3099 : : // ...but we must still verify that the final range fits in the
3100 : : // domain. This catches -fstrict-enum restrictions where the domain
3101 : : // range is smaller than what fits in the underlying type.
3102 : 69943604 : wide_int min = wide_int::from (inner_lb, outer_prec, inner_sign);
3103 : 69943604 : wide_int max = wide_int::from (inner_ub, outer_prec, inner_sign);
3104 : 69943604 : if (inside_domain_p (min, max, outer))
3105 : 69943604 : create_possibly_reversed_range (r, outer_type, min, max);
3106 : : else
3107 : 0 : r.set_varying (outer_type);
3108 : 72980479 : }
3109 : :
3110 : :
3111 : : bool
3112 : 58655579 : operator_cast::fold_range (irange &r, tree type ATTRIBUTE_UNUSED,
3113 : : const irange &inner,
3114 : : const irange &outer,
3115 : : relation_trio) const
3116 : : {
3117 : 58655579 : if (empty_range_varying (r, type, inner, outer))
3118 : 32071 : return true;
3119 : :
3120 : 58623508 : gcc_checking_assert (outer.varying_p ());
3121 : 58623508 : gcc_checking_assert (inner.num_pairs () > 0);
3122 : :
3123 : : // Avoid a temporary by folding the first pair directly into the result.
3124 : 58623508 : fold_pair (r, 0, inner, outer);
3125 : :
3126 : : // Then process any additional pairs by unioning with their results.
3127 : 72302427 : for (unsigned x = 1; x < inner.num_pairs (); ++x)
3128 : : {
3129 : 14354544 : int_range_max tmp;
3130 : 14354544 : fold_pair (tmp, x, inner, outer);
3131 : 14354544 : r.union_ (tmp);
3132 : : // If we hit varying, go update the bitmask.
3133 : 14354544 : if (r.varying_p ())
3134 : : break;
3135 : 14354544 : }
3136 : :
3137 : 58623508 : update_bitmask (r, inner, outer);
3138 : 58623508 : return true;
3139 : : }
3140 : :
3141 : : void
3142 : 58623508 : operator_cast::update_bitmask (irange &r, const irange &lh,
3143 : : const irange &rh) const
3144 : : {
3145 : 58623508 : update_known_bitmask (r, CONVERT_EXPR, lh, rh);
3146 : 58623508 : }
3147 : :
3148 : : bool
3149 : 7914475 : operator_cast::op1_range (irange &r, tree type,
3150 : : const irange &lhs,
3151 : : const irange &op2,
3152 : : relation_trio) const
3153 : : {
3154 : 7914475 : if (lhs.undefined_p ())
3155 : : return false;
3156 : 7914475 : tree lhs_type = lhs.type ();
3157 : 7914475 : gcc_checking_assert (types_compatible_p (op2.type(), type));
3158 : :
3159 : : // If we are calculating a pointer, shortcut to what we really care about.
3160 : 7914475 : if (POINTER_TYPE_P (type))
3161 : : {
3162 : : // Conversion from other pointers or a constant (including 0/NULL)
3163 : : // are straightforward.
3164 : 0 : if (POINTER_TYPE_P (lhs.type ())
3165 : 0 : || (lhs.singleton_p ()
3166 : 0 : && TYPE_PRECISION (lhs.type ()) >= TYPE_PRECISION (type)))
3167 : : {
3168 : 0 : r = lhs;
3169 : 0 : range_cast (r, type);
3170 : : }
3171 : : else
3172 : : {
3173 : : // If the LHS is not a pointer nor a singleton, then it is
3174 : : // either VARYING or non-zero.
3175 : 0 : if (!lhs.undefined_p () && !contains_zero_p (lhs))
3176 : 0 : r.set_nonzero (type);
3177 : : else
3178 : 0 : r.set_varying (type);
3179 : : }
3180 : 0 : r.intersect (op2);
3181 : 0 : return true;
3182 : : }
3183 : :
3184 : 7914475 : if (truncating_cast_p (op2, lhs))
3185 : : {
3186 : 1333498 : if (lhs.varying_p ())
3187 : 165166 : r.set_varying (type);
3188 : : else
3189 : : {
3190 : : // We want to insert the LHS as an unsigned value since it
3191 : : // would not trigger the signed bit of the larger type.
3192 : 1168332 : int_range_max converted_lhs = lhs;
3193 : 1168332 : range_cast (converted_lhs, unsigned_type_for (lhs_type));
3194 : 1168332 : range_cast (converted_lhs, type);
3195 : : // Start by building the positive signed outer range for the type.
3196 : 1168332 : wide_int lim = wi::set_bit_in_zero (TYPE_PRECISION (lhs_type),
3197 : 2336664 : TYPE_PRECISION (type));
3198 : 1168332 : create_possibly_reversed_range (r, type, lim,
3199 : 1168332 : wi::max_value (TYPE_PRECISION (type),
3200 : : SIGNED));
3201 : : // For the signed part, we need to simply union the 2 ranges now.
3202 : 1168332 : r.union_ (converted_lhs);
3203 : :
3204 : : // Create maximal negative number outside of LHS bits.
3205 : 1168332 : lim = wi::mask (TYPE_PRECISION (lhs_type), true,
3206 : 2336664 : TYPE_PRECISION (type));
3207 : : // Add this to the unsigned LHS range(s).
3208 : 1168332 : int_range_max lim_range (type, lim, lim);
3209 : 1168332 : int_range_max lhs_neg;
3210 : 1168332 : range_op_handler (PLUS_EXPR).fold_range (lhs_neg, type,
3211 : : converted_lhs, lim_range);
3212 : : // lhs_neg now has all the negative versions of the LHS.
3213 : : // Now union in all the values from SIGNED MIN (0x80000) to
3214 : : // lim-1 in order to fill in all the ranges with the upper
3215 : : // bits set.
3216 : :
3217 : : // PR 97317. If the lhs has only 1 bit less precision than the rhs,
3218 : : // we don't need to create a range from min to lim-1
3219 : : // calculate neg range traps trying to create [lim, lim - 1].
3220 : 1168332 : wide_int min_val = wi::min_value (TYPE_PRECISION (type), SIGNED);
3221 : 1168332 : if (lim != min_val)
3222 : : {
3223 : 1166640 : int_range_max neg (type,
3224 : 2333280 : wi::min_value (TYPE_PRECISION (type),
3225 : : SIGNED),
3226 : 2333280 : lim - 1);
3227 : 1166640 : lhs_neg.union_ (neg);
3228 : 1166640 : }
3229 : : // And finally, munge the signed and unsigned portions.
3230 : 1168332 : r.union_ (lhs_neg);
3231 : 1168332 : }
3232 : : // And intersect with any known value passed in the extra operand.
3233 : 1333498 : r.intersect (op2);
3234 : 1333498 : if (r.undefined_p ())
3235 : : return true;
3236 : :
3237 : : // Now create a bitmask indicating that the lower bit must match the
3238 : : // bits in the LHS. Zero-extend LHS bitmask to precision of op1.
3239 : 1333439 : irange_bitmask bm = lhs.get_bitmask ();
3240 : 2666878 : wide_int mask = wide_int::from (bm.mask (), TYPE_PRECISION (type),
3241 : 2666878 : UNSIGNED);
3242 : 2666878 : wide_int value = wide_int::from (bm.value (), TYPE_PRECISION (type),
3243 : 2666878 : UNSIGNED);
3244 : :
3245 : : // Set then additonal unknown bits in mask.
3246 : 1333439 : wide_int lim = wi::mask (TYPE_PRECISION (lhs_type), true,
3247 : 2666878 : TYPE_PRECISION (type));
3248 : 1333439 : mask = mask | lim;
3249 : :
3250 : : // Now set the new bitmask for the range.
3251 : 1333439 : irange_bitmask new_bm (value, mask);
3252 : 1333439 : r.update_bitmask (new_bm);
3253 : 1333439 : return true;
3254 : 1333439 : }
3255 : :
3256 : 6580977 : int_range_max tmp;
3257 : 6580977 : if (TYPE_PRECISION (lhs_type) == TYPE_PRECISION (type))
3258 : 4961179 : tmp = lhs;
3259 : : else
3260 : : {
3261 : : // The cast is not truncating, and the range is restricted to
3262 : : // the range of the RHS by this assignment.
3263 : : //
3264 : : // Cast the range of the RHS to the type of the LHS.
3265 : 1619798 : fold_range (tmp, lhs_type, int_range<1> (type), int_range<1> (lhs_type));
3266 : : // Intersect this with the LHS range will produce the range,
3267 : : // which will be cast to the RHS type before returning.
3268 : 1619798 : tmp.intersect (lhs);
3269 : : }
3270 : :
3271 : : // Cast the calculated range to the type of the RHS.
3272 : 6580977 : fold_range (r, type, tmp, int_range<1> (type));
3273 : 6580977 : return true;
3274 : 6580977 : }
3275 : :
3276 : : // VIEW_CONVERT_EXPR works like a cast between integral values.
3277 : : // If the number of bits are not the same, behaviour is undefined,
3278 : : // so cast behaviour still works.
3279 : :
3280 : : bool
3281 : 268243 : operator_view::fold_range (irange &r, tree type,
3282 : : const irange &op1, const irange &op2,
3283 : : relation_trio rel) const
3284 : : {
3285 : 268243 : return m_cast.fold_range (r, type, op1, op2, rel);
3286 : : }
3287 : :
3288 : : bool
3289 : 0 : operator_view::fold_range (prange &r, tree type,
3290 : : const prange &op1, const prange &op2,
3291 : : relation_trio rel) const
3292 : : {
3293 : 0 : return m_cast.fold_range (r, type, op1, op2, rel);
3294 : : }
3295 : : bool
3296 : 256058 : operator_view::fold_range (irange &r, tree type,
3297 : : const prange &op1, const irange &op2,
3298 : : relation_trio rel) const
3299 : : {
3300 : 256058 : return m_cast.fold_range (r, type, op1, op2, rel);
3301 : : }
3302 : :
3303 : : bool
3304 : 0 : operator_view::fold_range (prange &r, tree type,
3305 : : const irange &op1, const prange &op2,
3306 : : relation_trio rel) const
3307 : : {
3308 : 0 : return m_cast.fold_range (r, type, op1, op2, rel);
3309 : : }
3310 : :
3311 : : bool
3312 : 11144 : operator_view::op1_range (irange &r, tree type,
3313 : : const irange &lhs, const irange &op2,
3314 : : relation_trio rel) const
3315 : : {
3316 : 11144 : return m_cast.op1_range (r, type, lhs, op2, rel);
3317 : : }
3318 : :
3319 : : bool
3320 : 0 : operator_view::op1_range (prange &r, tree type,
3321 : : const prange &lhs, const prange &op2,
3322 : : relation_trio rel) const
3323 : : {
3324 : 0 : return m_cast.op1_range (r, type, lhs, op2, rel);
3325 : : }
3326 : :
3327 : : bool
3328 : 0 : operator_view::op1_range (irange &r, tree type,
3329 : : const prange &lhs, const irange &op2,
3330 : : relation_trio rel) const
3331 : : {
3332 : 0 : return m_cast.op1_range (r, type, lhs, op2, rel);
3333 : : }
3334 : :
3335 : : bool
3336 : 0 : operator_view::op1_range (prange &r, tree type,
3337 : : const irange &lhs, const prange &op2,
3338 : : relation_trio rel) const
3339 : : {
3340 : 0 : return m_cast.op1_range (r, type, lhs, op2, rel);
3341 : : }
3342 : :
3343 : : void
3344 : 0 : operator_view::update_bitmask (irange &r, const irange &lh,
3345 : : const irange &rh) const
3346 : : {
3347 : 0 : m_cast.update_bitmask (r, lh, rh);
3348 : 0 : }
3349 : :
3350 : :
3351 : : class operator_logical_and : public range_operator
3352 : : {
3353 : : using range_operator::fold_range;
3354 : : using range_operator::op1_range;
3355 : : using range_operator::op2_range;
3356 : : public:
3357 : : bool fold_range (irange &r, tree type,
3358 : : const irange &lh,
3359 : : const irange &rh,
3360 : : relation_trio rel = TRIO_VARYING) const final override;
3361 : : bool op1_range (irange &r, tree type,
3362 : : const irange &lhs,
3363 : : const irange &op2,
3364 : : relation_trio rel = TRIO_VARYING) const final override;
3365 : : bool op2_range (irange &r, tree type,
3366 : : const irange &lhs,
3367 : : const irange &op1,
3368 : : relation_trio rel = TRIO_VARYING) const final override;
3369 : : // Check compatibility of all operands.
3370 : 0 : bool operand_check_p (tree t1, tree t2, tree t3) const final override
3371 : 0 : { return range_compatible_p (t1, t2) && range_compatible_p (t1, t3); }
3372 : : } op_logical_and;
3373 : :
3374 : : bool
3375 : 0 : operator_logical_and::fold_range (irange &r, tree type,
3376 : : const irange &lh,
3377 : : const irange &rh,
3378 : : relation_trio) const
3379 : : {
3380 : 0 : if (empty_range_varying (r, type, lh, rh))
3381 : 0 : return true;
3382 : :
3383 : : // Precision of LHS and both operands must match.
3384 : 0 : if (TYPE_PRECISION (lh.type ()) != TYPE_PRECISION (type)
3385 : 0 : || TYPE_PRECISION (type) != TYPE_PRECISION (rh.type ()))
3386 : 0 : return false;
3387 : :
3388 : : // 0 && anything is 0.
3389 : 0 : if ((wi::eq_p (lh.lower_bound (), 0) && wi::eq_p (lh.upper_bound (), 0))
3390 : 0 : || (wi::eq_p (lh.lower_bound (), 0) && wi::eq_p (rh.upper_bound (), 0)))
3391 : 0 : r = range_false (type);
3392 : 0 : else if (contains_zero_p (lh) || contains_zero_p (rh))
3393 : : // To reach this point, there must be a logical 1 on each side, and
3394 : : // the only remaining question is whether there is a zero or not.
3395 : 0 : r = range_true_and_false (type);
3396 : : else
3397 : 0 : r = range_true (type);
3398 : : return true;
3399 : : }
3400 : :
3401 : : bool
3402 : 888165 : operator_logical_and::op1_range (irange &r, tree type,
3403 : : const irange &lhs,
3404 : : const irange &op2 ATTRIBUTE_UNUSED,
3405 : : relation_trio) const
3406 : : {
3407 : 888165 : switch (get_bool_state (r, lhs, type))
3408 : : {
3409 : 471824 : case BRS_TRUE:
3410 : : // A true result means both sides of the AND must be true.
3411 : 471824 : r = range_true (type);
3412 : 471824 : break;
3413 : 416341 : default:
3414 : : // Any other result means only one side has to be false, the
3415 : : // other side can be anything. So we cannot be sure of any
3416 : : // result here.
3417 : 416341 : r = range_true_and_false (type);
3418 : 416341 : break;
3419 : : }
3420 : 888165 : return true;
3421 : : }
3422 : :
3423 : : bool
3424 : 0 : operator_logical_and::op2_range (irange &r, tree type,
3425 : : const irange &lhs,
3426 : : const irange &op1,
3427 : : relation_trio) const
3428 : : {
3429 : 0 : return operator_logical_and::op1_range (r, type, lhs, op1);
3430 : : }
3431 : :
3432 : :
3433 : : void
3434 : 7558551 : operator_bitwise_and::update_bitmask (irange &r, const irange &lh,
3435 : : const irange &rh) const
3436 : : {
3437 : 7558551 : update_known_bitmask (r, BIT_AND_EXPR, lh, rh);
3438 : 7558551 : }
3439 : :
3440 : : // Optimize BIT_AND_EXPR, BIT_IOR_EXPR and BIT_XOR_EXPR of signed types
3441 : : // by considering the number of leading redundant sign bit copies.
3442 : : // clrsb (X op Y) = min (clrsb (X), clrsb (Y)), so for example
3443 : : // [-1, 0] op [-1, 0] is [-1, 0] (where nonzero_bits doesn't help).
3444 : : static bool
3445 : 182379 : wi_optimize_signed_bitwise_op (irange &r, tree type,
3446 : : const wide_int &lh_lb, const wide_int &lh_ub,
3447 : : const wide_int &rh_lb, const wide_int &rh_ub)
3448 : : {
3449 : 364758 : int lh_clrsb = MIN (wi::clrsb (lh_lb), wi::clrsb (lh_ub));
3450 : 364758 : int rh_clrsb = MIN (wi::clrsb (rh_lb), wi::clrsb (rh_ub));
3451 : 182379 : int new_clrsb = MIN (lh_clrsb, rh_clrsb);
3452 : 182379 : if (new_clrsb == 0)
3453 : : return false;
3454 : 11637 : int type_prec = TYPE_PRECISION (type);
3455 : 11637 : int rprec = (type_prec - new_clrsb) - 1;
3456 : 11637 : value_range_with_overflow (r, type,
3457 : 23274 : wi::mask (rprec, true, type_prec),
3458 : 11637 : wi::mask (rprec, false, type_prec));
3459 : 11637 : return true;
3460 : : }
3461 : :
3462 : : // An AND of 8,16, 32 or 64 bits can produce a partial equivalence between
3463 : : // the LHS and op1.
3464 : :
3465 : : relation_kind
3466 : 6856368 : operator_bitwise_and::lhs_op1_relation (const irange &lhs,
3467 : : const irange &op1,
3468 : : const irange &op2,
3469 : : relation_kind) const
3470 : : {
3471 : 6856368 : if (lhs.undefined_p () || op1.undefined_p () || op2.undefined_p ())
3472 : : return VREL_VARYING;
3473 : 6852139 : if (!op2.singleton_p ())
3474 : : return VREL_VARYING;
3475 : : // if val == 0xff or 0xFFFF OR 0Xffffffff OR 0Xffffffffffffffff, return TRUE
3476 : 3804133 : int prec1 = TYPE_PRECISION (op1.type ());
3477 : 3804133 : int prec2 = TYPE_PRECISION (op2.type ());
3478 : 3804133 : int mask_prec = 0;
3479 : 3804133 : wide_int mask = op2.lower_bound ();
3480 : 3804133 : if (wi::eq_p (mask, wi::mask (8, false, prec2)))
3481 : : mask_prec = 8;
3482 : 3720491 : else if (wi::eq_p (mask, wi::mask (16, false, prec2)))
3483 : : mask_prec = 16;
3484 : 3705286 : else if (wi::eq_p (mask, wi::mask (32, false, prec2)))
3485 : : mask_prec = 32;
3486 : 3521191 : else if (wi::eq_p (mask, wi::mask (64, false, prec2)))
3487 : 798 : mask_prec = 64;
3488 : 3804133 : return bits_to_pe (MIN (prec1, mask_prec));
3489 : 3804133 : }
3490 : :
3491 : : // Optimize BIT_AND_EXPR and BIT_IOR_EXPR in terms of a mask if
3492 : : // possible. Basically, see if we can optimize:
3493 : : //
3494 : : // [LB, UB] op Z
3495 : : // into:
3496 : : // [LB op Z, UB op Z]
3497 : : //
3498 : : // If the optimization was successful, accumulate the range in R and
3499 : : // return TRUE.
3500 : :
3501 : : static bool
3502 : 23356891 : wi_optimize_and_or (irange &r,
3503 : : enum tree_code code,
3504 : : tree type,
3505 : : const wide_int &lh_lb, const wide_int &lh_ub,
3506 : : const wide_int &rh_lb, const wide_int &rh_ub)
3507 : : {
3508 : : // Calculate the singleton mask among the ranges, if any.
3509 : 23356891 : wide_int lower_bound, upper_bound, mask;
3510 : 23356891 : if (wi::eq_p (rh_lb, rh_ub))
3511 : : {
3512 : 21699749 : mask = rh_lb;
3513 : 21699749 : lower_bound = lh_lb;
3514 : 21699749 : upper_bound = lh_ub;
3515 : : }
3516 : 1657142 : else if (wi::eq_p (lh_lb, lh_ub))
3517 : : {
3518 : 362027 : mask = lh_lb;
3519 : 362027 : lower_bound = rh_lb;
3520 : 362027 : upper_bound = rh_ub;
3521 : : }
3522 : : else
3523 : : return false;
3524 : :
3525 : : // If Z is a constant which (for op | its bitwise not) has n
3526 : : // consecutive least significant bits cleared followed by m 1
3527 : : // consecutive bits set immediately above it and either
3528 : : // m + n == precision, or (x >> (m + n)) == (y >> (m + n)).
3529 : : //
3530 : : // The least significant n bits of all the values in the range are
3531 : : // cleared or set, the m bits above it are preserved and any bits
3532 : : // above these are required to be the same for all values in the
3533 : : // range.
3534 : 22061776 : wide_int w = mask;
3535 : 22061776 : int m = 0, n = 0;
3536 : 22061776 : if (code == BIT_IOR_EXPR)
3537 : 6407553 : w = ~w;
3538 : 22061776 : if (wi::eq_p (w, 0))
3539 : 7590419 : n = w.get_precision ();
3540 : : else
3541 : : {
3542 : 14471357 : n = wi::ctz (w);
3543 : 14471363 : w = ~(w | wi::mask (n, false, w.get_precision ()));
3544 : 14471357 : if (wi::eq_p (w, 0))
3545 : 8772545 : m = w.get_precision () - n;
3546 : : else
3547 : 5698812 : m = wi::ctz (w) - n;
3548 : : }
3549 : 22061776 : wide_int new_mask = wi::mask (m + n, true, w.get_precision ());
3550 : 22061782 : if ((new_mask & lower_bound) != (new_mask & upper_bound))
3551 : : return false;
3552 : :
3553 : 17184946 : wide_int res_lb, res_ub;
3554 : 17184946 : if (code == BIT_AND_EXPR)
3555 : : {
3556 : 11008432 : res_lb = wi::bit_and (lower_bound, mask);
3557 : 11008432 : res_ub = wi::bit_and (upper_bound, mask);
3558 : : }
3559 : 6176514 : else if (code == BIT_IOR_EXPR)
3560 : : {
3561 : 6176514 : res_lb = wi::bit_or (lower_bound, mask);
3562 : 6176514 : res_ub = wi::bit_or (upper_bound, mask);
3563 : : }
3564 : : else
3565 : 0 : gcc_unreachable ();
3566 : 17184946 : value_range_with_overflow (r, type, res_lb, res_ub);
3567 : :
3568 : : // Furthermore, if the mask is non-zero, an IOR cannot contain zero.
3569 : 17184946 : if (code == BIT_IOR_EXPR && wi::ne_p (mask, 0))
3570 : : {
3571 : 3134787 : int_range<2> tmp;
3572 : 3134787 : tmp.set_nonzero (type);
3573 : 3134787 : r.intersect (tmp);
3574 : 3134787 : }
3575 : 17184946 : return true;
3576 : 62603619 : }
3577 : :
3578 : : // For range [LB, UB] compute two wide_int bit masks.
3579 : : //
3580 : : // In the MAYBE_NONZERO bit mask, if some bit is unset, it means that
3581 : : // for all numbers in the range the bit is 0, otherwise it might be 0
3582 : : // or 1.
3583 : : //
3584 : : // In the MUSTBE_NONZERO bit mask, if some bit is set, it means that
3585 : : // for all numbers in the range the bit is 1, otherwise it might be 0
3586 : : // or 1.
3587 : :
3588 : : void
3589 : 13345352 : wi_set_zero_nonzero_bits (tree type,
3590 : : const wide_int &lb, const wide_int &ub,
3591 : : wide_int &maybe_nonzero,
3592 : : wide_int &mustbe_nonzero)
3593 : : {
3594 : 13345352 : signop sign = TYPE_SIGN (type);
3595 : :
3596 : 13345352 : if (wi::eq_p (lb, ub))
3597 : 5340588 : maybe_nonzero = mustbe_nonzero = lb;
3598 : 8004764 : else if (wi::ge_p (lb, 0, sign) || wi::lt_p (ub, 0, sign))
3599 : : {
3600 : 7581330 : wide_int xor_mask = lb ^ ub;
3601 : 7581330 : maybe_nonzero = lb | ub;
3602 : 7581330 : mustbe_nonzero = lb & ub;
3603 : 7581330 : if (xor_mask != 0)
3604 : : {
3605 : 7581330 : wide_int mask = wi::mask (wi::floor_log2 (xor_mask), false,
3606 : 15162660 : maybe_nonzero.get_precision ());
3607 : 7581330 : maybe_nonzero = maybe_nonzero | mask;
3608 : 7581335 : mustbe_nonzero = wi::bit_and_not (mustbe_nonzero, mask);
3609 : 7581330 : }
3610 : 7581330 : }
3611 : : else
3612 : : {
3613 : 423434 : maybe_nonzero = wi::minus_one (lb.get_precision ());
3614 : 423434 : mustbe_nonzero = wi::zero (lb.get_precision ());
3615 : : }
3616 : 13345352 : }
3617 : :
3618 : : void
3619 : 16919742 : operator_bitwise_and::wi_fold (irange &r, tree type,
3620 : : const wide_int &lh_lb,
3621 : : const wide_int &lh_ub,
3622 : : const wide_int &rh_lb,
3623 : : const wide_int &rh_ub) const
3624 : : {
3625 : : // The AND algorithm does not handle complex signed operations well.
3626 : : // If a signed range crosses the boundry between signed and unsigned
3627 : : // proces sit as 2 ranges and union the results.
3628 : 16919742 : if (TYPE_SIGN (type) == SIGNED
3629 : 16919742 : && wi::neg_p (lh_lb, SIGNED) != wi::neg_p (lh_ub, SIGNED))
3630 : : {
3631 : 601244 : int prec = TYPE_PRECISION (type);
3632 : 601244 : int_range_max tmp;
3633 : : // Process [lh_lb, -1]
3634 : 601244 : wi_fold (tmp, type, lh_lb, wi::minus_one (prec), rh_lb, rh_ub);
3635 : : // Now Process [0, rh_ub]
3636 : 601244 : wi_fold (r, type, wi::zero (prec), lh_ub, rh_lb, rh_ub);
3637 : 601244 : r.union_ (tmp);
3638 : 601244 : return;
3639 : 601244 : }
3640 : :
3641 : 16318498 : if (wi_optimize_and_or (r, BIT_AND_EXPR, type, lh_lb, lh_ub, rh_lb, rh_ub))
3642 : : return;
3643 : :
3644 : 5310066 : wide_int maybe_nonzero_lh, mustbe_nonzero_lh;
3645 : 5310066 : wide_int maybe_nonzero_rh, mustbe_nonzero_rh;
3646 : 5310066 : wi_set_zero_nonzero_bits (type, lh_lb, lh_ub,
3647 : : maybe_nonzero_lh, mustbe_nonzero_lh);
3648 : 5310066 : wi_set_zero_nonzero_bits (type, rh_lb, rh_ub,
3649 : : maybe_nonzero_rh, mustbe_nonzero_rh);
3650 : :
3651 : 5310066 : wide_int new_lb = mustbe_nonzero_lh & mustbe_nonzero_rh;
3652 : 5310066 : wide_int new_ub = maybe_nonzero_lh & maybe_nonzero_rh;
3653 : 5310066 : signop sign = TYPE_SIGN (type);
3654 : 5310066 : unsigned prec = TYPE_PRECISION (type);
3655 : : // If both input ranges contain only negative values, we can
3656 : : // truncate the result range maximum to the minimum of the
3657 : : // input range maxima.
3658 : 5310066 : if (wi::lt_p (lh_ub, 0, sign) && wi::lt_p (rh_ub, 0, sign))
3659 : : {
3660 : 47292 : new_ub = wi::min (new_ub, lh_ub, sign);
3661 : 47292 : new_ub = wi::min (new_ub, rh_ub, sign);
3662 : : }
3663 : : // If either input range contains only non-negative values
3664 : : // we can truncate the result range maximum to the respective
3665 : : // maximum of the input range.
3666 : 5310066 : if (wi::ge_p (lh_lb, 0, sign))
3667 : 4577421 : new_ub = wi::min (new_ub, lh_ub, sign);
3668 : 5310066 : if (wi::ge_p (rh_lb, 0, sign))
3669 : 5090238 : new_ub = wi::min (new_ub, rh_ub, sign);
3670 : : // PR68217: In case of signed & sign-bit-CST should
3671 : : // result in [-INF, 0] instead of [-INF, INF].
3672 : 5310066 : if (wi::gt_p (new_lb, new_ub, sign))
3673 : : {
3674 : 55861 : wide_int sign_bit = wi::set_bit_in_zero (prec - 1, prec);
3675 : 55861 : if (sign == SIGNED
3676 : 55861 : && ((wi::eq_p (lh_lb, lh_ub)
3677 : 66 : && !wi::cmps (lh_lb, sign_bit))
3678 : 55861 : || (wi::eq_p (rh_lb, rh_ub)
3679 : 0 : && !wi::cmps (rh_lb, sign_bit))))
3680 : : {
3681 : 0 : new_lb = wi::min_value (prec, sign);
3682 : 0 : new_ub = wi::zero (prec);
3683 : : }
3684 : 55861 : }
3685 : : // If the limits got swapped around, return varying.
3686 : 5310066 : if (wi::gt_p (new_lb, new_ub,sign))
3687 : : {
3688 : 55861 : if (sign == SIGNED
3689 : 55861 : && wi_optimize_signed_bitwise_op (r, type,
3690 : : lh_lb, lh_ub,
3691 : : rh_lb, rh_ub))
3692 : 3794 : return;
3693 : 52067 : r.set_varying (type);
3694 : : }
3695 : : else
3696 : 5254205 : value_range_with_overflow (r, type, new_lb, new_ub);
3697 : 5310066 : }
3698 : :
3699 : : static void
3700 : 528392 : set_nonzero_range_from_mask (irange &r, tree type, const irange &lhs)
3701 : : {
3702 : 528392 : if (lhs.undefined_p () || contains_zero_p (lhs))
3703 : 280944 : r.set_varying (type);
3704 : : else
3705 : 247448 : r.set_nonzero (type);
3706 : 528392 : }
3707 : :
3708 : : /* Find out smallest RES where RES > VAL && (RES & MASK) == RES, if any
3709 : : (otherwise return VAL). VAL and MASK must be zero-extended for
3710 : : precision PREC. If SGNBIT is non-zero, first xor VAL with SGNBIT
3711 : : (to transform signed values into unsigned) and at the end xor
3712 : : SGNBIT back. */
3713 : :
3714 : : wide_int
3715 : 36272 : masked_increment (const wide_int &val_in, const wide_int &mask,
3716 : : const wide_int &sgnbit, unsigned int prec)
3717 : : {
3718 : 36272 : wide_int bit = wi::one (prec), res;
3719 : 36272 : unsigned int i;
3720 : :
3721 : 36272 : wide_int val = val_in ^ sgnbit;
3722 : 653095 : for (i = 0; i < prec; i++, bit += bit)
3723 : : {
3724 : 606026 : res = mask;
3725 : 606026 : if ((res & bit) == 0)
3726 : 518786 : continue;
3727 : 87240 : res = bit - 1;
3728 : 87240 : res = wi::bit_and_not (val + bit, res);
3729 : 87240 : res &= mask;
3730 : 87240 : if (wi::gtu_p (res, val))
3731 : 25475 : return res ^ sgnbit;
3732 : : }
3733 : 10797 : return val ^ sgnbit;
3734 : 36272 : }
3735 : :
3736 : : // This was shamelessly stolen from register_edge_assert_for_2 and
3737 : : // adjusted to work with iranges.
3738 : :
3739 : : void
3740 : 3305319 : operator_bitwise_and::simple_op1_range_solver (irange &r, tree type,
3741 : : const irange &lhs,
3742 : : const irange &op2) const
3743 : : {
3744 : 3305319 : if (!op2.singleton_p ())
3745 : : {
3746 : 527469 : set_nonzero_range_from_mask (r, type, lhs);
3747 : 1061645 : return;
3748 : : }
3749 : 2777850 : unsigned int nprec = TYPE_PRECISION (type);
3750 : 2777850 : wide_int cst2v = op2.lower_bound ();
3751 : 2777850 : bool cst2n = wi::neg_p (cst2v, TYPE_SIGN (type));
3752 : 2777850 : wide_int sgnbit;
3753 : 2777850 : if (cst2n)
3754 : 567148 : sgnbit = wi::set_bit_in_zero (nprec - 1, nprec);
3755 : : else
3756 : 2210702 : sgnbit = wi::zero (nprec);
3757 : :
3758 : : // Solve [lhs.lower_bound (), +INF] = x & MASK.
3759 : : //
3760 : : // Minimum unsigned value for >= if (VAL & CST2) == VAL is VAL and
3761 : : // maximum unsigned value is ~0. For signed comparison, if CST2
3762 : : // doesn't have the most significant bit set, handle it similarly. If
3763 : : // CST2 has MSB set, the minimum is the same, and maximum is ~0U/2.
3764 : 2777850 : wide_int valv = lhs.lower_bound ();
3765 : 2777850 : wide_int minv = valv & cst2v, maxv;
3766 : 2777850 : bool we_know_nothing = false;
3767 : 2777850 : if (minv != valv)
3768 : : {
3769 : : // If (VAL & CST2) != VAL, X & CST2 can't be equal to VAL.
3770 : 8361 : minv = masked_increment (valv, cst2v, sgnbit, nprec);
3771 : 8361 : if (minv == valv)
3772 : : {
3773 : : // If we can't determine anything on this bound, fall
3774 : : // through and conservatively solve for the other end point.
3775 : 2777850 : we_know_nothing = true;
3776 : : }
3777 : : }
3778 : 4988552 : maxv = wi::mask (nprec - (cst2n ? 1 : 0), false, nprec);
3779 : 2777850 : if (we_know_nothing)
3780 : 4090 : r.set_varying (type);
3781 : : else
3782 : 2773760 : create_possibly_reversed_range (r, type, minv, maxv);
3783 : :
3784 : : // Solve [-INF, lhs.upper_bound ()] = x & MASK.
3785 : : //
3786 : : // Minimum unsigned value for <= is 0 and maximum unsigned value is
3787 : : // VAL | ~CST2 if (VAL & CST2) == VAL. Otherwise, find smallest
3788 : : // VAL2 where
3789 : : // VAL2 > VAL && (VAL2 & CST2) == VAL2 and use (VAL2 - 1) | ~CST2
3790 : : // as maximum.
3791 : : // For signed comparison, if CST2 doesn't have most significant bit
3792 : : // set, handle it similarly. If CST2 has MSB set, the maximum is
3793 : : // the same and minimum is INT_MIN.
3794 : 2777850 : valv = lhs.upper_bound ();
3795 : 2777850 : minv = valv & cst2v;
3796 : 2777850 : if (minv == valv)
3797 : 2749939 : maxv = valv;
3798 : : else
3799 : : {
3800 : 27911 : maxv = masked_increment (valv, cst2v, sgnbit, nprec);
3801 : 27911 : if (maxv == valv)
3802 : : {
3803 : : // If we couldn't determine anything on either bound, return
3804 : : // undefined.
3805 : 6707 : if (we_know_nothing)
3806 : 3117 : r.set_undefined ();
3807 : 6707 : return;
3808 : : }
3809 : 21204 : maxv -= 1;
3810 : : }
3811 : 2771143 : maxv |= ~cst2v;
3812 : 2771143 : minv = sgnbit;
3813 : 2771143 : int_range<2> upper_bits;
3814 : 2771143 : create_possibly_reversed_range (upper_bits, type, minv, maxv);
3815 : 2771143 : r.intersect (upper_bits);
3816 : 2777850 : }
3817 : :
3818 : : bool
3819 : 3464127 : operator_bitwise_and::op1_range (irange &r, tree type,
3820 : : const irange &lhs,
3821 : : const irange &op2,
3822 : : relation_trio) const
3823 : : {
3824 : 3464127 : if (lhs.undefined_p ())
3825 : : return false;
3826 : 3464127 : if (types_compatible_p (type, boolean_type_node))
3827 : 888165 : return op_logical_and.op1_range (r, type, lhs, op2);
3828 : :
3829 : 2575962 : r.set_undefined ();
3830 : 5881281 : for (unsigned i = 0; i < lhs.num_pairs (); ++i)
3831 : : {
3832 : 6610638 : int_range_max chunk (lhs.type (),
3833 : 6610638 : lhs.lower_bound (i),
3834 : 6610638 : lhs.upper_bound (i));
3835 : 3305319 : int_range_max res;
3836 : 3305319 : simple_op1_range_solver (res, type, chunk, op2);
3837 : 3305319 : r.union_ (res);
3838 : 3305319 : }
3839 : 2575962 : if (r.undefined_p ())
3840 : 923 : set_nonzero_range_from_mask (r, type, lhs);
3841 : :
3842 : : // For MASK == op1 & MASK, all the bits in MASK must be set in op1.
3843 : 2575962 : wide_int mask;
3844 : 2575962 : if (lhs == op2 && lhs.singleton_p (mask))
3845 : : {
3846 : 347929 : r.update_bitmask (irange_bitmask (mask, ~mask));
3847 : 347929 : return true;
3848 : : }
3849 : :
3850 : 2228033 : if (!op2.singleton_p (mask))
3851 : : return true;
3852 : :
3853 : : // For 0 = op1 & MASK, op1 is ~MASK.
3854 : 1753078 : if (lhs.zero_p ())
3855 : : {
3856 : 641451 : wide_int nz = wi::bit_not (op2.get_nonzero_bits ());
3857 : 641451 : int_range<2> tmp (type);
3858 : 641451 : tmp.set_nonzero_bits (nz);
3859 : 641451 : r.intersect (tmp);
3860 : 641451 : }
3861 : :
3862 : 1753078 : irange_bitmask lhs_bm = lhs.get_bitmask ();
3863 : : // given [5,7] mask 0x3 value 0x4 = N & [7, 7] mask 0x0 value 0x7
3864 : : // Nothing is known about the bits not specified in the mask value (op2),
3865 : : // Start with the mask, 1's will occur where values were masked.
3866 : 1753078 : wide_int op1_mask = ~mask;
3867 : : // Any bits that are unknown on the LHS are also unknown in op1,
3868 : : // so union the current mask with the LHS mask.
3869 : 1753078 : op1_mask |= lhs_bm.mask ();
3870 : : // The resulting zeros correspond to known bits in the LHS mask, and
3871 : : // the LHS value should tell us what they are. Mask off any
3872 : : // extraneous values thats are not convered by the mask.
3873 : 1753078 : wide_int op1_value = lhs_bm.value () & ~op1_mask;
3874 : 1753078 : irange_bitmask op1_bm (op1_value, op1_mask);
3875 : : // Intersect this mask with anything already known about the value.
3876 : : // A return valueof false indicated the bitmask is an UNDEFINED range.
3877 : 1753078 : if (op1_bm.intersect (r.get_bitmask ()))
3878 : 1753078 : r.update_bitmask (op1_bm);
3879 : : else
3880 : 0 : r.set_undefined ();
3881 : 1753078 : return true;
3882 : 4329040 : }
3883 : :
3884 : : bool
3885 : 923309 : operator_bitwise_and::op2_range (irange &r, tree type,
3886 : : const irange &lhs,
3887 : : const irange &op1,
3888 : : relation_trio) const
3889 : : {
3890 : 923309 : return operator_bitwise_and::op1_range (r, type, lhs, op1);
3891 : : }
3892 : :
3893 : :
3894 : : class operator_logical_or : public range_operator
3895 : : {
3896 : : using range_operator::fold_range;
3897 : : using range_operator::op1_range;
3898 : : using range_operator::op2_range;
3899 : : public:
3900 : : bool fold_range (irange &r, tree type,
3901 : : const irange &lh,
3902 : : const irange &rh,
3903 : : relation_trio rel = TRIO_VARYING) const final override;
3904 : : bool op1_range (irange &r, tree type,
3905 : : const irange &lhs,
3906 : : const irange &op2,
3907 : : relation_trio rel = TRIO_VARYING) const final override;
3908 : : bool op2_range (irange &r, tree type,
3909 : : const irange &lhs,
3910 : : const irange &op1,
3911 : : relation_trio rel = TRIO_VARYING) const final override;
3912 : : // Check compatibility of all operands.
3913 : 0 : bool operand_check_p (tree t1, tree t2, tree t3) const final override
3914 : 0 : { return range_compatible_p (t1, t2) && range_compatible_p (t1, t3); }
3915 : : } op_logical_or;
3916 : :
3917 : : bool
3918 : 0 : operator_logical_or::fold_range (irange &r, tree type ATTRIBUTE_UNUSED,
3919 : : const irange &lh,
3920 : : const irange &rh,
3921 : : relation_trio) const
3922 : : {
3923 : 0 : if (empty_range_varying (r, type, lh, rh))
3924 : 0 : return true;
3925 : :
3926 : 0 : r = lh;
3927 : 0 : r.union_ (rh);
3928 : 0 : return true;
3929 : : }
3930 : :
3931 : : bool
3932 : 327155 : operator_logical_or::op1_range (irange &r, tree type,
3933 : : const irange &lhs,
3934 : : const irange &op2 ATTRIBUTE_UNUSED,
3935 : : relation_trio) const
3936 : : {
3937 : 327155 : switch (get_bool_state (r, lhs, type))
3938 : : {
3939 : 198367 : case BRS_FALSE:
3940 : : // A false result means both sides of the OR must be false.
3941 : 198367 : r = range_false (type);
3942 : 198367 : break;
3943 : 128788 : default:
3944 : : // Any other result means only one side has to be true, the
3945 : : // other side can be anything. so we can't be sure of any result
3946 : : // here.
3947 : 128788 : r = range_true_and_false (type);
3948 : 128788 : break;
3949 : : }
3950 : 327155 : return true;
3951 : : }
3952 : :
3953 : : bool
3954 : 0 : operator_logical_or::op2_range (irange &r, tree type,
3955 : : const irange &lhs,
3956 : : const irange &op1,
3957 : : relation_trio) const
3958 : : {
3959 : 0 : return operator_logical_or::op1_range (r, type, lhs, op1);
3960 : : }
3961 : :
3962 : :
3963 : : void
3964 : 2413520 : operator_bitwise_or::update_bitmask (irange &r, const irange &lh,
3965 : : const irange &rh) const
3966 : : {
3967 : 2413520 : update_known_bitmask (r, BIT_IOR_EXPR, lh, rh);
3968 : 2413520 : }
3969 : :
3970 : : void
3971 : 7038393 : operator_bitwise_or::wi_fold (irange &r, tree type,
3972 : : const wide_int &lh_lb,
3973 : : const wide_int &lh_ub,
3974 : : const wide_int &rh_lb,
3975 : : const wide_int &rh_ub) const
3976 : : {
3977 : 7038393 : if (wi_optimize_and_or (r, BIT_IOR_EXPR, type, lh_lb, lh_ub, rh_lb, rh_ub))
3978 : 6354578 : return;
3979 : :
3980 : 861879 : wide_int maybe_nonzero_lh, mustbe_nonzero_lh;
3981 : 861879 : wide_int maybe_nonzero_rh, mustbe_nonzero_rh;
3982 : 861879 : wi_set_zero_nonzero_bits (type, lh_lb, lh_ub,
3983 : : maybe_nonzero_lh, mustbe_nonzero_lh);
3984 : 861879 : wi_set_zero_nonzero_bits (type, rh_lb, rh_ub,
3985 : : maybe_nonzero_rh, mustbe_nonzero_rh);
3986 : 861879 : wide_int new_lb = mustbe_nonzero_lh | mustbe_nonzero_rh;
3987 : 861879 : wide_int new_ub = maybe_nonzero_lh | maybe_nonzero_rh;
3988 : 861879 : signop sign = TYPE_SIGN (type);
3989 : : // If the input ranges contain only positive values we can
3990 : : // truncate the minimum of the result range to the maximum
3991 : : // of the input range minima.
3992 : 861879 : if (wi::ge_p (lh_lb, 0, sign)
3993 : 861879 : && wi::ge_p (rh_lb, 0, sign))
3994 : : {
3995 : 654802 : new_lb = wi::max (new_lb, lh_lb, sign);
3996 : 654802 : new_lb = wi::max (new_lb, rh_lb, sign);
3997 : : }
3998 : : // If either input range contains only negative values
3999 : : // we can truncate the minimum of the result range to the
4000 : : // respective minimum range.
4001 : 861879 : if (wi::lt_p (lh_ub, 0, sign))
4002 : 19920 : new_lb = wi::max (new_lb, lh_lb, sign);
4003 : 861879 : if (wi::lt_p (rh_ub, 0, sign))
4004 : 10096 : new_lb = wi::max (new_lb, rh_lb, sign);
4005 : : // If the limits got swapped around, return a conservative range.
4006 : 861879 : if (wi::gt_p (new_lb, new_ub, sign))
4007 : : {
4008 : : // Make sure that nonzero|X is nonzero.
4009 : 178064 : if (wi::gt_p (lh_lb, 0, sign)
4010 : 173513 : || wi::gt_p (rh_lb, 0, sign)
4011 : 120113 : || wi::lt_p (lh_ub, 0, sign)
4012 : 298177 : || wi::lt_p (rh_ub, 0, sign))
4013 : 57951 : r.set_nonzero (type);
4014 : 120113 : else if (sign == SIGNED
4015 : 120113 : && wi_optimize_signed_bitwise_op (r, type,
4016 : : lh_lb, lh_ub,
4017 : : rh_lb, rh_ub))
4018 : : return;
4019 : : else
4020 : 117782 : r.set_varying (type);
4021 : 175733 : return;
4022 : : }
4023 : 683815 : value_range_with_overflow (r, type, new_lb, new_ub);
4024 : 861909 : }
4025 : :
4026 : : bool
4027 : 573904 : operator_bitwise_or::op1_range (irange &r, tree type,
4028 : : const irange &lhs,
4029 : : const irange &op2,
4030 : : relation_trio) const
4031 : : {
4032 : 573904 : if (lhs.undefined_p ())
4033 : : return false;
4034 : : // If this is really a logical wi_fold, call that.
4035 : 573904 : if (types_compatible_p (type, boolean_type_node))
4036 : 327155 : return op_logical_or.op1_range (r, type, lhs, op2);
4037 : :
4038 : 246749 : if (lhs.zero_p ())
4039 : : {
4040 : 76749 : r.set_zero (type);
4041 : 76749 : return true;
4042 : : }
4043 : :
4044 : : // if (A < 0 && B < 0)
4045 : : // Sometimes gets translated to
4046 : : // _1 = A | B
4047 : : // if (_1 < 0))
4048 : : // It is useful for ranger to recognize a positive LHS means the RHS
4049 : : // operands are also positive when dealing with the ELSE range..
4050 : 238948 : if (TYPE_SIGN (type) == SIGNED && wi::ge_p (lhs.lower_bound (), 0, SIGNED))
4051 : : {
4052 : 27009 : unsigned prec = TYPE_PRECISION (type);
4053 : 27009 : r.set (type, wi::zero (prec), wi::max_value (prec, SIGNED));
4054 : 27009 : return true;
4055 : : }
4056 : 142991 : r.set_varying (type);
4057 : 142991 : return true;
4058 : : }
4059 : :
4060 : : bool
4061 : 287663 : operator_bitwise_or::op2_range (irange &r, tree type,
4062 : : const irange &lhs,
4063 : : const irange &op1,
4064 : : relation_trio) const
4065 : : {
4066 : 287663 : return operator_bitwise_or::op1_range (r, type, lhs, op1);
4067 : : }
4068 : :
4069 : : void
4070 : 82845 : operator_bitwise_xor::update_bitmask (irange &r, const irange &lh,
4071 : : const irange &rh) const
4072 : : {
4073 : 82845 : update_known_bitmask (r, BIT_XOR_EXPR, lh, rh);
4074 : 82845 : }
4075 : :
4076 : : bool
4077 : 283220 : operator_bitwise_xor::fold_range (irange &r, tree type,
4078 : : const irange &lh, const irange &rh,
4079 : : relation_trio rel) const
4080 : : {
4081 : : // Handle X ^ UNDEFINED = UNDEFINED.
4082 : 283220 : if (lh.undefined_p () || rh.undefined_p ())
4083 : : {
4084 : 396 : r.set_undefined ();
4085 : 396 : return true;
4086 : : }
4087 : :
4088 : : // Next, handle X ^ X == [0, 0].
4089 : 282824 : if (rel.op1_op2 () == VREL_EQ)
4090 : : {
4091 : 40 : r.set_zero (type);
4092 : 40 : return true;
4093 : : }
4094 : :
4095 : : // If either operand is VARYING, the result is VARYING.
4096 : 282784 : if (lh.varying_p () || rh.varying_p ())
4097 : : {
4098 : : // If the operands are not equal, zero is not possible.
4099 : 198025 : if (rel.op1_op2 () != VREL_NE)
4100 : 197056 : r.set_varying (type);
4101 : : else
4102 : 969 : r.set_nonzero (type);
4103 : 198025 : return true;
4104 : : }
4105 : :
4106 : : // Now deal with X ^ 0 == X.
4107 : 84759 : if (lh.zero_p ())
4108 : : {
4109 : 1466 : r = rh;
4110 : 1466 : return true;
4111 : : }
4112 : 83293 : if (rh.zero_p ())
4113 : : {
4114 : 448 : r = lh;
4115 : 448 : return true;
4116 : : }
4117 : :
4118 : : // Start with the legacy range. This can sometimes pick up values
4119 : : // when there are a lot of subranges and fold_range aggregates them.
4120 : 82845 : bool res = range_operator::fold_range (r, type, lh, rh, rel);
4121 : :
4122 : : // Calculate the XOR identity : x ^ y = (x | y) & ~(x & y)
4123 : : // AND and OR are already much better optimized.
4124 : 82845 : int_range_max tmp1, tmp2, tmp3, new_result;
4125 : 82845 : int_range<2> varying;
4126 : 82845 : varying.set_varying (type);
4127 : :
4128 : 82845 : if (m_or.fold_range (tmp1, type, lh, rh, rel)
4129 : 82845 : && m_and.fold_range (tmp2, type, lh, rh, rel)
4130 : 82845 : && m_not.fold_range (tmp3, type, tmp2, varying, rel)
4131 : 165690 : && m_and.fold_range (new_result, type, tmp1, tmp3, rel))
4132 : : {
4133 : : // If the operands are not equal, or the LH does not contain any
4134 : : // element of the RH, zero is not possible.
4135 : 82845 : tmp1 = lh;
4136 : 82845 : if (rel.op1_op2 () == VREL_NE
4137 : 82845 : || (tmp1.intersect (rh) && tmp1.undefined_p ()))
4138 : : {
4139 : 32228 : tmp1.set_nonzero (type);
4140 : 32228 : new_result.intersect (tmp1);
4141 : : }
4142 : :
4143 : : // Combine with the legacy range if there was one.
4144 : 82845 : if (res)
4145 : 82845 : r.intersect (new_result);
4146 : : else
4147 : 0 : r = new_result;
4148 : 82845 : return true;
4149 : : }
4150 : : return res;
4151 : 82845 : }
4152 : :
4153 : : void
4154 : 164752 : operator_bitwise_xor::wi_fold (irange &r, tree type,
4155 : : const wide_int &lh_lb,
4156 : : const wide_int &lh_ub,
4157 : : const wide_int &rh_lb,
4158 : : const wide_int &rh_ub) const
4159 : : {
4160 : 164752 : signop sign = TYPE_SIGN (type);
4161 : 164752 : wide_int maybe_nonzero_lh, mustbe_nonzero_lh;
4162 : 164752 : wide_int maybe_nonzero_rh, mustbe_nonzero_rh;
4163 : 164752 : wi_set_zero_nonzero_bits (type, lh_lb, lh_ub,
4164 : : maybe_nonzero_lh, mustbe_nonzero_lh);
4165 : 164752 : wi_set_zero_nonzero_bits (type, rh_lb, rh_ub,
4166 : : maybe_nonzero_rh, mustbe_nonzero_rh);
4167 : :
4168 : 494256 : wide_int result_zero_bits = ((mustbe_nonzero_lh & mustbe_nonzero_rh)
4169 : 494256 : | ~(maybe_nonzero_lh | maybe_nonzero_rh));
4170 : 164752 : wide_int result_one_bits
4171 : 329504 : = (wi::bit_and_not (mustbe_nonzero_lh, maybe_nonzero_rh)
4172 : 329504 : | wi::bit_and_not (mustbe_nonzero_rh, maybe_nonzero_lh));
4173 : 164752 : wide_int new_ub = ~result_zero_bits;
4174 : 164752 : wide_int new_lb = result_one_bits;
4175 : :
4176 : : // If the range has all positive or all negative values, the result
4177 : : // is better than VARYING.
4178 : 164752 : if (wi::lt_p (new_lb, 0, sign) || wi::ge_p (new_ub, 0, sign))
4179 : 158347 : value_range_with_overflow (r, type, new_lb, new_ub);
4180 : 6405 : else if (sign == SIGNED
4181 : 6405 : && wi_optimize_signed_bitwise_op (r, type,
4182 : : lh_lb, lh_ub,
4183 : : rh_lb, rh_ub))
4184 : : ; /* Do nothing. */
4185 : : else
4186 : 893 : r.set_varying (type);
4187 : :
4188 : : /* Furthermore, XOR is non-zero if its arguments can't be equal. */
4189 : 164752 : if (wi::lt_p (lh_ub, rh_lb, sign)
4190 : 107791 : || wi::lt_p (rh_ub, lh_lb, sign)
4191 : 252191 : || wi::ne_p (result_one_bits, 0))
4192 : : {
4193 : 77313 : int_range<2> tmp;
4194 : 77313 : tmp.set_nonzero (type);
4195 : 77313 : r.intersect (tmp);
4196 : 77313 : }
4197 : 164752 : }
4198 : :
4199 : : bool
4200 : 82845 : operator_bitwise_xor::op1_op2_relation_effect (irange &lhs_range,
4201 : : tree type,
4202 : : const irange &,
4203 : : const irange &,
4204 : : relation_kind rel) const
4205 : : {
4206 : 82845 : if (rel == VREL_VARYING)
4207 : : return false;
4208 : :
4209 : 3738 : int_range<2> rel_range;
4210 : :
4211 : 3738 : switch (rel)
4212 : : {
4213 : 0 : case VREL_EQ:
4214 : 0 : rel_range.set_zero (type);
4215 : 0 : break;
4216 : 407 : case VREL_NE:
4217 : 407 : rel_range.set_nonzero (type);
4218 : 407 : break;
4219 : : default:
4220 : : return false;
4221 : : }
4222 : :
4223 : 407 : lhs_range.intersect (rel_range);
4224 : 407 : return true;
4225 : 3738 : }
4226 : :
4227 : : bool
4228 : 74344 : operator_bitwise_xor::op1_range (irange &r, tree type,
4229 : : const irange &lhs,
4230 : : const irange &op2,
4231 : : relation_trio) const
4232 : : {
4233 : 74344 : if (lhs.undefined_p () || lhs.varying_p ())
4234 : : {
4235 : 7634 : r = lhs;
4236 : 7634 : return true;
4237 : : }
4238 : 66710 : if (types_compatible_p (type, boolean_type_node))
4239 : : {
4240 : 1699 : switch (get_bool_state (r, lhs, type))
4241 : : {
4242 : 761 : case BRS_TRUE:
4243 : 761 : if (op2.varying_p ())
4244 : 729 : r.set_varying (type);
4245 : 32 : else if (op2.zero_p ())
4246 : 24 : r = range_true (type);
4247 : : // See get_bool_state for the rationale
4248 : 8 : else if (op2.undefined_p () || contains_zero_p (op2))
4249 : 0 : r = range_true_and_false (type);
4250 : : else
4251 : 8 : r = range_false (type);
4252 : : break;
4253 : 938 : case BRS_FALSE:
4254 : 938 : r = op2;
4255 : 938 : break;
4256 : : default:
4257 : : break;
4258 : : }
4259 : 1699 : return true;
4260 : : }
4261 : 65011 : r.set_varying (type);
4262 : 65011 : return true;
4263 : : }
4264 : :
4265 : : bool
4266 : 32435 : operator_bitwise_xor::op2_range (irange &r, tree type,
4267 : : const irange &lhs,
4268 : : const irange &op1,
4269 : : relation_trio) const
4270 : : {
4271 : 32435 : return operator_bitwise_xor::op1_range (r, type, lhs, op1);
4272 : : }
4273 : :
4274 : : class operator_trunc_mod : public range_operator
4275 : : {
4276 : : using range_operator::op1_range;
4277 : : using range_operator::op2_range;
4278 : : using range_operator::update_bitmask;
4279 : : public:
4280 : : virtual void wi_fold (irange &r, tree type,
4281 : : const wide_int &lh_lb,
4282 : : const wide_int &lh_ub,
4283 : : const wide_int &rh_lb,
4284 : : const wide_int &rh_ub) const;
4285 : : virtual bool op1_range (irange &r, tree type,
4286 : : const irange &lhs,
4287 : : const irange &op2,
4288 : : relation_trio) const;
4289 : : virtual bool op2_range (irange &r, tree type,
4290 : : const irange &lhs,
4291 : : const irange &op1,
4292 : : relation_trio) const;
4293 : 1027275 : void update_bitmask (irange &r, const irange &lh, const irange &rh) const
4294 : 1027275 : { update_known_bitmask (r, TRUNC_MOD_EXPR, lh, rh); }
4295 : : } op_trunc_mod;
4296 : :
4297 : : void
4298 : 1263992 : operator_trunc_mod::wi_fold (irange &r, tree type,
4299 : : const wide_int &lh_lb,
4300 : : const wide_int &lh_ub,
4301 : : const wide_int &rh_lb,
4302 : : const wide_int &rh_ub) const
4303 : : {
4304 : 1263992 : wide_int new_lb, new_ub, tmp;
4305 : 1263992 : signop sign = TYPE_SIGN (type);
4306 : 1263992 : unsigned prec = TYPE_PRECISION (type);
4307 : :
4308 : : // Mod 0 is undefined.
4309 : 1263992 : if (wi_zero_p (type, rh_lb, rh_ub))
4310 : : {
4311 : 8675 : r.set_undefined ();
4312 : 8675 : return;
4313 : : }
4314 : :
4315 : : // Check for constant and try to fold.
4316 : 1466043 : if (lh_lb == lh_ub && rh_lb == rh_ub)
4317 : : {
4318 : 21369 : wi::overflow_type ov = wi::OVF_NONE;
4319 : 21369 : tmp = wi::mod_trunc (lh_lb, rh_lb, sign, &ov);
4320 : 21369 : if (ov == wi::OVF_NONE)
4321 : : {
4322 : 21369 : r = int_range<2> (type, tmp, tmp);
4323 : 21369 : return;
4324 : : }
4325 : : }
4326 : :
4327 : : // ABS (A % B) < ABS (B) and either 0 <= A % B <= A or A <= A % B <= 0.
4328 : 1233948 : new_ub = rh_ub - 1;
4329 : 1233948 : if (sign == SIGNED)
4330 : : {
4331 : 524509 : tmp = -1 - rh_lb;
4332 : 524509 : new_ub = wi::smax (new_ub, tmp);
4333 : : }
4334 : :
4335 : 1233948 : if (sign == UNSIGNED)
4336 : 709439 : new_lb = wi::zero (prec);
4337 : : else
4338 : : {
4339 : 524509 : new_lb = -new_ub;
4340 : 524509 : tmp = lh_lb;
4341 : 524509 : if (wi::gts_p (tmp, 0))
4342 : 131020 : tmp = wi::zero (prec);
4343 : 524533 : new_lb = wi::smax (new_lb, tmp);
4344 : : }
4345 : 1233948 : tmp = lh_ub;
4346 : 1233948 : if (sign == SIGNED && wi::neg_p (tmp))
4347 : 26762 : tmp = wi::zero (prec);
4348 : 1233948 : new_ub = wi::min (new_ub, tmp, sign);
4349 : :
4350 : 1233948 : value_range_with_overflow (r, type, new_lb, new_ub);
4351 : 1264116 : }
4352 : :
4353 : : bool
4354 : 535746 : operator_trunc_mod::op1_range (irange &r, tree type,
4355 : : const irange &lhs,
4356 : : const irange &,
4357 : : relation_trio) const
4358 : : {
4359 : 535746 : if (lhs.undefined_p ())
4360 : : return false;
4361 : : // PR 91029.
4362 : 535746 : signop sign = TYPE_SIGN (type);
4363 : 535746 : unsigned prec = TYPE_PRECISION (type);
4364 : : // (a % b) >= x && x > 0 , then a >= x.
4365 : 535838 : if (wi::gt_p (lhs.lower_bound (), 0, sign))
4366 : : {
4367 : 134541 : r.set (type, lhs.lower_bound (), wi::max_value (prec, sign));
4368 : 134508 : return true;
4369 : : }
4370 : : // (a % b) <= x && x < 0 , then a <= x.
4371 : 401297 : if (wi::lt_p (lhs.upper_bound (), 0, sign))
4372 : : {
4373 : 5447 : r.set (type, wi::min_value (prec, sign), lhs.upper_bound ());
4374 : 5447 : return true;
4375 : : }
4376 : : return false;
4377 : : }
4378 : :
4379 : : bool
4380 : 350985 : operator_trunc_mod::op2_range (irange &r, tree type,
4381 : : const irange &lhs,
4382 : : const irange &,
4383 : : relation_trio) const
4384 : : {
4385 : 350985 : if (lhs.undefined_p ())
4386 : : return false;
4387 : : // PR 91029.
4388 : 350985 : signop sign = TYPE_SIGN (type);
4389 : 350985 : unsigned prec = TYPE_PRECISION (type);
4390 : : // (a % b) >= x && x > 0 , then b is in ~[-x, x] for signed
4391 : : // or b > x for unsigned.
4392 : 351018 : if (wi::gt_p (lhs.lower_bound (), 0, sign))
4393 : : {
4394 : 54083 : if (sign == SIGNED)
4395 : 2164 : r.set (type, wi::neg (lhs.lower_bound ()),
4396 : 4328 : lhs.lower_bound (), VR_ANTI_RANGE);
4397 : 51931 : else if (wi::lt_p (lhs.lower_bound (), wi::max_value (prec, sign),
4398 : : sign))
4399 : 51937 : r.set (type, lhs.lower_bound () + 1, wi::max_value (prec, sign));
4400 : : else
4401 : : return false;
4402 : 54083 : return true;
4403 : : }
4404 : : // (a % b) <= x && x < 0 , then b is in ~[x, -x].
4405 : 296929 : if (wi::lt_p (lhs.upper_bound (), 0, sign))
4406 : : {
4407 : 5288 : if (wi::gt_p (lhs.upper_bound (), wi::min_value (prec, sign), sign))
4408 : 5288 : r.set (type, lhs.upper_bound (),
4409 : 10576 : wi::neg (lhs.upper_bound ()), VR_ANTI_RANGE);
4410 : : else
4411 : : return false;
4412 : 5288 : return true;
4413 : : }
4414 : : return false;
4415 : : }
4416 : :
4417 : :
4418 : : class operator_logical_not : public range_operator
4419 : : {
4420 : : using range_operator::fold_range;
4421 : : using range_operator::op1_range;
4422 : : public:
4423 : : bool fold_range (irange &r, tree type,
4424 : : const irange &lh,
4425 : : const irange &rh,
4426 : : relation_trio rel = TRIO_VARYING) const final override;
4427 : : bool op1_range (irange &r, tree type,
4428 : : const irange &lhs,
4429 : : const irange &op2,
4430 : : relation_trio rel = TRIO_VARYING) const final override;
4431 : : // Check compatibility of LHS and op1.
4432 : 0 : bool operand_check_p (tree t1, tree t2, tree) const final override
4433 : 0 : { return range_compatible_p (t1, t2); }
4434 : : } op_logical_not;
4435 : :
4436 : : // Folding a logical NOT, oddly enough, involves doing nothing on the
4437 : : // forward pass through. During the initial walk backwards, the
4438 : : // logical NOT reversed the desired outcome on the way back, so on the
4439 : : // way forward all we do is pass the range forward.
4440 : : //
4441 : : // b_2 = x_1 < 20
4442 : : // b_3 = !b_2
4443 : : // if (b_3)
4444 : : // to determine the TRUE branch, walking backward
4445 : : // if (b_3) if ([1,1])
4446 : : // b_3 = !b_2 [1,1] = ![0,0]
4447 : : // b_2 = x_1 < 20 [0,0] = x_1 < 20, false, so x_1 == [20, 255]
4448 : : // which is the result we are looking for.. so.. pass it through.
4449 : :
4450 : : bool
4451 : 244764 : operator_logical_not::fold_range (irange &r, tree type,
4452 : : const irange &lh,
4453 : : const irange &rh ATTRIBUTE_UNUSED,
4454 : : relation_trio) const
4455 : : {
4456 : 244764 : if (empty_range_varying (r, type, lh, rh))
4457 : 0 : return true;
4458 : :
4459 : 244764 : r = lh;
4460 : 244764 : if (!lh.varying_p () && !lh.undefined_p ())
4461 : 52303 : r.invert ();
4462 : :
4463 : : return true;
4464 : : }
4465 : :
4466 : : bool
4467 : 26666 : operator_logical_not::op1_range (irange &r,
4468 : : tree type,
4469 : : const irange &lhs,
4470 : : const irange &op2,
4471 : : relation_trio) const
4472 : : {
4473 : : // Logical NOT is involutary...do it again.
4474 : 26666 : return fold_range (r, type, lhs, op2);
4475 : : }
4476 : :
4477 : : bool
4478 : 601854 : operator_bitwise_not::fold_range (irange &r, tree type,
4479 : : const irange &lh,
4480 : : const irange &rh,
4481 : : relation_trio) const
4482 : : {
4483 : 601854 : if (empty_range_varying (r, type, lh, rh))
4484 : 97 : return true;
4485 : :
4486 : 601757 : if (types_compatible_p (type, boolean_type_node))
4487 : 218098 : return op_logical_not.fold_range (r, type, lh, rh);
4488 : :
4489 : : // ~X is simply -1 - X.
4490 : 767318 : int_range<1> minusone (type, wi::minus_one (TYPE_PRECISION (type)),
4491 : 767318 : wi::minus_one (TYPE_PRECISION (type)));
4492 : 383659 : return range_op_handler (MINUS_EXPR).fold_range (r, type, minusone, lh);
4493 : 383659 : }
4494 : :
4495 : : bool
4496 : 52062 : operator_bitwise_not::op1_range (irange &r, tree type,
4497 : : const irange &lhs,
4498 : : const irange &op2,
4499 : : relation_trio) const
4500 : : {
4501 : 52062 : if (lhs.undefined_p ())
4502 : : return false;
4503 : 52062 : if (types_compatible_p (type, boolean_type_node))
4504 : 26666 : return op_logical_not.op1_range (r, type, lhs, op2);
4505 : :
4506 : : // ~X is -1 - X and since bitwise NOT is involutary...do it again.
4507 : 25396 : return fold_range (r, type, lhs, op2);
4508 : : }
4509 : :
4510 : : void
4511 : 0 : operator_bitwise_not::update_bitmask (irange &r, const irange &lh,
4512 : : const irange &rh) const
4513 : : {
4514 : 0 : update_known_bitmask (r, BIT_NOT_EXPR, lh, rh);
4515 : 0 : }
4516 : :
4517 : :
4518 : : bool
4519 : 304713 : operator_cst::fold_range (irange &r, tree type ATTRIBUTE_UNUSED,
4520 : : const irange &lh,
4521 : : const irange &rh ATTRIBUTE_UNUSED,
4522 : : relation_trio) const
4523 : : {
4524 : 304713 : r = lh;
4525 : 304713 : return true;
4526 : : }
4527 : :
4528 : :
4529 : : // Determine if there is a relationship between LHS and OP1.
4530 : :
4531 : : relation_kind
4532 : 904268 : operator_identity::lhs_op1_relation (const irange &lhs,
4533 : : const irange &op1 ATTRIBUTE_UNUSED,
4534 : : const irange &op2 ATTRIBUTE_UNUSED,
4535 : : relation_kind) const
4536 : : {
4537 : 904268 : if (lhs.undefined_p ())
4538 : 473 : return VREL_VARYING;
4539 : : // Simply a copy, so they are equivalent.
4540 : : return VREL_EQ;
4541 : : }
4542 : :
4543 : : bool
4544 : 904987 : operator_identity::fold_range (irange &r, tree type ATTRIBUTE_UNUSED,
4545 : : const irange &lh,
4546 : : const irange &rh ATTRIBUTE_UNUSED,
4547 : : relation_trio) const
4548 : : {
4549 : 904987 : r = lh;
4550 : 904987 : return true;
4551 : : }
4552 : :
4553 : : bool
4554 : 293276 : operator_identity::op1_range (irange &r, tree type ATTRIBUTE_UNUSED,
4555 : : const irange &lhs,
4556 : : const irange &op2 ATTRIBUTE_UNUSED,
4557 : : relation_trio) const
4558 : : {
4559 : 293276 : r = lhs;
4560 : 293276 : return true;
4561 : : }
4562 : :
4563 : :
4564 : : class operator_unknown : public range_operator
4565 : : {
4566 : : using range_operator::fold_range;
4567 : : public:
4568 : : virtual bool fold_range (irange &r, tree type,
4569 : : const irange &op1,
4570 : : const irange &op2,
4571 : : relation_trio rel = TRIO_VARYING) const;
4572 : : } op_unknown;
4573 : :
4574 : : bool
4575 : 809734 : operator_unknown::fold_range (irange &r, tree type,
4576 : : const irange &lh ATTRIBUTE_UNUSED,
4577 : : const irange &rh ATTRIBUTE_UNUSED,
4578 : : relation_trio) const
4579 : : {
4580 : 809734 : r.set_varying (type);
4581 : 809734 : return true;
4582 : : }
4583 : :
4584 : :
4585 : : void
4586 : 80005 : operator_abs::wi_fold (irange &r, tree type,
4587 : : const wide_int &lh_lb, const wide_int &lh_ub,
4588 : : const wide_int &rh_lb ATTRIBUTE_UNUSED,
4589 : : const wide_int &rh_ub ATTRIBUTE_UNUSED) const
4590 : : {
4591 : 80005 : wide_int min, max;
4592 : 80005 : signop sign = TYPE_SIGN (type);
4593 : 80005 : unsigned prec = TYPE_PRECISION (type);
4594 : :
4595 : : // Pass through LH for the easy cases.
4596 : 80005 : if (sign == UNSIGNED || wi::ge_p (lh_lb, 0, sign))
4597 : : {
4598 : 11782 : r = int_range<1> (type, lh_lb, lh_ub);
4599 : 11782 : return;
4600 : : }
4601 : :
4602 : : // -TYPE_MIN_VALUE = TYPE_MIN_VALUE with flag_wrapv so we can't get
4603 : : // a useful range.
4604 : 68223 : wide_int min_value = wi::min_value (prec, sign);
4605 : 68223 : wide_int max_value = wi::max_value (prec, sign);
4606 : 68223 : if (!TYPE_OVERFLOW_UNDEFINED (type) && wi::eq_p (lh_lb, min_value))
4607 : : {
4608 : 640 : r.set_varying (type);
4609 : 640 : return;
4610 : : }
4611 : :
4612 : : // ABS_EXPR may flip the range around, if the original range
4613 : : // included negative values.
4614 : 67583 : if (wi::eq_p (lh_lb, min_value))
4615 : : {
4616 : : // ABS ([-MIN, -MIN]) isn't representable, but we have traditionally
4617 : : // returned [-MIN,-MIN] so this preserves that behavior. PR37078
4618 : 35090 : if (wi::eq_p (lh_ub, min_value))
4619 : : {
4620 : 113 : r = int_range<1> (type, min_value, min_value);
4621 : 113 : return;
4622 : : }
4623 : 34977 : min = max_value;
4624 : : }
4625 : : else
4626 : 32493 : min = wi::abs (lh_lb);
4627 : :
4628 : 67470 : if (wi::eq_p (lh_ub, min_value))
4629 : 0 : max = max_value;
4630 : : else
4631 : 67470 : max = wi::abs (lh_ub);
4632 : :
4633 : : // If the range contains zero then we know that the minimum value in the
4634 : : // range will be zero.
4635 : 67470 : if (wi::le_p (lh_lb, 0, sign) && wi::ge_p (lh_ub, 0, sign))
4636 : : {
4637 : 57844 : if (wi::gt_p (min, max, sign))
4638 : 21608 : max = min;
4639 : 57844 : min = wi::zero (prec);
4640 : : }
4641 : : else
4642 : : {
4643 : : // If the range was reversed, swap MIN and MAX.
4644 : 9626 : if (wi::gt_p (min, max, sign))
4645 : 9019 : std::swap (min, max);
4646 : : }
4647 : :
4648 : : // If the new range has its limits swapped around (MIN > MAX), then
4649 : : // the operation caused one of them to wrap around. The only thing
4650 : : // we know is that the result is positive.
4651 : 67470 : if (wi::gt_p (min, max, sign))
4652 : : {
4653 : 0 : min = wi::zero (prec);
4654 : 0 : max = max_value;
4655 : : }
4656 : 67470 : r = int_range<1> (type, min, max);
4657 : 80758 : }
4658 : :
4659 : : bool
4660 : 59289 : operator_abs::op1_range (irange &r, tree type,
4661 : : const irange &lhs,
4662 : : const irange &op2,
4663 : : relation_trio) const
4664 : : {
4665 : 59289 : if (empty_range_varying (r, type, lhs, op2))
4666 : 0 : return true;
4667 : 59289 : if (TYPE_UNSIGNED (type))
4668 : : {
4669 : 0 : r = lhs;
4670 : 0 : return true;
4671 : : }
4672 : : // Start with the positives because negatives are an impossible result.
4673 : 59289 : int_range_max positives = range_positives (type);
4674 : 59289 : positives.intersect (lhs);
4675 : 59289 : r = positives;
4676 : : // Then add the negative of each pair:
4677 : : // ABS(op1) = [5,20] would yield op1 => [-20,-5][5,20].
4678 : 119367 : for (unsigned i = 0; i < positives.num_pairs (); ++i)
4679 : 60078 : r.union_ (int_range<1> (type,
4680 : 120156 : -positives.upper_bound (i),
4681 : 180234 : -positives.lower_bound (i)));
4682 : : // With flag_wrapv, -TYPE_MIN_VALUE = TYPE_MIN_VALUE which is
4683 : : // unrepresentable. Add -TYPE_MIN_VALUE in this case.
4684 : 59289 : wide_int min_value = wi::min_value (TYPE_PRECISION (type), TYPE_SIGN (type));
4685 : 59289 : wide_int lb = lhs.lower_bound ();
4686 : 59289 : if (!TYPE_OVERFLOW_UNDEFINED (type) && wi::eq_p (lb, min_value))
4687 : 169 : r.union_ (int_range<2> (type, lb, lb));
4688 : 59289 : return true;
4689 : 59289 : }
4690 : :
4691 : : void
4692 : 73045 : operator_abs::update_bitmask (irange &r, const irange &lh,
4693 : : const irange &rh) const
4694 : : {
4695 : 73045 : update_known_bitmask (r, ABS_EXPR, lh, rh);
4696 : 73045 : }
4697 : :
4698 : : class operator_absu : public range_operator
4699 : : {
4700 : : using range_operator::update_bitmask;
4701 : : public:
4702 : : virtual void wi_fold (irange &r, tree type,
4703 : : const wide_int &lh_lb, const wide_int &lh_ub,
4704 : : const wide_int &rh_lb, const wide_int &rh_ub)
4705 : : const final override;
4706 : : virtual void update_bitmask (irange &r, const irange &lh,
4707 : : const irange &rh) const final override;
4708 : : } op_absu;
4709 : :
4710 : : void
4711 : 13128 : operator_absu::wi_fold (irange &r, tree type,
4712 : : const wide_int &lh_lb, const wide_int &lh_ub,
4713 : : const wide_int &rh_lb ATTRIBUTE_UNUSED,
4714 : : const wide_int &rh_ub ATTRIBUTE_UNUSED) const
4715 : : {
4716 : 13128 : wide_int new_lb, new_ub;
4717 : :
4718 : : // Pass through VR0 the easy cases.
4719 : 13128 : if (wi::ges_p (lh_lb, 0))
4720 : : {
4721 : 2473 : new_lb = lh_lb;
4722 : 2473 : new_ub = lh_ub;
4723 : : }
4724 : : else
4725 : : {
4726 : 10655 : new_lb = wi::abs (lh_lb);
4727 : 10655 : new_ub = wi::abs (lh_ub);
4728 : :
4729 : : // If the range contains zero then we know that the minimum
4730 : : // value in the range will be zero.
4731 : 10655 : if (wi::ges_p (lh_ub, 0))
4732 : : {
4733 : 8192 : if (wi::gtu_p (new_lb, new_ub))
4734 : 6898 : new_ub = new_lb;
4735 : 8192 : new_lb = wi::zero (TYPE_PRECISION (type));
4736 : : }
4737 : : else
4738 : 2463 : std::swap (new_lb, new_ub);
4739 : : }
4740 : :
4741 : 13128 : gcc_checking_assert (TYPE_UNSIGNED (type));
4742 : 13128 : r = int_range<1> (type, new_lb, new_ub);
4743 : 13128 : }
4744 : :
4745 : : void
4746 : 11750 : operator_absu::update_bitmask (irange &r, const irange &lh,
4747 : : const irange &rh) const
4748 : : {
4749 : 11750 : update_known_bitmask (r, ABSU_EXPR, lh, rh);
4750 : 11750 : }
4751 : :
4752 : :
4753 : : bool
4754 : 580328 : operator_negate::fold_range (irange &r, tree type,
4755 : : const irange &lh,
4756 : : const irange &rh,
4757 : : relation_trio) const
4758 : : {
4759 : 580328 : if (empty_range_varying (r, type, lh, rh))
4760 : 932 : return true;
4761 : :
4762 : : // -X is simply 0 - X.
4763 : 579396 : int_range<1> zero;
4764 : 579396 : zero.set_zero (type);
4765 : 579396 : return range_op_handler (MINUS_EXPR).fold_range (r, type, zero, lh);
4766 : 579396 : }
4767 : :
4768 : : bool
4769 : 74104 : operator_negate::op1_range (irange &r, tree type,
4770 : : const irange &lhs,
4771 : : const irange &op2,
4772 : : relation_trio) const
4773 : : {
4774 : : // NEGATE is involutory.
4775 : 74104 : return fold_range (r, type, lhs, op2);
4776 : : }
4777 : :
4778 : :
4779 : : bool
4780 : 0 : operator_addr_expr::fold_range (irange &r, tree type,
4781 : : const irange &lh,
4782 : : const irange &rh,
4783 : : relation_trio) const
4784 : : {
4785 : 0 : if (empty_range_varying (r, type, lh, rh))
4786 : 0 : return true;
4787 : :
4788 : : // Return a non-null pointer of the LHS type (passed in op2).
4789 : 0 : if (lh.zero_p ())
4790 : 0 : r.set_zero (type);
4791 : 0 : else if (lh.undefined_p () || contains_zero_p (lh))
4792 : 0 : r.set_varying (type);
4793 : : else
4794 : 0 : r.set_nonzero (type);
4795 : : return true;
4796 : : }
4797 : :
4798 : : bool
4799 : 0 : operator_addr_expr::op1_range (irange &r, tree type,
4800 : : const irange &lhs,
4801 : : const irange &op2,
4802 : : relation_trio) const
4803 : : {
4804 : 0 : if (empty_range_varying (r, type, lhs, op2))
4805 : 0 : return true;
4806 : :
4807 : : // Return a non-null pointer of the LHS type (passed in op2), but only
4808 : : // if we cant overflow, eitherwise a no-zero offset could wrap to zero.
4809 : : // See PR 111009.
4810 : 0 : if (!lhs.undefined_p () && !contains_zero_p (lhs) && TYPE_OVERFLOW_UNDEFINED (type))
4811 : 0 : r.set_nonzero (type);
4812 : : else
4813 : 0 : r.set_varying (type);
4814 : : return true;
4815 : : }
4816 : :
4817 : : // Initialize any integral operators to the primary table
4818 : :
4819 : : void
4820 : 285306 : range_op_table::initialize_integral_ops ()
4821 : : {
4822 : 285306 : set (TRUNC_DIV_EXPR, op_trunc_div);
4823 : 285306 : set (FLOOR_DIV_EXPR, op_floor_div);
4824 : 285306 : set (ROUND_DIV_EXPR, op_round_div);
4825 : 285306 : set (CEIL_DIV_EXPR, op_ceil_div);
4826 : 285306 : set (EXACT_DIV_EXPR, op_exact_div);
4827 : 285306 : set (LSHIFT_EXPR, op_lshift);
4828 : 285306 : set (RSHIFT_EXPR, op_rshift);
4829 : 285306 : set (TRUTH_AND_EXPR, op_logical_and);
4830 : 285306 : set (TRUTH_OR_EXPR, op_logical_or);
4831 : 285306 : set (TRUNC_MOD_EXPR, op_trunc_mod);
4832 : 285306 : set (TRUTH_NOT_EXPR, op_logical_not);
4833 : 285306 : set (IMAGPART_EXPR, op_unknown);
4834 : 285306 : set (REALPART_EXPR, op_unknown);
4835 : 285306 : set (ABSU_EXPR, op_absu);
4836 : 285306 : set (OP_WIDEN_MULT_SIGNED, op_widen_mult_signed);
4837 : 285306 : set (OP_WIDEN_MULT_UNSIGNED, op_widen_mult_unsigned);
4838 : 285306 : set (OP_WIDEN_MULT_SIGNED_UNSIGNED, op_widen_mult_signed_unsigned);
4839 : 285306 : set (OP_WIDEN_PLUS_SIGNED, op_widen_plus_signed);
4840 : 285306 : set (OP_WIDEN_PLUS_UNSIGNED, op_widen_plus_unsigned);
4841 : :
4842 : 285306 : }
4843 : :
4844 : : bool
4845 : 41090 : operator_plus::overflow_free_p (const irange &lh, const irange &rh,
4846 : : relation_trio) const
4847 : : {
4848 : 41090 : if (lh.undefined_p () || rh.undefined_p ())
4849 : : return false;
4850 : :
4851 : 41090 : tree type = lh.type ();
4852 : 41090 : if (TYPE_OVERFLOW_UNDEFINED (type))
4853 : : return true;
4854 : :
4855 : 10713 : wi::overflow_type ovf;
4856 : 10713 : signop sgn = TYPE_SIGN (type);
4857 : 10713 : wide_int wmax0 = lh.upper_bound ();
4858 : 10713 : wide_int wmax1 = rh.upper_bound ();
4859 : 10713 : wi::add (wmax0, wmax1, sgn, &ovf);
4860 : 10713 : if (ovf != wi::OVF_NONE)
4861 : : return false;
4862 : :
4863 : 1288 : if (TYPE_UNSIGNED (type))
4864 : : return true;
4865 : :
4866 : 372 : wide_int wmin0 = lh.lower_bound ();
4867 : 372 : wide_int wmin1 = rh.lower_bound ();
4868 : 372 : wi::add (wmin0, wmin1, sgn, &ovf);
4869 : 372 : if (ovf != wi::OVF_NONE)
4870 : 261 : return false;
4871 : :
4872 : : return true;
4873 : 11085 : }
4874 : :
4875 : : bool
4876 : 31 : operator_minus::overflow_free_p (const irange &lh, const irange &rh,
4877 : : relation_trio) const
4878 : : {
4879 : 31 : if (lh.undefined_p () || rh.undefined_p ())
4880 : : return false;
4881 : :
4882 : 31 : tree type = lh.type ();
4883 : 31 : if (TYPE_OVERFLOW_UNDEFINED (type))
4884 : : return true;
4885 : :
4886 : 10 : wi::overflow_type ovf;
4887 : 10 : signop sgn = TYPE_SIGN (type);
4888 : 10 : wide_int wmin0 = lh.lower_bound ();
4889 : 10 : wide_int wmax1 = rh.upper_bound ();
4890 : 10 : wi::sub (wmin0, wmax1, sgn, &ovf);
4891 : 10 : if (ovf != wi::OVF_NONE)
4892 : : return false;
4893 : :
4894 : 7 : if (TYPE_UNSIGNED (type))
4895 : : return true;
4896 : :
4897 : 6 : wide_int wmax0 = lh.upper_bound ();
4898 : 6 : wide_int wmin1 = rh.lower_bound ();
4899 : 6 : wi::sub (wmax0, wmin1, sgn, &ovf);
4900 : 6 : if (ovf != wi::OVF_NONE)
4901 : 0 : return false;
4902 : :
4903 : : return true;
4904 : 16 : }
4905 : :
4906 : : bool
4907 : 3260 : operator_mult::overflow_free_p (const irange &lh, const irange &rh,
4908 : : relation_trio) const
4909 : : {
4910 : 3260 : if (lh.undefined_p () || rh.undefined_p ())
4911 : : return false;
4912 : :
4913 : 3260 : tree type = lh.type ();
4914 : 3260 : if (TYPE_OVERFLOW_UNDEFINED (type))
4915 : : return true;
4916 : :
4917 : 2778 : wi::overflow_type ovf;
4918 : 2778 : signop sgn = TYPE_SIGN (type);
4919 : 2778 : wide_int wmax0 = lh.upper_bound ();
4920 : 2778 : wide_int wmax1 = rh.upper_bound ();
4921 : 2778 : wi::mul (wmax0, wmax1, sgn, &ovf);
4922 : 2778 : if (ovf != wi::OVF_NONE)
4923 : : return false;
4924 : :
4925 : 105 : if (TYPE_UNSIGNED (type))
4926 : : return true;
4927 : :
4928 : 22 : wide_int wmin0 = lh.lower_bound ();
4929 : 22 : wide_int wmin1 = rh.lower_bound ();
4930 : 22 : wi::mul (wmin0, wmin1, sgn, &ovf);
4931 : 22 : if (ovf != wi::OVF_NONE)
4932 : : return false;
4933 : :
4934 : 22 : wi::mul (wmin0, wmax1, sgn, &ovf);
4935 : 22 : if (ovf != wi::OVF_NONE)
4936 : : return false;
4937 : :
4938 : 22 : wi::mul (wmax0, wmin1, sgn, &ovf);
4939 : 22 : if (ovf != wi::OVF_NONE)
4940 : : return false;
4941 : :
4942 : : return true;
4943 : 2800 : }
4944 : :
4945 : : #if CHECKING_P
4946 : : #include "selftest.h"
4947 : :
4948 : : namespace selftest
4949 : : {
4950 : : #define INT(x) wi::shwi ((x), TYPE_PRECISION (integer_type_node))
4951 : : #define UINT(x) wi::uhwi ((x), TYPE_PRECISION (unsigned_type_node))
4952 : : #define INT16(x) wi::shwi ((x), TYPE_PRECISION (short_integer_type_node))
4953 : : #define UINT16(x) wi::uhwi ((x), TYPE_PRECISION (short_unsigned_type_node))
4954 : : #define SCHAR(x) wi::shwi ((x), TYPE_PRECISION (signed_char_type_node))
4955 : : #define UCHAR(x) wi::uhwi ((x), TYPE_PRECISION (unsigned_char_type_node))
4956 : :
4957 : : static void
4958 : 4 : range_op_cast_tests ()
4959 : : {
4960 : 4 : int_range<2> r0, r1, r2, rold;
4961 : 4 : r0.set_varying (integer_type_node);
4962 : 4 : wide_int maxint = r0.upper_bound ();
4963 : :
4964 : : // If a range is in any way outside of the range for the converted
4965 : : // to range, default to the range for the new type.
4966 : 4 : r0.set_varying (short_integer_type_node);
4967 : 4 : wide_int minshort = r0.lower_bound ();
4968 : 4 : wide_int maxshort = r0.upper_bound ();
4969 : 4 : if (TYPE_PRECISION (integer_type_node)
4970 : 4 : > TYPE_PRECISION (short_integer_type_node))
4971 : : {
4972 : 8 : r1 = int_range<1> (integer_type_node,
4973 : 4 : wi::zero (TYPE_PRECISION (integer_type_node)),
4974 : 8 : maxint);
4975 : 4 : range_cast (r1, short_integer_type_node);
4976 : 4 : ASSERT_TRUE (r1.lower_bound () == minshort
4977 : : && r1.upper_bound() == maxshort);
4978 : : }
4979 : :
4980 : : // (unsigned char)[-5,-1] => [251,255].
4981 : 4 : r0 = rold = int_range<1> (signed_char_type_node, SCHAR (-5), SCHAR (-1));
4982 : 4 : range_cast (r0, unsigned_char_type_node);
4983 : 4 : ASSERT_TRUE (r0 == int_range<1> (unsigned_char_type_node,
4984 : : UCHAR (251), UCHAR (255)));
4985 : 4 : range_cast (r0, signed_char_type_node);
4986 : 4 : ASSERT_TRUE (r0 == rold);
4987 : :
4988 : : // (signed char)[15, 150] => [-128,-106][15,127].
4989 : 4 : r0 = rold = int_range<1> (unsigned_char_type_node, UCHAR (15), UCHAR (150));
4990 : 4 : range_cast (r0, signed_char_type_node);
4991 : 4 : r1 = int_range<1> (signed_char_type_node, SCHAR (15), SCHAR (127));
4992 : 4 : r2 = int_range<1> (signed_char_type_node, SCHAR (-128), SCHAR (-106));
4993 : 4 : r1.union_ (r2);
4994 : 4 : ASSERT_TRUE (r1 == r0);
4995 : 4 : range_cast (r0, unsigned_char_type_node);
4996 : 4 : ASSERT_TRUE (r0 == rold);
4997 : :
4998 : : // (unsigned char)[-5, 5] => [0,5][251,255].
4999 : 4 : r0 = rold = int_range<1> (signed_char_type_node, SCHAR (-5), SCHAR (5));
5000 : 4 : range_cast (r0, unsigned_char_type_node);
5001 : 4 : r1 = int_range<1> (unsigned_char_type_node, UCHAR (251), UCHAR (255));
5002 : 4 : r2 = int_range<1> (unsigned_char_type_node, UCHAR (0), UCHAR (5));
5003 : 4 : r1.union_ (r2);
5004 : 4 : ASSERT_TRUE (r0 == r1);
5005 : 4 : range_cast (r0, signed_char_type_node);
5006 : 4 : ASSERT_TRUE (r0 == rold);
5007 : :
5008 : : // (unsigned char)[-5,5] => [0,5][251,255].
5009 : 4 : r0 = int_range<1> (integer_type_node, INT (-5), INT (5));
5010 : 4 : range_cast (r0, unsigned_char_type_node);
5011 : 4 : r1 = int_range<1> (unsigned_char_type_node, UCHAR (0), UCHAR (5));
5012 : 4 : r1.union_ (int_range<1> (unsigned_char_type_node, UCHAR (251), UCHAR (255)));
5013 : 4 : ASSERT_TRUE (r0 == r1);
5014 : :
5015 : : // (unsigned char)[5U,1974U] => [0,255].
5016 : 4 : r0 = int_range<1> (unsigned_type_node, UINT (5), UINT (1974));
5017 : 4 : range_cast (r0, unsigned_char_type_node);
5018 : 4 : ASSERT_TRUE (r0 == int_range<1> (unsigned_char_type_node, UCHAR (0), UCHAR (255)));
5019 : 4 : range_cast (r0, integer_type_node);
5020 : : // Going to a wider range should not sign extend.
5021 : 4 : ASSERT_TRUE (r0 == int_range<1> (integer_type_node, INT (0), INT (255)));
5022 : :
5023 : : // (unsigned char)[-350,15] => [0,255].
5024 : 4 : r0 = int_range<1> (integer_type_node, INT (-350), INT (15));
5025 : 4 : range_cast (r0, unsigned_char_type_node);
5026 : 4 : ASSERT_TRUE (r0 == (int_range<1>
5027 : : (unsigned_char_type_node,
5028 : : min_limit (unsigned_char_type_node),
5029 : : max_limit (unsigned_char_type_node))));
5030 : :
5031 : : // Casting [-120,20] from signed char to unsigned short.
5032 : : // => [0, 20][0xff88, 0xffff].
5033 : 4 : r0 = int_range<1> (signed_char_type_node, SCHAR (-120), SCHAR (20));
5034 : 4 : range_cast (r0, short_unsigned_type_node);
5035 : 4 : r1 = int_range<1> (short_unsigned_type_node, UINT16 (0), UINT16 (20));
5036 : 8 : r2 = int_range<1> (short_unsigned_type_node,
5037 : 12 : UINT16 (0xff88), UINT16 (0xffff));
5038 : 4 : r1.union_ (r2);
5039 : 4 : ASSERT_TRUE (r0 == r1);
5040 : : // A truncating cast back to signed char will work because [-120, 20]
5041 : : // is representable in signed char.
5042 : 4 : range_cast (r0, signed_char_type_node);
5043 : 4 : ASSERT_TRUE (r0 == int_range<1> (signed_char_type_node,
5044 : : SCHAR (-120), SCHAR (20)));
5045 : :
5046 : : // unsigned char -> signed short
5047 : : // (signed short)[(unsigned char)25, (unsigned char)250]
5048 : : // => [(signed short)25, (signed short)250]
5049 : 4 : r0 = rold = int_range<1> (unsigned_char_type_node, UCHAR (25), UCHAR (250));
5050 : 4 : range_cast (r0, short_integer_type_node);
5051 : 4 : r1 = int_range<1> (short_integer_type_node, INT16 (25), INT16 (250));
5052 : 4 : ASSERT_TRUE (r0 == r1);
5053 : 4 : range_cast (r0, unsigned_char_type_node);
5054 : 4 : ASSERT_TRUE (r0 == rold);
5055 : :
5056 : : // Test casting a wider signed [-MIN,MAX] to a narrower unsigned.
5057 : 8 : r0 = int_range<1> (long_long_integer_type_node,
5058 : 4 : min_limit (long_long_integer_type_node),
5059 : 8 : max_limit (long_long_integer_type_node));
5060 : 4 : range_cast (r0, short_unsigned_type_node);
5061 : 8 : r1 = int_range<1> (short_unsigned_type_node,
5062 : 4 : min_limit (short_unsigned_type_node),
5063 : 8 : max_limit (short_unsigned_type_node));
5064 : 4 : ASSERT_TRUE (r0 == r1);
5065 : :
5066 : : // Casting NONZERO to a narrower type will wrap/overflow so
5067 : : // it's just the entire range for the narrower type.
5068 : : //
5069 : : // "NOT 0 at signed 32-bits" ==> [-MIN_32,-1][1, +MAX_32]. This is
5070 : : // is outside of the range of a smaller range, return the full
5071 : : // smaller range.
5072 : 4 : if (TYPE_PRECISION (integer_type_node)
5073 : 4 : > TYPE_PRECISION (short_integer_type_node))
5074 : : {
5075 : 4 : r0.set_nonzero (integer_type_node);
5076 : 4 : range_cast (r0, short_integer_type_node);
5077 : 8 : r1 = int_range<1> (short_integer_type_node,
5078 : 4 : min_limit (short_integer_type_node),
5079 : 8 : max_limit (short_integer_type_node));
5080 : 4 : ASSERT_TRUE (r0 == r1);
5081 : : }
5082 : :
5083 : : // Casting NONZERO from a narrower signed to a wider signed.
5084 : : //
5085 : : // NONZERO signed 16-bits is [-MIN_16,-1][1, +MAX_16].
5086 : : // Converting this to 32-bits signed is [-MIN_16,-1][1, +MAX_16].
5087 : 4 : r0.set_nonzero (short_integer_type_node);
5088 : 4 : range_cast (r0, integer_type_node);
5089 : 4 : r1 = int_range<1> (integer_type_node, INT (-32768), INT (-1));
5090 : 4 : r2 = int_range<1> (integer_type_node, INT (1), INT (32767));
5091 : 4 : r1.union_ (r2);
5092 : 4 : ASSERT_TRUE (r0 == r1);
5093 : 4 : }
5094 : :
5095 : : static void
5096 : 4 : range_op_lshift_tests ()
5097 : : {
5098 : : // Test that 0x808.... & 0x8.... still contains 0x8....
5099 : : // for a large set of numbers.
5100 : 4 : {
5101 : 4 : int_range_max res;
5102 : 4 : tree big_type = long_long_unsigned_type_node;
5103 : 4 : unsigned big_prec = TYPE_PRECISION (big_type);
5104 : : // big_num = 0x808,0000,0000,0000
5105 : 4 : wide_int big_num = wi::lshift (wi::uhwi (0x808, big_prec),
5106 : 8 : wi::uhwi (48, big_prec));
5107 : 8 : op_bitwise_and.fold_range (res, big_type,
5108 : 8 : int_range <1> (big_type),
5109 : 8 : int_range <1> (big_type, big_num, big_num));
5110 : : // val = 0x8,0000,0000,0000
5111 : 4 : wide_int val = wi::lshift (wi::uhwi (8, big_prec),
5112 : 8 : wi::uhwi (48, big_prec));
5113 : 4 : ASSERT_TRUE (res.contains_p (val));
5114 : 4 : }
5115 : :
5116 : 4 : if (TYPE_PRECISION (unsigned_type_node) > 31)
5117 : : {
5118 : : // unsigned VARYING = op1 << 1 should be VARYING.
5119 : 4 : int_range<2> lhs (unsigned_type_node);
5120 : 4 : int_range<2> shift (unsigned_type_node, INT (1), INT (1));
5121 : 4 : int_range_max op1;
5122 : 4 : op_lshift.op1_range (op1, unsigned_type_node, lhs, shift);
5123 : 4 : ASSERT_TRUE (op1.varying_p ());
5124 : :
5125 : : // 0 = op1 << 1 should be [0,0], [0x8000000, 0x8000000].
5126 : 4 : int_range<2> zero (unsigned_type_node, UINT (0), UINT (0));
5127 : 4 : op_lshift.op1_range (op1, unsigned_type_node, zero, shift);
5128 : 4 : ASSERT_TRUE (op1.num_pairs () == 2);
5129 : : // Remove the [0,0] range.
5130 : 4 : op1.intersect (zero);
5131 : 4 : ASSERT_TRUE (op1.num_pairs () == 1);
5132 : : // op1 << 1 should be [0x8000,0x8000] << 1,
5133 : : // which should result in [0,0].
5134 : 4 : int_range_max result;
5135 : 4 : op_lshift.fold_range (result, unsigned_type_node, op1, shift);
5136 : 4 : ASSERT_TRUE (result == zero);
5137 : 4 : }
5138 : : // signed VARYING = op1 << 1 should be VARYING.
5139 : 4 : if (TYPE_PRECISION (integer_type_node) > 31)
5140 : : {
5141 : : // unsigned VARYING = op1 << 1 should be VARYING.
5142 : 4 : int_range<2> lhs (integer_type_node);
5143 : 4 : int_range<2> shift (integer_type_node, INT (1), INT (1));
5144 : 4 : int_range_max op1;
5145 : 4 : op_lshift.op1_range (op1, integer_type_node, lhs, shift);
5146 : 4 : ASSERT_TRUE (op1.varying_p ());
5147 : :
5148 : : // 0 = op1 << 1 should be [0,0], [0x8000000, 0x8000000].
5149 : 4 : int_range<2> zero (integer_type_node, INT (0), INT (0));
5150 : 4 : op_lshift.op1_range (op1, integer_type_node, zero, shift);
5151 : 4 : ASSERT_TRUE (op1.num_pairs () == 2);
5152 : : // Remove the [0,0] range.
5153 : 4 : op1.intersect (zero);
5154 : 4 : ASSERT_TRUE (op1.num_pairs () == 1);
5155 : : // op1 << 1 should be [0x8000,0x8000] << 1,
5156 : : // which should result in [0,0].
5157 : 4 : int_range_max result;
5158 : 4 : op_lshift.fold_range (result, unsigned_type_node, op1, shift);
5159 : 4 : ASSERT_TRUE (result == zero);
5160 : 4 : }
5161 : 4 : }
5162 : :
5163 : : static void
5164 : 4 : range_op_rshift_tests ()
5165 : : {
5166 : : // unsigned: [3, MAX] = OP1 >> 1
5167 : 4 : {
5168 : 4 : int_range_max lhs (unsigned_type_node,
5169 : 4 : UINT (3), max_limit (unsigned_type_node));
5170 : 4 : int_range_max one (unsigned_type_node,
5171 : 8 : wi::one (TYPE_PRECISION (unsigned_type_node)),
5172 : 8 : wi::one (TYPE_PRECISION (unsigned_type_node)));
5173 : 4 : int_range_max op1;
5174 : 4 : op_rshift.op1_range (op1, unsigned_type_node, lhs, one);
5175 : 4 : ASSERT_FALSE (op1.contains_p (UINT (3)));
5176 : 4 : }
5177 : :
5178 : : // signed: [3, MAX] = OP1 >> 1
5179 : 4 : {
5180 : 4 : int_range_max lhs (integer_type_node,
5181 : 4 : INT (3), max_limit (integer_type_node));
5182 : 4 : int_range_max one (integer_type_node, INT (1), INT (1));
5183 : 4 : int_range_max op1;
5184 : 4 : op_rshift.op1_range (op1, integer_type_node, lhs, one);
5185 : 4 : ASSERT_FALSE (op1.contains_p (INT (-2)));
5186 : 4 : }
5187 : :
5188 : : // This is impossible, so OP1 should be [].
5189 : : // signed: [MIN, MIN] = OP1 >> 1
5190 : 4 : {
5191 : 4 : int_range_max lhs (integer_type_node,
5192 : 4 : min_limit (integer_type_node),
5193 : 4 : min_limit (integer_type_node));
5194 : 4 : int_range_max one (integer_type_node, INT (1), INT (1));
5195 : 4 : int_range_max op1;
5196 : 4 : op_rshift.op1_range (op1, integer_type_node, lhs, one);
5197 : 4 : ASSERT_TRUE (op1.undefined_p ());
5198 : 4 : }
5199 : :
5200 : : // signed: ~[-1] = OP1 >> 31
5201 : 4 : if (TYPE_PRECISION (integer_type_node) > 31)
5202 : : {
5203 : 4 : int_range_max lhs (integer_type_node, INT (-1), INT (-1), VR_ANTI_RANGE);
5204 : 4 : int_range_max shift (integer_type_node, INT (31), INT (31));
5205 : 4 : int_range_max op1;
5206 : 4 : op_rshift.op1_range (op1, integer_type_node, lhs, shift);
5207 : 4 : int_range_max negatives = range_negatives (integer_type_node);
5208 : 4 : negatives.intersect (op1);
5209 : 4 : ASSERT_TRUE (negatives.undefined_p ());
5210 : 4 : }
5211 : 4 : }
5212 : :
5213 : : static void
5214 : 4 : range_op_bitwise_and_tests ()
5215 : : {
5216 : 4 : int_range_max res;
5217 : 4 : wide_int min = min_limit (integer_type_node);
5218 : 4 : wide_int max = max_limit (integer_type_node);
5219 : 4 : wide_int tiny = wi::add (min, wi::one (TYPE_PRECISION (integer_type_node)));
5220 : 4 : int_range_max i1 (integer_type_node, tiny, max);
5221 : 4 : int_range_max i2 (integer_type_node, INT (255), INT (255));
5222 : :
5223 : : // [MIN+1, MAX] = OP1 & 255: OP1 is VARYING
5224 : 4 : op_bitwise_and.op1_range (res, integer_type_node, i1, i2);
5225 : 4 : ASSERT_TRUE (res == int_range<1> (integer_type_node));
5226 : :
5227 : : // VARYING = OP1 & 255: OP1 is VARYING
5228 : 4 : i1 = int_range<1> (integer_type_node);
5229 : 4 : op_bitwise_and.op1_range (res, integer_type_node, i1, i2);
5230 : 4 : ASSERT_TRUE (res == int_range<1> (integer_type_node));
5231 : :
5232 : : // For 0 = x & MASK, x is ~MASK.
5233 : 4 : {
5234 : 4 : int_range<2> zero (integer_type_node, INT (0), INT (0));
5235 : 4 : int_range<2> mask = int_range<2> (integer_type_node, INT (7), INT (7));
5236 : 4 : op_bitwise_and.op1_range (res, integer_type_node, zero, mask);
5237 : 4 : wide_int inv = wi::shwi (~7U, TYPE_PRECISION (integer_type_node));
5238 : 4 : ASSERT_TRUE (res.get_nonzero_bits () == inv);
5239 : 4 : }
5240 : :
5241 : : // (NONZERO | X) is nonzero.
5242 : 4 : i1.set_nonzero (integer_type_node);
5243 : 4 : i2.set_varying (integer_type_node);
5244 : 4 : op_bitwise_or.fold_range (res, integer_type_node, i1, i2);
5245 : 4 : ASSERT_TRUE (res.nonzero_p ());
5246 : :
5247 : : // (NEGATIVE | X) is nonzero.
5248 : 4 : i1 = int_range<1> (integer_type_node, INT (-5), INT (-3));
5249 : 4 : i2.set_varying (integer_type_node);
5250 : 4 : op_bitwise_or.fold_range (res, integer_type_node, i1, i2);
5251 : 4 : ASSERT_FALSE (res.contains_p (INT (0)));
5252 : 4 : }
5253 : :
5254 : : static void
5255 : 4 : range_relational_tests ()
5256 : : {
5257 : 4 : int_range<2> lhs (unsigned_char_type_node);
5258 : 4 : int_range<2> op1 (unsigned_char_type_node, UCHAR (8), UCHAR (10));
5259 : 4 : int_range<2> op2 (unsigned_char_type_node, UCHAR (20), UCHAR (20));
5260 : :
5261 : : // Never wrapping additions mean LHS > OP1.
5262 : 4 : relation_kind code = op_plus.lhs_op1_relation (lhs, op1, op2, VREL_VARYING);
5263 : 4 : ASSERT_TRUE (code == VREL_GT);
5264 : :
5265 : : // Most wrapping additions mean nothing...
5266 : 4 : op1 = int_range<2> (unsigned_char_type_node, UCHAR (8), UCHAR (10));
5267 : 4 : op2 = int_range<2> (unsigned_char_type_node, UCHAR (0), UCHAR (255));
5268 : 4 : code = op_plus.lhs_op1_relation (lhs, op1, op2, VREL_VARYING);
5269 : 4 : ASSERT_TRUE (code == VREL_VARYING);
5270 : :
5271 : : // However, always wrapping additions mean LHS < OP1.
5272 : 4 : op1 = int_range<2> (unsigned_char_type_node, UCHAR (1), UCHAR (255));
5273 : 4 : op2 = int_range<2> (unsigned_char_type_node, UCHAR (255), UCHAR (255));
5274 : 4 : code = op_plus.lhs_op1_relation (lhs, op1, op2, VREL_VARYING);
5275 : 4 : ASSERT_TRUE (code == VREL_LT);
5276 : 4 : }
5277 : :
5278 : : void
5279 : 4 : range_op_tests ()
5280 : : {
5281 : 4 : range_op_rshift_tests ();
5282 : 4 : range_op_lshift_tests ();
5283 : 4 : range_op_bitwise_and_tests ();
5284 : 4 : range_op_cast_tests ();
5285 : 4 : range_relational_tests ();
5286 : :
5287 : 4 : extern void range_op_float_tests ();
5288 : 4 : range_op_float_tests ();
5289 : 4 : }
5290 : :
5291 : : } // namespace selftest
5292 : :
5293 : : #endif // CHECKING_P
|