1 /* PR 71831 - __builtin_object_size poor results with no optimization
2 Verify that even without optimization __builtin_object_size result
3 is folded into a constant and dead code that depends on it is
5 /* { dg-do compile } */
6 /* { dg-options "-O0 -fdump-tree-ssa" } */
8 #define concat(a, b) a ## b
9 #define CAT(a, b) concat (a, b)
11 /* Create a symbol name unique to each tes and object size type. */
12 #define SYM(type) CAT (CAT (CAT (failure_on_line_, __LINE__), _type_), type)
14 /* References to the following undefined symbol which is unique for each
15 test case are expected to be eliminated. */
16 #define TEST_FAILURE(type) \
18 extern void SYM (type)(void); \
22 #define bos(obj, type) __builtin_object_size (obj, type)
23 #define size(obj, n) ((size_t)n == X ? sizeof *obj : (size_t)n)
25 #define test(expect, type, obj) \
27 if (bos (obj, type) != size (obj, expect)) \
28 TEST_FAILURE (type); \
31 #define FOLD_ALL(r0, r1, r2, r3, obj) \
39 #define FOLD_0_2(r0, r1, r2, r3, obj) \
45 /* For convenience. Substitute for 'sizeof object' in test cases where
46 the size can vary from target to target. */
47 #define X (size_t)0xdeadbeef
49 typedef __SIZE_TYPE__
size_t;
52 char ax2
[]; /* { dg-warning "assumed to have one element" } */
59 #if __SIZEOF_SHORT__ == 4
63 #elif __SIZEOF_INT__ == 4
69 static char a2x2
[2][2];
70 static char a3x5
[3][5];
72 struct Sx
{ char n
, a
[]; } sx
;
73 struct S0
{ char n
, a
[0]; } s0
;
74 struct S1
{ char n
, a
[1]; } s1
;
75 struct S2
{ char n
, a
[2]; } s2
;
76 struct S9
{ char n
, a
[9]; } s9
;
78 struct S2x2
{ char n
, a
[2][2]; } s2x2
;
79 struct S3x5
{ char n
, a
[3][5]; } s3x5
;
81 static __attribute__ ((noclone
, noinline
)) void
84 FOLD_ALL ( 1, 1, 1, 1, ax2
);
86 FOLD_ALL ( 1, 1, 1, 1, a1
);
87 FOLD_ALL ( 2, 2, 2, 2, a2
);
88 FOLD_ALL ( 9, 9, 9, 9, a9
);
90 FOLD_ALL ( 0, 0, 0, 0, a0
);
91 FOLD_ALL ( 1, 1, 1, 1, ax2
);
93 FOLD_ALL ( 0, 0, 0, 0, ia0
);
94 FOLD_ALL ( 4, 4, 4, 4, ia1
);
95 FOLD_ALL ( 36, 36, 36, 36, ia9
);
97 /* Not all results for multidimensional arrays make sense (see
98 bug 77293). The expected results below simply reflect those
99 obtained at -O2 (modulo the known limitations at -O1). */
100 FOLD_ALL ( 4, 4, 4, 4, a2x2
);
101 FOLD_ALL ( 4, 4, 4, 4, &a2x2
[0]);
102 FOLD_ALL ( 4, 2, 4, 2, &a2x2
[0][0]);
103 FOLD_0_2 ( 0, F1 (0), 0, 0, &a2x2
+ 1);
104 FOLD_0_2 ( 2, F1 ( 2), 2, F3 ( 2), &a2x2
[0] + 1);
105 FOLD_0_2 ( 3, F1 ( 1), 3, F3 ( 3), &a2x2
[0][0] + 1);
107 FOLD_ALL ( 15, 15, 15, 15, a3x5
);
108 FOLD_ALL ( 15, 5, 15, 5, &a3x5
[0][0] + 0);
109 FOLD_0_2 ( 14, F1 ( 4), 14, F3 (14), &a3x5
[0][0] + 1);
111 FOLD_ALL ( 1, 1, 1, 1, a1
+ 0);
112 FOLD_0_2 ( 0, F1 ( 0), 0, 0, &a1
+ 1);
113 FOLD_ALL ( 2, 2, 2, 2, a2
+ 0);
114 FOLD_0_2 ( 1, F1 ( 1), 1, F3 ( 1), a2
+ 1);
115 FOLD_0_2 ( 0, F1 ( 0), 0, 0, a2
+ 2);
118 static __attribute__ ((noclone
, noinline
)) void
121 /* The expected size of a declared object with a flexible array member
122 is sizeof sx in all __builtin_object_size types. */
123 FOLD_ALL ( X
, X
, X
, X
, &sx
);
125 /* The expected size of a flexible array member of a declared object
127 FOLD_ALL ( 0, 0, 0, 0, sx
.a
);
129 /* The expected size of a declared object with a zero-length array member
130 is sizeof sx in all __builtin_object_size types. */
131 FOLD_ALL ( X
, X
, X
, X
, &s0
);
133 /* The expected size of a zero-length array member of a declared object
135 FOLD_ALL ( 0, 0, 0, 0, s0
.a
);
137 FOLD_ALL ( X
, X
, X
, X
, &s1
);
138 FOLD_ALL ( 1, 1, 1, 1, s1
.a
);
139 FOLD_0_2 ( 0, F1 (0), 0, 0, s1
.a
+ 1);
141 FOLD_ALL ( X
, X
, X
, X
, &s9
);
142 FOLD_ALL ( 9, 9, 9, 9, s9
.a
);
143 FOLD_ALL ( 9, 9, 9, 9, s9
.a
+ 0);
144 FOLD_0_2 ( 8, F1 ( 8), 8, F3 ( 8), s9
.a
+ 1);
145 FOLD_0_2 ( 7, F1 ( 7), 7, F3 ( 7), s9
.a
+ 2);
146 FOLD_0_2 ( 0, F1 ( 0), 0, F3 ( 0), s9
.a
+ 9);
158 /* { dg-final { scan-tree-dump-not "failure_on_line" "ssa" } } */