1 ; RUN: opt -S -passes='require<profile-summary>,function(codegenprepare)' -disable-complex-addr-modes=false -addr-sink-new-phis=true -addr-sink-new-select=true -disable-cgp-delete-phis %s | FileCheck %s --check-prefix=CHECK --check-prefix=CHECK-YES
2 ; RUN: opt -S -passes='require<profile-summary>,function(codegenprepare)' -disable-complex-addr-modes=false -addr-sink-new-phis=false -addr-sink-new-select=true -disable-cgp-delete-phis %s | FileCheck %s --check-prefix=CHECK --check-prefix=CHECK-NO
4 "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128"
5 target triple = "x86_64-unknown-linux-gnu"
7 ; Can we sink for different base if there is no phi for base?
8 define i32 @test1(i1 %cond, ptr %b1, ptr %b2) {
11 %a1 = getelementptr inbounds i64, ptr %b1, i64 5
12 br i1 %cond, label %if.then, label %fallthrough
15 %a2 = getelementptr inbounds i64, ptr %b2, i64 5
20 ; CHECK-NO-LABEL: fallthrough:
23 %c = phi ptr [%a1, %entry], [%a2, %if.then]
24 %v = load i32, ptr %c, align 4
28 ; Can we sink for different base if there is phi for base?
29 define i32 @test2(i1 %cond, ptr %b1, ptr %b2) {
32 %a1 = getelementptr inbounds i64, ptr %b1, i64 5
33 br i1 %cond, label %if.then, label %fallthrough
36 %a2 = getelementptr inbounds i64, ptr %b2, i64 5
40 ; CHECK: getelementptr inbounds i8, {{.+}} 40
41 %b = phi ptr [%b1, %entry], [%b2, %if.then]
42 %c = phi ptr [%a1, %entry], [%a2, %if.then]
43 %v = load i32, ptr %c, align 4
47 ; Can we sink for different base if there is phi for base but not valid one?
48 define i32 @test3(i1 %cond, ptr %b1, ptr %b2) {
51 %a1 = getelementptr inbounds i64, ptr %b1, i64 5
52 br i1 %cond, label %if.then, label %fallthrough
55 %a2 = getelementptr inbounds i64, ptr %b2, i64 5
60 ; CHECK-NO-LABEL: fallthrough:
64 %b = phi ptr [%b2, %entry], [%b1, %if.then]
65 %c = phi ptr [%a1, %entry], [%a2, %if.then]
66 %v = load i32, ptr %c, align 4
70 ; Can we sink for different base if both addresses are in the same block?
71 define i32 @test4(i1 %cond, ptr %b1, ptr %b2) {
74 %a1 = getelementptr inbounds i64, ptr %b1, i64 5
75 %a2 = getelementptr inbounds i64, ptr %b2, i64 5
76 br i1 %cond, label %if.then, label %fallthrough
83 ; CHECK-NO-LABEL: fallthrough:
86 %c = phi ptr [%a1, %entry], [%a2, %if.then]
87 %v = load i32, ptr %c, align 4
91 ; Can we sink for different base if there is phi for base?
92 ; Both addresses are in the same block.
93 define i32 @test5(i1 %cond, ptr %b1, ptr %b2) {
96 %a1 = getelementptr inbounds i64, ptr %b1, i64 5
97 %a2 = getelementptr inbounds i64, ptr %b2, i64 5
98 br i1 %cond, label %if.then, label %fallthrough
101 br label %fallthrough
104 ; CHECK: getelementptr inbounds i8, {{.+}} 40
105 %b = phi ptr [%b1, %entry], [%b2, %if.then]
106 %c = phi ptr [%a1, %entry], [%a2, %if.then]
107 %v = load i32, ptr %c, align 4
111 ; Can we sink for different base if there is phi for base but not valid one?
112 ; Both addresses are in the same block.
113 define i32 @test6(i1 %cond, ptr %b1, ptr %b2) {
114 ; CHECK-LABEL: @test6
116 %a1 = getelementptr inbounds i64, ptr %b1, i64 5
117 %a2 = getelementptr inbounds i64, ptr %b2, i64 5
118 br i1 %cond, label %if.then, label %fallthrough
121 br label %fallthrough
124 ; CHECK-YES: sunk_phi
125 ; CHECK-NO-LABEL: fallthrough:
128 ; CHECK-NO-NEXT: load
129 %b = phi ptr [%b2, %entry], [%b1, %if.then]
130 %c = phi ptr [%a1, %entry], [%a2, %if.then]
131 %v = load i32, ptr %c, align 4
135 ; case with a loop. No phi node.
136 define i32 @test7(i32 %N, i1 %cond, ptr %b1, ptr %b2) {
137 ; CHECK-LABEL: @test7
139 %a1 = getelementptr inbounds i64, ptr %b1, i64 5
144 ; CHECK-YES: sunk_phi
145 %iv = phi i32 [0, %entry], [%iv.inc, %fallthrough]
146 %c3 = phi ptr [%a1, %entry], [%c, %fallthrough]
147 br i1 %cond, label %if.then, label %fallthrough
150 %a2 = getelementptr inbounds i64, ptr %b2, i64 5
151 br label %fallthrough
154 ; CHECK-YES: sunk_phi
155 ; CHECK-NO-LABEL: fallthrough:
157 ; CHECK-NO-NEXT: load
158 %c = phi ptr [%c3, %loop], [%a2, %if.then]
159 %v = load volatile i32, ptr %c, align 4
160 %iv.inc = add i32 %iv, 1
161 %cmp = icmp slt i32 %iv.inc, %N
162 br i1 %cmp, label %loop, label %exit
168 ; case with a loop. There is phi node.
169 define i32 @test8(i32 %N, i1 %cond, ptr %b1, ptr %b2) {
170 ; CHECK-LABEL: @test8
172 %a1 = getelementptr inbounds i64, ptr %b1, i64 5
176 %iv = phi i32 [0, %entry], [%iv.inc, %fallthrough]
177 %c3 = phi ptr [%a1, %entry], [%c, %fallthrough]
178 %b3 = phi ptr [%b1, %entry], [%b, %fallthrough]
179 br i1 %cond, label %if.then, label %fallthrough
182 %a2 = getelementptr inbounds i64, ptr %b2, i64 5
183 br label %fallthrough
186 ; CHECK: getelementptr inbounds i8, {{.+}} 40
187 %c = phi ptr [%c3, %loop], [%a2, %if.then]
188 %b = phi ptr [%b3, %loop], [%b2, %if.then]
189 %v = load volatile i32, ptr %c, align 4
190 %iv.inc = add i32 %iv, 1
191 %cmp = icmp slt i32 %iv.inc, %N
192 br i1 %cmp, label %loop, label %exit
198 ; case with a loop. There is phi node but it does not fit.
199 define i32 @test9(i32 %N, i1 %cond, ptr %b1, ptr %b2) {
200 ; CHECK-LABEL: @test9
202 %a1 = getelementptr inbounds i64, ptr %b1, i64 5
207 ; CHECK-YES: sunk_phi
208 %iv = phi i32 [0, %entry], [%iv.inc, %fallthrough]
209 %c3 = phi ptr [%a1, %entry], [%c, %fallthrough]
210 %b3 = phi ptr [%b1, %entry], [%b2, %fallthrough]
211 br i1 %cond, label %if.then, label %fallthrough
214 %a2 = getelementptr inbounds i64, ptr %b2, i64 5
215 br label %fallthrough
218 ; CHECK-YES: sunk_phi
219 ; CHECK-NO-LABEL: fallthrough:
222 ; CHECK-NO-NEXT: load
223 %c = phi ptr [%c3, %loop], [%a2, %if.then]
224 %b = phi ptr [%b3, %loop], [%b2, %if.then]
225 %v = load volatile i32, ptr %c, align 4
226 %iv.inc = add i32 %iv, 1
227 %cmp = icmp slt i32 %iv.inc, %N
228 br i1 %cmp, label %loop, label %exit
234 ; Case through a loop. No phi node.
235 define i32 @test10(i32 %N, i1 %cond, ptr %b1, ptr %b2) {
236 ; CHECK-LABEL: @test10
238 %a1 = getelementptr inbounds i64, ptr %b1, i64 5
239 br i1 %cond, label %if.then, label %fallthrough
242 %a2 = getelementptr inbounds i64, ptr %b2, i64 5
243 br label %fallthrough
246 ; CHECK-YES: sunk_phi
247 ; CHECK-NO-LABEL: fallthrough:
250 %c = phi ptr [%a1, %entry], [%a2, %if.then]
254 %iv = phi i32 [0, %fallthrough], [%iv.inc, %loop]
255 %iv.inc = add i32 %iv, 1
256 %cmp = icmp slt i32 %iv.inc, %N
257 br i1 %cmp, label %loop, label %exit
260 ; CHECK-YES: sunkaddr
261 %v = load volatile i32, ptr %c, align 4
265 ; Case through a loop. There is a phi.
266 define i32 @test11(i32 %N, i1 %cond, ptr %b1, ptr %b2) {
267 ; CHECK-LABEL: @test11
269 %a1 = getelementptr inbounds i64, ptr %b1, i64 5
270 br i1 %cond, label %if.then, label %fallthrough
273 %a2 = getelementptr inbounds i64, ptr %b2, i64 5
274 br label %fallthrough
280 %c = phi ptr [%a1, %entry], [%a2, %if.then]
281 %b = phi ptr [%b1, %entry], [%b2, %if.then]
285 %iv = phi i32 [0, %fallthrough], [%iv.inc, %loop]
286 %iv.inc = add i32 %iv, 1
287 %cmp = icmp slt i32 %iv.inc, %N
288 br i1 %cmp, label %loop, label %exit
292 %v = load volatile i32, ptr %c, align 4
296 ; Complex case with address value from previous iteration.
297 define i32 @test12(i32 %N, i1 %cond, ptr %b1, ptr %b2, ptr %b3) {
298 ; CHECK-LABEL: @test12
300 %a1 = getelementptr inbounds i64, ptr %b1, i64 5
305 ; CHECK-YES: sunk_phi
310 %iv = phi i32 [0, %entry], [%iv.inc, %backedge]
311 %c3 = phi ptr [%a1, %entry], [%c, %backedge]
312 %b4 = phi ptr [%b1, %entry], [%b5, %backedge]
313 br i1 %cond, label %if.then, label %fallthrough
316 %a2 = getelementptr inbounds i64, ptr %b2, i64 5
317 br label %fallthrough
320 ; CHECK-LABEL: fallthrough:
321 ; CHECK-YES: sunk_phi
324 ; CHECK-NO-NEXT: load
325 %c = phi ptr [%c3, %loop], [%a2, %if.then]
326 %b6 = phi ptr [%b4, %loop], [%b2, %if.then]
327 %v = load volatile i32, ptr %c, align 4
328 %a4 = getelementptr inbounds i64, ptr %b4, i64 5
329 %cmp = icmp slt i32 %iv, 20
330 br i1 %cmp, label %backedge, label %if.then.2
336 %b5 = phi ptr [%b4, %fallthrough], [%b6, %if.then.2]
337 %iv.inc = add i32 %iv, 1
338 %cmp2 = icmp slt i32 %iv.inc, %N
339 br i1 %cmp2, label %loop, label %exit
345 %struct.S = type {i32, i32}
347 define i32 @test13(i1 %cond, ptr %b1, ptr %b2, i64 %Index) {
348 ; CHECK-LABEL: @test13
350 %a1 = getelementptr inbounds %struct.S, ptr %b1, i64 %Index, i32 1
351 br i1 %cond, label %if.then, label %fallthrough
354 %i2 = mul i64 %Index, 2
355 %a2 = getelementptr inbounds %struct.S, ptr %b2, i64 %Index, i32 1
356 br label %fallthrough
359 ; CHECK-YES: sunk_phi
360 ; CHECK-NO-LABEL: fallthrough:
362 ; CHECK-NO-NEXT: load
363 %a = phi ptr [%a1, %entry], [%a2, %if.then]
364 %v = load i32, ptr %a, align 4
368 ; Select of Select case.
369 define i64 @test14(i1 %c1, i1 %c2, ptr %b1, ptr %b2, ptr %b3) {
370 ; CHECK-LABEL: @test14
372 ; CHECK-LABEL: entry:
373 %g1 = getelementptr inbounds i64, ptr %b1, i64 5
374 %g2 = getelementptr inbounds i64, ptr %b2, i64 5
375 %g3 = getelementptr inbounds i64, ptr %b3, i64 5
376 %s1 = select i1 %c1, ptr %g1, ptr %g2
377 %s2 = select i1 %c2, ptr %s1, ptr %g3
379 %v = load i64 , ptr %s2, align 8
383 ; Select of Phi case.
384 define i64 @test15(i1 %c1, i1 %c2, ptr %b1, ptr %b2, ptr %b3) {
385 ; CHECK-LABEL: @test15
387 %g1 = getelementptr inbounds i64, ptr %b1, i64 5
388 %g2 = getelementptr inbounds i64, ptr %b2, i64 5
389 %g3 = getelementptr inbounds i64, ptr %b3, i64 5
390 br i1 %c1, label %if.then, label %fallthrough
393 br label %fallthrough
396 ; CHECK-LABEL: fallthrough:
397 %p1 = phi ptr [%g1, %entry], [%g2, %if.then]
398 %s1 = select i1 %c2, ptr %p1, ptr %g3
399 ; CHECK-YES: sunkaddr
401 ; CHECK-NO-NEXT: select
402 ; CHECK-NO-NEXT: load
403 %v = load i64 , ptr %s1, align 8
407 ; Select of Phi case. Phi exists
408 define i64 @test16(i1 %c1, i1 %c2, ptr %b1, ptr %b2, ptr %b3) {
409 ; CHECK-LABEL: @test16
411 %g1 = getelementptr inbounds i64, ptr %b1, i64 5
412 %g2 = getelementptr inbounds i64, ptr %b2, i64 5
413 %g3 = getelementptr inbounds i64, ptr %b3, i64 5
414 br i1 %c1, label %if.then, label %fallthrough
417 br label %fallthrough
420 ; CHECK-LABEL: fallthrough:
421 %p = phi ptr [%b1, %entry], [%b2, %if.then]
422 %p1 = phi ptr [%g1, %entry], [%g2, %if.then]
423 %s1 = select i1 %c2, ptr %p1, ptr %g3
425 %v = load i64 , ptr %s1, align 8
429 ; Phi of Select case.
430 define i64 @test17(i1 %c1, i1 %c2, ptr %b1, ptr %b2, ptr %b3) {
431 ; CHECK-LABEL: @test17
433 %g1 = getelementptr inbounds i64, ptr %b1, i64 5
434 %g2 = getelementptr inbounds i64, ptr %b2, i64 5
435 %g3 = getelementptr inbounds i64, ptr %b3, i64 5
436 %s1 = select i1 %c2, ptr %g1, ptr %g2
437 br i1 %c1, label %if.then, label %fallthrough
440 br label %fallthrough
443 ; CHECK-LABEL: fallthrough:
444 %p1 = phi ptr [%s1, %entry], [%g3, %if.then]
445 ; CHECK-YES: sunkaddr
447 ; CHECK-NO-NEXT: load
448 %v = load i64 , ptr %p1, align 8
452 ; The same two addr modes by different paths
453 define i32 @test18(i1 %cond1, i1 %cond2, ptr %b1, ptr %b2) {
454 ; CHECK-LABEL: @test18
456 %g1 = getelementptr inbounds i64, ptr %b2, i64 5
457 br i1 %cond1, label %if.then1, label %if.then2
460 %g2 = getelementptr inbounds i64, ptr %b1, i64 5
461 br label %fallthrough
464 br i1 %cond2, label %fallthrough, label %if.then3
467 br label %fallthrough
470 ; CHECK-YES: sunk_phi
471 ; CHECK-NO-LABEL: fallthrough:
473 ; CHECK-NO-NEXT: load
474 %c = phi ptr [%g2, %if.then1], [%g1, %if.then2], [%g1, %if.then3]
475 %v1 = load i32, ptr %c, align 4
476 %g1_1 = getelementptr inbounds i64, ptr %b2, i64 5
477 %v2 = load i32, ptr %g1_1, align 4
478 %v = add i32 %v1, %v2
482 ; Different types but null is the first?
483 define i32 @test19(i1 %cond1, i1 %cond2, ptr %b2, ptr %b1) {
484 ; CHECK-LABEL: @test19
486 %g1 = getelementptr inbounds i64, ptr %b2, i64 5
487 %bc1 = bitcast ptr %g1 to ptr
488 br i1 %cond1, label %if.then1, label %if.then2
491 %g2 = getelementptr inbounds i8, ptr %b1, i64 40
492 %bc2 = bitcast ptr %g2 to ptr
493 br label %fallthrough
496 %bc1_1 = bitcast ptr %g1 to ptr
497 br i1 %cond2, label %fallthrough, label %if.then3
500 %g3 = getelementptr inbounds i64, ptr null, i64 5
501 %bc1_2 = bitcast ptr %g3 to ptr
502 br label %fallthrough
505 ; CHECK-NOT: sunk_phi
506 %c = phi ptr [%bc2, %if.then1], [%bc1_1, %if.then2], [%bc1_2, %if.then3]
507 %v1 = load i32, ptr %c, align 4
508 %g1_1 = getelementptr inbounds i64, ptr %b2, i64 5
509 %bc1_1_1 = bitcast ptr %g1_1 to ptr
510 %v2 = load i32, ptr %bc1_1_1, align 4
511 %v = add i32 %v1, %v2