xref: /llvm-project/llvm/test/CodeGen/BPF/preserve-static-offset/load-undo-chain-u8.ll (revision 030b8cb1561db4161b108b59044717d89026cf70)
1; RUN: opt --bpf-check-and-opt-ir -mtriple=bpf-pc-linux -S -o - %s | FileCheck %s
2;
3; Check unroll of getelementptr.and.load when direct memory offset is
4; used instead of field indexes.
5;
6; Source:
7;    #define __ctx __attribute__((preserve_static_offset))
8;
9;    struct foo {
10;      char aa;
11;      char bb;
12;    };
13;
14;    struct bar {
15;      char a;
16;      struct foo b;
17;    } __ctx;
18;
19;    extern void consume(char);
20;
21;    void buz(struct bar *p) {
22;      consume(((struct foo *)(((char*)&p->b) + 1))->bb);
23;    }
24;
25; Compilation flag:
26;   clang -cc1 -O2 -triple bpf -S -emit-llvm -disable-llvm-passes -o - \
27;       | opt -passes=sroa,bpf-preserve-static-offset -S -o -
28
29; Function Attrs: nounwind
30define dso_local void @buz(ptr noundef %p) #0 {
31entry:
32  %bb1 = call i8 (ptr, i1, i8, i8, i8, i1, ...)
33    @llvm.bpf.getelementptr.and.load.i8
34      (ptr readonly elementtype(i8) %p,
35       i1 false, i8 0, i8 1, i8 0, i1 true, i64 immarg 3)
36    #4, !tbaa !2
37  call void @consume(i8 noundef signext %bb1)
38  ret void
39}
40
41; CHECK: define dso_local void @buz(ptr noundef %[[p:.*]])
42; CHECK:   %[[bb11:.*]] = getelementptr inbounds i8, ptr %[[p]], i64 3
43; CHECK:   %[[v2:.*]] = load i8, ptr %[[bb11]], align 1
44; CHECK:   call void @consume(i8 noundef signext %[[v2]])
45
46declare void @consume(i8 noundef signext) #1
47
48; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
49declare ptr @llvm.preserve.static.offset(ptr readnone) #2
50
51; Function Attrs: nocallback nofree nounwind willreturn
52declare i8 @llvm.bpf.getelementptr.and.load.i8(ptr nocapture, i1 immarg, i8 immarg, i8 immarg, i8 immarg, i1 immarg, ...) #3
53
54attributes #0 = { nounwind "no-trapping-math"="true" "stack-protector-buffer-size"="8" }
55attributes #1 = { "no-trapping-math"="true" "stack-protector-buffer-size"="8" }
56attributes #2 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
57attributes #3 = { nocallback nofree nounwind willreturn }
58attributes #4 = { memory(argmem: read) }
59
60!llvm.module.flags = !{!0}
61!llvm.ident = !{!1}
62
63!0 = !{i32 1, !"wchar_size", i32 4}
64!1 = !{!"clang"}
65!2 = !{!3, !4, i64 1}
66!3 = !{!"foo", !4, i64 0, !4, i64 1}
67!4 = !{!"omnipotent char", !5, i64 0}
68!5 = !{!"Simple C/C++ TBAA"}
69