1/* __builtin_object_size (ptr, object_size_type) computation
2 Copyright (C) 2004-2023 Free Software Foundation, Inc.
3 Contributed by Jakub Jelinek <jakub@redhat.com>
4
5This file is part of GCC.
6
7GCC is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
9the Free Software Foundation; either version 3, or (at your option)
10any later version.
11
12GCC is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along 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 "tree-pass.h"
28#include "ssa.h"
29#include "gimple-pretty-print.h"
30#include "fold-const.h"
31#include "tree-object-size.h"
32#include "gimple-iterator.h"
33#include "gimple-fold.h"
34#include "tree-cfg.h"
35#include "tree-dfa.h"
36#include "stringpool.h"
37#include "attribs.h"
38#include "builtins.h"
39#include "gimplify-me.h"
40
41struct object_size_info
42{
43 int object_size_type;
44 unsigned char pass;
45 bool changed;
46 bitmap visited, reexamine, unknowns;
47 unsigned int *depths;
48 unsigned int *stack, *tos;
49};
50
51struct GTY(()) object_size
52{
53 /* Estimate of bytes till the end of the object. */
54 tree size;
55 /* Estimate of the size of the whole object. */
56 tree wholesize;
57};
58
59static tree compute_object_offset (tree, const_tree);
60static bool addr_object_size (struct object_size_info *,
61 const_tree, int, tree *, tree *t = NULL);
62static tree alloc_object_size (const gcall *, int);
63static tree pass_through_call (const gcall *);
64static void collect_object_sizes_for (struct object_size_info *, tree);
65static void expr_object_size (struct object_size_info *, tree, tree);
66static bool merge_object_sizes (struct object_size_info *, tree, tree);
67static bool plus_stmt_object_size (struct object_size_info *, tree, gimple *);
68static bool cond_expr_object_size (struct object_size_info *, tree, gimple *);
69static void init_offset_limit (void);
70static void check_for_plus_in_loops (struct object_size_info *, tree);
71static void check_for_plus_in_loops_1 (struct object_size_info *, tree,
72 unsigned int);
73
74/* object_sizes[0] is upper bound for the object size and number of bytes till
75 the end of the object.
76 object_sizes[1] is upper bound for the object size and number of bytes till
77 the end of the subobject (innermost array or field with address taken).
78 object_sizes[2] is lower bound for the object size and number of bytes till
79 the end of the object and object_sizes[3] lower bound for subobject.
80
81 For static object sizes, the object size and the bytes till the end of the
82 object are both INTEGER_CST. In the dynamic case, they are finally either a
83 gimple variable or an INTEGER_CST. */
84static vec<object_size> object_sizes[OST_END];
85
86/* Bitmaps what object sizes have been computed already. */
87static bitmap computed[OST_END];
88
89/* Maximum value of offset we consider to be addition. */
90static unsigned HOST_WIDE_INT offset_limit;
91
92/* Tell the generic SSA updater what kind of update is needed after the pass
93 executes. */
94static unsigned todo;
95
96/* Return true if VAL represents an initial size for OBJECT_SIZE_TYPE. */
97
98static inline bool
99size_initval_p (tree val, int object_size_type)
100{
101 return ((object_size_type & OST_MINIMUM)
102 ? integer_all_onesp (val) : integer_zerop (val));
103}
104
105/* Return true if VAL represents an unknown size for OBJECT_SIZE_TYPE. */
106
107static inline bool
108size_unknown_p (tree val, int object_size_type)
109{
110 return ((object_size_type & OST_MINIMUM)
111 ? integer_zerop (val) : integer_all_onesp (val));
112}
113
114/* Return true if VAL represents a valid size for OBJECT_SIZE_TYPE. */
115
116static inline bool
117size_valid_p (tree val, int object_size_type)
118{
119 return ((object_size_type & OST_DYNAMIC) || TREE_CODE (val) == INTEGER_CST);
120}
121
122/* Return true if VAL is usable as an object size in the object_sizes
123 vectors. */
124
125static inline bool
126size_usable_p (tree val)
127{
128 return TREE_CODE (val) == SSA_NAME || TREE_CODE (val) == INTEGER_CST;
129}
130
131/* Return a tree with initial value for OBJECT_SIZE_TYPE. */
132
133static inline tree
134size_initval (int object_size_type)
135{
136 return ((object_size_type & OST_MINIMUM)
137 ? TYPE_MAX_VALUE (sizetype) : size_zero_node);
138}
139
140/* Return a tree with unknown value for OBJECT_SIZE_TYPE. */
141
142static inline tree
143size_unknown (int object_size_type)
144{
145 return ((object_size_type & OST_MINIMUM)
146 ? size_zero_node : TYPE_MAX_VALUE (sizetype));
147}
148
149/* Grow object_sizes[OBJECT_SIZE_TYPE] to num_ssa_names. */
150
151static inline void
152object_sizes_grow (int object_size_type)
153{
154 if (num_ssa_names > object_sizes[object_size_type].length ())
155 object_sizes[object_size_type].safe_grow (num_ssa_names, exact: true);
156}
157
158/* Release object_sizes[OBJECT_SIZE_TYPE]. */
159
160static inline void
161object_sizes_release (int object_size_type)
162{
163 object_sizes[object_size_type].release ();
164}
165
166/* Return true if object_sizes[OBJECT_SIZE_TYPE][VARNO] is unknown. */
167
168static inline bool
169object_sizes_unknown_p (int object_size_type, unsigned varno)
170{
171 return size_unknown_p (val: object_sizes[object_size_type][varno].size,
172 object_size_type);
173}
174
175/* Return the raw size expression for VARNO corresponding to OSI. This returns
176 the TREE_VEC as is and should only be used during gimplification. */
177
178static inline object_size
179object_sizes_get_raw (struct object_size_info *osi, unsigned varno)
180{
181 gcc_assert (osi->pass != 0);
182 return object_sizes[osi->object_size_type][varno];
183}
184
185/* Return a size tree for VARNO corresponding to OSI. If WHOLE is true, return
186 the whole object size. Use this for building size expressions based on size
187 of VARNO. */
188
189static inline tree
190object_sizes_get (struct object_size_info *osi, unsigned varno,
191 bool whole = false)
192{
193 tree ret;
194 int object_size_type = osi->object_size_type;
195
196 if (whole)
197 ret = object_sizes[object_size_type][varno].wholesize;
198 else
199 ret = object_sizes[object_size_type][varno].size;
200
201 if (object_size_type & OST_DYNAMIC)
202 {
203 if (TREE_CODE (ret) == MODIFY_EXPR)
204 return TREE_OPERAND (ret, 0);
205 else if (TREE_CODE (ret) == TREE_VEC)
206 return TREE_VEC_ELT (ret, TREE_VEC_LENGTH (ret) - 1);
207 else
208 gcc_checking_assert (size_usable_p (ret));
209 }
210
211 return ret;
212}
213
214/* Set size for VARNO corresponding to OSI to VAL. */
215
216static inline void
217object_sizes_initialize (struct object_size_info *osi, unsigned varno,
218 tree val, tree wholeval)
219{
220 int object_size_type = osi->object_size_type;
221
222 object_sizes[object_size_type][varno].size = val;
223 object_sizes[object_size_type][varno].wholesize = wholeval;
224}
225
226/* Return a MODIFY_EXPR for cases where SSA and EXPR have the same type. The
227 TREE_VEC is returned only in case of PHI nodes. */
228
229static tree
230bundle_sizes (tree name, tree expr)
231{
232 gcc_checking_assert (TREE_TYPE (name) == sizetype);
233
234 if (TREE_CODE (expr) == TREE_VEC)
235 {
236 TREE_VEC_ELT (expr, TREE_VEC_LENGTH (expr) - 1) = name;
237 return expr;
238 }
239
240 gcc_checking_assert (types_compatible_p (TREE_TYPE (expr), sizetype));
241 return build2 (MODIFY_EXPR, sizetype, name, expr);
242}
243
244/* Set size for VARNO corresponding to OSI to VAL if it is the new minimum or
245 maximum. For static sizes, each element of TREE_VEC is always INTEGER_CST
246 throughout the computation. For dynamic sizes, each element may either be a
247 gimple variable, a MODIFY_EXPR or a TREE_VEC. The MODIFY_EXPR is for
248 expressions that need to be gimplified. TREE_VECs are special, they're
249 emitted only for GIMPLE_PHI and the PHI result variable is the last element
250 of the vector. */
251
252static bool
253object_sizes_set (struct object_size_info *osi, unsigned varno, tree val,
254 tree wholeval)
255{
256 int object_size_type = osi->object_size_type;
257 object_size osize = object_sizes[object_size_type][varno];
258 bool changed = true;
259
260 tree oldval = osize.size;
261 tree old_wholeval = osize.wholesize;
262
263 if (object_size_type & OST_DYNAMIC)
264 {
265 if (bitmap_bit_p (osi->reexamine, varno))
266 {
267 if (size_unknown_p (val, object_size_type))
268 {
269 oldval = object_sizes_get (osi, varno);
270 old_wholeval = object_sizes_get (osi, varno, whole: true);
271 bitmap_set_bit (osi->unknowns, SSA_NAME_VERSION (oldval));
272 bitmap_set_bit (osi->unknowns, SSA_NAME_VERSION (old_wholeval));
273 bitmap_clear_bit (osi->reexamine, varno);
274 }
275 else
276 {
277 val = bundle_sizes (name: oldval, expr: val);
278 wholeval = bundle_sizes (name: old_wholeval, expr: wholeval);
279 }
280 }
281 else
282 {
283 gcc_checking_assert (size_initval_p (oldval, object_size_type));
284 gcc_checking_assert (size_initval_p (old_wholeval,
285 object_size_type));
286 /* For dynamic object sizes, all object sizes that are not gimple
287 variables will need to be gimplified. */
288 if (wholeval != val && !size_usable_p (val: wholeval))
289 {
290 bitmap_set_bit (osi->reexamine, varno);
291 wholeval = bundle_sizes (name: make_ssa_name (sizetype), expr: wholeval);
292 }
293 if (!size_usable_p (val))
294 {
295 bitmap_set_bit (osi->reexamine, varno);
296 tree newval = bundle_sizes (name: make_ssa_name (sizetype), expr: val);
297 if (val == wholeval)
298 wholeval = newval;
299 val = newval;
300 }
301 /* If the new value is a temporary variable, mark it for
302 reexamination. */
303 else if (TREE_CODE (val) == SSA_NAME && !SSA_NAME_DEF_STMT (val))
304 bitmap_set_bit (osi->reexamine, varno);
305 }
306 }
307 else
308 {
309 enum tree_code code = (object_size_type & OST_MINIMUM
310 ? MIN_EXPR : MAX_EXPR);
311
312 val = size_binop (code, val, oldval);
313 wholeval = size_binop (code, wholeval, old_wholeval);
314 changed = (tree_int_cst_compare (t1: val, t2: oldval) != 0
315 || tree_int_cst_compare (t1: old_wholeval, t2: wholeval) != 0);
316 }
317
318 object_sizes[object_size_type][varno].size = val;
319 object_sizes[object_size_type][varno].wholesize = wholeval;
320
321 return changed;
322}
323
324/* Set temporary SSA names for object size and whole size to resolve dependency
325 loops in dynamic size computation. */
326
327static inline void
328object_sizes_set_temp (struct object_size_info *osi, unsigned varno)
329{
330 tree val = object_sizes_get (osi, varno);
331
332 if (size_initval_p (val, object_size_type: osi->object_size_type))
333 object_sizes_set (osi, varno,
334 val: make_ssa_name (sizetype),
335 wholeval: make_ssa_name (sizetype));
336}
337
338/* Initialize OFFSET_LIMIT variable. */
339static void
340init_offset_limit (void)
341{
342 if (tree_fits_uhwi_p (TYPE_MAX_VALUE (sizetype)))
343 offset_limit = tree_to_uhwi (TYPE_MAX_VALUE (sizetype));
344 else
345 offset_limit = -1;
346 offset_limit /= 2;
347}
348
349/* Bytes at end of the object with SZ from offset OFFSET. If WHOLESIZE is not
350 NULL_TREE, use it to get the net offset of the pointer, which should always
351 be positive and hence, be within OFFSET_LIMIT for valid offsets. */
352
353static tree
354size_for_offset (tree sz, tree offset, tree wholesize = NULL_TREE)
355{
356 gcc_checking_assert (types_compatible_p (TREE_TYPE (sz), sizetype));
357
358 /* For negative offsets, if we have a distinct WHOLESIZE, use it to get a net
359 offset from the whole object. */
360 if (wholesize && wholesize != sz
361 && (TREE_CODE (sz) != INTEGER_CST
362 || TREE_CODE (wholesize) != INTEGER_CST
363 || tree_int_cst_compare (t1: sz, t2: wholesize)))
364 {
365 gcc_checking_assert (types_compatible_p (TREE_TYPE (wholesize),
366 sizetype));
367
368 /* Restructure SZ - OFFSET as
369 WHOLESIZE - (WHOLESIZE + OFFSET - SZ) so that the offset part, i.e.
370 WHOLESIZE + OFFSET - SZ is only allowed to be positive. */
371 tree tmp = size_binop (MAX_EXPR, wholesize, sz);
372 offset = fold_build2 (PLUS_EXPR, sizetype, tmp, offset);
373 offset = fold_build2 (MINUS_EXPR, sizetype, offset, sz);
374 sz = tmp;
375 }
376
377 /* Safe to convert now, since a valid net offset should be non-negative. */
378 if (!useless_type_conversion_p (sizetype, TREE_TYPE (offset)))
379 offset = fold_convert (sizetype, offset);
380
381 if (TREE_CODE (offset) == INTEGER_CST)
382 {
383 if (integer_zerop (offset))
384 return sz;
385
386 /* Negative or too large offset even after adjustment, cannot be within
387 bounds of an object. */
388 if (compare_tree_int (offset, offset_limit) > 0)
389 return size_zero_node;
390 }
391
392 return size_binop (MINUS_EXPR, size_binop (MAX_EXPR, sz, offset), offset);
393}
394
395/* Compute offset of EXPR within VAR. Return error_mark_node
396 if unknown. */
397
398static tree
399compute_object_offset (tree expr, const_tree var)
400{
401 enum tree_code code = PLUS_EXPR;
402 tree base, off, t;
403
404 if (expr == var)
405 return size_zero_node;
406
407 switch (TREE_CODE (expr))
408 {
409 case COMPONENT_REF:
410 base = compute_object_offset (TREE_OPERAND (expr, 0), var);
411 if (base == error_mark_node)
412 return base;
413
414 t = TREE_OPERAND (expr, 1);
415 off = size_binop (PLUS_EXPR,
416 component_ref_field_offset (expr),
417 size_int (tree_to_uhwi (DECL_FIELD_BIT_OFFSET (t))
418 / BITS_PER_UNIT));
419 break;
420
421 case REALPART_EXPR:
422 CASE_CONVERT:
423 case VIEW_CONVERT_EXPR:
424 case NON_LVALUE_EXPR:
425 return compute_object_offset (TREE_OPERAND (expr, 0), var);
426
427 case IMAGPART_EXPR:
428 base = compute_object_offset (TREE_OPERAND (expr, 0), var);
429 if (base == error_mark_node)
430 return base;
431
432 off = TYPE_SIZE_UNIT (TREE_TYPE (expr));
433 break;
434
435 case ARRAY_REF:
436 base = compute_object_offset (TREE_OPERAND (expr, 0), var);
437 if (base == error_mark_node)
438 return base;
439
440 t = TREE_OPERAND (expr, 1);
441 tree low_bound, unit_size;
442 low_bound = array_ref_low_bound (CONST_CAST_TREE (expr));
443 unit_size = array_ref_element_size (CONST_CAST_TREE (expr));
444 if (! integer_zerop (low_bound))
445 t = fold_build2 (MINUS_EXPR, TREE_TYPE (t), t, low_bound);
446 if (TREE_CODE (t) == INTEGER_CST && tree_int_cst_sgn (t) < 0)
447 {
448 code = MINUS_EXPR;
449 t = fold_build1 (NEGATE_EXPR, TREE_TYPE (t), t);
450 }
451 t = fold_convert (sizetype, t);
452 off = size_binop (MULT_EXPR, unit_size, t);
453 break;
454
455 case MEM_REF:
456 gcc_assert (TREE_CODE (TREE_OPERAND (expr, 0)) == ADDR_EXPR);
457 return wide_int_to_tree (sizetype, cst: mem_ref_offset (expr));
458
459 default:
460 return error_mark_node;
461 }
462
463 return size_binop (code, base, off);
464}
465
466/* Returns the size of the object designated by DECL considering its
467 initializer if it either has one or if it would not affect its size,
468 otherwise the size of the object without the initializer when MIN
469 is true, else null. An object's initializer affects the object's
470 size if it's a struct type with a flexible array member. */
471
472tree
473decl_init_size (tree decl, bool min)
474{
475 tree size = DECL_SIZE_UNIT (decl);
476 tree type = TREE_TYPE (decl);
477 if (TREE_CODE (type) != RECORD_TYPE)
478 return size;
479
480 tree last = last_field (type);
481 if (!last)
482 return size;
483
484 tree last_type = TREE_TYPE (last);
485 if (TREE_CODE (last_type) != ARRAY_TYPE
486 || TYPE_SIZE (last_type))
487 return size;
488
489 /* Use TYPE_SIZE_UNIT; DECL_SIZE_UNIT sometimes reflects the size
490 of the initializer and sometimes doesn't. */
491 size = TYPE_SIZE_UNIT (type);
492 tree ref = build3 (COMPONENT_REF, type, decl, last, NULL_TREE);
493 tree compsize = component_ref_size (ref);
494 if (!compsize)
495 return min ? size : NULL_TREE;
496
497 /* The size includes tail padding and initializer elements. */
498 tree pos = byte_position (last);
499 size = fold_build2 (PLUS_EXPR, TREE_TYPE (size), pos, compsize);
500 return size;
501}
502
503/* Compute __builtin_object_size for PTR, which is a ADDR_EXPR.
504 OBJECT_SIZE_TYPE is the second argument from __builtin_object_size.
505 If unknown, return size_unknown (object_size_type). */
506
507static bool
508addr_object_size (struct object_size_info *osi, const_tree ptr,
509 int object_size_type, tree *psize, tree *pwholesize)
510{
511 tree pt_var, pt_var_size = NULL_TREE, pt_var_wholesize = NULL_TREE;
512 tree var_size, bytes, wholebytes;
513
514 gcc_assert (TREE_CODE (ptr) == ADDR_EXPR);
515
516 /* Set to unknown and overwrite just before returning if the size
517 could be determined. */
518 *psize = size_unknown (object_size_type);
519 if (pwholesize)
520 *pwholesize = size_unknown (object_size_type);
521
522 pt_var = TREE_OPERAND (ptr, 0);
523 while (handled_component_p (t: pt_var))
524 pt_var = TREE_OPERAND (pt_var, 0);
525
526 if (!pt_var)
527 return false;
528
529 if (TREE_CODE (pt_var) == MEM_REF)
530 {
531 tree sz, wholesize;
532
533 if (!osi || (object_size_type & OST_SUBOBJECT) != 0
534 || TREE_CODE (TREE_OPERAND (pt_var, 0)) != SSA_NAME)
535 {
536 compute_builtin_object_size (TREE_OPERAND (pt_var, 0),
537 object_size_type & ~OST_SUBOBJECT, &sz);
538 wholesize = sz;
539 }
540 else
541 {
542 tree var = TREE_OPERAND (pt_var, 0);
543 if (osi->pass == 0)
544 collect_object_sizes_for (osi, var);
545 if (bitmap_bit_p (computed[object_size_type],
546 SSA_NAME_VERSION (var)))
547 {
548 sz = object_sizes_get (osi, SSA_NAME_VERSION (var));
549 wholesize = object_sizes_get (osi, SSA_NAME_VERSION (var), whole: true);
550 }
551 else
552 sz = wholesize = size_unknown (object_size_type);
553 }
554 if (!size_unknown_p (val: sz, object_size_type))
555 sz = size_for_offset (sz, TREE_OPERAND (pt_var, 1), wholesize);
556
557 if (!size_unknown_p (val: sz, object_size_type)
558 && (TREE_CODE (sz) != INTEGER_CST
559 || compare_tree_int (sz, offset_limit) < 0))
560 {
561 pt_var_size = sz;
562 pt_var_wholesize = wholesize;
563 }
564 }
565 else if (DECL_P (pt_var))
566 {
567 pt_var_size = pt_var_wholesize
568 = decl_init_size (decl: pt_var, min: object_size_type & OST_MINIMUM);
569 if (!pt_var_size)
570 return false;
571 }
572 else if (TREE_CODE (pt_var) == STRING_CST)
573 pt_var_size = pt_var_wholesize = TYPE_SIZE_UNIT (TREE_TYPE (pt_var));
574 else
575 return false;
576
577 if (pt_var_size)
578 {
579 /* Validate the size determined above if it is a constant. */
580 if (TREE_CODE (pt_var_size) == INTEGER_CST
581 && compare_tree_int (pt_var_size, offset_limit) >= 0)
582 return false;
583 }
584
585 if (pt_var != TREE_OPERAND (ptr, 0))
586 {
587 tree var;
588
589 if (object_size_type & OST_SUBOBJECT)
590 {
591 var = TREE_OPERAND (ptr, 0);
592
593 while (var != pt_var
594 && TREE_CODE (var) != BIT_FIELD_REF
595 && TREE_CODE (var) != COMPONENT_REF
596 && TREE_CODE (var) != ARRAY_REF
597 && TREE_CODE (var) != ARRAY_RANGE_REF
598 && TREE_CODE (var) != REALPART_EXPR
599 && TREE_CODE (var) != IMAGPART_EXPR)
600 var = TREE_OPERAND (var, 0);
601 if (var != pt_var && TREE_CODE (var) == ARRAY_REF)
602 var = TREE_OPERAND (var, 0);
603 if (! TYPE_SIZE_UNIT (TREE_TYPE (var))
604 || ! tree_fits_uhwi_p (TYPE_SIZE_UNIT (TREE_TYPE (var)))
605 || (pt_var_size && TREE_CODE (pt_var_size) == INTEGER_CST
606 && tree_int_cst_lt (t1: pt_var_size,
607 TYPE_SIZE_UNIT (TREE_TYPE (var)))))
608 var = pt_var;
609 else if (var != pt_var && TREE_CODE (pt_var) == MEM_REF)
610 {
611 tree v = var;
612 /* For &X->fld, compute object size if fld isn't a flexible array
613 member. */
614 bool is_flexible_array_mem_ref = false;
615 while (v && v != pt_var)
616 switch (TREE_CODE (v))
617 {
618 case ARRAY_REF:
619 if (TYPE_SIZE_UNIT (TREE_TYPE (TREE_OPERAND (v, 0))))
620 {
621 tree domain
622 = TYPE_DOMAIN (TREE_TYPE (TREE_OPERAND (v, 0)));
623 if (domain && TYPE_MAX_VALUE (domain))
624 {
625 v = NULL_TREE;
626 break;
627 }
628 }
629 v = TREE_OPERAND (v, 0);
630 break;
631 case REALPART_EXPR:
632 case IMAGPART_EXPR:
633 v = NULL_TREE;
634 break;
635 case COMPONENT_REF:
636 /* When the ref is not to an aggregate type, i.e, an array,
637 a record or a union, it will not have flexible size,
638 compute the object size directly. */
639 if (!AGGREGATE_TYPE_P (TREE_TYPE (v)))
640 {
641 v = NULL_TREE;
642 break;
643 }
644 /* if the ref is to a record or union type, but the type
645 does not include a flexible array recursively, compute
646 the object size directly. */
647 if (RECORD_OR_UNION_TYPE_P (TREE_TYPE (v)))
648 {
649 if (!TYPE_INCLUDES_FLEXARRAY (TREE_TYPE (v)))
650 {
651 v = NULL_TREE;
652 break;
653 }
654 else
655 {
656 v = TREE_OPERAND (v, 0);
657 break;
658 }
659 }
660 /* Now the ref is to an array type. */
661 gcc_assert (TREE_CODE (TREE_TYPE (v)) == ARRAY_TYPE);
662 is_flexible_array_mem_ref = array_ref_flexible_size_p (v);
663 while (v != pt_var && TREE_CODE (v) == COMPONENT_REF)
664 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (v, 0)))
665 != UNION_TYPE
666 && TREE_CODE (TREE_TYPE (TREE_OPERAND (v, 0)))
667 != QUAL_UNION_TYPE)
668 break;
669 else
670 v = TREE_OPERAND (v, 0);
671 if (TREE_CODE (v) == COMPONENT_REF
672 && TREE_CODE (TREE_TYPE (TREE_OPERAND (v, 0)))
673 == RECORD_TYPE)
674 {
675 /* compute object size only if v is not a
676 flexible array member. */
677 if (!is_flexible_array_mem_ref)
678 {
679 v = NULL_TREE;
680 break;
681 }
682 v = TREE_OPERAND (v, 0);
683 }
684 while (v != pt_var && TREE_CODE (v) == COMPONENT_REF)
685 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (v, 0)))
686 != UNION_TYPE
687 && TREE_CODE (TREE_TYPE (TREE_OPERAND (v, 0)))
688 != QUAL_UNION_TYPE)
689 break;
690 else
691 v = TREE_OPERAND (v, 0);
692 if (v != pt_var)
693 v = NULL_TREE;
694 else
695 v = pt_var;
696 break;
697 default:
698 v = pt_var;
699 break;
700 }
701 if (v == pt_var)
702 var = pt_var;
703 }
704 }
705 else
706 var = pt_var;
707
708 if (var != pt_var)
709 {
710 var_size = TYPE_SIZE_UNIT (TREE_TYPE (var));
711 if (!TREE_CONSTANT (var_size))
712 var_size = get_or_create_ssa_default_def (cfun, var_size);
713 if (!var_size)
714 return false;
715 }
716 else if (!pt_var_size)
717 return false;
718 else
719 var_size = pt_var_size;
720 bytes = compute_object_offset (TREE_OPERAND (ptr, 0), var);
721 if (bytes != error_mark_node)
722 {
723 bytes = size_for_offset (sz: var_size, offset: bytes);
724 if (var != pt_var && pt_var_size && TREE_CODE (pt_var) == MEM_REF)
725 {
726 tree bytes2 = compute_object_offset (TREE_OPERAND (ptr, 0),
727 var: pt_var);
728 if (bytes2 != error_mark_node)
729 {
730 bytes2 = size_for_offset (sz: pt_var_size, offset: bytes2);
731 bytes = size_binop (MIN_EXPR, bytes, bytes2);
732 }
733 }
734 }
735 else
736 bytes = size_unknown (object_size_type);
737
738 wholebytes
739 = object_size_type & OST_SUBOBJECT ? var_size : pt_var_wholesize;
740 }
741 else if (!pt_var_size)
742 return false;
743 else
744 {
745 bytes = pt_var_size;
746 wholebytes = pt_var_wholesize;
747 }
748
749 if (!size_unknown_p (val: bytes, object_size_type)
750 && size_valid_p (val: bytes, object_size_type)
751 && !size_unknown_p (val: bytes, object_size_type)
752 && size_valid_p (val: wholebytes, object_size_type))
753 {
754 *psize = bytes;
755 if (pwholesize)
756 *pwholesize = wholebytes;
757 return true;
758 }
759
760 return false;
761}
762
763
764/* Compute __builtin_object_size for CALL, which is a GIMPLE_CALL.
765 Handles calls to functions declared with attribute alloc_size.
766 OBJECT_SIZE_TYPE is the second argument from __builtin_object_size.
767 If unknown, return size_unknown (object_size_type). */
768
769static tree
770alloc_object_size (const gcall *call, int object_size_type)
771{
772 gcc_assert (is_gimple_call (call));
773
774 tree calltype;
775 tree callfn = gimple_call_fndecl (gs: call);
776 if (callfn)
777 calltype = TREE_TYPE (callfn);
778 else
779 calltype = gimple_call_fntype (gs: call);
780
781 if (!calltype)
782 return size_unknown (object_size_type);
783
784 /* Set to positions of alloc_size arguments. */
785 int arg1 = -1, arg2 = -1;
786 tree alloc_size = lookup_attribute (attr_name: "alloc_size",
787 TYPE_ATTRIBUTES (calltype));
788 if (alloc_size && TREE_VALUE (alloc_size))
789 {
790 tree p = TREE_VALUE (alloc_size);
791
792 arg1 = TREE_INT_CST_LOW (TREE_VALUE (p))-1;
793 if (TREE_CHAIN (p))
794 arg2 = TREE_INT_CST_LOW (TREE_VALUE (TREE_CHAIN (p)))-1;
795 }
796 else if (gimple_call_builtin_p (call, BUILT_IN_NORMAL)
797 && callfn && ALLOCA_FUNCTION_CODE_P (DECL_FUNCTION_CODE (callfn)))
798 arg1 = 0;
799
800 /* Non-const arguments are OK here, let the caller handle constness. */
801 if (arg1 < 0 || arg1 >= (int) gimple_call_num_args (gs: call)
802 || arg2 >= (int) gimple_call_num_args (gs: call))
803 return size_unknown (object_size_type);
804
805 tree bytes = NULL_TREE;
806 if (arg2 >= 0)
807 bytes = size_binop (MULT_EXPR,
808 fold_convert (sizetype, gimple_call_arg (call, arg1)),
809 fold_convert (sizetype, gimple_call_arg (call, arg2)));
810 else if (arg1 >= 0)
811 bytes = fold_convert (sizetype, gimple_call_arg (call, arg1));
812
813 return bytes ? bytes : size_unknown (object_size_type);
814}
815
816/* Compute __builtin_object_size for CALL, which is a call to either
817 BUILT_IN_STRDUP or BUILT_IN_STRNDUP; IS_STRNDUP indicates which it is.
818 OBJECT_SIZE_TYPE is the second argument from __builtin_object_size.
819 If unknown, return size_unknown (object_size_type). */
820
821static tree
822strdup_object_size (const gcall *call, int object_size_type, bool is_strndup)
823{
824 tree src = gimple_call_arg (gs: call, index: 0);
825 tree sz = size_unknown (object_size_type);
826 tree n = NULL_TREE;
827
828 if (is_strndup)
829 n = fold_build2 (PLUS_EXPR, sizetype, size_one_node,
830 gimple_call_arg (call, 1));
831 /* For strdup, simply emit strlen (SRC) + 1 and let the optimizer fold it the
832 way it likes. */
833 else
834 {
835 tree strlen_fn = builtin_decl_implicit (fncode: BUILT_IN_STRLEN);
836 if (strlen_fn)
837 {
838 sz = fold_build2 (PLUS_EXPR, sizetype, size_one_node,
839 build_call_expr (strlen_fn, 1, src));
840 todo = TODO_update_ssa_only_virtuals;
841 }
842 }
843
844 /* In all other cases, return the size of SRC since the object size cannot
845 exceed that. We cannot do this for OST_MINIMUM unless SRC points into a
846 string constant since otherwise the object size could go all the way down
847 to zero. */
848 if (!size_valid_p (val: sz, object_size_type)
849 || size_unknown_p (val: sz, object_size_type))
850 {
851 tree wholesrc = NULL_TREE;
852 if (TREE_CODE (src) == ADDR_EXPR)
853 wholesrc = get_base_address (TREE_OPERAND (src, 0));
854
855 /* If the source points within a string constant, we try to get its
856 length. */
857 if (wholesrc && TREE_CODE (wholesrc) == STRING_CST)
858 {
859 tree len = c_strlen (src, 0);
860 if (len)
861 sz = fold_build2 (PLUS_EXPR, sizetype, size_one_node, len);
862 }
863
864 /* For maximum estimate, our next best guess is the object size of the
865 source. */
866 if (size_unknown_p (val: sz, object_size_type)
867 && !(object_size_type & OST_MINIMUM))
868 compute_builtin_object_size (src, object_size_type, &sz);
869 }
870
871 /* String duplication allocates at least one byte, so we should never fail
872 for OST_MINIMUM. */
873 if ((!size_valid_p (val: sz, object_size_type)
874 || size_unknown_p (val: sz, object_size_type))
875 && (object_size_type & OST_MINIMUM))
876 sz = size_one_node;
877
878 /* Factor in the N. */
879 return n ? fold_build2 (MIN_EXPR, sizetype, n, sz) : sz;
880}
881
882/* If object size is propagated from one of function's arguments directly
883 to its return value, return that argument for GIMPLE_CALL statement CALL.
884 Otherwise return NULL. */
885
886static tree
887pass_through_call (const gcall *call)
888{
889 unsigned rf = gimple_call_return_flags (call);
890 if (rf & ERF_RETURNS_ARG)
891 {
892 unsigned argnum = rf & ERF_RETURN_ARG_MASK;
893 if (argnum < gimple_call_num_args (gs: call))
894 return gimple_call_arg (gs: call, index: argnum);
895 }
896
897 /* __builtin_assume_aligned is intentionally not marked RET1. */
898 if (gimple_call_builtin_p (call, BUILT_IN_ASSUME_ALIGNED))
899 return gimple_call_arg (gs: call, index: 0);
900
901 return NULL_TREE;
902}
903
904/* Emit PHI nodes for size expressions fo. */
905
906static void
907emit_phi_nodes (gimple *stmt, tree size, tree wholesize)
908{
909 tree phires;
910 gphi *wholephi = NULL;
911
912 if (wholesize != size)
913 {
914 phires = TREE_VEC_ELT (wholesize, TREE_VEC_LENGTH (wholesize) - 1);
915 wholephi = create_phi_node (phires, gimple_bb (g: stmt));
916 }
917
918 phires = TREE_VEC_ELT (size, TREE_VEC_LENGTH (size) - 1);
919 gphi *phi = create_phi_node (phires, gimple_bb (g: stmt));
920 gphi *obj_phi = as_a <gphi *> (p: stmt);
921
922 gcc_checking_assert (TREE_CODE (wholesize) == TREE_VEC);
923 gcc_checking_assert (TREE_CODE (size) == TREE_VEC);
924
925 for (unsigned i = 0; i < gimple_phi_num_args (gs: stmt); i++)
926 {
927 gimple_seq seq = NULL;
928 tree wsz = TREE_VEC_ELT (wholesize, i);
929 tree sz = TREE_VEC_ELT (size, i);
930
931 /* If we built an expression, we will need to build statements
932 and insert them on the edge right away. */
933 if (TREE_CODE (wsz) != SSA_NAME)
934 wsz = force_gimple_operand (wsz, &seq, true, NULL);
935 if (TREE_CODE (sz) != SSA_NAME)
936 {
937 gimple_seq s;
938 sz = force_gimple_operand (sz, &s, true, NULL);
939 gimple_seq_add_seq (&seq, s);
940 }
941
942 if (seq)
943 gsi_insert_seq_on_edge (gimple_phi_arg_edge (phi: obj_phi, i), seq);
944
945 if (wholephi)
946 add_phi_arg (wholephi, wsz,
947 gimple_phi_arg_edge (phi: obj_phi, i),
948 gimple_phi_arg_location (phi: obj_phi, i));
949
950 add_phi_arg (phi, sz,
951 gimple_phi_arg_edge (phi: obj_phi, i),
952 gimple_phi_arg_location (phi: obj_phi, i));
953 }
954}
955
956/* Descend through EXPR and return size_unknown if it uses any SSA variable
957 object_size_set or object_size_set_temp generated, which turned out to be
958 size_unknown, as noted in UNKNOWNS. */
959
960static tree
961propagate_unknowns (object_size_info *osi, tree expr)
962{
963 int object_size_type = osi->object_size_type;
964
965 switch (TREE_CODE (expr))
966 {
967 case SSA_NAME:
968 if (bitmap_bit_p (osi->unknowns, SSA_NAME_VERSION (expr)))
969 return size_unknown (object_size_type);
970 return expr;
971
972 case MIN_EXPR:
973 case MAX_EXPR:
974 {
975 tree res = propagate_unknowns (osi, TREE_OPERAND (expr, 0));
976 if (size_unknown_p (val: res, object_size_type))
977 return res;
978
979 res = propagate_unknowns (osi, TREE_OPERAND (expr, 1));
980 if (size_unknown_p (val: res, object_size_type))
981 return res;
982
983 return expr;
984 }
985 case MODIFY_EXPR:
986 {
987 tree res = propagate_unknowns (osi, TREE_OPERAND (expr, 1));
988 if (size_unknown_p (val: res, object_size_type))
989 return res;
990 return expr;
991 }
992 case TREE_VEC:
993 for (int i = 0; i < TREE_VEC_LENGTH (expr); i++)
994 {
995 tree res = propagate_unknowns (osi, TREE_VEC_ELT (expr, i));
996 if (size_unknown_p (val: res, object_size_type))
997 return res;
998 }
999 return expr;
1000 case PLUS_EXPR:
1001 case MINUS_EXPR:
1002 {
1003 tree res = propagate_unknowns (osi, TREE_OPERAND (expr, 0));
1004 if (size_unknown_p (val: res, object_size_type))
1005 return res;
1006
1007 return expr;
1008 }
1009 default:
1010 return expr;
1011 }
1012}
1013
1014/* Walk through size expressions that need reexamination and generate
1015 statements for them. */
1016
1017static void
1018gimplify_size_expressions (object_size_info *osi)
1019{
1020 int object_size_type = osi->object_size_type;
1021 bitmap_iterator bi;
1022 unsigned int i;
1023 bool changed;
1024
1025 /* Step 1: Propagate unknowns into expressions. */
1026 bitmap reexamine = BITMAP_ALLOC (NULL);
1027 bitmap_copy (reexamine, osi->reexamine);
1028 do
1029 {
1030 changed = false;
1031 EXECUTE_IF_SET_IN_BITMAP (reexamine, 0, i, bi)
1032 {
1033 object_size cur = object_sizes_get_raw (osi, varno: i);
1034
1035 if (size_unknown_p (val: propagate_unknowns (osi, expr: cur.size),
1036 object_size_type)
1037 || size_unknown_p (val: propagate_unknowns (osi, expr: cur.wholesize),
1038 object_size_type))
1039 {
1040 object_sizes_set (osi, varno: i,
1041 val: size_unknown (object_size_type),
1042 wholeval: size_unknown (object_size_type));
1043 changed = true;
1044 }
1045 }
1046 bitmap_copy (reexamine, osi->reexamine);
1047 }
1048 while (changed);
1049
1050 /* Release all unknowns. */
1051 EXECUTE_IF_SET_IN_BITMAP (osi->unknowns, 0, i, bi)
1052 release_ssa_name (ssa_name (i));
1053
1054 /* Expand all size expressions to put their definitions close to the objects
1055 for which size is being computed. */
1056 EXECUTE_IF_SET_IN_BITMAP (osi->reexamine, 0, i, bi)
1057 {
1058 gimple_seq seq = NULL;
1059 object_size osize = object_sizes_get_raw (osi, varno: i);
1060
1061 gimple *stmt = SSA_NAME_DEF_STMT (ssa_name (i));
1062 enum gimple_code code = gimple_code (g: stmt);
1063
1064 /* PHI nodes need special attention. */
1065 if (code == GIMPLE_PHI)
1066 emit_phi_nodes (stmt, size: osize.size, wholesize: osize.wholesize);
1067 else
1068 {
1069 tree size_expr = NULL_TREE;
1070
1071 /* Bundle wholesize in with the size to gimplify if needed. */
1072 if (osize.wholesize != osize.size
1073 && !size_usable_p (val: osize.wholesize))
1074 size_expr = size_binop (COMPOUND_EXPR,
1075 osize.wholesize,
1076 osize.size);
1077 else if (!size_usable_p (val: osize.size))
1078 size_expr = osize.size;
1079
1080 if (size_expr)
1081 {
1082 gimple_stmt_iterator gsi;
1083 if (code == GIMPLE_NOP)
1084 gsi = gsi_start_bb (bb: single_succ (ENTRY_BLOCK_PTR_FOR_FN (cfun)));
1085 else
1086 gsi = gsi_for_stmt (stmt);
1087
1088 force_gimple_operand (size_expr, &seq, true, NULL);
1089 gsi_insert_seq_before (&gsi, seq, GSI_CONTINUE_LINKING);
1090 }
1091 }
1092
1093 /* We're done, so replace the MODIFY_EXPRs with the SSA names. */
1094 object_sizes_initialize (osi, varno: i,
1095 val: object_sizes_get (osi, varno: i),
1096 wholeval: object_sizes_get (osi, varno: i, whole: true));
1097 }
1098}
1099
1100/* Compute __builtin_object_size value for PTR and set *PSIZE to
1101 the resulting value. If the declared object is known and PDECL
1102 is nonnull, sets *PDECL to the object's DECL. OBJECT_SIZE_TYPE
1103 is the second argument to __builtin_object_size.
1104 Returns true on success and false when the object size could not
1105 be determined. */
1106
1107bool
1108compute_builtin_object_size (tree ptr, int object_size_type,
1109 tree *psize)
1110{
1111 gcc_assert (object_size_type >= 0 && object_size_type < OST_END);
1112
1113 /* Set to unknown and overwrite just before returning if the size
1114 could be determined. */
1115 *psize = size_unknown (object_size_type);
1116
1117 if (! offset_limit)
1118 init_offset_limit ();
1119
1120 if (TREE_CODE (ptr) == ADDR_EXPR)
1121 return addr_object_size (NULL, ptr, object_size_type, psize);
1122
1123 if (TREE_CODE (ptr) != SSA_NAME
1124 || !POINTER_TYPE_P (TREE_TYPE (ptr)))
1125 return false;
1126
1127 if (computed[object_size_type] == NULL)
1128 {
1129 if (optimize || object_size_type & OST_SUBOBJECT)
1130 return false;
1131
1132 /* When not optimizing, rather than failing, make a small effort
1133 to determine the object size without the full benefit of
1134 the (costly) computation below. */
1135 gimple *def = SSA_NAME_DEF_STMT (ptr);
1136 if (gimple_code (g: def) == GIMPLE_ASSIGN)
1137 {
1138 tree_code code = gimple_assign_rhs_code (gs: def);
1139 if (code == POINTER_PLUS_EXPR)
1140 {
1141 tree offset = gimple_assign_rhs2 (gs: def);
1142 ptr = gimple_assign_rhs1 (gs: def);
1143
1144 if (((object_size_type & OST_DYNAMIC)
1145 || (tree_fits_shwi_p (offset)
1146 && compare_tree_int (offset, offset_limit) <= 0))
1147 && compute_builtin_object_size (ptr, object_size_type,
1148 psize))
1149 {
1150 *psize = size_for_offset (sz: *psize, offset);
1151 return true;
1152 }
1153 }
1154 }
1155 return false;
1156 }
1157
1158 struct object_size_info osi;
1159 osi.object_size_type = object_size_type;
1160 if (!bitmap_bit_p (computed[object_size_type], SSA_NAME_VERSION (ptr)))
1161 {
1162 bitmap_iterator bi;
1163 unsigned int i;
1164
1165 object_sizes_grow (object_size_type);
1166 if (dump_file)
1167 {
1168 fprintf (stream: dump_file, format: "Computing %s %s%sobject size for ",
1169 (object_size_type & OST_MINIMUM) ? "minimum" : "maximum",
1170 (object_size_type & OST_DYNAMIC) ? "dynamic " : "",
1171 (object_size_type & OST_SUBOBJECT) ? "sub" : "");
1172 print_generic_expr (dump_file, ptr, dump_flags);
1173 fprintf (stream: dump_file, format: ":\n");
1174 }
1175
1176 osi.visited = BITMAP_ALLOC (NULL);
1177 osi.reexamine = BITMAP_ALLOC (NULL);
1178
1179 if (object_size_type & OST_DYNAMIC)
1180 osi.unknowns = BITMAP_ALLOC (NULL);
1181 else
1182 {
1183 osi.depths = NULL;
1184 osi.stack = NULL;
1185 osi.tos = NULL;
1186 }
1187
1188 /* First pass: walk UD chains, compute object sizes that
1189 can be computed. osi.reexamine bitmap at the end will
1190 contain what variables were found in dependency cycles
1191 and therefore need to be reexamined. */
1192 osi.pass = 0;
1193 osi.changed = false;
1194 collect_object_sizes_for (&osi, ptr);
1195
1196 if (object_size_type & OST_DYNAMIC)
1197 {
1198 osi.pass = 1;
1199 gimplify_size_expressions (osi: &osi);
1200 BITMAP_FREE (osi.unknowns);
1201 bitmap_clear (osi.reexamine);
1202 }
1203
1204 /* Second pass: keep recomputing object sizes of variables
1205 that need reexamination, until no object sizes are
1206 increased or all object sizes are computed. */
1207 if (! bitmap_empty_p (map: osi.reexamine))
1208 {
1209 bitmap reexamine = BITMAP_ALLOC (NULL);
1210
1211 /* If looking for minimum instead of maximum object size,
1212 detect cases where a pointer is increased in a loop.
1213 Although even without this detection pass 2 would eventually
1214 terminate, it could take a long time. If a pointer is
1215 increasing this way, we need to assume 0 object size.
1216 E.g. p = &buf[0]; while (cond) p = p + 4; */
1217 if (object_size_type & OST_MINIMUM)
1218 {
1219 osi.depths = XCNEWVEC (unsigned int, num_ssa_names);
1220 osi.stack = XNEWVEC (unsigned int, num_ssa_names);
1221 osi.tos = osi.stack;
1222 osi.pass = 1;
1223 /* collect_object_sizes_for is changing
1224 osi.reexamine bitmap, so iterate over a copy. */
1225 bitmap_copy (reexamine, osi.reexamine);
1226 EXECUTE_IF_SET_IN_BITMAP (reexamine, 0, i, bi)
1227 if (bitmap_bit_p (osi.reexamine, i))
1228 check_for_plus_in_loops (&osi, ssa_name (i));
1229
1230 free (ptr: osi.depths);
1231 osi.depths = NULL;
1232 free (ptr: osi.stack);
1233 osi.stack = NULL;
1234 osi.tos = NULL;
1235 }
1236
1237 do
1238 {
1239 osi.pass = 2;
1240 osi.changed = false;
1241 /* collect_object_sizes_for is changing
1242 osi.reexamine bitmap, so iterate over a copy. */
1243 bitmap_copy (reexamine, osi.reexamine);
1244 EXECUTE_IF_SET_IN_BITMAP (reexamine, 0, i, bi)
1245 if (bitmap_bit_p (osi.reexamine, i))
1246 {
1247 collect_object_sizes_for (&osi, ssa_name (i));
1248 if (dump_file && (dump_flags & TDF_DETAILS))
1249 {
1250 fprintf (stream: dump_file, format: "Reexamining ");
1251 print_generic_expr (dump_file, ssa_name (i),
1252 dump_flags);
1253 fprintf (stream: dump_file, format: "\n");
1254 }
1255 }
1256 }
1257 while (osi.changed);
1258
1259 BITMAP_FREE (reexamine);
1260 }
1261 EXECUTE_IF_SET_IN_BITMAP (osi.reexamine, 0, i, bi)
1262 bitmap_set_bit (computed[object_size_type], i);
1263
1264 /* Debugging dumps. */
1265 if (dump_file)
1266 {
1267 EXECUTE_IF_SET_IN_BITMAP (osi.visited, 0, i, bi)
1268 if (!object_sizes_unknown_p (object_size_type, varno: i))
1269 {
1270 print_generic_expr (dump_file, ssa_name (i),
1271 dump_flags);
1272 fprintf (stream: dump_file,
1273 format: ": %s %s%sobject size ",
1274 ((object_size_type & OST_MINIMUM) ? "minimum"
1275 : "maximum"),
1276 (object_size_type & OST_DYNAMIC) ? "dynamic " : "",
1277 (object_size_type & OST_SUBOBJECT) ? "sub" : "");
1278 print_generic_expr (dump_file, object_sizes_get (osi: &osi, varno: i),
1279 dump_flags);
1280 fprintf (stream: dump_file, format: "\n");
1281 }
1282 }
1283
1284 BITMAP_FREE (osi.reexamine);
1285 BITMAP_FREE (osi.visited);
1286 }
1287
1288 *psize = object_sizes_get (osi: &osi, SSA_NAME_VERSION (ptr));
1289 return !size_unknown_p (val: *psize, object_size_type);
1290}
1291
1292/* Compute object_sizes for PTR, defined to VALUE, which is not an SSA_NAME. */
1293
1294static void
1295expr_object_size (struct object_size_info *osi, tree ptr, tree value)
1296{
1297 int object_size_type = osi->object_size_type;
1298 unsigned int varno = SSA_NAME_VERSION (ptr);
1299 tree bytes, wholesize;
1300
1301 gcc_assert (!object_sizes_unknown_p (object_size_type, varno));
1302 gcc_assert (osi->pass == 0);
1303
1304 if (TREE_CODE (value) == WITH_SIZE_EXPR)
1305 value = TREE_OPERAND (value, 0);
1306
1307 /* Pointer variables should have been handled by merge_object_sizes. */
1308 gcc_assert (TREE_CODE (value) != SSA_NAME
1309 || !POINTER_TYPE_P (TREE_TYPE (value)));
1310
1311 if (TREE_CODE (value) == ADDR_EXPR)
1312 addr_object_size (osi, ptr: value, object_size_type, psize: &bytes, pwholesize: &wholesize);
1313 else
1314 bytes = wholesize = size_unknown (object_size_type);
1315
1316 object_sizes_set (osi, varno, val: bytes, wholeval: wholesize);
1317}
1318
1319
1320/* Compute object_sizes for PTR, defined to the result of a call. */
1321
1322static void
1323call_object_size (struct object_size_info *osi, tree ptr, gcall *call)
1324{
1325 int object_size_type = osi->object_size_type;
1326 unsigned int varno = SSA_NAME_VERSION (ptr);
1327 tree bytes = NULL_TREE;
1328
1329 gcc_assert (is_gimple_call (call));
1330
1331 gcc_assert (!object_sizes_unknown_p (object_size_type, varno));
1332 gcc_assert (osi->pass == 0);
1333
1334 bool is_strdup = gimple_call_builtin_p (call, BUILT_IN_STRDUP);
1335 bool is_strndup = gimple_call_builtin_p (call, BUILT_IN_STRNDUP);
1336 if (is_strdup || is_strndup)
1337 bytes = strdup_object_size (call, object_size_type, is_strndup);
1338 else
1339 bytes = alloc_object_size (call, object_size_type);
1340
1341 if (!size_valid_p (val: bytes, object_size_type))
1342 bytes = size_unknown (object_size_type);
1343
1344 object_sizes_set (osi, varno, val: bytes, wholeval: bytes);
1345}
1346
1347
1348/* Compute object_sizes for PTR, defined to an unknown value. */
1349
1350static void
1351unknown_object_size (struct object_size_info *osi, tree ptr)
1352{
1353 int object_size_type = osi->object_size_type;
1354 unsigned int varno = SSA_NAME_VERSION (ptr);
1355
1356 gcc_checking_assert (!object_sizes_unknown_p (object_size_type, varno));
1357 gcc_checking_assert (osi->pass == 0);
1358 tree bytes = size_unknown (object_size_type);
1359
1360 object_sizes_set (osi, varno, val: bytes, wholeval: bytes);
1361}
1362
1363
1364/* Merge object sizes of ORIG + OFFSET into DEST. Return true if
1365 the object size might need reexamination later. */
1366
1367static bool
1368merge_object_sizes (struct object_size_info *osi, tree dest, tree orig)
1369{
1370 int object_size_type = osi->object_size_type;
1371 unsigned int varno = SSA_NAME_VERSION (dest);
1372 tree orig_bytes, wholesize;
1373
1374 if (object_sizes_unknown_p (object_size_type, varno))
1375 return false;
1376
1377 if (osi->pass == 0)
1378 collect_object_sizes_for (osi, orig);
1379
1380 orig_bytes = object_sizes_get (osi, SSA_NAME_VERSION (orig));
1381 wholesize = object_sizes_get (osi, SSA_NAME_VERSION (orig), whole: true);
1382
1383 if (object_sizes_set (osi, varno, val: orig_bytes, wholeval: wholesize))
1384 osi->changed = true;
1385
1386 return bitmap_bit_p (osi->reexamine, SSA_NAME_VERSION (orig));
1387}
1388
1389
1390/* Compute object_sizes for VAR, defined to the result of an assignment
1391 with operator POINTER_PLUS_EXPR. Return true if the object size might
1392 need reexamination later. */
1393
1394static bool
1395plus_stmt_object_size (struct object_size_info *osi, tree var, gimple *stmt)
1396{
1397 int object_size_type = osi->object_size_type;
1398 unsigned int varno = SSA_NAME_VERSION (var);
1399 tree bytes, wholesize;
1400 tree op0, op1;
1401 bool reexamine = false;
1402
1403 if (gimple_assign_rhs_code (gs: stmt) == POINTER_PLUS_EXPR)
1404 {
1405 op0 = gimple_assign_rhs1 (gs: stmt);
1406 op1 = gimple_assign_rhs2 (gs: stmt);
1407 }
1408 else if (gimple_assign_rhs_code (gs: stmt) == ADDR_EXPR)
1409 {
1410 tree rhs = TREE_OPERAND (gimple_assign_rhs1 (stmt), 0);
1411 gcc_assert (TREE_CODE (rhs) == MEM_REF);
1412 op0 = TREE_OPERAND (rhs, 0);
1413 op1 = TREE_OPERAND (rhs, 1);
1414 }
1415 else
1416 gcc_unreachable ();
1417
1418 if (object_sizes_unknown_p (object_size_type, varno))
1419 return false;
1420
1421 /* Handle PTR + OFFSET here. */
1422 if (size_valid_p (val: op1, object_size_type)
1423 && (TREE_CODE (op0) == SSA_NAME || TREE_CODE (op0) == ADDR_EXPR))
1424 {
1425 if (TREE_CODE (op0) == SSA_NAME)
1426 {
1427 if (osi->pass == 0)
1428 collect_object_sizes_for (osi, op0);
1429
1430 bytes = object_sizes_get (osi, SSA_NAME_VERSION (op0));
1431 wholesize = object_sizes_get (osi, SSA_NAME_VERSION (op0), whole: true);
1432 reexamine = bitmap_bit_p (osi->reexamine, SSA_NAME_VERSION (op0));
1433 }
1434 else
1435 {
1436 /* op0 will be ADDR_EXPR here. We should never come here during
1437 reexamination. */
1438 gcc_checking_assert (osi->pass == 0);
1439 addr_object_size (osi, ptr: op0, object_size_type, psize: &bytes, pwholesize: &wholesize);
1440 }
1441
1442 /* size_for_offset doesn't make sense for -1 size, but it does for size 0
1443 since the wholesize could be non-zero and a negative offset could give
1444 a non-zero size. */
1445 if (size_unknown_p (val: bytes, object_size_type: 0))
1446 ;
1447 else if ((object_size_type & OST_DYNAMIC)
1448 || compare_tree_int (op1, offset_limit) <= 0)
1449 bytes = size_for_offset (sz: bytes, offset: op1, wholesize);
1450 /* In the static case, with a negative offset, the best estimate for
1451 minimum size is size_unknown but for maximum size, the wholesize is a
1452 better estimate than size_unknown. */
1453 else if (object_size_type & OST_MINIMUM)
1454 bytes = size_unknown (object_size_type);
1455 else
1456 bytes = wholesize;
1457 }
1458 else
1459 bytes = wholesize = size_unknown (object_size_type);
1460
1461 if (!size_valid_p (val: bytes, object_size_type)
1462 || !size_valid_p (val: wholesize, object_size_type))
1463 bytes = wholesize = size_unknown (object_size_type);
1464
1465 if (object_sizes_set (osi, varno, val: bytes, wholeval: wholesize))
1466 osi->changed = true;
1467 return reexamine;
1468}
1469
1470/* Compute the dynamic object size for VAR. Return the result in SIZE and
1471 WHOLESIZE. */
1472
1473static void
1474dynamic_object_size (struct object_size_info *osi, tree var,
1475 tree *size, tree *wholesize)
1476{
1477 int object_size_type = osi->object_size_type;
1478
1479 if (TREE_CODE (var) == SSA_NAME)
1480 {
1481 unsigned varno = SSA_NAME_VERSION (var);
1482
1483 collect_object_sizes_for (osi, var);
1484 *size = object_sizes_get (osi, varno);
1485 *wholesize = object_sizes_get (osi, varno, whole: true);
1486 }
1487 else if (TREE_CODE (var) == ADDR_EXPR)
1488 addr_object_size (osi, ptr: var, object_size_type, psize: size, pwholesize: wholesize);
1489 else
1490 *size = *wholesize = size_unknown (object_size_type);
1491}
1492
1493/* Compute object_sizes for VAR, defined at STMT, which is
1494 a COND_EXPR. Return true if the object size might need reexamination
1495 later. */
1496
1497static bool
1498cond_expr_object_size (struct object_size_info *osi, tree var, gimple *stmt)
1499{
1500 tree then_, else_;
1501 int object_size_type = osi->object_size_type;
1502 unsigned int varno = SSA_NAME_VERSION (var);
1503 bool reexamine = false;
1504
1505 gcc_assert (gimple_assign_rhs_code (stmt) == COND_EXPR);
1506
1507 if (object_sizes_unknown_p (object_size_type, varno))
1508 return false;
1509
1510 then_ = gimple_assign_rhs2 (gs: stmt);
1511 else_ = gimple_assign_rhs3 (gs: stmt);
1512
1513 if (object_size_type & OST_DYNAMIC)
1514 {
1515 tree then_size, then_wholesize, else_size, else_wholesize;
1516
1517 dynamic_object_size (osi, var: then_, size: &then_size, wholesize: &then_wholesize);
1518 if (!size_unknown_p (val: then_size, object_size_type))
1519 dynamic_object_size (osi, var: else_, size: &else_size, wholesize: &else_wholesize);
1520
1521 tree cond_size, cond_wholesize;
1522 if (size_unknown_p (val: then_size, object_size_type)
1523 || size_unknown_p (val: else_size, object_size_type))
1524 cond_size = cond_wholesize = size_unknown (object_size_type);
1525 else
1526 {
1527 cond_size = fold_build3 (COND_EXPR, sizetype,
1528 gimple_assign_rhs1 (stmt),
1529 then_size, else_size);
1530 cond_wholesize = fold_build3 (COND_EXPR, sizetype,
1531 gimple_assign_rhs1 (stmt),
1532 then_wholesize, else_wholesize);
1533 }
1534
1535 object_sizes_set (osi, varno, val: cond_size, wholeval: cond_wholesize);
1536
1537 return false;
1538 }
1539
1540 if (TREE_CODE (then_) == SSA_NAME)
1541 reexamine |= merge_object_sizes (osi, dest: var, orig: then_);
1542 else
1543 expr_object_size (osi, ptr: var, value: then_);
1544
1545 if (object_sizes_unknown_p (object_size_type, varno))
1546 return reexamine;
1547
1548 if (TREE_CODE (else_) == SSA_NAME)
1549 reexamine |= merge_object_sizes (osi, dest: var, orig: else_);
1550 else
1551 expr_object_size (osi, ptr: var, value: else_);
1552
1553 return reexamine;
1554}
1555
1556/* Find size of an object passed as a parameter to the function. */
1557
1558static void
1559parm_object_size (struct object_size_info *osi, tree var)
1560{
1561 int object_size_type = osi->object_size_type;
1562 tree parm = SSA_NAME_VAR (var);
1563
1564 if (!(object_size_type & OST_DYNAMIC) || !POINTER_TYPE_P (TREE_TYPE (parm)))
1565 {
1566 expr_object_size (osi, ptr: var, value: parm);
1567 return;
1568 }
1569
1570 /* Look for access attribute. */
1571 rdwr_map rdwr_idx;
1572
1573 tree fndecl = cfun->decl;
1574 const attr_access *access = get_parm_access (rdwr_idx, parm, fndecl);
1575 tree typesize = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (parm)));
1576 tree sz = NULL_TREE;
1577
1578 /* If we have an access attribute with a usable size argument... */
1579 if (access && access->sizarg != UINT_MAX
1580 /* ... and either PARM is void * or has a type that is complete and has a
1581 constant size... */
1582 && ((typesize && poly_int_tree_p (t: typesize))
1583 || (!typesize && VOID_TYPE_P (TREE_TYPE (TREE_TYPE (parm))))))
1584 {
1585 tree fnargs = DECL_ARGUMENTS (fndecl);
1586 tree arg = NULL_TREE;
1587 unsigned argpos = 0;
1588
1589 /* ... then walk through the parameters to pick the size parameter and
1590 safely scale it by the type size if needed.
1591
1592 TODO: we could also compute the size of VLAs where the size is
1593 given by a function parameter. */
1594 for (arg = fnargs; arg; arg = TREE_CHAIN (arg), ++argpos)
1595 if (argpos == access->sizarg)
1596 {
1597 gcc_assert (INTEGRAL_TYPE_P (TREE_TYPE (arg)));
1598 sz = get_or_create_ssa_default_def (cfun, arg);
1599 if (sz != NULL_TREE)
1600 {
1601 sz = fold_convert (sizetype, sz);
1602 if (typesize)
1603 sz = size_binop (MULT_EXPR, sz, typesize);
1604 }
1605 break;
1606 }
1607 }
1608 if (!sz)
1609 sz = size_unknown (object_size_type);
1610
1611 object_sizes_set (osi, SSA_NAME_VERSION (var), val: sz, wholeval: sz);
1612}
1613
1614/* Compute an object size expression for VAR, which is the result of a PHI
1615 node. */
1616
1617static void
1618phi_dynamic_object_size (struct object_size_info *osi, tree var)
1619{
1620 int object_size_type = osi->object_size_type;
1621 unsigned int varno = SSA_NAME_VERSION (var);
1622 gimple *stmt = SSA_NAME_DEF_STMT (var);
1623 unsigned i, num_args = gimple_phi_num_args (gs: stmt);
1624 bool wholesize_needed = false;
1625
1626 /* The extra space is for the PHI result at the end, which object_sizes_set
1627 sets for us. */
1628 tree sizes = make_tree_vec (num_args + 1);
1629 tree wholesizes = make_tree_vec (num_args + 1);
1630
1631 /* Bail out if the size of any of the PHI arguments cannot be
1632 determined. */
1633 for (i = 0; i < num_args; i++)
1634 {
1635 edge e = gimple_phi_arg_edge (phi: as_a <gphi *> (p: stmt), i);
1636 if (e->flags & EDGE_COMPLEX)
1637 break;
1638
1639 tree rhs = gimple_phi_arg_def (gs: stmt, index: i);
1640 tree size, wholesize;
1641
1642 dynamic_object_size (osi, var: rhs, size: &size, wholesize: &wholesize);
1643
1644 if (size_unknown_p (val: size, object_size_type))
1645 break;
1646
1647 if (size != wholesize)
1648 wholesize_needed = true;
1649
1650 TREE_VEC_ELT (sizes, i) = size;
1651 TREE_VEC_ELT (wholesizes, i) = wholesize;
1652 }
1653
1654 if (i < num_args)
1655 {
1656 ggc_free (sizes);
1657 ggc_free (wholesizes);
1658 sizes = wholesizes = size_unknown (object_size_type);
1659 }
1660
1661 /* Point to the same TREE_VEC so that we can avoid emitting two PHI
1662 nodes. */
1663 else if (!wholesize_needed)
1664 {
1665 ggc_free (wholesizes);
1666 wholesizes = sizes;
1667 }
1668
1669 object_sizes_set (osi, varno, val: sizes, wholeval: wholesizes);
1670}
1671
1672/* Compute object sizes for VAR.
1673 For ADDR_EXPR an object size is the number of remaining bytes
1674 to the end of the object (where what is considered an object depends on
1675 OSI->object_size_type).
1676 For allocation GIMPLE_CALL like malloc or calloc object size is the size
1677 of the allocation.
1678 For POINTER_PLUS_EXPR where second operand is a constant integer,
1679 object size is object size of the first operand minus the constant.
1680 If the constant is bigger than the number of remaining bytes until the
1681 end of the object, object size is 0, but if it is instead a pointer
1682 subtraction, object size is size_unknown (object_size_type).
1683 To differentiate addition from subtraction, ADDR_EXPR returns
1684 size_unknown (object_size_type) for all objects bigger than half of the
1685 address space, and constants less than half of the address space are
1686 considered addition, while bigger constants subtraction.
1687 For a memcpy like GIMPLE_CALL that always returns one of its arguments, the
1688 object size is object size of that argument.
1689 Otherwise, object size is the maximum of object sizes of variables
1690 that it might be set to. */
1691
1692static void
1693collect_object_sizes_for (struct object_size_info *osi, tree var)
1694{
1695 int object_size_type = osi->object_size_type;
1696 unsigned int varno = SSA_NAME_VERSION (var);
1697 gimple *stmt;
1698 bool reexamine;
1699
1700 if (bitmap_bit_p (computed[object_size_type], varno))
1701 return;
1702
1703 if (osi->pass == 0)
1704 {
1705 if (bitmap_set_bit (osi->visited, varno))
1706 {
1707 /* Initialize to 0 for maximum size and M1U for minimum size so that
1708 it gets immediately overridden. */
1709 object_sizes_initialize (osi, varno,
1710 val: size_initval (object_size_type),
1711 wholeval: size_initval (object_size_type));
1712 }
1713 else
1714 {
1715 /* Found a dependency loop. Mark the variable for later
1716 re-examination. */
1717 if (object_size_type & OST_DYNAMIC)
1718 object_sizes_set_temp (osi, varno);
1719
1720 bitmap_set_bit (osi->reexamine, varno);
1721 if (dump_file && (dump_flags & TDF_DETAILS))
1722 {
1723 fprintf (stream: dump_file, format: "Found a dependency loop at ");
1724 print_generic_expr (dump_file, var, dump_flags);
1725 fprintf (stream: dump_file, format: "\n");
1726 }
1727 return;
1728 }
1729 }
1730
1731 if (dump_file && (dump_flags & TDF_DETAILS))
1732 {
1733 fprintf (stream: dump_file, format: "Visiting use-def links for ");
1734 print_generic_expr (dump_file, var, dump_flags);
1735 fprintf (stream: dump_file, format: "\n");
1736 }
1737
1738 stmt = SSA_NAME_DEF_STMT (var);
1739 reexamine = false;
1740
1741 switch (gimple_code (g: stmt))
1742 {
1743 case GIMPLE_ASSIGN:
1744 {
1745 tree rhs = gimple_assign_rhs1 (gs: stmt);
1746 if (gimple_assign_rhs_code (gs: stmt) == POINTER_PLUS_EXPR
1747 || (gimple_assign_rhs_code (gs: stmt) == ADDR_EXPR
1748 && TREE_CODE (TREE_OPERAND (rhs, 0)) == MEM_REF))
1749 reexamine = plus_stmt_object_size (osi, var, stmt);
1750 else if (gimple_assign_rhs_code (gs: stmt) == COND_EXPR)
1751 reexamine = cond_expr_object_size (osi, var, stmt);
1752 else if (gimple_assign_single_p (gs: stmt)
1753 || gimple_assign_unary_nop_p (stmt))
1754 {
1755 if (TREE_CODE (rhs) == SSA_NAME
1756 && POINTER_TYPE_P (TREE_TYPE (rhs)))
1757 reexamine = merge_object_sizes (osi, dest: var, orig: rhs);
1758 else
1759 expr_object_size (osi, ptr: var, value: rhs);
1760 }
1761 else
1762 unknown_object_size (osi, ptr: var);
1763 break;
1764 }
1765
1766 case GIMPLE_CALL:
1767 {
1768 gcall *call_stmt = as_a <gcall *> (p: stmt);
1769 tree arg = pass_through_call (call: call_stmt);
1770 if (arg)
1771 {
1772 if (TREE_CODE (arg) == SSA_NAME
1773 && POINTER_TYPE_P (TREE_TYPE (arg)))
1774 reexamine = merge_object_sizes (osi, dest: var, orig: arg);
1775 else
1776 expr_object_size (osi, ptr: var, value: arg);
1777 }
1778 else
1779 call_object_size (osi, ptr: var, call: call_stmt);
1780 break;
1781 }
1782
1783 case GIMPLE_ASM:
1784 /* Pointers defined by __asm__ statements can point anywhere. */
1785 unknown_object_size (osi, ptr: var);
1786 break;
1787
1788 case GIMPLE_NOP:
1789 if (SSA_NAME_VAR (var)
1790 && TREE_CODE (SSA_NAME_VAR (var)) == PARM_DECL)
1791 parm_object_size (osi, var);
1792 else
1793 /* Uninitialized SSA names point nowhere. */
1794 unknown_object_size (osi, ptr: var);
1795 break;
1796
1797 case GIMPLE_PHI:
1798 {
1799 unsigned i;
1800
1801 if (object_size_type & OST_DYNAMIC)
1802 {
1803 phi_dynamic_object_size (osi, var);
1804 break;
1805 }
1806
1807 for (i = 0; i < gimple_phi_num_args (gs: stmt); i++)
1808 {
1809 tree rhs = gimple_phi_arg (gs: stmt, index: i)->def;
1810
1811 if (object_sizes_unknown_p (object_size_type, varno))
1812 break;
1813
1814 if (TREE_CODE (rhs) == SSA_NAME)
1815 reexamine |= merge_object_sizes (osi, dest: var, orig: rhs);
1816 else if (osi->pass == 0)
1817 expr_object_size (osi, ptr: var, value: rhs);
1818 }
1819 break;
1820 }
1821
1822 default:
1823 gcc_unreachable ();
1824 }
1825
1826 if (! reexamine || object_sizes_unknown_p (object_size_type, varno))
1827 {
1828 bitmap_set_bit (computed[object_size_type], varno);
1829 if (!(object_size_type & OST_DYNAMIC))
1830 bitmap_clear_bit (osi->reexamine, varno);
1831 }
1832 else
1833 {
1834 bitmap_set_bit (osi->reexamine, varno);
1835 if (dump_file && (dump_flags & TDF_DETAILS))
1836 {
1837 fprintf (stream: dump_file, format: "Need to reexamine ");
1838 print_generic_expr (dump_file, var, dump_flags);
1839 fprintf (stream: dump_file, format: "\n");
1840 }
1841 }
1842}
1843
1844
1845/* Helper function for check_for_plus_in_loops. Called recursively
1846 to detect loops. */
1847
1848static void
1849check_for_plus_in_loops_1 (struct object_size_info *osi, tree var,
1850 unsigned int depth)
1851{
1852 gimple *stmt = SSA_NAME_DEF_STMT (var);
1853 unsigned int varno = SSA_NAME_VERSION (var);
1854
1855 if (osi->depths[varno])
1856 {
1857 if (osi->depths[varno] != depth)
1858 {
1859 unsigned int *sp;
1860
1861 /* Found a loop involving pointer addition. */
1862 for (sp = osi->tos; sp > osi->stack; )
1863 {
1864 --sp;
1865 bitmap_clear_bit (osi->reexamine, *sp);
1866 bitmap_set_bit (computed[osi->object_size_type], *sp);
1867 object_sizes_set (osi, varno: *sp, size_zero_node,
1868 wholeval: object_sizes_get (osi, varno: *sp, whole: true));
1869 if (*sp == varno)
1870 break;
1871 }
1872 }
1873 return;
1874 }
1875 else if (! bitmap_bit_p (osi->reexamine, varno))
1876 return;
1877
1878 osi->depths[varno] = depth;
1879 *osi->tos++ = varno;
1880
1881 switch (gimple_code (g: stmt))
1882 {
1883
1884 case GIMPLE_ASSIGN:
1885 {
1886 if ((gimple_assign_single_p (gs: stmt)
1887 || gimple_assign_unary_nop_p (stmt))
1888 && TREE_CODE (gimple_assign_rhs1 (stmt)) == SSA_NAME)
1889 {
1890 tree rhs = gimple_assign_rhs1 (gs: stmt);
1891
1892 check_for_plus_in_loops_1 (osi, var: rhs, depth);
1893 }
1894 else if (gimple_assign_rhs_code (gs: stmt) == POINTER_PLUS_EXPR)
1895 {
1896 tree basevar = gimple_assign_rhs1 (gs: stmt);
1897 tree cst = gimple_assign_rhs2 (gs: stmt);
1898
1899 gcc_assert (TREE_CODE (cst) == INTEGER_CST);
1900
1901 check_for_plus_in_loops_1 (osi, var: basevar,
1902 depth: depth + !integer_zerop (cst));
1903 }
1904 else
1905 gcc_unreachable ();
1906 break;
1907 }
1908
1909 case GIMPLE_CALL:
1910 {
1911 gcall *call_stmt = as_a <gcall *> (p: stmt);
1912 tree arg = pass_through_call (call: call_stmt);
1913 if (arg)
1914 {
1915 if (TREE_CODE (arg) == SSA_NAME)
1916 check_for_plus_in_loops_1 (osi, var: arg, depth);
1917 else
1918 gcc_unreachable ();
1919 }
1920 break;
1921 }
1922
1923 case GIMPLE_PHI:
1924 {
1925 unsigned i;
1926
1927 for (i = 0; i < gimple_phi_num_args (gs: stmt); i++)
1928 {
1929 tree rhs = gimple_phi_arg (gs: stmt, index: i)->def;
1930
1931 if (TREE_CODE (rhs) == SSA_NAME)
1932 check_for_plus_in_loops_1 (osi, var: rhs, depth);
1933 }
1934 break;
1935 }
1936
1937 default:
1938 gcc_unreachable ();
1939 }
1940
1941 osi->depths[varno] = 0;
1942 osi->tos--;
1943}
1944
1945
1946/* Check if some pointer we are computing object size of is being increased
1947 within a loop. If yes, assume all the SSA variables participating in
1948 that loop have minimum object sizes 0. */
1949
1950static void
1951check_for_plus_in_loops (struct object_size_info *osi, tree var)
1952{
1953 gimple *stmt = SSA_NAME_DEF_STMT (var);
1954
1955 /* NOTE: In the pre-tuples code, we handled a CALL_EXPR here,
1956 and looked for a POINTER_PLUS_EXPR in the pass-through
1957 argument, if any. In GIMPLE, however, such an expression
1958 is not a valid call operand. */
1959
1960 if (is_gimple_assign (gs: stmt)
1961 && gimple_assign_rhs_code (gs: stmt) == POINTER_PLUS_EXPR)
1962 {
1963 tree basevar = gimple_assign_rhs1 (gs: stmt);
1964 tree cst = gimple_assign_rhs2 (gs: stmt);
1965
1966 gcc_assert (TREE_CODE (cst) == INTEGER_CST);
1967
1968 /* Skip non-positive offsets. */
1969 if (integer_zerop (cst) || compare_tree_int (cst, offset_limit) > 0)
1970 return;
1971
1972 osi->depths[SSA_NAME_VERSION (basevar)] = 1;
1973 *osi->tos++ = SSA_NAME_VERSION (basevar);
1974 check_for_plus_in_loops_1 (osi, var, depth: 2);
1975 osi->depths[SSA_NAME_VERSION (basevar)] = 0;
1976 osi->tos--;
1977 }
1978}
1979
1980
1981/* Initialize data structures for the object size computation. */
1982
1983void
1984init_object_sizes (void)
1985{
1986 int object_size_type;
1987
1988 if (computed[0])
1989 return;
1990
1991 for (object_size_type = 0; object_size_type < OST_END; object_size_type++)
1992 {
1993 object_sizes_grow (object_size_type);
1994 computed[object_size_type] = BITMAP_ALLOC (NULL);
1995 }
1996
1997 init_offset_limit ();
1998}
1999
2000
2001/* Destroy data structures after the object size computation. */
2002
2003void
2004fini_object_sizes (void)
2005{
2006 int object_size_type;
2007
2008 for (object_size_type = 0; object_size_type < OST_END; object_size_type++)
2009 {
2010 object_sizes_release (object_size_type);
2011 BITMAP_FREE (computed[object_size_type]);
2012 }
2013}
2014
2015/* Dummy valueize function. */
2016
2017static tree
2018do_valueize (tree t)
2019{
2020 return t;
2021}
2022
2023/* Process a __builtin_object_size or __builtin_dynamic_object_size call in
2024 CALL early for subobjects before any object information is lost due to
2025 optimization. Insert a MIN or MAX expression of the result and
2026 __builtin_object_size at I so that it may be processed in the second pass.
2027 __builtin_dynamic_object_size is treated like __builtin_object_size here
2028 since we're only looking for constant bounds. */
2029
2030static void
2031early_object_sizes_execute_one (gimple_stmt_iterator *i, gimple *call)
2032{
2033 tree ost = gimple_call_arg (gs: call, index: 1);
2034 tree lhs = gimple_call_lhs (gs: call);
2035 gcc_assert (lhs != NULL_TREE);
2036
2037 if (!tree_fits_uhwi_p (ost))
2038 return;
2039
2040 unsigned HOST_WIDE_INT object_size_type = tree_to_uhwi (ost);
2041 tree ptr = gimple_call_arg (gs: call, index: 0);
2042
2043 if (object_size_type != 1 && object_size_type != 3)
2044 return;
2045
2046 if (TREE_CODE (ptr) != ADDR_EXPR && TREE_CODE (ptr) != SSA_NAME)
2047 return;
2048
2049 tree type = TREE_TYPE (lhs);
2050 tree bytes;
2051 if (!compute_builtin_object_size (ptr, object_size_type, psize: &bytes)
2052 || !int_fits_type_p (bytes, type))
2053 return;
2054
2055 tree tem = make_ssa_name (var: type);
2056 gimple_call_set_lhs (gs: call, lhs: tem);
2057 enum tree_code code = object_size_type & OST_MINIMUM ? MAX_EXPR : MIN_EXPR;
2058 tree cst = fold_convert (type, bytes);
2059 gimple *g = gimple_build_assign (lhs, code, tem, cst);
2060 gsi_insert_after (i, g, GSI_NEW_STMT);
2061 update_stmt (s: call);
2062}
2063
2064/* Attempt to fold one __builtin_dynamic_object_size call in CALL into an
2065 expression and insert it at I. Return true if it succeeds. */
2066
2067static bool
2068dynamic_object_sizes_execute_one (gimple_stmt_iterator *i, gimple *call)
2069{
2070 gcc_assert (gimple_call_num_args (call) == 2);
2071
2072 tree args[2];
2073 args[0] = gimple_call_arg (gs: call, index: 0);
2074 args[1] = gimple_call_arg (gs: call, index: 1);
2075
2076 location_t loc = EXPR_LOC_OR_LOC (args[0], input_location);
2077 tree result_type = gimple_call_return_type (gs: as_a <gcall *> (p: call));
2078 tree result = fold_builtin_call_array (loc, result_type,
2079 gimple_call_fn (gs: call), 2, args);
2080
2081 if (!result)
2082 return false;
2083
2084 /* fold_builtin_call_array may wrap the result inside a
2085 NOP_EXPR. */
2086 STRIP_NOPS (result);
2087 gimplify_and_update_call_from_tree (i, result);
2088
2089 if (dump_file && (dump_flags & TDF_DETAILS))
2090 {
2091 fprintf (stream: dump_file, format: "Simplified (dynamic)\n ");
2092 print_gimple_stmt (dump_file, call, 0, dump_flags);
2093 fprintf (stream: dump_file, format: " to ");
2094 print_generic_expr (dump_file, result);
2095 fprintf (stream: dump_file, format: "\n");
2096 }
2097 return true;
2098}
2099
2100static unsigned int
2101object_sizes_execute (function *fun, bool early)
2102{
2103 todo = 0;
2104
2105 basic_block bb;
2106 FOR_EACH_BB_FN (bb, fun)
2107 {
2108 gimple_stmt_iterator i;
2109 for (i = gsi_start_bb (bb); !gsi_end_p (i); gsi_next (i: &i))
2110 {
2111 tree result;
2112 bool dynamic = false;
2113
2114 gimple *call = gsi_stmt (i);
2115 if (gimple_call_builtin_p (call, BUILT_IN_DYNAMIC_OBJECT_SIZE))
2116 dynamic = true;
2117 else if (!gimple_call_builtin_p (call, BUILT_IN_OBJECT_SIZE))
2118 continue;
2119
2120 tree lhs = gimple_call_lhs (gs: call);
2121 if (!lhs)
2122 continue;
2123
2124 init_object_sizes ();
2125
2126 /* If early, only attempt to fold
2127 __builtin_object_size (x, 1) and __builtin_object_size (x, 3),
2128 and rather than folding the builtin to the constant if any,
2129 create a MIN_EXPR or MAX_EXPR of the __builtin_object_size
2130 call result and the computed constant. Do the same for
2131 __builtin_dynamic_object_size too. */
2132 if (early)
2133 {
2134 early_object_sizes_execute_one (i: &i, call);
2135 continue;
2136 }
2137
2138 if (dynamic)
2139 {
2140 if (dynamic_object_sizes_execute_one (i: &i, call))
2141 continue;
2142 else
2143 {
2144 /* If we could not find a suitable size expression, lower to
2145 __builtin_object_size so that we may at least get a
2146 constant lower or higher estimate. */
2147 tree bosfn = builtin_decl_implicit (fncode: BUILT_IN_OBJECT_SIZE);
2148 gimple_call_set_fndecl (gs: call, decl: bosfn);
2149 update_stmt (s: call);
2150
2151 if (dump_file && (dump_flags & TDF_DETAILS))
2152 {
2153 print_generic_expr (dump_file, gimple_call_arg (gs: call, index: 0),
2154 dump_flags);
2155 fprintf (stream: dump_file,
2156 format: ": Retrying as __builtin_object_size\n");
2157 }
2158 }
2159 }
2160
2161 result = gimple_fold_stmt_to_constant (call, do_valueize);
2162 if (!result)
2163 {
2164 tree ost = gimple_call_arg (gs: call, index: 1);
2165
2166 if (tree_fits_uhwi_p (ost))
2167 {
2168 unsigned HOST_WIDE_INT object_size_type = tree_to_uhwi (ost);
2169
2170 if (object_size_type & OST_MINIMUM)
2171 result = build_zero_cst (size_type_node);
2172 else if (object_size_type < OST_END)
2173 result = fold_convert (size_type_node,
2174 integer_minus_one_node);
2175 }
2176
2177 if (!result)
2178 continue;
2179 }
2180
2181 gcc_assert (TREE_CODE (result) == INTEGER_CST);
2182
2183 if (dump_file && (dump_flags & TDF_DETAILS))
2184 {
2185 fprintf (stream: dump_file, format: "Simplified\n ");
2186 print_gimple_stmt (dump_file, call, 0, dump_flags);
2187 fprintf (stream: dump_file, format: " to ");
2188 print_generic_expr (dump_file, result);
2189 fprintf (stream: dump_file, format: "\n");
2190 }
2191
2192 /* Propagate into all uses and fold those stmts. */
2193 if (!SSA_NAME_OCCURS_IN_ABNORMAL_PHI (lhs))
2194 replace_uses_by (lhs, result);
2195 else
2196 replace_call_with_value (&i, result);
2197 }
2198 }
2199
2200 fini_object_sizes ();
2201 return todo;
2202}
2203
2204/* Simple pass to optimize all __builtin_object_size () builtins. */
2205
2206namespace {
2207
2208const pass_data pass_data_object_sizes =
2209{
2210 .type: GIMPLE_PASS, /* type */
2211 .name: "objsz", /* name */
2212 .optinfo_flags: OPTGROUP_NONE, /* optinfo_flags */
2213 .tv_id: TV_NONE, /* tv_id */
2214 .properties_required: ( PROP_cfg | PROP_ssa ), /* properties_required */
2215 PROP_objsz, /* properties_provided */
2216 .properties_destroyed: 0, /* properties_destroyed */
2217 .todo_flags_start: 0, /* todo_flags_start */
2218 .todo_flags_finish: 0, /* todo_flags_finish */
2219};
2220
2221class pass_object_sizes : public gimple_opt_pass
2222{
2223public:
2224 pass_object_sizes (gcc::context *ctxt)
2225 : gimple_opt_pass (pass_data_object_sizes, ctxt)
2226 {}
2227
2228 /* opt_pass methods: */
2229 opt_pass * clone () final override { return new pass_object_sizes (m_ctxt); }
2230 unsigned int execute (function *fun) final override
2231 {
2232 return object_sizes_execute (fun, early: false);
2233 }
2234}; // class pass_object_sizes
2235
2236} // anon namespace
2237
2238gimple_opt_pass *
2239make_pass_object_sizes (gcc::context *ctxt)
2240{
2241 return new pass_object_sizes (ctxt);
2242}
2243
2244/* Early version of pass to optimize all __builtin_object_size () builtins. */
2245
2246namespace {
2247
2248const pass_data pass_data_early_object_sizes =
2249{
2250 .type: GIMPLE_PASS, /* type */
2251 .name: "early_objsz", /* name */
2252 .optinfo_flags: OPTGROUP_NONE, /* optinfo_flags */
2253 .tv_id: TV_NONE, /* tv_id */
2254 .properties_required: ( PROP_cfg | PROP_ssa ), /* properties_required */
2255 .properties_provided: 0, /* properties_provided */
2256 .properties_destroyed: 0, /* properties_destroyed */
2257 .todo_flags_start: 0, /* todo_flags_start */
2258 .todo_flags_finish: 0, /* todo_flags_finish */
2259};
2260
2261class pass_early_object_sizes : public gimple_opt_pass
2262{
2263public:
2264 pass_early_object_sizes (gcc::context *ctxt)
2265 : gimple_opt_pass (pass_data_early_object_sizes, ctxt)
2266 {}
2267
2268 /* opt_pass methods: */
2269 unsigned int execute (function *fun) final override
2270 {
2271 return object_sizes_execute (fun, early: true);
2272 }
2273}; // class pass_object_sizes
2274
2275} // anon namespace
2276
2277gimple_opt_pass *
2278make_pass_early_object_sizes (gcc::context *ctxt)
2279{
2280 return new pass_early_object_sizes (ctxt);
2281}
2282

source code of gcc/tree-object-size.cc