(root)/
gcc-13.2.0/
gcc/
testsuite/
gcc.dg/
strlenopt-11.c
       1  /* { dg-do run } */
       2  /* { dg-options "-O2 -fdump-tree-strlen" } */
       3  
       4  #include "strlenopt.h"
       5  
       6  __attribute__((noinline, noclone)) void
       7  fn1 (char *p, const char *z, size_t *lp)
       8  {
       9    char *q, *r, *s;
      10    char buf[64];
      11    size_t l[11];
      12    /* The first strlen stays, all the strcpy calls can be optimized
      13       into memcpy and most other strlen calls and all strchr calls
      14       optimized away.  l[6] = strlen (r); and l[9] = strlen (r); need
      15       to stay, because we need to invalidate the knowledge about
      16       r strlen after strcpy (q, "jklmnopqrst").  */
      17    l[0] = strlen (z);
      18    strcpy (buf, z);
      19    strcpy (p, "abcde");
      20    q = strchr (p, '\0');
      21    strcpy (q, "efghi");
      22    r = strchr (q, '\0');
      23    strcpy (r, buf);
      24    l[1] = strlen (p);
      25    l[2] = strlen (q);
      26    l[3] = strlen (r);
      27    strcpy (q, "jklmnopqrst");
      28    l[4] = strlen (p);
      29    l[5] = strlen (q);
      30    l[6] = strlen (r);
      31    s = strchr (q, '\0');
      32    strcpy (s, buf);
      33    l[7] = strlen (p);
      34    l[8] = strlen (q);
      35    l[9] = strlen (r);
      36    l[10] = strlen (s);
      37    memcpy (lp, l, sizeof l);
      38  }
      39  
      40  int
      41  main ()
      42  {
      43    char buf[64];
      44    size_t l[11];
      45    const char *volatile z = "ABCDEFG";
      46    memset (buf, '\0', sizeof buf);
      47    fn1 (buf, z, l);
      48    if (memcmp (buf, "abcdejklmnopqrstABCDEFG", 24) != 0)
      49      abort ();
      50    if (l[0] != 7)
      51      abort ();
      52    if (l[1] != 17 || l[2] != 12 || l[3] != 7)
      53      abort ();
      54    if (l[4] != 16 || l[5] != 11 || l[6] != 6)
      55      abort ();
      56    if (l[7] != 23 || l[8] != 18 || l[9] != 13 || l[10] != 7)
      57      abort ();
      58    return 0;
      59  }
      60  
      61  /* { dg-final { scan-tree-dump-times "strlen \\(" 3 "strlen1" } } */
      62  /* Some targets have BIGGEST_ALIGNMENT 8-bits, allowing fold_builtin_memory_op
      63     to expand the memcpy call at the end of fn1.  */
      64  /* { dg-final { scan-tree-dump-times "memcpy \\(" 7 "strlen1" { target { ! no_alignment_constraints } } } } */
      65  /* { dg-final { scan-tree-dump-times "memcpy \\(" 6 "strlen1" { target { no_alignment_constraints } } } } */
      66  /* { dg-final { scan-tree-dump-times "strcpy \\(" 0 "strlen1" } } */
      67  /* { dg-final { scan-tree-dump-times "strcat \\(" 0 "strlen1" } } */
      68  /* { dg-final { scan-tree-dump-times "strchr \\(" 0 "strlen1" } } */
      69  /* { dg-final { scan-tree-dump-times "stpcpy \\(" 0 "strlen1" } } */
      70  /* Where the memcpy is expanded, the assignemts to elements of l are
      71     propagated.  */
      72  /* { dg-final { scan-tree-dump-times "  _\[0-9\]* = strlen \\(\[^\n\r\]*;\[\n\r\]*  l.0. = " 1 "strlen1" { target { ! no_alignment_constraints } } } } */
      73  /* { dg-final { scan-tree-dump-times "  _\[0-9\]* = strlen \\(\[^\n\r\]*;\[\n\r\]*  l.6. = " 1 "strlen1" { target { ! no_alignment_constraints } } } } */
      74  /* { dg-final { scan-tree-dump-times "  _\[0-9\]* = strlen \\(\[^\n\r\]*;\[\n\r\]*  l.9. = " 1 "strlen1" { target { ! no_alignment_constraints } } } } */
      75  /* { dg-final { scan-tree-dump-times "  _\[0-9\]* = strlen \\(\[^\n\r\]*;" 3 "strlen1" { target { no_alignment_constraints } } } } */