1 ; RUN: llc -march=bpfel -filetype=asm -o - %s | FileCheck -check-prefixes=CHECK,CHECK-EL %s
2 ; RUN: llc -march=bpfeb -filetype=asm -o - %s | FileCheck -check-prefixes=CHECK,CHECK-EB %s
10 ; FIELD_BYTE_OFFSET = 0,
17 ; int field_read(struct s *arg) {
18 ; unsigned long long ull;
19 ; unsigned offset = __builtin_preserve_field_info(arg->b2, FIELD_BYTE_OFFSET);
20 ; unsigned size = __builtin_preserve_field_info(arg->b2, FIELD_BYTE_SIZE);
23 ; ull = *(unsigned char *)((void *)arg + offset); break;
25 ; ull = *(unsigned short *)((void *)arg + offset); break;
27 ; ull = *(unsigned int *)((void *)arg + offset); break;
29 ; ull = *(unsigned long long *)((void *)arg + offset); break;
31 ; ull <<= __builtin_preserve_field_info(arg->b2, FIELD_LSHIFT_U64);
32 ; if (__builtin_preserve_field_info(arg->b2, FIELD_SIGNEDNESS))
33 ; return ((long long)ull) >>__builtin_preserve_field_info(arg->b2, FIELD_RSHIFT_U64);
34 ; return ull >> __builtin_preserve_field_info(arg->b2, FIELD_RSHIFT_U64);
37 ; clang -target bpf -O2 -g -S -emit-llvm test.c
39 %struct.s = type { i32, i16 }
41 ; Function Attrs: nounwind readonly
42 define dso_local i32 @field_read(%struct.s* %arg) local_unnamed_addr #0 !dbg !26 {
44 call void @llvm.dbg.value(metadata %struct.s* %arg, metadata !37, metadata !DIExpression()), !dbg !41
45 %0 = tail call i16* @llvm.preserve.struct.access.index.p0i16.p0s_struct.ss(%struct.s* %arg, i32 1, i32 2), !dbg !42, !llvm.preserve.access.index !31
46 %1 = tail call i32 @llvm.bpf.preserve.field.info.p0i16(i16* %0, i64 0), !dbg !43
47 call void @llvm.dbg.value(metadata i32 %1, metadata !39, metadata !DIExpression()), !dbg !41
48 %2 = tail call i32 @llvm.bpf.preserve.field.info.p0i16(i16* %0, i64 1), !dbg !44
49 call void @llvm.dbg.value(metadata i32 %2, metadata !40, metadata !DIExpression()), !dbg !41
50 switch i32 %2, label %sw.epilog [
57 sw.bb: ; preds = %entry
58 %3 = bitcast %struct.s* %arg to i8*, !dbg !46
59 %idx.ext = zext i32 %1 to i64, !dbg !48
60 %add.ptr = getelementptr i8, i8* %3, i64 %idx.ext, !dbg !48
61 %4 = load i8, i8* %add.ptr, align 1, !dbg !49, !tbaa !50
62 %conv = zext i8 %4 to i64, !dbg !49
63 call void @llvm.dbg.value(metadata i64 %conv, metadata !38, metadata !DIExpression()), !dbg !41
64 br label %sw.epilog, !dbg !53
66 sw.bb1: ; preds = %entry
67 %5 = bitcast %struct.s* %arg to i8*, !dbg !54
68 %idx.ext2 = zext i32 %1 to i64, !dbg !55
69 %add.ptr3 = getelementptr i8, i8* %5, i64 %idx.ext2, !dbg !55
70 %6 = bitcast i8* %add.ptr3 to i16*, !dbg !56
71 %7 = load i16, i16* %6, align 2, !dbg !57, !tbaa !58
72 %conv4 = zext i16 %7 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 %8 = bitcast %struct.s* %arg to i8*, !dbg !61
78 %idx.ext6 = zext i32 %1 to i64, !dbg !62
79 %add.ptr7 = getelementptr i8, i8* %8, i64 %idx.ext6, !dbg !62
80 %9 = bitcast i8* %add.ptr7 to i32*, !dbg !63
81 %10 = load i32, i32* %9, align 4, !dbg !64, !tbaa !65
82 %conv8 = zext i32 %10 to i64, !dbg !64
83 call void @llvm.dbg.value(metadata i64 %conv8, metadata !38, metadata !DIExpression()), !dbg !41
84 br label %sw.epilog, !dbg !67
86 sw.bb9: ; preds = %entry
87 %11 = bitcast %struct.s* %arg to i8*, !dbg !68
88 %idx.ext10 = zext i32 %1 to i64, !dbg !69
89 %add.ptr11 = getelementptr i8, i8* %11, i64 %idx.ext10, !dbg !69
90 %12 = bitcast i8* %add.ptr11 to i64*, !dbg !70
91 %13 = load i64, i64* %12, align 8, !dbg !71, !tbaa !72
92 call void @llvm.dbg.value(metadata i64 %13, metadata !38, metadata !DIExpression()), !dbg !41
93 br label %sw.epilog, !dbg !74
95 sw.epilog: ; preds = %entry, %sw.bb9, %sw.bb5, %sw.bb1, %sw.bb
96 %ull.0 = phi i64 [ undef, %entry ], [ %13, %sw.bb9 ], [ %conv8, %sw.bb5 ], [ %conv4, %sw.bb1 ], [ %conv, %sw.bb ]
97 call void @llvm.dbg.value(metadata i64 %ull.0, metadata !38, metadata !DIExpression()), !dbg !41
98 %14 = tail call i32 @llvm.bpf.preserve.field.info.p0i16(i16* %0, i64 4), !dbg !75
99 %sh_prom = zext i32 %14 to i64, !dbg !76
100 %shl = shl i64 %ull.0, %sh_prom, !dbg !76
101 call void @llvm.dbg.value(metadata i64 %shl, metadata !38, metadata !DIExpression()), !dbg !41
102 %15 = tail call i32 @llvm.bpf.preserve.field.info.p0i16(i16* %0, i64 3), !dbg !77
103 %tobool = icmp eq i32 %15, 0, !dbg !77
104 %16 = tail call i32 @llvm.bpf.preserve.field.info.p0i16(i16* %0, i64 5), !dbg !41
105 %sh_prom12 = zext i32 %16 to i64, !dbg !41
106 %shr = ashr i64 %shl, %sh_prom12, !dbg !79
107 %shr15 = lshr i64 %shl, %sh_prom12, !dbg !79
108 %retval.0.in = select i1 %tobool, i64 %shr15, i64 %shr, !dbg !79
109 %retval.0 = trunc i64 %retval.0.in to i32, !dbg !41
110 ret i32 %retval.0, !dbg !80
113 ; CHECK: r{{[0-9]+}} = 4
114 ; CHECK: r{{[0-9]+}} = 4
115 ; CHECK-EL: r{{[0-9]+}} = 51
116 ; CHECK-EB: r{{[0-9]+}} = 41
117 ; CHECK: r{{[0-9]+}} = 60
118 ; CHECK: r{{[0-9]+}} = 1
120 ; CHECK: .long 1 # BTF_KIND_STRUCT(id = 2)
121 ; CHECK: .byte 115 # string offset=1
122 ; CHECK: .ascii ".text" # string offset=30
123 ; CHECK: .ascii "0:2" # string offset=36
125 ; CHECK: .long 16 # FieldReloc
126 ; CHECK-NEXT: .long 30 # Field reloc section string offset=30
127 ; CHECK-NEXT: .long 5
128 ; CHECK-NEXT: .long .Ltmp{{[0-9]+}}
129 ; CHECK-NEXT: .long 2
130 ; CHECK-NEXT: .long 36
131 ; CHECK-NEXT: .long 0
132 ; CHECK-NEXT: .long .Ltmp{{[0-9]+}}
133 ; CHECK-NEXT: .long 2
134 ; CHECK-NEXT: .long 36
135 ; CHECK-NEXT: .long 1
136 ; CHECK-NEXT: .long .Ltmp{{[0-9]+}}
137 ; CHECK-NEXT: .long 2
138 ; CHECK-NEXT: .long 36
139 ; CHECK-NEXT: .long 4
140 ; CHECK-NEXT: .long .Ltmp{{[0-9]+}}
141 ; CHECK-NEXT: .long 2
142 ; CHECK-NEXT: .long 36
143 ; CHECK-NEXT: .long 5
144 ; CHECK-NEXT: .long .Ltmp{{[0-9]+}}
145 ; CHECK-NEXT: .long 2
146 ; CHECK-NEXT: .long 36
147 ; CHECK-NEXT: .long 3
149 ; Function Attrs: nounwind readnone
150 declare i16* @llvm.preserve.struct.access.index.p0i16.p0s_struct.ss(%struct.s*, i32, i32) #1
152 ; Function Attrs: nounwind readnone
153 declare i32 @llvm.bpf.preserve.field.info.p0i16(i16*, i64) #1
155 ; Function Attrs: nounwind readnone speculatable willreturn
156 declare void @llvm.dbg.value(metadata, metadata, metadata) #2
158 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" }
159 attributes #1 = { nounwind readnone }
160 attributes #2 = { nounwind readnone speculatable willreturn }
163 !llvm.module.flags = !{!22, !23, !24}
166 !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)
167 !1 = !DIFile(filename: "test.c", directory: "/tmp/home/yhs/work/tests/core")
169 !3 = !DICompositeType(tag: DW_TAG_enumeration_type, file: !1, line: 6, baseType: !4, size: 32, elements: !5)
170 !4 = !DIBasicType(name: "unsigned int", size: 32, encoding: DW_ATE_unsigned)
171 !5 = !{!6, !7, !8, !9, !10, !11}
172 !6 = !DIEnumerator(name: "FIELD_BYTE_OFFSET", value: 0, isUnsigned: true)
173 !7 = !DIEnumerator(name: "FIELD_BYTE_SIZE", value: 1, isUnsigned: true)
174 !8 = !DIEnumerator(name: "FIELD_EXISTENCE", value: 2, isUnsigned: true)
175 !9 = !DIEnumerator(name: "FIELD_SIGNEDNESS", value: 3, isUnsigned: true)
176 !10 = !DIEnumerator(name: "FIELD_LSHIFT_U64", value: 4, isUnsigned: true)
177 !11 = !DIEnumerator(name: "FIELD_RSHIFT_U64", value: 5, isUnsigned: true)
178 !12 = !{!13, !15, !16, !18, !19, !21}
179 !13 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !14, size: 64)
180 !14 = !DIBasicType(name: "unsigned char", size: 8, encoding: DW_ATE_unsigned_char)
181 !15 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: null, size: 64)
182 !16 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !17, size: 64)
183 !17 = !DIBasicType(name: "unsigned short", size: 16, encoding: DW_ATE_unsigned)
184 !18 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !4, size: 64)
185 !19 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !20, size: 64)
186 !20 = !DIBasicType(name: "long long unsigned int", size: 64, encoding: DW_ATE_unsigned)
187 !21 = !DIBasicType(name: "long long int", size: 64, encoding: DW_ATE_signed)
188 !22 = !{i32 2, !"Dwarf Version", i32 4}
189 !23 = !{i32 2, !"Debug Info Version", i32 3}
190 !24 = !{i32 1, !"wchar_size", i32 4}
191 !25 = !{!"clang version 10.0.0 (https://github.com/llvm/llvm-project.git 923aa0ce806f7739b754167239fee2c9a15e2f31)"}
192 !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)
193 !27 = !DISubroutineType(types: !28)
195 !29 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
196 !30 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !31, size: 64)
197 !31 = distinct !DICompositeType(tag: DW_TAG_structure_type, name: "s", file: !1, line: 1, size: 64, elements: !32)
198 !32 = !{!33, !34, !35}
199 !33 = !DIDerivedType(tag: DW_TAG_member, name: "a", scope: !31, file: !1, line: 2, baseType: !29, size: 32)
200 !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)
201 !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)
202 !36 = !{!37, !38, !39, !40}
203 !37 = !DILocalVariable(name: "arg", arg: 1, scope: !26, file: !1, line: 14, type: !30)
204 !38 = !DILocalVariable(name: "ull", scope: !26, file: !1, line: 15, type: !20)
205 !39 = !DILocalVariable(name: "offset", scope: !26, file: !1, line: 16, type: !4)
206 !40 = !DILocalVariable(name: "size", scope: !26, file: !1, line: 17, type: !4)
207 !41 = !DILocation(line: 0, scope: !26)
208 !42 = !DILocation(line: 16, column: 56, scope: !26)
209 !43 = !DILocation(line: 16, column: 21, scope: !26)
210 !44 = !DILocation(line: 17, column: 19, scope: !26)
211 !45 = !DILocation(line: 18, column: 3, scope: !26)
212 !46 = !DILocation(line: 20, column: 30, scope: !47)
213 !47 = distinct !DILexicalBlock(scope: !26, file: !1, line: 18, column: 16)
214 !48 = !DILocation(line: 20, column: 42, scope: !47)
215 !49 = !DILocation(line: 20, column: 11, scope: !47)
216 !50 = !{!51, !51, i64 0}
217 !51 = !{!"omnipotent char", !52, i64 0}
218 !52 = !{!"Simple C/C++ TBAA"}
219 !53 = !DILocation(line: 20, column: 53, scope: !47)
220 !54 = !DILocation(line: 22, column: 31, scope: !47)
221 !55 = !DILocation(line: 22, column: 43, scope: !47)
222 !56 = !DILocation(line: 22, column: 12, scope: !47)
223 !57 = !DILocation(line: 22, column: 11, scope: !47)
224 !58 = !{!59, !59, i64 0}
225 !59 = !{!"short", !51, i64 0}
226 !60 = !DILocation(line: 22, column: 54, scope: !47)
227 !61 = !DILocation(line: 24, column: 29, scope: !47)
228 !62 = !DILocation(line: 24, column: 41, scope: !47)
229 !63 = !DILocation(line: 24, column: 12, scope: !47)
230 !64 = !DILocation(line: 24, column: 11, scope: !47)
231 !65 = !{!66, !66, i64 0}
232 !66 = !{!"int", !51, i64 0}
233 !67 = !DILocation(line: 24, column: 52, scope: !47)
234 !68 = !DILocation(line: 26, column: 35, scope: !47)
235 !69 = !DILocation(line: 26, column: 47, scope: !47)
236 !70 = !DILocation(line: 26, column: 12, scope: !47)
237 !71 = !DILocation(line: 26, column: 11, scope: !47)
238 !72 = !{!73, !73, i64 0}
239 !73 = !{!"long long", !51, i64 0}
240 !74 = !DILocation(line: 26, column: 58, scope: !47)
241 !75 = !DILocation(line: 28, column: 11, scope: !26)
242 !76 = !DILocation(line: 28, column: 7, scope: !26)
243 !77 = !DILocation(line: 29, column: 7, scope: !78)
244 !78 = distinct !DILexicalBlock(scope: !26, file: !1, line: 29, column: 7)
245 !79 = !DILocation(line: 29, column: 7, scope: !26)
246 !80 = !DILocation(line: 32, column: 1, scope: !26)