1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
| ; RUN: opt -loop-unroll -S %s -verify-loop-info -verify-dom-info -verify-loop-lcssa | FileCheck %s
%struct.spam = type { double, double, double, double, double, double, double }
define void @test2(i32* %arg) {
; CHECK-LABEL: void @test2
; CHECK-NEXT: entry:
; CHECK-NEXT: br label %for.header
; CHECK-LABEL: for.header: ; preds = %entry
; CHECK-NEXT: store i32 0, i32* %arg, align 4
; CHECK-NEXT: br label %for.latch
; CHECK-LABEL: for.latch: ; preds = %for.header
; CHECK-NEXT: %ptr.1 = getelementptr inbounds i32, i32* %arg, i64 1
; CHECK-NEXT: store i32 0, i32* %ptr.1, align 4
; CHECK-NEXT: br label %for.latch.1
; CHECK-LABEL: if.end.loopexit: ; preds = %for.latch.2
; CHECK-NEXT: ret void
; CHECK-LABEL: for.latch.1: ; preds = %for.latch
; CHECK-NEXT: %ptr.2 = getelementptr inbounds i32, i32* %arg, i64 2
; CHECK-NEXT: store i32 0, i32* %ptr.2, align 4
; CHECK-NEXT: br label %for.latch.2
; CHECK-LABEL: for.latch.2: ; preds = %for.latch.1
; CHECK-NEXT: %ptr.3 = getelementptr inbounds i32, i32* %arg, i64 3
; CHECK-NEXT: store i32 0, i32* %ptr.3, align 4
; CHECK-NEXT: br i1 true, label %if.end.loopexit, label %for.latch.3
; CHECK-LABEL: for.latch.3: ; preds = %for.latch.2
; CHECK-NEXT: unreachable
entry:
br label %for.header
for.header: ; preds = %for.latch, %entry
%indvars.iv800 = phi i64 [ 0, %entry ], [ %indvars.iv.next801, %for.latch ]
%ptr = getelementptr inbounds i32, i32* %arg, i64 %indvars.iv800
store i32 0, i32* %ptr, align 4
%indvars.iv.next801 = add nuw nsw i64 %indvars.iv800, 1
%exitcond802 = icmp eq i64 %indvars.iv.next801, 4
br i1 %exitcond802, label %if.end.loopexit, label %for.latch
for.latch: ; preds = %for.header
br label %for.header
if.end.loopexit: ; preds = %for.header
ret void
}
define double @test_with_lcssa(double %arg1, double* %arg2) {
; CHECK-LABEL: define double @test_with_lcssa(
; CHECK-LABEL: entry:
; CHECK-NEXT: br label %loop.header
; CHECK-LABEL: loop.header: ; preds = %entry
; CHECK-NEXT: %res = fsub double %arg1, 3.000000e+00
; CHECK-NEXT: br label %loop.latch
; CHECK-LABEL: loop.latch: ; preds = %loop.header
; CHECK-NEXT: %ptr = getelementptr inbounds double, double* %arg2, i64 1
; CHECK-NEXT: %lv = load double, double* %ptr, align 8
; CHECK-NEXT: %res.1 = fsub double %lv, %res
; CHECK-NEXT: br i1 true, label %loop.exit, label %loop.latch.1
; CHECK-LABEL: loop.exit: ; preds = %loop.latch
; CHECK-NEXT: %res.lcssa = phi double [ %res.1, %loop.latch ]
; CHECK-NEXT: ret double %res.lcssa
; CHECK-LABEL: loop.latch.1: ; preds = %loop.latch
; CHECK-NEXT: %ptr.1 = getelementptr inbounds double, double* %arg2, i64 2
; CHECK-NEXT: unreachable
entry:
br label %loop.header
loop.header: ; preds = %entry, %loop.latch
%iv = phi i64 [ 0, %entry ], [ %iv.next, %loop.latch ]
%d1 = phi double [ %arg1, %entry ], [ %lv, %loop.latch ]
%d2 = phi double [ 3.0, %entry ], [ %res, %loop.latch ]
%res = fsub double %d1, %d2
%iv.next = add nuw nsw i64 %iv, 1
%cond = icmp eq i64 %iv.next, 2
br i1 %cond, label %loop.exit, label %loop.latch
loop.latch: ; preds = %bb366
%ptr = getelementptr inbounds double, double* %arg2, i64 %iv.next
%lv = load double, double* %ptr, align 8
br label %loop.header
loop.exit: ; preds = %bb366
%res.lcssa = phi double [ %res, %loop.header ]
ret double %res.lcssa
}
; We unroll the outer loop and need to preserve LI for the inner loop.
define void @test_with_nested_loop(i32* %arg) {
; CHECK-LABEL: void @test_with_nested_loop
; CHECK-LABEL: entry:
; CHECK-NEXT: br label %outer.header
; CHECK-DAG: outer.header: ; preds = %entry
; CHECK-NEXT: br label %inner.body.preheader
; CHECK-DAG: inner.body.preheader: ; preds = %outer.header
; CHECK-NEXT: br label %inner.body
; CHECK-LABEL: inner.body: ; preds = %inner.body.preheader, %inner.body
; CHECK-NEXT: %j.iv = phi i64 [ %j.iv.next, %inner.body ], [ 0, %inner.body.preheader ]
; CHECK-NEXT: %ptr = getelementptr inbounds i32, i32* %arg, i64 %j.iv
; CHECK-NEXT: store i32 0, i32* %ptr, align 4
; CHECK-NEXT: %j.iv.next = add nuw nsw i64 %j.iv, 1
; CHECK-NEXT: %inner.cond = icmp eq i64 %j.iv.next, 40000
; CHECK-NEXT: br i1 %inner.cond, label %outer.latch, label %inner.body
; CHECK-LABEL: outer.latch: ; preds = %inner.body
; CHECK-NEXT: br label %inner.body.preheader.1
; CHECK-LABEL: exit: ; preds = %outer.latch.1
; CHECK-NEXT: ret void
; CHECK-LABEL: inner.body.preheader.1: ; preds = %outer.latch
; CHECK-NEXT: br label %inner.body.1
; CHECK-LABEL: inner.body.1: ; preds = %inner.body.1, %inner.body.preheader.1
; CHECK-NEXT: %j.iv.1 = phi i64 [ %j.iv.next.1, %inner.body.1 ], [ 0, %inner.body.preheader.1 ]
; CHECK-NEXT: %idx.1 = add i64 1, %j.iv.1
; CHECK-NEXT: %ptr.1 = getelementptr inbounds i32, i32* %arg, i64 %idx.1
; CHECK-NEXT: store i32 0, i32* %ptr.1, align 4
; CHECK-NEXT: %j.iv.next.1 = add nuw nsw i64 %j.iv.1, 1
; CHECK-NEXT: %inner.cond.1 = icmp eq i64 %j.iv.next.1, 40000
; CHECK-NEXT: br i1 %inner.cond.1, label %outer.latch.1, label %inner.body.1
; CHECK-LABEL: outer.latch.1: ; preds = %inner.body.1
; CHECK-NEXT: br i1 true, label %exit, label %inner.body.preheader.2
; CHECK-LABEL: inner.body.preheader.2: ; preds = %outer.latch.1
; CHECK-NEXT: br label %inner.body.2
; CHECK-LABEL: inner.body.2: ; preds = %inner.body.2, %inner.body.preheader.2
; CHECK-NEXT: %j.iv.2 = phi i64 [ %j.iv.next.2, %inner.body.2 ], [ 0, %inner.body.preheader.2 ]
; CHECK-NEXT: %idx.2 = add i64 2, %j.iv.2
; CHECK-NEXT: %ptr.2 = getelementptr inbounds i32, i32* %arg, i64 %idx.2
; CHECK-NEXT: store i32 0, i32* %ptr.2, align 4
; CHECK-NEXT: %j.iv.next.2 = add nuw nsw i64 %j.iv.2, 1
; CHECK-NEXT: %inner.cond.2 = icmp eq i64 %j.iv.next.2, 40000
; CHECK-NEXT: br i1 %inner.cond.2, label %outer.latch.2, label %inner.body.2
; CHECK-LABEL: outer.latch.2: ; preds = %inner.body.2
; CHECK-NEXT: unreachable
;
entry:
br label %outer.header
outer.header: ; preds = %outer.latch, %entry
%outer.iv = phi i64 [ 0, %entry ], [ %outer.iv.next, %outer.latch ]
%outer.iv.next = add nuw nsw i64 %outer.iv, 1
%outer.cond = icmp eq i64 %outer.iv, 2
br i1 %outer.cond, label %exit, label %inner.body
inner.body:
%j.iv = phi i64 [ 0, %outer.header ], [ %j.iv.next, %inner.body ]
%idx = add i64 %outer.iv, %j.iv
%ptr = getelementptr inbounds i32, i32* %arg, i64 %idx
store i32 0, i32* %ptr, align 4
%j.iv.next = add nuw nsw i64 %j.iv, 1
%inner.cond = icmp eq i64 %j.iv.next, 40000
br i1 %inner.cond, label %outer.latch, label %inner.body
outer.latch: ; preds = %inner.body
br label %outer.header
exit: ; preds = %outer.header
ret void
}
; We unroll the inner loop and need to preserve LI for the outer loop.
define void @test_with_nested_loop_unroll_inner(i32* %arg) {
; CHECK-LABEL: define void @test_with_nested_loop_unroll_inner(
; CHECK-LABEL: entry:
; CHECK-NEXT: br label %outer.header
; CHECK-LABEL: outer.header: ; preds = %inner.body, %entry
; CHECK-NEXT: %outer.iv = phi i64 [ 0, %entry ], [ %outer.iv.next, %inner.body ]
; CHECK-NEXT: %outer.iv.next = add nuw nsw i64 %outer.iv, 1
; CHECK-NEXT: %outer.cond = icmp eq i64 %outer.iv, 40000
; CHECK-NEXT: br i1 %outer.cond, label %exit, label %inner.body.preheader
; CHECK-LABEL: inner.body.preheader: ; preds = %outer.header
; CHECK-NEXT: br label %inner.body
; CHECK-LABEL: inner.body: ; preds = %inner.body.preheader
; CHECK-NEXT: %ptr = getelementptr inbounds i32, i32* %arg, i64 %outer.iv
; CHECK-NEXT: store i32 0, i32* %ptr, align 4
; CHECK-NEXT: %idx.1 = add i64 %outer.iv, 1
; CHECK-NEXT: %ptr.1 = getelementptr inbounds i32, i32* %arg, i64 %idx.1
; CHECK-NEXT: store i32 0, i32* %ptr.1, align 4
; CHECK-NEXT: br label %outer.header
; CHECK-LABEL: exit: ; preds = %outer.header
; CHECK-NEXT: ret void
;
entry:
br label %outer.header
outer.header: ; preds = %outer.latch, %entry
%outer.iv = phi i64 [ 0, %entry ], [ %outer.iv.next, %outer.latch ]
%outer.iv.next = add nuw nsw i64 %outer.iv, 1
%outer.cond = icmp eq i64 %outer.iv, 40000
br i1 %outer.cond, label %exit, label %inner.body
inner.body:
%j.iv = phi i64 [ 0, %outer.header ], [ %j.iv.next, %inner.body ]
%idx = add i64 %outer.iv, %j.iv
%ptr = getelementptr inbounds i32, i32* %arg, i64 %idx
store i32 0, i32* %ptr, align 4
%j.iv.next = add nuw nsw i64 %j.iv, 1
%inner.cond = icmp eq i64 %j.iv.next, 2
br i1 %inner.cond, label %outer.latch, label %inner.body
outer.latch: ; preds = %inner.body
br label %outer.header
exit: ; preds = %outer.header
ret void
}
; Check that we do not crash for headers with non-branch instructions, e.g.
; switch. We do not unroll in those cases.
define void @test_switchinst_in_header() {
; CHECK-LABEL: define void @test_switchinst_in_header() {
; CHECK-LABEL: entry:
; CHECK-NEXT: br label %while.header
; CHECK-LABEL: while.header: ; preds = %while.latch, %entry
; CHECK-NEXT: switch i32 undef, label %exit [
; CHECK-NEXT: i32 11, label %while.body1
; CHECK-NEXT: i32 5, label %while.body2
; CHECK-NEXT: ]
; CHECK-LABEL: while.body1: ; preds = %while.header
; CHECK-NEXT: unreachable
; CHECK-LABEL: while.body2: ; preds = %while.header
; CHECK-NEXT: br label %while.latch
; CHECK-LABEL: while.latch: ; preds = %while.body2
; CHECK-NEXT: br label %while.header
; CHECK-LABEL: exit: ; preds = %while.header
; CHECK-NEXT: ret void
;
entry:
br label %while.header
while.header: ; preds = %while.latch, %entry
switch i32 undef, label %exit [
i32 11, label %while.body1
i32 5, label %while.body2
]
while.body1: ; preds = %while.header
unreachable
while.body2: ; preds = %while.header
br label %while.latch
while.latch: ; preds = %while.body2
br label %while.header
exit: ; preds = %while.header
ret void
}
|