invariant-groups.ll
10.3 KB
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
363
; RUN: opt -basic-aa -print-memoryssa -verify-memoryssa -enable-new-pm=0 -analyze < %s 2>&1 | FileCheck %s
; RUN: opt -aa-pipeline=basic-aa -passes='print<memoryssa>' -verify-memoryssa < %s 2>&1 | FileCheck %s
;
; Currently, MemorySSA doesn't support invariant groups. So, we should ignore
; launder.invariant.group intrinsics entirely. We'll need to pay attention to
; them when/if we decide to support invariant groups.
@g = external global i32
define i32 @foo(i32* %a) {
; CHECK: 1 = MemoryDef(liveOnEntry)
; CHECK-NEXT: store i32 0
store i32 0, i32* %a, align 4, !invariant.group !0
; CHECK: 2 = MemoryDef(1)
; CHECK-NEXT: store i32 1
store i32 1, i32* @g, align 4
%1 = bitcast i32* %a to i8*
; CHECK: 3 = MemoryDef(2)
; CHECK-NEXT: %a8 = call i8* @llvm.launder.invariant.group.p0i8(i8* %1)
%a8 = call i8* @llvm.launder.invariant.group.p0i8(i8* %1)
%a32 = bitcast i8* %a8 to i32*
; This have to be MemoryUse(2), because we can't skip the barrier based on
; invariant.group.
; CHECK: MemoryUse(2)
; CHECK-NEXT: %2 = load i32
%2 = load i32, i32* %a32, align 4, !invariant.group !0
ret i32 %2
}
define i32 @skipBarrier(i32* %a) {
; CHECK: 1 = MemoryDef(liveOnEntry)
; CHECK-NEXT: store i32 0
store i32 0, i32* %a, align 4, !invariant.group !0
%1 = bitcast i32* %a to i8*
; CHECK: 2 = MemoryDef(1)
; CHECK-NEXT: %a8 = call i8* @llvm.launder.invariant.group.p0i8(i8* %1)
%a8 = call i8* @llvm.launder.invariant.group.p0i8(i8* %1)
%a32 = bitcast i8* %a8 to i32*
; We can skip the barrier only if the "skip" is not based on !invariant.group.
; CHECK: MemoryUse(1)
; CHECK-NEXT: %2 = load i32
%2 = load i32, i32* %a32, align 4, !invariant.group !0
ret i32 %2
}
define i32 @skipBarrier2(i32* %a) {
; CHECK: MemoryUse(liveOnEntry)
; CHECK-NEXT: %v = load i32
%v = load i32, i32* %a, align 4, !invariant.group !0
%1 = bitcast i32* %a to i8*
; CHECK: 1 = MemoryDef(liveOnEntry)
; CHECK-NEXT: %a8 = call i8* @llvm.launder.invariant.group.p0i8(i8* %1)
%a8 = call i8* @llvm.launder.invariant.group.p0i8(i8* %1)
%a32 = bitcast i8* %a8 to i32*
; We can skip the barrier only if the "skip" is not based on !invariant.group.
; CHECK: MemoryUse(liveOnEntry)
; CHECK-NEXT: %v2 = load i32
%v2 = load i32, i32* %a32, align 4, !invariant.group !0
; CHECK: 2 = MemoryDef(1)
; CHECK-NEXT: store i32 1
store i32 1, i32* @g, align 4
; FIXME: based on invariant.group it should be MemoryUse(liveOnEntry)
; CHECK: MemoryUse(2)
; CHECK-NEXT: %v3 = load i32
%v3 = load i32, i32* %a32, align 4, !invariant.group !0
%add = add nsw i32 %v2, %v3
%add2 = add nsw i32 %add, %v
ret i32 %add2
}
define i32 @handleInvariantGroups(i32* %a) {
; CHECK: 1 = MemoryDef(liveOnEntry)
; CHECK-NEXT: store i32 0
store i32 0, i32* %a, align 4, !invariant.group !0
; CHECK: 2 = MemoryDef(1)
; CHECK-NEXT: store i32 1
store i32 1, i32* @g, align 4
%1 = bitcast i32* %a to i8*
; CHECK: 3 = MemoryDef(2)
; CHECK-NEXT: %a8 = call i8* @llvm.launder.invariant.group.p0i8(i8* %1)
%a8 = call i8* @llvm.launder.invariant.group.p0i8(i8* %1)
%a32 = bitcast i8* %a8 to i32*
; CHECK: MemoryUse(2)
; CHECK-NEXT: %2 = load i32
%2 = load i32, i32* %a32, align 4, !invariant.group !0
; CHECK: 4 = MemoryDef(3)
; CHECK-NEXT: store i32 2
store i32 2, i32* @g, align 4
; FIXME: This can be changed to MemoryUse(2)
; CHECK: MemoryUse(4)
; CHECK-NEXT: %3 = load i32
%3 = load i32, i32* %a32, align 4, !invariant.group !0
%add = add nsw i32 %2, %3
ret i32 %add
}
define i32 @loop(i1 %a) {
entry:
%0 = alloca i32, align 4
; CHECK: 1 = MemoryDef(liveOnEntry)
; CHECK-NEXT: store i32 4
store i32 4, i32* %0, !invariant.group !0
; CHECK: 2 = MemoryDef(1)
; CHECK-NEXT: call void @clobber
call void @clobber(i32* %0)
br i1 %a, label %Loop.Body, label %Loop.End
Loop.Body:
; FIXME: MemoryUse(1)
; CHECK: MemoryUse(2)
; CHECK-NEXT: %1 = load i32
%1 = load i32, i32* %0, !invariant.group !0
br i1 %a, label %Loop.End, label %Loop.Body
Loop.End:
; FIXME: MemoryUse(1)
; CHECK: MemoryUse(2)
; CHECK-NEXT: %2 = load
%2 = load i32, i32* %0, align 4, !invariant.group !0
br i1 %a, label %Ret, label %Loop.Body
Ret:
ret i32 %2
}
define i8 @loop2(i8* %p) {
entry:
; CHECK: 1 = MemoryDef(liveOnEntry)
; CHECK-NEXT: store i8
store i8 4, i8* %p, !invariant.group !0
; CHECK: 2 = MemoryDef(1)
; CHECK-NEXT: call void @clobber
call void @clobber8(i8* %p)
; CHECK: 3 = MemoryDef(2)
; CHECK-NEXT: %after = call i8* @llvm.launder.invariant.group.p0i8(i8* %p)
%after = call i8* @llvm.launder.invariant.group.p0i8(i8* %p)
br i1 undef, label %Loop.Body, label %Loop.End
Loop.Body:
; 5 = MemoryPhi({entry,3},{Loop.Body,4},{Loop.End,6})
; CHECK: MemoryUse(6)
; CHECK-NEXT: %0 = load i8
%0 = load i8, i8* %after, !invariant.group !0
; FIXME: MemoryUse(1)
; CHECK: MemoryUse(6)
; CHECK-NEXT: %1 = load i8
%1 = load i8, i8* %p, !invariant.group !0
; CHECK: 4 = MemoryDef(6)
store i8 4, i8* %after, !invariant.group !0
br i1 undef, label %Loop.End, label %Loop.Body
Loop.End:
; 6 = MemoryPhi({entry,3},{Loop.Body,4})
; CHECK: MemoryUse(5)
; CHECK-NEXT: %2 = load
%2 = load i8, i8* %after, align 4, !invariant.group !0
; FIXME: MemoryUse(1)
; CHECK: MemoryUse(5)
; CHECK-NEXT: %3 = load
%3 = load i8, i8* %p, align 4, !invariant.group !0
br i1 undef, label %Ret, label %Loop.Body
Ret:
ret i8 %3
}
define i8 @loop3(i8* %p) {
entry:
; CHECK: 1 = MemoryDef(liveOnEntry)
; CHECK-NEXT: store i8
store i8 4, i8* %p, !invariant.group !0
; CHECK: 2 = MemoryDef(1)
; CHECK-NEXT: call void @clobber
call void @clobber8(i8* %p)
; CHECK: 3 = MemoryDef(2)
; CHECK-NEXT: %after = call i8* @llvm.launder.invariant.group.p0i8(i8* %p)
%after = call i8* @llvm.launder.invariant.group.p0i8(i8* %p)
br i1 undef, label %Loop.Body, label %Loop.End
Loop.Body:
; CHECK: 8 = MemoryPhi({entry,3},{Loop.Body,4},{Loop.next,5},{Loop.End,6})
; CHECK: MemoryUse(8)
; CHECK-NEXT: %0 = load i8
%0 = load i8, i8* %after, !invariant.group !0
; CHECK: 4 = MemoryDef(8)
; CHECK-NEXT: call void @clobber8
call void @clobber8(i8* %after)
; FIXME: MemoryUse(8)
; CHECK: MemoryUse(4)
; CHECK-NEXT: %1 = load i8
%1 = load i8, i8* %after, !invariant.group !0
br i1 undef, label %Loop.next, label %Loop.Body
Loop.next:
; CHECK: 5 = MemoryDef(4)
; CHECK-NEXT: call void @clobber8
call void @clobber8(i8* %after)
; FIXME: MemoryUse(8)
; CHECK: MemoryUse(5)
; CHECK-NEXT: %2 = load i8
%2 = load i8, i8* %after, !invariant.group !0
br i1 undef, label %Loop.End, label %Loop.Body
Loop.End:
; CHECK: 7 = MemoryPhi({entry,3},{Loop.next,5})
; CHECK: MemoryUse(7)
; CHECK-NEXT: %3 = load
%3 = load i8, i8* %after, align 4, !invariant.group !0
; CHECK: 6 = MemoryDef(7)
; CHECK-NEXT: call void @clobber8
call void @clobber8(i8* %after)
; FIXME: MemoryUse(7)
; CHECK: MemoryUse(6)
; CHECK-NEXT: %4 = load
%4 = load i8, i8* %after, align 4, !invariant.group !0
br i1 undef, label %Ret, label %Loop.Body
Ret:
ret i8 %3
}
define i8 @loop4(i8* %p) {
entry:
; CHECK: 1 = MemoryDef(liveOnEntry)
; CHECK-NEXT: store i8
store i8 4, i8* %p, !invariant.group !0
; CHECK: 2 = MemoryDef(1)
; CHECK-NEXT: call void @clobber
call void @clobber8(i8* %p)
; CHECK: 3 = MemoryDef(2)
; CHECK-NEXT: %after = call i8* @llvm.launder.invariant.group.p0i8(i8* %p)
%after = call i8* @llvm.launder.invariant.group.p0i8(i8* %p)
br i1 undef, label %Loop.Pre, label %Loop.End
Loop.Pre:
; CHECK: MemoryUse(2)
; CHECK-NEXT: %0 = load i8
%0 = load i8, i8* %after, !invariant.group !0
br label %Loop.Body
Loop.Body:
; CHECK: 6 = MemoryPhi({Loop.Pre,3},{Loop.Body,4},{Loop.End,5})
; CHECK-NEXT: MemoryUse(6)
; CHECK-NEXT: %1 = load i8
%1 = load i8, i8* %after, !invariant.group !0
; FIXME: MemoryUse(2)
; CHECK: MemoryUse(6)
; CHECK-NEXT: %2 = load i8
%2 = load i8, i8* %p, !invariant.group !0
; CHECK: 4 = MemoryDef(6)
store i8 4, i8* %after, !invariant.group !0
br i1 undef, label %Loop.End, label %Loop.Body
Loop.End:
; CHECK: 5 = MemoryPhi({entry,3},{Loop.Body,4})
; CHECK-NEXT: MemoryUse(5)
; CHECK-NEXT: %3 = load
%3 = load i8, i8* %after, align 4, !invariant.group !0
; FIXME: MemoryUse(2)
; CHECK: MemoryUse(5)
; CHECK-NEXT: %4 = load
%4 = load i8, i8* %p, align 4, !invariant.group !0
br i1 undef, label %Ret, label %Loop.Body
Ret:
ret i8 %3
}
; In the future we would like to CSE barriers if there is no clobber between.
; CHECK-LABEL: define i8 @optimizable()
define i8 @optimizable() {
entry:
%ptr = alloca i8
; CHECK: 1 = MemoryDef(liveOnEntry)
; CHECK-NEXT: store i8 42, i8* %ptr, align 1, !invariant.group !0
store i8 42, i8* %ptr, !invariant.group !0
; CHECK: 2 = MemoryDef(1)
; CHECK-NEXT: call i8* @llvm.launder.invariant.group
%ptr2 = call i8* @llvm.launder.invariant.group.p0i8(i8* %ptr)
; FIXME: This one could be CSEd.
; CHECK: 3 = MemoryDef(2)
; CHECK: call i8* @llvm.launder.invariant.group
%ptr3 = call i8* @llvm.launder.invariant.group.p0i8(i8* %ptr)
; CHECK: 4 = MemoryDef(3)
; CHECK-NEXT: call void @clobber8(i8* %ptr)
call void @clobber8(i8* %ptr)
; CHECK: 5 = MemoryDef(4)
; CHECK-NEXT: call void @use(i8* %ptr2)
call void @use(i8* %ptr2)
; CHECK: 6 = MemoryDef(5)
; CHECK-NEXT: call void @use(i8* %ptr3)
call void @use(i8* %ptr3)
; CHECK: MemoryUse(6)
; CHECK-NEXT: load i8, i8* %ptr3, {{.*}}!invariant.group
%v = load i8, i8* %ptr3, !invariant.group !0
ret i8 %v
}
; CHECK-LABEL: define i8 @unoptimizable2()
define i8 @unoptimizable2() {
%ptr = alloca i8
; CHECK: 1 = MemoryDef(liveOnEntry)
; CHECK-NEXT: store i8 42, i8* %ptr, align 1, !invariant.group !0
store i8 42, i8* %ptr, !invariant.group !0
; CHECK: 2 = MemoryDef(1)
; CHECK-NEXT: call i8* @llvm.launder.invariant.group
%ptr2 = call i8* @llvm.launder.invariant.group.p0i8(i8* %ptr)
; CHECK: 3 = MemoryDef(2)
store i8 43, i8* %ptr
; CHECK: 4 = MemoryDef(3)
; CHECK-NEXT: call i8* @llvm.launder.invariant.group
%ptr3 = call i8* @llvm.launder.invariant.group.p0i8(i8* %ptr)
; CHECK: 5 = MemoryDef(4)
; CHECK-NEXT: call void @clobber8(i8* %ptr)
call void @clobber8(i8* %ptr)
; CHECK: 6 = MemoryDef(5)
; CHECK-NEXT: call void @use(i8* %ptr2)
call void @use(i8* %ptr2)
; CHECK: 7 = MemoryDef(6)
; CHECK-NEXT: call void @use(i8* %ptr3)
call void @use(i8* %ptr3)
; CHECK: MemoryUse(7)
; CHECK-NEXT: %v = load i8, i8* %ptr3, align 1, !invariant.group !0
%v = load i8, i8* %ptr3, !invariant.group !0
ret i8 %v
}
declare i8* @llvm.launder.invariant.group.p0i8(i8*)
declare void @clobber(i32*)
declare void @clobber8(i8*)
declare void @use(i8* readonly)
!0 = !{!"group1"}