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 chain of GEP instructions. 4; The GEP chain in this example has unexpected shape and thus is 5; folded as i8 access. 6; 7; Source: 8; #define __ctx __attribute__((preserve_static_offset)) 9; 10; struct foo { 11; char aa; 12; char bb; 13; }; 14; 15; struct bar { 16; char a; 17; struct foo b; 18; } __ctx; 19; 20; extern void consume(char); 21; 22; void buz(struct bar *p) { 23; consume(((struct foo *)(((char*)&p->b) + 1))->bb); 24; } 25; 26; Compilation flag: 27; clang -cc1 -O2 -triple bpf -S -emit-llvm -disable-llvm-passes -o - \ 28; | opt -passes=function(sroa) -S -o - 29 30%struct.bar = type { i8, %struct.foo } 31%struct.foo = type { i8, i8 } 32 33; Function Attrs: nounwind 34define dso_local void @buz(ptr noundef %p) #0 { 35entry: 36 %0 = call ptr @llvm.preserve.static.offset(ptr %p) 37 %b = getelementptr inbounds %struct.bar, ptr %0, i32 0, i32 1 38 %add.ptr = getelementptr inbounds i8, ptr %b, i64 1 39; ~~ 40; these types do not match, thus GEP chain is folded as an offset 41; ~~~~~~~~~~~ 42 %bb = getelementptr inbounds %struct.foo, ptr %add.ptr, i32 0, i32 1 43 %1 = load i8, ptr %bb, align 1, !tbaa !2 44 call void @consume(i8 noundef signext %1) 45 ret void 46} 47 48; CHECK: %[[bb1:.*]] = call i8 (ptr, i1, i8, i8, i8, i1, ...) 49; CHECK-SAME: @llvm.bpf.getelementptr.and.load.i8 50; CHECK-SAME: (ptr readonly elementtype(i8) %{{[^,]+}}, 51; CHECK-SAME: i1 false, i8 0, i8 1, i8 0, i1 true, i64 immarg 3) 52; ^^^^^^^^^^^^ 53; offset from 'struct bar' start 54; CHECK-NEXT: call void @consume(i8 noundef signext %[[bb1]]) 55 56declare void @consume(i8 noundef signext) #1 57 58; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none) 59declare ptr @llvm.preserve.static.offset(ptr readnone) #2 60 61attributes #0 = { nounwind "no-trapping-math"="true" "stack-protector-buffer-size"="8" } 62attributes #1 = { "no-trapping-math"="true" "stack-protector-buffer-size"="8" } 63attributes #2 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) } 64 65!llvm.module.flags = !{!0} 66!llvm.ident = !{!1} 67 68!0 = !{i32 1, !"wchar_size", i32 4} 69!1 = !{!"clang"} 70!2 = !{!3, !4, i64 1} 71!3 = !{!"foo", !4, i64 0, !4, i64 1} 72!4 = !{!"omnipotent char", !5, i64 0} 73!5 = !{!"Simple C/C++ TBAA"} 74