1; RUN: opt -O2 %s | llvm-dis > %t1
2; RUN: llc -filetype=asm -o - %t1 | FileCheck -check-prefixes=CHECK %s
3; RUN: llc -mattr=+alu32 -filetype=asm -o - %t1 | FileCheck -check-prefixes=CHECK %s
4; Source code:
5;   struct v1 {int a; int b;};
6;   typedef struct v1 __v1;
7;   typedef __v1 arr[4][4];
8;   struct v3 { char c; int d[100]; };
9;   #define _(x) (__builtin_preserve_access_index(x))
10;   #define cast_to_arr(x) ((arr *)(x))
11;   int get_value(const int *arg);
12;   int test(struct v3 *arg) {
13;     return get_value(_(&cast_to_arr(&arg->d[0])[0][2][3].b));
14;   }
15; Compilation flag:
16;   clang -target bpf -O2 -g -S -emit-llvm -Xclang -disable-llvm-passes test.c
17
18target triple = "bpf"
19
20%struct.v3 = type { i8, [100 x i32] }
21%struct.v1 = type { i32, i32 }
22
23; Function Attrs: nounwind
24define dso_local i32 @test(%struct.v3* %arg) local_unnamed_addr #0 !dbg !24 {
25entry:
26  call void @llvm.dbg.value(metadata %struct.v3* %arg, metadata !34, metadata !DIExpression()), !dbg !35
27  %0 = tail call [100 x i32]* @llvm.preserve.struct.access.index.p0a100i32.p0s_struct.v3s(%struct.v3* elementtype(%struct.v3) %arg, i32 1, i32 1), !dbg !36, !llvm.preserve.access.index !28
28  %1 = tail call i32* @llvm.preserve.array.access.index.p0i32.p0a100i32([100 x i32]* elementtype([100 x i32]) %0, i32 1, i32 0), !dbg !36, !llvm.preserve.access.index !15
29  %2 = bitcast i32* %1 to [4 x [4 x %struct.v1]]*, !dbg !36
30  %3 = tail call [4 x [4 x %struct.v1]]* @llvm.preserve.array.access.index.p0a4a4s_struct.v1s.p0a4a4s_struct.v1s([4 x [4 x %struct.v1]]* elementtype([4 x [4 x %struct.v1]]) %2, i32 0, i32 0), !dbg !36, !llvm.preserve.access.index !4
31  %4 = tail call [4 x %struct.v1]* @llvm.preserve.array.access.index.p0a4s_struct.v1s.p0a4a4s_struct.v1s([4 x [4 x %struct.v1]]* elementtype([4 x [4 x %struct.v1]]) %3, i32 1, i32 2), !dbg !36, !llvm.preserve.access.index !5
32  %5 = tail call %struct.v1* @llvm.preserve.array.access.index.p0s_struct.v1s.p0a4s_struct.v1s([4 x %struct.v1]* elementtype([4 x %struct.v1]) %4, i32 1, i32 3), !dbg !36, !llvm.preserve.access.index !18
33  %6 = tail call i32* @llvm.preserve.struct.access.index.p0i32.p0s_struct.v1s(%struct.v1* elementtype(%struct.v1) %5, i32 1, i32 1), !dbg !36, !llvm.preserve.access.index !8
34  %call = tail call i32 @get_value(i32* %6) #4, !dbg !37
35  ret i32 %call, !dbg !38
36}
37
38; CHECK:              r2 = 4
39; CHECK:              r1 += r2
40; CHECK:              r2 = 92
41; CHECK:              r1 += r2
42; CHECK:              call get_value
43
44; CHECK:              .long   1                       # BTF_KIND_STRUCT(id = [[TID1:[0-9]+]])
45; CHECK:              .long   100                     # BTF_KIND_STRUCT(id = [[TID2:[0-9]+]])
46
47; CHECK:              .ascii  "v3"                    # string offset=1
48; CHECK:              .ascii  ".text"                 # string offset=46
49; CHECK:              .ascii  "0:1:0"                 # string offset=52
50; CHECK:              .ascii  "v1"                    # string offset=100
51; CHECK:              .ascii  "11:1"                  # string offset=107
52
53; CHECK:              .long   16                      # FieldReloc
54; CHECK-NEXT:         .long   46                      # Field reloc section string offset=46
55; CHECK-NEXT:         .long   2
56; CHECK-NEXT:         .long   .Ltmp{{[0-9]+}}
57; CHECK-NEXT:         .long   [[TID1]]
58; CHECK-NEXT:         .long   52
59; CHECK-NEXT:         .long   0
60; CHECK-NEXT:         .long   .Ltmp{{[0-9]+}}
61; CHECK-NEXT:         .long   [[TID2]]
62; CHECK-NEXT:         .long   107
63; CHECK-NEXT:         .long   0
64
65declare dso_local i32 @get_value(i32*) local_unnamed_addr #1
66
67; Function Attrs: nounwind readnone
68declare [100 x i32]* @llvm.preserve.struct.access.index.p0a100i32.p0s_struct.v3s(%struct.v3*, i32, i32) #2
69
70; Function Attrs: nounwind readnone
71declare i32* @llvm.preserve.array.access.index.p0i32.p0a100i32([100 x i32]*, i32, i32) #2
72
73; Function Attrs: nounwind readnone
74declare [4 x [4 x %struct.v1]]* @llvm.preserve.array.access.index.p0a4a4s_struct.v1s.p0a4a4s_struct.v1s([4 x [4 x %struct.v1]]*, i32, i32) #2
75
76; Function Attrs: nounwind readnone
77declare [4 x %struct.v1]* @llvm.preserve.array.access.index.p0a4s_struct.v1s.p0a4a4s_struct.v1s([4 x [4 x %struct.v1]]*, i32, i32) #2
78
79; Function Attrs: nounwind readnone
80declare %struct.v1* @llvm.preserve.array.access.index.p0s_struct.v1s.p0a4s_struct.v1s([4 x %struct.v1]*, i32, i32) #2
81
82; Function Attrs: nounwind readnone
83declare i32* @llvm.preserve.struct.access.index.p0i32.p0s_struct.v1s(%struct.v1*, i32, i32) #2
84
85; Function Attrs: nounwind readnone speculatable willreturn
86declare void @llvm.dbg.value(metadata, metadata, metadata) #3
87
88attributes #0 = { nounwind "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" }
89attributes #1 = { "correctly-rounded-divide-sqrt-fp-math"="false" "disable-tail-calls"="false" "frame-pointer"="all" "less-precise-fpmad"="false" "no-infs-fp-math"="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" }
90attributes #2 = { nounwind readnone }
91attributes #3 = { nounwind readnone speculatable willreturn }
92attributes #4 = { nounwind }
93
94!llvm.dbg.cu = !{!0}
95!llvm.module.flags = !{!20, !21, !22}
96!llvm.ident = !{!23}
97
98!0 = distinct !DICompileUnit(language: DW_LANG_C99, file: !1, producer: "clang version 10.0.0 (trunk 367256) (llvm/trunk 367266)", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, enums: !2, retainedTypes: !3, nameTableKind: None)
99!1 = !DIFile(filename: "test.c", directory: "/tmp/home/yhs/work/tests/llvm/cast")
100!2 = !{}
101!3 = !{!4, !15, !5, !18}
102!4 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !5, size: 64)
103!5 = !DIDerivedType(tag: DW_TAG_typedef, name: "arr", file: !1, line: 3, baseType: !6)
104!6 = !DICompositeType(tag: DW_TAG_array_type, baseType: !7, size: 1024, elements: !13)
105!7 = !DIDerivedType(tag: DW_TAG_typedef, name: "__v1", file: !1, line: 2, baseType: !8)
106!8 = distinct !DICompositeType(tag: DW_TAG_structure_type, name: "v1", file: !1, line: 1, size: 64, elements: !9)
107!9 = !{!10, !12}
108!10 = !DIDerivedType(tag: DW_TAG_member, name: "a", scope: !8, file: !1, line: 1, baseType: !11, size: 32)
109!11 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
110!12 = !DIDerivedType(tag: DW_TAG_member, name: "b", scope: !8, file: !1, line: 1, baseType: !11, size: 32, offset: 32)
111!13 = !{!14, !14}
112!14 = !DISubrange(count: 4)
113!15 = !DICompositeType(tag: DW_TAG_array_type, baseType: !11, size: 3200, elements: !16)
114!16 = !{!17}
115!17 = !DISubrange(count: 100)
116!18 = !DICompositeType(tag: DW_TAG_array_type, baseType: !7, size: 256, elements: !19)
117!19 = !{!14}
118!20 = !{i32 2, !"Dwarf Version", i32 4}
119!21 = !{i32 2, !"Debug Info Version", i32 3}
120!22 = !{i32 1, !"wchar_size", i32 4}
121!23 = !{!"clang version 10.0.0 (trunk 367256) (llvm/trunk 367266)"}
122!24 = distinct !DISubprogram(name: "test", scope: !1, file: !1, line: 8, type: !25, scopeLine: 8, flags: DIFlagPrototyped, isDefinition: true, isOptimized: true, unit: !0, retainedNodes: !33)
123!25 = !DISubroutineType(types: !26)
124!26 = !{!11, !27}
125!27 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !28, size: 64)
126!28 = distinct !DICompositeType(tag: DW_TAG_structure_type, name: "v3", file: !1, line: 4, size: 3232, elements: !29)
127!29 = !{!30, !32}
128!30 = !DIDerivedType(tag: DW_TAG_member, name: "c", scope: !28, file: !1, line: 4, baseType: !31, size: 8)
129!31 = !DIBasicType(name: "char", size: 8, encoding: DW_ATE_signed_char)
130!32 = !DIDerivedType(tag: DW_TAG_member, name: "d", scope: !28, file: !1, line: 4, baseType: !15, size: 3200, offset: 32)
131!33 = !{!34}
132!34 = !DILocalVariable(name: "arg", arg: 1, scope: !24, file: !1, line: 8, type: !27)
133!35 = !DILocation(line: 0, scope: !24)
134!36 = !DILocation(line: 9, column: 20, scope: !24)
135!37 = !DILocation(line: 9, column: 10, scope: !24)
136!38 = !DILocation(line: 9, column: 3, scope: !24)
137