Skip to content

Commit e51ef7f

Browse files
committed
[Test] Add test on missing opportunity in Loop Deletion
We can break the backedge in some cases when we can evaluate some of the values and conditions on the 1st iteration.
1 parent 4789fc7 commit e51ef7f

File tree

1 file changed

+228
-0
lines changed

1 file changed

+228
-0
lines changed
Lines changed: 228 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,228 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2+
; RUN: opt < %s -loop-deletion -S | FileCheck %s
3+
; RUN: opt < %s -passes='loop(loop-deletion)' -S | FileCheck %s
4+
5+
; The idea is that we know that %is.positive is true on the 1st iteration,
6+
; it means that we can evaluate %merge.phi = %sub on the 1st iteration,
7+
; and therefore prove that %sum.next = %sum + %sub = %sum + %limit - %sum = %limit,
8+
; and predicate is false.
9+
10+
; TODO: We can break the backedge here.
11+
define i32 @test_ne(i32 %limit) {
12+
; CHECK-LABEL: @test_ne(
13+
; CHECK-NEXT: entry:
14+
; CHECK-NEXT: br label [[LOOP:%.*]]
15+
; CHECK: loop:
16+
; CHECK-NEXT: [[SUM:%.*]] = phi i32 [ 0, [[ENTRY:%.*]] ], [ [[SUM_NEXT:%.*]], [[BACKEDGE:%.*]] ]
17+
; CHECK-NEXT: [[SUB:%.*]] = sub i32 [[LIMIT:%.*]], [[SUM]]
18+
; CHECK-NEXT: [[IS_POSITIVE:%.*]] = icmp sgt i32 [[SUB]], 0
19+
; CHECK-NEXT: br i1 [[IS_POSITIVE]], label [[BACKEDGE]], label [[IF_FALSE:%.*]]
20+
; CHECK: if.false:
21+
; CHECK-NEXT: br label [[BACKEDGE]]
22+
; CHECK: backedge:
23+
; CHECK-NEXT: [[MERGE_PHI:%.*]] = phi i32 [ 0, [[IF_FALSE]] ], [ [[SUB]], [[LOOP]] ]
24+
; CHECK-NEXT: [[SUM_NEXT]] = add i32 [[SUM]], [[MERGE_PHI]]
25+
; CHECK-NEXT: [[LOOP_COND:%.*]] = icmp ne i32 [[SUM_NEXT]], [[LIMIT]]
26+
; CHECK-NEXT: br i1 [[LOOP_COND]], label [[LOOP]], label [[DONE:%.*]]
27+
; CHECK: done:
28+
; CHECK-NEXT: [[SUM_NEXT_LCSSA:%.*]] = phi i32 [ [[SUM_NEXT]], [[BACKEDGE]] ]
29+
; CHECK-NEXT: ret i32 [[SUM_NEXT_LCSSA]]
30+
;
31+
entry:
32+
br label %loop
33+
34+
loop: ; preds = %backedge, %entry
35+
%sum = phi i32 [ 0, %entry ], [ %sum.next, %backedge ]
36+
%sub = sub i32 %limit, %sum
37+
%is.positive = icmp sgt i32 %sub, 0
38+
br i1 %is.positive, label %backedge, label %if.false
39+
40+
if.false: ; preds = %loop
41+
br label %backedge
42+
43+
backedge: ; preds = %if.false, %loop
44+
%merge.phi = phi i32 [ 0, %if.false ], [ %sub, %loop ]
45+
%sum.next = add i32 %sum, %merge.phi
46+
%loop.cond = icmp ne i32 %sum.next, %limit
47+
br i1 %loop.cond, label %loop, label %done
48+
49+
done: ; preds = %backedge
50+
%sum.next.lcssa = phi i32 [ %sum.next, %backedge ]
51+
ret i32 %sum.next.lcssa
52+
}
53+
54+
; TODO: We can break the backedge here.
55+
define i32 @test_slt(i32 %limit) {
56+
; CHECK-LABEL: @test_slt(
57+
; CHECK-NEXT: entry:
58+
; CHECK-NEXT: br label [[LOOP:%.*]]
59+
; CHECK: loop:
60+
; CHECK-NEXT: [[SUM:%.*]] = phi i32 [ 0, [[ENTRY:%.*]] ], [ [[SUM_NEXT:%.*]], [[BACKEDGE:%.*]] ]
61+
; CHECK-NEXT: [[SUB:%.*]] = sub i32 [[LIMIT:%.*]], [[SUM]]
62+
; CHECK-NEXT: [[IS_POSITIVE:%.*]] = icmp sgt i32 [[SUB]], 0
63+
; CHECK-NEXT: br i1 [[IS_POSITIVE]], label [[BACKEDGE]], label [[IF_FALSE:%.*]]
64+
; CHECK: if.false:
65+
; CHECK-NEXT: br label [[BACKEDGE]]
66+
; CHECK: backedge:
67+
; CHECK-NEXT: [[MERGE_PHI:%.*]] = phi i32 [ 0, [[IF_FALSE]] ], [ [[SUB]], [[LOOP]] ]
68+
; CHECK-NEXT: [[SUM_NEXT]] = add i32 [[SUM]], [[MERGE_PHI]]
69+
; CHECK-NEXT: [[LOOP_COND:%.*]] = icmp slt i32 [[SUM_NEXT]], [[LIMIT]]
70+
; CHECK-NEXT: br i1 [[LOOP_COND]], label [[LOOP]], label [[DONE:%.*]]
71+
; CHECK: done:
72+
; CHECK-NEXT: [[SUM_NEXT_LCSSA:%.*]] = phi i32 [ [[SUM_NEXT]], [[BACKEDGE]] ]
73+
; CHECK-NEXT: ret i32 [[SUM_NEXT_LCSSA]]
74+
;
75+
entry:
76+
br label %loop
77+
78+
loop: ; preds = %backedge, %entry
79+
%sum = phi i32 [ 0, %entry ], [ %sum.next, %backedge ]
80+
%sub = sub i32 %limit, %sum
81+
%is.positive = icmp sgt i32 %sub, 0
82+
br i1 %is.positive, label %backedge, label %if.false
83+
84+
if.false: ; preds = %loop
85+
br label %backedge
86+
87+
backedge: ; preds = %if.false, %loop
88+
%merge.phi = phi i32 [ 0, %if.false ], [ %sub, %loop ]
89+
%sum.next = add i32 %sum, %merge.phi
90+
%loop.cond = icmp slt i32 %sum.next, %limit
91+
br i1 %loop.cond, label %loop, label %done
92+
93+
done: ; preds = %backedge
94+
%sum.next.lcssa = phi i32 [ %sum.next, %backedge ]
95+
ret i32 %sum.next.lcssa
96+
}
97+
98+
; TODO: We can break the backedge here.
99+
define i32 @test_ult(i32 %limit) {
100+
; CHECK-LABEL: @test_ult(
101+
; CHECK-NEXT: entry:
102+
; CHECK-NEXT: br label [[LOOP:%.*]]
103+
; CHECK: loop:
104+
; CHECK-NEXT: [[SUM:%.*]] = phi i32 [ 0, [[ENTRY:%.*]] ], [ [[SUM_NEXT:%.*]], [[BACKEDGE:%.*]] ]
105+
; CHECK-NEXT: [[SUB:%.*]] = sub i32 [[LIMIT:%.*]], [[SUM]]
106+
; CHECK-NEXT: [[IS_POSITIVE:%.*]] = icmp sgt i32 [[SUB]], 0
107+
; CHECK-NEXT: br i1 [[IS_POSITIVE]], label [[BACKEDGE]], label [[IF_FALSE:%.*]]
108+
; CHECK: if.false:
109+
; CHECK-NEXT: br label [[BACKEDGE]]
110+
; CHECK: backedge:
111+
; CHECK-NEXT: [[MERGE_PHI:%.*]] = phi i32 [ 0, [[IF_FALSE]] ], [ [[SUB]], [[LOOP]] ]
112+
; CHECK-NEXT: [[SUM_NEXT]] = add i32 [[SUM]], [[MERGE_PHI]]
113+
; CHECK-NEXT: [[LOOP_COND:%.*]] = icmp ult i32 [[SUM_NEXT]], [[LIMIT]]
114+
; CHECK-NEXT: br i1 [[LOOP_COND]], label [[LOOP]], label [[DONE:%.*]]
115+
; CHECK: done:
116+
; CHECK-NEXT: [[SUM_NEXT_LCSSA:%.*]] = phi i32 [ [[SUM_NEXT]], [[BACKEDGE]] ]
117+
; CHECK-NEXT: ret i32 [[SUM_NEXT_LCSSA]]
118+
;
119+
entry:
120+
br label %loop
121+
122+
loop: ; preds = %backedge, %entry
123+
%sum = phi i32 [ 0, %entry ], [ %sum.next, %backedge ]
124+
%sub = sub i32 %limit, %sum
125+
%is.positive = icmp sgt i32 %sub, 0
126+
br i1 %is.positive, label %backedge, label %if.false
127+
128+
if.false: ; preds = %loop
129+
br label %backedge
130+
131+
backedge: ; preds = %if.false, %loop
132+
%merge.phi = phi i32 [ 0, %if.false ], [ %sub, %loop ]
133+
%sum.next = add i32 %sum, %merge.phi
134+
%loop.cond = icmp ult i32 %sum.next, %limit
135+
br i1 %loop.cond, label %loop, label %done
136+
137+
done: ; preds = %backedge
138+
%sum.next.lcssa = phi i32 [ %sum.next, %backedge ]
139+
ret i32 %sum.next.lcssa
140+
}
141+
142+
; TODO: We can break the backedge here.
143+
define i32 @test_sgt(i32 %limit) {
144+
; CHECK-LABEL: @test_sgt(
145+
; CHECK-NEXT: entry:
146+
; CHECK-NEXT: br label [[LOOP:%.*]]
147+
; CHECK: loop:
148+
; CHECK-NEXT: [[SUM:%.*]] = phi i32 [ 0, [[ENTRY:%.*]] ], [ [[SUM_NEXT:%.*]], [[BACKEDGE:%.*]] ]
149+
; CHECK-NEXT: [[SUB:%.*]] = sub i32 [[LIMIT:%.*]], [[SUM]]
150+
; CHECK-NEXT: [[IS_POSITIVE:%.*]] = icmp sgt i32 [[SUB]], 0
151+
; CHECK-NEXT: br i1 [[IS_POSITIVE]], label [[BACKEDGE]], label [[IF_FALSE:%.*]]
152+
; CHECK: if.false:
153+
; CHECK-NEXT: br label [[BACKEDGE]]
154+
; CHECK: backedge:
155+
; CHECK-NEXT: [[MERGE_PHI:%.*]] = phi i32 [ 0, [[IF_FALSE]] ], [ [[SUB]], [[LOOP]] ]
156+
; CHECK-NEXT: [[SUM_NEXT]] = add i32 [[SUM]], [[MERGE_PHI]]
157+
; CHECK-NEXT: [[LOOP_COND:%.*]] = icmp sgt i32 [[SUM_NEXT]], [[LIMIT]]
158+
; CHECK-NEXT: br i1 [[LOOP_COND]], label [[LOOP]], label [[DONE:%.*]]
159+
; CHECK: done:
160+
; CHECK-NEXT: [[SUM_NEXT_LCSSA:%.*]] = phi i32 [ [[SUM_NEXT]], [[BACKEDGE]] ]
161+
; CHECK-NEXT: ret i32 [[SUM_NEXT_LCSSA]]
162+
;
163+
entry:
164+
br label %loop
165+
166+
loop: ; preds = %backedge, %entry
167+
%sum = phi i32 [ 0, %entry ], [ %sum.next, %backedge ]
168+
%sub = sub i32 %limit, %sum
169+
%is.positive = icmp sgt i32 %sub, 0
170+
br i1 %is.positive, label %backedge, label %if.false
171+
172+
if.false: ; preds = %loop
173+
br label %backedge
174+
175+
backedge: ; preds = %if.false, %loop
176+
%merge.phi = phi i32 [ 0, %if.false ], [ %sub, %loop ]
177+
%sum.next = add i32 %sum, %merge.phi
178+
%loop.cond = icmp sgt i32 %sum.next, %limit
179+
br i1 %loop.cond, label %loop, label %done
180+
181+
done: ; preds = %backedge
182+
%sum.next.lcssa = phi i32 [ %sum.next, %backedge ]
183+
ret i32 %sum.next.lcssa
184+
}
185+
186+
; TODO: We can break the backedge here.
187+
define i32 @test_ugt(i32 %limit) {
188+
; CHECK-LABEL: @test_ugt(
189+
; CHECK-NEXT: entry:
190+
; CHECK-NEXT: br label [[LOOP:%.*]]
191+
; CHECK: loop:
192+
; CHECK-NEXT: [[SUM:%.*]] = phi i32 [ 0, [[ENTRY:%.*]] ], [ [[SUM_NEXT:%.*]], [[BACKEDGE:%.*]] ]
193+
; CHECK-NEXT: [[SUB:%.*]] = sub i32 [[LIMIT:%.*]], [[SUM]]
194+
; CHECK-NEXT: [[IS_POSITIVE:%.*]] = icmp sgt i32 [[SUB]], 0
195+
; CHECK-NEXT: br i1 [[IS_POSITIVE]], label [[BACKEDGE]], label [[IF_FALSE:%.*]]
196+
; CHECK: if.false:
197+
; CHECK-NEXT: br label [[BACKEDGE]]
198+
; CHECK: backedge:
199+
; CHECK-NEXT: [[MERGE_PHI:%.*]] = phi i32 [ 0, [[IF_FALSE]] ], [ [[SUB]], [[LOOP]] ]
200+
; CHECK-NEXT: [[SUM_NEXT]] = add i32 [[SUM]], [[MERGE_PHI]]
201+
; CHECK-NEXT: [[LOOP_COND:%.*]] = icmp ugt i32 [[SUM_NEXT]], [[LIMIT]]
202+
; CHECK-NEXT: br i1 [[LOOP_COND]], label [[LOOP]], label [[DONE:%.*]]
203+
; CHECK: done:
204+
; CHECK-NEXT: [[SUM_NEXT_LCSSA:%.*]] = phi i32 [ [[SUM_NEXT]], [[BACKEDGE]] ]
205+
; CHECK-NEXT: ret i32 [[SUM_NEXT_LCSSA]]
206+
;
207+
entry:
208+
br label %loop
209+
210+
loop: ; preds = %backedge, %entry
211+
%sum = phi i32 [ 0, %entry ], [ %sum.next, %backedge ]
212+
%sub = sub i32 %limit, %sum
213+
%is.positive = icmp sgt i32 %sub, 0
214+
br i1 %is.positive, label %backedge, label %if.false
215+
216+
if.false: ; preds = %loop
217+
br label %backedge
218+
219+
backedge: ; preds = %if.false, %loop
220+
%merge.phi = phi i32 [ 0, %if.false ], [ %sub, %loop ]
221+
%sum.next = add i32 %sum, %merge.phi
222+
%loop.cond = icmp ugt i32 %sum.next, %limit
223+
br i1 %loop.cond, label %loop, label %done
224+
225+
done: ; preds = %backedge
226+
%sum.next.lcssa = phi i32 [ %sum.next, %backedge ]
227+
ret i32 %sum.next.lcssa
228+
}

0 commit comments

Comments
 (0)