1 /* __builtin_object_size (ptr, object_size_type) computation
2 Copyright (C) 2004-2013 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 "diagnostic-core.h"
27 #include "gimple-pretty-print.h"
28 #include "tree-flow.h"
29 #include "tree-pass.h"
30 #include "tree-ssa-propagate.h"
32 struct object_size_info
35 bitmap visited
, reexamine
;
39 unsigned int *stack
, *tos
;
42 static unsigned HOST_WIDE_INT unknown
[4] = { -1, -1, 0, 0 };
44 static tree
compute_object_offset (const_tree
, const_tree
);
45 static unsigned HOST_WIDE_INT
addr_object_size (struct object_size_info
*,
47 static unsigned HOST_WIDE_INT
alloc_object_size (const_gimple
, int);
48 static tree
pass_through_call (const_gimple
);
49 static void collect_object_sizes_for (struct object_size_info
*, tree
);
50 static void expr_object_size (struct object_size_info
*, tree
, tree
);
51 static bool merge_object_sizes (struct object_size_info
*, tree
, tree
,
52 unsigned HOST_WIDE_INT
);
53 static bool plus_stmt_object_size (struct object_size_info
*, tree
, gimple
);
54 static bool cond_expr_object_size (struct object_size_info
*, tree
, gimple
);
55 static unsigned int compute_object_sizes (void);
56 static void init_offset_limit (void);
57 static void check_for_plus_in_loops (struct object_size_info
*, tree
);
58 static void check_for_plus_in_loops_1 (struct object_size_info
*, tree
,
61 /* object_sizes[0] is upper bound for number of bytes till the end of
63 object_sizes[1] is upper bound for number of bytes till the end of
64 the subobject (innermost array or field with address taken).
65 object_sizes[2] is lower bound for number of bytes till the end of
66 the object and object_sizes[3] lower bound for subobject. */
67 static vec
<unsigned HOST_WIDE_INT
> object_sizes
[4];
69 /* Bitmaps what object sizes have been computed already. */
70 static bitmap computed
[4];
72 /* Maximum value of offset we consider to be addition. */
73 static unsigned HOST_WIDE_INT offset_limit
;
76 /* Initialize OFFSET_LIMIT variable. */
78 init_offset_limit (void)
80 if (host_integerp (TYPE_MAX_VALUE (sizetype
), 1))
81 offset_limit
= tree_low_cst (TYPE_MAX_VALUE (sizetype
), 1);
88 /* Compute offset of EXPR within VAR. Return error_mark_node
92 compute_object_offset (const_tree expr
, const_tree var
)
94 enum tree_code code
= PLUS_EXPR
;
98 return size_zero_node
;
100 switch (TREE_CODE (expr
))
103 base
= compute_object_offset (TREE_OPERAND (expr
, 0), var
);
104 if (base
== error_mark_node
)
107 t
= TREE_OPERAND (expr
, 1);
108 off
= size_binop (PLUS_EXPR
, DECL_FIELD_OFFSET (t
),
109 size_int (tree_low_cst (DECL_FIELD_BIT_OFFSET (t
), 1)
115 case VIEW_CONVERT_EXPR
:
116 case NON_LVALUE_EXPR
:
117 return compute_object_offset (TREE_OPERAND (expr
, 0), var
);
120 base
= compute_object_offset (TREE_OPERAND (expr
, 0), var
);
121 if (base
== error_mark_node
)
124 off
= TYPE_SIZE_UNIT (TREE_TYPE (expr
));
128 base
= compute_object_offset (TREE_OPERAND (expr
, 0), var
);
129 if (base
== error_mark_node
)
132 t
= TREE_OPERAND (expr
, 1);
133 if (TREE_CODE (t
) == INTEGER_CST
&& tree_int_cst_sgn (t
) < 0)
136 t
= fold_build1 (NEGATE_EXPR
, TREE_TYPE (t
), t
);
138 t
= fold_convert (sizetype
, t
);
139 off
= size_binop (MULT_EXPR
, TYPE_SIZE_UNIT (TREE_TYPE (expr
)), t
);
143 gcc_assert (TREE_CODE (TREE_OPERAND (expr
, 0)) == ADDR_EXPR
);
144 return double_int_to_tree (sizetype
, mem_ref_offset (expr
));
147 return error_mark_node
;
150 return size_binop (code
, base
, off
);
154 /* Compute __builtin_object_size for PTR, which is a ADDR_EXPR.
155 OBJECT_SIZE_TYPE is the second argument from __builtin_object_size.
156 If unknown, return unknown[object_size_type]. */
158 static unsigned HOST_WIDE_INT
159 addr_object_size (struct object_size_info
*osi
, const_tree ptr
,
160 int object_size_type
)
162 tree pt_var
, pt_var_size
= NULL_TREE
, var_size
, bytes
;
164 gcc_assert (TREE_CODE (ptr
) == ADDR_EXPR
);
166 pt_var
= TREE_OPERAND (ptr
, 0);
167 while (handled_component_p (pt_var
))
168 pt_var
= TREE_OPERAND (pt_var
, 0);
171 && TREE_CODE (pt_var
) == MEM_REF
)
173 unsigned HOST_WIDE_INT sz
;
175 if (!osi
|| (object_size_type
& 1) != 0
176 || TREE_CODE (TREE_OPERAND (pt_var
, 0)) != SSA_NAME
)
178 sz
= compute_builtin_object_size (TREE_OPERAND (pt_var
, 0),
179 object_size_type
& ~1);
183 tree var
= TREE_OPERAND (pt_var
, 0);
185 collect_object_sizes_for (osi
, var
);
186 if (bitmap_bit_p (computed
[object_size_type
],
187 SSA_NAME_VERSION (var
)))
188 sz
= object_sizes
[object_size_type
][SSA_NAME_VERSION (var
)];
190 sz
= unknown
[object_size_type
];
192 if (sz
!= unknown
[object_size_type
])
194 double_int dsz
= double_int::from_uhwi (sz
) - mem_ref_offset (pt_var
);
195 if (dsz
.is_negative ())
197 else if (dsz
.fits_uhwi ())
200 sz
= unknown
[object_size_type
];
203 if (sz
!= unknown
[object_size_type
] && sz
< offset_limit
)
204 pt_var_size
= size_int (sz
);
208 && host_integerp (DECL_SIZE_UNIT (pt_var
), 1)
209 && (unsigned HOST_WIDE_INT
)
210 tree_low_cst (DECL_SIZE_UNIT (pt_var
), 1) < offset_limit
)
211 pt_var_size
= DECL_SIZE_UNIT (pt_var
);
213 && TREE_CODE (pt_var
) == STRING_CST
214 && TYPE_SIZE_UNIT (TREE_TYPE (pt_var
))
215 && host_integerp (TYPE_SIZE_UNIT (TREE_TYPE (pt_var
)), 1)
216 && (unsigned HOST_WIDE_INT
)
217 tree_low_cst (TYPE_SIZE_UNIT (TREE_TYPE (pt_var
)), 1)
219 pt_var_size
= TYPE_SIZE_UNIT (TREE_TYPE (pt_var
));
221 return unknown
[object_size_type
];
223 if (pt_var
!= TREE_OPERAND (ptr
, 0))
227 if (object_size_type
& 1)
229 var
= TREE_OPERAND (ptr
, 0);
232 && TREE_CODE (var
) != BIT_FIELD_REF
233 && TREE_CODE (var
) != COMPONENT_REF
234 && TREE_CODE (var
) != ARRAY_REF
235 && TREE_CODE (var
) != ARRAY_RANGE_REF
236 && TREE_CODE (var
) != REALPART_EXPR
237 && TREE_CODE (var
) != IMAGPART_EXPR
)
238 var
= TREE_OPERAND (var
, 0);
239 if (var
!= pt_var
&& TREE_CODE (var
) == ARRAY_REF
)
240 var
= TREE_OPERAND (var
, 0);
241 if (! TYPE_SIZE_UNIT (TREE_TYPE (var
))
242 || ! host_integerp (TYPE_SIZE_UNIT (TREE_TYPE (var
)), 1)
244 && tree_int_cst_lt (pt_var_size
,
245 TYPE_SIZE_UNIT (TREE_TYPE (var
)))))
247 else if (var
!= pt_var
&& TREE_CODE (pt_var
) == MEM_REF
)
250 /* For &X->fld, compute object size only if fld isn't the last
251 field, as struct { int i; char c[1]; } is often used instead
252 of flexible array member. */
253 while (v
&& v
!= pt_var
)
254 switch (TREE_CODE (v
))
257 if (TYPE_SIZE_UNIT (TREE_TYPE (TREE_OPERAND (v
, 0)))
258 && TREE_CODE (TREE_OPERAND (v
, 1)) == INTEGER_CST
)
261 = TYPE_DOMAIN (TREE_TYPE (TREE_OPERAND (v
, 0)));
263 && TYPE_MAX_VALUE (domain
)
264 && TREE_CODE (TYPE_MAX_VALUE (domain
))
266 && tree_int_cst_lt (TREE_OPERAND (v
, 1),
267 TYPE_MAX_VALUE (domain
)))
273 v
= TREE_OPERAND (v
, 0);
280 if (TREE_CODE (TREE_TYPE (v
)) != ARRAY_TYPE
)
285 while (v
!= pt_var
&& TREE_CODE (v
) == COMPONENT_REF
)
286 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (v
, 0)))
288 && TREE_CODE (TREE_TYPE (TREE_OPERAND (v
, 0)))
292 v
= TREE_OPERAND (v
, 0);
293 if (TREE_CODE (v
) == COMPONENT_REF
294 && TREE_CODE (TREE_TYPE (TREE_OPERAND (v
, 0)))
297 tree fld_chain
= DECL_CHAIN (TREE_OPERAND (v
, 1));
298 for (; fld_chain
; fld_chain
= DECL_CHAIN (fld_chain
))
299 if (TREE_CODE (fld_chain
) == FIELD_DECL
)
307 v
= TREE_OPERAND (v
, 0);
309 while (v
!= pt_var
&& TREE_CODE (v
) == COMPONENT_REF
)
310 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (v
, 0)))
312 && TREE_CODE (TREE_TYPE (TREE_OPERAND (v
, 0)))
316 v
= TREE_OPERAND (v
, 0);
334 var_size
= TYPE_SIZE_UNIT (TREE_TYPE (var
));
335 else if (!pt_var_size
)
336 return unknown
[object_size_type
];
338 var_size
= pt_var_size
;
339 bytes
= compute_object_offset (TREE_OPERAND (ptr
, 0), var
);
340 if (bytes
!= error_mark_node
)
342 if (TREE_CODE (bytes
) == INTEGER_CST
343 && tree_int_cst_lt (var_size
, bytes
))
344 bytes
= size_zero_node
;
346 bytes
= size_binop (MINUS_EXPR
, var_size
, bytes
);
350 && TREE_CODE (pt_var
) == MEM_REF
351 && bytes
!= error_mark_node
)
353 tree bytes2
= compute_object_offset (TREE_OPERAND (ptr
, 0), pt_var
);
354 if (bytes2
!= error_mark_node
)
356 if (TREE_CODE (bytes2
) == INTEGER_CST
357 && tree_int_cst_lt (pt_var_size
, bytes2
))
358 bytes2
= size_zero_node
;
360 bytes2
= size_binop (MINUS_EXPR
, pt_var_size
, bytes2
);
361 bytes
= size_binop (MIN_EXPR
, bytes
, bytes2
);
365 else if (!pt_var_size
)
366 return unknown
[object_size_type
];
370 if (host_integerp (bytes
, 1))
371 return tree_low_cst (bytes
, 1);
373 return unknown
[object_size_type
];
377 /* Compute __builtin_object_size for CALL, which is a GIMPLE_CALL.
378 Handles various allocation calls. OBJECT_SIZE_TYPE is the second
379 argument from __builtin_object_size. If unknown, return
380 unknown[object_size_type]. */
382 static unsigned HOST_WIDE_INT
383 alloc_object_size (const_gimple call
, int object_size_type
)
385 tree callee
, bytes
= NULL_TREE
;
387 int arg1
= -1, arg2
= -1;
389 gcc_assert (is_gimple_call (call
));
391 callee
= gimple_call_fndecl (call
);
393 return unknown
[object_size_type
];
395 alloc_size
= lookup_attribute ("alloc_size", TYPE_ATTRIBUTES (TREE_TYPE(callee
)));
396 if (alloc_size
&& TREE_VALUE (alloc_size
))
398 tree p
= TREE_VALUE (alloc_size
);
400 arg1
= TREE_INT_CST_LOW (TREE_VALUE (p
))-1;
402 arg2
= TREE_INT_CST_LOW (TREE_VALUE (TREE_CHAIN (p
)))-1;
405 if (DECL_BUILT_IN_CLASS (callee
) == BUILT_IN_NORMAL
)
406 switch (DECL_FUNCTION_CODE (callee
))
408 case BUILT_IN_CALLOC
:
411 case BUILT_IN_MALLOC
:
412 case BUILT_IN_ALLOCA
:
413 case BUILT_IN_ALLOCA_WITH_ALIGN
:
419 if (arg1
< 0 || arg1
>= (int)gimple_call_num_args (call
)
420 || TREE_CODE (gimple_call_arg (call
, arg1
)) != INTEGER_CST
422 && (arg2
>= (int)gimple_call_num_args (call
)
423 || TREE_CODE (gimple_call_arg (call
, arg2
)) != INTEGER_CST
)))
424 return unknown
[object_size_type
];
427 bytes
= size_binop (MULT_EXPR
,
428 fold_convert (sizetype
, gimple_call_arg (call
, arg1
)),
429 fold_convert (sizetype
, gimple_call_arg (call
, arg2
)));
431 bytes
= fold_convert (sizetype
, gimple_call_arg (call
, arg1
));
433 if (bytes
&& host_integerp (bytes
, 1))
434 return tree_low_cst (bytes
, 1);
436 return unknown
[object_size_type
];
440 /* If object size is propagated from one of function's arguments directly
441 to its return value, return that argument for GIMPLE_CALL statement CALL.
442 Otherwise return NULL. */
445 pass_through_call (const_gimple call
)
447 tree callee
= gimple_call_fndecl (call
);
450 && DECL_BUILT_IN_CLASS (callee
) == BUILT_IN_NORMAL
)
451 switch (DECL_FUNCTION_CODE (callee
))
453 case BUILT_IN_MEMCPY
:
454 case BUILT_IN_MEMMOVE
:
455 case BUILT_IN_MEMSET
:
456 case BUILT_IN_STRCPY
:
457 case BUILT_IN_STRNCPY
:
458 case BUILT_IN_STRCAT
:
459 case BUILT_IN_STRNCAT
:
460 case BUILT_IN_MEMCPY_CHK
:
461 case BUILT_IN_MEMMOVE_CHK
:
462 case BUILT_IN_MEMSET_CHK
:
463 case BUILT_IN_STRCPY_CHK
:
464 case BUILT_IN_STRNCPY_CHK
:
465 case BUILT_IN_STPNCPY_CHK
:
466 case BUILT_IN_STRCAT_CHK
:
467 case BUILT_IN_STRNCAT_CHK
:
468 case BUILT_IN_ASSUME_ALIGNED
:
469 if (gimple_call_num_args (call
) >= 1)
470 return gimple_call_arg (call
, 0);
480 /* Compute __builtin_object_size value for PTR. OBJECT_SIZE_TYPE is the
481 second argument from __builtin_object_size. */
483 unsigned HOST_WIDE_INT
484 compute_builtin_object_size (tree ptr
, int object_size_type
)
486 gcc_assert (object_size_type
>= 0 && object_size_type
<= 3);
489 init_offset_limit ();
491 if (TREE_CODE (ptr
) == ADDR_EXPR
)
492 return addr_object_size (NULL
, ptr
, object_size_type
);
494 if (TREE_CODE (ptr
) == SSA_NAME
495 && POINTER_TYPE_P (TREE_TYPE (ptr
))
496 && computed
[object_size_type
] != NULL
)
498 if (!bitmap_bit_p (computed
[object_size_type
], SSA_NAME_VERSION (ptr
)))
500 struct object_size_info osi
;
504 if (num_ssa_names
> object_sizes
[object_size_type
].length ())
505 object_sizes
[object_size_type
].safe_grow (num_ssa_names
);
508 fprintf (dump_file
, "Computing %s %sobject size for ",
509 (object_size_type
& 2) ? "minimum" : "maximum",
510 (object_size_type
& 1) ? "sub" : "");
511 print_generic_expr (dump_file
, ptr
, dump_flags
);
512 fprintf (dump_file
, ":\n");
515 osi
.visited
= BITMAP_ALLOC (NULL
);
516 osi
.reexamine
= BITMAP_ALLOC (NULL
);
517 osi
.object_size_type
= object_size_type
;
522 /* First pass: walk UD chains, compute object sizes that
523 can be computed. osi.reexamine bitmap at the end will
524 contain what variables were found in dependency cycles
525 and therefore need to be reexamined. */
528 collect_object_sizes_for (&osi
, ptr
);
530 /* Second pass: keep recomputing object sizes of variables
531 that need reexamination, until no object sizes are
532 increased or all object sizes are computed. */
533 if (! bitmap_empty_p (osi
.reexamine
))
535 bitmap reexamine
= BITMAP_ALLOC (NULL
);
537 /* If looking for minimum instead of maximum object size,
538 detect cases where a pointer is increased in a loop.
539 Although even without this detection pass 2 would eventually
540 terminate, it could take a long time. If a pointer is
541 increasing this way, we need to assume 0 object size.
542 E.g. p = &buf[0]; while (cond) p = p + 4; */
543 if (object_size_type
& 2)
545 osi
.depths
= XCNEWVEC (unsigned int, num_ssa_names
);
546 osi
.stack
= XNEWVEC (unsigned int, num_ssa_names
);
549 /* collect_object_sizes_for is changing
550 osi.reexamine bitmap, so iterate over a copy. */
551 bitmap_copy (reexamine
, osi
.reexamine
);
552 EXECUTE_IF_SET_IN_BITMAP (reexamine
, 0, i
, bi
)
553 if (bitmap_bit_p (osi
.reexamine
, i
))
554 check_for_plus_in_loops (&osi
, ssa_name (i
));
567 /* collect_object_sizes_for is changing
568 osi.reexamine bitmap, so iterate over a copy. */
569 bitmap_copy (reexamine
, osi
.reexamine
);
570 EXECUTE_IF_SET_IN_BITMAP (reexamine
, 0, i
, bi
)
571 if (bitmap_bit_p (osi
.reexamine
, i
))
573 collect_object_sizes_for (&osi
, ssa_name (i
));
574 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
576 fprintf (dump_file
, "Reexamining ");
577 print_generic_expr (dump_file
, ssa_name (i
),
579 fprintf (dump_file
, "\n");
585 BITMAP_FREE (reexamine
);
587 EXECUTE_IF_SET_IN_BITMAP (osi
.reexamine
, 0, i
, bi
)
588 bitmap_set_bit (computed
[object_size_type
], i
);
590 /* Debugging dumps. */
593 EXECUTE_IF_SET_IN_BITMAP (osi
.visited
, 0, i
, bi
)
594 if (object_sizes
[object_size_type
][i
]
595 != unknown
[object_size_type
])
597 print_generic_expr (dump_file
, ssa_name (i
),
600 ": %s %sobject size "
601 HOST_WIDE_INT_PRINT_UNSIGNED
"\n",
602 (object_size_type
& 2) ? "minimum" : "maximum",
603 (object_size_type
& 1) ? "sub" : "",
604 object_sizes
[object_size_type
][i
]);
608 BITMAP_FREE (osi
.reexamine
);
609 BITMAP_FREE (osi
.visited
);
612 return object_sizes
[object_size_type
][SSA_NAME_VERSION (ptr
)];
615 return unknown
[object_size_type
];
618 /* Compute object_sizes for PTR, defined to VALUE, which is not an SSA_NAME. */
621 expr_object_size (struct object_size_info
*osi
, tree ptr
, tree value
)
623 int object_size_type
= osi
->object_size_type
;
624 unsigned int varno
= SSA_NAME_VERSION (ptr
);
625 unsigned HOST_WIDE_INT bytes
;
627 gcc_assert (object_sizes
[object_size_type
][varno
]
628 != unknown
[object_size_type
]);
629 gcc_assert (osi
->pass
== 0);
631 if (TREE_CODE (value
) == WITH_SIZE_EXPR
)
632 value
= TREE_OPERAND (value
, 0);
634 /* Pointer variables should have been handled by merge_object_sizes. */
635 gcc_assert (TREE_CODE (value
) != SSA_NAME
636 || !POINTER_TYPE_P (TREE_TYPE (value
)));
638 if (TREE_CODE (value
) == ADDR_EXPR
)
639 bytes
= addr_object_size (osi
, value
, object_size_type
);
641 bytes
= unknown
[object_size_type
];
643 if ((object_size_type
& 2) == 0)
645 if (object_sizes
[object_size_type
][varno
] < bytes
)
646 object_sizes
[object_size_type
][varno
] = bytes
;
650 if (object_sizes
[object_size_type
][varno
] > bytes
)
651 object_sizes
[object_size_type
][varno
] = bytes
;
656 /* Compute object_sizes for PTR, defined to the result of a call. */
659 call_object_size (struct object_size_info
*osi
, tree ptr
, gimple call
)
661 int object_size_type
= osi
->object_size_type
;
662 unsigned int varno
= SSA_NAME_VERSION (ptr
);
663 unsigned HOST_WIDE_INT bytes
;
665 gcc_assert (is_gimple_call (call
));
667 gcc_assert (object_sizes
[object_size_type
][varno
]
668 != unknown
[object_size_type
]);
669 gcc_assert (osi
->pass
== 0);
671 bytes
= alloc_object_size (call
, object_size_type
);
673 if ((object_size_type
& 2) == 0)
675 if (object_sizes
[object_size_type
][varno
] < bytes
)
676 object_sizes
[object_size_type
][varno
] = bytes
;
680 if (object_sizes
[object_size_type
][varno
] > bytes
)
681 object_sizes
[object_size_type
][varno
] = bytes
;
686 /* Compute object_sizes for PTR, defined to an unknown value. */
689 unknown_object_size (struct object_size_info
*osi
, tree ptr
)
691 int object_size_type
= osi
->object_size_type
;
692 unsigned int varno
= SSA_NAME_VERSION (ptr
);
693 unsigned HOST_WIDE_INT bytes
;
695 gcc_assert (object_sizes
[object_size_type
][varno
]
696 != unknown
[object_size_type
]);
697 gcc_assert (osi
->pass
== 0);
699 bytes
= unknown
[object_size_type
];
701 if ((object_size_type
& 2) == 0)
703 if (object_sizes
[object_size_type
][varno
] < bytes
)
704 object_sizes
[object_size_type
][varno
] = bytes
;
708 if (object_sizes
[object_size_type
][varno
] > bytes
)
709 object_sizes
[object_size_type
][varno
] = bytes
;
714 /* Merge object sizes of ORIG + OFFSET into DEST. Return true if
715 the object size might need reexamination later. */
718 merge_object_sizes (struct object_size_info
*osi
, tree dest
, tree orig
,
719 unsigned HOST_WIDE_INT offset
)
721 int object_size_type
= osi
->object_size_type
;
722 unsigned int varno
= SSA_NAME_VERSION (dest
);
723 unsigned HOST_WIDE_INT orig_bytes
;
725 if (object_sizes
[object_size_type
][varno
] == unknown
[object_size_type
])
727 if (offset
>= offset_limit
)
729 object_sizes
[object_size_type
][varno
] = unknown
[object_size_type
];
734 collect_object_sizes_for (osi
, orig
);
736 orig_bytes
= object_sizes
[object_size_type
][SSA_NAME_VERSION (orig
)];
737 if (orig_bytes
!= unknown
[object_size_type
])
738 orig_bytes
= (offset
> orig_bytes
)
739 ? (unsigned HOST_WIDE_INT
) 0 : orig_bytes
- offset
;
741 if ((object_size_type
& 2) == 0)
743 if (object_sizes
[object_size_type
][varno
] < orig_bytes
)
745 object_sizes
[object_size_type
][varno
] = orig_bytes
;
751 if (object_sizes
[object_size_type
][varno
] > orig_bytes
)
753 object_sizes
[object_size_type
][varno
] = orig_bytes
;
757 return bitmap_bit_p (osi
->reexamine
, SSA_NAME_VERSION (orig
));
761 /* Compute object_sizes for VAR, defined to the result of an assignment
762 with operator POINTER_PLUS_EXPR. Return true if the object size might
763 need reexamination later. */
766 plus_stmt_object_size (struct object_size_info
*osi
, tree var
, gimple stmt
)
768 int object_size_type
= osi
->object_size_type
;
769 unsigned int varno
= SSA_NAME_VERSION (var
);
770 unsigned HOST_WIDE_INT bytes
;
773 if (gimple_assign_rhs_code (stmt
) == POINTER_PLUS_EXPR
)
775 op0
= gimple_assign_rhs1 (stmt
);
776 op1
= gimple_assign_rhs2 (stmt
);
778 else if (gimple_assign_rhs_code (stmt
) == ADDR_EXPR
)
780 tree rhs
= TREE_OPERAND (gimple_assign_rhs1 (stmt
), 0);
781 gcc_assert (TREE_CODE (rhs
) == MEM_REF
);
782 op0
= TREE_OPERAND (rhs
, 0);
783 op1
= TREE_OPERAND (rhs
, 1);
788 if (object_sizes
[object_size_type
][varno
] == unknown
[object_size_type
])
791 /* Handle PTR + OFFSET here. */
792 if (TREE_CODE (op1
) == INTEGER_CST
793 && (TREE_CODE (op0
) == SSA_NAME
794 || TREE_CODE (op0
) == ADDR_EXPR
))
796 if (! host_integerp (op1
, 1))
797 bytes
= unknown
[object_size_type
];
798 else if (TREE_CODE (op0
) == SSA_NAME
)
799 return merge_object_sizes (osi
, var
, op0
, tree_low_cst (op1
, 1));
802 unsigned HOST_WIDE_INT off
= tree_low_cst (op1
, 1);
804 /* op0 will be ADDR_EXPR here. */
805 bytes
= addr_object_size (osi
, op0
, object_size_type
);
806 if (bytes
== unknown
[object_size_type
])
808 else if (off
> offset_limit
)
809 bytes
= unknown
[object_size_type
];
810 else if (off
> bytes
)
817 bytes
= unknown
[object_size_type
];
819 if ((object_size_type
& 2) == 0)
821 if (object_sizes
[object_size_type
][varno
] < bytes
)
822 object_sizes
[object_size_type
][varno
] = bytes
;
826 if (object_sizes
[object_size_type
][varno
] > bytes
)
827 object_sizes
[object_size_type
][varno
] = bytes
;
833 /* Compute object_sizes for VAR, defined at STMT, which is
834 a COND_EXPR. Return true if the object size might need reexamination
838 cond_expr_object_size (struct object_size_info
*osi
, tree var
, gimple stmt
)
841 int object_size_type
= osi
->object_size_type
;
842 unsigned int varno
= SSA_NAME_VERSION (var
);
843 bool reexamine
= false;
845 gcc_assert (gimple_assign_rhs_code (stmt
) == COND_EXPR
);
847 if (object_sizes
[object_size_type
][varno
] == unknown
[object_size_type
])
850 then_
= gimple_assign_rhs2 (stmt
);
851 else_
= gimple_assign_rhs3 (stmt
);
853 if (TREE_CODE (then_
) == SSA_NAME
)
854 reexamine
|= merge_object_sizes (osi
, var
, then_
, 0);
856 expr_object_size (osi
, var
, then_
);
858 if (TREE_CODE (else_
) == SSA_NAME
)
859 reexamine
|= merge_object_sizes (osi
, var
, else_
, 0);
861 expr_object_size (osi
, var
, else_
);
866 /* Compute object sizes for VAR.
867 For ADDR_EXPR an object size is the number of remaining bytes
868 to the end of the object (where what is considered an object depends on
869 OSI->object_size_type).
870 For allocation GIMPLE_CALL like malloc or calloc object size is the size
872 For POINTER_PLUS_EXPR where second operand is a constant integer,
873 object size is object size of the first operand minus the constant.
874 If the constant is bigger than the number of remaining bytes until the
875 end of the object, object size is 0, but if it is instead a pointer
876 subtraction, object size is unknown[object_size_type].
877 To differentiate addition from subtraction, ADDR_EXPR returns
878 unknown[object_size_type] for all objects bigger than half of the address
879 space, and constants less than half of the address space are considered
880 addition, while bigger constants subtraction.
881 For a memcpy like GIMPLE_CALL that always returns one of its arguments, the
882 object size is object size of that argument.
883 Otherwise, object size is the maximum of object sizes of variables
884 that it might be set to. */
887 collect_object_sizes_for (struct object_size_info
*osi
, tree var
)
889 int object_size_type
= osi
->object_size_type
;
890 unsigned int varno
= SSA_NAME_VERSION (var
);
894 if (bitmap_bit_p (computed
[object_size_type
], varno
))
899 if (bitmap_set_bit (osi
->visited
, varno
))
901 object_sizes
[object_size_type
][varno
]
902 = (object_size_type
& 2) ? -1 : 0;
906 /* Found a dependency loop. Mark the variable for later
908 bitmap_set_bit (osi
->reexamine
, varno
);
909 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
911 fprintf (dump_file
, "Found a dependency loop at ");
912 print_generic_expr (dump_file
, var
, dump_flags
);
913 fprintf (dump_file
, "\n");
919 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
921 fprintf (dump_file
, "Visiting use-def links for ");
922 print_generic_expr (dump_file
, var
, dump_flags
);
923 fprintf (dump_file
, "\n");
926 stmt
= SSA_NAME_DEF_STMT (var
);
929 switch (gimple_code (stmt
))
933 tree rhs
= gimple_assign_rhs1 (stmt
);
934 if (gimple_assign_rhs_code (stmt
) == POINTER_PLUS_EXPR
935 || (gimple_assign_rhs_code (stmt
) == ADDR_EXPR
936 && TREE_CODE (TREE_OPERAND (rhs
, 0)) == MEM_REF
))
937 reexamine
= plus_stmt_object_size (osi
, var
, stmt
);
938 else if (gimple_assign_rhs_code (stmt
) == COND_EXPR
)
939 reexamine
= cond_expr_object_size (osi
, var
, stmt
);
940 else if (gimple_assign_single_p (stmt
)
941 || gimple_assign_unary_nop_p (stmt
))
943 if (TREE_CODE (rhs
) == SSA_NAME
944 && POINTER_TYPE_P (TREE_TYPE (rhs
)))
945 reexamine
= merge_object_sizes (osi
, var
, rhs
, 0);
947 expr_object_size (osi
, var
, rhs
);
950 unknown_object_size (osi
, var
);
956 tree arg
= pass_through_call (stmt
);
959 if (TREE_CODE (arg
) == SSA_NAME
960 && POINTER_TYPE_P (TREE_TYPE (arg
)))
961 reexamine
= merge_object_sizes (osi
, var
, arg
, 0);
963 expr_object_size (osi
, var
, arg
);
966 call_object_size (osi
, var
, stmt
);
971 /* Pointers defined by __asm__ statements can point anywhere. */
972 object_sizes
[object_size_type
][varno
] = unknown
[object_size_type
];
976 if (SSA_NAME_VAR (var
)
977 && TREE_CODE (SSA_NAME_VAR (var
)) == PARM_DECL
)
978 expr_object_size (osi
, var
, SSA_NAME_VAR (var
));
980 /* Uninitialized SSA names point nowhere. */
981 object_sizes
[object_size_type
][varno
] = unknown
[object_size_type
];
988 for (i
= 0; i
< gimple_phi_num_args (stmt
); i
++)
990 tree rhs
= gimple_phi_arg (stmt
, i
)->def
;
992 if (object_sizes
[object_size_type
][varno
]
993 == unknown
[object_size_type
])
996 if (TREE_CODE (rhs
) == SSA_NAME
)
997 reexamine
|= merge_object_sizes (osi
, var
, rhs
, 0);
998 else if (osi
->pass
== 0)
999 expr_object_size (osi
, var
, rhs
);
1009 || object_sizes
[object_size_type
][varno
] == unknown
[object_size_type
])
1011 bitmap_set_bit (computed
[object_size_type
], varno
);
1012 bitmap_clear_bit (osi
->reexamine
, varno
);
1016 bitmap_set_bit (osi
->reexamine
, varno
);
1017 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1019 fprintf (dump_file
, "Need to reexamine ");
1020 print_generic_expr (dump_file
, var
, dump_flags
);
1021 fprintf (dump_file
, "\n");
1027 /* Helper function for check_for_plus_in_loops. Called recursively
1031 check_for_plus_in_loops_1 (struct object_size_info
*osi
, tree var
,
1034 gimple stmt
= SSA_NAME_DEF_STMT (var
);
1035 unsigned int varno
= SSA_NAME_VERSION (var
);
1037 if (osi
->depths
[varno
])
1039 if (osi
->depths
[varno
] != depth
)
1043 /* Found a loop involving pointer addition. */
1044 for (sp
= osi
->tos
; sp
> osi
->stack
; )
1047 bitmap_clear_bit (osi
->reexamine
, *sp
);
1048 bitmap_set_bit (computed
[osi
->object_size_type
], *sp
);
1049 object_sizes
[osi
->object_size_type
][*sp
] = 0;
1056 else if (! bitmap_bit_p (osi
->reexamine
, varno
))
1059 osi
->depths
[varno
] = depth
;
1060 *osi
->tos
++ = varno
;
1062 switch (gimple_code (stmt
))
1067 if ((gimple_assign_single_p (stmt
)
1068 || gimple_assign_unary_nop_p (stmt
))
1069 && TREE_CODE (gimple_assign_rhs1 (stmt
)) == SSA_NAME
)
1071 tree rhs
= gimple_assign_rhs1 (stmt
);
1073 check_for_plus_in_loops_1 (osi
, rhs
, depth
);
1075 else if (gimple_assign_rhs_code (stmt
) == POINTER_PLUS_EXPR
)
1077 tree basevar
= gimple_assign_rhs1 (stmt
);
1078 tree cst
= gimple_assign_rhs2 (stmt
);
1080 gcc_assert (TREE_CODE (cst
) == INTEGER_CST
);
1082 check_for_plus_in_loops_1 (osi
, basevar
,
1083 depth
+ !integer_zerop (cst
));
1092 tree arg
= pass_through_call (stmt
);
1095 if (TREE_CODE (arg
) == SSA_NAME
)
1096 check_for_plus_in_loops_1 (osi
, arg
, depth
);
1107 for (i
= 0; i
< gimple_phi_num_args (stmt
); i
++)
1109 tree rhs
= gimple_phi_arg (stmt
, i
)->def
;
1111 if (TREE_CODE (rhs
) == SSA_NAME
)
1112 check_for_plus_in_loops_1 (osi
, rhs
, depth
);
1121 osi
->depths
[varno
] = 0;
1126 /* Check if some pointer we are computing object size of is being increased
1127 within a loop. If yes, assume all the SSA variables participating in
1128 that loop have minimum object sizes 0. */
1131 check_for_plus_in_loops (struct object_size_info
*osi
, tree var
)
1133 gimple stmt
= SSA_NAME_DEF_STMT (var
);
1135 /* NOTE: In the pre-tuples code, we handled a CALL_EXPR here,
1136 and looked for a POINTER_PLUS_EXPR in the pass-through
1137 argument, if any. In GIMPLE, however, such an expression
1138 is not a valid call operand. */
1140 if (is_gimple_assign (stmt
)
1141 && gimple_assign_rhs_code (stmt
) == POINTER_PLUS_EXPR
)
1143 tree basevar
= gimple_assign_rhs1 (stmt
);
1144 tree cst
= gimple_assign_rhs2 (stmt
);
1146 gcc_assert (TREE_CODE (cst
) == INTEGER_CST
);
1148 if (integer_zerop (cst
))
1151 osi
->depths
[SSA_NAME_VERSION (basevar
)] = 1;
1152 *osi
->tos
++ = SSA_NAME_VERSION (basevar
);
1153 check_for_plus_in_loops_1 (osi
, var
, 2);
1154 osi
->depths
[SSA_NAME_VERSION (basevar
)] = 0;
1160 /* Initialize data structures for the object size computation. */
1163 init_object_sizes (void)
1165 int object_size_type
;
1170 for (object_size_type
= 0; object_size_type
<= 3; object_size_type
++)
1172 object_sizes
[object_size_type
].safe_grow (num_ssa_names
);
1173 computed
[object_size_type
] = BITMAP_ALLOC (NULL
);
1176 init_offset_limit ();
1180 /* Destroy data structures after the object size computation. */
1183 fini_object_sizes (void)
1185 int object_size_type
;
1187 for (object_size_type
= 0; object_size_type
<= 3; object_size_type
++)
1189 object_sizes
[object_size_type
].release ();
1190 BITMAP_FREE (computed
[object_size_type
]);
1195 /* Simple pass to optimize all __builtin_object_size () builtins. */
1198 compute_object_sizes (void)
1203 gimple_stmt_iterator i
;
1204 for (i
= gsi_start_bb (bb
); !gsi_end_p (i
); gsi_next (&i
))
1207 gimple call
= gsi_stmt (i
);
1208 if (!gimple_call_builtin_p (call
, BUILT_IN_OBJECT_SIZE
))
1211 init_object_sizes ();
1212 result
= fold_call_stmt (call
, false);
1215 if (gimple_call_num_args (call
) == 2
1216 && POINTER_TYPE_P (TREE_TYPE (gimple_call_arg (call
, 0))))
1218 tree ost
= gimple_call_arg (call
, 1);
1220 if (host_integerp (ost
, 1))
1222 unsigned HOST_WIDE_INT object_size_type
1223 = tree_low_cst (ost
, 1);
1225 if (object_size_type
< 2)
1226 result
= fold_convert (size_type_node
,
1227 integer_minus_one_node
);
1228 else if (object_size_type
< 4)
1229 result
= build_zero_cst (size_type_node
);
1237 gcc_assert (TREE_CODE (result
) == INTEGER_CST
);
1239 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1241 fprintf (dump_file
, "Simplified\n ");
1242 print_gimple_stmt (dump_file
, call
, 0, dump_flags
);
1243 fprintf (dump_file
, " to ");
1244 print_generic_expr (dump_file
, result
, 0);
1245 fprintf (dump_file
, "\n");
1248 tree lhs
= gimple_call_lhs (call
);
1252 /* Propagate into all uses and fold those stmts. */
1254 imm_use_iterator iter
;
1255 FOR_EACH_IMM_USE_STMT (use_stmt
, iter
, lhs
)
1257 use_operand_p use_p
;
1258 FOR_EACH_IMM_USE_ON_STMT (use_p
, iter
)
1259 SET_USE (use_p
, result
);
1260 gimple_stmt_iterator gsi
= gsi_for_stmt (use_stmt
);
1262 update_stmt (gsi_stmt (gsi
));
1267 fini_object_sizes ();
1271 struct gimple_opt_pass pass_object_sizes
=
1276 OPTGROUP_NONE
, /* optinfo_flags */
1278 compute_object_sizes
, /* execute */
1281 0, /* static_pass_number */
1282 TV_NONE
, /* tv_id */
1283 PROP_cfg
| PROP_ssa
, /* properties_required */
1284 0, /* properties_provided */
1285 0, /* properties_destroyed */
1286 0, /* todo_flags_start */
1287 TODO_verify_ssa
/* todo_flags_finish */