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)
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/>. */
23 #include "coretypes.h"
27 #include "tree-pass.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"
36 #include "stringpool.h"
39 #include "gimplify-me.h"
41 struct object_size_info
46 bitmap visited
, reexamine
, unknowns
;
48 unsigned int *stack
, *tos
;
51 struct GTY(()) object_size
53 /* Estimate of bytes till the end of the object. */
55 /* Estimate of the size of the whole object. */
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
,
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
96 /* Return true if VAL represents an initial size for OBJECT_SIZE_TYPE. */
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. */
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. */
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
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. */
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. */
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. */
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]. */
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. */
169 object_sizes_unknown_p (int object_size_type
, unsigned varno
)
171 return size_unknown_p (object_sizes
[object_size_type
][varno
].size
,
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
190 object_sizes_get (struct object_size_info
*osi
, unsigned varno
,
194 int object_size_type
= osi
->object_size_type
;
197 ret
= object_sizes
[object_size_type
][varno
].wholesize
;
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);
208 gcc_checking_assert (size_usable_p (ret
));
214 /* Set size for VARNO corresponding to OSI to VAL. */
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. */
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
;
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
253 object_sizes_set (struct object_size_info
*osi
, unsigned varno
, tree val
,
256 int object_size_type
= osi
->object_size_type
;
257 object_size osize
= object_sizes
[object_size_type
][varno
];
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
);
277 val
= bundle_sizes (oldval
, val
);
278 wholeval
= bundle_sizes (old_wholeval
, wholeval
);
283 gcc_checking_assert (size_initval_p (oldval
, object_size_type
));
284 gcc_checking_assert (size_initval_p (old_wholeval
,
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
);
301 /* If the new value is a temporary variable, mark it for
303 else if (TREE_CODE (val
) == SSA_NAME
&& !SSA_NAME_DEF_STMT (val
))
304 bitmap_set_bit (osi
->reexamine
, varno
);
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
;
324 /* Set temporary SSA names for object size and whole size to resolve dependency
325 loops in dynamic size computation. */
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. */
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
));
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. */
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
),
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
);
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
))
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
399 compute_object_offset (tree expr
, const_tree var
)
401 enum tree_code code
= PLUS_EXPR
;
405 return size_zero_node
;
407 switch (TREE_CODE (expr
))
410 base
= compute_object_offset (TREE_OPERAND (expr
, 0), var
);
411 if (base
== error_mark_node
)
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
))
423 case VIEW_CONVERT_EXPR
:
424 case NON_LVALUE_EXPR
:
425 return compute_object_offset (TREE_OPERAND (expr
, 0), var
);
428 base
= compute_object_offset (TREE_OPERAND (expr
, 0), var
);
429 if (base
== error_mark_node
)
432 off
= TYPE_SIZE_UNIT (TREE_TYPE (expr
));
436 base
= compute_object_offset (TREE_OPERAND (expr
, 0), var
);
437 if (base
== error_mark_node
)
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)
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
);
456 gcc_assert (TREE_CODE (TREE_OPERAND (expr
, 0)) == ADDR_EXPR
);
457 return wide_int_to_tree (sizetype
, mem_ref_offset (expr
));
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. */
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
)
480 tree last
= last_field (type
);
484 tree last_type
= TREE_TYPE (last
);
485 if (TREE_CODE (last_type
) != ARRAY_TYPE
486 || TYPE_SIZE (last_type
))
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
);
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
);
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). */
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
);
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);
529 if (TREE_CODE (pt_var
) == MEM_REF
)
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
);
542 tree var
= TREE_OPERAND (pt_var
, 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);
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))
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
);
572 else if (TREE_CODE (pt_var
) == STRING_CST
)
573 pt_var_size
= pt_var_wholesize
= TYPE_SIZE_UNIT (TREE_TYPE (pt_var
));
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)
585 if (pt_var
!= TREE_OPERAND (ptr
, 0))
589 if (object_size_type
& OST_SUBOBJECT
)
591 var
= TREE_OPERAND (ptr
, 0);
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
)))))
609 else if (var
!= pt_var
&& TREE_CODE (pt_var
) == MEM_REF
)
612 /* For &X->fld, compute object size if fld isn't a flexible array
614 bool is_flexible_array_mem_ref
= false;
615 while (v
&& v
!= pt_var
)
616 switch (TREE_CODE (v
))
619 if (TYPE_SIZE_UNIT (TREE_TYPE (TREE_OPERAND (v
, 0))))
622 = TYPE_DOMAIN (TREE_TYPE (TREE_OPERAND (v
, 0)));
623 if (domain
&& TYPE_MAX_VALUE (domain
))
629 v
= TREE_OPERAND (v
, 0);
636 if (TREE_CODE (TREE_TYPE (v
)) != ARRAY_TYPE
)
641 is_flexible_array_mem_ref
= array_ref_flexible_size_p (v
);
642 while (v
!= pt_var
&& TREE_CODE (v
) == COMPONENT_REF
)
643 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (v
, 0)))
645 && TREE_CODE (TREE_TYPE (TREE_OPERAND (v
, 0)))
649 v
= TREE_OPERAND (v
, 0);
650 if (TREE_CODE (v
) == COMPONENT_REF
651 && TREE_CODE (TREE_TYPE (TREE_OPERAND (v
, 0)))
654 /* compute object size only if v is not a
655 flexible array member. */
656 if (!is_flexible_array_mem_ref
)
661 v
= TREE_OPERAND (v
, 0);
663 while (v
!= pt_var
&& TREE_CODE (v
) == COMPONENT_REF
)
664 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (v
, 0)))
666 && TREE_CODE (TREE_TYPE (TREE_OPERAND (v
, 0)))
670 v
= TREE_OPERAND (v
, 0);
689 var_size
= TYPE_SIZE_UNIT (TREE_TYPE (var
));
690 if (!TREE_CONSTANT (var_size
))
691 var_size
= get_or_create_ssa_default_def (cfun
, var_size
);
695 else if (!pt_var_size
)
698 var_size
= pt_var_size
;
699 bytes
= compute_object_offset (TREE_OPERAND (ptr
, 0), var
);
700 if (bytes
!= error_mark_node
)
702 bytes
= size_for_offset (var_size
, bytes
);
703 if (var
!= pt_var
&& pt_var_size
&& TREE_CODE (pt_var
) == MEM_REF
)
705 tree bytes2
= compute_object_offset (TREE_OPERAND (ptr
, 0),
707 if (bytes2
!= error_mark_node
)
709 bytes2
= size_for_offset (pt_var_size
, bytes2
);
710 bytes
= size_binop (MIN_EXPR
, bytes
, bytes2
);
715 bytes
= size_unknown (object_size_type
);
718 = object_size_type
& OST_SUBOBJECT
? var_size
: pt_var_wholesize
;
720 else if (!pt_var_size
)
725 wholebytes
= pt_var_wholesize
;
728 if (!size_unknown_p (bytes
, object_size_type
)
729 && size_valid_p (bytes
, object_size_type
)
730 && !size_unknown_p (bytes
, object_size_type
)
731 && size_valid_p (wholebytes
, object_size_type
))
735 *pwholesize
= wholebytes
;
743 /* Compute __builtin_object_size for CALL, which is a GIMPLE_CALL.
744 Handles calls to functions declared with attribute alloc_size.
745 OBJECT_SIZE_TYPE is the second argument from __builtin_object_size.
746 If unknown, return size_unknown (object_size_type). */
749 alloc_object_size (const gcall
*call
, int object_size_type
)
751 gcc_assert (is_gimple_call (call
));
754 tree callfn
= gimple_call_fndecl (call
);
756 calltype
= TREE_TYPE (callfn
);
758 calltype
= gimple_call_fntype (call
);
761 return size_unknown (object_size_type
);
763 /* Set to positions of alloc_size arguments. */
764 int arg1
= -1, arg2
= -1;
765 tree alloc_size
= lookup_attribute ("alloc_size",
766 TYPE_ATTRIBUTES (calltype
));
767 if (alloc_size
&& TREE_VALUE (alloc_size
))
769 tree p
= TREE_VALUE (alloc_size
);
771 arg1
= TREE_INT_CST_LOW (TREE_VALUE (p
))-1;
773 arg2
= TREE_INT_CST_LOW (TREE_VALUE (TREE_CHAIN (p
)))-1;
775 else if (gimple_call_builtin_p (call
, BUILT_IN_NORMAL
)
776 && callfn
&& ALLOCA_FUNCTION_CODE_P (DECL_FUNCTION_CODE (callfn
)))
779 /* Non-const arguments are OK here, let the caller handle constness. */
780 if (arg1
< 0 || arg1
>= (int) gimple_call_num_args (call
)
781 || arg2
>= (int) gimple_call_num_args (call
))
782 return size_unknown (object_size_type
);
784 tree bytes
= NULL_TREE
;
786 bytes
= size_binop (MULT_EXPR
,
787 fold_convert (sizetype
, gimple_call_arg (call
, arg1
)),
788 fold_convert (sizetype
, gimple_call_arg (call
, arg2
)));
790 bytes
= fold_convert (sizetype
, gimple_call_arg (call
, arg1
));
792 return bytes
? bytes
: size_unknown (object_size_type
);
795 /* Compute __builtin_object_size for CALL, which is a call to either
796 BUILT_IN_STRDUP or BUILT_IN_STRNDUP; IS_STRNDUP indicates which it is.
797 OBJECT_SIZE_TYPE is the second argument from __builtin_object_size.
798 If unknown, return size_unknown (object_size_type). */
801 strdup_object_size (const gcall
*call
, int object_size_type
, bool is_strndup
)
803 tree src
= gimple_call_arg (call
, 0);
804 tree sz
= size_unknown (object_size_type
);
808 n
= fold_build2 (PLUS_EXPR
, sizetype
, size_one_node
,
809 gimple_call_arg (call
, 1));
810 /* For strdup, simply emit strlen (SRC) + 1 and let the optimizer fold it the
814 tree strlen_fn
= builtin_decl_implicit (BUILT_IN_STRLEN
);
817 sz
= fold_build2 (PLUS_EXPR
, sizetype
, size_one_node
,
818 build_call_expr (strlen_fn
, 1, src
));
819 todo
= TODO_update_ssa_only_virtuals
;
823 /* In all other cases, return the size of SRC since the object size cannot
824 exceed that. We cannot do this for OST_MINIMUM unless SRC points into a
825 string constant since otherwise the object size could go all the way down
827 if (!size_valid_p (sz
, object_size_type
)
828 || size_unknown_p (sz
, object_size_type
))
830 tree wholesrc
= NULL_TREE
;
831 if (TREE_CODE (src
) == ADDR_EXPR
)
832 wholesrc
= get_base_address (TREE_OPERAND (src
, 0));
834 /* If the source points within a string constant, we try to get its
836 if (wholesrc
&& TREE_CODE (wholesrc
) == STRING_CST
)
838 tree len
= c_strlen (src
, 0);
840 sz
= fold_build2 (PLUS_EXPR
, sizetype
, size_one_node
, len
);
843 /* For maximum estimate, our next best guess is the object size of the
845 if (size_unknown_p (sz
, object_size_type
)
846 && !(object_size_type
& OST_MINIMUM
))
847 compute_builtin_object_size (src
, object_size_type
, &sz
);
850 /* String duplication allocates at least one byte, so we should never fail
852 if ((!size_valid_p (sz
, object_size_type
)
853 || size_unknown_p (sz
, object_size_type
))
854 && (object_size_type
& OST_MINIMUM
))
857 /* Factor in the N. */
858 return n
? fold_build2 (MIN_EXPR
, sizetype
, n
, sz
) : sz
;
861 /* If object size is propagated from one of function's arguments directly
862 to its return value, return that argument for GIMPLE_CALL statement CALL.
863 Otherwise return NULL. */
866 pass_through_call (const gcall
*call
)
868 unsigned rf
= gimple_call_return_flags (call
);
869 if (rf
& ERF_RETURNS_ARG
)
871 unsigned argnum
= rf
& ERF_RETURN_ARG_MASK
;
872 if (argnum
< gimple_call_num_args (call
))
873 return gimple_call_arg (call
, argnum
);
876 /* __builtin_assume_aligned is intentionally not marked RET1. */
877 if (gimple_call_builtin_p (call
, BUILT_IN_ASSUME_ALIGNED
))
878 return gimple_call_arg (call
, 0);
883 /* Emit PHI nodes for size expressions fo. */
886 emit_phi_nodes (gimple
*stmt
, tree size
, tree wholesize
)
889 gphi
*wholephi
= NULL
;
891 if (wholesize
!= size
)
893 phires
= TREE_VEC_ELT (wholesize
, TREE_VEC_LENGTH (wholesize
) - 1);
894 wholephi
= create_phi_node (phires
, gimple_bb (stmt
));
897 phires
= TREE_VEC_ELT (size
, TREE_VEC_LENGTH (size
) - 1);
898 gphi
*phi
= create_phi_node (phires
, gimple_bb (stmt
));
899 gphi
*obj_phi
= as_a
<gphi
*> (stmt
);
901 gcc_checking_assert (TREE_CODE (wholesize
) == TREE_VEC
);
902 gcc_checking_assert (TREE_CODE (size
) == TREE_VEC
);
904 for (unsigned i
= 0; i
< gimple_phi_num_args (stmt
); i
++)
906 gimple_seq seq
= NULL
;
907 tree wsz
= TREE_VEC_ELT (wholesize
, i
);
908 tree sz
= TREE_VEC_ELT (size
, i
);
910 /* If we built an expression, we will need to build statements
911 and insert them on the edge right away. */
912 if (TREE_CODE (wsz
) != SSA_NAME
)
913 wsz
= force_gimple_operand (wsz
, &seq
, true, NULL
);
914 if (TREE_CODE (sz
) != SSA_NAME
)
917 sz
= force_gimple_operand (sz
, &s
, true, NULL
);
918 gimple_seq_add_seq (&seq
, s
);
922 gsi_insert_seq_on_edge (gimple_phi_arg_edge (obj_phi
, i
), seq
);
925 add_phi_arg (wholephi
, wsz
,
926 gimple_phi_arg_edge (obj_phi
, i
),
927 gimple_phi_arg_location (obj_phi
, i
));
929 add_phi_arg (phi
, sz
,
930 gimple_phi_arg_edge (obj_phi
, i
),
931 gimple_phi_arg_location (obj_phi
, i
));
935 /* Descend through EXPR and return size_unknown if it uses any SSA variable
936 object_size_set or object_size_set_temp generated, which turned out to be
937 size_unknown, as noted in UNKNOWNS. */
940 propagate_unknowns (object_size_info
*osi
, tree expr
)
942 int object_size_type
= osi
->object_size_type
;
944 switch (TREE_CODE (expr
))
947 if (bitmap_bit_p (osi
->unknowns
, SSA_NAME_VERSION (expr
)))
948 return size_unknown (object_size_type
);
954 tree res
= propagate_unknowns (osi
, TREE_OPERAND (expr
, 0));
955 if (size_unknown_p (res
, object_size_type
))
958 res
= propagate_unknowns (osi
, TREE_OPERAND (expr
, 1));
959 if (size_unknown_p (res
, object_size_type
))
966 tree res
= propagate_unknowns (osi
, TREE_OPERAND (expr
, 1));
967 if (size_unknown_p (res
, object_size_type
))
972 for (int i
= 0; i
< TREE_VEC_LENGTH (expr
); i
++)
974 tree res
= propagate_unknowns (osi
, TREE_VEC_ELT (expr
, i
));
975 if (size_unknown_p (res
, object_size_type
))
982 tree res
= propagate_unknowns (osi
, TREE_OPERAND (expr
, 0));
983 if (size_unknown_p (res
, object_size_type
))
993 /* Walk through size expressions that need reexamination and generate
994 statements for them. */
997 gimplify_size_expressions (object_size_info
*osi
)
999 int object_size_type
= osi
->object_size_type
;
1004 /* Step 1: Propagate unknowns into expressions. */
1005 bitmap reexamine
= BITMAP_ALLOC (NULL
);
1006 bitmap_copy (reexamine
, osi
->reexamine
);
1010 EXECUTE_IF_SET_IN_BITMAP (reexamine
, 0, i
, bi
)
1012 object_size cur
= object_sizes_get_raw (osi
, i
);
1014 if (size_unknown_p (propagate_unknowns (osi
, cur
.size
),
1016 || size_unknown_p (propagate_unknowns (osi
, cur
.wholesize
),
1019 object_sizes_set (osi
, i
,
1020 size_unknown (object_size_type
),
1021 size_unknown (object_size_type
));
1025 bitmap_copy (reexamine
, osi
->reexamine
);
1029 /* Release all unknowns. */
1030 EXECUTE_IF_SET_IN_BITMAP (osi
->unknowns
, 0, i
, bi
)
1031 release_ssa_name (ssa_name (i
));
1033 /* Expand all size expressions to put their definitions close to the objects
1034 for which size is being computed. */
1035 EXECUTE_IF_SET_IN_BITMAP (osi
->reexamine
, 0, i
, bi
)
1037 gimple_seq seq
= NULL
;
1038 object_size osize
= object_sizes_get_raw (osi
, i
);
1040 gimple
*stmt
= SSA_NAME_DEF_STMT (ssa_name (i
));
1041 enum gimple_code code
= gimple_code (stmt
);
1043 /* PHI nodes need special attention. */
1044 if (code
== GIMPLE_PHI
)
1045 emit_phi_nodes (stmt
, osize
.size
, osize
.wholesize
);
1048 tree size_expr
= NULL_TREE
;
1050 /* Bundle wholesize in with the size to gimplify if needed. */
1051 if (osize
.wholesize
!= osize
.size
1052 && !size_usable_p (osize
.wholesize
))
1053 size_expr
= size_binop (COMPOUND_EXPR
,
1056 else if (!size_usable_p (osize
.size
))
1057 size_expr
= osize
.size
;
1061 gimple_stmt_iterator gsi
;
1062 if (code
== GIMPLE_NOP
)
1063 gsi
= gsi_start_bb (single_succ (ENTRY_BLOCK_PTR_FOR_FN (cfun
)));
1065 gsi
= gsi_for_stmt (stmt
);
1067 force_gimple_operand (size_expr
, &seq
, true, NULL
);
1068 gsi_insert_seq_before (&gsi
, seq
, GSI_CONTINUE_LINKING
);
1072 /* We're done, so replace the MODIFY_EXPRs with the SSA names. */
1073 object_sizes_initialize (osi
, i
,
1074 object_sizes_get (osi
, i
),
1075 object_sizes_get (osi
, i
, true));
1079 /* Compute __builtin_object_size value for PTR and set *PSIZE to
1080 the resulting value. If the declared object is known and PDECL
1081 is nonnull, sets *PDECL to the object's DECL. OBJECT_SIZE_TYPE
1082 is the second argument to __builtin_object_size.
1083 Returns true on success and false when the object size could not
1087 compute_builtin_object_size (tree ptr
, int object_size_type
,
1090 gcc_assert (object_size_type
>= 0 && object_size_type
< OST_END
);
1092 /* Set to unknown and overwrite just before returning if the size
1093 could be determined. */
1094 *psize
= size_unknown (object_size_type
);
1097 init_offset_limit ();
1099 if (TREE_CODE (ptr
) == ADDR_EXPR
)
1100 return addr_object_size (NULL
, ptr
, object_size_type
, psize
);
1102 if (TREE_CODE (ptr
) != SSA_NAME
1103 || !POINTER_TYPE_P (TREE_TYPE (ptr
)))
1106 if (computed
[object_size_type
] == NULL
)
1108 if (optimize
|| object_size_type
& OST_SUBOBJECT
)
1111 /* When not optimizing, rather than failing, make a small effort
1112 to determine the object size without the full benefit of
1113 the (costly) computation below. */
1114 gimple
*def
= SSA_NAME_DEF_STMT (ptr
);
1115 if (gimple_code (def
) == GIMPLE_ASSIGN
)
1117 tree_code code
= gimple_assign_rhs_code (def
);
1118 if (code
== POINTER_PLUS_EXPR
)
1120 tree offset
= gimple_assign_rhs2 (def
);
1121 ptr
= gimple_assign_rhs1 (def
);
1123 if (((object_size_type
& OST_DYNAMIC
)
1124 || (tree_fits_shwi_p (offset
)
1125 && compare_tree_int (offset
, offset_limit
) <= 0))
1126 && compute_builtin_object_size (ptr
, object_size_type
,
1129 *psize
= size_for_offset (*psize
, offset
);
1137 struct object_size_info osi
;
1138 osi
.object_size_type
= object_size_type
;
1139 if (!bitmap_bit_p (computed
[object_size_type
], SSA_NAME_VERSION (ptr
)))
1144 object_sizes_grow (object_size_type
);
1147 fprintf (dump_file
, "Computing %s %s%sobject size for ",
1148 (object_size_type
& OST_MINIMUM
) ? "minimum" : "maximum",
1149 (object_size_type
& OST_DYNAMIC
) ? "dynamic " : "",
1150 (object_size_type
& OST_SUBOBJECT
) ? "sub" : "");
1151 print_generic_expr (dump_file
, ptr
, dump_flags
);
1152 fprintf (dump_file
, ":\n");
1155 osi
.visited
= BITMAP_ALLOC (NULL
);
1156 osi
.reexamine
= BITMAP_ALLOC (NULL
);
1158 if (object_size_type
& OST_DYNAMIC
)
1159 osi
.unknowns
= BITMAP_ALLOC (NULL
);
1167 /* First pass: walk UD chains, compute object sizes that
1168 can be computed. osi.reexamine bitmap at the end will
1169 contain what variables were found in dependency cycles
1170 and therefore need to be reexamined. */
1172 osi
.changed
= false;
1173 collect_object_sizes_for (&osi
, ptr
);
1175 if (object_size_type
& OST_DYNAMIC
)
1178 gimplify_size_expressions (&osi
);
1179 BITMAP_FREE (osi
.unknowns
);
1180 bitmap_clear (osi
.reexamine
);
1183 /* Second pass: keep recomputing object sizes of variables
1184 that need reexamination, until no object sizes are
1185 increased or all object sizes are computed. */
1186 if (! bitmap_empty_p (osi
.reexamine
))
1188 bitmap reexamine
= BITMAP_ALLOC (NULL
);
1190 /* If looking for minimum instead of maximum object size,
1191 detect cases where a pointer is increased in a loop.
1192 Although even without this detection pass 2 would eventually
1193 terminate, it could take a long time. If a pointer is
1194 increasing this way, we need to assume 0 object size.
1195 E.g. p = &buf[0]; while (cond) p = p + 4; */
1196 if (object_size_type
& OST_MINIMUM
)
1198 osi
.depths
= XCNEWVEC (unsigned int, num_ssa_names
);
1199 osi
.stack
= XNEWVEC (unsigned int, num_ssa_names
);
1200 osi
.tos
= osi
.stack
;
1202 /* collect_object_sizes_for is changing
1203 osi.reexamine bitmap, so iterate over a copy. */
1204 bitmap_copy (reexamine
, osi
.reexamine
);
1205 EXECUTE_IF_SET_IN_BITMAP (reexamine
, 0, i
, bi
)
1206 if (bitmap_bit_p (osi
.reexamine
, i
))
1207 check_for_plus_in_loops (&osi
, ssa_name (i
));
1219 osi
.changed
= false;
1220 /* collect_object_sizes_for is changing
1221 osi.reexamine bitmap, so iterate over a copy. */
1222 bitmap_copy (reexamine
, osi
.reexamine
);
1223 EXECUTE_IF_SET_IN_BITMAP (reexamine
, 0, i
, bi
)
1224 if (bitmap_bit_p (osi
.reexamine
, i
))
1226 collect_object_sizes_for (&osi
, ssa_name (i
));
1227 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1229 fprintf (dump_file
, "Reexamining ");
1230 print_generic_expr (dump_file
, ssa_name (i
),
1232 fprintf (dump_file
, "\n");
1236 while (osi
.changed
);
1238 BITMAP_FREE (reexamine
);
1240 EXECUTE_IF_SET_IN_BITMAP (osi
.reexamine
, 0, i
, bi
)
1241 bitmap_set_bit (computed
[object_size_type
], i
);
1243 /* Debugging dumps. */
1246 EXECUTE_IF_SET_IN_BITMAP (osi
.visited
, 0, i
, bi
)
1247 if (!object_sizes_unknown_p (object_size_type
, i
))
1249 print_generic_expr (dump_file
, ssa_name (i
),
1252 ": %s %s%sobject size ",
1253 ((object_size_type
& OST_MINIMUM
) ? "minimum"
1255 (object_size_type
& OST_DYNAMIC
) ? "dynamic " : "",
1256 (object_size_type
& OST_SUBOBJECT
) ? "sub" : "");
1257 print_generic_expr (dump_file
, object_sizes_get (&osi
, i
),
1259 fprintf (dump_file
, "\n");
1263 BITMAP_FREE (osi
.reexamine
);
1264 BITMAP_FREE (osi
.visited
);
1267 *psize
= object_sizes_get (&osi
, SSA_NAME_VERSION (ptr
));
1268 return !size_unknown_p (*psize
, object_size_type
);
1271 /* Compute object_sizes for PTR, defined to VALUE, which is not an SSA_NAME. */
1274 expr_object_size (struct object_size_info
*osi
, tree ptr
, tree value
)
1276 int object_size_type
= osi
->object_size_type
;
1277 unsigned int varno
= SSA_NAME_VERSION (ptr
);
1278 tree bytes
, wholesize
;
1280 gcc_assert (!object_sizes_unknown_p (object_size_type
, varno
));
1281 gcc_assert (osi
->pass
== 0);
1283 if (TREE_CODE (value
) == WITH_SIZE_EXPR
)
1284 value
= TREE_OPERAND (value
, 0);
1286 /* Pointer variables should have been handled by merge_object_sizes. */
1287 gcc_assert (TREE_CODE (value
) != SSA_NAME
1288 || !POINTER_TYPE_P (TREE_TYPE (value
)));
1290 if (TREE_CODE (value
) == ADDR_EXPR
)
1291 addr_object_size (osi
, value
, object_size_type
, &bytes
, &wholesize
);
1293 bytes
= wholesize
= size_unknown (object_size_type
);
1295 object_sizes_set (osi
, varno
, bytes
, wholesize
);
1299 /* Compute object_sizes for PTR, defined to the result of a call. */
1302 call_object_size (struct object_size_info
*osi
, tree ptr
, gcall
*call
)
1304 int object_size_type
= osi
->object_size_type
;
1305 unsigned int varno
= SSA_NAME_VERSION (ptr
);
1306 tree bytes
= NULL_TREE
;
1308 gcc_assert (is_gimple_call (call
));
1310 gcc_assert (!object_sizes_unknown_p (object_size_type
, varno
));
1311 gcc_assert (osi
->pass
== 0);
1313 bool is_strdup
= gimple_call_builtin_p (call
, BUILT_IN_STRDUP
);
1314 bool is_strndup
= gimple_call_builtin_p (call
, BUILT_IN_STRNDUP
);
1315 if (is_strdup
|| is_strndup
)
1316 bytes
= strdup_object_size (call
, object_size_type
, is_strndup
);
1318 bytes
= alloc_object_size (call
, object_size_type
);
1320 if (!size_valid_p (bytes
, object_size_type
))
1321 bytes
= size_unknown (object_size_type
);
1323 object_sizes_set (osi
, varno
, bytes
, bytes
);
1327 /* Compute object_sizes for PTR, defined to an unknown value. */
1330 unknown_object_size (struct object_size_info
*osi
, tree ptr
)
1332 int object_size_type
= osi
->object_size_type
;
1333 unsigned int varno
= SSA_NAME_VERSION (ptr
);
1335 gcc_checking_assert (!object_sizes_unknown_p (object_size_type
, varno
));
1336 gcc_checking_assert (osi
->pass
== 0);
1337 tree bytes
= size_unknown (object_size_type
);
1339 object_sizes_set (osi
, varno
, bytes
, bytes
);
1343 /* Merge object sizes of ORIG + OFFSET into DEST. Return true if
1344 the object size might need reexamination later. */
1347 merge_object_sizes (struct object_size_info
*osi
, tree dest
, tree orig
)
1349 int object_size_type
= osi
->object_size_type
;
1350 unsigned int varno
= SSA_NAME_VERSION (dest
);
1351 tree orig_bytes
, wholesize
;
1353 if (object_sizes_unknown_p (object_size_type
, varno
))
1357 collect_object_sizes_for (osi
, orig
);
1359 orig_bytes
= object_sizes_get (osi
, SSA_NAME_VERSION (orig
));
1360 wholesize
= object_sizes_get (osi
, SSA_NAME_VERSION (orig
), true);
1362 if (object_sizes_set (osi
, varno
, orig_bytes
, wholesize
))
1363 osi
->changed
= true;
1365 return bitmap_bit_p (osi
->reexamine
, SSA_NAME_VERSION (orig
));
1369 /* Compute object_sizes for VAR, defined to the result of an assignment
1370 with operator POINTER_PLUS_EXPR. Return true if the object size might
1371 need reexamination later. */
1374 plus_stmt_object_size (struct object_size_info
*osi
, tree var
, gimple
*stmt
)
1376 int object_size_type
= osi
->object_size_type
;
1377 unsigned int varno
= SSA_NAME_VERSION (var
);
1378 tree bytes
, wholesize
;
1380 bool reexamine
= false;
1382 if (gimple_assign_rhs_code (stmt
) == POINTER_PLUS_EXPR
)
1384 op0
= gimple_assign_rhs1 (stmt
);
1385 op1
= gimple_assign_rhs2 (stmt
);
1387 else if (gimple_assign_rhs_code (stmt
) == ADDR_EXPR
)
1389 tree rhs
= TREE_OPERAND (gimple_assign_rhs1 (stmt
), 0);
1390 gcc_assert (TREE_CODE (rhs
) == MEM_REF
);
1391 op0
= TREE_OPERAND (rhs
, 0);
1392 op1
= TREE_OPERAND (rhs
, 1);
1397 if (object_sizes_unknown_p (object_size_type
, varno
))
1400 /* Handle PTR + OFFSET here. */
1401 if (size_valid_p (op1
, object_size_type
)
1402 && (TREE_CODE (op0
) == SSA_NAME
|| TREE_CODE (op0
) == ADDR_EXPR
))
1404 if (TREE_CODE (op0
) == SSA_NAME
)
1407 collect_object_sizes_for (osi
, op0
);
1409 bytes
= object_sizes_get (osi
, SSA_NAME_VERSION (op0
));
1410 wholesize
= object_sizes_get (osi
, SSA_NAME_VERSION (op0
), true);
1411 reexamine
= bitmap_bit_p (osi
->reexamine
, SSA_NAME_VERSION (op0
));
1415 /* op0 will be ADDR_EXPR here. We should never come here during
1417 gcc_checking_assert (osi
->pass
== 0);
1418 addr_object_size (osi
, op0
, object_size_type
, &bytes
, &wholesize
);
1421 /* size_for_offset doesn't make sense for -1 size, but it does for size 0
1422 since the wholesize could be non-zero and a negative offset could give
1424 if (size_unknown_p (bytes
, 0))
1426 else if ((object_size_type
& OST_DYNAMIC
)
1427 || compare_tree_int (op1
, offset_limit
) <= 0)
1428 bytes
= size_for_offset (bytes
, op1
, wholesize
);
1429 /* In the static case, with a negative offset, the best estimate for
1430 minimum size is size_unknown but for maximum size, the wholesize is a
1431 better estimate than size_unknown. */
1432 else if (object_size_type
& OST_MINIMUM
)
1433 bytes
= size_unknown (object_size_type
);
1438 bytes
= wholesize
= size_unknown (object_size_type
);
1440 if (!size_valid_p (bytes
, object_size_type
)
1441 || !size_valid_p (wholesize
, object_size_type
))
1442 bytes
= wholesize
= size_unknown (object_size_type
);
1444 if (object_sizes_set (osi
, varno
, bytes
, wholesize
))
1445 osi
->changed
= true;
1449 /* Compute the dynamic object size for VAR. Return the result in SIZE and
1453 dynamic_object_size (struct object_size_info
*osi
, tree var
,
1454 tree
*size
, tree
*wholesize
)
1456 int object_size_type
= osi
->object_size_type
;
1458 if (TREE_CODE (var
) == SSA_NAME
)
1460 unsigned varno
= SSA_NAME_VERSION (var
);
1462 collect_object_sizes_for (osi
, var
);
1463 *size
= object_sizes_get (osi
, varno
);
1464 *wholesize
= object_sizes_get (osi
, varno
, true);
1466 else if (TREE_CODE (var
) == ADDR_EXPR
)
1467 addr_object_size (osi
, var
, object_size_type
, size
, wholesize
);
1469 *size
= *wholesize
= size_unknown (object_size_type
);
1472 /* Compute object_sizes for VAR, defined at STMT, which is
1473 a COND_EXPR. Return true if the object size might need reexamination
1477 cond_expr_object_size (struct object_size_info
*osi
, tree var
, gimple
*stmt
)
1480 int object_size_type
= osi
->object_size_type
;
1481 unsigned int varno
= SSA_NAME_VERSION (var
);
1482 bool reexamine
= false;
1484 gcc_assert (gimple_assign_rhs_code (stmt
) == COND_EXPR
);
1486 if (object_sizes_unknown_p (object_size_type
, varno
))
1489 then_
= gimple_assign_rhs2 (stmt
);
1490 else_
= gimple_assign_rhs3 (stmt
);
1492 if (object_size_type
& OST_DYNAMIC
)
1494 tree then_size
, then_wholesize
, else_size
, else_wholesize
;
1496 dynamic_object_size (osi
, then_
, &then_size
, &then_wholesize
);
1497 if (!size_unknown_p (then_size
, object_size_type
))
1498 dynamic_object_size (osi
, else_
, &else_size
, &else_wholesize
);
1500 tree cond_size
, cond_wholesize
;
1501 if (size_unknown_p (then_size
, object_size_type
)
1502 || size_unknown_p (else_size
, object_size_type
))
1503 cond_size
= cond_wholesize
= size_unknown (object_size_type
);
1506 cond_size
= fold_build3 (COND_EXPR
, sizetype
,
1507 gimple_assign_rhs1 (stmt
),
1508 then_size
, else_size
);
1509 cond_wholesize
= fold_build3 (COND_EXPR
, sizetype
,
1510 gimple_assign_rhs1 (stmt
),
1511 then_wholesize
, else_wholesize
);
1514 object_sizes_set (osi
, varno
, cond_size
, cond_wholesize
);
1519 if (TREE_CODE (then_
) == SSA_NAME
)
1520 reexamine
|= merge_object_sizes (osi
, var
, then_
);
1522 expr_object_size (osi
, var
, then_
);
1524 if (object_sizes_unknown_p (object_size_type
, varno
))
1527 if (TREE_CODE (else_
) == SSA_NAME
)
1528 reexamine
|= merge_object_sizes (osi
, var
, else_
);
1530 expr_object_size (osi
, var
, else_
);
1535 /* Find size of an object passed as a parameter to the function. */
1538 parm_object_size (struct object_size_info
*osi
, tree var
)
1540 int object_size_type
= osi
->object_size_type
;
1541 tree parm
= SSA_NAME_VAR (var
);
1543 if (!(object_size_type
& OST_DYNAMIC
) || !POINTER_TYPE_P (TREE_TYPE (parm
)))
1545 expr_object_size (osi
, var
, parm
);
1549 /* Look for access attribute. */
1552 tree fndecl
= cfun
->decl
;
1553 const attr_access
*access
= get_parm_access (rdwr_idx
, parm
, fndecl
);
1554 tree typesize
= TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (parm
)));
1555 tree sz
= NULL_TREE
;
1557 /* If we have an explicit access attribute with a usable size argument... */
1558 if (access
&& access
->sizarg
!= UINT_MAX
&& !access
->internal_p
1559 /* ... and either PARM is void * or has a type that is complete and has a
1561 && ((typesize
&& poly_int_tree_p (typesize
))
1562 || (!typesize
&& VOID_TYPE_P (TREE_TYPE (TREE_TYPE (parm
))))))
1564 tree fnargs
= DECL_ARGUMENTS (fndecl
);
1565 tree arg
= NULL_TREE
;
1566 unsigned argpos
= 0;
1568 /* ... then walk through the parameters to pick the size parameter and
1569 safely scale it by the type size if needed. */
1570 for (arg
= fnargs
; arg
; arg
= TREE_CHAIN (arg
), ++argpos
)
1571 if (argpos
== access
->sizarg
&& INTEGRAL_TYPE_P (TREE_TYPE (arg
)))
1573 sz
= get_or_create_ssa_default_def (cfun
, arg
);
1574 if (sz
!= NULL_TREE
)
1576 sz
= fold_convert (sizetype
, sz
);
1578 sz
= size_binop (MULT_EXPR
, sz
, typesize
);
1584 sz
= size_unknown (object_size_type
);
1586 object_sizes_set (osi
, SSA_NAME_VERSION (var
), sz
, sz
);
1589 /* Compute an object size expression for VAR, which is the result of a PHI
1593 phi_dynamic_object_size (struct object_size_info
*osi
, tree var
)
1595 int object_size_type
= osi
->object_size_type
;
1596 unsigned int varno
= SSA_NAME_VERSION (var
);
1597 gimple
*stmt
= SSA_NAME_DEF_STMT (var
);
1598 unsigned i
, num_args
= gimple_phi_num_args (stmt
);
1599 bool wholesize_needed
= false;
1601 /* The extra space is for the PHI result at the end, which object_sizes_set
1603 tree sizes
= make_tree_vec (num_args
+ 1);
1604 tree wholesizes
= make_tree_vec (num_args
+ 1);
1606 /* Bail out if the size of any of the PHI arguments cannot be
1608 for (i
= 0; i
< num_args
; i
++)
1610 edge e
= gimple_phi_arg_edge (as_a
<gphi
*> (stmt
), i
);
1611 if (e
->flags
& EDGE_COMPLEX
)
1614 tree rhs
= gimple_phi_arg_def (stmt
, i
);
1615 tree size
, wholesize
;
1617 dynamic_object_size (osi
, rhs
, &size
, &wholesize
);
1619 if (size_unknown_p (size
, object_size_type
))
1622 if (size
!= wholesize
)
1623 wholesize_needed
= true;
1625 TREE_VEC_ELT (sizes
, i
) = size
;
1626 TREE_VEC_ELT (wholesizes
, i
) = wholesize
;
1632 ggc_free (wholesizes
);
1633 sizes
= wholesizes
= size_unknown (object_size_type
);
1636 /* Point to the same TREE_VEC so that we can avoid emitting two PHI
1638 else if (!wholesize_needed
)
1640 ggc_free (wholesizes
);
1644 object_sizes_set (osi
, varno
, sizes
, wholesizes
);
1647 /* Compute object sizes for VAR.
1648 For ADDR_EXPR an object size is the number of remaining bytes
1649 to the end of the object (where what is considered an object depends on
1650 OSI->object_size_type).
1651 For allocation GIMPLE_CALL like malloc or calloc object size is the size
1653 For POINTER_PLUS_EXPR where second operand is a constant integer,
1654 object size is object size of the first operand minus the constant.
1655 If the constant is bigger than the number of remaining bytes until the
1656 end of the object, object size is 0, but if it is instead a pointer
1657 subtraction, object size is size_unknown (object_size_type).
1658 To differentiate addition from subtraction, ADDR_EXPR returns
1659 size_unknown (object_size_type) for all objects bigger than half of the
1660 address space, and constants less than half of the address space are
1661 considered addition, while bigger constants subtraction.
1662 For a memcpy like GIMPLE_CALL that always returns one of its arguments, the
1663 object size is object size of that argument.
1664 Otherwise, object size is the maximum of object sizes of variables
1665 that it might be set to. */
1668 collect_object_sizes_for (struct object_size_info
*osi
, tree var
)
1670 int object_size_type
= osi
->object_size_type
;
1671 unsigned int varno
= SSA_NAME_VERSION (var
);
1675 if (bitmap_bit_p (computed
[object_size_type
], varno
))
1680 if (bitmap_set_bit (osi
->visited
, varno
))
1682 /* Initialize to 0 for maximum size and M1U for minimum size so that
1683 it gets immediately overridden. */
1684 object_sizes_initialize (osi
, varno
,
1685 size_initval (object_size_type
),
1686 size_initval (object_size_type
));
1690 /* Found a dependency loop. Mark the variable for later
1692 if (object_size_type
& OST_DYNAMIC
)
1693 object_sizes_set_temp (osi
, varno
);
1695 bitmap_set_bit (osi
->reexamine
, varno
);
1696 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1698 fprintf (dump_file
, "Found a dependency loop at ");
1699 print_generic_expr (dump_file
, var
, dump_flags
);
1700 fprintf (dump_file
, "\n");
1706 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1708 fprintf (dump_file
, "Visiting use-def links for ");
1709 print_generic_expr (dump_file
, var
, dump_flags
);
1710 fprintf (dump_file
, "\n");
1713 stmt
= SSA_NAME_DEF_STMT (var
);
1716 switch (gimple_code (stmt
))
1720 tree rhs
= gimple_assign_rhs1 (stmt
);
1721 if (gimple_assign_rhs_code (stmt
) == POINTER_PLUS_EXPR
1722 || (gimple_assign_rhs_code (stmt
) == ADDR_EXPR
1723 && TREE_CODE (TREE_OPERAND (rhs
, 0)) == MEM_REF
))
1724 reexamine
= plus_stmt_object_size (osi
, var
, stmt
);
1725 else if (gimple_assign_rhs_code (stmt
) == COND_EXPR
)
1726 reexamine
= cond_expr_object_size (osi
, var
, stmt
);
1727 else if (gimple_assign_single_p (stmt
)
1728 || gimple_assign_unary_nop_p (stmt
))
1730 if (TREE_CODE (rhs
) == SSA_NAME
1731 && POINTER_TYPE_P (TREE_TYPE (rhs
)))
1732 reexamine
= merge_object_sizes (osi
, var
, rhs
);
1734 expr_object_size (osi
, var
, rhs
);
1737 unknown_object_size (osi
, var
);
1743 gcall
*call_stmt
= as_a
<gcall
*> (stmt
);
1744 tree arg
= pass_through_call (call_stmt
);
1747 if (TREE_CODE (arg
) == SSA_NAME
1748 && POINTER_TYPE_P (TREE_TYPE (arg
)))
1749 reexamine
= merge_object_sizes (osi
, var
, arg
);
1751 expr_object_size (osi
, var
, arg
);
1754 call_object_size (osi
, var
, call_stmt
);
1759 /* Pointers defined by __asm__ statements can point anywhere. */
1760 unknown_object_size (osi
, var
);
1764 if (SSA_NAME_VAR (var
)
1765 && TREE_CODE (SSA_NAME_VAR (var
)) == PARM_DECL
)
1766 parm_object_size (osi
, var
);
1768 /* Uninitialized SSA names point nowhere. */
1769 unknown_object_size (osi
, var
);
1776 if (object_size_type
& OST_DYNAMIC
)
1778 phi_dynamic_object_size (osi
, var
);
1782 for (i
= 0; i
< gimple_phi_num_args (stmt
); i
++)
1784 tree rhs
= gimple_phi_arg (stmt
, i
)->def
;
1786 if (object_sizes_unknown_p (object_size_type
, varno
))
1789 if (TREE_CODE (rhs
) == SSA_NAME
)
1790 reexamine
|= merge_object_sizes (osi
, var
, rhs
);
1791 else if (osi
->pass
== 0)
1792 expr_object_size (osi
, var
, rhs
);
1801 if (! reexamine
|| object_sizes_unknown_p (object_size_type
, varno
))
1803 bitmap_set_bit (computed
[object_size_type
], varno
);
1804 if (!(object_size_type
& OST_DYNAMIC
))
1805 bitmap_clear_bit (osi
->reexamine
, varno
);
1809 bitmap_set_bit (osi
->reexamine
, varno
);
1810 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1812 fprintf (dump_file
, "Need to reexamine ");
1813 print_generic_expr (dump_file
, var
, dump_flags
);
1814 fprintf (dump_file
, "\n");
1820 /* Helper function for check_for_plus_in_loops. Called recursively
1824 check_for_plus_in_loops_1 (struct object_size_info
*osi
, tree var
,
1827 gimple
*stmt
= SSA_NAME_DEF_STMT (var
);
1828 unsigned int varno
= SSA_NAME_VERSION (var
);
1830 if (osi
->depths
[varno
])
1832 if (osi
->depths
[varno
] != depth
)
1836 /* Found a loop involving pointer addition. */
1837 for (sp
= osi
->tos
; sp
> osi
->stack
; )
1840 bitmap_clear_bit (osi
->reexamine
, *sp
);
1841 bitmap_set_bit (computed
[osi
->object_size_type
], *sp
);
1842 object_sizes_set (osi
, *sp
, size_zero_node
,
1843 object_sizes_get (osi
, *sp
, true));
1850 else if (! bitmap_bit_p (osi
->reexamine
, varno
))
1853 osi
->depths
[varno
] = depth
;
1854 *osi
->tos
++ = varno
;
1856 switch (gimple_code (stmt
))
1861 if ((gimple_assign_single_p (stmt
)
1862 || gimple_assign_unary_nop_p (stmt
))
1863 && TREE_CODE (gimple_assign_rhs1 (stmt
)) == SSA_NAME
)
1865 tree rhs
= gimple_assign_rhs1 (stmt
);
1867 check_for_plus_in_loops_1 (osi
, rhs
, depth
);
1869 else if (gimple_assign_rhs_code (stmt
) == POINTER_PLUS_EXPR
)
1871 tree basevar
= gimple_assign_rhs1 (stmt
);
1872 tree cst
= gimple_assign_rhs2 (stmt
);
1874 gcc_assert (TREE_CODE (cst
) == INTEGER_CST
);
1876 check_for_plus_in_loops_1 (osi
, basevar
,
1877 depth
+ !integer_zerop (cst
));
1886 gcall
*call_stmt
= as_a
<gcall
*> (stmt
);
1887 tree arg
= pass_through_call (call_stmt
);
1890 if (TREE_CODE (arg
) == SSA_NAME
)
1891 check_for_plus_in_loops_1 (osi
, arg
, depth
);
1902 for (i
= 0; i
< gimple_phi_num_args (stmt
); i
++)
1904 tree rhs
= gimple_phi_arg (stmt
, i
)->def
;
1906 if (TREE_CODE (rhs
) == SSA_NAME
)
1907 check_for_plus_in_loops_1 (osi
, rhs
, depth
);
1916 osi
->depths
[varno
] = 0;
1921 /* Check if some pointer we are computing object size of is being increased
1922 within a loop. If yes, assume all the SSA variables participating in
1923 that loop have minimum object sizes 0. */
1926 check_for_plus_in_loops (struct object_size_info
*osi
, tree var
)
1928 gimple
*stmt
= SSA_NAME_DEF_STMT (var
);
1930 /* NOTE: In the pre-tuples code, we handled a CALL_EXPR here,
1931 and looked for a POINTER_PLUS_EXPR in the pass-through
1932 argument, if any. In GIMPLE, however, such an expression
1933 is not a valid call operand. */
1935 if (is_gimple_assign (stmt
)
1936 && gimple_assign_rhs_code (stmt
) == POINTER_PLUS_EXPR
)
1938 tree basevar
= gimple_assign_rhs1 (stmt
);
1939 tree cst
= gimple_assign_rhs2 (stmt
);
1941 gcc_assert (TREE_CODE (cst
) == INTEGER_CST
);
1943 /* Skip non-positive offsets. */
1944 if (integer_zerop (cst
) || compare_tree_int (cst
, offset_limit
) > 0)
1947 osi
->depths
[SSA_NAME_VERSION (basevar
)] = 1;
1948 *osi
->tos
++ = SSA_NAME_VERSION (basevar
);
1949 check_for_plus_in_loops_1 (osi
, var
, 2);
1950 osi
->depths
[SSA_NAME_VERSION (basevar
)] = 0;
1956 /* Initialize data structures for the object size computation. */
1959 init_object_sizes (void)
1961 int object_size_type
;
1966 for (object_size_type
= 0; object_size_type
< OST_END
; object_size_type
++)
1968 object_sizes_grow (object_size_type
);
1969 computed
[object_size_type
] = BITMAP_ALLOC (NULL
);
1972 init_offset_limit ();
1976 /* Destroy data structures after the object size computation. */
1979 fini_object_sizes (void)
1981 int object_size_type
;
1983 for (object_size_type
= 0; object_size_type
< OST_END
; object_size_type
++)
1985 object_sizes_release (object_size_type
);
1986 BITMAP_FREE (computed
[object_size_type
]);
1990 /* Dummy valueize function. */
1993 do_valueize (tree t
)
1998 /* Process a __builtin_object_size or __builtin_dynamic_object_size call in
1999 CALL early for subobjects before any object information is lost due to
2000 optimization. Insert a MIN or MAX expression of the result and
2001 __builtin_object_size at I so that it may be processed in the second pass.
2002 __builtin_dynamic_object_size is treated like __builtin_object_size here
2003 since we're only looking for constant bounds. */
2006 early_object_sizes_execute_one (gimple_stmt_iterator
*i
, gimple
*call
)
2008 tree ost
= gimple_call_arg (call
, 1);
2009 tree lhs
= gimple_call_lhs (call
);
2010 gcc_assert (lhs
!= NULL_TREE
);
2012 if (!tree_fits_uhwi_p (ost
))
2015 unsigned HOST_WIDE_INT object_size_type
= tree_to_uhwi (ost
);
2016 tree ptr
= gimple_call_arg (call
, 0);
2018 if (object_size_type
!= 1 && object_size_type
!= 3)
2021 if (TREE_CODE (ptr
) != ADDR_EXPR
&& TREE_CODE (ptr
) != SSA_NAME
)
2024 tree type
= TREE_TYPE (lhs
);
2026 if (!compute_builtin_object_size (ptr
, object_size_type
, &bytes
)
2027 || !int_fits_type_p (bytes
, type
))
2030 tree tem
= make_ssa_name (type
);
2031 gimple_call_set_lhs (call
, tem
);
2032 enum tree_code code
= object_size_type
& OST_MINIMUM
? MAX_EXPR
: MIN_EXPR
;
2033 tree cst
= fold_convert (type
, bytes
);
2034 gimple
*g
= gimple_build_assign (lhs
, code
, tem
, cst
);
2035 gsi_insert_after (i
, g
, GSI_NEW_STMT
);
2039 /* Attempt to fold one __builtin_dynamic_object_size call in CALL into an
2040 expression and insert it at I. Return true if it succeeds. */
2043 dynamic_object_sizes_execute_one (gimple_stmt_iterator
*i
, gimple
*call
)
2045 gcc_assert (gimple_call_num_args (call
) == 2);
2048 args
[0] = gimple_call_arg (call
, 0);
2049 args
[1] = gimple_call_arg (call
, 1);
2051 location_t loc
= EXPR_LOC_OR_LOC (args
[0], input_location
);
2052 tree result_type
= gimple_call_return_type (as_a
<gcall
*> (call
));
2053 tree result
= fold_builtin_call_array (loc
, result_type
,
2054 gimple_call_fn (call
), 2, args
);
2059 /* fold_builtin_call_array may wrap the result inside a
2061 STRIP_NOPS (result
);
2062 gimplify_and_update_call_from_tree (i
, result
);
2064 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2066 fprintf (dump_file
, "Simplified (dynamic)\n ");
2067 print_gimple_stmt (dump_file
, call
, 0, dump_flags
);
2068 fprintf (dump_file
, " to ");
2069 print_generic_expr (dump_file
, result
);
2070 fprintf (dump_file
, "\n");
2076 object_sizes_execute (function
*fun
, bool early
)
2081 FOR_EACH_BB_FN (bb
, fun
)
2083 gimple_stmt_iterator i
;
2084 for (i
= gsi_start_bb (bb
); !gsi_end_p (i
); gsi_next (&i
))
2087 bool dynamic
= false;
2089 gimple
*call
= gsi_stmt (i
);
2090 if (gimple_call_builtin_p (call
, BUILT_IN_DYNAMIC_OBJECT_SIZE
))
2092 else if (!gimple_call_builtin_p (call
, BUILT_IN_OBJECT_SIZE
))
2095 tree lhs
= gimple_call_lhs (call
);
2099 init_object_sizes ();
2101 /* If early, only attempt to fold
2102 __builtin_object_size (x, 1) and __builtin_object_size (x, 3),
2103 and rather than folding the builtin to the constant if any,
2104 create a MIN_EXPR or MAX_EXPR of the __builtin_object_size
2105 call result and the computed constant. Do the same for
2106 __builtin_dynamic_object_size too. */
2109 early_object_sizes_execute_one (&i
, call
);
2115 if (dynamic_object_sizes_execute_one (&i
, call
))
2119 /* If we could not find a suitable size expression, lower to
2120 __builtin_object_size so that we may at least get a
2121 constant lower or higher estimate. */
2122 tree bosfn
= builtin_decl_implicit (BUILT_IN_OBJECT_SIZE
);
2123 gimple_call_set_fndecl (call
, bosfn
);
2126 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2128 print_generic_expr (dump_file
, gimple_call_arg (call
, 0),
2131 ": Retrying as __builtin_object_size\n");
2136 result
= gimple_fold_stmt_to_constant (call
, do_valueize
);
2139 tree ost
= gimple_call_arg (call
, 1);
2141 if (tree_fits_uhwi_p (ost
))
2143 unsigned HOST_WIDE_INT object_size_type
= tree_to_uhwi (ost
);
2145 if (object_size_type
& OST_MINIMUM
)
2146 result
= build_zero_cst (size_type_node
);
2147 else if (object_size_type
< OST_END
)
2148 result
= fold_convert (size_type_node
,
2149 integer_minus_one_node
);
2156 gcc_assert (TREE_CODE (result
) == INTEGER_CST
);
2158 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2160 fprintf (dump_file
, "Simplified\n ");
2161 print_gimple_stmt (dump_file
, call
, 0, dump_flags
);
2162 fprintf (dump_file
, " to ");
2163 print_generic_expr (dump_file
, result
);
2164 fprintf (dump_file
, "\n");
2167 /* Propagate into all uses and fold those stmts. */
2168 if (!SSA_NAME_OCCURS_IN_ABNORMAL_PHI (lhs
))
2169 replace_uses_by (lhs
, result
);
2171 replace_call_with_value (&i
, result
);
2175 fini_object_sizes ();
2179 /* Simple pass to optimize all __builtin_object_size () builtins. */
2183 const pass_data pass_data_object_sizes
=
2185 GIMPLE_PASS
, /* type */
2187 OPTGROUP_NONE
, /* optinfo_flags */
2188 TV_NONE
, /* tv_id */
2189 ( PROP_cfg
| PROP_ssa
), /* properties_required */
2190 PROP_objsz
, /* properties_provided */
2191 0, /* properties_destroyed */
2192 0, /* todo_flags_start */
2193 0, /* todo_flags_finish */
2196 class pass_object_sizes
: public gimple_opt_pass
2199 pass_object_sizes (gcc::context
*ctxt
)
2200 : gimple_opt_pass (pass_data_object_sizes
, ctxt
)
2203 /* opt_pass methods: */
2204 opt_pass
* clone () final override
{ return new pass_object_sizes (m_ctxt
); }
2205 unsigned int execute (function
*fun
) final override
2207 return object_sizes_execute (fun
, false);
2209 }; // class pass_object_sizes
2214 make_pass_object_sizes (gcc::context
*ctxt
)
2216 return new pass_object_sizes (ctxt
);
2219 /* Early version of pass to optimize all __builtin_object_size () builtins. */
2223 const pass_data pass_data_early_object_sizes
=
2225 GIMPLE_PASS
, /* type */
2226 "early_objsz", /* name */
2227 OPTGROUP_NONE
, /* optinfo_flags */
2228 TV_NONE
, /* tv_id */
2229 ( PROP_cfg
| PROP_ssa
), /* properties_required */
2230 0, /* properties_provided */
2231 0, /* properties_destroyed */
2232 0, /* todo_flags_start */
2233 0, /* todo_flags_finish */
2236 class pass_early_object_sizes
: public gimple_opt_pass
2239 pass_early_object_sizes (gcc::context
*ctxt
)
2240 : gimple_opt_pass (pass_data_early_object_sizes
, ctxt
)
2243 /* opt_pass methods: */
2244 unsigned int execute (function
*fun
) final override
2246 return object_sizes_execute (fun
, true);
2248 }; // class pass_object_sizes
2253 make_pass_early_object_sizes (gcc::context
*ctxt
)
2255 return new pass_early_object_sizes (ctxt
);