1 ; RUN: opt -basic-aa -print-memoryssa -verify-memoryssa -enable-new-pm=0 -analyze < %s 2>&1 | FileCheck %s
2 ; RUN: opt -aa-pipeline=basic-aa -passes='print<memoryssa>' -verify-memoryssa < %s 2>&1 | FileCheck %s
4 ; Currently, MemorySSA doesn't support invariant groups. So, we should ignore
5 ; launder.invariant.group intrinsics entirely. We'll need to pay attention to
6 ; them when/if we decide to support invariant groups.
8 @g = external global i32
10 define i32 @foo(i32* %a) {
11 ; CHECK: 1 = MemoryDef(liveOnEntry)
12 ; CHECK-NEXT: store i32 0
13 store i32 0, i32* %a, align 4, !invariant.group !0
15 ; CHECK: 2 = MemoryDef(1)
16 ; CHECK-NEXT: store i32 1
17 store i32 1, i32* @g, align 4
19 %1 = bitcast i32* %a to i8*
20 ; CHECK: 3 = MemoryDef(2)
21 ; CHECK-NEXT: %a8 = call i8* @llvm.launder.invariant.group.p0i8(i8* %1)
22 %a8 = call i8* @llvm.launder.invariant.group.p0i8(i8* %1)
23 %a32 = bitcast i8* %a8 to i32*
25 ; This have to be MemoryUse(2), because we can't skip the barrier based on
28 ; CHECK-NEXT: %2 = load i32
29 %2 = load i32, i32* %a32, align 4, !invariant.group !0
33 define i32 @skipBarrier(i32* %a) {
34 ; CHECK: 1 = MemoryDef(liveOnEntry)
35 ; CHECK-NEXT: store i32 0
36 store i32 0, i32* %a, align 4, !invariant.group !0
38 %1 = bitcast i32* %a to i8*
39 ; CHECK: 2 = MemoryDef(1)
40 ; CHECK-NEXT: %a8 = call i8* @llvm.launder.invariant.group.p0i8(i8* %1)
41 %a8 = call i8* @llvm.launder.invariant.group.p0i8(i8* %1)
42 %a32 = bitcast i8* %a8 to i32*
44 ; We can skip the barrier only if the "skip" is not based on !invariant.group.
46 ; CHECK-NEXT: %2 = load i32
47 %2 = load i32, i32* %a32, align 4, !invariant.group !0
51 define i32 @skipBarrier2(i32* %a) {
53 ; CHECK: MemoryUse(liveOnEntry)
54 ; CHECK-NEXT: %v = load i32
55 %v = load i32, i32* %a, align 4, !invariant.group !0
57 %1 = bitcast i32* %a to i8*
58 ; CHECK: 1 = MemoryDef(liveOnEntry)
59 ; CHECK-NEXT: %a8 = call i8* @llvm.launder.invariant.group.p0i8(i8* %1)
60 %a8 = call i8* @llvm.launder.invariant.group.p0i8(i8* %1)
61 %a32 = bitcast i8* %a8 to i32*
63 ; We can skip the barrier only if the "skip" is not based on !invariant.group.
64 ; CHECK: MemoryUse(liveOnEntry)
65 ; CHECK-NEXT: %v2 = load i32
66 %v2 = load i32, i32* %a32, align 4, !invariant.group !0
67 ; CHECK: 2 = MemoryDef(1)
68 ; CHECK-NEXT: store i32 1
69 store i32 1, i32* @g, align 4
71 ; FIXME: based on invariant.group it should be MemoryUse(liveOnEntry)
73 ; CHECK-NEXT: %v3 = load i32
74 %v3 = load i32, i32* %a32, align 4, !invariant.group !0
75 %add = add nsw i32 %v2, %v3
76 %add2 = add nsw i32 %add, %v
80 define i32 @handleInvariantGroups(i32* %a) {
81 ; CHECK: 1 = MemoryDef(liveOnEntry)
82 ; CHECK-NEXT: store i32 0
83 store i32 0, i32* %a, align 4, !invariant.group !0
85 ; CHECK: 2 = MemoryDef(1)
86 ; CHECK-NEXT: store i32 1
87 store i32 1, i32* @g, align 4
88 %1 = bitcast i32* %a to i8*
89 ; CHECK: 3 = MemoryDef(2)
90 ; CHECK-NEXT: %a8 = call i8* @llvm.launder.invariant.group.p0i8(i8* %1)
91 %a8 = call i8* @llvm.launder.invariant.group.p0i8(i8* %1)
92 %a32 = bitcast i8* %a8 to i32*
95 ; CHECK-NEXT: %2 = load i32
96 %2 = load i32, i32* %a32, align 4, !invariant.group !0
98 ; CHECK: 4 = MemoryDef(3)
99 ; CHECK-NEXT: store i32 2
100 store i32 2, i32* @g, align 4
102 ; FIXME: This can be changed to MemoryUse(2)
103 ; CHECK: MemoryUse(4)
104 ; CHECK-NEXT: %3 = load i32
105 %3 = load i32, i32* %a32, align 4, !invariant.group !0
106 %add = add nsw i32 %2, %3
110 define i32 @loop(i1 %a) {
112 %0 = alloca i32, align 4
113 ; CHECK: 1 = MemoryDef(liveOnEntry)
114 ; CHECK-NEXT: store i32 4
115 store i32 4, i32* %0, !invariant.group !0
116 ; CHECK: 2 = MemoryDef(1)
117 ; CHECK-NEXT: call void @clobber
118 call void @clobber(i32* %0)
119 br i1 %a, label %Loop.Body, label %Loop.End
122 ; FIXME: MemoryUse(1)
123 ; CHECK: MemoryUse(2)
124 ; CHECK-NEXT: %1 = load i32
125 %1 = load i32, i32* %0, !invariant.group !0
126 br i1 %a, label %Loop.End, label %Loop.Body
129 ; FIXME: MemoryUse(1)
130 ; CHECK: MemoryUse(2)
131 ; CHECK-NEXT: %2 = load
132 %2 = load i32, i32* %0, align 4, !invariant.group !0
133 br i1 %a, label %Ret, label %Loop.Body
139 define i8 @loop2(i8* %p) {
141 ; CHECK: 1 = MemoryDef(liveOnEntry)
142 ; CHECK-NEXT: store i8
143 store i8 4, i8* %p, !invariant.group !0
144 ; CHECK: 2 = MemoryDef(1)
145 ; CHECK-NEXT: call void @clobber
146 call void @clobber8(i8* %p)
148 ; CHECK: 3 = MemoryDef(2)
149 ; CHECK-NEXT: %after = call i8* @llvm.launder.invariant.group.p0i8(i8* %p)
150 %after = call i8* @llvm.launder.invariant.group.p0i8(i8* %p)
151 br i1 undef, label %Loop.Body, label %Loop.End
154 ; 5 = MemoryPhi({entry,3},{Loop.Body,4},{Loop.End,6})
155 ; CHECK: MemoryUse(6)
156 ; CHECK-NEXT: %0 = load i8
157 %0 = load i8, i8* %after, !invariant.group !0
159 ; FIXME: MemoryUse(1)
160 ; CHECK: MemoryUse(6)
161 ; CHECK-NEXT: %1 = load i8
162 %1 = load i8, i8* %p, !invariant.group !0
164 ; CHECK: 4 = MemoryDef(6)
165 store i8 4, i8* %after, !invariant.group !0
167 br i1 undef, label %Loop.End, label %Loop.Body
170 ; 6 = MemoryPhi({entry,3},{Loop.Body,4})
171 ; CHECK: MemoryUse(5)
172 ; CHECK-NEXT: %2 = load
173 %2 = load i8, i8* %after, align 4, !invariant.group !0
175 ; FIXME: MemoryUse(1)
176 ; CHECK: MemoryUse(5)
177 ; CHECK-NEXT: %3 = load
178 %3 = load i8, i8* %p, align 4, !invariant.group !0
179 br i1 undef, label %Ret, label %Loop.Body
186 define i8 @loop3(i8* %p) {
188 ; CHECK: 1 = MemoryDef(liveOnEntry)
189 ; CHECK-NEXT: store i8
190 store i8 4, i8* %p, !invariant.group !0
191 ; CHECK: 2 = MemoryDef(1)
192 ; CHECK-NEXT: call void @clobber
193 call void @clobber8(i8* %p)
195 ; CHECK: 3 = MemoryDef(2)
196 ; CHECK-NEXT: %after = call i8* @llvm.launder.invariant.group.p0i8(i8* %p)
197 %after = call i8* @llvm.launder.invariant.group.p0i8(i8* %p)
198 br i1 undef, label %Loop.Body, label %Loop.End
201 ; CHECK: 8 = MemoryPhi({entry,3},{Loop.Body,4},{Loop.next,5},{Loop.End,6})
202 ; CHECK: MemoryUse(8)
203 ; CHECK-NEXT: %0 = load i8
204 %0 = load i8, i8* %after, !invariant.group !0
206 ; CHECK: 4 = MemoryDef(8)
207 ; CHECK-NEXT: call void @clobber8
208 call void @clobber8(i8* %after)
210 ; FIXME: MemoryUse(8)
211 ; CHECK: MemoryUse(4)
212 ; CHECK-NEXT: %1 = load i8
213 %1 = load i8, i8* %after, !invariant.group !0
215 br i1 undef, label %Loop.next, label %Loop.Body
217 ; CHECK: 5 = MemoryDef(4)
218 ; CHECK-NEXT: call void @clobber8
219 call void @clobber8(i8* %after)
221 ; FIXME: MemoryUse(8)
222 ; CHECK: MemoryUse(5)
223 ; CHECK-NEXT: %2 = load i8
224 %2 = load i8, i8* %after, !invariant.group !0
226 br i1 undef, label %Loop.End, label %Loop.Body
229 ; CHECK: 7 = MemoryPhi({entry,3},{Loop.next,5})
230 ; CHECK: MemoryUse(7)
231 ; CHECK-NEXT: %3 = load
232 %3 = load i8, i8* %after, align 4, !invariant.group !0
234 ; CHECK: 6 = MemoryDef(7)
235 ; CHECK-NEXT: call void @clobber8
236 call void @clobber8(i8* %after)
238 ; FIXME: MemoryUse(7)
239 ; CHECK: MemoryUse(6)
240 ; CHECK-NEXT: %4 = load
241 %4 = load i8, i8* %after, align 4, !invariant.group !0
242 br i1 undef, label %Ret, label %Loop.Body
248 define i8 @loop4(i8* %p) {
250 ; CHECK: 1 = MemoryDef(liveOnEntry)
251 ; CHECK-NEXT: store i8
252 store i8 4, i8* %p, !invariant.group !0
253 ; CHECK: 2 = MemoryDef(1)
254 ; CHECK-NEXT: call void @clobber
255 call void @clobber8(i8* %p)
256 ; CHECK: 3 = MemoryDef(2)
257 ; CHECK-NEXT: %after = call i8* @llvm.launder.invariant.group.p0i8(i8* %p)
258 %after = call i8* @llvm.launder.invariant.group.p0i8(i8* %p)
259 br i1 undef, label %Loop.Pre, label %Loop.End
262 ; CHECK: MemoryUse(2)
263 ; CHECK-NEXT: %0 = load i8
264 %0 = load i8, i8* %after, !invariant.group !0
267 ; CHECK: 6 = MemoryPhi({Loop.Pre,3},{Loop.Body,4},{Loop.End,5})
268 ; CHECK-NEXT: MemoryUse(6)
269 ; CHECK-NEXT: %1 = load i8
270 %1 = load i8, i8* %after, !invariant.group !0
272 ; FIXME: MemoryUse(2)
273 ; CHECK: MemoryUse(6)
274 ; CHECK-NEXT: %2 = load i8
275 %2 = load i8, i8* %p, !invariant.group !0
277 ; CHECK: 4 = MemoryDef(6)
278 store i8 4, i8* %after, !invariant.group !0
279 br i1 undef, label %Loop.End, label %Loop.Body
282 ; CHECK: 5 = MemoryPhi({entry,3},{Loop.Body,4})
283 ; CHECK-NEXT: MemoryUse(5)
284 ; CHECK-NEXT: %3 = load
285 %3 = load i8, i8* %after, align 4, !invariant.group !0
287 ; FIXME: MemoryUse(2)
288 ; CHECK: MemoryUse(5)
289 ; CHECK-NEXT: %4 = load
290 %4 = load i8, i8* %p, align 4, !invariant.group !0
291 br i1 undef, label %Ret, label %Loop.Body
297 ; In the future we would like to CSE barriers if there is no clobber between.
298 ; CHECK-LABEL: define i8 @optimizable()
299 define i8 @optimizable() {
302 ; CHECK: 1 = MemoryDef(liveOnEntry)
303 ; CHECK-NEXT: store i8 42, i8* %ptr, align 1, !invariant.group !0
304 store i8 42, i8* %ptr, !invariant.group !0
305 ; CHECK: 2 = MemoryDef(1)
306 ; CHECK-NEXT: call i8* @llvm.launder.invariant.group
307 %ptr2 = call i8* @llvm.launder.invariant.group.p0i8(i8* %ptr)
308 ; FIXME: This one could be CSEd.
309 ; CHECK: 3 = MemoryDef(2)
310 ; CHECK: call i8* @llvm.launder.invariant.group
311 %ptr3 = call i8* @llvm.launder.invariant.group.p0i8(i8* %ptr)
312 ; CHECK: 4 = MemoryDef(3)
313 ; CHECK-NEXT: call void @clobber8(i8* %ptr)
314 call void @clobber8(i8* %ptr)
315 ; CHECK: 5 = MemoryDef(4)
316 ; CHECK-NEXT: call void @use(i8* %ptr2)
317 call void @use(i8* %ptr2)
318 ; CHECK: 6 = MemoryDef(5)
319 ; CHECK-NEXT: call void @use(i8* %ptr3)
320 call void @use(i8* %ptr3)
321 ; CHECK: MemoryUse(6)
322 ; CHECK-NEXT: load i8, i8* %ptr3, {{.*}}!invariant.group
323 %v = load i8, i8* %ptr3, !invariant.group !0
328 ; CHECK-LABEL: define i8 @unoptimizable2()
329 define i8 @unoptimizable2() {
331 ; CHECK: 1 = MemoryDef(liveOnEntry)
332 ; CHECK-NEXT: store i8 42, i8* %ptr, align 1, !invariant.group !0
333 store i8 42, i8* %ptr, !invariant.group !0
334 ; CHECK: 2 = MemoryDef(1)
335 ; CHECK-NEXT: call i8* @llvm.launder.invariant.group
336 %ptr2 = call i8* @llvm.launder.invariant.group.p0i8(i8* %ptr)
337 ; CHECK: 3 = MemoryDef(2)
338 store i8 43, i8* %ptr
339 ; CHECK: 4 = MemoryDef(3)
340 ; CHECK-NEXT: call i8* @llvm.launder.invariant.group
341 %ptr3 = call i8* @llvm.launder.invariant.group.p0i8(i8* %ptr)
342 ; CHECK: 5 = MemoryDef(4)
343 ; CHECK-NEXT: call void @clobber8(i8* %ptr)
344 call void @clobber8(i8* %ptr)
345 ; CHECK: 6 = MemoryDef(5)
346 ; CHECK-NEXT: call void @use(i8* %ptr2)
347 call void @use(i8* %ptr2)
348 ; CHECK: 7 = MemoryDef(6)
349 ; CHECK-NEXT: call void @use(i8* %ptr3)
350 call void @use(i8* %ptr3)
351 ; CHECK: MemoryUse(7)
352 ; CHECK-NEXT: %v = load i8, i8* %ptr3, align 1, !invariant.group !0
353 %v = load i8, i8* %ptr3, !invariant.group !0
358 declare i8* @llvm.launder.invariant.group.p0i8(i8*)
359 declare void @clobber(i32*)
360 declare void @clobber8(i8*)
361 declare void @use(i8* readonly)