1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2 ; RUN: opt -passes=simplifycfg -simplifycfg-require-and-preserve-domtree=1 -S < %s | FileCheck %s
4 define void @ifconvertstore(ptr %A, i32 %B, i32 %C, i32 %D) {
5 ; CHECK-LABEL: @ifconvertstore(
7 ; CHECK-NEXT: store i32 [[B:%.*]], ptr [[A:%.*]], align 4
8 ; CHECK-NEXT: [[CMP:%.*]] = icmp sgt i32 [[D:%.*]], 42
9 ; CHECK-NEXT: [[SPEC_STORE_SELECT:%.*]] = select i1 [[CMP]], i32 [[C:%.*]], i32 [[B]], !prof [[PROF0:![0-9]+]]
10 ; CHECK-NEXT: store i32 [[SPEC_STORE_SELECT]], ptr [[A]], align 4
11 ; CHECK-NEXT: ret void
14 ; First store to the location.
16 %cmp = icmp sgt i32 %D, 42
17 br i1 %cmp, label %if.then, label %ret.end, !prof !0
19 ; Make sure we speculate stores like the following one. It is cheap compared to
20 ; a mispredicated branch.
29 ; Store to a different location.
31 define void @noifconvertstore1(ptr %A1, ptr %A2, i32 %B, i32 %C, i32 %D) {
32 ; CHECK-LABEL: @noifconvertstore1(
34 ; CHECK-NEXT: store i32 [[B:%.*]], ptr [[A1:%.*]], align 4
35 ; CHECK-NEXT: [[CMP:%.*]] = icmp sgt i32 [[D:%.*]], 42
36 ; CHECK-NEXT: br i1 [[CMP]], label [[IF_THEN:%.*]], label [[RET_END:%.*]]
38 ; CHECK-NEXT: store i32 [[C:%.*]], ptr [[A2:%.*]], align 4
39 ; CHECK-NEXT: br label [[RET_END]]
41 ; CHECK-NEXT: ret void
45 %cmp = icmp sgt i32 %D, 42
46 br i1 %cmp, label %if.then, label %ret.end
56 ; This function could store to our address, so we can't repeat the first store a second time.
57 declare void @unknown_fun()
59 define void @noifconvertstore2(ptr %A, i32 %B, i32 %C, i32 %D) {
60 ; CHECK-LABEL: @noifconvertstore2(
62 ; CHECK-NEXT: store i32 [[B:%.*]], ptr [[A:%.*]], align 4
63 ; CHECK-NEXT: call void @unknown_fun()
64 ; CHECK-NEXT: [[CMP6:%.*]] = icmp sgt i32 [[D:%.*]], 42
65 ; CHECK-NEXT: br i1 [[CMP6]], label [[IF_THEN:%.*]], label [[RET_END:%.*]]
67 ; CHECK-NEXT: store i32 [[C:%.*]], ptr [[A]], align 4
68 ; CHECK-NEXT: br label [[RET_END]]
70 ; CHECK-NEXT: ret void
73 ; First store to the location.
75 call void @unknown_fun()
76 %cmp6 = icmp sgt i32 %D, 42
77 br i1 %cmp6, label %if.then, label %ret.end
87 ; Make sure we don't speculate volatile stores.
89 define void @noifconvertstore_volatile(ptr %A, i32 %B, i32 %C, i32 %D) {
90 ; CHECK-LABEL: @noifconvertstore_volatile(
92 ; CHECK-NEXT: store i32 [[B:%.*]], ptr [[A:%.*]], align 4
93 ; CHECK-NEXT: [[CMP6:%.*]] = icmp sgt i32 [[D:%.*]], 42
94 ; CHECK-NEXT: br i1 [[CMP6]], label [[IF_THEN:%.*]], label [[RET_END:%.*]]
96 ; CHECK-NEXT: store volatile i32 [[C:%.*]], ptr [[A]], align 4
97 ; CHECK-NEXT: br label [[RET_END]]
99 ; CHECK-NEXT: ret void
102 ; First store to the location.
104 %cmp6 = icmp sgt i32 %D, 42
105 br i1 %cmp6, label %if.then, label %ret.end
108 store volatile i32 %C, ptr %A
116 ;; Speculate a store, preceded by a local, non-escaping load
117 define i32 @load_before_store_noescape(i64 %i, i32 %b) {
118 ; CHECK-LABEL: @load_before_store_noescape(
120 ; CHECK-NEXT: [[A:%.*]] = alloca [2 x i32], align 8
121 ; CHECK-NEXT: store i64 4294967296, ptr [[A]], align 8
122 ; CHECK-NEXT: [[ARRAYIDX:%.*]] = getelementptr inbounds [2 x i32], ptr [[A]], i64 0, i64 [[I:%.*]]
123 ; CHECK-NEXT: [[TMP0:%.*]] = load i32, ptr [[ARRAYIDX]], align 4
124 ; CHECK-NEXT: [[CMP:%.*]] = icmp slt i32 [[TMP0]], [[B:%.*]]
125 ; CHECK-NEXT: [[SPEC_STORE_SELECT:%.*]] = select i1 [[CMP]], i32 [[B]], i32 [[TMP0]]
126 ; CHECK-NEXT: store i32 [[SPEC_STORE_SELECT]], ptr [[ARRAYIDX]], align 4
127 ; CHECK-NEXT: [[TMP1:%.*]] = load i32, ptr [[A]], align 4
128 ; CHECK-NEXT: [[ARRAYIDX2:%.*]] = getelementptr inbounds [2 x i32], ptr [[A]], i64 0, i64 1
129 ; CHECK-NEXT: [[TMP2:%.*]] = load i32, ptr [[ARRAYIDX2]], align 4
130 ; CHECK-NEXT: [[ADD:%.*]] = add nsw i32 [[TMP1]], [[TMP2]]
131 ; CHECK-NEXT: ret i32 [[ADD]]
134 %a = alloca [2 x i32], align 8
135 store i64 4294967296, ptr %a, align 8
136 %arrayidx = getelementptr inbounds [2 x i32], ptr %a, i64 0, i64 %i
137 %0 = load i32, ptr %arrayidx, align 4
138 %cmp = icmp slt i32 %0, %b
139 br i1 %cmp, label %if.then, label %if.end
142 store i32 %b, ptr %arrayidx, align 4
146 %1 = load i32, ptr %a, align 4
147 %arrayidx2 = getelementptr inbounds [2 x i32], ptr %a, i64 0, i64 1
148 %2 = load i32, ptr %arrayidx2, align 4
149 %add = add nsw i32 %1, %2
153 ;; Don't speculate a store, preceded by a local, escaping load
154 define i32 @load_before_store_escape(i64 %i, i32 %b) {
155 ; CHECK-LABEL: @load_before_store_escape(
157 ; CHECK-NEXT: [[A:%.*]] = alloca [2 x i32], align 8
158 ; CHECK-NEXT: store i64 4294967296, ptr [[A]], align 8
159 ; CHECK-NEXT: call void @fork_some_threads(ptr [[A]])
160 ; CHECK-NEXT: [[ARRAYIDX:%.*]] = getelementptr inbounds [2 x i32], ptr [[A]], i64 0, i64 [[I:%.*]]
161 ; CHECK-NEXT: [[TMP0:%.*]] = load i32, ptr [[ARRAYIDX]], align 4
162 ; CHECK-NEXT: [[CMP:%.*]] = icmp slt i32 [[TMP0]], [[B:%.*]]
163 ; CHECK-NEXT: br i1 [[CMP]], label [[IF_THEN:%.*]], label [[IF_END:%.*]]
165 ; CHECK-NEXT: store i32 [[B]], ptr [[ARRAYIDX]], align 4
166 ; CHECK-NEXT: br label [[IF_END]]
168 ; CHECK-NEXT: [[TMP1:%.*]] = load i32, ptr [[A]], align 4
169 ; CHECK-NEXT: [[ARRAYIDX2:%.*]] = getelementptr inbounds [2 x i32], ptr [[A]], i64 0, i64 1
170 ; CHECK-NEXT: [[TMP2:%.*]] = load i32, ptr [[ARRAYIDX2]], align 4
171 ; CHECK-NEXT: [[ADD:%.*]] = add nsw i32 [[TMP1]], [[TMP2]]
172 ; CHECK-NEXT: call void @join_some_threads()
173 ; CHECK-NEXT: ret i32 [[ADD]]
176 %a = alloca [2 x i32], align 8
177 store i64 4294967296, ptr %a, align 8
178 call void @fork_some_threads(ptr %a)
179 %arrayidx = getelementptr inbounds [2 x i32], ptr %a, i64 0, i64 %i
180 %0 = load i32, ptr %arrayidx, align 4
181 %cmp = icmp slt i32 %0, %b
182 br i1 %cmp, label %if.then, label %if.end
185 store i32 %b, ptr %arrayidx, align 4
189 %1 = load i32, ptr %a, align 4
190 %arrayidx2 = getelementptr inbounds [2 x i32], ptr %a, i64 0, i64 1
191 %2 = load i32, ptr %arrayidx2, align 4
192 %add = add nsw i32 %1, %2
193 call void @join_some_threads()
197 define i64 @load_before_store_noescape_byval(ptr byval([2 x i32]) %a, i64 %i, i32 %b) {
198 ; CHECK-LABEL: @load_before_store_noescape_byval(
200 ; CHECK-NEXT: store i64 -1, ptr [[A:%.*]], align 8
201 ; CHECK-NEXT: [[ARRAYIDX:%.*]] = getelementptr inbounds [2 x i32], ptr [[A]], i64 0, i64 [[I:%.*]]
202 ; CHECK-NEXT: [[V:%.*]] = load i32, ptr [[ARRAYIDX]], align 4
203 ; CHECK-NEXT: [[CMP:%.*]] = icmp slt i32 [[V]], [[B:%.*]]
204 ; CHECK-NEXT: [[SPEC_STORE_SELECT:%.*]] = select i1 [[CMP]], i32 [[B]], i32 [[V]]
205 ; CHECK-NEXT: store i32 [[SPEC_STORE_SELECT]], ptr [[ARRAYIDX]], align 4
206 ; CHECK-NEXT: [[V2:%.*]] = load i64, ptr [[A]], align 8
207 ; CHECK-NEXT: ret i64 [[V2]]
210 store i64 -1, ptr %a, align 8
211 %arrayidx = getelementptr inbounds [2 x i32], ptr %a, i64 0, i64 %i
212 %v = load i32, ptr %arrayidx, align 4
213 %cmp = icmp slt i32 %v, %b
214 br i1 %cmp, label %if.then, label %if.end
217 store i32 %b, ptr %arrayidx, align 4
221 %v2 = load i64, ptr %a, align 8
225 declare noalias ptr @malloc(i64 %size)
227 define i64 @load_before_store_noescape_malloc(i64 %i, i32 %b) {
228 ; CHECK-LABEL: @load_before_store_noescape_malloc(
230 ; CHECK-NEXT: [[A:%.*]] = call ptr @malloc(i64 8)
231 ; CHECK-NEXT: store i64 -1, ptr [[A]], align 8
232 ; CHECK-NEXT: [[ARRAYIDX:%.*]] = getelementptr inbounds [2 x i32], ptr [[A]], i64 0, i64 [[I:%.*]]
233 ; CHECK-NEXT: [[V:%.*]] = load i32, ptr [[ARRAYIDX]], align 4
234 ; CHECK-NEXT: [[CMP:%.*]] = icmp slt i32 [[V]], [[B:%.*]]
235 ; CHECK-NEXT: [[SPEC_STORE_SELECT:%.*]] = select i1 [[CMP]], i32 [[B]], i32 [[V]]
236 ; CHECK-NEXT: store i32 [[SPEC_STORE_SELECT]], ptr [[ARRAYIDX]], align 4
237 ; CHECK-NEXT: [[V2:%.*]] = load i64, ptr [[A]], align 8
238 ; CHECK-NEXT: ret i64 [[V2]]
241 %a = call ptr @malloc(i64 8)
242 store i64 -1, ptr %a, align 8
243 %arrayidx = getelementptr inbounds [2 x i32], ptr %a, i64 0, i64 %i
244 %v = load i32, ptr %arrayidx, align 4
245 %cmp = icmp slt i32 %v, %b
246 br i1 %cmp, label %if.then, label %if.end
249 store i32 %b, ptr %arrayidx, align 4
253 %v2 = load i64, ptr %a, align 8
257 define i64 @load_before_store_noescape_writable(ptr noalias writable dereferenceable(8) %a, i64 %i, i32 %b) {
258 ; CHECK-LABEL: @load_before_store_noescape_writable(
260 ; CHECK-NEXT: store i64 -1, ptr [[A:%.*]], align 8
261 ; CHECK-NEXT: [[ARRAYIDX:%.*]] = getelementptr inbounds [2 x i32], ptr [[A]], i64 0, i64 1
262 ; CHECK-NEXT: [[V:%.*]] = load i32, ptr [[ARRAYIDX]], align 4
263 ; CHECK-NEXT: [[CMP:%.*]] = icmp slt i32 [[V]], [[B:%.*]]
264 ; CHECK-NEXT: [[SPEC_STORE_SELECT:%.*]] = select i1 [[CMP]], i32 [[B]], i32 [[V]]
265 ; CHECK-NEXT: store i32 [[SPEC_STORE_SELECT]], ptr [[ARRAYIDX]], align 4
266 ; CHECK-NEXT: [[V2:%.*]] = load i64, ptr [[A]], align 8
267 ; CHECK-NEXT: ret i64 [[V2]]
270 store i64 -1, ptr %a, align 8
271 %arrayidx = getelementptr inbounds [2 x i32], ptr %a, i64 0, i64 1
272 %v = load i32, ptr %arrayidx, align 4
273 %cmp = icmp slt i32 %v, %b
274 br i1 %cmp, label %if.then, label %if.end
277 store i32 %b, ptr %arrayidx, align 4
281 %v2 = load i64, ptr %a, align 8
285 define i64 @load_before_store_noescape_writable_missing_noalias(ptr writable dereferenceable(8) %a, i64 %i, i32 %b) {
286 ; CHECK-LABEL: @load_before_store_noescape_writable_missing_noalias(
288 ; CHECK-NEXT: store i64 -1, ptr [[A:%.*]], align 8
289 ; CHECK-NEXT: [[ARRAYIDX:%.*]] = getelementptr inbounds [2 x i32], ptr [[A]], i64 0, i64 1
290 ; CHECK-NEXT: [[V:%.*]] = load i32, ptr [[ARRAYIDX]], align 4
291 ; CHECK-NEXT: [[CMP:%.*]] = icmp slt i32 [[V]], [[B:%.*]]
292 ; CHECK-NEXT: br i1 [[CMP]], label [[IF_THEN:%.*]], label [[IF_END:%.*]]
294 ; CHECK-NEXT: store i32 [[B]], ptr [[ARRAYIDX]], align 4
295 ; CHECK-NEXT: br label [[IF_END]]
297 ; CHECK-NEXT: [[V2:%.*]] = load i64, ptr [[A]], align 8
298 ; CHECK-NEXT: ret i64 [[V2]]
301 store i64 -1, ptr %a, align 8
302 %arrayidx = getelementptr inbounds [2 x i32], ptr %a, i64 0, i64 1
303 %v = load i32, ptr %arrayidx, align 4
304 %cmp = icmp slt i32 %v, %b
305 br i1 %cmp, label %if.then, label %if.end
308 store i32 %b, ptr %arrayidx, align 4
312 %v2 = load i64, ptr %a, align 8
316 define i64 @load_before_store_noescape_writable_missing_derefable(ptr noalias writable %a, i64 %i, i32 %b) {
317 ; CHECK-LABEL: @load_before_store_noescape_writable_missing_derefable(
319 ; CHECK-NEXT: store i64 -1, ptr [[A:%.*]], align 8
320 ; CHECK-NEXT: [[ARRAYIDX:%.*]] = getelementptr inbounds [2 x i32], ptr [[A]], i64 0, i64 1
321 ; CHECK-NEXT: [[V:%.*]] = load i32, ptr [[ARRAYIDX]], align 4
322 ; CHECK-NEXT: [[CMP:%.*]] = icmp slt i32 [[V]], [[B:%.*]]
323 ; CHECK-NEXT: br i1 [[CMP]], label [[IF_THEN:%.*]], label [[IF_END:%.*]]
325 ; CHECK-NEXT: store i32 [[B]], ptr [[ARRAYIDX]], align 4
326 ; CHECK-NEXT: br label [[IF_END]]
328 ; CHECK-NEXT: [[V2:%.*]] = load i64, ptr [[A]], align 8
329 ; CHECK-NEXT: ret i64 [[V2]]
332 store i64 -1, ptr %a, align 8
333 %arrayidx = getelementptr inbounds [2 x i32], ptr %a, i64 0, i64 1
334 %v = load i32, ptr %arrayidx, align 4
335 %cmp = icmp slt i32 %v, %b
336 br i1 %cmp, label %if.then, label %if.end
339 store i32 %b, ptr %arrayidx, align 4
343 %v2 = load i64, ptr %a, align 8
347 declare void @fork_some_threads(ptr);
348 declare void @join_some_threads();
350 ; Don't speculate if it's not the only instruction in the block (not counting
352 define i32 @not_alone_in_block(i64 %i, i32 %b) {
353 ; CHECK-LABEL: @not_alone_in_block(
355 ; CHECK-NEXT: [[A:%.*]] = alloca [2 x i32], align 8
356 ; CHECK-NEXT: store i64 4294967296, ptr [[A]], align 8
357 ; CHECK-NEXT: [[ARRAYIDX:%.*]] = getelementptr inbounds [2 x i32], ptr [[A]], i64 0, i64 [[I:%.*]]
358 ; CHECK-NEXT: [[TMP0:%.*]] = load i32, ptr [[ARRAYIDX]], align 4
359 ; CHECK-NEXT: [[CMP:%.*]] = icmp slt i32 [[TMP0]], [[B:%.*]]
360 ; CHECK-NEXT: br i1 [[CMP]], label [[IF_THEN:%.*]], label [[IF_END:%.*]]
362 ; CHECK-NEXT: store i32 [[B]], ptr [[ARRAYIDX]], align 4
363 ; CHECK-NEXT: store i32 [[B]], ptr [[A]], align 4
364 ; CHECK-NEXT: br label [[IF_END]]
366 ; CHECK-NEXT: [[TMP1:%.*]] = load i32, ptr [[A]], align 4
367 ; CHECK-NEXT: [[ARRAYIDX2:%.*]] = getelementptr inbounds [2 x i32], ptr [[A]], i64 0, i64 1
368 ; CHECK-NEXT: [[TMP2:%.*]] = load i32, ptr [[ARRAYIDX2]], align 4
369 ; CHECK-NEXT: [[ADD:%.*]] = add nsw i32 [[TMP1]], [[TMP2]]
370 ; CHECK-NEXT: ret i32 [[ADD]]
373 %a = alloca [2 x i32], align 8
374 store i64 4294967296, ptr %a, align 8
375 %arrayidx = getelementptr inbounds [2 x i32], ptr %a, i64 0, i64 %i
376 %0 = load i32, ptr %arrayidx, align 4
377 %cmp = icmp slt i32 %0, %b
378 br i1 %cmp, label %if.then, label %if.end
381 store i32 %b, ptr %arrayidx, align 4
382 store i32 %b, ptr %a, align 4
386 %1 = load i32, ptr %a, align 4
387 %arrayidx2 = getelementptr inbounds [2 x i32], ptr %a, i64 0, i64 1
388 %2 = load i32, ptr %arrayidx2, align 4
389 %add = add nsw i32 %1, %2
393 define void @wrong_align_store(ptr %A, i32 %B, i32 %C, i32 %D) {
394 ; CHECK-LABEL: @wrong_align_store(
396 ; CHECK-NEXT: store i32 [[B:%.*]], ptr [[A:%.*]], align 4
397 ; CHECK-NEXT: [[CMP:%.*]] = icmp sgt i32 [[D:%.*]], 42
398 ; CHECK-NEXT: br i1 [[CMP]], label [[IF_THEN:%.*]], label [[RET_END:%.*]]
400 ; CHECK-NEXT: store i32 [[C:%.*]], ptr [[A]], align 8
401 ; CHECK-NEXT: br label [[RET_END]]
403 ; CHECK-NEXT: ret void
406 store i32 %B, ptr %A, align 4
407 %cmp = icmp sgt i32 %D, 42
408 br i1 %cmp, label %if.then, label %ret.end
411 store i32 %C, ptr %A, align 8
418 define void @wrong_align_load(i32 %C, i32 %D) {
419 ; CHECK-LABEL: @wrong_align_load(
421 ; CHECK-NEXT: [[A:%.*]] = alloca i32, align 4
422 ; CHECK-NEXT: [[TMP0:%.*]] = load i32, ptr [[A]], align 4
423 ; CHECK-NEXT: [[CMP:%.*]] = icmp sgt i32 [[D:%.*]], 42
424 ; CHECK-NEXT: br i1 [[CMP]], label [[IF_THEN:%.*]], label [[RET_END:%.*]]
426 ; CHECK-NEXT: store i32 [[C:%.*]], ptr [[A]], align 8
427 ; CHECK-NEXT: br label [[RET_END]]
429 ; CHECK-NEXT: ret void
432 %A = alloca i32, align 4
433 load i32, ptr %A, align 4
434 %cmp = icmp sgt i32 %D, 42
435 br i1 %cmp, label %if.then, label %ret.end
438 store i32 %C, ptr %A, align 8
445 ; CHECK: !0 = !{!"branch_weights", i32 3, i32 5}
446 !0 = !{!"branch_weights", i32 3, i32 5}