c++: retval dtor on rethrow [PR112301]
[official-gcc.git] / gcc / tree-object-size.cc
blob28f27adf9ca2f2e86c527277cbb3a3a3e74a16ad
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 && ALLOCA_FUNCTION_CODE_P (DECL_FUNCTION_CODE (callfn)))
798 arg1 = 0;
800 /* Non-const arguments are OK here, let the caller handle constness. */
801 if (arg1 < 0 || arg1 >= (int) gimple_call_num_args (call)
802 || arg2 >= (int) gimple_call_num_args (call))
803 return size_unknown (object_size_type);
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));
813 return bytes ? bytes : size_unknown (object_size_type);
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). */
821 static tree
822 strdup_object_size (const gcall *call, int object_size_type, bool is_strndup)
824 tree src = gimple_call_arg (call, 0);
825 tree sz = size_unknown (object_size_type);
826 tree n = NULL_TREE;
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
835 tree strlen_fn = builtin_decl_implicit (BUILT_IN_STRLEN);
836 if (strlen_fn)
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;
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 (sz, object_size_type)
849 || size_unknown_p (sz, object_size_type))
851 tree wholesrc = NULL_TREE;
852 if (TREE_CODE (src) == ADDR_EXPR)
853 wholesrc = get_base_address (TREE_OPERAND (src, 0));
855 /* If the source points within a string constant, we try to get its
856 length. */
857 if (wholesrc && TREE_CODE (wholesrc) == STRING_CST)
859 tree len = c_strlen (src, 0);
860 if (len)
861 sz = fold_build2 (PLUS_EXPR, sizetype, size_one_node, len);
864 /* For maximum estimate, our next best guess is the object size of the
865 source. */
866 if (size_unknown_p (sz, object_size_type)
867 && !(object_size_type & OST_MINIMUM))
868 compute_builtin_object_size (src, object_size_type, &sz);
871 /* String duplication allocates at least one byte, so we should never fail
872 for OST_MINIMUM. */
873 if ((!size_valid_p (sz, object_size_type)
874 || size_unknown_p (sz, object_size_type))
875 && (object_size_type & OST_MINIMUM))
876 sz = size_one_node;
878 /* Factor in the N. */
879 return n ? fold_build2 (MIN_EXPR, sizetype, n, sz) : sz;
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. */
886 static tree
887 pass_through_call (const gcall *call)
889 unsigned rf = gimple_call_return_flags (call);
890 if (rf & ERF_RETURNS_ARG)
892 unsigned argnum = rf & ERF_RETURN_ARG_MASK;
893 if (argnum < gimple_call_num_args (call))
894 return gimple_call_arg (call, argnum);
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 (call, 0);
901 return NULL_TREE;
904 /* Emit PHI nodes for size expressions fo. */
906 static void
907 emit_phi_nodes (gimple *stmt, tree size, tree wholesize)
909 tree phires;
910 gphi *wholephi = NULL;
912 if (wholesize != size)
914 phires = TREE_VEC_ELT (wholesize, TREE_VEC_LENGTH (wholesize) - 1);
915 wholephi = create_phi_node (phires, gimple_bb (stmt));
918 phires = TREE_VEC_ELT (size, TREE_VEC_LENGTH (size) - 1);
919 gphi *phi = create_phi_node (phires, gimple_bb (stmt));
920 gphi *obj_phi = as_a <gphi *> (stmt);
922 gcc_checking_assert (TREE_CODE (wholesize) == TREE_VEC);
923 gcc_checking_assert (TREE_CODE (size) == TREE_VEC);
925 for (unsigned i = 0; i < gimple_phi_num_args (stmt); i++)
927 gimple_seq seq = NULL;
928 tree wsz = TREE_VEC_ELT (wholesize, i);
929 tree sz = TREE_VEC_ELT (size, i);
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)
937 gimple_seq s;
938 sz = force_gimple_operand (sz, &s, true, NULL);
939 gimple_seq_add_seq (&seq, s);
942 if (seq)
943 gsi_insert_seq_on_edge (gimple_phi_arg_edge (obj_phi, i), seq);
945 if (wholephi)
946 add_phi_arg (wholephi, wsz,
947 gimple_phi_arg_edge (obj_phi, i),
948 gimple_phi_arg_location (obj_phi, i));
950 add_phi_arg (phi, sz,
951 gimple_phi_arg_edge (obj_phi, i),
952 gimple_phi_arg_location (obj_phi, i));
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. */
960 static tree
961 propagate_unknowns (object_size_info *osi, tree expr)
963 int object_size_type = osi->object_size_type;
965 switch (TREE_CODE (expr))
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;
972 case MIN_EXPR:
973 case MAX_EXPR:
975 tree res = propagate_unknowns (osi, TREE_OPERAND (expr, 0));
976 if (size_unknown_p (res, object_size_type))
977 return res;
979 res = propagate_unknowns (osi, TREE_OPERAND (expr, 1));
980 if (size_unknown_p (res, object_size_type))
981 return res;
983 return expr;
985 case MODIFY_EXPR:
987 tree res = propagate_unknowns (osi, TREE_OPERAND (expr, 1));
988 if (size_unknown_p (res, object_size_type))
989 return res;
990 return expr;
992 case TREE_VEC:
993 for (int i = 0; i < TREE_VEC_LENGTH (expr); i++)
995 tree res = propagate_unknowns (osi, TREE_VEC_ELT (expr, i));
996 if (size_unknown_p (res, object_size_type))
997 return res;
999 return expr;
1000 case PLUS_EXPR:
1001 case MINUS_EXPR:
1003 tree res = propagate_unknowns (osi, TREE_OPERAND (expr, 0));
1004 if (size_unknown_p (res, object_size_type))
1005 return res;
1007 return expr;
1009 default:
1010 return expr;
1014 /* Walk through size expressions that need reexamination and generate
1015 statements for them. */
1017 static void
1018 gimplify_size_expressions (object_size_info *osi)
1020 int object_size_type = osi->object_size_type;
1021 bitmap_iterator bi;
1022 unsigned int i;
1023 bool changed;
1025 /* Step 1: Propagate unknowns into expressions. */
1026 bitmap reexamine = BITMAP_ALLOC (NULL);
1027 bitmap_copy (reexamine, osi->reexamine);
1030 changed = false;
1031 EXECUTE_IF_SET_IN_BITMAP (reexamine, 0, i, bi)
1033 object_size cur = object_sizes_get_raw (osi, i);
1035 if (size_unknown_p (propagate_unknowns (osi, cur.size),
1036 object_size_type)
1037 || size_unknown_p (propagate_unknowns (osi, cur.wholesize),
1038 object_size_type))
1040 object_sizes_set (osi, i,
1041 size_unknown (object_size_type),
1042 size_unknown (object_size_type));
1043 changed = true;
1046 bitmap_copy (reexamine, osi->reexamine);
1048 while (changed);
1050 /* Release all unknowns. */
1051 EXECUTE_IF_SET_IN_BITMAP (osi->unknowns, 0, i, bi)
1052 release_ssa_name (ssa_name (i));
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)
1058 gimple_seq seq = NULL;
1059 object_size osize = object_sizes_get_raw (osi, i);
1061 gimple *stmt = SSA_NAME_DEF_STMT (ssa_name (i));
1062 enum gimple_code code = gimple_code (stmt);
1064 /* PHI nodes need special attention. */
1065 if (code == GIMPLE_PHI)
1066 emit_phi_nodes (stmt, osize.size, osize.wholesize);
1067 else
1069 tree size_expr = NULL_TREE;
1071 /* Bundle wholesize in with the size to gimplify if needed. */
1072 if (osize.wholesize != osize.size
1073 && !size_usable_p (osize.wholesize))
1074 size_expr = size_binop (COMPOUND_EXPR,
1075 osize.wholesize,
1076 osize.size);
1077 else if (!size_usable_p (osize.size))
1078 size_expr = osize.size;
1080 if (size_expr)
1082 gimple_stmt_iterator gsi;
1083 if (code == GIMPLE_NOP)
1084 gsi = gsi_start_bb (single_succ (ENTRY_BLOCK_PTR_FOR_FN (cfun)));
1085 else
1086 gsi = gsi_for_stmt (stmt);
1088 force_gimple_operand (size_expr, &seq, true, NULL);
1089 gsi_insert_seq_before (&gsi, seq, GSI_CONTINUE_LINKING);
1093 /* We're done, so replace the MODIFY_EXPRs with the SSA names. */
1094 object_sizes_initialize (osi, i,
1095 object_sizes_get (osi, i),
1096 object_sizes_get (osi, i, true));
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. */
1107 bool
1108 compute_builtin_object_size (tree ptr, int object_size_type,
1109 tree *psize)
1111 gcc_assert (object_size_type >= 0 && object_size_type < OST_END);
1113 /* Set to unknown and overwrite just before returning if the size
1114 could be determined. */
1115 *psize = size_unknown (object_size_type);
1117 if (! offset_limit)
1118 init_offset_limit ();
1120 if (TREE_CODE (ptr) == ADDR_EXPR)
1121 return addr_object_size (NULL, ptr, object_size_type, psize);
1123 if (TREE_CODE (ptr) != SSA_NAME
1124 || !POINTER_TYPE_P (TREE_TYPE (ptr)))
1125 return false;
1127 if (computed[object_size_type] == NULL)
1129 if (optimize || object_size_type & OST_SUBOBJECT)
1130 return false;
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 (def) == GIMPLE_ASSIGN)
1138 tree_code code = gimple_assign_rhs_code (def);
1139 if (code == POINTER_PLUS_EXPR)
1141 tree offset = gimple_assign_rhs2 (def);
1142 ptr = gimple_assign_rhs1 (def);
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))
1150 *psize = size_for_offset (*psize, offset);
1151 return true;
1155 return false;
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)))
1162 bitmap_iterator bi;
1163 unsigned int i;
1165 object_sizes_grow (object_size_type);
1166 if (dump_file)
1168 fprintf (dump_file, "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 (dump_file, ":\n");
1176 osi.visited = BITMAP_ALLOC (NULL);
1177 osi.reexamine = BITMAP_ALLOC (NULL);
1179 if (object_size_type & OST_DYNAMIC)
1180 osi.unknowns = BITMAP_ALLOC (NULL);
1181 else
1183 osi.depths = NULL;
1184 osi.stack = NULL;
1185 osi.tos = NULL;
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);
1196 if (object_size_type & OST_DYNAMIC)
1198 osi.pass = 1;
1199 gimplify_size_expressions (&osi);
1200 BITMAP_FREE (osi.unknowns);
1201 bitmap_clear (osi.reexamine);
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 (osi.reexamine))
1209 bitmap reexamine = BITMAP_ALLOC (NULL);
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)
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));
1230 free (osi.depths);
1231 osi.depths = NULL;
1232 free (osi.stack);
1233 osi.stack = NULL;
1234 osi.tos = NULL;
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))
1247 collect_object_sizes_for (&osi, ssa_name (i));
1248 if (dump_file && (dump_flags & TDF_DETAILS))
1250 fprintf (dump_file, "Reexamining ");
1251 print_generic_expr (dump_file, ssa_name (i),
1252 dump_flags);
1253 fprintf (dump_file, "\n");
1257 while (osi.changed);
1259 BITMAP_FREE (reexamine);
1261 EXECUTE_IF_SET_IN_BITMAP (osi.reexamine, 0, i, bi)
1262 bitmap_set_bit (computed[object_size_type], i);
1264 /* Debugging dumps. */
1265 if (dump_file)
1267 EXECUTE_IF_SET_IN_BITMAP (osi.visited, 0, i, bi)
1268 if (!object_sizes_unknown_p (object_size_type, i))
1270 print_generic_expr (dump_file, ssa_name (i),
1271 dump_flags);
1272 fprintf (dump_file,
1273 ": %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, i),
1279 dump_flags);
1280 fprintf (dump_file, "\n");
1284 BITMAP_FREE (osi.reexamine);
1285 BITMAP_FREE (osi.visited);
1288 *psize = object_sizes_get (&osi, SSA_NAME_VERSION (ptr));
1289 return !size_unknown_p (*psize, object_size_type);
1292 /* Compute object_sizes for PTR, defined to VALUE, which is not an SSA_NAME. */
1294 static void
1295 expr_object_size (struct object_size_info *osi, tree ptr, tree value)
1297 int object_size_type = osi->object_size_type;
1298 unsigned int varno = SSA_NAME_VERSION (ptr);
1299 tree bytes, wholesize;
1301 gcc_assert (!object_sizes_unknown_p (object_size_type, varno));
1302 gcc_assert (osi->pass == 0);
1304 if (TREE_CODE (value) == WITH_SIZE_EXPR)
1305 value = TREE_OPERAND (value, 0);
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)));
1311 if (TREE_CODE (value) == ADDR_EXPR)
1312 addr_object_size (osi, value, object_size_type, &bytes, &wholesize);
1313 else
1314 bytes = wholesize = size_unknown (object_size_type);
1316 object_sizes_set (osi, varno, bytes, wholesize);
1320 /* Compute object_sizes for PTR, defined to the result of a call. */
1322 static void
1323 call_object_size (struct object_size_info *osi, tree ptr, gcall *call)
1325 int object_size_type = osi->object_size_type;
1326 unsigned int varno = SSA_NAME_VERSION (ptr);
1327 tree bytes = NULL_TREE;
1329 gcc_assert (is_gimple_call (call));
1331 gcc_assert (!object_sizes_unknown_p (object_size_type, varno));
1332 gcc_assert (osi->pass == 0);
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);
1341 if (!size_valid_p (bytes, object_size_type))
1342 bytes = size_unknown (object_size_type);
1344 object_sizes_set (osi, varno, bytes, bytes);
1348 /* Compute object_sizes for PTR, defined to an unknown value. */
1350 static void
1351 unknown_object_size (struct object_size_info *osi, tree ptr)
1353 int object_size_type = osi->object_size_type;
1354 unsigned int varno = SSA_NAME_VERSION (ptr);
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);
1360 object_sizes_set (osi, varno, bytes, bytes);
1364 /* Merge object sizes of ORIG + OFFSET into DEST. Return true if
1365 the object size might need reexamination later. */
1367 static bool
1368 merge_object_sizes (struct object_size_info *osi, tree dest, tree orig)
1370 int object_size_type = osi->object_size_type;
1371 unsigned int varno = SSA_NAME_VERSION (dest);
1372 tree orig_bytes, wholesize;
1374 if (object_sizes_unknown_p (object_size_type, varno))
1375 return false;
1377 if (osi->pass == 0)
1378 collect_object_sizes_for (osi, orig);
1380 orig_bytes = object_sizes_get (osi, SSA_NAME_VERSION (orig));
1381 wholesize = object_sizes_get (osi, SSA_NAME_VERSION (orig), true);
1383 if (object_sizes_set (osi, varno, orig_bytes, wholesize))
1384 osi->changed = true;
1386 return bitmap_bit_p (osi->reexamine, SSA_NAME_VERSION (orig));
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. */
1394 static bool
1395 plus_stmt_object_size (struct object_size_info *osi, tree var, gimple *stmt)
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;
1403 if (gimple_assign_rhs_code (stmt) == POINTER_PLUS_EXPR)
1405 op0 = gimple_assign_rhs1 (stmt);
1406 op1 = gimple_assign_rhs2 (stmt);
1408 else if (gimple_assign_rhs_code (stmt) == ADDR_EXPR)
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);
1415 else
1416 gcc_unreachable ();
1418 if (object_sizes_unknown_p (object_size_type, varno))
1419 return false;
1421 /* Handle PTR + OFFSET here. */
1422 if (size_valid_p (op1, object_size_type)
1423 && (TREE_CODE (op0) == SSA_NAME || TREE_CODE (op0) == ADDR_EXPR))
1425 if (TREE_CODE (op0) == SSA_NAME)
1427 if (osi->pass == 0)
1428 collect_object_sizes_for (osi, op0);
1430 bytes = object_sizes_get (osi, SSA_NAME_VERSION (op0));
1431 wholesize = object_sizes_get (osi, SSA_NAME_VERSION (op0), true);
1432 reexamine = bitmap_bit_p (osi->reexamine, SSA_NAME_VERSION (op0));
1434 else
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, op0, object_size_type, &bytes, &wholesize);
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 (bytes, 0))
1447 else if ((object_size_type & OST_DYNAMIC)
1448 || compare_tree_int (op1, offset_limit) <= 0)
1449 bytes = size_for_offset (bytes, 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;
1458 else
1459 bytes = wholesize = size_unknown (object_size_type);
1461 if (!size_valid_p (bytes, object_size_type)
1462 || !size_valid_p (wholesize, object_size_type))
1463 bytes = wholesize = size_unknown (object_size_type);
1465 if (object_sizes_set (osi, varno, bytes, wholesize))
1466 osi->changed = true;
1467 return reexamine;
1470 /* Compute the dynamic object size for VAR. Return the result in SIZE and
1471 WHOLESIZE. */
1473 static void
1474 dynamic_object_size (struct object_size_info *osi, tree var,
1475 tree *size, tree *wholesize)
1477 int object_size_type = osi->object_size_type;
1479 if (TREE_CODE (var) == SSA_NAME)
1481 unsigned varno = SSA_NAME_VERSION (var);
1483 collect_object_sizes_for (osi, var);
1484 *size = object_sizes_get (osi, varno);
1485 *wholesize = object_sizes_get (osi, varno, true);
1487 else if (TREE_CODE (var) == ADDR_EXPR)
1488 addr_object_size (osi, var, object_size_type, size, wholesize);
1489 else
1490 *size = *wholesize = size_unknown (object_size_type);
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. */
1497 static bool
1498 cond_expr_object_size (struct object_size_info *osi, tree var, gimple *stmt)
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;
1505 gcc_assert (gimple_assign_rhs_code (stmt) == COND_EXPR);
1507 if (object_sizes_unknown_p (object_size_type, varno))
1508 return false;
1510 then_ = gimple_assign_rhs2 (stmt);
1511 else_ = gimple_assign_rhs3 (stmt);
1513 if (object_size_type & OST_DYNAMIC)
1515 tree then_size, then_wholesize, else_size, else_wholesize;
1517 dynamic_object_size (osi, then_, &then_size, &then_wholesize);
1518 if (!size_unknown_p (then_size, object_size_type))
1519 dynamic_object_size (osi, else_, &else_size, &else_wholesize);
1521 tree cond_size, cond_wholesize;
1522 if (size_unknown_p (then_size, object_size_type)
1523 || size_unknown_p (else_size, object_size_type))
1524 cond_size = cond_wholesize = size_unknown (object_size_type);
1525 else
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);
1535 object_sizes_set (osi, varno, cond_size, cond_wholesize);
1537 return false;
1540 if (TREE_CODE (then_) == SSA_NAME)
1541 reexamine |= merge_object_sizes (osi, var, then_);
1542 else
1543 expr_object_size (osi, var, then_);
1545 if (object_sizes_unknown_p (object_size_type, varno))
1546 return reexamine;
1548 if (TREE_CODE (else_) == SSA_NAME)
1549 reexamine |= merge_object_sizes (osi, var, else_);
1550 else
1551 expr_object_size (osi, var, else_);
1553 return reexamine;
1556 /* Find size of an object passed as a parameter to the function. */
1558 static void
1559 parm_object_size (struct object_size_info *osi, tree var)
1561 int object_size_type = osi->object_size_type;
1562 tree parm = SSA_NAME_VAR (var);
1564 if (!(object_size_type & OST_DYNAMIC) || !POINTER_TYPE_P (TREE_TYPE (parm)))
1566 expr_object_size (osi, var, parm);
1567 return;
1570 /* Look for access attribute. */
1571 rdwr_map rdwr_idx;
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;
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 (typesize))
1583 || (!typesize && VOID_TYPE_P (TREE_TYPE (TREE_TYPE (parm))))))
1585 tree fnargs = DECL_ARGUMENTS (fndecl);
1586 tree arg = NULL_TREE;
1587 unsigned argpos = 0;
1589 /* ... then walk through the parameters to pick the size parameter and
1590 safely scale it by the type size if needed.
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)
1597 gcc_assert (INTEGRAL_TYPE_P (TREE_TYPE (arg)));
1598 sz = get_or_create_ssa_default_def (cfun, arg);
1599 if (sz != NULL_TREE)
1601 sz = fold_convert (sizetype, sz);
1602 if (typesize)
1603 sz = size_binop (MULT_EXPR, sz, typesize);
1605 break;
1608 if (!sz)
1609 sz = size_unknown (object_size_type);
1611 object_sizes_set (osi, SSA_NAME_VERSION (var), sz, sz);
1614 /* Compute an object size expression for VAR, which is the result of a PHI
1615 node. */
1617 static void
1618 phi_dynamic_object_size (struct object_size_info *osi, tree var)
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 (stmt);
1624 bool wholesize_needed = false;
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);
1631 /* Bail out if the size of any of the PHI arguments cannot be
1632 determined. */
1633 for (i = 0; i < num_args; i++)
1635 edge e = gimple_phi_arg_edge (as_a <gphi *> (stmt), i);
1636 if (e->flags & EDGE_COMPLEX)
1637 break;
1639 tree rhs = gimple_phi_arg_def (stmt, i);
1640 tree size, wholesize;
1642 dynamic_object_size (osi, rhs, &size, &wholesize);
1644 if (size_unknown_p (size, object_size_type))
1645 break;
1647 if (size != wholesize)
1648 wholesize_needed = true;
1650 TREE_VEC_ELT (sizes, i) = size;
1651 TREE_VEC_ELT (wholesizes, i) = wholesize;
1654 if (i < num_args)
1656 ggc_free (sizes);
1657 ggc_free (wholesizes);
1658 sizes = wholesizes = size_unknown (object_size_type);
1661 /* Point to the same TREE_VEC so that we can avoid emitting two PHI
1662 nodes. */
1663 else if (!wholesize_needed)
1665 ggc_free (wholesizes);
1666 wholesizes = sizes;
1669 object_sizes_set (osi, varno, sizes, wholesizes);
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. */
1692 static void
1693 collect_object_sizes_for (struct object_size_info *osi, tree var)
1695 int object_size_type = osi->object_size_type;
1696 unsigned int varno = SSA_NAME_VERSION (var);
1697 gimple *stmt;
1698 bool reexamine;
1700 if (bitmap_bit_p (computed[object_size_type], varno))
1701 return;
1703 if (osi->pass == 0)
1705 if (bitmap_set_bit (osi->visited, varno))
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 size_initval (object_size_type),
1711 size_initval (object_size_type));
1713 else
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);
1720 bitmap_set_bit (osi->reexamine, varno);
1721 if (dump_file && (dump_flags & TDF_DETAILS))
1723 fprintf (dump_file, "Found a dependency loop at ");
1724 print_generic_expr (dump_file, var, dump_flags);
1725 fprintf (dump_file, "\n");
1727 return;
1731 if (dump_file && (dump_flags & TDF_DETAILS))
1733 fprintf (dump_file, "Visiting use-def links for ");
1734 print_generic_expr (dump_file, var, dump_flags);
1735 fprintf (dump_file, "\n");
1738 stmt = SSA_NAME_DEF_STMT (var);
1739 reexamine = false;
1741 switch (gimple_code (stmt))
1743 case GIMPLE_ASSIGN:
1745 tree rhs = gimple_assign_rhs1 (stmt);
1746 if (gimple_assign_rhs_code (stmt) == POINTER_PLUS_EXPR
1747 || (gimple_assign_rhs_code (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 (stmt) == COND_EXPR)
1751 reexamine = cond_expr_object_size (osi, var, stmt);
1752 else if (gimple_assign_single_p (stmt)
1753 || gimple_assign_unary_nop_p (stmt))
1755 if (TREE_CODE (rhs) == SSA_NAME
1756 && POINTER_TYPE_P (TREE_TYPE (rhs)))
1757 reexamine = merge_object_sizes (osi, var, rhs);
1758 else
1759 expr_object_size (osi, var, rhs);
1761 else
1762 unknown_object_size (osi, var);
1763 break;
1766 case GIMPLE_CALL:
1768 gcall *call_stmt = as_a <gcall *> (stmt);
1769 tree arg = pass_through_call (call_stmt);
1770 if (arg)
1772 if (TREE_CODE (arg) == SSA_NAME
1773 && POINTER_TYPE_P (TREE_TYPE (arg)))
1774 reexamine = merge_object_sizes (osi, var, arg);
1775 else
1776 expr_object_size (osi, var, arg);
1778 else
1779 call_object_size (osi, var, call_stmt);
1780 break;
1783 case GIMPLE_ASM:
1784 /* Pointers defined by __asm__ statements can point anywhere. */
1785 unknown_object_size (osi, var);
1786 break;
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, var);
1795 break;
1797 case GIMPLE_PHI:
1799 unsigned i;
1801 if (object_size_type & OST_DYNAMIC)
1803 phi_dynamic_object_size (osi, var);
1804 break;
1807 for (i = 0; i < gimple_phi_num_args (stmt); i++)
1809 tree rhs = gimple_phi_arg (stmt, i)->def;
1811 if (object_sizes_unknown_p (object_size_type, varno))
1812 break;
1814 if (TREE_CODE (rhs) == SSA_NAME)
1815 reexamine |= merge_object_sizes (osi, var, rhs);
1816 else if (osi->pass == 0)
1817 expr_object_size (osi, var, rhs);
1819 break;
1822 default:
1823 gcc_unreachable ();
1826 if (! reexamine || object_sizes_unknown_p (object_size_type, varno))
1828 bitmap_set_bit (computed[object_size_type], varno);
1829 if (!(object_size_type & OST_DYNAMIC))
1830 bitmap_clear_bit (osi->reexamine, varno);
1832 else
1834 bitmap_set_bit (osi->reexamine, varno);
1835 if (dump_file && (dump_flags & TDF_DETAILS))
1837 fprintf (dump_file, "Need to reexamine ");
1838 print_generic_expr (dump_file, var, dump_flags);
1839 fprintf (dump_file, "\n");
1845 /* Helper function for check_for_plus_in_loops. Called recursively
1846 to detect loops. */
1848 static void
1849 check_for_plus_in_loops_1 (struct object_size_info *osi, tree var,
1850 unsigned int depth)
1852 gimple *stmt = SSA_NAME_DEF_STMT (var);
1853 unsigned int varno = SSA_NAME_VERSION (var);
1855 if (osi->depths[varno])
1857 if (osi->depths[varno] != depth)
1859 unsigned int *sp;
1861 /* Found a loop involving pointer addition. */
1862 for (sp = osi->tos; sp > osi->stack; )
1864 --sp;
1865 bitmap_clear_bit (osi->reexamine, *sp);
1866 bitmap_set_bit (computed[osi->object_size_type], *sp);
1867 object_sizes_set (osi, *sp, size_zero_node,
1868 object_sizes_get (osi, *sp, true));
1869 if (*sp == varno)
1870 break;
1873 return;
1875 else if (! bitmap_bit_p (osi->reexamine, varno))
1876 return;
1878 osi->depths[varno] = depth;
1879 *osi->tos++ = varno;
1881 switch (gimple_code (stmt))
1884 case GIMPLE_ASSIGN:
1886 if ((gimple_assign_single_p (stmt)
1887 || gimple_assign_unary_nop_p (stmt))
1888 && TREE_CODE (gimple_assign_rhs1 (stmt)) == SSA_NAME)
1890 tree rhs = gimple_assign_rhs1 (stmt);
1892 check_for_plus_in_loops_1 (osi, rhs, depth);
1894 else if (gimple_assign_rhs_code (stmt) == POINTER_PLUS_EXPR)
1896 tree basevar = gimple_assign_rhs1 (stmt);
1897 tree cst = gimple_assign_rhs2 (stmt);
1899 gcc_assert (TREE_CODE (cst) == INTEGER_CST);
1901 check_for_plus_in_loops_1 (osi, basevar,
1902 depth + !integer_zerop (cst));
1904 else
1905 gcc_unreachable ();
1906 break;
1909 case GIMPLE_CALL:
1911 gcall *call_stmt = as_a <gcall *> (stmt);
1912 tree arg = pass_through_call (call_stmt);
1913 if (arg)
1915 if (TREE_CODE (arg) == SSA_NAME)
1916 check_for_plus_in_loops_1 (osi, arg, depth);
1917 else
1918 gcc_unreachable ();
1920 break;
1923 case GIMPLE_PHI:
1925 unsigned i;
1927 for (i = 0; i < gimple_phi_num_args (stmt); i++)
1929 tree rhs = gimple_phi_arg (stmt, i)->def;
1931 if (TREE_CODE (rhs) == SSA_NAME)
1932 check_for_plus_in_loops_1 (osi, rhs, depth);
1934 break;
1937 default:
1938 gcc_unreachable ();
1941 osi->depths[varno] = 0;
1942 osi->tos--;
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. */
1950 static void
1951 check_for_plus_in_loops (struct object_size_info *osi, tree var)
1953 gimple *stmt = SSA_NAME_DEF_STMT (var);
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. */
1960 if (is_gimple_assign (stmt)
1961 && gimple_assign_rhs_code (stmt) == POINTER_PLUS_EXPR)
1963 tree basevar = gimple_assign_rhs1 (stmt);
1964 tree cst = gimple_assign_rhs2 (stmt);
1966 gcc_assert (TREE_CODE (cst) == INTEGER_CST);
1968 /* Skip non-positive offsets. */
1969 if (integer_zerop (cst) || compare_tree_int (cst, offset_limit) > 0)
1970 return;
1972 osi->depths[SSA_NAME_VERSION (basevar)] = 1;
1973 *osi->tos++ = SSA_NAME_VERSION (basevar);
1974 check_for_plus_in_loops_1 (osi, var, 2);
1975 osi->depths[SSA_NAME_VERSION (basevar)] = 0;
1976 osi->tos--;
1981 /* Initialize data structures for the object size computation. */
1983 void
1984 init_object_sizes (void)
1986 int object_size_type;
1988 if (computed[0])
1989 return;
1991 for (object_size_type = 0; object_size_type < OST_END; object_size_type++)
1993 object_sizes_grow (object_size_type);
1994 computed[object_size_type] = BITMAP_ALLOC (NULL);
1997 init_offset_limit ();
2001 /* Destroy data structures after the object size computation. */
2003 void
2004 fini_object_sizes (void)
2006 int object_size_type;
2008 for (object_size_type = 0; object_size_type < OST_END; object_size_type++)
2010 object_sizes_release (object_size_type);
2011 BITMAP_FREE (computed[object_size_type]);
2015 /* Dummy valueize function. */
2017 static tree
2018 do_valueize (tree t)
2020 return t;
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. */
2030 static void
2031 early_object_sizes_execute_one (gimple_stmt_iterator *i, gimple *call)
2033 tree ost = gimple_call_arg (call, 1);
2034 tree lhs = gimple_call_lhs (call);
2035 gcc_assert (lhs != NULL_TREE);
2037 if (!tree_fits_uhwi_p (ost))
2038 return;
2040 unsigned HOST_WIDE_INT object_size_type = tree_to_uhwi (ost);
2041 tree ptr = gimple_call_arg (call, 0);
2043 if (object_size_type != 1 && object_size_type != 3)
2044 return;
2046 if (TREE_CODE (ptr) != ADDR_EXPR && TREE_CODE (ptr) != SSA_NAME)
2047 return;
2049 tree type = TREE_TYPE (lhs);
2050 tree bytes;
2051 if (!compute_builtin_object_size (ptr, object_size_type, &bytes)
2052 || !int_fits_type_p (bytes, type))
2053 return;
2055 tree tem = make_ssa_name (type);
2056 gimple_call_set_lhs (call, 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 (call);
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. */
2067 static bool
2068 dynamic_object_sizes_execute_one (gimple_stmt_iterator *i, gimple *call)
2070 gcc_assert (gimple_call_num_args (call) == 2);
2072 tree args[2];
2073 args[0] = gimple_call_arg (call, 0);
2074 args[1] = gimple_call_arg (call, 1);
2076 location_t loc = EXPR_LOC_OR_LOC (args[0], input_location);
2077 tree result_type = gimple_call_return_type (as_a <gcall *> (call));
2078 tree result = fold_builtin_call_array (loc, result_type,
2079 gimple_call_fn (call), 2, args);
2081 if (!result)
2082 return false;
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);
2089 if (dump_file && (dump_flags & TDF_DETAILS))
2091 fprintf (dump_file, "Simplified (dynamic)\n ");
2092 print_gimple_stmt (dump_file, call, 0, dump_flags);
2093 fprintf (dump_file, " to ");
2094 print_generic_expr (dump_file, result);
2095 fprintf (dump_file, "\n");
2097 return true;
2100 static unsigned int
2101 object_sizes_execute (function *fun, bool early)
2103 todo = 0;
2105 basic_block bb;
2106 FOR_EACH_BB_FN (bb, fun)
2108 gimple_stmt_iterator i;
2109 for (i = gsi_start_bb (bb); !gsi_end_p (i); gsi_next (&i))
2111 tree result;
2112 bool dynamic = false;
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;
2120 tree lhs = gimple_call_lhs (call);
2121 if (!lhs)
2122 continue;
2124 init_object_sizes ();
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)
2134 early_object_sizes_execute_one (&i, call);
2135 continue;
2138 if (dynamic)
2140 if (dynamic_object_sizes_execute_one (&i, call))
2141 continue;
2142 else
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 (BUILT_IN_OBJECT_SIZE);
2148 gimple_call_set_fndecl (call, bosfn);
2149 update_stmt (call);
2151 if (dump_file && (dump_flags & TDF_DETAILS))
2153 print_generic_expr (dump_file, gimple_call_arg (call, 0),
2154 dump_flags);
2155 fprintf (dump_file,
2156 ": Retrying as __builtin_object_size\n");
2161 result = gimple_fold_stmt_to_constant (call, do_valueize);
2162 if (!result)
2164 tree ost = gimple_call_arg (call, 1);
2166 if (tree_fits_uhwi_p (ost))
2168 unsigned HOST_WIDE_INT object_size_type = tree_to_uhwi (ost);
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);
2177 if (!result)
2178 continue;
2181 gcc_assert (TREE_CODE (result) == INTEGER_CST);
2183 if (dump_file && (dump_flags & TDF_DETAILS))
2185 fprintf (dump_file, "Simplified\n ");
2186 print_gimple_stmt (dump_file, call, 0, dump_flags);
2187 fprintf (dump_file, " to ");
2188 print_generic_expr (dump_file, result);
2189 fprintf (dump_file, "\n");
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);
2200 fini_object_sizes ();
2201 return todo;
2204 /* Simple pass to optimize all __builtin_object_size () builtins. */
2206 namespace {
2208 const pass_data pass_data_object_sizes =
2210 GIMPLE_PASS, /* type */
2211 "objsz", /* name */
2212 OPTGROUP_NONE, /* optinfo_flags */
2213 TV_NONE, /* tv_id */
2214 ( PROP_cfg | PROP_ssa ), /* properties_required */
2215 PROP_objsz, /* properties_provided */
2216 0, /* properties_destroyed */
2217 0, /* todo_flags_start */
2218 0, /* todo_flags_finish */
2221 class pass_object_sizes : public gimple_opt_pass
2223 public:
2224 pass_object_sizes (gcc::context *ctxt)
2225 : gimple_opt_pass (pass_data_object_sizes, ctxt)
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
2232 return object_sizes_execute (fun, false);
2234 }; // class pass_object_sizes
2236 } // anon namespace
2238 gimple_opt_pass *
2239 make_pass_object_sizes (gcc::context *ctxt)
2241 return new pass_object_sizes (ctxt);
2244 /* Early version of pass to optimize all __builtin_object_size () builtins. */
2246 namespace {
2248 const pass_data pass_data_early_object_sizes =
2250 GIMPLE_PASS, /* type */
2251 "early_objsz", /* name */
2252 OPTGROUP_NONE, /* optinfo_flags */
2253 TV_NONE, /* tv_id */
2254 ( PROP_cfg | PROP_ssa ), /* properties_required */
2255 0, /* properties_provided */
2256 0, /* properties_destroyed */
2257 0, /* todo_flags_start */
2258 0, /* todo_flags_finish */
2261 class pass_early_object_sizes : public gimple_opt_pass
2263 public:
2264 pass_early_object_sizes (gcc::context *ctxt)
2265 : gimple_opt_pass (pass_data_early_object_sizes, ctxt)
2268 /* opt_pass methods: */
2269 unsigned int execute (function *fun) final override
2271 return object_sizes_execute (fun, true);
2273 }; // class pass_object_sizes
2275 } // anon namespace
2277 gimple_opt_pass *
2278 make_pass_early_object_sizes (gcc::context *ctxt)
2280 return new pass_early_object_sizes (ctxt);