1 /* __builtin_object_size (ptr, object_size_type) computation
2 Copyright (C) 2004-2015 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"
25 #include "hard-reg-set.h"
28 #include "tree-pass.h"
30 #include "gimple-pretty-print.h"
31 #include "diagnostic-core.h"
33 #include "fold-const.h"
34 #include "tree-object-size.h"
35 #include "internal-fn.h"
36 #include "gimple-fold.h"
37 #include "gimple-iterator.h"
38 #include "tree-ssa-propagate.h"
41 struct object_size_info
44 bitmap visited
, reexamine
;
48 unsigned int *stack
, *tos
;
51 static const unsigned HOST_WIDE_INT unknown
[4] = { -1, -1, 0, 0 };
53 static tree
compute_object_offset (const_tree
, const_tree
);
54 static unsigned HOST_WIDE_INT
addr_object_size (struct object_size_info
*,
56 static unsigned HOST_WIDE_INT
alloc_object_size (const gcall
*, int);
57 static tree
pass_through_call (const gcall
*);
58 static void collect_object_sizes_for (struct object_size_info
*, tree
);
59 static void expr_object_size (struct object_size_info
*, tree
, tree
);
60 static bool merge_object_sizes (struct object_size_info
*, tree
, tree
,
61 unsigned HOST_WIDE_INT
);
62 static bool plus_stmt_object_size (struct object_size_info
*, tree
, gimple
*);
63 static bool cond_expr_object_size (struct object_size_info
*, tree
, gimple
*);
64 static void init_offset_limit (void);
65 static void check_for_plus_in_loops (struct object_size_info
*, tree
);
66 static void check_for_plus_in_loops_1 (struct object_size_info
*, tree
,
69 /* object_sizes[0] is upper bound for number of bytes till the end of
71 object_sizes[1] is upper bound for number of bytes till the end of
72 the subobject (innermost array or field with address taken).
73 object_sizes[2] is lower bound for number of bytes till the end of
74 the object and object_sizes[3] lower bound for subobject. */
75 static vec
<unsigned HOST_WIDE_INT
> object_sizes
[4];
77 /* Bitmaps what object sizes have been computed already. */
78 static bitmap computed
[4];
80 /* Maximum value of offset we consider to be addition. */
81 static unsigned HOST_WIDE_INT offset_limit
;
84 /* Initialize OFFSET_LIMIT variable. */
86 init_offset_limit (void)
88 if (tree_fits_uhwi_p (TYPE_MAX_VALUE (sizetype
)))
89 offset_limit
= tree_to_uhwi (TYPE_MAX_VALUE (sizetype
));
96 /* Compute offset of EXPR within VAR. Return error_mark_node
100 compute_object_offset (const_tree expr
, const_tree var
)
102 enum tree_code code
= PLUS_EXPR
;
106 return size_zero_node
;
108 switch (TREE_CODE (expr
))
111 base
= compute_object_offset (TREE_OPERAND (expr
, 0), var
);
112 if (base
== error_mark_node
)
115 t
= TREE_OPERAND (expr
, 1);
116 off
= size_binop (PLUS_EXPR
, DECL_FIELD_OFFSET (t
),
117 size_int (tree_to_uhwi (DECL_FIELD_BIT_OFFSET (t
))
123 case VIEW_CONVERT_EXPR
:
124 case NON_LVALUE_EXPR
:
125 return compute_object_offset (TREE_OPERAND (expr
, 0), var
);
128 base
= compute_object_offset (TREE_OPERAND (expr
, 0), var
);
129 if (base
== error_mark_node
)
132 off
= TYPE_SIZE_UNIT (TREE_TYPE (expr
));
136 base
= compute_object_offset (TREE_OPERAND (expr
, 0), var
);
137 if (base
== error_mark_node
)
140 t
= TREE_OPERAND (expr
, 1);
141 if (TREE_CODE (t
) == INTEGER_CST
&& tree_int_cst_sgn (t
) < 0)
144 t
= fold_build1 (NEGATE_EXPR
, TREE_TYPE (t
), t
);
146 t
= fold_convert (sizetype
, t
);
147 off
= size_binop (MULT_EXPR
, TYPE_SIZE_UNIT (TREE_TYPE (expr
)), t
);
151 gcc_assert (TREE_CODE (TREE_OPERAND (expr
, 0)) == ADDR_EXPR
);
152 return wide_int_to_tree (sizetype
, mem_ref_offset (expr
));
155 return error_mark_node
;
158 return size_binop (code
, base
, off
);
162 /* Compute __builtin_object_size for PTR, which is a ADDR_EXPR.
163 OBJECT_SIZE_TYPE is the second argument from __builtin_object_size.
164 If unknown, return unknown[object_size_type]. */
166 static unsigned HOST_WIDE_INT
167 addr_object_size (struct object_size_info
*osi
, const_tree ptr
,
168 int object_size_type
)
170 tree pt_var
, pt_var_size
= NULL_TREE
, var_size
, bytes
;
172 gcc_assert (TREE_CODE (ptr
) == ADDR_EXPR
);
174 pt_var
= TREE_OPERAND (ptr
, 0);
175 while (handled_component_p (pt_var
))
176 pt_var
= TREE_OPERAND (pt_var
, 0);
179 && TREE_CODE (pt_var
) == MEM_REF
)
181 unsigned HOST_WIDE_INT sz
;
183 if (!osi
|| (object_size_type
& 1) != 0
184 || TREE_CODE (TREE_OPERAND (pt_var
, 0)) != SSA_NAME
)
186 sz
= compute_builtin_object_size (TREE_OPERAND (pt_var
, 0),
187 object_size_type
& ~1);
191 tree var
= TREE_OPERAND (pt_var
, 0);
193 collect_object_sizes_for (osi
, var
);
194 if (bitmap_bit_p (computed
[object_size_type
],
195 SSA_NAME_VERSION (var
)))
196 sz
= object_sizes
[object_size_type
][SSA_NAME_VERSION (var
)];
198 sz
= unknown
[object_size_type
];
200 if (sz
!= unknown
[object_size_type
])
202 offset_int dsz
= wi::sub (sz
, mem_ref_offset (pt_var
));
205 else if (wi::fits_uhwi_p (dsz
))
208 sz
= unknown
[object_size_type
];
211 if (sz
!= unknown
[object_size_type
] && sz
< offset_limit
)
212 pt_var_size
= size_int (sz
);
216 && tree_fits_uhwi_p (DECL_SIZE_UNIT (pt_var
))
217 && tree_to_uhwi (DECL_SIZE_UNIT (pt_var
)) < offset_limit
)
218 pt_var_size
= DECL_SIZE_UNIT (pt_var
);
220 && TREE_CODE (pt_var
) == STRING_CST
221 && TYPE_SIZE_UNIT (TREE_TYPE (pt_var
))
222 && tree_fits_uhwi_p (TYPE_SIZE_UNIT (TREE_TYPE (pt_var
)))
223 && tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (pt_var
)))
225 pt_var_size
= TYPE_SIZE_UNIT (TREE_TYPE (pt_var
));
227 return unknown
[object_size_type
];
229 if (pt_var
!= TREE_OPERAND (ptr
, 0))
233 if (object_size_type
& 1)
235 var
= TREE_OPERAND (ptr
, 0);
238 && TREE_CODE (var
) != BIT_FIELD_REF
239 && TREE_CODE (var
) != COMPONENT_REF
240 && TREE_CODE (var
) != ARRAY_REF
241 && TREE_CODE (var
) != ARRAY_RANGE_REF
242 && TREE_CODE (var
) != REALPART_EXPR
243 && TREE_CODE (var
) != IMAGPART_EXPR
)
244 var
= TREE_OPERAND (var
, 0);
245 if (var
!= pt_var
&& TREE_CODE (var
) == ARRAY_REF
)
246 var
= TREE_OPERAND (var
, 0);
247 if (! TYPE_SIZE_UNIT (TREE_TYPE (var
))
248 || ! tree_fits_uhwi_p (TYPE_SIZE_UNIT (TREE_TYPE (var
)))
250 && tree_int_cst_lt (pt_var_size
,
251 TYPE_SIZE_UNIT (TREE_TYPE (var
)))))
253 else if (var
!= pt_var
&& TREE_CODE (pt_var
) == MEM_REF
)
256 /* For &X->fld, compute object size only if fld isn't the last
257 field, as struct { int i; char c[1]; } is often used instead
258 of flexible array member. */
259 while (v
&& v
!= pt_var
)
260 switch (TREE_CODE (v
))
263 if (TYPE_SIZE_UNIT (TREE_TYPE (TREE_OPERAND (v
, 0)))
264 && TREE_CODE (TREE_OPERAND (v
, 1)) == INTEGER_CST
)
267 = TYPE_DOMAIN (TREE_TYPE (TREE_OPERAND (v
, 0)));
269 && TYPE_MAX_VALUE (domain
)
270 && TREE_CODE (TYPE_MAX_VALUE (domain
))
272 && tree_int_cst_lt (TREE_OPERAND (v
, 1),
273 TYPE_MAX_VALUE (domain
)))
279 v
= TREE_OPERAND (v
, 0);
286 if (TREE_CODE (TREE_TYPE (v
)) != ARRAY_TYPE
)
291 while (v
!= pt_var
&& TREE_CODE (v
) == COMPONENT_REF
)
292 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (v
, 0)))
294 && TREE_CODE (TREE_TYPE (TREE_OPERAND (v
, 0)))
298 v
= TREE_OPERAND (v
, 0);
299 if (TREE_CODE (v
) == COMPONENT_REF
300 && TREE_CODE (TREE_TYPE (TREE_OPERAND (v
, 0)))
303 tree fld_chain
= DECL_CHAIN (TREE_OPERAND (v
, 1));
304 for (; fld_chain
; fld_chain
= DECL_CHAIN (fld_chain
))
305 if (TREE_CODE (fld_chain
) == FIELD_DECL
)
313 v
= TREE_OPERAND (v
, 0);
315 while (v
!= pt_var
&& TREE_CODE (v
) == COMPONENT_REF
)
316 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (v
, 0)))
318 && TREE_CODE (TREE_TYPE (TREE_OPERAND (v
, 0)))
322 v
= TREE_OPERAND (v
, 0);
340 var_size
= TYPE_SIZE_UNIT (TREE_TYPE (var
));
341 else if (!pt_var_size
)
342 return unknown
[object_size_type
];
344 var_size
= pt_var_size
;
345 bytes
= compute_object_offset (TREE_OPERAND (ptr
, 0), var
);
346 if (bytes
!= error_mark_node
)
348 if (TREE_CODE (bytes
) == INTEGER_CST
349 && tree_int_cst_lt (var_size
, bytes
))
350 bytes
= size_zero_node
;
352 bytes
= size_binop (MINUS_EXPR
, var_size
, bytes
);
356 && TREE_CODE (pt_var
) == MEM_REF
357 && bytes
!= error_mark_node
)
359 tree bytes2
= compute_object_offset (TREE_OPERAND (ptr
, 0), pt_var
);
360 if (bytes2
!= error_mark_node
)
362 if (TREE_CODE (bytes2
) == INTEGER_CST
363 && tree_int_cst_lt (pt_var_size
, bytes2
))
364 bytes2
= size_zero_node
;
366 bytes2
= size_binop (MINUS_EXPR
, pt_var_size
, bytes2
);
367 bytes
= size_binop (MIN_EXPR
, bytes
, bytes2
);
371 else if (!pt_var_size
)
372 return unknown
[object_size_type
];
376 if (tree_fits_uhwi_p (bytes
))
377 return tree_to_uhwi (bytes
);
379 return unknown
[object_size_type
];
383 /* Compute __builtin_object_size for CALL, which is a GIMPLE_CALL.
384 Handles various allocation calls. OBJECT_SIZE_TYPE is the second
385 argument from __builtin_object_size. If unknown, return
386 unknown[object_size_type]. */
388 static unsigned HOST_WIDE_INT
389 alloc_object_size (const gcall
*call
, int object_size_type
)
391 tree callee
, bytes
= NULL_TREE
;
393 int arg1
= -1, arg2
= -1;
395 gcc_assert (is_gimple_call (call
));
397 callee
= gimple_call_fndecl (call
);
399 return unknown
[object_size_type
];
401 alloc_size
= lookup_attribute ("alloc_size",
402 TYPE_ATTRIBUTES (TREE_TYPE (callee
)));
403 if (alloc_size
&& TREE_VALUE (alloc_size
))
405 tree p
= TREE_VALUE (alloc_size
);
407 arg1
= TREE_INT_CST_LOW (TREE_VALUE (p
))-1;
409 arg2
= TREE_INT_CST_LOW (TREE_VALUE (TREE_CHAIN (p
)))-1;
412 if (DECL_BUILT_IN_CLASS (callee
) == BUILT_IN_NORMAL
)
413 switch (DECL_FUNCTION_CODE (callee
))
415 case BUILT_IN_CALLOC
:
418 case BUILT_IN_MALLOC
:
419 case BUILT_IN_ALLOCA
:
420 case BUILT_IN_ALLOCA_WITH_ALIGN
:
426 if (arg1
< 0 || arg1
>= (int)gimple_call_num_args (call
)
427 || TREE_CODE (gimple_call_arg (call
, arg1
)) != INTEGER_CST
429 && (arg2
>= (int)gimple_call_num_args (call
)
430 || TREE_CODE (gimple_call_arg (call
, arg2
)) != INTEGER_CST
)))
431 return unknown
[object_size_type
];
434 bytes
= size_binop (MULT_EXPR
,
435 fold_convert (sizetype
, gimple_call_arg (call
, arg1
)),
436 fold_convert (sizetype
, gimple_call_arg (call
, arg2
)));
438 bytes
= fold_convert (sizetype
, gimple_call_arg (call
, arg1
));
440 if (bytes
&& tree_fits_uhwi_p (bytes
))
441 return tree_to_uhwi (bytes
);
443 return unknown
[object_size_type
];
447 /* If object size is propagated from one of function's arguments directly
448 to its return value, return that argument for GIMPLE_CALL statement CALL.
449 Otherwise return NULL. */
452 pass_through_call (const gcall
*call
)
454 tree callee
= gimple_call_fndecl (call
);
457 && DECL_BUILT_IN_CLASS (callee
) == BUILT_IN_NORMAL
)
458 switch (DECL_FUNCTION_CODE (callee
))
460 case BUILT_IN_MEMCPY
:
461 case BUILT_IN_MEMMOVE
:
462 case BUILT_IN_MEMSET
:
463 case BUILT_IN_STRCPY
:
464 case BUILT_IN_STRNCPY
:
465 case BUILT_IN_STRCAT
:
466 case BUILT_IN_STRNCAT
:
467 case BUILT_IN_MEMCPY_CHK
:
468 case BUILT_IN_MEMMOVE_CHK
:
469 case BUILT_IN_MEMSET_CHK
:
470 case BUILT_IN_STRCPY_CHK
:
471 case BUILT_IN_STRNCPY_CHK
:
472 case BUILT_IN_STPNCPY_CHK
:
473 case BUILT_IN_STRCAT_CHK
:
474 case BUILT_IN_STRNCAT_CHK
:
475 case BUILT_IN_ASSUME_ALIGNED
:
476 if (gimple_call_num_args (call
) >= 1)
477 return gimple_call_arg (call
, 0);
487 /* Compute __builtin_object_size value for PTR. OBJECT_SIZE_TYPE is the
488 second argument from __builtin_object_size. */
490 unsigned HOST_WIDE_INT
491 compute_builtin_object_size (tree ptr
, int object_size_type
)
493 gcc_assert (object_size_type
>= 0 && object_size_type
<= 3);
496 init_offset_limit ();
498 if (TREE_CODE (ptr
) == ADDR_EXPR
)
499 return addr_object_size (NULL
, ptr
, object_size_type
);
501 if (TREE_CODE (ptr
) == SSA_NAME
502 && POINTER_TYPE_P (TREE_TYPE (ptr
))
503 && computed
[object_size_type
] != NULL
)
505 if (!bitmap_bit_p (computed
[object_size_type
], SSA_NAME_VERSION (ptr
)))
507 struct object_size_info osi
;
511 if (num_ssa_names
> object_sizes
[object_size_type
].length ())
512 object_sizes
[object_size_type
].safe_grow (num_ssa_names
);
515 fprintf (dump_file
, "Computing %s %sobject size for ",
516 (object_size_type
& 2) ? "minimum" : "maximum",
517 (object_size_type
& 1) ? "sub" : "");
518 print_generic_expr (dump_file
, ptr
, dump_flags
);
519 fprintf (dump_file
, ":\n");
522 osi
.visited
= BITMAP_ALLOC (NULL
);
523 osi
.reexamine
= BITMAP_ALLOC (NULL
);
524 osi
.object_size_type
= object_size_type
;
529 /* First pass: walk UD chains, compute object sizes that
530 can be computed. osi.reexamine bitmap at the end will
531 contain what variables were found in dependency cycles
532 and therefore need to be reexamined. */
535 collect_object_sizes_for (&osi
, ptr
);
537 /* Second pass: keep recomputing object sizes of variables
538 that need reexamination, until no object sizes are
539 increased or all object sizes are computed. */
540 if (! bitmap_empty_p (osi
.reexamine
))
542 bitmap reexamine
= BITMAP_ALLOC (NULL
);
544 /* If looking for minimum instead of maximum object size,
545 detect cases where a pointer is increased in a loop.
546 Although even without this detection pass 2 would eventually
547 terminate, it could take a long time. If a pointer is
548 increasing this way, we need to assume 0 object size.
549 E.g. p = &buf[0]; while (cond) p = p + 4; */
550 if (object_size_type
& 2)
552 osi
.depths
= XCNEWVEC (unsigned int, num_ssa_names
);
553 osi
.stack
= XNEWVEC (unsigned int, num_ssa_names
);
556 /* collect_object_sizes_for is changing
557 osi.reexamine bitmap, so iterate over a copy. */
558 bitmap_copy (reexamine
, osi
.reexamine
);
559 EXECUTE_IF_SET_IN_BITMAP (reexamine
, 0, i
, bi
)
560 if (bitmap_bit_p (osi
.reexamine
, i
))
561 check_for_plus_in_loops (&osi
, ssa_name (i
));
574 /* collect_object_sizes_for is changing
575 osi.reexamine bitmap, so iterate over a copy. */
576 bitmap_copy (reexamine
, osi
.reexamine
);
577 EXECUTE_IF_SET_IN_BITMAP (reexamine
, 0, i
, bi
)
578 if (bitmap_bit_p (osi
.reexamine
, i
))
580 collect_object_sizes_for (&osi
, ssa_name (i
));
581 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
583 fprintf (dump_file
, "Reexamining ");
584 print_generic_expr (dump_file
, ssa_name (i
),
586 fprintf (dump_file
, "\n");
592 BITMAP_FREE (reexamine
);
594 EXECUTE_IF_SET_IN_BITMAP (osi
.reexamine
, 0, i
, bi
)
595 bitmap_set_bit (computed
[object_size_type
], i
);
597 /* Debugging dumps. */
600 EXECUTE_IF_SET_IN_BITMAP (osi
.visited
, 0, i
, bi
)
601 if (object_sizes
[object_size_type
][i
]
602 != unknown
[object_size_type
])
604 print_generic_expr (dump_file
, ssa_name (i
),
607 ": %s %sobject size "
608 HOST_WIDE_INT_PRINT_UNSIGNED
"\n",
609 (object_size_type
& 2) ? "minimum" : "maximum",
610 (object_size_type
& 1) ? "sub" : "",
611 object_sizes
[object_size_type
][i
]);
615 BITMAP_FREE (osi
.reexamine
);
616 BITMAP_FREE (osi
.visited
);
619 return object_sizes
[object_size_type
][SSA_NAME_VERSION (ptr
)];
622 return unknown
[object_size_type
];
625 /* Compute object_sizes for PTR, defined to VALUE, which is not an SSA_NAME. */
628 expr_object_size (struct object_size_info
*osi
, tree ptr
, tree value
)
630 int object_size_type
= osi
->object_size_type
;
631 unsigned int varno
= SSA_NAME_VERSION (ptr
);
632 unsigned HOST_WIDE_INT bytes
;
634 gcc_assert (object_sizes
[object_size_type
][varno
]
635 != unknown
[object_size_type
]);
636 gcc_assert (osi
->pass
== 0);
638 if (TREE_CODE (value
) == WITH_SIZE_EXPR
)
639 value
= TREE_OPERAND (value
, 0);
641 /* Pointer variables should have been handled by merge_object_sizes. */
642 gcc_assert (TREE_CODE (value
) != SSA_NAME
643 || !POINTER_TYPE_P (TREE_TYPE (value
)));
645 if (TREE_CODE (value
) == ADDR_EXPR
)
646 bytes
= addr_object_size (osi
, value
, object_size_type
);
648 bytes
= unknown
[object_size_type
];
650 if ((object_size_type
& 2) == 0)
652 if (object_sizes
[object_size_type
][varno
] < bytes
)
653 object_sizes
[object_size_type
][varno
] = bytes
;
657 if (object_sizes
[object_size_type
][varno
] > bytes
)
658 object_sizes
[object_size_type
][varno
] = bytes
;
663 /* Compute object_sizes for PTR, defined to the result of a call. */
666 call_object_size (struct object_size_info
*osi
, tree ptr
, gcall
*call
)
668 int object_size_type
= osi
->object_size_type
;
669 unsigned int varno
= SSA_NAME_VERSION (ptr
);
670 unsigned HOST_WIDE_INT bytes
;
672 gcc_assert (is_gimple_call (call
));
674 gcc_assert (object_sizes
[object_size_type
][varno
]
675 != unknown
[object_size_type
]);
676 gcc_assert (osi
->pass
== 0);
678 bytes
= alloc_object_size (call
, object_size_type
);
680 if ((object_size_type
& 2) == 0)
682 if (object_sizes
[object_size_type
][varno
] < bytes
)
683 object_sizes
[object_size_type
][varno
] = bytes
;
687 if (object_sizes
[object_size_type
][varno
] > bytes
)
688 object_sizes
[object_size_type
][varno
] = bytes
;
693 /* Compute object_sizes for PTR, defined to an unknown value. */
696 unknown_object_size (struct object_size_info
*osi
, tree ptr
)
698 int object_size_type
= osi
->object_size_type
;
699 unsigned int varno
= SSA_NAME_VERSION (ptr
);
700 unsigned HOST_WIDE_INT bytes
;
702 gcc_assert (object_sizes
[object_size_type
][varno
]
703 != unknown
[object_size_type
]);
704 gcc_assert (osi
->pass
== 0);
706 bytes
= unknown
[object_size_type
];
708 if ((object_size_type
& 2) == 0)
710 if (object_sizes
[object_size_type
][varno
] < bytes
)
711 object_sizes
[object_size_type
][varno
] = bytes
;
715 if (object_sizes
[object_size_type
][varno
] > bytes
)
716 object_sizes
[object_size_type
][varno
] = bytes
;
721 /* Merge object sizes of ORIG + OFFSET into DEST. Return true if
722 the object size might need reexamination later. */
725 merge_object_sizes (struct object_size_info
*osi
, tree dest
, tree orig
,
726 unsigned HOST_WIDE_INT offset
)
728 int object_size_type
= osi
->object_size_type
;
729 unsigned int varno
= SSA_NAME_VERSION (dest
);
730 unsigned HOST_WIDE_INT orig_bytes
;
732 if (object_sizes
[object_size_type
][varno
] == unknown
[object_size_type
])
734 if (offset
>= offset_limit
)
736 object_sizes
[object_size_type
][varno
] = unknown
[object_size_type
];
741 collect_object_sizes_for (osi
, orig
);
743 orig_bytes
= object_sizes
[object_size_type
][SSA_NAME_VERSION (orig
)];
744 if (orig_bytes
!= unknown
[object_size_type
])
745 orig_bytes
= (offset
> orig_bytes
)
746 ? (unsigned HOST_WIDE_INT
) 0 : orig_bytes
- offset
;
748 if ((object_size_type
& 2) == 0)
750 if (object_sizes
[object_size_type
][varno
] < orig_bytes
)
752 object_sizes
[object_size_type
][varno
] = orig_bytes
;
758 if (object_sizes
[object_size_type
][varno
] > orig_bytes
)
760 object_sizes
[object_size_type
][varno
] = orig_bytes
;
764 return bitmap_bit_p (osi
->reexamine
, SSA_NAME_VERSION (orig
));
768 /* Compute object_sizes for VAR, defined to the result of an assignment
769 with operator POINTER_PLUS_EXPR. Return true if the object size might
770 need reexamination later. */
773 plus_stmt_object_size (struct object_size_info
*osi
, tree var
, gimple
*stmt
)
775 int object_size_type
= osi
->object_size_type
;
776 unsigned int varno
= SSA_NAME_VERSION (var
);
777 unsigned HOST_WIDE_INT bytes
;
780 if (gimple_assign_rhs_code (stmt
) == POINTER_PLUS_EXPR
)
782 op0
= gimple_assign_rhs1 (stmt
);
783 op1
= gimple_assign_rhs2 (stmt
);
785 else if (gimple_assign_rhs_code (stmt
) == ADDR_EXPR
)
787 tree rhs
= TREE_OPERAND (gimple_assign_rhs1 (stmt
), 0);
788 gcc_assert (TREE_CODE (rhs
) == MEM_REF
);
789 op0
= TREE_OPERAND (rhs
, 0);
790 op1
= TREE_OPERAND (rhs
, 1);
795 if (object_sizes
[object_size_type
][varno
] == unknown
[object_size_type
])
798 /* Handle PTR + OFFSET here. */
799 if (TREE_CODE (op1
) == INTEGER_CST
800 && (TREE_CODE (op0
) == SSA_NAME
801 || TREE_CODE (op0
) == ADDR_EXPR
))
803 if (! tree_fits_uhwi_p (op1
))
804 bytes
= unknown
[object_size_type
];
805 else if (TREE_CODE (op0
) == SSA_NAME
)
806 return merge_object_sizes (osi
, var
, op0
, tree_to_uhwi (op1
));
809 unsigned HOST_WIDE_INT off
= tree_to_uhwi (op1
);
811 /* op0 will be ADDR_EXPR here. */
812 bytes
= addr_object_size (osi
, op0
, object_size_type
);
813 if (bytes
== unknown
[object_size_type
])
815 else if (off
> offset_limit
)
816 bytes
= unknown
[object_size_type
];
817 else if (off
> bytes
)
824 bytes
= unknown
[object_size_type
];
826 if ((object_size_type
& 2) == 0)
828 if (object_sizes
[object_size_type
][varno
] < bytes
)
829 object_sizes
[object_size_type
][varno
] = bytes
;
833 if (object_sizes
[object_size_type
][varno
] > bytes
)
834 object_sizes
[object_size_type
][varno
] = bytes
;
840 /* Compute object_sizes for VAR, defined at STMT, which is
841 a COND_EXPR. Return true if the object size might need reexamination
845 cond_expr_object_size (struct object_size_info
*osi
, tree var
, gimple
*stmt
)
848 int object_size_type
= osi
->object_size_type
;
849 unsigned int varno
= SSA_NAME_VERSION (var
);
850 bool reexamine
= false;
852 gcc_assert (gimple_assign_rhs_code (stmt
) == COND_EXPR
);
854 if (object_sizes
[object_size_type
][varno
] == unknown
[object_size_type
])
857 then_
= gimple_assign_rhs2 (stmt
);
858 else_
= gimple_assign_rhs3 (stmt
);
860 if (TREE_CODE (then_
) == SSA_NAME
)
861 reexamine
|= merge_object_sizes (osi
, var
, then_
, 0);
863 expr_object_size (osi
, var
, then_
);
865 if (TREE_CODE (else_
) == SSA_NAME
)
866 reexamine
|= merge_object_sizes (osi
, var
, else_
, 0);
868 expr_object_size (osi
, var
, else_
);
873 /* Compute object sizes for VAR.
874 For ADDR_EXPR an object size is the number of remaining bytes
875 to the end of the object (where what is considered an object depends on
876 OSI->object_size_type).
877 For allocation GIMPLE_CALL like malloc or calloc object size is the size
879 For POINTER_PLUS_EXPR where second operand is a constant integer,
880 object size is object size of the first operand minus the constant.
881 If the constant is bigger than the number of remaining bytes until the
882 end of the object, object size is 0, but if it is instead a pointer
883 subtraction, object size is unknown[object_size_type].
884 To differentiate addition from subtraction, ADDR_EXPR returns
885 unknown[object_size_type] for all objects bigger than half of the address
886 space, and constants less than half of the address space are considered
887 addition, while bigger constants subtraction.
888 For a memcpy like GIMPLE_CALL that always returns one of its arguments, the
889 object size is object size of that argument.
890 Otherwise, object size is the maximum of object sizes of variables
891 that it might be set to. */
894 collect_object_sizes_for (struct object_size_info
*osi
, tree var
)
896 int object_size_type
= osi
->object_size_type
;
897 unsigned int varno
= SSA_NAME_VERSION (var
);
901 if (bitmap_bit_p (computed
[object_size_type
], varno
))
906 if (bitmap_set_bit (osi
->visited
, varno
))
908 object_sizes
[object_size_type
][varno
]
909 = (object_size_type
& 2) ? -1 : 0;
913 /* Found a dependency loop. Mark the variable for later
915 bitmap_set_bit (osi
->reexamine
, varno
);
916 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
918 fprintf (dump_file
, "Found a dependency loop at ");
919 print_generic_expr (dump_file
, var
, dump_flags
);
920 fprintf (dump_file
, "\n");
926 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
928 fprintf (dump_file
, "Visiting use-def links for ");
929 print_generic_expr (dump_file
, var
, dump_flags
);
930 fprintf (dump_file
, "\n");
933 stmt
= SSA_NAME_DEF_STMT (var
);
936 switch (gimple_code (stmt
))
940 tree rhs
= gimple_assign_rhs1 (stmt
);
941 if (gimple_assign_rhs_code (stmt
) == POINTER_PLUS_EXPR
942 || (gimple_assign_rhs_code (stmt
) == ADDR_EXPR
943 && TREE_CODE (TREE_OPERAND (rhs
, 0)) == MEM_REF
))
944 reexamine
= plus_stmt_object_size (osi
, var
, stmt
);
945 else if (gimple_assign_rhs_code (stmt
) == COND_EXPR
)
946 reexamine
= cond_expr_object_size (osi
, var
, stmt
);
947 else if (gimple_assign_single_p (stmt
)
948 || gimple_assign_unary_nop_p (stmt
))
950 if (TREE_CODE (rhs
) == SSA_NAME
951 && POINTER_TYPE_P (TREE_TYPE (rhs
)))
952 reexamine
= merge_object_sizes (osi
, var
, rhs
, 0);
954 expr_object_size (osi
, var
, rhs
);
957 unknown_object_size (osi
, var
);
963 gcall
*call_stmt
= as_a
<gcall
*> (stmt
);
964 tree arg
= pass_through_call (call_stmt
);
967 if (TREE_CODE (arg
) == SSA_NAME
968 && POINTER_TYPE_P (TREE_TYPE (arg
)))
969 reexamine
= merge_object_sizes (osi
, var
, arg
, 0);
971 expr_object_size (osi
, var
, arg
);
974 call_object_size (osi
, var
, call_stmt
);
979 /* Pointers defined by __asm__ statements can point anywhere. */
980 object_sizes
[object_size_type
][varno
] = unknown
[object_size_type
];
984 if (SSA_NAME_VAR (var
)
985 && TREE_CODE (SSA_NAME_VAR (var
)) == PARM_DECL
)
986 expr_object_size (osi
, var
, SSA_NAME_VAR (var
));
988 /* Uninitialized SSA names point nowhere. */
989 object_sizes
[object_size_type
][varno
] = unknown
[object_size_type
];
996 for (i
= 0; i
< gimple_phi_num_args (stmt
); i
++)
998 tree rhs
= gimple_phi_arg (stmt
, i
)->def
;
1000 if (object_sizes
[object_size_type
][varno
]
1001 == unknown
[object_size_type
])
1004 if (TREE_CODE (rhs
) == SSA_NAME
)
1005 reexamine
|= merge_object_sizes (osi
, var
, rhs
, 0);
1006 else if (osi
->pass
== 0)
1007 expr_object_size (osi
, var
, rhs
);
1017 || object_sizes
[object_size_type
][varno
] == unknown
[object_size_type
])
1019 bitmap_set_bit (computed
[object_size_type
], varno
);
1020 bitmap_clear_bit (osi
->reexamine
, varno
);
1024 bitmap_set_bit (osi
->reexamine
, varno
);
1025 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1027 fprintf (dump_file
, "Need to reexamine ");
1028 print_generic_expr (dump_file
, var
, dump_flags
);
1029 fprintf (dump_file
, "\n");
1035 /* Helper function for check_for_plus_in_loops. Called recursively
1039 check_for_plus_in_loops_1 (struct object_size_info
*osi
, tree var
,
1042 gimple
*stmt
= SSA_NAME_DEF_STMT (var
);
1043 unsigned int varno
= SSA_NAME_VERSION (var
);
1045 if (osi
->depths
[varno
])
1047 if (osi
->depths
[varno
] != depth
)
1051 /* Found a loop involving pointer addition. */
1052 for (sp
= osi
->tos
; sp
> osi
->stack
; )
1055 bitmap_clear_bit (osi
->reexamine
, *sp
);
1056 bitmap_set_bit (computed
[osi
->object_size_type
], *sp
);
1057 object_sizes
[osi
->object_size_type
][*sp
] = 0;
1064 else if (! bitmap_bit_p (osi
->reexamine
, varno
))
1067 osi
->depths
[varno
] = depth
;
1068 *osi
->tos
++ = varno
;
1070 switch (gimple_code (stmt
))
1075 if ((gimple_assign_single_p (stmt
)
1076 || gimple_assign_unary_nop_p (stmt
))
1077 && TREE_CODE (gimple_assign_rhs1 (stmt
)) == SSA_NAME
)
1079 tree rhs
= gimple_assign_rhs1 (stmt
);
1081 check_for_plus_in_loops_1 (osi
, rhs
, depth
);
1083 else if (gimple_assign_rhs_code (stmt
) == POINTER_PLUS_EXPR
)
1085 tree basevar
= gimple_assign_rhs1 (stmt
);
1086 tree cst
= gimple_assign_rhs2 (stmt
);
1088 gcc_assert (TREE_CODE (cst
) == INTEGER_CST
);
1090 check_for_plus_in_loops_1 (osi
, basevar
,
1091 depth
+ !integer_zerop (cst
));
1100 gcall
*call_stmt
= as_a
<gcall
*> (stmt
);
1101 tree arg
= pass_through_call (call_stmt
);
1104 if (TREE_CODE (arg
) == SSA_NAME
)
1105 check_for_plus_in_loops_1 (osi
, arg
, depth
);
1116 for (i
= 0; i
< gimple_phi_num_args (stmt
); i
++)
1118 tree rhs
= gimple_phi_arg (stmt
, i
)->def
;
1120 if (TREE_CODE (rhs
) == SSA_NAME
)
1121 check_for_plus_in_loops_1 (osi
, rhs
, depth
);
1130 osi
->depths
[varno
] = 0;
1135 /* Check if some pointer we are computing object size of is being increased
1136 within a loop. If yes, assume all the SSA variables participating in
1137 that loop have minimum object sizes 0. */
1140 check_for_plus_in_loops (struct object_size_info
*osi
, tree var
)
1142 gimple
*stmt
= SSA_NAME_DEF_STMT (var
);
1144 /* NOTE: In the pre-tuples code, we handled a CALL_EXPR here,
1145 and looked for a POINTER_PLUS_EXPR in the pass-through
1146 argument, if any. In GIMPLE, however, such an expression
1147 is not a valid call operand. */
1149 if (is_gimple_assign (stmt
)
1150 && gimple_assign_rhs_code (stmt
) == POINTER_PLUS_EXPR
)
1152 tree basevar
= gimple_assign_rhs1 (stmt
);
1153 tree cst
= gimple_assign_rhs2 (stmt
);
1155 gcc_assert (TREE_CODE (cst
) == INTEGER_CST
);
1157 if (integer_zerop (cst
))
1160 osi
->depths
[SSA_NAME_VERSION (basevar
)] = 1;
1161 *osi
->tos
++ = SSA_NAME_VERSION (basevar
);
1162 check_for_plus_in_loops_1 (osi
, var
, 2);
1163 osi
->depths
[SSA_NAME_VERSION (basevar
)] = 0;
1169 /* Initialize data structures for the object size computation. */
1172 init_object_sizes (void)
1174 int object_size_type
;
1179 for (object_size_type
= 0; object_size_type
<= 3; object_size_type
++)
1181 object_sizes
[object_size_type
].safe_grow (num_ssa_names
);
1182 computed
[object_size_type
] = BITMAP_ALLOC (NULL
);
1185 init_offset_limit ();
1189 /* Destroy data structures after the object size computation. */
1192 fini_object_sizes (void)
1194 int object_size_type
;
1196 for (object_size_type
= 0; object_size_type
<= 3; object_size_type
++)
1198 object_sizes
[object_size_type
].release ();
1199 BITMAP_FREE (computed
[object_size_type
]);
1204 /* Simple pass to optimize all __builtin_object_size () builtins. */
1208 const pass_data pass_data_object_sizes
=
1210 GIMPLE_PASS
, /* type */
1212 OPTGROUP_NONE
, /* optinfo_flags */
1213 TV_NONE
, /* tv_id */
1214 ( PROP_cfg
| PROP_ssa
), /* properties_required */
1215 0, /* properties_provided */
1216 0, /* properties_destroyed */
1217 0, /* todo_flags_start */
1218 0, /* todo_flags_finish */
1221 class pass_object_sizes
: public gimple_opt_pass
1224 pass_object_sizes (gcc::context
*ctxt
)
1225 : gimple_opt_pass (pass_data_object_sizes
, ctxt
)
1228 /* opt_pass methods: */
1229 opt_pass
* clone () { return new pass_object_sizes (m_ctxt
); }
1230 virtual unsigned int execute (function
*);
1232 }; // class pass_object_sizes
1234 /* Dummy valueize function. */
1237 do_valueize (tree t
)
1243 pass_object_sizes::execute (function
*fun
)
1246 FOR_EACH_BB_FN (bb
, fun
)
1248 gimple_stmt_iterator i
;
1249 for (i
= gsi_start_bb (bb
); !gsi_end_p (i
); gsi_next (&i
))
1252 gimple
*call
= gsi_stmt (i
);
1253 if (!gimple_call_builtin_p (call
, BUILT_IN_OBJECT_SIZE
))
1256 init_object_sizes ();
1258 /* In the first pass instance, only attempt to fold
1259 __builtin_object_size (x, 1) and __builtin_object_size (x, 3),
1260 and rather than folding the builtin to the constant if any,
1261 create a MIN_EXPR or MAX_EXPR of the __builtin_object_size
1262 call result and the computed constant. */
1263 if (first_pass_instance
)
1265 tree ost
= gimple_call_arg (call
, 1);
1266 if (tree_fits_uhwi_p (ost
))
1268 unsigned HOST_WIDE_INT object_size_type
= tree_to_uhwi (ost
);
1269 tree ptr
= gimple_call_arg (call
, 0);
1270 tree lhs
= gimple_call_lhs (call
);
1271 if ((object_size_type
== 1 || object_size_type
== 3)
1272 && (TREE_CODE (ptr
) == ADDR_EXPR
1273 || TREE_CODE (ptr
) == SSA_NAME
)
1276 tree type
= TREE_TYPE (lhs
);
1277 unsigned HOST_WIDE_INT bytes
1278 = compute_builtin_object_size (ptr
, object_size_type
);
1279 if (bytes
!= (unsigned HOST_WIDE_INT
) (object_size_type
== 1
1281 && wi::fits_to_tree_p (bytes
, type
))
1283 tree tem
= make_ssa_name (type
);
1284 gimple_call_set_lhs (call
, tem
);
1286 = object_size_type
== 1 ? MIN_EXPR
: MAX_EXPR
;
1287 tree cst
= build_int_cstu (type
, bytes
);
1289 = gimple_build_assign (lhs
, code
, tem
, cst
);
1290 gsi_insert_after (&i
, g
, GSI_NEW_STMT
);
1298 tree lhs
= gimple_call_lhs (call
);
1302 result
= gimple_fold_stmt_to_constant (call
, do_valueize
);
1305 tree ost
= gimple_call_arg (call
, 1);
1307 if (tree_fits_uhwi_p (ost
))
1309 unsigned HOST_WIDE_INT object_size_type
= tree_to_uhwi (ost
);
1311 if (object_size_type
< 2)
1312 result
= fold_convert (size_type_node
,
1313 integer_minus_one_node
);
1314 else if (object_size_type
< 4)
1315 result
= build_zero_cst (size_type_node
);
1322 gcc_assert (TREE_CODE (result
) == INTEGER_CST
);
1324 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1326 fprintf (dump_file
, "Simplified\n ");
1327 print_gimple_stmt (dump_file
, call
, 0, dump_flags
);
1328 fprintf (dump_file
, " to ");
1329 print_generic_expr (dump_file
, result
, 0);
1330 fprintf (dump_file
, "\n");
1333 /* Propagate into all uses and fold those stmts. */
1334 replace_uses_by (lhs
, result
);
1338 fini_object_sizes ();
1345 make_pass_object_sizes (gcc::context
*ctxt
)
1347 return new pass_object_sizes (ctxt
);