(root)/
gcc-13.2.0/
gcc/
testsuite/
gcc.dg/
strlenopt-83.c
       1  /* PR tree-optimization/83821 - local aggregate initialization defeats
       2     strlen optimization
       3     { dg-do compile }
       4     { dg-options "-O2 -Wall -fdump-tree-optimized" }
       5     { dg-require-effective-target alloca } */
       6  
       7  #include "strlenopt.h"
       8  char *p_p2, *p_p5, *p_p9, *p_p14;
       9  
      10  unsigned n0, n1, n2, n3, n4;
      11  
      12  
      13  static inline __attribute__ ((always_inline)) void
      14  elim_strlen_of_consecutive_strcpy (char *p)
      15  {
      16    p_p2 = p + 2;
      17    __builtin_strcpy (p_p2, "12");
      18  
      19    p_p5 = p_p2 + 3;
      20    __builtin_strcpy (p_p5, "124");
      21  
      22    p_p9 = p_p5 + 4;
      23    __builtin_strcpy (p_p9, "1245");
      24  
      25    p_p14 = p_p9 + 5;
      26  
      27    n0 = __builtin_strlen (p);
      28    n1 = __builtin_strlen (p_p2);
      29    n2 = __builtin_strlen (p_p5);
      30    n3 = __builtin_strlen (p_p9);
      31  
      32    /* The following isn't handled yet:
      33       n4 = __builtin_strlen (p_p14); */
      34  
      35    if (n0 || n1 != 2 || n2 != 3 || n3 != 4)
      36      __builtin_abort ();
      37  }
      38  
      39  
      40  void elim_strlen_of_consecutive_strcpy_in_alloca (unsigned n)
      41  {
      42    /* Only known sizes are handled so far.  */
      43    n = 14;
      44  
      45    char *p = __builtin_alloca (n);
      46  
      47    *p = '\0';
      48  
      49    elim_strlen_of_consecutive_strcpy (p);
      50  }
      51  
      52  
      53  void elim_strlen_of_consecutive_strcpy_in_vla (unsigned n)
      54  {
      55    /* Only known sizes are handled so far.  */
      56    n = 14;
      57  
      58    char vla[n];
      59  
      60    *vla = '\0';
      61  
      62    elim_strlen_of_consecutive_strcpy (vla);
      63  }
      64  
      65  void elim_strlen_of_consecutive_strcpy_in_malloc (unsigned n)
      66  {
      67    char *p = __builtin_malloc (n);
      68  
      69    *p = '\0';
      70  
      71    elim_strlen_of_consecutive_strcpy (p);
      72  }
      73  
      74  
      75  void elim_strlen_of_consecutive_strcpy_in_calloc (unsigned n)
      76  {
      77    char *p = __builtin_calloc (n, 1);
      78  
      79    /* Do not store into *P to verify that strlen knows it's zero.  */
      80  
      81    elim_strlen_of_consecutive_strcpy (p);
      82  }
      83  
      84  /* { dg-final { scan-tree-dump-not "abort" "optimized" } } */