75 lines · plain
1; RUN: opt -passes=bpf-preserve-static-offset -mtriple=bpf-pc-linux -S -o - %s | FileCheck %s2;3; Check that bpf-preserve-static-offset folds chain of GEP instructions.4; The GEP chain in this example has unexpected shape and thus is5; folded as i8 access.6;7; Source (IR modified by hand):8; #define __ctx __attribute__((preserve_static_offset))9; 10; struct foo {11; char a[2];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((&p->b)[1].a[1]);23; }24;25; Compilation flag:26; clang -cc1 -O2 -triple bpf -S -emit-llvm -disable-llvm-passes -o - \27; | opt -passes=function(sroa) -S -o -28;29; Modified to remove 'inbounds' from one of the GEP instructions.30 31%struct.bar = type { i8, %struct.foo }32%struct.foo = type { [2 x i8] }33 34; Function Attrs: nounwind35define dso_local void @buz(ptr noundef %p) #0 {36entry:37 %0 = call ptr @llvm.preserve.static.offset(ptr %p)38 %b = getelementptr inbounds %struct.bar, ptr %0, i32 0, i32 139 %arrayidx = getelementptr inbounds %struct.foo, ptr %b, i64 140; ^^^^^41; folded as i8 access because of this index42 %a = getelementptr %struct.foo, ptr %arrayidx, i32 0, i32 043 %arrayidx1 = getelementptr inbounds [2 x i8], ptr %a, i64 0, i64 144 %1 = load i8, ptr %arrayidx1, align 1, !tbaa !245 call void @consume(i8 noundef signext %1)46 ret void47}48 49; CHECK: %[[v1:.*]] = call i8 (ptr, i1, i8, i8, i8, i1, ...)50; CHECK-SAME: @llvm.bpf.getelementptr.and.load.i851; CHECK-SAME: (ptr readonly elementtype(i8) %{{[^,]+}},52; CHECK-SAME: i1 false, i8 0, i8 1, i8 0, i1 false, i64 immarg 4)53; ^^^^^^^^ ^^^^^^^^^^^^54; not inbounds ---' |55; offset from 'struct bar' start -------------'56; CHECK-NEXT: call void @consume(i8 noundef signext %[[v1]])57 58declare void @consume(i8 noundef signext) #159 60; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)61declare ptr @llvm.preserve.static.offset(ptr readnone) #262 63attributes #0 = { nounwind "no-trapping-math"="true" "stack-protector-buffer-size"="8" }64attributes #1 = { "no-trapping-math"="true" "stack-protector-buffer-size"="8" }65attributes #2 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }66 67!llvm.module.flags = !{!0}68!llvm.ident = !{!1}69 70!0 = !{i32 1, !"wchar_size", i32 4}71!1 = !{!"clang"}72!2 = !{!3, !3, i64 0}73!3 = !{!"omnipotent char", !4, i64 0}74!4 = !{!"Simple C/C++ TBAA"}75