1 /* PR tree-optimization/91315 - missing strlen lower bound of a string
2    known to be at least N characters
3    { dg-do compile }
4    { dg-options "-O2 -Wall -fdump-tree-optimized" } */
5 
6 #include "strlenopt.h"
7 
8 #define CAT(x, y) x ## y
9 #define CONCAT(x, y) CAT (x, y)
10 #define FAILNAME(name) CONCAT (call_ ## name ##_on_line_, __LINE__)
11 
12 #define FAIL(name) do {				\
13     extern void FAILNAME (name) (void);		\
14     FAILNAME (name)();				\
15   } while (0)
16 
17 /* Macro to emit a call to function named
18      call_in_true_branch_not_eliminated_on_line_NNN()
19    for each call that's expected to be eliminated.  The dg-final
20    scan-tree-dump-time directive at the bottom of the test verifies
21    that no such call appears in output.  */
22 #define ASSERT_ELIM(expr)						\
23   if (!!(expr)) FAIL (in_true_branch_not_eliminated); else (void)0
24 
25 char a[32];
26 
lower_bound_assign_1(void)27 void lower_bound_assign_1 (void)
28 {
29   a[0] = '1';
30   ASSERT_ELIM (strlen (a) < 1);
31 }
32 
lower_bound_assign_2(void)33 void lower_bound_assign_2 (void)
34 {
35   a[0] = '1';
36   a[1] = '2';
37   ASSERT_ELIM (strlen (a) < 2);
38 }
39 
lower_bound_assign_3(void)40 void lower_bound_assign_3 (void)
41 {
42   a[0] = '1';
43   a[1] = '2';
44   a[2] = '3';
45   ASSERT_ELIM (strlen (a) < 3);
46 }
47 
lower_bound_memcpy(void)48 void lower_bound_memcpy (void)
49 {
50   memcpy (a, "123", 3);
51   ASSERT_ELIM (strlen (a) < 3);
52 }
53 
lower_bound_memcpy_memcpy_2(void)54 void lower_bound_memcpy_memcpy_2 (void)
55 {
56   memcpy (a, "123", 3);
57   memcpy (a + 2, "345", 3);
58   ASSERT_ELIM (strlen (a) < 5);
59 }
60 
lower_bound_memcpy_memcpy_3(void)61 void lower_bound_memcpy_memcpy_3 (void)
62 {
63   memcpy (a, "123", 3);
64   memcpy (a + 3, "456", 3);
65   ASSERT_ELIM (strlen (a) < 6);
66 }
67 
68 /* FIXME: Not optimized yet.
69 void lower_bound_stpcpy_stpcpy_assign (void)
70 {
71   *stpcpy (strcpy (a, "123"), "4567") = '8';
72   ASSERT_ELIM (strlen (a) < 8);
73 }
74 */
75 
lower_bound_strcpy_strcat_assign(void)76 void lower_bound_strcpy_strcat_assign (void)
77 {
78   strcpy (a, "123");
79   strcat (a, "45");
80   a[5] = '6';
81   ASSERT_ELIM (strlen (a) < 6);
82 }
83 
84 /* { dg-final { scan-tree-dump-times "call_in_true_branch_not_eliminated_" 0 "optimized" } } */
85