1 ; RUN: opt -O2 %s | llvm-dis > %t1
2 ; RUN: llc -filetype=asm -o - %t1 | FileCheck -check-prefixes=CHECK,CHECK-EB,CHECK64 %s
3 ; RUN: llc -mattr=+alu32 -filetype=asm -o - %t1 | FileCheck -check-prefixes=CHECK,CHECK-EB,CHECK32 %s
11 ; FIELD_BYTE_OFFSET = 0,
18 ; int field_read(struct s *arg) {
19 ; unsigned long long ull;
20 ; unsigned offset = __builtin_preserve_field_info(arg->b2, FIELD_BYTE_OFFSET);
21 ; unsigned size = __builtin_preserve_field_info(arg->b2, FIELD_BYTE_SIZE);
24 ; ull = *(unsigned char *)((ptr)arg + offset); break;
26 ; ull = *(unsigned short *)((ptr)arg + offset); break;
28 ; ull = *(unsigned int *)((ptr)arg + offset); break;
30 ; ull = *(unsigned long long *)((ptr)arg + offset); break;
32 ; ull <<= __builtin_preserve_field_info(arg->b2, FIELD_LSHIFT_U64);
33 ; if (__builtin_preserve_field_info(arg->b2, FIELD_SIGNEDNESS))
34 ; return ((long long)ull) >>__builtin_preserve_field_info(arg->b2, FIELD_RSHIFT_U64);
35 ; return ull >> __builtin_preserve_field_info(arg->b2, FIELD_RSHIFT_U64);
38 ; clang -target bpfeb -O2 -g -S -emit-llvm -Xclang -disable-llvm-passes test.c
40 target triple = "bpfeb"
42 %struct.s = type { i32, i16 }
44 ; Function Attrs: nounwind readonly
45 define dso_local i32 @field_read(ptr %arg) local_unnamed_addr #0 !dbg !26 {
47 call void @llvm.dbg.value(metadata ptr %arg, metadata !37, metadata !DIExpression()), !dbg !41
48 %0 = tail call ptr @llvm.preserve.struct.access.index.p0.p0.ss(ptr elementtype(%struct.s) %arg, i32 1, i32 2), !dbg !42, !llvm.preserve.access.index !31
49 %1 = tail call i32 @llvm.bpf.preserve.field.info.p0(ptr %0, i64 0), !dbg !43
50 call void @llvm.dbg.value(metadata i32 %1, metadata !39, metadata !DIExpression()), !dbg !41
51 %2 = tail call i32 @llvm.bpf.preserve.field.info.p0(ptr %0, i64 1), !dbg !44
52 call void @llvm.dbg.value(metadata i32 %2, metadata !40, metadata !DIExpression()), !dbg !41
53 switch i32 %2, label %sw.epilog [
60 sw.bb: ; preds = %entry
61 %idx.ext = zext i32 %1 to i64, !dbg !48
62 %add.ptr = getelementptr i8, ptr %arg, i64 %idx.ext, !dbg !48
63 %3 = load i8, ptr %add.ptr, align 1, !dbg !49, !tbaa !50
64 %conv = zext i8 %3 to i64, !dbg !49
65 call void @llvm.dbg.value(metadata i64 %conv, metadata !38, metadata !DIExpression()), !dbg !41
66 br label %sw.epilog, !dbg !53
68 sw.bb1: ; preds = %entry
69 %idx.ext2 = zext i32 %1 to i64, !dbg !55
70 %add.ptr3 = getelementptr i8, ptr %arg, i64 %idx.ext2, !dbg !55
71 %4 = load i16, ptr %add.ptr3, align 2, !dbg !57, !tbaa !58
72 %conv4 = zext i16 %4 to i64, !dbg !57
73 call void @llvm.dbg.value(metadata i64 %conv4, metadata !38, metadata !DIExpression()), !dbg !41
74 br label %sw.epilog, !dbg !60
76 sw.bb5: ; preds = %entry
77 %idx.ext6 = zext i32 %1 to i64, !dbg !62
78 %add.ptr7 = getelementptr i8, ptr %arg, i64 %idx.ext6, !dbg !62
79 %5 = load i32, ptr %add.ptr7, align 4, !dbg !64, !tbaa !65
80 %conv8 = zext i32 %5 to i64, !dbg !64
81 call void @llvm.dbg.value(metadata i64 %conv8, metadata !38, metadata !DIExpression()), !dbg !41
82 br label %sw.epilog, !dbg !67
84 sw.bb9: ; preds = %entry
85 %idx.ext10 = zext i32 %1 to i64, !dbg !69
86 %add.ptr11 = getelementptr i8, ptr %arg, i64 %idx.ext10, !dbg !69
87 %6 = load i64, ptr %add.ptr11, align 8, !dbg !71, !tbaa !72
88 call void @llvm.dbg.value(metadata i64 %6, metadata !38, metadata !DIExpression()), !dbg !41
89 br label %sw.epilog, !dbg !74
91 sw.epilog: ; preds = %entry, %sw.bb9, %sw.bb5, %sw.bb1, %sw.bb
92 %ull.0 = phi i64 [ undef, %entry ], [ %6, %sw.bb9 ], [ %conv8, %sw.bb5 ], [ %conv4, %sw.bb1 ], [ %conv, %sw.bb ]
93 call void @llvm.dbg.value(metadata i64 %ull.0, metadata !38, metadata !DIExpression()), !dbg !41
94 %7 = tail call i32 @llvm.bpf.preserve.field.info.p0(ptr %0, i64 4), !dbg !75
95 %sh_prom = zext i32 %7 to i64, !dbg !76
96 %shl = shl i64 %ull.0, %sh_prom, !dbg !76
97 call void @llvm.dbg.value(metadata i64 %shl, metadata !38, metadata !DIExpression()), !dbg !41
98 %8 = tail call i32 @llvm.bpf.preserve.field.info.p0(ptr %0, i64 3), !dbg !77
99 %tobool = icmp eq i32 %8, 0, !dbg !77
100 %9 = tail call i32 @llvm.bpf.preserve.field.info.p0(ptr %0, i64 5), !dbg !41
101 %sh_prom12 = zext i32 %9 to i64, !dbg !41
102 %shr = ashr i64 %shl, %sh_prom12, !dbg !79
103 %shr15 = lshr i64 %shl, %sh_prom12, !dbg !79
104 %retval.0.in = select i1 %tobool, i64 %shr15, i64 %shr, !dbg !79
105 %retval.0 = trunc i64 %retval.0.in to i32, !dbg !41
106 ret i32 %retval.0, !dbg !80
109 ; CHECK: r{{[0-9]+}} = 4
110 ; CHECK: r{{[0-9]+}} = 4
111 ; CHECK-EB: r{{[0-9]+}} <<= 41
112 ; CHECK64: r{{[0-9]+}} s>>= 60
113 ; CHECK64: r{{[0-9]+}} >>= 60
114 ; CHECK32: r{{[0-9]+}} >>= 60
115 ; CHECK32: r{{[0-9]+}} s>>= 60
116 ; CHECK: r{{[0-9]+}} = 1
118 ; CHECK: .long 1 # BTF_KIND_STRUCT(id = 2)
119 ; CHECK: .byte 115 # string offset=1
120 ; CHECK: .ascii ".text" # string offset=30
121 ; CHECK: .ascii "0:2" # string offset=36
123 ; CHECK: .long 16 # FieldReloc
124 ; CHECK-NEXT: .long 30 # Field reloc section string offset=30
125 ; CHECK-NEXT: .long 8
126 ; CHECK-NEXT: .long .Ltmp{{[0-9]+}}
127 ; CHECK-NEXT: .long 2
128 ; CHECK-NEXT: .long 36
129 ; CHECK-NEXT: .long 1
130 ; CHECK-NEXT: .long .Ltmp{{[0-9]+}}
131 ; CHECK-NEXT: .long 2
132 ; CHECK-NEXT: .long 36
133 ; CHECK-NEXT: .long 0
134 ; CHECK-NEXT: .long .Ltmp{{[0-9]+}}
135 ; CHECK-NEXT: .long 2
136 ; CHECK-NEXT: .long 36
137 ; CHECK-NEXT: .long 0
138 ; CHECK-NEXT: .long .Ltmp{{[0-9]+}}
139 ; CHECK-NEXT: .long 2
140 ; CHECK-NEXT: .long 36
141 ; CHECK-NEXT: .long 0
142 ; CHECK-NEXT: .long .Ltmp{{[0-9]+}}
143 ; CHECK-NEXT: .long 2
144 ; CHECK-NEXT: .long 36
145 ; CHECK-NEXT: .long 4
146 ; CHECK-NEXT: .long .Ltmp{{[0-9]+}}
147 ; CHECK-NEXT: .long 2
148 ; CHECK-NEXT: .long 36
149 ; CHECK-NEXT: .long 5
150 ; CHECK-NEXT: .long .Ltmp{{[0-9]+}}
151 ; CHECK-NEXT: .long 2
152 ; CHECK-NEXT: .long 36
153 ; CHECK-NEXT: .long 5
154 ; CHECK-NEXT: .long .Ltmp{{[0-9]+}}
155 ; CHECK-NEXT: .long 2
156 ; CHECK-NEXT: .long 36
157 ; CHECK-NEXT: .long 3
159 ; Function Attrs: nounwind readnone
160 declare ptr @llvm.preserve.struct.access.index.p0.p0.ss(ptr, i32, i32) #1
162 ; Function Attrs: nounwind readnone
163 declare i32 @llvm.bpf.preserve.field.info.p0(ptr, i64) #1
165 ; Function Attrs: nounwind readnone speculatable willreturn
166 declare void @llvm.dbg.value(metadata, metadata, metadata) #2
168 attributes #0 = { nounwind readonly "correctly-rounded-divide-sqrt-fp-math"="false" "disable-tail-calls"="false" "frame-pointer"="all" "less-precise-fpmad"="false" "min-legal-vector-width"="0" "no-infs-fp-math"="false" "no-jump-tables"="false" "no-nans-fp-math"="false" "no-signed-zeros-fp-math"="false" "no-trapping-math"="false" "stack-protector-buffer-size"="8" "unsafe-fp-math"="false" "use-soft-float"="false" }
169 attributes #1 = { nounwind readnone }
170 attributes #2 = { nounwind readnone speculatable willreturn }
173 !llvm.module.flags = !{!22, !23, !24}
176 !0 = distinct !DICompileUnit(language: DW_LANG_C99, file: !1, producer: "clang version 10.0.0 (https://github.com/llvm/llvm-project.git 923aa0ce806f7739b754167239fee2c9a15e2f31)", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, enums: !2, retainedTypes: !12, nameTableKind: None)
177 !1 = !DIFile(filename: "test.c", directory: "/tmp/home/yhs/work/tests/core")
179 !3 = !DICompositeType(tag: DW_TAG_enumeration_type, file: !1, line: 6, baseType: !4, size: 32, elements: !5)
180 !4 = !DIBasicType(name: "unsigned int", size: 32, encoding: DW_ATE_unsigned)
181 !5 = !{!6, !7, !8, !9, !10, !11}
182 !6 = !DIEnumerator(name: "FIELD_BYTE_OFFSET", value: 0, isUnsigned: true)
183 !7 = !DIEnumerator(name: "FIELD_BYTE_SIZE", value: 1, isUnsigned: true)
184 !8 = !DIEnumerator(name: "FIELD_EXISTENCE", value: 2, isUnsigned: true)
185 !9 = !DIEnumerator(name: "FIELD_SIGNEDNESS", value: 3, isUnsigned: true)
186 !10 = !DIEnumerator(name: "FIELD_LSHIFT_U64", value: 4, isUnsigned: true)
187 !11 = !DIEnumerator(name: "FIELD_RSHIFT_U64", value: 5, isUnsigned: true)
188 !12 = !{!13, !15, !16, !18, !19, !21}
189 !13 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !14, size: 64)
190 !14 = !DIBasicType(name: "unsigned char", size: 8, encoding: DW_ATE_unsigned_char)
191 !15 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: null, size: 64)
192 !16 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !17, size: 64)
193 !17 = !DIBasicType(name: "unsigned short", size: 16, encoding: DW_ATE_unsigned)
194 !18 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !4, size: 64)
195 !19 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !20, size: 64)
196 !20 = !DIBasicType(name: "long long unsigned int", size: 64, encoding: DW_ATE_unsigned)
197 !21 = !DIBasicType(name: "long long int", size: 64, encoding: DW_ATE_signed)
198 !22 = !{i32 2, !"Dwarf Version", i32 4}
199 !23 = !{i32 2, !"Debug Info Version", i32 3}
200 !24 = !{i32 1, !"wchar_size", i32 4}
201 !25 = !{!"clang version 10.0.0 (https://github.com/llvm/llvm-project.git 923aa0ce806f7739b754167239fee2c9a15e2f31)"}
202 !26 = distinct !DISubprogram(name: "field_read", scope: !1, file: !1, line: 14, type: !27, scopeLine: 14, flags: DIFlagPrototyped, isDefinition: true, isOptimized: true, unit: !0, retainedNodes: !36)
203 !27 = !DISubroutineType(types: !28)
205 !29 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
206 !30 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !31, size: 64)
207 !31 = distinct !DICompositeType(tag: DW_TAG_structure_type, name: "s", file: !1, line: 1, size: 64, elements: !32)
208 !32 = !{!33, !34, !35}
209 !33 = !DIDerivedType(tag: DW_TAG_member, name: "a", scope: !31, file: !1, line: 2, baseType: !29, size: 32)
210 !34 = !DIDerivedType(tag: DW_TAG_member, name: "b1", scope: !31, file: !1, line: 3, baseType: !29, size: 9, offset: 32, flags: DIFlagBitField, extraData: i64 32)
211 !35 = !DIDerivedType(tag: DW_TAG_member, name: "b2", scope: !31, file: !1, line: 4, baseType: !29, size: 4, offset: 41, flags: DIFlagBitField, extraData: i64 32)
212 !36 = !{!37, !38, !39, !40}
213 !37 = !DILocalVariable(name: "arg", arg: 1, scope: !26, file: !1, line: 14, type: !30)
214 !38 = !DILocalVariable(name: "ull", scope: !26, file: !1, line: 15, type: !20)
215 !39 = !DILocalVariable(name: "offset", scope: !26, file: !1, line: 16, type: !4)
216 !40 = !DILocalVariable(name: "size", scope: !26, file: !1, line: 17, type: !4)
217 !41 = !DILocation(line: 0, scope: !26)
218 !42 = !DILocation(line: 16, column: 56, scope: !26)
219 !43 = !DILocation(line: 16, column: 21, scope: !26)
220 !44 = !DILocation(line: 17, column: 19, scope: !26)
221 !45 = !DILocation(line: 18, column: 3, scope: !26)
222 !46 = !DILocation(line: 20, column: 30, scope: !47)
223 !47 = distinct !DILexicalBlock(scope: !26, file: !1, line: 18, column: 16)
224 !48 = !DILocation(line: 20, column: 42, scope: !47)
225 !49 = !DILocation(line: 20, column: 11, scope: !47)
226 !50 = !{!51, !51, i64 0}
227 !51 = !{!"omnipotent char", !52, i64 0}
228 !52 = !{!"Simple C/C++ TBAA"}
229 !53 = !DILocation(line: 20, column: 53, scope: !47)
230 !54 = !DILocation(line: 22, column: 31, scope: !47)
231 !55 = !DILocation(line: 22, column: 43, scope: !47)
232 !56 = !DILocation(line: 22, column: 12, scope: !47)
233 !57 = !DILocation(line: 22, column: 11, scope: !47)
234 !58 = !{!59, !59, i64 0}
235 !59 = !{!"short", !51, i64 0}
236 !60 = !DILocation(line: 22, column: 54, scope: !47)
237 !61 = !DILocation(line: 24, column: 29, scope: !47)
238 !62 = !DILocation(line: 24, column: 41, scope: !47)
239 !63 = !DILocation(line: 24, column: 12, scope: !47)
240 !64 = !DILocation(line: 24, column: 11, scope: !47)
241 !65 = !{!66, !66, i64 0}
242 !66 = !{!"int", !51, i64 0}
243 !67 = !DILocation(line: 24, column: 52, scope: !47)
244 !68 = !DILocation(line: 26, column: 35, scope: !47)
245 !69 = !DILocation(line: 26, column: 47, scope: !47)
246 !70 = !DILocation(line: 26, column: 12, scope: !47)
247 !71 = !DILocation(line: 26, column: 11, scope: !47)
248 !72 = !{!73, !73, i64 0}
249 !73 = !{!"long long", !51, i64 0}
250 !74 = !DILocation(line: 26, column: 58, scope: !47)
251 !75 = !DILocation(line: 28, column: 11, scope: !26)
252 !76 = !DILocation(line: 28, column: 7, scope: !26)
253 !77 = !DILocation(line: 29, column: 7, scope: !78)
254 !78 = distinct !DILexicalBlock(scope: !26, file: !1, line: 29, column: 7)
255 !79 = !DILocation(line: 29, column: 7, scope: !26)
256 !80 = !DILocation(line: 32, column: 1, scope: !26)