RISC-V: Make stack_save_restore tests more robust
[official-gcc.git] / gcc / tree-object-size.cc
bloba62af050056315fd63802e0f77e72f4eff52dbcd
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 explicit access attribute with a usable size argument... */
1579 if (access && access->sizarg != UINT_MAX && !access->internal_p
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. */
1591 for (arg = fnargs; arg; arg = TREE_CHAIN (arg), ++argpos)
1592 if (argpos == access->sizarg && INTEGRAL_TYPE_P (TREE_TYPE (arg)))
1594 sz = get_or_create_ssa_default_def (cfun, arg);
1595 if (sz != NULL_TREE)
1597 sz = fold_convert (sizetype, sz);
1598 if (typesize)
1599 sz = size_binop (MULT_EXPR, sz, typesize);
1601 break;
1604 if (!sz)
1605 sz = size_unknown (object_size_type);
1607 object_sizes_set (osi, SSA_NAME_VERSION (var), sz, sz);
1610 /* Compute an object size expression for VAR, which is the result of a PHI
1611 node. */
1613 static void
1614 phi_dynamic_object_size (struct object_size_info *osi, tree var)
1616 int object_size_type = osi->object_size_type;
1617 unsigned int varno = SSA_NAME_VERSION (var);
1618 gimple *stmt = SSA_NAME_DEF_STMT (var);
1619 unsigned i, num_args = gimple_phi_num_args (stmt);
1620 bool wholesize_needed = false;
1622 /* The extra space is for the PHI result at the end, which object_sizes_set
1623 sets for us. */
1624 tree sizes = make_tree_vec (num_args + 1);
1625 tree wholesizes = make_tree_vec (num_args + 1);
1627 /* Bail out if the size of any of the PHI arguments cannot be
1628 determined. */
1629 for (i = 0; i < num_args; i++)
1631 edge e = gimple_phi_arg_edge (as_a <gphi *> (stmt), i);
1632 if (e->flags & EDGE_COMPLEX)
1633 break;
1635 tree rhs = gimple_phi_arg_def (stmt, i);
1636 tree size, wholesize;
1638 dynamic_object_size (osi, rhs, &size, &wholesize);
1640 if (size_unknown_p (size, object_size_type))
1641 break;
1643 if (size != wholesize)
1644 wholesize_needed = true;
1646 TREE_VEC_ELT (sizes, i) = size;
1647 TREE_VEC_ELT (wholesizes, i) = wholesize;
1650 if (i < num_args)
1652 ggc_free (sizes);
1653 ggc_free (wholesizes);
1654 sizes = wholesizes = size_unknown (object_size_type);
1657 /* Point to the same TREE_VEC so that we can avoid emitting two PHI
1658 nodes. */
1659 else if (!wholesize_needed)
1661 ggc_free (wholesizes);
1662 wholesizes = sizes;
1665 object_sizes_set (osi, varno, sizes, wholesizes);
1668 /* Compute object sizes for VAR.
1669 For ADDR_EXPR an object size is the number of remaining bytes
1670 to the end of the object (where what is considered an object depends on
1671 OSI->object_size_type).
1672 For allocation GIMPLE_CALL like malloc or calloc object size is the size
1673 of the allocation.
1674 For POINTER_PLUS_EXPR where second operand is a constant integer,
1675 object size is object size of the first operand minus the constant.
1676 If the constant is bigger than the number of remaining bytes until the
1677 end of the object, object size is 0, but if it is instead a pointer
1678 subtraction, object size is size_unknown (object_size_type).
1679 To differentiate addition from subtraction, ADDR_EXPR returns
1680 size_unknown (object_size_type) for all objects bigger than half of the
1681 address space, and constants less than half of the address space are
1682 considered addition, while bigger constants subtraction.
1683 For a memcpy like GIMPLE_CALL that always returns one of its arguments, the
1684 object size is object size of that argument.
1685 Otherwise, object size is the maximum of object sizes of variables
1686 that it might be set to. */
1688 static void
1689 collect_object_sizes_for (struct object_size_info *osi, tree var)
1691 int object_size_type = osi->object_size_type;
1692 unsigned int varno = SSA_NAME_VERSION (var);
1693 gimple *stmt;
1694 bool reexamine;
1696 if (bitmap_bit_p (computed[object_size_type], varno))
1697 return;
1699 if (osi->pass == 0)
1701 if (bitmap_set_bit (osi->visited, varno))
1703 /* Initialize to 0 for maximum size and M1U for minimum size so that
1704 it gets immediately overridden. */
1705 object_sizes_initialize (osi, varno,
1706 size_initval (object_size_type),
1707 size_initval (object_size_type));
1709 else
1711 /* Found a dependency loop. Mark the variable for later
1712 re-examination. */
1713 if (object_size_type & OST_DYNAMIC)
1714 object_sizes_set_temp (osi, varno);
1716 bitmap_set_bit (osi->reexamine, varno);
1717 if (dump_file && (dump_flags & TDF_DETAILS))
1719 fprintf (dump_file, "Found a dependency loop at ");
1720 print_generic_expr (dump_file, var, dump_flags);
1721 fprintf (dump_file, "\n");
1723 return;
1727 if (dump_file && (dump_flags & TDF_DETAILS))
1729 fprintf (dump_file, "Visiting use-def links for ");
1730 print_generic_expr (dump_file, var, dump_flags);
1731 fprintf (dump_file, "\n");
1734 stmt = SSA_NAME_DEF_STMT (var);
1735 reexamine = false;
1737 switch (gimple_code (stmt))
1739 case GIMPLE_ASSIGN:
1741 tree rhs = gimple_assign_rhs1 (stmt);
1742 if (gimple_assign_rhs_code (stmt) == POINTER_PLUS_EXPR
1743 || (gimple_assign_rhs_code (stmt) == ADDR_EXPR
1744 && TREE_CODE (TREE_OPERAND (rhs, 0)) == MEM_REF))
1745 reexamine = plus_stmt_object_size (osi, var, stmt);
1746 else if (gimple_assign_rhs_code (stmt) == COND_EXPR)
1747 reexamine = cond_expr_object_size (osi, var, stmt);
1748 else if (gimple_assign_single_p (stmt)
1749 || gimple_assign_unary_nop_p (stmt))
1751 if (TREE_CODE (rhs) == SSA_NAME
1752 && POINTER_TYPE_P (TREE_TYPE (rhs)))
1753 reexamine = merge_object_sizes (osi, var, rhs);
1754 else
1755 expr_object_size (osi, var, rhs);
1757 else
1758 unknown_object_size (osi, var);
1759 break;
1762 case GIMPLE_CALL:
1764 gcall *call_stmt = as_a <gcall *> (stmt);
1765 tree arg = pass_through_call (call_stmt);
1766 if (arg)
1768 if (TREE_CODE (arg) == SSA_NAME
1769 && POINTER_TYPE_P (TREE_TYPE (arg)))
1770 reexamine = merge_object_sizes (osi, var, arg);
1771 else
1772 expr_object_size (osi, var, arg);
1774 else
1775 call_object_size (osi, var, call_stmt);
1776 break;
1779 case GIMPLE_ASM:
1780 /* Pointers defined by __asm__ statements can point anywhere. */
1781 unknown_object_size (osi, var);
1782 break;
1784 case GIMPLE_NOP:
1785 if (SSA_NAME_VAR (var)
1786 && TREE_CODE (SSA_NAME_VAR (var)) == PARM_DECL)
1787 parm_object_size (osi, var);
1788 else
1789 /* Uninitialized SSA names point nowhere. */
1790 unknown_object_size (osi, var);
1791 break;
1793 case GIMPLE_PHI:
1795 unsigned i;
1797 if (object_size_type & OST_DYNAMIC)
1799 phi_dynamic_object_size (osi, var);
1800 break;
1803 for (i = 0; i < gimple_phi_num_args (stmt); i++)
1805 tree rhs = gimple_phi_arg (stmt, i)->def;
1807 if (object_sizes_unknown_p (object_size_type, varno))
1808 break;
1810 if (TREE_CODE (rhs) == SSA_NAME)
1811 reexamine |= merge_object_sizes (osi, var, rhs);
1812 else if (osi->pass == 0)
1813 expr_object_size (osi, var, rhs);
1815 break;
1818 default:
1819 gcc_unreachable ();
1822 if (! reexamine || object_sizes_unknown_p (object_size_type, varno))
1824 bitmap_set_bit (computed[object_size_type], varno);
1825 if (!(object_size_type & OST_DYNAMIC))
1826 bitmap_clear_bit (osi->reexamine, varno);
1828 else
1830 bitmap_set_bit (osi->reexamine, varno);
1831 if (dump_file && (dump_flags & TDF_DETAILS))
1833 fprintf (dump_file, "Need to reexamine ");
1834 print_generic_expr (dump_file, var, dump_flags);
1835 fprintf (dump_file, "\n");
1841 /* Helper function for check_for_plus_in_loops. Called recursively
1842 to detect loops. */
1844 static void
1845 check_for_plus_in_loops_1 (struct object_size_info *osi, tree var,
1846 unsigned int depth)
1848 gimple *stmt = SSA_NAME_DEF_STMT (var);
1849 unsigned int varno = SSA_NAME_VERSION (var);
1851 if (osi->depths[varno])
1853 if (osi->depths[varno] != depth)
1855 unsigned int *sp;
1857 /* Found a loop involving pointer addition. */
1858 for (sp = osi->tos; sp > osi->stack; )
1860 --sp;
1861 bitmap_clear_bit (osi->reexamine, *sp);
1862 bitmap_set_bit (computed[osi->object_size_type], *sp);
1863 object_sizes_set (osi, *sp, size_zero_node,
1864 object_sizes_get (osi, *sp, true));
1865 if (*sp == varno)
1866 break;
1869 return;
1871 else if (! bitmap_bit_p (osi->reexamine, varno))
1872 return;
1874 osi->depths[varno] = depth;
1875 *osi->tos++ = varno;
1877 switch (gimple_code (stmt))
1880 case GIMPLE_ASSIGN:
1882 if ((gimple_assign_single_p (stmt)
1883 || gimple_assign_unary_nop_p (stmt))
1884 && TREE_CODE (gimple_assign_rhs1 (stmt)) == SSA_NAME)
1886 tree rhs = gimple_assign_rhs1 (stmt);
1888 check_for_plus_in_loops_1 (osi, rhs, depth);
1890 else if (gimple_assign_rhs_code (stmt) == POINTER_PLUS_EXPR)
1892 tree basevar = gimple_assign_rhs1 (stmt);
1893 tree cst = gimple_assign_rhs2 (stmt);
1895 gcc_assert (TREE_CODE (cst) == INTEGER_CST);
1897 check_for_plus_in_loops_1 (osi, basevar,
1898 depth + !integer_zerop (cst));
1900 else
1901 gcc_unreachable ();
1902 break;
1905 case GIMPLE_CALL:
1907 gcall *call_stmt = as_a <gcall *> (stmt);
1908 tree arg = pass_through_call (call_stmt);
1909 if (arg)
1911 if (TREE_CODE (arg) == SSA_NAME)
1912 check_for_plus_in_loops_1 (osi, arg, depth);
1913 else
1914 gcc_unreachable ();
1916 break;
1919 case GIMPLE_PHI:
1921 unsigned i;
1923 for (i = 0; i < gimple_phi_num_args (stmt); i++)
1925 tree rhs = gimple_phi_arg (stmt, i)->def;
1927 if (TREE_CODE (rhs) == SSA_NAME)
1928 check_for_plus_in_loops_1 (osi, rhs, depth);
1930 break;
1933 default:
1934 gcc_unreachable ();
1937 osi->depths[varno] = 0;
1938 osi->tos--;
1942 /* Check if some pointer we are computing object size of is being increased
1943 within a loop. If yes, assume all the SSA variables participating in
1944 that loop have minimum object sizes 0. */
1946 static void
1947 check_for_plus_in_loops (struct object_size_info *osi, tree var)
1949 gimple *stmt = SSA_NAME_DEF_STMT (var);
1951 /* NOTE: In the pre-tuples code, we handled a CALL_EXPR here,
1952 and looked for a POINTER_PLUS_EXPR in the pass-through
1953 argument, if any. In GIMPLE, however, such an expression
1954 is not a valid call operand. */
1956 if (is_gimple_assign (stmt)
1957 && gimple_assign_rhs_code (stmt) == POINTER_PLUS_EXPR)
1959 tree basevar = gimple_assign_rhs1 (stmt);
1960 tree cst = gimple_assign_rhs2 (stmt);
1962 gcc_assert (TREE_CODE (cst) == INTEGER_CST);
1964 /* Skip non-positive offsets. */
1965 if (integer_zerop (cst) || compare_tree_int (cst, offset_limit) > 0)
1966 return;
1968 osi->depths[SSA_NAME_VERSION (basevar)] = 1;
1969 *osi->tos++ = SSA_NAME_VERSION (basevar);
1970 check_for_plus_in_loops_1 (osi, var, 2);
1971 osi->depths[SSA_NAME_VERSION (basevar)] = 0;
1972 osi->tos--;
1977 /* Initialize data structures for the object size computation. */
1979 void
1980 init_object_sizes (void)
1982 int object_size_type;
1984 if (computed[0])
1985 return;
1987 for (object_size_type = 0; object_size_type < OST_END; object_size_type++)
1989 object_sizes_grow (object_size_type);
1990 computed[object_size_type] = BITMAP_ALLOC (NULL);
1993 init_offset_limit ();
1997 /* Destroy data structures after the object size computation. */
1999 void
2000 fini_object_sizes (void)
2002 int object_size_type;
2004 for (object_size_type = 0; object_size_type < OST_END; object_size_type++)
2006 object_sizes_release (object_size_type);
2007 BITMAP_FREE (computed[object_size_type]);
2011 /* Dummy valueize function. */
2013 static tree
2014 do_valueize (tree t)
2016 return t;
2019 /* Process a __builtin_object_size or __builtin_dynamic_object_size call in
2020 CALL early for subobjects before any object information is lost due to
2021 optimization. Insert a MIN or MAX expression of the result and
2022 __builtin_object_size at I so that it may be processed in the second pass.
2023 __builtin_dynamic_object_size is treated like __builtin_object_size here
2024 since we're only looking for constant bounds. */
2026 static void
2027 early_object_sizes_execute_one (gimple_stmt_iterator *i, gimple *call)
2029 tree ost = gimple_call_arg (call, 1);
2030 tree lhs = gimple_call_lhs (call);
2031 gcc_assert (lhs != NULL_TREE);
2033 if (!tree_fits_uhwi_p (ost))
2034 return;
2036 unsigned HOST_WIDE_INT object_size_type = tree_to_uhwi (ost);
2037 tree ptr = gimple_call_arg (call, 0);
2039 if (object_size_type != 1 && object_size_type != 3)
2040 return;
2042 if (TREE_CODE (ptr) != ADDR_EXPR && TREE_CODE (ptr) != SSA_NAME)
2043 return;
2045 tree type = TREE_TYPE (lhs);
2046 tree bytes;
2047 if (!compute_builtin_object_size (ptr, object_size_type, &bytes)
2048 || !int_fits_type_p (bytes, type))
2049 return;
2051 tree tem = make_ssa_name (type);
2052 gimple_call_set_lhs (call, tem);
2053 enum tree_code code = object_size_type & OST_MINIMUM ? MAX_EXPR : MIN_EXPR;
2054 tree cst = fold_convert (type, bytes);
2055 gimple *g = gimple_build_assign (lhs, code, tem, cst);
2056 gsi_insert_after (i, g, GSI_NEW_STMT);
2057 update_stmt (call);
2060 /* Attempt to fold one __builtin_dynamic_object_size call in CALL into an
2061 expression and insert it at I. Return true if it succeeds. */
2063 static bool
2064 dynamic_object_sizes_execute_one (gimple_stmt_iterator *i, gimple *call)
2066 gcc_assert (gimple_call_num_args (call) == 2);
2068 tree args[2];
2069 args[0] = gimple_call_arg (call, 0);
2070 args[1] = gimple_call_arg (call, 1);
2072 location_t loc = EXPR_LOC_OR_LOC (args[0], input_location);
2073 tree result_type = gimple_call_return_type (as_a <gcall *> (call));
2074 tree result = fold_builtin_call_array (loc, result_type,
2075 gimple_call_fn (call), 2, args);
2077 if (!result)
2078 return false;
2080 /* fold_builtin_call_array may wrap the result inside a
2081 NOP_EXPR. */
2082 STRIP_NOPS (result);
2083 gimplify_and_update_call_from_tree (i, result);
2085 if (dump_file && (dump_flags & TDF_DETAILS))
2087 fprintf (dump_file, "Simplified (dynamic)\n ");
2088 print_gimple_stmt (dump_file, call, 0, dump_flags);
2089 fprintf (dump_file, " to ");
2090 print_generic_expr (dump_file, result);
2091 fprintf (dump_file, "\n");
2093 return true;
2096 static unsigned int
2097 object_sizes_execute (function *fun, bool early)
2099 todo = 0;
2101 basic_block bb;
2102 FOR_EACH_BB_FN (bb, fun)
2104 gimple_stmt_iterator i;
2105 for (i = gsi_start_bb (bb); !gsi_end_p (i); gsi_next (&i))
2107 tree result;
2108 bool dynamic = false;
2110 gimple *call = gsi_stmt (i);
2111 if (gimple_call_builtin_p (call, BUILT_IN_DYNAMIC_OBJECT_SIZE))
2112 dynamic = true;
2113 else if (!gimple_call_builtin_p (call, BUILT_IN_OBJECT_SIZE))
2114 continue;
2116 tree lhs = gimple_call_lhs (call);
2117 if (!lhs)
2118 continue;
2120 init_object_sizes ();
2122 /* If early, only attempt to fold
2123 __builtin_object_size (x, 1) and __builtin_object_size (x, 3),
2124 and rather than folding the builtin to the constant if any,
2125 create a MIN_EXPR or MAX_EXPR of the __builtin_object_size
2126 call result and the computed constant. Do the same for
2127 __builtin_dynamic_object_size too. */
2128 if (early)
2130 early_object_sizes_execute_one (&i, call);
2131 continue;
2134 if (dynamic)
2136 if (dynamic_object_sizes_execute_one (&i, call))
2137 continue;
2138 else
2140 /* If we could not find a suitable size expression, lower to
2141 __builtin_object_size so that we may at least get a
2142 constant lower or higher estimate. */
2143 tree bosfn = builtin_decl_implicit (BUILT_IN_OBJECT_SIZE);
2144 gimple_call_set_fndecl (call, bosfn);
2145 update_stmt (call);
2147 if (dump_file && (dump_flags & TDF_DETAILS))
2149 print_generic_expr (dump_file, gimple_call_arg (call, 0),
2150 dump_flags);
2151 fprintf (dump_file,
2152 ": Retrying as __builtin_object_size\n");
2157 result = gimple_fold_stmt_to_constant (call, do_valueize);
2158 if (!result)
2160 tree ost = gimple_call_arg (call, 1);
2162 if (tree_fits_uhwi_p (ost))
2164 unsigned HOST_WIDE_INT object_size_type = tree_to_uhwi (ost);
2166 if (object_size_type & OST_MINIMUM)
2167 result = build_zero_cst (size_type_node);
2168 else if (object_size_type < OST_END)
2169 result = fold_convert (size_type_node,
2170 integer_minus_one_node);
2173 if (!result)
2174 continue;
2177 gcc_assert (TREE_CODE (result) == INTEGER_CST);
2179 if (dump_file && (dump_flags & TDF_DETAILS))
2181 fprintf (dump_file, "Simplified\n ");
2182 print_gimple_stmt (dump_file, call, 0, dump_flags);
2183 fprintf (dump_file, " to ");
2184 print_generic_expr (dump_file, result);
2185 fprintf (dump_file, "\n");
2188 /* Propagate into all uses and fold those stmts. */
2189 if (!SSA_NAME_OCCURS_IN_ABNORMAL_PHI (lhs))
2190 replace_uses_by (lhs, result);
2191 else
2192 replace_call_with_value (&i, result);
2196 fini_object_sizes ();
2197 return todo;
2200 /* Simple pass to optimize all __builtin_object_size () builtins. */
2202 namespace {
2204 const pass_data pass_data_object_sizes =
2206 GIMPLE_PASS, /* type */
2207 "objsz", /* name */
2208 OPTGROUP_NONE, /* optinfo_flags */
2209 TV_NONE, /* tv_id */
2210 ( PROP_cfg | PROP_ssa ), /* properties_required */
2211 PROP_objsz, /* properties_provided */
2212 0, /* properties_destroyed */
2213 0, /* todo_flags_start */
2214 0, /* todo_flags_finish */
2217 class pass_object_sizes : public gimple_opt_pass
2219 public:
2220 pass_object_sizes (gcc::context *ctxt)
2221 : gimple_opt_pass (pass_data_object_sizes, ctxt)
2224 /* opt_pass methods: */
2225 opt_pass * clone () final override { return new pass_object_sizes (m_ctxt); }
2226 unsigned int execute (function *fun) final override
2228 return object_sizes_execute (fun, false);
2230 }; // class pass_object_sizes
2232 } // anon namespace
2234 gimple_opt_pass *
2235 make_pass_object_sizes (gcc::context *ctxt)
2237 return new pass_object_sizes (ctxt);
2240 /* Early version of pass to optimize all __builtin_object_size () builtins. */
2242 namespace {
2244 const pass_data pass_data_early_object_sizes =
2246 GIMPLE_PASS, /* type */
2247 "early_objsz", /* name */
2248 OPTGROUP_NONE, /* optinfo_flags */
2249 TV_NONE, /* tv_id */
2250 ( PROP_cfg | PROP_ssa ), /* properties_required */
2251 0, /* properties_provided */
2252 0, /* properties_destroyed */
2253 0, /* todo_flags_start */
2254 0, /* todo_flags_finish */
2257 class pass_early_object_sizes : public gimple_opt_pass
2259 public:
2260 pass_early_object_sizes (gcc::context *ctxt)
2261 : gimple_opt_pass (pass_data_early_object_sizes, ctxt)
2264 /* opt_pass methods: */
2265 unsigned int execute (function *fun) final override
2267 return object_sizes_execute (fun, true);
2269 }; // class pass_object_sizes
2271 } // anon namespace
2273 gimple_opt_pass *
2274 make_pass_early_object_sizes (gcc::context *ctxt)
2276 return new pass_early_object_sizes (ctxt);