xref: /llvm-project/llvm/test/CodeGen/BPF/preserve-static-offset/load-unroll-inline.ll (revision 29441e4f5fa5f5c7709f7cf180815ba97f611297)
1030b8cb1SEduard Zingerman; RUN: opt -O2 -mtriple=bpf-pc-linux -S -o - %s | FileCheck %s
2030b8cb1SEduard Zingerman;
3030b8cb1SEduard Zingerman; Check position of bpf-preserve-static-offset pass in the pipeline:
4030b8cb1SEduard Zingerman; - preserve.static.offset call is preserved if address is passed as
5030b8cb1SEduard Zingerman;   a parameter to an inline-able function;
6030b8cb1SEduard Zingerman; - second bpf-preserve-static-offset pass (after inlining) should introduce
7030b8cb1SEduard Zingerman;   getelementptr.and.load call using the preserved marker after loops
8030b8cb1SEduard Zingerman;   unrolling;
9030b8cb1SEduard Zingerman; - readonly and tbaa attributes should allow replacement of
10030b8cb1SEduard Zingerman;   getelementptr.and.load calls by CSE transformation.
11030b8cb1SEduard Zingerman;
12030b8cb1SEduard Zingerman; Source:
13030b8cb1SEduard Zingerman;    #define __ctx __attribute__((preserve_static_offset))
14030b8cb1SEduard Zingerman;
15030b8cb1SEduard Zingerman;    struct foo {
16030b8cb1SEduard Zingerman;      int a;
17030b8cb1SEduard Zingerman;      int b[4];
18030b8cb1SEduard Zingerman;    } __ctx;
19030b8cb1SEduard Zingerman;
20030b8cb1SEduard Zingerman;    extern void consume(int);
21030b8cb1SEduard Zingerman;
22030b8cb1SEduard Zingerman;    static inline void bar(int * restrict p) {
23030b8cb1SEduard Zingerman;        consume(p[1]);
24030b8cb1SEduard Zingerman;    }
25030b8cb1SEduard Zingerman;
26030b8cb1SEduard Zingerman;    void quux(struct foo *p){
27030b8cb1SEduard Zingerman;      unsigned long i = 0;
28030b8cb1SEduard Zingerman;    #pragma clang loop unroll(full)
29030b8cb1SEduard Zingerman;      while (i < 2) {
30030b8cb1SEduard Zingerman;        bar(p->b);
31030b8cb1SEduard Zingerman;        ++i;
32030b8cb1SEduard Zingerman;      }
33030b8cb1SEduard Zingerman;    }
34030b8cb1SEduard Zingerman;
35030b8cb1SEduard Zingerman; Compilation flag:
36030b8cb1SEduard Zingerman;   clang -cc1 -O2 -triple bpf -S -emit-llvm -disable-llvm-passes -o - \
37030b8cb1SEduard Zingerman;       | opt -passes=function(sroa) -S -o -
38030b8cb1SEduard Zingerman
39030b8cb1SEduard Zingerman%struct.foo = type { i32, [4 x i32] }
40030b8cb1SEduard Zingerman
41030b8cb1SEduard Zingerman; Function Attrs: nounwind
42030b8cb1SEduard Zingermandefine dso_local void @quux(ptr noundef %p) #0 {
43030b8cb1SEduard Zingermanentry:
44030b8cb1SEduard Zingerman  br label %while.cond
45030b8cb1SEduard Zingerman
46030b8cb1SEduard Zingermanwhile.cond:                                       ; preds = %while.body, %entry
47030b8cb1SEduard Zingerman  %i.0 = phi i64 [ 0, %entry ], [ %inc, %while.body ]
48030b8cb1SEduard Zingerman  %cmp = icmp ult i64 %i.0, 2
49030b8cb1SEduard Zingerman  br i1 %cmp, label %while.body, label %while.end
50030b8cb1SEduard Zingerman
51030b8cb1SEduard Zingermanwhile.body:                                       ; preds = %while.cond
52030b8cb1SEduard Zingerman  %0 = call ptr @llvm.preserve.static.offset(ptr %p)
53030b8cb1SEduard Zingerman  %b = getelementptr inbounds %struct.foo, ptr %0, i32 0, i32 1
54030b8cb1SEduard Zingerman  %arraydecay = getelementptr inbounds [4 x i32], ptr %b, i64 0, i64 0
55030b8cb1SEduard Zingerman  call void @bar(ptr noundef %arraydecay)
56030b8cb1SEduard Zingerman  %inc = add i64 %i.0, 1
57030b8cb1SEduard Zingerman  br label %while.cond, !llvm.loop !2
58030b8cb1SEduard Zingerman
59030b8cb1SEduard Zingermanwhile.end:                                        ; preds = %while.cond
60030b8cb1SEduard Zingerman  ret void
61030b8cb1SEduard Zingerman}
62030b8cb1SEduard Zingerman
63030b8cb1SEduard Zingerman; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
64030b8cb1SEduard Zingermandeclare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #1
65030b8cb1SEduard Zingerman
66030b8cb1SEduard Zingerman; Function Attrs: inlinehint nounwind
67030b8cb1SEduard Zingermandefine internal void @bar(ptr noalias noundef %p) #2 {
68030b8cb1SEduard Zingermanentry:
69030b8cb1SEduard Zingerman  %arrayidx = getelementptr inbounds i32, ptr %p, i64 1
70030b8cb1SEduard Zingerman  %0 = load i32, ptr %arrayidx, align 4, !tbaa !5
71030b8cb1SEduard Zingerman  call void @consume(i32 noundef %0)
72030b8cb1SEduard Zingerman  ret void
73030b8cb1SEduard Zingerman}
74030b8cb1SEduard Zingerman
75*29441e4fSNikita Popov; CHECK:      define dso_local void @quux(ptr noundef readonly captures(none) %[[p:.*]])
76030b8cb1SEduard Zingerman; CHECK:        %[[v1:.*]] = tail call i32 (ptr, i1, i8, i8, i8, i1, ...)
77030b8cb1SEduard Zingerman; CHECK-SAME:     @llvm.bpf.getelementptr.and.load.i32
7890ba3309SNikita Popov; CHECK-SAME:       (ptr readonly elementtype(i8) %[[p]],
7990ba3309SNikita Popov; CHECK-SAME:        i1 false, i8 0, i8 1, i8 2, i1 true, i64 immarg 8)
80030b8cb1SEduard Zingerman; CHECK:        tail call void @consume(i32 noundef %[[v1]])
81030b8cb1SEduard Zingerman; CHECK:        tail call void @consume(i32 noundef %[[v1]])
82030b8cb1SEduard Zingerman
83030b8cb1SEduard Zingerman; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
84030b8cb1SEduard Zingermandeclare ptr @llvm.preserve.static.offset(ptr readnone) #3
85030b8cb1SEduard Zingerman
86030b8cb1SEduard Zingerman; Function Attrs: nocallback nofree nosync nounwind willreturn memory(argmem: readwrite)
87030b8cb1SEduard Zingermandeclare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #1
88030b8cb1SEduard Zingerman
89030b8cb1SEduard Zingermandeclare void @consume(i32 noundef) #4
90030b8cb1SEduard Zingerman
91030b8cb1SEduard Zingermanattributes #0 = { nounwind "no-trapping-math"="true" "stack-protector-buffer-size"="8" }
92030b8cb1SEduard Zingermanattributes #1 = { nocallback nofree nosync nounwind willreturn memory(argmem: readwrite) }
93030b8cb1SEduard Zingermanattributes #2 = { inlinehint nounwind "no-trapping-math"="true" "stack-protector-buffer-size"="8" }
94030b8cb1SEduard Zingermanattributes #3 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }
95030b8cb1SEduard Zingermanattributes #4 = { "no-trapping-math"="true" "stack-protector-buffer-size"="8" }
96030b8cb1SEduard Zingerman
97030b8cb1SEduard Zingerman!llvm.module.flags = !{!0}
98030b8cb1SEduard Zingerman!llvm.ident = !{!1}
99030b8cb1SEduard Zingerman
100030b8cb1SEduard Zingerman!0 = !{i32 1, !"wchar_size", i32 4}
101030b8cb1SEduard Zingerman!1 = !{!"clang"}
102030b8cb1SEduard Zingerman!2 = distinct !{!2, !3, !4}
103030b8cb1SEduard Zingerman!3 = !{!"llvm.loop.mustprogress"}
104030b8cb1SEduard Zingerman!4 = !{!"llvm.loop.unroll.full"}
105030b8cb1SEduard Zingerman!5 = !{!6, !6, i64 0}
106030b8cb1SEduard Zingerman!6 = !{!"int", !7, i64 0}
107030b8cb1SEduard Zingerman!7 = !{!"omnipotent char", !8, i64 0}
108030b8cb1SEduard Zingerman!8 = !{!"Simple C/C++ TBAA"}
109