1 ; RUN: opt -passes=bpf-preserve-static-offset -mtriple=bpf-pc-linux -S -o - %s | FileCheck %s
3 ; Check that bpf-preserve-static-offset folds chain of GEP instructions.
4 ; The GEP chain in this example has unexpected shape and thus is
8 ; #define __ctx __attribute__((preserve_static_offset))
19 ; extern void consume(char);
21 ; void buz(struct bar *p) {
22 ; consume((&p->b)[1].a[1]);
26 ; clang -cc1 -O2 -triple bpf -S -emit-llvm -disable-llvm-passes -o - \
27 ; | opt -passes=function(sroa) -S -o -
29 %struct.bar = type { i8, %struct.foo }
30 %struct.foo = type { [2 x i8] }
32 ; Function Attrs: nounwind
33 define dso_local void @buz(ptr noundef %p) #0 {
35 %0 = call ptr @llvm.preserve.static.offset(ptr %p)
36 %b = getelementptr inbounds %struct.bar, ptr %0, i32 0, i32 1
37 %arrayidx = getelementptr inbounds %struct.foo, ptr %b, i64 1
39 ; folded as i8 access because of this index
40 %a = getelementptr inbounds %struct.foo, ptr %arrayidx, i32 0, i32 0
41 %arrayidx1 = getelementptr inbounds [2 x i8], ptr %a, i64 0, i64 1
42 %1 = load i8, ptr %arrayidx1, align 1, !tbaa !2
43 call void @consume(i8 noundef signext %1)
47 ; CHECK: %[[v1:.*]] = call i8 (ptr, i1, i8, i8, i8, i1, ...)
48 ; CHECK-SAME: @llvm.bpf.getelementptr.and.load.i8
49 ; CHECK-SAME: (ptr readonly elementtype(i8) %{{[^,]+}},
50 ; CHECK-SAME: i1 false, i8 0, i8 1, i8 0, i1 true, i64 immarg 4)
52 ; offset from 'struct bar' start
53 ; CHECK-NEXT: call void @consume(i8 noundef signext %[[v1]])
55 declare void @consume(i8 noundef signext) #1
57 ; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
58 declare ptr @llvm.preserve.static.offset(ptr readnone) #2
60 attributes #0 = { nounwind "no-trapping-math"="true" "stack-protector-buffer-size"="8" }
61 attributes #1 = { "no-trapping-math"="true" "stack-protector-buffer-size"="8" }
62 attributes #2 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
64 !llvm.module.flags = !{!0}
67 !0 = !{i32 1, !"wchar_size", i32 4}
70 !3 = !{!"omnipotent char", !4, i64 0}
71 !4 = !{!"Simple C/C++ TBAA"}