(root)/
gcc-13.2.0/
gcc/
testsuite/
gcc.dg/
strlenopt-4.c
       1  /* { dg-do run } */
       2  /* { dg-options "-O2 -fdump-tree-strlen" } */
       3  
       4  #include "strlenopt.h"
       5  
       6  /* If stpcpy can't be used, this is optimized into
       7     strcpy (p, q); strcat (p, r); memcpy (p + strlen (p), "abcd", 5);
       8     If stpcpy can be used (see strlenopt-4g.c test),
       9     this is optimized into
      10     memcpy (stpcpy (stpcpy (p, q), r), "abcd", 5);  */
      11  __attribute__((noinline, noclone)) void
      12  foo (char *p, const char *q, const char *r)
      13  {
      14    strcpy (p, q);
      15    strcat (p, r);
      16    strcat (p, "abcd");
      17  }
      18  
      19  /* If stpcpy can't be used, this is optimized into
      20     memcpy (p, "abcd", 4); strcpy (p + 4, q); strcat (p, r);
      21     If stpcpy can be used, this is optimized into
      22     memcpy (p, "abcd", 4); strcpy (stpcpy (p + 4, q), r);  */
      23  __attribute__((noinline, noclone)) void
      24  bar (char *p, const char *q, const char *r)
      25  {
      26    strcpy (p, "abcd");
      27    strcat (p, q);
      28    strcat (p, r);
      29  }
      30  
      31  /* If stpcpy can't be used, this is optimized into
      32     strcat (p, q); memcpy (t1 = p + strlen (p), "abcd", 4);
      33     strcpy (t1 + 4, r); memcpy (p + strlen (p), "efgh", 5);
      34     If stpcpy can be used, this is optimized into
      35     t1 = stpcpy (p + strlen (p), q); memcpy (t1, "abcd", 4);
      36     memcpy (stpcpy (t1 + 4, r), "efgh", 5);  */
      37  __attribute__((noinline, noclone)) void
      38  baz (char *p, const char *q, const char *r)
      39  {
      40    strcat (p, q);
      41    strcat (p, "abcd");
      42    strcat (p, r);
      43    strcat (p, "efgh");
      44  }
      45  
      46  char buf[64];
      47  
      48  int
      49  main ()
      50  {
      51    char *volatile p = buf;
      52    const char *volatile q = "ij";
      53    const char *volatile r = "klmno";
      54    foo (p, q, r);
      55    if (memcmp (buf, "ijklmnoabcd\0\0\0\0\0\0\0\0", 20) != 0)
      56      abort ();
      57    memset (buf, '\0', sizeof buf);
      58    bar (p, q, r);
      59    if (memcmp (buf, "abcdijklmno\0\0\0\0\0\0\0\0", 20) != 0)
      60      abort ();
      61    memset (buf, 'v', 3);
      62    memset (buf + 3, '\0', -3 + sizeof buf);
      63    baz (p, q, r);
      64    if (memcmp (buf, "vvvijabcdklmnoefgh\0", 20) != 0)
      65      abort ();
      66    return 0;
      67  }
      68  
      69  /* { dg-final { scan-tree-dump-times "strlen \\(" 3 "strlen1" } } */
      70  /* { dg-final { scan-tree-dump-times "memcpy \\(" 4 "strlen1" } } */
      71  /* { dg-final { scan-tree-dump-times "strcpy \\(" 3 "strlen1" } } */
      72  /* { dg-final { scan-tree-dump-times "strcat \\(" 3 "strlen1" } } */
      73  /* { dg-final { scan-tree-dump-times "strchr \\(" 0 "strlen1" } } */
      74  /* { dg-final { scan-tree-dump-times "stpcpy \\(" 0 "strlen1" } } */