Debian Codegraph @

clang/test/CIR/CodeGen/volatile.cpp

raw
1 // RUN: %clang_cc1 -std=c++20 -triple x86_64-unknown-linux-gnu -fclangir -emit-cir %s -o %t.cir
2 // RUN: FileCheck --check-prefix=CIR %s < %t.cir
3 // RUN: %clang_cc1 -std=c++20 -triple x86_64-unknown-linux-gnu -fclangir -emit-llvm %s -o %t-cir.ll
4 // RUN: FileCheck --check-prefix=LLVM %s < %t-cir.ll
5 // RUN: %clang_cc1 -std=c++20 -triple x86_64-unknown-linux-gnu -emit-llvm %s -o %t.ll
6 // RUN: FileCheck --check-prefix=OGCG %s < %t.ll
7
8 int test_load(volatile int *ptr) {
9 return *ptr;
10 }
11
12 // CIR: cir.func {{.*}} @_Z9test_loadPVi
13 // CIR: cir.load volatile
14
15 // LLVM: define {{.*}} i32 @_Z9test_loadPVi
16 // LLVM: load volatile i32, ptr %{{.*}}
17
18 // OGCG: define {{.*}} i32 @_Z9test_loadPVi
19 // OGCG: load volatile i32, ptr %{{.*}}
20
21 void test_store(volatile int *ptr) {
22 *ptr = 42;
23 }
24
25 // CIR: cir.func {{.*}} @_Z10test_storePVi
26 // CIR: cir.store volatile
27
28 // LLVM: define {{.*}} void @_Z10test_storePVi
29 // LLVM: store volatile i32 42, ptr %{{.*}}
30
31 // OGCG: define {{.*}} void @_Z10test_storePVi
32 // OGCG: store volatile i32 42, ptr %{{.*}}
33
34 struct Foo {
35 int x;
36 volatile int y;
37 volatile int z: 4;
38 };
39
40 int test_load_field1(volatile Foo *ptr) {
41 return ptr->x;
42 }
43
44 // CIR: cir.func {{.*}} @_Z16test_load_field1PV3Foo
45 // CIR: %[[MEMBER_ADDR:.*]] = cir.get_member
46 // CIR: %{{.+}} = cir.load volatile{{.*}} %[[MEMBER_ADDR]]
47
48 // LLVM: define {{.*}} i32 @_Z16test_load_field1PV3Foo
49 // LLVM: %[[MEMBER_ADDR:.*]] = getelementptr %struct.Foo, ptr %{{.*}}, i32 0, i32 0
50 // LLVM: %{{.*}} = load volatile i32, ptr %[[MEMBER_ADDR]]
51
52 // OGCG: define {{.*}} i32 @_Z16test_load_field1PV3Foo
53 // OGCG: %[[MEMBER_ADDR:.*]] = getelementptr inbounds nuw %struct.Foo, ptr %{{.*}}, i32 0, i32 0
54 // OGCG: %{{.*}} = load volatile i32, ptr %[[MEMBER_ADDR]]
55
56 int test_load_field2(Foo *ptr) {
57 return ptr->y;
58 }
59
60 // CIR: cir.func {{.*}} @_Z16test_load_field2P3Foo
61 // CIR: %[[MEMBER_ADDR:.*]] = cir.get_member
62 // CIR: %{{.+}} = cir.load volatile{{.*}} %[[MEMBER_ADDR]]
63
64 // LLVM: define {{.*}} i32 @_Z16test_load_field2P3Foo
65 // LLVM: %[[MEMBER_ADDR:.*]] = getelementptr %struct.Foo, ptr %{{.*}}, i32 0, i32 1
66 // LLVM: %{{.*}} = load volatile i32, ptr %[[MEMBER_ADDR]]
67
68 // OGCG: define {{.*}} i32 @_Z16test_load_field2P3Foo
69 // OGCG: %[[MEMBER_ADDR:.*]] = getelementptr inbounds nuw %struct.Foo, ptr %{{.*}}, i32 0, i32 1
70 // OGCG: %{{.*}} = load volatile i32, ptr %[[MEMBER_ADDR]]
71
72 int test_load_field3(Foo *ptr) {
73 return ptr->z;
74 }
75
76 // CIR: cir.func {{.*}} @_Z16test_load_field3P3Foo
77 // CIR: %[[MEMBER_ADDR:.*]] = cir.get_member
78 // CIR: %{{.*}} = cir.get_bitfield align(4) (#bfi_z, %[[MEMBER_ADDR:.+]] {is_volatile} : !cir.ptr<!u8i>) -> !s32i
79
80 // LLVM: define {{.*}} i32 @_Z16test_load_field3P3Foo
81 // LLVM: %[[MEMBER_ADDR:.*]] = getelementptr %struct.Foo, ptr %{{.*}}, i32 0, i32 2
82 // LLVM: %[[TMP1:.*]] = load volatile i8, ptr %[[MEMBER_ADDR]]
83 // LLVM: %[[TMP2:.*]] = shl i8 %[[TMP1]], 4
84 // LLVM: %[[TMP3:.*]] = ashr i8 %[[TMP2]], 4
85 // LLVM: %{{.*}} = sext i8 %[[TMP3]] to i32
86
87 // OGCG: define {{.*}} i32 @_Z16test_load_field3P3Foo
88 // OGCG: %[[MEMBER_ADDR:.*]] = getelementptr inbounds nuw %struct.Foo, ptr %{{.*}}, i32 0, i32 2
89 // OGCG: %[[TMP1:.*]] = load volatile i8, ptr %[[MEMBER_ADDR]]
90 // OGCG: %[[TMP2:.*]] = shl i8 %[[TMP1]], 4
91 // OGCG: %[[TMP3:.*]] = ashr i8 %[[TMP2]], 4
92 // OGCG: %{{.*}} = sext i8 %[[TMP3]] to i32
93
94 void test_store_field1(volatile Foo *ptr) {
95 ptr->x = 42;
96 }
97
98 // CIR: cir.func {{.*}} @_Z17test_store_field1PV3Foo
99 // CIR: %[[MEMBER_ADDR:.*]] = cir.get_member
100 // CIR: cir.store volatile{{.*}} %{{.+}}, %[[MEMBER_ADDR]]
101
102 // LLVM: define {{.*}} void @_Z17test_store_field1PV3Foo
103 // LLVM: %[[MEMBER_ADDR:.*]] = getelementptr %struct.Foo, ptr %{{.*}}, i32 0, i32 0
104 // LLVM: store volatile i32 42, ptr %[[MEMBER_ADDR]]
105
106 // OGCG: define {{.*}} void @_Z17test_store_field1PV3Foo
107 // OGCG: %[[MEMBER_ADDR:.*]] = getelementptr inbounds nuw %struct.Foo, ptr %{{.*}}, i32 0, i32 0
108 // OGCG: store volatile i32 42, ptr %[[MEMBER_ADDR]]
109
110 void test_store_field2(Foo *ptr) {
111 ptr->y = 42;
112 }
113
114 // CIR: cir.func {{.*}} @_Z17test_store_field2P3Foo
115 // CIR: %[[MEMBER_ADDR:.*]] = cir.get_member
116 // CIR: cir.store volatile{{.*}} %{{.+}}, %[[MEMBER_ADDR]]
117
118 // LLVM: define {{.*}} void @_Z17test_store_field2P3Foo
119 // LLVM: %[[MEMBER_ADDR:.*]] = getelementptr %struct.Foo, ptr %{{.*}}, i32 0, i32 1
120 // LLVM: store volatile i32 42, ptr %[[MEMBER_ADDR]]
121
122 // OGCG: define {{.*}} void @_Z17test_store_field2P3Foo
123 // OGCG: %[[MEMBER_ADDR:.*]] = getelementptr inbounds nuw %struct.Foo, ptr %{{.*}}, i32 0, i32 1
124 // OGCG: store volatile i32 42, ptr %[[MEMBER_ADDR]]
125
126 void test_store_field3(Foo *ptr) {
127 ptr->z = 4;
128 }
129
130 // CIR: cir.func {{.*}} @_Z17test_store_field3P3Foo
131 // CIR: %[[MEMBER_ADDR:.*]] = cir.get_member
132 // CIR: cir.set_bitfield align(4) (#bfi_z, %[[MEMBER_ADDR:.+]] : !cir.ptr<!u8i>, %1 : !s32i) {is_volatile}
133
134 // LLVM: define {{.*}} void @_Z17test_store_field3P3Foo
135 // LLVM: %[[MEMBER_ADDR:.*]] = getelementptr %struct.Foo, ptr %{{.*}}, i32 0, i32 2
136 // LLVM: %[[TMP1:.*]] = load volatile i8, ptr %[[MEMBER_ADDR]]
137 // LLVM: %[[TMP2:.*]] = and i8 %[[TMP1]], -16
138 // LLVM: %[[TMP3:.*]] = or i8 %[[TMP2]], 4
139 // LLVM: store volatile i8 %[[TMP3]], ptr %[[MEMBER_ADDR]]
140
141 // OGCG: define {{.*}} void @_Z17test_store_field3P3Foo
142 // OGCG: %[[MEMBER_ADDR:.*]] = getelementptr inbounds nuw %struct.Foo, ptr %{{.*}}, i32 0, i32 2
143 // OGCG: %[[TMP1:.*]] = load volatile i8, ptr %[[MEMBER_ADDR]]
144 // OGCG: %[[TMP2:.*]] = and i8 %[[TMP1]], -16
145 // OGCG: %[[TMP3:.*]] = or i8 %[[TMP2]], 4
146 // OGCG: store volatile i8 %[[TMP3]], ptr %[[MEMBER_ADDR]]
147
148 struct A {
149 int x;
150 void set_x(int val) volatile;
151 int get_x() volatile;
152 };
153
154 void A::set_x(int val) volatile {
155 x = val;
156 }
157
158 // CIR: cir.func {{.*}} @_ZNV1A5set_xEi
159 // CIR: %[[MEMBER_ADDR:.*]] = cir.get_member %{{.*}}[0] {name = "x"}
160 // CIR: cir.store volatile {{.*}} %{{.*}}, %[[MEMBER_ADDR]]
161
162 // LLVM: define {{.*}} void @_ZNV1A5set_xEi
163 // LLVM: %[[MEMBER_ADDR:.*]] = getelementptr %struct.A, ptr %{{.*}}, i32 0, i32 0
164 // LLVM: store volatile i32 %{{.*}}, ptr %[[MEMBER_ADDR]]
165
166 // OGCG: define {{.*}} void @_ZNV1A5set_xEi
167 // OGCG: %[[MEMBER_ADDR:.*]] = getelementptr inbounds nuw %struct.A, ptr %{{.*}}, i32 0, i32 0
168 // OGCG: store volatile i32 %{{.*}}, ptr %[[MEMBER_ADDR]]
169
170 int A::get_x() volatile {
171 return x;
172 }
173
174 // CIR: cir.func {{.*}} @_ZNV1A5get_xEv
175 // CIR: %[[MEMBER_ADDR:.*]] = cir.get_member %{{.*}}[0] {name = "x"}
176 // CIR: cir.load volatile {{.*}} %[[MEMBER_ADDR]]
177
178 // LLVM: define {{.*}} i32 @_ZNV1A5get_xEv
179 // LLVM: %[[MEMBER_ADDR:.*]] = getelementptr %struct.A, ptr %{{.*}}, i32 0, i32 0
180 // LLVM: %{{.*}} = load volatile i32, ptr %[[MEMBER_ADDR]]
181
182 // OGCG: define {{.*}} i32 @_ZNV1A5get_xEv
183 // OGCG: %[[MEMBER_ADDR:.*]] = getelementptr inbounds nuw %struct.A, ptr %{{.*}}, i32 0, i32 0
184 // OGCG: %{{.*}} = load volatile i32, ptr %[[MEMBER_ADDR]]