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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
| ; RUN: opt < %s -licm -S | FileCheck %s
; RUN: opt < %s -aa-pipeline=basic-aa -passes='require<opt-remark-emit>,loop(licm)' -S | FileCheck %s
; RUN: opt < %s -licm -enable-mssa-loop-dependency=true -verify-memoryssa -S | FileCheck %s
@X = global i32 0 ; <i32*> [#uses=1]
declare void @foo()
declare i32 @llvm.bitreverse.i32(i32)
; This testcase tests for a problem where LICM hoists
; potentially trapping instructions when they are not guaranteed to execute.
define i32 @test1(i1 %c) {
; CHECK-LABEL: @test1(
%A = load i32, i32* @X ; <i32> [#uses=2]
br label %Loop
Loop: ; preds = %LoopTail, %0
call void @foo( )
br i1 %c, label %LoopTail, label %IfUnEqual
IfUnEqual: ; preds = %Loop
; CHECK: IfUnEqual:
; CHECK-NEXT: sdiv i32 4, %A
%B1 = sdiv i32 4, %A ; <i32> [#uses=1]
br label %LoopTail
LoopTail: ; preds = %IfUnEqual, %Loop
%B = phi i32 [ 0, %Loop ], [ %B1, %IfUnEqual ] ; <i32> [#uses=1]
br i1 %c, label %Loop, label %Out
Out: ; preds = %LoopTail
%C = sub i32 %A, %B ; <i32> [#uses=1]
ret i32 %C
}
declare void @foo2(i32) nounwind
;; It is ok and desirable to hoist this potentially trapping instruction.
define i32 @test2(i1 %c) {
; CHECK-LABEL: @test2(
; CHECK-NEXT: load i32, i32* @X
; CHECK-NEXT: %B = sdiv i32 4, %A
%A = load i32, i32* @X
br label %Loop
Loop:
;; Should have hoisted this div!
%B = sdiv i32 4, %A
br label %loop2
loop2:
call void @foo2( i32 %B )
br i1 %c, label %Loop, label %Out
Out:
%C = sub i32 %A, %B
ret i32 %C
}
; This loop invariant instruction should be constant folded, not hoisted.
define i32 @test3(i1 %c) {
; CHECK-LABEL: define i32 @test3(
; CHECK: call void @foo2(i32 6)
%A = load i32, i32* @X ; <i32> [#uses=2]
br label %Loop
Loop:
%B = add i32 4, 2 ; <i32> [#uses=2]
call void @foo2( i32 %B )
br i1 %c, label %Loop, label %Out
Out: ; preds = %Loop
%C = sub i32 %A, %B ; <i32> [#uses=1]
ret i32 %C
}
; CHECK-LABEL: @test4(
; CHECK: call
; CHECK: sdiv
; CHECK: ret
define i32 @test4(i32 %x, i32 %y) nounwind uwtable ssp {
entry:
br label %for.body
for.body: ; preds = %entry, %for.body
%i.02 = phi i32 [ 0, %entry ], [ %inc, %for.body ]
%n.01 = phi i32 [ 0, %entry ], [ %add, %for.body ]
call void @foo_may_call_exit(i32 0)
%div = sdiv i32 %x, %y
%add = add nsw i32 %n.01, %div
%inc = add nsw i32 %i.02, 1
%cmp = icmp slt i32 %inc, 10000
br i1 %cmp, label %for.body, label %for.end
for.end: ; preds = %for.body
%n.0.lcssa = phi i32 [ %add, %for.body ]
ret i32 %n.0.lcssa
}
declare void @foo_may_call_exit(i32)
; PR14854
; CHECK-LABEL: @test5(
; CHECK: extractvalue
; CHECK: br label %tailrecurse
; CHECK: tailrecurse:
; CHECK: ifend:
; CHECK: insertvalue
define { i32*, i32 } @test5(i32 %i, { i32*, i32 } %e) {
entry:
br label %tailrecurse
tailrecurse: ; preds = %then, %entry
%i.tr = phi i32 [ %i, %entry ], [ %cmp2, %then ]
%out = extractvalue { i32*, i32 } %e, 1
%d = insertvalue { i32*, i32 } %e, i32* null, 0
%cmp1 = icmp sgt i32 %out, %i.tr
br i1 %cmp1, label %then, label %ifend
then: ; preds = %tailrecurse
call void @foo()
%cmp2 = add i32 %i.tr, 1
br label %tailrecurse
ifend: ; preds = %tailrecurse
ret { i32*, i32 } %d
}
; CHECK: define void @test6(float %f)
; CHECK: fneg
; CHECK: br label %for.body
define void @test6(float %f) #2 {
entry:
br label %for.body
for.body: ; preds = %for.body, %entry
%i = phi i32 [ 0, %entry ], [ %inc, %for.body ]
call void @foo_may_call_exit(i32 0)
%neg = fneg float %f
call void @use(float %neg)
%inc = add nsw i32 %i, 1
%cmp = icmp slt i32 %inc, 10000
br i1 %cmp, label %for.body, label %for.end
for.end: ; preds = %for.body
ret void
}
declare void @use(float)
; CHECK: define i32 @hoist_bitreverse(i32 %0)
; CHECK: bitreverse
; CHECK: br label %header
define i32 @hoist_bitreverse(i32 %0) {
br label %header
header:
%sum = phi i32 [ 0, %1 ], [ %5, %latch ]
%2 = phi i32 [ 0, %1 ], [ %6, %latch ]
%3 = icmp slt i32 %2, 1024
br i1 %3, label %body, label %return
body:
%4 = call i32 @llvm.bitreverse.i32(i32 %0)
%5 = add i32 %sum, %4
br label %latch
latch:
%6 = add nsw i32 %2, 1
br label %header
return:
ret i32 %sum
}
; Can neither sink nor hoist
define i32 @test_volatile(i1 %c) {
; CHECK-LABEL: @test_volatile(
; CHECK-LABEL: Loop:
; CHECK: load volatile i32, i32* @X
; CHECK-LABEL: Out:
br label %Loop
Loop:
%A = load volatile i32, i32* @X
br i1 %c, label %Loop, label %Out
Out:
ret i32 %A
}
declare {}* @llvm.invariant.start.p0i8(i64, i8* nocapture) nounwind readonly
declare void @llvm.invariant.end.p0i8({}*, i64, i8* nocapture) nounwind
declare void @escaping.invariant.start({}*) nounwind
; invariant.start dominates the load, and in this scope, the
; load is invariant. So, we can hoist the `addrld` load out of the loop.
define i32 @test_fence(i8* %addr, i32 %n, i8* %volatile) {
; CHECK-LABEL: @test_fence
; CHECK-LABEL: entry
; CHECK: invariant.start
; CHECK: %addrld = load atomic i32, i32* %addr.i unordered, align 8
; CHECK: br label %loop
entry:
%gep = getelementptr inbounds i8, i8* %addr, i64 8
%addr.i = bitcast i8* %gep to i32 *
store atomic i32 5, i32 * %addr.i unordered, align 8
fence release
%invst = call {}* @llvm.invariant.start.p0i8(i64 4, i8* %gep)
br label %loop
loop:
%indvar = phi i32 [ %indvar.next, %loop ], [ 0, %entry ]
%sum = phi i32 [ %sum.next, %loop ], [ 0, %entry ]
%volload = load atomic i8, i8* %volatile unordered, align 8
fence acquire
%volchk = icmp eq i8 %volload, 0
%addrld = load atomic i32, i32* %addr.i unordered, align 8
%sel = select i1 %volchk, i32 0, i32 %addrld
%sum.next = add i32 %sel, %sum
%indvar.next = add i32 %indvar, 1
%cond = icmp slt i32 %indvar.next, %n
br i1 %cond, label %loop, label %loopexit
loopexit:
ret i32 %sum
}
; Same as test above, but the load is no longer invariant (presence of
; invariant.end). We cannot hoist the addrld out of loop.
define i32 @test_fence1(i8* %addr, i32 %n, i8* %volatile) {
; CHECK-LABEL: @test_fence1
; CHECK-LABEL: entry
; CHECK: invariant.start
; CHECK-NEXT: invariant.end
; CHECK-NEXT: br label %loop
entry:
%gep = getelementptr inbounds i8, i8* %addr, i64 8
%addr.i = bitcast i8* %gep to i32 *
store atomic i32 5, i32 * %addr.i unordered, align 8
fence release
%invst = call {}* @llvm.invariant.start.p0i8(i64 4, i8* %gep)
call void @llvm.invariant.end.p0i8({}* %invst, i64 4, i8* %gep)
br label %loop
loop:
%indvar = phi i32 [ %indvar.next, %loop ], [ 0, %entry ]
%sum = phi i32 [ %sum.next, %loop ], [ 0, %entry ]
%volload = load atomic i8, i8* %volatile unordered, align 8
fence acquire
%volchk = icmp eq i8 %volload, 0
%addrld = load atomic i32, i32* %addr.i unordered, align 8
%sel = select i1 %volchk, i32 0, i32 %addrld
%sum.next = add i32 %sel, %sum
%indvar.next = add i32 %indvar, 1
%cond = icmp slt i32 %indvar.next, %n
br i1 %cond, label %loop, label %loopexit
loopexit:
ret i32 %sum
}
; same as test above, but instead of invariant.end, we have the result of
; invariant.start escaping through a call. We cannot hoist the load.
define i32 @test_fence2(i8* %addr, i32 %n, i8* %volatile) {
; CHECK-LABEL: @test_fence2
; CHECK-LABEL: entry
; CHECK-NOT: load
; CHECK: br label %loop
entry:
%gep = getelementptr inbounds i8, i8* %addr, i64 8
%addr.i = bitcast i8* %gep to i32 *
store atomic i32 5, i32 * %addr.i unordered, align 8
fence release
%invst = call {}* @llvm.invariant.start.p0i8(i64 4, i8* %gep)
call void @escaping.invariant.start({}* %invst)
br label %loop
loop:
%indvar = phi i32 [ %indvar.next, %loop ], [ 0, %entry ]
%sum = phi i32 [ %sum.next, %loop ], [ 0, %entry ]
%volload = load atomic i8, i8* %volatile unordered, align 8
fence acquire
%volchk = icmp eq i8 %volload, 0
%addrld = load atomic i32, i32* %addr.i unordered, align 8
%sel = select i1 %volchk, i32 0, i32 %addrld
%sum.next = add i32 %sel, %sum
%indvar.next = add i32 %indvar, 1
%cond = icmp slt i32 %indvar.next, %n
br i1 %cond, label %loop, label %loopexit
loopexit:
ret i32 %sum
}
; FIXME: invariant.start dominates the load, and in this scope, the
; load is invariant. So, we can hoist the `addrld` load out of the loop.
; Consider the loadoperand addr.i bitcasted before being passed to
; invariant.start
define i32 @test_fence3(i32* %addr, i32 %n, i8* %volatile) {
; CHECK-LABEL: @test_fence3
; CHECK-LABEL: entry
; CHECK: invariant.start
; CHECK-NOT: %addrld = load atomic i32, i32* %addr.i unordered, align 8
; CHECK: br label %loop
entry:
%addr.i = getelementptr inbounds i32, i32* %addr, i64 8
%gep = bitcast i32* %addr.i to i8 *
store atomic i32 5, i32 * %addr.i unordered, align 8
fence release
%invst = call {}* @llvm.invariant.start.p0i8(i64 4, i8* %gep)
br label %loop
loop:
%indvar = phi i32 [ %indvar.next, %loop ], [ 0, %entry ]
%sum = phi i32 [ %sum.next, %loop ], [ 0, %entry ]
%volload = load atomic i8, i8* %volatile unordered, align 8
fence acquire
%volchk = icmp eq i8 %volload, 0
%addrld = load atomic i32, i32* %addr.i unordered, align 8
%sel = select i1 %volchk, i32 0, i32 %addrld
%sum.next = add i32 %sel, %sum
%indvar.next = add i32 %indvar, 1
%cond = icmp slt i32 %indvar.next, %n
br i1 %cond, label %loop, label %loopexit
loopexit:
ret i32 %sum
}
; We should not hoist the addrld out of the loop.
define i32 @test_fence4(i32* %addr, i32 %n, i8* %volatile) {
; CHECK-LABEL: @test_fence4
; CHECK-LABEL: entry
; CHECK-NOT: %addrld = load atomic i32, i32* %addr.i unordered, align 8
; CHECK: br label %loop
entry:
%addr.i = getelementptr inbounds i32, i32* %addr, i64 8
%gep = bitcast i32* %addr.i to i8 *
br label %loop
loop:
%indvar = phi i32 [ %indvar.next, %loop ], [ 0, %entry ]
%sum = phi i32 [ %sum.next, %loop ], [ 0, %entry ]
store atomic i32 5, i32 * %addr.i unordered, align 8
fence release
%invst = call {}* @llvm.invariant.start.p0i8(i64 4, i8* %gep)
%volload = load atomic i8, i8* %volatile unordered, align 8
fence acquire
%volchk = icmp eq i8 %volload, 0
%addrld = load atomic i32, i32* %addr.i unordered, align 8
%sel = select i1 %volchk, i32 0, i32 %addrld
%sum.next = add i32 %sel, %sum
%indvar.next = add i32 %indvar, 1
%cond = icmp slt i32 %indvar.next, %n
br i1 %cond, label %loop, label %loopexit
loopexit:
ret i32 %sum
}
|