Debian Codegraph @

clang/test/CIR/CodeGen/label-values.c

raw
1 // RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir -emit-cir %s -o %t.cir
2 // RUN: FileCheck --input-file=%t.cir %s --check-prefix=CIR
3 // RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -emit-llvm %s -o %t.ll
4 // RUN: FileCheck --input-file=%t.ll %s --check-prefix=OGCG
5
6 void A(void) {
7 void *ptr = &&LABEL_A;
8 goto *ptr;
9 LABEL_A:
10 return;
11 }
12 // CIR: cir.func {{.*}} @A
13 // CIR: [[PTR:%.*]] = cir.alloca !cir.ptr<!void>, !cir.ptr<!cir.ptr<!void>>, ["ptr", init] {alignment = 8 : i64}
14 // CIR: [[BLOCK:%.*]] = cir.block_address <@A, "LABEL_A"> : !cir.ptr<!void>
15 // CIR: cir.store align(8) [[BLOCK]], [[PTR]] : !cir.ptr<!void>, !cir.ptr<!cir.ptr<!void>>
16 // CIR: [[BLOCKADD:%.*]] = cir.load align(8) [[PTR]] : !cir.ptr<!cir.ptr<!void>>, !cir.ptr<!void>
17 // CIR: cir.br ^bb1([[BLOCKADD]] : !cir.ptr<!void>)
18 // CIR: ^bb1([[PHI:%.*]]: !cir.ptr<!void> {{.*}}): // pred: ^bb0
19 // CIR: cir.indirect_br [[PHI]] : !cir.ptr<!void>, [
20 // CIR: ^bb2
21 // CIR: ]
22 // CIR: ^bb2: // pred: ^bb1
23 // CIR: cir.label "LABEL_A"
24 // CIR: cir.return
25
26 // OGCG: define dso_local void @A()
27 // OGCG: [[PTR:%.*]] = alloca ptr, align 8
28 // OGCG: store ptr blockaddress(@A, %LABEL_A), ptr [[PTR]], align 8
29 // OGCG: [[BLOCKADD:%.*]] = load ptr, ptr [[PTR]], align 8
30 // OGCG: br label %indirectgoto
31 // OGCG: LABEL_A: ; preds = %indirectgoto
32 // OGCG: ret void
33 // OGCG: indirectgoto: ; preds = %entry
34 // OGCG: %indirect.goto.dest = phi ptr [ [[BLOCKADD]], %entry ]
35 // OGCG: indirectbr ptr %indirect.goto.dest, [label %LABEL_A]
36
37 void B(void) {
38 LABEL_B:
39 void *ptr = &&LABEL_B;
40 goto *ptr;
41 }
42
43 // CIR: cir.func {{.*}} @B()
44 // CIR: [[PTR:%.*]] = cir.alloca !cir.ptr<!void>, !cir.ptr<!cir.ptr<!void>>, ["ptr", init] {alignment = 8 : i64}
45 // CIR: cir.br ^bb1
46 // CIR: ^bb1: // 2 preds: ^bb0, ^bb2
47 // CIR: cir.label "LABEL_B"
48 // CIR: [[BLOCK:%.*]] = cir.block_address <@B, "LABEL_B"> : !cir.ptr<!void>
49 // CIR: cir.store align(8) [[BLOCK]], [[PTR]] : !cir.ptr<!void>, !cir.ptr<!cir.ptr<!void>>
50 // CIR: [[BLOCKADD:%.*]] = cir.load align(8) [[PTR]] : !cir.ptr<!cir.ptr<!void>>, !cir.ptr<!void>
51 // CIR: cir.br ^bb2([[BLOCKADD]] : !cir.ptr<!void>)
52 // CIR: ^bb2([[PHI:%.*]]: !cir.ptr<!void> {{.*}}): // pred: ^bb1
53 // CIR: cir.indirect_br [[PHI]] : !cir.ptr<!void>, [
54 // CIR-NEXT: ^bb1
55 // CIR: ]
56
57 // OGCG: define dso_local void @B
58 // OGCG: [[PTR:%.*]] = alloca ptr, align 8
59 // OGCG: br label %LABEL_B
60 // OGCG: LABEL_B: ; preds = %indirectgoto, %entry
61 // OGCG: store ptr blockaddress(@B, %LABEL_B), ptr [[PTR]], align 8
62 // OGCG: [[BLOCKADD:%.*]] = load ptr, ptr [[PTR]], align 8
63 // OGCG: br label %indirectgoto
64 // OGCG: indirectgoto: ; preds = %LABEL_B
65 // OGCG: %indirect.goto.dest = phi ptr [ [[BLOCKADD]], %LABEL_B ]
66 // OGCG: indirectbr ptr %indirect.goto.dest, [label %LABEL_B]
67
68 void C(int x) {
69 void *ptr = (x == 0) ? &&LABEL_A : &&LABEL_B;
70 goto *ptr;
71 LABEL_A:
72 return;
73 LABEL_B:
74 return;
75 }
76
77 // CIR: cir.func {{.*}} @C
78 // CIR: [[BLOCK1:%.*]] = cir.block_address <@C, "LABEL_A"> : !cir.ptr<!void>
79 // CIR: [[BLOCK2:%.*]] = cir.block_address <@C, "LABEL_B"> : !cir.ptr<!void>
80 // CIR: [[COND:%.*]] = cir.select if [[CMP:%.*]] then [[BLOCK1]] else [[BLOCK2]] : (!cir.bool, !cir.ptr<!void>, !cir.ptr<!void>) -> !cir.ptr<!void>
81 // CIR: cir.store align(8) [[COND]], [[PTR:%.*]] : !cir.ptr<!void>, !cir.ptr<!cir.ptr<!void>>
82 // CIR: [[BLOCKADD:%.*]] = cir.load align(8) [[PTR]] : !cir.ptr<!cir.ptr<!void>>, !cir.ptr<!void>
83 // CIR: cir.br ^bb1([[BLOCKADD]] : !cir.ptr<!void>)
84 // CIR: ^bb1([[PHI:%.*]]: !cir.ptr<!void> {{.*}}): // pred: ^bb0
85 // CIR: cir.indirect_br [[PHI]] : !cir.ptr<!void>, [
86 // CIR-NEXT: ^bb2,
87 // CIR-NEXT: ^bb4
88 // CIR: ]
89 // CIR: ^bb2: // pred: ^bb1
90 // CIR: cir.label "LABEL_A"
91 // CIR: cir.br ^bb3
92 // CIR: ^bb3: // 2 preds: ^bb2, ^bb4
93 // CIR: cir.return
94 // CIR: ^bb4: // pred: ^bb1
95 // CIR: cir.label "LABEL_B"
96 // CIR: cir.br ^bb3
97
98 // OGCG: define dso_local void @C
99 // OGCG: [[COND:%.*]] = select i1 [[CMP:%.*]], ptr blockaddress(@C, %LABEL_A), ptr blockaddress(@C, %LABEL_B)
100 // OGCG: store ptr [[COND]], ptr [[PTR:%.*]], align 8
101 // OGCG: [[BLOCKADD:%.*]] = load ptr, ptr [[PTR]], align 8
102 // OGCG: br label %indirectgoto
103 // OGCG: LABEL_A: ; preds = %indirectgoto
104 // OGCG: br label %return
105 // OGCG: LABEL_B: ; preds = %indirectgoto
106 // OGCG: br label %return
107 // OGCG: return: ; preds = %LABEL_B, %LABEL_A
108 // OGCG: ret void
109 // OGCG: indirectgoto: ; preds = %entry
110 // OGCG: %indirect.goto.dest = phi ptr [ [[BLOCKADD]], %entry ]
111 // OGCG: indirectbr ptr %indirect.goto.dest, [label %LABEL_A, label %LABEL_B]
112
113 void D(void) {
114 void *ptr = &&LABEL_A;
115 void *ptr2 = &&LABEL_A;
116 goto *ptr2;
117 LABEL_A:
118 void *ptr3 = &&LABEL_A;
119 return;
120 }
121
122 // CIR: cir.func {{.*}} @D
123 // CIR: %[[PTR:.*]] = cir.alloca !cir.ptr<!void>, !cir.ptr<!cir.ptr<!void>>, ["ptr", init]
124 // CIR: %[[PTR2:.*]] = cir.alloca !cir.ptr<!void>, !cir.ptr<!cir.ptr<!void>>, ["ptr2", init]
125 // CIR: %[[PTR3:.*]] = cir.alloca !cir.ptr<!void>, !cir.ptr<!cir.ptr<!void>>, ["ptr3", init]
126 // CIR: %[[BLK1:.*]] = cir.block_address <@D, "LABEL_A"> : !cir.ptr<!void>
127 // CIR: cir.store align(8) %[[BLK1]], %[[PTR]] : !cir.ptr<!void>, !cir.ptr<!cir.ptr<!void>>
128 // CIR: %[[BLK2:.*]] = cir.block_address <@D, "LABEL_A"> : !cir.ptr<!void>
129 // CIR: cir.store align(8) %[[BLK2]], %[[PTR2]] : !cir.ptr<!void>, !cir.ptr<!cir.ptr<!void>>
130 // CIR: cir.br ^bb1
131 // CIR: ^bb1([[PHI:%*.]]: !cir.ptr<!void> {{.*}}): // pred: ^bb0
132 // CIR: cir.indirect_br [[PHI]] : !cir.ptr<!void>, [
133 // CIR-DAG: ^bb2,
134 // CIR-DAG: ^bb2,
135 // CIR-DAG: ^bb2
136 // CIR: ]
137 // CIR: ^bb2: // 3 preds: ^bb1, ^bb1, ^bb1
138 // CIR: cir.label "LABEL_A"
139 // CIR: %[[BLK3:.*]] = cir.block_address <@D, "LABEL_A"> : !cir.ptr<!void>
140 // CIR: cir.store align(8) %[[BLK3]], %[[PTR3]] : !cir.ptr<!void>, !cir.ptr<!cir.ptr<!void>>
141 // CIR: cir.return
142
143 // OGCG: define dso_local void @D
144 // OGCG: %[[PTR:.*]] = alloca ptr, align 8
145 // OGCG: %[[PTR2:.*]] = alloca ptr, align 8
146 // OGCG: %[[PTR3:.*]] = alloca ptr, align 8
147 // OGCG: store ptr blockaddress(@D, %LABEL_A), ptr %[[PTR]], align 8
148 // OGCG: store ptr blockaddress(@D, %LABEL_A), ptr %[[PTR2]], align 8
149 // OGCG: %[[BLOCKADD:.*]] = load ptr, ptr %[[PTR2]], align 8
150 // OGCG: br label %indirectgoto
151 // OGCG: LABEL_A: ; preds = %indirectgoto, %indirectgoto, %indirectgoto
152 // OGCG: store ptr blockaddress(@D, %LABEL_A), ptr %[[PTR3]], align 8
153 // OGCG: ret void
154 // OGCG: indirectgoto: ; preds = %entry
155 // OGCG: %indirect.goto.dest = phi ptr [ %[[BLOCKADD]], %entry ]
156 // OGCG: indirectbr ptr %indirect.goto.dest, [label %LABEL_A, label %LABEL_A, label %LABEL_A]
157
158 // This test checks that CIR preserves insertion order of blockaddresses
159 // for indirectbr, even if some were resolved immediately and others later.
160 void E(void) {
161 void *ptr = &&LABEL_D;
162 void *ptr2 = &&LABEL_C;
163 LABEL_A:
164 LABEL_B:
165 void *ptr3 = &&LABEL_B;
166 void *ptr4 = &&LABEL_A;
167 LABEL_C:
168 LABEL_D:
169 return;
170 }
171
172 //CIR: cir.func {{.*}} @E()
173 //CIR: ^bb1({{.*}}: !cir.ptr<!void> {{.*}}): // no predecessors
174 //CIR: cir.indirect_br {{.*}} poison : !cir.ptr<!void>, [
175 //CIR-NEXT: ^bb5,
176 //CIR-NEXT: ^bb4,
177 //CIR-NEXT: ^bb3,
178 //CIR-NEXT: ^bb2
179 //CIR: ]
180 //CIR: ^bb2: // 2 preds: ^bb0, ^bb1
181 //CIR: cir.label "LABEL_A"
182 //CIR: ^bb3: // 2 preds: ^bb1, ^bb2
183 //CIR: cir.label "LABEL_B"
184 //CIR: ^bb4: // 2 preds: ^bb1, ^bb3
185 //CIR: cir.label "LABEL_C"
186 //CIR: ^bb5: // 2 preds: ^bb1, ^bb4
187 //CIR: cir.label "LABEL_D"
188
189 // OGCG: define dso_local void @E() #0 {
190 // OGCG: store ptr blockaddress(@E, %LABEL_D), ptr %ptr, align 8
191 // OGCG: store ptr blockaddress(@E, %LABEL_C), ptr %ptr2, align 8
192 // OGCG: br label %LABEL_A
193 // OGCG: A: ; preds = %indirectgoto, %entry
194 // OGCG: br label %LABEL_B
195 // OGCG: B: ; preds = %indirectgoto, %LABEL_A
196 // OGCG: store ptr blockaddress(@E, %LABEL_B), ptr %ptr3, align 8
197 // OGCG: store ptr blockaddress(@E, %LABEL_A), ptr %ptr4, align 8
198 // OGCG: br label %LABEL_C
199 // OGCG: C: ; preds = %LABEL_B, %indirectgoto
200 // OGCG: br label %LABEL_D
201 // OGCG: D: ; preds = %LABEL_C, %indirectgoto
202 // OGCG: ret void
203 // OGCG: indirectgoto: ; No predecessors!
204 // OGCG: indirectbr ptr poison, [label %LABEL_D, label %LABEL_C, label %LABEL_B, label %LABEL_A]