(root)/
gcc-13.2.0/
gcc/
testsuite/
gcc.dg/
strlenopt-13.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 *y, const char *z, size_t *lp)
       8  {
       9    char *q, *r, *s;
      10    char buf1[64], buf2[64];
      11    size_t l[8];
      12    /* These two strlen calls stay, all strcpy calls are optimized into
      13       memcpy, all strchr calls optimized away, and most other strlen
      14       calls too.  */
      15    l[0] = strlen (y);
      16    l[1] = strlen (z);
      17    strcpy (buf1, y);
      18    strcpy (buf2, z);
      19    strcpy (p, "abcde");
      20    q = strchr (p, '\0');
      21    strcpy (q, "efghi");
      22    r = strchr (q, '\0');
      23    strcpy (r, buf1);
      24    l[2] = strlen (p);
      25    l[3] = strlen (q);
      26    l[4] = strlen (r);
      27    strcpy (r, buf2);
      28    /* Except for these two calls, strlen (r) before and after the above
      29       is non-constant, so adding l[4] - l[1] to all previous strlens
      30       might make the expressions already too complex.  */
      31    l[5] = strlen (p);
      32    l[6] = strlen (q);
      33    /* This one is of course optimized, it is l[1].  */
      34    l[7] = strlen (r);
      35    memcpy (lp, l, sizeof l);
      36  }
      37  
      38  int
      39  main ()
      40  {
      41    char buf[64];
      42    size_t l[8];
      43    const char *volatile y = "ABCDEFG";
      44    const char *volatile z = "HIJK";
      45    memset (buf, '\0', sizeof buf);
      46    fn1 (buf, y, z, l);
      47    if (memcmp (buf, "abcdeefghiHIJK", 15) != 0)
      48      abort ();
      49    if (l[0] != 7 || l[1] != 4)
      50      abort ();
      51    if (l[2] != 17 || l[3] != 12 || l[4] != 7)
      52      abort ();
      53    if (l[5] != 14 || l[6] != 9 || l[7] != 4)
      54      abort ();
      55    return 0;
      56  }
      57  
      58  /* { dg-final { scan-tree-dump-times "strlen \\(" 4 "strlen1" } } */
      59  /* Some targets have BIGGEST_ALIGNMENT 8-bits, allowing fold_builtin_memory_op
      60     to expand the memcpy call at the end of fn1.  */
      61  /* { dg-final { scan-tree-dump-times "memcpy \\(" 7 "strlen1" { target { ! no_alignment_constraints } } } } */
      62  /* { dg-final { scan-tree-dump-times "memcpy \\(" 6 "strlen1" { target { no_alignment_constraints } } } } */
      63  /* { dg-final { scan-tree-dump-times "strcpy \\(" 0 "strlen1" } } */
      64  /* { dg-final { scan-tree-dump-times "strcat \\(" 0 "strlen1" } } */
      65  /* { dg-final { scan-tree-dump-times "strchr \\(" 0 "strlen1" } } */
      66  /* { dg-final { scan-tree-dump-times "stpcpy \\(" 0 "strlen1" } } */
      67  /* Where the memcpy is expanded, the assignemts to elements of l are
      68     propagated.  */
      69  /* { dg-final { scan-tree-dump-times "  _\[0-9\]* = strlen \\(\[^\n\r\]*;\[\n\r\]*  l.0. = " 1 "strlen1" { target { ! no_alignment_constraints } } } } */
      70  /* { dg-final { scan-tree-dump-times "  _\[0-9\]* = strlen \\(\[^\n\r\]*;\[\n\r\]*  l.1. = " 1 "strlen1" { target { ! no_alignment_constraints } } } } */
      71  /* { dg-final { scan-tree-dump-times "  _\[0-9\]* = strlen \\(\[^\n\r\]*;\[\n\r\]*  l.5. = " 1 "strlen1" { target { ! no_alignment_constraints } } } } */
      72  /* { dg-final { scan-tree-dump-times "  _\[0-9\]* = strlen \\(\[^\n\r\]*;\[\n\r\]*  l.6. = " 1 "strlen1" { target { ! no_alignment_constraints } } } } */
      73  /* { dg-final { scan-tree-dump-times "  _\[0-9\]* = strlen \\(\[^\n\r\]*;" 4 "strlen1" { target { no_alignment_constraints } } } } */