Branch data Line data Source code
1 : : /* String length optimization
2 : : Copyright (C) 2011-2025 Free Software Foundation, Inc.
3 : : Contributed by Jakub Jelinek <jakub@redhat.com>
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 "rtl.h"
26 : : #include "tree.h"
27 : : #include "gimple.h"
28 : : #include "alloc-pool.h"
29 : : #include "tree-pass.h"
30 : : #include "ssa.h"
31 : : #include "cgraph.h"
32 : : #include "gimple-pretty-print.h"
33 : : #include "gimple-ssa-warn-access.h"
34 : : #include "gimple-ssa-warn-restrict.h"
35 : : #include "fold-const.h"
36 : : #include "stor-layout.h"
37 : : #include "gimple-iterator.h"
38 : : #include "gimple-fold.h"
39 : : #include "tree-eh.h"
40 : : #include "gimplify.h"
41 : : #include "gimplify-me.h"
42 : : #include "expr.h"
43 : : #include "tree-cfg.h"
44 : : #include "tree-dfa.h"
45 : : #include "domwalk.h"
46 : : #include "tree-ssa-alias.h"
47 : : #include "tree-ssa-propagate.h"
48 : : #include "tree-ssa-strlen.h"
49 : : #include "tree-hash-traits.h"
50 : : #include "builtins.h"
51 : : #include "pointer-query.h"
52 : : #include "target.h"
53 : : #include "diagnostic-core.h"
54 : : #include "diagnostic.h"
55 : : #include "intl.h"
56 : : #include "attribs.h"
57 : : #include "calls.h"
58 : : #include "cfgloop.h"
59 : : #include "tree-ssa-loop.h"
60 : : #include "tree-scalar-evolution.h"
61 : : #include "vr-values.h"
62 : : #include "gimple-range.h"
63 : : #include "tree-ssa.h"
64 : :
65 : : /* A vector indexed by SSA_NAME_VERSION. 0 means unknown, positive value
66 : : is an index into strinfo vector, negative value stands for
67 : : string length of a string literal (~strlen). */
68 : : static vec<int> ssa_ver_to_stridx;
69 : :
70 : : /* Number of currently active string indexes plus one. */
71 : : static int max_stridx;
72 : :
73 : : /* Set to true to optimize, false when just checking. */
74 : : static bool strlen_optimize;
75 : :
76 : : /* String information record. */
77 : : struct strinfo
78 : : {
79 : : /* Number of leading characters that are known to be nonzero. This is
80 : : also the length of the string if FULL_STRING_P.
81 : :
82 : : The values in a list of related string pointers must be consistent;
83 : : that is, if strinfo B comes X bytes after strinfo A, it must be
84 : : the case that A->nonzero_chars == X + B->nonzero_chars. */
85 : : tree nonzero_chars;
86 : : /* Any of the corresponding pointers for querying alias oracle. */
87 : : tree ptr;
88 : : /* STMT is used for two things:
89 : :
90 : : - To record the statement that should be used for delayed length
91 : : computations. We maintain the invariant that all related strinfos
92 : : have delayed lengths or none do.
93 : :
94 : : - To record the malloc or calloc call that produced this result
95 : : to optimize away malloc/memset sequences. STMT is reset after
96 : : a calloc-allocated object has been stored a non-zero value into. */
97 : : gimple *stmt;
98 : : /* Set to the dynamic allocation statement for the object (alloca,
99 : : calloc, malloc, or VLA). Unlike STMT, once set for a strinfo
100 : : object, ALLOC doesn't change. */
101 : : gimple *alloc;
102 : : /* Pointer to '\0' if known, if NULL, it can be computed as
103 : : ptr + length. */
104 : : tree endptr;
105 : : /* Reference count. Any changes to strinfo entry possibly shared
106 : : with dominating basic blocks need unshare_strinfo first, except
107 : : for dont_invalidate which affects only the immediately next
108 : : maybe_invalidate. */
109 : : int refcount;
110 : : /* Copy of index. get_strinfo (si->idx) should return si; */
111 : : int idx;
112 : : /* These 3 fields are for chaining related string pointers together.
113 : : E.g. for
114 : : bl = strlen (b); dl = strlen (d); strcpy (a, b); c = a + bl;
115 : : strcpy (c, d); e = c + dl;
116 : : strinfo(a) -> strinfo(c) -> strinfo(e)
117 : : All have ->first field equal to strinfo(a)->idx and are doubly
118 : : chained through prev/next fields. The later strinfos are required
119 : : to point into the same string with zero or more bytes after
120 : : the previous pointer and all bytes in between the two pointers
121 : : must be non-zero. Functions like strcpy or memcpy are supposed
122 : : to adjust all previous strinfo lengths, but not following strinfo
123 : : lengths (those are uncertain, usually invalidated during
124 : : maybe_invalidate, except when the alias oracle knows better).
125 : : Functions like strcat on the other side adjust the whole
126 : : related strinfo chain.
127 : : They are updated lazily, so to use the chain the same first fields
128 : : and si->prev->next == si->idx needs to be verified. */
129 : : int first;
130 : : int next;
131 : : int prev;
132 : : /* A flag whether the string is known to be written in the current
133 : : function. */
134 : : bool writable;
135 : : /* A flag for the next maybe_invalidate that this strinfo shouldn't
136 : : be invalidated. Always cleared by maybe_invalidate. */
137 : : bool dont_invalidate;
138 : : /* True if the string is known to be nul-terminated after NONZERO_CHARS
139 : : characters. False is useful when detecting strings that are built
140 : : up via successive memcpys. */
141 : : bool full_string_p;
142 : : };
143 : :
144 : : /* Pool for allocating strinfo_struct entries. */
145 : : static object_allocator<strinfo> strinfo_pool ("strinfo pool");
146 : :
147 : : /* Vector mapping positive string indexes to strinfo, for the
148 : : current basic block. The first pointer in the vector is special,
149 : : it is either NULL, meaning the vector isn't shared, or it is
150 : : a basic block pointer to the owner basic_block if shared.
151 : : If some other bb wants to modify the vector, the vector needs
152 : : to be unshared first, and only the owner bb is supposed to free it. */
153 : : static vec<strinfo *, va_heap, vl_embed> *stridx_to_strinfo;
154 : :
155 : : /* One OFFSET->IDX mapping. */
156 : : struct stridxlist
157 : : {
158 : : struct stridxlist *next;
159 : : HOST_WIDE_INT offset;
160 : : int idx;
161 : : };
162 : :
163 : : /* Hash table entry, mapping a DECL to a chain of OFFSET->IDX mappings. */
164 : : struct decl_stridxlist_map
165 : : {
166 : : struct tree_map_base base;
167 : : struct stridxlist list;
168 : : };
169 : :
170 : : /* Hash table for mapping decls to a chained list of offset -> idx
171 : : mappings. */
172 : : typedef hash_map<tree_decl_hash, stridxlist> decl_to_stridxlist_htab_t;
173 : : static decl_to_stridxlist_htab_t *decl_to_stridxlist_htab;
174 : :
175 : : /* Hash table mapping strlen (or strnlen with constant bound and return
176 : : smaller than bound) calls to stridx instances describing
177 : : the calls' arguments. Non-null only when warn_stringop_truncation
178 : : is non-zero. */
179 : : typedef std::pair<int, location_t> stridx_strlenloc;
180 : : static hash_map<tree, stridx_strlenloc> *strlen_to_stridx;
181 : :
182 : : /* Obstack for struct stridxlist and struct decl_stridxlist_map. */
183 : : static struct obstack stridx_obstack;
184 : :
185 : : /* Last memcpy statement if it could be adjusted if the trailing
186 : : '\0' written is immediately overwritten, or
187 : : *x = '\0' store that could be removed if it is immediately overwritten. */
188 : : struct laststmt_struct
189 : : {
190 : : gimple *stmt;
191 : : tree len;
192 : : int stridx;
193 : : } laststmt;
194 : :
195 : : static int get_stridx_plus_constant (strinfo *, unsigned HOST_WIDE_INT, tree);
196 : : static bool get_range_strlen_dynamic (tree, gimple *, c_strlen_data *,
197 : : bitmap, pointer_query *, unsigned *);
198 : :
199 : : /* Sets MINMAX to either the constant value or the range VAL is in
200 : : and returns either the constant value or VAL on success or null
201 : : when the range couldn't be determined. Uses RVALS or CFUN for
202 : : range info, whichever is nonnull. */
203 : :
204 : : tree
205 : 4616675 : get_range (tree val, gimple *stmt, wide_int minmax[2],
206 : : range_query *rvals /* = NULL */)
207 : : {
208 : 4616675 : if (!rvals)
209 : : {
210 : 102727 : if (!cfun)
211 : : /* When called from front ends for global initializers CFUN
212 : : may be null. */
213 : : return NULL_TREE;
214 : :
215 : 102721 : rvals = get_range_query (cfun);
216 : : }
217 : :
218 : 4616669 : value_range vr (TREE_TYPE (val));
219 : 4616669 : if (!rvals->range_of_expr (vr, val, stmt))
220 : : return NULL_TREE;
221 : :
222 : 4616669 : tree vrmin, vrmax;
223 : 4616669 : value_range_kind rng = get_legacy_range (vr, vrmin, vrmax);
224 : 4616669 : if (rng == VR_RANGE)
225 : : {
226 : : /* Only handle straight ranges. */
227 : 4311697 : minmax[0] = wi::to_wide (vrmin);
228 : 4311697 : minmax[1] = wi::to_wide (vrmax);
229 : 4311697 : return val;
230 : : }
231 : :
232 : : return NULL_TREE;
233 : 4616669 : }
234 : :
235 : : class strlen_pass : public dom_walker
236 : : {
237 : : public:
238 : 1064649 : strlen_pass (function *fun, cdi_direction direction)
239 : 1064649 : : dom_walker (direction),
240 : 1064649 : ptr_qry (get_range_query (fun)),
241 : 1064649 : m_cleanup_cfg (false)
242 : : {
243 : 1064649 : }
244 : :
245 : : ~strlen_pass ();
246 : :
247 : : edge before_dom_children (basic_block) final override;
248 : : void after_dom_children (basic_block) final override;
249 : :
250 : : bool check_and_optimize_stmt (bool *cleanup_eh);
251 : : bool check_and_optimize_call (bool *zero_write);
252 : : bool handle_assign (tree lhs, bool *zero_write);
253 : : bool handle_store (bool *zero_write);
254 : : void handle_pointer_plus ();
255 : : void handle_builtin_strlen ();
256 : : void handle_builtin_strchr ();
257 : : void handle_builtin_strcpy (built_in_function);
258 : : void handle_integral_assign (bool *cleanup_eh);
259 : : void handle_builtin_stxncpy_strncat (bool append_p);
260 : : void handle_builtin_memcpy (built_in_function bcode);
261 : : void handle_builtin_strcat (built_in_function bcode);
262 : : void handle_builtin_strncat (built_in_function);
263 : : bool handle_builtin_memset (bool *zero_write);
264 : : bool handle_builtin_memcmp ();
265 : : bool handle_builtin_string_cmp ();
266 : : void handle_alloc_call (built_in_function);
267 : : void maybe_warn_overflow (gimple *stmt, bool call_lhs, tree len,
268 : : strinfo *si = NULL, bool plus_one = false,
269 : : bool rawmem = false);
270 : : void maybe_warn_overflow (gimple *stmt, bool call_lhs,
271 : : unsigned HOST_WIDE_INT len,
272 : : strinfo *si = NULL,
273 : : bool plus_one = false, bool rawmem = false);
274 : : void adjust_last_stmt (strinfo *si, gimple *stmt, bool is_strcat);
275 : : tree strxcmp_eqz_result (gimple *stmt, tree arg1, int idx1,
276 : : tree arg2, int idx2,
277 : : unsigned HOST_WIDE_INT bound,
278 : : unsigned HOST_WIDE_INT len[2],
279 : : unsigned HOST_WIDE_INT *psize);
280 : : bool count_nonzero_bytes (tree expr_or_type,
281 : : gimple *stmt,
282 : : unsigned lenrange[3], bool *nulterm,
283 : : bool *allnul, bool *allnonnul);
284 : : bool count_nonzero_bytes (tree exp, tree vuse,
285 : : gimple *stmt,
286 : : unsigned HOST_WIDE_INT offset,
287 : : unsigned HOST_WIDE_INT nbytes,
288 : : unsigned lenrange[3], bool *nulterm,
289 : : bool *allnul, bool *allnonnul,
290 : : ssa_name_limit_t &snlim);
291 : : bool count_nonzero_bytes_addr (tree exp, tree vuse,
292 : : gimple *stmt,
293 : : unsigned HOST_WIDE_INT offset,
294 : : unsigned HOST_WIDE_INT nbytes,
295 : : unsigned lenrange[3], bool *nulterm,
296 : : bool *allnul, bool *allnonnul,
297 : : ssa_name_limit_t &snlim);
298 : : bool get_len_or_size (gimple *stmt, tree arg, int idx,
299 : : unsigned HOST_WIDE_INT lenrng[2],
300 : : unsigned HOST_WIDE_INT *size, bool *nulterm);
301 : :
302 : : /* A pointer_query object to store information about pointers and
303 : : their targets in. */
304 : : pointer_query ptr_qry;
305 : :
306 : : gimple_stmt_iterator m_gsi;
307 : :
308 : : /* Flag that will trigger TODO_cleanup_cfg to be returned in strlen
309 : : execute function. */
310 : : bool m_cleanup_cfg;
311 : : };
312 : :
313 : : /* Return:
314 : :
315 : : * +1 if SI is known to start with more than OFF nonzero characters.
316 : :
317 : : * 0 if SI is known to start with exactly OFF nonzero characters.
318 : :
319 : : * -1 if SI either does not start with OFF nonzero characters
320 : : or the relationship between the number of leading nonzero
321 : : characters in SI and OFF is unknown. */
322 : :
323 : : static int
324 : 5040 : compare_nonzero_chars (strinfo *si, unsigned HOST_WIDE_INT off)
325 : : {
326 : 5040 : if (si->nonzero_chars
327 : 4753 : && TREE_CODE (si->nonzero_chars) == INTEGER_CST)
328 : 4712 : return compare_tree_int (si->nonzero_chars, off);
329 : : else
330 : : return -1;
331 : : }
332 : :
333 : : /* Same as above but suitable also for strings with non-constant lengths.
334 : : Uses RVALS to determine length range. */
335 : :
336 : : static int
337 : 72417 : compare_nonzero_chars (strinfo *si, gimple *stmt,
338 : : unsigned HOST_WIDE_INT off,
339 : : range_query *rvals)
340 : : {
341 : 72417 : if (!si->nonzero_chars)
342 : : return -1;
343 : :
344 : 68702 : if (TREE_CODE (si->nonzero_chars) == INTEGER_CST)
345 : 68221 : return compare_tree_int (si->nonzero_chars, off);
346 : :
347 : 481 : if (!rvals || TREE_CODE (si->nonzero_chars) != SSA_NAME)
348 : : return -1;
349 : :
350 : 185 : int_range_max vr;
351 : 185 : if (!rvals->range_of_expr (vr, si->nonzero_chars, stmt)
352 : 185 : || vr.varying_p ()
353 : 370 : || vr.undefined_p ())
354 : : return -1;
355 : :
356 : : /* If the offset is less than the minimum length or if the bounds
357 : : of the length range are equal return the result of the comparison
358 : : same as in the constant case. Otherwise return a conservative
359 : : result. */
360 : 185 : signop sign = TYPE_SIGN (vr.type ());
361 : 185 : unsigned prec = TYPE_PRECISION (vr.type ());
362 : 185 : int cmpmin = wi::cmp (vr.lower_bound (), wi::uhwi (off, prec), sign);
363 : 185 : if (cmpmin > 0 || vr.singleton_p ())
364 : 133 : return cmpmin;
365 : :
366 : : return -1;
367 : 185 : }
368 : :
369 : : /* Return true if SI is known to be a zero-length string. */
370 : :
371 : : static inline bool
372 : 3240 : zero_length_string_p (strinfo *si)
373 : : {
374 : 3240 : return si->full_string_p && integer_zerop (si->nonzero_chars);
375 : : }
376 : :
377 : : /* Return strinfo vector entry IDX. */
378 : :
379 : : static inline strinfo *
380 : 267112 : get_strinfo (int idx)
381 : : {
382 : 267112 : if (vec_safe_length (stridx_to_strinfo) <= (unsigned int) idx)
383 : : return NULL;
384 : 214856 : return (*stridx_to_strinfo)[idx];
385 : : }
386 : :
387 : : /* Get the next strinfo in the chain after SI, or null if none. */
388 : :
389 : : static inline strinfo *
390 : 8436 : get_next_strinfo (strinfo *si)
391 : : {
392 : 8436 : if (si->next == 0)
393 : : return NULL;
394 : 6524 : strinfo *nextsi = get_strinfo (si->next);
395 : 6524 : if (nextsi == NULL || nextsi->first != si->first || nextsi->prev != si->idx)
396 : 12 : return NULL;
397 : : return nextsi;
398 : : }
399 : :
400 : : /* Helper function for get_stridx. Return the strinfo index of the address
401 : : of EXP, which is available in PTR if nonnull. If OFFSET_OUT, it is
402 : : OK to return the index for some X <= &EXP and store &EXP - X in
403 : : *OFFSET_OUT. When RVALS is nonnull uses it to determine range
404 : : information. */
405 : :
406 : : static int
407 : 1701704 : get_addr_stridx (tree exp, gimple *stmt,
408 : : tree ptr, unsigned HOST_WIDE_INT *offset_out,
409 : : range_query *rvals = NULL)
410 : : {
411 : 1701704 : HOST_WIDE_INT off;
412 : 1701704 : struct stridxlist *list, *last = NULL;
413 : 1701704 : tree base;
414 : :
415 : 1701704 : if (!decl_to_stridxlist_htab)
416 : : return 0;
417 : :
418 : 529733 : poly_int64 poff;
419 : 529733 : base = get_addr_base_and_unit_offset (exp, &poff);
420 : 529733 : if (base == NULL || !DECL_P (base) || !poff.is_constant (&off))
421 : : return 0;
422 : :
423 : 310727 : list = decl_to_stridxlist_htab->get (base);
424 : 310727 : if (list == NULL)
425 : : return 0;
426 : :
427 : 243929 : do
428 : : {
429 : 243929 : if (list->offset == off)
430 : : {
431 : 74718 : if (offset_out)
432 : 32475 : *offset_out = 0;
433 : 74718 : return list->idx;
434 : : }
435 : 169211 : if (list->offset > off)
436 : : return 0;
437 : 138165 : last = list;
438 : 138165 : list = list->next;
439 : : }
440 : 138165 : while (list);
441 : :
442 : 53035 : if ((offset_out || ptr) && last && last->idx > 0)
443 : : {
444 : 53035 : unsigned HOST_WIDE_INT rel_off
445 : 53035 : = (unsigned HOST_WIDE_INT) off - last->offset;
446 : 53035 : strinfo *si = get_strinfo (last->idx);
447 : 53035 : if (si && compare_nonzero_chars (si, stmt, rel_off, rvals) >= 0)
448 : : {
449 : 13087 : if (offset_out)
450 : : {
451 : 11545 : *offset_out = rel_off;
452 : 11545 : return last->idx;
453 : : }
454 : : else
455 : 1542 : return get_stridx_plus_constant (si, rel_off, ptr);
456 : : }
457 : : }
458 : : return 0;
459 : : }
460 : :
461 : : /* Returns string index for EXP. When EXP is an SSA_NAME that refers
462 : : to a known strinfo with an offset and OFFRNG is non-null, sets
463 : : both elements of the OFFRNG array to the range of the offset and
464 : : returns the index of the known strinfo. In this case the result
465 : : must not be used in for functions that modify the string.
466 : : When nonnull, uses RVALS to determine range information. */
467 : :
468 : : static int
469 : 5958868 : get_stridx (tree exp, gimple *stmt,
470 : : wide_int offrng[2] = NULL, range_query *rvals = NULL)
471 : : {
472 : 5958868 : if (offrng)
473 : 24411 : offrng[0] = offrng[1] = wi::zero (TYPE_PRECISION (ptrdiff_type_node));
474 : :
475 : 5958868 : if (TREE_CODE (exp) == SSA_NAME)
476 : : {
477 : 2495140 : if (ssa_ver_to_stridx[SSA_NAME_VERSION (exp)])
478 : : return ssa_ver_to_stridx[SSA_NAME_VERSION (exp)];
479 : :
480 : 2387318 : tree e = exp;
481 : 2387318 : int last_idx = 0;
482 : 2387318 : HOST_WIDE_INT offset = 0;
483 : : /* Follow a chain of at most 5 assignments. */
484 : 2550291 : for (int i = 0; i < 5; i++)
485 : : {
486 : 2543629 : gimple *def_stmt = SSA_NAME_DEF_STMT (e);
487 : 2543629 : if (!is_gimple_assign (def_stmt))
488 : : return last_idx;
489 : :
490 : 1235395 : tree_code rhs_code = gimple_assign_rhs_code (def_stmt);
491 : 1235395 : tree ptr, off;
492 : :
493 : 1235395 : if (rhs_code == ADDR_EXPR)
494 : : {
495 : : /* Handle indices/offsets into VLAs which are implemented
496 : : as pointers to arrays. */
497 : 49958 : ptr = gimple_assign_rhs1 (def_stmt);
498 : 49958 : ptr = TREE_OPERAND (ptr, 0);
499 : :
500 : : /* Handle also VLAs of types larger than char. */
501 : 49958 : if (tree eltsize = TYPE_SIZE_UNIT (TREE_TYPE (ptr)))
502 : : {
503 : 49473 : if (TREE_CODE (ptr) == ARRAY_REF)
504 : : {
505 : 17188 : off = TREE_OPERAND (ptr, 1);
506 : 17188 : ptr = TREE_OPERAND (ptr, 0);
507 : 17188 : if (!integer_onep (eltsize))
508 : : {
509 : : /* Scale the array index by the size of the element
510 : : type in the rare case that it's greater than
511 : : the typical 1 for char, making sure both operands
512 : : have the same type. */
513 : 10146 : eltsize = fold_convert (ssizetype, eltsize);
514 : 10146 : off = fold_convert (ssizetype, off);
515 : 10146 : off = fold_build2 (MULT_EXPR, ssizetype, off, eltsize);
516 : : }
517 : : }
518 : : else
519 : 32285 : off = integer_zero_node;
520 : : }
521 : : else
522 : : return 0;
523 : :
524 : 49473 : if (TREE_CODE (ptr) != MEM_REF)
525 : : return 0;
526 : :
527 : : /* Add the MEM_REF byte offset. */
528 : 13399 : tree mem_off = TREE_OPERAND (ptr, 1);
529 : 13399 : off = fold_build2 (PLUS_EXPR, TREE_TYPE (off), off, mem_off);
530 : 13399 : ptr = TREE_OPERAND (ptr, 0);
531 : : }
532 : 1185437 : else if (rhs_code == POINTER_PLUS_EXPR)
533 : : {
534 : 420124 : ptr = gimple_assign_rhs1 (def_stmt);
535 : 420124 : off = gimple_assign_rhs2 (def_stmt);
536 : : }
537 : : else
538 : : return 0;
539 : :
540 : 433523 : if (TREE_CODE (ptr) != SSA_NAME)
541 : : return 0;
542 : :
543 : 406536 : if (!tree_fits_shwi_p (off))
544 : : {
545 : 242292 : if (int idx = ssa_ver_to_stridx[SSA_NAME_VERSION (ptr)])
546 : 17422 : if (offrng)
547 : : {
548 : : /* Only when requested by setting OFFRNG to non-null,
549 : : return the index corresponding to the SSA_NAME.
550 : : Do this irrespective of the whether the offset
551 : : is known. */
552 : 264 : if (get_range (off, def_stmt, offrng, rvals))
553 : : {
554 : : /* When the offset range is known, increment it
555 : : it by the constant offset computed in prior
556 : : iterations and store it in the OFFRNG array. */
557 : 121 : offrng[0] += offset;
558 : 121 : offrng[1] += offset;
559 : : }
560 : : else
561 : : {
562 : : /* When the offset range cannot be determined
563 : : store [0, SIZE_MAX] and let the caller decide
564 : : if the offset matters. */
565 : 143 : offrng[1] = wi::to_wide (TYPE_MAX_VALUE (sizetype));
566 : 143 : offrng[0] = wi::zero (offrng[1].get_precision ());
567 : : }
568 : 264 : return idx;
569 : : }
570 : : return 0;
571 : : }
572 : :
573 : 164244 : HOST_WIDE_INT this_off = tree_to_shwi (off);
574 : 164244 : if (offrng)
575 : : {
576 : 1493 : offrng[0] += wi::shwi (this_off, offrng->get_precision ());
577 : 1493 : offrng[1] += offrng[0];
578 : : }
579 : :
580 : 164244 : if (this_off < 0)
581 : : return last_idx;
582 : :
583 : 164244 : offset = (unsigned HOST_WIDE_INT) offset + this_off;
584 : 164244 : if (offset < 0)
585 : : return last_idx;
586 : :
587 : 164244 : if (int idx = ssa_ver_to_stridx[SSA_NAME_VERSION (ptr)])
588 : : {
589 : 10362 : strinfo *si = get_strinfo (idx);
590 : 10362 : if (si)
591 : : {
592 : 2227 : if (compare_nonzero_chars (si, offset) >= 0)
593 : 1271 : return get_stridx_plus_constant (si, offset, exp);
594 : :
595 : 956 : if (offrng)
596 : 162973 : last_idx = idx;
597 : : }
598 : : }
599 : 162973 : e = ptr;
600 : : }
601 : :
602 : : return last_idx;
603 : : }
604 : :
605 : 3463728 : if (TREE_CODE (exp) == ADDR_EXPR)
606 : : {
607 : 1136068 : int idx = get_addr_stridx (TREE_OPERAND (exp, 0), stmt, exp, NULL);
608 : 1136068 : if (idx != 0)
609 : : return idx;
610 : : }
611 : :
612 : 3419943 : const char *p = c_getstr (exp);
613 : 3419943 : if (p)
614 : 372488 : return ~(int) strlen (p);
615 : :
616 : : return 0;
617 : : }
618 : :
619 : : /* Return true if strinfo vector is shared with the immediate dominator. */
620 : :
621 : : static inline bool
622 : 3167188 : strinfo_shared (void)
623 : : {
624 : 3167188 : return vec_safe_length (stridx_to_strinfo)
625 : 3167188 : && (*stridx_to_strinfo)[0] != NULL;
626 : : }
627 : :
628 : : /* Unshare strinfo vector that is shared with the immediate dominator. */
629 : :
630 : : static void
631 : 279781 : unshare_strinfo_vec (void)
632 : : {
633 : 279781 : strinfo *si;
634 : 279781 : unsigned int i = 0;
635 : :
636 : 279781 : gcc_assert (strinfo_shared ());
637 : 279781 : stridx_to_strinfo = vec_safe_copy (stridx_to_strinfo);
638 : 5653168 : for (i = 1; vec_safe_iterate (stridx_to_strinfo, i, &si); ++i)
639 : 5373387 : if (si != NULL)
640 : 1035408 : si->refcount++;
641 : 279781 : (*stridx_to_strinfo)[0] = NULL;
642 : 279781 : }
643 : :
644 : : /* Attempt to create a string index for exp, ADDR_EXPR's operand.
645 : : Return a pointer to the location where the string index can
646 : : be stored (if 0) or is stored, or NULL if this can't be tracked. */
647 : :
648 : : static int *
649 : 163384 : addr_stridxptr (tree exp)
650 : : {
651 : 163384 : HOST_WIDE_INT off;
652 : :
653 : 163384 : poly_int64 poff;
654 : 163384 : tree base = get_addr_base_and_unit_offset (exp, &poff);
655 : 163384 : if (base == NULL_TREE || !DECL_P (base) || !poff.is_constant (&off))
656 : : return NULL;
657 : :
658 : 139127 : if (!decl_to_stridxlist_htab)
659 : : {
660 : 42758 : decl_to_stridxlist_htab
661 : 42758 : = new hash_map<tree_decl_hash, stridxlist> (64);
662 : 42758 : gcc_obstack_init (&stridx_obstack);
663 : : }
664 : :
665 : 139127 : bool existed;
666 : 139127 : stridxlist *list = &decl_to_stridxlist_htab->get_or_insert (base, &existed);
667 : 139127 : if (existed)
668 : : {
669 : : int i;
670 : : stridxlist *before = NULL;
671 : 87808 : for (i = 0; i < 32; i++)
672 : : {
673 : 86970 : if (list->offset == off)
674 : 0 : return &list->idx;
675 : 86970 : if (list->offset > off && before == NULL)
676 : 86970 : before = list;
677 : 86970 : if (list->next == NULL)
678 : : break;
679 : 50750 : list = list->next;
680 : : }
681 : 37058 : if (i == 32)
682 : : return NULL;
683 : 36220 : if (before)
684 : : {
685 : 3431 : list = before;
686 : 3431 : before = XOBNEW (&stridx_obstack, struct stridxlist);
687 : 3431 : *before = *list;
688 : 3431 : list->next = before;
689 : 3431 : list->offset = off;
690 : 3431 : list->idx = 0;
691 : 3431 : return &list->idx;
692 : : }
693 : 32789 : list->next = XOBNEW (&stridx_obstack, struct stridxlist);
694 : 32789 : list = list->next;
695 : : }
696 : :
697 : 134858 : list->next = NULL;
698 : 134858 : list->offset = off;
699 : 134858 : list->idx = 0;
700 : 134858 : return &list->idx;
701 : : }
702 : :
703 : : /* Create a new string index, or return 0 if reached limit. */
704 : :
705 : : static int
706 : 721708 : new_stridx (tree exp)
707 : : {
708 : 721708 : int idx;
709 : 721708 : if (max_stridx >= param_max_tracked_strlens)
710 : : return 0;
711 : 721708 : if (TREE_CODE (exp) == SSA_NAME)
712 : : {
713 : 705424 : if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (exp))
714 : : return 0;
715 : 705424 : idx = max_stridx++;
716 : 705424 : ssa_ver_to_stridx[SSA_NAME_VERSION (exp)] = idx;
717 : 705424 : return idx;
718 : : }
719 : 16284 : if (TREE_CODE (exp) == ADDR_EXPR)
720 : : {
721 : 16256 : int *pidx = addr_stridxptr (TREE_OPERAND (exp, 0));
722 : 16256 : if (pidx != NULL)
723 : : {
724 : 16247 : gcc_assert (*pidx == 0);
725 : 16247 : *pidx = max_stridx++;
726 : 16247 : return *pidx;
727 : : }
728 : : }
729 : : return 0;
730 : : }
731 : :
732 : : /* Like new_stridx, but for ADDR_EXPR's operand instead. */
733 : :
734 : : static int
735 : 146062 : new_addr_stridx (tree exp)
736 : : {
737 : 146062 : int *pidx;
738 : 146062 : if (max_stridx >= param_max_tracked_strlens)
739 : : return 0;
740 : 146062 : pidx = addr_stridxptr (exp);
741 : 146062 : if (pidx != NULL)
742 : : {
743 : 122042 : gcc_assert (*pidx == 0);
744 : 122042 : *pidx = max_stridx++;
745 : 122042 : return *pidx;
746 : : }
747 : : return 0;
748 : : }
749 : :
750 : : /* Create a new strinfo. */
751 : :
752 : : static strinfo *
753 : 807649 : new_strinfo (tree ptr, int idx, tree nonzero_chars, bool full_string_p)
754 : : {
755 : 807649 : strinfo *si = strinfo_pool.allocate ();
756 : 807649 : si->nonzero_chars = nonzero_chars;
757 : 807649 : STRIP_USELESS_TYPE_CONVERSION (ptr);
758 : 807649 : si->ptr = ptr;
759 : 807649 : si->stmt = NULL;
760 : 807649 : si->alloc = NULL;
761 : 807649 : si->endptr = NULL_TREE;
762 : 807649 : si->refcount = 1;
763 : 807649 : si->idx = idx;
764 : 807649 : si->first = 0;
765 : 807649 : si->prev = 0;
766 : 807649 : si->next = 0;
767 : 807649 : si->writable = false;
768 : 807649 : si->dont_invalidate = false;
769 : 807649 : si->full_string_p = full_string_p;
770 : 807649 : return si;
771 : : }
772 : :
773 : : /* Decrease strinfo refcount and free it if not referenced anymore. */
774 : :
775 : : static inline void
776 : 11881305 : free_strinfo (strinfo *si)
777 : : {
778 : 11881305 : if (si && --si->refcount == 0)
779 : 807649 : strinfo_pool.remove (si);
780 : 11881305 : }
781 : :
782 : : /* Set strinfo in the vector entry IDX to SI. */
783 : :
784 : : static inline void
785 : 1136016 : set_strinfo (int idx, strinfo *si)
786 : : {
787 : 1136016 : if (vec_safe_length (stridx_to_strinfo) && (*stridx_to_strinfo)[0])
788 : 279781 : unshare_strinfo_vec ();
789 : 1136016 : if (vec_safe_length (stridx_to_strinfo) <= (unsigned int) idx)
790 : 800135 : vec_safe_grow_cleared (stridx_to_strinfo, idx + 1, true);
791 : 1136016 : (*stridx_to_strinfo)[idx] = si;
792 : 1136016 : }
793 : :
794 : : /* Return the first strinfo in the related strinfo chain
795 : : if all strinfos in between belong to the chain, otherwise NULL. */
796 : :
797 : : static strinfo *
798 : 21226 : verify_related_strinfos (strinfo *origsi)
799 : : {
800 : 21226 : strinfo *si = origsi, *psi;
801 : :
802 : 21226 : if (origsi->first == 0)
803 : : return NULL;
804 : 7439 : for (; si->prev; si = psi)
805 : : {
806 : 4560 : if (si->first != origsi->first)
807 : : return NULL;
808 : 4560 : psi = get_strinfo (si->prev);
809 : 4560 : if (psi == NULL)
810 : : return NULL;
811 : 4560 : if (psi->next != si->idx)
812 : : return NULL;
813 : : }
814 : 2879 : if (si->idx != si->first)
815 : : return NULL;
816 : : return si;
817 : : }
818 : :
819 : : /* Set SI's endptr to ENDPTR and compute its length based on SI->ptr.
820 : : Use LOC for folding. */
821 : :
822 : : static void
823 : 205 : set_endptr_and_length (location_t loc, strinfo *si, tree endptr)
824 : : {
825 : 205 : si->endptr = endptr;
826 : 205 : si->stmt = NULL;
827 : 205 : tree start_as_size = fold_convert_loc (loc, size_type_node, si->ptr);
828 : 205 : tree end_as_size = fold_convert_loc (loc, size_type_node, endptr);
829 : 205 : si->nonzero_chars = fold_build2_loc (loc, MINUS_EXPR, size_type_node,
830 : : end_as_size, start_as_size);
831 : 205 : si->full_string_p = true;
832 : 205 : }
833 : :
834 : : /* Return the string length, or NULL if it can't be computed.
835 : : The length may but need not be constant. Instead, it might be
836 : : the result of a strlen() call. */
837 : :
838 : : static tree
839 : 5809 : get_string_length (strinfo *si)
840 : : {
841 : : /* If the length has already been computed return it if it's exact
842 : : (i.e., the string is nul-terminated at NONZERO_CHARS), or return
843 : : null if it isn't. */
844 : 5809 : if (si->nonzero_chars)
845 : 11003 : return si->full_string_p ? si->nonzero_chars : NULL;
846 : :
847 : : /* If the string is the result of one of the built-in calls below
848 : : attempt to compute the length from the call statement. */
849 : 205 : if (si->stmt)
850 : : {
851 : 204 : gimple *stmt = si->stmt, *lenstmt;
852 : 204 : tree callee, lhs, fn, tem;
853 : 204 : location_t loc;
854 : 204 : gimple_stmt_iterator gsi;
855 : :
856 : 204 : gcc_assert (is_gimple_call (stmt));
857 : 204 : callee = gimple_call_fndecl (stmt);
858 : 204 : gcc_assert (callee && fndecl_built_in_p (callee, BUILT_IN_NORMAL));
859 : 204 : lhs = gimple_call_lhs (stmt);
860 : : /* unshare_strinfo is intentionally not called here. The (delayed)
861 : : transformation of strcpy or strcat into stpcpy is done at the place
862 : : of the former strcpy/strcat call and so can affect all the strinfos
863 : : with the same stmt. If they were unshared before and transformation
864 : : has been already done, the handling of BUILT_IN_STPCPY{,_CHK} should
865 : : just compute the right length. */
866 : 204 : switch (DECL_FUNCTION_CODE (callee))
867 : : {
868 : 3 : case BUILT_IN_STRCAT:
869 : 3 : case BUILT_IN_STRCAT_CHK:
870 : 3 : gsi = gsi_for_stmt (stmt);
871 : 3 : fn = builtin_decl_implicit (BUILT_IN_STRLEN);
872 : 3 : gcc_assert (lhs == NULL_TREE);
873 : 3 : tem = unshare_expr (gimple_call_arg (stmt, 0));
874 : 3 : lenstmt = gimple_build_call (fn, 1, tem);
875 : 3 : lhs = make_ssa_name (TREE_TYPE (TREE_TYPE (fn)), lenstmt);
876 : 3 : gimple_call_set_lhs (lenstmt, lhs);
877 : 6 : gimple_set_vuse (lenstmt, gimple_vuse (stmt));
878 : 3 : gsi_insert_before (&gsi, lenstmt, GSI_SAME_STMT);
879 : 3 : tem = gimple_call_arg (stmt, 0);
880 : 3 : if (!ptrofftype_p (TREE_TYPE (lhs)))
881 : : {
882 : 0 : lhs = convert_to_ptrofftype (lhs);
883 : 0 : lhs = force_gimple_operand_gsi (&gsi, lhs, true, NULL_TREE,
884 : : true, GSI_SAME_STMT);
885 : : }
886 : 3 : lenstmt = gimple_build_assign
887 : 3 : (make_ssa_name (TREE_TYPE (gimple_call_arg (stmt, 0))),
888 : : POINTER_PLUS_EXPR,tem, lhs);
889 : 3 : gsi_insert_before (&gsi, lenstmt, GSI_SAME_STMT);
890 : 3 : gimple_call_set_arg (stmt, 0, gimple_assign_lhs (lenstmt));
891 : 3 : lhs = NULL_TREE;
892 : : /* FALLTHRU */
893 : 202 : case BUILT_IN_STRCPY:
894 : 202 : case BUILT_IN_STRCPY_CHK:
895 : 202 : gcc_assert (builtin_decl_implicit_p (BUILT_IN_STPCPY));
896 : 202 : if (gimple_call_num_args (stmt) == 2)
897 : : fn = builtin_decl_implicit (BUILT_IN_STPCPY);
898 : : else
899 : 1 : fn = builtin_decl_explicit (BUILT_IN_STPCPY_CHK);
900 : 202 : gcc_assert (lhs == NULL_TREE);
901 : 202 : if (dump_file && (dump_flags & TDF_DETAILS) != 0)
902 : : {
903 : 0 : fprintf (dump_file, "Optimizing: ");
904 : 0 : print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
905 : : }
906 : 202 : gimple_call_set_fndecl (stmt, fn);
907 : 202 : lhs = make_ssa_name (TREE_TYPE (TREE_TYPE (fn)), stmt);
908 : 202 : gimple_call_set_lhs (stmt, lhs);
909 : 202 : update_stmt (stmt);
910 : 202 : if (dump_file && (dump_flags & TDF_DETAILS) != 0)
911 : : {
912 : 0 : fprintf (dump_file, "into: ");
913 : 0 : print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
914 : : }
915 : : /* FALLTHRU */
916 : 202 : case BUILT_IN_STPCPY:
917 : 202 : case BUILT_IN_STPCPY_CHK:
918 : 202 : gcc_assert (lhs != NULL_TREE);
919 : 202 : loc = gimple_location (stmt);
920 : 202 : set_endptr_and_length (loc, si, lhs);
921 : 202 : for (strinfo *chainsi = verify_related_strinfos (si);
922 : 213 : chainsi != NULL;
923 : 11 : chainsi = get_next_strinfo (chainsi))
924 : 11 : if (chainsi->nonzero_chars == NULL)
925 : 3 : set_endptr_and_length (loc, chainsi, lhs);
926 : : break;
927 : : case BUILT_IN_ALLOCA:
928 : : case BUILT_IN_ALLOCA_WITH_ALIGN:
929 : : case BUILT_IN_MALLOC:
930 : : break;
931 : : /* BUILT_IN_CALLOC always has si->nonzero_chars set. */
932 : 0 : default:
933 : 0 : gcc_unreachable ();
934 : 204 : break;
935 : : }
936 : : }
937 : :
938 : 205 : return si->nonzero_chars;
939 : : }
940 : :
941 : : /* Dump strlen data to FP for statement STMT. When non-null, RVALS
942 : : points to the valuation engine used to calculate ranges, and is
943 : : used to dump strlen range for non-constant results. */
944 : :
945 : : DEBUG_FUNCTION void
946 : 0 : dump_strlen_info (FILE *fp, gimple *stmt, range_query *rvals)
947 : : {
948 : 0 : if (stmt)
949 : : {
950 : 0 : fprintf (fp, "\nDumping strlen pass data after ");
951 : 0 : print_gimple_expr (fp, stmt, TDF_LINENO);
952 : 0 : fputc ('\n', fp);
953 : : }
954 : : else
955 : 0 : fprintf (fp, "\nDumping strlen pass data\n");
956 : :
957 : 0 : fprintf (fp, "max_stridx = %i\n", max_stridx);
958 : 0 : fprintf (fp, "ssa_ver_to_stridx has %u elements\n",
959 : : ssa_ver_to_stridx.length ());
960 : 0 : fprintf (fp, "stridx_to_strinfo");
961 : 0 : if (stridx_to_strinfo)
962 : : {
963 : 0 : fprintf (fp, " has %u elements\n", stridx_to_strinfo->length ());
964 : 0 : for (unsigned i = 0; i != stridx_to_strinfo->length (); ++i)
965 : : {
966 : 0 : if (strinfo *si = (*stridx_to_strinfo)[i])
967 : : {
968 : 0 : if (!si->idx)
969 : 0 : continue;
970 : 0 : fprintf (fp, " idx = %i", si->idx);
971 : 0 : if (si->ptr)
972 : : {
973 : 0 : fprintf (fp, ", ptr = ");
974 : 0 : print_generic_expr (fp, si->ptr);
975 : : }
976 : :
977 : 0 : if (si->nonzero_chars)
978 : : {
979 : 0 : fprintf (fp, ", nonzero_chars = ");
980 : 0 : print_generic_expr (fp, si->nonzero_chars);
981 : 0 : if (TREE_CODE (si->nonzero_chars) == SSA_NAME)
982 : : {
983 : 0 : int_range_max vr;
984 : 0 : if (rvals)
985 : 0 : rvals->range_of_expr (vr, si->nonzero_chars,
986 : : si->stmt);
987 : : else
988 : 0 : get_range_query (cfun)->range_of_expr (vr,
989 : : si->nonzero_chars);
990 : 0 : vr.dump (fp);
991 : 0 : }
992 : : }
993 : :
994 : 0 : fprintf (fp, ", refcount = %i", si->refcount);
995 : 0 : if (si->stmt)
996 : : {
997 : 0 : fprintf (fp, ", stmt = ");
998 : 0 : print_gimple_expr (fp, si->stmt, 0);
999 : : }
1000 : 0 : if (si->alloc)
1001 : : {
1002 : 0 : fprintf (fp, ", alloc = ");
1003 : 0 : print_gimple_expr (fp, si->alloc, 0);
1004 : : }
1005 : 0 : if (si->writable)
1006 : 0 : fprintf (fp, ", writable");
1007 : 0 : if (si->dont_invalidate)
1008 : 0 : fprintf (fp, ", dont_invalidate");
1009 : 0 : if (si->full_string_p)
1010 : 0 : fprintf (fp, ", full_string_p");
1011 : 0 : if (strinfo *next = get_next_strinfo (si))
1012 : : {
1013 : 0 : fprintf (fp, ", {");
1014 : 0 : do
1015 : 0 : fprintf (fp, "%i%s", next->idx, next->first ? ", " : "");
1016 : 0 : while ((next = get_next_strinfo (next)));
1017 : 0 : fprintf (fp, "}");
1018 : : }
1019 : 0 : fputs ("\n", fp);
1020 : : }
1021 : : }
1022 : : }
1023 : : else
1024 : 0 : fprintf (fp, " = null\n");
1025 : :
1026 : 0 : fprintf (fp, "decl_to_stridxlist_htab");
1027 : 0 : if (decl_to_stridxlist_htab)
1028 : : {
1029 : 0 : fputs ("\n", fp);
1030 : 0 : typedef decl_to_stridxlist_htab_t::iterator iter_t;
1031 : 0 : for (iter_t it = decl_to_stridxlist_htab->begin ();
1032 : 0 : it != decl_to_stridxlist_htab->end (); ++it)
1033 : : {
1034 : 0 : tree decl = (*it).first;
1035 : 0 : stridxlist *list = &(*it).second;
1036 : 0 : fprintf (fp, " decl = ");
1037 : 0 : print_generic_expr (fp, decl);
1038 : 0 : if (list)
1039 : : {
1040 : 0 : fprintf (fp, ", offsets = {");
1041 : 0 : for (; list; list = list->next)
1042 : 0 : fprintf (fp, "%lli%s", (long long) list->offset,
1043 : 0 : list->next ? ", " : "");
1044 : 0 : fputs ("}", fp);
1045 : : }
1046 : 0 : fputs ("\n", fp);
1047 : : }
1048 : : }
1049 : : else
1050 : 0 : fprintf (fp, " = null\n");
1051 : :
1052 : 0 : if (laststmt.stmt)
1053 : : {
1054 : 0 : fprintf (fp, "laststmt = ");
1055 : 0 : print_gimple_expr (fp, laststmt.stmt, 0);
1056 : 0 : fprintf (fp, ", len = ");
1057 : 0 : print_generic_expr (fp, laststmt.len);
1058 : 0 : fprintf (fp, ", stridx = %i\n", laststmt.stridx);
1059 : : }
1060 : 0 : }
1061 : :
1062 : : /* Helper of get_range_strlen_dynamic(). See below. */
1063 : :
1064 : : static bool
1065 : 2990 : get_range_strlen_phi (tree src, gphi *phi,
1066 : : c_strlen_data *pdata, bitmap visited,
1067 : : pointer_query *ptr_qry, unsigned *pssa_def_max)
1068 : : {
1069 : 2990 : if (!bitmap_set_bit (visited, SSA_NAME_VERSION (src)))
1070 : : return true;
1071 : :
1072 : 2891 : if (*pssa_def_max == 0)
1073 : : return false;
1074 : :
1075 : 2881 : --*pssa_def_max;
1076 : :
1077 : : /* Iterate over the PHI arguments and determine the minimum and maximum
1078 : : length/size of each and incorporate them into the overall result. */
1079 : 12260 : for (unsigned i = 0; i != gimple_phi_num_args (phi); ++i)
1080 : : {
1081 : 9379 : tree arg = gimple_phi_arg_def (phi, i);
1082 : 9379 : if (arg == gimple_phi_result (phi))
1083 : 3438 : continue;
1084 : :
1085 : 9379 : c_strlen_data argdata = { };
1086 : 9379 : if (!get_range_strlen_dynamic (arg, phi, &argdata, visited, ptr_qry,
1087 : : pssa_def_max))
1088 : : {
1089 : 330 : pdata->maxlen = build_all_ones_cst (size_type_node);
1090 : 330 : continue;
1091 : : }
1092 : :
1093 : : /* Set the DECL of an unterminated array this argument refers to
1094 : : if one hasn't been found yet. */
1095 : 9049 : if (!pdata->decl && argdata.decl)
1096 : 9 : pdata->decl = argdata.decl;
1097 : :
1098 : 12157 : if (!argdata.minlen
1099 : 9049 : || (integer_zerop (argdata.minlen)
1100 : 3451 : && (!argdata.maxbound
1101 : 842 : || integer_all_onesp (argdata.maxbound))
1102 : 2609 : && integer_all_onesp (argdata.maxlen)))
1103 : : {
1104 : : /* Set the upper bound of the length to unbounded. */
1105 : 3108 : pdata->maxlen = build_all_ones_cst (size_type_node);
1106 : 3108 : continue;
1107 : : }
1108 : :
1109 : : /* Adjust the minimum and maximum length determined so far and
1110 : : the upper bound on the array size. */
1111 : 5941 : if (TREE_CODE (argdata.minlen) == INTEGER_CST
1112 : 5941 : && (!pdata->minlen
1113 : 4474 : || tree_int_cst_lt (argdata.minlen, pdata->minlen)))
1114 : 2893 : pdata->minlen = argdata.minlen;
1115 : :
1116 : 5941 : if (TREE_CODE (argdata.maxlen) == INTEGER_CST
1117 : 5941 : && (!pdata->maxlen
1118 : : || (argdata.maxlen
1119 : 4625 : && tree_int_cst_lt (pdata->maxlen, argdata.maxlen))))
1120 : 2016 : pdata->maxlen = argdata.maxlen;
1121 : :
1122 : 5941 : if (!pdata->maxbound
1123 : 5817 : || TREE_CODE (pdata->maxbound) != INTEGER_CST
1124 : 10401 : || (argdata.maxbound
1125 : 4458 : && tree_int_cst_lt (pdata->maxbound, argdata.maxbound)
1126 : 785 : && !integer_all_onesp (argdata.maxbound)))
1127 : 2266 : pdata->maxbound = argdata.maxbound;
1128 : : }
1129 : :
1130 : : return true;
1131 : : }
1132 : :
1133 : : /* Return the maximum possible length of the string PTR that's less
1134 : : than MAXLEN given the size of the object of subobject it points
1135 : : to at the given STMT. MAXLEN is the maximum length of the string
1136 : : determined so far. Return null when no such maximum can be
1137 : : determined. */
1138 : :
1139 : : static tree
1140 : 1827 : get_maxbound (tree ptr, gimple *stmt, offset_int maxlen,
1141 : : pointer_query *ptr_qry)
1142 : : {
1143 : 1827 : access_ref aref;
1144 : 1827 : if (!ptr_qry->get_ref (ptr, stmt, &aref))
1145 : : return NULL_TREE;
1146 : :
1147 : 1827 : offset_int sizrem = aref.size_remaining ();
1148 : 1827 : if (sizrem <= 0)
1149 : : return NULL_TREE;
1150 : :
1151 : 1827 : if (sizrem < maxlen)
1152 : 419 : maxlen = sizrem - 1;
1153 : :
1154 : : /* Try to determine the maximum from the subobject at the offset.
1155 : : This handles MEM [&some-struct, member-offset] that's often
1156 : : the result of folding COMPONENT_REF [some-struct, member]. */
1157 : 1827 : tree reftype = TREE_TYPE (aref.ref);
1158 : 1827 : if (!RECORD_OR_UNION_TYPE_P (reftype)
1159 : 9 : || aref.offrng[0] != aref.offrng[1]
1160 : 1836 : || !wi::fits_shwi_p (aref.offrng[0]))
1161 : 1818 : return wide_int_to_tree (size_type_node, maxlen);
1162 : :
1163 : 9 : HOST_WIDE_INT off = aref.offrng[0].to_shwi ();
1164 : 9 : tree fld = field_at_offset (reftype, NULL_TREE, off);
1165 : 9 : if (!fld || !DECL_SIZE_UNIT (fld))
1166 : 1 : return wide_int_to_tree (size_type_node, maxlen);
1167 : :
1168 : 8 : offset_int size = wi::to_offset (DECL_SIZE_UNIT (fld));
1169 : 8 : if (maxlen < size)
1170 : 0 : return wide_int_to_tree (size_type_node, maxlen);
1171 : :
1172 : 8 : return wide_int_to_tree (size_type_node, size - 1);
1173 : : }
1174 : :
1175 : : /* Attempt to determine the length of the string SRC. On success, store
1176 : : the length in *PDATA and return true. Otherwise, return false.
1177 : : VISITED is a bitmap of visited PHI nodes. RVALS points to the valuation
1178 : : engine used to calculate ranges. PSSA_DEF_MAX to an SSA_NAME
1179 : : assignment limit used to prevent runaway recursion. */
1180 : :
1181 : : static bool
1182 : 488516 : get_range_strlen_dynamic (tree src, gimple *stmt,
1183 : : c_strlen_data *pdata, bitmap visited,
1184 : : pointer_query *ptr_qry, unsigned *pssa_def_max)
1185 : : {
1186 : 488516 : int idx = get_stridx (src, stmt);
1187 : 488516 : if (!idx)
1188 : : {
1189 : 275325 : if (TREE_CODE (src) == SSA_NAME)
1190 : : {
1191 : 269657 : gimple *def_stmt = SSA_NAME_DEF_STMT (src);
1192 : 269657 : if (gphi *phi = dyn_cast<gphi *>(def_stmt))
1193 : 2990 : return get_range_strlen_phi (src, phi, pdata, visited, ptr_qry,
1194 : 2990 : pssa_def_max);
1195 : : }
1196 : :
1197 : : /* Return success regardless of the result and handle *PDATA
1198 : : in the caller. */
1199 : 272335 : get_range_strlen (src, pdata, 1);
1200 : 272335 : return true;
1201 : : }
1202 : :
1203 : 213191 : if (idx < 0)
1204 : : {
1205 : : /* SRC is a string of constant length. */
1206 : 207294 : pdata->minlen = build_int_cst (size_type_node, ~idx);
1207 : 207294 : pdata->maxlen = pdata->minlen;
1208 : 207294 : pdata->maxbound = pdata->maxlen;
1209 : 207294 : return true;
1210 : : }
1211 : :
1212 : 5897 : if (strinfo *si = get_strinfo (idx))
1213 : : {
1214 : 2057 : pdata->minlen = get_string_length (si);
1215 : 2057 : if (!pdata->minlen && si->nonzero_chars)
1216 : : {
1217 : 39 : if (TREE_CODE (si->nonzero_chars) == INTEGER_CST)
1218 : 39 : pdata->minlen = si->nonzero_chars;
1219 : 0 : else if (TREE_CODE (si->nonzero_chars) == SSA_NAME)
1220 : : {
1221 : 0 : int_range_max vr;
1222 : 0 : ptr_qry->rvals->range_of_expr (vr, si->nonzero_chars, si->stmt);
1223 : 0 : if (vr.undefined_p () || vr.varying_p ())
1224 : 0 : pdata->minlen = build_zero_cst (size_type_node);
1225 : : else
1226 : : {
1227 : 0 : tree type = vr.type ();
1228 : 0 : pdata->minlen = wide_int_to_tree (type, vr.lower_bound ());
1229 : : }
1230 : 0 : }
1231 : : else
1232 : 0 : pdata->minlen = build_zero_cst (size_type_node);
1233 : :
1234 : 39 : tree base = si->ptr;
1235 : 39 : if (TREE_CODE (base) == ADDR_EXPR)
1236 : 39 : base = TREE_OPERAND (base, 0);
1237 : :
1238 : 39 : HOST_WIDE_INT off;
1239 : 39 : poly_int64 poff;
1240 : 39 : base = get_addr_base_and_unit_offset (base, &poff);
1241 : 39 : if (base
1242 : 39 : && DECL_P (base)
1243 : 39 : && TREE_CODE (TREE_TYPE (base)) == ARRAY_TYPE
1244 : 37 : && TYPE_SIZE_UNIT (TREE_TYPE (base))
1245 : 39 : && poff.is_constant (&off))
1246 : : {
1247 : 30 : tree basetype = TREE_TYPE (base);
1248 : 30 : tree size = TYPE_SIZE_UNIT (basetype);
1249 : 30 : if (TREE_CODE (size) == INTEGER_CST)
1250 : : {
1251 : 30 : ++off; /* Increment for the terminating nul. */
1252 : 30 : tree toffset = build_int_cst (size_type_node, off);
1253 : 30 : pdata->maxlen = fold_build2 (MINUS_EXPR, size_type_node,
1254 : : size, toffset);
1255 : 30 : if (tree_int_cst_lt (pdata->maxlen, pdata->minlen))
1256 : : /* This can happen when triggering UB, when base is an
1257 : : array which is known to be filled with at least size
1258 : : non-zero bytes. E.g. for
1259 : : char a[2]; memcpy (a, "12", sizeof a);
1260 : : We don't want to create an invalid range [2, 1]
1261 : : where 2 comes from the number of non-zero bytes and
1262 : : 1 from longest valid zero-terminated string that can
1263 : : be stored in such an array, so pick just one of
1264 : : those, pdata->minlen. See PR110603. */
1265 : 3 : pdata->maxlen = build_all_ones_cst (size_type_node);
1266 : : else
1267 : 27 : pdata->maxbound = pdata->maxlen;
1268 : : }
1269 : : else
1270 : 0 : pdata->maxlen = build_all_ones_cst (size_type_node);
1271 : : }
1272 : : else
1273 : 9 : pdata->maxlen = build_all_ones_cst (size_type_node);
1274 : 39 : }
1275 : 2018 : else if (pdata->minlen && TREE_CODE (pdata->minlen) == SSA_NAME)
1276 : : {
1277 : 1827 : int_range_max vr;
1278 : 1827 : ptr_qry->rvals->range_of_expr (vr, si->nonzero_chars, stmt);
1279 : 1827 : if (vr.varying_p () || vr.undefined_p ())
1280 : : {
1281 : 0 : pdata->minlen = build_zero_cst (size_type_node);
1282 : 0 : pdata->maxlen = build_all_ones_cst (size_type_node);
1283 : : }
1284 : : else
1285 : : {
1286 : 1827 : tree type = vr.type ();
1287 : 1827 : pdata->minlen = wide_int_to_tree (type, vr.lower_bound ());
1288 : 1827 : pdata->maxlen = wide_int_to_tree (type, vr.upper_bound ());
1289 : 1827 : offset_int max = offset_int::from (vr.upper_bound (0), SIGNED);
1290 : 1827 : if (tree maxbound = get_maxbound (si->ptr, stmt, max, ptr_qry))
1291 : 1827 : pdata->maxbound = maxbound;
1292 : : else
1293 : 0 : pdata->maxbound = pdata->maxlen;
1294 : : }
1295 : 1827 : }
1296 : 191 : else if (pdata->minlen && TREE_CODE (pdata->minlen) == INTEGER_CST)
1297 : : {
1298 : 184 : pdata->maxlen = pdata->minlen;
1299 : 184 : pdata->maxbound = pdata->minlen;
1300 : : }
1301 : : else
1302 : : {
1303 : : /* For PDATA->MINLEN that's a non-constant expression such
1304 : : as PLUS_EXPR whose value range is unknown, set the bounds
1305 : : to zero and SIZE_MAX. */
1306 : 7 : pdata->minlen = build_zero_cst (size_type_node);
1307 : 7 : pdata->maxlen = build_all_ones_cst (size_type_node);
1308 : : }
1309 : :
1310 : 2057 : return true;
1311 : : }
1312 : :
1313 : : return false;
1314 : : }
1315 : :
1316 : : /* Analogous to get_range_strlen but for dynamically created strings,
1317 : : i.e., those created by calls to strcpy as opposed to just string
1318 : : constants.
1319 : : Try to obtain the range of the lengths of the string(s) referenced
1320 : : by SRC, or the size of the largest array SRC refers to if the range
1321 : : of lengths cannot be determined, and store all in *PDATA. RVALS
1322 : : points to the valuation engine used to calculate ranges. */
1323 : :
1324 : : void
1325 : 479137 : get_range_strlen_dynamic (tree src, gimple *stmt, c_strlen_data *pdata,
1326 : : pointer_query &ptr_qry)
1327 : : {
1328 : 479137 : auto_bitmap visited;
1329 : 479137 : tree maxbound = pdata->maxbound;
1330 : :
1331 : 479137 : unsigned limit = param_ssa_name_def_chain_limit;
1332 : 479137 : if (!get_range_strlen_dynamic (src, stmt, pdata, visited, &ptr_qry, &limit))
1333 : : {
1334 : : /* On failure extend the length range to an impossible maximum
1335 : : (a valid MAXLEN must be less than PTRDIFF_MAX - 1). Other
1336 : : members can stay unchanged regardless. */
1337 : 3520 : pdata->minlen = ssize_int (0);
1338 : 3520 : pdata->maxlen = build_all_ones_cst (size_type_node);
1339 : : }
1340 : 475617 : else if (!pdata->minlen)
1341 : 1010 : pdata->minlen = ssize_int (0);
1342 : :
1343 : : /* If it's unchanged from it initial non-null value, set the conservative
1344 : : MAXBOUND to SIZE_MAX. Otherwise leave it null (if it is null). */
1345 : 479137 : if (maxbound && pdata->maxbound == maxbound)
1346 : 4546 : pdata->maxbound = build_all_ones_cst (size_type_node);
1347 : 479137 : }
1348 : :
1349 : : /* Invalidate string length information for strings whose length might
1350 : : change due to stores in STMT, except those marked DONT_INVALIDATE.
1351 : : For string-modifying statements, ZERO_WRITE is set when the statement
1352 : : wrote only zeros.
1353 : : Returns true if any STRIDX_TO_STRINFO entries were considered
1354 : : for invalidation. */
1355 : :
1356 : : static bool
1357 : 11159425 : maybe_invalidate (gimple *stmt, bool zero_write = false)
1358 : : {
1359 : 11159425 : if (dump_file && (dump_flags & TDF_DETAILS))
1360 : : {
1361 : 0 : fprintf (dump_file, "%s called for ", __func__);
1362 : 0 : print_gimple_stmt (dump_file, stmt, TDF_LINENO);
1363 : : }
1364 : :
1365 : : strinfo *si;
1366 : : bool nonempty = false;
1367 : :
1368 : 65328071 : for (unsigned i = 1; vec_safe_iterate (stridx_to_strinfo, i, &si); ++i)
1369 : : {
1370 : 54168646 : if (si == NULL || !POINTER_TYPE_P (TREE_TYPE (si->ptr)))
1371 : 51710953 : continue;
1372 : :
1373 : 3085910 : nonempty = true;
1374 : :
1375 : : /* Unconditionally reset DONT_INVALIDATE. */
1376 : 3085910 : bool dont_invalidate = si->dont_invalidate;
1377 : 3085910 : si->dont_invalidate = false;
1378 : :
1379 : 3085910 : if (dont_invalidate)
1380 : 299850 : continue;
1381 : :
1382 : 2786060 : ao_ref r;
1383 : 2786060 : tree size = si->nonzero_chars;
1384 : 2786060 : ao_ref_init_from_ptr_and_size (&r, si->ptr, size);
1385 : : /* Include the terminating nul in the size of the string
1386 : : to consider when determining possible clobber. But do not
1387 : : add it to 'size' since we don't know whether it would
1388 : : actually fit the allocated area. */
1389 : 2786060 : if (known_size_p (r.size))
1390 : : {
1391 : 2462493 : if (known_le (r.size, HOST_WIDE_INT_MAX - BITS_PER_UNIT))
1392 : 2462493 : r.max_size += BITS_PER_UNIT;
1393 : : else
1394 : 0 : r.max_size = -1;
1395 : : }
1396 : 2786060 : if (stmt_may_clobber_ref_p_1 (stmt, &r))
1397 : : {
1398 : 328367 : if (dump_file && (dump_flags & TDF_DETAILS))
1399 : : {
1400 : 0 : fputs (" statement may clobber object ", dump_file);
1401 : 0 : print_generic_expr (dump_file, si->ptr);
1402 : 0 : if (size && tree_fits_uhwi_p (size))
1403 : 0 : fprintf (dump_file, " " HOST_WIDE_INT_PRINT_UNSIGNED
1404 : : " bytes in size", tree_to_uhwi (size));
1405 : 0 : fputc ('\n', dump_file);
1406 : : }
1407 : :
1408 : 328367 : set_strinfo (i, NULL);
1409 : 328367 : free_strinfo (si);
1410 : 328367 : continue;
1411 : : }
1412 : :
1413 : 2457693 : if (size
1414 : 2457693 : && !zero_write
1415 : 2190198 : && si->stmt
1416 : 5065 : && is_gimple_call (si->stmt)
1417 : 2462758 : && (DECL_FUNCTION_CODE (gimple_call_fndecl (si->stmt))
1418 : : == BUILT_IN_CALLOC))
1419 : : {
1420 : : /* If the clobber test above considered the length of
1421 : : the string (including the nul), then for (potentially)
1422 : : non-zero writes that might modify storage allocated by
1423 : : calloc consider the whole object and if it might be
1424 : : clobbered by the statement reset the statement. */
1425 : 5065 : ao_ref_init_from_ptr_and_size (&r, si->ptr, NULL_TREE);
1426 : 5065 : if (stmt_may_clobber_ref_p_1 (stmt, &r))
1427 : 57 : si->stmt = NULL;
1428 : : }
1429 : : }
1430 : :
1431 : 11159425 : if (dump_file && (dump_flags & TDF_DETAILS))
1432 : 0 : fprintf (dump_file, "%s returns %i\n", __func__, nonempty);
1433 : :
1434 : 11159425 : return nonempty;
1435 : : }
1436 : :
1437 : : /* Unshare strinfo record SI, if it has refcount > 1 or
1438 : : if stridx_to_strinfo vector is shared with some other
1439 : : bbs. */
1440 : :
1441 : : static strinfo *
1442 : 30723 : unshare_strinfo (strinfo *si)
1443 : : {
1444 : 30723 : strinfo *nsi;
1445 : :
1446 : 30723 : if (si->refcount == 1 && !strinfo_shared ())
1447 : : return si;
1448 : :
1449 : 4523 : nsi = new_strinfo (si->ptr, si->idx, si->nonzero_chars, si->full_string_p);
1450 : 4523 : nsi->stmt = si->stmt;
1451 : 4523 : nsi->alloc = si->alloc;
1452 : 4523 : nsi->endptr = si->endptr;
1453 : 4523 : nsi->first = si->first;
1454 : 4523 : nsi->prev = si->prev;
1455 : 4523 : nsi->next = si->next;
1456 : 4523 : nsi->writable = si->writable;
1457 : 4523 : set_strinfo (si->idx, nsi);
1458 : 4523 : free_strinfo (si);
1459 : 4523 : return nsi;
1460 : : }
1461 : :
1462 : : /* Attempt to create a new strinfo for BASESI + OFF, or find existing
1463 : : strinfo if there is any. Return it's idx, or 0 if no strinfo has
1464 : : been created. */
1465 : :
1466 : : static int
1467 : 2813 : get_stridx_plus_constant (strinfo *basesi, unsigned HOST_WIDE_INT off,
1468 : : tree ptr)
1469 : : {
1470 : 2813 : if (TREE_CODE (ptr) == SSA_NAME && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ptr))
1471 : : return 0;
1472 : :
1473 : 2813 : if (compare_nonzero_chars (basesi, off) < 0
1474 : 2813 : || !tree_fits_uhwi_p (basesi->nonzero_chars))
1475 : : return 0;
1476 : :
1477 : 2813 : unsigned HOST_WIDE_INT nonzero_chars
1478 : 2813 : = tree_to_uhwi (basesi->nonzero_chars) - off;
1479 : 2813 : strinfo *si = basesi, *chainsi;
1480 : 2813 : if (si->first || si->prev || si->next)
1481 : 646 : si = verify_related_strinfos (basesi);
1482 : 646 : if (si == NULL
1483 : 2813 : || si->nonzero_chars == NULL_TREE
1484 : 2813 : || TREE_CODE (si->nonzero_chars) != INTEGER_CST)
1485 : : return 0;
1486 : :
1487 : 2813 : if (TREE_CODE (ptr) == SSA_NAME
1488 : 4084 : && ssa_ver_to_stridx.length () <= SSA_NAME_VERSION (ptr))
1489 : 0 : ssa_ver_to_stridx.safe_grow_cleared (num_ssa_names, true);
1490 : :
1491 : 2813 : gcc_checking_assert (compare_tree_int (si->nonzero_chars, off) != -1);
1492 : 4993 : for (chainsi = si; chainsi->next; chainsi = si)
1493 : : {
1494 : 2180 : si = get_next_strinfo (chainsi);
1495 : 2180 : if (si == NULL
1496 : 2180 : || si->nonzero_chars == NULL_TREE
1497 : 2180 : || TREE_CODE (si->nonzero_chars) != INTEGER_CST)
1498 : : break;
1499 : 2180 : int r = compare_tree_int (si->nonzero_chars, nonzero_chars);
1500 : 2180 : if (r != 1)
1501 : : {
1502 : 0 : if (r == 0)
1503 : : {
1504 : 0 : if (TREE_CODE (ptr) == SSA_NAME)
1505 : 0 : ssa_ver_to_stridx[SSA_NAME_VERSION (ptr)] = si->idx;
1506 : : else
1507 : : {
1508 : 0 : int *pidx = addr_stridxptr (TREE_OPERAND (ptr, 0));
1509 : 0 : if (pidx != NULL && *pidx == 0)
1510 : 0 : *pidx = si->idx;
1511 : : }
1512 : 0 : return si->idx;
1513 : : }
1514 : : break;
1515 : : }
1516 : : }
1517 : :
1518 : 2813 : int idx = new_stridx (ptr);
1519 : 2813 : if (idx == 0)
1520 : : return 0;
1521 : 2813 : si = new_strinfo (ptr, idx, build_int_cst (size_type_node, nonzero_chars),
1522 : 2813 : basesi->full_string_p);
1523 : 2813 : set_strinfo (idx, si);
1524 : 2813 : if (strinfo *nextsi = get_strinfo (chainsi->next))
1525 : : {
1526 : 0 : nextsi = unshare_strinfo (nextsi);
1527 : 0 : si->next = nextsi->idx;
1528 : 0 : nextsi->prev = idx;
1529 : : }
1530 : 2813 : chainsi = unshare_strinfo (chainsi);
1531 : 2813 : if (chainsi->first == 0)
1532 : 2167 : chainsi->first = chainsi->idx;
1533 : 2813 : chainsi->next = idx;
1534 : 2813 : if (chainsi->endptr == NULL_TREE && zero_length_string_p (si))
1535 : 123 : chainsi->endptr = ptr;
1536 : 2813 : si->endptr = chainsi->endptr;
1537 : 2813 : si->prev = chainsi->idx;
1538 : 2813 : si->first = chainsi->first;
1539 : 2813 : si->writable = chainsi->writable;
1540 : 2813 : return si->idx;
1541 : : }
1542 : :
1543 : : /* Note that PTR, a pointer SSA_NAME initialized in the current stmt, points
1544 : : to a zero-length string and if possible chain it to a related strinfo
1545 : : chain whose part is or might be CHAINSI. */
1546 : :
1547 : : static strinfo *
1548 : 2164 : zero_length_string (tree ptr, strinfo *chainsi)
1549 : : {
1550 : 2164 : strinfo *si;
1551 : 2164 : int idx;
1552 : 4328 : if (ssa_ver_to_stridx.length () <= SSA_NAME_VERSION (ptr))
1553 : 0 : ssa_ver_to_stridx.safe_grow_cleared (num_ssa_names, true);
1554 : 2164 : gcc_checking_assert (TREE_CODE (ptr) == SSA_NAME
1555 : : && ssa_ver_to_stridx[SSA_NAME_VERSION (ptr)] == 0);
1556 : :
1557 : 2164 : if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ptr))
1558 : : return NULL;
1559 : 2164 : if (chainsi != NULL)
1560 : : {
1561 : 2164 : si = verify_related_strinfos (chainsi);
1562 : 2164 : if (si)
1563 : : {
1564 : 265 : do
1565 : : {
1566 : : /* We shouldn't mix delayed and non-delayed lengths. */
1567 : 265 : gcc_assert (si->full_string_p);
1568 : 265 : if (si->endptr == NULL_TREE)
1569 : : {
1570 : 221 : si = unshare_strinfo (si);
1571 : 221 : si->endptr = ptr;
1572 : : }
1573 : 265 : chainsi = si;
1574 : 265 : si = get_next_strinfo (si);
1575 : : }
1576 : 265 : while (si != NULL);
1577 : 108 : if (zero_length_string_p (chainsi))
1578 : : {
1579 : 16 : if (chainsi->next)
1580 : : {
1581 : 0 : chainsi = unshare_strinfo (chainsi);
1582 : 0 : chainsi->next = 0;
1583 : : }
1584 : 16 : ssa_ver_to_stridx[SSA_NAME_VERSION (ptr)] = chainsi->idx;
1585 : 16 : return chainsi;
1586 : : }
1587 : : }
1588 : : else
1589 : : {
1590 : : /* We shouldn't mix delayed and non-delayed lengths. */
1591 : 2056 : gcc_assert (chainsi->full_string_p);
1592 : 2056 : if (chainsi->first || chainsi->prev || chainsi->next)
1593 : : {
1594 : 0 : chainsi = unshare_strinfo (chainsi);
1595 : 0 : chainsi->first = 0;
1596 : 0 : chainsi->prev = 0;
1597 : 0 : chainsi->next = 0;
1598 : : }
1599 : : }
1600 : : }
1601 : 2148 : idx = new_stridx (ptr);
1602 : 2148 : if (idx == 0)
1603 : : return NULL;
1604 : 2148 : si = new_strinfo (ptr, idx, build_int_cst (size_type_node, 0), true);
1605 : 2148 : set_strinfo (idx, si);
1606 : 2148 : si->endptr = ptr;
1607 : 2148 : if (chainsi != NULL)
1608 : : {
1609 : 2148 : chainsi = unshare_strinfo (chainsi);
1610 : 2148 : if (chainsi->first == 0)
1611 : 2056 : chainsi->first = chainsi->idx;
1612 : 2148 : chainsi->next = idx;
1613 : 2148 : if (chainsi->endptr == NULL_TREE)
1614 : 1454 : chainsi->endptr = ptr;
1615 : 2148 : si->prev = chainsi->idx;
1616 : 2148 : si->first = chainsi->first;
1617 : 2148 : si->writable = chainsi->writable;
1618 : : }
1619 : : return si;
1620 : : }
1621 : :
1622 : : /* For strinfo ORIGSI whose length has been just updated, adjust other
1623 : : related strinfos so that they match the new ORIGSI. This involves:
1624 : :
1625 : : - adding ADJ to the nonzero_chars fields
1626 : : - copying full_string_p from the new ORIGSI. */
1627 : :
1628 : : static void
1629 : 17905 : adjust_related_strinfos (location_t loc, strinfo *origsi, tree adj)
1630 : : {
1631 : 17905 : strinfo *si = verify_related_strinfos (origsi);
1632 : :
1633 : 17905 : if (si == NULL)
1634 : : return;
1635 : :
1636 : 5804 : while (1)
1637 : : {
1638 : 5804 : strinfo *nsi;
1639 : :
1640 : 5804 : if (si != origsi)
1641 : : {
1642 : 3996 : tree tem;
1643 : :
1644 : 3996 : si = unshare_strinfo (si);
1645 : : /* We shouldn't see delayed lengths here; the caller must
1646 : : have calculated the old length in order to calculate
1647 : : the adjustment. */
1648 : 3996 : gcc_assert (si->nonzero_chars);
1649 : 3996 : tem = fold_convert_loc (loc, TREE_TYPE (si->nonzero_chars), adj);
1650 : 3996 : si->nonzero_chars = fold_build2_loc (loc, PLUS_EXPR,
1651 : 3996 : TREE_TYPE (si->nonzero_chars),
1652 : : si->nonzero_chars, tem);
1653 : 3996 : si->full_string_p = origsi->full_string_p;
1654 : :
1655 : 3996 : si->endptr = NULL_TREE;
1656 : 3996 : si->dont_invalidate = true;
1657 : : }
1658 : 5804 : nsi = get_next_strinfo (si);
1659 : 5804 : if (nsi == NULL)
1660 : : return;
1661 : : si = nsi;
1662 : : }
1663 : : }
1664 : :
1665 : : /* Find if there are other SSA_NAME pointers equal to PTR
1666 : : for which we don't track their string lengths yet. If so, use
1667 : : IDX for them. */
1668 : :
1669 : : static void
1670 : 34462 : find_equal_ptrs (tree ptr, int idx)
1671 : : {
1672 : 34462 : if (TREE_CODE (ptr) != SSA_NAME)
1673 : : return;
1674 : 17474 : while (1)
1675 : : {
1676 : 17474 : gimple *stmt = SSA_NAME_DEF_STMT (ptr);
1677 : 17474 : if (!is_gimple_assign (stmt))
1678 : : return;
1679 : 8833 : ptr = gimple_assign_rhs1 (stmt);
1680 : 8833 : switch (gimple_assign_rhs_code (stmt))
1681 : : {
1682 : : case SSA_NAME:
1683 : : break;
1684 : 670 : CASE_CONVERT:
1685 : 670 : if (!POINTER_TYPE_P (TREE_TYPE (ptr)))
1686 : : return;
1687 : 0 : if (TREE_CODE (ptr) == SSA_NAME)
1688 : : break;
1689 : 0 : if (TREE_CODE (ptr) != ADDR_EXPR)
1690 : : return;
1691 : : /* FALLTHRU */
1692 : 1066 : case ADDR_EXPR:
1693 : 1066 : {
1694 : 1066 : int *pidx = addr_stridxptr (TREE_OPERAND (ptr, 0));
1695 : 1066 : if (pidx != NULL && *pidx == 0)
1696 : 0 : *pidx = idx;
1697 : : return;
1698 : : }
1699 : : default:
1700 : : return;
1701 : : }
1702 : :
1703 : : /* We might find an endptr created in this pass. Grow the
1704 : : vector in that case. */
1705 : 0 : if (ssa_ver_to_stridx.length () <= SSA_NAME_VERSION (ptr))
1706 : 0 : ssa_ver_to_stridx.safe_grow_cleared (num_ssa_names, true);
1707 : :
1708 : 0 : if (ssa_ver_to_stridx[SSA_NAME_VERSION (ptr)] != 0)
1709 : : return;
1710 : 0 : ssa_ver_to_stridx[SSA_NAME_VERSION (ptr)] = idx;
1711 : 0 : }
1712 : : }
1713 : :
1714 : : /* Return true if STMT is a call to a builtin function with the right
1715 : : arguments and attributes that should be considered for optimization
1716 : : by this pass. */
1717 : :
1718 : : static bool
1719 : 4299292 : valid_builtin_call (gimple *stmt)
1720 : : {
1721 : 4299292 : if (!gimple_call_builtin_p (stmt, BUILT_IN_NORMAL))
1722 : : return false;
1723 : :
1724 : 968382 : tree callee = gimple_call_fndecl (stmt);
1725 : 968382 : switch (DECL_FUNCTION_CODE (callee))
1726 : : {
1727 : 268058 : case BUILT_IN_MEMCMP:
1728 : 268058 : case BUILT_IN_MEMCMP_EQ:
1729 : 268058 : case BUILT_IN_STRCMP:
1730 : 268058 : case BUILT_IN_STRNCMP:
1731 : 268058 : case BUILT_IN_STRCHR:
1732 : 268058 : case BUILT_IN_STRLEN:
1733 : 268058 : case BUILT_IN_STRNLEN:
1734 : : /* The above functions should be pure. Punt if they aren't. */
1735 : 4135084 : if (gimple_vdef (stmt) || gimple_vuse (stmt) == NULL_TREE)
1736 : : return false;
1737 : : break;
1738 : :
1739 : 123371 : case BUILT_IN_ALLOCA:
1740 : 123371 : case BUILT_IN_ALLOCA_WITH_ALIGN:
1741 : 123371 : case BUILT_IN_CALLOC:
1742 : 123371 : case BUILT_IN_MALLOC:
1743 : 123371 : case BUILT_IN_MEMCPY:
1744 : 123371 : case BUILT_IN_MEMCPY_CHK:
1745 : 123371 : case BUILT_IN_MEMPCPY:
1746 : 123371 : case BUILT_IN_MEMPCPY_CHK:
1747 : 123371 : case BUILT_IN_MEMSET:
1748 : 123371 : case BUILT_IN_STPCPY:
1749 : 123371 : case BUILT_IN_STPCPY_CHK:
1750 : 123371 : case BUILT_IN_STPNCPY:
1751 : 123371 : case BUILT_IN_STPNCPY_CHK:
1752 : 123371 : case BUILT_IN_STRCAT:
1753 : 123371 : case BUILT_IN_STRCAT_CHK:
1754 : 123371 : case BUILT_IN_STRCPY:
1755 : 123371 : case BUILT_IN_STRCPY_CHK:
1756 : 123371 : case BUILT_IN_STRNCAT:
1757 : 123371 : case BUILT_IN_STRNCAT_CHK:
1758 : 123371 : case BUILT_IN_STRNCPY:
1759 : 123371 : case BUILT_IN_STRNCPY_CHK:
1760 : : /* The above functions should be neither const nor pure. Punt if they
1761 : : aren't. */
1762 : 3577652 : if (gimple_vdef (stmt) == NULL_TREE || gimple_vuse (stmt) == NULL_TREE)
1763 : : return false;
1764 : : break;
1765 : :
1766 : : default:
1767 : : break;
1768 : : }
1769 : :
1770 : : return true;
1771 : : }
1772 : :
1773 : : /* If the last .MEM setter statement before STMT is
1774 : : memcpy (x, y, strlen (y) + 1), the only .MEM use of it is STMT
1775 : : and STMT is known to overwrite x[strlen (x)], adjust the last memcpy to
1776 : : just memcpy (x, y, strlen (y)). SI must be the zero length
1777 : : strinfo. */
1778 : :
1779 : : void
1780 : 5685 : strlen_pass::adjust_last_stmt (strinfo *si, gimple *stmt, bool is_strcat)
1781 : : {
1782 : 5685 : tree vuse, callee, len;
1783 : 5685 : struct laststmt_struct last = laststmt;
1784 : 5685 : strinfo *lastsi, *firstsi;
1785 : 5685 : unsigned len_arg_no = 2;
1786 : :
1787 : 5685 : laststmt.stmt = NULL;
1788 : 5685 : laststmt.len = NULL_TREE;
1789 : 5685 : laststmt.stridx = 0;
1790 : :
1791 : 5685 : if (last.stmt == NULL)
1792 : 5480 : return;
1793 : :
1794 : 7523 : vuse = gimple_vuse (stmt);
1795 : 2043 : if (vuse == NULL_TREE
1796 : 2043 : || SSA_NAME_DEF_STMT (vuse) != last.stmt
1797 : 3006 : || !has_single_use (vuse))
1798 : : return;
1799 : :
1800 : 423 : gcc_assert (last.stridx > 0);
1801 : 423 : lastsi = get_strinfo (last.stridx);
1802 : 423 : if (lastsi == NULL)
1803 : : return;
1804 : :
1805 : 423 : if (lastsi != si)
1806 : : {
1807 : 329 : if (lastsi->first == 0 || lastsi->first != si->first)
1808 : : return;
1809 : :
1810 : 307 : firstsi = verify_related_strinfos (si);
1811 : 307 : if (firstsi == NULL)
1812 : : return;
1813 : 483 : while (firstsi != lastsi)
1814 : : {
1815 : 176 : firstsi = get_next_strinfo (firstsi);
1816 : 176 : if (firstsi == NULL)
1817 : : return;
1818 : : }
1819 : : }
1820 : :
1821 : 401 : if (!is_strcat && !zero_length_string_p (si))
1822 : : return;
1823 : :
1824 : 320 : if (is_gimple_assign (last.stmt))
1825 : : {
1826 : 29 : gimple_stmt_iterator gsi;
1827 : :
1828 : 29 : if (!integer_zerop (gimple_assign_rhs1 (last.stmt)))
1829 : : return;
1830 : 16 : if (stmt_could_throw_p (cfun, last.stmt))
1831 : : return;
1832 : 16 : gsi = gsi_for_stmt (last.stmt);
1833 : 16 : unlink_stmt_vdef (last.stmt);
1834 : 16 : release_defs (last.stmt);
1835 : 16 : gsi_remove (&gsi, true);
1836 : 16 : return;
1837 : : }
1838 : :
1839 : 291 : if (!valid_builtin_call (last.stmt))
1840 : : return;
1841 : :
1842 : 291 : callee = gimple_call_fndecl (last.stmt);
1843 : 291 : switch (DECL_FUNCTION_CODE (callee))
1844 : : {
1845 : 291 : case BUILT_IN_MEMCPY:
1846 : 291 : case BUILT_IN_MEMCPY_CHK:
1847 : 291 : break;
1848 : : default:
1849 : : return;
1850 : : }
1851 : :
1852 : 291 : len = gimple_call_arg (last.stmt, len_arg_no);
1853 : 291 : if (tree_fits_uhwi_p (len))
1854 : : {
1855 : 214 : if (!tree_fits_uhwi_p (last.len)
1856 : 214 : || integer_zerop (len)
1857 : 428 : || tree_to_uhwi (len) != tree_to_uhwi (last.len) + 1)
1858 : 86 : return;
1859 : : /* Don't adjust the length if it is divisible by 4, it is more efficient
1860 : : to store the extra '\0' in that case. */
1861 : 214 : if ((tree_to_uhwi (len) & 3) == 0)
1862 : : return;
1863 : :
1864 : : /* Don't fold away an out of bounds access, as this defeats proper
1865 : : warnings. */
1866 : 139 : tree dst = gimple_call_arg (last.stmt, 0);
1867 : :
1868 : 139 : access_ref aref;
1869 : 139 : tree size = compute_objsize (dst, stmt, 1, &aref, &ptr_qry);
1870 : 139 : if (size && tree_int_cst_lt (size, len))
1871 : : return;
1872 : : }
1873 : 77 : else if (TREE_CODE (len) == SSA_NAME)
1874 : : {
1875 : 77 : gimple *def_stmt = SSA_NAME_DEF_STMT (len);
1876 : 77 : if (!is_gimple_assign (def_stmt)
1877 : 77 : || gimple_assign_rhs_code (def_stmt) != PLUS_EXPR
1878 : 77 : || gimple_assign_rhs1 (def_stmt) != last.len
1879 : 154 : || !integer_onep (gimple_assign_rhs2 (def_stmt)))
1880 : 0 : return;
1881 : : }
1882 : : else
1883 : : return;
1884 : :
1885 : 205 : gimple_call_set_arg (last.stmt, len_arg_no, last.len);
1886 : 205 : update_stmt (last.stmt);
1887 : : }
1888 : :
1889 : : /* For an LHS that is an SSA_NAME that is the result of a strlen()
1890 : : call, or when BOUND is non-null, of a strnlen() call, set LHS
1891 : : range info to [0, min (MAX, BOUND)] when the range includes more
1892 : : than one value and return LHS. Otherwise, when the range
1893 : : [MIN, MAX] is such that MIN == MAX, return the tree representation
1894 : : of (MIN). The latter allows callers to fold suitable strnlen() calls
1895 : : to constants. */
1896 : :
1897 : : tree
1898 : 158631 : set_strlen_range (tree lhs, wide_int min, wide_int max,
1899 : : tree bound /* = NULL_TREE */)
1900 : : {
1901 : 158631 : if (TREE_CODE (lhs) != SSA_NAME
1902 : 158631 : || !INTEGRAL_TYPE_P (TREE_TYPE (lhs)))
1903 : : return NULL_TREE;
1904 : :
1905 : 154991 : if (bound)
1906 : : {
1907 : : /* For strnlen, adjust MIN and MAX as necessary. If the bound
1908 : : is less than the size of the array set MAX to it. It it's
1909 : : greater than MAX and MAX is non-zero bump MAX down to account
1910 : : for the necessary terminating nul. Otherwise leave it alone. */
1911 : 511 : if (TREE_CODE (bound) == INTEGER_CST)
1912 : : {
1913 : 330 : wide_int wibnd = wi::to_wide (bound);
1914 : 330 : int cmp = wi::cmpu (wibnd, max);
1915 : 330 : if (cmp < 0)
1916 : 260 : max = wibnd;
1917 : 124 : else if (cmp && wi::ne_p (max, min))
1918 : 54 : --max;
1919 : 330 : }
1920 : 181 : else if (TREE_CODE (bound) == SSA_NAME)
1921 : : {
1922 : 181 : int_range_max r;
1923 : 362 : get_range_query (cfun)->range_of_expr (r, bound);
1924 : 181 : if (!r.undefined_p ())
1925 : : {
1926 : : /* For a bound in a known range, adjust the range determined
1927 : : above as necessary. For a bound in some anti-range or
1928 : : in an unknown range, use the range determined by callers. */
1929 : 181 : if (wi::ltu_p (r.lower_bound (), min))
1930 : 0 : min = r.lower_bound ();
1931 : 181 : if (wi::ltu_p (r.upper_bound (), max))
1932 : 30 : max = r.upper_bound ();
1933 : : }
1934 : 181 : }
1935 : : }
1936 : :
1937 : 154991 : if (min == max)
1938 : 14 : return wide_int_to_tree (size_type_node, min);
1939 : :
1940 : 154977 : int_range_max vr (TREE_TYPE (lhs), min, max);
1941 : 154977 : set_range_info (lhs, vr);
1942 : 154977 : return lhs;
1943 : 154977 : }
1944 : :
1945 : : /* For an LHS that is an SSA_NAME and for strlen() or strnlen() argument
1946 : : SRC, set LHS range info to [0, min (N, BOUND)] if SRC refers to
1947 : : a character array A[N] with unknown length bounded by N, and for
1948 : : strnlen(), by min (N, BOUND). */
1949 : :
1950 : : static tree
1951 : 11356 : maybe_set_strlen_range (tree lhs, tree src, tree bound)
1952 : : {
1953 : 11356 : if (TREE_CODE (lhs) != SSA_NAME
1954 : 11356 : || !INTEGRAL_TYPE_P (TREE_TYPE (lhs)))
1955 : : return NULL_TREE;
1956 : :
1957 : 11356 : if (TREE_CODE (src) == SSA_NAME)
1958 : : {
1959 : 8150 : gimple *def = SSA_NAME_DEF_STMT (src);
1960 : 8150 : if (is_gimple_assign (def)
1961 : 8150 : && gimple_assign_rhs_code (def) == ADDR_EXPR)
1962 : 130 : src = gimple_assign_rhs1 (def);
1963 : : }
1964 : :
1965 : : /* The longest string is PTRDIFF_MAX - 1 bytes including the final
1966 : : NUL so that the difference between a pointer to just past it and
1967 : : one to its beginning is positive. */
1968 : 11356 : wide_int max = wi::to_wide (TYPE_MAX_VALUE (ptrdiff_type_node)) - 2;
1969 : :
1970 : 11356 : if (TREE_CODE (src) == ADDR_EXPR)
1971 : : {
1972 : : /* The last array member of a struct can be bigger than its size
1973 : : suggests if it's treated as a poor-man's flexible array member. */
1974 : 3336 : src = TREE_OPERAND (src, 0);
1975 : 3336 : if (TREE_CODE (src) != MEM_REF
1976 : 3336 : && !array_ref_flexible_size_p (src))
1977 : : {
1978 : 2300 : tree type = TREE_TYPE (src);
1979 : 2300 : tree size = TYPE_SIZE_UNIT (type);
1980 : 2300 : if (size
1981 : 2247 : && TREE_CODE (size) == INTEGER_CST
1982 : 4547 : && !integer_zerop (size))
1983 : : {
1984 : : /* Even though such uses of strlen would be undefined,
1985 : : avoid relying on arrays of arrays in case some genius
1986 : : decides to call strlen on an unterminated array element
1987 : : that's followed by a terminated one. Likewise, avoid
1988 : : assuming that a struct array member is necessarily
1989 : : nul-terminated (the nul may be in the member that
1990 : : follows). In those cases, assume that the length
1991 : : of the string stored in such an array is bounded
1992 : : by the size of the enclosing object if one can be
1993 : : determined. */
1994 : 2241 : tree base = get_base_address (src);
1995 : 2241 : if (VAR_P (base))
1996 : : {
1997 : 2178 : if (tree size = DECL_SIZE_UNIT (base))
1998 : 2178 : if (size
1999 : 2178 : && TREE_CODE (size) == INTEGER_CST
2000 : 2178 : && TREE_CODE (TREE_TYPE (base)) != POINTER_TYPE)
2001 : 2178 : max = wi::to_wide (size);
2002 : : }
2003 : : }
2004 : :
2005 : : /* For strlen() the upper bound above is equal to
2006 : : the longest string that can be stored in the array
2007 : : (i.e., it accounts for the terminating nul. For
2008 : : strnlen() bump up the maximum by one since the array
2009 : : need not be nul-terminated. */
2010 : 2300 : if (!bound && max != 0)
2011 : 2104 : --max;
2012 : : }
2013 : : }
2014 : :
2015 : 11356 : wide_int min = wi::zero (max.get_precision ());
2016 : 11356 : return set_strlen_range (lhs, min, max, bound);
2017 : 11356 : }
2018 : :
2019 : : /* Diagnose buffer overflow by a STMT writing LEN + PLUS_ONE bytes,
2020 : : either into a region allocated for the object SI when non-null,
2021 : : or into an object designated by the LHS of STMT otherwise.
2022 : : For a call STMT, when CALL_LHS is set use its left hand side
2023 : : as the destination, otherwise use argument zero.
2024 : : When nonnull uses RVALS to determine range information.
2025 : : RAWMEM may be set by memcpy and other raw memory functions
2026 : : to allow accesses across subobject boundaries. */
2027 : :
2028 : : void
2029 : 339500 : strlen_pass::maybe_warn_overflow (gimple *stmt, bool call_lhs, tree len,
2030 : : strinfo *si, bool plus_one, bool rawmem)
2031 : : {
2032 : 339500 : if (!len || warning_suppressed_p (stmt, OPT_Wstringop_overflow_))
2033 : 338644 : return;
2034 : :
2035 : : /* The DECL of the function performing the write if it is done
2036 : : by one. */
2037 : 338017 : tree writefn = NULL_TREE;
2038 : : /* The destination expression involved in the store or call STMT. */
2039 : 338017 : tree dest = NULL_TREE;
2040 : :
2041 : 338017 : if (is_gimple_assign (stmt))
2042 : 298465 : dest = gimple_assign_lhs (stmt);
2043 : 39552 : else if (is_gimple_call (stmt))
2044 : : {
2045 : 39552 : if (call_lhs)
2046 : 28710 : dest = gimple_call_lhs (stmt);
2047 : : else
2048 : : {
2049 : 10842 : gcc_assert (gimple_call_builtin_p (stmt, BUILT_IN_NORMAL));
2050 : 10842 : dest = gimple_call_arg (stmt, 0);
2051 : : }
2052 : :
2053 : 39552 : if (!dest)
2054 : : return;
2055 : 39552 : writefn = gimple_call_fndecl (stmt);
2056 : : }
2057 : : else
2058 : : return;
2059 : :
2060 : 338017 : if (warning_suppressed_p (dest, OPT_Wstringop_overflow_))
2061 : : return;
2062 : :
2063 : 337872 : const int ostype = rawmem ? 0 : 1;
2064 : :
2065 : : /* Use maximum precision to avoid overflow in the addition below.
2066 : : Make sure all operands have the same precision to keep wide_int
2067 : : from ICE'ing. */
2068 : :
2069 : 337872 : access_ref aref;
2070 : : /* The size of the destination region (which is smaller than
2071 : : the destination object for stores at a non-zero offset). */
2072 : 337872 : tree destsize = compute_objsize (dest, stmt, ostype, &aref, &ptr_qry);
2073 : :
2074 : 337872 : if (!destsize)
2075 : : {
2076 : 3 : aref.sizrng[0] = 0;
2077 : 3 : aref.sizrng[1] = wi::to_offset (max_object_size ());
2078 : : }
2079 : :
2080 : : /* Return early if the DESTSIZE size expression is the same as LEN
2081 : : and the offset into the destination is zero. This might happen
2082 : : in the case of a pair of malloc and memset calls to allocate
2083 : : an object and clear it as if by calloc. */
2084 : 337872 : if (destsize == len && !plus_one
2085 : 338151 : && aref.offrng[0] == 0 && aref.offrng[0] == aref.offrng[1])
2086 : 279 : return;
2087 : :
2088 : 1687965 : wide_int rng[2];
2089 : 337593 : if (!get_range (len, stmt, rng, ptr_qry.rvals))
2090 : 1010211 : return;
2091 : :
2092 : 336508 : widest_int lenrng[2] =
2093 : 1009524 : { widest_int::from (rng[0], SIGNED), widest_int::from (rng[1], SIGNED) };
2094 : :
2095 : 336508 : if (plus_one)
2096 : : {
2097 : 1448 : lenrng[0] += 1;
2098 : 1448 : lenrng[1] += 1;
2099 : : }
2100 : :
2101 : : /* The size of the remaining space in the destination computed
2102 : : as the size of the latter minus the offset into it. */
2103 : 1682540 : widest_int spcrng[2];
2104 : : {
2105 : 336508 : offset_int remrng[2];
2106 : 336508 : remrng[1] = aref.size_remaining (remrng);
2107 : 1004152 : spcrng[0] = remrng[0] == -1 ? 0 : widest_int::from (remrng[0], UNSIGNED);
2108 : 336508 : spcrng[1] = widest_int::from (remrng[1], UNSIGNED);
2109 : : }
2110 : :
2111 : 336508 : if (wi::leu_p (lenrng[0], spcrng[0])
2112 : 564664 : && wi::leu_p (lenrng[1], spcrng[1]))
2113 : 1006956 : return;
2114 : :
2115 : 109479 : location_t loc = gimple_or_expr_nonartificial_location (stmt, dest);
2116 : 109479 : bool warned = false;
2117 : 109479 : if (wi::leu_p (lenrng[0], spcrng[1]))
2118 : : {
2119 : 108345 : if (len != destsize
2120 : 108345 : && (!si || rawmem || !is_strlen_related_p (si->ptr, len)))
2121 : 108339 : return;
2122 : :
2123 : 12 : warned = (writefn
2124 : 6 : ? warning_at (loc, OPT_Wstringop_overflow_,
2125 : : "%qD writing one too many bytes into a region "
2126 : : "of a size that depends on %<strlen%>",
2127 : : writefn)
2128 : 6 : : warning_at (loc, OPT_Wstringop_overflow_,
2129 : : "writing one too many bytes into a region "
2130 : : "of a size that depends on %<strlen%>"));
2131 : : }
2132 : 1134 : else if (lenrng[0] == lenrng[1])
2133 : : {
2134 : 1132 : if (spcrng[0] == spcrng[1])
2135 : 2076 : warned = (writefn
2136 : 1275 : ? warning_n (loc, OPT_Wstringop_overflow_,
2137 : : lenrng[0].to_uhwi (),
2138 : : "%qD writing %wu byte into a region "
2139 : : "of size %wu",
2140 : : "%qD writing %wu bytes into a region "
2141 : : "of size %wu",
2142 : : writefn, lenrng[0].to_uhwi (),
2143 : : spcrng[0].to_uhwi ())
2144 : 1038 : : warning_n (loc, OPT_Wstringop_overflow_,
2145 : : lenrng[0].to_uhwi (),
2146 : : "writing %wu byte into a region "
2147 : : "of size %wu",
2148 : : "writing %wu bytes into a region "
2149 : : "of size %wu",
2150 : : lenrng[0].to_uhwi (),
2151 : : spcrng[0].to_uhwi ()));
2152 : : else
2153 : 188 : warned = (writefn
2154 : 185 : ? warning_n (loc, OPT_Wstringop_overflow_,
2155 : : lenrng[0].to_uhwi (),
2156 : : "%qD writing %wu byte into a region "
2157 : : "of size between %wu and %wu",
2158 : : "%qD writing %wu bytes into a region "
2159 : : "of size between %wu and %wu",
2160 : : writefn, lenrng[0].to_uhwi (),
2161 : : spcrng[0].to_uhwi (), spcrng[1].to_uhwi ())
2162 : 94 : : warning_n (loc, OPT_Wstringop_overflow_,
2163 : : lenrng[0].to_uhwi (),
2164 : : "writing %wu byte into a region "
2165 : : "of size between %wu and %wu",
2166 : : "writing %wu bytes into a region "
2167 : : "of size between %wu and %wu",
2168 : : lenrng[0].to_uhwi (),
2169 : : spcrng[0].to_uhwi (), spcrng[1].to_uhwi ()));
2170 : : }
2171 : 2 : else if (spcrng[0] == spcrng[1])
2172 : 4 : warned = (writefn
2173 : 4 : ? warning_at (loc, OPT_Wstringop_overflow_,
2174 : : "%qD writing between %wu and %wu bytes "
2175 : : "into a region of size %wu",
2176 : : writefn, lenrng[0].to_uhwi (),
2177 : : lenrng[1].to_uhwi (),
2178 : : spcrng[0].to_uhwi ())
2179 : 2 : : warning_at (loc, OPT_Wstringop_overflow_,
2180 : : "writing between %wu and %wu bytes "
2181 : : "into a region of size %wu",
2182 : : lenrng[0].to_uhwi (),
2183 : : lenrng[1].to_uhwi (),
2184 : : spcrng[0].to_uhwi ()));
2185 : : else
2186 : 0 : warned = (writefn
2187 : 0 : ? warning_at (loc, OPT_Wstringop_overflow_,
2188 : : "%qD writing between %wu and %wu bytes "
2189 : : "into a region of size between %wu and %wu",
2190 : : writefn, lenrng[0].to_uhwi (),
2191 : : lenrng[1].to_uhwi (),
2192 : : spcrng[0].to_uhwi (), spcrng[1].to_uhwi ())
2193 : 0 : : warning_at (loc, OPT_Wstringop_overflow_,
2194 : : "writing between %wu and %wu bytes "
2195 : : "into a region of size between %wu and %wu",
2196 : : lenrng[0].to_uhwi (),
2197 : : lenrng[1].to_uhwi (),
2198 : : spcrng[0].to_uhwi (), spcrng[1].to_uhwi ()));
2199 : :
2200 : 1140 : if (!warned)
2201 : : return;
2202 : :
2203 : 856 : suppress_warning (stmt, OPT_Wstringop_overflow_);
2204 : :
2205 : 856 : aref.inform_access (access_write_only);
2206 : 3031827 : }
2207 : :
2208 : : /* Convenience wrapper for the above. */
2209 : :
2210 : : void
2211 : 327303 : strlen_pass::maybe_warn_overflow (gimple *stmt, bool call_lhs,
2212 : : unsigned HOST_WIDE_INT len,
2213 : : strinfo *si, bool plus_one, bool rawmem)
2214 : : {
2215 : 327303 : tree tlen = build_int_cst (size_type_node, len);
2216 : 327303 : maybe_warn_overflow (stmt, call_lhs, tlen, si, plus_one, rawmem);
2217 : 327303 : }
2218 : :
2219 : : /* Handle a strlen call. If strlen of the argument is known, replace
2220 : : the strlen call with the known value, otherwise remember that strlen
2221 : : of the argument is stored in the lhs SSA_NAME. */
2222 : :
2223 : : void
2224 : 13335 : strlen_pass::handle_builtin_strlen ()
2225 : : {
2226 : 13335 : gimple *stmt = gsi_stmt (m_gsi);
2227 : 13335 : tree lhs = gimple_call_lhs (stmt);
2228 : :
2229 : 13335 : if (lhs == NULL_TREE)
2230 : 1955 : return;
2231 : :
2232 : 13335 : location_t loc = gimple_location (stmt);
2233 : 13335 : tree callee = gimple_call_fndecl (stmt);
2234 : 13335 : tree src = gimple_call_arg (stmt, 0);
2235 : 13335 : tree bound = (DECL_FUNCTION_CODE (callee) == BUILT_IN_STRNLEN
2236 : 13335 : ? gimple_call_arg (stmt, 1) : NULL_TREE);
2237 : 13335 : int idx = get_stridx (src, stmt);
2238 : 13335 : if (idx || (bound && integer_zerop (bound)))
2239 : : {
2240 : 3644 : strinfo *si = NULL;
2241 : 3644 : tree rhs;
2242 : :
2243 : 3644 : if (idx < 0)
2244 : 289 : rhs = build_int_cst (TREE_TYPE (lhs), ~idx);
2245 : 3355 : else if (idx == 0)
2246 : : rhs = bound;
2247 : : else
2248 : : {
2249 : 3325 : rhs = NULL_TREE;
2250 : 3325 : si = get_strinfo (idx);
2251 : 3325 : if (si != NULL)
2252 : : {
2253 : 1636 : rhs = get_string_length (si);
2254 : : /* For strnlen, if bound is constant, even if si is not known
2255 : : to be zero terminated, if we know at least bound bytes are
2256 : : not zero, the return value will be bound. */
2257 : 1636 : if (rhs == NULL_TREE
2258 : 1636 : && bound != NULL_TREE
2259 : 18 : && TREE_CODE (bound) == INTEGER_CST
2260 : 16 : && si->nonzero_chars != NULL_TREE
2261 : 16 : && TREE_CODE (si->nonzero_chars) == INTEGER_CST
2262 : 1652 : && tree_int_cst_le (bound, si->nonzero_chars))
2263 : : rhs = bound;
2264 : : }
2265 : : }
2266 : 1955 : if (rhs != NULL_TREE)
2267 : : {
2268 : 1792 : if (dump_file && (dump_flags & TDF_DETAILS) != 0)
2269 : : {
2270 : 0 : fprintf (dump_file, "Optimizing: ");
2271 : 0 : print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
2272 : : }
2273 : 1792 : rhs = unshare_expr (rhs);
2274 : 1792 : if (!useless_type_conversion_p (TREE_TYPE (lhs), TREE_TYPE (rhs)))
2275 : 0 : rhs = fold_convert_loc (loc, TREE_TYPE (lhs), rhs);
2276 : :
2277 : 1792 : if (bound)
2278 : 333 : rhs = fold_build2_loc (loc, MIN_EXPR, TREE_TYPE (rhs), rhs, bound);
2279 : :
2280 : 1792 : gimplify_and_update_call_from_tree (&m_gsi, rhs);
2281 : 1792 : stmt = gsi_stmt (m_gsi);
2282 : 1792 : update_stmt (stmt);
2283 : 1792 : if (dump_file && (dump_flags & TDF_DETAILS) != 0)
2284 : : {
2285 : 0 : fprintf (dump_file, "into: ");
2286 : 0 : print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
2287 : : }
2288 : :
2289 : 1792 : if (si != NULL
2290 : : /* Don't update anything for strnlen. */
2291 : 1792 : && bound == NULL_TREE
2292 : 1443 : && TREE_CODE (si->nonzero_chars) != SSA_NAME
2293 : 1025 : && TREE_CODE (si->nonzero_chars) != INTEGER_CST
2294 : 2032 : && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (lhs))
2295 : : {
2296 : 240 : si = unshare_strinfo (si);
2297 : 240 : si->nonzero_chars = lhs;
2298 : 240 : gcc_assert (si->full_string_p);
2299 : : }
2300 : :
2301 : 1792 : if (strlen_to_stridx
2302 : 1792 : && (bound == NULL_TREE
2303 : : /* For strnlen record this only if the call is proven
2304 : : to return the same value as strlen would. */
2305 : 333 : || (TREE_CODE (bound) == INTEGER_CST
2306 : 237 : && TREE_CODE (rhs) == INTEGER_CST
2307 : 237 : && tree_int_cst_lt (rhs, bound))))
2308 : 1608 : strlen_to_stridx->put (lhs, stridx_strlenloc (idx, loc));
2309 : :
2310 : 1792 : return;
2311 : : }
2312 : : }
2313 : 11543 : if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (lhs))
2314 : : return;
2315 : :
2316 : 11543 : if (idx == 0)
2317 : 9691 : idx = new_stridx (src);
2318 : : else
2319 : : {
2320 : 1852 : strinfo *si = get_strinfo (idx);
2321 : 1852 : if (si != NULL)
2322 : : {
2323 : 163 : if (!si->full_string_p && !si->stmt)
2324 : : {
2325 : : /* Until now we only had a lower bound on the string length.
2326 : : Install LHS as the actual length. */
2327 : 161 : si = unshare_strinfo (si);
2328 : 161 : tree old = si->nonzero_chars;
2329 : 161 : si->nonzero_chars = lhs;
2330 : 161 : si->full_string_p = true;
2331 : 161 : if (old && TREE_CODE (old) == INTEGER_CST)
2332 : : {
2333 : 158 : old = fold_convert_loc (loc, TREE_TYPE (lhs), old);
2334 : 158 : tree adj = fold_build2_loc (loc, MINUS_EXPR,
2335 : 158 : TREE_TYPE (lhs), lhs, old);
2336 : 158 : adjust_related_strinfos (loc, si, adj);
2337 : : /* Use the constant minimum length as the lower bound
2338 : : of the non-constant length. */
2339 : 158 : wide_int min = wi::to_wide (old);
2340 : 158 : wide_int max
2341 : 158 : = wi::to_wide (TYPE_MAX_VALUE (ptrdiff_type_node)) - 2;
2342 : 158 : if (wi::gtu_p (min, max))
2343 : 1 : max = wi::to_wide (TYPE_MAX_VALUE (TREE_TYPE (lhs)));
2344 : 158 : set_strlen_range (lhs, min, max);
2345 : 158 : }
2346 : : else
2347 : : {
2348 : 3 : si->first = 0;
2349 : 3 : si->prev = 0;
2350 : 3 : si->next = 0;
2351 : : }
2352 : : }
2353 : 163 : return;
2354 : : }
2355 : : }
2356 : 11380 : if (idx)
2357 : : {
2358 : 11356 : if (!bound)
2359 : : {
2360 : : /* Only store the new length information for calls to strlen(),
2361 : : not for those to strnlen(). */
2362 : 10845 : strinfo *si = new_strinfo (src, idx, lhs, true);
2363 : 10845 : set_strinfo (idx, si);
2364 : 10845 : find_equal_ptrs (src, idx);
2365 : : }
2366 : :
2367 : : /* For SRC that is an array of N elements, set LHS's range
2368 : : to [0, min (N, BOUND)]. A constant return value means
2369 : : the range would have consisted of a single value. In
2370 : : that case, fold the result into the returned constant. */
2371 : 11356 : if (tree ret = maybe_set_strlen_range (lhs, src, bound))
2372 : 11356 : if (TREE_CODE (ret) == INTEGER_CST)
2373 : : {
2374 : 13 : if (dump_file && (dump_flags & TDF_DETAILS) != 0)
2375 : : {
2376 : 0 : fprintf (dump_file, "Optimizing: ");
2377 : 0 : print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
2378 : : }
2379 : 13 : if (!useless_type_conversion_p (TREE_TYPE (lhs), TREE_TYPE (ret)))
2380 : 0 : ret = fold_convert_loc (loc, TREE_TYPE (lhs), ret);
2381 : 13 : gimplify_and_update_call_from_tree (&m_gsi, ret);
2382 : 13 : stmt = gsi_stmt (m_gsi);
2383 : 13 : update_stmt (stmt);
2384 : 13 : if (dump_file && (dump_flags & TDF_DETAILS) != 0)
2385 : : {
2386 : 0 : fprintf (dump_file, "into: ");
2387 : 0 : print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
2388 : : }
2389 : : }
2390 : :
2391 : 11356 : if (strlen_to_stridx && !bound)
2392 : 10844 : strlen_to_stridx->put (lhs, stridx_strlenloc (idx, loc));
2393 : : }
2394 : : }
2395 : :
2396 : : /* Handle a strchr call. If strlen of the first argument is known, replace
2397 : : the strchr (x, 0) call with the endptr or x + strlen, otherwise remember
2398 : : that lhs of the call is endptr and strlen of the argument is endptr - x. */
2399 : :
2400 : : void
2401 : 584 : strlen_pass::handle_builtin_strchr ()
2402 : : {
2403 : 584 : gimple *stmt = gsi_stmt (m_gsi);
2404 : 584 : tree lhs = gimple_call_lhs (stmt);
2405 : :
2406 : 584 : if (lhs == NULL_TREE)
2407 : : return;
2408 : :
2409 : 584 : if (!integer_zerop (gimple_call_arg (stmt, 1)))
2410 : : return;
2411 : :
2412 : 0 : tree src = gimple_call_arg (stmt, 0);
2413 : :
2414 : : /* Avoid folding if the first argument is not a nul-terminated array.
2415 : : Defer warning until later. */
2416 : 0 : if (!check_nul_terminated_array (NULL_TREE, src))
2417 : : return;
2418 : :
2419 : 0 : int idx = get_stridx (src, stmt);
2420 : 0 : if (idx)
2421 : : {
2422 : 0 : strinfo *si = NULL;
2423 : 0 : tree rhs;
2424 : :
2425 : 0 : if (idx < 0)
2426 : 0 : rhs = build_int_cst (size_type_node, ~idx);
2427 : : else
2428 : : {
2429 : 0 : rhs = NULL_TREE;
2430 : 0 : si = get_strinfo (idx);
2431 : 0 : if (si != NULL)
2432 : 0 : rhs = get_string_length (si);
2433 : : }
2434 : 0 : if (rhs != NULL_TREE)
2435 : : {
2436 : 0 : location_t loc = gimple_location (stmt);
2437 : :
2438 : 0 : if (dump_file && (dump_flags & TDF_DETAILS) != 0)
2439 : : {
2440 : 0 : fprintf (dump_file, "Optimizing: ");
2441 : 0 : print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
2442 : : }
2443 : 0 : if (si != NULL && si->endptr != NULL_TREE)
2444 : : {
2445 : 0 : rhs = unshare_expr (si->endptr);
2446 : 0 : if (!useless_type_conversion_p (TREE_TYPE (lhs),
2447 : 0 : TREE_TYPE (rhs)))
2448 : 0 : rhs = fold_convert_loc (loc, TREE_TYPE (lhs), rhs);
2449 : : }
2450 : : else
2451 : : {
2452 : 0 : rhs = fold_convert_loc (loc, sizetype, unshare_expr (rhs));
2453 : 0 : rhs = fold_build2_loc (loc, POINTER_PLUS_EXPR,
2454 : 0 : TREE_TYPE (src), src, rhs);
2455 : 0 : if (!useless_type_conversion_p (TREE_TYPE (lhs),
2456 : 0 : TREE_TYPE (rhs)))
2457 : 0 : rhs = fold_convert_loc (loc, TREE_TYPE (lhs), rhs);
2458 : : }
2459 : 0 : gimplify_and_update_call_from_tree (&m_gsi, rhs);
2460 : 0 : stmt = gsi_stmt (m_gsi);
2461 : 0 : update_stmt (stmt);
2462 : 0 : if (dump_file && (dump_flags & TDF_DETAILS) != 0)
2463 : : {
2464 : 0 : fprintf (dump_file, "into: ");
2465 : 0 : print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
2466 : : }
2467 : 0 : if (si != NULL
2468 : 0 : && si->endptr == NULL_TREE
2469 : 0 : && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (lhs))
2470 : : {
2471 : 0 : si = unshare_strinfo (si);
2472 : 0 : si->endptr = lhs;
2473 : : }
2474 : 0 : zero_length_string (lhs, si);
2475 : 0 : return;
2476 : : }
2477 : : }
2478 : 0 : if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (lhs))
2479 : : return;
2480 : 0 : if (TREE_CODE (src) != SSA_NAME || !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (src))
2481 : : {
2482 : 0 : if (idx == 0)
2483 : 0 : idx = new_stridx (src);
2484 : 0 : else if (get_strinfo (idx) != NULL)
2485 : : {
2486 : 0 : zero_length_string (lhs, NULL);
2487 : 0 : return;
2488 : : }
2489 : 0 : if (idx)
2490 : : {
2491 : 0 : location_t loc = gimple_location (stmt);
2492 : 0 : tree lhsu = fold_convert_loc (loc, size_type_node, lhs);
2493 : 0 : tree srcu = fold_convert_loc (loc, size_type_node, src);
2494 : 0 : tree length = fold_build2_loc (loc, MINUS_EXPR,
2495 : : size_type_node, lhsu, srcu);
2496 : 0 : strinfo *si = new_strinfo (src, idx, length, true);
2497 : 0 : si->endptr = lhs;
2498 : 0 : set_strinfo (idx, si);
2499 : 0 : find_equal_ptrs (src, idx);
2500 : 0 : zero_length_string (lhs, si);
2501 : : }
2502 : : }
2503 : : else
2504 : 0 : zero_length_string (lhs, NULL);
2505 : : }
2506 : :
2507 : : /* Handle a strcpy-like ({st{r,p}cpy,__st{r,p}cpy_chk}) call.
2508 : : If strlen of the second argument is known, strlen of the first argument
2509 : : is the same after this call. Furthermore, attempt to convert it to
2510 : : memcpy. Uses RVALS to determine range information. */
2511 : :
2512 : : void
2513 : 2803 : strlen_pass::handle_builtin_strcpy (built_in_function bcode)
2514 : : {
2515 : 2803 : int idx, didx;
2516 : 2803 : tree src, dst, srclen, len, lhs, type, fn, oldlen;
2517 : 2803 : bool success;
2518 : 2803 : gimple *stmt = gsi_stmt (m_gsi);
2519 : 2803 : strinfo *si, *dsi, *olddsi, *zsi;
2520 : 2803 : location_t loc;
2521 : :
2522 : 2803 : src = gimple_call_arg (stmt, 1);
2523 : 2803 : dst = gimple_call_arg (stmt, 0);
2524 : 2803 : lhs = gimple_call_lhs (stmt);
2525 : 2803 : idx = get_stridx (src, stmt);
2526 : 2803 : si = NULL;
2527 : 2803 : if (idx > 0)
2528 : 1525 : si = get_strinfo (idx);
2529 : :
2530 : 2803 : didx = get_stridx (dst, stmt);
2531 : 2803 : olddsi = NULL;
2532 : 2803 : oldlen = NULL_TREE;
2533 : 2803 : if (didx > 0)
2534 : 1294 : olddsi = get_strinfo (didx);
2535 : 1509 : else if (didx < 0)
2536 : : return;
2537 : :
2538 : 1294 : if (olddsi != NULL)
2539 : 474 : adjust_last_stmt (olddsi, stmt, false);
2540 : :
2541 : 2803 : srclen = NULL_TREE;
2542 : 2803 : if (si != NULL)
2543 : 1439 : srclen = get_string_length (si);
2544 : 1364 : else if (idx < 0)
2545 : 9 : srclen = build_int_cst (size_type_node, ~idx);
2546 : :
2547 : 2803 : maybe_warn_overflow (stmt, false, srclen, olddsi, true);
2548 : :
2549 : 2803 : if (olddsi != NULL)
2550 : 474 : adjust_last_stmt (olddsi, stmt, false);
2551 : :
2552 : 2803 : loc = gimple_location (stmt);
2553 : 2803 : if (srclen == NULL_TREE)
2554 : 1355 : switch (bcode)
2555 : : {
2556 : 1042 : case BUILT_IN_STRCPY:
2557 : 1042 : case BUILT_IN_STRCPY_CHK:
2558 : 1042 : if (lhs != NULL_TREE || !builtin_decl_implicit_p (BUILT_IN_STPCPY))
2559 : : return;
2560 : : break;
2561 : 313 : case BUILT_IN_STPCPY:
2562 : 313 : case BUILT_IN_STPCPY_CHK:
2563 : 313 : if (lhs == NULL_TREE)
2564 : : return;
2565 : : else
2566 : : {
2567 : 313 : tree lhsuint = fold_convert_loc (loc, size_type_node, lhs);
2568 : 313 : srclen = fold_convert_loc (loc, size_type_node, dst);
2569 : 313 : srclen = fold_build2_loc (loc, MINUS_EXPR, size_type_node,
2570 : : lhsuint, srclen);
2571 : : }
2572 : 313 : break;
2573 : 0 : default:
2574 : 0 : gcc_unreachable ();
2575 : : }
2576 : :
2577 : 2216 : if (didx == 0)
2578 : : {
2579 : 1109 : didx = new_stridx (dst);
2580 : 1109 : if (didx == 0)
2581 : : return;
2582 : : }
2583 : 2214 : if (olddsi != NULL)
2584 : : {
2585 : 457 : oldlen = olddsi->nonzero_chars;
2586 : 457 : dsi = unshare_strinfo (olddsi);
2587 : 457 : dsi->nonzero_chars = srclen;
2588 : 457 : dsi->full_string_p = (srclen != NULL_TREE);
2589 : : /* Break the chain, so adjust_related_strinfo on later pointers in
2590 : : the chain won't adjust this one anymore. */
2591 : 457 : dsi->next = 0;
2592 : 457 : dsi->stmt = NULL;
2593 : 457 : dsi->endptr = NULL_TREE;
2594 : : }
2595 : : else
2596 : : {
2597 : 1757 : dsi = new_strinfo (dst, didx, srclen, srclen != NULL_TREE);
2598 : 1757 : set_strinfo (didx, dsi);
2599 : 1757 : find_equal_ptrs (dst, didx);
2600 : : }
2601 : 2214 : dsi->writable = true;
2602 : 2214 : dsi->dont_invalidate = true;
2603 : :
2604 : 2214 : if (dsi->nonzero_chars == NULL_TREE)
2605 : : {
2606 : 455 : strinfo *chainsi;
2607 : :
2608 : : /* If string length of src is unknown, use delayed length
2609 : : computation. If string length of dst will be needed, it
2610 : : can be computed by transforming this strcpy call into
2611 : : stpcpy and subtracting dst from the return value. */
2612 : :
2613 : : /* Look for earlier strings whose length could be determined if
2614 : : this strcpy is turned into an stpcpy. */
2615 : :
2616 : 455 : if (dsi->prev != 0 && (chainsi = verify_related_strinfos (dsi)) != NULL)
2617 : : {
2618 : 5 : for (; chainsi && chainsi != dsi; chainsi = get_strinfo (chainsi->next))
2619 : : {
2620 : : /* When setting a stmt for delayed length computation
2621 : : prevent all strinfos through dsi from being
2622 : : invalidated. */
2623 : 3 : chainsi = unshare_strinfo (chainsi);
2624 : 3 : chainsi->stmt = stmt;
2625 : 3 : chainsi->nonzero_chars = NULL_TREE;
2626 : 3 : chainsi->full_string_p = false;
2627 : 3 : chainsi->endptr = NULL_TREE;
2628 : 3 : chainsi->dont_invalidate = true;
2629 : : }
2630 : : }
2631 : 455 : dsi->stmt = stmt;
2632 : :
2633 : : /* Try to detect overlap before returning. This catches cases
2634 : : like strcpy (d, d + n) where n is non-constant whose range
2635 : : is such that (n <= strlen (d) holds).
2636 : :
2637 : : OLDDSI->NONZERO_chars may have been reset by this point with
2638 : : oldlen holding it original value. */
2639 : 455 : if (olddsi && oldlen)
2640 : : {
2641 : : /* Add 1 for the terminating NUL. */
2642 : 111 : tree type = TREE_TYPE (oldlen);
2643 : 111 : oldlen = fold_build2 (PLUS_EXPR, type, oldlen,
2644 : : build_int_cst (type, 1));
2645 : 111 : check_bounds_or_overlap (stmt, olddsi->ptr, src, oldlen, NULL_TREE);
2646 : : }
2647 : :
2648 : 455 : return;
2649 : : }
2650 : :
2651 : 1759 : if (olddsi != NULL)
2652 : : {
2653 : 171 : tree adj = NULL_TREE;
2654 : 171 : if (oldlen == NULL_TREE)
2655 : : ;
2656 : 74 : else if (integer_zerop (oldlen))
2657 : : adj = srclen;
2658 : 61 : else if (TREE_CODE (oldlen) == INTEGER_CST
2659 : 3 : || TREE_CODE (srclen) == INTEGER_CST)
2660 : 116 : adj = fold_build2_loc (loc, MINUS_EXPR,
2661 : 58 : TREE_TYPE (srclen), srclen,
2662 : 58 : fold_convert_loc (loc, TREE_TYPE (srclen),
2663 : : oldlen));
2664 : 71 : if (adj != NULL_TREE)
2665 : 71 : adjust_related_strinfos (loc, dsi, adj);
2666 : : else
2667 : 100 : dsi->prev = 0;
2668 : : }
2669 : : /* strcpy src may not overlap dst, so src doesn't need to be
2670 : : invalidated either. */
2671 : 1759 : if (si != NULL)
2672 : 1439 : si->dont_invalidate = true;
2673 : :
2674 : 1759 : fn = NULL_TREE;
2675 : 1759 : zsi = NULL;
2676 : 1759 : switch (bcode)
2677 : : {
2678 : 1343 : case BUILT_IN_STRCPY:
2679 : 1343 : fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
2680 : 1343 : if (lhs)
2681 : 95 : ssa_ver_to_stridx[SSA_NAME_VERSION (lhs)] = didx;
2682 : : break;
2683 : 1 : case BUILT_IN_STRCPY_CHK:
2684 : 1 : fn = builtin_decl_explicit (BUILT_IN_MEMCPY_CHK);
2685 : 1 : if (lhs)
2686 : 0 : ssa_ver_to_stridx[SSA_NAME_VERSION (lhs)] = didx;
2687 : : break;
2688 : 343 : case BUILT_IN_STPCPY:
2689 : : /* This would need adjustment of the lhs (subtract one),
2690 : : or detection that the trailing '\0' doesn't need to be
2691 : : written, if it will be immediately overwritten.
2692 : : fn = builtin_decl_explicit (BUILT_IN_MEMPCPY); */
2693 : 343 : if (lhs)
2694 : : {
2695 : 343 : dsi->endptr = lhs;
2696 : 343 : zsi = zero_length_string (lhs, dsi);
2697 : : }
2698 : : break;
2699 : 72 : case BUILT_IN_STPCPY_CHK:
2700 : : /* This would need adjustment of the lhs (subtract one),
2701 : : or detection that the trailing '\0' doesn't need to be
2702 : : written, if it will be immediately overwritten.
2703 : : fn = builtin_decl_explicit (BUILT_IN_MEMPCPY_CHK); */
2704 : 72 : if (lhs)
2705 : : {
2706 : 72 : dsi->endptr = lhs;
2707 : 72 : zsi = zero_length_string (lhs, dsi);
2708 : : }
2709 : : break;
2710 : 0 : default:
2711 : 0 : gcc_unreachable ();
2712 : : }
2713 : 510 : if (zsi != NULL)
2714 : 415 : zsi->dont_invalidate = true;
2715 : :
2716 : 1759 : if (fn)
2717 : : {
2718 : 1344 : tree args = TYPE_ARG_TYPES (TREE_TYPE (fn));
2719 : 1344 : type = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (args)));
2720 : : }
2721 : : else
2722 : 415 : type = size_type_node;
2723 : :
2724 : 1759 : len = fold_convert_loc (loc, type, unshare_expr (srclen));
2725 : 1759 : len = fold_build2_loc (loc, PLUS_EXPR, type, len, build_int_cst (type, 1));
2726 : :
2727 : : /* Disable warning for the transformed statement? */
2728 : 1759 : opt_code no_warning_opt = no_warning;
2729 : :
2730 : 1759 : if (const strinfo *chksi = si ? olddsi ? olddsi : dsi : NULL)
2731 : : {
2732 : 1439 : no_warning_opt = check_bounds_or_overlap (stmt, chksi->ptr, si->ptr,
2733 : : NULL_TREE, len);
2734 : 1439 : if (no_warning_opt)
2735 : 247 : suppress_warning (stmt, no_warning_opt);
2736 : : }
2737 : :
2738 : 1759 : if (fn == NULL_TREE)
2739 : : return;
2740 : :
2741 : 1344 : len = force_gimple_operand_gsi (&m_gsi, len, true, NULL_TREE, true,
2742 : : GSI_SAME_STMT);
2743 : 1344 : if (dump_file && (dump_flags & TDF_DETAILS) != 0)
2744 : : {
2745 : 0 : fprintf (dump_file, "Optimizing: ");
2746 : 0 : print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
2747 : : }
2748 : 1344 : if (gimple_call_num_args (stmt) == 2)
2749 : 1343 : success = update_gimple_call (&m_gsi, fn, 3, dst, src, len);
2750 : : else
2751 : 1 : success = update_gimple_call (&m_gsi, fn, 4, dst, src, len,
2752 : : gimple_call_arg (stmt, 2));
2753 : 1344 : if (success)
2754 : : {
2755 : 1344 : stmt = gsi_stmt (m_gsi);
2756 : 1344 : update_stmt (stmt);
2757 : 1344 : if (dump_file && (dump_flags & TDF_DETAILS) != 0)
2758 : : {
2759 : 0 : fprintf (dump_file, "into: ");
2760 : 0 : print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
2761 : : }
2762 : : /* Allow adjust_last_stmt to decrease this memcpy's size. */
2763 : 1344 : laststmt.stmt = stmt;
2764 : 1344 : laststmt.len = srclen;
2765 : 1344 : laststmt.stridx = dsi->idx;
2766 : : }
2767 : 0 : else if (dump_file && (dump_flags & TDF_DETAILS) != 0)
2768 : 0 : fprintf (dump_file, "not possible.\n");
2769 : :
2770 : 1344 : if (no_warning_opt)
2771 : 247 : suppress_warning (stmt, no_warning_opt);
2772 : : }
2773 : :
2774 : : /* Check the size argument to the built-in forms of stpncpy and strncpy
2775 : : for out-of-bounds offsets or overlapping access, and to see if the
2776 : : size argument is derived from a call to strlen() on the source argument,
2777 : : and if so, issue an appropriate warning. */
2778 : :
2779 : : void
2780 : 805 : strlen_pass::handle_builtin_strncat (built_in_function)
2781 : : {
2782 : : /* Same as stxncpy(). */
2783 : 805 : handle_builtin_stxncpy_strncat (true);
2784 : 805 : }
2785 : :
2786 : : /* Return true if LEN depends on a call to strlen(SRC) in an interesting
2787 : : way. LEN can either be an integer expression, or a pointer (to char).
2788 : : When it is the latter (such as in recursive calls to self) it is
2789 : : assumed to be the argument in some call to strlen() whose relationship
2790 : : to SRC is being ascertained. */
2791 : :
2792 : : bool
2793 : 883 : is_strlen_related_p (tree src, tree len)
2794 : : {
2795 : 1341 : if (TREE_CODE (TREE_TYPE (len)) == POINTER_TYPE
2796 : 1341 : && operand_equal_p (src, len, 0))
2797 : : return true;
2798 : :
2799 : 1196 : if (TREE_CODE (len) != SSA_NAME)
2800 : : return false;
2801 : :
2802 : 826 : if (TREE_CODE (src) == SSA_NAME)
2803 : : {
2804 : 578 : gimple *srcdef = SSA_NAME_DEF_STMT (src);
2805 : 578 : if (is_gimple_assign (srcdef))
2806 : : {
2807 : : /* Handle bitwise AND used in conversions from wider size_t
2808 : : to narrower unsigned types. */
2809 : 240 : tree_code code = gimple_assign_rhs_code (srcdef);
2810 : 240 : if (code == BIT_AND_EXPR
2811 : 240 : || code == NOP_EXPR)
2812 : 151 : return is_strlen_related_p (gimple_assign_rhs1 (srcdef), len);
2813 : :
2814 : : return false;
2815 : : }
2816 : :
2817 : 338 : if (gimple_call_builtin_p (srcdef, BUILT_IN_NORMAL))
2818 : : {
2819 : : /* If SRC is the result of a call to an allocation function
2820 : : or strlen, use the function's argument instead. */
2821 : 97 : tree func = gimple_call_fndecl (srcdef);
2822 : 97 : built_in_function code = DECL_FUNCTION_CODE (func);
2823 : 97 : if (code == BUILT_IN_ALLOCA
2824 : 97 : || code == BUILT_IN_ALLOCA_WITH_ALIGN
2825 : 16 : || code == BUILT_IN_MALLOC
2826 : 16 : || code == BUILT_IN_STRLEN)
2827 : 95 : return is_strlen_related_p (gimple_call_arg (srcdef, 0), len);
2828 : :
2829 : : /* FIXME: Handle other functions with attribute alloc_size. */
2830 : : return false;
2831 : : }
2832 : : }
2833 : :
2834 : 489 : gimple *lendef = SSA_NAME_DEF_STMT (len);
2835 : 489 : if (!lendef)
2836 : : return false;
2837 : :
2838 : 489 : if (is_gimple_call (lendef))
2839 : : {
2840 : 68 : tree func = gimple_call_fndecl (lendef);
2841 : 68 : if (!valid_builtin_call (lendef)
2842 : 68 : || DECL_FUNCTION_CODE (func) != BUILT_IN_STRLEN)
2843 : : return false;
2844 : :
2845 : 68 : tree arg = gimple_call_arg (lendef, 0);
2846 : 68 : return is_strlen_related_p (src, arg);
2847 : : }
2848 : :
2849 : 421 : if (!is_gimple_assign (lendef))
2850 : : return false;
2851 : :
2852 : 217 : tree_code code = gimple_assign_rhs_code (lendef);
2853 : 217 : tree rhs1 = gimple_assign_rhs1 (lendef);
2854 : 217 : tree rhstype = TREE_TYPE (rhs1);
2855 : :
2856 : 217 : if ((TREE_CODE (rhstype) == POINTER_TYPE && code == POINTER_PLUS_EXPR)
2857 : 217 : || (INTEGRAL_TYPE_P (rhstype)
2858 : 217 : && (code == BIT_AND_EXPR
2859 : 217 : || code == NOP_EXPR)))
2860 : : {
2861 : : /* Pointer plus (an integer), and truncation are considered among
2862 : : the (potentially) related expressions to strlen. */
2863 : : return is_strlen_related_p (src, rhs1);
2864 : : }
2865 : :
2866 : 85 : if (tree rhs2 = gimple_assign_rhs2 (lendef))
2867 : : {
2868 : : /* Integer subtraction is considered strlen-related when both
2869 : : arguments are integers and second one is strlen-related. */
2870 : 82 : rhstype = TREE_TYPE (rhs2);
2871 : 82 : if (INTEGRAL_TYPE_P (rhstype) && code == MINUS_EXPR)
2872 : : return is_strlen_related_p (src, rhs2);
2873 : : }
2874 : :
2875 : : return false;
2876 : : }
2877 : :
2878 : : /* Called by handle_builtin_stxncpy_strncat and by
2879 : : gimple_fold_builtin_strncpy in gimple-fold.cc.
2880 : : Check to see if the specified bound is a) equal to the size of
2881 : : the destination DST and if so, b) if it's immediately followed by
2882 : : DST[CNT - 1] = '\0'. If a) holds and b) does not, warn. Otherwise,
2883 : : do nothing. Return true if diagnostic has been issued.
2884 : :
2885 : : The purpose is to diagnose calls to strncpy and stpncpy that do
2886 : : not nul-terminate the copy while allowing for the idiom where
2887 : : such a call is immediately followed by setting the last element
2888 : : to nul, as in:
2889 : : char a[32];
2890 : : strncpy (a, s, sizeof a);
2891 : : a[sizeof a - 1] = '\0';
2892 : : */
2893 : :
2894 : : bool
2895 : 3592 : maybe_diag_stxncpy_trunc (gimple_stmt_iterator gsi, tree src, tree cnt,
2896 : : pointer_query *ptr_qry /* = NULL */)
2897 : : {
2898 : 3592 : gimple *stmt = gsi_stmt (gsi);
2899 : 3592 : if (warning_suppressed_p (stmt, OPT_Wstringop_truncation))
2900 : : return false;
2901 : :
2902 : 17870 : wide_int cntrange[2];
2903 : 3574 : int_range_max r;
2904 : 7148 : if (!get_range_query (cfun)->range_of_expr (r, cnt)
2905 : 3574 : || r.varying_p ()
2906 : 6704 : || r.undefined_p ())
2907 : : return false;
2908 : :
2909 : 3130 : tree min, max;
2910 : 3130 : value_range_kind kind = get_legacy_range (r, min, max);
2911 : 3130 : cntrange[0] = wi::to_wide (min);
2912 : 3130 : cntrange[1] = wi::to_wide (max);
2913 : 3130 : if (kind == VR_ANTI_RANGE)
2914 : : {
2915 : 178 : wide_int maxobjsize = wi::to_wide (TYPE_MAX_VALUE (ptrdiff_type_node));
2916 : :
2917 : 178 : if (wi::ltu_p (cntrange[1], maxobjsize))
2918 : : {
2919 : 0 : cntrange[0] = cntrange[1] + 1;
2920 : 0 : cntrange[1] = maxobjsize;
2921 : : }
2922 : : else
2923 : : {
2924 : 178 : cntrange[1] = cntrange[0] - 1;
2925 : 178 : cntrange[0] = wi::zero (TYPE_PRECISION (TREE_TYPE (cnt)));
2926 : : }
2927 : 178 : }
2928 : :
2929 : : /* Negative value is the constant string length. If it's less than
2930 : : the lower bound there is no truncation. Avoid calling get_stridx()
2931 : : when ssa_ver_to_stridx is empty. That implies the caller isn't
2932 : : running under the control of this pass and ssa_ver_to_stridx hasn't
2933 : : been created yet. */
2934 : 3130 : int sidx = ssa_ver_to_stridx.length () ? get_stridx (src, stmt) : 0;
2935 : 2435 : if (sidx < 0 && wi::gtu_p (cntrange[0], ~sidx))
2936 : 177 : return false;
2937 : :
2938 : 2953 : tree dst = gimple_call_arg (stmt, 0);
2939 : 2953 : tree dstdecl = dst;
2940 : 2953 : if (TREE_CODE (dstdecl) == ADDR_EXPR)
2941 : 1102 : dstdecl = TREE_OPERAND (dstdecl, 0);
2942 : :
2943 : 2953 : tree ref = NULL_TREE;
2944 : :
2945 : 2953 : if (!sidx)
2946 : : {
2947 : : /* If the source is a non-string return early to avoid warning
2948 : : for possible truncation (if the truncation is certain SIDX
2949 : : is non-zero). */
2950 : 2396 : tree srcdecl = gimple_call_arg (stmt, 1);
2951 : 2396 : if (TREE_CODE (srcdecl) == ADDR_EXPR)
2952 : 1228 : srcdecl = TREE_OPERAND (srcdecl, 0);
2953 : 2396 : if (get_attr_nonstring_decl (srcdecl, &ref))
2954 : : return false;
2955 : : }
2956 : :
2957 : : /* Likewise, if the destination refers to an array/pointer declared
2958 : : nonstring return early. */
2959 : 2543 : if (get_attr_nonstring_decl (dstdecl, &ref))
2960 : : return false;
2961 : :
2962 : : /* Look for dst[i] = '\0'; after the stxncpy() call and if found
2963 : : avoid the truncation warning. */
2964 : 1933 : gsi_next_nondebug (&gsi);
2965 : 1933 : gimple *next_stmt = gsi_stmt (gsi);
2966 : 1933 : if (!next_stmt)
2967 : : {
2968 : : /* When there is no statement in the same basic block check
2969 : : the immediate successor block. */
2970 : 55 : if (basic_block bb = gimple_bb (stmt))
2971 : : {
2972 : 30 : if (single_succ_p (bb))
2973 : : {
2974 : : /* For simplicity, ignore blocks with multiple outgoing
2975 : : edges for now and only consider successor blocks along
2976 : : normal edges. */
2977 : 30 : edge e = EDGE_SUCC (bb, 0);
2978 : 30 : if (!(e->flags & EDGE_ABNORMAL))
2979 : : {
2980 : 30 : gsi = gsi_start_bb (e->dest);
2981 : 30 : next_stmt = gsi_stmt (gsi);
2982 : 30 : if (next_stmt && is_gimple_debug (next_stmt))
2983 : : {
2984 : 21 : gsi_next_nondebug (&gsi);
2985 : 21 : next_stmt = gsi_stmt (gsi);
2986 : : }
2987 : : }
2988 : : }
2989 : : }
2990 : : }
2991 : :
2992 : 1933 : if (next_stmt && is_gimple_assign (next_stmt))
2993 : : {
2994 : 460 : tree lhs = gimple_assign_lhs (next_stmt);
2995 : 460 : tree_code code = TREE_CODE (lhs);
2996 : 460 : if (code == ARRAY_REF || code == MEM_REF)
2997 : 116 : lhs = TREE_OPERAND (lhs, 0);
2998 : :
2999 : 460 : tree func = gimple_call_fndecl (stmt);
3000 : 460 : if (DECL_FUNCTION_CODE (func) == BUILT_IN_STPNCPY)
3001 : : {
3002 : 65 : tree ret = gimple_call_lhs (stmt);
3003 : 65 : if (ret && operand_equal_p (ret, lhs, 0))
3004 : 104 : return false;
3005 : : }
3006 : :
3007 : : /* Determine the base address and offset of the reference,
3008 : : ignoring the innermost array index. */
3009 : 444 : if (TREE_CODE (ref) == ARRAY_REF)
3010 : 58 : ref = TREE_OPERAND (ref, 0);
3011 : :
3012 : 444 : poly_int64 dstoff;
3013 : 444 : tree dstbase = get_addr_base_and_unit_offset (ref, &dstoff);
3014 : :
3015 : 444 : poly_int64 lhsoff;
3016 : 444 : tree lhsbase = get_addr_base_and_unit_offset (lhs, &lhsoff);
3017 : 444 : if (lhsbase
3018 : 444 : && dstbase
3019 : 441 : && known_eq (dstoff, lhsoff)
3020 : 805 : && operand_equal_p (dstbase, lhsbase, 0))
3021 : : return false;
3022 : : }
3023 : :
3024 : 1829 : int prec = TYPE_PRECISION (TREE_TYPE (cnt));
3025 : 10974 : wide_int lenrange[2];
3026 : 1829 : if (strinfo *sisrc = sidx > 0 ? get_strinfo (sidx) : NULL)
3027 : : {
3028 : 99 : lenrange[0] = (sisrc->nonzero_chars
3029 : 99 : && TREE_CODE (sisrc->nonzero_chars) == INTEGER_CST
3030 : 198 : ? wi::to_wide (sisrc->nonzero_chars)
3031 : 1 : : wi::zero (prec));
3032 : 99 : lenrange[1] = lenrange[0];
3033 : : }
3034 : 1730 : else if (sidx < 0)
3035 : 375 : lenrange[0] = lenrange[1] = wi::shwi (~sidx, prec);
3036 : : else
3037 : : {
3038 : 1355 : c_strlen_data lendata = { };
3039 : : /* Set MAXBOUND to an arbitrary non-null non-integer node as a request
3040 : : to have it set to the length of the longest string in a PHI. */
3041 : 1355 : lendata.maxbound = src;
3042 : 1355 : get_range_strlen (src, &lendata, /* eltsize = */1);
3043 : 1355 : if (TREE_CODE (lendata.minlen) == INTEGER_CST
3044 : 1355 : && TREE_CODE (lendata.maxbound) == INTEGER_CST)
3045 : : {
3046 : : /* When LENDATA.MAXLEN is unknown, reset LENDATA.MINLEN
3047 : : which stores the length of the shortest known string. */
3048 : 1355 : if (integer_all_onesp (lendata.maxlen))
3049 : 612 : lenrange[0] = wi::shwi (0, prec);
3050 : : else
3051 : 743 : lenrange[0] = wi::to_wide (lendata.minlen, prec);
3052 : 1355 : lenrange[1] = wi::to_wide (lendata.maxbound, prec);
3053 : : }
3054 : : else
3055 : : {
3056 : 0 : lenrange[0] = wi::shwi (0, prec);
3057 : 0 : lenrange[1] = wi::shwi (-1, prec);
3058 : : }
3059 : : }
3060 : :
3061 : 1829 : location_t callloc = gimple_or_expr_nonartificial_location (stmt, dst);
3062 : 1829 : tree func = gimple_call_fndecl (stmt);
3063 : :
3064 : 1829 : if (lenrange[0] != 0 || !wi::neg_p (lenrange[1]))
3065 : : {
3066 : : /* If the longest source string is shorter than the lower bound
3067 : : of the specified count the copy is definitely nul-terminated. */
3068 : 1248 : if (wi::ltu_p (lenrange[1], cntrange[0]))
3069 : : return false;
3070 : :
3071 : 908 : if (wi::neg_p (lenrange[1]))
3072 : : {
3073 : : /* The length of one of the strings is unknown but at least
3074 : : one has non-zero length and that length is stored in
3075 : : LENRANGE[1]. Swap the bounds to force a "may be truncated"
3076 : : warning below. */
3077 : 0 : lenrange[1] = lenrange[0];
3078 : 0 : lenrange[0] = wi::shwi (0, prec);
3079 : : }
3080 : :
3081 : : /* Set to true for strncat whose bound is derived from the length
3082 : : of the destination (the expected usage pattern). */
3083 : 908 : bool cat_dstlen_bounded = false;
3084 : 908 : if (DECL_FUNCTION_CODE (func) == BUILT_IN_STRNCAT)
3085 : 156 : cat_dstlen_bounded = is_strlen_related_p (dst, cnt);
3086 : :
3087 : 1120 : if (lenrange[0] == cntrange[1] && cntrange[0] == cntrange[1])
3088 : 203 : return warning_n (callloc, OPT_Wstringop_truncation,
3089 : : cntrange[0].to_uhwi (),
3090 : : "%qD output truncated before terminating "
3091 : : "nul copying %E byte from a string of the "
3092 : : "same length",
3093 : : "%qD output truncated before terminating nul "
3094 : : "copying %E bytes from a string of the same "
3095 : : "length",
3096 : : func, cnt);
3097 : 705 : else if (!cat_dstlen_bounded)
3098 : : {
3099 : 701 : if (wi::geu_p (lenrange[0], cntrange[1]))
3100 : : {
3101 : : /* The shortest string is longer than the upper bound of
3102 : : the count so the truncation is certain. */
3103 : 345 : if (cntrange[0] == cntrange[1])
3104 : 304 : return warning_n (callloc, OPT_Wstringop_truncation,
3105 : : cntrange[0].to_uhwi (),
3106 : : "%qD output truncated copying %E byte "
3107 : : "from a string of length %wu",
3108 : : "%qD output truncated copying %E bytes "
3109 : : "from a string of length %wu",
3110 : : func, cnt, lenrange[0].to_uhwi ());
3111 : :
3112 : 41 : return warning_at (callloc, OPT_Wstringop_truncation,
3113 : : "%qD output truncated copying between %wu "
3114 : : "and %wu bytes from a string of length %wu",
3115 : : func, cntrange[0].to_uhwi (),
3116 : : cntrange[1].to_uhwi (), lenrange[0].to_uhwi ());
3117 : : }
3118 : 356 : else if (wi::geu_p (lenrange[1], cntrange[1]))
3119 : : {
3120 : : /* The longest string is longer than the upper bound of
3121 : : the count so the truncation is possible. */
3122 : 80 : if (cntrange[0] == cntrange[1])
3123 : 60 : return warning_n (callloc, OPT_Wstringop_truncation,
3124 : : cntrange[0].to_uhwi (),
3125 : : "%qD output may be truncated copying %E "
3126 : : "byte from a string of length %wu",
3127 : : "%qD output may be truncated copying %E "
3128 : : "bytes from a string of length %wu",
3129 : : func, cnt, lenrange[1].to_uhwi ());
3130 : :
3131 : 20 : return warning_at (callloc, OPT_Wstringop_truncation,
3132 : : "%qD output may be truncated copying between "
3133 : : "%wu and %wu bytes from a string of length %wu",
3134 : : func, cntrange[0].to_uhwi (),
3135 : : cntrange[1].to_uhwi (), lenrange[1].to_uhwi ());
3136 : : }
3137 : : }
3138 : :
3139 : 276 : if (!cat_dstlen_bounded
3140 : 276 : && cntrange[0] != cntrange[1]
3141 : 276 : && wi::leu_p (cntrange[0], lenrange[0])
3142 : 539 : && wi::leu_p (cntrange[1], lenrange[0] + 1))
3143 : : {
3144 : : /* If the source (including the terminating nul) is longer than
3145 : : the lower bound of the specified count but shorter than the
3146 : : upper bound the copy may (but need not) be truncated. */
3147 : 20 : return warning_at (callloc, OPT_Wstringop_truncation,
3148 : : "%qD output may be truncated copying between "
3149 : : "%wu and %wu bytes from a string of length %wu",
3150 : : func, cntrange[0].to_uhwi (),
3151 : : cntrange[1].to_uhwi (), lenrange[0].to_uhwi ());
3152 : : }
3153 : : }
3154 : :
3155 : 841 : access_ref aref;
3156 : 841 : if (tree dstsize = compute_objsize (dst, stmt, 1, &aref, ptr_qry))
3157 : : {
3158 : : /* The source length is unknown. Try to determine the destination
3159 : : size and see if it matches the specified bound. If not, bail.
3160 : : Otherwise go on to see if it should be diagnosed for possible
3161 : : truncation. */
3162 : 841 : if (!dstsize)
3163 : : return false;
3164 : :
3165 : 841 : if (wi::to_wide (dstsize) != cntrange[1])
3166 : : return false;
3167 : :
3168 : : /* Avoid warning for strncpy(a, b, N) calls where the following
3169 : : equalities hold:
3170 : : N == sizeof a && N == sizeof b */
3171 : 264 : if (tree srcsize = compute_objsize (src, stmt, 1, &aref, ptr_qry))
3172 : 264 : if (wi::to_wide (srcsize) == cntrange[1])
3173 : : return false;
3174 : :
3175 : 250 : if (cntrange[0] == cntrange[1])
3176 : 193 : return warning_at (callloc, OPT_Wstringop_truncation,
3177 : : "%qD specified bound %E equals destination size",
3178 : : func, cnt);
3179 : : }
3180 : :
3181 : : return false;
3182 : 16209 : }
3183 : :
3184 : : /* Check the arguments to the built-in forms of stpncpy, strncpy, and
3185 : : strncat, for out-of-bounds offsets or overlapping access, and to see
3186 : : if the size is derived from calling strlen() on the source argument,
3187 : : and if so, issue the appropriate warning.
3188 : : APPEND_P is true for strncat. */
3189 : :
3190 : : void
3191 : 3262 : strlen_pass::handle_builtin_stxncpy_strncat (bool append_p)
3192 : : {
3193 : 3262 : if (!strlen_to_stridx)
3194 : 3150 : return;
3195 : :
3196 : 3219 : gimple *stmt = gsi_stmt (m_gsi);
3197 : :
3198 : 3219 : tree dst = gimple_call_arg (stmt, 0);
3199 : 3219 : tree src = gimple_call_arg (stmt, 1);
3200 : 3219 : tree len = gimple_call_arg (stmt, 2);
3201 : : /* An upper bound of the size of the destination. */
3202 : 3219 : tree dstsize = NULL_TREE;
3203 : : /* The length of the destination and source strings (plus 1 for those
3204 : : whose FULL_STRING_P is set, i.e., whose length is exact rather than
3205 : : a lower bound). */
3206 : 3219 : tree dstlenp1 = NULL_TREE, srclenp1 = NULL_TREE;;
3207 : :
3208 : 3219 : int didx = get_stridx (dst, stmt);
3209 : 3219 : if (strinfo *sidst = didx > 0 ? get_strinfo (didx) : NULL)
3210 : : {
3211 : : /* Compute the size of the destination string including the nul
3212 : : if it is known to be nul-terminated. */
3213 : 313 : if (sidst->nonzero_chars)
3214 : : {
3215 : 270 : if (sidst->full_string_p)
3216 : : {
3217 : : /* String is known to be nul-terminated. */
3218 : 250 : tree type = TREE_TYPE (sidst->nonzero_chars);
3219 : 250 : dstlenp1 = fold_build2 (PLUS_EXPR, type, sidst->nonzero_chars,
3220 : : build_int_cst (type, 1));
3221 : : }
3222 : : else
3223 : : dstlenp1 = sidst->nonzero_chars;
3224 : : }
3225 : 43 : else if (TREE_CODE (sidst->ptr) == SSA_NAME)
3226 : : {
3227 : 43 : gimple *def_stmt = SSA_NAME_DEF_STMT (sidst->ptr);
3228 : 43 : dstsize = gimple_call_alloc_size (def_stmt);
3229 : : }
3230 : :
3231 : 313 : dst = sidst->ptr;
3232 : : }
3233 : :
3234 : 3219 : int sidx = get_stridx (src, stmt);
3235 : 3219 : strinfo *sisrc = sidx > 0 ? get_strinfo (sidx) : NULL;
3236 : 363 : if (sisrc)
3237 : : {
3238 : : /* strncat() and strncpy() can modify the source string by writing
3239 : : over the terminating nul so SISRC->DONT_INVALIDATE must be left
3240 : : clear. */
3241 : :
3242 : : /* Compute the size of the source string including the terminating
3243 : : nul if its known to be nul-terminated. */
3244 : 340 : if (sisrc->nonzero_chars)
3245 : : {
3246 : 340 : if (sisrc->full_string_p)
3247 : : {
3248 : 333 : tree type = TREE_TYPE (sisrc->nonzero_chars);
3249 : 333 : srclenp1 = fold_build2 (PLUS_EXPR, type, sisrc->nonzero_chars,
3250 : : build_int_cst (type, 1));
3251 : : }
3252 : : else
3253 : : srclenp1 = sisrc->nonzero_chars;
3254 : : }
3255 : :
3256 : 340 : src = sisrc->ptr;
3257 : : }
3258 : : else
3259 : : srclenp1 = NULL_TREE;
3260 : :
3261 : 3219 : opt_code opt = check_bounds_or_overlap (stmt, dst, src, dstlenp1, srclenp1);
3262 : 3219 : if (opt != no_warning)
3263 : : {
3264 : 210 : suppress_warning (stmt, opt);
3265 : 210 : return;
3266 : : }
3267 : :
3268 : : /* If the length argument was computed from strlen(S) for some string
3269 : : S retrieve the strinfo index for the string (PSS->FIRST) along with
3270 : : the location of the strlen() call (PSS->SECOND). */
3271 : 3009 : stridx_strlenloc *pss = strlen_to_stridx->get (len);
3272 : 3009 : if (!pss || pss->first <= 0)
3273 : : {
3274 : 2897 : if (maybe_diag_stxncpy_trunc (m_gsi, src, len))
3275 : 241 : suppress_warning (stmt, OPT_Wstringop_truncation);
3276 : :
3277 : 2897 : return;
3278 : : }
3279 : :
3280 : : /* Retrieve the strinfo data for the string S that LEN was computed
3281 : : from as some function F of strlen (S) (i.e., LEN need not be equal
3282 : : to strlen(S)). */
3283 : 112 : strinfo *silen = get_strinfo (pss->first);
3284 : :
3285 : 112 : location_t callloc = gimple_or_expr_nonartificial_location (stmt, dst);
3286 : :
3287 : 112 : tree func = gimple_call_fndecl (stmt);
3288 : :
3289 : 112 : bool warned = false;
3290 : :
3291 : : /* When -Wstringop-truncation is set, try to determine truncation
3292 : : before diagnosing possible overflow. Truncation is implied by
3293 : : the LEN argument being equal to strlen(SRC), regardless of
3294 : : whether its value is known. Otherwise, when appending, or
3295 : : when copying into a destination of known size, issue the more
3296 : : generic -Wstringop-overflow which triggers for LEN arguments
3297 : : that in any meaningful way depend on strlen(SRC). */
3298 : 112 : if (!append_p
3299 : 112 : && sisrc == silen
3300 : 64 : && is_strlen_related_p (src, len)
3301 : 156 : && warning_at (callloc, OPT_Wstringop_truncation,
3302 : : "%qD output truncated before terminating nul "
3303 : : "copying as many bytes from a string as its length",
3304 : : func))
3305 : : warned = true;
3306 : 1 : else if ((append_p || !dstsize || len == dstlenp1)
3307 : 84 : && silen && is_strlen_related_p (src, silen->ptr))
3308 : : {
3309 : : /* Issue -Wstringop-overflow when appending or when writing into
3310 : : a destination of a known size. Otherwise, when copying into
3311 : : a destination of an unknown size, it's truncation. */
3312 : 166 : opt_code opt = (append_p || dstsize
3313 : 83 : ? OPT_Wstringop_overflow_ : OPT_Wstringop_truncation);
3314 : 83 : warned = warning_at (callloc, opt,
3315 : : "%qD specified bound depends on the length "
3316 : : "of the source argument",
3317 : : func);
3318 : : }
3319 : 111 : if (warned)
3320 : : {
3321 : 87 : location_t strlenloc = pss->second;
3322 : 87 : if (strlenloc != UNKNOWN_LOCATION && strlenloc != callloc)
3323 : 87 : inform (strlenloc, "length computed here");
3324 : : }
3325 : : }
3326 : :
3327 : : /* Handle a memcpy-like ({mem{,p}cpy,__mem{,p}cpy_chk}) call.
3328 : : If strlen of the second argument is known and length of the third argument
3329 : : is that plus one, strlen of the first argument is the same after this
3330 : : call. Uses RVALS to determine range information. */
3331 : :
3332 : : void
3333 : 58482 : strlen_pass::handle_builtin_memcpy (built_in_function bcode)
3334 : : {
3335 : 58482 : tree lhs, oldlen, newlen;
3336 : 58482 : gimple *stmt = gsi_stmt (m_gsi);
3337 : 58482 : strinfo *si, *dsi;
3338 : :
3339 : 58482 : tree len = gimple_call_arg (stmt, 2);
3340 : 58482 : tree src = gimple_call_arg (stmt, 1);
3341 : 58482 : tree dst = gimple_call_arg (stmt, 0);
3342 : :
3343 : 58482 : int didx = get_stridx (dst, stmt);
3344 : 58482 : strinfo *olddsi = NULL;
3345 : 58482 : if (didx > 0)
3346 : 13538 : olddsi = get_strinfo (didx);
3347 : 44944 : else if (didx < 0)
3348 : : return;
3349 : :
3350 : 13538 : if (olddsi != NULL
3351 : 13538 : && !integer_zerop (len))
3352 : : {
3353 : 8708 : maybe_warn_overflow (stmt, false, len, olddsi, false, true);
3354 : 8708 : if (tree_fits_uhwi_p (len))
3355 : 3798 : adjust_last_stmt (olddsi, stmt, false);
3356 : : }
3357 : :
3358 : 58482 : int idx = get_stridx (src, stmt);
3359 : 58482 : if (idx == 0)
3360 : : return;
3361 : :
3362 : 17942 : bool full_string_p;
3363 : 17942 : if (idx > 0)
3364 : : {
3365 : 5823 : gimple *def_stmt;
3366 : :
3367 : : /* Handle memcpy (x, y, l) where l's relationship with strlen (y)
3368 : : is known. */
3369 : 5823 : si = get_strinfo (idx);
3370 : 5823 : if (si == NULL || si->nonzero_chars == NULL_TREE)
3371 : : return;
3372 : 1357 : if (TREE_CODE (len) == INTEGER_CST
3373 : 870 : && TREE_CODE (si->nonzero_chars) == INTEGER_CST)
3374 : : {
3375 : 859 : if (tree_int_cst_le (len, si->nonzero_chars))
3376 : : {
3377 : : /* Copying LEN nonzero characters, where LEN is constant. */
3378 : : newlen = len;
3379 : : full_string_p = false;
3380 : : }
3381 : : else
3382 : : {
3383 : : /* Copying the whole of the analyzed part of SI. */
3384 : 117 : newlen = si->nonzero_chars;
3385 : 117 : full_string_p = si->full_string_p;
3386 : : }
3387 : : }
3388 : : else
3389 : : {
3390 : 498 : if (!si->full_string_p)
3391 : : return;
3392 : 474 : if (TREE_CODE (len) != SSA_NAME)
3393 : : return;
3394 : 463 : def_stmt = SSA_NAME_DEF_STMT (len);
3395 : 463 : if (!is_gimple_assign (def_stmt)
3396 : 188 : || gimple_assign_rhs_code (def_stmt) != PLUS_EXPR
3397 : 113 : || gimple_assign_rhs1 (def_stmt) != si->nonzero_chars
3398 : 566 : || !integer_onep (gimple_assign_rhs2 (def_stmt)))
3399 : 360 : return;
3400 : : /* Copying variable-length string SI (and no more). */
3401 : 103 : newlen = si->nonzero_chars;
3402 : 103 : full_string_p = true;
3403 : : }
3404 : : }
3405 : : else
3406 : : {
3407 : 12119 : si = NULL;
3408 : : /* Handle memcpy (x, "abcd", 5) or
3409 : : memcpy (x, "abc\0uvw", 7). */
3410 : 12119 : if (!tree_fits_uhwi_p (len))
3411 : 74 : return;
3412 : :
3413 : 12045 : unsigned HOST_WIDE_INT clen = tree_to_uhwi (len);
3414 : 12045 : unsigned HOST_WIDE_INT nonzero_chars = ~idx;
3415 : 21054 : newlen = build_int_cst (size_type_node, MIN (nonzero_chars, clen));
3416 : 12045 : full_string_p = clen > nonzero_chars;
3417 : : }
3418 : :
3419 : 13007 : if (!full_string_p
3420 : 13007 : && olddsi
3421 : 621 : && olddsi->nonzero_chars
3422 : 369 : && TREE_CODE (olddsi->nonzero_chars) == INTEGER_CST
3423 : 13376 : && tree_int_cst_le (newlen, olddsi->nonzero_chars))
3424 : : {
3425 : : /* The SRC substring being written strictly overlaps
3426 : : a subsequence of the existing string OLDDSI. */
3427 : 154 : newlen = olddsi->nonzero_chars;
3428 : 154 : full_string_p = olddsi->full_string_p;
3429 : : }
3430 : :
3431 : 13007 : if (olddsi != NULL && TREE_CODE (len) == SSA_NAME)
3432 : 74 : adjust_last_stmt (olddsi, stmt, false);
3433 : :
3434 : 13007 : if (didx == 0)
3435 : : {
3436 : 9684 : didx = new_stridx (dst);
3437 : 9684 : if (didx == 0)
3438 : : return;
3439 : : }
3440 : 13004 : oldlen = NULL_TREE;
3441 : 13004 : if (olddsi != NULL)
3442 : : {
3443 : 2094 : dsi = unshare_strinfo (olddsi);
3444 : 2094 : oldlen = olddsi->nonzero_chars;
3445 : 2094 : dsi->nonzero_chars = newlen;
3446 : 2094 : dsi->full_string_p = full_string_p;
3447 : : /* Break the chain, so adjust_related_strinfo on later pointers in
3448 : : the chain won't adjust this one anymore. */
3449 : 2094 : dsi->next = 0;
3450 : 2094 : dsi->stmt = NULL;
3451 : 2094 : dsi->endptr = NULL_TREE;
3452 : : }
3453 : : else
3454 : : {
3455 : 10910 : dsi = new_strinfo (dst, didx, newlen, full_string_p);
3456 : 10910 : set_strinfo (didx, dsi);
3457 : 10910 : find_equal_ptrs (dst, didx);
3458 : : }
3459 : 13004 : dsi->writable = true;
3460 : 13004 : dsi->dont_invalidate = true;
3461 : 13004 : if (olddsi != NULL)
3462 : : {
3463 : 2094 : tree adj = NULL_TREE;
3464 : 2094 : location_t loc = gimple_location (stmt);
3465 : 2094 : if (oldlen == NULL_TREE)
3466 : : ;
3467 : 1249 : else if (integer_zerop (oldlen))
3468 : : adj = newlen;
3469 : 594 : else if (TREE_CODE (oldlen) == INTEGER_CST
3470 : 6 : || TREE_CODE (newlen) == INTEGER_CST)
3471 : 594 : adj = fold_build2_loc (loc, MINUS_EXPR, TREE_TYPE (newlen), newlen,
3472 : 594 : fold_convert_loc (loc, TREE_TYPE (newlen),
3473 : : oldlen));
3474 : 1249 : if (adj != NULL_TREE)
3475 : 1249 : adjust_related_strinfos (loc, dsi, adj);
3476 : : else
3477 : 845 : dsi->prev = 0;
3478 : : }
3479 : : /* memcpy src may not overlap dst, so src doesn't need to be
3480 : : invalidated either. */
3481 : 13004 : if (si != NULL)
3482 : 962 : si->dont_invalidate = true;
3483 : :
3484 : 13004 : if (full_string_p)
3485 : : {
3486 : 3259 : lhs = gimple_call_lhs (stmt);
3487 : 3259 : switch (bcode)
3488 : : {
3489 : 3203 : case BUILT_IN_MEMCPY:
3490 : 3203 : case BUILT_IN_MEMCPY_CHK:
3491 : : /* Allow adjust_last_stmt to decrease this memcpy's size. */
3492 : 3203 : laststmt.stmt = stmt;
3493 : 3203 : laststmt.len = dsi->nonzero_chars;
3494 : 3203 : laststmt.stridx = dsi->idx;
3495 : 3203 : if (lhs)
3496 : 323 : ssa_ver_to_stridx[SSA_NAME_VERSION (lhs)] = didx;
3497 : : break;
3498 : : case BUILT_IN_MEMPCPY:
3499 : : case BUILT_IN_MEMPCPY_CHK:
3500 : : break;
3501 : 0 : default:
3502 : 0 : gcc_unreachable ();
3503 : : }
3504 : : }
3505 : : }
3506 : :
3507 : : /* Handle a strcat-like ({strcat,__strcat_chk}) call.
3508 : : If strlen of the second argument is known, strlen of the first argument
3509 : : is increased by the length of the second argument. Furthermore, attempt
3510 : : to convert it to memcpy/strcpy if the length of the first argument
3511 : : is known. */
3512 : :
3513 : : void
3514 : 825 : strlen_pass::handle_builtin_strcat (built_in_function bcode)
3515 : : {
3516 : 825 : int idx, didx;
3517 : 825 : tree srclen, args, type, fn, objsz, endptr;
3518 : 825 : bool success;
3519 : 825 : gimple *stmt = gsi_stmt (m_gsi);
3520 : 825 : strinfo *si, *dsi;
3521 : 825 : location_t loc = gimple_location (stmt);
3522 : :
3523 : 825 : tree src = gimple_call_arg (stmt, 1);
3524 : 825 : tree dst = gimple_call_arg (stmt, 0);
3525 : :
3526 : : /* Bail if the source is the same as destination. It will be diagnosed
3527 : : elsewhere. */
3528 : 825 : if (operand_equal_p (src, dst, 0))
3529 : : return;
3530 : :
3531 : 786 : tree lhs = gimple_call_lhs (stmt);
3532 : :
3533 : 786 : didx = get_stridx (dst, stmt);
3534 : 786 : if (didx < 0)
3535 : : return;
3536 : :
3537 : 786 : dsi = NULL;
3538 : 786 : if (didx > 0)
3539 : 468 : dsi = get_strinfo (didx);
3540 : :
3541 : 786 : srclen = NULL_TREE;
3542 : 786 : si = NULL;
3543 : 786 : idx = get_stridx (src, stmt);
3544 : 786 : if (idx < 0)
3545 : 84 : srclen = build_int_cst (size_type_node, ~idx);
3546 : 702 : else if (idx > 0)
3547 : : {
3548 : 394 : si = get_strinfo (idx);
3549 : 394 : if (si != NULL)
3550 : 353 : srclen = get_string_length (si);
3551 : : }
3552 : :
3553 : : /* Disable warning for the transformed statement? */
3554 : 786 : opt_code no_warning_opt = no_warning;
3555 : :
3556 : 786 : if (dsi == NULL || get_string_length (dsi) == NULL_TREE)
3557 : : {
3558 : 463 : {
3559 : : /* The concatenation always involves copying at least one byte
3560 : : (the terminating nul), even if the source string is empty.
3561 : : If the source is unknown assume it's one character long and
3562 : : used that as both sizes. */
3563 : 463 : tree slen = srclen;
3564 : 463 : if (slen)
3565 : : {
3566 : 197 : tree type = TREE_TYPE (slen);
3567 : 197 : slen = fold_build2 (PLUS_EXPR, type, slen, build_int_cst (type, 1));
3568 : : }
3569 : :
3570 : 463 : tree sptr = si && si->ptr ? si->ptr : src;
3571 : 463 : no_warning_opt = check_bounds_or_overlap (stmt, dst, sptr, NULL_TREE,
3572 : : slen);
3573 : 463 : if (no_warning_opt)
3574 : 49 : suppress_warning (stmt, no_warning_opt);
3575 : : }
3576 : :
3577 : : /* strcat (p, q) can be transformed into
3578 : : tmp = p + strlen (p); endptr = stpcpy (tmp, q);
3579 : : with length endptr - p if we need to compute the length
3580 : : later on. Don't do this transformation if we don't need
3581 : : it. */
3582 : 600 : if (builtin_decl_implicit_p (BUILT_IN_STPCPY) && lhs == NULL_TREE)
3583 : : {
3584 : 69 : if (didx == 0)
3585 : : {
3586 : 41 : didx = new_stridx (dst);
3587 : 41 : if (didx == 0)
3588 : : return;
3589 : : }
3590 : 69 : if (dsi == NULL)
3591 : : {
3592 : 69 : dsi = new_strinfo (dst, didx, NULL_TREE, false);
3593 : 69 : set_strinfo (didx, dsi);
3594 : 69 : find_equal_ptrs (dst, didx);
3595 : : }
3596 : : else
3597 : : {
3598 : 0 : dsi = unshare_strinfo (dsi);
3599 : 0 : dsi->nonzero_chars = NULL_TREE;
3600 : 0 : dsi->full_string_p = false;
3601 : 0 : dsi->next = 0;
3602 : 0 : dsi->endptr = NULL_TREE;
3603 : : }
3604 : 69 : dsi->writable = true;
3605 : 69 : dsi->stmt = stmt;
3606 : 69 : dsi->dont_invalidate = true;
3607 : : }
3608 : 463 : return;
3609 : : }
3610 : :
3611 : 323 : tree dstlen = dsi->nonzero_chars;
3612 : 323 : endptr = dsi->endptr;
3613 : :
3614 : 323 : dsi = unshare_strinfo (dsi);
3615 : 323 : dsi->endptr = NULL_TREE;
3616 : 323 : dsi->stmt = NULL;
3617 : 323 : dsi->writable = true;
3618 : :
3619 : 323 : if (srclen != NULL_TREE)
3620 : : {
3621 : 240 : dsi->nonzero_chars = fold_build2_loc (loc, PLUS_EXPR,
3622 : 240 : TREE_TYPE (dsi->nonzero_chars),
3623 : : dsi->nonzero_chars, srclen);
3624 : 240 : gcc_assert (dsi->full_string_p);
3625 : 240 : adjust_related_strinfos (loc, dsi, srclen);
3626 : 240 : dsi->dont_invalidate = true;
3627 : : }
3628 : : else
3629 : : {
3630 : 83 : dsi->nonzero_chars = NULL;
3631 : 83 : dsi->full_string_p = false;
3632 : 83 : if (lhs == NULL_TREE && builtin_decl_implicit_p (BUILT_IN_STPCPY))
3633 : 63 : dsi->dont_invalidate = true;
3634 : : }
3635 : :
3636 : 323 : if (si != NULL)
3637 : : /* strcat src may not overlap dst, so src doesn't need to be
3638 : : invalidated either. */
3639 : 191 : si->dont_invalidate = true;
3640 : :
3641 : : /* For now. Could remove the lhs from the call and add
3642 : : lhs = dst; afterwards. */
3643 : 323 : if (lhs)
3644 : : return;
3645 : :
3646 : 189 : fn = NULL_TREE;
3647 : 189 : objsz = NULL_TREE;
3648 : 189 : switch (bcode)
3649 : : {
3650 : 174 : case BUILT_IN_STRCAT:
3651 : 174 : if (srclen != NULL_TREE)
3652 : 94 : fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
3653 : : else
3654 : 80 : fn = builtin_decl_implicit (BUILT_IN_STRCPY);
3655 : : break;
3656 : 15 : case BUILT_IN_STRCAT_CHK:
3657 : 15 : if (srclen != NULL_TREE)
3658 : 14 : fn = builtin_decl_explicit (BUILT_IN_MEMCPY_CHK);
3659 : : else
3660 : 1 : fn = builtin_decl_explicit (BUILT_IN_STRCPY_CHK);
3661 : 15 : objsz = gimple_call_arg (stmt, 2);
3662 : 15 : break;
3663 : 0 : default:
3664 : 0 : gcc_unreachable ();
3665 : : }
3666 : :
3667 : 189 : if (fn == NULL_TREE)
3668 : : return;
3669 : :
3670 : 189 : if (dsi && dstlen)
3671 : : {
3672 : 189 : tree type = TREE_TYPE (dstlen);
3673 : :
3674 : : /* Compute the size of the source sequence, including the nul. */
3675 : 189 : tree srcsize = srclen ? srclen : size_zero_node;
3676 : 189 : tree one = build_int_cst (type, 1);
3677 : 189 : srcsize = fold_build2 (PLUS_EXPR, type, srcsize, one);
3678 : 189 : tree dstsize = fold_build2 (PLUS_EXPR, type, dstlen, one);
3679 : 189 : tree sptr = si && si->ptr ? si->ptr : src;
3680 : :
3681 : 189 : no_warning_opt = check_bounds_or_overlap (stmt, dst, sptr, dstsize,
3682 : : srcsize);
3683 : 189 : if (no_warning_opt)
3684 : 71 : suppress_warning (stmt, no_warning_opt);
3685 : : }
3686 : :
3687 : 189 : tree len = NULL_TREE;
3688 : 189 : if (srclen != NULL_TREE)
3689 : : {
3690 : 108 : args = TYPE_ARG_TYPES (TREE_TYPE (fn));
3691 : 108 : type = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (args)));
3692 : :
3693 : 108 : len = fold_convert_loc (loc, type, unshare_expr (srclen));
3694 : 108 : len = fold_build2_loc (loc, PLUS_EXPR, type, len,
3695 : 108 : build_int_cst (type, 1));
3696 : 108 : len = force_gimple_operand_gsi (&m_gsi, len, true, NULL_TREE, true,
3697 : : GSI_SAME_STMT);
3698 : : }
3699 : 189 : if (endptr)
3700 : 24 : dst = fold_convert_loc (loc, TREE_TYPE (dst), unshare_expr (endptr));
3701 : : else
3702 : 165 : dst = fold_build2_loc (loc, POINTER_PLUS_EXPR, TREE_TYPE (dst), dst,
3703 : : fold_convert_loc (loc, sizetype,
3704 : : unshare_expr (dstlen)));
3705 : 189 : dst = force_gimple_operand_gsi (&m_gsi, dst, true, NULL_TREE, true,
3706 : : GSI_SAME_STMT);
3707 : 189 : if (objsz)
3708 : : {
3709 : 30 : objsz = fold_build2_loc (loc, MINUS_EXPR, TREE_TYPE (objsz), objsz,
3710 : 15 : fold_convert_loc (loc, TREE_TYPE (objsz),
3711 : : unshare_expr (dstlen)));
3712 : 15 : objsz = force_gimple_operand_gsi (&m_gsi, objsz, true, NULL_TREE, true,
3713 : : GSI_SAME_STMT);
3714 : : }
3715 : 189 : if (dump_file && (dump_flags & TDF_DETAILS) != 0)
3716 : : {
3717 : 0 : fprintf (dump_file, "Optimizing: ");
3718 : 0 : print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
3719 : : }
3720 : 189 : if (srclen != NULL_TREE)
3721 : 202 : success = update_gimple_call (&m_gsi, fn, 3 + (objsz != NULL_TREE),
3722 : : dst, src, len, objsz);
3723 : : else
3724 : 161 : success = update_gimple_call (&m_gsi, fn, 2 + (objsz != NULL_TREE),
3725 : : dst, src, objsz);
3726 : 189 : if (success)
3727 : : {
3728 : 189 : stmt = gsi_stmt (m_gsi);
3729 : 189 : update_stmt (stmt);
3730 : 189 : if (dump_file && (dump_flags & TDF_DETAILS) != 0)
3731 : : {
3732 : 0 : fprintf (dump_file, "into: ");
3733 : 0 : print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
3734 : : }
3735 : : /* If srclen == NULL, note that current string length can be
3736 : : computed by transforming this strcpy into stpcpy. */
3737 : 189 : if (srclen == NULL_TREE && dsi->dont_invalidate)
3738 : 63 : dsi->stmt = stmt;
3739 : 189 : adjust_last_stmt (dsi, stmt, true);
3740 : 189 : if (srclen != NULL_TREE)
3741 : : {
3742 : 108 : laststmt.stmt = stmt;
3743 : 108 : laststmt.len = srclen;
3744 : 108 : laststmt.stridx = dsi->idx;
3745 : : }
3746 : : }
3747 : 0 : else if (dump_file && (dump_flags & TDF_DETAILS) != 0)
3748 : 0 : fprintf (dump_file, "not possible.\n");
3749 : :
3750 : 189 : if (no_warning_opt)
3751 : 71 : suppress_warning (stmt, no_warning_opt);
3752 : : }
3753 : :
3754 : : /* Handle a call to an allocation function like alloca, malloc or calloc,
3755 : : or an ordinary allocation function declared with attribute alloc_size. */
3756 : :
3757 : : void
3758 : 64562 : strlen_pass::handle_alloc_call (built_in_function bcode)
3759 : : {
3760 : 64562 : gimple *stmt = gsi_stmt (m_gsi);
3761 : 64562 : tree lhs = gimple_call_lhs (stmt);
3762 : 64562 : if (lhs == NULL_TREE)
3763 : : return;
3764 : :
3765 : 64466 : gcc_assert (get_stridx (lhs, stmt) == 0);
3766 : 64466 : int idx = new_stridx (lhs);
3767 : 64466 : tree length = NULL_TREE;
3768 : 64466 : if (bcode == BUILT_IN_CALLOC)
3769 : 390 : length = build_int_cst (size_type_node, 0);
3770 : 64466 : strinfo *si = new_strinfo (lhs, idx, length, length != NULL_TREE);
3771 : 64466 : if (bcode == BUILT_IN_CALLOC)
3772 : : {
3773 : : /* Only set STMT for calloc and malloc. */
3774 : 390 : si->stmt = stmt;
3775 : : /* Only set ENDPTR for calloc. */
3776 : 390 : si->endptr = lhs;
3777 : : }
3778 : 64076 : else if (bcode == BUILT_IN_MALLOC)
3779 : 23397 : si->stmt = stmt;
3780 : :
3781 : : /* Set ALLOC is set for all allocation functions. */
3782 : 64466 : si->alloc = stmt;
3783 : 64466 : set_strinfo (idx, si);
3784 : 64466 : si->writable = true;
3785 : 64466 : si->dont_invalidate = true;
3786 : : }
3787 : :
3788 : : /* Handle a call to memset.
3789 : : After a call to calloc, memset(,0,) is unnecessary.
3790 : : memset(malloc(n),0,n) is calloc(n,1).
3791 : : return true when the call is transformed, false otherwise.
3792 : : When nonnull uses RVALS to determine range information. */
3793 : :
3794 : : bool
3795 : 24411 : strlen_pass::handle_builtin_memset (bool *zero_write)
3796 : : {
3797 : 24411 : gimple *memset_stmt = gsi_stmt (m_gsi);
3798 : 24411 : tree ptr = gimple_call_arg (memset_stmt, 0);
3799 : 24411 : tree memset_val = gimple_call_arg (memset_stmt, 1);
3800 : 24411 : tree memset_size = gimple_call_arg (memset_stmt, 2);
3801 : :
3802 : : /* Set to the non-constant offset added to PTR. */
3803 : 122055 : wide_int offrng[2];
3804 : 24411 : int idx1 = get_stridx (ptr, memset_stmt, offrng, ptr_qry.rvals);
3805 : 24411 : if (idx1 == 0
3806 : 15870 : && TREE_CODE (memset_val) == INTEGER_CST
3807 : 39456 : && ((TREE_CODE (memset_size) == INTEGER_CST
3808 : 7756 : && !integer_zerop (memset_size))
3809 : 7289 : || TREE_CODE (memset_size) == SSA_NAME))
3810 : : {
3811 : 15045 : unsigned HOST_WIDE_INT mask = (HOST_WIDE_INT_1U << CHAR_TYPE_SIZE) - 1;
3812 : 15045 : bool full_string_p = (wi::to_wide (memset_val) & mask) == 0;
3813 : :
3814 : : /* We only handle symbolic lengths when writing non-zero values. */
3815 : 15045 : if (full_string_p && TREE_CODE (memset_size) != INTEGER_CST)
3816 : : return false;
3817 : :
3818 : 10889 : idx1 = new_stridx (ptr);
3819 : 10889 : if (idx1 == 0)
3820 : : return false;
3821 : 10881 : tree newlen;
3822 : 10881 : if (full_string_p)
3823 : 5797 : newlen = build_int_cst (size_type_node, 0);
3824 : 5084 : else if (TREE_CODE (memset_size) == INTEGER_CST)
3825 : 1951 : newlen = fold_convert (size_type_node, memset_size);
3826 : : else
3827 : : newlen = memset_size;
3828 : :
3829 : 10881 : strinfo *dsi = new_strinfo (ptr, idx1, newlen, full_string_p);
3830 : 10881 : set_strinfo (idx1, dsi);
3831 : 10881 : find_equal_ptrs (ptr, idx1);
3832 : 10881 : dsi->dont_invalidate = true;
3833 : 10881 : dsi->writable = true;
3834 : 10881 : return false;
3835 : : }
3836 : :
3837 : 9366 : if (idx1 <= 0)
3838 : : return false;
3839 : 8541 : strinfo *si1 = get_strinfo (idx1);
3840 : 8541 : if (!si1)
3841 : : return false;
3842 : 3999 : gimple *alloc_stmt = si1->alloc;
3843 : 3999 : if (!alloc_stmt || !is_gimple_call (alloc_stmt))
3844 : : return false;
3845 : 2491 : tree callee1 = gimple_call_fndecl (alloc_stmt);
3846 : 2491 : if (!valid_builtin_call (alloc_stmt))
3847 : : return false;
3848 : 686 : tree alloc_size = gimple_call_arg (alloc_stmt, 0);
3849 : :
3850 : : /* Check for overflow. */
3851 : 686 : maybe_warn_overflow (memset_stmt, false, memset_size, NULL, false, true);
3852 : :
3853 : : /* Bail when there is no statement associated with the destination
3854 : : (the statement may be null even when SI1->ALLOC is not). */
3855 : 686 : if (!si1->stmt)
3856 : : return false;
3857 : :
3858 : : /* Avoid optimizing if store is at a variable offset from the beginning
3859 : : of the allocated object. */
3860 : 929 : if (offrng[0] != 0 || offrng[0] != offrng[1])
3861 : 49 : return false;
3862 : :
3863 : : /* Bail when the call writes a non-zero value. */
3864 : 431 : if (!integer_zerop (memset_val))
3865 : : return false;
3866 : :
3867 : : /* Let the caller know the memset call cleared the destination. */
3868 : 384 : *zero_write = true;
3869 : :
3870 : 384 : enum built_in_function code1 = DECL_FUNCTION_CODE (callee1);
3871 : 384 : if (code1 == BUILT_IN_CALLOC)
3872 : : /* Not touching alloc_stmt */ ;
3873 : 376 : else if (code1 == BUILT_IN_MALLOC
3874 : 376 : && operand_equal_p (memset_size, alloc_size, 0))
3875 : : {
3876 : : /* Replace the malloc + memset calls with calloc. */
3877 : 311 : gimple_stmt_iterator gsi1 = gsi_for_stmt (si1->stmt);
3878 : 622 : update_gimple_call (&gsi1, builtin_decl_implicit (BUILT_IN_CALLOC), 2,
3879 : : alloc_size, build_one_cst (size_type_node));
3880 : 311 : si1->nonzero_chars = build_int_cst (size_type_node, 0);
3881 : 311 : si1->full_string_p = true;
3882 : 311 : si1->stmt = gsi_stmt (gsi1);
3883 : : }
3884 : : else
3885 : 65 : return false;
3886 : 319 : tree lhs = gimple_call_lhs (memset_stmt);
3887 : 319 : unlink_stmt_vdef (memset_stmt);
3888 : 319 : if (lhs)
3889 : : {
3890 : 1 : gimple *assign = gimple_build_assign (lhs, ptr);
3891 : 1 : gsi_replace (&m_gsi, assign, false);
3892 : : }
3893 : : else
3894 : : {
3895 : 318 : gsi_remove (&m_gsi, true);
3896 : 318 : release_defs (memset_stmt);
3897 : : }
3898 : :
3899 : : return true;
3900 : 73233 : }
3901 : :
3902 : : /* Return first such statement if RES is used in statements testing its
3903 : : equality to zero, and null otherwise. If EXCLUSIVE is true, return
3904 : : nonnull if and only RES is used in such expressions exclusively and
3905 : : in none other. */
3906 : :
3907 : : gimple *
3908 : 77152 : use_in_zero_equality (tree res, bool exclusive)
3909 : : {
3910 : 77152 : gimple *first_use = NULL;
3911 : :
3912 : 77152 : use_operand_p use_p;
3913 : 77152 : imm_use_iterator iter;
3914 : :
3915 : 150702 : FOR_EACH_IMM_USE_FAST (use_p, iter, res)
3916 : : {
3917 : 83056 : gimple *use_stmt = USE_STMT (use_p);
3918 : :
3919 : 83056 : if (is_gimple_debug (use_stmt))
3920 : 5889 : continue;
3921 : :
3922 : 77167 : if (gimple_code (use_stmt) == GIMPLE_ASSIGN)
3923 : : {
3924 : 12705 : tree_code code = gimple_assign_rhs_code (use_stmt);
3925 : 12705 : if (code == COND_EXPR)
3926 : : {
3927 : 0 : tree cond_expr = gimple_assign_rhs1 (use_stmt);
3928 : 0 : if ((TREE_CODE (cond_expr) != EQ_EXPR
3929 : 0 : && (TREE_CODE (cond_expr) != NE_EXPR))
3930 : 0 : || !integer_zerop (TREE_OPERAND (cond_expr, 1)))
3931 : : {
3932 : 0 : if (exclusive)
3933 : : return NULL;
3934 : 0 : continue;
3935 : : }
3936 : : }
3937 : 12705 : else if (code == EQ_EXPR || code == NE_EXPR)
3938 : : {
3939 : 5724 : if (!integer_zerop (gimple_assign_rhs2 (use_stmt)))
3940 : : {
3941 : 7 : if (exclusive)
3942 : : return NULL;
3943 : 1 : continue;
3944 : : }
3945 : : }
3946 : 6981 : else if (exclusive)
3947 : : return NULL;
3948 : : else
3949 : 18 : continue;
3950 : : }
3951 : 64462 : else if (gimple_code (use_stmt) == GIMPLE_COND)
3952 : : {
3953 : 62084 : tree_code code = gimple_cond_code (use_stmt);
3954 : 62086 : if ((code != EQ_EXPR && code != NE_EXPR)
3955 : 62084 : || !integer_zerop (gimple_cond_rhs (use_stmt)))
3956 : : {
3957 : 262 : if (exclusive)
3958 : : return NULL;
3959 : 2 : continue;
3960 : : }
3961 : : }
3962 : 2378 : else if (exclusive)
3963 : : return NULL;
3964 : : else
3965 : 101 : continue;
3966 : :
3967 : 67539 : if (!first_use)
3968 : 73550 : first_use = use_stmt;
3969 : : }
3970 : :
3971 : : return first_use;
3972 : : }
3973 : :
3974 : : /* Handle a call to memcmp. We try to handle small comparisons by
3975 : : converting them to load and compare, and replacing the call to memcmp
3976 : : with a __builtin_memcmp_eq call where possible.
3977 : : return true when call is transformed, return false otherwise. */
3978 : :
3979 : : bool
3980 : 68082 : strlen_pass::handle_builtin_memcmp ()
3981 : : {
3982 : 68082 : gcall *stmt = as_a <gcall *> (gsi_stmt (m_gsi));
3983 : 68082 : tree res = gimple_call_lhs (stmt);
3984 : :
3985 : 68082 : if (!res || !use_in_zero_equality (res))
3986 : 2190 : return false;
3987 : :
3988 : 65892 : tree arg1 = gimple_call_arg (stmt, 0);
3989 : 65892 : tree arg2 = gimple_call_arg (stmt, 1);
3990 : 65892 : tree len = gimple_call_arg (stmt, 2);
3991 : 65892 : unsigned HOST_WIDE_INT leni;
3992 : :
3993 : 65892 : if (tree_fits_uhwi_p (len)
3994 : 90932 : && (leni = tree_to_uhwi (len)) <= GET_MODE_SIZE (word_mode)
3995 : 88615 : && pow2p_hwi (leni))
3996 : : {
3997 : 9469 : leni *= CHAR_TYPE_SIZE;
3998 : 9469 : unsigned align1 = get_pointer_alignment (arg1);
3999 : 9469 : unsigned align2 = get_pointer_alignment (arg2);
4000 : 9469 : unsigned align = MIN (align1, align2);
4001 : 9469 : scalar_int_mode mode;
4002 : 9469 : if (int_mode_for_size (leni, 1).exists (&mode)
4003 : 9469 : && (align >= leni || !targetm.slow_unaligned_access (mode, align)))
4004 : : {
4005 : 9469 : location_t loc = gimple_location (stmt);
4006 : 9469 : tree type, off;
4007 : 9469 : type = build_nonstandard_integer_type (leni, 1);
4008 : 18938 : gcc_assert (known_eq (GET_MODE_BITSIZE (TYPE_MODE (type)), leni));
4009 : 9469 : tree ptrtype = build_pointer_type_for_mode (char_type_node,
4010 : : ptr_mode, true);
4011 : 9469 : off = build_int_cst (ptrtype, 0);
4012 : 9469 : arg1 = build2_loc (loc, MEM_REF, type, arg1, off);
4013 : 9469 : arg2 = build2_loc (loc, MEM_REF, type, arg2, off);
4014 : 9469 : tree tem1 = fold_const_aggregate_ref (arg1);
4015 : 9469 : if (tem1)
4016 : 191 : arg1 = tem1;
4017 : 9469 : tree tem2 = fold_const_aggregate_ref (arg2);
4018 : 9469 : if (tem2)
4019 : 6587 : arg2 = tem2;
4020 : 9469 : res = fold_convert_loc (loc, TREE_TYPE (res),
4021 : : fold_build2_loc (loc, NE_EXPR,
4022 : : boolean_type_node,
4023 : : arg1, arg2));
4024 : 9469 : gimplify_and_update_call_from_tree (&m_gsi, res);
4025 : 9469 : return true;
4026 : : }
4027 : : }
4028 : :
4029 : 56423 : gimple_call_set_fndecl (stmt, builtin_decl_explicit (BUILT_IN_MEMCMP_EQ));
4030 : 56423 : return true;
4031 : : }
4032 : :
4033 : : /* Given strinfo IDX for ARG, sets LENRNG[] to the range of lengths
4034 : : of the string(s) referenced by ARG if it can be determined.
4035 : : If the length cannot be determined, sets *SIZE to the size of
4036 : : the array the string is stored in, if any. If no such array is
4037 : : known, sets *SIZE to -1. When the strings are nul-terminated sets
4038 : : *NULTERM to true, otherwise to false. When nonnull uses RVALS to
4039 : : determine range information. Returns true on success. */
4040 : :
4041 : : bool
4042 : 513079 : strlen_pass::get_len_or_size (gimple *stmt, tree arg, int idx,
4043 : : unsigned HOST_WIDE_INT lenrng[2],
4044 : : unsigned HOST_WIDE_INT *size, bool *nulterm)
4045 : : {
4046 : : /* Invalidate. */
4047 : 513079 : *size = HOST_WIDE_INT_M1U;
4048 : :
4049 : 513079 : if (idx < 0)
4050 : : {
4051 : : /* IDX is the inverted constant string length. */
4052 : 253535 : lenrng[0] = ~idx;
4053 : 253535 : lenrng[1] = lenrng[0];
4054 : 253535 : *nulterm = true;
4055 : 253535 : return true;
4056 : : }
4057 : :
4058 : : /* Set so that both LEN and ~LEN are invalid lengths, i.e., maximum
4059 : : possible length + 1. */
4060 : 259544 : lenrng[0] = lenrng[1] = HOST_WIDE_INT_MAX;
4061 : :
4062 : 259544 : if (strinfo *si = idx ? get_strinfo (idx) : NULL)
4063 : : {
4064 : : /* FIXME: Handle all this in_range_strlen_dynamic. */
4065 : 1076 : if (!si->nonzero_chars)
4066 : : ;
4067 : 1073 : else if (tree_fits_uhwi_p (si->nonzero_chars))
4068 : : {
4069 : 960 : lenrng[0] = tree_to_uhwi (si->nonzero_chars);
4070 : 960 : *nulterm = si->full_string_p;
4071 : : /* Set the upper bound only if the string is known to be
4072 : : nul-terminated, otherwise leave it at maximum + 1. */
4073 : 960 : if (*nulterm)
4074 : 606 : lenrng[1] = lenrng[0];
4075 : : }
4076 : 113 : else if (TREE_CODE (si->nonzero_chars) == SSA_NAME)
4077 : : {
4078 : 110 : int_range_max r;
4079 : 220 : if (get_range_query (cfun)->range_of_expr (r, si->nonzero_chars)
4080 : 110 : && !r.undefined_p ()
4081 : 220 : && !r.varying_p ())
4082 : : {
4083 : 110 : lenrng[0] = r.lower_bound ().to_uhwi ();
4084 : 110 : lenrng[1] = r.upper_bound ().to_uhwi ();
4085 : 110 : *nulterm = si->full_string_p;
4086 : : }
4087 : 110 : }
4088 : : }
4089 : :
4090 : 259544 : if (lenrng[0] != HOST_WIDE_INT_MAX)
4091 : : return true;
4092 : :
4093 : : /* Compute the minimum and maximum real or possible lengths. */
4094 : 258474 : c_strlen_data lendata = { };
4095 : : /* Set MAXBOUND to an arbitrary non-null non-integer node as a request
4096 : : to have it set to the length of the longest string in a PHI. */
4097 : 258474 : lendata.maxbound = arg;
4098 : 258474 : get_range_strlen_dynamic (arg, stmt, &lendata, ptr_qry);
4099 : :
4100 : 258474 : unsigned HOST_WIDE_INT maxbound = HOST_WIDE_INT_M1U;
4101 : 258474 : if (tree_fits_uhwi_p (lendata.maxbound)
4102 : 258474 : && !integer_all_onesp (lendata.maxbound))
4103 : 2274 : maxbound = tree_to_uhwi (lendata.maxbound);
4104 : :
4105 : 258474 : if (tree_fits_uhwi_p (lendata.minlen) && tree_fits_uhwi_p (lendata.maxlen))
4106 : : {
4107 : 258474 : unsigned HOST_WIDE_INT minlen = tree_to_uhwi (lendata.minlen);
4108 : 258474 : unsigned HOST_WIDE_INT maxlen = tree_to_uhwi (lendata.maxlen);
4109 : :
4110 : : /* The longest string in this data model. */
4111 : 258474 : const unsigned HOST_WIDE_INT lenmax
4112 : 258474 : = tree_to_uhwi (max_object_size ()) - 2;
4113 : :
4114 : 258474 : if (maxbound == HOST_WIDE_INT_M1U)
4115 : : {
4116 : 256200 : lenrng[0] = minlen;
4117 : 256200 : lenrng[1] = maxlen;
4118 : 256200 : *nulterm = minlen == maxlen;
4119 : : }
4120 : 2274 : else if (maxlen < lenmax)
4121 : : {
4122 : 1833 : *size = maxbound + 1;
4123 : 1833 : *nulterm = false;
4124 : : }
4125 : : else
4126 : : return false;
4127 : :
4128 : 258033 : return true;
4129 : : }
4130 : :
4131 : 0 : if (maxbound != HOST_WIDE_INT_M1U
4132 : 0 : && lendata.maxlen
4133 : 0 : && !integer_all_onesp (lendata.maxlen))
4134 : : {
4135 : : /* Set *SIZE to LENDATA.MAXBOUND which is a conservative estimate
4136 : : of the longest string based on the sizes of the arrays referenced
4137 : : by ARG. */
4138 : 0 : *size = maxbound + 1;
4139 : 0 : *nulterm = false;
4140 : 0 : return true;
4141 : : }
4142 : :
4143 : : return false;
4144 : : }
4145 : :
4146 : : /* If IDX1 and IDX2 refer to strings A and B of unequal lengths, return
4147 : : the result of 0 == strncmp (A, B, BOUND) (which is the same as strcmp
4148 : : for a sufficiently large BOUND). If the result is based on the length
4149 : : of one string being greater than the longest string that would fit in
4150 : : the array pointer to by the argument, set *PLEN and *PSIZE to
4151 : : the corresponding length (or its complement when the string is known
4152 : : to be at least as long and need not be nul-terminated) and size.
4153 : : Otherwise return null. */
4154 : :
4155 : : tree
4156 : 129054 : strlen_pass::strxcmp_eqz_result (gimple *stmt, tree arg1, int idx1,
4157 : : tree arg2, int idx2,
4158 : : unsigned HOST_WIDE_INT bound,
4159 : : unsigned HOST_WIDE_INT len[2],
4160 : : unsigned HOST_WIDE_INT *psize)
4161 : : {
4162 : : /* Determine the range the length of each string is in and whether it's
4163 : : known to be nul-terminated, or the size of the array it's stored in. */
4164 : 129054 : bool nul1, nul2;
4165 : 129054 : unsigned HOST_WIDE_INT siz1, siz2;
4166 : 129054 : unsigned HOST_WIDE_INT len1rng[2], len2rng[2];
4167 : 129054 : if (!get_len_or_size (stmt, arg1, idx1, len1rng, &siz1, &nul1)
4168 : 129054 : || !get_len_or_size (stmt, arg2, idx2, len2rng, &siz2, &nul2))
4169 : 281 : return NULL_TREE;
4170 : :
4171 : : /* BOUND is set to HWI_M1U for strcmp and less to strncmp, and LENiRNG
4172 : : to HWI_MAX when invalid. Adjust the length of each string to consider
4173 : : to be no more than BOUND. */
4174 : 128773 : if (len1rng[0] < HOST_WIDE_INT_MAX && len1rng[0] > bound)
4175 : 33 : len1rng[0] = bound;
4176 : 128773 : if (len1rng[1] < HOST_WIDE_INT_MAX && len1rng[1] > bound)
4177 : 66 : len1rng[1] = bound;
4178 : 128773 : if (len2rng[0] < HOST_WIDE_INT_MAX && len2rng[0] > bound)
4179 : 109 : len2rng[0] = bound;
4180 : 128773 : if (len2rng[1] < HOST_WIDE_INT_MAX && len2rng[1] > bound)
4181 : 109 : len2rng[1] = bound;
4182 : :
4183 : : /* Two empty strings are equal. */
4184 : 128773 : if (len1rng[1] == 0 && len2rng[1] == 0)
4185 : 3 : return integer_one_node;
4186 : :
4187 : : /* The strings are definitely unequal when the lower bound of the length
4188 : : of one of them is greater than the length of the longest string that
4189 : : would fit into the other array. */
4190 : 128770 : if (len1rng[0] == HOST_WIDE_INT_MAX
4191 : 799 : && len2rng[0] != HOST_WIDE_INT_MAX
4192 : 692 : && ((len2rng[0] < bound && len2rng[0] >= siz1)
4193 : 619 : || len2rng[0] > siz1))
4194 : : {
4195 : 99 : *psize = siz1;
4196 : 99 : len[0] = len1rng[0];
4197 : : /* Set LEN[0] to the lower bound of ARG1's length when it's
4198 : : nul-terminated or to the complement of its minimum length
4199 : : otherwise, */
4200 : 99 : len[1] = nul2 ? len2rng[0] : ~len2rng[0];
4201 : 99 : return integer_zero_node;
4202 : : }
4203 : :
4204 : 128671 : if (len2rng[0] == HOST_WIDE_INT_MAX
4205 : 353 : && len1rng[0] != HOST_WIDE_INT_MAX
4206 : 246 : && ((len1rng[0] < bound && len1rng[0] >= siz2)
4207 : 186 : || len1rng[0] > siz2))
4208 : : {
4209 : 82 : *psize = siz2;
4210 : 82 : len[0] = nul1 ? len1rng[0] : ~len1rng[0];
4211 : 82 : len[1] = len2rng[0];
4212 : 82 : return integer_zero_node;
4213 : : }
4214 : :
4215 : : /* The strings are also definitely unequal when their lengths are unequal
4216 : : and at least one is nul-terminated. */
4217 : 128589 : if (len1rng[0] != HOST_WIDE_INT_MAX
4218 : 127889 : && len2rng[0] != HOST_WIDE_INT_MAX
4219 : 127725 : && ((len1rng[1] < len2rng[0] && nul1)
4220 : 127706 : || (len2rng[1] < len1rng[0] && nul2)))
4221 : : {
4222 : 28 : if (bound <= len1rng[0] || bound <= len2rng[0])
4223 : 6 : *psize = bound;
4224 : : else
4225 : 22 : *psize = HOST_WIDE_INT_M1U;
4226 : :
4227 : 28 : len[0] = len1rng[0];
4228 : 28 : len[1] = len2rng[0];
4229 : 28 : return integer_zero_node;
4230 : : }
4231 : :
4232 : : /* The string lengths may be equal or unequal. Even when equal and
4233 : : both strings nul-terminated, without the string contents there's
4234 : : no way to determine whether they are equal. */
4235 : : return NULL_TREE;
4236 : : }
4237 : :
4238 : : /* Diagnose pointless calls to strcmp or strncmp STMT with string
4239 : : arguments of lengths LEN or size SIZ and (for strncmp) BOUND,
4240 : : whose result is used in equality expressions that evaluate to
4241 : : a constant due to one argument being longer than the size of
4242 : : the other. */
4243 : :
4244 : : static void
4245 : 209 : maybe_warn_pointless_strcmp (gimple *stmt, HOST_WIDE_INT bound,
4246 : : unsigned HOST_WIDE_INT len[2],
4247 : : unsigned HOST_WIDE_INT siz)
4248 : : {
4249 : 209 : tree lhs = gimple_call_lhs (stmt);
4250 : 209 : gimple *use = use_in_zero_equality (lhs, /* exclusive = */ false);
4251 : 209 : if (!use)
4252 : : return;
4253 : :
4254 : 102 : bool at_least = false;
4255 : :
4256 : : /* Excessive LEN[i] indicates a lower bound. */
4257 : 102 : if (len[0] > HOST_WIDE_INT_MAX)
4258 : : {
4259 : 3 : at_least = true;
4260 : 3 : len[0] = ~len[0];
4261 : : }
4262 : :
4263 : 102 : if (len[1] > HOST_WIDE_INT_MAX)
4264 : : {
4265 : 0 : at_least = true;
4266 : 0 : len[1] = ~len[1];
4267 : : }
4268 : :
4269 : 102 : unsigned HOST_WIDE_INT minlen = MIN (len[0], len[1]);
4270 : :
4271 : : /* FIXME: Include a note pointing to the declaration of the smaller
4272 : : array. */
4273 : 102 : location_t stmt_loc = gimple_or_expr_nonartificial_location (stmt, lhs);
4274 : :
4275 : 102 : tree callee = gimple_call_fndecl (stmt);
4276 : 102 : bool warned = false;
4277 : 102 : if (siz <= minlen && bound == -1)
4278 : 119 : warned = warning_at (stmt_loc, OPT_Wstring_compare,
4279 : : (at_least
4280 : : ? G_("%qD of a string of length %wu or more and "
4281 : : "an array of size %wu evaluates to nonzero")
4282 : : : G_("%qD of a string of length %wu and an array "
4283 : : "of size %wu evaluates to nonzero")),
4284 : : callee, minlen, siz);
4285 : 41 : else if (!at_least && siz <= HOST_WIDE_INT_MAX)
4286 : : {
4287 : 21 : if (len[0] != HOST_WIDE_INT_MAX && len[1] != HOST_WIDE_INT_MAX)
4288 : 6 : warned = warning_at (stmt_loc, OPT_Wstring_compare,
4289 : : "%qD of strings of length %wu and %wu "
4290 : : "and bound of %wu evaluates to nonzero",
4291 : : callee, len[0], len[1], bound);
4292 : : else
4293 : 15 : warned = warning_at (stmt_loc, OPT_Wstring_compare,
4294 : : "%qD of a string of length %wu, an array "
4295 : : "of size %wu and bound of %wu evaluates to "
4296 : : "nonzero",
4297 : : callee, minlen, siz, bound);
4298 : : }
4299 : :
4300 : 82 : if (!warned)
4301 : 44 : return;
4302 : :
4303 : 58 : location_t use_loc = gimple_location (use);
4304 : 58 : if (LOCATION_LINE (stmt_loc) != LOCATION_LINE (use_loc))
4305 : 12 : inform (use_loc, "in this expression");
4306 : : }
4307 : :
4308 : :
4309 : : /* Optimize a call to strcmp or strncmp either by folding it to a constant
4310 : : when possible or by transforming the latter to the former. Warn about
4311 : : calls where the length of one argument is greater than the size of
4312 : : the array to which the other argument points if the latter's length
4313 : : is not known. Return true when the call has been transformed into
4314 : : another and false otherwise. */
4315 : :
4316 : : bool
4317 : 129563 : strlen_pass::handle_builtin_string_cmp ()
4318 : : {
4319 : 129563 : gcall *stmt = as_a <gcall *> (gsi_stmt (m_gsi));
4320 : 129563 : tree lhs = gimple_call_lhs (stmt);
4321 : :
4322 : 129563 : if (!lhs)
4323 : : return false;
4324 : :
4325 : 129563 : tree arg1 = gimple_call_arg (stmt, 0);
4326 : 129563 : tree arg2 = gimple_call_arg (stmt, 1);
4327 : 129563 : int idx1 = get_stridx (arg1, stmt);
4328 : 129563 : int idx2 = get_stridx (arg2, stmt);
4329 : :
4330 : : /* For strncmp set to the value of the third argument if known. */
4331 : 129563 : HOST_WIDE_INT bound = -1;
4332 : 129563 : tree len = NULL_TREE;
4333 : : /* Extract the strncmp bound. */
4334 : 129563 : if (gimple_call_num_args (stmt) == 3)
4335 : : {
4336 : 1767 : len = gimple_call_arg (stmt, 2);
4337 : 1767 : if (tree_fits_shwi_p (len))
4338 : 1265 : bound = tree_to_shwi (len);
4339 : :
4340 : : /* If the bound argument is NOT known, do nothing. */
4341 : 1265 : if (bound < 0)
4342 : : return false;
4343 : : }
4344 : :
4345 : : /* Avoid folding if either argument is not a nul-terminated array.
4346 : : Defer warning until later. */
4347 : 129061 : if (!check_nul_terminated_array (NULL_TREE, arg1, len)
4348 : 129061 : || !check_nul_terminated_array (NULL_TREE, arg2, len))
4349 : 7 : return false;
4350 : :
4351 : 129054 : {
4352 : : /* Set to the length of one argument (or its complement if it's
4353 : : the lower bound of a range) and the size of the array storing
4354 : : the other if the result is based on the former being equal to
4355 : : or greater than the latter. */
4356 : 129054 : unsigned HOST_WIDE_INT len[2] = { HOST_WIDE_INT_MAX, HOST_WIDE_INT_MAX };
4357 : 129054 : unsigned HOST_WIDE_INT siz = HOST_WIDE_INT_M1U;
4358 : :
4359 : : /* Try to determine if the two strings are either definitely equal
4360 : : or definitely unequal and if so, either fold the result to zero
4361 : : (when equal) or set the range of the result to ~[0, 0] otherwise. */
4362 : 129054 : if (tree eqz = strxcmp_eqz_result (stmt, arg1, idx1, arg2, idx2, bound,
4363 : : len, &siz))
4364 : : {
4365 : 212 : if (integer_zerop (eqz))
4366 : : {
4367 : 209 : maybe_warn_pointless_strcmp (stmt, bound, len, siz);
4368 : :
4369 : : /* When the lengths of the first two string arguments are
4370 : : known to be unequal set the range of the result to non-zero.
4371 : : This allows the call to be eliminated if its result is only
4372 : : used in tests for equality to zero. */
4373 : 209 : int_range_max nz;
4374 : 209 : nz.set_nonzero (TREE_TYPE (lhs));
4375 : 209 : set_range_info (lhs, nz);
4376 : 209 : return false;
4377 : 209 : }
4378 : : /* When the two strings are definitely equal (such as when they
4379 : : are both empty) fold the call to the constant result. */
4380 : 3 : replace_call_with_value (&m_gsi, integer_zero_node);
4381 : 3 : return true;
4382 : : }
4383 : : }
4384 : :
4385 : : /* Return if nothing is known about the strings pointed to by ARG1
4386 : : and ARG2. */
4387 : 128842 : if (idx1 == 0 && idx2 == 0)
4388 : : return false;
4389 : :
4390 : : /* Determine either the length or the size of each of the strings,
4391 : : whichever is available. */
4392 : 127682 : HOST_WIDE_INT cstlen1 = -1, cstlen2 = -1;
4393 : 127682 : HOST_WIDE_INT arysiz1 = -1, arysiz2 = -1;
4394 : :
4395 : 127682 : {
4396 : 127682 : unsigned HOST_WIDE_INT len1rng[2], len2rng[2];
4397 : 127682 : unsigned HOST_WIDE_INT arsz1, arsz2;
4398 : 127682 : bool nulterm[2];
4399 : :
4400 : 127682 : if (!get_len_or_size (stmt, arg1, idx1, len1rng, &arsz1, nulterm)
4401 : 127682 : || !get_len_or_size (stmt, arg2, idx2, len2rng, &arsz2, nulterm + 1))
4402 : 160 : return false;
4403 : :
4404 : 127522 : if (len1rng[0] == len1rng[1] && len1rng[0] < HOST_WIDE_INT_MAX)
4405 : 502 : cstlen1 = len1rng[0];
4406 : 127020 : else if (arsz1 < HOST_WIDE_INT_M1U)
4407 : 543 : arysiz1 = arsz1;
4408 : :
4409 : 127522 : if (len2rng[0] == len2rng[1] && len2rng[0] < HOST_WIDE_INT_MAX)
4410 : 126446 : cstlen2 = len2rng[0];
4411 : 1076 : else if (arsz2 < HOST_WIDE_INT_M1U)
4412 : 130 : arysiz2 = arsz2;
4413 : : }
4414 : :
4415 : : /* Bail if neither the string length nor the size of the array
4416 : : it is stored in can be determined. */
4417 : 127522 : if ((cstlen1 < 0 && arysiz1 < 0)
4418 : 1045 : || (cstlen2 < 0 && arysiz2 < 0)
4419 : 584 : || (cstlen1 < 0 && cstlen2 < 0))
4420 : : return false;
4421 : :
4422 : 584 : if (cstlen1 >= 0)
4423 : 366 : ++cstlen1;
4424 : 584 : if (cstlen2 >= 0)
4425 : 466 : ++cstlen2;
4426 : :
4427 : : /* The exact number of characters to compare. */
4428 : 584 : HOST_WIDE_INT cmpsiz;
4429 : 584 : if (cstlen1 >= 0 && cstlen2 >= 0)
4430 : 248 : cmpsiz = MIN (cstlen1, cstlen2);
4431 : 336 : else if (cstlen1 >= 0)
4432 : : cmpsiz = cstlen1;
4433 : : else
4434 : 218 : cmpsiz = cstlen2;
4435 : 584 : if (bound >= 0)
4436 : 106 : cmpsiz = MIN (cmpsiz, bound);
4437 : : /* The size of the array in which the unknown string is stored. */
4438 : 584 : HOST_WIDE_INT varsiz = arysiz1 < 0 ? arysiz2 : arysiz1;
4439 : :
4440 : 584 : if ((varsiz < 0 || cmpsiz < varsiz) && use_in_zero_equality (lhs))
4441 : : {
4442 : : /* If the known length is less than the size of the other array
4443 : : and the strcmp result is only used to test equality to zero,
4444 : : transform the call to the equivalent _eq call. */
4445 : 452 : if (tree fn = builtin_decl_implicit (bound < 0 ? BUILT_IN_STRCMP_EQ
4446 : : : BUILT_IN_STRNCMP_EQ))
4447 : : {
4448 : 416 : tree n = build_int_cst (size_type_node, cmpsiz);
4449 : 416 : update_gimple_call (&m_gsi, fn, 3, arg1, arg2, n);
4450 : 416 : return true;
4451 : : }
4452 : : }
4453 : :
4454 : : return false;
4455 : : }
4456 : :
4457 : : /* Handle a POINTER_PLUS_EXPR statement.
4458 : : For p = "abcd" + 2; compute associated length, or if
4459 : : p = q + off is pointing to a '\0' character of a string, call
4460 : : zero_length_string on it. */
4461 : :
4462 : : void
4463 : 654010 : strlen_pass::handle_pointer_plus ()
4464 : : {
4465 : 654010 : gimple *stmt = gsi_stmt (m_gsi);
4466 : 654010 : tree lhs = gimple_assign_lhs (stmt), off;
4467 : 654010 : int idx = get_stridx (gimple_assign_rhs1 (stmt), stmt);
4468 : 654010 : strinfo *si, *zsi;
4469 : :
4470 : 654010 : if (idx == 0)
4471 : : return;
4472 : :
4473 : 60033 : if (idx < 0)
4474 : : {
4475 : 3738 : tree off = gimple_assign_rhs2 (stmt);
4476 : 3738 : if (tree_fits_uhwi_p (off)
4477 : 16 : && tree_to_uhwi (off) <= (unsigned HOST_WIDE_INT) ~idx)
4478 : 16 : ssa_ver_to_stridx[SSA_NAME_VERSION (lhs)]
4479 : 32 : = ~(~idx - (int) tree_to_uhwi (off));
4480 : 3738 : return;
4481 : : }
4482 : :
4483 : 56295 : si = get_strinfo (idx);
4484 : 56295 : if (si == NULL || si->nonzero_chars == NULL_TREE)
4485 : : return;
4486 : :
4487 : 6770 : off = gimple_assign_rhs2 (stmt);
4488 : 6770 : zsi = NULL;
4489 : 6770 : if (si->full_string_p && operand_equal_p (si->nonzero_chars, off, 0))
4490 : 1519 : zsi = zero_length_string (lhs, si);
4491 : 5251 : else if (TREE_CODE (off) == SSA_NAME)
4492 : : {
4493 : 2217 : gimple *def_stmt = SSA_NAME_DEF_STMT (off);
4494 : 2217 : if (gimple_assign_single_p (def_stmt)
4495 : 266 : && si->full_string_p
4496 : 2480 : && operand_equal_p (si->nonzero_chars,
4497 : 263 : gimple_assign_rhs1 (def_stmt), 0))
4498 : 230 : zsi = zero_length_string (lhs, si);
4499 : : }
4500 : 1749 : if (zsi != NULL
4501 : 1749 : && si->endptr != NULL_TREE
4502 : 1713 : && si->endptr != lhs
4503 : 212 : && TREE_CODE (si->endptr) == SSA_NAME)
4504 : : {
4505 : 212 : enum tree_code rhs_code
4506 : 212 : = useless_type_conversion_p (TREE_TYPE (lhs), TREE_TYPE (si->endptr))
4507 : 212 : ? SSA_NAME : NOP_EXPR;
4508 : 212 : gimple_assign_set_rhs_with_ops (&m_gsi, rhs_code, si->endptr);
4509 : 212 : gcc_assert (gsi_stmt (m_gsi) == stmt);
4510 : 212 : update_stmt (stmt);
4511 : : }
4512 : : }
4513 : :
4514 : : /* Set LENRANGE to the number of nonzero bytes for a store of TYPE and
4515 : : clear all flags. Return true on success and false on failure. */
4516 : :
4517 : : static bool
4518 : 78243 : nonzero_bytes_for_type (tree type, unsigned lenrange[3],
4519 : : bool *nulterm, bool *allnul, bool *allnonnul)
4520 : : {
4521 : : /* Use the size of the type of the expression as the size of the store,
4522 : : and set the upper bound of the length range to that of the size.
4523 : : Nothing is known about the contents so clear all flags. */
4524 : 78243 : tree typesize = TYPE_SIZE_UNIT (type);
4525 : 78243 : if (!type)
4526 : : return false;
4527 : :
4528 : 78243 : if (!tree_fits_uhwi_p (typesize))
4529 : : return false;
4530 : :
4531 : 78230 : unsigned HOST_WIDE_INT sz = tree_to_uhwi (typesize);
4532 : 78230 : if (sz > UINT_MAX)
4533 : : return false;
4534 : :
4535 : 78230 : lenrange[2] = sz;
4536 : 78230 : lenrange[1] = lenrange[2] ? lenrange[2] - 1 : 0;
4537 : 78230 : lenrange[0] = 0;
4538 : 78230 : *nulterm = false;
4539 : 78230 : *allnul = false;
4540 : 78230 : *allnonnul = false;
4541 : 78230 : return true;
4542 : : }
4543 : :
4544 : : /* Recursively determine the minimum and maximum number of leading nonzero
4545 : : bytes in the representation of EXP at memory state VUSE and set
4546 : : LENRANGE[0] and LENRANGE[1] to each.
4547 : : Sets LENRANGE[2] to the total size of the access (which may be less
4548 : : than LENRANGE[1] when what's being referenced by EXP is a pointer
4549 : : rather than an array).
4550 : : Sets *NULTERM if the representation contains a zero byte, sets *ALLNUL
4551 : : if all the bytes are zero, and *ALLNONNUL is all are nonzero.
4552 : : OFFSET and NBYTES are the offset into the representation and
4553 : : the size of the access to it determined from an ADDR_EXPR (i.e.,
4554 : : a pointer) or MEM_REF or zero for other expressions.
4555 : : Uses RVALS to determine range information.
4556 : : Avoids recursing deeper than the limits in SNLIM allow.
4557 : : Returns true on success and false otherwise. */
4558 : :
4559 : : bool
4560 : 1276424 : strlen_pass::count_nonzero_bytes (tree exp, tree vuse, gimple *stmt,
4561 : : unsigned HOST_WIDE_INT offset,
4562 : : unsigned HOST_WIDE_INT nbytes,
4563 : : unsigned lenrange[3], bool *nulterm,
4564 : : bool *allnul, bool *allnonnul,
4565 : : ssa_name_limit_t &snlim)
4566 : : {
4567 : 1290142 : if (TREE_CODE (exp) == SSA_NAME)
4568 : : {
4569 : : /* Handle non-zero single-character stores specially. */
4570 : 175846 : tree type = TREE_TYPE (exp);
4571 : 175846 : if (TREE_CODE (type) == INTEGER_TYPE
4572 : 167251 : && TYPE_MODE (type) == TYPE_MODE (char_type_node)
4573 : 161543 : && TYPE_PRECISION (type) == TYPE_PRECISION (char_type_node)
4574 : 337389 : && tree_expr_nonzero_p (exp))
4575 : : {
4576 : : /* If the character EXP is known to be non-zero (even if its
4577 : : exact value is not known) recurse once to set the range
4578 : : for an arbitrary constant. */
4579 : 13718 : exp = build_int_cst (type, 1);
4580 : 13718 : return count_nonzero_bytes (exp, vuse, stmt,
4581 : : offset, 1, lenrange,
4582 : 13718 : nulterm, allnul, allnonnul, snlim);
4583 : : }
4584 : :
4585 : 162128 : gimple *g = SSA_NAME_DEF_STMT (exp);
4586 : 162128 : if (gimple_assign_single_p (g))
4587 : : {
4588 : 66848 : exp = gimple_assign_rhs1 (g);
4589 : 66848 : if (!DECL_P (exp)
4590 : 66595 : && TREE_CODE (exp) != CONSTRUCTOR
4591 : 65210 : && TREE_CODE (exp) != MEM_REF)
4592 : : return false;
4593 : : /* Handle DECLs, CONSTRUCTOR and MEM_REF below. */
4594 : : stmt = g;
4595 : : }
4596 : 95280 : else if (gimple_code (g) == GIMPLE_PHI)
4597 : : {
4598 : : /* Avoid processing an SSA_NAME that has already been visited
4599 : : or if an SSA_NAME limit has been reached. Indicate success
4600 : : if the former and failure if the latter. */
4601 : 48011 : if (int res = snlim.next_phi (exp))
4602 : 22051 : return res > 0;
4603 : :
4604 : : /* Determine the minimum and maximum from the PHI arguments. */
4605 : 25960 : unsigned int n = gimple_phi_num_args (g);
4606 : 93156 : for (unsigned i = 0; i != n; i++)
4607 : : {
4608 : 75069 : tree def = gimple_phi_arg_def (g, i);
4609 : 75069 : if (!count_nonzero_bytes (def, vuse, g,
4610 : : offset, nbytes, lenrange, nulterm,
4611 : : allnul, allnonnul, snlim))
4612 : : return false;
4613 : : }
4614 : :
4615 : : return true;
4616 : : }
4617 : : }
4618 : :
4619 : 1198394 : if (TREE_CODE (exp) == CONSTRUCTOR)
4620 : : {
4621 : 44179 : if (nbytes)
4622 : : /* If NBYTES has already been determined by an outer MEM_REF
4623 : : fail rather than overwriting it (this shouldn't happen). */
4624 : : return false;
4625 : :
4626 : 44179 : tree type = TREE_TYPE (exp);
4627 : 44179 : tree size = TYPE_SIZE_UNIT (type);
4628 : 44179 : if (!size || !tree_fits_uhwi_p (size))
4629 : : return false;
4630 : :
4631 : 44179 : unsigned HOST_WIDE_INT byte_size = tree_to_uhwi (size);
4632 : 44179 : if (byte_size <= offset)
4633 : : return false;
4634 : :
4635 : 44179 : nbytes = byte_size - offset;
4636 : : }
4637 : :
4638 : 1198394 : if (TREE_CODE (exp) == MEM_REF)
4639 : : {
4640 : 604280 : if (nbytes)
4641 : : return false;
4642 : :
4643 : 603842 : tree arg = TREE_OPERAND (exp, 0);
4644 : 603842 : tree off = TREE_OPERAND (exp, 1);
4645 : :
4646 : 603842 : if (TREE_CODE (off) != INTEGER_CST || !tree_fits_uhwi_p (off))
4647 : : return false;
4648 : :
4649 : 603842 : unsigned HOST_WIDE_INT wioff = tree_to_uhwi (off);
4650 : 603842 : if (INT_MAX < wioff)
4651 : : return false;
4652 : :
4653 : 564601 : offset += wioff;
4654 : 564601 : if (INT_MAX < offset)
4655 : : return false;
4656 : :
4657 : : /* The size of the MEM_REF access determines the number of bytes. */
4658 : 564601 : tree type = TREE_TYPE (exp);
4659 : 564601 : tree typesize = TYPE_SIZE_UNIT (type);
4660 : 564601 : if (!typesize || !tree_fits_uhwi_p (typesize))
4661 : : return false;
4662 : 564601 : nbytes = tree_to_uhwi (typesize);
4663 : 564601 : if (!nbytes)
4664 : : return false;
4665 : :
4666 : : /* Handle MEM_REF = SSA_NAME types of assignments. */
4667 : 564601 : return count_nonzero_bytes_addr (arg, vuse, stmt,
4668 : : offset, nbytes, lenrange, nulterm,
4669 : 564601 : allnul, allnonnul, snlim);
4670 : : }
4671 : :
4672 : 594114 : if (VAR_P (exp) || TREE_CODE (exp) == CONST_DECL)
4673 : : {
4674 : : /* If EXP can be folded into a constant use the result. Otherwise
4675 : : proceed to use EXP to determine a range of the result. */
4676 : 299565 : if (tree fold_exp = ctor_for_folding (exp))
4677 : 299563 : if (fold_exp != error_mark_node)
4678 : 594114 : exp = fold_exp;
4679 : : }
4680 : :
4681 : 594114 : const char *prep = NULL;
4682 : 594114 : if (TREE_CODE (exp) == STRING_CST)
4683 : : {
4684 : 11204 : unsigned nchars = TREE_STRING_LENGTH (exp);
4685 : 11204 : if (nchars <= offset)
4686 : : return false;
4687 : :
4688 : 11198 : if (!nbytes)
4689 : : /* If NBYTES hasn't been determined earlier, either from ADDR_EXPR
4690 : : (i.e., it's the size of a pointer), or from MEM_REF (as the size
4691 : : of the access), set it here to the size of the string, including
4692 : : all internal and trailing nuls if the string has any. */
4693 : 7729 : nbytes = nchars - offset;
4694 : 3469 : else if (nchars - offset < nbytes)
4695 : : return false;
4696 : :
4697 : 11195 : prep = TREE_STRING_POINTER (exp) + offset;
4698 : : }
4699 : :
4700 : 594105 : unsigned char buf[256];
4701 : 11195 : if (!prep)
4702 : : {
4703 : 582910 : if (CHAR_BIT != 8 || BITS_PER_UNIT != 8 || offset > INT_MAX)
4704 : : return false;
4705 : : /* If the pointer to representation hasn't been set above
4706 : : for STRING_CST point it at the buffer. */
4707 : 582910 : prep = reinterpret_cast <char *>(buf);
4708 : : /* Try to extract the representation of the constant object
4709 : : or expression starting from the offset. */
4710 : 582910 : unsigned repsize = native_encode_expr (exp, buf, sizeof buf, offset);
4711 : 582910 : if (repsize < nbytes)
4712 : : {
4713 : : /* Handle vector { 0x12345678, 0x23003412, x_1(D), y_2(D) }
4714 : : and similar cases. Even when not all the elements are constant,
4715 : : we can perhaps figure out something from the constant ones
4716 : : and assume the others can be anything. */
4717 : 308670 : if (TREE_CODE (exp) == CONSTRUCTOR
4718 : 4799 : && CONSTRUCTOR_NELTS (exp)
4719 : 3414 : && VECTOR_TYPE_P (TREE_TYPE (exp))
4720 : 310055 : && nbytes <= sizeof buf)
4721 : : {
4722 : 1385 : tree v0 = CONSTRUCTOR_ELT (exp, 0)->value;
4723 : 1385 : unsigned HOST_WIDE_INT elt_sz
4724 : 1385 : = int_size_in_bytes (TREE_TYPE (v0));
4725 : 1385 : unsigned int i, s = 0;
4726 : 1385 : tree v, idx;
4727 : 1465 : FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (exp), i, idx, v)
4728 : : {
4729 : 1465 : if (idx
4730 : 1465 : && (VECTOR_TYPE_P (TREE_TYPE (v0))
4731 : 15 : || !tree_fits_uhwi_p (idx)
4732 : 15 : || tree_to_uhwi (idx) != i))
4733 : : {
4734 : : s = 0;
4735 : : break;
4736 : : }
4737 : 1465 : if ((i + 1) * elt_sz <= offset)
4738 : 0 : continue;
4739 : 1465 : unsigned int o = 0;
4740 : 1465 : if (i * elt_sz < offset)
4741 : 0 : o = offset % elt_sz;
4742 : 2930 : repsize = native_encode_expr (v, buf + s,
4743 : 1465 : sizeof (buf) - s, o);
4744 : 1465 : if (repsize != elt_sz - o)
4745 : : break;
4746 : 80 : s += repsize;
4747 : : }
4748 : 1385 : if (s != 0 && s < nbytes)
4749 : : {
4750 : 66 : unsigned HOST_WIDE_INT n = strnlen (prep, s);
4751 : 66 : if (n < lenrange[0])
4752 : 52 : lenrange[0] = n;
4753 : 66 : if (lenrange[1] < n && n != s)
4754 : 2 : lenrange[1] = n;
4755 : 66 : if (lenrange[2] < nbytes)
4756 : 52 : lenrange[2] = nbytes;
4757 : : /* We haven't processed all bytes, the rest are unknown.
4758 : : So, clear NULTERM if none of the initial bytes are
4759 : : zero, and clear ALLNUL and ALLNONNULL because we don't
4760 : : know about the remaining bytes. */
4761 : 66 : if (n == s)
4762 : 44 : *nulterm = false;
4763 : 66 : *allnul = false;
4764 : 66 : *allnonnul = false;
4765 : 66 : return true;
4766 : : }
4767 : 1319 : else if (s != nbytes)
4768 : : {
4769 : : /* See below. */
4770 : 1319 : lenrange[0] = 0;
4771 : 1319 : prep = NULL;
4772 : : }
4773 : : }
4774 : : else
4775 : : {
4776 : : /* This should only happen when REPSIZE is zero because EXP
4777 : : doesn't denote an object with a known initializer, except
4778 : : perhaps when the reference reads past its end. */
4779 : 307285 : lenrange[0] = 0;
4780 : 307285 : prep = NULL;
4781 : : }
4782 : : }
4783 : 274240 : else if (!nbytes)
4784 : : nbytes = repsize;
4785 : 55134 : else if (nbytes < repsize)
4786 : : return false;
4787 : : }
4788 : :
4789 : 594039 : if (!nbytes)
4790 : 49520 : return nonzero_bytes_for_type (TREE_TYPE (exp), lenrange,
4791 : 49520 : nulterm, allnul, allnonnul);
4792 : :
4793 : : /* Compute the number of leading nonzero bytes in the representation
4794 : : and update the minimum and maximum. */
4795 : 544519 : unsigned HOST_WIDE_INT n = prep ? strnlen (prep, nbytes) : nbytes;
4796 : :
4797 : 544519 : if (n < lenrange[0])
4798 : 224484 : lenrange[0] = n;
4799 : 544519 : if (lenrange[1] < n)
4800 : 419728 : lenrange[1] = n;
4801 : :
4802 : : /* Set the size of the representation. */
4803 : 544519 : if (lenrange[2] < nbytes)
4804 : 528338 : lenrange[2] = nbytes;
4805 : :
4806 : : /* Clear NULTERM if none of the bytes is zero. */
4807 : 544519 : if (n == nbytes)
4808 : 418230 : *nulterm = false;
4809 : :
4810 : 544519 : if (n)
4811 : : {
4812 : : /* When the initial number of non-zero bytes N is non-zero, reset
4813 : : *ALLNUL; if N is less than that the size of the representation
4814 : : also clear *ALLNONNUL. */
4815 : 427417 : *allnul = false;
4816 : 427417 : if (n < nbytes)
4817 : 9187 : *allnonnul = false;
4818 : : }
4819 : 117102 : else if (*allnul || *allnonnul)
4820 : : {
4821 : 113550 : *allnonnul = false;
4822 : :
4823 : 113550 : if (*allnul)
4824 : : {
4825 : : /* When either ALLNUL is set and N is zero, also determine
4826 : : whether all subsequent bytes after the first one (which
4827 : : is nul) are zero or nonzero and clear ALLNUL if not. */
4828 : 910830 : for (const char *p = prep; p != prep + nbytes; ++p)
4829 : 805903 : if (*p)
4830 : : {
4831 : 2270 : *allnul = false;
4832 : 2270 : break;
4833 : : }
4834 : : }
4835 : : }
4836 : :
4837 : : return true;
4838 : : }
4839 : :
4840 : : /* Like count_nonzero_bytes, but instead of counting bytes in EXP, count
4841 : : bytes that are pointed to by EXP, which should be a pointer. */
4842 : :
4843 : : bool
4844 : 709277 : strlen_pass::count_nonzero_bytes_addr (tree exp, tree vuse, gimple *stmt,
4845 : : unsigned HOST_WIDE_INT offset,
4846 : : unsigned HOST_WIDE_INT nbytes,
4847 : : unsigned lenrange[3], bool *nulterm,
4848 : : bool *allnul, bool *allnonnul,
4849 : : ssa_name_limit_t &snlim)
4850 : : {
4851 : 709277 : int idx = get_stridx (exp, stmt);
4852 : 709277 : if (idx > 0)
4853 : : {
4854 : : /* get_strinfo reflects string lengths before the current statement,
4855 : : where the current statement is the outermost count_nonzero_bytes
4856 : : stmt. If there are any stores in between stmt and that
4857 : : current statement, the string length information might describe
4858 : : something significantly different. */
4859 : 17452 : if (gimple_vuse (stmt) != vuse)
4860 : : return false;
4861 : :
4862 : 6992 : strinfo *si = get_strinfo (idx);
4863 : 6992 : if (!si)
4864 : : return false;
4865 : :
4866 : : /* Handle both constant lengths as well non-constant lengths
4867 : : in some range. */
4868 : 1505 : unsigned HOST_WIDE_INT minlen, maxlen;
4869 : 1505 : if (tree_fits_shwi_p (si->nonzero_chars))
4870 : 645 : minlen = maxlen = tree_to_shwi (si->nonzero_chars);
4871 : 860 : else if (si->nonzero_chars
4872 : 792 : && TREE_CODE (si->nonzero_chars) == SSA_NAME)
4873 : : {
4874 : 792 : int_range_max vr;
4875 : 792 : if (!ptr_qry.rvals->range_of_expr (vr, si->nonzero_chars, stmt)
4876 : 792 : || vr.undefined_p ()
4877 : 1575 : || vr.varying_p ())
4878 : 9 : return false;
4879 : :
4880 : 783 : minlen = vr.lower_bound ().to_uhwi ();
4881 : 783 : maxlen = vr.upper_bound ().to_uhwi ();
4882 : 792 : }
4883 : : else
4884 : : return false;
4885 : :
4886 : 1428 : if (maxlen < offset)
4887 : : return false;
4888 : :
4889 : 1355 : minlen = minlen < offset ? 0 : minlen - offset;
4890 : 1355 : maxlen -= offset;
4891 : 1355 : if (maxlen + 1 < nbytes)
4892 : : return false;
4893 : :
4894 : 1036 : if (nbytes <= minlen || !si->full_string_p)
4895 : 953 : *nulterm = false;
4896 : :
4897 : 1036 : if (nbytes < minlen)
4898 : : {
4899 : 102 : minlen = nbytes;
4900 : 102 : if (nbytes < maxlen)
4901 : : maxlen = nbytes;
4902 : : }
4903 : :
4904 : 1036 : if (!si->full_string_p)
4905 : 192 : maxlen = nbytes;
4906 : :
4907 : 1036 : if (minlen < lenrange[0])
4908 : 1036 : lenrange[0] = minlen;
4909 : 1036 : if (lenrange[1] < maxlen)
4910 : 1036 : lenrange[1] = maxlen;
4911 : :
4912 : 1036 : if (lenrange[2] < nbytes)
4913 : 1036 : lenrange[2] = nbytes;
4914 : :
4915 : : /* Since only the length of the string are known and not its contents,
4916 : : clear ALLNUL and ALLNONNUL purely on the basis of the length. */
4917 : 1036 : *allnul = false;
4918 : 1036 : if (minlen < nbytes)
4919 : 95 : *allnonnul = false;
4920 : :
4921 : 1036 : return true;
4922 : : }
4923 : :
4924 : 700551 : if (TREE_CODE (exp) == ADDR_EXPR)
4925 : 309820 : return count_nonzero_bytes (TREE_OPERAND (exp, 0), vuse, stmt,
4926 : : offset, nbytes,
4927 : 309820 : lenrange, nulterm, allnul, allnonnul, snlim);
4928 : :
4929 : 390731 : if (TREE_CODE (exp) == SSA_NAME)
4930 : : {
4931 : 389127 : gimple *g = SSA_NAME_DEF_STMT (exp);
4932 : 389127 : if (gimple_code (g) == GIMPLE_PHI)
4933 : : {
4934 : : /* Avoid processing an SSA_NAME that has already been visited
4935 : : or if an SSA_NAME limit has been reached. Indicate success
4936 : : if the former and failure if the latter. */
4937 : 87161 : if (int res = snlim.next_phi (exp))
4938 : 21623 : return res > 0;
4939 : :
4940 : : /* Determine the minimum and maximum from the PHI arguments. */
4941 : 65538 : unsigned int n = gimple_phi_num_args (g);
4942 : 205899 : for (unsigned i = 0; i != n; i++)
4943 : : {
4944 : 144676 : tree def = gimple_phi_arg_def (g, i);
4945 : 144676 : if (!count_nonzero_bytes_addr (def, vuse, g,
4946 : : offset, nbytes, lenrange,
4947 : : nulterm, allnul, allnonnul,
4948 : : snlim))
4949 : : return false;
4950 : : }
4951 : :
4952 : : return true;
4953 : : }
4954 : : }
4955 : :
4956 : : /* Otherwise we don't know anything. */
4957 : 303570 : lenrange[0] = 0;
4958 : 303570 : if (lenrange[1] < nbytes)
4959 : 246964 : lenrange[1] = nbytes;
4960 : 303570 : if (lenrange[2] < nbytes)
4961 : 245532 : lenrange[2] = nbytes;
4962 : 303570 : *nulterm = false;
4963 : 303570 : *allnul = false;
4964 : 303570 : *allnonnul = false;
4965 : 303570 : return true;
4966 : : }
4967 : :
4968 : : /* Same as above except with an implicit SSA_NAME limit. When EXPR_OR_TYPE
4969 : : is a type rather than an expression use its size to compute the range.
4970 : : RVALS is used to determine ranges of dynamically computed string lengths
4971 : : (the results of strlen). */
4972 : :
4973 : : bool
4974 : 920258 : strlen_pass::count_nonzero_bytes (tree expr_or_type, gimple *stmt,
4975 : : unsigned lenrange[3], bool *nulterm,
4976 : : bool *allnul, bool *allnonnul)
4977 : : {
4978 : 920258 : if (TYPE_P (expr_or_type))
4979 : 28723 : return nonzero_bytes_for_type (expr_or_type, lenrange,
4980 : 28723 : nulterm, allnul, allnonnul);
4981 : :
4982 : : /* Set to optimistic values so the caller doesn't have to worry about
4983 : : initializing these and to what. On success, the function will clear
4984 : : these if it determines their values are different but being recursive
4985 : : it never sets either to true. On failure, their values are
4986 : : unspecified. */
4987 : 891535 : *nulterm = true;
4988 : 891535 : *allnul = true;
4989 : 891535 : *allnonnul = true;
4990 : :
4991 : 891535 : ssa_name_limit_t snlim;
4992 : 891535 : tree expr = expr_or_type;
4993 : 1783070 : return count_nonzero_bytes (expr, gimple_vuse (stmt), stmt,
4994 : : 0, 0, lenrange, nulterm, allnul, allnonnul,
4995 : : snlim);
4996 : 891535 : }
4997 : :
4998 : : /* Handle a single or multibyte store other than by a built-in function,
4999 : : either via a single character assignment or by multi-byte assignment
5000 : : either via MEM_REF or via a type other than char (such as in
5001 : : '*(int*)a = 12345'). Return true to let the caller advance *GSI to
5002 : : the next statement in the basic block and false otherwise. */
5003 : :
5004 : : bool
5005 : 361041 : strlen_pass::handle_store (bool *zero_write)
5006 : : {
5007 : 361041 : gimple *stmt = gsi_stmt (m_gsi);
5008 : : /* The LHS and RHS of the store. The RHS is null if STMT is a function
5009 : : call. STORETYPE is the type of the store (determined from either
5010 : : the RHS of the assignment statement or the LHS of a function call. */
5011 : 361041 : tree lhs, rhs, storetype;
5012 : 361041 : if (is_gimple_assign (stmt))
5013 : : {
5014 : 332318 : lhs = gimple_assign_lhs (stmt);
5015 : 332318 : rhs = gimple_assign_rhs1 (stmt);
5016 : 332318 : storetype = TREE_TYPE (rhs);
5017 : : }
5018 : 28723 : else if (is_gimple_call (stmt))
5019 : : {
5020 : 28723 : lhs = gimple_call_lhs (stmt);
5021 : 28723 : rhs = NULL_TREE;
5022 : 28723 : storetype = TREE_TYPE (lhs);
5023 : : }
5024 : : else
5025 : : return true;
5026 : :
5027 : 361041 : tree ssaname = NULL_TREE;
5028 : 361041 : strinfo *si = NULL;
5029 : 361041 : int idx = -1;
5030 : :
5031 : 361041 : range_query *const rvals = ptr_qry.rvals;
5032 : :
5033 : : /* The offset of the first byte in LHS modified by the store. */
5034 : 361041 : unsigned HOST_WIDE_INT offset = 0;
5035 : :
5036 : 361041 : if (TREE_CODE (lhs) == MEM_REF
5037 : 361041 : && TREE_CODE (TREE_OPERAND (lhs, 0)) == SSA_NAME)
5038 : : {
5039 : 102871 : tree mem_offset = TREE_OPERAND (lhs, 1);
5040 : 102871 : if (tree_fits_uhwi_p (mem_offset))
5041 : : {
5042 : : /* Get the strinfo for the base, and use it if it starts with at
5043 : : least OFFSET nonzero characters. This is trivially true if
5044 : : OFFSET is zero. */
5045 : 102871 : offset = tree_to_uhwi (mem_offset);
5046 : 102871 : idx = get_stridx (TREE_OPERAND (lhs, 0), stmt);
5047 : 102871 : if (idx > 0)
5048 : 8002 : si = get_strinfo (idx);
5049 : 102871 : if (offset == 0)
5050 : 85735 : ssaname = TREE_OPERAND (lhs, 0);
5051 : 17136 : else if (si == NULL
5052 : 17136 : || compare_nonzero_chars (si, stmt, offset, rvals) < 0)
5053 : : {
5054 : 16668 : *zero_write = rhs ? initializer_zerop (rhs) : false;
5055 : :
5056 : 16668 : bool dummy;
5057 : 16668 : unsigned lenrange[] = { UINT_MAX, 0, 0 };
5058 : 16668 : if (count_nonzero_bytes (rhs ? rhs : storetype, stmt, lenrange,
5059 : : &dummy, &dummy, &dummy))
5060 : 14793 : maybe_warn_overflow (stmt, true, lenrange[2]);
5061 : :
5062 : 16668 : return true;
5063 : : }
5064 : : }
5065 : : }
5066 : : else
5067 : : {
5068 : 258170 : idx = get_addr_stridx (lhs, stmt, NULL_TREE, &offset, rvals);
5069 : 258170 : if (idx > 0)
5070 : 39134 : si = get_strinfo (idx);
5071 : : }
5072 : :
5073 : : /* Minimum and maximum leading non-zero bytes and the size of the store. */
5074 : 344373 : unsigned lenrange[] = { UINT_MAX, 0, 0 };
5075 : :
5076 : : /* Set to the minimum length of the string being assigned if known. */
5077 : 344373 : unsigned HOST_WIDE_INT rhs_minlen;
5078 : :
5079 : : /* STORING_NONZERO_P is true iff not all stored characters are zero.
5080 : : STORING_ALL_NONZERO_P is true if all stored characters are zero.
5081 : : STORING_ALL_ZEROS_P is true iff all stored characters are zero.
5082 : : Both are false when it's impossible to determine which is true. */
5083 : 344373 : bool storing_nonzero_p;
5084 : 344373 : bool storing_all_nonzero_p;
5085 : 344373 : bool storing_all_zeros_p;
5086 : : /* FULL_STRING_P is set when the stored sequence of characters form
5087 : : a nul-terminated string. */
5088 : 344373 : bool full_string_p;
5089 : :
5090 : 344373 : const bool ranges_valid
5091 : 373096 : = count_nonzero_bytes (rhs ? rhs : storetype, stmt,
5092 : : lenrange, &full_string_p,
5093 : : &storing_all_zeros_p, &storing_all_nonzero_p);
5094 : :
5095 : 344373 : if (ranges_valid)
5096 : : {
5097 : 312510 : rhs_minlen = lenrange[0];
5098 : 312510 : storing_nonzero_p = lenrange[1] > 0;
5099 : 312510 : *zero_write = storing_all_zeros_p;
5100 : :
5101 : 312510 : maybe_warn_overflow (stmt, true, lenrange[2]);
5102 : : }
5103 : : else
5104 : : {
5105 : 31863 : rhs_minlen = HOST_WIDE_INT_M1U;
5106 : 31863 : full_string_p = false;
5107 : 31863 : storing_nonzero_p = false;
5108 : 31863 : storing_all_zeros_p = false;
5109 : 31863 : storing_all_nonzero_p = false;
5110 : : }
5111 : :
5112 : 344373 : if (si != NULL)
5113 : : {
5114 : : /* The count_nonzero_bytes call above might have unshared si.
5115 : : Fetch it again from the vector. */
5116 : 20026 : si = get_strinfo (idx);
5117 : : /* The corresponding element is set to 1 if the first and last
5118 : : element, respectively, of the sequence of characters being
5119 : : written over the string described by SI ends before
5120 : : the terminating nul (if it has one), to zero if the nul is
5121 : : being overwritten but not beyond, or negative otherwise. */
5122 : 20026 : int store_before_nul[2];
5123 : 20026 : if (ranges_valid)
5124 : : {
5125 : : /* The offset of the last stored byte. */
5126 : 19217 : unsigned HOST_WIDE_INT endoff = offset + lenrange[2] - 1;
5127 : 19217 : store_before_nul[0]
5128 : 19217 : = compare_nonzero_chars (si, stmt, offset, rvals);
5129 : 19217 : if (endoff == offset)
5130 : : store_before_nul[1] = store_before_nul[0];
5131 : : else
5132 : 8044 : store_before_nul[1]
5133 : 8044 : = compare_nonzero_chars (si, stmt, endoff, rvals);
5134 : : }
5135 : : else
5136 : : {
5137 : 809 : store_before_nul[0]
5138 : 809 : = compare_nonzero_chars (si, stmt, offset, rvals);
5139 : 809 : store_before_nul[1] = store_before_nul[0];
5140 : 809 : gcc_assert (offset == 0 || store_before_nul[0] >= 0);
5141 : : }
5142 : :
5143 : 20026 : if (storing_all_zeros_p
5144 : 8902 : && store_before_nul[0] == 0
5145 : 8536 : && store_before_nul[1] == 0
5146 : 6175 : && si->full_string_p)
5147 : : {
5148 : : /* When overwriting a '\0' with a '\0', the store can be removed
5149 : : if we know it has been stored in the current function. */
5150 : 128 : if (!stmt_could_throw_p (cfun, stmt) && si->writable)
5151 : : {
5152 : 126 : unlink_stmt_vdef (stmt);
5153 : 126 : release_defs (stmt);
5154 : 126 : gsi_remove (&m_gsi, true);
5155 : 126 : return false;
5156 : : }
5157 : : else
5158 : : {
5159 : 2 : si->writable = true;
5160 : 2 : gsi_next (&m_gsi);
5161 : 2 : return false;
5162 : : }
5163 : : }
5164 : :
5165 : 19898 : if (store_before_nul[1] > 0
5166 : 1132 : && storing_nonzero_p
5167 : 923 : && lenrange[0] == lenrange[1]
5168 : 910 : && lenrange[0] == lenrange[2]
5169 : 906 : && TREE_CODE (storetype) == INTEGER_TYPE)
5170 : : {
5171 : : /* Handle a store of one or more non-nul characters that ends
5172 : : before the terminating nul of the destination and so does
5173 : : not affect its length
5174 : : If si->nonzero_chars > OFFSET, we aren't overwriting '\0',
5175 : : and if we aren't storing '\0', we know that the length of
5176 : : the string and any other zero terminated string in memory
5177 : : remains the same. In that case we move to the next gimple
5178 : : statement and return to signal the caller that it shouldn't
5179 : : invalidate anything.
5180 : :
5181 : : This is beneficial for cases like:
5182 : :
5183 : : char p[20];
5184 : : void foo (char *q)
5185 : : {
5186 : : strcpy (p, "foobar");
5187 : : size_t len = strlen (p); // can be folded to 6
5188 : : size_t len2 = strlen (q); // has to be computed
5189 : : p[0] = 'X';
5190 : : size_t len3 = strlen (p); // can be folded to 6
5191 : : size_t len4 = strlen (q); // can be folded to len2
5192 : : bar (len, len2, len3, len4);
5193 : : } */
5194 : 699 : gsi_next (&m_gsi);
5195 : 699 : return false;
5196 : : }
5197 : :
5198 : 18990 : if (storing_nonzero_p
5199 : 9919 : || storing_all_zeros_p
5200 : 1145 : || (full_string_p && lenrange[1] == 0)
5201 : 939 : || (offset != 0 && store_before_nul[1] > 0))
5202 : : {
5203 : : /* When STORING_NONZERO_P, we know that the string will start
5204 : : with at least OFFSET + 1 nonzero characters. If storing
5205 : : a single character, set si->NONZERO_CHARS to the result.
5206 : : If storing multiple characters, try to determine the number
5207 : : of leading non-zero characters and set si->NONZERO_CHARS to
5208 : : the result instead.
5209 : :
5210 : : When STORING_ALL_ZEROS_P, or the first byte written is zero,
5211 : : i.e. FULL_STRING_P && LENRANGE[1] == 0, we know that the
5212 : : string is now OFFSET characters long.
5213 : :
5214 : : Otherwise, we're storing an unknown value at offset OFFSET,
5215 : : so need to clip the nonzero_chars to OFFSET.
5216 : : Use the minimum length of the string (or individual character)
5217 : : being stored if it's known. Otherwise, STORING_NONZERO_P
5218 : : guarantees it's at least 1. */
5219 : 36534 : HOST_WIDE_INT len
5220 : 18267 : = storing_nonzero_p && ranges_valid ? lenrange[0] : 1;
5221 : 18267 : location_t loc = gimple_location (stmt);
5222 : 18267 : tree oldlen = si->nonzero_chars;
5223 : 18267 : if (store_before_nul[1] == 0 && si->full_string_p)
5224 : : /* We're overwriting the nul terminator with a nonzero or
5225 : : unknown character. If the previous stmt was a memcpy,
5226 : : its length may be decreased. */
5227 : 676 : adjust_last_stmt (si, stmt, false);
5228 : 18267 : si = unshare_strinfo (si);
5229 : 18267 : if (storing_nonzero_p)
5230 : : {
5231 : 9280 : gcc_assert (len >= 0);
5232 : 9280 : si->nonzero_chars = build_int_cst (size_type_node, offset + len);
5233 : : }
5234 : : else
5235 : 8987 : si->nonzero_chars = build_int_cst (size_type_node, offset);
5236 : :
5237 : : /* Set FULL_STRING_P only if the length of the strings being
5238 : : written is the same, and clear it if the strings have
5239 : : different lengths. In the latter case the length stored
5240 : : in si->NONZERO_CHARS becomes the lower bound.
5241 : : FIXME: Handle the upper bound of the length if possible. */
5242 : 18267 : si->full_string_p = full_string_p && lenrange[0] == lenrange[1];
5243 : :
5244 : 18267 : if (storing_all_zeros_p
5245 : 8774 : && ssaname
5246 : 18472 : && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ssaname))
5247 : 205 : si->endptr = ssaname;
5248 : : else
5249 : 18062 : si->endptr = NULL;
5250 : 18267 : si->next = 0;
5251 : 18267 : si->stmt = NULL;
5252 : 18267 : si->writable = true;
5253 : 18267 : si->dont_invalidate = true;
5254 : 18267 : if (oldlen)
5255 : : {
5256 : 16187 : tree adj = fold_build2_loc (loc, MINUS_EXPR, size_type_node,
5257 : : si->nonzero_chars, oldlen);
5258 : 16187 : adjust_related_strinfos (loc, si, adj);
5259 : : }
5260 : : else
5261 : 2080 : si->prev = 0;
5262 : : }
5263 : : }
5264 : 324347 : else if (idx == 0 && (storing_all_zeros_p || storing_nonzero_p))
5265 : : {
5266 : 207215 : if (ssaname)
5267 : 61650 : idx = new_stridx (ssaname);
5268 : : else
5269 : 145565 : idx = new_addr_stridx (lhs);
5270 : 207215 : if (idx != 0)
5271 : : {
5272 : 183195 : tree ptr = (ssaname ? ssaname : build_fold_addr_expr (lhs));
5273 : :
5274 : 183195 : HOST_WIDE_INT slen;
5275 : 183195 : if (storing_all_zeros_p)
5276 : : slen = 0;
5277 : 101609 : else if (storing_nonzero_p && ranges_valid)
5278 : : {
5279 : : /* FIXME: Handle the upper bound of the length when
5280 : : LENRANGE[0] != LENRANGE[1]. */
5281 : 101609 : slen = lenrange[0];
5282 : 101609 : if (lenrange[0] != lenrange[1])
5283 : : /* Set the minimum length but ignore the maximum
5284 : : for now. */
5285 : 33806 : full_string_p = false;
5286 : : }
5287 : : else
5288 : : slen = -1;
5289 : :
5290 : 101609 : tree len = (slen <= 0
5291 : 216933 : ? size_zero_node
5292 : 67871 : : build_int_cst (size_type_node, slen));
5293 : 183195 : si = new_strinfo (ptr, idx, len, slen >= 0 && full_string_p);
5294 : 183195 : set_strinfo (idx, si);
5295 : 183195 : if (storing_all_zeros_p
5296 : 81586 : && ssaname
5297 : 208217 : && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ssaname))
5298 : 25022 : si->endptr = ssaname;
5299 : 183195 : si->dont_invalidate = true;
5300 : 183195 : si->writable = true;
5301 : : }
5302 : : }
5303 : 91340 : else if (idx == 0
5304 : 91340 : && rhs_minlen < HOST_WIDE_INT_M1U
5305 : 62677 : && ssaname == NULL_TREE
5306 : 54945 : && TREE_CODE (TREE_TYPE (lhs)) == ARRAY_TYPE)
5307 : : {
5308 : 497 : HOST_WIDE_INT a = int_size_in_bytes (TREE_TYPE (lhs));
5309 : 497 : if (a > 0 && (unsigned HOST_WIDE_INT) a > rhs_minlen)
5310 : : {
5311 : 497 : int idx = new_addr_stridx (lhs);
5312 : 497 : if (idx != 0)
5313 : : {
5314 : 497 : si = new_strinfo (build_fold_addr_expr (lhs), idx,
5315 : 497 : build_int_cst (size_type_node, rhs_minlen),
5316 : : full_string_p);
5317 : 497 : set_strinfo (idx, si);
5318 : 497 : si->dont_invalidate = true;
5319 : : }
5320 : : }
5321 : : }
5322 : :
5323 : 361813 : if (si != NULL && offset == 0 && storing_all_zeros_p && lenrange[2] == 1)
5324 : : {
5325 : : /* For single-byte stores only, allow adjust_last_stmt to remove
5326 : : the statement if the stored '\0' is immediately overwritten. */
5327 : 42990 : laststmt.stmt = stmt;
5328 : 42990 : laststmt.len = build_int_cst (size_type_node, 1);
5329 : 42990 : laststmt.stridx = si->idx;
5330 : : }
5331 : : return true;
5332 : : }
5333 : :
5334 : : /* Try to fold strstr (s, t) eq/ne s to strncmp (s, t, strlen (t)) eq/ne 0. */
5335 : :
5336 : : static void
5337 : 3181415 : fold_strstr_to_strncmp (tree rhs1, tree rhs2, gimple *stmt)
5338 : : {
5339 : 3181415 : if (TREE_CODE (rhs1) != SSA_NAME
5340 : 3108679 : || TREE_CODE (rhs2) != SSA_NAME)
5341 : : return;
5342 : :
5343 : 1836755 : gimple *call_stmt = NULL;
5344 : 1836755 : for (int pass = 0; pass < 2; pass++)
5345 : : {
5346 : 1224508 : gimple *g = SSA_NAME_DEF_STMT (rhs1);
5347 : 1224508 : if (gimple_call_builtin_p (g, BUILT_IN_STRSTR)
5348 : 28 : && has_single_use (rhs1)
5349 : 1224519 : && gimple_call_arg (g, 0) == rhs2)
5350 : : {
5351 : : call_stmt = g;
5352 : : break;
5353 : : }
5354 : 1224497 : std::swap (rhs1, rhs2);
5355 : : }
5356 : :
5357 : 612258 : if (call_stmt)
5358 : : {
5359 : 11 : tree arg0 = gimple_call_arg (call_stmt, 0);
5360 : :
5361 : 11 : if (arg0 == rhs2)
5362 : : {
5363 : 11 : tree arg1 = gimple_call_arg (call_stmt, 1);
5364 : 11 : tree arg1_len = NULL_TREE;
5365 : 11 : int idx = get_stridx (arg1, call_stmt);
5366 : :
5367 : 11 : if (idx)
5368 : : {
5369 : 10 : if (idx < 0)
5370 : 9 : arg1_len = build_int_cst (size_type_node, ~idx);
5371 : : else
5372 : : {
5373 : 1 : strinfo *si = get_strinfo (idx);
5374 : 1 : if (si)
5375 : 1 : arg1_len = get_string_length (si);
5376 : : }
5377 : : }
5378 : :
5379 : 10 : if (arg1_len != NULL_TREE)
5380 : : {
5381 : 10 : gimple_stmt_iterator gsi = gsi_for_stmt (call_stmt);
5382 : 10 : tree strncmp_decl = builtin_decl_explicit (BUILT_IN_STRNCMP);
5383 : :
5384 : 10 : if (!is_gimple_val (arg1_len))
5385 : : {
5386 : 1 : tree arg1_len_tmp = make_ssa_name (TREE_TYPE (arg1_len));
5387 : 1 : gassign *arg1_stmt = gimple_build_assign (arg1_len_tmp,
5388 : : arg1_len);
5389 : 1 : gsi_insert_before (&gsi, arg1_stmt, GSI_SAME_STMT);
5390 : 1 : arg1_len = arg1_len_tmp;
5391 : : }
5392 : :
5393 : 10 : gcall *strncmp_call = gimple_build_call (strncmp_decl, 3,
5394 : : arg0, arg1, arg1_len);
5395 : 10 : tree strncmp_lhs = make_ssa_name (integer_type_node);
5396 : 20 : gimple_set_vuse (strncmp_call, gimple_vuse (call_stmt));
5397 : 10 : gimple_call_set_lhs (strncmp_call, strncmp_lhs);
5398 : 10 : gsi_remove (&gsi, true);
5399 : 10 : gsi_insert_before (&gsi, strncmp_call, GSI_SAME_STMT);
5400 : 10 : tree zero = build_zero_cst (TREE_TYPE (strncmp_lhs));
5401 : :
5402 : 10 : if (is_gimple_assign (stmt))
5403 : : {
5404 : 8 : if (gimple_assign_rhs_code (stmt) == COND_EXPR)
5405 : : {
5406 : 0 : tree cond = gimple_assign_rhs1 (stmt);
5407 : 0 : TREE_OPERAND (cond, 0) = strncmp_lhs;
5408 : 0 : TREE_OPERAND (cond, 1) = zero;
5409 : : }
5410 : : else
5411 : : {
5412 : 8 : gimple_assign_set_rhs1 (stmt, strncmp_lhs);
5413 : 8 : gimple_assign_set_rhs2 (stmt, zero);
5414 : : }
5415 : : }
5416 : : else
5417 : : {
5418 : 2 : gcond *cond = as_a<gcond *> (stmt);
5419 : 2 : gimple_cond_set_lhs (cond, strncmp_lhs);
5420 : 2 : gimple_cond_set_rhs (cond, zero);
5421 : : }
5422 : 10 : update_stmt (stmt);
5423 : : }
5424 : : }
5425 : : }
5426 : : }
5427 : :
5428 : : /* Return true if TYPE corresponds to a narrow character type. */
5429 : :
5430 : : static bool
5431 : 8303116 : is_char_type (tree type)
5432 : : {
5433 : 8303116 : return (TREE_CODE (type) == INTEGER_TYPE
5434 : 2845974 : && TYPE_MODE (type) == TYPE_MODE (char_type_node)
5435 : 8688811 : && TYPE_PRECISION (type) == TYPE_PRECISION (char_type_node));
5436 : : }
5437 : :
5438 : : /* Check the built-in call at GSI for validity and optimize it.
5439 : : Uses RVALS to determine range information.
5440 : : Return true to let the caller advance *GSI to the next statement
5441 : : in the basic block and false otherwise. */
5442 : :
5443 : : bool
5444 : 5213778 : strlen_pass::check_and_optimize_call (bool *zero_write)
5445 : : {
5446 : 5213778 : gimple *stmt = gsi_stmt (m_gsi);
5447 : :
5448 : 5213778 : if (!gimple_call_builtin_p (stmt, BUILT_IN_NORMAL))
5449 : : {
5450 : 3972966 : tree fntype = gimple_call_fntype (stmt);
5451 : 3972966 : if (!fntype)
5452 : : return true;
5453 : :
5454 : 3854253 : if (lookup_attribute ("alloc_size", TYPE_ATTRIBUTES (fntype)))
5455 : : {
5456 : 31951 : handle_alloc_call (BUILT_IN_NONE);
5457 : 31951 : return true;
5458 : : }
5459 : :
5460 : 3822302 : if (tree lhs = gimple_call_lhs (stmt))
5461 : 1333552 : handle_assign (lhs, zero_write);
5462 : :
5463 : : /* Proceed to handle user-defined formatting functions. */
5464 : : }
5465 : :
5466 : : /* When not optimizing we must be checking printf calls which
5467 : : we do even for user-defined functions when they are declared
5468 : : with attribute format. */
5469 : 5063114 : if (!flag_optimize_strlen
5470 : 4296442 : || !strlen_optimize
5471 : 9359556 : || !valid_builtin_call (stmt))
5472 : 4095777 : return !handle_printf_call (&m_gsi, ptr_qry);
5473 : :
5474 : 967337 : tree callee = gimple_call_fndecl (stmt);
5475 : 967337 : switch (DECL_FUNCTION_CODE (callee))
5476 : : {
5477 : 13335 : case BUILT_IN_STRLEN:
5478 : 13335 : case BUILT_IN_STRNLEN:
5479 : 13335 : handle_builtin_strlen ();
5480 : 13335 : break;
5481 : 584 : case BUILT_IN_STRCHR:
5482 : 584 : handle_builtin_strchr ();
5483 : 584 : break;
5484 : 2803 : case BUILT_IN_STRCPY:
5485 : 2803 : case BUILT_IN_STRCPY_CHK:
5486 : 2803 : case BUILT_IN_STPCPY:
5487 : 2803 : case BUILT_IN_STPCPY_CHK:
5488 : 2803 : handle_builtin_strcpy (DECL_FUNCTION_CODE (callee));
5489 : 2803 : break;
5490 : :
5491 : 805 : case BUILT_IN_STRNCAT:
5492 : 805 : case BUILT_IN_STRNCAT_CHK:
5493 : 805 : handle_builtin_strncat (DECL_FUNCTION_CODE (callee));
5494 : 805 : break;
5495 : :
5496 : 2457 : case BUILT_IN_STPNCPY:
5497 : 2457 : case BUILT_IN_STPNCPY_CHK:
5498 : 2457 : case BUILT_IN_STRNCPY:
5499 : 2457 : case BUILT_IN_STRNCPY_CHK:
5500 : 2457 : handle_builtin_stxncpy_strncat (false);
5501 : 2457 : break;
5502 : :
5503 : 58482 : case BUILT_IN_MEMCPY:
5504 : 58482 : case BUILT_IN_MEMCPY_CHK:
5505 : 58482 : case BUILT_IN_MEMPCPY:
5506 : 58482 : case BUILT_IN_MEMPCPY_CHK:
5507 : 58482 : handle_builtin_memcpy (DECL_FUNCTION_CODE (callee));
5508 : 58482 : break;
5509 : 825 : case BUILT_IN_STRCAT:
5510 : 825 : case BUILT_IN_STRCAT_CHK:
5511 : 825 : handle_builtin_strcat (DECL_FUNCTION_CODE (callee));
5512 : 825 : break;
5513 : 32611 : case BUILT_IN_ALLOCA:
5514 : 32611 : case BUILT_IN_ALLOCA_WITH_ALIGN:
5515 : 32611 : case BUILT_IN_MALLOC:
5516 : 32611 : case BUILT_IN_CALLOC:
5517 : 32611 : handle_alloc_call (DECL_FUNCTION_CODE (callee));
5518 : 32611 : break;
5519 : 24411 : case BUILT_IN_MEMSET:
5520 : 24411 : if (handle_builtin_memset (zero_write))
5521 : : return false;
5522 : : break;
5523 : 68082 : case BUILT_IN_MEMCMP:
5524 : 68082 : if (handle_builtin_memcmp ())
5525 : : return false;
5526 : : break;
5527 : 129563 : case BUILT_IN_STRCMP:
5528 : 129563 : case BUILT_IN_STRNCMP:
5529 : 129563 : if (handle_builtin_string_cmp ())
5530 : : return false;
5531 : : break;
5532 : 633379 : default:
5533 : 633379 : if (handle_printf_call (&m_gsi, ptr_qry))
5534 : : return false;
5535 : : break;
5536 : : }
5537 : :
5538 : : return true;
5539 : : }
5540 : :
5541 : : /* Handle an assignment statement at *GSI to a LHS of integral type.
5542 : : If GSI's basic block needs clean-up of EH, set *CLEANUP_EH to true. */
5543 : :
5544 : : void
5545 : 8690092 : strlen_pass::handle_integral_assign (bool *cleanup_eh)
5546 : : {
5547 : 8690092 : gimple *stmt = gsi_stmt (m_gsi);
5548 : 8690092 : tree lhs = gimple_assign_lhs (stmt);
5549 : 8690092 : tree lhs_type = TREE_TYPE (lhs);
5550 : :
5551 : 8690092 : enum tree_code code = gimple_assign_rhs_code (stmt);
5552 : 8690092 : if (code == COND_EXPR)
5553 : : {
5554 : 9670 : tree cond = gimple_assign_rhs1 (stmt);
5555 : 9670 : enum tree_code cond_code = TREE_CODE (cond);
5556 : :
5557 : 9670 : if (cond_code == EQ_EXPR || cond_code == NE_EXPR)
5558 : 0 : fold_strstr_to_strncmp (TREE_OPERAND (cond, 0),
5559 : 0 : TREE_OPERAND (cond, 1), stmt);
5560 : : }
5561 : 8680422 : else if (code == EQ_EXPR || code == NE_EXPR)
5562 : 203585 : fold_strstr_to_strncmp (gimple_assign_rhs1 (stmt),
5563 : : gimple_assign_rhs2 (stmt), stmt);
5564 : 8476837 : else if (gimple_assign_load_p (stmt)
5565 : 2426454 : && TREE_CODE (lhs_type) == INTEGER_TYPE
5566 : 2270215 : && TYPE_MODE (lhs_type) == TYPE_MODE (char_type_node)
5567 : 325838 : && (TYPE_PRECISION (lhs_type)
5568 : 325838 : == TYPE_PRECISION (char_type_node))
5569 : 8795975 : && !gimple_has_volatile_ops (stmt))
5570 : : {
5571 : 307564 : tree off = integer_zero_node;
5572 : 307564 : unsigned HOST_WIDE_INT coff = 0;
5573 : 307564 : int idx = 0;
5574 : 307564 : tree rhs1 = gimple_assign_rhs1 (stmt);
5575 : 307564 : if (code == MEM_REF)
5576 : : {
5577 : 133915 : idx = get_stridx (TREE_OPERAND (rhs1, 0), stmt);
5578 : 133915 : if (idx > 0)
5579 : : {
5580 : 5851 : strinfo *si = get_strinfo (idx);
5581 : 5851 : if (si
5582 : 925 : && si->nonzero_chars
5583 : 915 : && TREE_CODE (si->nonzero_chars) == INTEGER_CST
5584 : 5949 : && (wi::to_widest (si->nonzero_chars)
5585 : 196 : >= wi::to_widest (off)))
5586 : 98 : off = TREE_OPERAND (rhs1, 1);
5587 : : else
5588 : : /* This case is not useful. See if get_addr_stridx
5589 : : returns something usable. */
5590 : : idx = 0;
5591 : : }
5592 : : }
5593 : 5753 : if (idx <= 0)
5594 : 307466 : idx = get_addr_stridx (rhs1, stmt, NULL_TREE, &coff);
5595 : 307564 : if (idx > 0)
5596 : : {
5597 : 4984 : strinfo *si = get_strinfo (idx);
5598 : 4984 : if (si
5599 : 195 : && si->nonzero_chars
5600 : 195 : && TREE_CODE (si->nonzero_chars) == INTEGER_CST)
5601 : : {
5602 : 130 : widest_int w1 = wi::to_widest (si->nonzero_chars);
5603 : 130 : widest_int w2 = wi::to_widest (off) + coff;
5604 : 130 : if (w1 == w2
5605 : 130 : && si->full_string_p)
5606 : : {
5607 : 32 : if (dump_file && (dump_flags & TDF_DETAILS) != 0)
5608 : : {
5609 : 0 : fprintf (dump_file, "Optimizing: ");
5610 : 0 : print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
5611 : : }
5612 : :
5613 : : /* Reading the final '\0' character. */
5614 : 32 : tree zero = build_int_cst (lhs_type, 0);
5615 : 32 : gimple_set_vuse (stmt, NULL_TREE);
5616 : 32 : gimple_assign_set_rhs_from_tree (&m_gsi, zero);
5617 : 32 : *cleanup_eh
5618 : 32 : |= maybe_clean_or_replace_eh_stmt (stmt,
5619 : : gsi_stmt (m_gsi));
5620 : 32 : stmt = gsi_stmt (m_gsi);
5621 : 32 : update_stmt (stmt);
5622 : :
5623 : 32 : if (dump_file && (dump_flags & TDF_DETAILS) != 0)
5624 : : {
5625 : 0 : fprintf (dump_file, "into: ");
5626 : 0 : print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
5627 : : }
5628 : : }
5629 : 98 : else if (w1 > w2)
5630 : : {
5631 : : /* Reading a character before the final '\0'
5632 : : character. Just set the value range to ~[0, 0]
5633 : : if we don't have anything better. */
5634 : 1 : int_range_max r;
5635 : 2 : if (!get_range_query (cfun)->range_of_expr (r, lhs)
5636 : 1 : || r.varying_p ())
5637 : : {
5638 : 1 : r.set_nonzero (lhs_type);
5639 : 1 : set_range_info (lhs, r);
5640 : : }
5641 : 1 : }
5642 : 130 : }
5643 : : }
5644 : : }
5645 : 8169273 : else if (code == MEM_REF && TREE_CODE (lhs) == SSA_NAME)
5646 : : {
5647 : 559217 : if (int idx = new_stridx (lhs))
5648 : : {
5649 : : /* Record multi-byte assignments from MEM_REFs. */
5650 : 559217 : bool storing_all_nonzero_p;
5651 : 559217 : bool storing_all_zeros_p;
5652 : 559217 : bool full_string_p;
5653 : 559217 : unsigned lenrange[] = { UINT_MAX, 0, 0 };
5654 : 559217 : tree rhs = gimple_assign_rhs1 (stmt);
5655 : 559217 : const bool ranges_valid
5656 : 559217 : = count_nonzero_bytes (rhs, stmt,
5657 : : lenrange, &full_string_p,
5658 : : &storing_all_zeros_p,
5659 : : &storing_all_nonzero_p);
5660 : 559217 : if (ranges_valid)
5661 : : {
5662 : 515545 : tree length = build_int_cst (sizetype, lenrange[0]);
5663 : 515545 : strinfo *si = new_strinfo (lhs, idx, length, full_string_p);
5664 : 515545 : set_strinfo (idx, si);
5665 : 515545 : si->writable = true;
5666 : 515545 : si->dont_invalidate = true;
5667 : : }
5668 : : }
5669 : : }
5670 : :
5671 : 8690092 : if (strlen_to_stridx)
5672 : : {
5673 : 8687452 : tree rhs1 = gimple_assign_rhs1 (stmt);
5674 : 8687452 : if (stridx_strlenloc *ps = strlen_to_stridx->get (rhs1))
5675 : 5401 : strlen_to_stridx->put (lhs, stridx_strlenloc (*ps));
5676 : : }
5677 : 8690092 : }
5678 : :
5679 : : /* Handle assignment statement at *GSI to LHS. Set *ZERO_WRITE if
5680 : : the assignment stores all zero bytes. */
5681 : :
5682 : : bool
5683 : 5970764 : strlen_pass::handle_assign (tree lhs, bool *zero_write)
5684 : : {
5685 : 5970764 : tree type = TREE_TYPE (lhs);
5686 : 5970764 : if (TREE_CODE (type) == ARRAY_TYPE)
5687 : 113757 : type = TREE_TYPE (type);
5688 : :
5689 : 5970764 : bool is_char_store = is_char_type (type);
5690 : 5970764 : if (!is_char_store && TREE_CODE (lhs) == MEM_REF)
5691 : : {
5692 : : /* To consider stores into char objects via integer types other
5693 : : than char but not those to non-character objects, determine
5694 : : the type of the destination rather than just the type of
5695 : : the access. */
5696 : 3464773 : for (int i = 0; i != 2; ++i)
5697 : : {
5698 : 2332352 : tree ref = TREE_OPERAND (lhs, i);
5699 : 2332352 : type = TREE_TYPE (ref);
5700 : 2332352 : if (TREE_CODE (type) == POINTER_TYPE)
5701 : 2242787 : type = TREE_TYPE (type);
5702 : 2332352 : if (TREE_CODE (type) == ARRAY_TYPE)
5703 : 107479 : type = TREE_TYPE (type);
5704 : 2332352 : if (is_char_type (type))
5705 : : {
5706 : : is_char_store = true;
5707 : : break;
5708 : : }
5709 : : }
5710 : : }
5711 : :
5712 : : /* Handle a single or multibyte assignment. */
5713 : 5970764 : if (is_char_store && !handle_store (zero_write))
5714 : : return false;
5715 : :
5716 : : return true;
5717 : : }
5718 : :
5719 : :
5720 : : /* Attempt to check for validity of the performed access a single statement
5721 : : at *GSI using string length knowledge, and to optimize it.
5722 : : If the given basic block needs clean-up of EH, CLEANUP_EH is set to
5723 : : true. Return true to let the caller advance *GSI to the next statement
5724 : : in the basic block and false otherwise. */
5725 : :
5726 : : bool
5727 : 77501410 : strlen_pass::check_and_optimize_stmt (bool *cleanup_eh)
5728 : : {
5729 : 77501410 : gimple *stmt = gsi_stmt (m_gsi);
5730 : :
5731 : : /* For statements that modify a string, set to true if the write
5732 : : is only zeros. */
5733 : 77501410 : bool zero_write = false;
5734 : :
5735 : 77501410 : if (is_gimple_call (stmt))
5736 : : {
5737 : 5213778 : if (!check_and_optimize_call (&zero_write))
5738 : : return false;
5739 : : }
5740 : 72287632 : else if (!flag_optimize_strlen || !strlen_optimize)
5741 : : return true;
5742 : 67843724 : else if (is_gimple_assign (stmt) && !gimple_clobber_p (stmt))
5743 : : {
5744 : : /* Handle non-clobbering assignment. */
5745 : 18762042 : tree lhs = gimple_assign_lhs (stmt);
5746 : 18762042 : tree lhs_type = TREE_TYPE (lhs);
5747 : :
5748 : 18762042 : if (TREE_CODE (lhs) == SSA_NAME && POINTER_TYPE_P (lhs_type))
5749 : : {
5750 : 3500110 : if (gimple_assign_single_p (stmt)
5751 : 3500110 : || (gimple_assign_cast_p (stmt)
5752 : 283946 : && POINTER_TYPE_P (TREE_TYPE (gimple_assign_rhs1 (stmt)))))
5753 : : {
5754 : 2584746 : int idx = get_stridx (gimple_assign_rhs1 (stmt), stmt);
5755 : 2584746 : ssa_ver_to_stridx[SSA_NAME_VERSION (lhs)] = idx;
5756 : : }
5757 : 915364 : else if (gimple_assign_rhs_code (stmt) == POINTER_PLUS_EXPR)
5758 : 654010 : handle_pointer_plus ();
5759 : : }
5760 : 15261932 : else if (TREE_CODE (lhs) == SSA_NAME && INTEGRAL_TYPE_P (lhs_type))
5761 : : /* Handle assignment to a character. */
5762 : 8690092 : handle_integral_assign (cleanup_eh);
5763 : 6571840 : else if (TREE_CODE (lhs) != SSA_NAME && !TREE_SIDE_EFFECTS (lhs))
5764 : 4637212 : if (!handle_assign (lhs, &zero_write))
5765 : : return false;
5766 : : }
5767 : 49081682 : else if (gcond *cond = dyn_cast<gcond *> (stmt))
5768 : : {
5769 : 3775214 : enum tree_code code = gimple_cond_code (cond);
5770 : 3775214 : if (code == EQ_EXPR || code == NE_EXPR)
5771 : 2977830 : fold_strstr_to_strncmp (gimple_cond_lhs (stmt),
5772 : : gimple_cond_rhs (stmt), stmt);
5773 : : }
5774 : :
5775 : 99018239 : if (gimple_vdef (stmt))
5776 : 10416745 : maybe_invalidate (stmt, zero_write);
5777 : : return true;
5778 : : }
5779 : :
5780 : : /* Recursively call maybe_invalidate on stmts that might be executed
5781 : : in between dombb and current bb and that contain a vdef. Stop when
5782 : : *count stmts are inspected, or if the whole strinfo vector has
5783 : : been invalidated. */
5784 : :
5785 : : static void
5786 : 675035 : do_invalidate (basic_block dombb, gimple *phi, bitmap visited, int *count)
5787 : : {
5788 : 675035 : unsigned int i, n = gimple_phi_num_args (phi);
5789 : :
5790 : 1190180 : for (i = 0; i < n; i++)
5791 : : {
5792 : 1024870 : tree vuse = gimple_phi_arg_def (phi, i);
5793 : 1024870 : gimple *stmt = SSA_NAME_DEF_STMT (vuse);
5794 : 1024870 : basic_block bb = gimple_bb (stmt);
5795 : 1351486 : if (bb == NULL
5796 : 1024870 : || bb == dombb
5797 : 869875 : || !bitmap_set_bit (visited, bb->index)
5798 : 1775164 : || !dominated_by_p (CDI_DOMINATORS, bb, dombb))
5799 : 326616 : continue;
5800 : 939043 : while (1)
5801 : : {
5802 : 939043 : if (gimple_code (stmt) == GIMPLE_PHI)
5803 : : {
5804 : 195963 : do_invalidate (dombb, stmt, visited, count);
5805 : 195963 : if (*count == 0)
5806 : : return;
5807 : : break;
5808 : : }
5809 : 743080 : if (--*count == 0)
5810 : : return;
5811 : 742680 : if (!maybe_invalidate (stmt))
5812 : : {
5813 : 418450 : *count = 0;
5814 : 418450 : return;
5815 : : }
5816 : 324230 : vuse = gimple_vuse (stmt);
5817 : 324230 : stmt = SSA_NAME_DEF_STMT (vuse);
5818 : 324230 : if (gimple_bb (stmt) != bb)
5819 : : {
5820 : 127433 : bb = gimple_bb (stmt);
5821 : 127433 : if (bb == NULL
5822 : 127433 : || bb == dombb
5823 : 80583 : || !bitmap_set_bit (visited, bb->index)
5824 : 177283 : || !dominated_by_p (CDI_DOMINATORS, bb, dombb))
5825 : : break;
5826 : : }
5827 : : }
5828 : : }
5829 : : }
5830 : :
5831 : : /* Release pointer_query cache. */
5832 : :
5833 : 1064649 : strlen_pass::~strlen_pass ()
5834 : : {
5835 : 1064649 : ptr_qry.flush_cache ();
5836 : 2129298 : }
5837 : :
5838 : : /* Callback for walk_dominator_tree. Attempt to optimize various
5839 : : string ops by remembering string lengths pointed by pointer SSA_NAMEs. */
5840 : :
5841 : : edge
5842 : 11486834 : strlen_pass::before_dom_children (basic_block bb)
5843 : : {
5844 : 11486834 : basic_block dombb = get_immediate_dominator (CDI_DOMINATORS, bb);
5845 : :
5846 : 11486834 : if (dombb == NULL)
5847 : 1064649 : stridx_to_strinfo = NULL;
5848 : : else
5849 : : {
5850 : 10422185 : stridx_to_strinfo = ((vec<strinfo *, va_heap, vl_embed> *) dombb->aux);
5851 : 10422185 : if (stridx_to_strinfo)
5852 : : {
5853 : 2779795 : for (gphi_iterator gsi = gsi_start_phis (bb); !gsi_end_p (gsi);
5854 : 433100 : gsi_next (&gsi))
5855 : : {
5856 : 912172 : gphi *phi = gsi.phi ();
5857 : 1824344 : if (virtual_operand_p (gimple_phi_result (phi)))
5858 : : {
5859 : 479072 : bitmap visited = BITMAP_ALLOC (NULL);
5860 : 479072 : int count_vdef = 100;
5861 : 479072 : do_invalidate (dombb, phi, visited, &count_vdef);
5862 : 479072 : BITMAP_FREE (visited);
5863 : 479072 : if (count_vdef == 0)
5864 : : {
5865 : : /* If there were too many vdefs in between immediate
5866 : : dominator and current bb, invalidate everything.
5867 : : If stridx_to_strinfo has been unshared, we need
5868 : : to free it, otherwise just set it to NULL. */
5869 : 418850 : if (!strinfo_shared ())
5870 : : {
5871 : : unsigned int i;
5872 : : strinfo *si;
5873 : :
5874 : 478935 : for (i = 1;
5875 : 519675 : vec_safe_iterate (stridx_to_strinfo, i, &si);
5876 : : ++i)
5877 : : {
5878 : 478935 : free_strinfo (si);
5879 : 478935 : (*stridx_to_strinfo)[i] = NULL;
5880 : : }
5881 : : }
5882 : : else
5883 : 378110 : stridx_to_strinfo = NULL;
5884 : : }
5885 : 479072 : break;
5886 : : }
5887 : : }
5888 : : }
5889 : : }
5890 : :
5891 : : /* If all PHI arguments have the same string index, the PHI result
5892 : : has it as well. */
5893 : 16209898 : for (gphi_iterator gsi = gsi_start_phis (bb); !gsi_end_p (gsi);
5894 : 4723064 : gsi_next (&gsi))
5895 : : {
5896 : 4723064 : gphi *phi = gsi.phi ();
5897 : 4723064 : tree result = gimple_phi_result (phi);
5898 : 9446128 : if (!virtual_operand_p (result) && POINTER_TYPE_P (TREE_TYPE (result)))
5899 : : {
5900 : 760293 : int idx = get_stridx (gimple_phi_arg_def (phi, 0), phi);
5901 : 760293 : if (idx != 0)
5902 : : {
5903 : 32890 : unsigned int i, n = gimple_phi_num_args (phi);
5904 : 43404 : for (i = 1; i < n; i++)
5905 : 30876 : if (idx != get_stridx (gimple_phi_arg_def (phi, i), phi))
5906 : : break;
5907 : 32890 : if (i == n)
5908 : 12528 : ssa_ver_to_stridx[SSA_NAME_VERSION (result)] = idx;
5909 : : }
5910 : : }
5911 : : }
5912 : :
5913 : 11486834 : bool cleanup_eh = false;
5914 : :
5915 : : /* Attempt to optimize individual statements. */
5916 : 111961912 : for (m_gsi = gsi_start_bb (bb); !gsi_end_p (m_gsi); )
5917 : : {
5918 : : /* Reset search depth performance counter. */
5919 : 77501410 : ptr_qry.depth = 0;
5920 : :
5921 : 77501410 : if (check_and_optimize_stmt (&cleanup_eh))
5922 : 77433940 : gsi_next (&m_gsi);
5923 : : }
5924 : :
5925 : 11486834 : if (cleanup_eh && gimple_purge_dead_eh_edges (bb))
5926 : 0 : m_cleanup_cfg = true;
5927 : :
5928 : 11486834 : bb->aux = stridx_to_strinfo;
5929 : 11486834 : if (vec_safe_length (stridx_to_strinfo) && !strinfo_shared ())
5930 : 750189 : (*stridx_to_strinfo)[0] = (strinfo *) bb;
5931 : 11486834 : return NULL;
5932 : : }
5933 : :
5934 : : /* Callback for walk_dominator_tree. Free strinfo vector if it is
5935 : : owned by the current bb, clear bb->aux. */
5936 : :
5937 : : void
5938 : 11486834 : strlen_pass::after_dom_children (basic_block bb)
5939 : : {
5940 : 11486834 : if (bb->aux)
5941 : : {
5942 : 2438993 : stridx_to_strinfo = ((vec<strinfo *, va_heap, vl_embed> *) bb->aux);
5943 : 2438993 : if (vec_safe_length (stridx_to_strinfo)
5944 : 2438993 : && (*stridx_to_strinfo)[0] == (strinfo *) bb)
5945 : : {
5946 : : unsigned int i;
5947 : : strinfo *si;
5948 : :
5949 : 11819669 : for (i = 1; vec_safe_iterate (stridx_to_strinfo, i, &si); ++i)
5950 : 11069480 : free_strinfo (si);
5951 : 750189 : vec_free (stridx_to_strinfo);
5952 : : }
5953 : 2438993 : bb->aux = NULL;
5954 : : }
5955 : 11486834 : }
5956 : :
5957 : : namespace {
5958 : :
5959 : : static unsigned int
5960 : 1064649 : printf_strlen_execute (function *fun, bool warn_only)
5961 : : {
5962 : 1064649 : strlen_optimize = !warn_only;
5963 : :
5964 : 1064649 : calculate_dominance_info (CDI_DOMINATORS);
5965 : 1064649 : loop_optimizer_init (LOOPS_NORMAL);
5966 : 1064649 : scev_initialize ();
5967 : :
5968 : 1064649 : gcc_assert (!strlen_to_stridx);
5969 : 1064649 : if (warn_stringop_overflow || warn_stringop_truncation)
5970 : 1064409 : strlen_to_stridx = new hash_map<tree, stridx_strlenloc> ();
5971 : :
5972 : : /* This has to happen after initializing the loop optimizer
5973 : : and initializing SCEV as they create new SSA_NAMEs. */
5974 : 2129298 : ssa_ver_to_stridx.safe_grow_cleared (num_ssa_names, true);
5975 : 1064649 : max_stridx = 1;
5976 : :
5977 : 1064649 : enable_ranger (fun);
5978 : : /* String length optimization is implemented as a walk of the dominator
5979 : : tree and a forward walk of statements within each block. */
5980 : 1064649 : strlen_pass walker (fun, CDI_DOMINATORS);
5981 : 1064649 : walker.walk (ENTRY_BLOCK_PTR_FOR_FN (fun));
5982 : :
5983 : 1064649 : if (dump_file && (dump_flags & TDF_DETAILS))
5984 : 0 : walker.ptr_qry.dump (dump_file, true);
5985 : :
5986 : 1064649 : ssa_ver_to_stridx.release ();
5987 : 1064649 : strinfo_pool.release ();
5988 : 1064649 : if (decl_to_stridxlist_htab)
5989 : : {
5990 : 42758 : obstack_free (&stridx_obstack, NULL);
5991 : 85516 : delete decl_to_stridxlist_htab;
5992 : 42758 : decl_to_stridxlist_htab = NULL;
5993 : : }
5994 : 1064649 : laststmt.stmt = NULL;
5995 : 1064649 : laststmt.len = NULL_TREE;
5996 : 1064649 : laststmt.stridx = 0;
5997 : :
5998 : 1064649 : if (strlen_to_stridx)
5999 : : {
6000 : 1064409 : strlen_to_stridx->empty ();
6001 : 2128818 : delete strlen_to_stridx;
6002 : 1064409 : strlen_to_stridx = NULL;
6003 : : }
6004 : :
6005 : 1064649 : disable_ranger (fun);
6006 : 1064649 : scev_finalize ();
6007 : 1064649 : loop_optimizer_finalize ();
6008 : :
6009 : 1064649 : return walker.m_cleanup_cfg ? TODO_cleanup_cfg : 0;
6010 : 1064649 : }
6011 : :
6012 : : /* This file defines two passes: one for warnings that runs only when
6013 : : optimization is disabled, and another that implements optimizations
6014 : : and also issues warnings. */
6015 : :
6016 : : const pass_data pass_data_warn_printf =
6017 : : {
6018 : : GIMPLE_PASS, /* type */
6019 : : "warn-printf", /* name */
6020 : : OPTGROUP_NONE, /* optinfo_flags */
6021 : : TV_NONE, /* tv_id */
6022 : : /* Normally an optimization pass would require PROP_ssa but because
6023 : : this pass runs early, with no optimization, to do sprintf format
6024 : : checking, it only requires PROP_cfg. */
6025 : : PROP_cfg, /* properties_required */
6026 : : 0, /* properties_provided */
6027 : : 0, /* properties_destroyed */
6028 : : 0, /* todo_flags_start */
6029 : : 0, /* todo_flags_finish */
6030 : : };
6031 : :
6032 : : class pass_warn_printf : public gimple_opt_pass
6033 : : {
6034 : : public:
6035 : 282866 : pass_warn_printf (gcc::context *ctxt)
6036 : 565732 : : gimple_opt_pass (pass_data_warn_printf, ctxt)
6037 : : {}
6038 : :
6039 : : bool gate (function *) final override;
6040 : 61046 : unsigned int execute (function *fun) final override
6041 : : {
6042 : 61046 : return printf_strlen_execute (fun, true);
6043 : : }
6044 : : };
6045 : :
6046 : :
6047 : : /* Return true to run the warning pass only when not optimizing and
6048 : : iff either -Wformat-overflow or -Wformat-truncation is specified. */
6049 : :
6050 : : bool
6051 : 2692213 : pass_warn_printf::gate (function *)
6052 : : {
6053 : 2692213 : return !optimize && (warn_format_overflow > 0 || warn_format_trunc > 0);
6054 : : }
6055 : :
6056 : : const pass_data pass_data_strlen =
6057 : : {
6058 : : GIMPLE_PASS, /* type */
6059 : : "strlen", /* name */
6060 : : OPTGROUP_NONE, /* optinfo_flags */
6061 : : TV_TREE_STRLEN, /* tv_id */
6062 : : PROP_cfg | PROP_ssa, /* properties_required */
6063 : : 0, /* properties_provided */
6064 : : 0, /* properties_destroyed */
6065 : : 0, /* todo_flags_start */
6066 : : 0, /* todo_flags_finish */
6067 : : };
6068 : :
6069 : : class pass_strlen : public gimple_opt_pass
6070 : : {
6071 : : public:
6072 : 565732 : pass_strlen (gcc::context *ctxt)
6073 : 1131464 : : gimple_opt_pass (pass_data_strlen, ctxt)
6074 : : {}
6075 : :
6076 : 282866 : opt_pass * clone () final override { return new pass_strlen (m_ctxt); }
6077 : :
6078 : : bool gate (function *) final override;
6079 : 1003603 : unsigned int execute (function *fun) final override
6080 : : {
6081 : 1003603 : return printf_strlen_execute (fun, false);
6082 : : }
6083 : : };
6084 : :
6085 : : /* Return true to run the pass only when the sprintf and/or strlen
6086 : : optimizations are enabled and -Wformat-overflow or -Wformat-truncation
6087 : : are specified. */
6088 : :
6089 : : bool
6090 : 1003634 : pass_strlen::gate (function *)
6091 : : {
6092 : 1003634 : return ((warn_format_overflow > 0
6093 : 892355 : || warn_format_trunc > 0
6094 : 892350 : || warn_restrict > 0
6095 : 892163 : || flag_optimize_strlen > 0
6096 : 115416 : || flag_printf_return_value)
6097 : 1895977 : && optimize > 0);
6098 : : }
6099 : :
6100 : : } // anon namespace
6101 : :
6102 : : gimple_opt_pass *
6103 : 282866 : make_pass_warn_printf (gcc::context *ctxt)
6104 : : {
6105 : 282866 : return new pass_warn_printf (ctxt);
6106 : : }
6107 : :
6108 : : gimple_opt_pass *
6109 : 282866 : make_pass_strlen (gcc::context *ctxt)
6110 : : {
6111 : 282866 : return new pass_strlen (ctxt);
6112 : : }
|