xref: /llvm-project/llvm/test/CodeGen/BPF/simplifycfg.ll (revision a9fe30a1f1808f8f68ffbaecf9c11c8419491851)
1; RUN: opt -O2 -S < %s | FileCheck %s
2;
3; This test tries to ensure that simplifycfg hoisting common instructions
4; of then/else branch indeed happens. BPF target has added an IR pass
5; before loop optimizations as Commit 1d51dc38d89b
6; ([SimplifyCFG][LoopRotate] SimplifyCFG: disable common instruction
7;  hoisting by default, enable late in pipeline)
8; disabled common instruction hoisting. Due to optimization triggered
9; code changes, later SimplifyCFG may not be able to perform optimization
10; even common inst hoisting is enabled.
11;
12; Source:
13;   typedef struct {
14;     void *f_back;
15;   } FrameData;
16;   extern int get_data(void *, void *);
17;   extern void get_frame_ptr(void *);
18;   int test() {
19;     void *frame_ptr;
20;     FrameData frame;
21;
22;     get_frame_ptr(&frame_ptr);
23;
24;     #pragma nounroll
25;     for (int i = 0; i < 6; i++) {
26;       if (frame_ptr && get_data(frame_ptr, &frame)) {
27;         frame_ptr = frame.f_back;
28;       }
29;     }
30;     return frame_ptr == 0;
31;   }
32; Compilation flag:
33;   clang -target bpf -O2 -Xclang -disable-llvm-passes -S -emit-llvm t.c -o t.ll
34
35target datalayout = "e-m:e-p:64:64-i64:64-i128:128-n32:64-S128"
36target triple = "bpf"
37
38%struct.FrameData = type { ptr }
39
40; Function Attrs: nounwind
41define dso_local i32 @test() #0 {
42entry:
43  %frame_ptr = alloca ptr, align 8
44  %frame = alloca %struct.FrameData, align 8
45  %i = alloca i32, align 4
46  call void @llvm.lifetime.start.p0(i64 8, ptr %frame_ptr) #3
47  call void @llvm.lifetime.start.p0(i64 8, ptr %frame) #3
48  call void @get_frame_ptr(ptr %frame_ptr)
49  call void @llvm.lifetime.start.p0(i64 4, ptr %i) #3
50  store i32 0, ptr %i, align 4, !tbaa !2
51  br label %for.cond
52
53; CHECK-LABEL:    entry
54; CHECK:          %[[PTR:[0-9]+]] = load ptr, ptr %frame_ptr, align 8
55; CHECK:          %{{[0-9a-z.]+}} = icmp eq ptr %[[PTR]], null
56; CHECK:          br label
57
58for.cond:                                         ; preds = %for.inc, %entry
59  %0 = load i32, ptr %i, align 4, !tbaa !2
60  %cmp = icmp slt i32 %0, 6
61  br i1 %cmp, label %for.body, label %for.cond.cleanup
62
63for.cond.cleanup:                                 ; preds = %for.cond
64  call void @llvm.lifetime.end.p0(i64 4, ptr %i) #3
65  br label %for.end
66
67for.body:                                         ; preds = %for.cond
68  %1 = load ptr, ptr %frame_ptr, align 8, !tbaa !6
69  %tobool = icmp ne ptr %1, null
70  br i1 %tobool, label %land.lhs.true, label %if.end
71
72land.lhs.true:                                    ; preds = %for.body
73  %2 = load ptr, ptr %frame_ptr, align 8, !tbaa !6
74  %call = call i32 @get_data(ptr %2, ptr %frame)
75  %tobool1 = icmp ne i32 %call, 0
76  br i1 %tobool1, label %if.then, label %if.end
77
78if.then:                                          ; preds = %land.lhs.true
79  %3 = load ptr, ptr %frame, align 8, !tbaa !8
80  store ptr %3, ptr %frame_ptr, align 8, !tbaa !6
81  br label %if.end
82
83if.end:                                           ; preds = %if.then, %land.lhs.true, %for.body
84  br label %for.inc
85
86for.inc:                                          ; preds = %if.end
87  %4 = load i32, ptr %i, align 4, !tbaa !2
88  %inc = add nsw i32 %4, 1
89  store i32 %inc, ptr %i, align 4, !tbaa !2
90  br label %for.cond, !llvm.loop !10
91
92for.end:                                          ; preds = %for.cond.cleanup
93  %5 = load ptr, ptr %frame_ptr, align 8, !tbaa !6
94  %cmp2 = icmp eq ptr %5, null
95  %conv = zext i1 %cmp2 to i32
96  call void @llvm.lifetime.end.p0(i64 8, ptr %frame) #3
97  call void @llvm.lifetime.end.p0(i64 8, ptr %frame_ptr) #3
98  ret i32 %conv
99}
100
101; Function Attrs: argmemonly nounwind willreturn
102declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #1
103
104declare dso_local void @get_frame_ptr(ptr) #2
105
106declare dso_local i32 @get_data(ptr, ptr) #2
107
108; Function Attrs: argmemonly nounwind willreturn
109declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #1
110
111attributes #0 = { nounwind "correctly-rounded-divide-sqrt-fp-math"="false" "disable-tail-calls"="false" "frame-pointer"="all" "less-precise-fpmad"="false" "min-legal-vector-width"="0" "no-infs-fp-math"="false" "no-jump-tables"="false" "no-nans-fp-math"="false" "no-signed-zeros-fp-math"="false" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "unsafe-fp-math"="false" "use-soft-float"="false" }
112attributes #1 = { argmemonly nounwind willreturn }
113attributes #2 = { "correctly-rounded-divide-sqrt-fp-math"="false" "disable-tail-calls"="false" "frame-pointer"="all" "less-precise-fpmad"="false" "no-infs-fp-math"="false" "no-nans-fp-math"="false" "no-signed-zeros-fp-math"="false" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "unsafe-fp-math"="false" "use-soft-float"="false" }
114attributes #3 = { nounwind }
115
116!llvm.module.flags = !{!0}
117!llvm.ident = !{!1}
118
119!0 = !{i32 1, !"wchar_size", i32 4}
120!1 = !{!"clang version 12.0.0 (https://github.com/llvm/llvm-project.git 1b3c1c543269da36ae41ab84f646cf98d2e5b1e5)"}
121!2 = !{!3, !3, i64 0}
122!3 = !{!"int", !4, i64 0}
123!4 = !{!"omnipotent char", !5, i64 0}
124!5 = !{!"Simple C/C++ TBAA"}
125!6 = !{!7, !7, i64 0}
126!7 = !{!"any pointer", !4, i64 0}
127!8 = !{!9, !7, i64 0}
128!9 = !{!"", !7, i64 0}
129!10 = distinct !{!10, !11}
130!11 = !{!"llvm.loop.unroll.disable"}
131