1 /* __builtin_object_size (ptr, object_size_type) computation
2 Copyright (C) 2004-2014 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"
26 #include "tree-object-size.h"
27 #include "diagnostic-core.h"
28 #include "gimple-pretty-print.h"
30 #include "basic-block.h"
31 #include "tree-ssa-alias.h"
32 #include "internal-fn.h"
33 #include "gimple-fold.h"
34 #include "gimple-expr.h"
37 #include "gimple-iterator.h"
38 #include "gimple-ssa.h"
39 #include "stringpool.h"
40 #include "tree-ssanames.h"
41 #include "tree-pass.h"
42 #include "tree-ssa-propagate.h"
43 #include "tree-phinodes.h"
44 #include "ssa-iterators.h"
47 struct object_size_info
50 bitmap visited
, reexamine
;
54 unsigned int *stack
, *tos
;
57 static const unsigned HOST_WIDE_INT unknown
[4] = { -1, -1, 0, 0 };
59 static tree
compute_object_offset (const_tree
, const_tree
);
60 static unsigned HOST_WIDE_INT
addr_object_size (struct object_size_info
*,
62 static unsigned HOST_WIDE_INT
alloc_object_size (const_gimple_call
, int);
63 static tree
pass_through_call (const_gimple_call
);
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 unsigned HOST_WIDE_INT
);
68 static bool plus_stmt_object_size (struct object_size_info
*, tree
, gimple
);
69 static bool cond_expr_object_size (struct object_size_info
*, tree
, gimple
);
70 static void init_offset_limit (void);
71 static void check_for_plus_in_loops (struct object_size_info
*, tree
);
72 static void check_for_plus_in_loops_1 (struct object_size_info
*, tree
,
75 /* object_sizes[0] is upper bound for number of bytes till the end of
77 object_sizes[1] is upper bound for number of bytes till the end of
78 the subobject (innermost array or field with address taken).
79 object_sizes[2] is lower bound for number of bytes till the end of
80 the object and object_sizes[3] lower bound for subobject. */
81 static vec
<unsigned HOST_WIDE_INT
> object_sizes
[4];
83 /* Bitmaps what object sizes have been computed already. */
84 static bitmap computed
[4];
86 /* Maximum value of offset we consider to be addition. */
87 static unsigned HOST_WIDE_INT offset_limit
;
90 /* Initialize OFFSET_LIMIT variable. */
92 init_offset_limit (void)
94 if (tree_fits_uhwi_p (TYPE_MAX_VALUE (sizetype
)))
95 offset_limit
= tree_to_uhwi (TYPE_MAX_VALUE (sizetype
));
102 /* Compute offset of EXPR within VAR. Return error_mark_node
106 compute_object_offset (const_tree expr
, const_tree var
)
108 enum tree_code code
= PLUS_EXPR
;
112 return size_zero_node
;
114 switch (TREE_CODE (expr
))
117 base
= compute_object_offset (TREE_OPERAND (expr
, 0), var
);
118 if (base
== error_mark_node
)
121 t
= TREE_OPERAND (expr
, 1);
122 off
= size_binop (PLUS_EXPR
, DECL_FIELD_OFFSET (t
),
123 size_int (tree_to_uhwi (DECL_FIELD_BIT_OFFSET (t
))
129 case VIEW_CONVERT_EXPR
:
130 case NON_LVALUE_EXPR
:
131 return compute_object_offset (TREE_OPERAND (expr
, 0), var
);
134 base
= compute_object_offset (TREE_OPERAND (expr
, 0), var
);
135 if (base
== error_mark_node
)
138 off
= TYPE_SIZE_UNIT (TREE_TYPE (expr
));
142 base
= compute_object_offset (TREE_OPERAND (expr
, 0), var
);
143 if (base
== error_mark_node
)
146 t
= TREE_OPERAND (expr
, 1);
147 if (TREE_CODE (t
) == INTEGER_CST
&& tree_int_cst_sgn (t
) < 0)
150 t
= fold_build1 (NEGATE_EXPR
, TREE_TYPE (t
), t
);
152 t
= fold_convert (sizetype
, t
);
153 off
= size_binop (MULT_EXPR
, TYPE_SIZE_UNIT (TREE_TYPE (expr
)), t
);
157 gcc_assert (TREE_CODE (TREE_OPERAND (expr
, 0)) == ADDR_EXPR
);
158 return wide_int_to_tree (sizetype
, mem_ref_offset (expr
));
161 return error_mark_node
;
164 return size_binop (code
, base
, off
);
168 /* Compute __builtin_object_size for PTR, which is a ADDR_EXPR.
169 OBJECT_SIZE_TYPE is the second argument from __builtin_object_size.
170 If unknown, return unknown[object_size_type]. */
172 static unsigned HOST_WIDE_INT
173 addr_object_size (struct object_size_info
*osi
, const_tree ptr
,
174 int object_size_type
)
176 tree pt_var
, pt_var_size
= NULL_TREE
, var_size
, bytes
;
178 gcc_assert (TREE_CODE (ptr
) == ADDR_EXPR
);
180 pt_var
= TREE_OPERAND (ptr
, 0);
181 while (handled_component_p (pt_var
))
182 pt_var
= TREE_OPERAND (pt_var
, 0);
185 && TREE_CODE (pt_var
) == MEM_REF
)
187 unsigned HOST_WIDE_INT sz
;
189 if (!osi
|| (object_size_type
& 1) != 0
190 || TREE_CODE (TREE_OPERAND (pt_var
, 0)) != SSA_NAME
)
192 sz
= compute_builtin_object_size (TREE_OPERAND (pt_var
, 0),
193 object_size_type
& ~1);
197 tree var
= TREE_OPERAND (pt_var
, 0);
199 collect_object_sizes_for (osi
, var
);
200 if (bitmap_bit_p (computed
[object_size_type
],
201 SSA_NAME_VERSION (var
)))
202 sz
= object_sizes
[object_size_type
][SSA_NAME_VERSION (var
)];
204 sz
= unknown
[object_size_type
];
206 if (sz
!= unknown
[object_size_type
])
208 offset_int dsz
= wi::sub (sz
, mem_ref_offset (pt_var
));
211 else if (wi::fits_uhwi_p (dsz
))
214 sz
= unknown
[object_size_type
];
217 if (sz
!= unknown
[object_size_type
] && sz
< offset_limit
)
218 pt_var_size
= size_int (sz
);
222 && tree_fits_uhwi_p (DECL_SIZE_UNIT (pt_var
))
223 && tree_to_uhwi (DECL_SIZE_UNIT (pt_var
)) < offset_limit
)
224 pt_var_size
= DECL_SIZE_UNIT (pt_var
);
226 && TREE_CODE (pt_var
) == STRING_CST
227 && TYPE_SIZE_UNIT (TREE_TYPE (pt_var
))
228 && tree_fits_uhwi_p (TYPE_SIZE_UNIT (TREE_TYPE (pt_var
)))
229 && tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (pt_var
)))
231 pt_var_size
= TYPE_SIZE_UNIT (TREE_TYPE (pt_var
));
233 return unknown
[object_size_type
];
235 if (pt_var
!= TREE_OPERAND (ptr
, 0))
239 if (object_size_type
& 1)
241 var
= TREE_OPERAND (ptr
, 0);
244 && TREE_CODE (var
) != BIT_FIELD_REF
245 && TREE_CODE (var
) != COMPONENT_REF
246 && TREE_CODE (var
) != ARRAY_REF
247 && TREE_CODE (var
) != ARRAY_RANGE_REF
248 && TREE_CODE (var
) != REALPART_EXPR
249 && TREE_CODE (var
) != IMAGPART_EXPR
)
250 var
= TREE_OPERAND (var
, 0);
251 if (var
!= pt_var
&& TREE_CODE (var
) == ARRAY_REF
)
252 var
= TREE_OPERAND (var
, 0);
253 if (! TYPE_SIZE_UNIT (TREE_TYPE (var
))
254 || ! tree_fits_uhwi_p (TYPE_SIZE_UNIT (TREE_TYPE (var
)))
256 && tree_int_cst_lt (pt_var_size
,
257 TYPE_SIZE_UNIT (TREE_TYPE (var
)))))
259 else if (var
!= pt_var
&& TREE_CODE (pt_var
) == MEM_REF
)
262 /* For &X->fld, compute object size only if fld isn't the last
263 field, as struct { int i; char c[1]; } is often used instead
264 of flexible array member. */
265 while (v
&& v
!= pt_var
)
266 switch (TREE_CODE (v
))
269 if (TYPE_SIZE_UNIT (TREE_TYPE (TREE_OPERAND (v
, 0)))
270 && TREE_CODE (TREE_OPERAND (v
, 1)) == INTEGER_CST
)
273 = TYPE_DOMAIN (TREE_TYPE (TREE_OPERAND (v
, 0)));
275 && TYPE_MAX_VALUE (domain
)
276 && TREE_CODE (TYPE_MAX_VALUE (domain
))
278 && tree_int_cst_lt (TREE_OPERAND (v
, 1),
279 TYPE_MAX_VALUE (domain
)))
285 v
= TREE_OPERAND (v
, 0);
292 if (TREE_CODE (TREE_TYPE (v
)) != ARRAY_TYPE
)
297 while (v
!= pt_var
&& TREE_CODE (v
) == COMPONENT_REF
)
298 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (v
, 0)))
300 && TREE_CODE (TREE_TYPE (TREE_OPERAND (v
, 0)))
304 v
= TREE_OPERAND (v
, 0);
305 if (TREE_CODE (v
) == COMPONENT_REF
306 && TREE_CODE (TREE_TYPE (TREE_OPERAND (v
, 0)))
309 tree fld_chain
= DECL_CHAIN (TREE_OPERAND (v
, 1));
310 for (; fld_chain
; fld_chain
= DECL_CHAIN (fld_chain
))
311 if (TREE_CODE (fld_chain
) == FIELD_DECL
)
319 v
= TREE_OPERAND (v
, 0);
321 while (v
!= pt_var
&& TREE_CODE (v
) == COMPONENT_REF
)
322 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (v
, 0)))
324 && TREE_CODE (TREE_TYPE (TREE_OPERAND (v
, 0)))
328 v
= TREE_OPERAND (v
, 0);
346 var_size
= TYPE_SIZE_UNIT (TREE_TYPE (var
));
347 else if (!pt_var_size
)
348 return unknown
[object_size_type
];
350 var_size
= pt_var_size
;
351 bytes
= compute_object_offset (TREE_OPERAND (ptr
, 0), var
);
352 if (bytes
!= error_mark_node
)
354 if (TREE_CODE (bytes
) == INTEGER_CST
355 && tree_int_cst_lt (var_size
, bytes
))
356 bytes
= size_zero_node
;
358 bytes
= size_binop (MINUS_EXPR
, var_size
, bytes
);
362 && TREE_CODE (pt_var
) == MEM_REF
363 && bytes
!= error_mark_node
)
365 tree bytes2
= compute_object_offset (TREE_OPERAND (ptr
, 0), pt_var
);
366 if (bytes2
!= error_mark_node
)
368 if (TREE_CODE (bytes2
) == INTEGER_CST
369 && tree_int_cst_lt (pt_var_size
, bytes2
))
370 bytes2
= size_zero_node
;
372 bytes2
= size_binop (MINUS_EXPR
, pt_var_size
, bytes2
);
373 bytes
= size_binop (MIN_EXPR
, bytes
, bytes2
);
377 else if (!pt_var_size
)
378 return unknown
[object_size_type
];
382 if (tree_fits_uhwi_p (bytes
))
383 return tree_to_uhwi (bytes
);
385 return unknown
[object_size_type
];
389 /* Compute __builtin_object_size for CALL, which is a GIMPLE_CALL.
390 Handles various allocation calls. OBJECT_SIZE_TYPE is the second
391 argument from __builtin_object_size. If unknown, return
392 unknown[object_size_type]. */
394 static unsigned HOST_WIDE_INT
395 alloc_object_size (const_gimple_call call
, int object_size_type
)
397 tree callee
, bytes
= NULL_TREE
;
399 int arg1
= -1, arg2
= -1;
401 gcc_assert (is_gimple_call (call
));
403 callee
= gimple_call_fndecl (call
);
405 return unknown
[object_size_type
];
407 alloc_size
= lookup_attribute ("alloc_size",
408 TYPE_ATTRIBUTES (TREE_TYPE (callee
)));
409 if (alloc_size
&& TREE_VALUE (alloc_size
))
411 tree p
= TREE_VALUE (alloc_size
);
413 arg1
= TREE_INT_CST_LOW (TREE_VALUE (p
))-1;
415 arg2
= TREE_INT_CST_LOW (TREE_VALUE (TREE_CHAIN (p
)))-1;
418 if (DECL_BUILT_IN_CLASS (callee
) == BUILT_IN_NORMAL
)
419 switch (DECL_FUNCTION_CODE (callee
))
421 case BUILT_IN_CALLOC
:
424 case BUILT_IN_MALLOC
:
425 case BUILT_IN_ALLOCA
:
426 case BUILT_IN_ALLOCA_WITH_ALIGN
:
432 if (arg1
< 0 || arg1
>= (int)gimple_call_num_args (call
)
433 || TREE_CODE (gimple_call_arg (call
, arg1
)) != INTEGER_CST
435 && (arg2
>= (int)gimple_call_num_args (call
)
436 || TREE_CODE (gimple_call_arg (call
, arg2
)) != INTEGER_CST
)))
437 return unknown
[object_size_type
];
440 bytes
= size_binop (MULT_EXPR
,
441 fold_convert (sizetype
, gimple_call_arg (call
, arg1
)),
442 fold_convert (sizetype
, gimple_call_arg (call
, arg2
)));
444 bytes
= fold_convert (sizetype
, gimple_call_arg (call
, arg1
));
446 if (bytes
&& tree_fits_uhwi_p (bytes
))
447 return tree_to_uhwi (bytes
);
449 return unknown
[object_size_type
];
453 /* If object size is propagated from one of function's arguments directly
454 to its return value, return that argument for GIMPLE_CALL statement CALL.
455 Otherwise return NULL. */
458 pass_through_call (const_gimple_call call
)
460 tree callee
= gimple_call_fndecl (call
);
463 && DECL_BUILT_IN_CLASS (callee
) == BUILT_IN_NORMAL
)
464 switch (DECL_FUNCTION_CODE (callee
))
466 case BUILT_IN_MEMCPY
:
467 case BUILT_IN_MEMMOVE
:
468 case BUILT_IN_MEMSET
:
469 case BUILT_IN_STRCPY
:
470 case BUILT_IN_STRNCPY
:
471 case BUILT_IN_STRCAT
:
472 case BUILT_IN_STRNCAT
:
473 case BUILT_IN_MEMCPY_CHK
:
474 case BUILT_IN_MEMMOVE_CHK
:
475 case BUILT_IN_MEMSET_CHK
:
476 case BUILT_IN_STRCPY_CHK
:
477 case BUILT_IN_STRNCPY_CHK
:
478 case BUILT_IN_STPNCPY_CHK
:
479 case BUILT_IN_STRCAT_CHK
:
480 case BUILT_IN_STRNCAT_CHK
:
481 case BUILT_IN_ASSUME_ALIGNED
:
482 if (gimple_call_num_args (call
) >= 1)
483 return gimple_call_arg (call
, 0);
493 /* Compute __builtin_object_size value for PTR. OBJECT_SIZE_TYPE is the
494 second argument from __builtin_object_size. */
496 unsigned HOST_WIDE_INT
497 compute_builtin_object_size (tree ptr
, int object_size_type
)
499 gcc_assert (object_size_type
>= 0 && object_size_type
<= 3);
502 init_offset_limit ();
504 if (TREE_CODE (ptr
) == ADDR_EXPR
)
505 return addr_object_size (NULL
, ptr
, object_size_type
);
507 if (TREE_CODE (ptr
) == SSA_NAME
508 && POINTER_TYPE_P (TREE_TYPE (ptr
))
509 && computed
[object_size_type
] != NULL
)
511 if (!bitmap_bit_p (computed
[object_size_type
], SSA_NAME_VERSION (ptr
)))
513 struct object_size_info osi
;
517 if (num_ssa_names
> object_sizes
[object_size_type
].length ())
518 object_sizes
[object_size_type
].safe_grow (num_ssa_names
);
521 fprintf (dump_file
, "Computing %s %sobject size for ",
522 (object_size_type
& 2) ? "minimum" : "maximum",
523 (object_size_type
& 1) ? "sub" : "");
524 print_generic_expr (dump_file
, ptr
, dump_flags
);
525 fprintf (dump_file
, ":\n");
528 osi
.visited
= BITMAP_ALLOC (NULL
);
529 osi
.reexamine
= BITMAP_ALLOC (NULL
);
530 osi
.object_size_type
= object_size_type
;
535 /* First pass: walk UD chains, compute object sizes that
536 can be computed. osi.reexamine bitmap at the end will
537 contain what variables were found in dependency cycles
538 and therefore need to be reexamined. */
541 collect_object_sizes_for (&osi
, ptr
);
543 /* Second pass: keep recomputing object sizes of variables
544 that need reexamination, until no object sizes are
545 increased or all object sizes are computed. */
546 if (! bitmap_empty_p (osi
.reexamine
))
548 bitmap reexamine
= BITMAP_ALLOC (NULL
);
550 /* If looking for minimum instead of maximum object size,
551 detect cases where a pointer is increased in a loop.
552 Although even without this detection pass 2 would eventually
553 terminate, it could take a long time. If a pointer is
554 increasing this way, we need to assume 0 object size.
555 E.g. p = &buf[0]; while (cond) p = p + 4; */
556 if (object_size_type
& 2)
558 osi
.depths
= XCNEWVEC (unsigned int, num_ssa_names
);
559 osi
.stack
= XNEWVEC (unsigned int, num_ssa_names
);
562 /* collect_object_sizes_for is changing
563 osi.reexamine bitmap, so iterate over a copy. */
564 bitmap_copy (reexamine
, osi
.reexamine
);
565 EXECUTE_IF_SET_IN_BITMAP (reexamine
, 0, i
, bi
)
566 if (bitmap_bit_p (osi
.reexamine
, i
))
567 check_for_plus_in_loops (&osi
, ssa_name (i
));
580 /* collect_object_sizes_for is changing
581 osi.reexamine bitmap, so iterate over a copy. */
582 bitmap_copy (reexamine
, osi
.reexamine
);
583 EXECUTE_IF_SET_IN_BITMAP (reexamine
, 0, i
, bi
)
584 if (bitmap_bit_p (osi
.reexamine
, i
))
586 collect_object_sizes_for (&osi
, ssa_name (i
));
587 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
589 fprintf (dump_file
, "Reexamining ");
590 print_generic_expr (dump_file
, ssa_name (i
),
592 fprintf (dump_file
, "\n");
598 BITMAP_FREE (reexamine
);
600 EXECUTE_IF_SET_IN_BITMAP (osi
.reexamine
, 0, i
, bi
)
601 bitmap_set_bit (computed
[object_size_type
], i
);
603 /* Debugging dumps. */
606 EXECUTE_IF_SET_IN_BITMAP (osi
.visited
, 0, i
, bi
)
607 if (object_sizes
[object_size_type
][i
]
608 != unknown
[object_size_type
])
610 print_generic_expr (dump_file
, ssa_name (i
),
613 ": %s %sobject size "
614 HOST_WIDE_INT_PRINT_UNSIGNED
"\n",
615 (object_size_type
& 2) ? "minimum" : "maximum",
616 (object_size_type
& 1) ? "sub" : "",
617 object_sizes
[object_size_type
][i
]);
621 BITMAP_FREE (osi
.reexamine
);
622 BITMAP_FREE (osi
.visited
);
625 return object_sizes
[object_size_type
][SSA_NAME_VERSION (ptr
)];
628 return unknown
[object_size_type
];
631 /* Compute object_sizes for PTR, defined to VALUE, which is not an SSA_NAME. */
634 expr_object_size (struct object_size_info
*osi
, tree ptr
, tree value
)
636 int object_size_type
= osi
->object_size_type
;
637 unsigned int varno
= SSA_NAME_VERSION (ptr
);
638 unsigned HOST_WIDE_INT bytes
;
640 gcc_assert (object_sizes
[object_size_type
][varno
]
641 != unknown
[object_size_type
]);
642 gcc_assert (osi
->pass
== 0);
644 if (TREE_CODE (value
) == WITH_SIZE_EXPR
)
645 value
= TREE_OPERAND (value
, 0);
647 /* Pointer variables should have been handled by merge_object_sizes. */
648 gcc_assert (TREE_CODE (value
) != SSA_NAME
649 || !POINTER_TYPE_P (TREE_TYPE (value
)));
651 if (TREE_CODE (value
) == ADDR_EXPR
)
652 bytes
= addr_object_size (osi
, value
, object_size_type
);
654 bytes
= unknown
[object_size_type
];
656 if ((object_size_type
& 2) == 0)
658 if (object_sizes
[object_size_type
][varno
] < bytes
)
659 object_sizes
[object_size_type
][varno
] = bytes
;
663 if (object_sizes
[object_size_type
][varno
] > bytes
)
664 object_sizes
[object_size_type
][varno
] = bytes
;
669 /* Compute object_sizes for PTR, defined to the result of a call. */
672 call_object_size (struct object_size_info
*osi
, tree ptr
, gimple_call call
)
674 int object_size_type
= osi
->object_size_type
;
675 unsigned int varno
= SSA_NAME_VERSION (ptr
);
676 unsigned HOST_WIDE_INT bytes
;
678 gcc_assert (is_gimple_call (call
));
680 gcc_assert (object_sizes
[object_size_type
][varno
]
681 != unknown
[object_size_type
]);
682 gcc_assert (osi
->pass
== 0);
684 bytes
= alloc_object_size (call
, object_size_type
);
686 if ((object_size_type
& 2) == 0)
688 if (object_sizes
[object_size_type
][varno
] < bytes
)
689 object_sizes
[object_size_type
][varno
] = bytes
;
693 if (object_sizes
[object_size_type
][varno
] > bytes
)
694 object_sizes
[object_size_type
][varno
] = bytes
;
699 /* Compute object_sizes for PTR, defined to an unknown value. */
702 unknown_object_size (struct object_size_info
*osi
, tree ptr
)
704 int object_size_type
= osi
->object_size_type
;
705 unsigned int varno
= SSA_NAME_VERSION (ptr
);
706 unsigned HOST_WIDE_INT bytes
;
708 gcc_assert (object_sizes
[object_size_type
][varno
]
709 != unknown
[object_size_type
]);
710 gcc_assert (osi
->pass
== 0);
712 bytes
= unknown
[object_size_type
];
714 if ((object_size_type
& 2) == 0)
716 if (object_sizes
[object_size_type
][varno
] < bytes
)
717 object_sizes
[object_size_type
][varno
] = bytes
;
721 if (object_sizes
[object_size_type
][varno
] > bytes
)
722 object_sizes
[object_size_type
][varno
] = bytes
;
727 /* Merge object sizes of ORIG + OFFSET into DEST. Return true if
728 the object size might need reexamination later. */
731 merge_object_sizes (struct object_size_info
*osi
, tree dest
, tree orig
,
732 unsigned HOST_WIDE_INT offset
)
734 int object_size_type
= osi
->object_size_type
;
735 unsigned int varno
= SSA_NAME_VERSION (dest
);
736 unsigned HOST_WIDE_INT orig_bytes
;
738 if (object_sizes
[object_size_type
][varno
] == unknown
[object_size_type
])
740 if (offset
>= offset_limit
)
742 object_sizes
[object_size_type
][varno
] = unknown
[object_size_type
];
747 collect_object_sizes_for (osi
, orig
);
749 orig_bytes
= object_sizes
[object_size_type
][SSA_NAME_VERSION (orig
)];
750 if (orig_bytes
!= unknown
[object_size_type
])
751 orig_bytes
= (offset
> orig_bytes
)
752 ? (unsigned HOST_WIDE_INT
) 0 : orig_bytes
- offset
;
754 if ((object_size_type
& 2) == 0)
756 if (object_sizes
[object_size_type
][varno
] < orig_bytes
)
758 object_sizes
[object_size_type
][varno
] = orig_bytes
;
764 if (object_sizes
[object_size_type
][varno
] > orig_bytes
)
766 object_sizes
[object_size_type
][varno
] = orig_bytes
;
770 return bitmap_bit_p (osi
->reexamine
, SSA_NAME_VERSION (orig
));
774 /* Compute object_sizes for VAR, defined to the result of an assignment
775 with operator POINTER_PLUS_EXPR. Return true if the object size might
776 need reexamination later. */
779 plus_stmt_object_size (struct object_size_info
*osi
, tree var
, gimple stmt
)
781 int object_size_type
= osi
->object_size_type
;
782 unsigned int varno
= SSA_NAME_VERSION (var
);
783 unsigned HOST_WIDE_INT bytes
;
786 if (gimple_assign_rhs_code (stmt
) == POINTER_PLUS_EXPR
)
788 op0
= gimple_assign_rhs1 (stmt
);
789 op1
= gimple_assign_rhs2 (stmt
);
791 else if (gimple_assign_rhs_code (stmt
) == ADDR_EXPR
)
793 tree rhs
= TREE_OPERAND (gimple_assign_rhs1 (stmt
), 0);
794 gcc_assert (TREE_CODE (rhs
) == MEM_REF
);
795 op0
= TREE_OPERAND (rhs
, 0);
796 op1
= TREE_OPERAND (rhs
, 1);
801 if (object_sizes
[object_size_type
][varno
] == unknown
[object_size_type
])
804 /* Handle PTR + OFFSET here. */
805 if (TREE_CODE (op1
) == INTEGER_CST
806 && (TREE_CODE (op0
) == SSA_NAME
807 || TREE_CODE (op0
) == ADDR_EXPR
))
809 if (! tree_fits_uhwi_p (op1
))
810 bytes
= unknown
[object_size_type
];
811 else if (TREE_CODE (op0
) == SSA_NAME
)
812 return merge_object_sizes (osi
, var
, op0
, tree_to_uhwi (op1
));
815 unsigned HOST_WIDE_INT off
= tree_to_uhwi (op1
);
817 /* op0 will be ADDR_EXPR here. */
818 bytes
= addr_object_size (osi
, op0
, object_size_type
);
819 if (bytes
== unknown
[object_size_type
])
821 else if (off
> offset_limit
)
822 bytes
= unknown
[object_size_type
];
823 else if (off
> bytes
)
830 bytes
= unknown
[object_size_type
];
832 if ((object_size_type
& 2) == 0)
834 if (object_sizes
[object_size_type
][varno
] < bytes
)
835 object_sizes
[object_size_type
][varno
] = bytes
;
839 if (object_sizes
[object_size_type
][varno
] > bytes
)
840 object_sizes
[object_size_type
][varno
] = bytes
;
846 /* Compute object_sizes for VAR, defined at STMT, which is
847 a COND_EXPR. Return true if the object size might need reexamination
851 cond_expr_object_size (struct object_size_info
*osi
, tree var
, gimple stmt
)
854 int object_size_type
= osi
->object_size_type
;
855 unsigned int varno
= SSA_NAME_VERSION (var
);
856 bool reexamine
= false;
858 gcc_assert (gimple_assign_rhs_code (stmt
) == COND_EXPR
);
860 if (object_sizes
[object_size_type
][varno
] == unknown
[object_size_type
])
863 then_
= gimple_assign_rhs2 (stmt
);
864 else_
= gimple_assign_rhs3 (stmt
);
866 if (TREE_CODE (then_
) == SSA_NAME
)
867 reexamine
|= merge_object_sizes (osi
, var
, then_
, 0);
869 expr_object_size (osi
, var
, then_
);
871 if (TREE_CODE (else_
) == SSA_NAME
)
872 reexamine
|= merge_object_sizes (osi
, var
, else_
, 0);
874 expr_object_size (osi
, var
, else_
);
879 /* Compute object sizes for VAR.
880 For ADDR_EXPR an object size is the number of remaining bytes
881 to the end of the object (where what is considered an object depends on
882 OSI->object_size_type).
883 For allocation GIMPLE_CALL like malloc or calloc object size is the size
885 For POINTER_PLUS_EXPR where second operand is a constant integer,
886 object size is object size of the first operand minus the constant.
887 If the constant is bigger than the number of remaining bytes until the
888 end of the object, object size is 0, but if it is instead a pointer
889 subtraction, object size is unknown[object_size_type].
890 To differentiate addition from subtraction, ADDR_EXPR returns
891 unknown[object_size_type] for all objects bigger than half of the address
892 space, and constants less than half of the address space are considered
893 addition, while bigger constants subtraction.
894 For a memcpy like GIMPLE_CALL that always returns one of its arguments, the
895 object size is object size of that argument.
896 Otherwise, object size is the maximum of object sizes of variables
897 that it might be set to. */
900 collect_object_sizes_for (struct object_size_info
*osi
, tree var
)
902 int object_size_type
= osi
->object_size_type
;
903 unsigned int varno
= SSA_NAME_VERSION (var
);
907 if (bitmap_bit_p (computed
[object_size_type
], varno
))
912 if (bitmap_set_bit (osi
->visited
, varno
))
914 object_sizes
[object_size_type
][varno
]
915 = (object_size_type
& 2) ? -1 : 0;
919 /* Found a dependency loop. Mark the variable for later
921 bitmap_set_bit (osi
->reexamine
, varno
);
922 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
924 fprintf (dump_file
, "Found a dependency loop at ");
925 print_generic_expr (dump_file
, var
, dump_flags
);
926 fprintf (dump_file
, "\n");
932 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
934 fprintf (dump_file
, "Visiting use-def links for ");
935 print_generic_expr (dump_file
, var
, dump_flags
);
936 fprintf (dump_file
, "\n");
939 stmt
= SSA_NAME_DEF_STMT (var
);
942 switch (gimple_code (stmt
))
946 tree rhs
= gimple_assign_rhs1 (stmt
);
947 if (gimple_assign_rhs_code (stmt
) == POINTER_PLUS_EXPR
948 || (gimple_assign_rhs_code (stmt
) == ADDR_EXPR
949 && TREE_CODE (TREE_OPERAND (rhs
, 0)) == MEM_REF
))
950 reexamine
= plus_stmt_object_size (osi
, var
, stmt
);
951 else if (gimple_assign_rhs_code (stmt
) == COND_EXPR
)
952 reexamine
= cond_expr_object_size (osi
, var
, stmt
);
953 else if (gimple_assign_single_p (stmt
)
954 || gimple_assign_unary_nop_p (stmt
))
956 if (TREE_CODE (rhs
) == SSA_NAME
957 && POINTER_TYPE_P (TREE_TYPE (rhs
)))
958 reexamine
= merge_object_sizes (osi
, var
, rhs
, 0);
960 expr_object_size (osi
, var
, rhs
);
963 unknown_object_size (osi
, var
);
969 gimple_call call_stmt
= as_a
<gimple_call
> (stmt
);
970 tree arg
= pass_through_call (call_stmt
);
973 if (TREE_CODE (arg
) == SSA_NAME
974 && POINTER_TYPE_P (TREE_TYPE (arg
)))
975 reexamine
= merge_object_sizes (osi
, var
, arg
, 0);
977 expr_object_size (osi
, var
, arg
);
980 call_object_size (osi
, var
, call_stmt
);
985 /* Pointers defined by __asm__ statements can point anywhere. */
986 object_sizes
[object_size_type
][varno
] = unknown
[object_size_type
];
990 if (SSA_NAME_VAR (var
)
991 && TREE_CODE (SSA_NAME_VAR (var
)) == PARM_DECL
)
992 expr_object_size (osi
, var
, SSA_NAME_VAR (var
));
994 /* Uninitialized SSA names point nowhere. */
995 object_sizes
[object_size_type
][varno
] = unknown
[object_size_type
];
1002 for (i
= 0; i
< gimple_phi_num_args (stmt
); i
++)
1004 tree rhs
= gimple_phi_arg (stmt
, i
)->def
;
1006 if (object_sizes
[object_size_type
][varno
]
1007 == unknown
[object_size_type
])
1010 if (TREE_CODE (rhs
) == SSA_NAME
)
1011 reexamine
|= merge_object_sizes (osi
, var
, rhs
, 0);
1012 else if (osi
->pass
== 0)
1013 expr_object_size (osi
, var
, rhs
);
1023 || object_sizes
[object_size_type
][varno
] == unknown
[object_size_type
])
1025 bitmap_set_bit (computed
[object_size_type
], varno
);
1026 bitmap_clear_bit (osi
->reexamine
, varno
);
1030 bitmap_set_bit (osi
->reexamine
, varno
);
1031 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1033 fprintf (dump_file
, "Need to reexamine ");
1034 print_generic_expr (dump_file
, var
, dump_flags
);
1035 fprintf (dump_file
, "\n");
1041 /* Helper function for check_for_plus_in_loops. Called recursively
1045 check_for_plus_in_loops_1 (struct object_size_info
*osi
, tree var
,
1048 gimple stmt
= SSA_NAME_DEF_STMT (var
);
1049 unsigned int varno
= SSA_NAME_VERSION (var
);
1051 if (osi
->depths
[varno
])
1053 if (osi
->depths
[varno
] != depth
)
1057 /* Found a loop involving pointer addition. */
1058 for (sp
= osi
->tos
; sp
> osi
->stack
; )
1061 bitmap_clear_bit (osi
->reexamine
, *sp
);
1062 bitmap_set_bit (computed
[osi
->object_size_type
], *sp
);
1063 object_sizes
[osi
->object_size_type
][*sp
] = 0;
1070 else if (! bitmap_bit_p (osi
->reexamine
, varno
))
1073 osi
->depths
[varno
] = depth
;
1074 *osi
->tos
++ = varno
;
1076 switch (gimple_code (stmt
))
1081 if ((gimple_assign_single_p (stmt
)
1082 || gimple_assign_unary_nop_p (stmt
))
1083 && TREE_CODE (gimple_assign_rhs1 (stmt
)) == SSA_NAME
)
1085 tree rhs
= gimple_assign_rhs1 (stmt
);
1087 check_for_plus_in_loops_1 (osi
, rhs
, depth
);
1089 else if (gimple_assign_rhs_code (stmt
) == POINTER_PLUS_EXPR
)
1091 tree basevar
= gimple_assign_rhs1 (stmt
);
1092 tree cst
= gimple_assign_rhs2 (stmt
);
1094 gcc_assert (TREE_CODE (cst
) == INTEGER_CST
);
1096 check_for_plus_in_loops_1 (osi
, basevar
,
1097 depth
+ !integer_zerop (cst
));
1106 gimple_call call_stmt
= as_a
<gimple_call
> (stmt
);
1107 tree arg
= pass_through_call (call_stmt
);
1110 if (TREE_CODE (arg
) == SSA_NAME
)
1111 check_for_plus_in_loops_1 (osi
, arg
, depth
);
1122 for (i
= 0; i
< gimple_phi_num_args (stmt
); i
++)
1124 tree rhs
= gimple_phi_arg (stmt
, i
)->def
;
1126 if (TREE_CODE (rhs
) == SSA_NAME
)
1127 check_for_plus_in_loops_1 (osi
, rhs
, depth
);
1136 osi
->depths
[varno
] = 0;
1141 /* Check if some pointer we are computing object size of is being increased
1142 within a loop. If yes, assume all the SSA variables participating in
1143 that loop have minimum object sizes 0. */
1146 check_for_plus_in_loops (struct object_size_info
*osi
, tree var
)
1148 gimple stmt
= SSA_NAME_DEF_STMT (var
);
1150 /* NOTE: In the pre-tuples code, we handled a CALL_EXPR here,
1151 and looked for a POINTER_PLUS_EXPR in the pass-through
1152 argument, if any. In GIMPLE, however, such an expression
1153 is not a valid call operand. */
1155 if (is_gimple_assign (stmt
)
1156 && gimple_assign_rhs_code (stmt
) == POINTER_PLUS_EXPR
)
1158 tree basevar
= gimple_assign_rhs1 (stmt
);
1159 tree cst
= gimple_assign_rhs2 (stmt
);
1161 gcc_assert (TREE_CODE (cst
) == INTEGER_CST
);
1163 if (integer_zerop (cst
))
1166 osi
->depths
[SSA_NAME_VERSION (basevar
)] = 1;
1167 *osi
->tos
++ = SSA_NAME_VERSION (basevar
);
1168 check_for_plus_in_loops_1 (osi
, var
, 2);
1169 osi
->depths
[SSA_NAME_VERSION (basevar
)] = 0;
1175 /* Initialize data structures for the object size computation. */
1178 init_object_sizes (void)
1180 int object_size_type
;
1185 for (object_size_type
= 0; object_size_type
<= 3; object_size_type
++)
1187 object_sizes
[object_size_type
].safe_grow (num_ssa_names
);
1188 computed
[object_size_type
] = BITMAP_ALLOC (NULL
);
1191 init_offset_limit ();
1195 /* Destroy data structures after the object size computation. */
1198 fini_object_sizes (void)
1200 int object_size_type
;
1202 for (object_size_type
= 0; object_size_type
<= 3; object_size_type
++)
1204 object_sizes
[object_size_type
].release ();
1205 BITMAP_FREE (computed
[object_size_type
]);
1210 /* Simple pass to optimize all __builtin_object_size () builtins. */
1214 const pass_data pass_data_object_sizes
=
1216 GIMPLE_PASS
, /* type */
1218 OPTGROUP_NONE
, /* optinfo_flags */
1219 TV_NONE
, /* tv_id */
1220 ( PROP_cfg
| PROP_ssa
), /* properties_required */
1221 0, /* properties_provided */
1222 0, /* properties_destroyed */
1223 0, /* todo_flags_start */
1224 0, /* todo_flags_finish */
1227 class pass_object_sizes
: public gimple_opt_pass
1230 pass_object_sizes (gcc::context
*ctxt
)
1231 : gimple_opt_pass (pass_data_object_sizes
, ctxt
)
1234 /* opt_pass methods: */
1235 opt_pass
* clone () { return new pass_object_sizes (m_ctxt
); }
1236 virtual unsigned int execute (function
*);
1238 }; // class pass_object_sizes
1241 pass_object_sizes::execute (function
*fun
)
1244 FOR_EACH_BB_FN (bb
, fun
)
1246 gimple_stmt_iterator i
;
1247 for (i
= gsi_start_bb (bb
); !gsi_end_p (i
); gsi_next (&i
))
1250 gimple call
= gsi_stmt (i
);
1251 if (!gimple_call_builtin_p (call
, BUILT_IN_OBJECT_SIZE
))
1254 init_object_sizes ();
1255 result
= fold_call_stmt (as_a
<gimple_call
> (call
), false);
1258 if (gimple_call_num_args (call
) == 2
1259 && POINTER_TYPE_P (TREE_TYPE (gimple_call_arg (call
, 0))))
1261 tree ost
= gimple_call_arg (call
, 1);
1263 if (tree_fits_uhwi_p (ost
))
1265 unsigned HOST_WIDE_INT object_size_type
1266 = tree_to_uhwi (ost
);
1268 if (object_size_type
< 2)
1269 result
= fold_convert (size_type_node
,
1270 integer_minus_one_node
);
1271 else if (object_size_type
< 4)
1272 result
= build_zero_cst (size_type_node
);
1280 gcc_assert (TREE_CODE (result
) == INTEGER_CST
);
1282 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1284 fprintf (dump_file
, "Simplified\n ");
1285 print_gimple_stmt (dump_file
, call
, 0, dump_flags
);
1286 fprintf (dump_file
, " to ");
1287 print_generic_expr (dump_file
, result
, 0);
1288 fprintf (dump_file
, "\n");
1291 tree lhs
= gimple_call_lhs (call
);
1295 /* Propagate into all uses and fold those stmts. */
1297 imm_use_iterator iter
;
1298 FOR_EACH_IMM_USE_STMT (use_stmt
, iter
, lhs
)
1300 use_operand_p use_p
;
1301 FOR_EACH_IMM_USE_ON_STMT (use_p
, iter
)
1302 SET_USE (use_p
, result
);
1303 gimple_stmt_iterator gsi
= gsi_for_stmt (use_stmt
);
1305 update_stmt (gsi_stmt (gsi
));
1310 fini_object_sizes ();
1317 make_pass_object_sizes (gcc::context
*ctxt
)
1319 return new pass_object_sizes (ctxt
);