Debian Codegraph @

gcc/testsuite/c-c++-common/torture/pr116098-1.c

raw
1 /* { dg-do run } */
2 /* PR tree-optimization/116098 */
3 /* truthy was being miscompiled where the VCE was not being pulled out
4 of the if statement by factor_out_conditional_operation before the rest of
5 phiopt would happen which assumed VCE would be correct. */
6 /* The unused label was causing truthy to have different code generation than truthy_1. */
7
8
9 #ifndef __cplusplus
10 #define bool _Bool
11 #endif
12
13 enum ValueType {
14 VALUE_BOOLEAN,
15 VALUE_NUM,
16 };
17
18 struct Value {
19 enum ValueType type;
20 union {
21 bool boolean;
22 int num;
23 };
24 };
25
26 static struct Value s_value;
27 static bool s_b;
28
29
30 bool truthy_1(void) __attribute__((noinline));
31 bool
32 truthy_1(void)
33 {
34 struct Value value = s_value;
35 if (s_b) s_b = 0;
36 enum ValueType t = value.type;
37 if (t != VALUE_BOOLEAN)
38 return 1;
39 return value.boolean;
40 }
41 bool truthy(void) __attribute__((noinline));
42 bool
43 truthy(void)
44 {
45 struct Value value = s_value;
46 if (s_b) s_b = 0;
47 enum ValueType t = value.type;
48 if (t != VALUE_BOOLEAN)
49 return 1;
50 /* This unused label should not cause any difference in code generation. */
51 a: __attribute__((unused));
52 return value.boolean;
53 }
54
55 int
56 main(void)
57 {
58 s_b = 0;
59 s_value = (struct Value) {
60 .type = VALUE_NUM,
61 .num = 2,
62 };
63 s_value = (struct Value) {
64 .type = VALUE_BOOLEAN,
65 .boolean = !truthy_1(),
66 };
67 bool b = truthy_1();
68 if (b)
69 __builtin_abort();
70
71 s_b = 0;
72 s_value = (struct Value) {
73 .type = VALUE_NUM,
74 .num = 2,
75 };
76 s_value = (struct Value) {
77 .type = VALUE_BOOLEAN,
78 .boolean = !truthy(),
79 };
80 b = truthy();
81 if (b)
82 __builtin_abort();
83 }
84