1 ; RUN: opt < %s -aa-pipeline=basic-aa,globals-aa -passes='require<globals-aa>,gvn' -S | FileCheck %s
3 ; This tests the safe no-alias conclusions of GMR -- when there is
4 ; a non-escaping global as one indentified underlying object and some pointer
5 ; that would inherently have escaped any other function as the other underlying
6 ; pointer of an alias query.
8 @g1 = internal global i32 0
10 define i32 @test1(ptr %param) {
11 ; Ensure that we can fold a store to a load of a global across a store to
12 ; a parameter when the global is non-escaping.
14 ; CHECK-LABEL: @test1(
15 ; CHECK: store i32 42, ptr @g1
20 store i32 7, ptr %param
21 %v = load i32, ptr @g1
25 @g1_tls = internal thread_local global i32 0
27 define i32 @test1_tls(ptr %param) {
28 ; Ensure that we can fold a store to a load of a global across a store to
29 ; a parameter when the global is non-escaping.
31 ; CHECK-LABEL: define i32 @test1_tls(
32 ; CHECK-SAME: ptr [[PARAM:%.*]]) {
34 ; CHECK-NEXT: [[P:%.*]] = call ptr @llvm.threadlocal.address.p0(ptr @g1_tls)
35 ; CHECK-NEXT: store i32 42, ptr [[P]], align 4
36 ; CHECK-NEXT: store i32 7, ptr [[PARAM]], align 4
37 ; CHECK-NEXT: ret i32 42
40 %p = call ptr @llvm.threadlocal.address(ptr @g1_tls)
42 store i32 7, ptr %param
43 %p2 = call ptr @llvm.threadlocal.address(ptr @g1_tls)
44 %v = load i32, ptr %p2
48 define ptr @test1_tls_noopt(ptr %coro, ptr %param) presplitcoroutine {
49 ; CHECK-LABEL: define ptr @test1_tls_noopt(
50 ; CHECK-SAME: ptr [[CORO:%.*]], ptr [[PARAM:%.*]]) #[[ATTR0:[0-9]+]] {
52 ; CHECK-NEXT: [[P:%.*]] = call ptr @llvm.threadlocal.address.p0(ptr @g1_tls)
53 ; CHECK-NEXT: store i32 42, ptr [[P]], align 4
54 ; CHECK-NEXT: [[TMP0:%.*]] = call i8 @llvm.coro.suspend(token none, i1 false)
55 ; CHECK-NEXT: switch i8 [[TMP0]], label [[SUSPEND:%.*]] [
56 ; CHECK-NEXT: i8 0, label [[RESUME:%.*]]
57 ; CHECK-NEXT: i8 1, label [[SUSPEND]]
60 ; CHECK-NEXT: [[P2:%.*]] = call ptr @llvm.threadlocal.address.p0(ptr @g1_tls)
61 ; CHECK-NEXT: [[V:%.*]] = load i32, ptr [[P2]], align 4
62 ; CHECK-NEXT: store i32 [[V]], ptr [[PARAM]], align 4
63 ; CHECK-NEXT: ret ptr [[CORO]]
65 ; CHECK-NEXT: [[TMP1:%.*]] = call i1 @llvm.coro.end(ptr [[CORO]], i1 false, token none)
66 ; CHECK-NEXT: ret ptr [[CORO]]
69 %p = call ptr @llvm.threadlocal.address(ptr @g1_tls)
72 %0 = call i8 @llvm.coro.suspend(token none, i1 false)
73 switch i8 %0, label %suspend [i8 0, label %resume
76 %p2 = call ptr @llvm.threadlocal.address(ptr @g1_tls)
77 %v = load i32, ptr %p2
78 store i32 %v, ptr %param, align 4
82 call i1 @llvm.coro.end(ptr %coro, i1 0, token none)
89 ; Ensure that we can fold a store to a load of a global across a store to
90 ; the pointer returned by a function call. Since the global could not escape,
91 ; this function cannot be returning its address.
93 ; CHECK-LABEL: @test2(
94 ; CHECK: store i32 42, ptr @g1
98 %ptr = call ptr @f() readnone
100 store i32 7, ptr %ptr
101 %v = load i32, ptr @g1
105 @g2 = external global ptr
107 define i32 @test3() {
108 ; Ensure that we can fold a store to a load of a global across a store to
109 ; the pointer loaded from that global. Because the global does not escape, it
110 ; cannot alias a pointer loaded out of a global.
112 ; CHECK-LABEL: @test3(
113 ; CHECK: store i32 42, ptr @g1
114 ; CHECK: store i32 7, ptr
115 ; CHECK-NOT: load i32
118 store i32 42, ptr @g1
119 %ptr1 = load ptr, ptr @g2
120 store i32 7, ptr %ptr1
121 %v = load i32, ptr @g1
125 @g3 = internal global i32 1
126 @g4 = internal global [10 x ptr] zeroinitializer
128 define i32 @test4(ptr %param, i32 %n, i1 %c1, i1 %c2, i1 %c3) {
129 ; Ensure that we can fold a store to a load of a global across a store to
130 ; the pointer loaded from that global even when the load is behind PHIs and
131 ; selects, and there is a mixture of a load and another global or argument.
132 ; Note that we can't eliminate the load here because it is used in a PHI and
133 ; GVN doesn't try to do real DCE. The store is still forwarded by GVN though.
135 ; CHECK-LABEL: @test4(
136 ; CHECK: store i32 42, ptr @g1
137 ; CHECK: store i32 7, ptr
140 %call = call ptr @f()
141 store i32 42, ptr @g1
142 %ptr1 = load ptr, ptr @g2
143 %ptr2 = select i1 %c1, ptr %ptr1, ptr %param
144 %ptr3 = select i1 %c3, ptr %ptr2, ptr @g3
148 %iv = phi i32 [ 0, %entry ], [ %inc, %loop ]
149 %ptr = phi ptr [ %ptr3, %entry ], [ %ptr5, %loop ]
150 store i32 7, ptr %ptr
151 %ptr4 = load ptr, ptr getelementptr ([10 x ptr], ptr @g4, i32 0, i32 1)
152 %ptr5 = select i1 %c2, ptr %ptr4, ptr %call
153 %inc = add i32 %iv, 1
154 %test = icmp slt i32 %inc, %n
155 br i1 %test, label %loop, label %exit
158 %v = load i32, ptr @g1
162 define i32 @test5(ptr %param) {
163 ; Ensure that we can fold a store to a load of a global across a store to
164 ; a parameter that has been dereferenced when the global is non-escaping.
166 ; CHECK-LABEL: @test5(
167 ; CHECK: %p = load ptr
168 ; CHECK: store i32 42, ptr @g1
169 ; CHECK-NOT: load i32
172 %p = load ptr, ptr %param
173 store i32 42, ptr @g1
175 %v = load i32, ptr @g1