ada: Rename Is_Constr_Subt_For_UN_Aliased flag
[official-gcc.git] / gcc / tree-object-size.cc
blobd0dbd8bbb1d2a7572c6879c897815c617f17d69f
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>
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
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/>. */
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"
41 struct object_size_info
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;
51 struct GTY(()) object_size
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;
59 static tree compute_object_offset (tree, const_tree);
60 static bool addr_object_size (struct object_size_info *,
61 const_tree, int, tree *, tree *t = NULL);
62 static tree alloc_object_size (const gcall *, int);
63 static tree pass_through_call (const gcall *);
64 static void collect_object_sizes_for (struct object_size_info *, tree);
65 static void expr_object_size (struct object_size_info *, tree, tree);
66 static bool merge_object_sizes (struct object_size_info *, tree, tree);
67 static bool plus_stmt_object_size (struct object_size_info *, tree, gimple *);
68 static bool cond_expr_object_size (struct object_size_info *, tree, gimple *);
69 static void init_offset_limit (void);
70 static void check_for_plus_in_loops (struct object_size_info *, tree);
71 static void check_for_plus_in_loops_1 (struct object_size_info *, tree,
72 unsigned int);
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.
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. */
84 static vec<object_size> object_sizes[OST_END];
86 /* Bitmaps what object sizes have been computed already. */
87 static bitmap computed[OST_END];
89 /* Maximum value of offset we consider to be addition. */
90 static unsigned HOST_WIDE_INT offset_limit;
92 /* Tell the generic SSA updater what kind of update is needed after the pass
93 executes. */
94 static unsigned todo;
96 /* Return true if VAL represents an initial size for OBJECT_SIZE_TYPE. */
98 static inline bool
99 size_initval_p (tree val, int object_size_type)
101 return ((object_size_type & OST_MINIMUM)
102 ? integer_all_onesp (val) : integer_zerop (val));
105 /* Return true if VAL represents an unknown size for OBJECT_SIZE_TYPE. */
107 static inline bool
108 size_unknown_p (tree val, int object_size_type)
110 return ((object_size_type & OST_MINIMUM)
111 ? integer_zerop (val) : integer_all_onesp (val));
114 /* Return true if VAL represents a valid size for OBJECT_SIZE_TYPE. */
116 static inline bool
117 size_valid_p (tree val, int object_size_type)
119 return ((object_size_type & OST_DYNAMIC) || TREE_CODE (val) == INTEGER_CST);
122 /* Return true if VAL is usable as an object size in the object_sizes
123 vectors. */
125 static inline bool
126 size_usable_p (tree val)
128 return TREE_CODE (val) == SSA_NAME || TREE_CODE (val) == INTEGER_CST;
131 /* Return a tree with initial value for OBJECT_SIZE_TYPE. */
133 static inline tree
134 size_initval (int object_size_type)
136 return ((object_size_type & OST_MINIMUM)
137 ? TYPE_MAX_VALUE (sizetype) : size_zero_node);
140 /* Return a tree with unknown value for OBJECT_SIZE_TYPE. */
142 static inline tree
143 size_unknown (int object_size_type)
145 return ((object_size_type & OST_MINIMUM)
146 ? size_zero_node : TYPE_MAX_VALUE (sizetype));
149 /* Grow object_sizes[OBJECT_SIZE_TYPE] to num_ssa_names. */
151 static inline void
152 object_sizes_grow (int object_size_type)
154 if (num_ssa_names > object_sizes[object_size_type].length ())
155 object_sizes[object_size_type].safe_grow (num_ssa_names, true);
158 /* Release object_sizes[OBJECT_SIZE_TYPE]. */
160 static inline void
161 object_sizes_release (int object_size_type)
163 object_sizes[object_size_type].release ();
166 /* Return true if object_sizes[OBJECT_SIZE_TYPE][VARNO] is unknown. */
168 static inline bool
169 object_sizes_unknown_p (int object_size_type, unsigned varno)
171 return size_unknown_p (object_sizes[object_size_type][varno].size,
172 object_size_type);
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. */
178 static inline object_size
179 object_sizes_get_raw (struct object_size_info *osi, unsigned varno)
181 gcc_assert (osi->pass != 0);
182 return object_sizes[osi->object_size_type][varno];
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. */
189 static inline tree
190 object_sizes_get (struct object_size_info *osi, unsigned varno,
191 bool whole = false)
193 tree ret;
194 int object_size_type = osi->object_size_type;
196 if (whole)
197 ret = object_sizes[object_size_type][varno].wholesize;
198 else
199 ret = object_sizes[object_size_type][varno].size;
201 if (object_size_type & OST_DYNAMIC)
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));
211 return ret;
214 /* Set size for VARNO corresponding to OSI to VAL. */
216 static inline void
217 object_sizes_initialize (struct object_size_info *osi, unsigned varno,
218 tree val, tree wholeval)
220 int object_size_type = osi->object_size_type;
222 object_sizes[object_size_type][varno].size = val;
223 object_sizes[object_size_type][varno].wholesize = wholeval;
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. */
229 static tree
230 bundle_sizes (tree name, tree expr)
232 gcc_checking_assert (TREE_TYPE (name) == sizetype);
234 if (TREE_CODE (expr) == TREE_VEC)
236 TREE_VEC_ELT (expr, TREE_VEC_LENGTH (expr) - 1) = name;
237 return expr;
240 gcc_checking_assert (types_compatible_p (TREE_TYPE (expr), sizetype));
241 return build2 (MODIFY_EXPR, sizetype, name, expr);
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. */
252 static bool
253 object_sizes_set (struct object_size_info *osi, unsigned varno, tree val,
254 tree wholeval)
256 int object_size_type = osi->object_size_type;
257 object_size osize = object_sizes[object_size_type][varno];
258 bool changed = true;
260 tree oldval = osize.size;
261 tree old_wholeval = osize.wholesize;
263 if (object_size_type & OST_DYNAMIC)
265 if (bitmap_bit_p (osi->reexamine, varno))
267 if (size_unknown_p (val, object_size_type))
269 oldval = object_sizes_get (osi, varno);
270 old_wholeval = object_sizes_get (osi, varno, 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);
275 else
277 val = bundle_sizes (oldval, val);
278 wholeval = bundle_sizes (old_wholeval, wholeval);
281 else
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 (wholeval))
290 bitmap_set_bit (osi->reexamine, varno);
291 wholeval = bundle_sizes (make_ssa_name (sizetype), wholeval);
293 if (!size_usable_p (val))
295 bitmap_set_bit (osi->reexamine, varno);
296 tree newval = bundle_sizes (make_ssa_name (sizetype), val);
297 if (val == wholeval)
298 wholeval = newval;
299 val = newval;
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);
307 else
309 enum tree_code code = (object_size_type & OST_MINIMUM
310 ? MIN_EXPR : MAX_EXPR);
312 val = size_binop (code, val, oldval);
313 wholeval = size_binop (code, wholeval, old_wholeval);
314 changed = (tree_int_cst_compare (val, oldval) != 0
315 || tree_int_cst_compare (old_wholeval, wholeval) != 0);
318 object_sizes[object_size_type][varno].size = val;
319 object_sizes[object_size_type][varno].wholesize = wholeval;
321 return changed;
324 /* Set temporary SSA names for object size and whole size to resolve dependency
325 loops in dynamic size computation. */
327 static inline void
328 object_sizes_set_temp (struct object_size_info *osi, unsigned varno)
330 tree val = object_sizes_get (osi, varno);
332 if (size_initval_p (val, osi->object_size_type))
333 object_sizes_set (osi, varno,
334 make_ssa_name (sizetype),
335 make_ssa_name (sizetype));
338 /* Initialize OFFSET_LIMIT variable. */
339 static void
340 init_offset_limit (void)
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;
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. */
353 static tree
354 size_for_offset (tree sz, tree offset, tree wholesize = NULL_TREE)
356 gcc_checking_assert (types_compatible_p (TREE_TYPE (sz), sizetype));
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 (sz, wholesize)))
365 gcc_checking_assert (types_compatible_p (TREE_TYPE (wholesize),
366 sizetype));
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;
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);
381 if (TREE_CODE (offset) == INTEGER_CST)
383 if (integer_zerop (offset))
384 return sz;
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;
392 return size_binop (MINUS_EXPR, size_binop (MAX_EXPR, sz, offset), offset);
395 /* Compute offset of EXPR within VAR. Return error_mark_node
396 if unknown. */
398 static tree
399 compute_object_offset (tree expr, const_tree var)
401 enum tree_code code = PLUS_EXPR;
402 tree base, off, t;
404 if (expr == var)
405 return size_zero_node;
407 switch (TREE_CODE (expr))
409 case COMPONENT_REF:
410 base = compute_object_offset (TREE_OPERAND (expr, 0), var);
411 if (base == error_mark_node)
412 return base;
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;
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);
427 case IMAGPART_EXPR:
428 base = compute_object_offset (TREE_OPERAND (expr, 0), var);
429 if (base == error_mark_node)
430 return base;
432 off = TYPE_SIZE_UNIT (TREE_TYPE (expr));
433 break;
435 case ARRAY_REF:
436 base = compute_object_offset (TREE_OPERAND (expr, 0), var);
437 if (base == error_mark_node)
438 return base;
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)
448 code = MINUS_EXPR;
449 t = fold_build1 (NEGATE_EXPR, TREE_TYPE (t), t);
451 t = fold_convert (sizetype, t);
452 off = size_binop (MULT_EXPR, unit_size, t);
453 break;
455 case MEM_REF:
456 gcc_assert (TREE_CODE (TREE_OPERAND (expr, 0)) == ADDR_EXPR);
457 return wide_int_to_tree (sizetype, mem_ref_offset (expr));
459 default:
460 return error_mark_node;
463 return size_binop (code, base, off);
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. */
472 tree
473 decl_init_size (tree decl, bool min)
475 tree size = DECL_SIZE_UNIT (decl);
476 tree type = TREE_TYPE (decl);
477 if (TREE_CODE (type) != RECORD_TYPE)
478 return size;
480 tree last = last_field (type);
481 if (!last)
482 return size;
484 tree last_type = TREE_TYPE (last);
485 if (TREE_CODE (last_type) != ARRAY_TYPE
486 || TYPE_SIZE (last_type))
487 return size;
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;
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;
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). */
507 static bool
508 addr_object_size (struct object_size_info *osi, const_tree ptr,
509 int object_size_type, tree *psize, tree *pwholesize)
511 tree pt_var, pt_var_size = NULL_TREE, pt_var_wholesize = NULL_TREE;
512 tree var_size, bytes, wholebytes;
514 gcc_assert (TREE_CODE (ptr) == ADDR_EXPR);
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);
522 pt_var = TREE_OPERAND (ptr, 0);
523 while (handled_component_p (pt_var))
524 pt_var = TREE_OPERAND (pt_var, 0);
526 if (!pt_var)
527 return false;
529 if (TREE_CODE (pt_var) == MEM_REF)
531 tree sz, wholesize;
533 if (!osi || (object_size_type & OST_SUBOBJECT) != 0
534 || TREE_CODE (TREE_OPERAND (pt_var, 0)) != SSA_NAME)
536 compute_builtin_object_size (TREE_OPERAND (pt_var, 0),
537 object_size_type & ~OST_SUBOBJECT, &sz);
538 wholesize = sz;
540 else
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)))
548 sz = object_sizes_get (osi, SSA_NAME_VERSION (var));
549 wholesize = object_sizes_get (osi, SSA_NAME_VERSION (var), true);
551 else
552 sz = wholesize = size_unknown (object_size_type);
554 if (!size_unknown_p (sz, object_size_type))
555 sz = size_for_offset (sz, TREE_OPERAND (pt_var, 1), wholesize);
557 if (!size_unknown_p (sz, object_size_type)
558 && (TREE_CODE (sz) != INTEGER_CST
559 || compare_tree_int (sz, offset_limit) < 0))
561 pt_var_size = sz;
562 pt_var_wholesize = wholesize;
565 else if (DECL_P (pt_var))
567 pt_var_size = pt_var_wholesize
568 = decl_init_size (pt_var, object_size_type & OST_MINIMUM);
569 if (!pt_var_size)
570 return false;
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;
577 if (pt_var_size)
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;
585 if (pt_var != TREE_OPERAND (ptr, 0))
587 tree var;
589 if (object_size_type & OST_SUBOBJECT)
591 var = TREE_OPERAND (ptr, 0);
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 (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)
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))
618 case ARRAY_REF:
619 if (TYPE_SIZE_UNIT (TREE_TYPE (TREE_OPERAND (v, 0))))
621 tree domain
622 = TYPE_DOMAIN (TREE_TYPE (TREE_OPERAND (v, 0)));
623 if (domain && TYPE_MAX_VALUE (domain))
625 v = NULL_TREE;
626 break;
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)))
641 v = NULL_TREE;
642 break;
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)))
649 if (!TYPE_INCLUDES_FLEXARRAY (TREE_TYPE (v)))
651 v = NULL_TREE;
652 break;
654 else
656 v = TREE_OPERAND (v, 0);
657 break;
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)
675 /* compute object size only if v is not a
676 flexible array member. */
677 if (!is_flexible_array_mem_ref)
679 v = NULL_TREE;
680 break;
682 v = TREE_OPERAND (v, 0);
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;
701 if (v == pt_var)
702 var = pt_var;
705 else
706 var = pt_var;
708 if (var != pt_var)
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;
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)
723 bytes = size_for_offset (var_size, bytes);
724 if (var != pt_var && pt_var_size && TREE_CODE (pt_var) == MEM_REF)
726 tree bytes2 = compute_object_offset (TREE_OPERAND (ptr, 0),
727 pt_var);
728 if (bytes2 != error_mark_node)
730 bytes2 = size_for_offset (pt_var_size, bytes2);
731 bytes = size_binop (MIN_EXPR, bytes, bytes2);
735 else
736 bytes = size_unknown (object_size_type);
738 wholebytes
739 = object_size_type & OST_SUBOBJECT ? var_size : pt_var_wholesize;
741 else if (!pt_var_size)
742 return false;
743 else
745 bytes = pt_var_size;
746 wholebytes = pt_var_wholesize;
749 if (!size_unknown_p (bytes, object_size_type)
750 && size_valid_p (bytes, object_size_type)
751 && !size_unknown_p (bytes, object_size_type)
752 && size_valid_p (wholebytes, object_size_type))
754 *psize = bytes;
755 if (pwholesize)
756 *pwholesize = wholebytes;
757 return true;
760 return false;
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). */
769 static tree
770 alloc_object_size (const gcall *call, int object_size_type)
772 gcc_assert (is_gimple_call (call));
774 tree calltype;
775 tree callfn = gimple_call_fndecl (call);
776 if (callfn)
777 calltype = TREE_TYPE (callfn);
778 else
779 calltype = gimple_call_fntype (call);
781 if (!calltype)
782 return size_unknown (object_size_type);
784 /* Set to positions of alloc_size arguments. */
785 int arg1 = -1, arg2 = -1;
786 tree alloc_size = lookup_attribute ("alloc_size",
787 TYPE_ATTRIBUTES (calltype));
788 if (alloc_size && TREE_VALUE (alloc_size))
790 tree p = TREE_VALUE (alloc_size);
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;
796 else if (gimple_call_builtin_p (call, BUILT_IN_NORMAL)
797 && callfn
798 && ALLOCA_FUNCTION_CODE_P (DECL_FUNCTION_CODE (callfn)))
799 arg1 = 0;
801 /* Non-const arguments are OK here, let the caller handle constness. */
802 if (arg1 < 0
803 || (unsigned) arg1 >= gimple_call_num_args (call)
804 || (arg2 >= 0 && (unsigned) arg2 >= gimple_call_num_args (call)))
805 return size_unknown (object_size_type);
807 tree targ1 = gimple_call_arg (call, arg1);
808 if (!INTEGRAL_TYPE_P (TREE_TYPE (targ1))
809 || TYPE_PRECISION (TREE_TYPE (targ1)) > TYPE_PRECISION (sizetype))
810 return size_unknown (object_size_type);
811 targ1 = fold_convert (sizetype, targ1);
812 tree bytes = NULL_TREE;
813 if (arg2 >= 0)
815 tree targ2 = gimple_call_arg (call, arg2);
816 if (!INTEGRAL_TYPE_P (TREE_TYPE (targ2))
817 || TYPE_PRECISION (TREE_TYPE (targ2)) > TYPE_PRECISION (sizetype))
818 return size_unknown (object_size_type);
819 targ2 = fold_convert (sizetype, targ2);
820 bytes = size_binop (MULT_EXPR, targ1, targ2);
822 else
823 bytes = targ1;
825 return bytes ? bytes : size_unknown (object_size_type);
828 /* Compute __builtin_object_size for CALL, which is a call to either
829 BUILT_IN_STRDUP or BUILT_IN_STRNDUP; IS_STRNDUP indicates which it is.
830 OBJECT_SIZE_TYPE is the second argument from __builtin_object_size.
831 If unknown, return size_unknown (object_size_type). */
833 static tree
834 strdup_object_size (const gcall *call, int object_size_type, bool is_strndup)
836 tree src = gimple_call_arg (call, 0);
837 tree sz = size_unknown (object_size_type);
838 tree n = NULL_TREE;
840 if (is_strndup)
841 n = fold_build2 (PLUS_EXPR, sizetype, size_one_node,
842 gimple_call_arg (call, 1));
843 /* For strdup, simply emit strlen (SRC) + 1 and let the optimizer fold it the
844 way it likes. */
845 else
847 tree strlen_fn = builtin_decl_implicit (BUILT_IN_STRLEN);
848 if (strlen_fn)
850 sz = fold_build2 (PLUS_EXPR, sizetype, size_one_node,
851 build_call_expr (strlen_fn, 1, src));
852 todo = TODO_update_ssa_only_virtuals;
856 /* In all other cases, return the size of SRC since the object size cannot
857 exceed that. We cannot do this for OST_MINIMUM unless SRC points into a
858 string constant since otherwise the object size could go all the way down
859 to zero. */
860 if (!size_valid_p (sz, object_size_type)
861 || size_unknown_p (sz, object_size_type))
863 tree wholesrc = NULL_TREE;
864 if (TREE_CODE (src) == ADDR_EXPR)
865 wholesrc = get_base_address (TREE_OPERAND (src, 0));
867 /* If the source points within a string constant, we try to get its
868 length. */
869 if (wholesrc && TREE_CODE (wholesrc) == STRING_CST)
871 tree len = c_strlen (src, 0);
872 if (len)
873 sz = fold_build2 (PLUS_EXPR, sizetype, size_one_node, len);
876 /* For maximum estimate, our next best guess is the object size of the
877 source. */
878 if (size_unknown_p (sz, object_size_type)
879 && !(object_size_type & OST_MINIMUM))
880 compute_builtin_object_size (src, object_size_type, &sz);
883 /* String duplication allocates at least one byte, so we should never fail
884 for OST_MINIMUM. */
885 if ((!size_valid_p (sz, object_size_type)
886 || size_unknown_p (sz, object_size_type))
887 && (object_size_type & OST_MINIMUM))
888 sz = size_one_node;
890 /* Factor in the N. */
891 return n ? fold_build2 (MIN_EXPR, sizetype, n, sz) : sz;
894 /* If object size is propagated from one of function's arguments directly
895 to its return value, return that argument for GIMPLE_CALL statement CALL.
896 Otherwise return NULL. */
898 static tree
899 pass_through_call (const gcall *call)
901 unsigned rf = gimple_call_return_flags (call);
902 if (rf & ERF_RETURNS_ARG)
904 unsigned argnum = rf & ERF_RETURN_ARG_MASK;
905 if (argnum < gimple_call_num_args (call))
906 return gimple_call_arg (call, argnum);
909 /* __builtin_assume_aligned is intentionally not marked RET1. */
910 if (gimple_call_builtin_p (call, BUILT_IN_ASSUME_ALIGNED))
911 return gimple_call_arg (call, 0);
913 return NULL_TREE;
916 /* Emit PHI nodes for size expressions fo. */
918 static void
919 emit_phi_nodes (gimple *stmt, tree size, tree wholesize)
921 tree phires;
922 gphi *wholephi = NULL;
924 if (wholesize != size)
926 phires = TREE_VEC_ELT (wholesize, TREE_VEC_LENGTH (wholesize) - 1);
927 wholephi = create_phi_node (phires, gimple_bb (stmt));
930 phires = TREE_VEC_ELT (size, TREE_VEC_LENGTH (size) - 1);
931 gphi *phi = create_phi_node (phires, gimple_bb (stmt));
932 gphi *obj_phi = as_a <gphi *> (stmt);
934 gcc_checking_assert (TREE_CODE (wholesize) == TREE_VEC);
935 gcc_checking_assert (TREE_CODE (size) == TREE_VEC);
937 for (unsigned i = 0; i < gimple_phi_num_args (stmt); i++)
939 gimple_seq seq = NULL;
940 tree wsz = TREE_VEC_ELT (wholesize, i);
941 tree sz = TREE_VEC_ELT (size, i);
943 /* If we built an expression, we will need to build statements
944 and insert them on the edge right away. */
945 if (TREE_CODE (wsz) != SSA_NAME)
946 wsz = force_gimple_operand (wsz, &seq, true, NULL);
947 if (TREE_CODE (sz) != SSA_NAME)
949 gimple_seq s;
950 sz = force_gimple_operand (sz, &s, true, NULL);
951 gimple_seq_add_seq (&seq, s);
954 if (seq)
955 gsi_insert_seq_on_edge (gimple_phi_arg_edge (obj_phi, i), seq);
957 if (wholephi)
958 add_phi_arg (wholephi, wsz,
959 gimple_phi_arg_edge (obj_phi, i),
960 gimple_phi_arg_location (obj_phi, i));
962 add_phi_arg (phi, sz,
963 gimple_phi_arg_edge (obj_phi, i),
964 gimple_phi_arg_location (obj_phi, i));
968 /* Descend through EXPR and return size_unknown if it uses any SSA variable
969 object_size_set or object_size_set_temp generated, which turned out to be
970 size_unknown, as noted in UNKNOWNS. */
972 static tree
973 propagate_unknowns (object_size_info *osi, tree expr)
975 int object_size_type = osi->object_size_type;
977 switch (TREE_CODE (expr))
979 case SSA_NAME:
980 if (bitmap_bit_p (osi->unknowns, SSA_NAME_VERSION (expr)))
981 return size_unknown (object_size_type);
982 return expr;
984 case MIN_EXPR:
985 case MAX_EXPR:
987 tree res = propagate_unknowns (osi, TREE_OPERAND (expr, 0));
988 if (size_unknown_p (res, object_size_type))
989 return res;
991 res = propagate_unknowns (osi, TREE_OPERAND (expr, 1));
992 if (size_unknown_p (res, object_size_type))
993 return res;
995 return expr;
997 case MODIFY_EXPR:
999 tree res = propagate_unknowns (osi, TREE_OPERAND (expr, 1));
1000 if (size_unknown_p (res, object_size_type))
1001 return res;
1002 return expr;
1004 case TREE_VEC:
1005 for (int i = 0; i < TREE_VEC_LENGTH (expr); i++)
1007 tree res = propagate_unknowns (osi, TREE_VEC_ELT (expr, i));
1008 if (size_unknown_p (res, object_size_type))
1009 return res;
1011 return expr;
1012 case PLUS_EXPR:
1013 case MINUS_EXPR:
1015 tree res = propagate_unknowns (osi, TREE_OPERAND (expr, 0));
1016 if (size_unknown_p (res, object_size_type))
1017 return res;
1019 return expr;
1021 default:
1022 return expr;
1026 /* Walk through size expressions that need reexamination and generate
1027 statements for them. */
1029 static void
1030 gimplify_size_expressions (object_size_info *osi)
1032 int object_size_type = osi->object_size_type;
1033 bitmap_iterator bi;
1034 unsigned int i;
1035 bool changed;
1037 /* Step 1: Propagate unknowns into expressions. */
1038 bitmap reexamine = BITMAP_ALLOC (NULL);
1039 bitmap_copy (reexamine, osi->reexamine);
1042 changed = false;
1043 EXECUTE_IF_SET_IN_BITMAP (reexamine, 0, i, bi)
1045 object_size cur = object_sizes_get_raw (osi, i);
1047 if (size_unknown_p (propagate_unknowns (osi, cur.size),
1048 object_size_type)
1049 || size_unknown_p (propagate_unknowns (osi, cur.wholesize),
1050 object_size_type))
1052 object_sizes_set (osi, i,
1053 size_unknown (object_size_type),
1054 size_unknown (object_size_type));
1055 changed = true;
1058 bitmap_copy (reexamine, osi->reexamine);
1060 while (changed);
1062 /* Release all unknowns. */
1063 EXECUTE_IF_SET_IN_BITMAP (osi->unknowns, 0, i, bi)
1064 release_ssa_name (ssa_name (i));
1066 /* Expand all size expressions to put their definitions close to the objects
1067 for which size is being computed. */
1068 EXECUTE_IF_SET_IN_BITMAP (osi->reexamine, 0, i, bi)
1070 gimple_seq seq = NULL;
1071 object_size osize = object_sizes_get_raw (osi, i);
1073 gimple *stmt = SSA_NAME_DEF_STMT (ssa_name (i));
1074 enum gimple_code code = gimple_code (stmt);
1076 /* PHI nodes need special attention. */
1077 if (code == GIMPLE_PHI)
1078 emit_phi_nodes (stmt, osize.size, osize.wholesize);
1079 else
1081 tree size_expr = NULL_TREE;
1083 /* Bundle wholesize in with the size to gimplify if needed. */
1084 if (osize.wholesize != osize.size
1085 && !size_usable_p (osize.wholesize))
1086 size_expr = size_binop (COMPOUND_EXPR,
1087 osize.wholesize,
1088 osize.size);
1089 else if (!size_usable_p (osize.size))
1090 size_expr = osize.size;
1092 if (size_expr)
1094 gimple_stmt_iterator gsi;
1095 if (code == GIMPLE_NOP)
1096 gsi = gsi_start_bb (single_succ (ENTRY_BLOCK_PTR_FOR_FN (cfun)));
1097 else
1098 gsi = gsi_for_stmt (stmt);
1100 force_gimple_operand (size_expr, &seq, true, NULL);
1101 gsi_insert_seq_before (&gsi, seq, GSI_CONTINUE_LINKING);
1105 /* We're done, so replace the MODIFY_EXPRs with the SSA names. */
1106 object_sizes_initialize (osi, i,
1107 object_sizes_get (osi, i),
1108 object_sizes_get (osi, i, true));
1112 /* Compute __builtin_object_size value for PTR and set *PSIZE to
1113 the resulting value. If the declared object is known and PDECL
1114 is nonnull, sets *PDECL to the object's DECL. OBJECT_SIZE_TYPE
1115 is the second argument to __builtin_object_size.
1116 Returns true on success and false when the object size could not
1117 be determined. */
1119 bool
1120 compute_builtin_object_size (tree ptr, int object_size_type,
1121 tree *psize)
1123 gcc_assert (object_size_type >= 0 && object_size_type < OST_END);
1125 /* Set to unknown and overwrite just before returning if the size
1126 could be determined. */
1127 *psize = size_unknown (object_size_type);
1129 if (! offset_limit)
1130 init_offset_limit ();
1132 if (TREE_CODE (ptr) == ADDR_EXPR)
1133 return addr_object_size (NULL, ptr, object_size_type, psize);
1135 if (TREE_CODE (ptr) != SSA_NAME
1136 || !POINTER_TYPE_P (TREE_TYPE (ptr)))
1137 return false;
1139 if (computed[object_size_type] == NULL)
1141 if (optimize || object_size_type & OST_SUBOBJECT)
1142 return false;
1144 /* When not optimizing, rather than failing, make a small effort
1145 to determine the object size without the full benefit of
1146 the (costly) computation below. */
1147 gimple *def = SSA_NAME_DEF_STMT (ptr);
1148 if (gimple_code (def) == GIMPLE_ASSIGN)
1150 tree_code code = gimple_assign_rhs_code (def);
1151 if (code == POINTER_PLUS_EXPR)
1153 tree offset = gimple_assign_rhs2 (def);
1154 ptr = gimple_assign_rhs1 (def);
1156 if (((object_size_type & OST_DYNAMIC)
1157 || (tree_fits_shwi_p (offset)
1158 && compare_tree_int (offset, offset_limit) <= 0))
1159 && compute_builtin_object_size (ptr, object_size_type,
1160 psize))
1162 *psize = size_for_offset (*psize, offset);
1163 return true;
1167 return false;
1170 struct object_size_info osi;
1171 osi.object_size_type = object_size_type;
1172 if (!bitmap_bit_p (computed[object_size_type], SSA_NAME_VERSION (ptr)))
1174 bitmap_iterator bi;
1175 unsigned int i;
1177 object_sizes_grow (object_size_type);
1178 if (dump_file)
1180 fprintf (dump_file, "Computing %s %s%sobject size for ",
1181 (object_size_type & OST_MINIMUM) ? "minimum" : "maximum",
1182 (object_size_type & OST_DYNAMIC) ? "dynamic " : "",
1183 (object_size_type & OST_SUBOBJECT) ? "sub" : "");
1184 print_generic_expr (dump_file, ptr, dump_flags);
1185 fprintf (dump_file, ":\n");
1188 osi.visited = BITMAP_ALLOC (NULL);
1189 osi.reexamine = BITMAP_ALLOC (NULL);
1191 if (object_size_type & OST_DYNAMIC)
1192 osi.unknowns = BITMAP_ALLOC (NULL);
1193 else
1195 osi.depths = NULL;
1196 osi.stack = NULL;
1197 osi.tos = NULL;
1200 /* First pass: walk UD chains, compute object sizes that
1201 can be computed. osi.reexamine bitmap at the end will
1202 contain what variables were found in dependency cycles
1203 and therefore need to be reexamined. */
1204 osi.pass = 0;
1205 osi.changed = false;
1206 collect_object_sizes_for (&osi, ptr);
1208 if (object_size_type & OST_DYNAMIC)
1210 osi.pass = 1;
1211 gimplify_size_expressions (&osi);
1212 BITMAP_FREE (osi.unknowns);
1213 bitmap_clear (osi.reexamine);
1216 /* Second pass: keep recomputing object sizes of variables
1217 that need reexamination, until no object sizes are
1218 increased or all object sizes are computed. */
1219 if (! bitmap_empty_p (osi.reexamine))
1221 bitmap reexamine = BITMAP_ALLOC (NULL);
1223 /* If looking for minimum instead of maximum object size,
1224 detect cases where a pointer is increased in a loop.
1225 Although even without this detection pass 2 would eventually
1226 terminate, it could take a long time. If a pointer is
1227 increasing this way, we need to assume 0 object size.
1228 E.g. p = &buf[0]; while (cond) p = p + 4; */
1229 if (object_size_type & OST_MINIMUM)
1231 osi.depths = XCNEWVEC (unsigned int, num_ssa_names);
1232 osi.stack = XNEWVEC (unsigned int, num_ssa_names);
1233 osi.tos = osi.stack;
1234 osi.pass = 1;
1235 /* collect_object_sizes_for is changing
1236 osi.reexamine bitmap, so iterate over a copy. */
1237 bitmap_copy (reexamine, osi.reexamine);
1238 EXECUTE_IF_SET_IN_BITMAP (reexamine, 0, i, bi)
1239 if (bitmap_bit_p (osi.reexamine, i))
1240 check_for_plus_in_loops (&osi, ssa_name (i));
1242 free (osi.depths);
1243 osi.depths = NULL;
1244 free (osi.stack);
1245 osi.stack = NULL;
1246 osi.tos = NULL;
1251 osi.pass = 2;
1252 osi.changed = false;
1253 /* collect_object_sizes_for is changing
1254 osi.reexamine bitmap, so iterate over a copy. */
1255 bitmap_copy (reexamine, osi.reexamine);
1256 EXECUTE_IF_SET_IN_BITMAP (reexamine, 0, i, bi)
1257 if (bitmap_bit_p (osi.reexamine, i))
1259 collect_object_sizes_for (&osi, ssa_name (i));
1260 if (dump_file && (dump_flags & TDF_DETAILS))
1262 fprintf (dump_file, "Reexamining ");
1263 print_generic_expr (dump_file, ssa_name (i),
1264 dump_flags);
1265 fprintf (dump_file, "\n");
1269 while (osi.changed);
1271 BITMAP_FREE (reexamine);
1273 EXECUTE_IF_SET_IN_BITMAP (osi.reexamine, 0, i, bi)
1274 bitmap_set_bit (computed[object_size_type], i);
1276 /* Debugging dumps. */
1277 if (dump_file)
1279 EXECUTE_IF_SET_IN_BITMAP (osi.visited, 0, i, bi)
1280 if (!object_sizes_unknown_p (object_size_type, i))
1282 print_generic_expr (dump_file, ssa_name (i),
1283 dump_flags);
1284 fprintf (dump_file,
1285 ": %s %s%sobject size ",
1286 ((object_size_type & OST_MINIMUM) ? "minimum"
1287 : "maximum"),
1288 (object_size_type & OST_DYNAMIC) ? "dynamic " : "",
1289 (object_size_type & OST_SUBOBJECT) ? "sub" : "");
1290 print_generic_expr (dump_file, object_sizes_get (&osi, i),
1291 dump_flags);
1292 fprintf (dump_file, "\n");
1296 BITMAP_FREE (osi.reexamine);
1297 BITMAP_FREE (osi.visited);
1300 *psize = object_sizes_get (&osi, SSA_NAME_VERSION (ptr));
1301 return !size_unknown_p (*psize, object_size_type);
1304 /* Compute object_sizes for PTR, defined to VALUE, which is not an SSA_NAME. */
1306 static void
1307 expr_object_size (struct object_size_info *osi, tree ptr, tree value)
1309 int object_size_type = osi->object_size_type;
1310 unsigned int varno = SSA_NAME_VERSION (ptr);
1311 tree bytes, wholesize;
1313 gcc_assert (!object_sizes_unknown_p (object_size_type, varno));
1314 gcc_assert (osi->pass == 0);
1316 if (TREE_CODE (value) == WITH_SIZE_EXPR)
1317 value = TREE_OPERAND (value, 0);
1319 /* Pointer variables should have been handled by merge_object_sizes. */
1320 gcc_assert (TREE_CODE (value) != SSA_NAME
1321 || !POINTER_TYPE_P (TREE_TYPE (value)));
1323 if (TREE_CODE (value) == ADDR_EXPR)
1324 addr_object_size (osi, value, object_size_type, &bytes, &wholesize);
1325 else
1326 bytes = wholesize = size_unknown (object_size_type);
1328 object_sizes_set (osi, varno, bytes, wholesize);
1332 /* Compute object_sizes for PTR, defined to the result of a call. */
1334 static void
1335 call_object_size (struct object_size_info *osi, tree ptr, gcall *call)
1337 int object_size_type = osi->object_size_type;
1338 unsigned int varno = SSA_NAME_VERSION (ptr);
1339 tree bytes = NULL_TREE;
1341 gcc_assert (is_gimple_call (call));
1343 gcc_assert (!object_sizes_unknown_p (object_size_type, varno));
1344 gcc_assert (osi->pass == 0);
1346 bool is_strdup = gimple_call_builtin_p (call, BUILT_IN_STRDUP);
1347 bool is_strndup = gimple_call_builtin_p (call, BUILT_IN_STRNDUP);
1348 if (is_strdup || is_strndup)
1349 bytes = strdup_object_size (call, object_size_type, is_strndup);
1350 else
1351 bytes = alloc_object_size (call, object_size_type);
1353 if (!size_valid_p (bytes, object_size_type))
1354 bytes = size_unknown (object_size_type);
1356 object_sizes_set (osi, varno, bytes, bytes);
1360 /* Compute object_sizes for PTR, defined to an unknown value. */
1362 static void
1363 unknown_object_size (struct object_size_info *osi, tree ptr)
1365 int object_size_type = osi->object_size_type;
1366 unsigned int varno = SSA_NAME_VERSION (ptr);
1368 gcc_checking_assert (!object_sizes_unknown_p (object_size_type, varno));
1369 gcc_checking_assert (osi->pass == 0);
1370 tree bytes = size_unknown (object_size_type);
1372 object_sizes_set (osi, varno, bytes, bytes);
1376 /* Merge object sizes of ORIG + OFFSET into DEST. Return true if
1377 the object size might need reexamination later. */
1379 static bool
1380 merge_object_sizes (struct object_size_info *osi, tree dest, tree orig)
1382 int object_size_type = osi->object_size_type;
1383 unsigned int varno = SSA_NAME_VERSION (dest);
1384 tree orig_bytes, wholesize;
1386 if (object_sizes_unknown_p (object_size_type, varno))
1387 return false;
1389 if (osi->pass == 0)
1390 collect_object_sizes_for (osi, orig);
1392 orig_bytes = object_sizes_get (osi, SSA_NAME_VERSION (orig));
1393 wholesize = object_sizes_get (osi, SSA_NAME_VERSION (orig), true);
1395 if (object_sizes_set (osi, varno, orig_bytes, wholesize))
1396 osi->changed = true;
1398 return bitmap_bit_p (osi->reexamine, SSA_NAME_VERSION (orig));
1402 /* Compute object_sizes for VAR, defined to the result of an assignment
1403 with operator POINTER_PLUS_EXPR. Return true if the object size might
1404 need reexamination later. */
1406 static bool
1407 plus_stmt_object_size (struct object_size_info *osi, tree var, gimple *stmt)
1409 int object_size_type = osi->object_size_type;
1410 unsigned int varno = SSA_NAME_VERSION (var);
1411 tree bytes, wholesize;
1412 tree op0, op1;
1413 bool reexamine = false;
1415 if (gimple_assign_rhs_code (stmt) == POINTER_PLUS_EXPR)
1417 op0 = gimple_assign_rhs1 (stmt);
1418 op1 = gimple_assign_rhs2 (stmt);
1420 else if (gimple_assign_rhs_code (stmt) == ADDR_EXPR)
1422 tree rhs = TREE_OPERAND (gimple_assign_rhs1 (stmt), 0);
1423 gcc_assert (TREE_CODE (rhs) == MEM_REF);
1424 op0 = TREE_OPERAND (rhs, 0);
1425 op1 = TREE_OPERAND (rhs, 1);
1427 else
1428 gcc_unreachable ();
1430 if (object_sizes_unknown_p (object_size_type, varno))
1431 return false;
1433 /* Handle PTR + OFFSET here. */
1434 if (size_valid_p (op1, object_size_type)
1435 && (TREE_CODE (op0) == SSA_NAME || TREE_CODE (op0) == ADDR_EXPR))
1437 if (TREE_CODE (op0) == SSA_NAME)
1439 if (osi->pass == 0)
1440 collect_object_sizes_for (osi, op0);
1442 bytes = object_sizes_get (osi, SSA_NAME_VERSION (op0));
1443 wholesize = object_sizes_get (osi, SSA_NAME_VERSION (op0), true);
1444 reexamine = bitmap_bit_p (osi->reexamine, SSA_NAME_VERSION (op0));
1446 else
1448 /* op0 will be ADDR_EXPR here. We should never come here during
1449 reexamination. */
1450 gcc_checking_assert (osi->pass == 0);
1451 addr_object_size (osi, op0, object_size_type, &bytes, &wholesize);
1454 /* size_for_offset doesn't make sense for -1 size, but it does for size 0
1455 since the wholesize could be non-zero and a negative offset could give
1456 a non-zero size. */
1457 if (size_unknown_p (bytes, 0))
1459 else if ((object_size_type & OST_DYNAMIC)
1460 || compare_tree_int (op1, offset_limit) <= 0)
1461 bytes = size_for_offset (bytes, op1, wholesize);
1462 /* In the static case, with a negative offset, the best estimate for
1463 minimum size is size_unknown but for maximum size, the wholesize is a
1464 better estimate than size_unknown. */
1465 else if (object_size_type & OST_MINIMUM)
1466 bytes = size_unknown (object_size_type);
1467 else
1468 bytes = wholesize;
1470 else
1471 bytes = wholesize = size_unknown (object_size_type);
1473 if (!size_valid_p (bytes, object_size_type)
1474 || !size_valid_p (wholesize, object_size_type))
1475 bytes = wholesize = size_unknown (object_size_type);
1477 if (object_sizes_set (osi, varno, bytes, wholesize))
1478 osi->changed = true;
1479 return reexamine;
1482 /* Compute the dynamic object size for VAR. Return the result in SIZE and
1483 WHOLESIZE. */
1485 static void
1486 dynamic_object_size (struct object_size_info *osi, tree var,
1487 tree *size, tree *wholesize)
1489 int object_size_type = osi->object_size_type;
1491 if (TREE_CODE (var) == SSA_NAME)
1493 unsigned varno = SSA_NAME_VERSION (var);
1495 collect_object_sizes_for (osi, var);
1496 *size = object_sizes_get (osi, varno);
1497 *wholesize = object_sizes_get (osi, varno, true);
1499 else if (TREE_CODE (var) == ADDR_EXPR)
1500 addr_object_size (osi, var, object_size_type, size, wholesize);
1501 else
1502 *size = *wholesize = size_unknown (object_size_type);
1505 /* Compute object_sizes for VAR, defined at STMT, which is
1506 a COND_EXPR. Return true if the object size might need reexamination
1507 later. */
1509 static bool
1510 cond_expr_object_size (struct object_size_info *osi, tree var, gimple *stmt)
1512 tree then_, else_;
1513 int object_size_type = osi->object_size_type;
1514 unsigned int varno = SSA_NAME_VERSION (var);
1515 bool reexamine = false;
1517 gcc_assert (gimple_assign_rhs_code (stmt) == COND_EXPR);
1519 if (object_sizes_unknown_p (object_size_type, varno))
1520 return false;
1522 then_ = gimple_assign_rhs2 (stmt);
1523 else_ = gimple_assign_rhs3 (stmt);
1525 if (object_size_type & OST_DYNAMIC)
1527 tree then_size, then_wholesize, else_size, else_wholesize;
1529 dynamic_object_size (osi, then_, &then_size, &then_wholesize);
1530 if (!size_unknown_p (then_size, object_size_type))
1531 dynamic_object_size (osi, else_, &else_size, &else_wholesize);
1533 tree cond_size, cond_wholesize;
1534 if (size_unknown_p (then_size, object_size_type)
1535 || size_unknown_p (else_size, object_size_type))
1536 cond_size = cond_wholesize = size_unknown (object_size_type);
1537 else
1539 cond_size = fold_build3 (COND_EXPR, sizetype,
1540 gimple_assign_rhs1 (stmt),
1541 then_size, else_size);
1542 cond_wholesize = fold_build3 (COND_EXPR, sizetype,
1543 gimple_assign_rhs1 (stmt),
1544 then_wholesize, else_wholesize);
1547 object_sizes_set (osi, varno, cond_size, cond_wholesize);
1549 return false;
1552 if (TREE_CODE (then_) == SSA_NAME)
1553 reexamine |= merge_object_sizes (osi, var, then_);
1554 else
1555 expr_object_size (osi, var, then_);
1557 if (object_sizes_unknown_p (object_size_type, varno))
1558 return reexamine;
1560 if (TREE_CODE (else_) == SSA_NAME)
1561 reexamine |= merge_object_sizes (osi, var, else_);
1562 else
1563 expr_object_size (osi, var, else_);
1565 return reexamine;
1568 /* Find size of an object passed as a parameter to the function. */
1570 static void
1571 parm_object_size (struct object_size_info *osi, tree var)
1573 int object_size_type = osi->object_size_type;
1574 tree parm = SSA_NAME_VAR (var);
1576 if (!(object_size_type & OST_DYNAMIC) || !POINTER_TYPE_P (TREE_TYPE (parm)))
1578 expr_object_size (osi, var, parm);
1579 return;
1582 /* Look for access attribute. */
1583 rdwr_map rdwr_idx;
1585 tree fndecl = cfun->decl;
1586 const attr_access *access = get_parm_access (rdwr_idx, parm, fndecl);
1587 tree typesize = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (parm)));
1588 tree sz = NULL_TREE;
1590 /* If we have an access attribute with a usable size argument... */
1591 if (access && access->sizarg != UINT_MAX
1592 /* ... and either PARM is void * or has a type that is complete and has a
1593 constant size... */
1594 && ((typesize && poly_int_tree_p (typesize))
1595 || (!typesize && VOID_TYPE_P (TREE_TYPE (TREE_TYPE (parm))))))
1597 tree fnargs = DECL_ARGUMENTS (fndecl);
1598 tree arg = NULL_TREE;
1599 unsigned argpos = 0;
1601 /* ... then walk through the parameters to pick the size parameter and
1602 safely scale it by the type size if needed.
1604 TODO: we could also compute the size of VLAs where the size is
1605 given by a function parameter. */
1606 for (arg = fnargs; arg; arg = TREE_CHAIN (arg), ++argpos)
1607 if (argpos == access->sizarg)
1609 gcc_assert (INTEGRAL_TYPE_P (TREE_TYPE (arg)));
1610 sz = get_or_create_ssa_default_def (cfun, arg);
1611 if (sz != NULL_TREE)
1613 sz = fold_convert (sizetype, sz);
1614 if (typesize)
1615 sz = size_binop (MULT_EXPR, sz, typesize);
1617 break;
1620 if (!sz)
1621 sz = size_unknown (object_size_type);
1623 object_sizes_set (osi, SSA_NAME_VERSION (var), sz, sz);
1626 /* Compute an object size expression for VAR, which is the result of a PHI
1627 node. */
1629 static void
1630 phi_dynamic_object_size (struct object_size_info *osi, tree var)
1632 int object_size_type = osi->object_size_type;
1633 unsigned int varno = SSA_NAME_VERSION (var);
1634 gimple *stmt = SSA_NAME_DEF_STMT (var);
1635 unsigned i, num_args = gimple_phi_num_args (stmt);
1636 bool wholesize_needed = false;
1638 /* The extra space is for the PHI result at the end, which object_sizes_set
1639 sets for us. */
1640 tree sizes = make_tree_vec (num_args + 1);
1641 tree wholesizes = make_tree_vec (num_args + 1);
1643 /* Bail out if the size of any of the PHI arguments cannot be
1644 determined. */
1645 for (i = 0; i < num_args; i++)
1647 edge e = gimple_phi_arg_edge (as_a <gphi *> (stmt), i);
1648 if (e->flags & EDGE_COMPLEX)
1649 break;
1651 tree rhs = gimple_phi_arg_def (stmt, i);
1652 tree size, wholesize;
1654 dynamic_object_size (osi, rhs, &size, &wholesize);
1656 if (size_unknown_p (size, object_size_type))
1657 break;
1659 if (size != wholesize)
1660 wholesize_needed = true;
1662 TREE_VEC_ELT (sizes, i) = size;
1663 TREE_VEC_ELT (wholesizes, i) = wholesize;
1666 if (i < num_args)
1668 ggc_free (sizes);
1669 ggc_free (wholesizes);
1670 sizes = wholesizes = size_unknown (object_size_type);
1673 /* Point to the same TREE_VEC so that we can avoid emitting two PHI
1674 nodes. */
1675 else if (!wholesize_needed)
1677 ggc_free (wholesizes);
1678 wholesizes = sizes;
1681 object_sizes_set (osi, varno, sizes, wholesizes);
1684 /* Compute object sizes for VAR.
1685 For ADDR_EXPR an object size is the number of remaining bytes
1686 to the end of the object (where what is considered an object depends on
1687 OSI->object_size_type).
1688 For allocation GIMPLE_CALL like malloc or calloc object size is the size
1689 of the allocation.
1690 For POINTER_PLUS_EXPR where second operand is a constant integer,
1691 object size is object size of the first operand minus the constant.
1692 If the constant is bigger than the number of remaining bytes until the
1693 end of the object, object size is 0, but if it is instead a pointer
1694 subtraction, object size is size_unknown (object_size_type).
1695 To differentiate addition from subtraction, ADDR_EXPR returns
1696 size_unknown (object_size_type) for all objects bigger than half of the
1697 address space, and constants less than half of the address space are
1698 considered addition, while bigger constants subtraction.
1699 For a memcpy like GIMPLE_CALL that always returns one of its arguments, the
1700 object size is object size of that argument.
1701 Otherwise, object size is the maximum of object sizes of variables
1702 that it might be set to. */
1704 static void
1705 collect_object_sizes_for (struct object_size_info *osi, tree var)
1707 int object_size_type = osi->object_size_type;
1708 unsigned int varno = SSA_NAME_VERSION (var);
1709 gimple *stmt;
1710 bool reexamine;
1712 if (bitmap_bit_p (computed[object_size_type], varno))
1713 return;
1715 if (osi->pass == 0)
1717 if (bitmap_set_bit (osi->visited, varno))
1719 /* Initialize to 0 for maximum size and M1U for minimum size so that
1720 it gets immediately overridden. */
1721 object_sizes_initialize (osi, varno,
1722 size_initval (object_size_type),
1723 size_initval (object_size_type));
1725 else
1727 /* Found a dependency loop. Mark the variable for later
1728 re-examination. */
1729 if (object_size_type & OST_DYNAMIC)
1730 object_sizes_set_temp (osi, varno);
1732 bitmap_set_bit (osi->reexamine, varno);
1733 if (dump_file && (dump_flags & TDF_DETAILS))
1735 fprintf (dump_file, "Found a dependency loop at ");
1736 print_generic_expr (dump_file, var, dump_flags);
1737 fprintf (dump_file, "\n");
1739 return;
1743 if (dump_file && (dump_flags & TDF_DETAILS))
1745 fprintf (dump_file, "Visiting use-def links for ");
1746 print_generic_expr (dump_file, var, dump_flags);
1747 fprintf (dump_file, "\n");
1750 stmt = SSA_NAME_DEF_STMT (var);
1751 reexamine = false;
1753 switch (gimple_code (stmt))
1755 case GIMPLE_ASSIGN:
1757 tree rhs = gimple_assign_rhs1 (stmt);
1758 if (gimple_assign_rhs_code (stmt) == POINTER_PLUS_EXPR
1759 || (gimple_assign_rhs_code (stmt) == ADDR_EXPR
1760 && TREE_CODE (TREE_OPERAND (rhs, 0)) == MEM_REF))
1761 reexamine = plus_stmt_object_size (osi, var, stmt);
1762 else if (gimple_assign_rhs_code (stmt) == COND_EXPR)
1763 reexamine = cond_expr_object_size (osi, var, stmt);
1764 else if (gimple_assign_single_p (stmt)
1765 || gimple_assign_unary_nop_p (stmt))
1767 if (TREE_CODE (rhs) == SSA_NAME
1768 && POINTER_TYPE_P (TREE_TYPE (rhs)))
1769 reexamine = merge_object_sizes (osi, var, rhs);
1770 else
1771 expr_object_size (osi, var, rhs);
1773 else
1774 unknown_object_size (osi, var);
1775 break;
1778 case GIMPLE_CALL:
1780 gcall *call_stmt = as_a <gcall *> (stmt);
1781 tree arg = pass_through_call (call_stmt);
1782 if (arg)
1784 if (TREE_CODE (arg) == SSA_NAME
1785 && POINTER_TYPE_P (TREE_TYPE (arg)))
1786 reexamine = merge_object_sizes (osi, var, arg);
1787 else
1788 expr_object_size (osi, var, arg);
1790 else
1791 call_object_size (osi, var, call_stmt);
1792 break;
1795 case GIMPLE_ASM:
1796 /* Pointers defined by __asm__ statements can point anywhere. */
1797 unknown_object_size (osi, var);
1798 break;
1800 case GIMPLE_NOP:
1801 if (SSA_NAME_VAR (var)
1802 && TREE_CODE (SSA_NAME_VAR (var)) == PARM_DECL)
1803 parm_object_size (osi, var);
1804 else
1805 /* Uninitialized SSA names point nowhere. */
1806 unknown_object_size (osi, var);
1807 break;
1809 case GIMPLE_PHI:
1811 unsigned i;
1813 if (object_size_type & OST_DYNAMIC)
1815 phi_dynamic_object_size (osi, var);
1816 break;
1819 for (i = 0; i < gimple_phi_num_args (stmt); i++)
1821 tree rhs = gimple_phi_arg (stmt, i)->def;
1823 if (object_sizes_unknown_p (object_size_type, varno))
1824 break;
1826 if (TREE_CODE (rhs) == SSA_NAME)
1827 reexamine |= merge_object_sizes (osi, var, rhs);
1828 else if (osi->pass == 0)
1829 expr_object_size (osi, var, rhs);
1831 break;
1834 default:
1835 gcc_unreachable ();
1838 if (! reexamine || object_sizes_unknown_p (object_size_type, varno))
1840 bitmap_set_bit (computed[object_size_type], varno);
1841 if (!(object_size_type & OST_DYNAMIC))
1842 bitmap_clear_bit (osi->reexamine, varno);
1844 else
1846 bitmap_set_bit (osi->reexamine, varno);
1847 if (dump_file && (dump_flags & TDF_DETAILS))
1849 fprintf (dump_file, "Need to reexamine ");
1850 print_generic_expr (dump_file, var, dump_flags);
1851 fprintf (dump_file, "\n");
1857 /* Helper function for check_for_plus_in_loops. Called recursively
1858 to detect loops. */
1860 static void
1861 check_for_plus_in_loops_1 (struct object_size_info *osi, tree var,
1862 unsigned int depth)
1864 gimple *stmt = SSA_NAME_DEF_STMT (var);
1865 unsigned int varno = SSA_NAME_VERSION (var);
1867 if (osi->depths[varno])
1869 if (osi->depths[varno] != depth)
1871 unsigned int *sp;
1873 /* Found a loop involving pointer addition. */
1874 for (sp = osi->tos; sp > osi->stack; )
1876 --sp;
1877 bitmap_clear_bit (osi->reexamine, *sp);
1878 bitmap_set_bit (computed[osi->object_size_type], *sp);
1879 object_sizes_set (osi, *sp, size_zero_node,
1880 object_sizes_get (osi, *sp, true));
1881 if (*sp == varno)
1882 break;
1885 return;
1887 else if (! bitmap_bit_p (osi->reexamine, varno))
1888 return;
1890 osi->depths[varno] = depth;
1891 *osi->tos++ = varno;
1893 switch (gimple_code (stmt))
1896 case GIMPLE_ASSIGN:
1898 if ((gimple_assign_single_p (stmt)
1899 || gimple_assign_unary_nop_p (stmt))
1900 && TREE_CODE (gimple_assign_rhs1 (stmt)) == SSA_NAME)
1902 tree rhs = gimple_assign_rhs1 (stmt);
1904 check_for_plus_in_loops_1 (osi, rhs, depth);
1906 else if (gimple_assign_rhs_code (stmt) == POINTER_PLUS_EXPR)
1908 tree basevar = gimple_assign_rhs1 (stmt);
1909 tree cst = gimple_assign_rhs2 (stmt);
1911 gcc_assert (TREE_CODE (cst) == INTEGER_CST);
1913 check_for_plus_in_loops_1 (osi, basevar,
1914 depth + !integer_zerop (cst));
1916 else
1917 gcc_unreachable ();
1918 break;
1921 case GIMPLE_CALL:
1923 gcall *call_stmt = as_a <gcall *> (stmt);
1924 tree arg = pass_through_call (call_stmt);
1925 if (arg)
1927 if (TREE_CODE (arg) == SSA_NAME)
1928 check_for_plus_in_loops_1 (osi, arg, depth);
1929 else
1930 gcc_unreachable ();
1932 break;
1935 case GIMPLE_PHI:
1937 unsigned i;
1939 for (i = 0; i < gimple_phi_num_args (stmt); i++)
1941 tree rhs = gimple_phi_arg (stmt, i)->def;
1943 if (TREE_CODE (rhs) == SSA_NAME)
1944 check_for_plus_in_loops_1 (osi, rhs, depth);
1946 break;
1949 default:
1950 gcc_unreachable ();
1953 osi->depths[varno] = 0;
1954 osi->tos--;
1958 /* Check if some pointer we are computing object size of is being increased
1959 within a loop. If yes, assume all the SSA variables participating in
1960 that loop have minimum object sizes 0. */
1962 static void
1963 check_for_plus_in_loops (struct object_size_info *osi, tree var)
1965 gimple *stmt = SSA_NAME_DEF_STMT (var);
1967 /* NOTE: In the pre-tuples code, we handled a CALL_EXPR here,
1968 and looked for a POINTER_PLUS_EXPR in the pass-through
1969 argument, if any. In GIMPLE, however, such an expression
1970 is not a valid call operand. */
1972 if (is_gimple_assign (stmt)
1973 && gimple_assign_rhs_code (stmt) == POINTER_PLUS_EXPR)
1975 tree basevar = gimple_assign_rhs1 (stmt);
1976 tree cst = gimple_assign_rhs2 (stmt);
1978 gcc_assert (TREE_CODE (cst) == INTEGER_CST);
1980 /* Skip non-positive offsets. */
1981 if (integer_zerop (cst) || compare_tree_int (cst, offset_limit) > 0)
1982 return;
1984 osi->depths[SSA_NAME_VERSION (basevar)] = 1;
1985 *osi->tos++ = SSA_NAME_VERSION (basevar);
1986 check_for_plus_in_loops_1 (osi, var, 2);
1987 osi->depths[SSA_NAME_VERSION (basevar)] = 0;
1988 osi->tos--;
1993 /* Initialize data structures for the object size computation. */
1995 void
1996 init_object_sizes (void)
1998 int object_size_type;
2000 if (computed[0])
2001 return;
2003 for (object_size_type = 0; object_size_type < OST_END; object_size_type++)
2005 object_sizes_grow (object_size_type);
2006 computed[object_size_type] = BITMAP_ALLOC (NULL);
2009 init_offset_limit ();
2013 /* Destroy data structures after the object size computation. */
2015 void
2016 fini_object_sizes (void)
2018 int object_size_type;
2020 for (object_size_type = 0; object_size_type < OST_END; object_size_type++)
2022 object_sizes_release (object_size_type);
2023 BITMAP_FREE (computed[object_size_type]);
2027 /* Dummy valueize function. */
2029 static tree
2030 do_valueize (tree t)
2032 return t;
2035 /* Process a __builtin_object_size or __builtin_dynamic_object_size call in
2036 CALL early for subobjects before any object information is lost due to
2037 optimization. Insert a MIN or MAX expression of the result and
2038 __builtin_object_size at I so that it may be processed in the second pass.
2039 __builtin_dynamic_object_size is treated like __builtin_object_size here
2040 since we're only looking for constant bounds. */
2042 static void
2043 early_object_sizes_execute_one (gimple_stmt_iterator *i, gimple *call)
2045 tree ost = gimple_call_arg (call, 1);
2046 tree lhs = gimple_call_lhs (call);
2047 gcc_assert (lhs != NULL_TREE);
2049 if (!tree_fits_uhwi_p (ost))
2050 return;
2052 unsigned HOST_WIDE_INT object_size_type = tree_to_uhwi (ost);
2053 tree ptr = gimple_call_arg (call, 0);
2055 if (object_size_type != 1 && object_size_type != 3)
2056 return;
2058 if (TREE_CODE (ptr) != ADDR_EXPR && TREE_CODE (ptr) != SSA_NAME)
2059 return;
2061 tree type = TREE_TYPE (lhs);
2062 tree bytes;
2063 if (!compute_builtin_object_size (ptr, object_size_type, &bytes)
2064 || !int_fits_type_p (bytes, type))
2065 return;
2067 tree tem = make_ssa_name (type);
2068 gimple_call_set_lhs (call, tem);
2069 enum tree_code code = object_size_type & OST_MINIMUM ? MAX_EXPR : MIN_EXPR;
2070 tree cst = fold_convert (type, bytes);
2071 gimple *g = gimple_build_assign (lhs, code, tem, cst);
2072 gsi_insert_after (i, g, GSI_NEW_STMT);
2073 update_stmt (call);
2076 /* Attempt to fold one __builtin_dynamic_object_size call in CALL into an
2077 expression and insert it at I. Return true if it succeeds. */
2079 static bool
2080 dynamic_object_sizes_execute_one (gimple_stmt_iterator *i, gimple *call)
2082 gcc_assert (gimple_call_num_args (call) == 2);
2084 tree args[2];
2085 args[0] = gimple_call_arg (call, 0);
2086 args[1] = gimple_call_arg (call, 1);
2088 location_t loc = EXPR_LOC_OR_LOC (args[0], input_location);
2089 tree result_type = gimple_call_return_type (as_a <gcall *> (call));
2090 tree result = fold_builtin_call_array (loc, result_type,
2091 gimple_call_fn (call), 2, args);
2093 if (!result)
2094 return false;
2096 /* fold_builtin_call_array may wrap the result inside a
2097 NOP_EXPR. */
2098 STRIP_NOPS (result);
2099 gimplify_and_update_call_from_tree (i, result);
2101 if (dump_file && (dump_flags & TDF_DETAILS))
2103 fprintf (dump_file, "Simplified (dynamic)\n ");
2104 print_gimple_stmt (dump_file, call, 0, dump_flags);
2105 fprintf (dump_file, " to ");
2106 print_generic_expr (dump_file, result);
2107 fprintf (dump_file, "\n");
2109 return true;
2112 static unsigned int
2113 object_sizes_execute (function *fun, bool early)
2115 todo = 0;
2117 basic_block bb;
2118 FOR_EACH_BB_FN (bb, fun)
2120 gimple_stmt_iterator i;
2121 for (i = gsi_start_bb (bb); !gsi_end_p (i); gsi_next (&i))
2123 tree result;
2124 bool dynamic = false;
2126 gimple *call = gsi_stmt (i);
2127 if (gimple_call_builtin_p (call, BUILT_IN_DYNAMIC_OBJECT_SIZE))
2128 dynamic = true;
2129 else if (!gimple_call_builtin_p (call, BUILT_IN_OBJECT_SIZE))
2130 continue;
2132 tree lhs = gimple_call_lhs (call);
2133 if (!lhs)
2134 continue;
2136 init_object_sizes ();
2138 /* If early, only attempt to fold
2139 __builtin_object_size (x, 1) and __builtin_object_size (x, 3),
2140 and rather than folding the builtin to the constant if any,
2141 create a MIN_EXPR or MAX_EXPR of the __builtin_object_size
2142 call result and the computed constant. Do the same for
2143 __builtin_dynamic_object_size too. */
2144 if (early)
2146 early_object_sizes_execute_one (&i, call);
2147 continue;
2150 if (dynamic)
2152 if (dynamic_object_sizes_execute_one (&i, call))
2153 continue;
2154 else
2156 /* If we could not find a suitable size expression, lower to
2157 __builtin_object_size so that we may at least get a
2158 constant lower or higher estimate. */
2159 tree bosfn = builtin_decl_implicit (BUILT_IN_OBJECT_SIZE);
2160 gimple_call_set_fndecl (call, bosfn);
2161 update_stmt (call);
2163 if (dump_file && (dump_flags & TDF_DETAILS))
2165 print_generic_expr (dump_file, gimple_call_arg (call, 0),
2166 dump_flags);
2167 fprintf (dump_file,
2168 ": Retrying as __builtin_object_size\n");
2173 result = gimple_fold_stmt_to_constant (call, do_valueize);
2174 if (!result)
2176 tree ost = gimple_call_arg (call, 1);
2178 if (tree_fits_uhwi_p (ost))
2180 unsigned HOST_WIDE_INT object_size_type = tree_to_uhwi (ost);
2182 if (object_size_type & OST_MINIMUM)
2183 result = build_zero_cst (size_type_node);
2184 else if (object_size_type < OST_END)
2185 result = fold_convert (size_type_node,
2186 integer_minus_one_node);
2189 if (!result)
2190 continue;
2193 gcc_assert (TREE_CODE (result) == INTEGER_CST);
2195 if (dump_file && (dump_flags & TDF_DETAILS))
2197 fprintf (dump_file, "Simplified\n ");
2198 print_gimple_stmt (dump_file, call, 0, dump_flags);
2199 fprintf (dump_file, " to ");
2200 print_generic_expr (dump_file, result);
2201 fprintf (dump_file, "\n");
2204 /* Propagate into all uses and fold those stmts. */
2205 if (!SSA_NAME_OCCURS_IN_ABNORMAL_PHI (lhs))
2206 replace_uses_by (lhs, result);
2207 else
2208 replace_call_with_value (&i, result);
2212 fini_object_sizes ();
2213 return todo;
2216 /* Simple pass to optimize all __builtin_object_size () builtins. */
2218 namespace {
2220 const pass_data pass_data_object_sizes =
2222 GIMPLE_PASS, /* type */
2223 "objsz", /* name */
2224 OPTGROUP_NONE, /* optinfo_flags */
2225 TV_NONE, /* tv_id */
2226 ( PROP_cfg | PROP_ssa ), /* properties_required */
2227 PROP_objsz, /* properties_provided */
2228 0, /* properties_destroyed */
2229 0, /* todo_flags_start */
2230 0, /* todo_flags_finish */
2233 class pass_object_sizes : public gimple_opt_pass
2235 public:
2236 pass_object_sizes (gcc::context *ctxt)
2237 : gimple_opt_pass (pass_data_object_sizes, ctxt)
2240 /* opt_pass methods: */
2241 opt_pass * clone () final override { return new pass_object_sizes (m_ctxt); }
2242 unsigned int execute (function *fun) final override
2244 return object_sizes_execute (fun, false);
2246 }; // class pass_object_sizes
2248 } // anon namespace
2250 gimple_opt_pass *
2251 make_pass_object_sizes (gcc::context *ctxt)
2253 return new pass_object_sizes (ctxt);
2256 /* Early version of pass to optimize all __builtin_object_size () builtins. */
2258 namespace {
2260 const pass_data pass_data_early_object_sizes =
2262 GIMPLE_PASS, /* type */
2263 "early_objsz", /* name */
2264 OPTGROUP_NONE, /* optinfo_flags */
2265 TV_NONE, /* tv_id */
2266 ( PROP_cfg | PROP_ssa ), /* properties_required */
2267 0, /* properties_provided */
2268 0, /* properties_destroyed */
2269 0, /* todo_flags_start */
2270 0, /* todo_flags_finish */
2273 class pass_early_object_sizes : public gimple_opt_pass
2275 public:
2276 pass_early_object_sizes (gcc::context *ctxt)
2277 : gimple_opt_pass (pass_data_early_object_sizes, ctxt)
2280 /* opt_pass methods: */
2281 unsigned int execute (function *fun) final override
2283 return object_sizes_execute (fun, true);
2285 }; // class pass_object_sizes
2287 } // anon namespace
2289 gimple_opt_pass *
2290 make_pass_early_object_sizes (gcc::context *ctxt)
2292 return new pass_early_object_sizes (ctxt);