Line data Source code
1 : /* Definitions of the pointer_query and related classes.
2 :
3 : Copyright (C) 2020-2026 Free Software Foundation, Inc.
4 :
5 : This file is part of GCC.
6 :
7 : GCC is free software; you can redistribute it and/or modify it under
8 : the terms of the GNU General Public License as published by the Free
9 : Software Foundation; either version 3, or (at your option) any later
10 : version.
11 :
12 : GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 : WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 : FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 : for more details.
16 :
17 : You should have received a copy of the GNU General Public License
18 : along with GCC; see the file COPYING3. If not see
19 : <http://www.gnu.org/licenses/>. */
20 :
21 : #include "config.h"
22 : #include "system.h"
23 : #include "coretypes.h"
24 : #include "backend.h"
25 : #include "tree.h"
26 : #include "gimple.h"
27 : #include "stringpool.h"
28 : #include "tree-vrp.h"
29 : #include "diagnostic-core.h"
30 : #include "fold-const.h"
31 : #include "tree-object-size.h"
32 : #include "tree-ssa-strlen.h"
33 : #include "langhooks.h"
34 : #include "attribs.h"
35 : #include "gimple-iterator.h"
36 : #include "gimple-fold.h"
37 : #include "gimple-ssa.h"
38 : #include "intl.h"
39 : #include "attr-fnspec.h"
40 : #include "gimple-range.h"
41 : #include "pointer-query.h"
42 : #include "tree-pretty-print.h"
43 : #include "tree-ssanames.h"
44 : #include "target.h"
45 :
46 : static bool compute_objsize_r (tree, gimple *, bool, int, access_ref *,
47 : ssa_name_limit_t &, pointer_query *);
48 :
49 : /* Wrapper around the wide_int overload of get_range that accepts
50 : offset_int instead. For middle end expressions returns the same
51 : result. For a subset of nonconstamt expressions emitted by the front
52 : end determines a more precise range than would be possible otherwise. */
53 :
54 : static bool
55 4842912 : get_offset_range (tree x, gimple *stmt, offset_int r[2], range_query *rvals)
56 : {
57 4842912 : offset_int add = 0;
58 4842912 : if (TREE_CODE (x) == PLUS_EXPR)
59 : {
60 : /* Handle constant offsets in pointer addition expressions seen
61 : n the front end IL. */
62 39 : tree op = TREE_OPERAND (x, 1);
63 39 : if (TREE_CODE (op) == INTEGER_CST)
64 : {
65 39 : op = fold_convert (signed_type_for (TREE_TYPE (op)), op);
66 39 : add = wi::to_offset (op);
67 39 : x = TREE_OPERAND (x, 0);
68 : }
69 : }
70 :
71 4842912 : if (TREE_CODE (x) == NOP_EXPR)
72 : /* Also handle conversions to sizetype seen in the front end IL. */
73 119 : x = TREE_OPERAND (x, 0);
74 :
75 4842912 : tree type = TREE_TYPE (x);
76 4842912 : if ((!INTEGRAL_TYPE_P (type)
77 : /* ??? We get along without caring about overflow by using
78 : offset_int, but that falls apart when indexes are bigger
79 : than pointer differences. */
80 1771498 : || TYPE_PRECISION (type) > TYPE_PRECISION (ptrdiff_type_node))
81 4843078 : && !POINTER_TYPE_P (type))
82 : return false;
83 :
84 4842737 : if (TREE_CODE (x) != INTEGER_CST
85 711512 : && TREE_CODE (x) != SSA_NAME)
86 : {
87 182 : if (TYPE_UNSIGNED (type)
88 182 : && TYPE_PRECISION (type) == TYPE_PRECISION (sizetype))
89 50 : type = signed_type_for (type);
90 :
91 182 : r[0] = wi::to_offset (TYPE_MIN_VALUE (type)) + add;
92 182 : r[1] = wi::to_offset (TYPE_MAX_VALUE (type)) + add;
93 182 : return x;
94 : }
95 :
96 24212775 : wide_int wr[2];
97 4842555 : if (!get_range (x, stmt, wr, rvals))
98 : return false;
99 :
100 4673019 : signop sgn = SIGNED;
101 : /* Only convert signed integers or unsigned sizetype to a signed
102 : offset and avoid converting large positive values in narrower
103 : types to negative offsets. */
104 4673019 : if (TYPE_UNSIGNED (type)
105 4673019 : && wr[0].get_precision () < TYPE_PRECISION (sizetype))
106 : sgn = UNSIGNED;
107 :
108 4673019 : r[0] = offset_int::from (wr[0], sgn);
109 4673019 : r[1] = offset_int::from (wr[1], sgn);
110 4673019 : return true;
111 14527665 : }
112 :
113 : /* Return the argument that the call STMT to a built-in function returns
114 : or null if it doesn't. On success, set OFFRNG[] to the range of offsets
115 : from the argument reflected in the value returned by the built-in if it
116 : can be determined, otherwise to 0 and HWI_M1U respectively. Set
117 : *PAST_END for functions like mempcpy that might return a past the end
118 : pointer (most functions return a dereferenceable pointer to an existing
119 : element of an array). */
120 :
121 : static tree
122 343909 : gimple_call_return_array (gimple *stmt, offset_int offrng[2], bool *past_end,
123 : ssa_name_limit_t &snlim, pointer_query *qry)
124 : {
125 : /* Clear and set below for the rare function(s) that might return
126 : a past-the-end pointer. */
127 343909 : *past_end = false;
128 :
129 343909 : {
130 : /* Check for attribute fn spec to see if the function returns one
131 : of its arguments. */
132 343909 : attr_fnspec fnspec = gimple_call_fnspec (as_a <gcall *>(stmt));
133 343909 : unsigned int argno;
134 343909 : if (fnspec.returns_arg (&argno))
135 : {
136 : /* Functions return the first argument (not a range). */
137 5413 : offrng[0] = offrng[1] = 0;
138 5413 : return gimple_call_arg (stmt, argno);
139 : }
140 : }
141 :
142 338496 : if (gimple_call_num_args (stmt) < 1)
143 : return NULL_TREE;
144 :
145 325889 : tree fn = gimple_call_fndecl (stmt);
146 325889 : if (!gimple_call_builtin_p (stmt, BUILT_IN_NORMAL))
147 : {
148 : /* See if this is a call to placement new. */
149 253260 : if (!fn
150 237295 : || !DECL_IS_OPERATOR_NEW_P (fn)
151 262519 : || DECL_IS_REPLACEABLE_OPERATOR_NEW_P (fn))
152 : return NULL_TREE;
153 :
154 : /* Check the mangling, keeping in mind that operator new takes
155 : a size_t which could be unsigned int or unsigned long. */
156 9259 : tree fname = DECL_ASSEMBLER_NAME (fn);
157 9259 : if (!id_equal (fname, "_ZnwjPv") // ordinary form
158 9208 : && !id_equal (fname, "_ZnwmPv") // ordinary form
159 777 : && !id_equal (fname, "_ZnajPv") // array form
160 10032 : && !id_equal (fname, "_ZnamPv")) // array form
161 : return NULL_TREE;
162 :
163 8581 : if (gimple_call_num_args (stmt) != 2)
164 : return NULL_TREE;
165 :
166 : /* Allocation functions return a pointer to the beginning. */
167 8581 : offrng[0] = offrng[1] = 0;
168 8581 : return gimple_call_arg (stmt, 1);
169 : }
170 :
171 72629 : switch (DECL_FUNCTION_CODE (fn))
172 : {
173 0 : case BUILT_IN_MEMCPY:
174 0 : case BUILT_IN_MEMCPY_CHK:
175 0 : case BUILT_IN_MEMMOVE:
176 0 : case BUILT_IN_MEMMOVE_CHK:
177 0 : case BUILT_IN_MEMSET:
178 0 : case BUILT_IN_STRCAT:
179 0 : case BUILT_IN_STRCAT_CHK:
180 0 : case BUILT_IN_STRCPY:
181 0 : case BUILT_IN_STRCPY_CHK:
182 0 : case BUILT_IN_STRNCAT:
183 0 : case BUILT_IN_STRNCAT_CHK:
184 0 : case BUILT_IN_STRNCPY:
185 0 : case BUILT_IN_STRNCPY_CHK:
186 : /* Functions return the first argument (not a range). */
187 0 : offrng[0] = offrng[1] = 0;
188 0 : return gimple_call_arg (stmt, 0);
189 :
190 211 : case BUILT_IN_MEMPCPY:
191 211 : case BUILT_IN_MEMPCPY_CHK:
192 211 : {
193 : /* The returned pointer is in a range constrained by the smaller
194 : of the upper bound of the size argument and the source object
195 : size. */
196 211 : offrng[0] = 0;
197 211 : offrng[1] = HOST_WIDE_INT_M1U;
198 211 : tree off = gimple_call_arg (stmt, 2);
199 211 : bool off_valid = get_offset_range (off, stmt, offrng, qry->rvals);
200 211 : if (!off_valid || offrng[0] != offrng[1])
201 : {
202 : /* If the offset is either indeterminate or in some range,
203 : try to constrain its upper bound to at most the size
204 : of the source object. */
205 92 : access_ref aref;
206 92 : tree src = gimple_call_arg (stmt, 1);
207 92 : if (compute_objsize_r (src, stmt, false, 1, &aref, snlim, qry)
208 92 : && aref.sizrng[1] < offrng[1])
209 36 : offrng[1] = aref.sizrng[1];
210 : }
211 :
212 : /* Mempcpy may return a past-the-end pointer. */
213 211 : *past_end = true;
214 211 : return gimple_call_arg (stmt, 0);
215 : }
216 :
217 2706 : case BUILT_IN_MEMCHR:
218 2706 : {
219 2706 : tree off = gimple_call_arg (stmt, 2);
220 2706 : if (get_offset_range (off, stmt, offrng, qry->rvals))
221 2648 : offrng[1] -= 1;
222 : else
223 58 : offrng[1] = HOST_WIDE_INT_M1U;
224 :
225 2706 : offrng[0] = 0;
226 2706 : return gimple_call_arg (stmt, 0);
227 : }
228 :
229 362 : case BUILT_IN_STRCHR:
230 362 : case BUILT_IN_STRRCHR:
231 362 : case BUILT_IN_STRSTR:
232 362 : offrng[0] = 0;
233 362 : offrng[1] = HOST_WIDE_INT_M1U;
234 362 : return gimple_call_arg (stmt, 0);
235 :
236 98 : case BUILT_IN_STPCPY:
237 98 : case BUILT_IN_STPCPY_CHK:
238 98 : {
239 98 : access_ref aref;
240 98 : tree src = gimple_call_arg (stmt, 1);
241 98 : if (compute_objsize_r (src, stmt, false, 1, &aref, snlim, qry))
242 98 : offrng[1] = aref.sizrng[1] - 1;
243 : else
244 0 : offrng[1] = HOST_WIDE_INT_M1U;
245 :
246 98 : offrng[0] = 0;
247 98 : return gimple_call_arg (stmt, 0);
248 : }
249 :
250 88 : case BUILT_IN_STPNCPY:
251 88 : case BUILT_IN_STPNCPY_CHK:
252 88 : {
253 : /* The returned pointer is in a range between the first argument
254 : and it plus the smaller of the upper bound of the size argument
255 : and the source object size. */
256 88 : offrng[1] = HOST_WIDE_INT_M1U;
257 88 : tree off = gimple_call_arg (stmt, 2);
258 88 : if (!get_offset_range (off, stmt, offrng, qry->rvals)
259 88 : || offrng[0] != offrng[1])
260 : {
261 : /* If the offset is either indeterminate or in some range,
262 : try to constrain its upper bound to at most the size
263 : of the source object. */
264 13 : access_ref aref;
265 13 : tree src = gimple_call_arg (stmt, 1);
266 13 : if (compute_objsize_r (src, stmt, false, 1, &aref, snlim, qry)
267 13 : && aref.sizrng[1] < offrng[1])
268 13 : offrng[1] = aref.sizrng[1];
269 : }
270 :
271 : /* When the source is the empty string the returned pointer is
272 : a copy of the argument. Otherwise stpcpy can also return
273 : a past-the-end pointer. */
274 88 : offrng[0] = 0;
275 88 : *past_end = true;
276 88 : return gimple_call_arg (stmt, 0);
277 : }
278 :
279 : default:
280 : break;
281 : }
282 :
283 : return NULL_TREE;
284 : }
285 :
286 : /* Return true when EXP's range can be determined and set RANGE[] to it
287 : after adjusting it if necessary to make EXP a represents a valid size
288 : of object, or a valid size argument to an allocation function declared
289 : with attribute alloc_size (whose argument may be signed), or to a string
290 : manipulation function like memset.
291 : When ALLOW_ZERO is set in FLAGS, allow returning a range of [0, 0] for
292 : a size in an anti-range [1, N] where N > PTRDIFF_MAX. A zero range is
293 : a (nearly) invalid argument to allocation functions like malloc but it
294 : is a valid argument to functions like memset.
295 : When USE_LARGEST is set in FLAGS set RANGE to the largest valid subrange
296 : in a multi-range, otherwise to the smallest valid subrange. */
297 :
298 : bool
299 1176040 : get_size_range (range_query *query, tree exp, gimple *stmt, tree range[2],
300 : int flags /* = 0 */)
301 : {
302 1176040 : if (!exp)
303 : return false;
304 :
305 1176040 : if (tree_fits_uhwi_p (exp))
306 : {
307 : /* EXP is a constant. */
308 826046 : range[0] = range[1] = exp;
309 826046 : return true;
310 : }
311 :
312 349994 : tree exptype = TREE_TYPE (exp);
313 349994 : bool integral = INTEGRAL_TYPE_P (exptype);
314 :
315 349994 : wide_int min, max;
316 349994 : enum value_range_kind range_type;
317 :
318 349994 : if (!query)
319 45811 : query = get_range_query (cfun);
320 :
321 349994 : if (integral)
322 : {
323 349972 : int_range_max vr;
324 349972 : tree tmin, tmax;
325 :
326 349972 : query->range_of_expr (vr, exp, stmt);
327 :
328 349972 : if (vr.undefined_p ())
329 6 : vr.set_varying (TREE_TYPE (exp));
330 349972 : range_type = get_legacy_range (vr, tmin, tmax);
331 349972 : min = wi::to_wide (tmin);
332 349972 : max = wi::to_wide (tmax);
333 349972 : }
334 : else
335 : range_type = VR_VARYING;
336 :
337 349972 : if (range_type == VR_VARYING)
338 : {
339 45401 : if (integral)
340 : {
341 : /* Use the full range of the type of the expression when
342 : no value range information is available. */
343 45379 : range[0] = TYPE_MIN_VALUE (exptype);
344 45379 : range[1] = TYPE_MAX_VALUE (exptype);
345 45379 : return true;
346 : }
347 :
348 22 : range[0] = NULL_TREE;
349 22 : range[1] = NULL_TREE;
350 22 : return false;
351 : }
352 :
353 304593 : unsigned expprec = TYPE_PRECISION (exptype);
354 :
355 304593 : bool signed_p = !TYPE_UNSIGNED (exptype);
356 :
357 304593 : if (range_type == VR_ANTI_RANGE)
358 : {
359 22793 : if (signed_p)
360 : {
361 81 : if (wi::les_p (max, 0))
362 : {
363 : /* EXP is not in a strictly negative range. That means
364 : it must be in some (not necessarily strictly) positive
365 : range which includes zero. Since in signed to unsigned
366 : conversions negative values end up converted to large
367 : positive values, and otherwise they are not valid sizes,
368 : the resulting range is in both cases [0, TYPE_MAX]. */
369 15 : min = wi::zero (expprec);
370 15 : max = wi::to_wide (TYPE_MAX_VALUE (exptype));
371 : }
372 66 : else if (wi::les_p (min - 1, 0))
373 : {
374 : /* EXP is not in a negative-positive range. That means EXP
375 : is either negative, or greater than max. Since negative
376 : sizes are invalid make the range [MAX + 1, TYPE_MAX]. */
377 42 : min = max + 1;
378 42 : max = wi::to_wide (TYPE_MAX_VALUE (exptype));
379 : }
380 : else
381 : {
382 24 : max = min - 1;
383 24 : min = wi::zero (expprec);
384 : }
385 : }
386 : else
387 : {
388 22712 : wide_int maxsize = wi::to_wide (max_object_size ());
389 22712 : min = wide_int::from (min, maxsize.get_precision (), UNSIGNED);
390 22712 : max = wide_int::from (max, maxsize.get_precision (), UNSIGNED);
391 22712 : if (wi::eq_p (0, min - 1))
392 : {
393 : /* EXP is unsigned and not in the range [1, MAX]. That means
394 : it's either zero or greater than MAX. Even though 0 would
395 : normally be detected by -Walloc-zero, unless ALLOW_ZERO
396 : is set, set the range to [MAX, TYPE_MAX] so that when MAX
397 : is greater than the limit the whole range is diagnosed. */
398 364 : wide_int maxsize = wi::to_wide (max_object_size ());
399 364 : if (flags & SR_ALLOW_ZERO)
400 : {
401 426 : if (wi::leu_p (maxsize, max + 1)
402 213 : || !(flags & SR_USE_LARGEST))
403 126 : min = max = wi::zero (expprec);
404 : else
405 : {
406 87 : min = max + 1;
407 87 : max = wi::to_wide (TYPE_MAX_VALUE (exptype));
408 : }
409 : }
410 : else
411 : {
412 151 : min = max + 1;
413 151 : max = wi::to_wide (TYPE_MAX_VALUE (exptype));
414 : }
415 364 : }
416 44696 : else if ((flags & SR_USE_LARGEST)
417 32504 : && wi::ltu_p (max + 1, maxsize))
418 : {
419 : /* When USE_LARGEST is set and the larger of the two subranges
420 : is a valid size, use it... */
421 36 : min = max + 1;
422 36 : max = maxsize;
423 : }
424 : else
425 : {
426 : /* ...otherwise use the smaller subrange. */
427 22312 : max = min - 1;
428 22312 : min = wi::zero (expprec);
429 : }
430 22712 : }
431 : }
432 :
433 304593 : range[0] = wide_int_to_tree (exptype, min);
434 304593 : range[1] = wide_int_to_tree (exptype, max);
435 :
436 304593 : return true;
437 349994 : }
438 :
439 : bool
440 52357 : get_size_range (tree exp, tree range[2], int flags /* = 0 */)
441 : {
442 52357 : return get_size_range (/*query=*/NULL, exp, /*stmt=*/NULL, range, flags);
443 : }
444 :
445 : /* If STMT is a call to an allocation function, returns the constant
446 : maximum size of the object allocated by the call represented as
447 : sizetype. If nonnull, sets RNG1[] to the range of the size.
448 : When nonnull, uses RVALS for range information, otherwise gets global
449 : range info.
450 : Returns null when STMT is not a call to a valid allocation function. */
451 :
452 : tree
453 493817 : gimple_call_alloc_size (gimple *stmt, wide_int rng1[2] /* = NULL */,
454 : range_query *qry /* = NULL */)
455 : {
456 493817 : if (!stmt || !is_gimple_call (stmt))
457 : return NULL_TREE;
458 :
459 493813 : tree allocfntype;
460 493813 : if (tree fndecl = gimple_call_fndecl (stmt))
461 477529 : allocfntype = TREE_TYPE (fndecl);
462 : else
463 16284 : allocfntype = gimple_call_fntype (stmt);
464 :
465 493813 : if (!allocfntype)
466 : return NULL_TREE;
467 :
468 492724 : unsigned argidx1 = UINT_MAX, argidx2 = UINT_MAX;
469 492724 : tree at = lookup_attribute ("alloc_size", TYPE_ATTRIBUTES (allocfntype));
470 492724 : if (!at)
471 : {
472 347657 : if (!gimple_call_builtin_p (stmt, BUILT_IN_ALLOCA_WITH_ALIGN))
473 : return NULL_TREE;
474 :
475 : argidx1 = 0;
476 : }
477 :
478 149906 : unsigned nargs = gimple_call_num_args (stmt);
479 :
480 149906 : if (argidx1 == UINT_MAX)
481 : {
482 145067 : tree atval = TREE_VALUE (at);
483 145067 : if (!atval)
484 : return NULL_TREE;
485 :
486 145067 : argidx1 = TREE_INT_CST_LOW (TREE_VALUE (atval)) - 1;
487 145067 : if (nargs <= argidx1)
488 : return NULL_TREE;
489 :
490 145067 : atval = TREE_CHAIN (atval);
491 145067 : if (atval)
492 : {
493 1073 : argidx2 = TREE_INT_CST_LOW (TREE_VALUE (atval)) - 1;
494 1073 : if (nargs <= argidx2)
495 : return NULL_TREE;
496 : }
497 : }
498 :
499 149906 : tree size = gimple_call_arg (stmt, argidx1);
500 :
501 749530 : wide_int rng1_buf[2];
502 : /* If RNG1 is not set, use the buffer. */
503 149906 : if (!rng1)
504 38 : rng1 = rng1_buf;
505 :
506 : /* Use maximum precision to avoid overflow below. */
507 149906 : const int prec = ADDR_MAX_PRECISION;
508 :
509 149906 : {
510 149906 : tree r[2];
511 : /* Determine the largest valid range size, including zero. */
512 149906 : if (!get_size_range (qry, size, stmt, r, SR_ALLOW_ZERO | SR_USE_LARGEST))
513 2 : return NULL_TREE;
514 149904 : rng1[0] = wi::to_wide (r[0], prec);
515 149904 : rng1[1] = wi::to_wide (r[1], prec);
516 : }
517 :
518 149904 : if (argidx2 > nargs && TREE_CODE (size) == INTEGER_CST)
519 56633 : return fold_convert (sizetype, size);
520 :
521 : /* To handle ranges do the math in wide_int and return the product
522 : of the upper bounds as a constant. Ignore anti-ranges. */
523 93271 : tree n = argidx2 < nargs ? gimple_call_arg (stmt, argidx2) : integer_one_node;
524 466355 : wide_int rng2[2];
525 93271 : {
526 93271 : tree r[2];
527 : /* As above, use the full non-negative range on failure. */
528 93271 : if (!get_size_range (qry, n, stmt, r, SR_ALLOW_ZERO | SR_USE_LARGEST))
529 0 : return NULL_TREE;
530 93271 : rng2[0] = wi::to_wide (r[0], prec);
531 93271 : rng2[1] = wi::to_wide (r[1], prec);
532 : }
533 :
534 : /* Compute products of both bounds for the caller but return the lesser
535 : of SIZE_MAX and the product of the upper bounds as a constant. */
536 93271 : rng1[0] = rng1[0] * rng2[0];
537 93271 : rng1[1] = rng1[1] * rng2[1];
538 :
539 93271 : const tree size_max = TYPE_MAX_VALUE (sizetype);
540 93271 : if (wi::gtu_p (rng1[1], wi::to_wide (size_max, prec)))
541 : {
542 79 : rng1[1] = wi::to_wide (size_max, prec);
543 79 : return size_max;
544 : }
545 :
546 93192 : return wide_int_to_tree (sizetype, rng1[1]);
547 729531 : }
548 :
549 : /* For an access to an object referenced to by the function parameter PTR
550 : of pointer type, and set RNG[] to the range of sizes of the object
551 : obtainedfrom the attribute access specification for the current function.
552 : Set STATIC_ARRAY if the array parameter has been declared [static].
553 : Return the function parameter on success and null otherwise. */
554 :
555 : static tree
556 831379 : gimple_parm_array_size (tree ptr, wide_int rng[2],
557 : bool *static_array /* = NULL */)
558 : {
559 : /* For a function argument try to determine the byte size of the array
560 : from the current function declaratation (e.g., attribute access or
561 : related). */
562 831379 : tree var = SSA_NAME_VAR (ptr);
563 831379 : if (TREE_CODE (var) != PARM_DECL || !POINTER_TYPE_P (TREE_TYPE (var)))
564 : return NULL_TREE;
565 :
566 786215 : const unsigned prec = TYPE_PRECISION (sizetype);
567 :
568 786215 : rdwr_map rdwr_idx;
569 786215 : attr_access *access = get_parm_access (rdwr_idx, var);
570 786215 : if (!access)
571 : return NULL_TREE;
572 :
573 2882 : if (access->sizarg != UINT_MAX)
574 : {
575 : /* TODO: Try to extract the range from the argument based on
576 : those of subsequent assertions or based on known calls to
577 : the current function. */
578 : return NULL_TREE;
579 : }
580 :
581 2862 : if (!access->minsize)
582 : return NULL_TREE;
583 :
584 : /* Only consider ordinary array bound at level 2 (or above if it's
585 : ever added). */
586 2017 : if (warn_array_parameter < 2 && !access->static_p)
587 : return NULL_TREE;
588 :
589 215 : if (static_array)
590 215 : *static_array = access->static_p;
591 :
592 215 : rng[0] = wi::zero (prec);
593 215 : rng[1] = wi::uhwi (access->minsize, prec);
594 : /* Multiply the array bound encoded in the attribute by the size
595 : of what the pointer argument to which it decays points to. */
596 215 : tree eltype = TREE_TYPE (TREE_TYPE (ptr));
597 215 : tree size = TYPE_SIZE_UNIT (eltype);
598 215 : if (!size || TREE_CODE (size) != INTEGER_CST)
599 : return NULL_TREE;
600 :
601 185 : rng[1] *= wi::to_wide (size, prec);
602 185 : return var;
603 786215 : }
604 :
605 : /* Initialize the object. */
606 :
607 20449785 : access_ref::access_ref ()
608 20449785 : : ref (), eval ([](tree x){ return x; }), deref (), ref_nullptr_p (false),
609 20449785 : trail1special (true), base0 (true), parmarray ()
610 : {
611 : /* Set to valid. */
612 20449785 : offrng[0] = offrng[1] = 0;
613 20449785 : offmax[0] = offmax[1] = 0;
614 : /* Invalidate. */
615 20449785 : sizrng[0] = sizrng[1] = -1;
616 20449785 : }
617 :
618 : /* Return the PHI node REF refers to or null if it doesn't. */
619 :
620 : gphi *
621 634947 : access_ref::phi () const
622 : {
623 634947 : if (!ref || TREE_CODE (ref) != SSA_NAME)
624 : return NULL;
625 :
626 631976 : gimple *def_stmt = SSA_NAME_DEF_STMT (ref);
627 631976 : if (!def_stmt || gimple_code (def_stmt) != GIMPLE_PHI)
628 : return NULL;
629 :
630 631051 : return as_a <gphi *> (def_stmt);
631 : }
632 :
633 : /* Determine the size and offset for ARG, append it to ALL_REFS, and
634 : merge the result with *THIS. Ignore ARG if SKIP_NULL is set and
635 : ARG refers to the null pointer. Return true on success and false
636 : on failure. */
637 :
638 : void
639 711532 : access_ref::merge_ref (vec<access_ref> *all_refs, tree arg, gimple *stmt,
640 : int ostype, bool skip_null,
641 : ssa_name_limit_t &snlim, pointer_query &qry)
642 : {
643 711532 : access_ref aref;
644 711532 : if (!compute_objsize_r (arg, stmt, false, ostype, &aref, snlim, &qry)
645 711532 : || aref.sizrng[0] < 0)
646 : {
647 : /* This may be a PHI with all null pointer arguments. Handle it
648 : conservatively by setting all properties to the most permissive
649 : values. */
650 62039 : base0 = false;
651 62039 : offrng[0] = offrng[1] = 0;
652 62039 : add_max_offset ();
653 62039 : set_max_size_range ();
654 62039 : return;
655 : }
656 :
657 649493 : if (all_refs)
658 : {
659 264 : access_ref dummy_ref;
660 264 : aref.get_ref (all_refs, &dummy_ref, ostype, &snlim, &qry);
661 : }
662 :
663 649493 : if (TREE_CODE (arg) == SSA_NAME)
664 543197 : qry.put_ref (arg, aref, ostype);
665 :
666 649493 : if (all_refs)
667 264 : all_refs->safe_push (aref);
668 :
669 649493 : aref.deref += deref;
670 :
671 649493 : bool merged_parmarray = aref.parmarray;
672 :
673 649493 : const bool nullp = skip_null && integer_zerop (arg);
674 649493 : const offset_int maxobjsize = wi::to_offset (max_object_size ());
675 649493 : offset_int minsize = sizrng[0];
676 :
677 649493 : if (sizrng[0] < 0)
678 : {
679 : /* If *THIS doesn't contain a meaningful result yet set it to AREF
680 : unless the argument is null and it's okay to ignore it. */
681 560259 : if (!nullp)
682 540262 : *this = aref;
683 :
684 : /* Set if the current argument refers to one or more objects of
685 : known size (or range of sizes), as opposed to referring to
686 : one or more unknown object(s). */
687 560259 : const bool arg_known_size = (aref.sizrng[0] != 0
688 1037605 : || aref.sizrng[1] != maxobjsize);
689 560259 : if (arg_known_size)
690 86281 : sizrng[0] = aref.sizrng[0];
691 :
692 : return;
693 : }
694 :
695 : /* Disregard null pointers in PHIs with two or more arguments.
696 : TODO: Handle this better! */
697 89234 : if (nullp)
698 : return;
699 :
700 86146 : const bool known_size = (sizrng[0] != 0 || sizrng[1] != maxobjsize);
701 :
702 168765 : if (known_size && aref.sizrng[0] < minsize)
703 25607 : minsize = aref.sizrng[0];
704 :
705 : /* Extend the size and offset of *THIS to account for AREF. The result
706 : can be cached but results in false negatives. */
707 :
708 86146 : offset_int orng[2];
709 86146 : if (sizrng[1] < aref.sizrng[1])
710 : {
711 26333 : orng[0] = offrng[0];
712 26333 : orng[1] = offrng[1];
713 26333 : *this = aref;
714 : }
715 : else
716 : {
717 59813 : orng[0] = aref.offrng[0];
718 59813 : orng[1] = aref.offrng[1];
719 : }
720 :
721 86146 : if (orng[0] < offrng[0])
722 7948 : offrng[0] = orng[0];
723 86146 : if (offrng[1] < orng[1])
724 19941 : offrng[1] = orng[1];
725 :
726 : /* Reset the PHI's BASE0 flag if any of the nonnull arguments
727 : refers to an object at an unknown offset. */
728 86146 : if (!aref.base0)
729 17476 : base0 = false;
730 :
731 86146 : sizrng[0] = minsize;
732 86146 : parmarray = merged_parmarray;
733 :
734 86146 : return;
735 : }
736 :
737 : /* Determine and return the largest object to which *THIS refers. If
738 : *THIS refers to a PHI and PREF is nonnull, fill *PREF with the details
739 : of the object determined by compute_objsize(ARG, OSTYPE) for each PHI
740 : argument ARG. */
741 :
742 : tree
743 631047 : access_ref::get_ref (vec<access_ref> *all_refs,
744 : access_ref *pref /* = NULL */,
745 : int ostype /* = 1 */,
746 : ssa_name_limit_t *psnlim /* = NULL */,
747 : pointer_query *qry /* = NULL */) const
748 : {
749 631047 : if (!ref || TREE_CODE (ref) != SSA_NAME)
750 : return NULL;
751 :
752 : /* FIXME: Calling get_ref() with a null PSNLIM is dangerous and might
753 : cause unbounded recursion. */
754 630841 : ssa_name_limit_t snlim_buf;
755 630841 : if (!psnlim)
756 82 : psnlim = &snlim_buf;
757 :
758 630841 : pointer_query empty_qry;
759 630841 : if (!qry)
760 82 : qry = &empty_qry;
761 :
762 630841 : if (gimple *def_gs = SSA_NAME_DEF_STMT (ref))
763 : {
764 630841 : if (gassign *def_stmt = dyn_cast<gassign *> (def_gs))
765 : {
766 0 : tree_code code = gimple_assign_rhs_code (def_stmt);
767 0 : if (code != MIN_EXPR && code != MAX_EXPR)
768 : return NULL_TREE;
769 :
770 0 : access_ref aref;
771 0 : tree arg1 = gimple_assign_rhs1 (def_stmt);
772 0 : aref.merge_ref (all_refs, arg1, def_stmt, ostype, false,
773 : *psnlim, *qry);
774 :
775 0 : tree arg2 = gimple_assign_rhs2 (def_stmt);
776 0 : aref.merge_ref (all_refs, arg2, def_stmt, ostype, false,
777 : *psnlim, *qry);
778 :
779 0 : if (pref && pref != this)
780 : {
781 0 : tree ref = pref->ref;
782 0 : *pref = aref;
783 0 : pref->ref = ref;
784 : }
785 :
786 0 : return aref.ref;
787 : }
788 : }
789 : else
790 : return NULL_TREE;
791 :
792 630841 : gphi *phi_stmt = this->phi ();
793 630841 : if (!phi_stmt)
794 23 : return ref;
795 :
796 630818 : if (!psnlim->visit_phi (ref))
797 : return NULL_TREE;
798 :
799 : /* The conservative result of the PHI reflecting the offset and size
800 : of the largest PHI argument, regardless of whether or not they all
801 : refer to the same object. */
802 568765 : access_ref phi_ref;
803 568765 : if (pref)
804 : {
805 : /* The identity of the object has not been determined yet but
806 : PREF->REF is set by the caller to the PHI for convenience.
807 : The size is negative/invalid and the offset is zero (it's
808 : updated only after the identity of the object has been
809 : established). */
810 568765 : gcc_assert (pref->sizrng[0] < 0);
811 568765 : gcc_assert (pref->offrng[0] == 0 && pref->offrng[1] == 0);
812 :
813 568765 : phi_ref = *pref;
814 : }
815 :
816 568765 : const offset_int maxobjsize = wi::to_offset (max_object_size ());
817 568765 : const unsigned nargs = gimple_phi_num_args (phi_stmt);
818 749587 : for (unsigned i = 0; i < nargs; ++i)
819 : {
820 711532 : access_ref phi_arg_ref;
821 711532 : bool skip_null = i || i + 1 < nargs;
822 711532 : tree arg = gimple_phi_arg_def (phi_stmt, i);
823 711532 : phi_ref.merge_ref (all_refs, arg, phi_stmt, ostype, skip_null,
824 : *psnlim, *qry);
825 :
826 711532 : if (!phi_ref.base0
827 1242242 : && phi_ref.sizrng[0] == 0
828 1242242 : && phi_ref.sizrng[1] >= maxobjsize)
829 : /* When an argument results in the most permissive result,
830 : the remaining arguments cannot constrain it. Short-circuit
831 : the evaluation. */
832 : break;
833 : }
834 :
835 568765 : if (phi_ref.sizrng[0] < 0)
836 : {
837 : /* Fail if none of the PHI's arguments resulted in updating PHI_REF
838 : (perhaps because they have all been already visited by prior
839 : recursive calls). */
840 0 : psnlim->leave_phi (ref);
841 0 : return NULL_TREE;
842 : }
843 :
844 : /* Avoid changing *THIS. */
845 568765 : if (pref && pref != this)
846 : {
847 : /* Keep the SSA_NAME of the PHI unchanged so that all PHI arguments
848 : can be referred to later if necessary. This is useful even if
849 : they all refer to the same object. */
850 568765 : tree ref = pref->ref;
851 568765 : *pref = phi_ref;
852 568765 : pref->ref = ref;
853 : }
854 :
855 568765 : psnlim->leave_phi (ref);
856 :
857 568765 : return phi_ref.ref;
858 630841 : }
859 :
860 : /* Return the maximum amount of space remaining and if non-null, set
861 : argument to the minimum. */
862 :
863 : offset_int
864 16258203 : access_ref::size_remaining (offset_int *pmin /* = NULL */) const
865 : {
866 16258203 : offset_int minbuf;
867 16258203 : if (!pmin)
868 11877182 : pmin = &minbuf;
869 :
870 16258203 : if (sizrng[0] < 0)
871 : {
872 : /* If the identity of the object hasn't been determined return
873 : the maximum size range. */
874 0 : *pmin = 0;
875 0 : return wi::to_offset (max_object_size ());
876 : }
877 :
878 : /* add_offset() ensures the offset range isn't inverted. */
879 16258203 : gcc_checking_assert (offrng[0] <= offrng[1]);
880 :
881 16258203 : if (base0)
882 : {
883 : /* The offset into referenced object is zero-based (i.e., it's
884 : not referenced by a pointer into middle of some unknown object). */
885 10635575 : if (offrng[0] < 0 && offrng[1] < 0)
886 : {
887 : /* If the offset is negative the remaining size is zero. */
888 2509 : *pmin = 0;
889 2509 : return 0;
890 : }
891 :
892 10633066 : if (sizrng[1] <= offrng[0])
893 : {
894 : /* If the starting offset is greater than or equal to the upper
895 : bound on the size of the object, the space remaining is zero.
896 : As a special case, if it's equal, set *PMIN to -1 to let
897 : the caller know the offset is valid and just past the end. */
898 78170 : *pmin = sizrng[1] == offrng[0] ? -1 : 0;
899 73700 : return 0;
900 : }
901 :
902 : /* Otherwise return the size minus the lower bound of the offset. */
903 10559366 : offset_int or0 = offrng[0] < 0 ? 0 : offrng[0];
904 :
905 10559366 : *pmin = sizrng[0] - or0;
906 10559366 : return sizrng[1] - or0;
907 : }
908 :
909 : /* The offset to the referenced object isn't zero-based (i.e., it may
910 : refer to a byte other than the first. The size of such an object
911 : is constrained only by the size of the address space (the result
912 : of max_object_size()). */
913 5622628 : if (sizrng[1] <= offrng[0])
914 : {
915 5 : *pmin = 0;
916 5 : return 0;
917 : }
918 :
919 5622623 : offset_int or0 = offrng[0] < 0 ? 0 : offrng[0];
920 :
921 5622623 : *pmin = sizrng[0] - or0;
922 5622623 : return sizrng[1] - or0;
923 : }
924 :
925 : /* Return true if the offset and object size are in range for SIZE. */
926 :
927 : bool
928 617940 : access_ref::offset_in_range (const offset_int &size) const
929 : {
930 617940 : if (size_remaining () < size)
931 : return false;
932 :
933 605949 : if (base0)
934 62624 : return offmax[0] >= 0 && offmax[1] <= sizrng[1];
935 :
936 543343 : offset_int maxoff = wi::to_offset (TYPE_MAX_VALUE (ptrdiff_type_node));
937 543343 : return offmax[0] > -maxoff && offmax[1] < maxoff;
938 : }
939 :
940 : /* Add the range [MIN, MAX] to the offset range. For known objects (with
941 : zero-based offsets) at least one of whose offset's bounds is in range,
942 : constrain the other (or both) to the bounds of the object (i.e., zero
943 : and the upper bound of its size). This improves the quality of
944 : diagnostics. */
945 :
946 8466065 : void access_ref::add_offset (const offset_int &min, const offset_int &max)
947 : {
948 8466065 : if (min <= max)
949 : {
950 : /* To add an ordinary range just add it to the bounds. */
951 8240191 : offrng[0] += min;
952 8240191 : offrng[1] += max;
953 : }
954 225874 : else if (!base0)
955 : {
956 : /* To add an inverted range to an offset to an unknown object
957 : expand it to the maximum. */
958 196670 : add_max_offset ();
959 4691667 : return;
960 : }
961 : else
962 : {
963 : /* To add an inverted range to an offset to an known object set
964 : the upper bound to the maximum representable offset value
965 : (which may be greater than MAX_OBJECT_SIZE).
966 : The lower bound is either the sum of the current offset and
967 : MIN when abs(MAX) is greater than the former, or zero otherwise.
968 : Zero because then the inverted range includes the negative of
969 : the lower bound. */
970 29204 : offset_int maxoff = wi::to_offset (TYPE_MAX_VALUE (ptrdiff_type_node));
971 29204 : offrng[1] = maxoff;
972 :
973 29204 : if (max >= 0)
974 : {
975 0 : offrng[0] = 0;
976 0 : if (offmax[0] > 0)
977 0 : offmax[0] = 0;
978 0 : return;
979 : }
980 :
981 29204 : offset_int absmax = wi::abs (max);
982 29204 : if (offrng[0] < absmax)
983 : {
984 26750 : offrng[0] += min;
985 : /* Cap the lower bound at the upper (set to MAXOFF above)
986 : to avoid inadvertently recreating an inverted range. */
987 26750 : if (offrng[1] < offrng[0])
988 2 : offrng[0] = offrng[1];
989 : }
990 : else
991 2454 : offrng[0] = 0;
992 : }
993 :
994 : /* Set the minimum and maximmum computed so far. */
995 8269395 : if (offrng[1] < 0 && offrng[1] < offmax[0])
996 31177 : offmax[0] = offrng[1];
997 8269395 : if (offrng[0] > 0 && offrng[0] > offmax[1])
998 3021197 : offmax[1] = offrng[0];
999 :
1000 8269395 : if (!base0)
1001 : return;
1002 :
1003 : /* When referencing a known object check to see if the offset computed
1004 : so far is in bounds... */
1005 3971068 : offset_int remrng[2];
1006 3971068 : remrng[1] = size_remaining (remrng);
1007 3971068 : if (remrng[1] > 0 || remrng[0] < 0)
1008 : {
1009 : /* ...if so, constrain it so that neither bound exceeds the size of
1010 : the object. Out of bounds offsets are left unchanged, and, for
1011 : better or worse, become in bounds later. They should be detected
1012 : and diagnosed at the point they first become invalid by
1013 : -Warray-bounds. */
1014 3967730 : if (offrng[0] < 0)
1015 112835 : offrng[0] = 0;
1016 3967730 : if (offrng[1] > sizrng[1])
1017 142767 : offrng[1] = sizrng[1];
1018 : }
1019 : }
1020 :
1021 : /* Issue one inform message describing each target of an access REF.
1022 : WRITE is set for a write access and clear for a read access. */
1023 :
1024 : void
1025 3579 : access_ref::inform_access (access_mode mode, int ostype /* = 1 */) const
1026 : {
1027 3579 : const access_ref &aref = *this;
1028 3579 : if (!aref.ref)
1029 3080 : return;
1030 :
1031 3438 : if (phi ())
1032 : {
1033 : /* Set MAXREF to refer to the largest object and fill ALL_REFS
1034 : with data for all objects referenced by the PHI arguments. */
1035 82 : access_ref maxref;
1036 82 : auto_vec<access_ref> all_refs;
1037 82 : if (!get_ref (&all_refs, &maxref, ostype))
1038 : return;
1039 :
1040 82 : if (all_refs.length ())
1041 : {
1042 : /* Except for MAXREF, the rest of the arguments' offsets need not
1043 : reflect one added to the PHI itself. Determine the latter from
1044 : MAXREF on which the result is based. */
1045 82 : const offset_int orng[] =
1046 : {
1047 82 : offrng[0] - maxref.offrng[0],
1048 82 : wi::smax (offrng[1] - maxref.offrng[1], offrng[0]),
1049 : };
1050 :
1051 : /* Add the final PHI's offset to that of each of the arguments
1052 : and recurse to issue an inform message for it. */
1053 692 : for (unsigned i = 0; i != all_refs.length (); ++i)
1054 : {
1055 : /* Skip any PHIs; those could lead to infinite recursion. */
1056 264 : if (all_refs[i].phi ())
1057 35 : continue;
1058 :
1059 229 : all_refs[i].add_offset (orng[0], orng[1]);
1060 229 : all_refs[i].inform_access (mode, ostype);
1061 : }
1062 82 : return;
1063 : }
1064 82 : }
1065 :
1066 : /* Convert offset range and avoid including a zero range since it
1067 : isn't necessarily meaningful. */
1068 3356 : HOST_WIDE_INT diff_min = tree_to_shwi (TYPE_MIN_VALUE (ptrdiff_type_node));
1069 3356 : HOST_WIDE_INT diff_max = tree_to_shwi (TYPE_MAX_VALUE (ptrdiff_type_node));
1070 3356 : HOST_WIDE_INT minoff;
1071 3356 : HOST_WIDE_INT maxoff = diff_max;
1072 3356 : if (wi::fits_shwi_p (aref.offrng[0]))
1073 3356 : minoff = aref.offrng[0].to_shwi ();
1074 : else
1075 0 : minoff = aref.offrng[0] < 0 ? diff_min : diff_max;
1076 :
1077 3356 : if (wi::fits_shwi_p (aref.offrng[1]))
1078 3353 : maxoff = aref.offrng[1].to_shwi ();
1079 :
1080 3356 : if (maxoff <= diff_min || maxoff >= diff_max)
1081 : /* Avoid mentioning an upper bound that's equal to or in excess
1082 : of the maximum of ptrdiff_t. */
1083 124 : maxoff = minoff;
1084 :
1085 : /* Convert size range and always include it since all sizes are
1086 : meaningful. */
1087 3356 : unsigned long long minsize = 0, maxsize = 0;
1088 3356 : if (wi::fits_shwi_p (aref.sizrng[0])
1089 3356 : && wi::fits_shwi_p (aref.sizrng[1]))
1090 : {
1091 3356 : minsize = aref.sizrng[0].to_shwi ();
1092 3356 : maxsize = aref.sizrng[1].to_shwi ();
1093 : }
1094 :
1095 : /* SIZRNG doesn't necessarily have the same range as the allocation
1096 : size determined by gimple_call_alloc_size (). */
1097 3356 : char sizestr[80];
1098 3356 : if (minsize == maxsize)
1099 3101 : sprintf (sizestr, "%llu", minsize);
1100 : else
1101 255 : sprintf (sizestr, "[%llu, %llu]", minsize, maxsize);
1102 :
1103 3356 : char offstr[80];
1104 3356 : if (minoff == 0
1105 3356 : && (maxoff == 0 || aref.sizrng[1] <= maxoff))
1106 978 : offstr[0] = '\0';
1107 2378 : else if (minoff == maxoff)
1108 2113 : sprintf (offstr, "%lli", (long long) minoff);
1109 : else
1110 265 : sprintf (offstr, "[%lli, %lli]", (long long) minoff, (long long) maxoff);
1111 :
1112 3356 : location_t loc = UNKNOWN_LOCATION;
1113 :
1114 3356 : tree ref = this->ref;
1115 3356 : tree allocfn = NULL_TREE;
1116 3356 : if (TREE_CODE (ref) == SSA_NAME)
1117 : {
1118 771 : gimple *stmt = SSA_NAME_DEF_STMT (ref);
1119 771 : if (!stmt)
1120 : return;
1121 :
1122 771 : if (is_gimple_call (stmt))
1123 : {
1124 731 : loc = gimple_location (stmt);
1125 731 : if (gimple_call_builtin_p (stmt, BUILT_IN_ALLOCA_WITH_ALIGN))
1126 : {
1127 : /* Strip the SSA_NAME suffix from the variable name and
1128 : recreate an identifier with the VLA's original name. */
1129 23 : ref = gimple_call_lhs (stmt);
1130 23 : if (SSA_NAME_IDENTIFIER (ref))
1131 : {
1132 22 : ref = SSA_NAME_IDENTIFIER (ref);
1133 22 : const char *id = IDENTIFIER_POINTER (ref);
1134 22 : size_t len = strcspn (id, ".$");
1135 22 : if (!len)
1136 0 : len = strlen (id);
1137 22 : ref = get_identifier_with_length (id, len);
1138 : }
1139 : }
1140 : else
1141 : {
1142 : /* Except for VLAs, retrieve the allocation function. */
1143 708 : allocfn = gimple_call_fndecl (stmt);
1144 708 : if (!allocfn)
1145 7 : allocfn = gimple_call_fn (stmt);
1146 708 : if (TREE_CODE (allocfn) == SSA_NAME)
1147 : {
1148 : /* For an ALLOC_CALL via a function pointer make a small
1149 : effort to determine the destination of the pointer. */
1150 4 : gimple *def = SSA_NAME_DEF_STMT (allocfn);
1151 4 : if (gimple_assign_single_p (def))
1152 : {
1153 3 : tree rhs = gimple_assign_rhs1 (def);
1154 3 : if (DECL_P (rhs))
1155 : allocfn = rhs;
1156 2 : else if (TREE_CODE (rhs) == COMPONENT_REF)
1157 1 : allocfn = TREE_OPERAND (rhs, 1);
1158 : }
1159 : }
1160 : }
1161 : }
1162 40 : else if (gimple_nop_p (stmt))
1163 : /* Handle DECL_PARM below. */
1164 5 : ref = SSA_NAME_VAR (ref);
1165 35 : else if (is_gimple_assign (stmt)
1166 35 : && (gimple_assign_rhs_code (stmt) == MIN_EXPR
1167 18 : || gimple_assign_rhs_code (stmt) == MAX_EXPR))
1168 : {
1169 : /* MIN or MAX_EXPR here implies a reference to a known object
1170 : and either an unknown or distinct one (the latter being
1171 : the result of an invalid relational expression). Determine
1172 : the identity of the former and point to it in the note.
1173 : TODO: Consider merging with PHI handling. */
1174 105 : access_ref arg_ref[2];
1175 35 : tree arg = gimple_assign_rhs1 (stmt);
1176 35 : compute_objsize (arg, /* ostype = */ 1 , &arg_ref[0]);
1177 35 : arg = gimple_assign_rhs2 (stmt);
1178 35 : compute_objsize (arg, /* ostype = */ 1 , &arg_ref[1]);
1179 :
1180 : /* Use the argument that references a known object with more
1181 : space remaining. */
1182 35 : const bool idx
1183 35 : = (!arg_ref[0].ref || !arg_ref[0].base0
1184 52 : || (arg_ref[0].base0 && arg_ref[1].base0
1185 11 : && (arg_ref[0].size_remaining ()
1186 22 : < arg_ref[1].size_remaining ())));
1187 :
1188 35 : arg_ref[idx].offrng[0] = offrng[0];
1189 35 : arg_ref[idx].offrng[1] = offrng[1];
1190 35 : arg_ref[idx].inform_access (mode);
1191 35 : return;
1192 : }
1193 : }
1194 :
1195 3321 : if (DECL_P (ref))
1196 2482 : loc = DECL_SOURCE_LOCATION (ref);
1197 839 : else if (EXPR_P (ref) && EXPR_HAS_LOCATION (ref))
1198 0 : loc = EXPR_LOCATION (ref);
1199 839 : else if (TREE_CODE (ref) != IDENTIFIER_NODE
1200 839 : && TREE_CODE (ref) != SSA_NAME)
1201 : {
1202 108 : if (TREE_CODE (ref) == INTEGER_CST && ref_nullptr_p)
1203 : {
1204 5 : if (mode == access_read_write || mode == access_write_only)
1205 1 : inform (loc, "destination object is likely at address zero");
1206 : else
1207 4 : inform (loc, "source object is likely at address zero");
1208 : }
1209 : return;
1210 : }
1211 :
1212 3213 : if (mode == access_read_write || mode == access_write_only)
1213 : {
1214 1624 : if (allocfn == NULL_TREE)
1215 : {
1216 1422 : if (*offstr)
1217 1119 : inform (loc, "at offset %s into destination object %qE of size %s",
1218 : offstr, ref, sizestr);
1219 : else
1220 303 : inform (loc, "destination object %qE of size %s", ref, sizestr);
1221 : return;
1222 : }
1223 :
1224 202 : if (*offstr)
1225 55 : inform (loc,
1226 : "at offset %s into destination object of size %s "
1227 : "allocated by %qE", offstr, sizestr, allocfn);
1228 : else
1229 147 : inform (loc, "destination object of size %s allocated by %qE",
1230 : sizestr, allocfn);
1231 : return;
1232 : }
1233 :
1234 1589 : if (mode == access_read_only)
1235 : {
1236 379 : if (allocfn == NULL_TREE)
1237 : {
1238 372 : if (*offstr)
1239 249 : inform (loc, "at offset %s into source object %qE of size %s",
1240 : offstr, ref, sizestr);
1241 : else
1242 123 : inform (loc, "source object %qE of size %s", ref, sizestr);
1243 :
1244 : return;
1245 : }
1246 :
1247 7 : if (*offstr)
1248 0 : inform (loc,
1249 : "at offset %s into source object of size %s allocated by %qE",
1250 : offstr, sizestr, allocfn);
1251 : else
1252 7 : inform (loc, "source object of size %s allocated by %qE",
1253 : sizestr, allocfn);
1254 : return;
1255 : }
1256 :
1257 1210 : if (allocfn == NULL_TREE)
1258 : {
1259 711 : if (*offstr)
1260 588 : inform (loc, "at offset %s into object %qE of size %s",
1261 : offstr, ref, sizestr);
1262 : else
1263 123 : inform (loc, "object %qE of size %s", ref, sizestr);
1264 :
1265 : return;
1266 : }
1267 :
1268 499 : if (*offstr)
1269 252 : inform (loc,
1270 : "at offset %s into object of size %s allocated by %qE",
1271 : offstr, sizestr, allocfn);
1272 : else
1273 247 : inform (loc, "object of size %s allocated by %qE",
1274 : sizestr, allocfn);
1275 : }
1276 :
1277 : /* Dump *THIS to FILE. */
1278 :
1279 : void
1280 0 : access_ref::dump (FILE *file) const
1281 : {
1282 0 : for (int i = deref; i < 0; ++i)
1283 0 : fputc ('&', file);
1284 :
1285 0 : for (int i = 0; i < deref; ++i)
1286 0 : fputc ('*', file);
1287 :
1288 0 : if (gphi *phi_stmt = phi ())
1289 : {
1290 0 : fputs ("PHI <", file);
1291 0 : unsigned nargs = gimple_phi_num_args (phi_stmt);
1292 0 : for (unsigned i = 0; i != nargs; ++i)
1293 : {
1294 0 : tree arg = gimple_phi_arg_def (phi_stmt, i);
1295 0 : print_generic_expr (file, arg);
1296 0 : if (i + 1 < nargs)
1297 0 : fputs (", ", file);
1298 : }
1299 0 : fputc ('>', file);
1300 : }
1301 : else
1302 0 : print_generic_expr (file, ref);
1303 :
1304 0 : if (offrng[0] != offrng[1])
1305 0 : fprintf (file, " + [%lli, %lli]",
1306 0 : (long long) offrng[0].to_shwi (),
1307 0 : (long long) offrng[1].to_shwi ());
1308 0 : else if (offrng[0] != 0)
1309 0 : fprintf (file, " %c %lli",
1310 0 : offrng[0] < 0 ? '-' : '+',
1311 0 : (long long) offrng[0].to_shwi ());
1312 :
1313 0 : if (base0)
1314 0 : fputs (" (base0)", file);
1315 :
1316 0 : fputs ("; size: ", file);
1317 0 : if (sizrng[0] != sizrng[1])
1318 : {
1319 0 : offset_int maxsize = wi::to_offset (max_object_size ());
1320 0 : if (sizrng[0] == 0 && sizrng[1] >= maxsize)
1321 0 : fputs ("unknown", file);
1322 : else
1323 0 : fprintf (file, "[%llu, %llu]",
1324 0 : (unsigned long long) sizrng[0].to_uhwi (),
1325 0 : (unsigned long long) sizrng[1].to_uhwi ());
1326 : }
1327 0 : else if (sizrng[0] != 0)
1328 0 : fprintf (file, "%llu",
1329 0 : (unsigned long long) sizrng[0].to_uhwi ());
1330 :
1331 0 : fputc ('\n', file);
1332 0 : }
1333 :
1334 : /* Set the access to at most MAXWRITE and MAXREAD bytes, and at least 1
1335 : when MINWRITE or MINREAD, respectively, is set. */
1336 753968 : access_data::access_data (range_query *query, gimple *stmt, access_mode mode,
1337 : tree maxwrite /* = NULL_TREE */,
1338 : bool minwrite /* = false */,
1339 : tree maxread /* = NULL_TREE */,
1340 : bool minread /* = false */)
1341 753968 : : stmt (stmt), call (), dst (), src (), mode (mode), ostype ()
1342 : {
1343 753968 : set_bound (dst_bndrng, maxwrite, minwrite, query, stmt);
1344 753968 : set_bound (src_bndrng, maxread, minread, query, stmt);
1345 753968 : }
1346 :
1347 : /* Set the access to at most MAXWRITE and MAXREAD bytes, and at least 1
1348 : when MINWRITE or MINREAD, respectively, is set. */
1349 108 : access_data::access_data (range_query *query, tree expr, access_mode mode,
1350 : tree maxwrite /* = NULL_TREE */,
1351 : bool minwrite /* = false */,
1352 : tree maxread /* = NULL_TREE */,
1353 : bool minread /* = false */)
1354 108 : : stmt (), call (expr), dst (), src (), mode (mode), ostype ()
1355 : {
1356 108 : set_bound (dst_bndrng, maxwrite, minwrite, query, stmt);
1357 108 : set_bound (src_bndrng, maxread, minread, query, stmt);
1358 108 : }
1359 :
1360 : /* Set BNDRNG to the range of BOUND for the statement STMT. */
1361 :
1362 : void
1363 1508152 : access_data::set_bound (offset_int bndrng[2], tree bound, bool minaccess,
1364 : range_query *query, gimple *stmt)
1365 : {
1366 : /* Set the default bounds of the access and adjust below. */
1367 2676061 : bndrng[0] = minaccess ? 1 : 0;
1368 1508152 : bndrng[1] = HOST_WIDE_INT_M1U;
1369 :
1370 : /* When BOUND is nonnull and a range can be extracted from it,
1371 : set the bounds of the access to reflect both it and MINACCESS.
1372 : BNDRNG[0] is the size of the minimum access. */
1373 1508152 : tree rng[2];
1374 1508152 : if (bound && get_size_range (query, bound, stmt, rng, SR_ALLOW_ZERO))
1375 : {
1376 49117 : bndrng[0] = wi::to_offset (rng[0]);
1377 49117 : bndrng[1] = wi::to_offset (rng[1]);
1378 56170 : bndrng[0] = bndrng[0] > 0 && minaccess ? 1 : 0;
1379 : }
1380 1508152 : }
1381 :
1382 : /* Set a bit for the PHI in VISITED and return true if it wasn't
1383 : already set. */
1384 :
1385 : bool
1386 774420 : ssa_name_limit_t::visit_phi (tree ssa_name)
1387 : {
1388 774420 : if (!visited)
1389 523819 : visited = BITMAP_ALLOC (NULL);
1390 :
1391 : /* Return false if SSA_NAME has already been visited. */
1392 774420 : return bitmap_set_bit (visited, SSA_NAME_VERSION (ssa_name));
1393 : }
1394 :
1395 : /* Clear a bit for the PHI in VISITED. */
1396 :
1397 : void
1398 568765 : ssa_name_limit_t::leave_phi (tree ssa_name)
1399 : {
1400 : /* Return false if SSA_NAME has already been visited. */
1401 568765 : bitmap_clear_bit (visited, SSA_NAME_VERSION (ssa_name));
1402 568765 : }
1403 :
1404 : /* Return false if the SSA_NAME chain length counter has reached
1405 : the limit, otherwise increment the counter and return true. */
1406 :
1407 : bool
1408 6952489 : ssa_name_limit_t::next ()
1409 : {
1410 : /* Return a negative value to let caller avoid recursing beyond
1411 : the specified limit. */
1412 6952489 : if (ssa_def_max == 0)
1413 : return false;
1414 :
1415 6952480 : --ssa_def_max;
1416 6952480 : return true;
1417 : }
1418 :
1419 : /* If the SSA_NAME has already been "seen" return a positive value.
1420 : Otherwise add it to VISITED. If the SSA_NAME limit has been
1421 : reached, return a negative value. Otherwise return zero. */
1422 :
1423 : int
1424 143602 : ssa_name_limit_t::next_phi (tree ssa_name)
1425 : {
1426 143602 : {
1427 143602 : gimple *def_stmt = SSA_NAME_DEF_STMT (ssa_name);
1428 : /* Return a positive value if the PHI has already been visited. */
1429 143602 : if (gimple_code (def_stmt) == GIMPLE_PHI
1430 143602 : && !visit_phi (ssa_name))
1431 : return 1;
1432 : }
1433 :
1434 : /* Return a negative value to let caller avoid recursing beyond
1435 : the specified limit. */
1436 102822 : if (ssa_def_max == 0)
1437 : return -1;
1438 :
1439 102822 : --ssa_def_max;
1440 :
1441 102822 : return 0;
1442 : }
1443 :
1444 12799478 : ssa_name_limit_t::~ssa_name_limit_t ()
1445 : {
1446 12799478 : if (visited)
1447 523819 : BITMAP_FREE (visited);
1448 12799478 : }
1449 :
1450 : /* Default ctor. Initialize object with pointers to the range_query
1451 : instance to use or null. */
1452 :
1453 14890560 : pointer_query::pointer_query (range_query *qry /* = NULL */)
1454 14890560 : : rvals (qry), hits (), misses (), failures (), depth (), max_depth (),
1455 14890560 : var_cache ()
1456 : {
1457 : /* No op. */
1458 14890560 : }
1459 :
1460 : /* Return a pointer to the cached access_ref instance for the SSA_NAME
1461 : PTR if it's there or null otherwise. */
1462 :
1463 : const access_ref *
1464 6952480 : pointer_query::get_ref (tree ptr, int ostype /* = 1 */) const
1465 : {
1466 6952480 : unsigned version = SSA_NAME_VERSION (ptr);
1467 6952480 : unsigned idx = version << 1 | (ostype & 1);
1468 6952480 : if (var_cache.indices.length () <= idx)
1469 : {
1470 3088498 : ++misses;
1471 3088498 : return NULL;
1472 : }
1473 :
1474 3863982 : unsigned cache_idx = var_cache.indices[idx];
1475 3863982 : if (var_cache.access_refs.length () <= cache_idx)
1476 : {
1477 0 : ++misses;
1478 0 : return NULL;
1479 : }
1480 :
1481 3863982 : const access_ref &cache_ref = var_cache.access_refs[cache_idx];
1482 3863982 : if (cache_ref.ref)
1483 : {
1484 1842563 : ++hits;
1485 1842563 : return &cache_ref;
1486 : }
1487 :
1488 2021419 : ++misses;
1489 2021419 : return NULL;
1490 : }
1491 :
1492 : /* Retrieve the access_ref instance for a variable from the cache if it's
1493 : there or compute it and insert it into the cache if it's nonnonull. */
1494 :
1495 : bool
1496 8963846 : pointer_query::get_ref (tree ptr, gimple *stmt, access_ref *pref,
1497 : int ostype /* = 1 */)
1498 : {
1499 8963846 : const unsigned version
1500 8963846 : = TREE_CODE (ptr) == SSA_NAME ? SSA_NAME_VERSION (ptr) : 0;
1501 :
1502 2199974 : if (version)
1503 : {
1504 2199974 : unsigned idx = version << 1 | (ostype & 1);
1505 2199974 : if (idx < var_cache.indices.length ())
1506 : {
1507 1137761 : unsigned cache_idx = var_cache.indices[idx] - 1;
1508 1137761 : if (cache_idx < var_cache.access_refs.length ()
1509 1137761 : && var_cache.access_refs[cache_idx].ref)
1510 : {
1511 0 : ++hits;
1512 0 : *pref = var_cache.access_refs[cache_idx];
1513 0 : return true;
1514 : }
1515 : }
1516 :
1517 2199974 : ++misses;
1518 : }
1519 :
1520 8963846 : if (!compute_objsize (ptr, stmt, ostype, pref, this))
1521 : {
1522 9658 : ++failures;
1523 9658 : return false;
1524 : }
1525 :
1526 : return true;
1527 : }
1528 :
1529 : /* Add a copy of the access_ref REF for the SSA_NAME to the cache if it's
1530 : nonnull. */
1531 :
1532 : void
1533 3716390 : pointer_query::put_ref (tree ptr, const access_ref &ref, int ostype /* = 1 */)
1534 : {
1535 : /* Only add populated/valid entries. */
1536 3716390 : if (!ref.ref || ref.sizrng[0] < 0)
1537 : return;
1538 :
1539 : /* Add REF to the two-level cache. */
1540 3716390 : unsigned version = SSA_NAME_VERSION (ptr);
1541 3716390 : unsigned idx = version << 1 | (ostype & 1);
1542 :
1543 : /* Grow INDICES if necessary. An index is valid if it's nonzero.
1544 : Its value minus one is the index into ACCESS_REFS. Not all
1545 : entries are valid. */
1546 3716390 : if (var_cache.indices.length () <= idx)
1547 1910964 : var_cache.indices.safe_grow_cleared (idx + 1);
1548 :
1549 3716390 : if (!var_cache.indices[idx])
1550 5704728 : var_cache.indices[idx] = var_cache.access_refs.length () + 1;
1551 :
1552 : /* Grow ACCESS_REF cache if necessary. An entry is valid if its
1553 : REF member is nonnull. All entries except for the last two
1554 : are valid. Once nonnull, the REF value must stay unchanged. */
1555 3716390 : unsigned cache_idx = var_cache.indices[idx];
1556 3716390 : if (var_cache.access_refs.length () <= cache_idx)
1557 3362926 : var_cache.access_refs.safe_grow_cleared (cache_idx + 1);
1558 :
1559 3716390 : access_ref &cache_ref = var_cache.access_refs[cache_idx];
1560 3716390 : if (cache_ref.ref)
1561 : {
1562 353464 : gcc_checking_assert (cache_ref.ref == ref.ref);
1563 : return;
1564 : }
1565 :
1566 3362926 : cache_ref = ref;
1567 : }
1568 :
1569 : /* Flush the cache if it's nonnull. */
1570 :
1571 : void
1572 8615427 : pointer_query::flush_cache ()
1573 : {
1574 8615427 : var_cache.indices.release ();
1575 8615427 : var_cache.access_refs.release ();
1576 8615427 : }
1577 :
1578 : /* Dump statistics and, optionally, cache contents to DUMP_FILE. */
1579 :
1580 : void
1581 167 : pointer_query::dump (FILE *dump_file, bool contents /* = false */)
1582 : {
1583 167 : unsigned nused = 0, nrefs = 0;
1584 167 : unsigned nidxs = var_cache.indices.length ();
1585 167 : for (unsigned i = 0; i != nidxs; ++i)
1586 : {
1587 0 : unsigned ari = var_cache.indices[i];
1588 0 : if (!ari)
1589 0 : continue;
1590 :
1591 0 : ++nused;
1592 :
1593 0 : const access_ref &aref = var_cache.access_refs[ari];
1594 0 : if (!aref.ref)
1595 0 : continue;
1596 :
1597 0 : ++nrefs;
1598 : }
1599 :
1600 167 : fprintf (dump_file, "pointer_query counters:\n"
1601 : " index cache size: %u\n"
1602 : " index entries: %u\n"
1603 : " access cache size: %u\n"
1604 : " access entries: %u\n"
1605 : " hits: %u\n"
1606 : " misses: %u\n"
1607 : " failures: %u\n"
1608 : " max_depth: %u\n",
1609 : nidxs, nused,
1610 : var_cache.access_refs.length (), nrefs,
1611 : hits, misses, failures, max_depth);
1612 :
1613 167 : if (!contents || !nidxs)
1614 : return;
1615 :
1616 0 : fputs ("\npointer_query cache contents:\n", dump_file);
1617 :
1618 0 : for (unsigned i = 0; i != nidxs; ++i)
1619 : {
1620 0 : unsigned ari = var_cache.indices[i];
1621 0 : if (!ari)
1622 0 : continue;
1623 :
1624 0 : const access_ref &aref = var_cache.access_refs[ari];
1625 0 : if (!aref.ref)
1626 0 : continue;
1627 :
1628 : /* The level-1 cache index corresponds to the SSA_NAME_VERSION
1629 : shifted left by one and ORed with the Object Size Type in
1630 : the lowest bit. Print the two separately. */
1631 0 : unsigned ver = i >> 1;
1632 0 : unsigned ost = i & 1;
1633 :
1634 0 : fprintf (dump_file, " %u.%u[%u]: ", ver, ost, ari);
1635 0 : if (tree name = ssa_name (ver))
1636 : {
1637 0 : print_generic_expr (dump_file, name);
1638 0 : fputs (" = ", dump_file);
1639 : }
1640 : else
1641 0 : fprintf (dump_file, " _%u = ", ver);
1642 :
1643 0 : aref.dump (dump_file);
1644 : }
1645 :
1646 0 : fputc ('\n', dump_file);
1647 : }
1648 :
1649 : /* A helper of compute_objsize_r() to determine the size from an assignment
1650 : statement STMT with the RHS of either MIN_EXPR or MAX_EXPR. On success
1651 : set PREF->REF to the operand with more or less space remaining,
1652 : respectively, if both refer to the same (sub)object, or to PTR if they
1653 : might not, and return true. Otherwise, if the identity of neither
1654 : operand can be determined, return false. */
1655 :
1656 : static bool
1657 1333 : handle_min_max_size (tree ptr, int ostype, access_ref *pref,
1658 : ssa_name_limit_t &snlim, pointer_query *qry)
1659 : {
1660 1333 : gimple *stmt = SSA_NAME_DEF_STMT (ptr);
1661 1333 : const tree_code code = gimple_assign_rhs_code (stmt);
1662 :
1663 : /* In a valid MAX_/MIN_EXPR both operands must refer to the same array.
1664 : Determine the size/offset of each and use the one with more or less
1665 : space remaining, respectively. If either fails, use the information
1666 : determined from the other instead, adjusted up or down as appropriate
1667 : for the expression. */
1668 1333 : access_ref aref[2] = { *pref, *pref };
1669 1333 : tree arg1 = gimple_assign_rhs1 (stmt);
1670 1333 : if (!compute_objsize_r (arg1, stmt, false, ostype, &aref[0], snlim, qry))
1671 : {
1672 7 : aref[0].base0 = false;
1673 7 : aref[0].offrng[0] = aref[0].offrng[1] = 0;
1674 7 : aref[0].add_max_offset ();
1675 7 : aref[0].set_max_size_range ();
1676 : }
1677 :
1678 1333 : tree arg2 = gimple_assign_rhs2 (stmt);
1679 1333 : if (!compute_objsize_r (arg2, stmt, false, ostype, &aref[1], snlim, qry))
1680 : {
1681 0 : aref[1].base0 = false;
1682 0 : aref[1].offrng[0] = aref[1].offrng[1] = 0;
1683 0 : aref[1].add_max_offset ();
1684 0 : aref[1].set_max_size_range ();
1685 : }
1686 :
1687 1333 : if (!aref[0].ref && !aref[1].ref)
1688 : /* Fail if the identity of neither argument could be determined. */
1689 : return false;
1690 :
1691 1333 : bool i0 = false;
1692 1333 : if (aref[0].ref && aref[0].base0)
1693 : {
1694 128 : if (aref[1].ref && aref[1].base0)
1695 : {
1696 : /* If the object referenced by both arguments has been determined
1697 : set *PREF to the one with more or less space remainng, whichever
1698 : is appropriate for CODE.
1699 : TODO: Indicate when the objects are distinct so it can be
1700 : diagnosed. */
1701 89 : i0 = code == MAX_EXPR;
1702 89 : const bool i1 = !i0;
1703 :
1704 89 : if (aref[i0].size_remaining () < aref[i1].size_remaining ())
1705 23 : *pref = aref[i1];
1706 : else
1707 66 : *pref = aref[i0];
1708 :
1709 89 : if (aref[i0].ref != aref[i1].ref)
1710 : /* If the operands don't refer to the same (sub)object set
1711 : PREF->REF to the SSA_NAME from which STMT was obtained
1712 : so that both can be identified in a diagnostic. */
1713 63 : pref->ref = ptr;
1714 :
1715 : return true;
1716 : }
1717 :
1718 : /* If only the object referenced by one of the arguments could be
1719 : determined, use it and... */
1720 39 : *pref = aref[0];
1721 39 : i0 = true;
1722 39 : }
1723 : else
1724 1205 : *pref = aref[1];
1725 :
1726 1244 : const bool i1 = !i0;
1727 : /* ...see if the offset obtained from the other pointer can be used
1728 : to tighten up the bound on the offset obtained from the first. */
1729 668 : if ((code == MAX_EXPR && aref[i1].offrng[1] < aref[i0].offrng[0])
1730 1894 : || (code == MIN_EXPR && aref[i0].offrng[0] < aref[i1].offrng[1]))
1731 : {
1732 126 : pref->offrng[0] = aref[i0].offrng[0];
1733 126 : pref->offrng[1] = aref[i0].offrng[1];
1734 : }
1735 :
1736 : /* Replace PTR->REF with the SSA_NAME to indicate the expression
1737 : might not refer to the same (sub)object. */
1738 1244 : pref->ref = ptr;
1739 1244 : return true;
1740 : }
1741 :
1742 : /* A helper of compute_objsize_r() to determine the size of a DECL.
1743 : Return true on success and (possibly in the future) false on failure. */
1744 :
1745 : static bool
1746 5226093 : handle_decl (tree decl, bool addr, access_ref *pref)
1747 : {
1748 5226093 : tree decl_type = TREE_TYPE (decl);
1749 :
1750 5226093 : pref->ref = decl;
1751 :
1752 : /* Reset the offset in case it was set by a prior call and not
1753 : cleared by the caller. The offset is only adjusted after
1754 : the identity of the object has been determined. */
1755 5226093 : pref->offrng[0] = pref->offrng[1] = 0;
1756 :
1757 5226093 : if (!addr && POINTER_TYPE_P (decl_type))
1758 : {
1759 : /* Set the maximum size if the reference is to the pointer
1760 : itself (as opposed to what it points to), and clear
1761 : BASE0 since the offset isn't necessarily zero-based. */
1762 47718 : pref->set_max_size_range ();
1763 47718 : pref->base0 = false;
1764 47718 : return true;
1765 : }
1766 :
1767 : /* Valid offsets into the object are nonnegative. */
1768 5178375 : pref->base0 = true;
1769 :
1770 5178375 : if (tree size = decl_init_size (decl, false))
1771 5153138 : if (TREE_CODE (size) == INTEGER_CST)
1772 : {
1773 5153004 : pref->sizrng[0] = wi::to_offset (size);
1774 5153004 : pref->sizrng[1] = pref->sizrng[0];
1775 5153004 : return true;
1776 : }
1777 :
1778 25371 : pref->set_max_size_range ();
1779 25371 : return true;
1780 : }
1781 :
1782 : /* A helper of compute_objsize_r() to determine the size from ARRAY_REF
1783 : AREF. ADDR is true if PTR is the operand of ADDR_EXPR. Return true
1784 : on success and false on failure. */
1785 :
1786 : static bool
1787 1008705 : handle_array_ref (tree aref, gimple *stmt, bool addr, int ostype,
1788 : access_ref *pref, ssa_name_limit_t &snlim,
1789 : pointer_query *qry)
1790 : {
1791 1008705 : gcc_assert (TREE_CODE (aref) == ARRAY_REF);
1792 :
1793 1008705 : tree arefop = TREE_OPERAND (aref, 0);
1794 1008705 : tree reftype = TREE_TYPE (arefop);
1795 1008705 : if (!addr && TREE_CODE (TREE_TYPE (reftype)) == POINTER_TYPE)
1796 : /* Avoid arrays of pointers. FIXME: Handle pointers to arrays
1797 : of known bound. */
1798 : return false;
1799 :
1800 999041 : if (!compute_objsize_r (arefop, stmt, addr, ostype, pref, snlim, qry))
1801 : return false;
1802 :
1803 999041 : offset_int orng[2];
1804 999041 : tree off = pref->eval (TREE_OPERAND (aref, 1));
1805 999041 : range_query *const rvals = qry ? qry->rvals : NULL;
1806 999041 : if (!get_offset_range (off, stmt, orng, rvals))
1807 : {
1808 : /* Set ORNG to the maximum offset representable in ptrdiff_t. */
1809 49427 : orng[1] = wi::to_offset (TYPE_MAX_VALUE (ptrdiff_type_node));
1810 49427 : orng[0] = -orng[1] - 1;
1811 : }
1812 :
1813 : /* Convert the array index range determined above to a byte offset. */
1814 999041 : tree lowbnd = array_ref_low_bound (aref);
1815 999041 : if (TREE_CODE (lowbnd) == INTEGER_CST && !integer_zerop (lowbnd))
1816 : {
1817 : /* Adjust the index by the low bound of the array domain (0 in C/C++,
1818 : 1 in Fortran and anything in Ada) by applying the same processing
1819 : as in get_offset_range. */
1820 17511 : const wide_int wlb = wi::to_wide (lowbnd);
1821 17511 : signop sgn = SIGNED;
1822 17511 : if (TYPE_UNSIGNED (TREE_TYPE (lowbnd))
1823 17511 : && wlb.get_precision () < TYPE_PRECISION (sizetype))
1824 : sgn = UNSIGNED;
1825 17511 : const offset_int lb = offset_int::from (wlb, sgn);
1826 17511 : orng[0] -= lb;
1827 17511 : orng[1] -= lb;
1828 17511 : }
1829 :
1830 999041 : tree eltype = TREE_TYPE (aref);
1831 999041 : tree tpsize = TYPE_SIZE_UNIT (eltype);
1832 999041 : if (!tpsize || TREE_CODE (tpsize) != INTEGER_CST)
1833 : {
1834 585 : pref->add_max_offset ();
1835 585 : return true;
1836 : }
1837 :
1838 998456 : offset_int sz = wi::to_offset (tpsize);
1839 998456 : orng[0] *= sz;
1840 998456 : orng[1] *= sz;
1841 :
1842 998456 : if (ostype && TREE_CODE (eltype) == ARRAY_TYPE)
1843 : {
1844 : /* Except for the permissive raw memory functions which use
1845 : the size of the whole object determined above, use the size
1846 : of the referenced array. Because the overall offset is from
1847 : the beginning of the complete array object add this overall
1848 : offset to the size of array. */
1849 7152 : offset_int sizrng[2] =
1850 : {
1851 7152 : pref->offrng[0] + orng[0] + sz,
1852 7152 : pref->offrng[1] + orng[1] + sz
1853 : };
1854 7152 : if (sizrng[1] < sizrng[0])
1855 2 : std::swap (sizrng[0], sizrng[1]);
1856 7152 : if (sizrng[0] >= 0 && sizrng[0] <= pref->sizrng[0])
1857 6573 : pref->sizrng[0] = sizrng[0];
1858 7152 : if (sizrng[1] >= 0 && sizrng[1] <= pref->sizrng[1])
1859 6755 : pref->sizrng[1] = sizrng[1];
1860 : }
1861 :
1862 998456 : pref->add_offset (orng[0], orng[1]);
1863 998456 : return true;
1864 : }
1865 :
1866 : /* Given a COMPONENT_REF CREF, set *PREF size to the size of the referenced
1867 : member. */
1868 :
1869 : static void
1870 57450 : set_component_ref_size (tree cref, access_ref *pref)
1871 : {
1872 57450 : const tree base = TREE_OPERAND (cref, 0);
1873 57450 : const tree base_type = TREE_TYPE (base);
1874 :
1875 : /* SAM is set for array members that might need special treatment. */
1876 57450 : special_array_member sam;
1877 57450 : tree size = component_ref_size (cref, &sam);
1878 57450 : if (sam == special_array_member::int_0)
1879 191 : pref->sizrng[0] = pref->sizrng[1] = 0;
1880 57259 : else if (!pref->trail1special && sam == special_array_member::trail_1)
1881 27 : pref->sizrng[0] = pref->sizrng[1] = 1;
1882 57232 : else if (size && TREE_CODE (size) == INTEGER_CST)
1883 54987 : pref->sizrng[0] = pref->sizrng[1] = wi::to_offset (size);
1884 : else
1885 : {
1886 : /* When the size of the member is unknown it's either a flexible
1887 : array member or a trailing special array member (either zero
1888 : length or one-element). Set the size to the maximum minus
1889 : the constant size of the base object's type. */
1890 2245 : pref->sizrng[0] = 0;
1891 2245 : pref->sizrng[1] = wi::to_offset (TYPE_MAX_VALUE (ptrdiff_type_node));
1892 2245 : if (tree base_size = TYPE_SIZE_UNIT (base_type))
1893 2245 : if (TREE_CODE (base_size) == INTEGER_CST)
1894 2204 : pref->sizrng[1] -= wi::to_offset (base_size);
1895 : }
1896 57450 : }
1897 :
1898 : /* A helper of compute_objsize_r() to determine the size from COMPONENT_REF
1899 : CREF. Return true on success and false on failure. */
1900 :
1901 : static bool
1902 3335986 : handle_component_ref (tree cref, gimple *stmt, bool addr, int ostype,
1903 : access_ref *pref, ssa_name_limit_t &snlim,
1904 : pointer_query *qry)
1905 : {
1906 3335986 : gcc_assert (TREE_CODE (cref) == COMPONENT_REF);
1907 :
1908 3335986 : const tree base = TREE_OPERAND (cref, 0);
1909 3335986 : const tree field = TREE_OPERAND (cref, 1);
1910 3335986 : access_ref base_ref = *pref;
1911 :
1912 : /* Unconditionally determine the size of the base object (it could
1913 : be smaller than the referenced member when the object is stored
1914 : in a buffer with an insufficient size). */
1915 3335986 : if (!compute_objsize_r (base, stmt, addr, 0, &base_ref, snlim, qry))
1916 : return false;
1917 :
1918 : /* Add the offset of the member to the offset into the object computed
1919 : so far. */
1920 3335986 : tree offset = byte_position (field);
1921 3335986 : if (TREE_CODE (offset) == INTEGER_CST)
1922 3335959 : base_ref.add_offset (wi::to_offset (offset));
1923 : else
1924 27 : base_ref.add_max_offset ();
1925 :
1926 3335986 : if (!base_ref.ref)
1927 : /* PREF->REF may have been already set to an SSA_NAME earlier
1928 : to provide better context for diagnostics. In that case,
1929 : leave it unchanged. */
1930 0 : base_ref.ref = base;
1931 :
1932 3335986 : const tree base_type = TREE_TYPE (base);
1933 3335986 : if (TREE_CODE (base_type) == UNION_TYPE)
1934 : /* In accesses through union types consider the entire unions
1935 : rather than just their members. */
1936 : ostype = 0;
1937 :
1938 2932315 : if (ostype == 0)
1939 : {
1940 : /* In OSTYPE zero (for raw memory functions like memcpy), use
1941 : the maximum size instead if the identity of the enclosing
1942 : object cannot be determined. */
1943 3278521 : *pref = base_ref;
1944 3278521 : return true;
1945 : }
1946 :
1947 57465 : pref->ref = field;
1948 :
1949 57465 : if (!addr && POINTER_TYPE_P (TREE_TYPE (field)))
1950 : {
1951 : /* Set maximum size if the reference is to the pointer member
1952 : itself (as opposed to what it points to). */
1953 15 : pref->set_max_size_range ();
1954 15 : return true;
1955 : }
1956 :
1957 57450 : set_component_ref_size (cref, pref);
1958 :
1959 57450 : if (base_ref.size_remaining () < pref->size_remaining ())
1960 : /* Use the base object if it's smaller than the member. */
1961 438 : *pref = base_ref;
1962 :
1963 : return true;
1964 : }
1965 :
1966 : /* A helper of compute_objsize_r() to determine the size from MEM_REF
1967 : MREF. Return true on success and false on failure. */
1968 :
1969 : static bool
1970 3071411 : handle_mem_ref (tree mref, gimple *stmt, int ostype, access_ref *pref,
1971 : ssa_name_limit_t &snlim, pointer_query *qry)
1972 : {
1973 3071411 : gcc_assert (TREE_CODE (mref) == MEM_REF);
1974 :
1975 3071411 : tree mreftype = TYPE_MAIN_VARIANT (TREE_TYPE (mref));
1976 3071411 : if (VECTOR_TYPE_P (mreftype))
1977 : {
1978 : /* Hack: Handle MEM_REFs of vector types as those to complete
1979 : objects; those may be synthesized from multiple assignments
1980 : to consecutive data members (see PR 93200 and 96963).
1981 : FIXME: Vectorized assignments should only be present after
1982 : vectorization so this hack is only necessary after it has
1983 : run and could be avoided in calls from prior passes (e.g.,
1984 : tree-ssa-strlen.cc).
1985 : FIXME: Deal with this more generally, e.g., by marking up
1986 : such MEM_REFs at the time they're created. */
1987 70231 : ostype = 0;
1988 : }
1989 :
1990 3071411 : tree mrefop = TREE_OPERAND (mref, 0);
1991 3071411 : if (!compute_objsize_r (mrefop, stmt, false, ostype, pref, snlim, qry))
1992 : return false;
1993 :
1994 3071405 : ++pref->deref;
1995 :
1996 3071405 : offset_int orng[2];
1997 3071405 : tree off = pref->eval (TREE_OPERAND (mref, 1));
1998 3071405 : range_query *const rvals = qry ? qry->rvals : NULL;
1999 3071405 : if (!get_offset_range (off, stmt, orng, rvals))
2000 : {
2001 : /* Set ORNG to the maximum offset representable in ptrdiff_t. */
2002 0 : orng[1] = wi::to_offset (TYPE_MAX_VALUE (ptrdiff_type_node));
2003 0 : orng[0] = -orng[1] - 1;
2004 : }
2005 :
2006 3071405 : pref->add_offset (orng[0], orng[1]);
2007 3071405 : return true;
2008 : }
2009 :
2010 : /* A helper of compute_objsize_r() to determine the size from SSA_NAME
2011 : PTR. Return true on success and false on failure. */
2012 :
2013 : static bool
2014 6952489 : handle_ssa_name (tree ptr, bool addr, int ostype,
2015 : access_ref *pref, ssa_name_limit_t &snlim,
2016 : pointer_query *qry)
2017 : {
2018 6952489 : if (!snlim.next ())
2019 : return false;
2020 :
2021 : /* Only process an SSA_NAME if the recursion limit has not yet
2022 : been reached. */
2023 6952480 : if (qry)
2024 : {
2025 6952480 : if (++qry->depth > qry->max_depth)
2026 724182 : qry->max_depth = qry->depth;
2027 6952480 : if (const access_ref *cache_ref = qry->get_ref (ptr, ostype))
2028 : {
2029 : /* Add the number of DEREFerences accumulated so far. */
2030 1842563 : const int deref = pref->deref;
2031 1842563 : *pref = *cache_ref;
2032 1842563 : pref->deref += deref;
2033 1842563 : return true;
2034 : }
2035 : }
2036 :
2037 5109917 : gimple *stmt = SSA_NAME_DEF_STMT (ptr);
2038 5109917 : if (is_gimple_call (stmt))
2039 : {
2040 : /* If STMT is a call to an allocation function get the size
2041 : from its argument(s). If successful, also set *PREF->REF
2042 : to PTR for the caller to include in diagnostics. */
2043 2468875 : wide_int wr[2];
2044 493775 : range_query *const rvals = qry ? qry->rvals : NULL;
2045 493775 : if (gimple_call_alloc_size (stmt, wr, rvals))
2046 : {
2047 149866 : pref->ref = ptr;
2048 149866 : pref->sizrng[0] = offset_int::from (wr[0], UNSIGNED);
2049 149866 : pref->sizrng[1] = offset_int::from (wr[1], UNSIGNED);
2050 : /* Constrain both bounds to a valid size. */
2051 149866 : offset_int maxsize = wi::to_offset (max_object_size ());
2052 149866 : if (pref->sizrng[0] > maxsize)
2053 352 : pref->sizrng[0] = maxsize;
2054 149866 : if (pref->sizrng[1] > maxsize)
2055 14538 : pref->sizrng[1] = maxsize;
2056 : }
2057 : else
2058 : {
2059 : /* For functions known to return one of their pointer arguments
2060 : try to determine what the returned pointer points to, and on
2061 : success add OFFRNG which was set to the offset added by
2062 : the function (e.g., memchr) to the overall offset. */
2063 343909 : bool past_end;
2064 343909 : offset_int offrng[2];
2065 343909 : if (tree ret = gimple_call_return_array (stmt, offrng, &past_end,
2066 : snlim, qry))
2067 : {
2068 17459 : if (!compute_objsize_r (ret, stmt, addr, ostype, pref, snlim, qry))
2069 1099 : return false;
2070 :
2071 : /* Cap OFFRNG[1] to at most the remaining size of
2072 : the object. */
2073 16360 : offset_int remrng[2];
2074 16360 : remrng[1] = pref->size_remaining (remrng);
2075 16360 : if (remrng[1] != 0 && !past_end)
2076 : /* Decrement the size for functions that never return
2077 : a past-the-end pointer. */
2078 16059 : remrng[1] -= 1;
2079 :
2080 16360 : if (remrng[1] < offrng[1])
2081 411 : offrng[1] = remrng[1];
2082 16360 : pref->add_offset (offrng[0], offrng[1]);
2083 : }
2084 : else
2085 : {
2086 : /* For other calls that might return arbitrary pointers
2087 : including into the middle of objects set the size
2088 : range to maximum, clear PREF->BASE0, and also set
2089 : PREF->REF to include in diagnostics. */
2090 326450 : pref->set_max_size_range ();
2091 326450 : pref->base0 = false;
2092 326450 : pref->ref = ptr;
2093 : }
2094 : }
2095 492676 : qry->put_ref (ptr, *pref, ostype);
2096 492676 : return true;
2097 1481325 : }
2098 :
2099 4616142 : if (gimple_nop_p (stmt))
2100 : {
2101 : /* For a function argument try to determine the byte size
2102 : of the array from the current function declaratation
2103 : (e.g., attribute access or related). */
2104 4156895 : wide_int wr[2];
2105 831379 : bool static_array = false;
2106 831379 : if (tree ref = gimple_parm_array_size (ptr, wr, &static_array))
2107 : {
2108 185 : pref->parmarray = !static_array;
2109 185 : pref->sizrng[0] = offset_int::from (wr[0], UNSIGNED);
2110 185 : pref->sizrng[1] = offset_int::from (wr[1], UNSIGNED);
2111 185 : pref->ref = ref;
2112 185 : qry->put_ref (ptr, *pref, ostype);
2113 185 : return true;
2114 : }
2115 :
2116 831194 : pref->set_max_size_range ();
2117 831194 : pref->base0 = false;
2118 831194 : pref->ref = ptr;
2119 831194 : qry->put_ref (ptr, *pref, ostype);
2120 831194 : return true;
2121 2494137 : }
2122 :
2123 3784763 : if (gimple_code (stmt) == GIMPLE_PHI)
2124 : {
2125 : /* Pass PTR to get_ref() via PREF. If all PHI arguments refer
2126 : to the same object the function will replace it with it. */
2127 630701 : pref->ref = ptr;
2128 630701 : access_ref phi_ref = *pref;
2129 630701 : if (!pref->get_ref (NULL, &phi_ref, ostype, &snlim, qry))
2130 : return false;
2131 568648 : *pref = phi_ref;
2132 568648 : qry->put_ref (ptr, *pref, ostype);
2133 568648 : return true;
2134 : }
2135 :
2136 3154062 : gassign *assign = dyn_cast<gassign *> (stmt);
2137 3154062 : if (!assign)
2138 : {
2139 : /* Clear BASE0 since the assigned pointer might point into
2140 : the middle of the object, set the maximum size range and,
2141 : if the SSA_NAME refers to a function argumnent, set
2142 : PREF->REF to it. */
2143 7922 : pref->base0 = false;
2144 7922 : pref->set_max_size_range ();
2145 7922 : pref->ref = ptr;
2146 7922 : return true;
2147 : }
2148 :
2149 3146140 : tree_code code = gimple_assign_rhs_code (assign);
2150 :
2151 3146140 : if (code == MAX_EXPR || code == MIN_EXPR)
2152 : {
2153 1333 : if (!handle_min_max_size (ptr, ostype, pref, snlim, qry))
2154 : return false;
2155 :
2156 1333 : qry->put_ref (ptr, *pref, ostype);
2157 1333 : return true;
2158 : }
2159 :
2160 3144807 : tree rhs = gimple_assign_rhs1 (assign);
2161 :
2162 3144807 : if (code == POINTER_PLUS_EXPR
2163 3144807 : && TREE_CODE (TREE_TYPE (rhs)) == POINTER_TYPE)
2164 : {
2165 : /* Compute the size of the object first. */
2166 816512 : if (!compute_objsize_r (rhs, assign, addr, ostype, pref, snlim, qry))
2167 : return false;
2168 :
2169 769020 : offset_int orng[2];
2170 769020 : tree off = gimple_assign_rhs2 (assign);
2171 769020 : range_query *const rvals = qry ? qry->rvals : NULL;
2172 769020 : if (get_offset_range (off, assign, orng, rvals))
2173 648848 : pref->add_offset (orng[0], orng[1]);
2174 : else
2175 120172 : pref->add_max_offset ();
2176 :
2177 769020 : qry->put_ref (ptr, *pref, ostype);
2178 769020 : return true;
2179 : }
2180 :
2181 2328295 : if (code == ADDR_EXPR || code == SSA_NAME)
2182 : {
2183 511480 : if (!compute_objsize_r (rhs, assign, addr, ostype, pref, snlim, qry))
2184 : return false;
2185 510137 : qry->put_ref (ptr, *pref, ostype);
2186 510137 : return true;
2187 : }
2188 :
2189 1816815 : if (ostype > 1 && POINTER_TYPE_P (TREE_TYPE (rhs)))
2190 : {
2191 : /* When determining the qualifiers follow the pointer but
2192 : avoid caching the result. As the pointer is added to
2193 : and/or dereferenced the computed size and offset need
2194 : not be meaningful for other queries involving the same
2195 : pointer. */
2196 0 : if (!compute_objsize_r (rhs, assign, addr, ostype, pref, snlim, qry))
2197 : return false;
2198 :
2199 0 : rhs = pref->ref;
2200 : }
2201 :
2202 : /* (This could also be an assignment from a nonlocal pointer.) Save
2203 : PTR to mention in diagnostics but otherwise treat it as a pointer
2204 : to an unknown object. */
2205 1816815 : pref->ref = rhs;
2206 1816815 : pref->base0 = false;
2207 1816815 : pref->set_max_size_range ();
2208 1816815 : return true;
2209 : }
2210 :
2211 : /* Helper to compute the size of the object referenced by the PTR
2212 : expression which must have pointer type, using Object Size type
2213 : OSTYPE (only the least significant 2 bits are used).
2214 : On success, sets PREF->REF to the DECL of the referenced object
2215 : if it's unique, otherwise to null, PREF->OFFRNG to the range of
2216 : offsets into it, and PREF->SIZRNG to the range of sizes of
2217 : the object(s).
2218 : ADDR is true for an enclosing ADDR_EXPR.
2219 : SNLIM is used to avoid visiting the same PHI operand multiple
2220 : times, and, when nonnull, RVALS to determine range information.
2221 : Returns true on success, false when a meaningful size (or range)
2222 : cannot be determined.
2223 :
2224 : The function is intended for diagnostics and should not be used
2225 : to influence code generation or optimization. */
2226 :
2227 : static bool
2228 22382310 : compute_objsize_r (tree ptr, gimple *stmt, bool addr, int ostype,
2229 : access_ref *pref, ssa_name_limit_t &snlim,
2230 : pointer_query *qry)
2231 : {
2232 22383072 : STRIP_NOPS (ptr);
2233 :
2234 22383072 : if (DECL_P (ptr))
2235 5226093 : return handle_decl (ptr, addr, pref);
2236 :
2237 17156979 : switch (TREE_CODE (ptr))
2238 : {
2239 1800010 : case ADDR_EXPR:
2240 1800010 : {
2241 1800010 : tree ref = TREE_OPERAND (ptr, 0);
2242 1800010 : if (!compute_objsize_r (ref, stmt, true, ostype, pref, snlim, qry))
2243 : return false;
2244 :
2245 1800010 : --pref->deref;
2246 1800010 : return true;
2247 : }
2248 :
2249 4019 : case BIT_FIELD_REF:
2250 4019 : {
2251 4019 : tree ref = TREE_OPERAND (ptr, 0);
2252 4019 : if (!compute_objsize_r (ref, stmt, addr, ostype, pref, snlim, qry))
2253 : return false;
2254 :
2255 4019 : offset_int off = wi::to_offset (pref->eval (TREE_OPERAND (ptr, 2)));
2256 4019 : pref->add_offset (off / BITS_PER_UNIT);
2257 4019 : return true;
2258 : }
2259 :
2260 1008705 : case ARRAY_REF:
2261 1008705 : return handle_array_ref (ptr, stmt, addr, ostype, pref, snlim, qry);
2262 :
2263 3335986 : case COMPONENT_REF:
2264 3335986 : return handle_component_ref (ptr, stmt, addr, ostype, pref, snlim, qry);
2265 :
2266 3071411 : case MEM_REF:
2267 3071411 : return handle_mem_ref (ptr, stmt, ostype, pref, snlim, qry);
2268 :
2269 10848 : case TARGET_MEM_REF:
2270 10848 : {
2271 10848 : tree ref = TREE_OPERAND (ptr, 0);
2272 10848 : if (!compute_objsize_r (ref, stmt, addr, ostype, pref, snlim, qry))
2273 : return false;
2274 :
2275 : /* TODO: Handle remaining operands. Until then, add maximum offset. */
2276 10848 : pref->ref = ptr;
2277 10848 : pref->add_max_offset ();
2278 10848 : return true;
2279 : }
2280 :
2281 626966 : case INTEGER_CST:
2282 : /* Pointer constants other than null smaller than param_min_pagesize
2283 : might be the result of erroneous null pointer addition/subtraction.
2284 : Unless zero is a valid address set size to zero. For null pointers,
2285 : set size to the maximum for now since those may be the result of
2286 : jump threading. Similarly, for values >= param_min_pagesize in
2287 : order to support (type *) 0x7cdeab00. */
2288 626966 : if (integer_zerop (ptr)
2289 675718 : || wi::to_widest (ptr) >= param_min_pagesize)
2290 581916 : pref->set_max_size_range ();
2291 45050 : else if (POINTER_TYPE_P (TREE_TYPE (ptr)))
2292 : {
2293 585 : tree deref_type = TREE_TYPE (TREE_TYPE (ptr));
2294 585 : addr_space_t as = TYPE_ADDR_SPACE (deref_type);
2295 585 : if (targetm.addr_space.zero_address_valid (as))
2296 0 : pref->set_max_size_range ();
2297 : else
2298 : {
2299 585 : pref->sizrng[0] = pref->sizrng[1] = 0;
2300 585 : pref->ref_nullptr_p = true;
2301 : }
2302 : }
2303 : else
2304 44465 : pref->sizrng[0] = pref->sizrng[1] = 0;
2305 :
2306 626966 : pref->ref = ptr;
2307 626966 : return true;
2308 :
2309 257047 : case STRING_CST:
2310 257047 : pref->sizrng[0] = pref->sizrng[1] = TREE_STRING_LENGTH (ptr);
2311 257047 : pref->ref = ptr;
2312 257047 : return true;
2313 :
2314 531 : case POINTER_PLUS_EXPR:
2315 531 : {
2316 531 : tree ref = TREE_OPERAND (ptr, 0);
2317 531 : if (!compute_objsize_r (ref, stmt, addr, ostype, pref, snlim, qry))
2318 : return false;
2319 :
2320 : /* The below only makes sense if the offset is being applied to the
2321 : address of the object. */
2322 531 : if (pref->deref != -1)
2323 : return false;
2324 :
2325 441 : offset_int orng[2];
2326 441 : tree off = pref->eval (TREE_OPERAND (ptr, 1));
2327 441 : if (get_offset_range (off, stmt, orng, qry->rvals))
2328 432 : pref->add_offset (orng[0], orng[1]);
2329 : else
2330 9 : pref->add_max_offset ();
2331 : return true;
2332 : }
2333 :
2334 762 : case VIEW_CONVERT_EXPR:
2335 762 : ptr = TREE_OPERAND (ptr, 0);
2336 762 : return compute_objsize_r (ptr, stmt, addr, ostype, pref, snlim, qry);
2337 :
2338 6952489 : case SSA_NAME:
2339 6952489 : return handle_ssa_name (ptr, addr, ostype, pref, snlim, qry);
2340 :
2341 88205 : default:
2342 88205 : break;
2343 : }
2344 :
2345 : /* Assume all other expressions point into an unknown object
2346 : of the maximum valid size. */
2347 88205 : pref->ref = ptr;
2348 88205 : pref->base0 = false;
2349 88205 : pref->set_max_size_range ();
2350 88205 : if (TREE_CODE (ptr) == SSA_NAME)
2351 0 : qry->put_ref (ptr, *pref);
2352 : return true;
2353 : }
2354 :
2355 : /* A "public" wrapper around the above. Clients should use this overload
2356 : instead. */
2357 :
2358 : tree
2359 11100612 : compute_objsize (tree ptr, gimple *stmt, int ostype, access_ref *pref,
2360 : pointer_query *ptr_qry)
2361 : {
2362 11100612 : pointer_query qry;
2363 11100612 : if (ptr_qry)
2364 11099498 : ptr_qry->depth = 0;
2365 : else
2366 : ptr_qry = &qry;
2367 :
2368 : /* Clear and invalidate in case *PREF is being reused. */
2369 11100612 : pref->offrng[0] = pref->offrng[1] = 0;
2370 11100612 : pref->sizrng[0] = pref->sizrng[1] = -1;
2371 :
2372 11100612 : ssa_name_limit_t snlim;
2373 11100612 : if (!compute_objsize_r (ptr, stmt, false, ostype, pref, snlim, ptr_qry))
2374 : return NULL_TREE;
2375 :
2376 11090849 : offset_int maxsize = pref->size_remaining ();
2377 11090849 : if (pref->base0 && pref->offrng[0] < 0 && pref->offrng[1] >= 0)
2378 109 : pref->offrng[0] = 0;
2379 11090849 : return wide_int_to_tree (sizetype, maxsize);
2380 11100612 : }
2381 :
2382 : /* Transitional wrapper. The function should be removed once callers
2383 : transition to the pointer_query API. */
2384 :
2385 : tree
2386 648605 : compute_objsize (tree ptr, gimple *stmt, int ostype, access_ref *pref,
2387 : range_query *rvals /* = NULL */)
2388 : {
2389 648605 : pointer_query qry;
2390 648605 : qry.rvals = rvals;
2391 648605 : return compute_objsize (ptr, stmt, ostype, pref, &qry);
2392 648605 : }
2393 :
2394 : /* Legacy wrapper around the above. The function should be removed
2395 : once callers transition to one of the two above. */
2396 :
2397 : tree
2398 0 : compute_objsize (tree ptr, gimple *stmt, int ostype, tree *pdecl /* = NULL */,
2399 : tree *poff /* = NULL */, range_query *rvals /* = NULL */)
2400 : {
2401 : /* Set the initial offsets to zero and size to negative to indicate
2402 : none has been computed yet. */
2403 0 : access_ref ref;
2404 0 : tree size = compute_objsize (ptr, stmt, ostype, &ref, rvals);
2405 0 : if (!size || !ref.base0)
2406 : return NULL_TREE;
2407 :
2408 0 : if (pdecl)
2409 0 : *pdecl = ref.ref;
2410 :
2411 0 : if (poff)
2412 0 : *poff = wide_int_to_tree (ptrdiff_type_node, ref.offrng[ref.offrng[0] < 0]);
2413 :
2414 : return size;
2415 : }
2416 :
2417 : /* Determine the offset *FLDOFF of the first byte of a struct member
2418 : of TYPE (possibly recursively) into which the byte offset OFF points,
2419 : starting after the field START_AFTER if it's non-null. On success,
2420 : if nonnull, set *FLDOFF to the offset of the first byte, and return
2421 : the field decl. If nonnull, set *NEXTOFF to the offset of the next
2422 : field (which reflects any padding between the returned field and
2423 : the next). Otherwise, if no such member can be found, return null. */
2424 :
2425 : tree
2426 1570 : field_at_offset (tree type, tree start_after, HOST_WIDE_INT off,
2427 : HOST_WIDE_INT *fldoff /* = nullptr */,
2428 : HOST_WIDE_INT *nextoff /* = nullptr */)
2429 : {
2430 1570 : tree first_fld = TYPE_FIELDS (type);
2431 :
2432 1570 : HOST_WIDE_INT offbuf = 0, nextbuf = 0;
2433 1570 : if (!fldoff)
2434 19 : fldoff = &offbuf;
2435 1570 : if (!nextoff)
2436 720 : nextoff = &nextbuf;
2437 :
2438 1570 : *nextoff = 0;
2439 :
2440 : /* The field to return. */
2441 1570 : tree last_fld = NULL_TREE;
2442 : /* The next field to advance to. */
2443 1570 : tree next_fld = NULL_TREE;
2444 :
2445 : /* NEXT_FLD's cached offset. */
2446 1570 : HOST_WIDE_INT next_pos = -1;
2447 :
2448 1852 : for (tree fld = first_fld; fld; fld = next_fld)
2449 : {
2450 : next_fld = fld;
2451 2049 : do
2452 : /* Advance to the next relevant data member. */
2453 2049 : next_fld = TREE_CHAIN (next_fld);
2454 : while (next_fld
2455 3958 : && (TREE_CODE (next_fld) != FIELD_DECL
2456 1693 : || DECL_ARTIFICIAL (next_fld)));
2457 :
2458 1833 : if (TREE_CODE (fld) != FIELD_DECL || DECL_ARTIFICIAL (fld))
2459 18 : continue;
2460 :
2461 1815 : if (fld == start_after)
2462 0 : continue;
2463 :
2464 1815 : tree fldtype = TREE_TYPE (fld);
2465 : /* The offset of FLD within its immediately enclosing structure. */
2466 1815 : HOST_WIDE_INT fldpos = next_pos < 0 ? int_byte_position (fld) : next_pos;
2467 :
2468 1815 : tree typesize = TYPE_SIZE_UNIT (fldtype);
2469 1815 : if (typesize && TREE_CODE (typesize) != INTEGER_CST)
2470 : /* Bail if FLD is a variable length member. */
2471 : return NULL_TREE;
2472 :
2473 : /* If the size is not available the field is a flexible array
2474 : member. Treat this case as success. */
2475 3620 : HOST_WIDE_INT fldsize = (tree_fits_uhwi_p (typesize)
2476 1815 : ? tree_to_uhwi (typesize)
2477 : : off);
2478 :
2479 : /* If OFF is beyond the end of the current field continue. */
2480 1815 : HOST_WIDE_INT fldend = fldpos + fldsize;
2481 1815 : if (fldend < off)
2482 236 : continue;
2483 :
2484 1579 : if (next_fld)
2485 : {
2486 : /* If OFF is equal to the offset of the next field continue
2487 : to it and skip the array/struct business below. */
2488 1453 : tree pos = byte_position (next_fld);
2489 1453 : if (!tree_fits_shwi_p (pos))
2490 : /* Bail if NEXT_FLD is a variable length member. */
2491 : return NULL_TREE;
2492 1453 : next_pos = tree_to_shwi (pos);
2493 1453 : *nextoff = *fldoff + next_pos;
2494 1453 : if (*nextoff == off && TREE_CODE (type) != UNION_TYPE)
2495 23 : continue;
2496 : }
2497 : else
2498 126 : *nextoff = HOST_WIDE_INT_MAX;
2499 :
2500 : /* OFF refers somewhere into the current field or just past its end,
2501 : which could mean it refers to the next field. */
2502 1556 : if (TREE_CODE (fldtype) == ARRAY_TYPE)
2503 : {
2504 : /* Will be set to the offset of the first byte of the array
2505 : element (which may be an array) of FLDTYPE into which
2506 : OFF - FLDPOS points (which may be past ELTOFF). */
2507 577 : HOST_WIDE_INT eltoff = 0;
2508 577 : if (tree ft = array_elt_at_offset (fldtype, off - fldpos, &eltoff))
2509 577 : fldtype = ft;
2510 : else
2511 0 : continue;
2512 :
2513 : /* Advance the position to include the array element above.
2514 : If OFF - FLPOS refers to a member of FLDTYPE, the member
2515 : will be determined below. */
2516 577 : fldpos += eltoff;
2517 : }
2518 :
2519 1556 : *fldoff += fldpos;
2520 :
2521 1556 : if (TREE_CODE (fldtype) == RECORD_TYPE)
2522 : /* Drill down into the current field if it's a struct. */
2523 850 : fld = field_at_offset (fldtype, start_after, off - fldpos,
2524 : fldoff, nextoff);
2525 :
2526 1556 : last_fld = fld;
2527 :
2528 : /* Unless the offset is just past the end of the field return it.
2529 : Otherwise save it and return it only if the offset of the next
2530 : next field is greater (i.e., there is padding between the two)
2531 : or if there is no next field. */
2532 1556 : if (off < fldend)
2533 : break;
2534 : }
2535 :
2536 1570 : if (*nextoff == HOST_WIDE_INT_MAX && next_fld)
2537 35 : *nextoff = next_pos;
2538 :
2539 : return last_fld;
2540 : }
2541 :
2542 : /* Determine the offset *ELTOFF of the first byte of the array element
2543 : of array ARTYPE into which the byte offset OFF points. On success
2544 : set *ELTOFF to the offset of the first byte and return type.
2545 : Otherwise, if no such element can be found, return null. */
2546 :
2547 : tree
2548 609 : array_elt_at_offset (tree artype, HOST_WIDE_INT off,
2549 : HOST_WIDE_INT *eltoff /* = nullptr */,
2550 : HOST_WIDE_INT *subar_size /* = nullptr */)
2551 : {
2552 609 : gcc_assert (TREE_CODE (artype) == ARRAY_TYPE);
2553 :
2554 609 : HOST_WIDE_INT dummy;
2555 609 : if (!eltoff)
2556 0 : eltoff = &dummy;
2557 609 : if (!subar_size)
2558 577 : subar_size = &dummy;
2559 :
2560 609 : tree eltype = artype;
2561 641 : while (TREE_CODE (TREE_TYPE (eltype)) == ARRAY_TYPE)
2562 32 : eltype = TREE_TYPE (eltype);
2563 :
2564 609 : tree subartype = eltype;
2565 609 : if (RECORD_OR_UNION_TYPE_P (TREE_TYPE (eltype))
2566 593 : || TYPE_MODE (TREE_TYPE (eltype)) != TYPE_MODE (char_type_node))
2567 16 : eltype = TREE_TYPE (eltype);
2568 :
2569 609 : *subar_size = int_size_in_bytes (subartype);
2570 :
2571 609 : if (eltype == artype)
2572 : {
2573 561 : *eltoff = 0;
2574 561 : return artype;
2575 : }
2576 :
2577 48 : HOST_WIDE_INT artype_size = int_size_in_bytes (artype);
2578 48 : HOST_WIDE_INT eltype_size = int_size_in_bytes (eltype);
2579 :
2580 48 : if (off < artype_size)// * eltype_size)
2581 : {
2582 32 : *eltoff = (off / eltype_size) * eltype_size;
2583 32 : return TREE_CODE (eltype) == ARRAY_TYPE ? TREE_TYPE (eltype) : eltype;
2584 : }
2585 :
2586 : return NULL_TREE;
2587 : }
2588 :
2589 : /* Wrapper around build_array_type_nelts that makes sure the array
2590 : can be created at all and handles zero sized arrays specially. */
2591 :
2592 : tree
2593 12082 : build_printable_array_type (tree eltype, unsigned HOST_WIDE_INT nelts)
2594 : {
2595 : /* Cannot build an array type of functions or methods without
2596 : an error diagnostic. */
2597 12082 : if (FUNC_OR_METHOD_TYPE_P (eltype))
2598 : {
2599 1 : tree arrtype = make_node (ARRAY_TYPE);
2600 1 : TREE_TYPE (arrtype) = eltype;
2601 1 : TYPE_SIZE (arrtype) = bitsize_zero_node;
2602 1 : TYPE_SIZE_UNIT (arrtype) = size_zero_node;
2603 1 : return arrtype;
2604 : }
2605 :
2606 12081 : if (TYPE_SIZE_UNIT (eltype)
2607 11807 : && TREE_CODE (TYPE_SIZE_UNIT (eltype)) == INTEGER_CST
2608 11791 : && !integer_zerop (TYPE_SIZE_UNIT (eltype))
2609 11701 : && TYPE_ALIGN_UNIT (eltype) > 1
2610 27567 : && wi::zext (wi::to_wide (TYPE_SIZE_UNIT (eltype)),
2611 27567 : ffs_hwi (TYPE_ALIGN_UNIT (eltype)) - 1) != 0)
2612 3 : eltype = TYPE_MAIN_VARIANT (eltype);
2613 :
2614 : /* Consider excessive NELTS an array of unknown bound. */
2615 12081 : tree idxtype = NULL_TREE;
2616 12081 : if (nelts < HOST_WIDE_INT_MAX)
2617 : {
2618 12048 : if (nelts)
2619 11748 : return build_array_type_nelts (eltype, nelts);
2620 300 : idxtype = build_range_type (sizetype, size_zero_node, NULL_TREE);
2621 : }
2622 :
2623 333 : tree arrtype = build_array_type (eltype, idxtype);
2624 333 : arrtype = build_distinct_type_copy (TYPE_MAIN_VARIANT (arrtype));
2625 333 : TYPE_SIZE (arrtype) = bitsize_zero_node;
2626 333 : TYPE_SIZE_UNIT (arrtype) = size_zero_node;
2627 333 : return arrtype;
2628 : }
|