1 /* __builtin_object_size (ptr, object_size_type) computation
2 Copyright (C) 2004-2020 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-fold.h"
33 #include "gimple-iterator.h"
35 #include "stringpool.h"
38 struct object_size_info
43 bitmap visited
, reexamine
;
45 unsigned int *stack
, *tos
;
48 static const unsigned HOST_WIDE_INT unknown
[4] = {
55 static tree
compute_object_offset (const_tree
, const_tree
);
56 static bool addr_object_size (struct object_size_info
*,
57 const_tree
, int, unsigned HOST_WIDE_INT
*,
58 tree
* = NULL
, tree
* = NULL
);
59 static unsigned HOST_WIDE_INT
alloc_object_size (const gcall
*, int);
60 static tree
pass_through_call (const gcall
*);
61 static void collect_object_sizes_for (struct object_size_info
*, tree
);
62 static void expr_object_size (struct object_size_info
*, tree
, tree
);
63 static bool merge_object_sizes (struct object_size_info
*, tree
, tree
,
64 unsigned HOST_WIDE_INT
);
65 static bool plus_stmt_object_size (struct object_size_info
*, tree
, gimple
*);
66 static bool cond_expr_object_size (struct object_size_info
*, tree
, gimple
*);
67 static void init_offset_limit (void);
68 static void check_for_plus_in_loops (struct object_size_info
*, tree
);
69 static void check_for_plus_in_loops_1 (struct object_size_info
*, tree
,
72 /* object_sizes[0] is upper bound for number of bytes till the end of
74 object_sizes[1] is upper bound for number of bytes till the end of
75 the subobject (innermost array or field with address taken).
76 object_sizes[2] is lower bound for number of bytes till the end of
77 the object and object_sizes[3] lower bound for subobject. */
78 static vec
<unsigned HOST_WIDE_INT
> object_sizes
[4];
80 /* Bitmaps what object sizes have been computed already. */
81 static bitmap computed
[4];
83 /* Maximum value of offset we consider to be addition. */
84 static unsigned HOST_WIDE_INT offset_limit
;
87 /* Initialize OFFSET_LIMIT variable. */
89 init_offset_limit (void)
91 if (tree_fits_uhwi_p (TYPE_MAX_VALUE (sizetype
)))
92 offset_limit
= tree_to_uhwi (TYPE_MAX_VALUE (sizetype
));
99 /* Compute offset of EXPR within VAR. Return error_mark_node
103 compute_object_offset (const_tree expr
, const_tree var
)
105 enum tree_code code
= PLUS_EXPR
;
109 return size_zero_node
;
111 switch (TREE_CODE (expr
))
114 base
= compute_object_offset (TREE_OPERAND (expr
, 0), var
);
115 if (base
== error_mark_node
)
118 t
= TREE_OPERAND (expr
, 1);
119 off
= size_binop (PLUS_EXPR
, DECL_FIELD_OFFSET (t
),
120 size_int (tree_to_uhwi (DECL_FIELD_BIT_OFFSET (t
))
126 case VIEW_CONVERT_EXPR
:
127 case NON_LVALUE_EXPR
:
128 return compute_object_offset (TREE_OPERAND (expr
, 0), var
);
131 base
= compute_object_offset (TREE_OPERAND (expr
, 0), var
);
132 if (base
== error_mark_node
)
135 off
= TYPE_SIZE_UNIT (TREE_TYPE (expr
));
139 base
= compute_object_offset (TREE_OPERAND (expr
, 0), var
);
140 if (base
== error_mark_node
)
143 t
= TREE_OPERAND (expr
, 1);
144 tree low_bound
, unit_size
;
145 low_bound
= array_ref_low_bound (CONST_CAST_TREE (expr
));
146 unit_size
= array_ref_element_size (CONST_CAST_TREE (expr
));
147 if (! integer_zerop (low_bound
))
148 t
= fold_build2 (MINUS_EXPR
, TREE_TYPE (t
), t
, low_bound
);
149 if (TREE_CODE (t
) == INTEGER_CST
&& tree_int_cst_sgn (t
) < 0)
152 t
= fold_build1 (NEGATE_EXPR
, TREE_TYPE (t
), t
);
154 t
= fold_convert (sizetype
, t
);
155 off
= size_binop (MULT_EXPR
, unit_size
, t
);
159 gcc_assert (TREE_CODE (TREE_OPERAND (expr
, 0)) == ADDR_EXPR
);
160 return wide_int_to_tree (sizetype
, mem_ref_offset (expr
));
163 return error_mark_node
;
166 return size_binop (code
, base
, off
);
169 /* Returns the size of the object designated by DECL considering its
170 initializer if it either has one or if it would not affect its size,
171 otherwise the size of the object without the initializer when MIN
172 is true, else null. An object's initializer affects the object's
173 size if it's a struct type with a flexible array member. */
176 decl_init_size (tree decl
, bool min
)
178 tree size
= DECL_SIZE_UNIT (decl
);
179 tree type
= TREE_TYPE (decl
);
180 if (TREE_CODE (type
) != RECORD_TYPE
)
183 tree last
= last_field (type
);
187 tree last_type
= TREE_TYPE (last
);
188 if (TREE_CODE (last_type
) != ARRAY_TYPE
189 || TYPE_SIZE (last_type
))
192 /* Use TYPE_SIZE_UNIT; DECL_SIZE_UNIT sometimes reflects the size
193 of the initializer and sometimes doesn't. */
194 size
= TYPE_SIZE_UNIT (type
);
195 tree ref
= build3 (COMPONENT_REF
, type
, decl
, last
, NULL_TREE
);
196 tree compsize
= component_ref_size (ref
);
198 return min
? size
: NULL_TREE
;
200 /* The size includes tail padding and initializer elements. */
201 tree pos
= byte_position (last
);
202 size
= fold_build2 (PLUS_EXPR
, TREE_TYPE (size
), pos
, compsize
);
206 /* Compute __builtin_object_size for PTR, which is a ADDR_EXPR.
207 OBJECT_SIZE_TYPE is the second argument from __builtin_object_size.
208 If unknown, return unknown[object_size_type]. */
211 addr_object_size (struct object_size_info
*osi
, const_tree ptr
,
212 int object_size_type
, unsigned HOST_WIDE_INT
*psize
,
213 tree
*pdecl
/* = NULL */, tree
*poff
/* = NULL */)
215 tree pt_var
, pt_var_size
= NULL_TREE
, var_size
, bytes
;
217 tree dummy_decl
, dummy_off
= size_zero_node
;
223 gcc_assert (TREE_CODE (ptr
) == ADDR_EXPR
);
225 /* Set to unknown and overwrite just before returning if the size
226 could be determined. */
227 *psize
= unknown
[object_size_type
];
229 pt_var
= TREE_OPERAND (ptr
, 0);
230 while (handled_component_p (pt_var
))
231 pt_var
= TREE_OPERAND (pt_var
, 0);
236 if (TREE_CODE (pt_var
) == MEM_REF
)
238 unsigned HOST_WIDE_INT sz
;
240 if (!osi
|| (object_size_type
& 1) != 0
241 || TREE_CODE (TREE_OPERAND (pt_var
, 0)) != SSA_NAME
)
243 compute_builtin_object_size (TREE_OPERAND (pt_var
, 0),
244 object_size_type
& ~1, &sz
, pdecl
, poff
);
248 tree var
= TREE_OPERAND (pt_var
, 0);
250 collect_object_sizes_for (osi
, var
);
251 if (bitmap_bit_p (computed
[object_size_type
],
252 SSA_NAME_VERSION (var
)))
253 sz
= object_sizes
[object_size_type
][SSA_NAME_VERSION (var
)];
255 sz
= unknown
[object_size_type
];
257 if (sz
!= unknown
[object_size_type
])
259 offset_int mem_offset
;
260 if (mem_ref_offset (pt_var
).is_constant (&mem_offset
))
263 *poff
= wide_int_to_tree (ptrdiff_type_node
,
264 mem_offset
+ wi::to_offset (*poff
));
266 *poff
= wide_int_to_tree (ptrdiff_type_node
, mem_offset
);
267 offset_int dsz
= wi::sub (sz
, mem_offset
);
270 else if (wi::fits_uhwi_p (dsz
))
273 sz
= unknown
[object_size_type
];
276 sz
= unknown
[object_size_type
];
279 if (sz
!= unknown
[object_size_type
] && sz
< offset_limit
)
280 pt_var_size
= size_int (sz
);
282 else if (DECL_P (pt_var
))
285 pt_var_size
= decl_init_size (pt_var
, object_size_type
& 2);
289 else if (TREE_CODE (pt_var
) == STRING_CST
)
290 pt_var_size
= TYPE_SIZE_UNIT (TREE_TYPE (pt_var
));
296 /* Validate the size determined above. */
297 if (!tree_fits_uhwi_p (pt_var_size
)
298 || tree_to_uhwi (pt_var_size
) >= offset_limit
)
302 if (pt_var
!= TREE_OPERAND (ptr
, 0))
306 if (object_size_type
& 1)
308 var
= TREE_OPERAND (ptr
, 0);
311 && TREE_CODE (var
) != BIT_FIELD_REF
312 && TREE_CODE (var
) != COMPONENT_REF
313 && TREE_CODE (var
) != ARRAY_REF
314 && TREE_CODE (var
) != ARRAY_RANGE_REF
315 && TREE_CODE (var
) != REALPART_EXPR
316 && TREE_CODE (var
) != IMAGPART_EXPR
)
317 var
= TREE_OPERAND (var
, 0);
318 if (var
!= pt_var
&& TREE_CODE (var
) == ARRAY_REF
)
319 var
= TREE_OPERAND (var
, 0);
320 if (! TYPE_SIZE_UNIT (TREE_TYPE (var
))
321 || ! tree_fits_uhwi_p (TYPE_SIZE_UNIT (TREE_TYPE (var
)))
323 && tree_int_cst_lt (pt_var_size
,
324 TYPE_SIZE_UNIT (TREE_TYPE (var
)))))
326 else if (var
!= pt_var
&& TREE_CODE (pt_var
) == MEM_REF
)
329 /* For &X->fld, compute object size only if fld isn't the last
330 field, as struct { int i; char c[1]; } is often used instead
331 of flexible array member. */
332 while (v
&& v
!= pt_var
)
333 switch (TREE_CODE (v
))
336 if (TYPE_SIZE_UNIT (TREE_TYPE (TREE_OPERAND (v
, 0)))
337 && TREE_CODE (TREE_OPERAND (v
, 1)) == INTEGER_CST
)
340 = TYPE_DOMAIN (TREE_TYPE (TREE_OPERAND (v
, 0)));
342 && TYPE_MAX_VALUE (domain
)
343 && TREE_CODE (TYPE_MAX_VALUE (domain
))
345 && tree_int_cst_lt (TREE_OPERAND (v
, 1),
346 TYPE_MAX_VALUE (domain
)))
352 v
= TREE_OPERAND (v
, 0);
359 if (TREE_CODE (TREE_TYPE (v
)) != ARRAY_TYPE
)
364 while (v
!= pt_var
&& TREE_CODE (v
) == COMPONENT_REF
)
365 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (v
, 0)))
367 && TREE_CODE (TREE_TYPE (TREE_OPERAND (v
, 0)))
371 v
= TREE_OPERAND (v
, 0);
372 if (TREE_CODE (v
) == COMPONENT_REF
373 && TREE_CODE (TREE_TYPE (TREE_OPERAND (v
, 0)))
376 tree fld_chain
= DECL_CHAIN (TREE_OPERAND (v
, 1));
377 for (; fld_chain
; fld_chain
= DECL_CHAIN (fld_chain
))
378 if (TREE_CODE (fld_chain
) == FIELD_DECL
)
386 v
= TREE_OPERAND (v
, 0);
388 while (v
!= pt_var
&& TREE_CODE (v
) == COMPONENT_REF
)
389 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (v
, 0)))
391 && TREE_CODE (TREE_TYPE (TREE_OPERAND (v
, 0)))
395 v
= TREE_OPERAND (v
, 0);
413 var_size
= TYPE_SIZE_UNIT (TREE_TYPE (var
));
414 else if (!pt_var_size
)
417 var_size
= pt_var_size
;
418 bytes
= compute_object_offset (TREE_OPERAND (ptr
, 0), var
);
419 if (bytes
!= error_mark_node
)
422 if (TREE_CODE (bytes
) == INTEGER_CST
423 && tree_int_cst_lt (var_size
, bytes
))
424 bytes
= size_zero_node
;
426 bytes
= size_binop (MINUS_EXPR
, var_size
, bytes
);
430 && TREE_CODE (pt_var
) == MEM_REF
431 && bytes
!= error_mark_node
)
433 tree bytes2
= compute_object_offset (TREE_OPERAND (ptr
, 0), pt_var
);
434 if (bytes2
!= error_mark_node
)
436 if (TREE_CODE (bytes2
) == INTEGER_CST
437 && tree_int_cst_lt (pt_var_size
, bytes2
))
438 bytes2
= size_zero_node
;
440 bytes2
= size_binop (MINUS_EXPR
, pt_var_size
, bytes2
);
441 *poff
= size_binop (PLUS_EXPR
, *poff
, bytes2
);
442 bytes
= size_binop (MIN_EXPR
, bytes
, bytes2
);
446 else if (!pt_var_size
)
452 *poff
= size_zero_node
;
455 if (tree_fits_uhwi_p (bytes
))
457 *psize
= tree_to_uhwi (bytes
);
465 /* Compute __builtin_object_size for CALL, which is a GIMPLE_CALL.
466 Handles calls to functions declared with attribute alloc_size.
467 OBJECT_SIZE_TYPE is the second argument from __builtin_object_size.
468 If unknown, return unknown[object_size_type]. */
470 static unsigned HOST_WIDE_INT
471 alloc_object_size (const gcall
*call
, int object_size_type
)
473 gcc_assert (is_gimple_call (call
));
476 if (tree callfn
= gimple_call_fndecl (call
))
477 calltype
= TREE_TYPE (callfn
);
479 calltype
= gimple_call_fntype (call
);
482 return unknown
[object_size_type
];
484 /* Set to positions of alloc_size arguments. */
485 int arg1
= -1, arg2
= -1;
486 tree alloc_size
= lookup_attribute ("alloc_size",
487 TYPE_ATTRIBUTES (calltype
));
488 if (alloc_size
&& TREE_VALUE (alloc_size
))
490 tree p
= TREE_VALUE (alloc_size
);
492 arg1
= TREE_INT_CST_LOW (TREE_VALUE (p
))-1;
494 arg2
= TREE_INT_CST_LOW (TREE_VALUE (TREE_CHAIN (p
)))-1;
497 if (arg1
< 0 || arg1
>= (int)gimple_call_num_args (call
)
498 || TREE_CODE (gimple_call_arg (call
, arg1
)) != INTEGER_CST
500 && (arg2
>= (int)gimple_call_num_args (call
)
501 || TREE_CODE (gimple_call_arg (call
, arg2
)) != INTEGER_CST
)))
502 return unknown
[object_size_type
];
504 tree bytes
= NULL_TREE
;
506 bytes
= size_binop (MULT_EXPR
,
507 fold_convert (sizetype
, gimple_call_arg (call
, arg1
)),
508 fold_convert (sizetype
, gimple_call_arg (call
, arg2
)));
510 bytes
= fold_convert (sizetype
, gimple_call_arg (call
, arg1
));
512 if (bytes
&& tree_fits_uhwi_p (bytes
))
513 return tree_to_uhwi (bytes
);
515 return unknown
[object_size_type
];
519 /* If object size is propagated from one of function's arguments directly
520 to its return value, return that argument for GIMPLE_CALL statement CALL.
521 Otherwise return NULL. */
524 pass_through_call (const gcall
*call
)
526 unsigned rf
= gimple_call_return_flags (call
);
527 if (rf
& ERF_RETURNS_ARG
)
529 unsigned argnum
= rf
& ERF_RETURN_ARG_MASK
;
530 if (argnum
< gimple_call_num_args (call
))
531 return gimple_call_arg (call
, argnum
);
534 /* __builtin_assume_aligned is intentionally not marked RET1. */
535 if (gimple_call_builtin_p (call
, BUILT_IN_ASSUME_ALIGNED
))
536 return gimple_call_arg (call
, 0);
542 /* Compute __builtin_object_size value for PTR and set *PSIZE to
543 the resulting value. If the declared object is known and PDECL
544 is nonnull, sets *PDECL to the object's DECL. OBJECT_SIZE_TYPE
545 is the second argument to __builtin_object_size.
546 Returns true on success and false when the object size could not
550 compute_builtin_object_size (tree ptr
, int object_size_type
,
551 unsigned HOST_WIDE_INT
*psize
,
552 tree
*pdecl
/* = NULL */, tree
*poff
/* = NULL */)
554 gcc_assert (object_size_type
>= 0 && object_size_type
<= 3);
556 tree dummy_decl
, dummy_off
= size_zero_node
;
562 /* Set to unknown and overwrite just before returning if the size
563 could be determined. */
564 *psize
= unknown
[object_size_type
];
567 init_offset_limit ();
569 if (TREE_CODE (ptr
) == ADDR_EXPR
)
570 return addr_object_size (NULL
, ptr
, object_size_type
, psize
, pdecl
, poff
);
572 if (TREE_CODE (ptr
) != SSA_NAME
573 || !POINTER_TYPE_P (TREE_TYPE (ptr
)))
576 if (computed
[object_size_type
] == NULL
)
578 if (optimize
|| object_size_type
& 1)
581 /* When not optimizing, rather than failing, make a small effort
582 to determine the object size without the full benefit of
583 the (costly) computation below. */
584 gimple
*def
= SSA_NAME_DEF_STMT (ptr
);
585 if (gimple_code (def
) == GIMPLE_ASSIGN
)
587 tree_code code
= gimple_assign_rhs_code (def
);
588 if (code
== POINTER_PLUS_EXPR
)
590 tree offset
= gimple_assign_rhs2 (def
);
591 ptr
= gimple_assign_rhs1 (def
);
593 if (tree_fits_shwi_p (offset
)
594 && compute_builtin_object_size (ptr
, object_size_type
,
597 /* Return zero when the offset is out of bounds. */
598 unsigned HOST_WIDE_INT off
= tree_to_shwi (offset
);
599 *psize
= off
< *psize
? *psize
- off
: 0;
608 if (!bitmap_bit_p (computed
[object_size_type
], SSA_NAME_VERSION (ptr
)))
610 struct object_size_info osi
;
614 if (num_ssa_names
> object_sizes
[object_size_type
].length ())
615 object_sizes
[object_size_type
].safe_grow (num_ssa_names
, true);
618 fprintf (dump_file
, "Computing %s %sobject size for ",
619 (object_size_type
& 2) ? "minimum" : "maximum",
620 (object_size_type
& 1) ? "sub" : "");
621 print_generic_expr (dump_file
, ptr
, dump_flags
);
622 fprintf (dump_file
, ":\n");
625 osi
.visited
= BITMAP_ALLOC (NULL
);
626 osi
.reexamine
= BITMAP_ALLOC (NULL
);
627 osi
.object_size_type
= object_size_type
;
632 /* First pass: walk UD chains, compute object sizes that
633 can be computed. osi.reexamine bitmap at the end will
634 contain what variables were found in dependency cycles
635 and therefore need to be reexamined. */
638 collect_object_sizes_for (&osi
, ptr
);
640 /* Second pass: keep recomputing object sizes of variables
641 that need reexamination, until no object sizes are
642 increased or all object sizes are computed. */
643 if (! bitmap_empty_p (osi
.reexamine
))
645 bitmap reexamine
= BITMAP_ALLOC (NULL
);
647 /* If looking for minimum instead of maximum object size,
648 detect cases where a pointer is increased in a loop.
649 Although even without this detection pass 2 would eventually
650 terminate, it could take a long time. If a pointer is
651 increasing this way, we need to assume 0 object size.
652 E.g. p = &buf[0]; while (cond) p = p + 4; */
653 if (object_size_type
& 2)
655 osi
.depths
= XCNEWVEC (unsigned int, num_ssa_names
);
656 osi
.stack
= XNEWVEC (unsigned int, num_ssa_names
);
659 /* collect_object_sizes_for is changing
660 osi.reexamine bitmap, so iterate over a copy. */
661 bitmap_copy (reexamine
, osi
.reexamine
);
662 EXECUTE_IF_SET_IN_BITMAP (reexamine
, 0, i
, bi
)
663 if (bitmap_bit_p (osi
.reexamine
, i
))
664 check_for_plus_in_loops (&osi
, ssa_name (i
));
677 /* collect_object_sizes_for is changing
678 osi.reexamine bitmap, so iterate over a copy. */
679 bitmap_copy (reexamine
, osi
.reexamine
);
680 EXECUTE_IF_SET_IN_BITMAP (reexamine
, 0, i
, bi
)
681 if (bitmap_bit_p (osi
.reexamine
, i
))
683 collect_object_sizes_for (&osi
, ssa_name (i
));
684 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
686 fprintf (dump_file
, "Reexamining ");
687 print_generic_expr (dump_file
, ssa_name (i
),
689 fprintf (dump_file
, "\n");
695 BITMAP_FREE (reexamine
);
697 EXECUTE_IF_SET_IN_BITMAP (osi
.reexamine
, 0, i
, bi
)
698 bitmap_set_bit (computed
[object_size_type
], i
);
700 /* Debugging dumps. */
703 EXECUTE_IF_SET_IN_BITMAP (osi
.visited
, 0, i
, bi
)
704 if (object_sizes
[object_size_type
][i
]
705 != unknown
[object_size_type
])
707 print_generic_expr (dump_file
, ssa_name (i
),
710 ": %s %sobject size "
711 HOST_WIDE_INT_PRINT_UNSIGNED
"\n",
712 (object_size_type
& 2) ? "minimum" : "maximum",
713 (object_size_type
& 1) ? "sub" : "",
714 object_sizes
[object_size_type
][i
]);
718 BITMAP_FREE (osi
.reexamine
);
719 BITMAP_FREE (osi
.visited
);
722 *psize
= object_sizes
[object_size_type
][SSA_NAME_VERSION (ptr
)];
723 return *psize
!= unknown
[object_size_type
];
726 /* Compute object_sizes for PTR, defined to VALUE, which is not an SSA_NAME. */
729 expr_object_size (struct object_size_info
*osi
, tree ptr
, tree value
)
731 int object_size_type
= osi
->object_size_type
;
732 unsigned int varno
= SSA_NAME_VERSION (ptr
);
733 unsigned HOST_WIDE_INT bytes
;
735 gcc_assert (object_sizes
[object_size_type
][varno
]
736 != unknown
[object_size_type
]);
737 gcc_assert (osi
->pass
== 0);
739 if (TREE_CODE (value
) == WITH_SIZE_EXPR
)
740 value
= TREE_OPERAND (value
, 0);
742 /* Pointer variables should have been handled by merge_object_sizes. */
743 gcc_assert (TREE_CODE (value
) != SSA_NAME
744 || !POINTER_TYPE_P (TREE_TYPE (value
)));
746 if (TREE_CODE (value
) == ADDR_EXPR
)
747 addr_object_size (osi
, value
, object_size_type
, &bytes
);
749 bytes
= unknown
[object_size_type
];
751 if ((object_size_type
& 2) == 0)
753 if (object_sizes
[object_size_type
][varno
] < bytes
)
754 object_sizes
[object_size_type
][varno
] = bytes
;
758 if (object_sizes
[object_size_type
][varno
] > bytes
)
759 object_sizes
[object_size_type
][varno
] = bytes
;
764 /* Compute object_sizes for PTR, defined to the result of a call. */
767 call_object_size (struct object_size_info
*osi
, tree ptr
, gcall
*call
)
769 int object_size_type
= osi
->object_size_type
;
770 unsigned int varno
= SSA_NAME_VERSION (ptr
);
771 unsigned HOST_WIDE_INT bytes
;
773 gcc_assert (is_gimple_call (call
));
775 gcc_assert (object_sizes
[object_size_type
][varno
]
776 != unknown
[object_size_type
]);
777 gcc_assert (osi
->pass
== 0);
779 bytes
= alloc_object_size (call
, object_size_type
);
781 if ((object_size_type
& 2) == 0)
783 if (object_sizes
[object_size_type
][varno
] < bytes
)
784 object_sizes
[object_size_type
][varno
] = bytes
;
788 if (object_sizes
[object_size_type
][varno
] > bytes
)
789 object_sizes
[object_size_type
][varno
] = bytes
;
794 /* Compute object_sizes for PTR, defined to an unknown value. */
797 unknown_object_size (struct object_size_info
*osi
, tree ptr
)
799 int object_size_type
= osi
->object_size_type
;
800 unsigned int varno
= SSA_NAME_VERSION (ptr
);
801 unsigned HOST_WIDE_INT bytes
;
803 gcc_assert (object_sizes
[object_size_type
][varno
]
804 != unknown
[object_size_type
]);
805 gcc_assert (osi
->pass
== 0);
807 bytes
= unknown
[object_size_type
];
809 if ((object_size_type
& 2) == 0)
811 if (object_sizes
[object_size_type
][varno
] < bytes
)
812 object_sizes
[object_size_type
][varno
] = bytes
;
816 if (object_sizes
[object_size_type
][varno
] > bytes
)
817 object_sizes
[object_size_type
][varno
] = bytes
;
822 /* Merge object sizes of ORIG + OFFSET into DEST. Return true if
823 the object size might need reexamination later. */
826 merge_object_sizes (struct object_size_info
*osi
, tree dest
, tree orig
,
827 unsigned HOST_WIDE_INT offset
)
829 int object_size_type
= osi
->object_size_type
;
830 unsigned int varno
= SSA_NAME_VERSION (dest
);
831 unsigned HOST_WIDE_INT orig_bytes
;
833 if (object_sizes
[object_size_type
][varno
] == unknown
[object_size_type
])
835 if (offset
>= offset_limit
)
837 object_sizes
[object_size_type
][varno
] = unknown
[object_size_type
];
842 collect_object_sizes_for (osi
, orig
);
844 orig_bytes
= object_sizes
[object_size_type
][SSA_NAME_VERSION (orig
)];
845 if (orig_bytes
!= unknown
[object_size_type
])
846 orig_bytes
= (offset
> orig_bytes
)
847 ? HOST_WIDE_INT_0U
: orig_bytes
- offset
;
849 if ((object_size_type
& 2) == 0)
851 if (object_sizes
[object_size_type
][varno
] < orig_bytes
)
853 object_sizes
[object_size_type
][varno
] = orig_bytes
;
859 if (object_sizes
[object_size_type
][varno
] > orig_bytes
)
861 object_sizes
[object_size_type
][varno
] = orig_bytes
;
865 return bitmap_bit_p (osi
->reexamine
, SSA_NAME_VERSION (orig
));
869 /* Compute object_sizes for VAR, defined to the result of an assignment
870 with operator POINTER_PLUS_EXPR. Return true if the object size might
871 need reexamination later. */
874 plus_stmt_object_size (struct object_size_info
*osi
, tree var
, gimple
*stmt
)
876 int object_size_type
= osi
->object_size_type
;
877 unsigned int varno
= SSA_NAME_VERSION (var
);
878 unsigned HOST_WIDE_INT bytes
;
881 if (gimple_assign_rhs_code (stmt
) == POINTER_PLUS_EXPR
)
883 op0
= gimple_assign_rhs1 (stmt
);
884 op1
= gimple_assign_rhs2 (stmt
);
886 else if (gimple_assign_rhs_code (stmt
) == ADDR_EXPR
)
888 tree rhs
= TREE_OPERAND (gimple_assign_rhs1 (stmt
), 0);
889 gcc_assert (TREE_CODE (rhs
) == MEM_REF
);
890 op0
= TREE_OPERAND (rhs
, 0);
891 op1
= TREE_OPERAND (rhs
, 1);
896 if (object_sizes
[object_size_type
][varno
] == unknown
[object_size_type
])
899 /* Handle PTR + OFFSET here. */
900 if (TREE_CODE (op1
) == INTEGER_CST
901 && (TREE_CODE (op0
) == SSA_NAME
902 || TREE_CODE (op0
) == ADDR_EXPR
))
904 if (! tree_fits_uhwi_p (op1
))
905 bytes
= unknown
[object_size_type
];
906 else if (TREE_CODE (op0
) == SSA_NAME
)
907 return merge_object_sizes (osi
, var
, op0
, tree_to_uhwi (op1
));
910 unsigned HOST_WIDE_INT off
= tree_to_uhwi (op1
);
912 /* op0 will be ADDR_EXPR here. */
913 addr_object_size (osi
, op0
, object_size_type
, &bytes
);
914 if (bytes
== unknown
[object_size_type
])
916 else if (off
> offset_limit
)
917 bytes
= unknown
[object_size_type
];
918 else if (off
> bytes
)
925 bytes
= unknown
[object_size_type
];
927 if ((object_size_type
& 2) == 0)
929 if (object_sizes
[object_size_type
][varno
] < bytes
)
930 object_sizes
[object_size_type
][varno
] = bytes
;
934 if (object_sizes
[object_size_type
][varno
] > bytes
)
935 object_sizes
[object_size_type
][varno
] = bytes
;
941 /* Compute object_sizes for VAR, defined at STMT, which is
942 a COND_EXPR. Return true if the object size might need reexamination
946 cond_expr_object_size (struct object_size_info
*osi
, tree var
, gimple
*stmt
)
949 int object_size_type
= osi
->object_size_type
;
950 unsigned int varno
= SSA_NAME_VERSION (var
);
951 bool reexamine
= false;
953 gcc_assert (gimple_assign_rhs_code (stmt
) == COND_EXPR
);
955 if (object_sizes
[object_size_type
][varno
] == unknown
[object_size_type
])
958 then_
= gimple_assign_rhs2 (stmt
);
959 else_
= gimple_assign_rhs3 (stmt
);
961 if (TREE_CODE (then_
) == SSA_NAME
)
962 reexamine
|= merge_object_sizes (osi
, var
, then_
, 0);
964 expr_object_size (osi
, var
, then_
);
966 if (object_sizes
[object_size_type
][varno
] == unknown
[object_size_type
])
969 if (TREE_CODE (else_
) == SSA_NAME
)
970 reexamine
|= merge_object_sizes (osi
, var
, else_
, 0);
972 expr_object_size (osi
, var
, else_
);
977 /* Compute object sizes for VAR.
978 For ADDR_EXPR an object size is the number of remaining bytes
979 to the end of the object (where what is considered an object depends on
980 OSI->object_size_type).
981 For allocation GIMPLE_CALL like malloc or calloc object size is the size
983 For POINTER_PLUS_EXPR where second operand is a constant integer,
984 object size is object size of the first operand minus the constant.
985 If the constant is bigger than the number of remaining bytes until the
986 end of the object, object size is 0, but if it is instead a pointer
987 subtraction, object size is unknown[object_size_type].
988 To differentiate addition from subtraction, ADDR_EXPR returns
989 unknown[object_size_type] for all objects bigger than half of the address
990 space, and constants less than half of the address space are considered
991 addition, while bigger constants subtraction.
992 For a memcpy like GIMPLE_CALL that always returns one of its arguments, the
993 object size is object size of that argument.
994 Otherwise, object size is the maximum of object sizes of variables
995 that it might be set to. */
998 collect_object_sizes_for (struct object_size_info
*osi
, tree var
)
1000 int object_size_type
= osi
->object_size_type
;
1001 unsigned int varno
= SSA_NAME_VERSION (var
);
1005 if (bitmap_bit_p (computed
[object_size_type
], varno
))
1010 if (bitmap_set_bit (osi
->visited
, varno
))
1012 object_sizes
[object_size_type
][varno
]
1013 = (object_size_type
& 2) ? -1 : 0;
1017 /* Found a dependency loop. Mark the variable for later
1019 bitmap_set_bit (osi
->reexamine
, varno
);
1020 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1022 fprintf (dump_file
, "Found a dependency loop at ");
1023 print_generic_expr (dump_file
, var
, dump_flags
);
1024 fprintf (dump_file
, "\n");
1030 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1032 fprintf (dump_file
, "Visiting use-def links for ");
1033 print_generic_expr (dump_file
, var
, dump_flags
);
1034 fprintf (dump_file
, "\n");
1037 stmt
= SSA_NAME_DEF_STMT (var
);
1040 switch (gimple_code (stmt
))
1044 tree rhs
= gimple_assign_rhs1 (stmt
);
1045 if (gimple_assign_rhs_code (stmt
) == POINTER_PLUS_EXPR
1046 || (gimple_assign_rhs_code (stmt
) == ADDR_EXPR
1047 && TREE_CODE (TREE_OPERAND (rhs
, 0)) == MEM_REF
))
1048 reexamine
= plus_stmt_object_size (osi
, var
, stmt
);
1049 else if (gimple_assign_rhs_code (stmt
) == COND_EXPR
)
1050 reexamine
= cond_expr_object_size (osi
, var
, stmt
);
1051 else if (gimple_assign_single_p (stmt
)
1052 || gimple_assign_unary_nop_p (stmt
))
1054 if (TREE_CODE (rhs
) == SSA_NAME
1055 && POINTER_TYPE_P (TREE_TYPE (rhs
)))
1056 reexamine
= merge_object_sizes (osi
, var
, rhs
, 0);
1058 expr_object_size (osi
, var
, rhs
);
1061 unknown_object_size (osi
, var
);
1067 gcall
*call_stmt
= as_a
<gcall
*> (stmt
);
1068 tree arg
= pass_through_call (call_stmt
);
1071 if (TREE_CODE (arg
) == SSA_NAME
1072 && POINTER_TYPE_P (TREE_TYPE (arg
)))
1073 reexamine
= merge_object_sizes (osi
, var
, arg
, 0);
1075 expr_object_size (osi
, var
, arg
);
1078 call_object_size (osi
, var
, call_stmt
);
1083 /* Pointers defined by __asm__ statements can point anywhere. */
1084 object_sizes
[object_size_type
][varno
] = unknown
[object_size_type
];
1088 if (SSA_NAME_VAR (var
)
1089 && TREE_CODE (SSA_NAME_VAR (var
)) == PARM_DECL
)
1090 expr_object_size (osi
, var
, SSA_NAME_VAR (var
));
1092 /* Uninitialized SSA names point nowhere. */
1093 object_sizes
[object_size_type
][varno
] = unknown
[object_size_type
];
1100 for (i
= 0; i
< gimple_phi_num_args (stmt
); i
++)
1102 tree rhs
= gimple_phi_arg (stmt
, i
)->def
;
1104 if (object_sizes
[object_size_type
][varno
]
1105 == unknown
[object_size_type
])
1108 if (TREE_CODE (rhs
) == SSA_NAME
)
1109 reexamine
|= merge_object_sizes (osi
, var
, rhs
, 0);
1110 else if (osi
->pass
== 0)
1111 expr_object_size (osi
, var
, rhs
);
1121 || object_sizes
[object_size_type
][varno
] == unknown
[object_size_type
])
1123 bitmap_set_bit (computed
[object_size_type
], varno
);
1124 bitmap_clear_bit (osi
->reexamine
, varno
);
1128 bitmap_set_bit (osi
->reexamine
, varno
);
1129 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1131 fprintf (dump_file
, "Need to reexamine ");
1132 print_generic_expr (dump_file
, var
, dump_flags
);
1133 fprintf (dump_file
, "\n");
1139 /* Helper function for check_for_plus_in_loops. Called recursively
1143 check_for_plus_in_loops_1 (struct object_size_info
*osi
, tree var
,
1146 gimple
*stmt
= SSA_NAME_DEF_STMT (var
);
1147 unsigned int varno
= SSA_NAME_VERSION (var
);
1149 if (osi
->depths
[varno
])
1151 if (osi
->depths
[varno
] != depth
)
1155 /* Found a loop involving pointer addition. */
1156 for (sp
= osi
->tos
; sp
> osi
->stack
; )
1159 bitmap_clear_bit (osi
->reexamine
, *sp
);
1160 bitmap_set_bit (computed
[osi
->object_size_type
], *sp
);
1161 object_sizes
[osi
->object_size_type
][*sp
] = 0;
1168 else if (! bitmap_bit_p (osi
->reexamine
, varno
))
1171 osi
->depths
[varno
] = depth
;
1172 *osi
->tos
++ = varno
;
1174 switch (gimple_code (stmt
))
1179 if ((gimple_assign_single_p (stmt
)
1180 || gimple_assign_unary_nop_p (stmt
))
1181 && TREE_CODE (gimple_assign_rhs1 (stmt
)) == SSA_NAME
)
1183 tree rhs
= gimple_assign_rhs1 (stmt
);
1185 check_for_plus_in_loops_1 (osi
, rhs
, depth
);
1187 else if (gimple_assign_rhs_code (stmt
) == POINTER_PLUS_EXPR
)
1189 tree basevar
= gimple_assign_rhs1 (stmt
);
1190 tree cst
= gimple_assign_rhs2 (stmt
);
1192 gcc_assert (TREE_CODE (cst
) == INTEGER_CST
);
1194 check_for_plus_in_loops_1 (osi
, basevar
,
1195 depth
+ !integer_zerop (cst
));
1204 gcall
*call_stmt
= as_a
<gcall
*> (stmt
);
1205 tree arg
= pass_through_call (call_stmt
);
1208 if (TREE_CODE (arg
) == SSA_NAME
)
1209 check_for_plus_in_loops_1 (osi
, arg
, depth
);
1220 for (i
= 0; i
< gimple_phi_num_args (stmt
); i
++)
1222 tree rhs
= gimple_phi_arg (stmt
, i
)->def
;
1224 if (TREE_CODE (rhs
) == SSA_NAME
)
1225 check_for_plus_in_loops_1 (osi
, rhs
, depth
);
1234 osi
->depths
[varno
] = 0;
1239 /* Check if some pointer we are computing object size of is being increased
1240 within a loop. If yes, assume all the SSA variables participating in
1241 that loop have minimum object sizes 0. */
1244 check_for_plus_in_loops (struct object_size_info
*osi
, tree var
)
1246 gimple
*stmt
= SSA_NAME_DEF_STMT (var
);
1248 /* NOTE: In the pre-tuples code, we handled a CALL_EXPR here,
1249 and looked for a POINTER_PLUS_EXPR in the pass-through
1250 argument, if any. In GIMPLE, however, such an expression
1251 is not a valid call operand. */
1253 if (is_gimple_assign (stmt
)
1254 && gimple_assign_rhs_code (stmt
) == POINTER_PLUS_EXPR
)
1256 tree basevar
= gimple_assign_rhs1 (stmt
);
1257 tree cst
= gimple_assign_rhs2 (stmt
);
1259 gcc_assert (TREE_CODE (cst
) == INTEGER_CST
);
1261 if (integer_zerop (cst
))
1264 osi
->depths
[SSA_NAME_VERSION (basevar
)] = 1;
1265 *osi
->tos
++ = SSA_NAME_VERSION (basevar
);
1266 check_for_plus_in_loops_1 (osi
, var
, 2);
1267 osi
->depths
[SSA_NAME_VERSION (basevar
)] = 0;
1273 /* Initialize data structures for the object size computation. */
1276 init_object_sizes (void)
1278 int object_size_type
;
1283 for (object_size_type
= 0; object_size_type
<= 3; object_size_type
++)
1285 object_sizes
[object_size_type
].safe_grow (num_ssa_names
, true);
1286 computed
[object_size_type
] = BITMAP_ALLOC (NULL
);
1289 init_offset_limit ();
1293 /* Destroy data structures after the object size computation. */
1296 fini_object_sizes (void)
1298 int object_size_type
;
1300 for (object_size_type
= 0; object_size_type
<= 3; object_size_type
++)
1302 object_sizes
[object_size_type
].release ();
1303 BITMAP_FREE (computed
[object_size_type
]);
1308 /* Simple pass to optimize all __builtin_object_size () builtins. */
1312 const pass_data pass_data_object_sizes
=
1314 GIMPLE_PASS
, /* type */
1316 OPTGROUP_NONE
, /* optinfo_flags */
1317 TV_NONE
, /* tv_id */
1318 ( PROP_cfg
| PROP_ssa
), /* properties_required */
1319 0, /* properties_provided */
1320 0, /* properties_destroyed */
1321 0, /* todo_flags_start */
1322 0, /* todo_flags_finish */
1325 class pass_object_sizes
: public gimple_opt_pass
1328 pass_object_sizes (gcc::context
*ctxt
)
1329 : gimple_opt_pass (pass_data_object_sizes
, ctxt
), insert_min_max_p (false)
1332 /* opt_pass methods: */
1333 opt_pass
* clone () { return new pass_object_sizes (m_ctxt
); }
1334 void set_pass_param (unsigned int n
, bool param
)
1336 gcc_assert (n
== 0);
1337 insert_min_max_p
= param
;
1339 virtual unsigned int execute (function
*);
1342 /* Determines whether the pass instance creates MIN/MAX_EXPRs. */
1343 bool insert_min_max_p
;
1344 }; // class pass_object_sizes
1346 /* Dummy valueize function. */
1349 do_valueize (tree t
)
1355 pass_object_sizes::execute (function
*fun
)
1358 FOR_EACH_BB_FN (bb
, fun
)
1360 gimple_stmt_iterator i
;
1361 for (i
= gsi_start_bb (bb
); !gsi_end_p (i
); gsi_next (&i
))
1364 gimple
*call
= gsi_stmt (i
);
1365 if (!gimple_call_builtin_p (call
, BUILT_IN_OBJECT_SIZE
))
1368 init_object_sizes ();
1370 /* If insert_min_max_p, only attempt to fold
1371 __builtin_object_size (x, 1) and __builtin_object_size (x, 3),
1372 and rather than folding the builtin to the constant if any,
1373 create a MIN_EXPR or MAX_EXPR of the __builtin_object_size
1374 call result and the computed constant. */
1375 if (insert_min_max_p
)
1377 tree ost
= gimple_call_arg (call
, 1);
1378 if (tree_fits_uhwi_p (ost
))
1380 unsigned HOST_WIDE_INT object_size_type
= tree_to_uhwi (ost
);
1381 tree ptr
= gimple_call_arg (call
, 0);
1382 tree lhs
= gimple_call_lhs (call
);
1383 if ((object_size_type
== 1 || object_size_type
== 3)
1384 && (TREE_CODE (ptr
) == ADDR_EXPR
1385 || TREE_CODE (ptr
) == SSA_NAME
)
1388 tree type
= TREE_TYPE (lhs
);
1389 unsigned HOST_WIDE_INT bytes
;
1390 if (compute_builtin_object_size (ptr
, object_size_type
,
1392 && wi::fits_to_tree_p (bytes
, type
))
1394 tree tem
= make_ssa_name (type
);
1395 gimple_call_set_lhs (call
, tem
);
1397 = object_size_type
== 1 ? MIN_EXPR
: MAX_EXPR
;
1398 tree cst
= build_int_cstu (type
, bytes
);
1400 = gimple_build_assign (lhs
, code
, tem
, cst
);
1401 gsi_insert_after (&i
, g
, GSI_NEW_STMT
);
1409 tree lhs
= gimple_call_lhs (call
);
1413 result
= gimple_fold_stmt_to_constant (call
, do_valueize
);
1416 tree ost
= gimple_call_arg (call
, 1);
1418 if (tree_fits_uhwi_p (ost
))
1420 unsigned HOST_WIDE_INT object_size_type
= tree_to_uhwi (ost
);
1422 if (object_size_type
< 2)
1423 result
= fold_convert (size_type_node
,
1424 integer_minus_one_node
);
1425 else if (object_size_type
< 4)
1426 result
= build_zero_cst (size_type_node
);
1433 gcc_assert (TREE_CODE (result
) == INTEGER_CST
);
1435 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1437 fprintf (dump_file
, "Simplified\n ");
1438 print_gimple_stmt (dump_file
, call
, 0, dump_flags
);
1439 fprintf (dump_file
, " to ");
1440 print_generic_expr (dump_file
, result
);
1441 fprintf (dump_file
, "\n");
1444 /* Propagate into all uses and fold those stmts. */
1445 if (!SSA_NAME_OCCURS_IN_ABNORMAL_PHI (lhs
))
1446 replace_uses_by (lhs
, result
);
1448 replace_call_with_value (&i
, result
);
1452 fini_object_sizes ();
1459 make_pass_object_sizes (gcc::context
*ctxt
)
1461 return new pass_object_sizes (ctxt
);