1; RUN: opt -O2 -mtriple=bpf-pc-linux -S -o - %s | FileCheck %s 2; 3; Check position of bpf-preserve-static-offset pass in the pipeline: 4; - preserve.static.offset call is preserved if address is passed as 5; a parameter to an inline-able function; 6; - second bpf-preserve-static-offset pass (after inlining) should introduce 7; getelementptr.and.store call using the preserved marker after loops 8; unrolling; 9; - memory(argmem: readwrite) and tbaa attributes should allow 10; removing one getelementptr.and.store call. 11; 12; Source: 13; #define __ctx __attribute__((preserve_static_offset)) 14; 15; struct foo { 16; int a; 17; int b[4]; 18; } __ctx; 19; 20; static inline void bar(int * restrict p, unsigned long i) { 21; p[0] = i; 22; } 23; 24; void quux(struct foo *p){ 25; unsigned long i = 0; 26; #pragma clang loop unroll(full) 27; while (i < 2) { 28; bar(p->b, i); 29; ++i; 30; } 31; } 32; 33; Compilation flag: 34; clang -cc1 -O2 -triple bpf -S -emit-llvm -disable-llvm-passes -o - \ 35; | opt -passes=function(sroa) -S -o - 36 37%struct.foo = type { i32, [4 x i32] } 38 39; Function Attrs: nounwind 40define dso_local void @quux(ptr noundef %p) #0 { 41entry: 42 br label %while.cond 43 44while.cond: ; preds = %while.body, %entry 45 %i.0 = phi i64 [ 0, %entry ], [ %inc, %while.body ] 46 %cmp = icmp ult i64 %i.0, 2 47 br i1 %cmp, label %while.body, label %while.end 48 49while.body: ; preds = %while.cond 50 %0 = call ptr @llvm.preserve.static.offset(ptr %p) 51 %b = getelementptr inbounds %struct.foo, ptr %0, i32 0, i32 1 52 %arraydecay = getelementptr inbounds [4 x i32], ptr %b, i64 0, i64 0 53 call void @bar(ptr noundef %arraydecay, i64 noundef %i.0) 54 %inc = add i64 %i.0, 1 55 br label %while.cond, !llvm.loop !2 56 57while.end: ; preds = %while.cond 58 ret void 59} 60 61; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) 62declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #1 63 64; Function Attrs: inlinehint nounwind 65define internal void @bar(ptr noalias noundef %p, i64 noundef %i) #2 { 66entry: 67 %conv = trunc i64 %i to i32 68 %arrayidx = getelementptr inbounds i32, ptr %p, i64 0 69 store i32 %conv, ptr %arrayidx, align 4, !tbaa !5 70 ret void 71} 72 73; CHECK: define dso_local void @quux(ptr noundef writeonly captures(none) %[[p:.*]]) 74; CHECK-NEXT: entry: 75; CHECK-NEXT: tail call void (i32, ptr, i1, i8, i8, i8, i1, ...) 76; CHECK-SAME: @llvm.bpf.getelementptr.and.store.i32 77; CHECK-SAME: (i32 1, 78; CHECK-SAME: ptr writeonly elementtype(i8) %[[p]], 79; CHECK-SAME: i1 false, i8 0, i8 1, i8 2, i1 true, i64 immarg 4) 80; CHECK-NEXT: ret void 81 82; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none) 83declare ptr @llvm.preserve.static.offset(ptr readnone) #3 84 85; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) 86declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #1 87 88attributes #0 = { nounwind "no-trapping-math"="true" "stack-protector-buffer-size"="8" } 89attributes #1 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) } 90attributes #2 = { inlinehint nounwind "no-trapping-math"="true" "stack-protector-buffer-size"="8" } 91attributes #3 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) } 92 93!llvm.module.flags = !{!0} 94!llvm.ident = !{!1} 95 96!0 = !{i32 1, !"wchar_size", i32 4} 97!1 = !{!"clang"} 98!2 = distinct !{!2, !3, !4} 99!3 = !{!"llvm.loop.mustprogress"} 100!4 = !{!"llvm.loop.unroll.full"} 101!5 = !{!6, !6, i64 0} 102!6 = !{!"int", !7, i64 0} 103!7 = !{!"omnipotent char", !8, i64 0} 104!8 = !{!"Simple C/C++ TBAA"} 105