1 ; RUN: opt -O2 -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 type mismatch and thus is
8 ; #define __ctx __attribute__((preserve_static_offset))
20 ; void buz(struct bar *p) {
21 ; ((struct foo *)(((char*)&p->b) + 1))->bb = 42;
25 ; clang -cc1 -O2 -triple bpf -S -emit-llvm -disable-llvm-passes -o - \
26 ; | opt -passes=function(sroa) -S -o -
28 %struct.bar = type { i8, %struct.foo }
29 %struct.foo = type { i8, i8 }
31 ; Function Attrs: nounwind
32 define dso_local void @buz(ptr noundef %p) #0 {
34 %0 = call ptr @llvm.preserve.static.offset(ptr %p)
35 %b = getelementptr inbounds %struct.bar, ptr %0, i32 0, i32 1
36 %add.ptr = getelementptr inbounds i8, ptr %b, i64 1
38 ; these types do not match, thus GEP chain is folded as an offset
40 %bb = getelementptr inbounds %struct.foo, ptr %add.ptr, i32 0, i32 1
41 store i8 42, ptr %bb, align 1, !tbaa !2
45 ; CHECK: define dso_local void @buz(ptr nocapture noundef writeonly %[[p:.*]])
46 ; CHECK: tail call void (i8, ptr, i1, i8, i8, i8, i1, ...)
47 ; CHECK-SAME: @llvm.bpf.getelementptr.and.store.i8
49 ; CHECK-SAME: ptr writeonly elementtype(i8) %[[p]],
50 ; CHECK-SAME: i1 false, i8 0, i8 1, i8 0, i1 true, i64 immarg 3)
51 ; CHECK-SAME: #[[v2:.*]], !tbaa ![[v3:.*]]
52 ; CHECK: attributes #[[v2]] = { memory(argmem: write) }
54 ; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
55 declare ptr @llvm.preserve.static.offset(ptr readnone) #1
57 attributes #0 = { nounwind "no-trapping-math"="true" "stack-protector-buffer-size"="8" }
58 attributes #1 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
60 !llvm.module.flags = !{!0}
63 !0 = !{i32 1, !"wchar_size", i32 4}
66 !3 = !{!"foo", !4, i64 0, !4, i64 1}
67 !4 = !{!"omnipotent char", !5, i64 0}
68 !5 = !{!"Simple C/C++ TBAA"}