https://gcc.gnu.org/bugzilla/show_bug.cgi?id=79715
Bug ID: 79715 Summary: hand-rolled strdup with unused result not eliminated Product: gcc Version: 7.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: tree-optimization Assignee: unassigned at gcc dot gnu.org Reporter: msebor at gcc dot gnu.org Target Milestone: --- While looking for other opportunities similar to those noted in bug 79697 and the discussion of the patch for it (https://gcc.gnu.org/ml/gcc-patches/2017-02/msg01566.html) I noticed another one. The two functions f and g below are equivalent. GCC successfully transforms f into a return statement but it misses the same opportunity in g. Clang 5.0 generates the same optimal code for both functions. $ cat t.c && gcc -O2 -S -Wall -Wextra -Wpedantic -xc -fdump-tree-printf-return-value=/dev/stdout t.c void f (const char *s) { unsigned n = __builtin_strlen (s) + 1; char *p = __builtin_malloc (n); __builtin_memcpy (p, s, n); __builtin_free (p); } void g (const char *s) { unsigned n = __builtin_strlen (s) + 1; char *p = __builtin_malloc (n); __builtin_strcpy (p, s); __builtin_free (p); } ;; Function f (f, funcdef_no=0, decl_uid=1795, cgraph_uid=0, symbol_order=0) f (const char * s) { <bb 2> [100.00%]: return; } ;; Function g (g, funcdef_no=1, decl_uid=1800, cgraph_uid=1, symbol_order=1) g (const char * s) { char * p; unsigned int n; long unsigned int _1; unsigned int _2; long unsigned int _3; <bb 2> [100.00%]: _1 = __builtin_strlen (s_5(D)); _2 = (unsigned int) _1; n_6 = _2 + 1; _3 = (long unsigned int) n_6; p_8 = __builtin_malloc (_3); __builtin_strcpy (p_8, s_5(D)); __builtin_free (p_8); return; }