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
| ; RUN: opt < %s -S -early-cse | FileCheck %s
; RUN: opt < %s -S -passes=early-cse | FileCheck %s
declare {}* @llvm.invariant.start.p0i8(i64, i8* nocapture) nounwind readonly
declare void @llvm.invariant.end.p0i8({}*, i64, i8* nocapture) nounwind
; Check that we do load-load forwarding over invariant.start, since it does not
; clobber memory
define i8 @test_bypass1(i8 *%P) {
; CHECK-LABEL: @test_bypass1(
; CHECK-NEXT: %V1 = load i8, i8* %P
; CHECK-NEXT: %i = call {}* @llvm.invariant.start.p0i8(i64 1, i8* %P)
; CHECK-NEXT: ret i8 0
%V1 = load i8, i8* %P
%i = call {}* @llvm.invariant.start.p0i8(i64 1, i8* %P)
%V2 = load i8, i8* %P
%Diff = sub i8 %V1, %V2
ret i8 %Diff
}
; Trivial Store->load forwarding over invariant.start
define i8 @test_bypass2(i8 *%P) {
; CHECK-LABEL: @test_bypass2(
; CHECK-NEXT: store i8 42, i8* %P
; CHECK-NEXT: %i = call {}* @llvm.invariant.start.p0i8(i64 1, i8* %P)
; CHECK-NEXT: ret i8 42
store i8 42, i8* %P
%i = call {}* @llvm.invariant.start.p0i8(i64 1, i8* %P)
%V1 = load i8, i8* %P
ret i8 %V1
}
; We can DSE over invariant.start calls, since the first store to
; %P is valid, and the second store is actually unreachable based on semantics
; of invariant.start.
define void @test_bypass3(i8* %P) {
; CHECK-LABEL: @test_bypass3(
; CHECK-NEXT: %i = call {}* @llvm.invariant.start.p0i8(i64 1, i8* %P)
; CHECK-NEXT: store i8 60, i8* %P
store i8 50, i8* %P
%i = call {}* @llvm.invariant.start.p0i8(i64 1, i8* %P)
store i8 60, i8* %P
ret void
}
; FIXME: Now the first store can actually be eliminated, since there is no read within
; the invariant region, between start and end.
define void @test_bypass4(i8* %P) {
; CHECK-LABEL: @test_bypass4(
; CHECK-NEXT: store i8 50, i8* %P
; CHECK-NEXT: %i = call {}* @llvm.invariant.start.p0i8(i64 1, i8* %P)
; CHECK-NEXT: call void @llvm.invariant.end.p0i8({}* %i, i64 1, i8* %P)
; CHECK-NEXT: store i8 60, i8* %P
store i8 50, i8* %P
%i = call {}* @llvm.invariant.start.p0i8(i64 1, i8* %P)
call void @llvm.invariant.end.p0i8({}* %i, i64 1, i8* %P)
store i8 60, i8* %P
ret void
}
declare void @clobber()
declare {}* @llvm.invariant.start.p0i32(i64 %size, i32* nocapture %ptr)
declare void @llvm.invariant.end.p0i32({}*, i64, i32* nocapture) nounwind
define i32 @test_before_load(i32* %p) {
; CHECK-LABEL: @test_before_load
; CHECK: ret i32 0
call {}* @llvm.invariant.start.p0i32(i64 4, i32* %p)
%v1 = load i32, i32* %p
call void @clobber()
%v2 = load i32, i32* %p
%sub = sub i32 %v1, %v2
ret i32 %sub
}
define i32 @test_before_clobber(i32* %p) {
; CHECK-LABEL: @test_before_clobber
; CHECK: ret i32 0
%v1 = load i32, i32* %p
call {}* @llvm.invariant.start.p0i32(i64 4, i32* %p)
call void @clobber()
%v2 = load i32, i32* %p
%sub = sub i32 %v1, %v2
ret i32 %sub
}
define i32 @test_duplicate_scope(i32* %p) {
; CHECK-LABEL: @test_duplicate_scope
; CHECK: ret i32 0
%v1 = load i32, i32* %p
call {}* @llvm.invariant.start.p0i32(i64 4, i32* %p)
call void @clobber()
call {}* @llvm.invariant.start.p0i32(i64 4, i32* %p)
%v2 = load i32, i32* %p
%sub = sub i32 %v1, %v2
ret i32 %sub
}
define i32 @test_unanalzyable_load(i32* %p) {
; CHECK-LABEL: @test_unanalzyable_load
; CHECK: ret i32 0
call {}* @llvm.invariant.start.p0i32(i64 4, i32* %p)
call void @clobber()
%v1 = load i32, i32* %p
call void @clobber()
%v2 = load i32, i32* %p
%sub = sub i32 %v1, %v2
ret i32 %sub
}
define i32 @test_negative_after_clobber(i32* %p) {
; CHECK-LABEL: @test_negative_after_clobber
; CHECK: ret i32 %sub
%v1 = load i32, i32* %p
call void @clobber()
call {}* @llvm.invariant.start.p0i32(i64 4, i32* %p)
%v2 = load i32, i32* %p
%sub = sub i32 %v1, %v2
ret i32 %sub
}
define i32 @test_merge(i32* %p, i1 %cnd) {
; CHECK-LABEL: @test_merge
; CHECK: ret i32 0
%v1 = load i32, i32* %p
call {}* @llvm.invariant.start.p0i32(i64 4, i32* %p)
br i1 %cnd, label %merge, label %taken
taken:
call void @clobber()
br label %merge
merge:
%v2 = load i32, i32* %p
%sub = sub i32 %v1, %v2
ret i32 %sub
}
define i32 @test_negative_after_mergeclobber(i32* %p, i1 %cnd) {
; CHECK-LABEL: @test_negative_after_mergeclobber
; CHECK: ret i32 %sub
%v1 = load i32, i32* %p
br i1 %cnd, label %merge, label %taken
taken:
call void @clobber()
br label %merge
merge:
call {}* @llvm.invariant.start.p0i32(i64 4, i32* %p)
%v2 = load i32, i32* %p
%sub = sub i32 %v1, %v2
ret i32 %sub
}
; In theory, this version could work, but earlycse is incapable of
; merging facts along distinct paths.
define i32 @test_false_negative_merge(i32* %p, i1 %cnd) {
; CHECK-LABEL: @test_false_negative_merge
; CHECK: ret i32 %sub
%v1 = load i32, i32* %p
br i1 %cnd, label %merge, label %taken
taken:
call {}* @llvm.invariant.start.p0i32(i64 4, i32* %p)
call void @clobber()
br label %merge
merge:
%v2 = load i32, i32* %p
%sub = sub i32 %v1, %v2
ret i32 %sub
}
define i32 @test_merge_unanalyzable_load(i32* %p, i1 %cnd) {
; CHECK-LABEL: @test_merge_unanalyzable_load
; CHECK: ret i32 0
call {}* @llvm.invariant.start.p0i32(i64 4, i32* %p)
call void @clobber()
%v1 = load i32, i32* %p
br i1 %cnd, label %merge, label %taken
taken:
call void @clobber()
br label %merge
merge:
%v2 = load i32, i32* %p
%sub = sub i32 %v1, %v2
ret i32 %sub
}
define void @test_dse_before_load(i32* %p, i1 %cnd) {
; CHECK-LABEL: @test_dse_before_load
; CHECK-NOT: store
call {}* @llvm.invariant.start.p0i32(i64 4, i32* %p)
%v1 = load i32, i32* %p
call void @clobber()
store i32 %v1, i32* %p
ret void
}
define void @test_dse_after_load(i32* %p, i1 %cnd) {
; CHECK-LABEL: @test_dse_after_load
; CHECK-NOT: store
%v1 = load i32, i32* %p
call {}* @llvm.invariant.start.p0i32(i64 4, i32* %p)
call void @clobber()
store i32 %v1, i32* %p
ret void
}
; In this case, we have a false negative since MemoryLocation is implicitly
; typed due to the user of a Value to represent the address. Note that other
; passes will canonicalize away the bitcasts in this example.
define i32 @test_false_negative_types(i32* %p) {
; CHECK-LABEL: @test_false_negative_types
; CHECK: ret i32 %sub
call {}* @llvm.invariant.start.p0i32(i64 4, i32* %p)
%v1 = load i32, i32* %p
call void @clobber()
%pf = bitcast i32* %p to float*
%v2f = load float, float* %pf
%v2 = bitcast float %v2f to i32
%sub = sub i32 %v1, %v2
ret i32 %sub
}
define i32 @test_negative_size1(i32* %p) {
; CHECK-LABEL: @test_negative_size1
; CHECK: ret i32 %sub
call {}* @llvm.invariant.start.p0i32(i64 3, i32* %p)
%v1 = load i32, i32* %p
call void @clobber()
%v2 = load i32, i32* %p
%sub = sub i32 %v1, %v2
ret i32 %sub
}
define i32 @test_negative_size2(i32* %p) {
; CHECK-LABEL: @test_negative_size2
; CHECK: ret i32 %sub
call {}* @llvm.invariant.start.p0i32(i64 0, i32* %p)
%v1 = load i32, i32* %p
call void @clobber()
%v2 = load i32, i32* %p
%sub = sub i32 %v1, %v2
ret i32 %sub
}
define i32 @test_negative_scope(i32* %p) {
; CHECK-LABEL: @test_negative_scope
; CHECK: ret i32 %sub
%scope = call {}* @llvm.invariant.start.p0i32(i64 4, i32* %p)
call void @llvm.invariant.end.p0i32({}* %scope, i64 4, i32* %p)
%v1 = load i32, i32* %p
call void @clobber()
%v2 = load i32, i32* %p
%sub = sub i32 %v1, %v2
ret i32 %sub
}
define i32 @test_false_negative_scope(i32* %p) {
; CHECK-LABEL: @test_false_negative_scope
; CHECK: ret i32 %sub
%scope = call {}* @llvm.invariant.start.p0i32(i64 4, i32* %p)
%v1 = load i32, i32* %p
call void @clobber()
%v2 = load i32, i32* %p
call void @llvm.invariant.end.p0i32({}* %scope, i64 4, i32* %p)
%sub = sub i32 %v1, %v2
ret i32 %sub
}
; Invariant load defact starts an invariant.start scope of the appropriate size
define i32 @test_invariant_load_scope(i32* %p) {
; CHECK-LABEL: @test_invariant_load_scope
; CHECK: ret i32 0
%v1 = load i32, i32* %p, !invariant.load !{}
call void @clobber()
%v2 = load i32, i32* %p
%sub = sub i32 %v1, %v2
ret i32 %sub
}
|