1 ; RUN: opt %s -scalarrepl -S | FileCheck %s
2 ; Test promotion of allocas that have phis and select users.
3 target datalayout = "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-n8:16:32:64"
4 target triple = "x86_64-apple-darwin10.2"
6 %struct.X = type { i32 }
7 %PairTy = type {i32, i32}
10 ; CHECK: %a.0 = alloca i32
11 ; CHECK: %b.0 = alloca i32
12 define i32 @test1(i32 %x) nounwind readnone ssp {
14 %a = alloca %struct.X, align 8 ; <%struct.X*> [#uses=2]
15 %b = alloca %struct.X, align 8 ; <%struct.X*> [#uses=2]
16 %0 = getelementptr inbounds %struct.X* %a, i64 0, i32 0 ; <i32*> [#uses=1]
17 store i32 1, i32* %0, align 8
18 %1 = getelementptr inbounds %struct.X* %b, i64 0, i32 0 ; <i32*> [#uses=1]
19 store i32 2, i32* %1, align 8
20 %2 = icmp eq i32 %x, 0 ; <i1> [#uses=1]
21 %p.0 = select i1 %2, %struct.X* %b, %struct.X* %a ; <%struct.X*> [#uses=1]
22 %3 = getelementptr inbounds %struct.X* %p.0, i64 0, i32 0 ; <i32*> [#uses=1]
23 %4 = load i32* %3, align 8 ; <i32> [#uses=1]
28 ; CHECK: %X.ld = phi i32 [ 1, %entry ], [ 2, %T ]
29 ; CHECK-NEXT: ret i32 %X.ld
30 define i32 @test2(i1 %c) {
32 %A = alloca {i32, i32}
33 %B = getelementptr {i32, i32}* %A, i32 0, i32 0
35 br i1 %c, label %T, label %F
37 %C = getelementptr {i32, i32}* %A, i32 0, i32 1
41 %X = phi i32* [%B, %entry], [%C, %T]
47 ; CHECK-NEXT: %Q = select i1 %c, i32 1, i32 2
48 ; CHECK-NEXT: ret i32 %Q
50 define i32 @test3(i1 %c) {
51 %A = alloca {i32, i32}
52 %B = getelementptr {i32, i32}* %A, i32 0, i32 0
54 %C = getelementptr {i32, i32}* %A, i32 0, i32 1
57 %X = select i1 %c, i32* %B, i32* %C
62 ;; We can't scalarize this, a use of the select is not an element access.
63 define i64 @test4(i1 %c) {
67 ; CHECK: %A = alloca %PairTy
68 %B = getelementptr %PairTy* %A, i32 0, i32 0
70 %C = getelementptr %PairTy* %A, i32 0, i32 1
73 %X = select i1 %c, i32* %B, i32* %C
74 %Y = bitcast i32* %X to i64*
81 ;; Tests for promoting allocas used by selects.
85 define i32 @test5(i32 *%P) nounwind readnone ssp {
87 %b = alloca i32, align 8
88 store i32 2, i32* %b, align 8
90 ;; Select on constant condition should be folded.
91 %p.0 = select i1 false, i32* %b, i32* %P
92 store i32 123, i32* %p.0
94 %r = load i32* %b, align 8
98 ; CHECK: store i32 123, i32* %P
102 define i32 @test6(i32 %x, i1 %c) nounwind readnone ssp {
103 %a = alloca i32, align 8
104 %b = alloca i32, align 8
105 store i32 1, i32* %a, align 8
106 store i32 2, i32* %b, align 8
107 %p.0 = select i1 %c, i32* %b, i32* %a
108 %r = load i32* %p.0, align 8
111 ; CHECK-NEXT: %r = select i1 %c, i32 2, i32 1
112 ; CHECK-NEXT: ret i32 %r
115 ; Verify that the loads happen where the loads are, not where the select is.
116 define i32 @test7(i32 %x, i1 %c) nounwind readnone ssp {
117 %a = alloca i32, align 8
118 %b = alloca i32, align 8
121 %p.0 = select i1 %c, i32* %b, i32* %a
125 %r = load i32* %p.0, align 8
128 ; CHECK-NOT: alloca i32
129 ; CHECK: %r = select i1 %c, i32 2, i32 0
133 ;; Promote allocs that are PHI'd together by moving the loads.
134 define i32 @test8(i32 %x) nounwind readnone ssp {
136 ; CHECK-NOT: load i32
137 ; CHECK-NOT: store i32
138 ; CHECK: %p.0.ld = phi i32 [ 2, %entry ], [ 1, %T ]
139 ; CHECK-NEXT: ret i32 %p.0.ld
141 %a = alloca i32, align 8
142 %b = alloca i32, align 8
143 store i32 1, i32* %a, align 8
144 store i32 2, i32* %b, align 8
145 %c = icmp eq i32 %x, 0
146 br i1 %c, label %T, label %Cont
150 %p.0 = phi i32* [%b, %entry],[%a, %T]
151 %r = load i32* %p.0, align 8