1 int global;
2 int func(void);
3
4 /* These must fail. */
bad0(void)5 int bad0(void) { return __builtin_constant_p(global); }
bad1(void)6 int bad1(void) { return __builtin_constant_p(global++); }
bad2(int x)7 inline int bad2(int x) { return __builtin_constant_p(x++); }
bad3(int x)8 inline int bad3(int x) { return __builtin_constant_p(x); }
bad4(const char * x)9 inline int bad4(const char *x) { return __builtin_constant_p(x); }
bad5(void)10 int bad5(void) { return bad2(1); }
bad6(int x)11 inline int bad6(int x) { return __builtin_constant_p(x+1); }
bad7(void)12 int bad7(void) { return __builtin_constant_p(func()); }
bad8(void)13 int bad8(void) { char buf[10]; return __builtin_constant_p(buf); }
bad9(const char * x)14 int bad9(const char *x) { return __builtin_constant_p(x[123456]); }
bad10(void)15 int bad10(void) { return __builtin_constant_p(&global); }
16
17 /* These must pass, or we've broken gcc2 functionality. */
good0(void)18 int good0(void) { return __builtin_constant_p(1); }
good1(void)19 int good1(void) { return __builtin_constant_p("hi"); }
good2(void)20 int good2(void) { return __builtin_constant_p((1234 + 45) & ~7); }
21
22 /* These are extensions to gcc2. Failure indicates an optimization
23 regression. */
opt0(void)24 int opt0(void) { return bad3(1); }
opt1(void)25 int opt1(void) { return bad6(1); }
opt2(void)26 int opt2(void) { return __builtin_constant_p("hi"[0]); }
27
28 /*
29 * Opt3 is known to fail. It is one of the important cases that glibc
30 * was interested in though, so keep this around as a reminder.
31 *
32 * The solution is to add bits to recover bytes from constant pool
33 * elements given nothing but a constant pool label and an offset.
34 * When we can do that, and we can simplify strlen after the fact,
35 * then we can enable recognition of constant pool labels as constants.
36 */
37
38 /* int opt3(void) { return bad4("hi"); } */
39
40
41 /* Call through tables so -finline-functions can't screw with us. */
42 int (*bad_t0[])(void) = {
43 bad0, bad1, bad5, bad7, bad8, bad10
44 };
45
46 int (*bad_t1[])(int x) = {
47 bad2, bad3, bad6
48 };
49
50 int (*bad_t2[])(const char *x) = {
51 bad4, bad9
52 };
53
54 int (*good_t0[])(void) = {
55 good0, good1, good2
56 };
57
58 int (*opt_t0[])(void) = {
59 opt0, opt1, opt2 /* , opt3 */
60 };
61
62 #define N(arr) (sizeof(arr)/sizeof(*arr))
63
main()64 int main()
65 {
66 int i;
67
68 for (i = 0; i < N(bad_t0); ++i)
69 if ((*bad_t0[i])())
70 abort();
71
72 for (i = 0; i < N(bad_t1); ++i)
73 if ((*bad_t1[i])(1))
74 abort();
75
76 for (i = 0; i < N(bad_t2); ++i)
77 if ((*bad_t2[i])("hi"))
78 abort();
79
80 for (i = 0; i < N(good_t0); ++i)
81 if (! (*good_t0[i])())
82 abort();
83
84 #ifdef __OPTIMIZE__
85 for (i = 0; i < N(opt_t0); ++i)
86 if (! (*opt_t0[i])())
87 abort();
88 #endif
89
90 exit(0);
91 }
92