(root)/
gcc-13.2.0/
gcc/
testsuite/
gcc.dg/
strlenopt-91.c
       1  /* PR tree-optimization/92412 - excessive errno aliasing assumption defeats
       2     optimization
       3     { dg-do compile }
       4     { dg-options "-O2 -Wall -fdump-tree-optimized" }
       5     { dg-require-effective-target alloca } */
       6  
       7  typedef __SIZE_TYPE__ size_t;
       8  
       9  extern void* alloca (size_t);
      10  extern void* calloc (size_t, size_t);
      11  extern void* malloc (size_t);
      12  
      13  extern const char exta[4];
      14  static char stata[] = "123";
      15  
      16  void sink (const void*, ...);
      17  
      18  #define T(ptr, alloc) do {						\
      19      const char *p = ptr;						\
      20      if (p[0] != '1' || p[1] != '2' || p[2] != '3' || p[3] != '\0'	\
      21  	|| __builtin_strlen (p) != 3)					\
      22        return;								\
      23  									\
      24      void *q = alloc;							\
      25      __builtin_strcpy (q, p);						\
      26  									\
      27      if (p[0] != '1' || p[1] != '2' || p[2] != '3' || p[3] != '\0'	\
      28  	|| __builtin_strlen (p) != 3					\
      29  	|| __builtin_strlen (q) != 3)					\
      30        __builtin_abort ();						\
      31  									\
      32      sink (p, q);							\
      33    } while (0)
      34  
      35  
      36  void alloca_test_local (unsigned n)
      37  {
      38    char loca[] = "123";
      39    T (loca, alloca (n));
      40  }
      41  
      42  void alloca_test_extern_const (unsigned n)
      43  {
      44    T (exta, alloca (n));
      45  }
      46  
      47  void alloca_test_static (unsigned n)
      48  {
      49    T (stata, alloca (n));
      50  }
      51  
      52  
      53  // Verify fix for PR tree-optimization/92412.
      54  void calloc_test_local (unsigned m, unsigned n)
      55  {
      56    char loca[] = "123";
      57    T (loca, calloc (m, n));
      58  }
      59  
      60  void calloc_test_extern_const (unsigned m, unsigned n)
      61  {
      62    T (exta, calloc (m, n));
      63  }
      64  
      65  void calloc_test_static (unsigned m, unsigned n)
      66  {
      67    T (stata, calloc (m, n));
      68  }
      69  
      70  
      71  // Verify fix for PR tree-optimization/92412.
      72  void malloc_test_local (unsigned n)
      73  {
      74    char loca[] = "123";
      75    T (loca, malloc (n));
      76  }
      77  
      78  void malloc_test_extern_const (unsigned n)
      79  {
      80    T (exta, malloc (n));
      81  }
      82  
      83  void malloc_test_static (unsigned n)
      84  {
      85    T (stata, malloc (n));
      86  }
      87  
      88  
      89  #undef T
      90  #define T(ptr, n) do {							\
      91      const char *p = ptr;						\
      92      if (p[0] != '1' || p[1] != '2' || p[2] != '3' || p[3] != '\0'	\
      93  	|| __builtin_strlen (p) != 3)					\
      94        return;								\
      95  									\
      96      char vla[n];							\
      97      char *q = vla;							\
      98      __builtin_strcpy (q, p);						\
      99  									\
     100      if (p[0] != '1' || p[1] != '2' || p[2] != '3' || p[3] != '\0'	\
     101  	|| __builtin_strlen (p) != 3					\
     102  	|| __builtin_strlen (q) != 3)					\
     103        __builtin_abort ();						\
     104  									\
     105      sink (p, vla);							\
     106    } while (0)
     107  
     108  
     109  void vla_test_local (unsigned n)
     110  {
     111    char loca[] = "123";
     112    T (loca, n);
     113  }
     114  
     115  void vla_test_extern_const (unsigned n)
     116  {
     117    T (exta, n);
     118  }
     119  
     120  void vla_test_static (unsigned n)
     121  {
     122    T (stata, n);
     123  }
     124  
     125  /* { dg-final { scan-tree-dump-not "abort" "optimized" } } */