gcc/
[official-gcc.git] / gcc / tree-ssa-strlen.c
blobfc848ea41b918ca77e0ea1df90a162adf35eeca9
1 /* String length optimization
2 Copyright (C) 2011-2015 Free Software Foundation, Inc.
3 Contributed by Jakub Jelinek <jakub@redhat.com>
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "alias.h"
25 #include "symtab.h"
26 #include "options.h"
27 #include "tree.h"
28 #include "fold-const.h"
29 #include "stor-layout.h"
30 #include "bitmap.h"
31 #include "predict.h"
32 #include "tm.h"
33 #include "hard-reg-set.h"
34 #include "function.h"
35 #include "dominance.h"
36 #include "cfg.h"
37 #include "basic-block.h"
38 #include "tree-ssa-alias.h"
39 #include "internal-fn.h"
40 #include "gimple-fold.h"
41 #include "tree-eh.h"
42 #include "gimple-expr.h"
43 #include "gimple.h"
44 #include "gimplify.h"
45 #include "gimple-iterator.h"
46 #include "gimplify-me.h"
47 #include "gimple-ssa.h"
48 #include "tree-phinodes.h"
49 #include "ssa-iterators.h"
50 #include "stringpool.h"
51 #include "tree-ssanames.h"
52 #include "rtl.h"
53 #include "flags.h"
54 #include "insn-config.h"
55 #include "expmed.h"
56 #include "dojump.h"
57 #include "explow.h"
58 #include "calls.h"
59 #include "emit-rtl.h"
60 #include "varasm.h"
61 #include "stmt.h"
62 #include "expr.h"
63 #include "tree-dfa.h"
64 #include "tree-pass.h"
65 #include "domwalk.h"
66 #include "alloc-pool.h"
67 #include "tree-ssa-propagate.h"
68 #include "gimple-pretty-print.h"
69 #include "params.h"
70 #include "plugin-api.h"
71 #include "ipa-ref.h"
72 #include "cgraph.h"
73 #include "ipa-chkp.h"
75 /* A vector indexed by SSA_NAME_VERSION. 0 means unknown, positive value
76 is an index into strinfo vector, negative value stands for
77 string length of a string literal (~strlen). */
78 static vec<int> ssa_ver_to_stridx;
80 /* Number of currently active string indexes plus one. */
81 static int max_stridx;
83 /* String information record. */
84 typedef struct strinfo_struct
86 /* String length of this string. */
87 tree length;
88 /* Any of the corresponding pointers for querying alias oracle. */
89 tree ptr;
90 /* Statement for delayed length computation. */
91 gimple stmt;
92 /* Pointer to '\0' if known, if NULL, it can be computed as
93 ptr + length. */
94 tree endptr;
95 /* Reference count. Any changes to strinfo entry possibly shared
96 with dominating basic blocks need unshare_strinfo first, except
97 for dont_invalidate which affects only the immediately next
98 maybe_invalidate. */
99 int refcount;
100 /* Copy of index. get_strinfo (si->idx) should return si; */
101 int idx;
102 /* These 3 fields are for chaining related string pointers together.
103 E.g. for
104 bl = strlen (b); dl = strlen (d); strcpy (a, b); c = a + bl;
105 strcpy (c, d); e = c + dl;
106 strinfo(a) -> strinfo(c) -> strinfo(e)
107 All have ->first field equal to strinfo(a)->idx and are doubly
108 chained through prev/next fields. The later strinfos are required
109 to point into the same string with zero or more bytes after
110 the previous pointer and all bytes in between the two pointers
111 must be non-zero. Functions like strcpy or memcpy are supposed
112 to adjust all previous strinfo lengths, but not following strinfo
113 lengths (those are uncertain, usually invalidated during
114 maybe_invalidate, except when the alias oracle knows better).
115 Functions like strcat on the other side adjust the whole
116 related strinfo chain.
117 They are updated lazily, so to use the chain the same first fields
118 and si->prev->next == si->idx needs to be verified. */
119 int first;
120 int next;
121 int prev;
122 /* A flag whether the string is known to be written in the current
123 function. */
124 bool writable;
125 /* A flag for the next maybe_invalidate that this strinfo shouldn't
126 be invalidated. Always cleared by maybe_invalidate. */
127 bool dont_invalidate;
128 } *strinfo;
130 /* Pool for allocating strinfo_struct entries. */
131 static pool_allocator<strinfo_struct> strinfo_pool ("strinfo_struct pool", 64);
133 /* Vector mapping positive string indexes to strinfo, for the
134 current basic block. The first pointer in the vector is special,
135 it is either NULL, meaning the vector isn't shared, or it is
136 a basic block pointer to the owner basic_block if shared.
137 If some other bb wants to modify the vector, the vector needs
138 to be unshared first, and only the owner bb is supposed to free it. */
139 static vec<strinfo, va_heap, vl_embed> *stridx_to_strinfo;
141 /* One OFFSET->IDX mapping. */
142 struct stridxlist
144 struct stridxlist *next;
145 HOST_WIDE_INT offset;
146 int idx;
149 /* Hash table entry, mapping a DECL to a chain of OFFSET->IDX mappings. */
150 struct decl_stridxlist_map
152 struct tree_map_base base;
153 struct stridxlist list;
156 /* stridxlist hashtable helpers. */
158 struct stridxlist_hash_traits : default_hashmap_traits
160 static inline hashval_t hash (tree);
163 /* Hash a from tree in a decl_stridxlist_map. */
165 inline hashval_t
166 stridxlist_hash_traits::hash (tree item)
168 return DECL_UID (item);
171 /* Hash table for mapping decls to a chained list of offset -> idx
172 mappings. */
173 static hash_map<tree, stridxlist, stridxlist_hash_traits>
174 *decl_to_stridxlist_htab;
176 /* Obstack for struct stridxlist and struct decl_stridxlist_map. */
177 static struct obstack stridx_obstack;
179 /* Last memcpy statement if it could be adjusted if the trailing
180 '\0' written is immediately overwritten, or
181 *x = '\0' store that could be removed if it is immediately overwritten. */
182 struct laststmt_struct
184 gimple stmt;
185 tree len;
186 int stridx;
187 } laststmt;
189 static int get_stridx_plus_constant (strinfo, HOST_WIDE_INT, tree);
191 /* Return strinfo vector entry IDX. */
193 static inline strinfo
194 get_strinfo (int idx)
196 if (vec_safe_length (stridx_to_strinfo) <= (unsigned int) idx)
197 return NULL;
198 return (*stridx_to_strinfo)[idx];
201 /* Helper function for get_stridx. */
203 static int
204 get_addr_stridx (tree exp)
206 HOST_WIDE_INT off;
207 struct stridxlist *list;
208 tree base;
210 if (!decl_to_stridxlist_htab)
211 return 0;
213 base = get_addr_base_and_unit_offset (exp, &off);
214 if (base == NULL || !DECL_P (base))
215 return 0;
217 list = decl_to_stridxlist_htab->get (base);
218 if (list == NULL)
219 return 0;
223 if (list->offset == off)
224 return list->idx;
225 list = list->next;
227 while (list);
228 return 0;
231 /* Return string index for EXP. */
233 static int
234 get_stridx (tree exp)
236 tree s, o;
238 if (TREE_CODE (exp) == SSA_NAME)
240 if (ssa_ver_to_stridx[SSA_NAME_VERSION (exp)])
241 return ssa_ver_to_stridx[SSA_NAME_VERSION (exp)];
242 int i;
243 tree e = exp;
244 HOST_WIDE_INT off = 0;
245 for (i = 0; i < 5; i++)
247 gimple def_stmt = SSA_NAME_DEF_STMT (e);
248 if (!is_gimple_assign (def_stmt)
249 || gimple_assign_rhs_code (def_stmt) != POINTER_PLUS_EXPR)
250 return 0;
251 tree rhs1 = gimple_assign_rhs1 (def_stmt);
252 tree rhs2 = gimple_assign_rhs2 (def_stmt);
253 if (TREE_CODE (rhs1) != SSA_NAME
254 || !tree_fits_shwi_p (rhs2))
255 return 0;
256 HOST_WIDE_INT this_off = tree_to_shwi (rhs2);
257 if (this_off < 0)
258 return 0;
259 off = (unsigned HOST_WIDE_INT) off + this_off;
260 if (off < 0)
261 return 0;
262 if (ssa_ver_to_stridx[SSA_NAME_VERSION (rhs1)])
264 strinfo si
265 = get_strinfo (ssa_ver_to_stridx[SSA_NAME_VERSION (rhs1)]);
266 if (si
267 && si->length
268 && TREE_CODE (si->length) == INTEGER_CST
269 && compare_tree_int (si->length, off) != -1)
270 return get_stridx_plus_constant (si, off, exp);
272 e = rhs1;
274 return 0;
277 if (TREE_CODE (exp) == ADDR_EXPR)
279 int idx = get_addr_stridx (TREE_OPERAND (exp, 0));
280 if (idx != 0)
281 return idx;
284 s = string_constant (exp, &o);
285 if (s != NULL_TREE
286 && (o == NULL_TREE || tree_fits_shwi_p (o))
287 && TREE_STRING_LENGTH (s) > 0)
289 HOST_WIDE_INT offset = o ? tree_to_shwi (o) : 0;
290 const char *p = TREE_STRING_POINTER (s);
291 int max = TREE_STRING_LENGTH (s) - 1;
293 if (p[max] == '\0' && offset >= 0 && offset <= max)
294 return ~(int) strlen (p + offset);
296 return 0;
299 /* Return true if strinfo vector is shared with the immediate dominator. */
301 static inline bool
302 strinfo_shared (void)
304 return vec_safe_length (stridx_to_strinfo)
305 && (*stridx_to_strinfo)[0] != NULL;
308 /* Unshare strinfo vector that is shared with the immediate dominator. */
310 static void
311 unshare_strinfo_vec (void)
313 strinfo si;
314 unsigned int i = 0;
316 gcc_assert (strinfo_shared ());
317 stridx_to_strinfo = vec_safe_copy (stridx_to_strinfo);
318 for (i = 1; vec_safe_iterate (stridx_to_strinfo, i, &si); ++i)
319 if (si != NULL)
320 si->refcount++;
321 (*stridx_to_strinfo)[0] = NULL;
324 /* Attempt to create a string index for exp, ADDR_EXPR's operand.
325 Return a pointer to the location where the string index can
326 be stored (if 0) or is stored, or NULL if this can't be tracked. */
328 static int *
329 addr_stridxptr (tree exp)
331 HOST_WIDE_INT off;
333 tree base = get_addr_base_and_unit_offset (exp, &off);
334 if (base == NULL_TREE || !DECL_P (base))
335 return NULL;
337 if (!decl_to_stridxlist_htab)
339 decl_to_stridxlist_htab
340 = new hash_map<tree, stridxlist, stridxlist_hash_traits> (64);
341 gcc_obstack_init (&stridx_obstack);
344 bool existed;
345 stridxlist *list = &decl_to_stridxlist_htab->get_or_insert (base, &existed);
346 if (existed)
348 int i;
349 for (i = 0; i < 16; i++)
351 if (list->offset == off)
352 return &list->idx;
353 if (list->next == NULL)
354 break;
356 if (i == 16)
357 return NULL;
358 list->next = XOBNEW (&stridx_obstack, struct stridxlist);
359 list = list->next;
362 list->next = NULL;
363 list->offset = off;
364 list->idx = 0;
365 return &list->idx;
368 /* Create a new string index, or return 0 if reached limit. */
370 static int
371 new_stridx (tree exp)
373 int idx;
374 if (max_stridx >= PARAM_VALUE (PARAM_MAX_TRACKED_STRLENS))
375 return 0;
376 if (TREE_CODE (exp) == SSA_NAME)
378 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (exp))
379 return 0;
380 idx = max_stridx++;
381 ssa_ver_to_stridx[SSA_NAME_VERSION (exp)] = idx;
382 return idx;
384 if (TREE_CODE (exp) == ADDR_EXPR)
386 int *pidx = addr_stridxptr (TREE_OPERAND (exp, 0));
387 if (pidx != NULL)
389 gcc_assert (*pidx == 0);
390 *pidx = max_stridx++;
391 return *pidx;
394 return 0;
397 /* Like new_stridx, but for ADDR_EXPR's operand instead. */
399 static int
400 new_addr_stridx (tree exp)
402 int *pidx;
403 if (max_stridx >= PARAM_VALUE (PARAM_MAX_TRACKED_STRLENS))
404 return 0;
405 pidx = addr_stridxptr (exp);
406 if (pidx != NULL)
408 gcc_assert (*pidx == 0);
409 *pidx = max_stridx++;
410 return *pidx;
412 return 0;
415 /* Create a new strinfo. */
417 static strinfo
418 new_strinfo (tree ptr, int idx, tree length)
420 strinfo si = strinfo_pool.allocate ();
421 si->length = length;
422 si->ptr = ptr;
423 si->stmt = NULL;
424 si->endptr = NULL_TREE;
425 si->refcount = 1;
426 si->idx = idx;
427 si->first = 0;
428 si->prev = 0;
429 si->next = 0;
430 si->writable = false;
431 si->dont_invalidate = false;
432 return si;
435 /* Decrease strinfo refcount and free it if not referenced anymore. */
437 static inline void
438 free_strinfo (strinfo si)
440 if (si && --si->refcount == 0)
441 strinfo_pool.remove (si);
444 /* Set strinfo in the vector entry IDX to SI. */
446 static inline void
447 set_strinfo (int idx, strinfo si)
449 if (vec_safe_length (stridx_to_strinfo) && (*stridx_to_strinfo)[0])
450 unshare_strinfo_vec ();
451 if (vec_safe_length (stridx_to_strinfo) <= (unsigned int) idx)
452 vec_safe_grow_cleared (stridx_to_strinfo, idx + 1);
453 (*stridx_to_strinfo)[idx] = si;
456 /* Return string length, or NULL if it can't be computed. */
458 static tree
459 get_string_length (strinfo si)
461 if (si->length)
462 return si->length;
464 if (si->stmt)
466 gimple stmt = si->stmt, lenstmt;
467 bool with_bounds = gimple_call_with_bounds_p (stmt);
468 tree callee, lhs, fn, tem;
469 location_t loc;
470 gimple_stmt_iterator gsi;
472 gcc_assert (is_gimple_call (stmt));
473 callee = gimple_call_fndecl (stmt);
474 gcc_assert (callee && DECL_BUILT_IN_CLASS (callee) == BUILT_IN_NORMAL);
475 lhs = gimple_call_lhs (stmt);
476 /* unshare_strinfo is intentionally not called here. The (delayed)
477 transformation of strcpy or strcat into stpcpy is done at the place
478 of the former strcpy/strcat call and so can affect all the strinfos
479 with the same stmt. If they were unshared before and transformation
480 has been already done, the handling of BUILT_IN_STPCPY{,_CHK} should
481 just compute the right length. */
482 switch (DECL_FUNCTION_CODE (callee))
484 case BUILT_IN_STRCAT:
485 case BUILT_IN_STRCAT_CHK:
486 case BUILT_IN_STRCAT_CHKP:
487 case BUILT_IN_STRCAT_CHK_CHKP:
488 gsi = gsi_for_stmt (stmt);
489 fn = builtin_decl_implicit (BUILT_IN_STRLEN);
490 gcc_assert (lhs == NULL_TREE);
491 tem = unshare_expr (gimple_call_arg (stmt, 0));
492 if (with_bounds)
494 lenstmt = gimple_build_call (chkp_maybe_create_clone (fn)->decl,
495 2, tem, gimple_call_arg (stmt, 1));
496 gimple_call_set_with_bounds (lenstmt, true);
498 else
499 lenstmt = gimple_build_call (fn, 1, tem);
500 lhs = make_ssa_name (TREE_TYPE (TREE_TYPE (fn)), lenstmt);
501 gimple_call_set_lhs (lenstmt, lhs);
502 gimple_set_vuse (lenstmt, gimple_vuse (stmt));
503 gsi_insert_before (&gsi, lenstmt, GSI_SAME_STMT);
504 tem = gimple_call_arg (stmt, 0);
505 if (!ptrofftype_p (TREE_TYPE (lhs)))
507 lhs = convert_to_ptrofftype (lhs);
508 lhs = force_gimple_operand_gsi (&gsi, lhs, true, NULL_TREE,
509 true, GSI_SAME_STMT);
511 lenstmt = gimple_build_assign
512 (make_ssa_name (TREE_TYPE (gimple_call_arg (stmt, 0))),
513 POINTER_PLUS_EXPR,tem, lhs);
514 gsi_insert_before (&gsi, lenstmt, GSI_SAME_STMT);
515 gimple_call_set_arg (stmt, 0, gimple_assign_lhs (lenstmt));
516 lhs = NULL_TREE;
517 /* FALLTHRU */
518 case BUILT_IN_STRCPY:
519 case BUILT_IN_STRCPY_CHK:
520 case BUILT_IN_STRCPY_CHKP:
521 case BUILT_IN_STRCPY_CHK_CHKP:
522 gcc_assert (builtin_decl_implicit_p (BUILT_IN_STPCPY));
523 if (gimple_call_num_args (stmt) == (with_bounds ? 4 : 2))
524 fn = builtin_decl_implicit (BUILT_IN_STPCPY);
525 else
526 fn = builtin_decl_explicit (BUILT_IN_STPCPY_CHK);
527 if (with_bounds)
528 fn = chkp_maybe_create_clone (fn)->decl;
529 gcc_assert (lhs == NULL_TREE);
530 if (dump_file && (dump_flags & TDF_DETAILS) != 0)
532 fprintf (dump_file, "Optimizing: ");
533 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
535 gimple_call_set_fndecl (stmt, fn);
536 lhs = make_ssa_name (TREE_TYPE (TREE_TYPE (fn)), stmt);
537 gimple_call_set_lhs (stmt, lhs);
538 update_stmt (stmt);
539 if (dump_file && (dump_flags & TDF_DETAILS) != 0)
541 fprintf (dump_file, "into: ");
542 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
544 /* FALLTHRU */
545 case BUILT_IN_STPCPY:
546 case BUILT_IN_STPCPY_CHK:
547 case BUILT_IN_STPCPY_CHKP:
548 case BUILT_IN_STPCPY_CHK_CHKP:
549 gcc_assert (lhs != NULL_TREE);
550 loc = gimple_location (stmt);
551 si->endptr = lhs;
552 si->stmt = NULL;
553 lhs = fold_convert_loc (loc, size_type_node, lhs);
554 si->length = fold_convert_loc (loc, size_type_node, si->ptr);
555 si->length = fold_build2_loc (loc, MINUS_EXPR, size_type_node,
556 lhs, si->length);
557 break;
558 case BUILT_IN_MALLOC:
559 break;
560 /* BUILT_IN_CALLOC always has si->length set. */
561 default:
562 gcc_unreachable ();
563 break;
567 return si->length;
570 /* Invalidate string length information for strings whose length
571 might change due to stores in stmt. */
573 static bool
574 maybe_invalidate (gimple stmt)
576 strinfo si;
577 unsigned int i;
578 bool nonempty = false;
580 for (i = 1; vec_safe_iterate (stridx_to_strinfo, i, &si); ++i)
581 if (si != NULL)
583 if (!si->dont_invalidate)
585 ao_ref r;
586 /* Do not use si->length. */
587 ao_ref_init_from_ptr_and_size (&r, si->ptr, NULL_TREE);
588 if (stmt_may_clobber_ref_p_1 (stmt, &r))
590 set_strinfo (i, NULL);
591 free_strinfo (si);
592 continue;
595 si->dont_invalidate = false;
596 nonempty = true;
598 return nonempty;
601 /* Unshare strinfo record SI, if it has refcount > 1 or
602 if stridx_to_strinfo vector is shared with some other
603 bbs. */
605 static strinfo
606 unshare_strinfo (strinfo si)
608 strinfo nsi;
610 if (si->refcount == 1 && !strinfo_shared ())
611 return si;
613 nsi = new_strinfo (si->ptr, si->idx, si->length);
614 nsi->stmt = si->stmt;
615 nsi->endptr = si->endptr;
616 nsi->first = si->first;
617 nsi->prev = si->prev;
618 nsi->next = si->next;
619 nsi->writable = si->writable;
620 set_strinfo (si->idx, nsi);
621 free_strinfo (si);
622 return nsi;
625 /* Return first strinfo in the related strinfo chain
626 if all strinfos in between belong to the chain, otherwise
627 NULL. */
629 static strinfo
630 verify_related_strinfos (strinfo origsi)
632 strinfo si = origsi, psi;
634 if (origsi->first == 0)
635 return NULL;
636 for (; si->prev; si = psi)
638 if (si->first != origsi->first)
639 return NULL;
640 psi = get_strinfo (si->prev);
641 if (psi == NULL)
642 return NULL;
643 if (psi->next != si->idx)
644 return NULL;
646 if (si->idx != si->first)
647 return NULL;
648 return si;
651 /* Attempt to create a new strinfo for BASESI + OFF, or find existing
652 strinfo if there is any. Return it's idx, or 0 if no strinfo has
653 been created. */
655 static int
656 get_stridx_plus_constant (strinfo basesi, HOST_WIDE_INT off, tree ptr)
658 gcc_checking_assert (TREE_CODE (ptr) == SSA_NAME);
660 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ptr))
661 return 0;
663 if (basesi->length == NULL_TREE
664 || TREE_CODE (basesi->length) != INTEGER_CST
665 || compare_tree_int (basesi->length, off) == -1
666 || !tree_fits_shwi_p (basesi->length))
667 return 0;
669 HOST_WIDE_INT len = tree_to_shwi (basesi->length) - off;
670 strinfo si = basesi, chainsi;
671 if (si->first || si->prev || si->next)
672 si = verify_related_strinfos (basesi);
673 if (si == NULL
674 || si->length == NULL_TREE
675 || TREE_CODE (si->length) != INTEGER_CST)
676 return 0;
678 if (ssa_ver_to_stridx.length () <= SSA_NAME_VERSION (ptr))
679 ssa_ver_to_stridx.safe_grow_cleared (num_ssa_names);
681 gcc_checking_assert (compare_tree_int (si->length, off) != -1);
682 for (chainsi = si; chainsi->next; chainsi = si)
684 si = get_strinfo (chainsi->next);
685 if (si == NULL
686 || si->first != chainsi->first
687 || si->prev != chainsi->idx
688 || si->length == NULL_TREE
689 || TREE_CODE (si->length) != INTEGER_CST)
690 break;
691 int r = compare_tree_int (si->length, len);
692 if (r != 1)
694 if (r == 0)
696 ssa_ver_to_stridx[SSA_NAME_VERSION (ptr)] = si->idx;
697 return si->idx;
699 break;
703 int idx = new_stridx (ptr);
704 if (idx == 0)
705 return 0;
706 si = new_strinfo (ptr, idx, build_int_cst (size_type_node, len));
707 set_strinfo (idx, si);
708 if (chainsi->next)
710 strinfo nextsi = unshare_strinfo (get_strinfo (chainsi->next));
711 si->next = nextsi->idx;
712 nextsi->prev = idx;
714 chainsi = unshare_strinfo (chainsi);
715 if (chainsi->first == 0)
716 chainsi->first = chainsi->idx;
717 chainsi->next = idx;
718 if (chainsi->endptr == NULL_TREE && len == 0)
719 chainsi->endptr = ptr;
720 si->endptr = chainsi->endptr;
721 si->prev = chainsi->idx;
722 si->first = chainsi->first;
723 si->writable = chainsi->writable;
724 return si->idx;
727 /* Note that PTR, a pointer SSA_NAME initialized in the current stmt, points
728 to a zero-length string and if possible chain it to a related strinfo
729 chain whose part is or might be CHAINSI. */
731 static strinfo
732 zero_length_string (tree ptr, strinfo chainsi)
734 strinfo si;
735 int idx;
736 if (ssa_ver_to_stridx.length () <= SSA_NAME_VERSION (ptr))
737 ssa_ver_to_stridx.safe_grow_cleared (num_ssa_names);
738 gcc_checking_assert (TREE_CODE (ptr) == SSA_NAME
739 && ssa_ver_to_stridx[SSA_NAME_VERSION (ptr)] == 0);
741 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ptr))
742 return NULL;
743 if (chainsi != NULL)
745 si = verify_related_strinfos (chainsi);
746 if (si)
748 chainsi = si;
749 for (; chainsi->next; chainsi = si)
751 if (chainsi->endptr == NULL_TREE)
753 chainsi = unshare_strinfo (chainsi);
754 chainsi->endptr = ptr;
756 si = get_strinfo (chainsi->next);
757 if (si == NULL
758 || si->first != chainsi->first
759 || si->prev != chainsi->idx)
760 break;
762 gcc_assert (chainsi->length || chainsi->stmt);
763 if (chainsi->endptr == NULL_TREE)
765 chainsi = unshare_strinfo (chainsi);
766 chainsi->endptr = ptr;
768 if (chainsi->length && integer_zerop (chainsi->length))
770 if (chainsi->next)
772 chainsi = unshare_strinfo (chainsi);
773 chainsi->next = 0;
775 ssa_ver_to_stridx[SSA_NAME_VERSION (ptr)] = chainsi->idx;
776 return chainsi;
779 else if (chainsi->first || chainsi->prev || chainsi->next)
781 chainsi = unshare_strinfo (chainsi);
782 chainsi->first = 0;
783 chainsi->prev = 0;
784 chainsi->next = 0;
787 idx = new_stridx (ptr);
788 if (idx == 0)
789 return NULL;
790 si = new_strinfo (ptr, idx, build_int_cst (size_type_node, 0));
791 set_strinfo (idx, si);
792 si->endptr = ptr;
793 if (chainsi != NULL)
795 chainsi = unshare_strinfo (chainsi);
796 if (chainsi->first == 0)
797 chainsi->first = chainsi->idx;
798 chainsi->next = idx;
799 if (chainsi->endptr == NULL_TREE)
800 chainsi->endptr = ptr;
801 si->prev = chainsi->idx;
802 si->first = chainsi->first;
803 si->writable = chainsi->writable;
805 return si;
808 /* For strinfo ORIGSI whose length has been just updated
809 update also related strinfo lengths (add ADJ to each,
810 but don't adjust ORIGSI). */
812 static void
813 adjust_related_strinfos (location_t loc, strinfo origsi, tree adj)
815 strinfo si = verify_related_strinfos (origsi);
817 if (si == NULL)
818 return;
820 while (1)
822 strinfo nsi;
824 if (si != origsi)
826 tree tem;
828 si = unshare_strinfo (si);
829 if (si->length)
831 tem = fold_convert_loc (loc, TREE_TYPE (si->length), adj);
832 si->length = fold_build2_loc (loc, PLUS_EXPR,
833 TREE_TYPE (si->length), si->length,
834 tem);
836 else if (si->stmt != NULL)
837 /* Delayed length computation is unaffected. */
839 else
840 gcc_unreachable ();
842 si->endptr = NULL_TREE;
843 si->dont_invalidate = true;
845 if (si->next == 0)
846 return;
847 nsi = get_strinfo (si->next);
848 if (nsi == NULL
849 || nsi->first != si->first
850 || nsi->prev != si->idx)
851 return;
852 si = nsi;
856 /* Find if there are other SSA_NAME pointers equal to PTR
857 for which we don't track their string lengths yet. If so, use
858 IDX for them. */
860 static void
861 find_equal_ptrs (tree ptr, int idx)
863 if (TREE_CODE (ptr) != SSA_NAME)
864 return;
865 while (1)
867 gimple stmt = SSA_NAME_DEF_STMT (ptr);
868 if (!is_gimple_assign (stmt))
869 return;
870 ptr = gimple_assign_rhs1 (stmt);
871 switch (gimple_assign_rhs_code (stmt))
873 case SSA_NAME:
874 break;
875 CASE_CONVERT:
876 if (!POINTER_TYPE_P (TREE_TYPE (ptr)))
877 return;
878 if (TREE_CODE (ptr) == SSA_NAME)
879 break;
880 if (TREE_CODE (ptr) != ADDR_EXPR)
881 return;
882 /* FALLTHRU */
883 case ADDR_EXPR:
885 int *pidx = addr_stridxptr (TREE_OPERAND (ptr, 0));
886 if (pidx != NULL && *pidx == 0)
887 *pidx = idx;
888 return;
890 default:
891 return;
894 /* We might find an endptr created in this pass. Grow the
895 vector in that case. */
896 if (ssa_ver_to_stridx.length () <= SSA_NAME_VERSION (ptr))
897 ssa_ver_to_stridx.safe_grow_cleared (num_ssa_names);
899 if (ssa_ver_to_stridx[SSA_NAME_VERSION (ptr)] != 0)
900 return;
901 ssa_ver_to_stridx[SSA_NAME_VERSION (ptr)] = idx;
905 /* If the last .MEM setter statement before STMT is
906 memcpy (x, y, strlen (y) + 1), the only .MEM use of it is STMT
907 and STMT is known to overwrite x[strlen (x)], adjust the last memcpy to
908 just memcpy (x, y, strlen (y)). SI must be the zero length
909 strinfo. */
911 static void
912 adjust_last_stmt (strinfo si, gimple stmt, bool is_strcat)
914 tree vuse, callee, len;
915 struct laststmt_struct last = laststmt;
916 strinfo lastsi, firstsi;
917 unsigned len_arg_no = 2;
919 laststmt.stmt = NULL;
920 laststmt.len = NULL_TREE;
921 laststmt.stridx = 0;
923 if (last.stmt == NULL)
924 return;
926 vuse = gimple_vuse (stmt);
927 if (vuse == NULL_TREE
928 || SSA_NAME_DEF_STMT (vuse) != last.stmt
929 || !has_single_use (vuse))
930 return;
932 gcc_assert (last.stridx > 0);
933 lastsi = get_strinfo (last.stridx);
934 if (lastsi == NULL)
935 return;
937 if (lastsi != si)
939 if (lastsi->first == 0 || lastsi->first != si->first)
940 return;
942 firstsi = verify_related_strinfos (si);
943 if (firstsi == NULL)
944 return;
945 while (firstsi != lastsi)
947 strinfo nextsi;
948 if (firstsi->next == 0)
949 return;
950 nextsi = get_strinfo (firstsi->next);
951 if (nextsi == NULL
952 || nextsi->prev != firstsi->idx
953 || nextsi->first != si->first)
954 return;
955 firstsi = nextsi;
959 if (!is_strcat)
961 if (si->length == NULL_TREE || !integer_zerop (si->length))
962 return;
965 if (is_gimple_assign (last.stmt))
967 gimple_stmt_iterator gsi;
969 if (!integer_zerop (gimple_assign_rhs1 (last.stmt)))
970 return;
971 if (stmt_could_throw_p (last.stmt))
972 return;
973 gsi = gsi_for_stmt (last.stmt);
974 unlink_stmt_vdef (last.stmt);
975 release_defs (last.stmt);
976 gsi_remove (&gsi, true);
977 return;
980 if (!gimple_call_builtin_p (last.stmt, BUILT_IN_NORMAL))
981 return;
983 callee = gimple_call_fndecl (last.stmt);
984 switch (DECL_FUNCTION_CODE (callee))
986 case BUILT_IN_MEMCPY:
987 case BUILT_IN_MEMCPY_CHK:
988 break;
989 case BUILT_IN_MEMCPY_CHKP:
990 case BUILT_IN_MEMCPY_CHK_CHKP:
991 len_arg_no = 4;
992 break;
993 default:
994 return;
997 len = gimple_call_arg (last.stmt, len_arg_no);
998 if (tree_fits_uhwi_p (len))
1000 if (!tree_fits_uhwi_p (last.len)
1001 || integer_zerop (len)
1002 || tree_to_uhwi (len) != tree_to_uhwi (last.len) + 1)
1003 return;
1004 /* Don't adjust the length if it is divisible by 4, it is more efficient
1005 to store the extra '\0' in that case. */
1006 if ((tree_to_uhwi (len) & 3) == 0)
1007 return;
1009 else if (TREE_CODE (len) == SSA_NAME)
1011 gimple def_stmt = SSA_NAME_DEF_STMT (len);
1012 if (!is_gimple_assign (def_stmt)
1013 || gimple_assign_rhs_code (def_stmt) != PLUS_EXPR
1014 || gimple_assign_rhs1 (def_stmt) != last.len
1015 || !integer_onep (gimple_assign_rhs2 (def_stmt)))
1016 return;
1018 else
1019 return;
1021 gimple_call_set_arg (last.stmt, len_arg_no, last.len);
1022 update_stmt (last.stmt);
1025 /* Handle a strlen call. If strlen of the argument is known, replace
1026 the strlen call with the known value, otherwise remember that strlen
1027 of the argument is stored in the lhs SSA_NAME. */
1029 static void
1030 handle_builtin_strlen (gimple_stmt_iterator *gsi)
1032 int idx;
1033 tree src;
1034 gimple stmt = gsi_stmt (*gsi);
1035 tree lhs = gimple_call_lhs (stmt);
1037 if (lhs == NULL_TREE)
1038 return;
1040 src = gimple_call_arg (stmt, 0);
1041 idx = get_stridx (src);
1042 if (idx)
1044 strinfo si = NULL;
1045 tree rhs;
1047 if (idx < 0)
1048 rhs = build_int_cst (TREE_TYPE (lhs), ~idx);
1049 else
1051 rhs = NULL_TREE;
1052 si = get_strinfo (idx);
1053 if (si != NULL)
1054 rhs = get_string_length (si);
1056 if (rhs != NULL_TREE)
1058 if (dump_file && (dump_flags & TDF_DETAILS) != 0)
1060 fprintf (dump_file, "Optimizing: ");
1061 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
1063 rhs = unshare_expr (rhs);
1064 if (!useless_type_conversion_p (TREE_TYPE (lhs), TREE_TYPE (rhs)))
1065 rhs = fold_convert_loc (gimple_location (stmt),
1066 TREE_TYPE (lhs), rhs);
1067 if (!update_call_from_tree (gsi, rhs))
1068 gimplify_and_update_call_from_tree (gsi, rhs);
1069 stmt = gsi_stmt (*gsi);
1070 update_stmt (stmt);
1071 if (dump_file && (dump_flags & TDF_DETAILS) != 0)
1073 fprintf (dump_file, "into: ");
1074 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
1076 if (si != NULL
1077 && TREE_CODE (si->length) != SSA_NAME
1078 && TREE_CODE (si->length) != INTEGER_CST
1079 && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (lhs))
1081 si = unshare_strinfo (si);
1082 si->length = lhs;
1084 return;
1087 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (lhs))
1088 return;
1089 if (idx == 0)
1090 idx = new_stridx (src);
1091 else if (get_strinfo (idx) != NULL)
1092 return;
1093 if (idx)
1095 strinfo si = new_strinfo (src, idx, lhs);
1096 set_strinfo (idx, si);
1097 find_equal_ptrs (src, idx);
1101 /* Handle a strchr call. If strlen of the first argument is known, replace
1102 the strchr (x, 0) call with the endptr or x + strlen, otherwise remember
1103 that lhs of the call is endptr and strlen of the argument is endptr - x. */
1105 static void
1106 handle_builtin_strchr (gimple_stmt_iterator *gsi)
1108 int idx;
1109 tree src;
1110 gimple stmt = gsi_stmt (*gsi);
1111 tree lhs = gimple_call_lhs (stmt);
1112 bool with_bounds = gimple_call_with_bounds_p (stmt);
1114 if (lhs == NULL_TREE)
1115 return;
1117 if (!integer_zerop (gimple_call_arg (stmt, with_bounds ? 2 : 1)))
1118 return;
1120 src = gimple_call_arg (stmt, 0);
1121 idx = get_stridx (src);
1122 if (idx)
1124 strinfo si = NULL;
1125 tree rhs;
1127 if (idx < 0)
1128 rhs = build_int_cst (size_type_node, ~idx);
1129 else
1131 rhs = NULL_TREE;
1132 si = get_strinfo (idx);
1133 if (si != NULL)
1134 rhs = get_string_length (si);
1136 if (rhs != NULL_TREE)
1138 location_t loc = gimple_location (stmt);
1140 if (dump_file && (dump_flags & TDF_DETAILS) != 0)
1142 fprintf (dump_file, "Optimizing: ");
1143 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
1145 if (si != NULL && si->endptr != NULL_TREE)
1147 rhs = unshare_expr (si->endptr);
1148 if (!useless_type_conversion_p (TREE_TYPE (lhs),
1149 TREE_TYPE (rhs)))
1150 rhs = fold_convert_loc (loc, TREE_TYPE (lhs), rhs);
1152 else
1154 rhs = fold_convert_loc (loc, sizetype, unshare_expr (rhs));
1155 rhs = fold_build2_loc (loc, POINTER_PLUS_EXPR,
1156 TREE_TYPE (src), src, rhs);
1157 if (!useless_type_conversion_p (TREE_TYPE (lhs),
1158 TREE_TYPE (rhs)))
1159 rhs = fold_convert_loc (loc, TREE_TYPE (lhs), rhs);
1161 if (!update_call_from_tree (gsi, rhs))
1162 gimplify_and_update_call_from_tree (gsi, rhs);
1163 stmt = gsi_stmt (*gsi);
1164 update_stmt (stmt);
1165 if (dump_file && (dump_flags & TDF_DETAILS) != 0)
1167 fprintf (dump_file, "into: ");
1168 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
1170 if (si != NULL
1171 && si->endptr == NULL_TREE
1172 && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (lhs))
1174 si = unshare_strinfo (si);
1175 si->endptr = lhs;
1177 zero_length_string (lhs, si);
1178 return;
1181 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (lhs))
1182 return;
1183 if (TREE_CODE (src) != SSA_NAME || !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (src))
1185 if (idx == 0)
1186 idx = new_stridx (src);
1187 else if (get_strinfo (idx) != NULL)
1189 zero_length_string (lhs, NULL);
1190 return;
1192 if (idx)
1194 location_t loc = gimple_location (stmt);
1195 tree lhsu = fold_convert_loc (loc, size_type_node, lhs);
1196 tree srcu = fold_convert_loc (loc, size_type_node, src);
1197 tree length = fold_build2_loc (loc, MINUS_EXPR,
1198 size_type_node, lhsu, srcu);
1199 strinfo si = new_strinfo (src, idx, length);
1200 si->endptr = lhs;
1201 set_strinfo (idx, si);
1202 find_equal_ptrs (src, idx);
1203 zero_length_string (lhs, si);
1206 else
1207 zero_length_string (lhs, NULL);
1210 /* Handle a strcpy-like ({st{r,p}cpy,__st{r,p}cpy_chk}) call.
1211 If strlen of the second argument is known, strlen of the first argument
1212 is the same after this call. Furthermore, attempt to convert it to
1213 memcpy. */
1215 static void
1216 handle_builtin_strcpy (enum built_in_function bcode, gimple_stmt_iterator *gsi)
1218 int idx, didx;
1219 tree src, dst, srclen, len, lhs, args, type, fn, oldlen;
1220 bool success;
1221 gimple stmt = gsi_stmt (*gsi);
1222 strinfo si, dsi, olddsi, zsi;
1223 location_t loc;
1224 bool with_bounds = gimple_call_with_bounds_p (stmt);
1226 src = gimple_call_arg (stmt, with_bounds ? 2 : 1);
1227 dst = gimple_call_arg (stmt, 0);
1228 lhs = gimple_call_lhs (stmt);
1229 idx = get_stridx (src);
1230 si = NULL;
1231 if (idx > 0)
1232 si = get_strinfo (idx);
1234 didx = get_stridx (dst);
1235 olddsi = NULL;
1236 oldlen = NULL_TREE;
1237 if (didx > 0)
1238 olddsi = get_strinfo (didx);
1239 else if (didx < 0)
1240 return;
1242 if (olddsi != NULL)
1243 adjust_last_stmt (olddsi, stmt, false);
1245 srclen = NULL_TREE;
1246 if (si != NULL)
1247 srclen = get_string_length (si);
1248 else if (idx < 0)
1249 srclen = build_int_cst (size_type_node, ~idx);
1251 loc = gimple_location (stmt);
1252 if (srclen == NULL_TREE)
1253 switch (bcode)
1255 case BUILT_IN_STRCPY:
1256 case BUILT_IN_STRCPY_CHK:
1257 case BUILT_IN_STRCPY_CHKP:
1258 case BUILT_IN_STRCPY_CHK_CHKP:
1259 if (lhs != NULL_TREE || !builtin_decl_implicit_p (BUILT_IN_STPCPY))
1260 return;
1261 break;
1262 case BUILT_IN_STPCPY:
1263 case BUILT_IN_STPCPY_CHK:
1264 case BUILT_IN_STPCPY_CHKP:
1265 case BUILT_IN_STPCPY_CHK_CHKP:
1266 if (lhs == NULL_TREE)
1267 return;
1268 else
1270 tree lhsuint = fold_convert_loc (loc, size_type_node, lhs);
1271 srclen = fold_convert_loc (loc, size_type_node, dst);
1272 srclen = fold_build2_loc (loc, MINUS_EXPR, size_type_node,
1273 lhsuint, srclen);
1275 break;
1276 default:
1277 gcc_unreachable ();
1280 if (didx == 0)
1282 didx = new_stridx (dst);
1283 if (didx == 0)
1284 return;
1286 if (olddsi != NULL)
1288 oldlen = olddsi->length;
1289 dsi = unshare_strinfo (olddsi);
1290 dsi->length = srclen;
1291 /* Break the chain, so adjust_related_strinfo on later pointers in
1292 the chain won't adjust this one anymore. */
1293 dsi->next = 0;
1294 dsi->stmt = NULL;
1295 dsi->endptr = NULL_TREE;
1297 else
1299 dsi = new_strinfo (dst, didx, srclen);
1300 set_strinfo (didx, dsi);
1301 find_equal_ptrs (dst, didx);
1303 dsi->writable = true;
1304 dsi->dont_invalidate = true;
1306 if (dsi->length == NULL_TREE)
1308 strinfo chainsi;
1310 /* If string length of src is unknown, use delayed length
1311 computation. If string lenth of dst will be needed, it
1312 can be computed by transforming this strcpy call into
1313 stpcpy and subtracting dst from the return value. */
1315 /* Look for earlier strings whose length could be determined if
1316 this strcpy is turned into an stpcpy. */
1318 if (dsi->prev != 0 && (chainsi = verify_related_strinfos (dsi)) != NULL)
1320 for (; chainsi && chainsi != dsi; chainsi = get_strinfo (chainsi->next))
1322 /* When setting a stmt for delayed length computation
1323 prevent all strinfos through dsi from being
1324 invalidated. */
1325 chainsi = unshare_strinfo (chainsi);
1326 chainsi->stmt = stmt;
1327 chainsi->length = NULL_TREE;
1328 chainsi->endptr = NULL_TREE;
1329 chainsi->dont_invalidate = true;
1332 dsi->stmt = stmt;
1333 return;
1336 if (olddsi != NULL)
1338 tree adj = NULL_TREE;
1339 if (oldlen == NULL_TREE)
1341 else if (integer_zerop (oldlen))
1342 adj = srclen;
1343 else if (TREE_CODE (oldlen) == INTEGER_CST
1344 || TREE_CODE (srclen) == INTEGER_CST)
1345 adj = fold_build2_loc (loc, MINUS_EXPR,
1346 TREE_TYPE (srclen), srclen,
1347 fold_convert_loc (loc, TREE_TYPE (srclen),
1348 oldlen));
1349 if (adj != NULL_TREE)
1350 adjust_related_strinfos (loc, dsi, adj);
1351 else
1352 dsi->prev = 0;
1354 /* strcpy src may not overlap dst, so src doesn't need to be
1355 invalidated either. */
1356 if (si != NULL)
1357 si->dont_invalidate = true;
1359 fn = NULL_TREE;
1360 zsi = NULL;
1361 switch (bcode)
1363 case BUILT_IN_STRCPY:
1364 case BUILT_IN_STRCPY_CHKP:
1365 fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
1366 if (lhs)
1367 ssa_ver_to_stridx[SSA_NAME_VERSION (lhs)] = didx;
1368 break;
1369 case BUILT_IN_STRCPY_CHK:
1370 case BUILT_IN_STRCPY_CHK_CHKP:
1371 fn = builtin_decl_explicit (BUILT_IN_MEMCPY_CHK);
1372 if (lhs)
1373 ssa_ver_to_stridx[SSA_NAME_VERSION (lhs)] = didx;
1374 break;
1375 case BUILT_IN_STPCPY:
1376 case BUILT_IN_STPCPY_CHKP:
1377 /* This would need adjustment of the lhs (subtract one),
1378 or detection that the trailing '\0' doesn't need to be
1379 written, if it will be immediately overwritten.
1380 fn = builtin_decl_explicit (BUILT_IN_MEMPCPY); */
1381 if (lhs)
1383 dsi->endptr = lhs;
1384 zsi = zero_length_string (lhs, dsi);
1386 break;
1387 case BUILT_IN_STPCPY_CHK:
1388 case BUILT_IN_STPCPY_CHK_CHKP:
1389 /* This would need adjustment of the lhs (subtract one),
1390 or detection that the trailing '\0' doesn't need to be
1391 written, if it will be immediately overwritten.
1392 fn = builtin_decl_explicit (BUILT_IN_MEMPCPY_CHK); */
1393 if (lhs)
1395 dsi->endptr = lhs;
1396 zsi = zero_length_string (lhs, dsi);
1398 break;
1399 default:
1400 gcc_unreachable ();
1402 if (zsi != NULL)
1403 zsi->dont_invalidate = true;
1405 if (fn == NULL_TREE)
1406 return;
1408 args = TYPE_ARG_TYPES (TREE_TYPE (fn));
1409 type = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (args)));
1411 len = fold_convert_loc (loc, type, unshare_expr (srclen));
1412 len = fold_build2_loc (loc, PLUS_EXPR, type, len, build_int_cst (type, 1));
1413 len = force_gimple_operand_gsi (gsi, len, true, NULL_TREE, true,
1414 GSI_SAME_STMT);
1415 if (dump_file && (dump_flags & TDF_DETAILS) != 0)
1417 fprintf (dump_file, "Optimizing: ");
1418 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
1420 if (with_bounds)
1422 fn = chkp_maybe_create_clone (fn)->decl;
1423 if (gimple_call_num_args (stmt) == 4)
1424 success = update_gimple_call (gsi, fn, 5, dst,
1425 gimple_call_arg (stmt, 1),
1426 src,
1427 gimple_call_arg (stmt, 3),
1428 len);
1429 else
1430 success = update_gimple_call (gsi, fn, 6, dst,
1431 gimple_call_arg (stmt, 1),
1432 src,
1433 gimple_call_arg (stmt, 3),
1434 len,
1435 gimple_call_arg (stmt, 4));
1437 else
1438 if (gimple_call_num_args (stmt) == 2)
1439 success = update_gimple_call (gsi, fn, 3, dst, src, len);
1440 else
1441 success = update_gimple_call (gsi, fn, 4, dst, src, len,
1442 gimple_call_arg (stmt, 2));
1443 if (success)
1445 stmt = gsi_stmt (*gsi);
1446 gimple_call_set_with_bounds (stmt, with_bounds);
1447 update_stmt (stmt);
1448 if (dump_file && (dump_flags & TDF_DETAILS) != 0)
1450 fprintf (dump_file, "into: ");
1451 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
1453 /* Allow adjust_last_stmt to decrease this memcpy's size. */
1454 laststmt.stmt = stmt;
1455 laststmt.len = srclen;
1456 laststmt.stridx = dsi->idx;
1458 else if (dump_file && (dump_flags & TDF_DETAILS) != 0)
1459 fprintf (dump_file, "not possible.\n");
1462 /* Handle a memcpy-like ({mem{,p}cpy,__mem{,p}cpy_chk}) call.
1463 If strlen of the second argument is known and length of the third argument
1464 is that plus one, strlen of the first argument is the same after this
1465 call. */
1467 static void
1468 handle_builtin_memcpy (enum built_in_function bcode, gimple_stmt_iterator *gsi)
1470 int idx, didx;
1471 tree src, dst, len, lhs, oldlen, newlen;
1472 gimple stmt = gsi_stmt (*gsi);
1473 strinfo si, dsi, olddsi;
1474 bool with_bounds = gimple_call_with_bounds_p (stmt);
1476 len = gimple_call_arg (stmt, with_bounds ? 4 : 2);
1477 src = gimple_call_arg (stmt, with_bounds ? 2 : 1);
1478 dst = gimple_call_arg (stmt, 0);
1479 idx = get_stridx (src);
1480 if (idx == 0)
1481 return;
1483 didx = get_stridx (dst);
1484 olddsi = NULL;
1485 if (didx > 0)
1486 olddsi = get_strinfo (didx);
1487 else if (didx < 0)
1488 return;
1490 if (olddsi != NULL
1491 && tree_fits_uhwi_p (len)
1492 && !integer_zerop (len))
1493 adjust_last_stmt (olddsi, stmt, false);
1495 if (idx > 0)
1497 gimple def_stmt;
1499 /* Handle memcpy (x, y, l) where l is strlen (y) + 1. */
1500 si = get_strinfo (idx);
1501 if (si == NULL || si->length == NULL_TREE)
1502 return;
1503 if (TREE_CODE (len) != SSA_NAME)
1504 return;
1505 def_stmt = SSA_NAME_DEF_STMT (len);
1506 if (!is_gimple_assign (def_stmt)
1507 || gimple_assign_rhs_code (def_stmt) != PLUS_EXPR
1508 || gimple_assign_rhs1 (def_stmt) != si->length
1509 || !integer_onep (gimple_assign_rhs2 (def_stmt)))
1510 return;
1512 else
1514 si = NULL;
1515 /* Handle memcpy (x, "abcd", 5) or
1516 memcpy (x, "abc\0uvw", 7). */
1517 if (!tree_fits_uhwi_p (len)
1518 || tree_to_uhwi (len) <= (unsigned HOST_WIDE_INT) ~idx)
1519 return;
1522 if (olddsi != NULL && TREE_CODE (len) == SSA_NAME)
1523 adjust_last_stmt (olddsi, stmt, false);
1525 if (didx == 0)
1527 didx = new_stridx (dst);
1528 if (didx == 0)
1529 return;
1531 if (si != NULL)
1532 newlen = si->length;
1533 else
1534 newlen = build_int_cst (size_type_node, ~idx);
1535 oldlen = NULL_TREE;
1536 if (olddsi != NULL)
1538 dsi = unshare_strinfo (olddsi);
1539 oldlen = olddsi->length;
1540 dsi->length = newlen;
1541 /* Break the chain, so adjust_related_strinfo on later pointers in
1542 the chain won't adjust this one anymore. */
1543 dsi->next = 0;
1544 dsi->stmt = NULL;
1545 dsi->endptr = NULL_TREE;
1547 else
1549 dsi = new_strinfo (dst, didx, newlen);
1550 set_strinfo (didx, dsi);
1551 find_equal_ptrs (dst, didx);
1553 dsi->writable = true;
1554 dsi->dont_invalidate = true;
1555 if (olddsi != NULL)
1557 tree adj = NULL_TREE;
1558 location_t loc = gimple_location (stmt);
1559 if (oldlen == NULL_TREE)
1561 else if (integer_zerop (oldlen))
1562 adj = dsi->length;
1563 else if (TREE_CODE (oldlen) == INTEGER_CST
1564 || TREE_CODE (dsi->length) == INTEGER_CST)
1565 adj = fold_build2_loc (loc, MINUS_EXPR,
1566 TREE_TYPE (dsi->length), dsi->length,
1567 fold_convert_loc (loc, TREE_TYPE (dsi->length),
1568 oldlen));
1569 if (adj != NULL_TREE)
1570 adjust_related_strinfos (loc, dsi, adj);
1571 else
1572 dsi->prev = 0;
1574 /* memcpy src may not overlap dst, so src doesn't need to be
1575 invalidated either. */
1576 if (si != NULL)
1577 si->dont_invalidate = true;
1579 lhs = gimple_call_lhs (stmt);
1580 switch (bcode)
1582 case BUILT_IN_MEMCPY:
1583 case BUILT_IN_MEMCPY_CHK:
1584 case BUILT_IN_MEMCPY_CHKP:
1585 case BUILT_IN_MEMCPY_CHK_CHKP:
1586 /* Allow adjust_last_stmt to decrease this memcpy's size. */
1587 laststmt.stmt = stmt;
1588 laststmt.len = dsi->length;
1589 laststmt.stridx = dsi->idx;
1590 if (lhs)
1591 ssa_ver_to_stridx[SSA_NAME_VERSION (lhs)] = didx;
1592 break;
1593 case BUILT_IN_MEMPCPY:
1594 case BUILT_IN_MEMPCPY_CHK:
1595 case BUILT_IN_MEMPCPY_CHKP:
1596 case BUILT_IN_MEMPCPY_CHK_CHKP:
1597 break;
1598 default:
1599 gcc_unreachable ();
1603 /* Handle a strcat-like ({strcat,__strcat_chk}) call.
1604 If strlen of the second argument is known, strlen of the first argument
1605 is increased by the length of the second argument. Furthermore, attempt
1606 to convert it to memcpy/strcpy if the length of the first argument
1607 is known. */
1609 static void
1610 handle_builtin_strcat (enum built_in_function bcode, gimple_stmt_iterator *gsi)
1612 int idx, didx;
1613 tree src, dst, srclen, dstlen, len, lhs, args, type, fn, objsz, endptr;
1614 bool success;
1615 gimple stmt = gsi_stmt (*gsi);
1616 strinfo si, dsi;
1617 location_t loc;
1618 bool with_bounds = gimple_call_with_bounds_p (stmt);
1620 src = gimple_call_arg (stmt, with_bounds ? 2 : 1);
1621 dst = gimple_call_arg (stmt, 0);
1622 lhs = gimple_call_lhs (stmt);
1624 didx = get_stridx (dst);
1625 if (didx < 0)
1626 return;
1628 dsi = NULL;
1629 if (didx > 0)
1630 dsi = get_strinfo (didx);
1631 if (dsi == NULL || get_string_length (dsi) == NULL_TREE)
1633 /* strcat (p, q) can be transformed into
1634 tmp = p + strlen (p); endptr = strpcpy (tmp, q);
1635 with length endptr - p if we need to compute the length
1636 later on. Don't do this transformation if we don't need
1637 it. */
1638 if (builtin_decl_implicit_p (BUILT_IN_STPCPY) && lhs == NULL_TREE)
1640 if (didx == 0)
1642 didx = new_stridx (dst);
1643 if (didx == 0)
1644 return;
1646 if (dsi == NULL)
1648 dsi = new_strinfo (dst, didx, NULL_TREE);
1649 set_strinfo (didx, dsi);
1650 find_equal_ptrs (dst, didx);
1652 else
1654 dsi = unshare_strinfo (dsi);
1655 dsi->length = NULL_TREE;
1656 dsi->next = 0;
1657 dsi->endptr = NULL_TREE;
1659 dsi->writable = true;
1660 dsi->stmt = stmt;
1661 dsi->dont_invalidate = true;
1663 return;
1666 srclen = NULL_TREE;
1667 si = NULL;
1668 idx = get_stridx (src);
1669 if (idx < 0)
1670 srclen = build_int_cst (size_type_node, ~idx);
1671 else if (idx > 0)
1673 si = get_strinfo (idx);
1674 if (si != NULL)
1675 srclen = get_string_length (si);
1678 loc = gimple_location (stmt);
1679 dstlen = dsi->length;
1680 endptr = dsi->endptr;
1682 dsi = unshare_strinfo (dsi);
1683 dsi->endptr = NULL_TREE;
1684 dsi->stmt = NULL;
1685 dsi->writable = true;
1687 if (srclen != NULL_TREE)
1689 dsi->length = fold_build2_loc (loc, PLUS_EXPR, TREE_TYPE (dsi->length),
1690 dsi->length, srclen);
1691 adjust_related_strinfos (loc, dsi, srclen);
1692 dsi->dont_invalidate = true;
1694 else
1696 dsi->length = NULL;
1697 if (lhs == NULL_TREE && builtin_decl_implicit_p (BUILT_IN_STPCPY))
1698 dsi->dont_invalidate = true;
1701 if (si != NULL)
1702 /* strcat src may not overlap dst, so src doesn't need to be
1703 invalidated either. */
1704 si->dont_invalidate = true;
1706 /* For now. Could remove the lhs from the call and add
1707 lhs = dst; afterwards. */
1708 if (lhs)
1709 return;
1711 fn = NULL_TREE;
1712 objsz = NULL_TREE;
1713 switch (bcode)
1715 case BUILT_IN_STRCAT:
1716 case BUILT_IN_STRCAT_CHKP:
1717 if (srclen != NULL_TREE)
1718 fn = builtin_decl_implicit (BUILT_IN_MEMCPY);
1719 else
1720 fn = builtin_decl_implicit (BUILT_IN_STRCPY);
1721 break;
1722 case BUILT_IN_STRCAT_CHK:
1723 case BUILT_IN_STRCAT_CHK_CHKP:
1724 if (srclen != NULL_TREE)
1725 fn = builtin_decl_explicit (BUILT_IN_MEMCPY_CHK);
1726 else
1727 fn = builtin_decl_explicit (BUILT_IN_STRCPY_CHK);
1728 objsz = gimple_call_arg (stmt, with_bounds ? 4 : 2);
1729 break;
1730 default:
1731 gcc_unreachable ();
1734 if (fn == NULL_TREE)
1735 return;
1737 len = NULL_TREE;
1738 if (srclen != NULL_TREE)
1740 args = TYPE_ARG_TYPES (TREE_TYPE (fn));
1741 type = TREE_VALUE (TREE_CHAIN (TREE_CHAIN (args)));
1743 len = fold_convert_loc (loc, type, unshare_expr (srclen));
1744 len = fold_build2_loc (loc, PLUS_EXPR, type, len,
1745 build_int_cst (type, 1));
1746 len = force_gimple_operand_gsi (gsi, len, true, NULL_TREE, true,
1747 GSI_SAME_STMT);
1749 if (endptr)
1750 dst = fold_convert_loc (loc, TREE_TYPE (dst), unshare_expr (endptr));
1751 else
1752 dst = fold_build2_loc (loc, POINTER_PLUS_EXPR,
1753 TREE_TYPE (dst), unshare_expr (dst),
1754 fold_convert_loc (loc, sizetype,
1755 unshare_expr (dstlen)));
1756 dst = force_gimple_operand_gsi (gsi, dst, true, NULL_TREE, true,
1757 GSI_SAME_STMT);
1758 if (dump_file && (dump_flags & TDF_DETAILS) != 0)
1760 fprintf (dump_file, "Optimizing: ");
1761 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
1763 if (with_bounds)
1765 fn = chkp_maybe_create_clone (fn)->decl;
1766 if (srclen != NULL_TREE)
1767 success = update_gimple_call (gsi, fn, 5 + (objsz != NULL_TREE),
1768 dst,
1769 gimple_call_arg (stmt, 1),
1770 src,
1771 gimple_call_arg (stmt, 3),
1772 len, objsz);
1773 else
1774 success = update_gimple_call (gsi, fn, 4 + (objsz != NULL_TREE),
1775 dst,
1776 gimple_call_arg (stmt, 1),
1777 src,
1778 gimple_call_arg (stmt, 3),
1779 objsz);
1781 else
1782 if (srclen != NULL_TREE)
1783 success = update_gimple_call (gsi, fn, 3 + (objsz != NULL_TREE),
1784 dst, src, len, objsz);
1785 else
1786 success = update_gimple_call (gsi, fn, 2 + (objsz != NULL_TREE),
1787 dst, src, objsz);
1788 if (success)
1790 stmt = gsi_stmt (*gsi);
1791 gimple_call_set_with_bounds (stmt, with_bounds);
1792 update_stmt (stmt);
1793 if (dump_file && (dump_flags & TDF_DETAILS) != 0)
1795 fprintf (dump_file, "into: ");
1796 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
1798 /* If srclen == NULL, note that current string length can be
1799 computed by transforming this strcpy into stpcpy. */
1800 if (srclen == NULL_TREE && dsi->dont_invalidate)
1801 dsi->stmt = stmt;
1802 adjust_last_stmt (dsi, stmt, true);
1803 if (srclen != NULL_TREE)
1805 laststmt.stmt = stmt;
1806 laststmt.len = srclen;
1807 laststmt.stridx = dsi->idx;
1810 else if (dump_file && (dump_flags & TDF_DETAILS) != 0)
1811 fprintf (dump_file, "not possible.\n");
1814 /* Handle a call to malloc or calloc. */
1816 static void
1817 handle_builtin_malloc (enum built_in_function bcode, gimple_stmt_iterator *gsi)
1819 gimple stmt = gsi_stmt (*gsi);
1820 tree lhs = gimple_call_lhs (stmt);
1821 gcc_assert (get_stridx (lhs) == 0);
1822 int idx = new_stridx (lhs);
1823 tree length = NULL_TREE;
1824 if (bcode == BUILT_IN_CALLOC)
1825 length = build_int_cst (size_type_node, 0);
1826 strinfo si = new_strinfo (lhs, idx, length);
1827 if (bcode == BUILT_IN_CALLOC)
1828 si->endptr = lhs;
1829 set_strinfo (idx, si);
1830 si->writable = true;
1831 si->stmt = stmt;
1832 si->dont_invalidate = true;
1835 /* Handle a call to memset.
1836 After a call to calloc, memset(,0,) is unnecessary.
1837 memset(malloc(n),0,n) is calloc(n,1). */
1839 static bool
1840 handle_builtin_memset (gimple_stmt_iterator *gsi)
1842 gimple stmt2 = gsi_stmt (*gsi);
1843 if (!integer_zerop (gimple_call_arg (stmt2, 1)))
1844 return true;
1845 tree ptr = gimple_call_arg (stmt2, 0);
1846 int idx1 = get_stridx (ptr);
1847 if (idx1 <= 0)
1848 return true;
1849 strinfo si1 = get_strinfo (idx1);
1850 if (!si1)
1851 return true;
1852 gimple stmt1 = si1->stmt;
1853 if (!stmt1 || !is_gimple_call (stmt1))
1854 return true;
1855 tree callee1 = gimple_call_fndecl (stmt1);
1856 if (!gimple_call_builtin_p (stmt1, BUILT_IN_NORMAL))
1857 return true;
1858 enum built_in_function code1 = DECL_FUNCTION_CODE (callee1);
1859 tree size = gimple_call_arg (stmt2, 2);
1860 if (code1 == BUILT_IN_CALLOC)
1861 /* Not touching stmt1 */ ;
1862 else if (code1 == BUILT_IN_MALLOC
1863 && operand_equal_p (gimple_call_arg (stmt1, 0), size, 0))
1865 gimple_stmt_iterator gsi1 = gsi_for_stmt (stmt1);
1866 update_gimple_call (&gsi1, builtin_decl_implicit (BUILT_IN_CALLOC), 2,
1867 size, build_one_cst (size_type_node));
1868 si1->length = build_int_cst (size_type_node, 0);
1869 si1->stmt = gsi_stmt (gsi1);
1871 else
1872 return true;
1873 tree lhs = gimple_call_lhs (stmt2);
1874 unlink_stmt_vdef (stmt2);
1875 if (lhs)
1877 gimple assign = gimple_build_assign (lhs, ptr);
1878 gsi_replace (gsi, assign, false);
1880 else
1882 gsi_remove (gsi, true);
1883 release_defs (stmt2);
1886 return false;
1889 /* Handle a POINTER_PLUS_EXPR statement.
1890 For p = "abcd" + 2; compute associated length, or if
1891 p = q + off is pointing to a '\0' character of a string, call
1892 zero_length_string on it. */
1894 static void
1895 handle_pointer_plus (gimple_stmt_iterator *gsi)
1897 gimple stmt = gsi_stmt (*gsi);
1898 tree lhs = gimple_assign_lhs (stmt), off;
1899 int idx = get_stridx (gimple_assign_rhs1 (stmt));
1900 strinfo si, zsi;
1902 if (idx == 0)
1903 return;
1905 if (idx < 0)
1907 tree off = gimple_assign_rhs2 (stmt);
1908 if (tree_fits_uhwi_p (off)
1909 && tree_to_uhwi (off) <= (unsigned HOST_WIDE_INT) ~idx)
1910 ssa_ver_to_stridx[SSA_NAME_VERSION (lhs)]
1911 = ~(~idx - (int) tree_to_uhwi (off));
1912 return;
1915 si = get_strinfo (idx);
1916 if (si == NULL || si->length == NULL_TREE)
1917 return;
1919 off = gimple_assign_rhs2 (stmt);
1920 zsi = NULL;
1921 if (operand_equal_p (si->length, off, 0))
1922 zsi = zero_length_string (lhs, si);
1923 else if (TREE_CODE (off) == SSA_NAME)
1925 gimple def_stmt = SSA_NAME_DEF_STMT (off);
1926 if (gimple_assign_single_p (def_stmt)
1927 && operand_equal_p (si->length, gimple_assign_rhs1 (def_stmt), 0))
1928 zsi = zero_length_string (lhs, si);
1930 if (zsi != NULL
1931 && si->endptr != NULL_TREE
1932 && si->endptr != lhs
1933 && TREE_CODE (si->endptr) == SSA_NAME)
1935 enum tree_code rhs_code
1936 = useless_type_conversion_p (TREE_TYPE (lhs), TREE_TYPE (si->endptr))
1937 ? SSA_NAME : NOP_EXPR;
1938 gimple_assign_set_rhs_with_ops (gsi, rhs_code, si->endptr);
1939 gcc_assert (gsi_stmt (*gsi) == stmt);
1940 update_stmt (stmt);
1944 /* Handle a single character store. */
1946 static bool
1947 handle_char_store (gimple_stmt_iterator *gsi)
1949 int idx = -1;
1950 strinfo si = NULL;
1951 gimple stmt = gsi_stmt (*gsi);
1952 tree ssaname = NULL_TREE, lhs = gimple_assign_lhs (stmt);
1954 if (TREE_CODE (lhs) == MEM_REF
1955 && TREE_CODE (TREE_OPERAND (lhs, 0)) == SSA_NAME)
1957 if (integer_zerop (TREE_OPERAND (lhs, 1)))
1959 ssaname = TREE_OPERAND (lhs, 0);
1960 idx = get_stridx (ssaname);
1963 else
1964 idx = get_addr_stridx (lhs);
1966 if (idx > 0)
1968 si = get_strinfo (idx);
1969 if (si != NULL && si->length != NULL_TREE && integer_zerop (si->length))
1971 if (initializer_zerop (gimple_assign_rhs1 (stmt)))
1973 /* When storing '\0', the store can be removed
1974 if we know it has been stored in the current function. */
1975 if (!stmt_could_throw_p (stmt) && si->writable)
1977 unlink_stmt_vdef (stmt);
1978 release_defs (stmt);
1979 gsi_remove (gsi, true);
1980 return false;
1982 else
1984 si->writable = true;
1985 gsi_next (gsi);
1986 return false;
1989 else
1990 /* Otherwise this statement overwrites the '\0' with
1991 something, if the previous stmt was a memcpy,
1992 its length may be decreased. */
1993 adjust_last_stmt (si, stmt, false);
1995 else if (si != NULL && integer_zerop (gimple_assign_rhs1 (stmt)))
1997 si = unshare_strinfo (si);
1998 si->length = build_int_cst (size_type_node, 0);
1999 si->endptr = NULL;
2000 si->prev = 0;
2001 si->next = 0;
2002 si->stmt = NULL;
2003 si->first = 0;
2004 si->writable = true;
2005 if (ssaname && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (ssaname))
2006 si->endptr = ssaname;
2007 si->dont_invalidate = true;
2009 /* If si->length is non-zero constant, we aren't overwriting '\0',
2010 and if we aren't storing '\0', we know that the length of the
2011 string and any other zero terminated string in memory remains
2012 the same. In that case we move to the next gimple statement and
2013 return to signal the caller that it shouldn't invalidate anything.
2015 This is benefical for cases like:
2017 char p[20];
2018 void foo (char *q)
2020 strcpy (p, "foobar");
2021 size_t len = strlen (p); // This can be optimized into 6
2022 size_t len2 = strlen (q); // This has to be computed
2023 p[0] = 'X';
2024 size_t len3 = strlen (p); // This can be optimized into 6
2025 size_t len4 = strlen (q); // This can be optimized into len2
2026 bar (len, len2, len3, len4);
2029 else if (si != NULL && si->length != NULL_TREE
2030 && TREE_CODE (si->length) == INTEGER_CST
2031 && integer_nonzerop (gimple_assign_rhs1 (stmt)))
2033 gsi_next (gsi);
2034 return false;
2037 else if (idx == 0 && initializer_zerop (gimple_assign_rhs1 (stmt)))
2039 if (ssaname)
2041 si = zero_length_string (ssaname, NULL);
2042 if (si != NULL)
2043 si->dont_invalidate = true;
2045 else
2047 int idx = new_addr_stridx (lhs);
2048 if (idx != 0)
2050 si = new_strinfo (build_fold_addr_expr (lhs), idx,
2051 build_int_cst (size_type_node, 0));
2052 set_strinfo (idx, si);
2053 si->dont_invalidate = true;
2056 if (si != NULL)
2057 si->writable = true;
2059 else if (idx == 0
2060 && TREE_CODE (gimple_assign_rhs1 (stmt)) == STRING_CST
2061 && ssaname == NULL_TREE
2062 && TREE_CODE (TREE_TYPE (lhs)) == ARRAY_TYPE)
2064 size_t l = strlen (TREE_STRING_POINTER (gimple_assign_rhs1 (stmt)));
2065 HOST_WIDE_INT a = int_size_in_bytes (TREE_TYPE (lhs));
2066 if (a > 0 && (unsigned HOST_WIDE_INT) a > l)
2068 int idx = new_addr_stridx (lhs);
2069 if (idx != 0)
2071 si = new_strinfo (build_fold_addr_expr (lhs), idx,
2072 build_int_cst (size_type_node, l));
2073 set_strinfo (idx, si);
2074 si->dont_invalidate = true;
2079 if (si != NULL && initializer_zerop (gimple_assign_rhs1 (stmt)))
2081 /* Allow adjust_last_stmt to remove it if the stored '\0'
2082 is immediately overwritten. */
2083 laststmt.stmt = stmt;
2084 laststmt.len = build_int_cst (size_type_node, 1);
2085 laststmt.stridx = si->idx;
2087 return true;
2090 /* Attempt to optimize a single statement at *GSI using string length
2091 knowledge. */
2093 static bool
2094 strlen_optimize_stmt (gimple_stmt_iterator *gsi)
2096 gimple stmt = gsi_stmt (*gsi);
2098 if (is_gimple_call (stmt))
2100 tree callee = gimple_call_fndecl (stmt);
2101 if (gimple_call_builtin_p (stmt, BUILT_IN_NORMAL))
2102 switch (DECL_FUNCTION_CODE (callee))
2104 case BUILT_IN_STRLEN:
2105 case BUILT_IN_STRLEN_CHKP:
2106 handle_builtin_strlen (gsi);
2107 break;
2108 case BUILT_IN_STRCHR:
2109 case BUILT_IN_STRCHR_CHKP:
2110 handle_builtin_strchr (gsi);
2111 break;
2112 case BUILT_IN_STRCPY:
2113 case BUILT_IN_STRCPY_CHK:
2114 case BUILT_IN_STPCPY:
2115 case BUILT_IN_STPCPY_CHK:
2116 case BUILT_IN_STRCPY_CHKP:
2117 case BUILT_IN_STRCPY_CHK_CHKP:
2118 case BUILT_IN_STPCPY_CHKP:
2119 case BUILT_IN_STPCPY_CHK_CHKP:
2120 handle_builtin_strcpy (DECL_FUNCTION_CODE (callee), gsi);
2121 break;
2122 case BUILT_IN_MEMCPY:
2123 case BUILT_IN_MEMCPY_CHK:
2124 case BUILT_IN_MEMPCPY:
2125 case BUILT_IN_MEMPCPY_CHK:
2126 case BUILT_IN_MEMCPY_CHKP:
2127 case BUILT_IN_MEMCPY_CHK_CHKP:
2128 case BUILT_IN_MEMPCPY_CHKP:
2129 case BUILT_IN_MEMPCPY_CHK_CHKP:
2130 handle_builtin_memcpy (DECL_FUNCTION_CODE (callee), gsi);
2131 break;
2132 case BUILT_IN_STRCAT:
2133 case BUILT_IN_STRCAT_CHK:
2134 case BUILT_IN_STRCAT_CHKP:
2135 case BUILT_IN_STRCAT_CHK_CHKP:
2136 handle_builtin_strcat (DECL_FUNCTION_CODE (callee), gsi);
2137 break;
2138 case BUILT_IN_MALLOC:
2139 case BUILT_IN_CALLOC:
2140 handle_builtin_malloc (DECL_FUNCTION_CODE (callee), gsi);
2141 break;
2142 case BUILT_IN_MEMSET:
2143 if (!handle_builtin_memset (gsi))
2144 return false;
2145 break;
2146 default:
2147 break;
2150 else if (is_gimple_assign (stmt) && !gimple_clobber_p (stmt))
2152 tree lhs = gimple_assign_lhs (stmt);
2154 if (TREE_CODE (lhs) == SSA_NAME && POINTER_TYPE_P (TREE_TYPE (lhs)))
2156 if (gimple_assign_single_p (stmt)
2157 || (gimple_assign_cast_p (stmt)
2158 && POINTER_TYPE_P (TREE_TYPE (gimple_assign_rhs1 (stmt)))))
2160 int idx = get_stridx (gimple_assign_rhs1 (stmt));
2161 ssa_ver_to_stridx[SSA_NAME_VERSION (lhs)] = idx;
2163 else if (gimple_assign_rhs_code (stmt) == POINTER_PLUS_EXPR)
2164 handle_pointer_plus (gsi);
2166 else if (TREE_CODE (lhs) != SSA_NAME && !TREE_SIDE_EFFECTS (lhs))
2168 tree type = TREE_TYPE (lhs);
2169 if (TREE_CODE (type) == ARRAY_TYPE)
2170 type = TREE_TYPE (type);
2171 if (TREE_CODE (type) == INTEGER_TYPE
2172 && TYPE_MODE (type) == TYPE_MODE (char_type_node)
2173 && TYPE_PRECISION (type) == TYPE_PRECISION (char_type_node))
2175 if (! handle_char_store (gsi))
2176 return false;
2181 if (gimple_vdef (stmt))
2182 maybe_invalidate (stmt);
2183 return true;
2186 /* Recursively call maybe_invalidate on stmts that might be executed
2187 in between dombb and current bb and that contain a vdef. Stop when
2188 *count stmts are inspected, or if the whole strinfo vector has
2189 been invalidated. */
2191 static void
2192 do_invalidate (basic_block dombb, gimple phi, bitmap visited, int *count)
2194 unsigned int i, n = gimple_phi_num_args (phi);
2196 for (i = 0; i < n; i++)
2198 tree vuse = gimple_phi_arg_def (phi, i);
2199 gimple stmt = SSA_NAME_DEF_STMT (vuse);
2200 basic_block bb = gimple_bb (stmt);
2201 if (bb == NULL
2202 || bb == dombb
2203 || !bitmap_set_bit (visited, bb->index)
2204 || !dominated_by_p (CDI_DOMINATORS, bb, dombb))
2205 continue;
2206 while (1)
2208 if (gimple_code (stmt) == GIMPLE_PHI)
2210 do_invalidate (dombb, stmt, visited, count);
2211 if (*count == 0)
2212 return;
2213 break;
2215 if (--*count == 0)
2216 return;
2217 if (!maybe_invalidate (stmt))
2219 *count = 0;
2220 return;
2222 vuse = gimple_vuse (stmt);
2223 stmt = SSA_NAME_DEF_STMT (vuse);
2224 if (gimple_bb (stmt) != bb)
2226 bb = gimple_bb (stmt);
2227 if (bb == NULL
2228 || bb == dombb
2229 || !bitmap_set_bit (visited, bb->index)
2230 || !dominated_by_p (CDI_DOMINATORS, bb, dombb))
2231 break;
2237 class strlen_dom_walker : public dom_walker
2239 public:
2240 strlen_dom_walker (cdi_direction direction) : dom_walker (direction) {}
2242 virtual void before_dom_children (basic_block);
2243 virtual void after_dom_children (basic_block);
2246 /* Callback for walk_dominator_tree. Attempt to optimize various
2247 string ops by remembering string lenths pointed by pointer SSA_NAMEs. */
2249 void
2250 strlen_dom_walker::before_dom_children (basic_block bb)
2252 basic_block dombb = get_immediate_dominator (CDI_DOMINATORS, bb);
2254 if (dombb == NULL)
2255 stridx_to_strinfo = NULL;
2256 else
2258 stridx_to_strinfo = ((vec<strinfo, va_heap, vl_embed> *) dombb->aux);
2259 if (stridx_to_strinfo)
2261 for (gphi_iterator gsi = gsi_start_phis (bb); !gsi_end_p (gsi);
2262 gsi_next (&gsi))
2264 gphi *phi = gsi.phi ();
2265 if (virtual_operand_p (gimple_phi_result (phi)))
2267 bitmap visited = BITMAP_ALLOC (NULL);
2268 int count_vdef = 100;
2269 do_invalidate (dombb, phi, visited, &count_vdef);
2270 BITMAP_FREE (visited);
2271 if (count_vdef == 0)
2273 /* If there were too many vdefs in between immediate
2274 dominator and current bb, invalidate everything.
2275 If stridx_to_strinfo has been unshared, we need
2276 to free it, otherwise just set it to NULL. */
2277 if (!strinfo_shared ())
2279 unsigned int i;
2280 strinfo si;
2282 for (i = 1;
2283 vec_safe_iterate (stridx_to_strinfo, i, &si);
2284 ++i)
2286 free_strinfo (si);
2287 (*stridx_to_strinfo)[i] = NULL;
2290 else
2291 stridx_to_strinfo = NULL;
2293 break;
2299 /* If all PHI arguments have the same string index, the PHI result
2300 has it as well. */
2301 for (gphi_iterator gsi = gsi_start_phis (bb); !gsi_end_p (gsi);
2302 gsi_next (&gsi))
2304 gphi *phi = gsi.phi ();
2305 tree result = gimple_phi_result (phi);
2306 if (!virtual_operand_p (result) && POINTER_TYPE_P (TREE_TYPE (result)))
2308 int idx = get_stridx (gimple_phi_arg_def (phi, 0));
2309 if (idx != 0)
2311 unsigned int i, n = gimple_phi_num_args (phi);
2312 for (i = 1; i < n; i++)
2313 if (idx != get_stridx (gimple_phi_arg_def (phi, i)))
2314 break;
2315 if (i == n)
2316 ssa_ver_to_stridx[SSA_NAME_VERSION (result)] = idx;
2321 /* Attempt to optimize individual statements. */
2322 for (gimple_stmt_iterator gsi = gsi_start_bb (bb); !gsi_end_p (gsi); )
2323 if (strlen_optimize_stmt (&gsi))
2324 gsi_next (&gsi);
2326 bb->aux = stridx_to_strinfo;
2327 if (vec_safe_length (stridx_to_strinfo) && !strinfo_shared ())
2328 (*stridx_to_strinfo)[0] = (strinfo) bb;
2331 /* Callback for walk_dominator_tree. Free strinfo vector if it is
2332 owned by the current bb, clear bb->aux. */
2334 void
2335 strlen_dom_walker::after_dom_children (basic_block bb)
2337 if (bb->aux)
2339 stridx_to_strinfo = ((vec<strinfo, va_heap, vl_embed> *) bb->aux);
2340 if (vec_safe_length (stridx_to_strinfo)
2341 && (*stridx_to_strinfo)[0] == (strinfo) bb)
2343 unsigned int i;
2344 strinfo si;
2346 for (i = 1; vec_safe_iterate (stridx_to_strinfo, i, &si); ++i)
2347 free_strinfo (si);
2348 vec_free (stridx_to_strinfo);
2350 bb->aux = NULL;
2354 /* Main entry point. */
2356 namespace {
2358 const pass_data pass_data_strlen =
2360 GIMPLE_PASS, /* type */
2361 "strlen", /* name */
2362 OPTGROUP_NONE, /* optinfo_flags */
2363 TV_TREE_STRLEN, /* tv_id */
2364 ( PROP_cfg | PROP_ssa ), /* properties_required */
2365 0, /* properties_provided */
2366 0, /* properties_destroyed */
2367 0, /* todo_flags_start */
2368 0, /* todo_flags_finish */
2371 class pass_strlen : public gimple_opt_pass
2373 public:
2374 pass_strlen (gcc::context *ctxt)
2375 : gimple_opt_pass (pass_data_strlen, ctxt)
2378 /* opt_pass methods: */
2379 virtual bool gate (function *) { return flag_optimize_strlen != 0; }
2380 virtual unsigned int execute (function *);
2382 }; // class pass_strlen
2384 unsigned int
2385 pass_strlen::execute (function *fun)
2387 ssa_ver_to_stridx.safe_grow_cleared (num_ssa_names);
2388 max_stridx = 1;
2390 calculate_dominance_info (CDI_DOMINATORS);
2392 /* String length optimization is implemented as a walk of the dominator
2393 tree and a forward walk of statements within each block. */
2394 strlen_dom_walker (CDI_DOMINATORS).walk (fun->cfg->x_entry_block_ptr);
2396 ssa_ver_to_stridx.release ();
2397 strinfo_pool.release ();
2398 if (decl_to_stridxlist_htab)
2400 obstack_free (&stridx_obstack, NULL);
2401 delete decl_to_stridxlist_htab;
2402 decl_to_stridxlist_htab = NULL;
2404 laststmt.stmt = NULL;
2405 laststmt.len = NULL_TREE;
2406 laststmt.stridx = 0;
2408 return 0;
2411 } // anon namespace
2413 gimple_opt_pass *
2414 make_pass_strlen (gcc::context *ctxt)
2416 return new pass_strlen (ctxt);