Debian Codegraph @

gcc/tree-object-size.cc

raw
1 /* __builtin_object_size (ptr, object_size_type) computation
2 Copyright (C) 2004-2025 Free Software Foundation, Inc.
3 Contributed by Jakub Jelinek <[email protected]>
4
5 This file is part of GCC.
6
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)
10 any later version.
11
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.
16
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/>. */
20
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "backend.h"
25 #include "tree.h"
26 #include "gimple.h"
27 #include "tree-pass.h"
28 #include "ssa.h"
29 #include "gimple-pretty-print.h"
30 #include "fold-const.h"
31 #include "tree-object-size.h"
32 #include "gimple-iterator.h"
33 #include "gimple-fold.h"
34 #include "tree-cfg.h"
35 #include "tree-dfa.h"
36 #include "stringpool.h"
37 #include "attribs.h"
38 #include "builtins.h"
39 #include "gimplify-me.h"
40 #include "gimplify.h"
41 #include "tree-ssa-dce.h"
42
43 struct object_size_info
44 {
45 int object_size_type;
46 unsigned char pass;
47 bool changed;
48 bitmap visited, reexamine;
49 unsigned int *depths;
50 unsigned int *stack, *tos;
51 };
52
53 struct GTY(()) object_size
54 {
55 /* Estimate of bytes till the end of the object. */
56 tree size;
57 /* Estimate of the size of the whole object. */
58 tree wholesize;
59 };
60
61 static tree compute_object_offset (tree, const_tree);
62 static bool addr_object_size (struct object_size_info *,
63 const_tree, int, tree *, tree *t = NULL);
64 static tree alloc_object_size (const gcall *, int);
65 static tree access_with_size_object_size (const gcall *, int);
66 static tree pass_through_call (const gcall *);
67 static void collect_object_sizes_for (struct object_size_info *, tree);
68 static void expr_object_size (struct object_size_info *, tree, tree);
69 static bool merge_object_sizes (struct object_size_info *, tree, tree);
70 static bool plus_stmt_object_size (struct object_size_info *, tree, gimple *);
71 static bool cond_expr_object_size (struct object_size_info *, tree, gimple *);
72 static void init_offset_limit (void);
73 static void check_for_plus_in_loops (struct object_size_info *, tree);
74 static void check_for_plus_in_loops_1 (struct object_size_info *, tree,
75 unsigned int);
76
77 /* object_sizes[0] is upper bound for the object size and number of bytes till
78 the end of the object.
79 object_sizes[1] is upper bound for the object size and number of bytes till
80 the end of the subobject (innermost array or field with address taken).
81 object_sizes[2] is lower bound for the object size and number of bytes till
82 the end of the object and object_sizes[3] lower bound for subobject.
83
84 For static object sizes, the object size and the bytes till the end of the
85 object are both INTEGER_CST. In the dynamic case, they are finally either a
86 gimple variable or an INTEGER_CST. */
87 static vec<object_size> object_sizes[OST_END];
88
89 /* Bitmaps what object sizes have been computed already. */
90 static bitmap computed[OST_END];
91
92 /* Maximum value of offset we consider to be addition. */
93 static unsigned HOST_WIDE_INT offset_limit;
94
95 /* Tell the generic SSA updater what kind of update is needed after the pass
96 executes. */
97 static unsigned todo;
98
99 /* Return true if VAL represents an initial size for OBJECT_SIZE_TYPE. */
100
101 static inline bool
102 size_initval_p (tree val, int object_size_type)
103 {
104 return ((object_size_type & OST_MINIMUM)
105 ? integer_all_onesp (val) : integer_zerop (val));
106 }
107
108 /* Return true if VAL represents an unknown size for OBJECT_SIZE_TYPE. */
109
110 static inline bool
111 size_unknown_p (tree val, int object_size_type)
112 {
113 return ((object_size_type & OST_MINIMUM)
114 ? integer_zerop (val) : integer_all_onesp (val));
115 }
116
117 /* Return true if VAL represents a valid size for OBJECT_SIZE_TYPE. */
118
119 static inline bool
120 size_valid_p (tree val, int object_size_type)
121 {
122 return ((object_size_type & OST_DYNAMIC) || TREE_CODE (val) == INTEGER_CST);
123 }
124
125 /* Return true if VAL is usable as an object size in the object_sizes
126 vectors. */
127
128 static inline bool
129 size_usable_p (tree val)
130 {
131 return TREE_CODE (val) == SSA_NAME || TREE_CODE (val) == INTEGER_CST;
132 }
133
134 /* Return a tree with initial value for OBJECT_SIZE_TYPE. */
135
136 static inline tree
137 size_initval (int object_size_type)
138 {
139 return ((object_size_type & OST_MINIMUM)
140 ? TYPE_MAX_VALUE (sizetype) : size_zero_node);
141 }
142
143 /* Return a tree with unknown value for OBJECT_SIZE_TYPE. */
144
145 static inline tree
146 size_unknown (int object_size_type)
147 {
148 return ((object_size_type & OST_MINIMUM)
149 ? size_zero_node : TYPE_MAX_VALUE (sizetype));
150 }
151
152 /* Grow object_sizes[OBJECT_SIZE_TYPE] to num_ssa_names. */
153
154 static inline void
155 object_sizes_grow (int object_size_type)
156 {
157 if (num_ssa_names > object_sizes[object_size_type].length ())
158 object_sizes[object_size_type].safe_grow (num_ssa_names, true);
159 }
160
161 /* Release object_sizes[OBJECT_SIZE_TYPE]. */
162
163 static inline void
164 object_sizes_release (int object_size_type)
165 {
166 object_sizes[object_size_type].release ();
167 }
168
169 /* Return true if object_sizes[OBJECT_SIZE_TYPE][VARNO] is unknown. */
170
171 static inline bool
172 object_sizes_unknown_p (int object_size_type, unsigned varno)
173 {
174 return size_unknown_p (object_sizes[object_size_type][varno].size,
175 object_size_type);
176 }
177
178 /* Return the raw size expression for VARNO corresponding to OSI. This returns
179 the TREE_VEC as is and should only be used during gimplification. */
180
181 static inline object_size
182 object_sizes_get_raw (struct object_size_info *osi, unsigned varno)
183 {
184 gcc_assert (osi->pass != 0);
185 return object_sizes[osi->object_size_type][varno];
186 }
187
188 /* Return a size tree for VARNO corresponding to OSI. If WHOLE is true, return
189 the whole object size. Use this for building size expressions based on size
190 of VARNO. */
191
192 static inline tree
193 object_sizes_get (struct object_size_info *osi, unsigned varno,
194 bool whole = false)
195 {
196 tree ret;
197 int object_size_type = osi->object_size_type;
198
199 if (whole)
200 ret = object_sizes[object_size_type][varno].wholesize;
201 else
202 ret = object_sizes[object_size_type][varno].size;
203
204 if (object_size_type & OST_DYNAMIC)
205 {
206 if (TREE_CODE (ret) == MODIFY_EXPR)
207 return TREE_OPERAND (ret, 0);
208 else if (TREE_CODE (ret) == TREE_VEC)
209 return TREE_VEC_ELT (ret, TREE_VEC_LENGTH (ret) - 1);
210 else
211 gcc_checking_assert (size_usable_p (ret));
212 }
213
214 return ret;
215 }
216
217 /* Set size for VARNO corresponding to OSI to VAL. */
218
219 static inline void
220 object_sizes_initialize (struct object_size_info *osi, unsigned varno,
221 tree val, tree wholeval)
222 {
223 int object_size_type = osi->object_size_type;
224
225 object_sizes[object_size_type][varno].size = val;
226 object_sizes[object_size_type][varno].wholesize = wholeval;
227 }
228
229 /* Return a MODIFY_EXPR for cases where SSA and EXPR have the same type. The
230 TREE_VEC is returned only in case of PHI nodes. */
231
232 static tree
233 bundle_sizes (tree name, tree expr)
234 {
235 gcc_checking_assert (TREE_TYPE (name) == sizetype);
236
237 if (TREE_CODE (expr) == TREE_VEC)
238 {
239 TREE_VEC_ELT (expr, TREE_VEC_LENGTH (expr) - 1) = name;
240 return expr;
241 }
242
243 gcc_checking_assert (types_compatible_p (TREE_TYPE (expr), sizetype));
244 return build2 (MODIFY_EXPR, sizetype, name, expr);
245 }
246
247 /* Set size for VARNO corresponding to OSI to VAL if it is the new minimum or
248 maximum. For static sizes, each element of TREE_VEC is always INTEGER_CST
249 throughout the computation. For dynamic sizes, each element may either be a
250 gimple variable, a MODIFY_EXPR or a TREE_VEC. The MODIFY_EXPR is for
251 expressions that need to be gimplified. TREE_VECs are special, they're
252 emitted only for GIMPLE_PHI and the PHI result variable is the last element
253 of the vector. */
254
255 static bool
256 object_sizes_set (struct object_size_info *osi, unsigned varno, tree val,
257 tree wholeval)
258 {
259 int object_size_type = osi->object_size_type;
260 object_size osize = object_sizes[object_size_type][varno];
261 bool changed = true;
262
263 tree oldval = osize.size;
264 tree old_wholeval = osize.wholesize;
265
266 if (object_size_type & OST_DYNAMIC)
267 {
268 if (bitmap_bit_p (osi->reexamine, varno))
269 {
270 val = bundle_sizes (oldval, val);
271 wholeval = bundle_sizes (old_wholeval, wholeval);
272 }
273 else
274 {
275 gcc_checking_assert (size_initval_p (oldval, object_size_type));
276 gcc_checking_assert (size_initval_p (old_wholeval,
277 object_size_type));
278 /* For dynamic object sizes, all object sizes that are not gimple
279 variables will need to be gimplified. */
280 if (wholeval != val && !size_usable_p (wholeval))
281 {
282 bitmap_set_bit (osi->reexamine, varno);
283 wholeval = bundle_sizes (make_ssa_name (sizetype), wholeval);
284 }
285 if (!size_usable_p (val))
286 {
287 bitmap_set_bit (osi->reexamine, varno);
288 tree newval = bundle_sizes (make_ssa_name (sizetype), val);
289 if (val == wholeval)
290 wholeval = newval;
291 val = newval;
292 }
293 /* If the new value is a temporary variable, mark it for
294 reexamination. */
295 else if (TREE_CODE (val) == SSA_NAME && !SSA_NAME_DEF_STMT (val))
296 bitmap_set_bit (osi->reexamine, varno);
297 }
298 }
299 else
300 {
301 enum tree_code code = (object_size_type & OST_MINIMUM
302 ? MIN_EXPR : MAX_EXPR);
303
304 val = size_binop (code, val, oldval);
305 wholeval = size_binop (code, wholeval, old_wholeval);
306 changed = (tree_int_cst_compare (val, oldval) != 0
307 || tree_int_cst_compare (old_wholeval, wholeval) != 0);
308 }
309
310 object_sizes[object_size_type][varno].size = val;
311 object_sizes[object_size_type][varno].wholesize = wholeval;
312
313 return changed;
314 }
315
316 /* Set temporary SSA names for object size and whole size to resolve dependency
317 loops in dynamic size computation. */
318
319 static inline void
320 object_sizes_set_temp (struct object_size_info *osi, unsigned varno)
321 {
322 tree val = object_sizes_get (osi, varno);
323
324 if (size_initval_p (val, osi->object_size_type))
325 {
326 val = make_ssa_name (sizetype);
327 tree wholeval = make_ssa_name (sizetype);
328 object_sizes_set (osi, varno, val, wholeval);
329 }
330 }
331
332 /* Initialize OFFSET_LIMIT variable. */
333 static void
334 init_offset_limit (void)
335 {
336 if (tree_fits_uhwi_p (TYPE_MAX_VALUE (sizetype)))
337 offset_limit = tree_to_uhwi (TYPE_MAX_VALUE (sizetype));
338 else
339 offset_limit = -1;
340 offset_limit /= 2;
341 }
342
343 /* Bytes at end of the object with SZ from offset OFFSET. If WHOLESIZE is not
344 NULL_TREE, use it to get the net offset of the pointer, which should always
345 be positive and hence, be within OFFSET_LIMIT for valid offsets. */
346
347 static tree
348 size_for_offset (tree sz, tree offset, tree wholesize = NULL_TREE,
349 bool strict = true)
350 {
351 gcc_checking_assert (types_compatible_p (TREE_TYPE (sz), sizetype));
352
353 /* For negative offsets, if we have a distinct WHOLESIZE, use it to get a net
354 offset from the whole object. */
355 if (wholesize && wholesize != sz
356 && (TREE_CODE (sz) != INTEGER_CST
357 || TREE_CODE (wholesize) != INTEGER_CST
358 || tree_int_cst_compare (sz, wholesize)))
359 {
360 gcc_checking_assert (types_compatible_p (TREE_TYPE (wholesize),
361 sizetype));
362
363 /* Restructure SZ - OFFSET as
364 WHOLESIZE - (WHOLESIZE + OFFSET - SZ) so that the offset part, i.e.
365 WHOLESIZE + OFFSET - SZ is only allowed to be positive. */
366 tree tmp = size_binop (MAX_EXPR, wholesize, sz);
367 offset = fold_build2 (PLUS_EXPR, sizetype, tmp, offset);
368 offset = fold_build2 (MINUS_EXPR, sizetype, offset, sz);
369 sz = tmp;
370 }
371
372 /* Safe to convert now, since a valid net offset should be non-negative. */
373 if (!useless_type_conversion_p (sizetype, TREE_TYPE (offset)))
374 offset = fold_convert (sizetype, offset);
375
376 if (TREE_CODE (offset) == INTEGER_CST)
377 {
378 if (integer_zerop (offset))
379 return sz;
380
381 /* Negative or too large offset even after adjustment, cannot be within
382 bounds of an object. The exception here is when the base object size
383 has been overestimated (e.g. through PHI nodes or a COND_EXPR) and the
384 adjusted offset remains negative. If the caller wants to be
385 permissive, return the base size. */
386 if (compare_tree_int (offset, offset_limit) > 0)
387 {
388 if (strict)
389 return size_zero_node;
390 else
391 return sz;
392 }
393 }
394
395 return size_binop (MINUS_EXPR, size_binop (MAX_EXPR, sz, offset), offset);
396 }
397
398 /* Compute offset of EXPR within VAR. Return error_mark_node
399 if unknown. */
400
401 static tree
402 compute_object_offset (tree expr, const_tree var)
403 {
404 enum tree_code code = PLUS_EXPR;
405 tree base, off, t;
406
407 if (expr == var)
408 return size_zero_node;
409
410 switch (TREE_CODE (expr))
411 {
412 case COMPONENT_REF:
413 base = compute_object_offset (TREE_OPERAND (expr, 0), var);
414 if (base == error_mark_node)
415 return base;
416
417 t = TREE_OPERAND (expr, 1);
418 off = size_binop (PLUS_EXPR,
419 component_ref_field_offset (expr),
420 size_int (tree_to_uhwi (DECL_FIELD_BIT_OFFSET (t))
421 / BITS_PER_UNIT));
422 break;
423
424 case REALPART_EXPR:
425 CASE_CONVERT:
426 case VIEW_CONVERT_EXPR:
427 case NON_LVALUE_EXPR:
428 return compute_object_offset (TREE_OPERAND (expr, 0), var);
429
430 case IMAGPART_EXPR:
431 base = compute_object_offset (TREE_OPERAND (expr, 0), var);
432 if (base == error_mark_node)
433 return base;
434
435 off = TYPE_SIZE_UNIT (TREE_TYPE (expr));
436 break;
437
438 case ARRAY_REF:
439 base = compute_object_offset (TREE_OPERAND (expr, 0), var);
440 if (base == error_mark_node)
441 return base;
442
443 t = TREE_OPERAND (expr, 1);
444 tree low_bound, unit_size;
445 low_bound = array_ref_low_bound (CONST_CAST_TREE (expr));
446 unit_size = array_ref_element_size (CONST_CAST_TREE (expr));
447 if (! integer_zerop (low_bound))
448 t = fold_build2 (MINUS_EXPR, TREE_TYPE (t), t, low_bound);
449 if (TREE_CODE (t) == INTEGER_CST && tree_int_cst_sgn (t) < 0)
450 {
451 code = MINUS_EXPR;
452 t = fold_build1 (NEGATE_EXPR, TREE_TYPE (t), t);
453 }
454 t = fold_convert (sizetype, t);
455 off = size_binop (MULT_EXPR, unit_size, t);
456 break;
457
458 case MEM_REF:
459 gcc_assert (TREE_CODE (TREE_OPERAND (expr, 0)) == ADDR_EXPR);
460 return wide_int_to_tree (sizetype, mem_ref_offset (expr));
461
462 default:
463 return error_mark_node;
464 }
465
466 return size_binop (code, base, off);
467 }
468
469 /* Return true if CONTAINER has a field of type INNER at OFFSET. */
470
471 static bool
472 inner_at_offset (tree container, tree inner, tree offset)
473 {
474 gcc_assert (RECORD_OR_UNION_TYPE_P (container));
475
476 for (tree t = TYPE_FIELDS (container); t; t = DECL_CHAIN (t))
477 {
478 if (TREE_CODE (t) != FIELD_DECL)
479 continue;
480
481 /* Skip over fields at bit offsets that are not BITS_PER_UNIT aligned
482 to avoid an accidental truncated match with BYTE_POSITION below since
483 the address of such fields cannot be taken. */
484 if (wi::bit_and (wi::to_offset (DECL_FIELD_BIT_OFFSET (t)),
485 BITS_PER_UNIT - 1) != 0)
486 continue;
487
488 tree byte_offset = byte_position (t);
489 if (TREE_CODE (byte_offset) != INTEGER_CST
490 || tree_int_cst_lt (offset, byte_offset))
491 return false;
492
493 /* For an array, check the element type, otherwise the actual type. This
494 deliberately does not support the case of jumping from a pointer to
495 the middle of an array to its containing struct. */
496 tree t_type = TREE_TYPE (t);
497 if (((TREE_CODE (t_type) == ARRAY_TYPE && TREE_TYPE (t_type) == inner)
498 || t_type == inner)
499 && tree_int_cst_equal (byte_offset, offset))
500 return true;
501
502 /* Nested structure or union, adjust the expected offset and dive in. */
503 if (RECORD_OR_UNION_TYPE_P (TREE_TYPE (t))
504 && inner_at_offset (TREE_TYPE (t), inner,
505 fold_build2 (MINUS_EXPR, sizetype, offset,
506 byte_offset)))
507 return true;
508 }
509
510 return false;
511 }
512
513 /* For the input MEMREF of type MEMREF_TYPE, look for the presence of a field
514 of BASE_TYPE at OFFSET and return an adjusted WHOLESIZE if found. */
515
516 static tree
517 get_wholesize_for_memref (tree memref, tree wholesize)
518 {
519 tree base = TREE_OPERAND (memref, 0);
520 tree offset = fold_convert (sizetype, TREE_OPERAND (memref, 1));
521 tree memref_type = TREE_TYPE (memref);
522 tree base_type = TREE_TYPE (base);
523
524 if (POINTER_TYPE_P (base_type))
525 base_type = TREE_TYPE ((base_type));
526
527 if (dump_file && (dump_flags & TDF_DETAILS))
528 {
529 fprintf (dump_file, "wholesize_for_memref: ");
530 print_generic_expr (dump_file, wholesize, dump_flags);
531 fprintf (dump_file, ", offset: ");
532 print_generic_expr (dump_file, offset, dump_flags);
533 fprintf (dump_file, "\n");
534 }
535
536 if (TREE_CODE (offset) != INTEGER_CST
537 || compare_tree_int (offset, offset_limit) < 0
538 || !RECORD_OR_UNION_TYPE_P (memref_type))
539 return wholesize;
540
541 offset = fold_build1 (NEGATE_EXPR, sizetype, offset);
542
543 if (inner_at_offset (memref_type, base_type, offset))
544 wholesize = size_binop (PLUS_EXPR, wholesize, offset);
545
546 if (dump_file && (dump_flags & TDF_DETAILS))
547 {
548 fprintf (dump_file, " new wholesize: ");
549 print_generic_expr (dump_file, wholesize, dump_flags);
550 fprintf (dump_file, "\n");
551 }
552
553 return wholesize;
554 }
555
556 /* Returns the size of the object designated by DECL considering its
557 initializer if it either has one or if it would not affect its size,
558 otherwise the size of the object without the initializer when MIN
559 is true, else null. An object's initializer affects the object's
560 size if it's a struct type with a flexible array member. */
561
562 tree
563 decl_init_size (tree decl, bool min)
564 {
565 tree size = DECL_SIZE_UNIT (decl);
566 tree type = TREE_TYPE (decl);
567 if (TREE_CODE (type) != RECORD_TYPE)
568 return size;
569
570 tree last = last_field (type);
571 if (!last)
572 return size;
573
574 tree last_type = TREE_TYPE (last);
575 if (TREE_CODE (last_type) != ARRAY_TYPE
576 || TYPE_SIZE (last_type))
577 return size;
578
579 /* Use TYPE_SIZE_UNIT; DECL_SIZE_UNIT sometimes reflects the size
580 of the initializer and sometimes doesn't. */
581 size = TYPE_SIZE_UNIT (type);
582 tree ref = build3 (COMPONENT_REF, type, decl, last, NULL_TREE);
583 tree compsize = component_ref_size (ref);
584 if (!compsize)
585 return min ? size : NULL_TREE;
586
587 /* The size includes tail padding and initializer elements. */
588 tree pos = byte_position (last);
589 size = fold_build2 (PLUS_EXPR, TREE_TYPE (size), pos, compsize);
590 return size;
591 }
592
593 /* Compute __builtin_object_size for PTR, which is a ADDR_EXPR.
594 OBJECT_SIZE_TYPE is the second argument from __builtin_object_size.
595 If unknown, return size_unknown (object_size_type). */
596
597 static bool
598 addr_object_size (struct object_size_info *osi, const_tree ptr,
599 int object_size_type, tree *psize, tree *pwholesize)
600 {
601 tree pt_var, pt_var_size = NULL_TREE, pt_var_wholesize = NULL_TREE;
602 tree var_size, bytes, wholebytes;
603
604 gcc_assert (TREE_CODE (ptr) == ADDR_EXPR);
605
606 /* Set to unknown and overwrite just before returning if the size
607 could be determined. */
608 *psize = size_unknown (object_size_type);
609 if (pwholesize)
610 *pwholesize = size_unknown (object_size_type);
611
612 pt_var = TREE_OPERAND (ptr, 0);
613 while (handled_component_p (pt_var))
614 pt_var = TREE_OPERAND (pt_var, 0);
615
616 if (!pt_var)
617 return false;
618
619 if (TREE_CODE (pt_var) == MEM_REF)
620 {
621 tree sz, wholesize;
622
623 if (!osi || (object_size_type & OST_SUBOBJECT) != 0
624 || TREE_CODE (TREE_OPERAND (pt_var, 0)) != SSA_NAME)
625 {
626 compute_builtin_object_size (TREE_OPERAND (pt_var, 0),
627 object_size_type & ~OST_SUBOBJECT, &sz);
628 wholesize = get_wholesize_for_memref (pt_var, sz);
629 }
630 else
631 {
632 tree var = TREE_OPERAND (pt_var, 0);
633 if (osi->pass == 0)
634 collect_object_sizes_for (osi, var);
635 if (bitmap_bit_p (computed[object_size_type],
636 SSA_NAME_VERSION (var)))
637 {
638 sz = object_sizes_get (osi, SSA_NAME_VERSION (var));
639 wholesize = object_sizes_get (osi, SSA_NAME_VERSION (var), true);
640 wholesize = get_wholesize_for_memref (pt_var, wholesize);
641 }
642 else
643 sz = wholesize = size_unknown (object_size_type);
644 }
645 if (!size_unknown_p (sz, object_size_type))
646 sz = size_for_offset (sz, TREE_OPERAND (pt_var, 1), wholesize);
647
648 if (!size_unknown_p (sz, object_size_type)
649 && (TREE_CODE (sz) != INTEGER_CST
650 || compare_tree_int (sz, offset_limit) < 0))
651 {
652 pt_var_size = sz;
653 pt_var_wholesize = wholesize;
654 }
655 }
656 else if (DECL_P (pt_var))
657 {
658 pt_var_size = pt_var_wholesize
659 = decl_init_size (pt_var, object_size_type & OST_MINIMUM);
660 if (!pt_var_size)
661 return false;
662 }
663 else if (TREE_CODE (pt_var) == STRING_CST)
664 pt_var_size = pt_var_wholesize = TYPE_SIZE_UNIT (TREE_TYPE (pt_var));
665 else
666 return false;
667
668 if (pt_var_size)
669 {
670 /* Validate the size determined above if it is a constant. */
671 if (TREE_CODE (pt_var_size) == INTEGER_CST
672 && compare_tree_int (pt_var_size, offset_limit) >= 0)
673 return false;
674 }
675
676 if (pt_var != TREE_OPERAND (ptr, 0))
677 {
678 tree var;
679
680 if (object_size_type & OST_SUBOBJECT)
681 {
682 var = TREE_OPERAND (ptr, 0);
683
684 while (var != pt_var
685 && TREE_CODE (var) != BIT_FIELD_REF
686 && TREE_CODE (var) != COMPONENT_REF
687 && TREE_CODE (var) != ARRAY_REF
688 && TREE_CODE (var) != ARRAY_RANGE_REF
689 && TREE_CODE (var) != REALPART_EXPR
690 && TREE_CODE (var) != IMAGPART_EXPR)
691 var = TREE_OPERAND (var, 0);
692 if (var != pt_var && TREE_CODE (var) == ARRAY_REF)
693 var = TREE_OPERAND (var, 0);
694 if (! TYPE_SIZE_UNIT (TREE_TYPE (var))
695 || ! tree_fits_uhwi_p (TYPE_SIZE_UNIT (TREE_TYPE (var)))
696 || (pt_var_size && TREE_CODE (pt_var_size) == INTEGER_CST
697 && tree_int_cst_lt (pt_var_size,
698 TYPE_SIZE_UNIT (TREE_TYPE (var)))))
699 var = pt_var;
700 else if (var != pt_var && TREE_CODE (pt_var) == MEM_REF)
701 {
702 tree v = var;
703 /* For &X->fld, compute object size if fld isn't a flexible array
704 member. */
705 bool is_flexible_array_mem_ref = false;
706 while (v && v != pt_var)
707 switch (TREE_CODE (v))
708 {
709 case ARRAY_REF:
710 if (TYPE_SIZE_UNIT (TREE_TYPE (TREE_OPERAND (v, 0))))
711 {
712 tree domain
713 = TYPE_DOMAIN (TREE_TYPE (TREE_OPERAND (v, 0)));
714 if (domain && TYPE_MAX_VALUE (domain))
715 {
716 v = NULL_TREE;
717 break;
718 }
719 }
720 v = TREE_OPERAND (v, 0);
721 break;
722 case REALPART_EXPR:
723 case IMAGPART_EXPR:
724 v = NULL_TREE;
725 break;
726 case COMPONENT_REF:
727 /* When the ref is not to an aggregate type, i.e, an array,
728 a record or a union, it will not have flexible size,
729 compute the object size directly. */
730 if (!AGGREGATE_TYPE_P (TREE_TYPE (v)))
731 {
732 v = NULL_TREE;
733 break;
734 }
735 /* if the ref is to a record or union type, but the type
736 does not include a flexible array recursively, compute
737 the object size directly. */
738 if (RECORD_OR_UNION_TYPE_P (TREE_TYPE (v)))
739 {
740 if (!TYPE_INCLUDES_FLEXARRAY (TREE_TYPE (v)))
741 {
742 v = NULL_TREE;
743 break;
744 }
745 else
746 {
747 v = TREE_OPERAND (v, 0);
748 break;
749 }
750 }
751 /* Now the ref is to an array type. */
752 gcc_assert (TREE_CODE (TREE_TYPE (v)) == ARRAY_TYPE);
753 is_flexible_array_mem_ref = array_ref_flexible_size_p (v);
754 while (v != pt_var && TREE_CODE (v) == COMPONENT_REF)
755 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (v, 0)))
756 != UNION_TYPE
757 && TREE_CODE (TREE_TYPE (TREE_OPERAND (v, 0)))
758 != QUAL_UNION_TYPE)
759 break;
760 else
761 v = TREE_OPERAND (v, 0);
762 if (TREE_CODE (v) == COMPONENT_REF
763 && TREE_CODE (TREE_TYPE (TREE_OPERAND (v, 0)))
764 == RECORD_TYPE)
765 {
766 /* compute object size only if v is not a
767 flexible array member. */
768 if (!is_flexible_array_mem_ref)
769 {
770 v = NULL_TREE;
771 break;
772 }
773 v = TREE_OPERAND (v, 0);
774 }
775 while (v != pt_var && TREE_CODE (v) == COMPONENT_REF)
776 if (TREE_CODE (TREE_TYPE (TREE_OPERAND (v, 0)))
777 != UNION_TYPE
778 && TREE_CODE (TREE_TYPE (TREE_OPERAND (v, 0)))
779 != QUAL_UNION_TYPE)
780 break;
781 else
782 v = TREE_OPERAND (v, 0);
783 if (v != pt_var)
784 v = NULL_TREE;
785 else
786 v = pt_var;
787 break;
788 default:
789 v = pt_var;
790 break;
791 }
792 if (v == pt_var)
793 var = pt_var;
794 }
795 }
796 else
797 var = pt_var;
798
799 if (var != pt_var)
800 {
801 var_size = TYPE_SIZE_UNIT (TREE_TYPE (var));
802 if (!TREE_CONSTANT (var_size))
803 var_size = get_or_create_ssa_default_def (cfun, var_size);
804 if (!var_size)
805 return false;
806 }
807 else if (!pt_var_size)
808 return false;
809 else
810 var_size = pt_var_size;
811 bytes = compute_object_offset (TREE_OPERAND (ptr, 0), var);
812 if (bytes != error_mark_node)
813 {
814 bytes = size_for_offset (var_size, bytes);
815 if (var != pt_var && pt_var_size && TREE_CODE (pt_var) == MEM_REF)
816 {
817 tree bytes2 = compute_object_offset (TREE_OPERAND (ptr, 0),
818 pt_var);
819 if (bytes2 != error_mark_node)
820 {
821 bytes2 = size_for_offset (pt_var_size, bytes2);
822 bytes = size_binop (MIN_EXPR, bytes, bytes2);
823 }
824 }
825 }
826 else
827 bytes = size_unknown (object_size_type);
828
829 wholebytes
830 = object_size_type & OST_SUBOBJECT ? var_size : pt_var_wholesize;
831 }
832 else if (!pt_var_size)
833 return false;
834 else
835 {
836 bytes = pt_var_size;
837 wholebytes = pt_var_wholesize;
838 }
839
840 if (!size_unknown_p (bytes, object_size_type)
841 && size_valid_p (bytes, object_size_type)
842 && !size_unknown_p (bytes, object_size_type)
843 && size_valid_p (wholebytes, object_size_type))
844 {
845 *psize = bytes;
846 if (pwholesize)
847 *pwholesize = wholebytes;
848 return true;
849 }
850
851 return false;
852 }
853
854 /* Compute __builtin_object_size for a CALL to .ACCESS_WITH_SIZE,
855 OBJECT_SIZE_TYPE is the second argument from __builtin_object_size.
856 The 2nd, 3rd, and the 4th parameters of the call determine the size of
857 the CALL:
858
859 2nd argument REF_TO_SIZE: The reference to the size of the object,
860 3rd argument CLASS_OF_SIZE: The size referenced by the REF_TO_SIZE represents
861 0: the number of bytes;
862 1: the number of the elements of the object type;
863 4th argument TYPE_OF_SIZE: A constant 0 with its TYPE being the same as the TYPE
864 of the object referenced by REF_TO_SIZE
865 6th argument: A constant 0 with the pointer TYPE to the original flexible
866 array type.
867
868 The size of the element can be retrived from the TYPE of the 6th argument
869 of the call, which is the pointer to the array type. */
870 static tree
871 access_with_size_object_size (const gcall *call, int object_size_type)
872 {
873 /* If not for dynamic object size, return. */
874 if ((object_size_type & OST_DYNAMIC) == 0)
875 return size_unknown (object_size_type);
876
877 gcc_assert (gimple_call_internal_p (call, IFN_ACCESS_WITH_SIZE));
878 /* The type of the 6th argument type is the pointer TYPE to the original
879 flexible array type. */
880 tree pointer_to_array_type = TREE_TYPE (gimple_call_arg (call, 5));
881 gcc_assert (POINTER_TYPE_P (pointer_to_array_type));
882 tree element_type = TREE_TYPE (TREE_TYPE (pointer_to_array_type));
883 tree element_size = TYPE_SIZE_UNIT (element_type);
884 tree ref_to_size = gimple_call_arg (call, 1);
885 unsigned int class_of_size = TREE_INT_CST_LOW (gimple_call_arg (call, 2));
886 tree type = TREE_TYPE (gimple_call_arg (call, 3));
887
888 tree size = fold_build2 (MEM_REF, type, ref_to_size,
889 build_int_cst (ptr_type_node, 0));
890
891 /* If size is negative value, treat it as zero. */
892 if (!TYPE_UNSIGNED (type))
893 {
894 tree cond_expr = fold_build2 (LT_EXPR, boolean_type_node,
895 unshare_expr (size), build_zero_cst (type));
896 size = fold_build3 (COND_EXPR, integer_type_node, cond_expr,
897 build_zero_cst (type), size);
898 }
899
900 if (class_of_size == 1)
901 size = size_binop (MULT_EXPR,
902 fold_convert (sizetype, size),
903 fold_convert (sizetype, element_size));
904 else
905 size = fold_convert (sizetype, size);
906
907 if (!todo)
908 todo = TODO_update_ssa_only_virtuals;
909
910 return size;
911 }
912
913 /* Compute __builtin_object_size for CALL, which is a GIMPLE_CALL.
914 Handles calls to functions declared with attribute alloc_size.
915 OBJECT_SIZE_TYPE is the second argument from __builtin_object_size.
916 If unknown, return size_unknown (object_size_type). */
917
918 static tree
919 alloc_object_size (const gcall *call, int object_size_type)
920 {
921 gcc_assert (is_gimple_call (call));
922
923 tree calltype;
924 tree callfn = gimple_call_fndecl (call);
925 if (callfn)
926 calltype = TREE_TYPE (callfn);
927 else
928 calltype = gimple_call_fntype (call);
929
930 if (!calltype)
931 return size_unknown (object_size_type);
932
933 /* Set to positions of alloc_size arguments. */
934 int arg1 = -1, arg2 = -1;
935 tree alloc_size = lookup_attribute ("alloc_size",
936 TYPE_ATTRIBUTES (calltype));
937 if (alloc_size && TREE_VALUE (alloc_size))
938 {
939 tree p = TREE_VALUE (alloc_size);
940
941 arg1 = TREE_INT_CST_LOW (TREE_VALUE (p))-1;
942 if (TREE_CHAIN (p))
943 arg2 = TREE_INT_CST_LOW (TREE_VALUE (TREE_CHAIN (p)))-1;
944 }
945 else if (gimple_call_builtin_p (call, BUILT_IN_NORMAL)
946 && callfn
947 && ALLOCA_FUNCTION_CODE_P (DECL_FUNCTION_CODE (callfn)))
948 arg1 = 0;
949
950 /* Non-const arguments are OK here, let the caller handle constness. */
951 if (arg1 < 0
952 || (unsigned) arg1 >= gimple_call_num_args (call)
953 || (arg2 >= 0 && (unsigned) arg2 >= gimple_call_num_args (call)))
954 return size_unknown (object_size_type);
955
956 tree targ1 = gimple_call_arg (call, arg1);
957 if (!INTEGRAL_TYPE_P (TREE_TYPE (targ1))
958 || TYPE_PRECISION (TREE_TYPE (targ1)) > TYPE_PRECISION (sizetype))
959 return size_unknown (object_size_type);
960 targ1 = fold_convert (sizetype, targ1);
961 tree bytes = NULL_TREE;
962 if (arg2 >= 0)
963 {
964 tree targ2 = gimple_call_arg (call, arg2);
965 if (!INTEGRAL_TYPE_P (TREE_TYPE (targ2))
966 || TYPE_PRECISION (TREE_TYPE (targ2)) > TYPE_PRECISION (sizetype))
967 return size_unknown (object_size_type);
968 targ2 = fold_convert (sizetype, targ2);
969 bytes = size_binop (MULT_EXPR, targ1, targ2);
970 }
971 else
972 bytes = targ1;
973
974 return bytes ? bytes : size_unknown (object_size_type);
975 }
976
977 /* Compute __builtin_object_size for CALL, which is a call to either
978 BUILT_IN_STRDUP or BUILT_IN_STRNDUP; IS_STRNDUP indicates which it is.
979 OBJECT_SIZE_TYPE is the second argument from __builtin_object_size.
980 If unknown, return size_unknown (object_size_type). */
981
982 static tree
983 strdup_object_size (const gcall *call, int object_size_type, bool is_strndup)
984 {
985 tree src = gimple_call_arg (call, 0);
986 tree sz = size_unknown (object_size_type);
987 tree n = NULL_TREE;
988
989 if (is_strndup)
990 n = fold_build2 (PLUS_EXPR, sizetype, size_one_node,
991 gimple_call_arg (call, 1));
992 /* For strdup, simply emit strlen (SRC) + 1 and let the optimizer fold it the
993 way it likes. */
994 else
995 {
996 tree strlen_fn = builtin_decl_implicit (BUILT_IN_STRLEN);
997 if (strlen_fn)
998 {
999 sz = fold_build2 (PLUS_EXPR, sizetype, size_one_node,
1000 build_call_expr (strlen_fn, 1, src));
1001 todo = TODO_update_ssa_only_virtuals;
1002 }
1003 }
1004
1005 /* In all other cases, return the size of SRC since the object size cannot
1006 exceed that. We cannot do this for OST_MINIMUM unless SRC points into a
1007 string constant since otherwise the object size could go all the way down
1008 to zero. */
1009 if (!size_valid_p (sz, object_size_type)
1010 || size_unknown_p (sz, object_size_type))
1011 {
1012 tree wholesrc = NULL_TREE;
1013 if (TREE_CODE (src) == ADDR_EXPR)
1014 wholesrc = get_base_address (TREE_OPERAND (src, 0));
1015
1016 /* If the source points within a string constant, we try to get its
1017 length. */
1018 if (wholesrc && TREE_CODE (wholesrc) == STRING_CST)
1019 {
1020 tree len = c_strlen (src, 0);
1021 if (len)
1022 sz = fold_build2 (PLUS_EXPR, sizetype, size_one_node, len);
1023 }
1024
1025 /* For maximum estimate, our next best guess is the object size of the
1026 source. */
1027 if (size_unknown_p (sz, object_size_type)
1028 && !(object_size_type & OST_MINIMUM))
1029 compute_builtin_object_size (src, object_size_type, &sz);
1030 }
1031
1032 /* String duplication allocates at least one byte, so we should never fail
1033 for OST_MINIMUM. */
1034 if ((!size_valid_p (sz, object_size_type)
1035 || size_unknown_p (sz, object_size_type))
1036 && (object_size_type & OST_MINIMUM))
1037 sz = size_one_node;
1038
1039 /* Factor in the N. */
1040 return n ? fold_build2 (MIN_EXPR, sizetype, n, sz) : sz;
1041 }
1042
1043 /* If object size is propagated from one of function's arguments directly
1044 to its return value, return that argument for GIMPLE_CALL statement CALL.
1045 Otherwise return NULL. */
1046
1047 static tree
1048 pass_through_call (const gcall *call)
1049 {
1050 unsigned rf = gimple_call_return_flags (call);
1051 if (rf & ERF_RETURNS_ARG)
1052 {
1053 unsigned argnum = rf & ERF_RETURN_ARG_MASK;
1054 if (argnum < gimple_call_num_args (call))
1055 return gimple_call_arg (call, argnum);
1056 }
1057
1058 /* __builtin_assume_aligned is intentionally not marked RET1. */
1059 if (gimple_call_builtin_p (call, BUILT_IN_ASSUME_ALIGNED))
1060 return gimple_call_arg (call, 0);
1061
1062 return NULL_TREE;
1063 }
1064
1065 /* Emit PHI nodes for size expressions fo. */
1066
1067 static void
1068 emit_phi_nodes (gimple *stmt, tree size, tree wholesize)
1069 {
1070 tree phires;
1071 gphi *wholephi = NULL;
1072
1073 if (wholesize != size)
1074 {
1075 phires = TREE_VEC_ELT (wholesize, TREE_VEC_LENGTH (wholesize) - 1);
1076 wholephi = create_phi_node (phires, gimple_bb (stmt));
1077 }
1078
1079 phires = TREE_VEC_ELT (size, TREE_VEC_LENGTH (size) - 1);
1080 gphi *phi = create_phi_node (phires, gimple_bb (stmt));
1081 gphi *obj_phi = as_a <gphi *> (stmt);
1082
1083 gcc_checking_assert (TREE_CODE (wholesize) == TREE_VEC);
1084 gcc_checking_assert (TREE_CODE (size) == TREE_VEC);
1085
1086 for (unsigned i = 0; i < gimple_phi_num_args (stmt); i++)
1087 {
1088 gimple_seq seq = NULL;
1089 tree wsz = TREE_VEC_ELT (wholesize, i);
1090 tree sz = TREE_VEC_ELT (size, i);
1091
1092 /* If we built an expression, we will need to build statements
1093 and insert them on the edge right away. */
1094 if (TREE_CODE (wsz) != SSA_NAME)
1095 wsz = force_gimple_operand (wsz, &seq, true, NULL);
1096 if (TREE_CODE (sz) != SSA_NAME)
1097 {
1098 gimple_seq s;
1099 sz = force_gimple_operand (sz, &s, true, NULL);
1100 gimple_seq_add_seq (&seq, s);
1101 }
1102
1103 if (seq)
1104 gsi_insert_seq_on_edge (gimple_phi_arg_edge (obj_phi, i), seq);
1105
1106 if (wholephi)
1107 add_phi_arg (wholephi, wsz,
1108 gimple_phi_arg_edge (obj_phi, i),
1109 gimple_phi_arg_location (obj_phi, i));
1110
1111 add_phi_arg (phi, sz,
1112 gimple_phi_arg_edge (obj_phi, i),
1113 gimple_phi_arg_location (obj_phi, i));
1114 }
1115 }
1116
1117 /* Descend through EXPR and return size_unknown if it uses any SSA variable
1118 object_size_set or object_size_set_temp generated, which turned out to be
1119 size_unknown, as noted in UNKNOWNS. */
1120
1121 static tree
1122 propagate_unknowns (object_size_info *osi, tree expr, bitmap unknowns)
1123 {
1124 int object_size_type = osi->object_size_type;
1125
1126 switch (TREE_CODE (expr))
1127 {
1128 case SSA_NAME:
1129 if (bitmap_bit_p (unknowns, SSA_NAME_VERSION (expr)))
1130 return size_unknown (object_size_type);
1131 return expr;
1132
1133 case MIN_EXPR:
1134 case MAX_EXPR:
1135 {
1136 tree res = propagate_unknowns (osi, TREE_OPERAND (expr, 0),
1137 unknowns);
1138 if (size_unknown_p (res, object_size_type))
1139 return res;
1140
1141 res = propagate_unknowns (osi, TREE_OPERAND (expr, 1), unknowns);
1142 if (size_unknown_p (res, object_size_type))
1143 return res;
1144
1145 return expr;
1146 }
1147 case MODIFY_EXPR:
1148 {
1149 tree res = propagate_unknowns (osi, TREE_OPERAND (expr, 1),
1150 unknowns);
1151 if (size_unknown_p (res, object_size_type))
1152 return res;
1153 return expr;
1154 }
1155 case TREE_VEC:
1156 for (int i = 0; i < TREE_VEC_LENGTH (expr); i++)
1157 {
1158 tree res = propagate_unknowns (osi, TREE_VEC_ELT (expr, i),
1159 unknowns);
1160 if (size_unknown_p (res, object_size_type))
1161 return res;
1162 }
1163 return expr;
1164 case PLUS_EXPR:
1165 case MINUS_EXPR:
1166 {
1167 tree res = propagate_unknowns (osi, TREE_OPERAND (expr, 0),
1168 unknowns);
1169 if (size_unknown_p (res, object_size_type))
1170 return res;
1171
1172 return expr;
1173 }
1174 default:
1175 return expr;
1176 }
1177 }
1178
1179 /* Walk through size expressions that need reexamination and generate
1180 statements for them. */
1181
1182 static void
1183 gimplify_size_expressions (object_size_info *osi)
1184 {
1185 int object_size_type = osi->object_size_type;
1186 bitmap_iterator bi;
1187 unsigned int i;
1188 bool changed;
1189
1190 /* Step 1: Propagate unknowns into expressions. */
1191 bitmap reexamine = BITMAP_ALLOC (NULL);
1192 bitmap_copy (reexamine, osi->reexamine);
1193 bitmap unknowns = BITMAP_ALLOC (NULL);
1194 do
1195 {
1196 changed = false;
1197 EXECUTE_IF_SET_IN_BITMAP (reexamine, 0, i, bi)
1198 {
1199 object_size cur = object_sizes_get_raw (osi, i);
1200
1201 if (size_unknown_p (propagate_unknowns (osi, cur.size, unknowns),
1202 object_size_type)
1203 || size_unknown_p (propagate_unknowns (osi, cur.wholesize,
1204 unknowns),
1205 object_size_type))
1206 {
1207 /* Record the SSAs we're overwriting to propagate the
1208 unknwons. */
1209 tree oldval = object_sizes_get (osi, i);
1210 tree old_wholeval = object_sizes_get (osi, i, true);
1211
1212 bitmap_set_bit (unknowns, SSA_NAME_VERSION (oldval));
1213 bitmap_set_bit (unknowns, SSA_NAME_VERSION (old_wholeval));
1214 object_sizes_initialize (osi, i,
1215 size_unknown (object_size_type),
1216 size_unknown (object_size_type));
1217 bitmap_clear_bit (osi->reexamine, i);
1218 changed = true;
1219 }
1220 }
1221 bitmap_copy (reexamine, osi->reexamine);
1222 }
1223 while (changed);
1224
1225 /* Release all unknowns. */
1226 EXECUTE_IF_SET_IN_BITMAP (unknowns, 0, i, bi)
1227 release_ssa_name (ssa_name (i));
1228
1229 BITMAP_FREE (unknowns);
1230 BITMAP_FREE (reexamine);
1231
1232 /* Expand all size expressions to put their definitions close to the objects
1233 for which size is being computed. */
1234 EXECUTE_IF_SET_IN_BITMAP (osi->reexamine, 0, i, bi)
1235 {
1236 gimple_seq seq = NULL;
1237 object_size osize = object_sizes_get_raw (osi, i);
1238
1239 gimple *stmt = SSA_NAME_DEF_STMT (ssa_name (i));
1240 enum gimple_code code = gimple_code (stmt);
1241
1242 /* PHI nodes need special attention. */
1243 if (code == GIMPLE_PHI)
1244 emit_phi_nodes (stmt, osize.size, osize.wholesize);
1245 else
1246 {
1247 tree size_expr = NULL_TREE;
1248
1249 /* Bundle wholesize in with the size to gimplify if needed. */
1250 if (osize.wholesize != osize.size
1251 && !size_usable_p (osize.wholesize))
1252 size_expr = size_binop (COMPOUND_EXPR,
1253 osize.wholesize,
1254 osize.size);
1255 else if (!size_usable_p (osize.size))
1256 size_expr = osize.size;
1257
1258 if (size_expr)
1259 {
1260 gimple_stmt_iterator gsi;
1261 if (code == GIMPLE_NOP)
1262 gsi = gsi_start_bb (single_succ (ENTRY_BLOCK_PTR_FOR_FN (cfun)));
1263 else
1264 gsi = gsi_for_stmt (stmt);
1265
1266 force_gimple_operand (size_expr, &seq, true, NULL);
1267 gsi_insert_seq_before (&gsi, seq, GSI_CONTINUE_LINKING);
1268 }
1269 }
1270
1271 /* We're done, so replace the MODIFY_EXPRs with the SSA names. */
1272 object_sizes_initialize (osi, i,
1273 object_sizes_get (osi, i),
1274 object_sizes_get (osi, i, true));
1275 }
1276 }
1277
1278 /* Compute __builtin_object_size value for PTR and set *PSIZE to
1279 the resulting value. If the declared object is known and PDECL
1280 is nonnull, sets *PDECL to the object's DECL. OBJECT_SIZE_TYPE
1281 is the second argument to __builtin_object_size.
1282 Returns true on success and false when the object size could not
1283 be determined. */
1284
1285 bool
1286 compute_builtin_object_size (tree ptr, int object_size_type,
1287 tree *psize)
1288 {
1289 gcc_assert (object_size_type >= 0 && object_size_type < OST_END);
1290
1291 /* Set to unknown and overwrite just before returning if the size
1292 could be determined. */
1293 *psize = size_unknown (object_size_type);
1294
1295 if (! offset_limit)
1296 init_offset_limit ();
1297
1298 if (TREE_CODE (ptr) == ADDR_EXPR)
1299 return addr_object_size (NULL, ptr, object_size_type, psize);
1300
1301 if (TREE_CODE (ptr) != SSA_NAME
1302 || !POINTER_TYPE_P (TREE_TYPE (ptr)))
1303 return false;
1304
1305 if (computed[object_size_type] == NULL)
1306 {
1307 if (optimize || object_size_type & OST_SUBOBJECT)
1308 return false;
1309
1310 /* When not optimizing, rather than failing, make a small effort
1311 to determine the object size without the full benefit of
1312 the (costly) computation below. */
1313 gimple *def = SSA_NAME_DEF_STMT (ptr);
1314 if (gimple_code (def) == GIMPLE_ASSIGN)
1315 {
1316 tree_code code = gimple_assign_rhs_code (def);
1317 if (code == POINTER_PLUS_EXPR)
1318 {
1319 tree offset = gimple_assign_rhs2 (def);
1320 ptr = gimple_assign_rhs1 (def);
1321
1322 if (((object_size_type & OST_DYNAMIC)
1323 || (tree_fits_shwi_p (offset)
1324 && compare_tree_int (offset, offset_limit) <= 0))
1325 && compute_builtin_object_size (ptr, object_size_type,
1326 psize))
1327 {
1328 *psize = size_for_offset (*psize, offset);
1329 return true;
1330 }
1331 }
1332 }
1333 return false;
1334 }
1335
1336 struct object_size_info osi;
1337 osi.object_size_type = object_size_type;
1338 if (!bitmap_bit_p (computed[object_size_type], SSA_NAME_VERSION (ptr)))
1339 {
1340 bitmap_iterator bi;
1341 unsigned int i;
1342
1343 object_sizes_grow (object_size_type);
1344 if (dump_file)
1345 {
1346 fprintf (dump_file, "Computing %s %s%sobject size for ",
1347 (object_size_type & OST_MINIMUM) ? "minimum" : "maximum",
1348 (object_size_type & OST_DYNAMIC) ? "dynamic " : "",
1349 (object_size_type & OST_SUBOBJECT) ? "sub" : "");
1350 print_generic_expr (dump_file, ptr, dump_flags);
1351 fprintf (dump_file, ":\n");
1352 }
1353
1354 osi.visited = BITMAP_ALLOC (NULL);
1355 osi.reexamine = BITMAP_ALLOC (NULL);
1356
1357 if (!(object_size_type & OST_DYNAMIC))
1358 {
1359 osi.depths = NULL;
1360 osi.stack = NULL;
1361 osi.tos = NULL;
1362 }
1363
1364 /* First pass: walk UD chains, compute object sizes that can be computed.
1365 osi.reexamine bitmap at the end will contain versions of SSA_NAMES
1366 that need to be reexamined. For both static and dynamic size
1367 computation, reexamination is for propagation across dependency loops.
1368 The dynamic case has the additional use case where the computed
1369 expression needs to be gimplified. */
1370 osi.pass = 0;
1371 osi.changed = false;
1372 collect_object_sizes_for (&osi, ptr);
1373
1374 if (object_size_type & OST_DYNAMIC)
1375 {
1376 osi.pass = 1;
1377 gimplify_size_expressions (&osi);
1378 bitmap_clear (osi.reexamine);
1379 }
1380
1381 /* Second pass: keep recomputing object sizes of variables
1382 that need reexamination, until no object sizes are
1383 increased or all object sizes are computed. */
1384 if (! bitmap_empty_p (osi.reexamine))
1385 {
1386 bitmap reexamine = BITMAP_ALLOC (NULL);
1387
1388 /* If looking for minimum instead of maximum object size,
1389 detect cases where a pointer is increased in a loop.
1390 Although even without this detection pass 2 would eventually
1391 terminate, it could take a long time. If a pointer is
1392 increasing this way, we need to assume 0 object size.
1393 E.g. p = &buf[0]; while (cond) p = p + 4; */
1394 if (object_size_type & OST_MINIMUM)
1395 {
1396 osi.depths = XCNEWVEC (unsigned int, num_ssa_names);
1397 osi.stack = XNEWVEC (unsigned int, num_ssa_names);
1398 osi.tos = osi.stack;
1399 osi.pass = 1;
1400 /* collect_object_sizes_for is changing
1401 osi.reexamine bitmap, so iterate over a copy. */
1402 bitmap_copy (reexamine, osi.reexamine);
1403 EXECUTE_IF_SET_IN_BITMAP (reexamine, 0, i, bi)
1404 if (bitmap_bit_p (osi.reexamine, i))
1405 check_for_plus_in_loops (&osi, ssa_name (i));
1406
1407 free (osi.depths);
1408 osi.depths = NULL;
1409 free (osi.stack);
1410 osi.stack = NULL;
1411 osi.tos = NULL;
1412 }
1413
1414 do
1415 {
1416 osi.pass = 2;
1417 osi.changed = false;
1418 /* collect_object_sizes_for is changing
1419 osi.reexamine bitmap, so iterate over a copy. */
1420 bitmap_copy (reexamine, osi.reexamine);
1421 EXECUTE_IF_SET_IN_BITMAP (reexamine, 0, i, bi)
1422 if (bitmap_bit_p (osi.reexamine, i))
1423 {
1424 collect_object_sizes_for (&osi, ssa_name (i));
1425 if (dump_file && (dump_flags & TDF_DETAILS))
1426 {
1427 fprintf (dump_file, "Reexamining ");
1428 print_generic_expr (dump_file, ssa_name (i),
1429 dump_flags);
1430 fprintf (dump_file, "\n");
1431 }
1432 }
1433 }
1434 while (osi.changed);
1435
1436 BITMAP_FREE (reexamine);
1437 }
1438 EXECUTE_IF_SET_IN_BITMAP (osi.reexamine, 0, i, bi)
1439 bitmap_set_bit (computed[object_size_type], i);
1440
1441 /* Debugging dumps. */
1442 if (dump_file)
1443 {
1444 EXECUTE_IF_SET_IN_BITMAP (osi.visited, 0, i, bi)
1445 if (!object_sizes_unknown_p (object_size_type, i))
1446 {
1447 print_generic_expr (dump_file, ssa_name (i),
1448 dump_flags);
1449 fprintf (dump_file,
1450 ": %s %s%sobject size ",
1451 ((object_size_type & OST_MINIMUM) ? "minimum"
1452 : "maximum"),
1453 (object_size_type & OST_DYNAMIC) ? "dynamic " : "",
1454 (object_size_type & OST_SUBOBJECT) ? "sub" : "");
1455 print_generic_expr (dump_file, object_sizes_get (&osi, i),
1456 dump_flags);
1457 fprintf (dump_file, "\n");
1458 }
1459 }
1460
1461 BITMAP_FREE (osi.reexamine);
1462 BITMAP_FREE (osi.visited);
1463 }
1464
1465 *psize = object_sizes_get (&osi, SSA_NAME_VERSION (ptr));
1466 return !size_unknown_p (*psize, object_size_type);
1467 }
1468
1469 /* Compute object_sizes for PTR, defined to VALUE, which is not an SSA_NAME. */
1470
1471 static void
1472 expr_object_size (struct object_size_info *osi, tree ptr, tree value)
1473 {
1474 int object_size_type = osi->object_size_type;
1475 unsigned int varno = SSA_NAME_VERSION (ptr);
1476 tree bytes, wholesize;
1477
1478 gcc_assert (!object_sizes_unknown_p (object_size_type, varno));
1479 gcc_assert (osi->pass == 0);
1480
1481 if (TREE_CODE (value) == WITH_SIZE_EXPR)
1482 value = TREE_OPERAND (value, 0);
1483
1484 /* Pointer variables should have been handled by merge_object_sizes. */
1485 gcc_assert (TREE_CODE (value) != SSA_NAME
1486 || !POINTER_TYPE_P (TREE_TYPE (value)));
1487
1488 if (TREE_CODE (value) == ADDR_EXPR)
1489 addr_object_size (osi, value, object_size_type, &bytes, &wholesize);
1490 else
1491 bytes = wholesize = size_unknown (object_size_type);
1492
1493 object_sizes_set (osi, varno, bytes, wholesize);
1494 }
1495
1496
1497 /* Compute object_sizes for PTR, defined to the result of a call. */
1498
1499 static void
1500 call_object_size (struct object_size_info *osi, tree ptr, gcall *call)
1501 {
1502 int object_size_type = osi->object_size_type;
1503 unsigned int varno = SSA_NAME_VERSION (ptr);
1504 tree bytes = NULL_TREE;
1505
1506 gcc_assert (is_gimple_call (call));
1507
1508 gcc_assert (!object_sizes_unknown_p (object_size_type, varno));
1509 gcc_assert (osi->pass == 0);
1510
1511 bool is_strdup = gimple_call_builtin_p (call, BUILT_IN_STRDUP);
1512 bool is_strndup = gimple_call_builtin_p (call, BUILT_IN_STRNDUP);
1513 bool is_access_with_size
1514 = gimple_call_internal_p (call, IFN_ACCESS_WITH_SIZE);
1515 if (is_strdup || is_strndup)
1516 bytes = strdup_object_size (call, object_size_type, is_strndup);
1517 else if (is_access_with_size)
1518 bytes = access_with_size_object_size (call, object_size_type);
1519 else
1520 bytes = alloc_object_size (call, object_size_type);
1521
1522 if (!size_valid_p (bytes, object_size_type))
1523 bytes = size_unknown (object_size_type);
1524
1525 object_sizes_set (osi, varno, bytes, bytes);
1526 }
1527
1528
1529 /* Compute object_sizes for PTR, defined to an unknown value. */
1530
1531 static void
1532 unknown_object_size (struct object_size_info *osi, tree ptr)
1533 {
1534 int object_size_type = osi->object_size_type;
1535 unsigned int varno = SSA_NAME_VERSION (ptr);
1536
1537 gcc_checking_assert (!object_sizes_unknown_p (object_size_type, varno));
1538 gcc_checking_assert (osi->pass == 0);
1539 tree bytes = size_unknown (object_size_type);
1540
1541 object_sizes_set (osi, varno, bytes, bytes);
1542 }
1543
1544
1545 /* Merge object sizes of ORIG + OFFSET into DEST. Return true if
1546 the object size might need reexamination later. */
1547
1548 static bool
1549 merge_object_sizes (struct object_size_info *osi, tree dest, tree orig)
1550 {
1551 int object_size_type = osi->object_size_type;
1552 unsigned int varno = SSA_NAME_VERSION (dest);
1553 tree orig_bytes, wholesize;
1554
1555 if (object_sizes_unknown_p (object_size_type, varno))
1556 return false;
1557
1558 if (osi->pass == 0)
1559 collect_object_sizes_for (osi, orig);
1560
1561 orig_bytes = object_sizes_get (osi, SSA_NAME_VERSION (orig));
1562 wholesize = object_sizes_get (osi, SSA_NAME_VERSION (orig), true);
1563
1564 if (object_sizes_set (osi, varno, orig_bytes, wholesize))
1565 osi->changed = true;
1566
1567 return bitmap_bit_p (osi->reexamine, SSA_NAME_VERSION (orig));
1568 }
1569
1570
1571 /* Compute object_sizes for VAR, defined to the result of an assignment
1572 with operator POINTER_PLUS_EXPR. Return true if the object size might
1573 need reexamination later. */
1574
1575 static bool
1576 plus_stmt_object_size (struct object_size_info *osi, tree var, gimple *stmt)
1577 {
1578 int object_size_type = osi->object_size_type;
1579 unsigned int varno = SSA_NAME_VERSION (var);
1580 tree bytes, wholesize;
1581 tree op0, op1;
1582 bool reexamine = false;
1583
1584 if (gimple_assign_rhs_code (stmt) == POINTER_PLUS_EXPR)
1585 {
1586 op0 = gimple_assign_rhs1 (stmt);
1587 op1 = gimple_assign_rhs2 (stmt);
1588 }
1589 else if (gimple_assign_rhs_code (stmt) == ADDR_EXPR)
1590 {
1591 tree rhs = TREE_OPERAND (gimple_assign_rhs1 (stmt), 0);
1592 gcc_assert (TREE_CODE (rhs) == MEM_REF);
1593 op0 = TREE_OPERAND (rhs, 0);
1594 op1 = TREE_OPERAND (rhs, 1);
1595 }
1596 else
1597 gcc_unreachable ();
1598
1599 if (object_sizes_unknown_p (object_size_type, varno))
1600 return false;
1601
1602 /* Handle PTR + OFFSET here. */
1603 if ((TREE_CODE (op0) == SSA_NAME || TREE_CODE (op0) == ADDR_EXPR))
1604 {
1605 if (TREE_CODE (op0) == SSA_NAME)
1606 {
1607 if (osi->pass == 0)
1608 collect_object_sizes_for (osi, op0);
1609
1610 bytes = object_sizes_get (osi, SSA_NAME_VERSION (op0));
1611 wholesize = object_sizes_get (osi, SSA_NAME_VERSION (op0), true);
1612 reexamine = bitmap_bit_p (osi->reexamine, SSA_NAME_VERSION (op0));
1613 }
1614 else
1615 {
1616 /* op0 will be ADDR_EXPR here. We should never come here during
1617 reexamination. */
1618 gcc_checking_assert (osi->pass == 0);
1619 addr_object_size (osi, op0, object_size_type, &bytes, &wholesize);
1620 }
1621
1622 bool pos_offset = (size_valid_p (op1, 0)
1623 && compare_tree_int (op1, offset_limit) <= 0);
1624
1625 /* size_for_offset doesn't make sense for -1 size, but it does for size 0
1626 since the wholesize could be non-zero and a negative offset could give
1627 a non-zero size. */
1628 if (size_unknown_p (bytes, 0))
1629 ;
1630 /* In the static case, We want SIZE_FOR_OFFSET to go a bit easy on us if
1631 it sees a negative offset since BYTES could have been
1632 overestimated. */
1633 else if ((object_size_type & OST_DYNAMIC)
1634 || bytes != wholesize
1635 || pos_offset)
1636 bytes = size_for_offset (bytes, op1, wholesize,
1637 ((object_size_type & OST_DYNAMIC)
1638 || pos_offset));
1639 /* In the static case, with a negative offset, the best estimate for
1640 minimum size is size_unknown but for maximum size, the wholesize is a
1641 better estimate than size_unknown. */
1642 else if (object_size_type & OST_MINIMUM)
1643 bytes = size_unknown (object_size_type);
1644 else
1645 bytes = wholesize;
1646 }
1647 else
1648 bytes = wholesize = size_unknown (object_size_type);
1649
1650 if (!size_valid_p (bytes, object_size_type)
1651 || !size_valid_p (wholesize, object_size_type))
1652 bytes = wholesize = size_unknown (object_size_type);
1653
1654 if (object_sizes_set (osi, varno, bytes, wholesize))
1655 osi->changed = true;
1656 return reexamine;
1657 }
1658
1659 /* Compute the dynamic object size for VAR. Return the result in SIZE and
1660 WHOLESIZE. */
1661
1662 static void
1663 dynamic_object_size (struct object_size_info *osi, tree var,
1664 tree *size, tree *wholesize)
1665 {
1666 int object_size_type = osi->object_size_type;
1667
1668 if (TREE_CODE (var) == SSA_NAME)
1669 {
1670 unsigned varno = SSA_NAME_VERSION (var);
1671
1672 collect_object_sizes_for (osi, var);
1673 *size = object_sizes_get (osi, varno);
1674 *wholesize = object_sizes_get (osi, varno, true);
1675 }
1676 else if (TREE_CODE (var) == ADDR_EXPR)
1677 addr_object_size (osi, var, object_size_type, size, wholesize);
1678 else
1679 *size = *wholesize = size_unknown (object_size_type);
1680 }
1681
1682 /* Compute object_sizes for VAR, defined at STMT, which is
1683 a COND_EXPR. Return true if the object size might need reexamination
1684 later. */
1685
1686 static bool
1687 cond_expr_object_size (struct object_size_info *osi, tree var, gimple *stmt)
1688 {
1689 tree then_, else_;
1690 int object_size_type = osi->object_size_type;
1691 unsigned int varno = SSA_NAME_VERSION (var);
1692 bool reexamine = false;
1693
1694 gcc_assert (gimple_assign_rhs_code (stmt) == COND_EXPR);
1695
1696 if (object_sizes_unknown_p (object_size_type, varno))
1697 return false;
1698
1699 then_ = gimple_assign_rhs2 (stmt);
1700 else_ = gimple_assign_rhs3 (stmt);
1701
1702 if (object_size_type & OST_DYNAMIC)
1703 {
1704 tree then_size, then_wholesize, else_size, else_wholesize;
1705
1706 dynamic_object_size (osi, then_, &then_size, &then_wholesize);
1707 if (!size_unknown_p (then_size, object_size_type))
1708 dynamic_object_size (osi, else_, &else_size, &else_wholesize);
1709
1710 tree cond_size, cond_wholesize;
1711 if (size_unknown_p (then_size, object_size_type)
1712 || size_unknown_p (else_size, object_size_type))
1713 cond_size = cond_wholesize = size_unknown (object_size_type);
1714 else
1715 {
1716 cond_size = fold_build3 (COND_EXPR, sizetype,
1717 gimple_assign_rhs1 (stmt),
1718 then_size, else_size);
1719 cond_wholesize = fold_build3 (COND_EXPR, sizetype,
1720 gimple_assign_rhs1 (stmt),
1721 then_wholesize, else_wholesize);
1722 }
1723
1724 object_sizes_set (osi, varno, cond_size, cond_wholesize);
1725
1726 return false;
1727 }
1728
1729 if (TREE_CODE (then_) == SSA_NAME)
1730 reexamine |= merge_object_sizes (osi, var, then_);
1731 else
1732 expr_object_size (osi, var, then_);
1733
1734 if (object_sizes_unknown_p (object_size_type, varno))
1735 return reexamine;
1736
1737 if (TREE_CODE (else_) == SSA_NAME)
1738 reexamine |= merge_object_sizes (osi, var, else_);
1739 else
1740 expr_object_size (osi, var, else_);
1741
1742 return reexamine;
1743 }
1744
1745 /* Find size of an object passed as a parameter to the function. */
1746
1747 static void
1748 parm_object_size (struct object_size_info *osi, tree var)
1749 {
1750 int object_size_type = osi->object_size_type;
1751 tree parm = SSA_NAME_VAR (var);
1752
1753 if (!(object_size_type & OST_DYNAMIC) || !POINTER_TYPE_P (TREE_TYPE (parm)))
1754 {
1755 expr_object_size (osi, var, parm);
1756 return;
1757 }
1758
1759 /* Look for access attribute. */
1760 rdwr_map rdwr_idx;
1761
1762 tree fndecl = cfun->decl;
1763 const attr_access *access = get_parm_access (rdwr_idx, parm, fndecl);
1764 tree typesize = TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (parm)));
1765 tree sz = NULL_TREE;
1766
1767 /* If we have an access attribute with a usable size argument... */
1768 if (access && access->sizarg != UINT_MAX
1769 /* ... and either PARM is void * or has a type that is complete and has a
1770 constant size... */
1771 && ((typesize && poly_int_tree_p (typesize))
1772 || (!typesize && VOID_TYPE_P (TREE_TYPE (TREE_TYPE (parm))))))
1773 {
1774 tree fnargs = DECL_ARGUMENTS (fndecl);
1775 tree arg = NULL_TREE;
1776 unsigned argpos = 0;
1777
1778 /* ... then walk through the parameters to pick the size parameter and
1779 safely scale it by the type size if needed.
1780
1781 TODO: we could also compute the size of VLAs where the size is
1782 given by a function parameter. */
1783 for (arg = fnargs; arg; arg = TREE_CHAIN (arg), ++argpos)
1784 if (argpos == access->sizarg)
1785 {
1786 gcc_assert (INTEGRAL_TYPE_P (TREE_TYPE (arg)));
1787 sz = get_or_create_ssa_default_def (cfun, arg);
1788 if (sz != NULL_TREE)
1789 {
1790 sz = fold_convert (sizetype, sz);
1791 if (typesize)
1792 sz = size_binop (MULT_EXPR, sz, typesize);
1793 }
1794 break;
1795 }
1796 }
1797 if (!sz)
1798 sz = size_unknown (object_size_type);
1799
1800 object_sizes_set (osi, SSA_NAME_VERSION (var), sz, sz);
1801 }
1802
1803 /* Compute an object size expression for VAR, which is the result of a PHI
1804 node. */
1805
1806 static void
1807 phi_dynamic_object_size (struct object_size_info *osi, tree var)
1808 {
1809 int object_size_type = osi->object_size_type;
1810 unsigned int varno = SSA_NAME_VERSION (var);
1811 gimple *stmt = SSA_NAME_DEF_STMT (var);
1812 unsigned i, num_args = gimple_phi_num_args (stmt);
1813 bool wholesize_needed = false;
1814
1815 /* The extra space is for the PHI result at the end, which object_sizes_set
1816 sets for us. */
1817 tree sizes = make_tree_vec (num_args + 1);
1818 tree wholesizes = make_tree_vec (num_args + 1);
1819
1820 /* Bail out if the size of any of the PHI arguments cannot be
1821 determined. */
1822 for (i = 0; i < num_args; i++)
1823 {
1824 edge e = gimple_phi_arg_edge (as_a <gphi *> (stmt), i);
1825 if (e->flags & EDGE_COMPLEX)
1826 break;
1827
1828 tree rhs = gimple_phi_arg_def (stmt, i);
1829 tree size, wholesize;
1830
1831 dynamic_object_size (osi, rhs, &size, &wholesize);
1832
1833 if (size_unknown_p (size, object_size_type))
1834 break;
1835
1836 if (size != wholesize)
1837 wholesize_needed = true;
1838
1839 TREE_VEC_ELT (sizes, i) = size;
1840 TREE_VEC_ELT (wholesizes, i) = wholesize;
1841 }
1842
1843 if (i < num_args)
1844 {
1845 ggc_free (sizes);
1846 ggc_free (wholesizes);
1847 sizes = wholesizes = size_unknown (object_size_type);
1848 }
1849
1850 /* Point to the same TREE_VEC so that we can avoid emitting two PHI
1851 nodes. */
1852 else if (!wholesize_needed)
1853 {
1854 ggc_free (wholesizes);
1855 wholesizes = sizes;
1856 }
1857
1858 object_sizes_set (osi, varno, sizes, wholesizes);
1859 }
1860
1861 /* Compute object sizes for VAR.
1862 For ADDR_EXPR an object size is the number of remaining bytes
1863 to the end of the object (where what is considered an object depends on
1864 OSI->object_size_type).
1865 For allocation GIMPLE_CALL like malloc or calloc object size is the size
1866 of the allocation.
1867 For POINTER_PLUS_EXPR where second operand is a constant integer,
1868 object size is object size of the first operand minus the constant.
1869 If the constant is bigger than the number of remaining bytes until the
1870 end of the object, object size is 0, but if it is instead a pointer
1871 subtraction, object size is size_unknown (object_size_type).
1872 To differentiate addition from subtraction, ADDR_EXPR returns
1873 size_unknown (object_size_type) for all objects bigger than half of the
1874 address space, and constants less than half of the address space are
1875 considered addition, while bigger constants subtraction.
1876 For a memcpy like GIMPLE_CALL that always returns one of its arguments, the
1877 object size is object size of that argument.
1878 Otherwise, object size is the maximum of object sizes of variables
1879 that it might be set to. */
1880
1881 static void
1882 collect_object_sizes_for (struct object_size_info *osi, tree var)
1883 {
1884 int object_size_type = osi->object_size_type;
1885 unsigned int varno = SSA_NAME_VERSION (var);
1886 gimple *stmt;
1887 bool reexamine;
1888
1889 if (bitmap_bit_p (computed[object_size_type], varno))
1890 return;
1891
1892 if (osi->pass == 0)
1893 {
1894 if (bitmap_set_bit (osi->visited, varno))
1895 {
1896 /* Initialize to 0 for maximum size and M1U for minimum size so that
1897 it gets immediately overridden. */
1898 object_sizes_initialize (osi, varno,
1899 size_initval (object_size_type),
1900 size_initval (object_size_type));
1901 }
1902 else
1903 {
1904 /* Found a dependency loop. Mark the variable for later
1905 re-examination. */
1906 if (object_size_type & OST_DYNAMIC)
1907 object_sizes_set_temp (osi, varno);
1908
1909 bitmap_set_bit (osi->reexamine, varno);
1910 if (dump_file && (dump_flags & TDF_DETAILS))
1911 {
1912 fprintf (dump_file, "Found a dependency loop at ");
1913 print_generic_expr (dump_file, var, dump_flags);
1914 fprintf (dump_file, "\n");
1915 }
1916 return;
1917 }
1918 }
1919
1920 if (dump_file && (dump_flags & TDF_DETAILS))
1921 {
1922 fprintf (dump_file, "Visiting use-def links for ");
1923 print_generic_expr (dump_file, var, dump_flags);
1924 fprintf (dump_file, "\n");
1925 }
1926
1927 stmt = SSA_NAME_DEF_STMT (var);
1928 reexamine = false;
1929
1930 switch (gimple_code (stmt))
1931 {
1932 case GIMPLE_ASSIGN:
1933 {
1934 tree rhs = gimple_assign_rhs1 (stmt);
1935 if (gimple_assign_rhs_code (stmt) == POINTER_PLUS_EXPR
1936 || (gimple_assign_rhs_code (stmt) == ADDR_EXPR
1937 && TREE_CODE (TREE_OPERAND (rhs, 0)) == MEM_REF))
1938 reexamine = plus_stmt_object_size (osi, var, stmt);
1939 else if (gimple_assign_rhs_code (stmt) == COND_EXPR)
1940 reexamine = cond_expr_object_size (osi, var, stmt);
1941 else if (gimple_assign_single_p (stmt)
1942 || gimple_assign_unary_nop_p (stmt))
1943 {
1944 if (TREE_CODE (rhs) == SSA_NAME
1945 && POINTER_TYPE_P (TREE_TYPE (rhs)))
1946 reexamine = merge_object_sizes (osi, var, rhs);
1947 else
1948 expr_object_size (osi, var, rhs);
1949 }
1950 else
1951 unknown_object_size (osi, var);
1952 break;
1953 }
1954
1955 case GIMPLE_CALL:
1956 {
1957 gcall *call_stmt = as_a <gcall *> (stmt);
1958 tree arg = pass_through_call (call_stmt);
1959 if (arg)
1960 {
1961 if (TREE_CODE (arg) == SSA_NAME
1962 && POINTER_TYPE_P (TREE_TYPE (arg)))
1963 reexamine = merge_object_sizes (osi, var, arg);
1964 else
1965 expr_object_size (osi, var, arg);
1966 }
1967 else
1968 call_object_size (osi, var, call_stmt);
1969 break;
1970 }
1971
1972 case GIMPLE_ASM:
1973 /* Pointers defined by __asm__ statements can point anywhere. */
1974 unknown_object_size (osi, var);
1975 break;
1976
1977 case GIMPLE_NOP:
1978 if (SSA_NAME_VAR (var)
1979 && TREE_CODE (SSA_NAME_VAR (var)) == PARM_DECL)
1980 parm_object_size (osi, var);
1981 else
1982 /* Uninitialized SSA names point nowhere. */
1983 unknown_object_size (osi, var);
1984 break;
1985
1986 case GIMPLE_PHI:
1987 {
1988 unsigned i;
1989
1990 if (object_size_type & OST_DYNAMIC)
1991 {
1992 phi_dynamic_object_size (osi, var);
1993 break;
1994 }
1995
1996 for (i = 0; i < gimple_phi_num_args (stmt); i++)
1997 {
1998 tree rhs = gimple_phi_arg (stmt, i)->def;
1999
2000 if (object_sizes_unknown_p (object_size_type, varno))
2001 break;
2002
2003 if (TREE_CODE (rhs) == SSA_NAME)
2004 reexamine |= merge_object_sizes (osi, var, rhs);
2005 else if (osi->pass == 0)
2006 expr_object_size (osi, var, rhs);
2007 }
2008 break;
2009 }
2010
2011 default:
2012 gcc_unreachable ();
2013 }
2014
2015 /* Dynamic sizes use placeholder temps to return an answer, so it is always
2016 safe to set COMPUTED for them. */
2017 if ((object_size_type & OST_DYNAMIC)
2018 || !reexamine || object_sizes_unknown_p (object_size_type, varno))
2019 {
2020 bitmap_set_bit (computed[object_size_type], varno);
2021 if (!(object_size_type & OST_DYNAMIC))
2022 bitmap_clear_bit (osi->reexamine, varno);
2023 else if (reexamine)
2024 bitmap_set_bit (osi->reexamine, varno);
2025 }
2026 else
2027 {
2028 bitmap_set_bit (osi->reexamine, varno);
2029 if (dump_file && (dump_flags & TDF_DETAILS))
2030 {
2031 fprintf (dump_file, "Need to reexamine ");
2032 print_generic_expr (dump_file, var, dump_flags);
2033 fprintf (dump_file, "\n");
2034 }
2035 }
2036 }
2037
2038
2039 /* Helper function for check_for_plus_in_loops. Called recursively
2040 to detect loops. */
2041
2042 static void
2043 check_for_plus_in_loops_1 (struct object_size_info *osi, tree var,
2044 unsigned int depth)
2045 {
2046 gimple *stmt = SSA_NAME_DEF_STMT (var);
2047 unsigned int varno = SSA_NAME_VERSION (var);
2048
2049 if (osi->depths[varno])
2050 {
2051 if (osi->depths[varno] != depth)
2052 {
2053 unsigned int *sp;
2054
2055 /* Found a loop involving pointer addition. */
2056 for (sp = osi->tos; sp > osi->stack; )
2057 {
2058 --sp;
2059 bitmap_clear_bit (osi->reexamine, *sp);
2060 bitmap_set_bit (computed[osi->object_size_type], *sp);
2061 object_sizes_set (osi, *sp, size_zero_node,
2062 object_sizes_get (osi, *sp, true));
2063 if (*sp == varno)
2064 break;
2065 }
2066 }
2067 return;
2068 }
2069 else if (! bitmap_bit_p (osi->reexamine, varno))
2070 return;
2071
2072 osi->depths[varno] = depth;
2073 *osi->tos++ = varno;
2074
2075 switch (gimple_code (stmt))
2076 {
2077
2078 case GIMPLE_ASSIGN:
2079 {
2080 if ((gimple_assign_single_p (stmt)
2081 || gimple_assign_unary_nop_p (stmt))
2082 && TREE_CODE (gimple_assign_rhs1 (stmt)) == SSA_NAME)
2083 {
2084 tree rhs = gimple_assign_rhs1 (stmt);
2085
2086 check_for_plus_in_loops_1 (osi, rhs, depth);
2087 }
2088 else if (gimple_assign_rhs_code (stmt) == POINTER_PLUS_EXPR)
2089 {
2090 tree basevar = gimple_assign_rhs1 (stmt);
2091 tree cst = gimple_assign_rhs2 (stmt);
2092
2093 gcc_assert (TREE_CODE (cst) == INTEGER_CST);
2094
2095 check_for_plus_in_loops_1 (osi, basevar,
2096 depth + !integer_zerop (cst));
2097 }
2098 else
2099 gcc_unreachable ();
2100 break;
2101 }
2102
2103 case GIMPLE_CALL:
2104 {
2105 gcall *call_stmt = as_a <gcall *> (stmt);
2106 tree arg = pass_through_call (call_stmt);
2107 if (arg)
2108 {
2109 if (TREE_CODE (arg) == SSA_NAME)
2110 check_for_plus_in_loops_1 (osi, arg, depth);
2111 else
2112 gcc_unreachable ();
2113 }
2114 break;
2115 }
2116
2117 case GIMPLE_PHI:
2118 {
2119 unsigned i;
2120
2121 for (i = 0; i < gimple_phi_num_args (stmt); i++)
2122 {
2123 tree rhs = gimple_phi_arg (stmt, i)->def;
2124
2125 if (TREE_CODE (rhs) == SSA_NAME)
2126 check_for_plus_in_loops_1 (osi, rhs, depth);
2127 }
2128 break;
2129 }
2130
2131 default:
2132 gcc_unreachable ();
2133 }
2134
2135 osi->depths[varno] = 0;
2136 osi->tos--;
2137 }
2138
2139
2140 /* Check if some pointer we are computing object size of is being increased
2141 within a loop. If yes, assume all the SSA variables participating in
2142 that loop have minimum object sizes 0. */
2143
2144 static void
2145 check_for_plus_in_loops (struct object_size_info *osi, tree var)
2146 {
2147 gimple *stmt = SSA_NAME_DEF_STMT (var);
2148
2149 /* NOTE: In the pre-tuples code, we handled a CALL_EXPR here,
2150 and looked for a POINTER_PLUS_EXPR in the pass-through
2151 argument, if any. In GIMPLE, however, such an expression
2152 is not a valid call operand. */
2153
2154 if (is_gimple_assign (stmt)
2155 && gimple_assign_rhs_code (stmt) == POINTER_PLUS_EXPR)
2156 {
2157 tree basevar = gimple_assign_rhs1 (stmt);
2158 tree offset = gimple_assign_rhs2 (stmt);
2159
2160 /* Skip non-positive offsets. */
2161 if (TREE_CODE (offset) != INTEGER_CST
2162 || integer_zerop (offset) || compare_tree_int (offset, offset_limit) > 0)
2163 return;
2164
2165 osi->depths[SSA_NAME_VERSION (basevar)] = 1;
2166 *osi->tos++ = SSA_NAME_VERSION (basevar);
2167 check_for_plus_in_loops_1 (osi, var, 2);
2168 osi->depths[SSA_NAME_VERSION (basevar)] = 0;
2169 osi->tos--;
2170 }
2171 }
2172
2173
2174 /* Initialize data structures for the object size computation. */
2175
2176 void
2177 init_object_sizes (void)
2178 {
2179 int object_size_type;
2180
2181 if (computed[0])
2182 return;
2183
2184 for (object_size_type = 0; object_size_type < OST_END; object_size_type++)
2185 {
2186 object_sizes_grow (object_size_type);
2187 computed[object_size_type] = BITMAP_ALLOC (NULL);
2188 }
2189
2190 init_offset_limit ();
2191 }
2192
2193
2194 /* Destroy data structures after the object size computation. */
2195
2196 void
2197 fini_object_sizes (void)
2198 {
2199 int object_size_type;
2200
2201 for (object_size_type = 0; object_size_type < OST_END; object_size_type++)
2202 {
2203 object_sizes_release (object_size_type);
2204 BITMAP_FREE (computed[object_size_type]);
2205 }
2206 }
2207
2208 /* Dummy valueize function. */
2209
2210 static tree
2211 do_valueize (tree t)
2212 {
2213 return t;
2214 }
2215
2216 /* Process a __builtin_object_size or __builtin_dynamic_object_size call in
2217 CALL early for subobjects before any object information is lost due to
2218 optimization. Insert a MIN or MAX expression of the result and
2219 __builtin_object_size at I so that it may be processed in the second pass.
2220 __builtin_dynamic_object_size is treated like __builtin_object_size here
2221 since we're only looking for constant bounds. */
2222
2223 static void
2224 early_object_sizes_execute_one (gimple_stmt_iterator *i, gimple *call)
2225 {
2226 tree ost = gimple_call_arg (call, 1);
2227 tree lhs = gimple_call_lhs (call);
2228 gcc_assert (lhs != NULL_TREE);
2229
2230 if (!tree_fits_uhwi_p (ost))
2231 return;
2232
2233 unsigned HOST_WIDE_INT object_size_type = tree_to_uhwi (ost);
2234 tree ptr = gimple_call_arg (call, 0);
2235
2236 if (object_size_type != 1 && object_size_type != 3)
2237 return;
2238
2239 if (TREE_CODE (ptr) != ADDR_EXPR && TREE_CODE (ptr) != SSA_NAME)
2240 return;
2241
2242 tree type = TREE_TYPE (lhs);
2243 tree bytes;
2244 if (!compute_builtin_object_size (ptr, object_size_type, &bytes)
2245 || !int_fits_type_p (bytes, type))
2246 return;
2247
2248 tree tem = make_ssa_name (type);
2249 gimple_call_set_lhs (call, tem);
2250 enum tree_code code = object_size_type & OST_MINIMUM ? MAX_EXPR : MIN_EXPR;
2251 tree cst = fold_convert (type, bytes);
2252 gimple *g = gimple_build_assign (lhs, code, tem, cst);
2253 gsi_insert_after (i, g, GSI_NEW_STMT);
2254 update_stmt (call);
2255 }
2256
2257 /* Attempt to fold one __builtin_dynamic_object_size call in CALL into an
2258 expression and insert it at I. Return true if it succeeds. */
2259
2260 static bool
2261 dynamic_object_sizes_execute_one (gimple_stmt_iterator *i, gimple *call)
2262 {
2263 gcc_assert (gimple_call_num_args (call) == 2);
2264
2265 tree args[2];
2266 args[0] = gimple_call_arg (call, 0);
2267 args[1] = gimple_call_arg (call, 1);
2268
2269 location_t loc = EXPR_LOC_OR_LOC (args[0], input_location);
2270 tree result_type = gimple_call_return_type (as_a <gcall *> (call));
2271 tree result = fold_builtin_call_array (loc, result_type,
2272 gimple_call_fn (call), 2, args);
2273
2274 if (!result)
2275 return false;
2276
2277 /* fold_builtin_call_array may wrap the result inside a
2278 NOP_EXPR. */
2279 STRIP_NOPS (result);
2280 gimplify_and_update_call_from_tree (i, result);
2281
2282 if (dump_file && (dump_flags & TDF_DETAILS))
2283 {
2284 fprintf (dump_file, "Simplified (dynamic)\n ");
2285 print_gimple_stmt (dump_file, call, 0, dump_flags);
2286 fprintf (dump_file, " to ");
2287 print_generic_expr (dump_file, result);
2288 fprintf (dump_file, "\n");
2289 }
2290 return true;
2291 }
2292
2293 static unsigned int
2294 object_sizes_execute (function *fun, bool early)
2295 {
2296 todo = 0;
2297 auto_bitmap sdce_worklist;
2298
2299 basic_block bb;
2300 FOR_EACH_BB_FN (bb, fun)
2301 {
2302 gimple_stmt_iterator i;
2303 for (i = gsi_start_bb (bb); !gsi_end_p (i); gsi_next (&i))
2304 {
2305 tree result;
2306 bool dynamic = false;
2307
2308 gimple *call = gsi_stmt (i);
2309 if (gimple_call_builtin_p (call, BUILT_IN_DYNAMIC_OBJECT_SIZE))
2310 dynamic = true;
2311 else if (!gimple_call_builtin_p (call, BUILT_IN_OBJECT_SIZE))
2312 continue;
2313
2314 tree lhs = gimple_call_lhs (call);
2315 if (!lhs)
2316 continue;
2317
2318 init_object_sizes ();
2319
2320 /* If early, only attempt to fold
2321 __builtin_object_size (x, 1) and __builtin_object_size (x, 3),
2322 and rather than folding the builtin to the constant if any,
2323 create a MIN_EXPR or MAX_EXPR of the __builtin_object_size
2324 call result and the computed constant. Do the same for
2325 __builtin_dynamic_object_size too. */
2326 if (early)
2327 {
2328 early_object_sizes_execute_one (&i, call);
2329 continue;
2330 }
2331
2332 if (dynamic)
2333 {
2334 if (dynamic_object_sizes_execute_one (&i, call))
2335 continue;
2336 else
2337 {
2338 /* If we could not find a suitable size expression, lower to
2339 __builtin_object_size so that we may at least get a
2340 constant lower or higher estimate. */
2341 tree bosfn = builtin_decl_implicit (BUILT_IN_OBJECT_SIZE);
2342 gimple_call_set_fndecl (call, bosfn);
2343 update_stmt (call);
2344
2345 if (dump_file && (dump_flags & TDF_DETAILS))
2346 {
2347 print_generic_expr (dump_file, gimple_call_arg (call, 0),
2348 dump_flags);
2349 fprintf (dump_file,
2350 ": Retrying as __builtin_object_size\n");
2351 }
2352 }
2353 }
2354
2355 result = gimple_fold_stmt_to_constant (call, do_valueize);
2356 if (!result)
2357 {
2358 tree ost = gimple_call_arg (call, 1);
2359
2360 if (tree_fits_uhwi_p (ost))
2361 {
2362 unsigned HOST_WIDE_INT object_size_type = tree_to_uhwi (ost);
2363
2364 if (object_size_type & OST_MINIMUM)
2365 result = build_zero_cst (size_type_node);
2366 else if (object_size_type < OST_END)
2367 result = fold_convert (size_type_node,
2368 integer_minus_one_node);
2369 }
2370
2371 if (!result)
2372 continue;
2373 }
2374
2375 gcc_assert (TREE_CODE (result) == INTEGER_CST);
2376
2377 if (dump_file && (dump_flags & TDF_DETAILS))
2378 {
2379 fprintf (dump_file, "Simplified\n ");
2380 print_gimple_stmt (dump_file, call, 0, dump_flags);
2381 fprintf (dump_file, " to ");
2382 print_generic_expr (dump_file, result);
2383 fprintf (dump_file, "\n");
2384 }
2385
2386 /* Propagate into all uses and fold those stmts. */
2387 if (!SSA_NAME_OCCURS_IN_ABNORMAL_PHI (lhs))
2388 {
2389 replace_uses_by (lhs, result);
2390 /* Mark lhs as being possiblely DCEd. */
2391 bitmap_set_bit (sdce_worklist, SSA_NAME_VERSION (lhs));
2392 }
2393 else
2394 replace_call_with_value (&i, result);
2395 }
2396 }
2397
2398 fini_object_sizes ();
2399 simple_dce_from_worklist (sdce_worklist);
2400 return todo;
2401 }
2402
2403 /* Simple pass to optimize all __builtin_object_size () builtins. */
2404
2405 namespace {
2406
2407 const pass_data pass_data_object_sizes =
2408 {
2409 GIMPLE_PASS, /* type */
2410 "objsz", /* name */
2411 OPTGROUP_NONE, /* optinfo_flags */
2412 TV_NONE, /* tv_id */
2413 ( PROP_cfg | PROP_ssa ), /* properties_required */
2414 PROP_objsz, /* properties_provided */
2415 0, /* properties_destroyed */
2416 0, /* todo_flags_start */
2417 0, /* todo_flags_finish */
2418 };
2419
2420 class pass_object_sizes : public gimple_opt_pass
2421 {
2422 public:
2423 pass_object_sizes (gcc::context *ctxt)
2424 : gimple_opt_pass (pass_data_object_sizes, ctxt)
2425 {}
2426
2427 /* opt_pass methods: */
2428 opt_pass * clone () final override { return new pass_object_sizes (m_ctxt); }
2429 unsigned int execute (function *fun) final override
2430 {
2431 return object_sizes_execute (fun, false);
2432 }
2433 }; // class pass_object_sizes
2434
2435 } // anon namespace
2436
2437 gimple_opt_pass *
2438 make_pass_object_sizes (gcc::context *ctxt)
2439 {
2440 return new pass_object_sizes (ctxt);
2441 }
2442
2443 /* Early version of pass to optimize all __builtin_object_size () builtins. */
2444
2445 namespace {
2446
2447 const pass_data pass_data_early_object_sizes =
2448 {
2449 GIMPLE_PASS, /* type */
2450 "early_objsz", /* name */
2451 OPTGROUP_NONE, /* optinfo_flags */
2452 TV_NONE, /* tv_id */
2453 ( PROP_cfg | PROP_ssa ), /* properties_required */
2454 0, /* properties_provided */
2455 0, /* properties_destroyed */
2456 0, /* todo_flags_start */
2457 0, /* todo_flags_finish */
2458 };
2459
2460 class pass_early_object_sizes : public gimple_opt_pass
2461 {
2462 public:
2463 pass_early_object_sizes (gcc::context *ctxt)
2464 : gimple_opt_pass (pass_data_early_object_sizes, ctxt)
2465 {}
2466
2467 /* opt_pass methods: */
2468 unsigned int execute (function *fun) final override
2469 {
2470 return object_sizes_execute (fun, true);
2471 }
2472 }; // class pass_object_sizes
2473
2474 } // anon namespace
2475
2476 gimple_opt_pass *
2477 make_pass_early_object_sizes (gcc::context *ctxt)
2478 {
2479 return new pass_early_object_sizes (ctxt);
2480 }