xref: /llvm-project/llvm/test/CodeGen/BPF/preserve-static-offset/load-chain-2.ll (revision 030b8cb1561db4161b108b59044717d89026cf70)
1; RUN: opt -passes=bpf-preserve-static-offset -mtriple=bpf-pc-linux -S -o - %s | FileCheck %s
2;
3; Check that bpf-preserve-static-offset folds GEP chains that end by
4; getelementptr.and.load.
5;
6; Source (modified by hand):
7;    #define __ctx __attribute__((preserve_static_offset))
8;
9;    struct bar {
10;      int aa;
11;      int bb;
12;    };
13;
14;    struct foo {
15;      int a;
16;      struct bar b;
17;    } __ctx;
18;
19;    extern void consume(int);
20;
21;    void bar(struct foo *p) {
22;      consume(p->b.bb);
23;    }
24;
25; Compilation flag:
26;   clang -cc1 -O2 -triple bpf -S -emit-llvm -disable-llvm-passes -o - \
27;       | opt -passes=function(sroa) -S -o -
28;
29; And modified to fold last getelementptr/load as a single
30; getelementptr.and.load.
31
32%struct.foo = type { i32, %struct.bar }
33%struct.bar = type { i32, i32 }
34
35; Function Attrs: nounwind
36define dso_local void @bar(ptr noundef %p) #0 {
37entry:
38  %0 = call ptr @llvm.preserve.static.offset(ptr %p)
39  %b = getelementptr inbounds %struct.foo, ptr %0, i32 0, i32 1
40  %bb1 = call i32 (ptr, i1, i8, i8, i8, i1, ...)
41           @llvm.bpf.getelementptr.and.load.i32
42             (ptr readonly elementtype(%struct.bar) %b,
43              i1 false, i8 0, i8 1, i8 2, i1 true, i32 immarg 0, i32 immarg 1)
44           #4, !tbaa !2
45  call void @consume(i32 noundef %bb1)
46  ret void
47}
48
49; CHECK:      define dso_local void @bar(ptr noundef %[[p:.*]])
50; CHECK:        %[[bb1:.*]] = call i32 (ptr, i1, i8, i8, i8, i1, ...)
51; CHECK-SAME:     @llvm.bpf.getelementptr.and.load.i32
52; CHECK-SAME:       (ptr readonly elementtype(%struct.foo) %[[p]],
53; CHECK-SAME:        i1 false, i8 0, i8 1, i8 2, i1 true, i32 immarg 0, i32 immarg 1, i32 immarg 1)
54; CHECK-SAME:      #[[v2:.*]], !tbaa
55; CHECK-NEXT:   call void @consume(i32 noundef %[[bb1]])
56; CHECK:      attributes #[[v2]] = { memory(argmem: read) }
57
58declare void @consume(i32 noundef) #1
59
60; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
61declare ptr @llvm.preserve.static.offset(ptr readnone) #2
62
63; Function Attrs: nocallback nofree nounwind willreturn
64declare i32 @llvm.bpf.getelementptr.and.load.i32(ptr nocapture, i1 immarg, i8 immarg, i8 immarg, i8 immarg, i1 immarg, ...) #3
65
66attributes #0 = { nounwind "no-trapping-math"="true" "stack-protector-buffer-size"="8" }
67attributes #1 = { "no-trapping-math"="true" "stack-protector-buffer-size"="8" }
68attributes #2 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
69attributes #3 = { nocallback nofree nounwind willreturn }
70attributes #4 = { memory(argmem: read) }
71
72!llvm.module.flags = !{!0}
73!llvm.ident = !{!1}
74
75!0 = !{i32 1, !"wchar_size", i32 4}
76!1 = !{!"clang"}
77!2 = !{!3, !4, i64 8}
78!3 = !{!"foo", !4, i64 0, !7, i64 4}
79!4 = !{!"int", !5, i64 0}
80!5 = !{!"omnipotent char", !6, i64 0}
81!6 = !{!"Simple C/C++ TBAA"}
82!7 = !{!"bar", !4, i64 0, !4, i64 4}
83