xref: /llvm-project/llvm/test/Transforms/GuardWidening/basic-loop.ll (revision 0b5bb6923f09ebc257ebed4ace1d8b6b113b2bf5)
1137995d8SPhilip Reames; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
28cf5b69fSNikita Popov; RUN: opt -S -passes="loop-mssa(guard-widening)" -verify-memoryssa < %s | FileCheck %s
3137995d8SPhilip Reames
4137995d8SPhilip Reamesdeclare void @llvm.experimental.guard(i1,...)
5137995d8SPhilip Reames
6137995d8SPhilip Reames@G = external global i32
7137995d8SPhilip Reames
8137995d8SPhilip Reames; Show that we can widen into early checks within a loop, and in the process
9137995d8SPhilip Reames; expose optimization oppurtunities.
10137995d8SPhilip Reamesdefine void @widen_within_loop(i1 %cond_0, i1 %cond_1, i1 %cond_2) {
11137995d8SPhilip Reames; CHECK-LABEL: @widen_within_loop(
12137995d8SPhilip Reames; CHECK-NEXT:  entry:
13*0b5bb692SSerguei Katkov; CHECK-NEXT:    [[COND_2_GW_FR:%.*]] = freeze i1 [[COND_2:%.*]]
14*0b5bb692SSerguei Katkov; CHECK-NEXT:    [[COND_1_GW_FR:%.*]] = freeze i1 [[COND_1:%.*]]
15137995d8SPhilip Reames; CHECK-NEXT:    br label [[LOOP:%.*]]
16137995d8SPhilip Reames; CHECK:       loop:
174b53f866SMatt Arsenault; CHECK-NEXT:    store i32 0, ptr @G, align 4
18*0b5bb692SSerguei Katkov; CHECK-NEXT:    [[WIDE_CHK:%.*]] = and i1 [[COND_0:%.*]], [[COND_1_GW_FR]]
19*0b5bb692SSerguei Katkov; CHECK-NEXT:    [[WIDE_CHK1:%.*]] = and i1 [[WIDE_CHK]], [[COND_2_GW_FR]]
20137995d8SPhilip Reames; CHECK-NEXT:    call void (i1, ...) @llvm.experimental.guard(i1 [[WIDE_CHK1]]) [ "deopt"(i32 0) ]
214b53f866SMatt Arsenault; CHECK-NEXT:    store i32 1, ptr @G, align 4
224b53f866SMatt Arsenault; CHECK-NEXT:    store i32 2, ptr @G, align 4
234b53f866SMatt Arsenault; CHECK-NEXT:    store i32 3, ptr @G, align 4
24137995d8SPhilip Reames; CHECK-NEXT:    br label [[LOOP]]
25137995d8SPhilip Reames;
26137995d8SPhilip Reamesentry:
27137995d8SPhilip Reames  br label %loop
28137995d8SPhilip Reames
29137995d8SPhilip Reamesloop:
304b53f866SMatt Arsenault  store i32 0, ptr @G
31137995d8SPhilip Reames  call void(i1, ...) @llvm.experimental.guard(i1 %cond_0) [ "deopt"(i32 0) ]
324b53f866SMatt Arsenault  store i32 1, ptr @G
33137995d8SPhilip Reames  call void(i1, ...) @llvm.experimental.guard(i1 %cond_1) [ "deopt"(i32 1) ]
344b53f866SMatt Arsenault  store i32 2, ptr @G
35137995d8SPhilip Reames  call void(i1, ...) @llvm.experimental.guard(i1 %cond_2) [ "deopt"(i32 2) ]
364b53f866SMatt Arsenault  store i32 3, ptr @G
37137995d8SPhilip Reames  br label %loop
38137995d8SPhilip Reames}
39137995d8SPhilip Reames
40137995d8SPhilip Reamesdefine void @widen_into_preheader(i1 %cond_0, i1 %cond_1, i1 %cond_2) {
41137995d8SPhilip Reames; CHECK-LABEL: @widen_into_preheader(
42137995d8SPhilip Reames; CHECK-NEXT:  entry:
43*0b5bb692SSerguei Katkov; CHECK-NEXT:    [[COND_2_GW_FR:%.*]] = freeze i1 [[COND_2:%.*]]
44*0b5bb692SSerguei Katkov; CHECK-NEXT:    [[COND_1_GW_FR:%.*]] = freeze i1 [[COND_1:%.*]]
454b53f866SMatt Arsenault; CHECK-NEXT:    store i32 0, ptr @G, align 4
46*0b5bb692SSerguei Katkov; CHECK-NEXT:    [[WIDE_CHK:%.*]] = and i1 [[COND_0:%.*]], [[COND_1_GW_FR]]
47*0b5bb692SSerguei Katkov; CHECK-NEXT:    [[WIDE_CHK1:%.*]] = and i1 [[WIDE_CHK]], [[COND_2_GW_FR]]
48137995d8SPhilip Reames; CHECK-NEXT:    call void (i1, ...) @llvm.experimental.guard(i1 [[WIDE_CHK1]]) [ "deopt"(i32 0) ]
49137995d8SPhilip Reames; CHECK-NEXT:    br label [[LOOP:%.*]]
50137995d8SPhilip Reames; CHECK:       loop:
514b53f866SMatt Arsenault; CHECK-NEXT:    store i32 1, ptr @G, align 4
524b53f866SMatt Arsenault; CHECK-NEXT:    store i32 2, ptr @G, align 4
534b53f866SMatt Arsenault; CHECK-NEXT:    store i32 3, ptr @G, align 4
54137995d8SPhilip Reames; CHECK-NEXT:    br label [[LOOP]]
55137995d8SPhilip Reames;
56137995d8SPhilip Reamesentry:
574b53f866SMatt Arsenault  store i32 0, ptr @G
58137995d8SPhilip Reames  call void(i1, ...) @llvm.experimental.guard(i1 %cond_0) [ "deopt"(i32 0) ]
59137995d8SPhilip Reames  br label %loop
60137995d8SPhilip Reames
61137995d8SPhilip Reamesloop:
624b53f866SMatt Arsenault  store i32 1, ptr @G
63137995d8SPhilip Reames  call void(i1, ...) @llvm.experimental.guard(i1 %cond_1) [ "deopt"(i32 1) ]
644b53f866SMatt Arsenault  store i32 2, ptr @G
65137995d8SPhilip Reames  call void(i1, ...) @llvm.experimental.guard(i1 %cond_2) [ "deopt"(i32 2) ]
664b53f866SMatt Arsenault  store i32 3, ptr @G
67137995d8SPhilip Reames  br label %loop
68137995d8SPhilip Reames}
69137995d8SPhilip Reames
70137995d8SPhilip Reamesdefine void @dont_widen_over_common_exit(i1 %cond_0, i1 %cond_1, i1 %cond_2) {
71137995d8SPhilip Reames; CHECK-LABEL: @dont_widen_over_common_exit(
72137995d8SPhilip Reames; CHECK-NEXT:  entry:
73137995d8SPhilip Reames; CHECK-NEXT:    br label [[LOOP:%.*]]
74137995d8SPhilip Reames; CHECK:       loop:
754b53f866SMatt Arsenault; CHECK-NEXT:    store i32 0, ptr @G, align 4
76137995d8SPhilip Reames; CHECK-NEXT:    call void (i1, ...) @llvm.experimental.guard(i1 [[COND_0:%.*]]) [ "deopt"(i32 0) ]
774b53f866SMatt Arsenault; CHECK-NEXT:    store i32 1, ptr @G, align 4
78137995d8SPhilip Reames; CHECK-NEXT:    br i1 [[COND_1:%.*]], label [[BACKEDGE:%.*]], label [[EXIT:%.*]]
79137995d8SPhilip Reames; CHECK:       backedge:
804b53f866SMatt Arsenault; CHECK-NEXT:    store i32 2, ptr @G, align 4
81137995d8SPhilip Reames; CHECK-NEXT:    call void (i1, ...) @llvm.experimental.guard(i1 [[COND_2:%.*]]) [ "deopt"(i32 2) ]
824b53f866SMatt Arsenault; CHECK-NEXT:    store i32 3, ptr @G, align 4
83137995d8SPhilip Reames; CHECK-NEXT:    br label [[LOOP]]
84137995d8SPhilip Reames; CHECK:       exit:
85137995d8SPhilip Reames; CHECK-NEXT:    ret void
86137995d8SPhilip Reames;
87137995d8SPhilip Reamesentry:
88137995d8SPhilip Reames  br label %loop
89137995d8SPhilip Reames
90137995d8SPhilip Reamesloop:
914b53f866SMatt Arsenault  store i32 0, ptr @G
92137995d8SPhilip Reames  call void(i1, ...) @llvm.experimental.guard(i1 %cond_0) [ "deopt"(i32 0) ]
934b53f866SMatt Arsenault  store i32 1, ptr @G
94137995d8SPhilip Reames  br i1 %cond_1, label %backedge, label %exit
95137995d8SPhilip Reames
96137995d8SPhilip Reamesbackedge:
974b53f866SMatt Arsenault  store i32 2, ptr @G
98137995d8SPhilip Reames  call void(i1, ...) @llvm.experimental.guard(i1 %cond_2) [ "deopt"(i32 2) ]
994b53f866SMatt Arsenault  store i32 3, ptr @G
100137995d8SPhilip Reames  br label %loop
101137995d8SPhilip Reames
102137995d8SPhilip Reamesexit:
103137995d8SPhilip Reames  ret void
104137995d8SPhilip Reames}
105137995d8SPhilip Reames
106137995d8SPhilip Reamesdefine void @widen_over_common_exit_to_ph(i1 %cond_0, i1 %cond_1, i1 %cond_2) {
107137995d8SPhilip Reames; CHECK-LABEL: @widen_over_common_exit_to_ph(
108137995d8SPhilip Reames; CHECK-NEXT:  entry:
109*0b5bb692SSerguei Katkov; CHECK-NEXT:    [[COND_2_GW_FR:%.*]] = freeze i1 [[COND_2:%.*]]
1104b53f866SMatt Arsenault; CHECK-NEXT:    store i32 0, ptr @G, align 4
111*0b5bb692SSerguei Katkov; CHECK-NEXT:    [[WIDE_CHK:%.*]] = and i1 [[COND_0:%.*]], [[COND_2_GW_FR]]
112137995d8SPhilip Reames; CHECK-NEXT:    call void (i1, ...) @llvm.experimental.guard(i1 [[WIDE_CHK]]) [ "deopt"(i32 0) ]
113137995d8SPhilip Reames; CHECK-NEXT:    br label [[LOOP:%.*]]
114137995d8SPhilip Reames; CHECK:       loop:
1154b53f866SMatt Arsenault; CHECK-NEXT:    store i32 1, ptr @G, align 4
116137995d8SPhilip Reames; CHECK-NEXT:    br i1 [[COND_1:%.*]], label [[BACKEDGE:%.*]], label [[EXIT:%.*]]
117137995d8SPhilip Reames; CHECK:       backedge:
1184b53f866SMatt Arsenault; CHECK-NEXT:    store i32 2, ptr @G, align 4
1194b53f866SMatt Arsenault; CHECK-NEXT:    store i32 3, ptr @G, align 4
120137995d8SPhilip Reames; CHECK-NEXT:    br label [[LOOP]]
121137995d8SPhilip Reames; CHECK:       exit:
122137995d8SPhilip Reames; CHECK-NEXT:    ret void
123137995d8SPhilip Reames;
124137995d8SPhilip Reamesentry:
1254b53f866SMatt Arsenault  store i32 0, ptr @G
126137995d8SPhilip Reames  call void(i1, ...) @llvm.experimental.guard(i1 %cond_0) [ "deopt"(i32 0) ]
127137995d8SPhilip Reames  br label %loop
128137995d8SPhilip Reames
129137995d8SPhilip Reamesloop:
1304b53f866SMatt Arsenault  store i32 1, ptr @G
131137995d8SPhilip Reames  br i1 %cond_1, label %backedge, label %exit
132137995d8SPhilip Reames
133137995d8SPhilip Reamesbackedge:
1344b53f866SMatt Arsenault  store i32 2, ptr @G
135137995d8SPhilip Reames  call void(i1, ...) @llvm.experimental.guard(i1 %cond_2) [ "deopt"(i32 2) ]
1364b53f866SMatt Arsenault  store i32 3, ptr @G
137137995d8SPhilip Reames  br label %loop
138137995d8SPhilip Reames
139137995d8SPhilip Reamesexit:
140137995d8SPhilip Reames  ret void
141137995d8SPhilip Reames}
142137995d8SPhilip Reames
143